On 06/09/2022 21:34, Max Nikulin wrote:

Does anyone have an idea why it was necessary to drop completion of stored links based on their description for the sake of ido?

I have no idea what is the proper way to enable ido for `org-insert-link'. Functions and variables specific to ido were removed from Org.
(ido-everywhere) and (ido-mode) are not enough.

I tried

    (setq-local completing-read-function #'ido-completing-read)

and the command broke completing read completely.

    (add-function :override completing-read-function #'ido-completing-read)

inspired by `ido-everywhere' code broke M-x, but it enabled ido for `org-insert-link'.

I believe that descriptions as completion options were removed because ido signals an error when nil is passed inside completion list. I consider it as a bug in ido (at least in Emacs-27), but even when `completing-read-default' is used, it causes appearance of undesired "nil" option. No description is a frequent case for links.

So I am attaching a patch to restore completion of link targets by their description, nil descriptions are filtered out.

The change is caused by the auto-desc local variable in `org-insert-link', its usage is rather strange and confusing currently. Despite with this patch descriptions are restored, I believe that logic related to auto-desc should be removed, anyway it was broken for 10 years. I am unsure in which thread the next change should be discussed.

P.S. My question is related to the following threads:
- Carlos Pita. Adding target and custom id links doesn't ask for description. Tue, 2 Aug 2022 14:44:58 -0300. https://list.orgmode.org/d99a712c-18d1-4a4f-8093-35a0bfb46...@gmail.com - Max Nikulin. Re: Bug: org-store-link uses CUSTOM_ID instead of target point. Sat, 6 Nov 2021 19:51:29 +0700. https://list.orgmode.org/e2c807a7-1924-6f08-9e63-4f70aee9d...@gmail.com
From 92e36ec13e84d2af1b367e4a3cac34288d1e8c27 Mon Sep 17 00:00:00 2001
From: Max Nikulin <maniku...@gmail.com>
Date: Sat, 10 Sep 2022 17:23:13 +0700
Subject: [PATCH] ol.el: Restore complete by description for insert link

* lisp/ol.el (org-insert-link): Allow completion of link target by its
description.  Almost certainly the feature was removed unintentionally.

Link descriptions were added to completion options in
the commit 1e34c5d34 Bastien Guerry,  "org.el: Fontify links to current
buffer when inserting a link", 2012-08-03 14:08:20 +0200
in response to
https://list.orgmode.org/877gw6ocva....@okhotsk19.lowtem.hokudai.ac.jp/T/#u
Yagnesh Raghava Yakkala, "#+LABEL and CUSTOM_ID with reftex",
Mon, 21 May 2012 04:45:29 +0900

List of description was removed from completion options
likely because `ido-completing-read' signals an error in the case of nil
variant (that is not uncommon for links with no description), see the
commit 7f096ad37 Tony Day, "org-insert-link: Use ido when inserting
links", 2012-10-12 14:39:53 +1100 and the discussion of the patch
- https://list.orgmode.org/04d0e787-a8a1-4246-8dd2-d607e38d6...@gmail.com/T/#u
  tony day.  [PATCH] * org-insert-link: use ido when inserting links.
  Fri, 12 Oct 2012 14:58:29 +1100
- https://list.orgmode.org/5ce03302-7c87-44be-b4af-a6a92c96c...@gmail.com/T/#u
  tony day.  [PATCH] org-insert-link: allow ido usage when inserting
  links.  Fri, 14 Sep 2012 19:21:50 +1000
- https://list.orgmode.org/0cada13b-8a22-4f34-91b1-2232997c1...@gmail.com/T/#u
  tony day.  [PATCH] org-insert-link: allow ido usage when inserting
  links. Fri, 12 Oct 2012 14:56:10 +1100
- https://list.orgmode.org/97f9790d-3c7f-490b-be9b-1a652bb9f...@gmail.com/
  tony day.  PATCH: using ido when inserting links.
  Fri, 14 Sep 2012 18:58:43 +1000

Since auto-desc variable added by first commit was not removed
by second one, I assume that disabling the feature was a side effect
rather than the purpose.
---
 lisp/ol.el | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/lisp/ol.el b/lisp/ol.el
index 7e5398b22..20b085682 100644
--- a/lisp/ol.el
+++ b/lisp/ol.el
@@ -1878,7 +1878,12 @@ Use TAB to complete link prefixes, then RET for type-specific completion support
 		   "Link: "
 		   (append
 		    (mapcar (lambda (x) (concat x ":")) all-prefixes)
-		    (mapcar #'car org-stored-links))
+		    (mapcar #'car org-stored-links)
+                    ;; Allow description completion.  Avoid "nil" option
+                    ;; in the case of `completing-read-default' and
+                    ;; an error in `ido-completing-read' when some links
+                    ;; have no description.
+                    (delq nil (mapcar 'cadr org-stored-links)))
 		   nil nil nil
 		   'org-link--history
 		   (caar org-stored-links)))
-- 
2.25.1

Reply via email to