SLIME Tips and Techniques - Part 7 (Multiple REPLs)
Wednesday, April 25, 2007
I normally don't use multiple REPLs in SLIME; however, sometimes it is useful to have them. You can create additional REPLs in a number of different ways, depending on what specifically you're after. Here are some typical use case scenarios:
- You want to "play around" with some ideas but don't want to "muck up" your existing lisp environment and REPL. In that case, you probably want to start up a new lisp instance and test things in a completely independent REPL. When I do "M-x slime" multiple times (and say "y" to the "Create an additional *inferior-lisp*?" prompt), that is exactly what I get - multiple lisp instances with a separate REPL for each one. I can use "C-c C-x c" to switch between them so that I can do my "mucking around" in the one REPL and do "real stuff" in the other REPL ("C-c C-x c" opens a buffer which controls which REPL is "active" when you're in a lisp buffer compiling/evaluating/etc code. An example of what is displayed when you press "C-c C-x c" is in the screen shot below - pressing "d" in the "*SLIME connections*" buffer selects which REPL is the default one).
- You want to have multiple REPLs on the same lisp instance (with
each REPL sharing the REPL history and */**/***/etc values). This is
useful if you want to use one REPL for a demo but want to be able to
enter other commands in another REPL to set up or modify an
environment for the demo. There are probably other use cases for this;
however, this is what I normally use #2 for. Again (as with #1), I can
use "C-c C-x c" to switch between the REPLs so I have control over
which one is "active". Although SLIME does not provide any standard
function for automatically creating multiple REPLs, it is easy enough
to do if you are using a lisp with threads. In your existing SLIME
REPL, you can just enter "(swank::create-server :port some-port)" and
then in emacs do a "M-x slime-connect" and specify the host & port. If
you do this a lot, you could automate it with something like the
(defun slime-new-repl (&optional new-port) "Create additional REPL for the current Lisp connection." (interactive) (if (slime-current-connection) (let ((port (or new-port (slime-connection-port (slime-connection))))) (slime-eval `(swank::create-server :port ,port)) (slime-connect slime-lisp-host port)) (error "Not connected")))and just do "M-x slime-new-repl" whenever you wanted a new REPL. As you can see from the above function, although you can specify any port, you can also just reuse the existing port for the separate REPL, so it's easy enough to do when you're working on a remote machine too (without having to open new ports). I thought that, at one stage, answering "n" to the "Create an additional *inferior-lisp*?" prompt (when you run "M-x slime" multiple times) did this too; however, at the moment, it only re-loads the lisp using the existing REPL buffer. Maybe this is a "feature" or maybe it's a bug.
- You want to have multiple REPLs on the same lisp instance (with each REPL having it's own REPL history and */**/***/etc values). I thought that it was possible to do this at one time with SLIME; however, I don't think there is any way that you can currently do this in SLIME.
slime-new-repland then pressing "C-c C-x c":
Notice that the history is shared between the two REPLs.