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