165
|
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.11
|
|
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, and Java 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, and
|
|
40 ;; Java, with a consistent indentation model across all modes. This
|
|
41 ;; indentation model is intuitive and very flexible, so that almost
|
|
42 ;; any desired style of indentation can be supported. Installation,
|
|
43 ;; usage, and programming details are contained in an accompanying
|
|
44 ;; 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 ;; YOU CAN IGNORE ALL BYTE-COMPILER WARNINGS. They are the result of
|
|
65 ;; the cross-Emacsen support. GNU Emacs 19 (from the FSF), GNU XEmacs
|
|
66 ;; 19 (formerly Lucid Emacs), and GNU Emacs 18 all do things
|
|
67 ;; differently and there's no way to shut the byte-compiler up at the
|
|
68 ;; necessary granularity. Let me say this again: YOU CAN IGNORE ALL
|
|
69 ;; BYTE-COMPILER WARNINGS (you'd be surprised at how many people don't
|
|
70 ;; follow this advice :-).
|
|
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/ftp/emacs/
|
|
80
|
|
81 ;; Or if you don't have access to the World Wide Web, through
|
|
82 ;; anonymous ftp from:
|
|
83 ;;
|
|
84 ;; ftp://ftp.python.org/pub/emacs
|
|
85
|
|
86 ;;; Code:
|
|
87
|
|
88
|
|
89
|
|
90 ;; Figure out what features this Emacs has
|
|
91 (defconst c-emacs-features
|
|
92 (let ((infodock-p (boundp 'infodock-version))
|
|
93 (comments
|
|
94 ;; XEmacs 19 and beyond use 8-bit modify-syntax-entry flags.
|
|
95 ;; Emacs 19 uses a 1-bit flag. We will have to set up our
|
|
96 ;; syntax tables differently to handle this.
|
|
97 (let ((table (copy-syntax-table))
|
|
98 entry)
|
|
99 (modify-syntax-entry ?a ". 12345678" table)
|
|
100 (cond
|
|
101 ;; XEmacs 19, and beyond Emacs 19.34
|
|
102 ((arrayp table)
|
|
103 (setq entry (aref table ?a))
|
|
104 ;; In Emacs, table entries are cons cells
|
|
105 (if (consp entry) (setq entry (car entry))))
|
|
106 ;; XEmacs 20
|
|
107 ((fboundp 'get-char-table) (setq entry (get-char-table ?a table)))
|
|
108 ;; before and including Emacs 19.34
|
|
109 ((and (fboundp 'char-table-p)
|
|
110 (char-table-p table))
|
|
111 (setq entry (car (char-table-range table [?a]))))
|
|
112 ;; incompatible
|
|
113 (t (error "CC Mode is incompatible with this version of Emacs")))
|
|
114 (if (= (logand (lsh entry -16) 255) 255)
|
|
115 '8-bit
|
|
116 '1-bit))))
|
|
117 (if infodock-p
|
|
118 (list comments 'infodock)
|
|
119 (list comments)))
|
|
120 "A list of features extant in the Emacs you are using.
|
|
121 There are many flavors of Emacs out there, each with different
|
|
122 features supporting those needed by CC Mode. Here's the current
|
|
123 supported list, along with the values for this variable:
|
|
124
|
|
125 XEmacs 19: (8-bit)
|
|
126 XEmacs 20: (8-bit)
|
|
127 Emacs 19: (1-bit)
|
|
128
|
|
129 Infodock (based on XEmacs) has an additional symbol on this list:
|
|
130 'infodock.")
|
|
131
|
|
132
|
|
133
|
|
134 ;; important macros and subroutines
|
|
135 (defsubst c-point (position)
|
|
136 ;; Returns the value of point at certain commonly referenced POSITIONs.
|
|
137 ;; POSITION can be one of the following symbols:
|
|
138 ;;
|
|
139 ;; bol -- beginning of line
|
|
140 ;; eol -- end of line
|
|
141 ;; bod -- beginning of defun
|
|
142 ;; boi -- back to indentation
|
|
143 ;; ionl -- indentation of next line
|
|
144 ;; iopl -- indentation of previous line
|
|
145 ;; bonl -- beginning of next line
|
|
146 ;; bopl -- beginning of previous line
|
|
147 ;;
|
|
148 ;; This function does not modify point or mark.
|
|
149 (let ((here (point)))
|
|
150 (cond
|
|
151 ((eq position 'bol) (beginning-of-line))
|
|
152 ((eq position 'eol) (end-of-line))
|
|
153 ((eq position 'bod)
|
|
154 (beginning-of-defun)
|
|
155 ;; if defun-prompt-regexp is non-nil, b-o-d won't leave us at
|
|
156 ;; the open brace.
|
|
157 (and defun-prompt-regexp
|
|
158 (looking-at defun-prompt-regexp)
|
|
159 (goto-char (match-end 0)))
|
|
160 )
|
|
161 ((eq position 'boi) (back-to-indentation))
|
|
162 ((eq position 'bonl) (forward-line 1))
|
|
163 ((eq position 'bopl) (forward-line -1))
|
|
164 ((eq position 'iopl)
|
|
165 (forward-line -1)
|
|
166 (back-to-indentation))
|
|
167 ((eq position 'ionl)
|
|
168 (forward-line 1)
|
|
169 (back-to-indentation))
|
|
170 (t (error "unknown buffer position requested: %s" position))
|
|
171 )
|
|
172 (prog1
|
|
173 (point)
|
|
174 (goto-char here))))
|
|
175
|
|
176 (defmacro c-safe (&rest body)
|
|
177 ;; safely execute BODY, return nil if an error occurred
|
|
178 (` (condition-case nil
|
|
179 (progn (,@ body))
|
|
180 (error nil))))
|
|
181
|
|
182 (defsubst c-keep-region-active ()
|
|
183 ;; Do whatever is necessary to keep the region active in XEmacs.
|
|
184 ;; Ignore byte-compiler warnings you might see. This is not needed
|
|
185 ;; for Emacs.
|
|
186 (and (boundp 'zmacs-region-stays)
|
|
187 (setq zmacs-region-stays t)))
|
|
188
|
|
189
|
|
190
|
|
191 (defsubst c-load-all ()
|
|
192 ;; make sure all necessary components of CC Mode are loaded in.
|
|
193 (require 'cc-vars)
|
|
194 (require 'cc-engine)
|
|
195 (require 'cc-langs)
|
|
196 (require 'cc-menus)
|
|
197 (require 'cc-align)
|
|
198 (require 'cc-styles)
|
|
199 (require 'cc-cmds))
|
|
200
|
|
201
|
|
202 ;;;###autoload
|
|
203 (defun c-mode ()
|
|
204 "Major mode for editing K&R and ANSI C code.
|
|
205 To submit a problem report, enter `\\[c-submit-bug-report]' from a
|
|
206 c-mode buffer. This automatically sets up a mail buffer with version
|
|
207 information already added. You just need to add a description of the
|
|
208 problem, including a reproducible test case and send the message.
|
|
209
|
|
210 To see what version of CC Mode you are running, enter `\\[c-version]'.
|
|
211
|
|
212 The hook variable `c-mode-hook' is run with no args, if that value is
|
|
213 bound and has a non-nil value. Also the hook `c-mode-common-hook' is
|
|
214 run first.
|
|
215
|
|
216 Key bindings:
|
|
217 \\{c-mode-map}"
|
|
218 (interactive)
|
|
219 (c-load-all)
|
|
220 (kill-all-local-variables)
|
|
221 (set-syntax-table c-mode-syntax-table)
|
|
222 (setq major-mode 'c-mode
|
|
223 mode-name "C"
|
|
224 local-abbrev-table c-mode-abbrev-table)
|
|
225 (use-local-map c-mode-map)
|
|
226 (c-common-init)
|
|
227 (setq comment-start "/* "
|
|
228 comment-end " */"
|
|
229 comment-multi-line t
|
|
230 c-conditional-key c-C-conditional-key
|
|
231 c-class-key c-C-class-key
|
|
232 c-baseclass-key nil
|
|
233 c-comment-start-regexp c-C-comment-start-regexp
|
|
234 imenu-generic-expression cc-imenu-c-generic-expression)
|
|
235 (run-hooks 'c-mode-common-hook)
|
|
236 (run-hooks 'c-mode-hook)
|
|
237 (c-update-modeline))
|
|
238
|
|
239
|
|
240 ;;;###autoload
|
|
241 (defun c++-mode ()
|
|
242 "Major mode for editing C++ code.
|
|
243 To submit a problem report, enter `\\[c-submit-bug-report]' from a
|
|
244 c++-mode buffer. This automatically sets up a mail buffer with
|
|
245 version information already added. You just need to add a description
|
|
246 of the problem, including a reproducible test case, and send the
|
|
247 message.
|
|
248
|
|
249 To see what version of CC Mode you are running, enter `\\[c-version]'.
|
|
250
|
|
251 The hook variable `c++-mode-hook' is run with no args, if that
|
|
252 variable is bound and has a non-nil value. Also the hook
|
|
253 `c-mode-common-hook' is run first.
|
|
254
|
|
255 Key bindings:
|
|
256 \\{c++-mode-map}"
|
|
257 (interactive)
|
|
258 (c-load-all)
|
|
259 (kill-all-local-variables)
|
|
260 (set-syntax-table c++-mode-syntax-table)
|
|
261 (setq major-mode 'c++-mode
|
|
262 mode-name "C++"
|
|
263 local-abbrev-table c++-mode-abbrev-table)
|
|
264 (use-local-map c++-mode-map)
|
|
265 (c-common-init)
|
|
266 (setq comment-start "// "
|
|
267 comment-end ""
|
|
268 comment-multi-line nil
|
|
269 c-conditional-key c-C++-conditional-key
|
|
270 c-comment-start-regexp c-C++-comment-start-regexp
|
|
271 c-class-key c-C++-class-key
|
|
272 c-access-key c-C++-access-key
|
|
273 c-double-slash-is-comments-p t
|
|
274 c-recognize-knr-p nil
|
|
275 imenu-generic-expression cc-imenu-c++-generic-expression)
|
|
276 (run-hooks 'c-mode-common-hook)
|
|
277 (run-hooks 'c++-mode-hook)
|
|
278 (c-update-modeline))
|
|
279
|
|
280
|
|
281 ;;;###autoload
|
|
282 (defun objc-mode ()
|
|
283 "Major mode for editing Objective C code.
|
|
284 To submit a problem report, enter `\\[c-submit-bug-report]' from an
|
|
285 objc-mode buffer. This automatically sets up a mail buffer with
|
|
286 version information already added. You just need to add a description
|
|
287 of the problem, including a reproducible test case, and send the
|
|
288 message.
|
|
289
|
|
290 To see what version of CC Mode you are running, enter `\\[c-version]'.
|
|
291
|
|
292 The hook variable `objc-mode-hook' is run with no args, if that value
|
|
293 is bound and has a non-nil value. Also the hook `c-mode-common-hook'
|
|
294 is run first.
|
|
295
|
|
296 Key bindings:
|
|
297 \\{objc-mode-map}"
|
|
298 (interactive)
|
|
299 (c-load-all)
|
|
300 (kill-all-local-variables)
|
|
301 (set-syntax-table objc-mode-syntax-table)
|
|
302 (setq major-mode 'objc-mode
|
|
303 mode-name "ObjC"
|
|
304 local-abbrev-table objc-mode-abbrev-table)
|
|
305 (use-local-map objc-mode-map)
|
|
306 (c-common-init)
|
|
307 (setq comment-start "// "
|
|
308 comment-end ""
|
|
309 comment-multi-line nil
|
|
310 c-conditional-key c-C-conditional-key
|
|
311 c-comment-start-regexp c-C++-comment-start-regexp
|
|
312 c-class-key c-ObjC-class-key
|
|
313 c-baseclass-key nil
|
|
314 c-access-key c-ObjC-access-key
|
|
315 c-double-slash-is-comments-p t
|
|
316 c-method-key c-ObjC-method-key)
|
|
317 (run-hooks 'c-mode-common-hook)
|
|
318 (run-hooks 'objc-mode-hook)
|
|
319 (c-update-modeline))
|
|
320
|
|
321
|
|
322 ;;;###autoload
|
|
323 (defun java-mode ()
|
|
324 "Major mode for editing Java code.
|
|
325 To submit a problem report, enter `\\[c-submit-bug-report]' from an
|
|
326 java-mode buffer. This automatically sets up a mail buffer with
|
|
327 version information already added. You just need to add a description
|
|
328 of the problem, including a reproducible test case and send the
|
|
329 message.
|
|
330
|
|
331 To see what version of CC Mode you are running, enter `\\[c-version]'.
|
|
332
|
|
333 The hook variable `java-mode-hook' is run with no args, if that value
|
|
334 is bound and has a non-nil value. Also the common hook
|
|
335 `c-mode-common-hook' is run first. Note that this mode automatically
|
|
336 sets the \"java\" style before calling any hooks so be careful if you
|
|
337 set styles in `c-mode-common-hook'.
|
|
338
|
|
339 Key bindings:
|
|
340 \\{java-mode-map}"
|
|
341 (interactive)
|
|
342 (c-load-all)
|
|
343 (kill-all-local-variables)
|
|
344 (set-syntax-table java-mode-syntax-table)
|
|
345 (setq major-mode 'java-mode
|
|
346 mode-name "Java"
|
|
347 local-abbrev-table java-mode-abbrev-table)
|
|
348 (use-local-map java-mode-map)
|
|
349 (c-common-init)
|
|
350 (setq comment-start "// "
|
|
351 comment-end ""
|
|
352 comment-multi-line nil
|
|
353 c-conditional-key c-Java-conditional-key
|
|
354 c-comment-start-regexp c-Java-comment-start-regexp
|
|
355 c-class-key c-Java-class-key
|
|
356 c-method-key c-Java-method-key
|
|
357 c-double-slash-is-comments-p t
|
|
358 c-baseclass-key nil
|
|
359 c-recognize-knr-p nil
|
|
360 c-access-key c-Java-access-key
|
|
361 ;defun-prompt-regexp c-Java-defun-prompt-regexp
|
|
362 imenu-generic-expression cc-imenu-java-generic-expression
|
|
363 )
|
|
364 (c-set-style "java")
|
|
365 (run-hooks 'c-mode-common-hook)
|
|
366 (run-hooks 'java-mode-hook)
|
|
367 (c-update-modeline))
|
|
368
|
|
369
|
|
370 ;; defuns for submitting bug reports
|
|
371 (defconst c-version "5.11"
|
|
372 "CC Mode version number.")
|
|
373
|
|
374 (defconst c-mode-help-address
|
|
375 "bug-gnu-emacs@prep.ai.mit.edu, cc-mode-help@python.org"
|
|
376 "Address for CC Mode bug reports.")
|
|
377
|
|
378 (defun c-version ()
|
|
379 "Echo the current version of CC Mode in the minibuffer."
|
|
380 (interactive)
|
|
381 (message "Using CC Mode version %s" c-version)
|
|
382 (c-keep-region-active))
|
|
383
|
|
384 ;; Get reporter-submit-bug-report when byte-compiling
|
|
385 (eval-when-compile
|
|
386 (require 'reporter))
|
|
387
|
|
388 (defun c-submit-bug-report ()
|
|
389 "Submit via mail a bug report on CC Mode."
|
|
390 (interactive)
|
|
391 (require 'cc-vars)
|
|
392 ;; load in reporter
|
|
393 (let ((reporter-prompt-for-summary-p t)
|
|
394 (reporter-dont-compact-list '(c-offsets-alist))
|
|
395 (style c-indentation-style)
|
|
396 (hook c-special-indent-hook)
|
|
397 (c-features c-emacs-features))
|
|
398 (and
|
|
399 (if (y-or-n-p "Do you want to submit a report on CC Mode? ")
|
|
400 t (message "") nil)
|
|
401 (require 'reporter)
|
|
402 (reporter-submit-bug-report
|
|
403 c-mode-help-address
|
|
404 (concat "CC Mode " c-version " ("
|
|
405 (cond ((eq major-mode 'c++-mode) "C++")
|
|
406 ((eq major-mode 'c-mode) "C")
|
|
407 ((eq major-mode 'objc-mode) "ObjC")
|
|
408 ((eq major-mode 'java-mode) "Java")
|
|
409 )
|
|
410 ")")
|
|
411 (let ((vars (list
|
|
412 ;; report only the vars that affect indentation
|
|
413 'c-basic-offset
|
|
414 'c-offsets-alist
|
|
415 'c-cleanup-list
|
|
416 'c-comment-only-line-offset
|
|
417 'c-backslash-column
|
|
418 'c-delete-function
|
|
419 'c-electric-pound-behavior
|
|
420 'c-hanging-braces-alist
|
|
421 'c-hanging-colons-alist
|
|
422 'c-hanging-comment-starter-p
|
|
423 'c-hanging-comment-ender-p
|
|
424 'c-indent-comments-syntactically-p
|
|
425 'c-tab-always-indent
|
|
426 'c-recognize-knr-p
|
|
427 'c-label-minimum-indentation
|
|
428 'defun-prompt-regexp
|
|
429 'tab-width
|
|
430 )))
|
|
431 (if (not (boundp 'defun-prompt-regexp))
|
|
432 (delq 'defun-prompt-regexp vars)
|
|
433 vars))
|
|
434 (function
|
|
435 (lambda ()
|
|
436 (insert
|
|
437 "Buffer Style: " style "\n\n"
|
|
438 (if hook
|
|
439 (concat "\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
|
|
440 "c-special-indent-hook is set to '"
|
|
441 (format "%s" hook)
|
|
442 ".\nPerhaps this is your problem?\n"
|
|
443 "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n")
|
|
444 "\n")
|
|
445 (format "c-emacs-features: %s\n" c-features)
|
|
446 )))
|
|
447 nil
|
|
448 "Dear Barry,"
|
|
449 ))))
|
|
450
|
|
451
|
|
452 (provide 'cc-mode)
|
|
453 ;;; cc-mode.el ends here
|