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