Interactively Evaluating Clojure Code in Comments in Emacs
There’s a loose convention in Clojure of leaving top-level (comment ...)
-style comments in source files to give examples of how a
particular piece of code works or to provide a convenient means of
invoking functionality contained in the file. You can even see this
in the source files to Clojure proper.
Even though leaving commented out code can seem a bit messy, it has also saved me a ton of time relearning how to invoke something, so I have somewhat mixed feelings about the practice. But, regardless of the merits of using this in production code, it’s unarguably useful in development, and I use it extensively as I work to test out function invocations with different arguments, and to store little bits of test data.
Frustratingly, this form does not play nicely with CIDER’s C-M-x
(cider-eval-defun-at-point
) and C-c M-;
(cider-eval-defun-to-comment
) commands, which expect the target form
to be at the top-level of the file. The containing (comment ...)
form means that technically isn’t so, leading me to perpetually
evaluate the wrong form…
(comment
(+ 1 1) <point>
) ; => nil
…and be told for the millionth time that comment
s evaluate to
nil
.
I finally discovered that there is an easy way to change this behavior, with just a single settings tweak:
(setq clojure-toplevel-inside-comment-form t)
Now things will work as you (might) expect…
(comment
(+ 1 1) <point> ; => 2
)
… which is certainly more useful, and destined to become a permanent part of my Emacs config.