James Williams
LinkedInMastodonGithub

Groovy Support in Org Mode Babel

Tags: Groovy

Org Mode is a set of Emacs plugins that function as a sort of catch-all for outlining, organization, agenda/calendar planning, time tracking, and more. Over the past week, I've switched from raw LaTeX to using OrgMode for some work that's has been scientific equation heavy. I'll leave the hows and whys of that for another blog post.

One of the more impressive features of Org Mode is Babel. It's a mode that allows you to include source code inside a org document. Babel allows you to run the code and inject the results back into the document. Babel is a no nonsense way to do Literate Programming. Literate Programming is a concept championed by Donald Knuth that stresses programs should be written firstly for people and secondarily for machines. Instead of separating the documentation into separate files from the source where they can likely get stale. Literate programs combine documentation and source code into the same set of files. From those files, one can generate a set for documentation purposes removing some or all of the corresponding code from it and a set of "tangled" code files used for program execution.

Journo from CoffeeScript creator Jeremy Askenas demonstrates some of these concepts. Literate programming has not taken off in the traditional programming community but has drawn a following in the scientific community. One can create a document that contains the code to generate all its figures and graphs. This makes the work reproducible and more maintainable. Adding a dataset that changes how your graphs plot? Just add them to your org file and re-export your LaTeX or PDF. Babel supports a lot of languages. Unfortunately, Groovy isn't one of them. Wiring in Groovy support was dead simple thanks to similar compilation strings for Java and Groovy code. All I had to do was execute a mass find/replace from java/Java to groovy/Groovy.

Below is a small example showing the code to embed a executable Groovy snippet in a document.

#+begin_src groovy :classname Sample :results output :session
println 'Hello World'
#+end_src

It can be run with Ctrl-c Ctrl-c.

You can read more about Org mode or Babel at the included links. Check out this Youtube video to see Babel in action.