emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

scratch/icomplete-vertical-mode-improvements 94368e2 06/10: Adjust scrol


From: João Távora
Subject: scratch/icomplete-vertical-mode-improvements 94368e2 06/10: Adjust scrolling behaviour of icomplete-vertical-mode
Date: Fri, 28 May 2021 06:10:06 -0400 (EDT)

branch: scratch/icomplete-vertical-mode-improvements
commit 94368e211f9a28dfc7fd466578b9fdf77423e3a1
Author: João Távora <joaotavora@gmail.com>
Commit: João Távora <joaotavora@gmail.com>

    Adjust scrolling behaviour of icomplete-vertical-mode
    
    * lisp/icomplete.el (icomplete--last-selected): Document
    (icomplete-forward-completions)
    (icomplete-backward-completions): Set icomplete--last-selected
    here.
    (icomplete--render-vertical): Adjust and document.
---
 lisp/icomplete.el | 43 ++++++++++++++++++++++++++++---------------
 1 file changed, 28 insertions(+), 15 deletions(-)

diff --git a/lisp/icomplete.el b/lisp/icomplete.el
index 4c651e2..8357d8f 100644
--- a/lisp/icomplete.el
+++ b/lisp/icomplete.el
@@ -228,7 +228,7 @@ the default otherwise."
 (defvar icomplete--comp-predecessors nil
   "Completions to list before the selected one.")
 (defvar icomplete--last-selected nil
-  "Last completion selected.")
+  "Last completion explicitly selected.")
 
 (defun icomplete-forward-completions ()
   "Step forward completions by one entry.
@@ -243,9 +243,10 @@ Second entry becomes the first and can be selected with
       (cond (icomplete-rotate
              (setcdr (last comps) (cons (pop comps) (cdr last))))
             (t
-             (push (pop comps) icomplete--comp-predecessors)))
-      (completion--cache-all-sorted-completions beg end comps))
-    (setq icomplete--last-selected nil)))
+             (push (pop comps) icomplete--comp-predecessors)
+             (setq icomplete--last-selected
+                   (cons (car comps) (buffer-modified-tick)))))
+      (completion--cache-all-sorted-completions beg end comps))))
 
 (defun icomplete-backward-completions ()
   "Step backward completions by one entry.
@@ -262,9 +263,10 @@ Last entry becomes the first and can be selected with
            (push (car (cdr last-but-one)) comps)
            (setcdr last-but-one (cdr (cdr last-but-one))))
           (icomplete--comp-predecessors
-           (push (pop icomplete--comp-predecessors) comps)))
-    (completion--cache-all-sorted-completions beg end comps)
-    (setq icomplete--last-selected nil)))
+           (push (pop icomplete--comp-predecessors) comps)
+           (setq icomplete--last-selected
+                 (cons (car comps) (buffer-modified-tick)))))
+    (completion--cache-all-sorted-completions beg end comps)))
 
 ;;; Helpers for `fido-mode' (or `ido-mode' emulation)
 ;;;
@@ -695,9 +697,18 @@ See `icomplete-mode' and `minibuffer-setup-hook'."
               (put-text-property 0 1 'cursor t text)
               (overlay-put icomplete-overlay 'after-string text))))))))
 
-(defun icomplete--render-vertical (comps)
-  ;; First attempt to keep selection stable.  Do this only if the
-  ;; selection didn't change but the candidates did.
+(cl-defun icomplete--render-vertical (comps md)
+  ;; This is loopapalooza
+  ;;
+  ;; First, attempt to keep selection stable.  Do this only if
+  ;; rotation is off and there is a selection.
+  ;;
+  ;; Most importantly, also only do this if the buffer changed,
+  ;; meaning there are new candidates within which we want to search
+  ;; for our manually selected one.  That's because if we were to
+  ;; needlessly search for our selection in unchanged candidates, we'd
+  ;; obviously always find it in the first position, lose the illusion
+  ;; of a dropdown scroll.
   (when (and icomplete--last-selected
              (not (eq (cdr icomplete--last-selected) (buffer-modified-tick)))
              (null icomplete-rotate))
@@ -713,7 +724,11 @@ See `icomplete-mode' and `minibuffer-setup-hook'."
       (icomplete--field-end)
       comps)
      and return nil
-     do (push comp preds)))
+     do (push comp preds)
+     finally (setq icomplete--last-selected nil)))
+  ;; The ugliest loop, collect the completions before and after the
+  ;; selected one, considering scrolling positions.
+  ;;
   (cl-loop
    with preds = (and (null icomplete-rotate) icomplete--comp-predecessors)
    with succs = (cdr comps)
@@ -740,10 +755,8 @@ See `icomplete-mode' and `minibuffer-setup-hook'."
              #'identity
              (nconc before
                     (list
-                     (setq icomplete--last-selected
-                           (cons (propertize (car comps) 'face
-                                             'icomplete-selected-match)
-                                 (buffer-modified-tick)))))
+                     (propertize (car comps) 'face
+                                             'icomplete-selected-match)))
                     after)
              icomplete-separator)))))
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]