* How to get the content of a code block by its name?
@ 2021-02-12 1:54 Rodrigo Morales
2021-02-12 4:50 ` Kyle Meyer
0 siblings, 1 reply; 3+ messages in thread
From: Rodrigo Morales @ 2021-02-12 1:54 UTC (permalink / raw)
To: emacs-orgmode
* The question
Let's suppose I have this simple code block in a buffer (let's say
=A=)
#+NAME: five-numbers
#+begin_src bash
echo foo
seq 1 5
echo bar
#+end_src
How can I get the content of the code block with name =five-numbers=
as an string from any point in buffer =A=?
* Additional information
I'm asking this because I would like to have a function which would
help me insert a code block in the =:prologue= header argument of
another code block.
#+begin_src bash :prologue (get-code-block-as-string "five-numbers")
echo a
#+end_src
--
Greetings,
Rodrigo Morales.
IRC: rdrg109 (freenode)
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: How to get the content of a code block by its name?
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
0 siblings, 1 reply; 3+ messages in thread
From: Kyle Meyer @ 2021-02-12 4:50 UTC (permalink / raw)
To: Rodrigo Morales; +Cc: emacs-orgmode
Rodrigo Morales writes:
> * The question
>
> Let's suppose I have this simple code block in a buffer (let's say
> =A=)
>
> #+NAME: five-numbers
> #+begin_src bash
> echo foo
> seq 1 5
> echo bar
> #+end_src
>
> How can I get the content of the code block with name =five-numbers=
> as an string from any point in buffer =A=?
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: How to get the content of a code block by its name?
2021-02-12 4:50 ` Kyle Meyer
@ 2021-02-12 20:56 ` Rodrigo Morales
0 siblings, 0 replies; 3+ messages in thread
From: Rodrigo Morales @ 2021-02-12 20:56 UTC (permalink / raw)
To: Kyle Meyer; +Cc: emacs-orgmode
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)
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2021-02-12 21:01 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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).