]> code.delx.au - gnu-emacs-elpa/commitdiff
Improve counsel-async initial anchoring
authorOleh Krehel <ohwoeowho@gmail.com>
Fri, 25 Mar 2016 21:07:42 +0000 (22:07 +0100)
committerOleh Krehel <ohwoeowho@gmail.com>
Fri, 25 Mar 2016 21:07:42 +0000 (22:07 +0100)
* 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
ivy.el

index f2607a40b036158258c63c062d0e860d905be3b9..ebdca2fd1cd874fa2ac67f11c91590517e032029 100644 (file)
@@ -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 1d7fcb3878d06989afe03b9ea564df29c2f172b8..37648bb8d77f863850eac803f1b6beca9f1d8459 100644 (file)
--- 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)