Clojure with Emacs and Slime/Swank on Ubuntu

Alternatives

Phil Hagleberg has created some tools which will not only help with the topic described in this tutorial but can also help with emacs configuration in general. His tutorial on the clojure-specific piece is located here: in which are found tricks of the trade concerning clojure authorship.

Prerequisites

This tutorial assumes that the following tutorials have been successfully completed:

Install clojure-mode

clojure-mode is a emacs mode to handle clojure files.

~$ cd ~/opt 

~$ git clone git://github.com/jochu/clojure-mode.git 

Install Slime

Install Superior Lisp Interation Mode for Emacs.

Slime Homepage: http://common-lisp.net/project/slime

~$ cd ~/opt 

~$ git clone git://git.boinkor.net/slime.git 

Install swank-clojure

swank-clojure, the server-side of slime, provides a server-based clojure REPL.

~$ cd ~/opt

~$ git clone git://github.com/jochu/swank-clojure.git 

Configure Emacs

Add these specifics to the .emacs file.

;; clojure-mode
(add-to-list 'load-path "~/opt/clojure-mode")
(require 'clojure-mode)

;; swank-clojure
(add-to-list 'load-path "~/opt/swank-clojure/src/emacs")

(setq swank-clojure-jar-path "~/.clojure/clojure.jar"
      swank-clojure-extra-classpaths (list
				      "~/opt/swank-clojure/src/main/clojure"
				      "~/.clojure/clojure-contrib.jar"))

(require 'swank-clojure-autoload)

;; slime
(eval-after-load "slime" 
  '(progn (slime-setup '(slime-repl))))

(add-to-list 'load-path "~/opt/slime")
(require 'slime)
(slime-setup) 

Test Configuration

Download ants.clj from http://clojure.googlegroups.com/web/ants.clj

Save ants.clj to your home directory.

Open emacs and load the file. Emacs key combinations are written like M-x and C-x where M stands for Meta (which is the Alt key on many keyboards) and C stands for Control (normally labeled Crtl.)

~$ emacs

### Open the ants file

C-x

C-f

ants.clj

Split the screen horizontally and start slime.

C-x 2

### Click with the mouse on the bottom buffer to select it.

M-x slime

Back in the ants.clj file, page down to the ;; use ;; section. Compile and load the file with slime. This will also launch a new application window.

C-c 

C-k

Now, interactively execute the last four lines of the file. With the cursor after the line (def ants (setup)), do this:

C-x

C-e

Execute the last three lines using the same key combination.

Ants marching ;-)