lisp/*: Fix typos in docstrings and messages.
[emacs.git] / lisp / progmodes / cc-fonts.el
blobe9af813eeb300c5a91108ac513ce642b0b8aa326
1 ;;; cc-fonts.el --- font lock support for CC Mode
3 ;; Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 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 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;;; Commentary:
29 ;; Some comments on the use of faces:
31 ;; o `c-label-face-name' is either `font-lock-constant-face' (in
32 ;; Emacs), or `font-lock-reference-face'.
34 ;; o `c-constant-face-name', `c-reference-face-name' and
35 ;; `c-doc-markup-face-name' are essentially set up like
36 ;; `c-label-face-name'.
38 ;; o `c-preprocessor-face-name' is `font-lock-preprocessor-face' in
39 ;; XEmacs and - in lack of a closer equivalent -
40 ;; `font-lock-builtin-face' or `font-lock-reference-face' in Emacs.
42 ;; o `c-doc-face-name' is `font-lock-doc-string-face' in XEmacs,
43 ;; `font-lock-doc-face' in Emacs 21 and later, or
44 ;; `font-lock-comment-face' in older Emacs (that since source
45 ;; documentation are actually comments in these languages, as opposed
46 ;; to elisp).
48 ;; TBD: We should probably provide real faces for the above uses and
49 ;; instead initialize them from the standard faces.
51 ;;; Code:
53 ;; The faces that already have been put onto the text is tested in
54 ;; various places to direct further fontifications. For this to work,
55 ;; the following assumptions regarding the faces must hold (apart from
56 ;; the dependencies on the font locking order):
58 ;; o `font-lock-comment-face' and the face in `c-doc-face-name' is
59 ;; not used in anything but comments.
60 ;; o If any face (e.g. `c-doc-markup-face-name') but those above is
61 ;; used in comments, it doesn't replace them.
62 ;; o `font-lock-string-face' is not used in anything but string
63 ;; literals (single or double quoted).
64 ;; o `font-lock-keyword-face' and the face in `c-label-face-name' are
65 ;; never overlaid with other faces.
67 (eval-when-compile
68 (let ((load-path
69 (if (and (boundp 'byte-compile-dest-file)
70 (stringp byte-compile-dest-file))
71 (cons (file-name-directory byte-compile-dest-file) load-path)
72 load-path)))
73 (load "cc-bytecomp" nil t)))
75 (cc-require 'cc-defs)
76 (cc-require-when-compile 'cc-langs)
77 (cc-require 'cc-vars)
78 (cc-require 'cc-engine)
79 (cc-require-when-compile 'cc-awk) ; Change from cc-require, 2003/6/18 to
80 ;; prevent cc-awk being loaded when it's not needed. There is now a (require
81 ;; 'cc-awk) in (defun awk-mode ..).
83 ;; Avoid repeated loading through the eval-after-load directive in
84 ;; cc-mode.el.
85 (provide 'cc-fonts)
87 (cc-external-require 'font-lock)
89 (cc-bytecomp-defvar parse-sexp-lookup-properties) ; Emacs only.
91 ;; Need to declare these local symbols during compilation since
92 ;; they're referenced from lambdas in `byte-compile' calls that are
93 ;; executed at compile time. They don't need to have the proper
94 ;; definitions, though, since the generated functions aren't called
95 ;; during compilation.
96 (cc-bytecomp-defvar c-preprocessor-face-name)
97 (cc-bytecomp-defvar c-reference-face-name)
98 (cc-bytecomp-defun c-fontify-recorded-types-and-refs)
99 (cc-bytecomp-defun c-font-lock-declarators)
100 (cc-bytecomp-defun c-font-lock-objc-method)
101 (cc-bytecomp-defun c-font-lock-invalid-string)
104 ;; Note that font-lock in XEmacs doesn't expand face names as
105 ;; variables, so we have to use the (eval . FORM) in the font lock
106 ;; matchers wherever we use these alias variables.
108 (defconst c-preprocessor-face-name
109 (cond ((c-face-name-p 'font-lock-preprocessor-face)
110 ;; XEmacs has a font-lock-preprocessor-face.
111 'font-lock-preprocessor-face)
112 ((c-face-name-p 'font-lock-builtin-face)
113 ;; In Emacs font-lock-builtin-face has traditionally been
114 ;; used for preprocessor directives.
115 'font-lock-builtin-face)
117 'font-lock-reference-face)))
119 (cc-bytecomp-defvar font-lock-constant-face)
121 (defconst c-label-face-name
122 (cond ((c-face-name-p 'font-lock-label-face)
123 ;; If it happens to occur in the future. (Well, the more
124 ;; pragmatic reason is to get unique faces for the test
125 ;; suite.)
126 'font-lock-label-face)
127 ((and (c-face-name-p 'font-lock-constant-face)
128 (eq font-lock-constant-face 'font-lock-constant-face))
129 ;; Test both if font-lock-constant-face exists and that it's
130 ;; not an alias for something else. This is important since
131 ;; we compare already set faces in various places.
132 'font-lock-constant-face)
134 'font-lock-reference-face)))
136 (defconst c-constant-face-name
137 (if (and (c-face-name-p 'font-lock-constant-face)
138 (eq font-lock-constant-face 'font-lock-constant-face))
139 ;; This doesn't exist in some earlier versions of XEmacs 21.
140 'font-lock-constant-face
141 c-label-face-name))
143 (defconst c-reference-face-name
144 (with-no-warnings
145 (if (and (c-face-name-p 'font-lock-reference-face)
146 (eq font-lock-reference-face 'font-lock-reference-face))
147 ;; This is considered obsolete in Emacs, but it still maps well
148 ;; to this use. (Another reason to do this is to get unique
149 ;; faces for the test suite.)
150 'font-lock-reference-face
151 c-label-face-name)))
153 ;; This should not mapped to a face that also is used to fontify things
154 ;; that aren't comments or string literals.
155 (defconst c-doc-face-name
156 (cond ((c-face-name-p 'font-lock-doc-string-face)
157 ;; XEmacs.
158 'font-lock-doc-string-face)
159 ((c-face-name-p 'font-lock-doc-face)
160 ;; Emacs 21 and later.
161 'font-lock-doc-face)
163 'font-lock-comment-face)))
165 (defconst c-doc-markup-face-name
166 (if (c-face-name-p 'font-lock-doc-markup-face)
167 ;; If it happens to occur in the future. (Well, the more
168 ;; pragmatic reason is to get unique faces for the test
169 ;; suite.)
170 'font-lock-doc-markup-face
171 c-label-face-name))
173 (defconst c-negation-char-face-name
174 (if (c-face-name-p 'font-lock-negation-char-face)
175 ;; Emacs 22 has a special face for negation chars.
176 'font-lock-negation-char-face))
178 (cc-bytecomp-defun face-inverse-video-p) ; Only in Emacs.
179 (cc-bytecomp-defun face-property-instance) ; Only in XEmacs.
181 (defun c-make-inverse-face (oldface newface)
182 ;; Emacs and XEmacs have completely different face manipulation
183 ;; routines. :P
184 (copy-face oldface newface)
185 (cond ((fboundp 'face-inverse-video-p)
186 ;; Emacs. This only looks at the inverse flag in the current
187 ;; frame. Other display configurations might be different,
188 ;; but it can only show if the same Emacs has frames on
189 ;; e.g. a color and a monochrome display simultaneously.
190 (unless (face-inverse-video-p oldface)
191 (invert-face newface)))
192 ((fboundp 'face-property-instance)
193 ;; XEmacs. Same pitfall here.
194 (unless (face-property-instance oldface 'reverse)
195 (invert-face newface)))))
197 (eval-and-compile
198 ;; We need the following functions during compilation since they're
199 ;; called when the `c-lang-defconst' initializers are evaluated.
200 ;; Define them at runtime too for the sake of derived modes.
202 (defmacro c-put-font-lock-face (from to face)
203 ;; Put a face on a region (overriding any existing face) in the way
204 ;; font-lock would do it. In XEmacs that means putting an
205 ;; additional font-lock property, or else the font-lock package
206 ;; won't recognize it as fontified and might override it
207 ;; incorrectly.
209 ;; This function does a hidden buffer change.
210 (if (fboundp 'font-lock-set-face)
211 ;; Note: This function has no docstring in XEmacs so it might be
212 ;; considered internal.
213 `(font-lock-set-face ,from ,to ,face)
214 `(put-text-property ,from ,to 'face ,face)))
216 (defmacro c-remove-font-lock-face (from to)
217 ;; This is the inverse of `c-put-font-lock-face'.
219 ;; This function does a hidden buffer change.
220 (if (fboundp 'font-lock-remove-face)
221 `(font-lock-remove-face ,from ,to)
222 `(remove-text-properties ,from ,to '(face nil))))
224 (defmacro c-put-font-lock-string-face (from to)
225 ;; Put `font-lock-string-face' on a string. The surrounding
226 ;; quotes are included in Emacs but not in XEmacs. The passed
227 ;; region should include them.
229 ;; This function does a hidden buffer change.
230 (if (featurep 'xemacs)
231 `(c-put-font-lock-face (1+ ,from) (1- ,to) 'font-lock-string-face)
232 `(c-put-font-lock-face ,from ,to 'font-lock-string-face)))
234 (defmacro c-fontify-types-and-refs (varlist &rest body)
235 ;; Like `let', but additionally activates `c-record-type-identifiers'
236 ;; and `c-record-ref-identifiers', and fontifies the recorded ranges
237 ;; accordingly on exit.
239 ;; This function does hidden buffer changes.
240 `(let ((c-record-type-identifiers t)
241 c-record-ref-identifiers
242 ,@varlist)
243 (prog1 (progn ,@body)
244 (c-fontify-recorded-types-and-refs))))
245 (put 'c-fontify-types-and-refs 'lisp-indent-function 1)
247 (defun c-skip-comments-and-strings (limit)
248 ;; If the point is within a region fontified as a comment or
249 ;; string literal skip to the end of it or to LIMIT, whichever
250 ;; comes first, and return t. Otherwise return nil. The match
251 ;; data is not clobbered.
253 ;; This function might do hidden buffer changes.
254 (when (c-got-face-at (point) c-literal-faces)
255 (while (progn
256 (goto-char (next-single-property-change
257 (point) 'face nil limit))
258 (and (< (point) limit)
259 (c-got-face-at (point) c-literal-faces))))
262 (defun c-make-syntactic-matcher (regexp)
263 ;; Returns a byte compiled function suitable for use in place of a
264 ;; regexp string in a `font-lock-keywords' matcher, except that
265 ;; only matches outside comments and string literals count.
267 ;; This function does not do any hidden buffer changes, but the
268 ;; generated functions will. (They are however used in places
269 ;; covered by the font-lock context.)
270 (byte-compile
271 `(lambda (limit)
272 (let (res)
273 (while (and (setq res (re-search-forward ,regexp limit t))
274 (progn
275 (goto-char (match-beginning 0))
276 (or (c-skip-comments-and-strings limit)
277 (progn
278 (goto-char (match-end 0))
279 nil)))))
280 res))))
282 (defun c-make-font-lock-search-function (regexp &rest highlights)
283 ;; This function makes a byte compiled function that works much like
284 ;; a matcher element in `font-lock-keywords'. It cuts out a little
285 ;; bit of the overhead compared to a real matcher. The main reason
286 ;; is however to pass the real search limit to the anchored
287 ;; matcher(s), since most (if not all) font-lock implementations
288 ;; arbitrarily limits anchored matchers to the same line, and also
289 ;; to insulate against various other irritating differences between
290 ;; the different (X)Emacs font-lock packages.
292 ;; REGEXP is the matcher, which must be a regexp. Only matches
293 ;; where the beginning is outside any comment or string literal are
294 ;; significant.
296 ;; HIGHLIGHTS is a list of highlight specs, just like in
297 ;; `font-lock-keywords', with these limitations: The face is always
298 ;; overridden (no big disadvantage, since hits in comments etc are
299 ;; filtered anyway), there is no "laxmatch", and an anchored matcher
300 ;; is always a form which must do all the fontification directly.
301 ;; `limit' is a variable bound to the real limit in the context of
302 ;; the anchored matcher forms.
304 ;; This function does not do any hidden buffer changes, but the
305 ;; generated functions will. (They are however used in places
306 ;; covered by the font-lock context.)
308 ;; Note: Replace `byte-compile' with `eval' to debug the generated
309 ;; lambda easier.
310 (byte-compile
311 `(lambda (limit)
312 (let (;; The font-lock package in Emacs is known to clobber
313 ;; `parse-sexp-lookup-properties' (when it exists).
314 (parse-sexp-lookup-properties
315 (cc-eval-when-compile
316 (boundp 'parse-sexp-lookup-properties))))
317 (while (re-search-forward ,regexp limit t)
318 (unless (progn
319 (goto-char (match-beginning 0))
320 (c-skip-comments-and-strings limit))
321 (goto-char (match-end 0))
322 ,@(mapcar
323 (lambda (highlight)
324 (if (integerp (car highlight))
325 (progn
326 (unless (eq (nth 2 highlight) t)
327 (error
328 "The override flag must currently be t in %s"
329 highlight))
330 (when (nth 3 highlight)
331 (error
332 "The laxmatch flag may currently not be set in %s"
333 highlight))
334 `(save-match-data
335 (c-put-font-lock-face
336 (match-beginning ,(car highlight))
337 (match-end ,(car highlight))
338 ,(elt highlight 1))))
339 (when (nth 3 highlight)
340 (error "Match highlights currently not supported in %s"
341 highlight))
342 `(progn
343 ,(nth 1 highlight)
344 (save-match-data ,(car highlight))
345 ,(nth 2 highlight))))
346 highlights))))
347 nil)))
349 ; (eval-after-load "edebug" ; 2006-07-09: def-edebug-spec is now in subr.el.
350 ; '(progn
351 (def-edebug-spec c-fontify-types-and-refs let*)
352 (def-edebug-spec c-make-syntactic-matcher t)
353 ;; If there are literal quoted or backquoted highlight specs in
354 ;; the call to `c-make-font-lock-search-function' then let's
355 ;; instrument the forms in them.
356 (def-edebug-spec c-make-font-lock-search-function
357 (form &rest &or ("quote" (&rest form)) ("`" (&rest form)) form)));))
359 (defun c-fontify-recorded-types-and-refs ()
360 ;; Convert the ranges recorded on `c-record-type-identifiers' and
361 ;; `c-record-ref-identifiers' to fontification.
363 ;; This function does hidden buffer changes.
364 (let (elem)
365 (while (consp c-record-type-identifiers)
366 (setq elem (car c-record-type-identifiers)
367 c-record-type-identifiers (cdr c-record-type-identifiers))
368 (c-put-font-lock-face (car elem) (cdr elem)
369 'font-lock-type-face))
370 (while c-record-ref-identifiers
371 (setq elem (car c-record-ref-identifiers)
372 c-record-ref-identifiers (cdr c-record-ref-identifiers))
373 ;; Note that the reference face is a variable that is
374 ;; dereferenced, since it's an alias in Emacs.
375 (c-put-font-lock-face (car elem) (cdr elem)
376 c-reference-face-name))))
378 (c-lang-defconst c-cpp-matchers
379 "Font lock matchers for preprocessor directives and purely lexical
380 stuff. Used on level 1 and higher."
382 ;; Note: `c-font-lock-declarations' assumes that no matcher here
383 ;; sets `font-lock-type-face' in languages where
384 ;; `c-recognize-<>-arglists' is set.
386 t `(,@(when (c-lang-const c-opt-cpp-prefix)
387 (let* ((noncontinued-line-end "\\(\\=\\|\\(\\=\\|[^\\]\\)[\n\r]\\)")
388 (ncle-depth (regexp-opt-depth noncontinued-line-end))
389 (sws-depth (c-lang-const c-syntactic-ws-depth))
390 (nsws-depth (c-lang-const c-nonempty-syntactic-ws-depth)))
392 `(;; The stuff after #error and #warning is a message, so
393 ;; fontify it as a string.
394 ,@(when (c-lang-const c-cpp-message-directives)
395 (let* ((re (c-make-keywords-re 'appendable ; nil
396 (c-lang-const c-cpp-message-directives)))
397 (re-depth (regexp-opt-depth re)))
398 `((,(concat noncontinued-line-end
399 (c-lang-const c-opt-cpp-prefix)
401 "\\s +\\(.*\\)$")
402 ,(+ ncle-depth re-depth 1) font-lock-string-face t))))
404 ;; Fontify filenames in #include <...> as strings.
405 ,@(when (c-lang-const c-cpp-include-directives)
406 (let* ((re (c-make-keywords-re nil
407 (c-lang-const c-cpp-include-directives)))
408 (re-depth (regexp-opt-depth re)))
409 `((,(concat noncontinued-line-end
410 (c-lang-const c-opt-cpp-prefix)
412 (c-lang-const c-syntactic-ws)
413 "\\(<[^>\n\r]*>?\\)")
414 (,(+ ncle-depth re-depth sws-depth 1)
415 font-lock-string-face)
417 ;; Use an anchored matcher to put paren syntax
418 ;; on the brackets.
419 (,(byte-compile
420 `(lambda (limit)
421 (let ((beg (match-beginning
422 ,(+ ncle-depth re-depth sws-depth 1)))
423 (end (1- (match-end ,(+ ncle-depth re-depth
424 sws-depth 1)))))
425 (if (eq (char-after end) ?>)
426 (progn
427 (c-mark-<-as-paren beg)
428 (c-mark->-as-paren end))
429 ;; (c-clear-char-property beg 'syntax-table)
430 (c-clear-char-property beg 'category)))
431 nil)))))))
433 ;; #define.
434 ,@(when (c-lang-const c-opt-cpp-macro-define)
435 `((,(c-make-font-lock-search-function
436 (concat
437 noncontinued-line-end
438 (c-lang-const c-opt-cpp-prefix)
439 (c-lang-const c-opt-cpp-macro-define)
440 (c-lang-const c-nonempty-syntactic-ws)
441 "\\(" (c-lang-const ; 1 + ncle + nsws
442 c-symbol-key) "\\)"
443 (concat "\\(" ; 2 + ncle + nsws + c-sym-key
444 ;; Macro with arguments - a "function".
445 "\\(\(\\)" ; 3 + ncle + nsws + c-sym-key
446 "\\|"
447 ;; Macro without arguments - a "variable".
448 "\\([^\(]\\|$\\)"
449 "\\)"))
450 `((if (match-beginning
451 ,(+ 3 ncle-depth nsws-depth
452 (c-lang-const c-symbol-key-depth)))
454 ;; "Function". Fontify the name and the arguments.
455 (save-restriction
456 (c-put-font-lock-face
457 (match-beginning ,(+ 1 ncle-depth nsws-depth))
458 (match-end ,(+ 1 ncle-depth nsws-depth))
459 'font-lock-function-name-face)
460 (goto-char
461 (match-end
462 ,(+ 3 ncle-depth nsws-depth
463 (c-lang-const c-symbol-key-depth))))
465 (narrow-to-region (point-min) limit)
466 (while (and
467 (progn
468 (c-forward-syntactic-ws)
469 (looking-at c-symbol-key))
470 (progn
471 (c-put-font-lock-face
472 (match-beginning 0) (match-end 0)
473 'font-lock-variable-name-face)
474 (goto-char (match-end 0))
475 (c-forward-syntactic-ws)
476 (eq (char-after) ?,)))
477 (forward-char)))
479 ;; "Variable".
480 (c-put-font-lock-face
481 (match-beginning ,(+ 1 ncle-depth nsws-depth))
482 (match-end ,(+ 1 ncle-depth nsws-depth))
483 'font-lock-variable-name-face)))))))
485 ;; Fontify cpp function names in preprocessor
486 ;; expressions in #if and #elif.
487 ,@(when (and (c-lang-const c-cpp-expr-directives)
488 (c-lang-const c-cpp-expr-functions))
489 (let ((ced-re (c-make-keywords-re t
490 (c-lang-const c-cpp-expr-directives)))
491 (cef-re (c-make-keywords-re t
492 (c-lang-const c-cpp-expr-functions))))
493 `((,(c-make-font-lock-search-function
494 (concat noncontinued-line-end
495 (c-lang-const c-opt-cpp-prefix)
496 ced-re ; 1 + ncle-depth
497 ;; Match the whole logical line to look
498 ;; for the functions in.
499 "\\(\\\\\\(.\\|[\n\r]\\)\\|[^\n\r]\\)*")
500 `((let ((limit (match-end 0)))
501 (while (re-search-forward ,cef-re limit 'move)
502 (c-put-font-lock-face (match-beginning 1)
503 (match-end 1)
504 c-preprocessor-face-name)))
505 (goto-char (match-end ,(1+ ncle-depth)))))))))
507 ;; Fontify the directive names.
508 (,(c-make-font-lock-search-function
509 (concat noncontinued-line-end
510 "\\("
511 (c-lang-const c-opt-cpp-prefix)
512 "[" (c-lang-const c-symbol-chars) "]+"
513 "\\)")
514 `(,(1+ ncle-depth) c-preprocessor-face-name t)))
516 (eval . (list ,(c-make-syntactic-matcher
517 (concat noncontinued-line-end
518 (c-lang-const c-opt-cpp-prefix)
519 "if\\(n\\)def\\>"))
520 ,(+ ncle-depth 1)
521 c-negation-char-face-name
522 'append))
525 ,@(when (c-major-mode-is 'pike-mode)
526 ;; Recognize hashbangs in Pike.
527 `((eval . (list "\\`#![^\n\r]*"
528 0 c-preprocessor-face-name))))
530 ;; Make hard spaces visible through an inverted `font-lock-warning-face'.
531 (eval . (list
532 "\240"
533 0 (progn
534 (unless (c-face-name-p 'c-nonbreakable-space-face)
535 (c-make-inverse-face 'font-lock-warning-face
536 'c-nonbreakable-space-face))
537 ''c-nonbreakable-space-face)))
540 (defun c-font-lock-invalid-string ()
541 ;; Assuming the point is after the opening character of a string,
542 ;; fontify that char with `font-lock-warning-face' if the string
543 ;; decidedly isn't terminated properly.
545 ;; This function does hidden buffer changes.
546 (let ((start (1- (point))))
547 (save-excursion
548 (and (eq (elt (parse-partial-sexp start (c-point 'eol)) 8) start)
549 (if (integerp c-multiline-string-start-char)
550 ;; There's no multiline string start char before the
551 ;; string, so newlines aren't allowed.
552 (not (eq (char-before start) c-multiline-string-start-char))
553 ;; Multiline strings are allowed anywhere if
554 ;; c-multiline-string-start-char is t.
555 (not c-multiline-string-start-char))
556 (if c-string-escaped-newlines
557 ;; There's no \ before the newline.
558 (not (eq (char-before (point)) ?\\))
559 ;; Escaped newlines aren't supported.
561 (c-put-font-lock-face start (1+ start) 'font-lock-warning-face)))))
563 (c-lang-defconst c-basic-matchers-before
564 "Font lock matchers for basic keywords, labels, references and various
565 other easily recognizable things that should be fontified before generic
566 casts and declarations are fontified. Used on level 2 and higher."
568 ;; Note: `c-font-lock-declarations' assumes that no matcher here
569 ;; sets `font-lock-type-face' in languages where
570 ;; `c-recognize-<>-arglists' is set.
572 t `(;; Put a warning face on the opener of unclosed strings that
573 ;; can't span lines. Later font
574 ;; lock packages have a `font-lock-syntactic-face-function' for
575 ;; this, but it doesn't give the control we want since any
576 ;; fontification done inside the function will be
577 ;; unconditionally overridden.
578 ,(c-make-font-lock-search-function
579 ;; Match a char before the string starter to make
580 ;; `c-skip-comments-and-strings' work correctly.
581 (concat ".\\(" c-string-limit-regexp "\\)")
582 '((c-font-lock-invalid-string)))
584 ;; Fontify keyword constants.
585 ,@(when (c-lang-const c-constant-kwds)
586 (let ((re (c-make-keywords-re nil (c-lang-const c-constant-kwds))))
587 (if (c-major-mode-is 'pike-mode)
588 ;; No symbol is a keyword after "->" in Pike.
589 `((eval . (list ,(concat "\\(\\=.?\\|[^>]\\|[^-]>\\)"
590 "\\<\\(" re "\\)\\>")
591 2 c-constant-face-name)))
592 `((eval . (list ,(concat "\\<\\(" re "\\)\\>")
593 1 c-constant-face-name))))))
595 ;; Fontify all keywords except the primitive types.
596 ,(if (c-major-mode-is 'pike-mode)
597 ;; No symbol is a keyword after "->" in Pike.
598 `(,(concat "\\(\\=.?\\|[^>]\\|[^-]>\\)"
599 "\\<" (c-lang-const c-regular-keywords-regexp))
600 2 font-lock-keyword-face)
601 `(,(concat "\\<" (c-lang-const c-regular-keywords-regexp))
602 1 font-lock-keyword-face))
604 ;; Fontify leading identifiers in fully qualified names like
605 ;; "foo::bar" in languages that supports such things.
606 ,@(when (c-lang-const c-opt-identifier-concat-key)
607 (if (c-major-mode-is 'java-mode)
608 ;; Java needs special treatment since "." is used both to
609 ;; qualify names and in normal indexing. Here we look for
610 ;; capital characters at the beginning of an identifier to
611 ;; recognize the class. "*" is also recognized to cover
612 ;; wildcard import declarations. All preceding dot separated
613 ;; identifiers are taken as package names and therefore
614 ;; fontified as references.
615 `(,(c-make-font-lock-search-function
616 ;; Search for class identifiers preceded by ".". The
617 ;; anchored matcher takes it from there.
618 (concat (c-lang-const c-opt-identifier-concat-key)
619 (c-lang-const c-simple-ws) "*"
620 (concat "\\("
621 "[" c-upper "]"
622 "[" (c-lang-const c-symbol-chars) "]*"
623 "\\|"
624 "\\*"
625 "\\)"))
626 `((let (id-end)
627 (goto-char (1+ (match-beginning 0)))
628 (while (and (eq (char-before) ?.)
629 (progn
630 (backward-char)
631 (c-backward-syntactic-ws)
632 (setq id-end (point))
633 (< (skip-chars-backward
634 ,(c-lang-const c-symbol-chars)) 0))
635 (not (get-text-property (point) 'face)))
636 (c-put-font-lock-face (point) id-end
637 c-reference-face-name)
638 (c-backward-syntactic-ws)))
640 (goto-char (match-end 0)))))
642 `((,(byte-compile
643 ;; Must use a function here since we match longer than
644 ;; we want to move before doing a new search. This is
645 ;; not necessary for XEmacs since it restarts the
646 ;; search from the end of the first highlighted
647 ;; submatch (something that causes problems in other
648 ;; places).
649 `(lambda (limit)
650 (while (re-search-forward
651 ,(concat "\\(\\<" ; 1
652 "\\(" (c-lang-const c-symbol-key) "\\)" ; 2
653 (c-lang-const c-simple-ws) "*"
654 (c-lang-const c-opt-identifier-concat-key)
655 (c-lang-const c-simple-ws) "*"
656 "\\)"
657 "\\("
658 (c-lang-const c-opt-after-id-concat-key)
659 "\\)")
660 limit t)
661 (unless (progn
662 (goto-char (match-beginning 0))
663 (c-skip-comments-and-strings limit))
664 (or (get-text-property (match-beginning 2) 'face)
665 (c-put-font-lock-face (match-beginning 2)
666 (match-end 2)
667 c-reference-face-name))
668 (goto-char (match-end 1))))))))))
670 ;; Fontify the special declarations in Objective-C.
671 ,@(when (c-major-mode-is 'objc-mode)
672 `(;; Fontify class names in the beginning of message expressions.
673 ,(c-make-font-lock-search-function
674 "\\["
675 '((c-fontify-types-and-refs ()
676 (c-forward-syntactic-ws limit)
677 (let ((start (point)))
678 ;; In this case we accept both primitive and known types.
679 (when (eq (c-forward-type) 'known)
680 (goto-char start)
681 (let ((c-promote-possible-types t))
682 (c-forward-type))))
683 (if (> (point) limit) (goto-char limit)))))
685 ;; The @interface/@implementation/@protocol directives.
686 ,(c-make-font-lock-search-function
687 (concat "\\<"
688 (regexp-opt
689 '("@interface" "@implementation" "@protocol")
691 "\\>")
692 '((c-fontify-types-and-refs
693 (;; The font-lock package in Emacs is known to clobber
694 ;; `parse-sexp-lookup-properties' (when it exists).
695 (parse-sexp-lookup-properties
696 (cc-eval-when-compile
697 (boundp 'parse-sexp-lookup-properties))))
698 (c-forward-objc-directive)
699 nil)
700 (goto-char (match-beginning 0))))))
702 (eval . (list "\\(!\\)[^=]" 1 c-negation-char-face-name))
705 (defun c-font-lock-complex-decl-prepare (limit)
706 ;; This function will be called from font-lock for a region bounded by POINT
707 ;; and LIMIT, as though it were to identify a keyword for
708 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
709 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
710 ;; Fontification".
712 ;; Called before any of the matchers in `c-complex-decl-matchers'.
714 ;; This function does hidden buffer changes.
716 ;;(message "c-font-lock-complex-decl-prepare %s %s" (point) limit)
718 ;; Clear the list of found types if we start from the start of the
719 ;; buffer, to make it easier to get rid of misspelled types and
720 ;; variables that has gotten recognized as types in malformed code.
721 (when (bobp)
722 (c-clear-found-types))
724 ;; Clear the c-type char properties in the region to recalculate
725 ;; them properly. This is necessary e.g. to handle constructs that
726 ;; might been required as declarations temporarily during editing.
727 ;; The interesting properties are anyway those put on the closest
728 ;; token before the region.
729 (c-clear-char-properties (point) limit 'c-type)
731 ;; Update `c-state-cache' to the beginning of the region. This will
732 ;; make `c-beginning-of-syntax' go faster when it's used later on,
733 ;; and it's near the point most of the time.
734 (c-parse-state)
736 ;; Check if the fontified region starts inside a declarator list so
737 ;; that `c-font-lock-declarators' should be called at the start.
738 (let ((prop (save-excursion
739 (c-backward-syntactic-ws)
740 (unless (bobp)
741 (c-get-char-property (1- (point)) 'c-type)))))
742 (when (memq prop '(c-decl-id-start c-decl-type-start))
743 (c-forward-syntactic-ws limit)
744 (c-font-lock-declarators limit t (eq prop 'c-decl-type-start))))
746 nil)
748 (defun c-font-lock-<>-arglists (limit)
749 ;; This function will be called from font-lock for a region bounded by POINT
750 ;; and LIMIT, as though it were to identify a keyword for
751 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
752 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
753 ;; Fontification".
755 ;; Fontify types and references in names containing angle bracket
756 ;; arglists from the point to LIMIT. Note that
757 ;; `c-font-lock-declarations' already has handled many of them.
759 ;; This function might do hidden buffer changes.
761 (let (;; The font-lock package in Emacs is known to clobber
762 ;; `parse-sexp-lookup-properties' (when it exists).
763 (parse-sexp-lookup-properties
764 (cc-eval-when-compile
765 (boundp 'parse-sexp-lookup-properties)))
766 (c-parse-and-markup-<>-arglists t)
767 c-restricted-<>-arglists
768 id-start id-end id-face pos kwd-sym)
770 (while (and (< (point) limit)
771 (re-search-forward c-opt-<>-arglist-start limit t))
773 (setq id-start (match-beginning 1)
774 id-end (match-end 1)
775 pos (point))
777 (goto-char id-start)
778 (unless (c-skip-comments-and-strings limit)
779 (setq kwd-sym nil
780 c-restricted-<>-arglists nil
781 id-face (get-text-property id-start 'face))
783 (if (cond
784 ((eq id-face 'font-lock-type-face)
785 ;; The identifier got the type face so it has already been
786 ;; handled in `c-font-lock-declarations'.
787 nil)
789 ((eq id-face 'font-lock-keyword-face)
790 (when (looking-at c-opt-<>-sexp-key)
791 ;; There's a special keyword before the "<" that tells
792 ;; that it's an angle bracket arglist.
793 (setq kwd-sym (c-keyword-sym (match-string 1)))))
796 ;; There's a normal identifier before the "<". If we're not in
797 ;; a declaration context then we set `c-restricted-<>-arglists'
798 ;; to avoid recognizing templates in function calls like "foo (a
799 ;; < b, c > d)".
800 (c-backward-syntactic-ws)
801 (when (and (memq (char-before) '(?\( ?,))
802 (not (eq (get-text-property (1- (point)) 'c-type)
803 'c-decl-arg-start)))
804 (setq c-restricted-<>-arglists t))
807 (progn
808 (goto-char (1- pos))
809 ;; Check for comment/string both at the identifier and
810 ;; at the "<".
811 (unless (c-skip-comments-and-strings limit)
813 (c-fontify-types-and-refs ()
814 (when (c-forward-<>-arglist (c-keyword-member
815 kwd-sym 'c-<>-type-kwds))
816 (when (and c-opt-identifier-concat-key
817 (not (get-text-property id-start 'face)))
818 (c-forward-syntactic-ws)
819 (if (looking-at c-opt-identifier-concat-key)
820 (c-put-font-lock-face id-start id-end
821 c-reference-face-name)
822 (c-put-font-lock-face id-start id-end
823 'font-lock-type-face)))))
825 (goto-char pos)))
826 (goto-char pos)))))
827 nil)
829 (defun c-font-lock-declarators (limit list types)
830 ;; Assuming the point is at the start of a declarator in a
831 ;; declaration, fontify it. If LIST is non-nil, fontify also all
832 ;; following declarators in a comma separated list (e.g. "foo" and
833 ;; "bar" in "int foo = 17, bar;"). Stop at LIMIT. If TYPES is
834 ;; non-nil, fontify all identifiers as types. Nil is always
835 ;; returned.
837 ;; This function might do hidden buffer changes.
839 ;;(message "c-font-lock-declarators from %s to %s" (point) limit)
840 (c-fontify-types-and-refs
841 ((pos (point)) next-pos id-start id-end
842 paren-depth
843 id-face got-init
844 c-last-identifier-range
845 (separator-prop (if types 'c-decl-type-start 'c-decl-id-start)))
847 (while (and
849 (< (point) limit)
851 (let (got-identifier)
852 (setq paren-depth 0)
853 ;; Skip over type decl prefix operators. (Note similar
854 ;; code in `c-forward-decl-or-cast-1'.)
855 (while (and (looking-at c-type-decl-prefix-key)
856 (if (and (c-major-mode-is 'c++-mode)
857 (match-beginning 2))
858 ;; If the second submatch matches in C++ then
859 ;; we're looking at an identifier that's a
860 ;; prefix only if it specifies a member pointer.
861 (progn
862 (setq id-start (point))
863 (c-forward-name)
864 (if (looking-at "\\(::\\)")
865 ;; We only check for a trailing "::" and
866 ;; let the "*" that should follow be
867 ;; matched in the next round.
869 ;; It turned out to be the real identifier,
870 ;; so flag that and stop.
871 (setq got-identifier t)
872 nil))
874 (if (eq (char-after) ?\()
875 (progn
876 (setq paren-depth (1+ paren-depth))
877 (forward-char))
878 (goto-char (match-end 1)))
879 (c-forward-syntactic-ws))
881 ;; If we didn't pass the identifier above already, do it now.
882 (unless got-identifier
883 (setq id-start (point))
884 (c-forward-name))
885 (setq id-end (point))
887 (/= id-end pos))
889 ;; Skip out of the parens surrounding the identifier.
890 (or (= paren-depth 0)
891 (c-safe (goto-char (scan-lists (point) 1 paren-depth))))
893 (<= (point) limit)
895 (progn
896 (when (looking-at c-decl-hangon-key)
897 (c-forward-keyword-clause 1))
898 (<= (point) limit))
900 ;; Search syntactically to the end of the declarator (";",
901 ;; ",", a closen paren, eob etc) or to the beginning of an
902 ;; initializer or function prototype ("=" or "\\s\(").
903 ;; Note that the open paren will match array specs in
904 ;; square brackets, and we treat them as initializers too.
905 (c-syntactic-re-search-forward
906 "[;,]\\|\\s)\\|\\'\\|\\(=\\|\\s(\\)" limit t t))
908 (setq next-pos (match-beginning 0)
909 id-face (if (and (eq (char-after next-pos) ?\()
910 (let (c-last-identifier-range)
911 (save-excursion
912 (goto-char next-pos)
913 (c-at-toplevel-p))))
914 'font-lock-function-name-face
915 'font-lock-variable-name-face)
916 got-init (and (match-beginning 1)
917 (char-after (match-beginning 1))))
919 (if types
920 ;; Register and fontify the identifer as a type.
921 (let ((c-promote-possible-types t))
922 (goto-char id-start)
923 (c-forward-type))
924 ;; Fontify the last symbol in the identifier if it isn't fontified
925 ;; already. The check is necessary only in certain cases where this
926 ;; function is used "sloppily", e.g. in `c-simple-decl-matchers'.
927 (when (and c-last-identifier-range
928 (not (get-text-property (car c-last-identifier-range)
929 'face)))
930 (c-put-font-lock-face (car c-last-identifier-range)
931 (cdr c-last-identifier-range)
932 id-face)))
934 (goto-char next-pos)
935 (setq pos nil)
936 (when list
937 ;; Jump past any initializer or function prototype to see if
938 ;; there's a ',' to continue at.
940 (cond ((eq id-face 'font-lock-function-name-face)
941 ;; Skip a parenthesized initializer (C++) or a function
942 ;; prototype.
943 (if (c-safe (c-forward-sexp 1) t)
944 (c-forward-syntactic-ws limit)
945 (goto-char limit)))
947 (got-init
948 ;; Skip an initializer expression. If we're at a '='
949 ;; then accept a brace list directly after it to cope
950 ;; with array initializers. Otherwise stop at braces
951 ;; to avoid going past full function and class blocks.
952 (and (if (and (eq got-init ?=)
953 (= (c-forward-token-2 1 nil limit) 0)
954 (looking-at "{"))
955 (c-safe (c-forward-sexp) t)
957 ;; FIXME: Should look for c-decl-end markers here;
958 ;; we might go far into the following declarations
959 ;; in e.g. ObjC mode (see e.g. methods-4.m).
960 (c-syntactic-re-search-forward "[;,{]" limit 'move t)
961 (backward-char)))
963 (t (c-forward-syntactic-ws limit)))
965 ;; If a ',' is found we set pos to the next declarator and iterate.
966 (when (and (< (point) limit) (looking-at ","))
967 (c-put-char-property (point) 'c-type separator-prop)
968 (forward-char)
969 (c-forward-syntactic-ws limit)
970 (setq pos (point))))))
971 nil)
973 (defconst c-font-lock-maybe-decl-faces
974 ;; List of faces that might be put at the start of a type when
975 ;; `c-font-lock-declarations' runs. This needs to be evaluated to
976 ;; ensure that face name aliases in Emacs are resolved.
977 (list nil
978 font-lock-type-face
979 c-reference-face-name
980 font-lock-keyword-face))
982 (defun c-font-lock-declarations (limit)
983 ;; This function will be called from font-lock for a region bounded by POINT
984 ;; and LIMIT, as though it were to identify a keyword for
985 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
986 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
987 ;; Fontification".
989 ;; Fontify all the declarations, casts and labels from the point to LIMIT.
990 ;; Assumes that strings and comments have been fontified already.
992 ;; This function might do hidden buffer changes.
994 ;;(message "c-font-lock-declarations search from %s to %s" (point) limit)
996 (save-restriction
997 (let (;; The position where `c-find-decl-spots' stopped.
998 start-pos
999 ;; 'decl if we're in an arglist containing declarations (but
1000 ;; if `c-recognize-paren-inits' is set it might also be an
1001 ;; initializer arglist), '<> if the arglist is of angle
1002 ;; bracket type, 'arglist if it's some other arglist, or nil
1003 ;; if not in an arglist at all.
1004 context
1005 ;; The position of the next token after the closing paren of
1006 ;; the last detected cast.
1007 last-cast-end
1008 ;; The result from `c-forward-decl-or-cast-1'.
1009 decl-or-cast
1010 ;; The maximum of the end positions of all the checked type
1011 ;; decl expressions in the successfully identified
1012 ;; declarations. The position might be either before or
1013 ;; after the syntactic whitespace following the last token
1014 ;; in the type decl expression.
1015 (max-type-decl-end 0)
1016 ;; Same as `max-type-decl-*', but used when we're before
1017 ;; `token-pos'.
1018 (max-type-decl-end-before-token 0)
1019 ;; Set according to the context to direct the heuristics for
1020 ;; recognizing C++ templates.
1021 c-restricted-<>-arglists
1022 ;; Turn on recording of identifier ranges in
1023 ;; `c-forward-decl-or-cast-1' and `c-forward-label' for
1024 ;; later fontification.
1025 (c-record-type-identifiers t)
1026 label-type
1027 c-record-ref-identifiers
1028 ;; Make `c-forward-type' calls mark up template arglists if
1029 ;; it finds any. That's necessary so that we later will
1030 ;; stop inside them to fontify types there.
1031 (c-parse-and-markup-<>-arglists t)
1032 ;; The font-lock package in Emacs is known to clobber
1033 ;; `parse-sexp-lookup-properties' (when it exists).
1034 (parse-sexp-lookup-properties
1035 (cc-eval-when-compile
1036 (boundp 'parse-sexp-lookup-properties))))
1038 ;; Below we fontify a whole declaration even when it crosses the limit,
1039 ;; to avoid gaps when lazy-lock fontifies the file a screenful at a
1040 ;; time. That is however annoying during editing, e.g. the following is
1041 ;; a common situation while the first line is being written:
1043 ;; my_variable
1044 ;; some_other_variable = 0;
1046 ;; font-lock will put the limit at the beginning of the second line
1047 ;; here, and if we go past it we'll fontify "my_variable" as a type and
1048 ;; "some_other_variable" as an identifier, and the latter will not
1049 ;; correct itself until the second line is changed. To avoid that we
1050 ;; narrow to the limit if the region to fontify is a single line.
1051 (narrow-to-region
1052 (point-min)
1053 (if (<= limit (c-point 'bonl))
1054 (save-excursion
1055 ;; Narrow after any operator chars following the limit though,
1056 ;; since those characters can be useful in recognizing a
1057 ;; declaration (in particular the '{' that opens a function body
1058 ;; after the header).
1059 (goto-char limit)
1060 (skip-chars-forward c-nonsymbol-chars)
1061 (point))
1062 limit))
1064 (c-find-decl-spots
1065 limit
1066 c-decl-start-re
1067 c-font-lock-maybe-decl-faces
1069 (lambda (match-pos inside-macro)
1070 (setq start-pos (point))
1071 (when
1072 ;; The result of the form below is true when we don't recognize a
1073 ;; declaration or cast.
1074 (if (and (eq (get-text-property (point) 'face)
1075 'font-lock-keyword-face)
1076 (looking-at c-not-decl-init-keywords))
1077 ;; Don't do anything more if we're looking at a keyword that
1078 ;; can't start a declaration.
1081 ;; Set `context'. Look for "<" for the sake of C++-style template
1082 ;; arglists.
1083 (if (memq (char-before match-pos) '(?\( ?, ?\[ ?<))
1085 ;; Find out the type of the arglist.
1086 (if (<= match-pos (point-min))
1087 (setq context 'arglist)
1088 (let ((type (c-get-char-property (1- match-pos) 'c-type)))
1089 (cond ((eq type 'c-decl-arg-start)
1090 ;; Got a cached hit in a declaration arglist.
1091 (setq context 'decl))
1092 ((or (eq type 'c-<>-arg-sep)
1093 (eq (char-before match-pos) ?<))
1094 ;; Inside an angle bracket arglist.
1095 (setq context '<>))
1096 (type
1097 ;; Got a cached hit in some other type of arglist.
1098 (setq context 'arglist))
1099 ((if inside-macro
1100 (< match-pos max-type-decl-end-before-token)
1101 (< match-pos max-type-decl-end))
1102 ;; The point is within the range of a previously
1103 ;; encountered type decl expression, so the arglist
1104 ;; is probably one that contains declarations.
1105 ;; However, if `c-recognize-paren-inits' is set it
1106 ;; might also be an initializer arglist.
1107 (setq context 'decl)
1108 ;; The result of this check is cached with a char
1109 ;; property on the match token, so that we can look
1110 ;; it up again when refontifying single lines in a
1111 ;; multiline declaration.
1112 (c-put-char-property (1- match-pos)
1113 'c-type 'c-decl-arg-start))
1115 (setq context 'arglist)))))
1117 (setq context nil))
1119 ;; If we're in a normal arglist context we don't want to
1120 ;; recognize commas in nested angle bracket arglists since
1121 ;; those commas could be part of our own arglist.
1122 (setq c-restricted-<>-arglists (and c-recognize-<>-arglists
1123 (eq context 'arglist))
1125 ;; Now analyze the construct.
1126 decl-or-cast (c-forward-decl-or-cast-1
1127 match-pos context last-cast-end))
1129 (if (not decl-or-cast)
1130 ;; False alarm. Return t to go on to the next check.
1133 (if (eq decl-or-cast 'cast)
1134 ;; Save the position after the previous cast so we can feed
1135 ;; it to `c-forward-decl-or-cast-1' in the next round. That
1136 ;; helps it discover cast chains like "(a) (b) c".
1137 (setq last-cast-end (point))
1139 ;; Set `max-type-decl-end' or `max-type-decl-end-before-token'
1140 ;; under the assumption that we're after the first type decl
1141 ;; expression in the declaration now. That's not really true;
1142 ;; we could also be after a parenthesized initializer
1143 ;; expression in C++, but this is only used as a last resort
1144 ;; to slant ambiguous expression/declarations, and overall
1145 ;; it's worth the risk to occasionally fontify an expression
1146 ;; as a declaration in an initializer expression compared to
1147 ;; getting ambiguous things in normal function prototypes
1148 ;; fontified as expressions.
1149 (if inside-macro
1150 (when (> (point) max-type-decl-end-before-token)
1151 (setq max-type-decl-end-before-token (point)))
1152 (when (> (point) max-type-decl-end)
1153 (setq max-type-decl-end (point))))
1155 ;; Back up to the type to fontify the declarator(s).
1156 (goto-char (car decl-or-cast))
1158 (let ((decl-list
1159 (if context
1160 ;; Should normally not fontify a list of
1161 ;; declarators inside an arglist, but the first
1162 ;; argument in the ';' separated list of a "for"
1163 ;; statement is an exception.
1164 (when (eq (char-before match-pos) ?\()
1165 (save-excursion
1166 (goto-char (1- match-pos))
1167 (c-backward-syntactic-ws)
1168 (and (c-simple-skip-symbol-backward)
1169 (looking-at c-paren-stmt-key))))
1170 t)))
1172 ;; Fix the `c-decl-id-start' or `c-decl-type-start' property
1173 ;; before the first declarator if it's a list.
1174 ;; `c-font-lock-declarators' handles the rest.
1175 (when decl-list
1176 (save-excursion
1177 (c-backward-syntactic-ws)
1178 (unless (bobp)
1179 (c-put-char-property (1- (point)) 'c-type
1180 (if (cdr decl-or-cast)
1181 'c-decl-type-start
1182 'c-decl-id-start)))))
1184 (c-font-lock-declarators
1185 (point-max) decl-list (cdr decl-or-cast))))
1187 ;; A cast or declaration has been successfully identified, so do
1188 ;; all the fontification of types and refs that's been recorded.
1189 (c-fontify-recorded-types-and-refs)
1190 nil))
1192 ;; It was a false alarm. Check if we're in a label (or other
1193 ;; construct with `:' except bitfield) instead.
1194 (goto-char start-pos)
1195 (when (setq label-type (c-forward-label t match-pos nil))
1196 ;; Can't use `c-fontify-types-and-refs' here since we
1197 ;; use the label face at times.
1198 (cond ((eq label-type 'goto-target)
1199 (c-put-font-lock-face (caar c-record-ref-identifiers)
1200 (cdar c-record-ref-identifiers)
1201 c-label-face-name))
1202 ((eq label-type 'qt-1kwd-colon)
1203 (c-put-font-lock-face (caar c-record-ref-identifiers)
1204 (cdar c-record-ref-identifiers)
1205 'font-lock-keyword-face))
1206 ((eq label-type 'qt-2kwds-colon)
1207 (mapc
1208 (lambda (kwd)
1209 (c-put-font-lock-face (car kwd) (cdr kwd)
1210 'font-lock-keyword-face))
1211 c-record-ref-identifiers)))
1212 (setq c-record-ref-identifiers nil)
1213 ;; `c-forward-label' has probably added a `c-decl-end'
1214 ;; marker, so return t to `c-find-decl-spots' to signal
1215 ;; that.
1216 t))))
1218 nil)))
1220 (c-lang-defconst c-simple-decl-matchers
1221 "Simple font lock matchers for types and declarations. These are used
1222 on level 2 only and so aren't combined with `c-complex-decl-matchers'."
1224 t `(;; Objective-C methods.
1225 ,@(when (c-major-mode-is 'objc-mode)
1226 `((,(c-lang-const c-opt-method-key)
1227 (,(byte-compile
1228 (lambda (limit)
1229 (let (;; The font-lock package in Emacs is known to clobber
1230 ;; `parse-sexp-lookup-properties' (when it exists).
1231 (parse-sexp-lookup-properties
1232 (cc-eval-when-compile
1233 (boundp 'parse-sexp-lookup-properties))))
1234 (save-restriction
1235 (narrow-to-region (point-min) limit)
1236 (c-font-lock-objc-method)))
1237 nil))
1238 (goto-char (match-end 1))))))
1240 ;; Fontify all type names and the identifiers in the
1241 ;; declarations they might start. Use eval here since
1242 ;; `c-known-type-key' gets its value from
1243 ;; `*-font-lock-extra-types' on mode init.
1244 (eval . (list ,(c-make-font-lock-search-function
1245 'c-known-type-key
1246 '(1 'font-lock-type-face t)
1247 '((c-font-lock-declarators limit t nil)
1248 (save-match-data
1249 (goto-char (match-end 1))
1250 (c-forward-syntactic-ws))
1251 (goto-char (match-end 1))))))
1253 ;; Fontify types preceded by `c-type-prefix-kwds' and the
1254 ;; identifiers in the declarations they might start.
1255 ,@(when (c-lang-const c-type-prefix-kwds)
1256 (let* ((prefix-re (c-make-keywords-re nil
1257 (c-lang-const c-type-prefix-kwds)))
1258 (type-match (+ 2
1259 (regexp-opt-depth prefix-re)
1260 (c-lang-const c-simple-ws-depth))))
1261 `((,(c-make-font-lock-search-function
1262 (concat "\\<\\(" prefix-re "\\)" ; 1
1263 (c-lang-const c-simple-ws) "+"
1264 (concat "\\(" ; 2 + prefix-re + c-simple-ws
1265 (c-lang-const c-symbol-key)
1266 "\\)"))
1267 `(,type-match
1268 'font-lock-type-face t)
1269 `((c-font-lock-declarators limit t nil)
1270 (save-match-data
1271 (goto-char (match-end ,type-match))
1272 (c-forward-syntactic-ws))
1273 (goto-char (match-end ,type-match))))))))
1275 ;; Fontify special declarations that lacks a type.
1276 ,@(when (c-lang-const c-typeless-decl-kwds)
1277 `((,(c-make-font-lock-search-function
1278 (concat "\\<\\("
1279 (regexp-opt (c-lang-const c-typeless-decl-kwds))
1280 "\\)\\>")
1281 '((c-font-lock-declarators limit t nil)
1282 (save-match-data
1283 (goto-char (match-end 1))
1284 (c-forward-syntactic-ws))
1285 (goto-char (match-end 1)))))))
1287 ;; Fontify generic colon labels in languages that support them.
1288 ,@(when (c-lang-const c-recognize-colon-labels)
1289 `(c-font-lock-labels))))
1291 (c-lang-defconst c-complex-decl-matchers
1292 "Complex font lock matchers for types and declarations. Used on level
1293 3 and higher."
1295 ;; Note: This code in this form dumps a number of funtions into the
1296 ;; resulting constant, `c-matchers-3'. At run time, font lock will call
1297 ;; each of them as a "FUNCTION" (see Elisp page "Search-based
1298 ;; Fontification"). The font lock region is delimited by POINT and the
1299 ;; single parameter, LIMIT. Each of these functions returns NIL (thus
1300 ;; inhibiting spurious font-lock-keyword-face highlighting and another
1301 ;; call).
1303 t `(;; Initialize some things before the search functions below.
1304 c-font-lock-complex-decl-prepare
1306 ,@(if (c-major-mode-is 'objc-mode)
1307 ;; Fontify method declarations in Objective-C, but first
1308 ;; we have to put the `c-decl-end' `c-type' property on
1309 ;; all the @-style directives that haven't been handled in
1310 ;; `c-basic-matchers-before'.
1311 `(,(c-make-font-lock-search-function
1312 (c-make-keywords-re t
1313 ;; Exclude "@class" since that directive ends with a
1314 ;; semicolon anyway.
1315 (delete "@class"
1316 (append (c-lang-const c-protection-kwds)
1317 (c-lang-const c-other-decl-kwds)
1318 nil)))
1319 '((c-put-char-property (1- (match-end 1))
1320 'c-type 'c-decl-end)))
1321 c-font-lock-objc-methods))
1323 ;; Fontify all declarations, casts and normal labels.
1324 c-font-lock-declarations
1326 ;; Fontify angle bracket arglists like templates in C++.
1327 ,@(when (c-lang-const c-recognize-<>-arglists)
1328 `(c-font-lock-<>-arglists))
1330 ;; The first two rules here mostly find occurences that
1331 ;; `c-font-lock-declarations' has found already, but not
1332 ;; declarations containing blocks in the type (see note below).
1333 ;; It's also useful to fontify these everywhere to show e.g. when
1334 ;; a type keyword is accidentally used as an identifier.
1336 ;; Fontify basic types.
1337 ,(let ((re (c-make-keywords-re nil
1338 (c-lang-const c-primitive-type-kwds))))
1339 (if (c-major-mode-is 'pike-mode)
1340 ;; No symbol is a keyword after "->" in Pike.
1341 `(,(concat "\\(\\=.?\\|[^>]\\|[^-]>\\)"
1342 "\\<\\(" re "\\)\\>")
1343 2 font-lock-type-face)
1344 `(,(concat "\\<\\(" re "\\)\\>")
1345 1 'font-lock-type-face)))
1347 ;; Fontify types preceded by `c-type-prefix-kwds'.
1348 ,@(when (c-lang-const c-type-prefix-kwds)
1349 `((,(byte-compile
1350 `(lambda (limit)
1351 (c-fontify-types-and-refs
1352 ((c-promote-possible-types t)
1353 ;; The font-lock package in Emacs is known to clobber
1354 ;; `parse-sexp-lookup-properties' (when it exists).
1355 (parse-sexp-lookup-properties
1356 (cc-eval-when-compile
1357 (boundp 'parse-sexp-lookup-properties))))
1358 (save-restriction
1359 ;; Narrow to avoid going past the limit in
1360 ;; `c-forward-type'.
1361 (narrow-to-region (point) limit)
1362 (while (re-search-forward
1363 ,(concat "\\<\\("
1364 (c-make-keywords-re nil
1365 (c-lang-const c-type-prefix-kwds))
1366 "\\)\\>")
1367 limit t)
1368 (unless (c-skip-comments-and-strings limit)
1369 (c-forward-syntactic-ws)
1370 ;; Handle prefix declaration specifiers.
1371 (when (looking-at c-prefix-spec-kwds-re)
1372 (c-forward-keyword-clause 1))
1373 ,(if (c-major-mode-is 'c++-mode)
1374 `(when (and (c-forward-type)
1375 (eq (char-after) ?=))
1376 ;; In C++ we additionally check for a "class
1377 ;; X = Y" construct which is used in
1378 ;; templates, to fontify Y as a type.
1379 (forward-char)
1380 (c-forward-syntactic-ws)
1381 (c-forward-type))
1382 `(c-forward-type))
1383 )))))))))
1385 ;; Fontify symbols after closing braces as declaration
1386 ;; identifiers under the assumption that they are part of
1387 ;; declarations like "class Foo { ... } foo;". It's too
1388 ;; expensive to check this accurately by skipping past the
1389 ;; brace block, so we use the heuristic that it's such a
1390 ;; declaration if the first identifier is on the same line as
1391 ;; the closing brace. `c-font-lock-declarations' will later
1392 ;; override it if it turns out to be an new declaration, but
1393 ;; it will be wrong if it's an expression (see the test
1394 ;; decls-8.cc).
1395 ,@(when (c-lang-const c-opt-block-decls-with-vars-key)
1396 `((,(c-make-font-lock-search-function
1397 (concat "}"
1398 (c-lang-const c-single-line-syntactic-ws)
1399 "\\(" ; 1 + c-single-line-syntactic-ws-depth
1400 (c-lang-const c-type-decl-prefix-key)
1401 "\\|"
1402 (c-lang-const c-symbol-key)
1403 "\\)")
1404 `((c-font-lock-declarators limit t nil)
1405 (progn
1406 (c-put-char-property (match-beginning 0) 'c-type
1407 'c-decl-id-start)
1408 (goto-char (match-beginning
1409 ,(1+ (c-lang-const
1410 c-single-line-syntactic-ws-depth)))))
1411 (goto-char (match-end 0)))))))
1413 ;; Fontify the type in C++ "new" expressions.
1414 ,@(when (c-major-mode-is 'c++-mode)
1415 ;; This pattern is a probably a "(MATCHER . ANCHORED-HIGHLIGHTER)"
1416 ;; (see Elisp page "Search-based Fontification").
1417 `(("\\<new\\>"
1418 (c-font-lock-c++-new))))
1421 (defun c-font-lock-labels (limit)
1422 ;; Fontify all statement labels from the point to LIMIT. Assumes
1423 ;; that strings and comments have been fontified already. Nil is
1424 ;; always returned.
1426 ;; Note: This function is only used on decoration level 2; this is
1427 ;; taken care of directly by the gargantuan
1428 ;; `c-font-lock-declarations' on higher levels.
1430 ;; This function might do hidden buffer changes.
1432 (let (continue-pos id-start
1433 ;; The font-lock package in Emacs is known to clobber
1434 ;; `parse-sexp-lookup-properties' (when it exists).
1435 (parse-sexp-lookup-properties
1436 (cc-eval-when-compile
1437 (boundp 'parse-sexp-lookup-properties))))
1439 (while (re-search-forward ":[^:]" limit t)
1440 (setq continue-pos (point))
1441 (goto-char (match-beginning 0))
1442 (unless (c-skip-comments-and-strings limit)
1444 (c-backward-syntactic-ws)
1445 (and (setq id-start (c-on-identifier))
1447 (not (get-text-property id-start 'face))
1449 (progn
1450 (goto-char id-start)
1451 (c-backward-syntactic-ws)
1453 ;; Check for a char that precedes a statement.
1454 (memq (char-before) '(?\} ?\{ ?\;))
1455 ;; Check for a preceding label. We exploit the font
1456 ;; locking made earlier by this function.
1457 (and (eq (char-before) ?:)
1458 (progn
1459 (backward-char)
1460 (c-backward-syntactic-ws)
1461 (not (bobp)))
1462 (eq (get-text-property (1- (point)) 'face)
1463 c-label-face-name))
1464 ;; Check for a keyword that precedes a statement.
1465 (c-after-conditional)))
1467 (progn
1468 ;; Got a label.
1469 (goto-char id-start)
1470 (looking-at c-symbol-key)
1471 (c-put-font-lock-face (match-beginning 0) (match-end 0)
1472 c-label-face-name)))
1474 (goto-char continue-pos))))
1475 nil)
1477 (c-lang-defconst c-basic-matchers-after
1478 "Font lock matchers for various things that should be fontified after
1479 generic casts and declarations are fontified. Used on level 2 and
1480 higher."
1482 t `(;; Fontify the identifiers inside enum lists. (The enum type
1483 ;; name is handled by `c-simple-decl-matchers' or
1484 ;; `c-complex-decl-matchers' below.
1485 ,@(when (c-lang-const c-brace-id-list-kwds)
1486 `((,(c-make-font-lock-search-function
1487 (concat
1488 "\\<\\("
1489 (c-make-keywords-re nil (c-lang-const c-brace-id-list-kwds))
1490 "\\)\\>"
1491 ;; Disallow various common punctuation chars that can't come
1492 ;; before the '{' of the enum list, to avoid searching too far.
1493 "[^\]\[{}();,/#=]*"
1494 "{")
1495 '((c-font-lock-declarators limit t nil)
1496 (save-match-data
1497 (goto-char (match-end 0))
1498 (c-put-char-property (1- (point)) 'c-type
1499 'c-decl-id-start)
1500 (c-forward-syntactic-ws))
1501 (goto-char (match-end 0)))))))
1503 ;; Fontify labels after goto etc.
1504 ,@(when (c-lang-const c-before-label-kwds)
1505 `(;; (Got three different interpretation levels here,
1506 ;; which makes it a bit complicated: 1) The backquote
1507 ;; stuff is expanded when compiled or loaded, 2) the
1508 ;; eval form is evaluated at font-lock setup (to
1509 ;; substitute c-label-face-name correctly), and 3) the
1510 ;; resulting structure is interpreted during
1511 ;; fontification.)
1512 (eval
1513 . ,(let* ((c-before-label-re
1514 (c-make-keywords-re nil
1515 (c-lang-const c-before-label-kwds))))
1516 `(list
1517 ,(concat "\\<\\(" c-before-label-re "\\)\\>"
1518 "\\s *"
1519 "\\(" ; identifier-offset
1520 (c-lang-const c-symbol-key)
1521 "\\)")
1522 (list ,(+ (regexp-opt-depth c-before-label-re) 2)
1523 c-label-face-name nil t))))))
1525 ;; Fontify the clauses after various keywords.
1526 ,@(when (or (c-lang-const c-type-list-kwds)
1527 (c-lang-const c-ref-list-kwds)
1528 (c-lang-const c-colon-type-list-kwds)
1529 (c-lang-const c-paren-type-kwds))
1530 `((,(c-make-font-lock-search-function
1531 (concat "\\<\\("
1532 (c-make-keywords-re nil
1533 (append (c-lang-const c-type-list-kwds)
1534 (c-lang-const c-ref-list-kwds)
1535 (c-lang-const c-colon-type-list-kwds)
1536 (c-lang-const c-paren-type-kwds)))
1537 "\\)\\>")
1538 '((c-fontify-types-and-refs ((c-promote-possible-types t))
1539 (c-forward-keyword-clause 1)
1540 (if (> (point) limit) (goto-char limit))))))))
1543 (c-lang-defconst c-matchers-1
1544 t (c-lang-const c-cpp-matchers))
1546 (c-lang-defconst c-matchers-2
1547 t (append (c-lang-const c-matchers-1)
1548 (c-lang-const c-basic-matchers-before)
1549 (c-lang-const c-simple-decl-matchers)
1550 (c-lang-const c-basic-matchers-after)))
1552 (c-lang-defconst c-matchers-3
1553 t (append (c-lang-const c-matchers-1)
1554 (c-lang-const c-basic-matchers-before)
1555 (c-lang-const c-complex-decl-matchers)
1556 (c-lang-const c-basic-matchers-after)))
1558 (defun c-compose-keywords-list (base-list)
1559 ;; Incorporate the font lock keyword lists according to
1560 ;; `c-doc-comment-style' on the given keyword list and return it.
1561 ;; This is used in the function bindings of the
1562 ;; `*-font-lock-keywords-*' symbols since we have to build the list
1563 ;; when font-lock is initialized.
1565 (unless (memq c-doc-face-name c-literal-faces)
1566 (setq c-literal-faces (cons c-doc-face-name c-literal-faces)))
1568 (let* ((doc-keywords
1569 (if (consp (car-safe c-doc-comment-style))
1570 (cdr-safe (or (assq c-buffer-is-cc-mode c-doc-comment-style)
1571 (assq 'other c-doc-comment-style)))
1572 c-doc-comment-style))
1573 (list (nconc (apply 'nconc
1574 (mapcar
1575 (lambda (doc-style)
1576 (let ((sym (intern
1577 (concat (symbol-name doc-style)
1578 "-font-lock-keywords"))))
1579 (cond ((fboundp sym)
1580 (funcall sym))
1581 ((boundp sym)
1582 (append (eval sym) nil)))))
1583 (if (listp doc-keywords)
1584 doc-keywords
1585 (list doc-keywords))))
1586 base-list)))
1588 ;; Kludge: If `c-font-lock-complex-decl-prepare' is on the list we
1589 ;; move it first since the doc comment font lockers might add
1590 ;; `c-type' text properties, so they have to be cleared before that.
1591 (when (memq 'c-font-lock-complex-decl-prepare list)
1592 (setq list (cons 'c-font-lock-complex-decl-prepare
1593 (delq 'c-font-lock-complex-decl-prepare
1594 (append list nil)))))
1596 list))
1598 (defun c-override-default-keywords (def-var)
1599 ;; This is used to override the value on a `*-font-lock-keywords'
1600 ;; variable only if it's nil or has the same value as one of the
1601 ;; `*-font-lock-keywords-*' variables. Older font-lock packages
1602 ;; define a default value for `*-font-lock-keywords' which we want
1603 ;; to override, but we should otoh avoid clobbering a user setting.
1604 ;; This heuristic for that isn't perfect, but I can't think of any
1605 ;; better. /mast
1606 (when (and (boundp def-var)
1607 (memq (symbol-value def-var)
1608 (cons nil
1609 (mapcar
1610 (lambda (suffix)
1611 (let ((sym (intern (concat (symbol-name def-var)
1612 suffix))))
1613 (and (boundp sym) (symbol-value sym))))
1614 '("-1" "-2" "-3")))))
1615 ;; The overriding is done by unbinding the variable so that the normal
1616 ;; defvar will install its default value later on.
1617 (makunbound def-var)))
1620 ;;; C.
1622 (c-override-default-keywords 'c-font-lock-keywords)
1624 (defconst c-font-lock-keywords-1 (c-lang-const c-matchers-1 c)
1625 "Minimal font locking for C mode.
1626 Fontifies only preprocessor directives (in addition to the syntactic
1627 fontification of strings and comments).")
1629 (defconst c-font-lock-keywords-2 (c-lang-const c-matchers-2 c)
1630 "Fast normal font locking for C mode.
1631 In addition to `c-font-lock-keywords-1', this adds fontification of
1632 keywords, simple types, declarations that are easy to recognize, the
1633 user defined types on `c-font-lock-extra-types', and the doc comment
1634 styles specified by `c-doc-comment-style'.")
1636 (defconst c-font-lock-keywords-3 (c-lang-const c-matchers-3 c)
1637 "Accurate normal font locking for C mode.
1638 Like `c-font-lock-keywords-2' but detects declarations in a more
1639 accurate way that works in most cases for arbitrary types without the
1640 need for `c-font-lock-extra-types'.")
1642 (defvar c-font-lock-keywords c-font-lock-keywords-3
1643 "Default expressions to highlight in C mode.")
1645 (defun c-font-lock-keywords-2 ()
1646 (c-compose-keywords-list c-font-lock-keywords-2))
1647 (defun c-font-lock-keywords-3 ()
1648 (c-compose-keywords-list c-font-lock-keywords-3))
1649 (defun c-font-lock-keywords ()
1650 (c-compose-keywords-list c-font-lock-keywords))
1653 ;;; C++.
1655 (defun c-font-lock-c++-new (limit)
1656 ;; Assuming point is after a "new" word, check that it isn't inside
1657 ;; a string or comment, and if so try to fontify the type in the
1658 ;; allocation expression. Nil is always returned.
1660 ;; As usual, C++ takes the prize in coming up with a hard to parse
1661 ;; syntax. :P
1663 ;; This function might do hidden buffer changes.
1665 (unless (c-skip-comments-and-strings limit)
1666 (save-excursion
1667 (catch 'false-alarm
1668 ;; A "new" keyword is followed by one to three expressions, where
1669 ;; the type is the middle one, and the only required part.
1670 (let (expr1-pos expr2-pos
1671 ;; Enable recording of identifier ranges in `c-forward-type'
1672 ;; etc for later fontification. Not using
1673 ;; `c-fontify-types-and-refs' here since the ranges should
1674 ;; be fontified selectively only when an allocation
1675 ;; expression is successfully recognized.
1676 (c-record-type-identifiers t)
1677 c-record-ref-identifiers
1678 ;; The font-lock package in Emacs is known to clobber
1679 ;; `parse-sexp-lookup-properties' (when it exists).
1680 (parse-sexp-lookup-properties
1681 (cc-eval-when-compile
1682 (boundp 'parse-sexp-lookup-properties))))
1683 (c-forward-syntactic-ws)
1685 ;; The first placement arglist is always parenthesized, if it
1686 ;; exists.
1687 (when (eq (char-after) ?\()
1688 (setq expr1-pos (1+ (point)))
1689 (condition-case nil
1690 (c-forward-sexp)
1691 (scan-error (throw 'false-alarm t)))
1692 (c-forward-syntactic-ws))
1694 ;; The second expression is either a type followed by some "*" or
1695 ;; "[...]" or similar, or a parenthesized type followed by a full
1696 ;; identifierless declarator.
1697 (setq expr2-pos (1+ (point)))
1698 (cond ((eq (char-after) ?\())
1699 ((let ((c-promote-possible-types t))
1700 (c-forward-type)))
1701 (t (setq expr2-pos nil)))
1703 (when expr1-pos
1704 (cond
1705 ((not expr2-pos)
1706 ;; No second expression, so the first has to be a
1707 ;; parenthesized type.
1708 (goto-char expr1-pos)
1709 (let ((c-promote-possible-types t))
1710 (c-forward-type)))
1712 ((eq (char-before expr2-pos) ?\()
1713 ;; Got two parenthesized expressions, so we have to look
1714 ;; closer at them to decide which is the type. No need to
1715 ;; handle `c-record-ref-identifiers' since all references
1716 ;; has already been handled by other fontification rules.
1717 (let (expr1-res expr2-res)
1719 (goto-char expr1-pos)
1720 (when (setq expr1-res (c-forward-type))
1721 (unless (looking-at
1722 (cc-eval-when-compile
1723 (concat (c-lang-const c-symbol-start c++)
1724 "\\|[*:\)\[]")))
1725 ;; There's something after the would-be type that
1726 ;; can't be there, so this is a placement arglist.
1727 (setq expr1-res nil)))
1729 (goto-char expr2-pos)
1730 (when (setq expr2-res (c-forward-type))
1731 (unless (looking-at
1732 (cc-eval-when-compile
1733 (concat (c-lang-const c-symbol-start c++)
1734 "\\|[*:\)\[]")))
1735 ;; There's something after the would-be type that can't
1736 ;; be there, so this is an initialization expression.
1737 (setq expr2-res nil))
1738 (when (and (c-go-up-list-forward)
1739 (progn (c-forward-syntactic-ws)
1740 (eq (char-after) ?\()))
1741 ;; If there's a third initialization expression
1742 ;; then the second one is the type, so demote the
1743 ;; first match.
1744 (setq expr1-res nil)))
1746 ;; We fontify the most likely type, with a preference for
1747 ;; the first argument since a placement arglist is more
1748 ;; unusual than an initializer.
1749 (cond ((memq expr1-res '(t known prefix)))
1750 ((memq expr2-res '(t known prefix)))
1751 ((eq expr1-res 'found)
1752 (let ((c-promote-possible-types t))
1753 (goto-char expr1-pos)
1754 (c-forward-type)))
1755 ((eq expr2-res 'found)
1756 (let ((c-promote-possible-types t))
1757 (goto-char expr2-pos)
1758 (c-forward-type)))
1759 ((and (eq expr1-res 'maybe) (not expr2-res))
1760 (let ((c-promote-possible-types t))
1761 (goto-char expr1-pos)
1762 (c-forward-type)))
1763 ((and (not expr1-res) (eq expr2-res 'maybe))
1764 (let ((c-promote-possible-types t))
1765 (goto-char expr2-pos)
1766 (c-forward-type)))
1767 ;; If both type matches are 'maybe then we're
1768 ;; too uncertain to promote either of them.
1769 )))))
1771 ;; Fontify the type that now is recorded in
1772 ;; `c-record-type-identifiers', if any.
1773 (c-fontify-recorded-types-and-refs)))))
1774 nil)
1776 (c-override-default-keywords 'c++-font-lock-keywords)
1778 (defconst c++-font-lock-keywords-1 (c-lang-const c-matchers-1 c++)
1779 "Minimal font locking for C++ mode.
1780 Fontifies only preprocessor directives (in addition to the syntactic
1781 fontification of strings and comments).")
1783 (defconst c++-font-lock-keywords-2 (c-lang-const c-matchers-2 c++)
1784 "Fast normal font locking for C++ mode.
1785 In addition to `c++-font-lock-keywords-1', this adds fontification of
1786 keywords, simple types, declarations that are easy to recognize, the
1787 user defined types on `c++-font-lock-extra-types', and the doc comment
1788 styles specified by `c-doc-comment-style'.")
1790 (defconst c++-font-lock-keywords-3 (c-lang-const c-matchers-3 c++)
1791 "Accurate normal font locking for C++ mode.
1792 Like `c++-font-lock-keywords-2' but detects declarations in a more
1793 accurate way that works in most cases for arbitrary types without the
1794 need for `c++-font-lock-extra-types'.")
1796 (defvar c++-font-lock-keywords c++-font-lock-keywords-3
1797 "Default expressions to highlight in C++ mode.")
1799 (defun c++-font-lock-keywords-2 ()
1800 (c-compose-keywords-list c++-font-lock-keywords-2))
1801 (defun c++-font-lock-keywords-3 ()
1802 (c-compose-keywords-list c++-font-lock-keywords-3))
1803 (defun c++-font-lock-keywords ()
1804 (c-compose-keywords-list c++-font-lock-keywords))
1807 ;;; Objective-C.
1809 (defun c-font-lock-objc-method ()
1810 ;; Assuming the point is after the + or - that starts an Objective-C
1811 ;; method declaration, fontify it. This must be done before normal
1812 ;; casts, declarations and labels are fontified since they will get
1813 ;; false matches in these things.
1815 ;; This function might do hidden buffer changes.
1817 (c-fontify-types-and-refs
1818 ((first t)
1819 (c-promote-possible-types t))
1821 (while (and
1822 (progn
1823 (c-forward-syntactic-ws)
1825 ;; An optional method type.
1826 (if (eq (char-after) ?\()
1827 (progn
1828 (forward-char)
1829 (c-forward-syntactic-ws)
1830 (c-forward-type)
1831 (prog1 (c-go-up-list-forward)
1832 (c-forward-syntactic-ws)))
1835 ;; The name. The first time it's the first part of
1836 ;; the function name, the rest of the time it's an
1837 ;; argument name.
1838 (looking-at c-symbol-key)
1839 (progn
1840 (goto-char (match-end 0))
1841 (c-put-font-lock-face (match-beginning 0)
1842 (point)
1843 (if first
1844 'font-lock-function-name-face
1845 'font-lock-variable-name-face))
1846 (c-forward-syntactic-ws)
1848 ;; Another optional part of the function name.
1849 (when (looking-at c-symbol-key)
1850 (goto-char (match-end 0))
1851 (c-put-font-lock-face (match-beginning 0)
1852 (point)
1853 'font-lock-function-name-face)
1854 (c-forward-syntactic-ws))
1856 ;; There's another argument if a colon follows.
1857 (eq (char-after) ?:)))
1858 (forward-char)
1859 (setq first nil))))
1861 (defun c-font-lock-objc-methods (limit)
1862 ;; Fontify method declarations in Objective-C. Nil is always
1863 ;; returned.
1865 ;; This function might do hidden buffer changes.
1867 (let (;; The font-lock package in Emacs is known to clobber
1868 ;; `parse-sexp-lookup-properties' (when it exists).
1869 (parse-sexp-lookup-properties
1870 (cc-eval-when-compile
1871 (boundp 'parse-sexp-lookup-properties))))
1873 (c-find-decl-spots
1874 limit
1875 "[-+]"
1877 (lambda (match-pos inside-macro)
1878 (forward-char)
1879 (c-font-lock-objc-method))))
1880 nil)
1882 (c-override-default-keywords 'objc-font-lock-keywords)
1884 (defconst objc-font-lock-keywords-1 (c-lang-const c-matchers-1 objc)
1885 "Minimal font locking for Objective-C mode.
1886 Fontifies only compiler directives (in addition to the syntactic
1887 fontification of strings and comments).")
1889 (defconst objc-font-lock-keywords-2 (c-lang-const c-matchers-2 objc)
1890 "Fast normal font locking for Objective-C mode.
1891 In addition to `objc-font-lock-keywords-1', this adds fontification of
1892 keywords, simple types, declarations that are easy to recognize, the
1893 user defined types on `objc-font-lock-extra-types', and the doc
1894 comment styles specified by `c-doc-comment-style'.")
1896 (defconst objc-font-lock-keywords-3 (c-lang-const c-matchers-3 objc)
1897 "Accurate normal font locking for Objective-C mode.
1898 Like `objc-font-lock-keywords-2' but detects declarations in a more
1899 accurate way that works in most cases for arbitrary types without the
1900 need for `objc-font-lock-extra-types'.")
1902 (defvar objc-font-lock-keywords objc-font-lock-keywords-3
1903 "Default expressions to highlight in Objective-C mode.")
1905 (defun objc-font-lock-keywords-2 ()
1906 (c-compose-keywords-list objc-font-lock-keywords-2))
1907 (defun objc-font-lock-keywords-3 ()
1908 (c-compose-keywords-list objc-font-lock-keywords-3))
1909 (defun objc-font-lock-keywords ()
1910 (c-compose-keywords-list objc-font-lock-keywords))
1912 ;; Kludge to override the default value that
1913 ;; `objc-font-lock-extra-types' might have gotten from the font-lock
1914 ;; package. The value replaced here isn't relevant now anyway since
1915 ;; those types are builtin and therefore listed directly in
1916 ;; `c-primitive-type-kwds'.
1917 (when (equal (sort (append objc-font-lock-extra-types nil) 'string-lessp)
1918 '("BOOL" "Class" "IMP" "SEL"))
1919 (setq objc-font-lock-extra-types
1920 (cc-eval-when-compile (list (concat "[" c-upper "]\\sw*")))))
1923 ;;; Java.
1925 (c-override-default-keywords 'java-font-lock-keywords)
1927 (defconst java-font-lock-keywords-1 (c-lang-const c-matchers-1 java)
1928 "Minimal font locking for Java mode.
1929 Fontifies nothing except the syntactic fontification of strings and
1930 comments.")
1932 (defconst java-font-lock-keywords-2 (c-lang-const c-matchers-2 java)
1933 "Fast normal font locking for Java mode.
1934 In addition to `java-font-lock-keywords-1', this adds fontification of
1935 keywords, simple types, declarations that are easy to recognize, the
1936 user defined types on `java-font-lock-extra-types', and the doc
1937 comment styles specified by `c-doc-comment-style'.")
1939 (defconst java-font-lock-keywords-3 (c-lang-const c-matchers-3 java)
1940 "Accurate normal font locking for Java mode.
1941 Like `java-font-lock-keywords-2' but detects declarations in a more
1942 accurate way that works in most cases for arbitrary types without the
1943 need for `java-font-lock-extra-types'.")
1945 (defvar java-font-lock-keywords java-font-lock-keywords-3
1946 "Default expressions to highlight in Java mode.")
1948 (defun java-font-lock-keywords-2 ()
1949 (c-compose-keywords-list java-font-lock-keywords-2))
1950 (defun java-font-lock-keywords-3 ()
1951 (c-compose-keywords-list java-font-lock-keywords-3))
1952 (defun java-font-lock-keywords ()
1953 (c-compose-keywords-list java-font-lock-keywords))
1956 ;;; CORBA IDL.
1958 (c-override-default-keywords 'idl-font-lock-keywords)
1960 (defconst idl-font-lock-keywords-1 (c-lang-const c-matchers-1 idl)
1961 "Minimal font locking for CORBA IDL mode.
1962 Fontifies nothing except the syntactic fontification of strings and
1963 comments.")
1965 (defconst idl-font-lock-keywords-2 (c-lang-const c-matchers-2 idl)
1966 "Fast normal font locking for CORBA IDL mode.
1967 In addition to `idl-font-lock-keywords-1', this adds fontification of
1968 keywords, simple types, declarations that are easy to recognize, the
1969 user defined types on `idl-font-lock-extra-types', and the doc comment
1970 styles specified by `c-doc-comment-style'.")
1972 (defconst idl-font-lock-keywords-3 (c-lang-const c-matchers-3 idl)
1973 "Accurate normal font locking for CORBA IDL mode.
1974 Like `idl-font-lock-keywords-2' but detects declarations in a more
1975 accurate way that works in most cases for arbitrary types without the
1976 need for `idl-font-lock-extra-types'.")
1978 (defvar idl-font-lock-keywords idl-font-lock-keywords-3
1979 "Default expressions to highlight in CORBA IDL mode.")
1981 (defun idl-font-lock-keywords-2 ()
1982 (c-compose-keywords-list idl-font-lock-keywords-2))
1983 (defun idl-font-lock-keywords-3 ()
1984 (c-compose-keywords-list idl-font-lock-keywords-3))
1985 (defun idl-font-lock-keywords ()
1986 (c-compose-keywords-list idl-font-lock-keywords))
1989 ;;; Pike.
1991 (c-override-default-keywords 'pike-font-lock-keywords)
1993 (defconst pike-font-lock-keywords-1 (c-lang-const c-matchers-1 pike)
1994 "Minimal font locking for Pike mode.
1995 Fontifies only preprocessor directives (in addition to the syntactic
1996 fontification of strings and comments).")
1998 (defconst pike-font-lock-keywords-2 (c-lang-const c-matchers-2 pike)
1999 "Fast normal font locking for Pike mode.
2000 In addition to `pike-font-lock-keywords-1', this adds fontification of
2001 keywords, simple types, declarations that are easy to recognize, the
2002 user defined types on `pike-font-lock-extra-types', and the doc
2003 comment styles specified by `c-doc-comment-style'.")
2005 (defconst pike-font-lock-keywords-3 (c-lang-const c-matchers-3 pike)
2006 "Accurate normal font locking for Pike mode.
2007 Like `pike-font-lock-keywords-2' but detects declarations in a more
2008 accurate way that works in most cases for arbitrary types without the
2009 need for `pike-font-lock-extra-types'.")
2011 (defvar pike-font-lock-keywords pike-font-lock-keywords-3
2012 "Default expressions to highlight in Pike mode.")
2014 (defun pike-font-lock-keywords-2 ()
2015 (c-compose-keywords-list pike-font-lock-keywords-2))
2016 (defun pike-font-lock-keywords-3 ()
2017 (c-compose-keywords-list pike-font-lock-keywords-3))
2018 (defun pike-font-lock-keywords ()
2019 (c-compose-keywords-list pike-font-lock-keywords))
2022 ;;; Doc comments.
2024 (defun c-font-lock-doc-comments (prefix limit keywords)
2025 ;; Fontify the comments between the point and LIMIT whose start
2026 ;; matches PREFIX with `c-doc-face-name'. Assumes comments have been
2027 ;; fontified with `font-lock-comment-face' already. nil is always
2028 ;; returned.
2030 ;; After the fontification of a matching comment, fontification
2031 ;; according to KEYWORDS is applied inside it. It's a list like
2032 ;; `font-lock-keywords' except that anchored matches and eval
2033 ;; clauses aren't supported and that some abbreviated forms can't be
2034 ;; used. The buffer is narrowed to the comment while KEYWORDS is
2035 ;; applied; leading comment starters are included but trailing
2036 ;; comment enders for block comment are not.
2038 ;; Note that faces added through KEYWORDS should never replace the
2039 ;; existing `c-doc-face-name' face since the existence of that face
2040 ;; is used as a flag in other code to skip comments.
2042 ;; This function might do hidden buffer changes.
2044 (let (comment-beg region-beg)
2045 (if (eq (get-text-property (point) 'face)
2046 'font-lock-comment-face)
2047 ;; Handle the case when the fontified region starts inside a
2048 ;; comment.
2049 (let ((range (c-literal-limits)))
2050 (setq region-beg (point))
2051 (when range
2052 (goto-char (car range)))
2053 (when (looking-at prefix)
2054 (setq comment-beg (point)))))
2056 (while (or
2057 comment-beg
2059 ;; Search for the prefix until a match is found at the start
2060 ;; of a comment.
2061 (while (when (re-search-forward prefix limit t)
2062 (setq comment-beg (match-beginning 0))
2063 (or (not (c-got-face-at comment-beg
2064 c-literal-faces))
2065 (and (/= comment-beg (point-min))
2066 (c-got-face-at (1- comment-beg)
2067 c-literal-faces))))
2068 (setq comment-beg nil))
2069 (setq region-beg comment-beg))
2071 (if (eq (elt (parse-partial-sexp comment-beg (+ comment-beg 2)) 7) t)
2072 ;; Collect a sequence of doc style line comments.
2073 (progn
2074 (goto-char comment-beg)
2075 (while (and (progn
2076 (c-forward-single-comment)
2077 (skip-syntax-forward " ")
2078 (< (point) limit))
2079 (looking-at prefix))))
2080 (goto-char comment-beg)
2081 (c-forward-single-comment))
2082 (if (> (point) limit) (goto-char limit))
2083 (setq comment-beg nil)
2085 (let ((region-end (point))
2086 (keylist keywords) keyword matcher highlights)
2087 (c-put-font-lock-face region-beg region-end c-doc-face-name)
2088 (save-restriction
2089 ;; Narrow to the doc comment. Among other things, this
2090 ;; helps by making "^" match at the start of the comment.
2091 ;; Do not include a trailing block comment ender, though.
2092 (and (> region-end (1+ region-beg))
2093 (progn (goto-char region-end)
2094 (backward-char 2)
2095 (looking-at "\\*/"))
2096 (setq region-end (point)))
2097 (narrow-to-region region-beg region-end)
2099 (while keylist
2100 (setq keyword (car keylist)
2101 keylist (cdr keylist)
2102 matcher (car keyword))
2103 (goto-char region-beg)
2104 (while (if (stringp matcher)
2105 (re-search-forward matcher region-end t)
2106 (funcall matcher region-end))
2107 (setq highlights (cdr keyword))
2108 (if (consp (car highlights))
2109 (while highlights
2110 (font-lock-apply-highlight (car highlights))
2111 (setq highlights (cdr highlights)))
2112 (font-lock-apply-highlight highlights))))
2114 (goto-char region-end)))))
2115 nil)
2116 (put 'c-font-lock-doc-comments 'lisp-indent-function 2)
2118 (defun c-find-invalid-doc-markup (regexp limit)
2119 ;; Used to fontify invalid markup in doc comments after the correct
2120 ;; ones have been fontified: Find the first occurence of REGEXP
2121 ;; between the point and LIMIT that only is fontified with
2122 ;; `c-doc-face-name'. If a match is found then submatch 0 surrounds
2123 ;; the first char and t is returned, otherwise nil is returned.
2125 ;; This function might do hidden buffer changes.
2126 (let (start)
2127 (while (if (re-search-forward regexp limit t)
2128 (not (eq (get-text-property
2129 (setq start (match-beginning 0)) 'face)
2130 c-doc-face-name))
2131 (setq start nil)))
2132 (when start
2133 (store-match-data (list (copy-marker start)
2134 (copy-marker (1+ start))))
2135 t)))
2137 ;; GtkDoc patterns contributed by Masatake YAMATO <jet@gyve.org>.
2139 (defconst gtkdoc-font-lock-doc-comments
2140 (let ((symbol "[a-zA-Z0-9_]+")
2141 (header "^ \\* "))
2142 `((,(concat header "\\(" symbol "\\):[ \t]*$")
2143 1 ,c-doc-markup-face-name prepend nil)
2144 (,(concat symbol "()")
2145 0 ,c-doc-markup-face-name prepend nil)
2146 (,(concat header "\\(" "@" symbol "\\):")
2147 1 ,c-doc-markup-face-name prepend nil)
2148 (,(concat "[#%@]" symbol)
2149 0 ,c-doc-markup-face-name prepend nil))
2152 (defconst gtkdoc-font-lock-doc-protection
2153 `(("< \\(public\\|private\\|protected\\) >"
2154 1 ,c-doc-markup-face-name prepend nil)))
2156 (defconst gtkdoc-font-lock-keywords
2157 `((,(lambda (limit)
2158 (c-font-lock-doc-comments "/\\*\\*$" limit
2159 gtkdoc-font-lock-doc-comments)
2160 (c-font-lock-doc-comments "/\\*< " limit
2161 gtkdoc-font-lock-doc-protection)
2162 ))))
2164 ;; Javadoc.
2166 (defconst javadoc-font-lock-doc-comments
2167 `(("{@[a-z]+[^}\n\r]*}" ; "{@foo ...}" markup.
2168 0 ,c-doc-markup-face-name prepend nil)
2169 ("^\\(/\\*\\)?\\(\\s \\|\\*\\)*\\(@[a-z]+\\)" ; "@foo ..." markup.
2170 3 ,c-doc-markup-face-name prepend nil)
2171 (,(concat "</?\\sw" ; HTML tags.
2172 "\\("
2173 (concat "\\sw\\|\\s \\|[=\n\r*.:]\\|"
2174 "\"[^\"]*\"\\|'[^']*'")
2175 "\\)*>")
2176 0 ,c-doc-markup-face-name prepend nil)
2177 ("&\\(\\sw\\|[.:]\\)+;" ; HTML entities.
2178 0 ,c-doc-markup-face-name prepend nil)
2179 ;; Fontify remaining markup characters as invalid. Note
2180 ;; that the Javadoc spec is hazy about when "@" is
2181 ;; allowed in non-markup use.
2182 (,(lambda (limit)
2183 (c-find-invalid-doc-markup "[<>&]\\|{@" limit))
2184 0 'font-lock-warning-face prepend nil)))
2186 (defconst javadoc-font-lock-keywords
2187 `((,(lambda (limit)
2188 (c-font-lock-doc-comments "/\\*\\*" limit
2189 javadoc-font-lock-doc-comments)))))
2191 ;; Pike autodoc.
2193 (defconst autodoc-decl-keywords
2194 ;; Adorned regexp matching the keywords that introduce declarations
2195 ;; in Pike Autodoc.
2196 (cc-eval-when-compile
2197 (c-make-keywords-re t '("@decl" "@elem" "@index" "@member") 'pike-mode)))
2199 (defconst autodoc-decl-type-keywords
2200 ;; Adorned regexp matching the keywords that are followed by a type.
2201 (cc-eval-when-compile
2202 (c-make-keywords-re t '("@elem" "@member") 'pike-mode)))
2204 (defun autodoc-font-lock-line-markup (limit)
2205 ;; Fontify all line oriented keywords between the point and LIMIT.
2206 ;; Nil is always returned.
2208 ;; This function might do hidden buffer changes.
2210 (let ((line-re (concat "^\\(\\(/\\*!\\|\\s *\\("
2211 c-current-comment-prefix
2212 "\\)\\)\\s *\\)@[A-Za-z_-]+\\(\\s \\|$\\)"))
2213 (markup-faces (list c-doc-markup-face-name c-doc-face-name)))
2215 (while (re-search-forward line-re limit t)
2216 (goto-char (match-end 1))
2218 (if (looking-at autodoc-decl-keywords)
2219 (let* ((kwd-pos (point))
2220 (start (match-end 1))
2221 (pos start)
2222 end)
2224 (c-put-font-lock-face (point) pos markup-faces)
2226 ;; Put a declaration end mark at the markup keyword and
2227 ;; remove the faces from the rest of the line so that it
2228 ;; gets refontified as a declaration later on by
2229 ;; `c-font-lock-declarations'.
2230 (c-put-char-property (1- pos) 'c-type 'c-decl-end)
2231 (goto-char pos)
2232 (while (progn
2233 (end-of-line)
2234 (setq end (point))
2235 (and (eq (char-before) ?@)
2236 (not (eobp))
2237 (progn (forward-char)
2238 (skip-syntax-forward " ")
2239 (looking-at c-current-comment-prefix))))
2240 (goto-char (match-end 0))
2241 (c-remove-font-lock-face pos (1- end))
2242 (c-put-font-lock-face (1- end) end markup-faces)
2243 (setq pos (point)))
2245 ;; Include the final newline in the removed area. This
2246 ;; has no visual effect but it avoids some tricky special
2247 ;; cases in the testsuite wrt the differences in string
2248 ;; fontification in Emacs vs XEmacs.
2249 (c-remove-font-lock-face pos (min (1+ (point)) (point-max)))
2251 ;; Must handle string literals explicitly inside the declaration.
2252 (goto-char start)
2253 (while (re-search-forward
2254 "\"\\([^\\\"]\\|\\\\.\\)*\"\\|'\\([^\\']\\|\\\\.\\)*'"
2255 end 'move)
2256 (c-put-font-lock-string-face (match-beginning 0)
2257 (point)))
2259 ;; Fontify types after keywords that always are followed
2260 ;; by them.
2261 (goto-char kwd-pos)
2262 (when (looking-at autodoc-decl-type-keywords)
2263 (c-fontify-types-and-refs ((c-promote-possible-types t))
2264 (goto-char start)
2265 (c-forward-syntactic-ws)
2266 (c-forward-type))))
2268 ;; Mark each whole line as markup, as long as the logical line
2269 ;; continues.
2270 (while (progn
2271 (c-put-font-lock-face (point)
2272 (progn (end-of-line) (point))
2273 markup-faces)
2274 (and (eq (char-before) ?@)
2275 (not (eobp))
2276 (progn (forward-char)
2277 (skip-syntax-forward " ")
2278 (looking-at c-current-comment-prefix))))
2279 (goto-char (match-end 0))))))
2281 nil)
2283 (defconst autodoc-font-lock-doc-comments
2284 `(("@\\(\\w+{\\|\\[\\([^\]@\n\r]\\|@@\\)*\\]\\|[@}]\\|$\\)"
2285 ;; In-text markup.
2286 0 ,c-doc-markup-face-name prepend nil)
2287 (autodoc-font-lock-line-markup)
2288 ;; Fontify remaining markup characters as invalid.
2289 (,(lambda (limit)
2290 (c-find-invalid-doc-markup "@" limit))
2291 0 'font-lock-warning-face prepend nil)
2294 (defun autodoc-font-lock-keywords ()
2295 ;; Note that we depend on that `c-current-comment-prefix' has got
2296 ;; its proper value here.
2298 ;; This function might do hidden buffer changes.
2300 ;; The `c-type' text property with `c-decl-end' is used to mark the
2301 ;; end of the `autodoc-decl-keywords' occurrences to fontify the
2302 ;; following declarations.
2303 (setq c-type-decl-end-used t)
2305 `((,(lambda (limit)
2306 (c-font-lock-doc-comments "/[*/]!" limit
2307 autodoc-font-lock-doc-comments)))))
2310 ;; 2006-07-10: awk-font-lock-keywords has been moved back to cc-awk.el.
2311 (cc-provide 'cc-fonts)
2313 ;; arch-tag: 2f65f405-735f-4da5-8d4b-b957844c5203
2314 ;;; cc-fonts.el ends here