I want to create a variant of `defun' that takes arguments from Emacs and assembles a body to be evaluated by SLIME.
It seemed to me that this macro definition w ould work:
(defmacro ldefun (name arglist &rest body) `(defun ,name ,arglist (slime-eval (progn ,@body))))
For example, here is how it expands in a simple case:
(macroexpand '(ldefun testslime (first second) (list first (+ 1 second))))
;=> (defun testslime (first second) ;=> (slime-eval (progn (list first (+ 1 second)))))
But that didn't turn out.
In fact, even the `slime-eval' form just above does not work in a stand-alone fashion:
(slime-eval (progn (list 2 (+ 1 6))))
Gives this error:
Execution of a form compiled with errors. Form: (2 7)
Whereas from the REPL, I get this behavior:
CL-USER> (progn (list 2 (+ 1 6))) (2 7)
So, I find this behavior surprising. Can anyone help me out?
Note: I'm running Slime CVS from 2007-06-16 and SBCL 1.0.5.
You probably want to quote the argument to slime-eval; that error is b/c the (progn...) is being evaluated in Emacs, not CL.
I am trying to continue on the quest I described before --
"I want to create a variant of `defun' that takes arguments from Emacs and assembles a body to be evaluated by SLIME."
I think I am most of the way there.
Dan McCarthy pointed out:
You probably want to quote the argument to slime-eval; that error is b/c the (progn...) is being evaluated in Emacs, not CL.
Oops -- yes that I should be sure SLIME is doing the evalution, not Emacs -- but note, I also need to make sure that in the data I am sending to SLIME contains the values of the arguments, not just their names!
Here is a macro that accomplishes this:
(defmacro lefun (name arglist &rest body) `(defun ,name ,arglist (slime-eval (list (append (list 'lambda ',arglist) ',body) ,@arglist))))
Unfortunately, it still produces an error when I test it with the following code:
(lefun myplus (a b) (+ a b)) (myplus 2 3)
Specifically, the above triggers SLDB with the following message:
Execution of a form compiled with errors. Form: ((LAMBDA (A B) (+ A B)) 2 3) Compile-time error: illegal function call
I do not know what this means.
It seems there is a work-around using Vladimir's suggestion on how to interact programmatically with the REPL:
(defmacro lefun (name arglist &rest body) `(defun ,name ,arglist (setq argument-values (list )) (second (slime-eval (list 'swank:eval-and-grab-output (format "%S" (list (append (list 'lambda ',arglist) ',body) ,@arglist))))))) Testing:
(lefun current-package () *package*)
(current-package)
(lefun elephant-toy () (elephant::open-store '(:bdb "/home/joe/musndb")) (elephant::add-to-root "foo" 2) (elephant::close-store))
(elephant-toy)
In conclusion, I seem to have tentatively solved the problem -- but I would appreciate it if someone could explain why I am seeing the error mentioned above!
Joe Corneli jcorneli@planetmath.org wrote on 2007-06-20:
I am trying to continue on the quest I described before --
"I want to create a variant of `defun' that takes arguments from Emacs and assembles a body to be evaluated by SLIME."
...
Here is a macro that accomplishes this:
(defmacro lefun (name arglist &rest body) `(defun ,name ,arglist (slime-eval (list (append (list 'lambda ',arglist) ',body) ,@arglist))))
Unfortunately, it still produces an error when I test it with the following code:
(lefun myplus (a b) (+ a b)) (myplus 2 3)
Specifically, the above triggers SLDB with the following message:
Execution of a form compiled with errors. Form: ((LAMBDA (A B) (+ A B)) 2 3) Compile-time error: illegal function call
I do not know what this means.
Forms comming from Emacs to SWANK (the Common Lisp side of Slime) are READ in the :SWANK-IO-PACKAGE, i.e. the form that was really evaluted in Common Lisp looked liked this:
((SWANK-IO-PACKAGE::LAMBDA (A B) (SWANK-IO-PACKAGE::+ A B)) 2 3)
Which is not a valid form according to CL's evaluation model.
You have to send symbols to CL with an explicit package identifier:
(defmacro lefun (name arglist &rest body) `(defun ,name ,arglist (slime-eval (list (append (list 'cl:lambda ',arglist) ',body) ,@arglist))))
and
(lefun myplus (a b) (cl:+ a b))
HTH,
-T.