-
Notifications
You must be signed in to change notification settings - Fork 5
/
error-tip.el
243 lines (213 loc) · 8.76 KB
/
error-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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
;;; error-tip.el --- showing error library by popup.el -*- lexical-binding: t; -*-
;; Copyright (C) 2014 by Yuta Yamada
;; Author: Yuta Yamada <cokesboy"at"gmail.com>
;; URL: https://github.com/yuutayamada/flycheck-tip
;; Version: 0.5.0
;; Package-Requires: ((emacs "24.1") (popup "0.5.0"))
;;; License:
;; 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:
;;; Code:
(require 'cl-lib)
(require 'popup)
(require 'notifications) ; this introduced from Emacs 24
(defvar error-tip-notify-keep-messages nil
"If the value is non-nil, keep error messages to notification area.
This feature only activates when you leave from popup's message.")
(defvar error-tip-notify-last-notification nil
"Last notification id.")
(defvar error-tip-notify-timeout (* 60 1000)
"Value for time out. The default value is 1 minute.")
(defvar error-tip-notify-parametors
'(:title "flycheck-tip" :category "im.error")
"Parameters for ‘error-tip-notify’.
You can add ‘notifications-notify’s parametors without :body, :replaces-id and
:timeout.
Example:
(setq error-tip-notify-parametors
(append error-tip-notify-parametors '(:app-icon \"/path/to/icon-file\")))")
;; INTERNAL VARIABLE
(defvar error-tip-popup-object nil)
(defvar error-tip-timer-object nil)
(defvar error-tip-current-errors nil)
(defvar error-tip-timer-delay 0.3
"Whether how much delay showing error popup.
If you set nil to this variable, then do not use delay timer.")
(defvar error-tip-newline-character nil
"Use this variable if you want change specific characters to turn to newlines.")
(defvar error-tip-state nil)
(defun error-tip-cycle (errors &optional reverse)
(error-tip-delete-popup)
(setq error-tip-state nil)
(when errors
(let*
((next (assoc-default :next errors))
(previous (assoc-default :previous errors))
(cur-line (assoc-default :current-line errors))
(jump (lambda (errs)
;; Set errors forcefully at EOB
(when (and (eobp) (eq (point) (point-at-bol)))
(setq errs previous))
(goto-char (point-min))
(unless (line-move (1- (error-tip-get (car errs) 'line)) t)
(push (cons 'eob (line-number-at-pos)) error-tip-state))
(setq error-tip-current-errors errs)
(if (null error-tip-timer-delay)
(error-tip-popup-error-message (error-tip-get-errors))
(error-tip-cancel-timer)
(error-tip-register-timer))))
(target (if (not reverse)
(or next previous cur-line)
(reverse (or previous next cur-line)))))
(funcall jump target))))
(defun error-tip-get (err element)
(when (fboundp 'flycheck-tip--get)
(flycheck-tip--get element err)))
(defun error-tip-collect-current-file-errors (errors)
"Collect errors from ERRORS."
(cl-loop with c-line = (line-number-at-pos (point))
for err in errors
for err-line = (error-tip-get err 'line)
if (and buffer-file-truename ; whether file or buffer
(not (equal (expand-file-name buffer-file-truename)
(error-tip-get err 'file))))
do '() ; skip
else if (< c-line err-line)
collect err into next
else if (> c-line err-line)
collect err into previous
else if (= c-line err-line)
collect err into current-line
finally return (when (or next previous current-line)
(list (cons :next next)
(cons :previous previous)
(cons :current-line current-line)))))
(defun error-tip-popup-error-message (errors &optional point)
"Popup error message(s) from ERRORS.
If there are multiple errors on current line, all current line's errors are
appeared. The POINT arg is a point to show up error(s)."
(setq error-tip-popup-object
(popup-tip
(error-tip-format errors)
:nowait t
:point (let ((p (or point (error-tip-get-point))))
(if (and (eobp) (eq (point) (point-at-bol)))
(1- p)
p))))
(add-hook 'pre-command-hook 'error-tip-delete-popup))
(defun error-tip-get-point ()
"Return point where the popup message emerges."
(1+ (point-at-bol)))
(defun error-tip-format (errors)
"Format ERRORS."
(let ((messages (format "*%s" (mapconcat 'identity errors "\n*"))))
(if error-tip-newline-character
(replace-regexp-in-string error-tip-newline-character "\n" messages)
messages)))
(defun error-tip-get-errors ()
"Get errors."
(cl-loop with current-line = (line-number-at-pos (point))
for error in error-tip-current-errors
for e-line = (error-tip-get error 'line)
for e-str = (error-tip-get error 'message)
if (or (equal current-line e-line)
(and (equal 1 current-line)
(equal 0 e-line)))
collect e-str into result
else if (or (and (< (- 1 current-line) e-line)
(> (+ 1 current-line) e-line))
;; #12
(let ((line-eob (assoc-default 'eob error-tip-state)))
(and line-eob (<= line-eob e-line))))
collect e-str into fallback
finally return (or result fallback)))
(defun error-tip-delete-popup ()
"Delete popup object."
(condition-case err
(when (popup-live-p error-tip-popup-object)
(popup-delete error-tip-popup-object)
(when error-tip-notify-keep-messages (error-tip-notify)))
(error err))
(remove-hook 'pre-command-hook 'error-tip-delete-popup))
(defun error-tip-register-timer ()
"Register timer that show error message."
(setq error-tip-timer-object
(run-with-timer
error-tip-timer-delay nil
(lambda ()
(error-tip-popup-error-message (error-tip-get-errors))))))
(defun error-tip-cancel-timer ()
"Cancel `error-tip-timer-object'."
(when (timerp error-tip-timer-object)
(cancel-timer error-tip-timer-object)))
;;;###autoload
(defun error-tip-error-p ()
"Return non-nil if error is occurred in current buffer.
This function can catch error against flycheck, and flymake."
(cond
((bound-and-true-p flycheck-current-errors)
'flycheck)
((or (bound-and-true-p flymake-err-info)
(and (fboundp 'flymake-diagnostics)
(flymake-diagnostics)))
'flymake)
(t nil)))
;;;###autoload
(defun error-tip-cycle-dwim (&optional reverse)
"Showing error function.
This function switches proper error showing function by context.
(whether flycheck or flymake) The REVERSE option jumps by inverse if
the value is non-nil."
(interactive)
(funcall (cl-case (error-tip-error-p)
(flycheck 'flycheck-tip-cycle)
(flymake 'flymake-tip-cycle))
reverse))
;;;###autoload
(defun error-tip-cycle-dwim-reverse ()
"Same as ‘error-tip-cycle-dwim’, but it jumps to inverse direction."
(interactive)
(error-tip-cycle-dwim t))
;; Show errors by using notifications.el(D-Bus)
(defun error-tip-notify ()
"Keep ERROR-MESSAGES on notification area.
See also ‘error-tip-notify-keep-messages’"
(setq error-tip-notify-last-notification
(apply
`((lambda ()
(notifications-notify
,@(and error-tip-notify-parametors)
:body ,(format "%s"
(error-tip-format
(if (cl-struct-p (car error-tip-current-errors))
(error-tip-get-errors)
error-tip-current-errors)))
:replaces-id error-tip-notify-last-notification
:timeout error-tip-notify-timeout))))))
;; Manual test:
;; (defun error-tip--test-toggle-flymake-and-flycheck ()
;; (interactive)
;; (if flymake-mode
;; (flymake-mode 0)
;; (flymake-mode 1))
;; (if flycheck-mode
;; (flycheck-mode 0)
;; (flycheck-mode 1) ))
;; (error-tip--test-toggle-flymake-and-flycheck)
(provide 'error-tip)
;; Local Variables:
;; coding: utf-8
;; mode: emacs-lisp
;; End:
;;; error-tip.el ends here