Agenda view from nearby notes

I often leave TODOs in my notes if I need to complete a train of thought or elaborate on something. I’ve been trying to implement an agenda view which picks up TODOs from not only the current note, but ones that are closely linked to it.

The minimum (not) working example I came up with was adding an custom agenda view which rebinds the org-agenda-files list:

(setq org-agenda-custom-commands
  '(("e" "todos in nearby notes"
     ((todo "" ((org-agenda-files

Basically the org-roam-db--links-with-max-distance call gets a list of all files that are within 3 links of the file in the current buffer. This doesn’t seem to work because by the time the let binding is evaluated, buffer-file-name evaluates to nil because the agenda buffer is already created.

The other option I (and @zaeph) considered were just a wrapper around the agenda view. My version of this was

(defun tq/agenda-nearby-notes ()
  (let ((org-agenda-files (org-roam-db--links-with-max-distance

and @zaeph’s

 (defun zp/org-roam-agenda ()
  (let ((org-agenda-custom-commands
         `(("e" "todos in nearby notes"
            ((todo "" ((org-agenda-files
    (org-agenda nil "e")))

Disadvantages of wrappers are that It can’t be launched from my the default org-agenda dispatcher, but it does give a bit more flexibility in functionality.

Has anyone implemented anything similar to this or has a better version they use?

1 Like

I finally got back to this. This is what I settled on. I can provide a prefix argument to configure how far I want it to search for TODOs

(defun tq/org-agenda-nearby-notes (&optional distance)
  (interactive "P")
  (let ((org-agenda-files (org-roam-db--links-with-max-distance
                           buffer-file-name (or distance 3)))
        (org-agenda-custom-commands '(("e" "" ((alltodo ""))))))
    (org-agenda nil "e")))