corfu-echo.el (3898B)
1 ;;; corfu-echo.el --- Show candidate documentation in echo area -*- 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: 2022 8 ;; Version: 1.5 9 ;; Package-Requires: ((emacs "28.1") (compat "30") (corfu "1.5")) 10 ;; Homepage: https://github.com/minad/corfu 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 ;; Show candidate documentation in echo area. Enable `corfu-echo-mode'. 30 31 ;;; Code: 32 33 (require 'corfu) 34 (eval-when-compile 35 (require 'subr-x)) 36 37 (defface corfu-echo 38 '((t :inherit completions-annotations)) 39 "Face used for echo area messages." 40 :group 'corfu-faces) 41 42 (defcustom corfu-echo-delay '(2.0 . 1.0) 43 "Show documentation string in the echo area after that number of seconds. 44 The value can be a pair of two floats to specify initial and 45 subsequent delay." 46 :type '(choice (const :tag "Never" nil) 47 (number :tag "Delay in seconds") 48 (cons :tag "Two Delays" 49 (choice :tag "Initial " number) 50 (choice :tag "Subsequent" number))) 51 :group 'corfu) 52 53 (defvar corfu-echo--timer nil 54 "Echo area message timer.") 55 56 (defvar corfu-echo--message nil 57 "Last echo message.") 58 59 (defun corfu-echo--cancel (&optional msg) 60 "Cancel echo timer and refresh MSG." 61 (when corfu-echo--timer 62 (cancel-timer corfu-echo--timer) 63 (setq corfu-echo--timer nil)) 64 (corfu-echo--show msg) 65 (unless corfu-echo--message 66 (setq corfu-echo--timer nil 67 corfu-echo--message nil))) 68 69 (defun corfu-echo--show (msg) 70 "Show MSG in echo area." 71 (when (or msg corfu-echo--message) 72 (setq msg (or msg "") 73 corfu-echo--message msg) 74 (corfu--message "%s" (if (text-property-not-all 0 (length msg) 'face nil msg) 75 msg 76 (propertize msg 'face 'corfu-echo))))) 77 78 ;;;###autoload 79 (define-minor-mode corfu-echo-mode 80 "Show candidate documentation in echo area." 81 :global t :group 'corfu) 82 83 (cl-defmethod corfu--exhibit :after (&context (corfu-echo-mode (eql t)) &optional _auto) 84 (if-let (((not (minibufferp))) 85 (delay (if (consp corfu-echo-delay) 86 (funcall (if corfu-echo--message #'cdr #'car) 87 corfu-echo-delay) 88 corfu-echo-delay)) 89 (extra (nth 4 completion-in-region--data)) 90 (fun (plist-get extra :company-docsig)) 91 (cand (and (>= corfu--index 0) 92 (nth corfu--index corfu--candidates)))) 93 (if (<= delay 0) 94 (corfu-echo--show (funcall fun cand)) 95 (corfu-echo--cancel) 96 (setq corfu-echo--timer 97 (run-at-time delay nil 98 (lambda () 99 (corfu-echo--show (funcall fun cand)))))) 100 (corfu-echo--cancel))) 101 102 (cl-defmethod corfu--teardown :before (_buf &context (corfu-echo-mode (eql t))) 103 (corfu-echo--cancel)) 104 105 (cl-defmethod corfu--prepare :before (&context (corfu-echo-mode (eql t))) 106 ;; The refreshing is needed to prevent flicker if corfu-echo-delay=t. 107 (corfu-echo--cancel corfu-echo--message)) 108 109 (provide 'corfu-echo) 110 ;;; corfu-echo.el ends here