Push links to Org items always to top of currently known links

When adding a link to an item via `org-store-link`, and the link is not known
yet, the links is always put at the beginning of the list of currently known
links, as stored in `org-stored-links`.  This allows to conveniently insert this
link via `org-insert-link` by just choosing the first element of the list, which
is selected by default.

However, when the link to the requested item is already present in
`org-stored-links`, the link is not pushed to the beginning of
`org-stored-links` by `org-store-link`, but kept where it is.  When calling
`org-insert-link` to insert a link to the item, manual selection of the correct
link is required, which is annoying and unnecessarily interrupting the current
workflow.  Even worse, when overlooking the notification that the link is
already stored, one will assume that the link to the requested item is at the
top of `org-stored-links` (which is isn't), subsequently inserting false links
when blindly calling `org-insert-link`.  (Yes, this has happend to me …)

This patch fixes this issue by ensuring that links to items (regardless whether
they have already been known or not) are always put at the front of the
`org-stored-links`.  This patch also removes the rarely used
`db/org-clear-stored-links` function, whose purpose was to provide some kind of
workaround, but turned out not to be convenient enough to actually be
used (because it also removed potentially useful links when clearing the cache).
This commit is contained in:
Daniel - 2022-12-20 08:46:47 +01:00
parent 46581c8867
commit a54ade74a4
No known key found for this signature in database
GPG Key ID: 784AA8DF0CCDF625
2 changed files with 23 additions and 10 deletions

25
init.el
View File

@ -761,7 +761,6 @@
db/org-onenote-open
db/org-outlook-open
db/org-rfc-open
db/org-clear-stored-links
db/org-cleanup-continuous-clocks
db/find-csv-in-org
db/org-mark-current-default-task
@ -1047,7 +1046,29 @@ respectively."
(use-package ol
:init (setq org-link-keep-stored-after-insertion t)
:commands (org-store-link))
:commands (org-store-link)
:config (progn
(define-advice org-store-link (:around
(orig-func &rest args)
db/org--push-new-links-to-beginning)
"Ensure that new links in `org-store-link' are always at the beginning."
;; The idea here is to store the list of already known links,
;; stored in `org-stored-links', and call `org-store-link' with an
;; empty value of `org-stored-links'. This way, the link to the
;; current item is store in any case, and we prepend these new
;; values (can be more than one if CUSTOM_ID is set) to the old
;; list of links. We also remove the newly prepended links from
;; the list of already known links.
(let ((org-stored-links--original org-stored-links)
(org-stored-links--new (let (org-stored-links)
;; This is the call to `org-store-link'.
(apply orig-func args)
org-stored-links)))
(setq org-stored-links (nconc org-stored-links--new
(cl-remove-if #'(lambda (x)
(member x org-stored-links--new))
org-stored-links--original)))))))
(use-package org-id
:init (setq org-id-link-to-org-use-id t))

View File

@ -787,14 +787,6 @@ open RFC in HTML format in the default browser."
(warn "`db/rfc-cache-path' not defined or not an absolute writable path, opening RFC in browser.")
(browse-url (concat "https://tools.ietf.org/html/rfc" number)))))
(defun db/org-clear-stored-links ()
"Clear list of stored links by setting `org-stored-links' to NIL.
This might be handy when links are kept by setting
`org-link-keep-stored-after-insertion' to T, but too many links
have accumulated over time."
(interactive)
(setq org-stored-links nil))
;;; Org Utilities