The Complete Computing Environment

Dynamic Org Captures


An Emacs Lisp Macro for Org captures with dynamic file name:

(setq cce--journal-topic-id "journal"
      '(insert ":PROPERTIES:\n"
               ":ID: " cce--journal-topic-id "-" (format-time-string "%Gw%V") "\n"
               "#+TITLE: Journal " (format-time-string "%GW%V") "\n"
               "#+FILETAGS: :Journal:\n"
               "[[id:" cce--journal-topic-id "][Journal]]\n"))

(cl-defmacro cce-def-dynamic-org-capture (keys description 
                                               &key name 
                                               (top-matter cce--standard-journal-topmatter)
  `'(,keys ,description entry
           (file+olp+datetree cce--last-capture-location)
            (lambda ()
              (let ((filename (format-time-string ,capture-path-tmpl))
                    (template-filename ,template-path))
                (setq cce--last-capture-location filename)
                (unless (file-exists-p filename)
                  (message "Insert top-matter for %s" filename)
                  (find-file filename)
                  (goto-char (point-min))
                ;; lift from org-capture-get-template
                (let ((filename (expand-file-name template-filename org-directory)))
                  (if (file-exists-p filename) (org-file-contents filename)
                    (format "* Template file %S not found" file))))))
           ,@(unless (seq-contains extra-capture-args :tree-type)
               '(:tree-type week))))

(provide 'cce/dynamic-org-capture)

For a long time I used a system of my own invention to have dynamic file names in org-capture using defdaily and frankly it kind of stunk. Relying on timers was janky and often didn't behave how I wanted it to.

When I was reading the source code for Denote I made a realization in how they implemented support for "dynamic" file names in their capture templates.

Thanks denote.