-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvertico-suspend.el
110 lines (95 loc) · 4.6 KB
/
vertico-suspend.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
99
100
101
102
103
104
105
106
107
108
109
110
;;; vertico-suspend.el --- Suspend the current Vertico session -*- lexical-binding: t -*-
;; Copyright (C) 2021-2025 Free Software Foundation, Inc.
;; Author: Daniel Mendler <mail@daniel-mendler.de>
;; Maintainer: Daniel Mendler <mail@daniel-mendler.de>
;; Created: 2023
;; Version: 2.0
;; Package-Requires: ((emacs "28.1") (compat "30") (vertico "2.0"))
;; URL: https://github.com/minad/vertico
;; 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:
;; This package is a Vertico extension providing the `vertico-suspend'
;; command to suspend the current Vertico completion session. If
;; `vertico-suspend' is called from within the currently active
;; Vertico minibuffer, the completion session is suspended. Otherwise
;; the last session is restored. It is possible to suspend multiple
;; nested Vertico sessions. Note that `vertico-suspend' requires that
;; recursive minibuffers are enabled by setting the customizable
;; variable `enable-recursive-minibuffers' to t.
;;
;; (keymap-global-set "M-S" #'vertico-suspend)
;;
;; See also the related extension `vertico-repeat', which uses a
;; different technique, storing a completion session history.
;;
;; There exists a small issue with `vertico-suspend'. The setting
;; `echo-keystrokes' does not work. Unfortunately this cannot be
;; fixed without modifying the C source of Emacs, since Emacs forcibly
;; disables echo if a minibuffer is active.
;;; Code:
(require 'vertico)
(defvar vertico-buffer--restore)
(declare-function vertico-buffer-mode "ext:vertico-buffer")
(defvar-local vertico-suspend--ov nil)
;;;###autoload
(defun vertico-suspend ()
"Suspend the current completion session.
If the command is invoked from within the Vertico minibuffer, the
current session is suspended. If the command is invoked from
outside the minibuffer, the active minibuffer is either selected
or the latest completion session is restored."
(interactive)
(unless enable-recursive-minibuffers
(user-error "Recursive minibuffers must be enabled"))
(advice-add #'set-minibuffer-message :around #'vertico-suspend--message)
(if-let ((win (active-minibuffer-window))
(buf (window-buffer win))
((buffer-local-value 'vertico--input buf)))
(cond
((minibufferp)
(add-hook 'pre-redisplay-functions #'vertico-suspend--unselect nil 'local)
(setq vertico-suspend--ov (make-overlay (point-min) (point-max) nil t t))
(overlay-put vertico-suspend--ov 'invisible t)
(overlay-put vertico-suspend--ov 'priority 1000)
(overlay-put vertico--candidates-ov 'before-string nil)
(overlay-put vertico--candidates-ov 'after-string nil)
(set-window-parameter win 'no-other-window t)
(when (bound-and-true-p vertico-buffer-mode)
(vertico-buffer-mode -1)
(setq vertico-buffer--restore #'ignore))
(vertico-suspend--unselect))
(t
(select-window win)
(set-window-parameter win 'no-other-window nil)
(remove-hook 'pre-redisplay-functions #'vertico-suspend--unselect 'local)
(when vertico-suspend--ov
(delete-overlay vertico-suspend--ov)
(setq vertico-suspend--ov nil))
(when (eq #'ignore (bound-and-true-p vertico-buffer--restore))
(setq vertico-buffer--restore nil)
(vertico-buffer-mode 1))))
(user-error "No Vertico session to suspend or resume")))
(defun vertico-suspend--unselect (&rest _)
"Ensure that suspended minibuffer is not selected."
(let ((win (get-buffer-window)))
(when (eq win (selected-window))
(unless (frame-root-window-p win)
(window-resize win (- (window-pixel-height win)) nil nil 'pixelwise))
(select-window (minibuffer-selected-window) t))))
(defun vertico-suspend--message (&rest app)
"Apply APP in non-suspended minibuffers, otherwise bail out."
(when-let ((win (active-minibuffer-window))
((not (buffer-local-value 'vertico-suspend--ov (window-buffer win)))))
(apply app)))
(provide 'vertico-suspend)
;;; vertico-suspend.el ends here