Added links and references to Clojure documentation in intro and reference pages.
[Worg/babel-doc.git] / org-contrib / babel / reference.org
blob4df55acede8728d74e84a2f4b757127822ac7a4a
1 #+OPTIONS:    H:3 num:nil toc:3 \n:nil @:t ::t |:t ^:{} -:t f:t *:t TeX:t LaTeX:nil skip:nil d:(HIDE) tags:not-in-toc
2 #+STARTUP:    align fold nodlcheck hidestars oddeven lognotestate hideblocks
3 #+SEQ_TODO:   TODO(t) INPROGRESS(i) WAITING(w@) | DONE(d) CANCELED(c@)
4 #+TAGS:       Write(w) Update(u) Fix(f) Check(c) noexport(n)
5 #+TITLE:      Org-babel: Reference
6 #+AUTHOR:     Eric Schulte, Dan Davison, Tom Dye
7 #+EMAIL:      schulte.eric at gmail dot com, davison at stats dot ox dot ac dot uk, tsd at tsdye dot com
8 #+LANGUAGE:   en
9 #+STYLE:      <style type="text/css">#outline-container-syntax { clear:both; }</style>
11 #+begin_html
12   <style type="text/css">
13    <!--/*--><![CDATA[/*><!--*/
14   #table-of-contents {
15     max-width: 100%;
16     margin: 0;
17   }
18     /*]]>*/-->
19   </style>
20 #+end_html
22 [[file:index.org][{Back to Babel's index}]]
24 #+begin_html
25   <div id="logo2" style="float: left; text-align: center; max-width:
26                          400px; font-size: 8pt; margin: 1em;">
27     <p>
28       <img src="../../images/babel/babelfish.png"  alt="Babel Fish"/>
29       <p>
30         The Babel Fish is small, yellow, and simultaneously translates
31         from one spoken language to another.
32       </p> 
33       <p> 
34         &ndash; The Hitchhiker's Guide to the Galaxy, Douglas Adams
35       </p>
36     </p>
37   </div>
38 #+end_html
40 * Syntax
41   :PROPERTIES:
42   :CUSTOM_ID: syntax
43   :END:
45 ** Source Code Block
46 The basic syntax of source code blocks in Org-babel is as follows:
48 : #+begin_src language header-arguments
49 : body
50 : #+end_src
52 - language :: The language of the code in the source code block. Valid
53      values must be members of =org-babel-interpreters=.
54 - header-arguments :: Header arguments control many facets of the
55      evaluation and output of source code blocks.  See the [[header-arguments][Header
56      Arguments]] section for a complete review of available header
57      arguments.
58 - body :: The source code to be evaluated.  An important key-binding
59      is C-c '.  This calls =org-edit-src-code=, a function that brings
60      up an edit buffer containing the code using the Emacs major mode
61      appropriate to the language.  You can edit your source code block
62      as you regularly would in Emacs.
64 This syntax can be expanded by naming the source code block.
66 : #+source: name
67 : #+begin_src language header-arguments
68 :   
69 : #+end_src
71 - name :: This name is associated with the source code block.  This is
72      similar to the =#+tblname= lines which can be used to name tables
73      in Org-mode files.  By referencing the name of a source code
74      block it is possible to evaluate the block from other places in
75      the file, from other files, or from inside Org-mode tables.  It
76      is also possible to pass arguments to a source code block through
77      this =#+source:= line (see [[alternate-argument-syntax][Alternate argument syntax]]).
79 ** Library of Babel
80 [[file:library-of-babel.org][Library of Babel]] functions can be called using the following syntax.
82 : #+lob: R-plot(data=R-plot-example-data)
84 ** Aliases
85    A number of aliases for Org-babel keywords have emerged that may
86    be more natural than their predecessors.  The following aliases
87    could be used in the following example
88    #+begin_example
89      ,#+srcname: alias-example
90      ,#+begin_src emacs-lisp 
91        '((call lob)
92          (source function srcname)
93          (results resname))  
94      ,#+end_src
95      
96      ,#+results: alias-example
97      | call    | lob      |         |
98      | source  | function | srcname |
99      | results | resname  |         |
100    #+end_example
101      - =#+srcname:= could be replaced with either of two aliases,  =#+source:= or =#+function:=.
102      - =#+results:= could be replaced with its alias, =#+resname:=.
104    When calling Library of Babel functions, as in the following
105    example, there are two acceptable keywords.  The =#+lob= call in
106    the example could be replaced with its alias, =#+call=.
107    #+begin_example
108      ,#+lob: R-plot(data=R-plot-example-data)
109    #+end_example
111 * Languages
112   :PROPERTIES:
113   :CUSTOM_ID: languages
114   :END:
115   
116   Org-babel has support for the following languages.
117   
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   | 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]]                       |
130   | Objective Caml | org-babel-doc-ocaml      | ocaml      | [[http://caml.inria.fr/][ocaml]], [[http://www-rocq.inria.fr/~acohen/tuareg/][tuareg-mode]]                          |
131   | Perl           | org-babel-doc-perl       | perl       | [[http://www.perl.org/][perl]], [[http://www.emacswiki.org/emacs/CPerlMode][cperl-mode]] (optional)                 |
132   | Python         | org-babel-doc-python     | python     | [[http://www.python.org/][python]], [[https://launchpad.net/python-mode][python-mode]] (optional)              |
133   | 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]]                                 |
134   | 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]]         |
135   | Sass           | org-babel-doc-sass       | sass       | [[http://sass-lang.com/][sass]], [[http://github.com/nex3/haml/blob/master/extra/sass-mode.el][sass-mode]]                             |
136   | GNU Screen     | [[file:languages/org-babel-doc-screen.org][org-babel-doc-screen]]     | screen     | [[http://www.gnu.org/software/screen/][screen]], a terminal                          |
137   | shell          | org-babel-doc-sh         | sh[fn:1]   | a shell                                     |
138   | SQL            | org-babel-doc-sql        | sql        | none                                        |
139   
140   To add support for a particular language to your Org-babel
141   installation first make sure that the requirements of the language
142   are met, then add a line like the following to your Emacs
143   configuration, (replace "identifier" with one of the
144   entries in the Identifier column of the table).
145   #+begin_src emacs-lisp 
146     (require 'org-babel-identifier)
147   #+end_src
149 * Header Arguments
150     :PROPERTIES:
151     :CUSTOM_ID: header-arguments
152     :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.
159 ** Using Header Arguments
161 The values of header arguments can be set in three different ways,
162 each more specific than the last.
164 *** System-wide
165     :PROPERTIES:
166     :CUSTOM_ID: system-wide-header-argument
167     :END:
169  System-wide values of header arguments can be specified by
170  customizing the =org-babel-default-header-args= variable:
171  #+begin_example 
172    org-babel-default-header-args is a variable defined in `org-babel.el'.
173    Its value is 
174    ((:session . "none")
175     (:results . "replace")
176     (:exports . "code")
177     (:cache . "no")
178     (:noweb . "no"))
179    
180    
181    Documentation:
182    Default arguments to use when evaluating a source block.
183  #+end_example
184  [[#default-noweb]]  
185  For example, the following example could be used to set the default value
186  of =:noweb= header arguments to =yes=.  This would have the effect of 
187  expanding =:noweb= references by default when evaluating source code blocks.
188  #+begin_src emacs-lisp :results silent :exports code
189    (setq org-babel-default-header-args
190          (cons '(:noweb . "yes")
191                (assq-delete-all :noweb org-babel-default-header-args)))
192  #+end_src
194 *** Org-mode Properties
196  Header arguments are also read from [[http://orgmode.org/manual/Properties-and-Columns.html#Properties-and-Columns][Org-mode properties]], which
197  means they can be set on the outline header level.  For example, the
198  value of the =:cache= header argument will default to true in all
199  source code blocks under the following example of an Org-mode outline header:
200  #+begin_example 
201    ,* outline header
202      :PROPERTIES:
203      :cache:    yes
204      :CUSTOM_ID: property-set-header-arguments
205      :END:
206  #+end_example
207  Properties defined in this way override the properties set in
208  =org-babel-default-header-args=.  It is convenient to use the
209  =org-set-property= function bound to =C-c C-x p= to set properties
210  in Org-mode documents.
212 *** Source Code Block
213     :PROPERTIES:
214     :CUSTOM_ID: single-block-header-arguments
215     :END:
216  The most common way to assign values to header arguments is at the
217  source code block level.  This can be done by listing a sequence of
218  header arguments and their values as part of the =#+begin_src=
219  line.  Properties set in this way override both the values of
220  =org-babel-default-header-args= and header argument specified as
221  properties.  In the following example, the
222  =:results= header argument is set to =silent=, meaning the results
223  of execution will not be inserted in the buffer, and the =:exports=
224  header argument is set to =code=, meaning only the body of the
225  source code block
226  will be preserved on export to HTML or LaTeX.
227  #+begin_example 
228    ,#+source: factorial
229    ,#+begin_src haskell :results silent :exports code
230      fac 0 = 1
231      fac n = n * fac (n-1)  
232    ,#+end_src
233  #+end_example
235 ** Specific Header Arguments
236     :PROPERTIES:
237     :CUSTOM_ID: header-argument-specific-documentation
238     :END:
240 *** =:var=
241     :PROPERTIES:
242     :CUSTOM_ID: header-argument-var
243     :END:
245     The =:var= header argument is used to pass arguments to
246     source code blocks.  The specifics of how arguments are included
247     in a source code block are language specific and are
248     addressed in the language-specific documentation. However, the
249     syntax used to specify arguments is the same across all
250     languages.  The values passed to arguments can be or
251     - literal values
252     - values from org-mode tables
253     - the results of other source code blocks
255     These values can be indexed in a manner similar to arrays -- see
256     [[var-argument-indexing][argument indexing]].
258     The following syntax is used to pass arguments to source code
259     blocks using the =:var= header argument.
261     #+begin_example
262       :var name=assign
263     #+end_example
265     where =assign= can take one of the following forms
267     - literal value :: either a string ="string"= or a number =9=.
268     - reference :: a table name:
269          
270          #+begin_example
271            ,#+tblname: example-table
272            | 1 |
273            | 2 |
274            | 3 |
275            | 4 |
276            
277            ,#+source: table-length
278            ,#+begin_src emacs-lisp :var table=example-table
279              (length table)
280            ,#+end_src
281            
282            ,#+results: table-length
283            : 4
284          #+end_example
285          
286          a source code block name, as assigned by =#+srcname:=,
287          followed by parentheses:
288          
289          #+begin_example
290            ,#+begin_src emacs-lisp :var length=table-length()
291              (* 2 length)
292            ,#+end_src
293            
294            ,#+results:
295            : 8
296          #+end_example
297          
298          In addition, an argument can be passed to the source code
299          block referenced by =:var=.  The argument is passed within
300          the parentheses following the source code block name:
301          
302          #+begin_example 
303            ,#+source: double
304            ,#+begin_src emacs-lisp :var input=8
305              (* 2 input)
306            ,#+end_src
307            
308            ,#+results: double
309            : 16
310            
311            ,#+source: squared
312            ,#+begin_src emacs-lisp :var input=double(input=1)
313              (* input input)
314            ,#+end_src
315            
316            ,#+results: squared
317            : 4
318          #+end_example
320 **** alternate argument syntax
321      :PROPERTIES:
322      :CUSTOM_ID: alternate-argument-syntax
323      :END:
324      
325      It is also possible to specify arguments in a potentially more
326      natural way using the =#+source:= line of a source code block.
327      As in the following example arguments can be packed inside of
328      parenthesis following the source name.
329      #+begin_example 
330        ,#+source: double(input=0)
331        ,#+begin_src emacs-lisp
332          (* 2 input)
333        ,#+end_src
334      #+end_example
335      
336 **** indexable variable values
337      :PROPERTIES:
338      :CUSTOM_ID: var-argument-indexing
339      :END:
340      
341      It is possible to assign a portion of a value to a
342      variable in a source block.  The following example
343      assigns the second and third rows of the table
344      =example-table= to the variable =data=:
345    
346      #+begin_example
347        :var data=example-table[1:2]
348      #+end_example
350      *Note:* ranges are indexed using the =:= operator.
351      
352      *Note:* indices are 0 based.
354      The following example assigns the second column of the
355      first row of =example-table= to =data=:
356    
357      #+begin_example
358        :var data=example-table[0,1]
359      #+end_example
360    
361      It is possible to index into the results of source code blocks
362      as well as tables.  Any number of dimensions can be indexed.
363      Dimensions are separated from one another by commas.  
365      For more information on indexing behavior see the documentation
366      for the =org-babel-ref-index-list= function -- provided below.
367      
368      #+begin_example 
369        org-babel-ref-index-list is a Lisp function in `org-babel-ref.el'.
370        
371        (org-babel-ref-index-list INDEX LIS)
372        
373        Return the subset of LIS indexed by INDEX.  If INDEX is
374        separated by ,s then each PORTION is assumed to index into the
375        next deepest nesting or dimension.  A valid PORTION can consist
376        of either an integer index, or two integers separated by a : in
377        which case the entire range is returned.
378      #+end_example
380      *Note:* In Emacs, the documentation for any function or variable
381      can be read using the =describe-function= (M-x describe
382      function) and =describe-variable= (M-x describe variable)
383      functions, respectively.
385 *** =:results=
386     :PROPERTIES:
387     :CUSTOM_ID: header-argument-results
388     :END:
390     There are three types of results header argument:
391     - *collection* header arguments specify how the results should be collected from
392        the source code block;
393     - *type* header arguments specify what type of result the source code block
394        will return -- which has implications for how they will be
395        inserted into the Org-mode buffer; and
396     - *handling* header arguments specify how the results of
397        evaluating the source code block should be handled.
399      *Note:* only one option from each type may be supplied per source code
400        block.
402 **** collection
403      The following options are mutually exclusive, and specify how the
404      results should be collected from the source code block.
406      - value :: This is the default.  The result is the value
407                 of the last statement in the source code block.
408                 This header argument places Org-babel in functional
409                 mode.  Note that in some languages, e.g., python,
410                 use of this result type requires that a =return=
411                 statement be included in the body of the source code
412                 block. E.g., =:results value=.
413     - output :: The result is the collection of everything printed
414                 to stdout during the execution of the source code
415                 block.  This header argument places Org-babel in scripting
416                 mode.  E.g., =:results output=.
418 **** type
419      The following options are mutually exclusive and specify what
420      type of results the code block will return.  By default, results
421      are inserted as either a *table* or *scalar* depending on their
422      value.
424      - table, vector :: The results should be interpreted as an Org-mode table.
425                         If a single value is returned, Org-babel will convert it
426                         into a table with one row and one column.  E.g., =:results
427                         value table=.
428      - scalar, verbatim :: The results should be interpreted
429           literally -- meaning they will not be converted into a table.
430           The results will be inserted into the Org-mode buffer as
431           quoted text.  E.g., =:results value verbatim=.
432      - file :: The results will be interpreted as the path to a file,
433                and will be inserted into the Org-mode buffer as a file
434                link.  E.g., =:results value file=.
435      - raw, org :: The results are interpreted as raw Org-mode code and
436                    are inserted directly into the buffer.  If the results look
437                    like a table they will be aligned as such by Org-mode.
438                    E.g., =:results value raw=.
439      - html :: Results are assumed to be HTML and will be enclosed in
440                a =begin_html= block.  E.g., =:results value html=.
441      - latex :: Results assumed to be LaTeX and are enclosed in a
442                 =begin_latex= block.  E.g., =:results value latex=.
443      - code :: Result are assumed to be parseable code and are
444                enclosed in a code block.  E.g., =:results value code=.
445      - pp :: The result is converted to pretty-printed code and is
446              enclosed in a code block.  This option currently supports
447              Emacs Lisp, python, and ruby.  E.g., =:results value pp=.
449 **** handling
450      The following results options indicate what Org-babel should do
451      with the results once they are collected.
453      - silent :: The results will be echoed in the minibuffer but
454                  will not be inserted into the Org-mode buffer.  E.g.,
455                  =:results output silent=.
456      - replace :: The default value.  The results will be inserted
457                   into the Org-mode buffer.  E.g., =:results output
458                   replace=.
460 *** =:exports=
461     :PROPERTIES:
462     :CUSTOM_ID: header-argument-exports
463     :END:
465     Specify what should be included in HTML or LaTeX exports of the
466     Org-mode file.
468     - code :: the default.  The body of code is included
469               into the exported file.  E.g., =:exports code=.
470     - results :: the result of evaluating the code is included in the
471                   exported file. E.g., =:exports results=.
472     - both :: both the code and results are included in the exported
473                file. E.g., =:exports both=.
474     - none :: nothing is included in the exported file.  E.g.,
475                =:exports none=.
477 *** =:tangle=
478     :PROPERTIES:
479     :CUSTOM_ID: tangle-header-arguments
480     :END: 
482     Specify whether or not the source code block should be included
483     in tangled extraction of source code files.
485     - yes :: the source code block is exported to a source code file
486              named after the basename (name w/o extension) of the
487              Org-mode file.  E.g., =:tangle yes=.
488     - no :: the default.  The source code block is not
489           exported to a source code file.  E.g., =:tangle no=.
490     - other :: Any other string passed to the =:tangle= header argument
491                 is interpreted as a file basename to which the block will
492                 be exported.  E.g., =:tangle basename=.
494 *** =:session=
495     :PROPERTIES:
496     :CUSTOM_ID: header-argument-session
497     :END:
499     Start a session for an interpreted language where state is
500     preserved.  This applies particularly to the supported languages
501     perl, python, R and ruby.
503     By default, a session is not started.
505     A string passed to the =:session= header argument will give the
506     session a name.  This makes it possible to run concurrent
507     sessions for each interpreted language.
509     Results are handled somewhat differently if a session is invoked.
512     |                   | non-session (default)    | =:session=                          |
513     |-------------------+--------------------------+-------------------------------------|
514     | =:results value=  | value of last expression | value of last expression            |
515     | =:results output= | contents of stdout       | concatenation of interpreter output |
516     
519     *Note:*  With =:results value=, the result in both =:session= and
520     non-session is returned to Org-mode as a table (a one- or
521     two-dimensional vector of strings or numbers) when appropriate.
523 **** Non-session
524 ***** =:results value=
525       This is the default. Internally, the value is obtained by
526       wrapping the code in a function definition in the external
527       language, and evaluating that function. Therefore, code should be
528       written as if it were the body of such a function. In particular,
529       note that python does not automatically return a value from a
530       function unless a =return= statement is present, and so a
531       'return' statement will usually be required in python.
533       This is the only one of the four evaluation contexts in which the
534       code is automatically wrapped in a function definition.
536 ***** =:results output=
537       The code is passed to the interpreter as an external process, and
538       the contents of the standard output stream are returned as
539       text. (In certain languages this also contains the error output
540       stream; this is an area for future work.)
542 **** =:session=
543 ***** =:results value=
544       The code is passed to the interpreter running as an interactive
545       Emacs inferior process. The result returned is the result of the
546       last evaluation performed by the interpreter. (This is obtained in
547       a language-specific manner: the value of the variable =_= in
548       python and ruby, and the value of =.Last.value= in R).
550 ***** =:results output= 
551        The code is passed to the interpreter running as an interactive
552        Emacs inferior process. The result returned is the concatenation
553        of the sequence of (text) output from the interactive
554        interpreter. Notice that this is not necessarily the same as what
555        would be sent to stdout if the same code were passed to a
556        non-interactive interpreter running as an external process. For
557        example, compare the following two blocks:
559 #+begin_src python :results output
560        print "hello"
561        2
562        print "bye"
563 #+end_src
565 #+resname:
566        : hello
567        : bye
569        In non-session mode, the '2' is not printed and does not appear.
571 #+begin_src python :results output :session
572        print "hello"
573        2
574        print "bye"
575 #+end_src
577 #+resname:
578        : hello
579        : 2
580        : bye
582        But in =:session= mode, the interactive interpreter receives input '2'
583        and prints out its value, '2'. (Indeed, the other print statements are
584        unnecessary here).
586 *** =:noweb=
587     :PROPERTIES:
588     :CUSTOM_ID: header-argument-noweb
589     :END:
591     Controls the expansion of [[noweb-reference-syntax][noweb syntax]] references in a
592     source code block.  This header argument can have one of two
593     values: =yes= or =no=. 
594     - =no= :: the default.  No [[noweb-reference-syntax][noweb syntax]] specific action is taken
595          on evaluating source code blocks/  However, noweb references
596          will still be expanded during tangling.
597     - =yes= :: all [[noweb-reference-syntax][noweb syntax]] references in the body of the source
598                code block will be expanded before the block is evaluated.
600 **** Noweb Prefix Lines
602      Noweb insertions are now placed behind the line prefix of the
603      =<<reference>>=.
604      
605      This behavior is illustrated in the following example.  Because
606      the =<<example>>= noweb reference appears behind the SQL
607      comment syntax, each line of the expanded noweb reference will
608      be commented.  
610      This source code block:
611    
612      #+begin_example 
613        -- <<example>>
614      #+end_example
615      
616    
617      expands to:
619      #+begin_example 
620        -- this is the
621        -- multi-line body of example
622      #+end_example
623    
624      Note that noweb replacement text that does *not* contain any
625      newlines will not be affected by this change, so it is still
626      possible to use inline noweb references.
627    
628      Thanks to Sébastien Vauban for this idea.
630 *** =:cache=
631     :PROPERTIES:
632     :CUSTOM_ID: header-argument-cache
633     :END:
635     Controls the use of in-buffer caching of source code block
636     results to avoid re-running unchanged source code blocks.  This
637     header argument can have one of two values: =yes= or =no=.
638     - =no= :: The default.  No caching takes place and the source
639          code block will be run every time it is executed.
640     - =yes= :: every time the source code block is run a sha1 hash of
641          the code and arguments passed to the block will be
642          generated.  This hash is packed into the =#+results:= line
643          of the results and will be checked on subsequent executions
644          of the source code block.  If the source code block has not
645          changed since the last time it was evaluated, it will not be
646          re-evaluated. 
648 * Noweb Reference Syntax
649   :PROPERTIES:
650   :CUSTOM_ID: noweb-reference-syntax
651   :END:
653   The [[http://www.cs.tufts.edu/~nr/noweb/][Noweb]] Literate Programming system allows named blocks of code to
654   be referenced by using the familiar Noweb syntax:
655   : <<code-block-name>>
657   Noweb references are handled differently during evaluation and
658   tangling. 
660   When a document is tangled, Noweb references are replaced with the
661   named source code block.  
663   When a source code block is evaluated, the action depends upon the
664   value of the =:noweb= header argument.  If =:noweb yes=, then a
665   Noweb reference is expanded before evaluation.  If =:noweb no=,
666   the default, then the reference is not expanded before
667   evaluation.  
669   *Note:* the default value, =:noweb no=, was chosen to ensure that
670   Org-babel does not break correct code in a language, such as Ruby,
671   where =<<arg>>= is a syntactically valid construct.  If =<<arg>>= is
672   not syntactically valid in languages that you use, then please
673   consider [[*System%20wide][setting the default value]].
674   
675   An example that uses the Noweb reference syntax is provided in the
676   [[literate programming example]].
678 * Key Bindings & Useful Functions
680   Org-babel re-binds many common Org-mode key sequences depending on
681   the context.  When in a source-code block the following sequences
682   are rebound
683   | \C-c \C-c | [[function-org-babel-execute][org-babel-execute-src-block]]     |
684   | \C-c \C-o | [[function-org-babel-open-src-block-result][org-babel-open-src-block-result]] |
685   | \C-up     | [[function-org-babel-load-in-session][org-babel-load-in-session]]       |
686   | \M-down   | [[function-org-babel-pop-to-session][org-babel-pop-to-session]]        |
688   Org-babel also exposes a number of functions behind a the common
689   org-babel-key-prefix of "\C-c \M-b".  These functions are as follows
690   #+begin_src emacs-lisp :exports none
691      (lambda (binding
692        (list (format "\\C-c \\M-b %s"
693                      (car binding))
694              (format "[[function-%s][%s]]"
695                      (cdr binding) (cdr binding))))
696      org-babel-key-bindings)
697   #+end_src
698   | \C-c \M-b t | [[function-org-babel-tangle][org-babel-tangle]]                  |
699   | \C-c \M-b T | [[function-org-babel-tangle-file][org-babel-tangle-file]]             |
700   | \C-c \M-b e | [[function-org-babel-execute-src-block][org-babel-execute-src-block]]       |
701   | \C-c \M-b s | [[function-org-babel-execute-subtree][org-babel-execute-subtree]]         |
702   | \C-c \M-b b | [[function-org-babel-execute-buffer][org-babel-execute-buffer]]          |
703   | \C-c \M-b h | [[function-org-babel-sha1-hash][org-babel-sha1-hash]]               |
704   | \C-c \M-b g | [[function-org-babel-goto-named-source-block][org-babel-goto-named-source-block]] |
705   | \C-c \M-b l | [[function-org-babel-lob-ingest][org-babel-lob-ingest]]              |
707 ** Functions
708 *** org-babel-execute-src-block
709     :PROPERTIES:
710     :CUSTOM_ID: function-org-babel-execute-src-block
711     :END:
713 #+begin_example 
714   org-babel-execute-src-block is an interactive Lisp function in
715   `org-babel.el'.
716   
717   (org-babel-execute-src-block &optional ARG INFO PARAMS)
718   
719   Execute the current source code block, and insert the results
720   into the buffer.  Source code execution and the collection and
721   formatting of results can be controlled through a variety of
722   header arguments.
723   
724   Optionally supply a value for INFO in the form returned by
725   `org-babel-get-src-block-info'.
726   
727   Optionally supply a value for PARAMS which will be merged with
728   the header arguments specified at the front of the source code
729   block.
730 #+end_example
732 *** org-babel-open-src-block-result
733     :PROPERTIES:
734     :CUSTOM_ID: function-org-babel-open-src-block-result
735     :END:
737 #+begin_example 
738   org-babel-open-src-block-result is an interactive Lisp function in
739   `org-babel.el'.
740   
741   (org-babel-open-src-block-result &optional RE-RUN)
742   
743   If `point' is on a src block then open the results of the
744   source code block, otherwise return nil.  With optional prefix
745   argument RE-RUN the source-code block is evaluated even if
746   results already exist.
747 #+end_example
749 *** org-babel-load-in-session
750     :PROPERTIES:
751     :CUSTOM_ID: function-org-babel-load-in-session
752     :END:
754 #+begin_example 
755   org-babel-load-in-session is an interactive Lisp function in
756   `org-babel.el'.
757   
758   (org-babel-load-in-session &optional ARG INFO)
759   
760   Load the body of the current source-code block.  Evaluate the
761   header arguments for the source block before entering the
762   session.  After loading the body this pops open the session.
763   
764   [back]
765 #+end_example
767 *** org-babel-pop-to-session
768     :PROPERTIES:
769     :CUSTOM_ID: function-org-babel-pop-to-session
770     :END:
772 #+begin_example 
773   org-babel-pop-to-session is an interactive Lisp function in
774   `org-babel.el'.
775   
776   (org-babel-pop-to-session &optional ARG INFO)
777   
778   Pop to the session of the current source-code block.  If
779   called with a prefix argument then evaluate the header arguments
780   for the source block before entering the session.  Copy the body
781   of the source block to the kill ring.
782   
783   [back]
784 #+end_example
786 *** org-babel-tangle
787     :PROPERTIES:
788     :CUSTOM_ID: function-org-babel-tangle
789     :END:
791 #+begin_example 
792   org-babel-tangle is an interactive Lisp function in
793   `org-babel-tangle.el'.
794   
795   It is bound to C-c M-b t.
796   
797   (org-babel-tangle &optional TARGET-FILE LANG)
798   
799   Extract the bodies of all source code blocks from the current
800   file into their own source-specific files.  Optional argument
801   TARGET-FILE can be used to specify a default export file for all
802   source blocks.  Optional argument LANG can be used to limit the
803   exported source code blocks by language.
804 #+end_example
806 *** org-babel-execute-subtree
807     :PROPERTIES:
808     :CUSTOM_ID: function-org-babel-execute-subtree
809     :END:
811 #+begin_example 
812   org-babel-execute-subtree is an interactive Lisp function in
813   `org-babel.el'.
814   
815   It is bound to C-c M-b s.
816   
817   (org-babel-execute-subtree &optional ARG)
818   
819   Replace EVAL snippets in the entire subtree.
820 #+end_example
822 *** org-babel-execute-buffer
823     :PROPERTIES:
824     :CUSTOM_ID: function-org-babel-execute-buffer
825     :END:
827 #+begin_example 
828   org-babel-execute-buffer is an interactive Lisp function in
829   `org-babel.el'.
830   
831   It is bound to C-c M-b b.
832   
833   (org-babel-execute-buffer &optional ARG)
834   
835   Replace EVAL snippets in the entire buffer.
836 #+end_example
838 *** org-babel-sha1-hash
839     :PROPERTIES:
840     :CUSTOM_ID: function-org-babel-sha1-hash
841     :END:
843 #+begin_example 
844   org-babel-sha1-hash is an interactive Lisp function in `org-babel.el'.
845   
846   It is bound to C-c M-b h.
847   
848   (org-babel-sha1-hash &optional INFO)
849   
850   Not documented.
851 #+end_example
853 *** org-babel-goto-named-source-block
854     :PROPERTIES:
855     :CUSTOM_ID: function-org-babel-goto-named-source-block
856     :END:
858 #+begin_example 
859   org-babel-goto-named-source-block is an interactive Lisp function in
860   `org-babel.el'.
861   
862   It is bound to C-c M-b g.
863   
864   (org-babel-goto-named-source-block &optional NAME)
865   
866   Go to a named source-code block.
867 #+end_example
869 *** org-babel-lob-ingest
870     :PROPERTIES:
871     :CUSTOM_ID: function-org-babel-lob-ingest
872     :END:
874 #+begin_example 
875   org-babel-lob-ingest is an interactive Lisp function in
876   `org-babel-lob.el'.
877   
878   It is bound to C-c M-b l.
879   
880   (org-babel-lob-ingest &optional FILE)
881   
882   Add all source-blocks defined in FILE to `org-babel-library-of-babel'.
883 #+end_example
885 * Batch Execution
886 It is possible to call Org-babel functions from the command line.
887 This shell script calls `[[function-org-babel-tangle][org-babel-tangle]]' on every one of it's
888 arguments.
890 Be sure to adjust the paths to fit your system.
891 #+begin_src sh
892   #!/bin/sh
893   # -*- mode: shell-script -*-
894   #
895   # tangle a file with org-babel
896   #
897   DIR=`pwd`
898   FILES=""
899   
900   # wrap each argument in the code required to call tangle on it
901   for i in $@; do
902   FILES="$FILES \"$i\""
903   done
904   
905   emacsclient \
906   --eval "(progn 
907   (add-to-list 'load-path (expand-file-name \"~/src/org/lisp/\"))
908   (add-to-list 'load-path (expand-file-name \"~/src/org/contrib/lisp/\"))
909   (require 'org)(require 'org-exp)(require 'org-babel)
910   (mapc (lambda (file)
911          (find-file (expand-file-name file \"$DIR\"))
912          (org-babel-tangle)
913          (kill-buffer)) '($FILES)))"
914 #+end_src
916 * Footnotes
918 [fn:1] The former use of the =shell= identifier is now deprecated.