@@ -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
200213This function must take two parameters: the first one is the
201214link, 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'.
18021817If the LINK-LOCATION parameter is non-nil, this value will be used as
18031818the 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