Configure Symbola font only for symbols and punctuation
[emacs.git] / lisp / progmodes / cc-fonts.el
blobad564f7c3dfee2a2a171aed95255cbf9635c03c9
1 ;;; cc-fonts.el --- font lock support for CC Mode
3 ;; Copyright (C) 2002-2015 Free Software Foundation, Inc.
5 ;; Authors: 2003- Alan Mackenzie
6 ;; 2002- Martin Stjernholm
7 ;; Maintainer: bug-cc-mode@gnu.org
8 ;; Created: 07-Jan-2002
9 ;; Keywords: c languages
10 ;; Package: cc-mode
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;;; Commentary:
29 ;; Some comments on the use of faces:
31 ;; o `c-label-face-name' is either `font-lock-constant-face' (in
32 ;; Emacs), or `font-lock-reference-face'.
34 ;; o `c-constant-face-name', `c-reference-face-name' and
35 ;; `c-doc-markup-face-name' are essentially set up like
36 ;; `c-label-face-name'.
38 ;; o `c-preprocessor-face-name' is `font-lock-preprocessor-face' in
39 ;; XEmacs and - in lack of a closer equivalent -
40 ;; `font-lock-builtin-face' or `font-lock-reference-face' in Emacs.
42 ;; o `c-doc-face-name' is `font-lock-doc-string-face' in XEmacs,
43 ;; `font-lock-doc-face' in Emacs 21 and later, or
44 ;; `font-lock-comment-face' in older Emacs (that since source
45 ;; documentation are actually comments in these languages, as opposed
46 ;; to elisp).
48 ;; TBD: We should probably provide real faces for the above uses and
49 ;; instead initialize them from the standard faces.
51 ;;; Code:
53 ;; The faces that already have been put onto the text is tested in
54 ;; various places to direct further fontifications. For this to work,
55 ;; the following assumptions regarding the faces must hold (apart from
56 ;; the dependencies on the font locking order):
58 ;; o `font-lock-comment-face' and the face in `c-doc-face-name' is
59 ;; not used in anything but comments.
60 ;; o If any face (e.g. `c-doc-markup-face-name') but those above is
61 ;; used in comments, it doesn't replace them.
62 ;; o `font-lock-string-face' is not used in anything but string
63 ;; literals (single or double quoted).
64 ;; o `font-lock-keyword-face' and the face in `c-label-face-name' are
65 ;; never overlaid with other faces.
67 (eval-when-compile
68 (let ((load-path
69 (if (and (boundp 'byte-compile-dest-file)
70 (stringp byte-compile-dest-file))
71 (cons (file-name-directory byte-compile-dest-file) load-path)
72 load-path)))
73 (load "cc-bytecomp" nil t)))
75 (cc-require 'cc-defs)
76 (cc-require-when-compile 'cc-langs)
77 (cc-require 'cc-vars)
78 (cc-require 'cc-engine)
79 (cc-require-when-compile 'cc-awk) ; Change from cc-require, 2003/6/18 to
80 ;; prevent cc-awk being loaded when it's not needed. There is now a (require
81 ;; 'cc-awk) in (defun awk-mode ..).
83 ;; Avoid repeated loading through the eval-after-load directive in
84 ;; cc-mode.el.
85 (provide 'cc-fonts)
87 (cc-external-require 'font-lock)
89 (cc-bytecomp-defvar parse-sexp-lookup-properties) ; Emacs only.
91 ;; Need to declare these local symbols during compilation since
92 ;; they're referenced from lambdas in `byte-compile' calls that are
93 ;; executed at compile time. They don't need to have the proper
94 ;; definitions, though, since the generated functions aren't called
95 ;; during compilation.
96 (cc-bytecomp-defvar c-preprocessor-face-name)
97 (cc-bytecomp-defvar c-reference-face-name)
98 (cc-bytecomp-defun c-fontify-recorded-types-and-refs)
99 (cc-bytecomp-defun c-font-lock-declarators)
100 (cc-bytecomp-defun c-font-lock-objc-method)
101 (cc-bytecomp-defun c-font-lock-invalid-string)
104 ;; Note that font-lock in XEmacs doesn't expand face names as
105 ;; variables, so we have to use the (eval . FORM) in the font lock
106 ;; matchers wherever we use these alias variables.
108 (defconst c-preprocessor-face-name
109 (cond ((c-face-name-p 'font-lock-preprocessor-face)
110 ;; XEmacs has a font-lock-preprocessor-face.
111 'font-lock-preprocessor-face)
112 ((c-face-name-p 'font-lock-builtin-face)
113 ;; In Emacs font-lock-builtin-face has traditionally been
114 ;; used for preprocessor directives.
115 'font-lock-builtin-face)
117 'font-lock-reference-face)))
119 (cc-bytecomp-defvar font-lock-constant-face)
121 (defconst c-label-face-name
122 (cond ((c-face-name-p 'font-lock-label-face)
123 ;; If it happens to occur in the future. (Well, the more
124 ;; pragmatic reason is to get unique faces for the test
125 ;; suite.)
126 'font-lock-label-face)
127 ((and (c-face-name-p 'font-lock-constant-face)
128 (eq font-lock-constant-face 'font-lock-constant-face))
129 ;; Test both if font-lock-constant-face exists and that it's
130 ;; not an alias for something else. This is important since
131 ;; we compare already set faces in various places.
132 'font-lock-constant-face)
134 'font-lock-reference-face)))
136 (defconst c-constant-face-name
137 (if (and (c-face-name-p 'font-lock-constant-face)
138 (eq font-lock-constant-face 'font-lock-constant-face))
139 ;; This doesn't exist in some earlier versions of XEmacs 21.
140 'font-lock-constant-face
141 c-label-face-name))
143 (defconst c-reference-face-name
144 (with-no-warnings
145 (if (and (c-face-name-p 'font-lock-reference-face)
146 (eq font-lock-reference-face 'font-lock-reference-face))
147 ;; This is considered obsolete in Emacs, but it still maps well
148 ;; to this use. (Another reason to do this is to get unique
149 ;; faces for the test suite.)
150 'font-lock-reference-face
151 c-label-face-name)))
153 ;; This should not mapped to a face that also is used to fontify things
154 ;; that aren't comments or string literals.
155 (defconst c-doc-face-name
156 (cond ((c-face-name-p 'font-lock-doc-string-face)
157 ;; XEmacs.
158 'font-lock-doc-string-face)
159 ((c-face-name-p 'font-lock-doc-face)
160 ;; Emacs 21 and later.
161 'font-lock-doc-face)
163 'font-lock-comment-face)))
165 (defconst c-doc-markup-face-name
166 (if (c-face-name-p 'font-lock-doc-markup-face)
167 ;; If it happens to occur in the future. (Well, the more
168 ;; pragmatic reason is to get unique faces for the test
169 ;; suite.)
170 'font-lock-doc-markup-face
171 c-label-face-name))
173 (defconst c-negation-char-face-name
174 (if (c-face-name-p 'font-lock-negation-char-face)
175 ;; Emacs 22 has a special face for negation chars.
176 'font-lock-negation-char-face))
178 (cc-bytecomp-defun face-inverse-video-p) ; Only in Emacs.
180 (defun c-make-inverse-face (oldface newface)
181 ;; Emacs and XEmacs have completely different face manipulation
182 ;; routines. :P
183 (copy-face oldface newface)
184 (cond ((fboundp 'face-inverse-video-p)
185 ;; Emacs. This only looks at the inverse flag in the current
186 ;; frame. Other display configurations might be different,
187 ;; but it can only show if the same Emacs has frames on
188 ;; e.g. a color and a monochrome display simultaneously.
189 (unless (face-inverse-video-p oldface)
190 (invert-face newface)))
191 ((fboundp 'face-property-instance)
192 ;; XEmacs. Same pitfall here.
193 (unless (face-property-instance oldface 'reverse)
194 (invert-face newface)))))
196 (defvar c-annotation-face 'c-annotation-face)
198 (defface c-annotation-face
199 '((default :inherit font-lock-constant-face))
200 "Face for highlighting annotations in Java mode and similar modes."
201 :version "24.1"
202 :group 'c)
204 (eval-and-compile
205 ;; We need the following definitions during compilation since they're
206 ;; used when the `c-lang-defconst' initializers are evaluated. Define
207 ;; them at runtime too for the sake of derived modes.
209 ;; This indicates the "font locking context", and is set just before
210 ;; fontification is done. If non-nil, it says, e.g., point starts
211 ;; from within a #if preprocessor construct.
212 (defvar c-font-lock-context nil)
213 (make-variable-buffer-local 'c-font-lock-context)
215 (defmacro c-put-font-lock-face (from to face)
216 ;; Put a face on a region (overriding any existing face) in the way
217 ;; font-lock would do it. In XEmacs that means putting an
218 ;; additional font-lock property, or else the font-lock package
219 ;; won't recognize it as fontified and might override it
220 ;; incorrectly.
222 ;; This function does a hidden buffer change.
223 (if (fboundp 'font-lock-set-face)
224 ;; Note: This function has no docstring in XEmacs so it might be
225 ;; considered internal.
226 `(font-lock-set-face ,from ,to ,face)
227 `(put-text-property ,from ,to 'face ,face)))
229 (defmacro c-remove-font-lock-face (from to)
230 ;; This is the inverse of `c-put-font-lock-face'.
232 ;; This function does a hidden buffer change.
233 (if (fboundp 'font-lock-remove-face)
234 `(font-lock-remove-face ,from ,to)
235 `(remove-text-properties ,from ,to '(face nil))))
237 (defmacro c-put-font-lock-string-face (from to)
238 ;; Put `font-lock-string-face' on a string. The surrounding
239 ;; quotes are included in Emacs but not in XEmacs. The passed
240 ;; region should include them.
242 ;; This function does a hidden buffer change.
243 (if (featurep 'xemacs)
244 `(c-put-font-lock-face (1+ ,from) (1- ,to) 'font-lock-string-face)
245 `(c-put-font-lock-face ,from ,to 'font-lock-string-face)))
247 (defmacro c-fontify-types-and-refs (varlist &rest body)
248 ;; Like `let', but additionally activates `c-record-type-identifiers'
249 ;; and `c-record-ref-identifiers', and fontifies the recorded ranges
250 ;; accordingly on exit.
252 ;; This function does hidden buffer changes.
253 `(let ((c-record-type-identifiers t)
254 c-record-ref-identifiers
255 ,@varlist)
256 (prog1 (progn ,@body)
257 (c-fontify-recorded-types-and-refs))))
258 (put 'c-fontify-types-and-refs 'lisp-indent-function 1)
260 (defun c-skip-comments-and-strings (limit)
261 ;; If the point is within a region fontified as a comment or
262 ;; string literal skip to the end of it or to LIMIT, whichever
263 ;; comes first, and return t. Otherwise return nil. The match
264 ;; data is not clobbered.
266 ;; This function might do hidden buffer changes.
267 (when (c-got-face-at (point) c-literal-faces)
268 (while (progn
269 (goto-char (c-next-single-property-change
270 (point) 'face nil limit))
271 (and (< (point) limit)
272 (c-got-face-at (point) c-literal-faces))))
275 (defun c-make-syntactic-matcher (regexp)
276 ;; Returns a byte compiled function suitable for use in place of a
277 ;; regexp string in a `font-lock-keywords' matcher, except that
278 ;; only matches outside comments and string literals count.
280 ;; This function does not do any hidden buffer changes, but the
281 ;; generated functions will. (They are however used in places
282 ;; covered by the font-lock context.)
283 (byte-compile
284 `(lambda (limit)
285 (let (res)
286 (while (and (setq res (re-search-forward ,regexp limit t))
287 (progn
288 (goto-char (match-beginning 0))
289 (or (c-skip-comments-and-strings limit)
290 (progn
291 (goto-char (match-end 0))
292 nil)))))
293 res))))
295 (defun c-make-font-lock-search-form (regexp highlights)
296 ;; Return a lisp form which will fontify every occurrence of REGEXP
297 ;; (a regular expression, NOT a function) between POINT and `limit'
298 ;; with HIGHLIGHTS, a list of highlighters as specified on page
299 ;; "Search-based Fontification" in the elisp manual.
300 `(while (re-search-forward ,regexp limit t)
301 (unless (progn
302 (goto-char (match-beginning 0))
303 (c-skip-comments-and-strings limit))
304 (goto-char (match-end 0))
305 ,@(mapcar
306 (lambda (highlight)
307 (if (integerp (car highlight))
308 ;; e.g. highlight is (1 font-lock-type-face t)
309 (progn
310 (unless (eq (nth 2 highlight) t)
311 (error
312 "The override flag must currently be t in %s"
313 highlight))
314 (when (nth 3 highlight)
315 (error
316 "The laxmatch flag may currently not be set in %s"
317 highlight))
318 `(save-match-data
319 (c-put-font-lock-face
320 (match-beginning ,(car highlight))
321 (match-end ,(car highlight))
322 ,(elt highlight 1))))
323 ;; highlight is an "ANCHORED HIGHLIGHTER" of the form
324 ;; (ANCHORED-MATCHER PRE-FORM POST-FORM SUBEXP-HIGHLIGHTERS...)
325 (when (nth 3 highlight)
326 (error "Match highlights currently not supported in %s"
327 highlight))
328 `(progn
329 ,(nth 1 highlight)
330 (save-match-data ,(car highlight))
331 ,(nth 2 highlight))))
332 highlights))))
334 (defun c-make-font-lock-search-function (regexp &rest highlights)
335 ;; This function makes a byte compiled function that works much like
336 ;; a matcher element in `font-lock-keywords'. It cuts out a little
337 ;; bit of the overhead compared to a real matcher. The main reason
338 ;; is however to pass the real search limit to the anchored
339 ;; matcher(s), since most (if not all) font-lock implementations
340 ;; arbitrarily limit anchored matchers to the same line, and also
341 ;; to insulate against various other irritating differences between
342 ;; the different (X)Emacs font-lock packages.
344 ;; REGEXP is the matcher, which must be a regexp. Only matches
345 ;; where the beginning is outside any comment or string literal are
346 ;; significant.
348 ;; HIGHLIGHTS is a list of highlight specs, just like in
349 ;; `font-lock-keywords', with these limitations: The face is always
350 ;; overridden (no big disadvantage, since hits in comments etc are
351 ;; filtered anyway), there is no "laxmatch", and an anchored matcher
352 ;; is always a form which must do all the fontification directly.
353 ;; `limit' is a variable bound to the real limit in the context of
354 ;; the anchored matcher forms.
356 ;; This function does not do any hidden buffer changes, but the
357 ;; generated functions will. (They are however used in places
358 ;; covered by the font-lock context.)
360 ;; Note: Replace `byte-compile' with `eval' to debug the generated
361 ;; lambda more easily.
362 (byte-compile
363 `(lambda (limit)
364 (let ( ;; The font-lock package in Emacs is known to clobber
365 ;; `parse-sexp-lookup-properties' (when it exists).
366 (parse-sexp-lookup-properties
367 (cc-eval-when-compile
368 (boundp 'parse-sexp-lookup-properties))))
369 ,(c-make-font-lock-search-form regexp highlights))
370 nil)))
372 (defun c-make-font-lock-BO-decl-search-function (regexp &rest highlights)
373 ;; This function makes a byte compiled function that first moves back
374 ;; to the beginning of the current declaration (if any), then searches
375 ;; forward for matcher elements (as in `font-lock-keywords') and
376 ;; fontifies them.
378 ;; The motivation for moving back to the declaration start is to
379 ;; establish a context for the current text when, e.g., a character
380 ;; is typed on a C++ inheritance continuation line, or a jit-lock
381 ;; chunk starts there.
383 ;; The new function works much like a matcher element in
384 ;; `font-lock-keywords'. It cuts out a little bit of the overhead
385 ;; compared to a real matcher. The main reason is however to pass the
386 ;; real search limit to the anchored matcher(s), since most (if not
387 ;; all) font-lock implementations arbitrarily limit anchored matchers
388 ;; to the same line, and also to insulate against various other
389 ;; irritating differences between the different (X)Emacs font-lock
390 ;; packages.
392 ;; REGEXP is the matcher, which must be a regexp. Only matches
393 ;; where the beginning is outside any comment or string literal are
394 ;; significant.
396 ;; HIGHLIGHTS is a list of highlight specs, just like in
397 ;; `font-lock-keywords', with these limitations: The face is always
398 ;; overridden (no big disadvantage, since hits in comments etc are
399 ;; filtered anyway), there is no "laxmatch", and an anchored matcher
400 ;; is always a form which must do all the fontification directly.
401 ;; `limit' is a variable bound to the real limit in the context of
402 ;; the anchored matcher forms.
404 ;; This function does not do any hidden buffer changes, but the
405 ;; generated functions will. (They are however used in places
406 ;; covered by the font-lock context.)
408 ;; Note: Replace `byte-compile' with `eval' to debug the generated
409 ;; lambda more easily.
410 (byte-compile
411 `(lambda (limit)
412 (let ( ;; The font-lock package in Emacs is known to clobber
413 ;; `parse-sexp-lookup-properties' (when it exists).
414 (parse-sexp-lookup-properties
415 (cc-eval-when-compile
416 (boundp 'parse-sexp-lookup-properties)))
417 (BOD-limit
418 (c-determine-limit 1000)))
419 (goto-char
420 (let ((here (point)))
421 (if (eq (car (c-beginning-of-decl-1 BOD-limit)) 'same)
422 (point)
423 here)))
424 ,(c-make-font-lock-search-form regexp highlights))
425 nil)))
427 (defun c-make-font-lock-context-search-function (normal &rest state-stanzas)
428 ;; This function makes a byte compiled function that works much like
429 ;; a matcher element in `font-lock-keywords', with the following
430 ;; enhancement: the generated function will test for particular "font
431 ;; lock contexts" at the start of the region, i.e. is this point in
432 ;; the middle of some particular construct? if so the generated
433 ;; function will first fontify the tail of the construct, before
434 ;; going into the main loop and fontify full constructs up to limit.
436 ;; The generated function takes one parameter called `limit', and
437 ;; will fontify the region between POINT and LIMIT.
439 ;; NORMAL is a list of the form (REGEXP HIGHLIGHTS .....), and is
440 ;; used to fontify the "regular" bit of the region.
441 ;; STATE-STANZAS is list of elements of the form (STATE LIM REGEXP
442 ;; HIGHLIGHTS), each element coding one possible font lock context.
444 ;; o - REGEXP is a font-lock regular expression (NOT a function),
445 ;; o - HIGHLIGHTS is a list of zero or more highlighters as defined
446 ;; on page "Search-based Fontification" in the elisp manual. As
447 ;; yet (2009-06), they must have OVERRIDE set, and may not have
448 ;; LAXMATCH set.
450 ;; o - STATE is the "font lock context" (e.g. in-cpp-expr) and is
451 ;; not quoted.
452 ;; o - LIM is a lisp form whose evaluation will yield the limit
453 ;; position in the buffer for fontification by this stanza.
455 ;; This function does not do any hidden buffer changes, but the
456 ;; generated functions will. (They are however used in places
457 ;; covered by the font-lock context.)
459 ;; Note: Replace `byte-compile' with `eval' to debug the generated
460 ;; lambda more easily.
461 (byte-compile
462 `(lambda (limit)
463 (let ( ;; The font-lock package in Emacs is known to clobber
464 ;; `parse-sexp-lookup-properties' (when it exists).
465 (parse-sexp-lookup-properties
466 (cc-eval-when-compile
467 (boundp 'parse-sexp-lookup-properties))))
468 ,@(mapcar
469 (lambda (stanza)
470 (let ((state (car stanza))
471 (lim (nth 1 stanza))
472 (regexp (nth 2 stanza))
473 (highlights (cdr (cddr stanza))))
474 `(if (eq c-font-lock-context ',state)
475 (let ((limit ,lim))
476 ,(c-make-font-lock-search-form
477 regexp highlights)))))
478 state-stanzas)
479 ,(c-make-font-lock-search-form (car normal) (cdr normal))
480 nil))))
482 ; (eval-after-load "edebug" ; 2006-07-09: def-edebug-spec is now in subr.el.
483 ; '(progn
484 (def-edebug-spec c-fontify-types-and-refs let*)
485 (def-edebug-spec c-make-syntactic-matcher t)
486 ;; If there are literal quoted or backquoted highlight specs in
487 ;; the call to `c-make-font-lock-search-function' then let's
488 ;; instrument the forms in them.
489 (def-edebug-spec c-make-font-lock-search-function
490 (form &rest &or ("quote" (&rest form)) ("`" (&rest form)) form)));))
492 (defun c-fontify-recorded-types-and-refs ()
493 ;; Convert the ranges recorded on `c-record-type-identifiers' and
494 ;; `c-record-ref-identifiers' to fontification.
496 ;; This function does hidden buffer changes.
497 (let (elem)
498 (while (consp c-record-type-identifiers)
499 (setq elem (car c-record-type-identifiers)
500 c-record-type-identifiers (cdr c-record-type-identifiers))
501 (c-put-font-lock-face (car elem) (cdr elem)
502 'font-lock-type-face))
503 (while c-record-ref-identifiers
504 (setq elem (car c-record-ref-identifiers)
505 c-record-ref-identifiers (cdr c-record-ref-identifiers))
506 ;; Note that the reference face is a variable that is
507 ;; dereferenced, since it's an alias in Emacs.
508 (c-put-font-lock-face (car elem) (cdr elem)
509 c-reference-face-name))))
511 (c-lang-defconst c-cpp-matchers
512 "Font lock matchers for preprocessor directives and purely lexical
513 stuff. Used on level 1 and higher."
515 ;; Note: `c-font-lock-declarations' assumes that no matcher here
516 ;; sets `font-lock-type-face' in languages where
517 ;; `c-recognize-<>-arglists' is set.
519 t `(,@(when (c-lang-const c-opt-cpp-prefix)
520 (let* ((noncontinued-line-end "\\(\\=\\|\\(\\=\\|[^\\]\\)[\n\r]\\)")
521 (ncle-depth (regexp-opt-depth noncontinued-line-end))
522 (sws-depth (c-lang-const c-syntactic-ws-depth))
523 (nsws-depth (c-lang-const c-nonempty-syntactic-ws-depth)))
525 `(;; The stuff after #error and #warning is a message, so
526 ;; fontify it as a string.
527 ,@(when (c-lang-const c-cpp-message-directives)
528 (let* ((re (c-make-keywords-re 'appendable ; nil
529 (c-lang-const c-cpp-message-directives)))
530 (re-depth (regexp-opt-depth re)))
531 `((,(concat noncontinued-line-end
532 (c-lang-const c-opt-cpp-prefix)
534 "\\s +\\(.*\\)$")
535 ,(+ ncle-depth re-depth 1) font-lock-string-face t))))
537 ;; Fontify filenames in #include <...> as strings.
538 ,@(when (c-lang-const c-cpp-include-directives)
539 (let* ((re (c-make-keywords-re nil
540 (c-lang-const c-cpp-include-directives)))
541 (re-depth (regexp-opt-depth re)))
542 ;; We used to use a font-lock "anchored matcher" here for
543 ;; the paren syntax. This failed when the ">" was at EOL,
544 ;; since `font-lock-fontify-anchored-keywords' terminated
545 ;; its loop at EOL without executing our lambda form at
546 ;; all.
547 `((,(c-make-font-lock-search-function
548 (concat noncontinued-line-end
549 (c-lang-const c-opt-cpp-prefix)
551 (c-lang-const c-syntactic-ws)
552 "\\(<[^>\n\r]*>?\\)")
553 `(,(+ ncle-depth re-depth sws-depth 1)
554 font-lock-string-face t)
555 `((let ((beg (match-beginning
556 ,(+ ncle-depth re-depth sws-depth 1)))
557 (end (1- (match-end ,(+ ncle-depth re-depth
558 sws-depth 1)))))
559 (if (eq (char-after end) ?>)
560 (progn
561 (c-mark-<-as-paren beg)
562 (c-mark->-as-paren end))
563 (c-unmark-<->-as-paren beg)))
564 nil))))))
566 ;; #define.
567 ,@(when (c-lang-const c-opt-cpp-macro-define)
568 `((,(c-make-font-lock-search-function
569 (concat
570 noncontinued-line-end
571 (c-lang-const c-opt-cpp-prefix)
572 (c-lang-const c-opt-cpp-macro-define)
573 (c-lang-const c-nonempty-syntactic-ws)
574 "\\(" (c-lang-const ; 1 + ncle + nsws
575 c-symbol-key) "\\)"
576 (concat "\\(" ; 2 + ncle + nsws + c-sym-key
577 ;; Macro with arguments - a "function".
578 "\\(\(\\)" ; 3 + ncle + nsws + c-sym-key
579 "\\|"
580 ;; Macro without arguments - a "variable".
581 "\\([^\(]\\|$\\)"
582 "\\)"))
583 `((if (match-beginning
584 ,(+ 3 ncle-depth nsws-depth
585 (c-lang-const c-symbol-key-depth)))
587 ;; "Function". Fontify the name and the arguments.
588 (save-restriction
589 (c-put-font-lock-face
590 (match-beginning ,(+ 1 ncle-depth nsws-depth))
591 (match-end ,(+ 1 ncle-depth nsws-depth))
592 'font-lock-function-name-face)
593 (goto-char
594 (match-end
595 ,(+ 3 ncle-depth nsws-depth
596 (c-lang-const c-symbol-key-depth))))
598 (narrow-to-region (point-min) limit)
599 (while (and
600 (progn
601 (c-forward-syntactic-ws)
602 (looking-at c-symbol-key))
603 (progn
604 (c-put-font-lock-face
605 (match-beginning 0) (match-end 0)
606 'font-lock-variable-name-face)
607 (goto-char (match-end 0))
608 (c-forward-syntactic-ws)
609 (eq (char-after) ?,)))
610 (forward-char)))
612 ;; "Variable".
613 (c-put-font-lock-face
614 (match-beginning ,(+ 1 ncle-depth nsws-depth))
615 (match-end ,(+ 1 ncle-depth nsws-depth))
616 'font-lock-variable-name-face)))))))
618 ;; Fontify cpp function names in preprocessor
619 ;; expressions in #if and #elif.
620 ,@(when (and (c-lang-const c-cpp-expr-directives)
621 (c-lang-const c-cpp-expr-functions))
622 (let ((ced-re (c-make-keywords-re t
623 (c-lang-const c-cpp-expr-directives)))
624 (cef-re (c-make-keywords-re t
625 (c-lang-const c-cpp-expr-functions))))
627 `((,(c-make-font-lock-context-search-function
628 `(,(concat noncontinued-line-end
629 (c-lang-const c-opt-cpp-prefix)
630 ced-re ; 1 + ncle-depth
631 ;; Match the whole logical line to look
632 ;; for the functions in.
633 "\\(\\\\\\(.\\|[\n\r]\\)\\|[^\n\r]\\)*")
634 ((let ((limit (match-end 0)))
635 (while (re-search-forward ,cef-re limit 'move)
636 (c-put-font-lock-face (match-beginning 1)
637 (match-end 1)
638 c-preprocessor-face-name)))
639 (goto-char (match-end ,(1+ ncle-depth)))))
640 `(in-cpp-expr
641 (save-excursion (c-end-of-macro) (point))
642 ,cef-re
643 (1 c-preprocessor-face-name t)))))))
645 ;; Fontify the directive names.
646 (,(c-make-font-lock-search-function
647 (concat noncontinued-line-end
648 "\\("
649 (c-lang-const c-opt-cpp-prefix)
650 "[" (c-lang-const c-symbol-chars) "]+"
651 "\\)")
652 `(,(1+ ncle-depth) c-preprocessor-face-name t)))
654 (eval . (list ,(c-make-syntactic-matcher
655 (concat noncontinued-line-end
656 (c-lang-const c-opt-cpp-prefix)
657 "if\\(n\\)def\\>"))
658 ,(+ ncle-depth 1)
659 c-negation-char-face-name
660 'append))
663 ,@(when (c-major-mode-is 'pike-mode)
664 ;; Recognize hashbangs in Pike.
665 `((eval . (list "\\`#![^\n\r]*"
666 0 c-preprocessor-face-name))))
668 ;; Make hard spaces visible through an inverted `font-lock-warning-face'.
669 (eval . (list
670 "\240"
671 0 (progn
672 (unless (c-face-name-p 'c-nonbreakable-space-face)
673 (c-make-inverse-face 'font-lock-warning-face
674 'c-nonbreakable-space-face))
675 ''c-nonbreakable-space-face)))
678 (defun c-font-lock-invalid-string ()
679 ;; Assuming the point is after the opening character of a string,
680 ;; fontify that char with `font-lock-warning-face' if the string
681 ;; decidedly isn't terminated properly.
683 ;; This function does hidden buffer changes.
684 (let ((start (1- (point))))
685 (save-excursion
686 (and (eq (elt (parse-partial-sexp start (c-point 'eol)) 8) start)
687 (if (if (eval-when-compile (integerp ?c))
688 ;; Emacs
689 (integerp c-multiline-string-start-char)
690 ;; XEmacs
691 (characterp c-multiline-string-start-char))
692 ;; There's no multiline string start char before the
693 ;; string, so newlines aren't allowed.
694 (not (eq (char-before start) c-multiline-string-start-char))
695 ;; Multiline strings are allowed anywhere if
696 ;; c-multiline-string-start-char is t.
697 (not c-multiline-string-start-char))
698 (if c-string-escaped-newlines
699 ;; There's no \ before the newline.
700 (not (eq (char-before (point)) ?\\))
701 ;; Escaped newlines aren't supported.
703 (c-put-font-lock-face start (1+ start) 'font-lock-warning-face)))))
705 (c-lang-defconst c-basic-matchers-before
706 "Font lock matchers for basic keywords, labels, references and various
707 other easily recognizable things that should be fontified before generic
708 casts and declarations are fontified. Used on level 2 and higher."
710 ;; Note: `c-font-lock-declarations' assumes that no matcher here
711 ;; sets `font-lock-type-face' in languages where
712 ;; `c-recognize-<>-arglists' is set.
714 t `(;; Put a warning face on the opener of unclosed strings that
715 ;; can't span lines. Later font
716 ;; lock packages have a `font-lock-syntactic-face-function' for
717 ;; this, but it doesn't give the control we want since any
718 ;; fontification done inside the function will be
719 ;; unconditionally overridden.
720 ,(c-make-font-lock-search-function
721 ;; Match a char before the string starter to make
722 ;; `c-skip-comments-and-strings' work correctly.
723 (concat ".\\(" c-string-limit-regexp "\\)")
724 '((c-font-lock-invalid-string)))
726 ;; Fontify keyword constants.
727 ,@(when (c-lang-const c-constant-kwds)
728 (let ((re (c-make-keywords-re nil (c-lang-const c-constant-kwds))))
729 (if (c-major-mode-is 'pike-mode)
730 ;; No symbol is a keyword after "->" in Pike.
731 `((eval . (list ,(concat "\\(\\=.?\\|[^>]\\|[^-]>\\)"
732 "\\<\\(" re "\\)\\>")
733 2 c-constant-face-name)))
734 `((eval . (list ,(concat "\\<\\(" re "\\)\\>")
735 1 c-constant-face-name))))))
737 ;; Fontify all keywords except the primitive types.
738 ,(if (c-major-mode-is 'pike-mode)
739 ;; No symbol is a keyword after "->" in Pike.
740 `(,(concat "\\(\\=.?\\|[^>]\\|[^-]>\\)"
741 "\\<" (c-lang-const c-regular-keywords-regexp))
742 2 font-lock-keyword-face)
743 `(,(concat "\\<" (c-lang-const c-regular-keywords-regexp))
744 1 font-lock-keyword-face))
746 ;; Fontify leading identifiers in fully qualified names like
747 ;; "foo::bar" in languages that supports such things.
748 ,@(when (c-lang-const c-opt-identifier-concat-key)
749 (if (c-major-mode-is 'java-mode)
750 ;; Java needs special treatment since "." is used both to
751 ;; qualify names and in normal indexing. Here we look for
752 ;; capital characters at the beginning of an identifier to
753 ;; recognize the class. "*" is also recognized to cover
754 ;; wildcard import declarations. All preceding dot separated
755 ;; identifiers are taken as package names and therefore
756 ;; fontified as references.
757 `(,(c-make-font-lock-search-function
758 ;; Search for class identifiers preceded by ".". The
759 ;; anchored matcher takes it from there.
760 (concat (c-lang-const c-opt-identifier-concat-key)
761 (c-lang-const c-simple-ws) "*"
762 (concat "\\("
763 "[" c-upper "]"
764 "[" (c-lang-const c-symbol-chars) "]*"
765 "\\|"
766 "\\*"
767 "\\)"))
768 `((let (id-end)
769 (goto-char (1+ (match-beginning 0)))
770 (while (and (eq (char-before) ?.)
771 (progn
772 (backward-char)
773 (c-backward-syntactic-ws)
774 (setq id-end (point))
775 (< (skip-chars-backward
776 ,(c-lang-const c-symbol-chars)) 0))
777 (not (get-text-property (point) 'face)))
778 (c-put-font-lock-face (point) id-end
779 c-reference-face-name)
780 (c-backward-syntactic-ws)))
782 (goto-char (match-end 0)))))
784 `((,(byte-compile
785 ;; Must use a function here since we match longer than
786 ;; we want to move before doing a new search. This is
787 ;; not necessary for XEmacs since it restarts the
788 ;; search from the end of the first highlighted
789 ;; submatch (something that causes problems in other
790 ;; places).
791 `(lambda (limit)
792 (while (re-search-forward
793 ,(concat "\\(\\<" ; 1
794 "\\(" (c-lang-const c-symbol-key) "\\)" ; 2
795 (c-lang-const c-simple-ws) "*"
796 (c-lang-const c-opt-identifier-concat-key)
797 (c-lang-const c-simple-ws) "*"
798 "\\)"
799 "\\("
800 (c-lang-const c-opt-after-id-concat-key)
801 "\\)")
802 limit t)
803 (unless (progn
804 (goto-char (match-beginning 0))
805 (c-skip-comments-and-strings limit))
806 (or (get-text-property (match-beginning 2) 'face)
807 (c-put-font-lock-face (match-beginning 2)
808 (match-end 2)
809 c-reference-face-name))
810 (goto-char (match-end 1))))))))))
812 ;; Fontify the special declarations in Objective-C.
813 ,@(when (c-major-mode-is 'objc-mode)
814 `(;; Fontify class names in the beginning of message expressions.
815 ,(c-make-font-lock-search-function
816 "\\["
817 '((c-fontify-types-and-refs ()
818 (c-forward-syntactic-ws limit)
819 (let ((start (point)))
820 ;; In this case we accept both primitive and known types.
821 (when (eq (c-forward-type) 'known)
822 (goto-char start)
823 (let ((c-promote-possible-types t))
824 (c-forward-type))))
825 (if (> (point) limit) (goto-char limit)))))
827 ;; The @interface/@implementation/@protocol directives.
828 ,(c-make-font-lock-search-function
829 (concat "\\<"
830 (regexp-opt
831 '("@interface" "@implementation" "@protocol")
833 "\\>")
834 '((c-fontify-types-and-refs
835 (;; The font-lock package in Emacs is known to clobber
836 ;; `parse-sexp-lookup-properties' (when it exists).
837 (parse-sexp-lookup-properties
838 (cc-eval-when-compile
839 (boundp 'parse-sexp-lookup-properties))))
840 (c-forward-objc-directive)
841 nil)
842 (goto-char (match-beginning 0))))))
844 (eval . (list "\\(!\\)[^=]" 1 c-negation-char-face-name))
847 (defun c-font-lock-complex-decl-prepare (limit)
848 ;; This function will be called from font-lock for a region bounded by POINT
849 ;; and LIMIT, as though it were to identify a keyword for
850 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
851 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
852 ;; Fontification".
854 ;; Called before any of the matchers in `c-complex-decl-matchers'.
856 ;; This function does hidden buffer changes.
858 ;;(message "c-font-lock-complex-decl-prepare %s %s" (point) limit)
860 ;; Clear the list of found types if we start from the start of the
861 ;; buffer, to make it easier to get rid of misspelled types and
862 ;; variables that have gotten recognized as types in malformed code.
863 (when (bobp)
864 (c-clear-found-types))
866 ;; Clear the c-type char properties which mark the region, to recalculate
867 ;; them properly. The most interesting properties are those put on the
868 ;; closest token before the region.
869 (save-excursion
870 (let ((pos (point)))
871 (c-backward-syntactic-ws)
872 (c-clear-char-properties
873 (if (and (not (bobp))
874 (memq (c-get-char-property (1- (point)) 'c-type)
875 '(c-decl-arg-start
876 c-decl-end
877 c-decl-id-start
878 c-decl-type-start)))
879 (1- (point))
880 pos)
881 limit 'c-type)))
883 ;; Update `c-state-cache' to the beginning of the region. This will
884 ;; make `c-beginning-of-syntax' go faster when it's used later on,
885 ;; and it's near the point most of the time.
886 (c-parse-state)
888 ;; Check if the fontified region starts inside a declarator list so
889 ;; that `c-font-lock-declarators' should be called at the start.
890 ;; The declared identifiers are font-locked correctly as types, if
891 ;; that is what they are.
892 (let ((prop (save-excursion
893 (c-backward-syntactic-ws)
894 (unless (bobp)
895 (c-get-char-property (1- (point)) 'c-type)))))
896 (when (memq prop '(c-decl-id-start c-decl-type-start))
897 (c-forward-syntactic-ws limit)
898 (c-font-lock-declarators limit t (eq prop 'c-decl-type-start))))
900 (setq c-font-lock-context ;; (c-guess-font-lock-context)
901 (save-excursion
902 (if (and c-cpp-expr-intro-re
903 (c-beginning-of-macro)
904 (looking-at c-cpp-expr-intro-re))
905 'in-cpp-expr)))
906 nil)
908 (defun c-font-lock-<>-arglists (limit)
909 ;; This function will be called from font-lock for a region bounded by POINT
910 ;; and LIMIT, as though it were to identify a keyword for
911 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
912 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
913 ;; Fontification".
915 ;; Fontify types and references in names containing angle bracket
916 ;; arglists from the point to LIMIT. Note that
917 ;; `c-font-lock-declarations' already has handled many of them.
919 ;; This function might do hidden buffer changes.
921 (let (;; The font-lock package in Emacs is known to clobber
922 ;; `parse-sexp-lookup-properties' (when it exists).
923 (parse-sexp-lookup-properties
924 (cc-eval-when-compile
925 (boundp 'parse-sexp-lookup-properties)))
926 (c-parse-and-markup-<>-arglists t)
927 c-restricted-<>-arglists
928 id-start id-end id-face pos kwd-sym)
930 (while (and (< (point) limit)
931 (re-search-forward c-opt-<>-arglist-start limit t))
933 (setq id-start (match-beginning 1)
934 id-end (match-end 1)
935 pos (point))
937 (goto-char id-start)
938 (unless (c-skip-comments-and-strings limit)
939 (setq kwd-sym nil
940 c-restricted-<>-arglists nil
941 id-face (get-text-property id-start 'face))
943 (if (cond
944 ((eq id-face 'font-lock-type-face)
945 ;; The identifier got the type face so it has already been
946 ;; handled in `c-font-lock-declarations'.
947 nil)
949 ((eq id-face 'font-lock-keyword-face)
950 (when (looking-at c-opt-<>-sexp-key)
951 ;; There's a special keyword before the "<" that tells
952 ;; that it's an angle bracket arglist.
953 (setq kwd-sym (c-keyword-sym (match-string 1)))))
956 ;; There's a normal identifier before the "<". If we're not in
957 ;; a declaration context then we set `c-restricted-<>-arglists'
958 ;; to avoid recognizing templates in function calls like "foo (a
959 ;; < b, c > d)".
960 (c-backward-syntactic-ws)
961 (when (and (memq (char-before) '(?\( ?,))
962 (not (eq (get-text-property (1- (point)) 'c-type)
963 'c-decl-arg-start)))
964 (setq c-restricted-<>-arglists t))
967 (progn
968 (goto-char (1- pos))
969 ;; Check for comment/string both at the identifier and
970 ;; at the "<".
971 (unless (c-skip-comments-and-strings limit)
973 (c-fontify-types-and-refs ()
974 (when (c-forward-<>-arglist (c-keyword-member
975 kwd-sym 'c-<>-type-kwds))
976 (when (and c-opt-identifier-concat-key
977 (not (get-text-property id-start 'face)))
978 (c-forward-syntactic-ws)
979 (if (looking-at c-opt-identifier-concat-key)
980 (c-put-font-lock-face id-start id-end
981 c-reference-face-name)
982 (c-put-font-lock-face id-start id-end
983 'font-lock-type-face)))))
985 (goto-char pos)))
986 (goto-char pos)))))
987 nil)
989 (defun c-font-lock-declarators (limit list types)
990 ;; Assuming the point is at the start of a declarator in a declaration,
991 ;; fontify the identifier it declares. (If TYPES is set, it does this via
992 ;; the macro `c-fontify-types-and-refs'.)
994 ;; If LIST is non-nil, also fontify the ids in any following declarators in
995 ;; a comma separated list (e.g. "foo" and "*bar" in "int foo = 17, *bar;");
996 ;; additionally, mark the commas with c-type property 'c-decl-id-start or
997 ;; 'c-decl-type-start (according to TYPES). Stop at LIMIT.
999 ;; If TYPES is non-nil, fontify all identifiers as types.
1001 ;; Nil is always returned. The function leaves point at the delimiter after
1002 ;; the last declarator it processes.
1004 ;; This function might do hidden buffer changes.
1006 ;;(message "c-font-lock-declarators from %s to %s" (point) limit)
1007 (c-fontify-types-and-refs
1008 ((pos (point)) next-pos id-start id-end
1009 paren-depth
1010 id-face got-init
1011 c-last-identifier-range
1012 (separator-prop (if types 'c-decl-type-start 'c-decl-id-start))
1013 brackets-after-id)
1015 ;; The following `while' fontifies a single declarator id each time round.
1016 ;; It loops only when LIST is non-nil.
1017 (while
1018 ;; Inside the following "condition form", we move forward over the
1019 ;; declarator's identifier up as far as any opening bracket (for array
1020 ;; size) or paren (for parameters of function-type) or brace (for
1021 ;; array/struct initialization) or "=" or terminating delimiter
1022 ;; (e.g. "," or ";" or "}").
1023 (and
1025 (< (point) limit)
1027 ;; The following form moves forward over the declarator's
1028 ;; identifier (and what precedes it), returning t. If there
1029 ;; wasn't one, it returns nil, terminating the `while'.
1030 (let (got-identifier)
1031 (setq paren-depth 0)
1032 ;; Skip over type decl prefix operators, one for each iteration
1033 ;; of the while. These are, e.g. "*" in "int *foo" or "(" and
1034 ;; "*" in "int (*foo) (void)" (Note similar code in
1035 ;; `c-forward-decl-or-cast-1'.)
1036 (while (and (looking-at c-type-decl-prefix-key)
1037 (if (and (c-major-mode-is 'c++-mode)
1038 (match-beginning 3))
1039 ;; If the third submatch matches in C++ then
1040 ;; we're looking at an identifier that's a
1041 ;; prefix only if it specifies a member pointer.
1042 (progn
1043 (setq id-start (point))
1044 (c-forward-name)
1045 (if (looking-at "\\(::\\)")
1046 ;; We only check for a trailing "::" and
1047 ;; let the "*" that should follow be
1048 ;; matched in the next round.
1050 ;; It turned out to be the real identifier,
1051 ;; so flag that and stop.
1052 (setq got-identifier t)
1053 nil))
1055 (if (eq (char-after) ?\()
1056 (progn
1057 (setq paren-depth (1+ paren-depth))
1058 (forward-char))
1059 (goto-char (match-end 1)))
1060 (c-forward-syntactic-ws))
1062 ;; If we haven't passed the identifier already, do it now.
1063 (unless got-identifier
1064 (setq id-start (point))
1065 (c-forward-name))
1066 (setq id-end (point))
1068 (/= id-end pos))
1070 ;; Skip out of the parens surrounding the identifier. If closing
1071 ;; parens are missing, this form returns nil.
1072 (or (= paren-depth 0)
1073 (c-safe (goto-char (scan-lists (point) 1 paren-depth))))
1075 (<= (point) limit)
1077 ;; Skip over any trailing bit, such as "__attribute__".
1078 (progn
1079 (when (looking-at c-decl-hangon-key)
1080 (c-forward-keyword-clause 1))
1081 (<= (point) limit))
1083 ;; Search syntactically to the end of the declarator (";",
1084 ;; ",", a closing paren, eob etc) or to the beginning of an
1085 ;; initializer or function prototype ("=" or "\\s\(").
1086 ;; Note that square brackets are now not also treated as
1087 ;; initializers, since this broke when there were also
1088 ;; initializing brace lists.
1089 (let (found)
1090 (while
1091 (and (setq found (c-syntactic-re-search-forward
1092 "[;,]\\|\\s)\\|\\'\\|\\(=\\|\\s(\\)" limit t t))
1093 (eq (char-before) ?\[)
1094 (c-go-up-list-forward))
1095 (setq brackets-after-id t))
1096 found))
1098 (setq next-pos (match-beginning 0)
1099 id-face (if (and (eq (char-after next-pos) ?\()
1100 (not brackets-after-id))
1101 'font-lock-function-name-face
1102 'font-lock-variable-name-face)
1103 got-init (and (match-beginning 1)
1104 (char-after (match-beginning 1))))
1106 (if types
1107 ;; Register and fontify the identifier as a type.
1108 (let ((c-promote-possible-types t))
1109 (goto-char id-start)
1110 (c-forward-type))
1111 ;; Fontify the last symbol in the identifier if it isn't fontified
1112 ;; already. The check is necessary only in certain cases where this
1113 ;; function is used "sloppily", e.g. in `c-simple-decl-matchers'.
1114 (when (and c-last-identifier-range
1115 (not (get-text-property (car c-last-identifier-range)
1116 'face)))
1117 (c-put-font-lock-face (car c-last-identifier-range)
1118 (cdr c-last-identifier-range)
1119 id-face)))
1121 (goto-char next-pos)
1122 (setq pos nil) ; So as to terminate the enclosing `while' form.
1123 (when list
1124 ;; Jump past any initializer or function prototype to see if
1125 ;; there's a ',' to continue at.
1126 (cond ((eq id-face 'font-lock-function-name-face)
1127 ;; Skip a parenthesized initializer (C++) or a function
1128 ;; prototype.
1129 (if (c-safe (c-forward-sexp 1) t) ; over the parameter list.
1130 (c-forward-syntactic-ws limit)
1131 (goto-char limit))) ; unbalanced parens
1133 (got-init ; "=" sign OR opening "(", "[", or "{"
1134 ;; Skip an initializer expression. If we're at a '='
1135 ;; then accept a brace list directly after it to cope
1136 ;; with array initializers. Otherwise stop at braces
1137 ;; to avoid going past full function and class blocks.
1138 (and (if (and (eq got-init ?=)
1139 (= (c-forward-token-2 1 nil limit) 0)
1140 (looking-at "{"))
1141 (c-safe (c-forward-sexp) t) ; over { .... }
1143 ;; FIXME: Should look for c-decl-end markers here;
1144 ;; we might go far into the following declarations
1145 ;; in e.g. ObjC mode (see e.g. methods-4.m).
1146 (c-syntactic-re-search-forward "[;,{]" limit 'move t)
1147 (backward-char)))
1149 (t (c-forward-syntactic-ws limit)))
1151 ;; If a ',' is found we set pos to the next declarator and iterate.
1152 (when (and (< (point) limit) (looking-at ","))
1153 (c-put-char-property (point) 'c-type separator-prop)
1154 (forward-char)
1155 (c-forward-syntactic-ws limit)
1156 (setq pos (point)))))) ; acts to make the `while' form continue.
1157 nil)
1159 (defconst c-font-lock-maybe-decl-faces
1160 ;; List of faces that might be put at the start of a type when
1161 ;; `c-font-lock-declarations' runs. This needs to be evaluated to
1162 ;; ensure that face name aliases in Emacs are resolved.
1163 (list nil
1164 font-lock-type-face
1165 c-reference-face-name
1166 font-lock-keyword-face))
1168 (defun c-font-lock-declarations (limit)
1169 ;; Fontify all the declarations, casts and labels from the point to LIMIT.
1170 ;; Assumes that strings and comments have been fontified already.
1172 ;; This function will be called from font-lock for a region bounded by POINT
1173 ;; and LIMIT, as though it were to identify a keyword for
1174 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
1175 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
1176 ;; Fontification".
1178 ;; This function might do hidden buffer changes.
1180 ;;(message "c-font-lock-declarations search from %s to %s" (point) limit)
1182 (save-restriction
1183 (let (;; The position where `c-find-decl-spots' last stopped.
1184 start-pos
1185 ;; o - 'decl if we're in an arglist containing declarations
1186 ;; (but if `c-recognize-paren-inits' is set it might also be
1187 ;; an initializer arglist);
1188 ;; o - '<> if the arglist is of angle bracket type;
1189 ;; o - 'arglist if it's some other arglist;
1190 ;; o - nil, if not in an arglist at all. This includes the
1191 ;; parenthesized condition which follows "if", "while", etc.
1192 context
1193 ;; A list of starting positions of possible type declarations, or of
1194 ;; the typedef preceding one, if any.
1195 last-cast-end
1196 ;; The result from `c-forward-decl-or-cast-1'.
1197 decl-or-cast
1198 ;; The maximum of the end positions of all the checked type
1199 ;; decl expressions in the successfully identified
1200 ;; declarations. The position might be either before or
1201 ;; after the syntactic whitespace following the last token
1202 ;; in the type decl expression.
1203 (max-type-decl-end 0)
1204 ;; Same as `max-type-decl-*', but used when we're before
1205 ;; `token-pos'.
1206 (max-type-decl-end-before-token 0)
1207 ;; Set according to the context to direct the heuristics for
1208 ;; recognizing C++ templates.
1209 c-restricted-<>-arglists
1210 ;; Turn on recording of identifier ranges in
1211 ;; `c-forward-decl-or-cast-1' and `c-forward-label' for
1212 ;; later fontification.
1213 (c-record-type-identifiers t)
1214 label-type
1215 c-record-ref-identifiers
1216 ;; Make `c-forward-type' calls mark up template arglists if
1217 ;; it finds any. That's necessary so that we later will
1218 ;; stop inside them to fontify types there.
1219 (c-parse-and-markup-<>-arglists t)
1220 lbrace ; position of some {.
1221 ;; The font-lock package in Emacs is known to clobber
1222 ;; `parse-sexp-lookup-properties' (when it exists).
1223 (parse-sexp-lookup-properties
1224 (cc-eval-when-compile
1225 (boundp 'parse-sexp-lookup-properties))))
1227 ;; Below we fontify a whole declaration even when it crosses the limit,
1228 ;; to avoid gaps when jit/lazy-lock fontifies the file a block at a
1229 ;; time. That is however annoying during editing, e.g. the following is
1230 ;; a common situation while the first line is being written:
1232 ;; my_variable
1233 ;; some_other_variable = 0;
1235 ;; font-lock will put the limit at the beginning of the second line
1236 ;; here, and if we go past it we'll fontify "my_variable" as a type and
1237 ;; "some_other_variable" as an identifier, and the latter will not
1238 ;; correct itself until the second line is changed. To avoid that we
1239 ;; narrow to the limit if the region to fontify is a single line.
1240 (if (<= limit (c-point 'bonl))
1241 (narrow-to-region
1242 (point-min)
1243 (save-excursion
1244 ;; Narrow after any operator chars following the limit though,
1245 ;; since those characters can be useful in recognizing a
1246 ;; declaration (in particular the '{' that opens a function body
1247 ;; after the header).
1248 (goto-char limit)
1249 (skip-chars-forward c-nonsymbol-chars)
1250 (point))))
1252 (c-find-decl-spots
1253 limit
1254 c-decl-start-re
1255 c-font-lock-maybe-decl-faces
1257 (lambda (match-pos inside-macro)
1258 ;; Note to maintainers: don't use `limit' inside this lambda form;
1259 ;; c-find-decl-spots sometimes narrows to less than `limit'.
1260 (setq start-pos (point))
1261 (when
1262 ;; The result of the form below is true when we don't recognize a
1263 ;; declaration or cast.
1264 (if (or (and (eq (get-text-property (point) 'face)
1265 'font-lock-keyword-face)
1266 (looking-at c-not-decl-init-keywords))
1267 (and c-macro-with-semi-re
1268 (looking-at c-macro-with-semi-re))) ; 2008-11-04
1269 ;; Don't do anything more if we're looking at a keyword that
1270 ;; can't start a declaration.
1273 ;; Set `context' and `c-restricted-<>-arglists'. Look for
1274 ;; "<" for the sake of C++-style template arglists.
1275 ;; Ignore "(" when it's part of a control flow construct
1276 ;; (e.g. "for (").
1277 (let ((type (and (> match-pos (point-min))
1278 (c-get-char-property (1- match-pos) 'c-type))))
1279 (cond ((not (memq (char-before match-pos) '(?\( ?, ?\[ ?<)))
1280 (setq context nil
1281 c-restricted-<>-arglists nil))
1282 ;; A control flow expression or a decltype
1283 ((and (eq (char-before match-pos) ?\()
1284 (save-excursion
1285 (goto-char match-pos)
1286 (backward-char)
1287 (c-backward-token-2)
1288 (or (looking-at c-block-stmt-2-key)
1289 (looking-at c-block-stmt-1-2-key)
1290 (looking-at c-typeof-key))))
1291 (setq context nil
1292 c-restricted-<>-arglists t))
1293 ;; Near BOB.
1294 ((<= match-pos (point-min))
1295 (setq context 'arglist
1296 c-restricted-<>-arglists t))
1297 ;; Got a cached hit in a declaration arglist.
1298 ((eq type 'c-decl-arg-start)
1299 (setq context 'decl
1300 c-restricted-<>-arglists nil))
1301 ;; Inside an angle bracket arglist.
1302 ((or (eq type 'c-<>-arg-sep)
1303 (eq (char-before match-pos) ?<))
1304 (setq context '<>
1305 c-restricted-<>-arglists nil))
1306 ;; Got a cached hit in some other type of arglist.
1307 (type
1308 (setq context 'arglist
1309 c-restricted-<>-arglists t))
1310 ((if inside-macro
1311 (< match-pos max-type-decl-end-before-token)
1312 (< match-pos max-type-decl-end))
1313 ;; The point is within the range of a previously
1314 ;; encountered type decl expression, so the arglist
1315 ;; is probably one that contains declarations.
1316 ;; However, if `c-recognize-paren-inits' is set it
1317 ;; might also be an initializer arglist.
1318 (setq context 'decl
1319 c-restricted-<>-arglists nil)
1320 ;; The result of this check is cached with a char
1321 ;; property on the match token, so that we can look
1322 ;; it up again when refontifying single lines in a
1323 ;; multiline declaration.
1324 (c-put-char-property (1- match-pos)
1325 'c-type 'c-decl-arg-start))
1326 (t (setq context 'arglist
1327 c-restricted-<>-arglists t))))
1329 ;; Check we haven't missed a preceding "typedef".
1330 (when (not (looking-at c-typedef-key))
1331 (c-backward-syntactic-ws)
1332 (c-backward-token-2)
1333 (or (looking-at c-typedef-key)
1334 (goto-char start-pos)))
1336 ;; In QT, "more" is an irritating keyword that expands to nothing.
1337 ;; We skip over it to prevent recognition of "more slots: <symbol>"
1338 ;; as a bitfield declaration.
1339 (when (and (c-major-mode-is 'c++-mode)
1340 (looking-at
1341 (concat "\\(more\\)\\([^" c-symbol-chars "]\\|$\\)")))
1342 (goto-char (match-end 1))
1343 (c-forward-syntactic-ws))
1345 ;; Now analyze the construct.
1346 (setq decl-or-cast (c-forward-decl-or-cast-1
1347 match-pos context last-cast-end))
1349 (cond
1350 ((eq decl-or-cast 'cast)
1351 ;; Save the position after the previous cast so we can feed
1352 ;; it to `c-forward-decl-or-cast-1' in the next round. That
1353 ;; helps it discover cast chains like "(a) (b) c".
1354 (setq last-cast-end (point))
1355 (c-fontify-recorded-types-and-refs)
1356 nil)
1358 (decl-or-cast
1359 ;; We've found a declaration.
1361 ;; Set `max-type-decl-end' or `max-type-decl-end-before-token'
1362 ;; under the assumption that we're after the first type decl
1363 ;; expression in the declaration now. That's not really true;
1364 ;; we could also be after a parenthesized initializer
1365 ;; expression in C++, but this is only used as a last resort
1366 ;; to slant ambiguous expression/declarations, and overall
1367 ;; it's worth the risk to occasionally fontify an expression
1368 ;; as a declaration in an initializer expression compared to
1369 ;; getting ambiguous things in normal function prototypes
1370 ;; fontified as expressions.
1371 (if inside-macro
1372 (when (> (point) max-type-decl-end-before-token)
1373 (setq max-type-decl-end-before-token (point)))
1374 (when (> (point) max-type-decl-end)
1375 (setq max-type-decl-end (point))))
1377 ;; Back up to the type to fontify the declarator(s).
1378 (goto-char (car decl-or-cast))
1380 (let ((decl-list
1381 (if context
1382 ;; Should normally not fontify a list of
1383 ;; declarators inside an arglist, but the first
1384 ;; argument in the ';' separated list of a "for"
1385 ;; statement is an exception.
1386 (when (eq (char-before match-pos) ?\()
1387 (save-excursion
1388 (goto-char (1- match-pos))
1389 (c-backward-syntactic-ws)
1390 (and (c-simple-skip-symbol-backward)
1391 (looking-at c-paren-stmt-key))))
1392 t)))
1394 ;; Fix the `c-decl-id-start' or `c-decl-type-start' property
1395 ;; before the first declarator if it's a list.
1396 ;; `c-font-lock-declarators' handles the rest.
1397 (when decl-list
1398 (save-excursion
1399 (c-backward-syntactic-ws)
1400 (unless (bobp)
1401 (c-put-char-property (1- (point)) 'c-type
1402 (if (cdr decl-or-cast)
1403 'c-decl-type-start
1404 'c-decl-id-start)))))
1406 (c-font-lock-declarators
1407 (point-max) decl-list (cdr decl-or-cast)))
1409 ;; A declaration has been successfully identified, so do all the
1410 ;; fontification of types and refs that've been recorded.
1411 (c-fontify-recorded-types-and-refs)
1412 nil)
1414 ;; Restore point, since at this point in the code it has been
1415 ;; left undefined by c-forward-decl-or-cast-1 above.
1416 ((progn (goto-char start-pos) nil))
1418 ;; If point is inside a bracelist, there's no point checking it
1419 ;; being at a declarator.
1420 ((let ((paren-state (c-parse-state)))
1421 (setq lbrace (c-cheap-inside-bracelist-p paren-state)))
1422 ;; Move past this bracelist to prevent an endless loop.
1423 (goto-char lbrace)
1424 (unless (c-safe (progn (forward-list) t))
1425 (goto-char start-pos)
1426 (c-forward-token-2))
1427 nil)
1429 ;; If point is just after a ")" which is followed by an
1430 ;; identifier which isn't a label, or at the matching "(", we're
1431 ;; at either a macro invocation, a cast, or a
1432 ;; for/while/etc. statement. The cast case is handled above.
1433 ;; None of these cases can contain a declarator.
1434 ((or (and (eq (char-before match-pos) ?\))
1435 (c-on-identifier)
1436 (save-excursion (not (c-forward-label))))
1437 (and (eq (char-after) ?\()
1438 (save-excursion
1439 (and
1440 (progn (c-backward-token-2) (c-on-identifier))
1441 (save-excursion (not (c-forward-label)))
1442 (progn (c-backward-token-2)
1443 (eq (char-after) ?\())))))
1444 (c-forward-token-2) ; Must prevent looping.
1445 nil)
1447 ((and (not c-enums-contain-decls)
1448 ;; An optimization quickly to eliminate scans of long enum
1449 ;; declarations in the next cond arm.
1450 (let ((paren-state (c-parse-state)))
1451 (and
1452 (numberp (car paren-state))
1453 (save-excursion
1454 (goto-char (car paren-state))
1455 (c-backward-over-enum-header)))))
1456 (c-forward-token-2)
1457 nil)
1460 ;; Are we at a declarator? Try to go back to the declaration
1461 ;; to check this. If we get there, check whether a "typedef"
1462 ;; is there, then fontify the declarators accordingly.
1463 (let ((decl-search-lim (c-determine-limit 1000))
1464 paren-state bod-res encl-pos is-typedef
1465 c-recognize-knr-p) ; Strictly speaking, bogus, but it
1466 ; speeds up lisp.h tremendously.
1467 (save-excursion
1468 (if (c-back-over-member-initializers)
1469 t ; Can't be at a declarator
1470 (unless (or (eobp)
1471 (looking-at "\\s(\\|\\s)"))
1472 (forward-char))
1473 (setq bod-res (car (c-beginning-of-decl-1 decl-search-lim)))
1474 (if (and (eq bod-res 'same)
1475 (save-excursion
1476 (c-backward-syntactic-ws)
1477 (eq (char-before) ?\})))
1478 (c-beginning-of-decl-1 decl-search-lim))
1480 ;; We're now putatively at the declaration.
1481 (setq paren-state (c-parse-state))
1482 ;; At top level or inside a "{"?
1483 (if (or (not (setq encl-pos
1484 (c-most-enclosing-brace paren-state)))
1485 (eq (char-after encl-pos) ?\{))
1486 (progn
1487 (when (looking-at c-typedef-key) ; "typedef"
1488 (setq is-typedef t)
1489 (goto-char (match-end 0))
1490 (c-forward-syntactic-ws))
1491 ;; At a real declaration?
1492 (if (memq (c-forward-type t) '(t known found decltype))
1493 (progn
1494 (c-font-lock-declarators (point-max) t is-typedef)
1495 nil)
1496 ;; False alarm. Return t to go on to the next check.
1497 (goto-char start-pos)
1499 t)))))))
1501 ;; It was a false alarm. Check if we're in a label (or other
1502 ;; construct with `:' except bitfield) instead.
1503 (goto-char start-pos)
1504 (when (setq label-type (c-forward-label t match-pos nil))
1505 ;; Can't use `c-fontify-types-and-refs' here since we
1506 ;; use the label face at times.
1507 (cond ((eq label-type 'goto-target)
1508 (c-put-font-lock-face (caar c-record-ref-identifiers)
1509 (cdar c-record-ref-identifiers)
1510 c-label-face-name))
1511 ((eq label-type 'qt-1kwd-colon)
1512 (c-put-font-lock-face (caar c-record-ref-identifiers)
1513 (cdar c-record-ref-identifiers)
1514 'font-lock-keyword-face))
1515 ((eq label-type 'qt-2kwds-colon)
1516 (mapc
1517 (lambda (kwd)
1518 (c-put-font-lock-face (car kwd) (cdr kwd)
1519 'font-lock-keyword-face))
1520 c-record-ref-identifiers)))
1521 (setq c-record-ref-identifiers nil)
1522 ;; `c-forward-label' has probably added a `c-decl-end'
1523 ;; marker, so return t to `c-find-decl-spots' to signal
1524 ;; that.
1525 t))))
1527 nil)))
1529 (defun c-font-lock-enum-tail (limit)
1530 ;; Fontify an enum's identifiers when POINT is within the enum's brace
1531 ;; block.
1533 ;; This function will be called from font-lock for a region bounded by POINT
1534 ;; and LIMIT, as though it were to identify a keyword for
1535 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
1536 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
1537 ;; Fontification".
1539 ;; Note that this function won't attempt to fontify beyond the end of the
1540 ;; current enum block, if any.
1541 (let* ((paren-state (c-parse-state))
1542 (encl-pos (c-most-enclosing-brace paren-state)))
1543 (when (and
1544 encl-pos
1545 (eq (char-after encl-pos) ?\{)
1546 (save-excursion
1547 (goto-char encl-pos)
1548 (c-backward-over-enum-header)))
1549 (c-syntactic-skip-backward "^{," nil t)
1550 (c-put-char-property (1- (point)) 'c-type 'c-decl-id-start)
1552 (c-forward-syntactic-ws)
1553 (c-font-lock-declarators limit t nil)))
1554 nil)
1556 (defun c-font-lock-enclosing-decls (limit)
1557 ;; Fontify the declarators of (nested) declarations we're in the middle of.
1558 ;; This is mainly for when a jit-lock etc. chunk starts inside the brace
1559 ;; block of a struct/union/class, etc.
1561 ;; This function will be called from font-lock for a region bounded by POINT
1562 ;; and LIMIT, as though it were to identify a keyword for
1563 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
1564 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
1565 ;; Fontification".
1566 (let* ((paren-state (c-parse-state))
1567 (decl-search-lim (c-determine-limit 1000))
1568 decl-context in-typedef ps-elt)
1569 ;; Are we in any nested struct/union/class/etc. braces?
1570 (while paren-state
1571 (setq ps-elt (car paren-state)
1572 paren-state (cdr paren-state))
1573 (when (and (atom ps-elt)
1574 (eq (char-after ps-elt) ?\{))
1575 (goto-char ps-elt)
1576 (setq decl-context (c-beginning-of-decl-1 decl-search-lim)
1577 in-typedef (looking-at c-typedef-key))
1578 (if in-typedef (c-forward-token-2))
1579 (when (and c-opt-block-decls-with-vars-key
1580 (looking-at c-opt-block-decls-with-vars-key))
1581 (goto-char ps-elt)
1582 (when (c-safe (c-forward-sexp))
1583 (c-forward-syntactic-ws)
1584 (c-font-lock-declarators limit t in-typedef)))))))
1586 (c-lang-defconst c-simple-decl-matchers
1587 "Simple font lock matchers for types and declarations. These are used
1588 on level 2 only and so aren't combined with `c-complex-decl-matchers'."
1590 t `(;; Objective-C methods.
1591 ,@(when (c-major-mode-is 'objc-mode)
1592 `((,(c-lang-const c-opt-method-key)
1593 (,(byte-compile
1594 (lambda (limit)
1595 (let (;; The font-lock package in Emacs is known to clobber
1596 ;; `parse-sexp-lookup-properties' (when it exists).
1597 (parse-sexp-lookup-properties
1598 (cc-eval-when-compile
1599 (boundp 'parse-sexp-lookup-properties))))
1600 (save-restriction
1601 (narrow-to-region (point-min) limit)
1602 (c-font-lock-objc-method)))
1603 nil))
1604 (goto-char (match-end 1))))))
1606 ;; Fontify all type names and the identifiers in the
1607 ;; declarations they might start. Use eval here since
1608 ;; `c-known-type-key' gets its value from
1609 ;; `*-font-lock-extra-types' on mode init.
1610 (eval . (list ,(c-make-font-lock-search-function
1611 'c-known-type-key
1612 '(1 'font-lock-type-face t)
1613 '((c-font-lock-declarators limit t nil)
1614 (save-match-data
1615 (goto-char (match-end 1))
1616 (c-forward-syntactic-ws))
1617 (goto-char (match-end 1))))))
1619 ;; Fontify types preceded by `c-type-prefix-kwds' and the
1620 ;; identifiers in the declarations they might start.
1621 ,@(when (c-lang-const c-type-prefix-kwds)
1622 (let* ((prefix-re (c-make-keywords-re nil
1623 (c-lang-const c-type-prefix-kwds)))
1624 (type-match (+ 2
1625 (regexp-opt-depth prefix-re)
1626 (c-lang-const c-simple-ws-depth))))
1627 `((,(c-make-font-lock-search-function
1628 (concat "\\<\\(" prefix-re "\\)" ; 1
1629 (c-lang-const c-simple-ws) "+"
1630 (concat "\\(" ; 2 + prefix-re + c-simple-ws
1631 (c-lang-const c-symbol-key)
1632 "\\)"))
1633 `(,type-match
1634 'font-lock-type-face t)
1635 `((c-font-lock-declarators limit t nil)
1636 (save-match-data
1637 (goto-char (match-end ,type-match))
1638 (c-forward-syntactic-ws))
1639 (goto-char (match-end ,type-match))))))))
1641 ;; Fontify special declarations that lacks a type.
1642 ,@(when (c-lang-const c-typeless-decl-kwds)
1643 `((,(c-make-font-lock-search-function
1644 (concat "\\<\\("
1645 (regexp-opt (c-lang-const c-typeless-decl-kwds))
1646 "\\)\\>")
1647 '((c-font-lock-declarators limit t nil)
1648 (save-match-data
1649 (goto-char (match-end 1))
1650 (c-forward-syntactic-ws))
1651 (goto-char (match-end 1)))))))
1653 ;; Fontify generic colon labels in languages that support them.
1654 ,@(when (c-lang-const c-recognize-colon-labels)
1655 `(c-font-lock-labels))))
1657 (c-lang-defconst c-complex-decl-matchers
1658 "Complex font lock matchers for types and declarations. Used on level
1659 3 and higher."
1661 ;; Note: This code in this form dumps a number of functions into the
1662 ;; resulting constant, `c-matchers-3'. At run time, font lock will call
1663 ;; each of them as a "FUNCTION" (see Elisp page "Search-based
1664 ;; Fontification"). The font lock region is delimited by POINT and the
1665 ;; single parameter, LIMIT. Each of these functions returns NIL (thus
1666 ;; inhibiting spurious font-lock-keyword-face highlighting and another
1667 ;; call).
1669 t `(;; Initialize some things before the search functions below.
1670 c-font-lock-complex-decl-prepare
1672 ,@(if (c-major-mode-is 'objc-mode)
1673 ;; Fontify method declarations in Objective-C, but first
1674 ;; we have to put the `c-decl-end' `c-type' property on
1675 ;; all the @-style directives that haven't been handled in
1676 ;; `c-basic-matchers-before'.
1677 `(,(c-make-font-lock-search-function
1678 (c-make-keywords-re t
1679 ;; Exclude "@class" since that directive ends with a
1680 ;; semicolon anyway.
1681 (delete "@class"
1682 (append (c-lang-const c-protection-kwds)
1683 (c-lang-const c-other-decl-kwds)
1684 nil)))
1685 '((c-put-char-property (1- (match-end 1))
1686 'c-type 'c-decl-end)))
1687 c-font-lock-objc-methods))
1689 ;; Fontify all declarations, casts and normal labels.
1690 c-font-lock-declarations
1692 ;; Fontify declarators when POINT is within their declaration.
1693 c-font-lock-enclosing-decls
1695 ;; Fontify angle bracket arglists like templates in C++.
1696 ,@(when (c-lang-const c-recognize-<>-arglists)
1697 `(c-font-lock-<>-arglists))
1699 ;; The first two rules here mostly find occurrences that
1700 ;; `c-font-lock-declarations' has found already, but not
1701 ;; declarations containing blocks in the type (see note below).
1702 ;; It's also useful to fontify these everywhere to show e.g. when
1703 ;; a type keyword is accidentally used as an identifier.
1705 ;; Fontify basic types.
1706 ,(let ((re (c-make-keywords-re nil
1707 (c-lang-const c-primitive-type-kwds))))
1708 (if (c-major-mode-is 'pike-mode)
1709 ;; No symbol is a keyword after "->" in Pike.
1710 `(,(concat "\\(\\=.?\\|[^>]\\|[^-]>\\)"
1711 "\\<\\(" re "\\)\\>")
1712 2 font-lock-type-face)
1713 `(,(concat "\\<\\(" re "\\)\\>")
1714 1 'font-lock-type-face)))
1716 ;; Fontify types preceded by `c-type-prefix-kwds' (e.g. "struct").
1717 ,@(when (c-lang-const c-type-prefix-kwds)
1718 `((,(byte-compile
1719 `(lambda (limit)
1720 (c-fontify-types-and-refs
1721 ((c-promote-possible-types t)
1722 ;; The font-lock package in Emacs is known to clobber
1723 ;; `parse-sexp-lookup-properties' (when it exists).
1724 (parse-sexp-lookup-properties
1725 (cc-eval-when-compile
1726 (boundp 'parse-sexp-lookup-properties))))
1727 (save-restriction
1728 ;; Narrow to avoid going past the limit in
1729 ;; `c-forward-type'.
1730 (narrow-to-region (point) limit)
1731 (while (re-search-forward
1732 ,(concat "\\<\\("
1733 (c-make-keywords-re nil
1734 (c-lang-const c-type-prefix-kwds))
1735 "\\)\\>")
1736 limit t)
1737 (unless (c-skip-comments-and-strings limit)
1738 (c-forward-syntactic-ws)
1739 ;; Handle prefix declaration specifiers.
1740 (when (or (looking-at c-prefix-spec-kwds-re)
1741 (and (c-major-mode-is 'java-mode)
1742 (looking-at "@[A-Za-z0-9]+")))
1743 (c-forward-keyword-clause 1))
1744 ,(if (c-major-mode-is 'c++-mode)
1745 `(when (and (c-forward-type)
1746 (eq (char-after) ?=))
1747 ;; In C++ we additionally check for a "class
1748 ;; X = Y" construct which is used in
1749 ;; templates, to fontify Y as a type.
1750 (forward-char)
1751 (c-forward-syntactic-ws)
1752 (c-forward-type))
1753 `(c-forward-type))
1754 )))))))))
1756 ;; Fontify symbols after closing braces as declaration
1757 ;; identifiers under the assumption that they are part of
1758 ;; declarations like "class Foo { ... } foo;". It's too
1759 ;; expensive to check this accurately by skipping past the
1760 ;; brace block, so we use the heuristic that it's such a
1761 ;; declaration if the first identifier is on the same line as
1762 ;; the closing brace. `c-font-lock-declarations' will later
1763 ;; override it if it turns out to be an new declaration, but
1764 ;; it will be wrong if it's an expression (see the test
1765 ;; decls-8.cc).
1766 ;; ,@(when (c-lang-const c-opt-block-decls-with-vars-key)
1767 ;; `((,(c-make-font-lock-search-function
1768 ;; (concat "}"
1769 ;; (c-lang-const c-single-line-syntactic-ws)
1770 ;; "\\(" ; 1 + c-single-line-syntactic-ws-depth
1771 ;; (c-lang-const c-type-decl-prefix-key)
1772 ;; "\\|"
1773 ;; (c-lang-const c-symbol-key)
1774 ;; "\\)")
1775 ;; `((c-font-lock-declarators limit t nil) ; That nil says use `font-lock-variable-name-face';
1776 ;; ; t would mean `font-lock-function-name-face'.
1777 ;; (progn
1778 ;; (c-put-char-property (match-beginning 0) 'c-type
1779 ;; 'c-decl-id-start)
1780 ;; ; 'c-decl-type-start)
1781 ;; (goto-char (match-beginning
1782 ;; ,(1+ (c-lang-const
1783 ;; c-single-line-syntactic-ws-depth)))))
1784 ;; (goto-char (match-end 0)))))))
1786 ;; Fontify the type in C++ "new" expressions.
1787 ,@(when (c-major-mode-is 'c++-mode)
1788 ;; This pattern is a probably a "(MATCHER . ANCHORED-HIGHLIGHTER)"
1789 ;; (see Elisp page "Search-based Fontification").
1790 `(("\\<new\\>"
1791 (c-font-lock-c++-new))))
1794 (defun c-font-lock-labels (limit)
1795 ;; Fontify all statement labels from the point to LIMIT. Assumes
1796 ;; that strings and comments have been fontified already. Nil is
1797 ;; always returned.
1799 ;; Note: This function is only used on decoration level 2; this is
1800 ;; taken care of directly by the gargantuan
1801 ;; `c-font-lock-declarations' on higher levels.
1803 ;; This function might do hidden buffer changes.
1805 (let (continue-pos id-start
1806 ;; The font-lock package in Emacs is known to clobber
1807 ;; `parse-sexp-lookup-properties' (when it exists).
1808 (parse-sexp-lookup-properties
1809 (cc-eval-when-compile
1810 (boundp 'parse-sexp-lookup-properties))))
1812 (while (re-search-forward ":[^:]" limit t)
1813 (setq continue-pos (point))
1814 (goto-char (match-beginning 0))
1815 (unless (c-skip-comments-and-strings limit)
1817 (c-backward-syntactic-ws)
1818 (and (setq id-start (c-on-identifier))
1820 (not (get-text-property id-start 'face))
1822 (progn
1823 (goto-char id-start)
1824 (c-backward-syntactic-ws)
1826 ;; Check for a char that precedes a statement.
1827 (memq (char-before) '(?\} ?\{ ?\;))
1828 ;; Check for a preceding label. We exploit the font
1829 ;; locking made earlier by this function.
1830 (and (eq (char-before) ?:)
1831 (progn
1832 (backward-char)
1833 (c-backward-syntactic-ws)
1834 (not (bobp)))
1835 (eq (get-text-property (1- (point)) 'face)
1836 c-label-face-name))
1837 ;; Check for a keyword that precedes a statement.
1838 (c-after-conditional)))
1840 (progn
1841 ;; Got a label.
1842 (goto-char id-start)
1843 (looking-at c-symbol-key)
1844 (c-put-font-lock-face (match-beginning 0) (match-end 0)
1845 c-label-face-name)))
1847 (goto-char continue-pos))))
1848 nil)
1850 (c-lang-defconst c-basic-matchers-after
1851 "Font lock matchers for various things that should be fontified after
1852 generic casts and declarations are fontified. Used on level 2 and
1853 higher."
1855 t `(,@(when (c-lang-const c-brace-id-list-kwds)
1856 ;; Fontify the remaining identifiers inside an enum list when we start
1857 ;; inside it.
1858 `(c-font-lock-enum-tail
1859 ;; Fontify the identifiers inside enum lists. (The enum type
1860 ;; name is handled by `c-simple-decl-matchers' or
1861 ;; `c-complex-decl-matchers' below.
1862 (,(c-make-font-lock-search-function
1863 (concat
1864 "\\<\\("
1865 (c-make-keywords-re nil (c-lang-const c-brace-id-list-kwds))
1866 "\\)\\>"
1867 ;; Disallow various common punctuation chars that can't come
1868 ;; before the '{' of the enum list, to avoid searching too far.
1869 "[^\]\[{}();/#=]*"
1870 "{")
1871 '((c-font-lock-declarators limit t nil)
1872 (save-match-data
1873 (goto-char (match-end 0))
1874 (c-put-char-property (1- (point)) 'c-type
1875 'c-decl-id-start)
1876 (c-forward-syntactic-ws))
1877 (goto-char (match-end 0)))))))
1879 ;; Fontify labels after goto etc.
1880 ,@(when (c-lang-const c-before-label-kwds)
1881 `(;; (Got three different interpretation levels here,
1882 ;; which makes it a bit complicated: 1) The backquote
1883 ;; stuff is expanded when compiled or loaded, 2) the
1884 ;; eval form is evaluated at font-lock setup (to
1885 ;; substitute c-label-face-name correctly), and 3) the
1886 ;; resulting structure is interpreted during
1887 ;; fontification.)
1888 (eval
1889 . ,(let* ((c-before-label-re
1890 (c-make-keywords-re nil
1891 (c-lang-const c-before-label-kwds))))
1892 `(list
1893 ,(concat "\\<\\(" c-before-label-re "\\)\\>"
1894 "\\s *"
1895 "\\(" ; identifier-offset
1896 (c-lang-const c-symbol-key)
1897 "\\)")
1898 (list ,(+ (regexp-opt-depth c-before-label-re) 2)
1899 c-label-face-name nil t))))))
1901 ;; Fontify the clauses after various keywords.
1902 ,@(when (or (c-lang-const c-type-list-kwds)
1903 (c-lang-const c-ref-list-kwds)
1904 (c-lang-const c-colon-type-list-kwds))
1905 `((,(c-make-font-lock-BO-decl-search-function
1906 (concat "\\<\\("
1907 (c-make-keywords-re nil
1908 (append (c-lang-const c-type-list-kwds)
1909 (c-lang-const c-ref-list-kwds)
1910 (c-lang-const c-colon-type-list-kwds)))
1911 "\\)\\>")
1912 '((c-fontify-types-and-refs ((c-promote-possible-types t))
1913 (c-forward-keyword-clause 1)
1914 (if (> (point) limit) (goto-char limit))))))))
1916 ,@(when (c-lang-const c-paren-type-kwds)
1917 `((,(c-make-font-lock-search-function
1918 (concat "\\<\\("
1919 (c-make-keywords-re nil
1920 (c-lang-const c-paren-type-kwds))
1921 "\\)\\>")
1922 '((c-fontify-types-and-refs ((c-promote-possible-types t))
1923 (c-forward-keyword-clause 1)
1924 (if (> (point) limit) (goto-char limit))))))))
1926 ,@(when (c-major-mode-is 'java-mode)
1927 `((eval . (list "\\<\\(@[a-zA-Z0-9]+\\)\\>" 1 c-annotation-face))))
1930 (c-lang-defconst c-matchers-1
1931 t (c-lang-const c-cpp-matchers))
1933 (c-lang-defconst c-matchers-2
1934 t (append (c-lang-const c-matchers-1)
1935 (c-lang-const c-basic-matchers-before)
1936 (c-lang-const c-simple-decl-matchers)
1937 (c-lang-const c-basic-matchers-after)))
1939 (c-lang-defconst c-matchers-3
1940 t (append (c-lang-const c-matchers-1)
1941 (c-lang-const c-basic-matchers-before)
1942 (c-lang-const c-complex-decl-matchers)
1943 (c-lang-const c-basic-matchers-after)))
1945 (defun c-compose-keywords-list (base-list)
1946 ;; Incorporate the font lock keyword lists according to
1947 ;; `c-doc-comment-style' on the given keyword list and return it.
1948 ;; This is used in the function bindings of the
1949 ;; `*-font-lock-keywords-*' symbols since we have to build the list
1950 ;; when font-lock is initialized.
1952 (unless (memq c-doc-face-name c-literal-faces)
1953 (setq c-literal-faces (cons c-doc-face-name c-literal-faces)))
1955 (let* ((doc-keywords
1956 (if (consp (car-safe c-doc-comment-style))
1957 (cdr-safe (or (assq c-buffer-is-cc-mode c-doc-comment-style)
1958 (assq 'other c-doc-comment-style)))
1959 c-doc-comment-style))
1960 (list (nconc (apply 'nconc
1961 (mapcar
1962 (lambda (doc-style)
1963 (let ((sym (intern
1964 (concat (symbol-name doc-style)
1965 "-font-lock-keywords"))))
1966 (cond ((fboundp sym)
1967 (funcall sym))
1968 ((boundp sym)
1969 (append (eval sym) nil)))))
1970 (if (listp doc-keywords)
1971 doc-keywords
1972 (list doc-keywords))))
1973 base-list)))
1975 ;; Kludge: If `c-font-lock-complex-decl-prepare' is on the list we
1976 ;; move it first since the doc comment font lockers might add
1977 ;; `c-type' text properties, so they have to be cleared before that.
1978 (when (memq 'c-font-lock-complex-decl-prepare list)
1979 (setq list (cons 'c-font-lock-complex-decl-prepare
1980 (delq 'c-font-lock-complex-decl-prepare
1981 (append list nil)))))
1983 list))
1985 (defun c-override-default-keywords (def-var)
1986 ;; This is used to override the value on a `*-font-lock-keywords'
1987 ;; variable only if it's nil or has the same value as one of the
1988 ;; `*-font-lock-keywords-*' variables. Older font-lock packages
1989 ;; define a default value for `*-font-lock-keywords' which we want
1990 ;; to override, but we should otoh avoid clobbering a user setting.
1991 ;; This heuristic for that isn't perfect, but I can't think of any
1992 ;; better. /mast
1993 (when (and (boundp def-var)
1994 (memq (symbol-value def-var)
1995 (cons nil
1996 (mapcar
1997 (lambda (suffix)
1998 (let ((sym (intern (concat (symbol-name def-var)
1999 suffix))))
2000 (and (boundp sym) (symbol-value sym))))
2001 '("-1" "-2" "-3")))))
2002 ;; The overriding is done by unbinding the variable so that the normal
2003 ;; defvar will install its default value later on.
2004 (makunbound def-var)))
2007 ;;; C.
2009 (c-override-default-keywords 'c-font-lock-keywords)
2011 (defconst c-font-lock-keywords-1 (c-lang-const c-matchers-1 c)
2012 "Minimal font locking for C mode.
2013 Fontifies only preprocessor directives (in addition to the syntactic
2014 fontification of strings and comments).")
2016 (defconst c-font-lock-keywords-2 (c-lang-const c-matchers-2 c)
2017 "Fast normal font locking for C mode.
2018 In addition to `c-font-lock-keywords-1', this adds fontification of
2019 keywords, simple types, declarations that are easy to recognize, the
2020 user defined types on `c-font-lock-extra-types', and the doc comment
2021 styles specified by `c-doc-comment-style'.")
2023 (defconst c-font-lock-keywords-3 (c-lang-const c-matchers-3 c)
2024 "Accurate normal font locking for C mode.
2025 Like the variable `c-font-lock-keywords-2' but detects declarations in a more
2026 accurate way that works in most cases for arbitrary types without the
2027 need for `c-font-lock-extra-types'.")
2029 (defvar c-font-lock-keywords c-font-lock-keywords-3
2030 "Default expressions to highlight in C mode.")
2032 (defun c-font-lock-keywords-2 ()
2033 (c-compose-keywords-list c-font-lock-keywords-2))
2034 (defun c-font-lock-keywords-3 ()
2035 (c-compose-keywords-list c-font-lock-keywords-3))
2036 (defun c-font-lock-keywords ()
2037 (c-compose-keywords-list c-font-lock-keywords))
2040 ;;; C++.
2042 (defun c-font-lock-c++-new (limit)
2043 ;; FIXME!!! Put in a comment about the context of this function's
2044 ;; invocation. I think it's called as an ANCHORED-MATCHER within an
2045 ;; ANCHORED-HIGHLIGHTER. (2007/2/10).
2047 ;; Assuming point is after a "new" word, check that it isn't inside
2048 ;; a string or comment, and if so try to fontify the type in the
2049 ;; allocation expression. Nil is always returned.
2051 ;; As usual, C++ takes the prize in coming up with a hard to parse
2052 ;; syntax. :P
2054 ;; This function might do hidden buffer changes.
2056 (unless (c-skip-comments-and-strings limit)
2057 (save-excursion
2058 (catch 'false-alarm
2059 ;; A "new" keyword is followed by one to three expressions, where
2060 ;; the type is the middle one, and the only required part.
2061 (let (expr1-pos expr2-pos
2062 ;; Enable recording of identifier ranges in `c-forward-type'
2063 ;; etc for later fontification. Not using
2064 ;; `c-fontify-types-and-refs' here since the ranges should
2065 ;; be fontified selectively only when an allocation
2066 ;; expression is successfully recognized.
2067 (c-record-type-identifiers t)
2068 c-record-ref-identifiers
2069 ;; The font-lock package in Emacs is known to clobber
2070 ;; `parse-sexp-lookup-properties' (when it exists).
2071 (parse-sexp-lookup-properties
2072 (cc-eval-when-compile
2073 (boundp 'parse-sexp-lookup-properties))))
2074 (c-forward-syntactic-ws)
2076 ;; The first placement arglist is always parenthesized, if it
2077 ;; exists.
2078 (when (eq (char-after) ?\()
2079 (setq expr1-pos (1+ (point)))
2080 (condition-case nil
2081 (c-forward-sexp)
2082 (scan-error (throw 'false-alarm t)))
2083 (c-forward-syntactic-ws))
2085 ;; The second expression is either a type followed by some "*" or
2086 ;; "[...]" or similar, or a parenthesized type followed by a full
2087 ;; identifierless declarator.
2088 (setq expr2-pos (1+ (point)))
2089 (cond ((eq (char-after) ?\())
2090 ((let ((c-promote-possible-types t))
2091 (c-forward-type)))
2092 (t (setq expr2-pos nil)))
2094 (when expr1-pos
2095 (cond
2096 ((not expr2-pos)
2097 ;; No second expression, so the first has to be a
2098 ;; parenthesized type.
2099 (goto-char expr1-pos)
2100 (let ((c-promote-possible-types t))
2101 (c-forward-type)))
2103 ((eq (char-before expr2-pos) ?\()
2104 ;; Got two parenthesized expressions, so we have to look
2105 ;; closer at them to decide which is the type. No need to
2106 ;; handle `c-record-ref-identifiers' since all references
2107 ;; have already been handled by other fontification rules.
2108 (let (expr1-res expr2-res)
2110 (goto-char expr1-pos)
2111 (when (setq expr1-res (c-forward-type))
2112 (unless (looking-at
2113 (cc-eval-when-compile
2114 (concat (c-lang-const c-symbol-start c++)
2115 "\\|[*:\)\[]")))
2116 ;; There's something after the would-be type that
2117 ;; can't be there, so this is a placement arglist.
2118 (setq expr1-res nil)))
2120 (goto-char expr2-pos)
2121 (when (setq expr2-res (c-forward-type))
2122 (unless (looking-at
2123 (cc-eval-when-compile
2124 (concat (c-lang-const c-symbol-start c++)
2125 "\\|[*:\)\[]")))
2126 ;; There's something after the would-be type that can't
2127 ;; be there, so this is an initialization expression.
2128 (setq expr2-res nil))
2129 (when (and (c-go-up-list-forward)
2130 (progn (c-forward-syntactic-ws)
2131 (eq (char-after) ?\()))
2132 ;; If there's a third initialization expression
2133 ;; then the second one is the type, so demote the
2134 ;; first match.
2135 (setq expr1-res nil)))
2137 ;; We fontify the most likely type, with a preference for
2138 ;; the first argument since a placement arglist is more
2139 ;; unusual than an initializer.
2140 (cond ((memq expr1-res '(t known prefix)))
2141 ((memq expr2-res '(t known prefix)))
2142 ;; Presumably 'decltype's will be fontified elsewhere.
2143 ((eq expr1-res 'decltype))
2144 ((eq expr2-res 'decltype))
2145 ((eq expr1-res 'found)
2146 (let ((c-promote-possible-types t))
2147 (goto-char expr1-pos)
2148 (c-forward-type)))
2149 ((eq expr2-res 'found)
2150 (let ((c-promote-possible-types t))
2151 (goto-char expr2-pos)
2152 (c-forward-type)))
2153 ((and (eq expr1-res 'maybe) (not expr2-res))
2154 (let ((c-promote-possible-types t))
2155 (goto-char expr1-pos)
2156 (c-forward-type)))
2157 ((and (not expr1-res) (eq expr2-res 'maybe))
2158 (let ((c-promote-possible-types t))
2159 (goto-char expr2-pos)
2160 (c-forward-type)))
2161 ;; If both type matches are 'maybe then we're
2162 ;; too uncertain to promote either of them.
2163 )))))
2165 ;; Fontify the type that now is recorded in
2166 ;; `c-record-type-identifiers', if any.
2167 (c-fontify-recorded-types-and-refs)))))
2168 nil)
2170 (c-override-default-keywords 'c++-font-lock-keywords)
2172 (defconst c++-font-lock-keywords-1 (c-lang-const c-matchers-1 c++)
2173 "Minimal font locking for C++ mode.
2174 Fontifies only preprocessor directives (in addition to the syntactic
2175 fontification of strings and comments).")
2177 (defconst c++-font-lock-keywords-2 (c-lang-const c-matchers-2 c++)
2178 "Fast normal font locking for C++ mode.
2179 In addition to `c++-font-lock-keywords-1', this adds fontification of
2180 keywords, simple types, declarations that are easy to recognize, the
2181 user defined types on `c++-font-lock-extra-types', and the doc comment
2182 styles specified by `c-doc-comment-style'.")
2184 (defconst c++-font-lock-keywords-3 (c-lang-const c-matchers-3 c++)
2185 "Accurate normal font locking for C++ mode.
2186 Like the variable `c++-font-lock-keywords-2' but detects declarations in a more
2187 accurate way that works in most cases for arbitrary types without the
2188 need for `c++-font-lock-extra-types'.")
2190 (defvar c++-font-lock-keywords c++-font-lock-keywords-3
2191 "Default expressions to highlight in C++ mode.")
2193 (defun c++-font-lock-keywords-2 ()
2194 (c-compose-keywords-list c++-font-lock-keywords-2))
2195 (defun c++-font-lock-keywords-3 ()
2196 (c-compose-keywords-list c++-font-lock-keywords-3))
2197 (defun c++-font-lock-keywords ()
2198 (c-compose-keywords-list c++-font-lock-keywords))
2201 ;;; Objective-C.
2203 (defun c-font-lock-objc-method ()
2204 ;; Assuming the point is after the + or - that starts an Objective-C
2205 ;; method declaration, fontify it. This must be done before normal
2206 ;; casts, declarations and labels are fontified since they will get
2207 ;; false matches in these things.
2209 ;; This function might do hidden buffer changes.
2211 (c-fontify-types-and-refs
2212 ((first t)
2213 (c-promote-possible-types t))
2215 (while (and
2216 (progn
2217 (c-forward-syntactic-ws)
2219 ;; An optional method type.
2220 (if (eq (char-after) ?\()
2221 (progn
2222 (forward-char)
2223 (c-forward-syntactic-ws)
2224 (c-forward-type)
2225 (prog1 (c-go-up-list-forward)
2226 (c-forward-syntactic-ws)))
2229 ;; The name. The first time it's the first part of
2230 ;; the function name, the rest of the time it's an
2231 ;; argument name.
2232 (looking-at c-symbol-key)
2233 (progn
2234 (goto-char (match-end 0))
2235 (c-put-font-lock-face (match-beginning 0)
2236 (point)
2237 (if first
2238 'font-lock-function-name-face
2239 'font-lock-variable-name-face))
2240 (c-forward-syntactic-ws)
2242 ;; Another optional part of the function name.
2243 (when (looking-at c-symbol-key)
2244 (goto-char (match-end 0))
2245 (c-put-font-lock-face (match-beginning 0)
2246 (point)
2247 'font-lock-function-name-face)
2248 (c-forward-syntactic-ws))
2250 ;; There's another argument if a colon follows.
2251 (eq (char-after) ?:)))
2252 (forward-char)
2253 (setq first nil))))
2255 (defun c-font-lock-objc-methods (limit)
2256 ;; Fontify method declarations in Objective-C. Nil is always
2257 ;; returned.
2259 ;; This function might do hidden buffer changes.
2261 (let (;; The font-lock package in Emacs is known to clobber
2262 ;; `parse-sexp-lookup-properties' (when it exists).
2263 (parse-sexp-lookup-properties
2264 (cc-eval-when-compile
2265 (boundp 'parse-sexp-lookup-properties))))
2267 (c-find-decl-spots
2268 limit
2269 "[-+]"
2271 (lambda (match-pos inside-macro)
2272 (forward-char)
2273 (c-font-lock-objc-method))))
2274 nil)
2276 (c-override-default-keywords 'objc-font-lock-keywords)
2278 (defconst objc-font-lock-keywords-1 (c-lang-const c-matchers-1 objc)
2279 "Minimal font locking for Objective-C mode.
2280 Fontifies only compiler directives (in addition to the syntactic
2281 fontification of strings and comments).")
2283 (defconst objc-font-lock-keywords-2 (c-lang-const c-matchers-2 objc)
2284 "Fast normal font locking for Objective-C mode.
2285 In addition to `objc-font-lock-keywords-1', this adds fontification of
2286 keywords, simple types, declarations that are easy to recognize, the
2287 user defined types on `objc-font-lock-extra-types', and the doc
2288 comment styles specified by `c-doc-comment-style'.")
2290 (defconst objc-font-lock-keywords-3 (c-lang-const c-matchers-3 objc)
2291 "Accurate normal font locking for Objective-C mode.
2292 Like the variable `objc-font-lock-keywords-2' but detects declarations in a more
2293 accurate way that works in most cases for arbitrary types without the
2294 need for `objc-font-lock-extra-types'.")
2296 (defvar objc-font-lock-keywords objc-font-lock-keywords-3
2297 "Default expressions to highlight in Objective-C mode.")
2299 (defun objc-font-lock-keywords-2 ()
2300 (c-compose-keywords-list objc-font-lock-keywords-2))
2301 (defun objc-font-lock-keywords-3 ()
2302 (c-compose-keywords-list objc-font-lock-keywords-3))
2303 (defun objc-font-lock-keywords ()
2304 (c-compose-keywords-list objc-font-lock-keywords))
2306 ;; Kludge to override the default value that
2307 ;; `objc-font-lock-extra-types' might have gotten from the font-lock
2308 ;; package. The value replaced here isn't relevant now anyway since
2309 ;; those types are builtin and therefore listed directly in
2310 ;; `c-primitive-type-kwds'.
2311 (when (equal (sort (append objc-font-lock-extra-types nil) 'string-lessp)
2312 '("BOOL" "Class" "IMP" "SEL"))
2313 (setq objc-font-lock-extra-types
2314 (cc-eval-when-compile (list (concat "[" c-upper "]\\sw*")))))
2317 ;;; Java.
2319 (c-override-default-keywords 'java-font-lock-keywords)
2321 (defconst java-font-lock-keywords-1 (c-lang-const c-matchers-1 java)
2322 "Minimal font locking for Java mode.
2323 Fontifies nothing except the syntactic fontification of strings and
2324 comments.")
2326 (defconst java-font-lock-keywords-2 (c-lang-const c-matchers-2 java)
2327 "Fast normal font locking for Java mode.
2328 In addition to `java-font-lock-keywords-1', this adds fontification of
2329 keywords, simple types, declarations that are easy to recognize, the
2330 user defined types on `java-font-lock-extra-types', and the doc
2331 comment styles specified by `c-doc-comment-style'.")
2333 (defconst java-font-lock-keywords-3 (c-lang-const c-matchers-3 java)
2334 "Accurate normal font locking for Java mode.
2335 Like variable `java-font-lock-keywords-2' but detects declarations in a more
2336 accurate way that works in most cases for arbitrary types without the
2337 need for `java-font-lock-extra-types'.")
2339 (defvar java-font-lock-keywords java-font-lock-keywords-3
2340 "Default expressions to highlight in Java mode.")
2342 (defun java-font-lock-keywords-2 ()
2343 (c-compose-keywords-list java-font-lock-keywords-2))
2344 (defun java-font-lock-keywords-3 ()
2345 (c-compose-keywords-list java-font-lock-keywords-3))
2346 (defun java-font-lock-keywords ()
2347 (c-compose-keywords-list java-font-lock-keywords))
2350 ;;; CORBA IDL.
2352 (c-override-default-keywords 'idl-font-lock-keywords)
2354 (defconst idl-font-lock-keywords-1 (c-lang-const c-matchers-1 idl)
2355 "Minimal font locking for CORBA IDL mode.
2356 Fontifies nothing except the syntactic fontification of strings and
2357 comments.")
2359 (defconst idl-font-lock-keywords-2 (c-lang-const c-matchers-2 idl)
2360 "Fast normal font locking for CORBA IDL mode.
2361 In addition to `idl-font-lock-keywords-1', this adds fontification of
2362 keywords, simple types, declarations that are easy to recognize, the
2363 user defined types on `idl-font-lock-extra-types', and the doc comment
2364 styles specified by `c-doc-comment-style'.")
2366 (defconst idl-font-lock-keywords-3 (c-lang-const c-matchers-3 idl)
2367 "Accurate normal font locking for CORBA IDL mode.
2368 Like the variable `idl-font-lock-keywords-2' but detects declarations in a more
2369 accurate way that works in most cases for arbitrary types without the
2370 need for `idl-font-lock-extra-types'.")
2372 (defvar idl-font-lock-keywords idl-font-lock-keywords-3
2373 "Default expressions to highlight in CORBA IDL mode.")
2375 (defun idl-font-lock-keywords-2 ()
2376 (c-compose-keywords-list idl-font-lock-keywords-2))
2377 (defun idl-font-lock-keywords-3 ()
2378 (c-compose-keywords-list idl-font-lock-keywords-3))
2379 (defun idl-font-lock-keywords ()
2380 (c-compose-keywords-list idl-font-lock-keywords))
2383 ;;; Pike.
2385 (c-override-default-keywords 'pike-font-lock-keywords)
2387 (defconst pike-font-lock-keywords-1 (c-lang-const c-matchers-1 pike)
2388 "Minimal font locking for Pike mode.
2389 Fontifies only preprocessor directives (in addition to the syntactic
2390 fontification of strings and comments).")
2392 (defconst pike-font-lock-keywords-2 (c-lang-const c-matchers-2 pike)
2393 "Fast normal font locking for Pike mode.
2394 In addition to `pike-font-lock-keywords-1', this adds fontification of
2395 keywords, simple types, declarations that are easy to recognize, the
2396 user defined types on `pike-font-lock-extra-types', and the doc
2397 comment styles specified by `c-doc-comment-style'.")
2399 (defconst pike-font-lock-keywords-3 (c-lang-const c-matchers-3 pike)
2400 "Accurate normal font locking for Pike mode.
2401 Like the variable `pike-font-lock-keywords-2' but detects declarations in a more
2402 accurate way that works in most cases for arbitrary types without the
2403 need for `pike-font-lock-extra-types'.")
2405 (defvar pike-font-lock-keywords pike-font-lock-keywords-3
2406 "Default expressions to highlight in Pike mode.")
2408 (defun pike-font-lock-keywords-2 ()
2409 (c-compose-keywords-list pike-font-lock-keywords-2))
2410 (defun pike-font-lock-keywords-3 ()
2411 (c-compose-keywords-list pike-font-lock-keywords-3))
2412 (defun pike-font-lock-keywords ()
2413 (c-compose-keywords-list pike-font-lock-keywords))
2416 ;;; Doc comments.
2418 (defun c-font-lock-doc-comments (prefix limit keywords)
2419 ;; Fontify the comments between the point and LIMIT whose start
2420 ;; matches PREFIX with `c-doc-face-name'. Assumes comments have been
2421 ;; fontified with `font-lock-comment-face' already. nil is always
2422 ;; returned.
2424 ;; After the fontification of a matching comment, fontification
2425 ;; according to KEYWORDS is applied inside it. It's a list like
2426 ;; `font-lock-keywords' except that anchored matches and eval
2427 ;; clauses aren't supported and that some abbreviated forms can't be
2428 ;; used. The buffer is narrowed to the comment while KEYWORDS is
2429 ;; applied; leading comment starters are included but trailing
2430 ;; comment enders for block comment are not.
2432 ;; Note that faces added through KEYWORDS should never replace the
2433 ;; existing `c-doc-face-name' face since the existence of that face
2434 ;; is used as a flag in other code to skip comments.
2436 ;; This function might do hidden buffer changes.
2438 (let (comment-beg region-beg)
2439 (if (eq (get-text-property (point) 'face)
2440 'font-lock-comment-face)
2441 ;; Handle the case when the fontified region starts inside a
2442 ;; comment.
2443 (let ((range (c-literal-limits)))
2444 (setq region-beg (point))
2445 (when range
2446 (goto-char (car range)))
2447 (when (looking-at prefix)
2448 (setq comment-beg (point)))))
2450 (while (or
2451 comment-beg
2453 ;; Search for the prefix until a match is found at the start
2454 ;; of a comment.
2455 (while (when (re-search-forward prefix limit t)
2456 (setq comment-beg (match-beginning 0))
2457 (or (not (c-got-face-at comment-beg
2458 c-literal-faces))
2459 (and (/= comment-beg (point-min))
2460 (c-got-face-at (1- comment-beg)
2461 c-literal-faces))))
2462 (setq comment-beg nil))
2463 (setq region-beg comment-beg))
2465 (if (elt (parse-partial-sexp comment-beg (+ comment-beg 2)) 7)
2466 ;; Collect a sequence of doc style line comments.
2467 (progn
2468 (goto-char comment-beg)
2469 (while (and (progn
2470 (c-forward-single-comment)
2471 (skip-syntax-forward " ")
2472 (< (point) limit))
2473 (looking-at prefix))))
2474 (goto-char comment-beg)
2475 (c-forward-single-comment))
2476 (if (> (point) limit) (goto-char limit))
2477 (setq comment-beg nil)
2479 (let ((region-end (point))
2480 (keylist keywords) keyword matcher highlights)
2481 (c-put-font-lock-face region-beg region-end c-doc-face-name)
2482 (save-restriction
2483 ;; Narrow to the doc comment. Among other things, this
2484 ;; helps by making "^" match at the start of the comment.
2485 ;; Do not include a trailing block comment ender, though.
2486 (and (> region-end (1+ region-beg))
2487 (progn (goto-char region-end)
2488 (backward-char 2)
2489 (looking-at "\\*/"))
2490 (setq region-end (point)))
2491 (narrow-to-region region-beg region-end)
2493 (while keylist
2494 (setq keyword (car keylist)
2495 keylist (cdr keylist)
2496 matcher (car keyword))
2497 (goto-char region-beg)
2498 (while (if (stringp matcher)
2499 (re-search-forward matcher region-end t)
2500 (funcall matcher region-end))
2501 (setq highlights (cdr keyword))
2502 (if (consp (car highlights))
2503 (while highlights
2504 (font-lock-apply-highlight (car highlights))
2505 (setq highlights (cdr highlights)))
2506 (font-lock-apply-highlight highlights))))
2508 (goto-char region-end)))))
2509 nil)
2510 (put 'c-font-lock-doc-comments 'lisp-indent-function 2)
2512 (defun c-find-invalid-doc-markup (regexp limit)
2513 ;; Used to fontify invalid markup in doc comments after the correct
2514 ;; ones have been fontified: Find the first occurrence of REGEXP
2515 ;; between the point and LIMIT that only is fontified with
2516 ;; `c-doc-face-name'. If a match is found then submatch 0 surrounds
2517 ;; the first char and t is returned, otherwise nil is returned.
2519 ;; This function might do hidden buffer changes.
2520 (let (start)
2521 (while (if (re-search-forward regexp limit t)
2522 (not (eq (get-text-property
2523 (setq start (match-beginning 0)) 'face)
2524 c-doc-face-name))
2525 (setq start nil)))
2526 (when start
2527 (store-match-data (list (copy-marker start)
2528 (copy-marker (1+ start))))
2529 t)))
2531 ;; GtkDoc patterns contributed by Masatake YAMATO <jet@gyve.org>.
2533 (defconst gtkdoc-font-lock-doc-comments
2534 (let ((symbol "[a-zA-Z0-9_]+")
2535 (header "^ \\* "))
2536 `((,(concat header "\\(" symbol "\\):[ \t]*$")
2537 1 ,c-doc-markup-face-name prepend nil)
2538 (,(concat symbol "()")
2539 0 ,c-doc-markup-face-name prepend nil)
2540 (,(concat header "\\(" "@" symbol "\\):")
2541 1 ,c-doc-markup-face-name prepend nil)
2542 (,(concat "[#%@]" symbol)
2543 0 ,c-doc-markup-face-name prepend nil))
2546 (defconst gtkdoc-font-lock-doc-protection
2547 `(("< \\(public\\|private\\|protected\\) >"
2548 1 ,c-doc-markup-face-name prepend nil)))
2550 (defconst gtkdoc-font-lock-keywords
2551 `((,(lambda (limit)
2552 (c-font-lock-doc-comments "/\\*\\*$" limit
2553 gtkdoc-font-lock-doc-comments)
2554 (c-font-lock-doc-comments "/\\*< " limit
2555 gtkdoc-font-lock-doc-protection)
2556 ))))
2558 ;; Javadoc.
2560 (defconst javadoc-font-lock-doc-comments
2561 `(("{@[a-z]+[^}\n\r]*}" ; "{@foo ...}" markup.
2562 0 ,c-doc-markup-face-name prepend nil)
2563 ("^\\(/\\*\\)?\\(\\s \\|\\*\\)*\\(@[a-z]+\\)" ; "@foo ..." markup.
2564 3 ,c-doc-markup-face-name prepend nil)
2565 (,(concat "</?\\sw" ; HTML tags.
2566 "\\("
2567 (concat "\\sw\\|\\s \\|[=\n\r*.:]\\|"
2568 "\"[^\"]*\"\\|'[^']*'")
2569 "\\)*>")
2570 0 ,c-doc-markup-face-name prepend nil)
2571 ("&\\(\\sw\\|[.:]\\)+;" ; HTML entities.
2572 0 ,c-doc-markup-face-name prepend nil)
2573 ;; Fontify remaining markup characters as invalid. Note
2574 ;; that the Javadoc spec is hazy about when "@" is
2575 ;; allowed in non-markup use.
2576 (,(lambda (limit)
2577 (c-find-invalid-doc-markup "[<>&]\\|{@" limit))
2578 0 'font-lock-warning-face prepend nil)))
2580 (defconst javadoc-font-lock-keywords
2581 `((,(lambda (limit)
2582 (c-font-lock-doc-comments "/\\*\\*" limit
2583 javadoc-font-lock-doc-comments)))))
2585 ;; Pike autodoc.
2587 (defconst autodoc-decl-keywords
2588 ;; Adorned regexp matching the keywords that introduce declarations
2589 ;; in Pike Autodoc.
2590 (cc-eval-when-compile
2591 (c-make-keywords-re t '("@decl" "@elem" "@index" "@member") 'pike-mode)))
2593 (defconst autodoc-decl-type-keywords
2594 ;; Adorned regexp matching the keywords that are followed by a type.
2595 (cc-eval-when-compile
2596 (c-make-keywords-re t '("@elem" "@member") 'pike-mode)))
2598 (defun autodoc-font-lock-line-markup (limit)
2599 ;; Fontify all line oriented keywords between the point and LIMIT.
2600 ;; Nil is always returned.
2602 ;; This function might do hidden buffer changes.
2604 (let ((line-re (concat "^\\(\\(/\\*!\\|\\s *\\("
2605 c-current-comment-prefix
2606 "\\)\\)\\s *\\)@[A-Za-z_-]+\\(\\s \\|$\\)"))
2607 (markup-faces (list c-doc-markup-face-name c-doc-face-name)))
2609 (while (re-search-forward line-re limit t)
2610 (goto-char (match-end 1))
2612 (if (looking-at autodoc-decl-keywords)
2613 (let* ((kwd-pos (point))
2614 (start (match-end 1))
2615 (pos start)
2616 end)
2618 (c-put-font-lock-face (point) pos markup-faces)
2620 ;; Put a declaration end mark at the markup keyword and
2621 ;; remove the faces from the rest of the line so that it
2622 ;; gets refontified as a declaration later on by
2623 ;; `c-font-lock-declarations'.
2624 (c-put-char-property (1- pos) 'c-type 'c-decl-end)
2625 (goto-char pos)
2626 (while (progn
2627 (end-of-line)
2628 (setq end (point))
2629 (and (eq (char-before) ?@)
2630 (not (eobp))
2631 (progn (forward-char)
2632 (skip-syntax-forward " ")
2633 (looking-at c-current-comment-prefix))))
2634 (goto-char (match-end 0))
2635 (c-remove-font-lock-face pos (1- end))
2636 (c-put-font-lock-face (1- end) end markup-faces)
2637 (setq pos (point)))
2639 ;; Include the final newline in the removed area. This
2640 ;; has no visual effect but it avoids some tricky special
2641 ;; cases in the testsuite wrt the differences in string
2642 ;; fontification in Emacs vs XEmacs.
2643 (c-remove-font-lock-face pos (min (1+ (point)) (point-max)))
2645 ;; Must handle string literals explicitly inside the declaration.
2646 (goto-char start)
2647 (while (re-search-forward
2648 "\"\\([^\\\"]\\|\\\\.\\)*\"\\|'\\([^\\']\\|\\\\.\\)*'"
2649 end 'move)
2650 (c-put-font-lock-string-face (match-beginning 0)
2651 (point)))
2653 ;; Fontify types after keywords that always are followed
2654 ;; by them.
2655 (goto-char kwd-pos)
2656 (when (looking-at autodoc-decl-type-keywords)
2657 (c-fontify-types-and-refs ((c-promote-possible-types t))
2658 (goto-char start)
2659 (c-forward-syntactic-ws)
2660 (c-forward-type))))
2662 ;; Mark each whole line as markup, as long as the logical line
2663 ;; continues.
2664 (while (progn
2665 (c-put-font-lock-face (point)
2666 (progn (end-of-line) (point))
2667 markup-faces)
2668 (and (eq (char-before) ?@)
2669 (not (eobp))
2670 (progn (forward-char)
2671 (skip-syntax-forward " ")
2672 (looking-at c-current-comment-prefix))))
2673 (goto-char (match-end 0))))))
2675 nil)
2677 (defconst autodoc-font-lock-doc-comments
2678 `(("@\\(\\w+{\\|\\[\\([^\]@\n\r]\\|@@\\)*\\]\\|[@}]\\|$\\)"
2679 ;; In-text markup.
2680 0 ,c-doc-markup-face-name prepend nil)
2681 (autodoc-font-lock-line-markup)
2682 ;; Fontify remaining markup characters as invalid.
2683 (,(lambda (limit)
2684 (c-find-invalid-doc-markup "@" limit))
2685 0 'font-lock-warning-face prepend nil)
2688 (defun autodoc-font-lock-keywords ()
2689 ;; Note that we depend on that `c-current-comment-prefix' has got
2690 ;; its proper value here.
2692 ;; This function might do hidden buffer changes.
2694 ;; The `c-type' text property with `c-decl-end' is used to mark the
2695 ;; end of the `autodoc-decl-keywords' occurrences to fontify the
2696 ;; following declarations.
2697 (setq c-type-decl-end-used t)
2699 `((,(lambda (limit)
2700 (c-font-lock-doc-comments "/[*/]!" limit
2701 autodoc-font-lock-doc-comments)))))
2704 ;; 2006-07-10: awk-font-lock-keywords has been moved back to cc-awk.el.
2705 (cc-provide 'cc-fonts)
2707 ;;; Local Variables:
2708 ;;; indent-tabs-mode: t
2709 ;;; tab-width: 8
2710 ;;; End:
2711 ;;; cc-fonts.el ends here