Subject: bug#65459: completing-read INITIAL-VALUE unaware of COLLECTION and REQUIRE-MATCH

From: Stefan Monnier <[email-masked]>
Date: Thu, 24 Aug 2023 17:02:47 -0400

>> Using the DEFault arg gives you the same benefit without prefilling the
>> minibuffer, so I must be missing something. What would be the advantage
>> for the users by prefilling the minibuffer with "Chronological Dating"?
> Yes, without prefilling the minibuffer.  Prefilling the minibuffer is
> quicker for users, giving them the possibility to see the current
> stage if they forget  default key shortcuts.

The `format-prompt` thingy in my example code is there to show to the
users what the default will be.

> The more we discuss this the more it seems the prefilling
> the minbuffer was something we never wanted users to have.

Indeed, it was never intended for regular use.  It's only meant to be
used in specific cases like `read-file-name` where we do have something
to prefill that is almost always useful.

[ You used the term "dogma" earlier, but I like to think of Emacs
  design as being rather undogmatic in that we go through a lot of
  trouble to allow people to do even those things that we don't want
  them to do.  ]

>> > > So, IIUC, you have a `completing-read` call asking them which template
>> > > to insert, and you want to order the set of completions based on
>> > > knowledge of the stage at which they are?
>> > > No ordering actually happens, a particular element in collection is used
>> > > to prefill the minibuffer entry and consecutive elements in simple
>> > > cycling continue through the next stages.
>> 
>> 
>> Yes, the ordering I'm talking about is the order in the operational
>> flow refined such that if the likely next stage is "Composition and
>> Provenance", then you'll want to use:
>> 
>> "Composition and Provenance" "Isotope Analysis" "Physical Analysis" "Chronological Dating"
>> 
>> That's what you want, right?
>
> Correct, but the actual collection might still be 
>
> "Physical_Analysis" "Chronological Dating" "Composition and Provenance" "Isotope Analysis"
>
> Rather than applying modifications to it, I can just specify the start index.

That's an implementation detail.

E.g. you can use

    (defconst my-phases
      '("Physical_Analysis" "Chronological Dating" "Composition and Provenance" "Isotope Analysis"))

    (defun my-rotate (collection first)
      (let ((x (member first collection)))
        (if (not x) collection
          (let ((idx (- (length collection) (length x))))
            (append x (seq-take collection idx))))))

     [...]
     (let ((next-phase (my-guess-next-phase)))
       (completing-read (format-prompt "Phase" next-phase)
                        (my-rotate my-phases next-phase)
                        nil t nil nil next-phase))
     [...]

>> You mean, if they use, say, `icomplete-mode` or `vertico-mode`, you'd
>> prefer that those UIs use an alphabetical ordering rather than the one
>> based on operational flow?
>
> I would think that if they use vertico, there is a reason that is convenient
> to them to use cempletion, if they are not employing simple cycling (through
> repeated use of <down>). 

Both `icomplete-mode` and `vertico-mode` offer/encourage the use of
cycling (but their cycling code is completely different from the one
you're using so it doesn't obey `next-history-element`), so I expect
their users would also appreciate if the ordering of the completions is
chosen with the same care as what you do for the
`next-line-or-history-element` case.

> Quite right, and help programmers from the need to go as low level as
> calling 'minibuffer-with-setup-hook' hacks.  The unfortunate thing is that
> the inclusion of INITIAL makes people want to use it, as I did in certain
> circumstances.   Having to go through 'minibuffer-with-setup-hook' hacks
> is not something one looks forward to do in the interactive clause of a
> function.

+1

I guess we really should work on a replacement for `completing-read`, eh?

        Stefan