comparison lisp/cc-mode/cc-langs.el @ 165:5a88923fcbfe r20-3b9

Import from CVS: tag r20-3b9
author cvs
date Mon, 13 Aug 2007 09:44:42 +0200
parents
children 85ec50267440
comparison
equal deleted inserted replaced
164:4e0740e5aab2 165:5a88923fcbfe
1 ;;; cc-langs.el --- specific language support for CC Mode
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: 22-Apr-1997 (split from cc-mode.el)
10 ;; Version: 5.11
11 ;; Keywords: c languages oop
12
13 ;; This file is part of GNU Emacs.
14
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; any later version.
19
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 ;; Boston, MA 02111-1307, USA.
29
30 (eval-when-compile
31 (load-file "./cc-styles.el"))
32
33
34 ;; Regular expressions and other values which must be parameterized on
35 ;; a per-language basis.
36
37 ;; Keywords defining protection levels
38 (defconst c-protection-key "\\<\\(public\\|protected\\|private\\)\\>")
39
40 ;; Regex describing a `symbol' in all languages We cannot use just
41 ;; `word' syntax class since `_' cannot be in word class. Putting
42 ;; underscore in word class breaks forward word movement behavior that
43 ;; users are familiar with.
44 (defconst c-symbol-key "\\(\\w\\|\\s_\\)+")
45
46
47 ;; keywords introducing class definitions. language specific
48 (defconst c-C-class-key "\\(struct\\|union\\)")
49 (defconst c-C++-class-key "\\(class\\|struct\\|union\\)")
50
51 (defconst c-ObjC-class-key
52 (concat
53 "@\\(interface\\|implementation\\)\\s +"
54 c-symbol-key ;name of the class
55 "\\(\\s *:\\s *" c-symbol-key "\\)?" ;maybe followed by the superclass
56 "\\(\\s *<[^>]+>\\)?" ;and maybe the adopted protocols list
57 ))
58
59 (defconst c-Java-class-key
60 (concat
61 "\\(" c-protection-key "\\s +\\)?"
62 "\\(interface\\|class\\)\\s +"
63 c-symbol-key ;name of the class
64 "\\(\\s *extends\\s *" c-symbol-key "\\)?" ;maybe followed by superclass
65 ;;"\\(\\s *implements *[^{]+{\\)?" ;maybe the adopted protocols list
66 ))
67
68 (defvar c-class-key c-C-class-key)
69 (make-variable-buffer-local 'c-class-key)
70
71
72 ;; regexp describing access protection clauses. language specific
73 (defvar c-access-key nil)
74 (make-variable-buffer-local 'c-access-key)
75 (defconst c-C++-access-key (concat c-protection-key "[ \t]*:"))
76 (defconst c-ObjC-access-key (concat "@" c-protection-key))
77 (defconst c-Java-access-key nil)
78
79
80 ;; keywords introducing conditional blocks
81 (defconst c-C-conditional-key nil)
82 (defconst c-C++-conditional-key nil)
83 (defconst c-Java-conditional-key nil)
84
85 (let ((all-kws "for\\|if\\|do\\|else\\|while\\|switch")
86 (exc-kws "\\|try\\|catch")
87 (thr-kws "\\|finally\\|synchronized")
88 (front "\\b\\(")
89 (back "\\)\\b[^_]"))
90 (setq c-C-conditional-key (concat front all-kws back)
91 c-C++-conditional-key (concat front all-kws exc-kws back)
92 c-Java-conditional-key (concat front all-kws exc-kws thr-kws back)))
93
94 (defvar c-conditional-key c-C-conditional-key)
95 (make-variable-buffer-local 'c-conditional-key)
96
97
98 ;; keywords describing method definition introductions
99 (defvar c-method-key nil)
100 (make-variable-buffer-local 'c-method-key)
101
102 (defconst c-ObjC-method-key
103 (concat
104 "^\\s *[+-]\\s *"
105 "\\(([^)]*)\\)?" ; return type
106 ;; \\s- in objc syntax table does not include \n
107 ;; since it is considered the end of //-comments.
108 "[ \t\n]*" c-symbol-key))
109
110 (defconst c-Java-method-key
111 (concat
112 "^\\s *[+-]\\s *"
113 "\\(([^)]*)\\)?" ; return type
114 ;; \\s- in java syntax table does not include \n
115 ;; since it is considered the end of //-comments.
116 "[ \t\n]*" c-symbol-key))
117
118
119 ;; comment starter definitions for various languages. language specific
120 (defconst c-C-comment-start-regexp "/[*]")
121 (defconst c-C++-comment-start-regexp "/[/*]")
122 ;; We need to match all 3 Java style comments
123 ;; 1) Traditional C block; 2) javadoc /** ...; 3) C++ style
124 (defconst c-Java-comment-start-regexp "/\\(/\\|[*][*]?\\)")
125 (defvar c-comment-start-regexp c-C-comment-start-regexp)
126 (make-variable-buffer-local 'c-comment-start-regexp)
127
128
129
130 ;; Regexp describing a switch's case or default label for all languages
131 (defconst c-switch-label-key "\\(\\(case[( \t]+\\S .*\\)\\|default[ \t]*\\):")
132 ;; Regexp describing any label.
133 (defconst c-label-key (concat c-symbol-key ":\\([^:]\\|$\\)"))
134
135 ;; Regexp describing class inheritance declarations. TBD: this should
136 ;; be language specific, and only makes sense for C++
137 (defconst c-inher-key
138 (concat "\\(\\<static\\>\\s +\\)?"
139 c-C++-class-key "[ \t]+" c-symbol-key
140 "\\([ \t]*:[ \t]*\\)\\s *[^;]"))
141
142 ;; Regexp describing C++ base classes in a derived class definition.
143 ;; TBD: this should be language specific, and only makes sense for C++
144 (defvar c-baseclass-key
145 (concat
146 ":?[ \t]*\\(virtual[ \t]+\\)?\\("
147 c-protection-key "[ \t]+\\)" c-symbol-key))
148 (make-variable-buffer-local 'c-baseclass-key)
149
150 ;; Regexp describing friend declarations in C++ classes.
151 (defconst c-C++-friend-key
152 "friend[ \t]+\\|template[ \t]*<.+>[ \t]*friend[ \t]+")
153
154 ;; Regexp describing Java inheritance and throws clauses.
155 (defconst c-Java-special-key "\\(implements\\|extends\\|throws\\)[^_]")
156
157 ;; Regexp describing the beginning of a Java top-level definition.
158 (defconst c-Java-defun-prompt-regexp
159 "^[ \t]*\\(\\(\\(public\\|protected\\|private\\|const\\|abstract\\|synchronized\\|final\\|static\\|threadsafe\\|transient\\|native\\|volatile\\)\\s-+\\)*\\(\\(\\([[a-zA-Z][][_$.a-zA-Z0-9]*[][_$.a-zA-Z0-9]+\\|[[a-zA-Z]\\)\\s-*\\)\\s-+\\)\\)?\\(\\([[a-zA-Z][][_$.a-zA-Z0-9]*\\s-+\\)\\s-*\\)?\\([_a-zA-Z][^][ \t:;.,{}()=]*\\|\\([_$a-zA-Z][_$.a-zA-Z0-9]*\\)\\)\\s-*\\(([^);{}]*)\\)?\\([] \t]*\\)\\(\\s-*\\<throws\\>\\s-*\\(\\([_$a-zA-Z][_$.a-zA-Z0-9]*\\)[, \t\n\r\f]*\\)+\\)?\\s-*")
160
161
162
163 ;; internal state variables
164
165 ;; Internal state of hungry delete key feature
166 (defvar c-hungry-delete-key nil)
167 (make-variable-buffer-local 'c-hungry-delete-key)
168
169 ;; Internal state of auto newline feature.
170 (defvar c-auto-newline nil)
171 (make-variable-buffer-local 'c-auto-newline)
172
173 ;; Internal auto-newline/hungry-delete designation string for mode line.
174 (defvar c-auto-hungry-string nil)
175 (make-variable-buffer-local 'c-auto-hungry-string)
176
177 ;; Buffer local language-specific comment style flag.
178 (defvar c-double-slash-is-comments-p nil)
179 (make-variable-buffer-local 'c-double-slash-is-comments-p)
180
181 ;; Non-nil means K&R style argument declarations are valid.
182 (defvar c-recognize-knr-p t)
183 (make-variable-buffer-local 'c-recognize-knr-p)
184
185
186
187 (defun c-use-java-style ()
188 "Institutes `java' indentation style.
189 For use with the variable `java-mode-hook'."
190 (c-set-style "java"))
191
192 (defun c-common-init ()
193 ;; Common initializations for c++-mode and c-mode.
194 ;;
195 ;; these variables should always be buffer local; they do not affect
196 ;; indentation style.
197 (make-local-variable 'paragraph-start)
198 (make-local-variable 'paragraph-separate)
199 (make-local-variable 'paragraph-ignore-fill-prefix)
200 (make-local-variable 'require-final-newline)
201 (make-local-variable 'parse-sexp-ignore-comments)
202 (make-local-variable 'indent-line-function)
203 (make-local-variable 'indent-region-function)
204 (make-local-variable 'comment-start)
205 (make-local-variable 'comment-end)
206 (make-local-variable 'comment-column)
207 (make-local-variable 'comment-start-skip)
208 (make-local-variable 'comment-multi-line)
209 (make-local-variable 'outline-regexp)
210 (make-local-variable 'outline-level)
211 (make-local-variable 'adaptive-fill-regexp)
212 (make-local-variable 'imenu-generic-expression) ;set in the mode functions
213 ;; Emacs 19.30 and beyond only, AFAIK
214 (if (boundp 'fill-paragraph-function)
215 (progn
216 (make-local-variable 'fill-paragraph-function)
217 (setq fill-paragraph-function 'c-fill-paragraph)))
218 ;; now set their values
219 (setq paragraph-start (concat page-delimiter "\\|$")
220 paragraph-separate paragraph-start
221 paragraph-ignore-fill-prefix t
222 require-final-newline t
223 parse-sexp-ignore-comments t
224 indent-line-function 'c-indent-line
225 indent-region-function 'c-indent-region
226 outline-regexp "[^#\n\^M]"
227 outline-level 'c-outline-level
228 comment-column 32
229 comment-start-skip "/\\*+ *\\|// *"
230 adaptive-fill-regexp nil)
231 ;; we have to do something special for c-offsets-alist so that the
232 ;; buffer local value has its own alist structure.
233 (setq c-offsets-alist (copy-alist c-offsets-alist))
234 ;; setup the comment indent variable in a Emacs version portable way
235 ;; ignore any byte compiler warnings you might get here
236 (make-local-variable 'comment-indent-function)
237 (setq comment-indent-function 'c-comment-indent)
238 ;; add menus to menubar
239 (easy-menu-add (c-mode-menu mode-name))
240 ;; put auto-hungry designators onto minor-mode-alist, but only once
241 (or (assq 'c-auto-hungry-string minor-mode-alist)
242 (setq minor-mode-alist
243 (cons '(c-auto-hungry-string c-auto-hungry-string)
244 minor-mode-alist))))
245
246 (defun c-postprocess-file-styles ()
247 "Function that post processes relevant file local variables.
248 Currently, this function simply applies any style and offset settings
249 found in the file's Local Variable list. It first applies any style
250 setting found in `c-file-style', then it applies any offset settings
251 it finds in `c-file-offsets'."
252 ;; apply file styles and offsets
253 (and c-file-style
254 (c-set-style c-file-style))
255 (and c-file-offsets
256 (mapcar
257 (function
258 (lambda (langentry)
259 (let ((langelem (car langentry))
260 (offset (cdr langentry)))
261 (c-set-offset langelem offset)
262 )))
263 c-file-offsets)))
264
265 (add-hook 'hack-local-variables-hook 'c-postprocess-file-styles)
266
267
268 ;; Common routines
269 (defsubst c-make-inherited-keymap ()
270 (let ((map (make-sparse-keymap)))
271 (cond
272 ;; XEmacs 19 & 20
273 ((fboundp 'set-keymap-parents)
274 (set-keymap-parents map c-mode-base-map))
275 ;; Emacs 19
276 ((fboundp 'set-keymap-parent)
277 (set-keymap-parent map c-mode-base-map))
278 ;; incompatible
279 (t (error "CC Mode is incompatible with this version of Emacs")))
280 map))
281
282 (defun c-populate-syntax-table (table)
283 ;; Populate the syntax TABLE
284 ;; DO NOT TRY TO SET _ (UNDERSCORE) TO WORD CLASS!
285 (modify-syntax-entry ?_ "_" table)
286 (modify-syntax-entry ?\\ "\\" table)
287 (modify-syntax-entry ?+ "." table)
288 (modify-syntax-entry ?- "." table)
289 (modify-syntax-entry ?= "." table)
290 (modify-syntax-entry ?% "." table)
291 (modify-syntax-entry ?< "." table)
292 (modify-syntax-entry ?> "." table)
293 (modify-syntax-entry ?& "." table)
294 (modify-syntax-entry ?| "." table)
295 (modify-syntax-entry ?\' "\"" table))
296
297 (defun c-setup-dual-comments (table)
298 ;; Set up TABLE to handle block and line style comments
299 (cond
300 ;; XEmacs 19 & 20
301 ((memq '8-bit c-emacs-features)
302 (modify-syntax-entry ?/ ". 1456" table)
303 (modify-syntax-entry ?* ". 23" table)
304 (modify-syntax-entry ?\n "> b" table)
305 ;; Give CR the same syntax as newline, for selective-display
306 (modify-syntax-entry ?\^m "> b" table))
307 ;; Emacs 19
308 ((memq '1-bit c-emacs-features)
309 (modify-syntax-entry ?/ ". 124b" table)
310 (modify-syntax-entry ?* ". 23" table)
311 (modify-syntax-entry ?\n "> b" table)
312 ;; Give CR the same syntax as newline, for selective-display
313 (modify-syntax-entry ?\^m "> b" table))
314 ;; incompatible
315 (t (error "CC Mode is incompatible with this version of Emacs"))
316 ))
317
318 (defvar c-mode-base-map ()
319 "Keymap shared by all CC Mode related modes.")
320
321 (if c-mode-base-map
322 nil
323 ;; TBD: should we even worry about naming this keymap. My vote: no,
324 ;; because Emacs and XEmacs do it differently.
325 (setq c-mode-base-map (make-sparse-keymap))
326 ;; put standard keybindings into MAP
327 ;; the following mappings correspond more or less directly to BOCM
328 (define-key c-mode-base-map "{" 'c-electric-brace)
329 (define-key c-mode-base-map "}" 'c-electric-brace)
330 (define-key c-mode-base-map ";" 'c-electric-semi&comma)
331 (define-key c-mode-base-map "#" 'c-electric-pound)
332 (define-key c-mode-base-map ":" 'c-electric-colon)
333 ;; Lucid Emacs 19.9 defined these two, the second of which was
334 ;; commented out...
335 ;; (define-key c-mode-base-map "\e{" 'c-insert-braces)
336 ;; Commented out electric square brackets because nobody likes them.
337 ;; (define-key c-mode-base-map "[" 'c-insert-brackets)
338 (define-key c-mode-base-map "\C-c\C-m" 'c-mark-function)
339 (define-key c-mode-base-map "\e\C-q" 'c-indent-exp)
340 (define-key c-mode-base-map "\ea" 'c-beginning-of-statement)
341 (define-key c-mode-base-map "\ee" 'c-end-of-statement)
342 (define-key c-mode-base-map "\C-c\C-n" 'c-forward-conditional)
343 (define-key c-mode-base-map "\C-c\C-p" 'c-backward-conditional)
344 (define-key c-mode-base-map "\C-c\C-u" 'c-up-conditional)
345 (define-key c-mode-base-map "\t" 'c-indent-command)
346 ;; In XEmacs 19 and Emacs 19, this binds both the BackSpace and
347 ;; Delete keysyms to c-electric-backspace. In XEmacs 20 it binds
348 ;; only BackSpace, so we now bind them individually
349 (define-key c-mode-base-map [delete] 'c-electric-delete)
350 (define-key c-mode-base-map [backspace] 'c-electric-backspace)
351 ;; these are new keybindings, with no counterpart to BOCM
352 (define-key c-mode-base-map "," 'c-electric-semi&comma)
353 (define-key c-mode-base-map "*" 'c-electric-star)
354 (define-key c-mode-base-map "\C-c\C-q" 'c-indent-defun)
355 (define-key c-mode-base-map "\C-c\C-\\" 'c-backslash-region)
356 ;; TBD: where if anywhere, to put c-backward|forward-into-nomenclature
357 (define-key c-mode-base-map "\C-c\C-a" 'c-toggle-auto-state)
358 (define-key c-mode-base-map "\C-c\C-b" 'c-submit-bug-report)
359 (define-key c-mode-base-map "\C-c\C-c" 'comment-region)
360 (define-key c-mode-base-map "\C-c\C-d" 'c-toggle-hungry-state)
361 (define-key c-mode-base-map "\C-c\C-e" 'c-macro-expand)
362 (define-key c-mode-base-map "\C-c\C-o" 'c-set-offset)
363 (define-key c-mode-base-map "\C-c\C-s" 'c-show-syntactic-information)
364 (define-key c-mode-base-map "\C-c\C-t" 'c-toggle-auto-hungry-state)
365 (define-key c-mode-base-map "\C-c." 'c-set-style)
366 ;; conflicts with OOBR
367 ;;(define-key c-mode-base-map "\C-c\C-v" 'c-version)
368 )
369
370
371
372 ;; Support for C
373
374 (defvar c-mode-abbrev-table nil
375 "Abbrev table in use in c-mode buffers.")
376 (define-abbrev-table 'c-mode-abbrev-table ())
377
378 (defvar c-mode-map ()
379 "Keymap used in c-mode buffers.")
380 (if c-mode-map
381 nil
382 (setq c-mode-map (c-make-inherited-keymap))
383 ;; add bindings which are only useful for C
384 )
385
386 (defvar c-mode-syntax-table nil
387 "Syntax table used in c-mode buffers.")
388 (if c-mode-syntax-table
389 ()
390 (setq c-mode-syntax-table (make-syntax-table))
391 (c-populate-syntax-table c-mode-syntax-table)
392 ;; add extra comment syntax
393 (modify-syntax-entry ?/ ". 14" c-mode-syntax-table)
394 (modify-syntax-entry ?* ". 23" c-mode-syntax-table))
395
396 (defun c-enable-//-in-c-mode ()
397 "Enables // as a comment delimiter in `c-mode'.
398 ANSI C currently does *not* allow this, although many C compilers
399 support optional C++ style comments. To use, call this function from
400 your `.emacs' file before you visit any C files. The changes are
401 global and affect all future `c-mode' buffers."
402 (c-setup-dual-comments c-mode-syntax-table)
403 (setq-default c-C-comment-start-regexp c-C++-comment-start-regexp))
404
405
406
407 ;; Support for C++
408
409 (defvar c++-mode-abbrev-table nil
410 "Abbrev table in use in c++-mode buffers.")
411 (define-abbrev-table 'c++-mode-abbrev-table ())
412
413 (defvar c++-mode-map ()
414 "Keymap used in c++-mode buffers.")
415 (if c++-mode-map
416 nil
417 (setq c++-mode-map (c-make-inherited-keymap))
418 ;; add bindings which are only useful for C++
419 (define-key c++-mode-map "\C-c:" 'c-scope-operator)
420 (define-key c++-mode-map "/" 'c-electric-slash)
421 (define-key c++-mode-map "<" 'c-electric-lt-gt)
422 (define-key c++-mode-map ">" 'c-electric-lt-gt))
423
424 (defvar c++-mode-syntax-table nil
425 "Syntax table used in c++-mode buffers.")
426 (if c++-mode-syntax-table
427 ()
428 (setq c++-mode-syntax-table (make-syntax-table))
429 (c-populate-syntax-table c++-mode-syntax-table)
430 ;; add extra comment syntax
431 (c-setup-dual-comments c++-mode-syntax-table)
432 ;; TBD: does it make sense for colon to be symbol class in C++?
433 ;; I'm not so sure, since c-label-key is busted on lines like:
434 ;; Foo::bar( i );
435 ;; maybe c-label-key should be fixed instead of commenting this out,
436 ;; but it also bothers me that this only seems appropriate for C++
437 ;; and not C.
438 ;;(modify-syntax-entry ?: "_" c++-mode-syntax-table)
439 )
440
441
442
443 ;; Support for Objective-C
444
445 (defvar objc-mode-abbrev-table nil
446 "Abbrev table in use in objc-mode buffers.")
447 (define-abbrev-table 'objc-mode-abbrev-table ())
448
449 (defvar objc-mode-map ()
450 "Keymap used in objc-mode buffers.")
451 (if objc-mode-map
452 nil
453 (setq objc-mode-map (c-make-inherited-keymap))
454 ;; add bindings which are only useful for Objective-C
455 (define-key objc-mode-map "/" 'c-electric-slash))
456
457 (defvar objc-mode-syntax-table nil
458 "Syntax table used in objc-mode buffers.")
459 (if objc-mode-syntax-table
460 ()
461 (setq objc-mode-syntax-table (make-syntax-table))
462 (c-populate-syntax-table objc-mode-syntax-table)
463 ;; add extra comment syntax
464 (c-setup-dual-comments objc-mode-syntax-table)
465 ;; everyone gets these
466 (modify-syntax-entry ?@ "_" objc-mode-syntax-table)
467 )
468
469
470
471 ;; Support for Java
472
473 (defvar java-mode-abbrev-table nil
474 "Abbrev table in use in java-mode buffers.")
475 (define-abbrev-table 'java-mode-abbrev-table ())
476
477 (defvar java-mode-map ()
478 "Keymap used in java-mode buffers.")
479 (if java-mode-map
480 nil
481 (setq java-mode-map (c-make-inherited-keymap))
482 ;; add bindings which are only useful for Java
483 (define-key java-mode-map "/" 'c-electric-slash))
484
485 (defvar java-mode-syntax-table nil
486 "Syntax table used in java-mode buffers.")
487 (if java-mode-syntax-table
488 ()
489 (setq java-mode-syntax-table (make-syntax-table))
490 (c-populate-syntax-table java-mode-syntax-table)
491 ;; add extra comment syntax
492 (c-setup-dual-comments java-mode-syntax-table)
493 ;; everyone gets these
494 (modify-syntax-entry ?@ "_" java-mode-syntax-table)
495 )
496
497
498
499 (provide 'cc-langs)
500 ;;; cc-langs.el ends here