Convert consecutive FSF copyright years to ranges.
[bpt/emacs.git] / lisp / progmodes / cc-mode.el
CommitLineData
d9e94c22 1;;; cc-mode.el --- major mode for editing C and similar languages
785eecbb 2
73b0cd50 3;; Copyright (C) 1985, 1987, 1992-2011 Free Software Foundation, Inc.
785eecbb 4
d9e94c22
MS
5;; Authors: 2003- Alan Mackenzie
6;; 1998- Martin Stjernholm
7;; 1992-1999 Barry A. Warsaw
5858f68c
GM
8;; 1987 Dave Detlefs
9;; 1987 Stewart Clamen
785eecbb 10;; 1985 Richard M. Stallman
0ec8351b 11;; Maintainer: bug-cc-mode@gnu.org
785eecbb 12;; Created: a long, long, time ago. adapted from the original c-mode.el
bd78fa1d 13;; Keywords: c languages
785eecbb 14
785eecbb
RS
15;; This file is part of GNU Emacs.
16
b1fc2b50 17;; GNU Emacs is free software: you can redistribute it and/or modify
785eecbb 18;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
19;; the Free Software Foundation, either version 3 of the License, or
20;; (at your option) any later version.
785eecbb
RS
21
22;; GNU Emacs is distributed in the hope that it will be useful,
23;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25;; GNU General Public License for more details.
26
27;; You should have received a copy of the GNU General Public License
b1fc2b50 28;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
785eecbb 29
d9e94c22 30;;; Commentary:
130c507e
GM
31
32;; NOTE: Read the commentary below for the right way to submit bug reports!
33;; NOTE: See the accompanying texinfo manual for details on using this mode!
d9e94c22 34;; Note: The version string is in cc-defs.
785eecbb
RS
35
36;; This package provides GNU Emacs major modes for editing C, C++,
d9e94c22
MS
37;; Objective-C, Java, CORBA's IDL, Pike and AWK code. As of the
38;; latest Emacs and XEmacs releases, it is the default package for
39;; editing these languages. This package is called "CC Mode", and
40;; should be spelled exactly this way.
0ec8351b
BW
41
42;; CC Mode supports K&R and ANSI C, ANSI C++, Objective-C, Java,
d9e94c22
MS
43;; CORBA's IDL, Pike and AWK with a consistent indentation model
44;; across all modes. This indentation model is intuitive and very
45;; flexible, so that almost any desired style of indentation can be
46;; supported. Installation, usage, and programming details are
47;; contained in an accompanying texinfo manual.
785eecbb
RS
48
49;; CC Mode's immediate ancestors were, c++-mode.el, cplus-md.el, and
50;; cplus-md1.el..
51
785eecbb 52;; To submit bug reports, type "C-c C-b". These will be sent to
0ec8351b
BW
53;; bug-gnu-emacs@gnu.org (mirrored as the Usenet newsgroup
54;; gnu.emacs.bug) as well as bug-cc-mode@gnu.org, which directly
55;; contacts the CC Mode maintainers. Questions can sent to
56;; help-gnu-emacs@gnu.org (mirrored as gnu.emacs.help) and/or
130c507e
GM
57;; bug-cc-mode@gnu.org. Please do not send bugs or questions to our
58;; personal accounts; we reserve the right to ignore such email!
785eecbb 59
785eecbb
RS
60;; Many, many thanks go out to all the folks on the beta test list.
61;; Without their patience, testing, insight, code contributions, and
62;; encouragement CC Mode would be a far inferior package.
63
64;; You can get the latest version of CC Mode, including PostScript
65;; documentation and separate individual files from:
66;;
91b807c9 67;; http://cc-mode.sourceforge.net/
0ec8351b
BW
68;;
69;; You can join a moderated CC Mode announcement-only mailing list by
70;; visiting
785eecbb 71;;
91b807c9 72;; http://lists.sourceforge.net/mailman/listinfo/cc-mode-announce
785eecbb
RS
73
74;;; Code:
75
ae500c1a
GM
76;; For Emacs < 22.2.
77(eval-and-compile
78 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
79
51f606de
GM
80(eval-when-compile
81 (let ((load-path
130c507e
GM
82 (if (and (boundp 'byte-compile-dest-file)
83 (stringp byte-compile-dest-file))
84 (cons (file-name-directory byte-compile-dest-file) load-path)
51f606de 85 load-path)))
d9e94c22 86 (load "cc-bytecomp" nil t)))
130c507e
GM
87
88(cc-require 'cc-defs)
d9e94c22 89(cc-require-when-compile 'cc-langs)
130c507e 90(cc-require 'cc-vars)
130c507e 91(cc-require 'cc-engine)
d9e94c22 92(cc-require 'cc-styles)
130c507e
GM
93(cc-require 'cc-cmds)
94(cc-require 'cc-align)
d9e94c22 95(cc-require 'cc-menus)
130c507e 96
3efc2cd7 97;; Silence the compiler.
0386b551 98(cc-bytecomp-defvar adaptive-fill-first-line-regexp) ; Emacs
130c507e 99(cc-bytecomp-defun set-keymap-parents) ; XEmacs
0386b551 100(cc-bytecomp-defun run-mode-hooks) ; 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.
653d1554 113(autoload 'subword-mode "subword"
0386b551
AM
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)
d2f79585
TTN
193 (setq c-block-comment-prefix
194 (symbol-value 'c-comment-continuation-stars)))
a66cd3ee 195 (add-hook 'change-major-mode-hook 'c-leave-cc-mode-mode)
0ec8351b
BW
196 (setq c-initialization-ok t))
197 ;; Will try initialization hooks again if they failed.
d9e94c22
MS
198 (put 'c-initialize-cc-mode initprop c-initialization-ok))))
199
200 (unless new-style-init
28d88c16 201 (c-init-language-vars-for 'c-mode)))
1dad6919
RS
202
203\f
d9e94c22
MS
204;;; Common routines.
205
130c507e
GM
206(defvar c-mode-base-map ()
207 "Keymap shared by all CC Mode related modes.")
208
209(defun c-make-inherited-keymap ()
210 (let ((map (make-sparse-keymap)))
3efc2cd7
MS
211 ;; Necessary to use `cc-bytecomp-fboundp' below since this
212 ;; function is called from top-level forms that are evaluated
213 ;; while cc-bytecomp is active when one does M-x eval-buffer.
130c507e 214 (cond
d9e94c22 215 ;; Emacs
3efc2cd7 216 ((cc-bytecomp-fboundp 'set-keymap-parent)
130c507e 217 (set-keymap-parent map c-mode-base-map))
b705155a
SM
218 ;; XEmacs
219 ((cc-bytecomp-fboundp 'set-keymap-parents)
220 (set-keymap-parents map c-mode-base-map))
130c507e
GM
221 ;; incompatible
222 (t (error "CC Mode is incompatible with this version of Emacs")))
223 map))
224
a66cd3ee
MS
225(defun c-define-abbrev-table (name defs)
226 ;; Compatibility wrapper for `define-abbrev' which passes a non-nil
227 ;; sixth argument for SYSTEM-FLAG in emacsen that support it
0386b551 228 ;; (currently only Emacs >= 21.2).
d9e94c22
MS
229 (let ((table (or (symbol-value name)
230 (progn (define-abbrev-table name nil)
231 (symbol-value name)))))
a66cd3ee
MS
232 (while defs
233 (condition-case nil
234 (apply 'define-abbrev table (append (car defs) '(t)))
235 (wrong-number-of-arguments
236 (apply 'define-abbrev table (car defs))))
237 (setq defs (cdr defs)))))
d9e94c22 238(put 'c-define-abbrev-table 'lisp-indent-function 1)
a66cd3ee 239
0386b551
AM
240(defun c-bind-special-erase-keys ()
241 ;; Only used in Emacs to bind C-c C-<delete> and C-c C-<backspace>
242 ;; to the proper keys depending on `normal-erase-is-backspace'.
243 (if normal-erase-is-backspace
244 (progn
245 (define-key c-mode-base-map (kbd "C-c C-<delete>")
246 'c-hungry-delete-forward)
247 (define-key c-mode-base-map (kbd "C-c C-<backspace>")
cb694ab7 248 'c-hungry-delete-backwards))
0386b551 249 (define-key c-mode-base-map (kbd "C-c C-<delete>")
cb694ab7 250 'c-hungry-delete-backwards)
0386b551
AM
251 (define-key c-mode-base-map (kbd "C-c C-<backspace>")
252 'c-hungry-delete-forward)))
253
130c507e
GM
254(if c-mode-base-map
255 nil
0386b551 256
130c507e 257 (setq c-mode-base-map (make-sparse-keymap))
0386b551 258
130c507e
GM
259 ;; Separate M-BS from C-M-h. The former should remain
260 ;; backward-kill-word.
261 (define-key c-mode-base-map [(control meta h)] 'c-mark-function)
262 (define-key c-mode-base-map "\e\C-q" 'c-indent-exp)
263 (substitute-key-definition 'backward-sentence
264 'c-beginning-of-statement
265 c-mode-base-map global-map)
266 (substitute-key-definition 'forward-sentence
267 'c-end-of-statement
268 c-mode-base-map global-map)
269 (substitute-key-definition 'indent-new-comment-line
270 'c-indent-new-comment-line
271 c-mode-base-map global-map)
0386b551 272 (substitute-key-definition 'indent-for-tab-command
17ee4625
DN
273 ;; XXX Is this the right thing to do
274 ;; here?
275 'c-indent-line-or-region
0386b551 276 c-mode-base-map global-map)
a66cd3ee
MS
277 (when (fboundp 'comment-indent-new-line)
278 ;; indent-new-comment-line has changed name to
279 ;; comment-indent-new-line in Emacs 21.
280 (substitute-key-definition 'comment-indent-new-line
281 'c-indent-new-comment-line
282 c-mode-base-map global-map))
0386b551 283
130c507e 284 ;; RMS says don't make these the default.
51c9af45 285 ;; (April 2006): RMS has now approved these commands as defaults.
13ac2398
AM
286 (unless (memq 'argumentative-bod-function c-emacs-features)
287 (define-key c-mode-base-map "\e\C-a" 'c-beginning-of-defun)
288 (define-key c-mode-base-map "\e\C-e" 'c-end-of-defun))
0386b551 289
130c507e
GM
290 (define-key c-mode-base-map "\C-c\C-n" 'c-forward-conditional)
291 (define-key c-mode-base-map "\C-c\C-p" 'c-backward-conditional)
292 (define-key c-mode-base-map "\C-c\C-u" 'c-up-conditional)
d9e94c22
MS
293
294 ;; It doesn't suffice to put `c-fill-paragraph' on
295 ;; `fill-paragraph-function' since `c-fill-paragraph' must be called
296 ;; before any fill prefix adaption is done. E.g. `filladapt-mode'
297 ;; replaces `fill-paragraph' and does the adaption before calling
298 ;; `fill-paragraph-function', and we have to mask comments etc
299 ;; before that. Also, `c-fill-paragraph' chains on to
300 ;; `fill-paragraph' and the value on `fill-parapgraph-function' to
301 ;; do the actual filling work.
130c507e
GM
302 (substitute-key-definition 'fill-paragraph 'c-fill-paragraph
303 c-mode-base-map global-map)
304 ;; In XEmacs the default fill function is called
305 ;; fill-paragraph-or-region.
306 (substitute-key-definition 'fill-paragraph-or-region 'c-fill-paragraph
307 c-mode-base-map global-map)
d9e94c22 308
0386b551
AM
309 ;; We bind the forward deletion key and (implicitly) C-d to
310 ;; `c-electric-delete-forward', and the backward deletion key to
311 ;; `c-electric-backspace'. The hungry variants are bound to the
312 ;; same keys but prefixed with C-c. This implies that C-c C-d is
313 ;; `c-hungry-delete-forward'. For consistency, we bind not only C-c
cb694ab7
AM
314 ;; <backspace> to `c-hungry-delete-backwards' but also
315 ;; C-c C-<backspace>, so that the Ctrl key can be held down during
316 ;; the whole sequence regardless of the direction. This in turn
317 ;; implies that we bind C-c C-<delete> to `c-hungry-delete-forward',
318 ;; for the same reason.
0386b551 319
6d28be1d
EZ
320 ;; Bind the electric deletion functions to C-d and DEL. Emacs 21
321 ;; automatically maps the [delete] and [backspace] keys to these two
322 ;; depending on window system and user preferences. (In earlier
323 ;; versions it's possible to do the same by using `function-key-map'.)
324 (define-key c-mode-base-map "\C-d" 'c-electric-delete-forward)
325 (define-key c-mode-base-map "\177" 'c-electric-backspace)
0386b551 326 (define-key c-mode-base-map "\C-c\C-d" 'c-hungry-delete-forward)
cb694ab7
AM
327 (define-key c-mode-base-map [?\C-c ?\d] 'c-hungry-delete-backwards)
328 (define-key c-mode-base-map [?\C-c ?\C-\d] 'c-hungry-delete-backwards)
0386b551
AM
329 (define-key c-mode-base-map [?\C-c deletechar] 'c-hungry-delete-forward) ; C-c <delete> on a tty.
330 (define-key c-mode-base-map [?\C-c (control deletechar)] ; C-c C-<delete> on a tty.
331 'c-hungry-delete-forward)
332 (when (boundp 'normal-erase-is-backspace)
333 ;; The automatic C-d and DEL mapping functionality doesn't extend
334 ;; to special combinations like C-c C-<delete>, so we have to hook
335 ;; into the `normal-erase-is-backspace' system to bind it directly
336 ;; as appropriate.
337 (add-hook 'normal-erase-is-backspace-hook 'c-bind-special-erase-keys)
338 (c-bind-special-erase-keys))
339
340 (when (fboundp 'delete-forward-p)
341 ;; In XEmacs we fix the forward and backward deletion behavior by
342 ;; binding the keysyms for the [delete] and [backspace] keys
343 ;; directly, and use `delete-forward-p' to decide what [delete]
344 ;; should do. That's done in the XEmacs specific
345 ;; `c-electric-delete' and `c-hungry-delete' functions.
6d28be1d 346 (define-key c-mode-base-map [delete] 'c-electric-delete)
0386b551
AM
347 (define-key c-mode-base-map [backspace] 'c-electric-backspace)
348 (define-key c-mode-base-map (kbd "C-c <delete>") 'c-hungry-delete)
349 (define-key c-mode-base-map (kbd "C-c C-<delete>") 'c-hungry-delete)
cb694ab7
AM
350 (define-key c-mode-base-map (kbd "C-c <backspace>")
351 'c-hungry-delete-backwards)
352 (define-key c-mode-base-map (kbd "C-c C-<backspace>")
353 'c-hungry-delete-backwards))
0386b551
AM
354
355 (define-key c-mode-base-map "#" 'c-electric-pound)
356 (define-key c-mode-base-map "{" 'c-electric-brace)
357 (define-key c-mode-base-map "}" 'c-electric-brace)
130c507e 358 (define-key c-mode-base-map "/" 'c-electric-slash)
0386b551
AM
359 (define-key c-mode-base-map "*" 'c-electric-star)
360 (define-key c-mode-base-map ";" 'c-electric-semi&comma)
361 (define-key c-mode-base-map "," 'c-electric-semi&comma)
362 (define-key c-mode-base-map ":" 'c-electric-colon)
363 (define-key c-mode-base-map "(" 'c-electric-paren)
364 (define-key c-mode-base-map ")" 'c-electric-paren)
365
130c507e 366 (define-key c-mode-base-map "\C-c\C-\\" 'c-backslash-region)
0386b551 367 (define-key c-mode-base-map "\C-c\C-a" 'c-toggle-auto-newline)
130c507e
GM
368 (define-key c-mode-base-map "\C-c\C-b" 'c-submit-bug-report)
369 (define-key c-mode-base-map "\C-c\C-c" 'comment-region)
0386b551 370 (define-key c-mode-base-map "\C-c\C-l" 'c-toggle-electric-state)
130c507e 371 (define-key c-mode-base-map "\C-c\C-o" 'c-set-offset)
0386b551 372 (define-key c-mode-base-map "\C-c\C-q" 'c-indent-defun)
130c507e 373 (define-key c-mode-base-map "\C-c\C-s" 'c-show-syntactic-information)
0386b551 374 ;; (define-key c-mode-base-map "\C-c\C-t" 'c-toggle-auto-hungry-state) Commented out by ACM, 2005-03-05.
130c507e
GM
375 (define-key c-mode-base-map "\C-c." 'c-set-style)
376 ;; conflicts with OOBR
377 ;;(define-key c-mode-base-map "\C-c\C-v" 'c-version)
0386b551 378 ;; (define-key c-mode-base-map "\C-c\C-y" 'c-toggle-hungry-state) Commented out by ACM, 2005-11-22.
653d1554 379 (define-key c-mode-base-map "\C-c\C-w" 'subword-mode)
130c507e
GM
380 )
381
130c507e
GM
382;; We don't require the outline package, but we configure it a bit anyway.
383(cc-bytecomp-defvar outline-level)
384
d9e94c22
MS
385(defun c-mode-menu (modestr)
386 "Return a menu spec suitable for `easy-menu-define' that is exactly
387like the C mode menu except that the menu bar item name is MODESTR
388instead of \"C\".
389
390This function is provided for compatibility only; derived modes should
391preferably use the `c-mode-menu' language constant directly."
392 (cons modestr (c-lang-const c-mode-menu c)))
393
394;; Ugly hack to pull in the definition of `c-populate-syntax-table'
395;; from cc-langs to make it available at runtime. It's either this or
396;; moving the definition for it to cc-defs, but that would mean to
397;; break up the syntax table setup over two files.
398(defalias 'c-populate-syntax-table
399 (cc-eval-when-compile
400 (let ((f (symbol-function 'c-populate-syntax-table)))
401 (if (byte-code-function-p f) f (byte-compile f)))))
402
0386b551
AM
403;; CAUTION: Try to avoid installing things on
404;; `before-change-functions'. The macro `combine-after-change-calls'
405;; is used and it doesn't work if there are things on that hook. That
406;; can cause font lock functions to run in inconvenient places during
407;; temporary changes in some font lock support modes, causing extra
408;; unnecessary work and font lock glitches due to interactions between
409;; various text properties.
0ec1d2c5 410;;
9c184ed2
AM
411;; (2007-02-12): The macro `combine-after-change-calls' ISN'T used any
412;; more.
413
414(defun c-unfind-enclosing-token (pos)
415 ;; If POS is wholly inside a token, remove that id from
416 ;; `c-found-types', should it be present. Return t if we were in an
417 ;; id, else nil.
418 (save-excursion
419 (let ((tok-beg (progn (goto-char pos)
420 (and (c-beginning-of-current-token) (point))))
421 (tok-end (progn (goto-char pos)
422 (and (c-end-of-current-token) (point)))))
423 (when (and tok-beg tok-end)
424 (c-unfind-type (buffer-substring-no-properties tok-beg tok-end))
425 t))))
426
427(defun c-unfind-coalesced-tokens (beg end)
428 ;; unless the non-empty region (beg end) is entirely WS and there's at
429 ;; least one character of WS just before or after this region, remove
430 ;; the tokens which touch the region from `c-found-types' should they
431 ;; be present.
432 (or (c-partial-ws-p beg end)
433 (save-excursion
434 (progn
435 (goto-char beg)
436 (or (eq beg (point-min))
437 (c-skip-ws-backward (1- beg))
438 (/= (point) beg)
439 (= (c-backward-token-2) 1)
440 (c-unfind-type (buffer-substring-no-properties
441 (point) beg)))
442 (goto-char end)
443 (or (eq end (point-max))
444 (c-skip-ws-forward (1+ end))
445 (/= (point) end)
446 (progn (forward-char) (c-end-of-current-token) nil)
447 (c-unfind-type (buffer-substring-no-properties
448 end (point))))))))
449
450;; c-maybe-stale-found-type records a place near the region being
0ec1d2c5 451;; changed where an element of `found-types' might become stale. It
9c184ed2
AM
452;; is set in c-before-change and is either nil, or has the form:
453;;
454;; (c-decl-id-start "foo" 97 107 " (* ooka) " "o"), where
0ec1d2c5 455;;
9c184ed2
AM
456;; o - `c-decl-id-start' is the c-type text property value at buffer
457;; pos 96.
0ec1d2c5 458;;
9c184ed2
AM
459;; o - 97 107 is the region potentially containing the stale type -
460;; this is delimited by a non-nil c-type text property at 96 and
461;; either another one or a ";", "{", or "}" at 107.
0ec1d2c5 462;;
9c184ed2
AM
463;; o - " (* ooka) " is the (before change) buffer portion containing
464;; the suspect type (here "ooka").
465;;
466;; o - "o" is the buffer contents which is about to be deleted. This
467;; would be the empty string for an insertion.
468(defvar c-maybe-stale-found-type nil)
469(make-variable-buffer-local 'c-maybe-stale-found-type)
470
d9e94c22
MS
471(defun c-basic-common-init (mode default-style)
472 "Do the necessary initialization for the syntax handling routines
473and the line breaking/filling code. Intended to be used by other
474packages that embed CC Mode.
475
476MODE is the CC Mode flavor to set up, e.g. 'c-mode or 'java-mode.
477DEFAULT-STYLE tells which indentation style to install. It has the
478same format as `c-default-style'.
479
480Note that `c-init-language-vars' must be called before this function.
481This function cannot do that since `c-init-language-vars' is a macro
482that requires a literal mode spec at compile time."
d9e94c22 483
a66cd3ee
MS
484 (setq c-buffer-is-cc-mode mode)
485
130c507e
GM
486 ;; these variables should always be buffer local; they do not affect
487 ;; indentation style.
130c507e
GM
488 (make-local-variable 'comment-start)
489 (make-local-variable 'comment-end)
130c507e 490 (make-local-variable 'comment-start-skip)
175069ef 491
0386b551
AM
492 (make-local-variable 'paragraph-start)
493 (make-local-variable 'paragraph-separate)
494 (make-local-variable 'paragraph-ignore-fill-prefix)
495 (make-local-variable 'adaptive-fill-mode)
496 (make-local-variable 'adaptive-fill-regexp)
a66cd3ee 497
130c507e 498 ;; now set their values
175069ef
SM
499 (set (make-local-variable 'parse-sexp-ignore-comments) t)
500 (set (make-local-variable 'indent-line-function) 'c-indent-line)
501 (set (make-local-variable 'indent-region-function) 'c-indent-region)
502 (set (make-local-variable 'normal-auto-fill-function) 'c-do-auto-fill)
503 (set (make-local-variable 'comment-multi-line) t)
504 (set (make-local-variable 'comment-line-break-function)
505 'c-indent-new-comment-line)
a66cd3ee 506
fd8771f5
MS
507 ;; Install `c-fill-paragraph' on `fill-paragraph-function' so that a
508 ;; direct call to `fill-paragraph' behaves better. This still
509 ;; doesn't work with filladapt but it's better than nothing.
175069ef 510 (set (make-local-variable 'fill-paragraph-function) 'c-fill-paragraph)
fd8771f5 511
0ec1d2c5
AM
512 ;; Initialise the cache of brace pairs, and opening braces/brackets/parens.
513 (c-state-cache-init)
514
0386b551 515 (when (or c-recognize-<>-arglists
1d1e4868 516 (c-major-mode-is 'awk-mode)
452ea855 517 (c-major-mode-is '(java-mode c-mode c++-mode objc-mode)))
0386b551
AM
518 ;; We'll use the syntax-table text property to change the syntax
519 ;; of some chars for this language, so do the necessary setup for
520 ;; that.
521 ;;
522 ;; Note to other package developers: It's ok to turn this on in CC
523 ;; Mode buffers when CC Mode doesn't, but it's not ok to turn it
524 ;; off if CC Mode has turned it on.
525
526 ;; Emacs.
527 (when (boundp 'parse-sexp-lookup-properties)
175069ef 528 (set (make-local-variable 'parse-sexp-lookup-properties) t))
0386b551
AM
529
530 ;; Same as above for XEmacs.
531 (when (boundp 'lookup-syntax-properties)
175069ef 532 (set (make-local-variable 'lookup-syntax-properties) t)))
d9e94c22 533
d0fcee66 534 ;; Use this in Emacs 21+ to avoid meddling with the rear-nonsticky
d9e94c22
MS
535 ;; property on each character.
536 (when (boundp 'text-property-default-nonsticky)
d0fcee66
AM
537 (mapc (lambda (tprop)
538 (unless (assq tprop text-property-default-nonsticky)
175069ef
SM
539 (set (make-local-variable 'text-property-default-nonsticky)
540 (cons `(,tprop . t) text-property-default-nonsticky))))
d0fcee66 541 '(syntax-table category c-type)))
d9e94c22
MS
542
543 ;; In Emacs 21 and later it's possible to turn off the ad-hoc
544 ;; heuristic that open parens in column 0 are defun starters. Since
0386b551
AM
545 ;; we have c-state-cache, that heuristic isn't useful and only causes
546 ;; trouble, so turn it off.
99c8496e
AM
547;; 2006/12/17: This facility is somewhat confused, and doesn't really seem
548;; helpful. Comment it out for now.
549;; (when (memq 'col-0-paren c-emacs-features)
550;; (make-local-variable 'open-paren-in-column-0-is-defun-start)
551;; (setq open-paren-in-column-0-is-defun-start nil))
d9e94c22 552
d9e94c22
MS
553 (c-clear-found-types)
554
555 ;; now set the mode style based on default-style
556 (let ((style (if (stringp default-style)
557 default-style
558 (or (cdr (assq mode default-style))
559 (cdr (assq 'other default-style))
130c507e
GM
560 "gnu"))))
561 ;; Override style variables if `c-old-style-variable-behavior' is
562 ;; set. Also override if we are using global style variables,
563 ;; have already initialized a style once, and are switching to a
564 ;; different style. (It's doubtful whether this is desirable, but
565 ;; the whole situation with nonlocal style variables is a bit
566 ;; awkward. It's at least the most compatible way with the old
567 ;; style init procedure.)
568 (c-set-style style (not (or c-old-style-variable-behavior
569 (and (not c-style-variables-are-local-p)
570 c-indentation-style
571 (not (string-equal c-indentation-style
572 style)))))))
a66cd3ee
MS
573 (c-setup-paragraph-variables)
574
130c507e
GM
575 ;; we have to do something special for c-offsets-alist so that the
576 ;; buffer local value has its own alist structure.
577 (setq c-offsets-alist (copy-alist c-offsets-alist))
a66cd3ee 578
130c507e 579 ;; setup the comment indent variable in a Emacs version portable way
175069ef 580 (set (make-local-variable 'comment-indent-function) 'c-comment-indent)
a66cd3ee 581
cb694ab7
AM
582;; ;; Put submode indicators onto minor-mode-alist, but only once.
583;; (or (assq 'c-submode-indicators minor-mode-alist)
584;; (setq minor-mode-alist
585;; (cons '(c-submode-indicators c-submode-indicators)
586;; minor-mode-alist)))
587 (c-update-modeline)
a66cd3ee 588
d9e94c22
MS
589 ;; Install the functions that ensure that various internal caches
590 ;; don't become invalid due to buffer changes.
922ad43e
GM
591 (when (featurep 'xemacs)
592 (make-local-hook 'before-change-functions)
593 (make-local-hook 'after-change-functions))
9c184ed2 594 (add-hook 'before-change-functions 'c-before-change nil t)
8714fc18 595 (add-hook 'after-change-functions 'c-after-change nil t)
7d562742 596 (set (make-local-variable 'font-lock-extend-after-change-region-function)
a0526d5e
AM
597 'c-extend-after-change-region)) ; Currently (2009-05) used by all
598 ; lanaguages with #define (C, C++,; ObjC), and by AWK.
d9e94c22 599
d9e94c22
MS
600(defun c-setup-doc-comment-style ()
601 "Initialize the variables that depend on the value of `c-doc-comment-style'."
602 (when (and (featurep 'font-lock)
603 (symbol-value 'font-lock-mode))
604 ;; Force font lock mode to reinitialize itself.
605 (font-lock-mode 0)
606 (font-lock-mode 1)))
607
f3b554af
GM
608;; Buffer local variables defining the region to be fontified by a font lock
609;; after-change function. They are set in c-after-change to
610;; after-change-function's BEG and END, and may be modified by a
611;; `c-before-font-lock-function'.
612(defvar c-new-BEG 0)
613(make-variable-buffer-local 'c-new-BEG)
614(defvar c-new-END 0)
615(make-variable-buffer-local 'c-new-END)
616
d9e94c22
MS
617(defun c-common-init (&optional mode)
618 "Common initialization for all CC Mode modes.
619In addition to the work done by `c-basic-common-init' and
620`c-font-lock-init', this function sets up various other things as
621customary in CC Mode modes but which aren't strictly necessary for CC
622Mode to operate correctly.
623
624MODE is the symbol for the mode to initialize, like 'c-mode. See
625`c-basic-common-init' for details. It's only optional to be
0386b551 626compatible with old code; callers should always specify it."
d9e94c22
MS
627
628 (unless mode
629 ;; Called from an old third party package. The fallback is to
630 ;; initialize for C.
28d88c16 631 (c-init-language-vars-for 'c-mode))
d9e94c22
MS
632
633 (c-basic-common-init mode c-default-style)
634 (when mode
635 ;; Only initialize font locking if we aren't called from an old package.
636 (c-font-lock-init))
637
1d1e4868
AM
638 ;; Starting a mode is a sort of "change". So call the change functions...
639 (save-restriction
640 (widen)
43a91810
AM
641 (setq c-new-BEG (point-min))
642 (setq c-new-END (point-max))
1d1e4868 643 (save-excursion
dd969a56 644 (if c-get-state-before-change-functions
8a249abc
AM
645 (mapc (lambda (fn)
646 (funcall fn (point-min) (point-max)))
647 c-get-state-before-change-functions))
1d1e4868
AM
648 (if c-before-font-lock-function
649 (funcall c-before-font-lock-function (point-min) (point-max)
650 (- (point-max) (point-min))))))
651
175069ef
SM
652 (set (make-local-variable 'outline-regexp) "[^#\n\^M]")
653 (set (make-local-variable 'outline-level) 'c-outline-level)
d9e94c22
MS
654
655 (let ((rfn (assq mode c-require-final-newline)))
656 (when rfn
1750e02f 657 (and (cdr rfn)
175069ef
SM
658 (set (make-local-variable 'require-final-newline)
659 mode-require-final-newline)))))
130c507e 660
00af0b67
AM
661(defun c-count-cfss (lv-alist)
662 ;; LV-ALIST is an alist like `file-local-variables-alist'. Count how many
663 ;; elements with the key `c-file-style' there are in it.
664 (let ((elt-ptr lv-alist) elt (cownt 0))
665 (while elt-ptr
666 (setq elt (car elt-ptr)
667 elt-ptr (cdr elt-ptr))
668 (when (eq (car elt) 'c-file-style)
669 (setq cownt (1+ cownt))))
670 cownt))
671
a36abf0e
CY
672(defun c-before-hack-hook ()
673 "Set the CC Mode style and \"offsets\" when in the buffer's local variables.
674They are set only when, respectively, the pseudo variables
675`c-file-style' and `c-file-offsets' are present in the list.
676
677This function is called from the hook `before-hack-local-variables-hook'."
678 (when c-buffer-is-cc-mode
05c123e6
AM
679 (let ((mode-cons (assq 'mode file-local-variables-alist))
680 (stile (cdr (assq 'c-file-style file-local-variables-alist)))
a36abf0e 681 (offsets (cdr (assq 'c-file-offsets file-local-variables-alist))))
05c123e6
AM
682 (when mode-cons
683 (hack-one-local-variable (car mode-cons) (cdr mode-cons))
684 (setq file-local-variables-alist
685 (delq mode-cons file-local-variables-alist)))
a36abf0e
CY
686 (when stile
687 (or (stringp stile) (error "c-file-style is not a string"))
00af0b67
AM
688 (if (boundp 'dir-local-variables-alist)
689 ;; Determine whether `c-file-style' was set in the file's local
690 ;; variables or in a .dir-locals.el (a directory setting).
691 (let ((cfs-in-file-and-dir-count
692 (c-count-cfss file-local-variables-alist))
693 (cfs-in-dir-count (c-count-cfss dir-local-variables-alist)))
694 (c-set-style stile
695 (= cfs-in-file-and-dir-count cfs-in-dir-count)))
696 (c-set-style stile)))
a36abf0e
CY
697 (when offsets
698 (mapc
699 (lambda (langentry)
700 (let ((langelem (car langentry))
701 (offset (cdr langentry)))
702 (c-set-offset langelem offset)))
703 offsets)))))
704
cb694ab7
AM
705(defun c-remove-any-local-eval-or-mode-variables ()
706 ;; If the buffer specifies `mode' or `eval' in its File Local Variable list
707 ;; or on the first line, remove all occurrences. See
708 ;; `c-postprocess-file-styles' for justification. There is no need to save
5c4c911a
AM
709 ;; point here, or even bother too much about the buffer contents. However,
710 ;; DON'T mess up the kill-ring.
cb694ab7
AM
711 ;;
712 ;; Most of the code here is derived from Emacs 21.3's `hack-local-variables'
713 ;; in files.el.
714 (goto-char (point-max))
715 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
716 (let (lv-point (prefix "") (suffix ""))
717 (when (let ((case-fold-search t))
718 (search-forward "Local Variables:" nil t))
719 (setq lv-point (point))
720 ;; The prefix is what comes before "local variables:" in its line.
721 ;; The suffix is what comes after "local variables:" in its line.
722 (skip-chars-forward " \t")
723 (or (eolp)
724 (setq suffix (buffer-substring (point)
725 (progn (end-of-line) (point)))))
726 (goto-char (match-beginning 0))
727 (or (bolp)
728 (setq prefix
729 (buffer-substring (point)
730 (progn (beginning-of-line) (point)))))
731
732 (while (search-forward-regexp
733 (concat "^[ \t]*"
734 (regexp-quote prefix)
735 "\\(mode\\|eval\\):.*"
736 (regexp-quote suffix)
737 "$")
738 nil t)
5c4c911a
AM
739 (forward-line 0)
740 (delete-region (point) (progn (forward-line) (point)))))
cb694ab7
AM
741
742 ;; Delete the first line, if we've got one, in case it contains a mode spec.
743 (unless (and lv-point
744 (progn (goto-char lv-point)
745 (forward-line 0)
746 (bobp)))
747 (goto-char (point-min))
5c4c911a
AM
748 (unless (eobp)
749 (delete-region (point) (progn (forward-line) (point)))))))
cb694ab7 750
130c507e 751(defun c-postprocess-file-styles ()
d9e94c22 752 "Function that post processes relevant file local variables in CC Mode.
130c507e
GM
753Currently, this function simply applies any style and offset settings
754found in the file's Local Variable list. It first applies any style
755setting found in `c-file-style', then it applies any offset settings
756it finds in `c-file-offsets'.
757
758Note that the style variables are always made local to the buffer."
d9e94c22 759
130c507e 760 ;; apply file styles and offsets
09f90d2c
RS
761 (when c-buffer-is-cc-mode
762 (if (or c-file-style c-file-offsets)
763 (c-make-styles-buffer-local t))
c5fcadd1
AM
764 (when c-file-style
765 (or (stringp c-file-style)
766 (error "c-file-style is not a string"))
a3c11552 767 (c-set-style c-file-style))
b9a6ed29 768
09f90d2c 769 (and c-file-offsets
d61ca8d5 770 (mapc
09f90d2c
RS
771 (lambda (langentry)
772 (let ((langelem (car langentry))
773 (offset (cdr langentry)))
774 (c-set-offset langelem offset)))
0386b551
AM
775 c-file-offsets))
776 ;; Problem: The file local variable block might have explicitly set a
777 ;; style variable. The `c-set-style' or `mapcar' call might have
778 ;; overwritten this. So we run `hack-local-variables' again to remedy
779 ;; this. There are no guarantees this will work properly, particularly as
780 ;; we have no control over what the other hook functions on
cb694ab7
AM
781 ;; `hack-local-variables-hook' would have done. We now (2006/2/1) remove
782 ;; any `eval' or `mode' expressions before we evaluate again (see below).
783 ;; ACM, 2005/11/2.
784 ;;
785 ;; Problem (bug reported by Gustav Broberg): if one of the variables is
786 ;; `mode', this will invoke c-mode (etc.) again, setting up the style etc.
787 ;; We prevent this by temporarily removing `mode' from the Local Variables
788 ;; section.
0386b551 789 (if (or c-file-style c-file-offsets)
51c9af45
AM
790 (let ((hack-local-variables-hook nil) (inhibit-read-only t))
791 (c-tentative-buffer-changes
cb694ab7
AM
792 (c-remove-any-local-eval-or-mode-variables)
793 (hack-local-variables))
794 nil))))
130c507e 795
a36abf0e
CY
796(if (boundp 'before-hack-local-variables-hook)
797 (add-hook 'before-hack-local-variables-hook 'c-before-hack-hook)
798 (add-hook 'hack-local-variables-hook 'c-postprocess-file-styles))
130c507e 799
3efc2cd7
MS
800(defmacro c-run-mode-hooks (&rest hooks)
801 ;; Emacs 21.1 has introduced a system with delayed mode hooks that
dd969a56 802 ;; requires the use of the new function `run-mode-hooks'.
3efc2cd7
MS
803 (if (cc-bytecomp-fboundp 'run-mode-hooks)
804 `(run-mode-hooks ,@hooks)
805 `(progn ,@(mapcar (lambda (hook) `(run-hooks ,hook)) hooks))))
806
130c507e 807\f
1d1e4868
AM
808;;; Change hooks, linking with Font Lock.
809
1d1e4868
AM
810;; Buffer local variables recording Beginning/End-of-Macro position before a
811;; change, when a macro straddles, respectively, the BEG or END (or both) of
812;; the change region. Otherwise these have the values BEG/END.
813(defvar c-old-BOM 0)
814(make-variable-buffer-local 'c-old-BOM)
815(defvar c-old-EOM 0)
816(make-variable-buffer-local 'c-old-EOM)
817
818(defun c-extend-region-for-CPP (beg end)
f8016ed6
AM
819 ;; Set c-old-BOM or c-old-EOM respectively to BEG, END, each extended to the
820 ;; beginning/end of any preprocessor construct they may be in.
1d1e4868
AM
821 ;;
822 ;; Point is undefined both before and after this function call; the buffer
823 ;; has already been widened, and match-data saved. The return value is
824 ;; meaningless.
825 ;;
dd969a56
AM
826 ;; This function is in the C/C++/ObjC values of
827 ;; `c-get-state-before-change-functions' and is called exclusively as a
1d1e4868
AM
828 ;; before change function.
829 (goto-char beg)
830 (c-beginning-of-macro)
831 (setq c-old-BOM (point))
832
833 (goto-char end)
3d14bb73
AM
834 (when (c-beginning-of-macro)
835 (c-end-of-macro)
836 (or (eobp) (forward-char))) ; Over the terminating NL which may be marked
837 ; with a c-cpp-delimiter category property
f8016ed6 838 (setq c-old-EOM (point)))
1d1e4868
AM
839
840(defun c-neutralize-CPP-line (beg end)
c5fcadd1
AM
841 ;; BEG and END bound a region, typically a preprocessor line. Put a
842 ;; "punctuation" syntax-table property on syntactically obtrusive
843 ;; characters, ones which would interact syntactically with stuff outside
844 ;; this region.
1d1e4868
AM
845 ;;
846 ;; These are unmatched string delimiters, or unmatched
847 ;; parens/brackets/braces. An unclosed comment is regarded as valid, NOT
848 ;; obtrusive.
c5fcadd1
AM
849 (save-excursion
850 (let (s)
851 (while
852 (progn
853 (setq s (parse-partial-sexp beg end -1))
854 (cond
855 ((< (nth 0 s) 0) ; found an unmated ),},]
856 (c-put-char-property (1- (point)) 'syntax-table '(1))
857 t)
858 ((nth 3 s) ; In a string
859 (c-put-char-property (nth 8 s) 'syntax-table '(1))
860 t)
861 ((> (nth 0 s) 0) ; In a (,{,[
862 (c-put-char-property (nth 1 s) 'syntax-table '(1))
863 t)
864 (t nil)))))))
1d1e4868 865
0ec1d2c5 866(defun c-neutralize-syntax-in-and-mark-CPP (begg endd old-len)
a0526d5e
AM
867 ;; (i) Extend the font lock region to cover all changed preprocessor
868 ;; regions; it does this by setting the variables `c-new-BEG' and
869 ;; `c-new-END' to the new boundaries.
870 ;;
871 ;; (ii) "Neutralize" every preprocessor line wholly or partially in the
872 ;; extended changed region. "Restore" lines which were CPP lines before the
873 ;; change and are no longer so; these can be located from the Buffer local
874 ;; variables `c-old-BOM' and `c-old-EOM'.
0ec1d2c5
AM
875 ;;
876 ;; (iii) Mark every CPP construct by placing a `category' property value
877 ;; `c-cpp-delimiter' at its start and end. The marked characters are the
878 ;; opening # and usually the terminating EOL, but sometimes the character
879 ;; before a comment/string delimiter.
880 ;;
1d1e4868
AM
881 ;; That is, set syntax-table properties on characters that would otherwise
882 ;; interact syntactically with those outside the CPP line(s).
0ec1d2c5 883 ;;
1d1e4868
AM
884 ;; This function is called from an after-change function, BEGG ENDD and
885 ;; OLD-LEN being the standard parameters. It prepares the buffer for font
886 ;; locking, hence must get called before `font-lock-after-change-function'.
887 ;;
888 ;; Point is undefined both before and after this function call, the buffer
889 ;; has been widened, and match-data saved. The return value is ignored.
890 ;;
891 ;; This function is the C/C++/ObjC value of `c-before-font-lock-function'.
892 ;;
c5fcadd1 893 ;; Note: SPEED _MATTERS_ IN THIS FUNCTION!!!
0ec1d2c5 894 ;;
c5fcadd1 895 ;; This function might make hidden buffer changes.
0ec1d2c5 896 (c-save-buffer-state (limits)
a0526d5e
AM
897 ;; First determine the region, (c-new-BEG c-new-END), which will get font
898 ;; locked. It might need "neutralizing". This region may not start
899 ;; inside a string, comment, or macro.
c5fcadd1 900 (goto-char c-old-BOM) ; already set to old start of macro or begg.
a0526d5e 901 (setq c-new-BEG
43a91810
AM
902 (min c-new-BEG
903 (if (setq limits (c-state-literal-at (point)))
904 (cdr limits) ; go forward out of any string or comment.
905 (point))))
c5fcadd1 906
1d1e4868 907 (goto-char endd)
0ec1d2c5 908 (if (setq limits (c-state-literal-at (point)))
c5fcadd1
AM
909 (goto-char (car limits))) ; go backward out of any string or comment.
910 (if (c-beginning-of-macro)
911 (c-end-of-macro))
43a91810
AM
912 (setq c-new-END (max c-new-END
913 (+ (- c-old-EOM old-len) (- endd begg))
914 (point)))
c5fcadd1 915
0ec1d2c5 916 ;; Clear all old relevant properties.
a0526d5e 917 (c-clear-char-property-with-value c-new-BEG c-new-END 'syntax-table '(1))
0ec1d2c5
AM
918 (c-clear-char-property-with-value c-new-BEG c-new-END 'category 'c-cpp-delimiter)
919 ;; FIXME!!! What about the "<" and ">" category properties? 2009-11-16
1d1e4868 920
a0526d5e
AM
921 ;; Add needed properties to each CPP construct in the region.
922 (goto-char c-new-BEG)
0ec1d2c5 923 (let ((pps-position c-new-BEG) pps-state mbeg)
a0526d5e
AM
924 (while (and (< (point) c-new-END)
925 (search-forward-regexp c-anchored-cpp-prefix c-new-END t))
c5fcadd1
AM
926 ;; If we've found a "#" inside a string/comment, ignore it.
927 (setq pps-state
928 (parse-partial-sexp pps-position (point) nil nil pps-state)
929 pps-position (point))
930 (unless (or (nth 3 pps-state) ; in a string?
931 (nth 4 pps-state)) ; in a comment?
0ec1d2c5
AM
932 (goto-char (match-beginning 0))
933 (setq mbeg (point))
934 (if (> (c-syntactic-end-of-macro) mbeg)
935 (progn
936 (c-neutralize-CPP-line mbeg (point))
937 (c-set-cpp-delimiters mbeg (point))
938 ;(setq pps-position (point))
939 )
940 (forward-line)) ; no infinite loop with, e.g., "#//"
941 )))))
1d1e4868
AM
942
943(defun c-before-change (beg end)
dd969a56
AM
944 ;; Function to be put on `before-change-functions'. Primarily, this calls
945 ;; the language dependent `c-get-state-before-change-functions'. It is
1d1e4868
AM
946 ;; otherwise used only to remove stale entries from the `c-found-types'
947 ;; cache, and to record entries which a `c-after-change' function might
948 ;; confirm as stale.
0ec1d2c5 949 ;;
1d1e4868
AM
950 ;; Note that this function must be FAST rather than accurate. Note
951 ;; also that it only has any effect when font locking is enabled.
952 ;; We exploit this by checking for font-lock-*-face instead of doing
953 ;; rigourous syntactic analysis.
954
955 ;; If either change boundary is wholly inside an identifier, delete
956 ;; it/them from the cache. Don't worry about being inside a string
957 ;; or a comment - "wrongly" removing a symbol from `c-found-types'
958 ;; isn't critical.
959 (setq c-maybe-stale-found-type nil)
960 (save-restriction
961 (save-match-data
962 (widen)
963 (save-excursion
964 ;; Are we inserting/deleting stuff in the middle of an identifier?
965 (c-unfind-enclosing-token beg)
966 (c-unfind-enclosing-token end)
967 ;; Are we coalescing two tokens together, e.g. "fo o" -> "foo"?
968 (when (< beg end)
969 (c-unfind-coalesced-tokens beg end))
970 ;; Are we (potentially) disrupting the syntactic context which
971 ;; makes a type a type? E.g. by inserting stuff after "foo" in
972 ;; "foo bar;", or before "foo" in "typedef foo *bar;"?
973 ;;
974 ;; We search for appropriate c-type properties "near" the change.
975 ;; First, find an appropriate boundary for this property search.
976 (let (lim
977 type type-pos
978 marked-id term-pos
979 (end1
980 (or (and (eq (get-text-property end 'face) 'font-lock-comment-face)
981 (previous-single-property-change end 'face))
982 end)))
983 (when (>= end1 beg) ; Don't hassle about changes entirely in comments.
984 ;; Find a limit for the search for a `c-type' property
985 (while
986 (and (/= (skip-chars-backward "^;{}") 0)
987 (> (point) (point-min))
988 (memq (c-get-char-property (1- (point)) 'face)
989 '(font-lock-comment-face font-lock-string-face))))
990 (setq lim (max (point-min) (1- (point))))
991
992 ;; Look for the latest `c-type' property before end1
993 (when (and (> end1 (point-min))
994 (setq type-pos
995 (if (get-text-property (1- end1) 'c-type)
996 end1
997 (previous-single-property-change end1 'c-type nil lim))))
998 (setq type (get-text-property (max (1- type-pos) lim) 'c-type))
999
1000 (when (memq type '(c-decl-id-start c-decl-type-start))
1001 ;; Get the identifier, if any, that the property is on.
1002 (goto-char (1- type-pos))
1003 (setq marked-id
1004 (when (looking-at "\\(\\sw\\|\\s_\\)")
1005 (c-beginning-of-current-token)
1006 (buffer-substring-no-properties (point) type-pos)))
1007
1008 (goto-char end1)
1009 (skip-chars-forward "^;{}") ; FIXME!!! loop for comment, maybe
1010 (setq lim (point))
1011 (setq term-pos
1012 (or (next-single-property-change end 'c-type nil lim) lim))
1013 (setq c-maybe-stale-found-type
1014 (list type marked-id
1015 type-pos term-pos
1016 (buffer-substring-no-properties type-pos term-pos)
1017 (buffer-substring-no-properties beg end)))))))
1018
dd969a56
AM
1019 (if c-get-state-before-change-functions
1020 (mapc (lambda (fn)
1021 (funcall fn beg end))
1022 c-get-state-before-change-functions))
1d1e4868
AM
1023 ))))
1024
1025(defun c-after-change (beg end old-len)
1026 ;; Function put on `after-change-functions' to adjust various caches
1027 ;; etc. Prefer speed to finesse here, since there will be an order
1028 ;; of magnitude more calls to this function than any of the
1029 ;; functions that use the caches.
1030 ;;
1031 ;; Note that care must be taken so that this is called before any
1032 ;; font-lock callbacks since we might get calls to functions using
1033 ;; these caches from inside them, and we must thus be sure that this
1034 ;; has already been executed.
1035 ;;
1036 ;; This calls the language variable c-before-font-lock-function, if non nil.
1037 ;; This typically sets `syntax-table' properties.
1038
1039 (c-save-buffer-state ()
1040 ;; When `combine-after-change-calls' is used we might get calls
1041 ;; with regions outside the current narrowing. This has been
1042 ;; observed in Emacs 20.7.
1043 (save-restriction
1044 (save-match-data ; c-recognize-<>-arglists changes match-data
1045 (widen)
1046
1047 (when (> end (point-max))
1048 ;; Some emacsen might return positions past the end. This has been
1049 ;; observed in Emacs 20.7 when rereading a buffer changed on disk
1050 ;; (haven't been able to minimize it, but Emacs 21.3 appears to
1051 ;; work).
1052 (setq end (point-max))
1053 (when (> beg end)
1054 (setq beg end)))
1055
8a249abc
AM
1056 ;; C-y is capable of spuriously converting category properties
1057 ;; c-</>-as-paren-syntax into hard syntax-table properties. Remove
1058 ;; these when it happens.
1059 (c-clear-char-property-with-value beg end 'syntax-table
1060 c-<-as-paren-syntax)
1061 (c-clear-char-property-with-value beg end 'syntax-table
1062 c->-as-paren-syntax)
1063
1d1e4868
AM
1064 (c-trim-found-types beg end old-len) ; maybe we don't need all of these.
1065 (c-invalidate-sws-region-after beg end)
1066 (c-invalidate-state-cache beg)
1067 (c-invalidate-find-decl-cache beg)
1068
1069 (when c-recognize-<>-arglists
1070 (c-after-change-check-<>-operators beg end))
1071
ba7fdf30
AM
1072 ;; (c-new-BEG c-new-END) will be the region to fontify. It may become
1073 ;; larger than (beg end).
1074 (setq c-new-BEG beg
1075 c-new-END end)
1d1e4868
AM
1076 (if c-before-font-lock-function
1077 (save-excursion
1078 (funcall c-before-font-lock-function beg end old-len)))))))
1079
1080(defun c-after-font-lock-init ()
1081 ;; Put on `font-lock-mode-hook'.
1082 (remove-hook 'after-change-functions 'c-after-change t)
1083 (add-hook 'after-change-functions 'c-after-change nil t))
1084
1085(defun c-font-lock-init ()
1086 "Set up the font-lock variables for using the font-lock support in CC Mode.
1087This does not load the font-lock package. Use after
1088`c-basic-common-init' and after cc-fonts has been loaded."
1089
175069ef 1090 (set (make-local-variable 'font-lock-defaults)
1d1e4868
AM
1091 `(,(if (c-major-mode-is 'awk-mode)
1092 ;; awk-mode currently has only one font lock level.
1093 'awk-font-lock-keywords
1094 (mapcar 'c-mode-symbol
1095 '("font-lock-keywords" "font-lock-keywords-1"
1096 "font-lock-keywords-2" "font-lock-keywords-3")))
1097 nil nil
1098 ,c-identifier-syntax-modifications
1099 c-beginning-of-syntax
1d1e4868
AM
1100 (font-lock-mark-block-function
1101 . c-mark-function)))
922ad43e
GM
1102 (if (featurep 'xemacs)
1103 (make-local-hook 'font-lock-mode-hook))
1d1e4868
AM
1104 (add-hook 'font-lock-mode-hook 'c-after-font-lock-init nil t))
1105
8714fc18
AM
1106(defun c-extend-after-change-region (beg end old-len)
1107 "Extend the region to be fontified, if necessary."
1108 ;; Note: the parameters are ignored here. This somewhat indirect
1109 ;; implementation exists because it is minimally different from the
1110 ;; stand-alone CC Mode which, lacking
1111 ;; font-lock-extend-after-change-region-function, is forced to use advice
1112 ;; instead.
1113 ;;
a0526d5e
AM
1114 ;; Of the seven CC Mode languages, currently (2009-05) only C, C++, Objc
1115 ;; (the languages with #define) and AWK Mode make non-null use of this
1116 ;; function.
8714fc18 1117 (cons c-new-BEG c-new-END))
1d1e4868
AM
1118
1119\f
130c507e
GM
1120;; Support for C
1121
d9e94c22
MS
1122;;;###autoload
1123(defvar c-mode-syntax-table nil
1124 "Syntax table used in c-mode buffers.")
1125(or c-mode-syntax-table
1126 (setq c-mode-syntax-table
1127 (funcall (c-lang-const c-make-mode-syntax-table c))))
1128
130c507e
GM
1129(defvar c-mode-abbrev-table nil
1130 "Abbreviation table used in c-mode buffers.")
a66cd3ee
MS
1131(c-define-abbrev-table 'c-mode-abbrev-table
1132 '(("else" "else" c-electric-continued-statement 0)
1133 ("while" "while" c-electric-continued-statement 0)))
130c507e
GM
1134
1135(defvar c-mode-map ()
1136 "Keymap used in c-mode buffers.")
1137(if c-mode-map
1138 nil
1139 (setq c-mode-map (c-make-inherited-keymap))
1140 ;; add bindings which are only useful for C
1141 (define-key c-mode-map "\C-c\C-e" 'c-macro-expand)
1142 )
1143
1144(easy-menu-define c-c-menu c-mode-map "C Mode Commands"
d9e94c22
MS
1145 (cons "C" (c-lang-const c-mode-menu c)))
1146
1147;; In XEmacs >= 21.5 modes should add their own entries to
1148;; `auto-mode-alist'. The comment form of autoload is used to avoid
1149;; doing this on load. That since `add-to-list' prepends the value
1150;; which could cause it to clobber user settings. Later emacsen have
1151;; an append option, but it's not safe to use.
50fdde0e 1152
38477bef
AM
1153;; The extension ".C" is associated with C++ while the lowercase
1154;; variant goes with C. On case insensitive file systems, this means
50fdde0e
MS
1155;; that ".c" files also might open C++ mode if the C++ entry comes
1156;; first on `auto-mode-alist'. Thus we try to ensure that ".C" comes
1157;; after ".c", and since `add-to-list' adds the entry first we have to
1158;; add the ".C" entry first.
1159;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(cc\\|hh\\)\\'" . c++-mode))
1160;;;###autoload (add-to-list 'auto-mode-alist '("\\.[ch]\\(pp\\|xx\\|\\+\\+\\)\\'" . c++-mode))
1161;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(CC?\\|HH?\\)\\'" . c++-mode))
1162
d9e94c22
MS
1163;;;###autoload (add-to-list 'auto-mode-alist '("\\.[ch]\\'" . c-mode))
1164
1165;; NB: The following two associate yacc and lex files to C Mode, which
1166;; is not really suitable for those formats. Anyway, afaik there's
1167;; currently no better mode for them, and besides this is legacy.
1168;;;###autoload (add-to-list 'auto-mode-alist '("\\.y\\(acc\\)?\\'" . c-mode))
1169;;;###autoload (add-to-list 'auto-mode-alist '("\\.lex\\'" . c-mode))
1170
f63b48bb
DN
1171;; Preprocessed files generated by C and C++ compilers.
1172;;;###autoload (add-to-list 'auto-mode-alist '("\\.i\\'" . c-mode))
1173;;;###autoload (add-to-list 'auto-mode-alist '("\\.ii\\'" . c++-mode))
1174
1175
785eecbb
RS
1176;;;###autoload
1177(defun c-mode ()
1178 "Major mode for editing K&R and ANSI C code.
1179To submit a problem report, enter `\\[c-submit-bug-report]' from a
1180c-mode buffer. This automatically sets up a mail buffer with version
1181information already added. You just need to add a description of the
50fdde0e 1182problem, including a reproducible test case, and send the message.
785eecbb
RS
1183
1184To see what version of CC Mode you are running, enter `\\[c-version]'.
1185
d9e94c22
MS
1186The hook `c-mode-common-hook' is run with no args at mode
1187initialization, then `c-mode-hook'.
785eecbb
RS
1188
1189Key bindings:
1190\\{c-mode-map}"
1191 (interactive)
785eecbb 1192 (kill-all-local-variables)
d9e94c22 1193 (c-initialize-cc-mode t)
785eecbb 1194 (set-syntax-table c-mode-syntax-table)
175069ef 1195 (setq major-mode 'c-mode ; FIXME: Use define-derived-mode.
785eecbb 1196 mode-name "C"
130c507e
GM
1197 local-abbrev-table c-mode-abbrev-table
1198 abbrev-mode t)
785eecbb 1199 (use-local-map c-mode-map)
28d88c16 1200 (c-init-language-vars-for 'c-mode)
a66cd3ee 1201 (c-common-init 'c-mode)
d9e94c22 1202 (easy-menu-add c-c-menu)
51f606de 1203 (cc-imenu-init cc-imenu-c-generic-expression)
3efc2cd7 1204 (c-run-mode-hooks 'c-mode-common-hook 'c-mode-hook)
785eecbb
RS
1205 (c-update-modeline))
1206
1207\f
130c507e
GM
1208;; Support for C++
1209
d9e94c22
MS
1210;;;###autoload
1211(defvar c++-mode-syntax-table nil
1212 "Syntax table used in c++-mode buffers.")
1213(or c++-mode-syntax-table
1214 (setq c++-mode-syntax-table
1215 (funcall (c-lang-const c-make-mode-syntax-table c++))))
1216
130c507e
GM
1217(defvar c++-mode-abbrev-table nil
1218 "Abbreviation table used in c++-mode buffers.")
a66cd3ee
MS
1219(c-define-abbrev-table 'c++-mode-abbrev-table
1220 '(("else" "else" c-electric-continued-statement 0)
1221 ("while" "while" c-electric-continued-statement 0)
1222 ("catch" "catch" c-electric-continued-statement 0)))
130c507e
GM
1223
1224(defvar c++-mode-map ()
1225 "Keymap used in c++-mode buffers.")
1226(if c++-mode-map
1227 nil
1228 (setq c++-mode-map (c-make-inherited-keymap))
1229 ;; add bindings which are only useful for C++
1230 (define-key c++-mode-map "\C-c\C-e" 'c-macro-expand)
1231 (define-key c++-mode-map "\C-c:" 'c-scope-operator)
1232 (define-key c++-mode-map "<" 'c-electric-lt-gt)
1233 (define-key c++-mode-map ">" 'c-electric-lt-gt))
1234
1235(easy-menu-define c-c++-menu c++-mode-map "C++ Mode Commands"
d9e94c22
MS
1236 (cons "C++" (c-lang-const c-mode-menu c++)))
1237
785eecbb
RS
1238;;;###autoload
1239(defun c++-mode ()
1240 "Major mode for editing C++ code.
1241To submit a problem report, enter `\\[c-submit-bug-report]' from a
1242c++-mode buffer. This automatically sets up a mail buffer with
1243version information already added. You just need to add a description
1244of the problem, including a reproducible test case, and send the
1245message.
1246
1247To see what version of CC Mode you are running, enter `\\[c-version]'.
1248
d9e94c22
MS
1249The hook `c-mode-common-hook' is run with no args at mode
1250initialization, then `c++-mode-hook'.
785eecbb
RS
1251
1252Key bindings:
1253\\{c++-mode-map}"
1254 (interactive)
785eecbb 1255 (kill-all-local-variables)
d9e94c22 1256 (c-initialize-cc-mode t)
785eecbb 1257 (set-syntax-table c++-mode-syntax-table)
175069ef 1258 (setq major-mode 'c++-mode ; FIXME: Use define-derived-mode.
785eecbb 1259 mode-name "C++"
130c507e
GM
1260 local-abbrev-table c++-mode-abbrev-table
1261 abbrev-mode t)
785eecbb 1262 (use-local-map c++-mode-map)
28d88c16 1263 (c-init-language-vars-for 'c++-mode)
a66cd3ee 1264 (c-common-init 'c++-mode)
d9e94c22 1265 (easy-menu-add c-c++-menu)
51f606de 1266 (cc-imenu-init cc-imenu-c++-generic-expression)
3efc2cd7 1267 (c-run-mode-hooks 'c-mode-common-hook 'c++-mode-hook)
785eecbb
RS
1268 (c-update-modeline))
1269
1270\f
130c507e
GM
1271;; Support for Objective-C
1272
d9e94c22
MS
1273;;;###autoload
1274(defvar objc-mode-syntax-table nil
1275 "Syntax table used in objc-mode buffers.")
1276(or objc-mode-syntax-table
1277 (setq objc-mode-syntax-table
1278 (funcall (c-lang-const c-make-mode-syntax-table objc))))
1279
130c507e
GM
1280(defvar objc-mode-abbrev-table nil
1281 "Abbreviation table used in objc-mode buffers.")
a66cd3ee
MS
1282(c-define-abbrev-table 'objc-mode-abbrev-table
1283 '(("else" "else" c-electric-continued-statement 0)
1284 ("while" "while" c-electric-continued-statement 0)))
130c507e
GM
1285
1286(defvar objc-mode-map ()
1287 "Keymap used in objc-mode buffers.")
1288(if objc-mode-map
1289 nil
1290 (setq objc-mode-map (c-make-inherited-keymap))
1291 ;; add bindings which are only useful for Objective-C
1292 (define-key objc-mode-map "\C-c\C-e" 'c-macro-expand))
1293
1294(easy-menu-define c-objc-menu objc-mode-map "ObjC Mode Commands"
d9e94c22
MS
1295 (cons "ObjC" (c-lang-const c-mode-menu objc)))
1296
1297;;;###autoload (add-to-list 'auto-mode-alist '("\\.m\\'" . objc-mode))
130c507e 1298
785eecbb
RS
1299;;;###autoload
1300(defun objc-mode ()
1301 "Major mode for editing Objective C code.
1302To submit a problem report, enter `\\[c-submit-bug-report]' from an
1303objc-mode buffer. This automatically sets up a mail buffer with
1304version information already added. You just need to add a description
1305of the problem, including a reproducible test case, and send the
1306message.
1307
1308To see what version of CC Mode you are running, enter `\\[c-version]'.
1309
d9e94c22
MS
1310The hook `c-mode-common-hook' is run with no args at mode
1311initialization, then `objc-mode-hook'.
785eecbb
RS
1312
1313Key bindings:
1314\\{objc-mode-map}"
1315 (interactive)
785eecbb 1316 (kill-all-local-variables)
d9e94c22 1317 (c-initialize-cc-mode t)
785eecbb 1318 (set-syntax-table objc-mode-syntax-table)
175069ef 1319 (setq major-mode 'objc-mode ; FIXME: Use define-derived-mode.
785eecbb 1320 mode-name "ObjC"
130c507e
GM
1321 local-abbrev-table objc-mode-abbrev-table
1322 abbrev-mode t)
785eecbb 1323 (use-local-map objc-mode-map)
28d88c16 1324 (c-init-language-vars-for 'objc-mode)
a66cd3ee 1325 (c-common-init 'objc-mode)
d9e94c22
MS
1326 (easy-menu-add c-objc-menu)
1327 (cc-imenu-init nil 'cc-imenu-objc-function)
3efc2cd7 1328 (c-run-mode-hooks 'c-mode-common-hook 'objc-mode-hook)
785eecbb
RS
1329 (c-update-modeline))
1330
1331\f
130c507e
GM
1332;; Support for Java
1333
d9e94c22
MS
1334;;;###autoload
1335(defvar java-mode-syntax-table nil
1336 "Syntax table used in java-mode buffers.")
1337(or java-mode-syntax-table
1338 (setq java-mode-syntax-table
1339 (funcall (c-lang-const c-make-mode-syntax-table java))))
1340
130c507e
GM
1341(defvar java-mode-abbrev-table nil
1342 "Abbreviation table used in java-mode buffers.")
a66cd3ee
MS
1343(c-define-abbrev-table 'java-mode-abbrev-table
1344 '(("else" "else" c-electric-continued-statement 0)
1345 ("while" "while" c-electric-continued-statement 0)
1346 ("catch" "catch" c-electric-continued-statement 0)
1347 ("finally" "finally" c-electric-continued-statement 0)))
130c507e
GM
1348
1349(defvar java-mode-map ()
1350 "Keymap used in java-mode buffers.")
1351(if java-mode-map
1352 nil
1353 (setq java-mode-map (c-make-inherited-keymap))
1354 ;; add bindings which are only useful for Java
1355 )
1356
d9e94c22
MS
1357;; Regexp trying to describe the beginning of a Java top-level
1358;; definition. This is not used by CC Mode, nor is it maintained
1359;; since it's practically impossible to write a regexp that reliably
1360;; matches such a construct. Other tools are necessary.
1361(defconst c-Java-defun-prompt-regexp
1362 "^[ \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-*")
1363
130c507e 1364(easy-menu-define c-java-menu java-mode-map "Java Mode Commands"
d9e94c22
MS
1365 (cons "Java" (c-lang-const c-mode-menu java)))
1366
1367;;;###autoload (add-to-list 'auto-mode-alist '("\\.java\\'" . java-mode))
130c507e 1368
785eecbb
RS
1369;;;###autoload
1370(defun java-mode ()
1371 "Major mode for editing Java code.
dc2b4c5f 1372To submit a problem report, enter `\\[c-submit-bug-report]' from a
785eecbb
RS
1373java-mode buffer. This automatically sets up a mail buffer with
1374version information already added. You just need to add a description
50fdde0e 1375of the problem, including a reproducible test case, and send the
785eecbb
RS
1376message.
1377
1378To see what version of CC Mode you are running, enter `\\[c-version]'.
1379
d9e94c22
MS
1380The hook `c-mode-common-hook' is run with no args at mode
1381initialization, then `java-mode-hook'.
785eecbb
RS
1382
1383Key bindings:
1384\\{java-mode-map}"
1385 (interactive)
785eecbb 1386 (kill-all-local-variables)
d9e94c22 1387 (c-initialize-cc-mode t)
785eecbb 1388 (set-syntax-table java-mode-syntax-table)
175069ef 1389 (setq major-mode 'java-mode ; FIXME: Use define-derived-mode.
785eecbb 1390 mode-name "Java"
91b807c9 1391 local-abbrev-table java-mode-abbrev-table
a66cd3ee 1392 abbrev-mode t)
785eecbb 1393 (use-local-map java-mode-map)
28d88c16 1394 (c-init-language-vars-for 'java-mode)
a66cd3ee 1395 (c-common-init 'java-mode)
d9e94c22 1396 (easy-menu-add c-java-menu)
51f606de 1397 (cc-imenu-init cc-imenu-java-generic-expression)
3efc2cd7 1398 (c-run-mode-hooks 'c-mode-common-hook 'java-mode-hook)
785eecbb
RS
1399 (c-update-modeline))
1400
1401\f
130c507e
GM
1402;; Support for CORBA's IDL language
1403
d9e94c22
MS
1404;;;###autoload
1405(defvar idl-mode-syntax-table nil
1406 "Syntax table used in idl-mode buffers.")
1407(or idl-mode-syntax-table
1408 (setq idl-mode-syntax-table
1409 (funcall (c-lang-const c-make-mode-syntax-table idl))))
1410
130c507e
GM
1411(defvar idl-mode-abbrev-table nil
1412 "Abbreviation table used in idl-mode buffers.")
a66cd3ee 1413(c-define-abbrev-table 'idl-mode-abbrev-table nil)
130c507e
GM
1414
1415(defvar idl-mode-map ()
1416 "Keymap used in idl-mode buffers.")
1417(if idl-mode-map
1418 nil
1419 (setq idl-mode-map (c-make-inherited-keymap))
1420 ;; add bindings which are only useful for IDL
1421 )
1422
1423(easy-menu-define c-idl-menu idl-mode-map "IDL Mode Commands"
d9e94c22
MS
1424 (cons "IDL" (c-lang-const c-mode-menu idl)))
1425
1426;;;###autoload (add-to-list 'auto-mode-alist '("\\.idl\\'" . idl-mode))
130c507e 1427
aac90c52
RS
1428;;;###autoload
1429(defun idl-mode ()
d9e94c22 1430 "Major mode for editing CORBA's IDL, PSDL and CIDL code.
aac90c52
RS
1431To submit a problem report, enter `\\[c-submit-bug-report]' from an
1432idl-mode buffer. This automatically sets up a mail buffer with
1433version information already added. You just need to add a description
1434of the problem, including a reproducible test case, and send the
1435message.
1436
1437To see what version of CC Mode you are running, enter `\\[c-version]'.
1438
d9e94c22
MS
1439The hook `c-mode-common-hook' is run with no args at mode
1440initialization, then `idl-mode-hook'.
aac90c52
RS
1441
1442Key bindings:
1443\\{idl-mode-map}"
1444 (interactive)
aac90c52 1445 (kill-all-local-variables)
d9e94c22 1446 (c-initialize-cc-mode t)
aac90c52 1447 (set-syntax-table idl-mode-syntax-table)
175069ef 1448 (setq major-mode 'idl-mode ; FIXME: Use define-derived-mode.
aac90c52
RS
1449 mode-name "IDL"
1450 local-abbrev-table idl-mode-abbrev-table)
1451 (use-local-map idl-mode-map)
28d88c16 1452 (c-init-language-vars-for 'idl-mode)
a66cd3ee 1453 (c-common-init 'idl-mode)
d9e94c22
MS
1454 (easy-menu-add c-idl-menu)
1455 ;;(cc-imenu-init cc-imenu-idl-generic-expression) ;TODO
3efc2cd7 1456 (c-run-mode-hooks 'c-mode-common-hook 'idl-mode-hook)
aac90c52
RS
1457 (c-update-modeline))
1458
1459\f
130c507e
GM
1460;; Support for Pike
1461
d9e94c22
MS
1462;;;###autoload
1463(defvar pike-mode-syntax-table nil
1464 "Syntax table used in pike-mode buffers.")
1465(or pike-mode-syntax-table
1466 (setq pike-mode-syntax-table
1467 (funcall (c-lang-const c-make-mode-syntax-table pike))))
1468
130c507e
GM
1469(defvar pike-mode-abbrev-table nil
1470 "Abbreviation table used in pike-mode buffers.")
a66cd3ee
MS
1471(c-define-abbrev-table 'pike-mode-abbrev-table
1472 '(("else" "else" c-electric-continued-statement 0)
1473 ("while" "while" c-electric-continued-statement 0)))
130c507e
GM
1474
1475(defvar pike-mode-map ()
1476 "Keymap used in pike-mode buffers.")
1477(if pike-mode-map
1478 nil
1479 (setq pike-mode-map (c-make-inherited-keymap))
1480 ;; additional bindings
1481 (define-key pike-mode-map "\C-c\C-e" 'c-macro-expand))
1482
1483(easy-menu-define c-pike-menu pike-mode-map "Pike Mode Commands"
d9e94c22
MS
1484 (cons "Pike" (c-lang-const c-mode-menu pike)))
1485
50fdde0e
MS
1486;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(u?lpc\\|pike\\|pmod\\(.in\\)?\\)\\'" . pike-mode))
1487;;;###autoload (add-to-list 'interpreter-mode-alist '("pike" . pike-mode))
130c507e 1488
0ec8351b
BW
1489;;;###autoload
1490(defun pike-mode ()
1491 "Major mode for editing Pike code.
a66cd3ee
MS
1492To submit a problem report, enter `\\[c-submit-bug-report]' from a
1493pike-mode buffer. This automatically sets up a mail buffer with
0ec8351b
BW
1494version information already added. You just need to add a description
1495of the problem, including a reproducible test case, and send the
1496message.
1497
1498To see what version of CC Mode you are running, enter `\\[c-version]'.
1499
d9e94c22
MS
1500The hook `c-mode-common-hook' is run with no args at mode
1501initialization, then `pike-mode-hook'.
0ec8351b
BW
1502
1503Key bindings:
1504\\{pike-mode-map}"
1505 (interactive)
0ec8351b 1506 (kill-all-local-variables)
d9e94c22 1507 (c-initialize-cc-mode t)
0ec8351b 1508 (set-syntax-table pike-mode-syntax-table)
175069ef 1509 (setq major-mode 'pike-mode ; FIXME: Use define-derived-mode.
0ec8351b 1510 mode-name "Pike"
130c507e 1511 local-abbrev-table pike-mode-abbrev-table
a66cd3ee 1512 abbrev-mode t)
0ec8351b 1513 (use-local-map pike-mode-map)
28d88c16 1514 (c-init-language-vars-for 'pike-mode)
a66cd3ee 1515 (c-common-init 'pike-mode)
d9e94c22
MS
1516 (easy-menu-add c-pike-menu)
1517 ;;(cc-imenu-init cc-imenu-pike-generic-expression) ;TODO
3efc2cd7 1518 (c-run-mode-hooks 'c-mode-common-hook 'pike-mode-hook)
0ec8351b
BW
1519 (c-update-modeline))
1520
1521\f
0386b551 1522;; Support for AWK
d9e94c22 1523
50fdde0e
MS
1524;;;###autoload (add-to-list 'auto-mode-alist '("\\.awk\\'" . awk-mode))
1525;;;###autoload (add-to-list 'interpreter-mode-alist '("awk" . awk-mode))
1526;;;###autoload (add-to-list 'interpreter-mode-alist '("mawk" . awk-mode))
1527;;;###autoload (add-to-list 'interpreter-mode-alist '("nawk" . awk-mode))
1528;;;###autoload (add-to-list 'interpreter-mode-alist '("gawk" . awk-mode))
d9e94c22 1529
50fdde0e
MS
1530;;; Autoload directives must be on the top level, so we construct an
1531;;; autoload form instead.
791887d9 1532;;;###autoload (autoload 'awk-mode "cc-mode" "Major mode for editing AWK code." t)
50fdde0e 1533
0386b551
AM
1534(defvar awk-mode-abbrev-table nil
1535 "Abbreviation table used in awk-mode buffers.")
1536(c-define-abbrev-table 'awk-mode-abbrev-table
1537 '(("else" "else" c-electric-continued-statement 0)
1538 ("while" "while" c-electric-continued-statement 0)))
1539
1540(defvar awk-mode-map ()
1541 "Keymap used in awk-mode buffers.")
1542(if awk-mode-map
1543 nil
1544 (setq awk-mode-map (c-make-inherited-keymap))
1545 ;; add bindings which are only useful for awk.
1546 (define-key awk-mode-map "#" 'self-insert-command)
1547 (define-key awk-mode-map "/" 'self-insert-command)
1548 (define-key awk-mode-map "*" 'self-insert-command)
1549 (define-key awk-mode-map "\C-c\C-n" 'undefined) ; #if doesn't exist in awk.
1550 (define-key awk-mode-map "\C-c\C-p" 'undefined)
1551 (define-key awk-mode-map "\C-c\C-u" 'undefined)
1552 (define-key awk-mode-map "\M-a" 'c-beginning-of-statement) ; 2003/10/7
1553 (define-key awk-mode-map "\M-e" 'c-end-of-statement) ; 2003/10/7
1554 (define-key awk-mode-map "\C-\M-a" 'c-awk-beginning-of-defun)
1555 (define-key awk-mode-map "\C-\M-e" 'c-awk-end-of-defun))
1556
1557(easy-menu-define c-awk-menu awk-mode-map "AWK Mode Commands"
1558 (cons "AWK" (c-lang-const c-mode-menu awk)))
1559
ae500c1a
GM
1560;; (require 'cc-awk) brings these in.
1561(defvar awk-mode-syntax-table)
1562(declare-function c-awk-unstick-NL-prop "cc-awk" ())
1563
0386b551
AM
1564(defun awk-mode ()
1565 "Major mode for editing AWK code.
d9e94c22
MS
1566To submit a problem report, enter `\\[c-submit-bug-report]' from an
1567awk-mode buffer. This automatically sets up a mail buffer with version
1568information already added. You just need to add a description of the
50fdde0e 1569problem, including a reproducible test case, and send the message.
d9e94c22
MS
1570
1571To see what version of CC Mode you are running, enter `\\[c-version]'.
1572
1573The hook `c-mode-common-hook' is run with no args at mode
1574initialization, then `awk-mode-hook'.
1575
1576Key bindings:
1577\\{awk-mode-map}"
0386b551
AM
1578 (interactive)
1579 (require 'cc-awk) ; Added 2003/6/10.
1580 (kill-all-local-variables)
1581 (c-initialize-cc-mode t)
1582 (set-syntax-table awk-mode-syntax-table)
175069ef 1583 (setq major-mode 'awk-mode ; FIXME: Use define-derived-mode.
0386b551
AM
1584 mode-name "AWK"
1585 local-abbrev-table awk-mode-abbrev-table
1586 abbrev-mode t)
1587 (use-local-map awk-mode-map)
1588 (c-init-language-vars-for 'awk-mode)
1589 (c-common-init 'awk-mode)
0386b551 1590 (c-awk-unstick-NL-prop)
0386b551
AM
1591
1592 ;; Prevent Xemacs's buffer-syntactic-context being used. See the comment
1593 ;; in cc-engine.el, just before (defun c-fast-in-literal ...
1594 (defalias 'c-in-literal 'c-slow-in-literal)
1595
1596 (c-run-mode-hooks 'c-mode-common-hook 'awk-mode-hook)
1597 (c-update-modeline))
d9e94c22
MS
1598
1599\f
c2efc1d0 1600;; bug reporting
785eecbb
RS
1601
1602(defconst c-mode-help-address
a66cd3ee
MS
1603 "bug-cc-mode@gnu.org"
1604 "Address(es) for CC Mode bug reports.")
785eecbb
RS
1605
1606(defun c-version ()
1607 "Echo the current version of CC Mode in the minibuffer."
1608 (interactive)
1609 (message "Using CC Mode version %s" c-version)
1610 (c-keep-region-active))
1611
130c507e
GM
1612(defvar c-prepare-bug-report-hooks nil)
1613
1614;; Dynamic variables used by reporter.
1615(defvar reporter-prompt-for-summary-p)
1616(defvar reporter-dont-compact-list)
1617
785eecbb
RS
1618(defun c-submit-bug-report ()
1619 "Submit via mail a bug report on CC Mode."
1620 (interactive)
77e31a8d 1621 (require 'reporter)
785eecbb
RS
1622 ;; load in reporter
1623 (let ((reporter-prompt-for-summary-p t)
1624 (reporter-dont-compact-list '(c-offsets-alist))
1625 (style c-indentation-style)
785eecbb
RS
1626 (c-features c-emacs-features))
1627 (and
1628 (if (y-or-n-p "Do you want to submit a report on CC Mode? ")
1629 t (message "") nil)
785eecbb
RS
1630 (reporter-submit-bug-report
1631 c-mode-help-address
d9e94c22 1632 (concat "CC Mode " c-version " (" mode-name ")")
51f606de 1633 (let ((vars (append
51f606de 1634 c-style-variables
d9e94c22 1635 '(c-buffer-is-cc-mode
51f606de 1636 c-tab-always-indent
d9e94c22
MS
1637 c-syntactic-indentation
1638 c-syntactic-indentation-in-macros
1639 c-ignore-auto-fill
1640 c-auto-align-backslashes
1641 c-backspace-function
1642 c-delete-function
1643 c-electric-pound-behavior
1644 c-default-style
1645 c-enable-xemacs-performance-kludge-p
1646 c-old-style-variable-behavior
51f606de
GM
1647 defun-prompt-regexp
1648 tab-width
1649 comment-column
1650 parse-sexp-ignore-comments
d9e94c22
MS
1651 parse-sexp-lookup-properties
1652 lookup-syntax-properties
51f606de
GM
1653 ;; A brain-damaged XEmacs only variable that, if
1654 ;; set to nil can cause all kinds of chaos.
1655 signal-error-on-buffer-boundary
1656 ;; Variables that affect line breaking and comments.
d9e94c22 1657 auto-fill-mode
91b807c9 1658 auto-fill-function
51f606de
GM
1659 filladapt-mode
1660 comment-multi-line
1661 comment-start-skip
1662 fill-prefix
d9e94c22 1663 fill-column
51f606de
GM
1664 paragraph-start
1665 adaptive-fill-mode
1666 adaptive-fill-regexp)
1667 nil)))
d61ca8d5 1668 (mapc (lambda (var) (unless (boundp var)
175069ef 1669 (setq vars (delq var vars))))
d61ca8d5
GM
1670 '(signal-error-on-buffer-boundary
1671 filladapt-mode
1672 defun-prompt-regexp
1673 font-lock-mode
1674 font-lock-maximum-decoration
1675 parse-sexp-lookup-properties
1676 lookup-syntax-properties))
51f606de 1677 vars)
9714ec23 1678 (lambda ()
a66cd3ee 1679 (run-hooks 'c-prepare-bug-report-hooks)
d9e94c22 1680 (insert (format "Buffer Style: %s\nc-emacs-features: %s\n"
a66cd3ee 1681 style c-features)))))))
785eecbb
RS
1682
1683\f
130c507e 1684(cc-provide 'cc-mode)
ab5796a9 1685
785eecbb 1686;;; cc-mode.el ends here