Skip to content

Commit e3a05d0

Browse files
Hugo-Heagrenyantar92
authored andcommitted
ol.el: add description format parameter to org-link-parameters
* ol.el (org-link-parameters): Add parameter `:insert-description', a string or a function. * (org-insert-link): If no description is provided (pre-existing or as an argument), next option is to use the `:insert-description' (if non-nil) parameter to generate one. * (org-link-make-description-function): Add documentation to describe behaviour of nil return value, like that of `:insert-description'. Default descriptions are predictable within a link type, but because link types are quite diverse, are NOT predictable across many types. A type-parameter is thus a good place to store information on the default description.
1 parent 5a1b050 commit e3a05d0

File tree

2 files changed

+65
-21
lines changed

2 files changed

+65
-21
lines changed

etc/ORG-NEWS

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,13 @@ purpose of the variable. The replacement variable
400400
accepts =listings= and =verbatim= in place of =t= and =nil= (which
401401
still work, but are no longer listed as valid options).
402402

403+
*** ~org-link-parameters~ has a new ~:insert-description~ parameter
404+
405+
The value of ~:insert-description~ is used as the initial input when
406+
prompting for a link description. It can be a string (used as-is) or
407+
a function (called with the same arguments as
408+
~org-make-link-description-function~ to return a string to use).
409+
403410
* Version 9.5
404411

405412
** Important announcements and breaking changes

lisp/ol.el

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,19 @@ link.
141141
Function that inserts a link with completion. The function
142142
takes one optional prefix argument.
143143
144+
`:insert-description'
145+
146+
String or function used as a default when prompting users for a
147+
link's description. A string is used as-is, a function is
148+
called with two arguments: the link location (a string such as
149+
\"~/foobar\", \"id:some-org-id\" or \"https://www.foo.com\")
150+
and the description generated by `org-insert-link'. It should
151+
return the description to use (this reflects the behaviour of
152+
`org-link-make-description-function'). If it returns nil, no
153+
default description is used, but no error is thrown (from the
154+
user's perspective, this is equivalent to a default description
155+
of \"\").
156+
144157
`:display'
145158
146159
Value for `invisible' text property on the hidden parts of the
@@ -200,7 +213,9 @@ You can interactively set the value of this variable by calling
200213
This function must take two parameters: the first one is the
201214
link, the second one is the description generated by
202215
`org-insert-link'. The function should return the description to
203-
use."
216+
use. If it returns nil, no default description is used, but no
217+
error is thrown (from the user’s perspective, this is equivalent
218+
to a default description of \"\")."
204219
:group 'org-link
205220
:type '(choice (const nil) (function))
206221
:safe #'null)
@@ -1802,11 +1817,14 @@ prefix negates `org-link-keep-stored-after-insertion'.
18021817
If the LINK-LOCATION parameter is non-nil, this value will be used as
18031818
the link location instead of reading one interactively.
18041819
1805-
If the DESCRIPTION parameter is non-nil, this value will be used as the
1806-
default description. Otherwise, if `org-link-make-description-function'
1807-
is non-nil, this function will be called with the link target, and the
1808-
result will be the default link description. When called non-interactively,
1809-
don't allow to edit the default description."
1820+
If the DESCRIPTION parameter is non-nil, this value will be used
1821+
as the default description. If not, and the chosen link type has
1822+
a non-nil `:insert-description' parameter, that is used to
1823+
generate a description as described in `org-link-parameters'
1824+
docstring. Otherwise, if `org-link-make-description-function' is
1825+
non-nil, this function will be called with the link target, and
1826+
the result will be the default link description. When called
1827+
non-interactively, don't allow to edit the default description."
18101828
(interactive "P")
18111829
(let* ((wcf (current-window-configuration))
18121830
(origbuf (current-buffer))
@@ -1816,7 +1834,10 @@ don't allow to edit the default description."
18161834
(desc region)
18171835
(link link-location)
18181836
(abbrevs org-link-abbrev-alist-local)
1819-
entry all-prefixes auto-desc)
1837+
(all-prefixes (append (mapcar #'car abbrevs)
1838+
(mapcar #'car org-link-abbrev-alist)
1839+
(org-link-types)))
1840+
entry auto-desc)
18201841
(cond
18211842
(link-location) ; specified by arg, just use it.
18221843
((org-in-regexp org-link-bracket-re 1)
@@ -1857,9 +1878,6 @@ Use TAB to complete link prefixes, then RET for type-specific completion support
18571878
(unless (pos-visible-in-window-p (point-max))
18581879
(org-fit-window-to-buffer))
18591880
(and (window-live-p cw) (select-window cw))))
1860-
(setq all-prefixes (append (mapcar #'car abbrevs)
1861-
(mapcar #'car org-link-abbrev-alist)
1862-
(org-link-types)))
18631881
(unwind-protect
18641882
;; Fake a link history, containing the stored links.
18651883
(let ((org-link--history
@@ -1956,17 +1974,36 @@ Use TAB to complete link prefixes, then RET for type-specific completion support
19561974
(setq desc path)))))
19571975

19581976
(unless auto-desc
1959-
(let ((initial-input
1960-
(cond
1961-
(description)
1962-
((not org-link-make-description-function) desc)
1963-
(t (condition-case nil
1964-
(funcall org-link-make-description-function link desc)
1965-
(error
1966-
(message "Can't get link description from %S"
1967-
(symbol-name org-link-make-description-function))
1968-
(sit-for 2)
1969-
nil))))))
1977+
(let* ((type
1978+
(cond
1979+
((string-match (rx-to-string `(: string-start (submatch (or ,@all-prefixes)) ":")) link)
1980+
(match-string 1 link))
1981+
((file-name-absolute-p link) "file")
1982+
((string-match "\\`\\.\\.?/" link) "file")))
1983+
(initial-input
1984+
(cond
1985+
(description)
1986+
(desc)
1987+
((org-link-get-parameter type :insert-description)
1988+
(let ((def (org-link-get-parameter type :insert-description)))
1989+
(condition-case nil
1990+
(cond
1991+
((stringp def) def)
1992+
((functionp def)
1993+
(funcall def link desc)))
1994+
(error
1995+
(message "Can't get link description from org link parameter `:insert-description': %S"
1996+
def)
1997+
(sit-for 2)
1998+
nil))))
1999+
(org-link-make-description-function
2000+
(condition-case nil
2001+
(funcall org-link-make-description-function link desc)
2002+
(error
2003+
(message "Can't get link description from %S"
2004+
org-link-make-description-function)
2005+
(sit-for 2)
2006+
nil))))))
19702007
(setq desc (if (called-interactively-p 'any)
19712008
(read-string "Description: " initial-input)
19722009
initial-input))))

0 commit comments

Comments
 (0)