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