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