* lisp/textmodes/ispell.el (ispell-word): Add to the error message
[bpt/emacs.git] / lisp / progmodes / cc-fonts.el
CommitLineData
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
AM
292 (defun c-make-font-lock-search-form (regexp highlights)
293 ;; Return a lisp form which will fontify every occurence of REGEXP
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))))
320 ;; highlight is an "ANCHORED HIGHLIGHER" of the form
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
540stuff. 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
730other easily recognizable things that should be fontified before generic
731casts 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
1043 ;; array/struct initialisation) or "=" or terminating delimiter
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 (";",
2a15eb73
MS
1106 ;; ",", a closen paren, eob etc) or to the beginning of an
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
1125 ;; Register and fontify the identifer as a type.
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.
1280 (if (and (eq (get-text-property (point) 'face)
1281 'font-lock-keyword-face)
1282 (looking-at c-not-decl-init-keywords))
1283 ;; Don't do anything more if we're looking at a keyword that
1284 ;; can't start a declaration.
1285 t
1286
e15f8aaa
AM
1287 ;; Set `context' and `c-restricted-<>-arglists'. Look for
1288 ;; "<" for the sake of C++-style template arglists.
1289 ;; Ignore "(" when it's part of a control flow construct
1290 ;; (e.g. "for (").
1291 (let ((type (and (> match-pos (point-min))
1292 (c-get-char-property (1- match-pos) 'c-type))))
1293 (cond ((not (memq (char-before match-pos) '(?\( ?, ?\[ ?<)))
1294 (setq context nil
1295 c-restricted-<>-arglists nil))
1296 ;; A control flow expression
1297 ((and (eq (char-before match-pos) ?\()
1298 (save-excursion
1299 (goto-char match-pos)
1300 (backward-char)
1301 (c-backward-token-2)
1302 (looking-at c-block-stmt-2-key)))
1303 (setq context nil
1304 c-restricted-<>-arglists t))
1305 ;; Near BOB.
1306 ((<= match-pos (point-min))
1307 (setq context 'arglist
1308 c-restricted-<>-arglists t))
1309 ;; Got a cached hit in a declaration arglist.
1310 ((eq type 'c-decl-arg-start)
1311 (setq context 'decl
1312 c-restricted-<>-arglists nil))
1313 ;; Inside an angle bracket arglist.
1314 ((or (eq type 'c-<>-arg-sep)
1315 (eq (char-before match-pos) ?<))
1316 (setq context '<>
1317 c-restricted-<>-arglists nil))
1318 ;; Got a cached hit in some other type of arglist.
1319 (type
1320 (setq context 'arglist
1321 c-restricted-<>-arglists t))
1322 ((if inside-macro
1323 (< match-pos max-type-decl-end-before-token)
1324 (< match-pos max-type-decl-end))
1325 ;; The point is within the range of a previously
1326 ;; encountered type decl expression, so the arglist
1327 ;; is probably one that contains declarations.
1328 ;; However, if `c-recognize-paren-inits' is set it
1329 ;; might also be an initializer arglist.
1330 (setq context 'decl
1331 c-restricted-<>-arglists nil)
1332 ;; The result of this check is cached with a char
1333 ;; property on the match token, so that we can look
1334 ;; it up again when refontifying single lines in a
1335 ;; multiline declaration.
1336 (c-put-char-property (1- match-pos)
1337 'c-type 'c-decl-arg-start))
1338 (t (setq context 'arglist
1339 c-restricted-<>-arglists t))))
1340
1341 ;; Check we haven't missed a preceding "typedef".
1342 (when (not (looking-at c-typedef-key))
1343 (c-backward-syntactic-ws)
1344 (c-backward-token-2)
1345 (or (looking-at c-typedef-key)
1346 (goto-char start-pos)))
1347
1348 ;; Now analyze the construct.
b248a85d
AM
1349 ;; In QT, "more" is an irritating keyword that expands to nothing.
1350 ;; We skip over it to prevent recognition of "more slots: <symbol>"
1351 ;; as a bitfield declaration.
1352 (when (and (c-major-mode-is 'c++-mode)
1353 (looking-at
1354 (concat "\\(more\\)\\([^" c-symbol-chars "]\\|$\\)")))
1355 (goto-char (match-end 1))
1356 (c-forward-syntactic-ws))
e15f8aaa 1357 (setq decl-or-cast (c-forward-decl-or-cast-1
0386b551
AM
1358 match-pos context last-cast-end))
1359
8c0e3589
AM
1360 (cond
1361 ((eq decl-or-cast 'cast)
1362 ;; Save the position after the previous cast so we can feed
1363 ;; it to `c-forward-decl-or-cast-1' in the next round. That
1364 ;; helps it discover cast chains like "(a) (b) c".
1365 (setq last-cast-end (point))
1366 (c-fontify-recorded-types-and-refs)
1367 nil)
10f5e3e6 1368
8c0e3589
AM
1369 (decl-or-cast
1370 ;; We've found a declaration.
1371
1372 ;; Set `max-type-decl-end' or `max-type-decl-end-before-token'
1373 ;; under the assumption that we're after the first type decl
1374 ;; expression in the declaration now. That's not really true;
1375 ;; we could also be after a parenthesized initializer
1376 ;; expression in C++, but this is only used as a last resort
1377 ;; to slant ambiguous expression/declarations, and overall
1378 ;; it's worth the risk to occasionally fontify an expression
1379 ;; as a declaration in an initializer expression compared to
1380 ;; getting ambiguous things in normal function prototypes
1381 ;; fontified as expressions.
1382 (if inside-macro
1383 (when (> (point) max-type-decl-end-before-token)
1384 (setq max-type-decl-end-before-token (point)))
1385 (when (> (point) max-type-decl-end)
1386 (setq max-type-decl-end (point))))
1387
1388 ;; Back up to the type to fontify the declarator(s).
1389 (goto-char (car decl-or-cast))
1390
1391 (let ((decl-list
1392 (if context
1393 ;; Should normally not fontify a list of
1394 ;; declarators inside an arglist, but the first
1395 ;; argument in the ';' separated list of a "for"
1396 ;; statement is an exception.
1397 (when (eq (char-before match-pos) ?\()
1398 (save-excursion
1399 (goto-char (1- match-pos))
1400 (c-backward-syntactic-ws)
1401 (and (c-simple-skip-symbol-backward)
1402 (looking-at c-paren-stmt-key))))
1403 t)))
1404
1405 ;; Fix the `c-decl-id-start' or `c-decl-type-start' property
1406 ;; before the first declarator if it's a list.
1407 ;; `c-font-lock-declarators' handles the rest.
1408 (when decl-list
e15f8aaa 1409 (save-excursion
8c0e3589
AM
1410 (c-backward-syntactic-ws)
1411 (unless (bobp)
1412 (c-put-char-property (1- (point)) 'c-type
1413 (if (cdr decl-or-cast)
1414 'c-decl-type-start
1415 'c-decl-id-start)))))
1416
1417 (c-font-lock-declarators
1418 (point-max) decl-list (cdr decl-or-cast)))
1419
1420 ;; A declaration has been successfully identified, so do all the
1421 ;; fontification of types and refs that've been recorded.
0386b551 1422 (c-fontify-recorded-types-and-refs)
8c0e3589
AM
1423 nil)
1424
1425 (t
1426 ;; Are we at a declarator? Try to go back to the declaration
1427 ;; to check this. If we get there, check whether a "typedef"
1428 ;; is there, then fontify the declarators accordingly.
1429 (let ((decl-search-lim (max (- (point) 50000) (point-min)))
7d0ee75c 1430 paren-state bod-res encl-pos is-typedef
8c0e3589
AM
1431 c-recognize-knr-p) ; Strictly speaking, bogus, but it
1432 ; speeds up lisp.h tremendously.
1433 (save-excursion
1434 (setq bod-res (car (c-beginning-of-decl-1 decl-search-lim)))
1435 (if (and (eq bod-res 'same)
1436 (progn
1437 (c-backward-syntactic-ws)
1438 (eq (char-before) ?\})))
1439 (c-beginning-of-decl-1 decl-search-lim))
1440
1441 ;; We're now putatively at the declaration.
1442 (setq paren-state (c-parse-state))
1443 ;; At top level or inside a "{"?
1444 (if (or (not (setq encl-pos
1445 (c-most-enclosing-brace paren-state)))
1446 (eq (char-after encl-pos) ?\{))
1447 (progn
1448 (when (looking-at c-typedef-key) ; "typedef"
1449 (setq is-typedef t)
1450 (goto-char (match-end 0))
1451 (c-forward-syntactic-ws))
1452 ;; At a real declaration?
1453 (if (memq (c-forward-type t) '(t known found))
1454 (progn
1455 (c-font-lock-declarators limit t is-typedef)
1456 nil)
1457 ;; False alarm. Return t to go on to the next check.
1458 (goto-char start-pos)
1459 t))
1460 t))))))
0386b551 1461
1379f2c5
AM
1462 ;; It was a false alarm. Check if we're in a label (or other
1463 ;; construct with `:' except bitfield) instead.
0386b551 1464 (goto-char start-pos)
1379f2c5
AM
1465 (when (setq label-type (c-forward-label t match-pos nil))
1466 ;; Can't use `c-fontify-types-and-refs' here since we
1467 ;; use the label face at times.
1468 (cond ((eq label-type 'goto-target)
1469 (c-put-font-lock-face (caar c-record-ref-identifiers)
1470 (cdar c-record-ref-identifiers)
1471 c-label-face-name))
1472 ((eq label-type 'qt-1kwd-colon)
1473 (c-put-font-lock-face (caar c-record-ref-identifiers)
1474 (cdar c-record-ref-identifiers)
1475 'font-lock-keyword-face))
1476 ((eq label-type 'qt-2kwds-colon)
1477 (mapc
1478 (lambda (kwd)
1479 (c-put-font-lock-face (car kwd) (cdr kwd)
1480 'font-lock-keyword-face))
1481 c-record-ref-identifiers)))
1482 (setq c-record-ref-identifiers nil)
1483 ;; `c-forward-label' has probably added a `c-decl-end'
1484 ;; marker, so return t to `c-find-decl-spots' to signal
1485 ;; that.
1486 t))))
d9e94c22
MS
1487
1488 nil)))
1489
4f9e41e4
AM
1490(defun c-font-lock-enum-tail (limit)
1491 ;; Fontify an enum's identifiers when POINT is within the enum's brace
1492 ;; block.
1493 ;;
1494 ;; This function will be called from font-lock for a region bounded by POINT
1495 ;; and LIMIT, as though it were to identify a keyword for
1496 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
1497 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
1498 ;; Fontification".
1499 ;;
1500 ;; Note that this function won't attempt to fontify beyond the end of the
1501 ;; current enum block, if any.
1502 (let* ((paren-state (c-parse-state))
1503 (encl-pos (c-most-enclosing-brace paren-state))
1504 (start (point))
1505 )
1506 (when (and
1507 encl-pos
1508 (eq (char-after encl-pos) ?\{)
1509 (save-excursion
1510 (goto-char encl-pos)
1511 (c-backward-syntactic-ws)
1512 (c-simple-skip-symbol-backward)
1513 (or (looking-at c-brace-list-key) ; "enum"
1514 (progn (c-backward-syntactic-ws)
1515 (c-simple-skip-symbol-backward)
1516 (looking-at c-brace-list-key)))))
1517 (c-syntactic-skip-backward "^{," nil t)
1518 (c-put-char-property (1- (point)) 'c-type 'c-decl-id-start)
1519
1520 (c-forward-syntactic-ws)
1521 (c-font-lock-declarators limit t nil)))
1522 nil)
1523
bf2c1571
AM
1524(defun c-font-lock-enclosing-decls (limit)
1525 ;; Fontify the declarators of (nested) declarations we're in the middle of.
1526 ;; This is mainly for when a jit-lock etc. chunk starts inside the brace
1527 ;; block of a struct/union/class, etc.
7d0ee75c 1528 ;;
bf2c1571
AM
1529 ;; This function will be called from font-lock for a region bounded by POINT
1530 ;; and LIMIT, as though it were to identify a keyword for
1531 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
1532 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
1533 ;; Fontification".
1534 (let* ((paren-state (c-parse-state))
1535 (start (point))
1536 decl-context bo-decl in-typedef type-type ps-elt)
1537
1538 ;; First, are we actually in a "local" declaration?
1539 (setq decl-context (c-beginning-of-decl-1)
1540 bo-decl (point)
1541 in-typedef (looking-at c-typedef-key))
1542 (if in-typedef (c-forward-token-2))
1543 (when (and (eq (car decl-context) 'same)
1544 (< bo-decl start))
1545 ;; Are we genuinely at a type?
1546 (setq type-type (c-forward-type t))
1547 (if (and type-type
1548 (or (not (eq type-type 'maybe))
1549 (looking-at c-symbol-key)))
1550 (c-font-lock-declarators limit t in-typedef)))
1551
1552 ;; Secondly, are we in any nested struct/union/class/etc. braces?
1553 (while paren-state
1554 (setq ps-elt (car paren-state)
1555 paren-state (cdr paren-state))
1556 (when (and (atom ps-elt)
1557 (eq (char-after ps-elt) ?\{))
1558 (goto-char ps-elt)
1559 (setq decl-context (c-beginning-of-decl-1)
1560 in-typedef (looking-at c-typedef-key))
1561 (if in-typedef (c-forward-token-2))
ff7271b9
OT
1562 (when (and c-opt-block-decls-with-vars-key
1563 (looking-at c-opt-block-decls-with-vars-key))
bf2c1571
AM
1564 (goto-char ps-elt)
1565 (when (c-safe (c-forward-sexp))
1566 (c-forward-syntactic-ws)
1567 (c-font-lock-declarators limit t in-typedef)))))))
7d0ee75c 1568
d9e94c22
MS
1569(c-lang-defconst c-simple-decl-matchers
1570 "Simple font lock matchers for types and declarations. These are used
1571on level 2 only and so aren't combined with `c-complex-decl-matchers'."
1572
1573 t `(;; Objective-C methods.
1574 ,@(when (c-major-mode-is 'objc-mode)
1575 `((,(c-lang-const c-opt-method-key)
1576 (,(byte-compile
1577 (lambda (limit)
1578 (let (;; The font-lock package in Emacs is known to clobber
1579 ;; `parse-sexp-lookup-properties' (when it exists).
1580 (parse-sexp-lookup-properties
1581 (cc-eval-when-compile
1582 (boundp 'parse-sexp-lookup-properties))))
1583 (save-restriction
1584 (narrow-to-region (point-min) limit)
1585 (c-font-lock-objc-method)))
1586 nil))
1587 (goto-char (match-end 1))))))
1588
1589 ;; Fontify all type names and the identifiers in the
1590 ;; declarations they might start. Use eval here since
1591 ;; `c-known-type-key' gets its value from
1592 ;; `*-font-lock-extra-types' on mode init.
1593 (eval . (list ,(c-make-font-lock-search-function
1594 'c-known-type-key
1595 '(1 'font-lock-type-face t)
1596 '((c-font-lock-declarators limit t nil)
1597 (save-match-data
1598 (goto-char (match-end 1))
1599 (c-forward-syntactic-ws))
1600 (goto-char (match-end 1))))))
1601
1602 ;; Fontify types preceded by `c-type-prefix-kwds' and the
1603 ;; identifiers in the declarations they might start.
1604 ,@(when (c-lang-const c-type-prefix-kwds)
0386b551
AM
1605 (let* ((prefix-re (c-make-keywords-re nil
1606 (c-lang-const c-type-prefix-kwds)))
1607 (type-match (+ 2
1608 (regexp-opt-depth prefix-re)
1609 (c-lang-const c-simple-ws-depth))))
d9e94c22 1610 `((,(c-make-font-lock-search-function
0386b551
AM
1611 (concat "\\<\\(" prefix-re "\\)" ; 1
1612 (c-lang-const c-simple-ws) "+"
1613 (concat "\\(" ; 2 + prefix-re + c-simple-ws
1614 (c-lang-const c-symbol-key)
1615 "\\)"))
1616 `(,type-match
d9e94c22 1617 'font-lock-type-face t)
0386b551 1618 `((c-font-lock-declarators limit t nil)
d9e94c22 1619 (save-match-data
0386b551 1620 (goto-char (match-end ,type-match))
d9e94c22 1621 (c-forward-syntactic-ws))
0386b551 1622 (goto-char (match-end ,type-match))))))))
d9e94c22
MS
1623
1624 ;; Fontify special declarations that lacks a type.
1625 ,@(when (c-lang-const c-typeless-decl-kwds)
1626 `((,(c-make-font-lock-search-function
1627 (concat "\\<\\("
0386b551 1628 (regexp-opt (c-lang-const c-typeless-decl-kwds))
d9e94c22
MS
1629 "\\)\\>")
1630 '((c-font-lock-declarators limit t nil)
1631 (save-match-data
1632 (goto-char (match-end 1))
1633 (c-forward-syntactic-ws))
1634 (goto-char (match-end 1)))))))
0386b551
AM
1635
1636 ;; Fontify generic colon labels in languages that support them.
1637 ,@(when (c-lang-const c-recognize-colon-labels)
1638 `(c-font-lock-labels))))
d9e94c22
MS
1639
1640(c-lang-defconst c-complex-decl-matchers
1641 "Complex font lock matchers for types and declarations. Used on level
16423 and higher."
1643
e15f8aaa 1644 ;; Note: This code in this form dumps a number of functions into the
1379f2c5
AM
1645 ;; resulting constant, `c-matchers-3'. At run time, font lock will call
1646 ;; each of them as a "FUNCTION" (see Elisp page "Search-based
1647 ;; Fontification"). The font lock region is delimited by POINT and the
1648 ;; single parameter, LIMIT. Each of these functions returns NIL (thus
1649 ;; inhibiting spurious font-lock-keyword-face highlighting and another
1650 ;; call).
1651
d9e94c22
MS
1652 t `(;; Initialize some things before the search functions below.
1653 c-font-lock-complex-decl-prepare
1654
d9e94c22
MS
1655 ,@(if (c-major-mode-is 'objc-mode)
1656 ;; Fontify method declarations in Objective-C, but first
1657 ;; we have to put the `c-decl-end' `c-type' property on
1658 ;; all the @-style directives that haven't been handled in
1659 ;; `c-basic-matchers-before'.
1660 `(,(c-make-font-lock-search-function
1661 (c-make-keywords-re t
1662 ;; Exclude "@class" since that directive ends with a
1663 ;; semicolon anyway.
1664 (delete "@class"
1665 (append (c-lang-const c-protection-kwds)
1666 (c-lang-const c-other-decl-kwds)
1667 nil)))
1668 '((c-put-char-property (1- (match-end 1))
1669 'c-type 'c-decl-end)))
0386b551 1670 c-font-lock-objc-methods))
d9e94c22 1671
0386b551 1672 ;; Fontify all declarations, casts and normal labels.
d9e94c22
MS
1673 c-font-lock-declarations
1674
bf2c1571
AM
1675 ;; Fontify declarators when POINT is within their declaration.
1676 c-font-lock-enclosing-decls
1677
0386b551
AM
1678 ;; Fontify angle bracket arglists like templates in C++.
1679 ,@(when (c-lang-const c-recognize-<>-arglists)
1680 `(c-font-lock-<>-arglists))
1681
5a89f0a7 1682 ;; The first two rules here mostly find occurrences that
d9e94c22
MS
1683 ;; `c-font-lock-declarations' has found already, but not
1684 ;; declarations containing blocks in the type (see note below).
1685 ;; It's also useful to fontify these everywhere to show e.g. when
1686 ;; a type keyword is accidentally used as an identifier.
1687
1688 ;; Fontify basic types.
1689 ,(let ((re (c-make-keywords-re nil
1690 (c-lang-const c-primitive-type-kwds))))
1691 (if (c-major-mode-is 'pike-mode)
1692 ;; No symbol is a keyword after "->" in Pike.
0386b551 1693 `(,(concat "\\(\\=.?\\|[^>]\\|[^-]>\\)"
d9e94c22 1694 "\\<\\(" re "\\)\\>")
0386b551 1695 2 font-lock-type-face)
d9e94c22
MS
1696 `(,(concat "\\<\\(" re "\\)\\>")
1697 1 'font-lock-type-face)))
1698
e15f8aaa 1699 ;; Fontify types preceded by `c-type-prefix-kwds' (e.g. "struct").
d9e94c22
MS
1700 ,@(when (c-lang-const c-type-prefix-kwds)
1701 `((,(byte-compile
1702 `(lambda (limit)
1703 (c-fontify-types-and-refs
1704 ((c-promote-possible-types t)
1705 ;; The font-lock package in Emacs is known to clobber
1706 ;; `parse-sexp-lookup-properties' (when it exists).
1707 (parse-sexp-lookup-properties
1708 (cc-eval-when-compile
1709 (boundp 'parse-sexp-lookup-properties))))
1710 (save-restriction
1711 ;; Narrow to avoid going past the limit in
1712 ;; `c-forward-type'.
1713 (narrow-to-region (point) limit)
1714 (while (re-search-forward
1715 ,(concat "\\<\\("
1716 (c-make-keywords-re nil
1717 (c-lang-const c-type-prefix-kwds))
1718 "\\)\\>")
1719 limit t)
1720 (unless (c-skip-comments-and-strings limit)
1721 (c-forward-syntactic-ws)
1722 ;; Handle prefix declaration specifiers.
e3715033
AM
1723 (when (or (looking-at c-prefix-spec-kwds-re)
1724 (and (c-major-mode-is 'java-mode)
1725 (looking-at "@[A-Za-z0-9]+")))
0386b551 1726 (c-forward-keyword-clause 1))
d9e94c22
MS
1727 ,(if (c-major-mode-is 'c++-mode)
1728 `(when (and (c-forward-type)
1729 (eq (char-after) ?=))
1730 ;; In C++ we additionally check for a "class
1731 ;; X = Y" construct which is used in
1732 ;; templates, to fontify Y as a type.
1733 (forward-char)
1734 (c-forward-syntactic-ws)
1735 (c-forward-type))
1736 `(c-forward-type))
1737 )))))))))
1738
1739 ;; Fontify symbols after closing braces as declaration
1740 ;; identifiers under the assumption that they are part of
1741 ;; declarations like "class Foo { ... } foo;". It's too
1742 ;; expensive to check this accurately by skipping past the
1743 ;; brace block, so we use the heuristic that it's such a
1744 ;; declaration if the first identifier is on the same line as
1745 ;; the closing brace. `c-font-lock-declarations' will later
1746 ;; override it if it turns out to be an new declaration, but
1747 ;; it will be wrong if it's an expression (see the test
1748 ;; decls-8.cc).
e15f8aaa
AM
1749;; ,@(when (c-lang-const c-opt-block-decls-with-vars-key)
1750;; `((,(c-make-font-lock-search-function
1751;; (concat "}"
1752;; (c-lang-const c-single-line-syntactic-ws)
1753;; "\\(" ; 1 + c-single-line-syntactic-ws-depth
1754;; (c-lang-const c-type-decl-prefix-key)
1755;; "\\|"
1756;; (c-lang-const c-symbol-key)
1757;; "\\)")
1758;; `((c-font-lock-declarators limit t nil) ; That `nil' says use `font-lock-variable-name-face';
1759;; ; `t' would mean `font-lock-function-name-face'.
1760;; (progn
1761;; (c-put-char-property (match-beginning 0) 'c-type
1762;; 'c-decl-id-start)
1763;; ; 'c-decl-type-start)
1764;; (goto-char (match-beginning
1765;; ,(1+ (c-lang-const
1766;; c-single-line-syntactic-ws-depth)))))
1767;; (goto-char (match-end 0)))))))
d9e94c22
MS
1768
1769 ;; Fontify the type in C++ "new" expressions.
1770 ,@(when (c-major-mode-is 'c++-mode)
1379f2c5
AM
1771 ;; This pattern is a probably a "(MATCHER . ANCHORED-HIGHLIGHTER)"
1772 ;; (see Elisp page "Search-based Fontification").
d9e94c22
MS
1773 `(("\\<new\\>"
1774 (c-font-lock-c++-new))))
1775 ))
1776
1777(defun c-font-lock-labels (limit)
0386b551 1778 ;; Fontify all statement labels from the point to LIMIT. Assumes
d9e94c22
MS
1779 ;; that strings and comments have been fontified already. Nil is
1780 ;; always returned.
1781 ;;
0386b551
AM
1782 ;; Note: This function is only used on decoration level 2; this is
1783 ;; taken care of directly by the gargantuan
1784 ;; `c-font-lock-declarations' on higher levels.
1785 ;;
1786 ;; This function might do hidden buffer changes.
d9e94c22
MS
1787
1788 (let (continue-pos id-start
1789 ;; The font-lock package in Emacs is known to clobber
1790 ;; `parse-sexp-lookup-properties' (when it exists).
1791 (parse-sexp-lookup-properties
1792 (cc-eval-when-compile
1793 (boundp 'parse-sexp-lookup-properties))))
1794
1795 (while (re-search-forward ":[^:]" limit t)
1796 (setq continue-pos (point))
1797 (goto-char (match-beginning 0))
1798 (unless (c-skip-comments-and-strings limit)
1799
1800 (c-backward-syntactic-ws)
1801 (and (setq id-start (c-on-identifier))
1802
1803 (not (get-text-property id-start 'face))
1804
1805 (progn
1806 (goto-char id-start)
1807 (c-backward-syntactic-ws)
1808 (or
1809 ;; Check for a char that precedes a statement.
1810 (memq (char-before) '(?\} ?\{ ?\;))
1811 ;; Check for a preceding label. We exploit the font
1812 ;; locking made earlier by this function.
1813 (and (eq (char-before) ?:)
1814 (progn
1815 (backward-char)
1816 (c-backward-syntactic-ws)
1817 (not (bobp)))
1818 (eq (get-text-property (1- (point)) 'face)
1819 c-label-face-name))
1820 ;; Check for a keyword that precedes a statement.
1821 (c-after-conditional)))
1822
1823 (progn
1824 ;; Got a label.
1825 (goto-char id-start)
1826 (looking-at c-symbol-key)
1827 (c-put-font-lock-face (match-beginning 0) (match-end 0)
1828 c-label-face-name)))
1829
1830 (goto-char continue-pos))))
1831 nil)
1832
1833(c-lang-defconst c-basic-matchers-after
1834 "Font lock matchers for various things that should be fontified after
1835generic casts and declarations are fontified. Used on level 2 and
1836higher."
1837
4f9e41e4
AM
1838 t `(,@(when (c-lang-const c-brace-id-list-kwds)
1839 ;; Fontify the remaining identifiers inside an enum list when we start
1840 ;; inside it.
1841 `(c-font-lock-enum-tail
1842 ;; Fontify the identifiers inside enum lists. (The enum type
d9e94c22
MS
1843 ;; name is handled by `c-simple-decl-matchers' or
1844 ;; `c-complex-decl-matchers' below.
4f9e41e4 1845 (,(c-make-font-lock-search-function
d9e94c22
MS
1846 (concat
1847 "\\<\\("
1848 (c-make-keywords-re nil (c-lang-const c-brace-id-list-kwds))
1849 "\\)\\>"
1850 ;; Disallow various common punctuation chars that can't come
1851 ;; before the '{' of the enum list, to avoid searching too far.
1852 "[^\]\[{}();,/#=]*"
1853 "{")
1854 '((c-font-lock-declarators limit t nil)
1855 (save-match-data
1856 (goto-char (match-end 0))
1857 (c-put-char-property (1- (point)) 'c-type
1858 'c-decl-id-start)
1859 (c-forward-syntactic-ws))
1860 (goto-char (match-end 0)))))))
1861
0386b551
AM
1862 ;; Fontify labels after goto etc.
1863 ,@(when (c-lang-const c-before-label-kwds)
1864 `(;; (Got three different interpretation levels here,
d9e94c22
MS
1865 ;; which makes it a bit complicated: 1) The backquote
1866 ;; stuff is expanded when compiled or loaded, 2) the
1867 ;; eval form is evaluated at font-lock setup (to
1868 ;; substitute c-label-face-name correctly), and 3) the
1869 ;; resulting structure is interpreted during
1870 ;; fontification.)
1871 (eval
1872 . ,(let* ((c-before-label-re
1873 (c-make-keywords-re nil
1874 (c-lang-const c-before-label-kwds))))
1875 `(list
1876 ,(concat "\\<\\(" c-before-label-re "\\)\\>"
1877 "\\s *"
1878 "\\(" ; identifier-offset
1879 (c-lang-const c-symbol-key)
1880 "\\)")
0386b551
AM
1881 (list ,(+ (regexp-opt-depth c-before-label-re) 2)
1882 c-label-face-name nil t))))))
d9e94c22
MS
1883
1884 ;; Fontify the clauses after various keywords.
ef8cdf8c
AM
1885 ,@(when (or (c-lang-const c-type-list-kwds)
1886 (c-lang-const c-ref-list-kwds)
1887 (c-lang-const c-colon-type-list-kwds))
1888 `((,(c-make-font-lock-BO-decl-search-function
1889 (concat "\\<\\("
1890 (c-make-keywords-re nil
1891 (append (c-lang-const c-type-list-kwds)
1892 (c-lang-const c-ref-list-kwds)
1893 (c-lang-const c-colon-type-list-kwds)))
1894 "\\)\\>")
1895 '((c-fontify-types-and-refs ((c-promote-possible-types t))
1896 (c-forward-keyword-clause 1)
1897 (if (> (point) limit) (goto-char limit))))))))
1898
1899 ,@(when (c-lang-const c-paren-type-kwds)
1900 `((,(c-make-font-lock-search-function
1901 (concat "\\<\\("
1902 (c-make-keywords-re nil
1903 (c-lang-const c-paren-type-kwds))
1904 "\\)\\>")
1905 '((c-fontify-types-and-refs ((c-promote-possible-types t))
1906 (c-forward-keyword-clause 1)
1907 (if (> (point) limit) (goto-char limit))))))))
1908
1909 ,@(when (c-major-mode-is 'java-mode)
1910 `((eval . (list "\\<\\(@[a-zA-Z0-9]+\\)\\>" 1 c-annotation-face))))
d9e94c22
MS
1911 ))
1912
1913(c-lang-defconst c-matchers-1
1914 t (c-lang-const c-cpp-matchers))
1915
1916(c-lang-defconst c-matchers-2
1917 t (append (c-lang-const c-matchers-1)
1918 (c-lang-const c-basic-matchers-before)
1919 (c-lang-const c-simple-decl-matchers)
1920 (c-lang-const c-basic-matchers-after)))
1921
1922(c-lang-defconst c-matchers-3
1923 t (append (c-lang-const c-matchers-1)
1924 (c-lang-const c-basic-matchers-before)
1925 (c-lang-const c-complex-decl-matchers)
1926 (c-lang-const c-basic-matchers-after)))
1927
1928(defun c-compose-keywords-list (base-list)
1929 ;; Incorporate the font lock keyword lists according to
1930 ;; `c-doc-comment-style' on the given keyword list and return it.
1931 ;; This is used in the function bindings of the
1932 ;; `*-font-lock-keywords-*' symbols since we have to build the list
1933 ;; when font-lock is initialized.
1934
1935 (unless (memq c-doc-face-name c-literal-faces)
1936 (setq c-literal-faces (cons c-doc-face-name c-literal-faces)))
1937
1938 (let* ((doc-keywords
1939 (if (consp (car-safe c-doc-comment-style))
1940 (cdr-safe (or (assq c-buffer-is-cc-mode c-doc-comment-style)
1941 (assq 'other c-doc-comment-style)))
1942 c-doc-comment-style))
1943 (list (nconc (apply 'nconc
1944 (mapcar
1945 (lambda (doc-style)
1946 (let ((sym (intern
1947 (concat (symbol-name doc-style)
1948 "-font-lock-keywords"))))
1949 (cond ((fboundp sym)
1950 (funcall sym))
1951 ((boundp sym)
1952 (append (eval sym) nil)))))
1953 (if (listp doc-keywords)
1954 doc-keywords
1955 (list doc-keywords))))
1956 base-list)))
1957
1958 ;; Kludge: If `c-font-lock-complex-decl-prepare' is on the list we
1959 ;; move it first since the doc comment font lockers might add
7bfc3fdb 1960 ;; `c-type' text properties, so they have to be cleared before that.
d9e94c22
MS
1961 (when (memq 'c-font-lock-complex-decl-prepare list)
1962 (setq list (cons 'c-font-lock-complex-decl-prepare
1963 (delq 'c-font-lock-complex-decl-prepare
1964 (append list nil)))))
1965
1966 list))
1967
1968(defun c-override-default-keywords (def-var)
1969 ;; This is used to override the value on a `*-font-lock-keywords'
1970 ;; variable only if it's nil or has the same value as one of the
1971 ;; `*-font-lock-keywords-*' variables. Older font-lock packages
1972 ;; define a default value for `*-font-lock-keywords' which we want
1973 ;; to override, but we should otoh avoid clobbering a user setting.
1974 ;; This heuristic for that isn't perfect, but I can't think of any
1975 ;; better. /mast
d9e94c22
MS
1976 (when (and (boundp def-var)
1977 (memq (symbol-value def-var)
1978 (cons nil
1979 (mapcar
1980 (lambda (suffix)
1981 (let ((sym (intern (concat (symbol-name def-var)
1982 suffix))))
1983 (and (boundp sym) (symbol-value sym))))
1984 '("-1" "-2" "-3")))))
1985 ;; The overriding is done by unbinding the variable so that the normal
1986 ;; defvar will install its default value later on.
1987 (makunbound def-var)))
1988
1989\f
1990;;; C.
1991
1992(c-override-default-keywords 'c-font-lock-keywords)
1993
1994(defconst c-font-lock-keywords-1 (c-lang-const c-matchers-1 c)
1995 "Minimal font locking for C mode.
1996Fontifies only preprocessor directives (in addition to the syntactic
1997fontification of strings and comments).")
1998
1999(defconst c-font-lock-keywords-2 (c-lang-const c-matchers-2 c)
2000 "Fast normal font locking for C mode.
2001In addition to `c-font-lock-keywords-1', this adds fontification of
2002keywords, simple types, declarations that are easy to recognize, the
2003user defined types on `c-font-lock-extra-types', and the doc comment
2004styles specified by `c-doc-comment-style'.")
2005
2006(defconst c-font-lock-keywords-3 (c-lang-const c-matchers-3 c)
2007 "Accurate normal font locking for C mode.
2008Like `c-font-lock-keywords-2' but detects declarations in a more
2009accurate way that works in most cases for arbitrary types without the
2010need for `c-font-lock-extra-types'.")
2011
2012(defvar c-font-lock-keywords c-font-lock-keywords-3
2013 "Default expressions to highlight in C mode.")
2014
2015(defun c-font-lock-keywords-2 ()
2016 (c-compose-keywords-list c-font-lock-keywords-2))
2017(defun c-font-lock-keywords-3 ()
2018 (c-compose-keywords-list c-font-lock-keywords-3))
2019(defun c-font-lock-keywords ()
2020 (c-compose-keywords-list c-font-lock-keywords))
2021
2022\f
2023;;; C++.
2024
2025(defun c-font-lock-c++-new (limit)
e15f8aaa
AM
2026 ;; FIXME!!! Put in a comment about the context of this function's
2027 ;; invocation. I think it's called as an ANCHORED-MATCHER within an
2028 ;; ANCHORED-HIGHLIGHTER. (2007/2/10).
2029 ;;
d9e94c22
MS
2030 ;; Assuming point is after a "new" word, check that it isn't inside
2031 ;; a string or comment, and if so try to fontify the type in the
2032 ;; allocation expression. Nil is always returned.
2033 ;;
2034 ;; As usual, C++ takes the prize in coming up with a hard to parse
2035 ;; syntax. :P
0386b551
AM
2036 ;;
2037 ;; This function might do hidden buffer changes.
d9e94c22
MS
2038
2039 (unless (c-skip-comments-and-strings limit)
2040 (save-excursion
2041 (catch 'false-alarm
2042 ;; A "new" keyword is followed by one to three expressions, where
2043 ;; the type is the middle one, and the only required part.
2044 (let (expr1-pos expr2-pos
2045 ;; Enable recording of identifier ranges in `c-forward-type'
2046 ;; etc for later fontification. Not using
2047 ;; `c-fontify-types-and-refs' here since the ranges should
2048 ;; be fontified selectively only when an allocation
2049 ;; expression is successfully recognized.
2050 (c-record-type-identifiers t)
2051 c-record-ref-identifiers
2052 ;; The font-lock package in Emacs is known to clobber
2053 ;; `parse-sexp-lookup-properties' (when it exists).
2054 (parse-sexp-lookup-properties
2055 (cc-eval-when-compile
2056 (boundp 'parse-sexp-lookup-properties))))
2057 (c-forward-syntactic-ws)
2058
2059 ;; The first placement arglist is always parenthesized, if it
2060 ;; exists.
2061 (when (eq (char-after) ?\()
2062 (setq expr1-pos (1+ (point)))
2063 (condition-case nil
2064 (c-forward-sexp)
2065 (scan-error (throw 'false-alarm t)))
2066 (c-forward-syntactic-ws))
2067
2068 ;; The second expression is either a type followed by some "*" or
2069 ;; "[...]" or similar, or a parenthesized type followed by a full
2070 ;; identifierless declarator.
2071 (setq expr2-pos (1+ (point)))
2072 (cond ((eq (char-after) ?\())
2073 ((let ((c-promote-possible-types t))
2074 (c-forward-type)))
2075 (t (setq expr2-pos nil)))
2076
2077 (when expr1-pos
2078 (cond
2079 ((not expr2-pos)
2080 ;; No second expression, so the first has to be a
2081 ;; parenthesized type.
2082 (goto-char expr1-pos)
2083 (let ((c-promote-possible-types t))
2084 (c-forward-type)))
2085
2086 ((eq (char-before expr2-pos) ?\()
2087 ;; Got two parenthesized expressions, so we have to look
2088 ;; closer at them to decide which is the type. No need to
2089 ;; handle `c-record-ref-identifiers' since all references
2090 ;; has already been handled by other fontification rules.
2091 (let (expr1-res expr2-res)
2092
2093 (goto-char expr1-pos)
2094 (when (setq expr1-res (c-forward-type))
2095 (unless (looking-at
2096 (cc-eval-when-compile
2097 (concat (c-lang-const c-symbol-start c++)
2098 "\\|[*:\)\[]")))
2099 ;; There's something after the would-be type that
2100 ;; can't be there, so this is a placement arglist.
2101 (setq expr1-res nil)))
2102
2103 (goto-char expr2-pos)
2104 (when (setq expr2-res (c-forward-type))
2105 (unless (looking-at
2106 (cc-eval-when-compile
2107 (concat (c-lang-const c-symbol-start c++)
2108 "\\|[*:\)\[]")))
2109 ;; There's something after the would-be type that can't
2110 ;; be there, so this is an initialization expression.
2111 (setq expr2-res nil))
2112 (when (and (c-go-up-list-forward)
2113 (progn (c-forward-syntactic-ws)
2114 (eq (char-after) ?\()))
2115 ;; If there's a third initialization expression
2116 ;; then the second one is the type, so demote the
2117 ;; first match.
2118 (setq expr1-res nil)))
2119
2120 ;; We fontify the most likely type, with a preference for
2121 ;; the first argument since a placement arglist is more
2122 ;; unusual than an initializer.
2123 (cond ((memq expr1-res '(t known prefix)))
2124 ((memq expr2-res '(t known prefix)))
2125 ((eq expr1-res 'found)
2126 (let ((c-promote-possible-types t))
2127 (goto-char expr1-pos)
2128 (c-forward-type)))
2129 ((eq expr2-res 'found)
2130 (let ((c-promote-possible-types t))
2131 (goto-char expr2-pos)
2132 (c-forward-type)))
2133 ((and (eq expr1-res 'maybe) (not expr2-res))
2134 (let ((c-promote-possible-types t))
2135 (goto-char expr1-pos)
2136 (c-forward-type)))
2137 ((and (not expr1-res) (eq expr2-res 'maybe))
2138 (let ((c-promote-possible-types t))
2139 (goto-char expr2-pos)
2140 (c-forward-type)))
2141 ;; If both type matches are 'maybe then we're
2142 ;; too uncertain to promote either of them.
2143 )))))
2144
2145 ;; Fontify the type that now is recorded in
2146 ;; `c-record-type-identifiers', if any.
2147 (c-fontify-recorded-types-and-refs)))))
2148 nil)
2149
2150(c-override-default-keywords 'c++-font-lock-keywords)
2151
2152(defconst c++-font-lock-keywords-1 (c-lang-const c-matchers-1 c++)
2153 "Minimal font locking for C++ mode.
2154Fontifies only preprocessor directives (in addition to the syntactic
2155fontification of strings and comments).")
2156
2157(defconst c++-font-lock-keywords-2 (c-lang-const c-matchers-2 c++)
2158 "Fast normal font locking for C++ mode.
2159In addition to `c++-font-lock-keywords-1', this adds fontification of
2160keywords, simple types, declarations that are easy to recognize, the
2161user defined types on `c++-font-lock-extra-types', and the doc comment
2162styles specified by `c-doc-comment-style'.")
2163
2164(defconst c++-font-lock-keywords-3 (c-lang-const c-matchers-3 c++)
2165 "Accurate normal font locking for C++ mode.
2166Like `c++-font-lock-keywords-2' but detects declarations in a more
2167accurate way that works in most cases for arbitrary types without the
2168need for `c++-font-lock-extra-types'.")
2169
2170(defvar c++-font-lock-keywords c++-font-lock-keywords-3
2171 "Default expressions to highlight in C++ mode.")
2172
2173(defun c++-font-lock-keywords-2 ()
2174 (c-compose-keywords-list c++-font-lock-keywords-2))
2175(defun c++-font-lock-keywords-3 ()
2176 (c-compose-keywords-list c++-font-lock-keywords-3))
2177(defun c++-font-lock-keywords ()
2178 (c-compose-keywords-list c++-font-lock-keywords))
2179
2180\f
2181;;; Objective-C.
2182
d9e94c22
MS
2183(defun c-font-lock-objc-method ()
2184 ;; Assuming the point is after the + or - that starts an Objective-C
2185 ;; method declaration, fontify it. This must be done before normal
2186 ;; casts, declarations and labels are fontified since they will get
2187 ;; false matches in these things.
0386b551
AM
2188 ;;
2189 ;; This function might do hidden buffer changes.
d9e94c22
MS
2190
2191 (c-fontify-types-and-refs
2192 ((first t)
2193 (c-promote-possible-types t))
2194
2195 (while (and
2196 (progn
2197 (c-forward-syntactic-ws)
2198
2199 ;; An optional method type.
2200 (if (eq (char-after) ?\()
2201 (progn
2202 (forward-char)
2203 (c-forward-syntactic-ws)
2204 (c-forward-type)
2205 (prog1 (c-go-up-list-forward)
2206 (c-forward-syntactic-ws)))
2207 t))
2208
2209 ;; The name. The first time it's the first part of
2210 ;; the function name, the rest of the time it's an
2211 ;; argument name.
2212 (looking-at c-symbol-key)
2213 (progn
2214 (goto-char (match-end 0))
2215 (c-put-font-lock-face (match-beginning 0)
2216 (point)
2217 (if first
2218 'font-lock-function-name-face
2219 'font-lock-variable-name-face))
2220 (c-forward-syntactic-ws)
2221
2222 ;; Another optional part of the function name.
2223 (when (looking-at c-symbol-key)
2224 (goto-char (match-end 0))
2225 (c-put-font-lock-face (match-beginning 0)
2226 (point)
2227 'font-lock-function-name-face)
2228 (c-forward-syntactic-ws))
2229
2230 ;; There's another argument if a colon follows.
2231 (eq (char-after) ?:)))
2232 (forward-char)
2233 (setq first nil))))
2234
2235(defun c-font-lock-objc-methods (limit)
2236 ;; Fontify method declarations in Objective-C. Nil is always
2237 ;; returned.
0386b551
AM
2238 ;;
2239 ;; This function might do hidden buffer changes.
d9e94c22
MS
2240
2241 (let (;; The font-lock package in Emacs is known to clobber
2242 ;; `parse-sexp-lookup-properties' (when it exists).
2243 (parse-sexp-lookup-properties
2244 (cc-eval-when-compile
2245 (boundp 'parse-sexp-lookup-properties))))
2246
2247 (c-find-decl-spots
2248 limit
2249 "[-+]"
2250 nil
2251 (lambda (match-pos inside-macro)
2252 (forward-char)
2253 (c-font-lock-objc-method))))
2254 nil)
2255
2256(c-override-default-keywords 'objc-font-lock-keywords)
2257
2258(defconst objc-font-lock-keywords-1 (c-lang-const c-matchers-1 objc)
2259 "Minimal font locking for Objective-C mode.
2260Fontifies only compiler directives (in addition to the syntactic
2261fontification of strings and comments).")
2262
2263(defconst objc-font-lock-keywords-2 (c-lang-const c-matchers-2 objc)
2264 "Fast normal font locking for Objective-C mode.
2265In addition to `objc-font-lock-keywords-1', this adds fontification of
2266keywords, simple types, declarations that are easy to recognize, the
2267user defined types on `objc-font-lock-extra-types', and the doc
2268comment styles specified by `c-doc-comment-style'.")
2269
2270(defconst objc-font-lock-keywords-3 (c-lang-const c-matchers-3 objc)
2271 "Accurate normal font locking for Objective-C mode.
2272Like `objc-font-lock-keywords-2' but detects declarations in a more
2273accurate way that works in most cases for arbitrary types without the
2274need for `objc-font-lock-extra-types'.")
2275
2276(defvar objc-font-lock-keywords objc-font-lock-keywords-3
2277 "Default expressions to highlight in Objective-C mode.")
2278
2279(defun objc-font-lock-keywords-2 ()
2280 (c-compose-keywords-list objc-font-lock-keywords-2))
2281(defun objc-font-lock-keywords-3 ()
2282 (c-compose-keywords-list objc-font-lock-keywords-3))
2283(defun objc-font-lock-keywords ()
2284 (c-compose-keywords-list objc-font-lock-keywords))
2285
2286;; Kludge to override the default value that
2287;; `objc-font-lock-extra-types' might have gotten from the font-lock
2288;; package. The value replaced here isn't relevant now anyway since
2289;; those types are builtin and therefore listed directly in
2290;; `c-primitive-type-kwds'.
2291(when (equal (sort (append objc-font-lock-extra-types nil) 'string-lessp)
2292 '("BOOL" "Class" "IMP" "SEL"))
2293 (setq objc-font-lock-extra-types
2294 (cc-eval-when-compile (list (concat "[" c-upper "]\\sw*")))))
2295
2296\f
2297;;; Java.
2298
2299(c-override-default-keywords 'java-font-lock-keywords)
2300
2301(defconst java-font-lock-keywords-1 (c-lang-const c-matchers-1 java)
2302 "Minimal font locking for Java mode.
2303Fontifies nothing except the syntactic fontification of strings and
2304comments.")
2305
2306(defconst java-font-lock-keywords-2 (c-lang-const c-matchers-2 java)
2307 "Fast normal font locking for Java mode.
2308In addition to `java-font-lock-keywords-1', this adds fontification of
2309keywords, simple types, declarations that are easy to recognize, the
2310user defined types on `java-font-lock-extra-types', and the doc
2311comment styles specified by `c-doc-comment-style'.")
2312
2313(defconst java-font-lock-keywords-3 (c-lang-const c-matchers-3 java)
2314 "Accurate normal font locking for Java mode.
2315Like `java-font-lock-keywords-2' but detects declarations in a more
2316accurate way that works in most cases for arbitrary types without the
2317need for `java-font-lock-extra-types'.")
2318
2319(defvar java-font-lock-keywords java-font-lock-keywords-3
2320 "Default expressions to highlight in Java mode.")
2321
2322(defun java-font-lock-keywords-2 ()
2323 (c-compose-keywords-list java-font-lock-keywords-2))
2324(defun java-font-lock-keywords-3 ()
2325 (c-compose-keywords-list java-font-lock-keywords-3))
2326(defun java-font-lock-keywords ()
2327 (c-compose-keywords-list java-font-lock-keywords))
2328
2329\f
2330;;; CORBA IDL.
2331
2332(c-override-default-keywords 'idl-font-lock-keywords)
2333
2334(defconst idl-font-lock-keywords-1 (c-lang-const c-matchers-1 idl)
2335 "Minimal font locking for CORBA IDL mode.
2336Fontifies nothing except the syntactic fontification of strings and
2337comments.")
2338
2339(defconst idl-font-lock-keywords-2 (c-lang-const c-matchers-2 idl)
2340 "Fast normal font locking for CORBA IDL mode.
2341In addition to `idl-font-lock-keywords-1', this adds fontification of
2342keywords, simple types, declarations that are easy to recognize, the
2343user defined types on `idl-font-lock-extra-types', and the doc comment
2344styles specified by `c-doc-comment-style'.")
2345
2346(defconst idl-font-lock-keywords-3 (c-lang-const c-matchers-3 idl)
2347 "Accurate normal font locking for CORBA IDL mode.
2348Like `idl-font-lock-keywords-2' but detects declarations in a more
2349accurate way that works in most cases for arbitrary types without the
2350need for `idl-font-lock-extra-types'.")
2351
2352(defvar idl-font-lock-keywords idl-font-lock-keywords-3
2353 "Default expressions to highlight in CORBA IDL mode.")
2354
2355(defun idl-font-lock-keywords-2 ()
2356 (c-compose-keywords-list idl-font-lock-keywords-2))
2357(defun idl-font-lock-keywords-3 ()
2358 (c-compose-keywords-list idl-font-lock-keywords-3))
2359(defun idl-font-lock-keywords ()
2360 (c-compose-keywords-list idl-font-lock-keywords))
2361
2362\f
2363;;; Pike.
2364
2365(c-override-default-keywords 'pike-font-lock-keywords)
2366
2367(defconst pike-font-lock-keywords-1 (c-lang-const c-matchers-1 pike)
2368 "Minimal font locking for Pike mode.
2369Fontifies only preprocessor directives (in addition to the syntactic
2370fontification of strings and comments).")
2371
2372(defconst pike-font-lock-keywords-2 (c-lang-const c-matchers-2 pike)
2373 "Fast normal font locking for Pike mode.
2374In addition to `pike-font-lock-keywords-1', this adds fontification of
2375keywords, simple types, declarations that are easy to recognize, the
2376user defined types on `pike-font-lock-extra-types', and the doc
2377comment styles specified by `c-doc-comment-style'.")
2378
2379(defconst pike-font-lock-keywords-3 (c-lang-const c-matchers-3 pike)
2380 "Accurate normal font locking for Pike mode.
2381Like `pike-font-lock-keywords-2' but detects declarations in a more
2382accurate way that works in most cases for arbitrary types without the
2383need for `pike-font-lock-extra-types'.")
2384
2385(defvar pike-font-lock-keywords pike-font-lock-keywords-3
2386 "Default expressions to highlight in Pike mode.")
2387
2388(defun pike-font-lock-keywords-2 ()
2389 (c-compose-keywords-list pike-font-lock-keywords-2))
2390(defun pike-font-lock-keywords-3 ()
2391 (c-compose-keywords-list pike-font-lock-keywords-3))
2392(defun pike-font-lock-keywords ()
2393 (c-compose-keywords-list pike-font-lock-keywords))
2394
2395\f
2396;;; Doc comments.
2397
2398(defun c-font-lock-doc-comments (prefix limit keywords)
2399 ;; Fontify the comments between the point and LIMIT whose start
2400 ;; matches PREFIX with `c-doc-face-name'. Assumes comments have been
2401 ;; fontified with `font-lock-comment-face' already. nil is always
2402 ;; returned.
2403 ;;
2404 ;; After the fontification of a matching comment, fontification
2405 ;; according to KEYWORDS is applied inside it. It's a list like
2406 ;; `font-lock-keywords' except that anchored matches and eval
2407 ;; clauses aren't supported and that some abbreviated forms can't be
2408 ;; used. The buffer is narrowed to the comment while KEYWORDS is
2409 ;; applied; leading comment starters are included but trailing
2410 ;; comment enders for block comment are not.
2411 ;;
2412 ;; Note that faces added through KEYWORDS should never replace the
2413 ;; existing `c-doc-face-name' face since the existence of that face
2414 ;; is used as a flag in other code to skip comments.
0386b551
AM
2415 ;;
2416 ;; This function might do hidden buffer changes.
d9e94c22
MS
2417
2418 (let (comment-beg region-beg)
2419 (if (eq (get-text-property (point) 'face)
2420 'font-lock-comment-face)
2421 ;; Handle the case when the fontified region starts inside a
2422 ;; comment.
2423 (let ((range (c-literal-limits)))
2424 (setq region-beg (point))
2425 (when range
2426 (goto-char (car range)))
2427 (when (looking-at prefix)
2428 (setq comment-beg (point)))))
2429
2430 (while (or
2431 comment-beg
2432
2433 ;; Search for the prefix until a match is found at the start
2434 ;; of a comment.
2435 (while (when (re-search-forward prefix limit t)
2436 (setq comment-beg (match-beginning 0))
2437 (or (not (c-got-face-at comment-beg
2438 c-literal-faces))
2439 (and (/= comment-beg (point-min))
2440 (c-got-face-at (1- comment-beg)
2441 c-literal-faces))))
2442 (setq comment-beg nil))
2443 (setq region-beg comment-beg))
2444
2445 (if (eq (elt (parse-partial-sexp comment-beg (+ comment-beg 2)) 7) t)
2446 ;; Collect a sequence of doc style line comments.
2447 (progn
2448 (goto-char comment-beg)
2449 (while (and (progn
2450 (c-forward-single-comment)
2451 (skip-syntax-forward " ")
2452 (< (point) limit))
2453 (looking-at prefix))))
2454 (goto-char comment-beg)
2455 (c-forward-single-comment))
2456 (if (> (point) limit) (goto-char limit))
2457 (setq comment-beg nil)
2458
2459 (let ((region-end (point))
2460 (keylist keywords) keyword matcher highlights)
2461 (c-put-font-lock-face region-beg region-end c-doc-face-name)
2462 (save-restriction
2463 ;; Narrow to the doc comment. Among other things, this
2464 ;; helps by making "^" match at the start of the comment.
2465 ;; Do not include a trailing block comment ender, though.
2466 (and (> region-end (1+ region-beg))
2467 (progn (goto-char region-end)
2468 (backward-char 2)
2469 (looking-at "\\*/"))
2470 (setq region-end (point)))
2471 (narrow-to-region region-beg region-end)
2472
2473 (while keylist
2474 (setq keyword (car keylist)
2475 keylist (cdr keylist)
2476 matcher (car keyword))
2477 (goto-char region-beg)
2478 (while (if (stringp matcher)
2479 (re-search-forward matcher region-end t)
2480 (funcall matcher region-end))
2481 (setq highlights (cdr keyword))
2482 (if (consp (car highlights))
2483 (while highlights
2484 (font-lock-apply-highlight (car highlights))
2485 (setq highlights (cdr highlights)))
2486 (font-lock-apply-highlight highlights))))
2487
2488 (goto-char region-end)))))
2489 nil)
2490(put 'c-font-lock-doc-comments 'lisp-indent-function 2)
2491
2492(defun c-find-invalid-doc-markup (regexp limit)
2493 ;; Used to fontify invalid markup in doc comments after the correct
5a89f0a7 2494 ;; ones have been fontified: Find the first occurrence of REGEXP
d9e94c22
MS
2495 ;; between the point and LIMIT that only is fontified with
2496 ;; `c-doc-face-name'. If a match is found then submatch 0 surrounds
2497 ;; the first char and t is returned, otherwise nil is returned.
0386b551
AM
2498 ;;
2499 ;; This function might do hidden buffer changes.
d9e94c22
MS
2500 (let (start)
2501 (while (if (re-search-forward regexp limit t)
2502 (not (eq (get-text-property
2503 (setq start (match-beginning 0)) 'face)
2504 c-doc-face-name))
2505 (setq start nil)))
2506 (when start
2507 (store-match-data (list (copy-marker start)
2508 (copy-marker (1+ start))))
2509 t)))
2510
0386b551
AM
2511;; GtkDoc patterns contributed by Masatake YAMATO <jet@gyve.org>.
2512
2513(defconst gtkdoc-font-lock-doc-comments
2514 (let ((symbol "[a-zA-Z0-9_]+")
2515 (header "^ \\* "))
7d0ee75c 2516 `((,(concat header "\\(" symbol "\\):[ \t]*$")
0386b551
AM
2517 1 ,c-doc-markup-face-name prepend nil)
2518 (,(concat symbol "()")
2519 0 ,c-doc-markup-face-name prepend nil)
2520 (,(concat header "\\(" "@" symbol "\\):")
2521 1 ,c-doc-markup-face-name prepend nil)
68a4a27a 2522 (,(concat "[#%@]" symbol)
0386b551
AM
2523 0 ,c-doc-markup-face-name prepend nil))
2524 ))
2525
2526(defconst gtkdoc-font-lock-doc-protection
2527 `(("< \\(public\\|private\\|protected\\) >"
2528 1 ,c-doc-markup-face-name prepend nil)))
2529
2530(defconst gtkdoc-font-lock-keywords
2531 `((,(lambda (limit)
2532 (c-font-lock-doc-comments "/\\*\\*$" limit
2533 gtkdoc-font-lock-doc-comments)
2534 (c-font-lock-doc-comments "/\\*< " limit
2535 gtkdoc-font-lock-doc-protection)
2536 ))))
2537
2538;; Javadoc.
2539
7bfc3fdb
MS
2540(defconst javadoc-font-lock-doc-comments
2541 `(("{@[a-z]+[^}\n\r]*}" ; "{@foo ...}" markup.
2542 0 ,c-doc-markup-face-name prepend nil)
0386b551
AM
2543 ("^\\(/\\*\\)?\\(\\s \\|\\*\\)*\\(@[a-z]+\\)" ; "@foo ..." markup.
2544 3 ,c-doc-markup-face-name prepend nil)
7bfc3fdb
MS
2545 (,(concat "</?\\sw" ; HTML tags.
2546 "\\("
2547 (concat "\\sw\\|\\s \\|[=\n\r*.:]\\|"
2548 "\"[^\"]*\"\\|'[^']*'")
2549 "\\)*>")
2550 0 ,c-doc-markup-face-name prepend nil)
2551 ("&\\(\\sw\\|[.:]\\)+;" ; HTML entities.
2552 0 ,c-doc-markup-face-name prepend nil)
2553 ;; Fontify remaining markup characters as invalid. Note
2554 ;; that the Javadoc spec is hazy about when "@" is
2555 ;; allowed in non-markup use.
2556 (,(lambda (limit)
2557 (c-find-invalid-doc-markup "[<>&]\\|{@" limit))
0386b551 2558 0 'font-lock-warning-face prepend nil)))
7bfc3fdb
MS
2559
2560(defconst javadoc-font-lock-keywords
2561 `((,(lambda (limit)
2562 (c-font-lock-doc-comments "/\\*\\*" limit
2563 javadoc-font-lock-doc-comments)))))
d9e94c22 2564
0386b551
AM
2565;; Pike autodoc.
2566
d9e94c22
MS
2567(defconst autodoc-decl-keywords
2568 ;; Adorned regexp matching the keywords that introduce declarations
2569 ;; in Pike Autodoc.
2570 (cc-eval-when-compile
2571 (c-make-keywords-re t '("@decl" "@elem" "@index" "@member") 'pike-mode)))
2572
2573(defconst autodoc-decl-type-keywords
2574 ;; Adorned regexp matching the keywords that are followed by a type.
2575 (cc-eval-when-compile
2576 (c-make-keywords-re t '("@elem" "@member") 'pike-mode)))
2577
2578(defun autodoc-font-lock-line-markup (limit)
2579 ;; Fontify all line oriented keywords between the point and LIMIT.
2580 ;; Nil is always returned.
0386b551
AM
2581 ;;
2582 ;; This function might do hidden buffer changes.
d9e94c22
MS
2583
2584 (let ((line-re (concat "^\\(\\(/\\*!\\|\\s *\\("
2585 c-current-comment-prefix
2586 "\\)\\)\\s *\\)@[A-Za-z_-]+\\(\\s \\|$\\)"))
2587 (markup-faces (list c-doc-markup-face-name c-doc-face-name)))
2588
2589 (while (re-search-forward line-re limit t)
2590 (goto-char (match-end 1))
2591
2592 (if (looking-at autodoc-decl-keywords)
2593 (let* ((kwd-pos (point))
2594 (start (match-end 1))
2595 (pos start)
2596 end)
2597
2598 (c-put-font-lock-face (point) pos markup-faces)
2599
2600 ;; Put a declaration end mark at the markup keyword and
2601 ;; remove the faces from the rest of the line so that it
2602 ;; gets refontified as a declaration later on by
2603 ;; `c-font-lock-declarations'.
2604 (c-put-char-property (1- pos) 'c-type 'c-decl-end)
2605 (goto-char pos)
2606 (while (progn
2607 (end-of-line)
2608 (setq end (point))
2609 (and (eq (char-before) ?@)
2610 (not (eobp))
2611 (progn (forward-char)
0386b551 2612 (skip-syntax-forward " ")
d9e94c22
MS
2613 (looking-at c-current-comment-prefix))))
2614 (goto-char (match-end 0))
2615 (c-remove-font-lock-face pos (1- end))
2616 (c-put-font-lock-face (1- end) end markup-faces)
2617 (setq pos (point)))
2618
2619 ;; Include the final newline in the removed area. This
2620 ;; has no visual effect but it avoids some tricky special
2621 ;; cases in the testsuite wrt the differences in string
2622 ;; fontification in Emacs vs XEmacs.
2623 (c-remove-font-lock-face pos (min (1+ (point)) (point-max)))
2624
2625 ;; Must handle string literals explicitly inside the declaration.
2626 (goto-char start)
2627 (while (re-search-forward
2628 "\"\\([^\\\"]\\|\\\\.\\)*\"\\|'\\([^\\']\\|\\\\.\\)*'"
2629 end 'move)
2630 (c-put-font-lock-string-face (match-beginning 0)
2631 (point)))
2632
2633 ;; Fontify types after keywords that always are followed
2634 ;; by them.
2635 (goto-char kwd-pos)
2636 (when (looking-at autodoc-decl-type-keywords)
2637 (c-fontify-types-and-refs ((c-promote-possible-types t))
2638 (goto-char start)
2639 (c-forward-syntactic-ws)
2640 (c-forward-type))))
2641
2642 ;; Mark each whole line as markup, as long as the logical line
2643 ;; continues.
2644 (while (progn
2645 (c-put-font-lock-face (point)
2646 (progn (end-of-line) (point))
2647 markup-faces)
2648 (and (eq (char-before) ?@)
2649 (not (eobp))
2650 (progn (forward-char)
0386b551 2651 (skip-syntax-forward " ")
d9e94c22
MS
2652 (looking-at c-current-comment-prefix))))
2653 (goto-char (match-end 0))))))
2654
2655 nil)
2656
7bfc3fdb
MS
2657(defconst autodoc-font-lock-doc-comments
2658 `(("@\\(\\w+{\\|\\[\\([^\]@\n\r]\\|@@\\)*\\]\\|[@}]\\|$\\)"
2659 ;; In-text markup.
2660 0 ,c-doc-markup-face-name prepend nil)
2661 (autodoc-font-lock-line-markup)
2662 ;; Fontify remaining markup characters as invalid.
2663 (,(lambda (limit)
2664 (c-find-invalid-doc-markup "@" limit))
0386b551 2665 0 'font-lock-warning-face prepend nil)
7bfc3fdb
MS
2666 ))
2667
d9e94c22
MS
2668(defun autodoc-font-lock-keywords ()
2669 ;; Note that we depend on that `c-current-comment-prefix' has got
2670 ;; its proper value here.
0386b551
AM
2671 ;;
2672 ;; This function might do hidden buffer changes.
d9e94c22
MS
2673
2674 ;; The `c-type' text property with `c-decl-end' is used to mark the
2675 ;; end of the `autodoc-decl-keywords' occurrences to fontify the
2676 ;; following declarations.
2677 (setq c-type-decl-end-used t)
2678
7bfc3fdb
MS
2679 `((,(lambda (limit)
2680 (c-font-lock-doc-comments "/[*/]!" limit
2681 autodoc-font-lock-doc-comments)))))
d9e94c22
MS
2682
2683\f
3c0ab532 2684;; 2006-07-10: awk-font-lock-keywords has been moved back to cc-awk.el.
d9e94c22
MS
2685(cc-provide 'cc-fonts)
2686
2687;;; cc-fonts.el ends here