You can use with-out-str
to capture the output into a string.
user> (with-out-str
(print "foo"))
"foo"
Knowing that you can take the cider-pprint-eval-last-sexp-to-comment
function, copy/paste it and make a small change.
(define-key cider-mode-map (kbd "C-c C-p")
(lambda ()
(interactive)
(let* ((insert-before nil)
(bounds (cider-last-sexp 'bounds))
(insertion-point (nth (if insert-before 0 1) bounds))
(comment-postfix (concat cider-comment-postfix
(if insert-before "\n" ""))))
(cider-interactive-eval (concat "(with-out-str " (cider-last-sexp) " )")
(cider-eval-pprint-with-multiline-comment-handler
(current-buffer)
(set-marker (make-marker) insertion-point)
cider-comment-prefix
cider-comment-continued-prefix
comment-postfix)
bounds
(cider--nrepl-print-request-map fill-column)))))
This function prints the captured output instead of the value. If you use it on something that doesn't write to *out*
it will print the empty string.