Skip to content

Instantly share code, notes, and snippets.

@akashpal-21
Last active June 19, 2024 22:29
Show Gist options
  • Save akashpal-21/19e2350cf5b51addd3c61afcd254ca39 to your computer and use it in GitHub Desktop.
Save akashpal-21/19e2350cf5b51addd3c61afcd254ca39 to your computer and use it in GitHub Desktop.
org-roam-read+.el
;; org-roam-node-read+.el
(require 'org-roam-node)
;; Add a new constructor function to the org-roam-node struct
;; This constructor function avoids using &keys, and only takes a
;; curated lists of slots to construe the node struct.
(cl-defstruct (org-roam-node (:constructor org-roam-node-create)
(:constructor +org-roam-node-create
(id &optional file-title file-mtime level point file title olp))
(:copier nil))
"A heading or top level file with an assigned ID property."
file file-title file-hash file-atime file-mtime
id level point todo priority scheduled deadline title properties olp
tags aliases refs)
;;;
;; Modify the node list fn -- only query relevant information from the db
;; as curated --
;; Take care of sorting the entries by RTL: ROOT TIME LEVEL
;; Root entries in `org-roam-directory' come first
;; Between them they are sorted by modification time
;; And finally, Headline nodes come before file nodes
(defun +org-roam-node-list ()
(let ((rows (org-roam-db-query
"select nodes.id, files.title, files.mtime, nodes.\"level\", nodes.pos, nodes.file, nodes.title, nodes.olp
from nodes left join files on nodes.file = files.file
where
nodes.file not like '%%/references/%%' -- filter the references folder
order by
length(nodes.file) - length(replace(nodes.file, '/', '')),
case when nodes.file like '%%guides%%' then files.mtime end desc, -- put the folder named guides before other folders
files.mtime desc,
nodes.\"level\" desc;")))
(cl-loop for row in rows
collect (apply #'+org-roam-node-create row))))
;;;
;; Modify the org-roam-node-read--completions
;; Eliminate the bottlenecks caused by filtering and sorting
;; For filtering and sorting directly modify the query to the db
(defun +org-roam-node-read--completions (&rest args)
"Modfied `org-roam-node-read--completions'
Filtering and Sorting made redundant.
Return an alist for node completion.
The car is the displayed title or alias for the node, and the cdr
is the `org-roam-node'.
The displayed title is formatted according to `+org-roam-node-display-template'."
(mapcar (lambda (node)
(let ((display-candidate (+org-roam-node-display-template node)))
(cons display-candidate node)))
(+org-roam-node-list)))
(advice-add 'org-roam-node-read--completions :override #'+org-roam-node-read--completions)
;;;
;; Customise appearance of nodes in `org-roam-find' & `org-roam-insert'
(defun +org-roam-node-display-template (node)
(let* ((level (org-roam-node-level node))
(file (org-roam-node-file node))
(dir (if (string-match "/roam/\\([^/]+\\)/" file)
(match-string 1 file)
"/"))
(formatted-dir (format "(%s)" dir))
(dir-width 10) ;; Set fixed width for the directory component
(padded-dir (truncate-string-to-width (format "%-10s" formatted-dir) dir-width)))
(concat
padded-dir " "
(when (> level 0) (concat (propertize (org-roam-node-file-title node) 'face 'italic) " / "))
(when (> level 1) (concat (string-join (org-roam-node-olp node) " > ") " > "))
(propertize (org-roam-node-title node) 'face 'bold))))
;;;
(provide 'org-roam-node-read+)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment