English 中文(简体)
Clojure emacs slime + swank directory question
原标题:

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.

最佳回答

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. :-)

问题回答

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.

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))

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.





相关问题
Is there assembler REPL under linux?

Recently I ve started plaing with assembler under linux, there s good debuger, but comming from Ruby I m missing simple REPL that would let me enter a line of assembler code and see the result on ...

C# REPL tools; quick console-like compiling tool

Often times, I start a new instance of Visual Studio, just to create a console application that has some output and/or input. It s a temporary sandbox I use to test a method or something else and ...

How to play with Specs matchers in Scala REPL?

While debugging or exploring spec features it would be more advantageous to type them in REPL (Scala interpreter) rather then in file with spec and run it with something like maven. What is the ...

octave: load many functions from single file

How can I put multiple functions in one file and later get access to all of them in the octave interpreter ? I don t want to have a thousand files and want to group functions together. I d like ...

Clojure emacs slime + swank directory question

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 ...

scala: tracing implicits selection and other code magics

When trying to figure how a library works, implicit conversions are confusing. For example, looking at an expression like val foo: Foo = 1 , what converts 1 to Foo? Is it possible to instruct the ...

热门标签