Add 2011 to FSF/AIST copyright years.
[bpt/emacs.git] / lisp / progmodes / cc-mode.el
index 00ec64a..798d78c 100644 (file)
@@ -1,13 +1,14 @@
 ;;; cc-mode.el --- major mode for editing C and similar languages
 
 ;; Copyright (C) 1985, 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
-;;   1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
+;;   1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
 ;;   Free Software Foundation, Inc.
 
 ;; Authors:    2003- Alan Mackenzie
 ;;             1998- Martin Stjernholm
 ;;             1992-1999 Barry A. Warsaw
-;;             1987 Dave Detlefs and Stewart Clamen
+;;             1987 Dave Detlefs
+;;             1987 Stewart Clamen
 ;;             1985 Richard M. Stallman
 ;; Maintainer: bug-cc-mode@gnu.org
 ;; Created:    a long, long, time ago. adapted from the original c-mode.el
 
 ;; This file is part of GNU Emacs.
 
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs is free software: you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 3, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -26,9 +27,7 @@
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with this program; see the file COPYING.  If not, write to
-;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-;; Boston, MA 02110-1301, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; Commentary:
 
 
 ;;; Code:
 
+;; For Emacs < 22.2.
+(eval-and-compile
+  (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
+
 (eval-when-compile
   (let ((load-path
         (if (and (boundp 'byte-compile-dest-file)
 
 ;; Autoload directive for emacsen that doesn't have an older CC Mode
 ;; version in the dist.
-(autoload 'c-subword-mode "cc-subword"
+(autoload 'subword-mode "subword"
   "Mode enabling subword movement and editing keys." t)
 
 ;; Load cc-fonts first after font-lock is loaded, since it isn't
@@ -190,7 +193,8 @@ control).  See \"cc-mode.el\" for more info."
            (run-hooks 'c-initialization-hook)
            ;; Fix obsolete variables.
            (if (boundp 'c-comment-continuation-stars)
-               (setq c-block-comment-prefix c-comment-continuation-stars))
+               (setq c-block-comment-prefix
+                     (symbol-value 'c-comment-continuation-stars)))
            (add-hook 'change-major-mode-hook 'c-leave-cc-mode-mode)
            (setq c-initialization-ok t))
        ;; Will try initialization hooks again if they failed.
@@ -211,12 +215,12 @@ control).  See \"cc-mode.el\" for more info."
     ;; function is called from top-level forms that are evaluated
     ;; while cc-bytecomp is active when one does M-x eval-buffer.
     (cond
-     ;; XEmacs
-     ((cc-bytecomp-fboundp 'set-keymap-parents)
-      (set-keymap-parents map c-mode-base-map))
      ;; Emacs
      ((cc-bytecomp-fboundp 'set-keymap-parent)
       (set-keymap-parent map c-mode-base-map))
+     ;; XEmacs
+     ((cc-bytecomp-fboundp 'set-keymap-parents)
+      (set-keymap-parents map c-mode-base-map))
      ;; incompatible
      (t (error "CC Mode is incompatible with this version of Emacs")))
     map))
@@ -282,8 +286,9 @@ control).  See \"cc-mode.el\" for more info."
 
   ;; RMS says don't make these the default.
   ;; (April 2006): RMS has now approved these commands as defaults.
-  (define-key c-mode-base-map "\e\C-a"    'c-beginning-of-defun)
-  (define-key c-mode-base-map "\e\C-e"    'c-end-of-defun)
+  (unless (memq 'argumentative-bod-function c-emacs-features)
+    (define-key c-mode-base-map "\e\C-a"    'c-beginning-of-defun)
+    (define-key c-mode-base-map "\e\C-e"    'c-end-of-defun))
 
   (define-key c-mode-base-map "\C-c\C-n"  'c-forward-conditional)
   (define-key c-mode-base-map "\C-c\C-p"  'c-backward-conditional)
@@ -374,7 +379,7 @@ control).  See \"cc-mode.el\" for more info."
   ;; conflicts with OOBR
   ;;(define-key c-mode-base-map "\C-c\C-v"  'c-version)
   ;; (define-key c-mode-base-map "\C-c\C-y"  'c-toggle-hungry-state)  Commented out by ACM, 2005-11-22.
-  (define-key c-mode-base-map "\C-c\C-w" 'c-subword-mode)
+  (define-key c-mode-base-map "\C-c\C-w" 'subword-mode)
   )
 
 ;; We don't require the outline package, but we configure it a bit anyway.
@@ -466,117 +471,6 @@ preferably use the `c-mode-menu' language constant directly."
 (defvar c-maybe-stale-found-type nil)
 (make-variable-buffer-local 'c-maybe-stale-found-type)
 
-(defun c-before-change (beg end)
-  ;; Function to be put on `before-change-function'.  Currently
-  ;; (2007-02) it is used only to remove stale entries from the
-  ;; `c-found-types' cache, and to record entries which a
-  ;; `c-after-change' function might confirm as stale.
-  ;; 
-  ;; Note that this function must be FAST rather than accurate.  Note
-  ;; also that it only has any effect when font locking is enabled.
-  ;; We exploit this by checking for font-lock-*-face instead of doing
-  ;; rigourous syntactic analysis.
-
-  ;; If either change boundary is wholly inside an identifier, delete
-  ;; it/them from the cache.  Don't worry about being inside a string
-  ;; or a comment - "wrongly" removing a symbol from `c-found-types'
-  ;; isn't critical.
-  (setq c-maybe-stale-found-type nil)
-  (save-restriction
-    (save-match-data
-      (widen)
-      (save-excursion
-       ;; Are we inserting/deleting stuff in the middle of an identifier?
-       (c-unfind-enclosing-token beg)
-       (c-unfind-enclosing-token end)
-       ;; Are we coalescing two tokens together, e.g. "fo o" -> "foo"?
-       (when (< beg end)
-         (c-unfind-coalesced-tokens beg end))
-       ;; Are we (potentially) disrupting the syntactic context which
-       ;; makes a type a type?  E.g. by inserting stuff after "foo" in
-       ;; "foo bar;", or before "foo" in "typedef foo *bar;"?
-       ;;
-       ;; We search for appropriate c-type properties "near" the change.
-       ;; First, find an appropriate boundary for this property search.
-       (let (lim
-             type type-pos
-             marked-id term-pos
-             (end1
-              (or (and (eq (get-text-property end 'face) 'font-lock-comment-face)
-                       (previous-single-property-change end 'face))
-                  end)))
-         (when (>= end1 beg) ; Don't hassle about changes entirely in comments.
-           ;; Find a limit for the search for a `c-type' property
-           (while
-               (and (/= (skip-chars-backward "^;{}") 0)
-                    (> (point) (point-min))
-                    (memq (c-get-char-property (1- (point)) 'face)
-                          '(font-lock-comment-face font-lock-string-face))))
-           (setq lim (max (point-min) (1- (point))))
-
-           ;; Look for the latest `c-type' property before end1
-           (when (and (> end1 (point-min))
-                      (setq type-pos
-                            (if (get-text-property (1- end1) 'c-type)
-                                end1
-                              (previous-single-property-change end1 'c-type nil lim))))
-             (setq type (get-text-property (max (1- type-pos) lim) 'c-type))
-
-             (when (memq type '(c-decl-id-start c-decl-type-start))
-               ;; Get the identifier, if any, that the property is on.
-               (goto-char (1- type-pos))
-               (setq marked-id
-                     (when (looking-at "\\(\\sw\\|\\s_\\)")
-                       (c-beginning-of-current-token)
-                       (buffer-substring-no-properties (point) type-pos)))
-
-               (goto-char end1)
-               (skip-chars-forward "^;{}") ; FIXME!!!  loop for comment, maybe
-               (setq lim (point))
-               (setq term-pos
-                     (or (next-single-property-change end 'c-type nil lim) lim))
-               (setq c-maybe-stale-found-type
-                     (list type marked-id
-                           type-pos term-pos
-                           (buffer-substring-no-properties type-pos term-pos)
-                           (buffer-substring-no-properties beg end)))))))))))
-
-(defun c-after-change (beg end old-len)
-  ;; Function put on `after-change-functions' to adjust various caches
-  ;; etc.  Prefer speed to finesse here, since there will be an order
-  ;; of magnitude more calls to this function than any of the
-  ;; functions that use the caches.
-  ;;
-  ;; Note that care must be taken so that this is called before any
-  ;; font-lock callbacks since we might get calls to functions using
-  ;; these caches from inside them, and we must thus be sure that this
-  ;; has already been executed.
-
-  (c-save-buffer-state ()
-    ;; When `combine-after-change-calls' is used we might get calls
-    ;; with regions outside the current narrowing.  This has been
-    ;; observed in Emacs 20.7.
-    (save-restriction
-      (save-match-data           ; c-recognize-<>-arglists changes match-data
-       (widen)
-
-       (when (> end (point-max))
-         ;; Some emacsen might return positions past the end. This has been
-         ;; observed in Emacs 20.7 when rereading a buffer changed on disk
-         ;; (haven't been able to minimize it, but Emacs 21.3 appears to
-         ;; work).
-         (setq end (point-max))
-         (when (> beg end)
-           (setq beg end)))
-
-       (c-trim-found-types beg end old-len) ; maybe we don't need all of these.
-       (c-invalidate-sws-region-after beg end)
-       (c-invalidate-state-cache beg)
-       (c-invalidate-find-decl-cache beg)
-
-       (when c-recognize-<>-arglists
-         (c-after-change-check-<>-operators beg end))))))
-
 (defun c-basic-common-init (mode default-style)
   "Do the necessary initialization for the syntax handling routines
 and the line breaking/filling code.  Intended to be used by other
@@ -624,7 +518,8 @@ that requires a literal mode spec at compile time."
   (setq fill-paragraph-function 'c-fill-paragraph)
 
   (when (or c-recognize-<>-arglists
-           (c-major-mode-is 'awk-mode))
+           (c-major-mode-is 'awk-mode)
+           (c-major-mode-is '(c-mode c++-mode objc-mode)))
     ;; We'll use the syntax-table text property to change the syntax
     ;; of some chars for this language, so do the necessary setup for
     ;; that.
@@ -643,19 +538,15 @@ that requires a literal mode spec at compile time."
       (make-local-variable 'lookup-syntax-properties)
       (setq lookup-syntax-properties t)))
 
-  ;; Use this in Emacs 21 to avoid meddling with the rear-nonsticky
+  ;; Use this in Emacs 21+ to avoid meddling with the rear-nonsticky
   ;; property on each character.
   (when (boundp 'text-property-default-nonsticky)
     (make-local-variable 'text-property-default-nonsticky)
-    (let ((elem (assq 'syntax-table text-property-default-nonsticky)))
-      (if elem
-         (setcdr elem t)
-       (setq text-property-default-nonsticky
-             (cons '(syntax-table . t)
-                   text-property-default-nonsticky))))
-    (setq text-property-default-nonsticky
-         (cons '(c-type . t)
-               text-property-default-nonsticky)))
+    (mapc (lambda (tprop)
+           (unless (assq tprop text-property-default-nonsticky)
+             (setq text-property-default-nonsticky
+                   (cons `(,tprop . t) text-property-default-nonsticky))))
+         '(syntax-table category c-type)))
 
   ;; In Emacs 21 and later it's possible to turn off the ad-hoc
   ;; heuristic that open parens in column 0 are defun starters.  Since
@@ -709,35 +600,10 @@ that requires a literal mode spec at compile time."
   (make-local-hook 'before-change-functions)
   (add-hook 'before-change-functions 'c-before-change nil t)
   (make-local-hook 'after-change-functions)
-  (add-hook 'after-change-functions 'c-after-change nil t))
-
-(defun c-after-font-lock-init ()
-  ;; Put on `font-lock-mode-hook'.
-  (remove-hook 'after-change-functions 'c-after-change t)
-  (add-hook 'after-change-functions 'c-after-change nil t))
-
-(defun c-font-lock-init ()
-  "Set up the font-lock variables for using the font-lock support in CC Mode.
-This does not load the font-lock package.  Use after
-`c-basic-common-init' and after cc-fonts has been loaded."
-
-  (make-local-variable 'font-lock-defaults)
-  (setq font-lock-defaults
-       `(,(if (c-major-mode-is 'awk-mode)
-              ;; awk-mode currently has only one font lock level.
-              'awk-font-lock-keywords
-            (mapcar 'c-mode-symbol
-                    '("font-lock-keywords" "font-lock-keywords-1"
-                      "font-lock-keywords-2" "font-lock-keywords-3")))
-         nil nil
-         ,c-identifier-syntax-modifications
-         c-beginning-of-syntax
-         (font-lock-lines-before . 1)
-         (font-lock-mark-block-function
-          . c-mark-function)))
-
-  (make-local-hook 'font-lock-mode-hook)
-  (add-hook 'font-lock-mode-hook 'c-after-font-lock-init nil t))
+  (add-hook 'after-change-functions 'c-after-change nil t)
+  (set (make-local-variable 'font-lock-extend-after-change-region-function)
+       'c-extend-after-change-region)) ; Currently (2009-05) used by all
+                       ; lanaguages with #define (C, C++,; ObjC), and by AWK.
 
 (defun c-setup-doc-comment-style ()
   "Initialize the variables that depend on the value of `c-doc-comment-style'."
@@ -768,6 +634,16 @@ compatible with old code; callers should always specify it."
     ;; Only initialize font locking if we aren't called from an old package.
     (c-font-lock-init))
 
+  ;; Starting a mode is a sort of "change".  So call the change functions...
+  (save-restriction
+    (widen)
+    (save-excursion
+      (if c-get-state-before-change-function
+         (funcall c-get-state-before-change-function (point-min) (point-max)))
+      (if c-before-font-lock-function
+         (funcall c-before-font-lock-function (point-min) (point-max)
+                  (- (point-max) (point-min))))))
+
   (make-local-variable 'outline-regexp)
   (make-local-variable 'outline-level)
   (setq outline-regexp "[^#\n\^M]"
@@ -779,6 +655,31 @@ compatible with old code; callers should always specify it."
       (and (cdr rfn)
           (setq require-final-newline mode-require-final-newline)))))
 
+(defun c-before-hack-hook ()
+  "Set the CC Mode style and \"offsets\" when in the buffer's local variables.
+They are set only when, respectively, the pseudo variables
+`c-file-style' and `c-file-offsets' are present in the list.
+
+This function is called from the hook `before-hack-local-variables-hook'."
+  (when c-buffer-is-cc-mode
+    (let ((mode-cons (assq 'mode file-local-variables-alist))
+         (stile (cdr (assq 'c-file-style file-local-variables-alist)))
+         (offsets (cdr (assq 'c-file-offsets file-local-variables-alist))))
+      (when mode-cons
+       (hack-one-local-variable (car mode-cons) (cdr mode-cons))
+       (setq file-local-variables-alist
+             (delq mode-cons file-local-variables-alist)))
+      (when stile
+       (or (stringp stile) (error "c-file-style is not a string"))
+       (c-set-style stile))
+      (when offsets
+       (mapc
+        (lambda (langentry)
+          (let ((langelem (car langentry))
+                (offset (cdr langentry)))
+            (c-set-offset langelem offset)))
+        offsets)))))
+
 (defun c-remove-any-local-eval-or-mode-variables ()
   ;; If the buffer specifies `mode' or `eval' in its File Local Variable list
   ;; or on the first line, remove all occurrences.  See
@@ -838,8 +739,11 @@ Note that the style variables are always made local to the buffer."
   (when c-buffer-is-cc-mode
     (if (or c-file-style c-file-offsets)
        (c-make-styles-buffer-local t))
-    (and c-file-style
-        (c-set-style c-file-style))
+    (when c-file-style
+      (or (stringp c-file-style)
+         (error "c-file-style is not a string"))
+      (c-set-style c-file-style))
+
     (and c-file-offsets
         (mapc
          (lambda (langentry)
@@ -867,7 +771,9 @@ Note that the style variables are always made local to the buffer."
            (hack-local-variables))
          nil))))
 
-(add-hook 'hack-local-variables-hook 'c-postprocess-file-styles)
+(if (boundp 'before-hack-local-variables-hook)
+    (add-hook 'before-hack-local-variables-hook 'c-before-hack-hook)
+  (add-hook 'hack-local-variables-hook 'c-postprocess-file-styles))
 
 (defmacro c-run-mode-hooks (&rest hooks)
   ;; Emacs 21.1 has introduced a system with delayed mode hooks that
@@ -877,6 +783,301 @@ Note that the style variables are always made local to the buffer."
     `(progn ,@(mapcar (lambda (hook) `(run-hooks ,hook)) hooks))))
 
 \f
+;;; Change hooks, linking with Font Lock.
+
+;; Buffer local variables defining the region to be fontified by a font lock
+;; after-change function.  They are set in c-after-change to
+;; after-change-function's BEG and END, and may be modified by a
+;; `c-before-font-lock-function'.
+(defvar c-new-BEG 0)
+(make-variable-buffer-local 'c-new-BEG)
+(defvar c-new-END 0)
+(make-variable-buffer-local 'c-new-END)
+
+;; Buffer local variables recording Beginning/End-of-Macro position before a
+;; change, when a macro straddles, respectively, the BEG or END (or both) of
+;; the change region.  Otherwise these have the values BEG/END.
+(defvar c-old-BOM 0)
+(make-variable-buffer-local 'c-old-BOM)
+(defvar c-old-EOM 0)
+(make-variable-buffer-local 'c-old-EOM)
+
+(defun c-extend-region-for-CPP (beg end)
+  ;; Set c-old-BOM or c-old-EOM respectively to BEG, END, each extended to the
+  ;; beginning/end of any preprocessor construct they may be in.
+  ;;
+  ;; Point is undefined both before and after this function call; the buffer
+  ;; has already been widened, and match-data saved.  The return value is
+  ;; meaningless.
+  ;;
+  ;; This function is the C/C++/ObjC value of
+  ;; `c-get-state-before-change-function' and is called exclusively as a
+  ;; before change function.
+  (goto-char beg)
+  (c-beginning-of-macro)
+  (setq c-old-BOM (point))
+
+  (goto-char end)
+  (if (c-beginning-of-macro)
+    (c-end-of-macro))
+  (setq c-old-EOM (point)))
+
+(defun c-neutralize-CPP-line (beg end)
+  ;; BEG and END bound a region, typically a preprocessor line.  Put a
+  ;; "punctuation" syntax-table property on syntactically obtrusive
+  ;; characters, ones which would interact syntactically with stuff outside
+  ;; this region.
+  ;;
+  ;; These are unmatched string delimiters, or unmatched
+  ;; parens/brackets/braces.  An unclosed comment is regarded as valid, NOT
+  ;; obtrusive.
+  (save-excursion
+    (let (s)
+      (while
+         (progn
+           (setq s (parse-partial-sexp beg end -1))
+           (cond
+            ((< (nth 0 s) 0)           ; found an unmated ),},]
+             (c-put-char-property (1- (point)) 'syntax-table '(1))
+             t)
+            ((nth 3 s)                 ; In a string
+             (c-put-char-property (nth 8 s) 'syntax-table '(1))
+             t)
+            ((> (nth 0 s) 0)           ; In a (,{,[
+             (c-put-char-property (nth 1 s) 'syntax-table '(1))
+             t)
+            (t nil)))))))
+
+(defun c-extend-and-neutralize-syntax-in-CPP (begg endd old-len)
+  ;; (i) Extend the font lock region to cover all changed preprocessor
+  ;; regions; it does this by setting the variables `c-new-BEG' and
+  ;; `c-new-END' to the new boundaries.
+  ;; 
+  ;; (ii) "Neutralize" every preprocessor line wholly or partially in the
+  ;; extended changed region.  "Restore" lines which were CPP lines before the
+  ;; change and are no longer so; these can be located from the Buffer local
+  ;; variables `c-old-BOM' and `c-old-EOM'.
+  ;; 
+  ;; That is, set syntax-table properties on characters that would otherwise
+  ;; interact syntactically with those outside the CPP line(s).
+  ;; 
+  ;; This function is called from an after-change function, BEGG ENDD and
+  ;; OLD-LEN being the standard parameters.  It prepares the buffer for font
+  ;; locking, hence must get called before `font-lock-after-change-function'.
+  ;;
+  ;; Point is undefined both before and after this function call, the buffer
+  ;; has been widened, and match-data saved.  The return value is ignored.
+  ;;
+  ;; This function is the C/C++/ObjC value of `c-before-font-lock-function'.
+  ;;
+  ;; Note: SPEED _MATTERS_ IN THIS FUNCTION!!!
+  ;; 
+  ;; This function might make hidden buffer changes.
+  (c-save-buffer-state (limits mbeg+1)
+    ;; First determine the region, (c-new-BEG c-new-END), which will get font
+    ;; locked.  It might need "neutralizing".  This region may not start
+    ;; inside a string, comment, or macro.
+    (goto-char c-old-BOM)        ; already set to old start of macro or begg.
+    (setq c-new-BEG
+         (if (setq limits (c-literal-limits))
+             (cdr limits)          ; go forward out of any string or comment.
+           (point)))
+
+    (goto-char endd)
+    (if (setq limits (c-literal-limits))
+       (goto-char (car limits)))  ; go backward out of any string or comment.
+    (if (c-beginning-of-macro)
+       (c-end-of-macro))
+    (setq c-new-END (max (+ (- c-old-EOM old-len) (- endd begg))
+                  (point)))
+
+    ;; Clear any existing punctuation properties.
+    (c-clear-char-property-with-value c-new-BEG c-new-END 'syntax-table '(1))
+
+    ;; Add needed properties to each CPP construct in the region.
+    (goto-char c-new-BEG)
+    (let ((pps-position c-new-BEG)  pps-state)
+      (while (and (< (point) c-new-END)
+                 (search-forward-regexp c-anchored-cpp-prefix c-new-END t))
+       ;; If we've found a "#" inside a string/comment, ignore it.
+       (setq pps-state
+             (parse-partial-sexp pps-position (point) nil nil pps-state)
+             pps-position (point))
+       (unless (or (nth 3 pps-state)   ; in a string?
+                   (nth 4 pps-state))  ; in a comment?
+         (setq mbeg+1 (point))
+         (c-end-of-macro)        ; Do we need to go forward 1 char here?  No!
+         (c-neutralize-CPP-line mbeg+1 (point))
+         (setq pps-position (point))))))) ; no need to update pps-state.
+
+(defun c-before-change (beg end)
+  ;; Function to be put on `before-change-function'.  Primarily, this calls
+  ;; the language dependent `c-get-state-before-change-function'.  It is
+  ;; otherwise used only to remove stale entries from the `c-found-types'
+  ;; cache, and to record entries which a `c-after-change' function might
+  ;; confirm as stale.
+  ;; 
+  ;; Note that this function must be FAST rather than accurate.  Note
+  ;; also that it only has any effect when font locking is enabled.
+  ;; We exploit this by checking for font-lock-*-face instead of doing
+  ;; rigourous syntactic analysis.
+
+  ;; If either change boundary is wholly inside an identifier, delete
+  ;; it/them from the cache.  Don't worry about being inside a string
+  ;; or a comment - "wrongly" removing a symbol from `c-found-types'
+  ;; isn't critical.
+  (setq c-maybe-stale-found-type nil)
+  (save-restriction
+    (save-match-data
+      (widen)
+      (save-excursion
+       ;; Are we inserting/deleting stuff in the middle of an identifier?
+       (c-unfind-enclosing-token beg)
+       (c-unfind-enclosing-token end)
+       ;; Are we coalescing two tokens together, e.g. "fo o" -> "foo"?
+       (when (< beg end)
+         (c-unfind-coalesced-tokens beg end))
+       ;; Are we (potentially) disrupting the syntactic context which
+       ;; makes a type a type?  E.g. by inserting stuff after "foo" in
+       ;; "foo bar;", or before "foo" in "typedef foo *bar;"?
+       ;;
+       ;; We search for appropriate c-type properties "near" the change.
+       ;; First, find an appropriate boundary for this property search.
+       (let (lim
+             type type-pos
+             marked-id term-pos
+             (end1
+              (or (and (eq (get-text-property end 'face) 'font-lock-comment-face)
+                       (previous-single-property-change end 'face))
+                  end)))
+         (when (>= end1 beg) ; Don't hassle about changes entirely in comments.
+           ;; Find a limit for the search for a `c-type' property
+           (while
+               (and (/= (skip-chars-backward "^;{}") 0)
+                    (> (point) (point-min))
+                    (memq (c-get-char-property (1- (point)) 'face)
+                          '(font-lock-comment-face font-lock-string-face))))
+           (setq lim (max (point-min) (1- (point))))
+
+           ;; Look for the latest `c-type' property before end1
+           (when (and (> end1 (point-min))
+                      (setq type-pos
+                            (if (get-text-property (1- end1) 'c-type)
+                                end1
+                              (previous-single-property-change end1 'c-type nil lim))))
+             (setq type (get-text-property (max (1- type-pos) lim) 'c-type))
+
+             (when (memq type '(c-decl-id-start c-decl-type-start))
+               ;; Get the identifier, if any, that the property is on.
+               (goto-char (1- type-pos))
+               (setq marked-id
+                     (when (looking-at "\\(\\sw\\|\\s_\\)")
+                       (c-beginning-of-current-token)
+                       (buffer-substring-no-properties (point) type-pos)))
+
+               (goto-char end1)
+               (skip-chars-forward "^;{}") ; FIXME!!!  loop for comment, maybe
+               (setq lim (point))
+               (setq term-pos
+                     (or (next-single-property-change end 'c-type nil lim) lim))
+               (setq c-maybe-stale-found-type
+                     (list type marked-id
+                           type-pos term-pos
+                           (buffer-substring-no-properties type-pos term-pos)
+                           (buffer-substring-no-properties beg end)))))))
+
+       ;; (c-new-BEG c-new-END) will be the region to fontify.  It may become
+       ;; larger than (beg end).
+       (setq c-new-BEG beg
+             c-new-END end)
+       (if c-get-state-before-change-function
+           (funcall c-get-state-before-change-function beg end))
+       ))))
+
+(defun c-after-change (beg end old-len)
+  ;; Function put on `after-change-functions' to adjust various caches
+  ;; etc.  Prefer speed to finesse here, since there will be an order
+  ;; of magnitude more calls to this function than any of the
+  ;; functions that use the caches.
+  ;;
+  ;; Note that care must be taken so that this is called before any
+  ;; font-lock callbacks since we might get calls to functions using
+  ;; these caches from inside them, and we must thus be sure that this
+  ;; has already been executed.
+  ;;
+  ;; This calls the language variable c-before-font-lock-function, if non nil.
+  ;; This typically sets `syntax-table' properties.
+
+  (c-save-buffer-state ()
+    ;; When `combine-after-change-calls' is used we might get calls
+    ;; with regions outside the current narrowing.  This has been
+    ;; observed in Emacs 20.7.
+    (save-restriction
+      (save-match-data           ; c-recognize-<>-arglists changes match-data
+       (widen)
+
+       (when (> end (point-max))
+         ;; Some emacsen might return positions past the end. This has been
+         ;; observed in Emacs 20.7 when rereading a buffer changed on disk
+         ;; (haven't been able to minimize it, but Emacs 21.3 appears to
+         ;; work).
+         (setq end (point-max))
+         (when (> beg end)
+           (setq beg end)))
+
+       (c-trim-found-types beg end old-len) ; maybe we don't need all of these.
+       (c-invalidate-sws-region-after beg end)
+       (c-invalidate-state-cache beg)
+       (c-invalidate-find-decl-cache beg)
+
+       (when c-recognize-<>-arglists
+         (c-after-change-check-<>-operators beg end))
+
+       (if c-before-font-lock-function
+           (save-excursion
+             (funcall c-before-font-lock-function beg end old-len)))))))
+
+(defun c-after-font-lock-init ()
+  ;; Put on `font-lock-mode-hook'.
+  (remove-hook 'after-change-functions 'c-after-change t)
+  (add-hook 'after-change-functions 'c-after-change nil t))
+
+(defun c-font-lock-init ()
+  "Set up the font-lock variables for using the font-lock support in CC Mode.
+This does not load the font-lock package.  Use after
+`c-basic-common-init' and after cc-fonts has been loaded."
+
+  (make-local-variable 'font-lock-defaults)
+  (setq font-lock-defaults
+       `(,(if (c-major-mode-is 'awk-mode)
+              ;; awk-mode currently has only one font lock level.
+              'awk-font-lock-keywords
+            (mapcar 'c-mode-symbol
+                    '("font-lock-keywords" "font-lock-keywords-1"
+                      "font-lock-keywords-2" "font-lock-keywords-3")))
+         nil nil
+         ,c-identifier-syntax-modifications
+         c-beginning-of-syntax
+         (font-lock-mark-block-function
+          . c-mark-function)))
+
+  (make-local-hook 'font-lock-mode-hook)
+  (add-hook 'font-lock-mode-hook 'c-after-font-lock-init nil t))
+
+(defun c-extend-after-change-region (beg end old-len)
+  "Extend the region to be fontified, if necessary."
+  ;; Note: the parameters are ignored here.  This somewhat indirect
+  ;; implementation exists because it is minimally different from the
+  ;; stand-alone CC Mode which, lacking
+  ;; font-lock-extend-after-change-region-function, is forced to use advice
+  ;; instead.
+  ;;
+  ;; Of the seven CC Mode languages, currently (2009-05) only C, C++, Objc
+  ;; (the languages with #define) and AWK Mode make non-null use of this
+  ;; function.
+  (cons c-new-BEG c-new-END))
+
+\f
 ;; Support for C
 
 ;;;###autoload
@@ -910,8 +1111,8 @@ Note that the style variables are always made local to the buffer."
 ;; which could cause it to clobber user settings.  Later emacsen have
 ;; an append option, but it's not safe to use.
 
-;; The the extension ".C" is associated to C++ while the lowercase
-;; variant goes to C.  On case insensitive file systems, this means
+;; The extension ".C" is associated with C++ while the lowercase
+;; variant goes with C.  On case insensitive file systems, this means
 ;; that ".c" files also might open C++ mode if the C++ entry comes
 ;; first on `auto-mode-alist'.  Thus we try to ensure that ".C" comes
 ;; after ".c", and since `add-to-list' adds the entry first we have to
@@ -928,6 +1129,11 @@ Note that the style variables are always made local to the buffer."
 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.y\\(acc\\)?\\'" . c-mode))
 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.lex\\'" . c-mode))
 
+;; Preprocessed files generated by C and C++ compilers.
+;;;###autoload (add-to-list 'auto-mode-alist '("\\.i\\'" . c-mode))
+;;;###autoload (add-to-list 'auto-mode-alist '("\\.ii\\'" . c++-mode))
+
+
 ;;;###autoload
 (defun c-mode ()
   "Major mode for editing K&R and ANSI C code.
@@ -1312,6 +1518,10 @@ Key bindings:
 (easy-menu-define c-awk-menu awk-mode-map "AWK Mode Commands"
                  (cons "AWK" (c-lang-const c-mode-menu awk)))
 
+;; (require 'cc-awk) brings these in.
+(defvar awk-mode-syntax-table)
+(declare-function c-awk-unstick-NL-prop "cc-awk" ())
+
 (defun awk-mode ()
   "Major mode for editing AWK code.
 To submit a problem report, enter `\\[c-submit-bug-report]' from an
@@ -1338,21 +1548,7 @@ Key bindings:
   (use-local-map awk-mode-map)
   (c-init-language-vars-for 'awk-mode)
   (c-common-init 'awk-mode)
-  ;; The rest of CC Mode does not (yet) use `font-lock-syntactic-keywords',
-  ;; so it's not set by `c-font-lock-init'.
-  (make-local-variable 'font-lock-syntactic-keywords)
-  (setq font-lock-syntactic-keywords
-       '((c-awk-set-syntax-table-properties
-          0 (0)                        ; Everything on this line is a dummy.
-          nil t)))
   (c-awk-unstick-NL-prop)
-  (add-hook 'before-change-functions 'c-awk-before-change nil t)
-  (add-hook 'after-change-functions 'c-awk-after-change nil t)
-  (c-save-buffer-state nil
-    (save-restriction
-      (widen)
-      (c-awk-clear-NL-props (point-min) (point-max))
-      (c-awk-after-change (point-min) (point-max) 0))) ; Set syntax-table props.
 
   ;; Prevent Xemacs's buffer-syntactic-context being used.  See the comment
   ;; in cc-engine.el, just before (defun c-fast-in-literal ...
@@ -1448,5 +1644,5 @@ Key bindings:
 \f
 (cc-provide 'cc-mode)
 
-;;; arch-tag: 7825e5c4-fd09-439f-a04d-4c13208ba3d7
+;; arch-tag: 7825e5c4-fd09-439f-a04d-4c13208ba3d7
 ;;; cc-mode.el ends here