; * etc/NEWS: Fix last change.
[emacs.git] / lisp / progmodes / cc-fonts.el
blob7b99c2f54e530d718ee3d470856d4893ac7d68fa
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 <https://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 &optional check-point)
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. If CHECK-POINT
300 ;; is non-nil, we will check (< (point) limit) in the main loop.
301 `(while
302 ,(if check-point
303 `(and (< (point) limit)
304 (re-search-forward ,regexp limit t))
305 `(re-search-forward ,regexp limit t))
306 (unless (progn
307 (goto-char (match-beginning 0))
308 (c-skip-comments-and-strings limit))
309 (goto-char (match-end 0))
310 ,@(mapcar
311 (lambda (highlight)
312 (if (integerp (car highlight))
313 ;; e.g. highlight is (1 font-lock-type-face t)
314 (progn
315 (unless (eq (nth 2 highlight) t)
316 (error
317 "The override flag must currently be t in %s"
318 highlight))
319 (when (nth 3 highlight)
320 (error
321 "The laxmatch flag may currently not be set in %s"
322 highlight))
323 `(save-match-data
324 (c-put-font-lock-face
325 (match-beginning ,(car highlight))
326 (match-end ,(car highlight))
327 ,(elt highlight 1))))
328 ;; highlight is an "ANCHORED HIGHLIGHTER" of the form
329 ;; (ANCHORED-MATCHER PRE-FORM POST-FORM SUBEXP-HIGHLIGHTERS...)
330 (when (nth 3 highlight)
331 (error "Match highlights currently not supported in %s"
332 highlight))
333 `(progn
334 ,(nth 1 highlight)
335 (save-match-data ,(car highlight))
336 ,(nth 2 highlight))))
337 highlights))))
339 (defun c-make-font-lock-search-function (regexp &rest highlights)
340 ;; This function makes a byte compiled function that works much like
341 ;; a matcher element in `font-lock-keywords'. It cuts out a little
342 ;; bit of the overhead compared to a real matcher. The main reason
343 ;; is however to pass the real search limit to the anchored
344 ;; matcher(s), since most (if not all) font-lock implementations
345 ;; arbitrarily limit anchored matchers to the same line, and also
346 ;; to insulate against various other irritating differences between
347 ;; the different (X)Emacs font-lock packages.
349 ;; REGEXP is the matcher, which must be a regexp. Only matches
350 ;; where the beginning is outside any comment or string literal are
351 ;; significant.
353 ;; HIGHLIGHTS is a list of highlight specs, just like in
354 ;; `font-lock-keywords', with these limitations: The face is always
355 ;; overridden (no big disadvantage, since hits in comments etc are
356 ;; filtered anyway), there is no "laxmatch", and an anchored matcher
357 ;; is always a form which must do all the fontification directly.
358 ;; `limit' is a variable bound to the real limit in the context of
359 ;; the anchored matcher forms.
361 ;; This function does not do any hidden buffer changes, but the
362 ;; generated functions will. (They are however used in places
363 ;; covered by the font-lock context.)
365 ;; Note: Replace `byte-compile' with `eval' to debug the generated
366 ;; lambda more easily.
367 (byte-compile
368 `(lambda (limit)
369 (let ( ;; The font-lock package in Emacs is known to clobber
370 ;; `parse-sexp-lookup-properties' (when it exists).
371 (parse-sexp-lookup-properties
372 (cc-eval-when-compile
373 (boundp 'parse-sexp-lookup-properties))))
374 ,(c-make-font-lock-search-form regexp highlights))
375 nil)))
377 (defun c-make-font-lock-BO-decl-search-function (regexp &rest highlights)
378 ;; This function makes a byte compiled function that first moves back
379 ;; to the beginning of the current declaration (if any), then searches
380 ;; forward for matcher elements (as in `font-lock-keywords') and
381 ;; fontifies them.
383 ;; The motivation for moving back to the declaration start is to
384 ;; establish a context for the current text when, e.g., a character
385 ;; is typed on a C++ inheritance continuation line, or a jit-lock
386 ;; chunk starts there.
388 ;; The new function works much like a matcher element in
389 ;; `font-lock-keywords'. It cuts out a little bit of the overhead
390 ;; compared to a real matcher. The main reason is however to pass the
391 ;; real search limit to the anchored matcher(s), since most (if not
392 ;; all) font-lock implementations arbitrarily limit anchored matchers
393 ;; to the same line, and also to insulate against various other
394 ;; irritating differences between the different (X)Emacs font-lock
395 ;; packages.
397 ;; REGEXP is the matcher, which must be a regexp. Only matches
398 ;; where the beginning is outside any comment or string literal are
399 ;; significant.
401 ;; HIGHLIGHTS is a list of highlight specs, just like in
402 ;; `font-lock-keywords', with these limitations: The face is always
403 ;; overridden (no big disadvantage, since hits in comments etc are
404 ;; filtered anyway), there is no "laxmatch", and an anchored matcher
405 ;; is always a form which must do all the fontification directly.
406 ;; `limit' is a variable bound to the real limit in the context of
407 ;; the anchored matcher forms.
409 ;; This function does not do any hidden buffer changes, but the
410 ;; generated functions will. (They are however used in places
411 ;; covered by the font-lock context.)
413 ;; Note: Replace `byte-compile' with `eval' to debug the generated
414 ;; lambda more easily.
415 (byte-compile
416 `(lambda (limit)
417 (let ( ;; The font-lock package in Emacs is known to clobber
418 ;; `parse-sexp-lookup-properties' (when it exists).
419 (parse-sexp-lookup-properties
420 (cc-eval-when-compile
421 (boundp 'parse-sexp-lookup-properties)))
422 (BOD-limit
423 (c-determine-limit 1000)))
424 (goto-char
425 (let ((here (point)))
426 (if (eq (car (c-beginning-of-decl-1 BOD-limit)) 'same)
427 (point)
428 here)))
429 ,(c-make-font-lock-search-form regexp highlights))
430 nil)))
432 (defun c-make-font-lock-context-search-function (normal &rest state-stanzas)
433 ;; This function makes a byte compiled function that works much like
434 ;; a matcher element in `font-lock-keywords', with the following
435 ;; enhancement: the generated function will test for particular "font
436 ;; lock contexts" at the start of the region, i.e. is this point in
437 ;; the middle of some particular construct? if so the generated
438 ;; function will first fontify the tail of the construct, before
439 ;; going into the main loop and fontify full constructs up to limit.
441 ;; The generated function takes one parameter called `limit', and
442 ;; will fontify the region between POINT and LIMIT.
444 ;; NORMAL is a list of the form (REGEXP HIGHLIGHTS .....), and is
445 ;; used to fontify the "regular" bit of the region.
446 ;; STATE-STANZAS is list of elements of the form (STATE LIM REGEXP
447 ;; HIGHLIGHTS), each element coding one possible font lock context.
449 ;; o - REGEXP is a font-lock regular expression (NOT a function),
450 ;; o - HIGHLIGHTS is a list of zero or more highlighters as defined
451 ;; on page "Search-based Fontification" in the elisp manual. As
452 ;; yet (2009-06), they must have OVERRIDE set, and may not have
453 ;; LAXMATCH set.
455 ;; o - STATE is the "font lock context" (e.g. in-cpp-expr) and is
456 ;; not quoted.
457 ;; o - LIM is a lisp form whose evaluation will yield the limit
458 ;; position in the buffer for fontification by this stanza.
460 ;; This function does not do any hidden buffer changes, but the
461 ;; generated functions will. (They are however used in places
462 ;; covered by the font-lock context.)
464 ;; Note: Replace `byte-compile' with `eval' to debug the generated
465 ;; lambda more easily.
466 (byte-compile
467 `(lambda (limit)
468 (let ( ;; The font-lock package in Emacs is known to clobber
469 ;; `parse-sexp-lookup-properties' (when it exists).
470 (parse-sexp-lookup-properties
471 (cc-eval-when-compile
472 (boundp 'parse-sexp-lookup-properties))))
473 ,@(mapcar
474 (lambda (stanza)
475 (let ((state (car stanza))
476 (lim (nth 1 stanza))
477 (regexp (nth 2 stanza))
478 (highlights (cdr (cddr stanza))))
479 `(if (eq c-font-lock-context ',state)
480 (let ((limit ,lim))
481 ,(c-make-font-lock-search-form
482 regexp highlights)))))
483 state-stanzas)
484 ;; In the next form, check that point hasn't been moved beyond
485 ;; `limit' in any of the above stanzas.
486 ,(c-make-font-lock-search-form (car normal) (cdr normal) t)
487 nil))))
489 ; (eval-after-load "edebug" ; 2006-07-09: def-edebug-spec is now in subr.el.
490 ; '(progn
491 (def-edebug-spec c-fontify-types-and-refs let*)
492 (def-edebug-spec c-make-syntactic-matcher t)
493 ;; If there are literal quoted or backquoted highlight specs in
494 ;; the call to `c-make-font-lock-search-function' then let's
495 ;; instrument the forms in them.
496 (def-edebug-spec c-make-font-lock-search-function
497 (form &rest &or ("quote" (&rest form)) ("`" (&rest form)) form)));))
499 (defun c-fontify-recorded-types-and-refs ()
500 ;; Convert the ranges recorded on `c-record-type-identifiers' and
501 ;; `c-record-ref-identifiers' to fontification.
503 ;; This function does hidden buffer changes.
504 (let (elem)
505 (while (consp c-record-type-identifiers)
506 (setq elem (car c-record-type-identifiers)
507 c-record-type-identifiers (cdr c-record-type-identifiers))
508 (c-put-font-lock-face (car elem) (cdr elem)
509 'font-lock-type-face))
510 (while c-record-ref-identifiers
511 (setq elem (car c-record-ref-identifiers)
512 c-record-ref-identifiers (cdr c-record-ref-identifiers))
513 ;; Note that the reference face is a variable that is
514 ;; dereferenced, since it's an alias in Emacs.
515 (c-put-font-lock-face (car elem) (cdr elem)
516 c-reference-face-name))))
518 (c-lang-defconst c-cpp-matchers
519 "Font lock matchers for preprocessor directives and purely lexical
520 stuff. Used on level 1 and higher."
522 ;; Note: `c-font-lock-declarations' assumes that no matcher here
523 ;; sets `font-lock-type-face' in languages where
524 ;; `c-recognize-<>-arglists' is set.
526 t `(,@(when (c-lang-const c-opt-cpp-prefix)
527 (let* ((noncontinued-line-end "\\(\\=\\|\\(\\=\\|[^\\]\\)[\n\r]\\)")
528 (ncle-depth (regexp-opt-depth noncontinued-line-end))
529 (sws-depth (c-lang-const c-syntactic-ws-depth))
530 (nsws-depth (c-lang-const c-nonempty-syntactic-ws-depth)))
532 `(;; The stuff after #error and #warning is a message, so
533 ;; fontify it as a string.
534 ,@(when (c-lang-const c-cpp-message-directives)
535 (let* ((re (c-make-keywords-re 'appendable ; nil
536 (c-lang-const c-cpp-message-directives)))
537 (re-depth (regexp-opt-depth re)))
538 `((,(concat noncontinued-line-end
539 (c-lang-const c-opt-cpp-prefix)
541 "\\s +\\(.*\\)$")
542 ,(+ ncle-depth re-depth 1) font-lock-string-face t))))
544 ;; Fontify filenames in #include <...> as strings.
545 ,@(when (c-lang-const c-cpp-include-directives)
546 (let* ((re (c-make-keywords-re nil
547 (c-lang-const c-cpp-include-directives)))
548 (re-depth (regexp-opt-depth re)))
549 ;; We used to use a font-lock "anchored matcher" here for
550 ;; the paren syntax. This failed when the ">" was at EOL,
551 ;; since `font-lock-fontify-anchored-keywords' terminated
552 ;; its loop at EOL without executing our lambda form at
553 ;; all.
554 `((,(c-make-font-lock-search-function
555 (concat noncontinued-line-end
556 (c-lang-const c-opt-cpp-prefix)
558 (c-lang-const c-syntactic-ws)
559 "\\(<[^>\n\r]*>?\\)")
560 `(,(+ ncle-depth re-depth sws-depth 1)
561 font-lock-string-face t)
562 `((let ((beg (match-beginning
563 ,(+ ncle-depth re-depth sws-depth 1)))
564 (end (1- (match-end ,(+ ncle-depth re-depth
565 sws-depth 1)))))
566 (if (eq (char-after end) ?>)
567 (progn
568 (c-mark-<-as-paren beg)
569 (c-mark->-as-paren end))
570 (c-unmark-<->-as-paren beg)))
571 nil))))))
573 ;; #define.
574 ,@(when (c-lang-const c-opt-cpp-macro-define)
575 `((,(c-make-font-lock-search-function
576 (concat
577 noncontinued-line-end
578 (c-lang-const c-opt-cpp-prefix)
579 (c-lang-const c-opt-cpp-macro-define)
580 (c-lang-const c-nonempty-syntactic-ws)
581 "\\(" (c-lang-const ; 1 + ncle + nsws
582 c-symbol-key) "\\)"
583 (concat "\\(" ; 2 + ncle + nsws + c-sym-key
584 ;; Macro with arguments - a "function".
585 "\\((\\)" ; 3 + ncle + nsws + c-sym-key
586 "\\|"
587 ;; Macro without arguments - a "variable".
588 "\\([^(]\\|$\\)"
589 "\\)"))
590 `((if (match-beginning
591 ,(+ 3 ncle-depth nsws-depth
592 (c-lang-const c-symbol-key-depth)))
594 ;; "Function". Fontify the name and the arguments.
595 (save-restriction
596 (c-put-font-lock-face
597 (match-beginning ,(+ 1 ncle-depth nsws-depth))
598 (match-end ,(+ 1 ncle-depth nsws-depth))
599 'font-lock-function-name-face)
600 (goto-char
601 (match-end
602 ,(+ 3 ncle-depth nsws-depth
603 (c-lang-const c-symbol-key-depth))))
605 (narrow-to-region (point-min) limit)
606 (while (and
607 (progn
608 (c-forward-syntactic-ws)
609 (looking-at c-symbol-key))
610 (progn
611 (c-put-font-lock-face
612 (match-beginning 0) (match-end 0)
613 'font-lock-variable-name-face)
614 (goto-char (match-end 0))
615 (c-forward-syntactic-ws)
616 (eq (char-after) ?,)))
617 (forward-char)))
619 ;; "Variable".
620 (c-put-font-lock-face
621 (match-beginning ,(+ 1 ncle-depth nsws-depth))
622 (match-end ,(+ 1 ncle-depth nsws-depth))
623 'font-lock-variable-name-face)))))))
625 ;; Fontify cpp function names in preprocessor
626 ;; expressions in #if and #elif.
627 ,@(when (and (c-lang-const c-cpp-expr-directives)
628 (c-lang-const c-cpp-expr-functions))
629 (let ((ced-re (c-make-keywords-re t
630 (c-lang-const c-cpp-expr-directives)))
631 (cef-re (c-make-keywords-re t
632 (c-lang-const c-cpp-expr-functions))))
634 `((,(c-make-font-lock-context-search-function
635 `(,(concat noncontinued-line-end
636 (c-lang-const c-opt-cpp-prefix)
637 ced-re ; 1 + ncle-depth
638 ;; Match the whole logical line to look
639 ;; for the functions in.
640 "\\(\\\\\\(.\\|[\n\r]\\)\\|[^\n\r]\\)*")
641 ((let ((limit (match-end 0)))
642 (while (re-search-forward ,cef-re limit 'move)
643 (c-put-font-lock-face (match-beginning 1)
644 (match-end 1)
645 c-preprocessor-face-name)))
646 (goto-char (match-end ,(1+ ncle-depth)))))
647 `(in-cpp-expr
648 (save-excursion (c-end-of-macro) (point))
649 ,cef-re
650 (1 c-preprocessor-face-name t)))))))
652 ;; Fontify the directive names.
653 (,(c-make-font-lock-search-function
654 (concat noncontinued-line-end
655 "\\("
656 (c-lang-const c-opt-cpp-prefix)
657 "[" (c-lang-const c-symbol-chars) "]+"
658 "\\)")
659 `(,(1+ ncle-depth) c-preprocessor-face-name t)))
661 (eval . (list ,(c-make-syntactic-matcher
662 (concat noncontinued-line-end
663 (c-lang-const c-opt-cpp-prefix)
664 "if\\(n\\)def\\>"))
665 ,(+ ncle-depth 1)
666 c-negation-char-face-name
667 'append))
670 ,@(when (c-major-mode-is 'pike-mode)
671 ;; Recognize hashbangs in Pike.
672 `((eval . (list "\\`#![^\n\r]*"
673 0 c-preprocessor-face-name))))
675 ;; Make hard spaces visible through an inverted `font-lock-warning-face'.
676 (eval . (list
677 "\240"
678 0 (progn
679 (unless (c-face-name-p 'c-nonbreakable-space-face)
680 (c-make-inverse-face 'font-lock-warning-face
681 'c-nonbreakable-space-face))
682 ''c-nonbreakable-space-face)))
685 (defun c-font-lock-invalid-string ()
686 ;; Assuming the point is after the opening character of a string,
687 ;; fontify that char with `font-lock-warning-face' if the string
688 ;; decidedly isn't terminated properly.
690 ;; This function does hidden buffer changes.
691 (let ((start (1- (point))))
692 (save-excursion
693 (and (eq (elt (parse-partial-sexp start (c-point 'eol)) 8) start)
694 (if (if (eval-when-compile (integerp ?c))
695 ;; Emacs
696 (integerp c-multiline-string-start-char)
697 ;; XEmacs
698 (characterp c-multiline-string-start-char))
699 ;; There's no multiline string start char before the
700 ;; string, so newlines aren't allowed.
701 (not (eq (char-before start) c-multiline-string-start-char))
702 ;; Multiline strings are allowed anywhere if
703 ;; c-multiline-string-start-char is t.
704 (not c-multiline-string-start-char))
705 (if c-string-escaped-newlines
706 ;; There's no \ before the newline.
707 (not (eq (char-before (point)) ?\\))
708 ;; Escaped newlines aren't supported.
710 (c-put-font-lock-face start (1+ start) 'font-lock-warning-face)))))
712 (defun c-font-lock-invalid-single-quotes (limit)
713 ;; This function will be called from font-lock for a region bounded by POINT
714 ;; and LIMIT, as though it were to identify a keyword for
715 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
716 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
717 ;; Fontification".
719 ;; This function fontifies invalid single quotes with
720 ;; `font-lock-warning-face'. These are the single quotes which
721 ;; o - aren't inside a literal;
722 ;; o - are marked with a syntax-table text property value '(1); and
723 ;; o - are NOT marked with a non-null c-digit-separator property.
724 (let ((limits (c-literal-limits))
725 state beg end)
726 (if limits
727 (goto-char (cdr limits))) ; Even for being in a ' '
728 (while (< (point) limit)
729 (setq beg (point))
730 (setq state (parse-partial-sexp (point) limit nil nil nil 'syntax-table))
731 (setq end (point))
732 (goto-char beg)
733 (while (progn (skip-chars-forward "^'" end)
734 (< (point) end))
735 (if (and (equal (c-get-char-property (point) 'syntax-table) '(1))
736 (not (c-get-char-property (point) 'c-digit-separator)))
737 (c-put-font-lock-face (point) (1+ (point)) font-lock-warning-face))
738 (forward-char))
739 (parse-partial-sexp end limit nil nil state 'syntax-table)))
740 nil)
742 (c-lang-defconst c-basic-matchers-before
743 "Font lock matchers for basic keywords, labels, references and various
744 other easily recognizable things that should be fontified before generic
745 casts and declarations are fontified. Used on level 2 and higher."
747 ;; Note: `c-font-lock-declarations' assumes that no matcher here
748 ;; sets `font-lock-type-face' in languages where
749 ;; `c-recognize-<>-arglists' is set.
751 t `(;; Put a warning face on the opener of unclosed strings that
752 ;; can't span lines. Later font
753 ;; lock packages have a `font-lock-syntactic-face-function' for
754 ;; this, but it doesn't give the control we want since any
755 ;; fontification done inside the function will be
756 ;; unconditionally overridden.
757 ,(c-make-font-lock-search-function
758 ;; Match a char before the string starter to make
759 ;; `c-skip-comments-and-strings' work correctly.
760 (concat ".\\(" c-string-limit-regexp "\\)")
761 '((c-font-lock-invalid-string)))
763 ;; Invalid single quotes.
764 c-font-lock-invalid-single-quotes
766 ;; Fontify C++ raw strings.
767 ,@(when (c-major-mode-is 'c++-mode)
768 '(c-font-lock-raw-strings))
770 ;; Fontify keyword constants.
771 ,@(when (c-lang-const c-constant-kwds)
772 (let ((re (c-make-keywords-re nil (c-lang-const c-constant-kwds))))
773 (if (c-major-mode-is 'pike-mode)
774 ;; No symbol is a keyword after "->" in Pike.
775 `((eval . (list ,(concat "\\(\\=.?\\|[^>]\\|[^-]>\\)"
776 "\\<\\(" re "\\)\\>")
777 2 c-constant-face-name)))
778 `((eval . (list ,(concat "\\<\\(" re "\\)\\>")
779 1 c-constant-face-name))))))
781 ;; Fontify all keywords except the primitive types.
782 ,(if (c-major-mode-is 'pike-mode)
783 ;; No symbol is a keyword after "->" in Pike.
784 `(,(concat "\\(\\=.?\\|[^>]\\|[^-]>\\)"
785 "\\<" (c-lang-const c-regular-keywords-regexp))
786 2 font-lock-keyword-face)
787 `(,(concat "\\<" (c-lang-const c-regular-keywords-regexp))
788 1 font-lock-keyword-face))
790 ;; Fontify leading identifiers in fully qualified names like
791 ;; "foo::bar" in languages that supports such things.
792 ,@(when (c-lang-const c-opt-identifier-concat-key)
793 (if (c-major-mode-is 'java-mode)
794 ;; Java needs special treatment since "." is used both to
795 ;; qualify names and in normal indexing. Here we look for
796 ;; capital characters at the beginning of an identifier to
797 ;; recognize the class. "*" is also recognized to cover
798 ;; wildcard import declarations. All preceding dot separated
799 ;; identifiers are taken as package names and therefore
800 ;; fontified as references.
801 `(,(c-make-font-lock-search-function
802 ;; Search for class identifiers preceded by ".". The
803 ;; anchored matcher takes it from there.
804 (concat (c-lang-const c-opt-identifier-concat-key)
805 (c-lang-const c-simple-ws) "*"
806 (concat "\\("
807 "[" c-upper "]"
808 "[" (c-lang-const c-symbol-chars) "]*"
809 "\\|"
810 "\\*"
811 "\\)"))
812 `((let (id-end)
813 (goto-char (1+ (match-beginning 0)))
814 (while (and (eq (char-before) ?.)
815 (progn
816 (backward-char)
817 (c-backward-syntactic-ws)
818 (setq id-end (point))
819 (< (skip-chars-backward
820 ,(c-lang-const c-symbol-chars))
822 (not (get-text-property (point) 'face)))
823 (c-put-font-lock-face (point) id-end
824 c-reference-face-name)
825 (c-backward-syntactic-ws)))
827 (goto-char (match-end 0)))))
829 `((,(byte-compile
830 ;; Must use a function here since we match longer than
831 ;; we want to move before doing a new search. This is
832 ;; not necessary for XEmacs since it restarts the
833 ;; search from the end of the first highlighted
834 ;; submatch (something that causes problems in other
835 ;; places).
836 `(lambda (limit)
837 (while (re-search-forward
838 ,(concat "\\(\\<" ; 1
839 "\\(" (c-lang-const c-symbol-key) "\\)" ; 2
840 (c-lang-const c-simple-ws) "*"
841 (c-lang-const c-opt-identifier-concat-key)
842 (c-lang-const c-simple-ws) "*"
843 "\\)"
844 "\\("
845 (c-lang-const c-opt-after-id-concat-key)
846 "\\)")
847 limit t)
848 (unless (progn
849 (goto-char (match-beginning 0))
850 (c-skip-comments-and-strings limit))
851 (or (get-text-property (match-beginning 2) 'face)
852 (c-put-font-lock-face (match-beginning 2)
853 (match-end 2)
854 c-reference-face-name))
855 (goto-char (match-end 1))))))))))
857 ;; Fontify the special declarations in Objective-C.
858 ,@(when (c-major-mode-is 'objc-mode)
859 `(;; Fontify class names in the beginning of message expressions.
860 ,(c-make-font-lock-search-function
861 "\\["
862 '((c-fontify-types-and-refs ()
863 (c-forward-syntactic-ws limit)
864 (let ((start (point)))
865 ;; In this case we accept both primitive and known types.
866 (when (eq (c-forward-type) 'known)
867 (goto-char start)
868 (let ((c-promote-possible-types t))
869 (c-forward-type))))
870 (if (> (point) limit) (goto-char limit)))))
872 ;; The @interface/@implementation/@protocol directives.
873 ,(c-make-font-lock-search-function
874 (concat "\\<"
875 (regexp-opt
876 '("@interface" "@implementation" "@protocol")
878 "\\>")
879 '((c-fontify-types-and-refs
880 (;; The font-lock package in Emacs is known to clobber
881 ;; `parse-sexp-lookup-properties' (when it exists).
882 (parse-sexp-lookup-properties
883 (cc-eval-when-compile
884 (boundp 'parse-sexp-lookup-properties))))
885 (c-forward-objc-directive)
886 nil)
887 (goto-char (match-beginning 0))))))
889 (eval . (list "\\(!\\)[^=]" 1 c-negation-char-face-name))
892 (defun c-font-lock-complex-decl-prepare (limit)
893 ;; This function will be called from font-lock for a region bounded by POINT
894 ;; and LIMIT, as though it were to identify a keyword for
895 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
896 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
897 ;; Fontification".
899 ;; Called before any of the matchers in `c-complex-decl-matchers'.
901 ;; This function does hidden buffer changes.
903 ;;(message "c-font-lock-complex-decl-prepare %s %s" (point) limit)
905 ;; Clear the list of found types if we start from the start of the
906 ;; buffer, to make it easier to get rid of misspelled types and
907 ;; variables that have gotten recognized as types in malformed code.
908 (when (bobp)
909 (c-clear-found-types))
911 ;; Clear the c-type char properties which mark the region, to recalculate
912 ;; them properly. The most interesting properties are those put on the
913 ;; closest token before the region.
914 (save-excursion
915 (let ((pos (point)))
916 (c-backward-syntactic-ws)
917 (c-clear-char-properties
918 (if (and (not (bobp))
919 (memq (c-get-char-property (1- (point)) 'c-type)
920 '(c-decl-arg-start
921 c-decl-end
922 c-decl-id-start
923 c-decl-type-start)))
924 (1- (point))
925 pos)
926 limit 'c-type)))
928 ;; Update `c-state-cache' to the beginning of the region. This will
929 ;; make `c-beginning-of-syntax' go faster when it's used later on,
930 ;; and it's near the point most of the time.
931 (c-parse-state)
933 ;; Check if the fontified region starts inside a declarator list so
934 ;; that `c-font-lock-declarators' should be called at the start.
935 ;; The declared identifiers are font-locked correctly as types, if
936 ;; that is what they are.
937 (let ((prop (save-excursion
938 (c-backward-syntactic-ws)
939 (unless (bobp)
940 (c-get-char-property (1- (point)) 'c-type)))))
941 (when (memq prop '(c-decl-id-start c-decl-type-start))
942 (c-forward-syntactic-ws limit)
943 (c-font-lock-declarators limit t (eq prop 'c-decl-type-start)
944 (not (c-bs-at-toplevel-p (point))))))
946 (setq c-font-lock-context ;; (c-guess-font-lock-context)
947 (save-excursion
948 (if (and c-cpp-expr-intro-re
949 (c-beginning-of-macro)
950 (looking-at c-cpp-expr-intro-re))
951 'in-cpp-expr)))
952 nil)
954 (defun c-font-lock-<>-arglists (limit)
955 ;; This function will be called from font-lock for a region bounded by POINT
956 ;; and LIMIT, as though it were to identify a keyword for
957 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
958 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
959 ;; Fontification".
961 ;; Fontify types and references in names containing angle bracket
962 ;; arglists from the point to LIMIT. Note that
963 ;; `c-font-lock-declarations' has already handled many of them.
965 ;; This function might do hidden buffer changes.
967 (let (;; The font-lock package in Emacs is known to clobber
968 ;; `parse-sexp-lookup-properties' (when it exists).
969 (parse-sexp-lookup-properties
970 (cc-eval-when-compile
971 (boundp 'parse-sexp-lookup-properties)))
972 (c-parse-and-markup-<>-arglists t)
973 c-restricted-<>-arglists
974 id-start id-end id-face pos kwd-sym)
976 (while (and (< (point) limit)
977 (re-search-forward c-opt-<>-arglist-start limit t))
979 (setq id-start (match-beginning 1)
980 id-end (match-end 1)
981 pos (point))
983 (goto-char id-start)
984 (unless (c-skip-comments-and-strings limit)
985 (setq kwd-sym nil
986 c-restricted-<>-arglists nil
987 id-face (get-text-property id-start 'face))
989 (if (cond
990 ((eq id-face 'font-lock-type-face)
991 ;; The identifier got the type face so it has already been
992 ;; handled in `c-font-lock-declarations'.
993 nil)
995 ((eq id-face 'font-lock-keyword-face)
996 (when (looking-at c-opt-<>-sexp-key)
997 ;; There's a special keyword before the "<" that tells
998 ;; that it's an angle bracket arglist.
999 (setq kwd-sym (c-keyword-sym (match-string 1)))))
1002 ;; There's a normal identifier before the "<". If we're not in
1003 ;; a declaration context then we set `c-restricted-<>-arglists'
1004 ;; to avoid recognizing templates in function calls like "foo (a
1005 ;; < b, c > d)".
1006 (c-backward-syntactic-ws)
1007 (when (and (memq (char-before) '(?\( ?,))
1008 (not (eq (get-text-property (1- (point)) 'c-type)
1009 'c-decl-arg-start)))
1010 (setq c-restricted-<>-arglists t))
1013 (progn
1014 (goto-char (1- pos))
1015 ;; Check for comment/string both at the identifier and
1016 ;; at the "<".
1017 (unless (c-skip-comments-and-strings limit)
1019 (c-fontify-types-and-refs ()
1020 (when (c-forward-<>-arglist (c-keyword-member
1021 kwd-sym 'c-<>-type-kwds))
1022 (when (and c-opt-identifier-concat-key
1023 (not (get-text-property id-start 'face)))
1024 (c-forward-syntactic-ws)
1025 (cond ((looking-at c-opt-identifier-concat-key)
1026 (c-put-font-lock-face id-start id-end
1027 c-reference-face-name))
1028 ((eq (char-after) ?\())
1029 (t (c-put-font-lock-face id-start id-end
1030 'font-lock-type-face))))))
1032 (goto-char pos)))
1033 (goto-char pos)))))
1034 nil)
1036 (defun c-font-lock-declarators (limit list types not-top
1037 &optional template-class)
1038 ;; Assuming the point is at the start of a declarator in a declaration,
1039 ;; fontify the identifier it declares. (If TYPES is set, it does this via
1040 ;; the macro `c-fontify-types-and-refs'.)
1042 ;; If LIST is non-nil, also fontify the ids in any following declarators in
1043 ;; a comma separated list (e.g. "foo" and "*bar" in "int foo = 17, *bar;");
1044 ;; additionally, mark the commas with c-type property 'c-decl-id-start or
1045 ;; 'c-decl-type-start (according to TYPES). Stop at LIMIT.
1047 ;; If TYPES is non-nil, fontify all identifiers as types. If NOT-TOP is
1048 ;; non-nil, we are not at the top-level ("top-level" includes being directly
1049 ;; inside a class or namespace, etc.).
1051 ;; TEMPLATE-CLASS is non-nil when the declaration is in template delimiters
1052 ;; and was introduced by, e.g. "typename" or "class", such that if there is
1053 ;; a default (introduced by "="), it will be fontified as a type.
1054 ;; E.g. "<class X = Y>".
1056 ;; Nil is always returned. The function leaves point at the delimiter after
1057 ;; the last declarator it processes.
1059 ;; This function might do hidden buffer changes.
1061 ;;(message "c-font-lock-declarators from %s to %s" (point) limit)
1062 (c-fontify-types-and-refs
1063 ((pos (point)) next-pos id-start
1064 decl-res
1065 id-face got-type got-init
1066 c-last-identifier-range
1067 (separator-prop (if types 'c-decl-type-start 'c-decl-id-start)))
1069 ;; The following `while' fontifies a single declarator id each time round.
1070 ;; It loops only when LIST is non-nil.
1071 (while
1072 (and pos (setq decl-res (c-forward-declarator)))
1073 (setq next-pos (point)
1074 id-start (car decl-res)
1075 id-face (if (and (eq (char-after) ?\()
1076 (or (not (c-major-mode-is 'c++-mode))
1077 (not not-top)
1078 (car (cddr (cddr decl-res))) ; Id is in
1079 ; parens, etc.
1080 (save-excursion
1081 (forward-char)
1082 (c-forward-syntactic-ws)
1083 (looking-at "[*&]")))
1084 (not (car (cddr decl-res)))
1085 (or (not (c-major-mode-is 'c++-mode))
1086 (save-excursion
1087 (let (c-last-identifier-range)
1088 (forward-char)
1089 (c-forward-syntactic-ws)
1090 (catch 'is-function
1091 (while
1092 (progn
1093 (if (eq (char-after) ?\))
1094 (throw 'is-function t))
1095 (setq got-type (c-forward-type))
1096 (cond
1097 ((null got-type)
1098 (throw 'is-function nil))
1099 ((not (eq got-type 'maybe))
1100 (throw 'is-function t)))
1101 (c-forward-declarator nil t)
1102 (eq (char-after) ?,))
1103 (forward-char)
1104 (c-forward-syntactic-ws))
1105 t)))))
1106 'font-lock-function-name-face
1107 'font-lock-variable-name-face)
1108 got-init (and (cadr (cddr decl-res)) ; got-init
1109 (char-after)))
1111 (if types
1112 ;; Register and fontify the identifier as a type.
1113 (let ((c-promote-possible-types t))
1114 (goto-char id-start)
1115 (c-forward-type))
1116 ;; Fontify the last symbol in the identifier if it isn't fontified
1117 ;; already. The check is necessary only in certain cases where this
1118 ;; function is used "sloppily", e.g. in `c-simple-decl-matchers'.
1119 (when (and c-last-identifier-range
1120 (not (get-text-property (car c-last-identifier-range)
1121 'face)))
1122 (c-put-font-lock-face (car c-last-identifier-range)
1123 (cdr c-last-identifier-range)
1124 id-face)))
1126 (goto-char next-pos)
1127 (setq pos nil) ; So as to terminate the enclosing `while' form.
1128 (if (and template-class
1129 (eq got-init ?=) ; C++ "<class X = Y>"?
1130 (c-forward-token-2 1 nil limit) ; Over "="
1131 (let ((c-promote-possible-types t))
1132 (c-forward-type t))) ; Over "Y"
1133 (setq list nil)) ; Shouldn't be needed. We can't have a list, here.
1135 (when list
1136 ;; Jump past any initializer or function prototype to see if
1137 ;; there's a ',' to continue at.
1138 (cond ((eq id-face 'font-lock-function-name-face)
1139 ;; Skip a parenthesized initializer (C++) or a function
1140 ;; prototype.
1141 (if (c-safe (c-forward-sexp 1) t) ; over the parameter list.
1142 (c-forward-syntactic-ws limit)
1143 (goto-char limit))) ; unbalanced parens
1145 (got-init ; "=" sign OR opening "(", "[", or "{"
1146 ;; Skip an initializer expression. If we're at a '='
1147 ;; then accept a brace list directly after it to cope
1148 ;; with array initializers. Otherwise stop at braces
1149 ;; to avoid going past full function and class blocks.
1150 (and (if (and (eq got-init ?=)
1151 (= (c-forward-token-2 1 nil limit) 0)
1152 (looking-at "{"))
1153 (c-safe (c-forward-sexp) t) ; over { .... }
1155 (< (point) limit)
1156 ;; FIXME: Should look for c-decl-end markers here;
1157 ;; we might go far into the following declarations
1158 ;; in e.g. ObjC mode (see e.g. methods-4.m).
1159 (c-syntactic-re-search-forward "[;,{]" limit 'move t)
1160 (backward-char)))
1162 (t (c-forward-syntactic-ws limit)))
1164 ;; If a ',' is found we set pos to the next declarator and iterate.
1165 (when (and (< (point) limit) (looking-at ","))
1166 (c-put-char-property (point) 'c-type separator-prop)
1167 (forward-char)
1168 (c-forward-syntactic-ws limit)
1169 (setq pos (point)))))) ; acts to make the `while' form continue.
1170 nil)
1172 (defun c-get-fontification-context (match-pos not-front-decl &optional toplev)
1173 ;; Return a cons (CONTEXT . RESTRICTED-<>-ARGLISTS) for MATCH-POS.
1174 ;; NOT-FRONT-DECL is non-nil when a declaration later in the buffer than
1175 ;; MATCH-POS has already been parsed. TOPLEV is non-nil when MATCH-POS is
1176 ;; known to be at "top level", i.e. outside any braces, or directly inside a
1177 ;; namespace, class, etc.
1179 ;; CONTEXT is the fontification context of MATCH-POS, and is one of the
1180 ;; following:
1181 ;; 'decl In a comma-separated declaration context (typically
1182 ;; inside a function declaration arglist).
1183 ;; '<> In an angle bracket arglist.
1184 ;; 'arglist Some other type of arglist.
1185 ;; 'top Some other context and point is at the top-level (either
1186 ;; outside any braces or directly inside a class or namespace,
1187 ;; etc.)
1188 ;; nil Some other context or unknown context. Includes
1189 ;; within the parens of an if, for, ... construct.
1190 ;; 'not-decl Definitely not in a declaration.
1192 ;; RESTRICTED-<>-ARGLISTS is non-nil when a scan of template/generic
1193 ;; arguments lists (i.e. lists enclosed by <...>) is more strict about what
1194 ;; characters it allows within the list.
1195 (let ((type (and (> match-pos (point-min))
1196 (c-get-char-property (1- match-pos) 'c-type))))
1197 (cond ((not (memq (char-before match-pos) '(?\( ?, ?\[ ?< ?{)))
1198 (cons (and toplev 'top) nil))
1199 ;; A control flow expression or a decltype
1200 ((and (eq (char-before match-pos) ?\()
1201 (save-excursion
1202 (goto-char match-pos)
1203 (backward-char)
1204 (c-backward-token-2)
1205 (cond
1206 ((looking-at c-paren-stmt-key)
1207 ;; Allow comma separated <> arglists in for statements.
1208 (cons nil nil))
1209 ((or (looking-at c-block-stmt-2-key)
1210 (looking-at c-block-stmt-1-2-key)
1211 (looking-at c-typeof-key))
1212 (cons nil t))
1213 (t nil)))))
1214 ;; Near BOB.
1215 ((<= match-pos (point-min))
1216 (cons 'arglist t))
1217 ;; Got a cached hit in a declaration arglist.
1218 ((eq type 'c-decl-arg-start)
1219 (cons 'decl nil))
1220 ;; We're inside (probably) a brace list.
1221 ((eq type 'c-not-decl)
1222 (cons 'not-decl nil))
1223 ;; Inside a C++11 lambda function arglist.
1224 ((and (c-major-mode-is 'c++-mode)
1225 (eq (char-before match-pos) ?\()
1226 (save-excursion
1227 (goto-char match-pos)
1228 (c-backward-token-2)
1229 (and
1230 (c-safe (goto-char (scan-sexps (point) -1)))
1231 (c-looking-at-c++-lambda-capture-list))))
1232 (c-put-char-property (1- match-pos) 'c-type
1233 'c-decl-arg-start)
1234 (cons 'decl nil))
1235 ;; We're inside a brace list.
1236 ((and (eq (char-before match-pos) ?{)
1237 (save-excursion
1238 (goto-char (1- match-pos))
1239 (consp
1240 (c-looking-at-or-maybe-in-bracelist))))
1241 (c-put-char-property (1- match-pos) 'c-type
1242 'c-not-decl)
1243 (cons 'not-decl nil))
1244 ;; We're inside an "ordinary" open brace.
1245 ((eq (char-before match-pos) ?{)
1246 (cons (and toplev 'top) nil))
1247 ;; Inside an angle bracket arglist.
1248 ((or (eq type 'c-<>-arg-sep)
1249 (eq (char-before match-pos) ?<))
1250 (cons '<> nil))
1251 ;; Got a cached hit in some other type of arglist.
1252 (type
1253 (cons 'arglist t))
1254 ;; We're at a C++ uniform initialization.
1255 ((and (c-major-mode-is 'c++-mode)
1256 (eq (char-before match-pos) ?\()
1257 (save-excursion
1258 (goto-char match-pos)
1259 (and
1260 (zerop (c-backward-token-2 2))
1261 (looking-at c-identifier-start)
1262 (c-got-face-at (point)
1263 '(font-lock-variable-name-face)))))
1264 (cons 'not-decl nil))
1265 ((and not-front-decl
1266 ;; The point is within the range of a previously
1267 ;; encountered type decl expression, so the arglist
1268 ;; is probably one that contains declarations.
1269 ;; However, if `c-recognize-paren-inits' is set it
1270 ;; might also be an initializer arglist.
1271 (or (not c-recognize-paren-inits)
1272 (save-excursion
1273 (goto-char match-pos)
1274 (not (c-back-over-member-initializers)))))
1275 ;; The result of this check is cached with a char
1276 ;; property on the match token, so that we can look
1277 ;; it up again when refontifying single lines in a
1278 ;; multiline declaration.
1279 (c-put-char-property (1- match-pos)
1280 'c-type 'c-decl-arg-start)
1281 (cons 'decl nil))
1282 ;; Got an open paren preceded by an arith operator.
1283 ((and (eq (char-before match-pos) ?\()
1284 (save-excursion
1285 (goto-char match-pos)
1286 (and (zerop (c-backward-token-2 2))
1287 (looking-at c-arithmetic-op-regexp))))
1288 (cons nil nil))
1289 ;; In a C++ member initialization list.
1290 ((and (eq (char-before match-pos) ?,)
1291 (c-major-mode-is 'c++-mode)
1292 (save-excursion
1293 (goto-char match-pos)
1294 (c-back-over-member-initializers)))
1295 (c-put-char-property (1- match-pos) 'c-type 'c-not-decl)
1296 (cons 'not-decl nil))
1297 ;; At start of a declaration inside a declaration paren.
1298 ((save-excursion
1299 (goto-char match-pos)
1300 (and (memq (char-before match-pos) '(?\( ?\,))
1301 (c-go-up-list-backward match-pos)
1302 (eq (char-after) ?\()
1303 (let ((type (c-get-char-property (point) 'c-type)))
1304 (or (memq type '(c-decl-arg-start c-decl-type-start))
1305 (and
1306 (progn (c-backward-syntactic-ws) t)
1307 (c-back-over-compound-identifier)
1308 (progn
1309 (c-backward-syntactic-ws)
1310 (or (bobp)
1311 (progn
1312 (setq type (c-get-char-property (1- (point))
1313 'c-type))
1314 (memq type '(c-decl-arg-start
1315 c-decl-type-start))))))))))
1316 (cons 'decl nil))
1317 (t (cons 'arglist t)))))
1319 (defun c-font-lock-single-decl (limit decl-or-cast match-pos context toplev)
1320 ;; Try to fontify a single declaration, together with all its declarators.
1321 ;; Return nil if we're successful, non-nil if we fail. POINT should be
1322 ;; positioned at the start of the putative declaration before calling.
1323 ;; POINT is left undefined by this function.
1325 ;; LIMIT sets a maximum position we'll fontify out to.
1326 ;; DECL-OR-CAST has the form of a result from `c-forward-decl-or-cast-1',
1327 ;; and must indicate a declaration (i.e. not be nil or 'cast).
1328 ;; MATCH-POS is the position after the last symbol before the decl.
1329 ;; CONTEXT is the context of the current decl., as determined by
1330 ;; c-get-fontification-context.
1331 ;; TOPLEV is non-nil if the decl. is at the top level (i.e. outside any
1332 ;; braces, or directly inside a class, namespace, etc.)
1334 ;; Do we have an expression as the second or third clause of
1335 ;; a "for" paren expression?
1336 (if (save-excursion
1337 (and
1338 (car (cddr decl-or-cast)) ; maybe-expression flag.
1339 (c-go-up-list-backward)
1340 (eq (char-after) ?\()
1341 (progn (c-backward-syntactic-ws)
1342 (c-simple-skip-symbol-backward))
1343 (looking-at c-paren-stmt-key)
1344 (progn (goto-char match-pos)
1345 (while (and (eq (char-before) ?\))
1346 (c-go-list-backward))
1347 (c-backward-syntactic-ws))
1348 (eq (char-before) ?\;))))
1349 ;; We've got an expression in "for" parens. Remove the
1350 ;; "type" that would spuriously get fontified.
1351 (let ((elt (and (consp c-record-type-identifiers)
1352 (assq (cadr (cddr decl-or-cast))
1353 c-record-type-identifiers))))
1354 (when elt
1355 (setq c-record-type-identifiers
1356 (c-delq-from-dotted-list
1357 elt c-record-type-identifiers)))
1359 ;; Back up to the type to fontify the declarator(s).
1360 (goto-char (car decl-or-cast))
1362 (let ((decl-list
1363 (if (not (memq context '(nil top)))
1364 ;; Should normally not fontify a list of
1365 ;; declarators inside an arglist, but the first
1366 ;; argument in the ';' separated list of a "for"
1367 ;; statement is an exception.
1368 (when (eq (char-before match-pos) ?\()
1369 (save-excursion
1370 (goto-char (1- match-pos))
1371 (c-backward-syntactic-ws)
1372 (and (c-simple-skip-symbol-backward)
1373 (looking-at c-paren-stmt-key))))
1375 (template-class (and (eq context '<>)
1376 (save-excursion
1377 (goto-char match-pos)
1378 (c-forward-syntactic-ws)
1379 (looking-at c-template-typename-key)))))
1380 ;; Fix the `c-decl-id-start' or `c-decl-type-start' property
1381 ;; before the first declarator if it's a list.
1382 ;; `c-font-lock-declarators' handles the rest.
1383 (when decl-list
1384 (save-excursion
1385 (c-backward-syntactic-ws)
1386 (unless (bobp)
1387 (c-put-char-property (1- (point)) 'c-type
1388 (if (cadr decl-or-cast)
1389 'c-decl-type-start
1390 'c-decl-id-start)))))
1391 (c-font-lock-declarators
1392 (min limit (point-max)) decl-list
1393 (cadr decl-or-cast) (not toplev) template-class))
1395 ;; A declaration has been successfully identified, so do all the
1396 ;; fontification of types and refs that've been recorded.
1397 (c-fontify-recorded-types-and-refs)
1398 nil))
1401 (defun c-font-lock-declarations (limit)
1402 ;; Fontify all the declarations, casts and labels from the point to LIMIT.
1403 ;; Assumes that strings and comments have been fontified already.
1405 ;; This function will be called from font-lock for a region bounded by POINT
1406 ;; and LIMIT, as though it were to identify a keyword for
1407 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
1408 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
1409 ;; Fontification".
1411 ;; This function might do hidden buffer changes.
1413 ;;(message "c-font-lock-declarations search from %s to %s" (point) limit)
1415 (save-restriction
1416 (let (;; The position where `c-find-decl-spots' last stopped.
1417 start-pos
1418 ;; o - 'decl if we're in an arglist containing declarations
1419 ;; (but if `c-recognize-paren-inits' is set it might also be
1420 ;; an initializer arglist);
1421 ;; o - '<> if the arglist is of angle bracket type;
1422 ;; o - 'arglist if it's some other arglist;
1423 ;; o - nil, if not in an arglist at all. This includes the
1424 ;; parenthesized condition which follows "if", "while", etc.
1425 context
1426 ;; A list of starting positions of possible type declarations, or of
1427 ;; the typedef preceding one, if any.
1428 last-cast-end
1429 ;; The result from `c-forward-decl-or-cast-1'.
1430 decl-or-cast
1431 ;; The maximum of the end positions of all the checked type
1432 ;; decl expressions in the successfully identified
1433 ;; declarations. The position might be either before or
1434 ;; after the syntactic whitespace following the last token
1435 ;; in the type decl expression.
1436 (max-type-decl-end 0)
1437 ;; Same as `max-type-decl-*', but used when we're before
1438 ;; `token-pos'.
1439 (max-type-decl-end-before-token 0)
1440 ;; End of <..> construct which has had c-<>-arg-sep c-type
1441 ;; properties set within it.
1442 (max-<>-end 0)
1443 ;; Set according to the context to direct the heuristics for
1444 ;; recognizing C++ templates.
1445 c-restricted-<>-arglists
1446 ;; Turn on recording of identifier ranges in
1447 ;; `c-forward-decl-or-cast-1' and `c-forward-label' for
1448 ;; later fontification.
1449 (c-record-type-identifiers t)
1450 label-type
1451 c-record-ref-identifiers
1452 ;; Make `c-forward-type' calls mark up template arglists if
1453 ;; it finds any. That's necessary so that we later will
1454 ;; stop inside them to fontify types there.
1455 (c-parse-and-markup-<>-arglists t)
1456 ;; The font-lock package in Emacs is known to clobber
1457 ;; `parse-sexp-lookup-properties' (when it exists).
1458 (parse-sexp-lookup-properties
1459 (cc-eval-when-compile
1460 (boundp 'parse-sexp-lookup-properties))
1463 ;; Below we fontify a whole declaration even when it crosses the limit,
1464 ;; to avoid gaps when jit/lazy-lock fontifies the file a block at a
1465 ;; time. That is however annoying during editing, e.g. the following is
1466 ;; a common situation while the first line is being written:
1468 ;; my_variable
1469 ;; some_other_variable = 0;
1471 ;; font-lock will put the limit at the beginning of the second line
1472 ;; here, and if we go past it we'll fontify "my_variable" as a type and
1473 ;; "some_other_variable" as an identifier, and the latter will not
1474 ;; correct itself until the second line is changed. To avoid that we
1475 ;; narrow to the limit if the region to fontify is a single line.
1476 (if (<= limit (c-point 'bonl))
1477 (narrow-to-region
1478 (point-min)
1479 (save-excursion
1480 ;; Narrow after any operator chars following the limit though,
1481 ;; since those characters can be useful in recognizing a
1482 ;; declaration (in particular the '{' that opens a function body
1483 ;; after the header).
1484 (goto-char limit)
1485 (skip-chars-forward c-nonsymbol-chars)
1486 (point))))
1488 (c-find-decl-spots
1489 limit
1490 c-decl-start-re
1491 (eval c-maybe-decl-faces)
1493 (lambda (match-pos inside-macro &optional toplev)
1494 ;; Note to maintainers: don't use `limit' inside this lambda form;
1495 ;; c-find-decl-spots sometimes narrows to less than `limit'.
1496 (setq start-pos (point))
1497 (when
1498 ;; The result of the form below is true when we don't recognize a
1499 ;; declaration or cast, and we don't recognize a "non-decl",
1500 ;; typically a brace list.
1501 (if (or (and (eq (get-text-property (point) 'face)
1502 'font-lock-keyword-face)
1503 (looking-at c-not-decl-init-keywords))
1504 (and c-macro-with-semi-re
1505 (looking-at c-macro-with-semi-re))) ; 2008-11-04
1506 ;; Don't do anything more if we're looking at a keyword that
1507 ;; can't start a declaration.
1510 ;; Set `context' and `c-restricted-<>-arglists'. Look for
1511 ;; "<" for the sake of C++-style template arglists.
1512 ;; Ignore "(" when it's part of a control flow construct
1513 ;; (e.g. "for (").
1514 (let ((got-context
1515 (c-get-fontification-context
1516 match-pos
1517 (< match-pos (if inside-macro
1518 max-type-decl-end-before-token
1519 max-type-decl-end))
1520 toplev)))
1521 (setq context (car got-context)
1522 c-restricted-<>-arglists (cdr got-context)))
1524 ;; Check we haven't missed a preceding "typedef".
1525 (when (not (looking-at c-typedef-key))
1526 (c-backward-syntactic-ws)
1527 (c-backward-token-2)
1528 (or (looking-at c-typedef-key)
1529 (goto-char start-pos)))
1531 ;; In QT, "more" is an irritating keyword that expands to nothing.
1532 ;; We skip over it to prevent recognition of "more slots: <symbol>"
1533 ;; as a bitfield declaration.
1534 (when (and (c-major-mode-is 'c++-mode)
1535 (looking-at
1536 (concat "\\(more\\)\\([^" c-symbol-chars "]\\|$\\)")))
1537 (goto-char (match-end 1))
1538 (c-forward-syntactic-ws))
1540 ;; Now analyze the construct.
1541 (if (eq context 'not-decl)
1542 (progn
1543 (setq decl-or-cast nil)
1544 (if (c-syntactic-re-search-forward
1545 "," (min limit (point-max)) 'at-limit t)
1546 (c-put-char-property (1- (point)) 'c-type 'c-not-decl))
1547 nil)
1548 (setq decl-or-cast
1549 (c-forward-decl-or-cast-1
1550 match-pos context last-cast-end))
1552 ;; Ensure that c-<>-arg-sep c-type properties are in place on the
1553 ;; commas separating the arguments inside template/generic <..>s.
1554 (when (and (eq (char-before match-pos) ?<)
1555 (> match-pos max-<>-end))
1556 (save-excursion
1557 (goto-char match-pos)
1558 (c-backward-token-2)
1559 (if (and
1560 (eq (char-after) ?<)
1561 (let ((c-restricted-<>-arglists
1562 (save-excursion
1563 (c-backward-token-2)
1564 (and
1565 (not (looking-at c-opt-<>-sexp-key))
1566 (progn (c-backward-syntactic-ws)
1567 (memq (char-before) '(?\( ?,)))
1568 (not (eq (c-get-char-property (1- (point))
1569 'c-type)
1570 'c-decl-arg-start))))))
1571 (c-forward-<>-arglist nil)))
1572 (setq max-<>-end (point)))))
1574 (cond
1575 ((eq decl-or-cast 'cast)
1576 ;; Save the position after the previous cast so we can feed
1577 ;; it to `c-forward-decl-or-cast-1' in the next round. That
1578 ;; helps it discover cast chains like "(a) (b) c".
1579 (setq last-cast-end (point))
1580 (c-fontify-recorded-types-and-refs)
1581 nil)
1583 (decl-or-cast
1584 ;; We've found a declaration.
1586 ;; Set `max-type-decl-end' or `max-type-decl-end-before-token'
1587 ;; under the assumption that we're after the first type decl
1588 ;; expression in the declaration now. That's not really true;
1589 ;; we could also be after a parenthesized initializer
1590 ;; expression in C++, but this is only used as a last resort
1591 ;; to slant ambiguous expression/declarations, and overall
1592 ;; it's worth the risk to occasionally fontify an expression
1593 ;; as a declaration in an initializer expression compared to
1594 ;; getting ambiguous things in normal function prototypes
1595 ;; fontified as expressions.
1596 (if inside-macro
1597 (when (> (point) max-type-decl-end-before-token)
1598 (setq max-type-decl-end-before-token (point)))
1599 (when (> (point) max-type-decl-end)
1600 (setq max-type-decl-end (point))))
1601 (goto-char start-pos)
1602 (c-font-lock-single-decl limit decl-or-cast match-pos
1603 context
1604 (or toplev (nth 4 decl-or-cast))))
1606 (t t))))
1608 ;; It was a false alarm. Check if we're in a label (or other
1609 ;; construct with `:' except bitfield) instead.
1610 (goto-char start-pos)
1611 (when (setq label-type (c-forward-label t match-pos nil))
1612 ;; Can't use `c-fontify-types-and-refs' here since we
1613 ;; use the label face at times.
1614 (cond ((eq label-type 'goto-target)
1615 (c-put-font-lock-face (caar c-record-ref-identifiers)
1616 (cdar c-record-ref-identifiers)
1617 c-label-face-name))
1618 ((eq label-type 'qt-1kwd-colon)
1619 (c-put-font-lock-face (caar c-record-ref-identifiers)
1620 (cdar c-record-ref-identifiers)
1621 'font-lock-keyword-face))
1622 ((eq label-type 'qt-2kwds-colon)
1623 (mapc
1624 (lambda (kwd)
1625 (c-put-font-lock-face (car kwd) (cdr kwd)
1626 'font-lock-keyword-face))
1627 c-record-ref-identifiers)))
1628 (setq c-record-ref-identifiers nil)
1629 ;; `c-forward-label' has probably added a `c-decl-end'
1630 ;; marker, so return t to `c-find-decl-spots' to signal
1631 ;; that.
1632 t))))
1634 nil)))
1636 (defun c-font-lock-enum-body (limit)
1637 ;; Fontify the identifiers of each enum we find by searching forward.
1639 ;; This function will be called from font-lock for a region bounded by POINT
1640 ;; and LIMIT, as though it were to identify a keyword for
1641 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
1642 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
1643 ;; Fontification".
1644 (while (search-forward-regexp c-enum-clause-introduction-re limit t)
1645 (when (save-excursion
1646 (backward-char)
1647 (c-backward-over-enum-header))
1648 (c-forward-syntactic-ws)
1649 (c-font-lock-declarators limit t nil t)))
1650 nil)
1652 (defun c-font-lock-enum-tail (limit)
1653 ;; Fontify an enum's identifiers when POINT is within the enum's brace
1654 ;; block.
1656 ;; This function will be called from font-lock for a region bounded by POINT
1657 ;; and LIMIT, as though it were to identify a keyword for
1658 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
1659 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
1660 ;; Fontification".
1662 ;; Note that this function won't attempt to fontify beyond the end of the
1663 ;; current enum block, if any.
1664 (let* ((paren-state (c-parse-state))
1665 (encl-pos (c-most-enclosing-brace paren-state)))
1666 (when (and
1667 encl-pos
1668 (eq (char-after encl-pos) ?\{)
1669 (save-excursion
1670 (goto-char encl-pos)
1671 (c-backward-over-enum-header)))
1672 (c-syntactic-skip-backward "^{," nil t)
1673 (c-put-char-property (1- (point)) 'c-type 'c-decl-id-start)
1675 (c-forward-syntactic-ws)
1676 (c-font-lock-declarators limit t nil t)))
1677 nil)
1679 (defun c-font-lock-cut-off-declarators (limit)
1680 ;; Fontify any declarators "cut off" from their declaring type at the start
1681 ;; of the region being fontified.
1683 ;; This function will be called from font-lock- for a region bounded by
1684 ;; POINT and LIMIT, as though it were to identify a keyword for
1685 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
1686 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
1687 ;; fontification".
1688 (let ((here (point))
1689 (decl-search-lim (c-determine-limit 1000))
1690 paren-state encl-pos token-end context decl-or-cast
1691 start-pos top-level c-restricted-<>-arglists
1692 c-recognize-knr-p) ; Strictly speaking, bogus, but it
1693 ; speeds up lisp.h tremendously.
1694 (save-excursion
1695 (when (not (c-back-over-member-initializers))
1696 (unless (or (eobp)
1697 (looking-at "\\s(\\|\\s)"))
1698 (forward-char))
1699 (c-syntactic-skip-backward "^;{}" decl-search-lim t)
1700 (when (eq (char-before) ?})
1701 (c-go-list-backward) ; brace block of struct, etc.?
1702 (c-syntactic-skip-backward "^;{}" decl-search-lim t))
1703 (when (or (bobp)
1704 (memq (char-before) '(?\; ?{ ?})))
1705 (setq token-end (point))
1706 (c-forward-syntactic-ws here)
1707 (when (< (point) here)
1708 ;; We're now putatively at the declaration.
1709 (setq start-pos (point))
1710 (setq paren-state (c-parse-state))
1711 ;; At top level or inside a "{"?
1712 (if (or (not (setq encl-pos
1713 (c-most-enclosing-brace paren-state)))
1714 (eq (char-after encl-pos) ?\{))
1715 (progn
1716 (setq top-level (c-at-toplevel-p))
1717 (let ((got-context (c-get-fontification-context
1718 token-end nil top-level)))
1719 (setq context (car got-context)
1720 c-restricted-<>-arglists (cdr got-context)))
1721 (setq decl-or-cast
1722 (c-forward-decl-or-cast-1 token-end context nil))
1723 (when (consp decl-or-cast)
1724 (goto-char start-pos)
1725 (c-font-lock-single-decl limit decl-or-cast token-end
1726 context top-level))))))))
1727 nil))
1729 (defun c-font-lock-enclosing-decls (limit)
1730 ;; Fontify the declarators of (nested) declarations we're in the middle of.
1731 ;; This is mainly for when a jit-lock etc. chunk starts inside the brace
1732 ;; block of a struct/union/class, etc.
1734 ;; This function will be called from font-lock for a region bounded by POINT
1735 ;; and LIMIT, as though it were to identify a keyword for
1736 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
1737 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
1738 ;; Fontification".
1739 (let* ((paren-state (c-parse-state))
1740 (decl-search-lim (c-determine-limit 1000))
1741 in-typedef ps-elt)
1742 ;; Are we in any nested struct/union/class/etc. braces?
1743 (while paren-state
1744 (setq ps-elt (car paren-state)
1745 paren-state (cdr paren-state))
1746 (when (and (atom ps-elt)
1747 (eq (char-after ps-elt) ?\{))
1748 (goto-char ps-elt)
1749 (c-syntactic-skip-backward "^;{}" decl-search-lim)
1750 (c-forward-syntactic-ws)
1751 (setq in-typedef (looking-at c-typedef-key))
1752 (if in-typedef (c-forward-over-token-and-ws))
1753 (when (and c-opt-block-decls-with-vars-key
1754 (looking-at c-opt-block-decls-with-vars-key))
1755 (goto-char ps-elt)
1756 (when (c-safe (c-forward-sexp))
1757 (c-forward-syntactic-ws)
1758 (c-font-lock-declarators limit t in-typedef
1759 (not (c-bs-at-toplevel-p (point))))))))))
1761 (defun c-font-lock-raw-strings (limit)
1762 ;; Fontify C++ raw strings.
1764 ;; This function will be called from font-lock for a region bounded by POINT
1765 ;; and LIMIT, as though it were to identify a keyword for
1766 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
1767 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
1768 ;; Fontification".
1769 (let* ((state (c-state-semi-pp-to-literal (point)))
1770 (string-start (and (eq (cadr state) 'string)
1771 (car (cddr state))))
1772 (raw-id (and string-start
1773 (save-excursion
1774 (goto-char string-start)
1775 (and (eq (char-before) ?R)
1776 (looking-at "\"\\([^ ()\\\n\r\t]\\{0,16\\}\\)(")
1777 (match-string-no-properties 1))))))
1778 (while (< (point) limit)
1779 (if raw-id
1780 (progn
1781 (if (search-forward-regexp (concat ")\\(" (regexp-quote raw-id) "\\)\"")
1782 limit 'limit)
1783 (c-put-font-lock-face (match-beginning 1) (point) 'default))
1784 (setq raw-id nil))
1786 (when (search-forward-regexp
1787 "R\\(\"\\)\\([^ ()\\\n\r\t]\\{0,16\\}\\)(" limit 'limit)
1788 (when
1789 (or (and (eobp)
1790 (eq (c-get-char-property (1- (point)) 'face)
1791 'font-lock-warning-face))
1792 (eq (c-get-char-property (point) 'face) 'font-lock-string-face)
1793 (and (equal (c-get-char-property (match-end 2) 'syntax-table) '(1))
1794 (equal (c-get-char-property (match-beginning 1) 'syntax-table)
1795 '(1))))
1796 (let ((paren-prop (c-get-char-property (1- (point)) 'syntax-table)))
1797 (if paren-prop
1798 (progn
1799 (c-put-font-lock-face (match-beginning 0) (match-end 0)
1800 'font-lock-warning-face)
1801 (when
1802 (and
1803 (equal paren-prop '(15))
1804 (not (c-search-forward-char-property 'syntax-table '(15) limit)))
1805 (goto-char limit)))
1806 (c-put-font-lock-face (match-beginning 1) (match-end 2) 'default)
1807 (setq raw-id (match-string-no-properties 2)))))))))
1808 nil)
1810 (defun c-font-lock-c++-lambda-captures (limit)
1811 ;; Fontify the lambda capture component of C++ lambda declarations.
1813 ;; This function will be called from font-lock for a region bounded by POINT
1814 ;; and LIMIT, as though it were to identify a keyword for
1815 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
1816 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
1817 ;; Fontification".
1818 (let (mode capture-default id-start id-end declaration sub-begin sub-end)
1819 (while (and (< (point) limit)
1820 (search-forward "[" limit t))
1821 (when (progn (backward-char)
1822 (prog1
1823 (c-looking-at-c++-lambda-capture-list)
1824 (forward-char)))
1825 (c-forward-syntactic-ws)
1826 (setq mode (and (memq (char-after) '(?= ?&))
1827 (char-after)))
1828 ;; Is the first element of the list a bare "=" or "&"?
1829 (when mode
1830 (forward-char)
1831 (c-forward-syntactic-ws)
1832 (if (memq (char-after) '(?, ?\]))
1833 (progn
1834 (setq capture-default mode)
1835 (when (eq (char-after) ?,)
1836 (forward-char)
1837 (c-forward-syntactic-ws)))
1838 (c-backward-token-2)))
1840 ;; Go round the following loop once per captured item. We use "\\s)"
1841 ;; rather than "\\]" here to avoid infinite looping in this situation:
1842 ;; "unsigned items [] { [ }". The second "[" triggers this function,
1843 ;; but if we don't match the "}" with an "\\s)", the
1844 ;; `c-syntactic-re-search-forward' at the end of the loop fails to
1845 ;; move forward over it, leaving point stuck at the "}".
1846 (while (and (not (looking-at "\\s)"))
1847 (< (point) limit))
1848 (if (eq (char-after) ?&)
1849 (progn (setq mode ?&)
1850 (forward-char)
1851 (c-forward-syntactic-ws))
1852 (setq mode ?=))
1853 (if (c-on-identifier)
1854 (progn
1855 (setq id-start (point))
1856 (forward-char)
1857 (c-end-of-current-token)
1858 (setq id-end (point))
1859 (c-forward-syntactic-ws)
1861 (setq declaration (eq (char-after) ?=))
1862 (when declaration
1863 (forward-char) ; over "="
1864 (c-forward-syntactic-ws)
1865 (setq sub-begin (point)))
1866 (if (or (and (< (point) limit)
1867 (c-syntactic-re-search-forward "," limit t t))
1868 (and (c-go-up-list-forward nil limit)
1869 (eq (char-before) ?\])))
1870 (backward-char)
1871 (goto-char limit))
1872 (when declaration
1873 (save-excursion
1874 (setq sub-end (point))
1875 (goto-char sub-begin)
1876 (c-font-lock-c++-lambda-captures sub-end)))
1878 (c-put-font-lock-face id-start id-end
1879 (cond
1880 (declaration
1881 'font-lock-variable-name-face)
1882 ((and capture-default
1883 (eq mode capture-default))
1884 'font-lock-warning-face)
1885 ((eq mode ?=) font-lock-constant-face)
1886 (t 'font-lock-variable-name-face))))
1887 (c-syntactic-re-search-forward "," limit 'bound t))
1889 (c-forward-syntactic-ws)
1890 (when (eq (char-after) ?,)
1891 (forward-char)
1892 (c-forward-syntactic-ws)))
1894 (setq capture-default nil)
1895 (if (< (point) limit)
1896 (forward-char))))) ; over the terminating "]" or other close paren.
1897 nil)
1900 (c-lang-defconst c-simple-decl-matchers
1901 "Simple font lock matchers for types and declarations. These are used
1902 on level 2 only and so aren't combined with `c-complex-decl-matchers'."
1904 t `(;; Objective-C methods.
1905 ,@(when (c-major-mode-is 'objc-mode)
1906 `((,(c-lang-const c-opt-method-key)
1907 (,(byte-compile
1908 (lambda (limit)
1909 (let (;; The font-lock package in Emacs is known to clobber
1910 ;; `parse-sexp-lookup-properties' (when it exists).
1911 (parse-sexp-lookup-properties
1912 (cc-eval-when-compile
1913 (boundp 'parse-sexp-lookup-properties))))
1914 (save-restriction
1915 (narrow-to-region (point-min) limit)
1916 (c-font-lock-objc-method)))
1917 nil))
1918 (goto-char (match-end 1))))))
1920 ;; Fontify all type names and the identifiers in the
1921 ;; declarations they might start. Use eval here since
1922 ;; `c-known-type-key' gets its value from
1923 ;; `*-font-lock-extra-types' on mode init.
1924 (eval . (list ,(c-make-font-lock-search-function
1925 'c-known-type-key
1926 '(1 'font-lock-type-face t)
1927 '((c-font-lock-declarators limit t nil nil)
1928 (save-match-data
1929 (goto-char (match-end 1))
1930 (c-forward-syntactic-ws))
1931 (goto-char (match-end 1))))))
1933 ;; Fontify types preceded by `c-type-prefix-kwds' and the
1934 ;; identifiers in the declarations they might start.
1935 ,@(when (c-lang-const c-type-prefix-kwds)
1936 (let* ((prefix-re (c-make-keywords-re nil
1937 (c-lang-const c-type-prefix-kwds)))
1938 (type-match (+ 2
1939 (regexp-opt-depth prefix-re)
1940 (c-lang-const c-simple-ws-depth))))
1941 `((,(c-make-font-lock-search-function
1942 (concat "\\<\\(" prefix-re "\\)" ; 1
1943 (c-lang-const c-simple-ws) "+"
1944 (concat "\\(" ; 2 + prefix-re + c-simple-ws
1945 (c-lang-const c-symbol-key)
1946 "\\)"))
1947 `(,type-match
1948 'font-lock-type-face t)
1949 `((c-font-lock-declarators limit t nil nil)
1950 (save-match-data
1951 (goto-char (match-end ,type-match))
1952 (c-forward-syntactic-ws))
1953 (goto-char (match-end ,type-match))))))))
1955 ;; Fontify special declarations that lacks a type.
1956 ,@(when (c-lang-const c-typeless-decl-kwds)
1957 `((,(c-make-font-lock-search-function
1958 (concat "\\<\\("
1959 (regexp-opt (c-lang-const c-typeless-decl-kwds))
1960 "\\)\\>")
1961 '((c-font-lock-declarators limit t nil nil)
1962 (save-match-data
1963 (goto-char (match-end 1))
1964 (c-forward-syntactic-ws))
1965 (goto-char (match-end 1)))))))
1967 ;; Fontify generic colon labels in languages that support them.
1968 ,@(when (c-lang-const c-recognize-colon-labels)
1969 `(c-font-lock-labels))))
1971 (c-lang-defconst c-complex-decl-matchers
1972 "Complex font lock matchers for types and declarations. Used on level
1973 3 and higher."
1975 ;; Note: This code in this form dumps a number of functions into the
1976 ;; resulting constant, `c-matchers-3'. At run time, font lock will call
1977 ;; each of them as a "FUNCTION" (see Elisp page "Search-based
1978 ;; Fontification"). The font lock region is delimited by POINT and the
1979 ;; single parameter, LIMIT. Each of these functions returns NIL (thus
1980 ;; inhibiting spurious font-lock-keyword-face highlighting and another
1981 ;; call).
1983 t `(;; Initialize some things before the search functions below.
1984 c-font-lock-complex-decl-prepare
1986 ,@(if (c-major-mode-is 'objc-mode)
1987 ;; Fontify method declarations in Objective-C, but first
1988 ;; we have to put the `c-decl-end' `c-type' property on
1989 ;; all the @-style directives that haven't been handled in
1990 ;; `c-basic-matchers-before'.
1991 `(,(c-make-font-lock-search-function
1992 (c-make-keywords-re t
1993 ;; Exclude "@class" since that directive ends with a
1994 ;; semicolon anyway.
1995 (delete "@class"
1996 (append (c-lang-const c-protection-kwds)
1997 (c-lang-const c-other-decl-kwds)
1998 nil)))
1999 '((c-put-char-property (1- (match-end 1))
2000 'c-type 'c-decl-end)))
2001 c-font-lock-objc-methods))
2003 ;; Fontify declarators which have been cut off from their declaring
2004 ;; types at the start of the region.
2005 c-font-lock-cut-off-declarators
2007 ;; Fontify all declarations, casts and normal labels.
2008 c-font-lock-declarations
2010 ;; Fontify declarators when POINT is within their declaration.
2011 c-font-lock-enclosing-decls
2013 ;; Fontify angle bracket arglists like templates in C++.
2014 ,@(when (c-lang-const c-recognize-<>-arglists)
2015 `(c-font-lock-<>-arglists))
2017 ,@(when (c-major-mode-is 'c++-mode)
2018 `(c-font-lock-c++-lambda-captures))
2020 ;; The first two rules here mostly find occurrences that
2021 ;; `c-font-lock-declarations' has found already, but not
2022 ;; declarations containing blocks in the type (see note below).
2023 ;; It's also useful to fontify these everywhere to show e.g. when
2024 ;; a type keyword is accidentally used as an identifier.
2026 ;; Fontify basic types.
2027 ,(let ((re (c-make-keywords-re nil
2028 (c-lang-const c-primitive-type-kwds))))
2029 (if (c-major-mode-is 'pike-mode)
2030 ;; No symbol is a keyword after "->" in Pike.
2031 `(,(concat "\\(\\=.?\\|[^>]\\|[^-]>\\)"
2032 "\\<\\(" re "\\)\\>")
2033 2 font-lock-type-face)
2034 `(,(concat "\\<\\(" re "\\)\\>")
2035 1 'font-lock-type-face)))
2036 ;; Fontify the type in C++ "new" expressions.
2037 ,@(when (c-major-mode-is 'c++-mode)
2038 ;; This pattern is a probably a "(MATCHER . ANCHORED-HIGHLIGHTER)"
2039 ;; (see Elisp page "Search-based Fontification").
2040 `(("\\<new\\>"
2041 (c-font-lock-c++-new))))
2044 (defun c-font-lock-labels (limit)
2045 ;; Fontify all statement labels from the point to LIMIT. Assumes
2046 ;; that strings and comments have been fontified already. Nil is
2047 ;; always returned.
2049 ;; Note: This function is only used on decoration level 2; this is
2050 ;; taken care of directly by the gargantuan
2051 ;; `c-font-lock-declarations' on higher levels.
2053 ;; This function might do hidden buffer changes.
2055 (let (continue-pos id-start
2056 ;; The font-lock package in Emacs is known to clobber
2057 ;; `parse-sexp-lookup-properties' (when it exists).
2058 (parse-sexp-lookup-properties
2059 (cc-eval-when-compile
2060 (boundp 'parse-sexp-lookup-properties))))
2062 (while (re-search-forward ":[^:]" limit t)
2063 (setq continue-pos (point))
2064 (goto-char (match-beginning 0))
2065 (unless (c-skip-comments-and-strings limit)
2067 (c-backward-syntactic-ws)
2068 (and (setq id-start (c-on-identifier))
2070 (not (get-text-property id-start 'face))
2072 (progn
2073 (goto-char id-start)
2074 (c-backward-syntactic-ws)
2076 ;; Check for a char that precedes a statement.
2077 (memq (char-before) '(?\} ?\{ ?\;))
2078 ;; Check for a preceding label. We exploit the font
2079 ;; locking made earlier by this function.
2080 (and (eq (char-before) ?:)
2081 (progn
2082 (backward-char)
2083 (c-backward-syntactic-ws)
2084 (not (bobp)))
2085 (eq (get-text-property (1- (point)) 'face)
2086 c-label-face-name))
2087 ;; Check for a keyword that precedes a statement.
2088 (c-after-conditional)))
2090 (progn
2091 ;; Got a label.
2092 (goto-char id-start)
2093 (looking-at c-symbol-key)
2094 (c-put-font-lock-face (match-beginning 0) (match-end 0)
2095 c-label-face-name)))
2097 (goto-char continue-pos))))
2098 nil)
2100 (c-lang-defconst c-basic-matchers-after
2101 "Font lock matchers for various things that should be fontified after
2102 generic casts and declarations are fontified. Used on level 2 and
2103 higher."
2105 t `(,@(when (c-lang-const c-brace-list-decl-kwds)
2106 ;; Fontify the remaining identifiers inside an enum list when we start
2107 ;; inside it.
2108 `(c-font-lock-enum-tail
2109 ;; Fontify the identifiers inside enum lists. (The enum type
2110 ;; name is handled by `c-simple-decl-matchers' or
2111 ;; `c-complex-decl-matchers' below.
2112 c-font-lock-enum-body))
2114 ;; Fontify labels after goto etc.
2115 ,@(when (c-lang-const c-before-label-kwds)
2116 `(;; (Got three different interpretation levels here,
2117 ;; which makes it a bit complicated: 1) The backquote
2118 ;; stuff is expanded when compiled or loaded, 2) the
2119 ;; eval form is evaluated at font-lock setup (to
2120 ;; substitute c-label-face-name correctly), and 3) the
2121 ;; resulting structure is interpreted during
2122 ;; fontification.)
2123 (eval
2124 . ,(let* ((c-before-label-re
2125 (c-make-keywords-re nil
2126 (c-lang-const c-before-label-kwds))))
2127 `(list
2128 ,(concat "\\<\\(" c-before-label-re "\\)\\>"
2129 "\\s *"
2130 "\\(" ; identifier-offset
2131 (c-lang-const c-symbol-key)
2132 "\\)")
2133 (list ,(+ (regexp-opt-depth c-before-label-re) 2)
2134 c-label-face-name nil t))))))
2136 ;; Fontify the clauses after various keywords.
2137 ,@(when (or (c-lang-const c-type-list-kwds)
2138 (c-lang-const c-ref-list-kwds)
2139 (c-lang-const c-colon-type-list-kwds))
2140 `((,(c-make-font-lock-BO-decl-search-function
2141 (concat "\\<\\("
2142 (c-make-keywords-re nil
2143 (append (c-lang-const c-type-list-kwds)
2144 (c-lang-const c-ref-list-kwds)
2145 (c-lang-const c-colon-type-list-kwds)))
2146 "\\)\\>")
2147 '((c-fontify-types-and-refs ((c-promote-possible-types t))
2148 (c-forward-keyword-clause 1)
2149 (if (> (point) limit) (goto-char limit))))))))
2151 ,@(when (c-lang-const c-paren-type-kwds)
2152 `((,(c-make-font-lock-search-function
2153 (concat "\\<\\("
2154 (c-make-keywords-re nil
2155 (c-lang-const c-paren-type-kwds))
2156 "\\)\\>")
2157 '((c-fontify-types-and-refs ((c-promote-possible-types t))
2158 (c-forward-keyword-clause 1)
2159 (if (> (point) limit) (goto-char limit))))))))
2161 ,@(when (c-major-mode-is 'java-mode)
2162 `((eval . (list "\\<\\(@[a-zA-Z0-9]+\\)\\>" 1 c-annotation-face))))
2165 (c-lang-defconst c-matchers-1
2166 t (c-lang-const c-cpp-matchers))
2168 (c-lang-defconst c-matchers-2
2169 t (append (c-lang-const c-matchers-1)
2170 (c-lang-const c-basic-matchers-before)
2171 (c-lang-const c-simple-decl-matchers)
2172 (c-lang-const c-basic-matchers-after)))
2174 (c-lang-defconst c-matchers-3
2175 t (append (c-lang-const c-matchers-1)
2176 (c-lang-const c-basic-matchers-before)
2177 (c-lang-const c-complex-decl-matchers)
2178 (c-lang-const c-basic-matchers-after)))
2180 (defun c-compose-keywords-list (base-list)
2181 ;; Incorporate the font lock keyword lists according to
2182 ;; `c-doc-comment-style' on the given keyword list and return it.
2183 ;; This is used in the function bindings of the
2184 ;; `*-font-lock-keywords-*' symbols since we have to build the list
2185 ;; when font-lock is initialized.
2187 (unless (memq c-doc-face-name c-literal-faces)
2188 (setq c-literal-faces (cons c-doc-face-name c-literal-faces)))
2190 (let* ((doc-keywords
2191 (if (consp (car-safe c-doc-comment-style))
2192 (cdr-safe (or (assq c-buffer-is-cc-mode c-doc-comment-style)
2193 (assq 'other c-doc-comment-style)))
2194 c-doc-comment-style))
2195 (list (nconc (c--mapcan
2196 (lambda (doc-style)
2197 (let ((sym (intern
2198 (concat (symbol-name doc-style)
2199 "-font-lock-keywords"))))
2200 (cond ((fboundp sym)
2201 (funcall sym))
2202 ((boundp sym)
2203 (append (eval sym) nil)))))
2204 (if (listp doc-keywords)
2205 doc-keywords
2206 (list doc-keywords)))
2207 base-list)))
2209 ;; Kludge: If `c-font-lock-complex-decl-prepare' is on the list we
2210 ;; move it first since the doc comment font lockers might add
2211 ;; `c-type' text properties, so they have to be cleared before that.
2212 (when (memq 'c-font-lock-complex-decl-prepare list)
2213 (setq list (cons 'c-font-lock-complex-decl-prepare
2214 (delq 'c-font-lock-complex-decl-prepare
2215 (append list nil)))))
2217 list))
2219 (defun c-override-default-keywords (def-var)
2220 ;; This is used to override the value on a `*-font-lock-keywords'
2221 ;; variable only if it's nil or has the same value as one of the
2222 ;; `*-font-lock-keywords-*' variables. Older font-lock packages
2223 ;; define a default value for `*-font-lock-keywords' which we want
2224 ;; to override, but we should otoh avoid clobbering a user setting.
2225 ;; This heuristic for that isn't perfect, but I can't think of any
2226 ;; better. /mast
2227 (when (and (boundp def-var)
2228 (memq (symbol-value def-var)
2229 (cons nil
2230 (mapcar
2231 (lambda (suffix)
2232 (let ((sym (intern (concat (symbol-name def-var)
2233 suffix))))
2234 (and (boundp sym) (symbol-value sym))))
2235 '("-1" "-2" "-3")))))
2236 ;; The overriding is done by unbinding the variable so that the normal
2237 ;; defvar will install its default value later on.
2238 (makunbound def-var)))
2241 ;;; C.
2243 (c-override-default-keywords 'c-font-lock-keywords)
2245 (defconst c-font-lock-keywords-1 (c-lang-const c-matchers-1 c)
2246 "Minimal font locking for C mode.
2247 Fontifies only preprocessor directives (in addition to the syntactic
2248 fontification of strings and comments).")
2250 (defconst c-font-lock-keywords-2 (c-lang-const c-matchers-2 c)
2251 "Fast normal font locking for C mode.
2252 In addition to `c-font-lock-keywords-1', this adds fontification of
2253 keywords, simple types, declarations that are easy to recognize, the
2254 user defined types on `c-font-lock-extra-types', and the doc comment
2255 styles specified by `c-doc-comment-style'.")
2257 (defconst c-font-lock-keywords-3 (c-lang-const c-matchers-3 c)
2258 "Accurate normal font locking for C mode.
2259 Like the variable `c-font-lock-keywords-2' but detects declarations in a more
2260 accurate way that works in most cases for arbitrary types without the
2261 need for `c-font-lock-extra-types'.")
2263 (defvar c-font-lock-keywords c-font-lock-keywords-3
2264 "Default expressions to highlight in C mode.")
2266 (defun c-font-lock-keywords-2 ()
2267 (c-compose-keywords-list c-font-lock-keywords-2))
2268 (defun c-font-lock-keywords-3 ()
2269 (c-compose-keywords-list c-font-lock-keywords-3))
2270 (defun c-font-lock-keywords ()
2271 (c-compose-keywords-list c-font-lock-keywords))
2274 ;;; C++.
2276 (defun c-font-lock-c++-new (limit)
2277 ;; FIXME!!! Put in a comment about the context of this function's
2278 ;; invocation. I think it's called as an ANCHORED-MATCHER within an
2279 ;; ANCHORED-HIGHLIGHTER. (2007/2/10).
2281 ;; Assuming point is after a "new" word, check that it isn't inside
2282 ;; a string or comment, and if so try to fontify the type in the
2283 ;; allocation expression. Nil is always returned.
2285 ;; As usual, C++ takes the prize in coming up with a hard to parse
2286 ;; syntax. :P
2288 ;; This function might do hidden buffer changes.
2290 (unless (c-skip-comments-and-strings limit)
2291 (save-excursion
2292 (catch 'false-alarm
2293 ;; A "new" keyword is followed by one to three expressions, where
2294 ;; the type is the middle one, and the only required part.
2295 (let (expr1-pos expr2-pos
2296 ;; Enable recording of identifier ranges in `c-forward-type'
2297 ;; etc for later fontification. Not using
2298 ;; `c-fontify-types-and-refs' here since the ranges should
2299 ;; be fontified selectively only when an allocation
2300 ;; expression is successfully recognized.
2301 (c-record-type-identifiers t)
2302 c-record-ref-identifiers
2303 ;; The font-lock package in Emacs is known to clobber
2304 ;; `parse-sexp-lookup-properties' (when it exists).
2305 (parse-sexp-lookup-properties
2306 (cc-eval-when-compile
2307 (boundp 'parse-sexp-lookup-properties))))
2308 (c-forward-syntactic-ws)
2310 ;; The first placement arglist is always parenthesized, if it
2311 ;; exists.
2312 (when (eq (char-after) ?\()
2313 (setq expr1-pos (1+ (point)))
2314 (condition-case nil
2315 (c-forward-sexp)
2316 (scan-error (throw 'false-alarm t)))
2317 (c-forward-syntactic-ws))
2319 ;; The second expression is either a type followed by some "*" or
2320 ;; "[...]" or similar, or a parenthesized type followed by a full
2321 ;; identifierless declarator.
2322 (setq expr2-pos (1+ (point)))
2323 (cond ((eq (char-after) ?\())
2324 ((let ((c-promote-possible-types t))
2325 (c-forward-type)))
2326 (t (setq expr2-pos nil)))
2328 (when expr1-pos
2329 (cond
2330 ((not expr2-pos)
2331 ;; No second expression, so the first has to be a
2332 ;; parenthesized type.
2333 (goto-char expr1-pos)
2334 (let ((c-promote-possible-types t))
2335 (c-forward-type)))
2337 ((eq (char-before expr2-pos) ?\()
2338 ;; Got two parenthesized expressions, so we have to look
2339 ;; closer at them to decide which is the type. No need to
2340 ;; handle `c-record-ref-identifiers' since all references
2341 ;; have already been handled by other fontification rules.
2342 (let (expr1-res expr2-res)
2344 (goto-char expr1-pos)
2345 (when (setq expr1-res (c-forward-type))
2346 (unless (looking-at
2347 (cc-eval-when-compile
2348 (concat (c-lang-const c-symbol-start c++)
2349 "\\|[*:)[]")))
2350 ;; There's something after the would-be type that
2351 ;; can't be there, so this is a placement arglist.
2352 (setq expr1-res nil)))
2354 (goto-char expr2-pos)
2355 (when (setq expr2-res (c-forward-type))
2356 (unless (looking-at
2357 (cc-eval-when-compile
2358 (concat (c-lang-const c-symbol-start c++)
2359 "\\|[*:)[]")))
2360 ;; There's something after the would-be type that can't
2361 ;; be there, so this is an initialization expression.
2362 (setq expr2-res nil))
2363 (when (and (c-go-up-list-forward)
2364 (progn (c-forward-syntactic-ws)
2365 (eq (char-after) ?\()))
2366 ;; If there's a third initialization expression
2367 ;; then the second one is the type, so demote the
2368 ;; first match.
2369 (setq expr1-res nil)))
2371 ;; We fontify the most likely type, with a preference for
2372 ;; the first argument since a placement arglist is more
2373 ;; unusual than an initializer.
2374 (cond ((memq expr1-res '(t known prefix)))
2375 ((memq expr2-res '(t known prefix)))
2376 ;; Presumably 'decltype's will be fontified elsewhere.
2377 ((eq expr1-res 'decltype))
2378 ((eq expr2-res 'decltype))
2379 ((eq expr1-res 'found)
2380 (let ((c-promote-possible-types t))
2381 (goto-char expr1-pos)
2382 (c-forward-type)))
2383 ((eq expr2-res 'found)
2384 (let ((c-promote-possible-types t))
2385 (goto-char expr2-pos)
2386 (c-forward-type)))
2387 ((and (eq expr1-res 'maybe) (not expr2-res))
2388 (let ((c-promote-possible-types t))
2389 (goto-char expr1-pos)
2390 (c-forward-type)))
2391 ((and (not expr1-res) (eq expr2-res 'maybe))
2392 (let ((c-promote-possible-types t))
2393 (goto-char expr2-pos)
2394 (c-forward-type)))
2395 ;; If both type matches are 'maybe then we're
2396 ;; too uncertain to promote either of them.
2397 )))))
2399 ;; Fontify the type that now is recorded in
2400 ;; `c-record-type-identifiers', if any.
2401 (c-fontify-recorded-types-and-refs)))))
2402 nil)
2404 (c-override-default-keywords 'c++-font-lock-keywords)
2406 (defconst c++-font-lock-keywords-1 (c-lang-const c-matchers-1 c++)
2407 "Minimal font locking for C++ mode.
2408 Fontifies only preprocessor directives (in addition to the syntactic
2409 fontification of strings and comments).")
2411 (defconst c++-font-lock-keywords-2 (c-lang-const c-matchers-2 c++)
2412 "Fast normal font locking for C++ mode.
2413 In addition to `c++-font-lock-keywords-1', this adds fontification of
2414 keywords, simple types, declarations that are easy to recognize, the
2415 user defined types on `c++-font-lock-extra-types', and the doc comment
2416 styles specified by `c-doc-comment-style'.")
2418 (defconst c++-font-lock-keywords-3 (c-lang-const c-matchers-3 c++)
2419 "Accurate normal font locking for C++ mode.
2420 Like the variable `c++-font-lock-keywords-2' but detects declarations in a more
2421 accurate way that works in most cases for arbitrary types without the
2422 need for `c++-font-lock-extra-types'.")
2424 (defvar c++-font-lock-keywords c++-font-lock-keywords-3
2425 "Default expressions to highlight in C++ mode.")
2427 (defun c++-font-lock-keywords-2 ()
2428 (c-compose-keywords-list c++-font-lock-keywords-2))
2429 (defun c++-font-lock-keywords-3 ()
2430 (c-compose-keywords-list c++-font-lock-keywords-3))
2431 (defun c++-font-lock-keywords ()
2432 (c-compose-keywords-list c++-font-lock-keywords))
2435 ;;; Objective-C.
2437 (defun c-font-lock-objc-method ()
2438 ;; Assuming the point is after the + or - that starts an Objective-C
2439 ;; method declaration, fontify it. This must be done before normal
2440 ;; casts, declarations and labels are fontified since they will get
2441 ;; false matches in these things.
2443 ;; This function might do hidden buffer changes.
2445 (c-fontify-types-and-refs
2446 ((first t)
2447 (c-promote-possible-types t))
2449 (while (and
2450 (progn
2451 (c-forward-syntactic-ws)
2453 ;; An optional method type.
2454 (if (eq (char-after) ?\()
2455 (progn
2456 (forward-char)
2457 (c-forward-syntactic-ws)
2458 (c-forward-type)
2459 (prog1 (c-go-up-list-forward)
2460 (c-forward-syntactic-ws)))
2463 ;; The name. The first time it's the first part of
2464 ;; the function name, the rest of the time it's an
2465 ;; argument name.
2466 (looking-at c-symbol-key)
2467 (progn
2468 (goto-char (match-end 0))
2469 (c-put-font-lock-face (match-beginning 0)
2470 (point)
2471 (if first
2472 'font-lock-function-name-face
2473 'font-lock-variable-name-face))
2474 (c-forward-syntactic-ws)
2476 ;; Another optional part of the function name.
2477 (when (looking-at c-symbol-key)
2478 (goto-char (match-end 0))
2479 (c-put-font-lock-face (match-beginning 0)
2480 (point)
2481 'font-lock-function-name-face)
2482 (c-forward-syntactic-ws))
2484 ;; There's another argument if a colon follows.
2485 (eq (char-after) ?:)))
2486 (forward-char)
2487 (setq first nil))))
2489 (defun c-font-lock-objc-methods (limit)
2490 ;; Fontify method declarations in Objective-C. Nil is always
2491 ;; returned.
2493 ;; This function might do hidden buffer changes.
2495 (let (;; The font-lock package in Emacs is known to clobber
2496 ;; `parse-sexp-lookup-properties' (when it exists).
2497 (parse-sexp-lookup-properties
2498 (cc-eval-when-compile
2499 (boundp 'parse-sexp-lookup-properties))))
2501 (c-find-decl-spots
2502 limit
2503 "[-+]"
2505 (lambda (_match-pos _inside-macro &optional _top-level)
2506 (forward-char)
2507 (c-font-lock-objc-method))))
2508 nil)
2510 (c-override-default-keywords 'objc-font-lock-keywords)
2512 (defconst objc-font-lock-keywords-1 (c-lang-const c-matchers-1 objc)
2513 "Minimal font locking for Objective-C mode.
2514 Fontifies only compiler directives (in addition to the syntactic
2515 fontification of strings and comments).")
2517 (defconst objc-font-lock-keywords-2 (c-lang-const c-matchers-2 objc)
2518 "Fast normal font locking for Objective-C mode.
2519 In addition to `objc-font-lock-keywords-1', this adds fontification of
2520 keywords, simple types, declarations that are easy to recognize, the
2521 user defined types on `objc-font-lock-extra-types', and the doc
2522 comment styles specified by `c-doc-comment-style'.")
2524 (defconst objc-font-lock-keywords-3 (c-lang-const c-matchers-3 objc)
2525 "Accurate normal font locking for Objective-C mode.
2526 Like the variable `objc-font-lock-keywords-2' but detects declarations in a more
2527 accurate way that works in most cases for arbitrary types without the
2528 need for `objc-font-lock-extra-types'.")
2530 (defvar objc-font-lock-keywords objc-font-lock-keywords-3
2531 "Default expressions to highlight in Objective-C mode.")
2533 (defun objc-font-lock-keywords-2 ()
2534 (c-compose-keywords-list objc-font-lock-keywords-2))
2535 (defun objc-font-lock-keywords-3 ()
2536 (c-compose-keywords-list objc-font-lock-keywords-3))
2537 (defun objc-font-lock-keywords ()
2538 (c-compose-keywords-list objc-font-lock-keywords))
2540 ;; Kludge to override the default value that
2541 ;; `objc-font-lock-extra-types' might have gotten from the font-lock
2542 ;; package. The value replaced here isn't relevant now anyway since
2543 ;; those types are builtin and therefore listed directly in
2544 ;; `c-primitive-type-kwds'.
2545 (when (equal (sort (append objc-font-lock-extra-types nil) 'string-lessp)
2546 '("BOOL" "Class" "IMP" "SEL"))
2547 (setq objc-font-lock-extra-types
2548 (cc-eval-when-compile (list (concat "[" c-upper "]\\sw*")))))
2551 ;;; Java.
2553 (c-override-default-keywords 'java-font-lock-keywords)
2555 (defconst java-font-lock-keywords-1 (c-lang-const c-matchers-1 java)
2556 "Minimal font locking for Java mode.
2557 Fontifies nothing except the syntactic fontification of strings and
2558 comments.")
2560 (defconst java-font-lock-keywords-2 (c-lang-const c-matchers-2 java)
2561 "Fast normal font locking for Java mode.
2562 In addition to `java-font-lock-keywords-1', this adds fontification of
2563 keywords, simple types, declarations that are easy to recognize, the
2564 user defined types on `java-font-lock-extra-types', and the doc
2565 comment styles specified by `c-doc-comment-style'.")
2567 (defconst java-font-lock-keywords-3 (c-lang-const c-matchers-3 java)
2568 "Accurate normal font locking for Java mode.
2569 Like variable `java-font-lock-keywords-2' but detects declarations in a more
2570 accurate way that works in most cases for arbitrary types without the
2571 need for `java-font-lock-extra-types'.")
2573 (defvar java-font-lock-keywords java-font-lock-keywords-3
2574 "Default expressions to highlight in Java mode.")
2576 (defun java-font-lock-keywords-2 ()
2577 (c-compose-keywords-list java-font-lock-keywords-2))
2578 (defun java-font-lock-keywords-3 ()
2579 (c-compose-keywords-list java-font-lock-keywords-3))
2580 (defun java-font-lock-keywords ()
2581 (c-compose-keywords-list java-font-lock-keywords))
2584 ;;; CORBA IDL.
2586 (c-override-default-keywords 'idl-font-lock-keywords)
2588 (defconst idl-font-lock-keywords-1 (c-lang-const c-matchers-1 idl)
2589 "Minimal font locking for CORBA IDL mode.
2590 Fontifies nothing except the syntactic fontification of strings and
2591 comments.")
2593 (defconst idl-font-lock-keywords-2 (c-lang-const c-matchers-2 idl)
2594 "Fast normal font locking for CORBA IDL mode.
2595 In addition to `idl-font-lock-keywords-1', this adds fontification of
2596 keywords, simple types, declarations that are easy to recognize, the
2597 user defined types on `idl-font-lock-extra-types', and the doc comment
2598 styles specified by `c-doc-comment-style'.")
2600 (defconst idl-font-lock-keywords-3 (c-lang-const c-matchers-3 idl)
2601 "Accurate normal font locking for CORBA IDL mode.
2602 Like the variable `idl-font-lock-keywords-2' but detects declarations in a more
2603 accurate way that works in most cases for arbitrary types without the
2604 need for `idl-font-lock-extra-types'.")
2606 (defvar idl-font-lock-keywords idl-font-lock-keywords-3
2607 "Default expressions to highlight in CORBA IDL mode.")
2609 (defun idl-font-lock-keywords-2 ()
2610 (c-compose-keywords-list idl-font-lock-keywords-2))
2611 (defun idl-font-lock-keywords-3 ()
2612 (c-compose-keywords-list idl-font-lock-keywords-3))
2613 (defun idl-font-lock-keywords ()
2614 (c-compose-keywords-list idl-font-lock-keywords))
2617 ;;; Pike.
2619 (c-override-default-keywords 'pike-font-lock-keywords)
2621 (defconst pike-font-lock-keywords-1 (c-lang-const c-matchers-1 pike)
2622 "Minimal font locking for Pike mode.
2623 Fontifies only preprocessor directives (in addition to the syntactic
2624 fontification of strings and comments).")
2626 (defconst pike-font-lock-keywords-2 (c-lang-const c-matchers-2 pike)
2627 "Fast normal font locking for Pike mode.
2628 In addition to `pike-font-lock-keywords-1', this adds fontification of
2629 keywords, simple types, declarations that are easy to recognize, the
2630 user defined types on `pike-font-lock-extra-types', and the doc
2631 comment styles specified by `c-doc-comment-style'.")
2633 (defconst pike-font-lock-keywords-3 (c-lang-const c-matchers-3 pike)
2634 "Accurate normal font locking for Pike mode.
2635 Like the variable `pike-font-lock-keywords-2' but detects declarations in a more
2636 accurate way that works in most cases for arbitrary types without the
2637 need for `pike-font-lock-extra-types'.")
2639 (defvar pike-font-lock-keywords pike-font-lock-keywords-3
2640 "Default expressions to highlight in Pike mode.")
2642 (defun pike-font-lock-keywords-2 ()
2643 (c-compose-keywords-list pike-font-lock-keywords-2))
2644 (defun pike-font-lock-keywords-3 ()
2645 (c-compose-keywords-list pike-font-lock-keywords-3))
2646 (defun pike-font-lock-keywords ()
2647 (c-compose-keywords-list pike-font-lock-keywords))
2650 ;;; Doc comments.
2652 (defun c-font-lock-doc-comments (prefix limit keywords)
2653 ;; Fontify the comments between the point and LIMIT whose start
2654 ;; matches PREFIX with `c-doc-face-name'. Assumes comments have been
2655 ;; fontified with `font-lock-comment-face' already. nil is always
2656 ;; returned.
2658 ;; After the fontification of a matching comment, fontification
2659 ;; according to KEYWORDS is applied inside it. It's a list like
2660 ;; `font-lock-keywords' except that anchored matches and eval
2661 ;; clauses aren't supported and that some abbreviated forms can't be
2662 ;; used. The buffer is narrowed to the comment while KEYWORDS is
2663 ;; applied; leading comment starters are included but trailing
2664 ;; comment enders for block comment are not.
2666 ;; Note that faces added through KEYWORDS should never replace the
2667 ;; existing `c-doc-face-name' face since the existence of that face
2668 ;; is used as a flag in other code to skip comments.
2670 ;; This function might do hidden buffer changes.
2672 (let (comment-beg region-beg)
2673 (if (eq (get-text-property (point) 'face)
2674 'font-lock-comment-face)
2675 ;; Handle the case when the fontified region starts inside a
2676 ;; comment.
2677 (let ((start (c-literal-start)))
2678 (setq region-beg (point))
2679 (when start
2680 (goto-char start))
2681 (when (looking-at prefix)
2682 (setq comment-beg (point)))))
2684 (while (or
2685 comment-beg
2687 ;; Search for the prefix until a match is found at the start
2688 ;; of a comment.
2689 (while (when (re-search-forward prefix limit t)
2690 (setq comment-beg (match-beginning 0))
2691 (or (not (c-got-face-at comment-beg
2692 c-literal-faces))
2693 (and (/= comment-beg (point-min))
2694 (c-got-face-at (1- comment-beg)
2695 c-literal-faces))))
2696 (setq comment-beg nil))
2697 (setq region-beg comment-beg))
2699 (if (elt (parse-partial-sexp comment-beg (+ comment-beg 2)) 7)
2700 ;; Collect a sequence of doc style line comments.
2701 (progn
2702 (goto-char comment-beg)
2703 (while (and (progn
2704 (c-forward-single-comment)
2705 (skip-syntax-forward " ")
2706 (< (point) limit))
2707 (looking-at prefix))))
2708 (goto-char comment-beg)
2709 (c-forward-single-comment))
2710 (if (> (point) limit) (goto-char limit))
2711 (setq comment-beg nil)
2713 (let ((region-end (point))
2714 (keylist keywords) keyword matcher highlights)
2715 (c-put-font-lock-face region-beg region-end c-doc-face-name)
2716 (save-restriction
2717 ;; Narrow to the doc comment. Among other things, this
2718 ;; helps by making "^" match at the start of the comment.
2719 ;; Do not include a trailing block comment ender, though.
2720 (and (> region-end (1+ region-beg))
2721 (progn (goto-char region-end)
2722 (backward-char 2)
2723 (looking-at "\\*/"))
2724 (setq region-end (point)))
2725 (narrow-to-region region-beg region-end)
2727 (while keylist
2728 (setq keyword (car keylist)
2729 keylist (cdr keylist)
2730 matcher (car keyword))
2731 (goto-char region-beg)
2732 (while (if (stringp matcher)
2733 (re-search-forward matcher region-end t)
2734 (funcall matcher region-end))
2735 (setq highlights (cdr keyword))
2736 (if (consp (car highlights))
2737 (while highlights
2738 (font-lock-apply-highlight (car highlights))
2739 (setq highlights (cdr highlights)))
2740 (font-lock-apply-highlight highlights))))
2742 (goto-char region-end)))))
2743 nil)
2744 (put 'c-font-lock-doc-comments 'lisp-indent-function 2)
2746 (defun c-find-invalid-doc-markup (regexp limit)
2747 ;; Used to fontify invalid markup in doc comments after the correct
2748 ;; ones have been fontified: Find the first occurrence of REGEXP
2749 ;; between the point and LIMIT that only is fontified with
2750 ;; `c-doc-face-name'. If a match is found then submatch 0 surrounds
2751 ;; the first char and t is returned, otherwise nil is returned.
2753 ;; This function might do hidden buffer changes.
2754 (let (start)
2755 (while (if (re-search-forward regexp limit t)
2756 (not (eq (get-text-property
2757 (setq start (match-beginning 0)) 'face)
2758 c-doc-face-name))
2759 (setq start nil)))
2760 (when start
2761 (store-match-data (list (copy-marker start)
2762 (copy-marker (1+ start))))
2763 t)))
2765 ;; GtkDoc patterns contributed by Masatake YAMATO <jet@gyve.org>.
2767 (defconst gtkdoc-font-lock-doc-comments
2768 (let ((symbol "[a-zA-Z0-9_]+")
2769 (header "^ \\* "))
2770 `((,(concat header "\\(" symbol "\\):[ \t]*$")
2771 1 ,c-doc-markup-face-name prepend nil)
2772 (,(concat symbol "()")
2773 0 ,c-doc-markup-face-name prepend nil)
2774 (,(concat header "\\(" "@" symbol "\\):")
2775 1 ,c-doc-markup-face-name prepend nil)
2776 (,(concat "[#%@]" symbol)
2777 0 ,c-doc-markup-face-name prepend nil))
2780 (defconst gtkdoc-font-lock-doc-protection
2781 `(("< \\(public\\|private\\|protected\\) >"
2782 1 ,c-doc-markup-face-name prepend nil)))
2784 (defconst gtkdoc-font-lock-keywords
2785 `((,(lambda (limit)
2786 (c-font-lock-doc-comments "/\\*\\*$" limit
2787 gtkdoc-font-lock-doc-comments)
2788 (c-font-lock-doc-comments "/\\*< " limit
2789 gtkdoc-font-lock-doc-protection)
2790 ))))
2792 ;; Javadoc.
2794 (defconst javadoc-font-lock-doc-comments
2795 `(("{@[a-z]+[^}\n\r]*}" ; "{@foo ...}" markup.
2796 0 ,c-doc-markup-face-name prepend nil)
2797 ("^\\(/\\*\\)?\\(\\s \\|\\*\\)*\\(@[a-z]+\\)" ; "@foo ..." markup.
2798 3 ,c-doc-markup-face-name prepend nil)
2799 (,(concat "</?\\sw" ; HTML tags.
2800 "\\("
2801 (concat "\\sw\\|\\s \\|[=\n\r*.:]\\|"
2802 "\"[^\"]*\"\\|'[^']*'")
2803 "\\)*>")
2804 0 ,c-doc-markup-face-name prepend nil)
2805 ("&\\(\\sw\\|[.:]\\)+;" ; HTML entities.
2806 0 ,c-doc-markup-face-name prepend nil)
2807 ;; Fontify remaining markup characters as invalid. Note
2808 ;; that the Javadoc spec is hazy about when "@" is
2809 ;; allowed in non-markup use.
2810 (,(lambda (limit)
2811 (c-find-invalid-doc-markup "[<>&]\\|{@" limit))
2812 0 'font-lock-warning-face prepend nil)))
2814 (defconst javadoc-font-lock-keywords
2815 `((,(lambda (limit)
2816 (c-font-lock-doc-comments "/\\*\\*" limit
2817 javadoc-font-lock-doc-comments)))))
2819 ;; Pike autodoc.
2821 (defconst autodoc-decl-keywords
2822 ;; Adorned regexp matching the keywords that introduce declarations
2823 ;; in Pike Autodoc.
2824 (cc-eval-when-compile
2825 (c-make-keywords-re t '("@decl" "@elem" "@index" "@member") 'pike-mode)))
2827 (defconst autodoc-decl-type-keywords
2828 ;; Adorned regexp matching the keywords that are followed by a type.
2829 (cc-eval-when-compile
2830 (c-make-keywords-re t '("@elem" "@member") 'pike-mode)))
2832 (defun autodoc-font-lock-line-markup (limit)
2833 ;; Fontify all line oriented keywords between the point and LIMIT.
2834 ;; Nil is always returned.
2836 ;; This function might do hidden buffer changes.
2838 (let ((line-re (concat "^\\(\\(/\\*!\\|\\s *\\("
2839 c-current-comment-prefix
2840 "\\)\\)\\s *\\)@[A-Za-z_-]+\\(\\s \\|$\\)"))
2841 (markup-faces (list c-doc-markup-face-name c-doc-face-name)))
2843 (while (re-search-forward line-re limit t)
2844 (goto-char (match-end 1))
2846 (if (looking-at autodoc-decl-keywords)
2847 (let* ((kwd-pos (point))
2848 (start (match-end 1))
2849 (pos start)
2850 end)
2852 (c-put-font-lock-face (point) pos markup-faces)
2854 ;; Put a declaration end mark at the markup keyword and
2855 ;; remove the faces from the rest of the line so that it
2856 ;; gets refontified as a declaration later on by
2857 ;; `c-font-lock-declarations'.
2858 (c-put-char-property (1- pos) 'c-type 'c-decl-end)
2859 (goto-char pos)
2860 (while (progn
2861 (end-of-line)
2862 (setq end (point))
2863 (and (eq (char-before) ?@)
2864 (not (eobp))
2865 (progn (forward-char)
2866 (skip-syntax-forward " ")
2867 (looking-at c-current-comment-prefix))))
2868 (goto-char (match-end 0))
2869 (c-remove-font-lock-face pos (1- end))
2870 (c-put-font-lock-face (1- end) end markup-faces)
2871 (setq pos (point)))
2873 ;; Include the final newline in the removed area. This
2874 ;; has no visual effect but it avoids some tricky special
2875 ;; cases in the testsuite wrt the differences in string
2876 ;; fontification in Emacs vs XEmacs.
2877 (c-remove-font-lock-face pos (min (1+ (point)) (point-max)))
2879 ;; Must handle string literals explicitly inside the declaration.
2880 (goto-char start)
2881 (while (re-search-forward
2882 "\"\\([^\\\"]\\|\\\\.\\)*\"\\|'\\([^\\']\\|\\\\.\\)*'"
2883 end 'move)
2884 (c-put-font-lock-string-face (match-beginning 0)
2885 (point)))
2887 ;; Fontify types after keywords that always are followed
2888 ;; by them.
2889 (goto-char kwd-pos)
2890 (when (looking-at autodoc-decl-type-keywords)
2891 (c-fontify-types-and-refs ((c-promote-possible-types t))
2892 (goto-char start)
2893 (c-forward-syntactic-ws)
2894 (c-forward-type))))
2896 ;; Mark each whole line as markup, as long as the logical line
2897 ;; continues.
2898 (while (progn
2899 (c-put-font-lock-face (point)
2900 (progn (end-of-line) (point))
2901 markup-faces)
2902 (and (eq (char-before) ?@)
2903 (not (eobp))
2904 (progn (forward-char)
2905 (skip-syntax-forward " ")
2906 (looking-at c-current-comment-prefix))))
2907 (goto-char (match-end 0))))))
2909 nil)
2911 (defconst autodoc-font-lock-doc-comments
2912 `(("@\\(\\w+{\\|\\[\\([^]@\n\r]\\|@@\\)*\\]\\|[@}]\\|$\\)"
2913 ;; In-text markup.
2914 0 ,c-doc-markup-face-name prepend nil)
2915 (autodoc-font-lock-line-markup)
2916 ;; Fontify remaining markup characters as invalid.
2917 (,(lambda (limit)
2918 (c-find-invalid-doc-markup "@" limit))
2919 0 'font-lock-warning-face prepend nil)
2922 (defun autodoc-font-lock-keywords ()
2923 ;; Note that we depend on that `c-current-comment-prefix' has got
2924 ;; its proper value here.
2926 ;; This function might do hidden buffer changes.
2928 ;; The `c-type' text property with `c-decl-end' is used to mark the
2929 ;; end of the `autodoc-decl-keywords' occurrences to fontify the
2930 ;; following declarations.
2931 (setq c-type-decl-end-used t)
2933 `((,(lambda (limit)
2934 (c-font-lock-doc-comments "/[*/]!" limit
2935 autodoc-font-lock-doc-comments)))))
2938 ;; 2006-07-10: awk-font-lock-keywords has been moved back to cc-awk.el.
2939 (cc-provide 'cc-fonts)
2941 ;; Local Variables:
2942 ;; indent-tabs-mode: t
2943 ;; tab-width: 8
2944 ;; End:
2945 ;;; cc-fonts.el ends here