branch: externals/corfu
commit 4accd4b0a5181baf148eb6db42b8cec57c542d36
Author: Daniel Mendler <m...@daniel-mendler.de>
Commit: Daniel Mendler <m...@daniel-mendler.de>

    Improve corfu-history--sort
---
 extensions/corfu-history.el | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/extensions/corfu-history.el b/extensions/corfu-history.el
index 0e05cf3688..fd72683ab5 100644
--- a/extensions/corfu-history.el
+++ b/extensions/corfu-history.el
@@ -69,13 +69,14 @@ occur only if `history-delete-duplicates' is disabled."
   "Sort CANDS by history."
   (unless corfu-history--hash
     (let* ((len (length corfu-history))
-           (weight (round (* len corfu-history-duplicate))))
-      (setq corfu-history--hash (make-hash-table :test #'equal :size len))
-      (cl-loop for elem in corfu-history for idx from 0
-               for dup = (gethash elem corfu-history--hash) do
-               (puthash elem (if dup (- dup weight) idx) 
corfu-history--hash))))
-  (cl-loop for max = most-positive-fixnum for cand on cands do
-           (setcar cand (cons (car cand) (gethash (car cand) 
corfu-history--hash max))))
+           (dup (round (* len corfu-history-duplicate)))
+           (ht (make-hash-table :test #'equal :size len)))
+      (cl-loop for elem in corfu-history for idx from 0 do
+               (puthash elem (if-let ((n (gethash elem ht))) (- n dup) idx) 
ht))
+      (setq corfu-history--hash ht)))
+  (cl-loop for ht = corfu-history--hash for max = most-positive-fixnum
+           for cand on cands do
+           (setcar cand (cons (car cand) (gethash (car cand) ht max))))
   (setq cands (sort cands #'corfu-history--sort-predicate))
   (cl-loop for cand on cands do (setcar cand (caar cand)))
   cands)

Reply via email to