1 ;;; cc-mode.el --- major mode for editing C and similar languages
3 ;; Copyright (C) 1985, 1987, 1992-2017 Free Software Foundation, Inc.
5 ;; Authors: 2003- Alan Mackenzie
6 ;; 1998- Martin Stjernholm
7 ;; 1992-1999 Barry A. Warsaw
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/>.
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
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
72 ;; http://lists.sourceforge.net/mailman/listinfo/cc-mode-announce
74 ;; Externally maintained major modes which use CC-mode's engine include:
76 ;; - csharp-mode (https://github.com/josteink/csharp-mode)
83 ;; - math-mode (mathematica)
84 ;; - unrealscript-mode
91 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest _r
))))
95 (if (and (boundp 'byte-compile-dest-file
)
96 (stringp byte-compile-dest-file
))
97 (cons (file-name-directory byte-compile-dest-file
) load-path
)
99 (load "cc-bytecomp" nil t
)))
101 (cc-require 'cc-defs
)
102 (cc-require 'cc-vars
)
103 (cc-require-when-compile 'cc-langs
)
104 (cc-require 'cc-engine
)
105 (cc-require 'cc-styles
)
106 (cc-require 'cc-cmds
)
107 (cc-require 'cc-align
)
108 (cc-require 'cc-menus
)
109 (cc-require 'cc-guess
)
111 ;; Silence the compiler.
112 (cc-bytecomp-defvar adaptive-fill-first-line-regexp
) ; Emacs
113 (cc-bytecomp-defun run-mode-hooks) ; Emacs 21.1
115 ;; We set this variable during mode init, yet we don't require
117 (cc-bytecomp-defvar font-lock-defaults
)
119 ;; Menu support for both XEmacs and Emacs. If you don't have easymenu
120 ;; with your version of Emacs, you are incompatible!
121 (cc-external-require 'easymenu
)
123 ;; Load cc-fonts first after font-lock is loaded, since it isn't
124 ;; necessary until font locking is requested.
125 ; (eval-after-load "font-lock" ; 2006-07-09: font-lock is now preloaded.
127 (require 'cc-fonts
) ;)
130 ;; Other modes and packages which depend on CC Mode should do the
131 ;; following to make sure everything is loaded and available for their
134 ;; (require 'cc-mode)
136 ;; And in the major mode function:
138 ;; (c-initialize-cc-mode t)
139 ;; (c-init-language-vars some-mode)
140 ;; (c-common-init 'some-mode) ; Or perhaps (c-basic-common-init 'some-mode)
142 ;; If you're not writing a derived mode using the language variable
143 ;; system, then some-mode is one of the language modes directly
144 ;; supported by CC Mode. You can then use (c-init-language-vars-for
145 ;; 'some-mode) instead of `c-init-language-vars'.
146 ;; `c-init-language-vars-for' is a function that avoids the rather
147 ;; large expansion of `c-init-language-vars'.
149 ;; If you use `c-basic-common-init' then you might want to call
150 ;; `c-font-lock-init' too to set up CC Mode's font lock support.
152 ;; See cc-langs.el for further info. A small example of a derived mode
153 ;; is also available at <http://cc-mode.sourceforge.net/
154 ;; derived-mode-ex.el>.
156 (defun c-leave-cc-mode-mode ()
157 (when c-buffer-is-cc-mode
160 (c-save-buffer-state ()
161 (c-clear-char-properties (point-min) (point-max) 'category
)
162 (c-clear-char-properties (point-min) (point-max) 'syntax-table
)
163 (c-clear-char-properties (point-min) (point-max) 'c-is-sws
)
164 (c-clear-char-properties (point-min) (point-max) 'c-in-sws
)
165 (c-clear-char-properties (point-min) (point-max) 'c-type
)
166 (if (c-major-mode-is 'awk-mode
)
167 (c-clear-char-properties (point-min) (point-max) 'c-awk-NL-prop
))))
168 (setq c-buffer-is-cc-mode nil
)))
170 (defun c-init-language-vars-for (mode)
171 "Initialize the language variables for one of the language modes
172 directly supported by CC Mode. This can be used instead of the
173 `c-init-language-vars' macro if the language you want to use is one of
174 those, rather than a derived language defined through the language
175 variable system (see \"cc-langs.el\")."
176 (cond ((eq mode
'c-mode
) (c-init-language-vars c-mode
))
177 ((eq mode
'c
++-mode
) (c-init-language-vars c
++-mode
))
178 ((eq mode
'objc-mode
) (c-init-language-vars objc-mode
))
179 ((eq mode
'java-mode
) (c-init-language-vars java-mode
))
180 ((eq mode
'idl-mode
) (c-init-language-vars idl-mode
))
181 ((eq mode
'pike-mode
) (c-init-language-vars pike-mode
))
182 ((eq mode
'awk-mode
) (c-init-language-vars awk-mode
))
183 (t (error "Unsupported mode %s" mode
))))
186 (defun c-initialize-cc-mode (&optional new-style-init
)
187 "Initialize CC Mode for use in the current buffer.
188 If the optional NEW-STYLE-INIT is nil or left out then all necessary
189 initialization to run CC Mode for the C language is done. Otherwise
190 only some basic setup is done, and a call to `c-init-language-vars' or
191 `c-init-language-vars-for' is necessary too (which gives more
192 control). See \"cc-mode.el\" for more info."
194 (setq c-buffer-is-cc-mode t
)
196 (let ((initprop 'cc-mode-is-initialized
)
198 (unless (get 'c-initialize-cc-mode initprop
)
201 (put 'c-initialize-cc-mode initprop t
)
202 (c-initialize-builtin-style)
203 (run-hooks 'c-initialization-hook
)
204 ;; Fix obsolete variables.
205 (if (boundp 'c-comment-continuation-stars
)
206 (setq c-block-comment-prefix c-comment-continuation-stars
))
207 (add-hook 'change-major-mode-hook
'c-leave-cc-mode-mode
)
208 (setq c-initialization-ok t
)
209 ;; Connect up with Emacs's electric-indent-mode, for >= Emacs 24.4
210 (when (fboundp 'electric-indent-local-mode
)
211 (add-hook 'electric-indent-mode-hook
'c-electric-indent-mode-hook
)
212 (add-hook 'electric-indent-local-mode-hook
213 'c-electric-indent-local-mode-hook
)))
214 ;; Will try initialization hooks again if they failed.
215 (put 'c-initialize-cc-mode initprop c-initialization-ok
))))
217 (unless new-style-init
218 (c-init-language-vars-for 'c-mode
)))
223 (defvar c-mode-base-map
()
224 "Keymap shared by all CC Mode related modes.")
226 (defun c-make-inherited-keymap ()
227 (let ((map (make-sparse-keymap)))
228 (c-set-keymap-parent map c-mode-base-map
)
231 (defun c-define-abbrev-table (name defs
&optional doc
)
232 ;; Compatibility wrapper for `define-abbrev' which passes a non-nil
233 ;; sixth argument for SYSTEM-FLAG in emacsen that support it
234 ;; (currently only Emacs >= 21.2).
235 (let ((table (or (and (boundp name
) (symbol-value name
))
236 (progn (condition-case nil
237 (define-abbrev-table name nil doc
)
238 (wrong-number-of-arguments ;E.g. Emacs<23.
239 (eval `(defvar ,name nil
,doc
))
240 (define-abbrev-table name nil
)))
241 (symbol-value name
)))))
244 (apply 'define-abbrev table
(append (car defs
) '(t)))
245 (wrong-number-of-arguments
246 (apply 'define-abbrev table
(car defs
))))
247 (setq defs
(cdr defs
)))))
248 (put 'c-define-abbrev-table
'lisp-indent-function
1)
250 (defun c-bind-special-erase-keys ()
251 ;; Only used in Emacs to bind C-c C-<delete> and C-c C-<backspace>
252 ;; to the proper keys depending on `normal-erase-is-backspace'.
253 (if normal-erase-is-backspace
255 (define-key c-mode-base-map
(kbd "C-c C-<delete>")
256 'c-hungry-delete-forward
)
257 (define-key c-mode-base-map
(kbd "C-c C-<backspace>")
258 'c-hungry-delete-backwards
))
259 (define-key c-mode-base-map
(kbd "C-c C-<delete>")
260 'c-hungry-delete-backwards
)
261 (define-key c-mode-base-map
(kbd "C-c C-<backspace>")
262 'c-hungry-delete-forward
)))
267 (setq c-mode-base-map
(make-sparse-keymap))
268 (when (boundp 'prog-mode-map
)
269 (c-set-keymap-parent c-mode-base-map prog-mode-map
))
271 ;; Separate M-BS from C-M-h. The former should remain
272 ;; backward-kill-word.
273 (define-key c-mode-base-map
[(control meta h
)] 'c-mark-function
)
274 (define-key c-mode-base-map
"\e\C-q" 'c-indent-exp
)
275 (substitute-key-definition 'backward-sentence
276 'c-beginning-of-statement
277 c-mode-base-map global-map
)
278 (substitute-key-definition 'forward-sentence
280 c-mode-base-map global-map
)
281 (substitute-key-definition 'indent-new-comment-line
282 'c-indent-new-comment-line
283 c-mode-base-map global-map
)
284 (substitute-key-definition 'indent-for-tab-command
285 ;; XXX Is this the right thing to do
287 'c-indent-line-or-region
288 c-mode-base-map global-map
)
289 (when (fboundp 'comment-indent-new-line
)
290 ;; indent-new-comment-line has changed name to
291 ;; comment-indent-new-line in Emacs 21.
292 (substitute-key-definition 'comment-indent-new-line
293 'c-indent-new-comment-line
294 c-mode-base-map global-map
))
296 ;; RMS says don't make these the default.
297 ;; (April 2006): RMS has now approved these commands as defaults.
298 (unless (memq 'argumentative-bod-function c-emacs-features
)
299 (define-key c-mode-base-map
"\e\C-a" 'c-beginning-of-defun
)
300 (define-key c-mode-base-map
"\e\C-e" 'c-end-of-defun
))
302 (define-key c-mode-base-map
"\C-c\C-n" 'c-forward-conditional
)
303 (define-key c-mode-base-map
"\C-c\C-p" 'c-backward-conditional
)
304 (define-key c-mode-base-map
"\C-c\C-u" 'c-up-conditional
)
306 ;; It doesn't suffice to put `c-fill-paragraph' on
307 ;; `fill-paragraph-function' since `c-fill-paragraph' must be called
308 ;; before any fill prefix adaption is done. E.g. `filladapt-mode'
309 ;; replaces `fill-paragraph' and does the adaption before calling
310 ;; `fill-paragraph-function', and we have to mask comments etc
311 ;; before that. Also, `c-fill-paragraph' chains on to
312 ;; `fill-paragraph' and the value on `fill-paragraph-function' to
313 ;; do the actual filling work.
314 (substitute-key-definition 'fill-paragraph
'c-fill-paragraph
315 c-mode-base-map global-map
)
316 ;; In XEmacs the default fill function is called
317 ;; fill-paragraph-or-region.
318 (substitute-key-definition 'fill-paragraph-or-region
'c-fill-paragraph
319 c-mode-base-map global-map
)
321 ;; We bind the forward deletion key and (implicitly) C-d to
322 ;; `c-electric-delete-forward', and the backward deletion key to
323 ;; `c-electric-backspace'. The hungry variants are bound to the
324 ;; same keys but prefixed with C-c. This implies that C-c C-d is
325 ;; `c-hungry-delete-forward'. For consistency, we bind not only C-c
326 ;; <backspace> to `c-hungry-delete-backwards' but also
327 ;; C-c C-<backspace>, so that the Ctrl key can be held down during
328 ;; the whole sequence regardless of the direction. This in turn
329 ;; implies that we bind C-c C-<delete> to `c-hungry-delete-forward',
330 ;; for the same reason.
332 ;; Bind the electric deletion functions to C-d and DEL. Emacs 21
333 ;; automatically maps the [delete] and [backspace] keys to these two
334 ;; depending on window system and user preferences. (In earlier
335 ;; versions it's possible to do the same by using `function-key-map'.)
336 (define-key c-mode-base-map
"\C-d" 'c-electric-delete-forward
)
337 (define-key c-mode-base-map
"\177" 'c-electric-backspace
)
338 (define-key c-mode-base-map
"\C-c\C-d" 'c-hungry-delete-forward
)
339 (define-key c-mode-base-map
[?\C-c ?\d
] 'c-hungry-delete-backwards
)
340 (define-key c-mode-base-map
[?\C-c ?\C-\d
] 'c-hungry-delete-backwards
)
341 (define-key c-mode-base-map
[?\C-c deletechar
] 'c-hungry-delete-forward
) ; C-c <delete> on a tty.
342 (define-key c-mode-base-map
[?\C-c
(control deletechar
)] ; C-c C-<delete> on a tty.
343 'c-hungry-delete-forward
)
344 (when (boundp 'normal-erase-is-backspace
)
345 ;; The automatic C-d and DEL mapping functionality doesn't extend
346 ;; to special combinations like C-c C-<delete>, so we have to hook
347 ;; into the `normal-erase-is-backspace' system to bind it directly
349 (add-hook 'normal-erase-is-backspace-hook
'c-bind-special-erase-keys
)
350 (c-bind-special-erase-keys))
352 (when (fboundp 'delete-forward-p
)
353 ;; In XEmacs we fix the forward and backward deletion behavior by
354 ;; binding the keysyms for the [delete] and [backspace] keys
355 ;; directly, and use `delete-forward-p' to decide what [delete]
356 ;; should do. That's done in the XEmacs specific
357 ;; `c-electric-delete' and `c-hungry-delete' functions.
358 (define-key c-mode-base-map
[delete] 'c-electric-delete)
359 (define-key c-mode-base-map [backspace] 'c-electric-backspace)
360 (define-key c-mode-base-map (kbd "C-c <delete>") 'c-hungry-delete)
361 (define-key c-mode-base-map (kbd "C-c C-<delete>") 'c-hungry-delete)
362 (define-key c-mode-base-map (kbd "C-c <backspace>")
363 'c-hungry-delete-backwards)
364 (define-key c-mode-base-map (kbd "C-c C-<backspace>")
365 'c-hungry-delete-backwards))
367 (define-key c-mode-base-map "#" 'c-electric-pound)
368 (define-key c-mode-base-map "{" 'c-electric-brace)
369 (define-key c-mode-base-map "}" 'c-electric-brace)
370 (define-key c-mode-base-map "/" 'c-electric-slash)
371 (define-key c-mode-base-map "*" 'c-electric-star)
372 (define-key c-mode-base-map ";" 'c-electric-semi&comma)
373 (define-key c-mode-base-map "," 'c-electric-semi&comma)
374 (define-key c-mode-base-map ":" 'c-electric-colon)
375 (define-key c-mode-base-map "(" 'c-electric-paren)
376 (define-key c-mode-base-map ")" 'c-electric-paren)
378 (define-key c-mode-base-map "\C-c\C-\\" 'c-backslash-region)
379 (define-key c-mode-base-map "\C-c\C-a" 'c-toggle-auto-newline)
380 (define-key c-mode-base-map "\C-c\C-b" 'c-submit-bug-report)
381 (define-key c-mode-base-map "\C-c\C-c" 'comment-region)
382 (define-key c-mode-base-map "\C-c\C-l" 'c-toggle-electric-state)
383 (define-key c-mode-base-map "\C-c\C-o" 'c-set-offset)
384 (define-key c-mode-base-map "\C-c\C-q" 'c-indent-defun)
385 (define-key c-mode-base-map "\C-c\C-s" 'c-show-syntactic-information)
386 ;; (define-key c-mode-base-map "\C-c\C-t" 'c-toggle-auto-hungry-state) Commented out by ACM, 2005-03-05.
387 (define-key c-mode-base-map "\C-c." 'c-set-style)
388 ;; conflicts with OOBR
389 ;;(define-key c-mode-base-map "\C-c\C-v" 'c-version)
390 ;; (define-key c-mode-base-map "\C-c\C-y" 'c-toggle-hungry-state) Commented out by ACM, 2005-11-22.
391 (define-key c-mode-base-map "\C-c\C-w" 'c-subword-mode)
392 (define-key c-mode-base-map "\C-c\C-k" 'c-toggle-comment-style))
394 ;; We don't require the outline package, but we configure it a bit anyway.
395 (cc-bytecomp-defvar outline-level)
397 (defun c-mode-menu (modestr)
398 "Return a menu spec suitable for `easy-menu-define' that is exactly
399 like the C mode menu except that the menu bar item name is MODESTR
402 This function is provided for compatibility only; derived modes should
403 preferably use the `c-mode-menu' language constant directly."
404 (cons modestr (c-lang-const c-mode-menu c)))
406 ;; Ugly hack to pull in the definition of `c-populate-syntax-table'
407 ;; from cc-langs to make it available at runtime. It's either this or
408 ;; moving the definition for it to cc-defs, but that would mean to
409 ;; break up the syntax table setup over two files.
410 (defalias 'c-populate-syntax-table
411 (cc-eval-when-compile
412 (let ((f (symbol-function 'c-populate-syntax-table)))
413 (if (byte-code-function-p f) f (byte-compile f)))))
415 ;; CAUTION: Try to avoid installing things on
416 ;; `before-change-functions'. The macro `combine-after-change-calls'
417 ;; is used and it doesn't work if there are things on that hook. That
418 ;; can cause font lock functions to run in inconvenient places during
419 ;; temporary changes in some font lock support modes, causing extra
420 ;; unnecessary work and font lock glitches due to interactions between
421 ;; various text properties.
423 ;; (2007-02-12): The macro `combine-after-change-calls' ISN'T used any
426 (defun c-unfind-enclosing-token (pos)
427 ;; If POS is wholly inside a token, remove that id from
428 ;; `c-found-types', should it be present. Return t if we were in an
431 (let ((tok-beg (progn (goto-char pos)
432 (and (c-beginning-of-current-token) (point))))
433 (tok-end (progn (goto-char pos)
434 (and (c-end-of-current-token) (point)))))
435 (when (and tok-beg tok-end)
436 (c-unfind-type (buffer-substring-no-properties tok-beg tok-end))
439 (defun c-unfind-coalesced-tokens (beg end)
440 ;; If removing the region (beg end) would coalesce an identifier ending at
441 ;; beg with an identifier (fragment) beginning at end, or an identifier
442 ;; fragment ending at beg with an identifier beginning at end, remove the
443 ;; pertinent identifier(s) from `c-found-types'.
449 (progn (c-backward-syntactic-ws) (eq (point) beg))
450 (/= (skip-chars-backward c-symbol-chars (1- (point))) 0)
451 (progn (goto-char beg) (c-forward-syntactic-ws) (<= (point) end))
454 (looking-at c-symbol-char-key))
456 (c-simple-skip-symbol-backward)
457 (c-unfind-type (buffer-substring-no-properties (point) beg)))
462 (progn (c-forward-syntactic-ws) (eq (point) end))
463 (looking-at c-symbol-char-key)
464 (progn (c-backward-syntactic-ws) (>= (point) beg))
466 (/= (skip-chars-backward c-symbol-chars (1- (point))) 0))
468 (c-end-of-current-token)
469 (c-unfind-type (buffer-substring-no-properties end (point)))))))
471 ;; c-maybe-stale-found-type records a place near the region being
472 ;; changed where an element of `found-types' might become stale. It
473 ;; is set in c-before-change and is either nil, or has the form:
475 ;; (c-decl-id-start "foo" 97 107 " (* ooka) " "o"), where
477 ;; o - `c-decl-id-start' is the c-type text property value at buffer
480 ;; o - 97 107 is the region potentially containing the stale type -
481 ;; this is delimited by a non-nil c-type text property at 96 and
482 ;; either another one or a ";", "{", or "}" at 107.
484 ;; o - " (* ooka) " is the (before change) buffer portion containing
485 ;; the suspect type (here "ooka").
487 ;; o - "o" is the buffer contents which is about to be deleted. This
488 ;; would be the empty string for an insertion.
489 (defvar c-maybe-stale-found-type nil)
490 (make-variable-buffer-local 'c-maybe-stale-found-type)
492 (defvar c-just-done-before-change nil)
493 (make-variable-buffer-local 'c-just-done-before-change)
494 ;; This variable is set to t by `c-before-change' and to nil by
495 ;; `c-after-change'. It is used for two purposes: (i) to detect a spurious
496 ;; invocation of `before-change-functions' directly following on from a
497 ;; correct one. This happens in some Emacsen, for example when
498 ;; `basic-save-buffer' does (insert ?\n) when `require-final-newline' is
499 ;; non-nil; (ii) to detect when Emacs fails to invoke
500 ;; `before-change-functions'. This can happen when reverting a buffer - see
501 ;; bug #24094. It seems these failures happen only in GNU Emacs; XEmacs
502 ;; seems to maintain the strict alternation of calls to
503 ;; `before-change-functions' and `after-change-functions'.
505 (defun c-basic-common-init (mode default-style)
506 "Do the necessary initialization for the syntax handling routines
507 and the line breaking/filling code. Intended to be used by other
508 packages that embed CC Mode.
510 MODE is the CC Mode flavor to set up, e.g. `c-mode' or `java-mode'.
511 DEFAULT-STYLE tells which indentation style to install. It has the
512 same format as `c-default-style'.
514 Note that `c-init-language-vars' must be called before this function.
515 This function cannot do that since `c-init-language-vars' is a macro
516 that requires a literal mode spec at compile time."
518 (setq c-buffer-is-cc-mode mode)
520 ;; these variables should always be buffer local; they do not affect
521 ;; indentation style.
522 (make-local-variable 'comment-start)
523 (make-local-variable 'comment-end)
524 (make-local-variable 'comment-start-skip)
526 (make-local-variable 'paragraph-start)
527 (make-local-variable 'paragraph-separate)
528 (make-local-variable 'paragraph-ignore-fill-prefix)
529 (make-local-variable 'adaptive-fill-mode)
530 (make-local-variable 'adaptive-fill-regexp)
531 (make-local-variable 'fill-paragraph-handle-comment)
533 ;; now set their values
534 (set (make-local-variable 'parse-sexp-ignore-comments) t)
535 (set (make-local-variable 'indent-line-function) 'c-indent-line)
536 (set (make-local-variable 'indent-region-function) 'c-indent-region)
537 (set (make-local-variable 'normal-auto-fill-function) 'c-do-auto-fill)
538 (set (make-local-variable 'comment-multi-line) t)
539 (set (make-local-variable 'comment-line-break-function)
540 'c-indent-new-comment-line)
542 ;; Prevent time-wasting activity on C-y.
543 (when (boundp 'yank-handled-properties)
544 (make-local-variable 'yank-handled-properties)
545 (let ((yank-cat-handler (assq 'category yank-handled-properties)))
546 (when yank-cat-handler
547 (setq yank-handled-properties (remq yank-cat-handler
548 yank-handled-properties)))))
550 ;; For the benefit of adaptive fill, which otherwise mis-fills.
551 (setq fill-paragraph-handle-comment nil)
553 ;; Install `c-fill-paragraph' on `fill-paragraph-function' so that a
554 ;; direct call to `fill-paragraph' behaves better. This still
555 ;; doesn't work with filladapt but it's better than nothing.
556 (set (make-local-variable 'fill-paragraph-function) 'c-fill-paragraph)
558 ;; Initialize the cache of brace pairs, and opening braces/brackets/parens.
560 ;; Initialize the "brace stack" cache.
563 (when (or c-recognize-<>-arglists
564 (c-major-mode-is 'awk-mode)
565 (c-major-mode-is '(java-mode c-mode c++-mode objc-mode)))
566 ;; We'll use the syntax-table text property to change the syntax
567 ;; of some chars for this language, so do the necessary setup for
570 ;; Note to other package developers: It's ok to turn this on in CC
571 ;; Mode buffers when CC Mode doesn't, but it's not ok to turn it
572 ;; off if CC Mode has turned it on.
575 (when (boundp 'parse-sexp-lookup-properties)
576 (set (make-local-variable 'parse-sexp-lookup-properties) t))
578 ;; Same as above for XEmacs.
579 (when (boundp 'lookup-syntax-properties)
580 (set (make-local-variable 'lookup-syntax-properties) t)))
582 ;; Use this in Emacs 21+ to avoid meddling with the rear-nonsticky
583 ;; property on each character.
584 (when (boundp 'text-property-default-nonsticky)
585 (make-local-variable 'text-property-default-nonsticky)
586 (mapc (lambda (tprop)
587 (unless (assq tprop text-property-default-nonsticky)
588 (setq text-property-default-nonsticky
589 (cons `(,tprop . t) text-property-default-nonsticky))))
590 '(syntax-table category c-type)))
592 ;; In Emacs 21 and later it's possible to turn off the ad-hoc
593 ;; heuristic that open parens in column 0 are defun starters. Since
594 ;; we have c-state-cache, that heuristic isn't useful and only causes
595 ;; trouble, so turn it off.
596 ;; 2006/12/17: This facility is somewhat confused, and doesn't really seem
597 ;; helpful. Comment it out for now.
598 ;; (when (memq 'col-0-paren c-emacs-features)
599 ;; (make-local-variable 'open-paren-in-column-0-is-defun-start)
600 ;; (setq open-paren-in-column-0-is-defun-start nil))
602 (c-clear-found-types)
604 ;; now set the mode style based on default-style
605 (let ((style (cc-choose-style-for-mode mode default-style)))
606 ;; Override style variables if `c-old-style-variable-behavior' is
607 ;; set. Also override if we are using global style variables,
608 ;; have already initialized a style once, and are switching to a
609 ;; different style. (It's doubtful whether this is desirable, but
610 ;; the whole situation with nonlocal style variables is a bit
611 ;; awkward. It's at least the most compatible way with the old
612 ;; style init procedure.)
613 (c-set-style style (not (or c-old-style-variable-behavior
614 (and (not c-style-variables-are-local-p)
616 (not (string-equal c-indentation-style
618 (c-setup-paragraph-variables)
620 ;; we have to do something special for c-offsets-alist so that the
621 ;; buffer local value has its own alist structure.
622 (setq c-offsets-alist (copy-alist c-offsets-alist))
624 ;; setup the comment indent variable in a Emacs version portable way
625 (set (make-local-variable 'comment-indent-function) 'c-comment-indent)
626 ;; What sort of comments are default for M-;?
627 (setq c-block-comment-flag c-block-comment-is-default)
629 ;; In Emacs 24.4 onwards, prevent Emacs's built in electric indentation from
630 ;; messing up CC Mode's, and set `c-electric-flag' if `electric-indent-mode'
631 ;; has been called by the user.
632 (when (boundp 'electric-indent-inhibit) (setq electric-indent-inhibit t))
633 ;; CC-mode should obey Emacs's generic preferences, tho only do it if
634 ;; Emacs's generic preferences can be set per-buffer (Emacs>=24.4).
635 (when (fboundp 'electric-indent-local-mode)
636 (setq c-electric-flag electric-indent-mode))
638 ;; ;; Put submode indicators onto minor-mode-alist, but only once.
639 ;; (or (assq 'c-submode-indicators minor-mode-alist)
640 ;; (setq minor-mode-alist
641 ;; (cons '(c-submode-indicators c-submode-indicators)
642 ;; minor-mode-alist)))
645 ;; Install the functions that ensure that various internal caches
646 ;; don't become invalid due to buffer changes.
647 (when (featurep 'xemacs)
648 (make-local-hook 'before-change-functions)
649 (make-local-hook 'after-change-functions))
650 (add-hook 'before-change-functions 'c-before-change nil t)
651 (setq c-just-done-before-change nil)
652 (add-hook 'after-change-functions 'c-after-change nil t)
653 (when (boundp 'font-lock-extend-after-change-region-function)
654 (set (make-local-variable 'font-lock-extend-after-change-region-function)
655 'c-extend-after-change-region))) ; Currently (2009-05) used by all
656 ; languages with #define (C, C++,; ObjC), and by AWK.
658 (defun c-setup-doc-comment-style ()
659 "Initialize the variables that depend on the value of `c-doc-comment-style'."
660 (when (and (featurep 'font-lock)
661 (symbol-value 'font-lock-mode))
662 ;; Force font lock mode to reinitialize itself.
666 ;; Buffer local variables defining the region to be fontified by a font lock
667 ;; after-change function. They are initialized in c-before-change to
668 ;; before-change-functions' BEG and END. `c-new-END' is amended in
669 ;; c-after-change with after-change-functions' BEG, END, and OLD-LEN. These
670 ;; variables may be modified by any before/after-change function, in
671 ;; particular by functions in `c-get-state-before-change-functions' and
672 ;; `c-before-font-lock-functions'.
674 (make-variable-buffer-local 'c-new-BEG)
676 (make-variable-buffer-local 'c-new-END)
677 ;; The following two variables record the values of `c-new-BEG' and
678 ;; `c-new-END' just after `c-new-END' has been adjusted for the length of text
679 ;; inserted or removed. They may be read by any after-change function (but
680 ;; should not be altered by one).
682 (make-variable-buffer-local 'c-old-BEG)
684 (make-variable-buffer-local 'c-old-END)
686 (defun c-common-init (&optional mode)
687 "Common initialization for all CC Mode modes.
688 In addition to the work done by `c-basic-common-init' and
689 `c-font-lock-init', this function sets up various other things as
690 customary in CC Mode modes but which aren't strictly necessary for CC
691 Mode to operate correctly.
693 MODE is the symbol for the mode to initialize, like `c-mode'. See
694 `c-basic-common-init' for details. It's only optional to be
695 compatible with old code; callers should always specify it."
698 ;; Called from an old third party package. The fallback is to
700 (c-init-language-vars-for 'c-mode))
702 (c-basic-common-init mode c-default-style)
704 ;; Only initialize font locking if we aren't called from an old package.
707 ;; Starting a mode is a sort of "change". So call the change functions...
710 (setq c-new-BEG (point-min))
711 (setq c-new-END (point-max))
713 (let (before-change-functions after-change-functions)
715 (funcall fn (point-min) (point-max)))
716 c-get-state-before-change-functions)
718 (funcall fn (point-min) (point-max)
719 (- (point-max) (point-min))))
720 c-before-font-lock-functions))))
722 (set (make-local-variable 'outline-regexp) "[^#\n\^M]")
723 (set (make-local-variable 'outline-level) 'c-outline-level)
724 (set (make-local-variable 'add-log-current-defun-function)
726 (or (c-cpp-define-name) (c-defun-name))))
727 (let ((rfn (assq mode c-require-final-newline)))
729 (if (boundp 'mode-require-final-newline)
731 (set (make-local-variable 'require-final-newline)
732 mode-require-final-newline))
733 (set (make-local-variable 'require-final-newline) (cdr rfn))))))
735 (defun c-count-cfss (lv-alist)
736 ;; LV-ALIST is an alist like `file-local-variables-alist'. Count how many
737 ;; elements with the key `c-file-style' there are in it.
738 (let ((elt-ptr lv-alist) elt (cownt 0))
740 (setq elt (car elt-ptr)
741 elt-ptr (cdr elt-ptr))
742 (when (eq (car elt) 'c-file-style)
743 (setq cownt (1+ cownt))))
746 (defun c-before-hack-hook ()
747 "Set the CC Mode style and \"offsets\" when in the buffer's local variables.
748 They are set only when, respectively, the pseudo variables
749 `c-file-style' and `c-file-offsets' are present in the list.
751 This function is called from the hook `before-hack-local-variables-hook'."
752 (when c-buffer-is-cc-mode
753 (let ((mode-cons (assq 'mode file-local-variables-alist))
754 (stile (cdr (assq 'c-file-style file-local-variables-alist)))
755 (offsets (cdr (assq 'c-file-offsets file-local-variables-alist))))
757 (hack-one-local-variable (car mode-cons) (cdr mode-cons))
758 (setq file-local-variables-alist
759 (delq mode-cons file-local-variables-alist)))
761 (or (stringp stile) (error "c-file-style is not a string"))
762 (if (boundp 'dir-local-variables-alist)
763 ;; Determine whether `c-file-style' was set in the file's local
764 ;; variables or in a .dir-locals.el (a directory setting).
765 (let ((cfs-in-file-and-dir-count
766 (c-count-cfss file-local-variables-alist))
767 (cfs-in-dir-count (c-count-cfss dir-local-variables-alist)))
769 (and (= cfs-in-file-and-dir-count cfs-in-dir-count)
771 (c-set-style stile)))
775 (let ((langelem (car langentry))
776 (offset (cdr langentry)))
777 (c-set-offset langelem offset)))
780 (defun c-remove-any-local-eval-or-mode-variables ()
781 ;; If the buffer specifies `mode' or `eval' in its File Local Variable list
782 ;; or on the first line, remove all occurrences. See
783 ;; `c-postprocess-file-styles' for justification. There is no need to save
784 ;; point here, or even bother too much about the buffer contents. However,
785 ;; DON'T mess up the kill-ring.
787 ;; Most of the code here is derived from Emacs 21.3's `hack-local-variables'
789 (goto-char (point-max))
790 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
791 (let (lv-point (prefix "") (suffix ""))
792 (when (let ((case-fold-search t))
793 (search-forward "Local Variables:" nil t))
794 (setq lv-point (point))
795 ;; The prefix is what comes before "local variables:" in its line.
796 ;; The suffix is what comes after "local variables:" in its line.
797 (skip-chars-forward " \t")
799 (setq suffix (buffer-substring (point)
800 (progn (end-of-line) (point)))))
801 (goto-char (match-beginning 0))
804 (buffer-substring (point)
805 (progn (beginning-of-line) (point)))))
807 (while (search-forward-regexp
809 (regexp-quote prefix)
810 "\\(mode\\|eval\\):.*"
811 (regexp-quote suffix)
815 (delete-region (point) (progn (forward-line) (point)))))
817 ;; Delete the first line, if we've got one, in case it contains a mode spec.
818 (unless (and lv-point
819 (progn (goto-char lv-point)
822 (goto-char (point-min))
824 (delete-region (point) (progn (forward-line) (point)))))))
826 (defun c-postprocess-file-styles ()
827 "Function that post processes relevant file local variables in CC Mode.
828 Currently, this function simply applies any style and offset settings
829 found in the file's Local Variable list. It first applies any style
830 setting found in `c-file-style', then it applies any offset settings
831 it finds in `c-file-offsets'.
833 Note that the style variables are always made local to the buffer."
835 ;; apply file styles and offsets
836 (when c-buffer-is-cc-mode
837 (if (or c-file-style c-file-offsets)
838 (c-make-styles-buffer-local t))
840 (or (stringp c-file-style)
841 (error "c-file-style is not a string"))
842 (c-set-style c-file-style))
847 (let ((langelem (car langentry))
848 (offset (cdr langentry)))
849 (c-set-offset langelem offset)))
851 ;; Problem: The file local variable block might have explicitly set a
852 ;; style variable. The `c-set-style' or `mapcar' call might have
853 ;; overwritten this. So we run `hack-local-variables' again to remedy
854 ;; this. There are no guarantees this will work properly, particularly as
855 ;; we have no control over what the other hook functions on
856 ;; `hack-local-variables-hook' would have done. We now (2006/2/1) remove
857 ;; any `eval' or `mode' expressions before we evaluate again (see below).
860 ;; Problem (bug reported by Gustav Broberg): if one of the variables is
861 ;; `mode', this will invoke c-mode (etc.) again, setting up the style etc.
862 ;; We prevent this by temporarily removing `mode' from the Local Variables
864 (if (or c-file-style c-file-offsets)
865 (let ((hack-local-variables-hook nil) (inhibit-read-only t))
866 (c-tentative-buffer-changes
867 (c-remove-any-local-eval-or-mode-variables)
868 (hack-local-variables))
871 (if (boundp 'before-hack-local-variables-hook)
872 (add-hook 'before-hack-local-variables-hook 'c-before-hack-hook)
873 (add-hook 'hack-local-variables-hook 'c-postprocess-file-styles))
875 (defmacro c-run-mode-hooks (&rest hooks)
876 ;; Emacs 21.1 has introduced a system with delayed mode hooks that
877 ;; requires the use of the new function `run-mode-hooks'.
878 (if (cc-bytecomp-fboundp 'run-mode-hooks)
879 `(run-mode-hooks ,@hooks)
880 `(progn ,@(mapcar (lambda (hook) `(run-hooks ,hook)) hooks))))
883 ;;; Change hooks, linking with Font Lock and electric-indent-mode.
885 (defun c-called-from-text-property-change-p ()
886 ;; Is the primitive which invoked `before-change-functions' or
887 ;; `after-change-functions' one which merely changes text properties? This
888 ;; function must be called directly from a member of one of the above hooks.
890 ;; In the following call, frame 0 is `backtrace-frame', frame 1 is
891 ;; `c-called-from-text-property-change-p', frame 2 is
892 ;; `c-before/after-change', frame 3 is the primitive invoking the change
894 (memq (cadr (backtrace-frame 3))
895 '(put-text-property remove-list-of-text-properties)))
897 (defun c-depropertize-CPP (beg end)
898 ;; Remove the punctuation syntax-table text property from the CPP parts of
899 ;; (c-new-BEG c-new-END).
901 ;; This function is in the C/C++/ObjC values of
902 ;; `c-get-state-before-change-functions' and is called exclusively as a
903 ;; before change function.
904 (c-save-buffer-state (m-beg ss-found)
905 (goto-char c-new-BEG)
906 (while (and (< (point) beg)
907 (search-forward-regexp c-anchored-cpp-prefix beg 'bound))
908 (goto-char (match-beginning 1))
911 (c-clear-char-property-with-value m-beg (point) 'syntax-table '(1)))
913 (while (and (< (point) end)
915 (search-forward-regexp c-anchored-cpp-prefix end 'bound)))
916 (goto-char (match-beginning 1))
919 (if (and ss-found (> (point) end))
920 (c-clear-char-property-with-value m-beg (point) 'syntax-table '(1)))
922 (while (and (< (point) c-new-END)
923 (search-forward-regexp c-anchored-cpp-prefix c-new-END 'bound))
924 (goto-char (match-beginning 1))
927 (c-clear-char-property-with-value
928 m-beg (point) 'syntax-table '(1)))))
930 (defun c-extend-region-for-CPP (_beg _end)
931 ;; Adjust `c-new-BEG', `c-new-END' respectively to the beginning and end of
932 ;; any preprocessor construct they may be in.
934 ;; Point is undefined both before and after this function call; the buffer
935 ;; has already been widened, and match-data saved. The return value is
938 ;; This function is in the C/C++/ObjC values of
939 ;; `c-get-state-before-change-functions' and is called exclusively as a
940 ;; before change function.
941 (goto-char c-new-BEG)
942 (c-beginning-of-macro)
943 (when (< (point) c-new-BEG)
944 (setq c-new-BEG (max (point) (c-determine-limit 500 c-new-BEG))))
946 (goto-char c-new-END)
947 (when (c-beginning-of-macro)
949 (or (eobp) (forward-char))) ; Over the terminating NL which may be marked
950 ; with a c-cpp-delimiter category property
951 (when (> (point) c-new-END)
952 (setq c-new-END (min (point) (c-determine-+ve-limit 500 c-new-END)))))
954 (defun c-depropertize-new-text (beg end _old-len)
955 ;; Remove from the new text in (BEG END) any and all text properties which
956 ;; might interfere with CC Mode's proper working.
958 ;; This function is called exclusively as an after-change function. It
959 ;; appears in the value (for all languages) of
960 ;; `c-before-font-lock-functions'. The value of point is undefined both on
961 ;; entry and exit, and the return value has no significance. The parameters
962 ;; BEG, END, and OLD-LEN are the standard ones supplied to all after-change
964 (c-save-buffer-state ()
966 (c-clear-char-properties beg end 'syntax-table)
967 (c-clear-char-properties beg end 'category)
968 (c-clear-char-properties beg end 'c-is-sws)
969 (c-clear-char-properties beg end 'c-in-sws)
970 (c-clear-char-properties beg end 'c-type)
971 (c-clear-char-properties beg end 'c-awk-NL-prop))))
973 (defun c-extend-font-lock-region-for-macros (_begg endd _old-len)
974 ;; Extend the region (c-new-BEG c-new-END) to cover all (possibly changed)
975 ;; preprocessor macros; The return value has no significance.
977 ;; Point is undefined on both entry and exit to this function. The buffer
978 ;; will have been widened on entry.
980 ;; c-new-BEG has already been extended in `c-extend-region-for-CPP' so we
981 ;; don't need to repeat the exercise here.
983 ;; This function is in the C/C++/ObjC value of `c-before-font-lock-functions'.
985 (when (c-beginning-of-macro)
987 ;; Determine the region, (c-new-BEG c-new-END), which will get font
988 ;; locked. This restricts the region should there be long macros.
989 (setq c-new-END (min (max c-new-END (point))
990 (c-determine-+ve-limit 500 c-new-END)))))
992 (defun c-neutralize-CPP-line (beg end)
993 ;; BEG and END bound a region, typically a preprocessor line. Put a
994 ;; "punctuation" syntax-table property on syntactically obtrusive
995 ;; characters, ones which would interact syntactically with stuff outside
998 ;; These are unmatched string delimiters, or unmatched
999 ;; parens/brackets/braces. An unclosed comment is regarded as valid, NOT
1005 (setq s (parse-partial-sexp beg end -1))
1007 ((< (nth 0 s) 0) ; found an unmated ),},]
1008 (c-put-char-property (1- (point)) 'syntax-table '(1))
1010 ((nth 3 s) ; In a string
1011 (c-put-char-property (nth 8 s) 'syntax-table '(1))
1013 ((> (nth 0 s) 0) ; In a (,{,[
1014 (c-put-char-property (nth 1 s) 'syntax-table '(1))
1018 (defun c-neutralize-syntax-in-and-mark-CPP (_begg _endd _old-len)
1019 ;; (i) "Neutralize" every preprocessor line wholly or partially in the
1020 ;; changed region. "Restore" lines which were CPP lines before the change
1021 ;; and are no longer so.
1023 ;; (ii) Mark each CPP construct by placing a `category' property value
1024 ;; `c-cpp-delimiter' at its start and end. The marked characters are the
1025 ;; opening # and usually the terminating EOL, but sometimes the character
1026 ;; before a comment delimiter.
1028 ;; That is, set syntax-table properties on characters that would otherwise
1029 ;; interact syntactically with those outside the CPP line(s).
1031 ;; This function is called from an after-change function, BEGG ENDD and
1032 ;; OLD-LEN being the standard parameters. It prepares the buffer for font
1033 ;; locking, hence must get called before `font-lock-after-change-function'.
1035 ;; Point is undefined both before and after this function call, the buffer
1036 ;; has been widened, and match-data saved. The return value is ignored.
1038 ;; This function is in the C/C++/ObjC value of `c-before-font-lock-functions'.
1040 ;; Note: SPEED _MATTERS_ IN THIS FUNCTION!!!
1042 ;; This function might make hidden buffer changes.
1043 (c-save-buffer-state (limits)
1044 ;; Clear 'syntax-table properties "punctuation":
1045 ;; (c-clear-char-property-with-value c-new-BEG c-new-END 'syntax-table '(1))
1047 ;; CPP "comment" markers:
1048 (if (eval-when-compile (memq 'category-properties c-emacs-features));Emacs.
1049 (c-clear-char-property-with-value
1050 c-new-BEG c-new-END 'category 'c-cpp-delimiter))
1051 ;; FIXME!!! What about the "<" and ">" category properties? 2009-11-16
1053 ;; Add needed properties to each CPP construct in the region.
1054 (goto-char c-new-BEG)
1055 (if (setq limits (c-literal-limits)) ; Go past any literal.
1056 (goto-char (cdr limits)))
1057 (skip-chars-backward " \t")
1058 (let ((pps-position (point)) pps-state mbeg)
1059 (while (and (< (point) c-new-END)
1060 (search-forward-regexp c-anchored-cpp-prefix c-new-END t))
1061 ;; If we've found a "#" inside a macro/string/comment, ignore it.
1064 (goto-char (match-beginning 0))
1065 (let ((here (point)))
1066 (and (save-match-data (c-beginning-of-macro))
1070 (parse-partial-sexp pps-position (point) nil nil pps-state)
1071 pps-position (point))
1072 (or (nth 3 pps-state) ; in a string?
1073 (and (nth 4 pps-state)
1074 (not (eq (nth 7 pps-state) 'syntax-table)))))) ; in a comment?
1075 (goto-char (match-beginning 1))
1077 (if (> (c-no-comment-end-of-macro) mbeg)
1079 (c-neutralize-CPP-line mbeg (point)) ; "punctuation" properties
1080 (if (eval-when-compile
1081 (memq 'category-properties c-emacs-features)) ;Emacs.
1082 (c-set-cpp-delimiters mbeg (point)))) ; "comment" markers
1083 (forward-line)) ; no infinite loop with, e.g., "#//"
1086 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1087 ;; Parsing of quotes.
1089 ;; Valid digit separators in numbers will get the syntax-table "punctuation"
1090 ;; property, '(1), and also the text property `c-digit-separator' value t.
1092 ;; Invalid other quotes (i.e. those not validly bounding a single character,
1093 ;; or escaped character) will get the syntax-table "punctuation" property,
1096 ;; Note that, for convenience, these properties are applied even inside
1097 ;; comments and strings.
1099 (defconst c-maybe-quoted-number-head
1102 "\\([Xx]\\([0-9a-fA-F]\\('[0-9a-fA-F]\\|[0-9a-fA-F]\\)*'?\\)?\\)"
1104 "\\([Bb]\\([01]\\('[01]\\|[01]\\)*'?\\)?\\)"
1106 "\\('[0-7]\\|[0-7]\\)*'?"
1109 "[1-9]\\('[0-9]\\|[0-9]\\)*'?"
1111 "Regexp matching the head of a numeric literal, including with digit separators.")
1113 (defun c-quoted-number-head-before-point ()
1114 ;; Return non-nil when the head of a possibly quoted number is found
1115 ;; immediately before point. The value returned in this case is the buffer
1116 ;; position of the start of the head. That position is also in
1117 ;; (match-beginning 0).
1118 (when c-has-quoted-numbers
1120 (let ((here (point))
1122 (skip-chars-backward "0-9a-fA-F'")
1123 (if (and (memq (char-before) '(?x ?X))
1124 (eq (char-before (1- (point))) ?0))
1129 (search-forward-regexp c-maybe-quoted-number-head here t))
1131 (and (eq found here) (match-beginning 0))))))
1133 (defconst c-maybe-quoted-number-tail
1136 "\\([xX']?[0-9a-fA-F]\\('[0-9a-fA-F]\\|[0-9a-fA-F]\\)*\\)"
1138 "\\([bB']?[01]\\('[01]\\|[01]\\)*\\)"
1140 "\\('?[0-9]\\('[0-9]\\|[0-9]\\)*\\)"
1142 "Regexp matching the tail of a numeric literal, including with digit separators.
1143 Note that this is a strict tail, so won't match, e.g. \"0x....\".")
1145 (defun c-quoted-number-tail-after-point ()
1146 ;; Return non-nil when a proper tail of a possibly quoted number is found
1147 ;; immediately after point. The value returned in this case is the buffer
1148 ;; position of the end of the tail. That position is also in (match-end 0).
1149 (when c-has-quoted-numbers
1150 (and (looking-at c-maybe-quoted-number-tail)
1153 (defconst c-maybe-quoted-number
1156 "\\([Xx][0-9a-fA-F]\\('[0-9a-fA-F]\\|[0-9a-fA-F]\\)*\\)"
1158 "\\([Bb][01]\\('[01]\\|[01]\\)*\\)"
1160 "\\('[0-7]\\|[0-7]\\)*"
1163 "[1-9]\\('[0-9]\\|[0-9]\\)*"
1165 "Regexp matching a numeric literal, including with digit separators.")
1167 (defun c-quoted-number-straddling-point ()
1168 ;; Return non-nil if a definitely quoted number starts before point and ends
1169 ;; after point. In this case the number is bounded by (match-beginning 0)
1170 ;; and (match-end 0).
1171 (when c-has-quoted-numbers
1173 (let ((here (point))
1174 (bound (progn (skip-chars-forward "0-9a-fA-F'") (point))))
1176 (when (< (skip-chars-backward "0-9a-fA-F'") 0)
1177 (if (and (memq (char-before) '(?x ?X))
1178 (eq (char-before (1- (point))) ?0))
1180 (while (and (search-forward-regexp c-maybe-quoted-number bound t)
1181 (<= (match-end 0) here)))
1182 (and (< (match-beginning 0) here)
1183 (> (match-end 0) here)
1185 (goto-char (match-beginning 0))
1186 (save-excursion (search-forward "'" (match-end 0) t)))))))))
1188 (defun c-parse-quotes-before-change (beg end)
1189 ;; This function analyzes 's near the region (c-new-BEG c-new-END), amending
1190 ;; those two variables as needed to include 's into that region when they
1191 ;; might be syntactically relevant to the change in progress.
1193 ;; Having amended that region, the function removes pertinent text
1194 ;; properties (syntax-table properties with value '(1) and c-digit-separator
1195 ;; props with value t) from 's in it. This operation is performed even
1196 ;; within strings and comments.
1198 ;; This function is called exclusively as a before-change function via the
1199 ;; variable `c-get-state-before-change-functions'.
1200 (c-save-buffer-state ()
1201 (goto-char c-new-BEG)
1202 ;; We need to scan for 's from the BO (logical) line.
1204 (while (eq (char-before (1- (point))) ?\\)
1205 (beginning-of-line 0))
1206 (while (and (< (point) c-new-BEG)
1207 (search-forward "'" c-new-BEG t))
1209 ((c-quoted-number-straddling-point)
1210 (goto-char (match-end 0))
1211 (if (> (match-end 0) c-new-BEG)
1212 (setq c-new-BEG (match-beginning 0))))
1213 ((c-quoted-number-head-before-point)
1214 (if (>= (point) c-new-BEG)
1215 (setq c-new-BEG (match-beginning 0))))
1216 ((looking-at "\\([^'\\]\\|\\\\.\\)'")
1217 (goto-char (match-end 0))
1218 (if (> (match-end 0) c-new-BEG)
1219 (setq c-new-BEG (1- (match-beginning 0)))))
1220 ((or (>= (point) (1- c-new-BEG))
1221 (and (eq (point) (- c-new-BEG 2))
1222 (eq (char-after) ?\\)))
1223 (setq c-new-BEG (1- (point))))
1226 (goto-char c-new-END)
1227 ;; We will scan from the BO (logical) line.
1229 (while (eq (char-before (1- (point))) ?\\)
1230 (beginning-of-line 0))
1231 (while (and (< (point) c-new-END)
1232 (search-forward "'" c-new-END t))
1234 ((c-quoted-number-straddling-point)
1235 (goto-char (match-end 0))
1236 (if (> (match-end 0) c-new-END)
1237 (setq c-new-END (match-end 0))))
1238 ((c-quoted-number-tail-after-point)
1239 (goto-char (match-end 0))
1240 (if (> (match-end 0) c-new-END)
1241 (setq c-new-END (match-end 0))))
1242 ((looking-at "\\([^'\\]\\|\\\\.\\)'")
1243 (goto-char (match-end 0))
1244 (if (> (match-end 0) c-new-END)
1245 (setq c-new-END (match-end 0))))
1247 ;; Having reached c-new-END, handle any 's after it whose context may be
1248 ;; changed by the current buffer change.
1249 (goto-char c-new-END)
1251 ((c-quoted-number-tail-after-point)
1252 (setq c-new-END (match-end 0)))
1254 "\\(\\\\.\\|.\\)?\\('\\([^'\\]\\|\\\\.\\)\\)*'")
1255 (setq c-new-END (match-end 0))))
1257 ;; Remove the '(1) syntax-table property from any "'"s within (c-new-BEG
1259 (goto-char c-new-BEG)
1260 (when (c-search-forward-char-property-with-value-on-char
1261 'syntax-table '(1) ?\' c-new-END)
1262 (c-invalidate-state-cache (1- (point)))
1263 (c-truncate-semi-nonlit-pos-cache (1- (point)))
1264 (c-clear-char-property-with-value-on-char
1265 (1- (point)) c-new-END
1268 ;; Remove the c-digit-separator text property from the same "'"s.
1269 (when c-has-quoted-numbers
1270 (c-clear-char-property-with-value-on-char
1271 (1- (point)) c-new-END
1272 'c-digit-separator t
1275 (defun c-parse-quotes-after-change (beg end old-len)
1276 ;; This function applies syntax-table properties (value '(1)) and
1277 ;; c-digit-separator properties as needed to 's within the range (c-new-BEG
1278 ;; c-new-END). This operation is performed even within strings and
1281 ;; This function is called exclusively as an after-change function via the
1282 ;; variable `c-before-font-lock-functions'.
1283 (c-save-buffer-state (num-beg num-end)
1284 ;; Apply the needed syntax-table and c-digit-separator text properties to
1287 (goto-char c-new-BEG)
1288 (while (and (< (point) c-new-END)
1289 (search-forward "'" c-new-END 'limit))
1290 (cond ((and (eq (char-before (1- (point))) ?\\)
1291 ;; Check we've got an odd number of \s, here.
1294 (eq (logand (skip-chars-backward "\\\\") 1) 1)))) ; not a real '.
1295 ((c-quoted-number-straddling-point)
1296 (setq num-beg (match-beginning 0)
1297 num-end (match-end 0))
1298 (c-invalidate-state-cache num-beg)
1299 (c-truncate-semi-nonlit-pos-cache num-beg)
1300 (c-put-char-properties-on-char num-beg num-end
1301 'syntax-table '(1) ?')
1302 (c-put-char-properties-on-char num-beg num-end
1303 'c-digit-separator t ?')
1304 (goto-char num-end))
1305 ((looking-at "\\([^\\']\\|\\\\.\\)'") ; balanced quoted expression.
1306 (goto-char (match-end 0)))
1308 (c-invalidate-state-cache (1- (point)))
1309 (c-truncate-semi-nonlit-pos-cache (1- (point)))
1310 (c-put-char-property (1- (point)) 'syntax-table '(1))))
1311 ;; Prevent the next `c-quoted-number-straddling-point' getting
1312 ;; confused by already processed single quotes.
1313 (narrow-to-region (point) (point-max))))))
1315 (defun c-before-change (beg end)
1316 ;; Function to be put on `before-change-functions'. Primarily, this calls
1317 ;; the language dependent `c-get-state-before-change-functions'. It is
1318 ;; otherwise used only to remove stale entries from the `c-found-types'
1319 ;; cache, and to record entries which a `c-after-change' function might
1320 ;; confirm as stale.
1322 ;; Note that this function must be FAST rather than accurate. Note
1323 ;; also that it only has any effect when font locking is enabled.
1324 ;; We exploit this by checking for font-lock-*-face instead of doing
1325 ;; rigorous syntactic analysis.
1327 ;; If either change boundary is wholly inside an identifier, delete
1328 ;; it/them from the cache. Don't worry about being inside a string
1329 ;; or a comment - "wrongly" removing a symbol from `c-found-types'
1331 (unless (or (c-called-from-text-property-change-p)
1332 c-just-done-before-change) ; guard against a spurious second
1333 ; invocation of before-change-functions.
1334 (setq c-just-done-before-change t)
1335 ;; (c-new-BEG c-new-END) will be the region to fontify.
1336 (setq c-new-BEG beg c-new-END end)
1337 (setq c-maybe-stale-found-type nil)
1342 ;; Are we inserting/deleting stuff in the middle of an identifier?
1343 (c-unfind-enclosing-token beg)
1344 (c-unfind-enclosing-token end)
1345 ;; Are we coalescing two tokens together, e.g. "fo o" -> "foo"?
1347 (c-unfind-coalesced-tokens beg end))
1348 (c-invalidate-sws-region-before end)
1349 ;; Are we (potentially) disrupting the syntactic context which
1350 ;; makes a type a type? E.g. by inserting stuff after "foo" in
1351 ;; "foo bar;", or before "foo" in "typedef foo *bar;"?
1353 ;; We search for appropriate c-type properties "near" the change.
1354 ;; First, find an appropriate boundary for this property search.
1359 (or (and (eq (get-text-property end 'face)
1360 'font-lock-comment-face)
1361 (previous-single-property-change end 'face))
1363 (when (>= end1 beg) ; Don't hassle about changes entirely in comments.
1364 ;; Find a limit for the search for a `c-type' property
1366 (and (/= (skip-chars-backward "^;{}") 0)
1367 (> (point) (point-min))
1368 (memq (c-get-char-property (1- (point)) 'face)
1369 '(font-lock-comment-face font-lock-string-face))))
1370 (setq lim (max (point-min) (1- (point))))
1372 ;; Look for the latest `c-type' property before end1
1373 (when (and (> end1 (point-min))
1375 (if (get-text-property (1- end1) 'c-type)
1377 (previous-single-property-change end1 'c-type
1379 (setq type (get-text-property (max (1- type-pos) lim) 'c-type))
1381 (when (memq type '(c-decl-id-start c-decl-type-start))
1382 ;; Get the identifier, if any, that the property is on.
1383 (goto-char (1- type-pos))
1385 (when (looking-at "\\(\\sw\\|\\s_\\)")
1386 (c-beginning-of-current-token)
1387 (buffer-substring-no-properties (point) type-pos)))
1390 (skip-chars-forward "^;{}") ;FIXME!!! loop for comment, maybe
1393 (or (c-next-single-property-change end 'c-type nil lim)
1395 (setq c-maybe-stale-found-type
1396 (list type marked-id
1398 (buffer-substring-no-properties type-pos
1400 (buffer-substring-no-properties beg end)))))))
1402 (if c-get-state-before-change-functions
1404 (funcall fn beg end))
1405 c-get-state-before-change-functions))
1407 ;; The following must be done here rather than in `c-after-change' because
1408 ;; newly inserted parens would foul up the invalidation algorithm.
1409 (c-invalidate-state-cache beg)))
1411 (defvar c-in-after-change-fontification nil)
1412 (make-variable-buffer-local 'c-in-after-change-fontification)
1413 ;; A flag to prevent region expanding stuff being done twice for after-change
1416 (defun c-after-change (beg end old-len)
1417 ;; Function put on `after-change-functions' to adjust various caches
1418 ;; etc. Prefer speed to finesse here, since there will be an order
1419 ;; of magnitude more calls to this function than any of the
1420 ;; functions that use the caches.
1422 ;; Note that care must be taken so that this is called before any
1423 ;; font-lock callbacks since we might get calls to functions using
1424 ;; these caches from inside them, and we must thus be sure that this
1425 ;; has already been executed.
1427 ;; This calls the language variable c-before-font-lock-functions, if non nil.
1428 ;; This typically sets `syntax-table' properties.
1430 ;; We can sometimes get two consecutive calls to `after-change-functions'
1431 ;; without an intervening call to `before-change-functions' when reverting
1432 ;; the buffer (see bug #24094). Whatever the cause, assume that the entire
1433 ;; buffer has changed.
1434 (when (not c-just-done-before-change)
1437 (c-before-change (point-min) (point-max))
1438 (setq beg (point-min)
1440 old-len (- end beg))))
1442 ;; (c-new-BEG c-new-END) will be the region to fontify. It may become
1443 ;; larger than (beg end).
1444 (setq c-new-END (- (+ c-new-END (- end beg)) old-len))
1445 (setq c-old-BEG c-new-BEG c-old-END c-new-END)
1447 (unless (c-called-from-text-property-change-p)
1448 (setq c-just-done-before-change nil)
1449 (c-save-buffer-state (case-fold-search)
1450 ;; When `combine-after-change-calls' is used we might get calls
1451 ;; with regions outside the current narrowing. This has been
1452 ;; observed in Emacs 20.7.
1454 (save-match-data ; c-recognize-<>-arglists changes match-data
1457 (when (> end (point-max))
1458 ;; Some emacsen might return positions past the end. This has been
1459 ;; observed in Emacs 20.7 when rereading a buffer changed on disk
1460 ;; (haven't been able to minimize it, but Emacs 21.3 appears to
1462 (setq end (point-max))
1466 ;; C-y is capable of spuriously converting category properties
1467 ;; c-</>-as-paren-syntax and c-cpp-delimiter into hard syntax-table
1468 ;; properties. Remove these when it happens.
1469 (when (eval-when-compile (memq 'category-properties c-emacs-features))
1470 (c-save-buffer-state ()
1471 (c-clear-char-property-with-value beg end 'syntax-table
1472 c-<-as-paren-syntax)
1473 (c-clear-char-property-with-value beg end 'syntax-table
1474 c->-as-paren-syntax)
1475 (c-clear-char-property-with-value beg end 'syntax-table nil)))
1477 (c-trim-found-types beg end old-len) ; maybe we don't need all of these.
1478 (c-invalidate-sws-region-after beg end old-len)
1479 ;; (c-invalidate-state-cache beg) ; moved to `c-before-change'.
1480 (c-invalidate-find-decl-cache beg)
1482 (when c-recognize-<>-arglists
1483 (c-after-change-check-<>-operators beg end))
1485 (setq c-in-after-change-fontification t)
1488 (funcall fn beg end old-len))
1489 c-before-font-lock-functions)))))))
1491 (defun c-fl-decl-start (pos)
1492 ;; If the beginning of the line containing POS is in the middle of a "local"
1493 ;; declaration, return the beginning of that declaration. Otherwise return
1494 ;; nil. Note that declarations, in this sense, can be nested. (A local
1495 ;; declaration is one which does not start outside of struct braces (and
1496 ;; similar) enclosing POS. Brace list braces here are not "similar".
1498 ;; This function is called indirectly from font locking stuff - either from
1499 ;; c-after-change (to prepare for after-change font-locking) or from font
1500 ;; lock context (etc.) fontification.
1502 (let ((lit-start (c-literal-start))
1506 (goto-char (c-point 'bol new-pos))
1507 (when lit-start ; Comment or string.
1508 (goto-char lit-start))
1509 (setq bod-lim (c-determine-limit 500))
1511 ;; In C++ Mode, first check if we are within a (possibly nested) lambda
1512 ;; form capture list.
1513 (when (c-major-mode-is 'c++-mode)
1514 (let ((paren-state (c-parse-state))
1517 (while (setq opener (c-pull-open-brace paren-state))
1519 (if (c-looking-at-c++-lambda-capture-list)
1520 (setq capture-opener (point)))))))
1523 ;; Go to a less nested declaration each time round this loop.
1525 (c-syntactic-skip-backward "^;{}" bod-lim t)
1527 (progn (c-forward-syntactic-ws)
1528 (setq bo-decl (point))
1529 ;; Are we looking at a keyword such as "template" or
1530 ;; "typedef" which can decorate a type, or the type itself?
1531 (when (or (looking-at c-prefix-spec-kwds-re)
1533 ;; We've found another candidate position.
1534 (setq new-pos (min new-pos bo-decl))
1535 (goto-char bo-decl))
1537 ;; Try and go out a level to search again.
1539 (c-backward-syntactic-ws bod-lim)
1540 (and (> (point) bod-lim)
1541 (or (memq (char-before) '(?\( ?\[))
1542 (and (eq (char-before) ?\<)
1543 (eq (c-get-char-property
1544 (1- (point)) 'syntax-table)
1545 c-<-as-paren-syntax))
1546 (and (eq (char-before) ?{)
1549 (consp (c-looking-at-or-maybe-in-bracelist))))
1552 (backward-char)) ; back over (, [, <.
1553 (when (and capture-opener (< capture-opener new-pos))
1554 (setq new-pos capture-opener))
1555 (and (/= new-pos pos) new-pos)))
1557 (defun c-fl-decl-end (pos)
1558 ;; If POS is inside a declarator, return the end of the token that follows
1559 ;; the declarator, otherwise return nil.
1561 (let ((lit-start (c-literal-start))
1563 (if lit-start (goto-char lit-start))
1564 (c-backward-syntactic-ws)
1565 (when (setq pos1 (c-on-identifier))
1567 (let ((lim (save-excursion
1568 (and (c-beginning-of-macro)
1569 (progn (c-end-of-macro) (point))))))
1570 (when (and (c-forward-declarator lim)
1571 (eq (c-forward-token-2 1 nil lim) 0))
1572 (c-backward-syntactic-ws)
1575 (defun c-change-expand-fl-region (_beg _end _old-len)
1576 ;; Expand the region (c-new-BEG c-new-END) to an after-change font-lock
1577 ;; region. This will usually be the smallest sequence of whole lines
1578 ;; containing `c-new-BEG' and `c-new-END', but if `c-new-BEG' is in a
1579 ;; "local" declaration (see `c-fl-decl-start') the beginning of this is used
1580 ;; as the lower bound.
1582 ;; This is called from an after-change-function, but the parameters BEG END
1583 ;; and OLD-LEN are not used.
1586 (or (c-fl-decl-start c-new-BEG) (c-point 'bol c-new-BEG))
1588 (or (c-fl-decl-end c-new-END)
1589 (c-point 'bonl (max (1- c-new-END) (point-min)))))))
1591 (defun c-context-expand-fl-region (beg end)
1592 ;; Return a cons (NEW-BEG . NEW-END), where NEW-BEG is the beginning of a
1593 ;; "local" declaration containing BEG (see `c-fl-decl-start') or BOL BEG is
1594 ;; in. NEW-END is beginning of the line after the one END is in.
1595 (c-save-buffer-state ()
1596 (cons (or (c-fl-decl-start beg) (c-point 'bol beg))
1597 (or (c-fl-decl-end end) (c-point 'bonl (1- end))))))
1599 (defun c-before-context-fl-expand-region (beg end)
1600 ;; Expand the region (BEG END) as specified by
1601 ;; `c-before-context-fontification-functions'. Return a cons of the bounds
1602 ;; of the new region.
1606 (let ((new-beg beg) (new-end end)
1607 (new-region (cons beg end)))
1609 (setq new-region (funcall fn new-beg new-end))
1610 (setq new-beg (car new-region) new-end (cdr new-region)))
1611 c-before-context-fontification-functions)
1614 (defun c-font-lock-fontify-region (beg end &optional verbose)
1615 ;; Effectively advice around `font-lock-fontify-region' which extends the
1616 ;; region (BEG END), for example, to avoid context fontification chopping
1617 ;; off the start of the context. Do not extend the region if it's already
1618 ;; been done (i.e. from an after-change fontification. An example (C++)
1619 ;; where the chopping off used to happen is this:
1621 ;; template <typename T>
1624 ;; void myfunc(T* p) {}
1626 ;; Type a space in the first blank line, and the fontification of the next
1627 ;; line was fouled up by context fontification.
1628 (let (new-beg new-end new-region case-fold-search)
1629 (if (and c-in-after-change-fontification
1630 (< beg c-new-END) (> end c-new-BEG))
1631 ;; Region and the latest after-change fontification region overlap.
1632 ;; Determine the upper and lower bounds of our adjusted region
1635 (if (<= beg c-new-BEG)
1636 (setq c-in-after-change-fontification nil))
1638 (if (and (>= beg (c-point 'bol c-new-BEG))
1640 ;; Either jit-lock has accepted `c-new-BEG', or has
1641 ;; (probably) extended the change region spuriously to
1642 ;; BOL, which position likely has a syntactically
1643 ;; different position. To ensure correct fontification,
1644 ;; we start at `c-new-BEG', assuming any characters to the
1645 ;; left of `c-new-BEG' on the line do not require
1648 (setq new-region (c-before-context-fl-expand-region beg end)
1649 new-end (cdr new-region))
1652 (if (and (>= end (c-point 'bol c-new-END))
1656 (cdr (c-before-context-fl-expand-region beg end))))))
1657 ;; Context (etc.) fontification.
1658 (setq new-region (c-before-context-fl-expand-region beg end)
1659 new-beg (car new-region) new-end (cdr new-region)))
1660 (funcall (default-value 'font-lock-fontify-region-function)
1661 new-beg new-end verbose)))
1663 (defun c-after-font-lock-init ()
1664 ;; Put on `font-lock-mode-hook'. This function ensures our after-change
1665 ;; function will get executed before the font-lock one.
1666 (when (memq #'c-after-change after-change-functions)
1667 (remove-hook 'after-change-functions #'c-after-change t)
1668 (add-hook 'after-change-functions #'c-after-change nil t)))
1670 (defun c-font-lock-init ()
1671 "Set up the font-lock variables for using the font-lock support in CC Mode.
1672 This does not load the font-lock package. Use after
1673 `c-basic-common-init' and after cc-fonts has been loaded.
1674 This function is called from `c-common-init', once per mode initialization."
1676 (set (make-local-variable 'font-lock-defaults)
1677 `(,(if (c-major-mode-is 'awk-mode)
1678 ;; awk-mode currently has only one font lock level.
1679 'awk-font-lock-keywords
1680 (mapcar 'c-mode-symbol
1681 '("font-lock-keywords" "font-lock-keywords-1"
1682 "font-lock-keywords-2" "font-lock-keywords-3")))
1684 ,c-identifier-syntax-modifications
1685 c-beginning-of-syntax
1686 (font-lock-mark-block-function
1687 . c-mark-function)))
1689 ;; Prevent `font-lock-default-fontify-region' extending the region it will
1690 ;; fontify to whole lines by removing `font-lock-extend-region-wholelines'
1691 ;; from `font-lock-extend-region-functions'. (Emacs only). This fixes
1692 ;; Emacs bug #19669.
1693 (when (boundp 'font-lock-extend-region-functions)
1694 (setq font-lock-extend-region-functions
1695 (delq 'font-lock-extend-region-wholelines
1696 font-lock-extend-region-functions)))
1698 (make-local-variable 'font-lock-fontify-region-function)
1699 (setq font-lock-fontify-region-function 'c-font-lock-fontify-region)
1701 (if (featurep 'xemacs)
1702 (make-local-hook 'font-lock-mode-hook))
1703 (add-hook 'font-lock-mode-hook 'c-after-font-lock-init nil t))
1705 ;; Emacs 22 and later.
1706 (defun c-extend-after-change-region (beg end _old-len)
1707 "Extend the region to be fontified, if necessary."
1708 ;; Note: the parameter OLD-LEN is ignored here. This somewhat indirect
1709 ;; implementation exists because it is minimally different from the
1710 ;; stand-alone CC Mode which, lacking
1711 ;; font-lock-extend-after-change-region-function, is forced to use advice
1714 ;; Of the seven CC Mode languages, currently (2009-05) only C, C++, Objc
1715 ;; (the languages with #define) and AWK Mode make non-null use of this
1717 (when (eq font-lock-support-mode 'jit-lock-mode)
1720 (c-save-buffer-state () ; Protect the undo-list from put-text-property.
1721 (if (< c-new-BEG beg)
1722 (put-text-property c-new-BEG beg 'fontified nil))
1723 (if (> c-new-END end)
1724 (put-text-property end c-new-END 'fontified nil)))))
1725 (cons c-new-BEG c-new-END))
1727 ;; Emacs < 22 and XEmacs
1728 (defmacro c-advise-fl-for-region (function)
1729 `(defadvice ,function (before get-awk-region activate)
1730 ;; Make sure that any string/regexp is completely font-locked.
1731 (when c-buffer-is-cc-mode
1733 (ad-set-arg 1 c-new-END) ; end
1734 (ad-set-arg 0 c-new-BEG))))) ; beg
1736 (unless (boundp 'font-lock-extend-after-change-region-function)
1737 (c-advise-fl-for-region font-lock-after-change-function)
1738 (c-advise-fl-for-region jit-lock-after-change)
1739 (c-advise-fl-for-region lazy-lock-defer-rest-after-change)
1740 (c-advise-fl-for-region lazy-lock-defer-line-after-change))
1742 ;; Connect up to `electric-indent-mode' (Emacs 24.4 and later).
1743 (defun c-electric-indent-mode-hook ()
1744 ;; Emacs has en/disabled `electric-indent-mode'. Propagate this through to
1745 ;; each CC Mode buffer.
1747 (with-current-buffer buf
1748 (when c-buffer-is-cc-mode
1749 ;; Don't use `c-toggle-electric-state' here due to recursion.
1750 (setq c-electric-flag electric-indent-mode)
1751 (c-update-modeline))))
1754 (defun c-electric-indent-local-mode-hook ()
1755 ;; Emacs has en/disabled `electric-indent-local-mode' for this buffer.
1756 ;; Propagate this through to this buffer's value of `c-electric-flag'
1757 (when c-buffer-is-cc-mode
1758 (setq c-electric-flag electric-indent-mode)
1759 (c-update-modeline)))
1764 (defvar c-mode-syntax-table
1765 (funcall (c-lang-const c-make-mode-syntax-table c))
1766 "Syntax table used in c-mode buffers.")
1768 (c-define-abbrev-table 'c-mode-abbrev-table
1769 '(("else" "else" c-electric-continued-statement 0)
1770 ("while" "while" c-electric-continued-statement 0))
1771 "Abbreviation table used in c-mode buffers.")
1774 (let ((map (c-make-inherited-keymap)))
1776 "Keymap used in c-mode buffers.")
1777 ;; Add bindings which are only useful for C.
1778 (define-key c-mode-map "\C-c\C-e" 'c-macro-expand)
1781 (easy-menu-define c-c-menu c-mode-map "C Mode Commands"
1782 (cons "C" (c-lang-const c-mode-menu c)))
1784 ;; In XEmacs >= 21.5 modes should add their own entries to
1785 ;; `auto-mode-alist'. The comment form of autoload is used to avoid
1786 ;; doing this on load. That since `add-to-list' prepends the value
1787 ;; which could cause it to clobber user settings. Later emacsen have
1788 ;; an append option, but it's not safe to use.
1790 ;; The extension ".C" is associated with C++ while the lowercase
1791 ;; variant goes with C. On case insensitive file systems, this means
1792 ;; that ".c" files also might open C++ mode if the C++ entry comes
1793 ;; first on `auto-mode-alist'. Thus we try to ensure that ".C" comes
1794 ;; after ".c", and since `add-to-list' adds the entry first we have to
1795 ;; add the ".C" entry first.
1796 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(cc\\|hh\\)\\'" . c++-mode))
1797 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.[ch]\\(pp\\|xx\\|\\+\\+\\)\\'" . c++-mode))
1798 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(CC?\\|HH?\\)\\'" . c++-mode))
1800 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.c\\'" . c-mode))
1801 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.h\\'" . c-or-c++-mode))
1803 ;; NB: The following two associate yacc and lex files to C Mode, which
1804 ;; is not really suitable for those formats. Anyway, afaik there's
1805 ;; currently no better mode for them, and besides this is legacy.
1806 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.y\\(acc\\)?\\'" . c-mode))
1807 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.lex\\'" . c-mode))
1809 ;; Preprocessed files generated by C and C++ compilers.
1810 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.i\\'" . c-mode))
1811 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.ii\\'" . c++-mode))
1813 (unless (fboundp 'prog-mode) (defalias 'prog-mode 'fundamental-mode))
1816 (define-derived-mode c-mode prog-mode "C"
1817 "Major mode for editing C code.
1819 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1820 c-mode buffer. This automatically sets up a mail buffer with version
1821 information already added. You just need to add a description of the
1822 problem, including a reproducible test case, and send the message.
1824 To see what version of CC Mode you are running, enter `\\[c-version]'.
1826 The hook `c-mode-common-hook' is run with no args at mode
1827 initialization, then `c-mode-hook'.
1831 :after-hook (progn (c-make-noise-macro-regexps)
1832 (c-make-macro-with-semi-re)
1833 (c-update-modeline))
1834 (c-initialize-cc-mode t)
1835 (setq abbrev-mode t)
1836 (c-init-language-vars-for 'c-mode)
1837 (c-common-init 'c-mode)
1838 (easy-menu-add c-c-menu)
1839 (cc-imenu-init cc-imenu-c-generic-expression)
1840 (c-run-mode-hooks 'c-mode-common-hook))
1842 (defconst c-or-c++-mode--regexp
1844 (let ((id "[a-zA-Z0-9_]+") (ws "[ \t\r]+") (ws-maybe "[ \t\r]*"))
1845 (concat "^" ws-maybe "\\(?:"
1846 "using" ws "\\(?:namespace" ws "std;\\|std::\\)"
1847 "\\|" "namespace" "\\(:?" ws id "\\)?" ws-maybe "{"
1848 "\\|" "class" ws id ws-maybe "[:{\n]"
1849 "\\|" "template" ws-maybe "<.*>"
1850 "\\|" "#include" ws-maybe "<\\(?:string\\|iostream\\|map\\)>"
1852 "A regexp applied to C header files to check if they are really C++.")
1855 (defun c-or-c++-mode ()
1856 "Analyse buffer and enable either C or C++ mode.
1858 Some people and projects use .h extension for C++ header files
1859 which is also the one used for C header files. This makes
1860 matching on file name insufficient for detecting major mode that
1863 This function attempts to use file contents to determine whether
1864 the code is C or C++ and based on that chooses whether to enable
1865 `c-mode' or `c++-mode'."
1870 (goto-char (point-min))
1871 (re-search-forward c-or-c++-mode--regexp
1872 (+ (point) c-guess-region-max) t))))
1879 (defvar c++-mode-syntax-table
1880 (funcall (c-lang-const c-make-mode-syntax-table c++))
1881 "Syntax table used in c++-mode buffers.")
1883 (c-define-abbrev-table 'c++-mode-abbrev-table
1884 '(("else" "else" c-electric-continued-statement 0)
1885 ("while" "while" c-electric-continued-statement 0)
1886 ("catch" "catch" c-electric-continued-statement 0))
1887 "Abbreviation table used in c++-mode buffers.")
1889 (defvar c++-mode-map
1890 (let ((map (c-make-inherited-keymap)))
1892 "Keymap used in c++-mode buffers.")
1893 ;; Add bindings which are only useful for C++.
1894 (define-key c++-mode-map "\C-c\C-e" 'c-macro-expand)
1895 (define-key c++-mode-map "\C-c:" 'c-scope-operator)
1896 (define-key c++-mode-map "<" 'c-electric-lt-gt)
1897 (define-key c++-mode-map ">" 'c-electric-lt-gt)
1899 (easy-menu-define c-c++-menu c++-mode-map "C++ Mode Commands"
1900 (cons "C++" (c-lang-const c-mode-menu c++)))
1903 (define-derived-mode c++-mode prog-mode "C++"
1904 "Major mode for editing C++ code.
1905 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1906 c++-mode buffer. This automatically sets up a mail buffer with
1907 version information already added. You just need to add a description
1908 of the problem, including a reproducible test case, and send the
1911 To see what version of CC Mode you are running, enter `\\[c-version]'.
1913 The hook `c-mode-common-hook' is run with no args at mode
1914 initialization, then `c++-mode-hook'.
1918 :after-hook (progn (c-make-noise-macro-regexps)
1919 (c-make-macro-with-semi-re)
1920 (c-update-modeline))
1921 (c-initialize-cc-mode t)
1922 (setq abbrev-mode t)
1923 (c-init-language-vars-for 'c++-mode)
1924 (c-common-init 'c++-mode)
1925 (easy-menu-add c-c++-menu)
1926 (cc-imenu-init cc-imenu-c++-generic-expression)
1927 (c-run-mode-hooks 'c-mode-common-hook))
1930 ;; Support for Objective-C
1932 (defvar objc-mode-syntax-table
1933 (funcall (c-lang-const c-make-mode-syntax-table objc))
1934 "Syntax table used in objc-mode buffers.")
1936 (c-define-abbrev-table 'objc-mode-abbrev-table
1937 '(("else" "else" c-electric-continued-statement 0)
1938 ("while" "while" c-electric-continued-statement 0))
1939 "Abbreviation table used in objc-mode buffers.")
1941 (defvar objc-mode-map
1942 (let ((map (c-make-inherited-keymap)))
1944 "Keymap used in objc-mode buffers.")
1945 ;; Add bindings which are only useful for Objective-C.
1946 (define-key objc-mode-map "\C-c\C-e" 'c-macro-expand)
1948 (easy-menu-define c-objc-menu objc-mode-map "ObjC Mode Commands"
1949 (cons "ObjC" (c-lang-const c-mode-menu objc)))
1951 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.m\\'" . objc-mode))
1954 (define-derived-mode objc-mode prog-mode "ObjC"
1955 "Major mode for editing Objective C code.
1956 To submit a problem report, enter `\\[c-submit-bug-report]' from an
1957 objc-mode buffer. This automatically sets up a mail buffer with
1958 version information already added. You just need to add a description
1959 of the problem, including a reproducible test case, and send the
1962 To see what version of CC Mode you are running, enter `\\[c-version]'.
1964 The hook `c-mode-common-hook' is run with no args at mode
1965 initialization, then `objc-mode-hook'.
1969 :after-hook (progn (c-make-noise-macro-regexps)
1970 (c-make-macro-with-semi-re)
1971 (c-update-modeline))
1972 (c-initialize-cc-mode t)
1973 (setq abbrev-mode t)
1974 (c-init-language-vars-for 'objc-mode)
1975 (c-common-init 'objc-mode)
1976 (easy-menu-add c-objc-menu)
1977 (cc-imenu-init nil 'cc-imenu-objc-function)
1978 (c-run-mode-hooks 'c-mode-common-hook))
1983 (defvar java-mode-syntax-table
1984 (funcall (c-lang-const c-make-mode-syntax-table java))
1985 "Syntax table used in java-mode buffers.")
1987 (c-define-abbrev-table 'java-mode-abbrev-table
1988 '(("else" "else" c-electric-continued-statement 0)
1989 ("while" "while" c-electric-continued-statement 0)
1990 ("catch" "catch" c-electric-continued-statement 0)
1991 ("finally" "finally" c-electric-continued-statement 0))
1992 "Abbreviation table used in java-mode buffers.")
1994 (defvar java-mode-map
1995 (let ((map (c-make-inherited-keymap)))
1997 "Keymap used in java-mode buffers.")
1998 ;; Add bindings which are only useful for Java.
2000 ;; Regexp trying to describe the beginning of a Java top-level
2001 ;; definition. This is not used by CC Mode, nor is it maintained
2002 ;; since it's practically impossible to write a regexp that reliably
2003 ;; matches such a construct. Other tools are necessary.
2004 (defconst c-Java-defun-prompt-regexp
2005 "^[ \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-*")
2007 (easy-menu-define c-java-menu java-mode-map "Java Mode Commands"
2008 (cons "Java" (c-lang-const c-mode-menu java)))
2010 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.java\\'" . java-mode))
2013 (define-derived-mode java-mode prog-mode "Java"
2014 "Major mode for editing Java code.
2015 To submit a problem report, enter `\\[c-submit-bug-report]' from a
2016 java-mode buffer. This automatically sets up a mail buffer with
2017 version information already added. You just need to add a description
2018 of the problem, including a reproducible test case, and send the
2021 To see what version of CC Mode you are running, enter `\\[c-version]'.
2023 The hook `c-mode-common-hook' is run with no args at mode
2024 initialization, then `java-mode-hook'.
2028 :after-hook (c-update-modeline)
2029 (c-initialize-cc-mode t)
2030 (setq abbrev-mode t)
2031 (c-init-language-vars-for 'java-mode)
2032 (c-common-init 'java-mode)
2033 (easy-menu-add c-java-menu)
2034 (cc-imenu-init cc-imenu-java-generic-expression)
2035 (c-run-mode-hooks 'c-mode-common-hook))
2038 ;; Support for CORBA's IDL language
2040 (defvar idl-mode-syntax-table
2041 (funcall (c-lang-const c-make-mode-syntax-table idl))
2042 "Syntax table used in idl-mode buffers.")
2044 (c-define-abbrev-table 'idl-mode-abbrev-table nil
2045 "Abbreviation table used in idl-mode buffers.")
2047 (defvar idl-mode-map
2048 (let ((map (c-make-inherited-keymap)))
2050 "Keymap used in idl-mode buffers.")
2051 ;; Add bindings which are only useful for IDL.
2053 (easy-menu-define c-idl-menu idl-mode-map "IDL Mode Commands"
2054 (cons "IDL" (c-lang-const c-mode-menu idl)))
2056 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.idl\\'" . idl-mode))
2059 (define-derived-mode idl-mode prog-mode "IDL"
2060 "Major mode for editing CORBA's IDL, PSDL and CIDL code.
2061 To submit a problem report, enter `\\[c-submit-bug-report]' from an
2062 idl-mode buffer. This automatically sets up a mail buffer with
2063 version information already added. You just need to add a description
2064 of the problem, including a reproducible test case, and send the
2067 To see what version of CC Mode you are running, enter `\\[c-version]'.
2069 The hook `c-mode-common-hook' is run with no args at mode
2070 initialization, then `idl-mode-hook'.
2074 :after-hook (c-update-modeline)
2075 (c-initialize-cc-mode t)
2076 (c-init-language-vars-for 'idl-mode)
2077 (c-common-init 'idl-mode)
2078 (easy-menu-add c-idl-menu)
2079 ;;(cc-imenu-init cc-imenu-idl-generic-expression) ;TODO
2080 (c-run-mode-hooks 'c-mode-common-hook))
2085 (defvar pike-mode-syntax-table
2086 (funcall (c-lang-const c-make-mode-syntax-table pike))
2087 "Syntax table used in pike-mode buffers.")
2089 (c-define-abbrev-table 'pike-mode-abbrev-table
2090 '(("else" "else" c-electric-continued-statement 0)
2091 ("while" "while" c-electric-continued-statement 0))
2092 "Abbreviation table used in pike-mode buffers.")
2094 (defvar pike-mode-map
2095 (let ((map (c-make-inherited-keymap)))
2097 "Keymap used in pike-mode buffers.")
2098 ;; Additional bindings.
2099 (define-key pike-mode-map "\C-c\C-e" 'c-macro-expand)
2101 (easy-menu-define c-pike-menu pike-mode-map "Pike Mode Commands"
2102 (cons "Pike" (c-lang-const c-mode-menu pike)))
2104 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(u?lpc\\|pike\\|pmod\\(\\.in\\)?\\)\\'" . pike-mode))
2105 ;;;###autoload (add-to-list 'interpreter-mode-alist '("pike" . pike-mode))
2108 (define-derived-mode pike-mode prog-mode "Pike"
2109 "Major mode for editing Pike code.
2110 To submit a problem report, enter `\\[c-submit-bug-report]' from a
2111 pike-mode buffer. This automatically sets up a mail buffer with
2112 version information already added. You just need to add a description
2113 of the problem, including a reproducible test case, and send the
2116 To see what version of CC Mode you are running, enter `\\[c-version]'.
2118 The hook `c-mode-common-hook' is run with no args at mode
2119 initialization, then `pike-mode-hook'.
2123 :after-hook (c-update-modeline)
2124 (c-initialize-cc-mode t)
2125 (setq abbrev-mode t)
2126 (c-init-language-vars-for 'pike-mode)
2127 (c-common-init 'pike-mode)
2128 (easy-menu-add c-pike-menu)
2129 ;;(cc-imenu-init cc-imenu-pike-generic-expression) ;TODO
2130 (c-run-mode-hooks 'c-mode-common-hook))
2135 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.awk\\'" . awk-mode))
2136 ;;;###autoload (add-to-list 'interpreter-mode-alist '("awk" . awk-mode))
2137 ;;;###autoload (add-to-list 'interpreter-mode-alist '("mawk" . awk-mode))
2138 ;;;###autoload (add-to-list 'interpreter-mode-alist '("nawk" . awk-mode))
2139 ;;;###autoload (add-to-list 'interpreter-mode-alist '("gawk" . awk-mode))
2141 (c-define-abbrev-table 'awk-mode-abbrev-table
2142 '(("else" "else" c-electric-continued-statement 0)
2143 ("while" "while" c-electric-continued-statement 0))
2144 "Abbreviation table used in awk-mode buffers.")
2146 (defvar awk-mode-map
2147 (let ((map (c-make-inherited-keymap)))
2149 "Keymap used in awk-mode buffers.")
2150 ;; Add bindings which are only useful for awk.
2151 (define-key awk-mode-map "#" 'self-insert-command);Overrides electric parent binding.
2152 (define-key awk-mode-map "/" 'self-insert-command);Overrides electric parent binding.
2153 (define-key awk-mode-map "*" 'self-insert-command);Overrides electric parent binding.
2154 (define-key awk-mode-map "\C-c\C-n" 'undefined) ; #if doesn't exist in awk.
2155 (define-key awk-mode-map "\C-c\C-p" 'undefined)
2156 (define-key awk-mode-map "\C-c\C-u" 'undefined)
2157 (define-key awk-mode-map "\M-a" 'c-beginning-of-statement) ; 2003/10/7
2158 (define-key awk-mode-map "\M-e" 'c-end-of-statement) ; 2003/10/7
2159 (define-key awk-mode-map "\C-\M-a" 'c-awk-beginning-of-defun)
2160 (define-key awk-mode-map "\C-\M-e" 'c-awk-end-of-defun)
2162 (easy-menu-define c-awk-menu awk-mode-map "AWK Mode Commands"
2163 (cons "AWK" (c-lang-const c-mode-menu awk)))
2165 ;; (require 'cc-awk) brings these in.
2166 (defvar awk-mode-syntax-table)
2167 (declare-function c-awk-unstick-NL-prop "cc-awk" ())
2170 (define-derived-mode awk-mode prog-mode "AWK"
2171 "Major mode for editing AWK code.
2172 To submit a problem report, enter `\\[c-submit-bug-report]' from an
2173 awk-mode buffer. This automatically sets up a mail buffer with version
2174 information already added. You just need to add a description of the
2175 problem, including a reproducible test case, and send the message.
2177 To see what version of CC Mode you are running, enter `\\[c-version]'.
2179 The hook `c-mode-common-hook' is run with no args at mode
2180 initialization, then `awk-mode-hook'.
2184 :after-hook (c-update-modeline)
2185 ;; We need the next line to stop the macro defining
2186 ;; `awk-mode-syntax-table'. This would mask the real table which is
2187 ;; declared in cc-awk.el and hasn't yet been loaded.
2189 (require 'cc-awk) ; Added 2003/6/10.
2190 (c-initialize-cc-mode t)
2191 (set-syntax-table awk-mode-syntax-table)
2192 (setq abbrev-mode t)
2193 (c-init-language-vars-for 'awk-mode)
2194 (c-common-init 'awk-mode)
2195 (c-awk-unstick-NL-prop)
2196 (c-run-mode-hooks 'c-mode-common-hook))
2201 (defconst c-mode-help-address
2202 "submit@debbugs.gnu.org"
2203 "Address(es) for CC Mode bug reports.")
2206 "Echo the current version of CC Mode in the minibuffer."
2208 (message "Using CC Mode version %s" c-version)
2209 (c-keep-region-active))
2211 (define-obsolete-variable-alias 'c-prepare-bug-report-hooks
2212 'c-prepare-bug-report-hook "24.3")
2213 (defvar c-prepare-bug-report-hook nil)
2215 ;; Dynamic variables used by reporter.
2216 (defvar reporter-prompt-for-summary-p)
2217 (defvar reporter-dont-compact-list)
2219 ;; This could be "emacs,cc-mode" in the version included in Emacs.
2220 (defconst c-mode-bug-package "cc-mode"
2221 "The package to use in the bug submission.")
2223 ;; reporter-submit-bug-report requires sendmail.
2224 (declare-function mail-position-on-field "sendmail" (field &optional soft))
2226 (defun c-submit-bug-report ()
2227 "Submit via mail a bug report on CC Mode."
2231 (let ((reporter-prompt-for-summary-p t)
2232 (reporter-dont-compact-list '(c-offsets-alist))
2233 (style c-indentation-style)
2234 (c-features c-emacs-features))
2236 (if (y-or-n-p "Do you want to submit a report on CC Mode? ")
2238 (reporter-submit-bug-report
2240 (concat "CC Mode " c-version " (" mode-name ")")
2243 '(c-buffer-is-cc-mode
2245 c-syntactic-indentation
2246 c-syntactic-indentation-in-macros
2248 c-auto-align-backslashes
2249 c-backspace-function
2251 c-electric-pound-behavior
2253 c-enable-xemacs-performance-kludge-p
2254 c-old-style-variable-behavior
2258 parse-sexp-ignore-comments
2259 parse-sexp-lookup-properties
2260 lookup-syntax-properties
2261 ;; A brain-damaged XEmacs only variable that, if
2262 ;; set to nil can cause all kinds of chaos.
2263 signal-error-on-buffer-boundary
2264 ;; Variables that affect line breaking and comments.
2274 adaptive-fill-regexp)
2276 (mapc (lambda (var) (unless (boundp var)
2277 (setq vars (delq var vars))))
2278 '(signal-error-on-buffer-boundary
2283 font-lock-maximum-decoration
2284 parse-sexp-lookup-properties
2285 lookup-syntax-properties))
2288 (run-hooks 'c-prepare-bug-report-hook)
2290 (or (mail-position-on-field "X-Debbugs-Package")
2291 (insert c-mode-bug-package)))
2292 (insert (format "Buffer Style: %s\nc-emacs-features: %s\n"
2293 style c-features)))))))
2296 (cc-provide 'cc-mode)
2299 ;; indent-tabs-mode: t
2302 ;;; cc-mode.el ends here