Subject: bug#59314: 29.0.50; EUDC and message-mode header completion

From: Thomas Fitzsimmons <[email-masked]>
Date: Tue, 13 Dec 2022 20:34:00 -0500

Alexander Adolf <[email-masked]> writes:

> Thomas Fitzsimmons <[email-masked]> writes:
>
>> [...]
>> The scenario I'd like you to make work with a minimal patch is:
>> Scenario 3+1 + (setq message-expand-name-standard-ui t).
>>
>> Did you try that? 
>
> Yes.
>
>> I'm pretty sure if you get that working you'll find that we don't need
>> eudc-capf-complete in completion-at-point-functions (yet), since what
>> was there will already do what eudc-capf-complete was trying to
>> achieve.
>>
>> Basically, this whole part of the discussion I've been thinking in terms
>> of "(setq message-expand-name-standard-ui t)".
>>
>> When message-expand-name-standard-ui is nil, I think we've determined
>> that eudc-capf-complete's presence in completion-at-point-functions
>> breaks EUDC functionality.  Do you concur?
>
> Yes.
>
>> If that's the case we should probably remove eudc-capf-complete from
>> completion-at-point-functions for Emacs 29.1, since that would
>> represent a regression.
>
> That's one option of addressing this case. The other option would be to
> apply the last patch I sent to add `(setq-local completion-styles...)`
> to `eudc-capf-complete` (because this is a defect and needs to be added
> in any case). Albeit with an updated message, because the message
> pretends to fix the bug, which it apparently doesn't.
>
> My preferred outcome would thus be:
>
> 1) Re-remove the `(add-hook 'completion-at-point-functions
> #'eudc-capf-complete ...)` line from message.el.
>
> 2) Add the `(setq-local completion-styles ...)` fix to
> `eudc-capf-complete`.
>
> 3) See if we can figure a root cause for the completion styles breakage
> within reasonable time and effort.
>
> The attached patch implements 1) and 2).
>
>> I now understand that the intent of the code that was there before we
>> added eudc-capf-complete was:
>>
>> message-expand-name-standard-ui nil => use EUDC multi-selector UI
>> message-expand-name-standard-ui t   => use completion-at-point selector UI
>>
>> But the second case was already broken before we added
>> eudc-capf-complete.
>
> Indeed.
>
> And it also broken when not using EUDC at all, as I reported in my
> last message ('eudc removed from `message-expand-name-databases`).
>
>> I want to understand and fix that case.
>> [...]
>
> My suspicion is that something about the completion styles was changed.
> Mu next step would hence be to meditate over the `git blame` of
> minibuffer.el.
>
> Looking forward to your thoughts,

I've applied your patch locally and I'm testing with it, thanks.  I'm
also setting (setq message-expand-name-standard-ui t).

This may be a clue; while I was experimenting, I found that this:

@@ -8432,7 +8438,7 @@ message--name-table
                           (all-completions string
                                            (ecomplete-completion-table 'mail)
                                            pred)))))
-	   (if action candidates (try-completion string candidates))))))))
+	   (if action (cdr candidates) (try-completion string candidates))))))))

results in the expansion working (albeit always expanding to Emacs
ERT4).  So it seems the issue is with a list of more than one candidate,
probably on the return path through the completion functions in
minibuffer.el.  I haven't been able to follow the return path all the
way yet.

Other complications:

1. If I do completion via bbdb-complete-mail first, before I've done
   (setq message-expand-name-standard-ui t), message-expand-name gets
   added to message--old-style-completion-functions dynamically,
   resulting in completion doing absolutely nothing.  If I set
   message-expand-name-standard-ui back to nil, then stock BBDB
   completion works again.  So there is a stateful interaction between
   stock BBDB completion and message-expand-name-standard-ui, where you
   have to set message--old-style-completion-functions back to nil if
   you want to change between the two.

2. When I switch to the *unsent mail* buffer, and press TAB, message-tab
   runs.  But if I then press TAB again, completion-at-point runs.  I
   have to switch out of the buffer then back in for message-tab to be
   run again.  It seems to enter a mode, with a new TAB keybinding,
   where it's trying to use completion-at-point within a region.

Thomas