Skip to content

Commit

Permalink
If current buffer is a TRAMP buffer, use its filename for SSH tunnel
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadim Rodionov committed Nov 8, 2022
1 parent f56fb01 commit d35792e
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions nrepl-client.el
Original file line number Diff line number Diff line change
Expand Up @@ -569,14 +569,21 @@ If NO-ERROR is non-nil, show messages instead of throwing an error."

(defun nrepl--ssh-tunnel-connect (host port)
"Connect to a remote machine identified by HOST and PORT through SSH tunnel."
(message "[nREPL] Establishing SSH tunneled connection to %s:%s ..." host port)
(let* ((remote-dir (if host (format "/ssh:%s:" host) default-directory))
(let* ((current-buf (buffer-file-name))
(tramp-file-regexp "/ssh:\\(.+@\\)?\\(.+?\\)\\(:\\|#\\).+")
(remote-dir (cond
((string-match tramp-file-regexp current-buf) current-buf)
(host (format "/ssh:%s:" host))
(t default-directory)))
(remote-host (or (match-string 2 current-buf)
host))
(ssh (or (executable-find "ssh")
(error "[nREPL] Cannot locate 'ssh' executable")))
(cmd (nrepl--ssh-tunnel-command ssh remote-dir port))
(tunnel-buf (nrepl-tunnel-buffer-name
`((:host ,host) (:port ,port))))
`((:host ,remote-host) (:port ,port))))
(tunnel (start-process-shell-command "nrepl-tunnel" tunnel-buf cmd)))
(message "[nREPL] Establishing SSH tunneled connection to %s:%s ..." remote-host port)
(process-put tunnel :waiting-for-port t)
(set-process-filter tunnel (nrepl--ssh-tunnel-filter port))
(while (and (process-live-p tunnel)
Expand All @@ -589,7 +596,7 @@ If NO-ERROR is non-nil, show messages instead of throwing an error."
(thread-first
endpoint
(plist-put :tunnel tunnel)
(plist-put :remote-host host))))))
(plist-put :remote-host remote-host))))))

(defun nrepl--ssh-tunnel-command (ssh dir port)
"Command string to open SSH tunnel to the host associated with DIR's PORT."
Expand All @@ -598,11 +605,12 @@ If NO-ERROR is non-nil, show messages instead of throwing an error."
;; forwarding is set up, which is used to synchronise on, so that
;; the port forwarding is up when we try to connect.
(format-spec
"%s -v -N -L %p:localhost:%p %u'%h'"
"%s -v -N -L %p:localhost:%p %u'%h' %n"
`((?s . ,ssh)
(?p . ,port)
(?h . ,v-host)
(?u . ,(if v-user (format "-l '%s' " v-user) ""))))))
(?u . ,(if v-user (format "-l '%s' " v-user) ""))
(?n . ,(if v-port (format "-p '%s' " v-port) ""))))))

(autoload 'comint-watch-for-password-prompt "comint" "(autoload).")

Expand Down

0 comments on commit d35792e

Please sign in to comment.