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 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.

#+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 this command.
  If it is a longer string it is used as a tags/todo match string.


  String to place before context text.  Do not use \"* \"!
  Defaults to \"  \"


  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-buffer-name (or (plist-get params :temp) "*SJM/org-dynamic-buffer*"))
    (org-agenda-entry-text-leaders (or (plist-get params :leaders) "  "))
    (replace-star (or (plist-get params :replace-star) "- "))
      (if (> (length cmd-key) 1)
      (org-tags-view nil cmd-key)
    (org-agenda nil cmd-key))
      (with-current-buffer org-agenda-buffer-name
    (setq data (buffer-string))
      (insert data)
      (goto-char (point-max))
      (beginning-of-line 1)
      (while (not (bobp))
        (when (org-get-at-bol 'org-hd-marker)
        (beginning-of-line 0))
      (setq data (buffer-string))))
      (kill-buffer org-agenda-buffer-name)) ; Not needed now.
    (insert (replace-regexp-in-string "^\\* [A-Z-]* " replace-star data))))

(defun sjm/org-agenda-entry-text ()
  "Add some text from the entry as context to the current line.
Adapted from `org-agenda-entry-text-show-here."
    (let (m txt o)
    (setq m (org-get-at-bol 'org-hd-marker))
    (unless (marker-buffer m)
      (error "No marker points to an entry here"))
    (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.

| improve this answer | |

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.