]> code.delx.au - gnu-emacs-elpa/commitdiff
Allow to use "^" in swiper
authorOleh Krehel <ohwoeowho@gmail.com>
Fri, 8 May 2015 12:35:29 +0000 (14:35 +0200)
committerOleh Krehel <ohwoeowho@gmail.com>
Fri, 8 May 2015 12:42:51 +0000 (14:42 +0200)
* swiper.el (swiper--width): New defvar.
(swiper--candidates): Set `swiper--width'.

* ivy.el (ivy--transform-re): New defun - transform the regex
  specifically for `swiper'.
(ivy--filter): Call `ivy--transform-re'.

* ivy-test.el (ivy--transform-re): Add test.

Fixes #82

ivy-test.el
ivy.el
swiper.el

index 13a1467d2cc97c75f7cdac2f36f3dd187a3584bd..fffc5fd8346845a777dffc43caa7dd201bf0cc81 100644 (file)
            (ivy-with '(ivy-read "pattern: " '("blue" "yellow"))
                      "z C-m")
            "z")))
+
+(ert-deftest ivy--transform-re ()
+  (setq ivy-last
+        (make-ivy-state
+         :keymap swiper-map))
+  (setq swiper--width 4)
+  (setq ivy--regex-function #'ivy--regex-plus)
+  (should (string= (ivy--transform-re (funcall ivy--regex-function "^"))
+                   "."))
+  (should (string= (ivy--transform-re (funcall ivy--regex-function "^a"))
+                   "^[0-9 ]\\{5\\}a"))
+  (should (string= (ivy--transform-re (funcall ivy--regex-function "^a b"))
+                   "\\(^[0-9 ]\\{5\\}a\\).*?\\(b\\)")))
diff --git a/ivy.el b/ivy.el
index 48dde662e818e102ad8974675369a2eba0254412..a7e76e0bbe786c3d7a0a2812eae94c1027a79e60 100644 (file)
--- a/ivy.el
+++ b/ivy.el
@@ -967,10 +967,26 @@ Should be run via minibuffer `post-command-hook'."
        (font-lock-append-text-property 0 (length str) 'face face str))))
   str)
 
+(defun ivy--transform-re (regex)
+  "Transform REGEX into another regex.
+This is a work-around for `swiper' and line starts."
+  (if (equal (ivy-state-keymap ivy-last) swiper-map)
+      (cond
+        ((equal regex "^")
+         ".")
+        ((string-match "^\\(?:\\\\(\\)?\\(\\^\\)" regex)
+         (setq ivy--old-re "")
+         (replace-match (format "^[0-9 ]\\{%d\\}"
+                                (1+ swiper--width)) nil t regex 1))
+        (t
+         regex))
+    regex))
+
 (defun ivy--filter (name candidates)
   "Return all items that match NAME in CANDIDATES.
 CANDIDATES are assumed to be static."
-  (let* ((re (funcall ivy--regex-function name))
+  (let* ((re (ivy--transform-re
+              (funcall ivy--regex-function name)))
          (cands (cond ((and (equal re ivy--old-re)
                             ivy--old-cands)
                        ivy--old-cands)
index 2f8dc761e961dd0e5ad1660231e2e9318115d729..0879103c46db122c2b110a33e0d38912b0b50333 100644 (file)
--- a/swiper.el
+++ b/swiper.el
 (defvar swiper--format-spec ""
   "Store the current candidates format spec.")
 
+(defvar swiper--width nil
+  "Store the amount of digits needed for the longest line nubmer.")
+
 (defun swiper--candidates ()
   "Return a list of this buffer lines."
   (let ((n-lines (count-lines (point-min) (point-max))))
     (unless (zerop n-lines)
+      (setq swiper--width (1+ (floor (log n-lines 10))))
       (setq swiper--format-spec
-            (format "%%-%dd %%s" (1+ (floor (log n-lines 10)))))
+            (format "%%-%dd %%s" swiper--width))
       (let ((line-number 0)
             candidates)
         (save-excursion