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