In summary I want to capture org-agenda into a dynamic block with some entry text.

At the end of the week I provide a report as to what I've been doing and a list of what I intend to work on in the coming week. For listing what I have been doing I use a clocktable to show me time spent. And for what's still to do, I use a custom agenda view which captures specific tags and sorts the list by priority. For both of these tasks I have been manually copying items from the clocktable and agenda into my report.

So how might I create the org-agenda block in my report buffer automatically?

  • Migrated upon request by op.
    – Samuel Liew
    Jun 5 '20 at 12:47

For the second part (inserting org-agenda) this is what I came up with, I wonder if there is another way?
Using org-version 9.3.6. (updated slightly) for org-version 9.4.4

#+begin_src emacs-lisp
(defun org-dblock-write:sjm/org-insert-agenda (params)
  "Writes agenda items with some some text from the entry as context
to dynamic block.
Parameters are:


  If key is a string of length 1, it is used as a key in
  `org-agenda-custom-commands` and triggers that command.  If it
  is a longer string it is used as a tags/todo match string.


  String to insert before context text.  Defaults to two spaces \"  \".
  Do not use asterisk \"* \".


  Maximum number of lines to include, defaults to


  Regex to replace.  Defaults to heading asterisk characters and
  first uppercase word (TODO label): \"^\\* [A-Z-]* \"


  String to replace the org-heading star with.
  Defaults to \"- \" such that headings become list items.

Somewhat adapted from org-batch-agenda.
  (let ((data)
    (cmd-key (or (plist-get params :key) "b"))
    (org-agenda-entry-text-leaders (or (plist-get params :leaders) "  "))
    (org-agenda-entry-text-maxlines (or (plist-get params :count)
    (replace-this (or (plist-get params :repalce-this) "^\\* [A-Z-]* "))
    (replace-with (or (plist-get params :replace-with) "- "))
    (save-window-excursion ; Return to current  buffer and window when done.
      (if (> (length cmd-key) 1) ; If key is more than one character, THEN
      (org-tags-view nil cmd-key) ; Invoke tags view, ELSE
    (org-agenda nil cmd-key)) ; Invoke agenda view using key provided.
    (setq data (buffer-string)) ; copy agenda buffer contents to data
    (with-temp-buffer ; Using a temporary buffer to manipulate text.
      (insert data) ; place agenda data into buffer.
      (goto-char (point-max)) ; end-of-buffer
      (beginning-of-line 1)   ; beggining of last line.
      (while (not (bobp)) ; while not begging of buffer
        (when (org-get-at-bol 'org-hd-marker) ; get text property.
          (sjm/org-agenda-entry-text)) ; Insert item context underneath.
        (beginning-of-line 0)) ; Go to previous line
      (setq data (buffer-string)))) ; Copy buffer, close tmp buf & excursion.
    ;; Paste data, replacing asterisk as per replace-this with replace-with.
    (insert (replace-regexp-in-string replace-this replace-with data))))

(defun sjm/org-agenda-entry-text ()
  "Insert some text from the current agenda item as context.
Adapted from `org-agenda-entry-text-show-here', relies upon
`org-agenda-get-some-entry-text' for the bulk of the work."
  (save-excursion ; return to current place in buffer.
    (let (m txt o) ; declare some local variables.
    (setq m (org-get-at-bol 'org-hd-marker)) ; get text property
    (unless (marker-buffer m) ; get buffer that marker points into.
      (error "No marker points to an entry here"))
    ;; get some entry text, remove any properties and append a new-line.
    (setq txt (concat "\n" (org-no-properties
                 m org-agenda-entry-text-maxlines
    (when (string-match "\\S-" txt)
      (forward-line 1)
      (insert txt "\n\n")))))

The org-agenda is then applied using C-c -C-x C-u while within the following block

#+BEGIN: sjm/org-insert-agenda :key "b"

I've not really looked at the first part yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.