1 ;;; cc-mode.el --- major mode for editing C and similar languages
3 ;; Copyright (C) 1985, 1987, 1992-2011 Free Software Foundation, Inc.
5 ;; Authors: 2003- Alan Mackenzie
6 ;; 1998- Martin Stjernholm
7 ;; 1992-1999 Barry A. Warsaw
10 ;; 1985 Richard M. Stallman
11 ;; Maintainer: bug-cc-mode@gnu.org
12 ;; Created: a long, long, time ago. adapted from the original c-mode.el
13 ;; Keywords: c languages
15 ;; This file is part of GNU Emacs.
17 ;; GNU Emacs is free software: you can redistribute it and/or modify
18 ;; it under the terms of the GNU General Public License as published by
19 ;; the Free Software Foundation, either version 3 of the License, or
20 ;; (at your option) any later version.
22 ;; GNU Emacs is distributed in the hope that it will be useful,
23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 ;; GNU General Public License for more details.
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
32 ;; NOTE: Read the commentary below for the right way to submit bug reports!
33 ;; NOTE: See the accompanying texinfo manual for details on using this mode!
34 ;; Note: The version string is in cc-defs.
36 ;; This package provides GNU Emacs major modes for editing C, C++,
37 ;; Objective-C, Java, CORBA's IDL, Pike and AWK code. As of the
38 ;; latest Emacs and XEmacs releases, it is the default package for
39 ;; editing these languages. This package is called "CC Mode", and
40 ;; should be spelled exactly this way.
42 ;; CC Mode supports K&R and ANSI C, ANSI C++, Objective-C, Java,
43 ;; CORBA's IDL, Pike and AWK with a consistent indentation model
44 ;; across all modes. This indentation model is intuitive and very
45 ;; flexible, so that almost any desired style of indentation can be
46 ;; supported. Installation, usage, and programming details are
47 ;; contained in an accompanying texinfo manual.
49 ;; CC Mode's immediate ancestors were, c++-mode.el, cplus-md.el, and
52 ;; To submit bug reports, type "C-c C-b". These will be sent to
53 ;; bug-gnu-emacs@gnu.org (mirrored as the Usenet newsgroup
54 ;; gnu.emacs.bug) as well as bug-cc-mode@gnu.org, which directly
55 ;; contacts the CC Mode maintainers. Questions can sent to
56 ;; help-gnu-emacs@gnu.org (mirrored as gnu.emacs.help) and/or
57 ;; bug-cc-mode@gnu.org. Please do not send bugs or questions to our
58 ;; personal accounts; we reserve the right to ignore such email!
60 ;; Many, many thanks go out to all the folks on the beta test list.
61 ;; Without their patience, testing, insight, code contributions, and
62 ;; encouragement CC Mode would be a far inferior package.
64 ;; You can get the latest version of CC Mode, including PostScript
65 ;; documentation and separate individual files from:
67 ;; http://cc-mode.sourceforge.net/
69 ;; You can join a moderated CC Mode announcement-only mailing list by
72 ;; http://lists.sourceforge.net/mailman/listinfo/cc-mode-announce
78 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest r
))))
82 (if (and (boundp 'byte-compile-dest-file
)
83 (stringp byte-compile-dest-file
))
84 (cons (file-name-directory byte-compile-dest-file
) load-path
)
86 (load "cc-bytecomp" nil t
)))
89 (cc-require-when-compile 'cc-langs
)
91 (cc-require 'cc-engine
)
92 (cc-require 'cc-styles
)
94 (cc-require 'cc-align
)
95 (cc-require 'cc-menus
)
97 ;; Silence the compiler.
98 (cc-bytecomp-defvar adaptive-fill-first-line-regexp
) ; Emacs
99 (cc-bytecomp-defun set-keymap-parents) ; XEmacs
100 (cc-bytecomp-defun run-mode-hooks) ; Emacs 21.1
102 ;; We set these variables during mode init, yet we don't require
104 (cc-bytecomp-defvar font-lock-defaults
)
105 (cc-bytecomp-defvar font-lock-syntactic-keywords
)
107 ;; Menu support for both XEmacs and Emacs. If you don't have easymenu
108 ;; with your version of Emacs, you are incompatible!
109 (cc-external-require 'easymenu
)
111 ;; Autoload directive for emacsen that doesn't have an older CC Mode
112 ;; version in the dist.
113 (autoload 'subword-mode
"subword"
114 "Mode enabling subword movement and editing keys." t
)
116 ;; Load cc-fonts first after font-lock is loaded, since it isn't
117 ;; necessary until font locking is requested.
118 ; (eval-after-load "font-lock" ; 2006-07-09: font-lock is now preloaded.
120 (require 'cc-fonts
) ;)
122 ;; cc-langs isn't loaded when we're byte compiled, so add autoload
123 ;; directives for the interface functions.
124 (autoload 'c-make-init-lang-vars-fun
"cc-langs")
125 (autoload 'c-init-language-vars
"cc-langs" nil nil
'macro
)
128 ;; Other modes and packages which depend on CC Mode should do the
129 ;; following to make sure everything is loaded and available for their
132 ;; (require 'cc-mode)
134 ;; And in the major mode function:
136 ;; (c-initialize-cc-mode t)
137 ;; (c-init-language-vars some-mode)
138 ;; (c-common-init 'some-mode) ; Or perhaps (c-basic-common-init 'some-mode)
140 ;; If you're not writing a derived mode using the language variable
141 ;; system, then some-mode is one of the language modes directly
142 ;; supported by CC Mode. You can then use (c-init-language-vars-for
143 ;; 'some-mode) instead of `c-init-language-vars'.
144 ;; `c-init-language-vars-for' is a function that avoids the rather
145 ;; large expansion of `c-init-language-vars'.
147 ;; If you use `c-basic-common-init' then you might want to call
148 ;; `c-font-lock-init' too to set up CC Mode's font lock support.
150 ;; See cc-langs.el for further info. A small example of a derived mode
151 ;; is also available at <http://cc-mode.sourceforge.net/
152 ;; derived-mode-ex.el>.
154 (defun c-leave-cc-mode-mode ()
155 (setq c-buffer-is-cc-mode nil
))
157 (defun c-init-language-vars-for (mode)
158 "Initialize the language variables for one of the language modes
159 directly supported by CC Mode. This can be used instead of the
160 `c-init-language-vars' macro if the language you want to use is one of
161 those, rather than a derived language defined through the language
162 variable system (see \"cc-langs.el\")."
163 (cond ((eq mode
'c-mode
) (c-init-language-vars c-mode
))
164 ((eq mode
'c
++-mode
) (c-init-language-vars c
++-mode
))
165 ((eq mode
'objc-mode
) (c-init-language-vars objc-mode
))
166 ((eq mode
'java-mode
) (c-init-language-vars java-mode
))
167 ((eq mode
'idl-mode
) (c-init-language-vars idl-mode
))
168 ((eq mode
'pike-mode
) (c-init-language-vars pike-mode
))
169 ((eq mode
'awk-mode
) (c-init-language-vars awk-mode
))
170 (t (error "Unsupported mode %s" mode
))))
173 (defun c-initialize-cc-mode (&optional new-style-init
)
174 "Initialize CC Mode for use in the current buffer.
175 If the optional NEW-STYLE-INIT is nil or left out then all necessary
176 initialization to run CC Mode for the C language is done. Otherwise
177 only some basic setup is done, and a call to `c-init-language-vars' or
178 `c-init-language-vars-for' is necessary too (which gives more
179 control). See \"cc-mode.el\" for more info."
181 (setq c-buffer-is-cc-mode t
)
183 (let ((initprop 'cc-mode-is-initialized
)
185 (unless (get 'c-initialize-cc-mode initprop
)
188 (put 'c-initialize-cc-mode initprop t
)
189 (c-initialize-builtin-style)
190 (run-hooks 'c-initialization-hook
)
191 ;; Fix obsolete variables.
192 (if (boundp 'c-comment-continuation-stars
)
193 (setq c-block-comment-prefix
194 (symbol-value 'c-comment-continuation-stars
)))
195 (add-hook 'change-major-mode-hook
'c-leave-cc-mode-mode
)
196 (setq c-initialization-ok t
))
197 ;; Will try initialization hooks again if they failed.
198 (put 'c-initialize-cc-mode initprop c-initialization-ok
))))
200 (unless new-style-init
201 (c-init-language-vars-for 'c-mode
)))
206 (defvar c-mode-base-map
()
207 "Keymap shared by all CC Mode related modes.")
209 (defun c-make-inherited-keymap ()
210 (let ((map (make-sparse-keymap)))
211 ;; Necessary to use `cc-bytecomp-fboundp' below since this
212 ;; function is called from top-level forms that are evaluated
213 ;; while cc-bytecomp is active when one does M-x eval-buffer.
216 ((cc-bytecomp-fboundp 'set-keymap-parent
)
217 (set-keymap-parent map c-mode-base-map
))
219 ((cc-bytecomp-fboundp 'set-keymap-parents
)
220 (set-keymap-parents map c-mode-base-map
))
222 (t (error "CC Mode is incompatible with this version of Emacs")))
225 (defun c-define-abbrev-table (name defs
)
226 ;; Compatibility wrapper for `define-abbrev' which passes a non-nil
227 ;; sixth argument for SYSTEM-FLAG in emacsen that support it
228 ;; (currently only Emacs >= 21.2).
229 (let ((table (or (symbol-value name
)
230 (progn (define-abbrev-table name nil
)
231 (symbol-value name
)))))
234 (apply 'define-abbrev table
(append (car defs
) '(t)))
235 (wrong-number-of-arguments
236 (apply 'define-abbrev table
(car defs
))))
237 (setq defs
(cdr defs
)))))
238 (put 'c-define-abbrev-table
'lisp-indent-function
1)
240 (defun c-bind-special-erase-keys ()
241 ;; Only used in Emacs to bind C-c C-<delete> and C-c C-<backspace>
242 ;; to the proper keys depending on `normal-erase-is-backspace'.
243 (if normal-erase-is-backspace
245 (define-key c-mode-base-map
(kbd "C-c C-<delete>")
246 'c-hungry-delete-forward
)
247 (define-key c-mode-base-map
(kbd "C-c C-<backspace>")
248 'c-hungry-delete-backwards
))
249 (define-key c-mode-base-map
(kbd "C-c C-<delete>")
250 'c-hungry-delete-backwards
)
251 (define-key c-mode-base-map
(kbd "C-c C-<backspace>")
252 'c-hungry-delete-forward
)))
257 (setq c-mode-base-map
(make-sparse-keymap))
259 ;; Separate M-BS from C-M-h. The former should remain
260 ;; backward-kill-word.
261 (define-key c-mode-base-map
[(control meta h
)] 'c-mark-function
)
262 (define-key c-mode-base-map
"\e\C-q" 'c-indent-exp
)
263 (substitute-key-definition 'backward-sentence
264 'c-beginning-of-statement
265 c-mode-base-map global-map
)
266 (substitute-key-definition 'forward-sentence
268 c-mode-base-map global-map
)
269 (substitute-key-definition 'indent-new-comment-line
270 'c-indent-new-comment-line
271 c-mode-base-map global-map
)
272 (substitute-key-definition 'indent-for-tab-command
273 ;; XXX Is this the right thing to do
275 'c-indent-line-or-region
276 c-mode-base-map global-map
)
277 (when (fboundp 'comment-indent-new-line
)
278 ;; indent-new-comment-line has changed name to
279 ;; comment-indent-new-line in Emacs 21.
280 (substitute-key-definition 'comment-indent-new-line
281 'c-indent-new-comment-line
282 c-mode-base-map global-map
))
284 ;; RMS says don't make these the default.
285 ;; (April 2006): RMS has now approved these commands as defaults.
286 (unless (memq 'argumentative-bod-function c-emacs-features
)
287 (define-key c-mode-base-map
"\e\C-a" 'c-beginning-of-defun
)
288 (define-key c-mode-base-map
"\e\C-e" 'c-end-of-defun
))
290 (define-key c-mode-base-map
"\C-c\C-n" 'c-forward-conditional
)
291 (define-key c-mode-base-map
"\C-c\C-p" 'c-backward-conditional
)
292 (define-key c-mode-base-map
"\C-c\C-u" 'c-up-conditional
)
294 ;; It doesn't suffice to put `c-fill-paragraph' on
295 ;; `fill-paragraph-function' since `c-fill-paragraph' must be called
296 ;; before any fill prefix adaption is done. E.g. `filladapt-mode'
297 ;; replaces `fill-paragraph' and does the adaption before calling
298 ;; `fill-paragraph-function', and we have to mask comments etc
299 ;; before that. Also, `c-fill-paragraph' chains on to
300 ;; `fill-paragraph' and the value on `fill-parapgraph-function' to
301 ;; do the actual filling work.
302 (substitute-key-definition 'fill-paragraph
'c-fill-paragraph
303 c-mode-base-map global-map
)
304 ;; In XEmacs the default fill function is called
305 ;; fill-paragraph-or-region.
306 (substitute-key-definition 'fill-paragraph-or-region
'c-fill-paragraph
307 c-mode-base-map global-map
)
309 ;; We bind the forward deletion key and (implicitly) C-d to
310 ;; `c-electric-delete-forward', and the backward deletion key to
311 ;; `c-electric-backspace'. The hungry variants are bound to the
312 ;; same keys but prefixed with C-c. This implies that C-c C-d is
313 ;; `c-hungry-delete-forward'. For consistency, we bind not only C-c
314 ;; <backspace> to `c-hungry-delete-backwards' but also
315 ;; C-c C-<backspace>, so that the Ctrl key can be held down during
316 ;; the whole sequence regardless of the direction. This in turn
317 ;; implies that we bind C-c C-<delete> to `c-hungry-delete-forward',
318 ;; for the same reason.
320 ;; Bind the electric deletion functions to C-d and DEL. Emacs 21
321 ;; automatically maps the [delete] and [backspace] keys to these two
322 ;; depending on window system and user preferences. (In earlier
323 ;; versions it's possible to do the same by using `function-key-map'.)
324 (define-key c-mode-base-map
"\C-d" 'c-electric-delete-forward
)
325 (define-key c-mode-base-map
"\177" 'c-electric-backspace
)
326 (define-key c-mode-base-map
"\C-c\C-d" 'c-hungry-delete-forward
)
327 (define-key c-mode-base-map
[?\C-c ?\d
] 'c-hungry-delete-backwards
)
328 (define-key c-mode-base-map
[?\C-c ?\C-\d
] 'c-hungry-delete-backwards
)
329 (define-key c-mode-base-map
[?\C-c deletechar
] 'c-hungry-delete-forward
) ; C-c <delete> on a tty.
330 (define-key c-mode-base-map
[?\C-c
(control deletechar
)] ; C-c C-<delete> on a tty.
331 'c-hungry-delete-forward
)
332 (when (boundp 'normal-erase-is-backspace
)
333 ;; The automatic C-d and DEL mapping functionality doesn't extend
334 ;; to special combinations like C-c C-<delete>, so we have to hook
335 ;; into the `normal-erase-is-backspace' system to bind it directly
337 (add-hook 'normal-erase-is-backspace-hook
'c-bind-special-erase-keys
)
338 (c-bind-special-erase-keys))
340 (when (fboundp 'delete-forward-p
)
341 ;; In XEmacs we fix the forward and backward deletion behavior by
342 ;; binding the keysyms for the [delete] and [backspace] keys
343 ;; directly, and use `delete-forward-p' to decide what [delete]
344 ;; should do. That's done in the XEmacs specific
345 ;; `c-electric-delete' and `c-hungry-delete' functions.
346 (define-key c-mode-base-map
[delete] 'c-electric-delete)
347 (define-key c-mode-base-map [backspace] 'c-electric-backspace)
348 (define-key c-mode-base-map (kbd "C-c <delete>") 'c-hungry-delete)
349 (define-key c-mode-base-map (kbd "C-c C-<delete>") 'c-hungry-delete)
350 (define-key c-mode-base-map (kbd "C-c <backspace>")
351 'c-hungry-delete-backwards)
352 (define-key c-mode-base-map (kbd "C-c C-<backspace>")
353 'c-hungry-delete-backwards))
355 (define-key c-mode-base-map "#" 'c-electric-pound)
356 (define-key c-mode-base-map "{" 'c-electric-brace)
357 (define-key c-mode-base-map "}" 'c-electric-brace)
358 (define-key c-mode-base-map "/" 'c-electric-slash)
359 (define-key c-mode-base-map "*" 'c-electric-star)
360 (define-key c-mode-base-map ";" 'c-electric-semi&comma)
361 (define-key c-mode-base-map "," 'c-electric-semi&comma)
362 (define-key c-mode-base-map ":" 'c-electric-colon)
363 (define-key c-mode-base-map "(" 'c-electric-paren)
364 (define-key c-mode-base-map ")" 'c-electric-paren)
366 (define-key c-mode-base-map "\C-c\C-\\" 'c-backslash-region)
367 (define-key c-mode-base-map "\C-c\C-a" 'c-toggle-auto-newline)
368 (define-key c-mode-base-map "\C-c\C-b" 'c-submit-bug-report)
369 (define-key c-mode-base-map "\C-c\C-c" 'comment-region)
370 (define-key c-mode-base-map "\C-c\C-l" 'c-toggle-electric-state)
371 (define-key c-mode-base-map "\C-c\C-o" 'c-set-offset)
372 (define-key c-mode-base-map "\C-c\C-q" 'c-indent-defun)
373 (define-key c-mode-base-map "\C-c\C-s" 'c-show-syntactic-information)
374 ;; (define-key c-mode-base-map "\C-c\C-t" 'c-toggle-auto-hungry-state) Commented out by ACM, 2005-03-05.
375 (define-key c-mode-base-map "\C-c." 'c-set-style)
376 ;; conflicts with OOBR
377 ;;(define-key c-mode-base-map "\C-c\C-v" 'c-version)
378 ;; (define-key c-mode-base-map "\C-c\C-y" 'c-toggle-hungry-state) Commented out by ACM, 2005-11-22.
379 (define-key c-mode-base-map "\C-c\C-w" 'subword-mode)
382 ;; We don't require the outline package, but we configure it a bit anyway.
383 (cc-bytecomp-defvar outline-level)
385 (defun c-mode-menu (modestr)
386 "Return a menu spec suitable for `easy-menu-define' that is exactly
387 like the C mode menu except that the menu bar item name is MODESTR
390 This function is provided for compatibility only; derived modes should
391 preferably use the `c-mode-menu' language constant directly."
392 (cons modestr (c-lang-const c-mode-menu c)))
394 ;; Ugly hack to pull in the definition of `c-populate-syntax-table'
395 ;; from cc-langs to make it available at runtime. It's either this or
396 ;; moving the definition for it to cc-defs, but that would mean to
397 ;; break up the syntax table setup over two files.
398 (defalias 'c-populate-syntax-table
399 (cc-eval-when-compile
400 (let ((f (symbol-function 'c-populate-syntax-table)))
401 (if (byte-code-function-p f) f (byte-compile f)))))
403 ;; CAUTION: Try to avoid installing things on
404 ;; `before-change-functions'. The macro `combine-after-change-calls'
405 ;; is used and it doesn't work if there are things on that hook. That
406 ;; can cause font lock functions to run in inconvenient places during
407 ;; temporary changes in some font lock support modes, causing extra
408 ;; unnecessary work and font lock glitches due to interactions between
409 ;; various text properties.
411 ;; (2007-02-12): The macro `combine-after-change-calls' ISN'T used any
414 (defun c-unfind-enclosing-token (pos)
415 ;; If POS is wholly inside a token, remove that id from
416 ;; `c-found-types', should it be present. Return t if we were in an
419 (let ((tok-beg (progn (goto-char pos)
420 (and (c-beginning-of-current-token) (point))))
421 (tok-end (progn (goto-char pos)
422 (and (c-end-of-current-token) (point)))))
423 (when (and tok-beg tok-end)
424 (c-unfind-type (buffer-substring-no-properties tok-beg tok-end))
427 (defun c-unfind-coalesced-tokens (beg end)
428 ;; unless the non-empty region (beg end) is entirely WS and there's at
429 ;; least one character of WS just before or after this region, remove
430 ;; the tokens which touch the region from `c-found-types' should they
432 (or (c-partial-ws-p beg end)
436 (or (eq beg (point-min))
437 (c-skip-ws-backward (1- beg))
439 (= (c-backward-token-2) 1)
440 (c-unfind-type (buffer-substring-no-properties
443 (or (eq end (point-max))
444 (c-skip-ws-forward (1+ end))
446 (progn (forward-char) (c-end-of-current-token) nil)
447 (c-unfind-type (buffer-substring-no-properties
450 ;; c-maybe-stale-found-type records a place near the region being
451 ;; changed where an element of `found-types' might become stale. It
452 ;; is set in c-before-change and is either nil, or has the form:
454 ;; (c-decl-id-start "foo" 97 107 " (* ooka) " "o"), where
456 ;; o - `c-decl-id-start' is the c-type text property value at buffer
459 ;; o - 97 107 is the region potentially containing the stale type -
460 ;; this is delimited by a non-nil c-type text property at 96 and
461 ;; either another one or a ";", "{", or "}" at 107.
463 ;; o - " (* ooka) " is the (before change) buffer portion containing
464 ;; the suspect type (here "ooka").
466 ;; o - "o" is the buffer contents which is about to be deleted. This
467 ;; would be the empty string for an insertion.
468 (defvar c-maybe-stale-found-type nil)
469 (make-variable-buffer-local 'c-maybe-stale-found-type)
471 (defun c-basic-common-init (mode default-style)
472 "Do the necessary initialization for the syntax handling routines
473 and the line breaking/filling code. Intended to be used by other
474 packages that embed CC Mode.
476 MODE is the CC Mode flavor to set up, e.g. 'c-mode or 'java-mode.
477 DEFAULT-STYLE tells which indentation style to install. It has the
478 same format as `c-default-style'.
480 Note that `c-init-language-vars' must be called before this function.
481 This function cannot do that since `c-init-language-vars' is a macro
482 that requires a literal mode spec at compile time."
484 (setq c-buffer-is-cc-mode mode)
486 ;; these variables should always be buffer local; they do not affect
487 ;; indentation style.
488 (make-local-variable 'comment-start)
489 (make-local-variable 'comment-end)
490 (make-local-variable 'comment-start-skip)
492 (make-local-variable 'paragraph-start)
493 (make-local-variable 'paragraph-separate)
494 (make-local-variable 'paragraph-ignore-fill-prefix)
495 (make-local-variable 'adaptive-fill-mode)
496 (make-local-variable 'adaptive-fill-regexp)
498 ;; now set their values
499 (set (make-local-variable 'parse-sexp-ignore-comments) t)
500 (set (make-local-variable 'indent-line-function) 'c-indent-line)
501 (set (make-local-variable 'indent-region-function) 'c-indent-region)
502 (set (make-local-variable 'normal-auto-fill-function) 'c-do-auto-fill)
503 (set (make-local-variable 'comment-multi-line) t)
504 (set (make-local-variable 'comment-line-break-function)
505 'c-indent-new-comment-line)
507 ;; Install `c-fill-paragraph' on `fill-paragraph-function' so that a
508 ;; direct call to `fill-paragraph' behaves better. This still
509 ;; doesn't work with filladapt but it's better than nothing.
510 (set (make-local-variable 'fill-paragraph-function) 'c-fill-paragraph)
512 ;; Initialise the cache of brace pairs, and opening braces/brackets/parens.
515 (when (or c-recognize-<>-arglists
516 (c-major-mode-is 'awk-mode)
517 (c-major-mode-is '(java-mode c-mode c++-mode objc-mode)))
518 ;; We'll use the syntax-table text property to change the syntax
519 ;; of some chars for this language, so do the necessary setup for
522 ;; Note to other package developers: It's ok to turn this on in CC
523 ;; Mode buffers when CC Mode doesn't, but it's not ok to turn it
524 ;; off if CC Mode has turned it on.
527 (when (boundp 'parse-sexp-lookup-properties)
528 (set (make-local-variable 'parse-sexp-lookup-properties) t))
530 ;; Same as above for XEmacs.
531 (when (boundp 'lookup-syntax-properties)
532 (set (make-local-variable 'lookup-syntax-properties) t)))
534 ;; Use this in Emacs 21+ to avoid meddling with the rear-nonsticky
535 ;; property on each character.
536 (when (boundp 'text-property-default-nonsticky)
537 (mapc (lambda (tprop)
538 (unless (assq tprop text-property-default-nonsticky)
539 (set (make-local-variable 'text-property-default-nonsticky)
540 (cons `(,tprop . t) text-property-default-nonsticky))))
541 '(syntax-table category c-type)))
543 ;; In Emacs 21 and later it's possible to turn off the ad-hoc
544 ;; heuristic that open parens in column 0 are defun starters. Since
545 ;; we have c-state-cache, that heuristic isn't useful and only causes
546 ;; trouble, so turn it off.
547 ;; 2006/12/17: This facility is somewhat confused, and doesn't really seem
548 ;; helpful. Comment it out for now.
549 ;; (when (memq 'col-0-paren c-emacs-features)
550 ;; (make-local-variable 'open-paren-in-column-0-is-defun-start)
551 ;; (setq open-paren-in-column-0-is-defun-start nil))
553 (c-clear-found-types)
555 ;; now set the mode style based on default-style
556 (let ((style (if (stringp default-style)
558 (or (cdr (assq mode default-style))
559 (cdr (assq 'other default-style))
561 ;; Override style variables if `c-old-style-variable-behavior' is
562 ;; set. Also override if we are using global style variables,
563 ;; have already initialized a style once, and are switching to a
564 ;; different style. (It's doubtful whether this is desirable, but
565 ;; the whole situation with nonlocal style variables is a bit
566 ;; awkward. It's at least the most compatible way with the old
567 ;; style init procedure.)
568 (c-set-style style (not (or c-old-style-variable-behavior
569 (and (not c-style-variables-are-local-p)
571 (not (string-equal c-indentation-style
573 (c-setup-paragraph-variables)
575 ;; we have to do something special for c-offsets-alist so that the
576 ;; buffer local value has its own alist structure.
577 (setq c-offsets-alist (copy-alist c-offsets-alist))
579 ;; setup the comment indent variable in a Emacs version portable way
580 (set (make-local-variable 'comment-indent-function) 'c-comment-indent)
582 ;; ;; Put submode indicators onto minor-mode-alist, but only once.
583 ;; (or (assq 'c-submode-indicators minor-mode-alist)
584 ;; (setq minor-mode-alist
585 ;; (cons '(c-submode-indicators c-submode-indicators)
586 ;; minor-mode-alist)))
589 ;; Install the functions that ensure that various internal caches
590 ;; don't become invalid due to buffer changes.
591 (when (featurep 'xemacs)
592 (make-local-hook 'before-change-functions)
593 (make-local-hook 'after-change-functions))
594 (add-hook 'before-change-functions 'c-before-change nil t)
595 (add-hook 'after-change-functions 'c-after-change nil t)
596 (set (make-local-variable 'font-lock-extend-after-change-region-function)
597 'c-extend-after-change-region)) ; Currently (2009-05) used by all
598 ; lanaguages with #define (C, C++,; ObjC), and by AWK.
600 (defun c-setup-doc-comment-style ()
601 "Initialize the variables that depend on the value of `c-doc-comment-style'."
602 (when (and (featurep 'font-lock)
603 (symbol-value 'font-lock-mode))
604 ;; Force font lock mode to reinitialize itself.
608 ;; Buffer local variables defining the region to be fontified by a font lock
609 ;; after-change function. They are set in c-after-change to
610 ;; after-change-function's BEG and END, and may be modified by a
611 ;; `c-before-font-lock-function'.
613 (make-variable-buffer-local 'c-new-BEG)
615 (make-variable-buffer-local 'c-new-END)
617 (defun c-common-init (&optional mode)
618 "Common initialization for all CC Mode modes.
619 In addition to the work done by `c-basic-common-init' and
620 `c-font-lock-init', this function sets up various other things as
621 customary in CC Mode modes but which aren't strictly necessary for CC
622 Mode to operate correctly.
624 MODE is the symbol for the mode to initialize, like 'c-mode. See
625 `c-basic-common-init' for details. It's only optional to be
626 compatible with old code; callers should always specify it."
629 ;; Called from an old third party package. The fallback is to
631 (c-init-language-vars-for 'c-mode))
633 (c-basic-common-init mode c-default-style)
635 ;; Only initialize font locking if we aren't called from an old package.
638 ;; Starting a mode is a sort of "change". So call the change functions...
641 (setq c-new-BEG (point-min))
642 (setq c-new-END (point-max))
644 (if c-get-state-before-change-functions
646 (funcall fn (point-min) (point-max)))
647 c-get-state-before-change-functions))
648 (if c-before-font-lock-function
649 (funcall c-before-font-lock-function (point-min) (point-max)
650 (- (point-max) (point-min))))))
652 (set (make-local-variable 'outline-regexp) "[^#\n\^M]")
653 (set (make-local-variable 'outline-level) 'c-outline-level)
655 (let ((rfn (assq mode c-require-final-newline)))
658 (set (make-local-variable 'require-final-newline)
659 mode-require-final-newline)))))
661 (defun c-count-cfss (lv-alist)
662 ;; LV-ALIST is an alist like `file-local-variables-alist'. Count how many
663 ;; elements with the key `c-file-style' there are in it.
664 (let ((elt-ptr lv-alist) elt (cownt 0))
666 (setq elt (car elt-ptr)
667 elt-ptr (cdr elt-ptr))
668 (when (eq (car elt) 'c-file-style)
669 (setq cownt (1+ cownt))))
672 (defun c-before-hack-hook ()
673 "Set the CC Mode style and \"offsets\" when in the buffer's local variables.
674 They are set only when, respectively, the pseudo variables
675 `c-file-style' and `c-file-offsets' are present in the list.
677 This function is called from the hook `before-hack-local-variables-hook'."
678 (when c-buffer-is-cc-mode
679 (let ((mode-cons (assq 'mode file-local-variables-alist))
680 (stile (cdr (assq 'c-file-style file-local-variables-alist)))
681 (offsets (cdr (assq 'c-file-offsets file-local-variables-alist))))
683 (hack-one-local-variable (car mode-cons) (cdr mode-cons))
684 (setq file-local-variables-alist
685 (delq mode-cons file-local-variables-alist)))
687 (or (stringp stile) (error "c-file-style is not a string"))
688 (if (boundp 'dir-local-variables-alist)
689 ;; Determine whether `c-file-style' was set in the file's local
690 ;; variables or in a .dir-locals.el (a directory setting).
691 (let ((cfs-in-file-and-dir-count
692 (c-count-cfss file-local-variables-alist))
693 (cfs-in-dir-count (c-count-cfss dir-local-variables-alist)))
695 (= cfs-in-file-and-dir-count cfs-in-dir-count)))
696 (c-set-style stile)))
700 (let ((langelem (car langentry))
701 (offset (cdr langentry)))
702 (c-set-offset langelem offset)))
705 (defun c-remove-any-local-eval-or-mode-variables ()
706 ;; If the buffer specifies `mode' or `eval' in its File Local Variable list
707 ;; or on the first line, remove all occurrences. See
708 ;; `c-postprocess-file-styles' for justification. There is no need to save
709 ;; point here, or even bother too much about the buffer contents. However,
710 ;; DON'T mess up the kill-ring.
712 ;; Most of the code here is derived from Emacs 21.3's `hack-local-variables'
714 (goto-char (point-max))
715 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
716 (let (lv-point (prefix "") (suffix ""))
717 (when (let ((case-fold-search t))
718 (search-forward "Local Variables:" nil t))
719 (setq lv-point (point))
720 ;; The prefix is what comes before "local variables:" in its line.
721 ;; The suffix is what comes after "local variables:" in its line.
722 (skip-chars-forward " \t")
724 (setq suffix (buffer-substring (point)
725 (progn (end-of-line) (point)))))
726 (goto-char (match-beginning 0))
729 (buffer-substring (point)
730 (progn (beginning-of-line) (point)))))
732 (while (search-forward-regexp
734 (regexp-quote prefix)
735 "\\(mode\\|eval\\):.*"
736 (regexp-quote suffix)
740 (delete-region (point) (progn (forward-line) (point)))))
742 ;; Delete the first line, if we've got one, in case it contains a mode spec.
743 (unless (and lv-point
744 (progn (goto-char lv-point)
747 (goto-char (point-min))
749 (delete-region (point) (progn (forward-line) (point)))))))
751 (defun c-postprocess-file-styles ()
752 "Function that post processes relevant file local variables in CC Mode.
753 Currently, this function simply applies any style and offset settings
754 found in the file's Local Variable list. It first applies any style
755 setting found in `c-file-style', then it applies any offset settings
756 it finds in `c-file-offsets'.
758 Note that the style variables are always made local to the buffer."
760 ;; apply file styles and offsets
761 (when c-buffer-is-cc-mode
762 (if (or c-file-style c-file-offsets)
763 (c-make-styles-buffer-local t))
765 (or (stringp c-file-style)
766 (error "c-file-style is not a string"))
767 (c-set-style c-file-style))
772 (let ((langelem (car langentry))
773 (offset (cdr langentry)))
774 (c-set-offset langelem offset)))
776 ;; Problem: The file local variable block might have explicitly set a
777 ;; style variable. The `c-set-style' or `mapcar' call might have
778 ;; overwritten this. So we run `hack-local-variables' again to remedy
779 ;; this. There are no guarantees this will work properly, particularly as
780 ;; we have no control over what the other hook functions on
781 ;; `hack-local-variables-hook' would have done. We now (2006/2/1) remove
782 ;; any `eval' or `mode' expressions before we evaluate again (see below).
785 ;; Problem (bug reported by Gustav Broberg): if one of the variables is
786 ;; `mode', this will invoke c-mode (etc.) again, setting up the style etc.
787 ;; We prevent this by temporarily removing `mode' from the Local Variables
789 (if (or c-file-style c-file-offsets)
790 (let ((hack-local-variables-hook nil) (inhibit-read-only t))
791 (c-tentative-buffer-changes
792 (c-remove-any-local-eval-or-mode-variables)
793 (hack-local-variables))
796 (if (boundp 'before-hack-local-variables-hook)
797 (add-hook 'before-hack-local-variables-hook 'c-before-hack-hook)
798 (add-hook 'hack-local-variables-hook 'c-postprocess-file-styles))
800 (defmacro c-run-mode-hooks (&rest hooks)
801 ;; Emacs 21.1 has introduced a system with delayed mode hooks that
802 ;; requires the use of the new function `run-mode-hooks'.
803 (if (cc-bytecomp-fboundp 'run-mode-hooks)
804 `(run-mode-hooks ,@hooks)
805 `(progn ,@(mapcar (lambda (hook) `(run-hooks ,hook)) hooks))))
808 ;;; Change hooks, linking with Font Lock.
810 ;; Buffer local variables recording Beginning/End-of-Macro position before a
811 ;; change, when a macro straddles, respectively, the BEG or END (or both) of
812 ;; the change region. Otherwise these have the values BEG/END.
814 (make-variable-buffer-local 'c-old-BOM)
816 (make-variable-buffer-local 'c-old-EOM)
818 (defun c-extend-region-for-CPP (beg end)
819 ;; Set c-old-BOM or c-old-EOM respectively to BEG, END, each extended to the
820 ;; beginning/end of any preprocessor construct they may be in.
822 ;; Point is undefined both before and after this function call; the buffer
823 ;; has already been widened, and match-data saved. The return value is
826 ;; This function is in the C/C++/ObjC values of
827 ;; `c-get-state-before-change-functions' and is called exclusively as a
828 ;; before change function.
830 (c-beginning-of-macro)
831 (setq c-old-BOM (point))
834 (when (c-beginning-of-macro)
836 (or (eobp) (forward-char))) ; Over the terminating NL which may be marked
837 ; with a c-cpp-delimiter category property
838 (setq c-old-EOM (point)))
840 (defun c-neutralize-CPP-line (beg end)
841 ;; BEG and END bound a region, typically a preprocessor line. Put a
842 ;; "punctuation" syntax-table property on syntactically obtrusive
843 ;; characters, ones which would interact syntactically with stuff outside
846 ;; These are unmatched string delimiters, or unmatched
847 ;; parens/brackets/braces. An unclosed comment is regarded as valid, NOT
853 (setq s (parse-partial-sexp beg end -1))
855 ((< (nth 0 s) 0) ; found an unmated ),},]
856 (c-put-char-property (1- (point)) 'syntax-table '(1))
858 ((nth 3 s) ; In a string
859 (c-put-char-property (nth 8 s) 'syntax-table '(1))
861 ((> (nth 0 s) 0) ; In a (,{,[
862 (c-put-char-property (nth 1 s) 'syntax-table '(1))
866 (defun c-neutralize-syntax-in-and-mark-CPP (begg endd old-len)
867 ;; (i) Extend the font lock region to cover all changed preprocessor
868 ;; regions; it does this by setting the variables `c-new-BEG' and
869 ;; `c-new-END' to the new boundaries.
871 ;; (ii) "Neutralize" every preprocessor line wholly or partially in the
872 ;; extended changed region. "Restore" lines which were CPP lines before the
873 ;; change and are no longer so; these can be located from the Buffer local
874 ;; variables `c-old-BOM' and `c-old-EOM'.
876 ;; (iii) Mark every CPP construct by placing a `category' property value
877 ;; `c-cpp-delimiter' at its start and end. The marked characters are the
878 ;; opening # and usually the terminating EOL, but sometimes the character
879 ;; before a comment/string delimiter.
881 ;; That is, set syntax-table properties on characters that would otherwise
882 ;; interact syntactically with those outside the CPP line(s).
884 ;; This function is called from an after-change function, BEGG ENDD and
885 ;; OLD-LEN being the standard parameters. It prepares the buffer for font
886 ;; locking, hence must get called before `font-lock-after-change-function'.
888 ;; Point is undefined both before and after this function call, the buffer
889 ;; has been widened, and match-data saved. The return value is ignored.
891 ;; This function is the C/C++/ObjC value of `c-before-font-lock-function'.
893 ;; Note: SPEED _MATTERS_ IN THIS FUNCTION!!!
895 ;; This function might make hidden buffer changes.
896 (c-save-buffer-state (limits)
897 ;; First determine the region, (c-new-BEG c-new-END), which will get font
898 ;; locked. It might need "neutralizing". This region may not start
899 ;; inside a string, comment, or macro.
900 (goto-char c-old-BOM) ; already set to old start of macro or begg.
903 (if (setq limits (c-state-literal-at (point)))
904 (cdr limits) ; go forward out of any string or comment.
908 (if (setq limits (c-state-literal-at (point)))
909 (goto-char (car limits))) ; go backward out of any string or comment.
910 (if (c-beginning-of-macro)
912 (setq c-new-END (max c-new-END
913 (+ (- c-old-EOM old-len) (- endd begg))
916 ;; Clear all old relevant properties.
917 (c-clear-char-property-with-value c-new-BEG c-new-END 'syntax-table '(1))
918 (c-clear-char-property-with-value c-new-BEG c-new-END 'category 'c-cpp-delimiter)
919 ;; FIXME!!! What about the "<" and ">" category properties? 2009-11-16
921 ;; Add needed properties to each CPP construct in the region.
922 (goto-char c-new-BEG)
923 (let ((pps-position c-new-BEG) pps-state mbeg)
924 (while (and (< (point) c-new-END)
925 (search-forward-regexp c-anchored-cpp-prefix c-new-END t))
926 ;; If we've found a "#" inside a string/comment, ignore it.
928 (parse-partial-sexp pps-position (point) nil nil pps-state)
929 pps-position (point))
930 (unless (or (nth 3 pps-state) ; in a string?
931 (nth 4 pps-state)) ; in a comment?
932 (goto-char (match-beginning 0))
934 (if (> (c-syntactic-end-of-macro) mbeg)
936 (c-neutralize-CPP-line mbeg (point))
937 (c-set-cpp-delimiters mbeg (point))
938 ;(setq pps-position (point))
940 (forward-line)) ; no infinite loop with, e.g., "#//"
943 (defun c-before-change (beg end)
944 ;; Function to be put on `before-change-functions'. Primarily, this calls
945 ;; the language dependent `c-get-state-before-change-functions'. It is
946 ;; otherwise used only to remove stale entries from the `c-found-types'
947 ;; cache, and to record entries which a `c-after-change' function might
950 ;; Note that this function must be FAST rather than accurate. Note
951 ;; also that it only has any effect when font locking is enabled.
952 ;; We exploit this by checking for font-lock-*-face instead of doing
953 ;; rigourous syntactic analysis.
955 ;; If either change boundary is wholly inside an identifier, delete
956 ;; it/them from the cache. Don't worry about being inside a string
957 ;; or a comment - "wrongly" removing a symbol from `c-found-types'
959 (setq c-maybe-stale-found-type nil)
964 ;; Are we inserting/deleting stuff in the middle of an identifier?
965 (c-unfind-enclosing-token beg)
966 (c-unfind-enclosing-token end)
967 ;; Are we coalescing two tokens together, e.g. "fo o" -> "foo"?
969 (c-unfind-coalesced-tokens beg end))
970 ;; Are we (potentially) disrupting the syntactic context which
971 ;; makes a type a type? E.g. by inserting stuff after "foo" in
972 ;; "foo bar;", or before "foo" in "typedef foo *bar;"?
974 ;; We search for appropriate c-type properties "near" the change.
975 ;; First, find an appropriate boundary for this property search.
980 (or (and (eq (get-text-property end 'face) 'font-lock-comment-face)
981 (previous-single-property-change end 'face))
983 (when (>= end1 beg) ; Don't hassle about changes entirely in comments.
984 ;; Find a limit for the search for a `c-type' property
986 (and (/= (skip-chars-backward "^;{}") 0)
987 (> (point) (point-min))
988 (memq (c-get-char-property (1- (point)) 'face)
989 '(font-lock-comment-face font-lock-string-face))))
990 (setq lim (max (point-min) (1- (point))))
992 ;; Look for the latest `c-type' property before end1
993 (when (and (> end1 (point-min))
995 (if (get-text-property (1- end1) 'c-type)
997 (previous-single-property-change end1 'c-type nil lim))))
998 (setq type (get-text-property (max (1- type-pos) lim) 'c-type))
1000 (when (memq type '(c-decl-id-start c-decl-type-start))
1001 ;; Get the identifier, if any, that the property is on.
1002 (goto-char (1- type-pos))
1004 (when (looking-at "\\(\\sw\\|\\s_\\)")
1005 (c-beginning-of-current-token)
1006 (buffer-substring-no-properties (point) type-pos)))
1009 (skip-chars-forward "^;{}") ; FIXME!!! loop for comment, maybe
1012 (or (next-single-property-change end 'c-type nil lim) lim))
1013 (setq c-maybe-stale-found-type
1014 (list type marked-id
1016 (buffer-substring-no-properties type-pos term-pos)
1017 (buffer-substring-no-properties beg end)))))))
1019 (if c-get-state-before-change-functions
1021 (funcall fn beg end))
1022 c-get-state-before-change-functions))
1025 (defun c-after-change (beg end old-len)
1026 ;; Function put on `after-change-functions' to adjust various caches
1027 ;; etc. Prefer speed to finesse here, since there will be an order
1028 ;; of magnitude more calls to this function than any of the
1029 ;; functions that use the caches.
1031 ;; Note that care must be taken so that this is called before any
1032 ;; font-lock callbacks since we might get calls to functions using
1033 ;; these caches from inside them, and we must thus be sure that this
1034 ;; has already been executed.
1036 ;; This calls the language variable c-before-font-lock-function, if non nil.
1037 ;; This typically sets `syntax-table' properties.
1039 (c-save-buffer-state ()
1040 ;; When `combine-after-change-calls' is used we might get calls
1041 ;; with regions outside the current narrowing. This has been
1042 ;; observed in Emacs 20.7.
1044 (save-match-data ; c-recognize-<>-arglists changes match-data
1047 (when (> end (point-max))
1048 ;; Some emacsen might return positions past the end. This has been
1049 ;; observed in Emacs 20.7 when rereading a buffer changed on disk
1050 ;; (haven't been able to minimize it, but Emacs 21.3 appears to
1052 (setq end (point-max))
1056 ;; C-y is capable of spuriously converting category properties
1057 ;; c-</>-as-paren-syntax into hard syntax-table properties. Remove
1058 ;; these when it happens.
1059 (c-clear-char-property-with-value beg end 'syntax-table
1060 c-<-as-paren-syntax)
1061 (c-clear-char-property-with-value beg end 'syntax-table
1062 c->-as-paren-syntax)
1064 (c-trim-found-types beg end old-len) ; maybe we don't need all of these.
1065 (c-invalidate-sws-region-after beg end)
1066 (c-invalidate-state-cache beg)
1067 (c-invalidate-find-decl-cache beg)
1069 (when c-recognize-<>-arglists
1070 (c-after-change-check-<>-operators beg end))
1072 ;; (c-new-BEG c-new-END) will be the region to fontify. It may become
1073 ;; larger than (beg end).
1076 (if c-before-font-lock-function
1078 (funcall c-before-font-lock-function beg end old-len)))))))
1080 (defun c-after-font-lock-init ()
1081 ;; Put on `font-lock-mode-hook'.
1082 (remove-hook 'after-change-functions 'c-after-change t)
1083 (add-hook 'after-change-functions 'c-after-change nil t))
1085 (defun c-font-lock-init ()
1086 "Set up the font-lock variables for using the font-lock support in CC Mode.
1087 This does not load the font-lock package. Use after
1088 `c-basic-common-init' and after cc-fonts has been loaded."
1090 (set (make-local-variable 'font-lock-defaults)
1091 `(,(if (c-major-mode-is 'awk-mode)
1092 ;; awk-mode currently has only one font lock level.
1093 'awk-font-lock-keywords
1094 (mapcar 'c-mode-symbol
1095 '("font-lock-keywords" "font-lock-keywords-1"
1096 "font-lock-keywords-2" "font-lock-keywords-3")))
1098 ,c-identifier-syntax-modifications
1099 c-beginning-of-syntax
1100 (font-lock-mark-block-function
1101 . c-mark-function)))
1102 (if (featurep 'xemacs)
1103 (make-local-hook 'font-lock-mode-hook))
1104 (add-hook 'font-lock-mode-hook 'c-after-font-lock-init nil t))
1106 (defun c-extend-after-change-region (beg end old-len)
1107 "Extend the region to be fontified, if necessary."
1108 ;; Note: the parameters are ignored here. This somewhat indirect
1109 ;; implementation exists because it is minimally different from the
1110 ;; stand-alone CC Mode which, lacking
1111 ;; font-lock-extend-after-change-region-function, is forced to use advice
1114 ;; Of the seven CC Mode languages, currently (2009-05) only C, C++, Objc
1115 ;; (the languages with #define) and AWK Mode make non-null use of this
1117 (cons c-new-BEG c-new-END))
1123 (defvar c-mode-syntax-table nil
1124 "Syntax table used in c-mode buffers.")
1125 (or c-mode-syntax-table
1126 (setq c-mode-syntax-table
1127 (funcall (c-lang-const c-make-mode-syntax-table c))))
1129 (defvar c-mode-abbrev-table nil
1130 "Abbreviation table used in c-mode buffers.")
1131 (c-define-abbrev-table 'c-mode-abbrev-table
1132 '(("else" "else" c-electric-continued-statement 0)
1133 ("while" "while" c-electric-continued-statement 0)))
1135 (defvar c-mode-map ()
1136 "Keymap used in c-mode buffers.")
1139 (setq c-mode-map (c-make-inherited-keymap))
1140 ;; add bindings which are only useful for C
1141 (define-key c-mode-map "\C-c\C-e" 'c-macro-expand)
1144 (easy-menu-define c-c-menu c-mode-map "C Mode Commands"
1145 (cons "C" (c-lang-const c-mode-menu c)))
1147 ;; In XEmacs >= 21.5 modes should add their own entries to
1148 ;; `auto-mode-alist'. The comment form of autoload is used to avoid
1149 ;; doing this on load. That since `add-to-list' prepends the value
1150 ;; which could cause it to clobber user settings. Later emacsen have
1151 ;; an append option, but it's not safe to use.
1153 ;; The extension ".C" is associated with C++ while the lowercase
1154 ;; variant goes with C. On case insensitive file systems, this means
1155 ;; that ".c" files also might open C++ mode if the C++ entry comes
1156 ;; first on `auto-mode-alist'. Thus we try to ensure that ".C" comes
1157 ;; after ".c", and since `add-to-list' adds the entry first we have to
1158 ;; add the ".C" entry first.
1159 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(cc\\|hh\\)\\'" . c++-mode))
1160 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.[ch]\\(pp\\|xx\\|\\+\\+\\)\\'" . c++-mode))
1161 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(CC?\\|HH?\\)\\'" . c++-mode))
1163 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.[ch]\\'" . c-mode))
1165 ;; NB: The following two associate yacc and lex files to C Mode, which
1166 ;; is not really suitable for those formats. Anyway, afaik there's
1167 ;; currently no better mode for them, and besides this is legacy.
1168 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.y\\(acc\\)?\\'" . c-mode))
1169 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.lex\\'" . c-mode))
1171 ;; Preprocessed files generated by C and C++ compilers.
1172 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.i\\'" . c-mode))
1173 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.ii\\'" . c++-mode))
1177 (define-derived-mode c-mode prog-mode "C"
1178 "Major mode for editing K&R and ANSI C code.
1179 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1180 c-mode buffer. This automatically sets up a mail buffer with version
1181 information already added. You just need to add a description of the
1182 problem, including a reproducible test case, and send the message.
1184 To see what version of CC Mode you are running, enter `\\[c-version]'.
1186 The hook `c-mode-common-hook' is run with no args at mode
1187 initialization, then `c-mode-hook'.
1191 (c-initialize-cc-mode t)
1192 (set-syntax-table c-mode-syntax-table)
1193 (setq local-abbrev-table c-mode-abbrev-table
1195 (use-local-map c-mode-map)
1196 (c-init-language-vars-for 'c-mode)
1197 (c-common-init 'c-mode)
1198 (easy-menu-add c-c-menu)
1199 (cc-imenu-init cc-imenu-c-generic-expression)
1200 (c-run-mode-hooks 'c-mode-common-hook 'c-mode-hook)
1201 (c-update-modeline))
1207 (defvar c++-mode-syntax-table nil
1208 "Syntax table used in c++-mode buffers.")
1209 (or c++-mode-syntax-table
1210 (setq c++-mode-syntax-table
1211 (funcall (c-lang-const c-make-mode-syntax-table c++))))
1213 (defvar c++-mode-abbrev-table nil
1214 "Abbreviation table used in c++-mode buffers.")
1215 (c-define-abbrev-table 'c++-mode-abbrev-table
1216 '(("else" "else" c-electric-continued-statement 0)
1217 ("while" "while" c-electric-continued-statement 0)
1218 ("catch" "catch" c-electric-continued-statement 0)))
1220 (defvar c++-mode-map ()
1221 "Keymap used in c++-mode buffers.")
1224 (setq c++-mode-map (c-make-inherited-keymap))
1225 ;; add bindings which are only useful for C++
1226 (define-key c++-mode-map "\C-c\C-e" 'c-macro-expand)
1227 (define-key c++-mode-map "\C-c:" 'c-scope-operator)
1228 (define-key c++-mode-map "<" 'c-electric-lt-gt)
1229 (define-key c++-mode-map ">" 'c-electric-lt-gt))
1231 (easy-menu-define c-c++-menu c++-mode-map "C++ Mode Commands"
1232 (cons "C++" (c-lang-const c-mode-menu c++)))
1235 (define-derived-mode c++-mode prog-mode "C++"
1236 "Major mode for editing C++ code.
1237 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1238 c++-mode buffer. This automatically sets up a mail buffer with
1239 version information already added. You just need to add a description
1240 of the problem, including a reproducible test case, and send the
1243 To see what version of CC Mode you are running, enter `\\[c-version]'.
1245 The hook `c-mode-common-hook' is run with no args at mode
1246 initialization, then `c++-mode-hook'.
1250 (c-initialize-cc-mode t)
1251 (set-syntax-table c++-mode-syntax-table)
1252 (setq local-abbrev-table c++-mode-abbrev-table
1254 (use-local-map c++-mode-map)
1255 (c-init-language-vars-for 'c++-mode)
1256 (c-common-init 'c++-mode)
1257 (easy-menu-add c-c++-menu)
1258 (cc-imenu-init cc-imenu-c++-generic-expression)
1259 (c-run-mode-hooks 'c-mode-common-hook 'c++-mode-hook)
1260 (c-update-modeline))
1263 ;; Support for Objective-C
1266 (defvar objc-mode-syntax-table nil
1267 "Syntax table used in objc-mode buffers.")
1268 (or objc-mode-syntax-table
1269 (setq objc-mode-syntax-table
1270 (funcall (c-lang-const c-make-mode-syntax-table objc))))
1272 (defvar objc-mode-abbrev-table nil
1273 "Abbreviation table used in objc-mode buffers.")
1274 (c-define-abbrev-table 'objc-mode-abbrev-table
1275 '(("else" "else" c-electric-continued-statement 0)
1276 ("while" "while" c-electric-continued-statement 0)))
1278 (defvar objc-mode-map ()
1279 "Keymap used in objc-mode buffers.")
1282 (setq objc-mode-map (c-make-inherited-keymap))
1283 ;; add bindings which are only useful for Objective-C
1284 (define-key objc-mode-map "\C-c\C-e" 'c-macro-expand))
1286 (easy-menu-define c-objc-menu objc-mode-map "ObjC Mode Commands"
1287 (cons "ObjC" (c-lang-const c-mode-menu objc)))
1289 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.m\\'" . objc-mode))
1292 (define-derived-mode objc-mode prog-mode "ObjC"
1293 "Major mode for editing Objective C code.
1294 To submit a problem report, enter `\\[c-submit-bug-report]' from an
1295 objc-mode buffer. This automatically sets up a mail buffer with
1296 version information already added. You just need to add a description
1297 of the problem, including a reproducible test case, and send the
1300 To see what version of CC Mode you are running, enter `\\[c-version]'.
1302 The hook `c-mode-common-hook' is run with no args at mode
1303 initialization, then `objc-mode-hook'.
1307 (c-initialize-cc-mode t)
1308 (set-syntax-table objc-mode-syntax-table)
1309 (setq local-abbrev-table objc-mode-abbrev-table
1311 (use-local-map objc-mode-map)
1312 (c-init-language-vars-for 'objc-mode)
1313 (c-common-init 'objc-mode)
1314 (easy-menu-add c-objc-menu)
1315 (cc-imenu-init nil 'cc-imenu-objc-function)
1316 (c-run-mode-hooks 'c-mode-common-hook 'objc-mode-hook)
1317 (c-update-modeline))
1323 (defvar java-mode-syntax-table nil
1324 "Syntax table used in java-mode buffers.")
1325 (or java-mode-syntax-table
1326 (setq java-mode-syntax-table
1327 (funcall (c-lang-const c-make-mode-syntax-table java))))
1329 (defvar java-mode-abbrev-table nil
1330 "Abbreviation table used in java-mode buffers.")
1331 (c-define-abbrev-table 'java-mode-abbrev-table
1332 '(("else" "else" c-electric-continued-statement 0)
1333 ("while" "while" c-electric-continued-statement 0)
1334 ("catch" "catch" c-electric-continued-statement 0)
1335 ("finally" "finally" c-electric-continued-statement 0)))
1337 (defvar java-mode-map ()
1338 "Keymap used in java-mode buffers.")
1341 (setq java-mode-map (c-make-inherited-keymap))
1342 ;; add bindings which are only useful for Java
1345 ;; Regexp trying to describe the beginning of a Java top-level
1346 ;; definition. This is not used by CC Mode, nor is it maintained
1347 ;; since it's practically impossible to write a regexp that reliably
1348 ;; matches such a construct. Other tools are necessary.
1349 (defconst c-Java-defun-prompt-regexp
1350 "^[ \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:;.,{}()\x7f=]*\\|\\([_$a-zA-Z][_$.a-zA-Z0-9]*\\)\\)\\s-*\\(([^);{}]*)\\)?\\([] \t]*\\)\\(\\s-*\\<throws\\>\\s-*\\(\\([_$a-zA-Z][_$.a-zA-Z0-9]*\\)[, \t\n\r\f\v]*\\)+\\)?\\s-*")
1352 (easy-menu-define c-java-menu java-mode-map "Java Mode Commands"
1353 (cons "Java" (c-lang-const c-mode-menu java)))
1355 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.java\\'" . java-mode))
1358 (define-derived-mode java-mode prog-mode "Java"
1359 "Major mode for editing Java code.
1360 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1361 java-mode buffer. This automatically sets up a mail buffer with
1362 version information already added. You just need to add a description
1363 of the problem, including a reproducible test case, and send the
1366 To see what version of CC Mode you are running, enter `\\[c-version]'.
1368 The hook `c-mode-common-hook' is run with no args at mode
1369 initialization, then `java-mode-hook'.
1373 (c-initialize-cc-mode t)
1374 (set-syntax-table java-mode-syntax-table)
1375 (setq local-abbrev-table java-mode-abbrev-table
1377 (use-local-map java-mode-map)
1378 (c-init-language-vars-for 'java-mode)
1379 (c-common-init 'java-mode)
1380 (easy-menu-add c-java-menu)
1381 (cc-imenu-init cc-imenu-java-generic-expression)
1382 (c-run-mode-hooks 'c-mode-common-hook 'java-mode-hook)
1383 (c-update-modeline))
1386 ;; Support for CORBA's IDL language
1389 (defvar idl-mode-syntax-table nil
1390 "Syntax table used in idl-mode buffers.")
1391 (or idl-mode-syntax-table
1392 (setq idl-mode-syntax-table
1393 (funcall (c-lang-const c-make-mode-syntax-table idl))))
1395 (defvar idl-mode-abbrev-table nil
1396 "Abbreviation table used in idl-mode buffers.")
1397 (c-define-abbrev-table 'idl-mode-abbrev-table nil)
1399 (defvar idl-mode-map ()
1400 "Keymap used in idl-mode buffers.")
1403 (setq idl-mode-map (c-make-inherited-keymap))
1404 ;; add bindings which are only useful for IDL
1407 (easy-menu-define c-idl-menu idl-mode-map "IDL Mode Commands"
1408 (cons "IDL" (c-lang-const c-mode-menu idl)))
1410 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.idl\\'" . idl-mode))
1413 (define-derived-mode idl-mode prog-mode "IDL"
1414 "Major mode for editing CORBA's IDL, PSDL and CIDL code.
1415 To submit a problem report, enter `\\[c-submit-bug-report]' from an
1416 idl-mode buffer. This automatically sets up a mail buffer with
1417 version information already added. You just need to add a description
1418 of the problem, including a reproducible test case, and send the
1421 To see what version of CC Mode you are running, enter `\\[c-version]'.
1423 The hook `c-mode-common-hook' is run with no args at mode
1424 initialization, then `idl-mode-hook'.
1428 (c-initialize-cc-mode t)
1429 (set-syntax-table idl-mode-syntax-table)
1430 (setq local-abbrev-table idl-mode-abbrev-table)
1431 (use-local-map idl-mode-map)
1432 (c-init-language-vars-for 'idl-mode)
1433 (c-common-init 'idl-mode)
1434 (easy-menu-add c-idl-menu)
1435 ;;(cc-imenu-init cc-imenu-idl-generic-expression) ;TODO
1436 (c-run-mode-hooks 'c-mode-common-hook 'idl-mode-hook)
1437 (c-update-modeline))
1443 (defvar pike-mode-syntax-table nil
1444 "Syntax table used in pike-mode buffers.")
1445 (or pike-mode-syntax-table
1446 (setq pike-mode-syntax-table
1447 (funcall (c-lang-const c-make-mode-syntax-table pike))))
1449 (defvar pike-mode-abbrev-table nil
1450 "Abbreviation table used in pike-mode buffers.")
1451 (c-define-abbrev-table 'pike-mode-abbrev-table
1452 '(("else" "else" c-electric-continued-statement 0)
1453 ("while" "while" c-electric-continued-statement 0)))
1455 (defvar pike-mode-map ()
1456 "Keymap used in pike-mode buffers.")
1459 (setq pike-mode-map (c-make-inherited-keymap))
1460 ;; additional bindings
1461 (define-key pike-mode-map "\C-c\C-e" 'c-macro-expand))
1463 (easy-menu-define c-pike-menu pike-mode-map "Pike Mode Commands"
1464 (cons "Pike" (c-lang-const c-mode-menu pike)))
1466 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(u?lpc\\|pike\\|pmod\\(.in\\)?\\)\\'" . pike-mode))
1467 ;;;###autoload (add-to-list 'interpreter-mode-alist '("pike" . pike-mode))
1470 (define-derived-mode pike-mode prog-mode "Pike"
1471 "Major mode for editing Pike code.
1472 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1473 pike-mode buffer. This automatically sets up a mail buffer with
1474 version information already added. You just need to add a description
1475 of the problem, including a reproducible test case, and send the
1478 To see what version of CC Mode you are running, enter `\\[c-version]'.
1480 The hook `c-mode-common-hook' is run with no args at mode
1481 initialization, then `pike-mode-hook'.
1485 (c-initialize-cc-mode t)
1486 (set-syntax-table pike-mode-syntax-table)
1487 (setq local-abbrev-table pike-mode-abbrev-table
1489 (use-local-map pike-mode-map)
1490 (c-init-language-vars-for 'pike-mode)
1491 (c-common-init 'pike-mode)
1492 (easy-menu-add c-pike-menu)
1493 ;;(cc-imenu-init cc-imenu-pike-generic-expression) ;TODO
1494 (c-run-mode-hooks 'c-mode-common-hook 'pike-mode-hook)
1495 (c-update-modeline))
1500 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.awk\\'" . awk-mode))
1501 ;;;###autoload (add-to-list 'interpreter-mode-alist '("awk" . awk-mode))
1502 ;;;###autoload (add-to-list 'interpreter-mode-alist '("mawk" . awk-mode))
1503 ;;;###autoload (add-to-list 'interpreter-mode-alist '("nawk" . awk-mode))
1504 ;;;###autoload (add-to-list 'interpreter-mode-alist '("gawk" . awk-mode))
1506 ;;; Autoload directives must be on the top level, so we construct an
1507 ;;; autoload form instead.
1508 ;;;###autoload (autoload 'awk-mode "cc-mode" "Major mode for editing AWK code." t)
1510 (defvar awk-mode-abbrev-table nil
1511 "Abbreviation table used in awk-mode buffers.")
1512 (c-define-abbrev-table 'awk-mode-abbrev-table
1513 '(("else" "else" c-electric-continued-statement 0)
1514 ("while" "while" c-electric-continued-statement 0)))
1516 (defvar awk-mode-map ()
1517 "Keymap used in awk-mode buffers.")
1520 (setq awk-mode-map (c-make-inherited-keymap))
1521 ;; add bindings which are only useful for awk.
1522 (define-key awk-mode-map "#" 'self-insert-command)
1523 (define-key awk-mode-map "/" 'self-insert-command)
1524 (define-key awk-mode-map "*" 'self-insert-command)
1525 (define-key awk-mode-map "\C-c\C-n" 'undefined) ; #if doesn't exist in awk.
1526 (define-key awk-mode-map "\C-c\C-p" 'undefined)
1527 (define-key awk-mode-map "\C-c\C-u" 'undefined)
1528 (define-key awk-mode-map "\M-a" 'c-beginning-of-statement) ; 2003/10/7
1529 (define-key awk-mode-map "\M-e" 'c-end-of-statement) ; 2003/10/7
1530 (define-key awk-mode-map "\C-\M-a" 'c-awk-beginning-of-defun)
1531 (define-key awk-mode-map "\C-\M-e" 'c-awk-end-of-defun))
1533 (easy-menu-define c-awk-menu awk-mode-map "AWK Mode Commands"
1534 (cons "AWK" (c-lang-const c-mode-menu awk)))
1536 ;; (require 'cc-awk) brings these in.
1537 (defvar awk-mode-syntax-table)
1538 (declare-function c-awk-unstick-NL-prop "cc-awk" ())
1541 (define-derived-mode awk-mode prog-mode "AWK"
1542 "Major mode for editing AWK code.
1543 To submit a problem report, enter `\\[c-submit-bug-report]' from an
1544 awk-mode buffer. This automatically sets up a mail buffer with version
1545 information already added. You just need to add a description of the
1546 problem, including a reproducible test case, and send the message.
1548 To see what version of CC Mode you are running, enter `\\[c-version]'.
1550 The hook `c-mode-common-hook' is run with no args at mode
1551 initialization, then `awk-mode-hook'.
1555 (require 'cc-awk) ; Added 2003/6/10.
1556 (c-initialize-cc-mode t)
1557 (set-syntax-table awk-mode-syntax-table)
1558 (setq local-abbrev-table awk-mode-abbrev-table
1560 (use-local-map awk-mode-map)
1561 (c-init-language-vars-for 'awk-mode)
1562 (c-common-init 'awk-mode)
1563 (c-awk-unstick-NL-prop)
1565 ;; Prevent Xemacs's buffer-syntactic-context being used. See the comment
1566 ;; in cc-engine.el, just before (defun c-fast-in-literal ...
1567 (defalias 'c-in-literal 'c-slow-in-literal)
1569 (c-run-mode-hooks 'c-mode-common-hook 'awk-mode-hook)
1570 (c-update-modeline))
1575 (defconst c-mode-help-address
1576 "bug-cc-mode@gnu.org"
1577 "Address(es) for CC Mode bug reports.")
1580 "Echo the current version of CC Mode in the minibuffer."
1582 (message "Using CC Mode version %s" c-version)
1583 (c-keep-region-active))
1585 (defvar c-prepare-bug-report-hooks nil)
1587 ;; Dynamic variables used by reporter.
1588 (defvar reporter-prompt-for-summary-p)
1589 (defvar reporter-dont-compact-list)
1591 (defun c-submit-bug-report ()
1592 "Submit via mail a bug report on CC Mode."
1596 (let ((reporter-prompt-for-summary-p t)
1597 (reporter-dont-compact-list '(c-offsets-alist))
1598 (style c-indentation-style)
1599 (c-features c-emacs-features))
1601 (if (y-or-n-p "Do you want to submit a report on CC Mode? ")
1603 (reporter-submit-bug-report
1605 (concat "CC Mode " c-version " (" mode-name ")")
1608 '(c-buffer-is-cc-mode
1610 c-syntactic-indentation
1611 c-syntactic-indentation-in-macros
1613 c-auto-align-backslashes
1614 c-backspace-function
1616 c-electric-pound-behavior
1618 c-enable-xemacs-performance-kludge-p
1619 c-old-style-variable-behavior
1623 parse-sexp-ignore-comments
1624 parse-sexp-lookup-properties
1625 lookup-syntax-properties
1626 ;; A brain-damaged XEmacs only variable that, if
1627 ;; set to nil can cause all kinds of chaos.
1628 signal-error-on-buffer-boundary
1629 ;; Variables that affect line breaking and comments.
1639 adaptive-fill-regexp)
1641 (mapc (lambda (var) (unless (boundp var)
1642 (setq vars (delq var vars))))
1643 '(signal-error-on-buffer-boundary
1647 font-lock-maximum-decoration
1648 parse-sexp-lookup-properties
1649 lookup-syntax-properties))
1652 (run-hooks 'c-prepare-bug-report-hooks)
1653 (insert (format "Buffer Style: %s\nc-emacs-features: %s\n"
1654 style c-features)))))))
1657 (cc-provide 'cc-mode)
1659 ;;; cc-mode.el ends here