org-roam-capture: cleanup capture calls

This commit is contained in:
Jethro Kuan
2021-04-02 20:10:04 +08:00
parent f883c3270f
commit a35b0e7c6d
2 changed files with 55 additions and 69 deletions

View File

@ -308,8 +308,7 @@ Next, it expands the remaining template string using
(or (s--aget org-roam-capture--info key) (or (s--aget org-roam-capture--info key)
(when-let ((val (completing-read (format "%s: " key) nil))) (when-let ((val (completing-read (format "%s: " key) nil)))
(push (cons key val) org-roam-capture--info) (push (cons key val) org-roam-capture--info)
val))) val))))))
nil)))
(defun org-roam-capture--save-file-maybe () (defun org-roam-capture--save-file-maybe ()
"Save the file conditionally. "Save the file conditionally.
@ -524,25 +523,15 @@ This function is used solely in Org-roam's capture templates: see
(append converted options `(:org-roam ,org-roam-plist)))) (append converted options `(:org-roam ,org-roam-plist))))
(_ (user-error "Invalid capture template format: %s" template)))) (_ (user-error "Invalid capture template format: %s" template))))
(defcustom org-roam-capture-function #'org-capture (cl-defun org-roam-capture--capture (&key goto keys info context)
"Function that is invoked to start the `org-capture' process." (let* ((org-capture-templates
:group 'org-roam (mapcar #'org-roam-capture--convert-template org-roam-capture-templates))
:type 'function) (org-roam-capture--info info)
(org-roam-capture--context context)
(defun org-roam-capture--capture (&optional goto keys) (one-template-p (= (length org-capture-templates) 1)))
"Create a new file, and return the path to the edited file.
The templates are defined at `org-roam-capture-templates'. The
GOTO and KEYS argument have the same functionality as
`org-capture'."
(let* ((org-capture-templates (mapcar #'org-roam-capture--convert-template org-roam-capture-templates))
(one-template-p (= (length org-capture-templates) 1))
org-capture-templates-contexts)
(when one-template-p (when one-template-p
(setq keys (caar org-capture-templates))) (setq keys (caar org-capture-templates)))
(if (or one-template-p (org-capture goto keys)))
(eq org-roam-capture-function 'org-capture))
(org-capture goto keys)
(funcall-interactively org-roam-capture-function))))
;;;###autoload ;;;###autoload
(defun org-roam-capture (&optional goto keys) (defun org-roam-capture (&optional goto keys)
@ -551,16 +540,17 @@ This uses the templates defined at `org-roam-capture-templates'.
Arguments GOTO and KEYS see `org-capture'." Arguments GOTO and KEYS see `org-capture'."
(interactive "P") (interactive "P")
(let ((node (org-roam-node-read))) (let ((node (org-roam-node-read)))
;; TODO: fix this
(if (org-roam-node-id node) (if (org-roam-node-id node)
(let ((org-roam-capture--info (list (cons 'title (org-roam-node-title node)) (condition-case err
(cons 'slug (funcall org-roam-title-to-slug-function (org-roam-capture--capture goto keys
:info `((title . ,(org-roam-node-title node))
(slug . ,(funcall org-roam-title-to-slug-function
(org-roam-node-title node))) (org-roam-node-title node)))
(cons 'file (org-roam-node-file node)))) (file . ,(org-roam-node-file node)))
(org-roam-capture--context 'capture)) :context 'capture)
(condition-case err (error (user-error "%s. Please adjust `org-roam-capture-templates'"
(org-roam-capture--capture goto keys) (error-message-string err)))))))
(error (user-error "%s. Please adjust `org-roam-capture-templates'"
(error-message-string err))))))))
(provide 'org-roam-capture) (provide 'org-roam-capture)

View File

@ -491,8 +491,8 @@ nodes."
(defcustom org-roam-node-display-template (defcustom org-roam-node-display-template
"${title:48} ${tags:10}" "${title:48} ${tags:10}"
"Configures display formatting for Org-roam node." "Configures display formatting for Org-roam node."
:group 'org-roam :group 'org-roam
:type 'string) :type 'string)
(defun org-roam--tags-to-str (tags) (defun org-roam--tags-to-str (tags)
"Convert list of TAGS into a string." "Convert list of TAGS into a string."
@ -503,29 +503,29 @@ nodes."
(defun org-roam-node--format-entry (node width) (defun org-roam-node--format-entry (node width)
"Formats NODE for display in the results list. "Formats NODE for display in the results list.
WIDTH is the width of the results list." WIDTH is the width of the results list."
(let ((format (org-roam--process-display-format org-roam-node-display-template))) (let ((format (org-roam--process-display-format org-roam-node-display-template)))
(s-format (car format) (s-format (car format)
(lambda (field) (lambda (field)
(let* ((field (split-string field ":")) (let* ((field (split-string field ":"))
(field-name (car field)) (field-name (car field))
(field-width (cadr field)) (field-width (cadr field))
(getter (intern (concat "org-roam-node-" field-name))) (getter (intern (concat "org-roam-node-" field-name)))
(field-value (or (funcall getter node) ""))) (field-value (or (funcall getter node) "")))
(when (and (equal field-name "tags") (when (and (equal field-name "tags")
field-value) field-value)
(setq field-value (org-roam--tags-to-str field-value))) (setq field-value (org-roam--tags-to-str field-value)))
(when (and (equal field-name "file") (when (and (equal field-name "file")
field-value) field-value)
(setq field-value (file-relative-name field-value org-roam-directory))) (setq field-value (file-relative-name field-value org-roam-directory)))
(if (not field-width) (if (not field-width)
field-value field-value
(setq field-width (string-to-number field-width)) (setq field-width (string-to-number field-width))
(truncate-string-to-width (truncate-string-to-width
field-value field-value
(if (> field-width 0) (if (> field-width 0)
field-width field-width
(- width (cdr format))) (- width (cdr format)))
0 ?\s))))))) 0 ?\s)))))))
(defun org-roam-node-preview (file point) (defun org-roam-node-preview (file point)
"Get preview content for FILE at POINT." "Get preview content for FILE at POINT."
@ -670,12 +670,10 @@ If OTHER-WINDOW, visit the NODE in another window."
(let ((node (org-roam-node-read initial-input filter-fn))) (let ((node (org-roam-node-read initial-input filter-fn)))
(if (org-roam-node-file node) (if (org-roam-node-file node)
(org-roam-node-visit node other-window) (org-roam-node-visit node other-window)
(let ((org-roam-capture--info `((title . ,(org-roam-node-title node)) (setq org-roam-capture-additional-template-props (list :finalize 'find-file))
(slug . ,(funcall org-roam-title-to-slug-function (org-roam-capture--capture :info `((title . ,(org-roam-node-title node))
(org-roam-node-title node))))) (slug . ,(funcall org-roam-title-to-slug-function (org-roam-node-title node))))
(org-roam-capture--context 'title)) :context 'title))))
(setq org-roam-capture-additional-template-props (list :finalize 'find-file))
(org-roam-capture--capture)))))
(defun org-roam-node-insert (&optional filter-fn) (defun org-roam-node-insert (&optional filter-fn)
"Find an Org-roam file, and insert a relative org link to it at point. "Find an Org-roam file, and insert a relative org link to it at point.
@ -705,17 +703,15 @@ which takes as its argument an alist of path-completions."
(insert (org-link-make-string (insert (org-link-make-string
(concat "id:" (org-roam-node-id node)) (concat "id:" (org-roam-node-id node))
description))) description)))
(let ((org-roam-capture--info (setq org-roam-capture-additional-template-props
`((title . ,(org-roam-node-title node)) (list :region (when (and beg end)
(slug . ,(funcall org-roam-title-to-slug-function (org-roam-node-title node))))) (cons beg end))
(org-roam-capture--context 'title)) :insert-at (point-marker)
(setq org-roam-capture-additional-template-props :link-description description
(list :region (when (and beg end) :finalize 'insert-link))
(cons beg end)) (org-roam-capture--capture :info `((title . ,(org-roam-node-title node))
:insert-at (point-marker) (slug . ,(funcall org-roam-title-to-slug-function (org-roam-node-title node))))
:link-description description :context 'title))))
:finalize 'insert-link))
(org-roam-capture--capture)))))
(deactivate-mark))) (deactivate-mark)))
;;;###autoload ;;;###autoload