The blog of a juvenile Geekus biologicus

ob-exec

I created a generic Org-babel mode, named ob-exec. This emacs-lisp script enable to execute any executable on a code fragment within Org-mode.

How it works

ob-exec simply execute the code sample with the provided command-line

For example, suppose you want to use the command line glpsol of the GNU Linear Programming Kit on a Linear program encoded in MathProg. To the best of my knowledge, there is no org-babel mode for GLPK. It wouln’t be too difficult to build one, but suppose further that you feel lazy and want to execute GLPK solver directly. Then, you can insert the following source code in emacs:

#+begin_src exec :shebang "glpol --math"
var x, integer, >=0; # mobile phones
var y, integer, >=0; # fix phones


subject to time_limit: 2 * x + 1.5 * y <= 4 * 24; # two days
subject to resource_limit: 100 * x + 150 * y <= 5000;

maximize profit: 70 * x + 120 * y; # production income
solve;
printf "Maximal profit obtained by building %d mobile phones and %d fix phones.\n", x, y;
end;
#+end_src
GLPSOL--GLPK LP/MIP Solver 5.0
Parameter(s) specified in the command line:
 --math /tmp/babel-FIKJoj/exec-Hr6idN
Reading model section from /tmp/babel-FIKJoj/exec-Hr6idN...
12 lines were read
Generating time_limit...
Generating resource_limit...
Generating profit...
Model has been successfully generated
GLPK Integer Optimizer 5.0
3 rows, 2 columns, 6 non-zeros
2 integer variables, none of which are binary
Preprocessing...
2 rows, 2 columns, 4 non-zeros
2 integer variables, none of which are binary
Scaling...
 A: min|aij| =  1.500e+00  max|aij| =  1.500e+02  ratio =  1.000e+02
GM: min|aij| =  8.409e-01  max|aij| =  1.189e+00  ratio =  1.414e+00
EQ: min|aij| =  7.071e-01  max|aij| =  1.000e+00  ratio =  1.414e+00
2N: min|aij| =  7.500e-01  max|aij| =  1.172e+00  ratio =  1.562e+00
Constructing initial basis...
Size of triangular part is 2
Solving LP relaxation...
GLPK Simplex Optimizer 5.0
2 rows, 2 columns, 4 non-zeros
*     0: obj =  -0.000000000e+00 inf =   0.000e+00 (2)
*     2: obj =   3.995000000e+03 inf =   0.000e+00 (0)
OPTIMAL LP SOLUTION FOUND
Integer optimization begins...
Long-step dual simplex will be used
+     2: mip =     not found yet <=              +inf        (1; 0)
+     2: >>>>>   3.980000000e+03 <=   3.980000000e+03   0.0% (2; 0)
+     2: mip =   3.980000000e+03 <=     tree is empty   0.0% (0; 3)
INTEGER OPTIMAL SOLUTION FOUND
Time used:   0.0 secs
Memory used: 0.1 Mb (124166 bytes)
Maximal profit obtained by building 2 mobile phones and 32 fix phones.
Model has been successfully processed

The filename placeholder

By default, ob-exec will execute the :shebang command line on the file, by adding the temporary filename of the code sample to the end of the command line. When the command line is a little bit more complex, and the filename must appear within the command, ob-exec allows to insert the placeholder {} in the command line, which will be replaced by the filename of the org-babel code sample upon execution. For example, suppose you wanted to use an awk command, on a file, you could use the following :shebang.

#+begin_src :shebang "awk -f {} /proc/cpuinfo"
$1 ~ "processor" {
   print $0
}
#+end_src

How to install ob-exec

You will need to get the file ob-exec.el, available on https://forge.s1gm4.eu/sortion/ob-exec git repository.

With doomemacs, you could use in your package.el file:

(package! ob-exec
  :recipe (:repo "https://forge.s1gm4.eu/sortion/ob-exec.git"))

With straight, you could use:

(straight-use-package '(ob-exec :type git
                                   :repo "https://forge.s1gm4.eu/sortion/ob-exec.git"
                                   :branch "main"))

Limitations

ob-exec does not support syntax highlighting or any other fancy stuff you could have when using a proper org-babel mode associated with programming language with a real emacs mode.