Do not use `org-store-link` when inserting links to other items

When the link to another item is already present in the history of
`org-store-link`, nothing is updated.  Inserting the topmost link then results
in a wrong link being inserted.

Not using `org-store-link` at all fixes this problem and also leaves the history
of `org-store-link` untouched.  It also simplifies the implementation by not
relying on the complexity of `org-store-link`, but instead just only using
`org-entry-get` and `org-id-get-create`.
This commit is contained in:
Daniel - 2021-04-29 19:52:54 +02:00
parent 3bbf747aca
commit a0b1f4df0d
No known key found for this signature in database
GPG Key ID: 1C7071A75BB72D64
1 changed files with 13 additions and 13 deletions

View File

@ -729,31 +729,31 @@ Search through all items of the current buffer, or
`db/org-default-org-file' if the current buffer is not associated `db/org-default-org-file' if the current buffer is not associated
with a file. If ARG is non-nil, include all files in the with a file. If ARG is non-nil, include all files in the
variables `org-agenda-files' and variables `org-agenda-files' and
`org-agenda-text-search-extra-files' in this search. `org-agenda-text-search-extra-files' in this search."
Use `org-store-link' to save link to `org-stored-links'."
(interactive "P") (interactive "P")
(unless (derived-mode-p 'org-mode) (unless (derived-mode-p 'org-mode)
(user-error "Not in Org Mode")) (user-error "Not in Org Mode"))
(let ((pom (db/org--get-location arg))) (let* ((pom (db/org--get-location arg))
item id)
(org-with-point-at pom (org-with-point-at pom
(org-store-link nil t)) (setq item (org-entry-get (point) "ITEM")
(insert (apply #'format "[[%s][%s]]" (cl-first org-stored-links))))) id (org-id-get-create)))
(insert (format "[[%s][%s]]" id item))))
(defun db/org-add-link-to-current-clock () (defun db/org-add-link-to-current-clock ()
"Insert link to currently clocked-in item at point. "Insert link to currently clocked-in item at point.
Error out when not in an Org Mode buffer or when the clock is not
Uses `org-store-link' and `org-insert-link'. Error out when not active."
in an Org Mode buffer or when the clock is not active."
(interactive) (interactive)
(unless (derived-mode-p 'org-mode) (unless (derived-mode-p 'org-mode)
(user-error "Not in Org Mode, aborting")) (user-error "Not in Org Mode, aborting"))
(unless org-clock-marker (unless org-clock-marker
(user-error "No clocked-in task, aborting")) (user-error "No clocked-in task, aborting"))
(org-with-point-at org-clock-marker (let (item id)
(org-store-link nil t)) (org-with-point-at org-clock-marker
(pcase-let ((`(,location ,description) (cl-first org-stored-links))) (setq item (org-entry-get (point) "ITEM")
(org-insert-link nil location description))) id (org-id-get-create)))
(insert (format "[[%s][%s]]" id item))))
(defhydra hydra-org-linking (:color blue :hint none) (defhydra hydra-org-linking (:color blue :hint none)
" "