reddit / emacs

elisp: get description of the link under cursor

whudwl · 2022-05-02 07:59
I'm trying to get the description and target of the link under cursor. getting the target is easy enough: (org-element-property :raw-link (org-element-context)) But I did not find an obvious way to get the description. What's the easiest way to do that?
whudwl · 2022-05-02 08:58
looks like this does it: (let ((link (org-element-context))) (buffer-substring (org-element-property :contents-begin link) (org-element-property :contents-end link)))
Phil-Hudson · 2022-05-02 11:30
Here's my recipe for making an Org link description a "thing" within the meaning of built-in package `thingatpt`: (with-eval-after-load 'org (put 'org-link-description 'end-op (lambda () (re-search-forward (rx "[[" (zero-or-more (not "]")) "][" (zero-or-more (not "]"))) nil :noerror))) (put 'org-link-description 'beginning-op (lambda () (if (search-backward "[" nil :noerror) (forward-char) (user-error "Not an Org link")))))
akirakom · 2022-05-02 15:19
You could use this: (save-match-data (when (thing-at-point-looking-at org-link-bracket-re) (match-string-no-properties 2)))
whudwl · 2022-05-05 07:23
this is very elegant, thank you.