From 797798bd084ab62490b0ef38582bd57ee1f59c00 Mon Sep 17 00:00:00 2001 From: Adam Porter Date: Sun, 21 Jul 2019 17:21:48 -0500 Subject: [PATCH] Change: (org-ql-query) Accept comparator function to :sort --- README.org | 3 ++- org-ql.el | 16 +++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/README.org b/README.org index 7d1cdd0..34dde96 100644 --- a/README.org +++ b/README.org @@ -210,7 +210,7 @@ Return items matching ~QUERY~ in ~BUFFERS-OR-FILES~. If ~NARROW~ is non-nil, buffers are not widened. -~SORT~ is either nil, in which case items are not sorted; or one or a list of defined ~org-ql~ sorting methods: ~date~, ~deadline~, ~scheduled~, ~todo~, and ~priority~. +~SORT~ is either nil, in which case items are not sorted; or one or a list of defined ~org-ql~ sorting methods (~date~, ~deadline~, ~scheduled~, ~todo~, or ~priority~); or a user-defined comparator function that accepts two items as arguments and returns nil or non-nil. **** Macro: ~org-ql~ @@ -243,6 +243,7 @@ If ~MARKERS~ is non-nil, ~org-agenda-ng--add-markers~ is used to add markers to *Changed* + ~(regexp)~ selector accepts multiple regexps to test. ++ The ~:sort~ argument to ~org-ql~, ~org-ql-query~, etc. now also accepts a comparator function by which to sort items. *Fixed* + Handle date ranges in date-based selectors. (Thanks to [[https://github.com/codygman][Cody Goodman]], [[https://github.com/swflint][Samuel W. Flint]], and [[https://github.com/vikasrawal][Vikas Rawal]].) diff --git a/org-ql.el b/org-ql.el index 39f9221..363b8de 100644 --- a/org-ql.el +++ b/org-ql.el @@ -108,9 +108,11 @@ with point at the beginning of its heading. It is passed to `org-ql-query' as a lambda. By default, `org-element-headline-parser' is called to return an Org element. -SORT is a user defined sorting function, or an unquoted list of -one or more sorting methods, including: `date', `deadline', -`scheduled', `todo', and `priority'. +SORT is either nil, in which case items are not sorted; or one or +a list of predefined `org-ql' sorting methods (`date', `deadline', +`scheduled', `todo', or `priority'; or a user-defined comparator +function that accepts two items as arguments and returns nil or +non-nil. If NARROW is non-nil, query will run without widening the buffer (the default is to widen and search the entire buffer). @@ -158,8 +160,10 @@ compatible with Org Agenda code. If NARROW is non-nil, buffers are not widened. SORT is either nil, in which case items are not sorted; or one or -a list of defined `org-ql' sorting methods: `date', `deadline', -`scheduled', `todo', and `priority'." +a list of defined `org-ql' sorting methods (`date', `deadline', +`scheduled', `todo', or `priority'); or a user-defined comparator +function that accepts two items as arguments and returns nil or +non-nil." (declare (indent defun)) (-let* ((buffers (->> (cl-typecase buffers-or-files (null (list (current-buffer))) @@ -197,6 +201,8 @@ a list of defined `org-ql' sorting methods: `date', `deadline', always (memq elem '(date deadline scheduled todo priority))))) ;; Default sorting functions (org-ql--sort-by items sort)) + ;; Sort by user-given comparator. + ((pred functionp) (sort items sort)) (_ (user-error "SORT must be either nil, or one or a list of the defined sorting methods (see documentation)"))))) (defun org-ql--query-predicate (query)