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