source-code-chapter.texi 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. @chapter Working with source code or Embedded Source Code
  2. @section Syntax
  3. :PROPERTIES:
  4. :CUSTOM_ID: syntax
  5. :END:
  6. @subsection Source Code Block
  7. The basic syntax of source code blocks in Org-babel is as follows:
  8. @example
  9. #+begin_src language header-arguments
  10. body
  11. #+end_src
  12. @end example
  13. - language :: The language of the code in the source code block. Valid
  14. values must be members of =org-babel-interpreters=.
  15. - header-arguments :: Header arguments control many facets of the
  16. evaluation and output of source code blocks. See the [[header-arguments][Header
  17. Arguments]] section for a complete review of available header
  18. arguments.
  19. - body :: The source code to be evaluated. An important key-binding
  20. is C-c '. This calls =org-edit-src-code=, a function that brings
  21. up an edit buffer containing the code using the Emacs major mode
  22. appropriate to the language. You can edit your source code block
  23. as you regularly would in Emacs.
  24. This syntax can be expanded by naming the source code block.
  25. @example
  26. #+sourcename
  27. #+begin_src language header-arguments
  28. body
  29. #+end_src
  30. @end example
  31. - name :: This name is associated with the source code block. This is
  32. similar to the =#+tblname= lines that can be used to name tables
  33. in Org-mode files. Referencing the name of a source code
  34. block makes it possible to evaluate the block from other places in
  35. the file, other files, or inside Org-mode tables. It
  36. is also possible to pass arguments to a source code block through
  37. this =#+source:= line (see [[alternate-argument-syntax][Alternate argument syntax]]).
  38. @subsection Library of Babel
  39. [[file:library-of-babel.org][Library of Babel]] functions can be called using the following syntax.
  40. @example
  41. #+lob: R-plot(data=R-plot-example-data)
  42. @end example
  43. @subsection Aliases
  44. Keyword aliases are intended to make Org-babel feel natural to
  45. programmers fluent in a variety of languages. For example,
  46. @example
  47. #+srcname: alias-example
  48. #+begin_src emacs-lisp
  49. '((call lob)
  50. (source function srcname)
  51. (results resname))
  52. #+end_src
  53. #+results: alias-example
  54. | call | lob | |
  55. | source | function | srcname |
  56. | results | resname | |
  57. @end example
  58. - =#+srcname:= can be replaced with either of two aliases, =#+source:= or =#+function:=.
  59. - =#+results:= can be replaced with its alias, =#+resname:=.
  60. When calling Library of Babel functions, as in the following
  61. example, there are two acceptable keywords. The =#+lob= call in
  62. the example could be replaced with its alias, =#+call=.
  63. @example
  64. #+lob: R-plot(data=R-plot-example-data)
  65. @end example
  66. @section Languages
  67. :PROPERTIES:
  68. :CUSTOM_ID: languages
  69. :END:
  70. Org-babel has support for the following languages.
  71. | Language | Documentation | Identifier | Requirements |
  72. |----------------+-----------------------------+------------+---------------------------------------------|
  73. | Asymptote | org-babel-doc-asymptote | asymptote | [[http://asymptote.sourceforge.net/][asymptote]], [[http://asymptote.sourceforge.net/doc/Editing-modes.html][asy-mode]] |
  74. | C | [[file:languages/org-babel-doc-C.org][org-babel-doc-C]] | C | none |
  75. | Clojure | [[file:languages/org-babel-doc-clojure.org][org-babel-doc-clojure]] | clojure | [[http://clojure.org/][clojure]], [[http://www.emacswiki.org/emacs/clojure-mode.el][clojure-mode]], [[http://common-lisp.net/project/slime/][slime]], [[http://clojure.codestuffs.com/][swank-clojure]] |
  76. | css | org-babel-doc-css | css | none |
  77. | ditaa | org-babel-doc-ditaa | ditaa | [[http://ditaa.org/ditaa/][ditaa]] (bundled with Org-mode) |
  78. | Graphviz | org-babel-doc-dot | dot | [[http://www.graphviz.org/][dot]] |
  79. | Emacs Lisp | org-babel-doc-emacs-lisp | emacs-lisp | none |
  80. | gnuplot | org-babel-doc-gnuplot | gnuplot | [[http://www.gnuplot.info/][gnuplot]], [[http://cars9.uchicago.edu/~ravel/software/gnuplot-mode.html][gnuplot-mode]] |
  81. | Haskell | org-babel-doc-haskell | haskell | [[http://www.haskell.org/][haskell]], [[http://projects.haskell.org/haskellmode-emacs/][haskell-mode]], [[http://www.haskell.org/haskellwiki/Haskell_mode_for_Emacs#inf-haskell.el:_the_best_thing_since_the_breadknife][inf-haskell]], [[http://people.cs.uu.nl/andres/lhs2tex/][lhs2tex]] |
  82. | Matlab | [[file:languages/org-babel-doc-octave-matlab.org][org-babel-doc-octave-matlab]] | matlab | matlab, [[http://sourceforge.net/projects/matlab-emacs/][matlab.el]] |
  83. | LaTeX | [[file:languages/org-babel-doc-LaTeX.org][org-babel-doc-latex]] | latex | [[http://www.latex-project.org/][latex]], [[http://www.gnu.org/software/auctex/][auctex]], [[http://www.gnu.org/software/auctex/reftex.html][reftex]] |
  84. | Objective Caml | org-babel-doc-ocaml | ocaml | [[http://caml.inria.fr/][ocaml]], [[http://www-rocq.inria.fr/~acohen/tuareg/][tuareg-mode]] |
  85. | Octave | [[file:languages/org-babel-doc-octave-matlab.org][org-babel-doc-octave-matlab]] | octave | octave |
  86. | OZ | [[file:languages/org-babel-doc-oz.org][org-babel-doc-oz]] | oz | [[http://www.mozart-oz.org/][Mozart]] which includes a major mode |
  87. | Perl | org-babel-doc-perl | perl | [[http://www.perl.org/][perl]], [[http://www.emacswiki.org/emacs/CPerlMode][cperl-mode]] (optional) |
  88. | Python | org-babel-doc-python | python | [[http://www.python.org/][python]], [[https://launchpad.net/python-mode][python-mode]] (optional) |
  89. | R | [[file:languages/org-babel-doc-R.org][org-babel-doc-R]] | R | [[http://www.r-project.org/][R]], [[http://ess.r-project.org/][ess-mode]] |
  90. | Ruby | org-babel-doc-ruby | ruby | [[http://www.ruby-lang.org/][ruby]], [[http://www.ruby-lang.org/][irb]], [[http://github.com/eschulte/rinari/raw/master/util/ruby-mode.el][ruby-mode]], [[http://github.com/eschulte/rinari/raw/master/util/inf-ruby.el][inf-ruby mode]] |
  91. | Sass | org-babel-doc-sass | sass | [[http://sass-lang.com/][sass]], [[http://github.com/nex3/haml/blob/master/extra/sass-mode.el][sass-mode]] |
  92. | GNU Screen | [[file:languages/org-babel-doc-screen.org][org-babel-doc-screen]] | screen | [[http://www.gnu.org/software/screen/][screen]], a terminal |
  93. | shell | org-babel-doc-sh | sh[fn:1] | a shell |
  94. | SQL | org-babel-doc-sql | sql | none |
  95. To add support for a particular language to your Org-babel
  96. installation first make sure that the requirements of the language
  97. are met, then add a line like the following to your Emacs
  98. configuration, (replace "identifier" with one of the
  99. entries in the Identifier column of the table).
  100. @example
  101. (require 'org-babel-identifier)
  102. @end example
  103. @section Header Arguments
  104. :PROPERTIES:
  105. :CUSTOM_ID: header-arguments
  106. :END:
  107. Definitions of all Org-babel header arguments are given [[header-argument-specific-documentation][below]]. In
  108. addition, some languages may add their own header arguments. Please
  109. see the language-specific documentation for information on
  110. language-specific header arguments.
  111. @subsection Using Header Arguments
  112. The values of header arguments can be set in three different ways,
  113. each more specific than the last.
  114. @subsubsection System-wide
  115. :PROPERTIES:
  116. :CUSTOM_ID: system-wide-header-argument
  117. :END:
  118. System-wide values of header arguments can be specified by
  119. customizing the =org-babel-default-header-args= variable:
  120. @example
  121. org-babel-default-header-args is a variable defined in `org-babel.el'.
  122. Its value is
  123. ((:session . "none")
  124. (:results . "replace")
  125. (:exports . "code")
  126. (:cache . "no")
  127. (:noweb . "no"))
  128. Documentation:
  129. Default arguments to use when evaluating a source block.
  130. @end example
  131. [[#default-noweb]]
  132. For example, the following example could be used to set the default value
  133. of =:noweb= header arguments to =yes=. This would have the effect of
  134. expanding =:noweb= references by default when evaluating source code blocks.
  135. @example
  136. (setq org-babel-default-header-args
  137. (cons '(:noweb . "yes")
  138. (assq-delete-all :noweb org-babel-default-header-args)))
  139. @end example
  140. @subsubsection Org-mode Properties
  141. Header arguments are also read from [[http://orgmode.org/manual/Properties-and-Columns.html#Properties-and-Columns][Org-mode properties]], which
  142. means they can be set on the outline header level. For example, the
  143. value of the =:cache= header argument will default to true in all
  144. source code blocks under the following example of an Org-mode outline header:
  145. @example
  146. * outline header
  147. :PROPERTIES:
  148. :cache: yes
  149. :CUSTOM_ID: property-set-header-arguments
  150. :END:
  151. @end example
  152. Properties defined in this way override the properties set in
  153. =org-babel-default-header-args=. It is convenient to use the
  154. =org-set-property= function bound to =C-c C-x p= to set properties
  155. in Org-mode documents.
  156. @subsubsection Source Code Block
  157. :PROPERTIES:
  158. :CUSTOM_ID: single-block-header-arguments
  159. :END:
  160. The most common way to assign values to header arguments is at the
  161. source code block level. This can be done by listing a sequence of
  162. header arguments and their values as part of the =#+begin_src=
  163. line. Properties set in this way override both the values of
  164. =org-babel-default-header-args= and header argument specified as
  165. properties. In the following example, the
  166. =:results= header argument is set to =silent=, meaning the results
  167. of execution will not be inserted in the buffer, and the =:exports=
  168. header argument is set to =code=, meaning only the body of the
  169. source code block
  170. will be preserved on export to HTML or LaTeX.
  171. @example
  172. #+source: factorial
  173. #+begin_src haskell :results silent :exports code
  174. fac 0 = 1
  175. fac n = n * fac (n-1)
  176. #+end_src
  177. @end example
  178. @subsection Specific Header Arguments
  179. :PROPERTIES:
  180. :CUSTOM_ID: header-argument-specific-documentation
  181. :END:
  182. @subsubsection =:var=
  183. :PROPERTIES:
  184. :CUSTOM_ID: header-argument-var
  185. :END:
  186. The =:var= header argument is used to pass arguments to
  187. source code blocks. The specifics of how arguments are included
  188. in a source code block are language specific and are
  189. addressed in the language-specific documentation. However, the
  190. syntax used to specify arguments is the same across all
  191. languages. The values passed to arguments can be or
  192. - literal values
  193. - values from org-mode tables
  194. - the results of other source code blocks
  195. These values can be indexed in a manner similar to arrays -- see
  196. [[var-argument-indexing][argument indexing]].
  197. The following syntax is used to pass arguments to source code
  198. blocks using the =:var= header argument.
  199. @example
  200. :var name=assign
  201. @end example
  202. where =assign= can take one of the following forms
  203. - literal value :: either a string ="string"= or a number =9=.
  204. - reference :: a table name:
  205. @example
  206. #+tblname: example-table
  207. | 1 |
  208. | 2 |
  209. | 3 |
  210. | 4 |
  211. #+source: table-length
  212. #+begin_src emacs-lisp :var table=example-table
  213. (length table)
  214. #+end_src
  215. #+results: table-length
  216. : 4
  217. @end example
  218. a source code block name, as assigned by =#+srcname:=,
  219. followed by parentheses:
  220. @example
  221. #+begin_src emacs-lisp :var length=table-length()
  222. (* 2 length)
  223. #+end_src
  224. #+results:
  225. : 8
  226. @end example
  227. In addition, an argument can be passed to the source code
  228. block referenced by =:var=. The argument is passed within
  229. the parentheses following the source code block name:
  230. @example
  231. #+source: double
  232. #+begin_src emacs-lisp :var input=8
  233. (* 2 input)
  234. #+end_src
  235. #+results: double
  236. : 16
  237. #+source: squared
  238. #+begin_src emacs-lisp :var input=double(input=1)
  239. (* input input)
  240. #+end_src
  241. #+results: squared
  242. : 4
  243. @end example
  244. **** alternate argument syntax
  245. :PROPERTIES:
  246. :CUSTOM_ID: alternate-argument-syntax
  247. :END:
  248. It is also possible to specify arguments in a potentially more
  249. natural way using the =#+source:= line of a source code block.
  250. As in the following example arguments can be packed inside of
  251. parenthesis following the source name.
  252. @example
  253. #+source: double(input=0)
  254. #+begin_src emacs-lisp
  255. (* 2 input)
  256. #+end_src
  257. @end example
  258. **** indexable variable values
  259. :PROPERTIES:
  260. :CUSTOM_ID: var-argument-indexing
  261. :END:
  262. It is possible to assign a portion of a value to a
  263. variable in a source block. The following example
  264. assigns the second and third rows of the table
  265. =example-table= to the variable =data=:
  266. @example
  267. :var data=example-table[1:2]
  268. @end example
  269. *Note:* ranges are indexed using the =:= operator.
  270. *Note:* indices are 0 based.
  271. The following example assigns the second column of the
  272. first row of =example-table= to =data=:
  273. @example
  274. :var data=example-table[0,1]
  275. @end example
  276. It is possible to index into the results of source code blocks
  277. as well as tables. Any number of dimensions can be indexed.
  278. Dimensions are separated from one another by commas.
  279. For more information on indexing behavior see the documentation
  280. for the =org-babel-ref-index-list= function -- provided below.
  281. @example
  282. org-babel-ref-index-list is a Lisp function in `org-babel-ref.el'.
  283. (org-babel-ref-index-list INDEX LIS)
  284. Return the subset of LIS indexed by INDEX. If INDEX is
  285. separated by ,s then each PORTION is assumed to index into the
  286. next deepest nesting or dimension. A valid PORTION can consist
  287. of either an integer index, or two integers separated by a : in
  288. which case the entire range is returned.
  289. @end example
  290. *Note:* In Emacs, the documentation for any function or variable
  291. can be read using the =describe-function= (M-x describe
  292. function) and =describe-variable= (M-x describe variable)
  293. functions, respectively.
  294. @subsubsection =:results=
  295. :PROPERTIES:
  296. :CUSTOM_ID: header-argument-results
  297. :END:
  298. There are three types of results header argument:
  299. - *collection* header arguments specify how the results should be collected from
  300. the source code block;
  301. - *type* header arguments specify what type of result the source code block
  302. will return -- which has implications for how they will be
  303. inserted into the Org-mode buffer; and
  304. - *handling* header arguments specify how the results of
  305. evaluating the source code block should be handled.
  306. *Note:* only one option from each type may be supplied per source code
  307. block.
  308. **** collection
  309. :PROPERTIES:
  310. :CUSTOM_ID: header-argument-results-collection
  311. :END:
  312. The following options are mutually exclusive, and specify how the
  313. results should be collected from the source code block.
  314. - value :: This is the default. The result is the value
  315. of the last statement in the source code block.
  316. This header argument places Org-babel in functional
  317. mode. Note that in some languages, e.g., python,
  318. use of this result type requires that a =return=
  319. statement be included in the body of the source code
  320. block. E.g., =:results value=.
  321. - output :: The result is the collection of everything printed
  322. to stdout during the execution of the source code
  323. block. This header argument places Org-babel in scripting
  324. mode. E.g., =:results output=.
  325. **** type
  326. The following options are mutually exclusive and specify what
  327. type of results the code block will return. By default, results
  328. are inserted as either a *table* or *scalar* depending on their
  329. value.
  330. - table, vector :: The results should be interpreted as an Org-mode table.
  331. If a single value is returned, Org-babel will convert it
  332. into a table with one row and one column. E.g., =:results
  333. value table=.
  334. - scalar, verbatim :: The results should be interpreted
  335. literally -- meaning they will not be converted into a table.
  336. The results will be inserted into the Org-mode buffer as
  337. quoted text. E.g., =:results value verbatim=.
  338. - file :: The results will be interpreted as the path to a file,
  339. and will be inserted into the Org-mode buffer as a file
  340. link. E.g., =:results value file=.
  341. - raw, org :: The results are interpreted as raw Org-mode code and
  342. are inserted directly into the buffer. If the results look
  343. like a table they will be aligned as such by Org-mode.
  344. E.g., =:results value raw=.
  345. - html :: Results are assumed to be HTML and will be enclosed in
  346. a =begin_html= block. E.g., =:results value html=.
  347. - latex :: Results assumed to be LaTeX and are enclosed in a
  348. =begin_latex= block. E.g., =:results value latex=.
  349. - code :: Result are assumed to be parseable code and are
  350. enclosed in a code block. E.g., =:results value code=.
  351. - pp :: The result is converted to pretty-printed code and is
  352. enclosed in a code block. This option currently supports
  353. Emacs Lisp, python, and ruby. E.g., =:results value pp=.
  354. **** handling
  355. The following results options indicate what Org-babel should do
  356. with the results once they are collected.
  357. - silent :: The results will be echoed in the minibuffer but
  358. will not be inserted into the Org-mode buffer. E.g.,
  359. =:results output silent=.
  360. - replace :: The default value. The results will be inserted
  361. into the Org-mode buffer. E.g., =:results output
  362. replace=.
  363. @subsubsection =:file=
  364. :PROPERTIES:
  365. :CUSTOM_ID: header-argument-file
  366. :END:
  367. =:file= is used to specify a path for file output in which case an
  368. [[http://orgmode.org/manual/Link-format.html#Link-format][org style]] =file:= link is inserted into the buffer as the
  369. result. Common examples are graphical output from [[file:languages/org-babel-doc-R.org][R]], gnuplot,
  370. ditaa and [[file:languages/org-babel-doc-LaTeX.org][latex]] blocks.
  371. See the [[#header-argument-dir][=:dir= and remote execution]] section for examples.
  372. Note that for some languages, including [[file:languages/org-babel-doc-R.org][R]], gnuplot, [[file:languages/org-babel-doc-LaTeX.org][latex]] and
  373. ditaa, graphical output is sent to the specified file without the
  374. file being referenced explicitly in the code block. See the
  375. documentation for the individual languages for details. In
  376. contrast, general purpose languages such as python and ruby
  377. require that the code explicitly create output corresponding to
  378. the path indicated by =:file=.
  379. While the =:file= header argument can be used to specify the path
  380. to the output file,
  381. @subsubsection =:dir= and remote execution
  382. :PROPERTIES:
  383. :CUSTOM_ID: header-argument-dir
  384. :END:
  385. =:dir= specifies the /default directory/ during code block
  386. execution. If it is absent, then the directory associated with the
  387. current buffer is used. In other words, supplying =:dir path=
  388. temporarily has the same effect as changing the current directory
  389. with =M-x cd path=, and then not supplying =:dir=. Under the
  390. surface, =:dir= simply sets the value of the emacs variable
  391. =default-directory=.
  392. When using =:dir=, you should supply a relative path for [[#header-argument-file][file
  393. output]] (e.g. =:file myfile.jpg= or =:file results/myfile.jpg=) in
  394. which case that path will be interpreted relative to the default
  395. directory.
  396. In other words, if you want your plot to go into a folder called
  397. Work in your home directory, you could use
  398. @example
  399. #+begin_src R :file myplot.png :dir ~/Work
  400. matplot(matrix(rnorm(100), 10), type="l")
  401. #+end_src
  402. @end example
  403. **** Remote execution
  404. A directory on a remote machine can be specified using [[http://www.gnu.org/software/tramp/#Filename-Syntax][tramp
  405. filename syntax]], in which case the code will be executed on the
  406. remote machine[fn:2]. An example is
  407. @example
  408. #+begin_src R :file plot.png :dir /dand@@yakuba.princeton.edu:
  409. plot(1:10, main=system("hostname", intern=TRUE))
  410. #+end_src
  411. @end example
  412. Text results will be returned to the local org buffer as normal, and
  413. file output will be created on the remote machine with relative paths
  414. interpreted relative to the remote directory. An org link to the
  415. remote file will be created.
  416. So in the above example a plot will be created on the remote machine,
  417. and a link of the following form will be inserted in the org buffer:
  418. @example
  419. [[file:/scp:dand@@yakuba.princeton.edu:/home/dand/plot.png][plot.png]]
  420. @end example
  421. Most of this functionality follows immediately from the fact that
  422. =:dir= sets the value of the emacs variable =default-directory=,
  423. thanks to [[http://www.gnu.org/software/tramp/][tramp]]. Those using XEmacs, or GNU Emacs prior to
  424. version 23 may need to install tramp separately in order for the
  425. above features to work correctly.
  426. **** Further points
  427. - If =:dir= is used in conjunction with =:session=, although it
  428. will determine the starting directory for a new session as
  429. expected, no attempt is currently made to alter the directory
  430. associated with an existing session.
  431. - =:dir= should typically not be used to create files during
  432. export with =:exports results= or =:exports both=. The reason
  433. is that, in order to retain portability of exported material
  434. between machines, during export, links inserted into the buffer
  435. will *not* be expanded against default directory. Therefore, if
  436. default-directory is altered using =:dir=, it it probable that
  437. the file will be created in a location to which the link does
  438. not point.
  439. @subsubsection =:exports=
  440. :PROPERTIES:
  441. :CUSTOM_ID: header-argument-exports
  442. :END:
  443. Specify what should be included in HTML or LaTeX exports of the
  444. Org-mode file.
  445. - code :: the default. The body of code is included
  446. into the exported file. E.g., =:exports code=.
  447. - results :: the result of evaluating the code is included in the
  448. exported file. E.g., =:exports results=.
  449. - both :: both the code and results are included in the exported
  450. file. E.g., =:exports both=.
  451. - none :: nothing is included in the exported file. E.g.,
  452. =:exports none=.
  453. @subsubsection =:tangle=
  454. :PROPERTIES:
  455. :CUSTOM_ID: tangle-header-arguments
  456. :END:
  457. Specify whether or not the source code block should be included
  458. in tangled extraction of source code files.
  459. - yes :: the source code block is exported to a source code file
  460. named after the basename (name w/o extension) of the
  461. Org-mode file. E.g., =:tangle yes=.
  462. - no :: the default. The source code block is not
  463. exported to a source code file. E.g., =:tangle no=.
  464. - other :: Any other string passed to the =:tangle= header argument
  465. is interpreted as a file basename to which the block will
  466. be exported. E.g., =:tangle basename=.
  467. @subsubsection =:session=
  468. :PROPERTIES:
  469. :CUSTOM_ID: header-argument-session
  470. :END:
  471. Start a session for an interpreted language where state is
  472. preserved. This applies particularly to the supported languages
  473. python, R and ruby.
  474. By default, a session is not started.
  475. A string passed to the =:session= header argument will give the
  476. session a name. This makes it possible to run concurrent
  477. sessions for each interpreted language.
  478. @subsubsection =:noweb=
  479. :PROPERTIES:
  480. :CUSTOM_ID: header-argument-noweb
  481. :END:
  482. Controls the expansion of [[noweb-reference-syntax][noweb syntax]] references in a
  483. source code block. This header argument can have one of two
  484. values: =yes= or =no=.
  485. - =no= :: the default. No [[noweb-reference-syntax][noweb syntax]] specific action is taken
  486. on evaluating source code blocks/ However, noweb references
  487. will still be expanded during tangling.
  488. - =yes= :: all [[noweb-reference-syntax][noweb syntax]] references in the body of the source
  489. code block will be expanded before the block is evaluated.
  490. **** Noweb Prefix Lines
  491. Noweb insertions are now placed behind the line prefix of the
  492. =<<reference>>=.
  493. This behavior is illustrated in the following example. Because
  494. the =<<example>>= noweb reference appears behind the SQL
  495. comment syntax, each line of the expanded noweb reference will
  496. be commented.
  497. This source code block:
  498. @example
  499. -- <<example>>
  500. @end example
  501. expands to:
  502. @example
  503. -- this is the
  504. -- multi-line body of example
  505. @end example
  506. Note that noweb replacement text that does *not* contain any
  507. newlines will not be affected by this change, so it is still
  508. possible to use inline noweb references.
  509. Thanks to Sébastien Vauban for this idea.
  510. @subsubsection =:cache=
  511. :PROPERTIES:
  512. :CUSTOM_ID: header-argument-cache
  513. :END:
  514. Controls the use of in-buffer caching of source code block
  515. results to avoid re-running unchanged source code blocks. This
  516. header argument can have one of two values: =yes= or =no=.
  517. - =no= :: The default. No caching takes place and the source
  518. code block will be run every time it is executed.
  519. - =yes= :: every time the source code block is run a sha1 hash of
  520. the code and arguments passed to the block will be
  521. generated. This hash is packed into the =#+results:= line
  522. of the results and will be checked on subsequent executions
  523. of the source code block. If the source code block has not
  524. changed since the last time it was evaluated, it will not be
  525. re-evaluated.
  526. @section Results
  527. :PROPERTIES:
  528. :CUSTOM_ID: results-specification
  529. :END:
  530. The way in which results are handled depends on whether a [[header-argument-session][session]]
  531. is invoked, as well as on whether
  532. [[header-argument-results-collection][=:results value=] or
  533. [[header-argument-results-collection][=:results output=]] is used. The following table shows the
  534. possibilities:
  535. | | non-session (default) | =:session= |
  536. |-------------------+--------------------------+-------------------------------------|
  537. | =:results value= | value of last expression | value of last expression |
  538. | =:results output= | contents of stdout | concatenation of interpreter output |
  539. *Note:* With =:results value=, the result in both =:session= and
  540. non-session is returned to Org-mode as a table (a one- or
  541. two-dimensional vector of strings or numbers) when appropriate.
  542. **** Non-session
  543. ***** =:results value=
  544. This is the default. Internally, the value is obtained by
  545. wrapping the code in a function definition in the external
  546. language, and evaluating that function. Therefore, code should be
  547. written as if it were the body of such a function. In particular,
  548. note that python does not automatically return a value from a
  549. function unless a =return= statement is present, and so a
  550. 'return' statement will usually be required in python.
  551. This is the only one of the four evaluation contexts in which the
  552. code is automatically wrapped in a function definition.
  553. ***** =:results output=
  554. The code is passed to the interpreter as an external process, and
  555. the contents of the standard output stream are returned as
  556. text. (In certain languages this also contains the error output
  557. stream; this is an area for future work.)
  558. **** =:session=
  559. ***** =:results value=
  560. The code is passed to the interpreter running as an interactive
  561. Emacs inferior process. The result returned is the result of the
  562. last evaluation performed by the interpreter. (This is obtained in
  563. a language-specific manner: the value of the variable =_= in
  564. python and ruby, and the value of =.Last.value= in R).
  565. ***** =:results output=
  566. The code is passed to the interpreter running as an interactive
  567. Emacs inferior process. The result returned is the concatenation
  568. of the sequence of (text) output from the interactive
  569. interpreter. Notice that this is not necessarily the same as what
  570. would be sent to stdout if the same code were passed to a
  571. non-interactive interpreter running as an external process. For
  572. example, compare the following two blocks:
  573. @example
  574. #+begin_src python :results output
  575. print "hello"
  576. 2
  577. print "bye"
  578. #+end_src
  579. #+resname:
  580. : hello
  581. : bye
  582. @end example
  583. In non-session mode, the '2' is not printed and does not appear.
  584. @example
  585. #+begin_src python :results output :session
  586. print "hello"
  587. 2
  588. print "bye"
  589. #+end_src
  590. #+resname:
  591. : hello
  592. : 2
  593. : bye
  594. @end example
  595. But in =:session= mode, the interactive interpreter receives input '2'
  596. and prints out its value, '2'. (Indeed, the other print statements are
  597. unnecessary here).
  598. @section Noweb Reference Syntax
  599. :PROPERTIES:
  600. :CUSTOM_ID: noweb-reference-syntax
  601. :END:
  602. The [[http://www.cs.tufts.edu/~nr/noweb/][Noweb]] Literate Programming system allows named blocks of code to
  603. be referenced by using the familiar Noweb syntax:
  604. : <<code-block-name>>
  605. Noweb references are handled differently during evaluation and
  606. tangling.
  607. When a document is tangled, Noweb references are replaced with the
  608. named source code block.
  609. When a source code block is evaluated, the action depends upon the
  610. value of the =:noweb= header argument. If =:noweb yes=, then a
  611. Noweb reference is expanded before evaluation. If =:noweb no=,
  612. the default, then the reference is not expanded before
  613. evaluation.
  614. *Note:* the default value, =:noweb no=, was chosen to ensure that
  615. Org-babel does not break correct code in a language, such as Ruby,
  616. where =<<arg>>= is a syntactically valid construct. If =<<arg>>= is
  617. not syntactically valid in languages that you use, then please
  618. consider [[*System%20wide][setting the default value]].
  619. An example that uses the Noweb reference syntax is provided in the
  620. [[literate programming example]].
  621. @section Key Bindings & Useful Functions
  622. Org-babel re-binds many common Org-mode key sequences depending on
  623. the context. Within a source-code block the following sequences
  624. are rebound:
  625. | =C-c C-c= | [[function-org-babel-execute][org-babel-execute-src-block]] |
  626. | =C-c C-o= | [[function-org-babel-open-src-block-result][org-babel-open-src-block-result]] |
  627. | =C-up= | [[function-org-babel-load-in-session][org-babel-load-in-session]] |
  628. | =M-down= | [[function-org-babel-pop-to-session][org-babel-pop-to-session]] |
  629. Org-babel also exposes a number of functions behind the common
  630. =org-babel-key-prefix= of =C-c M-b=:
  631. #+begin_src emacs-lisp :exports none
  632. (lambda (binding
  633. (list (format "\\C-c \\M-b %s"
  634. (car binding))
  635. (format "[[function-%s][%s]]"
  636. (cdr binding) (cdr binding))))
  637. org-babel-key-bindings)
  638. #+end_src
  639. | =C-c M-b t= | [[function-org-babel-tangle][org-babel-tangle]] |
  640. | =C-c M-b T= | [[function-org-babel-tangle-file][org-babel-tangle-file]] |
  641. | =C-c M-b e= | [[function-org-babel-execute-src-block][org-babel-execute-src-block]] |
  642. | =C-c M-b s= | [[function-org-babel-execute-subtree][org-babel-execute-subtree]] |
  643. | =C-c M-b b= | [[function-org-babel-execute-buffer][org-babel-execute-buffer]] |
  644. | =C-c M-b h= | [[function-org-babel-sha1-hash][org-babel-sha1-hash]] |
  645. | =C-c M-b g= | [[function-org-babel-goto-named-source-block][org-babel-goto-named-source-block]] |
  646. | =C-c M-b l= | [[function-org-babel-lob-ingest][org-babel-lob-ingest]] |
  647. @subsection Functions
  648. @subsubsection org-babel-execute-src-block
  649. :PROPERTIES:
  650. :CUSTOM_ID: function-org-babel-execute-src-block
  651. :END:
  652. @example
  653. org-babel-execute-src-block is an interactive Lisp function in
  654. `org-babel.el'.
  655. (org-babel-execute-src-block &optional ARG INFO PARAMS)
  656. Execute the current source code block, and insert the results
  657. into the buffer. Source code execution and the collection and
  658. formatting of results can be controlled through a variety of
  659. header arguments.
  660. Optionally supply a value for INFO in the form returned by
  661. `org-babel-get-src-block-info'.
  662. Optionally supply a value for PARAMS which will be merged with
  663. the header arguments specified at the front of the source code
  664. block.
  665. @end example
  666. @subsubsection org-babel-open-src-block-result
  667. :PROPERTIES:
  668. :CUSTOM_ID: function-org-babel-open-src-block-result
  669. :END:
  670. @example
  671. org-babel-open-src-block-result is an interactive Lisp function in
  672. `org-babel.el'.
  673. (org-babel-open-src-block-result &optional RE-RUN)
  674. If `point' is on a src block then open the results of the
  675. source code block, otherwise return nil. With optional prefix
  676. argument RE-RUN the source-code block is evaluated even if
  677. results already exist.
  678. @end example
  679. @subsubsection org-babel-load-in-session
  680. :PROPERTIES:
  681. :CUSTOM_ID: function-org-babel-load-in-session
  682. :END:
  683. @example
  684. org-babel-load-in-session is an interactive Lisp function in
  685. `org-babel.el'.
  686. (org-babel-load-in-session &optional ARG INFO)
  687. Load the body of the current source-code block. Evaluate the
  688. header arguments for the source block before entering the
  689. session. After loading the body this pops open the session.
  690. [back]
  691. @end example
  692. @subsubsection org-babel-pop-to-session
  693. :PROPERTIES:
  694. :CUSTOM_ID: function-org-babel-pop-to-session
  695. :END:
  696. @example
  697. org-babel-pop-to-session is an interactive Lisp function in
  698. `org-babel.el'.
  699. (org-babel-pop-to-session &optional ARG INFO)
  700. Pop to the session of the current source-code block. If
  701. called with a prefix argument then evaluate the header arguments
  702. for the source block before entering the session. Copy the body
  703. of the source block to the kill ring.
  704. [back]
  705. @end example
  706. @subsubsection org-babel-tangle
  707. :PROPERTIES:
  708. :CUSTOM_ID: function-org-babel-tangle
  709. :END:
  710. @example
  711. org-babel-tangle is an interactive Lisp function in
  712. `org-babel-tangle.el'.
  713. It is bound to C-c M-b t.
  714. (org-babel-tangle &optional TARGET-FILE LANG)
  715. Extract the bodies of all source code blocks from the current
  716. file into their own source-specific files. Optional argument
  717. TARGET-FILE can be used to specify a default export file for all
  718. source blocks. Optional argument LANG can be used to limit the
  719. exported source code blocks by language.
  720. @end example
  721. @subsubsection org-babel-execute-subtree
  722. :PROPERTIES:
  723. :CUSTOM_ID: function-org-babel-execute-subtree
  724. :END:
  725. @example
  726. org-babel-execute-subtree is an interactive Lisp function in
  727. `org-babel.el'.
  728. It is bound to C-c M-b s.
  729. (org-babel-execute-subtree &optional ARG)
  730. Replace EVAL snippets in the entire subtree.
  731. @end example
  732. @subsubsection org-babel-execute-buffer
  733. :PROPERTIES:
  734. :CUSTOM_ID: function-org-babel-execute-buffer
  735. :END:
  736. @example
  737. org-babel-execute-buffer is an interactive Lisp function in
  738. `org-babel.el'.
  739. It is bound to C-c M-b b.
  740. (org-babel-execute-buffer &optional ARG)
  741. Replace EVAL snippets in the entire buffer.
  742. @end example
  743. @subsubsection org-babel-sha1-hash
  744. :PROPERTIES:
  745. :CUSTOM_ID: function-org-babel-sha1-hash
  746. :END:
  747. @example
  748. org-babel-sha1-hash is an interactive Lisp function in `org-babel.el'.
  749. It is bound to C-c M-b h.
  750. (org-babel-sha1-hash &optional INFO)
  751. Not documented.
  752. @end example
  753. @subsubsection org-babel-goto-named-source-block
  754. :PROPERTIES:
  755. :CUSTOM_ID: function-org-babel-goto-named-source-block
  756. :END:
  757. @example
  758. org-babel-goto-named-source-block is an interactive Lisp function in
  759. `org-babel.el'.
  760. It is bound to C-c M-b g.
  761. (org-babel-goto-named-source-block &optional NAME)
  762. Go to a named source-code block.
  763. @end example
  764. @subsubsection org-babel-lob-ingest
  765. :PROPERTIES:
  766. :CUSTOM_ID: function-org-babel-lob-ingest
  767. :END:
  768. @example
  769. org-babel-lob-ingest is an interactive Lisp function in
  770. `org-babel-lob.el'.
  771. It is bound to C-c M-b l.
  772. (org-babel-lob-ingest &optional FILE)
  773. Add all source-blocks defined in FILE to `org-babel-library-of-babel'.
  774. @end example
  775. @section Batch Execution
  776. It is possible to call Org-babel functions from the command line.
  777. This shell script calls [[function-org-babel-tangle][org-babel-tangle]] on every one of its
  778. arguments.
  779. Be sure to adjust the paths to fit your system.
  780. @example
  781. #!/bin/sh
  782. # -*- mode: shell-script -*-
  783. #
  784. # tangle a file with org-babel
  785. #
  786. DIR=`pwd`
  787. FILES=""
  788. # wrap each argument in the code required to call tangle on it
  789. for i in $@@; do
  790. FILES="$FILES \"$i\""
  791. done
  792. emacsclient \
  793. --eval "(progn
  794. (add-to-list 'load-path (expand-file-name \"~/src/org/lisp/\"))
  795. (add-to-list 'load-path (expand-file-name \"~/src/org/contrib/lisp/\"))
  796. (require 'org)(require 'org-exp)(require 'org-babel)
  797. (mapc (lambda (file)
  798. (find-file (expand-file-name file \"$DIR\"))
  799. (org-babel-tangle)
  800. (kill-buffer)) '($FILES)))"
  801. @end example
  802. @section Footnotes
  803. [fn:1] The former use of the =shell= identifier is now deprecated.
  804. [fn:2] As long as the interpreter executable is found on the remote
  805. machine: see the variable =tramp-remote-path=