warnings.el 13 KB

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