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