Thursday, May 5, 2011

Clojure in Emacs

This is a brief guide to setting up and hacking Clojure in Emacs.

1. Install Emacs.
Install Emacs23 if you're on a Linux and Carbon Emacs if you're on OSX. Aquamacs is discouraged due to issues with Emacs starter kit.

2. Tutorial
Take an hour and work through the tutorial to get comfortable with Emacs shortcuts. It'll be the most productive hour you ever invested. Grab a cheatsheet and get used to it by coding away in your favorite language.

3. Get the Emacs starter kit. It comes with major/minor modes for most languages.

If you're on OSX, Grab this version by topfunky for Carbon Emacs:
https://github.com/topfunky/emacs-starter-kit

If you're on Linux, clone it from the original fork here by technomancy:
https://github.com/technomancy/emacs-starter-kit

After cloning the started kit, make it your ~/.emacs.d/ folder

4. Install Emacs packages for Clojure

4.1 M-x package-list-packages
to list available packages
(If this doesn't work, your Emacs starter kit isn't working)

4.2 Select the following packages by pressing I to mark for installation
clojure-mode
clojure-test-mode
slime
slime-repl

4.3 Press X to install selected packages

5. Inferior mode, SLIME and SWANK

A small digression on Inferior mode, Superior mode and Swank.
Emacs is a text editor which can be extended using EmacsLisp.
Inferior mode is how you eval snippet of code directly from editor in a Lisp (configured in inferior-lisp-program by the major mode you currently are in).
SLIME or Superior Lisp Interaction mode for Emacs is how you start a REPL in Emacs.
Emacs being just a text editor can't really eval Lisp code unless it's Emacs lisp. The way inferior mode and superior mode really work is through Swank which is sort of a REPL server to which Emacs connects.

5.1 To eval an expression in inferior-mode, just move to the end of a Clojure expression and press C-x C-e.

5.2 To start Swank server for Clojure REPL, you need to install Swank-Clojure:

5.2.1 Install Leiningen (https://github.com/technomancy/leiningen)
Leiningen is a build tool for Clojure. Leiningen can be used to install Swank clojure as a plugin and start the Swank server for Emacs

To install Lein, Download the script, make it executable and run it

$ wget https://github.com/technomancy/leiningen/raw/stable/bin/lein
$ sudo cp lein /usr/bin
$ sudo chmod +x /usr/bin/lein
$ lein

5.2.2 Install Swank clojure (https://github.com/technomancy/swank-clojure)
After installing Lein, Install Swank clojure as a plugin:

$ lein plugin install swank-clojure 1.3.0

Lein installs plugins in ~/.lein/bin. swank-clojure startup script is created there.
Add ~/.lein/bin to your path

5.2.3 Start Swank server and connect from Emacs

$ swank-clojure
starts Swank server for Clojure REPL

Start Clojure REPL in Emacs by connecting to Swank server. Accept defaults for host and port
M-x slime-connect

Now ogle at the most elegant editor you'll ever see and hack away in the most elegant language you'll ever write code in.

(Reference: http://technomancy.us/126)

2 comments:

  1. Nice write up. Thanks.

    Although Mac OSX users can get the GNU build of Emacs (rather than Carbon) here

    http://emacsformacosx.com/

    Then you can use technomancy's emacs-starter-kit as you would on Linux.

    ReplyDelete
  2. I'd actually strongly recommend against Carbon Emacs; it is over four years old now. Even on Mac OS X, you should be running Emacs 23 or 24. The builds at http://emacsformacosx.com seem to be pretty good.

    ReplyDelete