emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] master ca91c88 01/57: Add an option for out-of-order matching


From: Oleh Krehel
Subject: [elpa] master ca91c88 01/57: Add an option for out-of-order matching
Date: Tue, 19 May 2015 14:21:17 +0000

branch: master
commit ca91c88ca38a4f47d10430c73ad4ce8a85173b3d
Author: Oleh Krehel <address@hidden>
Commit: Oleh Krehel <address@hidden>

    Add an option for out-of-order matching
    
    * ivy.el (ivy--regex-ignore-order): New defun.
    
    * swiper.el (swiper--init): Set `ivy--regex-function'.
    (swiper--update-input-ivy): Use `ivy--regex-function'.
    (swiper--action): Use `ivy--regex-function'.
    
    Example of use:
    
    (setq ivy-re-builders-alist
          '((t . ivy--regex-ignore-order)))
    
    With this, e.g. swiper will match "bar foo" from input "foo bar".
---
 ivy.el    |   20 ++++++++++++++++++++
 swiper.el |    8 +++++---
 2 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/ivy.el b/ivy.el
index 721dafd..9a0557f 100644
--- a/ivy.el
+++ b/ivy.el
@@ -770,6 +770,26 @@ When GREEDY is non-nil, join words in a greedy way."
                             ".*?")))))
                     ivy--regex-hash)))))
 
+(defun ivy--regex-ignore-order (str)
+  "Re-build regex from STR by splitting it on spaces.
+Ignore the order of each group."
+  (let* ((subs (split-string str " +" t))
+         (len (length subs)))
+    (cl-case len
+      (1
+       (setq ivy--subexps 0)
+       (car subs))
+      (t
+       (setq ivy--subexps len)
+       (let ((all (mapconcat #'identity subs "\\|")))
+         (mapconcat
+          (lambda (x)
+            (if (string-match "^\\\\(.*\\\\)$" x)
+                x
+              (format "\\(%s\\)" x)))
+          (make-list len all)
+          ".*?"))))))
+
 (defun ivy--regex-plus (str)
   "Build a regex sequence from STR.
 Spaces are wild, everything before \"!\" should match.
diff --git a/swiper.el b/swiper.el
index 96e955d..2f8dc76 100644
--- a/swiper.el
+++ b/swiper.el
@@ -170,7 +170,9 @@ When non-nil, INITIAL-INPUT is the initial search pattern."
   (setq swiper--opoint (point))
   (setq swiper--len 0)
   (setq swiper--anchor (line-number-at-pos))
-  (setq swiper--window (selected-window)))
+  (setq swiper--window (selected-window))
+  (setq ivy--regex-function
+        (cdr (assoc t ivy-re-builders-alist))))
 
 (defun swiper--ivy (&optional initial-input)
   "`isearch' with an overview using `ivy'.
@@ -227,7 +229,7 @@ Please remove it and update the \"swiper\" package."))
 (defun swiper--update-input-ivy ()
   "Called when `ivy' input is updated."
   (swiper--cleanup)
-  (let* ((re (ivy--regex ivy-text))
+  (let* ((re (funcall ivy--regex-function ivy-text))
          (str ivy--current)
          (num (if (string-match "^[0-9]+" str)
                   (string-to-number (match-string 0 str))
@@ -292,7 +294,7 @@ BEG and END, when specified, are the point bounds."
     (goto-char (point-min))
     (forward-line (1- (read x)))
     (re-search-forward
-     (ivy--regex input) (line-end-position) t)
+     (funcall ivy--regex-function input) (line-end-position) t)
     (swiper--ensure-visible)
     (when (/= (point) swiper--opoint)
       (unless (and transient-mark-mode mark-active)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]