aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoão Távora2019-02-12 21:55:34 +0000
committerJoão Távora2019-02-12 21:55:34 +0000
commit0daf79c64acce7dc0371e611e090184a90648ec1 (patch)
tree1f195242cbc143332065afb41a27feb39ea3abb9
parent8b44a4bffcba71da16bf909aae6f550a5374bee1 (diff)
downloademacs-0daf79c64acce7dc0371e611e090184a90648ec1.tar.gz
emacs-0daf79c64acce7dc0371e611e090184a90648ec1.zip
Score flex-style completions according to match tightnessscratch/new-flex-completion-style
The new completion style needs to score completion matches so that we can use it later on when sorting the completions. This is because, in the flex style, "foo" can now match "foobar", "frodo" and "barfromsober" but we probably want "foobar" to appear at the top of the completion list. This change makes the new flex completion style add sort-order hints under the completion string's `completion-style-sort-order' property. * lisp/minibuffer.el (completion-pcm--hilit-commonality): Propertize completion with 'completion-pcm-commonality-score. (completion-flx-all-completions): Propertize completion with completion-style-sort-order and completion-style-annotation.
-rw-r--r--lisp/minibuffer.el37
1 files changed, 30 insertions, 7 deletions
diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index cf626b3f32d..8ea70b14f12 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -3056,20 +3056,38 @@ PATTERN is as returned by `completion-pcm--string->pattern'."
3056 (let* ((pos (if point-idx (match-beginning point-idx) (match-end 0))) 3056 (let* ((pos (if point-idx (match-beginning point-idx) (match-end 0)))
3057 (md (match-data)) 3057 (md (match-data))
3058 (start (pop md)) 3058 (start (pop md))
3059 (end (pop md))) 3059 (end (pop md))
3060 (len (length str))
3061 (score-numerator 0)
3062 (score-denominator 0)
3063 (aux 0)
3064 (update-score
3065 (lambda (a b)
3066 "Update score variables given match range (A B)."
3067 (setq
3068 score-numerator (+ score-numerator (- b a))
3069 score-denominator (+ score-denominator (expt (- a aux) 1.5))
3070 aux b))))
3071 (funcall update-score 0 start)
3060 (while md 3072 (while md
3061 (put-text-property start (pop md) 3073 (funcall update-score start (car md))
3074 (put-text-property start
3075 (pop md)
3062 'font-lock-face 'completions-common-part 3076 'font-lock-face 'completions-common-part
3063 str) 3077 str)
3064 (setq start (pop md))) 3078 (setq start (pop md)))
3065 (put-text-property start end 3079 (put-text-property start end
3066 'font-lock-face 'completions-common-part 3080 'font-lock-face 'completions-common-part
3067 str) 3081 str)
3082 (funcall update-score start end)
3068 (if (> (length str) pos) 3083 (if (> (length str) pos)
3069 (put-text-property pos (1+ pos) 3084 (put-text-property pos (1+ pos)
3070 'font-lock-face 'completions-first-difference 3085 'font-lock-face 'completions-first-difference
3071 str))) 3086 str))
3072 str) 3087 (put-text-property
3088 0 1 'completion-pcm-commonality-score
3089 (/ score-numerator (* len (1+ score-denominator)) 1.0) str))
3090 str)
3073 completions)))) 3091 completions))))
3074 3092
3075(defun completion-pcm--find-all-completions (string table pred point 3093(defun completion-pcm--find-all-completions (string table pred point
@@ -3440,8 +3458,13 @@ which is at the core of flex logic. The extra
3440 string table pred point 3458 string table pred point
3441 #'completion-flex--make-flex-pattern))) 3459 #'completion-flex--make-flex-pattern)))
3442 (when all 3460 (when all
3443 (nconc (completion-pcm--hilit-commonality pattern all) 3461 (let ((hilighted (completion-pcm--hilit-commonality pattern all)))
3444 (length prefix))))) 3462 (mapc
3463 (lambda (comp)
3464 (let ((score (get-text-property 0 'completion-pcm-commonality-score comp)))
3465 (put-text-property 0 1 'completion-style-sort-order (- score) comp)))
3466 hilighted)
3467 (nconc hilighted (length prefix))))))
3445 3468
3446;; Initials completion 3469;; Initials completion
3447;; Complete /ums to /usr/monnier/src or lch to list-command-history. 3470;; Complete /ums to /usr/monnier/src or lch to list-command-history.