(feat) implement after-find-file-hook for org-roam-capture (#482)

Allows user to run functions when an `org-roam-capture` is successful.  This
might come in handy for moving point after finding the new file, since right
now, it is at `(point-min)`.
This commit is contained in:
Leo Vivier
2020-04-19 16:45:09 +02:00
committed by GitHub
parent 258d6f8a52
commit 89b941a207

View File

@ -149,14 +149,6 @@ Next, it expands the remaining template string using
(completing-read (format "%s: " key ) nil))) nil) (completing-read (format "%s: " key ) nil))) nil)
(org-capture-fill-template))) (org-capture-fill-template)))
(defun org-roam-capture--find-file-h ()
"Opens the newly created template file.
This is added as a hook to `org-capture-after-finalize-hook'."
(when-let ((file-path (org-roam-capture--get :file-path)))
(unless org-note-abort
(find-file file-path)))
(remove-hook 'org-capture-after-finalize-hook #'org-roam-capture--find-file-h))
(defun org-roam-capture--insert-link-h () (defun org-roam-capture--insert-link-h ()
"Insert the link into the original buffer, after the capture process is done. "Insert the link into the original buffer, after the capture process is done.
This is added as a hook to `org-capture-after-finalize-hook'." This is added as a hook to `org-capture-after-finalize-hook'."
@ -303,6 +295,22 @@ This function is used solely in Org-roam's capture templates: see
(push val converted))) (push val converted)))
(append (nreverse converted) `(:org-roam ,org-roam-plist)))) (append (nreverse converted) `(:org-roam ,org-roam-plist))))
(defun org-roam-capture--find-file-h ()
"Opens the newly created template file.
This is added as a hook to `org-capture-after-finalize-hook'.
Run the hooks defined in `org-roam-capture-after-find-file-hook'."
(unless org-note-abort
(when-let ((file-path (org-roam-capture--get :file-path)))
(find-file file-path))
(run-hooks 'org-roam-capture-after-find-file-hook))
(remove-hook 'org-capture-after-finalize-hook #'org-roam-capture--find-file-h))
(defcustom org-roam-capture-after-find-file-hook nil
"Hook that is run right after an Org-roam capture process is finalized.
Suitable for moving point."
:group 'org-roam
:type 'hook)
(defun org-roam--capture (&optional goto keys) (defun org-roam--capture (&optional goto keys)
"Create a new file, and return the path to the edited file. "Create a new file, and return the path to the edited file.
The templates are defined at `org-roam-capture-templates'. The The templates are defined at `org-roam-capture-templates'. The