bname needs to be a buffer name, not a filename.
[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 3;; Copyright (C) 1985, 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
d7a0267c
GM
4;; 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
5;; Free Software 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
1a484753 20;; the Free Software Foundation; either version 3, or (at your option)
785eecbb
RS
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
28d88c16
MS
157(defun c-init-language-vars-for (mode)
158 "Initialize the language variables for one of the language modes
159directly supported by CC Mode. This can be used instead of the
160`c-init-language-vars' macro if the language you want to use is one of
161those, rather than a derived language defined through the language
162variable system (see \"cc-langs.el\")."
28d88c16
MS
163 (cond ((eq mode 'c-mode) (c-init-language-vars c-mode))
164 ((eq mode 'c++-mode) (c-init-language-vars c++-mode))
165 ((eq mode 'objc-mode) (c-init-language-vars objc-mode))
166 ((eq mode 'java-mode) (c-init-language-vars java-mode))
167 ((eq mode 'idl-mode) (c-init-language-vars idl-mode))
168 ((eq mode 'pike-mode) (c-init-language-vars pike-mode))
169 ((eq mode 'awk-mode) (c-init-language-vars awk-mode))
170 (t (error "Unsupported mode %s" mode))))
171
aac90c52 172;;;###autoload
d9e94c22
MS
173(defun c-initialize-cc-mode (&optional new-style-init)
174 "Initialize CC Mode for use in the current buffer.
175If the optional NEW-STYLE-INIT is nil or left out then all necessary
176initialization to run CC Mode for the C language is done. Otherwise
28d88c16
MS
177only some basic setup is done, and a call to `c-init-language-vars' or
178`c-init-language-vars-for' is necessary too (which gives more
179control). See \"cc-mode.el\" for more info."
d9e94c22 180
611c76a7 181 (setq c-buffer-is-cc-mode t)
d9e94c22 182
0ec8351b
BW
183 (let ((initprop 'cc-mode-is-initialized)
184 c-initialization-ok)
185 (unless (get 'c-initialize-cc-mode initprop)
0ec8351b
BW
186 (unwind-protect
187 (progn
51f606de
GM
188 (put 'c-initialize-cc-mode initprop t)
189 (c-initialize-builtin-style)
0ec8351b 190 (run-hooks 'c-initialization-hook)
51f606de
GM
191 ;; Fix obsolete variables.
192 (if (boundp 'c-comment-continuation-stars)
193 (setq c-block-comment-prefix c-comment-continuation-stars))
a66cd3ee 194 (add-hook 'change-major-mode-hook 'c-leave-cc-mode-mode)
0ec8351b
BW
195 (setq c-initialization-ok t))
196 ;; Will try initialization hooks again if they failed.
d9e94c22
MS
197 (put 'c-initialize-cc-mode initprop c-initialization-ok))))
198
199 (unless new-style-init
28d88c16 200 (c-init-language-vars-for 'c-mode)))
1dad6919
RS
201
202\f
d9e94c22
MS
203;;; Common routines.
204
130c507e
GM
205(defvar c-mode-base-map ()
206 "Keymap shared by all CC Mode related modes.")
207
208(defun c-make-inherited-keymap ()
209 (let ((map (make-sparse-keymap)))
3efc2cd7
MS
210 ;; Necessary to use `cc-bytecomp-fboundp' below since this
211 ;; function is called from top-level forms that are evaluated
212 ;; while cc-bytecomp is active when one does M-x eval-buffer.
130c507e 213 (cond
d9e94c22 214 ;; XEmacs
3efc2cd7 215 ((cc-bytecomp-fboundp 'set-keymap-parents)
130c507e 216 (set-keymap-parents map c-mode-base-map))
d9e94c22 217 ;; Emacs
3efc2cd7 218 ((cc-bytecomp-fboundp 'set-keymap-parent)
130c507e
GM
219 (set-keymap-parent map c-mode-base-map))
220 ;; incompatible
221 (t (error "CC Mode is incompatible with this version of Emacs")))
222 map))
223
a66cd3ee
MS
224(defun c-define-abbrev-table (name defs)
225 ;; Compatibility wrapper for `define-abbrev' which passes a non-nil
226 ;; sixth argument for SYSTEM-FLAG in emacsen that support it
0386b551 227 ;; (currently only Emacs >= 21.2).
d9e94c22
MS
228 (let ((table (or (symbol-value name)
229 (progn (define-abbrev-table name nil)
230 (symbol-value name)))))
a66cd3ee
MS
231 (while defs
232 (condition-case nil
233 (apply 'define-abbrev table (append (car defs) '(t)))
234 (wrong-number-of-arguments
235 (apply 'define-abbrev table (car defs))))
236 (setq defs (cdr defs)))))
d9e94c22 237(put 'c-define-abbrev-table 'lisp-indent-function 1)
a66cd3ee 238
0386b551
AM
239(defun c-bind-special-erase-keys ()
240 ;; Only used in Emacs to bind C-c C-<delete> and C-c C-<backspace>
241 ;; to the proper keys depending on `normal-erase-is-backspace'.
242 (if normal-erase-is-backspace
243 (progn
244 (define-key c-mode-base-map (kbd "C-c C-<delete>")
245 'c-hungry-delete-forward)
246 (define-key c-mode-base-map (kbd "C-c C-<backspace>")
cb694ab7 247 'c-hungry-delete-backwards))
0386b551 248 (define-key c-mode-base-map (kbd "C-c C-<delete>")
cb694ab7 249 'c-hungry-delete-backwards)
0386b551
AM
250 (define-key c-mode-base-map (kbd "C-c C-<backspace>")
251 'c-hungry-delete-forward)))
252
130c507e
GM
253(if c-mode-base-map
254 nil
0386b551 255
130c507e 256 (setq c-mode-base-map (make-sparse-keymap))
0386b551 257
130c507e
GM
258 ;; Separate M-BS from C-M-h. The former should remain
259 ;; backward-kill-word.
260 (define-key c-mode-base-map [(control meta h)] 'c-mark-function)
261 (define-key c-mode-base-map "\e\C-q" 'c-indent-exp)
262 (substitute-key-definition 'backward-sentence
263 'c-beginning-of-statement
264 c-mode-base-map global-map)
265 (substitute-key-definition 'forward-sentence
266 'c-end-of-statement
267 c-mode-base-map global-map)
268 (substitute-key-definition 'indent-new-comment-line
269 'c-indent-new-comment-line
270 c-mode-base-map global-map)
0386b551 271 (substitute-key-definition 'indent-for-tab-command
17ee4625
DN
272 ;; XXX Is this the right thing to do
273 ;; here?
274 'c-indent-line-or-region
0386b551 275 c-mode-base-map global-map)
a66cd3ee
MS
276 (when (fboundp 'comment-indent-new-line)
277 ;; indent-new-comment-line has changed name to
278 ;; comment-indent-new-line in Emacs 21.
279 (substitute-key-definition 'comment-indent-new-line
280 'c-indent-new-comment-line
281 c-mode-base-map global-map))
0386b551 282
130c507e 283 ;; RMS says don't make these the default.
51c9af45
AM
284 ;; (April 2006): RMS has now approved these commands as defaults.
285 (define-key c-mode-base-map "\e\C-a" 'c-beginning-of-defun)
286 (define-key c-mode-base-map "\e\C-e" 'c-end-of-defun)
0386b551 287
130c507e
GM
288 (define-key c-mode-base-map "\C-c\C-n" 'c-forward-conditional)
289 (define-key c-mode-base-map "\C-c\C-p" 'c-backward-conditional)
290 (define-key c-mode-base-map "\C-c\C-u" 'c-up-conditional)
d9e94c22
MS
291
292 ;; It doesn't suffice to put `c-fill-paragraph' on
293 ;; `fill-paragraph-function' since `c-fill-paragraph' must be called
294 ;; before any fill prefix adaption is done. E.g. `filladapt-mode'
295 ;; replaces `fill-paragraph' and does the adaption before calling
296 ;; `fill-paragraph-function', and we have to mask comments etc
297 ;; before that. Also, `c-fill-paragraph' chains on to
298 ;; `fill-paragraph' and the value on `fill-parapgraph-function' to
299 ;; do the actual filling work.
130c507e
GM
300 (substitute-key-definition 'fill-paragraph 'c-fill-paragraph
301 c-mode-base-map global-map)
302 ;; In XEmacs the default fill function is called
303 ;; fill-paragraph-or-region.
304 (substitute-key-definition 'fill-paragraph-or-region 'c-fill-paragraph
305 c-mode-base-map global-map)
d9e94c22 306
0386b551
AM
307 ;; We bind the forward deletion key and (implicitly) C-d to
308 ;; `c-electric-delete-forward', and the backward deletion key to
309 ;; `c-electric-backspace'. The hungry variants are bound to the
310 ;; same keys but prefixed with C-c. This implies that C-c C-d is
311 ;; `c-hungry-delete-forward'. For consistency, we bind not only C-c
cb694ab7
AM
312 ;; <backspace> to `c-hungry-delete-backwards' but also
313 ;; C-c C-<backspace>, so that the Ctrl key can be held down during
314 ;; the whole sequence regardless of the direction. This in turn
315 ;; implies that we bind C-c C-<delete> to `c-hungry-delete-forward',
316 ;; for the same reason.
0386b551 317
6d28be1d
EZ
318 ;; Bind the electric deletion functions to C-d and DEL. Emacs 21
319 ;; automatically maps the [delete] and [backspace] keys to these two
320 ;; depending on window system and user preferences. (In earlier
321 ;; versions it's possible to do the same by using `function-key-map'.)
322 (define-key c-mode-base-map "\C-d" 'c-electric-delete-forward)
323 (define-key c-mode-base-map "\177" 'c-electric-backspace)
0386b551 324 (define-key c-mode-base-map "\C-c\C-d" 'c-hungry-delete-forward)
cb694ab7
AM
325 (define-key c-mode-base-map [?\C-c ?\d] 'c-hungry-delete-backwards)
326 (define-key c-mode-base-map [?\C-c ?\C-\d] 'c-hungry-delete-backwards)
0386b551
AM
327 (define-key c-mode-base-map [?\C-c deletechar] 'c-hungry-delete-forward) ; C-c <delete> on a tty.
328 (define-key c-mode-base-map [?\C-c (control deletechar)] ; C-c C-<delete> on a tty.
329 'c-hungry-delete-forward)
330 (when (boundp 'normal-erase-is-backspace)
331 ;; The automatic C-d and DEL mapping functionality doesn't extend
332 ;; to special combinations like C-c C-<delete>, so we have to hook
333 ;; into the `normal-erase-is-backspace' system to bind it directly
334 ;; as appropriate.
335 (add-hook 'normal-erase-is-backspace-hook 'c-bind-special-erase-keys)
336 (c-bind-special-erase-keys))
337
338 (when (fboundp 'delete-forward-p)
339 ;; In XEmacs we fix the forward and backward deletion behavior by
340 ;; binding the keysyms for the [delete] and [backspace] keys
341 ;; directly, and use `delete-forward-p' to decide what [delete]
342 ;; should do. That's done in the XEmacs specific
343 ;; `c-electric-delete' and `c-hungry-delete' functions.
6d28be1d 344 (define-key c-mode-base-map [delete] 'c-electric-delete)
0386b551
AM
345 (define-key c-mode-base-map [backspace] 'c-electric-backspace)
346 (define-key c-mode-base-map (kbd "C-c <delete>") 'c-hungry-delete)
347 (define-key c-mode-base-map (kbd "C-c C-<delete>") 'c-hungry-delete)
cb694ab7
AM
348 (define-key c-mode-base-map (kbd "C-c <backspace>")
349 'c-hungry-delete-backwards)
350 (define-key c-mode-base-map (kbd "C-c C-<backspace>")
351 'c-hungry-delete-backwards))
0386b551
AM
352
353 (define-key c-mode-base-map "#" 'c-electric-pound)
354 (define-key c-mode-base-map "{" 'c-electric-brace)
355 (define-key c-mode-base-map "}" 'c-electric-brace)
130c507e 356 (define-key c-mode-base-map "/" 'c-electric-slash)
0386b551
AM
357 (define-key c-mode-base-map "*" 'c-electric-star)
358 (define-key c-mode-base-map ";" 'c-electric-semi&comma)
359 (define-key c-mode-base-map "," 'c-electric-semi&comma)
360 (define-key c-mode-base-map ":" 'c-electric-colon)
361 (define-key c-mode-base-map "(" 'c-electric-paren)
362 (define-key c-mode-base-map ")" 'c-electric-paren)
363
130c507e 364 (define-key c-mode-base-map "\C-c\C-\\" 'c-backslash-region)
0386b551 365 (define-key c-mode-base-map "\C-c\C-a" 'c-toggle-auto-newline)
130c507e
GM
366 (define-key c-mode-base-map "\C-c\C-b" 'c-submit-bug-report)
367 (define-key c-mode-base-map "\C-c\C-c" 'comment-region)
0386b551 368 (define-key c-mode-base-map "\C-c\C-l" 'c-toggle-electric-state)
130c507e 369 (define-key c-mode-base-map "\C-c\C-o" 'c-set-offset)
0386b551 370 (define-key c-mode-base-map "\C-c\C-q" 'c-indent-defun)
130c507e 371 (define-key c-mode-base-map "\C-c\C-s" 'c-show-syntactic-information)
0386b551 372 ;; (define-key c-mode-base-map "\C-c\C-t" 'c-toggle-auto-hungry-state) Commented out by ACM, 2005-03-05.
130c507e
GM
373 (define-key c-mode-base-map "\C-c." 'c-set-style)
374 ;; conflicts with OOBR
375 ;;(define-key c-mode-base-map "\C-c\C-v" 'c-version)
0386b551
AM
376 ;; (define-key c-mode-base-map "\C-c\C-y" 'c-toggle-hungry-state) Commented out by ACM, 2005-11-22.
377 (define-key c-mode-base-map "\C-c\C-w" 'c-subword-mode)
130c507e
GM
378 )
379
130c507e
GM
380;; We don't require the outline package, but we configure it a bit anyway.
381(cc-bytecomp-defvar outline-level)
382
d9e94c22
MS
383(defun c-mode-menu (modestr)
384 "Return a menu spec suitable for `easy-menu-define' that is exactly
385like the C mode menu except that the menu bar item name is MODESTR
386instead of \"C\".
387
388This function is provided for compatibility only; derived modes should
389preferably use the `c-mode-menu' language constant directly."
390 (cons modestr (c-lang-const c-mode-menu c)))
391
392;; Ugly hack to pull in the definition of `c-populate-syntax-table'
393;; from cc-langs to make it available at runtime. It's either this or
394;; moving the definition for it to cc-defs, but that would mean to
395;; break up the syntax table setup over two files.
396(defalias 'c-populate-syntax-table
397 (cc-eval-when-compile
398 (let ((f (symbol-function 'c-populate-syntax-table)))
399 (if (byte-code-function-p f) f (byte-compile f)))))
400
0386b551
AM
401;; CAUTION: Try to avoid installing things on
402;; `before-change-functions'. The macro `combine-after-change-calls'
403;; is used and it doesn't work if there are things on that hook. That
404;; can cause font lock functions to run in inconvenient places during
405;; temporary changes in some font lock support modes, causing extra
406;; unnecessary work and font lock glitches due to interactions between
407;; various text properties.
9c184ed2
AM
408;;
409;; (2007-02-12): The macro `combine-after-change-calls' ISN'T used any
410;; more.
411
412(defun c-unfind-enclosing-token (pos)
413 ;; If POS is wholly inside a token, remove that id from
414 ;; `c-found-types', should it be present. Return t if we were in an
415 ;; id, else nil.
416 (save-excursion
417 (let ((tok-beg (progn (goto-char pos)
418 (and (c-beginning-of-current-token) (point))))
419 (tok-end (progn (goto-char pos)
420 (and (c-end-of-current-token) (point)))))
421 (when (and tok-beg tok-end)
422 (c-unfind-type (buffer-substring-no-properties tok-beg tok-end))
423 t))))
424
425(defun c-unfind-coalesced-tokens (beg end)
426 ;; unless the non-empty region (beg end) is entirely WS and there's at
427 ;; least one character of WS just before or after this region, remove
428 ;; the tokens which touch the region from `c-found-types' should they
429 ;; be present.
430 (or (c-partial-ws-p beg end)
431 (save-excursion
432 (progn
433 (goto-char beg)
434 (or (eq beg (point-min))
435 (c-skip-ws-backward (1- beg))
436 (/= (point) beg)
437 (= (c-backward-token-2) 1)
438 (c-unfind-type (buffer-substring-no-properties
439 (point) beg)))
440 (goto-char end)
441 (or (eq end (point-max))
442 (c-skip-ws-forward (1+ end))
443 (/= (point) end)
444 (progn (forward-char) (c-end-of-current-token) nil)
445 (c-unfind-type (buffer-substring-no-properties
446 end (point))))))))
447
448;; c-maybe-stale-found-type records a place near the region being
449;; changed where an element of `found-types' might become stale. It
450;; is set in c-before-change and is either nil, or has the form:
451;;
452;; (c-decl-id-start "foo" 97 107 " (* ooka) " "o"), where
453;;
454;; o - `c-decl-id-start' is the c-type text property value at buffer
455;; pos 96.
456;;
457;; o - 97 107 is the region potentially containing the stale type -
458;; this is delimited by a non-nil c-type text property at 96 and
459;; either another one or a ";", "{", or "}" at 107.
460;;
461;; o - " (* ooka) " is the (before change) buffer portion containing
462;; the suspect type (here "ooka").
463;;
464;; o - "o" is the buffer contents which is about to be deleted. This
465;; would be the empty string for an insertion.
466(defvar c-maybe-stale-found-type nil)
467(make-variable-buffer-local 'c-maybe-stale-found-type)
468
469(defun c-before-change (beg end)
470 ;; Function to be put on `before-change-function'. Currently
471 ;; (2007-02) it is used only to remove stale entries from the
472 ;; `c-found-types' cache, and to record entries which a
473 ;; `c-after-change' function might confirm as stale.
474 ;;
475 ;; Note that this function must be FAST rather than accurate. Note
476 ;; also that it only has any effect when font locking is enabled.
477 ;; We exploit this by checking for font-lock-*-face instead of doing
478 ;; rigourous syntactic analysis.
479
480 ;; If either change boundary is wholly inside an identifier, delete
481 ;; it/them from the cache. Don't worry about being inside a string
482 ;; or a comment - "wrongly" removing a symbol from `c-found-types'
483 ;; isn't critical.
484 (setq c-maybe-stale-found-type nil)
485 (save-restriction
d4e5bf0f
AM
486 (save-match-data
487 (widen)
488 (save-excursion
489 ;; Are we inserting/deleting stuff in the middle of an identifier?
490 (c-unfind-enclosing-token beg)
491 (c-unfind-enclosing-token end)
492 ;; Are we coalescing two tokens together, e.g. "fo o" -> "foo"?
493 (when (< beg end)
494 (c-unfind-coalesced-tokens beg end))
495 ;; Are we (potentially) disrupting the syntactic context which
496 ;; makes a type a type? E.g. by inserting stuff after "foo" in
497 ;; "foo bar;", or before "foo" in "typedef foo *bar;"?
498 ;;
499 ;; We search for appropriate c-type properties "near" the change.
500 ;; First, find an appropriate boundary for this property search.
501 (let (lim
502 type type-pos
503 marked-id term-pos
504 (end1
6bc7293b
AM
505 (or (and (eq (get-text-property end 'face) 'font-lock-comment-face)
506 (previous-single-property-change end 'face))
507 end)))
d4e5bf0f
AM
508 (when (>= end1 beg) ; Don't hassle about changes entirely in comments.
509 ;; Find a limit for the search for a `c-type' property
510 (while
511 (and (/= (skip-chars-backward "^;{}") 0)
512 (> (point) (point-min))
513 (memq (c-get-char-property (1- (point)) 'face)
514 '(font-lock-comment-face font-lock-string-face))))
515 (setq lim (max (point-min) (1- (point))))
516
517 ;; Look for the latest `c-type' property before end1
46ff7580 518 (when (and (> end1 (point-min))
d4e5bf0f
AM
519 (setq type-pos
520 (if (get-text-property (1- end1) 'c-type)
521 end1
522 (previous-single-property-change end1 'c-type nil lim))))
523 (setq type (get-text-property (max (1- type-pos) lim) 'c-type))
524
525 (when (memq type '(c-decl-id-start c-decl-type-start))
526 ;; Get the identifier, if any, that the property is on.
527 (goto-char (1- type-pos))
528 (setq marked-id
529 (when (looking-at "\\(\\sw\\|\\s_\\)")
530 (c-beginning-of-current-token)
531 (buffer-substring-no-properties (point) type-pos)))
532
533 (goto-char end1)
534 (skip-chars-forward "^;{}") ; FIXME!!! loop for comment, maybe
535 (setq lim (point))
536 (setq term-pos
537 (or (next-single-property-change end 'c-type nil lim) lim))
538 (setq c-maybe-stale-found-type
539 (list type marked-id
540 type-pos term-pos
541 (buffer-substring-no-properties type-pos term-pos)
542 (buffer-substring-no-properties beg end)))))))))))
9c184ed2
AM
543
544(defun c-after-change (beg end old-len)
0386b551
AM
545 ;; Function put on `after-change-functions' to adjust various caches
546 ;; etc. Prefer speed to finesse here, since there will be an order
547 ;; of magnitude more calls to this function than any of the
d9e94c22
MS
548 ;; functions that use the caches.
549 ;;
550 ;; Note that care must be taken so that this is called before any
551 ;; font-lock callbacks since we might get calls to functions using
552 ;; these caches from inside them, and we must thus be sure that this
553 ;; has already been executed.
d9e94c22
MS
554
555 (c-save-buffer-state ()
0386b551
AM
556 ;; When `combine-after-change-calls' is used we might get calls
557 ;; with regions outside the current narrowing. This has been
558 ;; observed in Emacs 20.7.
559 (save-restriction
cb694ab7
AM
560 (save-match-data ; c-recognize-<>-arglists changes match-data
561 (widen)
0386b551 562
cb694ab7
AM
563 (when (> end (point-max))
564 ;; Some emacsen might return positions past the end. This has been
565 ;; observed in Emacs 20.7 when rereading a buffer changed on disk
566 ;; (haven't been able to minimize it, but Emacs 21.3 appears to
567 ;; work).
568 (setq end (point-max))
569 (when (> beg end)
570 (setq beg end)))
0386b551 571
9c184ed2 572 (c-trim-found-types beg end old-len) ; maybe we don't need all of these.
cb694ab7
AM
573 (c-invalidate-sws-region-after beg end)
574 (c-invalidate-state-cache beg)
575 (c-invalidate-find-decl-cache beg)
0386b551 576
cb694ab7
AM
577 (when c-recognize-<>-arglists
578 (c-after-change-check-<>-operators beg end))))))
d9e94c22
MS
579
580(defun c-basic-common-init (mode default-style)
581 "Do the necessary initialization for the syntax handling routines
582and the line breaking/filling code. Intended to be used by other
583packages that embed CC Mode.
584
585MODE is the CC Mode flavor to set up, e.g. 'c-mode or 'java-mode.
586DEFAULT-STYLE tells which indentation style to install. It has the
587same format as `c-default-style'.
588
589Note that `c-init-language-vars' must be called before this function.
590This function cannot do that since `c-init-language-vars' is a macro
591that requires a literal mode spec at compile time."
d9e94c22 592
a66cd3ee
MS
593 (setq c-buffer-is-cc-mode mode)
594
130c507e
GM
595 ;; these variables should always be buffer local; they do not affect
596 ;; indentation style.
130c507e
GM
597 (make-local-variable 'parse-sexp-ignore-comments)
598 (make-local-variable 'indent-line-function)
599 (make-local-variable 'indent-region-function)
130c507e
GM
600 (make-local-variable 'normal-auto-fill-function)
601 (make-local-variable 'comment-start)
602 (make-local-variable 'comment-end)
130c507e
GM
603 (make-local-variable 'comment-start-skip)
604 (make-local-variable 'comment-multi-line)
0386b551
AM
605 (make-local-variable 'comment-line-break-function)
606 (make-local-variable 'paragraph-start)
607 (make-local-variable 'paragraph-separate)
608 (make-local-variable 'paragraph-ignore-fill-prefix)
609 (make-local-variable 'adaptive-fill-mode)
610 (make-local-variable 'adaptive-fill-regexp)
a66cd3ee 611
130c507e 612 ;; now set their values
e2c21e66 613 (setq parse-sexp-ignore-comments t
130c507e
GM
614 indent-line-function 'c-indent-line
615 indent-region-function 'c-indent-region
130c507e 616 normal-auto-fill-function 'c-do-auto-fill
0386b551
AM
617 comment-multi-line t
618 comment-line-break-function 'c-indent-new-comment-line)
a66cd3ee 619
fd8771f5
MS
620 ;; Install `c-fill-paragraph' on `fill-paragraph-function' so that a
621 ;; direct call to `fill-paragraph' behaves better. This still
622 ;; doesn't work with filladapt but it's better than nothing.
623 (make-local-variable 'fill-paragraph-function)
624 (setq fill-paragraph-function 'c-fill-paragraph)
625
0386b551
AM
626 (when (or c-recognize-<>-arglists
627 (c-major-mode-is 'awk-mode))
628 ;; We'll use the syntax-table text property to change the syntax
629 ;; of some chars for this language, so do the necessary setup for
630 ;; that.
631 ;;
632 ;; Note to other package developers: It's ok to turn this on in CC
633 ;; Mode buffers when CC Mode doesn't, but it's not ok to turn it
634 ;; off if CC Mode has turned it on.
635
636 ;; Emacs.
637 (when (boundp 'parse-sexp-lookup-properties)
638 (make-local-variable 'parse-sexp-lookup-properties)
639 (setq parse-sexp-lookup-properties t))
640
641 ;; Same as above for XEmacs.
642 (when (boundp 'lookup-syntax-properties)
643 (make-local-variable 'lookup-syntax-properties)
644 (setq lookup-syntax-properties t)))
d9e94c22
MS
645
646 ;; Use this in Emacs 21 to avoid meddling with the rear-nonsticky
647 ;; property on each character.
648 (when (boundp 'text-property-default-nonsticky)
649 (make-local-variable 'text-property-default-nonsticky)
650 (let ((elem (assq 'syntax-table text-property-default-nonsticky)))
651 (if elem
652 (setcdr elem t)
653 (setq text-property-default-nonsticky
654 (cons '(syntax-table . t)
655 text-property-default-nonsticky))))
656 (setq text-property-default-nonsticky
657 (cons '(c-type . t)
658 text-property-default-nonsticky)))
659
660 ;; In Emacs 21 and later it's possible to turn off the ad-hoc
661 ;; heuristic that open parens in column 0 are defun starters. Since
0386b551
AM
662 ;; we have c-state-cache, that heuristic isn't useful and only causes
663 ;; trouble, so turn it off.
99c8496e
AM
664;; 2006/12/17: This facility is somewhat confused, and doesn't really seem
665;; helpful. Comment it out for now.
666;; (when (memq 'col-0-paren c-emacs-features)
667;; (make-local-variable 'open-paren-in-column-0-is-defun-start)
668;; (setq open-paren-in-column-0-is-defun-start nil))
d9e94c22 669
d9e94c22
MS
670 (c-clear-found-types)
671
672 ;; now set the mode style based on default-style
673 (let ((style (if (stringp default-style)
674 default-style
675 (or (cdr (assq mode default-style))
676 (cdr (assq 'other default-style))
130c507e
GM
677 "gnu"))))
678 ;; Override style variables if `c-old-style-variable-behavior' is
679 ;; set. Also override if we are using global style variables,
680 ;; have already initialized a style once, and are switching to a
681 ;; different style. (It's doubtful whether this is desirable, but
682 ;; the whole situation with nonlocal style variables is a bit
683 ;; awkward. It's at least the most compatible way with the old
684 ;; style init procedure.)
685 (c-set-style style (not (or c-old-style-variable-behavior
686 (and (not c-style-variables-are-local-p)
687 c-indentation-style
688 (not (string-equal c-indentation-style
689 style)))))))
a66cd3ee
MS
690 (c-setup-paragraph-variables)
691
130c507e
GM
692 ;; we have to do something special for c-offsets-alist so that the
693 ;; buffer local value has its own alist structure.
694 (setq c-offsets-alist (copy-alist c-offsets-alist))
a66cd3ee 695
130c507e 696 ;; setup the comment indent variable in a Emacs version portable way
130c507e
GM
697 (make-local-variable 'comment-indent-function)
698 (setq comment-indent-function 'c-comment-indent)
a66cd3ee 699
cb694ab7
AM
700;; ;; Put submode indicators onto minor-mode-alist, but only once.
701;; (or (assq 'c-submode-indicators minor-mode-alist)
702;; (setq minor-mode-alist
703;; (cons '(c-submode-indicators c-submode-indicators)
704;; minor-mode-alist)))
705 (c-update-modeline)
a66cd3ee 706
d9e94c22
MS
707 ;; Install the functions that ensure that various internal caches
708 ;; don't become invalid due to buffer changes.
9c184ed2
AM
709 (make-local-hook 'before-change-functions)
710 (add-hook 'before-change-functions 'c-before-change nil t)
0386b551 711 (make-local-hook 'after-change-functions)
d9e94c22
MS
712 (add-hook 'after-change-functions 'c-after-change nil t))
713
714(defun c-after-font-lock-init ()
715 ;; Put on `font-lock-mode-hook'.
716 (remove-hook 'after-change-functions 'c-after-change t)
717 (add-hook 'after-change-functions 'c-after-change nil t))
718
719(defun c-font-lock-init ()
720 "Set up the font-lock variables for using the font-lock support in CC Mode.
721This does not load the font-lock package. Use after
722`c-basic-common-init' and after cc-fonts has been loaded."
723
724 (make-local-variable 'font-lock-defaults)
725 (setq font-lock-defaults
0386b551 726 `(,(if (c-major-mode-is 'awk-mode)
d9e94c22
MS
727 ;; awk-mode currently has only one font lock level.
728 'awk-font-lock-keywords
729 (mapcar 'c-mode-symbol
730 '("font-lock-keywords" "font-lock-keywords-1"
731 "font-lock-keywords-2" "font-lock-keywords-3")))
732 nil nil
733 ,c-identifier-syntax-modifications
734 c-beginning-of-syntax
5ac71a6a 735 (font-lock-lines-before . 1)
d9e94c22
MS
736 (font-lock-mark-block-function
737 . c-mark-function)))
0386b551
AM
738
739 (make-local-hook 'font-lock-mode-hook)
d9e94c22
MS
740 (add-hook 'font-lock-mode-hook 'c-after-font-lock-init nil t))
741
742(defun c-setup-doc-comment-style ()
743 "Initialize the variables that depend on the value of `c-doc-comment-style'."
744 (when (and (featurep 'font-lock)
745 (symbol-value 'font-lock-mode))
746 ;; Force font lock mode to reinitialize itself.
747 (font-lock-mode 0)
748 (font-lock-mode 1)))
749
750(defun c-common-init (&optional mode)
751 "Common initialization for all CC Mode modes.
752In addition to the work done by `c-basic-common-init' and
753`c-font-lock-init', this function sets up various other things as
754customary in CC Mode modes but which aren't strictly necessary for CC
755Mode to operate correctly.
756
757MODE is the symbol for the mode to initialize, like 'c-mode. See
758`c-basic-common-init' for details. It's only optional to be
0386b551 759compatible with old code; callers should always specify it."
d9e94c22
MS
760
761 (unless mode
762 ;; Called from an old third party package. The fallback is to
763 ;; initialize for C.
28d88c16 764 (c-init-language-vars-for 'c-mode))
d9e94c22
MS
765
766 (c-basic-common-init mode c-default-style)
767 (when mode
768 ;; Only initialize font locking if we aren't called from an old package.
769 (c-font-lock-init))
770
771 (make-local-variable 'outline-regexp)
772 (make-local-variable 'outline-level)
773 (setq outline-regexp "[^#\n\^M]"
774 outline-level 'c-outline-level)
775
776 (let ((rfn (assq mode c-require-final-newline)))
777 (when rfn
778 (make-local-variable 'require-final-newline)
1750e02f
RS
779 (and (cdr rfn)
780 (setq require-final-newline mode-require-final-newline)))))
130c507e 781
cb694ab7
AM
782(defun c-remove-any-local-eval-or-mode-variables ()
783 ;; If the buffer specifies `mode' or `eval' in its File Local Variable list
784 ;; or on the first line, remove all occurrences. See
785 ;; `c-postprocess-file-styles' for justification. There is no need to save
5c4c911a
AM
786 ;; point here, or even bother too much about the buffer contents. However,
787 ;; DON'T mess up the kill-ring.
cb694ab7
AM
788 ;;
789 ;; Most of the code here is derived from Emacs 21.3's `hack-local-variables'
790 ;; in files.el.
791 (goto-char (point-max))
792 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
793 (let (lv-point (prefix "") (suffix ""))
794 (when (let ((case-fold-search t))
795 (search-forward "Local Variables:" nil t))
796 (setq lv-point (point))
797 ;; The prefix is what comes before "local variables:" in its line.
798 ;; The suffix is what comes after "local variables:" in its line.
799 (skip-chars-forward " \t")
800 (or (eolp)
801 (setq suffix (buffer-substring (point)
802 (progn (end-of-line) (point)))))
803 (goto-char (match-beginning 0))
804 (or (bolp)
805 (setq prefix
806 (buffer-substring (point)
807 (progn (beginning-of-line) (point)))))
808
809 (while (search-forward-regexp
810 (concat "^[ \t]*"
811 (regexp-quote prefix)
812 "\\(mode\\|eval\\):.*"
813 (regexp-quote suffix)
814 "$")
815 nil t)
5c4c911a
AM
816 (forward-line 0)
817 (delete-region (point) (progn (forward-line) (point)))))
cb694ab7
AM
818
819 ;; Delete the first line, if we've got one, in case it contains a mode spec.
820 (unless (and lv-point
821 (progn (goto-char lv-point)
822 (forward-line 0)
823 (bobp)))
824 (goto-char (point-min))
5c4c911a
AM
825 (unless (eobp)
826 (delete-region (point) (progn (forward-line) (point)))))))
cb694ab7 827
130c507e 828(defun c-postprocess-file-styles ()
d9e94c22 829 "Function that post processes relevant file local variables in CC Mode.
130c507e
GM
830Currently, this function simply applies any style and offset settings
831found in the file's Local Variable list. It first applies any style
832setting found in `c-file-style', then it applies any offset settings
833it finds in `c-file-offsets'.
834
835Note that the style variables are always made local to the buffer."
d9e94c22 836
130c507e 837 ;; apply file styles and offsets
09f90d2c
RS
838 (when c-buffer-is-cc-mode
839 (if (or c-file-style c-file-offsets)
840 (c-make-styles-buffer-local t))
841 (and c-file-style
842 (c-set-style c-file-style))
843 (and c-file-offsets
d61ca8d5 844 (mapc
09f90d2c
RS
845 (lambda (langentry)
846 (let ((langelem (car langentry))
847 (offset (cdr langentry)))
848 (c-set-offset langelem offset)))
0386b551
AM
849 c-file-offsets))
850 ;; Problem: The file local variable block might have explicitly set a
851 ;; style variable. The `c-set-style' or `mapcar' call might have
852 ;; overwritten this. So we run `hack-local-variables' again to remedy
853 ;; this. There are no guarantees this will work properly, particularly as
854 ;; we have no control over what the other hook functions on
cb694ab7
AM
855 ;; `hack-local-variables-hook' would have done. We now (2006/2/1) remove
856 ;; any `eval' or `mode' expressions before we evaluate again (see below).
857 ;; ACM, 2005/11/2.
858 ;;
859 ;; Problem (bug reported by Gustav Broberg): if one of the variables is
860 ;; `mode', this will invoke c-mode (etc.) again, setting up the style etc.
861 ;; We prevent this by temporarily removing `mode' from the Local Variables
862 ;; section.
0386b551 863 (if (or c-file-style c-file-offsets)
51c9af45
AM
864 (let ((hack-local-variables-hook nil) (inhibit-read-only t))
865 (c-tentative-buffer-changes
cb694ab7
AM
866 (c-remove-any-local-eval-or-mode-variables)
867 (hack-local-variables))
868 nil))))
130c507e
GM
869
870(add-hook 'hack-local-variables-hook 'c-postprocess-file-styles)
871
3efc2cd7
MS
872(defmacro c-run-mode-hooks (&rest hooks)
873 ;; Emacs 21.1 has introduced a system with delayed mode hooks that
874 ;; require the use of the new function `run-mode-hooks'.
875 (if (cc-bytecomp-fboundp 'run-mode-hooks)
876 `(run-mode-hooks ,@hooks)
877 `(progn ,@(mapcar (lambda (hook) `(run-hooks ,hook)) hooks))))
878
130c507e
GM
879\f
880;; Support for C
881
d9e94c22
MS
882;;;###autoload
883(defvar c-mode-syntax-table nil
884 "Syntax table used in c-mode buffers.")
885(or c-mode-syntax-table
886 (setq c-mode-syntax-table
887 (funcall (c-lang-const c-make-mode-syntax-table c))))
888
130c507e
GM
889(defvar c-mode-abbrev-table nil
890 "Abbreviation table used in c-mode buffers.")
a66cd3ee
MS
891(c-define-abbrev-table 'c-mode-abbrev-table
892 '(("else" "else" c-electric-continued-statement 0)
893 ("while" "while" c-electric-continued-statement 0)))
130c507e
GM
894
895(defvar c-mode-map ()
896 "Keymap used in c-mode buffers.")
897(if c-mode-map
898 nil
899 (setq c-mode-map (c-make-inherited-keymap))
900 ;; add bindings which are only useful for C
901 (define-key c-mode-map "\C-c\C-e" 'c-macro-expand)
902 )
903
904(easy-menu-define c-c-menu c-mode-map "C Mode Commands"
d9e94c22
MS
905 (cons "C" (c-lang-const c-mode-menu c)))
906
907;; In XEmacs >= 21.5 modes should add their own entries to
908;; `auto-mode-alist'. The comment form of autoload is used to avoid
909;; doing this on load. That since `add-to-list' prepends the value
910;; which could cause it to clobber user settings. Later emacsen have
911;; an append option, but it's not safe to use.
50fdde0e
MS
912
913;; The the extension ".C" is associated to C++ while the lowercase
914;; variant goes to C. On case insensitive file systems, this means
915;; that ".c" files also might open C++ mode if the C++ entry comes
916;; first on `auto-mode-alist'. Thus we try to ensure that ".C" comes
917;; after ".c", and since `add-to-list' adds the entry first we have to
918;; add the ".C" entry first.
919;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(cc\\|hh\\)\\'" . c++-mode))
920;;;###autoload (add-to-list 'auto-mode-alist '("\\.[ch]\\(pp\\|xx\\|\\+\\+\\)\\'" . c++-mode))
921;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(CC?\\|HH?\\)\\'" . c++-mode))
922
d9e94c22
MS
923;;;###autoload (add-to-list 'auto-mode-alist '("\\.[ch]\\'" . c-mode))
924
925;; NB: The following two associate yacc and lex files to C Mode, which
926;; is not really suitable for those formats. Anyway, afaik there's
927;; currently no better mode for them, and besides this is legacy.
928;;;###autoload (add-to-list 'auto-mode-alist '("\\.y\\(acc\\)?\\'" . c-mode))
929;;;###autoload (add-to-list 'auto-mode-alist '("\\.lex\\'" . c-mode))
930
785eecbb
RS
931;;;###autoload
932(defun c-mode ()
933 "Major mode for editing K&R and ANSI C code.
934To submit a problem report, enter `\\[c-submit-bug-report]' from a
935c-mode buffer. This automatically sets up a mail buffer with version
936information already added. You just need to add a description of the
50fdde0e 937problem, including a reproducible test case, and send the message.
785eecbb
RS
938
939To see what version of CC Mode you are running, enter `\\[c-version]'.
940
d9e94c22
MS
941The hook `c-mode-common-hook' is run with no args at mode
942initialization, then `c-mode-hook'.
785eecbb
RS
943
944Key bindings:
945\\{c-mode-map}"
946 (interactive)
785eecbb 947 (kill-all-local-variables)
d9e94c22 948 (c-initialize-cc-mode t)
785eecbb
RS
949 (set-syntax-table c-mode-syntax-table)
950 (setq major-mode 'c-mode
951 mode-name "C"
130c507e
GM
952 local-abbrev-table c-mode-abbrev-table
953 abbrev-mode t)
785eecbb 954 (use-local-map c-mode-map)
28d88c16 955 (c-init-language-vars-for 'c-mode)
a66cd3ee 956 (c-common-init 'c-mode)
d9e94c22 957 (easy-menu-add c-c-menu)
51f606de 958 (cc-imenu-init cc-imenu-c-generic-expression)
3efc2cd7 959 (c-run-mode-hooks 'c-mode-common-hook 'c-mode-hook)
785eecbb
RS
960 (c-update-modeline))
961
962\f
130c507e
GM
963;; Support for C++
964
d9e94c22
MS
965;;;###autoload
966(defvar c++-mode-syntax-table nil
967 "Syntax table used in c++-mode buffers.")
968(or c++-mode-syntax-table
969 (setq c++-mode-syntax-table
970 (funcall (c-lang-const c-make-mode-syntax-table c++))))
971
130c507e
GM
972(defvar c++-mode-abbrev-table nil
973 "Abbreviation table used in c++-mode buffers.")
a66cd3ee
MS
974(c-define-abbrev-table 'c++-mode-abbrev-table
975 '(("else" "else" c-electric-continued-statement 0)
976 ("while" "while" c-electric-continued-statement 0)
977 ("catch" "catch" c-electric-continued-statement 0)))
130c507e
GM
978
979(defvar c++-mode-map ()
980 "Keymap used in c++-mode buffers.")
981(if c++-mode-map
982 nil
983 (setq c++-mode-map (c-make-inherited-keymap))
984 ;; add bindings which are only useful for C++
985 (define-key c++-mode-map "\C-c\C-e" 'c-macro-expand)
986 (define-key c++-mode-map "\C-c:" 'c-scope-operator)
987 (define-key c++-mode-map "<" 'c-electric-lt-gt)
988 (define-key c++-mode-map ">" 'c-electric-lt-gt))
989
990(easy-menu-define c-c++-menu c++-mode-map "C++ Mode Commands"
d9e94c22
MS
991 (cons "C++" (c-lang-const c-mode-menu c++)))
992
785eecbb
RS
993;;;###autoload
994(defun c++-mode ()
995 "Major mode for editing C++ code.
996To submit a problem report, enter `\\[c-submit-bug-report]' from a
997c++-mode buffer. This automatically sets up a mail buffer with
998version information already added. You just need to add a description
999of the problem, including a reproducible test case, and send the
1000message.
1001
1002To see what version of CC Mode you are running, enter `\\[c-version]'.
1003
d9e94c22
MS
1004The hook `c-mode-common-hook' is run with no args at mode
1005initialization, then `c++-mode-hook'.
785eecbb
RS
1006
1007Key bindings:
1008\\{c++-mode-map}"
1009 (interactive)
785eecbb 1010 (kill-all-local-variables)
d9e94c22 1011 (c-initialize-cc-mode t)
785eecbb
RS
1012 (set-syntax-table c++-mode-syntax-table)
1013 (setq major-mode 'c++-mode
1014 mode-name "C++"
130c507e
GM
1015 local-abbrev-table c++-mode-abbrev-table
1016 abbrev-mode t)
785eecbb 1017 (use-local-map c++-mode-map)
28d88c16 1018 (c-init-language-vars-for 'c++-mode)
a66cd3ee 1019 (c-common-init 'c++-mode)
d9e94c22 1020 (easy-menu-add c-c++-menu)
51f606de 1021 (cc-imenu-init cc-imenu-c++-generic-expression)
3efc2cd7 1022 (c-run-mode-hooks 'c-mode-common-hook 'c++-mode-hook)
785eecbb
RS
1023 (c-update-modeline))
1024
1025\f
130c507e
GM
1026;; Support for Objective-C
1027
d9e94c22
MS
1028;;;###autoload
1029(defvar objc-mode-syntax-table nil
1030 "Syntax table used in objc-mode buffers.")
1031(or objc-mode-syntax-table
1032 (setq objc-mode-syntax-table
1033 (funcall (c-lang-const c-make-mode-syntax-table objc))))
1034
130c507e
GM
1035(defvar objc-mode-abbrev-table nil
1036 "Abbreviation table used in objc-mode buffers.")
a66cd3ee
MS
1037(c-define-abbrev-table 'objc-mode-abbrev-table
1038 '(("else" "else" c-electric-continued-statement 0)
1039 ("while" "while" c-electric-continued-statement 0)))
130c507e
GM
1040
1041(defvar objc-mode-map ()
1042 "Keymap used in objc-mode buffers.")
1043(if objc-mode-map
1044 nil
1045 (setq objc-mode-map (c-make-inherited-keymap))
1046 ;; add bindings which are only useful for Objective-C
1047 (define-key objc-mode-map "\C-c\C-e" 'c-macro-expand))
1048
1049(easy-menu-define c-objc-menu objc-mode-map "ObjC Mode Commands"
d9e94c22
MS
1050 (cons "ObjC" (c-lang-const c-mode-menu objc)))
1051
1052;;;###autoload (add-to-list 'auto-mode-alist '("\\.m\\'" . objc-mode))
130c507e 1053
785eecbb
RS
1054;;;###autoload
1055(defun objc-mode ()
1056 "Major mode for editing Objective C code.
1057To submit a problem report, enter `\\[c-submit-bug-report]' from an
1058objc-mode buffer. This automatically sets up a mail buffer with
1059version information already added. You just need to add a description
1060of the problem, including a reproducible test case, and send the
1061message.
1062
1063To see what version of CC Mode you are running, enter `\\[c-version]'.
1064
d9e94c22
MS
1065The hook `c-mode-common-hook' is run with no args at mode
1066initialization, then `objc-mode-hook'.
785eecbb
RS
1067
1068Key bindings:
1069\\{objc-mode-map}"
1070 (interactive)
785eecbb 1071 (kill-all-local-variables)
d9e94c22 1072 (c-initialize-cc-mode t)
785eecbb
RS
1073 (set-syntax-table objc-mode-syntax-table)
1074 (setq major-mode 'objc-mode
1075 mode-name "ObjC"
130c507e
GM
1076 local-abbrev-table objc-mode-abbrev-table
1077 abbrev-mode t)
785eecbb 1078 (use-local-map objc-mode-map)
28d88c16 1079 (c-init-language-vars-for 'objc-mode)
a66cd3ee 1080 (c-common-init 'objc-mode)
d9e94c22
MS
1081 (easy-menu-add c-objc-menu)
1082 (cc-imenu-init nil 'cc-imenu-objc-function)
3efc2cd7 1083 (c-run-mode-hooks 'c-mode-common-hook 'objc-mode-hook)
785eecbb
RS
1084 (c-update-modeline))
1085
1086\f
130c507e
GM
1087;; Support for Java
1088
d9e94c22
MS
1089;;;###autoload
1090(defvar java-mode-syntax-table nil
1091 "Syntax table used in java-mode buffers.")
1092(or java-mode-syntax-table
1093 (setq java-mode-syntax-table
1094 (funcall (c-lang-const c-make-mode-syntax-table java))))
1095
130c507e
GM
1096(defvar java-mode-abbrev-table nil
1097 "Abbreviation table used in java-mode buffers.")
a66cd3ee
MS
1098(c-define-abbrev-table 'java-mode-abbrev-table
1099 '(("else" "else" c-electric-continued-statement 0)
1100 ("while" "while" c-electric-continued-statement 0)
1101 ("catch" "catch" c-electric-continued-statement 0)
1102 ("finally" "finally" c-electric-continued-statement 0)))
130c507e
GM
1103
1104(defvar java-mode-map ()
1105 "Keymap used in java-mode buffers.")
1106(if java-mode-map
1107 nil
1108 (setq java-mode-map (c-make-inherited-keymap))
1109 ;; add bindings which are only useful for Java
1110 )
1111
d9e94c22
MS
1112;; Regexp trying to describe the beginning of a Java top-level
1113;; definition. This is not used by CC Mode, nor is it maintained
1114;; since it's practically impossible to write a regexp that reliably
1115;; matches such a construct. Other tools are necessary.
1116(defconst c-Java-defun-prompt-regexp
1117 "^[ \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-*")
1118
130c507e 1119(easy-menu-define c-java-menu java-mode-map "Java Mode Commands"
d9e94c22
MS
1120 (cons "Java" (c-lang-const c-mode-menu java)))
1121
1122;;;###autoload (add-to-list 'auto-mode-alist '("\\.java\\'" . java-mode))
130c507e 1123
785eecbb
RS
1124;;;###autoload
1125(defun java-mode ()
1126 "Major mode for editing Java code.
dc2b4c5f 1127To submit a problem report, enter `\\[c-submit-bug-report]' from a
785eecbb
RS
1128java-mode buffer. This automatically sets up a mail buffer with
1129version information already added. You just need to add a description
50fdde0e 1130of the problem, including a reproducible test case, and send the
785eecbb
RS
1131message.
1132
1133To see what version of CC Mode you are running, enter `\\[c-version]'.
1134
d9e94c22
MS
1135The hook `c-mode-common-hook' is run with no args at mode
1136initialization, then `java-mode-hook'.
785eecbb
RS
1137
1138Key bindings:
1139\\{java-mode-map}"
1140 (interactive)
785eecbb 1141 (kill-all-local-variables)
d9e94c22 1142 (c-initialize-cc-mode t)
785eecbb
RS
1143 (set-syntax-table java-mode-syntax-table)
1144 (setq major-mode 'java-mode
1145 mode-name "Java"
91b807c9 1146 local-abbrev-table java-mode-abbrev-table
a66cd3ee 1147 abbrev-mode t)
785eecbb 1148 (use-local-map java-mode-map)
28d88c16 1149 (c-init-language-vars-for 'java-mode)
a66cd3ee 1150 (c-common-init 'java-mode)
d9e94c22 1151 (easy-menu-add c-java-menu)
51f606de 1152 (cc-imenu-init cc-imenu-java-generic-expression)
3efc2cd7 1153 (c-run-mode-hooks 'c-mode-common-hook 'java-mode-hook)
785eecbb
RS
1154 (c-update-modeline))
1155
1156\f
130c507e
GM
1157;; Support for CORBA's IDL language
1158
d9e94c22
MS
1159;;;###autoload
1160(defvar idl-mode-syntax-table nil
1161 "Syntax table used in idl-mode buffers.")
1162(or idl-mode-syntax-table
1163 (setq idl-mode-syntax-table
1164 (funcall (c-lang-const c-make-mode-syntax-table idl))))
1165
130c507e
GM
1166(defvar idl-mode-abbrev-table nil
1167 "Abbreviation table used in idl-mode buffers.")
a66cd3ee 1168(c-define-abbrev-table 'idl-mode-abbrev-table nil)
130c507e
GM
1169
1170(defvar idl-mode-map ()
1171 "Keymap used in idl-mode buffers.")
1172(if idl-mode-map
1173 nil
1174 (setq idl-mode-map (c-make-inherited-keymap))
1175 ;; add bindings which are only useful for IDL
1176 )
1177
1178(easy-menu-define c-idl-menu idl-mode-map "IDL Mode Commands"
d9e94c22
MS
1179 (cons "IDL" (c-lang-const c-mode-menu idl)))
1180
1181;;;###autoload (add-to-list 'auto-mode-alist '("\\.idl\\'" . idl-mode))
130c507e 1182
aac90c52
RS
1183;;;###autoload
1184(defun idl-mode ()
d9e94c22 1185 "Major mode for editing CORBA's IDL, PSDL and CIDL code.
aac90c52
RS
1186To submit a problem report, enter `\\[c-submit-bug-report]' from an
1187idl-mode buffer. This automatically sets up a mail buffer with
1188version information already added. You just need to add a description
1189of the problem, including a reproducible test case, and send the
1190message.
1191
1192To see what version of CC Mode you are running, enter `\\[c-version]'.
1193
d9e94c22
MS
1194The hook `c-mode-common-hook' is run with no args at mode
1195initialization, then `idl-mode-hook'.
aac90c52
RS
1196
1197Key bindings:
1198\\{idl-mode-map}"
1199 (interactive)
aac90c52 1200 (kill-all-local-variables)
d9e94c22 1201 (c-initialize-cc-mode t)
aac90c52
RS
1202 (set-syntax-table idl-mode-syntax-table)
1203 (setq major-mode 'idl-mode
1204 mode-name "IDL"
1205 local-abbrev-table idl-mode-abbrev-table)
1206 (use-local-map idl-mode-map)
28d88c16 1207 (c-init-language-vars-for 'idl-mode)
a66cd3ee 1208 (c-common-init 'idl-mode)
d9e94c22
MS
1209 (easy-menu-add c-idl-menu)
1210 ;;(cc-imenu-init cc-imenu-idl-generic-expression) ;TODO
3efc2cd7 1211 (c-run-mode-hooks 'c-mode-common-hook 'idl-mode-hook)
aac90c52
RS
1212 (c-update-modeline))
1213
1214\f
130c507e
GM
1215;; Support for Pike
1216
d9e94c22
MS
1217;;;###autoload
1218(defvar pike-mode-syntax-table nil
1219 "Syntax table used in pike-mode buffers.")
1220(or pike-mode-syntax-table
1221 (setq pike-mode-syntax-table
1222 (funcall (c-lang-const c-make-mode-syntax-table pike))))
1223
130c507e
GM
1224(defvar pike-mode-abbrev-table nil
1225 "Abbreviation table used in pike-mode buffers.")
a66cd3ee
MS
1226(c-define-abbrev-table 'pike-mode-abbrev-table
1227 '(("else" "else" c-electric-continued-statement 0)
1228 ("while" "while" c-electric-continued-statement 0)))
130c507e
GM
1229
1230(defvar pike-mode-map ()
1231 "Keymap used in pike-mode buffers.")
1232(if pike-mode-map
1233 nil
1234 (setq pike-mode-map (c-make-inherited-keymap))
1235 ;; additional bindings
1236 (define-key pike-mode-map "\C-c\C-e" 'c-macro-expand))
1237
1238(easy-menu-define c-pike-menu pike-mode-map "Pike Mode Commands"
d9e94c22
MS
1239 (cons "Pike" (c-lang-const c-mode-menu pike)))
1240
50fdde0e
MS
1241;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(u?lpc\\|pike\\|pmod\\(.in\\)?\\)\\'" . pike-mode))
1242;;;###autoload (add-to-list 'interpreter-mode-alist '("pike" . pike-mode))
130c507e 1243
0ec8351b
BW
1244;;;###autoload
1245(defun pike-mode ()
1246 "Major mode for editing Pike code.
a66cd3ee
MS
1247To submit a problem report, enter `\\[c-submit-bug-report]' from a
1248pike-mode buffer. This automatically sets up a mail buffer with
0ec8351b
BW
1249version information already added. You just need to add a description
1250of the problem, including a reproducible test case, and send the
1251message.
1252
1253To see what version of CC Mode you are running, enter `\\[c-version]'.
1254
d9e94c22
MS
1255The hook `c-mode-common-hook' is run with no args at mode
1256initialization, then `pike-mode-hook'.
0ec8351b
BW
1257
1258Key bindings:
1259\\{pike-mode-map}"
1260 (interactive)
0ec8351b 1261 (kill-all-local-variables)
d9e94c22 1262 (c-initialize-cc-mode t)
0ec8351b
BW
1263 (set-syntax-table pike-mode-syntax-table)
1264 (setq major-mode 'pike-mode
1265 mode-name "Pike"
130c507e 1266 local-abbrev-table pike-mode-abbrev-table
a66cd3ee 1267 abbrev-mode t)
0ec8351b 1268 (use-local-map pike-mode-map)
28d88c16 1269 (c-init-language-vars-for 'pike-mode)
a66cd3ee 1270 (c-common-init 'pike-mode)
d9e94c22
MS
1271 (easy-menu-add c-pike-menu)
1272 ;;(cc-imenu-init cc-imenu-pike-generic-expression) ;TODO
3efc2cd7 1273 (c-run-mode-hooks 'c-mode-common-hook 'pike-mode-hook)
0ec8351b
BW
1274 (c-update-modeline))
1275
1276\f
0386b551 1277;; Support for AWK
d9e94c22 1278
50fdde0e
MS
1279;;;###autoload (add-to-list 'auto-mode-alist '("\\.awk\\'" . awk-mode))
1280;;;###autoload (add-to-list 'interpreter-mode-alist '("awk" . awk-mode))
1281;;;###autoload (add-to-list 'interpreter-mode-alist '("mawk" . awk-mode))
1282;;;###autoload (add-to-list 'interpreter-mode-alist '("nawk" . awk-mode))
1283;;;###autoload (add-to-list 'interpreter-mode-alist '("gawk" . awk-mode))
d9e94c22 1284
50fdde0e
MS
1285;;; Autoload directives must be on the top level, so we construct an
1286;;; autoload form instead.
791887d9 1287;;;###autoload (autoload 'awk-mode "cc-mode" "Major mode for editing AWK code." t)
50fdde0e 1288
0386b551
AM
1289(defvar awk-mode-abbrev-table nil
1290 "Abbreviation table used in awk-mode buffers.")
1291(c-define-abbrev-table 'awk-mode-abbrev-table
1292 '(("else" "else" c-electric-continued-statement 0)
1293 ("while" "while" c-electric-continued-statement 0)))
1294
1295(defvar awk-mode-map ()
1296 "Keymap used in awk-mode buffers.")
1297(if awk-mode-map
1298 nil
1299 (setq awk-mode-map (c-make-inherited-keymap))
1300 ;; add bindings which are only useful for awk.
1301 (define-key awk-mode-map "#" 'self-insert-command)
1302 (define-key awk-mode-map "/" 'self-insert-command)
1303 (define-key awk-mode-map "*" 'self-insert-command)
1304 (define-key awk-mode-map "\C-c\C-n" 'undefined) ; #if doesn't exist in awk.
1305 (define-key awk-mode-map "\C-c\C-p" 'undefined)
1306 (define-key awk-mode-map "\C-c\C-u" 'undefined)
1307 (define-key awk-mode-map "\M-a" 'c-beginning-of-statement) ; 2003/10/7
1308 (define-key awk-mode-map "\M-e" 'c-end-of-statement) ; 2003/10/7
1309 (define-key awk-mode-map "\C-\M-a" 'c-awk-beginning-of-defun)
1310 (define-key awk-mode-map "\C-\M-e" 'c-awk-end-of-defun))
1311
1312(easy-menu-define c-awk-menu awk-mode-map "AWK Mode Commands"
1313 (cons "AWK" (c-lang-const c-mode-menu awk)))
1314
1315(defun awk-mode ()
1316 "Major mode for editing AWK code.
d9e94c22
MS
1317To submit a problem report, enter `\\[c-submit-bug-report]' from an
1318awk-mode buffer. This automatically sets up a mail buffer with version
1319information already added. You just need to add a description of the
50fdde0e 1320problem, including a reproducible test case, and send the message.
d9e94c22
MS
1321
1322To see what version of CC Mode you are running, enter `\\[c-version]'.
1323
1324The hook `c-mode-common-hook' is run with no args at mode
1325initialization, then `awk-mode-hook'.
1326
1327Key bindings:
1328\\{awk-mode-map}"
0386b551
AM
1329 (interactive)
1330 (require 'cc-awk) ; Added 2003/6/10.
1331 (kill-all-local-variables)
1332 (c-initialize-cc-mode t)
1333 (set-syntax-table awk-mode-syntax-table)
1334 (setq major-mode 'awk-mode
1335 mode-name "AWK"
1336 local-abbrev-table awk-mode-abbrev-table
1337 abbrev-mode t)
1338 (use-local-map awk-mode-map)
1339 (c-init-language-vars-for 'awk-mode)
1340 (c-common-init 'awk-mode)
1341 ;; The rest of CC Mode does not (yet) use `font-lock-syntactic-keywords',
1342 ;; so it's not set by `c-font-lock-init'.
1343 (make-local-variable 'font-lock-syntactic-keywords)
1344 (setq font-lock-syntactic-keywords
1345 '((c-awk-set-syntax-table-properties
1346 0 (0) ; Everything on this line is a dummy.
1347 nil t)))
1348 (c-awk-unstick-NL-prop)
1349 (add-hook 'before-change-functions 'c-awk-before-change nil t)
1350 (add-hook 'after-change-functions 'c-awk-after-change nil t)
1351 (c-save-buffer-state nil
1352 (save-restriction
1353 (widen)
1354 (c-awk-clear-NL-props (point-min) (point-max))
1355 (c-awk-after-change (point-min) (point-max) 0))) ; Set syntax-table props.
1356
1357 ;; Prevent Xemacs's buffer-syntactic-context being used. See the comment
1358 ;; in cc-engine.el, just before (defun c-fast-in-literal ...
1359 (defalias 'c-in-literal 'c-slow-in-literal)
1360
1361 (c-run-mode-hooks 'c-mode-common-hook 'awk-mode-hook)
1362 (c-update-modeline))
d9e94c22
MS
1363
1364\f
c2efc1d0 1365;; bug reporting
785eecbb
RS
1366
1367(defconst c-mode-help-address
a66cd3ee
MS
1368 "bug-cc-mode@gnu.org"
1369 "Address(es) for CC Mode bug reports.")
785eecbb
RS
1370
1371(defun c-version ()
1372 "Echo the current version of CC Mode in the minibuffer."
1373 (interactive)
1374 (message "Using CC Mode version %s" c-version)
1375 (c-keep-region-active))
1376
130c507e
GM
1377(defvar c-prepare-bug-report-hooks nil)
1378
1379;; Dynamic variables used by reporter.
1380(defvar reporter-prompt-for-summary-p)
1381(defvar reporter-dont-compact-list)
1382
785eecbb
RS
1383(defun c-submit-bug-report ()
1384 "Submit via mail a bug report on CC Mode."
1385 (interactive)
77e31a8d 1386 (require 'reporter)
785eecbb
RS
1387 ;; load in reporter
1388 (let ((reporter-prompt-for-summary-p t)
1389 (reporter-dont-compact-list '(c-offsets-alist))
1390 (style c-indentation-style)
785eecbb
RS
1391 (c-features c-emacs-features))
1392 (and
1393 (if (y-or-n-p "Do you want to submit a report on CC Mode? ")
1394 t (message "") nil)
785eecbb
RS
1395 (reporter-submit-bug-report
1396 c-mode-help-address
d9e94c22 1397 (concat "CC Mode " c-version " (" mode-name ")")
51f606de 1398 (let ((vars (append
51f606de 1399 c-style-variables
d9e94c22 1400 '(c-buffer-is-cc-mode
51f606de 1401 c-tab-always-indent
d9e94c22
MS
1402 c-syntactic-indentation
1403 c-syntactic-indentation-in-macros
1404 c-ignore-auto-fill
1405 c-auto-align-backslashes
1406 c-backspace-function
1407 c-delete-function
1408 c-electric-pound-behavior
1409 c-default-style
1410 c-enable-xemacs-performance-kludge-p
1411 c-old-style-variable-behavior
51f606de
GM
1412 defun-prompt-regexp
1413 tab-width
1414 comment-column
1415 parse-sexp-ignore-comments
d9e94c22
MS
1416 parse-sexp-lookup-properties
1417 lookup-syntax-properties
51f606de
GM
1418 ;; A brain-damaged XEmacs only variable that, if
1419 ;; set to nil can cause all kinds of chaos.
1420 signal-error-on-buffer-boundary
1421 ;; Variables that affect line breaking and comments.
d9e94c22 1422 auto-fill-mode
91b807c9 1423 auto-fill-function
51f606de
GM
1424 filladapt-mode
1425 comment-multi-line
1426 comment-start-skip
1427 fill-prefix
d9e94c22 1428 fill-column
51f606de
GM
1429 paragraph-start
1430 adaptive-fill-mode
1431 adaptive-fill-regexp)
1432 nil)))
d61ca8d5
GM
1433 (mapc (lambda (var) (unless (boundp var)
1434 (setq vars (delq var vars))))
1435 '(signal-error-on-buffer-boundary
1436 filladapt-mode
1437 defun-prompt-regexp
1438 font-lock-mode
1439 font-lock-maximum-decoration
1440 parse-sexp-lookup-properties
1441 lookup-syntax-properties))
51f606de 1442 vars)
9714ec23 1443 (lambda ()
a66cd3ee 1444 (run-hooks 'c-prepare-bug-report-hooks)
d9e94c22 1445 (insert (format "Buffer Style: %s\nc-emacs-features: %s\n"
a66cd3ee 1446 style c-features)))))))
785eecbb
RS
1447
1448\f
130c507e 1449(cc-provide 'cc-mode)
ab5796a9 1450
0386b551 1451;;; arch-tag: 7825e5c4-fd09-439f-a04d-4c13208ba3d7
785eecbb 1452;;; cc-mode.el ends here