dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

corfu-echo.el (3871B)


      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.3
      9 ;; Package-Requires: ((emacs "27.1") (compat "29.1.4.4") (corfu "1.3"))
     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 ((delay (if (consp corfu-echo-delay)
     85                       (funcall (if corfu-echo--message #'cdr #'car)
     86                                corfu-echo-delay)
     87                     corfu-echo-delay))
     88            (extra (nth 4 completion-in-region--data))
     89            (fun (plist-get extra :company-docsig))
     90            (cand (and (>= corfu--index 0)
     91                       (nth corfu--index corfu--candidates))))
     92       (if (<= delay 0)
     93           (corfu-echo--show (funcall fun cand))
     94         (corfu-echo--cancel)
     95         (setq corfu-echo--timer
     96               (run-at-time delay nil
     97                            (lambda ()
     98                              (corfu-echo--show (funcall fun cand))))))
     99     (corfu-echo--cancel)))
    100 
    101 (cl-defmethod corfu--teardown :before (_buf &context (corfu-echo-mode (eql t)))
    102   (corfu-echo--cancel))
    103 
    104 (cl-defmethod corfu--prepare :before (&context (corfu-echo-mode (eql t)))
    105   ;; The refreshing is needed to prevent flicker if corfu-echo-delay=t.
    106   (corfu-echo--cancel corfu-echo--message))
    107 
    108 (provide 'corfu-echo)
    109 ;;; corfu-echo.el ends here