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