Emacs with Slime and Swank for non-leiningen projects - emacs

I found this website which explains how to use emacs with leiningen, swank, and slime. Is there a way to use slime + swank in non-leiningen projects i.e. how can I connect to slime/swank repl to run a ad-hoc Clojure script while I write it as demonstrated here?

You need to have swank-clojure.jar in CLASSPATH and your script should have following code:
(require 'swank.swank)
(swank.swank/start-repl 4005)
to start swank process on port 4005 (or some other)...
P.S. You can look onto Incanter's swank script, that pass this code in environment variable, and later it evaluated as part of boostrap script

Related

Clojure - start a REPL without a project.clj

I started using Clojure with leiningen (and now boot).
Now I sometimes want to get quickly to a Clojure{Script} CIDER REPL in Emacs to execute just a few instructions. I don't want to create a project.clj file for that, since I just want a throwaway REPL.
Is there a way to get a Clojure REPL, for instance in the *scratch* buffer ?
well, you can execute M-x cider-jack-in anywhere you want, even with no project.clj in path. This works for me.
You can just type lein repl in the friendly console / shell / terminal right next to you -- no project.clj required. This will start a REPL as expected, to which you can then connect from Emacs via M-x cider-connect (which in recent versions will handily suggest host and port to connect to).
M-x cider-jack-in basically does the same thing (i.e. lein repl) behind the scenes.
I'm not a boot user, but according to the boot wiki for leiningen users it should be possible to call boot repl -s.
Use M-x cider-jack-in, and if you don't want it to warn you that you're running cider-jack-in without a Clojure project, add the following to your emacs.d/init.el:
(setq cider-allow-jack-in-without-project t)

What does the 'swank-clojure' do exactly, and do we have 'swank-SOMETHING_ELSE'?

My superficial understanding is that 'swank-clojure' makes 'M-x slime-connect' possible. I mean, it gives a connection to a clojure server something like 'lein swank'. Is my understanding correct? If not, what's the purpose of swank?
Then, is there any 'swank-SOMETHING_ELSE' for other lisp like implementations? For example, swank-clisp?
Do I need 'swank-clojure' for using SLIME/Clojure with 'M-x slime'?
ADDED
I found this link pretty useful.
SLIME and swank form a client server architecture to run and debug lisp programs. SLIME is the emacs frontend and swank is the backend. In between they create a network socket and communicate by sending across messages (S-expressions). In short it is just an RPC mechanism between emacs and the actual lisp backend.
The fact that the slime and swank are separate, are connected over a network and communicate via rpc messages means that they can be anywhere. So, slime can connect to a remote host/port to swank. All other forms you see (lein swank etc etc) do the same. They start swank on a port allowing for a remote connection of slime.
swank-clojure is the clojure port of swank. originally swank-clojure came with a helper elisp file called swank-clojure.el. The job of this file was to enable manual setup of swank parameters like the classpaths, jvm parameters etc. Since other tools like lein came along later, swank-clojure.el was deprecated. But it still lives on at: http://github.com/vu3rdd/swank-clojure-extra and provides the M-x swank-clojure-project which enables starting swank on a lein project.
It should be noted that SLIME originated in (and is still being actively developed for) Common Lisp. Infact, the clojure port of swank only has a subset of the features enjoyed by the original SLIME/swank versions. SLIME exists for all major variants of Common Lisp. There is a partial port of it for Scheme48. There are some partial implementations available under the contrib directory.
If you know that swank is already running on a port, use slime-connect. If you just want to use slime on a project, swank-clojure-project and lein swank seem to be the way to go.
swank-clojure.el is deprecated. Don't use it.
You need slime.el and you need to have swank-clojure "1.2.1" in your dev-dependcies in your project.clj file.
Swank is basically a server that you use slime to connect to from emacs. It it passed, from emacs, what you want run by the Lisp process that it's running.
You should use M-x slime-connect to connect to a swank server after starting with with lein swank.
Swank is the server counterpart to swank clients like emacs SLIME and the MCLIDE lisp development environment for Macintosh. Swank servers exist for many Common Lisp implementations and Lisp dialects such as Clojure and Gambit/scheme.
My understanding is that slime is the emacs part(the client), swank is the common lisp part(the server), swank-clojure is the clojure implementation of the swank server, not the original.

Why clojure starts with 'M-x slime' for my Aquamacs without any setup?

I have Aquamacs running on my Mac.
I installed the ESK for Aquamacs and it installs ELPA at ~/.emacs.d/elpa. With ELPA, I installed clojure-mode/clojure-est-mode/slime/slime-repl/swank-clojure.
The thing is that when I run 'M-x slime', clojure is run. Why is this? I'm curious as I don't have any setup for slime in my .emacs and even in /Users/smcho/Library/Preferences/Aquamacs Emacs/{Preferences.el, customizations.el}. I mean, I don't have (setq inferior-lisp-program "clojure") or something.
Or, does it have to do with my 'swank-clojure' installation? I see something like this at the inferior-lisp buffer.
(require 'swank.swank)
(swank.swank/ignore-protocol-version nil)
...
#ServerSocket ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=51847]
That's because of swank-clojure.el, a deprecated elisp program. Don't use it, as per my post here, on your next question. It may have bugs and thus isn't suitable to use. Use M-x slime-connect instead, after starting swank up with lein swank.
As Isaac said, swank-clojure.el sets 'slime-lisp-implementations' for clojure (in short, a way to start java with swank-clojure in classpath so that swank can be started). Then slime connects to this port and you get the repl.
The problem with this approach is that, you will need to setup classpath upfront. This gets tedious for each and every project you want to work on. So, best way is to use M-x swank-clojure-project or lein swank and then M-x slime-connect. I prefer swank-clojure-project.

Clojure 1.2 is released! How to get swank-clojure to use this?

I just set up a new emacs installation, installed ELPA and installed swank-clojure with using ELPA. Although Clojure 1.2 is released, when I hit "M-x slime", it still downloaded Clojure 1.1 for me instead of 1.2
How do I get it to use 1.2 now that it's available? Do I have to do it manually? Do I have to wait for slime/swank to be updated to use 1.2?
Update: Thanks for the recommendations for using leiningen, but what I'm after is for emacs to just launch a REPL itself. Is this problem happening because the swank-clojure package in ELPA is not being updated?
With Leiningen 1.3, it's easy to create a standalone swank session:
$ lein install swank-clojure 1.3.0-SNAPSHOT
$ ~/.lein/bin/swank-clojure
You can add ~/.lein/bin to your $PATH to make this easier.
Then inside Emacs:
M-x slime-connect
Launching swank from inside Emacs can be done with M-x lein-swank if you are inside a project directory. However, auto-download and install of Clojure and other dependencies via swank-clojure.el is error-prone and deprecated.
I believe that with the sort of setup that you describe, M-x slime will put everything in ~/.clojure and ~/.swank-clojure on the classpath. You can customise this by setting the swank-clojure-classpath variable (I have it customised to use ~/.clojure only; that's where I put the basic set of jars useful for launching one-off experimental REPLs.
As soon as you need more stuff on the classpath, the above is inadequate. There are two options for managing those more complex cases:
Option 1: launch Swank, then connect
The most straightforward approach is to use Leiningen. Put this in your project.clj:
(defproject repl-base "1.0.0-SNAPSHOT"
:description "A project to start Swank in."
:dependencies [[org.clojure/clojure "1.2.0"]
[org.clojure/clojure-contrib "1.2.0"]]
:dev-dependencies [[swank-clojure "1.2.1"]])
Then use lein swank in your project's directory to start a swank server and use M-x slime-connect to connect to it from within Emacs.
As another possibility, David Edgar Liebke's cljr will apparently launch stand-alone Swank instances for you; consult the README for details.
Option 2: launch Swank from within Emacs
Now, to be entirely honest, I tend to start Swank from within Emacs myself -- it's maybe a bit trickier to setup (and thus possibly not advisable in the beginning), but quite convenient later on. See my answer to an older question for one version of a function I use to launch Clojure-specific Swank instances complete with proper classpath configuration for Leiningen-style projects.
I use leiningen to connect emacs slime to the clojure instance. In a project you always end up with a couple of dependencies, which leiningen nicely places on the classpath.
It uses a small clojure file to describe the dependencies, e.g. :
(defproject myprojecy "0.1.0-SNAPSHOT"
:description "A project."
:dependencies [[org.clojure/clojure "1.2.0-beta1"]
[org.clojure/clojure-contrib "1.2.0-beta1"]]
:dev-dependencies [[swank-clojure "1.2.1"]])
You can see that changing versions is just a couple of keystrokes in an editor. Leiningen downloads the dependencies from the 'net which keeps the projects really smal and fast to version control.
running lein swank starts the program and the repl in swank :
ptimac:cljhack pti$ lein swank
user=> Connection opened on local port 4005
#<ServerSocket ServerSocket[addr=localhost/127.0.0.1,port=0,localport=4005]>
you can now connect in emacs using M-x slime-connect and accept the defaults.
You just need to set the swank-clojure-classpath variable before you hit M-x slime
Say if you have the clojure.jar, clojure-contrib.jar and swank-clojure.jar under c:\jars
In the scratch buffer paste this
(setq swank-clojure-classpath '("c:/jars/clojure.jar" "c:/jars/cloure-contrib.jar"
"c:/jars/swank-clojure.jar"))
Hit C-x C-e and the M-x slime , that should do it.
For details checkout
https://github.com/jochu/swank-clojure/blob/master/swank-clojure.el#L32-34
http://en.wikibooks.org/wiki/Clojure_Programming/FAQ#Where_does_swank-clojure_.28SLIME.29_look_for_Clojure.27s_jars.3F

How do I set up the Clojure classpath in Emacs after installing with ELPA?

I'm trying to add paths to my classpath in the Clojure REPL that I've set up in Emacs using ELPA. Apparently, this isn't the $CLASSPATH environment variable, but rather the swank-clojure-classpath variable that Swank sets up. Because I used ELPA to install Swank, Clojure, etc., there are a ton of .el files that take care of everything instead of my .emacs file. Unfortunately, I can't figure out how to change the classpath now.
I've tried using (setq 'swank-clojure-extra-classpaths (list ...)) both before and after the ELPA stuff in my .emacs, and I've tried adding paths directly to swank-clojure-classpath in .emacs, .emacs.d/init.el, and .emacs.d/user/user.el, but nothing works.
What I'm ultimately trying to do is to add both the current directory "." and the directory in which I keep my Clojure programs. I'm assuming swank-clojure-classpath is the thing I need to set here. Thanks for your help.
As mac says, you can use
M-x swank-clojure-project
to establish a slime REPL to a clojure project; the command will ask you for your projects root directory, and will establish a classpath that includes a variety of directories including src/ lib/ and resources/ if they are present.
Alternatively, if you are using leiningen, you can start that in a terminal with the command
$ lein swank
from inside your project root directory. This will establish a standard project classpath (as above). From here you can connect to this running process via Emacs with the command
M-x slime-connect
Finally a third option which I'd recommend is to connect via Emacs/slime (with M-x slime-connect) to a process started by your own shell script which specifies a custom set of JVM command line arguments e.g.
#!/bin/bash
java -server -cp "./lib/*":./src clojure.main -e "(do (require 'swank.swank) (swank.swank/start-repl))"
This allows you explicit control over how the VM is started, and is likely similar to what you will likely have to do in production anyway.
You want:
M-x swank-clojure-project
This adds all jars in your /lib dir.
If you want to :use a clojure file (bar.clj), in for instance /foo you would do:
(ns foo
(:use foo.bar))
Sorry I cannot help you with an answer, but maybe your question is wrong:
I myself haven't started a clojure session from within Emacs for ages. I think the better way is to describe all your dependencies in a single place (e.g. the maven pom.xml or leiningen's project.clj) and then start a swank session with those dependencies. I.e. add the swank-clojure lib to your (dev-)dependencies and then use lein swank or maven swank (not sure about the last one -- haven't used that much and not in a while) from the command line to start a swank session and use M-x slime-connect to attach to that session.
The advantage is that you get all the things in your classpath that you need -- and not more, so you cannot mistakenly use something from the repl that your final project doesn't know about.
This blog post gives a good summary how to do this right.