Fix a comment whitespace typo.
[emacs.git] / lisp / progmodes / cc-fonts.el
blobb35d33a5fd3e4ad9f5b357c9d3ddbad80a0bfa00
1 ;;; cc-fonts.el --- font lock support for CC Mode
3 ;; Copyright (C) 2002-2017 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 ;; We used to use a font-lock "anchored matcher" here for
543 ;; the paren syntax. This failed when the ">" was at EOL,
544 ;; since `font-lock-fontify-anchored-keywords' terminated
545 ;; its loop at EOL without executing our lambda form at
546 ;; all.
547 `((,(c-make-font-lock-search-function
548 (concat noncontinued-line-end
549 (c-lang-const c-opt-cpp-prefix)
551 (c-lang-const c-syntactic-ws)
552 "\\(<[^>\n\r]*>?\\)")
553 `(,(+ ncle-depth re-depth sws-depth 1)
554 font-lock-string-face t)
555 `((let ((beg (match-beginning
556 ,(+ ncle-depth re-depth sws-depth 1)))
557 (end (1- (match-end ,(+ ncle-depth re-depth
558 sws-depth 1)))))
559 (if (eq (char-after end) ?>)
560 (progn
561 (c-mark-<-as-paren beg)
562 (c-mark->-as-paren end))
563 (c-unmark-<->-as-paren beg)))
564 nil))))))
566 ;; #define.
567 ,@(when (c-lang-const c-opt-cpp-macro-define)
568 `((,(c-make-font-lock-search-function
569 (concat
570 noncontinued-line-end
571 (c-lang-const c-opt-cpp-prefix)
572 (c-lang-const c-opt-cpp-macro-define)
573 (c-lang-const c-nonempty-syntactic-ws)
574 "\\(" (c-lang-const ; 1 + ncle + nsws
575 c-symbol-key) "\\)"
576 (concat "\\(" ; 2 + ncle + nsws + c-sym-key
577 ;; Macro with arguments - a "function".
578 "\\((\\)" ; 3 + ncle + nsws + c-sym-key
579 "\\|"
580 ;; Macro without arguments - a "variable".
581 "\\([^(]\\|$\\)"
582 "\\)"))
583 `((if (match-beginning
584 ,(+ 3 ncle-depth nsws-depth
585 (c-lang-const c-symbol-key-depth)))
587 ;; "Function". Fontify the name and the arguments.
588 (save-restriction
589 (c-put-font-lock-face
590 (match-beginning ,(+ 1 ncle-depth nsws-depth))
591 (match-end ,(+ 1 ncle-depth nsws-depth))
592 'font-lock-function-name-face)
593 (goto-char
594 (match-end
595 ,(+ 3 ncle-depth nsws-depth
596 (c-lang-const c-symbol-key-depth))))
598 (narrow-to-region (point-min) limit)
599 (while (and
600 (progn
601 (c-forward-syntactic-ws)
602 (looking-at c-symbol-key))
603 (progn
604 (c-put-font-lock-face
605 (match-beginning 0) (match-end 0)
606 'font-lock-variable-name-face)
607 (goto-char (match-end 0))
608 (c-forward-syntactic-ws)
609 (eq (char-after) ?,)))
610 (forward-char)))
612 ;; "Variable".
613 (c-put-font-lock-face
614 (match-beginning ,(+ 1 ncle-depth nsws-depth))
615 (match-end ,(+ 1 ncle-depth nsws-depth))
616 'font-lock-variable-name-face)))))))
618 ;; Fontify cpp function names in preprocessor
619 ;; expressions in #if and #elif.
620 ,@(when (and (c-lang-const c-cpp-expr-directives)
621 (c-lang-const c-cpp-expr-functions))
622 (let ((ced-re (c-make-keywords-re t
623 (c-lang-const c-cpp-expr-directives)))
624 (cef-re (c-make-keywords-re t
625 (c-lang-const c-cpp-expr-functions))))
627 `((,(c-make-font-lock-context-search-function
628 `(,(concat noncontinued-line-end
629 (c-lang-const c-opt-cpp-prefix)
630 ced-re ; 1 + ncle-depth
631 ;; Match the whole logical line to look
632 ;; for the functions in.
633 "\\(\\\\\\(.\\|[\n\r]\\)\\|[^\n\r]\\)*")
634 ((let ((limit (match-end 0)))
635 (while (re-search-forward ,cef-re limit 'move)
636 (c-put-font-lock-face (match-beginning 1)
637 (match-end 1)
638 c-preprocessor-face-name)))
639 (goto-char (match-end ,(1+ ncle-depth)))))
640 `(in-cpp-expr
641 (save-excursion (c-end-of-macro) (point))
642 ,cef-re
643 (1 c-preprocessor-face-name t)))))))
645 ;; Fontify the directive names.
646 (,(c-make-font-lock-search-function
647 (concat noncontinued-line-end
648 "\\("
649 (c-lang-const c-opt-cpp-prefix)
650 "[" (c-lang-const c-symbol-chars) "]+"
651 "\\)")
652 `(,(1+ ncle-depth) c-preprocessor-face-name t)))
654 (eval . (list ,(c-make-syntactic-matcher
655 (concat noncontinued-line-end
656 (c-lang-const c-opt-cpp-prefix)
657 "if\\(n\\)def\\>"))
658 ,(+ ncle-depth 1)
659 c-negation-char-face-name
660 'append))
663 ,@(when (c-major-mode-is 'pike-mode)
664 ;; Recognize hashbangs in Pike.
665 `((eval . (list "\\`#![^\n\r]*"
666 0 c-preprocessor-face-name))))
668 ;; Make hard spaces visible through an inverted `font-lock-warning-face'.
669 (eval . (list
670 "\240"
671 0 (progn
672 (unless (c-face-name-p 'c-nonbreakable-space-face)
673 (c-make-inverse-face 'font-lock-warning-face
674 'c-nonbreakable-space-face))
675 ''c-nonbreakable-space-face)))
678 (defun c-font-lock-invalid-string ()
679 ;; Assuming the point is after the opening character of a string,
680 ;; fontify that char with `font-lock-warning-face' if the string
681 ;; decidedly isn't terminated properly.
683 ;; This function does hidden buffer changes.
684 (let ((start (1- (point))))
685 (save-excursion
686 (and (eq (elt (parse-partial-sexp start (c-point 'eol)) 8) start)
687 (if (if (eval-when-compile (integerp ?c))
688 ;; Emacs
689 (integerp c-multiline-string-start-char)
690 ;; XEmacs
691 (characterp c-multiline-string-start-char))
692 ;; There's no multiline string start char before the
693 ;; string, so newlines aren't allowed.
694 (not (eq (char-before start) c-multiline-string-start-char))
695 ;; Multiline strings are allowed anywhere if
696 ;; c-multiline-string-start-char is t.
697 (not c-multiline-string-start-char))
698 (if c-string-escaped-newlines
699 ;; There's no \ before the newline.
700 (not (eq (char-before (point)) ?\\))
701 ;; Escaped newlines aren't supported.
703 (c-put-font-lock-face start (1+ start) 'font-lock-warning-face)))))
705 (defun c-font-lock-invalid-single-quotes (limit)
706 ;; This function will be called from font-lock for a region bounded by POINT
707 ;; and LIMIT, as though it were to identify a keyword for
708 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
709 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
710 ;; Fontification".
712 ;; This function fontifies invalid single quotes with
713 ;; `font-lock-warning-face'. These are the single quotes which
714 ;; o - aren't inside a literal;
715 ;; o - are marked with a syntax-table text property value '(1); and
716 ;; o - are NOT marked with a non-null c-digit-separator property.
717 (let ((limits (c-literal-limits))
718 state beg end)
719 (if limits
720 (goto-char (cdr limits))) ; Even for being in a ' '
721 (while (< (point) limit)
722 (setq beg (point))
723 (setq state (parse-partial-sexp (point) limit nil nil nil 'syntax-table))
724 (setq end (point))
725 (goto-char beg)
726 (while (progn (skip-chars-forward "^'" end)
727 (< (point) end))
728 (if (and (equal (c-get-char-property (point) 'syntax-table) '(1))
729 (not (c-get-char-property (point) 'c-digit-separator)))
730 (c-put-font-lock-face (point) (1+ (point)) font-lock-warning-face))
731 (forward-char))
732 (parse-partial-sexp end limit nil nil state 'syntax-table)))
733 nil)
735 (c-lang-defconst c-basic-matchers-before
736 "Font lock matchers for basic keywords, labels, references and various
737 other easily recognizable things that should be fontified before generic
738 casts and declarations are fontified. Used on level 2 and higher."
740 ;; Note: `c-font-lock-declarations' assumes that no matcher here
741 ;; sets `font-lock-type-face' in languages where
742 ;; `c-recognize-<>-arglists' is set.
744 t `(;; Put a warning face on the opener of unclosed strings that
745 ;; can't span lines. Later font
746 ;; lock packages have a `font-lock-syntactic-face-function' for
747 ;; this, but it doesn't give the control we want since any
748 ;; fontification done inside the function will be
749 ;; unconditionally overridden.
750 ,(c-make-font-lock-search-function
751 ;; Match a char before the string starter to make
752 ;; `c-skip-comments-and-strings' work correctly.
753 (concat ".\\(" c-string-limit-regexp "\\)")
754 '((c-font-lock-invalid-string)))
756 ;; Invalid single quotes.
757 c-font-lock-invalid-single-quotes
759 ;; Fontify C++ raw strings.
760 ,@(when (c-major-mode-is 'c++-mode)
761 '(c-font-lock-raw-strings))
763 ;; Fontify keyword constants.
764 ,@(when (c-lang-const c-constant-kwds)
765 (let ((re (c-make-keywords-re nil (c-lang-const c-constant-kwds))))
766 (if (c-major-mode-is 'pike-mode)
767 ;; No symbol is a keyword after "->" in Pike.
768 `((eval . (list ,(concat "\\(\\=.?\\|[^>]\\|[^-]>\\)"
769 "\\<\\(" re "\\)\\>")
770 2 c-constant-face-name)))
771 `((eval . (list ,(concat "\\<\\(" re "\\)\\>")
772 1 c-constant-face-name))))))
774 ;; Fontify all keywords except the primitive types.
775 ,(if (c-major-mode-is 'pike-mode)
776 ;; No symbol is a keyword after "->" in Pike.
777 `(,(concat "\\(\\=.?\\|[^>]\\|[^-]>\\)"
778 "\\<" (c-lang-const c-regular-keywords-regexp))
779 2 font-lock-keyword-face)
780 `(,(concat "\\<" (c-lang-const c-regular-keywords-regexp))
781 1 font-lock-keyword-face))
783 ;; Fontify leading identifiers in fully qualified names like
784 ;; "foo::bar" in languages that supports such things.
785 ,@(when (c-lang-const c-opt-identifier-concat-key)
786 (if (c-major-mode-is 'java-mode)
787 ;; Java needs special treatment since "." is used both to
788 ;; qualify names and in normal indexing. Here we look for
789 ;; capital characters at the beginning of an identifier to
790 ;; recognize the class. "*" is also recognized to cover
791 ;; wildcard import declarations. All preceding dot separated
792 ;; identifiers are taken as package names and therefore
793 ;; fontified as references.
794 `(,(c-make-font-lock-search-function
795 ;; Search for class identifiers preceded by ".". The
796 ;; anchored matcher takes it from there.
797 (concat (c-lang-const c-opt-identifier-concat-key)
798 (c-lang-const c-simple-ws) "*"
799 (concat "\\("
800 "[" c-upper "]"
801 "[" (c-lang-const c-symbol-chars) "]*"
802 "\\|"
803 "\\*"
804 "\\)"))
805 `((let (id-end)
806 (goto-char (1+ (match-beginning 0)))
807 (while (and (eq (char-before) ?.)
808 (progn
809 (backward-char)
810 (c-backward-syntactic-ws)
811 (setq id-end (point))
812 (< (skip-chars-backward
813 ,(c-lang-const c-symbol-chars))
815 (not (get-text-property (point) 'face)))
816 (c-put-font-lock-face (point) id-end
817 c-reference-face-name)
818 (c-backward-syntactic-ws)))
820 (goto-char (match-end 0)))))
822 `((,(byte-compile
823 ;; Must use a function here since we match longer than
824 ;; we want to move before doing a new search. This is
825 ;; not necessary for XEmacs since it restarts the
826 ;; search from the end of the first highlighted
827 ;; submatch (something that causes problems in other
828 ;; places).
829 `(lambda (limit)
830 (while (re-search-forward
831 ,(concat "\\(\\<" ; 1
832 "\\(" (c-lang-const c-symbol-key) "\\)" ; 2
833 (c-lang-const c-simple-ws) "*"
834 (c-lang-const c-opt-identifier-concat-key)
835 (c-lang-const c-simple-ws) "*"
836 "\\)"
837 "\\("
838 (c-lang-const c-opt-after-id-concat-key)
839 "\\)")
840 limit t)
841 (unless (progn
842 (goto-char (match-beginning 0))
843 (c-skip-comments-and-strings limit))
844 (or (get-text-property (match-beginning 2) 'face)
845 (c-put-font-lock-face (match-beginning 2)
846 (match-end 2)
847 c-reference-face-name))
848 (goto-char (match-end 1))))))))))
850 ;; Fontify the special declarations in Objective-C.
851 ,@(when (c-major-mode-is 'objc-mode)
852 `(;; Fontify class names in the beginning of message expressions.
853 ,(c-make-font-lock-search-function
854 "\\["
855 '((c-fontify-types-and-refs ()
856 (c-forward-syntactic-ws limit)
857 (let ((start (point)))
858 ;; In this case we accept both primitive and known types.
859 (when (eq (c-forward-type) 'known)
860 (goto-char start)
861 (let ((c-promote-possible-types t))
862 (c-forward-type))))
863 (if (> (point) limit) (goto-char limit)))))
865 ;; The @interface/@implementation/@protocol directives.
866 ,(c-make-font-lock-search-function
867 (concat "\\<"
868 (regexp-opt
869 '("@interface" "@implementation" "@protocol")
871 "\\>")
872 '((c-fontify-types-and-refs
873 (;; The font-lock package in Emacs is known to clobber
874 ;; `parse-sexp-lookup-properties' (when it exists).
875 (parse-sexp-lookup-properties
876 (cc-eval-when-compile
877 (boundp 'parse-sexp-lookup-properties))))
878 (c-forward-objc-directive)
879 nil)
880 (goto-char (match-beginning 0))))))
882 (eval . (list "\\(!\\)[^=]" 1 c-negation-char-face-name))
885 (defun c-font-lock-complex-decl-prepare (limit)
886 ;; This function will be called from font-lock for a region bounded by POINT
887 ;; and LIMIT, as though it were to identify a keyword for
888 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
889 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
890 ;; Fontification".
892 ;; Called before any of the matchers in `c-complex-decl-matchers'.
894 ;; This function does hidden buffer changes.
896 ;;(message "c-font-lock-complex-decl-prepare %s %s" (point) limit)
898 ;; Clear the list of found types if we start from the start of the
899 ;; buffer, to make it easier to get rid of misspelled types and
900 ;; variables that have gotten recognized as types in malformed code.
901 (when (bobp)
902 (c-clear-found-types))
904 ;; Clear the c-type char properties which mark the region, to recalculate
905 ;; them properly. The most interesting properties are those put on the
906 ;; closest token before the region.
907 (save-excursion
908 (let ((pos (point)))
909 (c-backward-syntactic-ws)
910 (c-clear-char-properties
911 (if (and (not (bobp))
912 (memq (c-get-char-property (1- (point)) 'c-type)
913 '(c-decl-arg-start
914 c-decl-end
915 c-decl-id-start
916 c-decl-type-start)))
917 (1- (point))
918 pos)
919 limit 'c-type)))
921 ;; Update `c-state-cache' to the beginning of the region. This will
922 ;; make `c-beginning-of-syntax' go faster when it's used later on,
923 ;; and it's near the point most of the time.
924 (c-parse-state)
926 ;; Check if the fontified region starts inside a declarator list so
927 ;; that `c-font-lock-declarators' should be called at the start.
928 ;; The declared identifiers are font-locked correctly as types, if
929 ;; that is what they are.
930 (let ((prop (save-excursion
931 (c-backward-syntactic-ws)
932 (unless (bobp)
933 (c-get-char-property (1- (point)) 'c-type)))))
934 (when (memq prop '(c-decl-id-start c-decl-type-start))
935 (c-forward-syntactic-ws limit)
936 (c-font-lock-declarators limit t (eq prop 'c-decl-type-start)
937 (not (c-bs-at-toplevel-p (point))))))
939 (setq c-font-lock-context ;; (c-guess-font-lock-context)
940 (save-excursion
941 (if (and c-cpp-expr-intro-re
942 (c-beginning-of-macro)
943 (looking-at c-cpp-expr-intro-re))
944 'in-cpp-expr)))
945 nil)
947 (defun c-font-lock-<>-arglists (limit)
948 ;; This function will be called from font-lock for a region bounded by POINT
949 ;; and LIMIT, as though it were to identify a keyword for
950 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
951 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
952 ;; Fontification".
954 ;; Fontify types and references in names containing angle bracket
955 ;; arglists from the point to LIMIT. Note that
956 ;; `c-font-lock-declarations' has already handled many of them.
958 ;; This function might do hidden buffer changes.
960 (let (;; The font-lock package in Emacs is known to clobber
961 ;; `parse-sexp-lookup-properties' (when it exists).
962 (parse-sexp-lookup-properties
963 (cc-eval-when-compile
964 (boundp 'parse-sexp-lookup-properties)))
965 (c-parse-and-markup-<>-arglists t)
966 c-restricted-<>-arglists
967 id-start id-end id-face pos kwd-sym)
969 (while (and (< (point) limit)
970 (re-search-forward c-opt-<>-arglist-start limit t))
972 (setq id-start (match-beginning 1)
973 id-end (match-end 1)
974 pos (point))
976 (goto-char id-start)
977 (unless (c-skip-comments-and-strings limit)
978 (setq kwd-sym nil
979 c-restricted-<>-arglists nil
980 id-face (get-text-property id-start 'face))
982 (if (cond
983 ((eq id-face 'font-lock-type-face)
984 ;; The identifier got the type face so it has already been
985 ;; handled in `c-font-lock-declarations'.
986 nil)
988 ((eq id-face 'font-lock-keyword-face)
989 (when (looking-at c-opt-<>-sexp-key)
990 ;; There's a special keyword before the "<" that tells
991 ;; that it's an angle bracket arglist.
992 (setq kwd-sym (c-keyword-sym (match-string 1)))))
995 ;; There's a normal identifier before the "<". If we're not in
996 ;; a declaration context then we set `c-restricted-<>-arglists'
997 ;; to avoid recognizing templates in function calls like "foo (a
998 ;; < b, c > d)".
999 (c-backward-syntactic-ws)
1000 (when (and (memq (char-before) '(?\( ?,))
1001 (not (eq (get-text-property (1- (point)) 'c-type)
1002 'c-decl-arg-start)))
1003 (setq c-restricted-<>-arglists t))
1006 (progn
1007 (goto-char (1- pos))
1008 ;; Check for comment/string both at the identifier and
1009 ;; at the "<".
1010 (unless (c-skip-comments-and-strings limit)
1012 (c-fontify-types-and-refs ()
1013 (when (c-forward-<>-arglist (c-keyword-member
1014 kwd-sym 'c-<>-type-kwds))
1015 (when (and c-opt-identifier-concat-key
1016 (not (get-text-property id-start 'face)))
1017 (c-forward-syntactic-ws)
1018 (cond ((looking-at c-opt-identifier-concat-key)
1019 (c-put-font-lock-face id-start id-end
1020 c-reference-face-name))
1021 ((eq (char-after) ?\())
1022 (t (c-put-font-lock-face id-start id-end
1023 'font-lock-type-face))))))
1025 (goto-char pos)))
1026 (goto-char pos)))))
1027 nil)
1029 (defun c-font-lock-declarators (limit list types not-top)
1030 ;; Assuming the point is at the start of a declarator in a declaration,
1031 ;; fontify the identifier it declares. (If TYPES is set, it does this via
1032 ;; the macro `c-fontify-types-and-refs'.)
1034 ;; If LIST is non-nil, also fontify the ids in any following declarators in
1035 ;; a comma separated list (e.g. "foo" and "*bar" in "int foo = 17, *bar;");
1036 ;; additionally, mark the commas with c-type property 'c-decl-id-start or
1037 ;; 'c-decl-type-start (according to TYPES). Stop at LIMIT.
1039 ;; If TYPES is non-nil, fontify all identifiers as types. If NOT-TOP is
1040 ;; non-nil, we are not at the top-level ("top-level" includes being directly
1041 ;; inside a class or namespace, etc.).
1043 ;; Nil is always returned. The function leaves point at the delimiter after
1044 ;; the last declarator it processes.
1046 ;; This function might do hidden buffer changes.
1048 ;;(message "c-font-lock-declarators from %s to %s" (point) limit)
1049 (c-fontify-types-and-refs
1050 ((pos (point)) next-pos id-start
1051 decl-res
1052 id-face got-type got-init
1053 c-last-identifier-range
1054 (separator-prop (if types 'c-decl-type-start 'c-decl-id-start)))
1056 ;; The following `while' fontifies a single declarator id each time round.
1057 ;; It loops only when LIST is non-nil.
1058 (while
1059 (and pos (setq decl-res (c-forward-declarator limit)))
1060 (setq next-pos (point)
1061 id-start (car decl-res)
1062 id-face (if (and (eq (char-after) ?\()
1063 (or (not (c-major-mode-is 'c++-mode))
1064 (not not-top)
1065 (car (cddr (cddr decl-res))) ; Id is in
1066 ; parens, etc.
1067 (save-excursion
1068 (forward-char)
1069 (c-forward-syntactic-ws)
1070 (looking-at "[*&]")))
1071 (not (car (cddr decl-res)))
1072 (or (not (c-major-mode-is 'c++-mode))
1073 (save-excursion
1074 (let (c-last-identifier-range)
1075 (forward-char)
1076 (c-forward-syntactic-ws)
1077 (catch 'is-function
1078 (while
1079 (progn
1080 (if (eq (char-after) ?\))
1081 (throw 'is-function t))
1082 (setq got-type (c-forward-type))
1083 (cond
1084 ((null got-type)
1085 (throw 'is-function nil))
1086 ((not (eq got-type 'maybe))
1087 (throw 'is-function t)))
1088 (c-forward-declarator limit t)
1089 (eq (char-after) ?,))
1090 (forward-char)
1091 (c-forward-syntactic-ws))
1092 t)))))
1093 'font-lock-function-name-face
1094 'font-lock-variable-name-face)
1095 got-init (and (cadr (cddr decl-res)) ; got-init
1096 (char-after)))
1098 (if types
1099 ;; Register and fontify the identifier as a type.
1100 (let ((c-promote-possible-types t))
1101 (goto-char id-start)
1102 (c-forward-type))
1103 ;; Fontify the last symbol in the identifier if it isn't fontified
1104 ;; already. The check is necessary only in certain cases where this
1105 ;; function is used "sloppily", e.g. in `c-simple-decl-matchers'.
1106 (when (and c-last-identifier-range
1107 (not (get-text-property (car c-last-identifier-range)
1108 'face)))
1109 (c-put-font-lock-face (car c-last-identifier-range)
1110 (cdr c-last-identifier-range)
1111 id-face)))
1113 (goto-char next-pos)
1114 (setq pos nil) ; So as to terminate the enclosing `while' form.
1115 (when list
1116 ;; Jump past any initializer or function prototype to see if
1117 ;; there's a ',' to continue at.
1118 (cond ((eq id-face 'font-lock-function-name-face)
1119 ;; Skip a parenthesized initializer (C++) or a function
1120 ;; prototype.
1121 (if (c-safe (c-forward-sexp 1) t) ; over the parameter list.
1122 (c-forward-syntactic-ws limit)
1123 (goto-char limit))) ; unbalanced parens
1125 (got-init ; "=" sign OR opening "(", "[", or "{"
1126 ;; Skip an initializer expression. If we're at a '='
1127 ;; then accept a brace list directly after it to cope
1128 ;; with array initializers. Otherwise stop at braces
1129 ;; to avoid going past full function and class blocks.
1130 (and (if (and (eq got-init ?=)
1131 (= (c-forward-token-2 1 nil limit) 0)
1132 (looking-at "{"))
1133 (c-safe (c-forward-sexp) t) ; over { .... }
1135 (< (point) limit)
1136 ;; FIXME: Should look for c-decl-end markers here;
1137 ;; we might go far into the following declarations
1138 ;; in e.g. ObjC mode (see e.g. methods-4.m).
1139 (c-syntactic-re-search-forward "[;,{]" limit 'move t)
1140 (backward-char)))
1142 (t (c-forward-syntactic-ws limit)))
1144 ;; If a ',' is found we set pos to the next declarator and iterate.
1145 (when (and (< (point) limit) (looking-at ","))
1146 (c-put-char-property (point) 'c-type separator-prop)
1147 (forward-char)
1148 (c-forward-syntactic-ws limit)
1149 (setq pos (point)))))) ; acts to make the `while' form continue.
1150 nil)
1152 (defun c-get-fontification-context (match-pos not-front-decl &optional toplev)
1153 ;; Return a cons (CONTEXT . RESTRICTED-<>-ARGLISTS) for MATCH-POS.
1154 ;; NOT-FRONT-DECL is non-nil when a declaration later in the buffer than
1155 ;; MATCH-POS has already been parsed. TOPLEV is non-nil when MATCH-POS is
1156 ;; known to be at "top level", i.e. outside any braces, or directly inside a
1157 ;; namespace, class, etc.
1159 ;; CONTEXT is the fontification context of MATCH-POS, and is one of the
1160 ;; following:
1161 ;; 'decl In a comma-separated declaration context (typically
1162 ;; inside a function declaration arglist).
1163 ;; '<> In an angle bracket arglist.
1164 ;; 'arglist Some other type of arglist.
1165 ;; 'top Some other context and point is at the top-level (either
1166 ;; outside any braces or directly inside a class or namespace,
1167 ;; etc.)
1168 ;; nil Some other context or unknown context. Includes
1169 ;; within the parens of an if, for, ... construct.
1170 ;; 'not-decl Definitely not in a declaration.
1172 ;; RESTRICTED-<>-ARGLISTS is non-nil when a scan of template/generic
1173 ;; arguments lists (i.e. lists enclosed by <...>) is more strict about what
1174 ;; characters it allows within the list.
1175 (let ((type (and (> match-pos (point-min))
1176 (c-get-char-property (1- match-pos) 'c-type))))
1177 (cond ((not (memq (char-before match-pos) '(?\( ?, ?\[ ?< ?{)))
1178 (cons (and toplev 'top) nil))
1179 ;; A control flow expression or a decltype
1180 ((and (eq (char-before match-pos) ?\()
1181 (save-excursion
1182 (goto-char match-pos)
1183 (backward-char)
1184 (c-backward-token-2)
1185 (cond
1186 ((looking-at c-paren-stmt-key)
1187 ;; Allow comma separated <> arglists in for statements.
1188 (cons nil nil))
1189 ((or (looking-at c-block-stmt-2-key)
1190 (looking-at c-block-stmt-1-2-key)
1191 (looking-at c-typeof-key))
1192 (cons nil t))
1193 (t nil)))))
1194 ;; Near BOB.
1195 ((<= match-pos (point-min))
1196 (cons 'arglist t))
1197 ;; Got a cached hit in a declaration arglist.
1198 ((eq type 'c-decl-arg-start)
1199 (cons 'decl nil))
1200 ;; We're inside (probably) a brace list.
1201 ((eq type 'c-not-decl)
1202 (cons 'not-decl nil))
1203 ;; Inside a C++11 lambda function arglist.
1204 ((and (c-major-mode-is 'c++-mode)
1205 (eq (char-before match-pos) ?\()
1206 (save-excursion
1207 (goto-char match-pos)
1208 (c-backward-token-2)
1209 (and
1210 (c-safe (goto-char (scan-sexps (point) -1)))
1211 (c-looking-at-c++-lambda-capture-list))))
1212 (c-put-char-property (1- match-pos) 'c-type
1213 'c-decl-arg-start)
1214 (cons 'decl nil))
1215 ;; We're inside a brace list.
1216 ((and (eq (char-before match-pos) ?{)
1217 (save-excursion
1218 (goto-char (1- match-pos))
1219 (consp
1220 (c-looking-at-or-maybe-in-bracelist))))
1221 (c-put-char-property (1- match-pos) 'c-type
1222 'c-not-decl)
1223 (cons 'not-decl nil))
1224 ;; We're inside an "ordinary" open brace.
1225 ((eq (char-before match-pos) ?{)
1226 (cons (and toplev 'top) nil))
1227 ;; Inside an angle bracket arglist.
1228 ((or (eq type 'c-<>-arg-sep)
1229 (eq (char-before match-pos) ?<))
1230 (cons '<> nil))
1231 ;; Got a cached hit in some other type of arglist.
1232 (type
1233 (cons 'arglist t))
1234 ((and not-front-decl
1235 ;; The point is within the range of a previously
1236 ;; encountered type decl expression, so the arglist
1237 ;; is probably one that contains declarations.
1238 ;; However, if `c-recognize-paren-inits' is set it
1239 ;; might also be an initializer arglist.
1240 (or (not c-recognize-paren-inits)
1241 (save-excursion
1242 (goto-char match-pos)
1243 (not (c-back-over-member-initializers)))))
1244 ;; The result of this check is cached with a char
1245 ;; property on the match token, so that we can look
1246 ;; it up again when refontifying single lines in a
1247 ;; multiline declaration.
1248 (c-put-char-property (1- match-pos)
1249 'c-type 'c-decl-arg-start)
1250 (cons 'decl nil))
1251 ;; Got an open paren preceded by an arith operator.
1252 ((and (eq (char-before match-pos) ?\()
1253 (save-excursion
1254 (goto-char match-pos)
1255 (and (zerop (c-backward-token-2 2))
1256 (looking-at c-arithmetic-op-regexp))))
1257 (cons nil nil))
1258 ;; In a C++ member initialization list.
1259 ((and (eq (char-before match-pos) ?,)
1260 (c-major-mode-is 'c++-mode)
1261 (save-excursion
1262 (goto-char match-pos)
1263 (c-back-over-member-initializers)))
1264 (c-put-char-property (1- match-pos) 'c-type 'c-not-decl)
1265 (cons 'not-decl nil))
1266 ;; At start of a declaration inside a declaration paren.
1267 ((save-excursion
1268 (goto-char match-pos)
1269 (and (memq (char-before match-pos) '(?\( ?\,))
1270 (c-go-up-list-backward match-pos)
1271 (eq (char-after) ?\()
1272 (let ((type (c-get-char-property (point) 'c-type)))
1273 (or (memq type '(c-decl-arg-start c-decl-type-start))
1274 (and
1275 (progn (c-backward-syntactic-ws) t)
1276 (c-back-over-compound-identifier)
1277 (progn
1278 (c-backward-syntactic-ws)
1279 (or (bobp)
1280 (progn
1281 (setq type (c-get-char-property (1- (point))
1282 'c-type))
1283 (memq type '(c-decl-arg-start
1284 c-decl-type-start))))))))))
1285 (cons 'decl nil))
1286 (t (cons 'arglist t)))))
1288 (defun c-font-lock-single-decl (limit decl-or-cast match-pos context toplev)
1289 ;; Try to fontify a single declaration, together with all its declarators.
1290 ;; Return nil if we're successful, non-nil if we fail. POINT should be
1291 ;; positioned at the start of the putative declaration before calling.
1292 ;; POINT is left undefined by this function.
1294 ;; LIMIT sets a maximum position we'll fontify out to.
1295 ;; DECL-OR-CAST has the form of a result from `c-forward-decl-or-cast-1',
1296 ;; and must indicate a declaration (i.e. not be nil or 'cast).
1297 ;; MATCH-POS is the position after the last symbol before the decl.
1298 ;; CONTEXT is the context of the current decl., as determined by
1299 ;; c-get-fontification-context.
1300 ;; TOPLEV is non-nil if the decl. is at the top level (i.e. outside any
1301 ;; braces, or directly inside a class, namespace, etc.)
1303 ;; Do we have an expression as the second or third clause of
1304 ;; a "for" paren expression?
1305 (if (save-excursion
1306 (and
1307 (car (cddr decl-or-cast)) ; maybe-expression flag.
1308 (c-go-up-list-backward)
1309 (eq (char-after) ?\()
1310 (progn (c-backward-syntactic-ws)
1311 (c-simple-skip-symbol-backward))
1312 (looking-at c-paren-stmt-key)
1313 (progn (goto-char match-pos)
1314 (while (and (eq (char-before) ?\))
1315 (c-go-list-backward))
1316 (c-backward-syntactic-ws))
1317 (eq (char-before) ?\;))))
1318 ;; We've got an expression in "for" parens. Remove the
1319 ;; "type" that would spuriously get fontified.
1320 (let ((elt (and (consp c-record-type-identifiers)
1321 (assq (cadr (cddr decl-or-cast))
1322 c-record-type-identifiers))))
1323 (when elt
1324 (setq c-record-type-identifiers
1325 (c-delq-from-dotted-list
1326 elt c-record-type-identifiers)))
1328 ;; Back up to the type to fontify the declarator(s).
1329 (goto-char (car decl-or-cast))
1331 (let ((decl-list
1332 (if (not (memq context '(nil top)))
1333 ;; Should normally not fontify a list of
1334 ;; declarators inside an arglist, but the first
1335 ;; argument in the ';' separated list of a "for"
1336 ;; statement is an exception.
1337 (when (eq (char-before match-pos) ?\()
1338 (save-excursion
1339 (goto-char (1- match-pos))
1340 (c-backward-syntactic-ws)
1341 (and (c-simple-skip-symbol-backward)
1342 (looking-at c-paren-stmt-key))))
1343 t)))
1345 ;; Fix the `c-decl-id-start' or `c-decl-type-start' property
1346 ;; before the first declarator if it's a list.
1347 ;; `c-font-lock-declarators' handles the rest.
1348 (when decl-list
1349 (save-excursion
1350 (c-backward-syntactic-ws)
1351 (unless (bobp)
1352 (c-put-char-property (1- (point)) 'c-type
1353 (if (cadr decl-or-cast)
1354 'c-decl-type-start
1355 'c-decl-id-start)))))
1357 (c-font-lock-declarators
1358 (min limit (point-max)) decl-list
1359 (cadr decl-or-cast) (not toplev)))
1361 ;; A declaration has been successfully identified, so do all the
1362 ;; fontification of types and refs that've been recorded.
1363 (c-fontify-recorded-types-and-refs)
1364 nil))
1367 (defun c-font-lock-declarations (limit)
1368 ;; Fontify all the declarations, casts and labels from the point to LIMIT.
1369 ;; Assumes that strings and comments have been fontified already.
1371 ;; This function will be called from font-lock for a region bounded by POINT
1372 ;; and LIMIT, as though it were to identify a keyword for
1373 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
1374 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
1375 ;; Fontification".
1377 ;; This function might do hidden buffer changes.
1379 ;;(message "c-font-lock-declarations search from %s to %s" (point) limit)
1381 (save-restriction
1382 (let (;; The position where `c-find-decl-spots' last stopped.
1383 start-pos
1384 ;; o - 'decl if we're in an arglist containing declarations
1385 ;; (but if `c-recognize-paren-inits' is set it might also be
1386 ;; an initializer arglist);
1387 ;; o - '<> if the arglist is of angle bracket type;
1388 ;; o - 'arglist if it's some other arglist;
1389 ;; o - nil, if not in an arglist at all. This includes the
1390 ;; parenthesized condition which follows "if", "while", etc.
1391 context
1392 ;; A list of starting positions of possible type declarations, or of
1393 ;; the typedef preceding one, if any.
1394 last-cast-end
1395 ;; The result from `c-forward-decl-or-cast-1'.
1396 decl-or-cast
1397 ;; The maximum of the end positions of all the checked type
1398 ;; decl expressions in the successfully identified
1399 ;; declarations. The position might be either before or
1400 ;; after the syntactic whitespace following the last token
1401 ;; in the type decl expression.
1402 (max-type-decl-end 0)
1403 ;; Same as `max-type-decl-*', but used when we're before
1404 ;; `token-pos'.
1405 (max-type-decl-end-before-token 0)
1406 ;; End of <..> construct which has had c-<>-arg-sep c-type
1407 ;; properties set within it.
1408 (max-<>-end 0)
1409 ;; Set according to the context to direct the heuristics for
1410 ;; recognizing C++ templates.
1411 c-restricted-<>-arglists
1412 ;; Turn on recording of identifier ranges in
1413 ;; `c-forward-decl-or-cast-1' and `c-forward-label' for
1414 ;; later fontification.
1415 (c-record-type-identifiers t)
1416 label-type
1417 c-record-ref-identifiers
1418 ;; Make `c-forward-type' calls mark up template arglists if
1419 ;; it finds any. That's necessary so that we later will
1420 ;; stop inside them to fontify types there.
1421 (c-parse-and-markup-<>-arglists t)
1422 ;; The font-lock package in Emacs is known to clobber
1423 ;; `parse-sexp-lookup-properties' (when it exists).
1424 (parse-sexp-lookup-properties
1425 (cc-eval-when-compile
1426 (boundp 'parse-sexp-lookup-properties))
1429 ;; Below we fontify a whole declaration even when it crosses the limit,
1430 ;; to avoid gaps when jit/lazy-lock fontifies the file a block at a
1431 ;; time. That is however annoying during editing, e.g. the following is
1432 ;; a common situation while the first line is being written:
1434 ;; my_variable
1435 ;; some_other_variable = 0;
1437 ;; font-lock will put the limit at the beginning of the second line
1438 ;; here, and if we go past it we'll fontify "my_variable" as a type and
1439 ;; "some_other_variable" as an identifier, and the latter will not
1440 ;; correct itself until the second line is changed. To avoid that we
1441 ;; narrow to the limit if the region to fontify is a single line.
1442 (if (<= limit (c-point 'bonl))
1443 (narrow-to-region
1444 (point-min)
1445 (save-excursion
1446 ;; Narrow after any operator chars following the limit though,
1447 ;; since those characters can be useful in recognizing a
1448 ;; declaration (in particular the '{' that opens a function body
1449 ;; after the header).
1450 (goto-char limit)
1451 (skip-chars-forward c-nonsymbol-chars)
1452 (point))))
1454 (c-find-decl-spots
1455 limit
1456 c-decl-start-re
1457 (eval c-maybe-decl-faces)
1459 (lambda (match-pos inside-macro &optional toplev)
1460 ;; Note to maintainers: don't use `limit' inside this lambda form;
1461 ;; c-find-decl-spots sometimes narrows to less than `limit'.
1462 (setq start-pos (point))
1463 (when
1464 ;; The result of the form below is true when we don't recognize a
1465 ;; declaration or cast, and we don't recognize a "non-decl",
1466 ;; typically a brace list.
1467 (if (or (and (eq (get-text-property (point) 'face)
1468 'font-lock-keyword-face)
1469 (looking-at c-not-decl-init-keywords))
1470 (and c-macro-with-semi-re
1471 (looking-at c-macro-with-semi-re))) ; 2008-11-04
1472 ;; Don't do anything more if we're looking at a keyword that
1473 ;; can't start a declaration.
1476 ;; Set `context' and `c-restricted-<>-arglists'. Look for
1477 ;; "<" for the sake of C++-style template arglists.
1478 ;; Ignore "(" when it's part of a control flow construct
1479 ;; (e.g. "for (").
1480 (let ((got-context
1481 (c-get-fontification-context
1482 match-pos
1483 (< match-pos (if inside-macro
1484 max-type-decl-end-before-token
1485 max-type-decl-end))
1486 toplev)))
1487 (setq context (car got-context)
1488 c-restricted-<>-arglists (cdr got-context)))
1490 ;; Check we haven't missed a preceding "typedef".
1491 (when (not (looking-at c-typedef-key))
1492 (c-backward-syntactic-ws)
1493 (c-backward-token-2)
1494 (or (looking-at c-typedef-key)
1495 (goto-char start-pos)))
1497 ;; In QT, "more" is an irritating keyword that expands to nothing.
1498 ;; We skip over it to prevent recognition of "more slots: <symbol>"
1499 ;; as a bitfield declaration.
1500 (when (and (c-major-mode-is 'c++-mode)
1501 (looking-at
1502 (concat "\\(more\\)\\([^" c-symbol-chars "]\\|$\\)")))
1503 (goto-char (match-end 1))
1504 (c-forward-syntactic-ws))
1506 ;; Now analyze the construct.
1507 (if (eq context 'not-decl)
1508 (progn
1509 (setq decl-or-cast nil)
1510 (if (c-syntactic-re-search-forward
1511 "," (min limit (point-max)) 'at-limit t)
1512 (c-put-char-property (1- (point)) 'c-type 'c-not-decl))
1513 nil)
1514 (setq decl-or-cast
1515 (c-forward-decl-or-cast-1
1516 match-pos context last-cast-end))
1518 ;; Ensure that c-<>-arg-sep c-type properties are in place on the
1519 ;; commas separating the arguments inside template/generic <..>s.
1520 (when (and (eq (char-before match-pos) ?<)
1521 (> match-pos max-<>-end))
1522 (save-excursion
1523 (goto-char match-pos)
1524 (c-backward-token-2)
1525 (if (and
1526 (eq (char-after) ?<)
1527 (let ((c-restricted-<>-arglists
1528 (save-excursion
1529 (c-backward-token-2)
1530 (and
1531 (not (looking-at c-opt-<>-sexp-key))
1532 (progn (c-backward-syntactic-ws)
1533 (memq (char-before) '(?\( ?,)))
1534 (not (eq (c-get-char-property (1- (point))
1535 'c-type)
1536 'c-decl-arg-start))))))
1537 (c-forward-<>-arglist nil)))
1538 (setq max-<>-end (point)))))
1540 (cond
1541 ((eq decl-or-cast 'cast)
1542 ;; Save the position after the previous cast so we can feed
1543 ;; it to `c-forward-decl-or-cast-1' in the next round. That
1544 ;; helps it discover cast chains like "(a) (b) c".
1545 (setq last-cast-end (point))
1546 (c-fontify-recorded-types-and-refs)
1547 nil)
1549 (decl-or-cast
1550 ;; We've found a declaration.
1552 ;; Set `max-type-decl-end' or `max-type-decl-end-before-token'
1553 ;; under the assumption that we're after the first type decl
1554 ;; expression in the declaration now. That's not really true;
1555 ;; we could also be after a parenthesized initializer
1556 ;; expression in C++, but this is only used as a last resort
1557 ;; to slant ambiguous expression/declarations, and overall
1558 ;; it's worth the risk to occasionally fontify an expression
1559 ;; as a declaration in an initializer expression compared to
1560 ;; getting ambiguous things in normal function prototypes
1561 ;; fontified as expressions.
1562 (if inside-macro
1563 (when (> (point) max-type-decl-end-before-token)
1564 (setq max-type-decl-end-before-token (point)))
1565 (when (> (point) max-type-decl-end)
1566 (setq max-type-decl-end (point))))
1567 (goto-char start-pos)
1568 (c-font-lock-single-decl limit decl-or-cast match-pos
1569 context toplev))
1571 (t t))))
1573 ;; It was a false alarm. Check if we're in a label (or other
1574 ;; construct with `:' except bitfield) instead.
1575 (goto-char start-pos)
1576 (when (setq label-type (c-forward-label t match-pos nil))
1577 ;; Can't use `c-fontify-types-and-refs' here since we
1578 ;; use the label face at times.
1579 (cond ((eq label-type 'goto-target)
1580 (c-put-font-lock-face (caar c-record-ref-identifiers)
1581 (cdar c-record-ref-identifiers)
1582 c-label-face-name))
1583 ((eq label-type 'qt-1kwd-colon)
1584 (c-put-font-lock-face (caar c-record-ref-identifiers)
1585 (cdar c-record-ref-identifiers)
1586 'font-lock-keyword-face))
1587 ((eq label-type 'qt-2kwds-colon)
1588 (mapc
1589 (lambda (kwd)
1590 (c-put-font-lock-face (car kwd) (cdr kwd)
1591 'font-lock-keyword-face))
1592 c-record-ref-identifiers)))
1593 (setq c-record-ref-identifiers nil)
1594 ;; `c-forward-label' has probably added a `c-decl-end'
1595 ;; marker, so return t to `c-find-decl-spots' to signal
1596 ;; that.
1597 t))))
1599 nil)))
1601 (defun c-font-lock-enum-body (limit)
1602 ;; Fontify the identifiers of each enum we find by searching forward.
1604 ;; This function will be called from font-lock for a region bounded by POINT
1605 ;; and LIMIT, as though it were to identify a keyword for
1606 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
1607 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
1608 ;; Fontification".
1609 (while (search-forward-regexp c-enum-clause-introduction-re limit t)
1610 (when (save-excursion
1611 (backward-char)
1612 (c-backward-over-enum-header))
1613 (c-forward-syntactic-ws)
1614 (c-font-lock-declarators limit t nil t)))
1615 nil)
1617 (defun c-font-lock-enum-tail (limit)
1618 ;; Fontify an enum's identifiers when POINT is within the enum's brace
1619 ;; block.
1621 ;; This function will be called from font-lock for a region bounded by POINT
1622 ;; and LIMIT, as though it were to identify a keyword for
1623 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
1624 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
1625 ;; Fontification".
1627 ;; Note that this function won't attempt to fontify beyond the end of the
1628 ;; current enum block, if any.
1629 (let* ((paren-state (c-parse-state))
1630 (encl-pos (c-most-enclosing-brace paren-state)))
1631 (when (and
1632 encl-pos
1633 (eq (char-after encl-pos) ?\{)
1634 (save-excursion
1635 (goto-char encl-pos)
1636 (c-backward-over-enum-header)))
1637 (c-syntactic-skip-backward "^{," nil t)
1638 (c-put-char-property (1- (point)) 'c-type 'c-decl-id-start)
1640 (c-forward-syntactic-ws)
1641 (c-font-lock-declarators limit t nil t)))
1642 nil)
1644 (defun c-font-lock-cut-off-declarators (limit)
1645 ;; Fontify any declarators "cut off" from their declaring type at the start
1646 ;; of the region being fontified.
1648 ;; This function will be called from font-lock- for a region bounded by
1649 ;; POINT and LIMIT, as though it were to identify a keyword for
1650 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
1651 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
1652 ;; fontification".
1653 (let ((decl-search-lim (c-determine-limit 1000))
1654 paren-state encl-pos token-end context decl-or-cast
1655 start-pos top-level c-restricted-<>-arglists
1656 c-recognize-knr-p) ; Strictly speaking, bogus, but it
1657 ; speeds up lisp.h tremendously.
1658 (save-excursion
1659 (when (not (c-back-over-member-initializers))
1660 (unless (or (eobp)
1661 (looking-at "\\s(\\|\\s)"))
1662 (forward-char))
1663 (c-syntactic-skip-backward "^;{}" decl-search-lim t)
1664 (when (eq (char-before) ?})
1665 (c-go-list-backward) ; brace block of struct, etc.?
1666 (c-syntactic-skip-backward "^;{}" decl-search-lim t))
1667 (when (or (bobp)
1668 (memq (char-before) '(?\; ?{ ?})))
1669 (setq token-end (point))
1670 (c-forward-syntactic-ws)
1671 ;; We're now putatively at the declaration.
1672 (setq start-pos (point))
1673 (setq paren-state (c-parse-state))
1674 ;; At top level or inside a "{"?
1675 (if (or (not (setq encl-pos
1676 (c-most-enclosing-brace paren-state)))
1677 (eq (char-after encl-pos) ?\{))
1678 (progn
1679 (setq top-level (c-at-toplevel-p))
1680 (let ((got-context (c-get-fontification-context
1681 token-end nil top-level)))
1682 (setq context (car got-context)
1683 c-restricted-<>-arglists (cdr got-context)))
1684 (setq decl-or-cast
1685 (c-forward-decl-or-cast-1 token-end context nil))
1686 (when (consp decl-or-cast)
1687 (goto-char start-pos)
1688 (c-font-lock-single-decl limit decl-or-cast token-end
1689 context top-level)))))))
1690 nil))
1692 (defun c-font-lock-enclosing-decls (limit)
1693 ;; Fontify the declarators of (nested) declarations we're in the middle of.
1694 ;; This is mainly for when a jit-lock etc. chunk starts inside the brace
1695 ;; block of a struct/union/class, etc.
1697 ;; This function will be called from font-lock for a region bounded by POINT
1698 ;; and LIMIT, as though it were to identify a keyword for
1699 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
1700 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
1701 ;; Fontification".
1702 (let* ((paren-state (c-parse-state))
1703 (decl-search-lim (c-determine-limit 1000))
1704 in-typedef ps-elt)
1705 ;; Are we in any nested struct/union/class/etc. braces?
1706 (while paren-state
1707 (setq ps-elt (car paren-state)
1708 paren-state (cdr paren-state))
1709 (when (and (atom ps-elt)
1710 (eq (char-after ps-elt) ?\{))
1711 (goto-char ps-elt)
1712 (c-syntactic-skip-backward "^;{}" decl-search-lim)
1713 (when (or (bobp)
1714 (memq (char-before) '(?\; ?})))
1715 (c-forward-syntactic-ws)
1716 (setq in-typedef (looking-at c-typedef-key))
1717 (if in-typedef (c-forward-token-2))
1718 (when (and c-opt-block-decls-with-vars-key
1719 (looking-at c-opt-block-decls-with-vars-key))
1720 (goto-char ps-elt)
1721 (when (c-safe (c-forward-sexp))
1722 (c-forward-syntactic-ws)
1723 (c-font-lock-declarators limit t in-typedef
1724 (not (c-bs-at-toplevel-p (point)))))))))))
1726 (defun c-font-lock-raw-strings (limit)
1727 ;; Fontify C++ raw strings.
1729 ;; This function will be called from font-lock for a region bounded by POINT
1730 ;; and LIMIT, as though it were to identify a keyword for
1731 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
1732 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
1733 ;; Fontification".
1734 (let* ((state (c-state-semi-pp-to-literal (point)))
1735 (string-start (and (eq (cadr state) 'string)
1736 (car (cddr state))))
1737 (raw-id (and string-start
1738 (save-excursion
1739 (goto-char string-start)
1740 (and (eq (char-before) ?R)
1741 (looking-at "\"\\([^ ()\\\n\r\t]\\{0,16\\}\\)(")
1742 (match-string-no-properties 1))))))
1743 (while (< (point) limit)
1744 (if raw-id
1745 (progn
1746 (if (search-forward-regexp (concat ")\\(" (regexp-quote raw-id) "\\)\"")
1747 limit 'limit)
1748 (c-put-font-lock-face (match-beginning 1) (point) 'default))
1749 (setq raw-id nil))
1751 (when (search-forward-regexp
1752 "R\\(\"\\)\\([^ ()\\\n\r\t]\\{0,16\\}\\)(" limit 'limit)
1753 (when
1754 (or (and (eobp)
1755 (eq (c-get-char-property (1- (point)) 'face)
1756 'font-lock-warning-face))
1757 (eq (c-get-char-property (point) 'face) 'font-lock-string-face)
1758 (and (equal (c-get-char-property (match-end 2) 'syntax-table) '(1))
1759 (equal (c-get-char-property (match-beginning 1) 'syntax-table)
1760 '(1))))
1761 (let ((paren-prop (c-get-char-property (1- (point)) 'syntax-table)))
1762 (if paren-prop
1763 (progn
1764 (c-put-font-lock-face (match-beginning 0) (match-end 0)
1765 'font-lock-warning-face)
1766 (when
1767 (and
1768 (equal paren-prop '(15))
1769 (not (c-search-forward-char-property 'syntax-table '(15) limit)))
1770 (goto-char limit)))
1771 (c-put-font-lock-face (match-beginning 1) (match-end 2) 'default)
1772 (setq raw-id (match-string-no-properties 2)))))))))
1773 nil)
1775 (defun c-font-lock-c++-lambda-captures (limit)
1776 ;; Fontify the lambda capture component of C++ lambda declarations.
1778 ;; This function will be called from font-lock for a region bounded by POINT
1779 ;; and LIMIT, as though it were to identify a keyword for
1780 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
1781 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
1782 ;; Fontification".
1783 (let (mode capture-default id-start id-end declaration sub-begin sub-end)
1784 (while (and (< (point) limit)
1785 (search-forward "[" limit t))
1786 (when (progn (backward-char)
1787 (prog1
1788 (c-looking-at-c++-lambda-capture-list)
1789 (forward-char)))
1790 (c-forward-syntactic-ws)
1791 (setq mode (and (memq (char-after) '(?= ?&))
1792 (char-after)))
1793 ;; Is the first element of the list a bare "=" or "&"?
1794 (when mode
1795 (forward-char)
1796 (c-forward-syntactic-ws)
1797 (if (memq (char-after) '(?, ?\]))
1798 (progn
1799 (setq capture-default mode)
1800 (when (eq (char-after) ?,)
1801 (forward-char)
1802 (c-forward-syntactic-ws)))
1803 (c-backward-token-2)))
1805 ;; Go round the following loop once per captured item. We use "\\s)"
1806 ;; rather than "\\]" here to avoid infinite looping in this situation:
1807 ;; "unsigned items [] { [ }". The second "[" triggers this function,
1808 ;; but if we don't match the "}" with an "\\s)", the
1809 ;; `c-syntactic-re-search-forward' at the end of the loop fails to
1810 ;; move forward over it, leaving point stuck at the "}".
1811 (while (and (not (looking-at "\\s)"))
1812 (< (point) limit))
1813 (if (eq (char-after) ?&)
1814 (progn (setq mode ?&)
1815 (forward-char)
1816 (c-forward-syntactic-ws))
1817 (setq mode ?=))
1818 (if (c-on-identifier)
1819 (progn
1820 (setq id-start (point))
1821 (forward-char)
1822 (c-end-of-current-token)
1823 (setq id-end (point))
1824 (c-forward-syntactic-ws)
1826 (setq declaration (eq (char-after) ?=))
1827 (when declaration
1828 (forward-char) ; over "="
1829 (c-forward-syntactic-ws)
1830 (setq sub-begin (point)))
1831 (if (or (and (< (point) limit)
1832 (c-syntactic-re-search-forward "," limit t t))
1833 (and (c-go-up-list-forward nil limit)
1834 (eq (char-before) ?\])))
1835 (backward-char)
1836 (goto-char limit))
1837 (when declaration
1838 (save-excursion
1839 (setq sub-end (point))
1840 (goto-char sub-begin)
1841 (c-font-lock-c++-lambda-captures sub-end)))
1843 (c-put-font-lock-face id-start id-end
1844 (cond
1845 (declaration
1846 'font-lock-variable-name-face)
1847 ((and capture-default
1848 (eq mode capture-default))
1849 'font-lock-warning-face)
1850 ((eq mode ?=) font-lock-constant-face)
1851 (t 'font-lock-variable-name-face))))
1852 (c-syntactic-re-search-forward "," limit 'bound t))
1854 (c-forward-syntactic-ws)
1855 (when (eq (char-after) ?,)
1856 (forward-char)
1857 (c-forward-syntactic-ws)))
1859 (setq capture-default nil)
1860 (if (< (point) limit)
1861 (forward-char))))) ; over the terminating "]" or other close paren.
1862 nil)
1865 (c-lang-defconst c-simple-decl-matchers
1866 "Simple font lock matchers for types and declarations. These are used
1867 on level 2 only and so aren't combined with `c-complex-decl-matchers'."
1869 t `(;; Objective-C methods.
1870 ,@(when (c-major-mode-is 'objc-mode)
1871 `((,(c-lang-const c-opt-method-key)
1872 (,(byte-compile
1873 (lambda (limit)
1874 (let (;; The font-lock package in Emacs is known to clobber
1875 ;; `parse-sexp-lookup-properties' (when it exists).
1876 (parse-sexp-lookup-properties
1877 (cc-eval-when-compile
1878 (boundp 'parse-sexp-lookup-properties))))
1879 (save-restriction
1880 (narrow-to-region (point-min) limit)
1881 (c-font-lock-objc-method)))
1882 nil))
1883 (goto-char (match-end 1))))))
1885 ;; Fontify all type names and the identifiers in the
1886 ;; declarations they might start. Use eval here since
1887 ;; `c-known-type-key' gets its value from
1888 ;; `*-font-lock-extra-types' on mode init.
1889 (eval . (list ,(c-make-font-lock-search-function
1890 'c-known-type-key
1891 '(1 'font-lock-type-face t)
1892 '((c-font-lock-declarators limit t nil nil)
1893 (save-match-data
1894 (goto-char (match-end 1))
1895 (c-forward-syntactic-ws))
1896 (goto-char (match-end 1))))))
1898 ;; Fontify types preceded by `c-type-prefix-kwds' and the
1899 ;; identifiers in the declarations they might start.
1900 ,@(when (c-lang-const c-type-prefix-kwds)
1901 (let* ((prefix-re (c-make-keywords-re nil
1902 (c-lang-const c-type-prefix-kwds)))
1903 (type-match (+ 2
1904 (regexp-opt-depth prefix-re)
1905 (c-lang-const c-simple-ws-depth))))
1906 `((,(c-make-font-lock-search-function
1907 (concat "\\<\\(" prefix-re "\\)" ; 1
1908 (c-lang-const c-simple-ws) "+"
1909 (concat "\\(" ; 2 + prefix-re + c-simple-ws
1910 (c-lang-const c-symbol-key)
1911 "\\)"))
1912 `(,type-match
1913 'font-lock-type-face t)
1914 `((c-font-lock-declarators limit t nil nil)
1915 (save-match-data
1916 (goto-char (match-end ,type-match))
1917 (c-forward-syntactic-ws))
1918 (goto-char (match-end ,type-match))))))))
1920 ;; Fontify special declarations that lacks a type.
1921 ,@(when (c-lang-const c-typeless-decl-kwds)
1922 `((,(c-make-font-lock-search-function
1923 (concat "\\<\\("
1924 (regexp-opt (c-lang-const c-typeless-decl-kwds))
1925 "\\)\\>")
1926 '((c-font-lock-declarators limit t nil nil)
1927 (save-match-data
1928 (goto-char (match-end 1))
1929 (c-forward-syntactic-ws))
1930 (goto-char (match-end 1)))))))
1932 ;; Fontify generic colon labels in languages that support them.
1933 ,@(when (c-lang-const c-recognize-colon-labels)
1934 `(c-font-lock-labels))))
1936 (c-lang-defconst c-complex-decl-matchers
1937 "Complex font lock matchers for types and declarations. Used on level
1938 3 and higher."
1940 ;; Note: This code in this form dumps a number of functions into the
1941 ;; resulting constant, `c-matchers-3'. At run time, font lock will call
1942 ;; each of them as a "FUNCTION" (see Elisp page "Search-based
1943 ;; Fontification"). The font lock region is delimited by POINT and the
1944 ;; single parameter, LIMIT. Each of these functions returns NIL (thus
1945 ;; inhibiting spurious font-lock-keyword-face highlighting and another
1946 ;; call).
1948 t `(;; Initialize some things before the search functions below.
1949 c-font-lock-complex-decl-prepare
1951 ,@(if (c-major-mode-is 'objc-mode)
1952 ;; Fontify method declarations in Objective-C, but first
1953 ;; we have to put the `c-decl-end' `c-type' property on
1954 ;; all the @-style directives that haven't been handled in
1955 ;; `c-basic-matchers-before'.
1956 `(,(c-make-font-lock-search-function
1957 (c-make-keywords-re t
1958 ;; Exclude "@class" since that directive ends with a
1959 ;; semicolon anyway.
1960 (delete "@class"
1961 (append (c-lang-const c-protection-kwds)
1962 (c-lang-const c-other-decl-kwds)
1963 nil)))
1964 '((c-put-char-property (1- (match-end 1))
1965 'c-type 'c-decl-end)))
1966 c-font-lock-objc-methods))
1968 ;; Fontify declarators which have been cut off from their declaring
1969 ;; types at the start of the region.
1970 c-font-lock-cut-off-declarators
1972 ;; Fontify all declarations, casts and normal labels.
1973 c-font-lock-declarations
1975 ;; Fontify declarators when POINT is within their declaration.
1976 c-font-lock-enclosing-decls
1978 ;; Fontify angle bracket arglists like templates in C++.
1979 ,@(when (c-lang-const c-recognize-<>-arglists)
1980 `(c-font-lock-<>-arglists))
1982 ,@(when (c-major-mode-is 'c++-mode)
1983 `(c-font-lock-c++-lambda-captures))
1985 ;; The first two rules here mostly find occurrences that
1986 ;; `c-font-lock-declarations' has found already, but not
1987 ;; declarations containing blocks in the type (see note below).
1988 ;; It's also useful to fontify these everywhere to show e.g. when
1989 ;; a type keyword is accidentally used as an identifier.
1991 ;; Fontify basic types.
1992 ,(let ((re (c-make-keywords-re nil
1993 (c-lang-const c-primitive-type-kwds))))
1994 (if (c-major-mode-is 'pike-mode)
1995 ;; No symbol is a keyword after "->" in Pike.
1996 `(,(concat "\\(\\=.?\\|[^>]\\|[^-]>\\)"
1997 "\\<\\(" re "\\)\\>")
1998 2 font-lock-type-face)
1999 `(,(concat "\\<\\(" re "\\)\\>")
2000 1 'font-lock-type-face)))
2002 ;; Fontify types preceded by `c-type-prefix-kwds' (e.g. "struct").
2003 ,@(when (c-lang-const c-type-prefix-kwds)
2004 `((,(byte-compile
2005 `(lambda (limit)
2006 (c-fontify-types-and-refs
2007 ((c-promote-possible-types t)
2008 ;; The font-lock package in Emacs is known to clobber
2009 ;; `parse-sexp-lookup-properties' (when it exists).
2010 (parse-sexp-lookup-properties
2011 (cc-eval-when-compile
2012 (boundp 'parse-sexp-lookup-properties))))
2013 (save-restriction
2014 ;; Narrow to avoid going past the limit in
2015 ;; `c-forward-type'.
2016 (narrow-to-region (point) limit)
2017 (while (re-search-forward
2018 ,(concat "\\<\\("
2019 (c-make-keywords-re nil
2020 (c-lang-const c-type-prefix-kwds))
2021 "\\)\\>")
2022 limit t)
2023 (unless (c-skip-comments-and-strings limit)
2024 (c-forward-syntactic-ws)
2025 ;; Handle prefix declaration specifiers.
2026 (while
2028 (when (or (looking-at c-prefix-spec-kwds-re)
2029 (and (c-major-mode-is 'java-mode)
2030 (looking-at "@[A-Za-z0-9]+")))
2031 (c-forward-keyword-clause 1)
2033 (when (and c-opt-cpp-prefix
2034 (looking-at
2035 c-noise-macro-with-parens-name-re))
2036 (c-forward-noise-clause)
2037 t)))
2038 ,(if (c-major-mode-is 'c++-mode)
2039 `(when (and (c-forward-type)
2040 (eq (char-after) ?=))
2041 ;; In C++ we additionally check for a "class
2042 ;; X = Y" construct which is used in
2043 ;; templates, to fontify Y as a type.
2044 (forward-char)
2045 (c-forward-syntactic-ws)
2046 (c-forward-type))
2047 `(c-forward-type))
2048 )))))))))
2050 ;; Fontify symbols after closing braces as declaration
2051 ;; identifiers under the assumption that they are part of
2052 ;; declarations like "class Foo { ... } foo;". It's too
2053 ;; expensive to check this accurately by skipping past the
2054 ;; brace block, so we use the heuristic that it's such a
2055 ;; declaration if the first identifier is on the same line as
2056 ;; the closing brace. `c-font-lock-declarations' will later
2057 ;; override it if it turns out to be an new declaration, but
2058 ;; it will be wrong if it's an expression (see the test
2059 ;; decls-8.cc).
2060 ;; ,@(when (c-lang-const c-opt-block-decls-with-vars-key)
2061 ;; `((,(c-make-font-lock-search-function
2062 ;; (concat "}"
2063 ;; (c-lang-const c-single-line-syntactic-ws)
2064 ;; "\\(" ; 1 + c-single-line-syntactic-ws-depth
2065 ;; (c-lang-const c-type-decl-prefix-key)
2066 ;; "\\|"
2067 ;; (c-lang-const c-symbol-key)
2068 ;; "\\)")
2069 ;; `((c-font-lock-declarators limit t nil) ; That nil says use `font-lock-variable-name-face';
2070 ;; ; t would mean `font-lock-function-name-face'.
2071 ;; (progn
2072 ;; (c-put-char-property (match-beginning 0) 'c-type
2073 ;; 'c-decl-id-start)
2074 ;; ; 'c-decl-type-start)
2075 ;; (goto-char (match-beginning
2076 ;; ,(1+ (c-lang-const
2077 ;; c-single-line-syntactic-ws-depth)))))
2078 ;; (goto-char (match-end 0)))))))
2080 ;; Fontify the type in C++ "new" expressions.
2081 ,@(when (c-major-mode-is 'c++-mode)
2082 ;; This pattern is a probably a "(MATCHER . ANCHORED-HIGHLIGHTER)"
2083 ;; (see Elisp page "Search-based Fontification").
2084 `(("\\<new\\>"
2085 (c-font-lock-c++-new))))
2088 (defun c-font-lock-labels (limit)
2089 ;; Fontify all statement labels from the point to LIMIT. Assumes
2090 ;; that strings and comments have been fontified already. Nil is
2091 ;; always returned.
2093 ;; Note: This function is only used on decoration level 2; this is
2094 ;; taken care of directly by the gargantuan
2095 ;; `c-font-lock-declarations' on higher levels.
2097 ;; This function might do hidden buffer changes.
2099 (let (continue-pos id-start
2100 ;; The font-lock package in Emacs is known to clobber
2101 ;; `parse-sexp-lookup-properties' (when it exists).
2102 (parse-sexp-lookup-properties
2103 (cc-eval-when-compile
2104 (boundp 'parse-sexp-lookup-properties))))
2106 (while (re-search-forward ":[^:]" limit t)
2107 (setq continue-pos (point))
2108 (goto-char (match-beginning 0))
2109 (unless (c-skip-comments-and-strings limit)
2111 (c-backward-syntactic-ws)
2112 (and (setq id-start (c-on-identifier))
2114 (not (get-text-property id-start 'face))
2116 (progn
2117 (goto-char id-start)
2118 (c-backward-syntactic-ws)
2120 ;; Check for a char that precedes a statement.
2121 (memq (char-before) '(?\} ?\{ ?\;))
2122 ;; Check for a preceding label. We exploit the font
2123 ;; locking made earlier by this function.
2124 (and (eq (char-before) ?:)
2125 (progn
2126 (backward-char)
2127 (c-backward-syntactic-ws)
2128 (not (bobp)))
2129 (eq (get-text-property (1- (point)) 'face)
2130 c-label-face-name))
2131 ;; Check for a keyword that precedes a statement.
2132 (c-after-conditional)))
2134 (progn
2135 ;; Got a label.
2136 (goto-char id-start)
2137 (looking-at c-symbol-key)
2138 (c-put-font-lock-face (match-beginning 0) (match-end 0)
2139 c-label-face-name)))
2141 (goto-char continue-pos))))
2142 nil)
2144 (c-lang-defconst c-basic-matchers-after
2145 "Font lock matchers for various things that should be fontified after
2146 generic casts and declarations are fontified. Used on level 2 and
2147 higher."
2149 t `(,@(when (c-lang-const c-brace-list-decl-kwds)
2150 ;; Fontify the remaining identifiers inside an enum list when we start
2151 ;; inside it.
2152 `(c-font-lock-enum-tail
2153 ;; Fontify the identifiers inside enum lists. (The enum type
2154 ;; name is handled by `c-simple-decl-matchers' or
2155 ;; `c-complex-decl-matchers' below.
2156 c-font-lock-enum-body))
2158 ;; Fontify labels after goto etc.
2159 ,@(when (c-lang-const c-before-label-kwds)
2160 `(;; (Got three different interpretation levels here,
2161 ;; which makes it a bit complicated: 1) The backquote
2162 ;; stuff is expanded when compiled or loaded, 2) the
2163 ;; eval form is evaluated at font-lock setup (to
2164 ;; substitute c-label-face-name correctly), and 3) the
2165 ;; resulting structure is interpreted during
2166 ;; fontification.)
2167 (eval
2168 . ,(let* ((c-before-label-re
2169 (c-make-keywords-re nil
2170 (c-lang-const c-before-label-kwds))))
2171 `(list
2172 ,(concat "\\<\\(" c-before-label-re "\\)\\>"
2173 "\\s *"
2174 "\\(" ; identifier-offset
2175 (c-lang-const c-symbol-key)
2176 "\\)")
2177 (list ,(+ (regexp-opt-depth c-before-label-re) 2)
2178 c-label-face-name nil t))))))
2180 ;; Fontify the clauses after various keywords.
2181 ,@(when (or (c-lang-const c-type-list-kwds)
2182 (c-lang-const c-ref-list-kwds)
2183 (c-lang-const c-colon-type-list-kwds))
2184 `((,(c-make-font-lock-BO-decl-search-function
2185 (concat "\\<\\("
2186 (c-make-keywords-re nil
2187 (append (c-lang-const c-type-list-kwds)
2188 (c-lang-const c-ref-list-kwds)
2189 (c-lang-const c-colon-type-list-kwds)))
2190 "\\)\\>")
2191 '((c-fontify-types-and-refs ((c-promote-possible-types t))
2192 (c-forward-keyword-clause 1)
2193 (if (> (point) limit) (goto-char limit))))))))
2195 ,@(when (c-lang-const c-paren-type-kwds)
2196 `((,(c-make-font-lock-search-function
2197 (concat "\\<\\("
2198 (c-make-keywords-re nil
2199 (c-lang-const c-paren-type-kwds))
2200 "\\)\\>")
2201 '((c-fontify-types-and-refs ((c-promote-possible-types t))
2202 (c-forward-keyword-clause 1)
2203 (if (> (point) limit) (goto-char limit))))))))
2205 ,@(when (c-major-mode-is 'java-mode)
2206 `((eval . (list "\\<\\(@[a-zA-Z0-9]+\\)\\>" 1 c-annotation-face))))
2209 (c-lang-defconst c-matchers-1
2210 t (c-lang-const c-cpp-matchers))
2212 (c-lang-defconst c-matchers-2
2213 t (append (c-lang-const c-matchers-1)
2214 (c-lang-const c-basic-matchers-before)
2215 (c-lang-const c-simple-decl-matchers)
2216 (c-lang-const c-basic-matchers-after)))
2218 (c-lang-defconst c-matchers-3
2219 t (append (c-lang-const c-matchers-1)
2220 (c-lang-const c-basic-matchers-before)
2221 (c-lang-const c-complex-decl-matchers)
2222 (c-lang-const c-basic-matchers-after)))
2224 (defun c-compose-keywords-list (base-list)
2225 ;; Incorporate the font lock keyword lists according to
2226 ;; `c-doc-comment-style' on the given keyword list and return it.
2227 ;; This is used in the function bindings of the
2228 ;; `*-font-lock-keywords-*' symbols since we have to build the list
2229 ;; when font-lock is initialized.
2231 (unless (memq c-doc-face-name c-literal-faces)
2232 (setq c-literal-faces (cons c-doc-face-name c-literal-faces)))
2234 (let* ((doc-keywords
2235 (if (consp (car-safe c-doc-comment-style))
2236 (cdr-safe (or (assq c-buffer-is-cc-mode c-doc-comment-style)
2237 (assq 'other c-doc-comment-style)))
2238 c-doc-comment-style))
2239 (list (nconc (c--mapcan
2240 (lambda (doc-style)
2241 (let ((sym (intern
2242 (concat (symbol-name doc-style)
2243 "-font-lock-keywords"))))
2244 (cond ((fboundp sym)
2245 (funcall sym))
2246 ((boundp sym)
2247 (append (eval sym) nil)))))
2248 (if (listp doc-keywords)
2249 doc-keywords
2250 (list doc-keywords)))
2251 base-list)))
2253 ;; Kludge: If `c-font-lock-complex-decl-prepare' is on the list we
2254 ;; move it first since the doc comment font lockers might add
2255 ;; `c-type' text properties, so they have to be cleared before that.
2256 (when (memq 'c-font-lock-complex-decl-prepare list)
2257 (setq list (cons 'c-font-lock-complex-decl-prepare
2258 (delq 'c-font-lock-complex-decl-prepare
2259 (append list nil)))))
2261 list))
2263 (defun c-override-default-keywords (def-var)
2264 ;; This is used to override the value on a `*-font-lock-keywords'
2265 ;; variable only if it's nil or has the same value as one of the
2266 ;; `*-font-lock-keywords-*' variables. Older font-lock packages
2267 ;; define a default value for `*-font-lock-keywords' which we want
2268 ;; to override, but we should otoh avoid clobbering a user setting.
2269 ;; This heuristic for that isn't perfect, but I can't think of any
2270 ;; better. /mast
2271 (when (and (boundp def-var)
2272 (memq (symbol-value def-var)
2273 (cons nil
2274 (mapcar
2275 (lambda (suffix)
2276 (let ((sym (intern (concat (symbol-name def-var)
2277 suffix))))
2278 (and (boundp sym) (symbol-value sym))))
2279 '("-1" "-2" "-3")))))
2280 ;; The overriding is done by unbinding the variable so that the normal
2281 ;; defvar will install its default value later on.
2282 (makunbound def-var)))
2285 ;;; C.
2287 (c-override-default-keywords 'c-font-lock-keywords)
2289 (defconst c-font-lock-keywords-1 (c-lang-const c-matchers-1 c)
2290 "Minimal font locking for C mode.
2291 Fontifies only preprocessor directives (in addition to the syntactic
2292 fontification of strings and comments).")
2294 (defconst c-font-lock-keywords-2 (c-lang-const c-matchers-2 c)
2295 "Fast normal font locking for C mode.
2296 In addition to `c-font-lock-keywords-1', this adds fontification of
2297 keywords, simple types, declarations that are easy to recognize, the
2298 user defined types on `c-font-lock-extra-types', and the doc comment
2299 styles specified by `c-doc-comment-style'.")
2301 (defconst c-font-lock-keywords-3 (c-lang-const c-matchers-3 c)
2302 "Accurate normal font locking for C mode.
2303 Like the variable `c-font-lock-keywords-2' but detects declarations in a more
2304 accurate way that works in most cases for arbitrary types without the
2305 need for `c-font-lock-extra-types'.")
2307 (defvar c-font-lock-keywords c-font-lock-keywords-3
2308 "Default expressions to highlight in C mode.")
2310 (defun c-font-lock-keywords-2 ()
2311 (c-compose-keywords-list c-font-lock-keywords-2))
2312 (defun c-font-lock-keywords-3 ()
2313 (c-compose-keywords-list c-font-lock-keywords-3))
2314 (defun c-font-lock-keywords ()
2315 (c-compose-keywords-list c-font-lock-keywords))
2318 ;;; C++.
2320 (defun c-font-lock-c++-new (limit)
2321 ;; FIXME!!! Put in a comment about the context of this function's
2322 ;; invocation. I think it's called as an ANCHORED-MATCHER within an
2323 ;; ANCHORED-HIGHLIGHTER. (2007/2/10).
2325 ;; Assuming point is after a "new" word, check that it isn't inside
2326 ;; a string or comment, and if so try to fontify the type in the
2327 ;; allocation expression. Nil is always returned.
2329 ;; As usual, C++ takes the prize in coming up with a hard to parse
2330 ;; syntax. :P
2332 ;; This function might do hidden buffer changes.
2334 (unless (c-skip-comments-and-strings limit)
2335 (save-excursion
2336 (catch 'false-alarm
2337 ;; A "new" keyword is followed by one to three expressions, where
2338 ;; the type is the middle one, and the only required part.
2339 (let (expr1-pos expr2-pos
2340 ;; Enable recording of identifier ranges in `c-forward-type'
2341 ;; etc for later fontification. Not using
2342 ;; `c-fontify-types-and-refs' here since the ranges should
2343 ;; be fontified selectively only when an allocation
2344 ;; expression is successfully recognized.
2345 (c-record-type-identifiers t)
2346 c-record-ref-identifiers
2347 ;; The font-lock package in Emacs is known to clobber
2348 ;; `parse-sexp-lookup-properties' (when it exists).
2349 (parse-sexp-lookup-properties
2350 (cc-eval-when-compile
2351 (boundp 'parse-sexp-lookup-properties))))
2352 (c-forward-syntactic-ws)
2354 ;; The first placement arglist is always parenthesized, if it
2355 ;; exists.
2356 (when (eq (char-after) ?\()
2357 (setq expr1-pos (1+ (point)))
2358 (condition-case nil
2359 (c-forward-sexp)
2360 (scan-error (throw 'false-alarm t)))
2361 (c-forward-syntactic-ws))
2363 ;; The second expression is either a type followed by some "*" or
2364 ;; "[...]" or similar, or a parenthesized type followed by a full
2365 ;; identifierless declarator.
2366 (setq expr2-pos (1+ (point)))
2367 (cond ((eq (char-after) ?\())
2368 ((let ((c-promote-possible-types t))
2369 (c-forward-type)))
2370 (t (setq expr2-pos nil)))
2372 (when expr1-pos
2373 (cond
2374 ((not expr2-pos)
2375 ;; No second expression, so the first has to be a
2376 ;; parenthesized type.
2377 (goto-char expr1-pos)
2378 (let ((c-promote-possible-types t))
2379 (c-forward-type)))
2381 ((eq (char-before expr2-pos) ?\()
2382 ;; Got two parenthesized expressions, so we have to look
2383 ;; closer at them to decide which is the type. No need to
2384 ;; handle `c-record-ref-identifiers' since all references
2385 ;; have already been handled by other fontification rules.
2386 (let (expr1-res expr2-res)
2388 (goto-char expr1-pos)
2389 (when (setq expr1-res (c-forward-type))
2390 (unless (looking-at
2391 (cc-eval-when-compile
2392 (concat (c-lang-const c-symbol-start c++)
2393 "\\|[*:)[]")))
2394 ;; There's something after the would-be type that
2395 ;; can't be there, so this is a placement arglist.
2396 (setq expr1-res nil)))
2398 (goto-char expr2-pos)
2399 (when (setq expr2-res (c-forward-type))
2400 (unless (looking-at
2401 (cc-eval-when-compile
2402 (concat (c-lang-const c-symbol-start c++)
2403 "\\|[*:)[]")))
2404 ;; There's something after the would-be type that can't
2405 ;; be there, so this is an initialization expression.
2406 (setq expr2-res nil))
2407 (when (and (c-go-up-list-forward)
2408 (progn (c-forward-syntactic-ws)
2409 (eq (char-after) ?\()))
2410 ;; If there's a third initialization expression
2411 ;; then the second one is the type, so demote the
2412 ;; first match.
2413 (setq expr1-res nil)))
2415 ;; We fontify the most likely type, with a preference for
2416 ;; the first argument since a placement arglist is more
2417 ;; unusual than an initializer.
2418 (cond ((memq expr1-res '(t known prefix)))
2419 ((memq expr2-res '(t known prefix)))
2420 ;; Presumably 'decltype's will be fontified elsewhere.
2421 ((eq expr1-res 'decltype))
2422 ((eq expr2-res 'decltype))
2423 ((eq expr1-res 'found)
2424 (let ((c-promote-possible-types t))
2425 (goto-char expr1-pos)
2426 (c-forward-type)))
2427 ((eq expr2-res 'found)
2428 (let ((c-promote-possible-types t))
2429 (goto-char expr2-pos)
2430 (c-forward-type)))
2431 ((and (eq expr1-res 'maybe) (not expr2-res))
2432 (let ((c-promote-possible-types t))
2433 (goto-char expr1-pos)
2434 (c-forward-type)))
2435 ((and (not expr1-res) (eq expr2-res 'maybe))
2436 (let ((c-promote-possible-types t))
2437 (goto-char expr2-pos)
2438 (c-forward-type)))
2439 ;; If both type matches are 'maybe then we're
2440 ;; too uncertain to promote either of them.
2441 )))))
2443 ;; Fontify the type that now is recorded in
2444 ;; `c-record-type-identifiers', if any.
2445 (c-fontify-recorded-types-and-refs)))))
2446 nil)
2448 (c-override-default-keywords 'c++-font-lock-keywords)
2450 (defconst c++-font-lock-keywords-1 (c-lang-const c-matchers-1 c++)
2451 "Minimal font locking for C++ mode.
2452 Fontifies only preprocessor directives (in addition to the syntactic
2453 fontification of strings and comments).")
2455 (defconst c++-font-lock-keywords-2 (c-lang-const c-matchers-2 c++)
2456 "Fast normal font locking for C++ mode.
2457 In addition to `c++-font-lock-keywords-1', this adds fontification of
2458 keywords, simple types, declarations that are easy to recognize, the
2459 user defined types on `c++-font-lock-extra-types', and the doc comment
2460 styles specified by `c-doc-comment-style'.")
2462 (defconst c++-font-lock-keywords-3 (c-lang-const c-matchers-3 c++)
2463 "Accurate normal font locking for C++ mode.
2464 Like the variable `c++-font-lock-keywords-2' but detects declarations in a more
2465 accurate way that works in most cases for arbitrary types without the
2466 need for `c++-font-lock-extra-types'.")
2468 (defvar c++-font-lock-keywords c++-font-lock-keywords-3
2469 "Default expressions to highlight in C++ mode.")
2471 (defun c++-font-lock-keywords-2 ()
2472 (c-compose-keywords-list c++-font-lock-keywords-2))
2473 (defun c++-font-lock-keywords-3 ()
2474 (c-compose-keywords-list c++-font-lock-keywords-3))
2475 (defun c++-font-lock-keywords ()
2476 (c-compose-keywords-list c++-font-lock-keywords))
2479 ;;; Objective-C.
2481 (defun c-font-lock-objc-method ()
2482 ;; Assuming the point is after the + or - that starts an Objective-C
2483 ;; method declaration, fontify it. This must be done before normal
2484 ;; casts, declarations and labels are fontified since they will get
2485 ;; false matches in these things.
2487 ;; This function might do hidden buffer changes.
2489 (c-fontify-types-and-refs
2490 ((first t)
2491 (c-promote-possible-types t))
2493 (while (and
2494 (progn
2495 (c-forward-syntactic-ws)
2497 ;; An optional method type.
2498 (if (eq (char-after) ?\()
2499 (progn
2500 (forward-char)
2501 (c-forward-syntactic-ws)
2502 (c-forward-type)
2503 (prog1 (c-go-up-list-forward)
2504 (c-forward-syntactic-ws)))
2507 ;; The name. The first time it's the first part of
2508 ;; the function name, the rest of the time it's an
2509 ;; argument name.
2510 (looking-at c-symbol-key)
2511 (progn
2512 (goto-char (match-end 0))
2513 (c-put-font-lock-face (match-beginning 0)
2514 (point)
2515 (if first
2516 'font-lock-function-name-face
2517 'font-lock-variable-name-face))
2518 (c-forward-syntactic-ws)
2520 ;; Another optional part of the function name.
2521 (when (looking-at c-symbol-key)
2522 (goto-char (match-end 0))
2523 (c-put-font-lock-face (match-beginning 0)
2524 (point)
2525 'font-lock-function-name-face)
2526 (c-forward-syntactic-ws))
2528 ;; There's another argument if a colon follows.
2529 (eq (char-after) ?:)))
2530 (forward-char)
2531 (setq first nil))))
2533 (defun c-font-lock-objc-methods (limit)
2534 ;; Fontify method declarations in Objective-C. Nil is always
2535 ;; returned.
2537 ;; This function might do hidden buffer changes.
2539 (let (;; The font-lock package in Emacs is known to clobber
2540 ;; `parse-sexp-lookup-properties' (when it exists).
2541 (parse-sexp-lookup-properties
2542 (cc-eval-when-compile
2543 (boundp 'parse-sexp-lookup-properties))))
2545 (c-find-decl-spots
2546 limit
2547 "[-+]"
2549 (lambda (_match-pos _inside-macro &optional _top-level)
2550 (forward-char)
2551 (c-font-lock-objc-method))))
2552 nil)
2554 (c-override-default-keywords 'objc-font-lock-keywords)
2556 (defconst objc-font-lock-keywords-1 (c-lang-const c-matchers-1 objc)
2557 "Minimal font locking for Objective-C mode.
2558 Fontifies only compiler directives (in addition to the syntactic
2559 fontification of strings and comments).")
2561 (defconst objc-font-lock-keywords-2 (c-lang-const c-matchers-2 objc)
2562 "Fast normal font locking for Objective-C mode.
2563 In addition to `objc-font-lock-keywords-1', this adds fontification of
2564 keywords, simple types, declarations that are easy to recognize, the
2565 user defined types on `objc-font-lock-extra-types', and the doc
2566 comment styles specified by `c-doc-comment-style'.")
2568 (defconst objc-font-lock-keywords-3 (c-lang-const c-matchers-3 objc)
2569 "Accurate normal font locking for Objective-C mode.
2570 Like the variable `objc-font-lock-keywords-2' but detects declarations in a more
2571 accurate way that works in most cases for arbitrary types without the
2572 need for `objc-font-lock-extra-types'.")
2574 (defvar objc-font-lock-keywords objc-font-lock-keywords-3
2575 "Default expressions to highlight in Objective-C mode.")
2577 (defun objc-font-lock-keywords-2 ()
2578 (c-compose-keywords-list objc-font-lock-keywords-2))
2579 (defun objc-font-lock-keywords-3 ()
2580 (c-compose-keywords-list objc-font-lock-keywords-3))
2581 (defun objc-font-lock-keywords ()
2582 (c-compose-keywords-list objc-font-lock-keywords))
2584 ;; Kludge to override the default value that
2585 ;; `objc-font-lock-extra-types' might have gotten from the font-lock
2586 ;; package. The value replaced here isn't relevant now anyway since
2587 ;; those types are builtin and therefore listed directly in
2588 ;; `c-primitive-type-kwds'.
2589 (when (equal (sort (append objc-font-lock-extra-types nil) 'string-lessp)
2590 '("BOOL" "Class" "IMP" "SEL"))
2591 (setq objc-font-lock-extra-types
2592 (cc-eval-when-compile (list (concat "[" c-upper "]\\sw*")))))
2595 ;;; Java.
2597 (c-override-default-keywords 'java-font-lock-keywords)
2599 (defconst java-font-lock-keywords-1 (c-lang-const c-matchers-1 java)
2600 "Minimal font locking for Java mode.
2601 Fontifies nothing except the syntactic fontification of strings and
2602 comments.")
2604 (defconst java-font-lock-keywords-2 (c-lang-const c-matchers-2 java)
2605 "Fast normal font locking for Java mode.
2606 In addition to `java-font-lock-keywords-1', this adds fontification of
2607 keywords, simple types, declarations that are easy to recognize, the
2608 user defined types on `java-font-lock-extra-types', and the doc
2609 comment styles specified by `c-doc-comment-style'.")
2611 (defconst java-font-lock-keywords-3 (c-lang-const c-matchers-3 java)
2612 "Accurate normal font locking for Java mode.
2613 Like variable `java-font-lock-keywords-2' but detects declarations in a more
2614 accurate way that works in most cases for arbitrary types without the
2615 need for `java-font-lock-extra-types'.")
2617 (defvar java-font-lock-keywords java-font-lock-keywords-3
2618 "Default expressions to highlight in Java mode.")
2620 (defun java-font-lock-keywords-2 ()
2621 (c-compose-keywords-list java-font-lock-keywords-2))
2622 (defun java-font-lock-keywords-3 ()
2623 (c-compose-keywords-list java-font-lock-keywords-3))
2624 (defun java-font-lock-keywords ()
2625 (c-compose-keywords-list java-font-lock-keywords))
2628 ;;; CORBA IDL.
2630 (c-override-default-keywords 'idl-font-lock-keywords)
2632 (defconst idl-font-lock-keywords-1 (c-lang-const c-matchers-1 idl)
2633 "Minimal font locking for CORBA IDL mode.
2634 Fontifies nothing except the syntactic fontification of strings and
2635 comments.")
2637 (defconst idl-font-lock-keywords-2 (c-lang-const c-matchers-2 idl)
2638 "Fast normal font locking for CORBA IDL mode.
2639 In addition to `idl-font-lock-keywords-1', this adds fontification of
2640 keywords, simple types, declarations that are easy to recognize, the
2641 user defined types on `idl-font-lock-extra-types', and the doc comment
2642 styles specified by `c-doc-comment-style'.")
2644 (defconst idl-font-lock-keywords-3 (c-lang-const c-matchers-3 idl)
2645 "Accurate normal font locking for CORBA IDL mode.
2646 Like the variable `idl-font-lock-keywords-2' but detects declarations in a more
2647 accurate way that works in most cases for arbitrary types without the
2648 need for `idl-font-lock-extra-types'.")
2650 (defvar idl-font-lock-keywords idl-font-lock-keywords-3
2651 "Default expressions to highlight in CORBA IDL mode.")
2653 (defun idl-font-lock-keywords-2 ()
2654 (c-compose-keywords-list idl-font-lock-keywords-2))
2655 (defun idl-font-lock-keywords-3 ()
2656 (c-compose-keywords-list idl-font-lock-keywords-3))
2657 (defun idl-font-lock-keywords ()
2658 (c-compose-keywords-list idl-font-lock-keywords))
2661 ;;; Pike.
2663 (c-override-default-keywords 'pike-font-lock-keywords)
2665 (defconst pike-font-lock-keywords-1 (c-lang-const c-matchers-1 pike)
2666 "Minimal font locking for Pike mode.
2667 Fontifies only preprocessor directives (in addition to the syntactic
2668 fontification of strings and comments).")
2670 (defconst pike-font-lock-keywords-2 (c-lang-const c-matchers-2 pike)
2671 "Fast normal font locking for Pike mode.
2672 In addition to `pike-font-lock-keywords-1', this adds fontification of
2673 keywords, simple types, declarations that are easy to recognize, the
2674 user defined types on `pike-font-lock-extra-types', and the doc
2675 comment styles specified by `c-doc-comment-style'.")
2677 (defconst pike-font-lock-keywords-3 (c-lang-const c-matchers-3 pike)
2678 "Accurate normal font locking for Pike mode.
2679 Like the variable `pike-font-lock-keywords-2' but detects declarations in a more
2680 accurate way that works in most cases for arbitrary types without the
2681 need for `pike-font-lock-extra-types'.")
2683 (defvar pike-font-lock-keywords pike-font-lock-keywords-3
2684 "Default expressions to highlight in Pike mode.")
2686 (defun pike-font-lock-keywords-2 ()
2687 (c-compose-keywords-list pike-font-lock-keywords-2))
2688 (defun pike-font-lock-keywords-3 ()
2689 (c-compose-keywords-list pike-font-lock-keywords-3))
2690 (defun pike-font-lock-keywords ()
2691 (c-compose-keywords-list pike-font-lock-keywords))
2694 ;;; Doc comments.
2696 (defun c-font-lock-doc-comments (prefix limit keywords)
2697 ;; Fontify the comments between the point and LIMIT whose start
2698 ;; matches PREFIX with `c-doc-face-name'. Assumes comments have been
2699 ;; fontified with `font-lock-comment-face' already. nil is always
2700 ;; returned.
2702 ;; After the fontification of a matching comment, fontification
2703 ;; according to KEYWORDS is applied inside it. It's a list like
2704 ;; `font-lock-keywords' except that anchored matches and eval
2705 ;; clauses aren't supported and that some abbreviated forms can't be
2706 ;; used. The buffer is narrowed to the comment while KEYWORDS is
2707 ;; applied; leading comment starters are included but trailing
2708 ;; comment enders for block comment are not.
2710 ;; Note that faces added through KEYWORDS should never replace the
2711 ;; existing `c-doc-face-name' face since the existence of that face
2712 ;; is used as a flag in other code to skip comments.
2714 ;; This function might do hidden buffer changes.
2716 (let (comment-beg region-beg)
2717 (if (eq (get-text-property (point) 'face)
2718 'font-lock-comment-face)
2719 ;; Handle the case when the fontified region starts inside a
2720 ;; comment.
2721 (let ((start (c-literal-start)))
2722 (setq region-beg (point))
2723 (when start
2724 (goto-char start))
2725 (when (looking-at prefix)
2726 (setq comment-beg (point)))))
2728 (while (or
2729 comment-beg
2731 ;; Search for the prefix until a match is found at the start
2732 ;; of a comment.
2733 (while (when (re-search-forward prefix limit t)
2734 (setq comment-beg (match-beginning 0))
2735 (or (not (c-got-face-at comment-beg
2736 c-literal-faces))
2737 (and (/= comment-beg (point-min))
2738 (c-got-face-at (1- comment-beg)
2739 c-literal-faces))))
2740 (setq comment-beg nil))
2741 (setq region-beg comment-beg))
2743 (if (elt (parse-partial-sexp comment-beg (+ comment-beg 2)) 7)
2744 ;; Collect a sequence of doc style line comments.
2745 (progn
2746 (goto-char comment-beg)
2747 (while (and (progn
2748 (c-forward-single-comment)
2749 (skip-syntax-forward " ")
2750 (< (point) limit))
2751 (looking-at prefix))))
2752 (goto-char comment-beg)
2753 (c-forward-single-comment))
2754 (if (> (point) limit) (goto-char limit))
2755 (setq comment-beg nil)
2757 (let ((region-end (point))
2758 (keylist keywords) keyword matcher highlights)
2759 (c-put-font-lock-face region-beg region-end c-doc-face-name)
2760 (save-restriction
2761 ;; Narrow to the doc comment. Among other things, this
2762 ;; helps by making "^" match at the start of the comment.
2763 ;; Do not include a trailing block comment ender, though.
2764 (and (> region-end (1+ region-beg))
2765 (progn (goto-char region-end)
2766 (backward-char 2)
2767 (looking-at "\\*/"))
2768 (setq region-end (point)))
2769 (narrow-to-region region-beg region-end)
2771 (while keylist
2772 (setq keyword (car keylist)
2773 keylist (cdr keylist)
2774 matcher (car keyword))
2775 (goto-char region-beg)
2776 (while (if (stringp matcher)
2777 (re-search-forward matcher region-end t)
2778 (funcall matcher region-end))
2779 (setq highlights (cdr keyword))
2780 (if (consp (car highlights))
2781 (while highlights
2782 (font-lock-apply-highlight (car highlights))
2783 (setq highlights (cdr highlights)))
2784 (font-lock-apply-highlight highlights))))
2786 (goto-char region-end)))))
2787 nil)
2788 (put 'c-font-lock-doc-comments 'lisp-indent-function 2)
2790 (defun c-find-invalid-doc-markup (regexp limit)
2791 ;; Used to fontify invalid markup in doc comments after the correct
2792 ;; ones have been fontified: Find the first occurrence of REGEXP
2793 ;; between the point and LIMIT that only is fontified with
2794 ;; `c-doc-face-name'. If a match is found then submatch 0 surrounds
2795 ;; the first char and t is returned, otherwise nil is returned.
2797 ;; This function might do hidden buffer changes.
2798 (let (start)
2799 (while (if (re-search-forward regexp limit t)
2800 (not (eq (get-text-property
2801 (setq start (match-beginning 0)) 'face)
2802 c-doc-face-name))
2803 (setq start nil)))
2804 (when start
2805 (store-match-data (list (copy-marker start)
2806 (copy-marker (1+ start))))
2807 t)))
2809 ;; GtkDoc patterns contributed by Masatake YAMATO <jet@gyve.org>.
2811 (defconst gtkdoc-font-lock-doc-comments
2812 (let ((symbol "[a-zA-Z0-9_]+")
2813 (header "^ \\* "))
2814 `((,(concat header "\\(" symbol "\\):[ \t]*$")
2815 1 ,c-doc-markup-face-name prepend nil)
2816 (,(concat symbol "()")
2817 0 ,c-doc-markup-face-name prepend nil)
2818 (,(concat header "\\(" "@" symbol "\\):")
2819 1 ,c-doc-markup-face-name prepend nil)
2820 (,(concat "[#%@]" symbol)
2821 0 ,c-doc-markup-face-name prepend nil))
2824 (defconst gtkdoc-font-lock-doc-protection
2825 `(("< \\(public\\|private\\|protected\\) >"
2826 1 ,c-doc-markup-face-name prepend nil)))
2828 (defconst gtkdoc-font-lock-keywords
2829 `((,(lambda (limit)
2830 (c-font-lock-doc-comments "/\\*\\*$" limit
2831 gtkdoc-font-lock-doc-comments)
2832 (c-font-lock-doc-comments "/\\*< " limit
2833 gtkdoc-font-lock-doc-protection)
2834 ))))
2836 ;; Javadoc.
2838 (defconst javadoc-font-lock-doc-comments
2839 `(("{@[a-z]+[^}\n\r]*}" ; "{@foo ...}" markup.
2840 0 ,c-doc-markup-face-name prepend nil)
2841 ("^\\(/\\*\\)?\\(\\s \\|\\*\\)*\\(@[a-z]+\\)" ; "@foo ..." markup.
2842 3 ,c-doc-markup-face-name prepend nil)
2843 (,(concat "</?\\sw" ; HTML tags.
2844 "\\("
2845 (concat "\\sw\\|\\s \\|[=\n\r*.:]\\|"
2846 "\"[^\"]*\"\\|'[^']*'")
2847 "\\)*>")
2848 0 ,c-doc-markup-face-name prepend nil)
2849 ("&\\(\\sw\\|[.:]\\)+;" ; HTML entities.
2850 0 ,c-doc-markup-face-name prepend nil)
2851 ;; Fontify remaining markup characters as invalid. Note
2852 ;; that the Javadoc spec is hazy about when "@" is
2853 ;; allowed in non-markup use.
2854 (,(lambda (limit)
2855 (c-find-invalid-doc-markup "[<>&]\\|{@" limit))
2856 0 'font-lock-warning-face prepend nil)))
2858 (defconst javadoc-font-lock-keywords
2859 `((,(lambda (limit)
2860 (c-font-lock-doc-comments "/\\*\\*" limit
2861 javadoc-font-lock-doc-comments)))))
2863 ;; Pike autodoc.
2865 (defconst autodoc-decl-keywords
2866 ;; Adorned regexp matching the keywords that introduce declarations
2867 ;; in Pike Autodoc.
2868 (cc-eval-when-compile
2869 (c-make-keywords-re t '("@decl" "@elem" "@index" "@member") 'pike-mode)))
2871 (defconst autodoc-decl-type-keywords
2872 ;; Adorned regexp matching the keywords that are followed by a type.
2873 (cc-eval-when-compile
2874 (c-make-keywords-re t '("@elem" "@member") 'pike-mode)))
2876 (defun autodoc-font-lock-line-markup (limit)
2877 ;; Fontify all line oriented keywords between the point and LIMIT.
2878 ;; Nil is always returned.
2880 ;; This function might do hidden buffer changes.
2882 (let ((line-re (concat "^\\(\\(/\\*!\\|\\s *\\("
2883 c-current-comment-prefix
2884 "\\)\\)\\s *\\)@[A-Za-z_-]+\\(\\s \\|$\\)"))
2885 (markup-faces (list c-doc-markup-face-name c-doc-face-name)))
2887 (while (re-search-forward line-re limit t)
2888 (goto-char (match-end 1))
2890 (if (looking-at autodoc-decl-keywords)
2891 (let* ((kwd-pos (point))
2892 (start (match-end 1))
2893 (pos start)
2894 end)
2896 (c-put-font-lock-face (point) pos markup-faces)
2898 ;; Put a declaration end mark at the markup keyword and
2899 ;; remove the faces from the rest of the line so that it
2900 ;; gets refontified as a declaration later on by
2901 ;; `c-font-lock-declarations'.
2902 (c-put-char-property (1- pos) 'c-type 'c-decl-end)
2903 (goto-char pos)
2904 (while (progn
2905 (end-of-line)
2906 (setq end (point))
2907 (and (eq (char-before) ?@)
2908 (not (eobp))
2909 (progn (forward-char)
2910 (skip-syntax-forward " ")
2911 (looking-at c-current-comment-prefix))))
2912 (goto-char (match-end 0))
2913 (c-remove-font-lock-face pos (1- end))
2914 (c-put-font-lock-face (1- end) end markup-faces)
2915 (setq pos (point)))
2917 ;; Include the final newline in the removed area. This
2918 ;; has no visual effect but it avoids some tricky special
2919 ;; cases in the testsuite wrt the differences in string
2920 ;; fontification in Emacs vs XEmacs.
2921 (c-remove-font-lock-face pos (min (1+ (point)) (point-max)))
2923 ;; Must handle string literals explicitly inside the declaration.
2924 (goto-char start)
2925 (while (re-search-forward
2926 "\"\\([^\\\"]\\|\\\\.\\)*\"\\|'\\([^\\']\\|\\\\.\\)*'"
2927 end 'move)
2928 (c-put-font-lock-string-face (match-beginning 0)
2929 (point)))
2931 ;; Fontify types after keywords that always are followed
2932 ;; by them.
2933 (goto-char kwd-pos)
2934 (when (looking-at autodoc-decl-type-keywords)
2935 (c-fontify-types-and-refs ((c-promote-possible-types t))
2936 (goto-char start)
2937 (c-forward-syntactic-ws)
2938 (c-forward-type))))
2940 ;; Mark each whole line as markup, as long as the logical line
2941 ;; continues.
2942 (while (progn
2943 (c-put-font-lock-face (point)
2944 (progn (end-of-line) (point))
2945 markup-faces)
2946 (and (eq (char-before) ?@)
2947 (not (eobp))
2948 (progn (forward-char)
2949 (skip-syntax-forward " ")
2950 (looking-at c-current-comment-prefix))))
2951 (goto-char (match-end 0))))))
2953 nil)
2955 (defconst autodoc-font-lock-doc-comments
2956 `(("@\\(\\w+{\\|\\[\\([^]@\n\r]\\|@@\\)*\\]\\|[@}]\\|$\\)"
2957 ;; In-text markup.
2958 0 ,c-doc-markup-face-name prepend nil)
2959 (autodoc-font-lock-line-markup)
2960 ;; Fontify remaining markup characters as invalid.
2961 (,(lambda (limit)
2962 (c-find-invalid-doc-markup "@" limit))
2963 0 'font-lock-warning-face prepend nil)
2966 (defun autodoc-font-lock-keywords ()
2967 ;; Note that we depend on that `c-current-comment-prefix' has got
2968 ;; its proper value here.
2970 ;; This function might do hidden buffer changes.
2972 ;; The `c-type' text property with `c-decl-end' is used to mark the
2973 ;; end of the `autodoc-decl-keywords' occurrences to fontify the
2974 ;; following declarations.
2975 (setq c-type-decl-end-used t)
2977 `((,(lambda (limit)
2978 (c-font-lock-doc-comments "/[*/]!" limit
2979 autodoc-font-lock-doc-comments)))))
2982 ;; 2006-07-10: awk-font-lock-keywords has been moved back to cc-awk.el.
2983 (cc-provide 'cc-fonts)
2985 ;; Local Variables:
2986 ;; indent-tabs-mode: t
2987 ;; tab-width: 8
2988 ;; End:
2989 ;;; cc-fonts.el ends here