unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Sorting command completions by recency
@ 2021-02-17 17:53 Juri Linkov
  2021-02-17 18:33 ` Lars Ingebrigtsen
  0 siblings, 1 reply; 8+ messages in thread
From: Juri Linkov @ 2021-02-17 17:53 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 472 bytes --]

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:


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: sort-extended-command.patch --]
[-- Type: text/x-diff, Size: 1478 bytes --]

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)))

^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2021-02-18 17:34 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-17 17:53 Sorting command completions by recency Juri Linkov
2021-02-17 18:33 ` Lars Ingebrigtsen
2021-02-17 19:55   ` Stefan Monnier
2021-02-17 20:03     ` Clemens
2021-02-17 20:58       ` Stefan Monnier
2021-02-18 17:34         ` Juri Linkov
2021-02-17 20:09     ` Juri Linkov
2021-02-17 22:02       ` [External] : " Drew Adams

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).