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, 06 Jan 2023 08:22:39 +0000

>> Stefan and Eli, do you agree with that conclusion?
>
> I admit that I've lost the line of reasoning here (too much of the 
> previous context is being elided, forcing me to re-read the entire 
> discussion).  Which code is proposed for the release branch, and how 
> will Emacs behave with that code in this particular use case?
>

It's this patch.  It adds an invisible empty line at the beginning of 
*Completions* when completions-header-format is not a string (in 
particular, nil) or an empty string.
From d0443e0215196b1576800ae1d5f0f5357a8a8dad Mon Sep 17 00:00:00 2001
From: Gregory Heytings <[email-masked]>
Date: Sat, 31 Dec 2022 14:50:13 +0000
Subject: [PATCH] Fix handling of completions-header-format

* lisp/minibuffer.el (display-completion-list): Insert an
invisible line when completions-header-format is not a string (in
particular, nil) or is an empty string.  Fixes bug#60411.
Also, do not use completions-header-format when it is not a
string.
---
 lisp/minibuffer.el | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index 7a720cf2c0..6c7413c555 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -2275,8 +2275,14 @@ display-completion-list
 
     (with-current-buffer standard-output
       (goto-char (point-max))
-      (when completions-header-format
+      (when (stringp completions-header-format)
         (insert (format completions-header-format (length completions))))
+      (when (or (not (stringp completions-header-format))
+                (string= completions-header-format ""))
+        ;; 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