auto delete overlay with property 'evaporate
[eldoc-overlay.git] / eldoc-overlay-mode.el
blobdd66ddc1a1b69273597d1e628a37a99e8d83a158
1 ;;; eldoc-overlay-mode.el --- Display eldoc with contextual documentation overlay.
3 ;; Author: stardiviner <numbchild@gmail.com>
4 ;; Keywords: eldoc overlay
5 ;; URL: https://github.com/stardiviner/eldoc-overlay-mode
6 ;; Created: 14th Jan 2017
7 ;; Version: 0.0.1
8 ;; Package-Requires: ((emacs "25.1"))
10 ;;; Commentary:
14 ;;; Code:
15 ;;; ----------------------------------------------------------------------------
17 (defvar eldoc-overlay--overlay nil)
19 (defface eldoc-overlay-face
20 '((t (:inherit italic)))
21 "Face for eldoc-overlay-mode."
22 :group 'eldoc)
24 (defun eldoc-overlay--clear-overlay ()
25 "Clear eldoc-overlay overlays."
26 (when (overlayp eldoc-overlay--overlay)
27 (delete-overlay eldoc-overlay--overlay))
28 (remove-hook 'post-command-hook 'eldoc-overlay--clear-overlay))
30 (defun eldoc-overlay--string-display-next-line (string)
31 "Overwrite contents of next line with STRING until next command."
32 (let ((str (copy-sequence string))
33 start-pos end-pos)
34 (unwind-protect
35 (save-excursion
36 (eldoc-overlay--clear-overlay)
37 (forward-line)
38 (setq start-pos (point))
39 (end-of-line)
40 (setq end-pos (point))
41 (setq eldoc-overlay--overlay (make-overlay start-pos end-pos (current-buffer)))
42 ;; Change the face
43 (overlay-put eldoc-overlay--overlay 'face 'eldoc-overlay-face)
44 ;; Hide full line
45 (overlay-put eldoc-overlay--overlay 'display "")
46 (overlay-put eldoc-overlay--overlay 'line-prefix (make-string
47 (current-indentation) ?\s))
48 ;; auto delete overlay with property 'evaporate
49 (overlay-put eldoc-overlay--overlay 'evaporate t)
50 ;; Display message
51 (overlay-put eldoc-overlay--overlay 'before-string str))
52 (add-hook 'post-command-hook 'eldoc-overlay--clear-overlay))))
54 (defun eldoc-overlay-display-message-momentary (format-string &rest args)
55 "Display eldoc message FORMAT-STRING near point with extra ARGS."
56 (when format-string
57 (eldoc-overlay--string-display-next-line
58 (apply 'format format-string args))))
60 (defvar eldoc-overlay-mode-map
61 (let ((map (make-sparse-keymap)))
62 map))
64 ;;;###autoload
65 (define-minor-mode eldoc-overlay-mode
66 "Minor mode for displaying eldoc with contextual documentation overlay."
67 :init-value t
68 :lighter " ElDoc overlay"
69 :keymap eldoc-overlay-mode-map
70 :global t
71 (if eldoc-overlay-mode
72 (setq eldoc-message-function #'eldoc-overlay-display-message-momentary)
73 (setq eldoc-message-function #'eldoc-minibuffer-message)
77 ;;; ----------------------------------------------------------------------------
79 (provide 'eldoc-overlay-mode)
81 ;;; eldoc-overlay-mode.el ends here