forked from minad/consult
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsult-selectrum.el
75 lines (58 loc) · 3.15 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
;;; consult-selectrum.el --- Selectrum integration for Consult -*- lexical-binding: t; -*-
;; Author: Daniel Mendler, Consult and Selectrum contributors
;; Maintainer: Daniel Mendler
;; Created: 2020
;; License: GPL-3.0-or-later
;; Version: 0.1
;; Package-Requires: ((consult "0.1") (selectrum "3.0") (emacs "26.1"))
;; Homepage: https://github.com/minad/consult
;; 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:
;; The Selectrum integration for Consult ensures that previews work when using
;; Selectrum. Furthermore, some minor Selectrum-specific `completing-read'
;; tweaks are applied. This is an extra package, since the consult.el package
;; only depends on Emacs core components.
;;; Code:
(require 'consult)
(require 'selectrum)
(defun consult-selectrum--preview-update ()
"Preview function used for Selectrum."
(when-let* ((fun (car consult--preview-stack))
(cand (selectrum-get-current-candidate)))
(funcall fun cand)))
(defun consult-selectrum--preview-setup ()
"Setup preview support for selectrum."
(advice-remove 'selectrum--minibuffer-post-command-hook #'consult-selectrum--preview-update)
(when consult-preview-mode
(advice-add 'selectrum--minibuffer-post-command-hook :after #'consult-selectrum--preview-update)))
(add-hook 'consult-preview-mode-hook #'consult-selectrum--preview-setup)
(consult-selectrum--preview-setup) ;; call immediately to ensure load-order independence
;; HACK: Hopefully selectrum adds something like this to the official API.
;; https://github.com/raxod502/selectrum/issues/243
;; https://github.com/raxod502/selectrum/pull/244
(defsubst consult-selectrum--configure (options)
"Add OPTIONS to the next `selectrum-read' call."
(when (and options (eq completing-read-function #'selectrum-completing-read))
(letrec ((advice (lambda (orig prompt candidates &rest args)
(advice-remove #'selectrum-read advice)
(apply orig prompt candidates (append options args)))))
(advice-add #'selectrum-read :around advice))))
(cl-defun consult-selectrum--read-advice (_prompt _candidates &rest rest &key default-top &allow-other-keys)
"Advice for `consult--read' performing Selectrum-specific configuration.
_PROMPT, _CANDIDATES and REST are ignored.
DEFAULT-TOP keyword argument is used to configure Selectrum."
(consult-selectrum--configure
`(,@(unless default-top '(:no-move-default-candidate t)))))
(advice-add #'consult--read :before #'consult-selectrum--read-advice)
(provide 'consult-selectrum)
;;; consult-selectrum.el ends here