]> code.delx.au - gnu-emacs-elpa/commitdiff
counsel.el (counsel-unquote-regex-parens): Improve
authorOleh Krehel <ohwoeowho@gmail.com>
Mon, 4 Apr 2016 14:55:06 +0000 (16:55 +0200)
committerOleh Krehel <ohwoeowho@gmail.com>
Mon, 4 Apr 2016 14:55:06 +0000 (16:55 +0200)
* counsel.el (counsel-grep-function): Use different quoting. Now it's
  possible to use e.g. "(defun foo" as input.
(counsel-unquote-regex-parens): Does the proper unquote now - 4
replacements instead of previous 2.

* ivy-test.el (counsel-unquote-regex-parens): Add test.

counsel.el
ivy-test.el

index 31b0ba02a8e22b4d8d364693330ae665e04d1185..f4d9df6829585e0f5223bda168ec2701cf26050c 100644 (file)
         (format "%d chars more" (- n (length ivy-text)))))
 
 (defun counsel-unquote-regex-parens (str)
-  (replace-regexp-in-string
-   "\\\\)" ")"
-   (replace-regexp-in-string
-    "\\\\(" "("
-    str)))
+  (let ((start 0)
+        ms)
+    (while (setq start (string-match "\\\\)\\|\\\\(\\|[()]" str start))
+      (setq ms (match-string-no-properties 0 str))
+      (cond ((equal ms "\\(")
+             (setq str (replace-match "(" nil t str))
+             (setq start (+ start 1)))
+            ((equal ms "\\)")
+             (setq str (replace-match ")" nil t str))
+             (setq start (+ start 1)))
+            ((equal ms "(")
+             (setq str (replace-match "\\(" nil t str))
+             (setq start (+ start 2)))
+            ((equal ms ")")
+             (setq str (replace-match "\\)" nil t str))
+             (setq start (+ start 2)))
+            (t
+             (error "unexpected"))))
+    str))
 
 (defun counsel-directory-parent (dir)
   "Return the directory parent of directory DIR."
@@ -1359,7 +1373,7 @@ This uses `counsel-ag' with `counsel-pt-base-command' replacing
                   (setq ivy--old-re
                         (ivy--regex string)))))
       (counsel--async-command
-       (format "grep -nP --ignore-case '%s' %s" regex counsel--git-grep-dir))
+       (format "grep -nE --ignore-case \"%s\" %s" regex counsel--git-grep-dir))
       nil)))
 
 (defun counsel-grep-action (x)
index 7533169396ea122d58326450dd33b52776ab8368..866eaa722c05a8222ea15df279734cf41ad4ddef 100644 (file)
 (ert-deftest counsel-unquote-regex-parens ()
   (should (equal (counsel-unquote-regex-parens
                   (ivy--regex "foo bar"))
-                 "(foo).*?(bar)")))
+                 "(foo).*?(bar)"))
+  (should (equal (counsel-unquote-regex-parens
+                  (ivy--regex "(foo bar"))
+                 "(\\(foo).*?(bar)")))