Wrapper function for slime

2 views
Skip to first unread message

Craig McDaniel

unread,
Apr 20, 2009, 4:07:38 PM4/20/09
to Clojure
Using something like this run-slime wrapper to start slime may be
useful to others. It helps me avoid some issues when moving from
project to project without restarting emacs. Assuming jar files reside
in each separate project's own "lib" directory, Clojure source in its
"src" directory, and compiled classes in its "classes" directory, run-
slime does the following:

- changes the classpath used by swank-clojure for each project. This
was tricky because simply setting swank-clojure-extra-classpaths and
restarting slime won't do it. The contents of the slime-lisp-
implementations list must be changed.

- makes sure AOT compiling works by creating the "classes" directory
and setting the classpath to include both the src and classes
directories. Even if you specify the "classes" dir in the classpath,
it won't work unless the directory exists in advance of starting
slime.

- also sets the classpath to include all files in the lib
subdirectory. (Note that the lib/* classpath syntax won't work on Java
5.)

(defun reset-swank ()
"Because changing swank-clojure-extra-classpaths is not enough
to force a new instance of slime to use it."
(interactive)
(assq-delete-all 'clojure slime-lisp-implementations)
(add-to-list 'slime-lisp-implementations
`(clojure ,(swank-clojure-cmd) :init swank-clojure-
init) t))

(defun run-slime (dir)
(interactive "DProject directory: ")
(cd dir)
(when (not (file-directory-p "classes"))
(make-directory "classes"))
(setq swank-clojure-extra-classpaths '("src" "classes" "lib/*"))
(reset-swank)
(slime))

Craig McDaniel

unread,
Apr 21, 2009, 5:51:19 PM4/21/09
to Clojure
Correction:

(defun reset-swank ()
"Because changing swank-clojure-extra-classpaths is not enough
to force a new instance of slime to use it."
(interactive)
(setq slime-lisp-implementations
(assq-delete-all 'clojure slime-lisp-implementations))

Phil Hagelberg

unread,
Apr 21, 2009, 7:38:16 PM4/21/09
to clo...@googlegroups.com
Craig McDaniel <crai...@gmail.com> writes:

> Correction:
>
> (defun reset-swank ()
>
> (defun run-slime (dir)

Here's what I use: (plagiarized from Tim Dysinger)

(defun slime-project (path)
"Setup classpaths for a maven/clojure project & refresh slime"
(interactive "GPath: ")
(let ((original-dir default-dir))
(cd path)
(setq swank-clojure-binary nil)
(setq swank-clojure-jar-path "target/dependency")
(setq swank-clojure-extra-classpaths '("src" "target/classes" "target/dependency"))
(setq swank-clojure-extra-vm-args '("-Dclojure.compile.path=target/classes"))
(setq slime-lisp-implementations
(cons `(clojure ,(swank-clojure-cmd) :init swank-clojure-init)
(remove-if #'(lambda (x) (eq (car x) 'clojure))
slime-lisp-implementations)))
(cd original-dir)))

This uses maven's bytecode target directory conventions. It assumes
clojure, contrib, etc are unpacked in target/dependency. This has the
additional benefit of not requiring you to restart SLIME when you add a
new dependency, just unpack it in the right place and your classpath
stays the same. Very handy.

-Phil

Craig McDaniel

unread,
Apr 22, 2009, 9:51:38 AM4/22/09
to Clojure
Thanks Phil. I'll try it out. It's about time I started learning about
Maven anyway.

Craig McDaniel

unread,
Apr 22, 2009, 10:53:51 AM4/22/09
to Clojure
Phil, that's useful advice about unpacking jar files to a project's
dependency directory versus dropping jar files in there. That would be
a good candidate for a FAQ regarding how to add dependent jars during
development without restarting your REPL. And it decreases questions
about the deprecated add-classpath function.

-Craig
Reply all
Reply to author
Forward
0 new messages