Hi,
When writing Org-related code, I often need to get parts of a link. I
usually have to lookup the proper regexp and manually do string-match,
then match-string, etc.
The following code makes this process much simpler. Would it be helpful
if this were added to Org? I think it would save a lot of other
packages from having to reinvent this wheel.
An example of using it:
#+BEGIN_SRC elisp
(when-let* ((link (org-match-link (org-get-heading t t))))
(list :url (concat (org-link-protocol link) ":"
(org-link-path link))
:desc (org-link-description link)))
#+END_SRC
Thanks,
Adam
#+BEGIN_SRC elisp
(cl-defstruct org-link
protocol path description)
(defun org-link-match (&optional s)
"Return an `org-link' struct if an Org link is matched in string S or at
point.
Matches with `org-bracket-link-analytic-regexp'."
;; NOTE: HTTP paths will start with two slashes.
(cond (s (when (string-match org-bracket-link-analytic-regexp s)
(make-org-link :protocol (match-string-no-properties 2 s)
:path (match-string-no-properties 3 s)
:description (match-string-no-properties 5 s))))
(t (when (looking-at org-bracket-link-analytic-regexp)
(make-org-link :protocol (match-string-no-properties 2)
:path (match-string-no-properties 3)
:description (match-string-no-properties 5))))))
#+END_SRC