source-code-chapter.texi 42 KB

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