+;;; Common routines.
+
+(defvar c-mode-base-map ()
+ "Keymap shared by all CC Mode related modes.")
+
+(defun c-make-inherited-keymap ()
+ (let ((map (make-sparse-keymap)))
+ (cond
+ ;; XEmacs
+ ((fboundp 'set-keymap-parents)
+ (set-keymap-parents map c-mode-base-map))
+ ;; Emacs
+ ((fboundp 'set-keymap-parent)
+ (set-keymap-parent map c-mode-base-map))
+ ;; incompatible
+ (t (error "CC Mode is incompatible with this version of Emacs")))
+ map))
+
+(defun c-define-abbrev-table (name defs)
+ ;; Compatibility wrapper for `define-abbrev' which passes a non-nil
+ ;; sixth argument for SYSTEM-FLAG in emacsen that support it
+ ;; (currently only Emacs 21.2).
+ (let ((table (or (symbol-value name)
+ (progn (define-abbrev-table name nil)
+ (symbol-value name)))))
+ (while defs
+ (condition-case nil
+ (apply 'define-abbrev table (append (car defs) '(t)))
+ (wrong-number-of-arguments
+ (apply 'define-abbrev table (car defs))))
+ (setq defs (cdr defs)))))
+(put 'c-define-abbrev-table 'lisp-indent-function 1)
+
+(if c-mode-base-map
+ nil
+ ;; TBD: should we even worry about naming this keymap. My vote: no,
+ ;; because Emacs and XEmacs do it differently.
+ (setq c-mode-base-map (make-sparse-keymap))
+ ;; put standard keybindings into MAP
+ ;; the following mappings correspond more or less directly to BOCM
+ (define-key c-mode-base-map "{" 'c-electric-brace)
+ (define-key c-mode-base-map "}" 'c-electric-brace)
+ (define-key c-mode-base-map ";" 'c-electric-semi&comma)
+ (define-key c-mode-base-map "#" 'c-electric-pound)
+ (define-key c-mode-base-map ":" 'c-electric-colon)
+ (define-key c-mode-base-map "(" 'c-electric-paren)
+ (define-key c-mode-base-map ")" 'c-electric-paren)
+ ;; Separate M-BS from C-M-h. The former should remain
+ ;; backward-kill-word.
+ (define-key c-mode-base-map [(control meta h)] 'c-mark-function)
+ (define-key c-mode-base-map "\e\C-q" 'c-indent-exp)
+ (substitute-key-definition 'backward-sentence
+ 'c-beginning-of-statement
+ c-mode-base-map global-map)
+ (substitute-key-definition 'forward-sentence
+ 'c-end-of-statement
+ c-mode-base-map global-map)
+ (substitute-key-definition 'indent-new-comment-line
+ 'c-indent-new-comment-line
+ c-mode-base-map global-map)
+ (when (fboundp 'comment-indent-new-line)
+ ;; indent-new-comment-line has changed name to
+ ;; comment-indent-new-line in Emacs 21.
+ (substitute-key-definition 'comment-indent-new-line
+ 'c-indent-new-comment-line
+ c-mode-base-map global-map))
+ ;; RMS says don't make these the default.
+;; (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)
+ (define-key c-mode-base-map "\C-c\C-u" 'c-up-conditional)
+ (substitute-key-definition 'indent-for-tab-command
+ 'c-indent-command
+ c-mode-base-map global-map)
+
+ ;; It doesn't suffice to put `c-fill-paragraph' on
+ ;; `fill-paragraph-function' since `c-fill-paragraph' must be called
+ ;; before any fill prefix adaption is done. E.g. `filladapt-mode'
+ ;; replaces `fill-paragraph' and does the adaption before calling
+ ;; `fill-paragraph-function', and we have to mask comments etc
+ ;; before that. Also, `c-fill-paragraph' chains on to
+ ;; `fill-paragraph' and the value on `fill-parapgraph-function' to
+ ;; do the actual filling work.
+ (substitute-key-definition 'fill-paragraph 'c-fill-paragraph
+ c-mode-base-map global-map)
+ ;; In XEmacs the default fill function is called
+ ;; fill-paragraph-or-region.
+ (substitute-key-definition 'fill-paragraph-or-region 'c-fill-paragraph
+ c-mode-base-map global-map)
+
+ ;; Bind the electric deletion functions to C-d and DEL. Emacs 21
+ ;; automatically maps the [delete] and [backspace] keys to these two
+ ;; depending on window system and user preferences. (In earlier
+ ;; versions it's possible to do the same by using `function-key-map'.)
+ (define-key c-mode-base-map "\C-d" 'c-electric-delete-forward)
+ (define-key c-mode-base-map "\177" 'c-electric-backspace)
+ (when (boundp 'delete-key-deletes-forward)
+ ;; In XEmacs 20 and later we fix the forward and backward deletion
+ ;; behavior by binding the keysyms for the [delete] and
+ ;; [backspace] keys directly, and use `delete-forward-p' or
+ ;; `delete-key-deletes-forward' to decide what [delete] should do.
+ (define-key c-mode-base-map [delete] 'c-electric-delete)
+ (define-key c-mode-base-map [backspace] 'c-electric-backspace))
+ (define-key c-mode-base-map "," 'c-electric-semi&comma)
+ (define-key c-mode-base-map "*" 'c-electric-star)
+ (define-key c-mode-base-map "/" 'c-electric-slash)
+ (define-key c-mode-base-map "\C-c\C-q" 'c-indent-defun)
+ (define-key c-mode-base-map "\C-c\C-\\" 'c-backslash-region)
+ (define-key c-mode-base-map "\C-c\C-a" 'c-toggle-auto-state)
+ (define-key c-mode-base-map "\C-c\C-b" 'c-submit-bug-report)
+ (define-key c-mode-base-map "\C-c\C-c" 'comment-region)
+ (define-key c-mode-base-map "\C-c\C-d" 'c-toggle-hungry-state)
+ (define-key c-mode-base-map "\C-c\C-o" 'c-set-offset)
+ (define-key c-mode-base-map "\C-c\C-s" 'c-show-syntactic-information)
+ (define-key c-mode-base-map "\C-c\C-t" 'c-toggle-auto-hungry-state)
+ (define-key c-mode-base-map "\C-c." 'c-set-style)
+ ;; conflicts with OOBR
+ ;;(define-key c-mode-base-map "\C-c\C-v" 'c-version)
+ )
+
+;; We don't require the outline package, but we configure it a bit anyway.
+(cc-bytecomp-defvar outline-level)
+
+(defun c-mode-menu (modestr)
+ "Return a menu spec suitable for `easy-menu-define' that is exactly
+like the C mode menu except that the menu bar item name is MODESTR
+instead of \"C\".
+
+This function is provided for compatibility only; derived modes should
+preferably use the `c-mode-menu' language constant directly."
+ (cons modestr (c-lang-const c-mode-menu c)))
+
+;; Ugly hack to pull in the definition of `c-populate-syntax-table'
+;; from cc-langs to make it available at runtime. It's either this or
+;; moving the definition for it to cc-defs, but that would mean to
+;; break up the syntax table setup over two files.
+(defalias 'c-populate-syntax-table
+ (cc-eval-when-compile
+ (let ((f (symbol-function 'c-populate-syntax-table)))
+ (if (byte-code-function-p f) f (byte-compile f)))))
+
+(defun c-after-change (beg end len)
+ ;; Function put on `after-change-functions' to adjust various
+ ;; caches. 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 function does not do any hidden buffer changes.
+
+ (c-save-buffer-state ()
+ (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-invalidate-sws-region-after beg end)
+ (c-invalidate-state-cache beg)
+ (c-invalidate-find-decl-cache beg)))
+
+(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
+packages that embed CC Mode.
+
+MODE is the CC Mode flavor to set up, e.g. 'c-mode or 'java-mode.
+DEFAULT-STYLE tells which indentation style to install. It has the
+same format as `c-default-style'.
+
+Note that `c-init-language-vars' must be called before this function.
+This function cannot do that since `c-init-language-vars' is a macro
+that requires a literal mode spec at compile time."
+ ;;
+ ;; This function does not do any hidden buffer changes.
+
+ (setq c-buffer-is-cc-mode mode)
+
+ ;; these variables should always be buffer local; they do not affect
+ ;; indentation style.
+ (make-local-variable 'parse-sexp-ignore-comments)
+ (make-local-variable 'indent-line-function)
+ (make-local-variable 'indent-region-function)
+ (make-local-variable 'normal-auto-fill-function)
+ (make-local-variable 'comment-start)
+ (make-local-variable 'comment-end)
+ (make-local-variable 'comment-start-skip)
+ (make-local-variable 'comment-multi-line)
+ (make-local-variable 'paragraph-start)
+ (make-local-variable 'paragraph-separate)
+ (make-local-variable 'paragraph-ignore-fill-prefix)
+ (make-local-variable 'adaptive-fill-mode)
+ (make-local-variable 'adaptive-fill-regexp)
+
+ ;; now set their values
+ (setq parse-sexp-ignore-comments t
+ indent-line-function 'c-indent-line
+ indent-region-function 'c-indent-region
+ normal-auto-fill-function 'c-do-auto-fill
+ comment-multi-line t)
+
+ ;; Install `c-fill-paragraph' on `fill-paragraph-function' so that a
+ ;; direct call to `fill-paragraph' behaves better. This still
+ ;; doesn't work with filladapt but it's better than nothing.
+ (make-local-variable 'fill-paragraph-function)
+ (setq fill-paragraph-function 'c-fill-paragraph)
+
+ ;; (X)Emacs 20 and later.
+ (when (boundp 'comment-line-break-function)
+ (make-local-variable 'comment-line-break-function)
+ (setq comment-line-break-function
+ 'c-indent-new-comment-line))
+
+ ;; Emacs 20 and later.
+ (when (boundp 'parse-sexp-lookup-properties)
+ (make-local-variable 'parse-sexp-lookup-properties)
+ (setq parse-sexp-lookup-properties t))
+
+ ;; Same as above for XEmacs 21 (although currently undocumented).
+ (when (boundp 'lookup-syntax-properties)
+ (make-local-variable 'lookup-syntax-properties)
+ (setq lookup-syntax-properties t))
+
+ ;; 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)))
+
+ ;; 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
+ ;; we have c-state-cache that isn't useful and only causes trouble
+ ;; so turn it off.
+ (when (memq 'col-0-paren c-emacs-features)
+ (make-local-variable 'open-paren-in-column-0-is-defun-start)
+ (setq open-paren-in-column-0-is-defun-start nil))
+
+ ;; The `c-type' text property with `c-decl-end' is used to mark the
+ ;; ends of access keys to make interactive refontification work
+ ;; better.
+ (when c-opt-access-key
+ (setq c-type-decl-end-used t))
+
+ (c-clear-found-types)
+
+ ;; now set the mode style based on default-style
+ (let ((style (if (stringp default-style)
+ default-style
+ (or (cdr (assq mode default-style))
+ (cdr (assq 'other default-style))
+ "gnu"))))
+ ;; Override style variables if `c-old-style-variable-behavior' is
+ ;; set. Also override if we are using global style variables,
+ ;; have already initialized a style once, and are switching to a
+ ;; different style. (It's doubtful whether this is desirable, but
+ ;; the whole situation with nonlocal style variables is a bit
+ ;; awkward. It's at least the most compatible way with the old
+ ;; style init procedure.)
+ (c-set-style style (not (or c-old-style-variable-behavior
+ (and (not c-style-variables-are-local-p)
+ c-indentation-style
+ (not (string-equal c-indentation-style
+ style)))))))
+ (c-setup-paragraph-variables)
+
+ ;; we have to do something special for c-offsets-alist so that the
+ ;; buffer local value has its own alist structure.
+ (setq c-offsets-alist (copy-alist c-offsets-alist))
+
+ ;; setup the comment indent variable in a Emacs version portable way
+ (make-local-variable 'comment-indent-function)
+ (setq comment-indent-function 'c-comment-indent)
+
+ ;; put auto-hungry designators onto minor-mode-alist, but only once
+ (or (assq 'c-auto-hungry-string minor-mode-alist)
+ (setq minor-mode-alist
+ (cons '(c-auto-hungry-string c-auto-hungry-string)
+ minor-mode-alist)))
+
+ ;; Install the functions that ensure that various internal caches
+ ;; don't become invalid due to buffer changes.
+ (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-mode-is-new-awk-p)
+ ;; 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-setup-doc-comment-style ()
+ "Initialize the variables that depend on the value of `c-doc-comment-style'."
+ (when (and (featurep 'font-lock)
+ (symbol-value 'font-lock-mode))
+ ;; Force font lock mode to reinitialize itself.
+ (font-lock-mode 0)
+ (font-lock-mode 1)))
+
+(defun c-common-init (&optional mode)
+ "Common initialization for all CC Mode modes.
+In addition to the work done by `c-basic-common-init' and
+`c-font-lock-init', this function sets up various other things as
+customary in CC Mode modes but which aren't strictly necessary for CC
+Mode to operate correctly.
+
+MODE is the symbol for the mode to initialize, like 'c-mode. See
+`c-basic-common-init' for details. It's only optional to be
+compatible with old code; callers should always specify it.
+
+This function does not do any hidden buffer changes."
+
+ (unless mode
+ ;; Called from an old third party package. The fallback is to
+ ;; initialize for C.
+ (c-init-language-vars-for 'c-mode))
+
+ (c-basic-common-init mode c-default-style)
+ (when mode
+ ;; Only initialize font locking if we aren't called from an old package.
+ (c-font-lock-init))
+
+ (make-local-variable 'outline-regexp)
+ (make-local-variable 'outline-level)
+ (setq outline-regexp "[^#\n\^M]"
+ outline-level 'c-outline-level)
+
+ (let ((rfn (assq mode c-require-final-newline)))
+ (when rfn
+ (make-local-variable 'require-final-newline)
+ (setq require-final-newline (cdr rfn)))))
+
+(defun c-postprocess-file-styles ()
+ "Function that post processes relevant file local variables in CC Mode.
+Currently, this function simply applies any style and offset settings
+found in the file's Local Variable list. It first applies any style
+setting found in `c-file-style', then it applies any offset settings
+it finds in `c-file-offsets'.
+
+Note that the style variables are always made local to the buffer."
+ ;;
+ ;; This function does not do any hidden buffer changes.
+
+ ;; apply file styles and offsets
+ (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))
+ (and c-file-offsets
+ (mapcar
+ (lambda (langentry)
+ (let ((langelem (car langentry))
+ (offset (cdr langentry)))
+ (c-set-offset langelem offset)))
+ c-file-offsets))))
+
+(add-hook 'hack-local-variables-hook 'c-postprocess-file-styles)
+
+\f
+;; Support for C
+
+;;;###autoload
+(defvar c-mode-syntax-table nil
+ "Syntax table used in c-mode buffers.")
+(or c-mode-syntax-table
+ (setq c-mode-syntax-table
+ (funcall (c-lang-const c-make-mode-syntax-table c))))
+
+(defvar c-mode-abbrev-table nil
+ "Abbreviation table used in c-mode buffers.")
+(c-define-abbrev-table 'c-mode-abbrev-table
+ '(("else" "else" c-electric-continued-statement 0)
+ ("while" "while" c-electric-continued-statement 0)))
+
+(defvar c-mode-map ()
+ "Keymap used in c-mode buffers.")
+(if c-mode-map
+ nil
+ (setq c-mode-map (c-make-inherited-keymap))
+ ;; add bindings which are only useful for C
+ (define-key c-mode-map "\C-c\C-e" 'c-macro-expand)
+ )
+
+(easy-menu-define c-c-menu c-mode-map "C Mode Commands"
+ (cons "C" (c-lang-const c-mode-menu c)))
+
+;; In XEmacs >= 21.5 modes should add their own entries to
+;; `auto-mode-alist'. The comment form of autoload is used to avoid
+;; doing this on load. That since `add-to-list' prepends the value
+;; 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
+;; 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
+;; add the ".C" entry first.
+;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(cc\\|hh\\)\\'" . c++-mode))
+;;;###autoload (add-to-list 'auto-mode-alist '("\\.[ch]\\(pp\\|xx\\|\\+\\+\\)\\'" . c++-mode))
+;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(CC?\\|HH?\\)\\'" . c++-mode))
+
+;;;###autoload (add-to-list 'auto-mode-alist '("\\.[ch]\\'" . c-mode))
+
+;; NB: The following two associate yacc and lex files to C Mode, which
+;; is not really suitable for those formats. Anyway, afaik there's
+;; currently no better mode for them, and besides this is legacy.
+;;;###autoload (add-to-list 'auto-mode-alist '("\\.y\\(acc\\)?\\'" . c-mode))
+;;;###autoload (add-to-list 'auto-mode-alist '("\\.lex\\'" . c-mode))
+