Locating a bad expression in Emacs and Clojure
I work frequently in Emacs and Clojure. Emacs has two extensions that let you do interactive development: tools.nrepl and swank-clojure.
I sometimes make the mistake of modifying many functions in a module before I attempt executing or compiling the entire file again and am met with an error like:
Can't have more than 1 variadic overload [Thrown class java.lang.Exception]
Finding the source of this in a large file full of functions can be difficult.
There is a trick you can use in Emacs to make this simpler though. By using a simple keyboard macro you can step through all of the expressions in a buffer and evaluate each one in turn until you find the expression with the error in it.
The steps are as follows:
- Jump to the top of your buffer:
C-M-<
- Start recording a macro by typing:
C-x (
- Move forward a single sexpression by typing:
C-M-f
- Evaluate that expression by typing:
C-x C-e
- End the macro by typing:
C-x )
If it was in the first expression then you’re done. If it’s not, then check the next one by typing C-x e
, to continue checking you can then keep pressing just e
until you’ve found the offending code.