#+NAME: fun1
#+BEGIN_SRC elisp :var var1="val1"
(format "%s" var1)

#+NAME: fun2
#+HEADER: :var var2="val2"
#+CALL: fun1(var2)

#+NAME: fun3
#+BEGIN_SRC elisp :var var2="val2" var3=fun1(var2)
(format "%s" var3)

Both fun2 and fun3 complain Reference 'var2' not found in this buffer

Update: I need to simplify this form

#+NAME: funA
#+BEGIN_SRC elisp :var varA="valA"
(format "%s" varA)

#+NAME: funA2E
#+CALL: funA[:var varF="valF"](funB(funC(funD(funE(varF)))))

#+CALL: funA2E("foo")

#+CALL: funA2E("bar")

#+CALL: funA2E("baz")

You need to put the literal value of the argument inline in the CALL. For example:

#+NAME: fun1
#+BEGIN_SRC elisp :var var1="val1"
(format "%s" var1)

#+RESULTS: fun1
: val1

#+NAME: fun2
#+CALL: fun1(var1="val2")

#+RESULTS: fun2
: val2

#+NAME: fun3
#+CALL: fun1("val3")

#+RESULTS: fun3
: val3

Your fun2 fails because the HEADER line cannot be applied to CALL. Instead, you have to use a CALL: <name>[<inside header arguments>](<arguments>)[<end header arguments>] syntax (see org manual), as in this example:

#+NAME: fun4
#+CALL: fun1[:var var1="val4"]()

#+RESULTS: fun4
: val4

Your fun3 fails because you can't refer back to values of previous variables defined in the same :var header argument. The complete syntax for :var is described in the relevant section of the org manual

Update: Responding to your edit and clarification, I do not think this is possible using the CALL syntax, which is relatively inflexible. However, with lisp functions we can construct a string representation of the nested source block call, and then use org-babel-ref-resolve to do the equivalent of the CALL line.

For example, I define a source block funX to be the basic building block (this function can be any in any language supported by babel).

#+NAME: funX
#+BEGIN_SRC elisp :var a="A" s="X"
(format "%s.%s.%s" a s a)

Then, I have a lisp source block with a single parameter s, which constructs a deeply nested call to the funX block, with s inserted as a parameter to the innermost function:

#+name: funABCDE
#+BEGIN_SRC emacs-lisp :var s="\" ¡Hola! \""
  (defun funX (a s) (format "funX(\"%s\", %s)" a s))
  (defun nested-funX (s)
    (funX "a" (funX "b" (funX "c" (funX "d" (funX "e" s))))))
  (org-babel-ref-resolve (nested-funX s))

: a.b.c.d.e. ¡Hola! .e.d.c.b.a

We can then re-use that source block in a CALL line with a different value of s:

#+CALL: funABCDE("\" Nǐ hǎo! \"")

: a.b.c.d.e. Nǐ hǎo! .e.d.c.b.a

Unfortunately, it is necessary to use an extra pair of escaped quotation marks in the argument for this to work.

  • Thank you for reply, but how can I simplify a nested CALL with variables like the updated description above? – 陈泽宇 Jul 26 '17 at 2:16
  • I'm not sure what you mean by "simplify", but I have added an example of chaining (nesting) functions with a varying second parameter. Does that help? – deprecated Jul 26 '17 at 15:39
  • How can I use #+CALL: funY("Hello!") instead of the long call chain? Because I may repeat #+CALL: funY("Hello!"), but I don't want to copy #+CALL: funX("A", funX("B", funX("C", funX("D", funX("E", " Hello! "))))) many times. Also, the word "Hello" can be different in repeats – 陈泽宇 Jul 28 '17 at 1:50
  • OK, thanks for the clarification. I see what you want now, and I don't think it is possible. The programming features of the CALL syntax and header arguments are very limited – it is not a full programming language. In particular, it is not possible to add a "new" variable to a CALL statement, such as the varF in your funA2E – deprecated Jul 28 '17 at 17:47
  • I found a relevant thread on the org mailing list. This explains more fully why it is not possible, and lists several alternative approaches that might work: lists.gnu.org/archive/html/emacs-orgmode/2017-01/msg00003.html – deprecated Jul 28 '17 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.