Subject: Re: bug#72762: 30.0.60; Incorrect rendering of the completion-preview mode

From: Eshel Yaron <[email-masked]>
Date: Fri, 23 Aug 2024 07:30:32 +0200

Hi,

>> Date: Thu, 22 Aug 2024 17:29:30 +0000
>> From:  the_wurfkreuz via "Bug reports for GNU Emacs,
>>  the Swiss army knife of text editors" <[email-masked]>
>> 
>> Apparently, the completion-preview-mode rendering doesn't work correctly with minibuffer messages (i don't know what they're called exactly).
>> 
>> To see the problem, completion-preview-mode should show its completion suggestion simultaneously with any minibuffer messages like "[No Match]" or "[Incomplete Command]" and so on.

Thanks for the report!

Enabling Completion Preview mode in the minibuffer is not currently
supported, precisely because minibuffers make use of different
completion UIs which may conflict with the completion preview.  You
might notice that by default, minibuffer-mode is explicitly excluded in
global-completion-preview-modes.

That said, I do consider minibuffers to be a viable additional use case
for Completion Preview mode, and I'd like to help you make it work:

>> Reproduction:
>> - Load emacs with the evil-mode package.
>> - Activate evil-mode and completion-preview-mode.
>> - Evaluate the hook '(add-hook 'minibuffer-mode-hook #'completion-preview-mode)'.
>> - Execute 'evil-ex'.
>> - Try to write any long command to trigger the preview completion.

I don't use evil-mode, but I understand the situation after following
these steps.  evil-ex uses an overlay with after-string property to
display its automatic messages, which is the same mechanism Completion
Preview mode uses to display the preview.  A quick solution you can try
is to use completion-preview-active-mode-hook to turn off evil-ex's
automatic messages when the preview is visible by temporarily setting
evil-no-display to non-nil.

An arguably nicer solution is to give the completion preview overlay
higher priority, so it is displayed before the "[Incomplete Command]"
message.  However, we don't always want to give the completion preview
overlay a positive priority, since that may lead to incorrect results in
other scenarios.  What we can do is to add a variable that specifies the
overlay priority, so you can set it just where appropriate.

Eli, is the following alright for Emacs 30, or should this go on the
master branch?

diff --git a/lisp/completion-preview.el b/lisp/completion-preview.el
index d3e9acb1ed9..60abdbc0736 100644
--- a/lisp/completion-preview.el
+++ b/lisp/completion-preview.el
@@ -262,11 +262,16 @@ completion-preview-hide
     (setq completion-preview--overlay nil
           completion-preview--inhibit-update-p nil)))
 
+(defvar completion-preview-overlay-priority nil
+  "Value of the `priority' property for the completion preview overlay.")
+
 (defun completion-preview--make-overlay (pos string)
   "Make preview overlay showing STRING at POS, or move existing preview there."
   (if completion-preview--overlay
       (move-overlay completion-preview--overlay pos pos)
     (setq completion-preview--overlay (make-overlay pos pos))
+    (overlay-put completion-preview--overlay 'priority
+                 completion-preview-overlay-priority)
     (overlay-put completion-preview--overlay 'window (selected-window)))
   (add-text-properties 0 1 '(cursor 1) string)
   (overlay-put completion-preview--overlay 'after-string string)