16

Is there someway to escape brackets ([ and ]) so that they can be included in the description of an Org mode link? The following link doesn't work, for example:

[[http://mathoverflow.net/questions/195203/automorphisms-of-ideals-of-mathbbct][Automorphisms of ideals of C[t]]]

I hoped using a backslash (\[) would work or that there would be an org-entity, but that doesn't seem to be the case.

Omar
  • 4,732
  • 1
  • 17
  • 32
  • 1
    The problem is links in Org are parsed using regular expressions, and this task would be equivalent to recursion, which cannot be parsed with regular expressions. If you wanted to only add a one level of recursion, that would be doable by patching `org-make-link-regexps`, but in general that can't be done with the current setup. – wvxvw Jan 29 '15 at 21:45
  • Escaping can be done. You could try to adapt the relevant regexp `org-bracket-link-regexp` to handle this, though `org-insert-link` still wants to replace the brackets with braces and it may have other ramifications. – politza Jan 29 '15 at 21:50
  • Oh, I just remembered that `\[` and `\]` are supposed to give displayed equations (like `$$`), not escaped brackets. – Omar Jan 29 '15 at 21:53
  • Enclosing the whole link in brackets used to work: [[[link][description]]] but it stopped working in recent versions. – GrB Apr 16 '22 at 12:03

3 Answers3

5

A working solution, not so pretty though, is to use the org mode Macros.

The below macros are replaced by the ASCII codes of the [ and ] when exporting to html or latex.

# Square Bracket Open [
#+MACRO: BO @@latex:\char91@@ @@html:[@@
# Square Bracket Close ]
#+MACRO: BC @@latex:\char93@@ @@html:]@@

[[http://emacs.stackexchange.com][{{{BO}}}Emacs SE{{{BC}}}]]

Reference

Kaushal Modi
  • 25,203
  • 3
  • 74
  • 179
  • Thanks, but I was hoping to convince org-mode to render them as brackets inside the buffer. Your solution does of course work for export. – Omar Jan 29 '15 at 21:57
  • 1
    Doesn't work for HTML export. – Alex Mar 21 '18 at 00:48
3

Below is the modified version of org-make-link-regexp which will allow one nesting level of square brackets inside description:

(defun org-make-link-regexps ()
  "Update the link regular expressions.
This should be called after the variable `org-link-types' has changed."
  (setq org-link-types-re
    (concat
     "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
    org-link-re-with-space
    (concat
     "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
     "\\([^" org-non-link-chars " ]"
     "[^" org-non-link-chars "]*"
     "[^" org-non-link-chars " ]\\)>?")
    org-link-re-with-space2
    (concat
     "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
     "\\([^" org-non-link-chars " ]"
     "[^\t\n\r]*"
     "[^" org-non-link-chars " ]\\)>?")
    org-link-re-with-space3
    (concat
     "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
     "\\([^" org-non-link-chars " ]"
     "[^\t\n\r]*\\)")
    org-angle-link-re
    (concat
     "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
     "\\([^" org-non-link-chars " ]"
     "[^" org-non-link-chars "]*"
     "\\)>")
    org-plain-link-re
    (concat
     "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
     (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
    ;;   "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
    org-bracket-link-regexp
    ;; "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
    "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^[]*?\\[[^]]*?\\][^]]*?\\|[^][]+\\)\\]\\)?\\]"
    org-bracket-link-analytic-regexp
    (concat
     "\\[\\["
     "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
     "\\([^]]+\\)"
     "\\]"
     "\\(\\[" "\\([^[]*?\\[[^]]*?\\][^]]*?\\|[^]]+\\)" "\\]\\)?"
     ;; "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
     "\\]")
    org-bracket-link-analytic-regexp++
    (concat
     "\\[\\["
     "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
     "\\([^]]+\\)"
     "\\]"
     "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
     "\\]")
    org-any-link-re
    (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
        org-angle-link-re "\\)\\|\\("
        org-plain-link-re "\\)")))

But as noted above, this doesn't solve the problem of editing links (Org will still want to replace brackets with braces.) This also can only handle one nesting level of one bracketed group.

wvxvw
  • 11,222
  • 2
  • 30
  • 55
2

URL encode seems OK. I just use it for an url with brackets:
[http://www.example.com/Array%5Bi%5D][Your description]
Left Square Bracket ("["):5B
Right Square Bracket ("]") 5D HTH

FOM
  • 21
  • 1