This post is part 1 of a series (next). The full code is available on GitHub.

There was recently a thread on the OCaml Discourse forum about sample Emacs plugins using the Ecaml library. Since the library doesn’t seem to have been getting that much use, and since I have some personal interest in seeing it used more, I decided to help by writing a Getting Started guide. Here goes.

This is part 1 of a series (next). We’ll see why you might use Ecaml to write a plugin, and get it set up and running with a simple Hello World. All of the code for this series is available on GitHub.

(This guide has only been tested on macOS 10.12, but probably works on other Unices like Linux or OS X. Unfortunately I have no idea about Windows.)

1 Emacs Emacs is a great text editor operating system habitat. Emacs’s power and popularity arguably stem from its ability to be endlessly customized and enhanced by plugins (i.e., extensions, packages, modules)—for example, I’m writing this post in org-mode while editing its YAML metadata (in the same file) in yaml-mode . These two editing modes are able to play nicely in the same buffer thanks to a package called mmm-mode (“multiple major modes”… mode). In this series, we’re going to write an interpreter for a minimalist Turing-complete programming language. We want to be able to run the code we’re editing directly within Emacs, instead of switching to our shell and running it from there, so we’ll embed our interpreter as a plugin in Emacs. 1.1 Elisp In the past, Emacs plugins have all been written in a language called Emacs Lisp (or Elisp), which, as its name suggests, is a Lisp. Elisp is a perfectly nice language, especially for writing the sort of code you might find in a plugin, but it has a couple of drawbacks. The main one I’m interested in is speed. Ya see, Emacs’s Lisp interpreter ain’t exactly the fastest. A naïve speed comparison between Elisp and Ruby, for dramatic effect: ( benchmark-run ( print ( loop for i below 1000000 sum i ) ) ) time ruby -e 'p (0...1000000).reduce(&:+)' Both of these snippets just iterate from 0 to 999,999, adding up The Elisp code takes quite a bit longer to add up the natural numbers less than one million, and that’s including the startup time of the ruby interpreter! Now, to be fair, when compiled, the above Elisp code only takes around 0.125 seconds, but that’s still substantially slower than compiled languages like C or Java (or OCaml). 1.2 Native Plugins Emacs 25 added the ability to load native code plugins. Native code refers to code that runs directly on the processor, instead of being interpreted by another program, like Emacs’s Lisp interpreter or Ruby’s virtual machine. There’s quite a good introduction to the basics of writing native plugins here, but the gist of it is that Emacs exposes a small C API which you can write modules against. Compiling them to shared objects (i.e., dynamically linked libraries, .so ’s, .dll ’s, etc.) allows Emacs to dynamically load the plugin at runtime.

2 OCaml plugins? OCaml is a statically-typed functional programming language with a great deal of versatility, contrary to what you might have first expected when you just read the words “statically-typed” and “functional”. Of course, since you’re reading this blog post, I assume you are familiar with OCaml and won’t belabor singing its praises. I’ll just point out that OCaml has a quite nice way to interface with C code using its foreign function interface (FFI), which means we can use OCaml to write native plugins for Emacs, as long as we have some library to provide the interface between the Emacs C API and our OCaml plugin. That’s where Ecaml comes in.