From 9e981c7930b48e9a0279d79193fe4a378e972efc Mon Sep 17 00:00:00 2001 From: dan sutton Date: Fri, 7 Apr 2023 13:30:56 -0500 Subject: [PATCH 1/5] Cleanup socket repl startup. ** How to use: ** To use one of the defaults defined in `inf-clojure-socket-repl-startup-forms`, just `m-x inf-clojure-socket-repl` and choose your startup. To add your own custom startup ```emacs-lisp (setq inf-clojure-custom-startup "clojure -J-Dclojure.server.repl=\"{:port %d :accept clojure.core.server/repl}\" -A:grepl") ``` This will prompt you for a repl type so it's useful to also include ```emacs-lisp (setq inf-clojure-custom-repl-type 'clojure) ``` Or set these in a .dir-locals.el file for each project with ```emacs-lisp ;;; Directory Local Variables ;;; For more information see (info "(emacs) Directory Variables") ((nil . ((inf-clojure-custom-startup . "clojure -J-Dclojure.server.repl=\"{:port %d :accept clojure.core.server/repl}\" -A:grepl") (inf-clojure-custom-repl-type . clojure)))) ``` ** things i changed ** - added `(defvar inf-clojure-custom-repl-name nil)`. This was originally used but never defined so socket startup and inf-connect would fail. This also was not honored and now controls the repl buffer name. - removed `inf-clojure-socket-repl-type`. This was also used and never defined so it also caused startup to fail. But we already have a notion of `inf-clojure-custom-repl-type` so we can just reuse that. There's nothing special about it being the socket repl type for connect. - simplified the buffer startup mechanism. Lots of `let` bindings - added `inf-clojure--suppress-connect-message-p`. Ostensibly private, others can use it. But two things are sending messages: how we start the clojure process, and that we connected to the clojure process. I think how we start the clojure process is the far more important one as it includes port information, aliases, etc. --- inf-clojure.el | 61 ++++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 32 deletions(-) diff --git a/inf-clojure.el b/inf-clojure.el index 6f8d216..03dbda8 100644 --- a/inf-clojure.el +++ b/inf-clojure.el @@ -518,6 +518,12 @@ Should be a symbol that is a key in `inf-clojure-repl-features'." (const :tag "babashka" babashka) (const :tag "determine at startup" nil))) +(defvar inf-clojure-custom-repl-name nil + "A string to be used as the repl buffer name.") + +(defvar inf-clojure--suppress-connect-message-p nil + "A boolean of whether to suppress the message on connect.") + (defun inf-clojure--whole-comment-line-p (string) "Return non-nil iff STRING is a whole line semicolon comment." (string-match-p "^\s*;" string)) @@ -833,12 +839,12 @@ process buffer for a list of commands.)" (cmdlist (if (consp cmd) (list cmd) (split-string-and-unquote cmd))) - (repl-type (or inf-clojure-socket-repl-type - (unless prefix-arg + (repl-type (or (unless prefix-arg inf-clojure-custom-repl-type) - (car (rassoc cmd inf-clojure-startup-forms)) - (inf-clojure--prompt-repl-type)))) - (message "Starting Clojure REPL via `%s'..." cmd) + (car (rassoc cmd inf-clojure-startup-forms)) + (inf-clojure--prompt-repl-type)))) + (unless inf-clojure--suppress-connect-message-p + (message "Starting Clojure REPL via `%s'..." cmd)) (with-current-buffer (apply #'make-comint process-buffer-name (car cmdlist) nil (cdr cmdlist)) (inf-clojure-mode) @@ -849,7 +855,8 @@ process buffer for a list of commands.)" (setq inf-clojure-buffer (get-buffer repl-buffer-name)) (if inf-clojure-repl-use-same-window (pop-to-buffer-same-window repl-buffer-name) - (pop-to-buffer repl-buffer-name)))) + (pop-to-buffer repl-buffer-name)) + repl-buffer-name)) ;;;###autoload (defun inf-clojure-connect (host port) @@ -882,7 +889,6 @@ OUTPUT is the latest data received from the process" (insert output))) (let ((prompt-displayed (string-match inf-clojure-prompt output))) (when prompt-displayed - (message (format "Socket REPL startup detected for %s" (process-name process))) (with-current-buffer server-buffer (when inf-clojure-socket-callback (funcall inf-clojure-socket-callback))))))) @@ -917,11 +923,11 @@ If left as nil a random port will be selected between 5500-6000." ;;;###autoload (defun inf-clojure-socket-repl (cmd) - "Start a socket REPL server and connect to it via `inf-clojure'. -CMD is the command line used to start the socket REPL, if this -isn't provided you will be prompted to select from the defaults -provided in `inf-clojure-socket-repl-startup-forms' or -`inf-clojure-custom-startup' if this is defined." + "Start a socket REPL server and connects to it via `inf-clojure-connect'. +CMD is the command line instruction used to start the socket +REPL. It should be a string with \"%d\" in it to take a random +port. Set `inf-clojure-custom-startup' or choose from the +defaults provided in `inf-clojure-socket-repl-startup-forms'" (interactive (list (or (unless current-prefix-arg inf-clojure-custom-startup) (completing-read "Select Clojure socket REPL startup command: " @@ -937,13 +943,9 @@ provided in `inf-clojure-socket-repl-startup-forms' or (inf-clojure--prompt-repl-type))) (project-name (inf-clojure--project-name (or project-dir "standalone"))) (socket-process-name (format "*%s-%s-socket-server*" project-name repl-type)) - (socket-buffer-name (format "*%s-%s-socket*" project-name repl-type)) - (socket-buffer (get-buffer-create socket-buffer-name)) - (repl-buffer-name (format "%s-%s-repl" project-name repl-type)) - (socket-form (or cmd - (cdr (assoc repl-type inf-clojure-socket-repl-startup-forms)) - inf-clojure-custom-startup)) - (socket-cmd (format socket-form port)) + (socket-buffer (get-buffer-create + (format "*%s-%s-socket*" project-name repl-type))) + (socket-cmd (format cmd port)) (sock (let ((default-directory (or project-dir default-directory))) (start-file-process-shell-command socket-process-name socket-buffer @@ -952,19 +954,14 @@ provided in `inf-clojure-socket-repl-startup-forms' or (setq-local inf-clojure-socket-callback (lambda () - (let ((with-process-repl-buffer-name (concat "*" repl-buffer-name "*"))) - (setq inf-clojure-socket-repl-type - repl-type - inf-clojure-custom-repl-name - repl-buffer-name - repl-buffer - (get-buffer-create with-process-repl-buffer-name)) - (inf-clojure-connect host port) - (with-current-buffer with-process-repl-buffer-name - (setq inf-clojure-socket-buffer socket-buffer)) - (set-process-sentinel - (get-buffer-process (get-buffer with-process-repl-buffer-name)) - #'inf-clojure-socket-repl-sentinel))))) + (let* ((inf-clojure-custom-repl-type repl-type) + (inf-clojure--suppress-connect-message-p t) + (created-repl-buffer (inf-clojure-connect host port))) + (with-current-buffer (get-buffer created-repl-buffer) + (setq-local inf-clojure-socket-buffer socket-buffer) + (set-process-sentinel + (get-buffer-process (current-buffer)) + #'inf-clojure-socket-repl-sentinel)))))) (set-process-filter sock #'inf-clojure-socket-filter) (message "Starting %s socket REPL server at %s:%d with %s" repl-type host port socket-cmd))) From 6464c889dae7493a9a31365c463ea229d1b61a31 Mon Sep 17 00:00:00 2001 From: dan sutton Date: Fri, 7 Apr 2023 14:40:20 -0500 Subject: [PATCH 2/5] Don't use dynamic var to suppress startup message --- inf-clojure.el | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/inf-clojure.el b/inf-clojure.el index 03dbda8..193bb96 100644 --- a/inf-clojure.el +++ b/inf-clojure.el @@ -521,9 +521,6 @@ Should be a symbol that is a key in `inf-clojure-repl-features'." (defvar inf-clojure-custom-repl-name nil "A string to be used as the repl buffer name.") -(defvar inf-clojure--suppress-connect-message-p nil - "A boolean of whether to suppress the message on connect.") - (defun inf-clojure--whole-comment-line-p (string) "Return non-nil iff STRING is a whole line semicolon comment." (string-match-p "^\s*;" string)) @@ -801,7 +798,7 @@ The name is simply the final segment of the path." (file-name-nondirectory (directory-file-name dir))) ;;;###autoload -(defun inf-clojure (cmd) +(defun inf-clojure (cmd &optional suppress-message) "Run an inferior Clojure process, input and output via buffer `*inf-clojure*'. If there is a process already running in `*inf-clojure*', just switch to that buffer. @@ -815,6 +812,9 @@ and `inf-clojure-custom-startup' if those are set. Use a prefix to prevent using these when they are set. +Prints a message that it has connected to the host and port +unless SUPPRESS-MESSAGE is truthy. + Runs the hooks from `inf-clojure-mode-hook' (after the `comint-mode-hook' is run). \(Type \\[describe-mode] in the process buffer for a list of commands.)" @@ -843,7 +843,7 @@ process buffer for a list of commands.)" inf-clojure-custom-repl-type) (car (rassoc cmd inf-clojure-startup-forms)) (inf-clojure--prompt-repl-type)))) - (unless inf-clojure--suppress-connect-message-p + (unless suppress-message (message "Starting Clojure REPL via `%s'..." cmd)) (with-current-buffer (apply #'make-comint process-buffer-name (car cmdlist) nil (cdr cmdlist)) @@ -858,12 +858,14 @@ process buffer for a list of commands.)" (pop-to-buffer repl-buffer-name)) repl-buffer-name)) -;;;###autoload -(defun inf-clojure-connect (host port) +;;;###autol +(defun inf-clojure-connect (host port &optional suppress-message) "Connect to a running socket REPL server via `inf-clojure'. -HOST is the host the process is running on, PORT is where it's listening." +HOST is the host the process is running on, PORT is where it's +listening. SUPPRESS-MESSAGE is optional and if truthy will +prevent showing the startup message." (interactive "shost: \nnport: ") - (inf-clojure (cons host port))) + (inf-clojure (cons host port) suppress-message)) (defvar-local inf-clojure-socket-callback nil "Used to transfer state between the socket process buffer & REPL buffer.") @@ -927,7 +929,7 @@ If left as nil a random port will be selected between 5500-6000." CMD is the command line instruction used to start the socket REPL. It should be a string with \"%d\" in it to take a random port. Set `inf-clojure-custom-startup' or choose from the -defaults provided in `inf-clojure-socket-repl-startup-forms'" +defaults provided in `inf-clojure-socket-repl-startup-forms'." (interactive (list (or (unless current-prefix-arg inf-clojure-custom-startup) (completing-read "Select Clojure socket REPL startup command: " @@ -955,8 +957,7 @@ defaults provided in `inf-clojure-socket-repl-startup-forms'" inf-clojure-socket-callback (lambda () (let* ((inf-clojure-custom-repl-type repl-type) - (inf-clojure--suppress-connect-message-p t) - (created-repl-buffer (inf-clojure-connect host port))) + (created-repl-buffer (inf-clojure-connect host port :suppress-message))) (with-current-buffer (get-buffer created-repl-buffer) (setq-local inf-clojure-socket-buffer socket-buffer) (set-process-sentinel From 7dd81e86ee35ef1f98f78b9a8141dc0fc5ac7fb8 Mon Sep 17 00:00:00 2001 From: dan sutton Date: Fri, 7 Apr 2023 14:43:30 -0500 Subject: [PATCH 3/5] some lint cleanups --- inf-clojure.el | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/inf-clojure.el b/inf-clojure.el index 193bb96..1d27cc2 100644 --- a/inf-clojure.el +++ b/inf-clojure.el @@ -609,7 +609,9 @@ This should usually be a combination of `inf-clojure-prompt' and :package-version '(inf-clojure . "2.0.0")) (defcustom inf-clojure-auto-mode t - "When non-nil, automatically enable inf-clojure-minor-mode for all Clojure buffers." + "Automatically enable inf-clojure-minor-mode. +All buffers will automatically be in `inf-clojure-minor-mode' +unless set to nil.." :type 'boolean :safe #'booleanp :package-version '(inf-clojure . "3.1.0")) @@ -895,7 +897,7 @@ OUTPUT is the latest data received from the process" (when inf-clojure-socket-callback (funcall inf-clojure-socket-callback))))))) -(defun inf-clojure-socket-repl-sentinel (process event) +(defun inf-clojure-socket-repl-sentinel (process _event) "Ensures socket REPL are cleaned up when the REPL buffer is closed. PROCESS is the process object that is connected to a socket REPL. From bdcca2ba0c12fba818bb7c690c377d69dab029f0 Mon Sep 17 00:00:00 2001 From: dan sutton Date: Fri, 7 Apr 2023 14:47:35 -0500 Subject: [PATCH 4/5] remove extra period (whoops) --- inf-clojure.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inf-clojure.el b/inf-clojure.el index 1d27cc2..5cbe1fd 100644 --- a/inf-clojure.el +++ b/inf-clojure.el @@ -611,7 +611,7 @@ This should usually be a combination of `inf-clojure-prompt' and (defcustom inf-clojure-auto-mode t "Automatically enable inf-clojure-minor-mode. All buffers will automatically be in `inf-clojure-minor-mode' -unless set to nil.." +unless set to nil." :type 'boolean :safe #'booleanp :package-version '(inf-clojure . "3.1.0")) From 46a3ab7766c73593d6477f697be83ea22a066c84 Mon Sep 17 00:00:00 2001 From: dan sutton Date: Fri, 7 Apr 2023 14:52:21 -0500 Subject: [PATCH 5/5] clarify docstring --- inf-clojure.el | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inf-clojure.el b/inf-clojure.el index 5cbe1fd..4c5e018 100644 --- a/inf-clojure.el +++ b/inf-clojure.el @@ -610,8 +610,8 @@ This should usually be a combination of `inf-clojure-prompt' and (defcustom inf-clojure-auto-mode t "Automatically enable inf-clojure-minor-mode. -All buffers will automatically be in `inf-clojure-minor-mode' -unless set to nil." +All buffers in `clojure-mode' will automatically be in +`inf-clojure-minor-mode' unless set to nil." :type 'boolean :safe #'booleanp :package-version '(inf-clojure . "3.1.0"))