emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] ol.el: Always prompt for description in `org-insert-link'
@ 2022-09-14 16:11 Max Nikulin
  2022-09-16  3:55 ` Ihor Radchenko
  0 siblings, 1 reply; 4+ messages in thread
From: Max Nikulin @ 2022-09-14 16:11 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1136 bytes --]

Hi,

The `org-insert-link' function has the `auto-desc' variable to suppress 
user prompt for link description. It worked accordingly its intention 
(as I understand it) a couple of months a decade ago, after that the 
logic was broken. Actually the behavior is rather strange: users may 
edit description only when it is not identical to link path (target). I 
am attaching a patch that removes this variable with hope that always 
prompting the user for description is more consistent behavior.

Actually during last week completion stored link by its description has 
been restored, storing link has been changed to prefer nil as 
description to the path when nothing meaningful may be added. So 
behavior have become more close to original one.

I have an idea how to fix the code to bypass the description prompt if 
path is completed by description discarding identical path and 
description, but I prefer to drop the confusing variable and not depend 
on the means how the link was selected from the list of stored items.

Have I missed something and unconditional prompt to edit link 
description may be undesired in some cases?

[-- Attachment #2: 0001-ol.el-Always-prompt-for-description-in-org-insert-li.patch --]
[-- Type: text/x-patch, Size: 5963 bytes --]

From b4e5195da4f084a9efd9697db556168d9907a17e Mon Sep 17 00:00:00 2001
From: Max Nikulin <manikulin@gmail.com>
Date: Wed, 14 Sep 2022 22:43:51 +0700
Subject: [PATCH 1/2] ol.el: Always prompt for description in `org-insert-link'

* lisp/ol.el (org-insert-link): Do not bypass code trying to generated
description and prompt user when link path and description are
identical.  Make behavior of description prompt more consistent.

Remove confusing `auto-desc' local variable.  Originally the variable
was added with implementation of completion of stored link target by the
description in the commit 1e34c5d34 Bastien Guerry, "org.el: Fontify
links to current buffer when inserting a link",
2012-08-03 14:08:20 +0200.  The feature was broken soon by the commit
7f096ad37 Tony Day, "org-insert-link: Use ido when inserting links",
2012-10-12 14:39:53 +1100.  Last decade users were not asked to edit
description in the case of the same link target and description
(a remained side effect of 1e34c5d34).  Recent commit 0432f4fe6 Max
Nikulin, "ol.el: Restore complete by description for insert link",
2022-09-10 17:23:13 +0700 restored completion by description.
Due to the commit 4fc2c8dd8 Ihor Radchenko, "org-store-link: Default to
empty description for target/custom-id links", 2022-08-10 13:25:26 +0800
description identical to link path became a more rare case.

An alternative would be fixing condition to allow users to edit
description when it is the same as the path, but use stored description
without additional interaction when the link is chosen by description
completion.  Despite it was likely the original intention, always asking
the user to confirm or edit description may be more consistent behavior.
---
 lisp/ol.el | 72 ++++++++++++++++++++++++++----------------------------
 1 file changed, 35 insertions(+), 37 deletions(-)

diff --git a/lisp/ol.el b/lisp/ol.el
index f3f6e04ef..8f700397e 100644
--- a/lisp/ol.el
+++ b/lisp/ol.el
@@ -1822,7 +1822,7 @@ non-interactively, don't allow to edit the default description."
 	 (all-prefixes (append (mapcar #'car abbrevs)
 			       (mapcar #'car org-link-abbrev-alist)
 			       (org-link-types)))
-         entry auto-desc)
+         entry)
     (cond
      (link-location)		      ; specified by arg, just use it.
      ((org-in-regexp org-link-bracket-re 1)
@@ -1885,8 +1885,7 @@ Use TAB to complete link prefixes, then RET for type-specific completion support
 	    (unless (org-string-nw-p link) (user-error "No link selected"))
 	    (dolist (l org-stored-links)
 	      (when (equal link (cadr l))
-		(setq link (car l))
-		(setq auto-desc t)))
+		(setq link (car l))))
 	    (when (or (member link all-prefixes)
 		      (and (equal ":" (substring link -1))
 			   (member (substring link 0 -1) all-prefixes)
@@ -1963,41 +1962,40 @@ Use TAB to complete link prefixes, then RET for type-specific completion support
 	  (when (equal desc origpath)
 	    (setq desc path)))))
 
-    (unless auto-desc
-      (let* ((type
-              (cond
-               ((and all-prefixes
-                     (string-match (rx-to-string `(: string-start (submatch (or ,@all-prefixes)) ":")) link))
-                (match-string 1 link))
-               ((file-name-absolute-p link) "file")
-               ((string-match "\\`\\.\\.?/" link) "file")))
-             (initial-input
-	      (cond
-	       (description)
-               (desc)
-               ((org-link-get-parameter type :insert-description)
-                (let ((def (org-link-get-parameter type :insert-description)))
-                  (condition-case nil
-                      (cond
-                       ((stringp def) def)
-                       ((functionp def)
-                        (funcall def link desc)))
-                    (error
-                     (message "Can't get link description from org link parameter `:insert-description': %S"
-			      def)
-		     (sit-for 2)
-                     nil))))
-	       (org-link-make-description-function
+    (let* ((type
+            (cond
+             ((and all-prefixes
+                   (string-match (rx-to-string `(: string-start (submatch (or ,@all-prefixes)) ":")) link))
+              (match-string 1 link))
+             ((file-name-absolute-p link) "file")
+             ((string-match "\\`\\.\\.?/" link) "file")))
+           (initial-input
+            (cond
+             (description)
+             (desc)
+             ((org-link-get-parameter type :insert-description)
+              (let ((def (org-link-get-parameter type :insert-description)))
                 (condition-case nil
-		    (funcall org-link-make-description-function link desc)
-		  (error
-		   (message "Can't get link description from %S"
-		            org-link-make-description-function)
-		   (sit-for 2)
-		   nil))))))
-	(setq desc (if (called-interactively-p 'any)
-		       (read-string "Description: " initial-input)
-		     initial-input))))
+                    (cond
+                     ((stringp def) def)
+                     ((functionp def)
+                      (funcall def link desc)))
+                  (error
+                   (message "Can't get link description from org link parameter `:insert-description': %S"
+                            def)
+                   (sit-for 2)
+                   nil))))
+             (org-link-make-description-function
+              (condition-case nil
+                  (funcall org-link-make-description-function link desc)
+                (error
+                 (message "Can't get link description from %S"
+                          org-link-make-description-function)
+                 (sit-for 2)
+                 nil))))))
+      (setq desc (if (called-interactively-p 'any)
+                     (read-string "Description: " initial-input)
+                   initial-input)))
 
     (unless (org-string-nw-p desc) (setq desc nil))
     (when remove (apply #'delete-region remove))
-- 
2.25.1


[-- Attachment #3: 0002-ol.el-Mention-that-org-insert-link-may-edit-existing.patch --]
[-- Type: text/x-patch, Size: 1036 bytes --]

From e5d1cb2656bbd3f5026c4ab5661931b40d14c767 Mon Sep 17 00:00:00 2001
From: Max Nikulin <manikulin@gmail.com>
Date: Wed, 14 Sep 2022 22:55:28 +0700
Subject: [PATCH 2/2] ol.el: Mention that `org-insert-link' may edit existing
 link

* lisp/ol.el (org-insert-link): Add completion by description and edit
link features to the docstring.
---
 lisp/ol.el | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/lisp/ol.el b/lisp/ol.el
index 8f700397e..39d644025 100644
--- a/lisp/ol.el
+++ b/lisp/ol.el
@@ -1777,6 +1777,9 @@ The history can be used to select a link previously stored with
 press `RET' at the prompt), the link defaults to the most recently
 stored link.  As `SPC' triggers completion in the minibuffer, you need to
 use `M-SPC' or `C-q SPC' to force the insertion of a space character.
+Completion candidates include link descriptions.
+
+If there is a link under cursor then edit it.
 
 You will also be prompted for a description, and if one is given, it will
 be displayed in the buffer instead of the link.
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-09-17 12:40 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-09-14 16:11 [PATCH] ol.el: Always prompt for description in `org-insert-link' Max Nikulin
2022-09-16  3:55 ` Ihor Radchenko
2022-09-17 12:22   ` Bastien Guerry
2022-09-17 12:40     ` Ihor Radchenko

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).