The other day I wanted to put the contents of a source code block into a header argument of another block. I couldn't find any ready to use function to do that though, so I came up with this:

(defun extract-src-content (name)    
    (org-babel-goto-named-src-block name)
    (car (nthcdr 21 (cadr (org-element-at-point))))))

But that doesn't seem to be right. First my elisp experience is not that vast so I don't know how to extract the value for the :value key out of (cadr (org-element-at-point)) so I just took the 21th element which can't be right and will probably break at the next org update. Second I still think there should be an org-function available to extract the contents of a named source code block, since it has to have a way to do that for export or editing.

Someone has an answer to either question?

  • What's the right way to extract the value after :value out of (cadr (org-element-at-point))?
  • Is there a ready to use org-function (or even org-syntax) to accomplish the same thing?

Bonus question

  • I tried to ask this on the #org-mode irc channel, but even though there are a lot of joined users it seems to be dead. Why is it still linked on http://orgmode.org/community.html then?
  • You'll need to stay there for longer than five minutes in an IRC channel to get answers. Lurkers are fairly common either, so don't assume that a 1000 user channel will have them all ready to answer your questions.
    – wasamasa
    Commented Dec 6, 2016 at 9:15
  • Regarding ready-made functions, Org is really bad at the whole API thing. The sources are full of hardly modular code, most of it is for interactive instead of programmatic use and there's quite a few WTFs hidden. Be thankful the org-elements API is usable for accomplishing your task ;)
    – wasamasa
    Commented Dec 6, 2016 at 9:19
  • 2
    You may use (cadr (org-babel-lob--src-info NAME)) from ob-lob instead.
    – mutbuerger
    Commented Dec 6, 2016 at 10:50
  • @wasamasa: I definitely waited longer than 5 min. But it's true that after 1 to 2 hours often an answer comes (even though not always and rarely with an actual solution) Commented Dec 17, 2016 at 5:30

3 Answers 3


Sometimes it helps to formulate a question to realize the answer. I did another look into the org-mode sources and found org-element-property so I can do

(org-element-property :value (org-element-at-point))

instead of (car (nthcdr 21 (cadr (org-element-at-point)))) which is much better and according to John Kitchin also the built in way to do it.

  • 1
    Org-element-property is the function to use for that. Commented Dec 6, 2016 at 23:54

I would just use the builtin noweb functionality: set the option :noweb yes and refer to the quoted code by <<blockname>> in the other block:

#+NAME: sine
#+BEGIN_SRC python
  import math


#+BEGIN_SRC python :noweb yes :results output :exports results
  print text

: import math
: x=math.sin(math.pi)

Edit: To supress the two linebreaks (before and after the code) that are introduced by having the """ in separate lines, you can use

#+BEGIN_SRC python :noweb yes :results output :exports results
 print text

But this is a python detail -- and python wizards surely know a more elegant way. Other languages might need other solutions, or will be fine directly (not every language complains when a string goes over a line break).

Just note, that anything before the << will be repeated for every source line, so you can easily comment out this for example.

  • 1
    I'm sorry, I realized I stated my question wrong. I don't actually want to set a var but another header argument, so it has to be set in the header not inserted in the code block. Currently I've this: #+begin_src http :results raw :select (extract-src-content "jq-active-actions"). Is there any builtin way to pass the literal block to the :select argument? Commented Dec 6, 2016 at 20:00
  • Thanks for the hint, I have the needs to get contents of a src-block, your answer solved my problem!
    – zeit
    Commented Jan 3 at 9:58

I was led to my preferred answer to that question by a comment (Thank you! @Mutbuerger ) :

You may use (cadr (org-babel-lob--src-info NAME)) from ob-lob instead.

This brought me to:

(org-babel-expand-noweb-references (org-babel-lob--src-info NAME))

The other solution with (org-element-at-point) didn't feel "idiomatic" for cases, where you are not with point at the desired src-block, and thus have to (save-excursion (org-babel-goto-named-src-block ..., which is "Remember, where you are, virtually move the cursor to the wanted source, and only then read.

Sure, the approach from AA works, but I looked for a solution, that "doesn't move the cursor" (or at least does it transparently), and feels more "idiomatic". Since comments can disappear, I'm using this to make the org-babel-lob--src-info-solution from @Mutbuerger more (permanently) visible.

This will also expand noweb-refs, if enabled for the src-block.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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