forked from minad/consult
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsult-selectrum.el
98 lines (79 loc) · 4.24 KB
/
consult-selectrum.el
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
;;; consult-selectrum.el --- Selectrum integration for Consult -*- lexical-binding: t -*-
;; This file is not part of GNU Emacs.
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Integration code for the Selectrum completion system. This package
;; is automatically loaded by Consult.
;;; Code:
(require 'consult)
;; NOTE: It is not guaranteed that Selectrum is available during compilation!
(defvar selectrum-default-value-format)
(defvar selectrum-fix-vertical-window-height)
(defvar selectrum-highlight-candidates-function)
(defvar selectrum-is-active)
(defvar selectrum-move-default-candidate)
(defvar selectrum-refine-candidates-function)
(declare-function selectrum-exhibit "selectrum")
(declare-function selectrum-get-current-candidate "selectrum")
(defun consult-selectrum--filter (_category highlight)
"Return selectrum filter function with HIGHLIGHT."
;; Do not use selectrum-is-active here, since we want to always use
;; the Selectrum filtering when Selectrum is installed, even when
;; Selectrum is currently not active.
;; However if `selectrum-refine-candidates-function' is the default
;; function, which uses the completion styles, the Selectrum filtering
;; is not used and `consult--default-completion-filter' takes over.
(when (and (eq completing-read-function 'selectrum-completing-read)
(not (eq selectrum-refine-candidates-function
'selectrum-refine-candidates-using-completions-styles)))
(if highlight
(lambda (str cands)
(funcall selectrum-highlight-candidates-function str
(funcall selectrum-refine-candidates-function str cands)))
selectrum-refine-candidates-function)))
(defun consult-selectrum--candidate ()
"Return current selectrum candidate."
(and selectrum-is-active (selectrum-get-current-candidate)))
(defun consult-selectrum--refresh ()
"Refresh selectrum view."
(when selectrum-is-active
(if consult--narrow
(setq-local selectrum-default-value-format nil)
(kill-local-variable 'selectrum-default-value-format))
(selectrum-exhibit 'keep-selected)))
(cl-defun consult-selectrum--read-setup-adv (candidates &key default-top &allow-other-keys)
"Advice for `consult--read-setup' for Selectrum specific setup.
See `consult--read' for the CANDIDATES and DEFAULT-TOP arguments."
(setq-local selectrum-move-default-candidate default-top)
;; Fix selectrum height for async completion table
(when (functionp candidates) (setq-local selectrum-fix-vertical-window-height t)))
(defun consult-selectrum--split-wrap (orig split)
"Wrap candidates highlight/refinement ORIG function, splitting the input using SPLIT."
(lambda (str cands)
(funcall orig (cadr (funcall split str 0)) cands)))
(defun consult-selectrum--split-setup-adv (orig split)
"Advice for `consult--split-setup' to be used by Selectrum.
ORIG is the original function.
SPLIT is the splitter function."
(if (not selectrum-is-active)
(funcall orig split)
(setq-local selectrum-refine-candidates-function
(consult-selectrum--split-wrap selectrum-refine-candidates-function split))
(setq-local selectrum-highlight-candidates-function
(consult-selectrum--split-wrap selectrum-highlight-candidates-function split))))
(add-hook 'consult--completion-filter-hook #'consult-selectrum--filter)
(add-hook 'consult--completion-candidate-hook #'consult-selectrum--candidate)
(add-hook 'consult--completion-refresh-hook #'consult-selectrum--refresh)
(advice-add #'consult--read-setup :before #'consult-selectrum--read-setup-adv)
(advice-add #'consult--split-setup :around #'consult-selectrum--split-setup-adv)
(provide 'consult-selectrum)
;;; consult-selectrum.el ends here