[breaking] update org-capture templates syntax

This commit is contained in:
Jethro Kuan
2021-04-12 22:37:31 +08:00
parent a1710b41b3
commit eef1ed2873
2 changed files with 71 additions and 96 deletions

View File

@ -50,15 +50,15 @@ during the Org-roam capture process.")
This variable is populated dynamically, and is only non-nil This variable is populated dynamically, and is only non-nil
during the Org-roam capture process.") during the Org-roam capture process.")
(defconst org-roam-capture--template-keywords '(:file-path :head :olp) (defconst org-roam-capture--template-keywords '(:if-new)
"Keywords used in `org-roam-capture-templates' specific to Org-roam.") "Keywords used in `org-roam-capture-templates' specific to Org-roam.")
(defcustom org-roam-capture-templates (defcustom org-roam-capture-templates
(list (list :key "d" (list (list :key "d"
:desc "default" :desc "default"
:body "%?" :body "%?"
:file-path "%<%Y%m%d%H%M%S>-${slug}.org" :if-new '(file+head "%<%Y%m%d%H%M%S>-${slug}.org"
:head "#+title: ${title}\n" "#+title: ${title}\n")
:unnarrowed t)) :unnarrowed t))
"Capture templates for Org-roam. "Capture templates for Org-roam.
@ -71,8 +71,8 @@ TODO: Document this"
(list (list :key "r" (list (list :key "r"
:desc "ref" :desc "ref"
:body "%?" :body "%?"
:file-path "${slug}.org" :if-new '(file+head "${slug}.org"
:head "#+title: ${title}" "#+title: ${title}")
:unnarrowed t)) :unnarrowed t))
"The Org-roam templates used during a capture from the roam-ref protocol. "The Org-roam templates used during a capture from the roam-ref protocol.
Details on how to specify for the template is given in `org-roam-capture-templates'." Details on how to specify for the template is given in `org-roam-capture-templates'."
@ -175,7 +175,6 @@ This function is to be called in the Org-capture finalization process."
(funcall (intern (concat "org-roam-capture--finalize-" (funcall (intern (concat "org-roam-capture--finalize-"
(symbol-name (org-roam-capture--get :finalize)))) (symbol-name (org-roam-capture--get :finalize))))
:id id))) :id id)))
(org-roam-capture--save-file-maybe)
(remove-hook 'org-capture-after-finalize-hook #'org-roam-capture--finalize))) (remove-hook 'org-capture-after-finalize-hook #'org-roam-capture--finalize)))
(defun org-roam-capture--install-finalize () (defun org-roam-capture--install-finalize ()
@ -200,55 +199,36 @@ string using `org-capture-fill-template'."
(funcall fn org-roam-capture--node) (funcall fn org-roam-capture--node)
(completing-read (format "%s: " key) nil))))))) (completing-read (format "%s: " key) nil)))))))
(defun org-roam-capture--save-file-maybe () (defun org-roam-capture--set-target-location (&optional target)
"Save the file conditionally. "Find TARGET buffer and position."
The file is saved if the original value of :no-save is not t and (pcase (or target
`org-note-abort' is not t. It is added to (org-roam-capture--get :if-new)
`org-capture-after-finalize-hook'." (user-error "Template needs to specify `:if-new'"))
(cond (`(file ,path)
((and (org-roam-capture--get :new-file) (setq path (expand-file-name
org-note-abort) (s-trim (org-roam-capture--fill-template path))
(with-current-buffer (org-capture-get :buffer) org-roam-directory))
(set-buffer-modified-p nil) (set-buffer (org-capture-target-buffer path))
(kill-buffer))) (widen))
((and (not (org-roam-capture--get :orig-no-save)) (`(file+olp ,path ,olp)
(not org-note-abort)) (setq path (expand-file-name
(with-current-buffer (org-capture-get :buffer) (s-trim (org-roam-capture--fill-template path))
(save-buffer))))) org-roam-directory))
(set-buffer (org-capture-target-buffer path))
(defun org-roam-capture--new-file () (let ((m (org-roam-capture-find-or-create-olp olp)))
"Return the path to file during an Org-roam capture. (goto-char m))
(widen))
This function reads the file-name attribute of the currently (`(file+head ,path ,head)
active Org-roam template. (setq path (expand-file-name
(s-trim (org-roam-capture--fill-template path))
Else, to insert the header content in the file, `org-capture' org-roam-directory))
prepends the `:head' property of the Org-roam capture template. (let ((exists-p (file-exists-p path)))
(set-buffer (org-capture-target-buffer path))
To prevent the creation of a new file if the capture process is (unless exists-p
aborted, we do the following: (insert (org-roam-capture--fill-template head))))
(widen))
1. Save the original value of the capture template's :no-save. ;; TODO: support node
2. Set the capture template's :no-save to t. ))
3. Add a function on `org-capture-before-finalize-hook' that saves
the file if the original value of :no-save is not t and
`org-note-abort' is not t."
(let* ((name-templ (or (org-roam-capture--get :file-path)
(user-error "Template needs to specify `:file-path'")))
(rel-filename (s-trim (org-roam-capture--fill-template name-templ)))
(file-path (expand-file-name rel-filename org-roam-directory))
(roam-head (or (org-roam-capture--get :head) ""))
(org-template (org-capture-get :template))
(roam-template (concat roam-head org-template)))
(if (or (file-exists-p file-path)
(find-buffer-visiting file-path))
(make-directory (file-name-directory file-path) t)
(org-roam-capture--put :orig-no-save (org-capture-get :no-save)
:new-file t)
(org-capture-put :template roam-template
:type 'plain)
(org-capture-put :no-save t))
file-path))
(defun org-roam-capture-find-or-create-olp (olp) (defun org-roam-capture-find-or-create-olp (olp)
"Return a marker pointing to the entry at OLP in the current buffer. "Return a marker pointing to the entry at OLP in the current buffer.
@ -294,49 +274,44 @@ you can catch it with `condition-case'."
end (save-excursion (org-end-of-subtree t t)))) end (save-excursion (org-end-of-subtree t t))))
(point-marker)))) (point-marker))))
(defun org-roam-capture--get-ref-path (type path) (defun org-roam-capture--get-ref-path (ref)
"Get the file path to the ref with TYPE and PATH." (save-match-data
(caar (org-roam-db-query (when (string-match org-link-plain-re ref)
[:select [file] (let ((type (match-string 1 ref))
:from refs (path (match-string 2 ref)))
:where (= type $s1) (car (org-roam-db-query
:and (= ref $s2) [:select [nodes:file pos]
:limit 1] :from refs
type path))) :left-join nodes
:on (= refs:node-id nodes:id)
:where (= refs:type $s1)
:and (= refs:ref $s2)
:limit 1]
type path))))))
(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.
This function is used solely in Org-roam's capture templates: see This function is used solely in Org-roam's capture templates: see
`org-roam-capture-templates'." `org-roam-capture-templates'."
(let* ((file-path (cond ((plist-get org-roam-capture--info :ref)
(cond ((plist-get org-roam-capture--info :ref) (if-let ((file-pos (org-roam-capture--get-ref-path
(or (caar (org-roam-db-query [:select [file] (plist-get org-roam-capture--info :ref))))
:from refs (progn
:where (= ref $s1) (set-buffer (org-capture-target-buffer (car file-pos)))
:limit 1] (goto-char (cdr file-pos))
(plist-get org-roam-capture--info :ref))) (widen))
(org-roam-capture--new-file))) (org-roam-capture--set-target-location)))
((org-roam-node-file org-roam-capture--node) ((and (org-roam-node-file org-roam-capture--node)
(org-roam-node-file org-roam-capture--node)) (org-roam-node-point org-roam-capture--node))
(t (set-buffer (org-capture-target-buffer (org-roam-node-file org-roam-capture--node)))
(org-roam-capture--new-file))))) (goto-char (org-roam-node-point org-roam-capture--node))
(org-capture-put :template (org-end-of-subtree))
(org-roam-capture--fill-template (org-capture-get :template))) (t
(org-roam-capture--put :file-path file-path (org-roam-capture--set-target-location)))
:finalize (or (org-capture-get :finalize) (org-capture-put :template
(org-roam-capture--get :finalize))) (org-roam-capture--fill-template (org-capture-get :template)))
(set-buffer (org-capture-target-buffer file-path)) (org-roam-capture--put :finalize (or (org-capture-get :finalize)
(widen) (org-roam-capture--get :finalize))))
(if-let* ((olp (org-roam-capture--get :olp)))
(condition-case err
(when-let ((marker (org-roam-capture-find-or-create-olp olp)))
(goto-char marker)
(set-marker marker nil))
(error
(when (org-roam-capture--get :new-file)
(kill-buffer))
(signal (car err) (cdr err))))
(goto-char (point-max)))))
(defun org-roam-capture--convert-template (template &optional props) (defun org-roam-capture--convert-template (template &optional props)
"Convert TEMPLATE from Org-roam syntax to `org-capture-templates' syntax. "Convert TEMPLATE from Org-roam syntax to `org-capture-templates' syntax.

View File

@ -64,8 +64,8 @@
(list (list :key "d" (list (list :key "d"
:desc "default" :desc "default"
:body "* %?" :body "* %?"
:file-path (concat org-roam-dailies-directory "%<%Y-%m-%d>") :if-new `(file+head ,(concat org-roam-dailies-directory "%<%Y-%m-%d>.org")
:head "#+title: %<%Y-%m-%d>\n")) "#+title: %<%Y-%m-%d>\n")))
"Capture templates for daily-notes in Org-roam." "Capture templates for daily-notes in Org-roam."
:group 'org-roam :group 'org-roam
:type :type
@ -99,7 +99,7 @@ If FILE is not specified, use the current buffer's file-path."
When GOTO is non-nil, go the note without creating an entry." When GOTO is non-nil, go the note without creating an entry."
(org-roam-capture- :goto (when goto '(4)) (org-roam-capture- :goto (when goto '(4))
:info (list :node (org-roam-node-create)) :node (org-roam-node-create)
:templates org-roam-dailies-capture-templates :templates org-roam-dailies-capture-templates
:props (list :default-time time))) :props (list :default-time time)))