diff --git a/lisp/ol.el b/lisp/ol.el index 82fc69769..fa0ade8b8 100644 --- a/lisp/ol.el +++ b/lisp/ol.el @@ -255,6 +255,16 @@ See the manual for examples." (`(,(pred stringp) . ,(pred stringp)) t) (_ nil)))) +(defcustom org-link-abbrev-default-description nil + "Fallback behaviour for abbreviated links with no description. +If this is nil, do not set a description; some export backends +will use the fully expanded link as fallback. If 'key-tag, then +use the abbreviated form KEY:TAG. If 'tag, then use TAG." + :group 'org-link + :type '(choice (const :tag "None" nil) + (const :tag "KEY:TAG" key-tag) + (const :tag "TAG" tag))) + (defgroup org-link-follow nil "Options concerning following links in Org mode." :tag "Org Follow Link" @@ -993,6 +1003,16 @@ and then used in capture templates." if store-func collect store-func)) +(defun org-link-abbrev-default-desc (link) + (save-match-data + (when (string-match "^\\([^:]*\\)::?\\(.+\\)$" link) + (pcase org-link-abbrev-default-description + ('nil '(nil nil)) + ('key-tag + (list (match-beginning 1) (match-end 2))) + ('tag + (list (match-beginning 2) (match-end 2))))))) + (defun org-link-expand-abbrev (link) "Replace link abbreviations in LINK string. Abbreviations are defined in `org-link-abbrev-alist'." diff --git a/lisp/org-element.el b/lisp/org-element.el index a693cb68d..a82fce52a 100644 --- a/lisp/org-element.el +++ b/lisp/org-element.el @@ -3146,11 +3146,19 @@ Assume point is at the beginning of the link." ;; (e.g., insert [[shell:ls%20*.org]] instead of ;; [[shell:ls *.org]], which defeats Org's focus on ;; simplicity. - (setq raw-link (org-link-expand-abbrev - (org-link-unescape - (replace-regexp-in-string - "[ \t]*\n[ \t]*" " " - (match-string-no-properties 1))))) + (let ((link-spec (match-string-no-properties 1)) + (link-beg (match-beginning 1))) + (setq raw-link (org-link-expand-abbrev (org-link-unescape + (replace-regexp-in-string + "[ \t]*\n[ \t]*" " " + link-spec)))) + ;; If there is no description, try to find a fallback. + (unless contents-begin + (pcase-let ((`(,default-beg ,default-end) + (org-link-abbrev-default-desc link-spec))) + (when default-beg + (setq contents-begin (+ link-beg default-beg) + contents-end (+ link-beg default-end)))))) ;; Determine TYPE of link and set PATH accordingly. According ;; to RFC 3986, remove whitespaces from URI in external links. ;; In internal ones, treat indentation as a single space.