From bf2c1571f4085d3b52f37370f0bcdf7a23c53a50 Mon Sep 17 00:00:00 2001 From: Alan Mackenzie Date: Mon, 18 Jul 2011 17:15:24 +0000 Subject: [PATCH] CC Mode: Fontify declarators properly when, e.g., a jit-lock chunk begins inside a declaration. Changed cc-engine.el, cc-langs.el, cc-fonts.el. --- lisp/ChangeLog | 16 +++++++++++++ lisp/progmodes/cc-engine.el | 8 ++++--- lisp/progmodes/cc-fonts.el | 47 +++++++++++++++++++++++++++++++++++++ lisp/progmodes/cc-langs.el | 2 +- 4 files changed, 69 insertions(+), 4 deletions(-) diff --git a/lisp/ChangeLog b/lisp/ChangeLog index f320398975c..0ea3d94a01f 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,19 @@ +2011-07-18 Alan Mackenzie + + Fontify declarators properly when, e.g., a jit-lock chunk begins + inside a declaration. + + * progmodes/cc-langs.el (c-symbol-chars): Correct a typo. + + * progmodes/cc-fonts.el (c-font-lock-enclosing-decls): New + function. + (c-complex-decl-matchers): Insert reference to + c-font-lock-enclosing-decls. + + * progmodes/cc-engine.el (c-backward-single-comment): + (c-backward-comments): Bind open-paren-in-column-0-is-defun-start + to nil around calls to (forward-comment -1). + 2011-07-17 Lars Magne Ingebrigtsen * image.el (put-image): Doc typo fix. diff --git a/lisp/progmodes/cc-engine.el b/lisp/progmodes/cc-engine.el index 38f66b4504e..a6fd28be21d 100644 --- a/lisp/progmodes/cc-engine.el +++ b/lisp/progmodes/cc-engine.el @@ -1301,12 +1301,13 @@ This function does not do any hidden buffer changes." ;; same line. (re-search-forward "\\=\\s *[\n\r]" start t) - (if (if (forward-comment -1) + (if (if (let (open-paren-in-column-0-is-defun-start) (forward-comment -1)) (if (eolp) ;; If forward-comment above succeeded and we're at eol ;; then the newline we moved over above didn't end a ;; line comment, so we give it another go. - (forward-comment -1) + (let (open-paren-in-column-0-is-defun-start) + (forward-comment -1)) t)) ;; Emacs <= 20 and XEmacs move back over the closer of a @@ -1333,7 +1334,8 @@ comment at the start of cc-engine.el for more info." ;; return t when moving backwards at bob. (not (bobp)) - (if (forward-comment -1) + (if (let (open-paren-in-column-0-is-defun-start) + (forward-comment -1)) (if (looking-at "\\*/") ;; Emacs <= 20 and XEmacs move back over the ;; closer of a block comment that lacks an opener. diff --git a/lisp/progmodes/cc-fonts.el b/lisp/progmodes/cc-fonts.el index 600bbc76e9a..0500d48ddbc 100644 --- a/lisp/progmodes/cc-fonts.el +++ b/lisp/progmodes/cc-fonts.el @@ -1346,6 +1346,50 @@ casts and declarations are fontified. Used on level 2 and higher." (c-font-lock-declarators limit t nil))) nil) +(defun c-font-lock-enclosing-decls (limit) + ;; Fontify the declarators of (nested) declarations we're in the middle of. + ;; This is mainly for when a jit-lock etc. chunk starts inside the brace + ;; block of a struct/union/class, etc. + ;; + ;; This function will be called from font-lock for a region bounded by POINT + ;; and LIMIT, as though it were to identify a keyword for + ;; font-lock-keyword-face. It always returns NIL to inhibit this and + ;; 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? + (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) + in-typedef (looking-at c-typedef-key)) + (if in-typedef (c-forward-token-2)) + (when (looking-at c-opt-block-decls-with-vars-key) + (goto-char ps-elt) + (when (c-safe (c-forward-sexp)) + (c-forward-syntactic-ws) + (c-font-lock-declarators limit t in-typedef))))))) + (c-lang-defconst c-simple-decl-matchers "Simple font lock matchers for types and declarations. These are used on level 2 only and so aren't combined with `c-complex-decl-matchers'." @@ -1452,6 +1496,9 @@ on level 2 only and so aren't combined with `c-complex-decl-matchers'." ;; Fontify all declarations, casts and normal labels. c-font-lock-declarations + ;; Fontify declarators when POINT is within their declaration. + c-font-lock-enclosing-decls + ;; Fontify angle bracket arglists like templates in C++. ,@(when (c-lang-const c-recognize-<>-arglists) `(c-font-lock-<>-arglists)) diff --git a/lisp/progmodes/cc-langs.el b/lisp/progmodes/cc-langs.el index a6459e1724f..9ce23a080d6 100644 --- a/lisp/progmodes/cc-langs.el +++ b/lisp/progmodes/cc-langs.el @@ -523,7 +523,7 @@ operator at the top level." (c-lang-defconst c-symbol-chars "Set of characters that can be part of a symbol. -This is on the form that fits inside [ ] in a regexp." +This is of the form that fits inside [ ] in a regexp." ;; Pike note: With the backquote identifiers this would include most ;; operator chars too, but they are handled with other means instead. t (concat c-alnum "_$") -- 2.39.2