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