mirror of
https://github.com/org-roam/org-roam
synced 2025-08-03 12:27:23 -05:00
remove org-roam-capture--context
This commit is contained in:
@ -47,19 +47,6 @@
|
|||||||
This variable is populated dynamically, and is only non-nil
|
This variable is populated dynamically, and is only non-nil
|
||||||
during the Org-roam capture process.")
|
during the Org-roam capture process.")
|
||||||
|
|
||||||
(defvar org-roam-capture--context nil
|
|
||||||
"A symbol, that reflects the context for obtaining the exact point in a file.
|
|
||||||
This variable is populated dynamically, and is only active during
|
|
||||||
an Org-roam capture process.
|
|
||||||
|
|
||||||
The `title' context is used in `org-roam-insert' and
|
|
||||||
`org-roam-find-file', where the capture process is triggered upon
|
|
||||||
trying to create a new file without that `title'.
|
|
||||||
|
|
||||||
The `ref' context is used by `org-roam-protocol', where the
|
|
||||||
capture process is triggered upon trying to find or create a new
|
|
||||||
note with the given `ref'.")
|
|
||||||
|
|
||||||
(defvar org-roam-capture-additional-template-props nil
|
(defvar org-roam-capture-additional-template-props nil
|
||||||
"Additional props to be added to the Org-roam template.")
|
"Additional props to be added to the Org-roam template.")
|
||||||
|
|
||||||
@ -336,15 +323,12 @@ The file is saved if the original value of :no-save is not t and
|
|||||||
(with-current-buffer (org-capture-get :buffer)
|
(with-current-buffer (org-capture-get :buffer)
|
||||||
(save-buffer)))))
|
(save-buffer)))))
|
||||||
|
|
||||||
(defun org-roam-capture--new-file (&optional allow-existing-file-p)
|
(defun org-roam-capture--new-file ()
|
||||||
"Return the path to file during an Org-roam capture.
|
"Return the path to file during an Org-roam capture.
|
||||||
|
|
||||||
This function reads the file-name attribute of the currently
|
This function reads the file-name attribute of the currently
|
||||||
active Org-roam template.
|
active Org-roam template.
|
||||||
|
|
||||||
If the file path already exists, and not ALLOW-EXISTING-FILE-P,
|
|
||||||
raise a warning.
|
|
||||||
|
|
||||||
Else, to insert the header content in the file, `org-capture'
|
Else, to insert the header content in the file, `org-capture'
|
||||||
prepends the `:head' property of the Org-roam capture template.
|
prepends the `:head' property of the Org-roam capture template.
|
||||||
|
|
||||||
@ -365,27 +349,11 @@ the file if the original value of :no-save is not t and
|
|||||||
(roam-template (concat roam-head org-template)))
|
(roam-template (concat roam-head org-template)))
|
||||||
(if (or (file-exists-p file-path)
|
(if (or (file-exists-p file-path)
|
||||||
(find-buffer-visiting file-path))
|
(find-buffer-visiting file-path))
|
||||||
(unless allow-existing-file-p
|
|
||||||
(lwarn '(org-roam) :warning
|
|
||||||
"Attempted to recreate existing file: %s.
|
|
||||||
This can happen when your org-roam db is not in sync with your notes.
|
|
||||||
Using existing file..." file-path))
|
|
||||||
(make-directory (file-name-directory file-path) t)
|
(make-directory (file-name-directory file-path) t)
|
||||||
(org-roam-capture--put :orig-no-save (org-capture-get :no-save)
|
(org-roam-capture--put :orig-no-save (org-capture-get :no-save)
|
||||||
:new-file t)
|
:new-file t)
|
||||||
(pcase org-roam-capture--context
|
|
||||||
('dailies
|
|
||||||
;; Populate the header of the daily file before capture to prevent it
|
|
||||||
;; from appearing in the buffer-restriction
|
|
||||||
(save-window-excursion
|
|
||||||
(find-file file-path)
|
|
||||||
(insert (substring (org-capture-fill-template (concat roam-head "*"))
|
|
||||||
0 -2))
|
|
||||||
(set-buffer-modified-p nil))
|
|
||||||
(org-capture-put :template org-template))
|
|
||||||
(_
|
|
||||||
(org-capture-put :template roam-template
|
(org-capture-put :template roam-template
|
||||||
:type 'plain)))
|
:type 'plain)
|
||||||
(org-capture-put :no-save t))
|
(org-capture-put :no-save t))
|
||||||
file-path))
|
file-path))
|
||||||
|
|
||||||
@ -445,38 +413,25 @@ you can catch it with `condition-case'."
|
|||||||
|
|
||||||
(defun org-roam-capture--get-point ()
|
(defun org-roam-capture--get-point ()
|
||||||
"Return exact point to file for org-capture-template.
|
"Return exact point to file for org-capture-template.
|
||||||
The file to use is dependent on the context:
|
|
||||||
|
|
||||||
If the search is via title, it is assumed that the file does not
|
|
||||||
yet exist, and Org-roam will attempt to create new file.
|
|
||||||
|
|
||||||
If the search is via daily notes, 'time will be passed via
|
|
||||||
`org-roam-capture--info'. This is used to alter the default time
|
|
||||||
in `org-capture-templates'.
|
|
||||||
|
|
||||||
If the search is via ref, it is matched against the Org-roam database.
|
|
||||||
If there is no file with that ref, a file with that ref is created.
|
|
||||||
|
|
||||||
This function is used solely in Org-roam's capture templates: see
|
This function is used solely in Org-roam's capture templates: see
|
||||||
`org-roam-capture-templates'."
|
`org-roam-capture-templates'."
|
||||||
(let* ((file-path (pcase org-roam-capture--context
|
(let* ((file-path
|
||||||
('capture
|
(cond ((assoc 'file org-roam-capture--info)
|
||||||
(or (cdr (assoc 'file org-roam-capture--info))
|
(cdr (assoc 'file org-roam-capture--info)))
|
||||||
(org-roam-capture--new-file)))
|
((assoc 'ref org-roam-capture--info)
|
||||||
('title
|
(let ((ref (cdr (assoc 'ref org-roam-capture--info))))
|
||||||
(org-roam-capture--new-file))
|
(if-let ((file ((caar (org-roam-db-query
|
||||||
('dailies
|
[:select [file]
|
||||||
(org-capture-put :default-time (cdr (assoc 'time org-roam-capture--info)))
|
:from refs
|
||||||
(org-roam-capture--new-file 'allow-existing))
|
:where (= ref $s1)
|
||||||
;; ('ref
|
:limit 1]
|
||||||
;; (if-let ((ref (cdr (assoc 'ref org-roam-capture--info))))
|
ref)))))
|
||||||
;; (pcase (org-roam--split-ref ref)
|
file
|
||||||
;; (`(,type . ,path)
|
(user-error "No such ref \"%s\"" ref))))
|
||||||
;; (or (org-roam-capture--get-ref-path type path)
|
(t
|
||||||
;; (org-roam-capture--new-file)))
|
(when-let ((time (cdr (assoc 'time org-roam-capture--info))))
|
||||||
;; (_ (user-error "%s is not a valid ref" ref)))
|
(org-capture-put :default-time time))
|
||||||
;; (error "Ref not found in `org-roam-capture--info'")))
|
(org-roam-capture--new-file)))))
|
||||||
(_ (error "Invalid org-roam-capture-context")))))
|
|
||||||
(org-capture-put :template
|
(org-capture-put :template
|
||||||
(org-roam-capture--fill-template (org-capture-get :template)))
|
(org-roam-capture--fill-template (org-capture-get :template)))
|
||||||
(org-roam-capture--put :file-path file-path
|
(org-roam-capture--put :file-path file-path
|
||||||
@ -520,15 +475,13 @@ 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))))
|
||||||
|
|
||||||
(cl-defun org-roam-capture--capture (&key goto keys info context)
|
(cl-defun org-roam-capture--capture (&key goto keys info)
|
||||||
"Main entry point.
|
"Main entry point.
|
||||||
GOTO and KEYS correspond to `org-capture' arguments.
|
GOTO and KEYS correspond to `org-capture' arguments.
|
||||||
INFO is an alist for filling up Org-roam's capture templates.
|
INFO is an alist for filling up Org-roam's capture templates."
|
||||||
CONTEXT is the context of the call. TODO fix this"
|
|
||||||
(let* ((org-capture-templates
|
(let* ((org-capture-templates
|
||||||
(mapcar #'org-roam-capture--convert-template org-roam-capture-templates))
|
(mapcar #'org-roam-capture--convert-template org-roam-capture-templates))
|
||||||
(org-roam-capture--info info)
|
(org-roam-capture--info info)
|
||||||
(org-roam-capture--context context)
|
|
||||||
(one-template-p (= (length org-capture-templates) 1)))
|
(one-template-p (= (length org-capture-templates) 1)))
|
||||||
(when one-template-p
|
(when one-template-p
|
||||||
(setq keys (caar org-capture-templates)))
|
(setq keys (caar org-capture-templates)))
|
||||||
@ -546,8 +499,7 @@ Arguments GOTO and KEYS see `org-capture'."
|
|||||||
:info `((title . ,(org-roam-node-title node))
|
:info `((title . ,(org-roam-node-title node))
|
||||||
(slug . ,(funcall org-roam-title-to-slug-function
|
(slug . ,(funcall org-roam-title-to-slug-function
|
||||||
(org-roam-node-title node)))
|
(org-roam-node-title node)))
|
||||||
(file . ,(org-roam-node-file node)))
|
(file . ,(org-roam-node-file node))))))
|
||||||
:context 'capture)))
|
|
||||||
|
|
||||||
(provide 'org-roam-capture)
|
(provide 'org-roam-capture)
|
||||||
|
|
||||||
|
@ -78,7 +78,6 @@ It opens or creates a note with the given ref.
|
|||||||
:annotation orglink
|
:annotation orglink
|
||||||
:initial body)))
|
:initial body)))
|
||||||
(let* ((org-roam-capture-templates org-roam-capture-ref-templates)
|
(let* ((org-roam-capture-templates org-roam-capture-ref-templates)
|
||||||
(org-roam-capture--context 'ref)
|
|
||||||
(org-roam-capture--info decoded-alist)
|
(org-roam-capture--info decoded-alist)
|
||||||
(org-capture-link-is-already-stored t)
|
(org-capture-link-is-already-stored t)
|
||||||
(template (cdr (assoc 'template decoded-alist))))
|
(template (cdr (assoc 'template decoded-alist))))
|
||||||
|
@ -690,8 +690,7 @@ If OTHER-WINDOW, visit the NODE in another window."
|
|||||||
(setq org-roam-capture-additional-template-props (list :finalize 'find-file))
|
(setq org-roam-capture-additional-template-props (list :finalize 'find-file))
|
||||||
(org-roam-capture--capture
|
(org-roam-capture--capture
|
||||||
:info `((title . ,(org-roam-node-title node))
|
:info `((title . ,(org-roam-node-title node))
|
||||||
(slug . ,(funcall org-roam-title-to-slug-function (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)
|
(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.
|
||||||
@ -729,8 +728,7 @@ which takes as its argument an alist of path-completions."
|
|||||||
:finalize 'insert-link))
|
:finalize 'insert-link))
|
||||||
(org-roam-capture--capture
|
(org-roam-capture--capture
|
||||||
:info `((title . ,(org-roam-node-title node))
|
:info `((title . ,(org-roam-node-title node))
|
||||||
(slug . ,(funcall org-roam-title-to-slug-function (org-roam-node-title node))))
|
(slug . ,(funcall org-roam-title-to-slug-function (org-roam-node-title node))))))))
|
||||||
:context 'title))))
|
|
||||||
(deactivate-mark)))
|
(deactivate-mark)))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
|
Reference in New Issue
Block a user