Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiple connections for Emacs-26 and below #34

Merged
merged 1 commit into from
Aug 24, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 16 additions & 21 deletions monroe.el
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
;;; Code:

(require 'comint)
(eval-when-compile
(require 'cl))
(require 'cl-macs)
(require 'subr-x)

(defgroup monroe nil
"Interaction with the nREPL Server."
Expand Down Expand Up @@ -130,8 +130,8 @@ to the one used on nrepl side.")
;; stolen from nrepl.el.
(defmacro monroe-dbind-response (response keys &rest body)
"Destructure an nREPL response dict."
`(let ,(loop for key in keys
collect `(,key (cdr (assoc ,(format "%s" key) ,response))))
`(let ,(cl-loop for key in keys
collect `(,key (cdr (assoc ,(format "%s" key) ,response))))
,@body))

;;; Bencode
Expand Down Expand Up @@ -192,18 +192,14 @@ starting with 'd' and ending with 'e'."
(setq result (cons (monroe-bdecode-buffer) result)))
(nreverse result))))

(defun monroe-write-message (process message)
"Send message to given process."
(process-send-string process message))

(defun monroe-send-request (request callback)
"Send request as elisp object and assign callback to
be called when reply is received."
(let* ((id (number-to-string (incf monroe-requests-counter)))
(let* ((id (number-to-string (cl-incf monroe-requests-counter)))
(message (append (list "id" id) request))
(bmessage (monroe-encode message)))
(puthash id callback monroe-requests)
(monroe-write-message (monroe-connection) bmessage)))
(process-send-string (monroe-connection) bmessage)))

(defun monroe-clear-request-table ()
"Erases current request table."
Expand All @@ -216,7 +212,7 @@ be called when reply is received."

;;; nrepl messages we knows about

(defun monroe-send-hello (proc callback)
(defun monroe-send-hello (callback)
"Initiate nREPL session."
(monroe-send-request '("op" "clone") callback))

Expand Down Expand Up @@ -325,11 +321,10 @@ monroe-repl-buffer."
;; This 'ignore-errors' is a hard hack here since 'accept-process-output' will call filter
;; which will be this function causing Emacs to hit max stack size limit.
(ignore-errors
(when (eq ?e (aref string (- (length string) 1)))
(unless (accept-process-output process 0.01)
(while (> (buffer-size) 1)
(dolist (response (monroe-net-decode))
(monroe-dispatch response))))))))
(when (eq ?e (aref string (- (length string) 1)))
(unless (accept-process-output process 0.01)
(while (> (buffer-size) 1)
(mapc #'monroe-dispatch (monroe-net-decode))))))))

(defun monroe-new-session-handler (process)
"Returns callback that is called when new connection is established."
Expand Down Expand Up @@ -359,7 +354,7 @@ monroe-repl-buffer."

(defun monroe-extract-host (buff-name)
"Take host from monroe buffers."
(first (last (split-string (substring buff-name 1 -1) " "))))
(car (last (split-string (substring buff-name 1 -1) " "))))

(defun monroe-repl-buffer ()
"Returns right monroe buffer."
Expand All @@ -385,9 +380,9 @@ monroe-repl-buffer."
(defun monroe-connect (host-and-port)
"Connect to remote endpoint using provided hostname and port."
(let* ((hp (split-string (monroe-strip-protocol host-and-port) ":"))
(host (monroe-valid-host-string (first hp) "localhost"))
(host (monroe-valid-host-string (car hp) "localhost"))
(port (string-to-number
(monroe-valid-host-string (second hp) "7888")))
(monroe-valid-host-string (cadr hp) "7888")))
(name (concat "*monroe-connection: " host-and-port "*")))
(when (get-buffer name) (monroe-disconnect))
(message "Connecting to nREPL host on '%s:%d'..." host port)
Expand All @@ -396,7 +391,7 @@ monroe-repl-buffer."
(set-process-filter process 'monroe-net-filter)
(set-process-sentinel process 'monroe-sentinel)
(set-process-coding-system process 'utf-8-unix 'utf-8-unix)
(monroe-send-hello process (monroe-new-session-handler (process-buffer process)))
(monroe-send-hello (monroe-new-session-handler (process-buffer process)))
process)))

(defun monroe-disconnect ()
Expand Down Expand Up @@ -492,7 +487,7 @@ inside a container.")
(when (member "done" status)
(remhash id monroe-requests))
(when value
(destructuring-bind (file line)
(cl-destructuring-bind (file line)
(append (car (read-from-string value)) nil)
(monroe-jump-find-file (funcall monroe-translate-path-function file))
(when line
Expand Down