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

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

[elpa] master 3a08a88 50/67: ivy.el (ivy-read): Change index to preselec


From: Oleh Krehel
Subject: [elpa] master 3a08a88 50/67: ivy.el (ivy-read): Change index to preselect
Date: Sun, 22 Mar 2015 17:34:07 +0000

branch: master
commit 3a08a883dba1ed8bf548613d01831898cea5bc11
Author: Oleh Krehel <address@hidden>
Commit: Oleh Krehel <address@hidden>

    ivy.el (ivy-read): Change index to preselect
    
    * ivy.el (ivy-read): Update signature. Instead of giving the integer
      index of what to preselect, give a string to preselect.
    (ivy--preselect-index): New defun.
    
    * swiper.el (swiper--index-at-point): Rename to `ivy--preselect-index'.
    (swiper--ivy): Simplify.
---
 ivy.el    |   29 ++++++++++++++++++++++++++---
 swiper.el |   35 ++++++++++-------------------------
 2 files changed, 36 insertions(+), 28 deletions(-)

diff --git a/ivy.el b/ivy.el
index 5b2ef39..37c016d 100644
--- a/ivy.el
+++ b/ivy.el
@@ -134,18 +134,28 @@ On error (read-only), quit without selecting."
      (minibuffer-keyboard-quit))))
 
 ;;** Entry Point
-(defun ivy-read (prompt collection &optional initial-input update-fn index)
+(defun ivy-read (prompt collection &optional initial-input update-fn preselect)
   "Read a string in the minibuffer, with completion.
+
 PROMPT is a string to prompt with; normally it ends in a colon and a space.
+
 COLLECTION is a list of strings.
+
 If INITIAL-INPUT is non-nil, insert it in the minibuffer initially.
+
 UPDATE-FN is called each time the current candidate(s) is changed.
-If INDEX is non-nil select the corresponding candidate."
+
+If PRESELECT is non-nil select the corresponding candidate out of
+the ones that match INITIAL-INPUT."
   (cl-case (length collection)
     (0 nil)
     (1 (car collection))
     (t
-     (setq ivy--index (or index 0))
+     (setq ivy--index (or
+                       (and preselect
+                            (ivy--preselect-index
+                             collection initial-input preselect))
+                       0))
      (setq ivy--old-re nil)
      (setq ivy--old-cands nil)
      (setq ivy-text "")
@@ -169,6 +179,19 @@ If INDEX is non-nil select the corresponding candidate."
                 res)))
        (remove-hook 'post-command-hook #'ivy--exhibit)))))
 
+(defun ivy--preselect-index (candidates initial-input preselect)
+  "Return the index in CANDIDATES filtered by INITIAL-INPUT for PRESELECT."
+  (when initial-input
+    (setq candidates
+          (cl-remove-if-not
+           (lambda (x)
+             (string-match initial-input x))
+           candidates)))
+  (cl-position-if
+   (lambda (x)
+     (string-match preselect x))
+   candidates))
+
 (defvar ivy-text ""
   "Stores the user's string as it is typed in.")
 
diff --git a/swiper.el b/swiper.el
index fc5dae2..80c2339 100644
--- a/swiper.el
+++ b/swiper.el
@@ -134,42 +134,27 @@ When non-nil, INITIAL-INPUT is the initial search 
pattern."
   (setq swiper--anchor (line-number-at-pos))
   (setq swiper--window (selected-window)))
 
-(defun swiper--index-at-point (candidates initial-input)
-  "Return the index of current line in CANDIDATES filtered by INITIAL-INPUT."
-  (cl-position-if
-   `(lambda (x)
-      (string-match
-       ,(format
-         "[0-9]+ *%s"
-         (regexp-quote
-          (buffer-substring-no-properties
-           (line-beginning-position)
-           (line-end-position))))
-       x))
-   (cl-remove-if-not
-    (lambda (x)
-      (string-match initial-input x))
-    candidates)))
-
 (defun swiper--ivy (&optional initial-input)
   "`isearch' with an overview using `ivy'.
 When non-nil, INITIAL-INPUT is the initial search pattern."
   (interactive)
   (ido-mode -1)
   (swiper--init)
-  (let* ((candidates (swiper--candidates))
-         (index (if initial-input
-                    (swiper--index-at-point
-                     candidates initial-input)
-                  (1-
-                   (line-number-at-pos))))
-         res)
+  (let ((candidates (swiper--candidates))
+        (preselect (format
+                    "%d *%s"
+                    (line-number-at-pos)
+                    (regexp-quote
+                     (buffer-substring-no-properties
+                      (line-beginning-position)
+                      (line-end-position)))))
+        res)
     (unwind-protect
          (setq res (ivy-read "pattern: "
                              candidates
                              initial-input
                              #'swiper--update-input-ivy
-                             index))
+                             preselect))
       (ido-mode 1)
       (swiper--cleanup)
       (if (null ivy-exit)



reply via email to

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