+ (setq c-in-after-change-fontification t)
+ (save-excursion
+ (mapc (lambda (fn)
+ (funcall fn beg end old-len))
+ c-before-font-lock-functions))))))
+
+(defun c-set-fl-decl-start (pos)
+ ;; If the beginning of the line containing POS is in the middle of a "local"
+ ;; declaration (i.e. one which does not start outside of braces enclosing
+ ;; POS, such as a struct), return the beginning of that declaration.
+ ;; Otherwise return POS. Note that declarations, in this sense, can be
+ ;; nested.
+ ;;
+ ;; This function is called indirectly from font locking stuff - either from
+ ;; c-after-change (to prepare for after-change font-locking) or from font
+ ;; lock context (etc.) fontification.
+ (let ((lit-limits (c-literal-limits))
+ (new-pos pos)
+ bod-lim bo-decl)
+ (goto-char (c-point 'bol new-pos))
+ (when lit-limits ; Comment or string.
+ (goto-char (car lit-limits)))
+ (setq bod-lim (max (- (point) 500) (point-min)))
+
+ (while
+ ;; Go to a less nested declaration each time round this loop.
+ (and
+ (eq (car (c-beginning-of-decl-1 bod-lim)) 'same)
+ (progn (setq bo-decl (point))
+ ;; Are we looking at a keyword such as "template" or
+ ;; "typedef" which can decorate a type, or the type itself?
+ (when (or (looking-at c-prefix-spec-kwds-re)
+ (c-forward-type t))
+ ;; We've found another candidate position.
+ (setq new-pos (min new-pos bo-decl))
+ (goto-char bo-decl))
+ t)
+ ;; Try and go out a level to search again.
+ (progn
+ (c-backward-syntactic-ws bod-lim)
+ (or (memq (char-before) '(?\( ?\[))
+ (and (eq (char-before) ?\<)
+ (eq (c-get-char-property
+ (1- (point)) 'syntax-table)
+ c-<-as-paren-syntax))))
+ (not (bobp)))
+ (backward-char))
+ new-pos)) ; back over (, [, <.
+
+(defun c-change-set-fl-decl-start (beg end old-len)
+ ;; Set c-new-BEG to the beginning of a "local" declaration if it('s BOL) is
+ ;; inside one. This is called from an after-change-function, but the
+ ;; parameters BEG END and OLD-LEN are ignored. See `c-set-fl-decl-start'
+ ;; for the detailed functionality.
+ (if font-lock-mode
+ (setq c-new-BEG (c-set-fl-decl-start c-new-BEG))))
+
+(defun c-context-set-fl-decl-start (beg end)
+ ;; Return a cons (NEW-BEG . END), where NEW-BEG is the beginning of a
+ ;; "local" declaration (BOL at) NEW is inside or BEG. See
+ ;; `c-set-fl-decl-start' for the detailed functionality.
+ (cons (c-set-fl-decl-start beg) end))
+
+(defvar c-standard-font-lock-fontify-region-function nil
+ "Standard value of `font-lock-fontify-region-function'")
+
+(defun c-font-lock-fontify-region (beg end &optional verbose)
+ ;; Effectively advice around `font-lock-fontify-region' which extends the
+ ;; region (BEG END), for example, to avoid context fontification chopping
+ ;; off the start of the context. Do not do anything if it's already been
+ ;; done (i.e. from an after-change fontification. An example (C++) where
+ ;; this used to happen is this:
+ ;;
+ ;; template <typename T>
+ ;;
+ ;;
+ ;; void myfunc(T* p) {}
+ ;;
+ ;; Type a space in the first blank line, and the fontification of the next
+ ;; line was fouled up by context fontification.
+ (let ((new-beg beg) (new-end end) new-region)
+ (if c-in-after-change-fontification
+ (setq c-in-after-change-fontification nil)
+ (save-restriction
+ (widen)
+ (save-excursion
+ (mapc (lambda (fn)
+ (setq new-region (funcall fn new-beg new-end))
+ (setq new-beg (car new-region) new-end (cdr new-region)))
+ c-before-context-fontification-functions))))
+ (funcall c-standard-font-lock-fontify-region-function
+ new-beg new-end verbose)))