emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Adam Porter <adam@alphapapa.net>
To: emacs-orgmode@gnu.org
Subject: Suggestion: org-link-match struct and functions
Date: Thu, 13 Sep 2018 05:53:38 -0500	[thread overview]
Message-ID: <87o9d1mzwt.fsf@alphapapa.net> (raw)

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

                 reply	other threads:[~2018-09-13 10:53 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87o9d1mzwt.fsf@alphapapa.net \
    --to=adam@alphapapa.net \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).