]> code.delx.au - gnu-emacs/blob - lisp/progmodes/cc-mode.el
(Info-default-directory-list): Add sysdir last.
[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,87,92,93,94,95,96,97,98 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
10 ;; Keywords: c languages oop
11
12 (defconst c-version "5.21"
13 "CC Mode version number.")
14
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++,
38 ;; Objective-C, Java and IDL code. As of the latest Emacs and XEmacs
39 ;; releases, it is the default package for editing these languages.
40 ;; This package is called "CC Mode", and should be spelled exactly
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.
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
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 \f
83 (require 'cc-defs)
84
85 ;; sigh. give in to the pressure, but make really sure all the
86 ;; definitions we need are here
87 (if (or (not (fboundp 'functionp))
88 (not (fboundp 'char-before))
89 (not (c-safe (char-after) t))
90 (not (fboundp 'when))
91 (not (fboundp 'unless)))
92 (require 'cc-mode-19))
93
94 (require 'cc-menus)
95 (require 'cc-vars)
96 (require 'cc-engine)
97 (require 'cc-langs)
98 (require 'cc-align)
99 (require 'cc-styles)
100 (require 'cc-cmds)
101
102 (defvar c-buffer-is-cc-mode nil
103 "Non-nil for all buffers with a `major-mode' derived from CC Mode.
104 Otherwise, this variable is nil. I.e. this variable is non-nil for
105 `c-mode', `c++-mode', `objc-mode', `java-mode', `idl-mode', and any
106 other non-CC Mode mode that calls `c-initialize-cc-mode'
107 \(e.g. `awk-mode').")
108 (make-variable-buffer-local 'c-buffer-is-cc-mode)
109 (put 'c-buffer-is-cc-mode 'permanent-local t)
110
111
112 \f
113 ;; Other modes and packages which depend on CC Mode should do the
114 ;; following to make sure everything is loaded and available for their
115 ;; use:
116 ;;
117 ;; (require 'cc-mode)
118 ;; (c-initialize-cc-mode)
119
120 ;;;###autoload
121 (defun c-initialize-cc-mode (&optional skip-styles)
122 (setq c-buffer-is-cc-mode t)
123 (let ((initprop 'cc-mode-is-initialized))
124 ;; run the initialization hook, but only once
125 (or (get 'c-initialize-cc-mode initprop)
126 (progn
127 (or skip-styles
128 (c-initialize-builtin-style))
129 (run-hooks 'c-initialization-hook)
130 (put 'c-initialize-cc-mode initprop t)))
131 ))
132
133 \f
134 ;;;###autoload
135 (defun c-mode ()
136 "Major mode for editing K&R and ANSI C code.
137 To submit a problem report, enter `\\[c-submit-bug-report]' from a
138 c-mode buffer. This automatically sets up a mail buffer with version
139 information already added. You just need to add a description of the
140 problem, including a reproducible test case and send the message.
141
142 To see what version of CC Mode you are running, enter `\\[c-version]'.
143
144 The hook variable `c-mode-hook' is run with no args, if that value is
145 bound and has a non-nil value. Also the hook `c-mode-common-hook' is
146 run first.
147
148 Key bindings:
149 \\{c-mode-map}"
150 (interactive)
151 (c-initialize-cc-mode)
152 (kill-all-local-variables)
153 (set-syntax-table c-mode-syntax-table)
154 (setq major-mode 'c-mode
155 mode-name "C"
156 local-abbrev-table c-mode-abbrev-table)
157 (use-local-map c-mode-map)
158 (c-common-init)
159 (setq comment-start "/* "
160 comment-end " */"
161 c-conditional-key c-C-conditional-key
162 c-class-key c-C-class-key
163 c-baseclass-key nil
164 c-comment-start-regexp c-C++-comment-start-regexp
165 imenu-generic-expression cc-imenu-c-generic-expression
166 imenu-case-fold-search nil)
167 (run-hooks 'c-mode-common-hook)
168 (run-hooks 'c-mode-hook)
169 (c-update-modeline))
170
171 \f
172 ;;;###autoload
173 (defun c++-mode ()
174 "Major mode for editing C++ code.
175 To submit a problem report, enter `\\[c-submit-bug-report]' from a
176 c++-mode buffer. This automatically sets up a mail buffer with
177 version information already added. You just need to add a description
178 of the problem, including a reproducible test case, and send the
179 message.
180
181 To see what version of CC Mode you are running, enter `\\[c-version]'.
182
183 The hook variable `c++-mode-hook' is run with no args, if that
184 variable is bound and has a non-nil value. Also the hook
185 `c-mode-common-hook' is run first.
186
187 Key bindings:
188 \\{c++-mode-map}"
189 (interactive)
190 (c-initialize-cc-mode)
191 (kill-all-local-variables)
192 (set-syntax-table c++-mode-syntax-table)
193 (setq major-mode 'c++-mode
194 mode-name "C++"
195 local-abbrev-table c++-mode-abbrev-table)
196 (use-local-map c++-mode-map)
197 (c-common-init)
198 (setq comment-start "// "
199 comment-end ""
200 c-conditional-key c-C++-conditional-key
201 c-comment-start-regexp c-C++-comment-start-regexp
202 c-class-key c-C++-class-key
203 c-access-key c-C++-access-key
204 c-recognize-knr-p nil
205 imenu-generic-expression cc-imenu-c++-generic-expression
206 imenu-case-fold-search nil)
207 (run-hooks 'c-mode-common-hook)
208 (run-hooks 'c++-mode-hook)
209 (c-update-modeline))
210
211 \f
212 ;;;###autoload
213 (defun objc-mode ()
214 "Major mode for editing Objective C code.
215 To submit a problem report, enter `\\[c-submit-bug-report]' from an
216 objc-mode buffer. This automatically sets up a mail buffer with
217 version information already added. You just need to add a description
218 of the problem, including a reproducible test case, and send the
219 message.
220
221 To see what version of CC Mode you are running, enter `\\[c-version]'.
222
223 The hook variable `objc-mode-hook' is run with no args, if that value
224 is bound and has a non-nil value. Also the hook `c-mode-common-hook'
225 is run first.
226
227 Key bindings:
228 \\{objc-mode-map}"
229 (interactive)
230 (c-initialize-cc-mode)
231 (kill-all-local-variables)
232 (set-syntax-table objc-mode-syntax-table)
233 (setq major-mode 'objc-mode
234 mode-name "ObjC"
235 local-abbrev-table objc-mode-abbrev-table)
236 (use-local-map objc-mode-map)
237 (c-common-init)
238 (setq comment-start "// "
239 comment-end ""
240 c-conditional-key c-C-conditional-key
241 c-comment-start-regexp c-C++-comment-start-regexp
242 c-class-key c-ObjC-class-key
243 c-baseclass-key nil
244 c-access-key c-ObjC-access-key
245 c-method-key c-ObjC-method-key
246 imenu-create-index-function 'cc-imenu-objc-function
247 imenu-case-fold-search nil
248 )
249 (run-hooks 'c-mode-common-hook)
250 (run-hooks 'objc-mode-hook)
251 (c-update-modeline))
252
253 \f
254 ;;;###autoload
255 (defun java-mode ()
256 "Major mode for editing Java code.
257 To submit a problem report, enter `\\[c-submit-bug-report]' from a
258 java-mode buffer. This automatically sets up a mail buffer with
259 version information already added. You just need to add a description
260 of the problem, including a reproducible test case and send the
261 message.
262
263 To see what version of CC Mode you are running, enter `\\[c-version]'.
264
265 The hook variable `java-mode-hook' is run with no args, if that value
266 is bound and has a non-nil value. Also the common hook
267 `c-mode-common-hook' is run first. Note that this mode automatically
268 sets the \"java\" style before calling any hooks so be careful if you
269 set styles in `c-mode-common-hook'.
270
271 Key bindings:
272 \\{java-mode-map}"
273 (interactive)
274 (c-initialize-cc-mode)
275 (kill-all-local-variables)
276 (set-syntax-table java-mode-syntax-table)
277 (setq major-mode 'java-mode
278 mode-name "Java"
279 local-abbrev-table java-mode-abbrev-table)
280 (use-local-map java-mode-map)
281 (c-common-init)
282 (setq comment-start "// "
283 comment-end ""
284 c-conditional-key c-Java-conditional-key
285 c-comment-start-regexp c-Java-comment-start-regexp
286 c-class-key c-Java-class-key
287 c-method-key nil
288 c-baseclass-key nil
289 c-recognize-knr-p nil
290 c-access-key c-Java-access-key
291 ;defun-prompt-regexp c-Java-defun-prompt-regexp
292 imenu-generic-expression cc-imenu-java-generic-expression
293 imenu-case-fold-search nil
294 )
295 (c-set-style "java")
296 (run-hooks 'c-mode-common-hook)
297 (run-hooks 'java-mode-hook)
298 (c-update-modeline))
299
300 \f
301 ;;;###autoload
302 (defun idl-mode ()
303 "Major mode for editing CORBA's IDL code.
304 To submit a problem report, enter `\\[c-submit-bug-report]' from an
305 idl-mode buffer. This automatically sets up a mail buffer with
306 version information already added. You just need to add a description
307 of the problem, including a reproducible test case, and send the
308 message.
309
310 To see what version of CC Mode you are running, enter `\\[c-version]'.
311
312 The hook variable `idl-mode-hook' is run with no args, if that
313 variable is bound and has a non-nil value. Also the hook
314 `c-mode-common-hook' is run first.
315
316 Key bindings:
317 \\{idl-mode-map}"
318 (interactive)
319 (c-initialize-cc-mode)
320 (kill-all-local-variables)
321 (set-syntax-table idl-mode-syntax-table)
322 (setq major-mode 'idl-mode
323 mode-name "IDL"
324 local-abbrev-table idl-mode-abbrev-table)
325 (use-local-map idl-mode-map)
326 (c-common-init)
327 (setq comment-start "// "
328 comment-end ""
329 c-conditional-key c-C++-conditional-key
330 c-comment-start-regexp c-C++-comment-start-regexp
331 c-class-key c-C++-class-key
332 c-access-key c-C++-access-key
333 c-recognize-knr-p nil
334 ;; imenu-generic-expression cc-imenu-c++-generic-expression
335 ;; imenu-case-fold-search nil
336 )
337 (run-hooks 'c-mode-common-hook)
338 (run-hooks 'idl-mode-hook)
339 (c-update-modeline))
340
341 \f
342 ;; bug reporting
343
344 (defconst c-mode-help-address
345 "bug-gnu-emacs@prep.ai.mit.edu, cc-mode-help@python.org"
346 "Address for CC Mode bug reports.")
347
348 (defun c-version ()
349 "Echo the current version of CC Mode in the minibuffer."
350 (interactive)
351 (message "Using CC Mode version %s" c-version)
352 (c-keep-region-active))
353
354 (defun c-submit-bug-report ()
355 "Submit via mail a bug report on CC Mode."
356 (interactive)
357 (require 'reporter)
358 (require 'cc-vars)
359 ;; load in reporter
360 (let ((reporter-prompt-for-summary-p t)
361 (reporter-dont-compact-list '(c-offsets-alist))
362 (style c-indentation-style)
363 (hook c-special-indent-hook)
364 (c-features c-emacs-features))
365 (and
366 (if (y-or-n-p "Do you want to submit a report on CC Mode? ")
367 t (message "") nil)
368 (require 'reporter)
369 (reporter-submit-bug-report
370 c-mode-help-address
371 (concat "CC Mode " c-version " ("
372 (cond ((eq major-mode 'c++-mode) "C++")
373 ((eq major-mode 'c-mode) "C")
374 ((eq major-mode 'objc-mode) "ObjC")
375 ((eq major-mode 'java-mode) "Java")
376 )
377 ")")
378 (let ((vars (list
379 ;; report only the vars that affect indentation
380 'c-basic-offset
381 'c-offsets-alist
382 'c-cleanup-list
383 'c-comment-only-line-offset
384 'c-backslash-column
385 'c-delete-function
386 'c-electric-pound-behavior
387 'c-hanging-braces-alist
388 'c-hanging-colons-alist
389 'c-hanging-comment-starter-p
390 'c-hanging-comment-ender-p
391 'c-indent-comments-syntactically-p
392 'c-tab-always-indent
393 'c-comment-continuation-stars
394 'c-label-minimum-indentation
395 'defun-prompt-regexp
396 'tab-width
397 )))
398 (if (not (boundp 'defun-prompt-regexp))
399 (delq 'defun-prompt-regexp vars)
400 vars))
401 (function
402 (lambda ()
403 (insert
404 "Buffer Style: " style "\n\n"
405 (if hook
406 (concat "\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
407 "c-special-indent-hook is set to '"
408 (format "%s" hook)
409 ".\nPerhaps this is your problem?\n"
410 "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n")
411 "\n")
412 (format "c-emacs-features: %s\n" c-features)
413 )))
414 nil
415 "Dear Barry and Martin,"
416 ))))
417
418 \f
419 (provide 'cc-mode)
420 ;;; cc-mode.el ends here