This section provides useful org-mode code blocks examples that can used for literate programming and creating executable snippets.
Documentation:
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
#+RESULTS:
: 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
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)"
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. |
Code block source
#+HEADER: :includes (list "<iostream>" "<iomanip>" "<cmath>" "<string>")
#+HEADER: :exports both
#+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:
- 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