emacs-orgmode
[Top][All Lists]
Advanced

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

Function for retrieving the link of an Org Mode buffer


From: Rodrigo Morales
Subject: Function for retrieving the link of an Org Mode buffer
Date: Fri, 13 Aug 2021 20:14:34 -0500

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]