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