1 ;;; cc-langs.el --- specific language support for CC Mode
3 ;; Copyright (C) 1985,87,92,93,94,95,96,97,98 Free Software Foundation, Inc.
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: See cc-mode.el
11 ;; Keywords: c languages oop
13 ;; This file is part of GNU Emacs.
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)
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.
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.
34 ;; Regular expressions and other values which must be parameterized on
35 ;; a per-language basis.
37 ;; Keywords defining protection levels
38 (defconst c-protection-key
"\\<\\(public\\|protected\\|private\\)\\>")
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. Besides, this runs counter to Emacs
46 ;; I suspect this definition isn't correct in light of Java's
47 ;; definition of a symbol as being Unicode. I know so little about
48 ;; I18N (except how to sound cool and say I18N :-) that I'm willing to
49 ;; punt on this for now.
51 (defconst c-symbol-key
"[_a-zA-Z]\\(\\w\\|\\s_\\)*")
54 ;; keywords introducing class definitions. language specific
55 (defconst c-C-class-key
"\\(struct\\|union\\)")
56 (defconst c-C
++-class-key
"\\(class\\|struct\\|union\\)")
57 (defconst c-C-extra-toplevel-key
"\\(extern\\)[^_]")
58 (defconst c-C
++-extra-toplevel-key
"\\(extern\\|namespace\\)[^_]")
60 (defconst c-ObjC-class-key
62 "@\\(interface\\|implementation\\)\\s +"
63 c-symbol-key
;name of the class
64 "\\(\\s *:\\s *" c-symbol-key
"\\)?" ;maybe followed by the superclass
65 "\\(\\s *<[^>]+>\\)?" ;and maybe the adopted protocols list
68 (defconst c-Java-class-key
70 "\\(" c-protection-key
"\\s +\\)?"
71 "\\(interface\\|class\\)\\s +"
72 c-symbol-key
;name of the class
73 "\\(\\s *extends\\s *" c-symbol-key
"\\)?" ;maybe followed by superclass
74 ;;"\\(\\s *implements *[^{]+{\\)?" ;maybe the adopted protocols list
77 (defvar c-class-key c-C-class-key
)
78 (make-variable-buffer-local 'c-class-key
)
80 (defvar c-extra-toplevel-key c-C-extra-toplevel-key
)
81 (make-variable-buffer-local 'c-extra-toplevel-key
)
84 ;; regexp describing access protection clauses. language specific
85 (defvar c-access-key nil
)
86 (make-variable-buffer-local 'c-access-key
)
87 (defconst c-C
++-access-key
(concat c-protection-key
"[ \t]*:"))
88 (defconst c-ObjC-access-key
(concat "@" c-protection-key
))
89 (defconst c-Java-access-key nil
)
92 ;; keywords introducing conditional blocks
93 (defconst c-C-conditional-key nil
)
94 (defconst c-C
++-conditional-key nil
)
95 (defconst c-Java-conditional-key nil
)
97 (let ((all-kws "for\\|if\\|do\\|else\\|while\\|switch")
98 (exc-kws "\\|try\\|catch")
99 (thr-kws "\\|finally\\|synchronized")
102 (setq c-C-conditional-key
(concat front all-kws back
)
103 c-C
++-conditional-key
(concat front all-kws exc-kws back
)
104 c-Java-conditional-key
(concat front all-kws exc-kws thr-kws back
)))
106 (defvar c-conditional-key c-C-conditional-key
)
107 (make-variable-buffer-local 'c-conditional-key
)
110 ;; keywords describing method definition introductions
111 (defvar c-method-key nil
)
112 (make-variable-buffer-local 'c-method-key
)
114 (defconst c-ObjC-method-key
117 "\\(([^)]*)\\)?" ; return type
118 ;; \\s- in objc syntax table does not include \n
119 ;; since it is considered the end of //-comments.
120 "[ \t\n]*" c-symbol-key
))
124 ;; comment starter definitions for various languages. language specific
125 (defconst c-C
++-comment-start-regexp
"/[/*]")
126 ;; We need to match all 3 Java style comments
127 ;; 1) Traditional C block; 2) javadoc /** ...; 3) C++ style
128 (defconst c-Java-comment-start-regexp
"/\\(/\\|[*][*]?\\)")
129 (defvar c-comment-start-regexp c-C
++-comment-start-regexp
)
130 (make-variable-buffer-local 'c-comment-start-regexp
)
134 ;; Regexp describing a switch's case or default label for all languages
135 (defconst c-switch-label-key
"\\(\\(case[( \t]+\\S .*\\)\\|default[ \t]*\\):")
136 ;; Regexp describing any label.
137 (defconst c-label-key
(concat c-symbol-key
":\\([^:]\\|$\\)"))
139 ;; Regexp describing class inheritance declarations. TBD: this should
140 ;; be language specific, and only makes sense for C++
141 (defconst c-inher-key
142 (concat "\\(\\<static\\>\\s +\\)?"
143 c-C
++-class-key
"[ \t]+" c-symbol-key
144 "\\([ \t]*:[ \t]*\\)\\s *[^;]"))
146 ;; Regexp describing C++ base classes in a derived class definition.
147 ;; TBD: this should be language specific, and only makes sense for C++
148 (defvar c-baseclass-key
150 ":?[ \t]*\\(virtual[ \t]+\\)?\\("
151 c-protection-key
"[ \t]+\\)" c-symbol-key
))
152 (make-variable-buffer-local 'c-baseclass-key
)
154 ;; Regexp describing friend declarations in C++ classes.
155 (defconst c-C
++-friend-key
156 "friend[ \t]+\\|template[ \t]*<.+>[ \t]*friend[ \t]+")
158 ;; Regexp describing Java inheritance and throws clauses.
159 (defconst c-Java-special-key
"\\(implements\\|extends\\|throws\\)[^_]")
161 ;; Regexp describing the beginning of a Java top-level definition.
162 (defconst c-Java-defun-prompt-regexp
163 "^[ \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:;.,{}()\7f=]*\\|\\([_$a-zA-Z][_$.a-zA-Z0-9]*\\)\\)\\s-*\\(([^);{}]*)\\)?\\([] \t]*\\)\\(\\s-*\\<throws\\>\\s-*\\(\\([_$a-zA-Z][_$.a-zA-Z0-9]*\\)[, \t\n\r\f]*\\)+\\)?\\s-*")
165 ;; Regexp describing Javadoc markup that always starts paragraphs.
166 (defconst c-Java-javadoc-paragraph-start
167 "@\\(author\\|exception\\|param\\|return\\|see\\|version\\)")
171 ;; internal state variables
173 ;; Internal state of hungry delete key feature
174 (defvar c-hungry-delete-key nil
)
175 (make-variable-buffer-local 'c-hungry-delete-key
)
177 ;; Internal state of auto newline feature.
178 (defvar c-auto-newline nil
)
179 (make-variable-buffer-local 'c-auto-newline
)
181 ;; Internal auto-newline/hungry-delete designation string for mode line.
182 (defvar c-auto-hungry-string nil
)
183 (make-variable-buffer-local 'c-auto-hungry-string
)
185 ;; Non-nil means K&R style argument declarations are valid.
186 (defvar c-recognize-knr-p t
)
187 (make-variable-buffer-local 'c-recognize-knr-p
)
191 (defun c-use-java-style ()
192 "Institutes `java' indentation style.
193 For use with the variable `java-mode-hook'."
194 (c-set-style "java"))
196 (defun c-common-init ()
197 ;; Common initializations for all modes.
198 ;; these variables should always be buffer local; they do not affect
199 ;; indentation style.
200 (make-local-variable 'paragraph-start
)
201 (make-local-variable 'paragraph-separate
)
202 (make-local-variable 'paragraph-ignore-fill-prefix
)
203 (make-local-variable 'require-final-newline
)
204 (make-local-variable 'parse-sexp-ignore-comments
)
205 (make-local-variable 'indent-line-function
)
206 (make-local-variable 'indent-region-function
)
207 (make-local-variable 'comment-start
)
208 (make-local-variable 'comment-end
)
209 (make-local-variable 'comment-column
)
210 (make-local-variable 'comment-start-skip
)
211 (make-local-variable 'comment-multi-line
)
212 (make-local-variable 'outline-regexp
)
213 (make-local-variable 'outline-level
)
214 (make-local-variable 'adaptive-fill-regexp
)
215 (make-local-variable 'imenu-generic-expression
) ;set in the mode functions
217 (and (boundp 'comment-line-break-function
)
218 (make-local-variable 'comment-line-break-function
))
219 ;; Emacs 19.30 and beyond only, AFAIK
220 (if (boundp 'fill-paragraph-function
)
222 (make-local-variable 'fill-paragraph-function
)
223 (setq fill-paragraph-function
'c-fill-paragraph
)))
224 ;; now set their values
225 (setq paragraph-start
(concat page-delimiter
"\\|$")
226 paragraph-separate paragraph-start
227 paragraph-ignore-fill-prefix t
228 require-final-newline t
229 parse-sexp-ignore-comments t
230 indent-line-function
'c-indent-line
231 indent-region-function
'c-indent-region
232 outline-regexp
"[^#\n\^M]"
233 outline-level
'c-outline-level
235 comment-start-skip
"/\\*+ *\\|// *"
236 comment-multi-line nil
237 comment-line-break-function
'c-comment-line-break-function
238 adaptive-fill-regexp nil
)
239 ;; we have to do something special for c-offsets-alist so that the
240 ;; buffer local value has its own alist structure.
241 (setq c-offsets-alist
(copy-alist c-offsets-alist
))
242 ;; setup the comment indent variable in a Emacs version portable way
243 ;; ignore any byte compiler warnings you might get here
244 (make-local-variable 'comment-indent-function
)
245 (setq comment-indent-function
'c-comment-indent
)
246 ;; add menus to menubar
247 (easy-menu-add (c-mode-menu mode-name
))
248 ;; put auto-hungry designators onto minor-mode-alist, but only once
249 (or (assq 'c-auto-hungry-string minor-mode-alist
)
250 (setq minor-mode-alist
251 (cons '(c-auto-hungry-string c-auto-hungry-string
)
254 (defun c-postprocess-file-styles ()
255 "Function that post processes relevant file local variables.
256 Currently, this function simply applies any style and offset settings
257 found in the file's Local Variable list. It first applies any style
258 setting found in `c-file-style', then it applies any offset settings
259 it finds in `c-file-offsets'.
261 Note that the style variables are always made local to the buffer."
262 ;; apply file styles and offsets
263 (if (or c-file-style c-file-offsets
)
264 (c-make-styles-buffer-local t
))
266 (c-set-style c-file-style
))
271 (let ((langelem (car langentry
))
272 (offset (cdr langentry
)))
273 (c-set-offset langelem offset
)
277 (add-hook 'hack-local-variables-hook
'c-postprocess-file-styles
)
281 (defun c-make-inherited-keymap ()
282 (let ((map (make-sparse-keymap)))
285 ((fboundp 'set-keymap-parents
)
286 (set-keymap-parents map c-mode-base-map
))
288 ((fboundp 'set-keymap-parent
)
289 (set-keymap-parent map c-mode-base-map
))
291 (t (error "CC Mode is incompatible with this version of Emacs")))
294 (defun c-populate-syntax-table (table)
295 ;; Populate the syntax TABLE
296 ;; DO NOT TRY TO SET _ (UNDERSCORE) TO WORD CLASS!
297 (modify-syntax-entry ?_
"_" table
)
298 (modify-syntax-entry ?
\\ "\\" table
)
299 (modify-syntax-entry ?
+ "." table
)
300 (modify-syntax-entry ?-
"." table
)
301 (modify-syntax-entry ?
= "." table
)
302 (modify-syntax-entry ?%
"." table
)
303 (modify-syntax-entry ?
< "." table
)
304 (modify-syntax-entry ?
> "." table
)
305 (modify-syntax-entry ?
& "." table
)
306 (modify-syntax-entry ?|
"." table
)
307 (modify-syntax-entry ?
\' "\"" table
)
308 ;; Set up block and line oriented comments. The new C standard
309 ;; mandates both comment styles even in C, so since all languages
310 ;; now require dual comments, we make this the default.
313 ((memq '8-bit c-emacs-features
)
314 (modify-syntax-entry ?
/ ". 1456" table
)
315 (modify-syntax-entry ?
* ". 23" table
))
317 ((memq '1-bit c-emacs-features
)
318 (modify-syntax-entry ?
/ ". 124b" table
)
319 (modify-syntax-entry ?
* ". 23" table
))
321 (t (error "CC Mode is incompatible with this version of Emacs"))
323 (modify-syntax-entry ?
\n "> b" table
)
324 ;; Give CR the same syntax as newline, for selective-display
325 (modify-syntax-entry ?\^m
"> b" table
))
328 (defvar c-mode-base-map
()
329 "Keymap shared by all CC Mode related modes.")
333 ;; TBD: should we even worry about naming this keymap. My vote: no,
334 ;; because Emacs and XEmacs do it differently.
335 (setq c-mode-base-map
(make-sparse-keymap))
336 ;; put standard keybindings into MAP
337 ;; the following mappings correspond more or less directly to BOCM
338 (define-key c-mode-base-map
"{" 'c-electric-brace
)
339 (define-key c-mode-base-map
"}" 'c-electric-brace
)
340 (define-key c-mode-base-map
";" 'c-electric-semi
&comma
)
341 (define-key c-mode-base-map
"#" 'c-electric-pound
)
342 (define-key c-mode-base-map
":" 'c-electric-colon
)
343 ;; Separate M-BS from C-M-h. The former should remain
344 ;; backward-kill-word.
345 (define-key c-mode-base-map
[(control meta h
)] 'c-mark-function
)
346 (define-key c-mode-base-map
"\e\C-q" 'c-indent-exp
)
347 (define-key c-mode-base-map
"\ea" 'c-beginning-of-statement
)
348 (define-key c-mode-base-map
"\ee" 'c-end-of-statement
)
349 ;; RMS says don't make these the default.
350 ;; (define-key c-mode-base-map "\e\C-a" 'c-beginning-of-defun)
351 ;; (define-key c-mode-base-map "\e\C-e" 'c-end-of-defun)
352 (define-key c-mode-base-map
"\C-c\C-n" 'c-forward-conditional
)
353 (define-key c-mode-base-map
"\C-c\C-p" 'c-backward-conditional
)
354 (define-key c-mode-base-map
"\C-c\C-u" 'c-up-conditional
)
355 (define-key c-mode-base-map
"\t" 'c-indent-command
)
356 ;; Caution! Enter here at your own risk. We are trying to support
357 ;; several behaviors and it gets disgusting. :-(
359 ;; In XEmacs 19, Emacs 19, and Emacs 20, we use this to bind
360 ;; backwards deletion behavior to DEL, which both Delete and
361 ;; Backspace get translated to. There's no way to separate this
362 ;; behavior in a clean way, so deal with it! Besides, it's been
363 ;; this way since the dawn of BOCM.
364 (if (not (boundp 'delete-key-deletes-forward
))
365 (define-key c-mode-base-map
"\177" 'c-electric-backspace
)
366 ;; However, XEmacs 20 actually achieved enlightenment. It is
367 ;; possible to sanely define both backward and forward deletion
368 ;; behavior under X separately (TTYs are forever beyond hope, but
369 ;; who cares? XEmacs 20 does the right thing with these too).
370 (define-key c-mode-base-map
[delete] 'c-electric-delete)
371 (define-key c-mode-base-map [backspace] 'c-electric-backspace))
372 ;; these are new keybindings, with no counterpart to BOCM
373 (define-key c-mode-base-map "," 'c-electric-semi&comma)
374 (define-key c-mode-base-map "*" 'c-electric-star)
375 (define-key c-mode-base-map "/" 'c-electric-slash)
376 (define-key c-mode-base-map "\C-c\C-q" 'c-indent-defun)
377 (define-key c-mode-base-map "\C-c\C-\\" 'c-backslash-region)
378 ;; TBD: where if anywhere, to put c-backward|forward-into-nomenclature
379 (define-key c-mode-base-map "\C-c\C-a" 'c-toggle-auto-state)
380 (define-key c-mode-base-map "\C-c\C-b" 'c-submit-bug-report)
381 (define-key c-mode-base-map "\C-c\C-c" 'comment-region)
382 (define-key c-mode-base-map "\C-c\C-d" 'c-toggle-hungry-state)
383 (define-key c-mode-base-map "\C-c\C-o" 'c-set-offset)
384 (define-key c-mode-base-map "\C-c\C-s" 'c-show-syntactic-information)
385 (define-key c-mode-base-map "\C-c\C-t" 'c-toggle-auto-hungry-state)
386 (define-key c-mode-base-map "\C-c." 'c-set-style)
387 ;; conflicts with OOBR
388 ;;(define-key c-mode-base-map "\C-c\C-v" 'c-version)
391 ;; menu support for both XEmacs and Emacs. If you don't have easymenu
392 ;; with your version of Emacs, you are incompatible!
395 (defvar c-c-menu nil)
396 (defvar c-c++-menu nil)
397 (defvar c-objc-menu nil)
398 (defvar c-java-menu nil)
400 (defun c-mode-menu (modestr)
402 '(["Comment Out Region" comment-region (mark t)]
404 (comment-region (region-beginning) (region-end) '(4))
406 ["Fill Comment Paragraph" c-fill-paragraph t]
408 ["Indent Expression" c-indent-exp
409 (memq (char-after) '(?\( ?\[ ?\{))]
410 ["Indent Line" c-indent-command t]
411 ["Up Conditional" c-up-conditional t]
412 ["Backward Conditional" c-backward-conditional t]
413 ["Forward Conditional" c-forward-conditional t]
414 ["Backward Statement" c-beginning-of-statement t]
415 ["Forward Statement" c-end-of-statement t]
417 ["Macro Expand Region" c-macro-expand (mark t)]
418 ["Backslashify" c-backslash-region (mark t)]
426 (defvar c-mode-abbrev-table nil
427 "Abbreviation table used in c-mode buffers.")
428 (define-abbrev-table 'c-mode-abbrev-table ())
430 (defvar c-mode-map ()
431 "Keymap used in c-mode buffers.")
434 (setq c-mode-map (c-make-inherited-keymap))
435 ;; add bindings which are only useful for C
436 (define-key c-mode-map "\C-c\C-e" 'c-macro-expand)
440 (defvar c-mode-syntax-table nil
441 "Syntax table used in c-mode buffers.")
442 (if c-mode-syntax-table
444 (setq c-mode-syntax-table (make-syntax-table))
445 (c-populate-syntax-table c-mode-syntax-table))
447 (easy-menu-define c-c-menu c-mode-map "C Mode Commands"
453 (defvar c++-mode-abbrev-table nil
454 "Abbreviation table used in c++-mode buffers.")
455 (define-abbrev-table 'c++-mode-abbrev-table ())
457 (defvar c++-mode-map ()
458 "Keymap used in c++-mode buffers.")
461 (setq c++-mode-map (c-make-inherited-keymap))
462 ;; add bindings which are only useful for C++
463 (define-key c++-mode-map "\C-c\C-e" 'c-macro-expand)
464 (define-key c++-mode-map "\C-c:" 'c-scope-operator)
465 (define-key c++-mode-map "<" 'c-electric-lt-gt)
466 (define-key c++-mode-map ">" 'c-electric-lt-gt))
469 (defvar c++-mode-syntax-table nil
470 "Syntax table used in c++-mode buffers.")
471 (if c++-mode-syntax-table
473 (setq c++-mode-syntax-table (make-syntax-table))
474 (c-populate-syntax-table c++-mode-syntax-table)
475 ;; TBD: does it make sense for colon to be symbol class in C++?
476 ;; I'm not so sure, since c-label-key is busted on lines like:
478 ;; maybe c-label-key should be fixed instead of commenting this out,
479 ;; but it also bothers me that this only seems appropriate for C++
481 ;;(modify-syntax-entry ?: "_" c++-mode-syntax-table)
484 (easy-menu-define c-c++-menu c++-mode-map "C++ Mode Commands"
488 ;; Support for Objective-C
490 (defvar objc-mode-abbrev-table nil
491 "Abbreviation table used in objc-mode buffers.")
492 (define-abbrev-table 'objc-mode-abbrev-table ())
494 (defvar objc-mode-map ()
495 "Keymap used in objc-mode buffers.")
498 (setq objc-mode-map (c-make-inherited-keymap))
499 ;; add bindings which are only useful for Objective-C
500 (define-key objc-mode-map "\C-c\C-e" 'c-macro-expand))
503 (defvar objc-mode-syntax-table nil
504 "Syntax table used in objc-mode buffers.")
505 (if objc-mode-syntax-table
507 (setq objc-mode-syntax-table (make-syntax-table))
508 (c-populate-syntax-table objc-mode-syntax-table)
509 ;; add extra Objective-C only syntax
510 (modify-syntax-entry ?@ "_" objc-mode-syntax-table))
512 (easy-menu-define c-objc-menu objc-mode-map "ObjC Mode Commands"
513 (c-mode-menu "ObjC"))
518 (defvar java-mode-abbrev-table nil
519 "Abbreviation table used in java-mode buffers.")
520 (define-abbrev-table 'java-mode-abbrev-table ())
522 (defvar java-mode-map ()
523 "Keymap used in java-mode buffers.")
526 (setq java-mode-map (c-make-inherited-keymap))
527 ;; add bindings which are only useful for Java
531 (defvar java-mode-syntax-table nil
532 "Syntax table used in java-mode buffers.")
533 (if java-mode-syntax-table
535 (setq java-mode-syntax-table (make-syntax-table))
536 (c-populate-syntax-table java-mode-syntax-table))
538 (easy-menu-define c-java-menu java-mode-map "Java Mode Commands"
539 (c-mode-menu "Java"))
542 ;; Support for CORBA's IDL language
544 (defvar idl-mode-abbrev-table nil
545 "Abbreviation table used in idl-mode buffers.")
546 (define-abbrev-table 'idl-mode-abbrev-table ())
548 (defvar idl-mode-map ()
549 "Keymap used in idl-mode buffers.")
552 (setq idl-mode-map (c-make-inherited-keymap))
553 ;; add bindings which are only useful for IDL
557 (defvar idl-mode-syntax-table nil
558 "Syntax table used in idl-mode buffers.")
559 (if idl-mode-syntax-table
561 (setq idl-mode-syntax-table (make-syntax-table))
562 (c-populate-syntax-table idl-mode-syntax-table))
564 (easy-menu-define c-idl-menu idl-mode-map "IDL Mode Commands"
570 ;;; cc-langs.el ends here