Commit | Line | Data |
---|---|---|
d9e94c22 MS |
1 | ;;; cc-fonts.el --- font lock support for CC Mode |
2 | ||
73b0cd50 | 3 | ;; Copyright (C) 2002-2011 Free Software Foundation, Inc. |
d9e94c22 MS |
4 | |
5 | ;; Authors: 2003- Alan Mackenzie | |
6 | ;; 2002- Martin Stjernholm | |
7 | ;; Maintainer: bug-cc-mode@gnu.org | |
8 | ;; Created: 07-Jan-2002 | |
bd78fa1d CY |
9 | ;; Keywords: c languages |
10 | ;; Package: cc-mode | |
d9e94c22 MS |
11 | |
12 | ;; This file is part of GNU Emacs. | |
13 | ||
b1fc2b50 | 14 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
d9e94c22 | 15 | ;; it under the terms of the GNU General Public License as published by |
b1fc2b50 GM |
16 | ;; the Free Software Foundation, either version 3 of the License, or |
17 | ;; (at your option) any later version. | |
d9e94c22 MS |
18 | |
19 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
20 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | ;; GNU General Public License for more details. | |
23 | ||
24 | ;; You should have received a copy of the GNU General Public License | |
b1fc2b50 | 25 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
d9e94c22 MS |
26 | |
27 | ;;; Commentary: | |
28 | ||
29 | ;; Some comments on the use of faces: | |
30 | ;; | |
0386b551 AM |
31 | ;; o `c-label-face-name' is either `font-lock-constant-face' (in |
32 | ;; Emacs), or `font-lock-reference-face'. | |
d9e94c22 MS |
33 | ;; |
34 | ;; o `c-constant-face-name', `c-reference-face-name' and | |
35 | ;; `c-doc-markup-face-name' are essentially set up like | |
36 | ;; `c-label-face-name'. | |
37 | ;; | |
38 | ;; o `c-preprocessor-face-name' is `font-lock-preprocessor-face' in | |
39 | ;; XEmacs and - in lack of a closer equivalent - | |
40 | ;; `font-lock-builtin-face' or `font-lock-reference-face' in Emacs. | |
41 | ;; | |
42 | ;; o `c-doc-face-name' is `font-lock-doc-string-face' in XEmacs, | |
43 | ;; `font-lock-doc-face' in Emacs 21 and later, or | |
44 | ;; `font-lock-comment-face' in older Emacs (that since source | |
45 | ;; documentation are actually comments in these languages, as opposed | |
46 | ;; to elisp). | |
47 | ;; | |
d9e94c22 MS |
48 | ;; TBD: We should probably provide real faces for the above uses and |
49 | ;; instead initialize them from the standard faces. | |
50 | ||
51 | ;;; Code: | |
52 | ||
53 | ;; The faces that already have been put onto the text is tested in | |
54 | ;; various places to direct further fontifications. For this to work, | |
55 | ;; the following assumptions regarding the faces must hold (apart from | |
56 | ;; the dependencies on the font locking order): | |
57 | ;; | |
58 | ;; o `font-lock-comment-face' and the face in `c-doc-face-name' is | |
59 | ;; not used in anything but comments. | |
60 | ;; o If any face (e.g. `c-doc-markup-face-name') but those above is | |
61 | ;; used in comments, it doesn't replace them. | |
62 | ;; o `font-lock-string-face' is not used in anything but string | |
63 | ;; literals (single or double quoted). | |
64 | ;; o `font-lock-keyword-face' and the face in `c-label-face-name' are | |
65 | ;; never overlaid with other faces. | |
66 | ||
67 | (eval-when-compile | |
68 | (let ((load-path | |
69 | (if (and (boundp 'byte-compile-dest-file) | |
70 | (stringp byte-compile-dest-file)) | |
71 | (cons (file-name-directory byte-compile-dest-file) load-path) | |
72 | load-path))) | |
73 | (load "cc-bytecomp" nil t))) | |
74 | ||
75 | (cc-require 'cc-defs) | |
76 | (cc-require-when-compile 'cc-langs) | |
77 | (cc-require 'cc-vars) | |
78 | (cc-require 'cc-engine) | |
79 | (cc-require-when-compile 'cc-awk) ; Change from cc-require, 2003/6/18 to | |
80 | ;; prevent cc-awk being loaded when it's not needed. There is now a (require | |
81 | ;; 'cc-awk) in (defun awk-mode ..). | |
82 | ||
83 | ;; Avoid repeated loading through the eval-after-load directive in | |
84 | ;; cc-mode.el. | |
85 | (provide 'cc-fonts) | |
86 | ||
87 | (cc-external-require 'font-lock) | |
88 | ||
89 | (cc-bytecomp-defvar parse-sexp-lookup-properties) ; Emacs only. | |
90 | ||
91 | ;; Need to declare these local symbols during compilation since | |
92 | ;; they're referenced from lambdas in `byte-compile' calls that are | |
93 | ;; executed at compile time. They don't need to have the proper | |
94 | ;; definitions, though, since the generated functions aren't called | |
95 | ;; during compilation. | |
96 | (cc-bytecomp-defvar c-preprocessor-face-name) | |
97 | (cc-bytecomp-defvar c-reference-face-name) | |
98 | (cc-bytecomp-defun c-fontify-recorded-types-and-refs) | |
99 | (cc-bytecomp-defun c-font-lock-declarators) | |
d9e94c22 MS |
100 | (cc-bytecomp-defun c-font-lock-objc-method) |
101 | (cc-bytecomp-defun c-font-lock-invalid-string) | |
102 | ||
d9e94c22 MS |
103 | \f |
104 | ;; Note that font-lock in XEmacs doesn't expand face names as | |
105 | ;; variables, so we have to use the (eval . FORM) in the font lock | |
106 | ;; matchers wherever we use these alias variables. | |
107 | ||
108 | (defconst c-preprocessor-face-name | |
109 | (cond ((c-face-name-p 'font-lock-preprocessor-face) | |
110 | ;; XEmacs has a font-lock-preprocessor-face. | |
111 | 'font-lock-preprocessor-face) | |
112 | ((c-face-name-p 'font-lock-builtin-face) | |
0386b551 AM |
113 | ;; In Emacs font-lock-builtin-face has traditionally been |
114 | ;; used for preprocessor directives. | |
d9e94c22 MS |
115 | 'font-lock-builtin-face) |
116 | (t | |
117 | 'font-lock-reference-face))) | |
118 | ||
119 | (cc-bytecomp-defvar font-lock-constant-face) | |
120 | ||
121 | (defconst c-label-face-name | |
122 | (cond ((c-face-name-p 'font-lock-label-face) | |
123 | ;; If it happens to occur in the future. (Well, the more | |
124 | ;; pragmatic reason is to get unique faces for the test | |
125 | ;; suite.) | |
126 | 'font-lock-label-face) | |
127 | ((and (c-face-name-p 'font-lock-constant-face) | |
128 | (eq font-lock-constant-face 'font-lock-constant-face)) | |
129 | ;; Test both if font-lock-constant-face exists and that it's | |
130 | ;; not an alias for something else. This is important since | |
131 | ;; we compare already set faces in various places. | |
132 | 'font-lock-constant-face) | |
133 | (t | |
134 | 'font-lock-reference-face))) | |
135 | ||
136 | (defconst c-constant-face-name | |
137 | (if (and (c-face-name-p 'font-lock-constant-face) | |
138 | (eq font-lock-constant-face 'font-lock-constant-face)) | |
0386b551 | 139 | ;; This doesn't exist in some earlier versions of XEmacs 21. |
d9e94c22 MS |
140 | 'font-lock-constant-face |
141 | c-label-face-name)) | |
142 | ||
143 | (defconst c-reference-face-name | |
0386b551 AM |
144 | (with-no-warnings |
145 | (if (and (c-face-name-p 'font-lock-reference-face) | |
146 | (eq font-lock-reference-face 'font-lock-reference-face)) | |
147 | ;; This is considered obsolete in Emacs, but it still maps well | |
148 | ;; to this use. (Another reason to do this is to get unique | |
149 | ;; faces for the test suite.) | |
150 | 'font-lock-reference-face | |
151 | c-label-face-name))) | |
d9e94c22 MS |
152 | |
153 | ;; This should not mapped to a face that also is used to fontify things | |
154 | ;; that aren't comments or string literals. | |
155 | (defconst c-doc-face-name | |
156 | (cond ((c-face-name-p 'font-lock-doc-string-face) | |
157 | ;; XEmacs. | |
158 | 'font-lock-doc-string-face) | |
159 | ((c-face-name-p 'font-lock-doc-face) | |
160 | ;; Emacs 21 and later. | |
161 | 'font-lock-doc-face) | |
162 | (t | |
163 | 'font-lock-comment-face))) | |
164 | ||
165 | (defconst c-doc-markup-face-name | |
166 | (if (c-face-name-p 'font-lock-doc-markup-face) | |
167 | ;; If it happens to occur in the future. (Well, the more | |
168 | ;; pragmatic reason is to get unique faces for the test | |
169 | ;; suite.) | |
170 | 'font-lock-doc-markup-face | |
171 | c-label-face-name)) | |
172 | ||
0386b551 AM |
173 | (defconst c-negation-char-face-name |
174 | (if (c-face-name-p 'font-lock-negation-char-face) | |
175 | ;; Emacs 22 has a special face for negation chars. | |
176 | 'font-lock-negation-char-face)) | |
d9e94c22 MS |
177 | |
178 | (cc-bytecomp-defun face-inverse-video-p) ; Only in Emacs. | |
179 | (cc-bytecomp-defun face-property-instance) ; Only in XEmacs. | |
180 | ||
181 | (defun c-make-inverse-face (oldface newface) | |
182 | ;; Emacs and XEmacs have completely different face manipulation | |
183 | ;; routines. :P | |
d9e94c22 MS |
184 | (copy-face oldface newface) |
185 | (cond ((fboundp 'face-inverse-video-p) | |
0386b551 AM |
186 | ;; Emacs. This only looks at the inverse flag in the current |
187 | ;; frame. Other display configurations might be different, | |
188 | ;; but it can only show if the same Emacs has frames on | |
189 | ;; e.g. a color and a monochrome display simultaneously. | |
d9e94c22 MS |
190 | (unless (face-inverse-video-p oldface) |
191 | (invert-face newface))) | |
192 | ((fboundp 'face-property-instance) | |
193 | ;; XEmacs. Same pitfall here. | |
194 | (unless (face-property-instance oldface 'reverse) | |
0386b551 | 195 | (invert-face newface))))) |
d9e94c22 | 196 | |
452ea855 AM |
197 | (defvar c-annotation-face (make-face 'c-annotation-face) |
198 | "Face used to highlight annotations in java-mode and other modes that may wish to use it.") | |
199 | (set-face-foreground 'c-annotation-face "blue") | |
200 | ||
d9e94c22 | 201 | (eval-and-compile |
b128268e AM |
202 | ;; We need the following definitions during compilation since they're |
203 | ;; used when the `c-lang-defconst' initializers are evaluated. Define | |
204 | ;; them at runtime too for the sake of derived modes. | |
205 | ||
206 | ;; This indicates the "font locking context", and is set just before | |
207 | ;; fontification is done. If non-nil, it says, e.g., point starts | |
208 | ;; from within a #if preprocessor construct. | |
209 | (defvar c-font-lock-context nil) | |
210 | (make-variable-buffer-local 'c-font-lock-context) | |
7d0ee75c | 211 | |
d9e94c22 MS |
212 | (defmacro c-put-font-lock-face (from to face) |
213 | ;; Put a face on a region (overriding any existing face) in the way | |
214 | ;; font-lock would do it. In XEmacs that means putting an | |
215 | ;; additional font-lock property, or else the font-lock package | |
216 | ;; won't recognize it as fontified and might override it | |
217 | ;; incorrectly. | |
0386b551 AM |
218 | ;; |
219 | ;; This function does a hidden buffer change. | |
d9e94c22 MS |
220 | (if (fboundp 'font-lock-set-face) |
221 | ;; Note: This function has no docstring in XEmacs so it might be | |
222 | ;; considered internal. | |
223 | `(font-lock-set-face ,from ,to ,face) | |
224 | `(put-text-property ,from ,to 'face ,face))) | |
225 | ||
226 | (defmacro c-remove-font-lock-face (from to) | |
227 | ;; This is the inverse of `c-put-font-lock-face'. | |
0386b551 AM |
228 | ;; |
229 | ;; This function does a hidden buffer change. | |
d9e94c22 MS |
230 | (if (fboundp 'font-lock-remove-face) |
231 | `(font-lock-remove-face ,from ,to) | |
232 | `(remove-text-properties ,from ,to '(face nil)))) | |
233 | ||
234 | (defmacro c-put-font-lock-string-face (from to) | |
235 | ;; Put `font-lock-string-face' on a string. The surrounding | |
236 | ;; quotes are included in Emacs but not in XEmacs. The passed | |
237 | ;; region should include them. | |
0386b551 AM |
238 | ;; |
239 | ;; This function does a hidden buffer change. | |
d9e94c22 MS |
240 | (if (featurep 'xemacs) |
241 | `(c-put-font-lock-face (1+ ,from) (1- ,to) 'font-lock-string-face) | |
242 | `(c-put-font-lock-face ,from ,to 'font-lock-string-face))) | |
243 | ||
244 | (defmacro c-fontify-types-and-refs (varlist &rest body) | |
245 | ;; Like `let', but additionally activates `c-record-type-identifiers' | |
246 | ;; and `c-record-ref-identifiers', and fontifies the recorded ranges | |
247 | ;; accordingly on exit. | |
0386b551 AM |
248 | ;; |
249 | ;; This function does hidden buffer changes. | |
d9e94c22 MS |
250 | `(let ((c-record-type-identifiers t) |
251 | c-record-ref-identifiers | |
252 | ,@varlist) | |
253 | (prog1 (progn ,@body) | |
254 | (c-fontify-recorded-types-and-refs)))) | |
255 | (put 'c-fontify-types-and-refs 'lisp-indent-function 1) | |
d9e94c22 MS |
256 | |
257 | (defun c-skip-comments-and-strings (limit) | |
258 | ;; If the point is within a region fontified as a comment or | |
259 | ;; string literal skip to the end of it or to LIMIT, whichever | |
260 | ;; comes first, and return t. Otherwise return nil. The match | |
261 | ;; data is not clobbered. | |
0386b551 AM |
262 | ;; |
263 | ;; This function might do hidden buffer changes. | |
d9e94c22 MS |
264 | (when (c-got-face-at (point) c-literal-faces) |
265 | (while (progn | |
266 | (goto-char (next-single-property-change | |
267 | (point) 'face nil limit)) | |
268 | (and (< (point) limit) | |
269 | (c-got-face-at (point) c-literal-faces)))) | |
270 | t)) | |
271 | ||
0386b551 AM |
272 | (defun c-make-syntactic-matcher (regexp) |
273 | ;; Returns a byte compiled function suitable for use in place of a | |
274 | ;; regexp string in a `font-lock-keywords' matcher, except that | |
275 | ;; only matches outside comments and string literals count. | |
276 | ;; | |
277 | ;; This function does not do any hidden buffer changes, but the | |
278 | ;; generated functions will. (They are however used in places | |
279 | ;; covered by the font-lock context.) | |
280 | (byte-compile | |
281 | `(lambda (limit) | |
282 | (let (res) | |
283 | (while (and (setq res (re-search-forward ,regexp limit t)) | |
284 | (progn | |
285 | (goto-char (match-beginning 0)) | |
286 | (or (c-skip-comments-and-strings limit) | |
287 | (progn | |
288 | (goto-char (match-end 0)) | |
289 | nil))))) | |
290 | res)))) | |
291 | ||
b128268e | 292 | (defun c-make-font-lock-search-form (regexp highlights) |
8d5ed899 | 293 | ;; Return a lisp form which will fontify every occurrence of REGEXP |
b128268e AM |
294 | ;; (a regular expression, NOT a function) between POINT and `limit' |
295 | ;; with HIGHLIGHTS, a list of highlighters as specified on page | |
296 | ;; "Search-based Fontification" in the elisp manual. | |
297 | `(while (re-search-forward ,regexp limit t) | |
298 | (unless (progn | |
299 | (goto-char (match-beginning 0)) | |
300 | (c-skip-comments-and-strings limit)) | |
301 | (goto-char (match-end 0)) | |
302 | ,@(mapcar | |
303 | (lambda (highlight) | |
304 | (if (integerp (car highlight)) | |
305 | ;; e.g. highlight is (1 font-lock-type-face t) | |
306 | (progn | |
307 | (unless (eq (nth 2 highlight) t) | |
308 | (error | |
309 | "The override flag must currently be t in %s" | |
310 | highlight)) | |
311 | (when (nth 3 highlight) | |
312 | (error | |
313 | "The laxmatch flag may currently not be set in %s" | |
314 | highlight)) | |
315 | `(save-match-data | |
316 | (c-put-font-lock-face | |
317 | (match-beginning ,(car highlight)) | |
318 | (match-end ,(car highlight)) | |
319 | ,(elt highlight 1)))) | |
f6b1b0a8 | 320 | ;; highlight is an "ANCHORED HIGHLIGHTER" of the form |
b128268e AM |
321 | ;; (ANCHORED-MATCHER PRE-FORM POST-FORM SUBEXP-HIGHLIGHTERS...) |
322 | (when (nth 3 highlight) | |
323 | (error "Match highlights currently not supported in %s" | |
324 | highlight)) | |
325 | `(progn | |
326 | ,(nth 1 highlight) | |
327 | (save-match-data ,(car highlight)) | |
328 | ,(nth 2 highlight)))) | |
329 | highlights)))) | |
330 | ||
d9e94c22 MS |
331 | (defun c-make-font-lock-search-function (regexp &rest highlights) |
332 | ;; This function makes a byte compiled function that works much like | |
333 | ;; a matcher element in `font-lock-keywords'. It cuts out a little | |
334 | ;; bit of the overhead compared to a real matcher. The main reason | |
335 | ;; is however to pass the real search limit to the anchored | |
336 | ;; matcher(s), since most (if not all) font-lock implementations | |
e15f8aaa | 337 | ;; arbitrarily limit anchored matchers to the same line, and also |
d9e94c22 MS |
338 | ;; to insulate against various other irritating differences between |
339 | ;; the different (X)Emacs font-lock packages. | |
340 | ;; | |
341 | ;; REGEXP is the matcher, which must be a regexp. Only matches | |
342 | ;; where the beginning is outside any comment or string literal are | |
343 | ;; significant. | |
344 | ;; | |
345 | ;; HIGHLIGHTS is a list of highlight specs, just like in | |
346 | ;; `font-lock-keywords', with these limitations: The face is always | |
347 | ;; overridden (no big disadvantage, since hits in comments etc are | |
348 | ;; filtered anyway), there is no "laxmatch", and an anchored matcher | |
349 | ;; is always a form which must do all the fontification directly. | |
350 | ;; `limit' is a variable bound to the real limit in the context of | |
351 | ;; the anchored matcher forms. | |
352 | ;; | |
353 | ;; This function does not do any hidden buffer changes, but the | |
0386b551 AM |
354 | ;; generated functions will. (They are however used in places |
355 | ;; covered by the font-lock context.) | |
d9e94c22 MS |
356 | |
357 | ;; Note: Replace `byte-compile' with `eval' to debug the generated | |
e15f8aaa | 358 | ;; lambda more easily. |
d9e94c22 MS |
359 | (byte-compile |
360 | `(lambda (limit) | |
b128268e | 361 | (let ( ;; The font-lock package in Emacs is known to clobber |
d9e94c22 MS |
362 | ;; `parse-sexp-lookup-properties' (when it exists). |
363 | (parse-sexp-lookup-properties | |
364 | (cc-eval-when-compile | |
365 | (boundp 'parse-sexp-lookup-properties)))) | |
b128268e AM |
366 | |
367 | ;; (while (re-search-forward ,regexp limit t) | |
368 | ;; (unless (progn | |
369 | ;; (goto-char (match-beginning 0)) | |
370 | ;; (c-skip-comments-and-strings limit)) | |
371 | ;; (goto-char (match-end 0)) | |
372 | ;; ,@(mapcar | |
373 | ;; (lambda (highlight) | |
374 | ;; (if (integerp (car highlight)) | |
375 | ;; (progn | |
376 | ;; (unless (eq (nth 2 highlight) t) | |
377 | ;; (error | |
378 | ;; "The override flag must currently be t in %s" | |
379 | ;; highlight)) | |
380 | ;; (when (nth 3 highlight) | |
381 | ;; (error | |
382 | ;; "The laxmatch flag may currently not be set in %s" | |
383 | ;; highlight)) | |
384 | ;; `(save-match-data | |
385 | ;; (c-put-font-lock-face | |
386 | ;; (match-beginning ,(car highlight)) | |
387 | ;; (match-end ,(car highlight)) | |
388 | ;; ,(elt highlight 1)))) | |
389 | ;; (when (nth 3 highlight) | |
390 | ;; (error "Match highlights currently not supported in %s" | |
391 | ;; highlight)) | |
392 | ;; `(progn | |
393 | ;; ,(nth 1 highlight) | |
394 | ;; (save-match-data ,(car highlight)) | |
395 | ;; ,(nth 2 highlight)))) | |
396 | ;; highlights))) | |
397 | ,(c-make-font-lock-search-form regexp highlights)) | |
398 | ||
0386b551 AM |
399 | nil))) |
400 | ||
ef8cdf8c AM |
401 | (defun c-make-font-lock-BO-decl-search-function (regexp &rest highlights) |
402 | ;; This function makes a byte compiled function that first moves back | |
403 | ;; to the beginning of the current declaration (if any), then searches | |
404 | ;; forward for matcher elements (as in `font-lock-keywords') and | |
405 | ;; fontifies them. | |
406 | ;; | |
407 | ;; The motivation for moving back to the declaration start is to | |
408 | ;; establish a context for the current text when, e.g., a character | |
409 | ;; is typed on a C++ inheritance continuation line, or a jit-lock | |
410 | ;; chunk starts there. | |
7d0ee75c | 411 | ;; |
ef8cdf8c AM |
412 | ;; The new function works much like a matcher element in |
413 | ;; `font-lock-keywords'. It cuts out a little bit of the overhead | |
414 | ;; compared to a real matcher. The main reason is however to pass the | |
415 | ;; real search limit to the anchored matcher(s), since most (if not | |
416 | ;; all) font-lock implementations arbitrarily limit anchored matchers | |
417 | ;; to the same line, and also to insulate against various other | |
418 | ;; irritating differences between the different (X)Emacs font-lock | |
419 | ;; packages. | |
420 | ;; | |
421 | ;; REGEXP is the matcher, which must be a regexp. Only matches | |
422 | ;; where the beginning is outside any comment or string literal are | |
423 | ;; significant. | |
424 | ;; | |
425 | ;; HIGHLIGHTS is a list of highlight specs, just like in | |
426 | ;; `font-lock-keywords', with these limitations: The face is always | |
427 | ;; overridden (no big disadvantage, since hits in comments etc are | |
428 | ;; filtered anyway), there is no "laxmatch", and an anchored matcher | |
429 | ;; is always a form which must do all the fontification directly. | |
430 | ;; `limit' is a variable bound to the real limit in the context of | |
431 | ;; the anchored matcher forms. | |
432 | ;; | |
433 | ;; This function does not do any hidden buffer changes, but the | |
434 | ;; generated functions will. (They are however used in places | |
435 | ;; covered by the font-lock context.) | |
436 | ||
437 | ;; Note: Replace `byte-compile' with `eval' to debug the generated | |
438 | ;; lambda more easily. | |
439 | (byte-compile | |
440 | `(lambda (limit) | |
441 | (let ( ;; The font-lock package in Emacs is known to clobber | |
442 | ;; `parse-sexp-lookup-properties' (when it exists). | |
443 | (parse-sexp-lookup-properties | |
444 | (cc-eval-when-compile | |
445 | (boundp 'parse-sexp-lookup-properties)))) | |
446 | (goto-char | |
447 | (let ((here (point))) | |
448 | (if (eq (car (c-beginning-of-decl-1)) 'same) | |
449 | (point) | |
450 | here))) | |
451 | ,(c-make-font-lock-search-form regexp highlights)) | |
452 | nil))) | |
453 | ||
b128268e AM |
454 | (defun c-make-font-lock-context-search-function (normal &rest state-stanzas) |
455 | ;; This function makes a byte compiled function that works much like | |
456 | ;; a matcher element in `font-lock-keywords', with the following | |
457 | ;; enhancement: the generated function will test for particular "font | |
458 | ;; lock contexts" at the start of the region, i.e. is this point in | |
459 | ;; the middle of some particular construct? if so the generated | |
460 | ;; function will first fontify the tail of the construct, before | |
461 | ;; going into the main loop and fontify full constructs up to limit. | |
462 | ;; | |
463 | ;; The generated function takes one parameter called `limit', and | |
464 | ;; will fontify the region between POINT and LIMIT. | |
465 | ;; | |
466 | ;; NORMAL is a list of the form (REGEXP HIGHLIGHTS .....), and is | |
467 | ;; used to fontify the "regular" bit of the region. | |
468 | ;; STATE-STANZAS is list of elements of the form (STATE LIM REGEXP | |
469 | ;; HIGHLIGHTS), each element coding one possible font lock context. | |
470 | ||
471 | ;; o - REGEXP is a font-lock regular expression (NOT a function), | |
472 | ;; o - HIGHLIGHTS is a list of zero or more highlighters as defined | |
473 | ;; on page "Search-based Fontification" in the elisp manual. As | |
474 | ;; yet (2009-06), they must have OVERRIDE set, and may not have | |
475 | ;; LAXMATCH set. | |
476 | ;; | |
477 | ;; o - STATE is the "font lock context" (e.g. in-cpp-expr) and is | |
478 | ;; not quoted. | |
479 | ;; o - LIM is a lisp form whose evaluation will yield the limit | |
480 | ;; position in the buffer for fontification by this stanza. | |
481 | ;; | |
482 | ;; This function does not do any hidden buffer changes, but the | |
483 | ;; generated functions will. (They are however used in places | |
484 | ;; covered by the font-lock context.) | |
7d0ee75c | 485 | ;; |
b128268e AM |
486 | ;; Note: Replace `byte-compile' with `eval' to debug the generated |
487 | ;; lambda more easily. | |
488 | (byte-compile | |
489 | `(lambda (limit) | |
490 | (let ( ;; The font-lock package in Emacs is known to clobber | |
491 | ;; `parse-sexp-lookup-properties' (when it exists). | |
492 | (parse-sexp-lookup-properties | |
493 | (cc-eval-when-compile | |
494 | (boundp 'parse-sexp-lookup-properties)))) | |
495 | ,@(mapcar | |
496 | (lambda (stanza) | |
497 | (let ((state (car stanza)) | |
498 | (lim (nth 1 stanza)) | |
499 | (regexp (nth 2 stanza)) | |
500 | (highlights (cdr (cddr stanza)))) | |
501 | `(if (eq c-font-lock-context ',state) | |
502 | (let ((limit ,lim)) | |
503 | ,(c-make-font-lock-search-form | |
504 | regexp highlights))))) | |
505 | state-stanzas) | |
506 | ,(c-make-font-lock-search-form (car normal) (cdr normal)) | |
507 | nil)))) | |
508 | ||
3c0ab532 AM |
509 | ; (eval-after-load "edebug" ; 2006-07-09: def-edebug-spec is now in subr.el. |
510 | ; '(progn | |
511 | (def-edebug-spec c-fontify-types-and-refs let*) | |
512 | (def-edebug-spec c-make-syntactic-matcher t) | |
513 | ;; If there are literal quoted or backquoted highlight specs in | |
514 | ;; the call to `c-make-font-lock-search-function' then let's | |
515 | ;; instrument the forms in them. | |
516 | (def-edebug-spec c-make-font-lock-search-function | |
517 | (form &rest &or ("quote" (&rest form)) ("`" (&rest form)) form)));)) | |
d9e94c22 MS |
518 | |
519 | (defun c-fontify-recorded-types-and-refs () | |
0386b551 | 520 | ;; Convert the ranges recorded on `c-record-type-identifiers' and |
d9e94c22 | 521 | ;; `c-record-ref-identifiers' to fontification. |
0386b551 AM |
522 | ;; |
523 | ;; This function does hidden buffer changes. | |
d9e94c22 MS |
524 | (let (elem) |
525 | (while (consp c-record-type-identifiers) | |
526 | (setq elem (car c-record-type-identifiers) | |
527 | c-record-type-identifiers (cdr c-record-type-identifiers)) | |
528 | (c-put-font-lock-face (car elem) (cdr elem) | |
529 | 'font-lock-type-face)) | |
530 | (while c-record-ref-identifiers | |
531 | (setq elem (car c-record-ref-identifiers) | |
532 | c-record-ref-identifiers (cdr c-record-ref-identifiers)) | |
533 | ;; Note that the reference face is a variable that is | |
534 | ;; dereferenced, since it's an alias in Emacs. | |
535 | (c-put-font-lock-face (car elem) (cdr elem) | |
536 | c-reference-face-name)))) | |
537 | ||
538 | (c-lang-defconst c-cpp-matchers | |
539 | "Font lock matchers for preprocessor directives and purely lexical | |
540 | stuff. Used on level 1 and higher." | |
541 | ||
542 | ;; Note: `c-font-lock-declarations' assumes that no matcher here | |
543 | ;; sets `font-lock-type-face' in languages where | |
544 | ;; `c-recognize-<>-arglists' is set. | |
545 | ||
546 | t `(,@(when (c-lang-const c-opt-cpp-prefix) | |
547 | (let* ((noncontinued-line-end "\\(\\=\\|\\(\\=\\|[^\\]\\)[\n\r]\\)") | |
0386b551 AM |
548 | (ncle-depth (regexp-opt-depth noncontinued-line-end)) |
549 | (sws-depth (c-lang-const c-syntactic-ws-depth)) | |
550 | (nsws-depth (c-lang-const c-nonempty-syntactic-ws-depth))) | |
551 | ||
d9e94c22 MS |
552 | `(;; The stuff after #error and #warning is a message, so |
553 | ;; fontify it as a string. | |
0386b551 | 554 | ,@(when (c-lang-const c-cpp-message-directives) |
2ae6c6b5 | 555 | (let* ((re (c-make-keywords-re 'appendable ; nil |
0386b551 AM |
556 | (c-lang-const c-cpp-message-directives))) |
557 | (re-depth (regexp-opt-depth re))) | |
558 | `((,(concat noncontinued-line-end | |
559 | (c-lang-const c-opt-cpp-prefix) | |
560 | re | |
561 | "\\s +\\(.*\\)$") | |
2ae6c6b5 | 562 | ,(+ ncle-depth re-depth 1) font-lock-string-face t)))) |
d9e94c22 MS |
563 | |
564 | ;; Fontify filenames in #include <...> as strings. | |
0386b551 AM |
565 | ,@(when (c-lang-const c-cpp-include-directives) |
566 | (let* ((re (c-make-keywords-re nil | |
567 | (c-lang-const c-cpp-include-directives))) | |
568 | (re-depth (regexp-opt-depth re))) | |
569 | `((,(concat noncontinued-line-end | |
570 | (c-lang-const c-opt-cpp-prefix) | |
571 | re | |
572 | (c-lang-const c-syntactic-ws) | |
573 | "\\(<[^>\n\r]*>?\\)") | |
574 | (,(+ ncle-depth re-depth sws-depth 1) | |
575 | font-lock-string-face) | |
576 | ||
577 | ;; Use an anchored matcher to put paren syntax | |
578 | ;; on the brackets. | |
579 | (,(byte-compile | |
580 | `(lambda (limit) | |
581 | (let ((beg (match-beginning | |
582 | ,(+ ncle-depth re-depth sws-depth 1))) | |
583 | (end (1- (match-end ,(+ ncle-depth re-depth | |
584 | sws-depth 1))))) | |
585 | (if (eq (char-after end) ?>) | |
586 | (progn | |
587 | (c-mark-<-as-paren beg) | |
588 | (c-mark->-as-paren end)) | |
0ec1d2c5 AM |
589 | ;; (c-clear-char-property beg 'syntax-table) |
590 | (c-clear-char-property beg 'category))) | |
0386b551 | 591 | nil))))))) |
d9e94c22 MS |
592 | |
593 | ;; #define. | |
0386b551 AM |
594 | ,@(when (c-lang-const c-opt-cpp-macro-define) |
595 | `((,(c-make-font-lock-search-function | |
596 | (concat | |
597 | noncontinued-line-end | |
598 | (c-lang-const c-opt-cpp-prefix) | |
599 | (c-lang-const c-opt-cpp-macro-define) | |
600 | (c-lang-const c-nonempty-syntactic-ws) | |
601 | "\\(" (c-lang-const ; 1 + ncle + nsws | |
602 | c-symbol-key) "\\)" | |
603 | (concat "\\(" ; 2 + ncle + nsws + c-sym-key | |
604 | ;; Macro with arguments - a "function". | |
605 | "\\(\(\\)" ; 3 + ncle + nsws + c-sym-key | |
606 | "\\|" | |
607 | ;; Macro without arguments - a "variable". | |
608 | "\\([^\(]\\|$\\)" | |
609 | "\\)")) | |
610 | `((if (match-beginning | |
611 | ,(+ 3 ncle-depth nsws-depth | |
612 | (c-lang-const c-symbol-key-depth))) | |
613 | ||
614 | ;; "Function". Fontify the name and the arguments. | |
615 | (save-restriction | |
616 | (c-put-font-lock-face | |
617 | (match-beginning ,(+ 1 ncle-depth nsws-depth)) | |
618 | (match-end ,(+ 1 ncle-depth nsws-depth)) | |
619 | 'font-lock-function-name-face) | |
620 | (goto-char | |
621 | (match-end | |
622 | ,(+ 3 ncle-depth nsws-depth | |
623 | (c-lang-const c-symbol-key-depth)))) | |
624 | ||
625 | (narrow-to-region (point-min) limit) | |
626 | (while (and | |
627 | (progn | |
628 | (c-forward-syntactic-ws) | |
629 | (looking-at c-symbol-key)) | |
630 | (progn | |
631 | (c-put-font-lock-face | |
632 | (match-beginning 0) (match-end 0) | |
633 | 'font-lock-variable-name-face) | |
634 | (goto-char (match-end 0)) | |
635 | (c-forward-syntactic-ws) | |
636 | (eq (char-after) ?,))) | |
637 | (forward-char))) | |
638 | ||
639 | ;; "Variable". | |
640 | (c-put-font-lock-face | |
641 | (match-beginning ,(+ 1 ncle-depth nsws-depth)) | |
642 | (match-end ,(+ 1 ncle-depth nsws-depth)) | |
643 | 'font-lock-variable-name-face))))))) | |
d9e94c22 MS |
644 | |
645 | ;; Fontify cpp function names in preprocessor | |
646 | ;; expressions in #if and #elif. | |
0386b551 AM |
647 | ,@(when (and (c-lang-const c-cpp-expr-directives) |
648 | (c-lang-const c-cpp-expr-functions)) | |
649 | (let ((ced-re (c-make-keywords-re t | |
650 | (c-lang-const c-cpp-expr-directives))) | |
651 | (cef-re (c-make-keywords-re t | |
652 | (c-lang-const c-cpp-expr-functions)))) | |
b128268e AM |
653 | |
654 | `((,(c-make-font-lock-context-search-function | |
655 | `(,(concat noncontinued-line-end | |
656 | (c-lang-const c-opt-cpp-prefix) | |
657 | ced-re ; 1 + ncle-depth | |
658 | ;; Match the whole logical line to look | |
659 | ;; for the functions in. | |
660 | "\\(\\\\\\(.\\|[\n\r]\\)\\|[^\n\r]\\)*") | |
661 | ((let ((limit (match-end 0))) | |
662 | (while (re-search-forward ,cef-re limit 'move) | |
663 | (c-put-font-lock-face (match-beginning 1) | |
664 | (match-end 1) | |
665 | c-preprocessor-face-name))) | |
666 | (goto-char (match-end ,(1+ ncle-depth))))) | |
667 | `(in-cpp-expr | |
668 | (save-excursion (c-end-of-macro) (point)) | |
669 | ,cef-re | |
670 | (1 c-preprocessor-face-name t))))))) | |
d9e94c22 MS |
671 | |
672 | ;; Fontify the directive names. | |
673 | (,(c-make-font-lock-search-function | |
674 | (concat noncontinued-line-end | |
675 | "\\(" | |
676 | (c-lang-const c-opt-cpp-prefix) | |
677 | "[" (c-lang-const c-symbol-chars) "]+" | |
678 | "\\)") | |
679 | `(,(1+ ncle-depth) c-preprocessor-face-name t))) | |
bd8cf505 | 680 | |
0386b551 AM |
681 | (eval . (list ,(c-make-syntactic-matcher |
682 | (concat noncontinued-line-end | |
683 | (c-lang-const c-opt-cpp-prefix) | |
684 | "if\\(n\\)def\\>")) | |
685 | ,(+ ncle-depth 1) | |
686 | c-negation-char-face-name | |
687 | 'append)) | |
d9e94c22 MS |
688 | ))) |
689 | ||
690 | ,@(when (c-major-mode-is 'pike-mode) | |
0386b551 | 691 | ;; Recognize hashbangs in Pike. |
d9e94c22 MS |
692 | `((eval . (list "\\`#![^\n\r]*" |
693 | 0 c-preprocessor-face-name)))) | |
694 | ||
0386b551 | 695 | ;; Make hard spaces visible through an inverted `font-lock-warning-face'. |
d9e94c22 MS |
696 | (eval . (list |
697 | "\240" | |
698 | 0 (progn | |
0386b551 AM |
699 | (unless (c-face-name-p 'c-nonbreakable-space-face) |
700 | (c-make-inverse-face 'font-lock-warning-face | |
701 | 'c-nonbreakable-space-face)) | |
702 | ''c-nonbreakable-space-face))) | |
d9e94c22 MS |
703 | )) |
704 | ||
705 | (defun c-font-lock-invalid-string () | |
706 | ;; Assuming the point is after the opening character of a string, | |
0386b551 | 707 | ;; fontify that char with `font-lock-warning-face' if the string |
a6782a6e | 708 | ;; decidedly isn't terminated properly. |
0386b551 AM |
709 | ;; |
710 | ;; This function does hidden buffer changes. | |
a6782a6e MS |
711 | (let ((start (1- (point)))) |
712 | (save-excursion | |
0386b551 AM |
713 | (and (eq (elt (parse-partial-sexp start (c-point 'eol)) 8) start) |
714 | (if (integerp c-multiline-string-start-char) | |
715 | ;; There's no multiline string start char before the | |
716 | ;; string, so newlines aren't allowed. | |
717 | (not (eq (char-before start) c-multiline-string-start-char)) | |
718 | ;; Multiline strings are allowed anywhere if | |
719 | ;; c-multiline-string-start-char is t. | |
720 | (not c-multiline-string-start-char)) | |
721 | (if c-string-escaped-newlines | |
a6782a6e MS |
722 | ;; There's no \ before the newline. |
723 | (not (eq (char-before (point)) ?\\)) | |
0386b551 | 724 | ;; Escaped newlines aren't supported. |
a6782a6e | 725 | t) |
0386b551 | 726 | (c-put-font-lock-face start (1+ start) 'font-lock-warning-face))))) |
d9e94c22 MS |
727 | |
728 | (c-lang-defconst c-basic-matchers-before | |
729 | "Font lock matchers for basic keywords, labels, references and various | |
730 | other easily recognizable things that should be fontified before generic | |
731 | casts and declarations are fontified. Used on level 2 and higher." | |
732 | ||
733 | ;; Note: `c-font-lock-declarations' assumes that no matcher here | |
734 | ;; sets `font-lock-type-face' in languages where | |
735 | ;; `c-recognize-<>-arglists' is set. | |
736 | ||
737 | t `(;; Put a warning face on the opener of unclosed strings that | |
738 | ;; can't span lines. Later font | |
739 | ;; lock packages have a `font-lock-syntactic-face-function' for | |
740 | ;; this, but it doesn't give the control we want since any | |
741 | ;; fontification done inside the function will be | |
742 | ;; unconditionally overridden. | |
743 | ,(c-make-font-lock-search-function | |
744 | ;; Match a char before the string starter to make | |
745 | ;; `c-skip-comments-and-strings' work correctly. | |
746 | (concat ".\\(" c-string-limit-regexp "\\)") | |
747 | '((c-font-lock-invalid-string))) | |
748 | ||
749 | ;; Fontify keyword constants. | |
750 | ,@(when (c-lang-const c-constant-kwds) | |
751 | (let ((re (c-make-keywords-re nil (c-lang-const c-constant-kwds)))) | |
752 | (if (c-major-mode-is 'pike-mode) | |
753 | ;; No symbol is a keyword after "->" in Pike. | |
0386b551 | 754 | `((eval . (list ,(concat "\\(\\=.?\\|[^>]\\|[^-]>\\)" |
d9e94c22 | 755 | "\\<\\(" re "\\)\\>") |
0386b551 | 756 | 2 c-constant-face-name))) |
d9e94c22 MS |
757 | `((eval . (list ,(concat "\\<\\(" re "\\)\\>") |
758 | 1 c-constant-face-name)))))) | |
759 | ||
760 | ;; Fontify all keywords except the primitive types. | |
761 | ,(if (c-major-mode-is 'pike-mode) | |
762 | ;; No symbol is a keyword after "->" in Pike. | |
0386b551 | 763 | `(,(concat "\\(\\=.?\\|[^>]\\|[^-]>\\)" |
d9e94c22 | 764 | "\\<" (c-lang-const c-regular-keywords-regexp)) |
0386b551 | 765 | 2 font-lock-keyword-face) |
d9e94c22 MS |
766 | `(,(concat "\\<" (c-lang-const c-regular-keywords-regexp)) |
767 | 1 font-lock-keyword-face)) | |
768 | ||
769 | ;; Fontify leading identifiers in fully qualified names like | |
770 | ;; "foo::bar" in languages that supports such things. | |
771 | ,@(when (c-lang-const c-opt-identifier-concat-key) | |
2a15eb73 MS |
772 | (if (c-major-mode-is 'java-mode) |
773 | ;; Java needs special treatment since "." is used both to | |
774 | ;; qualify names and in normal indexing. Here we look for | |
775 | ;; capital characters at the beginning of an identifier to | |
776 | ;; recognize the class. "*" is also recognized to cover | |
777 | ;; wildcard import declarations. All preceding dot separated | |
778 | ;; identifiers are taken as package names and therefore | |
779 | ;; fontified as references. | |
780 | `(,(c-make-font-lock-search-function | |
781 | ;; Search for class identifiers preceded by ".". The | |
782 | ;; anchored matcher takes it from there. | |
783 | (concat (c-lang-const c-opt-identifier-concat-key) | |
0386b551 | 784 | (c-lang-const c-simple-ws) "*" |
2a15eb73 | 785 | (concat "\\(" |
0386b551 AM |
786 | "[" c-upper "]" |
787 | "[" (c-lang-const c-symbol-chars) "]*" | |
2a15eb73 MS |
788 | "\\|" |
789 | "\\*" | |
790 | "\\)")) | |
791 | `((let (id-end) | |
792 | (goto-char (1+ (match-beginning 0))) | |
793 | (while (and (eq (char-before) ?.) | |
794 | (progn | |
795 | (backward-char) | |
796 | (c-backward-syntactic-ws) | |
797 | (setq id-end (point)) | |
798 | (< (skip-chars-backward | |
799 | ,(c-lang-const c-symbol-chars)) 0)) | |
800 | (not (get-text-property (point) 'face))) | |
0386b551 AM |
801 | (c-put-font-lock-face (point) id-end |
802 | c-reference-face-name) | |
2a15eb73 MS |
803 | (c-backward-syntactic-ws))) |
804 | nil | |
805 | (goto-char (match-end 0))))) | |
806 | ||
807 | `((,(byte-compile | |
0386b551 AM |
808 | ;; Must use a function here since we match longer than |
809 | ;; we want to move before doing a new search. This is | |
810 | ;; not necessary for XEmacs since it restarts the | |
811 | ;; search from the end of the first highlighted | |
812 | ;; submatch (something that causes problems in other | |
813 | ;; places). | |
2a15eb73 MS |
814 | `(lambda (limit) |
815 | (while (re-search-forward | |
816 | ,(concat "\\(\\<" ; 1 | |
817 | "\\(" (c-lang-const c-symbol-key) "\\)" ; 2 | |
0386b551 | 818 | (c-lang-const c-simple-ws) "*" |
2a15eb73 | 819 | (c-lang-const c-opt-identifier-concat-key) |
0386b551 | 820 | (c-lang-const c-simple-ws) "*" |
2a15eb73 MS |
821 | "\\)" |
822 | "\\(" | |
823 | (c-lang-const c-opt-after-id-concat-key) | |
824 | "\\)") | |
825 | limit t) | |
826 | (unless (progn | |
827 | (goto-char (match-beginning 0)) | |
828 | (c-skip-comments-and-strings limit)) | |
829 | (or (get-text-property (match-beginning 2) 'face) | |
830 | (c-put-font-lock-face (match-beginning 2) | |
831 | (match-end 2) | |
832 | c-reference-face-name)) | |
833 | (goto-char (match-end 1)))))))))) | |
d9e94c22 MS |
834 | |
835 | ;; Fontify the special declarations in Objective-C. | |
836 | ,@(when (c-major-mode-is 'objc-mode) | |
837 | `(;; Fontify class names in the beginning of message expressions. | |
838 | ,(c-make-font-lock-search-function | |
839 | "\\[" | |
840 | '((c-fontify-types-and-refs () | |
841 | (c-forward-syntactic-ws limit) | |
842 | (let ((start (point))) | |
843 | ;; In this case we accept both primitive and known types. | |
844 | (when (eq (c-forward-type) 'known) | |
845 | (goto-char start) | |
846 | (let ((c-promote-possible-types t)) | |
847 | (c-forward-type)))) | |
848 | (if (> (point) limit) (goto-char limit))))) | |
849 | ||
850 | ;; The @interface/@implementation/@protocol directives. | |
0386b551 AM |
851 | ,(c-make-font-lock-search-function |
852 | (concat "\\<" | |
853 | (regexp-opt | |
d9e94c22 MS |
854 | '("@interface" "@implementation" "@protocol") |
855 | t) | |
856 | "\\>") | |
0386b551 AM |
857 | '((c-fontify-types-and-refs |
858 | (;; The font-lock package in Emacs is known to clobber | |
859 | ;; `parse-sexp-lookup-properties' (when it exists). | |
860 | (parse-sexp-lookup-properties | |
861 | (cc-eval-when-compile | |
862 | (boundp 'parse-sexp-lookup-properties)))) | |
863 | (c-forward-objc-directive) | |
864 | nil) | |
865 | (goto-char (match-beginning 0)))))) | |
866 | ||
867 | (eval . (list "\\(!\\)[^=]" 1 c-negation-char-face-name)) | |
d9e94c22 MS |
868 | )) |
869 | ||
870 | (defun c-font-lock-complex-decl-prepare (limit) | |
1379f2c5 AM |
871 | ;; This function will be called from font-lock for a region bounded by POINT |
872 | ;; and LIMIT, as though it were to identify a keyword for | |
873 | ;; font-lock-keyword-face. It always returns NIL to inhibit this and | |
874 | ;; prevent a repeat invocation. See elisp/lispref page "Search-based | |
875 | ;; Fontification". | |
876 | ;; | |
d9e94c22 | 877 | ;; Called before any of the matchers in `c-complex-decl-matchers'. |
0386b551 AM |
878 | ;; |
879 | ;; This function does hidden buffer changes. | |
d9e94c22 MS |
880 | |
881 | ;;(message "c-font-lock-complex-decl-prepare %s %s" (point) limit) | |
882 | ||
883 | ;; Clear the list of found types if we start from the start of the | |
884 | ;; buffer, to make it easier to get rid of misspelled types and | |
e15f8aaa | 885 | ;; variables that have gotten recognized as types in malformed code. |
d9e94c22 MS |
886 | (when (bobp) |
887 | (c-clear-found-types)) | |
888 | ||
e15f8aaa AM |
889 | ;; Clear the c-type char properties which mark the region, to recalculate |
890 | ;; them properly. The most interesting properties are those put on the | |
891 | ;; closest token before the region. | |
892 | (save-excursion | |
893 | (let ((pos (point))) | |
894 | (c-backward-syntactic-ws) | |
895 | (c-clear-char-properties | |
896 | (if (and (not (bobp)) | |
897 | (memq (c-get-char-property (1- (point)) 'c-type) | |
898 | '(c-decl-arg-start | |
899 | c-decl-end | |
900 | c-decl-id-start | |
901 | c-decl-type-start))) | |
902 | (1- (point)) | |
903 | pos) | |
904 | limit 'c-type))) | |
d9e94c22 MS |
905 | |
906 | ;; Update `c-state-cache' to the beginning of the region. This will | |
907 | ;; make `c-beginning-of-syntax' go faster when it's used later on, | |
908 | ;; and it's near the point most of the time. | |
909 | (c-parse-state) | |
910 | ||
911 | ;; Check if the fontified region starts inside a declarator list so | |
912 | ;; that `c-font-lock-declarators' should be called at the start. | |
e15f8aaa AM |
913 | ;; The declared identifiers are font-locked correctly as types, if |
914 | ;; that is what they are. | |
d9e94c22 MS |
915 | (let ((prop (save-excursion |
916 | (c-backward-syntactic-ws) | |
917 | (unless (bobp) | |
918 | (c-get-char-property (1- (point)) 'c-type))))) | |
919 | (when (memq prop '(c-decl-id-start c-decl-type-start)) | |
920 | (c-forward-syntactic-ws limit) | |
921 | (c-font-lock-declarators limit t (eq prop 'c-decl-type-start)))) | |
922 | ||
b128268e AM |
923 | (setq c-font-lock-context ;; (c-guess-font-lock-context) |
924 | (save-excursion | |
925 | (if (and c-cpp-expr-intro-re | |
926 | (c-beginning-of-macro) | |
927 | (looking-at c-cpp-expr-intro-re)) | |
928 | 'in-cpp-expr))) | |
d9e94c22 MS |
929 | nil) |
930 | ||
931 | (defun c-font-lock-<>-arglists (limit) | |
1379f2c5 AM |
932 | ;; This function will be called from font-lock for a region bounded by POINT |
933 | ;; and LIMIT, as though it were to identify a keyword for | |
934 | ;; font-lock-keyword-face. It always returns NIL to inhibit this and | |
935 | ;; prevent a repeat invocation. See elisp/lispref page "Search-based | |
936 | ;; Fontification". | |
937 | ;; | |
d9e94c22 | 938 | ;; Fontify types and references in names containing angle bracket |
0386b551 | 939 | ;; arglists from the point to LIMIT. Note that |
1379f2c5 | 940 | ;; `c-font-lock-declarations' already has handled many of them. |
0386b551 AM |
941 | ;; |
942 | ;; This function might do hidden buffer changes. | |
d9e94c22 MS |
943 | |
944 | (let (;; The font-lock package in Emacs is known to clobber | |
945 | ;; `parse-sexp-lookup-properties' (when it exists). | |
946 | (parse-sexp-lookup-properties | |
947 | (cc-eval-when-compile | |
948 | (boundp 'parse-sexp-lookup-properties))) | |
0386b551 AM |
949 | (c-parse-and-markup-<>-arglists t) |
950 | c-restricted-<>-arglists | |
951 | id-start id-end id-face pos kwd-sym) | |
d9e94c22 MS |
952 | |
953 | (while (and (< (point) limit) | |
954 | (re-search-forward c-opt-<>-arglist-start limit t)) | |
955 | ||
956 | (setq id-start (match-beginning 1) | |
957 | id-end (match-end 1) | |
958 | pos (point)) | |
959 | ||
960 | (goto-char id-start) | |
961 | (unless (c-skip-comments-and-strings limit) | |
0386b551 AM |
962 | (setq kwd-sym nil |
963 | c-restricted-<>-arglists nil | |
964 | id-face (get-text-property id-start 'face)) | |
965 | ||
966 | (if (cond | |
967 | ((eq id-face 'font-lock-type-face) | |
968 | ;; The identifier got the type face so it has already been | |
969 | ;; handled in `c-font-lock-declarations'. | |
970 | nil) | |
971 | ||
972 | ((eq id-face 'font-lock-keyword-face) | |
973 | (when (looking-at c-opt-<>-sexp-key) | |
974 | ;; There's a special keyword before the "<" that tells | |
975 | ;; that it's an angle bracket arglist. | |
976 | (setq kwd-sym (c-keyword-sym (match-string 1))))) | |
977 | ||
978 | (t | |
979 | ;; There's a normal identifier before the "<". If we're not in | |
980 | ;; a declaration context then we set `c-restricted-<>-arglists' | |
981 | ;; to avoid recognizing templates in function calls like "foo (a | |
982 | ;; < b, c > d)". | |
983 | (c-backward-syntactic-ws) | |
984 | (when (and (memq (char-before) '(?\( ?,)) | |
985 | (not (eq (get-text-property (1- (point)) 'c-type) | |
986 | 'c-decl-arg-start))) | |
987 | (setq c-restricted-<>-arglists t)) | |
988 | t)) | |
989 | ||
d9e94c22 MS |
990 | (progn |
991 | (goto-char (1- pos)) | |
992 | ;; Check for comment/string both at the identifier and | |
993 | ;; at the "<". | |
994 | (unless (c-skip-comments-and-strings limit) | |
995 | ||
0386b551 AM |
996 | (c-fontify-types-and-refs () |
997 | (when (c-forward-<>-arglist (c-keyword-member | |
998 | kwd-sym 'c-<>-type-kwds)) | |
999 | (when (and c-opt-identifier-concat-key | |
1000 | (not (get-text-property id-start 'face))) | |
1001 | (c-forward-syntactic-ws) | |
1002 | (if (looking-at c-opt-identifier-concat-key) | |
1003 | (c-put-font-lock-face id-start id-end | |
1004 | c-reference-face-name) | |
d9e94c22 | 1005 | (c-put-font-lock-face id-start id-end |
0386b551 | 1006 | 'font-lock-type-face))))) |
d9e94c22 MS |
1007 | |
1008 | (goto-char pos))) | |
1009 | (goto-char pos))))) | |
1010 | nil) | |
1011 | ||
1012 | (defun c-font-lock-declarators (limit list types) | |
e15f8aaa AM |
1013 | ;; Assuming the point is at the start of a declarator in a declaration, |
1014 | ;; fontify the identifier it declares. (If TYPES is set, it does this via | |
1015 | ;; the macro `c-fontify-types-and-refs'.) | |
1016 | ;; | |
1017 | ;; If LIST is non-nil, also fontify the ids in any following declarators in | |
1018 | ;; a comma separated list (e.g. "foo" and "*bar" in "int foo = 17, *bar;"); | |
1019 | ;; additionally, mark the commas with c-type property 'c-decl-id-start or | |
1020 | ;; 'c-decl-type-start (according to TYPES). Stop at LIMIT. | |
1021 | ;; | |
1022 | ;; If TYPES is non-nil, fontify all identifiers as types. | |
1023 | ;; | |
1024 | ;; Nil is always returned. The function leaves point at the delimiter after | |
1025 | ;; the last declarator it processes. | |
0386b551 AM |
1026 | ;; |
1027 | ;; This function might do hidden buffer changes. | |
d9e94c22 MS |
1028 | |
1029 | ;;(message "c-font-lock-declarators from %s to %s" (point) limit) | |
1030 | (c-fontify-types-and-refs | |
1031 | ((pos (point)) next-pos id-start id-end | |
1032 | paren-depth | |
1033 | id-face got-init | |
1034 | c-last-identifier-range | |
1035 | (separator-prop (if types 'c-decl-type-start 'c-decl-id-start))) | |
1036 | ||
e15f8aaa AM |
1037 | ;; The following `while' fontifies a single declarator id each time round. |
1038 | ;; It loops only when LIST is non-nil. | |
1039 | (while | |
1040 | ;; Inside the following "condition form", we move forward over the | |
1041 | ;; declarator's identifier up as far as any opening bracket (for array | |
1042 | ;; size) or paren (for parameters of function-type) or brace (for | |
e1dbe924 | 1043 | ;; array/struct initialization) or "=" or terminating delimiter |
e15f8aaa AM |
1044 | ;; (e.g. "," or ";" or "}"). |
1045 | (and | |
d9e94c22 MS |
1046 | pos |
1047 | (< (point) limit) | |
1048 | ||
e15f8aaa AM |
1049 | ;; The following form moves forward over the declarator's |
1050 | ;; identifier (and what precedes it), returning t. If there | |
1051 | ;; wasn't one, it returns nil, terminating the `while'. | |
d9e94c22 MS |
1052 | (let (got-identifier) |
1053 | (setq paren-depth 0) | |
e15f8aaa AM |
1054 | ;; Skip over type decl prefix operators, one for each iteration |
1055 | ;; of the while. These are, e.g. "*" in "int *foo" or "(" and | |
1056 | ;; "*" in "int (*foo) (void)" (Note similar code in | |
1057 | ;; `c-forward-decl-or-cast-1'.) | |
d9e94c22 MS |
1058 | (while (and (looking-at c-type-decl-prefix-key) |
1059 | (if (and (c-major-mode-is 'c++-mode) | |
e15f8aaa AM |
1060 | (match-beginning 3)) |
1061 | ;; If the third submatch matches in C++ then | |
d9e94c22 MS |
1062 | ;; we're looking at an identifier that's a |
1063 | ;; prefix only if it specifies a member pointer. | |
1064 | (progn | |
1065 | (setq id-start (point)) | |
1066 | (c-forward-name) | |
1067 | (if (looking-at "\\(::\\)") | |
1068 | ;; We only check for a trailing "::" and | |
1069 | ;; let the "*" that should follow be | |
1070 | ;; matched in the next round. | |
1071 | t | |
1072 | ;; It turned out to be the real identifier, | |
1073 | ;; so flag that and stop. | |
1074 | (setq got-identifier t) | |
1075 | nil)) | |
1076 | t)) | |
1077 | (if (eq (char-after) ?\() | |
1078 | (progn | |
1079 | (setq paren-depth (1+ paren-depth)) | |
1080 | (forward-char)) | |
1081 | (goto-char (match-end 1))) | |
1082 | (c-forward-syntactic-ws)) | |
1083 | ||
e15f8aaa | 1084 | ;; If we haven't passed the identifier already, do it now. |
d9e94c22 MS |
1085 | (unless got-identifier |
1086 | (setq id-start (point)) | |
1087 | (c-forward-name)) | |
1088 | (setq id-end (point)) | |
1089 | ||
1090 | (/= id-end pos)) | |
1091 | ||
e15f8aaa AM |
1092 | ;; Skip out of the parens surrounding the identifier. If closing |
1093 | ;; parens are missing, this form returns nil. | |
d9e94c22 MS |
1094 | (or (= paren-depth 0) |
1095 | (c-safe (goto-char (scan-lists (point) 1 paren-depth)))) | |
1096 | ||
1097 | (<= (point) limit) | |
1098 | ||
e15f8aaa | 1099 | ;; Skip over any trailing bit, such as "__attribute__". |
0386b551 AM |
1100 | (progn |
1101 | (when (looking-at c-decl-hangon-key) | |
1102 | (c-forward-keyword-clause 1)) | |
1103 | (<= (point) limit)) | |
1104 | ||
d9e94c22 | 1105 | ;; Search syntactically to the end of the declarator (";", |
da6062e6 | 1106 | ;; ",", a closing paren, eob etc) or to the beginning of an |
2a15eb73 MS |
1107 | ;; initializer or function prototype ("=" or "\\s\("). |
1108 | ;; Note that the open paren will match array specs in | |
1109 | ;; square brackets, and we treat them as initializers too. | |
d9e94c22 | 1110 | (c-syntactic-re-search-forward |
2a15eb73 | 1111 | "[;,]\\|\\s)\\|\\'\\|\\(=\\|\\s(\\)" limit t t)) |
d9e94c22 MS |
1112 | |
1113 | (setq next-pos (match-beginning 0) | |
39f7186c CY |
1114 | id-face (if (and (eq (char-after next-pos) ?\() |
1115 | (let (c-last-identifier-range) | |
1116 | (save-excursion | |
1117 | (goto-char next-pos) | |
1118 | (c-at-toplevel-p)))) | |
d9e94c22 MS |
1119 | 'font-lock-function-name-face |
1120 | 'font-lock-variable-name-face) | |
2a15eb73 MS |
1121 | got-init (and (match-beginning 1) |
1122 | (char-after (match-beginning 1)))) | |
d9e94c22 MS |
1123 | |
1124 | (if types | |
53964682 | 1125 | ;; Register and fontify the identifier as a type. |
d9e94c22 MS |
1126 | (let ((c-promote-possible-types t)) |
1127 | (goto-char id-start) | |
1128 | (c-forward-type)) | |
1129 | ;; Fontify the last symbol in the identifier if it isn't fontified | |
1130 | ;; already. The check is necessary only in certain cases where this | |
1131 | ;; function is used "sloppily", e.g. in `c-simple-decl-matchers'. | |
1132 | (when (and c-last-identifier-range | |
1133 | (not (get-text-property (car c-last-identifier-range) | |
1134 | 'face))) | |
1135 | (c-put-font-lock-face (car c-last-identifier-range) | |
1136 | (cdr c-last-identifier-range) | |
1137 | id-face))) | |
1138 | ||
1139 | (goto-char next-pos) | |
e15f8aaa | 1140 | (setq pos nil) ; So as to terminate the enclosing `while' form. |
d9e94c22 MS |
1141 | (when list |
1142 | ;; Jump past any initializer or function prototype to see if | |
1143 | ;; there's a ',' to continue at. | |
1144 | ||
1145 | (cond ((eq id-face 'font-lock-function-name-face) | |
1146 | ;; Skip a parenthesized initializer (C++) or a function | |
1147 | ;; prototype. | |
e15f8aaa | 1148 | (if (c-safe (c-forward-sexp 1) t) ; over the parameter list. |
d9e94c22 | 1149 | (c-forward-syntactic-ws limit) |
e15f8aaa | 1150 | (goto-char limit))) ; unbalanced parens |
d9e94c22 | 1151 | |
e15f8aaa | 1152 | (got-init ; "=" sign OR opening "(", "[", or "{" |
2a15eb73 MS |
1153 | ;; Skip an initializer expression. If we're at a '=' |
1154 | ;; then accept a brace list directly after it to cope | |
1155 | ;; with array initializers. Otherwise stop at braces | |
1156 | ;; to avoid going past full function and class blocks. | |
1157 | (and (if (and (eq got-init ?=) | |
3efc2cd7 | 1158 | (= (c-forward-token-2 1 nil limit) 0) |
2a15eb73 | 1159 | (looking-at "{")) |
e15f8aaa | 1160 | (c-safe (c-forward-sexp) t) ; over { .... } |
2a15eb73 | 1161 | t) |
0386b551 AM |
1162 | ;; FIXME: Should look for c-decl-end markers here; |
1163 | ;; we might go far into the following declarations | |
1164 | ;; in e.g. ObjC mode (see e.g. methods-4.m). | |
2a15eb73 MS |
1165 | (c-syntactic-re-search-forward "[;,{]" limit 'move t) |
1166 | (backward-char))) | |
d9e94c22 MS |
1167 | |
1168 | (t (c-forward-syntactic-ws limit))) | |
1169 | ||
1170 | ;; If a ',' is found we set pos to the next declarator and iterate. | |
1171 | (when (and (< (point) limit) (looking-at ",")) | |
1172 | (c-put-char-property (point) 'c-type separator-prop) | |
1173 | (forward-char) | |
1174 | (c-forward-syntactic-ws limit) | |
e15f8aaa | 1175 | (setq pos (point)))))) ; acts to make the `while' form continue. |
d9e94c22 MS |
1176 | nil) |
1177 | ||
1178 | (defconst c-font-lock-maybe-decl-faces | |
1179 | ;; List of faces that might be put at the start of a type when | |
1180 | ;; `c-font-lock-declarations' runs. This needs to be evaluated to | |
1181 | ;; ensure that face name aliases in Emacs are resolved. | |
1182 | (list nil | |
1183 | font-lock-type-face | |
1184 | c-reference-face-name | |
1185 | font-lock-keyword-face)) | |
1186 | ||
d9e94c22 | 1187 | (defun c-font-lock-declarations (limit) |
e15f8aaa AM |
1188 | ;; Fontify all the declarations, casts and labels from the point to LIMIT. |
1189 | ;; Assumes that strings and comments have been fontified already. | |
1190 | ;; | |
1379f2c5 AM |
1191 | ;; This function will be called from font-lock for a region bounded by POINT |
1192 | ;; and LIMIT, as though it were to identify a keyword for | |
1193 | ;; font-lock-keyword-face. It always returns NIL to inhibit this and | |
1194 | ;; prevent a repeat invocation. See elisp/lispref page "Search-based | |
1195 | ;; Fontification". | |
1196 | ;; | |
0386b551 | 1197 | ;; This function might do hidden buffer changes. |
d9e94c22 MS |
1198 | |
1199 | ;;(message "c-font-lock-declarations search from %s to %s" (point) limit) | |
1200 | ||
1201 | (save-restriction | |
e15f8aaa | 1202 | (let (;; The position where `c-find-decl-spots' last stopped. |
0386b551 | 1203 | start-pos |
e15f8aaa AM |
1204 | ;; o - 'decl if we're in an arglist containing declarations |
1205 | ;; (but if `c-recognize-paren-inits' is set it might also be | |
1206 | ;; an initializer arglist); | |
1207 | ;; o - '<> if the arglist is of angle bracket type; | |
1208 | ;; o - 'arglist if it's some other arglist; | |
1209 | ;; o - nil, if not in an arglist at all. This includes the | |
1210 | ;; parenthesised condition which follows "if", "while", etc. | |
0386b551 AM |
1211 | context |
1212 | ;; The position of the next token after the closing paren of | |
1213 | ;; the last detected cast. | |
d9e94c22 | 1214 | last-cast-end |
0386b551 AM |
1215 | ;; The result from `c-forward-decl-or-cast-1'. |
1216 | decl-or-cast | |
1217 | ;; The maximum of the end positions of all the checked type | |
1218 | ;; decl expressions in the successfully identified | |
1219 | ;; declarations. The position might be either before or | |
1220 | ;; after the syntactic whitespace following the last token | |
1221 | ;; in the type decl expression. | |
d9e94c22 MS |
1222 | (max-type-decl-end 0) |
1223 | ;; Same as `max-type-decl-*', but used when we're before | |
1224 | ;; `token-pos'. | |
1225 | (max-type-decl-end-before-token 0) | |
0386b551 AM |
1226 | ;; Set according to the context to direct the heuristics for |
1227 | ;; recognizing C++ templates. | |
1228 | c-restricted-<>-arglists | |
1229 | ;; Turn on recording of identifier ranges in | |
1230 | ;; `c-forward-decl-or-cast-1' and `c-forward-label' for | |
1231 | ;; later fontification. | |
1232 | (c-record-type-identifiers t) | |
1379f2c5 | 1233 | label-type |
d9e94c22 | 1234 | c-record-ref-identifiers |
0386b551 AM |
1235 | ;; Make `c-forward-type' calls mark up template arglists if |
1236 | ;; it finds any. That's necessary so that we later will | |
1237 | ;; stop inside them to fontify types there. | |
1238 | (c-parse-and-markup-<>-arglists t) | |
d9e94c22 MS |
1239 | ;; The font-lock package in Emacs is known to clobber |
1240 | ;; `parse-sexp-lookup-properties' (when it exists). | |
1241 | (parse-sexp-lookup-properties | |
1242 | (cc-eval-when-compile | |
1243 | (boundp 'parse-sexp-lookup-properties)))) | |
1244 | ||
1245 | ;; Below we fontify a whole declaration even when it crosses the limit, | |
abfc152b | 1246 | ;; to avoid gaps when jit/lazy-lock fontifies the file a block at a |
d9e94c22 MS |
1247 | ;; time. That is however annoying during editing, e.g. the following is |
1248 | ;; a common situation while the first line is being written: | |
1249 | ;; | |
1250 | ;; my_variable | |
1251 | ;; some_other_variable = 0; | |
1252 | ;; | |
1253 | ;; font-lock will put the limit at the beginning of the second line | |
1254 | ;; here, and if we go past it we'll fontify "my_variable" as a type and | |
1255 | ;; "some_other_variable" as an identifier, and the latter will not | |
1256 | ;; correct itself until the second line is changed. To avoid that we | |
1257 | ;; narrow to the limit if the region to fontify is a single line. | |
abfc152b AM |
1258 | (if (<= limit (c-point 'bonl)) |
1259 | (narrow-to-region | |
1260 | (point-min) | |
0386b551 AM |
1261 | (save-excursion |
1262 | ;; Narrow after any operator chars following the limit though, | |
1263 | ;; since those characters can be useful in recognizing a | |
1264 | ;; declaration (in particular the '{' that opens a function body | |
1265 | ;; after the header). | |
1266 | (goto-char limit) | |
1267 | (skip-chars-forward c-nonsymbol-chars) | |
abfc152b | 1268 | (point)))) |
d9e94c22 MS |
1269 | |
1270 | (c-find-decl-spots | |
1271 | limit | |
0386b551 | 1272 | c-decl-start-re |
d9e94c22 MS |
1273 | c-font-lock-maybe-decl-faces |
1274 | ||
1275 | (lambda (match-pos inside-macro) | |
0386b551 AM |
1276 | (setq start-pos (point)) |
1277 | (when | |
1278 | ;; The result of the form below is true when we don't recognize a | |
1279 | ;; declaration or cast. | |
536610a4 AM |
1280 | (if (or (and (eq (get-text-property (point) 'face) |
1281 | 'font-lock-keyword-face) | |
1282 | (looking-at c-not-decl-init-keywords)) | |
1283 | (and c-macro-with-semi-re | |
1284 | (looking-at c-macro-with-semi-re))) ; 2008-11-04 | |
0386b551 AM |
1285 | ;; Don't do anything more if we're looking at a keyword that |
1286 | ;; can't start a declaration. | |
1287 | t | |
1288 | ||
e15f8aaa AM |
1289 | ;; Set `context' and `c-restricted-<>-arglists'. Look for |
1290 | ;; "<" for the sake of C++-style template arglists. | |
1291 | ;; Ignore "(" when it's part of a control flow construct | |
1292 | ;; (e.g. "for ("). | |
1293 | (let ((type (and (> match-pos (point-min)) | |
1294 | (c-get-char-property (1- match-pos) 'c-type)))) | |
1295 | (cond ((not (memq (char-before match-pos) '(?\( ?, ?\[ ?<))) | |
1296 | (setq context nil | |
1297 | c-restricted-<>-arglists nil)) | |
1298 | ;; A control flow expression | |
1299 | ((and (eq (char-before match-pos) ?\() | |
1300 | (save-excursion | |
1301 | (goto-char match-pos) | |
1302 | (backward-char) | |
1303 | (c-backward-token-2) | |
1304 | (looking-at c-block-stmt-2-key))) | |
1305 | (setq context nil | |
1306 | c-restricted-<>-arglists t)) | |
1307 | ;; Near BOB. | |
1308 | ((<= match-pos (point-min)) | |
1309 | (setq context 'arglist | |
1310 | c-restricted-<>-arglists t)) | |
1311 | ;; Got a cached hit in a declaration arglist. | |
1312 | ((eq type 'c-decl-arg-start) | |
1313 | (setq context 'decl | |
1314 | c-restricted-<>-arglists nil)) | |
1315 | ;; Inside an angle bracket arglist. | |
1316 | ((or (eq type 'c-<>-arg-sep) | |
1317 | (eq (char-before match-pos) ?<)) | |
1318 | (setq context '<> | |
1319 | c-restricted-<>-arglists nil)) | |
1320 | ;; Got a cached hit in some other type of arglist. | |
1321 | (type | |
1322 | (setq context 'arglist | |
1323 | c-restricted-<>-arglists t)) | |
1324 | ((if inside-macro | |
1325 | (< match-pos max-type-decl-end-before-token) | |
1326 | (< match-pos max-type-decl-end)) | |
1327 | ;; The point is within the range of a previously | |
1328 | ;; encountered type decl expression, so the arglist | |
1329 | ;; is probably one that contains declarations. | |
1330 | ;; However, if `c-recognize-paren-inits' is set it | |
1331 | ;; might also be an initializer arglist. | |
1332 | (setq context 'decl | |
1333 | c-restricted-<>-arglists nil) | |
1334 | ;; The result of this check is cached with a char | |
1335 | ;; property on the match token, so that we can look | |
1336 | ;; it up again when refontifying single lines in a | |
1337 | ;; multiline declaration. | |
1338 | (c-put-char-property (1- match-pos) | |
1339 | 'c-type 'c-decl-arg-start)) | |
1340 | (t (setq context 'arglist | |
1341 | c-restricted-<>-arglists t)))) | |
1342 | ||
1343 | ;; Check we haven't missed a preceding "typedef". | |
1344 | (when (not (looking-at c-typedef-key)) | |
1345 | (c-backward-syntactic-ws) | |
1346 | (c-backward-token-2) | |
1347 | (or (looking-at c-typedef-key) | |
1348 | (goto-char start-pos))) | |
1349 | ||
1350 | ;; Now analyze the construct. | |
b248a85d AM |
1351 | ;; In QT, "more" is an irritating keyword that expands to nothing. |
1352 | ;; We skip over it to prevent recognition of "more slots: <symbol>" | |
1353 | ;; as a bitfield declaration. | |
1354 | (when (and (c-major-mode-is 'c++-mode) | |
1355 | (looking-at | |
1356 | (concat "\\(more\\)\\([^" c-symbol-chars "]\\|$\\)"))) | |
1357 | (goto-char (match-end 1)) | |
1358 | (c-forward-syntactic-ws)) | |
e15f8aaa | 1359 | (setq decl-or-cast (c-forward-decl-or-cast-1 |
0386b551 AM |
1360 | match-pos context last-cast-end)) |
1361 | ||
8c0e3589 AM |
1362 | (cond |
1363 | ((eq decl-or-cast 'cast) | |
1364 | ;; Save the position after the previous cast so we can feed | |
1365 | ;; it to `c-forward-decl-or-cast-1' in the next round. That | |
1366 | ;; helps it discover cast chains like "(a) (b) c". | |
1367 | (setq last-cast-end (point)) | |
1368 | (c-fontify-recorded-types-and-refs) | |
1369 | nil) | |
10f5e3e6 | 1370 | |
8c0e3589 AM |
1371 | (decl-or-cast |
1372 | ;; We've found a declaration. | |
1373 | ||
1374 | ;; Set `max-type-decl-end' or `max-type-decl-end-before-token' | |
1375 | ;; under the assumption that we're after the first type decl | |
1376 | ;; expression in the declaration now. That's not really true; | |
1377 | ;; we could also be after a parenthesized initializer | |
1378 | ;; expression in C++, but this is only used as a last resort | |
1379 | ;; to slant ambiguous expression/declarations, and overall | |
1380 | ;; it's worth the risk to occasionally fontify an expression | |
1381 | ;; as a declaration in an initializer expression compared to | |
1382 | ;; getting ambiguous things in normal function prototypes | |
1383 | ;; fontified as expressions. | |
1384 | (if inside-macro | |
1385 | (when (> (point) max-type-decl-end-before-token) | |
1386 | (setq max-type-decl-end-before-token (point))) | |
1387 | (when (> (point) max-type-decl-end) | |
1388 | (setq max-type-decl-end (point)))) | |
1389 | ||
1390 | ;; Back up to the type to fontify the declarator(s). | |
1391 | (goto-char (car decl-or-cast)) | |
1392 | ||
1393 | (let ((decl-list | |
1394 | (if context | |
1395 | ;; Should normally not fontify a list of | |
1396 | ;; declarators inside an arglist, but the first | |
1397 | ;; argument in the ';' separated list of a "for" | |
1398 | ;; statement is an exception. | |
1399 | (when (eq (char-before match-pos) ?\() | |
1400 | (save-excursion | |
1401 | (goto-char (1- match-pos)) | |
1402 | (c-backward-syntactic-ws) | |
1403 | (and (c-simple-skip-symbol-backward) | |
1404 | (looking-at c-paren-stmt-key)))) | |
1405 | t))) | |
1406 | ||
1407 | ;; Fix the `c-decl-id-start' or `c-decl-type-start' property | |
1408 | ;; before the first declarator if it's a list. | |
1409 | ;; `c-font-lock-declarators' handles the rest. | |
1410 | (when decl-list | |
e15f8aaa | 1411 | (save-excursion |
8c0e3589 AM |
1412 | (c-backward-syntactic-ws) |
1413 | (unless (bobp) | |
1414 | (c-put-char-property (1- (point)) 'c-type | |
1415 | (if (cdr decl-or-cast) | |
1416 | 'c-decl-type-start | |
1417 | 'c-decl-id-start))))) | |
1418 | ||
1419 | (c-font-lock-declarators | |
1420 | (point-max) decl-list (cdr decl-or-cast))) | |
1421 | ||
1422 | ;; A declaration has been successfully identified, so do all the | |
1423 | ;; fontification of types and refs that've been recorded. | |
0386b551 | 1424 | (c-fontify-recorded-types-and-refs) |
8c0e3589 AM |
1425 | nil) |
1426 | ||
1427 | (t | |
1428 | ;; Are we at a declarator? Try to go back to the declaration | |
1429 | ;; to check this. If we get there, check whether a "typedef" | |
1430 | ;; is there, then fontify the declarators accordingly. | |
1431 | (let ((decl-search-lim (max (- (point) 50000) (point-min))) | |
7d0ee75c | 1432 | paren-state bod-res encl-pos is-typedef |
8c0e3589 AM |
1433 | c-recognize-knr-p) ; Strictly speaking, bogus, but it |
1434 | ; speeds up lisp.h tremendously. | |
1435 | (save-excursion | |
1436 | (setq bod-res (car (c-beginning-of-decl-1 decl-search-lim))) | |
1437 | (if (and (eq bod-res 'same) | |
1438 | (progn | |
1439 | (c-backward-syntactic-ws) | |
1440 | (eq (char-before) ?\}))) | |
1441 | (c-beginning-of-decl-1 decl-search-lim)) | |
1442 | ||
1443 | ;; We're now putatively at the declaration. | |
1444 | (setq paren-state (c-parse-state)) | |
1445 | ;; At top level or inside a "{"? | |
1446 | (if (or (not (setq encl-pos | |
1447 | (c-most-enclosing-brace paren-state))) | |
1448 | (eq (char-after encl-pos) ?\{)) | |
1449 | (progn | |
1450 | (when (looking-at c-typedef-key) ; "typedef" | |
1451 | (setq is-typedef t) | |
1452 | (goto-char (match-end 0)) | |
1453 | (c-forward-syntactic-ws)) | |
1454 | ;; At a real declaration? | |
1455 | (if (memq (c-forward-type t) '(t known found)) | |
1456 | (progn | |
1457 | (c-font-lock-declarators limit t is-typedef) | |
1458 | nil) | |
1459 | ;; False alarm. Return t to go on to the next check. | |
1460 | (goto-char start-pos) | |
1461 | t)) | |
1462 | t)))))) | |
0386b551 | 1463 | |
1379f2c5 AM |
1464 | ;; It was a false alarm. Check if we're in a label (or other |
1465 | ;; construct with `:' except bitfield) instead. | |
0386b551 | 1466 | (goto-char start-pos) |
1379f2c5 AM |
1467 | (when (setq label-type (c-forward-label t match-pos nil)) |
1468 | ;; Can't use `c-fontify-types-and-refs' here since we | |
1469 | ;; use the label face at times. | |
1470 | (cond ((eq label-type 'goto-target) | |
1471 | (c-put-font-lock-face (caar c-record-ref-identifiers) | |
1472 | (cdar c-record-ref-identifiers) | |
1473 | c-label-face-name)) | |
1474 | ((eq label-type 'qt-1kwd-colon) | |
1475 | (c-put-font-lock-face (caar c-record-ref-identifiers) | |
1476 | (cdar c-record-ref-identifiers) | |
1477 | 'font-lock-keyword-face)) | |
1478 | ((eq label-type 'qt-2kwds-colon) | |
1479 | (mapc | |
1480 | (lambda (kwd) | |
1481 | (c-put-font-lock-face (car kwd) (cdr kwd) | |
1482 | 'font-lock-keyword-face)) | |
1483 | c-record-ref-identifiers))) | |
1484 | (setq c-record-ref-identifiers nil) | |
1485 | ;; `c-forward-label' has probably added a `c-decl-end' | |
1486 | ;; marker, so return t to `c-find-decl-spots' to signal | |
1487 | ;; that. | |
1488 | t)))) | |
d9e94c22 MS |
1489 | |
1490 | nil))) | |
1491 | ||
4f9e41e4 AM |
1492 | (defun c-font-lock-enum-tail (limit) |
1493 | ;; Fontify an enum's identifiers when POINT is within the enum's brace | |
1494 | ;; block. | |
1495 | ;; | |
1496 | ;; This function will be called from font-lock for a region bounded by POINT | |
1497 | ;; and LIMIT, as though it were to identify a keyword for | |
1498 | ;; font-lock-keyword-face. It always returns NIL to inhibit this and | |
1499 | ;; prevent a repeat invocation. See elisp/lispref page "Search-based | |
1500 | ;; Fontification". | |
1501 | ;; | |
1502 | ;; Note that this function won't attempt to fontify beyond the end of the | |
1503 | ;; current enum block, if any. | |
1504 | (let* ((paren-state (c-parse-state)) | |
1505 | (encl-pos (c-most-enclosing-brace paren-state)) | |
1506 | (start (point)) | |
1507 | ) | |
1508 | (when (and | |
1509 | encl-pos | |
1510 | (eq (char-after encl-pos) ?\{) | |
1511 | (save-excursion | |
1512 | (goto-char encl-pos) | |
1513 | (c-backward-syntactic-ws) | |
1514 | (c-simple-skip-symbol-backward) | |
1515 | (or (looking-at c-brace-list-key) ; "enum" | |
1516 | (progn (c-backward-syntactic-ws) | |
1517 | (c-simple-skip-symbol-backward) | |
1518 | (looking-at c-brace-list-key))))) | |
1519 | (c-syntactic-skip-backward "^{," nil t) | |
1520 | (c-put-char-property (1- (point)) 'c-type 'c-decl-id-start) | |
1521 | ||
1522 | (c-forward-syntactic-ws) | |
1523 | (c-font-lock-declarators limit t nil))) | |
1524 | nil) | |
1525 | ||
bf2c1571 AM |
1526 | (defun c-font-lock-enclosing-decls (limit) |
1527 | ;; Fontify the declarators of (nested) declarations we're in the middle of. | |
1528 | ;; This is mainly for when a jit-lock etc. chunk starts inside the brace | |
1529 | ;; block of a struct/union/class, etc. | |
7d0ee75c | 1530 | ;; |
bf2c1571 AM |
1531 | ;; This function will be called from font-lock for a region bounded by POINT |
1532 | ;; and LIMIT, as though it were to identify a keyword for | |
1533 | ;; font-lock-keyword-face. It always returns NIL to inhibit this and | |
1534 | ;; prevent a repeat invocation. See elisp/lispref page "Search-based | |
1535 | ;; Fontification". | |
1536 | (let* ((paren-state (c-parse-state)) | |
1537 | (start (point)) | |
1538 | decl-context bo-decl in-typedef type-type ps-elt) | |
1539 | ||
1540 | ;; First, are we actually in a "local" declaration? | |
1541 | (setq decl-context (c-beginning-of-decl-1) | |
1542 | bo-decl (point) | |
1543 | in-typedef (looking-at c-typedef-key)) | |
1544 | (if in-typedef (c-forward-token-2)) | |
1545 | (when (and (eq (car decl-context) 'same) | |
1546 | (< bo-decl start)) | |
1547 | ;; Are we genuinely at a type? | |
1548 | (setq type-type (c-forward-type t)) | |
1549 | (if (and type-type | |
1550 | (or (not (eq type-type 'maybe)) | |
1551 | (looking-at c-symbol-key))) | |
1552 | (c-font-lock-declarators limit t in-typedef))) | |
1553 | ||
1554 | ;; Secondly, are we in any nested struct/union/class/etc. braces? | |
1555 | (while paren-state | |
1556 | (setq ps-elt (car paren-state) | |
1557 | paren-state (cdr paren-state)) | |
1558 | (when (and (atom ps-elt) | |
1559 | (eq (char-after ps-elt) ?\{)) | |
1560 | (goto-char ps-elt) | |
1561 | (setq decl-context (c-beginning-of-decl-1) | |
1562 | in-typedef (looking-at c-typedef-key)) | |
1563 | (if in-typedef (c-forward-token-2)) | |
ff7271b9 OT |
1564 | (when (and c-opt-block-decls-with-vars-key |
1565 | (looking-at c-opt-block-decls-with-vars-key)) | |
bf2c1571 AM |
1566 | (goto-char ps-elt) |
1567 | (when (c-safe (c-forward-sexp)) | |
1568 | (c-forward-syntactic-ws) | |
1569 | (c-font-lock-declarators limit t in-typedef))))))) | |
7d0ee75c | 1570 | |
d9e94c22 MS |
1571 | (c-lang-defconst c-simple-decl-matchers |
1572 | "Simple font lock matchers for types and declarations. These are used | |
1573 | on level 2 only and so aren't combined with `c-complex-decl-matchers'." | |
1574 | ||
1575 | t `(;; Objective-C methods. | |
1576 | ,@(when (c-major-mode-is 'objc-mode) | |
1577 | `((,(c-lang-const c-opt-method-key) | |
1578 | (,(byte-compile | |
1579 | (lambda (limit) | |
1580 | (let (;; The font-lock package in Emacs is known to clobber | |
1581 | ;; `parse-sexp-lookup-properties' (when it exists). | |
1582 | (parse-sexp-lookup-properties | |
1583 | (cc-eval-when-compile | |
1584 | (boundp 'parse-sexp-lookup-properties)))) | |
1585 | (save-restriction | |
1586 | (narrow-to-region (point-min) limit) | |
1587 | (c-font-lock-objc-method))) | |
1588 | nil)) | |
1589 | (goto-char (match-end 1)))))) | |
1590 | ||
1591 | ;; Fontify all type names and the identifiers in the | |
1592 | ;; declarations they might start. Use eval here since | |
1593 | ;; `c-known-type-key' gets its value from | |
1594 | ;; `*-font-lock-extra-types' on mode init. | |
1595 | (eval . (list ,(c-make-font-lock-search-function | |
1596 | 'c-known-type-key | |
1597 | '(1 'font-lock-type-face t) | |
1598 | '((c-font-lock-declarators limit t nil) | |
1599 | (save-match-data | |
1600 | (goto-char (match-end 1)) | |
1601 | (c-forward-syntactic-ws)) | |
1602 | (goto-char (match-end 1)))))) | |
1603 | ||
1604 | ;; Fontify types preceded by `c-type-prefix-kwds' and the | |
1605 | ;; identifiers in the declarations they might start. | |
1606 | ,@(when (c-lang-const c-type-prefix-kwds) | |
0386b551 AM |
1607 | (let* ((prefix-re (c-make-keywords-re nil |
1608 | (c-lang-const c-type-prefix-kwds))) | |
1609 | (type-match (+ 2 | |
1610 | (regexp-opt-depth prefix-re) | |
1611 | (c-lang-const c-simple-ws-depth)))) | |
d9e94c22 | 1612 | `((,(c-make-font-lock-search-function |
0386b551 AM |
1613 | (concat "\\<\\(" prefix-re "\\)" ; 1 |
1614 | (c-lang-const c-simple-ws) "+" | |
1615 | (concat "\\(" ; 2 + prefix-re + c-simple-ws | |
1616 | (c-lang-const c-symbol-key) | |
1617 | "\\)")) | |
1618 | `(,type-match | |
d9e94c22 | 1619 | 'font-lock-type-face t) |
0386b551 | 1620 | `((c-font-lock-declarators limit t nil) |
d9e94c22 | 1621 | (save-match-data |
0386b551 | 1622 | (goto-char (match-end ,type-match)) |
d9e94c22 | 1623 | (c-forward-syntactic-ws)) |
0386b551 | 1624 | (goto-char (match-end ,type-match)))))))) |
d9e94c22 MS |
1625 | |
1626 | ;; Fontify special declarations that lacks a type. | |
1627 | ,@(when (c-lang-const c-typeless-decl-kwds) | |
1628 | `((,(c-make-font-lock-search-function | |
1629 | (concat "\\<\\(" | |
0386b551 | 1630 | (regexp-opt (c-lang-const c-typeless-decl-kwds)) |
d9e94c22 MS |
1631 | "\\)\\>") |
1632 | '((c-font-lock-declarators limit t nil) | |
1633 | (save-match-data | |
1634 | (goto-char (match-end 1)) | |
1635 | (c-forward-syntactic-ws)) | |
1636 | (goto-char (match-end 1))))))) | |
0386b551 AM |
1637 | |
1638 | ;; Fontify generic colon labels in languages that support them. | |
1639 | ,@(when (c-lang-const c-recognize-colon-labels) | |
1640 | `(c-font-lock-labels)))) | |
d9e94c22 MS |
1641 | |
1642 | (c-lang-defconst c-complex-decl-matchers | |
1643 | "Complex font lock matchers for types and declarations. Used on level | |
1644 | 3 and higher." | |
1645 | ||
e15f8aaa | 1646 | ;; Note: This code in this form dumps a number of functions into the |
1379f2c5 AM |
1647 | ;; resulting constant, `c-matchers-3'. At run time, font lock will call |
1648 | ;; each of them as a "FUNCTION" (see Elisp page "Search-based | |
1649 | ;; Fontification"). The font lock region is delimited by POINT and the | |
1650 | ;; single parameter, LIMIT. Each of these functions returns NIL (thus | |
1651 | ;; inhibiting spurious font-lock-keyword-face highlighting and another | |
1652 | ;; call). | |
1653 | ||
d9e94c22 MS |
1654 | t `(;; Initialize some things before the search functions below. |
1655 | c-font-lock-complex-decl-prepare | |
1656 | ||
d9e94c22 MS |
1657 | ,@(if (c-major-mode-is 'objc-mode) |
1658 | ;; Fontify method declarations in Objective-C, but first | |
1659 | ;; we have to put the `c-decl-end' `c-type' property on | |
1660 | ;; all the @-style directives that haven't been handled in | |
1661 | ;; `c-basic-matchers-before'. | |
1662 | `(,(c-make-font-lock-search-function | |
1663 | (c-make-keywords-re t | |
1664 | ;; Exclude "@class" since that directive ends with a | |
1665 | ;; semicolon anyway. | |
1666 | (delete "@class" | |
1667 | (append (c-lang-const c-protection-kwds) | |
1668 | (c-lang-const c-other-decl-kwds) | |
1669 | nil))) | |
1670 | '((c-put-char-property (1- (match-end 1)) | |
1671 | 'c-type 'c-decl-end))) | |
0386b551 | 1672 | c-font-lock-objc-methods)) |
d9e94c22 | 1673 | |
0386b551 | 1674 | ;; Fontify all declarations, casts and normal labels. |
d9e94c22 MS |
1675 | c-font-lock-declarations |
1676 | ||
bf2c1571 AM |
1677 | ;; Fontify declarators when POINT is within their declaration. |
1678 | c-font-lock-enclosing-decls | |
1679 | ||
0386b551 AM |
1680 | ;; Fontify angle bracket arglists like templates in C++. |
1681 | ,@(when (c-lang-const c-recognize-<>-arglists) | |
1682 | `(c-font-lock-<>-arglists)) | |
1683 | ||
5a89f0a7 | 1684 | ;; The first two rules here mostly find occurrences that |
d9e94c22 MS |
1685 | ;; `c-font-lock-declarations' has found already, but not |
1686 | ;; declarations containing blocks in the type (see note below). | |
1687 | ;; It's also useful to fontify these everywhere to show e.g. when | |
1688 | ;; a type keyword is accidentally used as an identifier. | |
1689 | ||
1690 | ;; Fontify basic types. | |
1691 | ,(let ((re (c-make-keywords-re nil | |
1692 | (c-lang-const c-primitive-type-kwds)))) | |
1693 | (if (c-major-mode-is 'pike-mode) | |
1694 | ;; No symbol is a keyword after "->" in Pike. | |
0386b551 | 1695 | `(,(concat "\\(\\=.?\\|[^>]\\|[^-]>\\)" |
d9e94c22 | 1696 | "\\<\\(" re "\\)\\>") |
0386b551 | 1697 | 2 font-lock-type-face) |
d9e94c22 MS |
1698 | `(,(concat "\\<\\(" re "\\)\\>") |
1699 | 1 'font-lock-type-face))) | |
1700 | ||
e15f8aaa | 1701 | ;; Fontify types preceded by `c-type-prefix-kwds' (e.g. "struct"). |
d9e94c22 MS |
1702 | ,@(when (c-lang-const c-type-prefix-kwds) |
1703 | `((,(byte-compile | |
1704 | `(lambda (limit) | |
1705 | (c-fontify-types-and-refs | |
1706 | ((c-promote-possible-types t) | |
1707 | ;; The font-lock package in Emacs is known to clobber | |
1708 | ;; `parse-sexp-lookup-properties' (when it exists). | |
1709 | (parse-sexp-lookup-properties | |
1710 | (cc-eval-when-compile | |
1711 | (boundp 'parse-sexp-lookup-properties)))) | |
1712 | (save-restriction | |
1713 | ;; Narrow to avoid going past the limit in | |
1714 | ;; `c-forward-type'. | |
1715 | (narrow-to-region (point) limit) | |
1716 | (while (re-search-forward | |
1717 | ,(concat "\\<\\(" | |
1718 | (c-make-keywords-re nil | |
1719 | (c-lang-const c-type-prefix-kwds)) | |
1720 | "\\)\\>") | |
1721 | limit t) | |
1722 | (unless (c-skip-comments-and-strings limit) | |
1723 | (c-forward-syntactic-ws) | |
1724 | ;; Handle prefix declaration specifiers. | |
e3715033 AM |
1725 | (when (or (looking-at c-prefix-spec-kwds-re) |
1726 | (and (c-major-mode-is 'java-mode) | |
1727 | (looking-at "@[A-Za-z0-9]+"))) | |
0386b551 | 1728 | (c-forward-keyword-clause 1)) |
d9e94c22 MS |
1729 | ,(if (c-major-mode-is 'c++-mode) |
1730 | `(when (and (c-forward-type) | |
1731 | (eq (char-after) ?=)) | |
1732 | ;; In C++ we additionally check for a "class | |
1733 | ;; X = Y" construct which is used in | |
1734 | ;; templates, to fontify Y as a type. | |
1735 | (forward-char) | |
1736 | (c-forward-syntactic-ws) | |
1737 | (c-forward-type)) | |
1738 | `(c-forward-type)) | |
1739 | ))))))))) | |
1740 | ||
1741 | ;; Fontify symbols after closing braces as declaration | |
1742 | ;; identifiers under the assumption that they are part of | |
1743 | ;; declarations like "class Foo { ... } foo;". It's too | |
1744 | ;; expensive to check this accurately by skipping past the | |
1745 | ;; brace block, so we use the heuristic that it's such a | |
1746 | ;; declaration if the first identifier is on the same line as | |
1747 | ;; the closing brace. `c-font-lock-declarations' will later | |
1748 | ;; override it if it turns out to be an new declaration, but | |
1749 | ;; it will be wrong if it's an expression (see the test | |
1750 | ;; decls-8.cc). | |
e15f8aaa AM |
1751 | ;; ,@(when (c-lang-const c-opt-block-decls-with-vars-key) |
1752 | ;; `((,(c-make-font-lock-search-function | |
1753 | ;; (concat "}" | |
1754 | ;; (c-lang-const c-single-line-syntactic-ws) | |
1755 | ;; "\\(" ; 1 + c-single-line-syntactic-ws-depth | |
1756 | ;; (c-lang-const c-type-decl-prefix-key) | |
1757 | ;; "\\|" | |
1758 | ;; (c-lang-const c-symbol-key) | |
1759 | ;; "\\)") | |
1760 | ;; `((c-font-lock-declarators limit t nil) ; That `nil' says use `font-lock-variable-name-face'; | |
1761 | ;; ; `t' would mean `font-lock-function-name-face'. | |
1762 | ;; (progn | |
1763 | ;; (c-put-char-property (match-beginning 0) 'c-type | |
1764 | ;; 'c-decl-id-start) | |
1765 | ;; ; 'c-decl-type-start) | |
1766 | ;; (goto-char (match-beginning | |
1767 | ;; ,(1+ (c-lang-const | |
1768 | ;; c-single-line-syntactic-ws-depth))))) | |
1769 | ;; (goto-char (match-end 0))))))) | |
d9e94c22 MS |
1770 | |
1771 | ;; Fontify the type in C++ "new" expressions. | |
1772 | ,@(when (c-major-mode-is 'c++-mode) | |
1379f2c5 AM |
1773 | ;; This pattern is a probably a "(MATCHER . ANCHORED-HIGHLIGHTER)" |
1774 | ;; (see Elisp page "Search-based Fontification"). | |
d9e94c22 MS |
1775 | `(("\\<new\\>" |
1776 | (c-font-lock-c++-new)))) | |
1777 | )) | |
1778 | ||
1779 | (defun c-font-lock-labels (limit) | |
0386b551 | 1780 | ;; Fontify all statement labels from the point to LIMIT. Assumes |
d9e94c22 MS |
1781 | ;; that strings and comments have been fontified already. Nil is |
1782 | ;; always returned. | |
1783 | ;; | |
0386b551 AM |
1784 | ;; Note: This function is only used on decoration level 2; this is |
1785 | ;; taken care of directly by the gargantuan | |
1786 | ;; `c-font-lock-declarations' on higher levels. | |
1787 | ;; | |
1788 | ;; This function might do hidden buffer changes. | |
d9e94c22 MS |
1789 | |
1790 | (let (continue-pos id-start | |
1791 | ;; The font-lock package in Emacs is known to clobber | |
1792 | ;; `parse-sexp-lookup-properties' (when it exists). | |
1793 | (parse-sexp-lookup-properties | |
1794 | (cc-eval-when-compile | |
1795 | (boundp 'parse-sexp-lookup-properties)))) | |
1796 | ||
1797 | (while (re-search-forward ":[^:]" limit t) | |
1798 | (setq continue-pos (point)) | |
1799 | (goto-char (match-beginning 0)) | |
1800 | (unless (c-skip-comments-and-strings limit) | |
1801 | ||
1802 | (c-backward-syntactic-ws) | |
1803 | (and (setq id-start (c-on-identifier)) | |
1804 | ||
1805 | (not (get-text-property id-start 'face)) | |
1806 | ||
1807 | (progn | |
1808 | (goto-char id-start) | |
1809 | (c-backward-syntactic-ws) | |
1810 | (or | |
1811 | ;; Check for a char that precedes a statement. | |
1812 | (memq (char-before) '(?\} ?\{ ?\;)) | |
1813 | ;; Check for a preceding label. We exploit the font | |
1814 | ;; locking made earlier by this function. | |
1815 | (and (eq (char-before) ?:) | |
1816 | (progn | |
1817 | (backward-char) | |
1818 | (c-backward-syntactic-ws) | |
1819 | (not (bobp))) | |
1820 | (eq (get-text-property (1- (point)) 'face) | |
1821 | c-label-face-name)) | |
1822 | ;; Check for a keyword that precedes a statement. | |
1823 | (c-after-conditional))) | |
1824 | ||
1825 | (progn | |
1826 | ;; Got a label. | |
1827 | (goto-char id-start) | |
1828 | (looking-at c-symbol-key) | |
1829 | (c-put-font-lock-face (match-beginning 0) (match-end 0) | |
1830 | c-label-face-name))) | |
1831 | ||
1832 | (goto-char continue-pos)))) | |
1833 | nil) | |
1834 | ||
1835 | (c-lang-defconst c-basic-matchers-after | |
1836 | "Font lock matchers for various things that should be fontified after | |
1837 | generic casts and declarations are fontified. Used on level 2 and | |
1838 | higher." | |
1839 | ||
4f9e41e4 AM |
1840 | t `(,@(when (c-lang-const c-brace-id-list-kwds) |
1841 | ;; Fontify the remaining identifiers inside an enum list when we start | |
1842 | ;; inside it. | |
1843 | `(c-font-lock-enum-tail | |
1844 | ;; Fontify the identifiers inside enum lists. (The enum type | |
d9e94c22 MS |
1845 | ;; name is handled by `c-simple-decl-matchers' or |
1846 | ;; `c-complex-decl-matchers' below. | |
4f9e41e4 | 1847 | (,(c-make-font-lock-search-function |
d9e94c22 MS |
1848 | (concat |
1849 | "\\<\\(" | |
1850 | (c-make-keywords-re nil (c-lang-const c-brace-id-list-kwds)) | |
1851 | "\\)\\>" | |
1852 | ;; Disallow various common punctuation chars that can't come | |
1853 | ;; before the '{' of the enum list, to avoid searching too far. | |
1854 | "[^\]\[{}();,/#=]*" | |
1855 | "{") | |
1856 | '((c-font-lock-declarators limit t nil) | |
1857 | (save-match-data | |
1858 | (goto-char (match-end 0)) | |
1859 | (c-put-char-property (1- (point)) 'c-type | |
1860 | 'c-decl-id-start) | |
1861 | (c-forward-syntactic-ws)) | |
1862 | (goto-char (match-end 0))))))) | |
1863 | ||
0386b551 AM |
1864 | ;; Fontify labels after goto etc. |
1865 | ,@(when (c-lang-const c-before-label-kwds) | |
1866 | `(;; (Got three different interpretation levels here, | |
d9e94c22 MS |
1867 | ;; which makes it a bit complicated: 1) The backquote |
1868 | ;; stuff is expanded when compiled or loaded, 2) the | |
1869 | ;; eval form is evaluated at font-lock setup (to | |
1870 | ;; substitute c-label-face-name correctly), and 3) the | |
1871 | ;; resulting structure is interpreted during | |
1872 | ;; fontification.) | |
1873 | (eval | |
1874 | . ,(let* ((c-before-label-re | |
1875 | (c-make-keywords-re nil | |
1876 | (c-lang-const c-before-label-kwds)))) | |
1877 | `(list | |
1878 | ,(concat "\\<\\(" c-before-label-re "\\)\\>" | |
1879 | "\\s *" | |
1880 | "\\(" ; identifier-offset | |
1881 | (c-lang-const c-symbol-key) | |
1882 | "\\)") | |
0386b551 AM |
1883 | (list ,(+ (regexp-opt-depth c-before-label-re) 2) |
1884 | c-label-face-name nil t)))))) | |
d9e94c22 MS |
1885 | |
1886 | ;; Fontify the clauses after various keywords. | |
ef8cdf8c AM |
1887 | ,@(when (or (c-lang-const c-type-list-kwds) |
1888 | (c-lang-const c-ref-list-kwds) | |
1889 | (c-lang-const c-colon-type-list-kwds)) | |
1890 | `((,(c-make-font-lock-BO-decl-search-function | |
1891 | (concat "\\<\\(" | |
1892 | (c-make-keywords-re nil | |
1893 | (append (c-lang-const c-type-list-kwds) | |
1894 | (c-lang-const c-ref-list-kwds) | |
1895 | (c-lang-const c-colon-type-list-kwds))) | |
1896 | "\\)\\>") | |
1897 | '((c-fontify-types-and-refs ((c-promote-possible-types t)) | |
1898 | (c-forward-keyword-clause 1) | |
1899 | (if (> (point) limit) (goto-char limit)))))))) | |
1900 | ||
1901 | ,@(when (c-lang-const c-paren-type-kwds) | |
1902 | `((,(c-make-font-lock-search-function | |
1903 | (concat "\\<\\(" | |
1904 | (c-make-keywords-re nil | |
1905 | (c-lang-const c-paren-type-kwds)) | |
1906 | "\\)\\>") | |
1907 | '((c-fontify-types-and-refs ((c-promote-possible-types t)) | |
1908 | (c-forward-keyword-clause 1) | |
1909 | (if (> (point) limit) (goto-char limit)))))))) | |
1910 | ||
1911 | ,@(when (c-major-mode-is 'java-mode) | |
1912 | `((eval . (list "\\<\\(@[a-zA-Z0-9]+\\)\\>" 1 c-annotation-face)))) | |
d9e94c22 MS |
1913 | )) |
1914 | ||
1915 | (c-lang-defconst c-matchers-1 | |
1916 | t (c-lang-const c-cpp-matchers)) | |
1917 | ||
1918 | (c-lang-defconst c-matchers-2 | |
1919 | t (append (c-lang-const c-matchers-1) | |
1920 | (c-lang-const c-basic-matchers-before) | |
1921 | (c-lang-const c-simple-decl-matchers) | |
1922 | (c-lang-const c-basic-matchers-after))) | |
1923 | ||
1924 | (c-lang-defconst c-matchers-3 | |
1925 | t (append (c-lang-const c-matchers-1) | |
1926 | (c-lang-const c-basic-matchers-before) | |
1927 | (c-lang-const c-complex-decl-matchers) | |
1928 | (c-lang-const c-basic-matchers-after))) | |
1929 | ||
1930 | (defun c-compose-keywords-list (base-list) | |
1931 | ;; Incorporate the font lock keyword lists according to | |
1932 | ;; `c-doc-comment-style' on the given keyword list and return it. | |
1933 | ;; This is used in the function bindings of the | |
1934 | ;; `*-font-lock-keywords-*' symbols since we have to build the list | |
1935 | ;; when font-lock is initialized. | |
1936 | ||
1937 | (unless (memq c-doc-face-name c-literal-faces) | |
1938 | (setq c-literal-faces (cons c-doc-face-name c-literal-faces))) | |
1939 | ||
1940 | (let* ((doc-keywords | |
1941 | (if (consp (car-safe c-doc-comment-style)) | |
1942 | (cdr-safe (or (assq c-buffer-is-cc-mode c-doc-comment-style) | |
1943 | (assq 'other c-doc-comment-style))) | |
1944 | c-doc-comment-style)) | |
1945 | (list (nconc (apply 'nconc | |
1946 | (mapcar | |
1947 | (lambda (doc-style) | |
1948 | (let ((sym (intern | |
1949 | (concat (symbol-name doc-style) | |
1950 | "-font-lock-keywords")))) | |
1951 | (cond ((fboundp sym) | |
1952 | (funcall sym)) | |
1953 | ((boundp sym) | |
1954 | (append (eval sym) nil))))) | |
1955 | (if (listp doc-keywords) | |
1956 | doc-keywords | |
1957 | (list doc-keywords)))) | |
1958 | base-list))) | |
1959 | ||
1960 | ;; Kludge: If `c-font-lock-complex-decl-prepare' is on the list we | |
1961 | ;; move it first since the doc comment font lockers might add | |
7bfc3fdb | 1962 | ;; `c-type' text properties, so they have to be cleared before that. |
d9e94c22 MS |
1963 | (when (memq 'c-font-lock-complex-decl-prepare list) |
1964 | (setq list (cons 'c-font-lock-complex-decl-prepare | |
1965 | (delq 'c-font-lock-complex-decl-prepare | |
1966 | (append list nil))))) | |
1967 | ||
1968 | list)) | |
1969 | ||
1970 | (defun c-override-default-keywords (def-var) | |
1971 | ;; This is used to override the value on a `*-font-lock-keywords' | |
1972 | ;; variable only if it's nil or has the same value as one of the | |
1973 | ;; `*-font-lock-keywords-*' variables. Older font-lock packages | |
1974 | ;; define a default value for `*-font-lock-keywords' which we want | |
1975 | ;; to override, but we should otoh avoid clobbering a user setting. | |
1976 | ;; This heuristic for that isn't perfect, but I can't think of any | |
1977 | ;; better. /mast | |
d9e94c22 MS |
1978 | (when (and (boundp def-var) |
1979 | (memq (symbol-value def-var) | |
1980 | (cons nil | |
1981 | (mapcar | |
1982 | (lambda (suffix) | |
1983 | (let ((sym (intern (concat (symbol-name def-var) | |
1984 | suffix)))) | |
1985 | (and (boundp sym) (symbol-value sym)))) | |
1986 | '("-1" "-2" "-3"))))) | |
1987 | ;; The overriding is done by unbinding the variable so that the normal | |
1988 | ;; defvar will install its default value later on. | |
1989 | (makunbound def-var))) | |
1990 | ||
1991 | \f | |
1992 | ;;; C. | |
1993 | ||
1994 | (c-override-default-keywords 'c-font-lock-keywords) | |
1995 | ||
1996 | (defconst c-font-lock-keywords-1 (c-lang-const c-matchers-1 c) | |
1997 | "Minimal font locking for C mode. | |
1998 | Fontifies only preprocessor directives (in addition to the syntactic | |
1999 | fontification of strings and comments).") | |
2000 | ||
2001 | (defconst c-font-lock-keywords-2 (c-lang-const c-matchers-2 c) | |
2002 | "Fast normal font locking for C mode. | |
2003 | In addition to `c-font-lock-keywords-1', this adds fontification of | |
2004 | keywords, simple types, declarations that are easy to recognize, the | |
2005 | user defined types on `c-font-lock-extra-types', and the doc comment | |
2006 | styles specified by `c-doc-comment-style'.") | |
2007 | ||
2008 | (defconst c-font-lock-keywords-3 (c-lang-const c-matchers-3 c) | |
2009 | "Accurate normal font locking for C mode. | |
2010 | Like `c-font-lock-keywords-2' but detects declarations in a more | |
2011 | accurate way that works in most cases for arbitrary types without the | |
2012 | need for `c-font-lock-extra-types'.") | |
2013 | ||
2014 | (defvar c-font-lock-keywords c-font-lock-keywords-3 | |
2015 | "Default expressions to highlight in C mode.") | |
2016 | ||
2017 | (defun c-font-lock-keywords-2 () | |
2018 | (c-compose-keywords-list c-font-lock-keywords-2)) | |
2019 | (defun c-font-lock-keywords-3 () | |
2020 | (c-compose-keywords-list c-font-lock-keywords-3)) | |
2021 | (defun c-font-lock-keywords () | |
2022 | (c-compose-keywords-list c-font-lock-keywords)) | |
2023 | ||
2024 | \f | |
2025 | ;;; C++. | |
2026 | ||
2027 | (defun c-font-lock-c++-new (limit) | |
e15f8aaa AM |
2028 | ;; FIXME!!! Put in a comment about the context of this function's |
2029 | ;; invocation. I think it's called as an ANCHORED-MATCHER within an | |
2030 | ;; ANCHORED-HIGHLIGHTER. (2007/2/10). | |
2031 | ;; | |
d9e94c22 MS |
2032 | ;; Assuming point is after a "new" word, check that it isn't inside |
2033 | ;; a string or comment, and if so try to fontify the type in the | |
2034 | ;; allocation expression. Nil is always returned. | |
2035 | ;; | |
2036 | ;; As usual, C++ takes the prize in coming up with a hard to parse | |
2037 | ;; syntax. :P | |
0386b551 AM |
2038 | ;; |
2039 | ;; This function might do hidden buffer changes. | |
d9e94c22 MS |
2040 | |
2041 | (unless (c-skip-comments-and-strings limit) | |
2042 | (save-excursion | |
2043 | (catch 'false-alarm | |
2044 | ;; A "new" keyword is followed by one to three expressions, where | |
2045 | ;; the type is the middle one, and the only required part. | |
2046 | (let (expr1-pos expr2-pos | |
2047 | ;; Enable recording of identifier ranges in `c-forward-type' | |
2048 | ;; etc for later fontification. Not using | |
2049 | ;; `c-fontify-types-and-refs' here since the ranges should | |
2050 | ;; be fontified selectively only when an allocation | |
2051 | ;; expression is successfully recognized. | |
2052 | (c-record-type-identifiers t) | |
2053 | c-record-ref-identifiers | |
2054 | ;; The font-lock package in Emacs is known to clobber | |
2055 | ;; `parse-sexp-lookup-properties' (when it exists). | |
2056 | (parse-sexp-lookup-properties | |
2057 | (cc-eval-when-compile | |
2058 | (boundp 'parse-sexp-lookup-properties)))) | |
2059 | (c-forward-syntactic-ws) | |
2060 | ||
2061 | ;; The first placement arglist is always parenthesized, if it | |
2062 | ;; exists. | |
2063 | (when (eq (char-after) ?\() | |
2064 | (setq expr1-pos (1+ (point))) | |
2065 | (condition-case nil | |
2066 | (c-forward-sexp) | |
2067 | (scan-error (throw 'false-alarm t))) | |
2068 | (c-forward-syntactic-ws)) | |
2069 | ||
2070 | ;; The second expression is either a type followed by some "*" or | |
2071 | ;; "[...]" or similar, or a parenthesized type followed by a full | |
2072 | ;; identifierless declarator. | |
2073 | (setq expr2-pos (1+ (point))) | |
2074 | (cond ((eq (char-after) ?\()) | |
2075 | ((let ((c-promote-possible-types t)) | |
2076 | (c-forward-type))) | |
2077 | (t (setq expr2-pos nil))) | |
2078 | ||
2079 | (when expr1-pos | |
2080 | (cond | |
2081 | ((not expr2-pos) | |
2082 | ;; No second expression, so the first has to be a | |
2083 | ;; parenthesized type. | |
2084 | (goto-char expr1-pos) | |
2085 | (let ((c-promote-possible-types t)) | |
2086 | (c-forward-type))) | |
2087 | ||
2088 | ((eq (char-before expr2-pos) ?\() | |
2089 | ;; Got two parenthesized expressions, so we have to look | |
2090 | ;; closer at them to decide which is the type. No need to | |
2091 | ;; handle `c-record-ref-identifiers' since all references | |
2092 | ;; has already been handled by other fontification rules. | |
2093 | (let (expr1-res expr2-res) | |
2094 | ||
2095 | (goto-char expr1-pos) | |
2096 | (when (setq expr1-res (c-forward-type)) | |
2097 | (unless (looking-at | |
2098 | (cc-eval-when-compile | |
2099 | (concat (c-lang-const c-symbol-start c++) | |
2100 | "\\|[*:\)\[]"))) | |
2101 | ;; There's something after the would-be type that | |
2102 | ;; can't be there, so this is a placement arglist. | |
2103 | (setq expr1-res nil))) | |
2104 | ||
2105 | (goto-char expr2-pos) | |
2106 | (when (setq expr2-res (c-forward-type)) | |
2107 | (unless (looking-at | |
2108 | (cc-eval-when-compile | |
2109 | (concat (c-lang-const c-symbol-start c++) | |
2110 | "\\|[*:\)\[]"))) | |
2111 | ;; There's something after the would-be type that can't | |
2112 | ;; be there, so this is an initialization expression. | |
2113 | (setq expr2-res nil)) | |
2114 | (when (and (c-go-up-list-forward) | |
2115 | (progn (c-forward-syntactic-ws) | |
2116 | (eq (char-after) ?\())) | |
2117 | ;; If there's a third initialization expression | |
2118 | ;; then the second one is the type, so demote the | |
2119 | ;; first match. | |
2120 | (setq expr1-res nil))) | |
2121 | ||
2122 | ;; We fontify the most likely type, with a preference for | |
2123 | ;; the first argument since a placement arglist is more | |
2124 | ;; unusual than an initializer. | |
2125 | (cond ((memq expr1-res '(t known prefix))) | |
2126 | ((memq expr2-res '(t known prefix))) | |
2127 | ((eq expr1-res 'found) | |
2128 | (let ((c-promote-possible-types t)) | |
2129 | (goto-char expr1-pos) | |
2130 | (c-forward-type))) | |
2131 | ((eq expr2-res 'found) | |
2132 | (let ((c-promote-possible-types t)) | |
2133 | (goto-char expr2-pos) | |
2134 | (c-forward-type))) | |
2135 | ((and (eq expr1-res 'maybe) (not expr2-res)) | |
2136 | (let ((c-promote-possible-types t)) | |
2137 | (goto-char expr1-pos) | |
2138 | (c-forward-type))) | |
2139 | ((and (not expr1-res) (eq expr2-res 'maybe)) | |
2140 | (let ((c-promote-possible-types t)) | |
2141 | (goto-char expr2-pos) | |
2142 | (c-forward-type))) | |
2143 | ;; If both type matches are 'maybe then we're | |
2144 | ;; too uncertain to promote either of them. | |
2145 | ))))) | |
2146 | ||
2147 | ;; Fontify the type that now is recorded in | |
2148 | ;; `c-record-type-identifiers', if any. | |
2149 | (c-fontify-recorded-types-and-refs))))) | |
2150 | nil) | |
2151 | ||
2152 | (c-override-default-keywords 'c++-font-lock-keywords) | |
2153 | ||
2154 | (defconst c++-font-lock-keywords-1 (c-lang-const c-matchers-1 c++) | |
2155 | "Minimal font locking for C++ mode. | |
2156 | Fontifies only preprocessor directives (in addition to the syntactic | |
2157 | fontification of strings and comments).") | |
2158 | ||
2159 | (defconst c++-font-lock-keywords-2 (c-lang-const c-matchers-2 c++) | |
2160 | "Fast normal font locking for C++ mode. | |
2161 | In addition to `c++-font-lock-keywords-1', this adds fontification of | |
2162 | keywords, simple types, declarations that are easy to recognize, the | |
2163 | user defined types on `c++-font-lock-extra-types', and the doc comment | |
2164 | styles specified by `c-doc-comment-style'.") | |
2165 | ||
2166 | (defconst c++-font-lock-keywords-3 (c-lang-const c-matchers-3 c++) | |
2167 | "Accurate normal font locking for C++ mode. | |
2168 | Like `c++-font-lock-keywords-2' but detects declarations in a more | |
2169 | accurate way that works in most cases for arbitrary types without the | |
2170 | need for `c++-font-lock-extra-types'.") | |
2171 | ||
2172 | (defvar c++-font-lock-keywords c++-font-lock-keywords-3 | |
2173 | "Default expressions to highlight in C++ mode.") | |
2174 | ||
2175 | (defun c++-font-lock-keywords-2 () | |
2176 | (c-compose-keywords-list c++-font-lock-keywords-2)) | |
2177 | (defun c++-font-lock-keywords-3 () | |
2178 | (c-compose-keywords-list c++-font-lock-keywords-3)) | |
2179 | (defun c++-font-lock-keywords () | |
2180 | (c-compose-keywords-list c++-font-lock-keywords)) | |
2181 | ||
2182 | \f | |
2183 | ;;; Objective-C. | |
2184 | ||
d9e94c22 MS |
2185 | (defun c-font-lock-objc-method () |
2186 | ;; Assuming the point is after the + or - that starts an Objective-C | |
2187 | ;; method declaration, fontify it. This must be done before normal | |
2188 | ;; casts, declarations and labels are fontified since they will get | |
2189 | ;; false matches in these things. | |
0386b551 AM |
2190 | ;; |
2191 | ;; This function might do hidden buffer changes. | |
d9e94c22 MS |
2192 | |
2193 | (c-fontify-types-and-refs | |
2194 | ((first t) | |
2195 | (c-promote-possible-types t)) | |
2196 | ||
2197 | (while (and | |
2198 | (progn | |
2199 | (c-forward-syntactic-ws) | |
2200 | ||
2201 | ;; An optional method type. | |
2202 | (if (eq (char-after) ?\() | |
2203 | (progn | |
2204 | (forward-char) | |
2205 | (c-forward-syntactic-ws) | |
2206 | (c-forward-type) | |
2207 | (prog1 (c-go-up-list-forward) | |
2208 | (c-forward-syntactic-ws))) | |
2209 | t)) | |
2210 | ||
2211 | ;; The name. The first time it's the first part of | |
2212 | ;; the function name, the rest of the time it's an | |
2213 | ;; argument name. | |
2214 | (looking-at c-symbol-key) | |
2215 | (progn | |
2216 | (goto-char (match-end 0)) | |
2217 | (c-put-font-lock-face (match-beginning 0) | |
2218 | (point) | |
2219 | (if first | |
2220 | 'font-lock-function-name-face | |
2221 | 'font-lock-variable-name-face)) | |
2222 | (c-forward-syntactic-ws) | |
2223 | ||
2224 | ;; Another optional part of the function name. | |
2225 | (when (looking-at c-symbol-key) | |
2226 | (goto-char (match-end 0)) | |
2227 | (c-put-font-lock-face (match-beginning 0) | |
2228 | (point) | |
2229 | 'font-lock-function-name-face) | |
2230 | (c-forward-syntactic-ws)) | |
2231 | ||
2232 | ;; There's another argument if a colon follows. | |
2233 | (eq (char-after) ?:))) | |
2234 | (forward-char) | |
2235 | (setq first nil)))) | |
2236 | ||
2237 | (defun c-font-lock-objc-methods (limit) | |
2238 | ;; Fontify method declarations in Objective-C. Nil is always | |
2239 | ;; returned. | |
0386b551 AM |
2240 | ;; |
2241 | ;; This function might do hidden buffer changes. | |
d9e94c22 MS |
2242 | |
2243 | (let (;; The font-lock package in Emacs is known to clobber | |
2244 | ;; `parse-sexp-lookup-properties' (when it exists). | |
2245 | (parse-sexp-lookup-properties | |
2246 | (cc-eval-when-compile | |
2247 | (boundp 'parse-sexp-lookup-properties)))) | |
2248 | ||
2249 | (c-find-decl-spots | |
2250 | limit | |
2251 | "[-+]" | |
2252 | nil | |
2253 | (lambda (match-pos inside-macro) | |
2254 | (forward-char) | |
2255 | (c-font-lock-objc-method)))) | |
2256 | nil) | |
2257 | ||
2258 | (c-override-default-keywords 'objc-font-lock-keywords) | |
2259 | ||
2260 | (defconst objc-font-lock-keywords-1 (c-lang-const c-matchers-1 objc) | |
2261 | "Minimal font locking for Objective-C mode. | |
2262 | Fontifies only compiler directives (in addition to the syntactic | |
2263 | fontification of strings and comments).") | |
2264 | ||
2265 | (defconst objc-font-lock-keywords-2 (c-lang-const c-matchers-2 objc) | |
2266 | "Fast normal font locking for Objective-C mode. | |
2267 | In addition to `objc-font-lock-keywords-1', this adds fontification of | |
2268 | keywords, simple types, declarations that are easy to recognize, the | |
2269 | user defined types on `objc-font-lock-extra-types', and the doc | |
2270 | comment styles specified by `c-doc-comment-style'.") | |
2271 | ||
2272 | (defconst objc-font-lock-keywords-3 (c-lang-const c-matchers-3 objc) | |
2273 | "Accurate normal font locking for Objective-C mode. | |
2274 | Like `objc-font-lock-keywords-2' but detects declarations in a more | |
2275 | accurate way that works in most cases for arbitrary types without the | |
2276 | need for `objc-font-lock-extra-types'.") | |
2277 | ||
2278 | (defvar objc-font-lock-keywords objc-font-lock-keywords-3 | |
2279 | "Default expressions to highlight in Objective-C mode.") | |
2280 | ||
2281 | (defun objc-font-lock-keywords-2 () | |
2282 | (c-compose-keywords-list objc-font-lock-keywords-2)) | |
2283 | (defun objc-font-lock-keywords-3 () | |
2284 | (c-compose-keywords-list objc-font-lock-keywords-3)) | |
2285 | (defun objc-font-lock-keywords () | |
2286 | (c-compose-keywords-list objc-font-lock-keywords)) | |
2287 | ||
2288 | ;; Kludge to override the default value that | |
2289 | ;; `objc-font-lock-extra-types' might have gotten from the font-lock | |
2290 | ;; package. The value replaced here isn't relevant now anyway since | |
2291 | ;; those types are builtin and therefore listed directly in | |
2292 | ;; `c-primitive-type-kwds'. | |
2293 | (when (equal (sort (append objc-font-lock-extra-types nil) 'string-lessp) | |
2294 | '("BOOL" "Class" "IMP" "SEL")) | |
2295 | (setq objc-font-lock-extra-types | |
2296 | (cc-eval-when-compile (list (concat "[" c-upper "]\\sw*"))))) | |
2297 | ||
2298 | \f | |
2299 | ;;; Java. | |
2300 | ||
2301 | (c-override-default-keywords 'java-font-lock-keywords) | |
2302 | ||
2303 | (defconst java-font-lock-keywords-1 (c-lang-const c-matchers-1 java) | |
2304 | "Minimal font locking for Java mode. | |
2305 | Fontifies nothing except the syntactic fontification of strings and | |
2306 | comments.") | |
2307 | ||
2308 | (defconst java-font-lock-keywords-2 (c-lang-const c-matchers-2 java) | |
2309 | "Fast normal font locking for Java mode. | |
2310 | In addition to `java-font-lock-keywords-1', this adds fontification of | |
2311 | keywords, simple types, declarations that are easy to recognize, the | |
2312 | user defined types on `java-font-lock-extra-types', and the doc | |
2313 | comment styles specified by `c-doc-comment-style'.") | |
2314 | ||
2315 | (defconst java-font-lock-keywords-3 (c-lang-const c-matchers-3 java) | |
2316 | "Accurate normal font locking for Java mode. | |
2317 | Like `java-font-lock-keywords-2' but detects declarations in a more | |
2318 | accurate way that works in most cases for arbitrary types without the | |
2319 | need for `java-font-lock-extra-types'.") | |
2320 | ||
2321 | (defvar java-font-lock-keywords java-font-lock-keywords-3 | |
2322 | "Default expressions to highlight in Java mode.") | |
2323 | ||
2324 | (defun java-font-lock-keywords-2 () | |
2325 | (c-compose-keywords-list java-font-lock-keywords-2)) | |
2326 | (defun java-font-lock-keywords-3 () | |
2327 | (c-compose-keywords-list java-font-lock-keywords-3)) | |
2328 | (defun java-font-lock-keywords () | |
2329 | (c-compose-keywords-list java-font-lock-keywords)) | |
2330 | ||
2331 | \f | |
2332 | ;;; CORBA IDL. | |
2333 | ||
2334 | (c-override-default-keywords 'idl-font-lock-keywords) | |
2335 | ||
2336 | (defconst idl-font-lock-keywords-1 (c-lang-const c-matchers-1 idl) | |
2337 | "Minimal font locking for CORBA IDL mode. | |
2338 | Fontifies nothing except the syntactic fontification of strings and | |
2339 | comments.") | |
2340 | ||
2341 | (defconst idl-font-lock-keywords-2 (c-lang-const c-matchers-2 idl) | |
2342 | "Fast normal font locking for CORBA IDL mode. | |
2343 | In addition to `idl-font-lock-keywords-1', this adds fontification of | |
2344 | keywords, simple types, declarations that are easy to recognize, the | |
2345 | user defined types on `idl-font-lock-extra-types', and the doc comment | |
2346 | styles specified by `c-doc-comment-style'.") | |
2347 | ||
2348 | (defconst idl-font-lock-keywords-3 (c-lang-const c-matchers-3 idl) | |
2349 | "Accurate normal font locking for CORBA IDL mode. | |
2350 | Like `idl-font-lock-keywords-2' but detects declarations in a more | |
2351 | accurate way that works in most cases for arbitrary types without the | |
2352 | need for `idl-font-lock-extra-types'.") | |
2353 | ||
2354 | (defvar idl-font-lock-keywords idl-font-lock-keywords-3 | |
2355 | "Default expressions to highlight in CORBA IDL mode.") | |
2356 | ||
2357 | (defun idl-font-lock-keywords-2 () | |
2358 | (c-compose-keywords-list idl-font-lock-keywords-2)) | |
2359 | (defun idl-font-lock-keywords-3 () | |
2360 | (c-compose-keywords-list idl-font-lock-keywords-3)) | |
2361 | (defun idl-font-lock-keywords () | |
2362 | (c-compose-keywords-list idl-font-lock-keywords)) | |
2363 | ||
2364 | \f | |
2365 | ;;; Pike. | |
2366 | ||
2367 | (c-override-default-keywords 'pike-font-lock-keywords) | |
2368 | ||
2369 | (defconst pike-font-lock-keywords-1 (c-lang-const c-matchers-1 pike) | |
2370 | "Minimal font locking for Pike mode. | |
2371 | Fontifies only preprocessor directives (in addition to the syntactic | |
2372 | fontification of strings and comments).") | |
2373 | ||
2374 | (defconst pike-font-lock-keywords-2 (c-lang-const c-matchers-2 pike) | |
2375 | "Fast normal font locking for Pike mode. | |
2376 | In addition to `pike-font-lock-keywords-1', this adds fontification of | |
2377 | keywords, simple types, declarations that are easy to recognize, the | |
2378 | user defined types on `pike-font-lock-extra-types', and the doc | |
2379 | comment styles specified by `c-doc-comment-style'.") | |
2380 | ||
2381 | (defconst pike-font-lock-keywords-3 (c-lang-const c-matchers-3 pike) | |
2382 | "Accurate normal font locking for Pike mode. | |
2383 | Like `pike-font-lock-keywords-2' but detects declarations in a more | |
2384 | accurate way that works in most cases for arbitrary types without the | |
2385 | need for `pike-font-lock-extra-types'.") | |
2386 | ||
2387 | (defvar pike-font-lock-keywords pike-font-lock-keywords-3 | |
2388 | "Default expressions to highlight in Pike mode.") | |
2389 | ||
2390 | (defun pike-font-lock-keywords-2 () | |
2391 | (c-compose-keywords-list pike-font-lock-keywords-2)) | |
2392 | (defun pike-font-lock-keywords-3 () | |
2393 | (c-compose-keywords-list pike-font-lock-keywords-3)) | |
2394 | (defun pike-font-lock-keywords () | |
2395 | (c-compose-keywords-list pike-font-lock-keywords)) | |
2396 | ||
2397 | \f | |
2398 | ;;; Doc comments. | |
2399 | ||
2400 | (defun c-font-lock-doc-comments (prefix limit keywords) | |
2401 | ;; Fontify the comments between the point and LIMIT whose start | |
2402 | ;; matches PREFIX with `c-doc-face-name'. Assumes comments have been | |
2403 | ;; fontified with `font-lock-comment-face' already. nil is always | |
2404 | ;; returned. | |
2405 | ;; | |
2406 | ;; After the fontification of a matching comment, fontification | |
2407 | ;; according to KEYWORDS is applied inside it. It's a list like | |
2408 | ;; `font-lock-keywords' except that anchored matches and eval | |
2409 | ;; clauses aren't supported and that some abbreviated forms can't be | |
2410 | ;; used. The buffer is narrowed to the comment while KEYWORDS is | |
2411 | ;; applied; leading comment starters are included but trailing | |
2412 | ;; comment enders for block comment are not. | |
2413 | ;; | |
2414 | ;; Note that faces added through KEYWORDS should never replace the | |
2415 | ;; existing `c-doc-face-name' face since the existence of that face | |
2416 | ;; is used as a flag in other code to skip comments. | |
0386b551 AM |
2417 | ;; |
2418 | ;; This function might do hidden buffer changes. | |
d9e94c22 MS |
2419 | |
2420 | (let (comment-beg region-beg) | |
2421 | (if (eq (get-text-property (point) 'face) | |
2422 | 'font-lock-comment-face) | |
2423 | ;; Handle the case when the fontified region starts inside a | |
2424 | ;; comment. | |
2425 | (let ((range (c-literal-limits))) | |
2426 | (setq region-beg (point)) | |
2427 | (when range | |
2428 | (goto-char (car range))) | |
2429 | (when (looking-at prefix) | |
2430 | (setq comment-beg (point))))) | |
2431 | ||
2432 | (while (or | |
2433 | comment-beg | |
2434 | ||
2435 | ;; Search for the prefix until a match is found at the start | |
2436 | ;; of a comment. | |
2437 | (while (when (re-search-forward prefix limit t) | |
2438 | (setq comment-beg (match-beginning 0)) | |
2439 | (or (not (c-got-face-at comment-beg | |
2440 | c-literal-faces)) | |
2441 | (and (/= comment-beg (point-min)) | |
2442 | (c-got-face-at (1- comment-beg) | |
2443 | c-literal-faces)))) | |
2444 | (setq comment-beg nil)) | |
2445 | (setq region-beg comment-beg)) | |
2446 | ||
2447 | (if (eq (elt (parse-partial-sexp comment-beg (+ comment-beg 2)) 7) t) | |
2448 | ;; Collect a sequence of doc style line comments. | |
2449 | (progn | |
2450 | (goto-char comment-beg) | |
2451 | (while (and (progn | |
2452 | (c-forward-single-comment) | |
2453 | (skip-syntax-forward " ") | |
2454 | (< (point) limit)) | |
2455 | (looking-at prefix)))) | |
2456 | (goto-char comment-beg) | |
2457 | (c-forward-single-comment)) | |
2458 | (if (> (point) limit) (goto-char limit)) | |
2459 | (setq comment-beg nil) | |
2460 | ||
2461 | (let ((region-end (point)) | |
2462 | (keylist keywords) keyword matcher highlights) | |
2463 | (c-put-font-lock-face region-beg region-end c-doc-face-name) | |
2464 | (save-restriction | |
2465 | ;; Narrow to the doc comment. Among other things, this | |
2466 | ;; helps by making "^" match at the start of the comment. | |
2467 | ;; Do not include a trailing block comment ender, though. | |
2468 | (and (> region-end (1+ region-beg)) | |
2469 | (progn (goto-char region-end) | |
2470 | (backward-char 2) | |
2471 | (looking-at "\\*/")) | |
2472 | (setq region-end (point))) | |
2473 | (narrow-to-region region-beg region-end) | |
2474 | ||
2475 | (while keylist | |
2476 | (setq keyword (car keylist) | |
2477 | keylist (cdr keylist) | |
2478 | matcher (car keyword)) | |
2479 | (goto-char region-beg) | |
2480 | (while (if (stringp matcher) | |
2481 | (re-search-forward matcher region-end t) | |
2482 | (funcall matcher region-end)) | |
2483 | (setq highlights (cdr keyword)) | |
2484 | (if (consp (car highlights)) | |
2485 | (while highlights | |
2486 | (font-lock-apply-highlight (car highlights)) | |
2487 | (setq highlights (cdr highlights))) | |
2488 | (font-lock-apply-highlight highlights)))) | |
2489 | ||
2490 | (goto-char region-end))))) | |
2491 | nil) | |
2492 | (put 'c-font-lock-doc-comments 'lisp-indent-function 2) | |
2493 | ||
2494 | (defun c-find-invalid-doc-markup (regexp limit) | |
2495 | ;; Used to fontify invalid markup in doc comments after the correct | |
5a89f0a7 | 2496 | ;; ones have been fontified: Find the first occurrence of REGEXP |
d9e94c22 MS |
2497 | ;; between the point and LIMIT that only is fontified with |
2498 | ;; `c-doc-face-name'. If a match is found then submatch 0 surrounds | |
2499 | ;; the first char and t is returned, otherwise nil is returned. | |
0386b551 AM |
2500 | ;; |
2501 | ;; This function might do hidden buffer changes. | |
d9e94c22 MS |
2502 | (let (start) |
2503 | (while (if (re-search-forward regexp limit t) | |
2504 | (not (eq (get-text-property | |
2505 | (setq start (match-beginning 0)) 'face) | |
2506 | c-doc-face-name)) | |
2507 | (setq start nil))) | |
2508 | (when start | |
2509 | (store-match-data (list (copy-marker start) | |
2510 | (copy-marker (1+ start)))) | |
2511 | t))) | |
2512 | ||
0386b551 AM |
2513 | ;; GtkDoc patterns contributed by Masatake YAMATO <jet@gyve.org>. |
2514 | ||
2515 | (defconst gtkdoc-font-lock-doc-comments | |
2516 | (let ((symbol "[a-zA-Z0-9_]+") | |
2517 | (header "^ \\* ")) | |
7d0ee75c | 2518 | `((,(concat header "\\(" symbol "\\):[ \t]*$") |
0386b551 AM |
2519 | 1 ,c-doc-markup-face-name prepend nil) |
2520 | (,(concat symbol "()") | |
2521 | 0 ,c-doc-markup-face-name prepend nil) | |
2522 | (,(concat header "\\(" "@" symbol "\\):") | |
2523 | 1 ,c-doc-markup-face-name prepend nil) | |
68a4a27a | 2524 | (,(concat "[#%@]" symbol) |
0386b551 AM |
2525 | 0 ,c-doc-markup-face-name prepend nil)) |
2526 | )) | |
2527 | ||
2528 | (defconst gtkdoc-font-lock-doc-protection | |
2529 | `(("< \\(public\\|private\\|protected\\) >" | |
2530 | 1 ,c-doc-markup-face-name prepend nil))) | |
2531 | ||
2532 | (defconst gtkdoc-font-lock-keywords | |
2533 | `((,(lambda (limit) | |
2534 | (c-font-lock-doc-comments "/\\*\\*$" limit | |
2535 | gtkdoc-font-lock-doc-comments) | |
2536 | (c-font-lock-doc-comments "/\\*< " limit | |
2537 | gtkdoc-font-lock-doc-protection) | |
2538 | )))) | |
2539 | ||
2540 | ;; Javadoc. | |
2541 | ||
7bfc3fdb MS |
2542 | (defconst javadoc-font-lock-doc-comments |
2543 | `(("{@[a-z]+[^}\n\r]*}" ; "{@foo ...}" markup. | |
2544 | 0 ,c-doc-markup-face-name prepend nil) | |
0386b551 AM |
2545 | ("^\\(/\\*\\)?\\(\\s \\|\\*\\)*\\(@[a-z]+\\)" ; "@foo ..." markup. |
2546 | 3 ,c-doc-markup-face-name prepend nil) | |
7bfc3fdb MS |
2547 | (,(concat "</?\\sw" ; HTML tags. |
2548 | "\\(" | |
2549 | (concat "\\sw\\|\\s \\|[=\n\r*.:]\\|" | |
2550 | "\"[^\"]*\"\\|'[^']*'") | |
2551 | "\\)*>") | |
2552 | 0 ,c-doc-markup-face-name prepend nil) | |
2553 | ("&\\(\\sw\\|[.:]\\)+;" ; HTML entities. | |
2554 | 0 ,c-doc-markup-face-name prepend nil) | |
2555 | ;; Fontify remaining markup characters as invalid. Note | |
2556 | ;; that the Javadoc spec is hazy about when "@" is | |
2557 | ;; allowed in non-markup use. | |
2558 | (,(lambda (limit) | |
2559 | (c-find-invalid-doc-markup "[<>&]\\|{@" limit)) | |
0386b551 | 2560 | 0 'font-lock-warning-face prepend nil))) |
7bfc3fdb MS |
2561 | |
2562 | (defconst javadoc-font-lock-keywords | |
2563 | `((,(lambda (limit) | |
2564 | (c-font-lock-doc-comments "/\\*\\*" limit | |
2565 | javadoc-font-lock-doc-comments))))) | |
d9e94c22 | 2566 | |
0386b551 AM |
2567 | ;; Pike autodoc. |
2568 | ||
d9e94c22 MS |
2569 | (defconst autodoc-decl-keywords |
2570 | ;; Adorned regexp matching the keywords that introduce declarations | |
2571 | ;; in Pike Autodoc. | |
2572 | (cc-eval-when-compile | |
2573 | (c-make-keywords-re t '("@decl" "@elem" "@index" "@member") 'pike-mode))) | |
2574 | ||
2575 | (defconst autodoc-decl-type-keywords | |
2576 | ;; Adorned regexp matching the keywords that are followed by a type. | |
2577 | (cc-eval-when-compile | |
2578 | (c-make-keywords-re t '("@elem" "@member") 'pike-mode))) | |
2579 | ||
2580 | (defun autodoc-font-lock-line-markup (limit) | |
2581 | ;; Fontify all line oriented keywords between the point and LIMIT. | |
2582 | ;; Nil is always returned. | |
0386b551 AM |
2583 | ;; |
2584 | ;; This function might do hidden buffer changes. | |
d9e94c22 MS |
2585 | |
2586 | (let ((line-re (concat "^\\(\\(/\\*!\\|\\s *\\(" | |
2587 | c-current-comment-prefix | |
2588 | "\\)\\)\\s *\\)@[A-Za-z_-]+\\(\\s \\|$\\)")) | |
2589 | (markup-faces (list c-doc-markup-face-name c-doc-face-name))) | |
2590 | ||
2591 | (while (re-search-forward line-re limit t) | |
2592 | (goto-char (match-end 1)) | |
2593 | ||
2594 | (if (looking-at autodoc-decl-keywords) | |
2595 | (let* ((kwd-pos (point)) | |
2596 | (start (match-end 1)) | |
2597 | (pos start) | |
2598 | end) | |
2599 | ||
2600 | (c-put-font-lock-face (point) pos markup-faces) | |
2601 | ||
2602 | ;; Put a declaration end mark at the markup keyword and | |
2603 | ;; remove the faces from the rest of the line so that it | |
2604 | ;; gets refontified as a declaration later on by | |
2605 | ;; `c-font-lock-declarations'. | |
2606 | (c-put-char-property (1- pos) 'c-type 'c-decl-end) | |
2607 | (goto-char pos) | |
2608 | (while (progn | |
2609 | (end-of-line) | |
2610 | (setq end (point)) | |
2611 | (and (eq (char-before) ?@) | |
2612 | (not (eobp)) | |
2613 | (progn (forward-char) | |
0386b551 | 2614 | (skip-syntax-forward " ") |
d9e94c22 MS |
2615 | (looking-at c-current-comment-prefix)))) |
2616 | (goto-char (match-end 0)) | |
2617 | (c-remove-font-lock-face pos (1- end)) | |
2618 | (c-put-font-lock-face (1- end) end markup-faces) | |
2619 | (setq pos (point))) | |
2620 | ||
2621 | ;; Include the final newline in the removed area. This | |
2622 | ;; has no visual effect but it avoids some tricky special | |
2623 | ;; cases in the testsuite wrt the differences in string | |
2624 | ;; fontification in Emacs vs XEmacs. | |
2625 | (c-remove-font-lock-face pos (min (1+ (point)) (point-max))) | |
2626 | ||
2627 | ;; Must handle string literals explicitly inside the declaration. | |
2628 | (goto-char start) | |
2629 | (while (re-search-forward | |
2630 | "\"\\([^\\\"]\\|\\\\.\\)*\"\\|'\\([^\\']\\|\\\\.\\)*'" | |
2631 | end 'move) | |
2632 | (c-put-font-lock-string-face (match-beginning 0) | |
2633 | (point))) | |
2634 | ||
2635 | ;; Fontify types after keywords that always are followed | |
2636 | ;; by them. | |
2637 | (goto-char kwd-pos) | |
2638 | (when (looking-at autodoc-decl-type-keywords) | |
2639 | (c-fontify-types-and-refs ((c-promote-possible-types t)) | |
2640 | (goto-char start) | |
2641 | (c-forward-syntactic-ws) | |
2642 | (c-forward-type)))) | |
2643 | ||
2644 | ;; Mark each whole line as markup, as long as the logical line | |
2645 | ;; continues. | |
2646 | (while (progn | |
2647 | (c-put-font-lock-face (point) | |
2648 | (progn (end-of-line) (point)) | |
2649 | markup-faces) | |
2650 | (and (eq (char-before) ?@) | |
2651 | (not (eobp)) | |
2652 | (progn (forward-char) | |
0386b551 | 2653 | (skip-syntax-forward " ") |
d9e94c22 MS |
2654 | (looking-at c-current-comment-prefix)))) |
2655 | (goto-char (match-end 0)))))) | |
2656 | ||
2657 | nil) | |
2658 | ||
7bfc3fdb MS |
2659 | (defconst autodoc-font-lock-doc-comments |
2660 | `(("@\\(\\w+{\\|\\[\\([^\]@\n\r]\\|@@\\)*\\]\\|[@}]\\|$\\)" | |
2661 | ;; In-text markup. | |
2662 | 0 ,c-doc-markup-face-name prepend nil) | |
2663 | (autodoc-font-lock-line-markup) | |
2664 | ;; Fontify remaining markup characters as invalid. | |
2665 | (,(lambda (limit) | |
2666 | (c-find-invalid-doc-markup "@" limit)) | |
0386b551 | 2667 | 0 'font-lock-warning-face prepend nil) |
7bfc3fdb MS |
2668 | )) |
2669 | ||
d9e94c22 MS |
2670 | (defun autodoc-font-lock-keywords () |
2671 | ;; Note that we depend on that `c-current-comment-prefix' has got | |
2672 | ;; its proper value here. | |
0386b551 AM |
2673 | ;; |
2674 | ;; This function might do hidden buffer changes. | |
d9e94c22 MS |
2675 | |
2676 | ;; The `c-type' text property with `c-decl-end' is used to mark the | |
2677 | ;; end of the `autodoc-decl-keywords' occurrences to fontify the | |
2678 | ;; following declarations. | |
2679 | (setq c-type-decl-end-used t) | |
2680 | ||
7bfc3fdb MS |
2681 | `((,(lambda (limit) |
2682 | (c-font-lock-doc-comments "/[*/]!" limit | |
2683 | autodoc-font-lock-doc-comments))))) | |
d9e94c22 MS |
2684 | |
2685 | \f | |
3c0ab532 | 2686 | ;; 2006-07-10: awk-font-lock-keywords has been moved back to cc-awk.el. |
d9e94c22 MS |
2687 | (cc-provide 'cc-fonts) |
2688 | ||
2689 | ;;; cc-fonts.el ends here |