Org-mode - Literate Programming Recipes
Table of Contents
1 Org-mode Literate Programming Recipes
1.1 Overview
This section provides useful org-mode code blocks examples that can used for literate programming and creating executable snippets.
Documentation:
1.2 Example: Runnable shell-script block
Code block source code
#+BEGIN_SRC sh :results verbatim :exports both echo "Language LC_ALL = "$LC_ALL echo "Kernel version = $(uname -r)" echo "Hostname = $(hostanme)" echo "Username = $(whoami)" echo "Current Date = $(date)" echo "Current directory = $(pwd)" #+END_SRC : Language LC_ALL = : Kernel version = 4.9.52-1-MANJARO : Hostname = : Username = archbox : Current Date = Sun Oct 8 00:53:19 -03 2017 : Current directory = /home/archbox/Documents/projects/emacs
Code block - to run the code block, place the cursor within it and type C-c C-c.
echo "Language LC_ALL = "$LC_ALL echo "Kernel version = $(uname -r)" echo "Hostname = $(hostanme)" echo "Username = $(whoami)" echo "Current Date = $(date)" echo "Current directory = $(pwd)"
Language LC_ALL = Kernel version = 4.9.52-1-MANJARO Hostname = Username = archbox Current Date = Sun Oct 8 00:53:19 -03 2017 Current directory = /home/archbox/Documents/projects/emacs
1.3 Example: Runnable shell-script block - export output to file
Source
#+BEGIN_SRC sh :exports both :file codes/report.txt echo -e "Language LC_ALL = "$LC_ALL echo -e "Kernel version = $(uname -r)" echo -e "Hostname = $(hostanme)" echo -e "Username = $(whoami)" echo -e "Current Date = $(date)" echo -e "Current directory = $(pwd)" #+END_SRC
The output is exported to the file codes/report.txt
echo -e "Language LC_ALL = "$LC_ALL echo -e "Kernel version = $(uname -r)" echo -e "Hostname = $(hostanme)" echo -e "Username = $(whoami)" echo -e "Current Date = $(date)" echo -e "Current directory = $(pwd)"
1.4 Example: Runnable C++ Code block
1.4.1 Complete C++ minimum program
Code block source
#+BEGIN_SRC c++ :exports both #include <iostream> int main(int argc, char** argv) { std::cout << "Hello, world." << std::endl; return 0; } #+END_SRC
Code block rendered
- To run this code block, place the cursor within it and type C-c C-c
#include <iostream> int main(int argc, char** argv) { std::cout << "Hello, world." << std::endl; return 0; }
Hello | world. |
1.4.2 Runnable C++ Snippet
Code block source
#+BEGIN_SRC cpp for(int i = -4; i < 10; i++){ std::cout << std::setw(10) << std::fixed << std::setprecision(2) << static_cast<double>(i) << std::setw(10) << std::fixed << std::setprecision(3) << log(i) << "\n"; } #+END_SRC
Code redered - after the user paste the code in Emacs.
for(int i = -4; i < 10; i++){ std::cout << std::setw(10) << std::fixed << std::setprecision(2) << static_cast<double>(i) << std::setw(10) << std::fixed << std::setprecision(3) << log(i) << "\n"; }
Program output:
-4.0 | nan |
-3.0 | nan |
-2.0 | nan |
-1.0 | nan |
0.0 | -inf |
1.0 | 0.0 |
2.0 | 0.693 |
3.0 | 1.099 |
4.0 | 1.386 |
5.0 | 1.609 |
6.0 | 1.792 |
7.0 | 1.946 |
8.0 | 2.079 |
9.0 | 2.197 |
1.5 Example: Exporting/Tangling code block to file
- Properties:
tangle: codes/scriptUnix.scala
- exports the code block to file codes/scriptUnix.scala
:tangle-mode (identity #o755)
- sets the file as "unix"-executable.
:padline no
-- Remove new line characters from the top of the file
Code block source
#+BEGIN_SRC scala :tangle codes/scriptUnix.scala :tangle-mode (identity #o755) :padline no #!/bin/sh exec scala -save "$0" "$@" !# // Display text in a GUI def displayText(text: String) = { import javax.swing.{JFrame, JTextArea, JScrollPane} val tarea = new JTextArea() val frame = new JFrame() frame.add(new JScrollPane(tarea)) frame.setSize(400, 500) frame.setVisible(true) frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE) tarea.setText(text) } def readFile(file: String) = { val src = scala.io.Source.fromFile(file) val txt = src.mkString src.close() txt } println("Testing Scala script") println("Arguments passed by user") args.foldLeft(0){(acc, a) => println(s"arg[${acc}] = ${a}") acc + 1 } displayText(readFile(args(0))) #+END_SRC
Code block rendered
- To tangle(aka export) this code block to the script
codes/scriptUnix.scala, run
M-x org-babel-tangle
#!/bin/sh exec scala -save "$0" "$@" !# // Display text in a GUI def displayText(text: String) = { import javax.swing.{JFrame, JTextArea, JScrollPane} val tarea = new JTextArea() val frame = new JFrame() frame.add(new JScrollPane(tarea)) frame.setSize(400, 500) frame.setVisible(true) frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE) tarea.setText(text) } def readFile(file: String) = { val src = scala.io.Source.fromFile(file) val txt = src.mkString src.close() txt } println("Testing Scala script") println("Arguments passed by user") args.foldLeft(0){(acc, a) => println(s"arg[${acc}] = ${a}") acc + 1 } displayText(readFile(args(0)))
To run the script codes/scriptUnix.scala, that displays the command line arguments and displays a file passed as first argument in a GUI, go to a terminal and run the command below.
$ codes/scriptUnix.scala /etc/protocols arg1 arg2 arg3 Testing Scala script Arguments passed by user arg[0] = /etc/protocols arg[1] = arg1 arg[2] = arg2 arg[3] = arg3