From 4c657dcd79ee641193ea952cd4c1a01d842aa395 Mon Sep 17 00:00:00 2001 From: Daniel Mendler Date: Mon, 19 Apr 2021 13:25:43 +0200 Subject: [PATCH 6/6] completion-all-sorted-completions: Sort alphabetically * lisp/minibuffer.el (completion-all-sorted-completions): Sort by history position, length and alphabetically. --- lisp/minibuffer.el | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el index 7146604ab4..f0277bf4fc 100644 --- a/lisp/minibuffer.el +++ b/lisp/minibuffer.el @@ -1389,8 +1389,9 @@ completion-all-sorted-completions (setq all (funcall sort-fun all))) (t (if (and (minibufferp) (not (eq minibuffer-history-variable t))) - ;; Sort first by history position and then by length. - ;; Put the default, if it exists, on top. + ;; Sort first by history position, then by length, + ;; then alphabetically. Put the default, if it exists, + ;; on top. (let* ((hist (symbol-value minibuffer-history-variable)) (hash (make-hash-table :test #'equal :size (length hist))) (index 0) @@ -1436,14 +1437,21 @@ completion-all-sorted-completions (length (car c))) (car c))) (pop c))) - (setq all (sort all #'car-less-than-car)) + (setq all (sort all (lambda (c1 c2) + (or (< (car c1) (car c2)) + (and (= (car c1) (car c2)) + (string< (cdr c1) (cdr c2))))))) ;; Drop decoration from the elements (let ((c all)) (while c (setcar c (cdar c)) (pop c)))) - ;; Sort only by length if no history is available. - (setq all (sort all (lambda (c1 c2) (< (length c1) (length c2)))))))) + ;; Sort only by length and alphabetically if no history + ;; is available. + (setq all (sort all (lambda (c1 c2) + (or (< (length c1) (length c2)) + (and (= (length c1) (length c2)) + (string< c1 c2))))))))) ;; Cache the result. This is not just for speed, but also so that ;; repeated calls to minibuffer-force-complete can cycle through -- 2.20.1