7

Is there a function in elisp that will return the description of an internal org-mode link?

I would want this to work for any type of supported link:

[[link][description]]

[[description]]

Etc.

1
  • 1
    org-open-at-point has some goodies (inside) that you may wish to look at -- i.e., M-x find-function ... I tend to avoid most org-mode questions directly nowadays because everyone has a different version and the library is under constant development.
    – lawlist
    Jan 23, 2018 at 2:31

3 Answers 3

8

I guess you want something like:

(defun get-description-at-point ()
  (interactive)
  (let ((link (org-element-context)))
    (message "%s" (buffer-substring (org-element-property :contents-begin link)
                                    (org-element-property :contents-end link)))))
1
  • 1
    The command behaves differently depending on whether it is invoked interactively or programmatically. I managed to make its behavior consistent by replacing buffer-substring with buffer-substring-no-properties.
    – Pablo
    Oct 14, 2021 at 22:27
2

You can also use regex to extract the link description:

(replace-regexp-in-string "\\(\\[\\[.*\\]\\[\\)\\(.*\\)\\]\\]" "[[\\2]]" "[[link][description]]")

will return [[description]]. If you omit the opening and closing braces in the second argument (that is, \\2), you will get description.

Note: I'm just beginning to use regex, so perhaps there are better solutions to set up the regex.

1
  • replace-regex-in-string -> replace-regexp-in-string
    – Hatshepsut
    Jul 8, 2018 at 22:09
2

There exists (org-link-display-format s), which replaces all links in s with there description.

For your case you than just need to add the brackets where you want them:

(concat "[[" (org-link-display-format "[[link][description]]") "]]")
1
  • Thank you! This seems like the sanest answer.
    – Beetle B.
    Mar 31 at 23:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.