10

Q: How can I have org-mode return the raw data for an agenda view without actually creating an agenda view?

I'd like to access my agenda for an arbitrary day. However, I do not want to create an agenda view per se. Instead, I want org-mode to collect and sort all of the elements that would go into the agenda view for that day and return them (ideally in a list) for further inspection and manipulation.

I had presumed that org-agenda-list would be the place to start. However, that function is a tangled beast and seems to intermingle the collecting, sorting, and displaying processes. As such, I presume (hope?) that I've simply missed the relevant function somewhere that provides the functionality I'm after.

Dan
  • 32,584
  • 6
  • 98
  • 168

1 Answers1

4

The following is a condensed example of how to extract the data that goes into an *Org Agenda* buffer when normally using the function org-agenda-list, with org-agenda-entry-types such as :deadline, :scheduled, :timestamp, sexp, :deadline*, and :scheduled*. The range of dates -- begin and end -- should be in a Gregorian list format -- e.g., '(6 1 2015). The customizable let-bound options are org-agenda-prefix-format and org-agenda-entry-types. The function returns a result in the format of a list.

(require 'calendar)
(require 'org)
(require 'org-agenda)
(require 'cl)

;; Portions of following code were extracted from:
;;   https://github.com/kiwanami/emacs-calfw written by Masashi Sakurai
;; Said code has been modified by @lawlist hereinbelow.
;;
(defun org-get-entries-fn (begin end)
"Return org schedule items between BEGIN and END.
USAGE:  (org-get-entries-fn '(6 1 2015) '(12 31 2020))"
  (unless
      (and
        (calendar-date-is-valid-p begin)
        (calendar-date-is-valid-p end))
    (let ((debug-on-quit nil))
      (signal 'quit '("One or both of your Gregorian dates are invalid."))))
  (let* (
      result
      (org-agenda-buffer nil) ;; prevent error from `org-compile-prefix-format'
      ;; The variable `org-agenda-only-exact-dates' is apparently not operational.
      (org-scheduled-past-days 0) ;; avoid duplicate entries for overdue items
      (org-agenda-prefix-format "• ")
      (org-agenda-entry-types '(:scheduled))
      (date-after
        (lambda (date num)
          "Return the date after NUM days from DATE."
          (calendar-gregorian-from-absolute
           (+ (calendar-absolute-from-gregorian date) num))))
      (enumerate-days
        (lambda (begin end)
          "Enumerate date objects between BEGIN and END."
          (when (> (calendar-absolute-from-gregorian begin)
                   (calendar-absolute-from-gregorian end))
            (error "Invalid period : %S - %S" begin end))
          (let ((d begin) ret (cont t))
            (while cont
              (push (copy-sequence d) ret)
              (setq cont (not (equal d end)))
              (setq d (funcall date-after d 1)))
            (nreverse ret)))) )
    (org-compile-prefix-format nil)
    (setq result
      (loop for date in (funcall enumerate-days begin end) append
        (loop for file in (org-agenda-files nil 'ifmode) append
          (progn
            (org-check-agenda-file file)
            (apply 'org-agenda-get-day-entries file date org-agenda-entry-types)))))
    result))
lawlist
  • 18,826
  • 5
  • 37
  • 118