branch: master
commit e32bc6320629f3bff57722e8bd65b6b5eaf134c1
Author: Oleh Krehel <[email protected]>
Commit: Oleh Krehel <[email protected]>
swiper.el: Fix non-matching lines issue with initial-input
* swiper.el (swiper--index-at-point): New defun.
(swiper--ivy): Update.
Fixes #20.
---
swiper.el | 29 ++++++++++++++++++++++++++---
1 files changed, 26 insertions(+), 3 deletions(-)
diff --git a/swiper.el b/swiper.el
index 6d889dd..e00aa1a 100644
--- a/swiper.el
+++ b/swiper.el
@@ -134,19 +134,42 @@ When non-nil, INITIAL-INPUT is the initial search
pattern."
(setq swiper--anchor (line-number-at-pos))
(setq swiper--window (selected-window)))
+(defun swiper--index-at-point (candidates initial-input)
+ "Return the index of current line in CANDIDATES filtered by INITIAL-INPUT."
+ (cl-position-if
+ `(lambda (x)
+ (string-match
+ ,(format
+ "[0-9]+ *%s"
+ (regexp-quote
+ (buffer-substring-no-properties
+ (line-beginning-position)
+ (line-end-position))))
+ x))
+ (cl-remove-if-not
+ (lambda (x)
+ (string-match initial-input x))
+ candidates)))
+
(defun swiper--ivy (&optional initial-input)
"`isearch' with an overview using `ivy'.
When non-nil, INITIAL-INPUT is the initial search pattern."
(interactive)
(ido-mode -1)
(swiper--init)
- (let (res)
+ (let* ((candidates (swiper--candidates))
+ (index (if initial-input
+ (swiper--index-at-point
+ candidates initial-input)
+ (1-
+ (line-number-at-pos))))
+ res)
(unwind-protect
(setq res (ivy-read "pattern: "
- (swiper--candidates)
+ candidates
initial-input
#'swiper--update-input-ivy
- (1- (line-number-at-pos))))
+ index))
(ido-mode 1)
(swiper--cleanup)
(if (null ivy-exit)