Merge pull request #2 from joaotavora/master

Misc cleanup to eldoc-box 

- Define customization group
- Simplify timer management
- Rearrange to make compiler happy
- Reformat header
This commit is contained in:
Yuan Fu 2018-12-12 13:13:04 -05:00 committed by GitHub
commit 27605bd370
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,11 +1,11 @@
;;; eldoc-box.el --- Display documentation in childframe -*- lexical-binding: t; -*- ;;; eldoc-box.el --- Display documentation in childframe -*- lexical-binding: t; -*-
;; Copyright (C) 2017 Sebastien Chapuis ;; Copyright (C) 2017-2018 Sebastien Chapuis, 2018 Yuan Fu
;; Version: 1.0 ;; Version: 1.0
;; Author: Sebastien Chapuis <sebastien@chapu.is> ;; Author: Sebastien Chapuis <sebastien@chapu.is>
;; Yuan Fu <casouri@gmail.com> made a lot of change to use it for ElDoc ;; Maintainer: Yuan Fu <casouri@gmail.com>
;; URL: https://github.com/casouri/eldoc-box ;; URL: https://github.com/casouri/eldoc-box
;; Package-Requires: ((emacs "26.1")) ;; Package-Requires: ((emacs "26.1"))
@ -30,6 +30,7 @@
;;; Commentary: ;;; Commentary:
;; ;;
;; Made a lot of change to use it for ElDoc
;;; Code: ;;; Code:
;; ;;
@ -38,6 +39,10 @@
;;;; Userland ;;;; Userland
;;;;; Variable ;;;;; Variable
(defgroup eldoc-box nil
"Display Eldoc docs in a pretty child frame."
:prefix "eldoc-box-"
:group 'eldoc)
(defface eldoc-box-border '((((background dark)) . (:background "white")) (defface eldoc-box-border '((((background dark)) . (:background "white"))
(((background light)) . (:background "black"))) (((background light)) . (:background "black")))
@ -95,6 +100,9 @@
(eldoc-box--inject-quit-func)) (eldoc-box--inject-quit-func))
(message "No documentation available")))) (message "No documentation available"))))
(defvar eldoc-box--frame nil ;; A backstage variable
"The frame to display doc.")
(defun eldoc-box-quit-frame () (defun eldoc-box-quit-frame ()
"Hide childframe used by eglot doc." "Hide childframe used by eglot doc."
(interactive) (interactive)
@ -104,10 +112,6 @@
;;;; Backstage ;;;; Backstage
;;;;; Variable ;;;;; Variable
(defvar eldoc-box--frame nil
"The frame to display doc.")
(defvar eldoc-box--buffer "*eldoc-box*" (defvar eldoc-box--buffer "*eldoc-box*"
"The buffer used to display eglot doc.") "The buffer used to display eglot doc.")
@ -181,6 +185,17 @@ Checkout `lsp-ui-doc--make-frame', `lsp-ui-doc--move-frame'."
(setq eldoc-box--frame frame))) (setq eldoc-box--frame frame)))
;;;;; ElDoc ;;;;; ElDoc
(define-minor-mode eldoc-box-hover-mode
"Displays hover documentations in a childframe. This mode is buffer local."
:lighter " ELDOC-BOX"
(if eldoc-box-hover-mode
(add-function :before-until (local 'eldoc-message-function)
#'eldoc-box--eldoc-message-function)
(remove-function (local 'eldoc-message-function) #'eldoc-box--eldoc-message-function)
;; if minor mode is turned off when childframe is visible
;; hide it
(eldoc-box-quit-frame)))
(defvar eldoc-box--cleanup-timer nil (defvar eldoc-box--cleanup-timer nil
"The timer used to cleanup childframe after ElDoc.") "The timer used to cleanup childframe after ElDoc.")
@ -196,48 +211,23 @@ Checkout `lsp-ui-doc--make-frame', `lsp-ui-doc--move-frame'."
;; shut it off. this code can save you ;; shut it off. this code can save you
;; this is gold, right here, you know it my friend? ;; this is gold, right here, you know it my friend?
(when (frame-parameter eldoc-box--frame 'visibility) (when (frame-parameter eldoc-box--frame 'visibility)
(eldoc-box-quit-frame)))) (eldoc-box-quit-frame)))
(setq eldoc-box--cleanup-timer nil))
(defvar eldoc-box--enabled-buffer-list nil
"A list of buffers that enabled `eldoc-box-hover-mode'.")
(defun eldoc-box--maybe-cancel-timer ()
"Cancel `eldoc-box--cleanup-timer' there is no live buffer with `eldoc-box-hover-mode' enabled left."
(when (and eldoc-box--cleanup-timer
(eq (cl-count-if #'buffer-live-p eldoc-box--enabled-buffer-list) 0))
(cancel-timer eldoc-box--cleanup-timer)
(setq eldoc-box--cleanup-timer nil)))
(defun eldoc-box--eldoc-message-function (str &rest args) (defun eldoc-box--eldoc-message-function (str &rest args)
"Frontend for eldoc. Display STR in childframe and ARGS works like `message'." "Frontend for eldoc. Display STR in childframe and ARGS works like `message'."
(if (stringp str) (if (stringp str)
(let ((doc (apply #'format str args))) (let ((doc (apply #'format str args)))
(if (and eldoc-box-only-multi-line (eq (cl-count ?\n doc) 0)) (unless (and eldoc-box-only-multi-line (eq (cl-count ?\n doc) 0))
(apply #'eldoc-minibuffer-message str args) (eldoc-box--display (apply #'format str args))
(eldoc-box--display (apply #'format str args))))
(eldoc-box-quit-frame)))
(define-minor-mode eldoc-box-hover-mode
"Displays hover documentations in a childframe. This mode is buffer local."
:lighter " ELDOC-BOX"
(if eldoc-box-hover-mode
(progn
;; Why a timer? ElDoc is mainly use in minibuffer, ;; Why a timer? ElDoc is mainly use in minibuffer,
;; where the text is constantly being flushed by other commands ;; where the text is constantly being flushed by other commands
;; so ElDoc doesn't try very hard to cleanup ;; so ElDoc doesn't try very hard to cleanup
(unless eldoc-box--cleanup-timer (when eldoc-box--cleanup-timer (cancel-timer eldoc-box--cleanup-timer))
(setq eldoc-box--cleanup-timer (run-with-idle-timer 1 t #'eldoc-box--maybe-cleanup))) (setq eldoc-box--cleanup-timer
(add-to-list 'eldoc-box--enabled-buffer-list (current-buffer)) (run-with-timer 1 nil #'eldoc-box--maybe-cleanup))))
(setq-local eldoc-message-function #'eldoc-box--eldoc-message-function)) (eldoc-box-quit-frame)
(setq-local eldoc-message-function #'eldoc-minibuffer-message) t))
(delete (current-buffer) eldoc-box--enabled-buffer-list)
;; this function has to be called after the current buffer is
;; removed from buffer list
(eldoc-box--maybe-cancel-timer)
;; if minor mode is turned off when childframe is visible
;; hide it
(eldoc-box-quit-frame)))
(provide 'eldoc-box) (provide 'eldoc-box)