]> code.delx.au - gnu-emacs-elpa/commitdiff
counsel-git-grep now works with ivy-resume
authorOleh Krehel <ohwoeowho@gmail.com>
Tue, 12 May 2015 13:24:29 +0000 (15:24 +0200)
committerOleh Krehel <ohwoeowho@gmail.com>
Tue, 12 May 2015 13:27:25 +0000 (15:27 +0200)
* ivy.el (ivy-state): New field DYNAMIC-COLLECTION.
(ivy-resume): Update.
(ivy-read): New argument DYNAMIC-COLLECTION. When this is non-nil,
ignore collection and matchers etc, and just obtain the filtered
candidates by calling DYNAMIC-COLLECTION each time the input changes.

Fixes #100

counsel.el
ivy.el

index 54f3a56023dda76bd9c016ebc76ef9ee23c7f5b7..447371d417d0ad956945d5879e2c13cbfe5eb216 100644 (file)
 (defun counsel-git-grep (&optional initial-input)
   "Grep for a string in the current git repository."
   (interactive)
-  (unwind-protect
-       (let* ((counsel--git-grep-dir (locate-dominating-file
-                                      default-directory ".git"))
-              (counsel--git-grep-count (counsel-git-grep-count ""))
-              (ivy--dynamic-function (when (> counsel--git-grep-count 20000)
-                                       'counsel-git-grep-function)))
-         (ivy-read "pattern: " 'counsel-git-grep-function
-                   :initial-input initial-input
-                   :matcher #'counsel-git-grep-matcher
-                   :action
-                   (lambda ()
-                     (let ((lst (split-string ivy--current ":")))
-                       (find-file (expand-file-name (car lst) counsel--git-grep-dir))
-                       (goto-char (point-min))
-                       (forward-line (1- (string-to-number (cadr lst))))
-                       (setq swiper--window (selected-window))
-                       (swiper--cleanup)
-                       (swiper--add-overlays (ivy--regex ivy-text))))))
-    (swiper--cleanup)))
+  (setq counsel--git-grep-dir
+        (locate-dominating-file default-directory ".git"))
+  (setq counsel--git-grep-count (counsel-git-grep-count ""))
+  (ivy-read "pattern: " 'counsel-git-grep-function
+            :initial-input initial-input
+            :matcher #'counsel-git-grep-matcher
+            :dynamic-collection (when (> counsel--git-grep-count 20000)
+                                  'counsel-git-grep-function)
+            :action
+            (lambda ()
+              (let ((lst (split-string ivy--current ":")))
+                (find-file (expand-file-name (car lst) counsel--git-grep-dir))
+                (goto-char (point-min))
+                (forward-line (1- (string-to-number (cadr lst))))
+                (unless (eq ivy-exit 'done)
+                  (setq swiper--window (selected-window))
+                  (swiper--cleanup)
+                  (swiper--add-overlays (ivy--regex ivy-text)))))
+            :unwind #'swiper--cleanup))
 
 (defun counsel-git-grep-matcher (x)
   (when (string-match "^[^:]+:[^:]+:" x)
diff --git a/ivy.el b/ivy.el
index 17a55460949b0cd0a00f50bb36d1837134309eef..96e20dc629522213686e9ebadb71df8e09229f8f 100644 (file)
--- a/ivy.el
+++ b/ivy.el
@@ -138,7 +138,9 @@ Only \"./\" and \"../\" apply here. They appear in reverse order."
   action
   unwind
   re-builder
-  matcher)
+  matcher
+  ;; When this is non-nil, call it for each input change to get new candidates
+  dynamic-collection)
 
 (defvar ivy-last nil
   "The last parameters passed to `ivy-read'.")
@@ -350,7 +352,8 @@ candidate."
    :action (ivy-state-action ivy-last)
    :unwind (ivy-state-unwind ivy-last)
    :re-builder (ivy-state-re-builder ivy-last)
-   :matcher (ivy-state-matcher ivy-last)))
+   :matcher (ivy-state-matcher ivy-last)
+   :dynamic-collection (ivy-state-dynamic-collection ivy-last)))
 
 (defun ivy-beginning-of-buffer ()
   "Select the first completion candidate."
@@ -566,7 +569,7 @@ Directories come first."
 (cl-defun ivy-read (prompt collection
                     &key predicate require-match initial-input
                       history preselect keymap update-fn sort
-                      action unwind re-builder matcher)
+                      action unwind re-builder matcher dynamic-collection)
   "Read a string in the minibuffer, with completion.
 
 PROMPT is a string to prompt with; normally it ends in a colon
@@ -593,7 +596,10 @@ UNWIND is a lambda to call before exiting.
 
 RE-BUILDER is a lambda that transforms text into a regex.
 
-MATCHER can completely override matching."
+MATCHER can completely override matching.
+
+DYNAMIC-COLLECTION is a function to call to update the list of
+candidates with each input."
   (setq ivy-last
         (make-ivy-state
          :prompt prompt
@@ -610,7 +616,8 @@ MATCHER can completely override matching."
          :window (selected-window)
          :unwind unwind
          :re-builder re-builder
-         :matcher matcher))
+         :matcher matcher
+         :dynamic-collection dynamic-collection))
   (setq ivy--directory nil)
   (setq ivy--regex-function
         (or re-builder
@@ -889,11 +896,8 @@ Everything after \"!\" should not match."
     (goto-char (minibuffer-prompt-end))
     (delete-region (line-end-position) (point-max))))
 
-(defvar ivy--dynamic-function nil
-  "When this is non-nil, call it for each input change to get new candidates.")
-
 (defvar ivy--full-length nil
-  "When `ivy--dynamic-function' is non-nil, this can be the total amount of candidates.")
+  "When :dynamic-collection is non-nil, this can be the total amount of candidates.")
 
 (defvar ivy--old-text ""
   "Store old `ivy-text' for dynamic completion.")
@@ -921,7 +925,7 @@ Everything after \"!\" should not match."
                       (if ivy--directory
                           (abbreviate-file-name ivy--directory)
                         ""))
-              (or (and ivy--dynamic-function
+              (or (and (ivy-state-dynamic-collection ivy-last)
                        ivy--full-length)
                   ivy--length))))
         (save-excursion
@@ -953,14 +957,14 @@ Everything after \"!\" should not match."
   "Insert Ivy completions display.
 Should be run via minibuffer `post-command-hook'."
   (setq ivy-text (ivy--input))
-  (if ivy--dynamic-function
+  (if (ivy-state-dynamic-collection ivy-last)
       ;; while-no-input would cause annoying
       ;; "Waiting for process to die...done" message interruptions
       (let ((inhibit-message t))
         (while-no-input
           (unless (equal ivy--old-text ivy-text)
-            (let ((store ivy--dynamic-function)
-                  (ivy--dynamic-function nil))
+            (cl-letf ((store (ivy-state-dynamic-collection ivy-last))
+                      ((ivy-state-dynamic-collection ivy-last) nil))
               (setq ivy--all-candidates (funcall store ivy-text))))
           (ivy--insert-minibuffer (ivy--format ivy--all-candidates))))
     (cond (ivy--directory