IELM: a REPL for emacs





Emacs-lisp (elisp) is a nice language to play around with code and try things as
you develop them – explorative programming. I often use the *scratch*
buffer for that, but sometimes it's nice to use a so-called 'REPL' (
Read-Eval-Print-Loop) instead. A REPL is a sort-of command-line interface
where your expressions are evaluated as soon as they are considered 'complete'
and you press Enter.



So, enter Emacs's built-in repl: IELM. You can activate it with M-x ielm,
and the interaction looks something like the following:





*** Welcome to IELM ***  Type (describe-mode) for help.
ELISP> 123
123
ELISP> (+ 1 2)
3
ELISP> ;; comment
ELISP> (defun fac (n)
(if (= 0 n)
1
(* n (fac (- n 1)))))
fac
ELISP> (fac 5)
120
ELISP>





By default, IELM evaluates complete expressions automatically as soon you as
you press Enter. So one thing to remember is that if you want to have
multi-line expression (like above), you must make sure that after each line
the expression is not complete (i.e., the brackets are not balanced) --
otherwise the expression will be evaluated too early. That makes modes like
autopair or paredit a bit inconvenient for this.



If you don't like that behavior, you can do:



(setq ielm-dynamic-return nil)



which will allow you to Enter as much as you want and only evaluate things when
you press C-j. But then you might as well use *scratch* I
suppose. Personally, I use IELM mostly as a calculator.


No comments:

Post a Comment

Followers

Popular Posts