views:

783

answers:

6

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.

A: 

In the *scratch* buffer, just type C-j to evaluate the expression before point.

cjm
A: 

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$
Kyle Burton
This looks more complicated than it should be - running a shell, which runs another emacs in batch mode, which runs the REPL, all inside the main emacs runtime. Anyway, it solves my problem, so thank you for help!
Michał Kwiatkowski
This REPL implementation doesn't handle multi-line inputs. If you don't end an expression in a single line it gives: Error parsing '(whatever' (end-of-file repl.el)Is there an easy way to fix that?
Michał Kwiatkowski
+4  A: 

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
jfm3
+19  A: 

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"

Greg Mattes
Exactly what I was looking for, big thanks!
Michał Kwiatkowski
+1  A: 

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

Alex Ott
+1  A: 

Eshell is another option for an interactive Elisp interpreter.

M-x eshell

Not only is it a command shell like bash (or cmd.exe if on Windows) but you can also interactively write and execute Elisp code.

~ $ ls
foo.txt
bar.txt
~ $ (+ 1 1)
2
Ray Vega