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 (defface c-invalid-face
197 '((((class color) (background light)) (:foreground "red1"))
198 (((class color)) (:foreground "hotpink"))
199 (t (:inverse-video t)))
200 "Face used to highlight invalid syntax."
203 ;; To make hard spaces visible an inverted version of
204 ;; `c-invalid-face-name' is used. Since font-lock in Emacs expands
205 ;; all face names in `font-lock-keywords' as variables we need to have
206 ;; a variable for it that resolves to its own name.
207 (defconst c-nonbreakable-space-face 'c-nonbreakable-space-face)
209 (cc-bytecomp-defun face-inverse-video-p) ; Only in Emacs.
210 (cc-bytecomp-defun face-property-instance) ; Only in XEmacs.
212 (defun c-make-inverse-face (oldface newface)
213 ;; Emacs and XEmacs have completely different face manipulation
216 ;; This function does not do any hidden buffer changes
217 (copy-face oldface newface)
218 (cond ((fboundp 'face-inverse-video-p)
219 ;; Emacs 20 and later. This only looks at the inverse flag
220 ;; in the current frame. Other display configurations might
221 ;; be different, but it can only show if the same Emacs has
222 ;; frames on e.g. a color and a monochrome display
224 (unless (face-inverse-video-p oldface)
225 (invert-face newface)))
226 ((fboundp 'face-property-instance)
227 ;; XEmacs. Same pitfall here.
228 (unless (face-property-instance oldface 'reverse)
229 (invert-face newface)))
231 ;; Emacs 19 has no inverse flag at all. Just inverse the
232 ;; face and hope it wasn't inversed already.
233 (invert-face newface))))
236 ;; We need the following functions during compilation since they're
237 ;; called when the `c-lang-defconst' initializers are evaluated.
238 ;; Define them at runtime too for the sake of derived modes.
240 (defmacro c-put-font-lock-face (from to face)
241 ;; Put a face on a region (overriding any existing face) in the way
242 ;; font-lock would do it. In XEmacs that means putting an
243 ;; additional font-lock property, or else the font-lock package
244 ;; won't recognize it as fontified and might override it
246 (if (fboundp 'font-lock-set-face)
247 ;; Note: This function has no docstring in XEmacs so it might be
248 ;; considered internal.
249 `(font-lock-set-face ,from ,to ,face)
250 `(put-text-property ,from ,to 'face ,face)))
252 (defmacro c-remove-font-lock-face (from to)
253 ;; This is the inverse of `c-put-font-lock-face'.
254 (if (fboundp 'font-lock-remove-face)
255 `(font-lock-remove-face ,from ,to)
256 `(remove-text-properties ,from ,to '(face nil))))
258 (defmacro c-put-font-lock-string-face (from to)
259 ;; Put `font-lock-string-face' on a string. The surrounding
260 ;; quotes are included in Emacs but not in XEmacs. The passed
261 ;; region should include them.
262 (if (featurep 'xemacs)
263 `(c-put-font-lock-face (1+ ,from) (1- ,to) 'font-lock-string-face)
264 `(c-put-font-lock-face ,from ,to 'font-lock-string-face)))
266 (defmacro c-fontify-types-and-refs (varlist &rest body)
267 ;; Like `let', but additionally activates `c-record-type-identifiers'
268 ;; and `c-record-ref-identifiers', and fontifies the recorded ranges
269 ;; accordingly on exit.
270 `(let ((c-record-type-identifiers t)
271 c-record-ref-identifiers
273 (prog1 (progn ,@body)
274 (c-fontify-recorded-types-and-refs))))
275 (put 'c-fontify-types-and-refs 'lisp-indent-function 1)
276 (eval-after-load "edebug" '(def-edebug-spec c-fontify-types-and-refs let*))
278 (defun c-skip-comments-and-strings (limit)
279 ;; If the point is within a region fontified as a comment or
280 ;; string literal skip to the end of it or to LIMIT, whichever
281 ;; comes first, and return t. Otherwise return nil. The match
282 ;; data is not clobbered.
283 (when (c-got-face-at (point) c-literal-faces)
285 (goto-char (next-single-property-change
286 (point) 'face nil limit))
287 (and (< (point) limit)
288 (c-got-face-at (point) c-literal-faces))))
291 (defun c-make-font-lock-search-function (regexp &rest highlights)
292 ;; This function makes a byte compiled function that works much like
293 ;; a matcher element in `font-lock-keywords'. It cuts out a little
294 ;; bit of the overhead compared to a real matcher. The main reason
295 ;; is however to pass the real search limit to the anchored
296 ;; matcher(s), since most (if not all) font-lock implementations
297 ;; arbitrarily limits anchored matchers to the same line, and also
298 ;; to insulate against various other irritating differences between
299 ;; the different (X)Emacs font-lock packages.
301 ;; REGEXP is the matcher, which must be a regexp. Only matches
302 ;; where the beginning is outside any comment or string literal are
305 ;; HIGHLIGHTS is a list of highlight specs, just like in
306 ;; `font-lock-keywords', with these limitations: The face is always
307 ;; overridden (no big disadvantage, since hits in comments etc are
308 ;; filtered anyway), there is no "laxmatch", and an anchored matcher
309 ;; is always a form which must do all the fontification directly.
310 ;; `limit' is a variable bound to the real limit in the context of
311 ;; the anchored matcher forms.
313 ;; This function does not do any hidden buffer changes, but the
314 ;; generated functions will. They are however used in places
315 ;; covered by the font-lock context.
317 ;; Note: Replace `byte-compile' with `eval' to debug the generated
321 (let (-match-end-pos-
322 ;; The font-lock package in Emacs is known to clobber
323 ;; `parse-sexp-lookup-properties' (when it exists).
324 (parse-sexp-lookup-properties
325 (cc-eval-when-compile
326 (boundp 'parse-sexp-lookup-properties))))
327 (while (re-search-forward ,regexp limit t)
328 (setq -match-end-pos- (point))
330 (goto-char (match-beginning 0))
331 (c-skip-comments-and-strings limit))
332 (goto-char -match-end-pos-)
335 (if (integerp (car highlight))
337 (unless (nth 2 highlight)
339 "The override flag must currently be set in %s"
341 (when (nth 3 highlight)
343 "The laxmatch flag may currently not be set in %s"
346 (c-put-font-lock-face
347 (match-beginning ,(car highlight))
348 (match-end ,(car highlight))
349 ,(elt highlight 1))))
350 (when (nth 3 highlight)
351 (error "Match highlights currently not supported in %s"
355 (save-match-data ,(car highlight))
356 ,(nth 2 highlight))))
360 (defun c-fontify-recorded-types-and-refs ()
361 ;; Converts the ranges recorded on `c-record-type-identifiers' and
362 ;; `c-record-ref-identifiers' to fontification.
364 (while (consp c-record-type-identifiers)
365 (setq elem (car c-record-type-identifiers)
366 c-record-type-identifiers (cdr c-record-type-identifiers))
367 (c-put-font-lock-face (car elem) (cdr elem)
368 'font-lock-type-face))
369 (while c-record-ref-identifiers
370 (setq elem (car c-record-ref-identifiers)
371 c-record-ref-identifiers (cdr c-record-ref-identifiers))
372 ;; Note that the reference face is a variable that is
373 ;; dereferenced, since it's an alias in Emacs.
374 (c-put-font-lock-face (car elem) (cdr elem)
375 c-reference-face-name))))
377 (c-lang-defconst c-cpp-matchers
378 "Font lock matchers for preprocessor directives and purely lexical
379 stuff. Used on level 1 and higher."
381 ;; Note: `c-font-lock-declarations' assumes that no matcher here
382 ;; sets `font-lock-type-face' in languages where
383 ;; `c-recognize-<>-arglists' is set.
385 t `(,@(when (c-lang-const c-opt-cpp-prefix)
386 (let* ((noncontinued-line-end "\\(\\=\\|\\(\\=\\|[^\\]\\)[\n\r]\\)")
387 (ncle-depth (c-regexp-opt-depth noncontinued-line-end))
388 (sws-depth (c-lang-const c-syntactic-ws-depth)))
389 `(;; The stuff after #error and #warning is a message, so
390 ;; fontify it as a string.
391 (,(concat noncontinued-line-end
392 (c-lang-const c-opt-cpp-prefix)
393 "\\(error\\|warning\\)\\>\\s *\\(.*\\)$")
394 ,(+ ncle-depth 2) font-lock-string-face)
396 ;; Fontify filenames in #include <...> as strings.
397 (,(concat noncontinued-line-end
398 (c-lang-const c-opt-cpp-prefix)
399 "\\(import\\|include\\)\\>"
400 (c-lang-const c-syntactic-ws)
401 "\\(<[^>\n\r]*>?\\)")
402 (,(+ ncle-depth sws-depth 2)
403 font-lock-string-face)
405 ;; Use an anchored matcher to put paren syntax on the brackets.
409 (match-beginning ,(+ ncle-depth sws-depth 2)))
411 (1- (match-end ,(+ ncle-depth sws-depth 2)))))
412 (if (eq (char-after end-pos) ?>)
414 (c-mark-<-as-paren beg-pos)
415 (c-mark->-as-paren end-pos))
416 (c-clear-char-property beg-pos 'syntax-table)))
420 (,(c-make-font-lock-search-function
422 noncontinued-line-end
423 (c-lang-const c-opt-cpp-prefix)
425 (c-lang-const c-syntactic-ws)
426 "\\(" (c-lang-const c-symbol-key) "\\)" ; 1 + ncle + sws
427 (concat "\\(" ; 2 + ncle + sws + c-sym-key
428 ;; Macro with arguments - a "function".
429 "\\(\(\\)" ; 3 + ncle + sws + c-sym-key
431 ;; Macro without arguments - a "variable".
434 `((if (match-beginning ,(+ 3 ncle-depth sws-depth
435 (c-lang-const c-symbol-key-depth)))
436 ;; "Function". Fontify the name and the arguments.
438 (c-put-font-lock-face
439 (match-beginning ,(+ 1 ncle-depth sws-depth))
440 (match-end ,(+ 1 ncle-depth sws-depth))
441 'font-lock-function-name-face)
442 (goto-char (match-end
443 ,(+ 3 ncle-depth sws-depth
444 (c-lang-const c-symbol-key-depth))))
446 (narrow-to-region (point-min) limit)
449 (c-forward-syntactic-ws)
450 (looking-at c-symbol-key))
452 (c-put-font-lock-face
453 (match-beginning 0) (match-end 0)
454 'font-lock-variable-name-face)
455 (goto-char (match-end 0))
456 (c-forward-syntactic-ws)
457 (eq (char-after) ?,)))
461 (c-put-font-lock-face
462 (match-beginning ,(+ 1 ncle-depth sws-depth))
463 (match-end ,(+ 1 ncle-depth sws-depth))
464 'font-lock-variable-name-face)))))
466 ;; Fontify cpp function names in preprocessor
467 ;; expressions in #if and #elif.
468 ,(when (c-lang-const c-cpp-defined-fns)
469 `(,(c-make-font-lock-search-function
470 (concat noncontinued-line-end
471 (c-lang-const c-opt-cpp-prefix)
472 "\\(if\\|elif\\)\\>" ; 1 + ncle-depth
473 ;; Match the whole logical line to look
474 ;; for the functions in.
475 "\\(\\\\\\(.\\|[\n\r]\\)\\|[^\n\r]\\)*")
476 `((let ((limit (match-end 0)))
477 (while (re-search-forward
480 (c-lang-const c-cpp-defined-fns)
485 (c-put-font-lock-face (match-beginning 1)
487 c-preprocessor-face-name)))
488 (goto-char (match-end ,(1+ ncle-depth)))))))
490 ;; Fontify the directive names.
491 (,(c-make-font-lock-search-function
492 (concat noncontinued-line-end
494 (c-lang-const c-opt-cpp-prefix)
495 "[" (c-lang-const c-symbol-chars) "]+"
497 `(,(1+ ncle-depth) c-preprocessor-face-name t)))
499 ;; fontify the n in ifndef
500 (,(concat noncontinued-line-end
501 (c-lang-const c-opt-cpp-prefix)
503 ,(+ ncle-depth 1) font-lock-negation-char-face prepend)
506 ,@(when (c-major-mode-is 'pike-mode)
507 `((eval . (list "\\`#![^\n\r]*"
508 0 c-preprocessor-face-name))))
510 ;; Make hard spaces visible through an inverted `c-invalid-face-name'.
514 (unless (c-face-name-p 'c-nonbreakable-space-face)
515 (c-make-inverse-face c-invalid-face-name
516 'c-nonbreakable-space-face))
517 'c-nonbreakable-space-face)))
520 (defun c-font-lock-invalid-string ()
521 ;; Assuming the point is after the opening character of a string,
522 ;; fontify that char with `c-invalid-face-name' if the string
523 ;; decidedly isn't terminated properly.
524 (let ((start (1- (point))))
526 (and (nth 3 (parse-partial-sexp start (c-point 'eol)))
527 (if (c-major-mode-is '(c-mode c++-mode objc-mode pike-mode))
528 ;; There's no \ before the newline.
529 (not (eq (char-before (point)) ?\\))
530 ;; Quoted newlines aren't supported.
532 (if (c-major-mode-is 'pike-mode)
533 ;; There's no # before the string, so newlines
535 (not (eq (char-before start) ?#))
537 (c-put-font-lock-face start (1+ start) c-invalid-face-name)))))
539 (c-lang-defconst c-basic-matchers-before
540 "Font lock matchers for basic keywords, labels, references and various
541 other easily recognizable things that should be fontified before generic
542 casts and declarations are fontified. Used on level 2 and higher."
544 ;; Note: `c-font-lock-declarations' assumes that no matcher here
545 ;; sets `font-lock-type-face' in languages where
546 ;; `c-recognize-<>-arglists' is set.
548 t `(;; Put a warning face on the opener of unclosed strings that
549 ;; can't span lines. Later font
550 ;; lock packages have a `font-lock-syntactic-face-function' for
551 ;; this, but it doesn't give the control we want since any
552 ;; fontification done inside the function will be
553 ;; unconditionally overridden.
554 ,(c-make-font-lock-search-function
555 ;; Match a char before the string starter to make
556 ;; `c-skip-comments-and-strings' work correctly.
557 (concat ".\\(" c-string-limit-regexp "\\)")
558 '((c-font-lock-invalid-string)))
560 ;; Fontify keyword constants.
561 ,@(when (c-lang-const c-constant-kwds)
562 (let ((re (c-make-keywords-re nil (c-lang-const c-constant-kwds))))
563 (if (c-major-mode-is 'pike-mode)
564 ;; No symbol is a keyword after "->" in Pike.
565 `((eval . (list ,(concat "\\(\\=\\|\\(\\=\\|[^-]\\)[^>]\\)"
566 "\\<\\(" re "\\)\\>")
567 3 c-constant-face-name)))
568 `((eval . (list ,(concat "\\<\\(" re "\\)\\>")
569 1 c-constant-face-name))))))
571 ;; Fontify all keywords except the primitive types.
572 ,(if (c-major-mode-is 'pike-mode)
573 ;; No symbol is a keyword after "->" in Pike.
574 `(,(concat "\\(\\=\\|\\(\\=\\|[^-]\\)[^>]\\)"
575 "\\<" (c-lang-const c-regular-keywords-regexp))
576 3 font-lock-keyword-face)
577 `(,(concat "\\<" (c-lang-const c-regular-keywords-regexp))
578 1 font-lock-keyword-face))
580 ;; Fontify leading identifiers in fully qualified names like
581 ;; "foo::bar" in languages that supports such things.
582 ,@(when (c-lang-const c-opt-identifier-concat-key)
583 (if (c-major-mode-is 'java-mode)
584 ;; Java needs special treatment since "." is used both to
585 ;; qualify names and in normal indexing. Here we look for
586 ;; capital characters at the beginning of an identifier to
587 ;; recognize the class. "*" is also recognized to cover
588 ;; wildcard import declarations. All preceding dot separated
589 ;; identifiers are taken as package names and therefore
590 ;; fontified as references.
591 `(,(c-make-font-lock-search-function
592 ;; Search for class identifiers preceded by ".". The
593 ;; anchored matcher takes it from there.
594 (concat (c-lang-const c-opt-identifier-concat-key)
597 "[" c-upper "][" (c-lang-const c-symbol-chars) "]*"
602 (goto-char (1+ (match-beginning 0)))
603 (while (and (eq (char-before) ?.)
606 (c-backward-syntactic-ws)
607 (setq id-end (point))
608 (< (skip-chars-backward
609 ,(c-lang-const c-symbol-chars)) 0))
610 (not (get-text-property (point) 'face)))
611 (c-put-font-lock-face (point) id-end c-reference-face-name)
612 (c-backward-syntactic-ws)))
614 (goto-char (match-end 0)))))
617 ;; Must use a function here since we match longer than we
618 ;; want to move before doing a new search. This is not
619 ;; necessary for XEmacs >= 20 since it restarts the search
620 ;; from the end of the first highlighted submatch (something
621 ;; that causes problems in other places).
623 (while (re-search-forward
624 ,(concat "\\(\\<" ; 1
625 "\\(" (c-lang-const c-symbol-key) "\\)" ; 2
627 (c-lang-const c-opt-identifier-concat-key)
631 (c-lang-const c-opt-after-id-concat-key)
635 (goto-char (match-beginning 0))
636 (c-skip-comments-and-strings limit))
637 (or (get-text-property (match-beginning 2) 'face)
638 (c-put-font-lock-face (match-beginning 2)
640 c-reference-face-name))
641 (goto-char (match-end 1))))))))))
643 ;; Fontify the special declarations in Objective-C.
644 ,@(when (c-major-mode-is 'objc-mode)
645 `(;; Fontify class names in the beginning of message expressions.
646 ,(c-make-font-lock-search-function
648 '((c-fontify-types-and-refs ()
649 (c-forward-syntactic-ws limit)
650 (let ((start (point)))
651 ;; In this case we accept both primitive and known types.
652 (when (eq (c-forward-type) 'known)
654 (let ((c-promote-possible-types t))
656 (if (> (point) limit) (goto-char limit)))))
658 ;; The @interface/@implementation/@protocol directives.
661 '("@interface" "@implementation" "@protocol")
666 (let (;; The font-lock package in Emacs is known to clobber
667 ;; `parse-sexp-lookup-properties' (when it exists).
668 (parse-sexp-lookup-properties
669 (cc-eval-when-compile
670 (boundp 'parse-sexp-lookup-properties))))
672 (narrow-to-region (point-min) limit)
673 (c-font-lock-objc-iip-decl)))
676 ("\\(!\\)[^=]" 1 font-lock-negation-char-face)
679 (defun c-font-lock-complex-decl-prepare (limit)
680 ;; Called before any of the matchers in `c-complex-decl-matchers'.
681 ;; Nil is always returned.
683 ;;(message "c-font-lock-complex-decl-prepare %s %s" (point) limit)
685 ;; Clear the list of found types if we start from the start of the
686 ;; buffer, to make it easier to get rid of misspelled types and
687 ;; variables that has gotten recognized as types in malformed code.
689 (c-clear-found-types))
691 ;; Clear the c-type char properties in the region to recalculate
692 ;; them properly. This is necessary e.g. to handle constructs that
693 ;; might been required as declarations temporarily during editing.
694 ;; The interesting properties are anyway those put on the closest
695 ;; token before the region.
696 (c-clear-char-properties (point) limit 'c-type)
698 ;; Update `c-state-cache' to the beginning of the region. This will
699 ;; make `c-beginning-of-syntax' go faster when it's used later on,
700 ;; and it's near the point most of the time.
703 ;; Check if the fontified region starts inside a declarator list so
704 ;; that `c-font-lock-declarators' should be called at the start.
705 (let ((prop (save-excursion
706 (c-backward-syntactic-ws)
708 (c-get-char-property (1- (point)) 'c-type)))))
709 (when (memq prop '(c-decl-id-start c-decl-type-start))
710 (c-forward-syntactic-ws limit)
711 (c-font-lock-declarators limit t (eq prop 'c-decl-type-start))))
715 (defun c-font-lock-<>-arglists (limit)
716 ;; Fontify types and references in names containing angle bracket
717 ;; arglists from the point to LIMIT. This will also fontify cases
718 ;; like normal function calls on the form "foo (a < b, c > d)", but
719 ;; `c-font-lock-declarations' will undo that later. Nil is always
722 (let (;; The font-lock package in Emacs is known to clobber
723 ;; `parse-sexp-lookup-properties' (when it exists).
724 (parse-sexp-lookup-properties
725 (cc-eval-when-compile
726 (boundp 'parse-sexp-lookup-properties)))
727 id-start id-end pos kwd-sym)
729 (while (and (< (point) limit)
730 (re-search-forward c-opt-<>-arglist-start limit t))
732 (setq id-start (match-beginning 1)
737 (unless (c-skip-comments-and-strings limit)
739 (if (or (not (eq (get-text-property id-start 'face)
740 'font-lock-keyword-face))
741 (when (looking-at c-opt-<>-sexp-key)
742 (setq kwd-sym (c-keyword-sym (match-string 1)))))
745 ;; Check for comment/string both at the identifier and
747 (unless (c-skip-comments-and-strings limit)
749 (when (c-forward-<>-arglist (c-keyword-member kwd-sym
752 (when (and c-opt-identifier-concat-key
753 (not (get-text-property id-start 'face)))
754 (c-forward-syntactic-ws)
755 (if (looking-at c-opt-identifier-concat-key)
756 (c-put-font-lock-face id-start id-end
757 c-reference-face-name)
758 (c-put-font-lock-face id-start id-end
759 'font-lock-type-face))))
765 (defun c-font-lock-declarators (limit list types)
766 ;; Assuming the point is at the start of a declarator in a
767 ;; declaration, fontify it. If LIST is non-nil, fontify also all
768 ;; following declarators in a comma separated list (e.g. "foo" and
769 ;; "bar" in "int foo = 17, bar;"). Stop at LIMIT. If TYPES is
770 ;; non-nil, fontify all identifiers as types. Nil is always
773 ;;(message "c-font-lock-declarators from %s to %s" (point) limit)
774 (c-fontify-types-and-refs
775 ((pos (point)) next-pos id-start id-end
778 c-last-identifier-range
779 (separator-prop (if types 'c-decl-type-start 'c-decl-id-start)))
785 (let (got-identifier)
787 ;; Skip over type decl prefix operators. (Note similar
788 ;; code in `c-font-lock-declarations'.)
789 (while (and (looking-at c-type-decl-prefix-key)
790 (if (and (c-major-mode-is 'c++-mode)
792 ;; If the second submatch matches in C++ then
793 ;; we're looking at an identifier that's a
794 ;; prefix only if it specifies a member pointer.
796 (setq id-start (point))
798 (if (looking-at "\\(::\\)")
799 ;; We only check for a trailing "::" and
800 ;; let the "*" that should follow be
801 ;; matched in the next round.
803 ;; It turned out to be the real identifier,
804 ;; so flag that and stop.
805 (setq got-identifier t)
808 (if (eq (char-after) ?\()
810 (setq paren-depth (1+ paren-depth))
812 (goto-char (match-end 1)))
813 (c-forward-syntactic-ws))
815 ;; If we didn't pass the identifier above already, do it now.
816 (unless got-identifier
817 (setq id-start (point))
819 (setq id-end (point))
823 ;; Skip out of the parens surrounding the identifier.
824 (or (= paren-depth 0)
825 (c-safe (goto-char (scan-lists (point) 1 paren-depth))))
829 ;; Search syntactically to the end of the declarator (";",
830 ;; ",", a closen paren, eob etc) or to the beginning of an
831 ;; initializer or function prototype ("=" or "\\s\(").
832 ;; Note that the open paren will match array specs in
833 ;; square brackets, and we treat them as initializers too.
834 (c-syntactic-re-search-forward
835 "[;,]\\|\\s)\\|\\'\\|\\(=\\|\\s(\\)" limit t t))
837 (setq next-pos (match-beginning 0)
838 id-face (if (eq (char-after next-pos) ?\()
839 'font-lock-function-name-face
840 'font-lock-variable-name-face)
841 got-init (and (match-beginning 1)
842 (char-after (match-beginning 1))))
845 ;; Register and fontify the identifer as a type.
846 (let ((c-promote-possible-types t))
849 ;; Fontify the last symbol in the identifier if it isn't fontified
850 ;; already. The check is necessary only in certain cases where this
851 ;; function is used "sloppily", e.g. in `c-simple-decl-matchers'.
852 (when (and c-last-identifier-range
853 (not (get-text-property (car c-last-identifier-range)
855 (c-put-font-lock-face (car c-last-identifier-range)
856 (cdr c-last-identifier-range)
862 ;; Jump past any initializer or function prototype to see if
863 ;; there's a ',' to continue at.
865 (cond ((eq id-face 'font-lock-function-name-face)
866 ;; Skip a parenthesized initializer (C++) or a function
868 (if (c-safe (c-forward-sexp 1) t)
869 (c-forward-syntactic-ws limit)
873 ;; Skip an initializer expression. If we're at a '='
874 ;; then accept a brace list directly after it to cope
875 ;; with array initializers. Otherwise stop at braces
876 ;; to avoid going past full function and class blocks.
877 (and (if (and (eq got-init ?=)
878 (= (c-forward-token-2 1 nil limit) 0)
880 (c-safe (c-forward-sexp) t)
882 (c-syntactic-re-search-forward "[;,{]" limit 'move t)
885 (t (c-forward-syntactic-ws limit)))
887 ;; If a ',' is found we set pos to the next declarator and iterate.
888 (when (and (< (point) limit) (looking-at ","))
889 (c-put-char-property (point) 'c-type separator-prop)
891 (c-forward-syntactic-ws limit)
892 (setq pos (point))))))
895 (defconst c-font-lock-maybe-decl-faces
896 ;; List of faces that might be put at the start of a type when
897 ;; `c-font-lock-declarations' runs. This needs to be evaluated to
898 ;; ensure that face name aliases in Emacs are resolved.
901 c-reference-face-name
902 font-lock-keyword-face))
904 ;; Macro used inside `c-font-lock-declarations'. It ought to be a
905 ;; defsubst or perhaps even a defun, but it contains lots of free
906 ;; variables that refer to things inside `c-font-lock-declarations'.
907 (defmacro c-fl-shift-type-backward (&optional short)
908 ;; `c-font-lock-declarations' can consume an arbitrary length list
909 ;; of types when parsing a declaration, which means that it
910 ;; sometimes consumes the identifier in the declaration as a type.
911 ;; This is used to "backtrack" and make the last type be treated
912 ;; as an identifier instead.
915 ;; These identifiers are bound only in the inner let.
916 '(setq identifier-type at-type
917 identifier-start type-start
918 identifier-end type-end))
919 (if (setq at-type (if (eq prev-at-type 'prefix)
922 (setq type-start prev-type-start
923 type-end prev-type-end)
924 (setq type-start start-pos
927 ;; These identifiers are bound only in the inner let.
928 '(setq start type-end
932 got-suffix-after-parens t
935 (defun c-font-lock-declarations (limit)
936 ;; Fontify all the declarations and casts from the point to LIMIT.
937 ;; Assumes that strings and comments have been fontified already.
938 ;; Nil is always returned.
940 ;; This function can make hidden buffer changes, but the font-lock
941 ;; context covers that.
943 ;;(message "c-font-lock-declarations search from %s to %s" (point) limit)
947 c-restricted-<>-arglists
948 ;; Nonzero if the `c-decl-prefix-re' match is in an arglist context,
949 ;; as opposed to a statement-level context. The major difference is
950 ;; that "," works as declaration delimiter in an arglist context,
951 ;; whereas it only separates declarators in the same declaration in
952 ;; a statement context. If it's nonzero then the value is the
953 ;; matched char, e.g. ?\( or ?,.
955 ;; 'decl if we're in an arglist containing declarations (but if
956 ;; `c-recognize-paren-inits' is set it might also be an initializer
957 ;; arglist), '<> if the arglist is of angle bracket type, 'other if
958 ;; it's some other arglist, or nil if not in an arglist at all.
960 ;; Set to the result of `c-forward-type'.
962 ;; These record the start and end of the type or possible type found
963 ;; by `c-forward-type'. `type-start' is at the start of the first
964 ;; type token, and `type-end' is at the start of the first token
965 ;; after the type (and after any specifiers).
967 ;; These store `at-type', `type-start' and `type-end' of the
968 ;; identifier before the one in those variables. The previous
969 ;; identifier might turn out to be the real type in a declaration if
970 ;; the last one has to be the declarator in it. If `prev-at-type'
971 ;; is nil then the other variables have undefined values.
972 prev-at-type prev-type-start prev-type-end
973 ;; Whether we've found a declaration or a cast. We might know this
974 ;; before we've found the type in it.
976 ;; Set when we need to back up to parse this as a declaration but
979 ;; Set if we've found a "typedef" specifier. The identifiers in the
980 ;; declaration are then fontified as types.
982 ;; Set if we've found a specifier that can start a declaration where
985 ;; The position of the next token after the closing paren of the
986 ;; last fontified cast.
988 ;; The same for the currently investigated cast.
990 ;; The maximum of the end positions of all the checked type decl
991 ;; expressions in the successfully identified declarations. The
992 ;; position might be either before or after the syntactic whitespace
993 ;; following the last token in the type decl expression.
994 (max-type-decl-end 0)
995 ;; Same as `max-type-decl-*', but used when we're before
997 (max-type-decl-end-before-token 0)
998 ;; Allow recording of identifier ranges in `c-forward-type' etc for
999 ;; later fontification. Not using `c-fontify-types-and-refs' here
1000 ;; since the ranges should be fontified selectively only when a
1001 ;; declaration or cast has been successfully recognized.
1002 c-record-type-identifiers
1003 c-record-ref-identifiers
1004 ;; The font-lock package in Emacs is known to clobber
1005 ;; `parse-sexp-lookup-properties' (when it exists).
1006 (parse-sexp-lookup-properties
1007 (cc-eval-when-compile
1008 (boundp 'parse-sexp-lookup-properties))))
1010 ;; Below we fontify a whole declaration even when it crosses the limit,
1011 ;; to avoid gaps when lazy-lock fontifies the file a screenful at a
1012 ;; time. That is however annoying during editing, e.g. the following is
1013 ;; a common situation while the first line is being written:
1016 ;; some_other_variable = 0;
1018 ;; font-lock will put the limit at the beginning of the second line
1019 ;; here, and if we go past it we'll fontify "my_variable" as a type and
1020 ;; "some_other_variable" as an identifier, and the latter will not
1021 ;; correct itself until the second line is changed. To avoid that we
1022 ;; narrow to the limit if the region to fontify is a single line.
1023 (when (<= limit (c-point 'bonl))
1027 ;; Narrow after any operator chars following the limit though, since
1028 ;; those characters can be useful in recognizing a declaration (in
1029 ;; particular the '{' that opens a function body after the header).
1031 (skip-chars-forward c-nonsymbol-chars)
1037 c-font-lock-maybe-decl-faces
1039 (lambda (match-pos inside-macro)
1041 ;; Don't do anything more if we're looking at a keyword
1042 ;; that can't start a declaration.
1043 (when (and (eq (get-text-property (point) 'face)
1044 'font-lock-keyword-face)
1045 (looking-at c-not-decl-init-keywords))
1046 (throw 'false-alarm t))
1048 ;; Set `arglist-match' and `arglist-type'. Look for "<" for the
1049 ;; sake of C++-style template arglists.
1050 (setq arglist-match (char-before match-pos))
1051 (if (memq arglist-match '(?\( ?, ?\[ ?<))
1053 ;; Find out the type of the arglist.
1054 (if (<= match-pos (point-min))
1055 (setq arglist-type 'other)
1056 (let ((type (c-get-char-property (1- match-pos) 'c-type)))
1057 (cond ((eq type 'c-decl-arg-start)
1058 ;; Got a cached hit in a declaration arglist.
1059 (setq arglist-type 'decl))
1060 ((or (eq type 'c-<>-arg-sep)
1061 (eq arglist-match ?<))
1062 ;; Inside an angle bracket arglist.
1063 (setq arglist-type '<>))
1065 ;; Got a cached hit in some other type of arglist.
1066 (setq arglist-type 'other))
1068 (< match-pos max-type-decl-end-before-token)
1069 (< match-pos max-type-decl-end))
1070 ;; The point is within the range of a previously
1071 ;; encountered type decl expression, so the arglist
1072 ;; is probably one that contains declarations.
1073 ;; However, if `c-recognize-paren-inits' is set it
1074 ;; might also be an initializer arglist.
1075 (setq arglist-type 'decl)
1076 ;; The result of this check is cached with a char
1077 ;; property on the match token, so that we can look
1078 ;; it up again when refontifying single lines in a
1079 ;; multiline declaration.
1080 (c-put-char-property (1- match-pos)
1081 'c-type 'c-decl-arg-start))
1083 (setq arglist-type 'other)))))
1085 (setq arglist-match nil
1090 backup-if-not-cast nil
1093 c-record-type-identifiers t
1094 c-record-ref-identifiers nil
1095 ;; `start-pos' is used below to point to the start of the
1096 ;; first type, i.e. after any leading specifiers. It might
1097 ;; also point at the beginning of the preceding syntactic
1100 ;; If we're in a normal arglist context we don't want to
1101 ;; recognize commas in nested angle bracket arglists since
1102 ;; those commas could be part of our own arglist.
1103 c-restricted-<>-arglists
1104 (and c-recognize-<>-arglists
1105 (eq arglist-type 'other)))
1107 (when (and c-restricted-<>-arglists
1108 (/= arglist-match ?,))
1109 ;; We're standing at the start of a normal arglist so remove any
1110 ;; angle bracket arglists containing commas that's been
1111 ;; recognized inside it by the preceding slightly opportunistic
1112 ;; scan in `c-font-lock-<>-arglists'.
1113 (while (and (c-syntactic-re-search-forward
1114 c-opt-<>-arglist-start-in-paren nil t t)
1115 (match-beginning 1))
1117 (when (save-match-data
1118 (and (c-get-char-property (point) 'syntax-table)
1119 (not (c-forward-<>-arglist nil t))))
1120 (c-remove-font-lock-face (match-beginning 2) (match-end 2))))
1121 (goto-char start-pos))
1123 ;; Check for a type, but be prepared to skip over leading
1124 ;; specifiers like "static". Unknown symbols are treated as
1125 ;; possible types, but they could also be specifiers disguised
1126 ;; through macros like __INLINE__, so we recognize both types and
1127 ;; known specifiers after them too.
1128 (while (let ((start (point))
1129 (res (unless (eq at-type t)
1130 ;; Don't look for a type if we already found a
1131 ;; positive one; we only loop for the
1132 ;; `c-specifier-key' check then.
1136 ;; Found a known or possible type or a prefix of a known
1140 ;; Got two identifiers with nothing but whitespace
1141 ;; between them. That can only happen in
1143 (setq at-decl-or-cast t)
1145 (when (eq at-type 'found)
1146 ;; If the previous identifier is a found type we
1147 ;; record it as a real one; it might be some sort of
1148 ;; alias for a prefix like "unsigned".
1150 (goto-char type-start)
1151 (let ((c-promote-possible-types t))
1152 (c-forward-type)))))
1154 (setq prev-at-type at-type
1155 prev-type-start type-start
1156 prev-type-end type-end
1161 ;; If the type isn't known we continue so that we'll
1162 ;; jump over all specifiers and type identifiers. The
1163 ;; reason to do this for a known type prefix is to make
1164 ;; things like "unsigned INT16" work.
1165 (setq res (not (eq res t))))
1167 (if (looking-at c-specifier-key)
1168 ;; Found a known specifier keyword. The specifier
1169 ;; keywords are restrictive, so we check for them
1170 ;; anywhere inside or around the type(s). We thereby
1171 ;; avoid having special cases for specifiers like MSVC
1172 ;; '__declspec' which can come after the type.
1174 (setq at-decl-or-cast t)
1175 (let ((kwd-sym (c-keyword-sym (match-string 1))))
1176 (when (c-keyword-member
1177 kwd-sym 'c-typedef-decl-kwds)
1178 (setq at-typedef t))
1179 (when (c-keyword-member
1180 kwd-sym 'c-typeless-decl-kwds)
1181 (setq maybe-typeless t)))
1182 (c-forward-keyword-clause)
1183 ;; Move type-end forward if we've passed a type,
1184 ;; otherwise move start-pos forward.
1186 (setq type-end (point))
1187 (setq start-pos (point))))
1192 ((eq at-type 'prefix)
1193 ;; A prefix type is itself a primitive type when it's not
1194 ;; followed by another type.
1198 ;; Got no type but set things up to continue anyway to handle the
1199 ;; various cases when a declaration doesn't start with a type.
1200 (setq type-end start-pos))
1202 ((and (eq at-type 'maybe)
1203 (c-major-mode-is 'c++-mode))
1204 ;; If it's C++ then check if the last "type" ends on the form
1205 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
1206 ;; (con|de)structor.
1208 (let (name end-2 end-1)
1209 (goto-char type-end)
1210 (c-backward-syntactic-ws)
1211 (setq end-2 (point))
1213 (c-simple-skip-symbol-backward)
1216 (buffer-substring-no-properties (point) end-2))
1217 ;; Cheating in the handling of syntactic ws below.
1218 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
1220 (setq end-1 (point))
1221 (c-simple-skip-symbol-backward))
1222 (>= (point) type-start)
1223 (equal (buffer-substring-no-properties (point) end-1)
1225 ;; It is a (con|de)structor name. In that case the
1226 ;; declaration is typeless so zap out any preceding
1227 ;; identifier(s) that we might have taken as types.
1228 (goto-char type-start)
1231 type-end type-start))))))
1233 ;; Check for and step over a type decl expression after the thing
1234 ;; that is or might be a type. This can't be skipped since we need
1235 ;; the correct end position of the declarator for
1236 ;; `max-type-decl-end-*'.
1237 (let ((start (point)) (paren-depth 0) pos
1238 ;; True if there's a non-open-paren match of
1239 ;; `c-type-decl-prefix-key'.
1241 ;; True if the declarator is surrounded by a parenthesis pair.
1243 ;; True if there is an identifier in the declarator.
1245 ;; True if there's a non-close-paren match of
1246 ;; `c-type-decl-suffix-key'.
1248 ;; True if there's a prefix or suffix match outside the
1249 ;; outermost paren pair that surrounds the declarator.
1250 got-prefix-before-parens
1251 got-suffix-after-parens
1252 ;; True if we've parsed the type decl to a token that
1253 ;; is known to end declarations in this context.
1255 ;; The earlier values of `at-type', `type-start' and
1256 ;; `type-end' if we've shifted the type backwards.
1257 identifier-type identifier-start identifier-end)
1258 (goto-char type-end)
1260 ;; Skip over type decl prefix operators. (Note similar code in
1261 ;; `c-font-lock-declarators'.)
1262 (while (and (looking-at c-type-decl-prefix-key)
1263 (if (and (c-major-mode-is 'c++-mode)
1264 (match-beginning 2))
1265 ;; If the second submatch matches in C++ then
1266 ;; we're looking at an identifier that's a prefix
1267 ;; only if it specifies a member pointer.
1268 (when (setq got-identifier (c-forward-name))
1269 (if (looking-at "\\(::\\)")
1270 ;; We only check for a trailing "::" and
1271 ;; let the "*" that should follow be
1272 ;; matched in the next round.
1273 (progn (setq got-identifier nil) t)
1274 ;; It turned out to be the real identifier,
1278 (if (eq (char-after) ?\()
1280 (setq paren-depth (1+ paren-depth))
1282 (unless got-prefix-before-parens
1283 (setq got-prefix-before-parens (= paren-depth 0)))
1285 (goto-char (match-end 1)))
1286 (c-forward-syntactic-ws))
1287 (setq got-parens (> paren-depth 0))
1289 ;; Skip over an identifier.
1291 (and (looking-at c-identifier-start)
1292 (setq got-identifier (c-forward-name))))
1294 ;; Skip over type decl suffix operators.
1295 (while (if (looking-at c-type-decl-suffix-key)
1296 (if (eq (char-after) ?\))
1297 (when (> paren-depth 0)
1298 (setq paren-depth (1- paren-depth))
1301 (when (if (save-match-data (looking-at "\\s\("))
1302 (c-safe (c-forward-sexp 1) t)
1303 (goto-char (match-end 1))
1305 (unless got-suffix-after-parens
1306 (setq got-suffix-after-parens (= paren-depth 0)))
1307 (setq got-suffix t)))
1308 ;; No suffix matched. We might have matched the
1309 ;; identifier as a type and the open paren of a function
1310 ;; arglist as a type decl prefix. In that case we
1311 ;; should "backtrack": Reinterpret the last type as the
1312 ;; identifier, move out of the arglist and continue
1313 ;; searching for suffix operators.
1315 ;; Do this even if there's no preceding type, to cope
1316 ;; with old style function declarations in K&R C,
1317 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
1318 ;; style declarations. That isn't applicable in an
1319 ;; arglist context, though.
1320 (when (and (= paren-depth 1)
1321 (not got-prefix-before-parens)
1322 (not (eq at-type t))
1325 (when c-recognize-typeless-decls
1326 (not arglist-type)))
1327 (setq pos (c-up-list-forward (point)))
1328 (eq (char-before pos) ?\)))
1329 (c-fl-shift-type-backward)
1332 (c-forward-syntactic-ws))
1334 (when (and maybe-typeless
1335 (not got-identifier)
1338 (not (eq at-type t)))
1339 ;; Have found no identifier but `c-typeless-decl-kwds' has
1340 ;; matched so we know we're inside a declaration. The
1341 ;; preceding type must be the identifier instead.
1342 (c-fl-shift-type-backward))
1346 (catch 'at-decl-or-cast
1348 (when (> paren-depth 0)
1349 ;; Encountered something inside parens that isn't matched by
1350 ;; the `c-type-decl-*' regexps, so it's not a type decl
1351 ;; expression. Try to skip out to the same paren depth to
1352 ;; not confuse the cast check below.
1353 (c-safe (goto-char (scan-lists (point) 1 paren-depth)))
1354 (throw 'at-decl-or-cast nil))
1357 (looking-at (cond ((eq arglist-type '<>) "[,>]")
1358 (arglist-type "[,\)]")
1361 ;; Now we've collected info about various characteristics of
1362 ;; the construct we're looking at. Below follows a decision
1363 ;; tree based on that. It's ordered to check more certain
1364 ;; signs before less certain ones.
1369 (when (and (or at-type maybe-typeless)
1370 (not (or got-prefix got-parens)))
1371 ;; Got another identifier directly after the type, so
1372 ;; it's a declaration.
1373 (throw 'at-decl-or-cast t))
1375 (when (and got-parens
1377 (not got-suffix-after-parens)
1378 (or prev-at-type maybe-typeless))
1379 ;; Got a declaration of the form "foo bar (gnu);"
1380 ;; where we've recognized "bar" as the type and "gnu"
1381 ;; as the declarator. In this case it's however more
1382 ;; likely that "bar" is the declarator and "gnu" a
1383 ;; function argument or initializer (if
1384 ;; `c-recognize-paren-inits' is set), since the parens
1385 ;; around "gnu" would be superfluous if it's a
1386 ;; declarator. Shift the type one step backward.
1387 (c-fl-shift-type-backward)))
1389 ;; Found no identifier.
1392 (when (or (= (point) start)
1396 ;; Got two types after each other, so if this isn't a
1397 ;; cast then the latter probably is the identifier and
1398 ;; we should back up to the previous type.
1399 (setq backup-if-not-cast t)
1400 (throw 'at-decl-or-cast t))
1402 (when (eq at-type t)
1403 ;; If the type is known we know that there can't be any
1404 ;; identifier somewhere else, and it's only in
1405 ;; declarations in e.g. function prototypes and in casts
1406 ;; that the identifier may be left out.
1407 (throw 'at-decl-or-cast t))
1409 (when (= (point) start)
1410 ;; Only got a single identifier (parsed as a type so
1413 ;; Check that the identifier isn't at the start of
1417 ((eq arglist-type 'decl)
1418 ;; Inside an arglist that contains declarations.
1419 ;; If K&R style declarations and parenthesis
1420 ;; style initializers aren't allowed then the
1421 ;; single identifier must be a type, else we
1422 ;; require that it's known or found (primitive
1423 ;; types are handled above).
1424 (or (and (not c-recognize-knr-p)
1425 (not c-recognize-paren-inits))
1426 (memq at-type '(known found))))
1427 ((eq arglist-type '<>)
1428 ;; Inside a template arglist. Accept known and
1429 ;; found types; other identifiers could just as
1430 ;; well be constants in C++.
1431 (memq at-type '(known found)))))
1432 (throw 'at-decl-or-cast t)
1433 (throw 'at-decl-or-cast nil))))
1439 (not (eq at-type t))
1443 (when c-recognize-typeless-decls
1444 (or (not got-suffix)
1446 c-after-suffixed-type-maybe-decl-key))))))
1447 ;; Got an empty paren pair and a preceding type that
1448 ;; probably really is the identifier. Shift the type
1449 ;; backwards to make the last one the identifier. This
1450 ;; is analogous to the "backtracking" done inside the
1451 ;; `c-type-decl-suffix-key' loop above.
1453 ;; Exception: In addition to the conditions in that
1454 ;; "backtracking" code, do not shift backward if we're
1455 ;; not looking at either `c-after-suffixed-type-decl-key'
1456 ;; or "[;,]". Since there's no preceding type, the
1457 ;; shift would mean that the declaration is typeless.
1458 ;; But if the regexp doesn't match then we will simply
1459 ;; fall through in the tests below and not recognize it
1460 ;; at all, so it's better to try it as an abstract
1461 ;; declarator instead.
1462 (c-fl-shift-type-backward)
1464 ;; Still no identifier.
1466 (when (and got-prefix (or got-parens got-suffix))
1467 ;; Require `got-prefix' together with either
1468 ;; `got-parens' or `got-suffix' to recognize it as an
1469 ;; abstract declarator: `got-parens' only is probably an
1470 ;; empty function call. `got-suffix' only can build an
1471 ;; ordinary expression together with the preceding
1472 ;; identifier which we've taken as a type. We could
1473 ;; actually accept on `got-prefix' only, but that can
1474 ;; easily occur temporarily while writing an expression
1475 ;; so we avoid that case anyway. We could do a better
1476 ;; job if we knew the point when the fontification was
1478 (throw 'at-decl-or-cast t))))
1480 (when at-decl-or-cast
1481 ;; By now we've located the type in the declaration that we
1483 (throw 'at-decl-or-cast t))
1485 (when (and got-identifier
1487 (looking-at c-after-suffixed-type-decl-key)
1491 (not (eq at-type t)))
1492 ;; Shift the type backward in the case that
1493 ;; there's a single identifier inside parens.
1494 ;; That can only occur in K&R style function
1495 ;; declarations so it's more likely that it
1496 ;; really is a function call. Therefore we
1497 ;; only do this after
1498 ;; `c-after-suffixed-type-decl-key' has
1500 (progn (c-fl-shift-type-backward) t)
1501 got-suffix-after-parens))
1502 ;; A declaration according to
1503 ;; `c-after-suffixed-type-decl-key'.
1504 (throw 'at-decl-or-cast t))
1506 (when (and (or got-prefix (not got-parens))
1507 (memq at-type '(t known)))
1508 ;; It's a declaration if a known type precedes it and it
1509 ;; can't be a function call.
1510 (throw 'at-decl-or-cast t))
1512 ;; If we get here we can't tell if this is a type decl or a
1513 ;; normal expression by looking at it alone. (That's under
1514 ;; the assumption that normal expressions always can look like
1515 ;; type decl expressions, which isn't really true but the
1516 ;; cases where it doesn't hold are so uncommon (e.g. some
1517 ;; placements of "const" in C++) it's not worth the effort to
1520 (unless (or at-decl-end (looking-at "=[^=]"))
1521 ;; If this is a declaration it should end here or its
1522 ;; initializer(*) should start here, so check for allowed
1523 ;; separation tokens. Note that this rule doesn't work
1524 ;; e.g. with a K&R arglist after a function header.
1526 ;; *) Don't check for C++ style initializers using parens
1527 ;; since those already have been matched as suffixes.
1528 (throw 'at-decl-or-cast nil))
1530 ;; Below are tests that only should be applied when we're
1531 ;; certain to not have parsed halfway through an expression.
1533 (when (memq at-type '(t known))
1534 ;; The expression starts with a known type so treat it as a
1536 (throw 'at-decl-or-cast t))
1538 (when (and (c-major-mode-is 'c++-mode)
1539 ;; In C++ we check if the identifier is a known
1540 ;; type, since (con|de)structors use the class name
1541 ;; as identifier. We've always shifted over the
1542 ;; identifier as a type and then backed up again in
1545 (or (memq identifier-type '(found known))
1546 (and (eq (char-after identifier-start) ?~)
1547 ;; `at-type' probably won't be 'found for
1548 ;; destructors since the "~" is then part
1549 ;; of the type name being checked against
1550 ;; the list of known types, so do a check
1551 ;; without that operator.
1553 (goto-char (1+ identifier-start))
1554 (c-forward-syntactic-ws)
1555 (c-with-syntax-table
1556 c-identifier-syntax-table
1557 (looking-at c-known-type-key)))
1558 (c-check-type (1+ identifier-start)
1560 (throw 'at-decl-or-cast t))
1564 (when (and got-prefix-before-parens
1566 (or at-decl-end (looking-at "=[^=]"))
1569 ;; Got something like "foo * bar;". Since we're not
1570 ;; inside an arglist it would be a meaningless
1571 ;; expression because the result isn't used. We
1572 ;; therefore choose to recognize it as a declaration.
1573 ;; Do not allow a suffix since it could then be a
1575 (throw 'at-decl-or-cast t))
1577 (when (and (or got-suffix-after-parens
1578 (looking-at "=[^=]"))
1580 (not (eq arglist-type 'other)))
1581 ;; Got something like "a (*b) (c);" or "a (b) = c;".
1582 ;; It could be an odd expression or it could be a
1583 ;; declaration. Treat it as a declaration if "a" has
1584 ;; been used as a type somewhere else (if it's a known
1585 ;; type we won't get here).
1586 (throw 'at-decl-or-cast t)))
1588 (when (and arglist-type
1590 (and (eq arglist-type 'decl)
1591 (not c-recognize-paren-inits)
1592 (or got-parens got-suffix))))
1593 ;; Got a type followed by an abstract declarator. If
1594 ;; `got-prefix' is set it's something like "a *" without
1595 ;; anything after it. If `got-parens' or `got-suffix' is
1596 ;; set it's "a()", "a[]", "a()[]", or similar, which we
1597 ;; accept only if the context rules out expressions.
1598 (throw 'at-decl-or-cast t)))
1600 ;; If we had a complete symbol table here (which rules out
1601 ;; `c-found-types') we should return t due to the
1602 ;; disambiguation rule (in at least C++) that anything that
1603 ;; can be parsed as a declaration is a declaration. Now we're
1604 ;; being more defensive and prefer to highlight things like
1605 ;; "foo (bar);" as a declaration only if we're inside an
1606 ;; arglist that contains declarations.
1607 (eq arglist-type 'decl))))
1609 ;; Point is now after the type decl expression.
1612 ;; Check for a cast.
1617 ;; Should be the first type/identifier in a cast paren.
1618 (memq arglist-match c-cast-parens)
1620 ;; The closing paren should follow.
1622 (c-forward-syntactic-ws)
1623 (looking-at "\\s\)"))
1625 ;; There should be a primary expression after it.
1628 (c-forward-syntactic-ws)
1629 (setq cast-end (point))
1630 (and (looking-at c-primary-expr-regexp)
1632 (setq pos (match-end 0))
1634 ;; Check if the expression begins with a prefix
1637 (if (match-beginning 1)
1638 ;; Expression begins with an ambiguous operator.
1639 ;; Treat it as a cast if it's a type decl or if
1640 ;; we've recognized the type somewhere else.
1642 (memq at-type '(t known found)))
1643 ;; Unless it's a keyword, it's the beginning of a
1644 ;; primary expression.
1645 (not (looking-at c-keywords-regexp)))))
1646 ;; If `c-primary-expr-regexp' matched a nonsymbol
1647 ;; token, check that it matched a whole one so that we
1648 ;; don't e.g. confuse the operator '-' with '->'. It's
1649 ;; ok if it matches further, though, since it e.g. can
1650 ;; match the float '.5' while the operator regexp only
1652 (or (not (looking-at c-nonsymbol-token-regexp))
1653 (<= (match-end 0) pos))))
1655 ;; There should either be a cast before it or something that
1656 ;; isn't an identifier or close paren.
1659 (goto-char (1- match-pos))
1660 (or (eq (point) last-cast-end)
1662 (c-backward-syntactic-ws)
1663 (if (< (skip-syntax-backward "w_") 0)
1664 ;; It's a symbol. Accept it only if it's one of
1665 ;; the keywords that can precede an expression
1666 ;; (without surrounding parens).
1667 (looking-at c-simple-stmt-key)
1669 ;; Check that it isn't a close paren (block close
1671 (not (memq (char-before) '(?\) ?\])))
1672 ;; Check that it isn't a nonsymbol identifier.
1673 (not (c-on-identifier)))))))))
1676 (setq last-cast-end cast-end)
1677 (when (and at-type (not (eq at-type t)))
1678 (let ((c-promote-possible-types t))
1679 (goto-char type-start)
1683 ;; We're at a declaration. Highlight the type and the following
1686 (when backup-if-not-cast
1687 (c-fl-shift-type-backward t))
1689 (when (and (eq arglist-type 'decl) (looking-at ","))
1690 ;; Make sure to propagate the `c-decl-arg-start' property to
1691 ;; the next argument if it's set in this one, to cope with
1692 ;; interactive refontification.
1693 (c-put-char-property (point) 'c-type 'c-decl-arg-start))
1695 ;; Set `max-type-decl-end' or `max-type-decl-end-before-token'
1696 ;; under the assumption that we're after the first type decl
1697 ;; expression in the declaration now. That's not really true; we
1698 ;; could also be after a parenthesized initializer expression in
1699 ;; C++, but this is only used as a last resort to slant ambiguous
1700 ;; expression/declarations, and overall it's worth the risk to
1701 ;; occasionally fontify an expression as a declaration in an
1702 ;; initializer expression compared to getting ambiguous things in
1703 ;; normal function prototypes fontified as expressions.
1705 (when (> (point) max-type-decl-end-before-token)
1706 (setq max-type-decl-end-before-token (point)))
1707 (when (> (point) max-type-decl-end)
1708 (setq max-type-decl-end (point))))
1710 (when (and at-type (not (eq at-type t)))
1711 (let ((c-promote-possible-types t))
1712 (goto-char type-start)
1715 (goto-char type-end)
1719 ;; Should normally not fontify a list of declarators
1720 ;; inside an arglist, but the first argument in the
1721 ;; ';' separated list of a "for" statement is an
1723 (when (and (eq arglist-match ?\() (/= match-pos 0))
1725 (goto-char (1- match-pos))
1726 (c-backward-syntactic-ws)
1727 (and (c-simple-skip-symbol-backward)
1728 (looking-at c-paren-stmt-key))))
1731 ;; Fix the `c-decl-id-start' or `c-decl-type-start' property
1732 ;; before the first declarator if it's a list.
1733 ;; `c-font-lock-declarators' handles the rest.
1736 (c-backward-syntactic-ws)
1738 (c-put-char-property (1- (point)) 'c-type
1741 'c-decl-id-start)))))
1743 (c-font-lock-declarators (point-max) decl-list at-typedef)))
1746 ;; False alarm. Skip the fontification done below.
1747 (throw 'false-alarm t)))
1749 ;; A cast or declaration has been successfully identified, so do
1750 ;; all the fontification of types and refs that's been recorded by
1751 ;; the calls to `c-forward-type' and `c-forward-name' above.
1752 (c-fontify-recorded-types-and-refs)
1757 (c-lang-defconst c-simple-decl-matchers
1758 "Simple font lock matchers for types and declarations. These are used
1759 on level 2 only and so aren't combined with `c-complex-decl-matchers'."
1761 t `(;; Objective-C methods.
1762 ,@(when (c-major-mode-is 'objc-mode)
1763 `((,(c-lang-const c-opt-method-key)
1766 (let (;; The font-lock package in Emacs is known to clobber
1767 ;; `parse-sexp-lookup-properties' (when it exists).
1768 (parse-sexp-lookup-properties
1769 (cc-eval-when-compile
1770 (boundp 'parse-sexp-lookup-properties))))
1772 (narrow-to-region (point-min) limit)
1773 (c-font-lock-objc-method)))
1775 (goto-char (match-end 1))))))
1777 ;; Fontify all type names and the identifiers in the
1778 ;; declarations they might start. Use eval here since
1779 ;; `c-known-type-key' gets its value from
1780 ;; `*-font-lock-extra-types' on mode init.
1781 (eval . (list ,(c-make-font-lock-search-function
1783 '(1 'font-lock-type-face t)
1784 '((c-font-lock-declarators limit t nil)
1786 (goto-char (match-end 1))
1787 (c-forward-syntactic-ws))
1788 (goto-char (match-end 1))))))
1790 ;; Fontify types preceded by `c-type-prefix-kwds' and the
1791 ;; identifiers in the declarations they might start.
1792 ,@(when (c-lang-const c-type-prefix-kwds)
1793 (let ((prefix-re (c-make-keywords-re nil
1794 (c-lang-const c-type-prefix-kwds))))
1795 `((,(c-make-font-lock-search-function
1796 (concat "\\<\\(" prefix-re "\\)"
1798 "\\(" (c-lang-const c-symbol-key) "\\)")
1799 `(,(+ (c-regexp-opt-depth prefix-re) 2)
1800 'font-lock-type-face t)
1801 '((c-font-lock-declarators limit t nil)
1803 (goto-char (match-end 2))
1804 (c-forward-syntactic-ws))
1805 (goto-char (match-end 2))))))))
1807 ;; Fontify special declarations that lacks a type.
1808 ,@(when (c-lang-const c-typeless-decl-kwds)
1809 `((,(c-make-font-lock-search-function
1811 (c-regexp-opt (c-lang-const c-typeless-decl-kwds))
1813 '((c-font-lock-declarators limit t nil)
1815 (goto-char (match-end 1))
1816 (c-forward-syntactic-ws))
1817 (goto-char (match-end 1)))))))
1820 (c-lang-defconst c-complex-decl-matchers
1821 "Complex font lock matchers for types and declarations. Used on level
1824 t `(;; Initialize some things before the search functions below.
1825 c-font-lock-complex-decl-prepare
1827 ;; Fontify angle bracket arglists like templates in C++.
1828 ,@(when (c-lang-const c-recognize-<>-arglists)
1829 `(c-font-lock-<>-arglists))
1831 ,@(if (c-major-mode-is 'objc-mode)
1832 ;; Fontify method declarations in Objective-C, but first
1833 ;; we have to put the `c-decl-end' `c-type' property on
1834 ;; all the @-style directives that haven't been handled in
1835 ;; `c-basic-matchers-before'.
1836 `(,(c-make-font-lock-search-function
1837 (c-make-keywords-re t
1838 ;; Exclude "@class" since that directive ends with a
1839 ;; semicolon anyway.
1841 (append (c-lang-const c-protection-kwds)
1842 (c-lang-const c-other-decl-kwds)
1844 '((c-put-char-property (1- (match-end 1))
1845 'c-type 'c-decl-end)))
1847 c-font-lock-objc-methods)
1849 (when (c-lang-const c-opt-access-key)
1850 `(,(c-make-font-lock-search-function
1851 (c-lang-const c-opt-access-key)
1852 '((c-put-char-property (1- (match-end 0))
1853 'c-type 'c-decl-end))))))
1855 ;; Fontify all declarations and casts.
1856 c-font-lock-declarations
1858 ;; The first two rules here mostly find occurences that
1859 ;; `c-font-lock-declarations' has found already, but not
1860 ;; declarations containing blocks in the type (see note below).
1861 ;; It's also useful to fontify these everywhere to show e.g. when
1862 ;; a type keyword is accidentally used as an identifier.
1864 ;; Fontify basic types.
1865 ,(let ((re (c-make-keywords-re nil
1866 (c-lang-const c-primitive-type-kwds))))
1867 (if (c-major-mode-is 'pike-mode)
1868 ;; No symbol is a keyword after "->" in Pike.
1869 `(,(concat "\\(\\=\\|\\(\\=\\|[^-]\\)[^>]\\)"
1870 "\\<\\(" re "\\)\\>")
1871 3 font-lock-type-face)
1872 `(,(concat "\\<\\(" re "\\)\\>")
1873 1 'font-lock-type-face)))
1875 ;; Fontify types preceded by `c-type-prefix-kwds'.
1876 ,@(when (c-lang-const c-type-prefix-kwds)
1879 (c-fontify-types-and-refs
1880 ((c-promote-possible-types t)
1881 ;; The font-lock package in Emacs is known to clobber
1882 ;; `parse-sexp-lookup-properties' (when it exists).
1883 (parse-sexp-lookup-properties
1884 (cc-eval-when-compile
1885 (boundp 'parse-sexp-lookup-properties))))
1887 ;; Narrow to avoid going past the limit in
1888 ;; `c-forward-type'.
1889 (narrow-to-region (point) limit)
1890 (while (re-search-forward
1892 (c-make-keywords-re nil
1893 (c-lang-const c-type-prefix-kwds))
1896 (unless (c-skip-comments-and-strings limit)
1897 (c-forward-syntactic-ws)
1898 ;; Handle prefix declaration specifiers.
1899 (when (looking-at c-specifier-key)
1900 (c-forward-keyword-clause))
1901 ,(if (c-major-mode-is 'c++-mode)
1902 `(when (and (c-forward-type)
1903 (eq (char-after) ?=))
1904 ;; In C++ we additionally check for a "class
1905 ;; X = Y" construct which is used in
1906 ;; templates, to fontify Y as a type.
1908 (c-forward-syntactic-ws)
1913 ;; Fontify symbols after closing braces as declaration
1914 ;; identifiers under the assumption that they are part of
1915 ;; declarations like "class Foo { ... } foo;". It's too
1916 ;; expensive to check this accurately by skipping past the
1917 ;; brace block, so we use the heuristic that it's such a
1918 ;; declaration if the first identifier is on the same line as
1919 ;; the closing brace. `c-font-lock-declarations' will later
1920 ;; override it if it turns out to be an new declaration, but
1921 ;; it will be wrong if it's an expression (see the test
1923 ,@(when (c-lang-const c-opt-block-decls-with-vars-key)
1924 `((,(c-make-font-lock-search-function
1926 (c-lang-const c-single-line-syntactic-ws)
1927 "\\(" ; 1 + c-single-line-syntactic-ws-depth
1928 (c-lang-const c-type-decl-prefix-key)
1930 (c-lang-const c-symbol-key)
1932 `((c-font-lock-declarators limit t nil)
1934 (c-put-char-property (match-beginning 0) 'c-type
1936 (goto-char (match-beginning
1938 c-single-line-syntactic-ws-depth)))))
1939 (goto-char (match-end 0)))))))
1941 ;; Fontify the type in C++ "new" expressions.
1942 ,@(when (c-major-mode-is 'c++-mode)
1944 (c-font-lock-c++-new))))
1947 (defun c-font-lock-labels (limit)
1948 ;; Fontify all the declarations from the point to LIMIT. Assumes
1949 ;; that strings and comments have been fontified already. Nil is
1952 ;; This function can make hidden buffer changes, but the font-lock
1953 ;; context covers that.
1955 (let (continue-pos id-start
1956 ;; The font-lock package in Emacs is known to clobber
1957 ;; `parse-sexp-lookup-properties' (when it exists).
1958 (parse-sexp-lookup-properties
1959 (cc-eval-when-compile
1960 (boundp 'parse-sexp-lookup-properties))))
1962 (while (re-search-forward ":[^:]" limit t)
1963 (setq continue-pos (point))
1964 (goto-char (match-beginning 0))
1965 (unless (c-skip-comments-and-strings limit)
1967 (c-backward-syntactic-ws)
1968 (and (setq id-start (c-on-identifier))
1970 (not (get-text-property id-start 'face))
1973 (goto-char id-start)
1974 (c-backward-syntactic-ws)
1976 ;; Check for a char that precedes a statement.
1977 (memq (char-before) '(?\} ?\{ ?\;))
1978 ;; Check for a preceding label. We exploit the font
1979 ;; locking made earlier by this function.
1980 (and (eq (char-before) ?:)
1983 (c-backward-syntactic-ws)
1985 (eq (get-text-property (1- (point)) 'face)
1987 ;; Check for a keyword that precedes a statement.
1988 (c-after-conditional)))
1992 (goto-char id-start)
1993 (looking-at c-symbol-key)
1994 (c-put-font-lock-face (match-beginning 0) (match-end 0)
1995 c-label-face-name)))
1997 (goto-char continue-pos))))
2000 (c-lang-defconst c-basic-matchers-after
2001 "Font lock matchers for various things that should be fontified after
2002 generic casts and declarations are fontified. Used on level 2 and
2005 t `(;; Fontify the identifiers inside enum lists. (The enum type
2006 ;; name is handled by `c-simple-decl-matchers' or
2007 ;; `c-complex-decl-matchers' below.
2008 ,@(when (c-lang-const c-brace-id-list-kwds)
2009 `((,(c-make-font-lock-search-function
2012 (c-make-keywords-re nil (c-lang-const c-brace-id-list-kwds))
2014 ;; Disallow various common punctuation chars that can't come
2015 ;; before the '{' of the enum list, to avoid searching too far.
2018 '((c-font-lock-declarators limit t nil)
2020 (goto-char (match-end 0))
2021 (c-put-char-property (1- (point)) 'c-type
2023 (c-forward-syntactic-ws))
2024 (goto-char (match-end 0)))))))
2026 ;; Fontify labels in languages that supports them.
2027 ,@(when (c-lang-const c-label-key)
2029 `(;; Fontify labels after goto etc.
2030 ;; (Got three different interpretation levels here,
2031 ;; which makes it a bit complicated: 1) The backquote
2032 ;; stuff is expanded when compiled or loaded, 2) the
2033 ;; eval form is evaluated at font-lock setup (to
2034 ;; substitute c-label-face-name correctly), and 3) the
2035 ;; resulting structure is interpreted during
2038 . ,(let* ((c-before-label-re
2039 (c-make-keywords-re nil
2040 (c-lang-const c-before-label-kwds))))
2042 ,(concat "\\<\\(" c-before-label-re "\\)\\>"
2044 "\\(" ; identifier-offset
2045 (c-lang-const c-symbol-key)
2047 (list ,(+ (c-regexp-opt-depth c-before-label-re) 2)
2048 c-label-face-name nil t))))
2050 ;; Fontify normal labels.
2051 c-font-lock-labels))
2053 ;; Fontify the clauses after various keywords.
2054 ,@(when (or (c-lang-const c-type-list-kwds)
2055 (c-lang-const c-ref-list-kwds)
2056 (c-lang-const c-colon-type-list-kwds)
2057 (c-lang-const c-paren-type-kwds))
2058 `((,(c-make-font-lock-search-function
2060 (c-make-keywords-re nil
2061 (append (c-lang-const c-type-list-kwds)
2062 (c-lang-const c-ref-list-kwds)
2063 (c-lang-const c-colon-type-list-kwds)
2064 (c-lang-const c-paren-type-kwds)))
2066 '((c-fontify-types-and-refs ((c-promote-possible-types t))
2067 (c-forward-keyword-clause)
2068 (if (> (point) limit) (goto-char limit))))))))
2071 (c-lang-defconst c-matchers-1
2072 t (c-lang-const c-cpp-matchers))
2074 (c-lang-defconst c-matchers-2
2075 t (append (c-lang-const c-matchers-1)
2076 (c-lang-const c-basic-matchers-before)
2077 (c-lang-const c-simple-decl-matchers)
2078 (c-lang-const c-basic-matchers-after)))
2080 (c-lang-defconst c-matchers-3
2081 t (append (c-lang-const c-matchers-1)
2082 (c-lang-const c-basic-matchers-before)
2083 (c-lang-const c-complex-decl-matchers)
2084 (c-lang-const c-basic-matchers-after)))
2086 (defun c-compose-keywords-list (base-list)
2087 ;; Incorporate the font lock keyword lists according to
2088 ;; `c-doc-comment-style' on the given keyword list and return it.
2089 ;; This is used in the function bindings of the
2090 ;; `*-font-lock-keywords-*' symbols since we have to build the list
2091 ;; when font-lock is initialized.
2093 (unless (memq c-doc-face-name c-literal-faces)
2094 (setq c-literal-faces (cons c-doc-face-name c-literal-faces)))
2096 (let* ((doc-keywords
2097 (if (consp (car-safe c-doc-comment-style))
2098 (cdr-safe (or (assq c-buffer-is-cc-mode c-doc-comment-style)
2099 (assq 'other c-doc-comment-style)))
2100 c-doc-comment-style))
2101 (list (nconc (apply 'nconc
2105 (concat (symbol-name doc-style)
2106 "-font-lock-keywords"))))
2107 (cond ((fboundp sym)
2110 (append (eval sym) nil)))))
2111 (if (listp doc-keywords)
2113 (list doc-keywords))))
2116 ;; Kludge: If `c-font-lock-complex-decl-prepare' is on the list we
2117 ;; move it first since the doc comment font lockers might add
2118 ;; `c-type' text properties, so they have to be cleared before that.
2119 (when (memq 'c-font-lock-complex-decl-prepare list)
2120 (setq list (cons 'c-font-lock-complex-decl-prepare
2121 (delq 'c-font-lock-complex-decl-prepare
2122 (append list nil)))))
2126 (defun c-override-default-keywords (def-var)
2127 ;; This is used to override the value on a `*-font-lock-keywords'
2128 ;; variable only if it's nil or has the same value as one of the
2129 ;; `*-font-lock-keywords-*' variables. Older font-lock packages
2130 ;; define a default value for `*-font-lock-keywords' which we want
2131 ;; to override, but we should otoh avoid clobbering a user setting.
2132 ;; This heuristic for that isn't perfect, but I can't think of any
2135 ;; This function does not do any hidden buffer changes.
2136 (when (and (boundp def-var)
2137 (memq (symbol-value def-var)
2141 (let ((sym (intern (concat (symbol-name def-var)
2143 (and (boundp sym) (symbol-value sym))))
2144 '("-1" "-2" "-3")))))
2145 ;; The overriding is done by unbinding the variable so that the normal
2146 ;; defvar will install its default value later on.
2147 (makunbound def-var)))
2152 (c-override-default-keywords 'c-font-lock-keywords)
2154 (defconst c-font-lock-keywords-1 (c-lang-const c-matchers-1 c)
2155 "Minimal font locking for C mode.
2156 Fontifies only preprocessor directives (in addition to the syntactic
2157 fontification of strings and comments).")
2159 (defconst c-font-lock-keywords-2 (c-lang-const c-matchers-2 c)
2160 "Fast normal font locking for C mode.
2161 In addition to `c-font-lock-keywords-1', this adds fontification of
2162 keywords, simple types, declarations that are easy to recognize, the
2163 user defined types on `c-font-lock-extra-types', and the doc comment
2164 styles specified by `c-doc-comment-style'.")
2166 (defconst c-font-lock-keywords-3 (c-lang-const c-matchers-3 c)
2167 "Accurate normal font locking for C mode.
2168 Like `c-font-lock-keywords-2' but detects declarations in a more
2169 accurate way that works in most cases for arbitrary types without the
2170 need for `c-font-lock-extra-types'.")
2172 (defvar c-font-lock-keywords c-font-lock-keywords-3
2173 "Default expressions to highlight in C mode.")
2175 (defun c-font-lock-keywords-2 ()
2176 (c-compose-keywords-list c-font-lock-keywords-2))
2177 (defun c-font-lock-keywords-3 ()
2178 (c-compose-keywords-list c-font-lock-keywords-3))
2179 (defun c-font-lock-keywords ()
2180 (c-compose-keywords-list c-font-lock-keywords))
2185 (defun c-font-lock-c++-new (limit)
2186 ;; Assuming point is after a "new" word, check that it isn't inside
2187 ;; a string or comment, and if so try to fontify the type in the
2188 ;; allocation expression. Nil is always returned.
2190 ;; As usual, C++ takes the prize in coming up with a hard to parse
2193 (unless (c-skip-comments-and-strings limit)
2196 ;; A "new" keyword is followed by one to three expressions, where
2197 ;; the type is the middle one, and the only required part.
2198 (let (expr1-pos expr2-pos
2199 ;; Enable recording of identifier ranges in `c-forward-type'
2200 ;; etc for later fontification. Not using
2201 ;; `c-fontify-types-and-refs' here since the ranges should
2202 ;; be fontified selectively only when an allocation
2203 ;; expression is successfully recognized.
2204 (c-record-type-identifiers t)
2205 c-record-ref-identifiers
2206 ;; The font-lock package in Emacs is known to clobber
2207 ;; `parse-sexp-lookup-properties' (when it exists).
2208 (parse-sexp-lookup-properties
2209 (cc-eval-when-compile
2210 (boundp 'parse-sexp-lookup-properties))))
2211 (c-forward-syntactic-ws)
2213 ;; The first placement arglist is always parenthesized, if it
2215 (when (eq (char-after) ?\()
2216 (setq expr1-pos (1+ (point)))
2219 (scan-error (throw 'false-alarm t)))
2220 (c-forward-syntactic-ws))
2222 ;; The second expression is either a type followed by some "*" or
2223 ;; "[...]" or similar, or a parenthesized type followed by a full
2224 ;; identifierless declarator.
2225 (setq expr2-pos (1+ (point)))
2226 (cond ((eq (char-after) ?\())
2227 ((let ((c-promote-possible-types t))
2229 (t (setq expr2-pos nil)))
2234 ;; No second expression, so the first has to be a
2235 ;; parenthesized type.
2236 (goto-char expr1-pos)
2237 (let ((c-promote-possible-types t))
2240 ((eq (char-before expr2-pos) ?\()
2241 ;; Got two parenthesized expressions, so we have to look
2242 ;; closer at them to decide which is the type. No need to
2243 ;; handle `c-record-ref-identifiers' since all references
2244 ;; has already been handled by other fontification rules.
2245 (let (expr1-res expr2-res)
2247 (goto-char expr1-pos)
2248 (when (setq expr1-res (c-forward-type))
2250 (cc-eval-when-compile
2251 (concat (c-lang-const c-symbol-start c++)
2253 ;; There's something after the would-be type that
2254 ;; can't be there, so this is a placement arglist.
2255 (setq expr1-res nil)))
2257 (goto-char expr2-pos)
2258 (when (setq expr2-res (c-forward-type))
2260 (cc-eval-when-compile
2261 (concat (c-lang-const c-symbol-start c++)
2263 ;; There's something after the would-be type that can't
2264 ;; be there, so this is an initialization expression.
2265 (setq expr2-res nil))
2266 (when (and (c-go-up-list-forward)
2267 (progn (c-forward-syntactic-ws)
2268 (eq (char-after) ?\()))
2269 ;; If there's a third initialization expression
2270 ;; then the second one is the type, so demote the
2272 (setq expr1-res nil)))
2274 ;; We fontify the most likely type, with a preference for
2275 ;; the first argument since a placement arglist is more
2276 ;; unusual than an initializer.
2277 (cond ((memq expr1-res '(t known prefix)))
2278 ((memq expr2-res '(t known prefix)))
2279 ((eq expr1-res 'found)
2280 (let ((c-promote-possible-types t))
2281 (goto-char expr1-pos)
2283 ((eq expr2-res 'found)
2284 (let ((c-promote-possible-types t))
2285 (goto-char expr2-pos)
2287 ((and (eq expr1-res 'maybe) (not expr2-res))
2288 (let ((c-promote-possible-types t))
2289 (goto-char expr1-pos)
2291 ((and (not expr1-res) (eq expr2-res 'maybe))
2292 (let ((c-promote-possible-types t))
2293 (goto-char expr2-pos)
2295 ;; If both type matches are 'maybe then we're
2296 ;; too uncertain to promote either of them.
2299 ;; Fontify the type that now is recorded in
2300 ;; `c-record-type-identifiers', if any.
2301 (c-fontify-recorded-types-and-refs)))))
2304 (c-override-default-keywords 'c++-font-lock-keywords)
2306 (defconst c++-font-lock-keywords-1 (c-lang-const c-matchers-1 c++)
2307 "Minimal font locking for C++ mode.
2308 Fontifies only preprocessor directives (in addition to the syntactic
2309 fontification of strings and comments).")
2311 (defconst c++-font-lock-keywords-2 (c-lang-const c-matchers-2 c++)
2312 "Fast normal font locking for C++ mode.
2313 In addition to `c++-font-lock-keywords-1', this adds fontification of
2314 keywords, simple types, declarations that are easy to recognize, the
2315 user defined types on `c++-font-lock-extra-types', and the doc comment
2316 styles specified by `c-doc-comment-style'.")
2318 (defconst c++-font-lock-keywords-3 (c-lang-const c-matchers-3 c++)
2319 "Accurate normal font locking for C++ mode.
2320 Like `c++-font-lock-keywords-2' but detects declarations in a more
2321 accurate way that works in most cases for arbitrary types without the
2322 need for `c++-font-lock-extra-types'.")
2324 (defvar c++-font-lock-keywords c++-font-lock-keywords-3
2325 "Default expressions to highlight in C++ mode.")
2327 (defun c++-font-lock-keywords-2 ()
2328 (c-compose-keywords-list c++-font-lock-keywords-2))
2329 (defun c++-font-lock-keywords-3 ()
2330 (c-compose-keywords-list c++-font-lock-keywords-3))
2331 (defun c++-font-lock-keywords ()
2332 (c-compose-keywords-list c++-font-lock-keywords))
2337 (defun c-font-lock-objc-iip-decl ()
2338 ;; Assuming the point is after an "@interface", "@implementation",
2339 ;; "@protocol" declaration, fontify all the types in the directive.
2340 ;; Return t if the directive was fully recognized. Point will then
2341 ;; be at the end of it.
2343 (c-fontify-types-and-refs
2345 (c-promote-possible-types t)
2346 ;; Turn off recognition of angle bracket arglists while parsing
2347 ;; types here since the protocol reference list might then be
2348 ;; considered part of the preceding name or superclass-name.
2349 c-recognize-<>-arglists)
2352 ;; Handle the name of the class itself.
2353 (c-forward-syntactic-ws)
2354 (unless (c-forward-type) (throw 'break nil))
2356 ;; Look for ": superclass-name" or "( category-name )".
2357 (when (looking-at "[:\(]")
2358 (setq start-char (char-after))
2360 (c-forward-syntactic-ws)
2361 (unless (c-forward-type) (throw 'break nil))
2362 (when (eq start-char ?\()
2363 (unless (eq (char-after) ?\)) (throw 'break nil))
2365 (c-forward-syntactic-ws)))
2367 ;; Look for a protocol reference list.
2368 (when (if (eq (char-after) ?<)
2370 (setq c-recognize-<>-arglists t)
2371 (c-forward-<>-arglist t t))
2373 (c-put-char-property (1- (point)) 'c-type 'c-decl-end)
2376 (defun c-font-lock-objc-method ()
2377 ;; Assuming the point is after the + or - that starts an Objective-C
2378 ;; method declaration, fontify it. This must be done before normal
2379 ;; casts, declarations and labels are fontified since they will get
2380 ;; false matches in these things.
2382 (c-fontify-types-and-refs
2384 (c-promote-possible-types t))
2388 (c-forward-syntactic-ws)
2390 ;; An optional method type.
2391 (if (eq (char-after) ?\()
2394 (c-forward-syntactic-ws)
2396 (prog1 (c-go-up-list-forward)
2397 (c-forward-syntactic-ws)))
2400 ;; The name. The first time it's the first part of
2401 ;; the function name, the rest of the time it's an
2403 (looking-at c-symbol-key)
2405 (goto-char (match-end 0))
2406 (c-put-font-lock-face (match-beginning 0)
2409 'font-lock-function-name-face
2410 'font-lock-variable-name-face))
2411 (c-forward-syntactic-ws)
2413 ;; Another optional part of the function name.
2414 (when (looking-at c-symbol-key)
2415 (goto-char (match-end 0))
2416 (c-put-font-lock-face (match-beginning 0)
2418 'font-lock-function-name-face)
2419 (c-forward-syntactic-ws))
2421 ;; There's another argument if a colon follows.
2422 (eq (char-after) ?:)))
2426 (defun c-font-lock-objc-methods (limit)
2427 ;; Fontify method declarations in Objective-C. Nil is always
2430 (let (;; The font-lock package in Emacs is known to clobber
2431 ;; `parse-sexp-lookup-properties' (when it exists).
2432 (parse-sexp-lookup-properties
2433 (cc-eval-when-compile
2434 (boundp 'parse-sexp-lookup-properties))))
2440 (lambda (match-pos inside-macro)
2442 (c-font-lock-objc-method))))
2445 (c-override-default-keywords 'objc-font-lock-keywords)
2447 (defconst objc-font-lock-keywords-1 (c-lang-const c-matchers-1 objc)
2448 "Minimal font locking for Objective-C mode.
2449 Fontifies only compiler directives (in addition to the syntactic
2450 fontification of strings and comments).")
2452 (defconst objc-font-lock-keywords-2 (c-lang-const c-matchers-2 objc)
2453 "Fast normal font locking for Objective-C mode.
2454 In addition to `objc-font-lock-keywords-1', this adds fontification of
2455 keywords, simple types, declarations that are easy to recognize, the
2456 user defined types on `objc-font-lock-extra-types', and the doc
2457 comment styles specified by `c-doc-comment-style'.")
2459 (defconst objc-font-lock-keywords-3 (c-lang-const c-matchers-3 objc)
2460 "Accurate normal font locking for Objective-C mode.
2461 Like `objc-font-lock-keywords-2' but detects declarations in a more
2462 accurate way that works in most cases for arbitrary types without the
2463 need for `objc-font-lock-extra-types'.")
2465 (defvar objc-font-lock-keywords objc-font-lock-keywords-3
2466 "Default expressions to highlight in Objective-C mode.")
2468 (defun objc-font-lock-keywords-2 ()
2469 (c-compose-keywords-list objc-font-lock-keywords-2))
2470 (defun objc-font-lock-keywords-3 ()
2471 (c-compose-keywords-list objc-font-lock-keywords-3))
2472 (defun objc-font-lock-keywords ()
2473 (c-compose-keywords-list objc-font-lock-keywords))
2475 ;; Kludge to override the default value that
2476 ;; `objc-font-lock-extra-types' might have gotten from the font-lock
2477 ;; package. The value replaced here isn't relevant now anyway since
2478 ;; those types are builtin and therefore listed directly in
2479 ;; `c-primitive-type-kwds'.
2480 (when (equal (sort (append objc-font-lock-extra-types nil) 'string-lessp)
2481 '("BOOL" "Class" "IMP" "SEL"))
2482 (setq objc-font-lock-extra-types
2483 (cc-eval-when-compile (list (concat "[" c-upper "]\\sw*")))))
2488 (c-override-default-keywords 'java-font-lock-keywords)
2490 (defconst java-font-lock-keywords-1 (c-lang-const c-matchers-1 java)
2491 "Minimal font locking for Java mode.
2492 Fontifies nothing except the syntactic fontification of strings and
2495 (defconst java-font-lock-keywords-2 (c-lang-const c-matchers-2 java)
2496 "Fast normal font locking for Java mode.
2497 In addition to `java-font-lock-keywords-1', this adds fontification of
2498 keywords, simple types, declarations that are easy to recognize, the
2499 user defined types on `java-font-lock-extra-types', and the doc
2500 comment styles specified by `c-doc-comment-style'.")
2502 (defconst java-font-lock-keywords-3 (c-lang-const c-matchers-3 java)
2503 "Accurate normal font locking for Java mode.
2504 Like `java-font-lock-keywords-2' but detects declarations in a more
2505 accurate way that works in most cases for arbitrary types without the
2506 need for `java-font-lock-extra-types'.")
2508 (defvar java-font-lock-keywords java-font-lock-keywords-3
2509 "Default expressions to highlight in Java mode.")
2511 (defun java-font-lock-keywords-2 ()
2512 (c-compose-keywords-list java-font-lock-keywords-2))
2513 (defun java-font-lock-keywords-3 ()
2514 (c-compose-keywords-list java-font-lock-keywords-3))
2515 (defun java-font-lock-keywords ()
2516 (c-compose-keywords-list java-font-lock-keywords))
2521 (c-override-default-keywords 'idl-font-lock-keywords)
2523 (defconst idl-font-lock-keywords-1 (c-lang-const c-matchers-1 idl)
2524 "Minimal font locking for CORBA IDL mode.
2525 Fontifies nothing except the syntactic fontification of strings and
2528 (defconst idl-font-lock-keywords-2 (c-lang-const c-matchers-2 idl)
2529 "Fast normal font locking for CORBA IDL mode.
2530 In addition to `idl-font-lock-keywords-1', this adds fontification of
2531 keywords, simple types, declarations that are easy to recognize, the
2532 user defined types on `idl-font-lock-extra-types', and the doc comment
2533 styles specified by `c-doc-comment-style'.")
2535 (defconst idl-font-lock-keywords-3 (c-lang-const c-matchers-3 idl)
2536 "Accurate normal font locking for CORBA IDL mode.
2537 Like `idl-font-lock-keywords-2' but detects declarations in a more
2538 accurate way that works in most cases for arbitrary types without the
2539 need for `idl-font-lock-extra-types'.")
2541 (defvar idl-font-lock-keywords idl-font-lock-keywords-3
2542 "Default expressions to highlight in CORBA IDL mode.")
2544 (defun idl-font-lock-keywords-2 ()
2545 (c-compose-keywords-list idl-font-lock-keywords-2))
2546 (defun idl-font-lock-keywords-3 ()
2547 (c-compose-keywords-list idl-font-lock-keywords-3))
2548 (defun idl-font-lock-keywords ()
2549 (c-compose-keywords-list idl-font-lock-keywords))
2554 (c-override-default-keywords 'pike-font-lock-keywords)
2556 (defconst pike-font-lock-keywords-1 (c-lang-const c-matchers-1 pike)
2557 "Minimal font locking for Pike mode.
2558 Fontifies only preprocessor directives (in addition to the syntactic
2559 fontification of strings and comments).")
2561 (defconst pike-font-lock-keywords-2 (c-lang-const c-matchers-2 pike)
2562 "Fast normal font locking for Pike mode.
2563 In addition to `pike-font-lock-keywords-1', this adds fontification of
2564 keywords, simple types, declarations that are easy to recognize, the
2565 user defined types on `pike-font-lock-extra-types', and the doc
2566 comment styles specified by `c-doc-comment-style'.")
2568 (defconst pike-font-lock-keywords-3 (c-lang-const c-matchers-3 pike)
2569 "Accurate normal font locking for Pike mode.
2570 Like `pike-font-lock-keywords-2' but detects declarations in a more
2571 accurate way that works in most cases for arbitrary types without the
2572 need for `pike-font-lock-extra-types'.")
2574 (defvar pike-font-lock-keywords pike-font-lock-keywords-3
2575 "Default expressions to highlight in Pike mode.")
2577 (defun pike-font-lock-keywords-2 ()
2578 (c-compose-keywords-list pike-font-lock-keywords-2))
2579 (defun pike-font-lock-keywords-3 ()
2580 (c-compose-keywords-list pike-font-lock-keywords-3))
2581 (defun pike-font-lock-keywords ()
2582 (c-compose-keywords-list pike-font-lock-keywords))
2587 (defun c-font-lock-doc-comments (prefix limit keywords)
2588 ;; Fontify the comments between the point and LIMIT whose start
2589 ;; matches PREFIX with `c-doc-face-name'. Assumes comments have been
2590 ;; fontified with `font-lock-comment-face' already. nil is always
2593 ;; After the fontification of a matching comment, fontification
2594 ;; according to KEYWORDS is applied inside it. It's a list like
2595 ;; `font-lock-keywords' except that anchored matches and eval
2596 ;; clauses aren't supported and that some abbreviated forms can't be
2597 ;; used. The buffer is narrowed to the comment while KEYWORDS is
2598 ;; applied; leading comment starters are included but trailing
2599 ;; comment enders for block comment are not.
2601 ;; Note that faces added through KEYWORDS should never replace the
2602 ;; existing `c-doc-face-name' face since the existence of that face
2603 ;; is used as a flag in other code to skip comments.
2605 (let (comment-beg region-beg)
2606 (if (eq (get-text-property (point) 'face)
2607 'font-lock-comment-face)
2608 ;; Handle the case when the fontified region starts inside a
2610 (let ((range (c-literal-limits)))
2611 (setq region-beg (point))
2613 (goto-char (car range)))
2614 (when (looking-at prefix)
2615 (setq comment-beg (point)))))
2620 ;; Search for the prefix until a match is found at the start
2622 (while (when (re-search-forward prefix limit t)
2623 (setq comment-beg (match-beginning 0))
2624 (or (not (c-got-face-at comment-beg
2626 (and (/= comment-beg (point-min))
2627 (c-got-face-at (1- comment-beg)
2629 (setq comment-beg nil))
2630 (setq region-beg comment-beg))
2632 (if (eq (elt (parse-partial-sexp comment-beg (+ comment-beg 2)) 7) t)
2633 ;; Collect a sequence of doc style line comments.
2635 (goto-char comment-beg)
2637 (c-forward-single-comment)
2638 (skip-syntax-forward " ")
2640 (looking-at prefix))))
2641 (goto-char comment-beg)
2642 (c-forward-single-comment))
2643 (if (> (point) limit) (goto-char limit))
2644 (setq comment-beg nil)
2646 (let ((region-end (point))
2647 (keylist keywords) keyword matcher highlights)
2648 (c-put-font-lock-face region-beg region-end c-doc-face-name)
2650 ;; Narrow to the doc comment. Among other things, this
2651 ;; helps by making "^" match at the start of the comment.
2652 ;; Do not include a trailing block comment ender, though.
2653 (and (> region-end (1+ region-beg))
2654 (progn (goto-char region-end)
2656 (looking-at "\\*/"))
2657 (setq region-end (point)))
2658 (narrow-to-region region-beg region-end)
2661 (setq keyword (car keylist)
2662 keylist (cdr keylist)
2663 matcher (car keyword))
2664 (goto-char region-beg)
2665 (while (if (stringp matcher)
2666 (re-search-forward matcher region-end t)
2667 (funcall matcher region-end))
2668 (setq highlights (cdr keyword))
2669 (if (consp (car highlights))
2671 (font-lock-apply-highlight (car highlights))
2672 (setq highlights (cdr highlights)))
2673 (font-lock-apply-highlight highlights))))
2675 (goto-char region-end)))))
2677 (put 'c-font-lock-doc-comments 'lisp-indent-function 2)
2679 (defun c-find-invalid-doc-markup (regexp limit)
2680 ;; Used to fontify invalid markup in doc comments after the correct
2681 ;; ones have been fontified: Find the first occurence of REGEXP
2682 ;; between the point and LIMIT that only is fontified with
2683 ;; `c-doc-face-name'. If a match is found then submatch 0 surrounds
2684 ;; the first char and t is returned, otherwise nil is returned.
2686 (while (if (re-search-forward regexp limit t)
2687 (not (eq (get-text-property
2688 (setq start (match-beginning 0)) 'face)
2692 (store-match-data (list (copy-marker start)
2693 (copy-marker (1+ start))))
2696 (defconst javadoc-font-lock-doc-comments
2697 `(("{@[a-z]+[^}\n\r]*}" ; "{@foo ...}" markup.
2698 0 ,c-doc-markup-face-name prepend nil)
2699 ("^\\(/\\*\\)?[ \t*]*\\(@[a-z]+\\)" ; "@foo ..." markup.
2700 2 ,c-doc-markup-face-name prepend nil)
2701 (,(concat "</?\\sw" ; HTML tags.
2703 (concat "\\sw\\|\\s \\|[=\n\r*.:]\\|"
2704 "\"[^\"]*\"\\|'[^']*'")
2706 0 ,c-doc-markup-face-name prepend nil)
2707 ("&\\(\\sw\\|[.:]\\)+;" ; HTML entities.
2708 0 ,c-doc-markup-face-name prepend nil)
2709 ;; Fontify remaining markup characters as invalid. Note
2710 ;; that the Javadoc spec is hazy about when "@" is
2711 ;; allowed in non-markup use.
2713 (c-find-invalid-doc-markup "[<>&]\\|{@" limit))
2714 0 ,c-invalid-face-name prepend nil)))
2716 (defconst javadoc-font-lock-keywords
2718 (c-font-lock-doc-comments "/\\*\\*" limit
2719 javadoc-font-lock-doc-comments)))))
2721 (defconst autodoc-decl-keywords
2722 ;; Adorned regexp matching the keywords that introduce declarations
2724 (cc-eval-when-compile
2725 (c-make-keywords-re t '("@decl" "@elem" "@index" "@member") 'pike-mode)))
2727 (defconst autodoc-decl-type-keywords
2728 ;; Adorned regexp matching the keywords that are followed by a type.
2729 (cc-eval-when-compile
2730 (c-make-keywords-re t '("@elem" "@member") 'pike-mode)))
2732 (defun autodoc-font-lock-line-markup (limit)
2733 ;; Fontify all line oriented keywords between the point and LIMIT.
2734 ;; Nil is always returned.
2736 (let ((line-re (concat "^\\(\\(/\\*!\\|\\s *\\("
2737 c-current-comment-prefix
2738 "\\)\\)\\s *\\)@[A-Za-z_-]+\\(\\s \\|$\\)"))
2739 (markup-faces (list c-doc-markup-face-name c-doc-face-name)))
2741 (while (re-search-forward line-re limit t)
2742 (goto-char (match-end 1))
2744 (if (looking-at autodoc-decl-keywords)
2745 (let* ((kwd-pos (point))
2746 (start (match-end 1))
2750 (c-put-font-lock-face (point) pos markup-faces)
2752 ;; Put a declaration end mark at the markup keyword and
2753 ;; remove the faces from the rest of the line so that it
2754 ;; gets refontified as a declaration later on by
2755 ;; `c-font-lock-declarations'.
2756 (c-put-char-property (1- pos) 'c-type 'c-decl-end)
2761 (and (eq (char-before) ?@)
2763 (progn (forward-char)
2764 (skip-chars-forward " \t")
2765 (looking-at c-current-comment-prefix))))
2766 (goto-char (match-end 0))
2767 (c-remove-font-lock-face pos (1- end))
2768 (c-put-font-lock-face (1- end) end markup-faces)
2771 ;; Include the final newline in the removed area. This
2772 ;; has no visual effect but it avoids some tricky special
2773 ;; cases in the testsuite wrt the differences in string
2774 ;; fontification in Emacs vs XEmacs.
2775 (c-remove-font-lock-face pos (min (1+ (point)) (point-max)))
2777 ;; Must handle string literals explicitly inside the declaration.
2779 (while (re-search-forward
2780 "\"\\([^\\\"]\\|\\\\.\\)*\"\\|'\\([^\\']\\|\\\\.\\)*'"
2782 (c-put-font-lock-string-face (match-beginning 0)
2785 ;; Fontify types after keywords that always are followed
2788 (when (looking-at autodoc-decl-type-keywords)
2789 (c-fontify-types-and-refs ((c-promote-possible-types t))
2791 (c-forward-syntactic-ws)
2794 ;; Mark each whole line as markup, as long as the logical line
2797 (c-put-font-lock-face (point)
2798 (progn (end-of-line) (point))
2800 (and (eq (char-before) ?@)
2802 (progn (forward-char)
2803 (skip-chars-forward " \t")
2804 (looking-at c-current-comment-prefix))))
2805 (goto-char (match-end 0))))))
2809 (defconst autodoc-font-lock-doc-comments
2810 `(("@\\(\\w+{\\|\\[\\([^\]@\n\r]\\|@@\\)*\\]\\|[@}]\\|$\\)"
2812 0 ,c-doc-markup-face-name prepend nil)
2813 (autodoc-font-lock-line-markup)
2814 ;; Fontify remaining markup characters as invalid.
2816 (c-find-invalid-doc-markup "@" limit))
2817 0 ,c-invalid-face-name prepend nil)
2820 (defun autodoc-font-lock-keywords ()
2821 ;; Note that we depend on that `c-current-comment-prefix' has got
2822 ;; its proper value here.
2824 ;; The `c-type' text property with `c-decl-end' is used to mark the
2825 ;; end of the `autodoc-decl-keywords' occurrences to fontify the
2826 ;; following declarations.
2827 (setq c-type-decl-end-used t)
2830 (c-font-lock-doc-comments "/[*/]!" limit
2831 autodoc-font-lock-doc-comments)))))
2836 ;; Awk regexps written with help from Peter Galbraith
2837 ;; <galbraith@mixing.qc.dfo.ca>.
2838 ;; Take GNU Emacs's 'words out of the following regexp-opts. They dont work
2839 ;; in Xemacs 21.4.4. acm 2002/9/19.
2840 (eval-after-load "cc-awk" ; Evaluate while loading cc-fonts
2841 `(defconst awk-font-lock-keywords ; Evaluate after loading cc-awk
2842 ',(eval-when-compile ; Evaluate while compiling cc-fonts
2845 '("^[ \t]*\\(func\\(tion\\)?\\)\\>[ \t]*\\(\\sw+\\)?"
2846 (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t))
2852 '("ARGC" "ARGIND" "ARGV" "BINMODE" "CONVFMT" "ENVIRON"
2853 "ERRNO" "FIELDWIDTHS" "FILENAME" "FNR" "FS" "IGNORECASE"
2854 "LINT" "NF" "NR" "OFMT" "OFS" "ORS" "PROCINFO" "RLENGTH"
2855 "RS" "RSTART" "RT" "SUBSEP" "TEXTDOMAIN") t) "\\>")
2856 'font-lock-variable-name-face)
2858 ;; Special file names. (acm, 2002/7/22)
2859 ;; The following regexp was created by first evaluating this in GNU Emacs 21.1:
2860 ;; (c-regexp-opt '("/dev/stdin" "/dev/stdout" "/dev/stderr" "/dev/fd/n" "/dev/pid"
2861 ;; "/dev/ppid" "/dev/pgrpid" "/dev/user") 'words)
2862 ;; , removing the "?:" from each "\\(?:" (for backward compatibility with older Emacsen)
2863 ;; , replacing the "n" in "dev/fd/n" with "[0-9]+"
2864 ;; , removing the unwanted \\< at the beginning, and finally filling out the
2865 ;; regexp so that a " must come before, and either a " or heuristic stuff after.
2866 ;; The surrounding quotes are fontified along with the filename, since, semantically,
2867 ;; they are an indivisible unit.
2868 '("\\(\"/dev/\\(fd/[0-9]+\\|p\\(\\(\\(gr\\)?p\\)?id\\)\\|\
2869 std\\(err\\|in\\|out\\)\\|user\\)\\)\\>\
2870 \\(\\(\"\\)\\|\\([^\"/\n\r][^\"\n\r]*\\)?$\\)"
2871 (1 font-lock-variable-name-face t)
2872 (8 font-lock-variable-name-face t t))
2873 ;; Do the same (almost) with
2874 ;; (c-regexp-opt '("/inet/tcp/lport/rhost/rport" "/inet/udp/lport/rhost/rport"
2875 ;; "/inet/raw/lport/rhost/rport") 'words)
2876 ;; This cannot be combined with the above pattern, because the match number
2877 ;; for the (optional) closing \" would then exceed 9.
2878 '("\\(\"/inet/\\(\\(raw\\|\\(tc\\|ud\\)p\\)/lport/rhost/rport\\)\\)\\>\
2879 \\(\\(\"\\)\\|\\([^\"/\n\r][^\"\n\r]*\\)?$\\)"
2880 (1 font-lock-variable-name-face t)
2881 (6 font-lock-variable-name-face t t))
2886 '("BEGIN" "END" "break" "continue" "delete" "do" "else"
2887 "exit" "for" "getline" "if" "in" "next" "nextfile"
2896 '("adump" "and" "asort" "atan2" "bindtextdomain" "close"
2897 "compl" "cos" "dcgettext" "exp" "extension" "fflush"
2898 "gensub" "gsub" "index" "int" "length" "log" "lshift"
2899 "match" "mktime" "or" "print" "printf" "rand" "rshift"
2900 "sin" "split" "sprintf" "sqrt" "srand" "stopme"
2901 "strftime" "strtonum" "sub" "substr" "system"
2902 "systime" "tolower" "toupper" "xor") t)
2904 0 c-preprocessor-face-name))
2906 ;; gawk debugging keywords. (acm, 2002/7/21)
2907 ;; (Removed, 2003/6/6. These functions are now fontified as built-ins)
2908 ;; (list (concat "\\<" (c-regexp-opt '("adump" "stopme") t) "\\>")
2909 ;; 0 'font-lock-warning-face)
2911 ;; User defined functions with an apparent spurious space before the
2912 ;; opening parenthesis. acm, 2002/5/30.
2913 `(,(concat "\\(\\w\\|_\\)" c-awk-escaped-nls* "[ \t]"
2914 c-awk-escaped-nls*-with-space* "(")
2915 (0 'font-lock-warning-face))
2917 ;; Space after \ in what looks like an escaped newline. 2002/5/31
2918 '("\\\\[ \t]+$" 0 font-lock-warning-face t)
2920 ;; Unbalanced string (") or regexp (/) delimiters. 2002/02/16.
2921 '("\\s|" 0 font-lock-warning-face t nil)
2922 ;; gawk 3.1 localizable strings ( _"translate me!"). 2002/5/21
2923 '("\\(_\\)\\s|" 1 font-lock-warning-face)
2924 '("\\(_\\)\\s\"" 1 font-lock-string-face) ; FIXME! not for XEmacs. 2002/10/6
2926 "Default expressions to highlight in AWK mode."))
2929 (cc-provide 'cc-fonts)
2931 ;;; arch-tag: 2f65f405-735f-4da5-8d4b-b957844c5203
2932 ;;; cc-fonts.el ends here