views:

214

answers:

1

when I start swank through leiningen it accepts the next slime connection and off I go. I would really like to have several emacs instances connect to the same swank instance. Can I do this? can I do this through leiningen?

+10  A: 

Well, you can start your first SLIME normally, then (require 'swank.swank) (or maybe it's required by default... not sure), do (swank.swank/start-repl port) with port replaced by some port number and you can connect a second instance of SLIME to that newly created REPL.

I've done it just now, with one Emacs connecting to a REPL started with lein swank, (swank.swank/start-repl 4006) in the first Emacs, M-x slime-connect in the second Emacs (providing 4006 as the port number), then I could do this:

; first Emacs 
(def x 5)

; second Emacs
x ; evaluates to 5
(def y 1234)

; first Emacs
y ; evaluates to 1234

Cool, no? :-)

Update: Oh, BTW -- (swank.swank/start-repl) starts the new REPL in the background and does not block the REPL you use to execute it. The return value is nil, so I'm not sure how to kill the new REPL...

(Update 2: Removed something I'm no longer sure about.)

Update 3: While the above method is perfectly general in that it makes it possible to connect an extra client regardless of how the original Swank instance has been started, it might be more convenient to start Swank with the command

lein swank 4005 "localhost" :dont-close true

The port and host name arguments must mentioned explicitly if :dont-close true is to be passed. 4005 and "localhost" are the default values.

This will make it possible to disconnect from Swank and reconnect later, but also to connect a number of clients simultaneously.

(I just noticed that this is possible while answering this new question on how to enable reconnections to Leiningen-started Swank; it suddenly occurred to me to check if :dont-close would also cause simultaneous connections to be accepted -- and it does.)

Michał Marczyk
Thats awesome. Exactly what I was looking for!
Arthur Ulfeldt
'Awesome' is exactly the right word. :-) BTW, You can use `swank.swank/start-repl` to embed Swank in any Clojure app -- I've read about people using it to very good effect. The main caveat is that Swank doesn't care about security, so you have to take care about limiting access to the REPL's port yourself.
Michał Marczyk
Apparently the functionality described in update 3 above requires a fairly recent lein swank; see the other question I linked to (and in particular the comments on my answer there) for details.
Michał Marczyk