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