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