branch: master commit ccbf9efc66c5fc4ae34503e90bbdb147cac233f5 Author: Oleh Krehel <ohwoeo...@gmail.com> Commit: Oleh Krehel <ohwoeo...@gmail.com>
Improve counsel-async initial anchoring * counsel.el (counsel--async-sentinel): Rely on `ivy-recompute-index-swiper-async' even when `ivy--old-cands' is nil. * ivy.el (ivy--recompute-index): Check for `ivy--old-cands' being nil before trying to index it. (ivy-recompute-index-swiper-async): When `ivy--old-cands' is nil, look for a string that's closest to the line number at point. --- counsel.el | 18 +++++++++--------- ivy.el | 29 ++++++++++++++++++----------- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/counsel.el b/counsel.el index f2607a4..ebdca2f 100644 --- a/counsel.el +++ b/counsel.el @@ -118,15 +118,15 @@ Or the time of the last minibuffer update.") (ivy--set-candidates (ivy--sort-maybe cands)) - (if (null ivy--old-cands) - (setq ivy--index - (or (ivy--preselect-index - (ivy-state-preselect ivy-last) - ivy--all-candidates) - 0)) - (let ((re (funcall ivy--regex-function ivy-text))) - (unless (stringp re) - (setq re (caar re))) + (let ((re (funcall ivy--regex-function ivy-text))) + (unless (stringp re) + (setq re (caar re))) + (if (null ivy--old-cands) + (unless (setq ivy--index (ivy--preselect-index + (ivy-state-preselect ivy-last) + ivy--all-candidates)) + (ivy--recompute-index + ivy-text re ivy--all-candidates)) (ivy--recompute-index ivy-text re ivy--all-candidates))) (setq ivy--old-cands ivy--all-candidates) diff --git a/ivy.el b/ivy.el index 1d7fcb3..37648bb 100644 --- a/ivy.el +++ b/ivy.el @@ -2189,7 +2189,7 @@ Prefix matches to NAME are put ahead of the list." (not (and (require 'flx nil 'noerror) (eq ivy--regex-function 'ivy--regex-fuzzy) (< (length cands) 200))) - + ivy--old-cands (cl-position (nth ivy--index ivy--old-cands) cands)) (funcall func re-str cands)))) @@ -2226,16 +2226,23 @@ Prefix matches to NAME are put ahead of the list." res))))) (defun ivy-recompute-index-swiper-async (_re-str cands) - (let ((tail (nthcdr ivy--index ivy--old-cands)) - idx) - (if (and tail ivy--old-cands (not (equal "^" ivy--old-re))) - (progn - (while (and tail (null idx)) - ;; Compare with `equal', since the collection is re-created - ;; each time with `split-string' - (setq idx (cl-position (pop tail) cands :test #'equal))) - (or idx 0)) - ivy--index))) + (if (null ivy--old-cands) + (let ((ln (with-ivy-window + (line-number-at-pos)))) + (or (cl-position-if (lambda (x) + (>= (string-to-number x) ln)) + cands) + 0)) + (let ((tail (nthcdr ivy--index ivy--old-cands)) + idx) + (if (and tail ivy--old-cands (not (equal "^" ivy--old-re))) + (progn + (while (and tail (null idx)) + ;; Compare with `equal', since the collection is re-created + ;; each time with `split-string' + (setq idx (cl-position (pop tail) cands :test #'equal))) + (or idx 0)) + ivy--index)))) (defun ivy-recompute-index-zero (_re-str _cands) 0)