]> code.delx.au - gnu-emacs-elpa/commitdiff
Improve TRAMP completion for ivy-mode
authorOleh Krehel <ohwoeowho@gmail.com>
Thu, 30 Apr 2015 09:25:09 +0000 (11:25 +0200)
committerOleh Krehel <ohwoeowho@gmail.com>
Thu, 30 Apr 2015 09:25:09 +0000 (11:25 +0200)
* ivy.el (ivy-build-tramp-name): New defun.
(ivy-alt-done): Use arcane TRAMP stuff to complete host names.

Re #59

ivy.el

diff --git a/ivy.el b/ivy.el
index 323fb8a959595158ff91e51a8d44f31b7ebf0bd0..d27953570d2f5af658e98950c34afeb2f33dffa8 100644 (file)
--- a/ivy.el
+++ b/ivy.el
@@ -224,6 +224,15 @@ When non-nil, it should contain one %d.")
                (setq ivy-exit 'done)))
     (exit-minibuffer)))
 
+(defun ivy-build-tramp-name (x)
+  "Reconstruct X into a path.
+Is is a cons cell, related to `tramp-get-completion-function'."
+  (let ((user (car x))
+        (domain (cadr x)))
+    (if user
+        (concat user "@" domain)
+      domain)))
+
 (defun ivy-alt-done (&optional arg)
   "Exit the minibuffer with the selected candidate.
 When ARG is t, exit with current text, ignoring the candidates."
@@ -231,20 +240,30 @@ When ARG is t, exit with current text, ignoring the candidates."
   (if arg
       (ivy-immediate-done)
     (let (dir)
-      (if (and ivy--directory
-               (or
-                (and
-                 (not (string= ivy--current "./"))
-                 (cl-plusp ivy--length)
-                 (file-directory-p
-                  (setq dir (expand-file-name
-                             ivy--current ivy--directory))))
-                (prog1 (string-match ":" ivy-text)
-                  (setq dir ivy-text))))
-          (progn
-            (ivy--cd dir)
-            (ivy--exhibit))
-        (ivy-done)))))
+      (cond ((and ivy--directory
+                  (or
+                   (and
+                    (not (string= ivy--current "./"))
+                    (cl-plusp ivy--length)
+                    (file-directory-p
+                     (setq dir (expand-file-name
+                                ivy--current ivy--directory))))))
+             (ivy--cd dir)
+             (ivy--exhibit))
+            ((string-match "\\`/\\([^/]+\\)[:@]\\'" ivy-text)
+             (let ((full-method ivy-text)
+                   (method (match-string 1 ivy-text))
+                   res)
+               (dolist (x (tramp-get-completion-function method))
+                 (setq res (append res (funcall (car x) (cadr x)))))
+               (setq res (delq nil res))
+               (let ((host (ivy-read "Find File: "
+                                     (mapcar #'ivy-build-tramp-name res))))
+                 (when host
+                   (setq ivy--directory "/")
+                   (ivy--cd (concat full-method host ":"))))))
+            (t
+             (ivy-done))))))
 
 (defun ivy-partial-or-done ()
   "Complete the minibuffer text as much as possible.