ahmed-shariff / org-roam-ql

Query language for org-roam
GNU General Public License v3.0
90 stars 5 forks source link

Add reversed short functions #14

Closed lhernanz closed 2 months ago

lhernanz commented 2 months ago

Thanks for this amazing package!

May I suggest that you would add reverse sort functions for when you want to inverse the order?

The following code would automatically add a reverse function for every slot named "function-reversed"

  (defun org-roam-ql--reverse-sort-function-for-slot (slot-name comparison-function-name)
    "Add a reverse sort function of SLOT-NAME of org-roam-node.
COMPARISON-FUNCTION-NAME is the symbol of function.
DOCSTRING is the documentation string to use for the function."
    (let ((getter (intern-soft
                   (format "org-roam-node-%s" slot-name))))
      (org-roam-ql-register-sort-fn
       (concat slot-name "-reversed")
       `(lambda (node1 node2)
          (,comparison-function-name (,getter node2) (,getter node1))))))

  (dolist (sort-function
           '(("title" . string<)
             ("file" . string<)
             ("file-title" . string<)
             ("level" . <)
             ("point" . <)
             ("scheduled" . org-roam-ql--sort-time-less)
             ("deadline" . org-roam-ql--sort-time-less)
             ("file-atime" . org-roam-ql--sort-time-less)
             ("file-mtime" . org-roam-ql--sort-time-less)))
    (org-roam-ql--sort-function-for-slot (car sort-function) (cdr sort-function))
    (org-roam-ql--reverse-sort-function-for-slot (car sort-function) (cdr sort-function))
    )
lhernanz commented 2 months ago

Another alternative would be to extend the current org-roam-ql--sort-function-for-slot function so that it always add one function and its reverse. I would like that alternative more as it enables the user to benefit from that capability for all the new slot functions that he might create in the future.

ahmed-shariff commented 2 months ago

Hey, I like this idea. Let me try whip something up.

ahmed-shariff commented 2 months ago

9071426d97b329bb98bc54b5287a322dc3814d95 has that. Try that and let me know what you think.

lhernanz commented 2 months ago

Thanks!

This is exactly what I had in mind on my second comment. It seems to work well.