From 6777cf85866ccbc475453bb9a216878d49576a76 Mon Sep 17 00:00:00 2001 From: Oleh Krehel Date: Mon, 4 Apr 2016 16:55:06 +0200 Subject: [PATCH] counsel.el (counsel-unquote-regex-parens): Improve * 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 | 26 ++++++++++++++++++++------ ivy-test.el | 5 ++++- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/counsel.el b/counsel.el index 31b0ba02a..f4d9df682 100644 --- a/counsel.el +++ b/counsel.el @@ -42,11 +42,25 @@ (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) diff --git a/ivy-test.el b/ivy-test.el index 753316939..866eaa722 100644 --- a/ivy-test.el +++ b/ivy-test.el @@ -170,4 +170,7 @@ (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)"))) -- 2.39.2