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