(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 'dash)
(require 's)
(require 'cl-lib)
;; Declarations
(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)
"Convert TEMPLATE from Org-roam syntax to `org-capture-templates' syntax."
(let* ((copy (copy-tree template))
converted
org-roam-plist
key
val)
;;put positional args on converted template
(dotimes (_ 5)
(push (pop copy) converted))
(while (setq key (pop copy)
val (pop copy))
(if (member key org-roam-capture--template-keywords)
(progn
(push val org-roam-plist)
(push key org-roam-plist))
(push key converted)
(push val converted)))
(append (nreverse converted) `(:org-roam ,org-roam-plist))))
(pcase template
(`(,_key ,_description) template)
(`(,key ,description ,type ,target . ,rest)
(let ((converted `(,key ,description ,type ,target
,(unless (keywordp (car rest)) (pop rest))))
org-roam-plist
options)
(while rest
(let* ((key (pop rest))
(val (pop rest))
(custom (member key org-roam-capture--template-keywords)))
(push val (if custom org-roam-plist options))
(push key (if custom org-roam-plist options))))
(append converted options `(:org-roam ,org-roam-plist))))
(_ (user-error "Invalid capture template format: %s" template))))
(defun org-roam-capture--find-file-h ()
"Opens the newly created template file.
@ -339,7 +338,10 @@ This uses the templates defined at `org-roam-capture-templates'."
(cons 'file file-path)))
(org-roam-capture--context '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)

View File

@ -34,6 +34,7 @@
;;; Library Requires
(require 'org-capture)
(require 'org-roam-capture)
(require 'org-roam-macs)
(defvar org-roam-dailies-capture-templates
'(("d" "daily" plain (function org-roam-capture--get-point)
@ -52,7 +53,8 @@
(org-roam-capture--info (list (cons 'time time)))
(org-roam-capture--context 'dailies))
(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 ()
"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))
,@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)
;;; 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)
(template (cdr (assoc 'template decoded-alist))))
(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.")))
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
:link-description link-description
: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 ()
"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))))
(org-roam-capture--context 'title))
(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 ()
"Find and open `org-roam-directory'."