Skip to content

Commit 59b3534

Browse files
author
Vadim Rodionov
committed
If current buffer is a TRAMP buffer, use it's filename for SSH tunnel
1 parent f56fb01 commit 59b3534

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

nrepl-client.el

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -569,14 +569,21 @@ If NO-ERROR is non-nil, show messages instead of throwing an error."
569569

570570
(defun nrepl--ssh-tunnel-connect (host port)
571571
"Connect to a remote machine identified by HOST and PORT through SSH tunnel."
572-
(message "[nREPL] Establishing SSH tunneled connection to %s:%s ..." host port)
573-
(let* ((remote-dir (if host (format "/ssh:%s:" host) default-directory))
572+
(let* ((current-buf (buffer-file-name))
573+
(tramp-file-regexp "/ssh:\\(.+@\\)?\\(.+?\\)\\(:\\|#\\).+")
574+
(remote-dir (cond
575+
((string-match tramp-file-regexp current-buf) current-buf)
576+
(host (format "/ssh:%s:" host))
577+
(t default-directory)))
578+
(remote-host (or (match-string 2 current-buf)
579+
host))
574580
(ssh (or (executable-find "ssh")
575581
(error "[nREPL] Cannot locate 'ssh' executable")))
576582
(cmd (nrepl--ssh-tunnel-command ssh remote-dir port))
577583
(tunnel-buf (nrepl-tunnel-buffer-name
578-
`((:host ,host) (:port ,port))))
584+
`((:host ,remote-host) (:port ,port))))
579585
(tunnel (start-process-shell-command "nrepl-tunnel" tunnel-buf cmd)))
586+
(message "[nREPL] Establishing SSH tunneled connection to %s:%s ..." remote-host port)
580587
(process-put tunnel :waiting-for-port t)
581588
(set-process-filter tunnel (nrepl--ssh-tunnel-filter port))
582589
(while (and (process-live-p tunnel)
@@ -589,7 +596,7 @@ If NO-ERROR is non-nil, show messages instead of throwing an error."
589596
(thread-first
590597
endpoint
591598
(plist-put :tunnel tunnel)
592-
(plist-put :remote-host host))))))
599+
(plist-put :remote-host remote-host))))))
593600

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

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

0 commit comments

Comments
 (0)