Skip to content

Commit

Permalink
ol.el: Always prompt for description in `org-insert-link'
Browse files Browse the repository at this point in the history
* 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 1e34c5d 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
7f096ad 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 1e34c5d).  Recent commit 0432f4f 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 4fc2c8d 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.
  • Loading branch information
maxnikulin authored and yantar92 committed Sep 16, 2022
1 parent e3348cc commit 69b36be
Showing 1 changed file with 35 additions and 37 deletions.
72 changes: 35 additions & 37 deletions lisp/ol.el
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 69b36be

Please sign in to comment.