vertico-reverse.el (3228B)
1 ;;; vertico-reverse.el --- Reverse the Vertico display -*- 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: 2021 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, which reverses the list of 29 ;; candidates. 30 ;; 31 ;; The mode can be enabled globally or via `vertico-multiform-mode' 32 ;; per command or completion category. Alternatively the reverse 33 ;; display can be toggled temporarily with M-R if 34 ;; `vertico-multiform-mode' is enabled. 35 36 ;;; Code: 37 38 (require 'vertico) 39 (eval-when-compile (require 'cl-lib)) 40 41 (defvar-keymap vertico-reverse-map 42 :doc "Additional keymap activated in reverse mode." 43 "<remap> <beginning-of-buffer>" #'vertico-last 44 "<remap> <minibuffer-beginning-of-buffer>" #'vertico-last 45 "<remap> <end-of-buffer>" #'vertico-first 46 "<remap> <scroll-down-command>" #'vertico-scroll-up 47 "<remap> <scroll-up-command>" #'vertico-scroll-down 48 "<remap> <next-line>" #'vertico-previous 49 "<remap> <previous-line>" #'vertico-next 50 "<remap> <next-line-or-history-element>" #'vertico-previous 51 "<remap> <previous-line-or-history-element>" #'vertico-next 52 "<remap> <backward-paragraph>" #'vertico-next-group 53 "<remap> <forward-paragraph>" #'vertico-previous-group) 54 55 ;;;###autoload 56 (define-minor-mode vertico-reverse-mode 57 "Reverse the Vertico display." 58 :global t :group 'vertico 59 ;; Reset overlays 60 (dolist (buf (buffer-list)) 61 (when-let ((ov (buffer-local-value 'vertico--candidates-ov buf))) 62 (overlay-put ov 'before-string nil))) 63 (cl-callf2 rassq-delete-all vertico-reverse-map minor-mode-map-alist) 64 (when vertico-reverse-mode 65 (push `(vertico--input . ,vertico-reverse-map) minor-mode-map-alist))) 66 67 (cl-defmethod vertico--display-candidates (lines &context (vertico-reverse-mode (eql t))) 68 (move-overlay vertico--candidates-ov (point-min) (point-min)) 69 (setq lines (nreverse lines)) 70 (unless (eq vertico-resize t) 71 (setq lines (nconc (make-list (max 0 (- vertico-count (length lines))) "\n") lines))) 72 (let ((string (apply #'concat lines))) 73 (add-face-text-property 0 (length string) 'default 'append string) 74 (overlay-put vertico--candidates-ov 'before-string string) 75 (overlay-put vertico--candidates-ov 'after-string nil)) 76 (vertico--resize-window (length lines))) 77 78 (provide 'vertico-reverse) 79 ;;; vertico-reverse.el ends here