branch: master
commit d45ee5459a72a664f29ab554d54e7861360d46e9
Author: Oleh Krehel <[email protected]>
Commit: Oleh Krehel <[email protected]>
Make the slash more magic during file name completion
* 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 | 31 ++++++++++++++++++++-----------
1 file changed, 20 insertions(+), 11 deletions(-)
diff --git a/ivy.el b/ivy.el
index e3e05b7..e07fa53 100644
--- 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)