@@ -122,33 +122,20 @@ Setting this to nil disables the timeout functionality."
122
122
:type 'integer
123
123
:group 'nrepl )
124
124
125
- (defcustom nrepl-connection-endpoint
126
- 'nrepl-connection-ssh-tunnel
127
- " A function that is called to determine command that will be run
128
- once an nrepl server process is running. Used to set up an ssh tunnel
129
- on remote connections.
130
-
131
- The arguments are dir and port. The return value
132
- should be an `plist` of the form
133
- (:proc-buffer-name \" *buf*\" :hostname \" hostname\" :port 1234)"
134
- :type 'function
135
- :group 'nrepl )
136
-
137
125
(defcustom nrepl-hide-special-buffers nil
138
126
" Control the display of some special buffers in buffer switching commands.
139
127
When true some special buffers like the connection and the server
140
128
buffer will be hidden."
141
129
:type 'boolean
142
130
:group 'nrepl )
143
131
144
-
145
132
146
133
; ;; nREPL Buffer Names
147
134
148
135
(defconst nrepl-repl-buffer-name-template " *cider-repl%s*" )
149
136
(defconst nrepl-connection-buffer-name-template " *nrepl-connection%s*" )
150
137
(defconst nrepl-server-buffer-name-template " *nrepl-server%s*" )
151
- (defconst nrepl-on-connection- buffer-name-template " *nrepl-on-connection %s*" )
138
+ (defconst nrepl-tunnel- buffer-name-template " *nrepl-tunnel %s*" )
152
139
153
140
(defun nrepl-format-buffer-name-template (buffer-name-template designation )
154
141
" Apply the DESIGNATION to the corresponding BUFFER-NAME-TEMPLATE."
@@ -160,8 +147,8 @@ buffer will be hidden."
160
147
(defun nrepl-make-buffer-name (buffer-name-template &optional project-dir host port )
161
148
" Generate a buffer name using BUFFER-NAME-TEMPLATE.
162
149
163
- If not supplied PROJECT-DIR, PORT and HOST default to the buffer local value of the
164
- `nrepl-project-dir' and `nrepl-endpoint' .
150
+ If not supplied PROJECT-DIR, PORT and HOST default to the buffer local
151
+ value of the `nrepl-project-dir' and `nrepl-endpoint' .
165
152
166
153
The name will include the project name if available or the endpoint host if
167
154
it is not. The name will also include the connection port if
@@ -193,11 +180,11 @@ PROJECT-DIR, HOST and PORT are as in `nrepl-make-buffer-name'."
193
180
(nrepl-make-buffer-name nrepl-server-buffer-name-template
194
181
project-dir host port)))
195
182
196
- (defun nrepl-on-connection -buffer-name (&optional project-dir host port )
197
- " Return the name of the on-connection buffer.
183
+ (defun nrepl-tunnel -buffer-name (&optional project-dir host port )
184
+ " Return the name of the tunnel buffer.
198
185
PROJECT-DIR, HOST and PORT are as in `nrepl-make-buffer-name' ."
199
186
(nrepl--make-hidden-name
200
- (nrepl-make-buffer-name nrepl-on-connection -buffer-name-template
187
+ (nrepl-make-buffer-name nrepl-tunnel -buffer-name-template
201
188
project-dir host port)))
202
189
203
190
@@ -209,7 +196,7 @@ PROJECT-DIR, HOST and PORT are as in `nrepl-make-buffer-name'."
209
196
(defvar-local nrepl-repl-buffer nil )
210
197
(defvar-local nrepl-endpoint nil )
211
198
(defvar-local nrepl-project-dir nil )
212
- (defvar-local nrepl-on-connection -buffer nil )
199
+ (defvar-local nrepl-tunnel -buffer nil )
213
200
214
201
(defvar-local nrepl-session nil
215
202
" Current nREPL session id." )
@@ -501,19 +488,18 @@ nil, pick them from the value returned by `nrepl-connection-endpoint'. If
501
488
REPLP is non-nil create a client connection which is associated with a repl
502
489
buffer. When non-nil, SERVER-PROC must be a running nrepl server process
503
490
within Emacs. Return the newly created client connection process."
504
- (let* ((endpoint (if (functionp nrepl-connection-endpoint)
505
- (funcall nrepl-connection-endpoint directory port)
506
- (nrepl--default-endpoint directory port)))
491
+ (let* ((endpoint (unless (and host port)
492
+ (nrepl-connection-endpoint directory port)))
507
493
(directory (or directory default-directory))
508
494
(host (or host (plist-get endpoint :hostname )))
509
495
(port (or port (plist-get endpoint :port )))
510
- (proc-buffer-name (plist-get endpoint :proc-buffer-name ))
511
496
(server-buf (and server-proc
512
497
(buffer-name (process-buffer server-proc))))
513
498
(client-buf (if replp
514
499
(cider-repl-create directory host port)
515
500
(nrepl-create-connection-buffer directory host port)))
516
501
(client-proc (open-network-stream " nrepl" client-buf host port))
502
+ (tunnel-proc (plist-get endpoint :proc ))
517
503
(nrepl-connection-dispatch client-buf))
518
504
519
505
(set-process-filter client-proc 'nrepl-client-filter )
@@ -532,10 +518,10 @@ within Emacs. Return the newly created client connection process."
532
518
; ; fixme: repl and connection buffers are the same thing
533
519
nrepl-connection-buffer client-buf
534
520
nrepl-repl-buffer (when replp client-buf)
535
- nrepl-on-connection- buffer proc-buffer-name
521
+ nrepl-tunnel- buffer ( and tunnel- proc ( process -buffer tunnel-proc))
536
522
nrepl-pending-requests (make-hash-table :test 'equal )
537
523
nrepl-completed-requests (make-hash-table :test 'equal )))
538
-
524
+
539
525
(nrepl-make-connection-default client-buf)
540
526
541
527
; ; Everything is set. We are ready to send requests.
@@ -844,52 +830,43 @@ Return a newly created process."
844
830
(error " Leiningen 2.x is required by CIDER " ))
845
831
(t (error " Could not start nREPL server: %s " problem)))))
846
832
847
- (defun nrepl-connection-ssh-tunnel (dir port )
848
- " Return an endpoint for SSH tunnel to project DIR stack, and PORT port.
849
- If DIR is remote, then attempt to open an SSH tunnel to port. If
850
- the ssh executable is not found on the stack, then fall back to
851
- specifying a direct conneciton."
852
- ; ; this abuses the -v option for ssh to get output when the port
853
- ; ; forwarding is set up, which is used to synchronise on, so that
854
- ; ; the port forwarding is up when we try to connect.
833
+ (defun nrepl-connection-endpoint (dir port )
834
+ " Return a connection endpoint.
835
+ The returned endpoint is a `plist` of the form:
836
+
837
+ (:proc PROCESS :hostname \" hostname\" :port 1234)
838
+
839
+ If DIR is local :proc is nil, :hostname is \" localhost\" and :port is PORT.
840
+
841
+ If DIR is remote and `ssh' executable has been found, attempt to start an
842
+ SSH tunnel and return it as :proc. If no `ssh' executable has been found,
843
+ fall back to specifying a direct connection to the remote host."
855
844
(if (file-remote-p dir)
856
845
(let ((ssh (executable-find " ssh" )))
857
846
(if ssh
858
847
; ; run cmd in a local shell
859
848
(let* ((cmd (nrepl--ssh-tunnel-command ssh dir port))
860
- (on-connection-buffer-name (nrepl-on-connection-buffer-name))
861
849
(proc (start-process-shell-command
862
- " nrepl-on-connection"
863
- on-connection-buffer-name
864
- cmd))
865
- (on-connection-buffer (get-buffer
866
- on-connection-buffer-name)))
867
- (with-current-buffer on-connection-buffer-name
868
- (setq-local nrepl-wait-for-port t ))
850
+ " nrepl-tunnel"
851
+ (nrepl-tunnel-buffer-name)
852
+ cmd)))
853
+ (process-put proc :waiting-for-port t )
869
854
(set-process-filter proc (nrepl--ssh-tunnel-filter port))
870
- (while (and (buffer-local-value 'nrepl-wait-for-port
871
- on-connection-buffer)
872
- (process-live-p proc))
855
+ (while (and (process-live-p proc)
856
+ (process-get proc :waiting-for-port ))
873
857
(accept-process-output nil 0.005 ))
874
858
(unless (process-live-p proc)
875
859
(message " SSH port forwarding failed " ))
876
- (list :hostname " localhost" :port port
877
- :proc-buffer-name on-connection-buffer-name))
878
- (nrepl--default-endpoint dir port)))
879
- (list :hostname " localhost" :port port :proc-buffer-name nil )))
880
-
881
- (defun nrepl--default-endpoint (dir port )
882
- " The endpoint for a repl in project DIR on PORT.
883
- Return a plist with :hostname, :port and :proc keys."
884
- (list :hostname (if (file-remote-p dir)
885
- tramp-current-host
886
- " localhost" )
887
- :port port
888
- :proc-buffer-name nil ))
860
+ (list :hostname " localhost" :port port :proc proc))
861
+ (list :hostname tramp-current-host :port port :proc nil )))
862
+ (list :hostname " localhost" :port port :proc nil )))
889
863
890
864
(defun nrepl--ssh-tunnel-command (ssh dir port )
891
865
" Command string to open SSH tunnel to the host associated with DIR's PORT."
892
866
(with-parsed-tramp-file-name dir nil
867
+ ; ; this abuses the -v option for ssh to get output when the port
868
+ ; ; forwarding is set up, which is used to synchronise on, so that
869
+ ; ; the port forwarding is up when we try to connect.
893
870
(format-spec
894
871
" %s -v -N -L %p:localhost:%p %u'%h'"
895
872
`((?s . , ssh )
@@ -901,17 +878,16 @@ Return a plist with :hostname, :port and :proc keys."
901
878
" Return a filter function for waiting on PORT to appear in output."
902
879
(let ((port-string (format " LOCALHOST:%s " port)))
903
880
(lambda (proc string )
881
+ (when (string-match port-string string)
882
+ (process-put proc :waiting-for-port nil ))
904
883
(when (buffer-live-p (process-buffer proc))
905
884
(with-current-buffer (process-buffer proc)
906
885
(let ((moving (= (point ) (process-mark proc))))
907
886
(save-excursion
908
887
(goto-char (process-mark proc))
909
888
(insert string)
910
889
(set-marker (process-mark proc) (point )))
911
- (if moving (goto-char (process-mark proc))))))
912
- (when (string-match port-string string)
913
- (with-current-buffer (process-buffer proc)
914
- (setq nrepl-wait-for-port nil ))))))
890
+ (if moving (goto-char (process-mark proc)))))))))
915
891
916
892
917
893
; ;; Utilities
@@ -1091,19 +1067,19 @@ Moves CONNECITON-BUFFER to the front of `nrepl-connection-list'."
1091
1067
(nrepl--connections-refresh))
1092
1068
(message " Not in an nREPL REPL buffer. " )))
1093
1069
1094
- (defun nrepl--close-connection-buffer (connection -buffer )
1095
- " Closes CONNECTION -BUFFER, removing it from `nrepl-connection-list' .
1070
+ (defun nrepl--close-connection-buffer (conn -buffer )
1071
+ " Closes CONN -BUFFER, removing it from `nrepl-connection-list' .
1096
1072
Also closes associated REPL and server buffers."
1097
- (let ((nrepl-connection-dispatch connection -buffer))
1098
- (let ((buffer (get-buffer connection -buffer)))
1073
+ (let ((nrepl-connection-dispatch conn -buffer))
1074
+ (let ((buffer (get-buffer conn -buffer)))
1099
1075
(setq nrepl-connection-list
1100
1076
(delq (buffer-name buffer) nrepl-connection-list))
1101
1077
(when (buffer-live-p buffer)
1102
- (dolist (buf-name `(,(buffer-local-value 'nrepl-server-buffer buffer)
1103
- ,(buffer-local-value 'nrepl-on-connection -buffer buffer)
1104
- , buffer ))
1105
- (when buf-name
1106
- (cider--close-buffer buf-name )))))))
1078
+ (dolist (buf `(,(buffer-local-value 'nrepl-server-buffer buffer)
1079
+ ,(buffer-local-value 'nrepl-tunnel -buffer buffer)
1080
+ , buffer ))
1081
+ (when buf
1082
+ (cider--close-buffer buf)))))))
1107
1083
1108
1084
1109
1085
; ;; Connection Browser
0 commit comments