Skip to content

Commit

Permalink
swiper.el (swiper--isearch-same-line-p): Add
Browse files Browse the repository at this point in the history
  • Loading branch information
abo-abo authored and astoff committed Jan 1, 2021
1 parent f5be2e0 commit 7a15b47
Showing 1 changed file with 50 additions and 38 deletions.
88 changes: 50 additions & 38 deletions swiper.el
Original file line number Diff line number Diff line change
Expand Up @@ -1321,51 +1321,63 @@ When not running `swiper-isearch' already, start it."
:background "#1a4b77" :foreground "black"))
"Face used by `swiper-isearch' for highlighting the current match.")

(defun swiper--isearch-same-line-p (s1 s2)
"Check if S1 and S2 are equal and on the same line."
(and (equal s1 s2)
(<= (count-lines
(get-text-property 0 'point s2)
(get-text-property 0 'point s1))
1)))

(defun swiper-isearch-format-function (_cands)
(let* ((half-height (/ ivy-height 2))
(current (ivy-state-current ivy-last))
(i (1- ivy--index))
(j 0)
(len 0)
res s)
(while (and (>= i 0)
(string= (nth i ivy--old-cands)
current))
(cl-decf i)
(cl-incf j))
(while (and (>= i 0)
(< len half-height))
(setq s (nth i ivy--old-cands))
(unless (equal s (car res))
(push (ivy--format-minibuffer-line s) res)
(cl-incf len))
(cl-decf i))
(setq res (nreverse res))
(let ((current-str
(ivy--add-face
(ivy--format-minibuffer-line current)
'ivy-current-match))
(start 0))
(dotimes (_ (1+ j))
(string-match ivy--old-re current-str start)
(setq start (match-end 0)))
(ivy-add-face-text-property
(match-beginning 0) (match-end 0)
'swiper-isearch-current-match current-str)
(push current-str res))
(cl-incf len)
(setq i (1+ ivy--index))
(while (and (< i ivy--length)
(string= (nth i ivy--old-cands) current))
(cl-incf i))
(while (and (< i ivy--length)
(< len ivy-height))
(setq s (nth i ivy--old-cands))
(unless (equal s (car res))
(push (ivy--format-minibuffer-line s) res)
(cl-incf len))
(cl-incf i))
(mapconcat #'identity (nreverse res) "\n")))
(with-ivy-window
(while (and (>= i 0)
(swiper--isearch-same-line-p
(nth i ivy--old-cands)
current))
(cl-decf i)
(cl-incf j))
(while (and (>= i 0)
(< len half-height))
(setq s (nth i ivy--old-cands))
(unless (swiper--isearch-same-line-p s (car res))
(push (ivy--format-minibuffer-line s) res)
(cl-incf len))
(cl-decf i))
(setq res (nreverse res))
(let ((current-str
(ivy--add-face
(ivy--format-minibuffer-line current)
'ivy-current-match))
(start 0))
(dotimes (_ (1+ j))
(string-match ivy--old-re current-str start)
(setq start (match-end 0)))
(ivy-add-face-text-property
(match-beginning 0) (match-end 0)
'swiper-isearch-current-match current-str)
(push current-str res))
(cl-incf len)
(setq i (1+ ivy--index))
(while (and (< i ivy--length)
(swiper--isearch-same-line-p
(nth i ivy--old-cands)
current))
(cl-incf i))
(while (and (< i ivy--length)
(< len ivy-height))
(setq s (nth i ivy--old-cands))
(unless (swiper--isearch-same-line-p s (car res))
(push (ivy--format-minibuffer-line s) res)
(cl-incf len))
(cl-incf i))
(mapconcat #'identity (nreverse res) "\n"))))

;;;###autoload
(defun swiper-isearch (&optional initial-input)
Expand Down

0 comments on commit 7a15b47

Please sign in to comment.