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