Hello Org, I like #+LINK keywords because they make documents self-sufficient: anyone opening my document can follow these links or export the buffer; they do not need to run some Elisp to add to org-link-parameters.
One thing I don't know how to customize, however, is how these links are exported when they have no description. Let's say I define this abbreviation: #+LINK: bug https://debbugs.gnu.org/ If I jot down references to [[bug:12345]] in my document, these will be exported as: <a href="https://debbugs.gnu.org/12345">https://debbugs.gnu.org/12345</a> Whereas I'd prefer: <a href="https://debbugs.gnu.org/12345">bug:12345</a> Looking at org-element-link-parser, I see that this is because the :contents-begin and :contents-end properties are nil, since they correspond to an unmatched optional group in org-link-bracket-re. I could probably customize org-link-parameters, but then my document would not be self-sufficient anymore. Besides, depending on the document I might use the same abbreviation for different URLs. Would it make sense to add a way for abbreviated links with no description to fallback to LINKKEY:TAG[1] instead of the full URL? If so, what would be the best way to go about it? (1) A single variable (e.g. org-link-abbrev-default-description), default nil, which a user could set to 'key-tag or just 'tag. (2) A third entry in org-link-abbrev-alist(-local), default nil, which a user could set to 'key-tag or just 'tag. (3) Something else (e.g. a new alist). I've attached a very crude patch for (1): now if I stick this footer in my Org files: #+LINK: bug https://debbugs.gnu.org/ # Local variables: # org-link-abbrev-default-description: key-tag # end: Then [[bug:12345]] is exported as <a href="https://debbugs.gnu.org/12345">bug:12345</a>. WDYT? If the idea is sound, I will clean up the patch, clarify docstrings, add :safe predicates and unit tests, and re-submit. Thank you for your time.
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.
[1] Or just TAG. If I look at ORG-NEWS for examples, I see a lot of [[doc:org-symbol][org-symbol]].