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