Imenu support changed.
[bpt/emacs.git] / lisp / progmodes / cc-mode.el
CommitLineData
785eecbb
RS
1;;; cc-mode.el --- major mode for editing C, C++, Objective-C, and Java code
2
3;; Copyright (C) 1985,87,92,93,94,95,96,97 Free Software Foundation, Inc.
4
5;; Authors: 1992-1997 Barry A. Warsaw
6;; 1987 Dave Detlefs and Stewart Clamen
7;; 1985 Richard M. Stallman
8;; Maintainer: cc-mode-help@python.org
9;; Created: a long, long, time ago. adapted from the original c-mode.el
785eecbb
RS
10;; Keywords: c languages oop
11
c2efc1d0
KH
12(defconst c-version "5.19"
13 "CC Mode version number.")
14
785eecbb
RS
15;; NOTE: Read the commentary below for the right way to submit bug reports!
16;; NOTE: See the accompanying texinfo manual for details on using this mode!
17
18;; This file is part of GNU Emacs.
19
20;; GNU Emacs is free software; you can redistribute it and/or modify
21;; it under the terms of the GNU General Public License as published by
22;; the Free Software Foundation; either version 2, or (at your option)
23;; any later version.
24
25;; GNU Emacs is distributed in the hope that it will be useful,
26;; but WITHOUT ANY WARRANTY; without even the implied warranty of
27;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28;; GNU General Public License for more details.
29
30;; You should have received a copy of the GNU General Public License
31;; along with GNU Emacs; see the file COPYING. If not, write to the
32;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
33;; Boston, MA 02111-1307, USA.
34
35;;; Commentary:
36
37;; This package provides GNU Emacs major modes for editing C, C++,
aac90c52 38;; Objective-C, Java and IDL code. As of the latest Emacs and XEmacs
785eecbb
RS
39;; releases, it is the default package for editing these languages.
40;; This package is called "CC Mode", and should be spelled exactly
aac90c52
RS
41;; this way. It supports K&R and ANSI C, ANSI C++, Objective-C, Java,
42;; and CORBA's IDL with a consistent indentation model across all
43;; modes. This indentation model is intuitive and very flexible, so
44;; that almost any desired style of indentation can be supported.
45;; Installation, usage, and programming details are contained in an
46;; accompanying texinfo manual.
785eecbb
RS
47
48;; CC Mode's immediate ancestors were, c++-mode.el, cplus-md.el, and
49;; cplus-md1.el..
50
51;; NOTE: This mode does not perform font-locking (a.k.a syntactic
52;; coloring, keyword highlighting, etc.) for any of the supported
53;; modes. Typically this is done by a package called font-lock.el
54;; which I do *not* maintain. You should contact the Emacs
55;; maintainers for questions about coloring or highlighting in any
56;; language mode.
57
58;; To submit bug reports, type "C-c C-b". These will be sent to
59;; bug-gnu-emacs@prep.ai.mit.edu as well as cc-mode-help@python.org,
60;; and I'll read about them there (the former is mirrored as the
61;; Usenet newsgroup gnu.emacs.bug). Questions can sent to
62;; help-gnu-emacs@prep.ai.mit.edu (mirrored as gnu.emacs.help) and/or
63;; cc-mode-help@python.org. Please do not send bugs or questions to
64;; my personal account.
65
785eecbb
RS
66;; Many, many thanks go out to all the folks on the beta test list.
67;; Without their patience, testing, insight, code contributions, and
68;; encouragement CC Mode would be a far inferior package.
69
70;; You can get the latest version of CC Mode, including PostScript
71;; documentation and separate individual files from:
72;;
73;; http://www.python.org/ftp/emacs/
74
75;; Or if you don't have access to the World Wide Web, through
76;; anonymous ftp from:
77;;
78;; ftp://ftp.python.org/pub/emacs
79
80;;; Code:
81
82(eval-when-compile
83 (require 'cc-menus))
84(require 'cc-defs)
85
07092cb1
RS
86(defvar c-buffer-is-cc-mode nil
87 "Non-nil for all buffers with a `major-mode' derived from CC Mode.
88Otherwise, this variable is nil. I.e. this variable is non-nil for
89`c-mode', `c++-mode', `objc-mode', `java-mode', `idl-mode', and any
90other non-CC Mode mode that calls `c-initialize-cc-mode'
91\(e.g. `awk-mode').")
92(make-variable-buffer-local 'c-buffer-is-cc-mode)
93(put 'c-buffer-is-cc-mode 'permanent-local t)
94
785eecbb 95\f
1dad6919
RS
96;; Other modes and packages which depend on CC Mode should do the
97;; following to make sure everything is loaded and available for their
98;; use:
99;;
100;; (require 'cc-mode)
101;; (c-initialize-cc-mode)
102
aac90c52 103;;;###autoload
1dad6919 104(defun c-initialize-cc-mode ()
611c76a7
RS
105 (setq c-buffer-is-cc-mode t)
106 ;; sigh. give in to the pressure, but make really sure all the
107 ;; definitions we need are here
108 (if (or (not (fboundp 'functionp))
109 (not (fboundp 'char-before))
110 (not (c-safe (char-after) t)))
111 (require 'cc-mode-19))
1dad6919 112 ;; make sure all necessary components of CC Mode are loaded in.
aac90c52
RS
113 (let ((initprop 'cc-mode-is-initialized))
114 (require 'cc-vars)
115 (require 'cc-engine)
116 (require 'cc-langs)
117 (require 'cc-menus)
118 (require 'cc-align)
119 (require 'cc-styles)
120 (require 'cc-cmds)
121 ;; run the initialization hook, but only once
122 (or (get 'c-initialize-cc-mode initprop)
123 (progn
124 (c-initialize-builtin-style)
125 (run-hooks 'c-initialization-hook)
126 (put 'c-initialize-cc-mode initprop t)))
127 ))
1dad6919
RS
128
129\f
785eecbb
RS
130;;;###autoload
131(defun c-mode ()
132 "Major mode for editing K&R and ANSI C code.
133To submit a problem report, enter `\\[c-submit-bug-report]' from a
134c-mode buffer. This automatically sets up a mail buffer with version
135information already added. You just need to add a description of the
136problem, including a reproducible test case and send the message.
137
138To see what version of CC Mode you are running, enter `\\[c-version]'.
139
140The hook variable `c-mode-hook' is run with no args, if that value is
141bound and has a non-nil value. Also the hook `c-mode-common-hook' is
142run first.
143
144Key bindings:
145\\{c-mode-map}"
146 (interactive)
1dad6919 147 (c-initialize-cc-mode)
785eecbb
RS
148 (kill-all-local-variables)
149 (set-syntax-table c-mode-syntax-table)
150 (setq major-mode 'c-mode
151 mode-name "C"
152 local-abbrev-table c-mode-abbrev-table)
153 (use-local-map c-mode-map)
154 (c-common-init)
155 (setq comment-start "/* "
156 comment-end " */"
785eecbb
RS
157 c-conditional-key c-C-conditional-key
158 c-class-key c-C-class-key
159 c-baseclass-key nil
611c76a7 160 c-comment-start-regexp c-C++-comment-start-regexp
c0b08eb0
DL
161 imenu-generic-expression cc-imenu-c-generic-expression
162 imenu-case-fold-search nil)
785eecbb
RS
163 (run-hooks 'c-mode-common-hook)
164 (run-hooks 'c-mode-hook)
165 (c-update-modeline))
166
167\f
168;;;###autoload
169(defun c++-mode ()
170 "Major mode for editing C++ code.
171To submit a problem report, enter `\\[c-submit-bug-report]' from a
172c++-mode buffer. This automatically sets up a mail buffer with
173version information already added. You just need to add a description
174of the problem, including a reproducible test case, and send the
175message.
176
177To see what version of CC Mode you are running, enter `\\[c-version]'.
178
179The hook variable `c++-mode-hook' is run with no args, if that
180variable is bound and has a non-nil value. Also the hook
181`c-mode-common-hook' is run first.
182
183Key bindings:
184\\{c++-mode-map}"
185 (interactive)
1dad6919 186 (c-initialize-cc-mode)
785eecbb
RS
187 (kill-all-local-variables)
188 (set-syntax-table c++-mode-syntax-table)
189 (setq major-mode 'c++-mode
190 mode-name "C++"
191 local-abbrev-table c++-mode-abbrev-table)
192 (use-local-map c++-mode-map)
193 (c-common-init)
194 (setq comment-start "// "
195 comment-end ""
785eecbb
RS
196 c-conditional-key c-C++-conditional-key
197 c-comment-start-regexp c-C++-comment-start-regexp
198 c-class-key c-C++-class-key
199 c-access-key c-C++-access-key
785eecbb 200 c-recognize-knr-p nil
c0b08eb0
DL
201 imenu-generic-expression cc-imenu-c++-generic-expression
202 imenu-case-fold-search nil)
785eecbb
RS
203 (run-hooks 'c-mode-common-hook)
204 (run-hooks 'c++-mode-hook)
205 (c-update-modeline))
206
207\f
208;;;###autoload
209(defun objc-mode ()
210 "Major mode for editing Objective C code.
211To submit a problem report, enter `\\[c-submit-bug-report]' from an
212objc-mode buffer. This automatically sets up a mail buffer with
213version information already added. You just need to add a description
214of the problem, including a reproducible test case, and send the
215message.
216
217To see what version of CC Mode you are running, enter `\\[c-version]'.
218
219The hook variable `objc-mode-hook' is run with no args, if that value
220is bound and has a non-nil value. Also the hook `c-mode-common-hook'
221is run first.
222
223Key bindings:
224\\{objc-mode-map}"
225 (interactive)
1dad6919 226 (c-initialize-cc-mode)
785eecbb
RS
227 (kill-all-local-variables)
228 (set-syntax-table objc-mode-syntax-table)
229 (setq major-mode 'objc-mode
230 mode-name "ObjC"
231 local-abbrev-table objc-mode-abbrev-table)
232 (use-local-map objc-mode-map)
233 (c-common-init)
234 (setq comment-start "// "
235 comment-end ""
785eecbb
RS
236 c-conditional-key c-C-conditional-key
237 c-comment-start-regexp c-C++-comment-start-regexp
238 c-class-key c-ObjC-class-key
239 c-baseclass-key nil
240 c-access-key c-ObjC-access-key
dc2b4c5f
KH
241 c-method-key c-ObjC-method-key
242 imenu-create-index-function 'cc-imenu-objc-function)
785eecbb
RS
243 (run-hooks 'c-mode-common-hook)
244 (run-hooks 'objc-mode-hook)
245 (c-update-modeline))
246
247\f
248;;;###autoload
249(defun java-mode ()
250 "Major mode for editing Java code.
dc2b4c5f 251To submit a problem report, enter `\\[c-submit-bug-report]' from a
785eecbb
RS
252java-mode buffer. This automatically sets up a mail buffer with
253version information already added. You just need to add a description
254of the problem, including a reproducible test case and send the
255message.
256
257To see what version of CC Mode you are running, enter `\\[c-version]'.
258
259The hook variable `java-mode-hook' is run with no args, if that value
260is bound and has a non-nil value. Also the common hook
261`c-mode-common-hook' is run first. Note that this mode automatically
262sets the \"java\" style before calling any hooks so be careful if you
263set styles in `c-mode-common-hook'.
264
265Key bindings:
266\\{java-mode-map}"
267 (interactive)
1dad6919 268 (c-initialize-cc-mode)
785eecbb
RS
269 (kill-all-local-variables)
270 (set-syntax-table java-mode-syntax-table)
271 (setq major-mode 'java-mode
272 mode-name "Java"
273 local-abbrev-table java-mode-abbrev-table)
274 (use-local-map java-mode-map)
275 (c-common-init)
276 (setq comment-start "// "
277 comment-end ""
785eecbb
RS
278 c-conditional-key c-Java-conditional-key
279 c-comment-start-regexp c-Java-comment-start-regexp
280 c-class-key c-Java-class-key
281 c-method-key c-Java-method-key
785eecbb
RS
282 c-baseclass-key nil
283 c-recognize-knr-p nil
284 c-access-key c-Java-access-key
285 ;defun-prompt-regexp c-Java-defun-prompt-regexp
286 imenu-generic-expression cc-imenu-java-generic-expression
c0b08eb0 287 imenu-case-fold-search nil
785eecbb
RS
288 )
289 (c-set-style "java")
290 (run-hooks 'c-mode-common-hook)
291 (run-hooks 'java-mode-hook)
292 (c-update-modeline))
293
294\f
aac90c52
RS
295;;;###autoload
296(defun idl-mode ()
297 "Major mode for editing CORBA's IDL code.
298To submit a problem report, enter `\\[c-submit-bug-report]' from an
299idl-mode buffer. This automatically sets up a mail buffer with
300version information already added. You just need to add a description
301of the problem, including a reproducible test case, and send the
302message.
303
304To see what version of CC Mode you are running, enter `\\[c-version]'.
305
306The hook variable `idl-mode-hook' is run with no args, if that
307variable is bound and has a non-nil value. Also the hook
308`c-mode-common-hook' is run first.
309
310Key bindings:
311\\{idl-mode-map}"
312 (interactive)
313 (c-initialize-cc-mode)
314 (kill-all-local-variables)
315 (set-syntax-table idl-mode-syntax-table)
316 (setq major-mode 'idl-mode
317 mode-name "IDL"
318 local-abbrev-table idl-mode-abbrev-table)
319 (use-local-map idl-mode-map)
320 (c-common-init)
321 (setq comment-start "// "
322 comment-end ""
aac90c52
RS
323 c-conditional-key c-C++-conditional-key
324 c-comment-start-regexp c-C++-comment-start-regexp
325 c-class-key c-C++-class-key
326 c-access-key c-C++-access-key
aac90c52
RS
327 c-recognize-knr-p nil)
328;; imenu-generic-expression cc-imenu-c++-generic-expression)
329 (run-hooks 'c-mode-common-hook)
330 (run-hooks 'idl-mode-hook)
331 (c-update-modeline))
332
333\f
c2efc1d0 334;; bug reporting
785eecbb
RS
335
336(defconst c-mode-help-address
337 "bug-gnu-emacs@prep.ai.mit.edu, cc-mode-help@python.org"
338 "Address for CC Mode bug reports.")
339
340(defun c-version ()
341 "Echo the current version of CC Mode in the minibuffer."
342 (interactive)
343 (message "Using CC Mode version %s" c-version)
344 (c-keep-region-active))
345
346;; Get reporter-submit-bug-report when byte-compiling
347(eval-when-compile
348 (require 'reporter))
349
350(defun c-submit-bug-report ()
351 "Submit via mail a bug report on CC Mode."
352 (interactive)
353 (require 'cc-vars)
354 ;; load in reporter
355 (let ((reporter-prompt-for-summary-p t)
356 (reporter-dont-compact-list '(c-offsets-alist))
357 (style c-indentation-style)
358 (hook c-special-indent-hook)
359 (c-features c-emacs-features))
360 (and
361 (if (y-or-n-p "Do you want to submit a report on CC Mode? ")
362 t (message "") nil)
363 (require 'reporter)
364 (reporter-submit-bug-report
365 c-mode-help-address
366 (concat "CC Mode " c-version " ("
367 (cond ((eq major-mode 'c++-mode) "C++")
368 ((eq major-mode 'c-mode) "C")
369 ((eq major-mode 'objc-mode) "ObjC")
370 ((eq major-mode 'java-mode) "Java")
371 )
372 ")")
373 (let ((vars (list
374 ;; report only the vars that affect indentation
375 'c-basic-offset
376 'c-offsets-alist
377 'c-cleanup-list
378 'c-comment-only-line-offset
379 'c-backslash-column
380 'c-delete-function
381 'c-electric-pound-behavior
382 'c-hanging-braces-alist
383 'c-hanging-colons-alist
384 'c-hanging-comment-starter-p
385 'c-hanging-comment-ender-p
386 'c-indent-comments-syntactically-p
387 'c-tab-always-indent
c2efc1d0 388 'c-comment-continuation-stars
785eecbb
RS
389 'c-label-minimum-indentation
390 'defun-prompt-regexp
391 'tab-width
392 )))
393 (if (not (boundp 'defun-prompt-regexp))
394 (delq 'defun-prompt-regexp vars)
395 vars))
396 (function
397 (lambda ()
398 (insert
399 "Buffer Style: " style "\n\n"
400 (if hook
401 (concat "\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
402 "c-special-indent-hook is set to '"
403 (format "%s" hook)
404 ".\nPerhaps this is your problem?\n"
405 "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n")
406 "\n")
407 (format "c-emacs-features: %s\n" c-features)
408 )))
409 nil
410 "Dear Barry,"
411 ))))
412
413\f
414(provide 'cc-mode)
415;;; cc-mode.el ends here