-
Notifications
You must be signed in to change notification settings - Fork 5
/
flycheck-popup-tip.el
138 lines (113 loc) · 4.91 KB
/
flycheck-popup-tip.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
;;; flycheck-popup-tip.el --- Display Flycheck error messages using popup.el
;; Copyright (C) 2017 Saša Jovanić
;; Author: Saša Jovanić <[email protected]>
;; URL: https://github.com/flycheck/flycheck-popup-tip/
;; Keywords: convenience, tools, flycheck, tooltip
;; Version: 0.12.2
;; Package-Version: 0.12.2
;; Package-Requires: ((flycheck "0.22") (popup "0.5") (emacs "24"))
;; This file is not 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 <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This is extension for Flycheck.
;; It displays Flycheck error messages in buffer using `popup.el' library.
;; For more information about Flycheck:
;; http://www.flycheck.org/
;; https://github.com/flycheck/flycheck
;; For more information about this Flycheck extension:
;; https://github.com/flycheck/flycheck-popup-tip
;;;; Setup
;; Add to your `init.el':
;;
;; (with-eval-after-load 'flycheck
;; '(add-hook 'flycheck-mode-hook 'flycheck-popup-tip-mode))
;;; Code:
(require 'flycheck)
(require 'popup)
(defgroup flycheck-popup-tip nil
"Display Flycheck errors in tooltips using popup.el."
:prefix "flycheck-popup-tip-"
:group 'flycheck
:link '(url-link :tag "Github" "https://github.com/flycheck/flycheck-popup-tip"))
(defcustom flycheck-popup-tip-error-prefix "\u27a4 "
"String to be displayed before every error line in popup."
:group 'flycheck-popup-tip
:type 'string
:package-version '(flycheck-popup-tip . "0.10"))
(defvar flycheck-popup-tip-object nil
"Temp popup object.")
(defvar flycheck-popup-tip-old-display-function nil
"The former value of `flycheck-display-errors-function'.")
(defun flycheck-popup-tip-delete-popup ()
"Delete messages currently being shown if any."
(when (popup-live-p flycheck-popup-tip-object)
(popup-delete flycheck-popup-tip-object))
(remove-hook 'pre-command-hook 'flycheck-popup-tip-delete-popup t))
(defun flycheck-popup-tip-format-errors (errors)
"Formats ERRORS messages for display."
(let* ((messages-and-id (mapcar #'flycheck-error-format-message-and-id
(delete-dups errors)))
(messages (sort
(mapcar
(lambda (m) (concat flycheck-popup-tip-error-prefix m))
messages-and-id)
'string-lessp)))
(propertize (mapconcat 'identity messages "\n")
'face
'(:inherit popup-tip-face
:underline nil
:overline nil
:strike-through nil
:box nil
:slant normal
:width normal
:weight normal))))
(defun flycheck-popup-tip-show-popup (errors)
"Display ERRORS, using popup.el library."
(flycheck-popup-tip-delete-popup)
(when errors
(setq flycheck-popup-tip-object
(popup-tip
(flycheck-popup-tip-format-errors errors)
:nostrip t
:nowait t))
(add-hook 'pre-command-hook 'flycheck-popup-tip-delete-popup nil t)))
;;;###autoload
(define-minor-mode flycheck-popup-tip-mode
"A minor mode to show Flycheck error messages in a popup."
:lighter nil
:group 'flycheck-popup-tip
(let ((hooks '(post-command-hook focus-out-hook)))
(cond
;; Use our display function and remember the old one but only if we haven't
;; yet configured it, to avoid activating twice.
((and flycheck-popup-tip-mode
(not (eq flycheck-display-errors-function
#'flycheck-popup-tip-show-popup)))
(setq flycheck-popup-tip-old-display-function
flycheck-display-errors-function
flycheck-display-errors-function
#'flycheck-popup-tip-show-popup)
(dolist (hook hooks)
(add-hook hook #'flycheck-popup-tip-delete-popup nil t)))
;; Reset the display function and remove ourselves from all hooks but only
;; if the mode is still active.
((and (not flycheck-popup-tip-mode)
(eq flycheck-display-errors-function
#'flycheck-popup-tip-show-popup))
(setq flycheck-display-errors-function
flycheck-popup-tip-old-display-function
flycheck-popup-tip-old-display-function nil)
(dolist (hook hooks)
(remove-hook hook 'flycheck-popup-tip-delete-popup t))))))
(provide 'flycheck-popup-tip)
;;; flycheck-popup-tip.el ends here