* 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-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\\)")))
(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)
(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