Emacs is the favored development environment for the majority of Clojure developers, and there are good reasons for that, but personally, I don’t think it should be the first choice of developers new to Clojure, unless they have used it previously; it’s just too much to learn at once.
I recommend people use an editor they’re comfortable with, combined with a command-line REPL. There is no reason to tackle the complexities of configuring and using Emacs, Slime, and Swank until you’ve got your head around the basics of Clojure and functional programming. Once you’ve got the basics down though, it’s worth venturing into the arcane world of Emacs. You may decide it’s not for you, and luckily there are alternatives, from your favorite editor combined with a REPL to plugins for popular IDEs like Netbeans (Enclojure), IntelliJ (La Clojure), and Eclipse (Counter-Clockwise).
But you’ll never know if it’s for you unless you give it a try. So, I’ll be demonstrating how to build and install Incanter (which includes Clojure and Clojure-contrib), and then set up a development environment with Emacs, Slime, Swank, and Paredit.
Setting up Clojure and Incanter
Incanter is available on Clojars, so you can include it in your projects by adding it as a dependency in your project.clj file (see below). Alternatively, you can clone the full distribution from Github, which includes REPL and Swank start up scripts. Note: The repl scripts are necessary, since Leiningen’s repl task does not work correctly with Clojure 1.2.
The following examples will assume you cloned Incanter’s github repository (see below for instructions) and have the repl and swank scripts included with the distribution.
First, you’ll need Git and Leiningen to grab and build Incanter. First clone Incanter from its Github repository:
$ git clone git://github.com/liebke/incanter.git
This will create an incanter subdirectory
$ cd incanter
Use lein deps to downloads the necessary dependencies:
$ lein deps
Once this process is complete, you can start a Clojure REPL with all of Incanter’s dependencies pre-configured on the CLASSPATH by either using the repl scripts included in the script/ directory.
or on Windows,
or you can start it directly with the java command:
$ java -cp 'lib/*' clojure.main
This will present you with the
user=> prompt. As a simple example of using Incanter from the REPL, we’ll generate a line plot of the sine function over the range -4 to 4, first load the necessary Incanter libraries:
user=> (use '(incanter core charts))
and then use the
user=> (view (function-plot sin -4 4))
Now that we know Incanter and Clojure are installed correctly, let’s set up an Emacs development environment.
Setting up and using Emacs, Swank, Slime, and Paredit
I’m a long time vi/vim user and I typically use MacVim, but I have recently gone back to Emacs (the editor I used when I first learned Lisp) in order to take advantage of Slime, Swank, and Paredit. Doing most of my development on a Macbook, I like Aquamacs, which blends standard OS X and Emacs behaviors. Another nice option on the Mac is Carbon Emacs.
The procedure I’m going to use to setup the Emacs development environment is based on the instructions provided by Phil Hagelberg (a.k.a Technomancy) in this blog post and in the README for his fork of swank-clojure.
The best way to install the necessary packages (clojure-mode, slime, slime-repl, swank-clojure) is by using the Emacs Lisp Package Archive, or ELPA.
To access ELPA, use the following command:
The meta-key on the Mac for most flavors of Emacs is the command key, but with Aquamacs it’s the alt/option key.
If the ‘package-list-packages’ command cannot be found, you’ll need to paste the following snippet of elisp in your *scratch* buffer and then evaluate it, (go here for more detailed instructions).
(let ((buffer (url-retrieve-synchronously "http://tromey.com/elpa/package-install.el"))) (save-excursion (set-buffer buffer) (goto-char (point-min)) (re-search-forward "^$" nil 'move) (eval-region (point) (point-max)) (kill-buffer (current-buffer))))
In Aquamacs, you’ll evaluate it by placing your cursor right after the last parentheses and entering:
On most other version of Emacs, including Carbon Emacs, you’ll enter
Once this has been done, you should be able access ELPA with:
You’ll see a list of packages, either scroll down to find or search for, using
C-s, the following packages:
When you’re cursor is on the appropriate package, hit the i key to select it. Once all the packages are selected, hit x to begin their installation. When it’s complete, you might see some warnings, but don’t worry about them.
Slime is an Emacs-mode for editing Lisp/Clojure code and Swank is a back-end service that Slime connects to, letting you evaluate Clojure code from within Emacs. Paredit provides some additional Clojure/Lisp editing functionality, although, like Emacs, it requires some getting used to (see mudphone’s introduction to Paredit presentation and the Paredit cheat sheet).
Now it’s time to start up a Swank server that will let us run Clojure code from Emacs. We can use Leiningen to start one up that is pre-configured with all of Incanter’s dependencies with the
script/swank script, or by running the following Leiningen command Incanter directory:
$ lein swank
This will generate some messages, ending with
Connection opened on local port 4005 #<ServerSocket ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=4005]>
Now we need to connect to the server from Emacs with the following command:
It will prompt you for the IP address and port of the server, just use the defaults it offers. It may then show the following prompt:
Versions differ: nil (slime) vs. 2009-09-14 (swank). Continue? (y or n)
Just say ‘yes’. You will then get a message confirming you’re connected, and a window will open with a Clojure REPL and a ‘user>’ prompt. A cool feature of
slime-connect is that you can connect to a swank server on a remote system, just provide the system’s IP address or host name, instead of the default 127.0.0.1, when prompted.
Now open or create a Clojure file, using ‘C-x C-f’ (or using ‘command-o’ or ‘command-n’ in Aquamacs). If you’re creating a new file, give it a
*.clj suffix and Emacs will start clojure-mode automatically.
Now start up Paredit,
You’re now ready to edit Clojure code. Start by loading a few Incanter libraries with the following line:
(use '(incanter core stats charts))
You’ll notice that closing parens are automatically created when you create an opening paren, this is due to Paredit. You can evaluate this block of code by placing your cursor right after the last paren, and entering ‘C-x C-e’. You should see the return value, nil, in the Emacs message pane.
Now let’s generate a plot of the PDF of the Normal distribution, over the range -3 to 3, by entering and evaluating the following line:
(view (function-plot pdf-normal -3 3))
That’s it, you’re all set up. Have fun!