Skip to content

Commit 8f4bd86

Browse files
author
Mike Bruce
committed
[clojure-emacs#209] close socket buffer when REPL buffer closed
The user gets asked if they want to close each buffer sequentially. Or whatever their process-kill-query-function dictates.
1 parent dc140c9 commit 8f4bd86

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

inf-clojure.el

+34-16
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,9 @@ HOST is the host the process is running on, PORT is where it's listening."
870870
"Used to transfer state between the socket process and the
871871
inf-clojure-connect process/buffer.")
872872

873+
(defvar-local inf-clojure-socket-buffer nil
874+
"Used to kill the socket buffer for a Clojure REPL with the REPL buffer is shutdown.")
875+
873876
(defcustom inf-clojure-cli-args nil
874877
"Arguments to be supplied when the clojure repl type is used e.g -Mdev."
875878
:type 'string
@@ -904,21 +907,29 @@ OUTPUT is the latest data received from the process"
904907
(when inf-clojure-socket-callback
905908
(funcall inf-clojure-socket-callback)))))))
906909

910+
(defun inf-clojure-repl-sentinel (process event)
911+
"This function is called on update change, it is mainly used to ensure the socket a REPL was connected to is cleaned up when the REPL buffer is closed."
912+
(when (not (process-live-p process))
913+
(let ((repl-buffer (process-buffer process)))
914+
(with-current-buffer repl-buffer
915+
(when inf-clojure-socket-buffer
916+
(kill-buffer inf-clojure-socket-buffer))))))
917+
907918
(defvar inf-clojure-socket-forms
908919
`((lein . (concat "JVM_OPTS='-Dclojure.server.repl="
909920
"{:port %s :accept clojure.core.server/repl}' lein repl"))
910921
(boot . (concat "export BOOT_JVM_OPTIONS='-Dclojure.server.repl="
911922
"\"{:port %s :accept clojure.core.server/repl}\"' boot repl"))
912923
(clojure . ,(concat
913-
"clojure"
914-
" -J-Dclojure.server.repl="
915-
"\"{:port %s :accept clojure.core.server/repl}\""))
924+
"clojure"
925+
" -J-Dclojure.server.repl="
926+
"\"{:port %s :accept clojure.core.server/repl}\""))
916927
(cljs . ,(concat
917-
"clojure"
918-
" -J-Dclojure.server.repl="
919-
"\"{:port %s :accept clojure.core.server/repl}\""))
928+
"clojure"
929+
" -J-Dclojure.server.repl="
930+
"\"{:port %s :accept clojure.core.server/repl}\""))
920931
(lein-clr . ,(concat "JVM_OPTS='-Dclojure.server.repl="
921-
"{:port %s :accept clojure.core.server/repl}' lein clr repl"))
932+
"{:port %s :accept clojure.core.server/repl}' lein clr repl"))
922933
(planck . "planck -n %s")
923934
(babashka . "bb socket-repl %s")))
924935

@@ -964,22 +975,29 @@ VALUE can be any string, by default this is either derived from your current dir
964975
(repl-buffer-name (format "%s-%s-repl" project-name repl-type))
965976
(socket-form (cdr (assoc (intern repl-type) inf-clojure-socket-forms)))
966977
(socket-cmd (concat (format socket-form (number-to-string port))
967-
(cond ((string= repl-type "clojure") inf-clojure-cli-args)
968-
((string= repl-type "cljs") inf-clojure-cljs-cli-args)
969-
(t nil))))
978+
(cond ((string= repl-type "clojure") inf-clojure-cli-args)
979+
((string= repl-type "cljs") inf-clojure-cljs-cli-args)
980+
(t nil))))
970981
(sock (start-file-process-shell-command
971982
socket-process-name socket-buffer
972983
socket-cmd)))
973984
(with-current-buffer socket-buffer
974985
(setq-local
975986
inf-clojure-socket-callback
976987
(lambda ()
977-
(setq-local inf-clojure-custom-repl-type
978-
(intern repl-type)
979-
inf-clojure-custom-repl-name
980-
;; comint mode includes the ** chars
981-
repl-buffer-name)
982-
(inf-clojure-connect host port))))
988+
(setq inf-clojure-custom-repl-type
989+
(intern repl-type)
990+
inf-clojure-custom-repl-name
991+
;; comint mode includes the ** chars
992+
repl-buffer-name
993+
repl-buffer
994+
(get-buffer-create repl-buffer-name))
995+
(inf-clojure-connect host port)
996+
(with-current-buffer (concat "*" repl-buffer-name "*")
997+
(setq inf-clojure-socket-buffer socket-buffer))
998+
(set-process-sentinel
999+
(get-buffer-process (get-buffer (concat "*" repl-buffer-name "*")))
1000+
#'inf-clojure-repl-sentinel))))
9831001
(set-process-filter sock #'inf-clojure-socket-filter)
9841002
(message "Starting socket server at %s:%s with '%s'" host port socket-cmd)))
9851003

0 commit comments

Comments
 (0)