Subject: Re: bug#45260: 28.0.50; Maybe flex should only sort when there is no sorting metadata?

From: Omar Antolín Camarena <[email-masked]>
Date: Tue, 17 Aug 2021 10:27:23 -0500

João said:

>  I've not seen an example of a table where its original sorting
>  mattered more _when_ there is some search pattern.

I'd argue the case I mentioned in the bug report is a good example. That
was the case of the consult-line command from Daniel Mendler's consult
package. That commands prompts for a line to go to using completions.
The completion candidates thus are all the lines in the current buffer.
The lines are prefixed with their line numbers in an affixation-function
and the display-sort-function is set to identity, so that the candidates
appear in the order they occur in the buffer.

The flex completion style is a pretty reasonable one to use with that
command, and it is a powerful way to find a line in a buffer by typing
realtively few characters in the line. But if you use a completion UI
that displays the candidates automatically while you type, such as
icomplete-mode (preferably icomplete-vertical-mode since these
candidates are full lines!), it is jarring that flex jumbles the line
order. I believe this to be a natural example where you want to keep the
candidates' order.

But also feel free to close this issue if you strongly disagree with it.
I'm not that invested since I do have what João suggested: an
"omar-flex" style that doesn't sort (through a completion-style I wrote
called orderless which includes a flex substyle, and to be honest I
hardly ever use flex, either João's version or the version in
orderless). Also, if I really want to use flex it is easy enough to
disable the sorting for specific commands such as consult-line:

(defun keep-flex-from-sorting (fn &rest args)
  (put 'flex 'completion--adjust-metadata nil)
  (unwind-protect
      (apply fn args)
    (put 'flex 'completion--adjust-metadata 'completion--flex-adjust-metadata)))

(advice-add 'consult-line :around #'keep-flex-from-sorting)

Since flex's sorting is what you want for the vast majorit of commands,
I think this advice approach is a perfectly serviceable work around.

[Since in Emacs I can usually work around any issue without needing any
upstream changes, I feel my debating skills have deteriorated: I don't
have to convince people my suggestions are a good idea! So I just
suggest but then don't insist. ;) ]

-- 
Omar