Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kill previous nrepl sessions when nrepl-jack-in called?

At this moment in time, I am using nrepl primarily to talk to Clojurescript apps. I like to use nrepl from within emacs. I start nrepl by typing M-x nrepl-jack-in.

Unfortunately, my nrepl session often gets completely hung. When this happens, I dutifully kill the 3 buffers related to nrepl. These buffers are:

  1. *nrepl*
  2. *nrepl-connection*
  3. *nrepl-server*

*nrepl-server* also has an active process, it ask me if I want to close it, and I say yes.

I then type M-x nrepl-jack-in again.

This is a pain.

I would like to overload nrepl-jack-in so that it automatically checks if any of these 3 buffers exist. If any of them do exist, it will kill these buffers and any active processes associated with these bufers. After doing this, the overloaded nrepl-jack-in will proceed as usual. I would like this because then, whenever I detect that nrepl has decided to hang itself again, I could just type M-X nrepl-jack-in and restart what I was doing.

like image 633
Stephen Cagle Avatar asked Oct 21 '12 22:10

Stephen Cagle


2 Answers

This should get the job done:

(defun my-nrepl-jack-in ()
  (interactive)
  (dolist (buffer (buffer-list))
    (when (string-prefix-p "*nrepl" (buffer-name buffer))
      (kill-buffer buffer)))
  (nrepl-jack-in nil))
like image 129
Bozhidar Batsov Avatar answered Nov 20 '22 05:11

Bozhidar Batsov


The chosen answer didn't quite work for me... The nrepl process sentinel threw an error, preventing it from restarting. I played with it a bit and came up with the following (which also gives a separate kill-nrepl function)

;; Disable prompt on killing buffer with a process
(setq kill-buffer-query-functions
      (remq 'process-kill-buffer-query-function
            kill-buffer-query-functions))

(defun nrepl-kill ()
  "Kill all nrepl buffers and processes"
  (interactive)
  (when (get-process "nrepl-server")
    (set-process-sentinel (get-process "nrepl-server")
                          (lambda (proc evt) t)))
  (dolist (buffer (buffer-list))
    (when (string-prefix-p "*nrepl" (buffer-name buffer))
      (kill-buffer buffer))))

(defun nrepl-me ()
  (interactive)
  (nrepl-kill)
  (nrepl-jack-in nil))
like image 40
levand Avatar answered Nov 20 '22 04:11

levand