2

I'm trying to create just a basic package called "a" (to learn) with asdf which I've already defined in package.lisp, and my main file a.lisp starts with:

(in-package :a)
...

If I'm developing my module I'd just like to run eval the whole buffer into the SLIME REPL by doing M-x slime-eval-buffer or something, but that does not happen, after running that command I'm still at the cl-user package:

;;; from a.lisp
A> (in-package :a)
#<PACKAGE "A">
CL-USER>     <--- should have stayed in A>

If however I paste the whole code in the SLIME REPL myself it already works. Pasting lots of code over and over is not nice at all though for interactive development.

Can you help? Do you have suggestions for what I'm getting wrong, or how you usually cope with this instead?

Thank you.

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
Alberto
  • 565
  • 5
  • 14
  • Make sure you read both the accepted answer and Rainer Joswig's! Together they helped me understand the topic much better. – Alberto May 11 '19 at 18:49

2 Answers2

7

Slime: Code from a buffer is usually not evaluated in a repl

I'd just like to run eval the whole buffer into the SLIME REPL by doing M-x slime-eval-buffer

The model is not that one 'evaluates in the SLIME REPL' from a source buffer. One evaluates the code in the connected Lisp. This connected Lisp may spawn a thread for each evaluation request.

You can see for example that after evaluating a buffer, the usual REPL variables like * (last result) are not updated.

When you paste code into the REPL buffer, then you execute from there and it executes code in a REPL.

Slime: actually evaluate from a buffer in a REPL

You can evaluate code from a Lisp buffer into the REPL. See the command c-u m-x slime-eval-last-expression-in-repl or shorter c-u c-c c-j. The c-u modifier causes slightly different behavior with packages:

  1. switch to the REPL buffer
  2. switch to the source buffer package
  3. evaluate the source code in the REPL (including setting the REPL vars)
  4. print the result in the REPL buffer (including setting the REPL vars)
  5. don't switch the REPL back to its earlier package

Note: this behavior is specific to SLIME and not necessarily how other environments behave.

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
  • 1
    Thank you Rainer! The existence of such feature makes a lot of sense and I didn't even know it existed. Thank you! I wish I could put two people with accepted answers as you both gave me very distinct and useful insights on the topic. – Alberto May 11 '19 at 18:47
4

It doesn't work like that. The in-package form is an instruction for the reader, so that the forms in the buffer after that are read in that package. In fact, when you do a C-c C-c (slime-compile-defun), it will look for the preceding in-package form regardless of whether that was evaluated or compiled somehow.

It would be a source of confusing errors otherwise, since you could (and frequently would) accidentally compile things in the wrong package because of some changed interaction order.

In order to switch the package in the REPL, you can execute an in-package form there, or you can use the SLIME shortcut C-c ~ from the buffer (which also switches the current directory).

Svante
  • 50,694
  • 11
  • 78
  • 122
  • WOW! Had no idea! Amazing!!! Are there any more commands that are special in this way that I should be aware of when doing this kind of evals in slime? or just in-package? Where did you learn all those things? It seems hard to gain more knowledge in Lisp as there are so few resources online. Thank you! – Alberto May 11 '19 at 15:05