]> code.delx.au - gnu-emacs/blob - lisp/progmodes/cc-mode.el
(ebrowse-tags-query-replace): Construct a
[gnu-emacs] / lisp / progmodes / cc-mode.el
1 ;;; cc-mode.el --- major mode for editing C, C++, Objective-C, and Java code
2
3 ;; Copyright (C) 1985,1987,1992-1999 Free Software Foundation, Inc.
4
5 ;; Authors: 1998-1999 Barry A. Warsaw and Martin Stjernholm
6 ;; 1992-1997 Barry A. Warsaw
7 ;; 1987 Dave Detlefs and Stewart Clamen
8 ;; 1985 Richard M. Stallman
9 ;; Maintainer: bug-cc-mode@gnu.org
10 ;; Created: a long, long, time ago. adapted from the original c-mode.el
11 ;; Keywords: c languages oop
12
13 (defconst c-version "5.26e"
14 "CC Mode version number.")
15
16 ;; NOTE: Read the commentary below for the right way to submit bug reports!
17 ;; NOTE: See the accompanying texinfo manual for details on using this mode!
18
19 ;; This file is part of GNU Emacs.
20
21 ;; GNU Emacs is free software; you can redistribute it and/or modify
22 ;; it under the terms of the GNU General Public License as published by
23 ;; the Free Software Foundation; either version 2, or (at your option)
24 ;; any later version.
25
26 ;; GNU Emacs is distributed in the hope that it will be useful,
27 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
28 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 ;; GNU General Public License for more details.
30
31 ;; You should have received a copy of the GNU General Public License
32 ;; along with GNU Emacs; see the file COPYING. If not, write to the
33 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
34 ;; Boston, MA 02111-1307, USA.
35
36 ;;; Commentary:
37
38 ;; This package provides GNU Emacs major modes for editing C, C++,
39 ;; Objective-C, Java, IDL and Pike code. As of the latest Emacs and
40 ;; XEmacs releases, it is the default package for editing these
41 ;; languages. This package is called "CC Mode", and should be spelled
42 ;; exactly this way.
43
44 ;; CC Mode supports K&R and ANSI C, ANSI C++, Objective-C, Java,
45 ;; CORBA's IDL, and Pike with a consistent indentation model across
46 ;; all modes. This indentation model is intuitive and very flexible,
47 ;; so that almost any desired style of indentation can be supported.
48 ;; Installation, usage, and programming details are contained in an
49 ;; accompanying texinfo manual.
50
51 ;; CC Mode's immediate ancestors were, c++-mode.el, cplus-md.el, and
52 ;; cplus-md1.el..
53
54 ;; NOTE: This mode does not perform font-locking (a.k.a syntactic
55 ;; coloring, keyword highlighting, etc.) for any of the supported
56 ;; modes. Typically this is done by a package called font-lock.el
57 ;; which we do *not* maintain. You should contact the Emacs or XEmacs
58 ;; maintainers for questions about coloring or highlighting in any
59 ;; language mode.
60
61 ;; To submit bug reports, type "C-c C-b". These will be sent to
62 ;; bug-gnu-emacs@gnu.org (mirrored as the Usenet newsgroup
63 ;; gnu.emacs.bug) as well as bug-cc-mode@gnu.org, which directly
64 ;; contacts the CC Mode maintainers. Questions can sent to
65 ;; help-gnu-emacs@gnu.org (mirrored as gnu.emacs.help) and/or
66 ;; bug-cc-mode@gnu.org. The old CC Mode contact address,
67 ;; cc-mode-help@python.org is currently still active, but its use is
68 ;; discouraged. Please use bug-cc-mode@gnu.org instead. Please do
69 ;; not send bugs or questions to our personal accounts; we reserve the
70 ;; right to ignore such email!
71
72 ;; Many, many thanks go out to all the folks on the beta test list.
73 ;; Without their patience, testing, insight, code contributions, and
74 ;; encouragement CC Mode would be a far inferior package.
75
76 ;; You can get the latest version of CC Mode, including PostScript
77 ;; documentation and separate individual files from:
78 ;;
79 ;; http://www.python.org/emacs/cc-mode/
80 ;;
81 ;; You can join a moderated CC Mode announcement-only mailing list by
82 ;; visiting
83 ;;
84 ;; http://www.python.org/mailman/listinfo/cc-mode-announce
85
86 ;;; Code:
87
88 (eval-when-compile
89 (let ((load-path
90 ;; Try to make sure the source directory is at the front of
91 ;; load-path when we load cc-defs.
92 (if (and (boundp 'byte-compile-current-file)
93 (stringp byte-compile-current-file))
94 ;; byte-compile-current-file is set by the byte compiler
95 ;; to the full path to this file.
96 (cons (file-name-directory byte-compile-current-file)
97 load-path)
98 load-path)))
99 ;; Load our version of cc-defs unconditionally, since an older
100 ;; version might very well be dumped in or already loaded. This
101 ;; way we ensure that the code is compiled with the correct macros
102 ;; and defsubsts. The same problem affects the subpackages that's
103 ;; require'd below, but that doesn't harm the compiler; it can
104 ;; only cause some bogus warnings.
105 (load "cc-defs" nil t)))
106
107 (require 'cc-defs) ; Not meaningless; this passes on require's from cc-defs.
108 (require 'cc-menus)
109 (require 'cc-vars)
110 (require 'cc-styles)
111 (require 'cc-langs)
112 (require 'cc-engine)
113 (require 'cc-align)
114 (require 'cc-cmds)
115
116 \f
117 ;; Other modes and packages which depend on CC Mode should do the
118 ;; following to make sure everything is loaded and available for their
119 ;; use:
120 ;;
121 ;; (require 'cc-mode)
122 ;; (c-initialize-cc-mode)
123
124 ;;;###autoload
125 (defun c-initialize-cc-mode ()
126 (setq c-buffer-is-cc-mode t)
127 (let ((initprop 'cc-mode-is-initialized)
128 c-initialization-ok)
129 (unless (get 'c-initialize-cc-mode initprop)
130 (unwind-protect
131 (progn
132 (put 'c-initialize-cc-mode initprop t)
133 (c-initialize-builtin-style)
134 (run-hooks 'c-initialization-hook)
135 ;; Fix obsolete variables.
136 (if (boundp 'c-comment-continuation-stars)
137 (setq c-block-comment-prefix c-comment-continuation-stars))
138 (setq c-initialization-ok t))
139 ;; Will try initialization hooks again if they failed.
140 (put 'c-initialize-cc-mode initprop c-initialization-ok)))
141 ))
142
143 \f
144 ;;;###autoload
145 (defun c-mode ()
146 "Major mode for editing K&R and ANSI C code.
147 To submit a problem report, enter `\\[c-submit-bug-report]' from a
148 c-mode buffer. This automatically sets up a mail buffer with version
149 information already added. You just need to add a description of the
150 problem, including a reproducible test case and send the message.
151
152 To see what version of CC Mode you are running, enter `\\[c-version]'.
153
154 The hook variable `c-mode-hook' is run with no args, if that value is
155 bound and has a non-nil value. Also the hook `c-mode-common-hook' is
156 run first.
157
158 Key bindings:
159 \\{c-mode-map}"
160 (interactive)
161 (c-initialize-cc-mode)
162 (kill-all-local-variables)
163 (set-syntax-table c-mode-syntax-table)
164 (setq major-mode 'c-mode
165 mode-name "C"
166 local-abbrev-table c-mode-abbrev-table)
167 (use-local-map c-mode-map)
168 (c-common-init)
169 (setq comment-start "/* "
170 comment-end " */"
171 c-conditional-key c-C-conditional-key
172 c-class-key c-C-class-key
173 c-baseclass-key nil
174 c-comment-start-regexp c-C-comment-start-regexp
175 c-bitfield-key c-C-bitfield-key
176 )
177 (cc-imenu-init cc-imenu-c-generic-expression)
178 (run-hooks 'c-mode-common-hook)
179 (run-hooks 'c-mode-hook)
180 (c-update-modeline))
181
182 \f
183 ;;;###autoload
184 (defun c++-mode ()
185 "Major mode for editing C++ code.
186 To submit a problem report, enter `\\[c-submit-bug-report]' from a
187 c++-mode buffer. This automatically sets up a mail buffer with
188 version information already added. You just need to add a description
189 of the problem, including a reproducible test case, and send the
190 message.
191
192 To see what version of CC Mode you are running, enter `\\[c-version]'.
193
194 The hook variable `c++-mode-hook' is run with no args, if that
195 variable is bound and has a non-nil value. Also the hook
196 `c-mode-common-hook' is run first.
197
198 Key bindings:
199 \\{c++-mode-map}"
200 (interactive)
201 (c-initialize-cc-mode)
202 (kill-all-local-variables)
203 (set-syntax-table c++-mode-syntax-table)
204 (setq major-mode 'c++-mode
205 mode-name "C++"
206 local-abbrev-table c++-mode-abbrev-table)
207 (use-local-map c++-mode-map)
208 (c-common-init)
209 (setq comment-start "// "
210 comment-end ""
211 c-conditional-key c-C++-conditional-key
212 c-comment-start-regexp c-C++-comment-start-regexp
213 c-class-key c-C++-class-key
214 c-extra-toplevel-key c-C++-extra-toplevel-key
215 c-access-key c-C++-access-key
216 c-recognize-knr-p nil
217 c-bitfield-key c-C-bitfield-key
218 )
219 (cc-imenu-init cc-imenu-c++-generic-expression)
220 (run-hooks 'c-mode-common-hook)
221 (run-hooks 'c++-mode-hook)
222 (c-update-modeline))
223
224 \f
225 ;;;###autoload
226 (defun objc-mode ()
227 "Major mode for editing Objective C code.
228 To submit a problem report, enter `\\[c-submit-bug-report]' from an
229 objc-mode buffer. This automatically sets up a mail buffer with
230 version information already added. You just need to add a description
231 of the problem, including a reproducible test case, and send the
232 message.
233
234 To see what version of CC Mode you are running, enter `\\[c-version]'.
235
236 The hook variable `objc-mode-hook' is run with no args, if that value
237 is bound and has a non-nil value. Also the hook `c-mode-common-hook'
238 is run first.
239
240 Key bindings:
241 \\{objc-mode-map}"
242 (interactive)
243 (c-initialize-cc-mode)
244 (kill-all-local-variables)
245 (set-syntax-table objc-mode-syntax-table)
246 (setq major-mode 'objc-mode
247 mode-name "ObjC"
248 local-abbrev-table objc-mode-abbrev-table)
249 (use-local-map objc-mode-map)
250 (c-common-init)
251 (setq comment-start "// "
252 comment-end ""
253 c-conditional-key c-ObjC-conditional-key
254 c-comment-start-regexp c-ObjC-comment-start-regexp
255 c-class-key c-ObjC-class-key
256 c-baseclass-key nil
257 c-access-key c-ObjC-access-key
258 c-method-key c-ObjC-method-key
259 )
260 (cc-imenu-init cc-imenu-objc-generic-expression)
261 (run-hooks 'c-mode-common-hook)
262 (run-hooks 'objc-mode-hook)
263 (c-update-modeline))
264
265 \f
266 ;;;###autoload
267 (defun java-mode ()
268 "Major mode for editing Java code.
269 To submit a problem report, enter `\\[c-submit-bug-report]' from a
270 java-mode buffer. This automatically sets up a mail buffer with
271 version information already added. You just need to add a description
272 of the problem, including a reproducible test case and send the
273 message.
274
275 To see what version of CC Mode you are running, enter `\\[c-version]'.
276
277 The hook variable `java-mode-hook' is run with no args, if that value
278 is bound and has a non-nil value. Also the common hook
279 `c-mode-common-hook' is run first. Note that this mode automatically
280 sets the \"java\" style before calling any hooks so be careful if you
281 set styles in `c-mode-common-hook'.
282
283 Key bindings:
284 \\{java-mode-map}"
285 (interactive)
286 (c-initialize-cc-mode)
287 (kill-all-local-variables)
288 (set-syntax-table java-mode-syntax-table)
289 (setq major-mode 'java-mode
290 mode-name "Java"
291 local-abbrev-table java-mode-abbrev-table)
292 (use-local-map java-mode-map)
293 (c-common-init)
294 (setq comment-start "// "
295 comment-end ""
296 paragraph-start (concat paragraph-start
297 "\\("
298 c-Java-javadoc-paragraph-start
299 "\\|$\\)")
300 c-conditional-key c-Java-conditional-key
301 c-comment-start-regexp c-Java-comment-start-regexp
302 c-class-key c-Java-class-key
303 c-method-key nil
304 c-baseclass-key nil
305 c-recognize-knr-p nil
306 c-access-key c-Java-access-key
307 c-inexpr-class-key c-Java-inexpr-class-key
308 ;defun-prompt-regexp c-Java-defun-prompt-regexp
309 )
310 (cc-imenu-init cc-imenu-java-generic-expression)
311 (run-hooks 'c-mode-common-hook)
312 (run-hooks 'java-mode-hook)
313 (c-update-modeline))
314
315 \f
316 ;;;###autoload
317 (defun idl-mode ()
318 "Major mode for editing CORBA's IDL code.
319 To submit a problem report, enter `\\[c-submit-bug-report]' from an
320 idl-mode buffer. This automatically sets up a mail buffer with
321 version information already added. You just need to add a description
322 of the problem, including a reproducible test case, and send the
323 message.
324
325 To see what version of CC Mode you are running, enter `\\[c-version]'.
326
327 The hook variable `idl-mode-hook' is run with no args, if that
328 variable is bound and has a non-nil value. Also the hook
329 `c-mode-common-hook' is run first.
330
331 Key bindings:
332 \\{idl-mode-map}"
333 (interactive)
334 (c-initialize-cc-mode)
335 (kill-all-local-variables)
336 (set-syntax-table idl-mode-syntax-table)
337 (setq major-mode 'idl-mode
338 mode-name "IDL"
339 local-abbrev-table idl-mode-abbrev-table)
340 (use-local-map idl-mode-map)
341 (c-common-init)
342 (setq comment-start "// "
343 comment-end ""
344 c-conditional-key c-IDL-conditional-key
345 c-comment-start-regexp c-IDL-comment-start-regexp
346 c-class-key c-IDL-class-key
347 c-method-key nil
348 c-baseclass-key nil
349 c-extra-toplevel-key c-IDL-extra-toplevel-key
350 c-access-key c-IDL-access-key
351 c-recognize-knr-p nil
352 )
353 ;;(cc-imenu-init cc-imenu-idl-generic-expression) ;FIXME
354 (run-hooks 'c-mode-common-hook)
355 (run-hooks 'idl-mode-hook)
356 (c-update-modeline))
357
358 \f
359 ;;;###autoload
360 (defun pike-mode ()
361 "Major mode for editing Pike code.
362 To submit a problem report, enter `\\[c-submit-bug-report]' from an
363 idl-mode buffer. This automatically sets up a mail buffer with
364 version information already added. You just need to add a description
365 of the problem, including a reproducible test case, and send the
366 message.
367
368 To see what version of CC Mode you are running, enter `\\[c-version]'.
369
370 The hook variable `pike-mode-hook' is run with no args, if that value
371 is bound and has a non-nil value. Also the common hook
372 `c-mode-common-hook' is run first.
373
374 Key bindings:
375 \\{pike-mode-map}"
376 (interactive)
377 (c-initialize-cc-mode)
378 (kill-all-local-variables)
379 (set-syntax-table pike-mode-syntax-table)
380 (setq major-mode 'pike-mode
381 mode-name "Pike"
382 local-abbrev-table pike-mode-abbrev-table)
383 (use-local-map pike-mode-map)
384 (c-common-init)
385 (setq comment-start "// "
386 comment-end ""
387 c-conditional-key c-Pike-conditional-key
388 c-comment-start-regexp c-Pike-comment-start-regexp
389 c-class-key c-Pike-class-key
390 c-method-key nil
391 c-baseclass-key nil
392 c-recognize-knr-p nil
393 c-access-key c-Pike-access-key
394 c-lambda-key c-Pike-lambda-key
395 c-inexpr-block-key c-Pike-inexpr-block-key
396 c-special-brace-lists c-Pike-special-brace-lists
397 )
398 ;;(cc-imenu-init cc-imenu-pike-generic-expression) ;FIXME
399 (run-hooks 'c-mode-common-hook)
400 (run-hooks 'pike-mode-hook)
401 (c-update-modeline))
402
403 \f
404 (defun c-setup-filladapt ()
405 "Convenience function to configure Kyle E. Jones' Filladapt mode for
406 CC Mode by making sure the proper entries are present on
407 `filladapt-token-table', `filladapt-token-match-table', and
408 `filladapt-token-conversion-table'. This is intended to be used on
409 `c-mode-common-hook' or similar."
410 ;; This function is intended to be used explicitly by the end user
411 ;; only.
412 ;;
413 ;; The default configuration already handles C++ comments, but we
414 ;; need to add handling of C block comments. A new filladapt token
415 ;; `c-comment' is added for that.
416 (let (p)
417 (setq p filladapt-token-table)
418 (while (and p (not (eq (car-safe (cdr-safe (car-safe p))) 'c-comment)))
419 (setq p (cdr-safe p)))
420 (if p
421 (setcar (car p) c-comment-prefix-regexp)
422 (setq filladapt-token-table
423 (append (list (car filladapt-token-table)
424 (list c-comment-prefix-regexp 'c-comment))
425 (cdr filladapt-token-table)))))
426 (unless (assq 'c-comment filladapt-token-match-table)
427 (setq filladapt-token-match-table
428 (append '((c-comment c-comment))
429 filladapt-token-match-table)))
430 (unless (assq 'c-comment filladapt-token-conversion-table)
431 (setq filladapt-token-conversion-table
432 (append '((c-comment . exact))
433 filladapt-token-conversion-table))))
434
435 \f
436 ;; bug reporting
437
438 (defconst c-mode-help-address
439 "bug-gnu-emacs@gnu.org, bug-cc-mode@gnu.org"
440 "Addresses for CC Mode bug reports.")
441
442 (defun c-version ()
443 "Echo the current version of CC Mode in the minibuffer."
444 (interactive)
445 (message "Using CC Mode version %s" c-version)
446 (c-keep-region-active))
447
448 (defun c-submit-bug-report ()
449 "Submit via mail a bug report on CC Mode."
450 (interactive)
451 (require 'reporter)
452 (require 'cc-vars)
453 ;; load in reporter
454 (let ((reporter-prompt-for-summary-p t)
455 (reporter-dont-compact-list '(c-offsets-alist))
456 (style c-indentation-style)
457 (hook c-special-indent-hook)
458 (c-features c-emacs-features))
459 (and
460 (if (y-or-n-p "Do you want to submit a report on CC Mode? ")
461 t (message "") nil)
462 (require 'reporter)
463 (reporter-submit-bug-report
464 c-mode-help-address
465 (concat "CC Mode " c-version " ("
466 (cond ((eq major-mode 'c++-mode) "C++")
467 ((eq major-mode 'c-mode) "C")
468 ((eq major-mode 'objc-mode) "ObjC")
469 ((eq major-mode 'java-mode) "Java")
470 ((eq major-mode 'idl-mode) "IDL")
471 ((eq major-mode 'pike-mode) "Pike")
472 )
473 ")")
474 (let ((vars (append
475 ;; report only the vars that affect indentation
476 c-style-variables
477 '(c-delete-function
478 c-electric-pound-behavior
479 c-indent-comments-syntactically-p
480 c-tab-always-indent
481 defun-prompt-regexp
482 tab-width
483 comment-column
484 parse-sexp-ignore-comments
485 ;; A brain-damaged XEmacs only variable that, if
486 ;; set to nil can cause all kinds of chaos.
487 signal-error-on-buffer-boundary
488 ;; Variables that affect line breaking and comments.
489 auto-fill-mode
490 filladapt-mode
491 comment-multi-line
492 comment-start-skip
493 fill-prefix
494 paragraph-start
495 adaptive-fill-mode
496 adaptive-fill-regexp)
497 nil)))
498 (delq 'c-special-indent-hook vars)
499 (unless (boundp 'defun-prompt-regexp)
500 (delq 'defun-prompt-regexp vars))
501 (unless (boundp 'filladapt-mode)
502 (delq 'filladapt-mode vars))
503 vars)
504 (function
505 (lambda ()
506 (insert
507 "Buffer Style: " style "\n\n"
508 (if (and hook
509 (or (/= (length hook) 1)
510 (not (eq (car hook) 'c-gnu-impose-minimum))
511 ))
512 (concat "\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
513 "c-special-indent-hook is set to '"
514 (format "%s" hook)
515 ".\nPerhaps this is your problem?\n"
516 "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n")
517 "\n")
518 (format "c-emacs-features: %s\n" c-features)
519 )))
520 nil
521 "Dear Barry and Martin,"
522 ))))
523
524 \f
525 (provide 'cc-mode)
526 ;;; cc-mode.el ends here