Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Emacs: Display org-agenda for current subtree or region
(defun org-agenda-subtree-or-region (prefix)
"Display an agenda view for the current subtree or region.
With prefix, display only TODO-keyword items."
(interactive "p")
(let (header)
(if (use-region-p)
(setq header "Region")
(put 'org-agenda-files 'org-restrict (list (buffer-file-name (current-buffer))))
(setq org-agenda-restrict (current-buffer))
(move-marker org-agenda-restrict-begin (region-beginning))
(move-marker org-agenda-restrict-end
(goto-char (1+ (region-end))) ; If point is at pos 0, include heading on that line
;; No region; restrict to subtree
(setq header "Subtree")
(org-agenda-set-restriction-lock 'subtree)))
;; sorting doesn't seem to be working, but the header is
(let ((org-agenda-sorting-strategy '(priority-down timestamp-up))
(org-agenda-overriding-header header))
(org-search-view (if (>= prefix 4) t nil) "*"))
(org-agenda-remove-restriction-lock t)
(message nil)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment