aboutsummaryrefslogtreecommitdiff
path: root/.config/emacs/completion-sorting.el
blob: c5c714ae535dd5036dec24311d6574e4a719a010 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
;; https://kristofferbalintona.me/articles/complement-corfu-vertico-and-completion-preview-with-prescientel-sorting/

(use-package prescient
  :ensure t
  :custom
  (prescient-aggressive-file-save t)
  (prescient-sort-length-enable t) ; testing this out
  (prescient-sort-full-matches-first t)
  (prescient-history-length 200)
  (prescient-frequency-decay 0.997)
  (prescient-frequency-threshold 0.05)
  :config
  (prescient-persist-mode 1))

;; Integration with corfu
(use-package corfu-prescient
  :ensure t
  :demand t
  :after corfu prescient
  :custom
  (corfu-prescient-enable-sorting t)
  (corfu-prescient-override-sorting nil) ; Don't override `display-sort-function'
  (corfu-prescient-enable-filtering nil) ; We want orderless to do the filtering
  :config
  (corfu-prescient-mode 1))

;; Integration with vertico
(use-package vertico-prescient
  :ensure t
  :demand t
  :after vertico prescient
  :custom
  (vertico-prescient-enable-sorting t)
  (vertico-prescient-override-sorting nil)
  (vertico-prescient-enable-filtering nil) ; We want orderless to do the filtering
  :config
  (vertico-prescient-mode 1))

;; Have `completion-preview-mode' use prescient's sorting algorithm
(with-eval-after-load 'prescient
  (setopt completion-preview-sort-function #'prescient-completion-sort))

(add-variable-watcher 'corfu-sort-function
                      (lambda (_symbol newval operation where)
                        "Match the value of `completion-preview-sort-function' to `corfu-sort-function'.
If `corfu-sort-function' is set buffer-locally, also set
`completion-preview-sort-function' buffer-locally.  Otherwise, change
the default value of `completion-preview-sort-function' accordingly.

This action only applies when the value of `corfu-sort-function' is
set (i.e., OPERATION is \\='set).  This excludes, e.g., let bindings."
                        (when (equal operation 'set)
                          (if where
                              (with-current-buffer where
                                (setq-local completion-preview-sort-function newval))
                            (setopt completion-preview-sort-function newval)))))