vertico-reverse.el (3255B)
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 ;; Version: 1.8 9 ;; Package-Requires: ((emacs "27.1") (compat "29.1.4.4") (vertico "1.8")) 10 ;; Homepage: https://github.com/minad/vertico 11 12 ;; This file is part of GNU Emacs. 13 14 ;; This program is free software: you can redistribute it and/or modify 15 ;; it under the terms of the GNU General Public License as published by 16 ;; the Free Software Foundation, either version 3 of the License, or 17 ;; (at your option) any later version. 18 19 ;; This program is distributed in the hope that it will be useful, 20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 ;; GNU General Public License for more details. 23 24 ;; You should have received a copy of the GNU General Public License 25 ;; along with this program. If not, see <https://www.gnu.org/licenses/>. 26 27 ;;; Commentary: 28 29 ;; This package is a Vertico extension, which reverses the list of 30 ;; candidates. 31 ;; 32 ;; The mode can be enabled globally or via `vertico-multiform-mode' 33 ;; per command or completion category. Alternatively the reverse 34 ;; display can be toggled temporarily with M-R if 35 ;; `vertico-multiform-mode' is enabled. 36 37 ;;; Code: 38 39 (require 'vertico) 40 (eval-when-compile (require 'cl-lib)) 41 42 (defvar-keymap vertico-reverse-map 43 :doc "Additional keymap activated in reverse mode." 44 "<remap> <beginning-of-buffer>" #'vertico-last 45 "<remap> <minibuffer-beginning-of-buffer>" #'vertico-last 46 "<remap> <end-of-buffer>" #'vertico-first 47 "<remap> <scroll-down-command>" #'vertico-scroll-up 48 "<remap> <scroll-up-command>" #'vertico-scroll-down 49 "<remap> <next-line>" #'vertico-previous 50 "<remap> <previous-line>" #'vertico-next 51 "<remap> <next-line-or-history-element>" #'vertico-previous 52 "<remap> <previous-line-or-history-element>" #'vertico-next 53 "<remap> <backward-paragraph>" #'vertico-next-group 54 "<remap> <forward-paragraph>" #'vertico-previous-group) 55 56 ;;;###autoload 57 (define-minor-mode vertico-reverse-mode 58 "Reverse the Vertico display." 59 :global t :group 'vertico 60 ;; Reset overlays 61 (dolist (buf (buffer-list)) 62 (when-let ((ov (buffer-local-value 'vertico--candidates-ov buf))) 63 (overlay-put ov 'before-string nil))) 64 (cl-callf2 rassq-delete-all vertico-reverse-map minor-mode-map-alist) 65 (when vertico-reverse-mode 66 (push `(vertico--input . ,vertico-reverse-map) minor-mode-map-alist))) 67 68 (cl-defmethod vertico--display-candidates (lines &context (vertico-reverse-mode (eql t))) 69 (move-overlay vertico--candidates-ov (point-min) (point-min)) 70 (setq lines (nreverse lines)) 71 (unless (eq vertico-resize t) 72 (setq lines (nconc (make-list (max 0 (- vertico-count (length lines))) "\n") lines))) 73 (let ((string (apply #'concat lines))) 74 (add-face-text-property 0 (length string) 'default 'append string) 75 (overlay-put vertico--candidates-ov 'before-string string) 76 (overlay-put vertico--candidates-ov 'after-string nil)) 77 (vertico--resize-window (length lines))) 78 79 (provide 'vertico-reverse) 80 ;;; vertico-reverse.el ends here