15.8 Extracting Source Code
Extracting source code from code blocks is a basic task in literate programming. Org has features to make this easy. In literate programming parlance, documents on creation are woven with code and documentation, and on export, the code is tangled for execution by a computer. Org facilitates weaving and tangling for producing, maintaining, sharing, and exporting literate programming documents. Org provides extensive customization options for extracting source code.
When Org tangles code blocks, it expands, merges, and transforms them. Then Org recomposes them into one or more separate files, as configured through the options. During this tangling process, Org expands variables in the source code, and resolves any noweb style references (see Noweb Reference Syntax).
Header argumentsβ
The βtangle
β header argument specifies if the code block is exported to source file(s).
βyes
ββ
Export the code block to source file. The file name for the source file is derived from the name of the Org file, and the file extension is derived from the source code language identifier. Example: β:tangle yes
β.
βno
ββ
The default. Do not extract the code in a source code file. Example: β:tangle no
β.
FILENAME
β
Export the code block to source file whose file name is derived from any string passed to the βtangle
β header argument. Org derives the file name as being relative to the directory of the Org fileβs location. Example: β:tangle FILENAME
β.
The βmkdirp
β header argument creates parent directories for tangled files if the directory does not exist. A βyes
β value enables directory creation whereas βno
β inhibits it.
The βcomments
β header argument controls inserting comments into tangled files. These are above and beyond whatever comments may already exist in the code block.
βno
ββ
The default. Do not insert any extra comments during tangling.
βlink
ββ
Wrap the code block in comments. Include links pointing back to the place in the Org file from where the code was tangled.
βyes
ββ
Kept for backward compatibility; same as βlink
β.
βorg
ββ
Nearest headline text from Org file is inserted as comment. The exact text that is inserted is picked from the leading context of the source block.
βboth
ββ
Includes both βlink
β and βorg
β options.
βnoweb
ββ
Includes βlink
β option, expands noweb references (see Noweb Reference Syntax), and wraps them in link comments inside the body of the code block.
The βpadline
β header argument controls insertion of newlines to pad source code in the tangled file.
βyes
ββ
Default. Insert a newline before and after each code block in the tangled file.
βno
ββ
Do not insert newlines to pad the tangled code blocks.
The βshebang
β header argument can turn results into executable script files. By setting it to a string valueβfor example, β:shebang "#!/bin/bash"
ββOrg inserts that string as the first line of the tangled file that the code block is extracted to. Org then turns on the tangled fileβs executable permission.
The βtangle-mode
β header argument specifies what permissions to set for tangled files by set-file-modes
. For example, to make a read-only tangled file, use β:tangle-mode (identity #o444)
β. To make it executable, use β:tangle-mode (identity #o755)
β. It also overrides executable permission granted by βshebang
β. When multiple source code blocks tangle to a single file with different and conflicting βtangle-mode
β header arguments, Orgβs behavior is undefined.
By default Org expands code blocks during tangling. The βno-expand
β header argument turns off such expansions. Note that one side-effect of expansion by org-babel-expand-src-block
also assigns values (see Environment of a Code Block) to variables. Expansions also replace noweb references with their targets (see Noweb Reference Syntax). Some of these expansions may cause premature assignment, hence this option. This option makes a difference only for tangling. It has no effect when exporting since code blocks for execution have to be expanded anyway.
Functionsβ
org-babel-tangle
β
Tangle the current file. Bound to C-c C-v t
.
With prefix argument only tangle the current code block.
org-babel-tangle-file
β
Choose a file to tangle. Bound to C-c C-v f
.
Hooksβ
org-babel-post-tangle-hook
β
This hook is run from within code files tangled by org-babel-tangle
, making it suitable for post-processing, compilation, and evaluation of code in the tangled files.
Jumping between code and Orgβ
Debuggers normally link errors and messages back to the source code. But for tangled files, we want to link back to the Org file, not to the tangled source file. To make this extra jump, Org uses org-babel-tangle-jump-to-org
function with two additional source code block header arguments:
- Set β
padline
β to trueβthis is the default setting. - Set β
comments
β to βlink
β, which makes Org insert links to the Org file.