From a54ade74a47d63622df864278bbd3f163fba96cd Mon Sep 17 00:00:00 2001 From: Daniel Borchmann Date: Tue, 20 Dec 2022 08:46:47 +0100 Subject: [PATCH] Push links to Org items always to top of currently known links MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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). --- init.el | 25 +++++++++++++++++++++++-- site-lisp/db-org.el | 8 -------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/init.el b/init.el index 2bf5237..511b036 100644 --- a/init.el +++ b/init.el @@ -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)) diff --git a/site-lisp/db-org.el b/site-lisp/db-org.el index 3037471..07e57dc 100644 --- a/site-lisp/db-org.el +++ b/site-lisp/db-org.el @@ -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