Subject: Re: bug#48545: 28.0.50; `icomplete-vertical-mode` does not support the `group-function`
From: João Távora <[email-masked]>
To: Kévin Le Gouguec <[email-masked]>
Cc: Daniel Mendler <[email-masked]>, Gregory Heytings <[email-masked]>, [email-masked], [email-masked]
Date: Thu, 19 Aug 2021 12:18:55 +0100
List-Id: <debbugs-submit.debbugs.gnu.org>

[Dmitry, I've tagged you since you're generally interested in this
stuff.]

João Távora <[email-masked]> writes:
> Kévin Le Gouguec <[email-masked]> writes:
>> I think Daniel is referring to what happens if you set completions-group
>> to t and type e.g. C-x 8 RET ROMAN TAB TAB (without icomplete): you will
>> see two "sections" titled 'symbol' and 'ancient-symbol'.
> Thanks you, easy to reproduce and see clearly what you mean now 

I've pushed this feature.  This is how I tested:

  src/emacs -Q --eval '(setq completions-group t)' \
               --eval '(setq xref-show-definitions-function (quote xref-show-definitions-completing-read))'
               -f fido-vertical-mode

  C-U M-. xref-location-marker RET

This should produce 5 locations.  It shows, with section headers, how 4
of these belong to xref.el and 1 belongs to elisp-mode.el.  When typing
a pattern which leads to filtering (e.g. with flex) sections are
preserved.  Sane scrolling should also be maintained, though this was
complicated to get right and may still have some bugs (I haven't
noticed any yet.)

I noticed a quirk, though.  If I add '-l etags' to the 'emacs -Q' line,
one gets 6 matches instead of 5 (due to etags having another
xref-location-marker).  That's fine, but due to the default
alphanumeric/length sorting, it gets shoved into the group of xref.el
matches and thus we get two xref.el groups.  I.e. it looks like

    xref.el
    (cl-defgeneric xref-location-marker)                          
    (cl-defmethod xref-location-marker ((l xref-file-location)))  
    (cl-defmethod xref-location-marker ((l xref-bogus-location))) 
    etags.el
    (cl-defmethod xref-location-marker ((l xref-etags-location))) 
    xref.el
    (cl-defmethod xref-location-marker ((l xref-buffer-location)))
    elisp-mode.el
    (cl-defmethod xref-location-marker ((l xref-elisp-location)))

I don't use completions-group so I don't care strongly for this, but I
believe that this is generally undesired, for non-filtering scenarios.
All the entries from the xref.el group should probably be clumped
together.  If a user were flex-matching and thus expecting certain sort
score-based order, it _would_ make sense to me, but here no flexy things
were happening at all.

To fix this, perhaps the default sorting methods should be turned off in
completion-all-sorted-completions in minibuffer.el if a table supplies
`group-function`.  A patch for this is after my sig.

Alternatively, tables that do support `group-function` could also start
specifying:

    (display-sort-function . identity)

I've tested both alternatives and they seem to do the right thing.  But
this may be the subject for some other bug, if someone cares.

Anyway, I'm marking this closed this bug since its main work is done.
Feedback welcome, of course.

João

diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index ffcd5d88ab..20fbc326a2 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -1506,17 +1506,18 @@ completion-all-sorted-completions
           (setq all (delete-dups all))
           (setq last (last all))
 
-          (if sort-fun
-              (setq all (funcall sort-fun all))
-            ;; Sort first by length and alphabetically.
-            (setq all (minibuffer--sort-by-length-alpha all))
-            ;; Sort by history position, put the default, if it
-            ;; exists, on top.
-            (when (minibufferp)
-              (setq all (minibuffer--sort-by-position
-                         (minibuffer--sort-preprocess-history
-                          (substring string 0 base-size))
-                         all))))
+          (cond (sort-fun
+                 (setq all (funcall sort-fun all)))
+                ((not (completion-metadata-get all-md 'group-function))
+                 ;; Sort first by length and alphabetically.
+                 (setq all (minibuffer--sort-by-length-alpha all))
+                 ;; Sort by history position, put the default, if it
+                 ;; exists, on top.
+                 (when (minibufferp)
+                   (setq all (minibuffer--sort-by-position
+                              (minibuffer--sort-preprocess-history
+                               (substring string 0 base-size))
+                              all)))))