1 ;;; cc-fonts.el --- font lock support for CC Mode
3 ;; Copyright (C) 2002, 2003, 2005 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 ;; Version: See cc-mode.el
10 ;; Keywords: c languages oop
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 2, or (at your option)
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 this program; see the file COPYING. If not, write to
26 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
31 ;; Some comments on the use of faces:
33 ;; o `c-label-face-name' is either `font-lock-constant-face' (in Emacs
34 ;; 20 and later), or `font-lock-reference-face'.
36 ;; o `c-constant-face-name', `c-reference-face-name' and
37 ;; `c-doc-markup-face-name' are essentially set up like
38 ;; `c-label-face-name'.
40 ;; o `c-preprocessor-face-name' is `font-lock-preprocessor-face' in
41 ;; XEmacs and - in lack of a closer equivalent -
42 ;; `font-lock-builtin-face' or `font-lock-reference-face' in Emacs.
44 ;; o `c-doc-face-name' is `font-lock-doc-string-face' in XEmacs,
45 ;; `font-lock-doc-face' in Emacs 21 and later, or
46 ;; `font-lock-comment-face' in older Emacs (that since source
47 ;; documentation are actually comments in these languages, as opposed
50 ;; o `c-invalid-face-name' is `font-lock-warning-face' in Emacs. In
51 ;; older XEmacs there's no corresponding standard face, so there
52 ;; it's mapped to a special `c-invalid-face'.
54 ;; TBD: We should probably provide real faces for the above uses and
55 ;; instead initialize them from the standard faces.
59 ;; The faces that already have been put onto the text is tested in
60 ;; various places to direct further fontifications. For this to work,
61 ;; the following assumptions regarding the faces must hold (apart from
62 ;; the dependencies on the font locking order):
64 ;; o `font-lock-comment-face' and the face in `c-doc-face-name' is
65 ;; not used in anything but comments.
66 ;; o If any face (e.g. `c-doc-markup-face-name') but those above is
67 ;; used in comments, it doesn't replace them.
68 ;; o `font-lock-string-face' is not used in anything but string
69 ;; literals (single or double quoted).
70 ;; o `font-lock-keyword-face' and the face in `c-label-face-name' are
71 ;; never overlaid with other faces.
75 (if (and (boundp 'byte-compile-dest-file
)
76 (stringp byte-compile-dest-file
))
77 (cons (file-name-directory byte-compile-dest-file
) load-path
)
79 (load "cc-bytecomp" nil t
)))
82 (cc-require-when-compile 'cc-langs
)
84 (cc-require 'cc-engine
)
85 (cc-require-when-compile 'cc-awk
) ; Change from cc-require, 2003/6/18 to
86 ;; prevent cc-awk being loaded when it's not needed. There is now a (require
87 ;; 'cc-awk) in (defun awk-mode ..).
89 ;; Avoid repeated loading through the eval-after-load directive in
93 (cc-external-require 'font-lock
)
95 (cc-bytecomp-defvar parse-sexp-lookup-properties
) ; Emacs only.
97 ;; Need to declare these local symbols during compilation since
98 ;; they're referenced from lambdas in `byte-compile' calls that are
99 ;; executed at compile time. They don't need to have the proper
100 ;; definitions, though, since the generated functions aren't called
101 ;; during compilation.
102 (cc-bytecomp-defvar c-preprocessor-face-name
)
103 (cc-bytecomp-defvar c-reference-face-name
)
104 (cc-bytecomp-defun c-fontify-recorded-types-and-refs)
105 (cc-bytecomp-defun c-font-lock-declarators)
106 (cc-bytecomp-defun c-font-lock-objc-iip-decl)
107 (cc-bytecomp-defun c-font-lock-objc-method)
108 (cc-bytecomp-defun c-font-lock-invalid-string)
110 ;; Emacs 19 doesn't have `defface'. This "replacement" leaves a lot
111 ;; to be wished for but at least it avoids any errors.
112 (cc-eval-when-compile
113 (or (fboundp 'defface
)
114 (cc-bytecomp-defmacro defface
(face spec doc
&rest args
)
115 `(make-face ',face
))))
118 ;; Note that font-lock in XEmacs doesn't expand face names as
119 ;; variables, so we have to use the (eval . FORM) in the font lock
120 ;; matchers wherever we use these alias variables.
122 (defconst c-preprocessor-face-name
123 (cond ((c-face-name-p 'font-lock-preprocessor-face
)
124 ;; XEmacs has a font-lock-preprocessor-face.
125 'font-lock-preprocessor-face
)
126 ((c-face-name-p 'font-lock-builtin-face
)
127 ;; In Emacs 20 and later font-lock-builtin-face has
128 ;; traditionally been used for preprocessor directives.
129 'font-lock-builtin-face
)
131 'font-lock-reference-face
)))
133 (cc-bytecomp-defvar font-lock-constant-face
)
135 (defconst c-label-face-name
136 (cond ((c-face-name-p 'font-lock-label-face
)
137 ;; If it happens to occur in the future. (Well, the more
138 ;; pragmatic reason is to get unique faces for the test
140 'font-lock-label-face
)
141 ((and (c-face-name-p 'font-lock-constant-face
)
142 (eq font-lock-constant-face
'font-lock-constant-face
))
143 ;; Test both if font-lock-constant-face exists and that it's
144 ;; not an alias for something else. This is important since
145 ;; we compare already set faces in various places.
146 'font-lock-constant-face
)
148 'font-lock-reference-face
)))
150 (defconst c-constant-face-name
151 (if (and (c-face-name-p 'font-lock-constant-face
)
152 (eq font-lock-constant-face
'font-lock-constant-face
))
153 ;; This doesn't exist in XEmacs <= 20 and some earlier versions
155 'font-lock-constant-face
158 (defconst c-reference-face-name
159 (if (and (c-face-name-p 'font-lock-reference-face
)
160 (eq font-lock-reference-face
'font-lock-reference-face
))
161 ;; This is considered obsolete in Emacs 20 and later, but it
162 ;; still maps well to this use. (Another reason to do this is
163 ;; to get unique faces for the test suite.)
164 'font-lock-reference-face
167 ;; This should not mapped to a face that also is used to fontify things
168 ;; that aren't comments or string literals.
169 (defconst c-doc-face-name
170 (cond ((c-face-name-p 'font-lock-doc-string-face
)
172 'font-lock-doc-string-face
)
173 ((c-face-name-p 'font-lock-doc-face
)
174 ;; Emacs 21 and later.
177 'font-lock-comment-face
)))
179 (defconst c-doc-markup-face-name
180 (if (c-face-name-p 'font-lock-doc-markup-face
)
181 ;; If it happens to occur in the future. (Well, the more
182 ;; pragmatic reason is to get unique faces for the test
184 'font-lock-doc-markup-face
187 (defconst c-invalid-face-name
188 (if (c-face-name-p 'font-lock-warning-face
)
189 ;; Emacs >= 20 and XEmacs >= 21 has a font-lock-warning-face.
190 'font-lock-warning-face
191 ;; Otherwise we provide a face.
194 (unless (c-face-name-p c-invalid-face-name
)
195 (defconst c-invalid-face
'c-invalid-face
) ; Necessary in Emacs 19.
196 ;; This face should be called `c-invalid' for consistency with the
197 ;; rest of emacs, but as it's only used in very old versions of Emacs,
198 ;; we leave it unchanged (the face-alias mechanism doesn't exist in
199 ;; those old versions).
200 (defface c-invalid-face
201 '((((class color
) (background light
)) (:foreground
"red1"))
202 (((class color
)) (:foreground
"hotpink"))
203 (t (:inverse-video t
)))
204 "Face used to highlight invalid syntax."
207 ;; To make hard spaces visible an inverted version of
208 ;; `c-invalid-face-name' is used. Since font-lock in Emacs expands
209 ;; all face names in `font-lock-keywords' as variables we need to have
210 ;; a variable for it.
211 (defconst c-nonbreakable-space-face
'c-nonbreakable-space
)
213 (cc-bytecomp-defun face-inverse-video-p) ; Only in Emacs.
214 (cc-bytecomp-defun face-property-instance) ; Only in XEmacs.
216 (defun c-make-inverse-face (oldface newface
)
217 ;; Emacs and XEmacs have completely different face manipulation
220 ;; This function does not do any hidden buffer changes
221 (copy-face oldface newface
)
222 (cond ((fboundp 'face-inverse-video-p
)
223 ;; Emacs 20 and later. This only looks at the inverse flag
224 ;; in the current frame. Other display configurations might
225 ;; be different, but it can only show if the same Emacs has
226 ;; frames on e.g. a color and a monochrome display
228 (unless (face-inverse-video-p oldface
)
229 (invert-face newface
)))
230 ((fboundp 'face-property-instance
)
231 ;; XEmacs. Same pitfall here.
232 (unless (face-property-instance oldface
'reverse
)
233 (invert-face newface
)))
235 ;; Emacs 19 has no inverse flag at all. Just inverse the
236 ;; face and hope it wasn't inversed already.
237 (invert-face newface
))))
240 ;; We need the following functions during compilation since they're
241 ;; called when the `c-lang-defconst' initializers are evaluated.
242 ;; Define them at runtime too for the sake of derived modes.
244 (defmacro c-put-font-lock-face
(from to face
)
245 ;; Put a face on a region (overriding any existing face) in the way
246 ;; font-lock would do it. In XEmacs that means putting an
247 ;; additional font-lock property, or else the font-lock package
248 ;; won't recognize it as fontified and might override it
250 (if (fboundp 'font-lock-set-face
)
251 ;; Note: This function has no docstring in XEmacs so it might be
252 ;; considered internal.
253 `(font-lock-set-face ,from
,to
,face
)
254 `(put-text-property ,from
,to
'face
,face
)))
256 (defmacro c-remove-font-lock-face
(from to
)
257 ;; This is the inverse of `c-put-font-lock-face'.
258 (if (fboundp 'font-lock-remove-face
)
259 `(font-lock-remove-face ,from
,to
)
260 `(remove-text-properties ,from
,to
'(face nil
))))
262 (defmacro c-put-font-lock-string-face
(from to
)
263 ;; Put `font-lock-string-face' on a string. The surrounding
264 ;; quotes are included in Emacs but not in XEmacs. The passed
265 ;; region should include them.
266 (if (featurep 'xemacs
)
267 `(c-put-font-lock-face (1+ ,from
) (1- ,to
) 'font-lock-string-face
)
268 `(c-put-font-lock-face ,from
,to
'font-lock-string-face
)))
270 (defmacro c-fontify-types-and-refs
(varlist &rest body
)
271 ;; Like `let', but additionally activates `c-record-type-identifiers'
272 ;; and `c-record-ref-identifiers', and fontifies the recorded ranges
273 ;; accordingly on exit.
274 `(let ((c-record-type-identifiers t
)
275 c-record-ref-identifiers
277 (prog1 (progn ,@body
)
278 (c-fontify-recorded-types-and-refs))))
279 (put 'c-fontify-types-and-refs
'lisp-indent-function
1)
280 (eval-after-load "edebug" '(def-edebug-spec c-fontify-types-and-refs let
*))
282 (defun c-skip-comments-and-strings (limit)
283 ;; If the point is within a region fontified as a comment or
284 ;; string literal skip to the end of it or to LIMIT, whichever
285 ;; comes first, and return t. Otherwise return nil. The match
286 ;; data is not clobbered.
287 (when (c-got-face-at (point) c-literal-faces
)
289 (goto-char (next-single-property-change
290 (point) 'face nil limit
))
291 (and (< (point) limit
)
292 (c-got-face-at (point) c-literal-faces
))))
295 (defun c-make-font-lock-search-function (regexp &rest highlights
)
296 ;; This function makes a byte compiled function that works much like
297 ;; a matcher element in `font-lock-keywords'. It cuts out a little
298 ;; bit of the overhead compared to a real matcher. The main reason
299 ;; is however to pass the real search limit to the anchored
300 ;; matcher(s), since most (if not all) font-lock implementations
301 ;; arbitrarily limits anchored matchers to the same line, and also
302 ;; to insulate against various other irritating differences between
303 ;; the different (X)Emacs font-lock packages.
305 ;; REGEXP is the matcher, which must be a regexp. Only matches
306 ;; where the beginning is outside any comment or string literal are
309 ;; HIGHLIGHTS is a list of highlight specs, just like in
310 ;; `font-lock-keywords', with these limitations: The face is always
311 ;; overridden (no big disadvantage, since hits in comments etc are
312 ;; filtered anyway), there is no "laxmatch", and an anchored matcher
313 ;; is always a form which must do all the fontification directly.
314 ;; `limit' is a variable bound to the real limit in the context of
315 ;; the anchored matcher forms.
317 ;; This function does not do any hidden buffer changes, but the
318 ;; generated functions will. They are however used in places
319 ;; covered by the font-lock context.
321 ;; Note: Replace `byte-compile' with `eval' to debug the generated
325 (let (-match-end-pos-
326 ;; The font-lock package in Emacs is known to clobber
327 ;; `parse-sexp-lookup-properties' (when it exists).
328 (parse-sexp-lookup-properties
329 (cc-eval-when-compile
330 (boundp 'parse-sexp-lookup-properties
))))
331 (while (re-search-forward ,regexp limit t
)
332 (setq -match-end-pos-
(point))
334 (goto-char (match-beginning 0))
335 (c-skip-comments-and-strings limit
))
336 (goto-char -match-end-pos-
)
339 (if (integerp (car highlight
))
341 (unless (nth 2 highlight
)
343 "The override flag must currently be set in %s"
345 (when (nth 3 highlight
)
347 "The laxmatch flag may currently not be set in %s"
350 (c-put-font-lock-face
351 (match-beginning ,(car highlight
))
352 (match-end ,(car highlight
))
353 ,(elt highlight
1))))
354 (when (nth 3 highlight
)
355 (error "Match highlights currently not supported in %s"
359 (save-match-data ,(car highlight
))
360 ,(nth 2 highlight
))))
364 (defun c-fontify-recorded-types-and-refs ()
365 ;; Converts the ranges recorded on `c-record-type-identifiers' and
366 ;; `c-record-ref-identifiers' to fontification.
368 (while (consp c-record-type-identifiers
)
369 (setq elem
(car c-record-type-identifiers
)
370 c-record-type-identifiers
(cdr c-record-type-identifiers
))
371 (c-put-font-lock-face (car elem
) (cdr elem
)
372 'font-lock-type-face
))
373 (while c-record-ref-identifiers
374 (setq elem
(car c-record-ref-identifiers
)
375 c-record-ref-identifiers
(cdr c-record-ref-identifiers
))
376 ;; Note that the reference face is a variable that is
377 ;; dereferenced, since it's an alias in Emacs.
378 (c-put-font-lock-face (car elem
) (cdr elem
)
379 c-reference-face-name
))))
381 (c-lang-defconst c-cpp-matchers
382 "Font lock matchers for preprocessor directives and purely lexical
383 stuff. Used on level 1 and higher."
385 ;; Note: `c-font-lock-declarations' assumes that no matcher here
386 ;; sets `font-lock-type-face' in languages where
387 ;; `c-recognize-<>-arglists' is set.
389 t
`(,@(when (c-lang-const c-opt-cpp-prefix
)
390 (let* ((noncontinued-line-end "\\(\\=\\|\\(\\=\\|[^\\]\\)[\n\r]\\)")
391 (ncle-depth (c-regexp-opt-depth noncontinued-line-end
))
392 (sws-depth (c-lang-const c-syntactic-ws-depth
)))
393 `(;; The stuff after #error and #warning is a message, so
394 ;; fontify it as a string.
395 (,(concat noncontinued-line-end
396 (c-lang-const c-opt-cpp-prefix
)
397 "\\(error\\|warning\\)\\>\\s *\\(.*\\)$")
398 ,(+ ncle-depth
2) font-lock-string-face
)
400 ;; Fontify filenames in #include <...> as strings.
401 (,(concat noncontinued-line-end
402 (c-lang-const c-opt-cpp-prefix
)
403 "\\(import\\|include\\)\\>"
404 (c-lang-const c-syntactic-ws
)
405 "\\(<[^>\n\r]*>?\\)")
406 (,(+ ncle-depth sws-depth
2)
407 font-lock-string-face
)
409 ;; Use an anchored matcher to put paren syntax on the brackets.
413 (match-beginning ,(+ ncle-depth sws-depth
2)))
415 (1- (match-end ,(+ ncle-depth sws-depth
2)))))
416 (if (eq (char-after end-pos
) ?
>)
418 (c-mark-<-as-paren beg-pos
)
419 (c-mark->-as-paren end-pos
))
420 (c-clear-char-property beg-pos
'syntax-table
)))
424 (,(c-make-font-lock-search-function
426 noncontinued-line-end
427 (c-lang-const c-opt-cpp-prefix
)
429 (c-lang-const c-syntactic-ws
)
430 "\\(" (c-lang-const c-symbol-key
) "\\)" ; 1 + ncle + sws
431 (concat "\\(" ; 2 + ncle + sws + c-sym-key
432 ;; Macro with arguments - a "function".
433 "\\(\(\\)" ; 3 + ncle + sws + c-sym-key
435 ;; Macro without arguments - a "variable".
438 `((if (match-beginning ,(+ 3 ncle-depth sws-depth
439 (c-lang-const c-symbol-key-depth
)))
440 ;; "Function". Fontify the name and the arguments.
442 (c-put-font-lock-face
443 (match-beginning ,(+ 1 ncle-depth sws-depth
))
444 (match-end ,(+ 1 ncle-depth sws-depth
))
445 'font-lock-function-name-face
)
446 (goto-char (match-end
447 ,(+ 3 ncle-depth sws-depth
448 (c-lang-const c-symbol-key-depth
))))
450 (narrow-to-region (point-min) limit
)
453 (c-forward-syntactic-ws)
454 (looking-at c-symbol-key
))
456 (c-put-font-lock-face
457 (match-beginning 0) (match-end 0)
458 'font-lock-variable-name-face
)
459 (goto-char (match-end 0))
460 (c-forward-syntactic-ws)
461 (eq (char-after) ?
,)))
465 (c-put-font-lock-face
466 (match-beginning ,(+ 1 ncle-depth sws-depth
))
467 (match-end ,(+ 1 ncle-depth sws-depth
))
468 'font-lock-variable-name-face
)))))
470 ;; Fontify cpp function names in preprocessor
471 ;; expressions in #if and #elif.
472 ,(when (c-lang-const c-cpp-defined-fns
)
473 `(,(c-make-font-lock-search-function
474 (concat noncontinued-line-end
475 (c-lang-const c-opt-cpp-prefix
)
476 "\\(if\\|elif\\)\\>" ; 1 + ncle-depth
477 ;; Match the whole logical line to look
478 ;; for the functions in.
479 "\\(\\\\\\(.\\|[\n\r]\\)\\|[^\n\r]\\)*")
480 `((let ((limit (match-end 0)))
481 (while (re-search-forward
484 (c-lang-const c-cpp-defined-fns
)
489 (c-put-font-lock-face (match-beginning 1)
491 c-preprocessor-face-name
)))
492 (goto-char (match-end ,(1+ ncle-depth
)))))))
494 ;; Fontify the directive names.
495 (,(c-make-font-lock-search-function
496 (concat noncontinued-line-end
498 (c-lang-const c-opt-cpp-prefix
)
499 "[" (c-lang-const c-symbol-chars
) "]+"
501 `(,(1+ ncle-depth
) c-preprocessor-face-name t
)))
503 ;; fontify the n in ifndef
504 (,(concat noncontinued-line-end
505 (c-lang-const c-opt-cpp-prefix
)
507 ,(+ ncle-depth
1) font-lock-negation-char-face prepend
)
510 ,@(when (c-major-mode-is 'pike-mode
)
511 `((eval .
(list "\\`#![^\n\r]*"
512 0 c-preprocessor-face-name
))))
514 ;; Make hard spaces visible through an inverted `c-invalid-face-name'.
518 (unless (c-face-name-p c-nonbreakable-space-face
)
519 (c-make-inverse-face c-invalid-face-name
520 c-nonbreakable-space-face
))
521 'c-nonbreakable-space-face
)))
524 (defun c-font-lock-invalid-string ()
525 ;; Assuming the point is after the opening character of a string,
526 ;; fontify that char with `c-invalid-face-name' if the string
527 ;; decidedly isn't terminated properly.
528 (let ((start (1- (point))))
530 (and (nth 3 (parse-partial-sexp start
(c-point 'eol
)))
531 (if (c-major-mode-is '(c-mode c
++-mode objc-mode pike-mode
))
532 ;; There's no \ before the newline.
533 (not (eq (char-before (point)) ?
\\))
534 ;; Quoted newlines aren't supported.
536 (if (c-major-mode-is 'pike-mode
)
537 ;; There's no # before the string, so newlines
539 (not (eq (char-before start
) ?
#))
541 (c-put-font-lock-face start
(1+ start
) c-invalid-face-name
)))))
543 (c-lang-defconst c-basic-matchers-before
544 "Font lock matchers for basic keywords, labels, references and various
545 other easily recognizable things that should be fontified before generic
546 casts and declarations are fontified. Used on level 2 and higher."
548 ;; Note: `c-font-lock-declarations' assumes that no matcher here
549 ;; sets `font-lock-type-face' in languages where
550 ;; `c-recognize-<>-arglists' is set.
552 t
`(;; Put a warning face on the opener of unclosed strings that
553 ;; can't span lines. Later font
554 ;; lock packages have a `font-lock-syntactic-face-function' for
555 ;; this, but it doesn't give the control we want since any
556 ;; fontification done inside the function will be
557 ;; unconditionally overridden.
558 ,(c-make-font-lock-search-function
559 ;; Match a char before the string starter to make
560 ;; `c-skip-comments-and-strings' work correctly.
561 (concat ".\\(" c-string-limit-regexp
"\\)")
562 '((c-font-lock-invalid-string)))
564 ;; Fontify keyword constants.
565 ,@(when (c-lang-const c-constant-kwds
)
566 (let ((re (c-make-keywords-re nil
(c-lang-const c-constant-kwds
))))
567 (if (c-major-mode-is 'pike-mode
)
568 ;; No symbol is a keyword after "->" in Pike.
569 `((eval .
(list ,(concat "\\(\\=\\|\\(\\=\\|[^-]\\)[^>]\\)"
570 "\\<\\(" re
"\\)\\>")
571 3 c-constant-face-name
)))
572 `((eval .
(list ,(concat "\\<\\(" re
"\\)\\>")
573 1 c-constant-face-name
))))))
575 ;; Fontify all keywords except the primitive types.
576 ,(if (c-major-mode-is 'pike-mode
)
577 ;; No symbol is a keyword after "->" in Pike.
578 `(,(concat "\\(\\=\\|\\(\\=\\|[^-]\\)[^>]\\)"
579 "\\<" (c-lang-const c-regular-keywords-regexp
))
580 3 font-lock-keyword-face
)
581 `(,(concat "\\<" (c-lang-const c-regular-keywords-regexp
))
582 1 font-lock-keyword-face
))
584 ;; Fontify leading identifiers in fully qualified names like
585 ;; "foo::bar" in languages that supports such things.
586 ,@(when (c-lang-const c-opt-identifier-concat-key
)
587 (if (c-major-mode-is 'java-mode
)
588 ;; Java needs special treatment since "." is used both to
589 ;; qualify names and in normal indexing. Here we look for
590 ;; capital characters at the beginning of an identifier to
591 ;; recognize the class. "*" is also recognized to cover
592 ;; wildcard import declarations. All preceding dot separated
593 ;; identifiers are taken as package names and therefore
594 ;; fontified as references.
595 `(,(c-make-font-lock-search-function
596 ;; Search for class identifiers preceded by ".". The
597 ;; anchored matcher takes it from there.
598 (concat (c-lang-const c-opt-identifier-concat-key
)
601 "[" c-upper
"][" (c-lang-const c-symbol-chars
) "]*"
606 (goto-char (1+ (match-beginning 0)))
607 (while (and (eq (char-before) ?.
)
610 (c-backward-syntactic-ws)
611 (setq id-end
(point))
612 (< (skip-chars-backward
613 ,(c-lang-const c-symbol-chars
)) 0))
614 (not (get-text-property (point) 'face
)))
615 (c-put-font-lock-face (point) id-end c-reference-face-name
)
616 (c-backward-syntactic-ws)))
618 (goto-char (match-end 0)))))
621 ;; Must use a function here since we match longer than we
622 ;; want to move before doing a new search. This is not
623 ;; necessary for XEmacs >= 20 since it restarts the search
624 ;; from the end of the first highlighted submatch (something
625 ;; that causes problems in other places).
627 (while (re-search-forward
628 ,(concat "\\(\\<" ; 1
629 "\\(" (c-lang-const c-symbol-key
) "\\)" ; 2
631 (c-lang-const c-opt-identifier-concat-key
)
635 (c-lang-const c-opt-after-id-concat-key
)
639 (goto-char (match-beginning 0))
640 (c-skip-comments-and-strings limit
))
641 (or (get-text-property (match-beginning 2) 'face
)
642 (c-put-font-lock-face (match-beginning 2)
644 c-reference-face-name
))
645 (goto-char (match-end 1))))))))))
647 ;; Fontify the special declarations in Objective-C.
648 ,@(when (c-major-mode-is 'objc-mode
)
649 `(;; Fontify class names in the beginning of message expressions.
650 ,(c-make-font-lock-search-function
652 '((c-fontify-types-and-refs ()
653 (c-forward-syntactic-ws limit
)
654 (let ((start (point)))
655 ;; In this case we accept both primitive and known types.
656 (when (eq (c-forward-type) 'known
)
658 (let ((c-promote-possible-types t
))
660 (if (> (point) limit
) (goto-char limit
)))))
662 ;; The @interface/@implementation/@protocol directives.
665 '("@interface" "@implementation" "@protocol")
670 (let (;; The font-lock package in Emacs is known to clobber
671 ;; `parse-sexp-lookup-properties' (when it exists).
672 (parse-sexp-lookup-properties
673 (cc-eval-when-compile
674 (boundp 'parse-sexp-lookup-properties
))))
676 (narrow-to-region (point-min) limit
)
677 (c-font-lock-objc-iip-decl)))
680 ("\\(!\\)[^=]" 1 font-lock-negation-char-face
)
683 (defun c-font-lock-complex-decl-prepare (limit)
684 ;; Called before any of the matchers in `c-complex-decl-matchers'.
685 ;; Nil is always returned.
687 ;;(message "c-font-lock-complex-decl-prepare %s %s" (point) limit)
689 ;; Clear the list of found types if we start from the start of the
690 ;; buffer, to make it easier to get rid of misspelled types and
691 ;; variables that has gotten recognized as types in malformed code.
693 (c-clear-found-types))
695 ;; Clear the c-type char properties in the region to recalculate
696 ;; them properly. This is necessary e.g. to handle constructs that
697 ;; might been required as declarations temporarily during editing.
698 ;; The interesting properties are anyway those put on the closest
699 ;; token before the region.
700 (c-clear-char-properties (point) limit
'c-type
)
702 ;; Update `c-state-cache' to the beginning of the region. This will
703 ;; make `c-beginning-of-syntax' go faster when it's used later on,
704 ;; and it's near the point most of the time.
707 ;; Check if the fontified region starts inside a declarator list so
708 ;; that `c-font-lock-declarators' should be called at the start.
709 (let ((prop (save-excursion
710 (c-backward-syntactic-ws)
712 (c-get-char-property (1- (point)) 'c-type
)))))
713 (when (memq prop
'(c-decl-id-start c-decl-type-start
))
714 (c-forward-syntactic-ws limit
)
715 (c-font-lock-declarators limit t
(eq prop
'c-decl-type-start
))))
719 (defun c-font-lock-<>-arglists
(limit)
720 ;; Fontify types and references in names containing angle bracket
721 ;; arglists from the point to LIMIT. This will also fontify cases
722 ;; like normal function calls on the form "foo (a < b, c > d)", but
723 ;; `c-font-lock-declarations' will undo that later. Nil is always
726 (let (;; The font-lock package in Emacs is known to clobber
727 ;; `parse-sexp-lookup-properties' (when it exists).
728 (parse-sexp-lookup-properties
729 (cc-eval-when-compile
730 (boundp 'parse-sexp-lookup-properties
)))
731 id-start id-end pos kwd-sym
)
733 (while (and (< (point) limit
)
734 (re-search-forward c-opt-
<>-arglist-start limit t
))
736 (setq id-start
(match-beginning 1)
741 (unless (c-skip-comments-and-strings limit
)
743 (if (or (not (eq (get-text-property id-start
'face
)
744 'font-lock-keyword-face
))
745 (when (looking-at c-opt-
<>-sexp-key
)
746 (setq kwd-sym
(c-keyword-sym (match-string 1)))))
749 ;; Check for comment/string both at the identifier and
751 (unless (c-skip-comments-and-strings limit
)
753 (when (c-forward-<>-arglist
(c-keyword-member kwd-sym
756 (when (and c-opt-identifier-concat-key
757 (not (get-text-property id-start
'face
)))
758 (c-forward-syntactic-ws)
759 (if (looking-at c-opt-identifier-concat-key
)
760 (c-put-font-lock-face id-start id-end
761 c-reference-face-name
)
762 (c-put-font-lock-face id-start id-end
763 'font-lock-type-face
))))
769 (defun c-font-lock-declarators (limit list types
)
770 ;; Assuming the point is at the start of a declarator in a
771 ;; declaration, fontify it. If LIST is non-nil, fontify also all
772 ;; following declarators in a comma separated list (e.g. "foo" and
773 ;; "bar" in "int foo = 17, bar;"). Stop at LIMIT. If TYPES is
774 ;; non-nil, fontify all identifiers as types. Nil is always
777 ;;(message "c-font-lock-declarators from %s to %s" (point) limit)
778 (c-fontify-types-and-refs
779 ((pos (point)) next-pos id-start id-end
782 c-last-identifier-range
783 (separator-prop (if types
'c-decl-type-start
'c-decl-id-start
)))
789 (let (got-identifier)
791 ;; Skip over type decl prefix operators. (Note similar
792 ;; code in `c-font-lock-declarations'.)
793 (while (and (looking-at c-type-decl-prefix-key
)
794 (if (and (c-major-mode-is 'c
++-mode
)
796 ;; If the second submatch matches in C++ then
797 ;; we're looking at an identifier that's a
798 ;; prefix only if it specifies a member pointer.
800 (setq id-start
(point))
802 (if (looking-at "\\(::\\)")
803 ;; We only check for a trailing "::" and
804 ;; let the "*" that should follow be
805 ;; matched in the next round.
807 ;; It turned out to be the real identifier,
808 ;; so flag that and stop.
809 (setq got-identifier t
)
812 (if (eq (char-after) ?\
()
814 (setq paren-depth
(1+ paren-depth
))
816 (goto-char (match-end 1)))
817 (c-forward-syntactic-ws))
819 ;; If we didn't pass the identifier above already, do it now.
820 (unless got-identifier
821 (setq id-start
(point))
823 (setq id-end
(point))
827 ;; Skip out of the parens surrounding the identifier.
828 (or (= paren-depth
0)
829 (c-safe (goto-char (scan-lists (point) 1 paren-depth
))))
833 ;; Search syntactically to the end of the declarator (";",
834 ;; ",", a closen paren, eob etc) or to the beginning of an
835 ;; initializer or function prototype ("=" or "\\s\(").
836 ;; Note that the open paren will match array specs in
837 ;; square brackets, and we treat them as initializers too.
838 (c-syntactic-re-search-forward
839 "[;,]\\|\\s)\\|\\'\\|\\(=\\|\\s(\\)" limit t t
))
841 (setq next-pos
(match-beginning 0)
842 id-face
(if (eq (char-after next-pos
) ?\
()
843 'font-lock-function-name-face
844 'font-lock-variable-name-face
)
845 got-init
(and (match-beginning 1)
846 (char-after (match-beginning 1))))
849 ;; Register and fontify the identifer as a type.
850 (let ((c-promote-possible-types t
))
853 ;; Fontify the last symbol in the identifier if it isn't fontified
854 ;; already. The check is necessary only in certain cases where this
855 ;; function is used "sloppily", e.g. in `c-simple-decl-matchers'.
856 (when (and c-last-identifier-range
857 (not (get-text-property (car c-last-identifier-range
)
859 (c-put-font-lock-face (car c-last-identifier-range
)
860 (cdr c-last-identifier-range
)
866 ;; Jump past any initializer or function prototype to see if
867 ;; there's a ',' to continue at.
869 (cond ((eq id-face
'font-lock-function-name-face
)
870 ;; Skip a parenthesized initializer (C++) or a function
872 (if (c-safe (c-forward-sexp 1) t
)
873 (c-forward-syntactic-ws limit
)
877 ;; Skip an initializer expression. If we're at a '='
878 ;; then accept a brace list directly after it to cope
879 ;; with array initializers. Otherwise stop at braces
880 ;; to avoid going past full function and class blocks.
881 (and (if (and (eq got-init ?
=)
882 (= (c-forward-token-2 1 nil limit
) 0)
884 (c-safe (c-forward-sexp) t
)
886 (c-syntactic-re-search-forward "[;,{]" limit
'move t
)
889 (t (c-forward-syntactic-ws limit
)))
891 ;; If a ',' is found we set pos to the next declarator and iterate.
892 (when (and (< (point) limit
) (looking-at ","))
893 (c-put-char-property (point) 'c-type separator-prop
)
895 (c-forward-syntactic-ws limit
)
896 (setq pos
(point))))))
899 (defconst c-font-lock-maybe-decl-faces
900 ;; List of faces that might be put at the start of a type when
901 ;; `c-font-lock-declarations' runs. This needs to be evaluated to
902 ;; ensure that face name aliases in Emacs are resolved.
905 c-reference-face-name
906 font-lock-keyword-face
))
908 ;; Macro used inside `c-font-lock-declarations'. It ought to be a
909 ;; defsubst or perhaps even a defun, but it contains lots of free
910 ;; variables that refer to things inside `c-font-lock-declarations'.
911 (defmacro c-fl-shift-type-backward
(&optional short
)
912 ;; `c-font-lock-declarations' can consume an arbitrary length list
913 ;; of types when parsing a declaration, which means that it
914 ;; sometimes consumes the identifier in the declaration as a type.
915 ;; This is used to "backtrack" and make the last type be treated
916 ;; as an identifier instead.
919 ;; These identifiers are bound only in the inner let.
920 '(setq identifier-type at-type
921 identifier-start type-start
922 identifier-end type-end
))
923 (if (setq at-type
(if (eq prev-at-type
'prefix
)
926 (setq type-start prev-type-start
927 type-end prev-type-end
)
928 (setq type-start start-pos
931 ;; These identifiers are bound only in the inner let.
932 '(setq start type-end
936 got-suffix-after-parens t
939 (defun c-font-lock-declarations (limit)
940 ;; Fontify all the declarations and casts from the point to LIMIT.
941 ;; Assumes that strings and comments have been fontified already.
942 ;; Nil is always returned.
944 ;; This function can make hidden buffer changes, but the font-lock
945 ;; context covers that.
947 ;;(message "c-font-lock-declarations search from %s to %s" (point) limit)
951 c-restricted-
<>-arglists
952 ;; Nonzero if the `c-decl-prefix-re' match is in an arglist context,
953 ;; as opposed to a statement-level context. The major difference is
954 ;; that "," works as declaration delimiter in an arglist context,
955 ;; whereas it only separates declarators in the same declaration in
956 ;; a statement context. If it's nonzero then the value is the
957 ;; matched char, e.g. ?\( or ?,.
959 ;; 'decl if we're in an arglist containing declarations (but if
960 ;; `c-recognize-paren-inits' is set it might also be an initializer
961 ;; arglist), '<> if the arglist is of angle bracket type, 'other if
962 ;; it's some other arglist, or nil if not in an arglist at all.
964 ;; Set to the result of `c-forward-type'.
966 ;; These record the start and end of the type or possible type found
967 ;; by `c-forward-type'. `type-start' is at the start of the first
968 ;; type token, and `type-end' is at the start of the first token
969 ;; after the type (and after any specifiers).
971 ;; These store `at-type', `type-start' and `type-end' of the
972 ;; identifier before the one in those variables. The previous
973 ;; identifier might turn out to be the real type in a declaration if
974 ;; the last one has to be the declarator in it. If `prev-at-type'
975 ;; is nil then the other variables have undefined values.
976 prev-at-type prev-type-start prev-type-end
977 ;; Whether we've found a declaration or a cast. We might know this
978 ;; before we've found the type in it.
980 ;; Set when we need to back up to parse this as a declaration but
983 ;; Set if we've found a "typedef" specifier. The identifiers in the
984 ;; declaration are then fontified as types.
986 ;; Set if we've found a specifier that can start a declaration where
989 ;; The position of the next token after the closing paren of the
990 ;; last fontified cast.
992 ;; The same for the currently investigated cast.
994 ;; The maximum of the end positions of all the checked type decl
995 ;; expressions in the successfully identified declarations. The
996 ;; position might be either before or after the syntactic whitespace
997 ;; following the last token in the type decl expression.
998 (max-type-decl-end 0)
999 ;; Same as `max-type-decl-*', but used when we're before
1001 (max-type-decl-end-before-token 0)
1002 ;; Allow recording of identifier ranges in `c-forward-type' etc for
1003 ;; later fontification. Not using `c-fontify-types-and-refs' here
1004 ;; since the ranges should be fontified selectively only when a
1005 ;; declaration or cast has been successfully recognized.
1006 c-record-type-identifiers
1007 c-record-ref-identifiers
1008 ;; The font-lock package in Emacs is known to clobber
1009 ;; `parse-sexp-lookup-properties' (when it exists).
1010 (parse-sexp-lookup-properties
1011 (cc-eval-when-compile
1012 (boundp 'parse-sexp-lookup-properties
))))
1014 ;; Below we fontify a whole declaration even when it crosses the limit,
1015 ;; to avoid gaps when lazy-lock fontifies the file a screenful at a
1016 ;; time. That is however annoying during editing, e.g. the following is
1017 ;; a common situation while the first line is being written:
1020 ;; some_other_variable = 0;
1022 ;; font-lock will put the limit at the beginning of the second line
1023 ;; here, and if we go past it we'll fontify "my_variable" as a type and
1024 ;; "some_other_variable" as an identifier, and the latter will not
1025 ;; correct itself until the second line is changed. To avoid that we
1026 ;; narrow to the limit if the region to fontify is a single line.
1027 (when (<= limit
(c-point 'bonl
))
1031 ;; Narrow after any operator chars following the limit though, since
1032 ;; those characters can be useful in recognizing a declaration (in
1033 ;; particular the '{' that opens a function body after the header).
1035 (skip-chars-forward c-nonsymbol-chars
)
1041 c-font-lock-maybe-decl-faces
1043 (lambda (match-pos inside-macro
)
1045 ;; Don't do anything more if we're looking at a keyword
1046 ;; that can't start a declaration.
1047 (when (and (eq (get-text-property (point) 'face
)
1048 'font-lock-keyword-face
)
1049 (looking-at c-not-decl-init-keywords
))
1050 (throw 'false-alarm t
))
1052 ;; Set `arglist-match' and `arglist-type'. Look for "<" for the
1053 ;; sake of C++-style template arglists.
1054 (setq arglist-match
(char-before match-pos
))
1055 (if (memq arglist-match
'(?\
( ?
, ?\
[ ?
<))
1057 ;; Find out the type of the arglist.
1058 (if (<= match-pos
(point-min))
1059 (setq arglist-type
'other
)
1060 (let ((type (c-get-char-property (1- match-pos
) 'c-type
)))
1061 (cond ((eq type
'c-decl-arg-start
)
1062 ;; Got a cached hit in a declaration arglist.
1063 (setq arglist-type
'decl
))
1064 ((or (eq type
'c-
<>-arg-sep
)
1065 (eq arglist-match ?
<))
1066 ;; Inside an angle bracket arglist.
1067 (setq arglist-type
'<>))
1069 ;; Got a cached hit in some other type of arglist.
1070 (setq arglist-type
'other
))
1072 (< match-pos max-type-decl-end-before-token
)
1073 (< match-pos max-type-decl-end
))
1074 ;; The point is within the range of a previously
1075 ;; encountered type decl expression, so the arglist
1076 ;; is probably one that contains declarations.
1077 ;; However, if `c-recognize-paren-inits' is set it
1078 ;; might also be an initializer arglist.
1079 (setq arglist-type
'decl
)
1080 ;; The result of this check is cached with a char
1081 ;; property on the match token, so that we can look
1082 ;; it up again when refontifying single lines in a
1083 ;; multiline declaration.
1084 (c-put-char-property (1- match-pos
)
1085 'c-type
'c-decl-arg-start
))
1087 (setq arglist-type
'other
)))))
1089 (setq arglist-match nil
1094 backup-if-not-cast nil
1097 c-record-type-identifiers t
1098 c-record-ref-identifiers nil
1099 ;; `start-pos' is used below to point to the start of the
1100 ;; first type, i.e. after any leading specifiers. It might
1101 ;; also point at the beginning of the preceding syntactic
1104 ;; If we're in a normal arglist context we don't want to
1105 ;; recognize commas in nested angle bracket arglists since
1106 ;; those commas could be part of our own arglist.
1107 c-restricted-
<>-arglists
1108 (and c-recognize-
<>-arglists
1109 (eq arglist-type
'other
)))
1111 (when (and c-restricted-
<>-arglists
1112 (/= arglist-match ?
,))
1113 ;; We're standing at the start of a normal arglist so remove any
1114 ;; angle bracket arglists containing commas that's been
1115 ;; recognized inside it by the preceding slightly opportunistic
1116 ;; scan in `c-font-lock-<>-arglists'.
1117 (while (and (c-syntactic-re-search-forward
1118 c-opt-
<>-arglist-start-in-paren nil t t
)
1119 (match-beginning 1))
1121 (when (save-match-data
1122 (and (c-get-char-property (point) 'syntax-table
)
1123 (not (c-forward-<>-arglist nil t
))))
1124 (c-remove-font-lock-face (match-beginning 2) (match-end 2))))
1125 (goto-char start-pos
))
1127 ;; Check for a type, but be prepared to skip over leading
1128 ;; specifiers like "static". Unknown symbols are treated as
1129 ;; possible types, but they could also be specifiers disguised
1130 ;; through macros like __INLINE__, so we recognize both types and
1131 ;; known specifiers after them too.
1132 (while (let ((start (point))
1133 (res (unless (eq at-type t
)
1134 ;; Don't look for a type if we already found a
1135 ;; positive one; we only loop for the
1136 ;; `c-specifier-key' check then.
1140 ;; Found a known or possible type or a prefix of a known
1144 ;; Got two identifiers with nothing but whitespace
1145 ;; between them. That can only happen in
1147 (setq at-decl-or-cast t
)
1149 (when (eq at-type
'found
)
1150 ;; If the previous identifier is a found type we
1151 ;; record it as a real one; it might be some sort of
1152 ;; alias for a prefix like "unsigned".
1154 (goto-char type-start
)
1155 (let ((c-promote-possible-types t
))
1156 (c-forward-type)))))
1158 (setq prev-at-type at-type
1159 prev-type-start type-start
1160 prev-type-end type-end
1165 ;; If the type isn't known we continue so that we'll
1166 ;; jump over all specifiers and type identifiers. The
1167 ;; reason to do this for a known type prefix is to make
1168 ;; things like "unsigned INT16" work.
1169 (setq res
(not (eq res t
))))
1171 (if (looking-at c-specifier-key
)
1172 ;; Found a known specifier keyword. The specifier
1173 ;; keywords are restrictive, so we check for them
1174 ;; anywhere inside or around the type(s). We thereby
1175 ;; avoid having special cases for specifiers like MSVC
1176 ;; '__declspec' which can come after the type.
1178 (setq at-decl-or-cast t
)
1179 (let ((kwd-sym (c-keyword-sym (match-string 1))))
1180 (when (c-keyword-member
1181 kwd-sym
'c-typedef-decl-kwds
)
1182 (setq at-typedef t
))
1183 (when (c-keyword-member
1184 kwd-sym
'c-typeless-decl-kwds
)
1185 (setq maybe-typeless t
)))
1186 (c-forward-keyword-clause)
1187 ;; Move type-end forward if we've passed a type,
1188 ;; otherwise move start-pos forward.
1190 (setq type-end
(point))
1191 (setq start-pos
(point))))
1196 ((eq at-type
'prefix
)
1197 ;; A prefix type is itself a primitive type when it's not
1198 ;; followed by another type.
1202 ;; Got no type but set things up to continue anyway to handle the
1203 ;; various cases when a declaration doesn't start with a type.
1204 (setq type-end start-pos
))
1206 ((and (eq at-type
'maybe
)
1207 (c-major-mode-is 'c
++-mode
))
1208 ;; If it's C++ then check if the last "type" ends on the form
1209 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
1210 ;; (con|de)structor.
1212 (let (name end-2 end-1
)
1213 (goto-char type-end
)
1214 (c-backward-syntactic-ws)
1215 (setq end-2
(point))
1217 (c-simple-skip-symbol-backward)
1220 (buffer-substring-no-properties (point) end-2
))
1221 ;; Cheating in the handling of syntactic ws below.
1222 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
1224 (setq end-1
(point))
1225 (c-simple-skip-symbol-backward))
1226 (>= (point) type-start
)
1227 (equal (buffer-substring-no-properties (point) end-1
)
1229 ;; It is a (con|de)structor name. In that case the
1230 ;; declaration is typeless so zap out any preceding
1231 ;; identifier(s) that we might have taken as types.
1232 (goto-char type-start
)
1235 type-end type-start
))))))
1237 ;; Check for and step over a type decl expression after the thing
1238 ;; that is or might be a type. This can't be skipped since we need
1239 ;; the correct end position of the declarator for
1240 ;; `max-type-decl-end-*'.
1241 (let ((start (point)) (paren-depth 0) pos
1242 ;; True if there's a non-open-paren match of
1243 ;; `c-type-decl-prefix-key'.
1245 ;; True if the declarator is surrounded by a parenthesis pair.
1247 ;; True if there is an identifier in the declarator.
1249 ;; True if there's a non-close-paren match of
1250 ;; `c-type-decl-suffix-key'.
1252 ;; True if there's a prefix or suffix match outside the
1253 ;; outermost paren pair that surrounds the declarator.
1254 got-prefix-before-parens
1255 got-suffix-after-parens
1256 ;; True if we've parsed the type decl to a token that
1257 ;; is known to end declarations in this context.
1259 ;; The earlier values of `at-type', `type-start' and
1260 ;; `type-end' if we've shifted the type backwards.
1261 identifier-type identifier-start identifier-end
)
1262 (goto-char type-end
)
1264 ;; Skip over type decl prefix operators. (Note similar code in
1265 ;; `c-font-lock-declarators'.)
1266 (while (and (looking-at c-type-decl-prefix-key
)
1267 (if (and (c-major-mode-is 'c
++-mode
)
1268 (match-beginning 2))
1269 ;; If the second submatch matches in C++ then
1270 ;; we're looking at an identifier that's a prefix
1271 ;; only if it specifies a member pointer.
1272 (when (setq got-identifier
(c-forward-name))
1273 (if (looking-at "\\(::\\)")
1274 ;; We only check for a trailing "::" and
1275 ;; let the "*" that should follow be
1276 ;; matched in the next round.
1277 (progn (setq got-identifier nil
) t
)
1278 ;; It turned out to be the real identifier,
1282 (if (eq (char-after) ?\
()
1284 (setq paren-depth
(1+ paren-depth
))
1286 (unless got-prefix-before-parens
1287 (setq got-prefix-before-parens
(= paren-depth
0)))
1289 (goto-char (match-end 1)))
1290 (c-forward-syntactic-ws))
1291 (setq got-parens
(> paren-depth
0))
1293 ;; Skip over an identifier.
1295 (and (looking-at c-identifier-start
)
1296 (setq got-identifier
(c-forward-name))))
1298 ;; Skip over type decl suffix operators.
1299 (while (if (looking-at c-type-decl-suffix-key
)
1300 (if (eq (char-after) ?\
))
1301 (when (> paren-depth
0)
1302 (setq paren-depth
(1- paren-depth
))
1305 (when (if (save-match-data (looking-at "\\s\("))
1306 (c-safe (c-forward-sexp 1) t
)
1307 (goto-char (match-end 1))
1309 (unless got-suffix-after-parens
1310 (setq got-suffix-after-parens
(= paren-depth
0)))
1311 (setq got-suffix t
)))
1312 ;; No suffix matched. We might have matched the
1313 ;; identifier as a type and the open paren of a function
1314 ;; arglist as a type decl prefix. In that case we
1315 ;; should "backtrack": Reinterpret the last type as the
1316 ;; identifier, move out of the arglist and continue
1317 ;; searching for suffix operators.
1319 ;; Do this even if there's no preceding type, to cope
1320 ;; with old style function declarations in K&R C,
1321 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
1322 ;; style declarations. That isn't applicable in an
1323 ;; arglist context, though.
1324 (when (and (= paren-depth
1)
1325 (not got-prefix-before-parens
)
1326 (not (eq at-type t
))
1329 (when c-recognize-typeless-decls
1330 (not arglist-type
)))
1331 (setq pos
(c-up-list-forward (point)))
1332 (eq (char-before pos
) ?\
)))
1333 (c-fl-shift-type-backward)
1336 (c-forward-syntactic-ws))
1338 (when (and maybe-typeless
1339 (not got-identifier
)
1342 (not (eq at-type t
)))
1343 ;; Have found no identifier but `c-typeless-decl-kwds' has
1344 ;; matched so we know we're inside a declaration. The
1345 ;; preceding type must be the identifier instead.
1346 (c-fl-shift-type-backward))
1350 (catch 'at-decl-or-cast
1352 (when (> paren-depth
0)
1353 ;; Encountered something inside parens that isn't matched by
1354 ;; the `c-type-decl-*' regexps, so it's not a type decl
1355 ;; expression. Try to skip out to the same paren depth to
1356 ;; not confuse the cast check below.
1357 (c-safe (goto-char (scan-lists (point) 1 paren-depth
)))
1358 (throw 'at-decl-or-cast nil
))
1361 (looking-at (cond ((eq arglist-type
'<>) "[,>]")
1362 (arglist-type "[,\)]")
1365 ;; Now we've collected info about various characteristics of
1366 ;; the construct we're looking at. Below follows a decision
1367 ;; tree based on that. It's ordered to check more certain
1368 ;; signs before less certain ones.
1373 (when (and (or at-type maybe-typeless
)
1374 (not (or got-prefix got-parens
)))
1375 ;; Got another identifier directly after the type, so
1376 ;; it's a declaration.
1377 (throw 'at-decl-or-cast t
))
1379 (when (and got-parens
1381 (not got-suffix-after-parens
)
1382 (or prev-at-type maybe-typeless
))
1383 ;; Got a declaration of the form "foo bar (gnu);"
1384 ;; where we've recognized "bar" as the type and "gnu"
1385 ;; as the declarator. In this case it's however more
1386 ;; likely that "bar" is the declarator and "gnu" a
1387 ;; function argument or initializer (if
1388 ;; `c-recognize-paren-inits' is set), since the parens
1389 ;; around "gnu" would be superfluous if it's a
1390 ;; declarator. Shift the type one step backward.
1391 (c-fl-shift-type-backward)))
1393 ;; Found no identifier.
1396 (when (or (= (point) start
)
1400 ;; Got two types after each other, so if this isn't a
1401 ;; cast then the latter probably is the identifier and
1402 ;; we should back up to the previous type.
1403 (setq backup-if-not-cast t
)
1404 (throw 'at-decl-or-cast t
))
1406 (when (eq at-type t
)
1407 ;; If the type is known we know that there can't be any
1408 ;; identifier somewhere else, and it's only in
1409 ;; declarations in e.g. function prototypes and in casts
1410 ;; that the identifier may be left out.
1411 (throw 'at-decl-or-cast t
))
1413 (when (= (point) start
)
1414 ;; Only got a single identifier (parsed as a type so
1417 ;; Check that the identifier isn't at the start of
1421 ((eq arglist-type
'decl
)
1422 ;; Inside an arglist that contains declarations.
1423 ;; If K&R style declarations and parenthesis
1424 ;; style initializers aren't allowed then the
1425 ;; single identifier must be a type, else we
1426 ;; require that it's known or found (primitive
1427 ;; types are handled above).
1428 (or (and (not c-recognize-knr-p
)
1429 (not c-recognize-paren-inits
))
1430 (memq at-type
'(known found
))))
1431 ((eq arglist-type
'<>)
1432 ;; Inside a template arglist. Accept known and
1433 ;; found types; other identifiers could just as
1434 ;; well be constants in C++.
1435 (memq at-type
'(known found
)))))
1436 (throw 'at-decl-or-cast t
)
1437 (throw 'at-decl-or-cast nil
))))
1443 (not (eq at-type t
))
1447 (when c-recognize-typeless-decls
1448 (or (not got-suffix
)
1450 c-after-suffixed-type-maybe-decl-key
))))))
1451 ;; Got an empty paren pair and a preceding type that
1452 ;; probably really is the identifier. Shift the type
1453 ;; backwards to make the last one the identifier. This
1454 ;; is analogous to the "backtracking" done inside the
1455 ;; `c-type-decl-suffix-key' loop above.
1457 ;; Exception: In addition to the conditions in that
1458 ;; "backtracking" code, do not shift backward if we're
1459 ;; not looking at either `c-after-suffixed-type-decl-key'
1460 ;; or "[;,]". Since there's no preceding type, the
1461 ;; shift would mean that the declaration is typeless.
1462 ;; But if the regexp doesn't match then we will simply
1463 ;; fall through in the tests below and not recognize it
1464 ;; at all, so it's better to try it as an abstract
1465 ;; declarator instead.
1466 (c-fl-shift-type-backward)
1468 ;; Still no identifier.
1470 (when (and got-prefix
(or got-parens got-suffix
))
1471 ;; Require `got-prefix' together with either
1472 ;; `got-parens' or `got-suffix' to recognize it as an
1473 ;; abstract declarator: `got-parens' only is probably an
1474 ;; empty function call. `got-suffix' only can build an
1475 ;; ordinary expression together with the preceding
1476 ;; identifier which we've taken as a type. We could
1477 ;; actually accept on `got-prefix' only, but that can
1478 ;; easily occur temporarily while writing an expression
1479 ;; so we avoid that case anyway. We could do a better
1480 ;; job if we knew the point when the fontification was
1482 (throw 'at-decl-or-cast t
))))
1484 (when at-decl-or-cast
1485 ;; By now we've located the type in the declaration that we
1487 (throw 'at-decl-or-cast t
))
1489 (when (and got-identifier
1491 (looking-at c-after-suffixed-type-decl-key
)
1495 (not (eq at-type t
)))
1496 ;; Shift the type backward in the case that
1497 ;; there's a single identifier inside parens.
1498 ;; That can only occur in K&R style function
1499 ;; declarations so it's more likely that it
1500 ;; really is a function call. Therefore we
1501 ;; only do this after
1502 ;; `c-after-suffixed-type-decl-key' has
1504 (progn (c-fl-shift-type-backward) t
)
1505 got-suffix-after-parens
))
1506 ;; A declaration according to
1507 ;; `c-after-suffixed-type-decl-key'.
1508 (throw 'at-decl-or-cast t
))
1510 (when (and (or got-prefix
(not got-parens
))
1511 (memq at-type
'(t known
)))
1512 ;; It's a declaration if a known type precedes it and it
1513 ;; can't be a function call.
1514 (throw 'at-decl-or-cast t
))
1516 ;; If we get here we can't tell if this is a type decl or a
1517 ;; normal expression by looking at it alone. (That's under
1518 ;; the assumption that normal expressions always can look like
1519 ;; type decl expressions, which isn't really true but the
1520 ;; cases where it doesn't hold are so uncommon (e.g. some
1521 ;; placements of "const" in C++) it's not worth the effort to
1524 (unless (or at-decl-end
(looking-at "=[^=]"))
1525 ;; If this is a declaration it should end here or its
1526 ;; initializer(*) should start here, so check for allowed
1527 ;; separation tokens. Note that this rule doesn't work
1528 ;; e.g. with a K&R arglist after a function header.
1530 ;; *) Don't check for C++ style initializers using parens
1531 ;; since those already have been matched as suffixes.
1532 (throw 'at-decl-or-cast nil
))
1534 ;; Below are tests that only should be applied when we're
1535 ;; certain to not have parsed halfway through an expression.
1537 (when (memq at-type
'(t known
))
1538 ;; The expression starts with a known type so treat it as a
1540 (throw 'at-decl-or-cast t
))
1542 (when (and (c-major-mode-is 'c
++-mode
)
1543 ;; In C++ we check if the identifier is a known
1544 ;; type, since (con|de)structors use the class name
1545 ;; as identifier. We've always shifted over the
1546 ;; identifier as a type and then backed up again in
1549 (or (memq identifier-type
'(found known
))
1550 (and (eq (char-after identifier-start
) ?~
)
1551 ;; `at-type' probably won't be 'found for
1552 ;; destructors since the "~" is then part
1553 ;; of the type name being checked against
1554 ;; the list of known types, so do a check
1555 ;; without that operator.
1557 (goto-char (1+ identifier-start
))
1558 (c-forward-syntactic-ws)
1559 (c-with-syntax-table
1560 c-identifier-syntax-table
1561 (looking-at c-known-type-key
)))
1562 (c-check-type (1+ identifier-start
)
1564 (throw 'at-decl-or-cast t
))
1568 (when (and got-prefix-before-parens
1570 (or at-decl-end
(looking-at "=[^=]"))
1573 ;; Got something like "foo * bar;". Since we're not
1574 ;; inside an arglist it would be a meaningless
1575 ;; expression because the result isn't used. We
1576 ;; therefore choose to recognize it as a declaration.
1577 ;; Do not allow a suffix since it could then be a
1579 (throw 'at-decl-or-cast t
))
1581 (when (and (or got-suffix-after-parens
1582 (looking-at "=[^=]"))
1584 (not (eq arglist-type
'other
)))
1585 ;; Got something like "a (*b) (c);" or "a (b) = c;".
1586 ;; It could be an odd expression or it could be a
1587 ;; declaration. Treat it as a declaration if "a" has
1588 ;; been used as a type somewhere else (if it's a known
1589 ;; type we won't get here).
1590 (throw 'at-decl-or-cast t
)))
1592 (when (and arglist-type
1594 (and (eq arglist-type
'decl
)
1595 (not c-recognize-paren-inits
)
1596 (or got-parens got-suffix
))))
1597 ;; Got a type followed by an abstract declarator. If
1598 ;; `got-prefix' is set it's something like "a *" without
1599 ;; anything after it. If `got-parens' or `got-suffix' is
1600 ;; set it's "a()", "a[]", "a()[]", or similar, which we
1601 ;; accept only if the context rules out expressions.
1602 (throw 'at-decl-or-cast t
)))
1604 ;; If we had a complete symbol table here (which rules out
1605 ;; `c-found-types') we should return t due to the
1606 ;; disambiguation rule (in at least C++) that anything that
1607 ;; can be parsed as a declaration is a declaration. Now we're
1608 ;; being more defensive and prefer to highlight things like
1609 ;; "foo (bar);" as a declaration only if we're inside an
1610 ;; arglist that contains declarations.
1611 (eq arglist-type
'decl
))))
1613 ;; Point is now after the type decl expression.
1616 ;; Check for a cast.
1621 ;; Should be the first type/identifier in a cast paren.
1622 (memq arglist-match c-cast-parens
)
1624 ;; The closing paren should follow.
1626 (c-forward-syntactic-ws)
1627 (looking-at "\\s\)"))
1629 ;; There should be a primary expression after it.
1632 (c-forward-syntactic-ws)
1633 (setq cast-end
(point))
1634 (and (looking-at c-primary-expr-regexp
)
1636 (setq pos
(match-end 0))
1638 ;; Check if the expression begins with a prefix
1641 (if (match-beginning 1)
1642 ;; Expression begins with an ambiguous operator.
1643 ;; Treat it as a cast if it's a type decl or if
1644 ;; we've recognized the type somewhere else.
1646 (memq at-type
'(t known found
)))
1647 ;; Unless it's a keyword, it's the beginning of a
1648 ;; primary expression.
1649 (not (looking-at c-keywords-regexp
)))))
1650 ;; If `c-primary-expr-regexp' matched a nonsymbol
1651 ;; token, check that it matched a whole one so that we
1652 ;; don't e.g. confuse the operator '-' with '->'. It's
1653 ;; ok if it matches further, though, since it e.g. can
1654 ;; match the float '.5' while the operator regexp only
1656 (or (not (looking-at c-nonsymbol-token-regexp
))
1657 (<= (match-end 0) pos
))))
1659 ;; There should either be a cast before it or something that
1660 ;; isn't an identifier or close paren.
1663 (goto-char (1- match-pos
))
1664 (or (eq (point) last-cast-end
)
1666 (c-backward-syntactic-ws)
1667 (if (< (skip-syntax-backward "w_") 0)
1668 ;; It's a symbol. Accept it only if it's one of
1669 ;; the keywords that can precede an expression
1670 ;; (without surrounding parens).
1671 (looking-at c-simple-stmt-key
)
1673 ;; Check that it isn't a close paren (block close
1675 (not (memq (char-before) '(?\
) ?\
])))
1676 ;; Check that it isn't a nonsymbol identifier.
1677 (not (c-on-identifier)))))))))
1680 (setq last-cast-end cast-end
)
1681 (when (and at-type
(not (eq at-type t
)))
1682 (let ((c-promote-possible-types t
))
1683 (goto-char type-start
)
1687 ;; We're at a declaration. Highlight the type and the following
1690 (when backup-if-not-cast
1691 (c-fl-shift-type-backward t
))
1693 (when (and (eq arglist-type
'decl
) (looking-at ","))
1694 ;; Make sure to propagate the `c-decl-arg-start' property to
1695 ;; the next argument if it's set in this one, to cope with
1696 ;; interactive refontification.
1697 (c-put-char-property (point) 'c-type
'c-decl-arg-start
))
1699 ;; Set `max-type-decl-end' or `max-type-decl-end-before-token'
1700 ;; under the assumption that we're after the first type decl
1701 ;; expression in the declaration now. That's not really true; we
1702 ;; could also be after a parenthesized initializer expression in
1703 ;; C++, but this is only used as a last resort to slant ambiguous
1704 ;; expression/declarations, and overall it's worth the risk to
1705 ;; occasionally fontify an expression as a declaration in an
1706 ;; initializer expression compared to getting ambiguous things in
1707 ;; normal function prototypes fontified as expressions.
1709 (when (> (point) max-type-decl-end-before-token
)
1710 (setq max-type-decl-end-before-token
(point)))
1711 (when (> (point) max-type-decl-end
)
1712 (setq max-type-decl-end
(point))))
1714 (when (and at-type
(not (eq at-type t
)))
1715 (let ((c-promote-possible-types t
))
1716 (goto-char type-start
)
1719 (goto-char type-end
)
1723 ;; Should normally not fontify a list of declarators
1724 ;; inside an arglist, but the first argument in the
1725 ;; ';' separated list of a "for" statement is an
1727 (when (and (eq arglist-match ?\
() (/= match-pos
0))
1729 (goto-char (1- match-pos
))
1730 (c-backward-syntactic-ws)
1731 (and (c-simple-skip-symbol-backward)
1732 (looking-at c-paren-stmt-key
))))
1735 ;; Fix the `c-decl-id-start' or `c-decl-type-start' property
1736 ;; before the first declarator if it's a list.
1737 ;; `c-font-lock-declarators' handles the rest.
1740 (c-backward-syntactic-ws)
1742 (c-put-char-property (1- (point)) 'c-type
1745 'c-decl-id-start
)))))
1747 (c-font-lock-declarators (point-max) decl-list at-typedef
)))
1750 ;; False alarm. Skip the fontification done below.
1751 (throw 'false-alarm t
)))
1753 ;; A cast or declaration has been successfully identified, so do
1754 ;; all the fontification of types and refs that's been recorded by
1755 ;; the calls to `c-forward-type' and `c-forward-name' above.
1756 (c-fontify-recorded-types-and-refs)
1761 (c-lang-defconst c-simple-decl-matchers
1762 "Simple font lock matchers for types and declarations. These are used
1763 on level 2 only and so aren't combined with `c-complex-decl-matchers'."
1765 t
`(;; Objective-C methods.
1766 ,@(when (c-major-mode-is 'objc-mode
)
1767 `((,(c-lang-const c-opt-method-key
)
1770 (let (;; The font-lock package in Emacs is known to clobber
1771 ;; `parse-sexp-lookup-properties' (when it exists).
1772 (parse-sexp-lookup-properties
1773 (cc-eval-when-compile
1774 (boundp 'parse-sexp-lookup-properties
))))
1776 (narrow-to-region (point-min) limit
)
1777 (c-font-lock-objc-method)))
1779 (goto-char (match-end 1))))))
1781 ;; Fontify all type names and the identifiers in the
1782 ;; declarations they might start. Use eval here since
1783 ;; `c-known-type-key' gets its value from
1784 ;; `*-font-lock-extra-types' on mode init.
1785 (eval .
(list ,(c-make-font-lock-search-function
1787 '(1 'font-lock-type-face t
)
1788 '((c-font-lock-declarators limit t nil
)
1790 (goto-char (match-end 1))
1791 (c-forward-syntactic-ws))
1792 (goto-char (match-end 1))))))
1794 ;; Fontify types preceded by `c-type-prefix-kwds' and the
1795 ;; identifiers in the declarations they might start.
1796 ,@(when (c-lang-const c-type-prefix-kwds
)
1797 (let ((prefix-re (c-make-keywords-re nil
1798 (c-lang-const c-type-prefix-kwds
))))
1799 `((,(c-make-font-lock-search-function
1800 (concat "\\<\\(" prefix-re
"\\)"
1802 "\\(" (c-lang-const c-symbol-key
) "\\)")
1803 `(,(+ (c-regexp-opt-depth prefix-re
) 2)
1804 'font-lock-type-face t
)
1805 '((c-font-lock-declarators limit t nil
)
1807 (goto-char (match-end 2))
1808 (c-forward-syntactic-ws))
1809 (goto-char (match-end 2))))))))
1811 ;; Fontify special declarations that lacks a type.
1812 ,@(when (c-lang-const c-typeless-decl-kwds
)
1813 `((,(c-make-font-lock-search-function
1815 (c-regexp-opt (c-lang-const c-typeless-decl-kwds
))
1817 '((c-font-lock-declarators limit t nil
)
1819 (goto-char (match-end 1))
1820 (c-forward-syntactic-ws))
1821 (goto-char (match-end 1)))))))
1824 (c-lang-defconst c-complex-decl-matchers
1825 "Complex font lock matchers for types and declarations. Used on level
1828 t
`(;; Initialize some things before the search functions below.
1829 c-font-lock-complex-decl-prepare
1831 ;; Fontify angle bracket arglists like templates in C++.
1832 ,@(when (c-lang-const c-recognize-
<>-arglists
)
1833 `(c-font-lock-<>-arglists
))
1835 ,@(if (c-major-mode-is 'objc-mode
)
1836 ;; Fontify method declarations in Objective-C, but first
1837 ;; we have to put the `c-decl-end' `c-type' property on
1838 ;; all the @-style directives that haven't been handled in
1839 ;; `c-basic-matchers-before'.
1840 `(,(c-make-font-lock-search-function
1841 (c-make-keywords-re t
1842 ;; Exclude "@class" since that directive ends with a
1843 ;; semicolon anyway.
1845 (append (c-lang-const c-protection-kwds
)
1846 (c-lang-const c-other-decl-kwds
)
1848 '((c-put-char-property (1- (match-end 1))
1849 'c-type
'c-decl-end
)))
1851 c-font-lock-objc-methods
)
1853 (when (c-lang-const c-opt-access-key
)
1854 `(,(c-make-font-lock-search-function
1855 (c-lang-const c-opt-access-key
)
1856 '((c-put-char-property (1- (match-end 0))
1857 'c-type
'c-decl-end
))))))
1859 ;; Fontify all declarations and casts.
1860 c-font-lock-declarations
1862 ;; The first two rules here mostly find occurences that
1863 ;; `c-font-lock-declarations' has found already, but not
1864 ;; declarations containing blocks in the type (see note below).
1865 ;; It's also useful to fontify these everywhere to show e.g. when
1866 ;; a type keyword is accidentally used as an identifier.
1868 ;; Fontify basic types.
1869 ,(let ((re (c-make-keywords-re nil
1870 (c-lang-const c-primitive-type-kwds
))))
1871 (if (c-major-mode-is 'pike-mode
)
1872 ;; No symbol is a keyword after "->" in Pike.
1873 `(,(concat "\\(\\=\\|\\(\\=\\|[^-]\\)[^>]\\)"
1874 "\\<\\(" re
"\\)\\>")
1875 3 font-lock-type-face
)
1876 `(,(concat "\\<\\(" re
"\\)\\>")
1877 1 'font-lock-type-face
)))
1879 ;; Fontify types preceded by `c-type-prefix-kwds'.
1880 ,@(when (c-lang-const c-type-prefix-kwds
)
1883 (c-fontify-types-and-refs
1884 ((c-promote-possible-types t
)
1885 ;; The font-lock package in Emacs is known to clobber
1886 ;; `parse-sexp-lookup-properties' (when it exists).
1887 (parse-sexp-lookup-properties
1888 (cc-eval-when-compile
1889 (boundp 'parse-sexp-lookup-properties
))))
1891 ;; Narrow to avoid going past the limit in
1892 ;; `c-forward-type'.
1893 (narrow-to-region (point) limit
)
1894 (while (re-search-forward
1896 (c-make-keywords-re nil
1897 (c-lang-const c-type-prefix-kwds
))
1900 (unless (c-skip-comments-and-strings limit
)
1901 (c-forward-syntactic-ws)
1902 ;; Handle prefix declaration specifiers.
1903 (when (looking-at c-specifier-key
)
1904 (c-forward-keyword-clause))
1905 ,(if (c-major-mode-is 'c
++-mode
)
1906 `(when (and (c-forward-type)
1907 (eq (char-after) ?
=))
1908 ;; In C++ we additionally check for a "class
1909 ;; X = Y" construct which is used in
1910 ;; templates, to fontify Y as a type.
1912 (c-forward-syntactic-ws)
1917 ;; Fontify symbols after closing braces as declaration
1918 ;; identifiers under the assumption that they are part of
1919 ;; declarations like "class Foo { ... } foo;". It's too
1920 ;; expensive to check this accurately by skipping past the
1921 ;; brace block, so we use the heuristic that it's such a
1922 ;; declaration if the first identifier is on the same line as
1923 ;; the closing brace. `c-font-lock-declarations' will later
1924 ;; override it if it turns out to be an new declaration, but
1925 ;; it will be wrong if it's an expression (see the test
1927 ,@(when (c-lang-const c-opt-block-decls-with-vars-key
)
1928 `((,(c-make-font-lock-search-function
1930 (c-lang-const c-single-line-syntactic-ws
)
1931 "\\(" ; 1 + c-single-line-syntactic-ws-depth
1932 (c-lang-const c-type-decl-prefix-key
)
1934 (c-lang-const c-symbol-key
)
1936 `((c-font-lock-declarators limit t nil
)
1938 (c-put-char-property (match-beginning 0) 'c-type
1940 (goto-char (match-beginning
1942 c-single-line-syntactic-ws-depth
)))))
1943 (goto-char (match-end 0)))))))
1945 ;; Fontify the type in C++ "new" expressions.
1946 ,@(when (c-major-mode-is 'c
++-mode
)
1948 (c-font-lock-c++-new
))))
1951 (defun c-font-lock-labels (limit)
1952 ;; Fontify all the declarations from the point to LIMIT. Assumes
1953 ;; that strings and comments have been fontified already. Nil is
1956 ;; This function can make hidden buffer changes, but the font-lock
1957 ;; context covers that.
1959 (let (continue-pos id-start
1960 ;; The font-lock package in Emacs is known to clobber
1961 ;; `parse-sexp-lookup-properties' (when it exists).
1962 (parse-sexp-lookup-properties
1963 (cc-eval-when-compile
1964 (boundp 'parse-sexp-lookup-properties
))))
1966 (while (re-search-forward ":[^:]" limit t
)
1967 (setq continue-pos
(point))
1968 (goto-char (match-beginning 0))
1969 (unless (c-skip-comments-and-strings limit
)
1971 (c-backward-syntactic-ws)
1972 (and (setq id-start
(c-on-identifier))
1974 (not (get-text-property id-start
'face
))
1977 (goto-char id-start
)
1978 (c-backward-syntactic-ws)
1980 ;; Check for a char that precedes a statement.
1981 (memq (char-before) '(?\
} ?\
{ ?\
;))
1982 ;; Check for a preceding label. We exploit the font
1983 ;; locking made earlier by this function.
1984 (and (eq (char-before) ?
:)
1987 (c-backward-syntactic-ws)
1989 (eq (get-text-property (1- (point)) 'face
)
1991 ;; Check for a keyword that precedes a statement.
1992 (c-after-conditional)))
1996 (goto-char id-start
)
1997 (looking-at c-symbol-key
)
1998 (c-put-font-lock-face (match-beginning 0) (match-end 0)
1999 c-label-face-name
)))
2001 (goto-char continue-pos
))))
2004 (c-lang-defconst c-basic-matchers-after
2005 "Font lock matchers for various things that should be fontified after
2006 generic casts and declarations are fontified. Used on level 2 and
2009 t
`(;; Fontify the identifiers inside enum lists. (The enum type
2010 ;; name is handled by `c-simple-decl-matchers' or
2011 ;; `c-complex-decl-matchers' below.
2012 ,@(when (c-lang-const c-brace-id-list-kwds
)
2013 `((,(c-make-font-lock-search-function
2016 (c-make-keywords-re nil
(c-lang-const c-brace-id-list-kwds
))
2018 ;; Disallow various common punctuation chars that can't come
2019 ;; before the '{' of the enum list, to avoid searching too far.
2022 '((c-font-lock-declarators limit t nil
)
2024 (goto-char (match-end 0))
2025 (c-put-char-property (1- (point)) 'c-type
2027 (c-forward-syntactic-ws))
2028 (goto-char (match-end 0)))))))
2030 ;; Fontify labels in languages that supports them.
2031 ,@(when (c-lang-const c-label-key
)
2033 `(;; Fontify labels after goto etc.
2034 ;; (Got three different interpretation levels here,
2035 ;; which makes it a bit complicated: 1) The backquote
2036 ;; stuff is expanded when compiled or loaded, 2) the
2037 ;; eval form is evaluated at font-lock setup (to
2038 ;; substitute c-label-face-name correctly), and 3) the
2039 ;; resulting structure is interpreted during
2042 .
,(let* ((c-before-label-re
2043 (c-make-keywords-re nil
2044 (c-lang-const c-before-label-kwds
))))
2046 ,(concat "\\<\\(" c-before-label-re
"\\)\\>"
2048 "\\(" ; identifier-offset
2049 (c-lang-const c-symbol-key
)
2051 (list ,(+ (c-regexp-opt-depth c-before-label-re
) 2)
2052 c-label-face-name nil t
))))
2054 ;; Fontify normal labels.
2055 c-font-lock-labels
))
2057 ;; Fontify the clauses after various keywords.
2058 ,@(when (or (c-lang-const c-type-list-kwds
)
2059 (c-lang-const c-ref-list-kwds
)
2060 (c-lang-const c-colon-type-list-kwds
)
2061 (c-lang-const c-paren-type-kwds
))
2062 `((,(c-make-font-lock-search-function
2064 (c-make-keywords-re nil
2065 (append (c-lang-const c-type-list-kwds
)
2066 (c-lang-const c-ref-list-kwds
)
2067 (c-lang-const c-colon-type-list-kwds
)
2068 (c-lang-const c-paren-type-kwds
)))
2070 '((c-fontify-types-and-refs ((c-promote-possible-types t
))
2071 (c-forward-keyword-clause)
2072 (if (> (point) limit
) (goto-char limit
))))))))
2075 (c-lang-defconst c-matchers-1
2076 t
(c-lang-const c-cpp-matchers
))
2078 (c-lang-defconst c-matchers-2
2079 t
(append (c-lang-const c-matchers-1
)
2080 (c-lang-const c-basic-matchers-before
)
2081 (c-lang-const c-simple-decl-matchers
)
2082 (c-lang-const c-basic-matchers-after
)))
2084 (c-lang-defconst c-matchers-3
2085 t
(append (c-lang-const c-matchers-1
)
2086 (c-lang-const c-basic-matchers-before
)
2087 (c-lang-const c-complex-decl-matchers
)
2088 (c-lang-const c-basic-matchers-after
)))
2090 (defun c-compose-keywords-list (base-list)
2091 ;; Incorporate the font lock keyword lists according to
2092 ;; `c-doc-comment-style' on the given keyword list and return it.
2093 ;; This is used in the function bindings of the
2094 ;; `*-font-lock-keywords-*' symbols since we have to build the list
2095 ;; when font-lock is initialized.
2097 (unless (memq c-doc-face-name c-literal-faces
)
2098 (setq c-literal-faces
(cons c-doc-face-name c-literal-faces
)))
2100 (let* ((doc-keywords
2101 (if (consp (car-safe c-doc-comment-style
))
2102 (cdr-safe (or (assq c-buffer-is-cc-mode c-doc-comment-style
)
2103 (assq 'other c-doc-comment-style
)))
2104 c-doc-comment-style
))
2105 (list (nconc (apply 'nconc
2109 (concat (symbol-name doc-style
)
2110 "-font-lock-keywords"))))
2111 (cond ((fboundp sym
)
2114 (append (eval sym
) nil
)))))
2115 (if (listp doc-keywords
)
2117 (list doc-keywords
))))
2120 ;; Kludge: If `c-font-lock-complex-decl-prepare' is on the list we
2121 ;; move it first since the doc comment font lockers might add
2122 ;; `c-type' text properties, so they have to be cleared before that.
2123 (when (memq 'c-font-lock-complex-decl-prepare list
)
2124 (setq list
(cons 'c-font-lock-complex-decl-prepare
2125 (delq 'c-font-lock-complex-decl-prepare
2126 (append list nil
)))))
2130 (defun c-override-default-keywords (def-var)
2131 ;; This is used to override the value on a `*-font-lock-keywords'
2132 ;; variable only if it's nil or has the same value as one of the
2133 ;; `*-font-lock-keywords-*' variables. Older font-lock packages
2134 ;; define a default value for `*-font-lock-keywords' which we want
2135 ;; to override, but we should otoh avoid clobbering a user setting.
2136 ;; This heuristic for that isn't perfect, but I can't think of any
2139 ;; This function does not do any hidden buffer changes.
2140 (when (and (boundp def-var
)
2141 (memq (symbol-value def-var
)
2145 (let ((sym (intern (concat (symbol-name def-var
)
2147 (and (boundp sym
) (symbol-value sym
))))
2148 '("-1" "-2" "-3")))))
2149 ;; The overriding is done by unbinding the variable so that the normal
2150 ;; defvar will install its default value later on.
2151 (makunbound def-var
)))
2156 (c-override-default-keywords 'c-font-lock-keywords
)
2158 (defconst c-font-lock-keywords-1
(c-lang-const c-matchers-1 c
)
2159 "Minimal font locking for C mode.
2160 Fontifies only preprocessor directives (in addition to the syntactic
2161 fontification of strings and comments).")
2163 (defconst c-font-lock-keywords-2
(c-lang-const c-matchers-2 c
)
2164 "Fast normal font locking for C mode.
2165 In addition to `c-font-lock-keywords-1', this adds fontification of
2166 keywords, simple types, declarations that are easy to recognize, the
2167 user defined types on `c-font-lock-extra-types', and the doc comment
2168 styles specified by `c-doc-comment-style'.")
2170 (defconst c-font-lock-keywords-3
(c-lang-const c-matchers-3 c
)
2171 "Accurate normal font locking for C mode.
2172 Like `c-font-lock-keywords-2' but detects declarations in a more
2173 accurate way that works in most cases for arbitrary types without the
2174 need for `c-font-lock-extra-types'.")
2176 (defvar c-font-lock-keywords c-font-lock-keywords-3
2177 "Default expressions to highlight in C mode.")
2179 (defun c-font-lock-keywords-2 ()
2180 (c-compose-keywords-list c-font-lock-keywords-2
))
2181 (defun c-font-lock-keywords-3 ()
2182 (c-compose-keywords-list c-font-lock-keywords-3
))
2183 (defun c-font-lock-keywords ()
2184 (c-compose-keywords-list c-font-lock-keywords
))
2189 (defun c-font-lock-c++-new
(limit)
2190 ;; Assuming point is after a "new" word, check that it isn't inside
2191 ;; a string or comment, and if so try to fontify the type in the
2192 ;; allocation expression. Nil is always returned.
2194 ;; As usual, C++ takes the prize in coming up with a hard to parse
2197 (unless (c-skip-comments-and-strings limit
)
2200 ;; A "new" keyword is followed by one to three expressions, where
2201 ;; the type is the middle one, and the only required part.
2202 (let (expr1-pos expr2-pos
2203 ;; Enable recording of identifier ranges in `c-forward-type'
2204 ;; etc for later fontification. Not using
2205 ;; `c-fontify-types-and-refs' here since the ranges should
2206 ;; be fontified selectively only when an allocation
2207 ;; expression is successfully recognized.
2208 (c-record-type-identifiers t
)
2209 c-record-ref-identifiers
2210 ;; The font-lock package in Emacs is known to clobber
2211 ;; `parse-sexp-lookup-properties' (when it exists).
2212 (parse-sexp-lookup-properties
2213 (cc-eval-when-compile
2214 (boundp 'parse-sexp-lookup-properties
))))
2215 (c-forward-syntactic-ws)
2217 ;; The first placement arglist is always parenthesized, if it
2219 (when (eq (char-after) ?\
()
2220 (setq expr1-pos
(1+ (point)))
2223 (scan-error (throw 'false-alarm t
)))
2224 (c-forward-syntactic-ws))
2226 ;; The second expression is either a type followed by some "*" or
2227 ;; "[...]" or similar, or a parenthesized type followed by a full
2228 ;; identifierless declarator.
2229 (setq expr2-pos
(1+ (point)))
2230 (cond ((eq (char-after) ?\
())
2231 ((let ((c-promote-possible-types t
))
2233 (t (setq expr2-pos nil
)))
2238 ;; No second expression, so the first has to be a
2239 ;; parenthesized type.
2240 (goto-char expr1-pos
)
2241 (let ((c-promote-possible-types t
))
2244 ((eq (char-before expr2-pos
) ?\
()
2245 ;; Got two parenthesized expressions, so we have to look
2246 ;; closer at them to decide which is the type. No need to
2247 ;; handle `c-record-ref-identifiers' since all references
2248 ;; has already been handled by other fontification rules.
2249 (let (expr1-res expr2-res
)
2251 (goto-char expr1-pos
)
2252 (when (setq expr1-res
(c-forward-type))
2254 (cc-eval-when-compile
2255 (concat (c-lang-const c-symbol-start c
++)
2257 ;; There's something after the would-be type that
2258 ;; can't be there, so this is a placement arglist.
2259 (setq expr1-res nil
)))
2261 (goto-char expr2-pos
)
2262 (when (setq expr2-res
(c-forward-type))
2264 (cc-eval-when-compile
2265 (concat (c-lang-const c-symbol-start c
++)
2267 ;; There's something after the would-be type that can't
2268 ;; be there, so this is an initialization expression.
2269 (setq expr2-res nil
))
2270 (when (and (c-go-up-list-forward)
2271 (progn (c-forward-syntactic-ws)
2272 (eq (char-after) ?\
()))
2273 ;; If there's a third initialization expression
2274 ;; then the second one is the type, so demote the
2276 (setq expr1-res nil
)))
2278 ;; We fontify the most likely type, with a preference for
2279 ;; the first argument since a placement arglist is more
2280 ;; unusual than an initializer.
2281 (cond ((memq expr1-res
'(t known prefix
)))
2282 ((memq expr2-res
'(t known prefix
)))
2283 ((eq expr1-res
'found
)
2284 (let ((c-promote-possible-types t
))
2285 (goto-char expr1-pos
)
2287 ((eq expr2-res
'found
)
2288 (let ((c-promote-possible-types t
))
2289 (goto-char expr2-pos
)
2291 ((and (eq expr1-res
'maybe
) (not expr2-res
))
2292 (let ((c-promote-possible-types t
))
2293 (goto-char expr1-pos
)
2295 ((and (not expr1-res
) (eq expr2-res
'maybe
))
2296 (let ((c-promote-possible-types t
))
2297 (goto-char expr2-pos
)
2299 ;; If both type matches are 'maybe then we're
2300 ;; too uncertain to promote either of them.
2303 ;; Fontify the type that now is recorded in
2304 ;; `c-record-type-identifiers', if any.
2305 (c-fontify-recorded-types-and-refs)))))
2308 (c-override-default-keywords 'c
++-font-lock-keywords
)
2310 (defconst c
++-font-lock-keywords-1
(c-lang-const c-matchers-1 c
++)
2311 "Minimal font locking for C++ mode.
2312 Fontifies only preprocessor directives (in addition to the syntactic
2313 fontification of strings and comments).")
2315 (defconst c
++-font-lock-keywords-2
(c-lang-const c-matchers-2 c
++)
2316 "Fast normal font locking for C++ mode.
2317 In addition to `c++-font-lock-keywords-1', this adds fontification of
2318 keywords, simple types, declarations that are easy to recognize, the
2319 user defined types on `c++-font-lock-extra-types', and the doc comment
2320 styles specified by `c-doc-comment-style'.")
2322 (defconst c
++-font-lock-keywords-3
(c-lang-const c-matchers-3 c
++)
2323 "Accurate normal font locking for C++ mode.
2324 Like `c++-font-lock-keywords-2' but detects declarations in a more
2325 accurate way that works in most cases for arbitrary types without the
2326 need for `c++-font-lock-extra-types'.")
2328 (defvar c
++-font-lock-keywords c
++-font-lock-keywords-3
2329 "Default expressions to highlight in C++ mode.")
2331 (defun c++-font-lock-keywords-2
()
2332 (c-compose-keywords-list c
++-font-lock-keywords-2
))
2333 (defun c++-font-lock-keywords-3
()
2334 (c-compose-keywords-list c
++-font-lock-keywords-3
))
2335 (defun c++-font-lock-keywords
()
2336 (c-compose-keywords-list c
++-font-lock-keywords
))
2341 (defun c-font-lock-objc-iip-decl ()
2342 ;; Assuming the point is after an "@interface", "@implementation",
2343 ;; "@protocol" declaration, fontify all the types in the directive.
2344 ;; Return t if the directive was fully recognized. Point will then
2345 ;; be at the end of it.
2347 (c-fontify-types-and-refs
2349 (c-promote-possible-types t
)
2350 ;; Turn off recognition of angle bracket arglists while parsing
2351 ;; types here since the protocol reference list might then be
2352 ;; considered part of the preceding name or superclass-name.
2353 c-recognize-
<>-arglists
)
2356 ;; Handle the name of the class itself.
2357 (c-forward-syntactic-ws)
2358 (unless (c-forward-type) (throw 'break nil
))
2360 ;; Look for ": superclass-name" or "( category-name )".
2361 (when (looking-at "[:\(]")
2362 (setq start-char
(char-after))
2364 (c-forward-syntactic-ws)
2365 (unless (c-forward-type) (throw 'break nil
))
2366 (when (eq start-char ?\
()
2367 (unless (eq (char-after) ?\
)) (throw 'break nil
))
2369 (c-forward-syntactic-ws)))
2371 ;; Look for a protocol reference list.
2372 (when (if (eq (char-after) ?
<)
2374 (setq c-recognize-
<>-arglists t
)
2375 (c-forward-<>-arglist t t
))
2377 (c-put-char-property (1- (point)) 'c-type
'c-decl-end
)
2380 (defun c-font-lock-objc-method ()
2381 ;; Assuming the point is after the + or - that starts an Objective-C
2382 ;; method declaration, fontify it. This must be done before normal
2383 ;; casts, declarations and labels are fontified since they will get
2384 ;; false matches in these things.
2386 (c-fontify-types-and-refs
2388 (c-promote-possible-types t
))
2392 (c-forward-syntactic-ws)
2394 ;; An optional method type.
2395 (if (eq (char-after) ?\
()
2398 (c-forward-syntactic-ws)
2400 (prog1 (c-go-up-list-forward)
2401 (c-forward-syntactic-ws)))
2404 ;; The name. The first time it's the first part of
2405 ;; the function name, the rest of the time it's an
2407 (looking-at c-symbol-key
)
2409 (goto-char (match-end 0))
2410 (c-put-font-lock-face (match-beginning 0)
2413 'font-lock-function-name-face
2414 'font-lock-variable-name-face
))
2415 (c-forward-syntactic-ws)
2417 ;; Another optional part of the function name.
2418 (when (looking-at c-symbol-key
)
2419 (goto-char (match-end 0))
2420 (c-put-font-lock-face (match-beginning 0)
2422 'font-lock-function-name-face
)
2423 (c-forward-syntactic-ws))
2425 ;; There's another argument if a colon follows.
2426 (eq (char-after) ?
:)))
2430 (defun c-font-lock-objc-methods (limit)
2431 ;; Fontify method declarations in Objective-C. Nil is always
2434 (let (;; The font-lock package in Emacs is known to clobber
2435 ;; `parse-sexp-lookup-properties' (when it exists).
2436 (parse-sexp-lookup-properties
2437 (cc-eval-when-compile
2438 (boundp 'parse-sexp-lookup-properties
))))
2444 (lambda (match-pos inside-macro
)
2446 (c-font-lock-objc-method))))
2449 (c-override-default-keywords 'objc-font-lock-keywords
)
2451 (defconst objc-font-lock-keywords-1
(c-lang-const c-matchers-1 objc
)
2452 "Minimal font locking for Objective-C mode.
2453 Fontifies only compiler directives (in addition to the syntactic
2454 fontification of strings and comments).")
2456 (defconst objc-font-lock-keywords-2
(c-lang-const c-matchers-2 objc
)
2457 "Fast normal font locking for Objective-C mode.
2458 In addition to `objc-font-lock-keywords-1', this adds fontification of
2459 keywords, simple types, declarations that are easy to recognize, the
2460 user defined types on `objc-font-lock-extra-types', and the doc
2461 comment styles specified by `c-doc-comment-style'.")
2463 (defconst objc-font-lock-keywords-3
(c-lang-const c-matchers-3 objc
)
2464 "Accurate normal font locking for Objective-C mode.
2465 Like `objc-font-lock-keywords-2' but detects declarations in a more
2466 accurate way that works in most cases for arbitrary types without the
2467 need for `objc-font-lock-extra-types'.")
2469 (defvar objc-font-lock-keywords objc-font-lock-keywords-3
2470 "Default expressions to highlight in Objective-C mode.")
2472 (defun objc-font-lock-keywords-2 ()
2473 (c-compose-keywords-list objc-font-lock-keywords-2
))
2474 (defun objc-font-lock-keywords-3 ()
2475 (c-compose-keywords-list objc-font-lock-keywords-3
))
2476 (defun objc-font-lock-keywords ()
2477 (c-compose-keywords-list objc-font-lock-keywords
))
2479 ;; Kludge to override the default value that
2480 ;; `objc-font-lock-extra-types' might have gotten from the font-lock
2481 ;; package. The value replaced here isn't relevant now anyway since
2482 ;; those types are builtin and therefore listed directly in
2483 ;; `c-primitive-type-kwds'.
2484 (when (equal (sort (append objc-font-lock-extra-types nil
) 'string-lessp
)
2485 '("BOOL" "Class" "IMP" "SEL"))
2486 (setq objc-font-lock-extra-types
2487 (cc-eval-when-compile (list (concat "[" c-upper
"]\\sw*")))))
2492 (c-override-default-keywords 'java-font-lock-keywords
)
2494 (defconst java-font-lock-keywords-1
(c-lang-const c-matchers-1 java
)
2495 "Minimal font locking for Java mode.
2496 Fontifies nothing except the syntactic fontification of strings and
2499 (defconst java-font-lock-keywords-2
(c-lang-const c-matchers-2 java
)
2500 "Fast normal font locking for Java mode.
2501 In addition to `java-font-lock-keywords-1', this adds fontification of
2502 keywords, simple types, declarations that are easy to recognize, the
2503 user defined types on `java-font-lock-extra-types', and the doc
2504 comment styles specified by `c-doc-comment-style'.")
2506 (defconst java-font-lock-keywords-3
(c-lang-const c-matchers-3 java
)
2507 "Accurate normal font locking for Java mode.
2508 Like `java-font-lock-keywords-2' but detects declarations in a more
2509 accurate way that works in most cases for arbitrary types without the
2510 need for `java-font-lock-extra-types'.")
2512 (defvar java-font-lock-keywords java-font-lock-keywords-3
2513 "Default expressions to highlight in Java mode.")
2515 (defun java-font-lock-keywords-2 ()
2516 (c-compose-keywords-list java-font-lock-keywords-2
))
2517 (defun java-font-lock-keywords-3 ()
2518 (c-compose-keywords-list java-font-lock-keywords-3
))
2519 (defun java-font-lock-keywords ()
2520 (c-compose-keywords-list java-font-lock-keywords
))
2525 (c-override-default-keywords 'idl-font-lock-keywords
)
2527 (defconst idl-font-lock-keywords-1
(c-lang-const c-matchers-1 idl
)
2528 "Minimal font locking for CORBA IDL mode.
2529 Fontifies nothing except the syntactic fontification of strings and
2532 (defconst idl-font-lock-keywords-2
(c-lang-const c-matchers-2 idl
)
2533 "Fast normal font locking for CORBA IDL mode.
2534 In addition to `idl-font-lock-keywords-1', this adds fontification of
2535 keywords, simple types, declarations that are easy to recognize, the
2536 user defined types on `idl-font-lock-extra-types', and the doc comment
2537 styles specified by `c-doc-comment-style'.")
2539 (defconst idl-font-lock-keywords-3
(c-lang-const c-matchers-3 idl
)
2540 "Accurate normal font locking for CORBA IDL mode.
2541 Like `idl-font-lock-keywords-2' but detects declarations in a more
2542 accurate way that works in most cases for arbitrary types without the
2543 need for `idl-font-lock-extra-types'.")
2545 (defvar idl-font-lock-keywords idl-font-lock-keywords-3
2546 "Default expressions to highlight in CORBA IDL mode.")
2548 (defun idl-font-lock-keywords-2 ()
2549 (c-compose-keywords-list idl-font-lock-keywords-2
))
2550 (defun idl-font-lock-keywords-3 ()
2551 (c-compose-keywords-list idl-font-lock-keywords-3
))
2552 (defun idl-font-lock-keywords ()
2553 (c-compose-keywords-list idl-font-lock-keywords
))
2558 (c-override-default-keywords 'pike-font-lock-keywords
)
2560 (defconst pike-font-lock-keywords-1
(c-lang-const c-matchers-1 pike
)
2561 "Minimal font locking for Pike mode.
2562 Fontifies only preprocessor directives (in addition to the syntactic
2563 fontification of strings and comments).")
2565 (defconst pike-font-lock-keywords-2
(c-lang-const c-matchers-2 pike
)
2566 "Fast normal font locking for Pike mode.
2567 In addition to `pike-font-lock-keywords-1', this adds fontification of
2568 keywords, simple types, declarations that are easy to recognize, the
2569 user defined types on `pike-font-lock-extra-types', and the doc
2570 comment styles specified by `c-doc-comment-style'.")
2572 (defconst pike-font-lock-keywords-3
(c-lang-const c-matchers-3 pike
)
2573 "Accurate normal font locking for Pike mode.
2574 Like `pike-font-lock-keywords-2' but detects declarations in a more
2575 accurate way that works in most cases for arbitrary types without the
2576 need for `pike-font-lock-extra-types'.")
2578 (defvar pike-font-lock-keywords pike-font-lock-keywords-3
2579 "Default expressions to highlight in Pike mode.")
2581 (defun pike-font-lock-keywords-2 ()
2582 (c-compose-keywords-list pike-font-lock-keywords-2
))
2583 (defun pike-font-lock-keywords-3 ()
2584 (c-compose-keywords-list pike-font-lock-keywords-3
))
2585 (defun pike-font-lock-keywords ()
2586 (c-compose-keywords-list pike-font-lock-keywords
))
2591 (defun c-font-lock-doc-comments (prefix limit keywords
)
2592 ;; Fontify the comments between the point and LIMIT whose start
2593 ;; matches PREFIX with `c-doc-face-name'. Assumes comments have been
2594 ;; fontified with `font-lock-comment-face' already. nil is always
2597 ;; After the fontification of a matching comment, fontification
2598 ;; according to KEYWORDS is applied inside it. It's a list like
2599 ;; `font-lock-keywords' except that anchored matches and eval
2600 ;; clauses aren't supported and that some abbreviated forms can't be
2601 ;; used. The buffer is narrowed to the comment while KEYWORDS is
2602 ;; applied; leading comment starters are included but trailing
2603 ;; comment enders for block comment are not.
2605 ;; Note that faces added through KEYWORDS should never replace the
2606 ;; existing `c-doc-face-name' face since the existence of that face
2607 ;; is used as a flag in other code to skip comments.
2609 (let (comment-beg region-beg
)
2610 (if (eq (get-text-property (point) 'face
)
2611 'font-lock-comment-face
)
2612 ;; Handle the case when the fontified region starts inside a
2614 (let ((range (c-literal-limits)))
2615 (setq region-beg
(point))
2617 (goto-char (car range
)))
2618 (when (looking-at prefix
)
2619 (setq comment-beg
(point)))))
2624 ;; Search for the prefix until a match is found at the start
2626 (while (when (re-search-forward prefix limit t
)
2627 (setq comment-beg
(match-beginning 0))
2628 (or (not (c-got-face-at comment-beg
2630 (and (/= comment-beg
(point-min))
2631 (c-got-face-at (1- comment-beg
)
2633 (setq comment-beg nil
))
2634 (setq region-beg comment-beg
))
2636 (if (eq (elt (parse-partial-sexp comment-beg
(+ comment-beg
2)) 7) t
)
2637 ;; Collect a sequence of doc style line comments.
2639 (goto-char comment-beg
)
2641 (c-forward-single-comment)
2642 (skip-syntax-forward " ")
2644 (looking-at prefix
))))
2645 (goto-char comment-beg
)
2646 (c-forward-single-comment))
2647 (if (> (point) limit
) (goto-char limit
))
2648 (setq comment-beg nil
)
2650 (let ((region-end (point))
2651 (keylist keywords
) keyword matcher highlights
)
2652 (c-put-font-lock-face region-beg region-end c-doc-face-name
)
2654 ;; Narrow to the doc comment. Among other things, this
2655 ;; helps by making "^" match at the start of the comment.
2656 ;; Do not include a trailing block comment ender, though.
2657 (and (> region-end
(1+ region-beg
))
2658 (progn (goto-char region-end
)
2660 (looking-at "\\*/"))
2661 (setq region-end
(point)))
2662 (narrow-to-region region-beg region-end
)
2665 (setq keyword
(car keylist
)
2666 keylist
(cdr keylist
)
2667 matcher
(car keyword
))
2668 (goto-char region-beg
)
2669 (while (if (stringp matcher
)
2670 (re-search-forward matcher region-end t
)
2671 (funcall matcher region-end
))
2672 (setq highlights
(cdr keyword
))
2673 (if (consp (car highlights
))
2675 (font-lock-apply-highlight (car highlights
))
2676 (setq highlights
(cdr highlights
)))
2677 (font-lock-apply-highlight highlights
))))
2679 (goto-char region-end
)))))
2681 (put 'c-font-lock-doc-comments
'lisp-indent-function
2)
2683 (defun c-find-invalid-doc-markup (regexp limit
)
2684 ;; Used to fontify invalid markup in doc comments after the correct
2685 ;; ones have been fontified: Find the first occurence of REGEXP
2686 ;; between the point and LIMIT that only is fontified with
2687 ;; `c-doc-face-name'. If a match is found then submatch 0 surrounds
2688 ;; the first char and t is returned, otherwise nil is returned.
2690 (while (if (re-search-forward regexp limit t
)
2691 (not (eq (get-text-property
2692 (setq start
(match-beginning 0)) 'face
)
2696 (store-match-data (list (copy-marker start
)
2697 (copy-marker (1+ start
))))
2700 (defconst javadoc-font-lock-doc-comments
2701 `(("{@[a-z]+[^}\n\r]*}" ; "{@foo ...}" markup.
2702 0 ,c-doc-markup-face-name prepend nil
)
2703 ("^\\(/\\*\\)?[ \t*]*\\(@[a-z]+\\)" ; "@foo ..." markup.
2704 2 ,c-doc-markup-face-name prepend nil
)
2705 (,(concat "</?\\sw" ; HTML tags.
2707 (concat "\\sw\\|\\s \\|[=\n\r*.:]\\|"
2708 "\"[^\"]*\"\\|'[^']*'")
2710 0 ,c-doc-markup-face-name prepend nil
)
2711 ("&\\(\\sw\\|[.:]\\)+;" ; HTML entities.
2712 0 ,c-doc-markup-face-name prepend nil
)
2713 ;; Fontify remaining markup characters as invalid. Note
2714 ;; that the Javadoc spec is hazy about when "@" is
2715 ;; allowed in non-markup use.
2717 (c-find-invalid-doc-markup "[<>&]\\|{@" limit
))
2718 0 ,c-invalid-face-name prepend nil
)))
2720 (defconst javadoc-font-lock-keywords
2722 (c-font-lock-doc-comments "/\\*\\*" limit
2723 javadoc-font-lock-doc-comments
)))))
2725 (defconst autodoc-decl-keywords
2726 ;; Adorned regexp matching the keywords that introduce declarations
2728 (cc-eval-when-compile
2729 (c-make-keywords-re t
'("@decl" "@elem" "@index" "@member") 'pike-mode
)))
2731 (defconst autodoc-decl-type-keywords
2732 ;; Adorned regexp matching the keywords that are followed by a type.
2733 (cc-eval-when-compile
2734 (c-make-keywords-re t
'("@elem" "@member") 'pike-mode
)))
2736 (defun autodoc-font-lock-line-markup (limit)
2737 ;; Fontify all line oriented keywords between the point and LIMIT.
2738 ;; Nil is always returned.
2740 (let ((line-re (concat "^\\(\\(/\\*!\\|\\s *\\("
2741 c-current-comment-prefix
2742 "\\)\\)\\s *\\)@[A-Za-z_-]+\\(\\s \\|$\\)"))
2743 (markup-faces (list c-doc-markup-face-name c-doc-face-name
)))
2745 (while (re-search-forward line-re limit t
)
2746 (goto-char (match-end 1))
2748 (if (looking-at autodoc-decl-keywords
)
2749 (let* ((kwd-pos (point))
2750 (start (match-end 1))
2754 (c-put-font-lock-face (point) pos markup-faces
)
2756 ;; Put a declaration end mark at the markup keyword and
2757 ;; remove the faces from the rest of the line so that it
2758 ;; gets refontified as a declaration later on by
2759 ;; `c-font-lock-declarations'.
2760 (c-put-char-property (1- pos
) 'c-type
'c-decl-end
)
2765 (and (eq (char-before) ?
@)
2767 (progn (forward-char)
2768 (skip-chars-forward " \t")
2769 (looking-at c-current-comment-prefix
))))
2770 (goto-char (match-end 0))
2771 (c-remove-font-lock-face pos
(1- end
))
2772 (c-put-font-lock-face (1- end
) end markup-faces
)
2775 ;; Include the final newline in the removed area. This
2776 ;; has no visual effect but it avoids some tricky special
2777 ;; cases in the testsuite wrt the differences in string
2778 ;; fontification in Emacs vs XEmacs.
2779 (c-remove-font-lock-face pos
(min (1+ (point)) (point-max)))
2781 ;; Must handle string literals explicitly inside the declaration.
2783 (while (re-search-forward
2784 "\"\\([^\\\"]\\|\\\\.\\)*\"\\|'\\([^\\']\\|\\\\.\\)*'"
2786 (c-put-font-lock-string-face (match-beginning 0)
2789 ;; Fontify types after keywords that always are followed
2792 (when (looking-at autodoc-decl-type-keywords
)
2793 (c-fontify-types-and-refs ((c-promote-possible-types t
))
2795 (c-forward-syntactic-ws)
2798 ;; Mark each whole line as markup, as long as the logical line
2801 (c-put-font-lock-face (point)
2802 (progn (end-of-line) (point))
2804 (and (eq (char-before) ?
@)
2806 (progn (forward-char)
2807 (skip-chars-forward " \t")
2808 (looking-at c-current-comment-prefix
))))
2809 (goto-char (match-end 0))))))
2813 (defconst autodoc-font-lock-doc-comments
2814 `(("@\\(\\w+{\\|\\[\\([^\]@\n\r]\\|@@\\)*\\]\\|[@}]\\|$\\)"
2816 0 ,c-doc-markup-face-name prepend nil
)
2817 (autodoc-font-lock-line-markup)
2818 ;; Fontify remaining markup characters as invalid.
2820 (c-find-invalid-doc-markup "@" limit
))
2821 0 ,c-invalid-face-name prepend nil
)
2824 (defun autodoc-font-lock-keywords ()
2825 ;; Note that we depend on that `c-current-comment-prefix' has got
2826 ;; its proper value here.
2828 ;; The `c-type' text property with `c-decl-end' is used to mark the
2829 ;; end of the `autodoc-decl-keywords' occurrences to fontify the
2830 ;; following declarations.
2831 (setq c-type-decl-end-used t
)
2834 (c-font-lock-doc-comments "/[*/]!" limit
2835 autodoc-font-lock-doc-comments
)))))
2840 ;; Awk regexps written with help from Peter Galbraith
2841 ;; <galbraith@mixing.qc.dfo.ca>.
2842 ;; Take GNU Emacs's 'words out of the following regexp-opts. They dont work
2843 ;; in Xemacs 21.4.4. acm 2002/9/19.
2844 (eval-after-load "cc-awk" ; Evaluate while loading cc-fonts
2845 `(defconst awk-font-lock-keywords
; Evaluate after loading cc-awk
2846 ',(eval-when-compile ; Evaluate while compiling cc-fonts
2849 '("^[ \t]*\\(func\\(tion\\)?\\)\\>[ \t]*\\(\\sw+\\)?"
2850 (1 font-lock-keyword-face
) (3 font-lock-function-name-face nil t
))
2856 '("ARGC" "ARGIND" "ARGV" "BINMODE" "CONVFMT" "ENVIRON"
2857 "ERRNO" "FIELDWIDTHS" "FILENAME" "FNR" "FS" "IGNORECASE"
2858 "LINT" "NF" "NR" "OFMT" "OFS" "ORS" "PROCINFO" "RLENGTH"
2859 "RS" "RSTART" "RT" "SUBSEP" "TEXTDOMAIN") t
) "\\>")
2860 'font-lock-variable-name-face
)
2862 ;; Special file names. (acm, 2002/7/22)
2863 ;; The following regexp was created by first evaluating this in GNU Emacs 21.1:
2864 ;; (c-regexp-opt '("/dev/stdin" "/dev/stdout" "/dev/stderr" "/dev/fd/n" "/dev/pid"
2865 ;; "/dev/ppid" "/dev/pgrpid" "/dev/user") 'words)
2866 ;; , removing the "?:" from each "\\(?:" (for backward compatibility with older Emacsen)
2867 ;; , replacing the "n" in "dev/fd/n" with "[0-9]+"
2868 ;; , removing the unwanted \\< at the beginning, and finally filling out the
2869 ;; regexp so that a " must come before, and either a " or heuristic stuff after.
2870 ;; The surrounding quotes are fontified along with the filename, since, semantically,
2871 ;; they are an indivisible unit.
2872 '("\\(\"/dev/\\(fd/[0-9]+\\|p\\(\\(\\(gr\\)?p\\)?id\\)\\|\
2873 std\\(err\\|in\\|out\\)\\|user\\)\\)\\>\
2874 \\(\\(\"\\)\\|\\([^\"/\n\r][^\"\n\r]*\\)?$\\)"
2875 (1 font-lock-variable-name-face t
)
2876 (8 font-lock-variable-name-face t t
))
2877 ;; Do the same (almost) with
2878 ;; (c-regexp-opt '("/inet/tcp/lport/rhost/rport" "/inet/udp/lport/rhost/rport"
2879 ;; "/inet/raw/lport/rhost/rport") 'words)
2880 ;; This cannot be combined with the above pattern, because the match number
2881 ;; for the (optional) closing \" would then exceed 9.
2882 '("\\(\"/inet/\\(\\(raw\\|\\(tc\\|ud\\)p\\)/lport/rhost/rport\\)\\)\\>\
2883 \\(\\(\"\\)\\|\\([^\"/\n\r][^\"\n\r]*\\)?$\\)"
2884 (1 font-lock-variable-name-face t
)
2885 (6 font-lock-variable-name-face t t
))
2890 '("BEGIN" "END" "break" "continue" "delete" "do" "else"
2891 "exit" "for" "getline" "if" "in" "next" "nextfile"
2900 '("adump" "and" "asort" "atan2" "bindtextdomain" "close"
2901 "compl" "cos" "dcgettext" "exp" "extension" "fflush"
2902 "gensub" "gsub" "index" "int" "length" "log" "lshift"
2903 "match" "mktime" "or" "print" "printf" "rand" "rshift"
2904 "sin" "split" "sprintf" "sqrt" "srand" "stopme"
2905 "strftime" "strtonum" "sub" "substr" "system"
2906 "systime" "tolower" "toupper" "xor") t
)
2908 0 c-preprocessor-face-name
))
2910 ;; gawk debugging keywords. (acm, 2002/7/21)
2911 ;; (Removed, 2003/6/6. These functions are now fontified as built-ins)
2912 ;; (list (concat "\\<" (c-regexp-opt '("adump" "stopme") t) "\\>")
2913 ;; 0 'font-lock-warning-face)
2915 ;; User defined functions with an apparent spurious space before the
2916 ;; opening parenthesis. acm, 2002/5/30.
2917 `(,(concat "\\(\\w\\|_\\)" c-awk-escaped-nls
* "[ \t]"
2918 c-awk-escaped-nls
*-with-space
* "(")
2919 (0 'font-lock-warning-face
))
2921 ;; Space after \ in what looks like an escaped newline. 2002/5/31
2922 '("\\\\[ \t]+$" 0 font-lock-warning-face t
)
2924 ;; Unbalanced string (") or regexp (/) delimiters. 2002/02/16.
2925 '("\\s|" 0 font-lock-warning-face t nil
)
2926 ;; gawk 3.1 localizable strings ( _"translate me!"). 2002/5/21
2927 '("\\(_\\)\\s|" 1 font-lock-warning-face
)
2928 '("\\(_\\)\\s\"" 1 font-lock-string-face
) ; FIXME! not for XEmacs. 2002/10/6
2930 "Default expressions to highlight in AWK mode."))
2933 (cc-provide 'cc-fonts
)
2935 ;;; arch-tag: 2f65f405-735f-4da5-8d4b-b957844c5203
2936 ;;; cc-fonts.el ends here