emacs-orgmode
[Top][All Lists]
Advanced

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

How to call a code block by its name in the :prologue header argument?


From: Rodrigo Morales
Subject: How to call a code block by its name in the :prologue header argument?
Date: Thu, 11 Feb 2021 20:50:02 -0500
User-agent: mu4e 1.4.14; emacs 27.1

* The problem

Let's suppose we have this simple code block

#+NAME: my-named-code-block
#+begin_src bash
echo foo-1
echo foo-2
#+end_src

which contain some commands that we want to execute before the
commands that are presented below. Therefore, we use the =:prologue=
header argument

#+begin_src bash :noweb yes :prologue "<<my-named-code-block>>"
echo a
#+end_src

However, when evaluating the code block, the buffer =*Org-Babel Error
Output*= is opened with the following content. Apparently, this
happens because the noweb reference added in the prologue is not
expanded. Therefore, =bash= executes =<<foo>>= which results in the
error shown below.

#+begin_example
bash: line 2: warning: here-document at line 1 delimited by end-of-file (wanted 
`my-named-code-block')
bash: line 2: syntax error near unexpected token `newline'
bash: line 2: `<<my-named-code-block>>'
#+end_example

* The question

How can I add the content of a code block as the =:prologue= of
another code block?

* Additional information

I don't want to add =<<foo>>= inside the code block (see below for
example) since that would imply that the content of the code block is
explicitly changed. Note that this doesn't happen when using
=:prologue= because it can be set through different ways that don't
imply changing the content of the code block where the commands are
intended to be executed.

#+begin_src bash
<<foo>>
echo a
#+end_src

* A possible solution

I was thinking in the following solution: Find/Create a function which
gets the content of a code block as an string given its name so that
it could be called as it follows

#+begin_src bash :prologue (get-code-block-as-string "my-named-code-block")
echo a
#+end_src

Recall that this would work because =:prologue= accept strings (see
below).

#+begin_src bash :prologue "echo foo\necho bar"
echo a
#+end_src

#+RESULTS:
#+begin_example
foo
bar
a
#+end_example

* Relevant information

Found the following relevant sources of information

[1] A user had asked this in a question in Stack Overflow but he
didn't get any significant response.

[2] Some user created a thread in the emacs-orgmode mailing list
asking why noweb expansion occurs before prologue expansion and
presents an example similar to mine. The thread didn't get any
response.

[3, 4] In a thread in the emacs-orgmode mailing list, this was asked
before but the only solution provided was to insert a noweb reference
inside the code block.

[1] 
https://stackoverflow.com/questions/50555643/how-to-add-one-code-block-as-prologue-to-another-code-block-for-noweb-style-exp
[2] https://lists.gnu.org/archive/html/emacs-orgmode/2019-08/msg00244.html
[3] https://lists.gnu.org/archive/html/emacs-orgmode/2020-08/msg00116.html
[4] https://lists.gnu.org/archive/html/emacs-orgmode/2020-08/msg00118.html


-- 
Greetings,
Rodrigo Morales.

IRC: rdrg109 (freenode)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]