(Feval_buffer): Doc fix.
[emacs.git] / lisp / warnings.el
blob99f345baa55dc6e5008aa4500d736c52765ace32
1 ;;; warnings.el --- log and display warnings
3 ;; Copyright (C) 2002 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: internal
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Commentary:
27 ;; This file implements the entry points `warn', `lwarn'
28 ;; and `display-warnings'.
30 ;;; Code:
32 (defvar warning-levels
33 '((:emergency "Emergency%s: " ding)
34 (:error "Error%s: ")
35 (:warning "Warning%s: ")
36 (:debug "Debug%s: "))
37 "List of severity level definitions for `display-warning'.
38 Each element looks like (LEVEL STRING FUNCTION) and
39 defines LEVEL as a severity level. STRING specifies the
40 description of this level. STRING should use `%s' to
41 specify where to put the warning group information,
42 or it can omit the `%s' so as not to include that information.
44 The optional FUNCTION, if non-nil, is a function to call
45 with no arguments, to get the user's attention.
47 The standard levels are :emergency, :error, :warning and :debug.
48 See `display-warning' for documentation of their meanings.
49 Level :debug is ignored by default (see `warning-minimum-level').")
50 (put 'warning-levels 'risky-local-variable t)
52 ;; These are for compatibility with XEmacs.
53 ;; I don't think there is any chance of designing meaningful criteria
54 ;; to distinguish so many levels.
55 (defvar warning-level-aliases
56 '((emergency . :emergency)
57 (error . :error)
58 (warning . :warning)
59 (notice . :warning)
60 (info . :warning)
61 (critical . :emergency)
62 (alarm . :emergency))
63 "Alist of aliases for severity levels for `display-warning'.
64 Each element looks like (ALIAS . LEVEL) and defines
65 ALIAS as equivalent to LEVEL. LEVEL must be defined in `warning-levels';
66 it may not itself be an alias.")
68 (defcustom warning-minimum-level :warning
69 "Minimum severity level for displaying the warning buffer.
70 If a warning's severity level is lower than this,
71 the warning is logged in the warnings buffer, but the buffer
72 is not immediately displayed. See also `warning-minimum-log-level'."
73 :group 'warnings
74 :type '(choice (const :emergency) (const :error) (const :warning))
75 :version "21.4")
76 (defvaralias 'display-warning-minimum-level 'warning-minimum-level)
78 (defcustom warning-minimum-log-level :warning
79 "Minimum severity level for logging a warning.
80 If a warning severity level is lower than this,
81 the warning is completely ignored."
82 :group 'warnings
83 :type '(choice (const :emergency) (const :error) (const :warning))
84 :version "21.4")
85 (defvaralias 'log-warning-minimum-level 'warning-minimum-log-level)
87 (defcustom warning-suppress-log-types nil
88 "List of warning types that should not be logged.
89 If any element of this list matches the GROUP argument to `display-warning',
90 the warning is completely ignored.
91 The element must match the first elements of GROUP.
92 Thus, (foo bar) as an element matches (foo bar)
93 or (foo bar ANYTHING...) as GROUP.
94 If GROUP is a symbol FOO, that is equivalent to the list (FOO),
95 so only the element (FOO) will match it."
96 :group 'warnings
97 :type '(repeat (repeat symbol))
98 :version "21.4")
100 (defcustom warning-suppress-types nil
101 "Custom groups for warnings not to display immediately.
102 If any element of this list matches the GROUP argument to `display-warning',
103 the warning is logged nonetheless, but the warnings buffer is
104 not immediately displayed.
105 The element must match an initial segment of the list GROUP.
106 Thus, (foo bar) as an element matches (foo bar)
107 or (foo bar ANYTHING...) as GROUP.
108 If GROUP is a symbol FOO, that is equivalent to the list (FOO),
109 so only the element (FOO) will match it.
110 See also `warning-suppress-log-types'."
111 :group 'warnings
112 :type '(repeat (repeat symbol))
113 :version "21.4")
115 ;;; The autoload cookie is so that programs can bind this variable
116 ;;; safely, testing the existing value, before they call one of the
117 ;;; warnings functions.
118 ;;;###autoload
119 (defvar warning-prefix-function nil
120 "Function to generate warning prefixes.
121 This function, if non-nil, is called with two arguments,
122 the severity level and its entry in `warning-levels',
123 and should return the entry that should actually be used.
124 The warnings buffer is current when this function is called
125 and the function can insert text in it. This text becomes
126 the beginning of the warning.")
128 ;;; The autoload cookie is so that programs can bind this variable
129 ;;; safely, testing the existing value, before they call one of the
130 ;;; warnings functions.
131 ;;;###autoload
132 (defvar warning-series nil
133 "Non-nil means treat multiple `display-warning' calls as a series.
134 A marker indicates a position in the warnings buffer
135 which is the start of the current series; it means that
136 additional warnings in the same buffer should not move point.
137 t means the next warning begins a series (and stores a marker here).
138 A symbol with a function definition is like t, except
139 also call that function before the next warning.")
140 (put 'warning-series 'risky-local-variable t)
142 ;;; The autoload cookie is so that programs can bind this variable
143 ;;; safely, testing the existing value, before they call one of the
144 ;;; warnings functions.
145 ;;;###autoload
146 (defvar warning-fill-prefix nil
147 "Non-nil means fill each warning text using this string as `fill-prefix'.")
149 ;;; The autoload cookie is so that programs can bind this variable
150 ;;; safely, testing the existing value, before they call one of the
151 ;;; warnings functions.
152 ;;;###autoload
153 (defvar warning-group-format " (%s)"
154 "Format for displaying the warning group in the warning message.
155 The result of formatting the group this way gets included in the
156 message under the control of the string in `warning-levels'.")
158 (defun warning-numeric-level (level)
159 "Return a numeric measure of the warning severity level LEVEL."
160 (let* ((elt (assq level warning-levels))
161 (link (memq elt warning-levels)))
162 (length link)))
164 (defun warning-suppress-p (group suppress-list)
165 "Non-nil if a warning with group GROUP should be suppressed.
166 SUPPRESS-LIST is the list of kinds of warnings to suppress."
167 (let (some-match)
168 (dolist (elt suppress-list)
169 (if (symbolp group)
170 ;; If GROUP is a symbol, the ELT must be (GROUP).
171 (if (and (consp elt)
172 (eq (car elt) group)
173 (null (cdr elt)))
174 (setq some-match t))
175 ;; If GROUP is a list, ELT must match it or some initial segment of it.
176 (let ((tem1 group)
177 (tem2 elt)
178 (match t))
179 ;; Check elements of ELT until we run out of them.
180 (while tem2
181 (if (not (equal (car tem1) (car tem2)))
182 (setq match nil))
183 (setq tem1 (cdr tem1)
184 tem2 (cdr tem2)))
185 ;; If ELT is an initial segment of GROUP, MATCH is t now.
186 ;; So set SOME-MATCH.
187 (if match
188 (setq some-match t)))))
189 ;; If some element of SUPPRESS-LIST matched,
190 ;; we return t.
191 some-match))
193 ;;;###autoload
194 (defun display-warning (group message &optional level buffer-name)
195 "Display a warning message, MESSAGE.
196 GROUP should be a custom group name (a symbol),
197 or else a list of symbols whose first element is a custom group name.
198 \(The rest of the symbols represent subcategories, for warning purposes
199 only, and you can use whatever symbols you like.)
201 LEVEL should be either :warning, :error, or :emergency.
202 :emergency -- a problem that will seriously impair Emacs operation soon
203 if you do not attend to it promptly.
204 :error -- data or circumstances that are inherently wrong.
205 :warning -- data or circumstances that are not inherently wrong,
206 but raise suspicion of a possible problem.
207 :debug -- info for debugging only.
209 BUFFER-NAME, if specified, is the name of the buffer for logging the
210 warning. By default, it is `*Warnings*'.
212 See the `warnings' custom group for user customization features.
214 See also `warning-series', `warning-prefix-function' and
215 `warning-fill-prefix' for additional programming features."
216 (unless level
217 (setq level :warning))
218 (if (assq level warning-level-aliases)
219 (setq level (cdr (assq level warning-level-aliases))))
220 (or (< (warning-numeric-level level)
221 (warning-numeric-level warning-minimum-log-level))
222 (warning-suppress-p group warning-suppress-log-types)
223 (let* ((groupname (if (consp group) (car group) group))
224 (buffer (get-buffer-create (or buffer-name "*Warnings*")))
225 (level-info (assq level warning-levels))
226 start end)
227 (with-current-buffer buffer
228 (goto-char (point-max))
229 (when (and warning-series (symbolp warning-series))
230 (setq warning-series
231 (prog1 (point-marker)
232 (unless (eq warning-series t)
233 (funcall warning-series)))))
234 (unless (bolp)
235 (newline))
236 (setq start (point))
237 (if warning-prefix-function
238 (setq level-info (funcall warning-prefix-function
239 level level-info)))
240 (insert (format (nth 1 level-info)
241 (format warning-group-format groupname))
242 message)
243 (newline)
244 (when (and warning-fill-prefix (not (string-match "\n" message)))
245 (let ((fill-prefix warning-fill-prefix)
246 (fill-column 78))
247 (fill-region start (point))))
248 (setq end (point))
249 (when (and (markerp warning-series)
250 (eq (marker-buffer warning-series) buffer))
251 (goto-char warning-series)))
252 (if (nth 2 level-info)
253 (funcall (nth 2 level-info)))
254 (if noninteractive
255 ;; Noninteractively, take the text we inserted
256 ;; in the warnings buffer and print it.
257 ;; Do this unconditionally, since there is no way
258 ;; to view logged messages unless we output them.
259 (with-current-buffer buffer
260 (save-excursion
261 ;; Don't include the final newline in the arg
262 ;; to `message', because it adds a newline.
263 (goto-char end)
264 (if (bolp)
265 (forward-char -1))
266 (message "%s" (buffer-substring start (point)))))
267 ;; Interactively, decide whether the warning merits
268 ;; immediate display.
269 (or (< (warning-numeric-level level)
270 (warning-numeric-level warning-minimum-level))
271 (warning-suppress-p group warning-suppress-types)
272 (let ((window (display-buffer buffer)))
273 (when (and (markerp warning-series)
274 (eq (marker-buffer warning-series) buffer))
275 (set-window-start window warning-series))
276 (sit-for 0)))))))
278 ;;;###autoload
279 (defun lwarn (group level message &rest args)
280 "Display a warning message made from (format MESSAGE ARGS...).
281 Aside from generating the message with `format',
282 this is equivalent to `display-warning'.
284 GROUP should be a custom group name (a symbol).
285 or else a list of symbols whose first element is a custom group name.
286 \(The rest of the symbols represent subcategories and
287 can be whatever you like.)
289 LEVEL should be either :warning, :error, or :emergency.
290 :emergency -- a problem that will seriously impair Emacs operation soon
291 if you do not attend to it promptly.
292 :error -- invalid data or circumstances.
293 :warning -- suspicious data or circumstances."
294 (display-warning group (apply 'format message args) level))
296 ;;;###autoload
297 (defun warn (message &rest args)
298 "Display a warning message made from (format MESSAGE ARGS...).
299 Aside from generating the message with `format',
300 this is equivalent to `display-warning', using
301 `emacs' as the group and `:warning' as the level."
302 (display-warning 'emacs (apply 'format message args)))
304 (provide 'warnings)
306 ;;; warnings.el ends here