emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] master 567ea7e 1/3: Ivy-mode now works better with `find-file'


From: Oleh Krehel
Subject: [elpa] master 567ea7e 1/3: Ivy-mode now works better with `find-file'
Date: Fri, 17 Apr 2015 08:11:58 +0000

branch: master
commit 567ea7e05bc0c36a944e1027b5703151050f4f14
Author: Oleh Krehel <address@hidden>
Commit: Oleh Krehel <address@hidden>

    Ivy-mode now works better with `find-file'
    
    * ivy.el (ivy-minibuffer-map): Bind "C-j" to visit a directory without
      exiting the minibuffer.
    (ivy--directory): New defvar.
    (ivy-done): Expand file names.
    (ivy-alt-done): New defun.
    (ivy-backward-delete-char): When completing file names, visit the parent 
dir.
    (ivy-read): Add the predicate argument, similar to `completing-read'.
    All code that uses `ivy-read' needs to be updated. Move the 
collection/predicate stuff here.
    (ivy-completing-read): Update.
    (ivy--insert-prompt): Display the current directory when completing file 
names.
---
 counsel.el |    6 ++--
 ivy.el     |   77 ++++++++++++++++++++++++++++++++++++++++++++++--------------
 swiper.el  |    1 +
 3 files changed, 63 insertions(+), 21 deletions(-)

diff --git a/counsel.el b/counsel.el
index fc62ade..e7787c0 100644
--- a/counsel.el
+++ b/counsel.el
@@ -5,7 +5,7 @@
 ;; Author: Oleh Krehel <address@hidden>
 ;; URL: https://github.com/abo-abo/swiper
 ;; Version: 0.1.0
-;; Package-Requires: ((emacs "24.1") (ivy "0.2.1"))
+;; Package-Requires: ((emacs "24.1") (swiper "0.2.1"))
 ;; Keywords: completion, matching
 
 ;; This file is part of GNU Emacs.
@@ -59,7 +59,7 @@
                                (and (boundp vv) (not (keywordp vv))))
                        (push (symbol-name vv) cands))))
                   cands)
-                nil nil preselect))
+                nil nil nil preselect))
      (list (if (equal val "")
                v
              (intern val)))))
@@ -81,7 +81,7 @@
                               (when (fboundp x)
                                 (push (symbol-name x) cands))))
                            cands)
-                         nil nil preselect))
+                         nil nil nil preselect))
      (list (if (equal val "")
                fn (intern val)))))
   (describe-function function))
diff --git a/ivy.el b/ivy.el
index d374dea..390af9c 100644
--- a/ivy.el
+++ b/ivy.el
@@ -70,6 +70,7 @@ This is usually meant as a quick exit out of the minibuffer."
 (defvar ivy-minibuffer-map
   (let ((map (make-sparse-keymap)))
     (define-key map (kbd "C-m") 'ivy-done)
+    (define-key map (kbd "C-j") 'ivy-alt-done)
     (define-key map (kbd "C-n") 'ivy-next-line)
     (define-key map (kbd "C-p") 'ivy-previous-line)
     (define-key map (kbd "C-s") 'ivy-next-line-or-history)
@@ -93,6 +94,9 @@ of `history-length', which see.")
 (defvar ivy-require-match t
   "Store require-match. See `completing-read'.")
 
+(defvar ivy--directory nil
+  "Current directory when completing file names.")
+
 ;;** Commands
 (defun ivy-done ()
   "Exit the minibuffer with the selected candidate."
@@ -102,10 +106,30 @@ of `history-length', which see.")
       (when (memq ivy-require-match '(nil confirm confirm-after-completion))
         (insert ivy-text)
         (setq ivy-exit 'done))
-    (insert ivy--current)
+    (if ivy--directory
+        (insert (expand-file-name ivy--current ivy--directory))
+      (insert ivy--current))
     (setq ivy-exit 'done))
   (exit-minibuffer))
 
+(defun ivy-alt-done ()
+  "Exit the minibuffer with the selected candidate."
+  (interactive)
+  (if (and ivy--directory
+           (file-directory-p
+            (expand-file-name ivy--current ivy--directory)))
+      (progn
+        (delete-minibuffer-contents)
+        (setq ivy--directory
+              (expand-file-name ivy--current ivy--directory))
+        (setq ivy--old-cands nil)
+        (setq ivy--all-candidates
+              (let ((default-directory ivy--directory))
+                (all-completions "" 'read-file-name-internal)))
+
+        (ivy--exhibit))
+    (ivy-done)))
+
 (defun ivy-beginning-of-buffer ()
   "Select the first completion candidate."
   (interactive)
@@ -168,15 +192,24 @@ If the input is empty, select the previous history 
element instead."
   "Forward to `backward-delete-char'.
 On error (read-only), call `ivy-on-del-error-function'."
   (interactive)
-  (condition-case nil
-      (backward-delete-char 1)
-    (error
-     (when ivy-on-del-error-function
-       (funcall ivy-on-del-error-function)))))
+  (if (and ivy--directory (= (minibuffer-prompt-end) (point)))
+      (progn
+        (setq ivy--old-cands nil)
+        (setq ivy--all-candidates
+              (let ((default-directory (setq ivy--directory
+                                             (file-name-directory
+                                              (directory-file-name 
ivy--directory)))))
+                (all-completions "" 'read-file-name-internal)))
+        (ivy--exhibit))
+    (condition-case nil
+        (backward-delete-char 1)
+      (error
+       (when ivy-on-del-error-function
+         (funcall ivy-on-del-error-function))))))
 
 ;;** Entry Point
 (defun ivy-read (prompt collection
-                 &optional initial-input keymap preselect update-fn)
+                 &optional predicate initial-input keymap preselect update-fn)
   "Read a string in the minibuffer, with completion.
 
 PROMPT is a string to prompt with; normally it ends in a colon
@@ -194,6 +227,17 @@ If PRESELECT is non-nil select the corresponding candidate 
out of
 the ones that match INITIAL-INPUT.
 
 UPDATE-FN is called each time the current candidate(s) is changed."
+  (setq ivy--directory nil)
+  (cond ((or (functionp collection)
+             (vectorp collection))
+         (when (eq collection 'read-file-name-internal)
+           (setq ivy--directory default-directory)
+           (setq initial-input nil))
+         (setq collection (all-completions "" collection predicate)))
+        ((hash-table-p collection)
+         (error "Hash table as a collection unsupported"))
+        ((listp (car collection))
+         (setq collection (all-completions "" collection predicate))))
   (cl-case (length collection)
     (0 nil)
     (1 (car collection))
@@ -215,6 +259,8 @@ UPDATE-FN is called each time the current candidate(s) is 
changed."
                   prompt)
                  ((string-match "%.*d" ivy-count-format)
                   (concat ivy-count-format prompt))
+                 (ivy--directory
+                  prompt)
                  (t
                   nil)))
      (setq ivy--action nil)
@@ -256,19 +302,10 @@ DEF is the default value.
 _INHERIT-INPUT-METHOD is ignored for now.
 
 The history, defaults and input-method arguments are ignored for now."
-  (cond ((or (functionp collection)
-             (vectorp collection))
-         (setq collection (all-completions "" collection predicate))
-         ;; find-file is problematic
-         (setq initial-input nil))
-        ((hash-table-p collection)
-         (error "Hash table as a collection unsupported"))
-        ((listp (car collection))
-         (setq collection (all-completions "" collection predicate))))
   (when (listp def)
     (setq def (car def)))
   (setq ivy-require-match require-match)
-  (ivy-read prompt collection initial-input nil def))
+  (ivy-read prompt collection predicate initial-input nil def))
 
 ;;;###autoload
 (define-minor-mode ivy-mode
@@ -388,7 +425,11 @@ When non-nil, it should contain one %d.")
   "Update the prompt according to `ivy--prompt'."
   (when ivy--prompt
     (let ((inhibit-read-only t)
-          (n-str (format ivy--prompt ivy--length)))
+          (n-str
+           (format
+            (if ivy--directory
+                (concat ivy--prompt (abbreviate-file-name ivy--directory))
+              ivy--prompt) ivy--length)))
       (save-excursion
         (goto-char (point-min))
         (delete-region (point-min) (minibuffer-prompt-end))
diff --git a/swiper.el b/swiper.el
index 8f0a083..e8257e8 100644
--- a/swiper.el
+++ b/swiper.el
@@ -179,6 +179,7 @@ When non-nil, INITIAL-INPUT is the initial search pattern."
                     (replace-regexp-in-string
                      "%s" "pattern: " swiper--format-spec)
                     candidates
+                    nil
                     initial-input
                     swiper-map
                     preselect



reply via email to

[Prev in Thread] Current Thread [Next in Thread]