source-code-chapter.texi 39 KB

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