Skip to content

Instantly share code, notes, and snippets.

@gonewest818
Last active November 9, 2017 19:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gonewest818/5f68a092994ba135d915bf14c807329f to your computer and use it in GitHub Desktop.
Save gonewest818/5f68a092994ba135d915bf14c807329f to your computer and use it in GitHub Desktop.
cider-pprint-eval-last-sexp-to-buffer
(setq lexical-binding t)
(defun cider-eval-pprint-to-multiline-comment-handler (buffer location arrow-prefix comment-prefix)
"Make a handler for evaluating and printing commented results in BUFFER.
LOCATION is the location at which to insert.
COMMENT-PREFIX is the comment prefix to use."
(cl-flet ((multiline-comment-handler (buffer value)
(with-current-buffer buffer
(save-excursion
(goto-char location)
(let ((lines (split-string value "[\n]+" t)))
;; only the first line gets arrow-prefix
(insert (concat arrow-prefix (pop lines) "\n"))
(dolist (elem lines)
(insert (concat comment-prefix elem "\n"))))))))
(nrepl-make-response-handler buffer
'()
#'multiline-comment-handler
#'multiline-comment-handler
'())))
(defun cider-pprint-eval-last-sexp-to-comment (loc)
"Evaluate the last sexp and insert result as comment at LOC.
With a prefix arg, LOC, insert before the form, otherwise afterwards."
(interactive "P")
(let* ((bounds (cider-last-sexp 'bounds))
(insertion-point (nth (if loc 0 1) bounds)))
(cider-interactive-eval nil
(cider-eval-pprint-to-multiline-comment-handler
(current-buffer) insertion-point ";; => " ";; ")
bounds
(cider--nrepl-pprint-request-plist (cider--pretty-print-width)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment