source-code-chapter.texi 39 KB

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