(completing-read): Add `group-function` to the completion metadata
A completion table can specify a `group-function` in its metadata.
The group function takes two arguments, a completion candidate and a
transform argument. The group function is used to group the
candidates after sorting and to enhance the completion UI with group
titles.
If the transform argument is nil, the function must return the title
of the group to which the completion candidate belongs. The function
may also return nil if the candidate does not belong to a group.
If the transform argument is non-nil, the function must return the
transformed candidate. For example, the transformation allows to
remove a redundant part of the candidate, which is then displayed in
the title.
The grouping functionality is guarded by the customizable variable
`completions-group` and turned off by default for the *Completions*
buffer.
The specific form of the `group-function` has been chosen in order to
allow allocation-free grouping. This is important for completion UIs,
which continously update the displayed set of candidates (Icomplete,
Vertico, Ivy, etc.). Only when the transform argument is non-nil the
candidate transformation is performed, which may involve a string
allocation as done in the function `xref--completing-read-group`.
The function `xref-show-definitions-completing-read` makes use of the
`group-function`, by moving the file name prefix to the title. If
grouping is enabled, the *Completions* are displayed as
"linenum:summary" instead of "file:linenum:summary". This way the
*Completions* buffer resembles the *Occur* buffer.
* doc/lispref/minibuf.texi: Add documentation.
* lisp/minibuffer.el (completion-metadata): Describe the
`group-function` in the docstring.
(completions-group): Add guard variable, off by default.
(completions-group-format): Add variable defining the format string
for the group titles.
(completions-group-title): Add face used by `completions-group-format`
for the group titles.
(completions-group-separator): Add face used by
`completions-group-format` for the group separator lines.
(minibuffer--group-by): New grouping function.
(minibuffer-completion-help): Use it.
(display-completion-list): Add optional GROUP-FUN argument.
(completion--insert-strings): Add optional GROUP-FUN argument. Insert
group titles if `completions-format` is `one-column`. Transform each
candidate with the GROUP-FUN. Attach the untransformed candidate to
the property `completion--string`.
* lisp/simple.el (choose-completion): Retrieve the untransformed
completion candidate from the property `completion--string`.
* lisp/progmodes/xref.el:
(xref--completing-read-group): New grouping function.
(xref-show-definitions-completing-read): Use it.