(feat)org-roam-format: add default-value (#1709)

Allow specification of default values in org-roam-format of the form ${key=default_val}.
This commit is contained in:
Jethro Kuan
2021-07-29 23:27:55 +08:00
committed by GitHub
parent d3b7c9b921
commit 127d6efa48
3 changed files with 14 additions and 10 deletions

View File

@ -505,7 +505,7 @@ also run Org-capture's template expansion."
(funcall (if org-capture-p #'org-capture-fill-template #'identity)
(org-roam-format
template
(lambda (key)
(lambda (key default-val)
(let ((fn (intern key))
(node-fn (intern (concat "org-roam-node-" key)))
(ksym (intern (concat ":" key))))
@ -516,7 +516,7 @@ also run Org-capture's template expansion."
(funcall node-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)))
(t (let ((r (completing-read (format "%s: " key) nil nil nil default-val)))
(plist-put org-roam-capture--info ksym r)
r))))))))

View File

@ -186,21 +186,25 @@ BEG and END are markers for the beginning and end regions."
;;; 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'."
The templates are of form ${foo} for variable foo, and
${foo=default} for variable foo with default value \"default\".
REPLACER takes an argument of the format variable and the default
value (possibly nil). 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)))
(replacer-match-data (match-data))
default-val)
(when (string-match "\\(.+\\)=\\(.+\\)" var)
(setq default-val (match-string 2 var)
var (match-string 1 var)))
(unwind-protect
(let ((v (progn
(set-match-data saved-match-data)
(funcall replacer var))))
(funcall replacer var default-val))))
(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
@ -218,7 +222,7 @@ Adapted from `s-format'."
(string-width
(org-roam-format
format
(lambda (field)
(lambda (field _default_val)
(setq fields-width
(+ fields-width
(string-to-number

View File

@ -505,7 +505,7 @@ Uses `org-roam-node-display-template' to format the entry."
(let ((fmt (org-roam--process-display-format org-roam-node-display-template)))
(org-roam-format
(car fmt)
(lambda (field)
(lambda (field _default-val)
(let* ((field (split-string field ":"))
(field-name (car field))
(field-width (cadr field))