views:

1277

answers:

4

I'm using emacs with clojure-swank and slime and trying to set my development environment. And I ran into a problem. When I start a repl I'm stuck in an unknown directory preventing me to load my namespace. Because the clojure repl can't find the right file.

Does anyone know how to change the current directory?

PS: I've just started using emacs and slime so I'm a noob.

+4  A: 

Short answer:
(load-file "full-path-to-definition")

Long answer: Here's what my bootstrapping process looks like:

In ~/.clojure/user.clj (this file is auto-run when you boot slime/clojure):
(add-classpath "file://path/to/foo.jar") ; Include these jars in the classpath
(add-classpath "file://path/to/foo2.jar")
(load-file "file://workspace/bootstrap.clj")

In bootstrap.clj:
(compile 'my.package)

Package file(s) is at /workspace/my/package.clj

In package.clj:
(ns my.package)
(defn foo [] (+ 2 2))

Greg Harman
+6  A: 

If you want to change slime's notion of the current directory, press ,cd<CR> (<CR> = Enter) and type in the path.

However, this is not really the proper solution to the problem. The proper solution involves setting up the classpath so that you can (use 'your.namespace). To this end, I wonder if this very long answer I provided to a question about setting up the classpath properly might be helpful... :-)

Incidentally, I somewhat object to solutions involving add-classpath, as that is currently marked as deprecated and was never meant to be relied upon in the first place... Though on the other hand, it certainly may work perfectly well and it's worth knowing about just in case it might come in handy as a quick-and-dirty classpath injection gimmick.

Now if you want a real nice SLIME-based development environment, I'd like to point you to a very nice clojure-project elisp function by Phil Hagelberg which sets up all relevant variables and launches SLIME in the main directory of a project (to be supplied interactively). It's been posted to the Clojure group, in fact here's a link to the Mail Archive's copy of that message. Note there's one thing which needs correction in there -- swank-clojure-jar-path ought to be set to the full path to clojure.jar. Otherwise it's a fantastic tool.

Actually I mentioned that function in this response to a question about managing the classpath when using Clojure and Emacs. The other answers might be interesting as well.

And if you're only just beginning to use SLIME, do watch the SLIME video, linked to from SLIME's homepage which is now available under a link posted by Michiel in the comments. It's a very good introduction. :-)

Michał Marczyk
I'm going to try the clojure-project function solution. But in which file do I add the code?
MrHus
The link to the video from the SLIME homepage is dead. The video still can be found here: http://www.guba.com/watch/3000054867
Michiel Borkent
Oh, I never noticed... Thanks, Michiel. I'll edit accordingly. @MrHus: You need to add it to some file which gets loaded when you start Emacs, like `~/.emacs` (on *nix) / `_emacs` (on Windows; I believe you put this in your profile's directory, e.g. `C:\Documents and Settings\yourusername\_emacs`, but I'm entirely sure, google around if this won't work). Just remember to fix `swank-clojure-jar-path`. Incidentally, judging by your asking this question, you might do well to look up Phil Hagelberg's `emacs-starter-kit`; there's also a related screencast on http://peepcode.com/.
Michał Marczyk
+3  A: 

Leiningen is a new Clojure build tool that worries about classpathing for you. You set up a simple project file in the project's root directory to specify the main class of your project, and it automagically discovers all the JARs in your lib directory and loads them for you.

I now just type "lein swank" at a command line and then M-x slime-connect in Emacs, and everything just works. (This could easily be automated with a little Elisp.)

More info in this blog post.

Paul Legato
This is actually a really good solution. Now I can keep working with Textmate. Thanks.
MrHus
+1  A: 

The best approach I've found when using Emacs, SLIME and swank-clojure is to use the (Emacs Lisp) function swank-clojure-project. From its documentation:

(swank-clojure-project PATH)

Setup classpath for a clojure project and starts a new SLIME session. Kills existing SLIME session, if any.

If you do a "M-x swank-clojure-project", it will interactively prompt you for your project directory; once you've selected it, all the jars in a lib subdirectory, as well as the src and classes folder will be added to your classpath. It will also honor a Maven/lein directory structure, in other words: it will usually just work.

If you change something, e.g. add a new jar file, simply do swank-clojure-project again.

Stefan Tilkov