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