Skip to content

Instantly share code, notes, and snippets.

@jvillste
Last active February 21, 2024 15:42
Show Gist options
  • Save jvillste/b7753bcfee752a05aeff20683d0bd6fd to your computer and use it in GitHub Desktop.
Save jvillste/b7753bcfee752a05aeff20683d0bd6fd to your computer and use it in GitHub Desktop.
Emacs commands for evaluating code so that stdout stderr go to the cider result buffer along with the result
(defun juvi-pprint-eval-to-result-buffer (form)
(cider-interactive-eval form
(nrepl-make-response-handler (cider-popup-buffer cider-result-buffer nil 'clojure-mode 'ancillary)
(lambda (buffer value)
(cider-emit-into-popup-buffer buffer value))
(lambda (buffer out)
(cider-emit-into-popup-buffer buffer out))
(lambda (buffer err)
(cider-emit-into-popup-buffer buffer err))
'())
nil
(cider--nrepl-print-request-map fill-column)))
(defun juvi-pprint-eval-last-sexp-to-result-buffer ()
(interactive)
(juvi-pprint-eval-to-result-buffer (cider-last-sexp)))
(define-key cider-mode-map (kbd "C-c C-p") 'juvi-pprint-eval-last-sexp-to-result-buffer)
(defun juvi-mark-function-for-eval ()
(interactive)
(setq juvi-marked-ns (cider-current-ns))
(setq juvi-marked-function (cider-last-sexp))
(message juvi-marked-function))
(define-key cider-mode-map (kbd "C-M-o C-M-e") 'juvi-mark-function-for-eval)
(defun juvi-eval-marked-function ()
(interactive)
(cider-load-buffer)
(juvi-pprint-eval-to-result-buffer (concat "(" juvi-marked-ns "/" juvi-marked-function ")")))
(define-key cider-mode-map (kbd "C-M-o C-M-i") 'juvi-eval-marked-function)
(defun juvi-eval-function-at-point ()
(interactive)
(juvi-pprint-eval-to-result-buffer (concat "(" (cider-current-ns) "/" (cider-last-sexp) ")")))
(define-key cider-mode-map (kbd "M-S-o M-S-i") 'juvi-eval-function-at-point)
(defun juvi-mark-sexp-for-eval ()
(interactive)
(setq juvi-marked-buffer (current-buffer))
(setq juvi-marked-sexp (cider-last-sexp)))
(define-key cider-mode-map (kbd "C-o C-e") 'juvi-mark-sexp-for-eval)
(defun juvi-eval-marked-sexp ()
(interactive)
(if (and juvi-marked-buffer juvi-marked-sexp)
(with-current-buffer juvi-marked-buffer
(save-excursion
(juvi-pprint-eval-to-result-buffer juvi-marked-sexp)))
(message "First mark sexp with `juvi-mark-sexp-for-eval`" 'font-lock-warning-face)))
(define-key cider-mode-map (kbd "C-o C-i") 'juvi-eval-marked-sexp)
(defun juvi-eval-marked-sexp-silently ()
(interactive)
(with-current-buffer juvi-marked-buffer
(cider-interactive-eval juvi-marked-sexp
(nrepl-make-response-handler juvi-marked-buffer
(lambda (buffer value))
(lambda (_buffer out))
(lambda (_buffer err))
nil)))
(message "Ran marked sexp."))
(define-key cider-mode-map (kbd "M-o M-i") 'juvi-eval-marked-sexp-silently)
(defun juvi-eval-last-sexp-to-kill-ring ()
(interactive)
(cider-interactive-eval
(cider-last-sexp)
(nrepl-make-response-handler (current-buffer)
(lambda (_buffer value)
(message "result is now in the kill ring")
(kill-new value))
(lambda (_buffer out)
(cider-emit-interactive-eval-output out))
(lambda (_buffer err)
(cider-emit-interactive-eval-err-output err))
'())
nil
(cider--nrepl-print-request-map fill-column)))
(define-key cider-mode-map (kbd "C-o C-p") 'juvi-eval-last-sexp-to-kill-ring)
(defun juvi-eval-last-sexp-output-to-kill-ring ()
(interactive)
(kill-new "")
(cider-interactive-eval (cider-last-sexp)
(nrepl-make-response-handler (current-buffer)
(lambda (_buffer _value))
(lambda (_buffer output)
(kill-append output nil))
(lambda (_buffer err)
(cider-emit-interactive-eval-err-output err))
'())
nil
(cider--nrepl-print-request-map fill-column))
(message "evaluation output is now in the kill ring"))
(define-key cider-mode-map (kbd "C-M-o C-M-p") 'juvi-eval-last-sexp-output-to-kill-ring)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment