(fix): allow group templates in capture-templates (#584)

Co-authored-by: N V <44036031+progfolio@users.noreply.github.com>
This commit is contained in:
Leo Vivier
2020-05-09 12:08:08 +02:00
committed by GitHub
parent c1bfa99ace
commit 46fd2a9a68
5 changed files with 44 additions and 22 deletions

View File

@ -33,6 +33,7 @@
(require 'org-capture) (require 'org-capture)
(require 'dash) (require 'dash)
(require 's) (require 's)
(require 'cl-lib)
;; Declarations ;; Declarations
(defvar org-roam-encrypt-files) (defvar org-roam-encrypt-files)
@ -279,23 +280,21 @@ This function is used solely in Org-roam's capture templates: see
(defun org-roam-capture--convert-template (template) (defun org-roam-capture--convert-template (template)
"Convert TEMPLATE from Org-roam syntax to `org-capture-templates' syntax." "Convert TEMPLATE from Org-roam syntax to `org-capture-templates' syntax."
(let* ((copy (copy-tree template)) (pcase template
converted (`(,_key ,_description) template)
org-roam-plist (`(,key ,description ,type ,target . ,rest)
key (let ((converted `(,key ,description ,type ,target
val) ,(unless (keywordp (car rest)) (pop rest))))
;;put positional args on converted template org-roam-plist
(dotimes (_ 5) options)
(push (pop copy) converted)) (while rest
(while (setq key (pop copy) (let* ((key (pop rest))
val (pop copy)) (val (pop rest))
(if (member key org-roam-capture--template-keywords) (custom (member key org-roam-capture--template-keywords)))
(progn (push val (if custom org-roam-plist options))
(push val org-roam-plist) (push key (if custom org-roam-plist options))))
(push key org-roam-plist)) (append converted options `(:org-roam ,org-roam-plist))))
(push key converted) (_ (user-error "Invalid capture template format: %s" template))))
(push val converted)))
(append (nreverse converted) `(:org-roam ,org-roam-plist))))
(defun org-roam-capture--find-file-h () (defun org-roam-capture--find-file-h ()
"Opens the newly created template file. "Opens the newly created template file.
@ -339,7 +338,10 @@ This uses the templates defined at `org-roam-capture-templates'."
(cons 'file file-path))) (cons 'file file-path)))
(org-roam-capture--context 'capture)) (org-roam-capture--context 'capture))
(setq org-roam-capture-additional-template-props (list :capture-fn 'org-roam-capture)) (setq org-roam-capture-additional-template-props (list :capture-fn 'org-roam-capture))
(org-roam-capture--capture)))) (condition-case err
(org-roam-capture--capture)
(error (user-error "%s. Please adjust `org-roam-capture-templates'"
(error-message-string err)))))))
(provide 'org-roam-capture) (provide 'org-roam-capture)

View File

@ -34,6 +34,7 @@
;;; Library Requires ;;; Library Requires
(require 'org-capture) (require 'org-capture)
(require 'org-roam-capture) (require 'org-roam-capture)
(require 'org-roam-macs)
(defvar org-roam-dailies-capture-templates (defvar org-roam-dailies-capture-templates
'(("d" "daily" plain (function org-roam-capture--get-point) '(("d" "daily" plain (function org-roam-capture--get-point)
@ -52,7 +53,8 @@
(org-roam-capture--info (list (cons 'time time))) (org-roam-capture--info (list (cons 'time time)))
(org-roam-capture--context 'dailies)) (org-roam-capture--context 'dailies))
(add-hook 'org-capture-after-finalize-hook #'org-roam-capture--find-file-h) (add-hook 'org-capture-after-finalize-hook #'org-roam-capture--find-file-h)
(org-roam-capture--capture))) (org-roam--with-template-error 'org-roam-dailies-capture-templates
(org-roam-capture--capture))))
(defun org-roam-dailies-today () (defun org-roam-dailies-today ()
"Create and find the daily note for today." "Create and find the daily note for today."

View File

@ -43,6 +43,21 @@ Like `with-temp-buffer', but propagates `org-roam-directory'."
(let ((org-roam-directory ,current-org-roam-directory)) (let ((org-roam-directory ,current-org-roam-directory))
,@body))))) ,@body)))))
(defmacro org-roam--with-template-error (templates &rest body)
"Eval BODY, and point to TEMPLATES on error.
Provides more informative error messages so that users know where
to look.
\(fn TEMPLATES BODY...)"
(declare (debug (form body)) (indent 1))
`(condition-case err
,@body
(error (user-error "%s. Please adjust `%s'"
(error-message-string err)
,templates))))
(provide 'org-roam-macs) (provide 'org-roam-macs)
;;; org-roam-macs.el ends here ;;; org-roam-macs.el ends here

View File

@ -62,7 +62,8 @@ It opens or creates a note with the given ref.
(org-roam-capture--info decoded-alist) (org-roam-capture--info decoded-alist)
(template (cdr (assoc 'template decoded-alist)))) (template (cdr (assoc 'template decoded-alist))))
(raise-frame) (raise-frame)
(org-roam-capture--capture nil template) (org-roam--with-template-error 'org-roam-capture-ref-templates
(org-roam-capture--capture nil template))
(message "Item captured."))) (message "Item captured.")))
nil) nil)

View File

@ -501,7 +501,8 @@ which takes as its argument an alist of path-completions. See
(setq org-roam-capture-additional-template-props (list :region region (setq org-roam-capture-additional-template-props (list :region region
:link-description link-description :link-description link-description
:capture-fn 'org-roam-insert)) :capture-fn 'org-roam-insert))
(org-roam-capture--capture))))) (org-roam--with-template-error 'org-roam-capture-templates
(org-roam-capture--capture))))))
(defun org-roam--get-title-path-completions () (defun org-roam--get-title-path-completions ()
"Return a list of cons pairs for titles to absolute path of Org-roam files." "Return a list of cons pairs for titles to absolute path of Org-roam files."
@ -539,7 +540,8 @@ which takes as its argument an alist of path-completions. See
(cons 'slug (org-roam--title-to-slug title)))) (cons 'slug (org-roam--title-to-slug title))))
(org-roam-capture--context 'title)) (org-roam-capture--context 'title))
(add-hook 'org-capture-after-finalize-hook #'org-roam-capture--find-file-h) (add-hook 'org-capture-after-finalize-hook #'org-roam-capture--find-file-h)
(org-roam-capture--capture)))))) (org-roam--with-template-error 'org-roam-capture-templates
(org-roam-capture--capture)))))))
(defun org-roam-find-directory () (defun org-roam-find-directory ()
"Find and open `org-roam-directory'." "Find and open `org-roam-directory'."