config

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

vertico-indexed.el (3095B)


      1 ;;; vertico-indexed.el --- Select indexed candidates -*- 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 prefixes candidates with indices
     29 ;; if enabled via `vertico-indexed-mode'.  It allows you to select candidates
     30 ;; with prefix arguments.  This is designed to be a faster alternative to
     31 ;; selecting a candidate with `vertico-next' and `vertico-previous'.
     32 
     33 ;;; Code:
     34 
     35 (require 'vertico)
     36 
     37 (defface vertico-indexed
     38   '((t :height 0.75 :inherit font-lock-comment-face))
     39   "Face used for the candidate index prefix."
     40   :group 'vertico-faces)
     41 
     42 (defcustom vertico-indexed-start 0
     43   "Start of the indexing."
     44   :group 'vertico
     45   :type 'natnum)
     46 
     47 (defvar vertico-indexed--commands
     48   '(vertico-insert vertico-exit vertico-directory-enter))
     49 (defvar-local vertico-indexed--min 0)
     50 (defvar-local vertico-indexed--max 0)
     51 
     52 ;;;###autoload
     53 (define-minor-mode vertico-indexed-mode
     54   "Prefix candidates with indices."
     55   :global t :group 'vertico)
     56 
     57 (cl-defmethod vertico--prepare :before (&context (vertico-indexed-mode (eql t)))
     58   (when (and prefix-arg (memq this-command vertico-indexed--commands))
     59     (let ((index (+ vertico-indexed--min
     60                     (- (prefix-numeric-value prefix-arg)
     61                        vertico-indexed-start))))
     62       (if (and (>= index vertico-indexed--min)
     63                (<= index vertico-indexed--max)
     64                (/= vertico--total 0))
     65           (setq vertico--index index prefix-arg nil)
     66         (minibuffer-message "Out of range")
     67         (setq this-command #'ignore)))))
     68 
     69 (cl-defmethod vertico--format-candidate :around
     70   (cand prefix suffix index start &context (vertico-indexed-mode (eql t)))
     71   (setq vertico-indexed--min start vertico-indexed--max index)
     72   (cl-call-next-method
     73    cand
     74    (concat (propertize (format
     75                         (if (> (+ vertico-indexed-start vertico-count) 10)
     76                             "%2d " "%1d ")
     77                         (+ (- index start) vertico-indexed-start))
     78                        'face 'vertico-indexed)
     79            prefix)
     80    suffix index start))
     81 
     82 (provide 'vertico-indexed)
     83 ;;; vertico-indexed.el ends here