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