]> code.delx.au - gnu-emacs/blob - lisp/progmodes/cc-fonts.el
Merge from trunk.
[gnu-emacs] / lisp / progmodes / cc-fonts.el
1 ;;; cc-fonts.el --- font lock support for CC Mode
2
3 ;; Copyright (C) 2002-2011 Free Software Foundation, Inc.
4
5 ;; Authors: 2003- Alan Mackenzie
6 ;; 2002- Martin Stjernholm
7 ;; Maintainer: bug-cc-mode@gnu.org
8 ;; Created: 07-Jan-2002
9 ;; Keywords: c languages
10 ;; Package: cc-mode
11
12 ;; This file is part of GNU Emacs.
13
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.
18
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.
23
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/>.
26
27 ;;; Commentary:
28
29 ;; Some comments on the use of faces:
30 ;;
31 ;; o `c-label-face-name' is either `font-lock-constant-face' (in
32 ;; Emacs), or `font-lock-reference-face'.
33 ;;
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'.
37 ;;
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.
41 ;;
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).
47 ;;
48 ;; TBD: We should probably provide real faces for the above uses and
49 ;; instead initialize them from the standard faces.
50
51 ;;; Code:
52
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):
57 ;;
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.
66
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)))
74
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 ..).
82
83 ;; Avoid repeated loading through the eval-after-load directive in
84 ;; cc-mode.el.
85 (provide 'cc-fonts)
86
87 (cc-external-require 'font-lock)
88
89 (cc-bytecomp-defvar parse-sexp-lookup-properties) ; Emacs only.
90
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)
102
103 \f
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.
107
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)
116 (t
117 'font-lock-reference-face)))
118
119 (cc-bytecomp-defvar font-lock-constant-face)
120
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)
133 (t
134 'font-lock-reference-face)))
135
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))
142
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)))
152
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)
162 (t
163 'font-lock-comment-face)))
164
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))
172
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))
177
178 (cc-bytecomp-defun face-inverse-video-p) ; Only in Emacs.
179 (cc-bytecomp-defun face-property-instance) ; Only in XEmacs.
180
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)))))
196
197 (defvar c-annotation-face (make-face 'c-annotation-face)
198 "Face used to highlight annotations in java-mode and other modes that may wish to use it.")
199 (set-face-foreground 'c-annotation-face "blue")
200
201 (eval-and-compile
202 ;; We need the following functions during compilation since they're
203 ;; called when the `c-lang-defconst' initializers are evaluated.
204 ;; Define them at runtime too for the sake of derived modes.
205
206 (defmacro c-put-font-lock-face (from to face)
207 ;; Put a face on a region (overriding any existing face) in the way
208 ;; font-lock would do it. In XEmacs that means putting an
209 ;; additional font-lock property, or else the font-lock package
210 ;; won't recognize it as fontified and might override it
211 ;; incorrectly.
212 ;;
213 ;; This function does a hidden buffer change.
214 (if (fboundp 'font-lock-set-face)
215 ;; Note: This function has no docstring in XEmacs so it might be
216 ;; considered internal.
217 `(font-lock-set-face ,from ,to ,face)
218 `(put-text-property ,from ,to 'face ,face)))
219
220 (defmacro c-remove-font-lock-face (from to)
221 ;; This is the inverse of `c-put-font-lock-face'.
222 ;;
223 ;; This function does a hidden buffer change.
224 (if (fboundp 'font-lock-remove-face)
225 `(font-lock-remove-face ,from ,to)
226 `(remove-text-properties ,from ,to '(face nil))))
227
228 (defmacro c-put-font-lock-string-face (from to)
229 ;; Put `font-lock-string-face' on a string. The surrounding
230 ;; quotes are included in Emacs but not in XEmacs. The passed
231 ;; region should include them.
232 ;;
233 ;; This function does a hidden buffer change.
234 (if (featurep 'xemacs)
235 `(c-put-font-lock-face (1+ ,from) (1- ,to) 'font-lock-string-face)
236 `(c-put-font-lock-face ,from ,to 'font-lock-string-face)))
237
238 (defmacro c-fontify-types-and-refs (varlist &rest body)
239 ;; Like `let', but additionally activates `c-record-type-identifiers'
240 ;; and `c-record-ref-identifiers', and fontifies the recorded ranges
241 ;; accordingly on exit.
242 ;;
243 ;; This function does hidden buffer changes.
244 `(let ((c-record-type-identifiers t)
245 c-record-ref-identifiers
246 ,@varlist)
247 (prog1 (progn ,@body)
248 (c-fontify-recorded-types-and-refs))))
249 (put 'c-fontify-types-and-refs 'lisp-indent-function 1)
250
251 (defun c-skip-comments-and-strings (limit)
252 ;; If the point is within a region fontified as a comment or
253 ;; string literal skip to the end of it or to LIMIT, whichever
254 ;; comes first, and return t. Otherwise return nil. The match
255 ;; data is not clobbered.
256 ;;
257 ;; This function might do hidden buffer changes.
258 (when (c-got-face-at (point) c-literal-faces)
259 (while (progn
260 (goto-char (next-single-property-change
261 (point) 'face nil limit))
262 (and (< (point) limit)
263 (c-got-face-at (point) c-literal-faces))))
264 t))
265
266 (defun c-make-syntactic-matcher (regexp)
267 ;; Returns a byte compiled function suitable for use in place of a
268 ;; regexp string in a `font-lock-keywords' matcher, except that
269 ;; only matches outside comments and string literals count.
270 ;;
271 ;; This function does not do any hidden buffer changes, but the
272 ;; generated functions will. (They are however used in places
273 ;; covered by the font-lock context.)
274 (byte-compile
275 `(lambda (limit)
276 (let (res)
277 (while (and (setq res (re-search-forward ,regexp limit t))
278 (progn
279 (goto-char (match-beginning 0))
280 (or (c-skip-comments-and-strings limit)
281 (progn
282 (goto-char (match-end 0))
283 nil)))))
284 res))))
285
286 (defun c-make-font-lock-search-function (regexp &rest highlights)
287 ;; This function makes a byte compiled function that works much like
288 ;; a matcher element in `font-lock-keywords'. It cuts out a little
289 ;; bit of the overhead compared to a real matcher. The main reason
290 ;; is however to pass the real search limit to the anchored
291 ;; matcher(s), since most (if not all) font-lock implementations
292 ;; arbitrarily limit anchored matchers to the same line, and also
293 ;; to insulate against various other irritating differences between
294 ;; the different (X)Emacs font-lock packages.
295 ;;
296 ;; REGEXP is the matcher, which must be a regexp. Only matches
297 ;; where the beginning is outside any comment or string literal are
298 ;; significant.
299 ;;
300 ;; HIGHLIGHTS is a list of highlight specs, just like in
301 ;; `font-lock-keywords', with these limitations: The face is always
302 ;; overridden (no big disadvantage, since hits in comments etc are
303 ;; filtered anyway), there is no "laxmatch", and an anchored matcher
304 ;; is always a form which must do all the fontification directly.
305 ;; `limit' is a variable bound to the real limit in the context of
306 ;; the anchored matcher forms.
307 ;;
308 ;; This function does not do any hidden buffer changes, but the
309 ;; generated functions will. (They are however used in places
310 ;; covered by the font-lock context.)
311
312 ;; Note: Replace `byte-compile' with `eval' to debug the generated
313 ;; lambda more easily.
314 (byte-compile
315 `(lambda (limit)
316 (let (;; The font-lock package in Emacs is known to clobber
317 ;; `parse-sexp-lookup-properties' (when it exists).
318 (parse-sexp-lookup-properties
319 (cc-eval-when-compile
320 (boundp 'parse-sexp-lookup-properties))))
321 (while (re-search-forward ,regexp limit t)
322 (unless (progn
323 (goto-char (match-beginning 0))
324 (c-skip-comments-and-strings limit))
325 (goto-char (match-end 0))
326 ,@(mapcar
327 (lambda (highlight)
328 (if (integerp (car highlight))
329 (progn
330 (unless (eq (nth 2 highlight) t)
331 (error
332 "The override flag must currently be t in %s"
333 highlight))
334 (when (nth 3 highlight)
335 (error
336 "The laxmatch flag may currently not be set in %s"
337 highlight))
338 `(save-match-data
339 (c-put-font-lock-face
340 (match-beginning ,(car highlight))
341 (match-end ,(car highlight))
342 ,(elt highlight 1))))
343 (when (nth 3 highlight)
344 (error "Match highlights currently not supported in %s"
345 highlight))
346 `(progn
347 ,(nth 1 highlight)
348 (save-match-data ,(car highlight))
349 ,(nth 2 highlight))))
350 highlights))))
351 nil)))
352
353 ; (eval-after-load "edebug" ; 2006-07-09: def-edebug-spec is now in subr.el.
354 ; '(progn
355 (def-edebug-spec c-fontify-types-and-refs let*)
356 (def-edebug-spec c-make-syntactic-matcher t)
357 ;; If there are literal quoted or backquoted highlight specs in
358 ;; the call to `c-make-font-lock-search-function' then let's
359 ;; instrument the forms in them.
360 (def-edebug-spec c-make-font-lock-search-function
361 (form &rest &or ("quote" (&rest form)) ("`" (&rest form)) form)));))
362
363 (defun c-fontify-recorded-types-and-refs ()
364 ;; Convert the ranges recorded on `c-record-type-identifiers' and
365 ;; `c-record-ref-identifiers' to fontification.
366 ;;
367 ;; This function does hidden buffer changes.
368 (let (elem)
369 (while (consp c-record-type-identifiers)
370 (setq elem (car c-record-type-identifiers)
371 c-record-type-identifiers (cdr c-record-type-identifiers))
372 (c-put-font-lock-face (car elem) (cdr elem)
373 'font-lock-type-face))
374 (while c-record-ref-identifiers
375 (setq elem (car c-record-ref-identifiers)
376 c-record-ref-identifiers (cdr c-record-ref-identifiers))
377 ;; Note that the reference face is a variable that is
378 ;; dereferenced, since it's an alias in Emacs.
379 (c-put-font-lock-face (car elem) (cdr elem)
380 c-reference-face-name))))
381
382 (c-lang-defconst c-cpp-matchers
383 "Font lock matchers for preprocessor directives and purely lexical
384 stuff. Used on level 1 and higher."
385
386 ;; Note: `c-font-lock-declarations' assumes that no matcher here
387 ;; sets `font-lock-type-face' in languages where
388 ;; `c-recognize-<>-arglists' is set.
389
390 t `(,@(when (c-lang-const c-opt-cpp-prefix)
391 (let* ((noncontinued-line-end "\\(\\=\\|\\(\\=\\|[^\\]\\)[\n\r]\\)")
392 (ncle-depth (regexp-opt-depth noncontinued-line-end))
393 (sws-depth (c-lang-const c-syntactic-ws-depth))
394 (nsws-depth (c-lang-const c-nonempty-syntactic-ws-depth)))
395
396 `(;; The stuff after #error and #warning is a message, so
397 ;; fontify it as a string.
398 ,@(when (c-lang-const c-cpp-message-directives)
399 (let* ((re (c-make-keywords-re 'appendable ; nil
400 (c-lang-const c-cpp-message-directives)))
401 (re-depth (regexp-opt-depth re)))
402 `((,(concat noncontinued-line-end
403 (c-lang-const c-opt-cpp-prefix)
404 re
405 "\\s +\\(.*\\)$")
406 ,(+ ncle-depth re-depth 1) font-lock-string-face t))))
407
408 ;; Fontify filenames in #include <...> as strings.
409 ,@(when (c-lang-const c-cpp-include-directives)
410 (let* ((re (c-make-keywords-re nil
411 (c-lang-const c-cpp-include-directives)))
412 (re-depth (regexp-opt-depth re)))
413 `((,(concat noncontinued-line-end
414 (c-lang-const c-opt-cpp-prefix)
415 re
416 (c-lang-const c-syntactic-ws)
417 "\\(<[^>\n\r]*>?\\)")
418 (,(+ ncle-depth re-depth sws-depth 1)
419 font-lock-string-face)
420
421 ;; Use an anchored matcher to put paren syntax
422 ;; on the brackets.
423 (,(byte-compile
424 `(lambda (limit)
425 (let ((beg (match-beginning
426 ,(+ ncle-depth re-depth sws-depth 1)))
427 (end (1- (match-end ,(+ ncle-depth re-depth
428 sws-depth 1)))))
429 (if (eq (char-after end) ?>)
430 (progn
431 (c-mark-<-as-paren beg)
432 (c-mark->-as-paren end))
433 ;; (c-clear-char-property beg 'syntax-table)
434 (c-clear-char-property beg 'category)))
435 nil)))))))
436
437 ;; #define.
438 ,@(when (c-lang-const c-opt-cpp-macro-define)
439 `((,(c-make-font-lock-search-function
440 (concat
441 noncontinued-line-end
442 (c-lang-const c-opt-cpp-prefix)
443 (c-lang-const c-opt-cpp-macro-define)
444 (c-lang-const c-nonempty-syntactic-ws)
445 "\\(" (c-lang-const ; 1 + ncle + nsws
446 c-symbol-key) "\\)"
447 (concat "\\(" ; 2 + ncle + nsws + c-sym-key
448 ;; Macro with arguments - a "function".
449 "\\(\(\\)" ; 3 + ncle + nsws + c-sym-key
450 "\\|"
451 ;; Macro without arguments - a "variable".
452 "\\([^\(]\\|$\\)"
453 "\\)"))
454 `((if (match-beginning
455 ,(+ 3 ncle-depth nsws-depth
456 (c-lang-const c-symbol-key-depth)))
457
458 ;; "Function". Fontify the name and the arguments.
459 (save-restriction
460 (c-put-font-lock-face
461 (match-beginning ,(+ 1 ncle-depth nsws-depth))
462 (match-end ,(+ 1 ncle-depth nsws-depth))
463 'font-lock-function-name-face)
464 (goto-char
465 (match-end
466 ,(+ 3 ncle-depth nsws-depth
467 (c-lang-const c-symbol-key-depth))))
468
469 (narrow-to-region (point-min) limit)
470 (while (and
471 (progn
472 (c-forward-syntactic-ws)
473 (looking-at c-symbol-key))
474 (progn
475 (c-put-font-lock-face
476 (match-beginning 0) (match-end 0)
477 'font-lock-variable-name-face)
478 (goto-char (match-end 0))
479 (c-forward-syntactic-ws)
480 (eq (char-after) ?,)))
481 (forward-char)))
482
483 ;; "Variable".
484 (c-put-font-lock-face
485 (match-beginning ,(+ 1 ncle-depth nsws-depth))
486 (match-end ,(+ 1 ncle-depth nsws-depth))
487 'font-lock-variable-name-face)))))))
488
489 ;; Fontify cpp function names in preprocessor
490 ;; expressions in #if and #elif.
491 ,@(when (and (c-lang-const c-cpp-expr-directives)
492 (c-lang-const c-cpp-expr-functions))
493 (let ((ced-re (c-make-keywords-re t
494 (c-lang-const c-cpp-expr-directives)))
495 (cef-re (c-make-keywords-re t
496 (c-lang-const c-cpp-expr-functions))))
497 `((,(c-make-font-lock-search-function
498 (concat noncontinued-line-end
499 (c-lang-const c-opt-cpp-prefix)
500 ced-re ; 1 + ncle-depth
501 ;; Match the whole logical line to look
502 ;; for the functions in.
503 "\\(\\\\\\(.\\|[\n\r]\\)\\|[^\n\r]\\)*")
504 `((let ((limit (match-end 0)))
505 (while (re-search-forward ,cef-re limit 'move)
506 (c-put-font-lock-face (match-beginning 1)
507 (match-end 1)
508 c-preprocessor-face-name)))
509 (goto-char (match-end ,(1+ ncle-depth)))))))))
510
511 ;; Fontify the directive names.
512 (,(c-make-font-lock-search-function
513 (concat noncontinued-line-end
514 "\\("
515 (c-lang-const c-opt-cpp-prefix)
516 "[" (c-lang-const c-symbol-chars) "]+"
517 "\\)")
518 `(,(1+ ncle-depth) c-preprocessor-face-name t)))
519
520 (eval . (list ,(c-make-syntactic-matcher
521 (concat noncontinued-line-end
522 (c-lang-const c-opt-cpp-prefix)
523 "if\\(n\\)def\\>"))
524 ,(+ ncle-depth 1)
525 c-negation-char-face-name
526 'append))
527 )))
528
529 ,@(when (c-major-mode-is 'pike-mode)
530 ;; Recognize hashbangs in Pike.
531 `((eval . (list "\\`#![^\n\r]*"
532 0 c-preprocessor-face-name))))
533
534 ;; Make hard spaces visible through an inverted `font-lock-warning-face'.
535 (eval . (list
536 "\240"
537 0 (progn
538 (unless (c-face-name-p 'c-nonbreakable-space-face)
539 (c-make-inverse-face 'font-lock-warning-face
540 'c-nonbreakable-space-face))
541 ''c-nonbreakable-space-face)))
542 ))
543
544 (defun c-font-lock-invalid-string ()
545 ;; Assuming the point is after the opening character of a string,
546 ;; fontify that char with `font-lock-warning-face' if the string
547 ;; decidedly isn't terminated properly.
548 ;;
549 ;; This function does hidden buffer changes.
550 (let ((start (1- (point))))
551 (save-excursion
552 (and (eq (elt (parse-partial-sexp start (c-point 'eol)) 8) start)
553 (if (integerp c-multiline-string-start-char)
554 ;; There's no multiline string start char before the
555 ;; string, so newlines aren't allowed.
556 (not (eq (char-before start) c-multiline-string-start-char))
557 ;; Multiline strings are allowed anywhere if
558 ;; c-multiline-string-start-char is t.
559 (not c-multiline-string-start-char))
560 (if c-string-escaped-newlines
561 ;; There's no \ before the newline.
562 (not (eq (char-before (point)) ?\\))
563 ;; Escaped newlines aren't supported.
564 t)
565 (c-put-font-lock-face start (1+ start) 'font-lock-warning-face)))))
566
567 (c-lang-defconst c-basic-matchers-before
568 "Font lock matchers for basic keywords, labels, references and various
569 other easily recognizable things that should be fontified before generic
570 casts and declarations are fontified. Used on level 2 and higher."
571
572 ;; Note: `c-font-lock-declarations' assumes that no matcher here
573 ;; sets `font-lock-type-face' in languages where
574 ;; `c-recognize-<>-arglists' is set.
575
576 t `(;; Put a warning face on the opener of unclosed strings that
577 ;; can't span lines. Later font
578 ;; lock packages have a `font-lock-syntactic-face-function' for
579 ;; this, but it doesn't give the control we want since any
580 ;; fontification done inside the function will be
581 ;; unconditionally overridden.
582 ,(c-make-font-lock-search-function
583 ;; Match a char before the string starter to make
584 ;; `c-skip-comments-and-strings' work correctly.
585 (concat ".\\(" c-string-limit-regexp "\\)")
586 '((c-font-lock-invalid-string)))
587
588 ;; Fontify keyword constants.
589 ,@(when (c-lang-const c-constant-kwds)
590 (let ((re (c-make-keywords-re nil (c-lang-const c-constant-kwds))))
591 (if (c-major-mode-is 'pike-mode)
592 ;; No symbol is a keyword after "->" in Pike.
593 `((eval . (list ,(concat "\\(\\=.?\\|[^>]\\|[^-]>\\)"
594 "\\<\\(" re "\\)\\>")
595 2 c-constant-face-name)))
596 `((eval . (list ,(concat "\\<\\(" re "\\)\\>")
597 1 c-constant-face-name))))))
598
599 ;; Fontify all keywords except the primitive types.
600 ,(if (c-major-mode-is 'pike-mode)
601 ;; No symbol is a keyword after "->" in Pike.
602 `(,(concat "\\(\\=.?\\|[^>]\\|[^-]>\\)"
603 "\\<" (c-lang-const c-regular-keywords-regexp))
604 2 font-lock-keyword-face)
605 `(,(concat "\\<" (c-lang-const c-regular-keywords-regexp))
606 1 font-lock-keyword-face))
607
608 ;; Fontify leading identifiers in fully qualified names like
609 ;; "foo::bar" in languages that supports such things.
610 ,@(when (c-lang-const c-opt-identifier-concat-key)
611 (if (c-major-mode-is 'java-mode)
612 ;; Java needs special treatment since "." is used both to
613 ;; qualify names and in normal indexing. Here we look for
614 ;; capital characters at the beginning of an identifier to
615 ;; recognize the class. "*" is also recognized to cover
616 ;; wildcard import declarations. All preceding dot separated
617 ;; identifiers are taken as package names and therefore
618 ;; fontified as references.
619 `(,(c-make-font-lock-search-function
620 ;; Search for class identifiers preceded by ".". The
621 ;; anchored matcher takes it from there.
622 (concat (c-lang-const c-opt-identifier-concat-key)
623 (c-lang-const c-simple-ws) "*"
624 (concat "\\("
625 "[" c-upper "]"
626 "[" (c-lang-const c-symbol-chars) "]*"
627 "\\|"
628 "\\*"
629 "\\)"))
630 `((let (id-end)
631 (goto-char (1+ (match-beginning 0)))
632 (while (and (eq (char-before) ?.)
633 (progn
634 (backward-char)
635 (c-backward-syntactic-ws)
636 (setq id-end (point))
637 (< (skip-chars-backward
638 ,(c-lang-const c-symbol-chars)) 0))
639 (not (get-text-property (point) 'face)))
640 (c-put-font-lock-face (point) id-end
641 c-reference-face-name)
642 (c-backward-syntactic-ws)))
643 nil
644 (goto-char (match-end 0)))))
645
646 `((,(byte-compile
647 ;; Must use a function here since we match longer than
648 ;; we want to move before doing a new search. This is
649 ;; not necessary for XEmacs since it restarts the
650 ;; search from the end of the first highlighted
651 ;; submatch (something that causes problems in other
652 ;; places).
653 `(lambda (limit)
654 (while (re-search-forward
655 ,(concat "\\(\\<" ; 1
656 "\\(" (c-lang-const c-symbol-key) "\\)" ; 2
657 (c-lang-const c-simple-ws) "*"
658 (c-lang-const c-opt-identifier-concat-key)
659 (c-lang-const c-simple-ws) "*"
660 "\\)"
661 "\\("
662 (c-lang-const c-opt-after-id-concat-key)
663 "\\)")
664 limit t)
665 (unless (progn
666 (goto-char (match-beginning 0))
667 (c-skip-comments-and-strings limit))
668 (or (get-text-property (match-beginning 2) 'face)
669 (c-put-font-lock-face (match-beginning 2)
670 (match-end 2)
671 c-reference-face-name))
672 (goto-char (match-end 1))))))))))
673
674 ;; Fontify the special declarations in Objective-C.
675 ,@(when (c-major-mode-is 'objc-mode)
676 `(;; Fontify class names in the beginning of message expressions.
677 ,(c-make-font-lock-search-function
678 "\\["
679 '((c-fontify-types-and-refs ()
680 (c-forward-syntactic-ws limit)
681 (let ((start (point)))
682 ;; In this case we accept both primitive and known types.
683 (when (eq (c-forward-type) 'known)
684 (goto-char start)
685 (let ((c-promote-possible-types t))
686 (c-forward-type))))
687 (if (> (point) limit) (goto-char limit)))))
688
689 ;; The @interface/@implementation/@protocol directives.
690 ,(c-make-font-lock-search-function
691 (concat "\\<"
692 (regexp-opt
693 '("@interface" "@implementation" "@protocol")
694 t)
695 "\\>")
696 '((c-fontify-types-and-refs
697 (;; The font-lock package in Emacs is known to clobber
698 ;; `parse-sexp-lookup-properties' (when it exists).
699 (parse-sexp-lookup-properties
700 (cc-eval-when-compile
701 (boundp 'parse-sexp-lookup-properties))))
702 (c-forward-objc-directive)
703 nil)
704 (goto-char (match-beginning 0))))))
705
706 (eval . (list "\\(!\\)[^=]" 1 c-negation-char-face-name))
707 ))
708
709 (defun c-font-lock-complex-decl-prepare (limit)
710 ;; This function will be called from font-lock for a region bounded by POINT
711 ;; and LIMIT, as though it were to identify a keyword for
712 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
713 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
714 ;; Fontification".
715 ;;
716 ;; Called before any of the matchers in `c-complex-decl-matchers'.
717 ;;
718 ;; This function does hidden buffer changes.
719
720 ;;(message "c-font-lock-complex-decl-prepare %s %s" (point) limit)
721
722 ;; Clear the list of found types if we start from the start of the
723 ;; buffer, to make it easier to get rid of misspelled types and
724 ;; variables that have gotten recognized as types in malformed code.
725 (when (bobp)
726 (c-clear-found-types))
727
728 ;; Clear the c-type char properties which mark the region, to recalculate
729 ;; them properly. The most interesting properties are those put on the
730 ;; closest token before the region.
731 (save-excursion
732 (let ((pos (point)))
733 (c-backward-syntactic-ws)
734 (c-clear-char-properties
735 (if (and (not (bobp))
736 (memq (c-get-char-property (1- (point)) 'c-type)
737 '(c-decl-arg-start
738 c-decl-end
739 c-decl-id-start
740 c-decl-type-start)))
741 (1- (point))
742 pos)
743 limit 'c-type)))
744
745 ;; Update `c-state-cache' to the beginning of the region. This will
746 ;; make `c-beginning-of-syntax' go faster when it's used later on,
747 ;; and it's near the point most of the time.
748 (c-parse-state)
749
750 ;; Check if the fontified region starts inside a declarator list so
751 ;; that `c-font-lock-declarators' should be called at the start.
752 ;; The declared identifiers are font-locked correctly as types, if
753 ;; that is what they are.
754 (let ((prop (save-excursion
755 (c-backward-syntactic-ws)
756 (unless (bobp)
757 (c-get-char-property (1- (point)) 'c-type)))))
758 (when (memq prop '(c-decl-id-start c-decl-type-start))
759 (c-forward-syntactic-ws limit)
760 (c-font-lock-declarators limit t (eq prop 'c-decl-type-start))))
761
762 nil)
763
764 (defun c-font-lock-<>-arglists (limit)
765 ;; This function will be called from font-lock for a region bounded by POINT
766 ;; and LIMIT, as though it were to identify a keyword for
767 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
768 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
769 ;; Fontification".
770 ;;
771 ;; Fontify types and references in names containing angle bracket
772 ;; arglists from the point to LIMIT. Note that
773 ;; `c-font-lock-declarations' already has handled many of them.
774 ;;
775 ;; This function might do hidden buffer changes.
776
777 (let (;; The font-lock package in Emacs is known to clobber
778 ;; `parse-sexp-lookup-properties' (when it exists).
779 (parse-sexp-lookup-properties
780 (cc-eval-when-compile
781 (boundp 'parse-sexp-lookup-properties)))
782 (c-parse-and-markup-<>-arglists t)
783 c-restricted-<>-arglists
784 id-start id-end id-face pos kwd-sym)
785
786 (while (and (< (point) limit)
787 (re-search-forward c-opt-<>-arglist-start limit t))
788
789 (setq id-start (match-beginning 1)
790 id-end (match-end 1)
791 pos (point))
792
793 (goto-char id-start)
794 (unless (c-skip-comments-and-strings limit)
795 (setq kwd-sym nil
796 c-restricted-<>-arglists nil
797 id-face (get-text-property id-start 'face))
798
799 (if (cond
800 ((eq id-face 'font-lock-type-face)
801 ;; The identifier got the type face so it has already been
802 ;; handled in `c-font-lock-declarations'.
803 nil)
804
805 ((eq id-face 'font-lock-keyword-face)
806 (when (looking-at c-opt-<>-sexp-key)
807 ;; There's a special keyword before the "<" that tells
808 ;; that it's an angle bracket arglist.
809 (setq kwd-sym (c-keyword-sym (match-string 1)))))
810
811 (t
812 ;; There's a normal identifier before the "<". If we're not in
813 ;; a declaration context then we set `c-restricted-<>-arglists'
814 ;; to avoid recognizing templates in function calls like "foo (a
815 ;; < b, c > d)".
816 (c-backward-syntactic-ws)
817 (when (and (memq (char-before) '(?\( ?,))
818 (not (eq (get-text-property (1- (point)) 'c-type)
819 'c-decl-arg-start)))
820 (setq c-restricted-<>-arglists t))
821 t))
822
823 (progn
824 (goto-char (1- pos))
825 ;; Check for comment/string both at the identifier and
826 ;; at the "<".
827 (unless (c-skip-comments-and-strings limit)
828
829 (c-fontify-types-and-refs ()
830 (when (c-forward-<>-arglist (c-keyword-member
831 kwd-sym 'c-<>-type-kwds))
832 (when (and c-opt-identifier-concat-key
833 (not (get-text-property id-start 'face)))
834 (c-forward-syntactic-ws)
835 (if (looking-at c-opt-identifier-concat-key)
836 (c-put-font-lock-face id-start id-end
837 c-reference-face-name)
838 (c-put-font-lock-face id-start id-end
839 'font-lock-type-face)))))
840
841 (goto-char pos)))
842 (goto-char pos)))))
843 nil)
844
845 (defun c-font-lock-declarators (limit list types)
846 ;; Assuming the point is at the start of a declarator in a declaration,
847 ;; fontify the identifier it declares. (If TYPES is set, it does this via
848 ;; the macro `c-fontify-types-and-refs'.)
849 ;;
850 ;; If LIST is non-nil, also fontify the ids in any following declarators in
851 ;; a comma separated list (e.g. "foo" and "*bar" in "int foo = 17, *bar;");
852 ;; additionally, mark the commas with c-type property 'c-decl-id-start or
853 ;; 'c-decl-type-start (according to TYPES). Stop at LIMIT.
854 ;;
855 ;; If TYPES is non-nil, fontify all identifiers as types.
856 ;;
857 ;; Nil is always returned. The function leaves point at the delimiter after
858 ;; the last declarator it processes.
859 ;;
860 ;; This function might do hidden buffer changes.
861
862 ;;(message "c-font-lock-declarators from %s to %s" (point) limit)
863 (c-fontify-types-and-refs
864 ((pos (point)) next-pos id-start id-end
865 paren-depth
866 id-face got-init
867 c-last-identifier-range
868 (separator-prop (if types 'c-decl-type-start 'c-decl-id-start)))
869
870 ;; The following `while' fontifies a single declarator id each time round.
871 ;; It loops only when LIST is non-nil.
872 (while
873 ;; Inside the following "condition form", we move forward over the
874 ;; declarator's identifier up as far as any opening bracket (for array
875 ;; size) or paren (for parameters of function-type) or brace (for
876 ;; array/struct initialisation) or "=" or terminating delimiter
877 ;; (e.g. "," or ";" or "}").
878 (and
879 pos
880 (< (point) limit)
881
882 ;; The following form moves forward over the declarator's
883 ;; identifier (and what precedes it), returning t. If there
884 ;; wasn't one, it returns nil, terminating the `while'.
885 (let (got-identifier)
886 (setq paren-depth 0)
887 ;; Skip over type decl prefix operators, one for each iteration
888 ;; of the while. These are, e.g. "*" in "int *foo" or "(" and
889 ;; "*" in "int (*foo) (void)" (Note similar code in
890 ;; `c-forward-decl-or-cast-1'.)
891 (while (and (looking-at c-type-decl-prefix-key)
892 (if (and (c-major-mode-is 'c++-mode)
893 (match-beginning 3))
894 ;; If the third submatch matches in C++ then
895 ;; we're looking at an identifier that's a
896 ;; prefix only if it specifies a member pointer.
897 (progn
898 (setq id-start (point))
899 (c-forward-name)
900 (if (looking-at "\\(::\\)")
901 ;; We only check for a trailing "::" and
902 ;; let the "*" that should follow be
903 ;; matched in the next round.
904 t
905 ;; It turned out to be the real identifier,
906 ;; so flag that and stop.
907 (setq got-identifier t)
908 nil))
909 t))
910 (if (eq (char-after) ?\()
911 (progn
912 (setq paren-depth (1+ paren-depth))
913 (forward-char))
914 (goto-char (match-end 1)))
915 (c-forward-syntactic-ws))
916
917 ;; If we haven't passed the identifier already, do it now.
918 (unless got-identifier
919 (setq id-start (point))
920 (c-forward-name))
921 (setq id-end (point))
922
923 (/= id-end pos))
924
925 ;; Skip out of the parens surrounding the identifier. If closing
926 ;; parens are missing, this form returns nil.
927 (or (= paren-depth 0)
928 (c-safe (goto-char (scan-lists (point) 1 paren-depth))))
929
930 (<= (point) limit)
931
932 ;; Skip over any trailing bit, such as "__attribute__".
933 (progn
934 (when (looking-at c-decl-hangon-key)
935 (c-forward-keyword-clause 1))
936 (<= (point) limit))
937
938 ;; Search syntactically to the end of the declarator (";",
939 ;; ",", a closen paren, eob etc) or to the beginning of an
940 ;; initializer or function prototype ("=" or "\\s\(").
941 ;; Note that the open paren will match array specs in
942 ;; square brackets, and we treat them as initializers too.
943 (c-syntactic-re-search-forward
944 "[;,]\\|\\s)\\|\\'\\|\\(=\\|\\s(\\)" limit t t))
945
946 (setq next-pos (match-beginning 0)
947 id-face (if (and (eq (char-after next-pos) ?\()
948 (let (c-last-identifier-range)
949 (save-excursion
950 (goto-char next-pos)
951 (c-at-toplevel-p))))
952 'font-lock-function-name-face
953 'font-lock-variable-name-face)
954 got-init (and (match-beginning 1)
955 (char-after (match-beginning 1))))
956
957 (if types
958 ;; Register and fontify the identifer as a type.
959 (let ((c-promote-possible-types t))
960 (goto-char id-start)
961 (c-forward-type))
962 ;; Fontify the last symbol in the identifier if it isn't fontified
963 ;; already. The check is necessary only in certain cases where this
964 ;; function is used "sloppily", e.g. in `c-simple-decl-matchers'.
965 (when (and c-last-identifier-range
966 (not (get-text-property (car c-last-identifier-range)
967 'face)))
968 (c-put-font-lock-face (car c-last-identifier-range)
969 (cdr c-last-identifier-range)
970 id-face)))
971
972 (goto-char next-pos)
973 (setq pos nil) ; So as to terminate the enclosing `while' form.
974 (when list
975 ;; Jump past any initializer or function prototype to see if
976 ;; there's a ',' to continue at.
977
978 (cond ((eq id-face 'font-lock-function-name-face)
979 ;; Skip a parenthesized initializer (C++) or a function
980 ;; prototype.
981 (if (c-safe (c-forward-sexp 1) t) ; over the parameter list.
982 (c-forward-syntactic-ws limit)
983 (goto-char limit))) ; unbalanced parens
984
985 (got-init ; "=" sign OR opening "(", "[", or "{"
986 ;; Skip an initializer expression. If we're at a '='
987 ;; then accept a brace list directly after it to cope
988 ;; with array initializers. Otherwise stop at braces
989 ;; to avoid going past full function and class blocks.
990 (and (if (and (eq got-init ?=)
991 (= (c-forward-token-2 1 nil limit) 0)
992 (looking-at "{"))
993 (c-safe (c-forward-sexp) t) ; over { .... }
994 t)
995 ;; FIXME: Should look for c-decl-end markers here;
996 ;; we might go far into the following declarations
997 ;; in e.g. ObjC mode (see e.g. methods-4.m).
998 (c-syntactic-re-search-forward "[;,{]" limit 'move t)
999 (backward-char)))
1000
1001 (t (c-forward-syntactic-ws limit)))
1002
1003 ;; If a ',' is found we set pos to the next declarator and iterate.
1004 (when (and (< (point) limit) (looking-at ","))
1005 (c-put-char-property (point) 'c-type separator-prop)
1006 (forward-char)
1007 (c-forward-syntactic-ws limit)
1008 (setq pos (point)))))) ; acts to make the `while' form continue.
1009 nil)
1010
1011 (defconst c-font-lock-maybe-decl-faces
1012 ;; List of faces that might be put at the start of a type when
1013 ;; `c-font-lock-declarations' runs. This needs to be evaluated to
1014 ;; ensure that face name aliases in Emacs are resolved.
1015 (list nil
1016 font-lock-type-face
1017 c-reference-face-name
1018 font-lock-keyword-face))
1019
1020 (defun c-font-lock-declarations (limit)
1021 ;; Fontify all the declarations, casts and labels from the point to LIMIT.
1022 ;; Assumes that strings and comments have been fontified already.
1023 ;;
1024 ;; This function will be called from font-lock for a region bounded by POINT
1025 ;; and LIMIT, as though it were to identify a keyword for
1026 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
1027 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
1028 ;; Fontification".
1029 ;;
1030 ;; This function might do hidden buffer changes.
1031
1032 ;;(message "c-font-lock-declarations search from %s to %s" (point) limit)
1033
1034 (save-restriction
1035 (let (;; The position where `c-find-decl-spots' last stopped.
1036 start-pos
1037 ;; o - 'decl if we're in an arglist containing declarations
1038 ;; (but if `c-recognize-paren-inits' is set it might also be
1039 ;; an initializer arglist);
1040 ;; o - '<> if the arglist is of angle bracket type;
1041 ;; o - 'arglist if it's some other arglist;
1042 ;; o - nil, if not in an arglist at all. This includes the
1043 ;; parenthesised condition which follows "if", "while", etc.
1044 context
1045 ;; The position of the next token after the closing paren of
1046 ;; the last detected cast.
1047 last-cast-end
1048 ;; The result from `c-forward-decl-or-cast-1'.
1049 decl-or-cast
1050 ;; The maximum of the end positions of all the checked type
1051 ;; decl expressions in the successfully identified
1052 ;; declarations. The position might be either before or
1053 ;; after the syntactic whitespace following the last token
1054 ;; in the type decl expression.
1055 (max-type-decl-end 0)
1056 ;; Same as `max-type-decl-*', but used when we're before
1057 ;; `token-pos'.
1058 (max-type-decl-end-before-token 0)
1059 ;; Set according to the context to direct the heuristics for
1060 ;; recognizing C++ templates.
1061 c-restricted-<>-arglists
1062 ;; Turn on recording of identifier ranges in
1063 ;; `c-forward-decl-or-cast-1' and `c-forward-label' for
1064 ;; later fontification.
1065 (c-record-type-identifiers t)
1066 label-type
1067 c-record-ref-identifiers
1068 ;; Make `c-forward-type' calls mark up template arglists if
1069 ;; it finds any. That's necessary so that we later will
1070 ;; stop inside them to fontify types there.
1071 (c-parse-and-markup-<>-arglists t)
1072 ;; The font-lock package in Emacs is known to clobber
1073 ;; `parse-sexp-lookup-properties' (when it exists).
1074 (parse-sexp-lookup-properties
1075 (cc-eval-when-compile
1076 (boundp 'parse-sexp-lookup-properties))))
1077
1078 ;; Below we fontify a whole declaration even when it crosses the limit,
1079 ;; to avoid gaps when jit/lazy-lock fontifies the file a block at a
1080 ;; time. That is however annoying during editing, e.g. the following is
1081 ;; a common situation while the first line is being written:
1082 ;;
1083 ;; my_variable
1084 ;; some_other_variable = 0;
1085 ;;
1086 ;; font-lock will put the limit at the beginning of the second line
1087 ;; here, and if we go past it we'll fontify "my_variable" as a type and
1088 ;; "some_other_variable" as an identifier, and the latter will not
1089 ;; correct itself until the second line is changed. To avoid that we
1090 ;; narrow to the limit if the region to fontify is a single line.
1091 (if (<= limit (c-point 'bonl))
1092 (narrow-to-region
1093 (point-min)
1094 (save-excursion
1095 ;; Narrow after any operator chars following the limit though,
1096 ;; since those characters can be useful in recognizing a
1097 ;; declaration (in particular the '{' that opens a function body
1098 ;; after the header).
1099 (goto-char limit)
1100 (skip-chars-forward c-nonsymbol-chars)
1101 (point))))
1102
1103 (c-find-decl-spots
1104 limit
1105 c-decl-start-re
1106 c-font-lock-maybe-decl-faces
1107
1108 (lambda (match-pos inside-macro)
1109 (setq start-pos (point))
1110 (when
1111 ;; The result of the form below is true when we don't recognize a
1112 ;; declaration or cast.
1113 (if (and (eq (get-text-property (point) 'face)
1114 'font-lock-keyword-face)
1115 (looking-at c-not-decl-init-keywords))
1116 ;; Don't do anything more if we're looking at a keyword that
1117 ;; can't start a declaration.
1118 t
1119
1120 ;; Set `context' and `c-restricted-<>-arglists'. Look for
1121 ;; "<" for the sake of C++-style template arglists.
1122 ;; Ignore "(" when it's part of a control flow construct
1123 ;; (e.g. "for (").
1124 (let ((type (and (> match-pos (point-min))
1125 (c-get-char-property (1- match-pos) 'c-type))))
1126 (cond ((not (memq (char-before match-pos) '(?\( ?, ?\[ ?<)))
1127 (setq context nil
1128 c-restricted-<>-arglists nil))
1129 ;; A control flow expression
1130 ((and (eq (char-before match-pos) ?\()
1131 (save-excursion
1132 (goto-char match-pos)
1133 (backward-char)
1134 (c-backward-token-2)
1135 (looking-at c-block-stmt-2-key)))
1136 (setq context nil
1137 c-restricted-<>-arglists t))
1138 ;; Near BOB.
1139 ((<= match-pos (point-min))
1140 (setq context 'arglist
1141 c-restricted-<>-arglists t))
1142 ;; Got a cached hit in a declaration arglist.
1143 ((eq type 'c-decl-arg-start)
1144 (setq context 'decl
1145 c-restricted-<>-arglists nil))
1146 ;; Inside an angle bracket arglist.
1147 ((or (eq type 'c-<>-arg-sep)
1148 (eq (char-before match-pos) ?<))
1149 (setq context '<>
1150 c-restricted-<>-arglists nil))
1151 ;; Got a cached hit in some other type of arglist.
1152 (type
1153 (setq context 'arglist
1154 c-restricted-<>-arglists t))
1155 ((if inside-macro
1156 (< match-pos max-type-decl-end-before-token)
1157 (< match-pos max-type-decl-end))
1158 ;; The point is within the range of a previously
1159 ;; encountered type decl expression, so the arglist
1160 ;; is probably one that contains declarations.
1161 ;; However, if `c-recognize-paren-inits' is set it
1162 ;; might also be an initializer arglist.
1163 (setq context 'decl
1164 c-restricted-<>-arglists nil)
1165 ;; The result of this check is cached with a char
1166 ;; property on the match token, so that we can look
1167 ;; it up again when refontifying single lines in a
1168 ;; multiline declaration.
1169 (c-put-char-property (1- match-pos)
1170 'c-type 'c-decl-arg-start))
1171 (t (setq context 'arglist
1172 c-restricted-<>-arglists t))))
1173
1174 ;; Check we haven't missed a preceding "typedef".
1175 (when (not (looking-at c-typedef-key))
1176 (c-backward-syntactic-ws)
1177 (c-backward-token-2)
1178 (or (looking-at c-typedef-key)
1179 (goto-char start-pos)))
1180
1181 ;; Now analyze the construct.
1182 ;; In QT, "more" is an irritating keyword that expands to nothing.
1183 ;; We skip over it to prevent recognition of "more slots: <symbol>"
1184 ;; as a bitfield declaration.
1185 (when (and (c-major-mode-is 'c++-mode)
1186 (looking-at
1187 (concat "\\(more\\)\\([^" c-symbol-chars "]\\|$\\)")))
1188 (goto-char (match-end 1))
1189 (c-forward-syntactic-ws))
1190 (setq decl-or-cast (c-forward-decl-or-cast-1
1191 match-pos context last-cast-end))
1192
1193 (cond
1194 ((eq decl-or-cast 'cast)
1195 ;; Save the position after the previous cast so we can feed
1196 ;; it to `c-forward-decl-or-cast-1' in the next round. That
1197 ;; helps it discover cast chains like "(a) (b) c".
1198 (setq last-cast-end (point))
1199 (c-fontify-recorded-types-and-refs)
1200 nil)
1201
1202 (decl-or-cast
1203 ;; We've found a declaration.
1204
1205 ;; Set `max-type-decl-end' or `max-type-decl-end-before-token'
1206 ;; under the assumption that we're after the first type decl
1207 ;; expression in the declaration now. That's not really true;
1208 ;; we could also be after a parenthesized initializer
1209 ;; expression in C++, but this is only used as a last resort
1210 ;; to slant ambiguous expression/declarations, and overall
1211 ;; it's worth the risk to occasionally fontify an expression
1212 ;; as a declaration in an initializer expression compared to
1213 ;; getting ambiguous things in normal function prototypes
1214 ;; fontified as expressions.
1215 (if inside-macro
1216 (when (> (point) max-type-decl-end-before-token)
1217 (setq max-type-decl-end-before-token (point)))
1218 (when (> (point) max-type-decl-end)
1219 (setq max-type-decl-end (point))))
1220
1221 ;; Back up to the type to fontify the declarator(s).
1222 (goto-char (car decl-or-cast))
1223
1224 (let ((decl-list
1225 (if context
1226 ;; Should normally not fontify a list of
1227 ;; declarators inside an arglist, but the first
1228 ;; argument in the ';' separated list of a "for"
1229 ;; statement is an exception.
1230 (when (eq (char-before match-pos) ?\()
1231 (save-excursion
1232 (goto-char (1- match-pos))
1233 (c-backward-syntactic-ws)
1234 (and (c-simple-skip-symbol-backward)
1235 (looking-at c-paren-stmt-key))))
1236 t)))
1237
1238 ;; Fix the `c-decl-id-start' or `c-decl-type-start' property
1239 ;; before the first declarator if it's a list.
1240 ;; `c-font-lock-declarators' handles the rest.
1241 (when decl-list
1242 (save-excursion
1243 (c-backward-syntactic-ws)
1244 (unless (bobp)
1245 (c-put-char-property (1- (point)) 'c-type
1246 (if (cdr decl-or-cast)
1247 'c-decl-type-start
1248 'c-decl-id-start)))))
1249
1250 (c-font-lock-declarators
1251 (point-max) decl-list (cdr decl-or-cast)))
1252
1253 ;; A declaration has been successfully identified, so do all the
1254 ;; fontification of types and refs that've been recorded.
1255 (c-fontify-recorded-types-and-refs)
1256 nil)
1257
1258 (t
1259 ;; Are we at a declarator? Try to go back to the declaration
1260 ;; to check this. If we get there, check whether a "typedef"
1261 ;; is there, then fontify the declarators accordingly.
1262 (let ((decl-search-lim (max (- (point) 50000) (point-min)))
1263 paren-state bod-res encl-pos is-typedef
1264 c-recognize-knr-p) ; Strictly speaking, bogus, but it
1265 ; speeds up lisp.h tremendously.
1266 (save-excursion
1267 (setq bod-res (car (c-beginning-of-decl-1 decl-search-lim)))
1268 (if (and (eq bod-res 'same)
1269 (progn
1270 (c-backward-syntactic-ws)
1271 (eq (char-before) ?\})))
1272 (c-beginning-of-decl-1 decl-search-lim))
1273
1274 ;; We're now putatively at the declaration.
1275 (setq paren-state (c-parse-state))
1276 ;; At top level or inside a "{"?
1277 (if (or (not (setq encl-pos
1278 (c-most-enclosing-brace paren-state)))
1279 (eq (char-after encl-pos) ?\{))
1280 (progn
1281 (when (looking-at c-typedef-key) ; "typedef"
1282 (setq is-typedef t)
1283 (goto-char (match-end 0))
1284 (c-forward-syntactic-ws))
1285 ;; At a real declaration?
1286 (if (memq (c-forward-type t) '(t known found))
1287 (progn
1288 (c-font-lock-declarators limit t is-typedef)
1289 nil)
1290 ;; False alarm. Return t to go on to the next check.
1291 (goto-char start-pos)
1292 t))
1293 t))))))
1294
1295 ;; It was a false alarm. Check if we're in a label (or other
1296 ;; construct with `:' except bitfield) instead.
1297 (goto-char start-pos)
1298 (when (setq label-type (c-forward-label t match-pos nil))
1299 ;; Can't use `c-fontify-types-and-refs' here since we
1300 ;; use the label face at times.
1301 (cond ((eq label-type 'goto-target)
1302 (c-put-font-lock-face (caar c-record-ref-identifiers)
1303 (cdar c-record-ref-identifiers)
1304 c-label-face-name))
1305 ((eq label-type 'qt-1kwd-colon)
1306 (c-put-font-lock-face (caar c-record-ref-identifiers)
1307 (cdar c-record-ref-identifiers)
1308 'font-lock-keyword-face))
1309 ((eq label-type 'qt-2kwds-colon)
1310 (mapc
1311 (lambda (kwd)
1312 (c-put-font-lock-face (car kwd) (cdr kwd)
1313 'font-lock-keyword-face))
1314 c-record-ref-identifiers)))
1315 (setq c-record-ref-identifiers nil)
1316 ;; `c-forward-label' has probably added a `c-decl-end'
1317 ;; marker, so return t to `c-find-decl-spots' to signal
1318 ;; that.
1319 t))))
1320
1321 nil)))
1322
1323 (defun c-font-lock-enum-tail (limit)
1324 ;; Fontify an enum's identifiers when POINT is within the enum's brace
1325 ;; block.
1326 ;;
1327 ;; This function will be called from font-lock for a region bounded by POINT
1328 ;; and LIMIT, as though it were to identify a keyword for
1329 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
1330 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
1331 ;; Fontification".
1332 ;;
1333 ;; Note that this function won't attempt to fontify beyond the end of the
1334 ;; current enum block, if any.
1335 (let* ((paren-state (c-parse-state))
1336 (encl-pos (c-most-enclosing-brace paren-state))
1337 (start (point))
1338 )
1339 (when (and
1340 encl-pos
1341 (eq (char-after encl-pos) ?\{)
1342 (save-excursion
1343 (goto-char encl-pos)
1344 (c-backward-syntactic-ws)
1345 (c-simple-skip-symbol-backward)
1346 (or (looking-at c-brace-list-key) ; "enum"
1347 (progn (c-backward-syntactic-ws)
1348 (c-simple-skip-symbol-backward)
1349 (looking-at c-brace-list-key)))))
1350 (c-syntactic-skip-backward "^{," nil t)
1351 (c-put-char-property (1- (point)) 'c-type 'c-decl-id-start)
1352
1353 (c-forward-syntactic-ws)
1354 (c-font-lock-declarators limit t nil)))
1355 nil)
1356
1357 (defun c-font-lock-enclosing-decls (limit)
1358 ;; Fontify the declarators of (nested) declarations we're in the middle of.
1359 ;; This is mainly for when a jit-lock etc. chunk starts inside the brace
1360 ;; block of a struct/union/class, etc.
1361 ;;
1362 ;; This function will be called from font-lock for a region bounded by POINT
1363 ;; and LIMIT, as though it were to identify a keyword for
1364 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
1365 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
1366 ;; Fontification".
1367 (let* ((paren-state (c-parse-state))
1368 (start (point))
1369 decl-context bo-decl in-typedef type-type ps-elt)
1370
1371 ;; First, are we actually in a "local" declaration?
1372 (setq decl-context (c-beginning-of-decl-1)
1373 bo-decl (point)
1374 in-typedef (looking-at c-typedef-key))
1375 (if in-typedef (c-forward-token-2))
1376 (when (and (eq (car decl-context) 'same)
1377 (< bo-decl start))
1378 ;; Are we genuinely at a type?
1379 (setq type-type (c-forward-type t))
1380 (if (and type-type
1381 (or (not (eq type-type 'maybe))
1382 (looking-at c-symbol-key)))
1383 (c-font-lock-declarators limit t in-typedef)))
1384
1385 ;; Secondly, are we in any nested struct/union/class/etc. braces?
1386 (while paren-state
1387 (setq ps-elt (car paren-state)
1388 paren-state (cdr paren-state))
1389 (when (and (atom ps-elt)
1390 (eq (char-after ps-elt) ?\{))
1391 (goto-char ps-elt)
1392 (setq decl-context (c-beginning-of-decl-1)
1393 in-typedef (looking-at c-typedef-key))
1394 (if in-typedef (c-forward-token-2))
1395 (when (looking-at c-opt-block-decls-with-vars-key)
1396 (goto-char ps-elt)
1397 (when (c-safe (c-forward-sexp))
1398 (c-forward-syntactic-ws)
1399 (c-font-lock-declarators limit t in-typedef)))))))
1400
1401 (c-lang-defconst c-simple-decl-matchers
1402 "Simple font lock matchers for types and declarations. These are used
1403 on level 2 only and so aren't combined with `c-complex-decl-matchers'."
1404
1405 t `(;; Objective-C methods.
1406 ,@(when (c-major-mode-is 'objc-mode)
1407 `((,(c-lang-const c-opt-method-key)
1408 (,(byte-compile
1409 (lambda (limit)
1410 (let (;; The font-lock package in Emacs is known to clobber
1411 ;; `parse-sexp-lookup-properties' (when it exists).
1412 (parse-sexp-lookup-properties
1413 (cc-eval-when-compile
1414 (boundp 'parse-sexp-lookup-properties))))
1415 (save-restriction
1416 (narrow-to-region (point-min) limit)
1417 (c-font-lock-objc-method)))
1418 nil))
1419 (goto-char (match-end 1))))))
1420
1421 ;; Fontify all type names and the identifiers in the
1422 ;; declarations they might start. Use eval here since
1423 ;; `c-known-type-key' gets its value from
1424 ;; `*-font-lock-extra-types' on mode init.
1425 (eval . (list ,(c-make-font-lock-search-function
1426 'c-known-type-key
1427 '(1 'font-lock-type-face t)
1428 '((c-font-lock-declarators limit t nil)
1429 (save-match-data
1430 (goto-char (match-end 1))
1431 (c-forward-syntactic-ws))
1432 (goto-char (match-end 1))))))
1433
1434 ;; Fontify types preceded by `c-type-prefix-kwds' and the
1435 ;; identifiers in the declarations they might start.
1436 ,@(when (c-lang-const c-type-prefix-kwds)
1437 (let* ((prefix-re (c-make-keywords-re nil
1438 (c-lang-const c-type-prefix-kwds)))
1439 (type-match (+ 2
1440 (regexp-opt-depth prefix-re)
1441 (c-lang-const c-simple-ws-depth))))
1442 `((,(c-make-font-lock-search-function
1443 (concat "\\<\\(" prefix-re "\\)" ; 1
1444 (c-lang-const c-simple-ws) "+"
1445 (concat "\\(" ; 2 + prefix-re + c-simple-ws
1446 (c-lang-const c-symbol-key)
1447 "\\)"))
1448 `(,type-match
1449 'font-lock-type-face t)
1450 `((c-font-lock-declarators limit t nil)
1451 (save-match-data
1452 (goto-char (match-end ,type-match))
1453 (c-forward-syntactic-ws))
1454 (goto-char (match-end ,type-match))))))))
1455
1456 ;; Fontify special declarations that lacks a type.
1457 ,@(when (c-lang-const c-typeless-decl-kwds)
1458 `((,(c-make-font-lock-search-function
1459 (concat "\\<\\("
1460 (regexp-opt (c-lang-const c-typeless-decl-kwds))
1461 "\\)\\>")
1462 '((c-font-lock-declarators limit t nil)
1463 (save-match-data
1464 (goto-char (match-end 1))
1465 (c-forward-syntactic-ws))
1466 (goto-char (match-end 1)))))))
1467
1468 ;; Fontify generic colon labels in languages that support them.
1469 ,@(when (c-lang-const c-recognize-colon-labels)
1470 `(c-font-lock-labels))))
1471
1472 (c-lang-defconst c-complex-decl-matchers
1473 "Complex font lock matchers for types and declarations. Used on level
1474 3 and higher."
1475
1476 ;; Note: This code in this form dumps a number of functions into the
1477 ;; resulting constant, `c-matchers-3'. At run time, font lock will call
1478 ;; each of them as a "FUNCTION" (see Elisp page "Search-based
1479 ;; Fontification"). The font lock region is delimited by POINT and the
1480 ;; single parameter, LIMIT. Each of these functions returns NIL (thus
1481 ;; inhibiting spurious font-lock-keyword-face highlighting and another
1482 ;; call).
1483
1484 t `(;; Initialize some things before the search functions below.
1485 c-font-lock-complex-decl-prepare
1486
1487 ,@(if (c-major-mode-is 'objc-mode)
1488 ;; Fontify method declarations in Objective-C, but first
1489 ;; we have to put the `c-decl-end' `c-type' property on
1490 ;; all the @-style directives that haven't been handled in
1491 ;; `c-basic-matchers-before'.
1492 `(,(c-make-font-lock-search-function
1493 (c-make-keywords-re t
1494 ;; Exclude "@class" since that directive ends with a
1495 ;; semicolon anyway.
1496 (delete "@class"
1497 (append (c-lang-const c-protection-kwds)
1498 (c-lang-const c-other-decl-kwds)
1499 nil)))
1500 '((c-put-char-property (1- (match-end 1))
1501 'c-type 'c-decl-end)))
1502 c-font-lock-objc-methods))
1503
1504 ;; Fontify all declarations, casts and normal labels.
1505 c-font-lock-declarations
1506
1507 ;; Fontify declarators when POINT is within their declaration.
1508 c-font-lock-enclosing-decls
1509
1510 ;; Fontify angle bracket arglists like templates in C++.
1511 ,@(when (c-lang-const c-recognize-<>-arglists)
1512 `(c-font-lock-<>-arglists))
1513
1514 ;; The first two rules here mostly find occurrences that
1515 ;; `c-font-lock-declarations' has found already, but not
1516 ;; declarations containing blocks in the type (see note below).
1517 ;; It's also useful to fontify these everywhere to show e.g. when
1518 ;; a type keyword is accidentally used as an identifier.
1519
1520 ;; Fontify basic types.
1521 ,(let ((re (c-make-keywords-re nil
1522 (c-lang-const c-primitive-type-kwds))))
1523 (if (c-major-mode-is 'pike-mode)
1524 ;; No symbol is a keyword after "->" in Pike.
1525 `(,(concat "\\(\\=.?\\|[^>]\\|[^-]>\\)"
1526 "\\<\\(" re "\\)\\>")
1527 2 font-lock-type-face)
1528 `(,(concat "\\<\\(" re "\\)\\>")
1529 1 'font-lock-type-face)))
1530
1531 ;; Fontify types preceded by `c-type-prefix-kwds' (e.g. "struct").
1532 ,@(when (c-lang-const c-type-prefix-kwds)
1533 `((,(byte-compile
1534 `(lambda (limit)
1535 (c-fontify-types-and-refs
1536 ((c-promote-possible-types t)
1537 ;; The font-lock package in Emacs is known to clobber
1538 ;; `parse-sexp-lookup-properties' (when it exists).
1539 (parse-sexp-lookup-properties
1540 (cc-eval-when-compile
1541 (boundp 'parse-sexp-lookup-properties))))
1542 (save-restriction
1543 ;; Narrow to avoid going past the limit in
1544 ;; `c-forward-type'.
1545 (narrow-to-region (point) limit)
1546 (while (re-search-forward
1547 ,(concat "\\<\\("
1548 (c-make-keywords-re nil
1549 (c-lang-const c-type-prefix-kwds))
1550 "\\)\\>")
1551 limit t)
1552 (unless (c-skip-comments-and-strings limit)
1553 (c-forward-syntactic-ws)
1554 ;; Handle prefix declaration specifiers.
1555 (when (looking-at c-prefix-spec-kwds-re)
1556 (c-forward-keyword-clause 1))
1557 ,(if (c-major-mode-is 'c++-mode)
1558 `(when (and (c-forward-type)
1559 (eq (char-after) ?=))
1560 ;; In C++ we additionally check for a "class
1561 ;; X = Y" construct which is used in
1562 ;; templates, to fontify Y as a type.
1563 (forward-char)
1564 (c-forward-syntactic-ws)
1565 (c-forward-type))
1566 `(c-forward-type))
1567 )))))))))
1568
1569 ;; Fontify symbols after closing braces as declaration
1570 ;; identifiers under the assumption that they are part of
1571 ;; declarations like "class Foo { ... } foo;". It's too
1572 ;; expensive to check this accurately by skipping past the
1573 ;; brace block, so we use the heuristic that it's such a
1574 ;; declaration if the first identifier is on the same line as
1575 ;; the closing brace. `c-font-lock-declarations' will later
1576 ;; override it if it turns out to be an new declaration, but
1577 ;; it will be wrong if it's an expression (see the test
1578 ;; decls-8.cc).
1579 ;; ,@(when (c-lang-const c-opt-block-decls-with-vars-key)
1580 ;; `((,(c-make-font-lock-search-function
1581 ;; (concat "}"
1582 ;; (c-lang-const c-single-line-syntactic-ws)
1583 ;; "\\(" ; 1 + c-single-line-syntactic-ws-depth
1584 ;; (c-lang-const c-type-decl-prefix-key)
1585 ;; "\\|"
1586 ;; (c-lang-const c-symbol-key)
1587 ;; "\\)")
1588 ;; `((c-font-lock-declarators limit t nil) ; That `nil' says use `font-lock-variable-name-face';
1589 ;; ; `t' would mean `font-lock-function-name-face'.
1590 ;; (progn
1591 ;; (c-put-char-property (match-beginning 0) 'c-type
1592 ;; 'c-decl-id-start)
1593 ;; ; 'c-decl-type-start)
1594 ;; (goto-char (match-beginning
1595 ;; ,(1+ (c-lang-const
1596 ;; c-single-line-syntactic-ws-depth)))))
1597 ;; (goto-char (match-end 0)))))))
1598
1599 ;; Fontify the type in C++ "new" expressions.
1600 ,@(when (c-major-mode-is 'c++-mode)
1601 ;; This pattern is a probably a "(MATCHER . ANCHORED-HIGHLIGHTER)"
1602 ;; (see Elisp page "Search-based Fontification").
1603 `(("\\<new\\>"
1604 (c-font-lock-c++-new))))
1605 ))
1606
1607 (defun c-font-lock-labels (limit)
1608 ;; Fontify all statement labels from the point to LIMIT. Assumes
1609 ;; that strings and comments have been fontified already. Nil is
1610 ;; always returned.
1611 ;;
1612 ;; Note: This function is only used on decoration level 2; this is
1613 ;; taken care of directly by the gargantuan
1614 ;; `c-font-lock-declarations' on higher levels.
1615 ;;
1616 ;; This function might do hidden buffer changes.
1617
1618 (let (continue-pos id-start
1619 ;; The font-lock package in Emacs is known to clobber
1620 ;; `parse-sexp-lookup-properties' (when it exists).
1621 (parse-sexp-lookup-properties
1622 (cc-eval-when-compile
1623 (boundp 'parse-sexp-lookup-properties))))
1624
1625 (while (re-search-forward ":[^:]" limit t)
1626 (setq continue-pos (point))
1627 (goto-char (match-beginning 0))
1628 (unless (c-skip-comments-and-strings limit)
1629
1630 (c-backward-syntactic-ws)
1631 (and (setq id-start (c-on-identifier))
1632
1633 (not (get-text-property id-start 'face))
1634
1635 (progn
1636 (goto-char id-start)
1637 (c-backward-syntactic-ws)
1638 (or
1639 ;; Check for a char that precedes a statement.
1640 (memq (char-before) '(?\} ?\{ ?\;))
1641 ;; Check for a preceding label. We exploit the font
1642 ;; locking made earlier by this function.
1643 (and (eq (char-before) ?:)
1644 (progn
1645 (backward-char)
1646 (c-backward-syntactic-ws)
1647 (not (bobp)))
1648 (eq (get-text-property (1- (point)) 'face)
1649 c-label-face-name))
1650 ;; Check for a keyword that precedes a statement.
1651 (c-after-conditional)))
1652
1653 (progn
1654 ;; Got a label.
1655 (goto-char id-start)
1656 (looking-at c-symbol-key)
1657 (c-put-font-lock-face (match-beginning 0) (match-end 0)
1658 c-label-face-name)))
1659
1660 (goto-char continue-pos))))
1661 nil)
1662
1663 (c-lang-defconst c-basic-matchers-after
1664 "Font lock matchers for various things that should be fontified after
1665 generic casts and declarations are fontified. Used on level 2 and
1666 higher."
1667
1668 t `(,@(when (c-lang-const c-brace-id-list-kwds)
1669 ;; Fontify the remaining identifiers inside an enum list when we start
1670 ;; inside it.
1671 `(c-font-lock-enum-tail
1672 ;; Fontify the identifiers inside enum lists. (The enum type
1673 ;; name is handled by `c-simple-decl-matchers' or
1674 ;; `c-complex-decl-matchers' below.
1675 (,(c-make-font-lock-search-function
1676 (concat
1677 "\\<\\("
1678 (c-make-keywords-re nil (c-lang-const c-brace-id-list-kwds))
1679 "\\)\\>"
1680 ;; Disallow various common punctuation chars that can't come
1681 ;; before the '{' of the enum list, to avoid searching too far.
1682 "[^\]\[{}();,/#=]*"
1683 "{")
1684 '((c-font-lock-declarators limit t nil)
1685 (save-match-data
1686 (goto-char (match-end 0))
1687 (c-put-char-property (1- (point)) 'c-type
1688 'c-decl-id-start)
1689 (c-forward-syntactic-ws))
1690 (goto-char (match-end 0)))))))
1691
1692 ;; Fontify labels after goto etc.
1693 ,@(when (c-lang-const c-before-label-kwds)
1694 `(;; (Got three different interpretation levels here,
1695 ;; which makes it a bit complicated: 1) The backquote
1696 ;; stuff is expanded when compiled or loaded, 2) the
1697 ;; eval form is evaluated at font-lock setup (to
1698 ;; substitute c-label-face-name correctly), and 3) the
1699 ;; resulting structure is interpreted during
1700 ;; fontification.)
1701 (eval
1702 . ,(let* ((c-before-label-re
1703 (c-make-keywords-re nil
1704 (c-lang-const c-before-label-kwds))))
1705 `(list
1706 ,(concat "\\<\\(" c-before-label-re "\\)\\>"
1707 "\\s *"
1708 "\\(" ; identifier-offset
1709 (c-lang-const c-symbol-key)
1710 "\\)")
1711 (list ,(+ (regexp-opt-depth c-before-label-re) 2)
1712 c-label-face-name nil t))))))
1713
1714 ;; Fontify the clauses after various keywords.
1715 ,@(when (or (c-lang-const c-type-list-kwds)
1716 (c-lang-const c-ref-list-kwds)
1717 (c-lang-const c-colon-type-list-kwds)
1718 (c-lang-const c-paren-type-kwds))
1719 `((,(c-make-font-lock-search-function
1720 (concat "\\<\\("
1721 (c-make-keywords-re nil
1722 (append (c-lang-const c-type-list-kwds)
1723 (c-lang-const c-ref-list-kwds)
1724 (c-lang-const c-colon-type-list-kwds)
1725 (c-lang-const c-paren-type-kwds)))
1726 "\\)\\>")
1727 '((c-fontify-types-and-refs ((c-promote-possible-types t))
1728 (c-forward-keyword-clause 1)
1729 (if (> (point) limit) (goto-char limit))))))))
1730
1731 ,@(when (c-major-mode-is 'java-mode)
1732 `((eval . (list "\\<\\(@[a-zA-Z0-9]+\\)\\>" 1 c-annotation-face))))
1733 ))
1734
1735 (c-lang-defconst c-matchers-1
1736 t (c-lang-const c-cpp-matchers))
1737
1738 (c-lang-defconst c-matchers-2
1739 t (append (c-lang-const c-matchers-1)
1740 (c-lang-const c-basic-matchers-before)
1741 (c-lang-const c-simple-decl-matchers)
1742 (c-lang-const c-basic-matchers-after)))
1743
1744 (c-lang-defconst c-matchers-3
1745 t (append (c-lang-const c-matchers-1)
1746 (c-lang-const c-basic-matchers-before)
1747 (c-lang-const c-complex-decl-matchers)
1748 (c-lang-const c-basic-matchers-after)))
1749
1750 (defun c-compose-keywords-list (base-list)
1751 ;; Incorporate the font lock keyword lists according to
1752 ;; `c-doc-comment-style' on the given keyword list and return it.
1753 ;; This is used in the function bindings of the
1754 ;; `*-font-lock-keywords-*' symbols since we have to build the list
1755 ;; when font-lock is initialized.
1756
1757 (unless (memq c-doc-face-name c-literal-faces)
1758 (setq c-literal-faces (cons c-doc-face-name c-literal-faces)))
1759
1760 (let* ((doc-keywords
1761 (if (consp (car-safe c-doc-comment-style))
1762 (cdr-safe (or (assq c-buffer-is-cc-mode c-doc-comment-style)
1763 (assq 'other c-doc-comment-style)))
1764 c-doc-comment-style))
1765 (list (nconc (apply 'nconc
1766 (mapcar
1767 (lambda (doc-style)
1768 (let ((sym (intern
1769 (concat (symbol-name doc-style)
1770 "-font-lock-keywords"))))
1771 (cond ((fboundp sym)
1772 (funcall sym))
1773 ((boundp sym)
1774 (append (eval sym) nil)))))
1775 (if (listp doc-keywords)
1776 doc-keywords
1777 (list doc-keywords))))
1778 base-list)))
1779
1780 ;; Kludge: If `c-font-lock-complex-decl-prepare' is on the list we
1781 ;; move it first since the doc comment font lockers might add
1782 ;; `c-type' text properties, so they have to be cleared before that.
1783 (when (memq 'c-font-lock-complex-decl-prepare list)
1784 (setq list (cons 'c-font-lock-complex-decl-prepare
1785 (delq 'c-font-lock-complex-decl-prepare
1786 (append list nil)))))
1787
1788 list))
1789
1790 (defun c-override-default-keywords (def-var)
1791 ;; This is used to override the value on a `*-font-lock-keywords'
1792 ;; variable only if it's nil or has the same value as one of the
1793 ;; `*-font-lock-keywords-*' variables. Older font-lock packages
1794 ;; define a default value for `*-font-lock-keywords' which we want
1795 ;; to override, but we should otoh avoid clobbering a user setting.
1796 ;; This heuristic for that isn't perfect, but I can't think of any
1797 ;; better. /mast
1798 (when (and (boundp def-var)
1799 (memq (symbol-value def-var)
1800 (cons nil
1801 (mapcar
1802 (lambda (suffix)
1803 (let ((sym (intern (concat (symbol-name def-var)
1804 suffix))))
1805 (and (boundp sym) (symbol-value sym))))
1806 '("-1" "-2" "-3")))))
1807 ;; The overriding is done by unbinding the variable so that the normal
1808 ;; defvar will install its default value later on.
1809 (makunbound def-var)))
1810
1811 \f
1812 ;;; C.
1813
1814 (c-override-default-keywords 'c-font-lock-keywords)
1815
1816 (defconst c-font-lock-keywords-1 (c-lang-const c-matchers-1 c)
1817 "Minimal font locking for C mode.
1818 Fontifies only preprocessor directives (in addition to the syntactic
1819 fontification of strings and comments).")
1820
1821 (defconst c-font-lock-keywords-2 (c-lang-const c-matchers-2 c)
1822 "Fast normal font locking for C mode.
1823 In addition to `c-font-lock-keywords-1', this adds fontification of
1824 keywords, simple types, declarations that are easy to recognize, the
1825 user defined types on `c-font-lock-extra-types', and the doc comment
1826 styles specified by `c-doc-comment-style'.")
1827
1828 (defconst c-font-lock-keywords-3 (c-lang-const c-matchers-3 c)
1829 "Accurate normal font locking for C mode.
1830 Like `c-font-lock-keywords-2' but detects declarations in a more
1831 accurate way that works in most cases for arbitrary types without the
1832 need for `c-font-lock-extra-types'.")
1833
1834 (defvar c-font-lock-keywords c-font-lock-keywords-3
1835 "Default expressions to highlight in C mode.")
1836
1837 (defun c-font-lock-keywords-2 ()
1838 (c-compose-keywords-list c-font-lock-keywords-2))
1839 (defun c-font-lock-keywords-3 ()
1840 (c-compose-keywords-list c-font-lock-keywords-3))
1841 (defun c-font-lock-keywords ()
1842 (c-compose-keywords-list c-font-lock-keywords))
1843
1844 \f
1845 ;;; C++.
1846
1847 (defun c-font-lock-c++-new (limit)
1848 ;; FIXME!!! Put in a comment about the context of this function's
1849 ;; invocation. I think it's called as an ANCHORED-MATCHER within an
1850 ;; ANCHORED-HIGHLIGHTER. (2007/2/10).
1851 ;;
1852 ;; Assuming point is after a "new" word, check that it isn't inside
1853 ;; a string or comment, and if so try to fontify the type in the
1854 ;; allocation expression. Nil is always returned.
1855 ;;
1856 ;; As usual, C++ takes the prize in coming up with a hard to parse
1857 ;; syntax. :P
1858 ;;
1859 ;; This function might do hidden buffer changes.
1860
1861 (unless (c-skip-comments-and-strings limit)
1862 (save-excursion
1863 (catch 'false-alarm
1864 ;; A "new" keyword is followed by one to three expressions, where
1865 ;; the type is the middle one, and the only required part.
1866 (let (expr1-pos expr2-pos
1867 ;; Enable recording of identifier ranges in `c-forward-type'
1868 ;; etc for later fontification. Not using
1869 ;; `c-fontify-types-and-refs' here since the ranges should
1870 ;; be fontified selectively only when an allocation
1871 ;; expression is successfully recognized.
1872 (c-record-type-identifiers t)
1873 c-record-ref-identifiers
1874 ;; The font-lock package in Emacs is known to clobber
1875 ;; `parse-sexp-lookup-properties' (when it exists).
1876 (parse-sexp-lookup-properties
1877 (cc-eval-when-compile
1878 (boundp 'parse-sexp-lookup-properties))))
1879 (c-forward-syntactic-ws)
1880
1881 ;; The first placement arglist is always parenthesized, if it
1882 ;; exists.
1883 (when (eq (char-after) ?\()
1884 (setq expr1-pos (1+ (point)))
1885 (condition-case nil
1886 (c-forward-sexp)
1887 (scan-error (throw 'false-alarm t)))
1888 (c-forward-syntactic-ws))
1889
1890 ;; The second expression is either a type followed by some "*" or
1891 ;; "[...]" or similar, or a parenthesized type followed by a full
1892 ;; identifierless declarator.
1893 (setq expr2-pos (1+ (point)))
1894 (cond ((eq (char-after) ?\())
1895 ((let ((c-promote-possible-types t))
1896 (c-forward-type)))
1897 (t (setq expr2-pos nil)))
1898
1899 (when expr1-pos
1900 (cond
1901 ((not expr2-pos)
1902 ;; No second expression, so the first has to be a
1903 ;; parenthesized type.
1904 (goto-char expr1-pos)
1905 (let ((c-promote-possible-types t))
1906 (c-forward-type)))
1907
1908 ((eq (char-before expr2-pos) ?\()
1909 ;; Got two parenthesized expressions, so we have to look
1910 ;; closer at them to decide which is the type. No need to
1911 ;; handle `c-record-ref-identifiers' since all references
1912 ;; has already been handled by other fontification rules.
1913 (let (expr1-res expr2-res)
1914
1915 (goto-char expr1-pos)
1916 (when (setq expr1-res (c-forward-type))
1917 (unless (looking-at
1918 (cc-eval-when-compile
1919 (concat (c-lang-const c-symbol-start c++)
1920 "\\|[*:\)\[]")))
1921 ;; There's something after the would-be type that
1922 ;; can't be there, so this is a placement arglist.
1923 (setq expr1-res nil)))
1924
1925 (goto-char expr2-pos)
1926 (when (setq expr2-res (c-forward-type))
1927 (unless (looking-at
1928 (cc-eval-when-compile
1929 (concat (c-lang-const c-symbol-start c++)
1930 "\\|[*:\)\[]")))
1931 ;; There's something after the would-be type that can't
1932 ;; be there, so this is an initialization expression.
1933 (setq expr2-res nil))
1934 (when (and (c-go-up-list-forward)
1935 (progn (c-forward-syntactic-ws)
1936 (eq (char-after) ?\()))
1937 ;; If there's a third initialization expression
1938 ;; then the second one is the type, so demote the
1939 ;; first match.
1940 (setq expr1-res nil)))
1941
1942 ;; We fontify the most likely type, with a preference for
1943 ;; the first argument since a placement arglist is more
1944 ;; unusual than an initializer.
1945 (cond ((memq expr1-res '(t known prefix)))
1946 ((memq expr2-res '(t known prefix)))
1947 ((eq expr1-res 'found)
1948 (let ((c-promote-possible-types t))
1949 (goto-char expr1-pos)
1950 (c-forward-type)))
1951 ((eq expr2-res 'found)
1952 (let ((c-promote-possible-types t))
1953 (goto-char expr2-pos)
1954 (c-forward-type)))
1955 ((and (eq expr1-res 'maybe) (not expr2-res))
1956 (let ((c-promote-possible-types t))
1957 (goto-char expr1-pos)
1958 (c-forward-type)))
1959 ((and (not expr1-res) (eq expr2-res 'maybe))
1960 (let ((c-promote-possible-types t))
1961 (goto-char expr2-pos)
1962 (c-forward-type)))
1963 ;; If both type matches are 'maybe then we're
1964 ;; too uncertain to promote either of them.
1965 )))))
1966
1967 ;; Fontify the type that now is recorded in
1968 ;; `c-record-type-identifiers', if any.
1969 (c-fontify-recorded-types-and-refs)))))
1970 nil)
1971
1972 (c-override-default-keywords 'c++-font-lock-keywords)
1973
1974 (defconst c++-font-lock-keywords-1 (c-lang-const c-matchers-1 c++)
1975 "Minimal font locking for C++ mode.
1976 Fontifies only preprocessor directives (in addition to the syntactic
1977 fontification of strings and comments).")
1978
1979 (defconst c++-font-lock-keywords-2 (c-lang-const c-matchers-2 c++)
1980 "Fast normal font locking for C++ mode.
1981 In addition to `c++-font-lock-keywords-1', this adds fontification of
1982 keywords, simple types, declarations that are easy to recognize, the
1983 user defined types on `c++-font-lock-extra-types', and the doc comment
1984 styles specified by `c-doc-comment-style'.")
1985
1986 (defconst c++-font-lock-keywords-3 (c-lang-const c-matchers-3 c++)
1987 "Accurate normal font locking for C++ mode.
1988 Like `c++-font-lock-keywords-2' but detects declarations in a more
1989 accurate way that works in most cases for arbitrary types without the
1990 need for `c++-font-lock-extra-types'.")
1991
1992 (defvar c++-font-lock-keywords c++-font-lock-keywords-3
1993 "Default expressions to highlight in C++ mode.")
1994
1995 (defun c++-font-lock-keywords-2 ()
1996 (c-compose-keywords-list c++-font-lock-keywords-2))
1997 (defun c++-font-lock-keywords-3 ()
1998 (c-compose-keywords-list c++-font-lock-keywords-3))
1999 (defun c++-font-lock-keywords ()
2000 (c-compose-keywords-list c++-font-lock-keywords))
2001
2002 \f
2003 ;;; Objective-C.
2004
2005 (defun c-font-lock-objc-method ()
2006 ;; Assuming the point is after the + or - that starts an Objective-C
2007 ;; method declaration, fontify it. This must be done before normal
2008 ;; casts, declarations and labels are fontified since they will get
2009 ;; false matches in these things.
2010 ;;
2011 ;; This function might do hidden buffer changes.
2012
2013 (c-fontify-types-and-refs
2014 ((first t)
2015 (c-promote-possible-types t))
2016
2017 (while (and
2018 (progn
2019 (c-forward-syntactic-ws)
2020
2021 ;; An optional method type.
2022 (if (eq (char-after) ?\()
2023 (progn
2024 (forward-char)
2025 (c-forward-syntactic-ws)
2026 (c-forward-type)
2027 (prog1 (c-go-up-list-forward)
2028 (c-forward-syntactic-ws)))
2029 t))
2030
2031 ;; The name. The first time it's the first part of
2032 ;; the function name, the rest of the time it's an
2033 ;; argument name.
2034 (looking-at c-symbol-key)
2035 (progn
2036 (goto-char (match-end 0))
2037 (c-put-font-lock-face (match-beginning 0)
2038 (point)
2039 (if first
2040 'font-lock-function-name-face
2041 'font-lock-variable-name-face))
2042 (c-forward-syntactic-ws)
2043
2044 ;; Another optional part of the function name.
2045 (when (looking-at c-symbol-key)
2046 (goto-char (match-end 0))
2047 (c-put-font-lock-face (match-beginning 0)
2048 (point)
2049 'font-lock-function-name-face)
2050 (c-forward-syntactic-ws))
2051
2052 ;; There's another argument if a colon follows.
2053 (eq (char-after) ?:)))
2054 (forward-char)
2055 (setq first nil))))
2056
2057 (defun c-font-lock-objc-methods (limit)
2058 ;; Fontify method declarations in Objective-C. Nil is always
2059 ;; returned.
2060 ;;
2061 ;; This function might do hidden buffer changes.
2062
2063 (let (;; The font-lock package in Emacs is known to clobber
2064 ;; `parse-sexp-lookup-properties' (when it exists).
2065 (parse-sexp-lookup-properties
2066 (cc-eval-when-compile
2067 (boundp 'parse-sexp-lookup-properties))))
2068
2069 (c-find-decl-spots
2070 limit
2071 "[-+]"
2072 nil
2073 (lambda (match-pos inside-macro)
2074 (forward-char)
2075 (c-font-lock-objc-method))))
2076 nil)
2077
2078 (c-override-default-keywords 'objc-font-lock-keywords)
2079
2080 (defconst objc-font-lock-keywords-1 (c-lang-const c-matchers-1 objc)
2081 "Minimal font locking for Objective-C mode.
2082 Fontifies only compiler directives (in addition to the syntactic
2083 fontification of strings and comments).")
2084
2085 (defconst objc-font-lock-keywords-2 (c-lang-const c-matchers-2 objc)
2086 "Fast normal font locking for Objective-C mode.
2087 In addition to `objc-font-lock-keywords-1', this adds fontification of
2088 keywords, simple types, declarations that are easy to recognize, the
2089 user defined types on `objc-font-lock-extra-types', and the doc
2090 comment styles specified by `c-doc-comment-style'.")
2091
2092 (defconst objc-font-lock-keywords-3 (c-lang-const c-matchers-3 objc)
2093 "Accurate normal font locking for Objective-C mode.
2094 Like `objc-font-lock-keywords-2' but detects declarations in a more
2095 accurate way that works in most cases for arbitrary types without the
2096 need for `objc-font-lock-extra-types'.")
2097
2098 (defvar objc-font-lock-keywords objc-font-lock-keywords-3
2099 "Default expressions to highlight in Objective-C mode.")
2100
2101 (defun objc-font-lock-keywords-2 ()
2102 (c-compose-keywords-list objc-font-lock-keywords-2))
2103 (defun objc-font-lock-keywords-3 ()
2104 (c-compose-keywords-list objc-font-lock-keywords-3))
2105 (defun objc-font-lock-keywords ()
2106 (c-compose-keywords-list objc-font-lock-keywords))
2107
2108 ;; Kludge to override the default value that
2109 ;; `objc-font-lock-extra-types' might have gotten from the font-lock
2110 ;; package. The value replaced here isn't relevant now anyway since
2111 ;; those types are builtin and therefore listed directly in
2112 ;; `c-primitive-type-kwds'.
2113 (when (equal (sort (append objc-font-lock-extra-types nil) 'string-lessp)
2114 '("BOOL" "Class" "IMP" "SEL"))
2115 (setq objc-font-lock-extra-types
2116 (cc-eval-when-compile (list (concat "[" c-upper "]\\sw*")))))
2117
2118 \f
2119 ;;; Java.
2120
2121 (c-override-default-keywords 'java-font-lock-keywords)
2122
2123 (defconst java-font-lock-keywords-1 (c-lang-const c-matchers-1 java)
2124 "Minimal font locking for Java mode.
2125 Fontifies nothing except the syntactic fontification of strings and
2126 comments.")
2127
2128 (defconst java-font-lock-keywords-2 (c-lang-const c-matchers-2 java)
2129 "Fast normal font locking for Java mode.
2130 In addition to `java-font-lock-keywords-1', this adds fontification of
2131 keywords, simple types, declarations that are easy to recognize, the
2132 user defined types on `java-font-lock-extra-types', and the doc
2133 comment styles specified by `c-doc-comment-style'.")
2134
2135 (defconst java-font-lock-keywords-3 (c-lang-const c-matchers-3 java)
2136 "Accurate normal font locking for Java mode.
2137 Like `java-font-lock-keywords-2' but detects declarations in a more
2138 accurate way that works in most cases for arbitrary types without the
2139 need for `java-font-lock-extra-types'.")
2140
2141 (defvar java-font-lock-keywords java-font-lock-keywords-3
2142 "Default expressions to highlight in Java mode.")
2143
2144 (defun java-font-lock-keywords-2 ()
2145 (c-compose-keywords-list java-font-lock-keywords-2))
2146 (defun java-font-lock-keywords-3 ()
2147 (c-compose-keywords-list java-font-lock-keywords-3))
2148 (defun java-font-lock-keywords ()
2149 (c-compose-keywords-list java-font-lock-keywords))
2150
2151 \f
2152 ;;; CORBA IDL.
2153
2154 (c-override-default-keywords 'idl-font-lock-keywords)
2155
2156 (defconst idl-font-lock-keywords-1 (c-lang-const c-matchers-1 idl)
2157 "Minimal font locking for CORBA IDL mode.
2158 Fontifies nothing except the syntactic fontification of strings and
2159 comments.")
2160
2161 (defconst idl-font-lock-keywords-2 (c-lang-const c-matchers-2 idl)
2162 "Fast normal font locking for CORBA IDL mode.
2163 In addition to `idl-font-lock-keywords-1', this adds fontification of
2164 keywords, simple types, declarations that are easy to recognize, the
2165 user defined types on `idl-font-lock-extra-types', and the doc comment
2166 styles specified by `c-doc-comment-style'.")
2167
2168 (defconst idl-font-lock-keywords-3 (c-lang-const c-matchers-3 idl)
2169 "Accurate normal font locking for CORBA IDL mode.
2170 Like `idl-font-lock-keywords-2' but detects declarations in a more
2171 accurate way that works in most cases for arbitrary types without the
2172 need for `idl-font-lock-extra-types'.")
2173
2174 (defvar idl-font-lock-keywords idl-font-lock-keywords-3
2175 "Default expressions to highlight in CORBA IDL mode.")
2176
2177 (defun idl-font-lock-keywords-2 ()
2178 (c-compose-keywords-list idl-font-lock-keywords-2))
2179 (defun idl-font-lock-keywords-3 ()
2180 (c-compose-keywords-list idl-font-lock-keywords-3))
2181 (defun idl-font-lock-keywords ()
2182 (c-compose-keywords-list idl-font-lock-keywords))
2183
2184 \f
2185 ;;; Pike.
2186
2187 (c-override-default-keywords 'pike-font-lock-keywords)
2188
2189 (defconst pike-font-lock-keywords-1 (c-lang-const c-matchers-1 pike)
2190 "Minimal font locking for Pike mode.
2191 Fontifies only preprocessor directives (in addition to the syntactic
2192 fontification of strings and comments).")
2193
2194 (defconst pike-font-lock-keywords-2 (c-lang-const c-matchers-2 pike)
2195 "Fast normal font locking for Pike mode.
2196 In addition to `pike-font-lock-keywords-1', this adds fontification of
2197 keywords, simple types, declarations that are easy to recognize, the
2198 user defined types on `pike-font-lock-extra-types', and the doc
2199 comment styles specified by `c-doc-comment-style'.")
2200
2201 (defconst pike-font-lock-keywords-3 (c-lang-const c-matchers-3 pike)
2202 "Accurate normal font locking for Pike mode.
2203 Like `pike-font-lock-keywords-2' but detects declarations in a more
2204 accurate way that works in most cases for arbitrary types without the
2205 need for `pike-font-lock-extra-types'.")
2206
2207 (defvar pike-font-lock-keywords pike-font-lock-keywords-3
2208 "Default expressions to highlight in Pike mode.")
2209
2210 (defun pike-font-lock-keywords-2 ()
2211 (c-compose-keywords-list pike-font-lock-keywords-2))
2212 (defun pike-font-lock-keywords-3 ()
2213 (c-compose-keywords-list pike-font-lock-keywords-3))
2214 (defun pike-font-lock-keywords ()
2215 (c-compose-keywords-list pike-font-lock-keywords))
2216
2217 \f
2218 ;;; Doc comments.
2219
2220 (defun c-font-lock-doc-comments (prefix limit keywords)
2221 ;; Fontify the comments between the point and LIMIT whose start
2222 ;; matches PREFIX with `c-doc-face-name'. Assumes comments have been
2223 ;; fontified with `font-lock-comment-face' already. nil is always
2224 ;; returned.
2225 ;;
2226 ;; After the fontification of a matching comment, fontification
2227 ;; according to KEYWORDS is applied inside it. It's a list like
2228 ;; `font-lock-keywords' except that anchored matches and eval
2229 ;; clauses aren't supported and that some abbreviated forms can't be
2230 ;; used. The buffer is narrowed to the comment while KEYWORDS is
2231 ;; applied; leading comment starters are included but trailing
2232 ;; comment enders for block comment are not.
2233 ;;
2234 ;; Note that faces added through KEYWORDS should never replace the
2235 ;; existing `c-doc-face-name' face since the existence of that face
2236 ;; is used as a flag in other code to skip comments.
2237 ;;
2238 ;; This function might do hidden buffer changes.
2239
2240 (let (comment-beg region-beg)
2241 (if (eq (get-text-property (point) 'face)
2242 'font-lock-comment-face)
2243 ;; Handle the case when the fontified region starts inside a
2244 ;; comment.
2245 (let ((range (c-literal-limits)))
2246 (setq region-beg (point))
2247 (when range
2248 (goto-char (car range)))
2249 (when (looking-at prefix)
2250 (setq comment-beg (point)))))
2251
2252 (while (or
2253 comment-beg
2254
2255 ;; Search for the prefix until a match is found at the start
2256 ;; of a comment.
2257 (while (when (re-search-forward prefix limit t)
2258 (setq comment-beg (match-beginning 0))
2259 (or (not (c-got-face-at comment-beg
2260 c-literal-faces))
2261 (and (/= comment-beg (point-min))
2262 (c-got-face-at (1- comment-beg)
2263 c-literal-faces))))
2264 (setq comment-beg nil))
2265 (setq region-beg comment-beg))
2266
2267 (if (eq (elt (parse-partial-sexp comment-beg (+ comment-beg 2)) 7) t)
2268 ;; Collect a sequence of doc style line comments.
2269 (progn
2270 (goto-char comment-beg)
2271 (while (and (progn
2272 (c-forward-single-comment)
2273 (skip-syntax-forward " ")
2274 (< (point) limit))
2275 (looking-at prefix))))
2276 (goto-char comment-beg)
2277 (c-forward-single-comment))
2278 (if (> (point) limit) (goto-char limit))
2279 (setq comment-beg nil)
2280
2281 (let ((region-end (point))
2282 (keylist keywords) keyword matcher highlights)
2283 (c-put-font-lock-face region-beg region-end c-doc-face-name)
2284 (save-restriction
2285 ;; Narrow to the doc comment. Among other things, this
2286 ;; helps by making "^" match at the start of the comment.
2287 ;; Do not include a trailing block comment ender, though.
2288 (and (> region-end (1+ region-beg))
2289 (progn (goto-char region-end)
2290 (backward-char 2)
2291 (looking-at "\\*/"))
2292 (setq region-end (point)))
2293 (narrow-to-region region-beg region-end)
2294
2295 (while keylist
2296 (setq keyword (car keylist)
2297 keylist (cdr keylist)
2298 matcher (car keyword))
2299 (goto-char region-beg)
2300 (while (if (stringp matcher)
2301 (re-search-forward matcher region-end t)
2302 (funcall matcher region-end))
2303 (setq highlights (cdr keyword))
2304 (if (consp (car highlights))
2305 (while highlights
2306 (font-lock-apply-highlight (car highlights))
2307 (setq highlights (cdr highlights)))
2308 (font-lock-apply-highlight highlights))))
2309
2310 (goto-char region-end)))))
2311 nil)
2312 (put 'c-font-lock-doc-comments 'lisp-indent-function 2)
2313
2314 (defun c-find-invalid-doc-markup (regexp limit)
2315 ;; Used to fontify invalid markup in doc comments after the correct
2316 ;; ones have been fontified: Find the first occurrence of REGEXP
2317 ;; between the point and LIMIT that only is fontified with
2318 ;; `c-doc-face-name'. If a match is found then submatch 0 surrounds
2319 ;; the first char and t is returned, otherwise nil is returned.
2320 ;;
2321 ;; This function might do hidden buffer changes.
2322 (let (start)
2323 (while (if (re-search-forward regexp limit t)
2324 (not (eq (get-text-property
2325 (setq start (match-beginning 0)) 'face)
2326 c-doc-face-name))
2327 (setq start nil)))
2328 (when start
2329 (store-match-data (list (copy-marker start)
2330 (copy-marker (1+ start))))
2331 t)))
2332
2333 ;; GtkDoc patterns contributed by Masatake YAMATO <jet@gyve.org>.
2334
2335 (defconst gtkdoc-font-lock-doc-comments
2336 (let ((symbol "[a-zA-Z0-9_]+")
2337 (header "^ \\* "))
2338 `((,(concat header "\\(" symbol "\\):[ \t]*$")
2339 1 ,c-doc-markup-face-name prepend nil)
2340 (,(concat symbol "()")
2341 0 ,c-doc-markup-face-name prepend nil)
2342 (,(concat header "\\(" "@" symbol "\\):")
2343 1 ,c-doc-markup-face-name prepend nil)
2344 (,(concat "[#%@]" symbol)
2345 0 ,c-doc-markup-face-name prepend nil))
2346 ))
2347
2348 (defconst gtkdoc-font-lock-doc-protection
2349 `(("< \\(public\\|private\\|protected\\) >"
2350 1 ,c-doc-markup-face-name prepend nil)))
2351
2352 (defconst gtkdoc-font-lock-keywords
2353 `((,(lambda (limit)
2354 (c-font-lock-doc-comments "/\\*\\*$" limit
2355 gtkdoc-font-lock-doc-comments)
2356 (c-font-lock-doc-comments "/\\*< " limit
2357 gtkdoc-font-lock-doc-protection)
2358 ))))
2359
2360 ;; Javadoc.
2361
2362 (defconst javadoc-font-lock-doc-comments
2363 `(("{@[a-z]+[^}\n\r]*}" ; "{@foo ...}" markup.
2364 0 ,c-doc-markup-face-name prepend nil)
2365 ("^\\(/\\*\\)?\\(\\s \\|\\*\\)*\\(@[a-z]+\\)" ; "@foo ..." markup.
2366 3 ,c-doc-markup-face-name prepend nil)
2367 (,(concat "</?\\sw" ; HTML tags.
2368 "\\("
2369 (concat "\\sw\\|\\s \\|[=\n\r*.:]\\|"
2370 "\"[^\"]*\"\\|'[^']*'")
2371 "\\)*>")
2372 0 ,c-doc-markup-face-name prepend nil)
2373 ("&\\(\\sw\\|[.:]\\)+;" ; HTML entities.
2374 0 ,c-doc-markup-face-name prepend nil)
2375 ;; Fontify remaining markup characters as invalid. Note
2376 ;; that the Javadoc spec is hazy about when "@" is
2377 ;; allowed in non-markup use.
2378 (,(lambda (limit)
2379 (c-find-invalid-doc-markup "[<>&]\\|{@" limit))
2380 0 'font-lock-warning-face prepend nil)))
2381
2382 (defconst javadoc-font-lock-keywords
2383 `((,(lambda (limit)
2384 (c-font-lock-doc-comments "/\\*\\*" limit
2385 javadoc-font-lock-doc-comments)))))
2386
2387 ;; Pike autodoc.
2388
2389 (defconst autodoc-decl-keywords
2390 ;; Adorned regexp matching the keywords that introduce declarations
2391 ;; in Pike Autodoc.
2392 (cc-eval-when-compile
2393 (c-make-keywords-re t '("@decl" "@elem" "@index" "@member") 'pike-mode)))
2394
2395 (defconst autodoc-decl-type-keywords
2396 ;; Adorned regexp matching the keywords that are followed by a type.
2397 (cc-eval-when-compile
2398 (c-make-keywords-re t '("@elem" "@member") 'pike-mode)))
2399
2400 (defun autodoc-font-lock-line-markup (limit)
2401 ;; Fontify all line oriented keywords between the point and LIMIT.
2402 ;; Nil is always returned.
2403 ;;
2404 ;; This function might do hidden buffer changes.
2405
2406 (let ((line-re (concat "^\\(\\(/\\*!\\|\\s *\\("
2407 c-current-comment-prefix
2408 "\\)\\)\\s *\\)@[A-Za-z_-]+\\(\\s \\|$\\)"))
2409 (markup-faces (list c-doc-markup-face-name c-doc-face-name)))
2410
2411 (while (re-search-forward line-re limit t)
2412 (goto-char (match-end 1))
2413
2414 (if (looking-at autodoc-decl-keywords)
2415 (let* ((kwd-pos (point))
2416 (start (match-end 1))
2417 (pos start)
2418 end)
2419
2420 (c-put-font-lock-face (point) pos markup-faces)
2421
2422 ;; Put a declaration end mark at the markup keyword and
2423 ;; remove the faces from the rest of the line so that it
2424 ;; gets refontified as a declaration later on by
2425 ;; `c-font-lock-declarations'.
2426 (c-put-char-property (1- pos) 'c-type 'c-decl-end)
2427 (goto-char pos)
2428 (while (progn
2429 (end-of-line)
2430 (setq end (point))
2431 (and (eq (char-before) ?@)
2432 (not (eobp))
2433 (progn (forward-char)
2434 (skip-syntax-forward " ")
2435 (looking-at c-current-comment-prefix))))
2436 (goto-char (match-end 0))
2437 (c-remove-font-lock-face pos (1- end))
2438 (c-put-font-lock-face (1- end) end markup-faces)
2439 (setq pos (point)))
2440
2441 ;; Include the final newline in the removed area. This
2442 ;; has no visual effect but it avoids some tricky special
2443 ;; cases in the testsuite wrt the differences in string
2444 ;; fontification in Emacs vs XEmacs.
2445 (c-remove-font-lock-face pos (min (1+ (point)) (point-max)))
2446
2447 ;; Must handle string literals explicitly inside the declaration.
2448 (goto-char start)
2449 (while (re-search-forward
2450 "\"\\([^\\\"]\\|\\\\.\\)*\"\\|'\\([^\\']\\|\\\\.\\)*'"
2451 end 'move)
2452 (c-put-font-lock-string-face (match-beginning 0)
2453 (point)))
2454
2455 ;; Fontify types after keywords that always are followed
2456 ;; by them.
2457 (goto-char kwd-pos)
2458 (when (looking-at autodoc-decl-type-keywords)
2459 (c-fontify-types-and-refs ((c-promote-possible-types t))
2460 (goto-char start)
2461 (c-forward-syntactic-ws)
2462 (c-forward-type))))
2463
2464 ;; Mark each whole line as markup, as long as the logical line
2465 ;; continues.
2466 (while (progn
2467 (c-put-font-lock-face (point)
2468 (progn (end-of-line) (point))
2469 markup-faces)
2470 (and (eq (char-before) ?@)
2471 (not (eobp))
2472 (progn (forward-char)
2473 (skip-syntax-forward " ")
2474 (looking-at c-current-comment-prefix))))
2475 (goto-char (match-end 0))))))
2476
2477 nil)
2478
2479 (defconst autodoc-font-lock-doc-comments
2480 `(("@\\(\\w+{\\|\\[\\([^\]@\n\r]\\|@@\\)*\\]\\|[@}]\\|$\\)"
2481 ;; In-text markup.
2482 0 ,c-doc-markup-face-name prepend nil)
2483 (autodoc-font-lock-line-markup)
2484 ;; Fontify remaining markup characters as invalid.
2485 (,(lambda (limit)
2486 (c-find-invalid-doc-markup "@" limit))
2487 0 'font-lock-warning-face prepend nil)
2488 ))
2489
2490 (defun autodoc-font-lock-keywords ()
2491 ;; Note that we depend on that `c-current-comment-prefix' has got
2492 ;; its proper value here.
2493 ;;
2494 ;; This function might do hidden buffer changes.
2495
2496 ;; The `c-type' text property with `c-decl-end' is used to mark the
2497 ;; end of the `autodoc-decl-keywords' occurrences to fontify the
2498 ;; following declarations.
2499 (setq c-type-decl-end-used t)
2500
2501 `((,(lambda (limit)
2502 (c-font-lock-doc-comments "/[*/]!" limit
2503 autodoc-font-lock-doc-comments)))))
2504
2505 \f
2506 ;; 2006-07-10: awk-font-lock-keywords has been moved back to cc-awk.el.
2507 (cc-provide 'cc-fonts)
2508
2509 ;;; cc-fonts.el ends here