branch: master
commit d8172a00fdf35aae5023ceb449523a31a7809749
Author: Oleh Krehel <[email protected]>
Commit: Oleh Krehel <[email protected]>
ivy.el (ivy--regex-fuzzy): Improve for "^" and "$"
* ivy.el (ivy--regex-fuzzy): Don't insert .* after ^ and before $.
* ivy-test.el (ivy--regex-fuzzy): Add test.
Fixes #139
---
ivy-test.el | 14 ++++++++++++++
ivy.el | 6 +++++-
2 files changed, 19 insertions(+), 1 deletions(-)
diff --git a/ivy-test.el b/ivy-test.el
index 91e0dc3..1062cd8 100644
--- a/ivy-test.el
+++ b/ivy-test.el
@@ -81,3 +81,17 @@
'("We're all Britons"
"and I am"
"your king."))))
+
+(ert-deftest ivy--regex-fuzzy ()
+ (should (string= (ivy--regex-fuzzy "tmux")
+ "t.*m.*u.*x"))
+ (should (string= (ivy--regex-fuzzy "^tmux")
+ "^t.*m.*u.*x"))
+ (should (string= (ivy--regex-fuzzy "^tmux$")
+ "^t.*m.*u.*x$"))
+ (should (string= (ivy--regex-fuzzy "")
+ ""))
+ (should (string= (ivy--regex-fuzzy "^")
+ "^"))
+ (should (string= (ivy--regex-fuzzy "$")
+ "$")))
diff --git a/ivy.el b/ivy.el
index e002e64..07ad9bf 100644
--- a/ivy.el
+++ b/ivy.el
@@ -966,7 +966,11 @@ Everything after \"!\" should not match."
(defun ivy--regex-fuzzy (str)
"Build a regex sequence from STR.
Insert .* between each char."
- (mapconcat #'string (string-to-list str) ".*"))
+ (if (string-match "\\`\\(\\^?\\)\\(.*?\\)\\(\\$?\\)\\'" str)
+ (concat (match-string 1 str)
+ (mapconcat #'string (string-to-list (match-string 2 str)) ".*")
+ (match-string 3 str))
+ str))
;;** Rest
(defun ivy--minibuffer-setup ()