emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* How to call a code block by its name in the :prologue header argument?
@ 2021-02-12  1:50 Rodrigo Morales
  0 siblings, 0 replies; only message in thread
From: Rodrigo Morales @ 2021-02-12  1:50 UTC (permalink / raw)
  To: emacs-orgmode


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


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-02-12  1:54 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-12  1:50 How to call a code block by its name in the :prologue header argument? Rodrigo Morales

Code repositories for project(s) associated with this 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).