Getting started with Clojure/Emacs/Slime May 4th, 2010
Patrick Stein

I spent some considerable time yesterday poring over the shelves in the programmer’s section of a local bookstore yesterday. Based on the available jobs at the moment, I was trying to decide whether it would be less painful to learn C#/.NET/AFW/blurpz or Hibernate/Springs/Struts/glorpka. My lambda, those things are fugly. When I open a book to find that my simple database example takes eight XML configuration files and twenty-five lines of calls to the same function (with a 25-character identifier (which, technically, should be namespace qualified, too)), I just don’t want to go there.

So, I walked away with Programming Clojure and a determination to think really hard about how to get paid to do something that’s not intensely painful.

Well, yesterday afternoon and late-night were intensely painful trying to get Clojure/Emacs/Slime all working together. Today, magickly, I messed something up in my .emacs file that convinced swank-clojure to download its own copies of the three JAR files it needs and zoom… I’m out of the gate.

Someday, I’d still like to be able to use my own JAR files for all of this, but in the meantime, I’m up and running.

Here’s what works

This is the relevant configuration from my .emacs file. It draws partly from these instructions by I’m not sure who, partly from this message by Constantine Vetoshev, partly from how my .emacs file was previously arranged, partly from sources now lost in the browser history sea, and partly from sheer luck.

First, some generic stuff up at the beginning:

(defun add-subdirs-to-load-path (dir)
  (let ((default-directory (concat dir "/")))
    (normal-top-level-add-subdirs-to-load-path)))

(add-to-list 'load-path "~/.emacs.d/site-lisp")
(add-subdirs-to-load-path "~/.emacs.d/site-lisp")

Then, prepping slime a bit:

(require 'slime-autoloads)
(add-to-list 'load-path "~/.emacs.d/site-lisp/slime/contrib")

(add-hook 'lisp-mode-hook (lambda () (slime-mode t)))
(add-hook 'inferior-lisp-mode-hook (lambda () (inferior-slime-mode t)))
(setq common-lisp-hyperspec-root
      "file:///Developer/Documentation/Lisp/clhs/HyperSpec/")

(slime-setup '(slime-repl))

(setq slime-net-coding-system 'utf-8-unix)

Then, setting up some general stuff for easy lisp implementations. (The –sbcl-nolineedit is something I personally use in my .sbclrc to decide whether to load linedit.)

(setq slime-lisp-implementations
      '((sbcl ("sbcl" "--sbcl-nolineedit"))
        (ccl ("ccl"))
        (ccl64 ("ccl64"))))

Some commands to simplify things so I don’t have to remember to M–– M-x slime:

(defmacro defslime-start (name mapping)
  `(defun ,name ()
     (interactive)
     (let ((slime-default-lisp ,mapping))
       (slime))))

(defslime-start ccl 'ccl)
(defslime-start ccl64 'ccl64)
(defslime-start clojure 'clojure)
(defslime-start sbcl 'sbcl)

Then, Clojure-specific SLIME stuff

(autoload 'clojure-mode "clojure-mode" "A major mode for Clojure" t)
(add-to-list 'auto-mode-alist '("\\.clj$" . clojure-mode))
(require 'swank-clojure)

(setq slime-lisp-implementations
      (append slime-lisp-implementations
              `((clojure ,(swank-clojure-cmd) :init swank-clojure-init))))

And, a touch more slime stuff to make things a little happier.

(add-hook 'slime-mode-hook
          (lambda ()
            (setq slime-truncate-lines nil)
            (slime-redirect-inferior-output)))

In my .emacs.d/site-lisp, I did the following:

% rm -rf slime swank-clojure clojure-mode
% git clone git://git.boinkor.net/slime.git
% git clone http://github.com/technomancy/swank-clojure.git
% git clone http://github.com/jochu/clojure-mode.git

What didn’t work

Before accidentally triggering swank-clojure to download its own JARs, I tried installing what I could with ELPA. I tried installing clojure, clojure-contrib, and swank-clojure with Lein. I tried installing them with Maven. I tried various combinations of versions of clojure and swank-clojure.

I have no idea how the JARs that swank-clojure built itself got built. I cannot reproduce it.

Edit: Ah, it appears that the Subversion repository for Clojure that I found is deprecated. But, I don’t have the energy to try the git repository myself at this point. Maybe next week.

10 Responses to “Getting started with Clojure/Emacs/Slime”

  1. Charles Neveu
    2010-05-04 @ 11:31 AM

    Two words: Clojure Box. Took me 2 minutes. Works out of the box.

    • pat
      2010-05-04 @ 11:44 AM

      Clojure-Box is for Windows. I’m on a Mac. Further, I already have Emacs, Slime, paredit, and clojure-mode installed and running. I already had Clojure running. I just wanted to make it go with swank-clojure.

      There are a variety of “out-of-the-box” and “pre-packaged” solutions for lots of things that I want to do. I generally don’t want six copies of everything on my machine though (especially when “everything” includes Emacs and a Lisp implementation), so I prefer to steer clear of those things when I can… or stick to only one “pre-package” system. 1/2 the point of package management is lost if I need five separate copies of libX11.dylib and eight copies of freetype.h on my machine.

  2. 2010-05-04 @ 9:15 PM

    Sorry to hear the bad experience. I had a similar experience until I discovered Incanter [1], the numerical/scientific computing package that is built on Clojure. It has the only sane Clojure getting-started installation procedure I could find [2] for someone using emacs/slime.

    [1] http://incanter.org/
    [2] http://wiki.github.com/liebke/incanter/#getstarted

    Best wishes.

  3. Terry Glass
    2010-05-30 @ 7:47 PM

    Thank you for this. Until now, I’ve been forced to use inferior lisp mode for either clojure or common lisp after they started adding all of the newbie friendly automagic install stuff to clojure mode.

  4. Kevin
    2010-06-04 @ 4:33 PM

    Thanks! I had the same problem a few months ago and I gave up by commenting the clojure elisp code out when I worked with SBCL and the SBCL elisp code when I worked with Clojure. Now I don’t need to do that 😀

  5. 2010-07-15 @ 6:51 AM

    I posted about setting up Emacs for Clojure development here:

    http://charsequence.blogspot.com/2010/07/setup-emacs-for-development-with.html

  6. Robert Goldman
    2010-11-11 @ 12:52 PM

    Patrick — do these directions still work? I was just trying to follow this, and in the middle of the readme for for swank-clojure I read:

    Previous versions of Swank Clojure bundled an Elisp library called
    swank-clojure.el that provided ways to launch your swank server from
    within your Emacs process. While swank-clojure is still distributed
    with the project, it’s a much more error-prone way of doing things
    than the method outlined above.

    where “the method outlined above” was some stuff about Leiningen and Maven that was just Greek to me, since I am just trying to get started with Clojure, and want a pretty much vanilla Clojure REPL to work with.

    This seems like “just figure out all about ASDF and then you can start your lisp environment…”

    • pat
      2010-11-11 @ 1:06 PM

      I fought with Leiningen and Maven for way too long back in May. I haven’t downloaded any Clojure or emacs-specific Clojure stuff since then. My REPL for Clojure still works with precompiled JARs that it fetched itself.

      I hope the above still works, or I just won’t ever upgrade.

  7. Robert Goldman
    2010-11-11 @ 10:25 PM

    OK, looks like the right solution is to get leiningen from github, then use leiningen to install swank-clojure and start swank-clojure from the terminal. Then use slime-connect to hook up. No longer are we to get our own copy of swank-clojure in el and install it ourselves.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <br> <cite> <code> <dd> <del datetime=""> <dl> <dt> <em> <i> <img alt="" height="" longdesc="" src="" width=""> <ins datetime="" cite=""> <li> <ol> <p> <q cite=""> <s> <strike> <strong> <sub> <sup> <u> <ul>

l