cl-lib.el: Partial revert of "2015-04-05 Rationalize c[ad]+r"
[emacs.git] / lisp / progmodes / cc-mode.el
blob1f58ba1ee9ba053ef935d063320cf75913ac7aa1
1 ;;; cc-mode.el --- major mode for editing C and similar languages
3 ;; Copyright (C) 1985, 1987, 1992-2015 Free Software Foundation, Inc.
5 ;; Authors: 2003- Alan Mackenzie
6 ;; 1998- Martin Stjernholm
7 ;; 1992-1999 Barry A. Warsaw
8 ;; 1987 Dave Detlefs
9 ;; 1987 Stewart Clamen
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/>.
30 ;;; Commentary:
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
50 ;; cplus-md1.el..
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
70 ;; visiting
72 ;; http://lists.sourceforge.net/mailman/listinfo/cc-mode-announce
74 ;;; Code:
76 ;; For Emacs < 22.2.
77 (eval-and-compile
78 (unless (fboundp 'declare-function) (defmacro declare-function (&rest _r))))
80 (eval-when-compile
81 (let ((load-path
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)
85 load-path)))
86 (load "cc-bytecomp" nil t)))
88 (cc-require 'cc-defs)
89 (cc-require 'cc-vars)
90 (cc-require-when-compile 'cc-langs)
91 (cc-require 'cc-engine)
92 (cc-require 'cc-styles)
93 (cc-require 'cc-cmds)
94 (cc-require 'cc-align)
95 (cc-require 'cc-menus)
96 (cc-require 'cc-guess)
98 ;; Silence the compiler.
99 (cc-bytecomp-defvar adaptive-fill-first-line-regexp) ; Emacs
100 (cc-bytecomp-defun run-mode-hooks) ; Emacs 21.1
102 ;; We set these variables during mode init, yet we don't require
103 ;; font-lock.
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 ;; Load cc-fonts first after font-lock is loaded, since it isn't
112 ;; necessary until font locking is requested.
113 ; (eval-after-load "font-lock" ; 2006-07-09: font-lock is now preloaded.
115 (require 'cc-fonts) ;)
118 ;; Other modes and packages which depend on CC Mode should do the
119 ;; following to make sure everything is loaded and available for their
120 ;; use:
122 ;; (require 'cc-mode)
124 ;; And in the major mode function:
126 ;; (c-initialize-cc-mode t)
127 ;; (c-init-language-vars some-mode)
128 ;; (c-common-init 'some-mode) ; Or perhaps (c-basic-common-init 'some-mode)
130 ;; If you're not writing a derived mode using the language variable
131 ;; system, then some-mode is one of the language modes directly
132 ;; supported by CC Mode. You can then use (c-init-language-vars-for
133 ;; 'some-mode) instead of `c-init-language-vars'.
134 ;; `c-init-language-vars-for' is a function that avoids the rather
135 ;; large expansion of `c-init-language-vars'.
137 ;; If you use `c-basic-common-init' then you might want to call
138 ;; `c-font-lock-init' too to set up CC Mode's font lock support.
140 ;; See cc-langs.el for further info. A small example of a derived mode
141 ;; is also available at <http://cc-mode.sourceforge.net/
142 ;; derived-mode-ex.el>.
144 (defun c-leave-cc-mode-mode ()
145 (setq c-buffer-is-cc-mode nil))
147 (defun c-init-language-vars-for (mode)
148 "Initialize the language variables for one of the language modes
149 directly supported by CC Mode. This can be used instead of the
150 `c-init-language-vars' macro if the language you want to use is one of
151 those, rather than a derived language defined through the language
152 variable system (see \"cc-langs.el\")."
153 (cond ((eq mode 'c-mode) (c-init-language-vars c-mode))
154 ((eq mode 'c++-mode) (c-init-language-vars c++-mode))
155 ((eq mode 'objc-mode) (c-init-language-vars objc-mode))
156 ((eq mode 'java-mode) (c-init-language-vars java-mode))
157 ((eq mode 'idl-mode) (c-init-language-vars idl-mode))
158 ((eq mode 'pike-mode) (c-init-language-vars pike-mode))
159 ((eq mode 'awk-mode) (c-init-language-vars awk-mode))
160 (t (error "Unsupported mode %s" mode))))
162 ;;;###autoload
163 (defun c-initialize-cc-mode (&optional new-style-init)
164 "Initialize CC Mode for use in the current buffer.
165 If the optional NEW-STYLE-INIT is nil or left out then all necessary
166 initialization to run CC Mode for the C language is done. Otherwise
167 only some basic setup is done, and a call to `c-init-language-vars' or
168 `c-init-language-vars-for' is necessary too (which gives more
169 control). See \"cc-mode.el\" for more info."
171 (setq c-buffer-is-cc-mode t)
173 (let ((initprop 'cc-mode-is-initialized)
174 c-initialization-ok)
175 (unless (get 'c-initialize-cc-mode initprop)
176 (unwind-protect
177 (progn
178 (put 'c-initialize-cc-mode initprop t)
179 (c-initialize-builtin-style)
180 (run-hooks 'c-initialization-hook)
181 ;; Fix obsolete variables.
182 (if (boundp 'c-comment-continuation-stars)
183 (setq c-block-comment-prefix c-comment-continuation-stars))
184 (add-hook 'change-major-mode-hook 'c-leave-cc-mode-mode)
185 (setq c-initialization-ok t)
186 ;; Connect up with Emacs's electric-indent-mode, for >= Emacs 24.4
187 (when (fboundp 'electric-indent-local-mode)
188 (add-hook 'electric-indent-mode-hook 'c-electric-indent-mode-hook)
189 (add-hook 'electric-indent-local-mode-hook
190 'c-electric-indent-local-mode-hook)))
191 ;; Will try initialization hooks again if they failed.
192 (put 'c-initialize-cc-mode initprop c-initialization-ok))))
194 (unless new-style-init
195 (c-init-language-vars-for 'c-mode)))
198 ;;; Common routines.
200 (defvar c-mode-base-map ()
201 "Keymap shared by all CC Mode related modes.")
203 (defun c-make-inherited-keymap ()
204 (let ((map (make-sparse-keymap)))
205 ;; Necessary to use `cc-bytecomp-fboundp' below since this
206 ;; function is called from top-level forms that are evaluated
207 ;; while cc-bytecomp is active when one does M-x eval-buffer.
208 (cond
209 ;; Emacs
210 ((cc-bytecomp-fboundp 'set-keymap-parent)
211 (set-keymap-parent map c-mode-base-map))
212 ;; XEmacs
213 ((fboundp 'set-keymap-parents)
214 (set-keymap-parents map c-mode-base-map))
215 ;; incompatible
216 (t (error "CC Mode is incompatible with this version of Emacs")))
217 map))
219 (defun c-define-abbrev-table (name defs &optional doc)
220 ;; Compatibility wrapper for `define-abbrev' which passes a non-nil
221 ;; sixth argument for SYSTEM-FLAG in emacsen that support it
222 ;; (currently only Emacs >= 21.2).
223 (let ((table (or (and (boundp name) (symbol-value name))
224 (progn (condition-case nil
225 (define-abbrev-table name nil doc)
226 (wrong-number-of-arguments ;E.g. Emacs<23.
227 (eval `(defvar ,name nil ,doc))
228 (define-abbrev-table name nil)))
229 (symbol-value name)))))
230 (while defs
231 (condition-case nil
232 (apply 'define-abbrev table (append (car defs) '(t)))
233 (wrong-number-of-arguments
234 (apply 'define-abbrev table (car defs))))
235 (setq defs (cdr defs)))))
236 (put 'c-define-abbrev-table 'lisp-indent-function 1)
238 (defun c-bind-special-erase-keys ()
239 ;; Only used in Emacs to bind C-c C-<delete> and C-c C-<backspace>
240 ;; to the proper keys depending on `normal-erase-is-backspace'.
241 (if normal-erase-is-backspace
242 (progn
243 (define-key c-mode-base-map (kbd "C-c C-<delete>")
244 'c-hungry-delete-forward)
245 (define-key c-mode-base-map (kbd "C-c C-<backspace>")
246 'c-hungry-delete-backwards))
247 (define-key c-mode-base-map (kbd "C-c C-<delete>")
248 'c-hungry-delete-backwards)
249 (define-key c-mode-base-map (kbd "C-c C-<backspace>")
250 'c-hungry-delete-forward)))
252 (if c-mode-base-map
255 (setq c-mode-base-map (make-sparse-keymap))
257 ;; Separate M-BS from C-M-h. The former should remain
258 ;; backward-kill-word.
259 (define-key c-mode-base-map [(control meta h)] 'c-mark-function)
260 (define-key c-mode-base-map "\e\C-q" 'c-indent-exp)
261 (substitute-key-definition 'backward-sentence
262 'c-beginning-of-statement
263 c-mode-base-map global-map)
264 (substitute-key-definition 'forward-sentence
265 'c-end-of-statement
266 c-mode-base-map global-map)
267 (substitute-key-definition 'indent-new-comment-line
268 'c-indent-new-comment-line
269 c-mode-base-map global-map)
270 (substitute-key-definition 'indent-for-tab-command
271 ;; XXX Is this the right thing to do
272 ;; here?
273 'c-indent-line-or-region
274 c-mode-base-map global-map)
275 (when (fboundp 'comment-indent-new-line)
276 ;; indent-new-comment-line has changed name to
277 ;; comment-indent-new-line in Emacs 21.
278 (substitute-key-definition 'comment-indent-new-line
279 'c-indent-new-comment-line
280 c-mode-base-map global-map))
282 ;; RMS says don't make these the default.
283 ;; (April 2006): RMS has now approved these commands as defaults.
284 (unless (memq 'argumentative-bod-function c-emacs-features)
285 (define-key c-mode-base-map "\e\C-a" 'c-beginning-of-defun)
286 (define-key c-mode-base-map "\e\C-e" 'c-end-of-defun))
288 (define-key c-mode-base-map "\C-c\C-n" 'c-forward-conditional)
289 (define-key c-mode-base-map "\C-c\C-p" 'c-backward-conditional)
290 (define-key c-mode-base-map "\C-c\C-u" 'c-up-conditional)
292 ;; It doesn't suffice to put `c-fill-paragraph' on
293 ;; `fill-paragraph-function' since `c-fill-paragraph' must be called
294 ;; before any fill prefix adaption is done. E.g. `filladapt-mode'
295 ;; replaces `fill-paragraph' and does the adaption before calling
296 ;; `fill-paragraph-function', and we have to mask comments etc
297 ;; before that. Also, `c-fill-paragraph' chains on to
298 ;; `fill-paragraph' and the value on `fill-paragraph-function' to
299 ;; do the actual filling work.
300 (substitute-key-definition 'fill-paragraph 'c-fill-paragraph
301 c-mode-base-map global-map)
302 ;; In XEmacs the default fill function is called
303 ;; fill-paragraph-or-region.
304 (substitute-key-definition 'fill-paragraph-or-region 'c-fill-paragraph
305 c-mode-base-map global-map)
307 ;; We bind the forward deletion key and (implicitly) C-d to
308 ;; `c-electric-delete-forward', and the backward deletion key to
309 ;; `c-electric-backspace'. The hungry variants are bound to the
310 ;; same keys but prefixed with C-c. This implies that C-c C-d is
311 ;; `c-hungry-delete-forward'. For consistency, we bind not only C-c
312 ;; <backspace> to `c-hungry-delete-backwards' but also
313 ;; C-c C-<backspace>, so that the Ctrl key can be held down during
314 ;; the whole sequence regardless of the direction. This in turn
315 ;; implies that we bind C-c C-<delete> to `c-hungry-delete-forward',
316 ;; for the same reason.
318 ;; Bind the electric deletion functions to C-d and DEL. Emacs 21
319 ;; automatically maps the [delete] and [backspace] keys to these two
320 ;; depending on window system and user preferences. (In earlier
321 ;; versions it's possible to do the same by using `function-key-map'.)
322 (define-key c-mode-base-map "\C-d" 'c-electric-delete-forward)
323 (define-key c-mode-base-map "\177" 'c-electric-backspace)
324 (define-key c-mode-base-map "\C-c\C-d" 'c-hungry-delete-forward)
325 (define-key c-mode-base-map [?\C-c ?\d] 'c-hungry-delete-backwards)
326 (define-key c-mode-base-map [?\C-c ?\C-\d] 'c-hungry-delete-backwards)
327 (define-key c-mode-base-map [?\C-c deletechar] 'c-hungry-delete-forward) ; C-c <delete> on a tty.
328 (define-key c-mode-base-map [?\C-c (control deletechar)] ; C-c C-<delete> on a tty.
329 'c-hungry-delete-forward)
330 (when (boundp 'normal-erase-is-backspace)
331 ;; The automatic C-d and DEL mapping functionality doesn't extend
332 ;; to special combinations like C-c C-<delete>, so we have to hook
333 ;; into the `normal-erase-is-backspace' system to bind it directly
334 ;; as appropriate.
335 (add-hook 'normal-erase-is-backspace-hook 'c-bind-special-erase-keys)
336 (c-bind-special-erase-keys))
338 (when (fboundp 'delete-forward-p)
339 ;; In XEmacs we fix the forward and backward deletion behavior by
340 ;; binding the keysyms for the [delete] and [backspace] keys
341 ;; directly, and use `delete-forward-p' to decide what [delete]
342 ;; should do. That's done in the XEmacs specific
343 ;; `c-electric-delete' and `c-hungry-delete' functions.
344 (define-key c-mode-base-map [delete] 'c-electric-delete)
345 (define-key c-mode-base-map [backspace] 'c-electric-backspace)
346 (define-key c-mode-base-map (kbd "C-c <delete>") 'c-hungry-delete)
347 (define-key c-mode-base-map (kbd "C-c C-<delete>") 'c-hungry-delete)
348 (define-key c-mode-base-map (kbd "C-c <backspace>")
349 'c-hungry-delete-backwards)
350 (define-key c-mode-base-map (kbd "C-c C-<backspace>")
351 'c-hungry-delete-backwards))
353 (define-key c-mode-base-map "#" 'c-electric-pound)
354 (define-key c-mode-base-map "{" 'c-electric-brace)
355 (define-key c-mode-base-map "}" 'c-electric-brace)
356 (define-key c-mode-base-map "/" 'c-electric-slash)
357 (define-key c-mode-base-map "*" 'c-electric-star)
358 (define-key c-mode-base-map ";" 'c-electric-semi&comma)
359 (define-key c-mode-base-map "," 'c-electric-semi&comma)
360 (define-key c-mode-base-map ":" 'c-electric-colon)
361 (define-key c-mode-base-map "(" 'c-electric-paren)
362 (define-key c-mode-base-map ")" 'c-electric-paren)
364 (define-key c-mode-base-map "\C-c\C-\\" 'c-backslash-region)
365 (define-key c-mode-base-map "\C-c\C-a" 'c-toggle-auto-newline)
366 (define-key c-mode-base-map "\C-c\C-b" 'c-submit-bug-report)
367 (define-key c-mode-base-map "\C-c\C-c" 'comment-region)
368 (define-key c-mode-base-map "\C-c\C-l" 'c-toggle-electric-state)
369 (define-key c-mode-base-map "\C-c\C-o" 'c-set-offset)
370 (define-key c-mode-base-map "\C-c\C-q" 'c-indent-defun)
371 (define-key c-mode-base-map "\C-c\C-s" 'c-show-syntactic-information)
372 ;; (define-key c-mode-base-map "\C-c\C-t" 'c-toggle-auto-hungry-state) Commented out by ACM, 2005-03-05.
373 (define-key c-mode-base-map "\C-c." 'c-set-style)
374 ;; conflicts with OOBR
375 ;;(define-key c-mode-base-map "\C-c\C-v" 'c-version)
376 ;; (define-key c-mode-base-map "\C-c\C-y" 'c-toggle-hungry-state) Commented out by ACM, 2005-11-22.
377 (define-key c-mode-base-map "\C-c\C-w" 'c-subword-mode)
380 ;; We don't require the outline package, but we configure it a bit anyway.
381 (cc-bytecomp-defvar outline-level)
383 (defun c-mode-menu (modestr)
384 "Return a menu spec suitable for `easy-menu-define' that is exactly
385 like the C mode menu except that the menu bar item name is MODESTR
386 instead of \"C\".
388 This function is provided for compatibility only; derived modes should
389 preferably use the `c-mode-menu' language constant directly."
390 (cons modestr (c-lang-const c-mode-menu c)))
392 ;; Ugly hack to pull in the definition of `c-populate-syntax-table'
393 ;; from cc-langs to make it available at runtime. It's either this or
394 ;; moving the definition for it to cc-defs, but that would mean to
395 ;; break up the syntax table setup over two files.
396 (defalias 'c-populate-syntax-table
397 (cc-eval-when-compile
398 (let ((f (symbol-function 'c-populate-syntax-table)))
399 (if (byte-code-function-p f) f (byte-compile f)))))
401 ;; CAUTION: Try to avoid installing things on
402 ;; `before-change-functions'. The macro `combine-after-change-calls'
403 ;; is used and it doesn't work if there are things on that hook. That
404 ;; can cause font lock functions to run in inconvenient places during
405 ;; temporary changes in some font lock support modes, causing extra
406 ;; unnecessary work and font lock glitches due to interactions between
407 ;; various text properties.
409 ;; (2007-02-12): The macro `combine-after-change-calls' ISN'T used any
410 ;; more.
412 (defun c-unfind-enclosing-token (pos)
413 ;; If POS is wholly inside a token, remove that id from
414 ;; `c-found-types', should it be present. Return t if we were in an
415 ;; id, else nil.
416 (save-excursion
417 (let ((tok-beg (progn (goto-char pos)
418 (and (c-beginning-of-current-token) (point))))
419 (tok-end (progn (goto-char pos)
420 (and (c-end-of-current-token) (point)))))
421 (when (and tok-beg tok-end)
422 (c-unfind-type (buffer-substring-no-properties tok-beg tok-end))
423 t))))
425 (defun c-unfind-coalesced-tokens (beg end)
426 ;; unless the non-empty region (beg end) is entirely WS and there's at
427 ;; least one character of WS just before or after this region, remove
428 ;; the tokens which touch the region from `c-found-types' should they
429 ;; be present.
430 (or (c-partial-ws-p beg end)
431 (save-excursion
432 (progn
433 (goto-char beg)
434 (or (eq beg (point-min))
435 (c-skip-ws-backward (1- beg))
436 (/= (point) beg)
437 (= (c-backward-token-2) 1)
438 (c-unfind-type (buffer-substring-no-properties
439 (point) beg)))
440 (goto-char end)
441 (or (eq end (point-max))
442 (c-skip-ws-forward (1+ end))
443 (/= (point) end)
444 (progn (forward-char) (c-end-of-current-token) nil)
445 (c-unfind-type (buffer-substring-no-properties
446 end (point))))))))
448 ;; c-maybe-stale-found-type records a place near the region being
449 ;; changed where an element of `found-types' might become stale. It
450 ;; is set in c-before-change and is either nil, or has the form:
452 ;; (c-decl-id-start "foo" 97 107 " (* ooka) " "o"), where
454 ;; o - `c-decl-id-start' is the c-type text property value at buffer
455 ;; pos 96.
457 ;; o - 97 107 is the region potentially containing the stale type -
458 ;; this is delimited by a non-nil c-type text property at 96 and
459 ;; either another one or a ";", "{", or "}" at 107.
461 ;; o - " (* ooka) " is the (before change) buffer portion containing
462 ;; the suspect type (here "ooka").
464 ;; o - "o" is the buffer contents which is about to be deleted. This
465 ;; would be the empty string for an insertion.
466 (defvar c-maybe-stale-found-type nil)
467 (make-variable-buffer-local 'c-maybe-stale-found-type)
469 (defvar c-just-done-before-change nil)
470 (make-variable-buffer-local 'c-just-done-before-change)
471 ;; This variable is set to t by `c-before-change' and to nil by
472 ;; `c-after-change'. It is used to detect a spurious invocation of
473 ;; `before-change-functions' directly following on from a correct one. This
474 ;; happens in some Emacsen, for example when `basic-save-buffer' does (insert
475 ;; ?\n) when `require-final-newline' is non-nil.
477 (defun c-basic-common-init (mode default-style)
478 "Do the necessary initialization for the syntax handling routines
479 and the line breaking/filling code. Intended to be used by other
480 packages that embed CC Mode.
482 MODE is the CC Mode flavor to set up, e.g. 'c-mode or 'java-mode.
483 DEFAULT-STYLE tells which indentation style to install. It has the
484 same format as `c-default-style'.
486 Note that `c-init-language-vars' must be called before this function.
487 This function cannot do that since `c-init-language-vars' is a macro
488 that requires a literal mode spec at compile time."
490 (setq c-buffer-is-cc-mode mode)
492 ;; these variables should always be buffer local; they do not affect
493 ;; indentation style.
494 (make-local-variable 'comment-start)
495 (make-local-variable 'comment-end)
496 (make-local-variable 'comment-start-skip)
498 (make-local-variable 'paragraph-start)
499 (make-local-variable 'paragraph-separate)
500 (make-local-variable 'paragraph-ignore-fill-prefix)
501 (make-local-variable 'adaptive-fill-mode)
502 (make-local-variable 'adaptive-fill-regexp)
503 (make-local-variable 'fill-paragraph-handle-comment)
505 ;; now set their values
506 (set (make-local-variable 'parse-sexp-ignore-comments) t)
507 (set (make-local-variable 'indent-line-function) 'c-indent-line)
508 (set (make-local-variable 'indent-region-function) 'c-indent-region)
509 (set (make-local-variable 'normal-auto-fill-function) 'c-do-auto-fill)
510 (set (make-local-variable 'comment-multi-line) t)
511 (set (make-local-variable 'comment-line-break-function)
512 'c-indent-new-comment-line)
514 ;; For the benefit of adaptive file, which otherwise mis-fills.
515 (setq fill-paragraph-handle-comment nil)
517 ;; Install `c-fill-paragraph' on `fill-paragraph-function' so that a
518 ;; direct call to `fill-paragraph' behaves better. This still
519 ;; doesn't work with filladapt but it's better than nothing.
520 (set (make-local-variable 'fill-paragraph-function) 'c-fill-paragraph)
522 ;; Initialize the cache of brace pairs, and opening braces/brackets/parens.
523 (c-state-cache-init)
525 (when (or c-recognize-<>-arglists
526 (c-major-mode-is 'awk-mode)
527 (c-major-mode-is '(java-mode c-mode c++-mode objc-mode)))
528 ;; We'll use the syntax-table text property to change the syntax
529 ;; of some chars for this language, so do the necessary setup for
530 ;; that.
532 ;; Note to other package developers: It's ok to turn this on in CC
533 ;; Mode buffers when CC Mode doesn't, but it's not ok to turn it
534 ;; off if CC Mode has turned it on.
536 ;; Emacs.
537 (when (boundp 'parse-sexp-lookup-properties)
538 (set (make-local-variable 'parse-sexp-lookup-properties) t))
540 ;; Same as above for XEmacs.
541 (when (boundp 'lookup-syntax-properties)
542 (set (make-local-variable 'lookup-syntax-properties) t)))
544 ;; Use this in Emacs 21+ to avoid meddling with the rear-nonsticky
545 ;; property on each character.
546 (when (boundp 'text-property-default-nonsticky)
547 (make-local-variable 'text-property-default-nonsticky)
548 (mapc (lambda (tprop)
549 (unless (assq tprop text-property-default-nonsticky)
550 (setq text-property-default-nonsticky
551 (cons `(,tprop . t) text-property-default-nonsticky))))
552 '(syntax-table category c-type)))
554 ;; In Emacs 21 and later it's possible to turn off the ad-hoc
555 ;; heuristic that open parens in column 0 are defun starters. Since
556 ;; we have c-state-cache, that heuristic isn't useful and only causes
557 ;; trouble, so turn it off.
558 ;; 2006/12/17: This facility is somewhat confused, and doesn't really seem
559 ;; helpful. Comment it out for now.
560 ;; (when (memq 'col-0-paren c-emacs-features)
561 ;; (make-local-variable 'open-paren-in-column-0-is-defun-start)
562 ;; (setq open-paren-in-column-0-is-defun-start nil))
564 (c-clear-found-types)
566 ;; now set the mode style based on default-style
567 (let ((style (cc-choose-style-for-mode mode default-style)))
568 ;; Override style variables if `c-old-style-variable-behavior' is
569 ;; set. Also override if we are using global style variables,
570 ;; have already initialized a style once, and are switching to a
571 ;; different style. (It's doubtful whether this is desirable, but
572 ;; the whole situation with nonlocal style variables is a bit
573 ;; awkward. It's at least the most compatible way with the old
574 ;; style init procedure.)
575 (c-set-style style (not (or c-old-style-variable-behavior
576 (and (not c-style-variables-are-local-p)
577 c-indentation-style
578 (not (string-equal c-indentation-style
579 style)))))))
580 (c-setup-paragraph-variables)
582 ;; we have to do something special for c-offsets-alist so that the
583 ;; buffer local value has its own alist structure.
584 (setq c-offsets-alist (copy-alist c-offsets-alist))
586 ;; setup the comment indent variable in a Emacs version portable way
587 (set (make-local-variable 'comment-indent-function) 'c-comment-indent)
589 ;; In Emacs 24.4 onwards, prevent Emacs's built in electric indentation from
590 ;; messing up CC Mode's, and set `c-electric-flag' if `electric-indent-mode'
591 ;; has been called by the user.
592 (when (boundp 'electric-indent-inhibit) (setq electric-indent-inhibit t))
593 ;; CC-mode should obey Emacs's generic preferences, tho only do it if
594 ;; Emacs's generic preferences can be set per-buffer (Emacs>=24.4).
595 (when (fboundp 'electric-indent-local-mode)
596 (setq c-electric-flag electric-indent-mode))
598 ;; ;; Put submode indicators onto minor-mode-alist, but only once.
599 ;; (or (assq 'c-submode-indicators minor-mode-alist)
600 ;; (setq minor-mode-alist
601 ;; (cons '(c-submode-indicators c-submode-indicators)
602 ;; minor-mode-alist)))
603 (c-update-modeline)
605 ;; Install the functions that ensure that various internal caches
606 ;; don't become invalid due to buffer changes.
607 (when (featurep 'xemacs)
608 (make-local-hook 'before-change-functions)
609 (make-local-hook 'after-change-functions))
610 (add-hook 'before-change-functions 'c-before-change nil t)
611 (setq c-just-done-before-change nil)
612 (add-hook 'after-change-functions 'c-after-change nil t)
613 (when (boundp 'font-lock-extend-after-change-region-function)
614 (set (make-local-variable 'font-lock-extend-after-change-region-function)
615 'c-extend-after-change-region))) ; Currently (2009-05) used by all
616 ; languages with #define (C, C++,; ObjC), and by AWK.
618 (defun c-setup-doc-comment-style ()
619 "Initialize the variables that depend on the value of `c-doc-comment-style'."
620 (when (and (featurep 'font-lock)
621 (symbol-value 'font-lock-mode))
622 ;; Force font lock mode to reinitialize itself.
623 (font-lock-mode 0)
624 (font-lock-mode 1)))
626 ;; Buffer local variables defining the region to be fontified by a font lock
627 ;; after-change function. They are set in c-after-change to
628 ;; after-change-functions' BEG and END, and may be modified by functions in
629 ;; `c-before-font-lock-functions'.
630 (defvar c-new-BEG 0)
631 (make-variable-buffer-local 'c-new-BEG)
632 (defvar c-new-END 0)
633 (make-variable-buffer-local 'c-new-END)
635 (defun c-common-init (&optional mode)
636 "Common initialization for all CC Mode modes.
637 In addition to the work done by `c-basic-common-init' and
638 `c-font-lock-init', this function sets up various other things as
639 customary in CC Mode modes but which aren't strictly necessary for CC
640 Mode to operate correctly.
642 MODE is the symbol for the mode to initialize, like 'c-mode. See
643 `c-basic-common-init' for details. It's only optional to be
644 compatible with old code; callers should always specify it."
646 (unless mode
647 ;; Called from an old third party package. The fallback is to
648 ;; initialize for C.
649 (c-init-language-vars-for 'c-mode))
651 (c-basic-common-init mode c-default-style)
652 (when mode
653 ;; Only initialize font locking if we aren't called from an old package.
654 (c-font-lock-init))
656 ;; Starting a mode is a sort of "change". So call the change functions...
657 (save-restriction
658 (widen)
659 (setq c-new-BEG (point-min))
660 (setq c-new-END (point-max))
661 (save-excursion
662 (let (before-change-functions after-change-functions)
663 (mapc (lambda (fn)
664 (funcall fn (point-min) (point-max)))
665 c-get-state-before-change-functions)
666 (mapc (lambda (fn)
667 (funcall fn (point-min) (point-max)
668 (- (point-max) (point-min))))
669 c-before-font-lock-functions))))
671 (set (make-local-variable 'outline-regexp) "[^#\n\^M]")
672 (set (make-local-variable 'outline-level) 'c-outline-level)
673 (set (make-local-variable 'add-log-current-defun-function)
674 (lambda ()
675 (or (c-cpp-define-name) (c-defun-name))))
676 (let ((rfn (assq mode c-require-final-newline)))
677 (when rfn
678 (if (boundp 'mode-require-final-newline)
679 (and (cdr rfn)
680 (set (make-local-variable 'require-final-newline)
681 mode-require-final-newline))
682 (set (make-local-variable 'require-final-newline) (cdr rfn))))))
684 (defun c-count-cfss (lv-alist)
685 ;; LV-ALIST is an alist like `file-local-variables-alist'. Count how many
686 ;; elements with the key `c-file-style' there are in it.
687 (let ((elt-ptr lv-alist) elt (cownt 0))
688 (while elt-ptr
689 (setq elt (car elt-ptr)
690 elt-ptr (cdr elt-ptr))
691 (when (eq (car elt) 'c-file-style)
692 (setq cownt (1+ cownt))))
693 cownt))
695 (defun c-before-hack-hook ()
696 "Set the CC Mode style and \"offsets\" when in the buffer's local variables.
697 They are set only when, respectively, the pseudo variables
698 `c-file-style' and `c-file-offsets' are present in the list.
700 This function is called from the hook `before-hack-local-variables-hook'."
701 (when c-buffer-is-cc-mode
702 (let ((mode-cons (assq 'mode file-local-variables-alist))
703 (stile (cdr (assq 'c-file-style file-local-variables-alist)))
704 (offsets (cdr (assq 'c-file-offsets file-local-variables-alist))))
705 (when mode-cons
706 (hack-one-local-variable (car mode-cons) (cdr mode-cons))
707 (setq file-local-variables-alist
708 (delq mode-cons file-local-variables-alist)))
709 (when stile
710 (or (stringp stile) (error "c-file-style is not a string"))
711 (if (boundp 'dir-local-variables-alist)
712 ;; Determine whether `c-file-style' was set in the file's local
713 ;; variables or in a .dir-locals.el (a directory setting).
714 (let ((cfs-in-file-and-dir-count
715 (c-count-cfss file-local-variables-alist))
716 (cfs-in-dir-count (c-count-cfss dir-local-variables-alist)))
717 (c-set-style stile
718 (and (= cfs-in-file-and-dir-count cfs-in-dir-count)
719 'keep-defaults)))
720 (c-set-style stile)))
721 (when offsets
722 (mapc
723 (lambda (langentry)
724 (let ((langelem (car langentry))
725 (offset (cdr langentry)))
726 (c-set-offset langelem offset)))
727 offsets)))))
729 (defun c-remove-any-local-eval-or-mode-variables ()
730 ;; If the buffer specifies `mode' or `eval' in its File Local Variable list
731 ;; or on the first line, remove all occurrences. See
732 ;; `c-postprocess-file-styles' for justification. There is no need to save
733 ;; point here, or even bother too much about the buffer contents. However,
734 ;; DON'T mess up the kill-ring.
736 ;; Most of the code here is derived from Emacs 21.3's `hack-local-variables'
737 ;; in files.el.
738 (goto-char (point-max))
739 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
740 (let (lv-point (prefix "") (suffix ""))
741 (when (let ((case-fold-search t))
742 (search-forward "Local Variables:" nil t))
743 (setq lv-point (point))
744 ;; The prefix is what comes before "local variables:" in its line.
745 ;; The suffix is what comes after "local variables:" in its line.
746 (skip-chars-forward " \t")
747 (or (eolp)
748 (setq suffix (buffer-substring (point)
749 (progn (end-of-line) (point)))))
750 (goto-char (match-beginning 0))
751 (or (bolp)
752 (setq prefix
753 (buffer-substring (point)
754 (progn (beginning-of-line) (point)))))
756 (while (search-forward-regexp
757 (concat "^[ \t]*"
758 (regexp-quote prefix)
759 "\\(mode\\|eval\\):.*"
760 (regexp-quote suffix)
761 "$")
762 nil t)
763 (forward-line 0)
764 (delete-region (point) (progn (forward-line) (point)))))
766 ;; Delete the first line, if we've got one, in case it contains a mode spec.
767 (unless (and lv-point
768 (progn (goto-char lv-point)
769 (forward-line 0)
770 (bobp)))
771 (goto-char (point-min))
772 (unless (eobp)
773 (delete-region (point) (progn (forward-line) (point)))))))
775 (defun c-postprocess-file-styles ()
776 "Function that post processes relevant file local variables in CC Mode.
777 Currently, this function simply applies any style and offset settings
778 found in the file's Local Variable list. It first applies any style
779 setting found in `c-file-style', then it applies any offset settings
780 it finds in `c-file-offsets'.
782 Note that the style variables are always made local to the buffer."
784 ;; apply file styles and offsets
785 (when c-buffer-is-cc-mode
786 (if (or c-file-style c-file-offsets)
787 (c-make-styles-buffer-local t))
788 (when c-file-style
789 (or (stringp c-file-style)
790 (error "c-file-style is not a string"))
791 (c-set-style c-file-style))
793 (and c-file-offsets
794 (mapc
795 (lambda (langentry)
796 (let ((langelem (car langentry))
797 (offset (cdr langentry)))
798 (c-set-offset langelem offset)))
799 c-file-offsets))
800 ;; Problem: The file local variable block might have explicitly set a
801 ;; style variable. The `c-set-style' or `mapcar' call might have
802 ;; overwritten this. So we run `hack-local-variables' again to remedy
803 ;; this. There are no guarantees this will work properly, particularly as
804 ;; we have no control over what the other hook functions on
805 ;; `hack-local-variables-hook' would have done. We now (2006/2/1) remove
806 ;; any `eval' or `mode' expressions before we evaluate again (see below).
807 ;; ACM, 2005/11/2.
809 ;; Problem (bug reported by Gustav Broberg): if one of the variables is
810 ;; `mode', this will invoke c-mode (etc.) again, setting up the style etc.
811 ;; We prevent this by temporarily removing `mode' from the Local Variables
812 ;; section.
813 (if (or c-file-style c-file-offsets)
814 (let ((hack-local-variables-hook nil) (inhibit-read-only t))
815 (c-tentative-buffer-changes
816 (c-remove-any-local-eval-or-mode-variables)
817 (hack-local-variables))
818 nil))))
820 (if (boundp 'before-hack-local-variables-hook)
821 (add-hook 'before-hack-local-variables-hook 'c-before-hack-hook)
822 (add-hook 'hack-local-variables-hook 'c-postprocess-file-styles))
824 (defmacro c-run-mode-hooks (&rest hooks)
825 ;; Emacs 21.1 has introduced a system with delayed mode hooks that
826 ;; requires the use of the new function `run-mode-hooks'.
827 (if (cc-bytecomp-fboundp 'run-mode-hooks)
828 `(run-mode-hooks ,@hooks)
829 `(progn ,@(mapcar (lambda (hook) `(run-hooks ,hook)) hooks))))
832 ;;; Change hooks, linking with Font Lock and electric-indent-mode.
834 ;; Buffer local variables recording Beginning/End-of-Macro position before a
835 ;; change, when a macro straddles, respectively, the BEG or END (or both) of
836 ;; the change region. Otherwise these have the values BEG/END.
837 (defvar c-old-BOM 0)
838 (make-variable-buffer-local 'c-old-BOM)
839 (defvar c-old-EOM 0)
840 (make-variable-buffer-local 'c-old-EOM)
842 (defun c-extend-region-for-CPP (beg end)
843 ;; Set c-old-BOM or c-old-EOM respectively to BEG, END, each extended to the
844 ;; beginning/end of any preprocessor construct they may be in.
846 ;; Point is undefined both before and after this function call; the buffer
847 ;; has already been widened, and match-data saved. The return value is
848 ;; meaningless.
850 ;; This function is in the C/C++/ObjC values of
851 ;; `c-get-state-before-change-functions' and is called exclusively as a
852 ;; before change function.
853 (goto-char beg)
854 (c-beginning-of-macro)
855 (setq c-old-BOM (point))
857 (goto-char end)
858 (when (c-beginning-of-macro)
859 (c-end-of-macro)
860 (or (eobp) (forward-char))) ; Over the terminating NL which may be marked
861 ; with a c-cpp-delimiter category property
862 (setq c-old-EOM (point)))
864 (defun c-extend-font-lock-region-for-macros (begg endd &optional old-len)
865 ;; Extend the region (BEGG ENDD) to cover all (possibly changed)
866 ;; preprocessor macros; return the cons (new-BEG . new-END). OLD-LEN should
867 ;; be either the old length parameter when called from an
868 ;; after-change-function, or nil otherwise. This defun uses the variables
869 ;; c-old-BOM, c-new-BOM.
871 ;; Point is undefined on both entry and exit to this function. The buffer
872 ;; will have been widened on entry.
873 (let (limits new-beg new-end)
874 (goto-char c-old-BOM) ; already set to old start of macro or begg.
875 (setq new-beg
876 (min begg
877 (if (setq limits (c-state-literal-at (point)))
878 (cdr limits) ; go forward out of any string or comment.
879 (point))))
881 (goto-char endd)
882 (if (setq limits (c-state-literal-at (point)))
883 (goto-char (car limits))) ; go backward out of any string or comment.
884 (if (c-beginning-of-macro)
885 (c-end-of-macro))
886 (setq new-end (max endd
887 (if old-len
888 (+ (- c-old-EOM old-len) (- endd begg))
889 c-old-EOM)
890 (point)))
891 (cons new-beg new-end)))
893 (defun c-neutralize-CPP-line (beg end)
894 ;; BEG and END bound a region, typically a preprocessor line. Put a
895 ;; "punctuation" syntax-table property on syntactically obtrusive
896 ;; characters, ones which would interact syntactically with stuff outside
897 ;; this region.
899 ;; These are unmatched string delimiters, or unmatched
900 ;; parens/brackets/braces. An unclosed comment is regarded as valid, NOT
901 ;; obtrusive.
902 (save-excursion
903 (let (s)
904 (while
905 (progn
906 (setq s (parse-partial-sexp beg end -1))
907 (cond
908 ((< (nth 0 s) 0) ; found an unmated ),},]
909 (c-put-char-property (1- (point)) 'syntax-table '(1))
911 ((nth 3 s) ; In a string
912 (c-put-char-property (nth 8 s) 'syntax-table '(1))
914 ((> (nth 0 s) 0) ; In a (,{,[
915 (c-put-char-property (nth 1 s) 'syntax-table '(1))
917 (t nil)))))))
919 (defun c-neutralize-syntax-in-and-mark-CPP (begg endd old-len)
920 ;; (i) Extend the font lock region to cover all changed preprocessor
921 ;; regions; it does this by setting the variables `c-new-BEG' and
922 ;; `c-new-END' to the new boundaries.
924 ;; (ii) "Neutralize" every preprocessor line wholly or partially in the
925 ;; extended changed region. "Restore" lines which were CPP lines before the
926 ;; change and are no longer so; these can be located from the Buffer local
927 ;; variables `c-old-BOM' and `c-old-EOM'.
929 ;; (iii) Mark every CPP construct by placing a `category' property value
930 ;; `c-cpp-delimiter' at its start and end. The marked characters are the
931 ;; opening # and usually the terminating EOL, but sometimes the character
932 ;; before a comment/string delimiter.
934 ;; That is, set syntax-table properties on characters that would otherwise
935 ;; interact syntactically with those outside the CPP line(s).
937 ;; This function is called from an after-change function, BEGG ENDD and
938 ;; OLD-LEN being the standard parameters. It prepares the buffer for font
939 ;; locking, hence must get called before `font-lock-after-change-function'.
941 ;; Point is undefined both before and after this function call, the buffer
942 ;; has been widened, and match-data saved. The return value is ignored.
944 ;; This function is in the C/C++/ObjC value of `c-before-font-lock-functions'.
946 ;; Note: SPEED _MATTERS_ IN THIS FUNCTION!!!
948 ;; This function might make hidden buffer changes.
949 (c-save-buffer-state (new-bounds)
950 ;; First determine the region, (c-new-BEG c-new-END), which will get font
951 ;; locked. It might need "neutralizing". This region may not start
952 ;; inside a string, comment, or macro.
953 (setq new-bounds (c-extend-font-lock-region-for-macros
954 c-new-BEG c-new-END old-len))
955 (setq c-new-BEG (max (car new-bounds) (c-determine-limit 500 begg))
956 c-new-END (min (cdr new-bounds) (c-determine-+ve-limit 500 endd)))
957 ;; Clear all old relevant properties.
958 (c-clear-char-property-with-value c-new-BEG c-new-END 'syntax-table '(1))
960 ;; CPP "comment" markers:
961 (if (eval-when-compile (memq 'category-properties c-emacs-features));Emacs.
962 (c-clear-char-property-with-value
963 c-new-BEG c-new-END 'category 'c-cpp-delimiter))
964 ;; FIXME!!! What about the "<" and ">" category properties? 2009-11-16
966 ;; Add needed properties to each CPP construct in the region.
967 (goto-char c-new-BEG)
968 (skip-chars-backward " \t")
969 (let ((pps-position (point)) pps-state mbeg)
970 (while (and (< (point) c-new-END)
971 (search-forward-regexp c-anchored-cpp-prefix c-new-END t))
972 ;; If we've found a "#" inside a macro/string/comment, ignore it.
973 (unless
974 (or (save-excursion
975 (goto-char (match-beginning 0))
976 (let ((here (point)))
977 (and (save-match-data (c-beginning-of-macro))
978 (< (point) here))))
979 (progn
980 (setq pps-state
981 (parse-partial-sexp pps-position (point) nil nil pps-state)
982 pps-position (point))
983 (or (nth 3 pps-state) ; in a string?
984 (nth 4 pps-state)))) ; in a comment?
985 (goto-char (match-beginning 1))
986 (setq mbeg (point))
987 (if (> (c-syntactic-end-of-macro) mbeg)
988 (progn
989 (c-neutralize-CPP-line mbeg (point)) ; "punctuation" properties
990 (if (eval-when-compile
991 (memq 'category-properties c-emacs-features)) ;Emacs.
992 (c-set-cpp-delimiters mbeg (point)))) ; "comment" markers
993 (forward-line)) ; no infinite loop with, e.g., "#//"
994 )))))
996 (defun c-before-change (beg end)
997 ;; Function to be put on `before-change-functions'. Primarily, this calls
998 ;; the language dependent `c-get-state-before-change-functions'. It is
999 ;; otherwise used only to remove stale entries from the `c-found-types'
1000 ;; cache, and to record entries which a `c-after-change' function might
1001 ;; confirm as stale.
1003 ;; Note that this function must be FAST rather than accurate. Note
1004 ;; also that it only has any effect when font locking is enabled.
1005 ;; We exploit this by checking for font-lock-*-face instead of doing
1006 ;; rigorous syntactic analysis.
1008 ;; If either change boundary is wholly inside an identifier, delete
1009 ;; it/them from the cache. Don't worry about being inside a string
1010 ;; or a comment - "wrongly" removing a symbol from `c-found-types'
1011 ;; isn't critical.
1012 (unless c-just-done-before-change ; Guard against a spurious second
1013 ; invocation of before-change-functions.
1014 (setq c-just-done-before-change t)
1015 (setq c-maybe-stale-found-type nil)
1016 (save-restriction
1017 (save-match-data
1018 (widen)
1019 (save-excursion
1020 ;; Are we inserting/deleting stuff in the middle of an identifier?
1021 (c-unfind-enclosing-token beg)
1022 (c-unfind-enclosing-token end)
1023 ;; Are we coalescing two tokens together, e.g. "fo o" -> "foo"?
1024 (when (< beg end)
1025 (c-unfind-coalesced-tokens beg end))
1026 ;; Are we (potentially) disrupting the syntactic context which
1027 ;; makes a type a type? E.g. by inserting stuff after "foo" in
1028 ;; "foo bar;", or before "foo" in "typedef foo *bar;"?
1030 ;; We search for appropriate c-type properties "near" the change.
1031 ;; First, find an appropriate boundary for this property search.
1032 (let (lim
1033 type type-pos
1034 marked-id term-pos
1035 (end1
1036 (or (and (eq (get-text-property end 'face)
1037 'font-lock-comment-face)
1038 (previous-single-property-change end 'face))
1039 end)))
1040 (when (>= end1 beg) ; Don't hassle about changes entirely in comments.
1041 ;; Find a limit for the search for a `c-type' property
1042 (while
1043 (and (/= (skip-chars-backward "^;{}") 0)
1044 (> (point) (point-min))
1045 (memq (c-get-char-property (1- (point)) 'face)
1046 '(font-lock-comment-face font-lock-string-face))))
1047 (setq lim (max (point-min) (1- (point))))
1049 ;; Look for the latest `c-type' property before end1
1050 (when (and (> end1 (point-min))
1051 (setq type-pos
1052 (if (get-text-property (1- end1) 'c-type)
1053 end1
1054 (previous-single-property-change end1 'c-type
1055 nil lim))))
1056 (setq type (get-text-property (max (1- type-pos) lim) 'c-type))
1058 (when (memq type '(c-decl-id-start c-decl-type-start))
1059 ;; Get the identifier, if any, that the property is on.
1060 (goto-char (1- type-pos))
1061 (setq marked-id
1062 (when (looking-at "\\(\\sw\\|\\s_\\)")
1063 (c-beginning-of-current-token)
1064 (buffer-substring-no-properties (point) type-pos)))
1066 (goto-char end1)
1067 (skip-chars-forward "^;{}") ;FIXME!!! loop for comment, maybe
1068 (setq lim (point))
1069 (setq term-pos
1070 (or (c-next-single-property-change end 'c-type nil lim)
1071 lim))
1072 (setq c-maybe-stale-found-type
1073 (list type marked-id
1074 type-pos term-pos
1075 (buffer-substring-no-properties type-pos
1076 term-pos)
1077 (buffer-substring-no-properties beg end)))))))
1079 (if c-get-state-before-change-functions
1080 (let (open-paren-in-column-0-is-defun-start)
1081 (mapc (lambda (fn)
1082 (funcall fn beg end))
1083 c-get-state-before-change-functions)))
1085 ;; The following must be done here rather than in `c-after-change' because
1086 ;; newly inserted parens would foul up the invalidation algorithm.
1087 (c-invalidate-state-cache beg)))
1089 (defvar c-in-after-change-fontification nil)
1090 (make-variable-buffer-local 'c-in-after-change-fontification)
1091 ;; A flag to prevent region expanding stuff being done twice for after-change
1092 ;; fontification.
1094 (defun c-after-change (beg end old-len)
1095 ;; Function put on `after-change-functions' to adjust various caches
1096 ;; etc. Prefer speed to finesse here, since there will be an order
1097 ;; of magnitude more calls to this function than any of the
1098 ;; functions that use the caches.
1100 ;; Note that care must be taken so that this is called before any
1101 ;; font-lock callbacks since we might get calls to functions using
1102 ;; these caches from inside them, and we must thus be sure that this
1103 ;; has already been executed.
1105 ;; This calls the language variable c-before-font-lock-functions, if non nil.
1106 ;; This typically sets `syntax-table' properties.
1108 (setq c-just-done-before-change nil)
1109 (c-save-buffer-state (case-fold-search open-paren-in-column-0-is-defun-start)
1110 ;; When `combine-after-change-calls' is used we might get calls
1111 ;; with regions outside the current narrowing. This has been
1112 ;; observed in Emacs 20.7.
1113 (save-restriction
1114 (save-match-data ; c-recognize-<>-arglists changes match-data
1115 (widen)
1117 (when (> end (point-max))
1118 ;; Some emacsen might return positions past the end. This has been
1119 ;; observed in Emacs 20.7 when rereading a buffer changed on disk
1120 ;; (haven't been able to minimize it, but Emacs 21.3 appears to
1121 ;; work).
1122 (setq end (point-max))
1123 (when (> beg end)
1124 (setq beg end)))
1126 ;; C-y is capable of spuriously converting category properties
1127 ;; c-</>-as-paren-syntax and c-cpp-delimiter into hard syntax-table
1128 ;; properties. Remove these when it happens.
1129 (when (eval-when-compile (memq 'category-properties c-emacs-features))
1130 (c-clear-char-property-with-value beg end 'syntax-table
1131 c-<-as-paren-syntax)
1132 (c-clear-char-property-with-value beg end 'syntax-table
1133 c->-as-paren-syntax)
1134 (c-clear-char-property-with-value beg end 'syntax-table nil))
1136 (c-trim-found-types beg end old-len) ; maybe we don't need all of these.
1137 (c-invalidate-sws-region-after beg end)
1138 ;; (c-invalidate-state-cache beg) ; moved to `c-before-change'.
1139 (c-invalidate-find-decl-cache beg)
1141 (when c-recognize-<>-arglists
1142 (c-after-change-check-<>-operators beg end))
1144 ;; (c-new-BEG c-new-END) will be the region to fontify. It may become
1145 ;; larger than (beg end).
1146 (setq c-new-BEG beg
1147 c-new-END end)
1148 (setq c-in-after-change-fontification t)
1149 (save-excursion
1150 (mapc (lambda (fn)
1151 (funcall fn beg end old-len))
1152 c-before-font-lock-functions))))))
1154 (defun c-fl-decl-start (pos)
1155 ;; If the beginning of the line containing POS is in the middle of a "local"
1156 ;; declaration (i.e. one which does not start outside of braces enclosing
1157 ;; POS, such as a struct), return the beginning of that declaration.
1158 ;; Otherwise return nil. Note that declarations, in this sense, can be
1159 ;; nested.
1161 ;; This function is called indirectly from font locking stuff - either from
1162 ;; c-after-change (to prepare for after-change font-locking) or from font
1163 ;; lock context (etc.) fontification.
1164 (let ((lit-limits (c-literal-limits))
1165 (new-pos pos)
1166 bod-lim bo-decl)
1167 (goto-char (c-point 'bol new-pos))
1168 (when lit-limits ; Comment or string.
1169 (goto-char (car lit-limits)))
1170 (setq bod-lim (c-determine-limit 500))
1172 (while
1173 ;; Go to a less nested declaration each time round this loop.
1174 (and
1175 (eq (car (c-beginning-of-decl-1 bod-lim)) 'same)
1176 (> (point) bod-lim)
1177 (progn (setq bo-decl (point))
1178 ;; Are we looking at a keyword such as "template" or
1179 ;; "typedef" which can decorate a type, or the type itself?
1180 (when (or (looking-at c-prefix-spec-kwds-re)
1181 (c-forward-type t))
1182 ;; We've found another candidate position.
1183 (setq new-pos (min new-pos bo-decl))
1184 (goto-char bo-decl))
1186 ;; Try and go out a level to search again.
1187 (progn
1188 (c-backward-syntactic-ws bod-lim)
1189 (and (> (point) bod-lim)
1190 (or (memq (char-before) '(?\( ?\[))
1191 (and (eq (char-before) ?\<)
1192 (eq (c-get-char-property
1193 (1- (point)) 'syntax-table)
1194 c-<-as-paren-syntax)))))
1195 (not (bobp)))
1196 (backward-char)) ; back over (, [, <.
1197 (and (/= new-pos pos) new-pos)))
1199 (defun c-change-expand-fl-region (beg end old-len)
1200 ;; Expand the region (c-new-BEG c-new-END) to an after-change font-lock
1201 ;; region. This will usually be the smallest sequence of whole lines
1202 ;; containing `c-new-BEG' and `c-new-END', but if `c-new-BEG' is in a
1203 ;; "local" declaration (see `c-fl-decl-start') the beginning of this is used
1204 ;; as the lower bound.
1206 ;; This is called from an after-change-function, but the parameters BEG END
1207 ;; and OLD-LEN are not used.
1208 (if font-lock-mode
1209 (setq c-new-BEG
1210 (or (c-fl-decl-start c-new-BEG) (c-point 'bol c-new-BEG))
1211 c-new-END (c-point 'bonl c-new-END))))
1213 (defun c-context-expand-fl-region (beg end)
1214 ;; Return a cons (NEW-BEG . NEW-END), where NEW-BEG is the beginning of a
1215 ;; "local" declaration containing BEG (see `c-fl-decl-start') or BOL BEG is
1216 ;; in. NEW-END is beginning of the line after the one END is in.
1217 (cons (or (c-fl-decl-start beg) (c-point 'bol beg))
1218 (c-point 'bonl end)))
1220 (defun c-before-context-fl-expand-region (beg end)
1221 ;; Expand the region (BEG END) as specified by
1222 ;; `c-before-context-fontification-functions'. Return a cons of the bounds
1223 ;; of the new region.
1224 (save-restriction
1225 (widen)
1226 (save-excursion
1227 (let ((new-beg beg) (new-end end) new-region)
1228 (mapc (lambda (fn)
1229 (setq new-region (funcall fn new-beg new-end))
1230 (setq new-beg (car new-region) new-end (cdr new-region)))
1231 c-before-context-fontification-functions)
1232 new-region))))
1234 (defun c-font-lock-fontify-region (beg end &optional verbose)
1235 ;; Effectively advice around `font-lock-fontify-region' which extends the
1236 ;; region (BEG END), for example, to avoid context fontification chopping
1237 ;; off the start of the context. Do not extend the region if it's already
1238 ;; been done (i.e. from an after-change fontification. An example (C++)
1239 ;; where the chopping off used to happen is this:
1241 ;; template <typename T>
1244 ;; void myfunc(T* p) {}
1246 ;; Type a space in the first blank line, and the fontification of the next
1247 ;; line was fouled up by context fontification.
1248 (let (new-beg new-end new-region case-fold-search
1249 open-paren-in-column-0-is-defun-start)
1250 (if (and c-in-after-change-fontification
1251 (< beg c-new-END) (> end c-new-BEG))
1252 ;; Region and the latest after-change fontification region overlap.
1253 ;; Determine the upper and lower bounds of our adjusted region
1254 ;; separately.
1255 (progn
1256 (if (<= beg c-new-BEG)
1257 (setq c-in-after-change-fontification nil))
1258 (setq new-beg
1259 (if (and (>= beg (c-point 'bol c-new-BEG))
1260 (<= beg c-new-BEG))
1261 ;; Either jit-lock has accepted `c-new-BEG', or has
1262 ;; (probably) extended the change region spuriously to
1263 ;; BOL, which position likely has a syntactically
1264 ;; different position. To ensure correct fontification,
1265 ;; we start at `c-new-BEG', assuming any characters to the
1266 ;; left of `c-new-BEG' on the line do not require
1267 ;; fontification.
1268 c-new-BEG
1269 (setq new-region (c-before-context-fl-expand-region beg end)
1270 new-end (cdr new-region))
1271 (car new-region)))
1272 (setq new-end
1273 (if (and (>= end (c-point 'bol c-new-END))
1274 (<= end c-new-END))
1275 c-new-END
1276 (or new-end
1277 (cdr (c-before-context-fl-expand-region beg end))))))
1278 ;; Context (etc.) fontification.
1279 (setq new-region (c-before-context-fl-expand-region beg end)
1280 new-beg (car new-region) new-end (cdr new-region)))
1281 (funcall (default-value 'font-lock-fontify-region-function)
1282 new-beg new-end verbose)))
1284 (defun c-after-font-lock-init ()
1285 ;; Put on `font-lock-mode-hook'. This function ensures our after-change
1286 ;; function will get executed before the font-lock one.
1287 (remove-hook 'after-change-functions 'c-after-change t)
1288 (add-hook 'after-change-functions 'c-after-change nil t))
1290 (defun c-font-lock-init ()
1291 "Set up the font-lock variables for using the font-lock support in CC Mode.
1292 This does not load the font-lock package. Use after
1293 `c-basic-common-init' and after cc-fonts has been loaded.
1294 This function is called from `c-common-init', once per mode initialization."
1296 (set (make-local-variable 'font-lock-defaults)
1297 `(,(if (c-major-mode-is 'awk-mode)
1298 ;; awk-mode currently has only one font lock level.
1299 'awk-font-lock-keywords
1300 (mapcar 'c-mode-symbol
1301 '("font-lock-keywords" "font-lock-keywords-1"
1302 "font-lock-keywords-2" "font-lock-keywords-3")))
1303 nil nil
1304 ,c-identifier-syntax-modifications
1305 c-beginning-of-syntax
1306 (font-lock-mark-block-function
1307 . c-mark-function)))
1309 ;; Prevent `font-lock-default-fontify-region' extending the region it will
1310 ;; fontify to whole lines by removing `font-lock-extend-region-whole-lines'
1311 ;; (and, coincidentally, `font-lock-extend-region-multiline' (which we do
1312 ;; not need)) from `font-lock-extend-region-functions'. (Emacs only). This
1313 ;; fixes Emacs bug #19669.
1314 (when (boundp 'font-lock-extend-region-functions)
1315 (setq font-lock-extend-region-functions nil))
1317 (make-local-variable 'font-lock-fontify-region-function)
1318 (setq font-lock-fontify-region-function 'c-font-lock-fontify-region)
1320 (if (featurep 'xemacs)
1321 (make-local-hook 'font-lock-mode-hook))
1322 (add-hook 'font-lock-mode-hook 'c-after-font-lock-init nil t))
1324 ;; Emacs 22 and later.
1325 (defun c-extend-after-change-region (_beg _end _old-len)
1326 "Extend the region to be fontified, if necessary."
1327 ;; Note: the parameter OLD-LEN is ignored here. This somewhat indirect
1328 ;; implementation exists because it is minimally different from the
1329 ;; stand-alone CC Mode which, lacking
1330 ;; font-lock-extend-after-change-region-function, is forced to use advice
1331 ;; instead.
1333 ;; Of the seven CC Mode languages, currently (2009-05) only C, C++, Objc
1334 ;; (the languages with #define) and AWK Mode make non-null use of this
1335 ;; function.
1336 (when (eq font-lock-support-mode 'jit-lock-mode)
1337 (save-restriction
1338 (widen)
1339 (if (< c-new-BEG beg)
1340 (put-text-property c-new-BEG beg 'fontified nil))
1341 (if (> c-new-END end)
1342 (put-text-property end c-new-END 'fontified nil))))
1343 (cons c-new-BEG c-new-END))
1345 ;; Emacs < 22 and XEmacs
1346 (defmacro c-advise-fl-for-region (function)
1347 `(defadvice ,function (before get-awk-region activate)
1348 ;; Make sure that any string/regexp is completely font-locked.
1349 (when c-buffer-is-cc-mode
1350 (save-excursion
1351 (ad-set-arg 1 c-new-END) ; end
1352 (ad-set-arg 0 c-new-BEG))))) ; beg
1354 (unless (boundp 'font-lock-extend-after-change-region-function)
1355 (c-advise-fl-for-region font-lock-after-change-function)
1356 (c-advise-fl-for-region jit-lock-after-change)
1357 (c-advise-fl-for-region lazy-lock-defer-rest-after-change)
1358 (c-advise-fl-for-region lazy-lock-defer-line-after-change))
1360 ;; Connect up to `electric-indent-mode' (Emacs 24.4 and later).
1361 (defun c-electric-indent-mode-hook ()
1362 ;; Emacs has en/disabled `electric-indent-mode'. Propagate this through to
1363 ;; each CC Mode buffer.
1364 (mapc (lambda (buf)
1365 (with-current-buffer buf
1366 (when c-buffer-is-cc-mode
1367 ;; Don't use `c-toggle-electric-state' here due to recursion.
1368 (setq c-electric-flag electric-indent-mode)
1369 (c-update-modeline))))
1370 (buffer-list)))
1372 (defun c-electric-indent-local-mode-hook ()
1373 ;; Emacs has en/disabled `electric-indent-local-mode' for this buffer.
1374 ;; Propagate this through to this buffer's value of `c-electric-flag'
1375 (when c-buffer-is-cc-mode
1376 (setq c-electric-flag electric-indent-mode)
1377 (c-update-modeline)))
1380 ;; Support for C
1382 (defvar c-mode-syntax-table
1383 (funcall (c-lang-const c-make-mode-syntax-table c))
1384 "Syntax table used in c-mode buffers.")
1386 (c-define-abbrev-table 'c-mode-abbrev-table
1387 '(("else" "else" c-electric-continued-statement 0)
1388 ("while" "while" c-electric-continued-statement 0))
1389 "Abbreviation table used in c-mode buffers.")
1391 (defvar c-mode-map
1392 (let ((map (c-make-inherited-keymap)))
1393 ;; Add bindings which are only useful for C.
1394 (define-key map "\C-c\C-e" 'c-macro-expand)
1395 map)
1396 "Keymap used in c-mode buffers.")
1399 (easy-menu-define c-c-menu c-mode-map "C Mode Commands"
1400 (cons "C" (c-lang-const c-mode-menu c)))
1402 ;; In XEmacs >= 21.5 modes should add their own entries to
1403 ;; `auto-mode-alist'. The comment form of autoload is used to avoid
1404 ;; doing this on load. That since `add-to-list' prepends the value
1405 ;; which could cause it to clobber user settings. Later emacsen have
1406 ;; an append option, but it's not safe to use.
1408 ;; The extension ".C" is associated with C++ while the lowercase
1409 ;; variant goes with C. On case insensitive file systems, this means
1410 ;; that ".c" files also might open C++ mode if the C++ entry comes
1411 ;; first on `auto-mode-alist'. Thus we try to ensure that ".C" comes
1412 ;; after ".c", and since `add-to-list' adds the entry first we have to
1413 ;; add the ".C" entry first.
1414 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(cc\\|hh\\)\\'" . c++-mode))
1415 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.[ch]\\(pp\\|xx\\|\\+\\+\\)\\'" . c++-mode))
1416 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(CC?\\|HH?\\)\\'" . c++-mode))
1418 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.[ch]\\'" . c-mode))
1420 ;; NB: The following two associate yacc and lex files to C Mode, which
1421 ;; is not really suitable for those formats. Anyway, afaik there's
1422 ;; currently no better mode for them, and besides this is legacy.
1423 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.y\\(acc\\)?\\'" . c-mode))
1424 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.lex\\'" . c-mode))
1426 ;; Preprocessed files generated by C and C++ compilers.
1427 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.i\\'" . c-mode))
1428 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.ii\\'" . c++-mode))
1430 (unless (fboundp 'prog-mode) (defalias 'prog-mode 'fundamental-mode))
1432 ;;;###autoload
1433 (define-derived-mode c-mode prog-mode "C"
1434 "Major mode for editing K&R and ANSI C code.
1435 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1436 c-mode buffer. This automatically sets up a mail buffer with version
1437 information already added. You just need to add a description of the
1438 problem, including a reproducible test case, and send the message.
1440 To see what version of CC Mode you are running, enter `\\[c-version]'.
1442 The hook `c-mode-common-hook' is run with no args at mode
1443 initialization, then `c-mode-hook'.
1445 Key bindings:
1446 \\{c-mode-map}"
1447 (c-initialize-cc-mode t)
1448 (set-syntax-table c-mode-syntax-table)
1449 (setq local-abbrev-table c-mode-abbrev-table
1450 abbrev-mode t)
1451 (use-local-map c-mode-map)
1452 (c-init-language-vars-for 'c-mode)
1453 (c-make-macro-with-semi-re) ; matches macro names whose expansion ends with ;
1454 (c-common-init 'c-mode)
1455 (easy-menu-add c-c-menu)
1456 (cc-imenu-init cc-imenu-c-generic-expression)
1457 (c-run-mode-hooks 'c-mode-common-hook 'c-mode-hook)
1458 (c-update-modeline))
1461 ;; Support for C++
1463 (defvar c++-mode-syntax-table
1464 (funcall (c-lang-const c-make-mode-syntax-table c++))
1465 "Syntax table used in c++-mode buffers.")
1467 (c-define-abbrev-table 'c++-mode-abbrev-table
1468 '(("else" "else" c-electric-continued-statement 0)
1469 ("while" "while" c-electric-continued-statement 0)
1470 ("catch" "catch" c-electric-continued-statement 0))
1471 "Abbreviation table used in c++-mode buffers.")
1473 (defvar c++-mode-map
1474 (let ((map (c-make-inherited-keymap)))
1475 ;; Add bindings which are only useful for C++.
1476 (define-key map "\C-c\C-e" 'c-macro-expand)
1477 (define-key map "\C-c:" 'c-scope-operator)
1478 (define-key map "<" 'c-electric-lt-gt)
1479 (define-key map ">" 'c-electric-lt-gt)
1480 map)
1481 "Keymap used in c++-mode buffers.")
1483 (easy-menu-define c-c++-menu c++-mode-map "C++ Mode Commands"
1484 (cons "C++" (c-lang-const c-mode-menu c++)))
1486 ;;;###autoload
1487 (define-derived-mode c++-mode prog-mode "C++"
1488 "Major mode for editing C++ code.
1489 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1490 c++-mode buffer. This automatically sets up a mail buffer with
1491 version information already added. You just need to add a description
1492 of the problem, including a reproducible test case, and send the
1493 message.
1495 To see what version of CC Mode you are running, enter `\\[c-version]'.
1497 The hook `c-mode-common-hook' is run with no args at mode
1498 initialization, then `c++-mode-hook'.
1500 Key bindings:
1501 \\{c++-mode-map}"
1502 (c-initialize-cc-mode t)
1503 (set-syntax-table c++-mode-syntax-table)
1504 (setq local-abbrev-table c++-mode-abbrev-table
1505 abbrev-mode t)
1506 (use-local-map c++-mode-map)
1507 (c-init-language-vars-for 'c++-mode)
1508 (c-make-macro-with-semi-re) ; matches macro names whose expansion ends with ;
1509 (c-common-init 'c++-mode)
1510 (easy-menu-add c-c++-menu)
1511 (cc-imenu-init cc-imenu-c++-generic-expression)
1512 (c-run-mode-hooks 'c-mode-common-hook 'c++-mode-hook)
1513 (c-update-modeline))
1516 ;; Support for Objective-C
1518 (defvar objc-mode-syntax-table
1519 (funcall (c-lang-const c-make-mode-syntax-table objc))
1520 "Syntax table used in objc-mode buffers.")
1522 (c-define-abbrev-table 'objc-mode-abbrev-table
1523 '(("else" "else" c-electric-continued-statement 0)
1524 ("while" "while" c-electric-continued-statement 0))
1525 "Abbreviation table used in objc-mode buffers.")
1527 (defvar objc-mode-map
1528 (let ((map (c-make-inherited-keymap)))
1529 ;; Add bindings which are only useful for Objective-C.
1530 (define-key map "\C-c\C-e" 'c-macro-expand)
1531 map)
1532 "Keymap used in objc-mode buffers.")
1534 (easy-menu-define c-objc-menu objc-mode-map "ObjC Mode Commands"
1535 (cons "ObjC" (c-lang-const c-mode-menu objc)))
1537 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.m\\'" . objc-mode))
1539 ;;;###autoload
1540 (define-derived-mode objc-mode prog-mode "ObjC"
1541 "Major mode for editing Objective C code.
1542 To submit a problem report, enter `\\[c-submit-bug-report]' from an
1543 objc-mode buffer. This automatically sets up a mail buffer with
1544 version information already added. You just need to add a description
1545 of the problem, including a reproducible test case, and send the
1546 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 `objc-mode-hook'.
1553 Key bindings:
1554 \\{objc-mode-map}"
1555 (c-initialize-cc-mode t)
1556 (set-syntax-table objc-mode-syntax-table)
1557 (setq local-abbrev-table objc-mode-abbrev-table
1558 abbrev-mode t)
1559 (use-local-map objc-mode-map)
1560 (c-init-language-vars-for 'objc-mode)
1561 (c-make-macro-with-semi-re) ; matches macro names whose expansion ends with ;
1562 (c-common-init 'objc-mode)
1563 (easy-menu-add c-objc-menu)
1564 (cc-imenu-init nil 'cc-imenu-objc-function)
1565 (c-run-mode-hooks 'c-mode-common-hook 'objc-mode-hook)
1566 (c-update-modeline))
1569 ;; Support for Java
1571 (defvar java-mode-syntax-table
1572 (funcall (c-lang-const c-make-mode-syntax-table java))
1573 "Syntax table used in java-mode buffers.")
1575 (c-define-abbrev-table 'java-mode-abbrev-table
1576 '(("else" "else" c-electric-continued-statement 0)
1577 ("while" "while" c-electric-continued-statement 0)
1578 ("catch" "catch" c-electric-continued-statement 0)
1579 ("finally" "finally" c-electric-continued-statement 0))
1580 "Abbreviation table used in java-mode buffers.")
1582 (defvar java-mode-map
1583 (let ((map (c-make-inherited-keymap)))
1584 ;; Add bindings which are only useful for Java.
1585 map)
1586 "Keymap used in java-mode buffers.")
1588 ;; Regexp trying to describe the beginning of a Java top-level
1589 ;; definition. This is not used by CC Mode, nor is it maintained
1590 ;; since it's practically impossible to write a regexp that reliably
1591 ;; matches such a construct. Other tools are necessary.
1592 (defconst c-Java-defun-prompt-regexp
1593 "^[ \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-*")
1595 (easy-menu-define c-java-menu java-mode-map "Java Mode Commands"
1596 (cons "Java" (c-lang-const c-mode-menu java)))
1598 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.java\\'" . java-mode))
1600 ;;;###autoload
1601 (define-derived-mode java-mode prog-mode "Java"
1602 "Major mode for editing Java code.
1603 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1604 java-mode buffer. This automatically sets up a mail buffer with
1605 version information already added. You just need to add a description
1606 of the problem, including a reproducible test case, and send the
1607 message.
1609 To see what version of CC Mode you are running, enter `\\[c-version]'.
1611 The hook `c-mode-common-hook' is run with no args at mode
1612 initialization, then `java-mode-hook'.
1614 Key bindings:
1615 \\{java-mode-map}"
1616 (c-initialize-cc-mode t)
1617 (set-syntax-table java-mode-syntax-table)
1618 (setq local-abbrev-table java-mode-abbrev-table
1619 abbrev-mode t)
1620 (use-local-map java-mode-map)
1621 (c-init-language-vars-for 'java-mode)
1622 (c-common-init 'java-mode)
1623 (easy-menu-add c-java-menu)
1624 (cc-imenu-init cc-imenu-java-generic-expression)
1625 (c-run-mode-hooks 'c-mode-common-hook 'java-mode-hook)
1626 (c-update-modeline))
1629 ;; Support for CORBA's IDL language
1631 (defvar idl-mode-syntax-table
1632 (funcall (c-lang-const c-make-mode-syntax-table idl))
1633 "Syntax table used in idl-mode buffers.")
1635 (c-define-abbrev-table 'idl-mode-abbrev-table nil
1636 "Abbreviation table used in idl-mode buffers.")
1638 (defvar idl-mode-map
1639 (let ((map (c-make-inherited-keymap)))
1640 ;; Add bindings which are only useful for IDL.
1641 map)
1642 "Keymap used in idl-mode buffers.")
1644 (easy-menu-define c-idl-menu idl-mode-map "IDL Mode Commands"
1645 (cons "IDL" (c-lang-const c-mode-menu idl)))
1647 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.idl\\'" . idl-mode))
1649 ;;;###autoload
1650 (define-derived-mode idl-mode prog-mode "IDL"
1651 "Major mode for editing CORBA's IDL, PSDL and CIDL code.
1652 To submit a problem report, enter `\\[c-submit-bug-report]' from an
1653 idl-mode buffer. This automatically sets up a mail buffer with
1654 version information already added. You just need to add a description
1655 of the problem, including a reproducible test case, and send the
1656 message.
1658 To see what version of CC Mode you are running, enter `\\[c-version]'.
1660 The hook `c-mode-common-hook' is run with no args at mode
1661 initialization, then `idl-mode-hook'.
1663 Key bindings:
1664 \\{idl-mode-map}"
1665 (c-initialize-cc-mode t)
1666 (set-syntax-table idl-mode-syntax-table)
1667 (setq local-abbrev-table idl-mode-abbrev-table)
1668 (use-local-map idl-mode-map)
1669 (c-init-language-vars-for 'idl-mode)
1670 (c-common-init 'idl-mode)
1671 (easy-menu-add c-idl-menu)
1672 ;;(cc-imenu-init cc-imenu-idl-generic-expression) ;TODO
1673 (c-run-mode-hooks 'c-mode-common-hook 'idl-mode-hook)
1674 (c-update-modeline))
1677 ;; Support for Pike
1679 (defvar pike-mode-syntax-table
1680 (funcall (c-lang-const c-make-mode-syntax-table pike))
1681 "Syntax table used in pike-mode buffers.")
1683 (c-define-abbrev-table 'pike-mode-abbrev-table
1684 '(("else" "else" c-electric-continued-statement 0)
1685 ("while" "while" c-electric-continued-statement 0))
1686 "Abbreviation table used in pike-mode buffers.")
1688 (defvar pike-mode-map
1689 (let ((map (c-make-inherited-keymap)))
1690 ;; Additional bindings.
1691 (define-key map "\C-c\C-e" 'c-macro-expand)
1692 map)
1693 "Keymap used in pike-mode buffers.")
1695 (easy-menu-define c-pike-menu pike-mode-map "Pike Mode Commands"
1696 (cons "Pike" (c-lang-const c-mode-menu pike)))
1698 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(u?lpc\\|pike\\|pmod\\(\\.in\\)?\\)\\'" . pike-mode))
1699 ;;;###autoload (add-to-list 'interpreter-mode-alist '("pike" . pike-mode))
1701 ;;;###autoload
1702 (define-derived-mode pike-mode prog-mode "Pike"
1703 "Major mode for editing Pike code.
1704 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1705 pike-mode buffer. This automatically sets up a mail buffer with
1706 version information already added. You just need to add a description
1707 of the problem, including a reproducible test case, and send the
1708 message.
1710 To see what version of CC Mode you are running, enter `\\[c-version]'.
1712 The hook `c-mode-common-hook' is run with no args at mode
1713 initialization, then `pike-mode-hook'.
1715 Key bindings:
1716 \\{pike-mode-map}"
1717 (c-initialize-cc-mode t)
1718 (set-syntax-table pike-mode-syntax-table)
1719 (setq local-abbrev-table pike-mode-abbrev-table
1720 abbrev-mode t)
1721 (use-local-map pike-mode-map)
1722 (c-init-language-vars-for 'pike-mode)
1723 (c-common-init 'pike-mode)
1724 (easy-menu-add c-pike-menu)
1725 ;;(cc-imenu-init cc-imenu-pike-generic-expression) ;TODO
1726 (c-run-mode-hooks 'c-mode-common-hook 'pike-mode-hook)
1727 (c-update-modeline))
1730 ;; Support for AWK
1732 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.awk\\'" . awk-mode))
1733 ;;;###autoload (add-to-list 'interpreter-mode-alist '("awk" . awk-mode))
1734 ;;;###autoload (add-to-list 'interpreter-mode-alist '("mawk" . awk-mode))
1735 ;;;###autoload (add-to-list 'interpreter-mode-alist '("nawk" . awk-mode))
1736 ;;;###autoload (add-to-list 'interpreter-mode-alist '("gawk" . awk-mode))
1738 (c-define-abbrev-table 'awk-mode-abbrev-table
1739 '(("else" "else" c-electric-continued-statement 0)
1740 ("while" "while" c-electric-continued-statement 0))
1741 "Abbreviation table used in awk-mode buffers.")
1743 (defvar awk-mode-map
1744 (let ((map (c-make-inherited-keymap)))
1745 ;; Add bindings which are only useful for awk.
1746 (define-key map "#" 'self-insert-command)
1747 (define-key map "/" 'self-insert-command)
1748 (define-key map "*" 'self-insert-command)
1749 (define-key map "\C-c\C-n" 'undefined) ; #if doesn't exist in awk.
1750 (define-key map "\C-c\C-p" 'undefined)
1751 (define-key map "\C-c\C-u" 'undefined)
1752 (define-key map "\M-a" 'c-beginning-of-statement) ; 2003/10/7
1753 (define-key map "\M-e" 'c-end-of-statement) ; 2003/10/7
1754 (define-key map "\C-\M-a" 'c-awk-beginning-of-defun)
1755 (define-key map "\C-\M-e" 'c-awk-end-of-defun)
1756 map)
1757 "Keymap used in awk-mode buffers.")
1759 (easy-menu-define c-awk-menu awk-mode-map "AWK Mode Commands"
1760 (cons "AWK" (c-lang-const c-mode-menu awk)))
1762 ;; (require 'cc-awk) brings these in.
1763 (defvar awk-mode-syntax-table)
1764 (declare-function c-awk-unstick-NL-prop "cc-awk" ())
1766 ;;;###autoload
1767 (define-derived-mode awk-mode prog-mode "AWK"
1768 "Major mode for editing AWK code.
1769 To submit a problem report, enter `\\[c-submit-bug-report]' from an
1770 awk-mode buffer. This automatically sets up a mail buffer with version
1771 information already added. You just need to add a description of the
1772 problem, including a reproducible test case, and send the message.
1774 To see what version of CC Mode you are running, enter `\\[c-version]'.
1776 The hook `c-mode-common-hook' is run with no args at mode
1777 initialization, then `awk-mode-hook'.
1779 Key bindings:
1780 \\{awk-mode-map}"
1781 ;; We need the next line to stop the macro defining
1782 ;; `awk-mode-syntax-table'. This would mask the real table which is
1783 ;; declared in cc-awk.el and hasn't yet been loaded.
1784 :syntax-table nil
1785 (require 'cc-awk) ; Added 2003/6/10.
1786 (c-initialize-cc-mode t)
1787 (set-syntax-table awk-mode-syntax-table)
1788 (setq local-abbrev-table awk-mode-abbrev-table
1789 abbrev-mode t)
1790 (use-local-map awk-mode-map)
1791 (c-init-language-vars-for 'awk-mode)
1792 (c-common-init 'awk-mode)
1793 (c-awk-unstick-NL-prop)
1795 (c-run-mode-hooks 'c-mode-common-hook 'awk-mode-hook)
1796 (c-update-modeline))
1799 ;; bug reporting
1801 (defconst c-mode-help-address
1802 "bug-cc-mode@gnu.org"
1803 "Address(es) for CC Mode bug reports.")
1805 (defun c-version ()
1806 "Echo the current version of CC Mode in the minibuffer."
1807 (interactive)
1808 (message "Using CC Mode version %s" c-version)
1809 (c-keep-region-active))
1811 (define-obsolete-variable-alias 'c-prepare-bug-report-hooks
1812 'c-prepare-bug-report-hook "24.3")
1813 (defvar c-prepare-bug-report-hook nil)
1815 ;; Dynamic variables used by reporter.
1816 (defvar reporter-prompt-for-summary-p)
1817 (defvar reporter-dont-compact-list)
1819 (defun c-submit-bug-report ()
1820 "Submit via mail a bug report on CC Mode."
1821 (interactive)
1822 (require 'reporter)
1823 ;; load in reporter
1824 (let ((reporter-prompt-for-summary-p t)
1825 (reporter-dont-compact-list '(c-offsets-alist))
1826 (style c-indentation-style)
1827 (c-features c-emacs-features))
1828 (and
1829 (if (y-or-n-p "Do you want to submit a report on CC Mode? ")
1830 t (message "") nil)
1831 (reporter-submit-bug-report
1832 c-mode-help-address
1833 (concat "CC Mode " c-version " (" mode-name ")")
1834 (let ((vars (append
1835 c-style-variables
1836 '(c-buffer-is-cc-mode
1837 c-tab-always-indent
1838 c-syntactic-indentation
1839 c-syntactic-indentation-in-macros
1840 c-ignore-auto-fill
1841 c-auto-align-backslashes
1842 c-backspace-function
1843 c-delete-function
1844 c-electric-pound-behavior
1845 c-default-style
1846 c-enable-xemacs-performance-kludge-p
1847 c-old-style-variable-behavior
1848 defun-prompt-regexp
1849 tab-width
1850 comment-column
1851 parse-sexp-ignore-comments
1852 parse-sexp-lookup-properties
1853 lookup-syntax-properties
1854 ;; A brain-damaged XEmacs only variable that, if
1855 ;; set to nil can cause all kinds of chaos.
1856 signal-error-on-buffer-boundary
1857 ;; Variables that affect line breaking and comments.
1858 auto-fill-mode
1859 auto-fill-function
1860 filladapt-mode
1861 comment-multi-line
1862 comment-start-skip
1863 fill-prefix
1864 fill-column
1865 paragraph-start
1866 adaptive-fill-mode
1867 adaptive-fill-regexp)
1868 nil)))
1869 (mapc (lambda (var) (unless (boundp var)
1870 (setq vars (delq var vars))))
1871 '(signal-error-on-buffer-boundary
1872 filladapt-mode
1873 defun-prompt-regexp
1874 font-lock-mode
1875 auto-fill-mode
1876 font-lock-maximum-decoration
1877 parse-sexp-lookup-properties
1878 lookup-syntax-properties))
1879 vars)
1880 (lambda ()
1881 (run-hooks 'c-prepare-bug-report-hook)
1882 (insert (format "Buffer Style: %s\nc-emacs-features: %s\n"
1883 style c-features)))))))
1886 (cc-provide 'cc-mode)
1888 ;;; Local Variables:
1889 ;;; indent-tabs-mode: t
1890 ;;; tab-width: 8
1891 ;;; End:
1892 ;;; cc-mode.el ends here