]> git.eshelyaron.com Git - emacs.git/commitdiff
Simplify some sorting functions using the new 'sort' interface
authorEshel Yaron <me@eshelyaron.com>
Fri, 29 Mar 2024 19:14:11 +0000 (20:14 +0100)
committerEshel Yaron <me@eshelyaron.com>
Fri, 29 Mar 2024 19:19:19 +0000 (20:19 +0100)
lisp/minibuffer.el

index 899ab54796c2d066e10611afb3cd99b4e5147120..005a5d272ca9a55892321ccf55a0a9674aa862ad 100644 (file)
@@ -1874,13 +1874,6 @@ scroll the window of possible completions."
     (if (eq (car bounds) base) md-at-point
       (completion-metadata (substring string 0 base) table pred))))
 
-(defun minibuffer--sort-by-key (elems keyfun)
-  "Return ELEMS sorted by increasing value of their KEYFUN.
-KEYFUN takes an element of ELEMS and should return a numerical value."
-  (mapcar #'cdr
-          (sort (mapcar (lambda (x) (cons (funcall keyfun x) x)) elems)
-                 #'car-less-than-car)))
-
 (defun minibuffer--sort-by-position (hist elems)
   "Sort ELEMS by their position in HIST."
   (let ((hash (make-hash-table :test #'equal :size (length hist)))
@@ -1890,15 +1883,11 @@ KEYFUN takes an element of ELEMS and should return a numerical value."
       (unless (gethash c hash)
         (puthash c index hash))
       (cl-incf index))
-    (minibuffer--sort-by-key
-     elems (lambda (x) (gethash x hash most-positive-fixnum)))))
+    (sort elems :key (lambda (x) (gethash x hash most-positive-fixnum)))))
 
 (defun minibuffer--sort-by-length-alpha (elems)
   "Sort ELEMS first by length, then alphabetically."
-  (sort elems (lambda (c1 c2)
-                (or (< (length c1) (length c2))
-                    (and (= (length c1) (length c2))
-                         (string< c1 c2))))))
+  (sort elems :key (lambda (c) (cons (length c) c))))
 
 (defun minibuffer--sort-preprocess-history (base)
   "Preprocess history.
@@ -1922,7 +1911,7 @@ Remove completion BASE prefix string from history elements."
 
 (defun minibuffer-sort-by-length (completions)
   "Sort COMPLETIONS by length."
-  (sort completions (lambda (a b) (< (length a) (length b)))))
+  (sort completions :key #'length))
 
 (defun minibuffer-sort-alphabetically (completions)
   "Sort COMPLETIONS alphabetically.
@@ -1931,7 +1920,7 @@ COMPLETIONS are sorted alphabetically by `string-lessp'.
 
 This is a suitable function to use for `completions-sort' or to
 include as `sort-function' in completion metadata."
-  (sort completions #'string-lessp))
+  (sort completions))
 
 (defvar minibuffer-completion-base nil
   "The base for the current completion.
@@ -1953,7 +1942,7 @@ this call of `completing-read', COMPLETIONS are sorted only by
 
 This is a suitable function to use for `completions-sort' or to
 include as `sort-function' in completion metadata."
-  (let ((alphabetized (sort completions #'string-lessp)))
+  (let ((alphabetized (sort completions)))
     ;; Only use history when it's specific to these completions.
     (if (eq minibuffer-history-variable
             (default-value minibuffer-history-variable))
@@ -3077,11 +3066,7 @@ completions list."
                  group-fun
                  (pcase completions-group-sort
                    ('nil #'identity)
-                   ('alphabetical
-                    (lambda (groups)
-                      (sort groups
-                            (lambda (x y)
-                              (string< (car x) (car y))))))
+                   ('alphabetical #'sort)
                    (_ completions-group-sort))
                  completions)))
 
@@ -4037,27 +4022,16 @@ and `read-file-name-function'."
 
 (defun minibuffer--sort-file-names-by-last-modified-time (files)
   "Sort file name completion candidates FILES by last modified time."
-  (let ((file-time-alist
-         (mapcar (lambda (file)
-                   (cons file
-                         (file-attribute-modification-time
+  (sort files
+        :key (lambda (f)
+               (list (or (file-attribute-modification-time
                           (ignore-errors
                             (file-attributes
                              (substitute-in-file-name
-                              (concat minibuffer-completion-base file)))))))
-                 files)))
-    (sort files (lambda (a b)
-                  (let ((atime (alist-get a file-time-alist
-                                          nil nil #'string=))
-                        (btime (alist-get b file-time-alist
-                                          nil nil #'string=)))
-                    (if atime
-                        (or (not btime)
-                            ;; Put more recently modified files first.
-                            (time-less-p btime atime)
-                            (and (time-equal-p atime btime)
-                                 (string-lessp a b)))
-                      (and (not btime) (string-lessp a b))))))))
+                              (concat minibuffer-completion-base f)))))
+                         `(,most-positive-fixnum))
+                     f))
+        :reverse t))
 
 (defun read-file-name-default (prompt &optional dir default-filename mustmatch initial predicate)
   "Default method for reading file names.