Keeping todo items in org-roam
A while ago I made an attempt to improve my work habits by keeping a document
with TODO items. It lasted only for a while, and I've since had the intention to
make another attempt. Since then I've started using org-roam and I've managed to
create a habit of writing daily journal notes using org-roam's daily-notes. A
few times I've thought that it might fit me well to put TODO items in the notes,
but that would mean that I'd have to somehow keep track of them. At first I
manually added a tag to each journal fily containing a TODO item. That didn't
work very well at all, which should have been obvious up front. Then I added the
folders where I keep roam files and journals to org-agenda-files
, that worked
a lot better. I'd still be using that, even if I expected it to slow down
considerably as the number of files grow, but then I found a post on dynamic and
fast agenda with org-roam.
I adjusted it slightly to fit my own setup a bit better, i.e. I made a Spacemacs
layer, roam-extra
, I use the tag todo
, and I use a different hook to get the
tag added on opening an org-roam file.
The layer consists of a single file, layers/roam-extra/funcs.el
. In it I
define 4 functions (they are pretty much copies of the functions in the post
linked above):
roam-extra:todo-p
- returns non-nil
if the current current buffer contains a TODO item.roam-extra:update-todo-tag
- updates the tags of the current buffer to reflect the presence of TODO items, i.e. ensure the the tagtodo
is present iff there's a TODO item.roam-extra:todo-files
- uses the org-roam DB to return a list of all files containing the tagtodo
.roam-extra:update-todo-files
- adjusts'org-agenda-files
to contain only the files with TODO items.
I've put the full contents of the file at the end of the post.
To ensure that the todo
tag is correct in all org-mode files I've added
roam-extra:update-todo-tag
to hooks that are invoked on opening an org-ram
file and when saving a file. (I would love to find a more specialise hook than
before-save-hook
, but it works for now.)
(add-hook 'org-roam-file-setup-hook #'roam-extra:update-todo-tag) (add-hook 'before-save-hook #'roam-extra:update-todo-tag)
To ensure that the list of files with TODO items is kept up to date when I open
I also wrap org-agenda
in an advice so roam-extra:update-todo-files
is
called prior to the agenda being opened.
(advice-add 'org-agenda :before #'roam-extra:update-todo-files)
The layer, layers/roam-extra/funcs.el
(defun roam-extra:todo-p () "Return non-nil if current buffer has any TODO entry. TODO entries marked as done are ignored, meaning the this function returns nil if current buffer contains only completed tasks." (org-element-map (org-element-parse-buffer 'headline) 'headline (lambda (h) (eq (org-element-property :todo-type h) 'todo)) nil 'first-match)) (defun roam-extra:update-todo-tag () "Update TODO tag in the current buffer." (when (and (not (active-minibuffer-window)) (org-roam--org-file-p buffer-file-name)) (let* ((file (buffer-file-name (buffer-base-buffer))) (all-tags (org-roam--extract-tags file)) (prop-tags (org-roam--extract-tags-prop file)) (tags prop-tags)) (if (roam-extra:todo-p) (setq tags (seq-uniq (cons "todo" tags))) (setq tags (remove "todo" tags))) (unless (equal prop-tags tags) (org-roam--set-global-prop "roam_tags" (combine-and-quote-strings tags)))))) (defun roam-extra:todo-files () "Return a list of note files containing todo tag." (seq-map #'car (org-roam-db-query [:select file :from tags :where (like tags (quote "%\"todo\"%"))]))) (defun roam-extra:update-todo-files (&rest _) "Update the value of `org-agenda-files'." (setq org-agenda-files (roam-extra:todo-files)))