emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Sorting command completions by recency


From: Juri Linkov
Subject: Sorting command completions by recency
Date: Wed, 17 Feb 2021 19:53:55 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (x86_64-pc-linux-gnu)

The recent discussion about filtering out command completions by mode
reminded the problem that while icomplete shows the most recently used
commands at the top of the list for M-x, trying to see a complete list
with TAB changes the order of displayed commands, i.e. in icomplete
there is one sorting order by recency, but in the *Completions* buffer
alphabetical sorting order.

This code puts the recent commands at the top as well,
but probably it needs to be opt-in:

diff --git a/lisp/simple.el b/lisp/simple.el
index 80bc968950..838f344ab5 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -1969,6 +1969,23 @@ read-extended-command
        (lambda (string pred action)
          (if (and suggest-key-bindings (eq action 'metadata))
             '(metadata
+               (display-sort-function
+                . (lambda (commands)
+                    (if (minibufferp)
+                        ;; Prefer recently used completions and put the 
default,
+                        ;; if it exists, on top.
+                        (let ((hist (symbol-value 
minibuffer-history-variable)))
+                          (sort commands
+                                (lambda (c1 c2)
+                                  (cond ((equal c1 minibuffer-default) t)
+                                        ((equal c2 minibuffer-default) nil)
+                                        ((and (member c1 hist) (member c2 
hist))
+                                         (> (length (member c1 hist))
+                                            (length (member c2 hist))))
+                                        ((member c1 hist) t)
+                                        ((member c2 hist) nil)
+                                        (t (string-lessp c1 c2))))))
+                      commands)))
               (affixation-function . read-extended-command--affixation)
               (category . command))
            (complete-with-action action obarray string pred)))

reply via email to

[Prev in Thread] Current Thread [Next in Thread]