Update CC Mode to version 5.31.4. The detailed changes are those
[bpt/emacs.git] / lisp / progmodes / cc-mode.el
CommitLineData
d9e94c22 1;;; cc-mode.el --- major mode for editing C and similar languages
785eecbb 2
92ab3834
GM
3;; Copyright (C) 1985, 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4;; 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
5;; Foundation, Inc.
785eecbb 6
d9e94c22
MS
7;; Authors: 2003- Alan Mackenzie
8;; 1998- Martin Stjernholm
9;; 1992-1999 Barry A. Warsaw
785eecbb
RS
10;; 1987 Dave Detlefs and Stewart Clamen
11;; 1985 Richard M. Stallman
0ec8351b 12;; Maintainer: bug-cc-mode@gnu.org
785eecbb 13;; Created: a long, long, time ago. adapted from the original c-mode.el
785eecbb
RS
14;; Keywords: c languages oop
15
785eecbb
RS
16;; This file is part of GNU Emacs.
17
18;; GNU Emacs is free software; you can redistribute it and/or modify
19;; it under the terms of the GNU General Public License as published by
20;; the Free Software Foundation; either version 2, or (at your option)
21;; any later version.
22
23;; GNU Emacs is distributed in the hope that it will be useful,
24;; but WITHOUT ANY WARRANTY; without even the implied warranty of
25;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26;; GNU General Public License for more details.
27
28;; You should have received a copy of the GNU General Public License
3efc2cd7
MS
29;; along with this program; see the file COPYING. If not, write to
30;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
31;; Boston, MA 02110-1301, USA.
785eecbb 32
d9e94c22 33;;; Commentary:
130c507e
GM
34
35;; NOTE: Read the commentary below for the right way to submit bug reports!
36;; NOTE: See the accompanying texinfo manual for details on using this mode!
d9e94c22 37;; Note: The version string is in cc-defs.
785eecbb
RS
38
39;; This package provides GNU Emacs major modes for editing C, C++,
d9e94c22
MS
40;; Objective-C, Java, CORBA's IDL, Pike and AWK code. As of the
41;; latest Emacs and XEmacs releases, it is the default package for
42;; editing these languages. This package is called "CC Mode", and
43;; should be spelled exactly this way.
0ec8351b
BW
44
45;; CC Mode supports K&R and ANSI C, ANSI C++, Objective-C, Java,
d9e94c22
MS
46;; CORBA's IDL, Pike and AWK with a consistent indentation model
47;; across all modes. This indentation model is intuitive and very
48;; flexible, so that almost any desired style of indentation can be
49;; supported. Installation, usage, and programming details are
50;; contained in an accompanying texinfo manual.
785eecbb
RS
51
52;; CC Mode's immediate ancestors were, c++-mode.el, cplus-md.el, and
53;; cplus-md1.el..
54
785eecbb 55;; To submit bug reports, type "C-c C-b". These will be sent to
0ec8351b
BW
56;; bug-gnu-emacs@gnu.org (mirrored as the Usenet newsgroup
57;; gnu.emacs.bug) as well as bug-cc-mode@gnu.org, which directly
58;; contacts the CC Mode maintainers. Questions can sent to
59;; help-gnu-emacs@gnu.org (mirrored as gnu.emacs.help) and/or
130c507e
GM
60;; bug-cc-mode@gnu.org. Please do not send bugs or questions to our
61;; personal accounts; we reserve the right to ignore such email!
785eecbb 62
785eecbb
RS
63;; Many, many thanks go out to all the folks on the beta test list.
64;; Without their patience, testing, insight, code contributions, and
65;; encouragement CC Mode would be a far inferior package.
66
67;; You can get the latest version of CC Mode, including PostScript
68;; documentation and separate individual files from:
69;;
91b807c9 70;; http://cc-mode.sourceforge.net/
0ec8351b
BW
71;;
72;; You can join a moderated CC Mode announcement-only mailing list by
73;; visiting
785eecbb 74;;
91b807c9 75;; http://lists.sourceforge.net/mailman/listinfo/cc-mode-announce
785eecbb
RS
76
77;;; Code:
78
51f606de
GM
79(eval-when-compile
80 (let ((load-path
130c507e
GM
81 (if (and (boundp 'byte-compile-dest-file)
82 (stringp byte-compile-dest-file))
83 (cons (file-name-directory byte-compile-dest-file) load-path)
51f606de 84 load-path)))
d9e94c22 85 (load "cc-bytecomp" nil t)))
130c507e
GM
86
87(cc-require 'cc-defs)
d9e94c22 88(cc-require-when-compile 'cc-langs)
130c507e 89(cc-require 'cc-vars)
130c507e 90(cc-require 'cc-engine)
d9e94c22 91(cc-require 'cc-styles)
130c507e
GM
92(cc-require 'cc-cmds)
93(cc-require 'cc-align)
d9e94c22 94(cc-require 'cc-menus)
130c507e 95
3efc2cd7 96;; Silence the compiler.
0386b551 97(cc-bytecomp-defvar adaptive-fill-first-line-regexp) ; Emacs
130c507e 98(cc-bytecomp-defun set-keymap-parents) ; XEmacs
0386b551 99(cc-bytecomp-defun run-mode-hooks) ; Emacs 21.1
3efc2cd7 100(cc-bytecomp-obsolete-fun make-local-hook) ; Marked obsolete in Emacs 21.1.
130c507e 101
d9e94c22
MS
102;; We set these variables during mode init, yet we don't require
103;; font-lock.
104(cc-bytecomp-defvar font-lock-defaults)
105(cc-bytecomp-defvar font-lock-syntactic-keywords)
106
130c507e
GM
107;; Menu support for both XEmacs and Emacs. If you don't have easymenu
108;; with your version of Emacs, you are incompatible!
d9e94c22
MS
109(cc-external-require 'easymenu)
110
0386b551
AM
111;; Autoload directive for emacsen that doesn't have an older CC Mode
112;; version in the dist.
113(autoload 'c-subword-mode "cc-subword"
114 "Mode enabling subword movement and editing keys." t)
115
d9e94c22
MS
116;; Load cc-fonts first after font-lock is loaded, since it isn't
117;; necessary until font locking is requested.
3c0ab532
AM
118; (eval-after-load "font-lock" ; 2006-07-09: font-lock is now preloaded.
119; '
120(require 'cc-fonts) ;)
d9e94c22
MS
121
122;; cc-langs isn't loaded when we're byte compiled, so add autoload
123;; directives for the interface functions.
124(autoload 'c-make-init-lang-vars-fun "cc-langs")
125(autoload 'c-init-language-vars "cc-langs" nil nil 'macro)
785eecbb
RS
126
127\f
1dad6919
RS
128;; Other modes and packages which depend on CC Mode should do the
129;; following to make sure everything is loaded and available for their
130;; use:
131;;
132;; (require 'cc-mode)
a66cd3ee
MS
133;;
134;; And in the major mode function:
135;;
d9e94c22
MS
136;; (c-initialize-cc-mode t)
137;; (c-init-language-vars some-mode)
138;; (c-common-init 'some-mode) ; Or perhaps (c-basic-common-init 'some-mode)
139;;
28d88c16
MS
140;; If you're not writing a derived mode using the language variable
141;; system, then some-mode is one of the language modes directly
142;; supported by CC Mode. You can then use (c-init-language-vars-for
143;; 'some-mode) instead of `c-init-language-vars'.
144;; `c-init-language-vars-for' is a function that avoids the rather
145;; large expansion of `c-init-language-vars'.
146;;
147;; If you use `c-basic-common-init' then you might want to call
148;; `c-font-lock-init' too to set up CC Mode's font lock support.
149;;
d9e94c22
MS
150;; See cc-langs.el for further info. A small example of a derived mode
151;; is also available at <http://cc-mode.sourceforge.net/
152;; derived-mode-ex.el>.
1dad6919 153
a66cd3ee
MS
154(defun c-leave-cc-mode-mode ()
155 (setq c-buffer-is-cc-mode nil))
156
26b8f810
AM
157;; Make the `c-lang-setvar' variables buffer local in the current buffer.
158;; These are typically standard emacs variables such as `comment-start'.
159(defmacro c-make-emacs-variables-local ()
160 `(progn
161 ,@(mapcan (lambda (init)
162 `((make-local-variable ',(car init))))
163 (cdr c-emacs-variable-inits))))
164
28d88c16
MS
165(defun c-init-language-vars-for (mode)
166 "Initialize the language variables for one of the language modes
167directly supported by CC Mode. This can be used instead of the
168`c-init-language-vars' macro if the language you want to use is one of
169those, rather than a derived language defined through the language
170variable system (see \"cc-langs.el\")."
26b8f810 171 (c-make-emacs-variables-local)
28d88c16
MS
172 (cond ((eq mode 'c-mode) (c-init-language-vars c-mode))
173 ((eq mode 'c++-mode) (c-init-language-vars c++-mode))
174 ((eq mode 'objc-mode) (c-init-language-vars objc-mode))
175 ((eq mode 'java-mode) (c-init-language-vars java-mode))
176 ((eq mode 'idl-mode) (c-init-language-vars idl-mode))
177 ((eq mode 'pike-mode) (c-init-language-vars pike-mode))
178 ((eq mode 'awk-mode) (c-init-language-vars awk-mode))
179 (t (error "Unsupported mode %s" mode))))
180
aac90c52 181;;;###autoload
d9e94c22
MS
182(defun c-initialize-cc-mode (&optional new-style-init)
183 "Initialize CC Mode for use in the current buffer.
184If the optional NEW-STYLE-INIT is nil or left out then all necessary
185initialization to run CC Mode for the C language is done. Otherwise
28d88c16
MS
186only some basic setup is done, and a call to `c-init-language-vars' or
187`c-init-language-vars-for' is necessary too (which gives more
188control). See \"cc-mode.el\" for more info."
d9e94c22 189
611c76a7 190 (setq c-buffer-is-cc-mode t)
d9e94c22 191
0ec8351b
BW
192 (let ((initprop 'cc-mode-is-initialized)
193 c-initialization-ok)
194 (unless (get 'c-initialize-cc-mode initprop)
0ec8351b
BW
195 (unwind-protect
196 (progn
51f606de
GM
197 (put 'c-initialize-cc-mode initprop t)
198 (c-initialize-builtin-style)
0ec8351b 199 (run-hooks 'c-initialization-hook)
51f606de
GM
200 ;; Fix obsolete variables.
201 (if (boundp 'c-comment-continuation-stars)
202 (setq c-block-comment-prefix c-comment-continuation-stars))
a66cd3ee 203 (add-hook 'change-major-mode-hook 'c-leave-cc-mode-mode)
0ec8351b
BW
204 (setq c-initialization-ok t))
205 ;; Will try initialization hooks again if they failed.
d9e94c22
MS
206 (put 'c-initialize-cc-mode initprop c-initialization-ok))))
207
208 (unless new-style-init
28d88c16 209 (c-init-language-vars-for 'c-mode)))
1dad6919
RS
210
211\f
d9e94c22
MS
212;;; Common routines.
213
130c507e
GM
214(defvar c-mode-base-map ()
215 "Keymap shared by all CC Mode related modes.")
216
217(defun c-make-inherited-keymap ()
218 (let ((map (make-sparse-keymap)))
3efc2cd7
MS
219 ;; Necessary to use `cc-bytecomp-fboundp' below since this
220 ;; function is called from top-level forms that are evaluated
221 ;; while cc-bytecomp is active when one does M-x eval-buffer.
130c507e 222 (cond
d9e94c22 223 ;; XEmacs
3efc2cd7 224 ((cc-bytecomp-fboundp 'set-keymap-parents)
130c507e 225 (set-keymap-parents map c-mode-base-map))
d9e94c22 226 ;; Emacs
3efc2cd7 227 ((cc-bytecomp-fboundp 'set-keymap-parent)
130c507e
GM
228 (set-keymap-parent map c-mode-base-map))
229 ;; incompatible
230 (t (error "CC Mode is incompatible with this version of Emacs")))
231 map))
232
a66cd3ee
MS
233(defun c-define-abbrev-table (name defs)
234 ;; Compatibility wrapper for `define-abbrev' which passes a non-nil
235 ;; sixth argument for SYSTEM-FLAG in emacsen that support it
0386b551 236 ;; (currently only Emacs >= 21.2).
d9e94c22
MS
237 (let ((table (or (symbol-value name)
238 (progn (define-abbrev-table name nil)
239 (symbol-value name)))))
a66cd3ee
MS
240 (while defs
241 (condition-case nil
242 (apply 'define-abbrev table (append (car defs) '(t)))
243 (wrong-number-of-arguments
244 (apply 'define-abbrev table (car defs))))
245 (setq defs (cdr defs)))))
d9e94c22 246(put 'c-define-abbrev-table 'lisp-indent-function 1)
a66cd3ee 247
0386b551
AM
248(defun c-bind-special-erase-keys ()
249 ;; Only used in Emacs to bind C-c C-<delete> and C-c C-<backspace>
250 ;; to the proper keys depending on `normal-erase-is-backspace'.
251 (if normal-erase-is-backspace
252 (progn
253 (define-key c-mode-base-map (kbd "C-c C-<delete>")
254 'c-hungry-delete-forward)
255 (define-key c-mode-base-map (kbd "C-c C-<backspace>")
cb694ab7 256 'c-hungry-delete-backwards))
0386b551 257 (define-key c-mode-base-map (kbd "C-c C-<delete>")
cb694ab7 258 'c-hungry-delete-backwards)
0386b551
AM
259 (define-key c-mode-base-map (kbd "C-c C-<backspace>")
260 'c-hungry-delete-forward)))
261
130c507e
GM
262(if c-mode-base-map
263 nil
0386b551 264
130c507e 265 (setq c-mode-base-map (make-sparse-keymap))
0386b551 266
130c507e
GM
267 ;; Separate M-BS from C-M-h. The former should remain
268 ;; backward-kill-word.
269 (define-key c-mode-base-map [(control meta h)] 'c-mark-function)
270 (define-key c-mode-base-map "\e\C-q" 'c-indent-exp)
271 (substitute-key-definition 'backward-sentence
272 'c-beginning-of-statement
273 c-mode-base-map global-map)
274 (substitute-key-definition 'forward-sentence
275 'c-end-of-statement
276 c-mode-base-map global-map)
277 (substitute-key-definition 'indent-new-comment-line
278 'c-indent-new-comment-line
279 c-mode-base-map global-map)
0386b551
AM
280 (substitute-key-definition 'indent-for-tab-command
281 'c-indent-command
282 c-mode-base-map global-map)
a66cd3ee
MS
283 (when (fboundp 'comment-indent-new-line)
284 ;; indent-new-comment-line has changed name to
285 ;; comment-indent-new-line in Emacs 21.
286 (substitute-key-definition 'comment-indent-new-line
287 'c-indent-new-comment-line
288 c-mode-base-map global-map))
0386b551 289
130c507e 290 ;; RMS says don't make these the default.
51c9af45
AM
291 ;; (April 2006): RMS has now approved these commands as defaults.
292 (define-key c-mode-base-map "\e\C-a" 'c-beginning-of-defun)
293 (define-key c-mode-base-map "\e\C-e" 'c-end-of-defun)
0386b551 294
130c507e
GM
295 (define-key c-mode-base-map "\C-c\C-n" 'c-forward-conditional)
296 (define-key c-mode-base-map "\C-c\C-p" 'c-backward-conditional)
297 (define-key c-mode-base-map "\C-c\C-u" 'c-up-conditional)
d9e94c22
MS
298
299 ;; It doesn't suffice to put `c-fill-paragraph' on
300 ;; `fill-paragraph-function' since `c-fill-paragraph' must be called
301 ;; before any fill prefix adaption is done. E.g. `filladapt-mode'
302 ;; replaces `fill-paragraph' and does the adaption before calling
303 ;; `fill-paragraph-function', and we have to mask comments etc
304 ;; before that. Also, `c-fill-paragraph' chains on to
305 ;; `fill-paragraph' and the value on `fill-parapgraph-function' to
306 ;; do the actual filling work.
130c507e
GM
307 (substitute-key-definition 'fill-paragraph 'c-fill-paragraph
308 c-mode-base-map global-map)
309 ;; In XEmacs the default fill function is called
310 ;; fill-paragraph-or-region.
311 (substitute-key-definition 'fill-paragraph-or-region 'c-fill-paragraph
312 c-mode-base-map global-map)
d9e94c22 313
0386b551
AM
314 ;; We bind the forward deletion key and (implicitly) C-d to
315 ;; `c-electric-delete-forward', and the backward deletion key to
316 ;; `c-electric-backspace'. The hungry variants are bound to the
317 ;; same keys but prefixed with C-c. This implies that C-c C-d is
318 ;; `c-hungry-delete-forward'. For consistency, we bind not only C-c
cb694ab7
AM
319 ;; <backspace> to `c-hungry-delete-backwards' but also
320 ;; C-c C-<backspace>, so that the Ctrl key can be held down during
321 ;; the whole sequence regardless of the direction. This in turn
322 ;; implies that we bind C-c C-<delete> to `c-hungry-delete-forward',
323 ;; for the same reason.
0386b551 324
6d28be1d
EZ
325 ;; Bind the electric deletion functions to C-d and DEL. Emacs 21
326 ;; automatically maps the [delete] and [backspace] keys to these two
327 ;; depending on window system and user preferences. (In earlier
328 ;; versions it's possible to do the same by using `function-key-map'.)
329 (define-key c-mode-base-map "\C-d" 'c-electric-delete-forward)
330 (define-key c-mode-base-map "\177" 'c-electric-backspace)
0386b551 331 (define-key c-mode-base-map "\C-c\C-d" 'c-hungry-delete-forward)
cb694ab7
AM
332 (define-key c-mode-base-map [?\C-c ?\d] 'c-hungry-delete-backwards)
333 (define-key c-mode-base-map [?\C-c ?\C-\d] 'c-hungry-delete-backwards)
0386b551
AM
334 (define-key c-mode-base-map [?\C-c deletechar] 'c-hungry-delete-forward) ; C-c <delete> on a tty.
335 (define-key c-mode-base-map [?\C-c (control deletechar)] ; C-c C-<delete> on a tty.
336 'c-hungry-delete-forward)
337 (when (boundp 'normal-erase-is-backspace)
338 ;; The automatic C-d and DEL mapping functionality doesn't extend
339 ;; to special combinations like C-c C-<delete>, so we have to hook
340 ;; into the `normal-erase-is-backspace' system to bind it directly
341 ;; as appropriate.
342 (add-hook 'normal-erase-is-backspace-hook 'c-bind-special-erase-keys)
343 (c-bind-special-erase-keys))
344
345 (when (fboundp 'delete-forward-p)
346 ;; In XEmacs we fix the forward and backward deletion behavior by
347 ;; binding the keysyms for the [delete] and [backspace] keys
348 ;; directly, and use `delete-forward-p' to decide what [delete]
349 ;; should do. That's done in the XEmacs specific
350 ;; `c-electric-delete' and `c-hungry-delete' functions.
6d28be1d 351 (define-key c-mode-base-map [delete] 'c-electric-delete)
0386b551
AM
352 (define-key c-mode-base-map [backspace] 'c-electric-backspace)
353 (define-key c-mode-base-map (kbd "C-c <delete>") 'c-hungry-delete)
354 (define-key c-mode-base-map (kbd "C-c C-<delete>") 'c-hungry-delete)
cb694ab7
AM
355 (define-key c-mode-base-map (kbd "C-c <backspace>")
356 'c-hungry-delete-backwards)
357 (define-key c-mode-base-map (kbd "C-c C-<backspace>")
358 'c-hungry-delete-backwards))
0386b551
AM
359
360 (define-key c-mode-base-map "#" 'c-electric-pound)
361 (define-key c-mode-base-map "{" 'c-electric-brace)
362 (define-key c-mode-base-map "}" 'c-electric-brace)
130c507e 363 (define-key c-mode-base-map "/" 'c-electric-slash)
0386b551
AM
364 (define-key c-mode-base-map "*" 'c-electric-star)
365 (define-key c-mode-base-map ";" 'c-electric-semi&comma)
366 (define-key c-mode-base-map "," 'c-electric-semi&comma)
367 (define-key c-mode-base-map ":" 'c-electric-colon)
368 (define-key c-mode-base-map "(" 'c-electric-paren)
369 (define-key c-mode-base-map ")" 'c-electric-paren)
370
130c507e 371 (define-key c-mode-base-map "\C-c\C-\\" 'c-backslash-region)
0386b551 372 (define-key c-mode-base-map "\C-c\C-a" 'c-toggle-auto-newline)
130c507e
GM
373 (define-key c-mode-base-map "\C-c\C-b" 'c-submit-bug-report)
374 (define-key c-mode-base-map "\C-c\C-c" 'comment-region)
0386b551 375 (define-key c-mode-base-map "\C-c\C-l" 'c-toggle-electric-state)
130c507e 376 (define-key c-mode-base-map "\C-c\C-o" 'c-set-offset)
0386b551 377 (define-key c-mode-base-map "\C-c\C-q" 'c-indent-defun)
130c507e 378 (define-key c-mode-base-map "\C-c\C-s" 'c-show-syntactic-information)
0386b551 379 ;; (define-key c-mode-base-map "\C-c\C-t" 'c-toggle-auto-hungry-state) Commented out by ACM, 2005-03-05.
130c507e
GM
380 (define-key c-mode-base-map "\C-c." 'c-set-style)
381 ;; conflicts with OOBR
382 ;;(define-key c-mode-base-map "\C-c\C-v" 'c-version)
0386b551
AM
383 ;; (define-key c-mode-base-map "\C-c\C-y" 'c-toggle-hungry-state) Commented out by ACM, 2005-11-22.
384 (define-key c-mode-base-map "\C-c\C-w" 'c-subword-mode)
130c507e
GM
385 )
386
130c507e
GM
387;; We don't require the outline package, but we configure it a bit anyway.
388(cc-bytecomp-defvar outline-level)
389
d9e94c22
MS
390(defun c-mode-menu (modestr)
391 "Return a menu spec suitable for `easy-menu-define' that is exactly
392like the C mode menu except that the menu bar item name is MODESTR
393instead of \"C\".
394
395This function is provided for compatibility only; derived modes should
396preferably use the `c-mode-menu' language constant directly."
397 (cons modestr (c-lang-const c-mode-menu c)))
398
399;; Ugly hack to pull in the definition of `c-populate-syntax-table'
400;; from cc-langs to make it available at runtime. It's either this or
401;; moving the definition for it to cc-defs, but that would mean to
402;; break up the syntax table setup over two files.
403(defalias 'c-populate-syntax-table
404 (cc-eval-when-compile
405 (let ((f (symbol-function 'c-populate-syntax-table)))
406 (if (byte-code-function-p f) f (byte-compile f)))))
407
0386b551
AM
408;; CAUTION: Try to avoid installing things on
409;; `before-change-functions'. The macro `combine-after-change-calls'
410;; is used and it doesn't work if there are things on that hook. That
411;; can cause font lock functions to run in inconvenient places during
412;; temporary changes in some font lock support modes, causing extra
413;; unnecessary work and font lock glitches due to interactions between
414;; various text properties.
415
d9e94c22 416(defun c-after-change (beg end len)
0386b551
AM
417 ;; Function put on `after-change-functions' to adjust various caches
418 ;; etc. Prefer speed to finesse here, since there will be an order
419 ;; of magnitude more calls to this function than any of the
d9e94c22
MS
420 ;; functions that use the caches.
421 ;;
422 ;; Note that care must be taken so that this is called before any
423 ;; font-lock callbacks since we might get calls to functions using
424 ;; these caches from inside them, and we must thus be sure that this
425 ;; has already been executed.
d9e94c22
MS
426
427 (c-save-buffer-state ()
0386b551
AM
428 ;; When `combine-after-change-calls' is used we might get calls
429 ;; with regions outside the current narrowing. This has been
430 ;; observed in Emacs 20.7.
431 (save-restriction
cb694ab7
AM
432 (save-match-data ; c-recognize-<>-arglists changes match-data
433 (widen)
0386b551 434
cb694ab7
AM
435 (when (> end (point-max))
436 ;; Some emacsen might return positions past the end. This has been
437 ;; observed in Emacs 20.7 when rereading a buffer changed on disk
438 ;; (haven't been able to minimize it, but Emacs 21.3 appears to
439 ;; work).
440 (setq end (point-max))
441 (when (> beg end)
442 (setq beg end)))
0386b551 443
cb694ab7
AM
444 (c-invalidate-sws-region-after beg end)
445 (c-invalidate-state-cache beg)
446 (c-invalidate-find-decl-cache beg)
0386b551 447
cb694ab7
AM
448 (when c-recognize-<>-arglists
449 (c-after-change-check-<>-operators beg end))))))
d9e94c22
MS
450
451(defun c-basic-common-init (mode default-style)
452 "Do the necessary initialization for the syntax handling routines
453and the line breaking/filling code. Intended to be used by other
454packages that embed CC Mode.
455
456MODE is the CC Mode flavor to set up, e.g. 'c-mode or 'java-mode.
457DEFAULT-STYLE tells which indentation style to install. It has the
458same format as `c-default-style'.
459
460Note that `c-init-language-vars' must be called before this function.
461This function cannot do that since `c-init-language-vars' is a macro
462that requires a literal mode spec at compile time."
d9e94c22 463
a66cd3ee
MS
464 (setq c-buffer-is-cc-mode mode)
465
130c507e
GM
466 ;; these variables should always be buffer local; they do not affect
467 ;; indentation style.
130c507e
GM
468 (make-local-variable 'parse-sexp-ignore-comments)
469 (make-local-variable 'indent-line-function)
470 (make-local-variable 'indent-region-function)
130c507e
GM
471 (make-local-variable 'normal-auto-fill-function)
472 (make-local-variable 'comment-start)
473 (make-local-variable 'comment-end)
130c507e
GM
474 (make-local-variable 'comment-start-skip)
475 (make-local-variable 'comment-multi-line)
0386b551
AM
476 (make-local-variable 'comment-line-break-function)
477 (make-local-variable 'paragraph-start)
478 (make-local-variable 'paragraph-separate)
479 (make-local-variable 'paragraph-ignore-fill-prefix)
480 (make-local-variable 'adaptive-fill-mode)
481 (make-local-variable 'adaptive-fill-regexp)
a66cd3ee 482
130c507e 483 ;; now set their values
e2c21e66 484 (setq parse-sexp-ignore-comments t
130c507e
GM
485 indent-line-function 'c-indent-line
486 indent-region-function 'c-indent-region
130c507e 487 normal-auto-fill-function 'c-do-auto-fill
0386b551
AM
488 comment-multi-line t
489 comment-line-break-function 'c-indent-new-comment-line)
a66cd3ee 490
fd8771f5
MS
491 ;; Install `c-fill-paragraph' on `fill-paragraph-function' so that a
492 ;; direct call to `fill-paragraph' behaves better. This still
493 ;; doesn't work with filladapt but it's better than nothing.
494 (make-local-variable 'fill-paragraph-function)
495 (setq fill-paragraph-function 'c-fill-paragraph)
496
0386b551
AM
497 (when (or c-recognize-<>-arglists
498 (c-major-mode-is 'awk-mode))
499 ;; We'll use the syntax-table text property to change the syntax
500 ;; of some chars for this language, so do the necessary setup for
501 ;; that.
502 ;;
503 ;; Note to other package developers: It's ok to turn this on in CC
504 ;; Mode buffers when CC Mode doesn't, but it's not ok to turn it
505 ;; off if CC Mode has turned it on.
506
507 ;; Emacs.
508 (when (boundp 'parse-sexp-lookup-properties)
509 (make-local-variable 'parse-sexp-lookup-properties)
510 (setq parse-sexp-lookup-properties t))
511
512 ;; Same as above for XEmacs.
513 (when (boundp 'lookup-syntax-properties)
514 (make-local-variable 'lookup-syntax-properties)
515 (setq lookup-syntax-properties t)))
d9e94c22
MS
516
517 ;; Use this in Emacs 21 to avoid meddling with the rear-nonsticky
518 ;; property on each character.
519 (when (boundp 'text-property-default-nonsticky)
520 (make-local-variable 'text-property-default-nonsticky)
521 (let ((elem (assq 'syntax-table text-property-default-nonsticky)))
522 (if elem
523 (setcdr elem t)
524 (setq text-property-default-nonsticky
525 (cons '(syntax-table . t)
526 text-property-default-nonsticky))))
527 (setq text-property-default-nonsticky
528 (cons '(c-type . t)
529 text-property-default-nonsticky)))
530
531 ;; In Emacs 21 and later it's possible to turn off the ad-hoc
532 ;; heuristic that open parens in column 0 are defun starters. Since
0386b551
AM
533 ;; we have c-state-cache, that heuristic isn't useful and only causes
534 ;; trouble, so turn it off.
99c8496e
AM
535;; 2006/12/17: This facility is somewhat confused, and doesn't really seem
536;; helpful. Comment it out for now.
537;; (when (memq 'col-0-paren c-emacs-features)
538;; (make-local-variable 'open-paren-in-column-0-is-defun-start)
539;; (setq open-paren-in-column-0-is-defun-start nil))
d9e94c22 540
d9e94c22
MS
541 (c-clear-found-types)
542
543 ;; now set the mode style based on default-style
544 (let ((style (if (stringp default-style)
545 default-style
546 (or (cdr (assq mode default-style))
547 (cdr (assq 'other default-style))
130c507e
GM
548 "gnu"))))
549 ;; Override style variables if `c-old-style-variable-behavior' is
550 ;; set. Also override if we are using global style variables,
551 ;; have already initialized a style once, and are switching to a
552 ;; different style. (It's doubtful whether this is desirable, but
553 ;; the whole situation with nonlocal style variables is a bit
554 ;; awkward. It's at least the most compatible way with the old
555 ;; style init procedure.)
556 (c-set-style style (not (or c-old-style-variable-behavior
557 (and (not c-style-variables-are-local-p)
558 c-indentation-style
559 (not (string-equal c-indentation-style
560 style)))))))
a66cd3ee
MS
561 (c-setup-paragraph-variables)
562
130c507e
GM
563 ;; we have to do something special for c-offsets-alist so that the
564 ;; buffer local value has its own alist structure.
565 (setq c-offsets-alist (copy-alist c-offsets-alist))
a66cd3ee 566
130c507e 567 ;; setup the comment indent variable in a Emacs version portable way
130c507e
GM
568 (make-local-variable 'comment-indent-function)
569 (setq comment-indent-function 'c-comment-indent)
a66cd3ee 570
cb694ab7
AM
571;; ;; Put submode indicators onto minor-mode-alist, but only once.
572;; (or (assq 'c-submode-indicators minor-mode-alist)
573;; (setq minor-mode-alist
574;; (cons '(c-submode-indicators c-submode-indicators)
575;; minor-mode-alist)))
576 (c-update-modeline)
a66cd3ee 577
d9e94c22
MS
578 ;; Install the functions that ensure that various internal caches
579 ;; don't become invalid due to buffer changes.
0386b551 580 (make-local-hook 'after-change-functions)
d9e94c22
MS
581 (add-hook 'after-change-functions 'c-after-change nil t))
582
583(defun c-after-font-lock-init ()
584 ;; Put on `font-lock-mode-hook'.
585 (remove-hook 'after-change-functions 'c-after-change t)
586 (add-hook 'after-change-functions 'c-after-change nil t))
587
588(defun c-font-lock-init ()
589 "Set up the font-lock variables for using the font-lock support in CC Mode.
590This does not load the font-lock package. Use after
591`c-basic-common-init' and after cc-fonts has been loaded."
592
593 (make-local-variable 'font-lock-defaults)
594 (setq font-lock-defaults
0386b551 595 `(,(if (c-major-mode-is 'awk-mode)
d9e94c22
MS
596 ;; awk-mode currently has only one font lock level.
597 'awk-font-lock-keywords
598 (mapcar 'c-mode-symbol
599 '("font-lock-keywords" "font-lock-keywords-1"
600 "font-lock-keywords-2" "font-lock-keywords-3")))
601 nil nil
602 ,c-identifier-syntax-modifications
603 c-beginning-of-syntax
5ac71a6a 604 (font-lock-lines-before . 1)
d9e94c22
MS
605 (font-lock-mark-block-function
606 . c-mark-function)))
0386b551
AM
607
608 (make-local-hook 'font-lock-mode-hook)
d9e94c22
MS
609 (add-hook 'font-lock-mode-hook 'c-after-font-lock-init nil t))
610
611(defun c-setup-doc-comment-style ()
612 "Initialize the variables that depend on the value of `c-doc-comment-style'."
613 (when (and (featurep 'font-lock)
614 (symbol-value 'font-lock-mode))
615 ;; Force font lock mode to reinitialize itself.
616 (font-lock-mode 0)
617 (font-lock-mode 1)))
618
619(defun c-common-init (&optional mode)
620 "Common initialization for all CC Mode modes.
621In addition to the work done by `c-basic-common-init' and
622`c-font-lock-init', this function sets up various other things as
623customary in CC Mode modes but which aren't strictly necessary for CC
624Mode to operate correctly.
625
626MODE is the symbol for the mode to initialize, like 'c-mode. See
627`c-basic-common-init' for details. It's only optional to be
0386b551 628compatible with old code; callers should always specify it."
d9e94c22
MS
629
630 (unless mode
631 ;; Called from an old third party package. The fallback is to
632 ;; initialize for C.
28d88c16 633 (c-init-language-vars-for 'c-mode))
d9e94c22
MS
634
635 (c-basic-common-init mode c-default-style)
636 (when mode
637 ;; Only initialize font locking if we aren't called from an old package.
638 (c-font-lock-init))
639
640 (make-local-variable 'outline-regexp)
641 (make-local-variable 'outline-level)
642 (setq outline-regexp "[^#\n\^M]"
643 outline-level 'c-outline-level)
644
645 (let ((rfn (assq mode c-require-final-newline)))
646 (when rfn
647 (make-local-variable 'require-final-newline)
1750e02f
RS
648 (and (cdr rfn)
649 (setq require-final-newline mode-require-final-newline)))))
130c507e 650
cb694ab7
AM
651(defun c-remove-any-local-eval-or-mode-variables ()
652 ;; If the buffer specifies `mode' or `eval' in its File Local Variable list
653 ;; or on the first line, remove all occurrences. See
654 ;; `c-postprocess-file-styles' for justification. There is no need to save
655 ;; point here, or even bother too much about the buffer contents.
656 ;;
657 ;; Most of the code here is derived from Emacs 21.3's `hack-local-variables'
658 ;; in files.el.
659 (goto-char (point-max))
660 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
661 (let (lv-point (prefix "") (suffix ""))
662 (when (let ((case-fold-search t))
663 (search-forward "Local Variables:" nil t))
664 (setq lv-point (point))
665 ;; The prefix is what comes before "local variables:" in its line.
666 ;; The suffix is what comes after "local variables:" in its line.
667 (skip-chars-forward " \t")
668 (or (eolp)
669 (setq suffix (buffer-substring (point)
670 (progn (end-of-line) (point)))))
671 (goto-char (match-beginning 0))
672 (or (bolp)
673 (setq prefix
674 (buffer-substring (point)
675 (progn (beginning-of-line) (point)))))
676
677 (while (search-forward-regexp
678 (concat "^[ \t]*"
679 (regexp-quote prefix)
680 "\\(mode\\|eval\\):.*"
681 (regexp-quote suffix)
682 "$")
683 nil t)
684 (beginning-of-line)
685 (kill-line 1)))
686
687 ;; Delete the first line, if we've got one, in case it contains a mode spec.
688 (unless (and lv-point
689 (progn (goto-char lv-point)
690 (forward-line 0)
691 (bobp)))
692 (goto-char (point-min))
693 (unless (eobp)
694 (kill-line 1)))))
695
130c507e 696(defun c-postprocess-file-styles ()
d9e94c22 697 "Function that post processes relevant file local variables in CC Mode.
130c507e
GM
698Currently, this function simply applies any style and offset settings
699found in the file's Local Variable list. It first applies any style
700setting found in `c-file-style', then it applies any offset settings
701it finds in `c-file-offsets'.
702
703Note that the style variables are always made local to the buffer."
d9e94c22 704
130c507e 705 ;; apply file styles and offsets
09f90d2c
RS
706 (when c-buffer-is-cc-mode
707 (if (or c-file-style c-file-offsets)
708 (c-make-styles-buffer-local t))
709 (and c-file-style
710 (c-set-style c-file-style))
711 (and c-file-offsets
712 (mapcar
713 (lambda (langentry)
714 (let ((langelem (car langentry))
715 (offset (cdr langentry)))
716 (c-set-offset langelem offset)))
0386b551
AM
717 c-file-offsets))
718 ;; Problem: The file local variable block might have explicitly set a
719 ;; style variable. The `c-set-style' or `mapcar' call might have
720 ;; overwritten this. So we run `hack-local-variables' again to remedy
721 ;; this. There are no guarantees this will work properly, particularly as
722 ;; we have no control over what the other hook functions on
cb694ab7
AM
723 ;; `hack-local-variables-hook' would have done. We now (2006/2/1) remove
724 ;; any `eval' or `mode' expressions before we evaluate again (see below).
725 ;; ACM, 2005/11/2.
726 ;;
727 ;; Problem (bug reported by Gustav Broberg): if one of the variables is
728 ;; `mode', this will invoke c-mode (etc.) again, setting up the style etc.
729 ;; We prevent this by temporarily removing `mode' from the Local Variables
730 ;; section.
0386b551 731 (if (or c-file-style c-file-offsets)
51c9af45
AM
732 (let ((hack-local-variables-hook nil) (inhibit-read-only t))
733 (c-tentative-buffer-changes
cb694ab7
AM
734 (c-remove-any-local-eval-or-mode-variables)
735 (hack-local-variables))
736 nil))))
130c507e
GM
737
738(add-hook 'hack-local-variables-hook 'c-postprocess-file-styles)
739
3efc2cd7
MS
740(defmacro c-run-mode-hooks (&rest hooks)
741 ;; Emacs 21.1 has introduced a system with delayed mode hooks that
742 ;; require the use of the new function `run-mode-hooks'.
743 (if (cc-bytecomp-fboundp 'run-mode-hooks)
744 `(run-mode-hooks ,@hooks)
745 `(progn ,@(mapcar (lambda (hook) `(run-hooks ,hook)) hooks))))
746
130c507e
GM
747\f
748;; Support for C
749
d9e94c22
MS
750;;;###autoload
751(defvar c-mode-syntax-table nil
752 "Syntax table used in c-mode buffers.")
753(or c-mode-syntax-table
754 (setq c-mode-syntax-table
755 (funcall (c-lang-const c-make-mode-syntax-table c))))
756
130c507e
GM
757(defvar c-mode-abbrev-table nil
758 "Abbreviation table used in c-mode buffers.")
a66cd3ee
MS
759(c-define-abbrev-table 'c-mode-abbrev-table
760 '(("else" "else" c-electric-continued-statement 0)
761 ("while" "while" c-electric-continued-statement 0)))
130c507e
GM
762
763(defvar c-mode-map ()
764 "Keymap used in c-mode buffers.")
765(if c-mode-map
766 nil
767 (setq c-mode-map (c-make-inherited-keymap))
768 ;; add bindings which are only useful for C
769 (define-key c-mode-map "\C-c\C-e" 'c-macro-expand)
770 )
771
772(easy-menu-define c-c-menu c-mode-map "C Mode Commands"
d9e94c22
MS
773 (cons "C" (c-lang-const c-mode-menu c)))
774
775;; In XEmacs >= 21.5 modes should add their own entries to
776;; `auto-mode-alist'. The comment form of autoload is used to avoid
777;; doing this on load. That since `add-to-list' prepends the value
778;; which could cause it to clobber user settings. Later emacsen have
779;; an append option, but it's not safe to use.
50fdde0e
MS
780
781;; The the extension ".C" is associated to C++ while the lowercase
782;; variant goes to C. On case insensitive file systems, this means
783;; that ".c" files also might open C++ mode if the C++ entry comes
784;; first on `auto-mode-alist'. Thus we try to ensure that ".C" comes
785;; after ".c", and since `add-to-list' adds the entry first we have to
786;; add the ".C" entry first.
787;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(cc\\|hh\\)\\'" . c++-mode))
788;;;###autoload (add-to-list 'auto-mode-alist '("\\.[ch]\\(pp\\|xx\\|\\+\\+\\)\\'" . c++-mode))
789;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(CC?\\|HH?\\)\\'" . c++-mode))
790
d9e94c22
MS
791;;;###autoload (add-to-list 'auto-mode-alist '("\\.[ch]\\'" . c-mode))
792
793;; NB: The following two associate yacc and lex files to C Mode, which
794;; is not really suitable for those formats. Anyway, afaik there's
795;; currently no better mode for them, and besides this is legacy.
796;;;###autoload (add-to-list 'auto-mode-alist '("\\.y\\(acc\\)?\\'" . c-mode))
797;;;###autoload (add-to-list 'auto-mode-alist '("\\.lex\\'" . c-mode))
798
785eecbb
RS
799;;;###autoload
800(defun c-mode ()
801 "Major mode for editing K&R and ANSI C code.
802To submit a problem report, enter `\\[c-submit-bug-report]' from a
803c-mode buffer. This automatically sets up a mail buffer with version
804information already added. You just need to add a description of the
50fdde0e 805problem, including a reproducible test case, and send the message.
785eecbb
RS
806
807To see what version of CC Mode you are running, enter `\\[c-version]'.
808
d9e94c22
MS
809The hook `c-mode-common-hook' is run with no args at mode
810initialization, then `c-mode-hook'.
785eecbb
RS
811
812Key bindings:
813\\{c-mode-map}"
814 (interactive)
785eecbb 815 (kill-all-local-variables)
d9e94c22 816 (c-initialize-cc-mode t)
785eecbb
RS
817 (set-syntax-table c-mode-syntax-table)
818 (setq major-mode 'c-mode
819 mode-name "C"
130c507e
GM
820 local-abbrev-table c-mode-abbrev-table
821 abbrev-mode t)
785eecbb 822 (use-local-map c-mode-map)
28d88c16 823 (c-init-language-vars-for 'c-mode)
a66cd3ee 824 (c-common-init 'c-mode)
d9e94c22 825 (easy-menu-add c-c-menu)
51f606de 826 (cc-imenu-init cc-imenu-c-generic-expression)
3efc2cd7 827 (c-run-mode-hooks 'c-mode-common-hook 'c-mode-hook)
785eecbb
RS
828 (c-update-modeline))
829
830\f
130c507e
GM
831;; Support for C++
832
d9e94c22
MS
833;;;###autoload
834(defvar c++-mode-syntax-table nil
835 "Syntax table used in c++-mode buffers.")
836(or c++-mode-syntax-table
837 (setq c++-mode-syntax-table
838 (funcall (c-lang-const c-make-mode-syntax-table c++))))
839
130c507e
GM
840(defvar c++-mode-abbrev-table nil
841 "Abbreviation table used in c++-mode buffers.")
a66cd3ee
MS
842(c-define-abbrev-table 'c++-mode-abbrev-table
843 '(("else" "else" c-electric-continued-statement 0)
844 ("while" "while" c-electric-continued-statement 0)
845 ("catch" "catch" c-electric-continued-statement 0)))
130c507e
GM
846
847(defvar c++-mode-map ()
848 "Keymap used in c++-mode buffers.")
849(if c++-mode-map
850 nil
851 (setq c++-mode-map (c-make-inherited-keymap))
852 ;; add bindings which are only useful for C++
853 (define-key c++-mode-map "\C-c\C-e" 'c-macro-expand)
854 (define-key c++-mode-map "\C-c:" 'c-scope-operator)
855 (define-key c++-mode-map "<" 'c-electric-lt-gt)
856 (define-key c++-mode-map ">" 'c-electric-lt-gt))
857
858(easy-menu-define c-c++-menu c++-mode-map "C++ Mode Commands"
d9e94c22
MS
859 (cons "C++" (c-lang-const c-mode-menu c++)))
860
785eecbb
RS
861;;;###autoload
862(defun c++-mode ()
863 "Major mode for editing C++ code.
864To submit a problem report, enter `\\[c-submit-bug-report]' from a
865c++-mode buffer. This automatically sets up a mail buffer with
866version information already added. You just need to add a description
867of the problem, including a reproducible test case, and send the
868message.
869
870To see what version of CC Mode you are running, enter `\\[c-version]'.
871
d9e94c22
MS
872The hook `c-mode-common-hook' is run with no args at mode
873initialization, then `c++-mode-hook'.
785eecbb
RS
874
875Key bindings:
876\\{c++-mode-map}"
877 (interactive)
785eecbb 878 (kill-all-local-variables)
d9e94c22 879 (c-initialize-cc-mode t)
785eecbb
RS
880 (set-syntax-table c++-mode-syntax-table)
881 (setq major-mode 'c++-mode
882 mode-name "C++"
130c507e
GM
883 local-abbrev-table c++-mode-abbrev-table
884 abbrev-mode t)
785eecbb 885 (use-local-map c++-mode-map)
28d88c16 886 (c-init-language-vars-for 'c++-mode)
a66cd3ee 887 (c-common-init 'c++-mode)
d9e94c22 888 (easy-menu-add c-c++-menu)
51f606de 889 (cc-imenu-init cc-imenu-c++-generic-expression)
3efc2cd7 890 (c-run-mode-hooks 'c-mode-common-hook 'c++-mode-hook)
785eecbb
RS
891 (c-update-modeline))
892
893\f
130c507e
GM
894;; Support for Objective-C
895
d9e94c22
MS
896;;;###autoload
897(defvar objc-mode-syntax-table nil
898 "Syntax table used in objc-mode buffers.")
899(or objc-mode-syntax-table
900 (setq objc-mode-syntax-table
901 (funcall (c-lang-const c-make-mode-syntax-table objc))))
902
130c507e
GM
903(defvar objc-mode-abbrev-table nil
904 "Abbreviation table used in objc-mode buffers.")
a66cd3ee
MS
905(c-define-abbrev-table 'objc-mode-abbrev-table
906 '(("else" "else" c-electric-continued-statement 0)
907 ("while" "while" c-electric-continued-statement 0)))
130c507e
GM
908
909(defvar objc-mode-map ()
910 "Keymap used in objc-mode buffers.")
911(if objc-mode-map
912 nil
913 (setq objc-mode-map (c-make-inherited-keymap))
914 ;; add bindings which are only useful for Objective-C
915 (define-key objc-mode-map "\C-c\C-e" 'c-macro-expand))
916
917(easy-menu-define c-objc-menu objc-mode-map "ObjC Mode Commands"
d9e94c22
MS
918 (cons "ObjC" (c-lang-const c-mode-menu objc)))
919
920;;;###autoload (add-to-list 'auto-mode-alist '("\\.m\\'" . objc-mode))
130c507e 921
785eecbb
RS
922;;;###autoload
923(defun objc-mode ()
924 "Major mode for editing Objective C code.
925To submit a problem report, enter `\\[c-submit-bug-report]' from an
926objc-mode buffer. This automatically sets up a mail buffer with
927version information already added. You just need to add a description
928of the problem, including a reproducible test case, and send the
929message.
930
931To see what version of CC Mode you are running, enter `\\[c-version]'.
932
d9e94c22
MS
933The hook `c-mode-common-hook' is run with no args at mode
934initialization, then `objc-mode-hook'.
785eecbb
RS
935
936Key bindings:
937\\{objc-mode-map}"
938 (interactive)
785eecbb 939 (kill-all-local-variables)
d9e94c22 940 (c-initialize-cc-mode t)
785eecbb
RS
941 (set-syntax-table objc-mode-syntax-table)
942 (setq major-mode 'objc-mode
943 mode-name "ObjC"
130c507e
GM
944 local-abbrev-table objc-mode-abbrev-table
945 abbrev-mode t)
785eecbb 946 (use-local-map objc-mode-map)
28d88c16 947 (c-init-language-vars-for 'objc-mode)
a66cd3ee 948 (c-common-init 'objc-mode)
d9e94c22
MS
949 (easy-menu-add c-objc-menu)
950 (cc-imenu-init nil 'cc-imenu-objc-function)
3efc2cd7 951 (c-run-mode-hooks 'c-mode-common-hook 'objc-mode-hook)
785eecbb
RS
952 (c-update-modeline))
953
954\f
130c507e
GM
955;; Support for Java
956
d9e94c22
MS
957;;;###autoload
958(defvar java-mode-syntax-table nil
959 "Syntax table used in java-mode buffers.")
960(or java-mode-syntax-table
961 (setq java-mode-syntax-table
962 (funcall (c-lang-const c-make-mode-syntax-table java))))
963
130c507e
GM
964(defvar java-mode-abbrev-table nil
965 "Abbreviation table used in java-mode buffers.")
a66cd3ee
MS
966(c-define-abbrev-table 'java-mode-abbrev-table
967 '(("else" "else" c-electric-continued-statement 0)
968 ("while" "while" c-electric-continued-statement 0)
969 ("catch" "catch" c-electric-continued-statement 0)
970 ("finally" "finally" c-electric-continued-statement 0)))
130c507e
GM
971
972(defvar java-mode-map ()
973 "Keymap used in java-mode buffers.")
974(if java-mode-map
975 nil
976 (setq java-mode-map (c-make-inherited-keymap))
977 ;; add bindings which are only useful for Java
978 )
979
d9e94c22
MS
980;; Regexp trying to describe the beginning of a Java top-level
981;; definition. This is not used by CC Mode, nor is it maintained
982;; since it's practically impossible to write a regexp that reliably
983;; matches such a construct. Other tools are necessary.
984(defconst c-Java-defun-prompt-regexp
985 "^[ \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-*")
986
130c507e 987(easy-menu-define c-java-menu java-mode-map "Java Mode Commands"
d9e94c22
MS
988 (cons "Java" (c-lang-const c-mode-menu java)))
989
990;;;###autoload (add-to-list 'auto-mode-alist '("\\.java\\'" . java-mode))
130c507e 991
785eecbb
RS
992;;;###autoload
993(defun java-mode ()
994 "Major mode for editing Java code.
dc2b4c5f 995To submit a problem report, enter `\\[c-submit-bug-report]' from a
785eecbb
RS
996java-mode buffer. This automatically sets up a mail buffer with
997version information already added. You just need to add a description
50fdde0e 998of the problem, including a reproducible test case, and send the
785eecbb
RS
999message.
1000
1001To see what version of CC Mode you are running, enter `\\[c-version]'.
1002
d9e94c22
MS
1003The hook `c-mode-common-hook' is run with no args at mode
1004initialization, then `java-mode-hook'.
785eecbb
RS
1005
1006Key bindings:
1007\\{java-mode-map}"
1008 (interactive)
785eecbb 1009 (kill-all-local-variables)
d9e94c22 1010 (c-initialize-cc-mode t)
785eecbb
RS
1011 (set-syntax-table java-mode-syntax-table)
1012 (setq major-mode 'java-mode
1013 mode-name "Java"
91b807c9 1014 local-abbrev-table java-mode-abbrev-table
a66cd3ee 1015 abbrev-mode t)
785eecbb 1016 (use-local-map java-mode-map)
28d88c16 1017 (c-init-language-vars-for 'java-mode)
a66cd3ee 1018 (c-common-init 'java-mode)
d9e94c22 1019 (easy-menu-add c-java-menu)
51f606de 1020 (cc-imenu-init cc-imenu-java-generic-expression)
3efc2cd7 1021 (c-run-mode-hooks 'c-mode-common-hook 'java-mode-hook)
785eecbb
RS
1022 (c-update-modeline))
1023
1024\f
130c507e
GM
1025;; Support for CORBA's IDL language
1026
d9e94c22
MS
1027;;;###autoload
1028(defvar idl-mode-syntax-table nil
1029 "Syntax table used in idl-mode buffers.")
1030(or idl-mode-syntax-table
1031 (setq idl-mode-syntax-table
1032 (funcall (c-lang-const c-make-mode-syntax-table idl))))
1033
130c507e
GM
1034(defvar idl-mode-abbrev-table nil
1035 "Abbreviation table used in idl-mode buffers.")
a66cd3ee 1036(c-define-abbrev-table 'idl-mode-abbrev-table nil)
130c507e
GM
1037
1038(defvar idl-mode-map ()
1039 "Keymap used in idl-mode buffers.")
1040(if idl-mode-map
1041 nil
1042 (setq idl-mode-map (c-make-inherited-keymap))
1043 ;; add bindings which are only useful for IDL
1044 )
1045
1046(easy-menu-define c-idl-menu idl-mode-map "IDL Mode Commands"
d9e94c22
MS
1047 (cons "IDL" (c-lang-const c-mode-menu idl)))
1048
1049;;;###autoload (add-to-list 'auto-mode-alist '("\\.idl\\'" . idl-mode))
130c507e 1050
aac90c52
RS
1051;;;###autoload
1052(defun idl-mode ()
d9e94c22 1053 "Major mode for editing CORBA's IDL, PSDL and CIDL code.
aac90c52
RS
1054To submit a problem report, enter `\\[c-submit-bug-report]' from an
1055idl-mode buffer. This automatically sets up a mail buffer with
1056version information already added. You just need to add a description
1057of the problem, including a reproducible test case, and send the
1058message.
1059
1060To see what version of CC Mode you are running, enter `\\[c-version]'.
1061
d9e94c22
MS
1062The hook `c-mode-common-hook' is run with no args at mode
1063initialization, then `idl-mode-hook'.
aac90c52
RS
1064
1065Key bindings:
1066\\{idl-mode-map}"
1067 (interactive)
aac90c52 1068 (kill-all-local-variables)
d9e94c22 1069 (c-initialize-cc-mode t)
aac90c52
RS
1070 (set-syntax-table idl-mode-syntax-table)
1071 (setq major-mode 'idl-mode
1072 mode-name "IDL"
1073 local-abbrev-table idl-mode-abbrev-table)
1074 (use-local-map idl-mode-map)
28d88c16 1075 (c-init-language-vars-for 'idl-mode)
a66cd3ee 1076 (c-common-init 'idl-mode)
d9e94c22
MS
1077 (easy-menu-add c-idl-menu)
1078 ;;(cc-imenu-init cc-imenu-idl-generic-expression) ;TODO
3efc2cd7 1079 (c-run-mode-hooks 'c-mode-common-hook 'idl-mode-hook)
aac90c52
RS
1080 (c-update-modeline))
1081
1082\f
130c507e
GM
1083;; Support for Pike
1084
d9e94c22
MS
1085;;;###autoload
1086(defvar pike-mode-syntax-table nil
1087 "Syntax table used in pike-mode buffers.")
1088(or pike-mode-syntax-table
1089 (setq pike-mode-syntax-table
1090 (funcall (c-lang-const c-make-mode-syntax-table pike))))
1091
130c507e
GM
1092(defvar pike-mode-abbrev-table nil
1093 "Abbreviation table used in pike-mode buffers.")
a66cd3ee
MS
1094(c-define-abbrev-table 'pike-mode-abbrev-table
1095 '(("else" "else" c-electric-continued-statement 0)
1096 ("while" "while" c-electric-continued-statement 0)))
130c507e
GM
1097
1098(defvar pike-mode-map ()
1099 "Keymap used in pike-mode buffers.")
1100(if pike-mode-map
1101 nil
1102 (setq pike-mode-map (c-make-inherited-keymap))
1103 ;; additional bindings
1104 (define-key pike-mode-map "\C-c\C-e" 'c-macro-expand))
1105
1106(easy-menu-define c-pike-menu pike-mode-map "Pike Mode Commands"
d9e94c22
MS
1107 (cons "Pike" (c-lang-const c-mode-menu pike)))
1108
50fdde0e
MS
1109;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(u?lpc\\|pike\\|pmod\\(.in\\)?\\)\\'" . pike-mode))
1110;;;###autoload (add-to-list 'interpreter-mode-alist '("pike" . pike-mode))
130c507e 1111
0ec8351b
BW
1112;;;###autoload
1113(defun pike-mode ()
1114 "Major mode for editing Pike code.
a66cd3ee
MS
1115To submit a problem report, enter `\\[c-submit-bug-report]' from a
1116pike-mode buffer. This automatically sets up a mail buffer with
0ec8351b
BW
1117version information already added. You just need to add a description
1118of the problem, including a reproducible test case, and send the
1119message.
1120
1121To see what version of CC Mode you are running, enter `\\[c-version]'.
1122
d9e94c22
MS
1123The hook `c-mode-common-hook' is run with no args at mode
1124initialization, then `pike-mode-hook'.
0ec8351b
BW
1125
1126Key bindings:
1127\\{pike-mode-map}"
1128 (interactive)
0ec8351b 1129 (kill-all-local-variables)
d9e94c22 1130 (c-initialize-cc-mode t)
0ec8351b
BW
1131 (set-syntax-table pike-mode-syntax-table)
1132 (setq major-mode 'pike-mode
1133 mode-name "Pike"
130c507e 1134 local-abbrev-table pike-mode-abbrev-table
a66cd3ee 1135 abbrev-mode t)
0ec8351b 1136 (use-local-map pike-mode-map)
28d88c16 1137 (c-init-language-vars-for 'pike-mode)
a66cd3ee 1138 (c-common-init 'pike-mode)
d9e94c22
MS
1139 (easy-menu-add c-pike-menu)
1140 ;;(cc-imenu-init cc-imenu-pike-generic-expression) ;TODO
3efc2cd7 1141 (c-run-mode-hooks 'c-mode-common-hook 'pike-mode-hook)
0ec8351b
BW
1142 (c-update-modeline))
1143
1144\f
0386b551 1145;; Support for AWK
d9e94c22 1146
50fdde0e
MS
1147;;;###autoload (add-to-list 'auto-mode-alist '("\\.awk\\'" . awk-mode))
1148;;;###autoload (add-to-list 'interpreter-mode-alist '("awk" . awk-mode))
1149;;;###autoload (add-to-list 'interpreter-mode-alist '("mawk" . awk-mode))
1150;;;###autoload (add-to-list 'interpreter-mode-alist '("nawk" . awk-mode))
1151;;;###autoload (add-to-list 'interpreter-mode-alist '("gawk" . awk-mode))
d9e94c22 1152
50fdde0e
MS
1153;;; Autoload directives must be on the top level, so we construct an
1154;;; autoload form instead.
791887d9 1155;;;###autoload (autoload 'awk-mode "cc-mode" "Major mode for editing AWK code." t)
50fdde0e 1156
0386b551
AM
1157(defvar awk-mode-abbrev-table nil
1158 "Abbreviation table used in awk-mode buffers.")
1159(c-define-abbrev-table 'awk-mode-abbrev-table
1160 '(("else" "else" c-electric-continued-statement 0)
1161 ("while" "while" c-electric-continued-statement 0)))
1162
1163(defvar awk-mode-map ()
1164 "Keymap used in awk-mode buffers.")
1165(if awk-mode-map
1166 nil
1167 (setq awk-mode-map (c-make-inherited-keymap))
1168 ;; add bindings which are only useful for awk.
1169 (define-key awk-mode-map "#" 'self-insert-command)
1170 (define-key awk-mode-map "/" 'self-insert-command)
1171 (define-key awk-mode-map "*" 'self-insert-command)
1172 (define-key awk-mode-map "\C-c\C-n" 'undefined) ; #if doesn't exist in awk.
1173 (define-key awk-mode-map "\C-c\C-p" 'undefined)
1174 (define-key awk-mode-map "\C-c\C-u" 'undefined)
1175 (define-key awk-mode-map "\M-a" 'c-beginning-of-statement) ; 2003/10/7
1176 (define-key awk-mode-map "\M-e" 'c-end-of-statement) ; 2003/10/7
1177 (define-key awk-mode-map "\C-\M-a" 'c-awk-beginning-of-defun)
1178 (define-key awk-mode-map "\C-\M-e" 'c-awk-end-of-defun))
1179
1180(easy-menu-define c-awk-menu awk-mode-map "AWK Mode Commands"
1181 (cons "AWK" (c-lang-const c-mode-menu awk)))
1182
1183(defun awk-mode ()
1184 "Major mode for editing AWK code.
d9e94c22
MS
1185To submit a problem report, enter `\\[c-submit-bug-report]' from an
1186awk-mode buffer. This automatically sets up a mail buffer with version
1187information already added. You just need to add a description of the
50fdde0e 1188problem, including a reproducible test case, and send the message.
d9e94c22
MS
1189
1190To see what version of CC Mode you are running, enter `\\[c-version]'.
1191
1192The hook `c-mode-common-hook' is run with no args at mode
1193initialization, then `awk-mode-hook'.
1194
1195Key bindings:
1196\\{awk-mode-map}"
0386b551
AM
1197 (interactive)
1198 (require 'cc-awk) ; Added 2003/6/10.
1199 (kill-all-local-variables)
1200 (c-initialize-cc-mode t)
1201 (set-syntax-table awk-mode-syntax-table)
1202 (setq major-mode 'awk-mode
1203 mode-name "AWK"
1204 local-abbrev-table awk-mode-abbrev-table
1205 abbrev-mode t)
1206 (use-local-map awk-mode-map)
1207 (c-init-language-vars-for 'awk-mode)
1208 (c-common-init 'awk-mode)
1209 ;; The rest of CC Mode does not (yet) use `font-lock-syntactic-keywords',
1210 ;; so it's not set by `c-font-lock-init'.
1211 (make-local-variable 'font-lock-syntactic-keywords)
1212 (setq font-lock-syntactic-keywords
1213 '((c-awk-set-syntax-table-properties
1214 0 (0) ; Everything on this line is a dummy.
1215 nil t)))
1216 (c-awk-unstick-NL-prop)
1217 (add-hook 'before-change-functions 'c-awk-before-change nil t)
1218 (add-hook 'after-change-functions 'c-awk-after-change nil t)
1219 (c-save-buffer-state nil
1220 (save-restriction
1221 (widen)
1222 (c-awk-clear-NL-props (point-min) (point-max))
1223 (c-awk-after-change (point-min) (point-max) 0))) ; Set syntax-table props.
1224
1225 ;; Prevent Xemacs's buffer-syntactic-context being used. See the comment
1226 ;; in cc-engine.el, just before (defun c-fast-in-literal ...
1227 (defalias 'c-in-literal 'c-slow-in-literal)
1228
1229 (c-run-mode-hooks 'c-mode-common-hook 'awk-mode-hook)
1230 (c-update-modeline))
d9e94c22
MS
1231
1232\f
c2efc1d0 1233;; bug reporting
785eecbb
RS
1234
1235(defconst c-mode-help-address
a66cd3ee
MS
1236 "bug-cc-mode@gnu.org"
1237 "Address(es) for CC Mode bug reports.")
785eecbb
RS
1238
1239(defun c-version ()
1240 "Echo the current version of CC Mode in the minibuffer."
1241 (interactive)
1242 (message "Using CC Mode version %s" c-version)
1243 (c-keep-region-active))
1244
130c507e
GM
1245(defvar c-prepare-bug-report-hooks nil)
1246
1247;; Dynamic variables used by reporter.
1248(defvar reporter-prompt-for-summary-p)
1249(defvar reporter-dont-compact-list)
1250
785eecbb
RS
1251(defun c-submit-bug-report ()
1252 "Submit via mail a bug report on CC Mode."
1253 (interactive)
77e31a8d 1254 (require 'reporter)
785eecbb
RS
1255 ;; load in reporter
1256 (let ((reporter-prompt-for-summary-p t)
1257 (reporter-dont-compact-list '(c-offsets-alist))
1258 (style c-indentation-style)
785eecbb
RS
1259 (c-features c-emacs-features))
1260 (and
1261 (if (y-or-n-p "Do you want to submit a report on CC Mode? ")
1262 t (message "") nil)
785eecbb
RS
1263 (reporter-submit-bug-report
1264 c-mode-help-address
d9e94c22 1265 (concat "CC Mode " c-version " (" mode-name ")")
51f606de 1266 (let ((vars (append
51f606de 1267 c-style-variables
d9e94c22 1268 '(c-buffer-is-cc-mode
51f606de 1269 c-tab-always-indent
d9e94c22
MS
1270 c-syntactic-indentation
1271 c-syntactic-indentation-in-macros
1272 c-ignore-auto-fill
1273 c-auto-align-backslashes
1274 c-backspace-function
1275 c-delete-function
1276 c-electric-pound-behavior
1277 c-default-style
1278 c-enable-xemacs-performance-kludge-p
1279 c-old-style-variable-behavior
51f606de
GM
1280 defun-prompt-regexp
1281 tab-width
1282 comment-column
1283 parse-sexp-ignore-comments
d9e94c22
MS
1284 parse-sexp-lookup-properties
1285 lookup-syntax-properties
51f606de
GM
1286 ;; A brain-damaged XEmacs only variable that, if
1287 ;; set to nil can cause all kinds of chaos.
1288 signal-error-on-buffer-boundary
1289 ;; Variables that affect line breaking and comments.
d9e94c22 1290 auto-fill-mode
91b807c9 1291 auto-fill-function
51f606de
GM
1292 filladapt-mode
1293 comment-multi-line
1294 comment-start-skip
1295 fill-prefix
d9e94c22 1296 fill-column
51f606de
GM
1297 paragraph-start
1298 adaptive-fill-mode
1299 adaptive-fill-regexp)
1300 nil)))
d9e94c22
MS
1301 (mapcar (lambda (var) (unless (boundp var)
1302 (setq vars (delq var vars))))
91b807c9
GM
1303 '(signal-error-on-buffer-boundary
1304 filladapt-mode
d9e94c22
MS
1305 defun-prompt-regexp
1306 font-lock-mode
1307 font-lock-maximum-decoration
1308 parse-sexp-lookup-properties
1309 lookup-syntax-properties))
51f606de 1310 vars)
9714ec23 1311 (lambda ()
a66cd3ee 1312 (run-hooks 'c-prepare-bug-report-hooks)
d9e94c22 1313 (insert (format "Buffer Style: %s\nc-emacs-features: %s\n"
a66cd3ee 1314 style c-features)))))))
785eecbb
RS
1315
1316\f
130c507e 1317(cc-provide 'cc-mode)
ab5796a9 1318
0386b551 1319;;; arch-tag: 7825e5c4-fd09-439f-a04d-4c13208ba3d7
785eecbb 1320;;; cc-mode.el ends here