branch: externals/vertico
commit a73ce1acd0bef5a4f47cbf4717885202f0195919
Author: Daniel Mendler <m...@daniel-mendler.de>
Commit: Daniel Mendler <m...@daniel-mendler.de>

    Add exponential decay
---
 extensions/vertico-sort.el | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/extensions/vertico-sort.el b/extensions/vertico-sort.el
index b8c71d4488..260b5e2ad7 100644
--- a/extensions/vertico-sort.el
+++ b/extensions/vertico-sort.el
@@ -39,11 +39,18 @@
   "History hash table and corresponding base string.")
 
 (defcustom vertico-sort-history-duplicate 10
-  "Number of history positions gained by duplicate history elements.
+  "Maximal number of history positions gained by duplicate history elements.
 The more often a duplicate element occurs in the history, the earlier it
-appears in the completion list.  Note that duplicates occur only if
+appears in the completion list.  The position gain decays exponentially
+with `vertico-sort-history-decay'.  Note that duplicates occur only if
 `history-delete-duplicates' is disabled."
-  :type 'natum
+  :type 'number
+  :group 'vertico)
+
+(defcustom vertico-sort-history-decay 0.005
+  "Exponential decay for the position gain of duplicate elements.
+See also `vertico-sort-history-duplicate'."
+  :type 'float
   :group 'vertico)
 
 (defun vertico-sort--history ()
@@ -68,10 +75,13 @@ appears in the completion list.  Note that duplicates occur 
only if
                      ;; Drop base string from history elements & special file 
handling.
                      (when (or (> base-len 0) file-sep)
                        (setq elem (substring elem base-len (and file-sep (1+ 
file-sep)))))
-                     (puthash elem (if-let ((n (gethash elem ht)))
-                                       (- n vertico-sort-history-duplicate)
-                                     (if (= idx 0) (/ most-negative-fixnum 2) 
idx))
-                              ht))))
+                     (let ((w (if-let ((w (gethash elem ht)))
+                                  ;; Reduce duplicate weight with exponential 
decay.
+                                  (- w (round (* vertico-sort-history-duplicate
+                                                 (exp (* -1.0 
vertico-sort-history-decay idx)))))
+                                ;; Never outrank the most recent element.
+                                (if (= idx 0) (/ most-negative-fixnum 2) 
idx))))
+                       (puthash elem w ht)))))
         (cdr (setq vertico-sort--history (cons base ht))))))
 
 (defun vertico-sort--length-string< (x y)

Reply via email to