(ibuffer-backward-line, ibuffer-forward-line)
[emacs.git] / lisp / progmodes / cc-fonts.el
blob8a0ba6d19207d377e8970733d008498b31974fcc
1 ;;; cc-fonts.el --- font lock support for CC Mode
3 ;; Copyright (C) 2002, 03 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)
17 ;; any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with 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.
29 ;;; Commentary:
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
48 ;; to elisp).
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.
57 ;;; Code:
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.
73 (eval-when-compile
74 (let ((load-path
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)
78 load-path)))
79 (load "cc-bytecomp" nil t)))
81 (cc-require 'cc-defs)
82 (cc-require-when-compile 'cc-langs)
83 (cc-require 'cc-vars)
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
90 ;; cc-mode.el.
91 (provide 'cc-fonts)
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
139 ;; suite.)
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
154 ;; of XEmacs 21.
155 'font-lock-constant-face
156 c-label-face-name))
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
165 c-label-face-name))
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)
171 ;; XEmacs.
172 'font-lock-doc-string-face)
173 ((c-face-name-p 'font-lock-doc-face)
174 ;; Emacs 21 and later.
175 'font-lock-doc-face)
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
183 ;; suite.)
184 'font-lock-doc-markup-face
185 c-label-face-name))
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.
192 'c-invalid-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 "red"))
198 (((class color)) (:foreground "hotpink"))
199 (t (:inverse-video t)))
200 "Face used to highlight invalid syntax."
201 :group 'c-fonts))
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
214 ;; routines. :P
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
223 ;; simultaneously.
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))))
235 (eval-and-compile
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
245 ;; incorrectly.
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
272 ,@varlist)
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)
284 (while (progn
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
303 ;; significant.
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
318 ;; lambda easier.
319 (byte-compile
320 `(lambda (limit)
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))
329 (unless (progn
330 (goto-char (match-beginning 0))
331 (c-skip-comments-and-strings limit))
332 (goto-char -match-end-pos-)
333 ,@(mapcar
334 (lambda (highlight)
335 (if (integerp (car highlight))
336 (progn
337 (unless (nth 2 highlight)
338 (error
339 "The override flag must currently be set in %s"
340 highlight))
341 (when (nth 3 highlight)
342 (error
343 "The laxmatch flag may currently not be set in %s"
344 highlight))
345 `(save-match-data
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"
352 highlight))
353 `(progn
354 ,(nth 1 highlight)
355 (save-match-data ,(car highlight))
356 ,(nth 2 highlight))))
357 highlights))))
358 nil))))
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.
363 (let (elem)
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.
406 (,(byte-compile
407 `(lambda (limit)
408 (let ((beg-pos
409 (match-beginning ,(+ ncle-depth sws-depth 2)))
410 (end-pos
411 (1- (match-end ,(+ ncle-depth sws-depth 2)))))
412 (if (eq (char-after end-pos) ?>)
413 (progn
414 (c-mark-<-as-paren beg-pos)
415 (c-mark->-as-paren end-pos))
416 (c-clear-char-property beg-pos 'syntax-table)))
417 nil))))
419 ;; #define.
420 (,(c-make-font-lock-search-function
421 (concat
422 noncontinued-line-end
423 (c-lang-const c-opt-cpp-prefix)
424 "define\\>"
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
430 "\\|"
431 ;; Macro without arguments - a "variable".
432 "\\([^\(]\\|$\\)"
433 "\\)"))
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.
437 (save-restriction
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)
447 (while (and
448 (progn
449 (c-forward-syntactic-ws)
450 (looking-at c-symbol-key))
451 (progn
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) ?,)))
458 (forward-char)))
460 ;; "Variable".
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
478 ,(concat "\\<\\("
479 (c-regexp-opt
480 (c-lang-const c-cpp-defined-fns)
481 nil)
482 "\\)\\>"
483 "\\s *\(?")
484 limit 'move)
485 (c-put-font-lock-face (match-beginning 1)
486 (match-end 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
493 "\\("
494 (c-lang-const c-opt-cpp-prefix)
495 "[" (c-lang-const c-symbol-chars) "]+"
496 "\\)")
497 `(,(1+ ncle-depth) c-preprocessor-face-name t)))
500 ,@(when (c-major-mode-is 'pike-mode)
501 `((eval . (list "\\`#![^\n\r]*"
502 0 c-preprocessor-face-name))))
504 ;; Make hard spaces visible through an inverted `c-invalid-face-name'.
505 (eval . (list
506 "\240"
507 0 (progn
508 (unless (c-face-name-p 'c-nonbreakable-space-face)
509 (c-make-inverse-face c-invalid-face-name
510 'c-nonbreakable-space-face))
511 'c-nonbreakable-space-face)))
514 (defun c-font-lock-invalid-string ()
515 ;; Assuming the point is after the opening character of a string,
516 ;; fontify that char with `c-invalid-face-name' if the string
517 ;; decidedly isn't terminated properly. Assumes the string already
518 ;; is syntactically fontified.
519 (let ((end (1+ (c-point 'eol))))
520 (and (eq (get-text-property (point) 'face) 'font-lock-string-face)
521 (= (next-single-property-change (point) 'face nil end) end)
522 ;; We're at eol inside a string. The first check above is
523 ;; necessary in XEmacs since it doesn't fontify the string
524 ;; delimiters themselves. Thus an empty string won't have
525 ;; the string face anywhere.
526 (if (c-major-mode-is '(c-mode c++-mode objc-mode pike-mode))
527 ;; There's no \ before the newline.
528 (not (eq (char-before (1- end)) ?\\))
529 ;; Quoted newlines aren't supported.
531 (if (c-major-mode-is 'pike-mode)
532 ;; There's no # before the string, so newlines
533 ;; aren't allowed.
534 (not (eq (char-before (1- (point))) ?#))
536 (c-put-font-lock-face (1- (point)) (point) c-invalid-face-name))))
538 (c-lang-defconst c-basic-matchers-before
539 "Font lock matchers for basic keywords, labels, references and various
540 other easily recognizable things that should be fontified before generic
541 casts and declarations are fontified. Used on level 2 and higher."
543 ;; Note: `c-font-lock-declarations' assumes that no matcher here
544 ;; sets `font-lock-type-face' in languages where
545 ;; `c-recognize-<>-arglists' is set.
547 t `(;; Put a warning face on the opener of unclosed strings that
548 ;; can't span lines. Later font
549 ;; lock packages have a `font-lock-syntactic-face-function' for
550 ;; this, but it doesn't give the control we want since any
551 ;; fontification done inside the function will be
552 ;; unconditionally overridden.
553 ,(c-make-font-lock-search-function
554 ;; Match a char before the string starter to make
555 ;; `c-skip-comments-and-strings' work correctly.
556 (concat ".\\(" c-string-limit-regexp "\\)")
557 '((c-font-lock-invalid-string)))
559 ;; Fontify keyword constants.
560 ,@(when (c-lang-const c-constant-kwds)
561 (let ((re (c-make-keywords-re nil (c-lang-const c-constant-kwds))))
562 (if (c-major-mode-is 'pike-mode)
563 ;; No symbol is a keyword after "->" in Pike.
564 `((eval . (list ,(concat "\\(\\=\\|\\(\\=\\|[^-]\\)[^>]\\)"
565 "\\<\\(" re "\\)\\>")
566 3 c-constant-face-name)))
567 `((eval . (list ,(concat "\\<\\(" re "\\)\\>")
568 1 c-constant-face-name))))))
570 ;; Fontify all keywords except the primitive types.
571 ,(if (c-major-mode-is 'pike-mode)
572 ;; No symbol is a keyword after "->" in Pike.
573 `(,(concat "\\(\\=\\|\\(\\=\\|[^-]\\)[^>]\\)"
574 "\\<" (c-lang-const c-regular-keywords-regexp))
575 3 font-lock-keyword-face)
576 `(,(concat "\\<" (c-lang-const c-regular-keywords-regexp))
577 1 font-lock-keyword-face))
579 ;; Fontify leading identifiers in fully qualified names like
580 ;; "foo::bar" in languages that supports such things.
581 ,@(when (c-lang-const c-opt-identifier-concat-key)
582 `((,(byte-compile
583 ;; Must use a function here since we match longer
584 ;; than we want to move before doing a new search.
585 ;; This is not necessary for XEmacs >= 20 since it
586 ;; restarts the search from the end of the first
587 ;; highlighted submatch (something that causes
588 ;; problems in other places).
589 `(lambda (limit)
590 (while (re-search-forward
591 ,(concat "\\(\\<" ; 1
592 "\\(" (c-lang-const c-symbol-key) "\\)" ; 2
593 "[ \t\n\r\f\v]*"
594 (c-lang-const c-opt-identifier-concat-key)
595 "[ \t\n\r\f\v]*"
596 "\\)"
597 "\\("
598 (c-lang-const c-opt-after-id-concat-key)
599 "\\)")
600 limit t)
601 (unless (progn
602 (goto-char (match-beginning 0))
603 (c-skip-comments-and-strings limit))
604 (or (get-text-property (match-beginning 2) 'face)
605 (c-put-font-lock-face (match-beginning 2)
606 (match-end 2)
607 c-reference-face-name))
608 (goto-char (match-end 1)))))))))
610 ;; Fontify the special declarations in Objective-C.
611 ,@(when (c-major-mode-is 'objc-mode)
612 `(;; Fontify class names in the beginning of message expressions.
613 ,(c-make-font-lock-search-function
614 "\\["
615 '((c-fontify-types-and-refs ()
616 (c-forward-syntactic-ws limit)
617 (let ((start (point)))
618 ;; In this case we accept both primitive and known types.
619 (when (eq (c-forward-type) 'known)
620 (goto-char start)
621 (let ((c-promote-possible-types t))
622 (c-forward-type))))
623 (if (> (point) limit) (goto-char limit)))))
625 ;; The @interface/@implementation/@protocol directives.
626 (,(concat "\\<"
627 (c-regexp-opt
628 '("@interface" "@implementation" "@protocol")
630 "\\>")
631 (,(byte-compile
632 (lambda (limit)
633 (let (;; The font-lock package in Emacs is known to clobber
634 ;; `parse-sexp-lookup-properties' (when it exists).
635 (parse-sexp-lookup-properties
636 (cc-eval-when-compile
637 (boundp 'parse-sexp-lookup-properties))))
638 (save-restriction
639 (narrow-to-region (point-min) limit)
640 (c-font-lock-objc-iip-decl)))
641 nil))))))
644 (defun c-font-lock-complex-decl-prepare (limit)
645 ;; Called before any of the matchers in `c-complex-decl-matchers'.
646 ;; Nil is always returned.
648 ;;(message "c-font-lock-complex-decl-prepare %s %s" (point) limit)
650 ;; Clear the list of found types if we start from the start of the
651 ;; buffer, to make it easier to get rid of misspelled types and
652 ;; variables that has gotten recognized as types in malformed code.
653 (when (bobp)
654 (c-clear-found-types))
656 ;; Clear the c-type char properties in the region to recalculate
657 ;; them properly. This is necessary e.g. to handle constructs that
658 ;; might been required as declarations temporarily during editing.
659 ;; The interesting properties are anyway those put on the closest
660 ;; token before the region.
661 (c-clear-char-properties (point) limit 'c-type)
663 ;; Update `c-state-cache' to the beginning of the region. This will
664 ;; make `c-beginning-of-syntax' go faster when it's used later on,
665 ;; and it's near the point most of the time.
666 (c-parse-state)
668 ;; Check if the fontified region starts inside a declarator list so
669 ;; that `c-font-lock-declarators' should be called at the start.
670 (let ((prop (save-excursion
671 (c-backward-syntactic-ws)
672 (unless (bobp)
673 (c-get-char-property (1- (point)) 'c-type)))))
674 (when (memq prop '(c-decl-id-start c-decl-type-start))
675 (c-forward-syntactic-ws limit)
676 (c-font-lock-declarators limit t (eq prop 'c-decl-type-start))))
678 nil)
680 (defun c-font-lock-<>-arglists (limit)
681 ;; Fontify types and references in names containing angle bracket
682 ;; arglists from the point to LIMIT. This will also fontify cases
683 ;; like normal function calls on the form "foo (a < b, c > d)", but
684 ;; `c-font-lock-declarations' will undo that later. Nil is always
685 ;; returned.
687 (let (;; The font-lock package in Emacs is known to clobber
688 ;; `parse-sexp-lookup-properties' (when it exists).
689 (parse-sexp-lookup-properties
690 (cc-eval-when-compile
691 (boundp 'parse-sexp-lookup-properties)))
692 id-start id-end pos kwd-sym)
694 (while (and (< (point) limit)
695 (re-search-forward c-opt-<>-arglist-start limit t))
697 (setq id-start (match-beginning 1)
698 id-end (match-end 1)
699 pos (point))
701 (goto-char id-start)
702 (unless (c-skip-comments-and-strings limit)
703 (setq kwd-sym nil)
704 (if (or (not (eq (get-text-property id-start 'face)
705 'font-lock-keyword-face))
706 (when (looking-at c-opt-<>-sexp-key)
707 (setq kwd-sym (c-keyword-sym (match-string 1)))))
708 (progn
709 (goto-char (1- pos))
710 ;; Check for comment/string both at the identifier and
711 ;; at the "<".
712 (unless (c-skip-comments-and-strings limit)
714 (when (c-forward-<>-arglist (c-keyword-member kwd-sym
715 'c-<>-type-kwds)
717 (when (and c-opt-identifier-concat-key
718 (not (get-text-property id-start 'face)))
719 (c-forward-syntactic-ws)
720 (if (looking-at c-opt-identifier-concat-key)
721 (c-put-font-lock-face id-start id-end
722 c-reference-face-name)
723 (c-put-font-lock-face id-start id-end
724 'font-lock-type-face))))
726 (goto-char pos)))
727 (goto-char pos)))))
728 nil)
730 (defun c-font-lock-declarators (limit list types)
731 ;; Assuming the point is at the start of a declarator in a
732 ;; declaration, fontify it. If LIST is non-nil, fontify also all
733 ;; following declarators in a comma separated list (e.g. "foo" and
734 ;; "bar" in "int foo = 17, bar;"). Stop at LIMIT. If TYPES is
735 ;; non-nil, fontify all identifiers as types. Nil is always
736 ;; returned.
738 ;;(message "c-font-lock-declarators from %s to %s" (point) limit)
739 (c-fontify-types-and-refs
740 ((pos (point)) next-pos id-start id-end
741 paren-depth
742 id-face got-init
743 c-last-identifier-range
744 (separator-prop (if types 'c-decl-type-start 'c-decl-id-start)))
746 (while (and
748 (< (point) limit)
750 (let (got-identifier)
751 (setq paren-depth 0)
752 ;; Skip over type decl prefix operators. (Note similar
753 ;; code in `c-font-lock-declarations'.)
754 (while (and (looking-at c-type-decl-prefix-key)
755 (if (and (c-major-mode-is 'c++-mode)
756 (match-beginning 2))
757 ;; If the second submatch matches in C++ then
758 ;; we're looking at an identifier that's a
759 ;; prefix only if it specifies a member pointer.
760 (progn
761 (setq id-start (point))
762 (c-forward-name)
763 (if (looking-at "\\(::\\)")
764 ;; We only check for a trailing "::" and
765 ;; let the "*" that should follow be
766 ;; matched in the next round.
768 ;; It turned out to be the real identifier,
769 ;; so flag that and stop.
770 (setq got-identifier t)
771 nil))
773 (if (eq (char-after) ?\()
774 (progn
775 (setq paren-depth (1+ paren-depth))
776 (forward-char))
777 (goto-char (match-end 1)))
778 (c-forward-syntactic-ws))
780 ;; If we didn't pass the identifier above already, do it now.
781 (unless got-identifier
782 (setq id-start (point))
783 (c-forward-name))
784 (setq id-end (point))
786 (/= id-end pos))
788 ;; Skip out of the parens surrounding the identifier.
789 (or (= paren-depth 0)
790 (c-safe (goto-char (scan-lists (point) 1 paren-depth))))
792 (<= (point) limit)
794 ;; Search syntactically to the end of the declarator (";",
795 ;; ",", ")", ">" (for <> arglists), eob etc) or to the
796 ;; beginning of an initializer or function prototype ("="
797 ;; or "\\s\(").
798 (c-syntactic-re-search-forward
799 "[\];,\{\}\[\)>]\\|\\'\\|\\(=\\|\\(\\s\(\\)\\)" limit t t))
801 (setq next-pos (match-beginning 0)
802 id-face (if (match-beginning 2)
803 'font-lock-function-name-face
804 'font-lock-variable-name-face)
805 got-init (match-beginning 1))
807 (if types
808 ;; Register and fontify the identifer as a type.
809 (let ((c-promote-possible-types t))
810 (goto-char id-start)
811 (c-forward-type))
812 ;; Fontify the last symbol in the identifier if it isn't fontified
813 ;; already. The check is necessary only in certain cases where this
814 ;; function is used "sloppily", e.g. in `c-simple-decl-matchers'.
815 (when (and c-last-identifier-range
816 (not (get-text-property (car c-last-identifier-range)
817 'face)))
818 (c-put-font-lock-face (car c-last-identifier-range)
819 (cdr c-last-identifier-range)
820 id-face)))
822 (goto-char next-pos)
823 (setq pos nil)
824 (when list
825 ;; Jump past any initializer or function prototype to see if
826 ;; there's a ',' to continue at.
828 (cond ((eq id-face 'font-lock-function-name-face)
829 ;; Skip a parenthesized initializer (C++) or a function
830 ;; prototype.
831 (if (c-safe (c-forward-sexp 1) t)
832 (c-forward-syntactic-ws limit)
833 (goto-char limit)))
835 (got-init
836 ;; Skip an initializer expression.
837 (if (c-syntactic-re-search-forward "[;,]" limit 'move t)
838 (backward-char)))
840 (t (c-forward-syntactic-ws limit)))
842 ;; If a ',' is found we set pos to the next declarator and iterate.
843 (when (and (< (point) limit) (looking-at ","))
844 (c-put-char-property (point) 'c-type separator-prop)
845 (forward-char)
846 (c-forward-syntactic-ws limit)
847 (setq pos (point))))))
848 nil)
850 (defconst c-font-lock-maybe-decl-faces
851 ;; List of faces that might be put at the start of a type when
852 ;; `c-font-lock-declarations' runs. This needs to be evaluated to
853 ;; ensure that face name aliases in Emacs are resolved.
854 (list nil
855 font-lock-type-face
856 c-reference-face-name
857 font-lock-keyword-face))
859 ;; Macro used inside `c-font-lock-declarations'. It ought to be a
860 ;; defsubst or perhaps even a defun, but it contains lots of free
861 ;; variables that refer to things inside `c-font-lock-declarations'.
862 (defmacro c-fl-shift-type-backward (&optional short)
863 ;; `c-font-lock-declarations' can consume an arbitrary length list
864 ;; of types when parsing a declaration, which means that it
865 ;; sometimes consumes the identifier in the declaration as a type.
866 ;; This is used to "backtrack" and make the last type be treated
867 ;; as an identifier instead.
868 `(progn
869 ,(unless short
870 ;; These identifiers are bound only in the inner let.
871 '(setq identifier-type at-type
872 identifier-start type-start
873 identifier-end type-end))
874 (if (setq at-type (if (eq prev-at-type 'prefix)
876 prev-at-type))
877 (setq type-start prev-type-start
878 type-end prev-type-end)
879 (setq type-start start-pos
880 type-end start-pos))
881 ,(unless short
882 ;; These identifiers are bound only in the inner let.
883 '(setq start type-end
884 got-parens nil
885 got-identifier t
886 got-suffix t
887 got-suffix-after-parens t
888 paren-depth 0))))
890 (defun c-font-lock-declarations (limit)
891 ;; Fontify all the declarations and casts from the point to LIMIT.
892 ;; Assumes that strings and comments have been fontified already.
893 ;; Nil is always returned.
895 ;; This function can make hidden buffer changes, but the font-lock
896 ;; context covers that.
898 ;;(message "c-font-lock-declarations search from %s to %s" (point) limit)
900 (save-restriction
901 (let (start-pos
902 c-disallow-comma-in-<>-arglists
903 ;; Nonzero if the `c-decl-prefix-re' match is in an arglist context,
904 ;; as opposed to a statement-level context. The major difference is
905 ;; that "," works as declaration delimiter in an arglist context,
906 ;; whereas it only separates declarators in the same declaration in
907 ;; a statement context. If it's nonzero then the value is the
908 ;; matched char, e.g. ?\( or ?,.
909 arglist-match
910 ;; 'decl if we're in an arglist containing declarations (but if
911 ;; `c-recognize-paren-inits' is set it might also be an initializer
912 ;; arglist), '<> if the arglist is of angle bracket type, 'other if
913 ;; it's some other arglist, or nil if not in an arglist at all.
914 arglist-type
915 ;; Set to the result of `c-forward-type'.
916 at-type
917 ;; These record the start and end of the type or possible type found
918 ;; by `c-forward-type'. `type-start' is at the start of the first
919 ;; type token, and `type-end' is at the start of the first token
920 ;; after the type (and after any specifiers).
921 type-start type-end
922 ;; These store `at-type', `type-start' and `type-end' of the
923 ;; identifier before the one in those variables. The previous
924 ;; identifier might turn out to be the real type in a declaration if
925 ;; the last one has to be the declarator in it. If `prev-at-type'
926 ;; is nil then the other variables have undefined values.
927 prev-at-type prev-type-start prev-type-end
928 ;; Whether we've found a declaration or a cast. We might know this
929 ;; before we've found the type in it.
930 at-decl-or-cast
931 ;; Set when we need to back up to parse this as a declaration but
932 ;; not as a cast.
933 backup-if-not-cast
934 ;; Set if we've found a "typedef" specifier. The identifiers in the
935 ;; declaration are then fontified as types.
936 at-typedef
937 ;; Set if we've found a specifier that can start a declaration where
938 ;; there's no type.
939 maybe-typeless
940 ;; The position of the next token after the closing paren of the
941 ;; last fontified cast.
942 last-cast-end
943 ;; The same for the currently investigated cast.
944 cast-end
945 ;; The maximum of the end positions of all the checked type decl
946 ;; expressions in the successfully identified declarations. The
947 ;; position might be either before or after the syntactic whitespace
948 ;; following the last token in the type decl expression.
949 (max-type-decl-end 0)
950 ;; Same as `max-type-decl-*', but used when we're before
951 ;; `token-pos'.
952 (max-type-decl-end-before-token 0)
953 ;; Allow recording of identifier ranges in `c-forward-type' etc for
954 ;; later fontification. Not using `c-fontify-types-and-refs' here
955 ;; since the ranges should be fontified selectively only when a
956 ;; declaration or cast has been successfully recognized.
957 c-record-type-identifiers
958 c-record-ref-identifiers
959 ;; The font-lock package in Emacs is known to clobber
960 ;; `parse-sexp-lookup-properties' (when it exists).
961 (parse-sexp-lookup-properties
962 (cc-eval-when-compile
963 (boundp 'parse-sexp-lookup-properties))))
965 ;; Below we fontify a whole declaration even when it crosses the limit,
966 ;; to avoid gaps when lazy-lock fontifies the file a screenful at a
967 ;; time. That is however annoying during editing, e.g. the following is
968 ;; a common situation while the first line is being written:
970 ;; my_variable
971 ;; some_other_variable = 0;
973 ;; font-lock will put the limit at the beginning of the second line
974 ;; here, and if we go past it we'll fontify "my_variable" as a type and
975 ;; "some_other_variable" as an identifier, and the latter will not
976 ;; correct itself until the second line is changed. To avoid that we
977 ;; narrow to the limit if the region to fontify is a single line.
978 (when (<= limit (c-point 'bonl))
979 (narrow-to-region
980 (point-min)
981 (save-excursion
982 ;; Narrow after any operator chars following the limit though, since
983 ;; those characters can be useful in recognizing a declaration (in
984 ;; particular the '{' that opens a function body after the header).
985 (goto-char limit)
986 (skip-chars-forward c-nonsymbol-chars)
987 (point))))
989 (c-find-decl-spots
990 limit
991 c-identifier-start
992 c-font-lock-maybe-decl-faces
994 (lambda (match-pos inside-macro)
995 (catch 'false-alarm
996 ;; Don't do anything more if we're looking at a keyword
997 ;; that can't start a declaration.
998 (when (and (eq (get-text-property (point) 'face)
999 'font-lock-keyword-face)
1000 (looking-at c-not-decl-init-keywords))
1001 (throw 'false-alarm t))
1003 ;; Set `arglist-match' and `arglist-type'. Look for "<" for the
1004 ;; sake of C++-style template arglists.
1005 (setq arglist-match (char-before match-pos))
1006 (if (memq arglist-match '(?\( ?, ?\[ ?<))
1008 ;; Find out the type of the arglist.
1009 (if (<= match-pos (point-min))
1010 (setq arglist-type 'other)
1011 (let ((type (c-get-char-property (1- match-pos) 'c-type)))
1012 (cond ((eq type 'c-decl-arg-start)
1013 ;; Got a cached hit in a declaration arglist.
1014 (setq arglist-type 'decl))
1015 ((or (eq type 'c-<>-arg-sep)
1016 (eq arglist-match ?<))
1017 ;; Inside an angle bracket arglist.
1018 (setq arglist-type '<>))
1019 (type
1020 ;; Got a cached hit in some other type of arglist.
1021 (setq arglist-type 'other))
1022 ((if inside-macro
1023 (< match-pos max-type-decl-end-before-token)
1024 (< match-pos max-type-decl-end))
1025 ;; The point is within the range of a previously
1026 ;; encountered type decl expression, so the arglist
1027 ;; is probably one that contains declarations.
1028 ;; However, if `c-recognize-paren-inits' is set it
1029 ;; might also be an initializer arglist.
1030 (setq arglist-type 'decl)
1031 ;; The result of this check is cached with a char
1032 ;; property on the match token, so that we can look
1033 ;; it up again when refontifying single lines in a
1034 ;; multiline declaration.
1035 (c-put-char-property (1- match-pos)
1036 'c-type 'c-decl-arg-start))
1038 (setq arglist-type 'other)))))
1040 (setq arglist-match nil
1041 arglist-type nil))
1043 (setq at-type nil
1044 at-decl-or-cast nil
1045 backup-if-not-cast nil
1046 at-typedef nil
1047 maybe-typeless nil
1048 c-record-type-identifiers t
1049 c-record-ref-identifiers nil
1050 ;; `start-pos' is used below to point to the start of the
1051 ;; first type, i.e. after any leading specifiers. It might
1052 ;; also point at the beginning of the preceding syntactic
1053 ;; whitespace.
1054 start-pos (point)
1055 ;; If we're in a normal arglist context we don't want to
1056 ;; recognize commas in nested angle bracket arglists since
1057 ;; those commas could be part of our own arglist.
1058 c-disallow-comma-in-<>-arglists
1059 (and c-recognize-<>-arglists
1060 (eq arglist-type 'other)))
1062 (when (and c-disallow-comma-in-<>-arglists
1063 (/= arglist-match ?,))
1064 ;; We're standing at the start of a normal arglist so remove any
1065 ;; angle bracket arglists containing commas that's been
1066 ;; recognized inside it by the preceding slightly opportunistic
1067 ;; scan in `c-font-lock-<>-arglists'.
1068 (while (and (c-syntactic-re-search-forward
1069 c-opt-<>-arglist-start-in-paren nil t t)
1070 (match-beginning 1))
1071 (backward-char)
1072 (when (save-match-data
1073 (and (c-get-char-property (point) 'syntax-table)
1074 (not (c-forward-<>-arglist nil t))))
1075 (c-remove-font-lock-face (match-beginning 2) (match-end 2))))
1076 (goto-char start-pos))
1078 ;; Check for a type, but be prepared to skip over leading
1079 ;; specifiers like "static". Unknown symbols are treated as
1080 ;; possible types, but they could also be specifiers disguised
1081 ;; through macros like __INLINE__, so we recognize both types and
1082 ;; known specifiers after them too.
1083 (while (let ((start (point))
1084 (res (unless (eq at-type t)
1085 ;; Don't look for a type if we already found a
1086 ;; positive one; we only loop for the
1087 ;; `c-specifier-key' check then.
1088 (c-forward-type))))
1090 (when res
1091 ;; Found a known or possible type or a prefix of a known
1092 ;; type.
1094 (when at-type
1095 ;; Got two identifiers with nothing but whitespace
1096 ;; between them. That can only happen in
1097 ;; declarations.
1098 (setq at-decl-or-cast t)
1100 (when (eq at-type 'found)
1101 ;; If the previous identifier is a found type we
1102 ;; record it as a real one; it might be some sort of
1103 ;; alias for a prefix like "unsigned".
1104 (save-excursion
1105 (goto-char type-start)
1106 (let ((c-promote-possible-types t))
1107 (c-forward-type)))))
1109 (setq prev-at-type at-type
1110 prev-type-start type-start
1111 prev-type-end type-end
1112 at-type res
1113 type-start start
1114 type-end (point))
1116 ;; If the type isn't known we continue so that we'll
1117 ;; jump over all specifiers and type identifiers. The
1118 ;; reason to do this for a known type prefix is to make
1119 ;; things like "unsigned INT16" work.
1120 (setq res (not (eq res t))))
1122 (if (looking-at c-specifier-key)
1123 ;; Found a known specifier keyword. The specifier
1124 ;; keywords are restrictive, so we check for them
1125 ;; anywhere inside or around the type(s). We thereby
1126 ;; avoid having special cases for specifiers like MSVC
1127 ;; '__declspec' which can come after the type.
1128 (progn
1129 (setq at-decl-or-cast t)
1130 (let ((kwd-sym (c-keyword-sym (match-string 1))))
1131 (when (c-keyword-member
1132 kwd-sym 'c-typedef-decl-kwds)
1133 (setq at-typedef t))
1134 (when (c-keyword-member
1135 kwd-sym 'c-typeless-decl-kwds)
1136 (setq maybe-typeless t)))
1137 (c-forward-keyword-clause)
1138 ;; Move type-end forward if we've passed a type,
1139 ;; otherwise move start-pos forward.
1140 (if at-type
1141 (setq type-end (point))
1142 (setq start-pos (point))))
1144 res)))
1146 (cond
1147 ((eq at-type 'prefix)
1148 ;; A prefix type is itself a primitive type when it's not
1149 ;; followed by another type.
1150 (setq at-type t))
1152 ((not at-type)
1153 ;; Got no type but set things up to continue anyway to handle the
1154 ;; various cases when a declaration doesn't start with a type.
1155 (setq type-end start-pos))
1157 ((and (eq at-type 'maybe)
1158 (c-major-mode-is 'c++-mode))
1159 ;; If it's C++ then check if the last "type" ends on the form
1160 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
1161 ;; (con|de)structor.
1162 (save-excursion
1163 (let (name end-2 end-1)
1164 (goto-char type-end)
1165 (c-backward-syntactic-ws)
1166 (setq end-2 (point))
1167 (when (and
1168 (c-simple-skip-symbol-backward)
1169 (progn
1170 (setq name
1171 (buffer-substring-no-properties (point) end-2))
1172 ;; Cheating in the handling of syntactic ws below.
1173 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
1174 (progn
1175 (setq end-1 (point))
1176 (c-simple-skip-symbol-backward))
1177 (>= (point) type-start)
1178 (equal (buffer-substring-no-properties (point) end-1)
1179 name))
1180 ;; It is a (con|de)structor name. In that case the
1181 ;; declaration is typeless so zap out any preceding
1182 ;; identifier(s) that we might have taken as types.
1183 (goto-char type-start)
1184 (setq at-type nil
1185 prev-at-type nil
1186 type-end type-start))))))
1188 ;; Check for and step over a type decl expression after the thing
1189 ;; that is or might be a type. This can't be skipped since we need
1190 ;; the correct end position of the declarator for
1191 ;; `max-type-decl-end-*'.
1192 (let ((start (point)) (paren-depth 0) pos
1193 ;; True if there's a non-open-paren match of
1194 ;; `c-type-decl-prefix-key'.
1195 got-prefix
1196 ;; True if the declarator is surrounded by a parenthesis pair.
1197 got-parens
1198 ;; True if there is an identifier in the declarator.
1199 got-identifier
1200 ;; True if there's a non-close-paren match of
1201 ;; `c-type-decl-suffix-key'.
1202 got-suffix
1203 ;; True if there's a prefix or suffix match outside the
1204 ;; outermost paren pair that surrounds the declarator.
1205 got-prefix-before-parens
1206 got-suffix-after-parens
1207 ;; True if we've parsed the type decl to a token that
1208 ;; is known to end declarations in this context.
1209 at-decl-end
1210 ;; The earlier values of `at-type', `type-start' and
1211 ;; `type-end' if we've shifted the type backwards.
1212 identifier-type identifier-start identifier-end)
1213 (goto-char type-end)
1215 ;; Skip over type decl prefix operators. (Note similar code in
1216 ;; `c-font-lock-declarators'.)
1217 (while (and (looking-at c-type-decl-prefix-key)
1218 (if (and (c-major-mode-is 'c++-mode)
1219 (match-beginning 2))
1220 ;; If the second submatch matches in C++ then
1221 ;; we're looking at an identifier that's a prefix
1222 ;; only if it specifies a member pointer.
1223 (when (setq got-identifier (c-forward-name))
1224 (if (looking-at "\\(::\\)")
1225 ;; We only check for a trailing "::" and
1226 ;; let the "*" that should follow be
1227 ;; matched in the next round.
1228 (progn (setq got-identifier nil) t)
1229 ;; It turned out to be the real identifier,
1230 ;; so stop.
1231 nil))
1233 (if (eq (char-after) ?\()
1234 (progn
1235 (setq paren-depth (1+ paren-depth))
1236 (forward-char))
1237 (unless got-prefix-before-parens
1238 (setq got-prefix-before-parens (= paren-depth 0)))
1239 (setq got-prefix t)
1240 (goto-char (match-end 1)))
1241 (c-forward-syntactic-ws))
1242 (setq got-parens (> paren-depth 0))
1244 ;; Skip over an identifier.
1245 (or got-identifier
1246 (and (looking-at c-identifier-start)
1247 (setq got-identifier (c-forward-name))))
1249 ;; Skip over type decl suffix operators.
1250 (while (if (looking-at c-type-decl-suffix-key)
1251 (if (eq (char-after) ?\))
1252 (when (> paren-depth 0)
1253 (setq paren-depth (1- paren-depth))
1254 (forward-char)
1256 (when (if (save-match-data (looking-at "\\s\("))
1257 (c-safe (c-forward-sexp 1) t)
1258 (goto-char (match-end 1))
1260 (unless got-suffix-after-parens
1261 (setq got-suffix-after-parens (= paren-depth 0)))
1262 (setq got-suffix t)))
1263 ;; No suffix matched. We might have matched the
1264 ;; identifier as a type and the open paren of a function
1265 ;; arglist as a type decl prefix. In that case we
1266 ;; should "backtrack": Reinterpret the last type as the
1267 ;; identifier, move out of the arglist and continue
1268 ;; searching for suffix operators.
1270 ;; Do this even if there's no preceding type, to cope
1271 ;; with old style function declarations in K&R C,
1272 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
1273 ;; style declarations. That isn't applicable in an
1274 ;; arglist context, though.
1275 (when (and (= paren-depth 1)
1276 (not got-prefix-before-parens)
1277 (not (eq at-type t))
1278 (or prev-at-type
1279 maybe-typeless
1280 (when c-recognize-typeless-decls
1281 (not arglist-type)))
1282 (setq pos (c-up-list-forward (point)))
1283 (eq (char-before pos) ?\)))
1284 (c-fl-shift-type-backward)
1285 (goto-char pos)
1287 (c-forward-syntactic-ws))
1289 (when (and maybe-typeless
1290 (not got-identifier)
1291 (not got-prefix)
1292 at-type
1293 (not (eq at-type t)))
1294 ;; Have found no identifier but `c-typeless-decl-kwds' has
1295 ;; matched so we know we're inside a declaration. The
1296 ;; preceding type must be the identifier instead.
1297 (c-fl-shift-type-backward))
1299 (setq
1300 at-decl-or-cast
1301 (catch 'at-decl-or-cast
1303 (when (> paren-depth 0)
1304 ;; Encountered something inside parens that isn't matched by
1305 ;; the `c-type-decl-*' regexps, so it's not a type decl
1306 ;; expression. Try to skip out to the same paren depth to
1307 ;; not confuse the cast check below.
1308 (c-safe (goto-char (scan-lists (point) 1 paren-depth)))
1309 (throw 'at-decl-or-cast nil))
1311 (setq at-decl-end
1312 (looking-at (cond ((eq arglist-type '<>) "[,>]")
1313 (arglist-type "[,\)]")
1314 (t "[,;]"))))
1316 ;; Now we've collected info about various characteristics of
1317 ;; the construct we're looking at. Below follows a decision
1318 ;; tree based on that. It's ordered to check more certain
1319 ;; signs before less certain ones.
1321 (if got-identifier
1322 (progn
1324 (when (and (or at-type maybe-typeless)
1325 (not (or got-prefix got-parens)))
1326 ;; Got another identifier directly after the type, so
1327 ;; it's a declaration.
1328 (throw 'at-decl-or-cast t))
1330 (when (and got-parens
1331 (not got-prefix)
1332 (not got-suffix-after-parens)
1333 (or prev-at-type maybe-typeless))
1334 ;; Got a declaration of the form "foo bar (gnu);"
1335 ;; where we've recognized "bar" as the type and "gnu"
1336 ;; as the declarator. In this case it's however more
1337 ;; likely that "bar" is the declarator and "gnu" a
1338 ;; function argument or initializer (if
1339 ;; `c-recognize-paren-inits' is set), since the parens
1340 ;; around "gnu" would be superfluous if it's a
1341 ;; declarator. Shift the type one step backward.
1342 (c-fl-shift-type-backward)))
1344 ;; Found no identifier.
1346 (if prev-at-type
1347 (when (or (= (point) start)
1348 (and got-suffix
1349 (not got-prefix)
1350 (not got-parens)))
1351 ;; Got two types after each other, so if this isn't a
1352 ;; cast then the latter probably is the identifier and
1353 ;; we should back up to the previous type.
1354 (setq backup-if-not-cast t)
1355 (throw 'at-decl-or-cast t))
1357 (when (eq at-type t)
1358 ;; If the type is known we know that there can't be any
1359 ;; identifier somewhere else, and it's only in
1360 ;; declarations in e.g. function prototypes and in casts
1361 ;; that the identifier may be left out.
1362 (throw 'at-decl-or-cast t))
1364 (when (= (point) start)
1365 ;; Only got a single identifier (parsed as a type so
1366 ;; far).
1367 (if (and
1368 ;; Check that the identifier isn't at the start of
1369 ;; an expression.
1370 at-decl-end
1371 (cond
1372 ((eq arglist-type 'decl)
1373 ;; Inside an arglist that contains declarations.
1374 ;; If K&R style declarations and parenthesis
1375 ;; style initializers aren't allowed then the
1376 ;; single identifier must be a type, else we
1377 ;; require that it's known or found (primitive
1378 ;; types are handled above).
1379 (or (and (not c-recognize-knr-p)
1380 (not c-recognize-paren-inits))
1381 (memq at-type '(known found))))
1382 ((eq arglist-type '<>)
1383 ;; Inside a template arglist. Accept known and
1384 ;; found types; other identifiers could just as
1385 ;; well be constants in C++.
1386 (memq at-type '(known found)))))
1387 (throw 'at-decl-or-cast t)
1388 (throw 'at-decl-or-cast nil))))
1390 (if (and
1391 got-parens
1392 (not got-prefix)
1393 (not arglist-type)
1394 (not (eq at-type t))
1396 prev-at-type
1397 maybe-typeless
1398 (when c-recognize-typeless-decls
1399 (or (not got-suffix)
1400 (not (looking-at
1401 c-after-suffixed-type-maybe-decl-key))))))
1402 ;; Got an empty paren pair and a preceding type that
1403 ;; probably really is the identifier. Shift the type
1404 ;; backwards to make the last one the identifier. This
1405 ;; is analogous to the "backtracking" done inside the
1406 ;; `c-type-decl-suffix-key' loop above.
1408 ;; Exception: In addition to the conditions in that
1409 ;; "backtracking" code, do not shift backward if we're
1410 ;; not looking at either `c-after-suffixed-type-decl-key'
1411 ;; or "[;,]". Since there's no preceding type, the
1412 ;; shift would mean that the declaration is typeless.
1413 ;; But if the regexp doesn't match then we will simply
1414 ;; fall through in the tests below and not recognize it
1415 ;; at all, so it's better to try it as an abstract
1416 ;; declarator instead.
1417 (c-fl-shift-type-backward)
1419 ;; Still no identifier.
1421 (when (and got-prefix (or got-parens got-suffix))
1422 ;; Require `got-prefix' together with either
1423 ;; `got-parens' or `got-suffix' to recognize it as an
1424 ;; abstract declarator: `got-parens' only is probably an
1425 ;; empty function call. `got-suffix' only can build an
1426 ;; ordinary expression together with the preceding
1427 ;; identifier which we've taken as a type. We could
1428 ;; actually accept on `got-prefix' only, but that can
1429 ;; easily occur temporarily while writing an expression
1430 ;; so we avoid that case anyway. We could do a better
1431 ;; job if we knew the point when the fontification was
1432 ;; invoked.
1433 (throw 'at-decl-or-cast t))))
1435 (when at-decl-or-cast
1436 ;; By now we've located the type in the declaration that we
1437 ;; know we're in.
1438 (throw 'at-decl-or-cast t))
1440 (when (and got-identifier
1441 (not arglist-type)
1442 (looking-at c-after-suffixed-type-decl-key)
1443 (if (and got-parens
1444 (not got-prefix)
1445 (not got-suffix)
1446 (not (eq at-type t)))
1447 ;; Shift the type backward in the case that
1448 ;; there's a single identifier inside parens.
1449 ;; That can only occur in K&R style function
1450 ;; declarations so it's more likely that it
1451 ;; really is a function call. Therefore we
1452 ;; only do this after
1453 ;; `c-after-suffixed-type-decl-key' has
1454 ;; matched.
1455 (progn (c-fl-shift-type-backward) t)
1456 got-suffix-after-parens))
1457 ;; A declaration according to
1458 ;; `c-after-suffixed-type-decl-key'.
1459 (throw 'at-decl-or-cast t))
1461 (when (and (or got-prefix (not got-parens))
1462 (memq at-type '(t known)))
1463 ;; It's a declaration if a known type precedes it and it
1464 ;; can't be a function call.
1465 (throw 'at-decl-or-cast t))
1467 ;; If we get here we can't tell if this is a type decl or a
1468 ;; normal expression by looking at it alone. (That's under
1469 ;; the assumption that normal expressions always can look like
1470 ;; type decl expressions, which isn't really true but the
1471 ;; cases where it doesn't hold are so uncommon (e.g. some
1472 ;; placements of "const" in C++) it's not worth the effort to
1473 ;; look for them.)
1475 (unless (or at-decl-end (looking-at "=[^=]"))
1476 ;; If this is a declaration it should end here or its
1477 ;; initializer(*) should start here, so check for allowed
1478 ;; separation tokens. Note that this rule doesn't work
1479 ;; e.g. with a K&R arglist after a function header.
1481 ;; *) Don't check for C++ style initializers using parens
1482 ;; since those already have been matched as suffixes.
1483 (throw 'at-decl-or-cast nil))
1485 ;; Below are tests that only should be applied when we're
1486 ;; certain to not have parsed halfway through an expression.
1488 (when (memq at-type '(t known))
1489 ;; The expression starts with a known type so treat it as a
1490 ;; declaration.
1491 (throw 'at-decl-or-cast t))
1493 (when (and (c-major-mode-is 'c++-mode)
1494 ;; In C++ we check if the identifier is a known
1495 ;; type, since (con|de)structors use the class name
1496 ;; as identifier. We've always shifted over the
1497 ;; identifier as a type and then backed up again in
1498 ;; this case.
1499 identifier-type
1500 (or (eq identifier-type 'found)
1501 (and (eq (char-after identifier-start) ?~)
1502 ;; `at-type' probably won't be 'found for
1503 ;; destructors since the "~" is then part
1504 ;; of the type name being checked against
1505 ;; the list of known types, so do a check
1506 ;; without that operator.
1507 (c-check-type (1+ identifier-start)
1508 identifier-end))))
1509 (throw 'at-decl-or-cast t))
1511 (if got-identifier
1512 (progn
1513 (when (and got-prefix-before-parens
1514 at-type
1515 (or at-decl-end (looking-at "=[^=]"))
1516 (not arglist-type)
1517 (not got-suffix))
1518 ;; Got something like "foo * bar;". Since we're not
1519 ;; inside an arglist it would be a meaningless
1520 ;; expression because the result isn't used. We
1521 ;; therefore choose to recognize it as a declaration.
1522 ;; Do not allow a suffix since it could then be a
1523 ;; function call.
1524 (throw 'at-decl-or-cast t))
1526 (when (and (or got-suffix-after-parens
1527 (looking-at "=[^=]"))
1528 (eq at-type 'found)
1529 (not (eq arglist-type 'other)))
1530 ;; Got something like "a (*b) (c);" or "a (b) = c;".
1531 ;; It could be an odd expression or it could be a
1532 ;; declaration. Treat it as a declaration if "a" has
1533 ;; been used as a type somewhere else (if it's a known
1534 ;; type we won't get here).
1535 (throw 'at-decl-or-cast t)))
1537 (when (and arglist-type
1538 (or got-prefix
1539 (and (eq arglist-type 'decl)
1540 (not c-recognize-paren-inits)
1541 (or got-parens got-suffix))))
1542 ;; Got a type followed by an abstract declarator. If
1543 ;; `got-prefix' is set it's something like "a *" without
1544 ;; anything after it. If `got-parens' or `got-suffix' is
1545 ;; set it's "a()", "a[]", "a()[]", or similar, which we
1546 ;; accept only if the context rules out expressions.
1547 (throw 'at-decl-or-cast t)))
1549 ;; If we had a complete symbol table here (which rules out
1550 ;; `c-found-types') we should return t due to the
1551 ;; disambiguation rule (in at least C++) that anything that
1552 ;; can be parsed as a declaration is a declaration. Now we're
1553 ;; being more defensive and prefer to highlight things like
1554 ;; "foo (bar);" as a declaration only if we're inside an
1555 ;; arglist that contains declarations.
1556 (eq arglist-type 'decl))))
1558 ;; Point is now after the type decl expression.
1560 (cond
1561 ;; Check for a cast.
1562 ((save-excursion
1563 (and
1564 c-cast-parens
1566 ;; Should be the first type/identifier in a cast paren.
1567 (memq arglist-match c-cast-parens)
1569 ;; The closing paren should follow.
1570 (progn
1571 (c-forward-syntactic-ws)
1572 (looking-at "\\s\)"))
1574 ;; There should be a primary expression after it.
1575 (let (pos)
1576 (forward-char)
1577 (c-forward-syntactic-ws)
1578 (setq cast-end (point))
1579 (and (looking-at c-primary-expr-regexp)
1580 (progn
1581 (setq pos (match-end 0))
1583 ;; Check if the expression begins with a prefix
1584 ;; keyword.
1585 (match-beginning 2)
1586 (if (match-beginning 1)
1587 ;; Expression begins with an ambiguous operator.
1588 ;; Treat it as a cast if it's a type decl or if
1589 ;; we've recognized the type somewhere else.
1590 (or at-decl-or-cast
1591 (memq at-type '(t known found)))
1592 ;; Unless it's a keyword, it's the beginning of a
1593 ;; primary expression.
1594 (not (looking-at c-keywords-regexp)))))
1595 ;; If `c-primary-expr-regexp' matched a nonsymbol
1596 ;; token, check that it matched a whole one so that we
1597 ;; don't e.g. confuse the operator '-' with '->'. It's
1598 ;; ok if it matches further, though, since it e.g. can
1599 ;; match the float '.5' while the operator regexp only
1600 ;; matches '.'.
1601 (or (not (looking-at c-nonsymbol-token-regexp))
1602 (<= (match-end 0) pos))))
1604 ;; There should either be a cast before it or something that
1605 ;; isn't an identifier or close paren.
1606 (/= match-pos 0)
1607 (progn
1608 (goto-char (1- match-pos))
1609 (or (eq (point) last-cast-end)
1610 (progn
1611 (c-backward-syntactic-ws)
1612 (if (< (skip-syntax-backward "w_") 0)
1613 ;; It's a symbol. Accept it only if it's one of
1614 ;; the keywords that can precede an expression
1615 ;; (without surrounding parens).
1616 (looking-at c-simple-stmt-key)
1617 (and
1618 ;; Check that it isn't a close paren (block close
1619 ;; is ok, though).
1620 (not (memq (char-before) '(?\) ?\])))
1621 ;; Check that it isn't a nonsymbol identifier.
1622 (not (c-on-identifier)))))))))
1624 ;; Handle the cast.
1625 (setq last-cast-end cast-end)
1626 (when (and at-type (not (eq at-type t)))
1627 (let ((c-promote-possible-types t))
1628 (goto-char type-start)
1629 (c-forward-type))))
1631 (at-decl-or-cast
1632 ;; We're at a declaration. Highlight the type and the following
1633 ;; declarators.
1635 (when backup-if-not-cast
1636 (c-fl-shift-type-backward t))
1638 (when (and (eq arglist-type 'decl) (looking-at ","))
1639 ;; Make sure to propagate the `c-decl-arg-start' property to
1640 ;; the next argument if it's set in this one, to cope with
1641 ;; interactive refontification.
1642 (c-put-char-property (point) 'c-type 'c-decl-arg-start))
1644 ;; Set `max-type-decl-end' or `max-type-decl-end-before-token'
1645 ;; under the assumption that we're after the first type decl
1646 ;; expression in the declaration now. That's not really true; we
1647 ;; could also be after a parenthesized initializer expression in
1648 ;; C++, but this is only used as a last resort to slant ambiguous
1649 ;; expression/declarations, and overall it's worth the risk to
1650 ;; occasionally fontify an expression as a declaration in an
1651 ;; initializer expression compared to getting ambiguous things in
1652 ;; normal function prototypes fontified as expressions.
1653 (if inside-macro
1654 (when (> (point) max-type-decl-end-before-token)
1655 (setq max-type-decl-end-before-token (point)))
1656 (when (> (point) max-type-decl-end)
1657 (setq max-type-decl-end (point))))
1659 (when (and at-type (not (eq at-type t)))
1660 (let ((c-promote-possible-types t))
1661 (goto-char type-start)
1662 (c-forward-type)))
1664 (goto-char type-end)
1666 (let ((decl-list
1667 (if arglist-type
1668 ;; Should normally not fontify a list of declarators
1669 ;; inside an arglist, but the first argument in the
1670 ;; ';' separated list of a "for" statement is an
1671 ;; exception.
1672 (when (and (eq arglist-match ?\() (/= match-pos 0))
1673 (save-excursion
1674 (goto-char (1- match-pos))
1675 (c-backward-syntactic-ws)
1676 (and (c-simple-skip-symbol-backward)
1677 (looking-at c-paren-stmt-key))))
1678 t)))
1680 ;; Fix the `c-decl-id-start' or `c-decl-type-start' property
1681 ;; before the first declarator if it's a list.
1682 ;; `c-font-lock-declarators' handles the rest.
1683 (when decl-list
1684 (save-excursion
1685 (c-backward-syntactic-ws)
1686 (unless (bobp)
1687 (c-put-char-property (1- (point)) 'c-type
1688 (if at-typedef
1689 'c-decl-type-start
1690 'c-decl-id-start)))))
1692 (c-font-lock-declarators (point-max) decl-list at-typedef)))
1695 ;; False alarm. Skip the fontification done below.
1696 (throw 'false-alarm t)))
1698 ;; A cast or declaration has been successfully identified, so do
1699 ;; all the fontification of types and refs that's been recorded by
1700 ;; the calls to `c-forward-type' and `c-forward-name' above.
1701 (c-fontify-recorded-types-and-refs)
1702 nil)))
1704 nil)))
1706 (c-lang-defconst c-simple-decl-matchers
1707 "Simple font lock matchers for types and declarations. These are used
1708 on level 2 only and so aren't combined with `c-complex-decl-matchers'."
1710 t `(;; Objective-C methods.
1711 ,@(when (c-major-mode-is 'objc-mode)
1712 `((,(c-lang-const c-opt-method-key)
1713 (,(byte-compile
1714 (lambda (limit)
1715 (let (;; The font-lock package in Emacs is known to clobber
1716 ;; `parse-sexp-lookup-properties' (when it exists).
1717 (parse-sexp-lookup-properties
1718 (cc-eval-when-compile
1719 (boundp 'parse-sexp-lookup-properties))))
1720 (save-restriction
1721 (narrow-to-region (point-min) limit)
1722 (c-font-lock-objc-method)))
1723 nil))
1724 (goto-char (match-end 1))))))
1726 ;; Fontify all type names and the identifiers in the
1727 ;; declarations they might start. Use eval here since
1728 ;; `c-known-type-key' gets its value from
1729 ;; `*-font-lock-extra-types' on mode init.
1730 (eval . (list ,(c-make-font-lock-search-function
1731 'c-known-type-key
1732 '(1 'font-lock-type-face t)
1733 '((c-font-lock-declarators limit t nil)
1734 (save-match-data
1735 (goto-char (match-end 1))
1736 (c-forward-syntactic-ws))
1737 (goto-char (match-end 1))))))
1739 ;; Fontify types preceded by `c-type-prefix-kwds' and the
1740 ;; identifiers in the declarations they might start.
1741 ,@(when (c-lang-const c-type-prefix-kwds)
1742 (let ((prefix-re (c-make-keywords-re nil
1743 (c-lang-const c-type-prefix-kwds))))
1744 `((,(c-make-font-lock-search-function
1745 (concat "\\<\\(" prefix-re "\\)"
1746 "[ \t\n\r\f\v]+"
1747 "\\(" (c-lang-const c-symbol-key) "\\)")
1748 `(,(+ (c-regexp-opt-depth prefix-re) 2)
1749 'font-lock-type-face t)
1750 '((c-font-lock-declarators limit t nil)
1751 (save-match-data
1752 (goto-char (match-end 2))
1753 (c-forward-syntactic-ws))
1754 (goto-char (match-end 2))))))))
1756 ;; Fontify special declarations that lacks a type.
1757 ,@(when (c-lang-const c-typeless-decl-kwds)
1758 `((,(c-make-font-lock-search-function
1759 (concat "\\<\\("
1760 (c-regexp-opt (c-lang-const c-typeless-decl-kwds))
1761 "\\)\\>")
1762 '((c-font-lock-declarators limit t nil)
1763 (save-match-data
1764 (goto-char (match-end 1))
1765 (c-forward-syntactic-ws))
1766 (goto-char (match-end 1)))))))
1769 (c-lang-defconst c-complex-decl-matchers
1770 "Complex font lock matchers for types and declarations. Used on level
1771 3 and higher."
1773 t `(;; Initialize some things before the search functions below.
1774 c-font-lock-complex-decl-prepare
1776 ;; Fontify angle bracket arglists like templates in C++.
1777 ,@(when (c-lang-const c-recognize-<>-arglists)
1778 `(c-font-lock-<>-arglists))
1780 ,@(if (c-major-mode-is 'objc-mode)
1781 ;; Fontify method declarations in Objective-C, but first
1782 ;; we have to put the `c-decl-end' `c-type' property on
1783 ;; all the @-style directives that haven't been handled in
1784 ;; `c-basic-matchers-before'.
1785 `(,(c-make-font-lock-search-function
1786 (c-make-keywords-re t
1787 ;; Exclude "@class" since that directive ends with a
1788 ;; semicolon anyway.
1789 (delete "@class"
1790 (append (c-lang-const c-protection-kwds)
1791 (c-lang-const c-other-decl-kwds)
1792 nil)))
1793 '((c-put-char-property (1- (match-end 1))
1794 'c-type 'c-decl-end)))
1796 c-font-lock-objc-methods)
1798 (when (c-lang-const c-opt-access-key)
1799 `(,(c-make-font-lock-search-function
1800 (c-lang-const c-opt-access-key)
1801 '((c-put-char-property (1- (match-end 0))
1802 'c-type 'c-decl-end))))))
1804 ;; Fontify all declarations and casts.
1805 c-font-lock-declarations
1807 ;; The first two rules here mostly find occurences that
1808 ;; `c-font-lock-declarations' has found already, but not
1809 ;; declarations containing blocks in the type (see note below).
1810 ;; It's also useful to fontify these everywhere to show e.g. when
1811 ;; a type keyword is accidentally used as an identifier.
1813 ;; Fontify basic types.
1814 ,(let ((re (c-make-keywords-re nil
1815 (c-lang-const c-primitive-type-kwds))))
1816 (if (c-major-mode-is 'pike-mode)
1817 ;; No symbol is a keyword after "->" in Pike.
1818 `(,(concat "\\(\\=\\|\\(\\=\\|[^-]\\)[^>]\\)"
1819 "\\<\\(" re "\\)\\>")
1820 3 font-lock-type-face)
1821 `(,(concat "\\<\\(" re "\\)\\>")
1822 1 'font-lock-type-face)))
1824 ;; Fontify types preceded by `c-type-prefix-kwds'.
1825 ,@(when (c-lang-const c-type-prefix-kwds)
1826 `((,(byte-compile
1827 `(lambda (limit)
1828 (c-fontify-types-and-refs
1829 ((c-promote-possible-types t)
1830 ;; The font-lock package in Emacs is known to clobber
1831 ;; `parse-sexp-lookup-properties' (when it exists).
1832 (parse-sexp-lookup-properties
1833 (cc-eval-when-compile
1834 (boundp 'parse-sexp-lookup-properties))))
1835 (save-restriction
1836 ;; Narrow to avoid going past the limit in
1837 ;; `c-forward-type'.
1838 (narrow-to-region (point) limit)
1839 (while (re-search-forward
1840 ,(concat "\\<\\("
1841 (c-make-keywords-re nil
1842 (c-lang-const c-type-prefix-kwds))
1843 "\\)\\>")
1844 limit t)
1845 (unless (c-skip-comments-and-strings limit)
1846 (c-forward-syntactic-ws)
1847 ;; Handle prefix declaration specifiers.
1848 (when (looking-at c-specifier-key)
1849 (c-forward-keyword-clause))
1850 ,(if (c-major-mode-is 'c++-mode)
1851 `(when (and (c-forward-type)
1852 (eq (char-after) ?=))
1853 ;; In C++ we additionally check for a "class
1854 ;; X = Y" construct which is used in
1855 ;; templates, to fontify Y as a type.
1856 (forward-char)
1857 (c-forward-syntactic-ws)
1858 (c-forward-type))
1859 `(c-forward-type))
1860 )))))))))
1862 ;; Fontify symbols after closing braces as declaration
1863 ;; identifiers under the assumption that they are part of
1864 ;; declarations like "class Foo { ... } foo;". It's too
1865 ;; expensive to check this accurately by skipping past the
1866 ;; brace block, so we use the heuristic that it's such a
1867 ;; declaration if the first identifier is on the same line as
1868 ;; the closing brace. `c-font-lock-declarations' will later
1869 ;; override it if it turns out to be an new declaration, but
1870 ;; it will be wrong if it's an expression (see the test
1871 ;; decls-8.cc).
1872 ,@(when (c-lang-const c-opt-block-decls-with-vars-key)
1873 `((,(c-make-font-lock-search-function
1874 (concat "}"
1875 (c-lang-const c-single-line-syntactic-ws)
1876 "\\(" ; 1 + c-single-line-syntactic-ws-depth
1877 (c-lang-const c-type-decl-prefix-key)
1878 "\\|"
1879 (c-lang-const c-symbol-key)
1880 "\\)")
1881 `((c-font-lock-declarators limit t nil)
1882 (progn
1883 (c-put-char-property (match-beginning 0) 'c-type
1884 'c-decl-id-start)
1885 (goto-char (match-beginning
1886 ,(1+ (c-lang-const
1887 c-single-line-syntactic-ws-depth)))))
1888 (goto-char (match-end 0)))))))
1890 ;; Fontify the type in C++ "new" expressions.
1891 ,@(when (c-major-mode-is 'c++-mode)
1892 `(("\\<new\\>"
1893 (c-font-lock-c++-new))))
1896 (defun c-font-lock-labels (limit)
1897 ;; Fontify all the declarations from the point to LIMIT. Assumes
1898 ;; that strings and comments have been fontified already. Nil is
1899 ;; always returned.
1901 ;; This function can make hidden buffer changes, but the font-lock
1902 ;; context covers that.
1904 (let (continue-pos id-start
1905 ;; The font-lock package in Emacs is known to clobber
1906 ;; `parse-sexp-lookup-properties' (when it exists).
1907 (parse-sexp-lookup-properties
1908 (cc-eval-when-compile
1909 (boundp 'parse-sexp-lookup-properties))))
1911 (while (re-search-forward ":[^:]" limit t)
1912 (setq continue-pos (point))
1913 (goto-char (match-beginning 0))
1914 (unless (c-skip-comments-and-strings limit)
1916 (c-backward-syntactic-ws)
1917 (and (setq id-start (c-on-identifier))
1919 (not (get-text-property id-start 'face))
1921 (progn
1922 (goto-char id-start)
1923 (c-backward-syntactic-ws)
1925 ;; Check for a char that precedes a statement.
1926 (memq (char-before) '(?\} ?\{ ?\;))
1927 ;; Check for a preceding label. We exploit the font
1928 ;; locking made earlier by this function.
1929 (and (eq (char-before) ?:)
1930 (progn
1931 (backward-char)
1932 (c-backward-syntactic-ws)
1933 (not (bobp)))
1934 (eq (get-text-property (1- (point)) 'face)
1935 c-label-face-name))
1936 ;; Check for a keyword that precedes a statement.
1937 (c-after-conditional)))
1939 (progn
1940 ;; Got a label.
1941 (goto-char id-start)
1942 (looking-at c-symbol-key)
1943 (c-put-font-lock-face (match-beginning 0) (match-end 0)
1944 c-label-face-name)))
1946 (goto-char continue-pos))))
1947 nil)
1949 (c-lang-defconst c-basic-matchers-after
1950 "Font lock matchers for various things that should be fontified after
1951 generic casts and declarations are fontified. Used on level 2 and
1952 higher."
1954 t `(;; Fontify the identifiers inside enum lists. (The enum type
1955 ;; name is handled by `c-simple-decl-matchers' or
1956 ;; `c-complex-decl-matchers' below.
1957 ,@(when (c-lang-const c-brace-id-list-kwds)
1958 `((,(c-make-font-lock-search-function
1959 (concat
1960 "\\<\\("
1961 (c-make-keywords-re nil (c-lang-const c-brace-id-list-kwds))
1962 "\\)\\>"
1963 ;; Disallow various common punctuation chars that can't come
1964 ;; before the '{' of the enum list, to avoid searching too far.
1965 "[^\]\[{}();,/#=]*"
1966 "{")
1967 '((c-font-lock-declarators limit t nil)
1968 (save-match-data
1969 (goto-char (match-end 0))
1970 (c-put-char-property (1- (point)) 'c-type
1971 'c-decl-id-start)
1972 (c-forward-syntactic-ws))
1973 (goto-char (match-end 0)))))))
1975 ;; Fontify labels in languages that supports them.
1976 ,@(when (c-lang-const c-label-key)
1978 `(;; Fontify labels after goto etc.
1979 ;; (Got three different interpretation levels here,
1980 ;; which makes it a bit complicated: 1) The backquote
1981 ;; stuff is expanded when compiled or loaded, 2) the
1982 ;; eval form is evaluated at font-lock setup (to
1983 ;; substitute c-label-face-name correctly), and 3) the
1984 ;; resulting structure is interpreted during
1985 ;; fontification.)
1986 (eval
1987 . ,(let* ((c-before-label-re
1988 (c-make-keywords-re nil
1989 (c-lang-const c-before-label-kwds))))
1990 `(list
1991 ,(concat "\\<\\(" c-before-label-re "\\)\\>"
1992 "\\s *"
1993 "\\(" ; identifier-offset
1994 (c-lang-const c-symbol-key)
1995 "\\)")
1996 (list ,(+ (c-regexp-opt-depth c-before-label-re) 2)
1997 c-label-face-name nil t))))
1999 ;; Fontify normal labels.
2000 c-font-lock-labels))
2002 ;; Fontify the clauses after various keywords.
2003 ,@(when (or (c-lang-const c-type-list-kwds)
2004 (c-lang-const c-ref-list-kwds)
2005 (c-lang-const c-colon-type-list-kwds)
2006 (c-lang-const c-paren-type-kwds))
2007 `((,(c-make-font-lock-search-function
2008 (concat "\\<\\("
2009 (c-make-keywords-re nil
2010 (append (c-lang-const c-type-list-kwds)
2011 (c-lang-const c-ref-list-kwds)
2012 (c-lang-const c-colon-type-list-kwds)
2013 (c-lang-const c-paren-type-kwds)))
2014 "\\)\\>")
2015 '((c-fontify-types-and-refs ((c-promote-possible-types t))
2016 (c-forward-keyword-clause)
2017 (if (> (point) limit) (goto-char limit))))))))
2020 (c-lang-defconst c-matchers-1
2021 t (c-lang-const c-cpp-matchers))
2023 (c-lang-defconst c-matchers-2
2024 t (append (c-lang-const c-matchers-1)
2025 (c-lang-const c-basic-matchers-before)
2026 (c-lang-const c-simple-decl-matchers)
2027 (c-lang-const c-basic-matchers-after)))
2029 (c-lang-defconst c-matchers-3
2030 t (append (c-lang-const c-matchers-1)
2031 (c-lang-const c-basic-matchers-before)
2032 (c-lang-const c-complex-decl-matchers)
2033 (c-lang-const c-basic-matchers-after)))
2035 (defun c-compose-keywords-list (base-list)
2036 ;; Incorporate the font lock keyword lists according to
2037 ;; `c-doc-comment-style' on the given keyword list and return it.
2038 ;; This is used in the function bindings of the
2039 ;; `*-font-lock-keywords-*' symbols since we have to build the list
2040 ;; when font-lock is initialized.
2042 (unless (memq c-doc-face-name c-literal-faces)
2043 (setq c-literal-faces (cons c-doc-face-name c-literal-faces)))
2045 (let* ((doc-keywords
2046 (if (consp (car-safe c-doc-comment-style))
2047 (cdr-safe (or (assq c-buffer-is-cc-mode c-doc-comment-style)
2048 (assq 'other c-doc-comment-style)))
2049 c-doc-comment-style))
2050 (list (nconc (apply 'nconc
2051 (mapcar
2052 (lambda (doc-style)
2053 (let ((sym (intern
2054 (concat (symbol-name doc-style)
2055 "-font-lock-keywords"))))
2056 (cond ((fboundp sym)
2057 (funcall sym))
2058 ((boundp sym)
2059 (append (eval sym) nil)))))
2060 (if (listp doc-keywords)
2061 doc-keywords
2062 (list doc-keywords))))
2063 base-list)))
2065 ;; Kludge: If `c-font-lock-complex-decl-prepare' is on the list we
2066 ;; move it first since the doc comment font lockers might add
2067 ;; `c-type' text properties so they have to be cleared before that.
2068 (when (memq 'c-font-lock-complex-decl-prepare list)
2069 (setq list (cons 'c-font-lock-complex-decl-prepare
2070 (delq 'c-font-lock-complex-decl-prepare
2071 (append list nil)))))
2073 list))
2075 (defun c-override-default-keywords (def-var)
2076 ;; This is used to override the value on a `*-font-lock-keywords'
2077 ;; variable only if it's nil or has the same value as one of the
2078 ;; `*-font-lock-keywords-*' variables. Older font-lock packages
2079 ;; define a default value for `*-font-lock-keywords' which we want
2080 ;; to override, but we should otoh avoid clobbering a user setting.
2081 ;; This heuristic for that isn't perfect, but I can't think of any
2082 ;; better. /mast
2084 ;; This function does not do any hidden buffer changes.
2085 (when (and (boundp def-var)
2086 (memq (symbol-value def-var)
2087 (cons nil
2088 (mapcar
2089 (lambda (suffix)
2090 (let ((sym (intern (concat (symbol-name def-var)
2091 suffix))))
2092 (and (boundp sym) (symbol-value sym))))
2093 '("-1" "-2" "-3")))))
2094 ;; The overriding is done by unbinding the variable so that the normal
2095 ;; defvar will install its default value later on.
2096 (makunbound def-var)))
2099 ;;; C.
2101 (c-override-default-keywords 'c-font-lock-keywords)
2103 (defconst c-font-lock-keywords-1 (c-lang-const c-matchers-1 c)
2104 "Minimal font locking for C mode.
2105 Fontifies only preprocessor directives (in addition to the syntactic
2106 fontification of strings and comments).")
2108 (defconst c-font-lock-keywords-2 (c-lang-const c-matchers-2 c)
2109 "Fast normal font locking for C mode.
2110 In addition to `c-font-lock-keywords-1', this adds fontification of
2111 keywords, simple types, declarations that are easy to recognize, the
2112 user defined types on `c-font-lock-extra-types', and the doc comment
2113 styles specified by `c-doc-comment-style'.")
2115 (defconst c-font-lock-keywords-3 (c-lang-const c-matchers-3 c)
2116 "Accurate normal font locking for C mode.
2117 Like `c-font-lock-keywords-2' but detects declarations in a more
2118 accurate way that works in most cases for arbitrary types without the
2119 need for `c-font-lock-extra-types'.")
2121 (defvar c-font-lock-keywords c-font-lock-keywords-3
2122 "Default expressions to highlight in C mode.")
2124 (defun c-font-lock-keywords-2 ()
2125 (c-compose-keywords-list c-font-lock-keywords-2))
2126 (defun c-font-lock-keywords-3 ()
2127 (c-compose-keywords-list c-font-lock-keywords-3))
2128 (defun c-font-lock-keywords ()
2129 (c-compose-keywords-list c-font-lock-keywords))
2132 ;;; C++.
2134 (defun c-font-lock-c++-new (limit)
2135 ;; Assuming point is after a "new" word, check that it isn't inside
2136 ;; a string or comment, and if so try to fontify the type in the
2137 ;; allocation expression. Nil is always returned.
2139 ;; As usual, C++ takes the prize in coming up with a hard to parse
2140 ;; syntax. :P
2142 (unless (c-skip-comments-and-strings limit)
2143 (save-excursion
2144 (catch 'false-alarm
2145 ;; A "new" keyword is followed by one to three expressions, where
2146 ;; the type is the middle one, and the only required part.
2147 (let (expr1-pos expr2-pos
2148 ;; Enable recording of identifier ranges in `c-forward-type'
2149 ;; etc for later fontification. Not using
2150 ;; `c-fontify-types-and-refs' here since the ranges should
2151 ;; be fontified selectively only when an allocation
2152 ;; expression is successfully recognized.
2153 (c-record-type-identifiers t)
2154 c-record-ref-identifiers
2155 ;; The font-lock package in Emacs is known to clobber
2156 ;; `parse-sexp-lookup-properties' (when it exists).
2157 (parse-sexp-lookup-properties
2158 (cc-eval-when-compile
2159 (boundp 'parse-sexp-lookup-properties))))
2160 (c-forward-syntactic-ws)
2162 ;; The first placement arglist is always parenthesized, if it
2163 ;; exists.
2164 (when (eq (char-after) ?\()
2165 (setq expr1-pos (1+ (point)))
2166 (condition-case nil
2167 (c-forward-sexp)
2168 (scan-error (throw 'false-alarm t)))
2169 (c-forward-syntactic-ws))
2171 ;; The second expression is either a type followed by some "*" or
2172 ;; "[...]" or similar, or a parenthesized type followed by a full
2173 ;; identifierless declarator.
2174 (setq expr2-pos (1+ (point)))
2175 (cond ((eq (char-after) ?\())
2176 ((let ((c-promote-possible-types t))
2177 (c-forward-type)))
2178 (t (setq expr2-pos nil)))
2180 (when expr1-pos
2181 (cond
2182 ((not expr2-pos)
2183 ;; No second expression, so the first has to be a
2184 ;; parenthesized type.
2185 (goto-char expr1-pos)
2186 (let ((c-promote-possible-types t))
2187 (c-forward-type)))
2189 ((eq (char-before expr2-pos) ?\()
2190 ;; Got two parenthesized expressions, so we have to look
2191 ;; closer at them to decide which is the type. No need to
2192 ;; handle `c-record-ref-identifiers' since all references
2193 ;; has already been handled by other fontification rules.
2194 (let (expr1-res expr2-res)
2196 (goto-char expr1-pos)
2197 (when (setq expr1-res (c-forward-type))
2198 (unless (looking-at
2199 (cc-eval-when-compile
2200 (concat (c-lang-const c-symbol-start c++)
2201 "\\|[*:\)\[]")))
2202 ;; There's something after the would-be type that
2203 ;; can't be there, so this is a placement arglist.
2204 (setq expr1-res nil)))
2206 (goto-char expr2-pos)
2207 (when (setq expr2-res (c-forward-type))
2208 (unless (looking-at
2209 (cc-eval-when-compile
2210 (concat (c-lang-const c-symbol-start c++)
2211 "\\|[*:\)\[]")))
2212 ;; There's something after the would-be type that can't
2213 ;; be there, so this is an initialization expression.
2214 (setq expr2-res nil))
2215 (when (and (c-go-up-list-forward)
2216 (progn (c-forward-syntactic-ws)
2217 (eq (char-after) ?\()))
2218 ;; If there's a third initialization expression
2219 ;; then the second one is the type, so demote the
2220 ;; first match.
2221 (setq expr1-res nil)))
2223 ;; We fontify the most likely type, with a preference for
2224 ;; the first argument since a placement arglist is more
2225 ;; unusual than an initializer.
2226 (cond ((memq expr1-res '(t known prefix)))
2227 ((memq expr2-res '(t known prefix)))
2228 ((eq expr1-res 'found)
2229 (let ((c-promote-possible-types t))
2230 (goto-char expr1-pos)
2231 (c-forward-type)))
2232 ((eq expr2-res 'found)
2233 (let ((c-promote-possible-types t))
2234 (goto-char expr2-pos)
2235 (c-forward-type)))
2236 ((and (eq expr1-res 'maybe) (not expr2-res))
2237 (let ((c-promote-possible-types t))
2238 (goto-char expr1-pos)
2239 (c-forward-type)))
2240 ((and (not expr1-res) (eq expr2-res 'maybe))
2241 (let ((c-promote-possible-types t))
2242 (goto-char expr2-pos)
2243 (c-forward-type)))
2244 ;; If both type matches are 'maybe then we're
2245 ;; too uncertain to promote either of them.
2246 )))))
2248 ;; Fontify the type that now is recorded in
2249 ;; `c-record-type-identifiers', if any.
2250 (c-fontify-recorded-types-and-refs)))))
2251 nil)
2253 (c-override-default-keywords 'c++-font-lock-keywords)
2255 (defconst c++-font-lock-keywords-1 (c-lang-const c-matchers-1 c++)
2256 "Minimal font locking for C++ mode.
2257 Fontifies only preprocessor directives (in addition to the syntactic
2258 fontification of strings and comments).")
2260 (defconst c++-font-lock-keywords-2 (c-lang-const c-matchers-2 c++)
2261 "Fast normal font locking for C++ mode.
2262 In addition to `c++-font-lock-keywords-1', this adds fontification of
2263 keywords, simple types, declarations that are easy to recognize, the
2264 user defined types on `c++-font-lock-extra-types', and the doc comment
2265 styles specified by `c-doc-comment-style'.")
2267 (defconst c++-font-lock-keywords-3 (c-lang-const c-matchers-3 c++)
2268 "Accurate normal font locking for C++ mode.
2269 Like `c++-font-lock-keywords-2' but detects declarations in a more
2270 accurate way that works in most cases for arbitrary types without the
2271 need for `c++-font-lock-extra-types'.")
2273 (defvar c++-font-lock-keywords c++-font-lock-keywords-3
2274 "Default expressions to highlight in C++ mode.")
2276 (defun c++-font-lock-keywords-2 ()
2277 (c-compose-keywords-list c++-font-lock-keywords-2))
2278 (defun c++-font-lock-keywords-3 ()
2279 (c-compose-keywords-list c++-font-lock-keywords-3))
2280 (defun c++-font-lock-keywords ()
2281 (c-compose-keywords-list c++-font-lock-keywords))
2284 ;;; Objective-C.
2286 (defun c-font-lock-objc-iip-decl ()
2287 ;; Assuming the point is after an "@interface", "@implementation",
2288 ;; "@protocol" declaration, fontify all the types in the directive.
2289 ;; Return t if the directive was fully recognized. Point will then
2290 ;; be at the end of it.
2292 (c-fontify-types-and-refs
2293 (start-char
2294 (c-promote-possible-types t)
2295 ;; Turn off recognition of angle bracket arglists while parsing
2296 ;; types here since the protocol reference list might then be
2297 ;; considered part of the preceding name or superclass-name.
2298 c-recognize-<>-arglists)
2299 (catch 'break
2301 ;; Handle the name of the class itself.
2302 (c-forward-syntactic-ws)
2303 (unless (c-forward-type) (throw 'break nil))
2305 ;; Look for ": superclass-name" or "( category-name )".
2306 (when (looking-at "[:\(]")
2307 (setq start-char (char-after))
2308 (forward-char)
2309 (c-forward-syntactic-ws)
2310 (unless (c-forward-type) (throw 'break nil))
2311 (when (eq start-char ?\()
2312 (unless (eq (char-after) ?\)) (throw 'break nil))
2313 (forward-char)
2314 (c-forward-syntactic-ws)))
2316 ;; Look for a protocol reference list.
2317 (when (if (eq (char-after) ?<)
2318 (progn
2319 (setq c-recognize-<>-arglists t)
2320 (c-forward-<>-arglist t t))
2322 (c-put-char-property (1- (point)) 'c-type 'c-decl-end)
2323 t))))
2325 (defun c-font-lock-objc-method ()
2326 ;; Assuming the point is after the + or - that starts an Objective-C
2327 ;; method declaration, fontify it. This must be done before normal
2328 ;; casts, declarations and labels are fontified since they will get
2329 ;; false matches in these things.
2331 (c-fontify-types-and-refs
2332 ((first t)
2333 (c-promote-possible-types t))
2335 (while (and
2336 (progn
2337 (c-forward-syntactic-ws)
2339 ;; An optional method type.
2340 (if (eq (char-after) ?\()
2341 (progn
2342 (forward-char)
2343 (c-forward-syntactic-ws)
2344 (c-forward-type)
2345 (prog1 (c-go-up-list-forward)
2346 (c-forward-syntactic-ws)))
2349 ;; The name. The first time it's the first part of
2350 ;; the function name, the rest of the time it's an
2351 ;; argument name.
2352 (looking-at c-symbol-key)
2353 (progn
2354 (goto-char (match-end 0))
2355 (c-put-font-lock-face (match-beginning 0)
2356 (point)
2357 (if first
2358 'font-lock-function-name-face
2359 'font-lock-variable-name-face))
2360 (c-forward-syntactic-ws)
2362 ;; Another optional part of the function name.
2363 (when (looking-at c-symbol-key)
2364 (goto-char (match-end 0))
2365 (c-put-font-lock-face (match-beginning 0)
2366 (point)
2367 'font-lock-function-name-face)
2368 (c-forward-syntactic-ws))
2370 ;; There's another argument if a colon follows.
2371 (eq (char-after) ?:)))
2372 (forward-char)
2373 (setq first nil))))
2375 (defun c-font-lock-objc-methods (limit)
2376 ;; Fontify method declarations in Objective-C. Nil is always
2377 ;; returned.
2379 (let (;; The font-lock package in Emacs is known to clobber
2380 ;; `parse-sexp-lookup-properties' (when it exists).
2381 (parse-sexp-lookup-properties
2382 (cc-eval-when-compile
2383 (boundp 'parse-sexp-lookup-properties))))
2385 (c-find-decl-spots
2386 limit
2387 "[-+]"
2389 (lambda (match-pos inside-macro)
2390 (forward-char)
2391 (c-font-lock-objc-method))))
2392 nil)
2394 (c-override-default-keywords 'objc-font-lock-keywords)
2396 (defconst objc-font-lock-keywords-1 (c-lang-const c-matchers-1 objc)
2397 "Minimal font locking for Objective-C mode.
2398 Fontifies only compiler directives (in addition to the syntactic
2399 fontification of strings and comments).")
2401 (defconst objc-font-lock-keywords-2 (c-lang-const c-matchers-2 objc)
2402 "Fast normal font locking for Objective-C mode.
2403 In addition to `objc-font-lock-keywords-1', this adds fontification of
2404 keywords, simple types, declarations that are easy to recognize, the
2405 user defined types on `objc-font-lock-extra-types', and the doc
2406 comment styles specified by `c-doc-comment-style'.")
2408 (defconst objc-font-lock-keywords-3 (c-lang-const c-matchers-3 objc)
2409 "Accurate normal font locking for Objective-C mode.
2410 Like `objc-font-lock-keywords-2' but detects declarations in a more
2411 accurate way that works in most cases for arbitrary types without the
2412 need for `objc-font-lock-extra-types'.")
2414 (defvar objc-font-lock-keywords objc-font-lock-keywords-3
2415 "Default expressions to highlight in Objective-C mode.")
2417 (defun objc-font-lock-keywords-2 ()
2418 (c-compose-keywords-list objc-font-lock-keywords-2))
2419 (defun objc-font-lock-keywords-3 ()
2420 (c-compose-keywords-list objc-font-lock-keywords-3))
2421 (defun objc-font-lock-keywords ()
2422 (c-compose-keywords-list objc-font-lock-keywords))
2424 ;; Kludge to override the default value that
2425 ;; `objc-font-lock-extra-types' might have gotten from the font-lock
2426 ;; package. The value replaced here isn't relevant now anyway since
2427 ;; those types are builtin and therefore listed directly in
2428 ;; `c-primitive-type-kwds'.
2429 (when (equal (sort (append objc-font-lock-extra-types nil) 'string-lessp)
2430 '("BOOL" "Class" "IMP" "SEL"))
2431 (setq objc-font-lock-extra-types
2432 (cc-eval-when-compile (list (concat "[" c-upper "]\\sw*")))))
2435 ;;; Java.
2437 (c-override-default-keywords 'java-font-lock-keywords)
2439 (defconst java-font-lock-keywords-1 (c-lang-const c-matchers-1 java)
2440 "Minimal font locking for Java mode.
2441 Fontifies nothing except the syntactic fontification of strings and
2442 comments.")
2444 (defconst java-font-lock-keywords-2 (c-lang-const c-matchers-2 java)
2445 "Fast normal font locking for Java mode.
2446 In addition to `java-font-lock-keywords-1', this adds fontification of
2447 keywords, simple types, declarations that are easy to recognize, the
2448 user defined types on `java-font-lock-extra-types', and the doc
2449 comment styles specified by `c-doc-comment-style'.")
2451 (defconst java-font-lock-keywords-3 (c-lang-const c-matchers-3 java)
2452 "Accurate normal font locking for Java mode.
2453 Like `java-font-lock-keywords-2' but detects declarations in a more
2454 accurate way that works in most cases for arbitrary types without the
2455 need for `java-font-lock-extra-types'.")
2457 (defvar java-font-lock-keywords java-font-lock-keywords-3
2458 "Default expressions to highlight in Java mode.")
2460 (defun java-font-lock-keywords-2 ()
2461 (c-compose-keywords-list java-font-lock-keywords-2))
2462 (defun java-font-lock-keywords-3 ()
2463 (c-compose-keywords-list java-font-lock-keywords-3))
2464 (defun java-font-lock-keywords ()
2465 (c-compose-keywords-list java-font-lock-keywords))
2468 ;;; CORBA IDL.
2470 (c-override-default-keywords 'idl-font-lock-keywords)
2472 (defconst idl-font-lock-keywords-1 (c-lang-const c-matchers-1 idl)
2473 "Minimal font locking for CORBA IDL mode.
2474 Fontifies nothing except the syntactic fontification of strings and
2475 comments.")
2477 (defconst idl-font-lock-keywords-2 (c-lang-const c-matchers-2 idl)
2478 "Fast normal font locking for CORBA IDL mode.
2479 In addition to `idl-font-lock-keywords-1', this adds fontification of
2480 keywords, simple types, declarations that are easy to recognize, the
2481 user defined types on `idl-font-lock-extra-types', and the doc comment
2482 styles specified by `c-doc-comment-style'.")
2484 (defconst idl-font-lock-keywords-3 (c-lang-const c-matchers-3 idl)
2485 "Accurate normal font locking for CORBA IDL mode.
2486 Like `idl-font-lock-keywords-2' but detects declarations in a more
2487 accurate way that works in most cases for arbitrary types without the
2488 need for `idl-font-lock-extra-types'.")
2490 (defvar idl-font-lock-keywords idl-font-lock-keywords-3
2491 "Default expressions to highlight in CORBA IDL mode.")
2493 (defun idl-font-lock-keywords-2 ()
2494 (c-compose-keywords-list idl-font-lock-keywords-2))
2495 (defun idl-font-lock-keywords-3 ()
2496 (c-compose-keywords-list idl-font-lock-keywords-3))
2497 (defun idl-font-lock-keywords ()
2498 (c-compose-keywords-list idl-font-lock-keywords))
2501 ;;; Pike.
2503 (c-override-default-keywords 'pike-font-lock-keywords)
2505 (defconst pike-font-lock-keywords-1 (c-lang-const c-matchers-1 pike)
2506 "Minimal font locking for Pike mode.
2507 Fontifies only preprocessor directives (in addition to the syntactic
2508 fontification of strings and comments).")
2510 (defconst pike-font-lock-keywords-2 (c-lang-const c-matchers-2 pike)
2511 "Fast normal font locking for Pike mode.
2512 In addition to `pike-font-lock-keywords-1', this adds fontification of
2513 keywords, simple types, declarations that are easy to recognize, the
2514 user defined types on `pike-font-lock-extra-types', and the doc
2515 comment styles specified by `c-doc-comment-style'.")
2517 (defconst pike-font-lock-keywords-3 (c-lang-const c-matchers-3 pike)
2518 "Accurate normal font locking for Pike mode.
2519 Like `pike-font-lock-keywords-2' but detects declarations in a more
2520 accurate way that works in most cases for arbitrary types without the
2521 need for `pike-font-lock-extra-types'.")
2523 (defvar pike-font-lock-keywords pike-font-lock-keywords-3
2524 "Default expressions to highlight in Pike mode.")
2526 (defun pike-font-lock-keywords-2 ()
2527 (c-compose-keywords-list pike-font-lock-keywords-2))
2528 (defun pike-font-lock-keywords-3 ()
2529 (c-compose-keywords-list pike-font-lock-keywords-3))
2530 (defun pike-font-lock-keywords ()
2531 (c-compose-keywords-list pike-font-lock-keywords))
2534 ;;; Doc comments.
2536 (defun c-font-lock-doc-comments (prefix limit keywords)
2537 ;; Fontify the comments between the point and LIMIT whose start
2538 ;; matches PREFIX with `c-doc-face-name'. Assumes comments have been
2539 ;; fontified with `font-lock-comment-face' already. nil is always
2540 ;; returned.
2542 ;; After the fontification of a matching comment, fontification
2543 ;; according to KEYWORDS is applied inside it. It's a list like
2544 ;; `font-lock-keywords' except that anchored matches and eval
2545 ;; clauses aren't supported and that some abbreviated forms can't be
2546 ;; used. The buffer is narrowed to the comment while KEYWORDS is
2547 ;; applied; leading comment starters are included but trailing
2548 ;; comment enders for block comment are not.
2550 ;; Note that faces added through KEYWORDS should never replace the
2551 ;; existing `c-doc-face-name' face since the existence of that face
2552 ;; is used as a flag in other code to skip comments.
2554 (let (comment-beg region-beg)
2555 (if (eq (get-text-property (point) 'face)
2556 'font-lock-comment-face)
2557 ;; Handle the case when the fontified region starts inside a
2558 ;; comment.
2559 (let ((range (c-literal-limits)))
2560 (setq region-beg (point))
2561 (when range
2562 (goto-char (car range)))
2563 (when (looking-at prefix)
2564 (setq comment-beg (point)))))
2566 (while (or
2567 comment-beg
2569 ;; Search for the prefix until a match is found at the start
2570 ;; of a comment.
2571 (while (when (re-search-forward prefix limit t)
2572 (setq comment-beg (match-beginning 0))
2573 (or (not (c-got-face-at comment-beg
2574 c-literal-faces))
2575 (and (/= comment-beg (point-min))
2576 (c-got-face-at (1- comment-beg)
2577 c-literal-faces))))
2578 (setq comment-beg nil))
2579 (setq region-beg comment-beg))
2581 (if (eq (elt (parse-partial-sexp comment-beg (+ comment-beg 2)) 7) t)
2582 ;; Collect a sequence of doc style line comments.
2583 (progn
2584 (goto-char comment-beg)
2585 (while (and (progn
2586 (c-forward-single-comment)
2587 (skip-syntax-forward " ")
2588 (< (point) limit))
2589 (looking-at prefix))))
2590 (goto-char comment-beg)
2591 (c-forward-single-comment))
2592 (if (> (point) limit) (goto-char limit))
2593 (setq comment-beg nil)
2595 (let ((region-end (point))
2596 (keylist keywords) keyword matcher highlights)
2597 (c-put-font-lock-face region-beg region-end c-doc-face-name)
2598 (save-restriction
2599 ;; Narrow to the doc comment. Among other things, this
2600 ;; helps by making "^" match at the start of the comment.
2601 ;; Do not include a trailing block comment ender, though.
2602 (and (> region-end (1+ region-beg))
2603 (progn (goto-char region-end)
2604 (backward-char 2)
2605 (looking-at "\\*/"))
2606 (setq region-end (point)))
2607 (narrow-to-region region-beg region-end)
2609 (while keylist
2610 (setq keyword (car keylist)
2611 keylist (cdr keylist)
2612 matcher (car keyword))
2613 (goto-char region-beg)
2614 (while (if (stringp matcher)
2615 (re-search-forward matcher region-end t)
2616 (funcall matcher region-end))
2617 (setq highlights (cdr keyword))
2618 (if (consp (car highlights))
2619 (while highlights
2620 (font-lock-apply-highlight (car highlights))
2621 (setq highlights (cdr highlights)))
2622 (font-lock-apply-highlight highlights))))
2624 (goto-char region-end)))))
2625 nil)
2626 (put 'c-font-lock-doc-comments 'lisp-indent-function 2)
2628 (defun c-find-invalid-doc-markup (regexp limit)
2629 ;; Used to fontify invalid markup in doc comments after the correct
2630 ;; ones have been fontified: Find the first occurence of REGEXP
2631 ;; between the point and LIMIT that only is fontified with
2632 ;; `c-doc-face-name'. If a match is found then submatch 0 surrounds
2633 ;; the first char and t is returned, otherwise nil is returned.
2634 (let (start)
2635 (while (if (re-search-forward regexp limit t)
2636 (not (eq (get-text-property
2637 (setq start (match-beginning 0)) 'face)
2638 c-doc-face-name))
2639 (setq start nil)))
2640 (when start
2641 (store-match-data (list (copy-marker start)
2642 (copy-marker (1+ start))))
2643 t)))
2645 (defun javadoc-font-lock-keywords ()
2646 (list
2647 (byte-compile
2648 `(lambda (limit)
2649 (c-font-lock-doc-comments "/\\*\\*" limit
2650 '(("{@[a-z]+[^}\n\r]*}" ; "{@foo ...}" markup.
2651 0 ,c-doc-markup-face-name prepend nil)
2652 ("^\\(/\\*\\)?[ \t*]*\\(@[a-z]+\\)" ; "@foo ..." markup.
2653 2 ,c-doc-markup-face-name prepend nil)
2654 (,(concat "</?\\sw" ; HTML tags.
2655 "\\("
2656 (concat "\\sw\\|\\s \\|[=\n\r*.:]\\|"
2657 "\"[^\"]*\"\\|'[^']*'")
2658 "\\)*>")
2659 0 ,c-doc-markup-face-name prepend nil)
2660 ("&\\(\\sw\\|[.:]\\)+;" ; HTML entities.
2661 0 ,c-doc-markup-face-name prepend nil)
2662 ;; Fontify remaining markup characters as invalid. Note
2663 ;; that the Javadoc spec is hazy about when "@" is allowed
2664 ;; in non-markup use.
2665 (,(lambda (limit)
2666 (c-find-invalid-doc-markup "[<>&]\\|{@" limit))
2667 0 ,c-invalid-face-name prepend nil)
2668 ))))))
2670 (defconst autodoc-decl-keywords
2671 ;; Adorned regexp matching the keywords that introduce declarations
2672 ;; in Pike Autodoc.
2673 (cc-eval-when-compile
2674 (c-make-keywords-re t '("@decl" "@elem" "@index" "@member") 'pike-mode)))
2676 (defconst autodoc-decl-type-keywords
2677 ;; Adorned regexp matching the keywords that are followed by a type.
2678 (cc-eval-when-compile
2679 (c-make-keywords-re t '("@elem" "@member") 'pike-mode)))
2681 (defun autodoc-font-lock-line-markup (limit)
2682 ;; Fontify all line oriented keywords between the point and LIMIT.
2683 ;; Nil is always returned.
2685 (let ((line-re (concat "^\\(\\(/\\*!\\|\\s *\\("
2686 c-current-comment-prefix
2687 "\\)\\)\\s *\\)@[A-Za-z_-]+\\(\\s \\|$\\)"))
2688 (markup-faces (list c-doc-markup-face-name c-doc-face-name)))
2690 (while (re-search-forward line-re limit t)
2691 (goto-char (match-end 1))
2693 (if (looking-at autodoc-decl-keywords)
2694 (let* ((kwd-pos (point))
2695 (start (match-end 1))
2696 (pos start)
2697 end)
2699 (c-put-font-lock-face (point) pos markup-faces)
2701 ;; Put a declaration end mark at the markup keyword and
2702 ;; remove the faces from the rest of the line so that it
2703 ;; gets refontified as a declaration later on by
2704 ;; `c-font-lock-declarations'.
2705 (c-put-char-property (1- pos) 'c-type 'c-decl-end)
2706 (goto-char pos)
2707 (while (progn
2708 (end-of-line)
2709 (setq end (point))
2710 (and (eq (char-before) ?@)
2711 (not (eobp))
2712 (progn (forward-char)
2713 (skip-chars-forward " \t")
2714 (looking-at c-current-comment-prefix))))
2715 (goto-char (match-end 0))
2716 (c-remove-font-lock-face pos (1- end))
2717 (c-put-font-lock-face (1- end) end markup-faces)
2718 (setq pos (point)))
2720 ;; Include the final newline in the removed area. This
2721 ;; has no visual effect but it avoids some tricky special
2722 ;; cases in the testsuite wrt the differences in string
2723 ;; fontification in Emacs vs XEmacs.
2724 (c-remove-font-lock-face pos (min (1+ (point)) (point-max)))
2726 ;; Must handle string literals explicitly inside the declaration.
2727 (goto-char start)
2728 (while (re-search-forward
2729 "\"\\([^\\\"]\\|\\\\.\\)*\"\\|'\\([^\\']\\|\\\\.\\)*'"
2730 end 'move)
2731 (c-put-font-lock-string-face (match-beginning 0)
2732 (point)))
2734 ;; Fontify types after keywords that always are followed
2735 ;; by them.
2736 (goto-char kwd-pos)
2737 (when (looking-at autodoc-decl-type-keywords)
2738 (c-fontify-types-and-refs ((c-promote-possible-types t))
2739 (goto-char start)
2740 (c-forward-syntactic-ws)
2741 (c-forward-type))))
2743 ;; Mark each whole line as markup, as long as the logical line
2744 ;; continues.
2745 (while (progn
2746 (c-put-font-lock-face (point)
2747 (progn (end-of-line) (point))
2748 markup-faces)
2749 (and (eq (char-before) ?@)
2750 (not (eobp))
2751 (progn (forward-char)
2752 (skip-chars-forward " \t")
2753 (looking-at c-current-comment-prefix))))
2754 (goto-char (match-end 0))))))
2756 nil)
2758 (defun autodoc-font-lock-keywords ()
2759 ;; Note that we depend on that `c-current-comment-prefix' has got
2760 ;; its proper value here.
2762 ;; The `c-type' text property with `c-decl-end' is used to mark the
2763 ;; end of the `autodoc-decl-keywords' occurrences to fontify the
2764 ;; following declarations.
2765 (setq c-type-decl-end-used t)
2767 (list
2768 (byte-compile
2769 `(lambda (limit)
2770 (c-font-lock-doc-comments "/[*/]!" limit
2771 '(("@\\(\\w+{\\|\\[\\([^\]@\n\r]\\|@@\\)*\\]\\|[@}]\\|$\\)"
2772 ;; In-text markup.
2773 0 ,c-doc-markup-face-name prepend nil)
2774 (autodoc-font-lock-line-markup)
2775 ;; Fontify remaining markup characters as invalid.
2776 (,(lambda (limit)
2777 (c-find-invalid-doc-markup "@" limit))
2778 0 ,c-invalid-face-name prepend nil)
2779 ))))))
2782 ;; AWK.
2784 ;; Awk regexps written with help from Peter Galbraith
2785 ;; <galbraith@mixing.qc.dfo.ca>.
2786 ;; Take GNU Emacs's 'words out of the following regexp-opts. They dont work
2787 ;; in Xemacs 21.4.4. acm 2002/9/19.
2788 (eval-after-load "cc-awk" ; Evaluate while loading cc-fonts
2789 `(defconst awk-font-lock-keywords ; Evaluate after loading cc-awk
2790 ',(eval-when-compile ; Evaluate while compiling cc-fonts
2791 (list
2792 ;; Function names.
2793 '("^[ \t]*\\(func\\(tion\\)?\\)\\>[ \t]*\\(\\sw+\\)?"
2794 (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t))
2796 ;; Variable names.
2797 (cons
2798 (concat "\\<"
2799 (c-regexp-opt
2800 '("ARGC" "ARGIND" "ARGV" "BINMODE" "CONVFMT" "ENVIRON"
2801 "ERRNO" "FIELDWIDTHS" "FILENAME" "FNR" "FS" "IGNORECASE"
2802 "LINT" "NF" "NR" "OFMT" "OFS" "ORS" "PROCINFO" "RLENGTH"
2803 "RS" "RSTART" "RT" "SUBSEP" "TEXTDOMAIN") t) "\\>")
2804 'font-lock-variable-name-face)
2806 ;; Special file names. (acm, 2002/7/22)
2807 ;; The following regexp was created by first evaluating this in GNU Emacs 21.1:
2808 ;; (c-regexp-opt '("/dev/stdin" "/dev/stdout" "/dev/stderr" "/dev/fd/n" "/dev/pid"
2809 ;; "/dev/ppid" "/dev/pgrpid" "/dev/user") 'words)
2810 ;; , removing the "?:" from each "\\(?:" (for backward compatibility with older Emacsen)
2811 ;; , replacing the "n" in "dev/fd/n" with "[0-9]+"
2812 ;; , removing the unwanted \\< at the beginning, and finally filling out the
2813 ;; regexp so that a " must come before, and either a " or heuristic stuff after.
2814 ;; The surrounding quotes are fontified along with the filename, since, semantically,
2815 ;; they are an indivisible unit.
2816 '("\\(\"/dev/\\(fd/[0-9]+\\|p\\(\\(\\(gr\\)?p\\)?id\\)\\|\
2817 std\\(err\\|in\\|out\\)\\|user\\)\\)\\>\
2818 \\(\\(\"\\)\\|\\([^\"/\n\r][^\"\n\r]*\\)?$\\)"
2819 (1 font-lock-variable-name-face t)
2820 (8 font-lock-variable-name-face t t))
2821 ;; Do the same (almost) with
2822 ;; (c-regexp-opt '("/inet/tcp/lport/rhost/rport" "/inet/udp/lport/rhost/rport"
2823 ;; "/inet/raw/lport/rhost/rport") 'words)
2824 ;; This cannot be combined with the above pattern, because the match number
2825 ;; for the (optional) closing \" would then exceed 9.
2826 '("\\(\"/inet/\\(\\(raw\\|\\(tc\\|ud\\)p\\)/lport/rhost/rport\\)\\)\\>\
2827 \\(\\(\"\\)\\|\\([^\"/\n\r][^\"\n\r]*\\)?$\\)"
2828 (1 font-lock-variable-name-face t)
2829 (6 font-lock-variable-name-face t t))
2831 ;; Keywords.
2832 (concat "\\<"
2833 (c-regexp-opt
2834 '("BEGIN" "END" "break" "continue" "delete" "do" "else"
2835 "exit" "for" "getline" "if" "in" "next" "nextfile"
2836 "return" "while")
2837 t) "\\>")
2839 ;; Builtins.
2840 `(eval . (list
2841 ,(concat
2842 "\\<"
2843 (c-regexp-opt
2844 '("adump" "and" "asort" "atan2" "bindtextdomain" "close"
2845 "compl" "cos" "dcgettext" "exp" "extension" "fflush"
2846 "gensub" "gsub" "index" "int" "length" "log" "lshift"
2847 "match" "mktime" "or" "print" "printf" "rand" "rshift"
2848 "sin" "split" "sprintf" "sqrt" "srand" "stopme"
2849 "strftime" "strtonum" "sub" "substr" "system"
2850 "systime" "tolower" "toupper" "xor") t)
2851 "\\>")
2852 0 c-preprocessor-face-name))
2854 ;; gawk debugging keywords. (acm, 2002/7/21)
2855 ;; (Removed, 2003/6/6. These functions are now fontified as built-ins)
2856 ;; (list (concat "\\<" (c-regexp-opt '("adump" "stopme") t) "\\>")
2857 ;; 0 'font-lock-warning-face)
2859 ;; User defined functions with an apparent spurious space before the
2860 ;; opening parenthesis. acm, 2002/5/30.
2861 `(,(concat "\\(\\w\\|_\\)" c-awk-escaped-nls* "[ \t]"
2862 c-awk-escaped-nls*-with-space* "(")
2863 (0 'font-lock-warning-face))
2865 ;; Space after \ in what looks like an escaped newline. 2002/5/31
2866 '("\\\\[ \t]+$" 0 font-lock-warning-face t)
2868 ;; Unbalanced string (") or regexp (/) delimiters. 2002/02/16.
2869 '("\\s|" 0 font-lock-warning-face t nil)
2870 ;; gawk 3.1 localizable strings ( _"translate me!"). 2002/5/21
2871 '("\\(_\\)\\s|" 1 font-lock-warning-face)
2872 '("\\(_\\)\\s\"" 1 font-lock-string-face) ; FIXME! not for XEmacs. 2002/10/6
2874 "Default expressions to highlight in AWK mode."))
2877 (cc-provide 'cc-fonts)
2879 ;;; cc-fonts.el ends here