forked from minad/consult
-
Notifications
You must be signed in to change notification settings - Fork 0
/
consult-vertico.el
63 lines (48 loc) · 2.5 KB
/
consult-vertico.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
;;; consult-vertico.el --- Vertico integration for Consult -*- lexical-binding: t -*-
;; Copyright (C) 2021-2023 Free Software Foundation, Inc.
;; This file is 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Integration code for the Vertico completion system. This package
;; is automatically loaded by Consult.
;;; Code:
(require 'consult)
;; NOTE: It is not guaranteed that Vertico is available during compilation!
(defvar vertico--input)
(declare-function vertico--exhibit "ext:vertico")
(declare-function vertico--candidate "ext:vertico")
(declare-function vertico--all-completions "ext:vertico")
(defun consult-vertico--candidate ()
"Return current candidate for Consult preview."
(and vertico--input (vertico--candidate 'highlight)))
(defun consult-vertico--refresh ()
"Refresh completion UI."
(when vertico--input
(setq vertico--input t)
(vertico--exhibit)))
(defun consult-vertico--filter-adv (orig pattern cands category highlight)
"Advice for ORIG `consult--completion-filter' function.
See `consult--completion-filter' for arguments PATTERN, CANDS, CATEGORY
and HIGHLIGHT."
(if (and (bound-and-true-p vertico-mode) (not highlight))
;; Optimize `consult--completion-filter' using the deferred highlighting
;; from Vertico. The advice is not necessary - it is a pure optimization.
(nconc (car (vertico--all-completions pattern cands nil (length pattern)
`(metadata (category . ,category))))
nil)
(funcall orig pattern cands category highlight)))
(advice-add #'consult--completion-filter :around #'consult-vertico--filter-adv)
(add-hook 'consult--completion-candidate-hook #'consult-vertico--candidate)
(add-hook 'consult--completion-refresh-hook #'consult-vertico--refresh)
(define-key consult-async-map [remap vertico-insert] 'vertico-next-group)
(provide 'consult-vertico)
;;; consult-vertico.el ends here