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