* Suggestion: org-link-match struct and functions
@ 2018-09-13 10:53 Adam Porter
0 siblings, 0 replies; only message in thread
From: Adam Porter @ 2018-09-13 10:53 UTC (permalink / raw)
To: emacs-orgmode
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
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2018-09-13 10:53 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-09-13 10:53 Suggestion: org-link-match struct and functions Adam Porter
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).