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