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 d69abe5 commit 1eb594f
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion counsel-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-counsel-tramp
;; Version: 0.6.3
;; Version: 0.7.3
;; Package-Requires: ((emacs "24.3") (counsel "0.10"))

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

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

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

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

(defcustom counsel-tramp-pre-command-hook nil
"Hook run before `counsel-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 (counsel-tramp--candidates include-file))))))
(when counsel-tramp-control-master
(let ((files (counsel-tramp--directory-files
(expand-file-name
counsel-tramp-control-master-path)
counsel-tramp-control-master-prefix)))
(dolist (controlmaster files)
(let ((file (file-name-nondirectory controlmaster)))
(when (string-match
(concat counsel-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:" counsel-tramp-localhost-directory) hosts)
(reverse hosts)))

(defun counsel-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 (counsel-tramp--directory-name-p file))
(when (string-match regexp file)
(push (expand-file-name file dir) files)))))
(nconc result (nreverse files))))

(defsubst counsel-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 ?\\)))))

;;;###autoload
(defun counsel-tramp ()
"Open your ~/.ssh/config with counsel interface.
Expand Down

0 comments on commit 1eb594f

Please sign in to comment.