]> git.eshelyaron.com Git - emacs.git/commitdiff
Introduce a variable to control ecomplete sorting
authorLars Ingebrigtsen <larsi@gnus.org>
Tue, 16 Jan 2018 14:22:11 +0000 (15:22 +0100)
committerLars Ingebrigtsen <larsi@gnus.org>
Tue, 16 Jan 2018 23:17:23 +0000 (00:17 +0100)
* lisp/ecomplete.el (ecomplete-sort-predicate): New variable.
(ecomplete-get-matches): Use it.

doc/misc/message.texi
etc/NEWS
lisp/ecomplete.el

index ca06de38d171f7ef0833dfc509785d2c3f16a70b..0d5f85f3dc4135e37976cfe67a9de0c553fc9773 100644 (file)
@@ -1485,6 +1485,9 @@ choose one of these completions, use the @kbd{M-n} command to move
 down to the list.  Use @kbd{M-n} and @kbd{M-p} to move down and up the
 list, and @kbd{RET} to choose a completion.
 
+The @code{ecomplete-sort-predicate} variable controls how
+@code{ecomplete} matches are sorted.
+
 @node Spelling
 @section Spelling
 @cindex spelling
index abbedcf5ecacbd575169438d2482f178999d3d9d..872bd3113fd77d93ddf8a03c95614ba31e19a340 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -230,6 +230,10 @@ are implemented in C using the Jansson library.
 It's a simple convenience function for looking up MIME types based on
 file name extensions.
 
++++
+** The ecomplete sorting has changed to a decay-based algorithm.  This
+can be controlled by the new `ecomplete-sort-predicate' variable.
+
 \f
 * Changes in Emacs 27.1 on Non-Free Operating Systems
 
index 43ab8e691e6379d6619f6288d21748b4284e1447..2197d9512de0fc2b243c00ce5425c31bef9ea7f9 100644 (file)
   :type '(symbol :tag "Coding system")
   :group 'ecomplete)
 
+(defcustom ecomplete-sort-predicate 'ecomplete-decay
+  "Predicate to use when sorting matched.
+The predicate is called with two parameters that represent the
+completion.  Each parameter is a list where the first element is
+the times the completion has been used, the second is the
+timestamp of the most recent usage, and the third item is the
+string that was matched."
+  :type '(radio (function-item :tag "Sort by usage and newness" ecomplete-decay)
+               (function-item :tag "Sort by times used" ecomplete-usage)
+               (function-item :tag "Sort by newness" ecomplete-newness)
+               (function :tag "Other"))
+  :group 'ecomplete)
+
 ;;; Internal variables.
 
 (defvar ecomplete-database nil)
           (loop for (key count time text) in elems
                 when (string-match match text)
                 collect (list count time text))
-          (lambda (l1 l2)
-            (> (car l1) (car l2))))))
+           ecomplete-sort-predicate)))
     (when (> (length candidates) 10)
       (setcdr (nthcdr 10 candidates) nil))
     (unless (zerop (length candidates))
@@ -189,6 +201,21 @@ matches."
        (forward-char 1)))
     (buffer-string)))
 
+(defun ecomplete-usage (l1 l2)
+  (> (car l1) (car l2)))
+
+(defun ecomplete-newness (l1 l2)
+  (> (cadr l1) (cadr l2)))
+
+(defun ecomplete-decay (l1 l2)
+  (> (ecomplete-decay-1 l1) (ecomplete-decay-1 l2)))
+
+(defun ecomplete-decay-1 (elem)
+  ;; We subtract 5% from the item for each week it hasn't been used.
+  (/ (car elem)
+     (expt 1.05 (/ (- (float-time) (cadr elem))
+                   (* 7 24 60 60)))))
+
 (provide 'ecomplete)
 
 ;;; ecomplete.el ends here