]> 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 ;; Prevent `font-lock-default-fontify-region' extending the region it will
1263 ;; fontify to whole lines by removing `font-lock-extend-region-whole-lines'
1264 ;; (and, coincidentally, `font-lock-extend-region-multiline' (which we do
1265 ;; not need)) from `font-lock-extend-region-functions'. (Emacs only). This
1266 ;; fixes Emacs bug #19669.
1267 (when (boundp 'font-lock-extend-region-functions)
1268 (setq font-lock-extend-region-functions nil))
1269
1270 (make-local-variable 'font-lock-fontify-region-function)
1271 (setq font-lock-fontify-region-function 'c-font-lock-fontify-region)
1272
1273 (if (featurep 'xemacs)
1274 (make-local-hook 'font-lock-mode-hook))
1275 (add-hook 'font-lock-mode-hook 'c-after-font-lock-init nil t))
1276
1277 ;; Emacs 22 and later.
1278 (defun c-extend-after-change-region (_beg _end _old-len)
1279 "Extend the region to be fontified, if necessary."
1280 ;; Note: the parameters are ignored here. This somewhat indirect
1281 ;; implementation exists because it is minimally different from the
1282 ;; stand-alone CC Mode which, lacking
1283 ;; font-lock-extend-after-change-region-function, is forced to use advice
1284 ;; instead.
1285 ;;
1286 ;; Of the seven CC Mode languages, currently (2009-05) only C, C++, Objc
1287 ;; (the languages with #define) and AWK Mode make non-null use of this
1288 ;; function.
1289 (cons c-new-BEG c-new-END))
1290
1291 ;; Emacs < 22 and XEmacs
1292 (defmacro c-advise-fl-for-region (function)
1293 `(defadvice ,function (before get-awk-region activate)
1294 ;; Make sure that any string/regexp is completely font-locked.
1295 (when c-buffer-is-cc-mode
1296 (save-excursion
1297 (ad-set-arg 1 c-new-END) ; end
1298 (ad-set-arg 0 c-new-BEG))))) ; beg
1299
1300 (unless (boundp 'font-lock-extend-after-change-region-function)
1301 (c-advise-fl-for-region font-lock-after-change-function)
1302 (c-advise-fl-for-region jit-lock-after-change)
1303 (c-advise-fl-for-region lazy-lock-defer-rest-after-change)
1304 (c-advise-fl-for-region lazy-lock-defer-line-after-change))
1305
1306 ;; Connect up to `electric-indent-mode' (Emacs 24.4 and later).
1307 (defun c-electric-indent-mode-hook ()
1308 ;; Emacs has en/disabled `electric-indent-mode'. Propagate this through to
1309 ;; each CC Mode buffer.
1310 (mapc (lambda (buf)
1311 (with-current-buffer buf
1312 (when c-buffer-is-cc-mode
1313 ;; Don't use `c-toggle-electric-state' here due to recursion.
1314 (setq c-electric-flag electric-indent-mode)
1315 (c-update-modeline))))
1316 (buffer-list)))
1317
1318 (defun c-electric-indent-local-mode-hook ()
1319 ;; Emacs has en/disabled `electric-indent-local-mode' for this buffer.
1320 ;; Propagate this through to this buffer's value of `c-electric-flag'
1321 (when c-buffer-is-cc-mode
1322 (setq c-electric-flag electric-indent-mode)
1323 (c-update-modeline)))
1324
1325 \f
1326 ;; Support for C
1327
1328 (defvar c-mode-syntax-table
1329 (funcall (c-lang-const c-make-mode-syntax-table c))
1330 "Syntax table used in c-mode buffers.")
1331
1332 (c-define-abbrev-table 'c-mode-abbrev-table
1333 '(("else" "else" c-electric-continued-statement 0)
1334 ("while" "while" c-electric-continued-statement 0))
1335 "Abbreviation table used in c-mode buffers.")
1336
1337 (defvar c-mode-map
1338 (let ((map (c-make-inherited-keymap)))
1339 ;; Add bindings which are only useful for C.
1340 (define-key map "\C-c\C-e" 'c-macro-expand)
1341 map)
1342 "Keymap used in c-mode buffers.")
1343
1344
1345 (easy-menu-define c-c-menu c-mode-map "C Mode Commands"
1346 (cons "C" (c-lang-const c-mode-menu c)))
1347
1348 ;; In XEmacs >= 21.5 modes should add their own entries to
1349 ;; `auto-mode-alist'. The comment form of autoload is used to avoid
1350 ;; doing this on load. That since `add-to-list' prepends the value
1351 ;; which could cause it to clobber user settings. Later emacsen have
1352 ;; an append option, but it's not safe to use.
1353
1354 ;; The extension ".C" is associated with C++ while the lowercase
1355 ;; variant goes with C. On case insensitive file systems, this means
1356 ;; that ".c" files also might open C++ mode if the C++ entry comes
1357 ;; first on `auto-mode-alist'. Thus we try to ensure that ".C" comes
1358 ;; after ".c", and since `add-to-list' adds the entry first we have to
1359 ;; add the ".C" entry first.
1360 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(cc\\|hh\\)\\'" . c++-mode))
1361 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.[ch]\\(pp\\|xx\\|\\+\\+\\)\\'" . c++-mode))
1362 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(CC?\\|HH?\\)\\'" . c++-mode))
1363
1364 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.[ch]\\'" . c-mode))
1365
1366 ;; NB: The following two associate yacc and lex files to C Mode, which
1367 ;; is not really suitable for those formats. Anyway, afaik there's
1368 ;; currently no better mode for them, and besides this is legacy.
1369 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.y\\(acc\\)?\\'" . c-mode))
1370 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.lex\\'" . c-mode))
1371
1372 ;; Preprocessed files generated by C and C++ compilers.
1373 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.i\\'" . c-mode))
1374 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.ii\\'" . c++-mode))
1375
1376 (unless (fboundp 'prog-mode) (defalias 'prog-mode 'fundamental-mode))
1377
1378 ;;;###autoload
1379 (define-derived-mode c-mode prog-mode "C"
1380 "Major mode for editing K&R and ANSI C code.
1381 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1382 c-mode buffer. This automatically sets up a mail buffer with version
1383 information already added. You just need to add a description of the
1384 problem, including a reproducible test case, and send the message.
1385
1386 To see what version of CC Mode you are running, enter `\\[c-version]'.
1387
1388 The hook `c-mode-common-hook' is run with no args at mode
1389 initialization, then `c-mode-hook'.
1390
1391 Key bindings:
1392 \\{c-mode-map}"
1393 (c-initialize-cc-mode t)
1394 (set-syntax-table c-mode-syntax-table)
1395 (setq local-abbrev-table c-mode-abbrev-table
1396 abbrev-mode t)
1397 (use-local-map c-mode-map)
1398 (c-init-language-vars-for 'c-mode)
1399 (c-make-macro-with-semi-re) ; matches macro names whose expansion ends with ;
1400 (c-common-init 'c-mode)
1401 (easy-menu-add c-c-menu)
1402 (cc-imenu-init cc-imenu-c-generic-expression)
1403 (c-run-mode-hooks 'c-mode-common-hook 'c-mode-hook)
1404 (c-update-modeline))
1405
1406 \f
1407 ;; Support for C++
1408
1409 (defvar c++-mode-syntax-table
1410 (funcall (c-lang-const c-make-mode-syntax-table c++))
1411 "Syntax table used in c++-mode buffers.")
1412
1413 (c-define-abbrev-table 'c++-mode-abbrev-table
1414 '(("else" "else" c-electric-continued-statement 0)
1415 ("while" "while" c-electric-continued-statement 0)
1416 ("catch" "catch" c-electric-continued-statement 0))
1417 "Abbreviation table used in c++-mode buffers.")
1418
1419 (defvar c++-mode-map
1420 (let ((map (c-make-inherited-keymap)))
1421 ;; Add bindings which are only useful for C++.
1422 (define-key map "\C-c\C-e" 'c-macro-expand)
1423 (define-key map "\C-c:" 'c-scope-operator)
1424 (define-key map "<" 'c-electric-lt-gt)
1425 (define-key map ">" 'c-electric-lt-gt)
1426 map)
1427 "Keymap used in c++-mode buffers.")
1428
1429 (easy-menu-define c-c++-menu c++-mode-map "C++ Mode Commands"
1430 (cons "C++" (c-lang-const c-mode-menu c++)))
1431
1432 ;;;###autoload
1433 (define-derived-mode c++-mode prog-mode "C++"
1434 "Major mode for editing 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
1437 version information already added. You just need to add a description
1438 of the problem, including a reproducible test case, and send the
1439 message.
1440
1441 To see what version of CC Mode you are running, enter `\\[c-version]'.
1442
1443 The hook `c-mode-common-hook' is run with no args at mode
1444 initialization, then `c++-mode-hook'.
1445
1446 Key bindings:
1447 \\{c++-mode-map}"
1448 (c-initialize-cc-mode t)
1449 (set-syntax-table c++-mode-syntax-table)
1450 (setq local-abbrev-table c++-mode-abbrev-table
1451 abbrev-mode t)
1452 (use-local-map c++-mode-map)
1453 (c-init-language-vars-for 'c++-mode)
1454 (c-make-macro-with-semi-re) ; matches macro names whose expansion ends with ;
1455 (c-common-init 'c++-mode)
1456 (easy-menu-add c-c++-menu)
1457 (cc-imenu-init cc-imenu-c++-generic-expression)
1458 (c-run-mode-hooks 'c-mode-common-hook 'c++-mode-hook)
1459 (c-update-modeline))
1460
1461 \f
1462 ;; Support for Objective-C
1463
1464 (defvar objc-mode-syntax-table
1465 (funcall (c-lang-const c-make-mode-syntax-table objc))
1466 "Syntax table used in objc-mode buffers.")
1467
1468 (c-define-abbrev-table 'objc-mode-abbrev-table
1469 '(("else" "else" c-electric-continued-statement 0)
1470 ("while" "while" c-electric-continued-statement 0))
1471 "Abbreviation table used in objc-mode buffers.")
1472
1473 (defvar objc-mode-map
1474 (let ((map (c-make-inherited-keymap)))
1475 ;; Add bindings which are only useful for Objective-C.
1476 (define-key map "\C-c\C-e" 'c-macro-expand)
1477 map)
1478 "Keymap used in objc-mode buffers.")
1479
1480 (easy-menu-define c-objc-menu objc-mode-map "ObjC Mode Commands"
1481 (cons "ObjC" (c-lang-const c-mode-menu objc)))
1482
1483 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.m\\'" . objc-mode))
1484
1485 ;;;###autoload
1486 (define-derived-mode objc-mode prog-mode "ObjC"
1487 "Major mode for editing Objective C code.
1488 To submit a problem report, enter `\\[c-submit-bug-report]' from an
1489 objc-mode buffer. This automatically sets up a mail buffer with
1490 version information already added. You just need to add a description
1491 of the problem, including a reproducible test case, and send the
1492 message.
1493
1494 To see what version of CC Mode you are running, enter `\\[c-version]'.
1495
1496 The hook `c-mode-common-hook' is run with no args at mode
1497 initialization, then `objc-mode-hook'.
1498
1499 Key bindings:
1500 \\{objc-mode-map}"
1501 (c-initialize-cc-mode t)
1502 (set-syntax-table objc-mode-syntax-table)
1503 (setq local-abbrev-table objc-mode-abbrev-table
1504 abbrev-mode t)
1505 (use-local-map objc-mode-map)
1506 (c-init-language-vars-for 'objc-mode)
1507 (c-make-macro-with-semi-re) ; matches macro names whose expansion ends with ;
1508 (c-common-init 'objc-mode)
1509 (easy-menu-add c-objc-menu)
1510 (cc-imenu-init nil 'cc-imenu-objc-function)
1511 (c-run-mode-hooks 'c-mode-common-hook 'objc-mode-hook)
1512 (c-update-modeline))
1513
1514 \f
1515 ;; Support for Java
1516
1517 (defvar java-mode-syntax-table
1518 (funcall (c-lang-const c-make-mode-syntax-table java))
1519 "Syntax table used in java-mode buffers.")
1520
1521 (c-define-abbrev-table 'java-mode-abbrev-table
1522 '(("else" "else" c-electric-continued-statement 0)
1523 ("while" "while" c-electric-continued-statement 0)
1524 ("catch" "catch" c-electric-continued-statement 0)
1525 ("finally" "finally" c-electric-continued-statement 0))
1526 "Abbreviation table used in java-mode buffers.")
1527
1528 (defvar java-mode-map
1529 (let ((map (c-make-inherited-keymap)))
1530 ;; Add bindings which are only useful for Java.
1531 map)
1532 "Keymap used in java-mode buffers.")
1533
1534 ;; Regexp trying to describe the beginning of a Java top-level
1535 ;; definition. This is not used by CC Mode, nor is it maintained
1536 ;; since it's practically impossible to write a regexp that reliably
1537 ;; matches such a construct. Other tools are necessary.
1538 (defconst c-Java-defun-prompt-regexp
1539 "^[ \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-*")
1540
1541 (easy-menu-define c-java-menu java-mode-map "Java Mode Commands"
1542 (cons "Java" (c-lang-const c-mode-menu java)))
1543
1544 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.java\\'" . java-mode))
1545
1546 ;;;###autoload
1547 (define-derived-mode java-mode prog-mode "Java"
1548 "Major mode for editing Java code.
1549 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1550 java-mode buffer. This automatically sets up a mail buffer with
1551 version information already added. You just need to add a description
1552 of the problem, including a reproducible test case, and send the
1553 message.
1554
1555 To see what version of CC Mode you are running, enter `\\[c-version]'.
1556
1557 The hook `c-mode-common-hook' is run with no args at mode
1558 initialization, then `java-mode-hook'.
1559
1560 Key bindings:
1561 \\{java-mode-map}"
1562 (c-initialize-cc-mode t)
1563 (set-syntax-table java-mode-syntax-table)
1564 (setq local-abbrev-table java-mode-abbrev-table
1565 abbrev-mode t)
1566 (use-local-map java-mode-map)
1567 (c-init-language-vars-for 'java-mode)
1568 (c-common-init 'java-mode)
1569 (easy-menu-add c-java-menu)
1570 (cc-imenu-init cc-imenu-java-generic-expression)
1571 (c-run-mode-hooks 'c-mode-common-hook 'java-mode-hook)
1572 (c-update-modeline))
1573
1574 \f
1575 ;; Support for CORBA's IDL language
1576
1577 (defvar idl-mode-syntax-table
1578 (funcall (c-lang-const c-make-mode-syntax-table idl))
1579 "Syntax table used in idl-mode buffers.")
1580
1581 (c-define-abbrev-table 'idl-mode-abbrev-table nil
1582 "Abbreviation table used in idl-mode buffers.")
1583
1584 (defvar idl-mode-map
1585 (let ((map (c-make-inherited-keymap)))
1586 ;; Add bindings which are only useful for IDL.
1587 map)
1588 "Keymap used in idl-mode buffers.")
1589
1590 (easy-menu-define c-idl-menu idl-mode-map "IDL Mode Commands"
1591 (cons "IDL" (c-lang-const c-mode-menu idl)))
1592
1593 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.idl\\'" . idl-mode))
1594
1595 ;;;###autoload
1596 (define-derived-mode idl-mode prog-mode "IDL"
1597 "Major mode for editing CORBA's IDL, PSDL and CIDL code.
1598 To submit a problem report, enter `\\[c-submit-bug-report]' from an
1599 idl-mode buffer. This automatically sets up a mail buffer with
1600 version information already added. You just need to add a description
1601 of the problem, including a reproducible test case, and send the
1602 message.
1603
1604 To see what version of CC Mode you are running, enter `\\[c-version]'.
1605
1606 The hook `c-mode-common-hook' is run with no args at mode
1607 initialization, then `idl-mode-hook'.
1608
1609 Key bindings:
1610 \\{idl-mode-map}"
1611 (c-initialize-cc-mode t)
1612 (set-syntax-table idl-mode-syntax-table)
1613 (setq local-abbrev-table idl-mode-abbrev-table)
1614 (use-local-map idl-mode-map)
1615 (c-init-language-vars-for 'idl-mode)
1616 (c-common-init 'idl-mode)
1617 (easy-menu-add c-idl-menu)
1618 ;;(cc-imenu-init cc-imenu-idl-generic-expression) ;TODO
1619 (c-run-mode-hooks 'c-mode-common-hook 'idl-mode-hook)
1620 (c-update-modeline))
1621
1622 \f
1623 ;; Support for Pike
1624
1625 (defvar pike-mode-syntax-table
1626 (funcall (c-lang-const c-make-mode-syntax-table pike))
1627 "Syntax table used in pike-mode buffers.")
1628
1629 (c-define-abbrev-table 'pike-mode-abbrev-table
1630 '(("else" "else" c-electric-continued-statement 0)
1631 ("while" "while" c-electric-continued-statement 0))
1632 "Abbreviation table used in pike-mode buffers.")
1633
1634 (defvar pike-mode-map
1635 (let ((map (c-make-inherited-keymap)))
1636 ;; Additional bindings.
1637 (define-key map "\C-c\C-e" 'c-macro-expand)
1638 map)
1639 "Keymap used in pike-mode buffers.")
1640
1641 (easy-menu-define c-pike-menu pike-mode-map "Pike Mode Commands"
1642 (cons "Pike" (c-lang-const c-mode-menu pike)))
1643
1644 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(u?lpc\\|pike\\|pmod\\(\\.in\\)?\\)\\'" . pike-mode))
1645 ;;;###autoload (add-to-list 'interpreter-mode-alist '("pike" . pike-mode))
1646
1647 ;;;###autoload
1648 (define-derived-mode pike-mode prog-mode "Pike"
1649 "Major mode for editing Pike code.
1650 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1651 pike-mode buffer. This automatically sets up a mail buffer with
1652 version information already added. You just need to add a description
1653 of the problem, including a reproducible test case, and send the
1654 message.
1655
1656 To see what version of CC Mode you are running, enter `\\[c-version]'.
1657
1658 The hook `c-mode-common-hook' is run with no args at mode
1659 initialization, then `pike-mode-hook'.
1660
1661 Key bindings:
1662 \\{pike-mode-map}"
1663 (c-initialize-cc-mode t)
1664 (set-syntax-table pike-mode-syntax-table)
1665 (setq local-abbrev-table pike-mode-abbrev-table
1666 abbrev-mode t)
1667 (use-local-map pike-mode-map)
1668 (c-init-language-vars-for 'pike-mode)
1669 (c-common-init 'pike-mode)
1670 (easy-menu-add c-pike-menu)
1671 ;;(cc-imenu-init cc-imenu-pike-generic-expression) ;TODO
1672 (c-run-mode-hooks 'c-mode-common-hook 'pike-mode-hook)
1673 (c-update-modeline))
1674
1675 \f
1676 ;; Support for AWK
1677
1678 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.awk\\'" . awk-mode))
1679 ;;;###autoload (add-to-list 'interpreter-mode-alist '("awk" . awk-mode))
1680 ;;;###autoload (add-to-list 'interpreter-mode-alist '("mawk" . awk-mode))
1681 ;;;###autoload (add-to-list 'interpreter-mode-alist '("nawk" . awk-mode))
1682 ;;;###autoload (add-to-list 'interpreter-mode-alist '("gawk" . awk-mode))
1683
1684 (c-define-abbrev-table 'awk-mode-abbrev-table
1685 '(("else" "else" c-electric-continued-statement 0)
1686 ("while" "while" c-electric-continued-statement 0))
1687 "Abbreviation table used in awk-mode buffers.")
1688
1689 (defvar awk-mode-map
1690 (let ((map (c-make-inherited-keymap)))
1691 ;; Add bindings which are only useful for awk.
1692 (define-key map "#" 'self-insert-command)
1693 (define-key map "/" 'self-insert-command)
1694 (define-key map "*" 'self-insert-command)
1695 (define-key map "\C-c\C-n" 'undefined) ; #if doesn't exist in awk.
1696 (define-key map "\C-c\C-p" 'undefined)
1697 (define-key map "\C-c\C-u" 'undefined)
1698 (define-key map "\M-a" 'c-beginning-of-statement) ; 2003/10/7
1699 (define-key map "\M-e" 'c-end-of-statement) ; 2003/10/7
1700 (define-key map "\C-\M-a" 'c-awk-beginning-of-defun)
1701 (define-key map "\C-\M-e" 'c-awk-end-of-defun)
1702 map)
1703 "Keymap used in awk-mode buffers.")
1704
1705 (easy-menu-define c-awk-menu awk-mode-map "AWK Mode Commands"
1706 (cons "AWK" (c-lang-const c-mode-menu awk)))
1707
1708 ;; (require 'cc-awk) brings these in.
1709 (defvar awk-mode-syntax-table)
1710 (declare-function c-awk-unstick-NL-prop "cc-awk" ())
1711
1712 ;;;###autoload
1713 (define-derived-mode awk-mode prog-mode "AWK"
1714 "Major mode for editing AWK code.
1715 To submit a problem report, enter `\\[c-submit-bug-report]' from an
1716 awk-mode buffer. This automatically sets up a mail buffer with version
1717 information already added. You just need to add a description of the
1718 problem, including a reproducible test case, and send the message.
1719
1720 To see what version of CC Mode you are running, enter `\\[c-version]'.
1721
1722 The hook `c-mode-common-hook' is run with no args at mode
1723 initialization, then `awk-mode-hook'.
1724
1725 Key bindings:
1726 \\{awk-mode-map}"
1727 ;; We need the next line to stop the macro defining
1728 ;; `awk-mode-syntax-table'. This would mask the real table which is
1729 ;; declared in cc-awk.el and hasn't yet been loaded.
1730 :syntax-table nil
1731 (require 'cc-awk) ; Added 2003/6/10.
1732 (c-initialize-cc-mode t)
1733 (set-syntax-table awk-mode-syntax-table)
1734 (setq local-abbrev-table awk-mode-abbrev-table
1735 abbrev-mode t)
1736 (use-local-map awk-mode-map)
1737 (c-init-language-vars-for 'awk-mode)
1738 (c-common-init 'awk-mode)
1739 (c-awk-unstick-NL-prop)
1740
1741 (c-run-mode-hooks 'c-mode-common-hook 'awk-mode-hook)
1742 (c-update-modeline))
1743
1744 \f
1745 ;; bug reporting
1746
1747 (defconst c-mode-help-address
1748 "bug-cc-mode@gnu.org"
1749 "Address(es) for CC Mode bug reports.")
1750
1751 (defun c-version ()
1752 "Echo the current version of CC Mode in the minibuffer."
1753 (interactive)
1754 (message "Using CC Mode version %s" c-version)
1755 (c-keep-region-active))
1756
1757 (define-obsolete-variable-alias 'c-prepare-bug-report-hooks
1758 'c-prepare-bug-report-hook "24.3")
1759 (defvar c-prepare-bug-report-hook nil)
1760
1761 ;; Dynamic variables used by reporter.
1762 (defvar reporter-prompt-for-summary-p)
1763 (defvar reporter-dont-compact-list)
1764
1765 (defun c-submit-bug-report ()
1766 "Submit via mail a bug report on CC Mode."
1767 (interactive)
1768 (require 'reporter)
1769 ;; load in reporter
1770 (let ((reporter-prompt-for-summary-p t)
1771 (reporter-dont-compact-list '(c-offsets-alist))
1772 (style c-indentation-style)
1773 (c-features c-emacs-features))
1774 (and
1775 (if (y-or-n-p "Do you want to submit a report on CC Mode? ")
1776 t (message "") nil)
1777 (reporter-submit-bug-report
1778 c-mode-help-address
1779 (concat "CC Mode " c-version " (" mode-name ")")
1780 (let ((vars (append
1781 c-style-variables
1782 '(c-buffer-is-cc-mode
1783 c-tab-always-indent
1784 c-syntactic-indentation
1785 c-syntactic-indentation-in-macros
1786 c-ignore-auto-fill
1787 c-auto-align-backslashes
1788 c-backspace-function
1789 c-delete-function
1790 c-electric-pound-behavior
1791 c-default-style
1792 c-enable-xemacs-performance-kludge-p
1793 c-old-style-variable-behavior
1794 defun-prompt-regexp
1795 tab-width
1796 comment-column
1797 parse-sexp-ignore-comments
1798 parse-sexp-lookup-properties
1799 lookup-syntax-properties
1800 ;; A brain-damaged XEmacs only variable that, if
1801 ;; set to nil can cause all kinds of chaos.
1802 signal-error-on-buffer-boundary
1803 ;; Variables that affect line breaking and comments.
1804 auto-fill-mode
1805 auto-fill-function
1806 filladapt-mode
1807 comment-multi-line
1808 comment-start-skip
1809 fill-prefix
1810 fill-column
1811 paragraph-start
1812 adaptive-fill-mode
1813 adaptive-fill-regexp)
1814 nil)))
1815 (mapc (lambda (var) (unless (boundp var)
1816 (setq vars (delq var vars))))
1817 '(signal-error-on-buffer-boundary
1818 filladapt-mode
1819 defun-prompt-regexp
1820 font-lock-mode
1821 auto-fill-mode
1822 font-lock-maximum-decoration
1823 parse-sexp-lookup-properties
1824 lookup-syntax-properties))
1825 vars)
1826 (lambda ()
1827 (run-hooks 'c-prepare-bug-report-hook)
1828 (insert (format "Buffer Style: %s\nc-emacs-features: %s\n"
1829 style c-features)))))))
1830
1831 \f
1832 (cc-provide 'cc-mode)
1833
1834 ;;; Local Variables:
1835 ;;; indent-tabs-mode: t
1836 ;;; tab-width: 8
1837 ;;; End:
1838 ;;; cc-mode.el ends here