source-code-chapter.texi 39 KB

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