replace s-format with org-roam-format

This commit is contained in:
Jethro Kuan
2021-04-23 15:54:57 +08:00
parent 3f31ff2cd9
commit a37dd6afea
3 changed files with 72 additions and 47 deletions

View File

@@ -482,23 +482,24 @@ This function is to be called in the Org-capture finalization process."
(add-hook 'org-capture-prepare-finalize-hook #'org-roam-capture--install-finalize) (add-hook 'org-capture-prepare-finalize-hook #'org-roam-capture--install-finalize)
(defun org-roam-capture--fill-template (str &optional org-capture-p) (defun org-roam-capture--fill-template (template &optional org-capture-p)
"Expand the template STR, returning the expanded template. "Expand TEMPLATE and return it.
It expands ${var} occurrences in STR. When ORG-CAPTURE-P, also It expands ${var} occurrences in TEMPLATE. When ORG-CAPTURE-P,
run Org-capture's template expansion." also run Org-capture's template expansion."
(funcall (if org-capture-p #'org-capture-fill-template #'identity) (funcall (if org-capture-p #'org-capture-fill-template #'identity)
(s-format str (org-roam-format
(lambda (key) template
(let ((fn (intern (concat "org-roam-node-" key))) (lambda (key)
(ksym (intern (concat ":" key)))) (let ((fn (intern (concat "org-roam-node-" key)))
(cond (ksym (intern (concat ":" key))))
((fboundp fn) (cond
(funcall fn org-roam-capture--node)) ((fboundp fn)
((plist-get org-roam-capture--info ksym) (funcall fn org-roam-capture--node))
(plist-get org-roam-capture--info ksym)) ((plist-get org-roam-capture--info ksym)
(t (let ((r (completing-read (format "%s: " key) nil))) (plist-get org-roam-capture--info ksym))
(plist-put org-roam-capture--info ksym r) (t (let ((r (completing-read (format "%s: " key) nil)))
r)))))))) (plist-put org-roam-capture--info ksym r)
r))))))))
(defun org-roam-capture--insert-ref () (defun org-roam-capture--insert-ref ()
"Insert the ref if any." "Insert the ref if any."

View File

@@ -142,19 +142,42 @@ BEG and END are markers for the beginning and end regions."
(marker-buffer beg)))) (marker-buffer beg))))
;;; Formatting ;;; Formatting
(defun org-roam-format (template replacer)
"Format TEMPLATE with the function REPLACER.
REPLACER takes an argument of the format variable and optionally
an extra argument which is the EXTRA value from the call to
`org-roam-format'.
Adapted from `s-format'."
(let ((saved-match-data (match-data)))
(unwind-protect
(replace-regexp-in-string
"\\${\\([^}]+\\)}"
(lambda (md)
(let ((var (match-string 1 md))
(replacer-match-data (match-data)))
(unwind-protect
(let ((v (progn
(set-match-data saved-match-data)
(funcall replacer var))))
(if v (format "%s" v) (signal 'org-roam-format-resolve md)))
(set-match-data replacer-match-data)))) template
;; Need literal to make sure it works
t t)
(set-match-data saved-match-data))))
(defun org-roam--process-display-format (format) (defun org-roam--process-display-format (format)
"Pre-calculate minimal widths needed by the FORMAT string." "Pre-calculate minimal widths needed by the FORMAT string."
(let* ((fields-width 0) (let* ((fields-width 0)
(string-width (string-width
(string-width (string-width
(s-format format (org-roam-format
(lambda (field) format
(setq fields-width (lambda (field)
(+ fields-width (setq fields-width
(string-to-number (+ fields-width
(or (cadr (split-string field ":")) (string-to-number
"")))) (or (cadr (split-string field ":"))
""))))) "")))))))))
(cons format (+ fields-width string-width)))) (cons format (+ fields-width string-width))))
;;; Diagnostics ;;; Diagnostics

View File

@@ -515,29 +515,30 @@ nodes."
(defun org-roam-node--format-entry (node width) (defun org-roam-node--format-entry (node width)
"Formats NODE for display in the results list. "Formats NODE for display in the results list.
WIDTH is the width of the results list." WIDTH is the width of the results list."
(let ((format (org-roam--process-display-format org-roam-node-display-template))) (let ((fmt (org-roam--process-display-format org-roam-node-display-template)))
(s-format (car format) (org-roam-format
(lambda (field) (car fmt)
(let* ((field (split-string field ":")) (lambda (field)
(field-name (car field)) (let* ((field (split-string field ":"))
(field-width (cadr field)) (field-name (car field))
(getter (intern (concat "org-roam-node-" field-name))) (field-width (cadr field))
(field-value (or (funcall getter node) ""))) (getter (intern (concat "org-roam-node-" field-name)))
(when (and (equal field-name "tags") (field-value (or (funcall getter node) "")))
field-value) (when (and (equal field-name "tags")
(setq field-value (org-roam--tags-to-str field-value))) field-value)
(when (and (equal field-name "file") (setq field-value (org-roam--tags-to-str field-value)))
field-value) (when (and (equal field-name "file")
(setq field-value (file-relative-name field-value org-roam-directory))) field-value)
(if (not field-width) (setq field-value (file-relative-name field-value org-roam-directory)))
field-value (if (not field-width)
(setq field-width (string-to-number field-width)) field-value
(truncate-string-to-width (setq field-width (string-to-number field-width))
field-value (truncate-string-to-width
(if (> field-width 0) field-value
field-width (if (> field-width 0)
(- width (cdr format))) field-width
0 ?\s))))))) (- width (cdr fmt)))
0 ?\s)))))))
(defun org-roam-node-preview (file point) (defun org-roam-node-preview (file point)
"Get preview content for FILE at POINT." "Get preview content for FILE at POINT."