emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Function for retrieving the link of an Org Mode buffer


From: John Kitchin
Subject: Re: Function for retrieving the link of an Org Mode buffer
Date: Sat, 14 Aug 2021 08:48:11 -0400

I would probably do it like this:

(org-element-map (org-element-parse-buffer) 'link
  (lambda (lnk)
    (let ((lnkplist '()))
      (setq lnkplist (plist-put lnkplist :link (org-element-property :raw-link lnk)))
      (when (org-element-property :contents-begin lnk)
(setq lnkplist (plist-put lnkplist :desc (buffer-substring-no-properties
 (org-element-property :contents-begin lnk)
 (org-element-property :contents-end lnk)))))
      lnkplist)))



John

-----------------------------------
Professor John Kitchin (he/him/his)
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803


On Fri, Aug 13, 2021 at 9:16 PM Rodrigo Morales <moralesrodrigo1100@gmail.com> wrote:

I've written the following function for retrieving the links from a
given Org Mode buffer.

#+BEGIN_SRC elisp
(defun my/org-collect-links-in-buffer (buffer)
  "Collect all the links in the current buffer. If the link has a
description, then it is also collected.

Returns a list of PLISTS of the form:

((:link LINK)
 (:link LINK :desc DESC)
 (:link LINK))"
  (with-current-buffer buffer
    (save-excursion
      (beginning-of-buffer)
      (let (links)
        (while (re-search-forward org-any-link-re nil t)
          (catch 'done
            (let* ((element (org-element-context))
                   (type (org-element-type element)))
              (unless (eq type 'link)
                (throw 'done t))
              (let (obj
                    (link (org-element-property :raw-link element))
                    desc)
                (push link obj)
                (push :link obj)
                (when (and (org-element-property :contents-begin element)
                           (org-element-property :contents-end element))
                  (setq desc (buffer-substring-no-properties
                              (org-element-property :contents-begin element)
                              (org-element-property :contents-end element)))
                  (push desc obj)
                  (push :desc obj))
                (push obj links)))))
        links))))
#+END_SRC

I would really appreciate any feedback.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]