Skip to content

Instantly share code, notes, and snippets.

@camsaul
Created January 5, 2021 21:37
Show Gist options
  • Save camsaul/da0ac3399b3aa4741f5de59a254b6546 to your computer and use it in GitHub Desktop.
Save camsaul/da0ac3399b3aa4741f5de59a254b6546 to your computer and use it in GitHub Desktop.
Save, load, and switch to CIDER REPL buffer Emacs Lisp snippet
(cl-defun cam/visible-buffer-matching (pred &optional return-multiple-values?)
"Return the first buffer visible in any window on any frame that satisfies PRED."
(dolist (frame (frame-list))
(dolist (window (window-list frame))
(let ((buffer (window-buffer window)))
(when (funcall pred buffer)
(cl-return-from cam/visible-buffer-matching (if return-multiple-values?
(list buffer window frame)
buffer)))))))
(cl-defmacro cam/when-let-visible-buffer (((binding &body pred-body) &rest more) &body body)
"Iterate through all *visibile* buffers; for each buffer, bind it to BINDING, and bind its window and frame to
anaphors `this-window' and `this-frame', respectively; then evaluate PRED-BODY. If the result is truthy, evaluate
BODY. Example:
\(cam/when-let-visible-buffer ((buffer (string-equal (buffer-name buffer) \"*Warnings*\")))
\(do-something buffer))"
(declare (indent 1))
`(cl-multiple-value-bind (,binding this-window this-frame) (cam/visible-buffer-matching (lambda (,binding)
,@pred-body) :return-multiple-values)
(when ,binding
,(if more
`(cam/when-let-visible-buffer ,more ,@body)
`(progn ,@body)))))
(defun cam/-clj-clear-nrepl-server-buffer ()
"Clear contents of the `*nrepl-server*` buffer."
(cam/when-let-visible-buffer ((buffer (string-prefix-p "*nrepl-server" (buffer-name buffer))))
(with-current-buffer buffer
(comint-clear-buffer))))
(defun cam/clj-save-load-switch-to-cider ()
(interactive)
(save-buffer)
(cam/-clj-clear-nrepl-server-buffer)
(cider-load-buffer-and-switch-to-repl-buffer :set-namespace)
(cider-repl-clear-buffer))
@camsaul
Copy link
Author

camsaul commented Jan 5, 2021

If you don't need cam/-clj-clear-nrepl-server-buffer you can just do

(defun cam/clj-save-load-switch-to-cider ()
  (interactive)
  (save-buffer)
  (cider-load-buffer-and-switch-to-repl-buffer t)
  (cider-repl-clear-buffer)) 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment