config

Personal configuration.
git clone git://code.dwrz.net/config
Log | Files | Refs

vertico-suspend.el (4693B)


      1 ;;; vertico-suspend.el --- Suspend the current Vertico session -*- lexical-binding: t -*-
      2 
      3 ;; Copyright (C) 2021-2024 Free Software Foundation, Inc.
      4 
      5 ;; Author: Daniel Mendler <mail@daniel-mendler.de>
      6 ;; Maintainer: Daniel Mendler <mail@daniel-mendler.de>
      7 ;; Created: 2023
      8 ;; Package-Requires: ((emacs "28.1") (compat "30") (vertico "1.9"))
      9 ;; URL: https://github.com/minad/vertico
     10 
     11 ;; This file is part of GNU Emacs.
     12 
     13 ;; This program is free software: you can redistribute it and/or modify
     14 ;; it under the terms of the GNU General Public License as published by
     15 ;; the Free Software Foundation, either version 3 of the License, or
     16 ;; (at your option) any later version.
     17 
     18 ;; This program is distributed in the hope that it will be useful,
     19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     21 ;; GNU General Public License for more details.
     22 
     23 ;; You should have received a copy of the GNU General Public License
     24 ;; along with this program.  If not, see <https://www.gnu.org/licenses/>.
     25 
     26 ;;; Commentary:
     27 
     28 ;; This package is a Vertico extension providing the `vertico-suspend'
     29 ;; command to suspend the current Vertico completion session.  If
     30 ;; `vertico-suspend' is called from within the currently active
     31 ;; Vertico minibuffer, the completion session is suspended.  Otherwise
     32 ;; the last session is restored.  It is possible to suspend multiple
     33 ;; nested Vertico sessions.  Note that `vertico-suspend' requires that
     34 ;; recursive minibuffers are enabled by setting the customizable
     35 ;; variable `enable-recursive-minibuffers' to t.
     36 ;;
     37 ;; (keymap-global-set "M-S" #'vertico-suspend)
     38 ;;
     39 ;; See also the related extension `vertico-repeat', which uses a
     40 ;; different technique, storing a completion session history.
     41 ;;
     42 ;; There exists a small issue with `vertico-suspend'.  The setting
     43 ;; `echo-keystrokes' does not work.  Unfortunately this cannot be
     44 ;; fixed without modifying the C source of Emacs, since Emacs forcibly
     45 ;; disables echo if a minibuffer is active.
     46 
     47 ;;; Code:
     48 
     49 (require 'vertico)
     50 
     51 (defvar vertico-buffer--restore)
     52 (declare-function vertico-buffer-mode "ext:vertico-buffer")
     53 (defvar-local vertico-suspend--ov nil)
     54 
     55 ;;;###autoload
     56 (defun vertico-suspend ()
     57   "Suspend the current completion session.
     58 If the command is invoked from within the Vertico minibuffer, the
     59 current session is suspended.  If the command is invoked from
     60 outside the minibuffer, the active minibuffer is either selected
     61 or the latest completion session is restored."
     62   (interactive)
     63   (unless enable-recursive-minibuffers
     64     (user-error "Recursive minibuffers must be enabled"))
     65   (advice-add #'set-minibuffer-message :around #'vertico-suspend--message)
     66   (if-let ((win (active-minibuffer-window))
     67            (buf (window-buffer win))
     68            ((buffer-local-value 'vertico--input buf)))
     69       (cond
     70        ((minibufferp)
     71         (add-hook 'pre-redisplay-functions #'vertico-suspend--unselect nil 'local)
     72         (setq vertico-suspend--ov (make-overlay (point-min) (point-max) nil t t))
     73         (overlay-put vertico-suspend--ov 'invisible t)
     74         (overlay-put vertico-suspend--ov 'priority 1000)
     75         (overlay-put vertico--candidates-ov 'before-string nil)
     76         (overlay-put vertico--candidates-ov 'after-string nil)
     77         (set-window-parameter win 'no-other-window t)
     78         (when (bound-and-true-p vertico-buffer-mode)
     79           (vertico-buffer-mode -1)
     80           (setq vertico-buffer--restore #'ignore))
     81         (vertico-suspend--unselect))
     82        (t
     83         (select-window win)
     84         (set-window-parameter win 'no-other-window nil)
     85         (remove-hook 'pre-redisplay-functions #'vertico-suspend--unselect 'local)
     86         (when vertico-suspend--ov
     87           (delete-overlay vertico-suspend--ov)
     88           (setq vertico-suspend--ov nil))
     89         (when (eq #'ignore (bound-and-true-p vertico-buffer--restore))
     90           (setq vertico-buffer--restore nil)
     91           (vertico-buffer-mode 1))))
     92     (user-error "No Vertico session to suspend or resume")))
     93 
     94 (defun vertico-suspend--unselect (&rest _)
     95   "Ensure that suspended minibuffer is not selected."
     96   (let ((win (get-buffer-window)))
     97     (when (eq win (selected-window))
     98       (unless (frame-root-window-p win)
     99         (window-resize win (- (window-pixel-height win)) nil nil 'pixelwise))
    100       (select-window (minibuffer-selected-window) t))))
    101 
    102 (defun vertico-suspend--message (&rest app)
    103   "Apply APP in non-suspended minibuffers, otherwise bail out."
    104   (when-let ((win (active-minibuffer-window))
    105              ((not (buffer-local-value 'vertico-suspend--ov (window-buffer win)))))
    106     (apply app)))
    107 
    108 (provide 'vertico-suspend)
    109 ;;; vertico-suspend.el ends here