Subject: bug#60411: 29.0.60; minibuffer-next-completion skips first candidate when completions-header-format and completion-show-help are nil

From: Gregory Heytings <[email-masked]>
Date: Fri, 30 Dec 2022 21:54:21 +0000

>
> 1. Evaluate: (setopt completions-header-format nil completion-show-help 
> nil)
>
> 2. Type M-x followed by TAB. The *Completions* buffer pops up and shows 
> all available commands.
>
> 3. Type M-<down>.
>
> Expected result: The first candidate in *Completions* is selected.
>
> Actual result: The second candidate in *Completions* is selected.
>

Thanks for your bug report.

I attach a patch to fix that bug, can you please try it?

It's a workaround, but it's the cleanest fix I can think of.
From 10d86c8025b3387644089fa3774b2c9883fabf36 Mon Sep 17 00:00:00 2001
From: Gregory Heytings <[email-masked]>
Date: Fri, 30 Dec 2022 21:45:24 +0000
Subject: [PATCH] Fix selection of completions with completions-header-format
 nil

* lisp/minibuffer.el (display-completion-list): Insert an
invisible line when completions-header-format is nil.
Fixes bug#60411.
---
 lisp/minibuffer.el | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index 7a720cf2c0..95b8962759 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -2275,8 +2275,12 @@ display-completion-list
 
     (with-current-buffer standard-output
       (goto-char (point-max))
-      (when completions-header-format
-        (insert (format completions-header-format (length completions))))
+      (if completions-header-format
+          (insert (format completions-header-format (length completions)))
+        ;; Insert an invisible line, otherwise the first call to
+        ;; 'minibuffer-next-completion' might select the second
+        ;; completion candidate.  See bug#60411.
+        (insert (propertize "\n" 'invisible t)))
       (completion--insert-strings completions group-fun)))
 
   (run-hooks 'completion-setup-hook)
-- 
2.39.0