]> code.delx.au - gnu-emacs-elpa/commitdiff
ivy.el (ivy--regex): Add optional greedy arg
authorOleh Krehel <ohwoeowho@gmail.com>
Tue, 21 Apr 2015 11:30:35 +0000 (13:30 +0200)
committerOleh Krehel <ohwoeowho@gmail.com>
Tue, 21 Apr 2015 11:30:35 +0000 (13:30 +0200)
* ivy.el (ivy--regex): When optional greedy arg is t, be greedy.  Don't
wrap a sub-expr in a group if it's already a group, for instance
"forward \(char\|list\)".

Greedy is needed, for instance, for "git grep", which doesn't work great
with non-greedy regex.

Re #47

ivy.el

diff --git a/ivy.el b/ivy.el
index 1353bf0039db9db0955a3b893b82587218eff6ca..3b4fd5ddd9c93fb53b80412a9a3eb0901cc3e3ed 100644 (file)
--- a/ivy.el
+++ b/ivy.el
@@ -538,9 +538,11 @@ Turning on Ivy mode will set `completing-read-function' to
   (make-hash-table :test 'equal)
   "Store pre-computed regex.")
 
-(defun ivy--regex (str)
-  "Re-build regex from STR in case it has a space."
-  (let ((hashed (gethash str ivy--regex-hash)))
+(defun ivy--regex (str &optional greedy)
+  "Re-build regex from STR in case it has a space.
+When GREEDY is non-nil, join words in a greedy way."
+  (let ((hashed (unless greedy
+                  (gethash str ivy--regex-hash))))
     (if hashed
         (prog1 (cdr hashed)
           (setq ivy--subexps (car hashed)))
@@ -553,9 +555,14 @@ Turning on Ivy mode will set `completing-read-function' to
                         (cons
                          (setq ivy--subexps (length subs))
                          (mapconcat
-                          (lambda (x) (format "\\(%s\\)" x))
+                          (lambda (x)
+                            (if (string-match "^\\\\(.*\\\\)$" x)
+                                x
+                              (format "\\(%s\\)" x)))
                           subs
-                          ".*?"))))
+                          (if greedy
+                              ".*"
+                            ".*?")))))
                     ivy--regex-hash)))))
 
 ;;** Rest