Skip to content

Commit

Permalink
Support controlmaster paths
Browse files Browse the repository at this point in the history
  • Loading branch information
masasam committed Jun 15, 2019
1 parent af5bd50 commit 6790bd2
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion helm-tramp.el
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

;; Author: Masashı Mıyaura
;; URL: https://github.com/masasam/emacs-helm-tramp
;; Version: 1.2.7
;; Version: 1.3.7
;; Package-Requires: ((emacs "24.3") (helm "2.0"))

;; This program is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -47,6 +47,21 @@
:group 'helm-tramp
:type 'string)

(defcustom helm-tramp-control-master nil
"If you want to put out a candidate for completion from ssh controlmaster, please set to t."
:group 'helm-tramp
:type 'string)

(defcustom helm-tramp-control-master-path "~/.ssh/"
"Path where ssh controlmaster exists."
:group 'helm-tramp
:type 'string)

(defcustom helm-tramp-control-master-prefix "master-"
"Prefix of ssh controlmaster."
:group 'helm-tramp
:type 'string)

(defcustom helm-tramp-pre-command-hook nil
"Hook run before `helm-tramp'.
The hook is called with one argument that is non-nil."
Expand Down Expand Up @@ -112,6 +127,24 @@ Kill all remote buffers."
(setq include-file (concat (file-name-as-directory "~/.ssh") include-file)))
(when (file-exists-p include-file)
(setq hosts (append hosts (helm-tramp--candidates include-file))))))
(when helm-tramp-control-master
(let ((files (helm-tramp--directory-files
(expand-file-name
helm-tramp-control-master-path)
helm-tramp-control-master-prefix)))
(dolist (controlmaster files)
(let ((file (file-name-nondirectory controlmaster)))
(when (string-match
(concat helm-tramp-control-master-prefix "\\(.+?\\)@\\(.+?\\):.+?$")
file)
(setq hostuser (match-string 1 file))
(setq hostname (match-string 2 file))
(push
(concat "/" tramp-default-method ":" hostuser "@" hostname ":")
hosts)
(push
(concat "/ssh:" hostuser "@" hostname "|sudo:root@" hostname ":/")
hosts))))))
(when (require 'docker-tramp nil t)
(cl-loop for line in (cdr (ignore-errors (apply #'process-lines "docker" (list "ps"))))
for info = (reverse (split-string line "[[:space:]]+" t))
Expand All @@ -137,6 +170,29 @@ Kill all remote buffers."
(push (concat "/sudo:root@localhost:" helm-tramp-localhost-directory) hosts)
(reverse hosts)))

(defun helm-tramp--directory-files (dir regexp)
"Return list of all files under DIR that have file names matching REGEXP."
(let ((result nil)
(files nil)
(tramp-mode (and tramp-mode (file-remote-p (expand-file-name dir)))))
(dolist (file (sort (file-name-all-completions "" dir)
'string<))
(unless (member file '("./" "../"))
(if (not (helm-tramp--directory-name-p file))
(when (string-match regexp file)
(push (expand-file-name file dir) files)))))
(nconc result (nreverse files))))

(defsubst helm-tramp--directory-name-p (name)
"Return non-nil if NAME ends with a directory separator character."
(let ((len (length name))
(lastc ?.))
(if (> len 0)
(setq lastc (aref name (1- len))))
(or (= lastc ?/)
(and (memq system-type '(windows-nt ms-dos))
(= lastc ?\\)))))

(defun helm-tramp-open (path)
"Tramp open with PATH."
(find-file path))
Expand Down

0 comments on commit 6790bd2

Please sign in to comment.