(feat!)capture: change id creation to headline on entry-type capture-templates

This commit is contained in:
Jethro Kuan
2022-01-20 14:46:22 -08:00
parent 445e3594b2
commit b5acacac20
3 changed files with 62 additions and 62 deletions

View File

@ -17,6 +17,7 @@
### Removed ### Removed
### Fixed ### Fixed
- [#2055](https://github.com/org-roam/org-roam/pull/2055) dailies: removed stray f require, which was causing require and compilation errors - [#2055](https://github.com/org-roam/org-roam/pull/2055) dailies: removed stray f require, which was causing require and compilation errors
- [#2056](https://github.com/org-roam/org-roam/pull/2056) capture: IDs are now created for entries in capture templates
### Changed ### Changed
- [#2040](https://github.com/org-roam/org-roam/pull/2040) completions: fix completions display-width for Helm users - [#2040](https://github.com/org-roam/org-roam/pull/2040) completions: fix completions display-width for Helm users
- [#2025](https://github.com/org-roam/org-roam/pull/2025) chore: removed the dependencies on f.el and s.el - [#2025](https://github.com/org-roam/org-roam/pull/2025) chore: removed the dependencies on f.el and s.el

View File

@ -317,11 +317,12 @@ In this case, interactive selection will be bypassed."
(when goto (run-hooks 'org-roam-dailies-find-file-hook))) (when goto (run-hooks 'org-roam-dailies-find-file-hook)))
(add-hook 'org-roam-capture-preface-hook #'org-roam-dailies--override-capture-time-h) (add-hook 'org-roam-capture-preface-hook #'org-roam-dailies--override-capture-time-h)
(defun org-roam-dailies--override-capture-time-h () (defun org-roam-dailies--override-capture-time-h ()
"Override the `:default-time' with the time from `:override-default-time'." "Override the `:default-time' with the time from `:override-default-time'."
(prog1 nil (when (org-roam-capture--get :override-default-time)
(when (org-roam-capture--get :override-default-time) (org-capture-put :default-time (org-roam-capture--get :override-default-time)))
(org-capture-put :default-time (org-roam-capture--get :override-default-time))))) nil)
;;; Bindings ;;; Bindings
(defvar org-roam-dailies-map (make-sparse-keymap) (defvar org-roam-dailies-map (make-sparse-keymap)

View File

@ -466,22 +466,21 @@ processing by `org-capture'.
Note: During the capture process this function is run by Note: During the capture process this function is run by
`org-capture-set-target-location', as a (function ...) based `org-capture-set-target-location', as a (function ...) based
capture target." capture target."
(let ((id (cond ((run-hook-with-args-until-success 'org-roam-capture-preface-hook)) (if-let ((id (run-hook-with-args-until-success 'org-roam-capture-preface-hook)))
(t (org-roam-capture--setup-target-location))))) (org-roam-capture--put :id id)
(org-roam-capture--adjust-point-for-capture-type) (org-roam-capture--setup-target-location))
(let ((template (org-capture-get :template))) (let ((template (org-capture-get :template)))
(when (stringp template) (when (stringp template)
(org-capture-put (org-capture-put
:template :template
(org-roam-capture--fill-template template)))) (org-roam-capture--fill-template template))))
(org-roam-capture--put :id id) (org-roam-capture--put :finalize (or (org-capture-get :finalize)
(org-roam-capture--put :finalize (or (org-capture-get :finalize) (org-roam-capture--get :finalize))))
(org-roam-capture--get :finalize)))))
(defun org-roam-capture--setup-target-location () (defun org-roam-capture--setup-target-location ()
"Initialize the buffer, and goto the location of the new capture. "Initialize the buffer, and goto the location of the new capture."
Return the ID of the location." (let ((target-entry-p t)
(let (p new-file-p) p new-file-p id)
(pcase (org-roam-capture--get-target) (pcase (org-roam-capture--get-target)
(`(file ,path) (`(file ,path)
(setq path (org-roam-capture--target-truepath path) (setq path (org-roam-capture--target-truepath path)
@ -489,7 +488,8 @@ Return the ID of the location."
(when new-file-p (org-roam-capture--put :new-file path)) (when new-file-p (org-roam-capture--put :new-file path))
(set-buffer (org-capture-target-buffer path)) (set-buffer (org-capture-target-buffer path))
(widen) (widen)
(setq p (goto-char (point-min)))) (setq p (goto-char (point-min))
target-entry-p nil))
(`(file+olp ,path ,olp) (`(file+olp ,path ,olp)
(setq path (org-roam-capture--target-truepath path) (setq path (org-roam-capture--target-truepath path)
new-file-p (org-roam-capture--new-file-p path)) new-file-p (org-roam-capture--new-file-p path))
@ -507,7 +507,8 @@ Return the ID of the location."
(org-roam-capture--put :new-file path) (org-roam-capture--put :new-file path)
(insert (org-roam-capture--fill-template head t))) (insert (org-roam-capture--fill-template head t)))
(widen) (widen)
(setq p (goto-char (point-min)))) (setq p (goto-char (point-min))
target-entry-p nil))
(`(file+head+olp ,path ,head ,olp) (`(file+head+olp ,path ,head ,olp)
(setq path (org-roam-capture--target-truepath path) (setq path (org-roam-capture--target-truepath path)
new-file-p (org-roam-capture--new-file-p path)) new-file-p (org-roam-capture--new-file-p path))
@ -569,17 +570,45 @@ Return the ID of the location."
(user-error "No node with title or id \"%s\"" title-or-id)))) (user-error "No node with title or id \"%s\"" title-or-id))))
(set-buffer (org-capture-target-buffer (org-roam-node-file node))) (set-buffer (org-capture-target-buffer (org-roam-node-file node)))
(goto-char (org-roam-node-point node)) (goto-char (org-roam-node-point node))
(setq p (org-roam-node-point node))))) (setq p (org-roam-node-point node)
target-entry-p (and (derived-mode-p 'org-mode) (org-at-heading-p))))))
;; Setup `org-id' for the current capture target and return it back to the ;; Setup `org-id' for the current capture target and return it back to the
;; caller. ;; caller.
(save-excursion ;; Unless it's an entry type, then we want to create an ID for the entry instead
(goto-char p) (pcase (org-capture-get :type)
(if-let ((id (org-entry-get p "ID"))) ('entry
(setf (org-roam-node-id org-roam-capture--node) id) (advice-add #'org-capture-place-entry :after #'org-roam-capture--create-id-for-entry)
(org-entry-put p "ID" (org-roam-node-id org-roam-capture--node))) (org-roam-capture--put :new-node-p t)
(prog1 (setq id (org-roam-node-id org-roam-capture--node)))
(org-id-get) (_
(run-hooks 'org-roam-capture-new-node-hook))))) (save-excursion
(goto-char p)
(unless (org-entry-get p "ID")
(org-roam-capture--put :new-node-p t))
(setq id (or (org-entry-get p "ID")
(org-roam-node-id org-roam-capture--node)))
(setf (org-roam-node-id org-roam-capture--node) id)
(org-entry-put p "ID" id))))
(org-roam-capture--put :id id)
(org-roam-capture--put :target-entry-p target-entry-p)
(advice-add #'org-capture-place-template :before #'org-roam-capture--set-target-entry-p-a)
(advice-add #'org-capture-place-template :after #'org-roam-capture-run-new-node-hook-a)))
(defun org-roam-capture--set-target-entry-p-a (_)
"Correct `:target-entry-p' in Org-capture template based on `:target.'"
(org-capture-put :target-entry-p (org-roam-capture--get :target-entry-p))
(advice-remove #'org-capture-place-template #'org-roam-capture--set-target-entry-p-a))
(defun org-roam-capture-run-new-node-hook-a (_)
"Advice to run after the Org-capture template is placed."
(when (org-roam-capture--get :new-node-p)
(run-hooks 'org-roam-capture-new-node-hook))
(advice-remove #'org-capture-place-template #'org-roam-capture--place-template-a))
(defun org-roam-capture--create-id-for-entry ()
"Create the ID for the new entry."
(org-entry-put (point) "ID" (org-roam-capture--get :id))
(advice-remove #'org-capture-place-entry #'org-roam-capture--create-id-for-entry))
(defun org-roam-capture--get-target () (defun org-roam-capture--get-target ()
"Get the current capture :target for the capture template in use." "Get the current capture :target for the capture template in use."
@ -592,9 +621,9 @@ PATH is a string that can optionally contain templated text in
it." it."
(or (org-roam-node-file org-roam-capture--node) (or (org-roam-node-file org-roam-capture--node)
(thread-first path (thread-first path
(org-roam-capture--fill-template t) (org-roam-capture--fill-template t)
(string-trim) (string-trim)
(expand-file-name org-roam-directory)))) (expand-file-name org-roam-directory))))
(defun org-roam-capture--new-file-p (path) (defun org-roam-capture--new-file-p (path)
"Return t if PATH is for a new file with no visiting buffer." "Return t if PATH is for a new file with no visiting buffer."
@ -648,37 +677,6 @@ you can catch it with `condition-case'."
end (save-excursion (org-end-of-subtree t t)))) end (save-excursion (org-end-of-subtree t t))))
(point-marker)))) (point-marker))))
(defun org-roam-capture--adjust-point-for-capture-type (&optional pos)
"Reposition the point for template insertion dependently on the capture type.
Return the newly adjusted position of `point'.
POS is the current position of point (an integer) inside the
currently active capture buffer, where the adjustment should
start to begin from. If it's nil, then it will default to
the current value of `point'."
(or pos (setq pos (point)))
(goto-char pos)
(let ((location-type (if (= pos 1) 'beginning-of-file 'heading-at-point)))
(and (eq location-type 'heading-at-point)
(cl-assert (org-at-heading-p)))
(pcase (org-capture-get :type)
(`plain
(cl-case location-type
(beginning-of-file
(if (org-capture-get :prepend)
(let ((el (org-element-at-point)))
(while (and (not (eobp))
(memq (org-element-type el)
'(drawer property-drawer keyword comment comment-block horizontal-rule)))
(goto-char (org-element-property :end el))
(setq el (org-element-at-point))))
(goto-char (org-entry-end-position))))
(heading-at-point
(if (org-capture-get :prepend)
(org-end-of-meta-data t)
(goto-char (org-entry-end-position))))))))
(point))
;;;; Finalizers ;;;; Finalizers
(add-hook 'org-capture-prepare-finalize-hook #'org-roam-capture--install-finalize-h) (add-hook 'org-capture-prepare-finalize-hook #'org-roam-capture--install-finalize-h)
(defun org-roam-capture--install-finalize-h () (defun org-roam-capture--install-finalize-h ()