

Table of Contents

Next: , Previous: Publishing, Up: Top

14 Working with source code

Source code here refers to any code typed in Org mode documents. Org can manage source code in any Org file once such code is tagged with begin and end markers. Working with source code begins with tagging source code blocks. Tagged source code blocks are not restricted to the preamble or the end of an Org document; they can go anywhere (with a few exceptions, such as not inside comments and fixed width areas). Here's a sample ‘src’ block in emacs-lisp:

     #+BEGIN_SRC emacs-lisp
       (defun org-xor (a b)
          "Exclusive or."
          (if a (not b) b))

Org can take the code in the block between the ‘#+BEGIN_SRC’ and ‘#+END_SRC’ tags, and format, compile, execute, and show the results. Org can simplify many housekeeping tasks essential to modern code maintenance. That's why these blocks in Org mode literature are sometimes referred to as ‘live code’ blocks (as compared to the static text and documentation around it). Users can control how ‘live’ they want each block by tweaking the headers for compiling, execution, extraction, and so on.

For editing ‘src’ code blocks, Org offers native Emacs major-modes. That leverages all the latest Emacs features for that source code language mode.






Org mode fulfills the promise of easy verification and maintenance of publishing reproducible research by keeping text, data, code, configuration settings of the execution environment, the results of the execution, and associated narratives, claims, references, and internal and external links—all in the same file.
