]> git.eshelyaron.com Git - emacs.git/commitdiff
; Reposition cursor after refreshing *Completions*
authorEshel Yaron <me@eshelyaron.com>
Wed, 21 Feb 2024 12:24:06 +0000 (13:24 +0100)
committerEshel Yaron <me@eshelyaron.com>
Wed, 21 Feb 2024 12:24:06 +0000 (13:24 +0100)
* lisp/minibuffer.el (minibuffer-completion-help): If *Completions* is
already visible and some candidate is highlighted, try to highlight it
again after populating the buffer anew.

lisp/minibuffer.el

index 6dbc32022906540e700422ef5340a46f5965b12f..4fc95f4e7b4b1ecf4a54d92475fab094de0c5c50 100644 (file)
@@ -2860,8 +2860,26 @@ completions list."
 (defun minibuffer-completion-help (&optional start end)
   "Display a list of possible completions of the current minibuffer contents."
   (interactive)
-  (message "Making completion list...")
-  (let* ((start (or start (minibuffer--completion-prompt-end)))
+  (let* ((current (when-let ((win (get-buffer-window "*Completions*" 0)))
+                    (get-text-property (window-point win) 'completion--string
+                                       (get-buffer "*Completions*"))))
+         (prev-next (when current
+                      (with-current-buffer "*Completions*"
+                        (save-excursion
+                          (goto-char (point-min))
+                          (text-property-search-forward 'completion--string current t)
+                          (cons
+                           (save-excursion
+                             (when-let ((pm (text-property-search-backward 'completion--string current)))
+                               (goto-char (prop-match-end pm))
+                               (when-let ((pm (text-property-search-backward 'cursor-face nil)))
+                                 (goto-char (prop-match-beginning pm))
+                                 (get-text-property (point) 'completion--string))))
+                           (save-excursion
+                             (when-let ((pm (text-property-search-forward 'cursor-face nil t)))
+                               (goto-char (prop-match-end pm))
+                               (get-text-property (point) 'completion--string))))))))
+         (start (or start (minibuffer--completion-prompt-end)))
          (end (or end (point-max)))
          (string (buffer-substring start end))
          (md (completion--field-metadata start))
@@ -3052,7 +3070,20 @@ completions list."
                                                      (if (eq (car bounds) (length result))
                                                          'exact 'finished)))))))
 
-                      (display-completion-list completions group-fun)))))
+                      (display-completion-list completions group-fun)
+                      (with-current-buffer standard-output
+                        (goto-char (point-min))
+                        (when-let
+                            ((pm
+                              (or (and current (text-property-search-forward 'completion--string current t))
+                                  (when-let ((next (cdr prev-next)))
+                                    (text-property-search-forward 'completion--string next t))
+                                  (when-let ((prev (car prev-next)))
+                                    (text-property-search-forward 'completion--string prev t)))))
+                          (goto-char (prop-match-beginning pm))
+                          (setq pm (text-property-search-forward 'cursor-face))
+                          (setq-local cursor-face-highlight-nonselected-window t)
+                          (set-window-point (get-buffer-window) (prop-match-beginning pm))))))))
           nil)))
     nil))