Skip to content

Commit

Permalink
Cleanup socket repl startup (#212)
Browse files Browse the repository at this point in the history
This simplifies the socket repl startup code and addresses some lint warnings.
  • Loading branch information
dpsutton authored Apr 8, 2023
1 parent 8ad2242 commit b153e51
Showing 1 changed file with 39 additions and 39 deletions.
78 changes: 39 additions & 39 deletions inf-clojure.el
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,9 @@ 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.")

(defun inf-clojure--whole-comment-line-p (string)
"Return non-nil iff STRING is a whole line semicolon comment."
(string-match-p "^\s*;" string))
Expand Down Expand Up @@ -606,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 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"))
Expand Down Expand Up @@ -795,7 +800,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.
Expand All @@ -809,6 +814,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.)"
Expand All @@ -833,12 +841,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 suppress-message
(message "Starting Clojure REPL via `%s'..." cmd))
(with-current-buffer (apply #'make-comint
process-buffer-name (car cmdlist) nil (cdr cmdlist))
(inf-clojure-mode)
Expand All @@ -849,14 +857,17 @@ 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)
;;;###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.")
Expand All @@ -882,12 +893,11 @@ 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)))))))

(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.
Expand Down Expand Up @@ -917,11 +927,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: "
Expand All @@ -937,13 +947,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
Expand All @@ -952,19 +958,13 @@ 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)
(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
(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)))

Expand Down

0 comments on commit b153e51

Please sign in to comment.