emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [feature request] The :pre header argument
@ 2021-02-18 21:48 Rodrigo Morales
  2021-02-19 13:33 ` Rodrigo Morales
  0 siblings, 1 reply; 4+ messages in thread
From: Rodrigo Morales @ 2021-02-18 21:48 UTC (permalink / raw)
  To: emacs-orgmode


This message contains my thoughts on a feature request which I think
would be useful: The =:pre= header argument, it would be used to
execute a code block before the code block at point is executed. It
would be similar to the behavior of the =:post= header argument.

Here's a simple example that shows how =:pre= could be used

#+NAME: clean-path-experiments
#+begin_src dash
if [ ! -z "$my__experiments" ] && [ -d "$my__experiments" ]; then
  find ~/e -mindepth 1 -maxdepth 1 -exec rm -rf {} +
fi
#+end_src

#+NAME: tree-command
#+begin_src dash
tree -a --noreport
#+end_src

#+NAME: create-dir-for-minimal-reproducible-example
#+HEADER: :pre clean-path-experiments()
#+HEADER: :post tree-command()
#+begin_src python
import os

[os.makedirs(_) for _ in ["a/foo", "a/bar", "b"]]
[os.mknod(_) for _ in ["a/1.txt", "a/2.txt", "a/foo/b.txt", "a/bar/b.txt", "b/b.txt"]]
#+end_src

#+RESULTS: create-dir-for-minimal-reproducible-example
#+begin_example
.
├── a
│   ├── 1.txt
│   ├── 2.txt
│   ├── bar
│   │   └── b.txt
│   └── foo
│       └── b.txt
└── b
    └── b.txt
#+end_example

I think that such header argument would be useful because of two
main reasons:

* It would improve readability of code blocks by explicitly expressing dependency

By having a header argument for the sole purpose of expressing
dependencies between code blocks, the readability of header arguments
would be improved. Recall that it is currently possible to express
such dependency by calling a code block through a =:var <<name>>=
header argument but I think that =:var= header argument must only be
used for defining variables (be it from results obtained from
different code blocks or literals).

The first code block shown below show the differences between using
=:var= and =:pre= for the same scenario. =first-code-block= uses the
=:var= header argument while the =second-code-block= uses the =:pre=
header argument.

#+NAME: first-code-block
#+begin_src org
For our experimentation, let's start with an empty directory and let's
execute the first step.

,#+NAME: first-step
,#+HEADER: :results silent
,#+HEADER: :var e=clean-path-experiments
,#+begin_src dash
touch first-step.txt
,#+end_src

We know execute the second step.

,#+NAME: second-step
,#+HEADER: :results silent
,#+HEADER: :var e=first-step
,#+begin_src dash
touch second-step.txt
,#+end_src

Finally, we execute the third step.

,#+NAME: third-step
,#+HEADER: :results silent
,#+HEADER: :var e=second-step
,#+begin_src dash
touch third-step.txt
,#+end_src
#+end_src

#+NAME: second-code-block
#+begin_src org
For our experimentation, let's start with an empty directory and let's
execute the first step.

,#+NAME: first-step
,#+HEADER: :results silent
,#+HEADER: :pre clean-path-experiments()
,#+begin_src dash
touch first-step.txt
,#+end_src

We know execute the second step.

,#+NAME: second-step
,#+HEADER: :results silent
,#+HEADER: :pre first-step()
,#+begin_src dash
touch second-step.txt
,#+end_src

Finally, we execute the third step.

,#+NAME: third-step
,#+HEADER: :results silent
,#+HEADER: :pre second-step()
,#+begin_src dash
touch third-step.txt
,#+end_src
#+end_src

In my opinion, the second code block looks more readable than the
first one.

* it would add importance to the =:post= header argument

The =:post= header argument can be used in Org Mode 9.3 to execute a
given code block after the code block at point is executed; having a
header argument that does the opposite of the =:post= header argument
would give relevance to the =:post= header argument.

-- 
Greetings,
Rodrigo Morales.

IRC: rdrg109 (freenode)


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-04-05  4:45 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-18 21:48 [feature request] The :pre header argument Rodrigo Morales
2021-02-19 13:33 ` Rodrigo Morales
2021-02-20  1:50   ` Rodrigo Morales
2021-04-05  4:49     ` Ihor Radchenko

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