From: Rodrigo Morales <moralesrodrigo1100@gmail.com>
To: Kyle Meyer <kyle@kyleam.com>
Cc: emacs-orgmode@gnu.org
Subject: Re: How to get the content of a code block by its name?
Date: Fri, 12 Feb 2021 15:56:06 -0500 [thread overview]
Message-ID: <87o8gp2eft.fsf@gmail.com> (raw)
In-Reply-To: <87h7mh99eb.fsf@kyleam.com>
Kyle Meyer <kyle@kyleam.com> writes:
> This came up somewhat recently, and there were a couple of suggestions:
> https://orgmode.org/list/CA+pajW+jViTPE2cwTZ=tHVZyHjrT000-BB6JK2ntE4P9UFBmpQ@mail.gmail.com/T/#u
Thanks for the information! That was really helpful. I retrieved some
information from that post and wrote the following function.I would
really appreciate if someone could give me some feedback on this
function.
#+begin_src elisp
(defun org-babel-get-block-as-string (name)
"Get the content of a code block with name NAME as an
string. This function also considers the code blocks stored in
`org-babel-library-of-babel'."
(interactive)
(catch 'found
;; Let's first search the code block in the current buffer.
(let ((position (org-babel-find-named-block name)) content)
(if position
(progn
(setq content
(save-excursion
(goto-char position)
;; This function uses `org-babel-expand-src-block'
;; instead of `org-edit-src-code' so that the
;; value of the header arguments :prologue and the
;; :epilogue are included as the content of the
;; code block.
(org-babel-expand-src-block)))
(throw 'found content))))
;; If the code block doesn't exist in the current buffer, then
;; let's search in `org-babel-library-of-babel'.
(let ((library-block-data (alist-get (intern name) org-babel-library-of-babel)) content)
(if library-block-data
(progn
(setq content (nth 1 library-block-data))
(throw 'found content))))
; If the code block is not found, then an error is shown. Throwing
; an error is important since this makes code blocks which uses
; this function as the :epilogue and :prologue don't be evaluated
; in the scenario that a code block is not found.
(error (format "The code block with name \"%s\" was not found" name))))
#+end_src
* Minimal example
Below, there is an example that shows how to use this function.
We can use =printf= as it follows
#+NAME: test-printf
#+begin_src bash
printf "%s\n" "printf-foo"
printf "%s\n" "printf-bar"
#+end_src
#+RESULTS: test-printf
#+begin_example
printf-foo
printf-bar
#+end_example
We can use =echo= as it follows
#+NAME: test-echo
#+begin_src bash
echo echo-foo
echo echo-bar
#+end_src
#+RESULTS: test-echo
#+begin_example
echo-foo
echo-bar
#+end_example
We can use both of them as it follows
#+HEADER: :prologue (org-babel-get-block-as-string "test-printf")
#+HEADER: :epilogue (org-babel-get-block-as-string "test-echo")
#+begin_src bash
echo a
#+end_src
#+RESULTS:
#+begin_example
printf-foo
printf-bar
a
echo-foo
echo-bar
#+end_example
--
Greetings,
Rodrigo Morales.
IRC: rdrg109 (freenode)
prev parent reply other threads:[~2021-02-12 21:01 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-02-12 1:54 How to get the content of a code block by its name? Rodrigo Morales
2021-02-12 4:50 ` Kyle Meyer
2021-02-12 20:56 ` Rodrigo Morales [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.orgmode.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87o8gp2eft.fsf@gmail.com \
--to=moralesrodrigo1100@gmail.com \
--cc=emacs-orgmode@gnu.org \
--cc=kyle@kyleam.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).