1 ;;; cc-mode.el --- major mode for editing C and similar languages
3 ;; Copyright (C) 1985,1987,1992-2003 Free Software Foundation, Inc.
5 ;; Authors: 2003- Alan Mackenzie
6 ;; 1998- Martin Stjernholm
7 ;; 1992-1999 Barry A. Warsaw
8 ;; 1987 Dave Detlefs and Stewart Clamen
9 ;; 1985 Richard M. Stallman
10 ;; Maintainer: bug-cc-mode@gnu.org
11 ;; Created: a long, long, time ago. adapted from the original c-mode.el
12 ;; Keywords: c languages oop
14 ;; This file is part of GNU Emacs.
16 ;; GNU Emacs is free software; you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation; either version 2, or (at your option)
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with this program; see the file COPYING. If not, write to
28 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29 ;; Boston, MA 02110-1301, USA.
33 ;; NOTE: Read the commentary below for the right way to submit bug reports!
34 ;; NOTE: See the accompanying texinfo manual for details on using this mode!
35 ;; Note: The version string is in cc-defs.
37 ;; This package provides GNU Emacs major modes for editing C, C++,
38 ;; Objective-C, Java, CORBA's IDL, Pike and AWK code. As of the
39 ;; latest Emacs and XEmacs releases, it is the default package for
40 ;; editing these languages. This package is called "CC Mode", and
41 ;; should be spelled exactly this way.
43 ;; CC Mode supports K&R and ANSI C, ANSI C++, Objective-C, Java,
44 ;; CORBA's IDL, Pike and AWK with a consistent indentation model
45 ;; across all modes. This indentation model is intuitive and very
46 ;; flexible, so that almost any desired style of indentation can be
47 ;; supported. Installation, usage, and programming details are
48 ;; contained in an accompanying texinfo manual.
50 ;; CC Mode's immediate ancestors were, c++-mode.el, cplus-md.el, and
53 ;; To submit bug reports, type "C-c C-b". These will be sent to
54 ;; bug-gnu-emacs@gnu.org (mirrored as the Usenet newsgroup
55 ;; gnu.emacs.bug) as well as bug-cc-mode@gnu.org, which directly
56 ;; contacts the CC Mode maintainers. Questions can sent to
57 ;; help-gnu-emacs@gnu.org (mirrored as gnu.emacs.help) and/or
58 ;; bug-cc-mode@gnu.org. Please do not send bugs or questions to our
59 ;; personal accounts; we reserve the right to ignore such email!
61 ;; Many, many thanks go out to all the folks on the beta test list.
62 ;; Without their patience, testing, insight, code contributions, and
63 ;; encouragement CC Mode would be a far inferior package.
65 ;; You can get the latest version of CC Mode, including PostScript
66 ;; documentation and separate individual files from:
68 ;; http://cc-mode.sourceforge.net/
70 ;; You can join a moderated CC Mode announcement-only mailing list by
73 ;; http://lists.sourceforge.net/mailman/listinfo/cc-mode-announce
79 (if (and (boundp 'byte-compile-dest-file)
80 (stringp byte-compile-dest-file))
81 (cons (file-name-directory byte-compile-dest-file) load-path)
83 (load "cc-bytecomp" nil t)))
86 (cc-require-when-compile 'cc-langs)
88 (cc-require 'cc-engine)
89 (cc-require 'cc-styles)
91 (cc-require 'cc-align)
92 (cc-require 'cc-menus)
94 ;; Silence the compiler.
95 (cc-bytecomp-defvar comment-line-break-function) ; (X)Emacs 20+
96 (cc-bytecomp-defvar adaptive-fill-first-line-regexp) ; Emacs 20+
97 (cc-bytecomp-defun set-keymap-parents) ; XEmacs
98 (cc-bytecomp-defun run-mode-hooks) ; Emacs 21.1+
99 (cc-bytecomp-obsolete-fun make-local-hook) ; Marked obsolete in Emacs 21.1.
101 ;; We set these variables during mode init, yet we don't require
103 (cc-bytecomp-defvar font-lock-defaults)
104 (cc-bytecomp-defvar font-lock-syntactic-keywords)
106 ;; Menu support for both XEmacs and Emacs. If you don't have easymenu
107 ;; with your version of Emacs, you are incompatible!
108 (cc-external-require 'easymenu)
110 ;; Load cc-fonts first after font-lock is loaded, since it isn't
111 ;; necessary until font locking is requested.
112 (eval-after-load "font-lock"
113 '(require 'cc-fonts))
115 ;; cc-langs isn't loaded when we're byte compiled, so add autoload
116 ;; directives for the interface functions.
117 (autoload 'c-make-init-lang-vars-fun "cc-langs")
118 (autoload 'c-init-language-vars "cc-langs" nil nil 'macro)
121 ;; Other modes and packages which depend on CC Mode should do the
122 ;; following to make sure everything is loaded and available for their
125 ;; (require 'cc-mode)
127 ;; And in the major mode function:
129 ;; (c-initialize-cc-mode t)
130 ;; (c-init-language-vars some-mode)
131 ;; (c-common-init 'some-mode) ; Or perhaps (c-basic-common-init 'some-mode)
133 ;; If you're not writing a derived mode using the language variable
134 ;; system, then some-mode is one of the language modes directly
135 ;; supported by CC Mode. You can then use (c-init-language-vars-for
136 ;; 'some-mode) instead of `c-init-language-vars'.
137 ;; `c-init-language-vars-for' is a function that avoids the rather
138 ;; large expansion of `c-init-language-vars'.
140 ;; If you use `c-basic-common-init' then you might want to call
141 ;; `c-font-lock-init' too to set up CC Mode's font lock support.
143 ;; See cc-langs.el for further info. A small example of a derived mode
144 ;; is also available at <http://cc-mode.sourceforge.net/
145 ;; derived-mode-ex.el>.
147 (defun c-leave-cc-mode-mode ()
148 (setq c-buffer-is-cc-mode nil))
150 (defun c-init-language-vars-for (mode)
151 "Initialize the language variables for one of the language modes
152 directly supported by CC Mode. This can be used instead of the
153 `c-init-language-vars' macro if the language you want to use is one of
154 those, rather than a derived language defined through the language
155 variable system (see \"cc-langs.el\")."
156 ;; This function does not do any hidden buffer changes.
157 (cond ((eq mode 'c-mode) (c-init-language-vars c-mode))
158 ((eq mode 'c++-mode) (c-init-language-vars c++-mode))
159 ((eq mode 'objc-mode) (c-init-language-vars objc-mode))
160 ((eq mode 'java-mode) (c-init-language-vars java-mode))
161 ((eq mode 'idl-mode) (c-init-language-vars idl-mode))
162 ((eq mode 'pike-mode) (c-init-language-vars pike-mode))
163 ((eq mode 'awk-mode) (c-init-language-vars awk-mode))
164 (t (error "Unsupported mode %s" mode))))
167 (defun c-initialize-cc-mode (&optional new-style-init)
168 "Initialize CC Mode for use in the current buffer.
169 If the optional NEW-STYLE-INIT is nil or left out then all necessary
170 initialization to run CC Mode for the C language is done. Otherwise
171 only some basic setup is done, and a call to `c-init-language-vars' or
172 `c-init-language-vars-for' is necessary too (which gives more
173 control). See \"cc-mode.el\" for more info."
175 ;; This function does not do any hidden buffer changes.
177 (setq c-buffer-is-cc-mode t)
179 (let ((initprop 'cc-mode-is-initialized)
181 (unless (get 'c-initialize-cc-mode initprop)
184 (put 'c-initialize-cc-mode initprop t)
185 (c-initialize-builtin-style)
186 (run-hooks 'c-initialization-hook)
187 ;; Fix obsolete variables.
188 (if (boundp 'c-comment-continuation-stars)
189 (setq c-block-comment-prefix c-comment-continuation-stars))
190 (add-hook 'change-major-mode-hook 'c-leave-cc-mode-mode)
191 (setq c-initialization-ok t))
192 ;; Will try initialization hooks again if they failed.
193 (put 'c-initialize-cc-mode initprop c-initialization-ok))))
195 (unless new-style-init
196 (c-init-language-vars-for 'c-mode)))
201 (defvar c-mode-base-map ()
202 "Keymap shared by all CC Mode related modes.")
204 (defun c-make-inherited-keymap ()
205 (let ((map (make-sparse-keymap)))
206 ;; Necessary to use `cc-bytecomp-fboundp' below since this
207 ;; function is called from top-level forms that are evaluated
208 ;; while cc-bytecomp is active when one does M-x eval-buffer.
211 ((cc-bytecomp-fboundp 'set-keymap-parents)
212 (set-keymap-parents map c-mode-base-map))
214 ((cc-bytecomp-fboundp 'set-keymap-parent)
215 (set-keymap-parent map c-mode-base-map))
217 (t (error "CC Mode is incompatible with this version of Emacs")))
220 (defun c-define-abbrev-table (name defs)
221 ;; Compatibility wrapper for `define-abbrev' which passes a non-nil
222 ;; sixth argument for SYSTEM-FLAG in emacsen that support it
223 ;; (currently only Emacs 21.2).
224 (let ((table (or (symbol-value name)
225 (progn (define-abbrev-table name nil)
226 (symbol-value name)))))
229 (apply 'define-abbrev table (append (car defs) '(t)))
230 (wrong-number-of-arguments
231 (apply 'define-abbrev table (car defs))))
232 (setq defs (cdr defs)))))
233 (put 'c-define-abbrev-table 'lisp-indent-function 1)
237 ;; TBD: should we even worry about naming this keymap. My vote: no,
238 ;; because Emacs and XEmacs do it differently.
239 (setq c-mode-base-map (make-sparse-keymap))
240 ;; put standard keybindings into MAP
241 ;; the following mappings correspond more or less directly to BOCM
242 (define-key c-mode-base-map "{" 'c-electric-brace)
243 (define-key c-mode-base-map "}" 'c-electric-brace)
244 (define-key c-mode-base-map ";" 'c-electric-semi&comma)
245 (define-key c-mode-base-map "#" 'c-electric-pound)
246 (define-key c-mode-base-map ":" 'c-electric-colon)
247 (define-key c-mode-base-map "(" 'c-electric-paren)
248 (define-key c-mode-base-map ")" 'c-electric-paren)
249 ;; Separate M-BS from C-M-h. The former should remain
250 ;; backward-kill-word.
251 (define-key c-mode-base-map [(control meta h)] 'c-mark-function)
252 (define-key c-mode-base-map "\e\C-q" 'c-indent-exp)
253 (substitute-key-definition 'backward-sentence
254 'c-beginning-of-statement
255 c-mode-base-map global-map)
256 (substitute-key-definition 'forward-sentence
258 c-mode-base-map global-map)
259 (substitute-key-definition 'indent-new-comment-line
260 'c-indent-new-comment-line
261 c-mode-base-map global-map)
262 (when (fboundp 'comment-indent-new-line)
263 ;; indent-new-comment-line has changed name to
264 ;; comment-indent-new-line in Emacs 21.
265 (substitute-key-definition 'comment-indent-new-line
266 'c-indent-new-comment-line
267 c-mode-base-map global-map))
268 ;; RMS says don't make these the default.
269 ;; (define-key c-mode-base-map "\e\C-a" 'c-beginning-of-defun)
270 ;; (define-key c-mode-base-map "\e\C-e" 'c-end-of-defun)
271 (define-key c-mode-base-map "\C-c\C-n" 'c-forward-conditional)
272 (define-key c-mode-base-map "\C-c\C-p" 'c-backward-conditional)
273 (define-key c-mode-base-map "\C-c\C-u" 'c-up-conditional)
274 (substitute-key-definition 'indent-for-tab-command
276 c-mode-base-map global-map)
278 ;; It doesn't suffice to put `c-fill-paragraph' on
279 ;; `fill-paragraph-function' since `c-fill-paragraph' must be called
280 ;; before any fill prefix adaption is done. E.g. `filladapt-mode'
281 ;; replaces `fill-paragraph' and does the adaption before calling
282 ;; `fill-paragraph-function', and we have to mask comments etc
283 ;; before that. Also, `c-fill-paragraph' chains on to
284 ;; `fill-paragraph' and the value on `fill-parapgraph-function' to
285 ;; do the actual filling work.
286 (substitute-key-definition 'fill-paragraph 'c-fill-paragraph
287 c-mode-base-map global-map)
288 ;; In XEmacs the default fill function is called
289 ;; fill-paragraph-or-region.
290 (substitute-key-definition 'fill-paragraph-or-region 'c-fill-paragraph
291 c-mode-base-map global-map)
293 ;; Bind the electric deletion functions to C-d and DEL. Emacs 21
294 ;; automatically maps the [delete] and [backspace] keys to these two
295 ;; depending on window system and user preferences. (In earlier
296 ;; versions it's possible to do the same by using `function-key-map'.)
297 (define-key c-mode-base-map "\C-d" 'c-electric-delete-forward)
298 (define-key c-mode-base-map "\177" 'c-electric-backspace)
299 (when (boundp 'delete-key-deletes-forward)
300 ;; In XEmacs 20 and later we fix the forward and backward deletion
301 ;; behavior by binding the keysyms for the [delete] and
302 ;; [backspace] keys directly, and use `delete-forward-p' or
303 ;; `delete-key-deletes-forward' to decide what [delete] should do.
304 (define-key c-mode-base-map [delete] 'c-electric-delete)
305 (define-key c-mode-base-map [backspace] 'c-electric-backspace))
306 (define-key c-mode-base-map "," 'c-electric-semi&comma)
307 (define-key c-mode-base-map "*" 'c-electric-star)
308 (define-key c-mode-base-map "/" 'c-electric-slash)
309 (define-key c-mode-base-map "\C-c\C-q" 'c-indent-defun)
310 (define-key c-mode-base-map "\C-c\C-\\" 'c-backslash-region)
311 (define-key c-mode-base-map "\C-c\C-a" 'c-toggle-auto-state)
312 (define-key c-mode-base-map "\C-c\C-b" 'c-submit-bug-report)
313 (define-key c-mode-base-map "\C-c\C-c" 'comment-region)
314 (define-key c-mode-base-map "\C-c\C-d" 'c-toggle-hungry-state)
315 (define-key c-mode-base-map "\C-c\C-o" 'c-set-offset)
316 (define-key c-mode-base-map "\C-c\C-s" 'c-show-syntactic-information)
317 (define-key c-mode-base-map "\C-c\C-t" 'c-toggle-auto-hungry-state)
318 (define-key c-mode-base-map "\C-c." 'c-set-style)
319 ;; conflicts with OOBR
320 ;;(define-key c-mode-base-map "\C-c\C-v" 'c-version)
323 ;; We don't require the outline package, but we configure it a bit anyway.
324 (cc-bytecomp-defvar outline-level)
326 (defun c-mode-menu (modestr)
327 "Return a menu spec suitable for `easy-menu-define' that is exactly
328 like the C mode menu except that the menu bar item name is MODESTR
331 This function is provided for compatibility only; derived modes should
332 preferably use the `c-mode-menu' language constant directly."
333 (cons modestr (c-lang-const c-mode-menu c)))
335 ;; Ugly hack to pull in the definition of `c-populate-syntax-table'
336 ;; from cc-langs to make it available at runtime. It's either this or
337 ;; moving the definition for it to cc-defs, but that would mean to
338 ;; break up the syntax table setup over two files.
339 (defalias 'c-populate-syntax-table
340 (cc-eval-when-compile
341 (let ((f (symbol-function 'c-populate-syntax-table)))
342 (if (byte-code-function-p f) f (byte-compile f)))))
344 (defun c-after-change (beg end len)
345 ;; Function put on `after-change-functions' to adjust various
346 ;; caches. Prefer speed to finesse here, since there will be an
347 ;; order of magnitude more calls to this function than any of the
348 ;; functions that use the caches.
350 ;; Note that care must be taken so that this is called before any
351 ;; font-lock callbacks since we might get calls to functions using
352 ;; these caches from inside them, and we must thus be sure that this
353 ;; has already been executed.
355 ;; This function does not do any hidden buffer changes.
357 (c-save-buffer-state ()
358 (when (> end (point-max))
359 ;; Some emacsen might return positions past the end. This has been
360 ;; observed in Emacs 20.7 when rereading a buffer changed on disk
361 ;; (haven't been able to minimize it, but Emacs 21.3 appears to
363 (setq end (point-max))
367 (c-invalidate-sws-region-after beg end)
368 (c-invalidate-state-cache beg)
369 (c-invalidate-find-decl-cache beg)))
371 (defun c-basic-common-init (mode default-style)
372 "Do the necessary initialization for the syntax handling routines
373 and the line breaking/filling code. Intended to be used by other
374 packages that embed CC Mode.
376 MODE is the CC Mode flavor to set up, e.g. 'c-mode or 'java-mode.
377 DEFAULT-STYLE tells which indentation style to install. It has the
378 same format as `c-default-style'.
380 Note that `c-init-language-vars' must be called before this function.
381 This function cannot do that since `c-init-language-vars' is a macro
382 that requires a literal mode spec at compile time."
384 ;; This function does not do any hidden buffer changes.
386 (setq c-buffer-is-cc-mode mode)
388 ;; these variables should always be buffer local; they do not affect
389 ;; indentation style.
390 (make-local-variable 'parse-sexp-ignore-comments)
391 (make-local-variable 'indent-line-function)
392 (make-local-variable 'indent-region-function)
393 (make-local-variable 'normal-auto-fill-function)
394 (make-local-variable 'comment-start)
395 (make-local-variable 'comment-end)
396 (make-local-variable 'comment-start-skip)
397 (make-local-variable 'comment-multi-line)
398 (make-local-variable 'paragraph-start)
399 (make-local-variable 'paragraph-separate)
400 (make-local-variable 'paragraph-ignore-fill-prefix)
401 (make-local-variable 'adaptive-fill-mode)
402 (make-local-variable 'adaptive-fill-regexp)
404 ;; now set their values
405 (setq parse-sexp-ignore-comments t
406 indent-line-function 'c-indent-line
407 indent-region-function 'c-indent-region
408 normal-auto-fill-function 'c-do-auto-fill
409 comment-multi-line t)
411 ;; Install `c-fill-paragraph' on `fill-paragraph-function' so that a
412 ;; direct call to `fill-paragraph' behaves better. This still
413 ;; doesn't work with filladapt but it's better than nothing.
414 (make-local-variable 'fill-paragraph-function)
415 (setq fill-paragraph-function 'c-fill-paragraph)
417 ;; (X)Emacs 20 and later.
418 (when (boundp 'comment-line-break-function)
419 (make-local-variable 'comment-line-break-function)
420 (setq comment-line-break-function
421 'c-indent-new-comment-line))
423 ;; Emacs 20 and later.
424 (when (boundp 'parse-sexp-lookup-properties)
425 (make-local-variable 'parse-sexp-lookup-properties)
426 (setq parse-sexp-lookup-properties t))
428 ;; Same as above for XEmacs 21 (although currently undocumented).
429 (when (boundp 'lookup-syntax-properties)
430 (make-local-variable 'lookup-syntax-properties)
431 (setq lookup-syntax-properties t))
433 ;; Use this in Emacs 21 to avoid meddling with the rear-nonsticky
434 ;; property on each character.
435 (when (boundp 'text-property-default-nonsticky)
436 (make-local-variable 'text-property-default-nonsticky)
437 (let ((elem (assq 'syntax-table text-property-default-nonsticky)))
440 (setq text-property-default-nonsticky
441 (cons '(syntax-table . t)
442 text-property-default-nonsticky))))
443 (setq text-property-default-nonsticky
445 text-property-default-nonsticky)))
447 ;; In Emacs 21 and later it's possible to turn off the ad-hoc
448 ;; heuristic that open parens in column 0 are defun starters. Since
449 ;; we have c-state-cache that isn't useful and only causes trouble
451 (when (memq 'col-0-paren c-emacs-features)
452 (make-local-variable 'open-paren-in-column-0-is-defun-start)
453 (setq open-paren-in-column-0-is-defun-start nil))
455 ;; The `c-type' text property with `c-decl-end' is used to mark the
456 ;; ends of access keys to make interactive refontification work
458 (when c-opt-access-key
459 (setq c-type-decl-end-used t))
461 (c-clear-found-types)
463 ;; now set the mode style based on default-style
464 (let ((style (if (stringp default-style)
466 (or (cdr (assq mode default-style))
467 (cdr (assq 'other default-style))
469 ;; Override style variables if `c-old-style-variable-behavior' is
470 ;; set. Also override if we are using global style variables,
471 ;; have already initialized a style once, and are switching to a
472 ;; different style. (It's doubtful whether this is desirable, but
473 ;; the whole situation with nonlocal style variables is a bit
474 ;; awkward. It's at least the most compatible way with the old
475 ;; style init procedure.)
476 (c-set-style style (not (or c-old-style-variable-behavior
477 (and (not c-style-variables-are-local-p)
479 (not (string-equal c-indentation-style
481 (c-setup-paragraph-variables)
483 ;; we have to do something special for c-offsets-alist so that the
484 ;; buffer local value has its own alist structure.
485 (setq c-offsets-alist (copy-alist c-offsets-alist))
487 ;; setup the comment indent variable in a Emacs version portable way
488 (make-local-variable 'comment-indent-function)
489 (setq comment-indent-function 'c-comment-indent)
491 ;; put auto-hungry designators onto minor-mode-alist, but only once
492 (or (assq 'c-auto-hungry-string minor-mode-alist)
493 (setq minor-mode-alist
494 (cons '(c-auto-hungry-string c-auto-hungry-string)
497 ;; Install the functions that ensure that various internal caches
498 ;; don't become invalid due to buffer changes.
499 (add-hook 'after-change-functions 'c-after-change nil t))
501 (defun c-after-font-lock-init ()
502 ;; Put on `font-lock-mode-hook'.
503 (remove-hook 'after-change-functions 'c-after-change t)
504 (add-hook 'after-change-functions 'c-after-change nil t))
506 (defun c-font-lock-init ()
507 "Set up the font-lock variables for using the font-lock support in CC Mode.
508 This does not load the font-lock package. Use after
509 `c-basic-common-init' and after cc-fonts has been loaded."
511 (make-local-variable 'font-lock-defaults)
512 (setq font-lock-defaults
513 `(,(if (c-mode-is-new-awk-p)
514 ;; awk-mode currently has only one font lock level.
515 'awk-font-lock-keywords
516 (mapcar 'c-mode-symbol
517 '("font-lock-keywords" "font-lock-keywords-1"
518 "font-lock-keywords-2" "font-lock-keywords-3")))
520 ,c-identifier-syntax-modifications
521 c-beginning-of-syntax
522 (font-lock-mark-block-function
524 (add-hook 'font-lock-mode-hook 'c-after-font-lock-init nil t))
526 (defun c-setup-doc-comment-style ()
527 "Initialize the variables that depend on the value of `c-doc-comment-style'."
528 (when (and (featurep 'font-lock)
529 (symbol-value 'font-lock-mode))
530 ;; Force font lock mode to reinitialize itself.
534 (defun c-common-init (&optional mode)
535 "Common initialization for all CC Mode modes.
536 In addition to the work done by `c-basic-common-init' and
537 `c-font-lock-init', this function sets up various other things as
538 customary in CC Mode modes but which aren't strictly necessary for CC
539 Mode to operate correctly.
541 MODE is the symbol for the mode to initialize, like 'c-mode. See
542 `c-basic-common-init' for details. It's only optional to be
543 compatible with old code; callers should always specify it.
545 This function does not do any hidden buffer changes."
548 ;; Called from an old third party package. The fallback is to
550 (c-init-language-vars-for 'c-mode))
552 (c-basic-common-init mode c-default-style)
554 ;; Only initialize font locking if we aren't called from an old package.
557 (make-local-variable 'outline-regexp)
558 (make-local-variable 'outline-level)
559 (setq outline-regexp "[^#\n\^M]"
560 outline-level 'c-outline-level)
562 (let ((rfn (assq mode c-require-final-newline)))
564 (make-local-variable 'require-final-newline)
566 (setq require-final-newline mode-require-final-newline)))))
568 (defun c-postprocess-file-styles ()
569 "Function that post processes relevant file local variables in CC Mode.
570 Currently, this function simply applies any style and offset settings
571 found in the file's Local Variable list. It first applies any style
572 setting found in `c-file-style', then it applies any offset settings
573 it finds in `c-file-offsets'.
575 Note that the style variables are always made local to the buffer."
577 ;; This function does not do any hidden buffer changes.
579 ;; apply file styles and offsets
580 (when c-buffer-is-cc-mode
581 (if (or c-file-style c-file-offsets)
582 (c-make-styles-buffer-local t))
584 (c-set-style c-file-style))
588 (let ((langelem (car langentry))
589 (offset (cdr langentry)))
590 (c-set-offset langelem offset)))
593 (add-hook 'hack-local-variables-hook 'c-postprocess-file-styles)
595 (defmacro c-run-mode-hooks (&rest hooks)
596 ;; Emacs 21.1 has introduced a system with delayed mode hooks that
597 ;; require the use of the new function `run-mode-hooks'.
598 (if (cc-bytecomp-fboundp 'run-mode-hooks)
599 `(run-mode-hooks ,@hooks)
600 `(progn ,@(mapcar (lambda (hook) `(run-hooks ,hook)) hooks))))
606 (defvar c-mode-syntax-table nil
607 "Syntax table used in c-mode buffers.")
608 (or c-mode-syntax-table
609 (setq c-mode-syntax-table
610 (funcall (c-lang-const c-make-mode-syntax-table c))))
612 (defvar c-mode-abbrev-table nil
613 "Abbreviation table used in c-mode buffers.")
614 (c-define-abbrev-table 'c-mode-abbrev-table
615 '(("else" "else" c-electric-continued-statement 0)
616 ("while" "while" c-electric-continued-statement 0)))
618 (defvar c-mode-map ()
619 "Keymap used in c-mode buffers.")
622 (setq c-mode-map (c-make-inherited-keymap))
623 ;; add bindings which are only useful for C
624 (define-key c-mode-map "\C-c\C-e" 'c-macro-expand)
627 (easy-menu-define c-c-menu c-mode-map "C Mode Commands"
628 (cons "C" (c-lang-const c-mode-menu c)))
630 ;; In XEmacs >= 21.5 modes should add their own entries to
631 ;; `auto-mode-alist'. The comment form of autoload is used to avoid
632 ;; doing this on load. That since `add-to-list' prepends the value
633 ;; which could cause it to clobber user settings. Later emacsen have
634 ;; an append option, but it's not safe to use.
636 ;; The the extension ".C" is associated to C++ while the lowercase
637 ;; variant goes to C. On case insensitive file systems, this means
638 ;; that ".c" files also might open C++ mode if the C++ entry comes
639 ;; first on `auto-mode-alist'. Thus we try to ensure that ".C" comes
640 ;; after ".c", and since `add-to-list' adds the entry first we have to
641 ;; add the ".C" entry first.
642 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(cc\\|hh\\)\\'" . c++-mode))
643 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.[ch]\\(pp\\|xx\\|\\+\\+\\)\\'" . c++-mode))
644 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(CC?\\|HH?\\)\\'" . c++-mode))
646 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.[ch]\\'" . c-mode))
648 ;; NB: The following two associate yacc and lex files to C Mode, which
649 ;; is not really suitable for those formats. Anyway, afaik there's
650 ;; currently no better mode for them, and besides this is legacy.
651 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.y\\(acc\\)?\\'" . c-mode))
652 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.lex\\'" . c-mode))
656 "Major mode for editing K&R and ANSI C code.
657 To submit a problem report, enter `\\[c-submit-bug-report]' from a
658 c-mode buffer. This automatically sets up a mail buffer with version
659 information already added. You just need to add a description of the
660 problem, including a reproducible test case, and send the message.
662 To see what version of CC Mode you are running, enter `\\[c-version]'.
664 The hook `c-mode-common-hook' is run with no args at mode
665 initialization, then `c-mode-hook'.
670 (kill-all-local-variables)
671 (c-initialize-cc-mode t)
672 (set-syntax-table c-mode-syntax-table)
673 (setq major-mode 'c-mode
675 local-abbrev-table c-mode-abbrev-table
677 (use-local-map c-mode-map)
678 (c-init-language-vars-for 'c-mode)
679 (c-common-init 'c-mode)
680 (easy-menu-add c-c-menu)
681 (cc-imenu-init cc-imenu-c-generic-expression)
682 (c-run-mode-hooks 'c-mode-common-hook 'c-mode-hook)
689 (defvar c++-mode-syntax-table nil
690 "Syntax table used in c++-mode buffers.")
691 (or c++-mode-syntax-table
692 (setq c++-mode-syntax-table
693 (funcall (c-lang-const c-make-mode-syntax-table c++))))
695 (defvar c++-mode-abbrev-table nil
696 "Abbreviation table used in c++-mode buffers.")
697 (c-define-abbrev-table 'c++-mode-abbrev-table
698 '(("else" "else" c-electric-continued-statement 0)
699 ("while" "while" c-electric-continued-statement 0)
700 ("catch" "catch" c-electric-continued-statement 0)))
702 (defvar c++-mode-map ()
703 "Keymap used in c++-mode buffers.")
706 (setq c++-mode-map (c-make-inherited-keymap))
707 ;; add bindings which are only useful for C++
708 (define-key c++-mode-map "\C-c\C-e" 'c-macro-expand)
709 (define-key c++-mode-map "\C-c:" 'c-scope-operator)
710 (define-key c++-mode-map "<" 'c-electric-lt-gt)
711 (define-key c++-mode-map ">" 'c-electric-lt-gt))
713 (easy-menu-define c-c++-menu c++-mode-map "C++ Mode Commands"
714 (cons "C++" (c-lang-const c-mode-menu c++)))
718 "Major mode for editing C++ code.
719 To submit a problem report, enter `\\[c-submit-bug-report]' from a
720 c++-mode buffer. This automatically sets up a mail buffer with
721 version information already added. You just need to add a description
722 of the problem, including a reproducible test case, and send the
725 To see what version of CC Mode you are running, enter `\\[c-version]'.
727 The hook `c-mode-common-hook' is run with no args at mode
728 initialization, then `c++-mode-hook'.
733 (kill-all-local-variables)
734 (c-initialize-cc-mode t)
735 (set-syntax-table c++-mode-syntax-table)
736 (setq major-mode 'c++-mode
738 local-abbrev-table c++-mode-abbrev-table
740 (use-local-map c++-mode-map)
741 (c-init-language-vars-for 'c++-mode)
742 (c-common-init 'c++-mode)
743 (easy-menu-add c-c++-menu)
744 (cc-imenu-init cc-imenu-c++-generic-expression)
745 (c-run-mode-hooks 'c-mode-common-hook 'c++-mode-hook)
749 ;; Support for Objective-C
752 (defvar objc-mode-syntax-table nil
753 "Syntax table used in objc-mode buffers.")
754 (or objc-mode-syntax-table
755 (setq objc-mode-syntax-table
756 (funcall (c-lang-const c-make-mode-syntax-table objc))))
758 (defvar objc-mode-abbrev-table nil
759 "Abbreviation table used in objc-mode buffers.")
760 (c-define-abbrev-table 'objc-mode-abbrev-table
761 '(("else" "else" c-electric-continued-statement 0)
762 ("while" "while" c-electric-continued-statement 0)))
764 (defvar objc-mode-map ()
765 "Keymap used in objc-mode buffers.")
768 (setq objc-mode-map (c-make-inherited-keymap))
769 ;; add bindings which are only useful for Objective-C
770 (define-key objc-mode-map "\C-c\C-e" 'c-macro-expand))
772 (easy-menu-define c-objc-menu objc-mode-map "ObjC Mode Commands"
773 (cons "ObjC" (c-lang-const c-mode-menu objc)))
775 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.m\\'" . objc-mode))
779 "Major mode for editing Objective C code.
780 To submit a problem report, enter `\\[c-submit-bug-report]' from an
781 objc-mode buffer. This automatically sets up a mail buffer with
782 version information already added. You just need to add a description
783 of the problem, including a reproducible test case, and send the
786 To see what version of CC Mode you are running, enter `\\[c-version]'.
788 The hook `c-mode-common-hook' is run with no args at mode
789 initialization, then `objc-mode-hook'.
794 (kill-all-local-variables)
795 (c-initialize-cc-mode t)
796 (set-syntax-table objc-mode-syntax-table)
797 (setq major-mode 'objc-mode
799 local-abbrev-table objc-mode-abbrev-table
801 ;; The `c-type' text property with `c-decl-end' is used to mark the
802 ;; end of the @-style directives.
803 (setq c-type-decl-end-used t)
804 (use-local-map objc-mode-map)
805 (c-init-language-vars-for 'objc-mode)
806 (c-common-init 'objc-mode)
807 (easy-menu-add c-objc-menu)
808 (cc-imenu-init nil 'cc-imenu-objc-function)
809 (c-run-mode-hooks 'c-mode-common-hook 'objc-mode-hook)
816 (defvar java-mode-syntax-table nil
817 "Syntax table used in java-mode buffers.")
818 (or java-mode-syntax-table
819 (setq java-mode-syntax-table
820 (funcall (c-lang-const c-make-mode-syntax-table java))))
822 (defvar java-mode-abbrev-table nil
823 "Abbreviation table used in java-mode buffers.")
824 (c-define-abbrev-table 'java-mode-abbrev-table
825 '(("else" "else" c-electric-continued-statement 0)
826 ("while" "while" c-electric-continued-statement 0)
827 ("catch" "catch" c-electric-continued-statement 0)
828 ("finally" "finally" c-electric-continued-statement 0)))
830 (defvar java-mode-map ()
831 "Keymap used in java-mode buffers.")
834 (setq java-mode-map (c-make-inherited-keymap))
835 ;; add bindings which are only useful for Java
838 ;; Regexp trying to describe the beginning of a Java top-level
839 ;; definition. This is not used by CC Mode, nor is it maintained
840 ;; since it's practically impossible to write a regexp that reliably
841 ;; matches such a construct. Other tools are necessary.
842 (defconst c-Java-defun-prompt-regexp
843 "^[ \t]*\\(\\(\\(public\\|protected\\|private\\|const\\|abstract\\|synchronized\\|final\\|static\\|threadsafe\\|transient\\|native\\|volatile\\)\\s-+\\)*\\(\\(\\([[a-zA-Z][][_$.a-zA-Z0-9]*[][_$.a-zA-Z0-9]+\\|[[a-zA-Z]\\)\\s-*\\)\\s-+\\)\\)?\\(\\([[a-zA-Z][][_$.a-zA-Z0-9]*\\s-+\\)\\s-*\\)?\\([_a-zA-Z][^][ \t:;.,{}()
\7f=]*\\|\\([_$a-zA-Z][_$.a-zA-Z0-9]*\\)\\)\\s-*\\(([^);{}]*)\\)?\\([] \t]*\\)\\(\\s-*\\<throws\\>\\s-*\\(\\([_$a-zA-Z][_$.a-zA-Z0-9]*\\)[, \t\n\r\f\v]*\\)+\\)?\\s-*")
845 (easy-menu-define c-java-menu java-mode-map "Java Mode Commands"
846 (cons "Java" (c-lang-const c-mode-menu java)))
848 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.java\\'" . java-mode))
852 "Major mode for editing Java code.
853 To submit a problem report, enter `\\[c-submit-bug-report]' from a
854 java-mode buffer. This automatically sets up a mail buffer with
855 version information already added. You just need to add a description
856 of the problem, including a reproducible test case, and send the
859 To see what version of CC Mode you are running, enter `\\[c-version]'.
861 The hook `c-mode-common-hook' is run with no args at mode
862 initialization, then `java-mode-hook'.
867 (kill-all-local-variables)
868 (c-initialize-cc-mode t)
869 (set-syntax-table java-mode-syntax-table)
870 (setq major-mode 'java-mode
872 local-abbrev-table java-mode-abbrev-table
874 (use-local-map java-mode-map)
875 (c-init-language-vars-for 'java-mode)
876 (c-common-init 'java-mode)
877 (easy-menu-add c-java-menu)
878 (cc-imenu-init cc-imenu-java-generic-expression)
879 (c-run-mode-hooks 'c-mode-common-hook 'java-mode-hook)
883 ;; Support for CORBA's IDL language
886 (defvar idl-mode-syntax-table nil
887 "Syntax table used in idl-mode buffers.")
888 (or idl-mode-syntax-table
889 (setq idl-mode-syntax-table
890 (funcall (c-lang-const c-make-mode-syntax-table idl))))
892 (defvar idl-mode-abbrev-table nil
893 "Abbreviation table used in idl-mode buffers.")
894 (c-define-abbrev-table 'idl-mode-abbrev-table nil)
896 (defvar idl-mode-map ()
897 "Keymap used in idl-mode buffers.")
900 (setq idl-mode-map (c-make-inherited-keymap))
901 ;; add bindings which are only useful for IDL
904 (easy-menu-define c-idl-menu idl-mode-map "IDL Mode Commands"
905 (cons "IDL" (c-lang-const c-mode-menu idl)))
907 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.idl\\'" . idl-mode))
911 "Major mode for editing CORBA's IDL, PSDL and CIDL code.
912 To submit a problem report, enter `\\[c-submit-bug-report]' from an
913 idl-mode buffer. This automatically sets up a mail buffer with
914 version information already added. You just need to add a description
915 of the problem, including a reproducible test case, and send the
918 To see what version of CC Mode you are running, enter `\\[c-version]'.
920 The hook `c-mode-common-hook' is run with no args at mode
921 initialization, then `idl-mode-hook'.
926 (kill-all-local-variables)
927 (c-initialize-cc-mode t)
928 (set-syntax-table idl-mode-syntax-table)
929 (setq major-mode 'idl-mode
931 local-abbrev-table idl-mode-abbrev-table)
932 (use-local-map idl-mode-map)
933 (c-init-language-vars-for 'idl-mode)
934 (c-common-init 'idl-mode)
935 (easy-menu-add c-idl-menu)
936 ;;(cc-imenu-init cc-imenu-idl-generic-expression) ;TODO
937 (c-run-mode-hooks 'c-mode-common-hook 'idl-mode-hook)
944 (defvar pike-mode-syntax-table nil
945 "Syntax table used in pike-mode buffers.")
946 (or pike-mode-syntax-table
947 (setq pike-mode-syntax-table
948 (funcall (c-lang-const c-make-mode-syntax-table pike))))
950 (defvar pike-mode-abbrev-table nil
951 "Abbreviation table used in pike-mode buffers.")
952 (c-define-abbrev-table 'pike-mode-abbrev-table
953 '(("else" "else" c-electric-continued-statement 0)
954 ("while" "while" c-electric-continued-statement 0)))
956 (defvar pike-mode-map ()
957 "Keymap used in pike-mode buffers.")
960 (setq pike-mode-map (c-make-inherited-keymap))
961 ;; additional bindings
962 (define-key pike-mode-map "\C-c\C-e" 'c-macro-expand))
964 (easy-menu-define c-pike-menu pike-mode-map "Pike Mode Commands"
965 (cons "Pike" (c-lang-const c-mode-menu pike)))
967 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(u?lpc\\|pike\\|pmod\\(.in\\)?\\)\\'" . pike-mode))
968 ;;;###autoload (add-to-list 'interpreter-mode-alist '("pike" . pike-mode))
972 "Major mode for editing Pike code.
973 To submit a problem report, enter `\\[c-submit-bug-report]' from a
974 pike-mode buffer. This automatically sets up a mail buffer with
975 version information already added. You just need to add a description
976 of the problem, including a reproducible test case, and send the
979 To see what version of CC Mode you are running, enter `\\[c-version]'.
981 The hook `c-mode-common-hook' is run with no args at mode
982 initialization, then `pike-mode-hook'.
987 (kill-all-local-variables)
988 (c-initialize-cc-mode t)
989 (set-syntax-table pike-mode-syntax-table)
990 (setq major-mode 'pike-mode
992 local-abbrev-table pike-mode-abbrev-table
994 (use-local-map pike-mode-map)
995 (c-init-language-vars-for 'pike-mode)
996 (c-common-init 'pike-mode)
997 (easy-menu-add c-pike-menu)
998 ;;(cc-imenu-init cc-imenu-pike-generic-expression) ;TODO
999 (c-run-mode-hooks 'c-mode-common-hook 'pike-mode-hook)
1000 (c-update-modeline))
1003 ;; Support for awk. This is purposely disabled for older (X)Emacsen which
1004 ;; don't support syntax-table properties.
1006 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.awk\\'" . awk-mode))
1007 ;;;###autoload (add-to-list 'interpreter-mode-alist '("awk" . awk-mode))
1008 ;;;###autoload (add-to-list 'interpreter-mode-alist '("mawk" . awk-mode))
1009 ;;;###autoload (add-to-list 'interpreter-mode-alist '("nawk" . awk-mode))
1010 ;;;###autoload (add-to-list 'interpreter-mode-alist '("gawk" . awk-mode))
1012 ;;; Autoload directives must be on the top level, so we construct an
1013 ;;; autoload form instead.
1014 ;;;###autoload (autoload 'awk-mode "cc-mode" "Major mode for editing AWK code." t)
1016 (if (not (memq 'syntax-properties c-emacs-features))
1017 (autoload 'awk-mode "awk-mode" "Major mode for editing AWK code." t)
1019 (defvar awk-mode-abbrev-table nil
1020 "Abbreviation table used in awk-mode buffers.")
1021 (c-define-abbrev-table 'awk-mode-abbrev-table
1022 '(("else" "else" c-electric-continued-statement 0)
1023 ("while" "while" c-electric-continued-statement 0)))
1025 (defvar awk-mode-map ()
1026 "Keymap used in awk-mode buffers.")
1029 (setq awk-mode-map (c-make-inherited-keymap))
1030 ;; add bindings which are only useful for awk.
1031 (define-key awk-mode-map "#" 'self-insert-command)
1032 (define-key awk-mode-map "/" 'self-insert-command)
1033 (define-key awk-mode-map "*" 'self-insert-command)
1034 (define-key awk-mode-map "\C-c\C-n" 'undefined) ; #if doesn't exist in awk.
1035 (define-key awk-mode-map "\C-c\C-p" 'undefined)
1036 (define-key awk-mode-map "\C-c\C-u" 'undefined)
1037 (define-key awk-mode-map "\M-a" 'undefined) ; c-awk-beginning-of-statement isn't yet implemented.
1038 (define-key awk-mode-map "\M-e" 'undefined) ; c-awk-end-of-statement isn't yet implemented.
1039 (define-key awk-mode-map "\C-\M-a" 'c-awk-beginning-of-defun)
1040 (define-key awk-mode-map "\C-\M-e" 'c-awk-end-of-defun))
1042 (easy-menu-define c-awk-menu awk-mode-map "AWK Mode Commands"
1043 (cons "AWK" (c-lang-const c-mode-menu awk)))
1046 "Major mode for editing AWK code.
1047 To submit a problem report, enter `\\[c-submit-bug-report]' from an
1048 awk-mode buffer. This automatically sets up a mail buffer with version
1049 information already added. You just need to add a description of the
1050 problem, including a reproducible test case, and send the message.
1052 To see what version of CC Mode you are running, enter `\\[c-version]'.
1054 The hook `c-mode-common-hook' is run with no args at mode
1055 initialization, then `awk-mode-hook'.
1060 (require 'cc-awk) ; Added 2003/6/10.
1061 (kill-all-local-variables)
1062 (c-initialize-cc-mode t)
1063 (set-syntax-table awk-mode-syntax-table)
1064 (setq major-mode 'awk-mode
1066 local-abbrev-table awk-mode-abbrev-table
1068 (use-local-map awk-mode-map)
1069 (c-init-language-vars-for 'awk-mode)
1070 (c-common-init 'awk-mode)
1071 ;; The rest of CC Mode does not (yet) use `font-lock-syntactic-keywords',
1072 ;; so it's not set by `c-font-lock-init'.
1073 (make-local-variable 'font-lock-syntactic-keywords)
1074 (setq font-lock-syntactic-keywords
1075 '((c-awk-set-syntax-table-properties
1076 0 (0) ; Everything on this line is a dummy.
1078 (c-awk-unstick-NL-prop)
1079 (add-hook 'before-change-functions 'c-awk-before-change nil t)
1080 (add-hook 'after-change-functions 'c-awk-after-change nil t)
1081 (c-save-buffer-state nil
1084 (c-awk-clear-NL-props (point-min) (point-max))
1085 (c-awk-after-change (point-min) (point-max) 0))) ; Set syntax-table props.
1087 ;; Prevent Xemacs's buffer-syntactic-context being used. See the comment
1088 ;; in cc-engine.el, just before (defun c-fast-in-literal ...
1089 (defalias 'c-in-literal 'c-slow-in-literal)
1091 (c-run-mode-hooks 'c-mode-common-hook 'awk-mode-hook)
1092 (c-update-modeline))
1093 ) ;; closes the (if (not (memq 'syntax-properties c-emacs-features))
1098 (defconst c-mode-help-address
1099 "bug-cc-mode@gnu.org"
1100 "Address(es) for CC Mode bug reports.")
1103 "Echo the current version of CC Mode in the minibuffer."
1105 (message "Using CC Mode version %s" c-version)
1106 (c-keep-region-active))
1108 (defvar c-prepare-bug-report-hooks nil)
1110 ;; Dynamic variables used by reporter.
1111 (defvar reporter-prompt-for-summary-p)
1112 (defvar reporter-dont-compact-list)
1114 (defun c-submit-bug-report ()
1115 "Submit via mail a bug report on CC Mode."
1119 (let ((reporter-prompt-for-summary-p t)
1120 (reporter-dont-compact-list '(c-offsets-alist))
1121 (style c-indentation-style)
1122 (c-features c-emacs-features))
1124 (if (y-or-n-p "Do you want to submit a report on CC Mode? ")
1126 (reporter-submit-bug-report
1128 (concat "CC Mode " c-version " (" mode-name ")")
1131 '(c-buffer-is-cc-mode
1133 c-syntactic-indentation
1134 c-syntactic-indentation-in-macros
1136 c-auto-align-backslashes
1137 c-backspace-function
1139 c-electric-pound-behavior
1141 c-enable-xemacs-performance-kludge-p
1142 c-old-style-variable-behavior
1146 parse-sexp-ignore-comments
1147 parse-sexp-lookup-properties
1148 lookup-syntax-properties
1149 ;; A brain-damaged XEmacs only variable that, if
1150 ;; set to nil can cause all kinds of chaos.
1151 signal-error-on-buffer-boundary
1152 ;; Variables that affect line breaking and comments.
1162 adaptive-fill-regexp)
1164 (mapcar (lambda (var) (unless (boundp var)
1165 (setq vars (delq var vars))))
1166 '(signal-error-on-buffer-boundary
1170 font-lock-maximum-decoration
1171 parse-sexp-lookup-properties
1172 lookup-syntax-properties))
1175 (run-hooks 'c-prepare-bug-report-hooks)
1176 (insert (format "Buffer Style: %s\nc-emacs-features: %s\n"
1177 style c-features)))))))
1180 (cc-provide 'cc-mode)
1182 ;;; arch-tag: 7825e5c4-fd09-439f-a04d-4c13208ba3d7
1183 ;;; cc-mode.el ends here