diff options
| author | Daniel Mendler <mail@daniel-mendler.de> | 2022-11-18 12:12:18 +0100 |
|---|---|---|
| committer | Daniel Mendler <mail@daniel-mendler.de> | 2022-11-18 12:17:13 +0100 |
| commit | 638e33a98db3574261bc41b738186d5f6b87b39d (patch) | |
| tree | b8d0405022baa935a096920ef5a3a790f513b940 /extensions | |
| parent | eb1cf628432dca5131d3aeaaab135b1d97a0718d (diff) | |
BREAKING CHANGE: Extract corfu-echo extension from corfu.el
Enable corfu-echo-mode to enable echo messages. The corfu-infoframe somewhat
conflicts with corfu-echo, since it is disturbing it both the info frame and the
echo message pop up after each other. Therefore it makes sense to maintain
corfu-echo as a separate extension, on the same level as corfu-infoframe.
Diffstat (limited to 'extensions')
| -rw-r--r-- | extensions/corfu-echo.el | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/extensions/corfu-echo.el b/extensions/corfu-echo.el new file mode 100644 index 0000000..9f01cdd --- /dev/null +++ b/extensions/corfu-echo.el @@ -0,0 +1,115 @@ +;;; corfu-echo.el --- Show candidate documentation in echo area -*- lexical-binding: t -*- + +;; Copyright (C) 2021-2022 Free Software Foundation, Inc. + +;; Author: Daniel Mendler <mail@daniel-mendler.de> +;; Maintainer: Daniel Mendler <mail@daniel-mendler.de> +;; Created: 2022 +;; Version: 0.1 +;; Package-Requires: ((emacs "27.1") (corfu "0.28")) +;; Homepage: https://github.com/minad/corfu + +;; 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: + +;; Show candidate documentation in echo area. Enable `corfu-echo-mode'. + +;;; Code: + +(require 'corfu) +(eval-when-compile + (require 'subr-x)) + +(defface corfu-echo + '((t :inherit completions-annotations)) + "Face used for echo area messages." + :group 'corfu-faces) + +(defcustom corfu-echo-delay '(1.0 . 0.2) + "Show documentation string in the echo area after that number of seconds. +Set to t for an instant message. The value can be a pair of two +floats to specify initial and subsequent delay." + :type '(choice (const :tag "Never" nil) + (const :tag "Instant" t) + (number :tag "Delay in seconds") + (cons :tag "Two Delays" + (choice :tag "Initial " number) + (choice :tag "Subsequent" number))) + :group 'corfu) + +(defvar-local corfu-echo--timer nil + "Echo area message timer.") + +(defvar-local corfu-echo--message nil + "Last echo message.") + +(defun corfu-echo--cancel (&optional hide) + "Cancel echo timer and optionally HIDE message." + (when corfu-echo--timer + (cancel-timer corfu-echo--timer) + (setq corfu-echo--timer nil)) + (corfu-echo--show (unless hide corfu-echo--message))) + +(defun corfu-echo--show (msg) + "Show MSG in echo area." + (when (or msg corfu-echo--message) + (setq msg (or msg "") + corfu-echo--message msg) + (corfu--message "%s" (if (text-property-not-all 0 (length msg) 'face nil msg) + msg + (propertize msg 'face 'corfu-echo))))) + +(defun corfu-echo--exhibit (&rest _) + "Show documentation string of current candidate in echo area." + (if-let* ((delay (if (consp corfu-echo-delay) + (funcall (if corfu-echo--message #'cdr #'car) + corfu-echo-delay) + corfu-echo-delay)) + (fun (plist-get corfu--extra :company-docsig)) + (cand (and (>= corfu--index 0) + (nth corfu--index corfu--candidates)))) + (if (or (eq delay t) (<= delay 0)) + (corfu-echo--show (funcall fun cand)) + (corfu-echo--cancel 'hide) + (setq corfu-echo--timer + (run-at-time delay nil + (lambda () + (corfu-echo--show (funcall fun cand)))))) + (corfu-echo--cancel 'hide))) + +(defun corfu-echo--teardown () + "Teardown echo display." + (corfu-echo--cancel 'hide) + (kill-local-variable 'corfu-echo--timer) + (kill-local-variable 'corfu-echo--message)) + +;;;###autoload +(define-minor-mode corfu-echo-mode + "Show candidate documentation in echo area." + :global t :group 'corfu + (cond + (corfu-echo-mode + (advice-add #'corfu--pre-command :before #'corfu-echo--cancel) + (advice-add #'corfu--exhibit :after #'corfu-echo--exhibit) + (advice-add #'corfu--teardown :before #'corfu-echo--teardown)) + (t + (advice-remove #'corfu--pre-command #'corfu-echo--cancel) + (advice-remove #'corfu--exhibit #'corfu-echo--exhibit) + (advice-remove #'corfu--teardown #'corfu-echo--teardown)))) + +(provide 'corfu-echo) +;;; corfu-echo.el ends here |
