1 ;;; cc-fonts.el --- font lock support for CC Mode
3 ;; Copyright (C) 2002-2011 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
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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
48 ;; TBD: We should probably provide real faces for the above uses and
49 ;; instead initialize them from the standard faces.
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.
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
)
73 (load "cc-bytecomp" nil t
)))
76 (cc-require-when-compile 'cc-langs
)
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
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
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
143 (defconst c-reference-face-name
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
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
)
158 'font-lock-doc-string-face
)
159 ((c-face-name-p 'font-lock-doc-face
)
160 ;; Emacs 21 and later.
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
170 'font-lock-doc-markup-face
173 (defconst c-negation-char-face-name
174 (if (c-face-name-p 'font-lock-negation-char-face
)
175 ;; Emacs 22 has a special face for negation chars.
176 'font-lock-negation-char-face
))
178 (cc-bytecomp-defun face-inverse-video-p) ; Only in Emacs.
179 (cc-bytecomp-defun face-property-instance) ; Only in XEmacs.
181 (defun c-make-inverse-face (oldface newface
)
182 ;; Emacs and XEmacs have completely different face manipulation
184 (copy-face oldface newface
)
185 (cond ((fboundp 'face-inverse-video-p
)
186 ;; Emacs. This only looks at the inverse flag in the current
187 ;; frame. Other display configurations might be different,
188 ;; but it can only show if the same Emacs has frames on
189 ;; e.g. a color and a monochrome display simultaneously.
190 (unless (face-inverse-video-p oldface
)
191 (invert-face newface
)))
192 ((fboundp 'face-property-instance
)
193 ;; XEmacs. Same pitfall here.
194 (unless (face-property-instance oldface
'reverse
)
195 (invert-face newface
)))))
197 (defvar c-annotation-face
(make-face 'c-annotation-face
)
198 "Face used to highlight annotations in java-mode and other modes that may wish to use it.")
199 (set-face-foreground 'c-annotation-face
"blue")
202 ;; We need the following definitions during compilation since they're
203 ;; used when the `c-lang-defconst' initializers are evaluated. Define
204 ;; them at runtime too for the sake of derived modes.
206 ;; This indicates the "font locking context", and is set just before
207 ;; fontification is done. If non-nil, it says, e.g., point starts
208 ;; from within a #if preprocessor construct.
209 (defvar c-font-lock-context nil
)
210 (make-variable-buffer-local 'c-font-lock-context
)
212 (defmacro c-put-font-lock-face
(from to face
)
213 ;; Put a face on a region (overriding any existing face) in the way
214 ;; font-lock would do it. In XEmacs that means putting an
215 ;; additional font-lock property, or else the font-lock package
216 ;; won't recognize it as fontified and might override it
219 ;; This function does a hidden buffer change.
220 (if (fboundp 'font-lock-set-face
)
221 ;; Note: This function has no docstring in XEmacs so it might be
222 ;; considered internal.
223 `(font-lock-set-face ,from
,to
,face
)
224 `(put-text-property ,from
,to
'face
,face
)))
226 (defmacro c-remove-font-lock-face
(from to
)
227 ;; This is the inverse of `c-put-font-lock-face'.
229 ;; This function does a hidden buffer change.
230 (if (fboundp 'font-lock-remove-face
)
231 `(font-lock-remove-face ,from
,to
)
232 `(remove-text-properties ,from
,to
'(face nil
))))
234 (defmacro c-put-font-lock-string-face
(from to
)
235 ;; Put `font-lock-string-face' on a string. The surrounding
236 ;; quotes are included in Emacs but not in XEmacs. The passed
237 ;; region should include them.
239 ;; This function does a hidden buffer change.
240 (if (featurep 'xemacs
)
241 `(c-put-font-lock-face (1+ ,from
) (1- ,to
) 'font-lock-string-face
)
242 `(c-put-font-lock-face ,from
,to
'font-lock-string-face
)))
244 (defmacro c-fontify-types-and-refs
(varlist &rest body
)
245 ;; Like `let', but additionally activates `c-record-type-identifiers'
246 ;; and `c-record-ref-identifiers', and fontifies the recorded ranges
247 ;; accordingly on exit.
249 ;; This function does hidden buffer changes.
250 `(let ((c-record-type-identifiers t
)
251 c-record-ref-identifiers
253 (prog1 (progn ,@body
)
254 (c-fontify-recorded-types-and-refs))))
255 (put 'c-fontify-types-and-refs
'lisp-indent-function
1)
257 (defun c-skip-comments-and-strings (limit)
258 ;; If the point is within a region fontified as a comment or
259 ;; string literal skip to the end of it or to LIMIT, whichever
260 ;; comes first, and return t. Otherwise return nil. The match
261 ;; data is not clobbered.
263 ;; This function might do hidden buffer changes.
264 (when (c-got-face-at (point) c-literal-faces
)
266 (goto-char (next-single-property-change
267 (point) 'face nil limit
))
268 (and (< (point) limit
)
269 (c-got-face-at (point) c-literal-faces
))))
272 (defun c-make-syntactic-matcher (regexp)
273 ;; Returns a byte compiled function suitable for use in place of a
274 ;; regexp string in a `font-lock-keywords' matcher, except that
275 ;; only matches outside comments and string literals count.
277 ;; This function does not do any hidden buffer changes, but the
278 ;; generated functions will. (They are however used in places
279 ;; covered by the font-lock context.)
283 (while (and (setq res
(re-search-forward ,regexp limit t
))
285 (goto-char (match-beginning 0))
286 (or (c-skip-comments-and-strings limit
)
288 (goto-char (match-end 0))
292 (defun c-make-font-lock-search-form (regexp highlights
)
293 ;; Return a lisp form which will fontify every occurrence of REGEXP
294 ;; (a regular expression, NOT a function) between POINT and `limit'
295 ;; with HIGHLIGHTS, a list of highlighters as specified on page
296 ;; "Search-based Fontification" in the elisp manual.
297 `(while (re-search-forward ,regexp limit t
)
299 (goto-char (match-beginning 0))
300 (c-skip-comments-and-strings limit
))
301 (goto-char (match-end 0))
304 (if (integerp (car highlight
))
305 ;; e.g. highlight is (1 font-lock-type-face t)
307 (unless (eq (nth 2 highlight
) t
)
309 "The override flag must currently be t in %s"
311 (when (nth 3 highlight
)
313 "The laxmatch flag may currently not be set in %s"
316 (c-put-font-lock-face
317 (match-beginning ,(car highlight
))
318 (match-end ,(car highlight
))
319 ,(elt highlight
1))))
320 ;; highlight is an "ANCHORED HIGHLIGHER" of the form
321 ;; (ANCHORED-MATCHER PRE-FORM POST-FORM SUBEXP-HIGHLIGHTERS...)
322 (when (nth 3 highlight
)
323 (error "Match highlights currently not supported in %s"
327 (save-match-data ,(car highlight
))
328 ,(nth 2 highlight
))))
331 (defun c-make-font-lock-search-function (regexp &rest highlights
)
332 ;; This function makes a byte compiled function that works much like
333 ;; a matcher element in `font-lock-keywords'. It cuts out a little
334 ;; bit of the overhead compared to a real matcher. The main reason
335 ;; is however to pass the real search limit to the anchored
336 ;; matcher(s), since most (if not all) font-lock implementations
337 ;; arbitrarily limit anchored matchers to the same line, and also
338 ;; to insulate against various other irritating differences between
339 ;; the different (X)Emacs font-lock packages.
341 ;; REGEXP is the matcher, which must be a regexp. Only matches
342 ;; where the beginning is outside any comment or string literal are
345 ;; HIGHLIGHTS is a list of highlight specs, just like in
346 ;; `font-lock-keywords', with these limitations: The face is always
347 ;; overridden (no big disadvantage, since hits in comments etc are
348 ;; filtered anyway), there is no "laxmatch", and an anchored matcher
349 ;; is always a form which must do all the fontification directly.
350 ;; `limit' is a variable bound to the real limit in the context of
351 ;; the anchored matcher forms.
353 ;; This function does not do any hidden buffer changes, but the
354 ;; generated functions will. (They are however used in places
355 ;; covered by the font-lock context.)
357 ;; Note: Replace `byte-compile' with `eval' to debug the generated
358 ;; lambda more easily.
361 (let ( ;; The font-lock package in Emacs is known to clobber
362 ;; `parse-sexp-lookup-properties' (when it exists).
363 (parse-sexp-lookup-properties
364 (cc-eval-when-compile
365 (boundp 'parse-sexp-lookup-properties
))))
367 ;; (while (re-search-forward ,regexp limit t)
369 ;; (goto-char (match-beginning 0))
370 ;; (c-skip-comments-and-strings limit))
371 ;; (goto-char (match-end 0))
373 ;; (lambda (highlight)
374 ;; (if (integerp (car highlight))
376 ;; (unless (eq (nth 2 highlight) t)
378 ;; "The override flag must currently be t in %s"
380 ;; (when (nth 3 highlight)
382 ;; "The laxmatch flag may currently not be set in %s"
385 ;; (c-put-font-lock-face
386 ;; (match-beginning ,(car highlight))
387 ;; (match-end ,(car highlight))
388 ;; ,(elt highlight 1))))
389 ;; (when (nth 3 highlight)
390 ;; (error "Match highlights currently not supported in %s"
393 ;; ,(nth 1 highlight)
394 ;; (save-match-data ,(car highlight))
395 ;; ,(nth 2 highlight))))
397 ,(c-make-font-lock-search-form regexp highlights
))
401 (defun c-make-font-lock-BO-decl-search-function (regexp &rest highlights
)
402 ;; This function makes a byte compiled function that first moves back
403 ;; to the beginning of the current declaration (if any), then searches
404 ;; forward for matcher elements (as in `font-lock-keywords') and
407 ;; The motivation for moving back to the declaration start is to
408 ;; establish a context for the current text when, e.g., a character
409 ;; is typed on a C++ inheritance continuation line, or a jit-lock
410 ;; chunk starts there.
412 ;; The new function works much like a matcher element in
413 ;; `font-lock-keywords'. It cuts out a little bit of the overhead
414 ;; compared to a real matcher. The main reason is however to pass the
415 ;; real search limit to the anchored matcher(s), since most (if not
416 ;; all) font-lock implementations arbitrarily limit anchored matchers
417 ;; to the same line, and also to insulate against various other
418 ;; irritating differences between the different (X)Emacs font-lock
421 ;; REGEXP is the matcher, which must be a regexp. Only matches
422 ;; where the beginning is outside any comment or string literal are
425 ;; HIGHLIGHTS is a list of highlight specs, just like in
426 ;; `font-lock-keywords', with these limitations: The face is always
427 ;; overridden (no big disadvantage, since hits in comments etc are
428 ;; filtered anyway), there is no "laxmatch", and an anchored matcher
429 ;; is always a form which must do all the fontification directly.
430 ;; `limit' is a variable bound to the real limit in the context of
431 ;; the anchored matcher forms.
433 ;; This function does not do any hidden buffer changes, but the
434 ;; generated functions will. (They are however used in places
435 ;; covered by the font-lock context.)
437 ;; Note: Replace `byte-compile' with `eval' to debug the generated
438 ;; lambda more easily.
441 (let ( ;; The font-lock package in Emacs is known to clobber
442 ;; `parse-sexp-lookup-properties' (when it exists).
443 (parse-sexp-lookup-properties
444 (cc-eval-when-compile
445 (boundp 'parse-sexp-lookup-properties
))))
447 (let ((here (point)))
448 (if (eq (car (c-beginning-of-decl-1)) 'same
)
451 ,(c-make-font-lock-search-form regexp highlights
))
454 (defun c-make-font-lock-context-search-function (normal &rest state-stanzas
)
455 ;; This function makes a byte compiled function that works much like
456 ;; a matcher element in `font-lock-keywords', with the following
457 ;; enhancement: the generated function will test for particular "font
458 ;; lock contexts" at the start of the region, i.e. is this point in
459 ;; the middle of some particular construct? if so the generated
460 ;; function will first fontify the tail of the construct, before
461 ;; going into the main loop and fontify full constructs up to limit.
463 ;; The generated function takes one parameter called `limit', and
464 ;; will fontify the region between POINT and LIMIT.
466 ;; NORMAL is a list of the form (REGEXP HIGHLIGHTS .....), and is
467 ;; used to fontify the "regular" bit of the region.
468 ;; STATE-STANZAS is list of elements of the form (STATE LIM REGEXP
469 ;; HIGHLIGHTS), each element coding one possible font lock context.
471 ;; o - REGEXP is a font-lock regular expression (NOT a function),
472 ;; o - HIGHLIGHTS is a list of zero or more highlighters as defined
473 ;; on page "Search-based Fontification" in the elisp manual. As
474 ;; yet (2009-06), they must have OVERRIDE set, and may not have
477 ;; o - STATE is the "font lock context" (e.g. in-cpp-expr) and is
479 ;; o - LIM is a lisp form whose evaluation will yield the limit
480 ;; position in the buffer for fontification by this stanza.
482 ;; This function does not do any hidden buffer changes, but the
483 ;; generated functions will. (They are however used in places
484 ;; covered by the font-lock context.)
486 ;; Note: Replace `byte-compile' with `eval' to debug the generated
487 ;; lambda more easily.
490 (let ( ;; The font-lock package in Emacs is known to clobber
491 ;; `parse-sexp-lookup-properties' (when it exists).
492 (parse-sexp-lookup-properties
493 (cc-eval-when-compile
494 (boundp 'parse-sexp-lookup-properties
))))
497 (let ((state (car stanza
))
499 (regexp (nth 2 stanza
))
500 (highlights (cdr (cddr stanza
))))
501 `(if (eq c-font-lock-context
',state
)
503 ,(c-make-font-lock-search-form
504 regexp highlights
)))))
506 ,(c-make-font-lock-search-form (car normal
) (cdr normal
))
509 ; (eval-after-load "edebug" ; 2006-07-09: def-edebug-spec is now in subr.el.
511 (def-edebug-spec c-fontify-types-and-refs let
*)
512 (def-edebug-spec c-make-syntactic-matcher t
)
513 ;; If there are literal quoted or backquoted highlight specs in
514 ;; the call to `c-make-font-lock-search-function' then let's
515 ;; instrument the forms in them.
516 (def-edebug-spec c-make-font-lock-search-function
517 (form &rest
&or
("quote" (&rest form
)) ("`" (&rest form
)) form
)));))
519 (defun c-fontify-recorded-types-and-refs ()
520 ;; Convert the ranges recorded on `c-record-type-identifiers' and
521 ;; `c-record-ref-identifiers' to fontification.
523 ;; This function does hidden buffer changes.
525 (while (consp c-record-type-identifiers
)
526 (setq elem
(car c-record-type-identifiers
)
527 c-record-type-identifiers
(cdr c-record-type-identifiers
))
528 (c-put-font-lock-face (car elem
) (cdr elem
)
529 'font-lock-type-face
))
530 (while c-record-ref-identifiers
531 (setq elem
(car c-record-ref-identifiers
)
532 c-record-ref-identifiers
(cdr c-record-ref-identifiers
))
533 ;; Note that the reference face is a variable that is
534 ;; dereferenced, since it's an alias in Emacs.
535 (c-put-font-lock-face (car elem
) (cdr elem
)
536 c-reference-face-name
))))
538 (c-lang-defconst c-cpp-matchers
539 "Font lock matchers for preprocessor directives and purely lexical
540 stuff. Used on level 1 and higher."
542 ;; Note: `c-font-lock-declarations' assumes that no matcher here
543 ;; sets `font-lock-type-face' in languages where
544 ;; `c-recognize-<>-arglists' is set.
546 t
`(,@(when (c-lang-const c-opt-cpp-prefix
)
547 (let* ((noncontinued-line-end "\\(\\=\\|\\(\\=\\|[^\\]\\)[\n\r]\\)")
548 (ncle-depth (regexp-opt-depth noncontinued-line-end
))
549 (sws-depth (c-lang-const c-syntactic-ws-depth
))
550 (nsws-depth (c-lang-const c-nonempty-syntactic-ws-depth
)))
552 `(;; The stuff after #error and #warning is a message, so
553 ;; fontify it as a string.
554 ,@(when (c-lang-const c-cpp-message-directives
)
555 (let* ((re (c-make-keywords-re 'appendable
; nil
556 (c-lang-const c-cpp-message-directives
)))
557 (re-depth (regexp-opt-depth re
)))
558 `((,(concat noncontinued-line-end
559 (c-lang-const c-opt-cpp-prefix
)
562 ,(+ ncle-depth re-depth
1) font-lock-string-face t
))))
564 ;; Fontify filenames in #include <...> as strings.
565 ,@(when (c-lang-const c-cpp-include-directives
)
566 (let* ((re (c-make-keywords-re nil
567 (c-lang-const c-cpp-include-directives
)))
568 (re-depth (regexp-opt-depth re
)))
569 `((,(concat noncontinued-line-end
570 (c-lang-const c-opt-cpp-prefix
)
572 (c-lang-const c-syntactic-ws
)
573 "\\(<[^>\n\r]*>?\\)")
574 (,(+ ncle-depth re-depth sws-depth
1)
575 font-lock-string-face
)
577 ;; Use an anchored matcher to put paren syntax
581 (let ((beg (match-beginning
582 ,(+ ncle-depth re-depth sws-depth
1)))
583 (end (1- (match-end ,(+ ncle-depth re-depth
585 (if (eq (char-after end
) ?
>)
587 (c-mark-<-as-paren beg
)
588 (c-mark->-as-paren end
))
589 ;; (c-clear-char-property beg 'syntax-table)
590 (c-clear-char-property beg
'category
)))
594 ,@(when (c-lang-const c-opt-cpp-macro-define
)
595 `((,(c-make-font-lock-search-function
597 noncontinued-line-end
598 (c-lang-const c-opt-cpp-prefix
)
599 (c-lang-const c-opt-cpp-macro-define
)
600 (c-lang-const c-nonempty-syntactic-ws
)
601 "\\(" (c-lang-const ; 1 + ncle + nsws
603 (concat "\\(" ; 2 + ncle + nsws + c-sym-key
604 ;; Macro with arguments - a "function".
605 "\\(\(\\)" ; 3 + ncle + nsws + c-sym-key
607 ;; Macro without arguments - a "variable".
610 `((if (match-beginning
611 ,(+ 3 ncle-depth nsws-depth
612 (c-lang-const c-symbol-key-depth
)))
614 ;; "Function". Fontify the name and the arguments.
616 (c-put-font-lock-face
617 (match-beginning ,(+ 1 ncle-depth nsws-depth
))
618 (match-end ,(+ 1 ncle-depth nsws-depth
))
619 'font-lock-function-name-face
)
622 ,(+ 3 ncle-depth nsws-depth
623 (c-lang-const c-symbol-key-depth
))))
625 (narrow-to-region (point-min) limit
)
628 (c-forward-syntactic-ws)
629 (looking-at c-symbol-key
))
631 (c-put-font-lock-face
632 (match-beginning 0) (match-end 0)
633 'font-lock-variable-name-face
)
634 (goto-char (match-end 0))
635 (c-forward-syntactic-ws)
636 (eq (char-after) ?
,)))
640 (c-put-font-lock-face
641 (match-beginning ,(+ 1 ncle-depth nsws-depth
))
642 (match-end ,(+ 1 ncle-depth nsws-depth
))
643 'font-lock-variable-name-face
)))))))
645 ;; Fontify cpp function names in preprocessor
646 ;; expressions in #if and #elif.
647 ,@(when (and (c-lang-const c-cpp-expr-directives
)
648 (c-lang-const c-cpp-expr-functions
))
649 (let ((ced-re (c-make-keywords-re t
650 (c-lang-const c-cpp-expr-directives
)))
651 (cef-re (c-make-keywords-re t
652 (c-lang-const c-cpp-expr-functions
))))
654 `((,(c-make-font-lock-context-search-function
655 `(,(concat noncontinued-line-end
656 (c-lang-const c-opt-cpp-prefix
)
657 ced-re
; 1 + ncle-depth
658 ;; Match the whole logical line to look
659 ;; for the functions in.
660 "\\(\\\\\\(.\\|[\n\r]\\)\\|[^\n\r]\\)*")
661 ((let ((limit (match-end 0)))
662 (while (re-search-forward ,cef-re limit
'move
)
663 (c-put-font-lock-face (match-beginning 1)
665 c-preprocessor-face-name
)))
666 (goto-char (match-end ,(1+ ncle-depth
)))))
668 (save-excursion (c-end-of-macro) (point))
670 (1 c-preprocessor-face-name t
)))))))
672 ;; Fontify the directive names.
673 (,(c-make-font-lock-search-function
674 (concat noncontinued-line-end
676 (c-lang-const c-opt-cpp-prefix
)
677 "[" (c-lang-const c-symbol-chars
) "]+"
679 `(,(1+ ncle-depth
) c-preprocessor-face-name t
)))
681 (eval .
(list ,(c-make-syntactic-matcher
682 (concat noncontinued-line-end
683 (c-lang-const c-opt-cpp-prefix
)
686 c-negation-char-face-name
690 ,@(when (c-major-mode-is 'pike-mode
)
691 ;; Recognize hashbangs in Pike.
692 `((eval .
(list "\\`#![^\n\r]*"
693 0 c-preprocessor-face-name
))))
695 ;; Make hard spaces visible through an inverted `font-lock-warning-face'.
699 (unless (c-face-name-p 'c-nonbreakable-space-face
)
700 (c-make-inverse-face 'font-lock-warning-face
701 'c-nonbreakable-space-face
))
702 ''c-nonbreakable-space-face
)))
705 (defun c-font-lock-invalid-string ()
706 ;; Assuming the point is after the opening character of a string,
707 ;; fontify that char with `font-lock-warning-face' if the string
708 ;; decidedly isn't terminated properly.
710 ;; This function does hidden buffer changes.
711 (let ((start (1- (point))))
713 (and (eq (elt (parse-partial-sexp start
(c-point 'eol
)) 8) start
)
714 (if (integerp c-multiline-string-start-char
)
715 ;; There's no multiline string start char before the
716 ;; string, so newlines aren't allowed.
717 (not (eq (char-before start
) c-multiline-string-start-char
))
718 ;; Multiline strings are allowed anywhere if
719 ;; c-multiline-string-start-char is t.
720 (not c-multiline-string-start-char
))
721 (if c-string-escaped-newlines
722 ;; There's no \ before the newline.
723 (not (eq (char-before (point)) ?
\\))
724 ;; Escaped newlines aren't supported.
726 (c-put-font-lock-face start
(1+ start
) 'font-lock-warning-face
)))))
728 (c-lang-defconst c-basic-matchers-before
729 "Font lock matchers for basic keywords, labels, references and various
730 other easily recognizable things that should be fontified before generic
731 casts and declarations are fontified. Used on level 2 and higher."
733 ;; Note: `c-font-lock-declarations' assumes that no matcher here
734 ;; sets `font-lock-type-face' in languages where
735 ;; `c-recognize-<>-arglists' is set.
737 t
`(;; Put a warning face on the opener of unclosed strings that
738 ;; can't span lines. Later font
739 ;; lock packages have a `font-lock-syntactic-face-function' for
740 ;; this, but it doesn't give the control we want since any
741 ;; fontification done inside the function will be
742 ;; unconditionally overridden.
743 ,(c-make-font-lock-search-function
744 ;; Match a char before the string starter to make
745 ;; `c-skip-comments-and-strings' work correctly.
746 (concat ".\\(" c-string-limit-regexp
"\\)")
747 '((c-font-lock-invalid-string)))
749 ;; Fontify keyword constants.
750 ,@(when (c-lang-const c-constant-kwds
)
751 (let ((re (c-make-keywords-re nil
(c-lang-const c-constant-kwds
))))
752 (if (c-major-mode-is 'pike-mode
)
753 ;; No symbol is a keyword after "->" in Pike.
754 `((eval .
(list ,(concat "\\(\\=.?\\|[^>]\\|[^-]>\\)"
755 "\\<\\(" re
"\\)\\>")
756 2 c-constant-face-name
)))
757 `((eval .
(list ,(concat "\\<\\(" re
"\\)\\>")
758 1 c-constant-face-name
))))))
760 ;; Fontify all keywords except the primitive types.
761 ,(if (c-major-mode-is 'pike-mode
)
762 ;; No symbol is a keyword after "->" in Pike.
763 `(,(concat "\\(\\=.?\\|[^>]\\|[^-]>\\)"
764 "\\<" (c-lang-const c-regular-keywords-regexp
))
765 2 font-lock-keyword-face
)
766 `(,(concat "\\<" (c-lang-const c-regular-keywords-regexp
))
767 1 font-lock-keyword-face
))
769 ;; Fontify leading identifiers in fully qualified names like
770 ;; "foo::bar" in languages that supports such things.
771 ,@(when (c-lang-const c-opt-identifier-concat-key
)
772 (if (c-major-mode-is 'java-mode
)
773 ;; Java needs special treatment since "." is used both to
774 ;; qualify names and in normal indexing. Here we look for
775 ;; capital characters at the beginning of an identifier to
776 ;; recognize the class. "*" is also recognized to cover
777 ;; wildcard import declarations. All preceding dot separated
778 ;; identifiers are taken as package names and therefore
779 ;; fontified as references.
780 `(,(c-make-font-lock-search-function
781 ;; Search for class identifiers preceded by ".". The
782 ;; anchored matcher takes it from there.
783 (concat (c-lang-const c-opt-identifier-concat-key
)
784 (c-lang-const c-simple-ws
) "*"
787 "[" (c-lang-const c-symbol-chars
) "]*"
792 (goto-char (1+ (match-beginning 0)))
793 (while (and (eq (char-before) ?.
)
796 (c-backward-syntactic-ws)
797 (setq id-end
(point))
798 (< (skip-chars-backward
799 ,(c-lang-const c-symbol-chars
)) 0))
800 (not (get-text-property (point) 'face
)))
801 (c-put-font-lock-face (point) id-end
802 c-reference-face-name
)
803 (c-backward-syntactic-ws)))
805 (goto-char (match-end 0)))))
808 ;; Must use a function here since we match longer than
809 ;; we want to move before doing a new search. This is
810 ;; not necessary for XEmacs since it restarts the
811 ;; search from the end of the first highlighted
812 ;; submatch (something that causes problems in other
815 (while (re-search-forward
816 ,(concat "\\(\\<" ; 1
817 "\\(" (c-lang-const c-symbol-key
) "\\)" ; 2
818 (c-lang-const c-simple-ws
) "*"
819 (c-lang-const c-opt-identifier-concat-key
)
820 (c-lang-const c-simple-ws
) "*"
823 (c-lang-const c-opt-after-id-concat-key
)
827 (goto-char (match-beginning 0))
828 (c-skip-comments-and-strings limit
))
829 (or (get-text-property (match-beginning 2) 'face
)
830 (c-put-font-lock-face (match-beginning 2)
832 c-reference-face-name
))
833 (goto-char (match-end 1))))))))))
835 ;; Fontify the special declarations in Objective-C.
836 ,@(when (c-major-mode-is 'objc-mode
)
837 `(;; Fontify class names in the beginning of message expressions.
838 ,(c-make-font-lock-search-function
840 '((c-fontify-types-and-refs ()
841 (c-forward-syntactic-ws limit
)
842 (let ((start (point)))
843 ;; In this case we accept both primitive and known types.
844 (when (eq (c-forward-type) 'known
)
846 (let ((c-promote-possible-types t
))
848 (if (> (point) limit
) (goto-char limit
)))))
850 ;; The @interface/@implementation/@protocol directives.
851 ,(c-make-font-lock-search-function
854 '("@interface" "@implementation" "@protocol")
857 '((c-fontify-types-and-refs
858 (;; The font-lock package in Emacs is known to clobber
859 ;; `parse-sexp-lookup-properties' (when it exists).
860 (parse-sexp-lookup-properties
861 (cc-eval-when-compile
862 (boundp 'parse-sexp-lookup-properties
))))
863 (c-forward-objc-directive)
865 (goto-char (match-beginning 0))))))
867 (eval .
(list "\\(!\\)[^=]" 1 c-negation-char-face-name
))
870 (defun c-font-lock-complex-decl-prepare (limit)
871 ;; This function will be called from font-lock for a region bounded by POINT
872 ;; and LIMIT, as though it were to identify a keyword for
873 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
874 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
877 ;; Called before any of the matchers in `c-complex-decl-matchers'.
879 ;; This function does hidden buffer changes.
881 ;;(message "c-font-lock-complex-decl-prepare %s %s" (point) limit)
883 ;; Clear the list of found types if we start from the start of the
884 ;; buffer, to make it easier to get rid of misspelled types and
885 ;; variables that have gotten recognized as types in malformed code.
887 (c-clear-found-types))
889 ;; Clear the c-type char properties which mark the region, to recalculate
890 ;; them properly. The most interesting properties are those put on the
891 ;; closest token before the region.
894 (c-backward-syntactic-ws)
895 (c-clear-char-properties
896 (if (and (not (bobp))
897 (memq (c-get-char-property (1- (point)) 'c-type
)
906 ;; Update `c-state-cache' to the beginning of the region. This will
907 ;; make `c-beginning-of-syntax' go faster when it's used later on,
908 ;; and it's near the point most of the time.
911 ;; Check if the fontified region starts inside a declarator list so
912 ;; that `c-font-lock-declarators' should be called at the start.
913 ;; The declared identifiers are font-locked correctly as types, if
914 ;; that is what they are.
915 (let ((prop (save-excursion
916 (c-backward-syntactic-ws)
918 (c-get-char-property (1- (point)) 'c-type
)))))
919 (when (memq prop
'(c-decl-id-start c-decl-type-start
))
920 (c-forward-syntactic-ws limit
)
921 (c-font-lock-declarators limit t
(eq prop
'c-decl-type-start
))))
923 (setq c-font-lock-context
;; (c-guess-font-lock-context)
925 (if (and c-cpp-expr-intro-re
926 (c-beginning-of-macro)
927 (looking-at c-cpp-expr-intro-re
))
931 (defun c-font-lock-<>-arglists
(limit)
932 ;; This function will be called from font-lock for a region bounded by POINT
933 ;; and LIMIT, as though it were to identify a keyword for
934 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
935 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
938 ;; Fontify types and references in names containing angle bracket
939 ;; arglists from the point to LIMIT. Note that
940 ;; `c-font-lock-declarations' already has handled many of them.
942 ;; This function might do hidden buffer changes.
944 (let (;; The font-lock package in Emacs is known to clobber
945 ;; `parse-sexp-lookup-properties' (when it exists).
946 (parse-sexp-lookup-properties
947 (cc-eval-when-compile
948 (boundp 'parse-sexp-lookup-properties
)))
949 (c-parse-and-markup-<>-arglists t
)
950 c-restricted-
<>-arglists
951 id-start id-end id-face pos kwd-sym
)
953 (while (and (< (point) limit
)
954 (re-search-forward c-opt-
<>-arglist-start limit t
))
956 (setq id-start
(match-beginning 1)
961 (unless (c-skip-comments-and-strings limit
)
963 c-restricted-
<>-arglists nil
964 id-face
(get-text-property id-start
'face
))
967 ((eq id-face
'font-lock-type-face
)
968 ;; The identifier got the type face so it has already been
969 ;; handled in `c-font-lock-declarations'.
972 ((eq id-face
'font-lock-keyword-face
)
973 (when (looking-at c-opt-
<>-sexp-key
)
974 ;; There's a special keyword before the "<" that tells
975 ;; that it's an angle bracket arglist.
976 (setq kwd-sym
(c-keyword-sym (match-string 1)))))
979 ;; There's a normal identifier before the "<". If we're not in
980 ;; a declaration context then we set `c-restricted-<>-arglists'
981 ;; to avoid recognizing templates in function calls like "foo (a
983 (c-backward-syntactic-ws)
984 (when (and (memq (char-before) '(?\
( ?
,))
985 (not (eq (get-text-property (1- (point)) 'c-type
)
987 (setq c-restricted-
<>-arglists t
))
992 ;; Check for comment/string both at the identifier and
994 (unless (c-skip-comments-and-strings limit
)
996 (c-fontify-types-and-refs ()
997 (when (c-forward-<>-arglist
(c-keyword-member
998 kwd-sym
'c-
<>-type-kwds
))
999 (when (and c-opt-identifier-concat-key
1000 (not (get-text-property id-start
'face
)))
1001 (c-forward-syntactic-ws)
1002 (if (looking-at c-opt-identifier-concat-key
)
1003 (c-put-font-lock-face id-start id-end
1004 c-reference-face-name
)
1005 (c-put-font-lock-face id-start id-end
1006 'font-lock-type-face
)))))
1012 (defun c-font-lock-declarators (limit list types
)
1013 ;; Assuming the point is at the start of a declarator in a declaration,
1014 ;; fontify the identifier it declares. (If TYPES is set, it does this via
1015 ;; the macro `c-fontify-types-and-refs'.)
1017 ;; If LIST is non-nil, also fontify the ids in any following declarators in
1018 ;; a comma separated list (e.g. "foo" and "*bar" in "int foo = 17, *bar;");
1019 ;; additionally, mark the commas with c-type property 'c-decl-id-start or
1020 ;; 'c-decl-type-start (according to TYPES). Stop at LIMIT.
1022 ;; If TYPES is non-nil, fontify all identifiers as types.
1024 ;; Nil is always returned. The function leaves point at the delimiter after
1025 ;; the last declarator it processes.
1027 ;; This function might do hidden buffer changes.
1029 ;;(message "c-font-lock-declarators from %s to %s" (point) limit)
1030 (c-fontify-types-and-refs
1031 ((pos (point)) next-pos id-start id-end
1034 c-last-identifier-range
1035 (separator-prop (if types
'c-decl-type-start
'c-decl-id-start
)))
1037 ;; The following `while' fontifies a single declarator id each time round.
1038 ;; It loops only when LIST is non-nil.
1040 ;; Inside the following "condition form", we move forward over the
1041 ;; declarator's identifier up as far as any opening bracket (for array
1042 ;; size) or paren (for parameters of function-type) or brace (for
1043 ;; array/struct initialisation) or "=" or terminating delimiter
1044 ;; (e.g. "," or ";" or "}").
1049 ;; The following form moves forward over the declarator's
1050 ;; identifier (and what precedes it), returning t. If there
1051 ;; wasn't one, it returns nil, terminating the `while'.
1052 (let (got-identifier)
1053 (setq paren-depth
0)
1054 ;; Skip over type decl prefix operators, one for each iteration
1055 ;; of the while. These are, e.g. "*" in "int *foo" or "(" and
1056 ;; "*" in "int (*foo) (void)" (Note similar code in
1057 ;; `c-forward-decl-or-cast-1'.)
1058 (while (and (looking-at c-type-decl-prefix-key
)
1059 (if (and (c-major-mode-is 'c
++-mode
)
1060 (match-beginning 3))
1061 ;; If the third submatch matches in C++ then
1062 ;; we're looking at an identifier that's a
1063 ;; prefix only if it specifies a member pointer.
1065 (setq id-start
(point))
1067 (if (looking-at "\\(::\\)")
1068 ;; We only check for a trailing "::" and
1069 ;; let the "*" that should follow be
1070 ;; matched in the next round.
1072 ;; It turned out to be the real identifier,
1073 ;; so flag that and stop.
1074 (setq got-identifier t
)
1077 (if (eq (char-after) ?\
()
1079 (setq paren-depth
(1+ paren-depth
))
1081 (goto-char (match-end 1)))
1082 (c-forward-syntactic-ws))
1084 ;; If we haven't passed the identifier already, do it now.
1085 (unless got-identifier
1086 (setq id-start
(point))
1088 (setq id-end
(point))
1092 ;; Skip out of the parens surrounding the identifier. If closing
1093 ;; parens are missing, this form returns nil.
1094 (or (= paren-depth
0)
1095 (c-safe (goto-char (scan-lists (point) 1 paren-depth
))))
1099 ;; Skip over any trailing bit, such as "__attribute__".
1101 (when (looking-at c-decl-hangon-key
)
1102 (c-forward-keyword-clause 1))
1105 ;; Search syntactically to the end of the declarator (";",
1106 ;; ",", a closen paren, eob etc) or to the beginning of an
1107 ;; initializer or function prototype ("=" or "\\s\(").
1108 ;; Note that the open paren will match array specs in
1109 ;; square brackets, and we treat them as initializers too.
1110 (c-syntactic-re-search-forward
1111 "[;,]\\|\\s)\\|\\'\\|\\(=\\|\\s(\\)" limit t t
))
1113 (setq next-pos
(match-beginning 0)
1114 id-face
(if (and (eq (char-after next-pos
) ?\
()
1115 (let (c-last-identifier-range)
1117 (goto-char next-pos
)
1118 (c-at-toplevel-p))))
1119 'font-lock-function-name-face
1120 'font-lock-variable-name-face
)
1121 got-init
(and (match-beginning 1)
1122 (char-after (match-beginning 1))))
1125 ;; Register and fontify the identifer as a type.
1126 (let ((c-promote-possible-types t
))
1127 (goto-char id-start
)
1129 ;; Fontify the last symbol in the identifier if it isn't fontified
1130 ;; already. The check is necessary only in certain cases where this
1131 ;; function is used "sloppily", e.g. in `c-simple-decl-matchers'.
1132 (when (and c-last-identifier-range
1133 (not (get-text-property (car c-last-identifier-range
)
1135 (c-put-font-lock-face (car c-last-identifier-range
)
1136 (cdr c-last-identifier-range
)
1139 (goto-char next-pos
)
1140 (setq pos nil
) ; So as to terminate the enclosing `while' form.
1142 ;; Jump past any initializer or function prototype to see if
1143 ;; there's a ',' to continue at.
1145 (cond ((eq id-face
'font-lock-function-name-face
)
1146 ;; Skip a parenthesized initializer (C++) or a function
1148 (if (c-safe (c-forward-sexp 1) t
) ; over the parameter list.
1149 (c-forward-syntactic-ws limit
)
1150 (goto-char limit
))) ; unbalanced parens
1152 (got-init ; "=" sign OR opening "(", "[", or "{"
1153 ;; Skip an initializer expression. If we're at a '='
1154 ;; then accept a brace list directly after it to cope
1155 ;; with array initializers. Otherwise stop at braces
1156 ;; to avoid going past full function and class blocks.
1157 (and (if (and (eq got-init ?
=)
1158 (= (c-forward-token-2 1 nil limit
) 0)
1160 (c-safe (c-forward-sexp) t
) ; over { .... }
1162 ;; FIXME: Should look for c-decl-end markers here;
1163 ;; we might go far into the following declarations
1164 ;; in e.g. ObjC mode (see e.g. methods-4.m).
1165 (c-syntactic-re-search-forward "[;,{]" limit
'move t
)
1168 (t (c-forward-syntactic-ws limit
)))
1170 ;; If a ',' is found we set pos to the next declarator and iterate.
1171 (when (and (< (point) limit
) (looking-at ","))
1172 (c-put-char-property (point) 'c-type separator-prop
)
1174 (c-forward-syntactic-ws limit
)
1175 (setq pos
(point)))))) ; acts to make the `while' form continue.
1178 (defconst c-font-lock-maybe-decl-faces
1179 ;; List of faces that might be put at the start of a type when
1180 ;; `c-font-lock-declarations' runs. This needs to be evaluated to
1181 ;; ensure that face name aliases in Emacs are resolved.
1184 c-reference-face-name
1185 font-lock-keyword-face
))
1187 (defun c-font-lock-declarations (limit)
1188 ;; Fontify all the declarations, casts and labels from the point to LIMIT.
1189 ;; Assumes that strings and comments have been fontified already.
1191 ;; This function will be called from font-lock for a region bounded by POINT
1192 ;; and LIMIT, as though it were to identify a keyword for
1193 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
1194 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
1197 ;; This function might do hidden buffer changes.
1199 ;;(message "c-font-lock-declarations search from %s to %s" (point) limit)
1202 (let (;; The position where `c-find-decl-spots' last stopped.
1204 ;; o - 'decl if we're in an arglist containing declarations
1205 ;; (but if `c-recognize-paren-inits' is set it might also be
1206 ;; an initializer arglist);
1207 ;; o - '<> if the arglist is of angle bracket type;
1208 ;; o - 'arglist if it's some other arglist;
1209 ;; o - nil, if not in an arglist at all. This includes the
1210 ;; parenthesised condition which follows "if", "while", etc.
1212 ;; The position of the next token after the closing paren of
1213 ;; the last detected cast.
1215 ;; The result from `c-forward-decl-or-cast-1'.
1217 ;; The maximum of the end positions of all the checked type
1218 ;; decl expressions in the successfully identified
1219 ;; declarations. The position might be either before or
1220 ;; after the syntactic whitespace following the last token
1221 ;; in the type decl expression.
1222 (max-type-decl-end 0)
1223 ;; Same as `max-type-decl-*', but used when we're before
1225 (max-type-decl-end-before-token 0)
1226 ;; Set according to the context to direct the heuristics for
1227 ;; recognizing C++ templates.
1228 c-restricted-
<>-arglists
1229 ;; Turn on recording of identifier ranges in
1230 ;; `c-forward-decl-or-cast-1' and `c-forward-label' for
1231 ;; later fontification.
1232 (c-record-type-identifiers t
)
1234 c-record-ref-identifiers
1235 ;; Make `c-forward-type' calls mark up template arglists if
1236 ;; it finds any. That's necessary so that we later will
1237 ;; stop inside them to fontify types there.
1238 (c-parse-and-markup-<>-arglists t
)
1239 ;; The font-lock package in Emacs is known to clobber
1240 ;; `parse-sexp-lookup-properties' (when it exists).
1241 (parse-sexp-lookup-properties
1242 (cc-eval-when-compile
1243 (boundp 'parse-sexp-lookup-properties
))))
1245 ;; Below we fontify a whole declaration even when it crosses the limit,
1246 ;; to avoid gaps when jit/lazy-lock fontifies the file a block at a
1247 ;; time. That is however annoying during editing, e.g. the following is
1248 ;; a common situation while the first line is being written:
1251 ;; some_other_variable = 0;
1253 ;; font-lock will put the limit at the beginning of the second line
1254 ;; here, and if we go past it we'll fontify "my_variable" as a type and
1255 ;; "some_other_variable" as an identifier, and the latter will not
1256 ;; correct itself until the second line is changed. To avoid that we
1257 ;; narrow to the limit if the region to fontify is a single line.
1258 (if (<= limit
(c-point 'bonl
))
1262 ;; Narrow after any operator chars following the limit though,
1263 ;; since those characters can be useful in recognizing a
1264 ;; declaration (in particular the '{' that opens a function body
1265 ;; after the header).
1267 (skip-chars-forward c-nonsymbol-chars
)
1273 c-font-lock-maybe-decl-faces
1275 (lambda (match-pos inside-macro
)
1276 (setq start-pos
(point))
1278 ;; The result of the form below is true when we don't recognize a
1279 ;; declaration or cast.
1280 (if (and (eq (get-text-property (point) 'face
)
1281 'font-lock-keyword-face
)
1282 (looking-at c-not-decl-init-keywords
))
1283 ;; Don't do anything more if we're looking at a keyword that
1284 ;; can't start a declaration.
1287 ;; Set `context' and `c-restricted-<>-arglists'. Look for
1288 ;; "<" for the sake of C++-style template arglists.
1289 ;; Ignore "(" when it's part of a control flow construct
1291 (let ((type (and (> match-pos
(point-min))
1292 (c-get-char-property (1- match-pos
) 'c-type
))))
1293 (cond ((not (memq (char-before match-pos
) '(?\
( ?
, ?\
[ ?
<)))
1295 c-restricted-
<>-arglists nil
))
1296 ;; A control flow expression
1297 ((and (eq (char-before match-pos
) ?\
()
1299 (goto-char match-pos
)
1301 (c-backward-token-2)
1302 (looking-at c-block-stmt-2-key
)))
1304 c-restricted-
<>-arglists t
))
1306 ((<= match-pos
(point-min))
1307 (setq context
'arglist
1308 c-restricted-
<>-arglists t
))
1309 ;; Got a cached hit in a declaration arglist.
1310 ((eq type
'c-decl-arg-start
)
1312 c-restricted-
<>-arglists nil
))
1313 ;; Inside an angle bracket arglist.
1314 ((or (eq type
'c-
<>-arg-sep
)
1315 (eq (char-before match-pos
) ?
<))
1317 c-restricted-
<>-arglists nil
))
1318 ;; Got a cached hit in some other type of arglist.
1320 (setq context
'arglist
1321 c-restricted-
<>-arglists t
))
1323 (< match-pos max-type-decl-end-before-token
)
1324 (< match-pos max-type-decl-end
))
1325 ;; The point is within the range of a previously
1326 ;; encountered type decl expression, so the arglist
1327 ;; is probably one that contains declarations.
1328 ;; However, if `c-recognize-paren-inits' is set it
1329 ;; might also be an initializer arglist.
1331 c-restricted-
<>-arglists nil
)
1332 ;; The result of this check is cached with a char
1333 ;; property on the match token, so that we can look
1334 ;; it up again when refontifying single lines in a
1335 ;; multiline declaration.
1336 (c-put-char-property (1- match-pos
)
1337 'c-type
'c-decl-arg-start
))
1338 (t (setq context
'arglist
1339 c-restricted-
<>-arglists t
))))
1341 ;; Check we haven't missed a preceding "typedef".
1342 (when (not (looking-at c-typedef-key
))
1343 (c-backward-syntactic-ws)
1344 (c-backward-token-2)
1345 (or (looking-at c-typedef-key
)
1346 (goto-char start-pos
)))
1348 ;; Now analyze the construct.
1349 ;; In QT, "more" is an irritating keyword that expands to nothing.
1350 ;; We skip over it to prevent recognition of "more slots: <symbol>"
1351 ;; as a bitfield declaration.
1352 (when (and (c-major-mode-is 'c
++-mode
)
1354 (concat "\\(more\\)\\([^" c-symbol-chars
"]\\|$\\)")))
1355 (goto-char (match-end 1))
1356 (c-forward-syntactic-ws))
1357 (setq decl-or-cast
(c-forward-decl-or-cast-1
1358 match-pos context last-cast-end
))
1361 ((eq decl-or-cast
'cast
)
1362 ;; Save the position after the previous cast so we can feed
1363 ;; it to `c-forward-decl-or-cast-1' in the next round. That
1364 ;; helps it discover cast chains like "(a) (b) c".
1365 (setq last-cast-end
(point))
1366 (c-fontify-recorded-types-and-refs)
1370 ;; We've found a declaration.
1372 ;; Set `max-type-decl-end' or `max-type-decl-end-before-token'
1373 ;; under the assumption that we're after the first type decl
1374 ;; expression in the declaration now. That's not really true;
1375 ;; we could also be after a parenthesized initializer
1376 ;; expression in C++, but this is only used as a last resort
1377 ;; to slant ambiguous expression/declarations, and overall
1378 ;; it's worth the risk to occasionally fontify an expression
1379 ;; as a declaration in an initializer expression compared to
1380 ;; getting ambiguous things in normal function prototypes
1381 ;; fontified as expressions.
1383 (when (> (point) max-type-decl-end-before-token
)
1384 (setq max-type-decl-end-before-token
(point)))
1385 (when (> (point) max-type-decl-end
)
1386 (setq max-type-decl-end
(point))))
1388 ;; Back up to the type to fontify the declarator(s).
1389 (goto-char (car decl-or-cast
))
1393 ;; Should normally not fontify a list of
1394 ;; declarators inside an arglist, but the first
1395 ;; argument in the ';' separated list of a "for"
1396 ;; statement is an exception.
1397 (when (eq (char-before match-pos
) ?\
()
1399 (goto-char (1- match-pos
))
1400 (c-backward-syntactic-ws)
1401 (and (c-simple-skip-symbol-backward)
1402 (looking-at c-paren-stmt-key
))))
1405 ;; Fix the `c-decl-id-start' or `c-decl-type-start' property
1406 ;; before the first declarator if it's a list.
1407 ;; `c-font-lock-declarators' handles the rest.
1410 (c-backward-syntactic-ws)
1412 (c-put-char-property (1- (point)) 'c-type
1413 (if (cdr decl-or-cast
)
1415 'c-decl-id-start
)))))
1417 (c-font-lock-declarators
1418 (point-max) decl-list
(cdr decl-or-cast
)))
1420 ;; A declaration has been successfully identified, so do all the
1421 ;; fontification of types and refs that've been recorded.
1422 (c-fontify-recorded-types-and-refs)
1426 ;; Are we at a declarator? Try to go back to the declaration
1427 ;; to check this. If we get there, check whether a "typedef"
1428 ;; is there, then fontify the declarators accordingly.
1429 (let ((decl-search-lim (max (- (point) 50000) (point-min)))
1430 paren-state bod-res encl-pos is-typedef
1431 c-recognize-knr-p
) ; Strictly speaking, bogus, but it
1432 ; speeds up lisp.h tremendously.
1434 (setq bod-res
(car (c-beginning-of-decl-1 decl-search-lim
)))
1435 (if (and (eq bod-res
'same
)
1437 (c-backward-syntactic-ws)
1438 (eq (char-before) ?\
})))
1439 (c-beginning-of-decl-1 decl-search-lim
))
1441 ;; We're now putatively at the declaration.
1442 (setq paren-state
(c-parse-state))
1443 ;; At top level or inside a "{"?
1444 (if (or (not (setq encl-pos
1445 (c-most-enclosing-brace paren-state
)))
1446 (eq (char-after encl-pos
) ?\
{))
1448 (when (looking-at c-typedef-key
) ; "typedef"
1450 (goto-char (match-end 0))
1451 (c-forward-syntactic-ws))
1452 ;; At a real declaration?
1453 (if (memq (c-forward-type t
) '(t known found
))
1455 (c-font-lock-declarators limit t is-typedef
)
1457 ;; False alarm. Return t to go on to the next check.
1458 (goto-char start-pos
)
1462 ;; It was a false alarm. Check if we're in a label (or other
1463 ;; construct with `:' except bitfield) instead.
1464 (goto-char start-pos
)
1465 (when (setq label-type
(c-forward-label t match-pos nil
))
1466 ;; Can't use `c-fontify-types-and-refs' here since we
1467 ;; use the label face at times.
1468 (cond ((eq label-type
'goto-target
)
1469 (c-put-font-lock-face (caar c-record-ref-identifiers
)
1470 (cdar c-record-ref-identifiers
)
1472 ((eq label-type
'qt-1kwd-colon
)
1473 (c-put-font-lock-face (caar c-record-ref-identifiers
)
1474 (cdar c-record-ref-identifiers
)
1475 'font-lock-keyword-face
))
1476 ((eq label-type
'qt-2kwds-colon
)
1479 (c-put-font-lock-face (car kwd
) (cdr kwd
)
1480 'font-lock-keyword-face
))
1481 c-record-ref-identifiers
)))
1482 (setq c-record-ref-identifiers nil
)
1483 ;; `c-forward-label' has probably added a `c-decl-end'
1484 ;; marker, so return t to `c-find-decl-spots' to signal
1490 (defun c-font-lock-enum-tail (limit)
1491 ;; Fontify an enum's identifiers when POINT is within the enum's brace
1494 ;; This function will be called from font-lock for a region bounded by POINT
1495 ;; and LIMIT, as though it were to identify a keyword for
1496 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
1497 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
1500 ;; Note that this function won't attempt to fontify beyond the end of the
1501 ;; current enum block, if any.
1502 (let* ((paren-state (c-parse-state))
1503 (encl-pos (c-most-enclosing-brace paren-state
))
1508 (eq (char-after encl-pos
) ?\
{)
1510 (goto-char encl-pos
)
1511 (c-backward-syntactic-ws)
1512 (c-simple-skip-symbol-backward)
1513 (or (looking-at c-brace-list-key
) ; "enum"
1514 (progn (c-backward-syntactic-ws)
1515 (c-simple-skip-symbol-backward)
1516 (looking-at c-brace-list-key
)))))
1517 (c-syntactic-skip-backward "^{," nil t
)
1518 (c-put-char-property (1- (point)) 'c-type
'c-decl-id-start
)
1520 (c-forward-syntactic-ws)
1521 (c-font-lock-declarators limit t nil
)))
1524 (defun c-font-lock-enclosing-decls (limit)
1525 ;; Fontify the declarators of (nested) declarations we're in the middle of.
1526 ;; This is mainly for when a jit-lock etc. chunk starts inside the brace
1527 ;; block of a struct/union/class, etc.
1529 ;; This function will be called from font-lock for a region bounded by POINT
1530 ;; and LIMIT, as though it were to identify a keyword for
1531 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
1532 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
1534 (let* ((paren-state (c-parse-state))
1536 decl-context bo-decl in-typedef type-type ps-elt
)
1538 ;; First, are we actually in a "local" declaration?
1539 (setq decl-context
(c-beginning-of-decl-1)
1541 in-typedef
(looking-at c-typedef-key
))
1542 (if in-typedef
(c-forward-token-2))
1543 (when (and (eq (car decl-context
) 'same
)
1545 ;; Are we genuinely at a type?
1546 (setq type-type
(c-forward-type t
))
1548 (or (not (eq type-type
'maybe
))
1549 (looking-at c-symbol-key
)))
1550 (c-font-lock-declarators limit t in-typedef
)))
1552 ;; Secondly, are we in any nested struct/union/class/etc. braces?
1554 (setq ps-elt
(car paren-state
)
1555 paren-state
(cdr paren-state
))
1556 (when (and (atom ps-elt
)
1557 (eq (char-after ps-elt
) ?\
{))
1559 (setq decl-context
(c-beginning-of-decl-1)
1560 in-typedef
(looking-at c-typedef-key
))
1561 (if in-typedef
(c-forward-token-2))
1562 (when (and c-opt-block-decls-with-vars-key
1563 (looking-at c-opt-block-decls-with-vars-key
))
1565 (when (c-safe (c-forward-sexp))
1566 (c-forward-syntactic-ws)
1567 (c-font-lock-declarators limit t in-typedef
)))))))
1569 (c-lang-defconst c-simple-decl-matchers
1570 "Simple font lock matchers for types and declarations. These are used
1571 on level 2 only and so aren't combined with `c-complex-decl-matchers'."
1573 t
`(;; Objective-C methods.
1574 ,@(when (c-major-mode-is 'objc-mode
)
1575 `((,(c-lang-const c-opt-method-key
)
1578 (let (;; The font-lock package in Emacs is known to clobber
1579 ;; `parse-sexp-lookup-properties' (when it exists).
1580 (parse-sexp-lookup-properties
1581 (cc-eval-when-compile
1582 (boundp 'parse-sexp-lookup-properties
))))
1584 (narrow-to-region (point-min) limit
)
1585 (c-font-lock-objc-method)))
1587 (goto-char (match-end 1))))))
1589 ;; Fontify all type names and the identifiers in the
1590 ;; declarations they might start. Use eval here since
1591 ;; `c-known-type-key' gets its value from
1592 ;; `*-font-lock-extra-types' on mode init.
1593 (eval .
(list ,(c-make-font-lock-search-function
1595 '(1 'font-lock-type-face t
)
1596 '((c-font-lock-declarators limit t nil
)
1598 (goto-char (match-end 1))
1599 (c-forward-syntactic-ws))
1600 (goto-char (match-end 1))))))
1602 ;; Fontify types preceded by `c-type-prefix-kwds' and the
1603 ;; identifiers in the declarations they might start.
1604 ,@(when (c-lang-const c-type-prefix-kwds
)
1605 (let* ((prefix-re (c-make-keywords-re nil
1606 (c-lang-const c-type-prefix-kwds
)))
1608 (regexp-opt-depth prefix-re
)
1609 (c-lang-const c-simple-ws-depth
))))
1610 `((,(c-make-font-lock-search-function
1611 (concat "\\<\\(" prefix-re
"\\)" ; 1
1612 (c-lang-const c-simple-ws
) "+"
1613 (concat "\\(" ; 2 + prefix-re + c-simple-ws
1614 (c-lang-const c-symbol-key
)
1617 'font-lock-type-face t
)
1618 `((c-font-lock-declarators limit t nil
)
1620 (goto-char (match-end ,type-match
))
1621 (c-forward-syntactic-ws))
1622 (goto-char (match-end ,type-match
))))))))
1624 ;; Fontify special declarations that lacks a type.
1625 ,@(when (c-lang-const c-typeless-decl-kwds
)
1626 `((,(c-make-font-lock-search-function
1628 (regexp-opt (c-lang-const c-typeless-decl-kwds
))
1630 '((c-font-lock-declarators limit t nil
)
1632 (goto-char (match-end 1))
1633 (c-forward-syntactic-ws))
1634 (goto-char (match-end 1)))))))
1636 ;; Fontify generic colon labels in languages that support them.
1637 ,@(when (c-lang-const c-recognize-colon-labels
)
1638 `(c-font-lock-labels))))
1640 (c-lang-defconst c-complex-decl-matchers
1641 "Complex font lock matchers for types and declarations. Used on level
1644 ;; Note: This code in this form dumps a number of functions into the
1645 ;; resulting constant, `c-matchers-3'. At run time, font lock will call
1646 ;; each of them as a "FUNCTION" (see Elisp page "Search-based
1647 ;; Fontification"). The font lock region is delimited by POINT and the
1648 ;; single parameter, LIMIT. Each of these functions returns NIL (thus
1649 ;; inhibiting spurious font-lock-keyword-face highlighting and another
1652 t
`(;; Initialize some things before the search functions below.
1653 c-font-lock-complex-decl-prepare
1655 ,@(if (c-major-mode-is 'objc-mode
)
1656 ;; Fontify method declarations in Objective-C, but first
1657 ;; we have to put the `c-decl-end' `c-type' property on
1658 ;; all the @-style directives that haven't been handled in
1659 ;; `c-basic-matchers-before'.
1660 `(,(c-make-font-lock-search-function
1661 (c-make-keywords-re t
1662 ;; Exclude "@class" since that directive ends with a
1663 ;; semicolon anyway.
1665 (append (c-lang-const c-protection-kwds
)
1666 (c-lang-const c-other-decl-kwds
)
1668 '((c-put-char-property (1- (match-end 1))
1669 'c-type
'c-decl-end
)))
1670 c-font-lock-objc-methods
))
1672 ;; Fontify all declarations, casts and normal labels.
1673 c-font-lock-declarations
1675 ;; Fontify declarators when POINT is within their declaration.
1676 c-font-lock-enclosing-decls
1678 ;; Fontify angle bracket arglists like templates in C++.
1679 ,@(when (c-lang-const c-recognize-
<>-arglists
)
1680 `(c-font-lock-<>-arglists
))
1682 ;; The first two rules here mostly find occurrences that
1683 ;; `c-font-lock-declarations' has found already, but not
1684 ;; declarations containing blocks in the type (see note below).
1685 ;; It's also useful to fontify these everywhere to show e.g. when
1686 ;; a type keyword is accidentally used as an identifier.
1688 ;; Fontify basic types.
1689 ,(let ((re (c-make-keywords-re nil
1690 (c-lang-const c-primitive-type-kwds
))))
1691 (if (c-major-mode-is 'pike-mode
)
1692 ;; No symbol is a keyword after "->" in Pike.
1693 `(,(concat "\\(\\=.?\\|[^>]\\|[^-]>\\)"
1694 "\\<\\(" re
"\\)\\>")
1695 2 font-lock-type-face
)
1696 `(,(concat "\\<\\(" re
"\\)\\>")
1697 1 'font-lock-type-face
)))
1699 ;; Fontify types preceded by `c-type-prefix-kwds' (e.g. "struct").
1700 ,@(when (c-lang-const c-type-prefix-kwds
)
1703 (c-fontify-types-and-refs
1704 ((c-promote-possible-types t
)
1705 ;; The font-lock package in Emacs is known to clobber
1706 ;; `parse-sexp-lookup-properties' (when it exists).
1707 (parse-sexp-lookup-properties
1708 (cc-eval-when-compile
1709 (boundp 'parse-sexp-lookup-properties
))))
1711 ;; Narrow to avoid going past the limit in
1712 ;; `c-forward-type'.
1713 (narrow-to-region (point) limit
)
1714 (while (re-search-forward
1716 (c-make-keywords-re nil
1717 (c-lang-const c-type-prefix-kwds
))
1720 (unless (c-skip-comments-and-strings limit
)
1721 (c-forward-syntactic-ws)
1722 ;; Handle prefix declaration specifiers.
1723 (when (or (looking-at c-prefix-spec-kwds-re
)
1724 (and (c-major-mode-is 'java-mode
)
1725 (looking-at "@[A-Za-z0-9]+")))
1726 (c-forward-keyword-clause 1))
1727 ,(if (c-major-mode-is 'c
++-mode
)
1728 `(when (and (c-forward-type)
1729 (eq (char-after) ?
=))
1730 ;; In C++ we additionally check for a "class
1731 ;; X = Y" construct which is used in
1732 ;; templates, to fontify Y as a type.
1734 (c-forward-syntactic-ws)
1739 ;; Fontify symbols after closing braces as declaration
1740 ;; identifiers under the assumption that they are part of
1741 ;; declarations like "class Foo { ... } foo;". It's too
1742 ;; expensive to check this accurately by skipping past the
1743 ;; brace block, so we use the heuristic that it's such a
1744 ;; declaration if the first identifier is on the same line as
1745 ;; the closing brace. `c-font-lock-declarations' will later
1746 ;; override it if it turns out to be an new declaration, but
1747 ;; it will be wrong if it's an expression (see the test
1749 ;; ,@(when (c-lang-const c-opt-block-decls-with-vars-key)
1750 ;; `((,(c-make-font-lock-search-function
1752 ;; (c-lang-const c-single-line-syntactic-ws)
1753 ;; "\\(" ; 1 + c-single-line-syntactic-ws-depth
1754 ;; (c-lang-const c-type-decl-prefix-key)
1756 ;; (c-lang-const c-symbol-key)
1758 ;; `((c-font-lock-declarators limit t nil) ; That `nil' says use `font-lock-variable-name-face';
1759 ;; ; `t' would mean `font-lock-function-name-face'.
1761 ;; (c-put-char-property (match-beginning 0) 'c-type
1762 ;; 'c-decl-id-start)
1763 ;; ; 'c-decl-type-start)
1764 ;; (goto-char (match-beginning
1765 ;; ,(1+ (c-lang-const
1766 ;; c-single-line-syntactic-ws-depth)))))
1767 ;; (goto-char (match-end 0)))))))
1769 ;; Fontify the type in C++ "new" expressions.
1770 ,@(when (c-major-mode-is 'c
++-mode
)
1771 ;; This pattern is a probably a "(MATCHER . ANCHORED-HIGHLIGHTER)"
1772 ;; (see Elisp page "Search-based Fontification").
1774 (c-font-lock-c++-new
))))
1777 (defun c-font-lock-labels (limit)
1778 ;; Fontify all statement labels from the point to LIMIT. Assumes
1779 ;; that strings and comments have been fontified already. Nil is
1782 ;; Note: This function is only used on decoration level 2; this is
1783 ;; taken care of directly by the gargantuan
1784 ;; `c-font-lock-declarations' on higher levels.
1786 ;; This function might do hidden buffer changes.
1788 (let (continue-pos id-start
1789 ;; The font-lock package in Emacs is known to clobber
1790 ;; `parse-sexp-lookup-properties' (when it exists).
1791 (parse-sexp-lookup-properties
1792 (cc-eval-when-compile
1793 (boundp 'parse-sexp-lookup-properties
))))
1795 (while (re-search-forward ":[^:]" limit t
)
1796 (setq continue-pos
(point))
1797 (goto-char (match-beginning 0))
1798 (unless (c-skip-comments-and-strings limit
)
1800 (c-backward-syntactic-ws)
1801 (and (setq id-start
(c-on-identifier))
1803 (not (get-text-property id-start
'face
))
1806 (goto-char id-start
)
1807 (c-backward-syntactic-ws)
1809 ;; Check for a char that precedes a statement.
1810 (memq (char-before) '(?\
} ?\
{ ?\
;))
1811 ;; Check for a preceding label. We exploit the font
1812 ;; locking made earlier by this function.
1813 (and (eq (char-before) ?
:)
1816 (c-backward-syntactic-ws)
1818 (eq (get-text-property (1- (point)) 'face
)
1820 ;; Check for a keyword that precedes a statement.
1821 (c-after-conditional)))
1825 (goto-char id-start
)
1826 (looking-at c-symbol-key
)
1827 (c-put-font-lock-face (match-beginning 0) (match-end 0)
1828 c-label-face-name
)))
1830 (goto-char continue-pos
))))
1833 (c-lang-defconst c-basic-matchers-after
1834 "Font lock matchers for various things that should be fontified after
1835 generic casts and declarations are fontified. Used on level 2 and
1838 t
`(,@(when (c-lang-const c-brace-id-list-kwds
)
1839 ;; Fontify the remaining identifiers inside an enum list when we start
1841 `(c-font-lock-enum-tail
1842 ;; Fontify the identifiers inside enum lists. (The enum type
1843 ;; name is handled by `c-simple-decl-matchers' or
1844 ;; `c-complex-decl-matchers' below.
1845 (,(c-make-font-lock-search-function
1848 (c-make-keywords-re nil
(c-lang-const c-brace-id-list-kwds
))
1850 ;; Disallow various common punctuation chars that can't come
1851 ;; before the '{' of the enum list, to avoid searching too far.
1854 '((c-font-lock-declarators limit t nil
)
1856 (goto-char (match-end 0))
1857 (c-put-char-property (1- (point)) 'c-type
1859 (c-forward-syntactic-ws))
1860 (goto-char (match-end 0)))))))
1862 ;; Fontify labels after goto etc.
1863 ,@(when (c-lang-const c-before-label-kwds
)
1864 `(;; (Got three different interpretation levels here,
1865 ;; which makes it a bit complicated: 1) The backquote
1866 ;; stuff is expanded when compiled or loaded, 2) the
1867 ;; eval form is evaluated at font-lock setup (to
1868 ;; substitute c-label-face-name correctly), and 3) the
1869 ;; resulting structure is interpreted during
1872 .
,(let* ((c-before-label-re
1873 (c-make-keywords-re nil
1874 (c-lang-const c-before-label-kwds
))))
1876 ,(concat "\\<\\(" c-before-label-re
"\\)\\>"
1878 "\\(" ; identifier-offset
1879 (c-lang-const c-symbol-key
)
1881 (list ,(+ (regexp-opt-depth c-before-label-re
) 2)
1882 c-label-face-name nil t
))))))
1884 ;; Fontify the clauses after various keywords.
1885 ,@(when (or (c-lang-const c-type-list-kwds
)
1886 (c-lang-const c-ref-list-kwds
)
1887 (c-lang-const c-colon-type-list-kwds
))
1888 `((,(c-make-font-lock-BO-decl-search-function
1890 (c-make-keywords-re nil
1891 (append (c-lang-const c-type-list-kwds
)
1892 (c-lang-const c-ref-list-kwds
)
1893 (c-lang-const c-colon-type-list-kwds
)))
1895 '((c-fontify-types-and-refs ((c-promote-possible-types t
))
1896 (c-forward-keyword-clause 1)
1897 (if (> (point) limit
) (goto-char limit
))))))))
1899 ,@(when (c-lang-const c-paren-type-kwds
)
1900 `((,(c-make-font-lock-search-function
1902 (c-make-keywords-re nil
1903 (c-lang-const c-paren-type-kwds
))
1905 '((c-fontify-types-and-refs ((c-promote-possible-types t
))
1906 (c-forward-keyword-clause 1)
1907 (if (> (point) limit
) (goto-char limit
))))))))
1909 ,@(when (c-major-mode-is 'java-mode
)
1910 `((eval .
(list "\\<\\(@[a-zA-Z0-9]+\\)\\>" 1 c-annotation-face
))))
1913 (c-lang-defconst c-matchers-1
1914 t
(c-lang-const c-cpp-matchers
))
1916 (c-lang-defconst c-matchers-2
1917 t
(append (c-lang-const c-matchers-1
)
1918 (c-lang-const c-basic-matchers-before
)
1919 (c-lang-const c-simple-decl-matchers
)
1920 (c-lang-const c-basic-matchers-after
)))
1922 (c-lang-defconst c-matchers-3
1923 t
(append (c-lang-const c-matchers-1
)
1924 (c-lang-const c-basic-matchers-before
)
1925 (c-lang-const c-complex-decl-matchers
)
1926 (c-lang-const c-basic-matchers-after
)))
1928 (defun c-compose-keywords-list (base-list)
1929 ;; Incorporate the font lock keyword lists according to
1930 ;; `c-doc-comment-style' on the given keyword list and return it.
1931 ;; This is used in the function bindings of the
1932 ;; `*-font-lock-keywords-*' symbols since we have to build the list
1933 ;; when font-lock is initialized.
1935 (unless (memq c-doc-face-name c-literal-faces
)
1936 (setq c-literal-faces
(cons c-doc-face-name c-literal-faces
)))
1938 (let* ((doc-keywords
1939 (if (consp (car-safe c-doc-comment-style
))
1940 (cdr-safe (or (assq c-buffer-is-cc-mode c-doc-comment-style
)
1941 (assq 'other c-doc-comment-style
)))
1942 c-doc-comment-style
))
1943 (list (nconc (apply 'nconc
1947 (concat (symbol-name doc-style
)
1948 "-font-lock-keywords"))))
1949 (cond ((fboundp sym
)
1952 (append (eval sym
) nil
)))))
1953 (if (listp doc-keywords
)
1955 (list doc-keywords
))))
1958 ;; Kludge: If `c-font-lock-complex-decl-prepare' is on the list we
1959 ;; move it first since the doc comment font lockers might add
1960 ;; `c-type' text properties, so they have to be cleared before that.
1961 (when (memq 'c-font-lock-complex-decl-prepare list
)
1962 (setq list
(cons 'c-font-lock-complex-decl-prepare
1963 (delq 'c-font-lock-complex-decl-prepare
1964 (append list nil
)))))
1968 (defun c-override-default-keywords (def-var)
1969 ;; This is used to override the value on a `*-font-lock-keywords'
1970 ;; variable only if it's nil or has the same value as one of the
1971 ;; `*-font-lock-keywords-*' variables. Older font-lock packages
1972 ;; define a default value for `*-font-lock-keywords' which we want
1973 ;; to override, but we should otoh avoid clobbering a user setting.
1974 ;; This heuristic for that isn't perfect, but I can't think of any
1976 (when (and (boundp def-var
)
1977 (memq (symbol-value def-var
)
1981 (let ((sym (intern (concat (symbol-name def-var
)
1983 (and (boundp sym
) (symbol-value sym
))))
1984 '("-1" "-2" "-3")))))
1985 ;; The overriding is done by unbinding the variable so that the normal
1986 ;; defvar will install its default value later on.
1987 (makunbound def-var
)))
1992 (c-override-default-keywords 'c-font-lock-keywords
)
1994 (defconst c-font-lock-keywords-1
(c-lang-const c-matchers-1 c
)
1995 "Minimal font locking for C mode.
1996 Fontifies only preprocessor directives (in addition to the syntactic
1997 fontification of strings and comments).")
1999 (defconst c-font-lock-keywords-2
(c-lang-const c-matchers-2 c
)
2000 "Fast normal font locking for C mode.
2001 In addition to `c-font-lock-keywords-1', this adds fontification of
2002 keywords, simple types, declarations that are easy to recognize, the
2003 user defined types on `c-font-lock-extra-types', and the doc comment
2004 styles specified by `c-doc-comment-style'.")
2006 (defconst c-font-lock-keywords-3
(c-lang-const c-matchers-3 c
)
2007 "Accurate normal font locking for C mode.
2008 Like `c-font-lock-keywords-2' but detects declarations in a more
2009 accurate way that works in most cases for arbitrary types without the
2010 need for `c-font-lock-extra-types'.")
2012 (defvar c-font-lock-keywords c-font-lock-keywords-3
2013 "Default expressions to highlight in C mode.")
2015 (defun c-font-lock-keywords-2 ()
2016 (c-compose-keywords-list c-font-lock-keywords-2
))
2017 (defun c-font-lock-keywords-3 ()
2018 (c-compose-keywords-list c-font-lock-keywords-3
))
2019 (defun c-font-lock-keywords ()
2020 (c-compose-keywords-list c-font-lock-keywords
))
2025 (defun c-font-lock-c++-new
(limit)
2026 ;; FIXME!!! Put in a comment about the context of this function's
2027 ;; invocation. I think it's called as an ANCHORED-MATCHER within an
2028 ;; ANCHORED-HIGHLIGHTER. (2007/2/10).
2030 ;; Assuming point is after a "new" word, check that it isn't inside
2031 ;; a string or comment, and if so try to fontify the type in the
2032 ;; allocation expression. Nil is always returned.
2034 ;; As usual, C++ takes the prize in coming up with a hard to parse
2037 ;; This function might do hidden buffer changes.
2039 (unless (c-skip-comments-and-strings limit
)
2042 ;; A "new" keyword is followed by one to three expressions, where
2043 ;; the type is the middle one, and the only required part.
2044 (let (expr1-pos expr2-pos
2045 ;; Enable recording of identifier ranges in `c-forward-type'
2046 ;; etc for later fontification. Not using
2047 ;; `c-fontify-types-and-refs' here since the ranges should
2048 ;; be fontified selectively only when an allocation
2049 ;; expression is successfully recognized.
2050 (c-record-type-identifiers t
)
2051 c-record-ref-identifiers
2052 ;; The font-lock package in Emacs is known to clobber
2053 ;; `parse-sexp-lookup-properties' (when it exists).
2054 (parse-sexp-lookup-properties
2055 (cc-eval-when-compile
2056 (boundp 'parse-sexp-lookup-properties
))))
2057 (c-forward-syntactic-ws)
2059 ;; The first placement arglist is always parenthesized, if it
2061 (when (eq (char-after) ?\
()
2062 (setq expr1-pos
(1+ (point)))
2065 (scan-error (throw 'false-alarm t
)))
2066 (c-forward-syntactic-ws))
2068 ;; The second expression is either a type followed by some "*" or
2069 ;; "[...]" or similar, or a parenthesized type followed by a full
2070 ;; identifierless declarator.
2071 (setq expr2-pos
(1+ (point)))
2072 (cond ((eq (char-after) ?\
())
2073 ((let ((c-promote-possible-types t
))
2075 (t (setq expr2-pos nil
)))
2080 ;; No second expression, so the first has to be a
2081 ;; parenthesized type.
2082 (goto-char expr1-pos
)
2083 (let ((c-promote-possible-types t
))
2086 ((eq (char-before expr2-pos
) ?\
()
2087 ;; Got two parenthesized expressions, so we have to look
2088 ;; closer at them to decide which is the type. No need to
2089 ;; handle `c-record-ref-identifiers' since all references
2090 ;; has already been handled by other fontification rules.
2091 (let (expr1-res expr2-res
)
2093 (goto-char expr1-pos
)
2094 (when (setq expr1-res
(c-forward-type))
2096 (cc-eval-when-compile
2097 (concat (c-lang-const c-symbol-start c
++)
2099 ;; There's something after the would-be type that
2100 ;; can't be there, so this is a placement arglist.
2101 (setq expr1-res nil
)))
2103 (goto-char expr2-pos
)
2104 (when (setq expr2-res
(c-forward-type))
2106 (cc-eval-when-compile
2107 (concat (c-lang-const c-symbol-start c
++)
2109 ;; There's something after the would-be type that can't
2110 ;; be there, so this is an initialization expression.
2111 (setq expr2-res nil
))
2112 (when (and (c-go-up-list-forward)
2113 (progn (c-forward-syntactic-ws)
2114 (eq (char-after) ?\
()))
2115 ;; If there's a third initialization expression
2116 ;; then the second one is the type, so demote the
2118 (setq expr1-res nil
)))
2120 ;; We fontify the most likely type, with a preference for
2121 ;; the first argument since a placement arglist is more
2122 ;; unusual than an initializer.
2123 (cond ((memq expr1-res
'(t known prefix
)))
2124 ((memq expr2-res
'(t known prefix
)))
2125 ((eq expr1-res
'found
)
2126 (let ((c-promote-possible-types t
))
2127 (goto-char expr1-pos
)
2129 ((eq expr2-res
'found
)
2130 (let ((c-promote-possible-types t
))
2131 (goto-char expr2-pos
)
2133 ((and (eq expr1-res
'maybe
) (not expr2-res
))
2134 (let ((c-promote-possible-types t
))
2135 (goto-char expr1-pos
)
2137 ((and (not expr1-res
) (eq expr2-res
'maybe
))
2138 (let ((c-promote-possible-types t
))
2139 (goto-char expr2-pos
)
2141 ;; If both type matches are 'maybe then we're
2142 ;; too uncertain to promote either of them.
2145 ;; Fontify the type that now is recorded in
2146 ;; `c-record-type-identifiers', if any.
2147 (c-fontify-recorded-types-and-refs)))))
2150 (c-override-default-keywords 'c
++-font-lock-keywords
)
2152 (defconst c
++-font-lock-keywords-1
(c-lang-const c-matchers-1 c
++)
2153 "Minimal font locking for C++ mode.
2154 Fontifies only preprocessor directives (in addition to the syntactic
2155 fontification of strings and comments).")
2157 (defconst c
++-font-lock-keywords-2
(c-lang-const c-matchers-2 c
++)
2158 "Fast normal font locking for C++ mode.
2159 In addition to `c++-font-lock-keywords-1', this adds fontification of
2160 keywords, simple types, declarations that are easy to recognize, the
2161 user defined types on `c++-font-lock-extra-types', and the doc comment
2162 styles specified by `c-doc-comment-style'.")
2164 (defconst c
++-font-lock-keywords-3
(c-lang-const c-matchers-3 c
++)
2165 "Accurate normal font locking for C++ mode.
2166 Like `c++-font-lock-keywords-2' but detects declarations in a more
2167 accurate way that works in most cases for arbitrary types without the
2168 need for `c++-font-lock-extra-types'.")
2170 (defvar c
++-font-lock-keywords c
++-font-lock-keywords-3
2171 "Default expressions to highlight in C++ mode.")
2173 (defun c++-font-lock-keywords-2
()
2174 (c-compose-keywords-list c
++-font-lock-keywords-2
))
2175 (defun c++-font-lock-keywords-3
()
2176 (c-compose-keywords-list c
++-font-lock-keywords-3
))
2177 (defun c++-font-lock-keywords
()
2178 (c-compose-keywords-list c
++-font-lock-keywords
))
2183 (defun c-font-lock-objc-method ()
2184 ;; Assuming the point is after the + or - that starts an Objective-C
2185 ;; method declaration, fontify it. This must be done before normal
2186 ;; casts, declarations and labels are fontified since they will get
2187 ;; false matches in these things.
2189 ;; This function might do hidden buffer changes.
2191 (c-fontify-types-and-refs
2193 (c-promote-possible-types t
))
2197 (c-forward-syntactic-ws)
2199 ;; An optional method type.
2200 (if (eq (char-after) ?\
()
2203 (c-forward-syntactic-ws)
2205 (prog1 (c-go-up-list-forward)
2206 (c-forward-syntactic-ws)))
2209 ;; The name. The first time it's the first part of
2210 ;; the function name, the rest of the time it's an
2212 (looking-at c-symbol-key
)
2214 (goto-char (match-end 0))
2215 (c-put-font-lock-face (match-beginning 0)
2218 'font-lock-function-name-face
2219 'font-lock-variable-name-face
))
2220 (c-forward-syntactic-ws)
2222 ;; Another optional part of the function name.
2223 (when (looking-at c-symbol-key
)
2224 (goto-char (match-end 0))
2225 (c-put-font-lock-face (match-beginning 0)
2227 'font-lock-function-name-face
)
2228 (c-forward-syntactic-ws))
2230 ;; There's another argument if a colon follows.
2231 (eq (char-after) ?
:)))
2235 (defun c-font-lock-objc-methods (limit)
2236 ;; Fontify method declarations in Objective-C. Nil is always
2239 ;; This function might do hidden buffer changes.
2241 (let (;; The font-lock package in Emacs is known to clobber
2242 ;; `parse-sexp-lookup-properties' (when it exists).
2243 (parse-sexp-lookup-properties
2244 (cc-eval-when-compile
2245 (boundp 'parse-sexp-lookup-properties
))))
2251 (lambda (match-pos inside-macro
)
2253 (c-font-lock-objc-method))))
2256 (c-override-default-keywords 'objc-font-lock-keywords
)
2258 (defconst objc-font-lock-keywords-1
(c-lang-const c-matchers-1 objc
)
2259 "Minimal font locking for Objective-C mode.
2260 Fontifies only compiler directives (in addition to the syntactic
2261 fontification of strings and comments).")
2263 (defconst objc-font-lock-keywords-2
(c-lang-const c-matchers-2 objc
)
2264 "Fast normal font locking for Objective-C mode.
2265 In addition to `objc-font-lock-keywords-1', this adds fontification of
2266 keywords, simple types, declarations that are easy to recognize, the
2267 user defined types on `objc-font-lock-extra-types', and the doc
2268 comment styles specified by `c-doc-comment-style'.")
2270 (defconst objc-font-lock-keywords-3
(c-lang-const c-matchers-3 objc
)
2271 "Accurate normal font locking for Objective-C mode.
2272 Like `objc-font-lock-keywords-2' but detects declarations in a more
2273 accurate way that works in most cases for arbitrary types without the
2274 need for `objc-font-lock-extra-types'.")
2276 (defvar objc-font-lock-keywords objc-font-lock-keywords-3
2277 "Default expressions to highlight in Objective-C mode.")
2279 (defun objc-font-lock-keywords-2 ()
2280 (c-compose-keywords-list objc-font-lock-keywords-2
))
2281 (defun objc-font-lock-keywords-3 ()
2282 (c-compose-keywords-list objc-font-lock-keywords-3
))
2283 (defun objc-font-lock-keywords ()
2284 (c-compose-keywords-list objc-font-lock-keywords
))
2286 ;; Kludge to override the default value that
2287 ;; `objc-font-lock-extra-types' might have gotten from the font-lock
2288 ;; package. The value replaced here isn't relevant now anyway since
2289 ;; those types are builtin and therefore listed directly in
2290 ;; `c-primitive-type-kwds'.
2291 (when (equal (sort (append objc-font-lock-extra-types nil
) 'string-lessp
)
2292 '("BOOL" "Class" "IMP" "SEL"))
2293 (setq objc-font-lock-extra-types
2294 (cc-eval-when-compile (list (concat "[" c-upper
"]\\sw*")))))
2299 (c-override-default-keywords 'java-font-lock-keywords
)
2301 (defconst java-font-lock-keywords-1
(c-lang-const c-matchers-1 java
)
2302 "Minimal font locking for Java mode.
2303 Fontifies nothing except the syntactic fontification of strings and
2306 (defconst java-font-lock-keywords-2
(c-lang-const c-matchers-2 java
)
2307 "Fast normal font locking for Java mode.
2308 In addition to `java-font-lock-keywords-1', this adds fontification of
2309 keywords, simple types, declarations that are easy to recognize, the
2310 user defined types on `java-font-lock-extra-types', and the doc
2311 comment styles specified by `c-doc-comment-style'.")
2313 (defconst java-font-lock-keywords-3
(c-lang-const c-matchers-3 java
)
2314 "Accurate normal font locking for Java mode.
2315 Like `java-font-lock-keywords-2' but detects declarations in a more
2316 accurate way that works in most cases for arbitrary types without the
2317 need for `java-font-lock-extra-types'.")
2319 (defvar java-font-lock-keywords java-font-lock-keywords-3
2320 "Default expressions to highlight in Java mode.")
2322 (defun java-font-lock-keywords-2 ()
2323 (c-compose-keywords-list java-font-lock-keywords-2
))
2324 (defun java-font-lock-keywords-3 ()
2325 (c-compose-keywords-list java-font-lock-keywords-3
))
2326 (defun java-font-lock-keywords ()
2327 (c-compose-keywords-list java-font-lock-keywords
))
2332 (c-override-default-keywords 'idl-font-lock-keywords
)
2334 (defconst idl-font-lock-keywords-1
(c-lang-const c-matchers-1 idl
)
2335 "Minimal font locking for CORBA IDL mode.
2336 Fontifies nothing except the syntactic fontification of strings and
2339 (defconst idl-font-lock-keywords-2
(c-lang-const c-matchers-2 idl
)
2340 "Fast normal font locking for CORBA IDL mode.
2341 In addition to `idl-font-lock-keywords-1', this adds fontification of
2342 keywords, simple types, declarations that are easy to recognize, the
2343 user defined types on `idl-font-lock-extra-types', and the doc comment
2344 styles specified by `c-doc-comment-style'.")
2346 (defconst idl-font-lock-keywords-3
(c-lang-const c-matchers-3 idl
)
2347 "Accurate normal font locking for CORBA IDL mode.
2348 Like `idl-font-lock-keywords-2' but detects declarations in a more
2349 accurate way that works in most cases for arbitrary types without the
2350 need for `idl-font-lock-extra-types'.")
2352 (defvar idl-font-lock-keywords idl-font-lock-keywords-3
2353 "Default expressions to highlight in CORBA IDL mode.")
2355 (defun idl-font-lock-keywords-2 ()
2356 (c-compose-keywords-list idl-font-lock-keywords-2
))
2357 (defun idl-font-lock-keywords-3 ()
2358 (c-compose-keywords-list idl-font-lock-keywords-3
))
2359 (defun idl-font-lock-keywords ()
2360 (c-compose-keywords-list idl-font-lock-keywords
))
2365 (c-override-default-keywords 'pike-font-lock-keywords
)
2367 (defconst pike-font-lock-keywords-1
(c-lang-const c-matchers-1 pike
)
2368 "Minimal font locking for Pike mode.
2369 Fontifies only preprocessor directives (in addition to the syntactic
2370 fontification of strings and comments).")
2372 (defconst pike-font-lock-keywords-2
(c-lang-const c-matchers-2 pike
)
2373 "Fast normal font locking for Pike mode.
2374 In addition to `pike-font-lock-keywords-1', this adds fontification of
2375 keywords, simple types, declarations that are easy to recognize, the
2376 user defined types on `pike-font-lock-extra-types', and the doc
2377 comment styles specified by `c-doc-comment-style'.")
2379 (defconst pike-font-lock-keywords-3
(c-lang-const c-matchers-3 pike
)
2380 "Accurate normal font locking for Pike mode.
2381 Like `pike-font-lock-keywords-2' but detects declarations in a more
2382 accurate way that works in most cases for arbitrary types without the
2383 need for `pike-font-lock-extra-types'.")
2385 (defvar pike-font-lock-keywords pike-font-lock-keywords-3
2386 "Default expressions to highlight in Pike mode.")
2388 (defun pike-font-lock-keywords-2 ()
2389 (c-compose-keywords-list pike-font-lock-keywords-2
))
2390 (defun pike-font-lock-keywords-3 ()
2391 (c-compose-keywords-list pike-font-lock-keywords-3
))
2392 (defun pike-font-lock-keywords ()
2393 (c-compose-keywords-list pike-font-lock-keywords
))
2398 (defun c-font-lock-doc-comments (prefix limit keywords
)
2399 ;; Fontify the comments between the point and LIMIT whose start
2400 ;; matches PREFIX with `c-doc-face-name'. Assumes comments have been
2401 ;; fontified with `font-lock-comment-face' already. nil is always
2404 ;; After the fontification of a matching comment, fontification
2405 ;; according to KEYWORDS is applied inside it. It's a list like
2406 ;; `font-lock-keywords' except that anchored matches and eval
2407 ;; clauses aren't supported and that some abbreviated forms can't be
2408 ;; used. The buffer is narrowed to the comment while KEYWORDS is
2409 ;; applied; leading comment starters are included but trailing
2410 ;; comment enders for block comment are not.
2412 ;; Note that faces added through KEYWORDS should never replace the
2413 ;; existing `c-doc-face-name' face since the existence of that face
2414 ;; is used as a flag in other code to skip comments.
2416 ;; This function might do hidden buffer changes.
2418 (let (comment-beg region-beg
)
2419 (if (eq (get-text-property (point) 'face
)
2420 'font-lock-comment-face
)
2421 ;; Handle the case when the fontified region starts inside a
2423 (let ((range (c-literal-limits)))
2424 (setq region-beg
(point))
2426 (goto-char (car range
)))
2427 (when (looking-at prefix
)
2428 (setq comment-beg
(point)))))
2433 ;; Search for the prefix until a match is found at the start
2435 (while (when (re-search-forward prefix limit t
)
2436 (setq comment-beg
(match-beginning 0))
2437 (or (not (c-got-face-at comment-beg
2439 (and (/= comment-beg
(point-min))
2440 (c-got-face-at (1- comment-beg
)
2442 (setq comment-beg nil
))
2443 (setq region-beg comment-beg
))
2445 (if (eq (elt (parse-partial-sexp comment-beg
(+ comment-beg
2)) 7) t
)
2446 ;; Collect a sequence of doc style line comments.
2448 (goto-char comment-beg
)
2450 (c-forward-single-comment)
2451 (skip-syntax-forward " ")
2453 (looking-at prefix
))))
2454 (goto-char comment-beg
)
2455 (c-forward-single-comment))
2456 (if (> (point) limit
) (goto-char limit
))
2457 (setq comment-beg nil
)
2459 (let ((region-end (point))
2460 (keylist keywords
) keyword matcher highlights
)
2461 (c-put-font-lock-face region-beg region-end c-doc-face-name
)
2463 ;; Narrow to the doc comment. Among other things, this
2464 ;; helps by making "^" match at the start of the comment.
2465 ;; Do not include a trailing block comment ender, though.
2466 (and (> region-end
(1+ region-beg
))
2467 (progn (goto-char region-end
)
2469 (looking-at "\\*/"))
2470 (setq region-end
(point)))
2471 (narrow-to-region region-beg region-end
)
2474 (setq keyword
(car keylist
)
2475 keylist
(cdr keylist
)
2476 matcher
(car keyword
))
2477 (goto-char region-beg
)
2478 (while (if (stringp matcher
)
2479 (re-search-forward matcher region-end t
)
2480 (funcall matcher region-end
))
2481 (setq highlights
(cdr keyword
))
2482 (if (consp (car highlights
))
2484 (font-lock-apply-highlight (car highlights
))
2485 (setq highlights
(cdr highlights
)))
2486 (font-lock-apply-highlight highlights
))))
2488 (goto-char region-end
)))))
2490 (put 'c-font-lock-doc-comments
'lisp-indent-function
2)
2492 (defun c-find-invalid-doc-markup (regexp limit
)
2493 ;; Used to fontify invalid markup in doc comments after the correct
2494 ;; ones have been fontified: Find the first occurrence of REGEXP
2495 ;; between the point and LIMIT that only is fontified with
2496 ;; `c-doc-face-name'. If a match is found then submatch 0 surrounds
2497 ;; the first char and t is returned, otherwise nil is returned.
2499 ;; This function might do hidden buffer changes.
2501 (while (if (re-search-forward regexp limit t
)
2502 (not (eq (get-text-property
2503 (setq start
(match-beginning 0)) 'face
)
2507 (store-match-data (list (copy-marker start
)
2508 (copy-marker (1+ start
))))
2511 ;; GtkDoc patterns contributed by Masatake YAMATO <jet@gyve.org>.
2513 (defconst gtkdoc-font-lock-doc-comments
2514 (let ((symbol "[a-zA-Z0-9_]+")
2516 `((,(concat header
"\\(" symbol
"\\):[ \t]*$")
2517 1 ,c-doc-markup-face-name prepend nil
)
2518 (,(concat symbol
"()")
2519 0 ,c-doc-markup-face-name prepend nil
)
2520 (,(concat header
"\\(" "@" symbol
"\\):")
2521 1 ,c-doc-markup-face-name prepend nil
)
2522 (,(concat "[#%@]" symbol
)
2523 0 ,c-doc-markup-face-name prepend nil
))
2526 (defconst gtkdoc-font-lock-doc-protection
2527 `(("< \\(public\\|private\\|protected\\) >"
2528 1 ,c-doc-markup-face-name prepend nil
)))
2530 (defconst gtkdoc-font-lock-keywords
2532 (c-font-lock-doc-comments "/\\*\\*$" limit
2533 gtkdoc-font-lock-doc-comments
)
2534 (c-font-lock-doc-comments "/\\*< " limit
2535 gtkdoc-font-lock-doc-protection
)
2540 (defconst javadoc-font-lock-doc-comments
2541 `(("{@[a-z]+[^}\n\r]*}" ; "{@foo ...}" markup.
2542 0 ,c-doc-markup-face-name prepend nil
)
2543 ("^\\(/\\*\\)?\\(\\s \\|\\*\\)*\\(@[a-z]+\\)" ; "@foo ..." markup.
2544 3 ,c-doc-markup-face-name prepend nil
)
2545 (,(concat "</?\\sw" ; HTML tags.
2547 (concat "\\sw\\|\\s \\|[=\n\r*.:]\\|"
2548 "\"[^\"]*\"\\|'[^']*'")
2550 0 ,c-doc-markup-face-name prepend nil
)
2551 ("&\\(\\sw\\|[.:]\\)+;" ; HTML entities.
2552 0 ,c-doc-markup-face-name prepend nil
)
2553 ;; Fontify remaining markup characters as invalid. Note
2554 ;; that the Javadoc spec is hazy about when "@" is
2555 ;; allowed in non-markup use.
2557 (c-find-invalid-doc-markup "[<>&]\\|{@" limit
))
2558 0 'font-lock-warning-face prepend nil
)))
2560 (defconst javadoc-font-lock-keywords
2562 (c-font-lock-doc-comments "/\\*\\*" limit
2563 javadoc-font-lock-doc-comments
)))))
2567 (defconst autodoc-decl-keywords
2568 ;; Adorned regexp matching the keywords that introduce declarations
2570 (cc-eval-when-compile
2571 (c-make-keywords-re t
'("@decl" "@elem" "@index" "@member") 'pike-mode
)))
2573 (defconst autodoc-decl-type-keywords
2574 ;; Adorned regexp matching the keywords that are followed by a type.
2575 (cc-eval-when-compile
2576 (c-make-keywords-re t
'("@elem" "@member") 'pike-mode
)))
2578 (defun autodoc-font-lock-line-markup (limit)
2579 ;; Fontify all line oriented keywords between the point and LIMIT.
2580 ;; Nil is always returned.
2582 ;; This function might do hidden buffer changes.
2584 (let ((line-re (concat "^\\(\\(/\\*!\\|\\s *\\("
2585 c-current-comment-prefix
2586 "\\)\\)\\s *\\)@[A-Za-z_-]+\\(\\s \\|$\\)"))
2587 (markup-faces (list c-doc-markup-face-name c-doc-face-name
)))
2589 (while (re-search-forward line-re limit t
)
2590 (goto-char (match-end 1))
2592 (if (looking-at autodoc-decl-keywords
)
2593 (let* ((kwd-pos (point))
2594 (start (match-end 1))
2598 (c-put-font-lock-face (point) pos markup-faces
)
2600 ;; Put a declaration end mark at the markup keyword and
2601 ;; remove the faces from the rest of the line so that it
2602 ;; gets refontified as a declaration later on by
2603 ;; `c-font-lock-declarations'.
2604 (c-put-char-property (1- pos
) 'c-type
'c-decl-end
)
2609 (and (eq (char-before) ?
@)
2611 (progn (forward-char)
2612 (skip-syntax-forward " ")
2613 (looking-at c-current-comment-prefix
))))
2614 (goto-char (match-end 0))
2615 (c-remove-font-lock-face pos
(1- end
))
2616 (c-put-font-lock-face (1- end
) end markup-faces
)
2619 ;; Include the final newline in the removed area. This
2620 ;; has no visual effect but it avoids some tricky special
2621 ;; cases in the testsuite wrt the differences in string
2622 ;; fontification in Emacs vs XEmacs.
2623 (c-remove-font-lock-face pos
(min (1+ (point)) (point-max)))
2625 ;; Must handle string literals explicitly inside the declaration.
2627 (while (re-search-forward
2628 "\"\\([^\\\"]\\|\\\\.\\)*\"\\|'\\([^\\']\\|\\\\.\\)*'"
2630 (c-put-font-lock-string-face (match-beginning 0)
2633 ;; Fontify types after keywords that always are followed
2636 (when (looking-at autodoc-decl-type-keywords
)
2637 (c-fontify-types-and-refs ((c-promote-possible-types t
))
2639 (c-forward-syntactic-ws)
2642 ;; Mark each whole line as markup, as long as the logical line
2645 (c-put-font-lock-face (point)
2646 (progn (end-of-line) (point))
2648 (and (eq (char-before) ?
@)
2650 (progn (forward-char)
2651 (skip-syntax-forward " ")
2652 (looking-at c-current-comment-prefix
))))
2653 (goto-char (match-end 0))))))
2657 (defconst autodoc-font-lock-doc-comments
2658 `(("@\\(\\w+{\\|\\[\\([^\]@\n\r]\\|@@\\)*\\]\\|[@}]\\|$\\)"
2660 0 ,c-doc-markup-face-name prepend nil
)
2661 (autodoc-font-lock-line-markup)
2662 ;; Fontify remaining markup characters as invalid.
2664 (c-find-invalid-doc-markup "@" limit
))
2665 0 'font-lock-warning-face prepend nil
)
2668 (defun autodoc-font-lock-keywords ()
2669 ;; Note that we depend on that `c-current-comment-prefix' has got
2670 ;; its proper value here.
2672 ;; This function might do hidden buffer changes.
2674 ;; The `c-type' text property with `c-decl-end' is used to mark the
2675 ;; end of the `autodoc-decl-keywords' occurrences to fontify the
2676 ;; following declarations.
2677 (setq c-type-decl-end-used t
)
2680 (c-font-lock-doc-comments "/[*/]!" limit
2681 autodoc-font-lock-doc-comments
)))))
2684 ;; 2006-07-10: awk-font-lock-keywords has been moved back to cc-awk.el.
2685 (cc-provide 'cc-fonts
)
2687 ;;; cc-fonts.el ends here