Right now I write expressions in the *scratch*
buffer and test them by evaluating with C-x C-e. I would really appreciate having an interactive interpreter like SLIME or irb, in which I could test Emacs Lisp expressions.
views:
783answers:
6In the *scratch*
buffer, just type C-j to evaluate the expression before point.
Well, if you're really interested in a literal REPL for emacs it is possible to write one using the -batch mode of emacs:
(require 'cl)
(defun read-expression ()
(condition-case
err
(read-string "> ")
(error
(message "Error reading '%s'" form)
(message (format "%s" err)))))
(defun read-expression-from-string (str)
(condition-case
err
(read-from-string str)
(error
(message "Error parsing '%s'" str)
(message (format "%s" err))
nil)))
(defun repl ()
(loop for expr = (read-string "> ") then (read-expression)
do
(let ((form (car (read-expression-from-string expr))))
(condition-case
err
(message " => %s" (eval form))
(error
(message "Error evaluating '%s'" form)
(message (format "%s" err)))))))
(repl)
You can call this from the command line, or, as you seem to want, from within an emacs buffer running a shell:
kburton@hypothesis:~/projects/elisp$ emacs -batch -l test.el
Loading 00debian-vars...
> (defvar x '(lambda (y) (* y 100)))
=> x
> (funcall x 0.25)
=> 25.0
>
kburton@hypothesis:~/projects/elisp$
Your best bet is the *scratch*
buffer. You can make it more like a REPL by first turning on the debugger:
M-x set-variable debug-on-error t
Then use C-j
instead of C-x C-e
, which will insert the result of evaluating the expression into the buffer on the line after the expression. Instead of things like command history, * * *
and so forth, you just move around the *scratch*
buffer and edit.
If you want things like * * *
to work, more like a usual REPL, try ielm
.
M-x ielm
It's easy to start an interactive Lisp session with:
M-x ielm
You can read more about this feature in the Emacs manual section on "Lisp Interaction"
To run just one elisp expression you can use M-: shortcut and enter expression in mini-buffer. For other cases you can use scratch buffer