source-code-chapter.texi 40 KB

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