add node display template

This commit is contained in:
Jethro Kuan
2021-04-01 18:01:46 +08:00
parent 490901957e
commit 49541298ba
2 changed files with 51 additions and 1 deletions

View File

@ -141,6 +141,22 @@ BEG and END are markers for the beginning and end regions."
read-only t)
(marker-buffer beg))))
;;; Formatting
(defun org-roam--process-display-format (format)
"Pre-calculate minimal widths needed by the FORMAT string."
(let* ((fields-width 0)
(string-width
(string-width
(s-format format
(lambda (field)
(setq fields-width
(+ fields-width
(string-to-number
(or (cadr (split-string field ":"))
""))))
"")))))
(cons format (+ fields-width string-width))))
;;; Diagnostics
;;;###autoload
(defun org-roam-version (&optional message)

View File

@ -489,6 +489,38 @@ nodes."
(org-roam-node-aliases node) alias-info))
node))
(defcustom org-roam-node-display-template
"${tags:10} ${title:48}"
"Configures display formatting for Org-roam node."
:group 'org-roam
:type 'string)
(defun org-roam-node--format-entry (node width)
"Formats NODE for display in the results list.
WIDTH is the width of the results list."
(let* ((format
(org-roam--process-display-format org-roam-node-display-template)))
(s-format
(car format)
(lambda (field)
(let* ((field (split-string field ":"))
(field-name (car field))
(field-width (cadr field))
(getter (intern (concat "org-roam-node-" field-name)))
(field-value (or (funcall getter node) "")))
(when (and (equal field-name "tags")
field-value)
(setq field-value (string-join field-value " ")))
(if (not field-width)
field-value
(setq field-width (string-to-number field-width))
(truncate-string-to-width
field-value
(if (> field-width 0)
field-width
(- width (cdr format)))
0 ?\s)))))))
(defun org-roam-node-preview (file point)
"Get preview content for FILE at POINT."
(save-excursion
@ -551,7 +583,9 @@ is the `org-roam-node'."
:title title
:point pos
:tags (gethash id tags-table))))
(cons (propertize alias 'node node) node)))))
(cons (propertize alias
'node node
'display (org-roam-node--format-entry node (1- (frame-width)))) node)))))
(defun org-roam-node-read (&optional initial-input filter-fn require-match)
"Read and return an `org-roam-node'.