Skip to content

Commit

Permalink
If current buffer is a TRAMP buffer, use it's filename for SSH tunnel
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadim Rodionov committed Nov 5, 2022
1 parent f56fb01 commit b0948e9
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 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 Down

0 comments on commit b0948e9

Please sign in to comment.