Subject: bug#49888: 28.0.50; fido-mode not sorting by recency

From: João Távora <[email-masked]>
Date: Sun, 15 Aug 2021 13:11:27 +0100

Manuel Uberti <[email-masked]> writes:

> In 'emacs -Q', this is what I did:
>
> - M-x savehist-mode RET
> - M-x fido-mode RET
> - M-x icomplete-vertical-mode RET
> - M-x emacs-init-time
> - C-x p p, pick a Git-versioned directory RET
> - f, pick the fourth candidate, C-x k RET
> - M-x now shows pr-ps-file-preview on top
> - C-x p p, pick the only available project
> - C-x p f does not offer the previously selected candidate
>
> Is it possible to make fido-mode sort the candidates by recency?

I've now taken a look at this.  Yes, it is certainly possible and
desirable _if_ there aren't any other overriding criteria.  This has to
do with the 'flex' completion style.  In that style, if anything has
been input that leads to "flexy" filtering, the default and correct
behaviour is to sort by completion score.

But if nothing has been input and there are basically no scores, it
makes sense to fall back to a sensible strategy like the one you
suggest.

Try this patch.  It will fix the M-x case, bugt not the C-x p f or C-x f
cases unfortunately.  Those will be harder to fix.

diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index 1e8e9fc624..36d95ad5f5 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -3947,27 +3947,36 @@ completion--flex-adjust-metadata
       ((compose-flex-sort-fn
         (existing-sort-fn) ; wish `cl-flet' had proper indentation...
         (lambda (completions)
-          (let ((pre-sorted
-                 (if existing-sort-fn
-                     (funcall existing-sort-fn completions)
-                   completions)))
-            (cond
-             ((or (not (window-minibuffer-p))
-                  ;; JT@2019-12-23: FIXME: this is still wrong.  What
-                  ;; we need to test here is "some input that actually
-                  ;; leads to flex filtering", not "something after
-                  ;; the minibuffer prompt".  Among other
-                  ;; inconsistencies, the latter is always true for
-                  ;; file searches, meaning the next clauses will be
-                  ;; ignored.
-                  (> (point-max) (minibuffer-prompt-end)))
-              (sort
-               pre-sorted
-               (lambda (c1 c2)
-                 (let ((s1 (get-text-property 0 'completion-score c1))
-                       (s2 (get-text-property 0 'completion-score c2)))
-                   (> (or s1 0) (or s2 0))))))
-             (t pre-sorted))))))
+          (cond
+           (;; Sort by flex score whenever outside the minibuffer or
+            ;; in the minibuffer with some input.  JT@2019-12-23:
+            ;; FIXME: this is still wrong.  What we need to test here
+            ;; is "some input that actually leads to flex filtering",
+            ;; not "something after the minibuffer prompt".  Among
+            ;; other inconsistencies, the latter is always true for
+            ;; file searches, meaning the next clauses in this cond
+            ;; will be ignored.
+            (or (not (window-minibuffer-p))
+                (> (point-max) (minibuffer-prompt-end)))
+            (sort
+             (if existing-sort-fn
+                 (funcall existing-sort-fn completions)
+               completions)
+             (lambda (c1 c2)
+               (let ((s1 (get-text-property 0 'completion-score c1))
+                     (s2 (get-text-property 0 'completion-score c2)))
+                 (> (or s1 0) (or s2 0))))))
+           (;; If no existing sort fn and nothing flexy happening, use
+            ;; the default sort.
+            (not existing-sort-fn)
+            (let ((la (minibuffer--sort-by-length-alpha completions))
+                  (hist (and (minibufferp)
+                             (and (not (eq minibuffer-history-variable t))
+                                  (symbol-value minibuffer-history-variable)))))
+              ;; Sort by history position, put the default, if it
+              ;; exists, on top.
+              (if hist (minibuffer--sort-by-position hist la) la)))
+           (t completions)))))
     `(metadata
       (display-sort-function
        . ,(compose-flex-sort-fn