complete-computing-environment/dynamic-org-captures.org

66 lines
2.9 KiB
Org Mode

:PROPERTIES:
:ID: 20221004T113216.759615
:END:
#+title: Dynamic Org Captures
#+FILETAGS: :CCE:
#+ARCOLOGY_KEY: cce/dynamic-org-captures
#+ARCOLOGY_ALLOW_CRAWL: t
#+ARROYO_MODULE_WANTS: cce/org_mode_installation.org
#+ARROYO_EMACS_MODULE: dynamic-org-capture
#+AUTO_TANGLE: t
An [[id:cce/programming_lisp_in_emacs][Emacs Lisp]] Macro for [[id:1fb8fb45-fac5-4449-a347-d55118bb377e][Org]] captures with dynamic file name:
#+begin_src emacs-lisp :tangle ~/org/cce/dynamic-org-capture.el
(setq cce--journal-topic-id "journal"
cce--standard-journal-topmatter
'(insert ":PROPERTIES:\n"
":ID: " cce--journal-topic-id "-" (format-time-string "%Gw%V") "\n"
":END:\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)
capture-path-tmpl
template-path
extra-capture-args)
`'(,keys ,description entry
(file+olp+datetree cce--last-capture-location)
(function
(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))
,top-matter)
;; 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))))))
,@extra-capture-args
,@(unless (seq-contains extra-capture-args :tree-type)
'(:tree-type week))))
(provide 'cce/dynamic-org-capture)
#+end_src
For a long time I used a system of my own invention to have dynamic file names in org-capture using [[id:cce/defdaily][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 [[https://git.sr.ht/~protesilaos/denote/][Denote]] I made a realization in how they implemented support for "dynamic" file names in their capture templates.
- the org-capture template can be a function and that is evaluated first
- the capture can set a variable
- =file+olp+datetree= and =file= capture targets *can be variables*.
- the first function can set the variable used in the target and give dynamic capture file names.
Thanks denote.