branch: master
commit d73376f434f801e6f9b1ca05b53eb20368edc512
Author: Oleh Krehel <[email protected]>
Commit: Oleh Krehel <[email protected]>
ivy.el (ivy--trim-trailing-re): Add
* swiper.el (swiper--isearch-function): Make use of `ivy--trim-trailing-re'.
Re #2155
---
ivy-test.el | 12 +++++++++---
ivy.el | 10 ++++++++++
swiper.el | 3 ---
3 files changed, 19 insertions(+), 6 deletions(-)
diff --git a/ivy-test.el b/ivy-test.el
index 0d53b07..f8c9727 100644
--- a/ivy-test.el
+++ b/ivy-test.el
@@ -219,7 +219,9 @@ will bring the behavior in line with the newer Emacsen."
"foo\\["))
(should (equal (ivy--regex
".org")
- "\\.org")))
+ "\\.org"))
+ (should (equal (ivy--regex "foo\\") "foo"))
+ (should (equal (ivy--regex "foo\\|") "foo")))
(ert-deftest ivy--split-negation ()
(should (equal (ivy--split-negation "") ()))
@@ -288,7 +290,9 @@ will bring the behavior in line with the newer Emacsen."
(should (string= (ivy--regex-fuzzy "^")
"^"))
(should (string= (ivy--regex-fuzzy "$")
- "$")))
+ "$"))
+ (should (equal (ivy--regex-fuzzy "abc\\|")
+ "\\(a\\)[^b\n]*\\(b\\)[^c\n]*\\(c\\)")))
(ert-deftest ivy--regex-ignore-order ()
(should (equal (ivy--regex-ignore-order "tmux")
@@ -320,7 +324,9 @@ will bring the behavior in line with the newer Emacsen."
(ivy--regex-ignore-order "! ! !")
;; Escape invalid regexps.
(should (equal (ivy--regex-ignore-order "foo[ bar[xy]")
- '(("foo\\[" . t) ("bar[xy]" . t)))))
+ '(("foo\\[" . t) ("bar[xy]" . t))))
+ (should (equal (ivy--regex-ignore-order "foo\\|")
+ '(("foo" . t)))))
(ert-deftest ivy--format ()
(should (string= (let ((ivy--index 10)
diff --git a/ivy.el b/ivy.el
index 58af21a..d580167 100644
--- a/ivy.el
+++ b/ivy.el
@@ -2563,6 +2563,13 @@ regexp is passed to `regexp-quote'."
(push s res)))
(mapcar #'ivy--regex-or-literal (nreverse res))))
+(defun ivy--trim-trailing-re (regex)
+ "Trim incomplete REGEX.
+If REGEX ends with \\|, trim it, since then it matches an empty string."
+ (if (string-match "\\`\\(.*\\)[\\]|\\'" regex)
+ (match-string 1 regex)
+ regex))
+
(defun ivy--regex (str &optional greedy)
"Re-build regex pattern from STR in case it has a space.
When GREEDY is non-nil, join words in a greedy way."
@@ -2574,6 +2581,7 @@ When GREEDY is non-nil, join words in a greedy way."
(cdr hashed))
(when (string-match-p "\\(?:[^\\]\\|^\\)\\\\\\'" str)
(setq str (substring str 0 -1)))
+ (setq str (ivy--trim-trailing-re str))
(cdr (puthash str
(let ((subs (ivy--split str)))
(if (= (length subs) 1)
@@ -2663,6 +2671,7 @@ foo\!bar -> matches \"foo!bar\"
foo\ bar -> matches \"foo bar\"
Returns a list suitable for `ivy-re-match'."
+ (setq str (ivy--trim-trailing-re str))
(let* (regex-parts
(raw-parts (ivy--split-negation str)))
(dolist (part (ivy--split-spaces (car raw-parts)))
@@ -2695,6 +2704,7 @@ match. Everything after \"!\" should not match."
(defun ivy--regex-fuzzy (str)
"Build a regex sequence from STR.
Insert .* between each char."
+ (setq str (ivy--trim-trailing-re str))
(if (string-match "\\`\\(\\^?\\)\\(.*?\\)\\(\\$?\\)\\'" str)
(prog1
(concat (match-string 1 str)
diff --git a/swiper.el b/swiper.el
index 9b57894..f5a09dc 100644
--- a/swiper.el
+++ b/swiper.el
@@ -1340,9 +1340,6 @@ See `ivy-format-functions-alist' for further information."
(unless (equal re-full "")
(let* ((case-fold-search (ivy--case-fold-p str))
(re (ivy-re-to-str re-full))
- (re (if (string-match "\\`\\(.*\\)[\\]|\\'" re)
- (match-string 1 re)
- re))
(res (swiper--isearch-function-1 re swiper--isearch-backward))
(idx-found (car res))
(cands (cdr res)))