]> code.delx.au - gnu-emacs/blob - lisp/progmodes/cc-mode.el
Merge from origin/emacs-24
[gnu-emacs] / lisp / progmodes / cc-mode.el
1 ;;; cc-mode.el --- major mode for editing C and similar languages
2
3 ;; Copyright (C) 1985, 1987, 1992-2015 Free Software Foundation, Inc.
4
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
14
15 ;; This file is part of GNU Emacs.
16
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.
21
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.
26
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/>.
29
30 ;;; Commentary:
31
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.
35
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.
41
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.
48
49 ;; CC Mode's immediate ancestors were, c++-mode.el, cplus-md.el, and
50 ;; cplus-md1.el..
51
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!
59
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.
63
64 ;; You can get the latest version of CC Mode, including PostScript
65 ;; documentation and separate individual files from:
66 ;;
67 ;; http://cc-mode.sourceforge.net/
68 ;;
69 ;; You can join a moderated CC Mode announcement-only mailing list by
70 ;; visiting
71 ;;
72 ;; http://lists.sourceforge.net/mailman/listinfo/cc-mode-announce
73
74 ;;; Code:
75
76 ;; For Emacs < 22.2.
77 (eval-and-compile
78 (unless (fboundp 'declare-function) (defmacro declare-function (&rest _r))))
79
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)))
87
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)
97
98 ;; Silence the compiler.
99 (cc-bytecomp-defvar adaptive-fill-first-line-regexp) ; Emacs
100 (cc-bytecomp-defun run-mode-hooks) ; Emacs 21.1
101
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)
106
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)
110
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.
114 ; '
115 (require 'cc-fonts) ;)
116
117 \f
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:
121 ;;
122 ;; (require 'cc-mode)
123 ;;
124 ;; And in the major mode function:
125 ;;
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)
129 ;;
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'.
136 ;;
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.
139 ;;
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>.
143
144 (defun c-leave-cc-mode-mode ()
145 (setq c-buffer-is-cc-mode nil))
146
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))))
161
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."
170
171 (setq c-buffer-is-cc-mode t)
172
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))))
193
194 (unless new-style-init
195 (c-init-language-vars-for 'c-mode)))
196
197 \f
198 ;;; Common routines.
199
200 (defvar c-mode-base-map ()
201 "Keymap shared by all CC Mode related modes.")
202
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))
218
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)
237
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)))
251
252 (if c-mode-base-map
253 nil
254
255 (setq c-mode-base-map (make-sparse-keymap))
256
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))
281
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))
287
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)
291
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)
306
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.
317
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))
337
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))
352
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)
363
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)
378 )
379
380 ;; We don't require the outline package, but we configure it a bit anyway.
381 (cc-bytecomp-defvar outline-level)
382
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\".
387
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)))
391
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)))))
400
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.
408 ;;
409 ;; (2007-02-12): The macro `combine-after-change-calls' ISN'T used any
410 ;; more.
411
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))))
424
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))))))))
447
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:
451 ;;
452 ;; (c-decl-id-start "foo" 97 107 " (* ooka) " "o"), where
453 ;;
454 ;; o - `c-decl-id-start' is the c-type text property value at buffer
455 ;; pos 96.
456 ;;
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.
460 ;;
461 ;; o - " (* ooka) " is the (before change) buffer portion containing
462 ;; the suspect type (here "ooka").
463 ;;
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)
468
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.
476
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.
481
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'.
485
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."
489
490 (setq c-buffer-is-cc-mode mode)
491
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)
497
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)
504
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)
513
514 ;; For the benefit of adaptive file, which otherwise mis-fills.
515 (setq fill-paragraph-handle-comment nil)
516
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)
521
522 ;; Initialize the cache of brace pairs, and opening braces/brackets/parens.
523 (c-state-cache-init)
524
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.
531 ;;
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.
535
536 ;; Emacs.
537 (when (boundp 'parse-sexp-lookup-properties)
538 (set (make-local-variable 'parse-sexp-lookup-properties) t))
539
540 ;; Same as above for XEmacs.
541 (when (boundp 'lookup-syntax-properties)
542 (set (make-local-variable 'lookup-syntax-properties) t)))
543
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)))
553
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))
563
564 (c-clear-found-types)
565
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)
581
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))
585
586 ;; setup the comment indent variable in a Emacs version portable way
587 (set (make-local-variable 'comment-indent-function) 'c-comment-indent)
588
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))
597
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)
604
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.
617
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)))
625
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)
634
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.
641
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."
645
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))
650
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))
655
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 (mapc (lambda (fn)
663 (funcall fn (point-min) (point-max)))
664 c-get-state-before-change-functions)
665 (mapc (lambda (fn)
666 (funcall fn (point-min) (point-max)
667 (- (point-max) (point-min))))
668 c-before-font-lock-functions)))
669
670 (set (make-local-variable 'outline-regexp) "[^#\n\^M]")
671 (set (make-local-variable 'outline-level) 'c-outline-level)
672 (set (make-local-variable 'add-log-current-defun-function)
673 (lambda ()
674 (or (c-cpp-define-name) (c-defun-name))))
675 (let ((rfn (assq mode c-require-final-newline)))
676 (when rfn
677 (if (boundp 'mode-require-final-newline)
678 (and (cdr rfn)
679 (set (make-local-variable 'require-final-newline)
680 mode-require-final-newline))
681 (set (make-local-variable 'require-final-newline) (cdr rfn))))))
682
683 (defun c-count-cfss (lv-alist)
684 ;; LV-ALIST is an alist like `file-local-variables-alist'. Count how many
685 ;; elements with the key `c-file-style' there are in it.
686 (let ((elt-ptr lv-alist) elt (cownt 0))
687 (while elt-ptr
688 (setq elt (car elt-ptr)
689 elt-ptr (cdr elt-ptr))
690 (when (eq (car elt) 'c-file-style)
691 (setq cownt (1+ cownt))))
692 cownt))
693
694 (defun c-before-hack-hook ()
695 "Set the CC Mode style and \"offsets\" when in the buffer's local variables.
696 They are set only when, respectively, the pseudo variables
697 `c-file-style' and `c-file-offsets' are present in the list.
698
699 This function is called from the hook `before-hack-local-variables-hook'."
700 (when c-buffer-is-cc-mode
701 (let ((mode-cons (assq 'mode file-local-variables-alist))
702 (stile (cdr (assq 'c-file-style file-local-variables-alist)))
703 (offsets (cdr (assq 'c-file-offsets file-local-variables-alist))))
704 (when mode-cons
705 (hack-one-local-variable (car mode-cons) (cdr mode-cons))
706 (setq file-local-variables-alist
707 (delq mode-cons file-local-variables-alist)))
708 (when stile
709 (or (stringp stile) (error "c-file-style is not a string"))
710 (if (boundp 'dir-local-variables-alist)
711 ;; Determine whether `c-file-style' was set in the file's local
712 ;; variables or in a .dir-locals.el (a directory setting).
713 (let ((cfs-in-file-and-dir-count
714 (c-count-cfss file-local-variables-alist))
715 (cfs-in-dir-count (c-count-cfss dir-local-variables-alist)))
716 (c-set-style stile
717 (and (= cfs-in-file-and-dir-count cfs-in-dir-count)
718 'keep-defaults)))
719 (c-set-style stile)))
720 (when offsets
721 (mapc
722 (lambda (langentry)
723 (let ((langelem (car langentry))
724 (offset (cdr langentry)))
725 (c-set-offset langelem offset)))
726 offsets)))))
727
728 (defun c-remove-any-local-eval-or-mode-variables ()
729 ;; If the buffer specifies `mode' or `eval' in its File Local Variable list
730 ;; or on the first line, remove all occurrences. See
731 ;; `c-postprocess-file-styles' for justification. There is no need to save
732 ;; point here, or even bother too much about the buffer contents. However,
733 ;; DON'T mess up the kill-ring.
734 ;;
735 ;; Most of the code here is derived from Emacs 21.3's `hack-local-variables'
736 ;; in files.el.
737 (goto-char (point-max))
738 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
739 (let (lv-point (prefix "") (suffix ""))
740 (when (let ((case-fold-search t))
741 (search-forward "Local Variables:" nil t))
742 (setq lv-point (point))
743 ;; The prefix is what comes before "local variables:" in its line.
744 ;; The suffix is what comes after "local variables:" in its line.
745 (skip-chars-forward " \t")
746 (or (eolp)
747 (setq suffix (buffer-substring (point)
748 (progn (end-of-line) (point)))))
749 (goto-char (match-beginning 0))
750 (or (bolp)
751 (setq prefix
752 (buffer-substring (point)
753 (progn (beginning-of-line) (point)))))
754
755 (while (search-forward-regexp
756 (concat "^[ \t]*"
757 (regexp-quote prefix)
758 "\\(mode\\|eval\\):.*"
759 (regexp-quote suffix)
760 "$")
761 nil t)
762 (forward-line 0)
763 (delete-region (point) (progn (forward-line) (point)))))
764
765 ;; Delete the first line, if we've got one, in case it contains a mode spec.
766 (unless (and lv-point
767 (progn (goto-char lv-point)
768 (forward-line 0)
769 (bobp)))
770 (goto-char (point-min))
771 (unless (eobp)
772 (delete-region (point) (progn (forward-line) (point)))))))
773
774 (defun c-postprocess-file-styles ()
775 "Function that post processes relevant file local variables in CC Mode.
776 Currently, this function simply applies any style and offset settings
777 found in the file's Local Variable list. It first applies any style
778 setting found in `c-file-style', then it applies any offset settings
779 it finds in `c-file-offsets'.
780
781 Note that the style variables are always made local to the buffer."
782
783 ;; apply file styles and offsets
784 (when c-buffer-is-cc-mode
785 (if (or c-file-style c-file-offsets)
786 (c-make-styles-buffer-local t))
787 (when c-file-style
788 (or (stringp c-file-style)
789 (error "c-file-style is not a string"))
790 (c-set-style c-file-style))
791
792 (and c-file-offsets
793 (mapc
794 (lambda (langentry)
795 (let ((langelem (car langentry))
796 (offset (cdr langentry)))
797 (c-set-offset langelem offset)))
798 c-file-offsets))
799 ;; Problem: The file local variable block might have explicitly set a
800 ;; style variable. The `c-set-style' or `mapcar' call might have
801 ;; overwritten this. So we run `hack-local-variables' again to remedy
802 ;; this. There are no guarantees this will work properly, particularly as
803 ;; we have no control over what the other hook functions on
804 ;; `hack-local-variables-hook' would have done. We now (2006/2/1) remove
805 ;; any `eval' or `mode' expressions before we evaluate again (see below).
806 ;; ACM, 2005/11/2.
807 ;;
808 ;; Problem (bug reported by Gustav Broberg): if one of the variables is
809 ;; `mode', this will invoke c-mode (etc.) again, setting up the style etc.
810 ;; We prevent this by temporarily removing `mode' from the Local Variables
811 ;; section.
812 (if (or c-file-style c-file-offsets)
813 (let ((hack-local-variables-hook nil) (inhibit-read-only t))
814 (c-tentative-buffer-changes
815 (c-remove-any-local-eval-or-mode-variables)
816 (hack-local-variables))
817 nil))))
818
819 (if (boundp 'before-hack-local-variables-hook)
820 (add-hook 'before-hack-local-variables-hook 'c-before-hack-hook)
821 (add-hook 'hack-local-variables-hook 'c-postprocess-file-styles))
822
823 (defmacro c-run-mode-hooks (&rest hooks)
824 ;; Emacs 21.1 has introduced a system with delayed mode hooks that
825 ;; requires the use of the new function `run-mode-hooks'.
826 (if (cc-bytecomp-fboundp 'run-mode-hooks)
827 `(run-mode-hooks ,@hooks)
828 `(progn ,@(mapcar (lambda (hook) `(run-hooks ,hook)) hooks))))
829
830 \f
831 ;;; Change hooks, linking with Font Lock and electric-indent-mode.
832
833 ;; Buffer local variables recording Beginning/End-of-Macro position before a
834 ;; change, when a macro straddles, respectively, the BEG or END (or both) of
835 ;; the change region. Otherwise these have the values BEG/END.
836 (defvar c-old-BOM 0)
837 (make-variable-buffer-local 'c-old-BOM)
838 (defvar c-old-EOM 0)
839 (make-variable-buffer-local 'c-old-EOM)
840
841 (defun c-extend-region-for-CPP (beg end)
842 ;; Set c-old-BOM or c-old-EOM respectively to BEG, END, each extended to the
843 ;; beginning/end of any preprocessor construct they may be in.
844 ;;
845 ;; Point is undefined both before and after this function call; the buffer
846 ;; has already been widened, and match-data saved. The return value is
847 ;; meaningless.
848 ;;
849 ;; This function is in the C/C++/ObjC values of
850 ;; `c-get-state-before-change-functions' and is called exclusively as a
851 ;; before change function.
852 (goto-char beg)
853 (c-beginning-of-macro)
854 (setq c-old-BOM (point))
855
856 (goto-char end)
857 (when (c-beginning-of-macro)
858 (c-end-of-macro)
859 (or (eobp) (forward-char))) ; Over the terminating NL which may be marked
860 ; with a c-cpp-delimiter category property
861 (setq c-old-EOM (point)))
862
863 (defun c-extend-font-lock-region-for-macros (begg endd &optional old-len)
864 ;; Extend the region (BEGG ENDD) to cover all (possibly changed)
865 ;; preprocessor macros; return the cons (new-BEG . new-END). OLD-LEN should
866 ;; be either the old length parameter when called from an
867 ;; after-change-function, or nil otherwise. This defun uses the variables
868 ;; c-old-BOM, c-new-BOM.
869 ;;
870 ;; Point is undefined on both entry and exit to this function. The buffer
871 ;; will have been widened on entry.
872 (let (limits new-beg new-end)
873 (goto-char c-old-BOM) ; already set to old start of macro or begg.
874 (setq new-beg
875 (min begg
876 (if (setq limits (c-state-literal-at (point)))
877 (cdr limits) ; go forward out of any string or comment.
878 (point))))
879
880 (goto-char endd)
881 (if (setq limits (c-state-literal-at (point)))
882 (goto-char (car limits))) ; go backward out of any string or comment.
883 (if (c-beginning-of-macro)
884 (c-end-of-macro))
885 (setq new-end (max endd
886 (if old-len
887 (+ (- c-old-EOM old-len) (- endd begg))
888 c-old-EOM)
889 (point)))
890 (cons new-beg new-end)))
891
892 (defun c-neutralize-CPP-line (beg end)
893 ;; BEG and END bound a region, typically a preprocessor line. Put a
894 ;; "punctuation" syntax-table property on syntactically obtrusive
895 ;; characters, ones which would interact syntactically with stuff outside
896 ;; this region.
897 ;;
898 ;; These are unmatched string delimiters, or unmatched
899 ;; parens/brackets/braces. An unclosed comment is regarded as valid, NOT
900 ;; obtrusive.
901 (save-excursion
902 (let (s)
903 (while
904 (progn
905 (setq s (parse-partial-sexp beg end -1))
906 (cond
907 ((< (nth 0 s) 0) ; found an unmated ),},]
908 (c-put-char-property (1- (point)) 'syntax-table '(1))
909 t)
910 ((nth 3 s) ; In a string
911 (c-put-char-property (nth 8 s) 'syntax-table '(1))
912 t)
913 ((> (nth 0 s) 0) ; In a (,{,[
914 (c-put-char-property (nth 1 s) 'syntax-table '(1))
915 t)
916 (t nil)))))))
917
918 (defun c-neutralize-syntax-in-and-mark-CPP (begg endd old-len)
919 ;; (i) Extend the font lock region to cover all changed preprocessor
920 ;; regions; it does this by setting the variables `c-new-BEG' and
921 ;; `c-new-END' to the new boundaries.
922 ;;
923 ;; (ii) "Neutralize" every preprocessor line wholly or partially in the
924 ;; extended changed region. "Restore" lines which were CPP lines before the
925 ;; change and are no longer so; these can be located from the Buffer local
926 ;; variables `c-old-BOM' and `c-old-EOM'.
927 ;;
928 ;; (iii) Mark every CPP construct by placing a `category' property value
929 ;; `c-cpp-delimiter' at its start and end. The marked characters are the
930 ;; opening # and usually the terminating EOL, but sometimes the character
931 ;; before a comment/string delimiter.
932 ;;
933 ;; That is, set syntax-table properties on characters that would otherwise
934 ;; interact syntactically with those outside the CPP line(s).
935 ;;
936 ;; This function is called from an after-change function, BEGG ENDD and
937 ;; OLD-LEN being the standard parameters. It prepares the buffer for font
938 ;; locking, hence must get called before `font-lock-after-change-function'.
939 ;;
940 ;; Point is undefined both before and after this function call, the buffer
941 ;; has been widened, and match-data saved. The return value is ignored.
942 ;;
943 ;; This function is in the C/C++/ObjC value of `c-before-font-lock-functions'.
944 ;;
945 ;; Note: SPEED _MATTERS_ IN THIS FUNCTION!!!
946 ;;
947 ;; This function might make hidden buffer changes.
948 (c-save-buffer-state (new-bounds)
949 ;; First determine the region, (c-new-BEG c-new-END), which will get font
950 ;; locked. It might need "neutralizing". This region may not start
951 ;; inside a string, comment, or macro.
952 (setq new-bounds (c-extend-font-lock-region-for-macros
953 c-new-BEG c-new-END old-len))
954 (setq c-new-BEG (max (car new-bounds) (c-determine-limit 500 begg))
955 c-new-END (min (cdr new-bounds) (c-determine-+ve-limit 500 endd)))
956 ;; Clear all old relevant properties.
957 (c-clear-char-property-with-value c-new-BEG c-new-END 'syntax-table '(1))
958
959 ;; CPP "comment" markers:
960 (if (eval-when-compile (memq 'category-properties c-emacs-features));Emacs.
961 (c-clear-char-property-with-value
962 c-new-BEG c-new-END 'category 'c-cpp-delimiter))
963 ;; FIXME!!! What about the "<" and ">" category properties? 2009-11-16
964
965 ;; Add needed properties to each CPP construct in the region.
966 (goto-char c-new-BEG)
967 (skip-chars-backward " \t")
968 (let ((pps-position (point)) pps-state mbeg)
969 (while (and (< (point) c-new-END)
970 (search-forward-regexp c-anchored-cpp-prefix c-new-END t))
971 ;; If we've found a "#" inside a macro/string/comment, ignore it.
972 (unless
973 (or (save-excursion
974 (goto-char (match-beginning 0))
975 (c-beginning-of-macro))
976 (progn
977 (setq pps-state
978 (parse-partial-sexp pps-position (point) nil nil pps-state)
979 pps-position (point))
980 (or (nth 3 pps-state) ; in a string?
981 (nth 4 pps-state)))) ; in a comment?
982 (goto-char (match-beginning 1))
983 (setq mbeg (point))
984 (if (> (c-syntactic-end-of-macro) mbeg)
985 (progn
986 (c-neutralize-CPP-line mbeg (point)) ; "punctuation" properties
987 (if (eval-when-compile
988 (memq 'category-properties c-emacs-features)) ;Emacs.
989 (c-set-cpp-delimiters mbeg (point)))) ; "comment" markers
990 (forward-line)) ; no infinite loop with, e.g., "#//"
991 )))))
992
993 (defun c-before-change (beg end)
994 ;; Function to be put on `before-change-functions'. Primarily, this calls
995 ;; the language dependent `c-get-state-before-change-functions'. It is
996 ;; otherwise used only to remove stale entries from the `c-found-types'
997 ;; cache, and to record entries which a `c-after-change' function might
998 ;; confirm as stale.
999 ;;
1000 ;; Note that this function must be FAST rather than accurate. Note
1001 ;; also that it only has any effect when font locking is enabled.
1002 ;; We exploit this by checking for font-lock-*-face instead of doing
1003 ;; rigorous syntactic analysis.
1004
1005 ;; If either change boundary is wholly inside an identifier, delete
1006 ;; it/them from the cache. Don't worry about being inside a string
1007 ;; or a comment - "wrongly" removing a symbol from `c-found-types'
1008 ;; isn't critical.
1009 (unless c-just-done-before-change ; Guard against a spurious second
1010 ; invocation of before-change-functions.
1011 (setq c-just-done-before-change t)
1012 (setq c-maybe-stale-found-type nil)
1013 (save-restriction
1014 (save-match-data
1015 (widen)
1016 (save-excursion
1017 ;; Are we inserting/deleting stuff in the middle of an identifier?
1018 (c-unfind-enclosing-token beg)
1019 (c-unfind-enclosing-token end)
1020 ;; Are we coalescing two tokens together, e.g. "fo o" -> "foo"?
1021 (when (< beg end)
1022 (c-unfind-coalesced-tokens beg end))
1023 ;; Are we (potentially) disrupting the syntactic context which
1024 ;; makes a type a type? E.g. by inserting stuff after "foo" in
1025 ;; "foo bar;", or before "foo" in "typedef foo *bar;"?
1026 ;;
1027 ;; We search for appropriate c-type properties "near" the change.
1028 ;; First, find an appropriate boundary for this property search.
1029 (let (lim
1030 type type-pos
1031 marked-id term-pos
1032 (end1
1033 (or (and (eq (get-text-property end 'face)
1034 'font-lock-comment-face)
1035 (previous-single-property-change end 'face))
1036 end)))
1037 (when (>= end1 beg) ; Don't hassle about changes entirely in comments.
1038 ;; Find a limit for the search for a `c-type' property
1039 (while
1040 (and (/= (skip-chars-backward "^;{}") 0)
1041 (> (point) (point-min))
1042 (memq (c-get-char-property (1- (point)) 'face)
1043 '(font-lock-comment-face font-lock-string-face))))
1044 (setq lim (max (point-min) (1- (point))))
1045
1046 ;; Look for the latest `c-type' property before end1
1047 (when (and (> end1 (point-min))
1048 (setq type-pos
1049 (if (get-text-property (1- end1) 'c-type)
1050 end1
1051 (previous-single-property-change end1 'c-type
1052 nil lim))))
1053 (setq type (get-text-property (max (1- type-pos) lim) 'c-type))
1054
1055 (when (memq type '(c-decl-id-start c-decl-type-start))
1056 ;; Get the identifier, if any, that the property is on.
1057 (goto-char (1- type-pos))
1058 (setq marked-id
1059 (when (looking-at "\\(\\sw\\|\\s_\\)")
1060 (c-beginning-of-current-token)
1061 (buffer-substring-no-properties (point) type-pos)))
1062
1063 (goto-char end1)
1064 (skip-chars-forward "^;{}") ;FIXME!!! loop for comment, maybe
1065 (setq lim (point))
1066 (setq term-pos
1067 (or (c-next-single-property-change end 'c-type nil lim)
1068 lim))
1069 (setq c-maybe-stale-found-type
1070 (list type marked-id
1071 type-pos term-pos
1072 (buffer-substring-no-properties type-pos
1073 term-pos)
1074 (buffer-substring-no-properties beg end)))))))
1075
1076 (if c-get-state-before-change-functions
1077 (let (open-paren-in-column-0-is-defun-start)
1078 (mapc (lambda (fn)
1079 (funcall fn beg end))
1080 c-get-state-before-change-functions)))
1081 )))
1082 ;; The following must be done here rather than in `c-after-change' because
1083 ;; newly inserted parens would foul up the invalidation algorithm.
1084 (c-invalidate-state-cache beg)))
1085
1086 (defvar c-in-after-change-fontification nil)
1087 (make-variable-buffer-local 'c-in-after-change-fontification)
1088 ;; A flag to prevent region expanding stuff being done twice for after-change
1089 ;; fontification.
1090
1091 (defun c-after-change (beg end old-len)
1092 ;; Function put on `after-change-functions' to adjust various caches
1093 ;; etc. Prefer speed to finesse here, since there will be an order
1094 ;; of magnitude more calls to this function than any of the
1095 ;; functions that use the caches.
1096 ;;
1097 ;; Note that care must be taken so that this is called before any
1098 ;; font-lock callbacks since we might get calls to functions using
1099 ;; these caches from inside them, and we must thus be sure that this
1100 ;; has already been executed.
1101 ;;
1102 ;; This calls the language variable c-before-font-lock-functions, if non nil.
1103 ;; This typically sets `syntax-table' properties.
1104
1105 (setq c-just-done-before-change nil)
1106 (c-save-buffer-state (case-fold-search open-paren-in-column-0-is-defun-start)
1107 ;; When `combine-after-change-calls' is used we might get calls
1108 ;; with regions outside the current narrowing. This has been
1109 ;; observed in Emacs 20.7.
1110 (save-restriction
1111 (save-match-data ; c-recognize-<>-arglists changes match-data
1112 (widen)
1113
1114 (when (> end (point-max))
1115 ;; Some emacsen might return positions past the end. This has been
1116 ;; observed in Emacs 20.7 when rereading a buffer changed on disk
1117 ;; (haven't been able to minimize it, but Emacs 21.3 appears to
1118 ;; work).
1119 (setq end (point-max))
1120 (when (> beg end)
1121 (setq beg end)))
1122
1123 ;; C-y is capable of spuriously converting category properties
1124 ;; c-</>-as-paren-syntax and c-cpp-delimiter into hard syntax-table
1125 ;; properties. Remove these when it happens.
1126 (when (eval-when-compile (memq 'category-properties c-emacs-features))
1127 (c-clear-char-property-with-value beg end 'syntax-table
1128 c-<-as-paren-syntax)
1129 (c-clear-char-property-with-value beg end 'syntax-table
1130 c->-as-paren-syntax)
1131 (c-clear-char-property-with-value beg end 'syntax-table nil))
1132
1133 (c-trim-found-types beg end old-len) ; maybe we don't need all of these.
1134 (c-invalidate-sws-region-after beg end)
1135 ;; (c-invalidate-state-cache beg) ; moved to `c-before-change'.
1136 (c-invalidate-find-decl-cache beg)
1137
1138 (when c-recognize-<>-arglists
1139 (c-after-change-check-<>-operators beg end))
1140
1141 ;; (c-new-BEG c-new-END) will be the region to fontify. It may become
1142 ;; larger than (beg end).
1143 (setq c-new-BEG beg
1144 c-new-END end)
1145 (setq c-in-after-change-fontification t)
1146 (save-excursion
1147 (mapc (lambda (fn)
1148 (funcall fn beg end old-len))
1149 c-before-font-lock-functions))))))
1150
1151 (defun c-set-fl-decl-start (pos)
1152 ;; If the beginning of the line containing POS is in the middle of a "local"
1153 ;; declaration (i.e. one which does not start outside of braces enclosing
1154 ;; POS, such as a struct), return the beginning of that declaration.
1155 ;; Otherwise return POS. Note that declarations, in this sense, can be
1156 ;; nested.
1157 ;;
1158 ;; This function is called indirectly from font locking stuff - either from
1159 ;; c-after-change (to prepare for after-change font-locking) or from font
1160 ;; lock context (etc.) fontification.
1161 (let ((lit-limits (c-literal-limits))
1162 (new-pos pos)
1163 bod-lim bo-decl)
1164 (goto-char (c-point 'bol new-pos))
1165 (when lit-limits ; Comment or string.
1166 (goto-char (car lit-limits)))
1167 (setq bod-lim (c-determine-limit 500))
1168
1169 (while
1170 ;; Go to a less nested declaration each time round this loop.
1171 (and
1172 (eq (car (c-beginning-of-decl-1 bod-lim)) 'same)
1173 (progn (setq bo-decl (point))
1174 ;; Are we looking at a keyword such as "template" or
1175 ;; "typedef" which can decorate a type, or the type itself?
1176 (when (or (looking-at c-prefix-spec-kwds-re)
1177 (c-forward-type t))
1178 ;; We've found another candidate position.
1179 (setq new-pos (min new-pos bo-decl))
1180 (goto-char bo-decl))
1181 t)
1182 ;; Try and go out a level to search again.
1183 (progn
1184 (c-backward-syntactic-ws bod-lim)
1185 (and (> (point) bod-lim)
1186 (or (memq (char-before) '(?\( ?\[))
1187 (and (eq (char-before) ?\<)
1188 (eq (c-get-char-property
1189 (1- (point)) 'syntax-table)
1190 c-<-as-paren-syntax)))))
1191 (not (bobp)))
1192 (backward-char))
1193 new-pos)) ; back over (, [, <.
1194
1195 (defun c-change-set-fl-decl-start (_beg _end _old-len)
1196 ;; Set c-new-BEG to the beginning of a "local" declaration if it('s BOL) is
1197 ;; inside one. This is called from an after-change-function, but the
1198 ;; parameters BEG END and OLD-LEN are ignored. See `c-set-fl-decl-start'
1199 ;; for the detailed functionality.
1200 (if font-lock-mode
1201 (setq c-new-BEG (c-set-fl-decl-start c-new-BEG))))
1202
1203 (defun c-context-set-fl-decl-start (beg end)
1204 ;; Return a cons (NEW-BEG . END), where NEW-BEG is the beginning of a
1205 ;; "local" declaration (BOL at) NEW is inside or BEG. See
1206 ;; `c-set-fl-decl-start' for the detailed functionality.
1207 (cons (c-set-fl-decl-start beg) end))
1208
1209 (defun c-font-lock-fontify-region (beg end &optional verbose)
1210 ;; Effectively advice around `font-lock-fontify-region' which extends the
1211 ;; region (BEG END), for example, to avoid context fontification chopping
1212 ;; off the start of the context. Do not do anything if it's already been
1213 ;; done (i.e. from an after-change fontification. An example (C++) where
1214 ;; this used to happen is this:
1215 ;;
1216 ;; template <typename T>
1217 ;;
1218 ;;
1219 ;; void myfunc(T* p) {}
1220 ;;
1221 ;; Type a space in the first blank line, and the fontification of the next
1222 ;; line was fouled up by context fontification.
1223 (let ((new-beg beg) (new-end end) new-region case-fold-search
1224 open-paren-in-column-0-is-defun-start)
1225 (if c-in-after-change-fontification
1226 (setq c-in-after-change-fontification nil)
1227 (save-restriction
1228 (widen)
1229 (save-excursion
1230 (mapc (lambda (fn)
1231 (setq new-region (funcall fn new-beg new-end))
1232 (setq new-beg (car new-region) new-end (cdr new-region)))
1233 c-before-context-fontification-functions))))
1234 (funcall (default-value 'font-lock-fontify-region-function)
1235 new-beg new-end verbose)))
1236
1237 (defun c-after-font-lock-init ()
1238 ;; Put on `font-lock-mode-hook'. This function ensures our after-change
1239 ;; function will get executed before the font-lock one.
1240 (remove-hook 'after-change-functions 'c-after-change t)
1241 (add-hook 'after-change-functions 'c-after-change nil t))
1242
1243 (defun c-font-lock-init ()
1244 "Set up the font-lock variables for using the font-lock support in CC Mode.
1245 This does not load the font-lock package. Use after
1246 `c-basic-common-init' and after cc-fonts has been loaded.
1247 This function is called from `c-common-init', once per mode initialization."
1248
1249 (set (make-local-variable 'font-lock-defaults)
1250 `(,(if (c-major-mode-is 'awk-mode)
1251 ;; awk-mode currently has only one font lock level.
1252 'awk-font-lock-keywords
1253 (mapcar 'c-mode-symbol
1254 '("font-lock-keywords" "font-lock-keywords-1"
1255 "font-lock-keywords-2" "font-lock-keywords-3")))
1256 nil nil
1257 ,c-identifier-syntax-modifications
1258 c-beginning-of-syntax
1259 (font-lock-mark-block-function
1260 . c-mark-function)))
1261
1262 (make-local-variable 'font-lock-fontify-region-function)
1263 (setq font-lock-fontify-region-function 'c-font-lock-fontify-region)
1264
1265 (if (featurep 'xemacs)
1266 (make-local-hook 'font-lock-mode-hook))
1267 (add-hook 'font-lock-mode-hook 'c-after-font-lock-init nil t))
1268
1269 ;; Emacs 22 and later.
1270 (defun c-extend-after-change-region (_beg _end _old-len)
1271 "Extend the region to be fontified, if necessary."
1272 ;; Note: the parameters are ignored here. This somewhat indirect
1273 ;; implementation exists because it is minimally different from the
1274 ;; stand-alone CC Mode which, lacking
1275 ;; font-lock-extend-after-change-region-function, is forced to use advice
1276 ;; instead.
1277 ;;
1278 ;; Of the seven CC Mode languages, currently (2009-05) only C, C++, Objc
1279 ;; (the languages with #define) and AWK Mode make non-null use of this
1280 ;; function.
1281 (cons c-new-BEG c-new-END))
1282
1283 ;; Emacs < 22 and XEmacs
1284 (defmacro c-advise-fl-for-region (function)
1285 `(defadvice ,function (before get-awk-region activate)
1286 ;; Make sure that any string/regexp is completely font-locked.
1287 (when c-buffer-is-cc-mode
1288 (save-excursion
1289 (ad-set-arg 1 c-new-END) ; end
1290 (ad-set-arg 0 c-new-BEG))))) ; beg
1291
1292 (unless (boundp 'font-lock-extend-after-change-region-function)
1293 (c-advise-fl-for-region font-lock-after-change-function)
1294 (c-advise-fl-for-region jit-lock-after-change)
1295 (c-advise-fl-for-region lazy-lock-defer-rest-after-change)
1296 (c-advise-fl-for-region lazy-lock-defer-line-after-change))
1297
1298 ;; Connect up to `electric-indent-mode' (Emacs 24.4 and later).
1299 (defun c-electric-indent-mode-hook ()
1300 ;; Emacs has en/disabled `electric-indent-mode'. Propagate this through to
1301 ;; each CC Mode buffer.
1302 (mapc (lambda (buf)
1303 (with-current-buffer buf
1304 (when c-buffer-is-cc-mode
1305 ;; Don't use `c-toggle-electric-state' here due to recursion.
1306 (setq c-electric-flag electric-indent-mode)
1307 (c-update-modeline))))
1308 (buffer-list)))
1309
1310 (defun c-electric-indent-local-mode-hook ()
1311 ;; Emacs has en/disabled `electric-indent-local-mode' for this buffer.
1312 ;; Propagate this through to this buffer's value of `c-electric-flag'
1313 (when c-buffer-is-cc-mode
1314 (setq c-electric-flag electric-indent-mode)
1315 (c-update-modeline)))
1316
1317 \f
1318 ;; Support for C
1319
1320 (defvar c-mode-syntax-table
1321 (funcall (c-lang-const c-make-mode-syntax-table c))
1322 "Syntax table used in c-mode buffers.")
1323
1324 (c-define-abbrev-table 'c-mode-abbrev-table
1325 '(("else" "else" c-electric-continued-statement 0)
1326 ("while" "while" c-electric-continued-statement 0))
1327 "Abbreviation table used in c-mode buffers.")
1328
1329 (defvar c-mode-map
1330 (let ((map (c-make-inherited-keymap)))
1331 ;; Add bindings which are only useful for C.
1332 (define-key map "\C-c\C-e" 'c-macro-expand)
1333 map)
1334 "Keymap used in c-mode buffers.")
1335
1336
1337 (easy-menu-define c-c-menu c-mode-map "C Mode Commands"
1338 (cons "C" (c-lang-const c-mode-menu c)))
1339
1340 ;; In XEmacs >= 21.5 modes should add their own entries to
1341 ;; `auto-mode-alist'. The comment form of autoload is used to avoid
1342 ;; doing this on load. That since `add-to-list' prepends the value
1343 ;; which could cause it to clobber user settings. Later emacsen have
1344 ;; an append option, but it's not safe to use.
1345
1346 ;; The extension ".C" is associated with C++ while the lowercase
1347 ;; variant goes with C. On case insensitive file systems, this means
1348 ;; that ".c" files also might open C++ mode if the C++ entry comes
1349 ;; first on `auto-mode-alist'. Thus we try to ensure that ".C" comes
1350 ;; after ".c", and since `add-to-list' adds the entry first we have to
1351 ;; add the ".C" entry first.
1352 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(cc\\|hh\\)\\'" . c++-mode))
1353 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.[ch]\\(pp\\|xx\\|\\+\\+\\)\\'" . c++-mode))
1354 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(CC?\\|HH?\\)\\'" . c++-mode))
1355
1356 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.[ch]\\'" . c-mode))
1357
1358 ;; NB: The following two associate yacc and lex files to C Mode, which
1359 ;; is not really suitable for those formats. Anyway, afaik there's
1360 ;; currently no better mode for them, and besides this is legacy.
1361 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.y\\(acc\\)?\\'" . c-mode))
1362 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.lex\\'" . c-mode))
1363
1364 ;; Preprocessed files generated by C and C++ compilers.
1365 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.i\\'" . c-mode))
1366 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.ii\\'" . c++-mode))
1367
1368 (unless (fboundp 'prog-mode) (defalias 'prog-mode 'fundamental-mode))
1369
1370 ;;;###autoload
1371 (define-derived-mode c-mode prog-mode "C"
1372 "Major mode for editing K&R and ANSI C code.
1373 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1374 c-mode buffer. This automatically sets up a mail buffer with version
1375 information already added. You just need to add a description of the
1376 problem, including a reproducible test case, and send the message.
1377
1378 To see what version of CC Mode you are running, enter `\\[c-version]'.
1379
1380 The hook `c-mode-common-hook' is run with no args at mode
1381 initialization, then `c-mode-hook'.
1382
1383 Key bindings:
1384 \\{c-mode-map}"
1385 (c-initialize-cc-mode t)
1386 (set-syntax-table c-mode-syntax-table)
1387 (setq local-abbrev-table c-mode-abbrev-table
1388 abbrev-mode t)
1389 (use-local-map c-mode-map)
1390 (c-init-language-vars-for 'c-mode)
1391 (c-make-macro-with-semi-re) ; matches macro names whose expansion ends with ;
1392 (c-common-init 'c-mode)
1393 (easy-menu-add c-c-menu)
1394 (cc-imenu-init cc-imenu-c-generic-expression)
1395 (c-run-mode-hooks 'c-mode-common-hook 'c-mode-hook)
1396 (c-update-modeline))
1397
1398 \f
1399 ;; Support for C++
1400
1401 (defvar c++-mode-syntax-table
1402 (funcall (c-lang-const c-make-mode-syntax-table c++))
1403 "Syntax table used in c++-mode buffers.")
1404
1405 (c-define-abbrev-table 'c++-mode-abbrev-table
1406 '(("else" "else" c-electric-continued-statement 0)
1407 ("while" "while" c-electric-continued-statement 0)
1408 ("catch" "catch" c-electric-continued-statement 0))
1409 "Abbreviation table used in c++-mode buffers.")
1410
1411 (defvar c++-mode-map
1412 (let ((map (c-make-inherited-keymap)))
1413 ;; Add bindings which are only useful for C++.
1414 (define-key map "\C-c\C-e" 'c-macro-expand)
1415 (define-key map "\C-c:" 'c-scope-operator)
1416 (define-key map "<" 'c-electric-lt-gt)
1417 (define-key map ">" 'c-electric-lt-gt)
1418 map)
1419 "Keymap used in c++-mode buffers.")
1420
1421 (easy-menu-define c-c++-menu c++-mode-map "C++ Mode Commands"
1422 (cons "C++" (c-lang-const c-mode-menu c++)))
1423
1424 ;;;###autoload
1425 (define-derived-mode c++-mode prog-mode "C++"
1426 "Major mode for editing C++ code.
1427 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1428 c++-mode buffer. This automatically sets up a mail buffer with
1429 version information already added. You just need to add a description
1430 of the problem, including a reproducible test case, and send the
1431 message.
1432
1433 To see what version of CC Mode you are running, enter `\\[c-version]'.
1434
1435 The hook `c-mode-common-hook' is run with no args at mode
1436 initialization, then `c++-mode-hook'.
1437
1438 Key bindings:
1439 \\{c++-mode-map}"
1440 (c-initialize-cc-mode t)
1441 (set-syntax-table c++-mode-syntax-table)
1442 (setq local-abbrev-table c++-mode-abbrev-table
1443 abbrev-mode t)
1444 (use-local-map c++-mode-map)
1445 (c-init-language-vars-for 'c++-mode)
1446 (c-make-macro-with-semi-re) ; matches macro names whose expansion ends with ;
1447 (c-common-init 'c++-mode)
1448 (easy-menu-add c-c++-menu)
1449 (cc-imenu-init cc-imenu-c++-generic-expression)
1450 (c-run-mode-hooks 'c-mode-common-hook 'c++-mode-hook)
1451 (c-update-modeline))
1452
1453 \f
1454 ;; Support for Objective-C
1455
1456 (defvar objc-mode-syntax-table
1457 (funcall (c-lang-const c-make-mode-syntax-table objc))
1458 "Syntax table used in objc-mode buffers.")
1459
1460 (c-define-abbrev-table 'objc-mode-abbrev-table
1461 '(("else" "else" c-electric-continued-statement 0)
1462 ("while" "while" c-electric-continued-statement 0))
1463 "Abbreviation table used in objc-mode buffers.")
1464
1465 (defvar objc-mode-map
1466 (let ((map (c-make-inherited-keymap)))
1467 ;; Add bindings which are only useful for Objective-C.
1468 (define-key map "\C-c\C-e" 'c-macro-expand)
1469 map)
1470 "Keymap used in objc-mode buffers.")
1471
1472 (easy-menu-define c-objc-menu objc-mode-map "ObjC Mode Commands"
1473 (cons "ObjC" (c-lang-const c-mode-menu objc)))
1474
1475 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.m\\'" . objc-mode))
1476
1477 ;;;###autoload
1478 (define-derived-mode objc-mode prog-mode "ObjC"
1479 "Major mode for editing Objective C code.
1480 To submit a problem report, enter `\\[c-submit-bug-report]' from an
1481 objc-mode buffer. This automatically sets up a mail buffer with
1482 version information already added. You just need to add a description
1483 of the problem, including a reproducible test case, and send the
1484 message.
1485
1486 To see what version of CC Mode you are running, enter `\\[c-version]'.
1487
1488 The hook `c-mode-common-hook' is run with no args at mode
1489 initialization, then `objc-mode-hook'.
1490
1491 Key bindings:
1492 \\{objc-mode-map}"
1493 (c-initialize-cc-mode t)
1494 (set-syntax-table objc-mode-syntax-table)
1495 (setq local-abbrev-table objc-mode-abbrev-table
1496 abbrev-mode t)
1497 (use-local-map objc-mode-map)
1498 (c-init-language-vars-for 'objc-mode)
1499 (c-make-macro-with-semi-re) ; matches macro names whose expansion ends with ;
1500 (c-common-init 'objc-mode)
1501 (easy-menu-add c-objc-menu)
1502 (cc-imenu-init nil 'cc-imenu-objc-function)
1503 (c-run-mode-hooks 'c-mode-common-hook 'objc-mode-hook)
1504 (c-update-modeline))
1505
1506 \f
1507 ;; Support for Java
1508
1509 (defvar java-mode-syntax-table
1510 (funcall (c-lang-const c-make-mode-syntax-table java))
1511 "Syntax table used in java-mode buffers.")
1512
1513 (c-define-abbrev-table 'java-mode-abbrev-table
1514 '(("else" "else" c-electric-continued-statement 0)
1515 ("while" "while" c-electric-continued-statement 0)
1516 ("catch" "catch" c-electric-continued-statement 0)
1517 ("finally" "finally" c-electric-continued-statement 0))
1518 "Abbreviation table used in java-mode buffers.")
1519
1520 (defvar java-mode-map
1521 (let ((map (c-make-inherited-keymap)))
1522 ;; Add bindings which are only useful for Java.
1523 map)
1524 "Keymap used in java-mode buffers.")
1525
1526 ;; Regexp trying to describe the beginning of a Java top-level
1527 ;; definition. This is not used by CC Mode, nor is it maintained
1528 ;; since it's practically impossible to write a regexp that reliably
1529 ;; matches such a construct. Other tools are necessary.
1530 (defconst c-Java-defun-prompt-regexp
1531 "^[ \t]*\\(\\(\\(public\\|protected\\|private\\|const\\|abstract\\|synchronized\\|final\\|static\\|threadsafe\\|transient\\|native\\|volatile\\)\\s-+\\)*\\(\\(\\([[a-zA-Z][][_$.a-zA-Z0-9]*[][_$.a-zA-Z0-9]+\\|[[a-zA-Z]\\)\\s-*\\)\\s-+\\)\\)?\\(\\([[a-zA-Z][][_$.a-zA-Z0-9]*\\s-+\\)\\s-*\\)?\\([_a-zA-Z][^][ \t:;.,{}()\7f=]*\\|\\([_$a-zA-Z][_$.a-zA-Z0-9]*\\)\\)\\s-*\\(([^);{}]*)\\)?\\([] \t]*\\)\\(\\s-*\\<throws\\>\\s-*\\(\\([_$a-zA-Z][_$.a-zA-Z0-9]*\\)[, \t\n\r\f\v]*\\)+\\)?\\s-*")
1532
1533 (easy-menu-define c-java-menu java-mode-map "Java Mode Commands"
1534 (cons "Java" (c-lang-const c-mode-menu java)))
1535
1536 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.java\\'" . java-mode))
1537
1538 ;;;###autoload
1539 (define-derived-mode java-mode prog-mode "Java"
1540 "Major mode for editing Java code.
1541 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1542 java-mode buffer. This automatically sets up a mail buffer with
1543 version information already added. You just need to add a description
1544 of the problem, including a reproducible test case, and send the
1545 message.
1546
1547 To see what version of CC Mode you are running, enter `\\[c-version]'.
1548
1549 The hook `c-mode-common-hook' is run with no args at mode
1550 initialization, then `java-mode-hook'.
1551
1552 Key bindings:
1553 \\{java-mode-map}"
1554 (c-initialize-cc-mode t)
1555 (set-syntax-table java-mode-syntax-table)
1556 (setq local-abbrev-table java-mode-abbrev-table
1557 abbrev-mode t)
1558 (use-local-map java-mode-map)
1559 (c-init-language-vars-for 'java-mode)
1560 (c-common-init 'java-mode)
1561 (easy-menu-add c-java-menu)
1562 (cc-imenu-init cc-imenu-java-generic-expression)
1563 (c-run-mode-hooks 'c-mode-common-hook 'java-mode-hook)
1564 (c-update-modeline))
1565
1566 \f
1567 ;; Support for CORBA's IDL language
1568
1569 (defvar idl-mode-syntax-table
1570 (funcall (c-lang-const c-make-mode-syntax-table idl))
1571 "Syntax table used in idl-mode buffers.")
1572
1573 (c-define-abbrev-table 'idl-mode-abbrev-table nil
1574 "Abbreviation table used in idl-mode buffers.")
1575
1576 (defvar idl-mode-map
1577 (let ((map (c-make-inherited-keymap)))
1578 ;; Add bindings which are only useful for IDL.
1579 map)
1580 "Keymap used in idl-mode buffers.")
1581
1582 (easy-menu-define c-idl-menu idl-mode-map "IDL Mode Commands"
1583 (cons "IDL" (c-lang-const c-mode-menu idl)))
1584
1585 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.idl\\'" . idl-mode))
1586
1587 ;;;###autoload
1588 (define-derived-mode idl-mode prog-mode "IDL"
1589 "Major mode for editing CORBA's IDL, PSDL and CIDL code.
1590 To submit a problem report, enter `\\[c-submit-bug-report]' from an
1591 idl-mode buffer. This automatically sets up a mail buffer with
1592 version information already added. You just need to add a description
1593 of the problem, including a reproducible test case, and send the
1594 message.
1595
1596 To see what version of CC Mode you are running, enter `\\[c-version]'.
1597
1598 The hook `c-mode-common-hook' is run with no args at mode
1599 initialization, then `idl-mode-hook'.
1600
1601 Key bindings:
1602 \\{idl-mode-map}"
1603 (c-initialize-cc-mode t)
1604 (set-syntax-table idl-mode-syntax-table)
1605 (setq local-abbrev-table idl-mode-abbrev-table)
1606 (use-local-map idl-mode-map)
1607 (c-init-language-vars-for 'idl-mode)
1608 (c-common-init 'idl-mode)
1609 (easy-menu-add c-idl-menu)
1610 ;;(cc-imenu-init cc-imenu-idl-generic-expression) ;TODO
1611 (c-run-mode-hooks 'c-mode-common-hook 'idl-mode-hook)
1612 (c-update-modeline))
1613
1614 \f
1615 ;; Support for Pike
1616
1617 (defvar pike-mode-syntax-table
1618 (funcall (c-lang-const c-make-mode-syntax-table pike))
1619 "Syntax table used in pike-mode buffers.")
1620
1621 (c-define-abbrev-table 'pike-mode-abbrev-table
1622 '(("else" "else" c-electric-continued-statement 0)
1623 ("while" "while" c-electric-continued-statement 0))
1624 "Abbreviation table used in pike-mode buffers.")
1625
1626 (defvar pike-mode-map
1627 (let ((map (c-make-inherited-keymap)))
1628 ;; Additional bindings.
1629 (define-key map "\C-c\C-e" 'c-macro-expand)
1630 map)
1631 "Keymap used in pike-mode buffers.")
1632
1633 (easy-menu-define c-pike-menu pike-mode-map "Pike Mode Commands"
1634 (cons "Pike" (c-lang-const c-mode-menu pike)))
1635
1636 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(u?lpc\\|pike\\|pmod\\(\\.in\\)?\\)\\'" . pike-mode))
1637 ;;;###autoload (add-to-list 'interpreter-mode-alist '("pike" . pike-mode))
1638
1639 ;;;###autoload
1640 (define-derived-mode pike-mode prog-mode "Pike"
1641 "Major mode for editing Pike code.
1642 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1643 pike-mode buffer. This automatically sets up a mail buffer with
1644 version information already added. You just need to add a description
1645 of the problem, including a reproducible test case, and send the
1646 message.
1647
1648 To see what version of CC Mode you are running, enter `\\[c-version]'.
1649
1650 The hook `c-mode-common-hook' is run with no args at mode
1651 initialization, then `pike-mode-hook'.
1652
1653 Key bindings:
1654 \\{pike-mode-map}"
1655 (c-initialize-cc-mode t)
1656 (set-syntax-table pike-mode-syntax-table)
1657 (setq local-abbrev-table pike-mode-abbrev-table
1658 abbrev-mode t)
1659 (use-local-map pike-mode-map)
1660 (c-init-language-vars-for 'pike-mode)
1661 (c-common-init 'pike-mode)
1662 (easy-menu-add c-pike-menu)
1663 ;;(cc-imenu-init cc-imenu-pike-generic-expression) ;TODO
1664 (c-run-mode-hooks 'c-mode-common-hook 'pike-mode-hook)
1665 (c-update-modeline))
1666
1667 \f
1668 ;; Support for AWK
1669
1670 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.awk\\'" . awk-mode))
1671 ;;;###autoload (add-to-list 'interpreter-mode-alist '("awk" . awk-mode))
1672 ;;;###autoload (add-to-list 'interpreter-mode-alist '("mawk" . awk-mode))
1673 ;;;###autoload (add-to-list 'interpreter-mode-alist '("nawk" . awk-mode))
1674 ;;;###autoload (add-to-list 'interpreter-mode-alist '("gawk" . awk-mode))
1675
1676 (c-define-abbrev-table 'awk-mode-abbrev-table
1677 '(("else" "else" c-electric-continued-statement 0)
1678 ("while" "while" c-electric-continued-statement 0))
1679 "Abbreviation table used in awk-mode buffers.")
1680
1681 (defvar awk-mode-map
1682 (let ((map (c-make-inherited-keymap)))
1683 ;; Add bindings which are only useful for awk.
1684 (define-key map "#" 'self-insert-command)
1685 (define-key map "/" 'self-insert-command)
1686 (define-key map "*" 'self-insert-command)
1687 (define-key map "\C-c\C-n" 'undefined) ; #if doesn't exist in awk.
1688 (define-key map "\C-c\C-p" 'undefined)
1689 (define-key map "\C-c\C-u" 'undefined)
1690 (define-key map "\M-a" 'c-beginning-of-statement) ; 2003/10/7
1691 (define-key map "\M-e" 'c-end-of-statement) ; 2003/10/7
1692 (define-key map "\C-\M-a" 'c-awk-beginning-of-defun)
1693 (define-key map "\C-\M-e" 'c-awk-end-of-defun)
1694 map)
1695 "Keymap used in awk-mode buffers.")
1696
1697 (easy-menu-define c-awk-menu awk-mode-map "AWK Mode Commands"
1698 (cons "AWK" (c-lang-const c-mode-menu awk)))
1699
1700 ;; (require 'cc-awk) brings these in.
1701 (defvar awk-mode-syntax-table)
1702 (declare-function c-awk-unstick-NL-prop "cc-awk" ())
1703
1704 ;;;###autoload
1705 (define-derived-mode awk-mode prog-mode "AWK"
1706 "Major mode for editing AWK code.
1707 To submit a problem report, enter `\\[c-submit-bug-report]' from an
1708 awk-mode buffer. This automatically sets up a mail buffer with version
1709 information already added. You just need to add a description of the
1710 problem, including a reproducible test case, and send the message.
1711
1712 To see what version of CC Mode you are running, enter `\\[c-version]'.
1713
1714 The hook `c-mode-common-hook' is run with no args at mode
1715 initialization, then `awk-mode-hook'.
1716
1717 Key bindings:
1718 \\{awk-mode-map}"
1719 ;; We need the next line to stop the macro defining
1720 ;; `awk-mode-syntax-table'. This would mask the real table which is
1721 ;; declared in cc-awk.el and hasn't yet been loaded.
1722 :syntax-table nil
1723 (require 'cc-awk) ; Added 2003/6/10.
1724 (c-initialize-cc-mode t)
1725 (set-syntax-table awk-mode-syntax-table)
1726 (setq local-abbrev-table awk-mode-abbrev-table
1727 abbrev-mode t)
1728 (use-local-map awk-mode-map)
1729 (c-init-language-vars-for 'awk-mode)
1730 (c-common-init 'awk-mode)
1731 (c-awk-unstick-NL-prop)
1732
1733 (c-run-mode-hooks 'c-mode-common-hook 'awk-mode-hook)
1734 (c-update-modeline))
1735
1736 \f
1737 ;; bug reporting
1738
1739 (defconst c-mode-help-address
1740 "bug-cc-mode@gnu.org"
1741 "Address(es) for CC Mode bug reports.")
1742
1743 (defun c-version ()
1744 "Echo the current version of CC Mode in the minibuffer."
1745 (interactive)
1746 (message "Using CC Mode version %s" c-version)
1747 (c-keep-region-active))
1748
1749 (define-obsolete-variable-alias 'c-prepare-bug-report-hooks
1750 'c-prepare-bug-report-hook "24.3")
1751 (defvar c-prepare-bug-report-hook nil)
1752
1753 ;; Dynamic variables used by reporter.
1754 (defvar reporter-prompt-for-summary-p)
1755 (defvar reporter-dont-compact-list)
1756
1757 (defun c-submit-bug-report ()
1758 "Submit via mail a bug report on CC Mode."
1759 (interactive)
1760 (require 'reporter)
1761 ;; load in reporter
1762 (let ((reporter-prompt-for-summary-p t)
1763 (reporter-dont-compact-list '(c-offsets-alist))
1764 (style c-indentation-style)
1765 (c-features c-emacs-features))
1766 (and
1767 (if (y-or-n-p "Do you want to submit a report on CC Mode? ")
1768 t (message "") nil)
1769 (reporter-submit-bug-report
1770 c-mode-help-address
1771 (concat "CC Mode " c-version " (" mode-name ")")
1772 (let ((vars (append
1773 c-style-variables
1774 '(c-buffer-is-cc-mode
1775 c-tab-always-indent
1776 c-syntactic-indentation
1777 c-syntactic-indentation-in-macros
1778 c-ignore-auto-fill
1779 c-auto-align-backslashes
1780 c-backspace-function
1781 c-delete-function
1782 c-electric-pound-behavior
1783 c-default-style
1784 c-enable-xemacs-performance-kludge-p
1785 c-old-style-variable-behavior
1786 defun-prompt-regexp
1787 tab-width
1788 comment-column
1789 parse-sexp-ignore-comments
1790 parse-sexp-lookup-properties
1791 lookup-syntax-properties
1792 ;; A brain-damaged XEmacs only variable that, if
1793 ;; set to nil can cause all kinds of chaos.
1794 signal-error-on-buffer-boundary
1795 ;; Variables that affect line breaking and comments.
1796 auto-fill-mode
1797 auto-fill-function
1798 filladapt-mode
1799 comment-multi-line
1800 comment-start-skip
1801 fill-prefix
1802 fill-column
1803 paragraph-start
1804 adaptive-fill-mode
1805 adaptive-fill-regexp)
1806 nil)))
1807 (mapc (lambda (var) (unless (boundp var)
1808 (setq vars (delq var vars))))
1809 '(signal-error-on-buffer-boundary
1810 filladapt-mode
1811 defun-prompt-regexp
1812 font-lock-mode
1813 auto-fill-mode
1814 font-lock-maximum-decoration
1815 parse-sexp-lookup-properties
1816 lookup-syntax-properties))
1817 vars)
1818 (lambda ()
1819 (run-hooks 'c-prepare-bug-report-hook)
1820 (insert (format "Buffer Style: %s\nc-emacs-features: %s\n"
1821 style c-features)))))))
1822
1823 \f
1824 (cc-provide 'cc-mode)
1825
1826 ;;; Local Variables:
1827 ;;; indent-tabs-mode: t
1828 ;;; tab-width: 8
1829 ;;; End:
1830 ;;; cc-mode.el ends here