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)
(defun org-roam-capture--fill-template (str &optional org-capture-p)
"Expand the template STR, returning the expanded template.
It expands ${var} occurrences in STR. When ORG-CAPTURE-P, also
run Org-capture's template expansion."
(defun org-roam-capture--fill-template (template &optional org-capture-p)
"Expand TEMPLATE and return it.
It expands ${var} occurrences in TEMPLATE. When ORG-CAPTURE-P,
also run Org-capture's template expansion."
(funcall (if org-capture-p #'org-capture-fill-template #'identity)
(s-format str
(lambda (key)
(let ((fn (intern (concat "org-roam-node-" key)))
(ksym (intern (concat ":" key))))
(cond
((fboundp fn)
(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-put org-roam-capture--info ksym r)
r))))))))
(org-roam-format
template
(lambda (key)
(let ((fn (intern (concat "org-roam-node-" key)))
(ksym (intern (concat ":" key))))
(cond
((fboundp fn)
(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-put org-roam-capture--info ksym r)
r))))))))
(defun org-roam-capture--insert-ref ()
"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))))
;;; 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)
"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 ":"))
""))))
"")))))
(org-roam-format
format
(lambda (field)
(setq fields-width
(+ fields-width
(string-to-number
(or (cadr (split-string field ":"))
"")))))))))
(cons format (+ fields-width string-width))))
;;; Diagnostics

View File

@@ -515,29 +515,30 @@ nodes."
(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 (org-roam--tags-to-str field-value)))
(when (and (equal field-name "file")
field-value)
(setq field-value (file-relative-name field-value org-roam-directory)))
(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)))))))
(let ((fmt (org-roam--process-display-format org-roam-node-display-template)))
(org-roam-format
(car fmt)
(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 (org-roam--tags-to-str field-value)))
(when (and (equal field-name "file")
field-value)
(setq field-value (file-relative-name field-value org-roam-directory)))
(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 fmt)))
0 ?\s)))))))
(defun org-roam-node-preview (file point)
"Get preview content for FILE at POINT."