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.
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.
Sign up to join this communityI 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)))))
buffer-substring
with buffer-substring-no-properties
.
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.
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]]") "]]")
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 mostorg-mode
questions directly nowadays because everyone has a different version and the library is under constant development.