emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Default description for abbreviated links
@ 2020-07-16 17:35 Kévin Le Gouguec
  2020-09-06  7:58 ` Bastien
  0 siblings, 1 reply; 3+ messages in thread
From: Kévin Le Gouguec @ 2020-07-16 17:35 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1964 bytes --]

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.



[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: org-link-description.patch --]
[-- Type: text/x-diff, Size: 2562 bytes --]

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.

[-- Attachment #3: Type: text/plain, Size: 107 bytes --]



[1] Or just TAG.  If I look at ORG-NEWS for examples, I see a lot of
    [[doc:org-symbol][org-symbol]].

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: Default description for abbreviated links
  2020-07-16 17:35 Default description for abbreviated links Kévin Le Gouguec
@ 2020-09-06  7:58 ` Bastien
  2020-09-21 16:19   ` Kévin Le Gouguec
  0 siblings, 1 reply; 3+ messages in thread
From: Bastien @ 2020-09-06  7:58 UTC (permalink / raw)
  To: Kévin Le Gouguec; +Cc: emacs-orgmode

Hello Kévin,

Kévin Le Gouguec <kevin.legouguec@gmail.com> writes:

> 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.  

thanks for sharing your need and ideas.

I think we can allow

  #+LINK: bug [[https://debbugs.gnu.org/%s][bug:%s]]

to define an abbreviated link producing the output you want.

Same in org-link-abbrev-alist(-local):

  (("bug" . "[[https://debbugs.gnu.org/%s][bug:%s]]"))

What do you think?  I'd rather not add an option or modify the
structure of org-link-abbrev-alist(-local).

Best,

-- 
 Bastien


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Default description for abbreviated links
  2020-09-06  7:58 ` Bastien
@ 2020-09-21 16:19   ` Kévin Le Gouguec
  0 siblings, 0 replies; 3+ messages in thread
From: Kévin Le Gouguec @ 2020-09-21 16:19 UTC (permalink / raw)
  To: Bastien; +Cc: emacs-orgmode

Hello Bastien!  Thank you for following up on this.

Bastien <bzg@gnu.org> writes:

> Kévin Le Gouguec <kevin.legouguec@gmail.com> writes:
>
>> 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.  
>
> thanks for sharing your need and ideas.
>
> I think we can allow
>
>   #+LINK: bug [[https://debbugs.gnu.org/%s][bug:%s]]
>
> to define an abbreviated link producing the output you want.
>
> Same in org-link-abbrev-alist(-local):
>
>   (("bug" . "[[https://debbugs.gnu.org/%s][bug:%s]]"))
>
> What do you think?  I'd rather not add an option or modify the
> structure of org-link-abbrev-alist(-local).

That's an interesting idea!  It sounds fairly more powerful than what I
had in mind (only allowing KEY:TAG or TAG), but I'm sure someone could
find some use for free-form formatting.


I'm not sure how to implement it though.  I just came back from a hike
through ox-html.el, ox.el, org-element.el and ol.el; going backward,
here is how descriptions are given to export backends:

(1) Link-transcoding functions (e.g. org-html-link) are given two
    arguments: a link object, and its description.

(2) The description argument is set in org-export-data, from whatever
    org-element-contents returns.

(3) This (IIUC) is defined by what org-element--parse-objects `push'es
    on the link object, which is determined by a recursive call to
    org-element--parse-objects from the link's :contents-begin property
    to its :contents-end property.

(4) org-element-link-parser sets these properties to the bounds of
    org-link-bracket-re's optional second group, if it exists.

AFAICT steps (2) and (3) are not specific to links; they are generic
steps that are independent of what kind of elements they are processing.
I don't think this is where the "description fallback" feature should be
implemented, since it would add special-casing just for links.

I'm guessing I should keep aiming at step (4), like in my first patch;
my problem is that this step just defines the link's :contents-begin and
:contents-end properties.  My first patch works because I'm only
allowing KEY:TAG and TAG, so I can re-use positions inside
org-link-bracket-re's first group.

If we are to create a completely new format string, how can we pass it
to the backends?


FWIW, I would still favor only allowing KEY:TAG and TAG, since that
covers all use-cases I've thought of so far (the one you quoted, and the
"doc:" links in ORG-NEWS).

I know you said you'd rather not modifying the structure of
org-link-abbrev-alist, but maybe adding an optional third item would be
the least intrusive way to go?  I.e. going from:

    ("linkkey" . REPLACE)

To:

    ("linkkey" REPLACE [DESCRIPTION-FALLBACK])

Where DESCRIPTION-FALLBACK could be nil (the default), 'key-tag or 'tag.


Thank you for your time.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-09-21 16:19 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-07-16 17:35 Default description for abbreviated links Kévin Le Gouguec
2020-09-06  7:58 ` Bastien
2020-09-21 16:19   ` Kévin Le Gouguec

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).