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