complete-computing-environment/org-roam-navigate-relations...

3.2 KiB

Find Org Roam notes via their relations - Where parallels cross

Archive

Are you an org-roam user that takes a lot of notes and struggles finding them? I will share here a function to reach lost notes via their relations.

This is really neat: you open this and can rifle through your notes, following links until you find the document you want. So imagine, I open it here, and because this page is linked on the CCE index page (and now, vice-versa!) I can navigate from this page to CCE, and then to any other CCE module, or another CCE-related page. This is especially useful when opening Topic Files.

Great for finding open threads, perhaps, especially once we're doing headings.

(defun cce/org-roam-db--links-to (&optional node)
  (let* ((node (or node (org-roam-node-at-point t))))
    (->> node
         (org-roam-node-id)
         (org-roam-db-query [:select [sources:id dests:id] :from links
                             :join nodes :as sources :on (= source sources:id)
                             :join nodes :as dests :on (= dest dests:id)
                             :where (= source $s1) :or (= dest $s1)])
         ;; pop out the one that isn't the node passed in:
         (--map
          (--first (not (equal it (org-roam-node-id node)))
                   it))
         (-map #'org-roam-node-from-id)
         (append (list node)))))

(defun cce/org-roam-navigate-note (arg &optional node choices)
  "Navigate notes by link. With universal ARG tries to use only to navigate the tags of the current note.

Optionally takes a selected NOTE and filepaths CHOICES."
  (interactive "P")
  (let* ((choices (or choices
                      (when arg (cce/org-roam-db--links-to (org-roam-node-at-point)))
                      (org-roam-node-list)))
         (next-node (org-roam-node-read nil (apply-partially #'-contains? choices) nil t)))
    (if (and node next-node
             (string= (org-roam-node-id node)
                      (org-roam-node-id next-node)))
        (org-roam-node-visit node)
      (cce/org-roam-navigate-note nil next-node
                                  (cce/org-roam-db--links-to next-node)))))

;; TODO: 2021-07 this doesn't work, needs an interactive that can take the string, org-roam-find it then navigate
                                        ; (bind-keys :map embark-buffer-map
                                        ;            ("n" . #'cce/org-roam-navigate-note))

(with-eval-after-load 'org-roam
  (bind-keys :map org-roam-prefix-map
             ("n" . cce/org-roam-navigate-note)))

(provide 'cce/navigate-note)