]> code.delx.au - gnu-emacs-elpa/commitdiff
Update the way spaces are quoted using ivy
authorOleh Krehel <ohwoeowho@gmail.com>
Thu, 14 May 2015 13:05:24 +0000 (15:05 +0200)
committerOleh Krehel <ohwoeowho@gmail.com>
Thu, 14 May 2015 13:05:24 +0000 (15:05 +0200)
* ivy.el (ivy--split): Split only on single spaces. From all other space
  groups, remove one space.

* ivy-test.el (ivy--split): Add test.

Fixes #109

ivy-test.el
ivy.el

index 951bfdab6d400720a5cb46fa3c3664a635291095..91e0dc35d962cbc820cacfd8cbc804849dc0d74b 100644 (file)
                    "^[0-9][0-9 ]\\{4\\}\\(a\\)"))
   (should (string= (swiper--re-builder "^a b")
                    "^[0-9][0-9 ]\\{4\\}\\(a\\).*?\\(b\\)")))
+
+(ert-deftest ivy--split ()
+  (should (equal (ivy--split "King of the who?")
+                 '("King" "of" "the" "who?")))
+  (should (equal (ivy--split "The  Brittons.")
+                 '("The Brittons.")))
+  (should (equal (ivy--split "Who  are the  Brittons?")
+                 '("Who are" "the Brittons?")))
+  (should (equal (ivy--split "We're  all  Britons and   I   am your   king.")
+                 '("We're all Britons"
+                  "and  I  am"
+                   "your  king."))))
diff --git a/ivy.el b/ivy.el
index 121e8f510ce292034217703c3e6eef632aacae1e..c0bbedb07f7999ecdc176f6fabe833d78acfca0b 100644 (file)
--- a/ivy.el
+++ b/ivy.el
@@ -801,17 +801,29 @@ Minibuffer bindings:
 The remaining spaces stick to their left.
 This allows to \"quote\" N spaces by inputting N+1 spaces."
   (let ((len (length str))
-        (start 0)
+        start0
+        (start1 0)
         res s)
-    (while (and (string-match " +" str start)
-                (< start len))
-      (setq s (substring str start (1- (match-end 0))))
+    (while (and (string-match " +" str start1)
+                (< start1 len))
+      (setq match-len (- (match-end 0) (match-beginning 0)))
+      (if (= match-len 1)
+          (progn
+            (when start0
+              (setq start1 start0)
+              (setq start0 nil))
+            (push (substring str start1 (match-beginning 0)) res)
+            (setq start1 (match-end 0)))
+        (setq str (replace-match
+                   (make-string (1- match-len) ?\ )
+                   nil nil str))
+        (setq start0 (or start0 start1))
+        (setq start1 (1- (match-end 0)))))
+    (if start0
+        (push (substring str start0) res)
+      (setq s (substring str start1))
       (unless (= (length s) 0)
-        (push s res))
-      (setq start (match-end 0)))
-    (setq s (substring str start))
-    (unless (= (length s) 0)
-      (push s res))
+        (push s res)))
     (nreverse res)))
 
 (defun ivy--regex (str &optional greedy)