emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Function for retrieving the link of an Org Mode buffer
@ 2021-08-14  1:14 Rodrigo Morales
  2021-08-14 12:48 ` John Kitchin
  0 siblings, 1 reply; 3+ messages in thread
From: Rodrigo Morales @ 2021-08-14  1:14 UTC (permalink / raw)
  To: emacs-orgmode


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.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Function for retrieving the link of an Org Mode buffer
  2021-08-14  1:14 Function for retrieving the link of an Org Mode buffer Rodrigo Morales
@ 2021-08-14 12:48 ` John Kitchin
  2021-08-16  5:18   ` [SOLVED] " Rodrigo Morales
  0 siblings, 1 reply; 3+ messages in thread
From: John Kitchin @ 2021-08-14 12:48 UTC (permalink / raw)
  To: Rodrigo Morales; +Cc: org-mode-email

[-- Attachment #1: Type: text/plain, Size: 2301 bytes --]

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
@johnkitchin
http://kitchingroup.cheme.cmu.edu



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.
>
>

[-- Attachment #2: Type: text/html, Size: 3326 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [SOLVED] Re: Function for retrieving the link of an Org Mode buffer
  2021-08-14 12:48 ` John Kitchin
@ 2021-08-16  5:18   ` Rodrigo Morales
  0 siblings, 0 replies; 3+ messages in thread
From: Rodrigo Morales @ 2021-08-16  5:18 UTC (permalink / raw)
  To: John Kitchin; +Cc: org-mode-email

[-- Attachment #1: Type: text/plain, Size: 432 bytes --]

On Sat, 14 Aug 2021 at 07:48, John Kitchin <jkitchin@andrew.cmu.edu> wrote:

> I would probably do it like this:
>

Thanks for the help! I just found this question at Emacs SE:
https://emacs.stackexchange.com/questions/38276 which asks this exact
question. I'm mentioning it just in case someone has this question
again.

P.S.: Sorry for creating this thread. I thought it was such a specific
question that nobody had asked before.

[-- Attachment #2: Type: text/html, Size: 837 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-08-16  5:20 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-14  1:14 Function for retrieving the link of an Org Mode buffer Rodrigo Morales
2021-08-14 12:48 ` John Kitchin
2021-08-16  5:18   ` [SOLVED] " Rodrigo Morales

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).