]> code.delx.au - gnu-emacs-elpa/commitdiff
Track the current selection better
authorDmitry Gutov <dgutov@yandex.ru>
Sun, 1 Feb 2015 05:35:20 +0000 (07:35 +0200)
committerDmitry Gutov <dgutov@yandex.ru>
Sun, 1 Feb 2015 05:35:20 +0000 (07:35 +0200)
* company-update-candidates: Restore selection even if it's 0. Don't rely on
  lexical sorting. Compare annotations. Maybe reset company-selection-changed.

* company-search-mode, company-search-abort: Save and restore the value of
  company-selection-changed.

#292

NEWS.md
company.el

diff --git a/NEWS.md b/NEWS.md
index aa98b51b8c1eafdcc8bb1a8634ba0ed3e9901dd4..b65d06709d24ec91b93f8b707770bb6eb40524cc 100644 (file)
--- a/NEWS.md
+++ b/NEWS.md
@@ -2,6 +2,7 @@
 
 ## Next
 
+* Better tracking of the current selection.
 * Pressing `<up>` or `<down>` doesn't quit the search mode.
 
 ## 2015-01-23 (0.8.9)
index 0352ac96e2a5602f882bf458d825e58f3ce89c33..f7f8613af442352e496180684e5c69cab170c9bc 100644 (file)
@@ -1061,18 +1061,22 @@ can retrieve meta-data for them."
 
 (defun company-update-candidates (candidates)
   (setq company-candidates-length (length candidates))
-  (if (> company-selection 0)
+  (if company-selection-changed
       ;; Try to restore the selection
       (let ((selected (nth company-selection company-candidates)))
         (setq company-selection 0
               company-candidates candidates)
         (when selected
-          (while (and candidates (string< (pop candidates) selected))
-            (cl-incf company-selection))
-          (unless candidates
-            ;; Make sure selection isn't out of bounds.
-            (setq company-selection (min (1- company-candidates-length)
-                                         company-selection)))))
+          (catch 'found
+            (while candidates
+              (let ((candidate (pop candidates)))
+                (when (and (string= candidate selected)
+                           (equal (company-call-backend 'annotation candidate)
+                                  (company-call-backend 'annotation selected)))
+                  (throw 'found t)))
+              (cl-incf company-selection))
+            (setq company-selection 0
+                  company-selection-changed nil))))
     (setq company-selection 0
           company-candidates candidates))
   ;; Calculate common.
@@ -1584,6 +1588,8 @@ from the rest of the back-ends in the group, if any, will be left at the end."
 
 (defvar-local company--search-old-selection 0)
 
+(defvar-local company--search-old-changed nil)
+
 (defun company--search (text lines)
   (let ((quoted (regexp-quote text))
         (i 0))
@@ -1664,7 +1670,8 @@ from the rest of the back-ends in the group, if any, will be left at the end."
   (interactive)
   (company--search-assert-enabled)
   (company-search-mode 0)
-  (company-set-selection company--search-old-selection t))
+  (company-set-selection company--search-old-selection t)
+  (setq company-selection-changed company--search-old-changed))
 
 (defun company-search-other-char ()
   (interactive)
@@ -1731,13 +1738,15 @@ Don't start this directly, use `company-search-candidates' or
   (if company-search-mode
       (if (company-manual-begin)
           (progn
-            (setq company--search-old-selection company-selection)
+            (setq company--search-old-selection company-selection
+                  company--search-old-changed company-selection-changed)
             (company-call-frontends 'update)
             (company-enable-overriding-keymap company-search-map))
         (setq company-search-mode nil))
     (kill-local-variable 'company-search-string)
     (kill-local-variable 'company-search-filtering)
     (kill-local-variable 'company--search-old-selection)
+    (kill-local-variable 'company--search-old-changed)
     (when company-backend
       (company--search-update-predicate "")
       (company-call-frontends 'update))