branch: externals/vertico commit 3e6bff477e2d9511e429e07ac65dc8ac618fc5cd Author: Daniel Mendler <m...@daniel-mendler.de> Commit: Daniel Mendler <m...@daniel-mendler.de>
Add vertico-multiform extension The extension allows you to configure per-command or per-completion category minibuffer modes. (setq vertico-multiform-commands '((consult-line buffer) (consult-imenu reverse buffer) (execute-extended-command flat))) (setq vertico-multiform-categories '((file buffer grid))) (vertico-multiform-mode) --- README.org | 19 +++---- extensions/vertico-multiform.el | 116 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+), 9 deletions(-) diff --git a/README.org b/README.org index 5ac9018a44..211ea011b0 100644 --- a/README.org +++ b/README.org @@ -231,15 +231,16 @@ extensions. Currently the following extensions come with the Vertico ELPA package: - - [[https://github.com/minad/vertico/blob/main/extensions/vertico-buffer.el][vertico-buffer]]: =vertico-buffer-mode= to display Vertico in a separate buffer - - [[https://github.com/minad/vertico/blob/main/extensions/vertico-directory.el][vertico-directory]]: Commands for Ido-like directory navigation - - [[https://github.com/minad/vertico/blob/main/extensions/vertico-flat.el][vertico-flat]]: =vertico-flat-mode= to enable a flat, horizontal display - - [[https://github.com/minad/vertico/blob/main/extensions/vertico-grid.el][vertico-grid]]: =vertico-grid-mode= to enable a grid display - - [[https://github.com/minad/vertico/blob/main/extensions/vertico-indexed.el][vertico-indexed]]: =vertico-indexed-mode= to select indexed candidates with prefix arguments - - [[https://github.com/minad/vertico/blob/main/extensions/vertico-mouse.el][vertico-mouse]]: =vertico-mouse-mode= to support for scrolling and candidate selection - - [[https://github.com/minad/vertico/blob/main/extensions/vertico-quick.el][vertico-quick]]: Commands to select using Avy-style quick keys - - [[https://github.com/minad/vertico/blob/main/extensions/vertico-repeat.el][vertico-repeat]]: The command =vertico-repeat= repeats the last completion session - - [[https://github.com/minad/vertico/blob/main/extensions/vertico-reverse.el][vertico-reverse]]: =vertico-reverse-mode= to reverse the display + - [[https://github.com/minad/vertico/blob/main/extensions/vertico-buffer.el][vertico-buffer]]: =vertico-buffer-mode= to display Vertico in a separate buffer. + - [[https://github.com/minad/vertico/blob/main/extensions/vertico-directory.el][vertico-directory]]: Commands for Ido-like directory navigation. + - [[https://github.com/minad/vertico/blob/main/extensions/vertico-flat.el][vertico-flat]]: =vertico-flat-mode= to enable a flat, horizontal display. + - [[https://github.com/minad/vertico/blob/main/extensions/vertico-grid.el][vertico-grid]]: =vertico-grid-mode= to enable a grid display. + - [[https://github.com/minad/vertico/blob/main/extensions/vertico-indexed.el][vertico-indexed]]: =vertico-indexed-mode= to select indexed candidates with prefix arguments. + - [[https://github.com/minad/vertico/blob/main/extensions/vertico-mouse.el][vertico-mouse]]: =vertico-mouse-mode= to support for scrolling and candidate selection. + - [[https://github.com/minad/vertico/blob/main/extensions/vertico-multiform.el][vertico-multiform]]: Configure Vertico modes per command or completion category.. + - [[https://github.com/minad/vertico/blob/main/extensions/vertico-quick.el][vertico-quick]]: Commands to select using Avy-style quick keys. + - [[https://github.com/minad/vertico/blob/main/extensions/vertico-repeat.el][vertico-repeat]]: The command =vertico-repeat= repeats the last completion session. + - [[https://github.com/minad/vertico/blob/main/extensions/vertico-reverse.el][vertico-reverse]]: =vertico-reverse-mode= to reverse the display. With these extensions it is possible to adapt Vertico such that it matches your preference or behaves similar to other familiar UIs. For example, the diff --git a/extensions/vertico-multiform.el b/extensions/vertico-multiform.el new file mode 100644 index 0000000000..f0a2f22646 --- /dev/null +++ b/extensions/vertico-multiform.el @@ -0,0 +1,116 @@ +;;; vertico-multiform.el --- Configure Vertico in different forms per command -*- lexical-binding: t -*- + +;; Copyright (C) 2021 Free Software Foundation, Inc. + +;; Author: Daniel Mendler <m...@daniel-mendler.de> +;; Maintainer: Daniel Mendler <m...@daniel-mendler.de> +;; Created: 2021 +;; Version: 0.1 +;; Package-Requires: ((emacs "27.1") (vertico "0.17")) +;; Homepage: 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 <http://www.gnu.org/licenses/>. + +;;; Commentary: + +;; This package is a Vertico extension for fine tuning the Vertico +;; display and other minibuffer modes per command or completion +;; category. For some commands you may want to use the `vertico-buffer' +;; display and for completion categories like file you prefer the +;; `vertico-grid-mode'. +;; +;; Example: +;; +;; (setq vertico-multiform-commands +;; '((consult-line buffer) +;; (consult-imenu reverse buffer) +;; (execute-extended-command flat))) +;; +;; (setq vertico-multiform-categories +;; '((file buffer grid))) +;; +;; (vertico-multiform-mode) + +;;; Code: + +(require 'vertico) + +(defcustom vertico-multiform-commands nil + "Alist of commands and list of modes to turn on per command. +Takes precedence over `vertico-multiform-categories'." + :group 'vertico + :type '(repeat (list symbol))) + +(defcustom vertico-multiform-categories nil + "Alist of categories and list of modes to turn on per categories. +Has lower precedence than `vertico-multiform-commands'." + :group 'vertico + :type '(repeat (list symbol))) + +(defun vertico-multiform--advice (&rest app) + "Advice for `vertico--advice' switching modes on and off. +APP is the original function call." + (let ((modes 'init) + (setup (make-symbol "vertico-multiform--setup")) + (exit (make-symbol "vertico-multiform--exit")) + (depth (1+ (recursion-depth)))) + (fset setup + (lambda () + (cond + ((= depth (recursion-depth)) + (when (eq modes 'init) + (let ((cat (completion-metadata-get + (completion-metadata + (buffer-substring (minibuffer-prompt-end) + (max (minibuffer-prompt-end) (point))) + minibuffer-completion-table + minibuffer-completion-predicate) + 'category))) + (setq modes + (mapcar (lambda (m) + (let ((v (intern (format "vertico-%s-mode" m)))) + (if (fboundp v) v m))) + (or (and cat (alist-get cat vertico-multiform-categories)) + (alist-get this-command vertico-multiform-commands)))))) + (mapc (lambda (f) (funcall f 1)) modes)) + ((= (1+ depth) (recursion-depth)) + (mapc (lambda (f) (funcall f -1)) modes))))) + (fset exit + (lambda () + (cond + ((= depth (recursion-depth)) + (mapc (lambda (f) (funcall f -1)) modes)) + ((= (1+ depth) (recursion-depth)) + (mapc (lambda (f) (funcall f 1)) modes))))) + ;; NOTE: The setup/exit nesting is only correct for shallow recursions. + ;; Hopefully nobody is crazy enough to work at recursion level 99. + (add-hook 'minibuffer-setup-hook setup (+ -99 depth)) + (add-hook 'minibuffer-exit-hook exit (- 99 depth)) + (unwind-protect + (apply app) + (remove-hook 'minibuffer-setup-hook setup) + (remove-hook 'minibuffer-exit-hook exit)))) + +;;;###autoload +(define-minor-mode vertico-multiform-mode + "Configure Vertico in various forms per command." + :global t :group 'vertico + (if vertico-multiform-mode + (advice-add #'vertico--advice :around #'vertico-multiform--advice) + (advice-remove #'vertico--advice #'vertico-multiform--advice))) + +(provide 'vertico-multiform) +;;; vertico-multiform.el ends here