Skip to content

Latest commit

 

History

History
292 lines (228 loc) · 7.47 KB

Org-mode-recipes.org

File metadata and controls

292 lines (228 loc) · 7.47 KB

Org-mode - Literate Programming Recipes

Org-mode Literate Programming Recipes

Overview

This section provides useful org-mode code blocks examples that can used for literate programming and creating executable snippets.

Documentation:

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

#+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

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)"

codes/report.txt

Example: Runnable C++ Code block

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;
}
Helloworld.

Runnable C++ Snippet

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:

Example: Exporting/Tangling code block to file

  • Properties:
    • tangle: 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

#!/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

Bookmarks to other examples