Subject: Completion highlighting applied outside completion-lazy-hilit-fn
From: João Guerra <[email-masked]>
To: [email-masked]
Date: Sun, 16 Mar 2025 16:27:00 +0100
List-Id: <debbugs-submit.debbugs.gnu.org>

TL;DR: Emacs applies additional completion highlighting even when
completion-lazy-hilit-fn is set, leading to incorrect behaviour with
alternative completion styles and frontends.

I'm implementing a custom fuzzy completion style and encountering an
issue where Emacs highlights parts of completions that it shouldn't.

For example, given the following directory structure:

```
<dir>/
  ...
  set-editing.el
  ...
```

and the input pattern `set`, my completion style produces three
possible matches for `set-editing.el`:

```
1 set-editing.el
  ^^^
2 set-editing.el
  ^^     ^
3 set-editing.el
  ^   ^  ^
```

Each match has a different score. In this case, match #3 has the
highest score and should be the one being highlighted.

Completion styles can bind completion-lazy-hilit-fn to control
highlighting. However, Emacs is still applying additional highlighting
outside completion-lazy-hilit-fn, resulting in the following
highlights:

```
set-editing.el
^^^ ^   ^
```

When completing files and buffers, but not commands, Emacs is
highlighting prefixes in
[completion--twq-all](https://github.com/emacs-mirror/emacs/blob/8ac894e2246f25d2a2a97d866b10e6e0b0fede5a/lisp/minibuffer.el#L677).
This becomes more evident if I make my completion-lazy-hilit-fn not do
any highlighting: completions are still highlighted when they
shouldn't at all.

Expected behavior: when lazy highlighting is enabled, Emacs shouldn't
apply any additional highlighting. While the current behavior works
for the default completion style (i.e. completions buffer), it easily
break as soon as users employ alternative styles or frontends.

Steps to reproduce (Emacs 30.1):
- emacs -Q
- Load:
    - https://github.com/minad/vertico/
    - https://github.com/lewang/flx/
    - https://gist.github.com/joca-bt/356ba1201c4fe842144e19ee1c723c28/
- Eval:
   - (vertico-mode t)
   - (setopt completion-styles '(flx))