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