emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master 14633fb: * lisp/minibuffer.el (minibuffer--sort-by-key): New func


From: Stefan Monnier
Subject: master 14633fb: * lisp/minibuffer.el (minibuffer--sort-by-key): New function
Date: Mon, 19 Apr 2021 14:57:46 -0400 (EDT)

branch: master
commit 14633fbc097f61ebf548d5304e51031196304e4f
Author: Stefan Monnier <monnier@iro.umontreal.ca>
Commit: Stefan Monnier <monnier@iro.umontreal.ca>

    * lisp/minibuffer.el (minibuffer--sort-by-key): New function
    
    (completion-all-sorted-completions): Use it.
---
 lisp/minibuffer.el | 22 +++++++++-------------
 1 file changed, 9 insertions(+), 13 deletions(-)

diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index dde700f..b98bc6a 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -1355,6 +1355,13 @@ 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 completion-all-sorted-completions (&optional start end)
   (or completion-all-sorted-completions
       (let* ((start (or start (minibuffer-prompt-end)))
@@ -1404,19 +1411,8 @@ scroll the window of possible completions."
                     (cl-incf index))
                   (when (stringp def)
                     (puthash def -1 hash))
-                  ;; Decorate elements with history position
-                  (let ((c all))
-                    (while c
-                      (setcar c (cons (gethash (car c) hash
-                                               most-positive-fixnum)
-                                      (car c)))
-                      (pop c)))
-                  (setq all (sort all #'car-less-than-car))
-                  ;; Drop decoration from the elements
-                  (let ((c all))
-                    (while c
-                      (setcar c (cdar c))
-                      (pop c)))))))
+                  (minibuffer--sort-by-key
+                   all (lambda (x) (gethash x hash most-positive-fixnum)))))))
 
           ;; Cache the result.  This is not just for speed, but also so that
           ;; repeated calls to minibuffer-force-complete can cycle through



reply via email to

[Prev in Thread] Current Thread [Next in Thread]