Mercurial > emacs
annotate lisp/progmodes/cwarn.el @ 62733:545cba9e95d4
(Info-mode, Info-edit-mode): Use run-mode-hooks.
author | Lute Kamstra <lute@gnu.org> |
---|---|
date | Thu, 26 May 2005 13:01:03 +0000 |
parents | 6535e32999e7 |
children | 18a818a2ee7c 02f1dbc4a199 |
rev | line source |
---|---|
38436
b174db545cfd
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
30812
diff
changeset
|
1 ;;; cwarn.el --- highlight suspicious C and C++ constructions |
26963 | 2 |
61283
6535e32999e7
(cwarn-mode): Specify :group.
Lute Kamstra <lute@gnu.org>
parents:
52401
diff
changeset
|
3 ;; Copyright (C) 1999, 2000, 2001, 2005 Free Software Foundation, Inc. |
26963 | 4 |
5 ;; Author: Anders Lindgren <andersl@andersl.com> | |
6 ;; Keywords: c, languages, faces | |
7 ;; X-Url: http://www.andersl.com/emacs | |
8 ;; Version: 1.3.1 1999-12-13 | |
9 | |
10 ;; This file is part of GNU Emacs. | |
11 | |
12 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
13 ;; it under the terms of the GNU General Public License as published by | |
14 ;; the Free Software Foundation; either version 2, or (at your option) | |
15 ;; any later version. | |
16 | |
17 ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 ;; GNU General Public License for more details. | |
21 | |
22 ;; You should have received a copy of the GNU General Public License | |
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
25 ;; Boston, MA 02111-1307, USA. | |
26 | |
27 ;;; Commentary: | |
28 | |
29 ;;{{{ Documentation | |
30 | |
31 ;; Description: | |
32 ;; | |
33 ;; CWarn is a package that highlights suspicious C and C++ constructions. | |
34 ;; | |
35 ;; For example, take a look at the following piece of C code: | |
36 ;; | |
37 ;; if (x = 0); | |
38 ;; foo(); | |
39 ;; | |
40 ;; The code contains two, possibly fatal, bugs. The first is that the | |
41 ;; assignment operator "=" is used as part of the test; the user | |
44447
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
42 ;; probably meant to use the comparison operator "==". |
26963 | 43 ;; |
44 ;; The second problem is that an extra semicolon is placed after | |
45 ;; closing parenthesis of the test expression. This makes the body of | |
46 ;; the if statement to be an empty statement, not the call to the | |
47 ;; function "foo", as the user probably intended. | |
48 ;; | |
49 ;; This package is capable of highlighting the following C and C++ | |
50 ;; constructions: | |
51 ;; | |
52 ;; * Assignments inside expressions, including variations like "+=". | |
53 ;; * Semicolon following immediately after `if', `for', and `while' | |
54 ;; (except, of course, after a `do .. while' statement). | |
55 ;; * C++ functions with reference parameters. | |
56 ;; | |
57 ;; Note that none of the constructions highlighted (especially not C++ | |
44447
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
58 ;; reference parameters) are considered errors by the language |
26963 | 59 ;; definitions. |
60 | |
61 ;; Usage: | |
62 ;; | |
63 ;; CWarn is implemented as two minor modes: `cwarn-mode' and | |
64 ;; `global-cwarn-mode'. The former can be applied to individual buffers | |
65 ;; and the latter to all buffers. | |
66 ;; | |
67 ;; Activate this package by Customize, or by placing the following line | |
68 ;; into the appropriate init file: | |
69 ;; | |
70 ;; (global-cwarn-mode 1) | |
71 ;; | |
72 ;; Also, `font-lock-mode' or `global-font-lock-mode' must be enabled. | |
73 | |
74 ;; Afterthought: | |
75 ;; | |
76 ;; After using this package for several weeks it feels as though I | |
77 ;; find stupid typo-style bugs while editing rather than at compile- | |
78 ;; or run-time, if I ever find them. | |
79 ;; | |
80 ;; On the other hand, I find myself using assignments inside | |
81 ;; expressions much more often than I used to do. The reason is that | |
82 ;; there is no risk of interpreting an assignment operator as a | |
83 ;; comparison ("hey, the assignment operator is red, duh!"). | |
84 | |
85 ;; Reporting bugs: | |
86 ;; | |
87 ;; Out of the last ten bugs you found, how many did you report? | |
88 ;; | |
89 ;; When reporting a bug, please: | |
90 ;; | |
91 ;; * Send a mail the maintainer of the package, or to the author | |
92 ;; if no maintainer exists. | |
93 ;; * Include the name of the package in the title of the mail, to | |
94 ;; simplify for the recipient. | |
95 ;; * State exactly what you did, what happened, and what you expected | |
96 ;; to see when you found the bug. | |
97 ;; * If the bug cause an error, set the variable `debug-on-error' to t, | |
44447
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
98 ;; repeat the operations that triggered the error and include |
26963 | 99 ;; the backtrace in the letter. |
100 ;; * If possible, include an example that activates the bug. | |
101 ;; * Should you speculate about the cause of the problem, please | |
102 ;; state explicitly that you are guessing. | |
103 | |
104 ;;}}} | |
105 | |
106 ;;; Code: | |
107 | |
108 ;;{{{ Dependencies | |
109 | |
110 (eval-when-compile (require 'cl)) | |
111 | |
112 (require 'custom) | |
113 (require 'font-lock) | |
114 (require 'cc-mode) | |
115 | |
116 ;;}}} | |
117 ;;{{{ Variables | |
118 | |
119 (defgroup cwarn nil | |
120 "Highlight suspicious C and C++ constructions." | |
30811
2ae4e53657a3
(cwarn) <defgroup>: Add :version, :link.
Dave Love <fx@gnu.org>
parents:
26963
diff
changeset
|
121 :version "21.1" |
2ae4e53657a3
(cwarn) <defgroup>: Add :version, :link.
Dave Love <fx@gnu.org>
parents:
26963
diff
changeset
|
122 :link '(url-link "http://www.andersl.com/emacs") |
26963 | 123 :group 'faces) |
124 | |
125 (defvar cwarn-mode nil | |
126 "*Non-nil when Cwarn mode is active. | |
127 | |
128 Never set this variable directly, use the command `cwarn-mode' | |
129 instead.") | |
130 | |
131 (defcustom cwarn-configuration | |
132 '((c-mode (not reference)) | |
133 (c++-mode t)) | |
134 "*List of items each describing which features are enable for a mode. | |
135 Each item is on the form (mode featurelist), where featurelist can be | |
136 on one of three forms: | |
137 | |
138 * A list of enabled features. | |
139 * A list starting with the atom `not' followed by the features | |
140 which are not enabled. | |
141 * The atom t, that represent that all features are enabled. | |
142 | |
143 See variable `cwarn-font-lock-feature-keywords-alist' for available | |
144 features." | |
30811
2ae4e53657a3
(cwarn) <defgroup>: Add :version, :link.
Dave Love <fx@gnu.org>
parents:
26963
diff
changeset
|
145 :type '(repeat sexp) |
26963 | 146 :group 'cwarn) |
147 | |
148 (defcustom cwarn-font-lock-feature-keywords-alist | |
149 '((assign . cwarn-font-lock-assignment-keywords) | |
150 (semicolon . cwarn-font-lock-semicolon-keywords) | |
151 (reference . cwarn-font-lock-reference-keywords)) | |
30811
2ae4e53657a3
(cwarn) <defgroup>: Add :version, :link.
Dave Love <fx@gnu.org>
parents:
26963
diff
changeset
|
152 "An alist mapping a CWarn feature to font-lock keywords. |
26963 | 153 The keywords could either a font-lock keyword list or a symbol. |
154 If it is a symbol it is assumed to be a variable containing a font-lock | |
155 keyword list." | |
30811
2ae4e53657a3
(cwarn) <defgroup>: Add :version, :link.
Dave Love <fx@gnu.org>
parents:
26963
diff
changeset
|
156 :type '(alist :key-type (choice (const assign) |
2ae4e53657a3
(cwarn) <defgroup>: Add :version, :link.
Dave Love <fx@gnu.org>
parents:
26963
diff
changeset
|
157 (const semicolon) |
2ae4e53657a3
(cwarn) <defgroup>: Add :version, :link.
Dave Love <fx@gnu.org>
parents:
26963
diff
changeset
|
158 (const reference)) |
40159
c302267b48cd
(cwarn-font-lock-feature-keywords-alist):
Gerd Moellmann <gerd@gnu.org>
parents:
38436
diff
changeset
|
159 :value-type (sexp :tag "Value")) |
26963 | 160 :group 'cwarn) |
161 | |
162 (defcustom cwarn-verbose t | |
163 "*When nil, CWarn mode will not generate any messages. | |
164 | |
165 Currently, messages are generated when the mode is activated and | |
166 deactivated." | |
167 :group 'cwarn | |
168 :type 'boolean) | |
169 | |
170 (defcustom cwarn-mode-text " CWarn" | |
171 "*String to display in the mode line when CWarn mode is active. | |
172 | |
173 \(When the string is not empty, make sure that it has a leading space.)" | |
174 :tag "CWarn mode text" ; To separate it from `global-...' | |
175 :group 'cwarn | |
176 :type 'string) | |
177 | |
178 (defcustom cwarn-load-hook nil | |
179 "*Functions to run when CWarn mode is first loaded." | |
180 :tag "Load Hook" | |
181 :group 'cwarn | |
182 :type 'hook) | |
183 | |
184 ;;}}} | |
185 ;;{{{ The modes | |
186 | |
187 ;;;###autoload | |
44447
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
188 (define-minor-mode cwarn-mode |
30811
2ae4e53657a3
(cwarn) <defgroup>: Add :version, :link.
Dave Love <fx@gnu.org>
parents:
26963
diff
changeset
|
189 "Minor mode that highlights suspicious C and C++ constructions. |
26963 | 190 |
191 Note, in addition to enabling this minor mode, the major mode must | |
192 be included in the variable `cwarn-configuration'. By default C and | |
193 C++ modes are included. | |
194 | |
195 With ARG, turn CWarn mode on if and only if arg is positive." | |
61283
6535e32999e7
(cwarn-mode): Specify :group.
Lute Kamstra <lute@gnu.org>
parents:
52401
diff
changeset
|
196 :group 'cwarn :lighter cwarn-mode-text |
44447
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
197 (cwarn-font-lock-keywords cwarn-mode) |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
198 (if font-lock-mode (font-lock-fontify-buffer))) |
26963 | 199 |
200 ;;;###autoload | |
201 (defun turn-on-cwarn-mode () | |
202 "Turn on CWarn mode. | |
203 | |
204 This function is designed to be added to hooks, for example: | |
205 (add-hook 'c-mode-hook 'turn-on-cwarn-mode)" | |
206 (cwarn-mode 1)) | |
207 | |
208 ;;}}} | |
209 ;;{{{ Help functions | |
210 | |
211 (defun cwarn-is-enabled (mode &optional feature) | |
212 "Non-nil if CWarn FEATURE is enabled for MODE. | |
213 feature is an atom representing one construction to highlight. | |
214 | |
215 Check if any feature is enabled for MODE if no feature is specified. | |
216 | |
217 The valid features are described by the variable | |
218 `cwarn-font-lock-feature-keywords-alist'." | |
219 (let ((mode-configuraion (assq mode cwarn-configuration))) | |
220 (and mode-configuraion | |
221 (or (null feature) | |
222 (let ((list-or-t (nth 1 mode-configuraion))) | |
223 (or (eq list-or-t t) | |
224 (if (eq (car-safe list-or-t) 'not) | |
225 (not (memq feature (cdr list-or-t))) | |
226 (memq feature list-or-t)))))))) | |
227 | |
228 (defun cwarn-inside-macro () | |
229 "True if point is inside a C macro definition." | |
230 (save-excursion | |
231 (beginning-of-line) | |
232 (while (eq (char-before (1- (point))) ?\\) | |
233 (forward-line -1)) | |
234 (back-to-indentation) | |
235 (eq (char-after) ?#))) | |
236 | |
44447
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
237 (defun cwarn-font-lock-keywords (addp) |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
238 "Install/Remove keywords into current buffer. |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
239 If ADDP is non-nil, install else remove." |
26963 | 240 (dolist (pair cwarn-font-lock-feature-keywords-alist) |
241 (let ((feature (car pair)) | |
242 (keywords (cdr pair))) | |
243 (if (not (listp keywords)) | |
244 (setq keywords (symbol-value keywords))) | |
44447
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
245 (if (cwarn-is-enabled major-mode feature) |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
246 (funcall (if addp 'font-lock-add-keywords 'font-lock-remove-keywords) |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
247 nil keywords))))) |
26963 | 248 |
249 ;;}}} | |
250 ;;{{{ Backward compatibility | |
251 | |
252 ;; This piece of code will be part of CC mode as of Emacs 20.4. | |
253 (if (not (fboundp 'c-at-toplevel-p)) | |
254 (defun c-at-toplevel-p () | |
255 "Return a determination as to whether point is at the `top-level'. | |
256 Being at the top-level means that point is either outside any | |
257 enclosing block (such function definition), or inside a class | |
258 definition, but outside any method blocks. | |
259 | |
260 If point is not at the top-level (e.g. it is inside a method | |
261 definition), then nil is returned. Otherwise, if point is at a | |
262 top-level not enclosed within a class definition, t is returned. | |
263 Otherwise, a 2-vector is returned where the zeroth element is the | |
264 buffer position of the start of the class declaration, and the first | |
44447
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
265 element is the buffer position of the enclosing class' opening |
26963 | 266 brace." |
267 (let ((state (c-parse-state))) | |
268 (or (not (c-most-enclosing-brace state)) | |
269 (c-search-uplist-for-classkey state)))) | |
270 ) | |
271 | |
272 ;;}}} | |
273 ;;{{{ Font-lock keywords and match functions | |
274 | |
275 ;; This section contains font-lock keywords. A font lock keyword can | |
276 ;; either contain a regular expression or a match function. All | |
277 ;; keywords defined here use match functions since the C and C++ | |
278 ;; constructions highlighted by CWarn are too complex to be matched by | |
279 ;; regular expressions. | |
280 ;; | |
281 ;; A match function should act like a normal forward search. They | |
282 ;; should return non-nil if they found a candidate and the match data | |
283 ;; should correspond to the highlight part of the font-lock keyword. | |
44447
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
284 ;; The functions should not generate errors, in that case font-lock |
26963 | 285 ;; will fail to highlight the buffer. A match function takes one |
286 ;; argument, LIMIT, that represent the end of area to be searched. | |
287 ;; | |
288 ;; The variable `cwarn-font-lock-feature-keywords-alist' contains a | |
289 ;; mapping from CWarn features to the font-lock keywords defined | |
290 ;; below. | |
291 | |
44447
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
292 (defmacro cwarn-font-lock-match (re &rest body) |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
293 "Match RE but only if BODY holds." |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
294 `(let ((res nil)) |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
295 (while |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
296 (progn |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
297 (setq res (re-search-forward ,re limit t)) |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
298 (and res |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
299 (save-excursion |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
300 (when (match-beginning 1) (goto-char (match-beginning 1))) |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
301 (condition-case nil ; In case something barfs. |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
302 (not (save-match-data |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
303 ,@body)) |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
304 (error t)))))) |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
305 res)) |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
306 |
26963 | 307 ;;{{{ Assignment in expressions |
308 | |
309 (defconst cwarn-font-lock-assignment-keywords | |
310 '((cwarn-font-lock-match-assignment-in-expression | |
311 (1 font-lock-warning-face)))) | |
312 | |
313 (defun cwarn-font-lock-match-assignment-in-expression (limit) | |
314 "Match assignments inside expressions." | |
44447
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
315 (cwarn-font-lock-match |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
316 "[^!<>=]\\(\\([-+*/%&^|]\\|<<\\|>>\\)?=\\)[^=]" |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
317 (backward-up-list 1) |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
318 (and (memq (following-char) '(?\( ?\[)) |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
319 (not (progn |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
320 (skip-chars-backward " ") |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
321 (skip-chars-backward "a-zA-Z0-9_") |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
322 (or |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
323 ;; Default parameter of function. |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
324 (c-at-toplevel-p) |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
325 (looking-at "for\\>"))))))) |
26963 | 326 |
327 ;;}}} | |
328 ;;{{{ Semicolon | |
329 | |
330 (defconst cwarn-font-lock-semicolon-keywords | |
331 '((cwarn-font-lock-match-dangerous-semicolon (0 font-lock-warning-face)))) | |
332 | |
333 (defun cwarn-font-lock-match-dangerous-semicolon (limit) | |
334 "Match semicolons directly after `for', `while', and `if'. | |
44447
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
335 The semicolon after a `do { ... } while (x);' construction is not matched." |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
336 (cwarn-font-lock-match |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
337 ";" |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
338 (backward-sexp 2) ; Expression and keyword. |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
339 (or (looking-at "\\(for\\|if\\)\\>") |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
340 (and (looking-at "while\\>") |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
341 (condition-case nil |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
342 (progn |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
343 (backward-sexp 2) ; Body and "do". |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
344 (not (looking-at "do\\>"))) |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
345 (error t)))))) |
26963 | 346 |
347 ;;}}} | |
348 ;;{{{ Reference | |
349 | |
350 (defconst cwarn-font-lock-reference-keywords | |
351 '((cwarn-font-lock-match-reference (1 font-lock-warning-face)))) | |
352 | |
353 (defun cwarn-font-lock-match-reference (limit) | |
354 "Font-lock matcher for C++ reference parameters." | |
44447
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
355 (cwarn-font-lock-match |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
356 "[^&]\\(&\\)[^&=]" |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
357 (backward-up-list 1) |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
358 (and (eq (following-char) ?\() |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
359 (not (cwarn-inside-macro)) |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
360 (c-at-toplevel-p)))) |
26963 | 361 |
362 ;;}}} | |
363 | |
364 ;;}}} | |
365 ;;{{{ The end | |
366 | |
44447
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
367 (defun turn-on-cwarn-mode-if-enabled () |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
368 "Turn on CWarn mode in the current buffer if applicable. |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
369 The mode is turned if some feature is enabled for the current |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
370 `major-mode' in `cwarn-configuration'." |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
371 (if (cwarn-is-enabled major-mode) (turn-on-cwarn-mode))) |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
372 |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
373 ;;;###autoload |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
374 (easy-mmode-define-global-mode global-cwarn-mode cwarn-mode |
811bba65a69f
(global-cwarn-mode): Use define-minor-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41827
diff
changeset
|
375 turn-on-cwarn-mode-if-enabled) |
26963 | 376 |
377 (provide 'cwarn) | |
378 | |
379 (run-hooks 'cwarn-load-hook) | |
380 | |
381 ;;}}} | |
382 | |
52401 | 383 ;;; arch-tag: 225fb5e2-0838-4eb1-88ce-3811c5e4d738 |
26963 | 384 ;;; cwarn.el ends here |