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