(feat): cache entered template variables (#952)

Fixes #951.
This commit is contained in:
Jethro Kuan 2020-07-20 22:14:21 +08:00 committed by GitHub
parent 80390b5a84
commit c24fb51b03
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 17 deletions

View file

@ -21,6 +21,7 @@
- [#854](https://github.com/org-roam/org-roam/pull/854) Warn instead of fail when duplicate refs and IDs exist. - [#854](https://github.com/org-roam/org-roam/pull/854) Warn instead of fail when duplicate refs and IDs exist.
- [#857](https://github.com/org-roam/org-roam/pull/857) Fix tag extraction for symlinked directories. - [#857](https://github.com/org-roam/org-roam/pull/857) Fix tag extraction for symlinked directories.
- [#894](https://github.com/org-roam/org-roam/pull/894) Perform link fixes on all Org-roam files - [#894](https://github.com/org-roam/org-roam/pull/894) Perform link fixes on all Org-roam files
- [#952](https://github.com/org-roam/org-roam/pull/952) Cache `${foo}` template variables so they do not need to be re-entered twice
## 1.2.0 (12-06-2020) ## 1.2.0 (12-06-2020)

View file

@ -208,11 +208,11 @@ Details on how to specify for the template is given in `org-roam-capture-templat
:org-roam))) :org-roam)))
(buffer-list))) (buffer-list)))
(defun org-roam-capture--fill-template (str &optional info) (defun org-roam-capture--fill-template (str)
"Expands the template STR, returning the string. "Expands the template STR, returning the string.
This is an extension of org-capture's template expansion. This is an extension of org-capture's template expansion.
First, it expands ${var} occurrences in STR, using the INFO alist. First, it expands ${var} occurrences in STR, using `org-roam-capture--info'.
If there is a ${var} with no matching var in the alist, the value If there is a ${var} with no matching var in the alist, the value
of var is prompted for via `completing-read'. of var is prompted for via `completing-read'.
@ -220,8 +220,10 @@ Next, it expands the remaining template string using
`org-capture-fill-template'." `org-capture-fill-template'."
(-> str (-> str
(s-format (lambda (key) (s-format (lambda (key)
(or (s--aget info key) (or (s--aget org-roam-capture--info key)
(completing-read (format "%s: " key ) nil))) nil) (when-let ((val (completing-read (format "%s: " key) nil)))
(push (cons key val) org-roam-capture--info)
val))) nil)
(org-capture-fill-template))) (org-capture-fill-template)))
(defun org-roam-capture--insert-link-h () (defun org-roam-capture--insert-link-h ()
@ -277,8 +279,7 @@ the file if the original value of :no-save is not t and
(let* ((name-templ (or (org-roam-capture--get :file-name) (let* ((name-templ (or (org-roam-capture--get :file-name)
org-roam-capture--file-name-default)) org-roam-capture--file-name-default))
(new-id (s-trim (org-roam-capture--fill-template (new-id (s-trim (org-roam-capture--fill-template
name-templ name-templ)))
org-roam-capture--info)))
(file-path (org-roam--file-path-from-id new-id)) (file-path (org-roam--file-path-from-id new-id))
(roam-head (or (org-roam-capture--get :head) (roam-head (or (org-roam-capture--get :head)
org-roam-capture--header-default)) org-roam-capture--header-default))
@ -299,16 +300,6 @@ the file if the original value of :no-save is not t and
:no-save t)) :no-save t))
file-path)) file-path))
(defun org-roam-capture--expand-template ()
"Expand capture template with information from `org-roam-capture--info'."
(org-capture-put :template
(s-format (org-capture-get :template)
(lambda (key)
(or (s--aget org-roam-capture--info key)
(when-let ((v (completing-read (format "%s: " key ) nil)))
(push (cons key v) org-roam-capture--info)
v))) nil)))
(defun org-roam-capture--get-point () (defun org-roam-capture--get-point ()
"Return exact point to file for org-capture-template. "Return exact point to file for org-capture-template.
The file to use is dependent on the context: The file to use is dependent on the context:
@ -341,7 +332,8 @@ This function is used solely in Org-roam's capture templates: see
(plist-get pl :path) (plist-get pl :path)
(org-roam-capture--new-file)))) (org-roam-capture--new-file))))
(_ (error "Invalid org-roam-capture-context"))))) (_ (error "Invalid org-roam-capture-context")))))
(org-roam-capture--expand-template) (org-capture-put :template
(org-roam-capture--fill-template (org-capture-get :template)))
(org-roam-capture--put :file-path file-path) (org-roam-capture--put :file-path file-path)
(while org-roam-capture-additional-template-props (while org-roam-capture-additional-template-props
(let ((prop (pop org-roam-capture-additional-template-props)) (let ((prop (pop org-roam-capture-additional-template-props))