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