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,12 +482,13 @@ 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
template
(lambda (key) (lambda (key)
(let ((fn (intern (concat "org-roam-node-" key))) (let ((fn (intern (concat "org-roam-node-" key)))
(ksym (intern (concat ":" key)))) (ksym (intern (concat ":" key))))

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
format
(lambda (field) (lambda (field)
(setq fields-width (setq fields-width
(+ fields-width (+ fields-width
(string-to-number (string-to-number
(or (cadr (split-string field ":")) (or (cadr (split-string field ":"))
"")))) "")))))))))
"")))))
(cons format (+ fields-width string-width)))) (cons format (+ fields-width string-width))))
;;; Diagnostics ;;; Diagnostics

View File

@@ -515,8 +515,9 @@ 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
(car fmt)
(lambda (field) (lambda (field)
(let* ((field (split-string field ":")) (let* ((field (split-string field ":"))
(field-name (car field)) (field-name (car field))
@@ -536,7 +537,7 @@ WIDTH is the width of the results list."
field-value field-value
(if (> field-width 0) (if (> field-width 0)
field-width field-width
(- width (cdr format))) (- width (cdr fmt)))
0 ?\s))))))) 0 ?\s)))))))
(defun org-roam-node-preview (file point) (defun org-roam-node-preview (file point)