dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

consult-vertico.el (2785B)


      1 ;;; consult-vertico.el --- Vertico integration for Consult -*- lexical-binding: t -*-
      2 
      3 ;; Copyright (C) 2021-2023 Free Software Foundation, Inc.
      4 
      5 ;; This file is part of GNU Emacs.
      6 
      7 ;; This program is free software: you can redistribute it and/or modify
      8 ;; it under the terms of the GNU General Public License as published by
      9 ;; the Free Software Foundation, either version 3 of the License, or
     10 ;; (at your option) any later version.
     11 
     12 ;; This program is distributed in the hope that it will be useful,
     13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 ;; GNU General Public License for more details.
     16 
     17 ;; You should have received a copy of the GNU General Public License
     18 ;; along with this program.  If not, see <https://www.gnu.org/licenses/>.
     19 
     20 ;;; Commentary:
     21 
     22 ;; Integration code for the Vertico completion system. This package
     23 ;; is automatically loaded by Consult.
     24 
     25 ;;; Code:
     26 
     27 (require 'consult)
     28 
     29 ;; NOTE: It is not guaranteed that Vertico is available during compilation!
     30 (defvar vertico--input)
     31 (defvar vertico--history-hash)
     32 (defvar vertico--lock-candidate)
     33 (declare-function vertico--exhibit "ext:vertico")
     34 (declare-function vertico--candidate "ext:vertico")
     35 (declare-function vertico--all-completions "ext:vertico")
     36 
     37 (defun consult-vertico--candidate ()
     38   "Return current candidate for Consult preview."
     39   (and vertico--input (vertico--candidate 'highlight)))
     40 
     41 (defun consult-vertico--refresh (&optional reset)
     42   "Refresh completion UI, keep current candidate unless RESET is non-nil."
     43   (when vertico--input
     44     (setq vertico--input t)
     45     (when reset
     46       (setq vertico--history-hash nil
     47             vertico--lock-candidate nil))
     48     (vertico--exhibit)))
     49 
     50 (defun consult-vertico--filter-adv (orig pattern cands category highlight)
     51   "Advice for ORIG `consult--completion-filter' function.
     52 See `consult--completion-filter' for arguments PATTERN, CANDS, CATEGORY
     53 and HIGHLIGHT."
     54   (if (and (bound-and-true-p vertico-mode) (not highlight))
     55       ;; Optimize `consult--completion-filter' using the deferred highlighting
     56       ;; from Vertico. The advice is not necessary - it is a pure optimization.
     57       (nconc (car (vertico--all-completions pattern cands nil (length pattern)
     58                                             `(metadata (category . ,category))))
     59              nil)
     60     (funcall orig pattern cands category highlight)))
     61 
     62 (advice-add #'consult--completion-filter :around #'consult-vertico--filter-adv)
     63 (add-hook 'consult--completion-candidate-hook #'consult-vertico--candidate)
     64 (add-hook 'consult--completion-refresh-hook #'consult-vertico--refresh)
     65 (define-key consult-async-map [remap vertico-insert] 'vertico-next-group)
     66 
     67 (provide 'consult-vertico)
     68 ;;; consult-vertico.el ends here