diff --git a/org-roam-capture.el b/org-roam-capture.el index d547ad4..42caa18 100644 --- a/org-roam-capture.el +++ b/org-roam-capture.el @@ -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) diff --git a/org-roam-dailies.el b/org-roam-dailies.el index af2e34c..0e92bb1 100644 --- a/org-roam-dailies.el +++ b/org-roam-dailies.el @@ -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." diff --git a/org-roam-macs.el b/org-roam-macs.el index 8f2a1a9..be0a67a 100644 --- a/org-roam-macs.el +++ b/org-roam-macs.el @@ -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 diff --git a/org-roam-protocol.el b/org-roam-protocol.el index 9ab14e8..d92494f 100644 --- a/org-roam-protocol.el +++ b/org-roam-protocol.el @@ -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) diff --git a/org-roam.el b/org-roam.el index 7131c89..1478416 100644 --- a/org-roam.el +++ b/org-roam.el @@ -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'."