;;; cc-fonts.el --- font lock support for CC Mode
-;; Copyright (C) 2002-2011 Free Software Foundation, Inc.
+;; Copyright (C) 2002-2015 Free Software Foundation, Inc.
;; Authors: 2003- Alan Mackenzie
;; 2002- Martin Stjernholm
'font-lock-negation-char-face))
(cc-bytecomp-defun face-inverse-video-p) ; Only in Emacs.
-(cc-bytecomp-defun face-property-instance) ; Only in XEmacs.
(defun c-make-inverse-face (oldface newface)
;; Emacs and XEmacs have completely different face manipulation
(unless (face-property-instance oldface 'reverse)
(invert-face newface)))))
-(defvar c-annotation-face (make-face 'c-annotation-face)
- "Face used to highlight annotations in java-mode and other modes that may wish to use it.")
-(set-face-foreground 'c-annotation-face "blue")
+(defvar c-annotation-face 'c-annotation-face)
+
+(defface c-annotation-face
+ '((default :inherit font-lock-constant-face))
+ "Face for highlighting annotations in Java mode and similar modes."
+ :version "24.1"
+ :group 'c)
(eval-and-compile
;; We need the following definitions during compilation since they're
;; This function might do hidden buffer changes.
(when (c-got-face-at (point) c-literal-faces)
(while (progn
- (goto-char (next-single-property-change
+ (goto-char (c-next-single-property-change
(point) 'face nil limit))
(and (< (point) limit)
(c-got-face-at (point) c-literal-faces))))
(match-beginning ,(car highlight))
(match-end ,(car highlight))
,(elt highlight 1))))
- ;; highlight is an "ANCHORED HIGHLIGHER" of the form
+ ;; highlight is an "ANCHORED HIGHLIGHTER" of the form
;; (ANCHORED-MATCHER PRE-FORM POST-FORM SUBEXP-HIGHLIGHTERS...)
(when (nth 3 highlight)
(error "Match highlights currently not supported in %s"
(parse-sexp-lookup-properties
(cc-eval-when-compile
(boundp 'parse-sexp-lookup-properties))))
-
- ;; (while (re-search-forward ,regexp limit t)
- ;; (unless (progn
- ;; (goto-char (match-beginning 0))
- ;; (c-skip-comments-and-strings limit))
- ;; (goto-char (match-end 0))
- ;; ,@(mapcar
- ;; (lambda (highlight)
- ;; (if (integerp (car highlight))
- ;; (progn
- ;; (unless (eq (nth 2 highlight) t)
- ;; (error
- ;; "The override flag must currently be t in %s"
- ;; highlight))
- ;; (when (nth 3 highlight)
- ;; (error
- ;; "The laxmatch flag may currently not be set in %s"
- ;; highlight))
- ;; `(save-match-data
- ;; (c-put-font-lock-face
- ;; (match-beginning ,(car highlight))
- ;; (match-end ,(car highlight))
- ;; ,(elt highlight 1))))
- ;; (when (nth 3 highlight)
- ;; (error "Match highlights currently not supported in %s"
- ;; highlight))
- ;; `(progn
- ;; ,(nth 1 highlight)
- ;; (save-match-data ,(car highlight))
- ;; ,(nth 2 highlight))))
- ;; highlights)))
,(c-make-font-lock-search-form regexp highlights))
-
nil)))
(defun c-make-font-lock-BO-decl-search-function (regexp &rest highlights)
;; `parse-sexp-lookup-properties' (when it exists).
(parse-sexp-lookup-properties
(cc-eval-when-compile
- (boundp 'parse-sexp-lookup-properties))))
+ (boundp 'parse-sexp-lookup-properties)))
+ (BOD-limit
+ (c-determine-limit 1000)))
(goto-char
(let ((here (point)))
- (if (eq (car (c-beginning-of-decl-1)) 'same)
+ (if (eq (car (c-beginning-of-decl-1 BOD-limit)) 'same)
(point)
here)))
,(c-make-font-lock-search-form regexp highlights))
(progn
(c-mark-<-as-paren beg)
(c-mark->-as-paren end))
- ;; (c-clear-char-property beg 'syntax-table)
- (c-clear-char-property beg 'category)))
+ (c-unmark-<->-as-paren beg)))
nil)))))))
;; #define.
(let ((start (1- (point))))
(save-excursion
(and (eq (elt (parse-partial-sexp start (c-point 'eol)) 8) start)
- (if (integerp c-multiline-string-start-char)
+ (if (if (eval-when-compile (integerp ?c))
+ ;; Emacs
+ (integerp c-multiline-string-start-char)
+ ;; XEmacs
+ (characterp c-multiline-string-start-char))
;; There's no multiline string start char before the
;; string, so newlines aren't allowed.
(not (eq (char-before start) c-multiline-string-start-char))
paren-depth
id-face got-init
c-last-identifier-range
- (separator-prop (if types 'c-decl-type-start 'c-decl-id-start)))
+ (separator-prop (if types 'c-decl-type-start 'c-decl-id-start))
+ brackets-after-id)
;; The following `while' fontifies a single declarator id each time round.
;; It loops only when LIST is non-nil.
;; Inside the following "condition form", we move forward over the
;; declarator's identifier up as far as any opening bracket (for array
;; size) or paren (for parameters of function-type) or brace (for
- ;; array/struct initialisation) or "=" or terminating delimiter
+ ;; array/struct initialization) or "=" or terminating delimiter
;; (e.g. "," or ";" or "}").
(and
pos
(<= (point) limit))
;; Search syntactically to the end of the declarator (";",
- ;; ",", a closen paren, eob etc) or to the beginning of an
+ ;; ",", a closing paren, eob etc) or to the beginning of an
;; initializer or function prototype ("=" or "\\s\(").
- ;; Note that the open paren will match array specs in
- ;; square brackets, and we treat them as initializers too.
- (c-syntactic-re-search-forward
- "[;,]\\|\\s)\\|\\'\\|\\(=\\|\\s(\\)" limit t t))
+ ;; Note that square brackets are now not also treated as
+ ;; initializers, since this broke when there were also
+ ;; initializing brace lists.
+ (let (found)
+ (while
+ (and (setq found (c-syntactic-re-search-forward
+ "[;,]\\|\\s)\\|\\'\\|\\(=\\|\\s(\\)" limit t t))
+ (eq (char-before) ?\[)
+ (c-go-up-list-forward))
+ (setq brackets-after-id t))
+ found))
(setq next-pos (match-beginning 0)
id-face (if (and (eq (char-after next-pos) ?\()
- (let (c-last-identifier-range)
- (save-excursion
- (goto-char next-pos)
- (c-at-toplevel-p))))
+ (not brackets-after-id))
'font-lock-function-name-face
'font-lock-variable-name-face)
got-init (and (match-beginning 1)
(char-after (match-beginning 1))))
(if types
- ;; Register and fontify the identifer as a type.
+ ;; Register and fontify the identifier as a type.
(let ((c-promote-possible-types t))
(goto-char id-start)
(c-forward-type))
(when list
;; Jump past any initializer or function prototype to see if
;; there's a ',' to continue at.
-
(cond ((eq id-face 'font-lock-function-name-face)
;; Skip a parenthesized initializer (C++) or a function
;; prototype.
;; o - '<> if the arglist is of angle bracket type;
;; o - 'arglist if it's some other arglist;
;; o - nil, if not in an arglist at all. This includes the
- ;; parenthesised condition which follows "if", "while", etc.
+ ;; parenthesized condition which follows "if", "while", etc.
context
- ;; The position of the next token after the closing paren of
- ;; the last detected cast.
+ ;; A list of starting positions of possible type declarations, or of
+ ;; the typedef preceding one, if any.
last-cast-end
;; The result from `c-forward-decl-or-cast-1'.
decl-or-cast
;; it finds any. That's necessary so that we later will
;; stop inside them to fontify types there.
(c-parse-and-markup-<>-arglists t)
+ lbrace ; position of some {.
;; The font-lock package in Emacs is known to clobber
;; `parse-sexp-lookup-properties' (when it exists).
(parse-sexp-lookup-properties
c-font-lock-maybe-decl-faces
(lambda (match-pos inside-macro)
+ ;; Note to maintainers: don't use `limit' inside this lambda form;
+ ;; c-find-decl-spots sometimes narrows to less than `limit'.
(setq start-pos (point))
(when
;; The result of the form below is true when we don't recognize a
;; declaration or cast.
- (if (and (eq (get-text-property (point) 'face)
- 'font-lock-keyword-face)
- (looking-at c-not-decl-init-keywords))
+ (if (or (and (eq (get-text-property (point) 'face)
+ 'font-lock-keyword-face)
+ (looking-at c-not-decl-init-keywords))
+ (and c-macro-with-semi-re
+ (looking-at c-macro-with-semi-re))) ; 2008-11-04
;; Don't do anything more if we're looking at a keyword that
;; can't start a declaration.
t
(cond ((not (memq (char-before match-pos) '(?\( ?, ?\[ ?<)))
(setq context nil
c-restricted-<>-arglists nil))
- ;; A control flow expression
+ ;; A control flow expression or a decltype
((and (eq (char-before match-pos) ?\()
(save-excursion
(goto-char match-pos)
(backward-char)
(c-backward-token-2)
- (looking-at c-block-stmt-2-key)))
+ (or (looking-at c-block-stmt-2-key)
+ (looking-at c-block-stmt-1-2-key)
+ (looking-at c-typeof-key))))
(setq context nil
c-restricted-<>-arglists t))
;; Near BOB.
(or (looking-at c-typedef-key)
(goto-char start-pos)))
- ;; Now analyze the construct.
;; In QT, "more" is an irritating keyword that expands to nothing.
;; We skip over it to prevent recognition of "more slots: <symbol>"
;; as a bitfield declaration.
(concat "\\(more\\)\\([^" c-symbol-chars "]\\|$\\)")))
(goto-char (match-end 1))
(c-forward-syntactic-ws))
+
+ ;; Now analyze the construct.
(setq decl-or-cast (c-forward-decl-or-cast-1
match-pos context last-cast-end))
(c-fontify-recorded-types-and-refs)
nil)
+ ;; Restore point, since at this point in the code it has been
+ ;; left undefined by c-forward-decl-or-cast-1 above.
+ ((progn (goto-char start-pos) nil))
+
+ ;; If point is inside a bracelist, there's no point checking it
+ ;; being at a declarator.
+ ((let ((paren-state (c-parse-state)))
+ (setq lbrace (c-cheap-inside-bracelist-p paren-state)))
+ ;; Move past this bracelist to prevent an endless loop.
+ (goto-char lbrace)
+ (unless (c-safe (progn (forward-list) t))
+ (goto-char start-pos)
+ (c-forward-token-2))
+ nil)
+
+ ;; If point is just after a ")" which is followed by an
+ ;; identifier which isn't a label, or at the matching "(", we're
+ ;; at either a macro invocation, a cast, or a
+ ;; for/while/etc. statement. The cast case is handled above.
+ ;; None of these cases can contain a declarator.
+ ((or (and (eq (char-before match-pos) ?\))
+ (c-on-identifier)
+ (save-excursion (not (c-forward-label))))
+ (and (eq (char-after) ?\()
+ (save-excursion
+ (and
+ (progn (c-backward-token-2) (c-on-identifier))
+ (save-excursion (not (c-forward-label)))
+ (progn (c-backward-token-2)
+ (eq (char-after) ?\())))))
+ (c-forward-token-2) ; Must prevent looping.
+ nil)
+
+ ((and (not c-enums-contain-decls)
+ ;; An optimization quickly to eliminate scans of long enum
+ ;; declarations in the next cond arm.
+ (let ((paren-state (c-parse-state)))
+ (and
+ (numberp (car paren-state))
+ (save-excursion
+ (goto-char (car paren-state))
+ (c-backward-over-enum-header)))))
+ (c-forward-token-2)
+ nil)
+
(t
;; Are we at a declarator? Try to go back to the declaration
;; to check this. If we get there, check whether a "typedef"
;; is there, then fontify the declarators accordingly.
- (let ((decl-search-lim (max (- (point) 50000) (point-min)))
+ (let ((decl-search-lim (c-determine-limit 1000))
paren-state bod-res encl-pos is-typedef
c-recognize-knr-p) ; Strictly speaking, bogus, but it
; speeds up lisp.h tremendously.
(save-excursion
- (setq bod-res (car (c-beginning-of-decl-1 decl-search-lim)))
- (if (and (eq bod-res 'same)
- (progn
- (c-backward-syntactic-ws)
- (eq (char-before) ?\})))
- (c-beginning-of-decl-1 decl-search-lim))
-
- ;; We're now putatively at the declaration.
- (setq paren-state (c-parse-state))
- ;; At top level or inside a "{"?
- (if (or (not (setq encl-pos
- (c-most-enclosing-brace paren-state)))
- (eq (char-after encl-pos) ?\{))
- (progn
- (when (looking-at c-typedef-key) ; "typedef"
- (setq is-typedef t)
- (goto-char (match-end 0))
- (c-forward-syntactic-ws))
- ;; At a real declaration?
- (if (memq (c-forward-type t) '(t known found))
- (progn
- (c-font-lock-declarators limit t is-typedef)
- nil)
- ;; False alarm. Return t to go on to the next check.
- (goto-char start-pos)
- t))
- t))))))
+ (if (c-back-over-member-initializers)
+ t ; Can't be at a declarator
+ (unless (or (eobp)
+ (looking-at "\\s(\\|\\s)"))
+ (forward-char))
+ (setq bod-res (car (c-beginning-of-decl-1 decl-search-lim)))
+ (if (and (eq bod-res 'same)
+ (save-excursion
+ (c-backward-syntactic-ws)
+ (eq (char-before) ?\})))
+ (c-beginning-of-decl-1 decl-search-lim))
+
+ ;; We're now putatively at the declaration.
+ (setq paren-state (c-parse-state))
+ ;; At top level or inside a "{"?
+ (if (or (not (setq encl-pos
+ (c-most-enclosing-brace paren-state)))
+ (eq (char-after encl-pos) ?\{))
+ (progn
+ (when (looking-at c-typedef-key) ; "typedef"
+ (setq is-typedef t)
+ (goto-char (match-end 0))
+ (c-forward-syntactic-ws))
+ ;; At a real declaration?
+ (if (memq (c-forward-type t) '(t known found decltype))
+ (progn
+ (c-font-lock-declarators (point-max) t is-typedef)
+ nil)
+ ;; False alarm. Return t to go on to the next check.
+ (goto-char start-pos)
+ t))
+ t)))))))
;; It was a false alarm. Check if we're in a label (or other
;; construct with `:' except bitfield) instead.
;; Note that this function won't attempt to fontify beyond the end of the
;; current enum block, if any.
(let* ((paren-state (c-parse-state))
- (encl-pos (c-most-enclosing-brace paren-state))
- (start (point))
- )
+ (encl-pos (c-most-enclosing-brace paren-state)))
(when (and
encl-pos
(eq (char-after encl-pos) ?\{)
(save-excursion
(goto-char encl-pos)
- (c-backward-syntactic-ws)
- (c-simple-skip-symbol-backward)
- (or (looking-at c-brace-list-key) ; "enum"
- (progn (c-backward-syntactic-ws)
- (c-simple-skip-symbol-backward)
- (looking-at c-brace-list-key)))))
+ (c-backward-over-enum-header)))
(c-syntactic-skip-backward "^{," nil t)
(c-put-char-property (1- (point)) 'c-type 'c-decl-id-start)
;; prevent a repeat invocation. See elisp/lispref page "Search-based
;; Fontification".
(let* ((paren-state (c-parse-state))
- (start (point))
- decl-context bo-decl in-typedef type-type ps-elt)
-
- ;; First, are we actually in a "local" declaration?
- (setq decl-context (c-beginning-of-decl-1)
- bo-decl (point)
- in-typedef (looking-at c-typedef-key))
- (if in-typedef (c-forward-token-2))
- (when (and (eq (car decl-context) 'same)
- (< bo-decl start))
- ;; Are we genuinely at a type?
- (setq type-type (c-forward-type t))
- (if (and type-type
- (or (not (eq type-type 'maybe))
- (looking-at c-symbol-key)))
- (c-font-lock-declarators limit t in-typedef)))
-
- ;; Secondly, are we in any nested struct/union/class/etc. braces?
+ (decl-search-lim (c-determine-limit 1000))
+ decl-context in-typedef ps-elt)
+ ;; Are we in any nested struct/union/class/etc. braces?
(while paren-state
(setq ps-elt (car paren-state)
paren-state (cdr paren-state))
(when (and (atom ps-elt)
(eq (char-after ps-elt) ?\{))
(goto-char ps-elt)
- (setq decl-context (c-beginning-of-decl-1)
+ (setq decl-context (c-beginning-of-decl-1 decl-search-lim)
in-typedef (looking-at c-typedef-key))
(if in-typedef (c-forward-token-2))
(when (and c-opt-block-decls-with-vars-key
"\\)\\>"
;; Disallow various common punctuation chars that can't come
;; before the '{' of the enum list, to avoid searching too far.
- "[^\]\[{}();,/#=]*"
+ "[^\]\[{}();/#=]*"
"{")
'((c-font-lock-declarators limit t nil)
(save-match-data
(defconst c-font-lock-keywords-3 (c-lang-const c-matchers-3 c)
"Accurate normal font locking for C mode.
-Like `c-font-lock-keywords-2' but detects declarations in a more
+Like the variable `c-font-lock-keywords-2' but detects declarations in a more
accurate way that works in most cases for arbitrary types without the
need for `c-font-lock-extra-types'.")
;; Got two parenthesized expressions, so we have to look
;; closer at them to decide which is the type. No need to
;; handle `c-record-ref-identifiers' since all references
- ;; has already been handled by other fontification rules.
+ ;; have already been handled by other fontification rules.
(let (expr1-res expr2-res)
(goto-char expr1-pos)
;; unusual than an initializer.
(cond ((memq expr1-res '(t known prefix)))
((memq expr2-res '(t known prefix)))
+ ;; Presumably 'decltype's will be fontified elsewhere.
+ ((eq expr1-res 'decltype))
+ ((eq expr2-res 'decltype))
((eq expr1-res 'found)
(let ((c-promote-possible-types t))
(goto-char expr1-pos)
(defconst c++-font-lock-keywords-3 (c-lang-const c-matchers-3 c++)
"Accurate normal font locking for C++ mode.
-Like `c++-font-lock-keywords-2' but detects declarations in a more
+Like the variable `c++-font-lock-keywords-2' but detects declarations in a more
accurate way that works in most cases for arbitrary types without the
need for `c++-font-lock-extra-types'.")
(defconst objc-font-lock-keywords-3 (c-lang-const c-matchers-3 objc)
"Accurate normal font locking for Objective-C mode.
-Like `objc-font-lock-keywords-2' but detects declarations in a more
+Like the variable `objc-font-lock-keywords-2' but detects declarations in a more
accurate way that works in most cases for arbitrary types without the
need for `objc-font-lock-extra-types'.")
(defconst java-font-lock-keywords-3 (c-lang-const c-matchers-3 java)
"Accurate normal font locking for Java mode.
-Like `java-font-lock-keywords-2' but detects declarations in a more
+Like variable `java-font-lock-keywords-2' but detects declarations in a more
accurate way that works in most cases for arbitrary types without the
need for `java-font-lock-extra-types'.")
(defconst idl-font-lock-keywords-3 (c-lang-const c-matchers-3 idl)
"Accurate normal font locking for CORBA IDL mode.
-Like `idl-font-lock-keywords-2' but detects declarations in a more
+Like the variable `idl-font-lock-keywords-2' but detects declarations in a more
accurate way that works in most cases for arbitrary types without the
need for `idl-font-lock-extra-types'.")
(defconst pike-font-lock-keywords-3 (c-lang-const c-matchers-3 pike)
"Accurate normal font locking for Pike mode.
-Like `pike-font-lock-keywords-2' but detects declarations in a more
+Like the variable `pike-font-lock-keywords-2' but detects declarations in a more
accurate way that works in most cases for arbitrary types without the
need for `pike-font-lock-extra-types'.")
(setq comment-beg nil))
(setq region-beg comment-beg))
- (if (eq (elt (parse-partial-sexp comment-beg (+ comment-beg 2)) 7) t)
+ (if (elt (parse-partial-sexp comment-beg (+ comment-beg 2)) 7)
;; Collect a sequence of doc style line comments.
(progn
(goto-char comment-beg)
;; 2006-07-10: awk-font-lock-keywords has been moved back to cc-awk.el.
(cc-provide 'cc-fonts)
+;;; Local Variables:
+;;; indent-tabs-mode: t
+;;; tab-width: 8
+;;; End:
;;; cc-fonts.el ends here