Subject: bug#67650: [PATCH] ; Hide completion preview when switching windows

From: Eshel Yaron <[email-masked]>
Date: Wed, 06 Dec 2023 14:33:01 +0100

Hi Eli,

Eli Zaretskii <[email-masked]> writes:

>> Date: Tue, 05 Dec 2023 21:26:27 +0100
>> From:  Eshel Yaron via "Bug reports for GNU Emacs,
>>  the Swiss army knife of text editors" <[email-masked]>
>>
>> This patch ensures that we dismiss the completion preview when users
>> switch to another window.  Previously, the completion preview would
>> remain in a window after switching to another window showing another
>> buffer, since the `post-command-hook` that Completion Preview mode sets
>> up locally doesn't run after a command switches to another buffer.
>
> Sorry, I don't understand:

No problem, let's clarify.

> why does it matter whether post-command-hook runs _after_ switching to
> another buffer?

Since then the value of `post-command-hook` is no longer the
buffer-local value you had in the buffer where you invoked the command,
and in our case that buffer-local value is the one that includes
`completion-preview--post-command`, where we would update (or dismiss)
the completion preview for that (original) buffer.

> When and why would a command that caused post-command-hook invocation
> NOT need to dismiss the preview?

What do you mean?  All commands "cause `post-command-hook`", no?
We show/hide/update the preview during `post-command-hook`.  So for
example, when `post-command-hook` runs after `self-insert-command`, we
don't dismiss the preview, in fact we show it.  Does that make sense?

> If post-command-hook should always dismiss the preview

That's not the case, see above.

> then we shouldn't need another hook, right?

Even if it were the case, we would need to handle the case when
`post-command-hook` runs in a different buffer, with a different
buffer-local value, after switching.  And indeed we need to handle that
case, which is what this patch does.

Here's a recipe that you can try to see this patch in action:

1. emacs -Q
2. M-x completion-preview-mode RET
3. Type `(def` and see the preview appears
4. C-x 4 b foo RET

Without this patch, you'd now see the preview linger in the window
showing the *scratch* buffer.  With this patch, the preview disappears
as soon as you switch to the other window.

Best,

Eshel