]> code.delx.au - gnu-emacs-elpa/commitdiff
Make the slash more magic during file name completion
authorOleh Krehel <ohwoeowho@gmail.com>
Mon, 18 Apr 2016 08:01:26 +0000 (10:01 +0200)
committerOleh Krehel <ohwoeowho@gmail.com>
Mon, 18 Apr 2016 08:07:57 +0000 (10:07 +0200)
* ivy.el (ivy--magic-file-slash): Extract from `ivy--exhibit'.
(ivy--exhibit): Update.

Update to the behavior: the slash ("/") will enter a directory even if
its name isn't completely typed out if either:

1. It's the only candidate.
2. The candidate index isn't 0, i.e. "C-n" has been typed at least once.
3. The input isn't "/".

The above rules still allow to keep the old behavior with "//" moving to
root and "/ssh:" opening tramp.

This is an experimental feature, please report if it breaks someone's
workflow.

Re #321.

ivy.el

diff --git a/ivy.el b/ivy.el
index e3e05b71d11668272c9e8ccfe1efb7b2cbd4f822..e07fa53ee664168353d2fb6e1fed248077a895a6 100644 (file)
--- a/ivy.el
+++ b/ivy.el
@@ -1944,6 +1944,25 @@ depending on the number of candidates."
             (cl-sort (copy-sequence collection) sort-fn)
           collection)))))
 
+(defun ivy--magic-file-slash ()
+  (cond ((member ivy-text ivy--all-candidates)
+         (ivy--cd (expand-file-name ivy-text ivy--directory)))
+        ((string-match "//\\'" ivy-text)
+         (if (and default-directory
+                  (string-match "\\`[[:alpha:]]:/" default-directory))
+             (ivy--cd (match-string 0 default-directory))
+           (ivy--cd "/")))
+        ((string-match "[[:alpha:]]:/\\'" ivy-text)
+         (let ((drive-root (match-string 0 ivy-text)))
+           (when (file-exists-p drive-root)
+             (ivy--cd drive-root))))
+        ((and (or (> ivy--index 0)
+                  (= ivy--length 1)
+                  (not (string= ivy-text "/")))
+              (let ((default-directory ivy--directory))
+                (file-directory-p ivy--current)))
+         (ivy--cd (expand-file-name ivy--current ivy--directory)))))
+
 (defun ivy--exhibit ()
   "Insert Ivy completions display.
 Should be run via minibuffer `post-command-hook'."
@@ -1966,17 +1985,7 @@ Should be run via minibuffer `post-command-hook'."
              (ivy--format ivy--all-candidates))))
       (cond (ivy--directory
              (if (string-match "/\\'" ivy-text)
-                 (if (member ivy-text ivy--all-candidates)
-                     (ivy--cd (expand-file-name ivy-text ivy--directory))
-                   (when (string-match "//\\'" ivy-text)
-                     (if (and default-directory
-                              (string-match "\\`[[:alpha:]]:/" default-directory))
-                         (ivy--cd (match-string 0 default-directory))
-                       (ivy--cd "/")))
-                   (when (string-match "[[:alpha:]]:/$" ivy-text)
-                     (let ((drive-root (match-string 0 ivy-text)))
-                       (when (file-exists-p drive-root)
-                         (ivy--cd drive-root)))))
+                 (ivy--magic-file-slash)
                (if (string-match "\\`~\\'" ivy-text)
                    (ivy--cd (expand-file-name "~/")))))
             ((eq (ivy-state-collection ivy-last) 'internal-complete-buffer)