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 Dec 6 '16 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 Dec 6 '16 at 9:19
  • 1
    You may use (cadr (org-babel-lob--src-info NAME)) from ob-lob instead. – mutbuerger Dec 6 '16 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) – David Ongaro Dec 17 '16 at 5:30

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.

| improve this answer | |
  • 1
    Org-element-property is the function to use for that. – John Kitchin Dec 6 '16 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.

| improve this answer | |
  • 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? – David Ongaro Dec 6 '16 at 20:00

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.