branch: externals/org
commit 9b5a67fd52a65301e214c59b9326951337cd1072
Author: Morgan Smith <[email protected]>
Commit: Ihor Radchenko <[email protected]>

    org-entries-lessp: Simplify and add error handling
    
    * lisp/org-agenda.el (org-entries-lessp): Simplify.
    Fix checkdoc warning.
    Add explicit error for trying to use `org-agenda-cmp-user-defined'
    when it is not a function.
    Add explicit error for unknown sorting strategies.
---
 lisp/org-agenda.el | 110 +++++++++++++++++++++++------------------------------
 1 file changed, 48 insertions(+), 62 deletions(-)

diff --git a/lisp/org-agenda.el b/lisp/org-agenda.el
index 0c530e2ffd..8c819514a5 100644
--- a/lisp/org-agenda.el
+++ b/lisp/org-agenda.el
@@ -7603,68 +7603,54 @@ their type."
          ((and (not ha) hb) +1))))
 
 (defun org-entries-lessp (a b)
-  "Predicate for sorting agenda entries."
-  ;; The following variables will be used when the form is evaluated.
-  ;; So even though the compiler complains, keep them.
-  (let ((ss org-agenda-sorting-strategy-selected))
-    (org-dlet
-       ((timestamp-up    (and (org-em 'timestamp-up 'timestamp-down ss)
-                              (org-cmp-ts a b "")))
-        (timestamp-down  (if timestamp-up (- timestamp-up) nil))
-        (scheduled-up    (and (org-em 'scheduled-up 'scheduled-down ss)
-                              (org-cmp-ts a b "scheduled")))
-        (scheduled-down  (if scheduled-up (- scheduled-up) nil))
-        (deadline-up     (and (org-em 'deadline-up 'deadline-down ss)
-                              (org-cmp-ts a b "deadline")))
-        (deadline-down   (if deadline-up (- deadline-up) nil))
-        (tsia-up         (and (org-em 'tsia-up 'tsia-down ss)
-                              (org-cmp-ts a b "timestamp_ia")))
-        (tsia-down       (if tsia-up (- tsia-up) nil))
-        (ts-up           (and (org-em 'ts-up 'ts-down ss)
-                              (org-cmp-ts a b "timestamp")))
-        (ts-down         (if ts-up (- ts-up) nil))
-        (time-up         (and (org-em 'time-up 'time-down ss)
-                              (org-cmp-time a b)))
-        (time-down       (if time-up (- time-up) nil))
-        (stats-up        (and (org-em 'stats-up 'stats-down ss)
-                              (org-cmp-values a b 'org-stats)))
-        (stats-down      (if stats-up (- stats-up) nil))
-        (priority-up     (and (org-em 'priority-up 'priority-down ss)
-                              (org-cmp-values a b 'priority)))
-        (priority-down   (if priority-up (- priority-up) nil))
-        (urgency-up     (and (org-em 'urgency-up 'urgency-down ss)
-                             (org-cmp-values a b 'urgency)))
-        (urgency-down   (if urgency-up (- urgency-up) nil))
-        (effort-up       (and (org-em 'effort-up 'effort-down ss)
-                              (org-cmp-effort a b)))
-        (effort-down     (if effort-up (- effort-up) nil))
-        (category-up     (and (or (org-em 'category-up 'category-down ss)
-                                  (memq 'category-keep ss))
-                              (org-cmp-category a b)))
-        (category-down   (if category-up (- category-up) nil))
-        (category-keep   (if category-up +1 nil))
-        (tag-up          (and (org-em 'tag-up 'tag-down ss)
-                              (org-cmp-tag a b)))
-        (tag-down        (if tag-up (- tag-up) nil))
-        (todo-state-up   (and (org-em 'todo-state-up 'todo-state-down ss)
-                              (org-cmp-todo-state a b)))
-        (todo-state-down (if todo-state-up (- todo-state-up) nil))
-        (habit-up        (and (org-em 'habit-up 'habit-down ss)
-                              (org-cmp-habit-p a b)))
-        (habit-down      (if habit-up (- habit-up) nil))
-        (alpha-up        (and (org-em 'alpha-up 'alpha-down ss)
-                              (org-cmp-alpha a b)))
-        (alpha-down      (if alpha-up (- alpha-up) nil))
-        (need-user-cmp   (org-em 'user-defined-up 'user-defined-down ss))
-        user-defined-up user-defined-down)
-      (when (and need-user-cmp org-agenda-cmp-user-defined
-                (functionp org-agenda-cmp-user-defined))
-       (setq user-defined-up
-             (funcall org-agenda-cmp-user-defined a b)
-             user-defined-down (if user-defined-up (- user-defined-up) nil)))
-      (cdr (assoc
-           (eval (cons 'or org-agenda-sorting-strategy-selected) t)
-           '((-1 . t) (1 . nil) (nil . nil)))))))
+  "Predicate for sorting agenda entries A and B."
+  (catch :org-entries-lessp-return
+    (dolist (strategy org-agenda-sorting-strategy-selected)
+      (when-let*
+          ((result
+            (cl-case strategy
+              (timestamp-up    (org-cmp-ts a b ""))
+              (timestamp-down  (org-cmp-ts b a ""))
+              (scheduled-up    (org-cmp-ts a b "scheduled"))
+              (scheduled-down  (org-cmp-ts b a "scheduled"))
+              (deadline-up     (org-cmp-ts a b "deadline"))
+              (deadline-down   (org-cmp-ts b a "deadline"))
+              (tsia-up         (org-cmp-ts a b "timestamp_ia"))
+              (tsia-down       (org-cmp-ts b a "timestamp_ia"))
+              (ts-up           (org-cmp-ts a b "timestamp"))
+              (ts-down         (org-cmp-ts b a "timestamp"))
+              (time-up         (org-cmp-time a b))
+              (time-down       (org-cmp-time b a))
+              (stats-up        (org-cmp-values a b 'org-stats))
+              (stats-down      (org-cmp-values b a 'org-stats))
+              (priority-up     (org-cmp-values a b 'priority))
+              (priority-down   (org-cmp-values b a 'priority))
+              (urgency-up      (org-cmp-values a b 'urgency))
+              (urgency-down    (org-cmp-values b a 'urgency))
+              (effort-up       (org-cmp-effort a b))
+              (effort-down     (org-cmp-effort b a))
+              (category-up     (org-cmp-category a b))
+              (category-down   (org-cmp-category b a))
+              (category-keep   (and (org-cmp-category a b) +1))
+              (tag-up          (org-cmp-tag a b))
+              (tag-down        (org-cmp-tag b a))
+              (todo-state-up   (org-cmp-todo-state a b))
+              (todo-state-down (org-cmp-todo-state b a))
+              (habit-up        (org-cmp-habit-p a b))
+              (habit-down      (org-cmp-habit-p b a))
+              (alpha-up        (org-cmp-alpha a b))
+              (alpha-down      (org-cmp-alpha b a))
+              (user-defined-up (unless (functionp org-agenda-cmp-user-defined)
+                                 (error "Please set 
`org-agenda-cmp-user-defined' to a function or remove `user-defined-up' from 
`org-agenda-sorting-strategy'"))
+                               (funcall org-agenda-cmp-user-defined a b))
+              (user-defined-down (unless (functionp 
org-agenda-cmp-user-defined)
+                                   (error "Please set 
`org-agenda-cmp-user-defined' to a function or remove `user-defined-down' from 
`org-agenda-sorting-strategy'"))
+                                 (funcall org-agenda-cmp-user-defined b a))
+              (t (error "Invalid value %S in `org-agenda-sorting-strategy'" 
strategy)))))
+        (cond
+         ((eq -1 result) (throw :org-entries-lessp-return t))
+         ((eq 1 result) (throw :org-entries-lessp-return nil)))))
+    nil))
 
 ;;; Agenda restriction lock
 

Reply via email to