branch: master
commit c4c1878580eddb229cae98b7d02b5804c9bcc330
Author: Oleh Krehel <[email protected]>
Commit: Oleh Krehel <[email protected]>
Fix swiper preselect issue with similar or identical lines
* ivy.el (ivy--preselect-index): Allow PRESELECT to be an integer.
* swiper.el (swiper--anchor):
(swiper--len): Remove unused defvar.
(swiper--init): Update.
(swiper--ivy): Set PRESELECT to `line-number-at-pos'.
Fixes #290
---
ivy.el | 16 +++++++++-------
swiper.el | 43 +++++++++++++++++++------------------------
2 files changed, 28 insertions(+), 31 deletions(-)
diff --git a/ivy.el b/ivy.el
index bf89836..92b87fd 100644
--- a/ivy.el
+++ b/ivy.el
@@ -1293,13 +1293,15 @@ When MATCHER is non-nil it's used instead of
`cl-remove-if-not'."
candidates))))
(when matcher
(setq candidates (funcall matcher "" candidates))))
- (or (cl-position preselect candidates :test #'equal)
- (and (stringp preselect)
- (let ((re (regexp-quote preselect)))
- (cl-position-if
- (lambda (x)
- (string-match re x))
- candidates)))))
+ (cond ((integerp preselect)
+ preselect)
+ ((cl-position preselect candidates :test #'equal))
+ ((stringp preselect)
+ (let ((re (regexp-quote preselect)))
+ (cl-position-if
+ (lambda (x)
+ (string-match re x))
+ candidates)))))
;;* Implementation
;;** Regex
diff --git a/swiper.el b/swiper.el
index 56247ed..eec7434 100644
--- a/swiper.el
+++ b/swiper.el
@@ -298,19 +298,11 @@ When non-nil, INITIAL-INPUT is the initial search
pattern."
(interactive)
(swiper--ivy initial-input))
-(defvar swiper--anchor nil
- "A line number to which the search should be anchored.")
-
-(defvar swiper--len 0
- "The last length of input for which an anchoring was made.")
-
(declare-function evil-jumper--set-jump "ext:evil-jumper")
(defun swiper--init ()
"Perform initialization common to both completion methods."
(setq swiper--opoint (point))
- (setq swiper--len 0)
- (setq swiper--anchor (line-number-at-pos))
(when (bound-and-true-p evil-jumper-mode)
(evil-jumper--set-jump)))
@@ -348,24 +340,27 @@ When non-nil, INITIAL-INPUT is the initial search
pattern."
(setq swiper-invocation-face
(plist-get (text-properties-at (point)) 'face))
(let ((candidates (swiper--candidates))
- (preselect (buffer-substring-no-properties
- (line-beginning-position)
- (line-end-position)))
+ (preselect
+ (if (bound-and-true-p visual-line-mode)
+ (concat " " (buffer-substring-no-properties
+ (line-beginning-position)
+ (line-end-position)))
+ (1- (line-number-at-pos))))
(minibuffer-allow-text-properties t))
(unwind-protect
- (ivy-read
- "Swiper: "
- candidates
- :initial-input initial-input
- :keymap swiper-map
- :preselect preselect
- :require-match t
- :update-fn #'swiper--update-input-ivy
- :unwind #'swiper--cleanup
- :action #'swiper--action
- :re-builder #'swiper--re-builder
- :history 'swiper-history
- :caller 'swiper)
+ (ivy-read
+ "Swiper: "
+ candidates
+ :initial-input initial-input
+ :keymap swiper-map
+ :preselect preselect
+ :require-match t
+ :update-fn #'swiper--update-input-ivy
+ :unwind #'swiper--cleanup
+ :action #'swiper--action
+ :re-builder #'swiper--re-builder
+ :history 'swiper-history
+ :caller 'swiper)
(when (null ivy-exit)
(goto-char swiper--opoint)))))