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

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

[elpa] master 2cedcf5 37/57: Introduce :matcher for counsel-git-grep


From: Oleh Krehel
Subject: [elpa] master 2cedcf5 37/57: Introduce :matcher for counsel-git-grep
Date: Tue, 19 May 2015 14:21:35 +0000

branch: master
commit 2cedcf59cf1063d92488a26aa80ef50069f6fe11
Author: Oleh Krehel <address@hidden>
Commit: Oleh Krehel <address@hidden>

    Introduce :matcher for counsel-git-grep
    
    * ivy.el (ivy-state): Add MATCHER field.
    (ivy-resume): Update.
    (ivy-read): Add MATCHER argument. To make things faster, MATCHER can
    reuse `ivy--old-re', but it's not required. MATCHER is a function that
    takes a candidate and returns non-nil if it matches.
    
    * counsel.el (counsel-git-grep): Use :matcher.
    (counsel-git-grep-matcher): New defun. Skip the file name and line
    number, then match as usual.
    
    Fixes #99
---
 counsel.el |   16 ++++++++++++
 ivy.el     |   80 +++++++++++++++++++++++++++++++++--------------------------
 2 files changed, 61 insertions(+), 35 deletions(-)

diff --git a/counsel.el b/counsel.el
index ce64c3a..54f3a56 100644
--- a/counsel.el
+++ b/counsel.el
@@ -222,6 +222,7 @@
                                        '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 ":")))
@@ -233,6 +234,21 @@
                        (swiper--add-overlays (ivy--regex ivy-text))))))
     (swiper--cleanup)))
 
+(defun counsel-git-grep-matcher (x)
+  (when (string-match "^[^:]+:[^:]+:" x)
+    (setq x (substring x (match-end 0)))
+    (if (stringp ivy--old-re)
+      (string-match ivy--old-re x)
+    (let ((res t))
+      (dolist (re ivy--old-re)
+        (setq res
+              (and res
+                   (ignore-errors
+                     (if (cdr re)
+                         (string-match (car re) x)
+                       (not (string-match (car re) x)))))))
+      res))))
+
 (defun counsel-locate-function (str &rest _u)
   (if (< (length str) 3)
       (list ""
diff --git a/ivy.el b/ivy.el
index 4d0647b..17a5546 100644
--- a/ivy.el
+++ b/ivy.el
@@ -137,7 +137,8 @@ Only \"./\" and \"../\" apply here. They appear in reverse 
order."
   window
   action
   unwind
-  re-builder)
+  re-builder
+  matcher)
 
 (defvar ivy-last nil
   "The last parameters passed to `ivy-read'.")
@@ -348,7 +349,8 @@ candidate."
    :sort (ivy-state-sort ivy-last)
    :action (ivy-state-action ivy-last)
    :unwind (ivy-state-unwind ivy-last)
-   :re-builder (ivy-state-re-builder ivy-last)))
+   :re-builder (ivy-state-re-builder ivy-last)
+   :matcher (ivy-state-matcher ivy-last)))
 
 (defun ivy-beginning-of-buffer ()
   "Select the first completion candidate."
@@ -564,7 +566,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)
+                      action unwind re-builder matcher)
   "Read a string in the minibuffer, with completion.
 
 PROMPT is a string to prompt with; normally it ends in a colon
@@ -589,7 +591,9 @@ ACTION is a lambda to call after a result was selected.
 
 UNWIND is a lambda to call before exiting.
 
-RE-BUILDER is a lambda that transforms text into a regex."
+RE-BUILDER is a lambda that transforms text into a regex.
+
+MATCHER can completely override matching."
   (setq ivy-last
         (make-ivy-state
          :prompt prompt
@@ -605,7 +609,8 @@ RE-BUILDER is a lambda that transforms text into a regex."
          :action action
          :window (selected-window)
          :unwind unwind
-         :re-builder re-builder))
+         :re-builder re-builder
+         :matcher matcher))
   (setq ivy--directory nil)
   (setq ivy--regex-function
         (or re-builder
@@ -1015,36 +1020,41 @@ Should be run via minibuffer `post-command-hook'."
   "Return all items that match NAME in CANDIDATES.
 CANDIDATES are assumed to be static."
   (let* ((re (funcall ivy--regex-function name))
-         (cands (cond ((and (equal re ivy--old-re)
-                            ivy--old-cands)
-                       ivy--old-cands)
-                      ((and ivy--old-re
-                            (stringp re)
-                            (stringp ivy--old-re)
-                            (not (string-match "\\\\" ivy--old-re))
-                            (not (equal ivy--old-re ""))
-                            (memq (cl-search
-                                   (if (string-match "\\\\)$" ivy--old-re)
-                                       (substring ivy--old-re 0 -2)
-                                     ivy--old-re)
-                                   re) '(0 2)))
-                       (ignore-errors
-                         (cl-remove-if-not
-                          (lambda (x) (string-match re x))
-                          ivy--old-cands)))
-                      (t
-                       (let ((re-list (if (stringp re) (list (cons re t)) re))
-                             (res candidates))
-                         (dolist (re re-list)
-                           (setq res
-                                 (ignore-errors
-                                   (funcall
-                                    (if (cdr re)
-                                        #'cl-remove-if-not
-                                      #'cl-remove-if)
-                                    `(lambda (x) (string-match ,(car re) x))
-                                    res))))
-                         res))))
+         (matcher (ivy-state-matcher ivy-last))
+         (cands (cond
+                  (matcher
+                   (let ((ivy--old-re re))
+                     (cl-remove-if-not matcher candidates)))
+                  ((and (equal re ivy--old-re)
+                        ivy--old-cands)
+                   ivy--old-cands)
+                  ((and ivy--old-re
+                        (stringp re)
+                        (stringp ivy--old-re)
+                        (not (string-match "\\\\" ivy--old-re))
+                        (not (equal ivy--old-re ""))
+                        (memq (cl-search
+                               (if (string-match "\\\\)$" ivy--old-re)
+                                   (substring ivy--old-re 0 -2)
+                                 ivy--old-re)
+                               re) '(0 2)))
+                   (ignore-errors
+                     (cl-remove-if-not
+                      (lambda (x) (string-match re x))
+                      ivy--old-cands)))
+                  (t
+                   (let ((re-list (if (stringp re) (list (cons re t)) re))
+                         (res candidates))
+                     (dolist (re re-list)
+                       (setq res
+                             (ignore-errors
+                               (funcall
+                                (if (cdr re)
+                                    #'cl-remove-if-not
+                                  #'cl-remove-if)
+                                `(lambda (x) (string-match ,(car re) x))
+                                res))))
+                     res))))
          (tail (nthcdr ivy--index ivy--old-cands))
          idx)
     (when (and tail ivy--old-cands)



reply via email to

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