From mboxrd@z Thu Jan 1 00:00:00 1970 From: Adam Porter Subject: Suggestion: org-link-match struct and functions Date: Thu, 13 Sep 2018 05:53:38 -0500 Message-ID: <87o9d1mzwt.fsf@alphapapa.net> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:45429) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g0PFx-0001SG-T7 for emacs-orgmode@gnu.org; Thu, 13 Sep 2018 06:53:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g0PFu-0005yr-OR for emacs-orgmode@gnu.org; Thu, 13 Sep 2018 06:53:53 -0400 Received: from [195.159.176.226] (port=60324 helo=blaine.gmane.org) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1g0PFu-0005yD-HL for emacs-orgmode@gnu.org; Thu, 13 Sep 2018 06:53:50 -0400 Received: from list by blaine.gmane.org with local (Exim 4.84_2) (envelope-from ) id 1g0PDk-0008Gh-OW for emacs-orgmode@gnu.org; Thu, 13 Sep 2018 12:51:36 +0200 List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: emacs-orgmode@gnu.org 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