config

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

vertico-indexed.el (3122B)


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