emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] Re: what is the purpose of "This link has already been stored"?


From: Ihor Radchenko
Subject: [PATCH] Re: what is the purpose of "This link has already been stored"?
Date: Wed, 05 Jul 2023 10:19:41 +0000

Ihor Radchenko <yantar92@posteo.net> writes:

> What Samuel described is a sequence of M-x org-store-link followed by
> M-x org-insert-all-links. With such workflow, user can expect that the
> links are going to be stored in order. If we are instead shuffle the
> stored links, M-x org-insert-all-links may result in unexpected
> behaviour.
>
> Currently, when link to place is already stored, not error is thrown, a
> message is displayed, no link is stored, and the stored link list is not
> altered.

Attaching the patch.
>From a05e2cc235b10a7096538da4f6ae6d17c82a6b25 Mon Sep 17 00:00:00 2001
Message-ID: 
<a05e2cc235b10a7096538da4f6ae6d17c82a6b25.1688552284.git.yantar92@posteo.net>
From: Ihor Radchenko <yantar92@posteo.net>
Date: Wed, 5 Jul 2023 13:14:56 +0300
Subject: [PATCH] org-store-link: Move already stored link to front by default

* lisp/ol.el (org-link-store-existing): New customization controlling
how to deal with already stored links.
(org-store-link): Respect the new customization, allowing duplicates
to (1) be added anyway; (2) be ignored; (3) be moved to front of
`org-stored-links'.  The default is (3).
* etc/ORG-NEWS (~org-store-link~ now moves an already stored link to
front of the ~org-stored-links~): Document the breaking change.

Reported-by: Samuel Wales <samologist@gmail.com>
Link: 
https://list.orgmode.org/orgmode/CAJcAo8sjD3_FX5pFQ4git9wRDNM3bMqTgP-R5mM8zcf1B3mjPg@mail.gmail.com/
---
 etc/ORG-NEWS | 15 +++++++++++++++
 lisp/ol.el   | 36 +++++++++++++++++++++++++++++-------
 2 files changed, 44 insertions(+), 7 deletions(-)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index d04e92275..288d50842 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -13,6 +13,21 @@ Please send Org bug reports to mailto:emacs-orgmode@gnu.org.
 
 * Version 9.7 (not released yet)
 ** Important announcements and breaking changes
+*** ~org-store-link~ now moves an already stored link to front of the 
~org-stored-links~
+
+Previously, when the link to be stored were stored already,
+~org-store-link~ displayed a message and did nothing.
+
+Now, ~org-store-link~ moves the stored link to front of the list of
+stored links.  This way, the link will show up first in the completion
+and when inserting all the stored links with ~org-insert-all-links~.
+
+The new behavior is controlled by new customization ~org-link-store-existing~.
+
+Users can set ~org-link-store-existing~ to nil to revert previous
+defaults.  The value of =store-duplicate= will force duplicate links
+in ~org-stored-links~.  The default value is =move-to-front=.
+
 *** Major changes and additions to Org API
 **** New term: "syntax node"
 
diff --git a/lisp/ol.el b/lisp/ol.el
index 6dd7e0fa1..3a8ca5f39 100644
--- a/lisp/ol.el
+++ b/lisp/ol.el
@@ -509,6 +509,20 @@ (defcustom org-link-keep-stored-after-insertion nil
   :type 'boolean
   :safe #'booleanp)
 
+(defcustom org-link-store-existing 'move-to-front
+  "Variable controlling how to deal with already stored links.
+When nil, ignore store request for an already stored link.
+When symbol `move-to-front', move the stored link to the front of
+`org-stored-links'.
+When symbol `store-duplicate', add a duplicate in front."
+  :group 'org-link-store
+  :type '(choice
+          (const :tag "Do no store duplicate" nil)
+          (const :tag "Move stored duplicate to front" move-to-front)
+          (const :tag "Store duplicate" store-duplicate))
+  :safe #'symbolp
+  :package-version '(Org . "9.7"))
+
 ;;; Public variables
 
 (defconst org-target-regexp (let ((border "[^<>\n\r \t]"))
@@ -1749,16 +1763,24 @@ (defun org-store-link (arg &optional interactive?)
       ;; Store and return the link
       (if (not (and interactive? link))
          (or agenda-link (and link (org-link-make-string link desc)))
-       (if (member (list link desc) org-stored-links)
-           (message "This link has already been stored")
-         (push (list link desc) org-stored-links)
-         (message "Stored: %s" (or desc link))
+        (dotimes (_ (if custom-id 2 1)) ; Store 2 links when CUSTOM-ID is 
non-nil.
+          (pcase org-link-store-existing
+            ((or `store-duplicate
+                 (guard (not (member (list link desc) org-stored-links))))
+             (push (list link desc) org-stored-links)
+            (message "Stored: %s" (or desc link)))
+            ((or`nil (guard (equal (list link desc) (car org-stored-links))))
+             (message "This link has already been stored"))
+            (`move-to-front
+             (setq org-stored-links
+                   (delete (list link desc) org-stored-links))
+             (push (list link desc) org-stored-links)
+             (message "Link moved to front: %s" (or desc link))))
          (when custom-id
            (setq link (concat "file:"
                               (abbreviate-file-name
-                               (buffer-file-name (buffer-base-buffer)))
-                              "::#" custom-id))
-           (push (list link desc) org-stored-links)))
+                               (buffer-file-name (buffer-base-buffer)))
+                              "::#" custom-id))))
        (car org-stored-links)))))
 
 ;;;###autoload
-- 
2.41.0

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>

reply via email to

[Prev in Thread] Current Thread [Next in Thread]