Skip to content

Instantly share code, notes, and snippets.

@JohnL4
Last active January 6, 2025 20:08
Show Gist options
  • Save JohnL4/4ddd2ec185b8a9b948db6d62edb9d32d to your computer and use it in GitHub Desktop.
Save JohnL4/4ddd2ec185b8a9b948db6d62edb9d32d to your computer and use it in GitHub Desktop.
Sort org-mode Todo items better (so high-priority, more-recently-scheduled items float to the top).
(defun my-org-agenda-todo-sort (a b)
"Function should only sort TODO items; since I can't return ``unsortable'' for things that don't compare, I just
return 0 and hope for the best. Seems to be working so far. Higher-priority and more-recently-scheduled items
have higher urgency."
(if (string-match "\\(Sched\\.\\s-*\\([0-9]+\\)x\\|Scheduled\\):\\s-+\\S-+ \\[#\\([ABC]\\)\\]" a)
(let ((a-sched-days (string-to-number (if (null (match-string 2 a)) "0" (match-string 2 a))))
(a-priority (match-string 3 a)))
(if (string-match "\\(Sched\\.\\s-*\\([0-9]+\\)x\\|Scheduled\\):\\s-+\\S-+ \\[#\\([ABC]\\)\\]" b)
(let ((b-sched-days (string-to-number (if (null (match-string 2 b)) "0" (match-string 2 b))))
(b-priority (match-string 3 b)))
;;(message "Agenda item a of type %s: %s" (type-of a) a)
;;(message "Agenda item b of type %s: %s" (type-of b) b)
;;(message "a-priority: %s; b-priority: %s; a-sched-days: %s; b-sched-days: %s"
;; a-priority b-priority a-sched-days b-sched-days)
(cond ((string< a-priority b-priority) 1)
((string> a-priority b-priority) -1)
(t (cond ((< a-sched-days b-sched-days) 1)
((> a-sched-days b-sched-days) -1)
(t 0)))))
0
)
)
0
)
)
(setq org-agenda-cmp-user-defined 'my-org-agenda-todo-sort)
(setq org-agenda-sorting-strategy '((agenda habit-down time-up user-defined-down category-keep)
(todo urgency-down category-keep)
(tags urgency-down category-keep)
(search category-keep)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment