branch: master
commit 67841857c4cfeba45db57183158dd0759a632ec6
Author: Oleh Krehel <[email protected]>
Commit: Oleh Krehel <[email protected]>
Allow to use "^" in swiper
* 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 | 13 +++++++++++++
ivy.el | 18 +++++++++++++++++-
swiper.el | 6 +++++-
3 files changed, 35 insertions(+), 2 deletions(-)
diff --git a/ivy-test.el b/ivy-test.el
index 13a1467..fffc5fd 100644
--- a/ivy-test.el
+++ b/ivy-test.el
@@ -60,3 +60,16 @@
(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 48dde66..a7e76e0 100644
--- 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)
diff --git a/swiper.el b/swiper.el
index 2f8dc76..0879103 100644
--- a/swiper.el
+++ b/swiper.el
@@ -127,12 +127,16 @@
(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