<-- home

Changing Emacs CIDER pretty print to comment

I frequently use CIDER to evaluate a Clojure form and pretty print the result into a comment directly below in the current buffer. The function I call is ~cider-pprint-eval-last-sexp~ which for me is bound to ~C-c C-p~. The docstring for that function says:

Evaluate the sexp preceding point and pprint its value.
If invoked with [prefix-command], insert as comment in the current
buffer, else display in a popup buffer.

So if I enter ~C-u C-c C-p~ I get the result pretty printed into a comment

(zipmap (range 0 15) (range 10 25))
;; => {0 10,
;; 1 11,
;; 2 12,
;; 3 13,
;; 4 14,
;; 5 15,
;; 6 16,
;; 7 17,
;; 8 18,
;; 9 19,
;; 10 20,
;; 11 21,
;; 12 22,
;; 13 23,
;; 14 24}

which is almost what I want, but semicolon comments are a little unflexible compared to Clojure's discard macro ~#_~.

Looking into the cider source I found documentation which hinted at how to customize the pretty print to comment output. Setting these three elisp variables changes the output to what I desire:

(setq cider-comment-prefix "#_" )
(setq cider-comment-continued-prefix "")
(setq cider-comment-postfix "\n")

(zipmap (range 0 15) (range 10 25))
#_{0 10,
 1 11,
 2 12,
 3 13,
 4 14,
 5 15,
 6 16,
 7 17,
 8 18,
 9 19,
 10 20,
 11 21,
 12 22,
 13 23,
 14 24}

- Adam Frey, August 2019

<-- home