File: vertico-mouse.el

package info (click to toggle)
vertico 2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 388 kB
  • sloc: lisp: 1,658; makefile: 7
file content (93 lines) | stat: -rw-r--r-- 3,365 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
;;; vertico-mouse.el --- Mouse support for Vertico -*- lexical-binding: t -*-

;; Copyright (C) 2021-2025 Free Software Foundation, Inc.

;; Author: Daniel Mendler <mail@daniel-mendler.de>
;; Maintainer: Daniel Mendler <mail@daniel-mendler.de>
;; Created: 2021
;; Version: 2.1
;; Package-Requires: ((emacs "28.1") (compat "30") (vertico "2.1"))
;; URL: 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 <https://www.gnu.org/licenses/>.

;;; Commentary:

;; This package is a Vertico extension, which adds mouse support.

;;; Code:

(require 'vertico)

(defface vertico-mouse
  '((t :inherit highlight))
  "Face used for mouse highlighting."
  :group 'vertico-faces)

(defun vertico-mouse--index (event)
  "Return candidate index at EVENT."
  (when-let ((object (posn-object (event-end event)))
             ((consp object)))
    (get-text-property (cdr object) 'vertico-mouse--index (car object))))

(defun vertico-mouse--click (key)
  "Create command handling mouse click, behave like KEY press."
  (lambda (event)
    (interactive "e")
    ;; Mouse clicks can even happen if another window is selected.
    (with-selected-window (active-minibuffer-window)
      (when-let ((vertico--index (vertico-mouse--index event))
                 (cmd (keymap-local-lookup key)))
        (funcall cmd)))))

(defvar-keymap vertico-mouse-map
  :doc "Additional keymap activated in mouse mode."
  "<mouse-1>" (vertico-mouse--click "RET")
  "<mouse-3>" (vertico-mouse--click "TAB"))
(fset 'vertico-mouse-map vertico-mouse-map)

(defun vertico-mouse--scroll-up (n)
  "Scroll up by N lines."
  (vertico--goto (max 0 (+ vertico--index n))))

(defun vertico-mouse--scroll-down (n)
  "Scroll down by N lines."
  (vertico-mouse--scroll-up (- n)))

;;;###autoload
(define-minor-mode vertico-mouse-mode
  "Mouse support for Vertico."
  :global t :group 'vertico)

(cl-defmethod vertico--format-candidate
  :around (cand prefix suffix index start &context (vertico-mouse-mode (eql t)))
  (setq cand (cl-call-next-method cand prefix
                                  (concat suffix #(" " 0 1 (display (space :align-to right))))
                                  index start))
  (add-text-properties 0 (1- (length cand))
                       `(vertico-mouse--index ,index
                         mouse-face vertico-mouse keymap vertico-mouse-map)
                       cand)
  cand)

(cl-defmethod vertico--setup :after (&context (vertico-mouse-mode (eql t)))
  (when (boundp 'mwheel-coalesce-scroll-events)
    (setq-local mwheel-coalesce-scroll-events t))
  (setq-local mwheel-scroll-up-function #'vertico-mouse--scroll-up
              mwheel-scroll-down-function #'vertico-mouse--scroll-down))

(provide 'vertico-mouse)
;;; vertico-mouse.el ends here