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)
(when-let ((val (completing-read (format "%s: " key) nil)))
(push (cons key val) org-roam-capture--info)
val)))
nil)))
val))))))
(defun org-roam-capture--save-file-maybe ()
"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))))
(_ (user-error "Invalid capture template format: %s" template))))
(defcustom org-roam-capture-function #'org-capture
"Function that is invoked to start the `org-capture' process."
:group 'org-roam
:type 'function)
(defun org-roam-capture--capture (&optional goto keys)
"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)
(cl-defun org-roam-capture--capture (&key goto keys info context)
(let* ((org-capture-templates
(mapcar #'org-roam-capture--convert-template org-roam-capture-templates))
(org-roam-capture--info info)
(org-roam-capture--context context)
(one-template-p (= (length org-capture-templates) 1)))
(when one-template-p
(setq keys (caar org-capture-templates)))
(if (or one-template-p
(eq org-roam-capture-function 'org-capture))
(org-capture goto keys)
(funcall-interactively org-roam-capture-function))))
(org-capture goto keys)))
;;;###autoload
(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'."
(interactive "P")
(let ((node (org-roam-node-read)))
;; TODO: fix this
(if (org-roam-node-id node)
(let ((org-roam-capture--info (list (cons 'title (org-roam-node-title node))
(cons 'slug (funcall org-roam-title-to-slug-function
(condition-case err
(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)))
(cons 'file (org-roam-node-file node))))
(org-roam-capture--context 'capture))
(condition-case err
(org-roam-capture--capture goto keys)
(error (user-error "%s. Please adjust `org-roam-capture-templates'"
(error-message-string err))))))))
(file . ,(org-roam-node-file node)))
:context 'capture)
(error (user-error "%s. Please adjust `org-roam-capture-templates'"
(error-message-string err)))))))
(provide 'org-roam-capture)

View File

@ -491,8 +491,8 @@ nodes."
(defcustom org-roam-node-display-template
"${title:48} ${tags:10}"
"Configures display formatting for Org-roam node."
:group 'org-roam
:type 'string)
:group 'org-roam
:type 'string)
(defun org-roam--tags-to-str (tags)
"Convert list of TAGS into a string."
@ -503,29 +503,29 @@ nodes."
(defun org-roam-node--format-entry (node width)
"Formats NODE for display in the results list.
WIDTH is the width of the results list."
(let ((format (org-roam--process-display-format org-roam-node-display-template)))
(s-format (car format)
(lambda (field)
(let* ((field (split-string field ":"))
(field-name (car field))
(field-width (cadr field))
(getter (intern (concat "org-roam-node-" field-name)))
(field-value (or (funcall getter node) "")))
(when (and (equal field-name "tags")
field-value)
(setq field-value (org-roam--tags-to-str field-value)))
(when (and (equal field-name "file")
field-value)
(setq field-value (file-relative-name field-value org-roam-directory)))
(if (not field-width)
field-value
(setq field-width (string-to-number field-width))
(truncate-string-to-width
field-value
(if (> field-width 0)
field-width
(- width (cdr format)))
0 ?\s)))))))
(let ((format (org-roam--process-display-format org-roam-node-display-template)))
(s-format (car format)
(lambda (field)
(let* ((field (split-string field ":"))
(field-name (car field))
(field-width (cadr field))
(getter (intern (concat "org-roam-node-" field-name)))
(field-value (or (funcall getter node) "")))
(when (and (equal field-name "tags")
field-value)
(setq field-value (org-roam--tags-to-str field-value)))
(when (and (equal field-name "file")
field-value)
(setq field-value (file-relative-name field-value org-roam-directory)))
(if (not field-width)
field-value
(setq field-width (string-to-number field-width))
(truncate-string-to-width
field-value
(if (> field-width 0)
field-width
(- width (cdr format)))
0 ?\s)))))))
(defun org-roam-node-preview (file 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)))
(if (org-roam-node-file node)
(org-roam-node-visit node other-window)
(let ((org-roam-capture--info `((title . ,(org-roam-node-title node))
(slug . ,(funcall org-roam-title-to-slug-function
(org-roam-node-title node)))))
(org-roam-capture--context 'title))
(setq org-roam-capture-additional-template-props (list :finalize 'find-file))
(org-roam-capture--capture)))))
(setq org-roam-capture-additional-template-props (list :finalize 'find-file))
(org-roam-capture--capture :info `((title . ,(org-roam-node-title node))
(slug . ,(funcall org-roam-title-to-slug-function (org-roam-node-title node))))
:context 'title))))
(defun org-roam-node-insert (&optional filter-fn)
"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
(concat "id:" (org-roam-node-id node))
description)))
(let ((org-roam-capture--info
`((title . ,(org-roam-node-title node))
(slug . ,(funcall org-roam-title-to-slug-function (org-roam-node-title node)))))
(org-roam-capture--context 'title))
(setq org-roam-capture-additional-template-props
(list :region (when (and beg end)
(cons beg end))
:insert-at (point-marker)
:link-description description
:finalize 'insert-link))
(org-roam-capture--capture)))))
(setq org-roam-capture-additional-template-props
(list :region (when (and beg end)
(cons beg end))
:insert-at (point-marker)
:link-description description
:finalize 'insert-link))
(org-roam-capture--capture :info `((title . ,(org-roam-node-title node))
(slug . ,(funcall org-roam-title-to-slug-function (org-roam-node-title node))))
:context 'title))))
(deactivate-mark)))
;;;###autoload