From: Eshel Yaron Date: Fri, 29 Mar 2024 19:14:11 +0000 (+0100) Subject: Simplify some sorting functions using the new 'sort' interface X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=6ed334d18d693a85ac69f908b5d5dd12ac796fd0;p=emacs.git Simplify some sorting functions using the new 'sort' interface --- diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el index 899ab54796c..005a5d272ca 100644 --- a/lisp/minibuffer.el +++ b/lisp/minibuffer.el @@ -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.