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