emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Rodrigo Morales <moralesrodrigo1100@gmail.com>
To: emacs-orgmode@gnu.org
Subject: How to call a code block by its name in the :prologue header argument?
Date: Thu, 11 Feb 2021 20:50:02 -0500	[thread overview]
Message-ID: <87ft22hx6d.fsf@gmail.com> (raw)


* 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	other threads:[~2021-02-12  1:54 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=87ft22hx6d.fsf@gmail.com \
    --to=moralesrodrigo1100@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /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).