Ediff: add some missing documentation
[emacs.git] / lisp / progmodes / cc-mode.el
blobf74e931a8bb056fe235cf5cce273e7758a003076
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
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 <https://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 ;; Externally maintained major modes which use CC-mode's engine include:
75 ;; - cuda-mode
76 ;; - csharp-mode (https://github.com/josteink/csharp-mode)
77 ;; - haxe-mode
78 ;; - d-mode
79 ;; - dart-mode
80 ;; - cc-php-js-cs.el
81 ;; - php-mode
82 ;; - yang-mode
83 ;; - math-mode (mathematica)
84 ;; - unrealscript-mode
85 ;; - groovy-mode
87 ;;; Code:
89 ;; For Emacs < 22.2.
90 (eval-and-compile
91 (unless (fboundp 'declare-function) (defmacro declare-function (&rest _r))))
93 (eval-when-compile
94 (let ((load-path
95 (if (and (boundp 'byte-compile-dest-file)
96 (stringp byte-compile-dest-file))
97 (cons (file-name-directory byte-compile-dest-file) load-path)
98 load-path)))
99 (load "cc-bytecomp" nil t)))
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
116 ;; font-lock.
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
132 ;; use:
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
158 (save-restriction
159 (widen)
160 (c-save-buffer-state ()
161 (c-clear-char-properties (point-min) (point-max) 'category)
162 (c-clear-char-properties (point-min) (point-max) 'syntax-table)
163 (c-clear-char-properties (point-min) (point-max) 'c-is-sws)
164 (c-clear-char-properties (point-min) (point-max) 'c-in-sws)
165 (c-clear-char-properties (point-min) (point-max) 'c-type)
166 (if (c-major-mode-is 'awk-mode)
167 (c-clear-char-properties (point-min) (point-max) 'c-awk-NL-prop))))
168 (setq c-buffer-is-cc-mode nil)))
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))))
185 ;;;###autoload
186 (defun c-initialize-cc-mode (&optional new-style-init)
187 "Initialize CC Mode for use in the current buffer.
188 If the optional NEW-STYLE-INIT is nil or left out then all necessary
189 initialization to run CC Mode for the C language is done. Otherwise
190 only some basic setup is done, and a call to `c-init-language-vars' or
191 `c-init-language-vars-for' is necessary too (which gives more
192 control). See \"cc-mode.el\" for more info."
194 (setq c-buffer-is-cc-mode t)
196 (let ((initprop 'cc-mode-is-initialized)
197 c-initialization-ok)
198 (unless (get 'c-initialize-cc-mode initprop)
199 (unwind-protect
200 (progn
201 (put 'c-initialize-cc-mode initprop t)
202 (c-initialize-builtin-style)
203 (run-hooks 'c-initialization-hook)
204 ;; Fix obsolete variables.
205 (if (boundp 'c-comment-continuation-stars)
206 (setq c-block-comment-prefix c-comment-continuation-stars))
207 (add-hook 'change-major-mode-hook 'c-leave-cc-mode-mode)
208 (setq c-initialization-ok t)
209 ;; Connect up with Emacs's electric-indent-mode, for >= Emacs 24.4
210 (when (fboundp 'electric-indent-local-mode)
211 (add-hook 'electric-indent-mode-hook 'c-electric-indent-mode-hook)
212 (add-hook 'electric-indent-local-mode-hook
213 'c-electric-indent-local-mode-hook)))
214 ;; Will try initialization hooks again if they failed.
215 (put 'c-initialize-cc-mode initprop c-initialization-ok))))
217 (unless new-style-init
218 (c-init-language-vars-for 'c-mode)))
221 ;;; Common routines.
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)
229 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)))))
242 (while defs
243 (condition-case nil
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
254 (progn
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)))
264 (if c-mode-base-map
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
279 'c-end-of-statement
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
286 ;; here?
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
348 ;; as appropriate.
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)
393 (define-key c-mode-base-map "\C-c\C-z" 'c-display-defun-name))
395 ;; We don't require the outline package, but we configure it a bit anyway.
396 (cc-bytecomp-defvar outline-level)
398 (defun c-mode-menu (modestr)
399 "Return a menu spec suitable for `easy-menu-define' that is exactly
400 like the C mode menu except that the menu bar item name is MODESTR
401 instead of \"C\".
403 This function is provided for compatibility only; derived modes should
404 preferably use the `c-mode-menu' language constant directly."
405 (cons modestr (c-lang-const c-mode-menu c)))
407 ;; Ugly hack to pull in the definition of `c-populate-syntax-table'
408 ;; from cc-langs to make it available at runtime. It's either this or
409 ;; moving the definition for it to cc-defs, but that would mean to
410 ;; break up the syntax table setup over two files.
411 (defalias 'c-populate-syntax-table
412 (cc-eval-when-compile
413 (let ((f (symbol-function 'c-populate-syntax-table)))
414 (if (byte-code-function-p f) f (byte-compile f)))))
416 ;; CAUTION: Try to avoid installing things on
417 ;; `before-change-functions'. The macro `combine-after-change-calls'
418 ;; is used and it doesn't work if there are things on that hook. That
419 ;; can cause font lock functions to run in inconvenient places during
420 ;; temporary changes in some font lock support modes, causing extra
421 ;; unnecessary work and font lock glitches due to interactions between
422 ;; various text properties.
424 ;; (2007-02-12): The macro `combine-after-change-calls' ISN'T used any
425 ;; more.
427 (defun c-unfind-enclosing-token (pos)
428 ;; If POS is wholly inside a token, remove that id from
429 ;; `c-found-types', should it be present. Return t if we were in an
430 ;; id, else nil.
431 (save-excursion
432 (let ((tok-beg (progn (goto-char pos)
433 (and (c-beginning-of-current-token) (point))))
434 (tok-end (progn (goto-char pos)
435 (and (c-end-of-current-token) (point)))))
436 (when (and tok-beg tok-end)
437 (c-unfind-type (buffer-substring-no-properties tok-beg tok-end))
438 t))))
440 (defun c-unfind-coalesced-tokens (beg end)
441 ;; If removing the region (beg end) would coalesce an identifier ending at
442 ;; beg with an identifier (fragment) beginning at end, or an identifier
443 ;; fragment ending at beg with an identifier beginning at end, remove the
444 ;; pertinent identifier(s) from `c-found-types'.
445 (save-excursion
446 (when (< beg end)
447 (goto-char beg)
448 (when
449 (and (not (bobp))
450 (progn (c-backward-syntactic-ws) (eq (point) beg))
451 (/= (skip-chars-backward c-symbol-chars (1- (point))) 0)
452 (progn (goto-char beg) (c-forward-syntactic-ws) (<= (point) end))
453 (> (point) beg)
454 (goto-char end)
455 (looking-at c-symbol-char-key))
456 (goto-char beg)
457 (c-simple-skip-symbol-backward)
458 (c-unfind-type (buffer-substring-no-properties (point) beg)))
460 (goto-char end)
461 (when
462 (and (not (eobp))
463 (progn (c-forward-syntactic-ws) (eq (point) end))
464 (looking-at c-symbol-char-key)
465 (progn (c-backward-syntactic-ws) (>= (point) beg))
466 (< (point) end)
467 (/= (skip-chars-backward c-symbol-chars (1- (point))) 0))
468 (goto-char (1+ end))
469 (c-end-of-current-token)
470 (c-unfind-type (buffer-substring-no-properties end (point)))))))
472 ;; c-maybe-stale-found-type records a place near the region being
473 ;; changed where an element of `found-types' might become stale. It
474 ;; is set in c-before-change and is either nil, or has the form:
476 ;; (c-decl-id-start "foo" 97 107 " (* ooka) " "o"), where
478 ;; o - `c-decl-id-start' is the c-type text property value at buffer
479 ;; pos 96.
481 ;; o - 97 107 is the region potentially containing the stale type -
482 ;; this is delimited by a non-nil c-type text property at 96 and
483 ;; either another one or a ";", "{", or "}" at 107.
485 ;; o - " (* ooka) " is the (before change) buffer portion containing
486 ;; the suspect type (here "ooka").
488 ;; o - "o" is the buffer contents which is about to be deleted. This
489 ;; would be the empty string for an insertion.
490 (defvar c-maybe-stale-found-type nil)
491 (make-variable-buffer-local 'c-maybe-stale-found-type)
493 (defvar c-just-done-before-change nil)
494 (make-variable-buffer-local 'c-just-done-before-change)
495 ;; This variable is set to t by `c-before-change' and to nil by
496 ;; `c-after-change'. It is used for two purposes: (i) to detect a spurious
497 ;; invocation of `before-change-functions' directly following on from a
498 ;; correct one. This happens in some Emacsen, for example when
499 ;; `basic-save-buffer' does (insert ?\n) when `require-final-newline' is
500 ;; non-nil; (ii) to detect when Emacs fails to invoke
501 ;; `before-change-functions'. This can happen when reverting a buffer - see
502 ;; bug #24094. It seems these failures happen only in GNU Emacs; XEmacs
503 ;; seems to maintain the strict alternation of calls to
504 ;; `before-change-functions' and `after-change-functions'.
506 (defun c-basic-common-init (mode default-style)
507 "Do the necessary initialization for the syntax handling routines
508 and the line breaking/filling code. Intended to be used by other
509 packages that embed CC Mode.
511 MODE is the CC Mode flavor to set up, e.g. `c-mode' or `java-mode'.
512 DEFAULT-STYLE tells which indentation style to install. It has the
513 same format as `c-default-style'.
515 Note that `c-init-language-vars' must be called before this function.
516 This function cannot do that since `c-init-language-vars' is a macro
517 that requires a literal mode spec at compile time."
519 (setq c-buffer-is-cc-mode mode)
521 ;; these variables should always be buffer local; they do not affect
522 ;; indentation style.
523 (make-local-variable 'comment-start)
524 (make-local-variable 'comment-end)
525 (make-local-variable 'comment-start-skip)
527 (make-local-variable 'paragraph-start)
528 (make-local-variable 'paragraph-separate)
529 (make-local-variable 'paragraph-ignore-fill-prefix)
530 (make-local-variable 'adaptive-fill-mode)
531 (make-local-variable 'adaptive-fill-regexp)
532 (make-local-variable 'fill-paragraph-handle-comment)
534 ;; now set their values
535 (set (make-local-variable 'parse-sexp-ignore-comments) t)
536 (set (make-local-variable 'indent-line-function) 'c-indent-line)
537 (set (make-local-variable 'indent-region-function) 'c-indent-region)
538 (set (make-local-variable 'normal-auto-fill-function) 'c-do-auto-fill)
539 (set (make-local-variable 'comment-multi-line) t)
540 (set (make-local-variable 'comment-line-break-function)
541 'c-indent-new-comment-line)
543 ;; Prevent time-wasting activity on C-y.
544 (when (boundp 'yank-handled-properties)
545 (make-local-variable 'yank-handled-properties)
546 (let ((yank-cat-handler (assq 'category yank-handled-properties)))
547 (when yank-cat-handler
548 (setq yank-handled-properties (remq yank-cat-handler
549 yank-handled-properties)))))
551 ;; For the benefit of adaptive fill, which otherwise mis-fills.
552 (setq fill-paragraph-handle-comment nil)
554 ;; Install `c-fill-paragraph' on `fill-paragraph-function' so that a
555 ;; direct call to `fill-paragraph' behaves better. This still
556 ;; doesn't work with filladapt but it's better than nothing.
557 (set (make-local-variable 'fill-paragraph-function) 'c-fill-paragraph)
559 ;; Initialize the cache of brace pairs, and opening braces/brackets/parens.
560 (c-state-cache-init)
561 ;; Initialize the "brace stack" cache.
562 (c-init-bs-cache)
564 (when (or c-recognize-<>-arglists
565 (c-major-mode-is 'awk-mode)
566 (c-major-mode-is '(java-mode c-mode c++-mode objc-mode)))
567 ;; We'll use the syntax-table text property to change the syntax
568 ;; of some chars for this language, so do the necessary setup for
569 ;; that.
571 ;; Note to other package developers: It's ok to turn this on in CC
572 ;; Mode buffers when CC Mode doesn't, but it's not ok to turn it
573 ;; off if CC Mode has turned it on.
575 ;; Emacs.
576 (when (boundp 'parse-sexp-lookup-properties)
577 (set (make-local-variable 'parse-sexp-lookup-properties) t))
579 ;; Same as above for XEmacs.
580 (when (boundp 'lookup-syntax-properties)
581 (set (make-local-variable 'lookup-syntax-properties) t)))
583 ;; Use this in Emacs 21+ to avoid meddling with the rear-nonsticky
584 ;; property on each character.
585 (when (boundp 'text-property-default-nonsticky)
586 (make-local-variable 'text-property-default-nonsticky)
587 (mapc (lambda (tprop)
588 (unless (assq tprop text-property-default-nonsticky)
589 (setq text-property-default-nonsticky
590 (cons `(,tprop . t) text-property-default-nonsticky))))
591 '(syntax-table category c-type)))
593 ;; In Emacs 21 and later it's possible to turn off the ad-hoc
594 ;; heuristic that open parens in column 0 are defun starters. Since
595 ;; we have c-state-cache, that heuristic isn't useful and only causes
596 ;; trouble, so turn it off.
597 ;; 2006/12/17: This facility is somewhat confused, and doesn't really seem
598 ;; helpful. Comment it out for now.
599 ;; (when (memq 'col-0-paren c-emacs-features)
600 ;; (make-local-variable 'open-paren-in-column-0-is-defun-start)
601 ;; (setq open-paren-in-column-0-is-defun-start nil))
603 (c-clear-found-types)
605 ;; now set the mode style based on default-style
606 (let ((style (cc-choose-style-for-mode mode default-style)))
607 ;; Override style variables if `c-old-style-variable-behavior' is
608 ;; set. Also override if we are using global style variables,
609 ;; have already initialized a style once, and are switching to a
610 ;; different style. (It's doubtful whether this is desirable, but
611 ;; the whole situation with nonlocal style variables is a bit
612 ;; awkward. It's at least the most compatible way with the old
613 ;; style init procedure.)
614 (c-set-style style (not (or c-old-style-variable-behavior
615 (and (not c-style-variables-are-local-p)
616 c-indentation-style
617 (not (string-equal c-indentation-style
618 style)))))))
619 (c-setup-paragraph-variables)
621 ;; we have to do something special for c-offsets-alist so that the
622 ;; buffer local value has its own alist structure.
623 (setq c-offsets-alist (copy-alist c-offsets-alist))
625 ;; setup the comment indent variable in a Emacs version portable way
626 (set (make-local-variable 'comment-indent-function) 'c-comment-indent)
627 ;; What sort of comments are default for M-;?
628 (setq c-block-comment-flag c-block-comment-is-default)
630 ;; In Emacs 24.4 onwards, prevent Emacs's built in electric indentation from
631 ;; messing up CC Mode's, and set `c-electric-flag' if `electric-indent-mode'
632 ;; has been called by the user.
633 (when (boundp 'electric-indent-inhibit) (setq electric-indent-inhibit t))
634 ;; CC-mode should obey Emacs's generic preferences, tho only do it if
635 ;; Emacs's generic preferences can be set per-buffer (Emacs>=24.4).
636 (when (fboundp 'electric-indent-local-mode)
637 (setq c-electric-flag electric-indent-mode))
639 ;; ;; Put submode indicators onto minor-mode-alist, but only once.
640 ;; (or (assq 'c-submode-indicators minor-mode-alist)
641 ;; (setq minor-mode-alist
642 ;; (cons '(c-submode-indicators c-submode-indicators)
643 ;; minor-mode-alist)))
644 (c-update-modeline)
646 ;; Install the functions that ensure that various internal caches
647 ;; don't become invalid due to buffer changes.
648 (when (featurep 'xemacs)
649 (make-local-hook 'before-change-functions)
650 (make-local-hook 'after-change-functions))
651 (add-hook 'before-change-functions 'c-before-change nil t)
652 (setq c-just-done-before-change nil)
653 (add-hook 'after-change-functions 'c-after-change nil t)
654 (when (boundp 'font-lock-extend-after-change-region-function)
655 (set (make-local-variable 'font-lock-extend-after-change-region-function)
656 'c-extend-after-change-region))) ; Currently (2009-05) used by all
657 ; languages with #define (C, C++,; ObjC), and by AWK.
659 (defun c-setup-doc-comment-style ()
660 "Initialize the variables that depend on the value of `c-doc-comment-style'."
661 (when (and (featurep 'font-lock)
662 (symbol-value 'font-lock-mode))
663 ;; Force font lock mode to reinitialize itself.
664 (font-lock-mode 0)
665 (font-lock-mode 1)))
667 ;; Buffer local variables defining the region to be fontified by a font lock
668 ;; after-change function. They are initialized in c-before-change to
669 ;; before-change-functions' BEG and END. `c-new-END' is amended in
670 ;; c-after-change with after-change-functions' BEG, END, and OLD-LEN. These
671 ;; variables may be modified by any before/after-change function, in
672 ;; particular by functions in `c-get-state-before-change-functions' and
673 ;; `c-before-font-lock-functions'.
674 (defvar c-new-BEG 0)
675 (make-variable-buffer-local 'c-new-BEG)
676 (defvar c-new-END 0)
677 (make-variable-buffer-local 'c-new-END)
678 ;; The following two variables record the values of `c-new-BEG' and
679 ;; `c-new-END' just after `c-new-END' has been adjusted for the length of text
680 ;; inserted or removed. They may be read by any after-change function (but
681 ;; should not be altered by one).
682 (defvar c-old-BEG 0)
683 (make-variable-buffer-local 'c-old-BEG)
684 (defvar c-old-END 0)
685 (make-variable-buffer-local 'c-old-END)
687 (defun c-common-init (&optional mode)
688 "Common initialization for all CC Mode modes.
689 In addition to the work done by `c-basic-common-init' and
690 `c-font-lock-init', this function sets up various other things as
691 customary in CC Mode modes but which aren't strictly necessary for CC
692 Mode to operate correctly.
694 MODE is the symbol for the mode to initialize, like `c-mode'. See
695 `c-basic-common-init' for details. It's only optional to be
696 compatible with old code; callers should always specify it."
698 (unless mode
699 ;; Called from an old third party package. The fallback is to
700 ;; initialize for C.
701 (c-init-language-vars-for 'c-mode))
703 (c-basic-common-init mode c-default-style)
704 (when mode
705 ;; Only initialize font locking if we aren't called from an old package.
706 (c-font-lock-init))
708 ;; Starting a mode is a sort of "change". So call the change functions...
709 (save-restriction
710 (widen)
711 (setq c-new-BEG (point-min))
712 (setq c-new-END (point-max))
713 (save-excursion
714 (let (before-change-functions after-change-functions)
715 (mapc (lambda (fn)
716 (funcall fn (point-min) (point-max)))
717 c-get-state-before-change-functions)
718 (mapc (lambda (fn)
719 (funcall fn (point-min) (point-max)
720 (- (point-max) (point-min))))
721 c-before-font-lock-functions))))
723 (set (make-local-variable 'outline-regexp) "[^#\n\^M]")
724 (set (make-local-variable 'outline-level) 'c-outline-level)
725 (set (make-local-variable 'add-log-current-defun-function)
726 (lambda ()
727 (or (c-cpp-define-name) (c-defun-name))))
728 (let ((rfn (assq mode c-require-final-newline)))
729 (when rfn
730 (if (boundp 'mode-require-final-newline)
731 (and (cdr rfn)
732 (set (make-local-variable 'require-final-newline)
733 mode-require-final-newline))
734 (set (make-local-variable 'require-final-newline) (cdr rfn))))))
736 (defun c-count-cfss (lv-alist)
737 ;; LV-ALIST is an alist like `file-local-variables-alist'. Count how many
738 ;; elements with the key `c-file-style' there are in it.
739 (let ((elt-ptr lv-alist) elt (cownt 0))
740 (while elt-ptr
741 (setq elt (car elt-ptr)
742 elt-ptr (cdr elt-ptr))
743 (when (eq (car elt) 'c-file-style)
744 (setq cownt (1+ cownt))))
745 cownt))
747 (defun c-before-hack-hook ()
748 "Set the CC Mode style and \"offsets\" when in the buffer's local variables.
749 They are set only when, respectively, the pseudo variables
750 `c-file-style' and `c-file-offsets' are present in the list.
752 This function is called from the hook `before-hack-local-variables-hook'."
753 (when c-buffer-is-cc-mode
754 (let ((mode-cons (assq 'mode file-local-variables-alist))
755 (stile (cdr (assq 'c-file-style file-local-variables-alist)))
756 (offsets (cdr (assq 'c-file-offsets file-local-variables-alist))))
757 (when mode-cons
758 (hack-one-local-variable (car mode-cons) (cdr mode-cons))
759 (setq file-local-variables-alist
760 (delq mode-cons file-local-variables-alist)))
761 (when stile
762 (or (stringp stile) (error "c-file-style is not a string"))
763 (if (boundp 'dir-local-variables-alist)
764 ;; Determine whether `c-file-style' was set in the file's local
765 ;; variables or in a .dir-locals.el (a directory setting).
766 (let ((cfs-in-file-and-dir-count
767 (c-count-cfss file-local-variables-alist))
768 (cfs-in-dir-count (c-count-cfss dir-local-variables-alist)))
769 (c-set-style stile
770 (and (= cfs-in-file-and-dir-count cfs-in-dir-count)
771 'keep-defaults)))
772 (c-set-style stile)))
773 (when offsets
774 (mapc
775 (lambda (langentry)
776 (let ((langelem (car langentry))
777 (offset (cdr langentry)))
778 (c-set-offset langelem offset)))
779 offsets)))))
781 (defun c-remove-any-local-eval-or-mode-variables ()
782 ;; If the buffer specifies `mode' or `eval' in its File Local Variable list
783 ;; or on the first line, remove all occurrences. See
784 ;; `c-postprocess-file-styles' for justification. There is no need to save
785 ;; point here, or even bother too much about the buffer contents. However,
786 ;; DON'T mess up the kill-ring.
788 ;; Most of the code here is derived from Emacs 21.3's `hack-local-variables'
789 ;; in files.el.
790 (goto-char (point-max))
791 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
792 (let (lv-point (prefix "") (suffix ""))
793 (when (let ((case-fold-search t))
794 (search-forward "Local Variables:" nil t))
795 (setq lv-point (point))
796 ;; The prefix is what comes before "local variables:" in its line.
797 ;; The suffix is what comes after "local variables:" in its line.
798 (skip-chars-forward " \t")
799 (or (eolp)
800 (setq suffix (buffer-substring (point)
801 (progn (end-of-line) (point)))))
802 (goto-char (match-beginning 0))
803 (or (bolp)
804 (setq prefix
805 (buffer-substring (point)
806 (progn (beginning-of-line) (point)))))
808 (while (search-forward-regexp
809 (concat "^[ \t]*"
810 (regexp-quote prefix)
811 "\\(mode\\|eval\\):.*"
812 (regexp-quote suffix)
813 "$")
814 nil t)
815 (forward-line 0)
816 (delete-region (point) (progn (forward-line) (point)))))
818 ;; Delete the first line, if we've got one, in case it contains a mode spec.
819 (unless (and lv-point
820 (progn (goto-char lv-point)
821 (forward-line 0)
822 (bobp)))
823 (goto-char (point-min))
824 (unless (eobp)
825 (delete-region (point) (progn (forward-line) (point)))))))
827 (defun c-postprocess-file-styles ()
828 "Function that post processes relevant file local variables in CC Mode.
829 Currently, this function simply applies any style and offset settings
830 found in the file's Local Variable list. It first applies any style
831 setting found in `c-file-style', then it applies any offset settings
832 it finds in `c-file-offsets'.
834 Note that the style variables are always made local to the buffer."
836 ;; apply file styles and offsets
837 (when c-buffer-is-cc-mode
838 (if (or c-file-style c-file-offsets)
839 (c-make-styles-buffer-local t))
840 (when c-file-style
841 (or (stringp c-file-style)
842 (error "c-file-style is not a string"))
843 (c-set-style c-file-style))
845 (and c-file-offsets
846 (mapc
847 (lambda (langentry)
848 (let ((langelem (car langentry))
849 (offset (cdr langentry)))
850 (c-set-offset langelem offset)))
851 c-file-offsets))
852 ;; Problem: The file local variable block might have explicitly set a
853 ;; style variable. The `c-set-style' or `mapcar' call might have
854 ;; overwritten this. So we run `hack-local-variables' again to remedy
855 ;; this. There are no guarantees this will work properly, particularly as
856 ;; we have no control over what the other hook functions on
857 ;; `hack-local-variables-hook' would have done. We now (2006/2/1) remove
858 ;; any `eval' or `mode' expressions before we evaluate again (see below).
859 ;; ACM, 2005/11/2.
861 ;; Problem (bug reported by Gustav Broberg): if one of the variables is
862 ;; `mode', this will invoke c-mode (etc.) again, setting up the style etc.
863 ;; We prevent this by temporarily removing `mode' from the Local Variables
864 ;; section.
865 (if (or c-file-style c-file-offsets)
866 (let ((hack-local-variables-hook nil) (inhibit-read-only t))
867 (c-tentative-buffer-changes
868 (c-remove-any-local-eval-or-mode-variables)
869 (hack-local-variables))
870 nil))))
872 (if (boundp 'before-hack-local-variables-hook)
873 (add-hook 'before-hack-local-variables-hook 'c-before-hack-hook)
874 (add-hook 'hack-local-variables-hook 'c-postprocess-file-styles))
876 (defmacro c-run-mode-hooks (&rest hooks)
877 ;; Emacs 21.1 has introduced a system with delayed mode hooks that
878 ;; requires the use of the new function `run-mode-hooks'.
879 (if (cc-bytecomp-fboundp 'run-mode-hooks)
880 `(run-mode-hooks ,@hooks)
881 `(progn ,@(mapcar (lambda (hook) `(run-hooks ,hook)) hooks))))
884 ;;; Change hooks, linking with Font Lock and electric-indent-mode.
886 (defun c-called-from-text-property-change-p ()
887 ;; Is the primitive which invoked `before-change-functions' or
888 ;; `after-change-functions' one which merely changes text properties? This
889 ;; function must be called directly from a member of one of the above hooks.
891 ;; In the following call, frame 0 is `backtrace-frame', frame 1 is
892 ;; `c-called-from-text-property-change-p', frame 2 is
893 ;; `c-before/after-change', frame 3 is the primitive invoking the change
894 ;; hook.
895 (memq (cadr (backtrace-frame 3))
896 '(put-text-property remove-list-of-text-properties)))
898 (defun c-depropertize-CPP (beg end)
899 ;; Remove the punctuation syntax-table text property from the CPP parts of
900 ;; (c-new-BEG c-new-END).
902 ;; This function is in the C/C++/ObjC values of
903 ;; `c-get-state-before-change-functions' and is called exclusively as a
904 ;; before change function.
905 (c-save-buffer-state (m-beg ss-found)
906 (goto-char c-new-BEG)
907 (while (and (< (point) beg)
908 (search-forward-regexp c-anchored-cpp-prefix beg 'bound))
909 (goto-char (match-beginning 1))
910 (setq m-beg (point))
911 (c-end-of-macro)
912 (c-clear-char-property-with-value m-beg (point) 'syntax-table '(1)))
914 (while (and (< (point) end)
915 (setq ss-found
916 (search-forward-regexp c-anchored-cpp-prefix end 'bound)))
917 (goto-char (match-beginning 1))
918 (setq m-beg (point))
919 (c-end-of-macro))
920 (if (and ss-found (> (point) end))
921 (c-clear-char-property-with-value m-beg (point) 'syntax-table '(1)))
923 (while (and (< (point) c-new-END)
924 (search-forward-regexp c-anchored-cpp-prefix c-new-END 'bound))
925 (goto-char (match-beginning 1))
926 (setq m-beg (point))
927 (c-end-of-macro)
928 (c-clear-char-property-with-value
929 m-beg (point) 'syntax-table '(1)))))
931 (defun c-extend-region-for-CPP (_beg _end)
932 ;; Adjust `c-new-BEG', `c-new-END' respectively to the beginning and end of
933 ;; any preprocessor construct they may be in.
935 ;; Point is undefined both before and after this function call; the buffer
936 ;; has already been widened, and match-data saved. The return value is
937 ;; meaningless.
939 ;; This function is in the C/C++/ObjC values of
940 ;; `c-get-state-before-change-functions' and is called exclusively as a
941 ;; before change function.
942 (goto-char c-new-BEG)
943 (c-beginning-of-macro)
944 (when (< (point) c-new-BEG)
945 (setq c-new-BEG (max (point) (c-determine-limit 500 c-new-BEG))))
947 (goto-char c-new-END)
948 (when (c-beginning-of-macro)
949 (c-end-of-macro)
950 (or (eobp) (forward-char))) ; Over the terminating NL which may be marked
951 ; with a c-cpp-delimiter category property
952 (when (> (point) c-new-END)
953 (setq c-new-END (min (point) (c-determine-+ve-limit 500 c-new-END)))))
955 (defun c-depropertize-new-text (beg end _old-len)
956 ;; Remove from the new text in (BEG END) any and all text properties which
957 ;; might interfere with CC Mode's proper working.
959 ;; This function is called exclusively as an after-change function. It
960 ;; appears in the value (for all languages) of
961 ;; `c-before-font-lock-functions'. The value of point is undefined both on
962 ;; entry and exit, and the return value has no significance. The parameters
963 ;; BEG, END, and OLD-LEN are the standard ones supplied to all after-change
964 ;; functions.
965 (c-save-buffer-state ()
966 (when (> end beg)
967 (c-clear-char-properties beg end 'syntax-table)
968 (c-clear-char-properties beg end 'category)
969 (c-clear-char-properties beg end 'c-is-sws)
970 (c-clear-char-properties beg end 'c-in-sws)
971 (c-clear-char-properties beg end 'c-type)
972 (c-clear-char-properties beg end 'c-awk-NL-prop))))
974 (defun c-extend-font-lock-region-for-macros (_begg endd _old-len)
975 ;; Extend the region (c-new-BEG c-new-END) to cover all (possibly changed)
976 ;; preprocessor macros; The return value has no significance.
978 ;; Point is undefined on both entry and exit to this function. The buffer
979 ;; will have been widened on entry.
981 ;; c-new-BEG has already been extended in `c-extend-region-for-CPP' so we
982 ;; don't need to repeat the exercise here.
984 ;; This function is in the C/C++/ObjC value of `c-before-font-lock-functions'.
985 (goto-char endd)
986 (when (c-beginning-of-macro)
987 (c-end-of-macro)
988 ;; Determine the region, (c-new-BEG c-new-END), which will get font
989 ;; locked. This restricts the region should there be long macros.
990 (setq c-new-END (min (max c-new-END (point))
991 (c-determine-+ve-limit 500 c-new-END)))))
993 (defun c-neutralize-CPP-line (beg end)
994 ;; BEG and END bound a region, typically a preprocessor line. Put a
995 ;; "punctuation" syntax-table property on syntactically obtrusive
996 ;; characters, ones which would interact syntactically with stuff outside
997 ;; this region.
999 ;; These are unmatched string delimiters, or unmatched
1000 ;; parens/brackets/braces. An unclosed comment is regarded as valid, NOT
1001 ;; obtrusive.
1002 (save-excursion
1003 (let (s)
1004 (while
1005 (progn
1006 (setq s (parse-partial-sexp beg end -1))
1007 (cond
1008 ((< (nth 0 s) 0) ; found an unmated ),},]
1009 (c-put-char-property (1- (point)) 'syntax-table '(1))
1011 ((nth 3 s) ; In a string
1012 (c-put-char-property (nth 8 s) 'syntax-table '(1))
1014 ((> (nth 0 s) 0) ; In a (,{,[
1015 (c-put-char-property (nth 1 s) 'syntax-table '(1))
1017 (t nil)))))))
1019 (defun c-neutralize-syntax-in-and-mark-CPP (_begg _endd _old-len)
1020 ;; (i) "Neutralize" every preprocessor line wholly or partially in the
1021 ;; changed region. "Restore" lines which were CPP lines before the change
1022 ;; and are no longer so.
1024 ;; (ii) Mark each CPP construct by placing a `category' property value
1025 ;; `c-cpp-delimiter' at its start and end. The marked characters are the
1026 ;; opening # and usually the terminating EOL, but sometimes the character
1027 ;; before a comment delimiter.
1029 ;; That is, set syntax-table properties on characters that would otherwise
1030 ;; interact syntactically with those outside the CPP line(s).
1032 ;; This function is called from an after-change function, BEGG ENDD and
1033 ;; OLD-LEN being the standard parameters. It prepares the buffer for font
1034 ;; locking, hence must get called before `font-lock-after-change-function'.
1036 ;; Point is undefined both before and after this function call, the buffer
1037 ;; has been widened, and match-data saved. The return value is ignored.
1039 ;; This function is in the C/C++/ObjC value of `c-before-font-lock-functions'.
1041 ;; Note: SPEED _MATTERS_ IN THIS FUNCTION!!!
1043 ;; This function might make hidden buffer changes.
1044 (c-save-buffer-state (limits)
1045 ;; Clear 'syntax-table properties "punctuation":
1046 ;; (c-clear-char-property-with-value c-new-BEG c-new-END 'syntax-table '(1))
1048 ;; CPP "comment" markers:
1049 (if (eval-when-compile (memq 'category-properties c-emacs-features));Emacs.
1050 (c-clear-char-property-with-value
1051 c-new-BEG c-new-END 'category 'c-cpp-delimiter))
1052 ;; FIXME!!! What about the "<" and ">" category properties? 2009-11-16
1054 ;; Add needed properties to each CPP construct in the region.
1055 (goto-char c-new-BEG)
1056 (if (setq limits (c-literal-limits)) ; Go past any literal.
1057 (goto-char (cdr limits)))
1058 (skip-chars-backward " \t")
1059 (let ((pps-position (point)) pps-state mbeg)
1060 (while (and (< (point) c-new-END)
1061 (search-forward-regexp c-anchored-cpp-prefix c-new-END t))
1062 ;; If we've found a "#" inside a macro/string/comment, ignore it.
1063 (unless
1064 (or (save-excursion
1065 (goto-char (match-beginning 0))
1066 (let ((here (point)))
1067 (and (save-match-data (c-beginning-of-macro))
1068 (< (point) here))))
1069 (progn
1070 (setq pps-state
1071 (parse-partial-sexp pps-position (point) nil nil pps-state)
1072 pps-position (point))
1073 (or (nth 3 pps-state) ; in a string?
1074 (and (nth 4 pps-state)
1075 (not (eq (nth 7 pps-state) 'syntax-table)))))) ; in a comment?
1076 (goto-char (match-beginning 1))
1077 (setq mbeg (point))
1078 (if (> (c-no-comment-end-of-macro) mbeg)
1079 (progn
1080 (c-neutralize-CPP-line mbeg (point)) ; "punctuation" properties
1081 (if (eval-when-compile
1082 (memq 'category-properties c-emacs-features)) ;Emacs.
1083 (c-set-cpp-delimiters mbeg (point)))) ; "comment" markers
1084 (forward-line)) ; no infinite loop with, e.g., "#//"
1085 )))))
1087 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1088 ;; Parsing of quotes.
1090 ;; Valid digit separators in numbers will get the syntax-table "punctuation"
1091 ;; property, '(1), and also the text property `c-digit-separator' value t.
1093 ;; Invalid other quotes (i.e. those not validly bounding a single character,
1094 ;; or escaped character) will get the syntax-table "punctuation" property,
1095 ;; '(1), too.
1097 ;; Note that, for convenience, these properties are applied even inside
1098 ;; comments and strings.
1100 (defconst c-maybe-quoted-number-head
1101 (concat
1102 "\\(0\\("
1103 "\\([Xx]\\([0-9a-fA-F]\\('[0-9a-fA-F]\\|[0-9a-fA-F]\\)*'?\\)?\\)"
1104 "\\|"
1105 "\\([Bb]\\([01]\\('[01]\\|[01]\\)*'?\\)?\\)"
1106 "\\|"
1107 "\\('[0-7]\\|[0-7]\\)*'?"
1108 "\\)"
1109 "\\|"
1110 "[1-9]\\('[0-9]\\|[0-9]\\)*'?"
1111 "\\)")
1112 "Regexp matching the head of a numeric literal, including with digit separators.")
1114 (defun c-quoted-number-head-before-point ()
1115 ;; Return non-nil when the head of a possibly quoted number is found
1116 ;; immediately before point. The value returned in this case is the buffer
1117 ;; position of the start of the head. That position is also in
1118 ;; (match-beginning 0).
1119 (when c-has-quoted-numbers
1120 (save-excursion
1121 (let ((here (point))
1122 found)
1123 (skip-chars-backward "0-9a-fA-F'")
1124 (if (and (memq (char-before) '(?x ?X))
1125 (eq (char-before (1- (point))) ?0))
1126 (backward-char 2))
1127 (while
1128 (and
1129 (setq found
1130 (search-forward-regexp c-maybe-quoted-number-head here t))
1131 (< found here)))
1132 (and (eq found here) (match-beginning 0))))))
1134 (defconst c-maybe-quoted-number-tail
1135 (concat
1136 "\\("
1137 "\\([xX']?[0-9a-fA-F]\\('[0-9a-fA-F]\\|[0-9a-fA-F]\\)*\\)"
1138 "\\|"
1139 "\\([bB']?[01]\\('[01]\\|[01]\\)*\\)"
1140 "\\|"
1141 "\\('?[0-9]\\('[0-9]\\|[0-9]\\)*\\)"
1142 "\\)")
1143 "Regexp matching the tail of a numeric literal, including with digit separators.
1144 Note that this is a strict tail, so won't match, e.g. \"0x....\".")
1146 (defun c-quoted-number-tail-after-point ()
1147 ;; Return non-nil when a proper tail of a possibly quoted number is found
1148 ;; immediately after point. The value returned in this case is the buffer
1149 ;; position of the end of the tail. That position is also in (match-end 0).
1150 (when c-has-quoted-numbers
1151 (and (looking-at c-maybe-quoted-number-tail)
1152 (match-end 0))))
1154 (defconst c-maybe-quoted-number
1155 (concat
1156 "\\(0\\("
1157 "\\([Xx][0-9a-fA-F]\\('[0-9a-fA-F]\\|[0-9a-fA-F]\\)*\\)"
1158 "\\|"
1159 "\\([Bb][01]\\('[01]\\|[01]\\)*\\)"
1160 "\\|"
1161 "\\('[0-7]\\|[0-7]\\)*"
1162 "\\)"
1163 "\\|"
1164 "[1-9]\\('[0-9]\\|[0-9]\\)*"
1165 "\\)")
1166 "Regexp matching a numeric literal, including with digit separators.")
1168 (defun c-quoted-number-straddling-point ()
1169 ;; Return non-nil if a definitely quoted number starts before point and ends
1170 ;; after point. In this case the number is bounded by (match-beginning 0)
1171 ;; and (match-end 0).
1172 (when c-has-quoted-numbers
1173 (save-excursion
1174 (let ((here (point))
1175 (bound (progn (skip-chars-forward "0-9a-fA-F'") (point))))
1176 (goto-char here)
1177 (when (< (skip-chars-backward "0-9a-fA-F'") 0)
1178 (if (and (memq (char-before) '(?x ?X))
1179 (eq (char-before (1- (point))) ?0))
1180 (backward-char 2))
1181 (while (and (search-forward-regexp c-maybe-quoted-number bound t)
1182 (<= (match-end 0) here)))
1183 (and (< (match-beginning 0) here)
1184 (> (match-end 0) here)
1185 (save-match-data
1186 (goto-char (match-beginning 0))
1187 (save-excursion (search-forward "'" (match-end 0) t)))))))))
1189 (defun c-parse-quotes-before-change (beg end)
1190 ;; This function analyzes 's near the region (c-new-BEG c-new-END), amending
1191 ;; those two variables as needed to include 's into that region when they
1192 ;; might be syntactically relevant to the change in progress.
1194 ;; Having amended that region, the function removes pertinent text
1195 ;; properties (syntax-table properties with value '(1) and c-digit-separator
1196 ;; props with value t) from 's in it. This operation is performed even
1197 ;; within strings and comments.
1199 ;; This function is called exclusively as a before-change function via the
1200 ;; variable `c-get-state-before-change-functions'.
1201 (c-save-buffer-state ()
1202 (goto-char c-new-BEG)
1203 ;; We need to scan for 's from the BO (logical) line.
1204 (beginning-of-line)
1205 (while (eq (char-before (1- (point))) ?\\)
1206 (beginning-of-line 0))
1207 (while (and (< (point) c-new-BEG)
1208 (search-forward "'" c-new-BEG t))
1209 (cond
1210 ((c-quoted-number-straddling-point)
1211 (goto-char (match-end 0))
1212 (if (> (match-end 0) c-new-BEG)
1213 (setq c-new-BEG (match-beginning 0))))
1214 ((c-quoted-number-head-before-point)
1215 (if (>= (point) c-new-BEG)
1216 (setq c-new-BEG (match-beginning 0))))
1217 ((looking-at "\\([^'\\]\\|\\\\.\\)'")
1218 (goto-char (match-end 0))
1219 (if (> (match-end 0) c-new-BEG)
1220 (setq c-new-BEG (1- (match-beginning 0)))))
1221 ((or (>= (point) (1- c-new-BEG))
1222 (and (eq (point) (- c-new-BEG 2))
1223 (eq (char-after) ?\\)))
1224 (setq c-new-BEG (1- (point))))
1225 (t nil)))
1227 (goto-char c-new-END)
1228 ;; We will scan from the BO (logical) line.
1229 (beginning-of-line)
1230 (while (eq (char-before (1- (point))) ?\\)
1231 (beginning-of-line 0))
1232 (while (and (< (point) c-new-END)
1233 (search-forward "'" c-new-END t))
1234 (cond
1235 ((c-quoted-number-straddling-point)
1236 (goto-char (match-end 0))
1237 (if (> (match-end 0) c-new-END)
1238 (setq c-new-END (match-end 0))))
1239 ((c-quoted-number-tail-after-point)
1240 (goto-char (match-end 0))
1241 (if (> (match-end 0) c-new-END)
1242 (setq c-new-END (match-end 0))))
1243 ((looking-at "\\([^'\\]\\|\\\\.\\)'")
1244 (goto-char (match-end 0))
1245 (if (> (match-end 0) c-new-END)
1246 (setq c-new-END (match-end 0))))
1247 (t nil)))
1248 ;; Having reached c-new-END, handle any 's after it whose context may be
1249 ;; changed by the current buffer change.
1250 (goto-char c-new-END)
1251 (cond
1252 ((c-quoted-number-tail-after-point)
1253 (setq c-new-END (match-end 0)))
1254 ((looking-at
1255 "\\(\\\\.\\|.\\)?\\('\\([^'\\]\\|\\\\.\\)\\)*'")
1256 (setq c-new-END (match-end 0))))
1258 ;; Remove the '(1) syntax-table property from any "'"s within (c-new-BEG
1259 ;; c-new-END).
1260 (goto-char c-new-BEG)
1261 (when (c-search-forward-char-property-with-value-on-char
1262 'syntax-table '(1) ?\' c-new-END)
1263 (c-invalidate-state-cache (1- (point)))
1264 (c-truncate-semi-nonlit-pos-cache (1- (point)))
1265 (c-clear-char-property-with-value-on-char
1266 (1- (point)) c-new-END
1267 'syntax-table '(1)
1269 ;; Remove the c-digit-separator text property from the same "'"s.
1270 (when c-has-quoted-numbers
1271 (c-clear-char-property-with-value-on-char
1272 (1- (point)) c-new-END
1273 'c-digit-separator t
1274 ?')))))
1276 (defun c-parse-quotes-after-change (beg end old-len)
1277 ;; This function applies syntax-table properties (value '(1)) and
1278 ;; c-digit-separator properties as needed to 's within the range (c-new-BEG
1279 ;; c-new-END). This operation is performed even within strings and
1280 ;; comments.
1282 ;; This function is called exclusively as an after-change function via the
1283 ;; variable `c-before-font-lock-functions'.
1284 (c-save-buffer-state (num-beg num-end)
1285 ;; Apply the needed syntax-table and c-digit-separator text properties to
1286 ;; quotes.
1287 (save-restriction
1288 (goto-char c-new-BEG)
1289 (while (and (< (point) c-new-END)
1290 (search-forward "'" c-new-END 'limit))
1291 (cond ((and (eq (char-before (1- (point))) ?\\)
1292 ;; Check we've got an odd number of \s, here.
1293 (save-excursion
1294 (backward-char)
1295 (eq (logand (skip-chars-backward "\\\\") 1) 1)))) ; not a real '.
1296 ((c-quoted-number-straddling-point)
1297 (setq num-beg (match-beginning 0)
1298 num-end (match-end 0))
1299 (c-invalidate-state-cache num-beg)
1300 (c-truncate-semi-nonlit-pos-cache num-beg)
1301 (c-put-char-properties-on-char num-beg num-end
1302 'syntax-table '(1) ?')
1303 (c-put-char-properties-on-char num-beg num-end
1304 'c-digit-separator t ?')
1305 (goto-char num-end))
1306 ((looking-at "\\([^\\']\\|\\\\.\\)'") ; balanced quoted expression.
1307 (goto-char (match-end 0)))
1309 (c-invalidate-state-cache (1- (point)))
1310 (c-truncate-semi-nonlit-pos-cache (1- (point)))
1311 (c-put-char-property (1- (point)) 'syntax-table '(1))))
1312 ;; Prevent the next `c-quoted-number-straddling-point' getting
1313 ;; confused by already processed single quotes.
1314 (narrow-to-region (point) (point-max))))))
1316 (defun c-before-change (beg end)
1317 ;; Function to be put on `before-change-functions'. Primarily, this calls
1318 ;; the language dependent `c-get-state-before-change-functions'. It is
1319 ;; otherwise used only to remove stale entries from the `c-found-types'
1320 ;; cache, and to record entries which a `c-after-change' function might
1321 ;; confirm as stale.
1323 ;; Note that this function must be FAST rather than accurate. Note
1324 ;; also that it only has any effect when font locking is enabled.
1325 ;; We exploit this by checking for font-lock-*-face instead of doing
1326 ;; rigorous syntactic analysis.
1328 ;; If either change boundary is wholly inside an identifier, delete
1329 ;; it/them from the cache. Don't worry about being inside a string
1330 ;; or a comment - "wrongly" removing a symbol from `c-found-types'
1331 ;; isn't critical.
1332 (unless (or (c-called-from-text-property-change-p)
1333 c-just-done-before-change) ; guard against a spurious second
1334 ; invocation of before-change-functions.
1335 (setq c-just-done-before-change t)
1336 ;; (c-new-BEG c-new-END) will be the region to fontify.
1337 (setq c-new-BEG beg c-new-END end)
1338 (setq c-maybe-stale-found-type nil)
1339 (save-restriction
1340 (save-match-data
1341 (widen)
1342 (save-excursion
1343 ;; Are we inserting/deleting stuff in the middle of an identifier?
1344 (c-unfind-enclosing-token beg)
1345 (c-unfind-enclosing-token end)
1346 ;; Are we coalescing two tokens together, e.g. "fo o" -> "foo"?
1347 (when (< beg end)
1348 (c-unfind-coalesced-tokens beg end))
1349 (c-invalidate-sws-region-before end)
1350 ;; Are we (potentially) disrupting the syntactic context which
1351 ;; makes a type a type? E.g. by inserting stuff after "foo" in
1352 ;; "foo bar;", or before "foo" in "typedef foo *bar;"?
1354 ;; We search for appropriate c-type properties "near" the change.
1355 ;; First, find an appropriate boundary for this property search.
1356 (let (lim
1357 type type-pos
1358 marked-id term-pos
1359 (end1
1360 (or (and (eq (get-text-property end 'face)
1361 'font-lock-comment-face)
1362 (previous-single-property-change end 'face))
1363 end)))
1364 (when (>= end1 beg) ; Don't hassle about changes entirely in comments.
1365 ;; Find a limit for the search for a `c-type' property
1366 (while
1367 (and (/= (skip-chars-backward "^;{}") 0)
1368 (> (point) (point-min))
1369 (memq (c-get-char-property (1- (point)) 'face)
1370 '(font-lock-comment-face font-lock-string-face))))
1371 (setq lim (max (point-min) (1- (point))))
1373 ;; Look for the latest `c-type' property before end1
1374 (when (and (> end1 (point-min))
1375 (setq type-pos
1376 (if (get-text-property (1- end1) 'c-type)
1377 end1
1378 (previous-single-property-change end1 'c-type
1379 nil lim))))
1380 (setq type (get-text-property (max (1- type-pos) lim) 'c-type))
1382 (when (memq type '(c-decl-id-start c-decl-type-start))
1383 ;; Get the identifier, if any, that the property is on.
1384 (goto-char (1- type-pos))
1385 (setq marked-id
1386 (when (looking-at "\\(\\sw\\|\\s_\\)")
1387 (c-beginning-of-current-token)
1388 (buffer-substring-no-properties (point) type-pos)))
1390 (goto-char end1)
1391 (skip-chars-forward "^;{}") ;FIXME!!! loop for comment, maybe
1392 (setq lim (point))
1393 (setq term-pos
1394 (or (c-next-single-property-change end 'c-type nil lim)
1395 lim))
1396 (setq c-maybe-stale-found-type
1397 (list type marked-id
1398 type-pos term-pos
1399 (buffer-substring-no-properties type-pos
1400 term-pos)
1401 (buffer-substring-no-properties beg end)))))))
1403 (if c-get-state-before-change-functions
1404 (mapc (lambda (fn)
1405 (funcall fn beg end))
1406 c-get-state-before-change-functions))
1408 ;; The following must be done here rather than in `c-after-change' because
1409 ;; newly inserted parens would foul up the invalidation algorithm.
1410 (c-invalidate-state-cache beg)))
1412 (defvar c-in-after-change-fontification nil)
1413 (make-variable-buffer-local 'c-in-after-change-fontification)
1414 ;; A flag to prevent region expanding stuff being done twice for after-change
1415 ;; fontification.
1417 (defun c-after-change (beg end old-len)
1418 ;; Function put on `after-change-functions' to adjust various caches
1419 ;; etc. Prefer speed to finesse here, since there will be an order
1420 ;; of magnitude more calls to this function than any of the
1421 ;; functions that use the caches.
1423 ;; Note that care must be taken so that this is called before any
1424 ;; font-lock callbacks since we might get calls to functions using
1425 ;; these caches from inside them, and we must thus be sure that this
1426 ;; has already been executed.
1428 ;; This calls the language variable c-before-font-lock-functions, if non nil.
1429 ;; This typically sets `syntax-table' properties.
1431 ;; We can sometimes get two consecutive calls to `after-change-functions'
1432 ;; without an intervening call to `before-change-functions' when reverting
1433 ;; the buffer (see bug #24094). Whatever the cause, assume that the entire
1434 ;; buffer has changed.
1435 (when (not c-just-done-before-change)
1436 (save-restriction
1437 (widen)
1438 (c-before-change (point-min) (point-max))
1439 (setq beg (point-min)
1440 end (point-max)
1441 old-len (- end beg))))
1443 ;; (c-new-BEG c-new-END) will be the region to fontify. It may become
1444 ;; larger than (beg end).
1445 (setq c-new-END (- (+ c-new-END (- end beg)) old-len))
1446 (setq c-old-BEG c-new-BEG c-old-END c-new-END)
1448 (unless (c-called-from-text-property-change-p)
1449 (setq c-just-done-before-change nil)
1450 (c-save-buffer-state (case-fold-search)
1451 ;; When `combine-after-change-calls' is used we might get calls
1452 ;; with regions outside the current narrowing. This has been
1453 ;; observed in Emacs 20.7.
1454 (save-restriction
1455 (save-match-data ; c-recognize-<>-arglists changes match-data
1456 (widen)
1458 (when (> end (point-max))
1459 ;; Some emacsen might return positions past the end. This has been
1460 ;; observed in Emacs 20.7 when rereading a buffer changed on disk
1461 ;; (haven't been able to minimize it, but Emacs 21.3 appears to
1462 ;; work).
1463 (setq end (point-max))
1464 (when (> beg end)
1465 (setq beg end)))
1467 ;; C-y is capable of spuriously converting category properties
1468 ;; c-</>-as-paren-syntax and c-cpp-delimiter into hard syntax-table
1469 ;; properties. Remove these when it happens.
1470 (when (eval-when-compile (memq 'category-properties c-emacs-features))
1471 (c-save-buffer-state ()
1472 (c-clear-char-property-with-value beg end 'syntax-table
1473 c-<-as-paren-syntax)
1474 (c-clear-char-property-with-value beg end 'syntax-table
1475 c->-as-paren-syntax)
1476 (c-clear-char-property-with-value beg end 'syntax-table nil)))
1478 (c-trim-found-types beg end old-len) ; maybe we don't need all of these.
1479 (c-invalidate-sws-region-after beg end old-len)
1480 ;; (c-invalidate-state-cache beg) ; moved to `c-before-change'.
1481 (c-invalidate-find-decl-cache beg)
1483 (when c-recognize-<>-arglists
1484 (c-after-change-check-<>-operators beg end))
1486 (setq c-in-after-change-fontification t)
1487 (save-excursion
1488 (mapc (lambda (fn)
1489 (funcall fn beg end old-len))
1490 c-before-font-lock-functions)))))))
1492 (defun c-fl-decl-start (pos)
1493 ;; If the beginning of the line containing POS is in the middle of a "local"
1494 ;; declaration, return the beginning of that declaration. Otherwise return
1495 ;; nil. Note that declarations, in this sense, can be nested. (A local
1496 ;; declaration is one which does not start outside of struct braces (and
1497 ;; similar) enclosing POS. Brace list braces here are not "similar".
1499 ;; This function is called indirectly from font locking stuff - either from
1500 ;; c-after-change (to prepare for after-change font-locking) or from font
1501 ;; lock context (etc.) fontification.
1502 (goto-char pos)
1503 (let ((lit-start (c-literal-start))
1504 (new-pos pos)
1505 capture-opener
1506 bod-lim bo-decl)
1507 (goto-char (c-point 'bol new-pos))
1508 (when lit-start ; Comment or string.
1509 (goto-char lit-start))
1510 (setq bod-lim (c-determine-limit 500))
1512 ;; In C++ Mode, first check if we are within a (possibly nested) lambda
1513 ;; form capture list.
1514 (when (c-major-mode-is 'c++-mode)
1515 (let ((paren-state (c-parse-state))
1516 opener)
1517 (save-excursion
1518 (while (setq opener (c-pull-open-brace paren-state))
1519 (goto-char opener)
1520 (if (c-looking-at-c++-lambda-capture-list)
1521 (setq capture-opener (point)))))))
1523 (while
1524 ;; Go to a less nested declaration each time round this loop.
1525 (and
1526 (c-syntactic-skip-backward "^;{}" bod-lim t)
1527 (> (point) bod-lim)
1528 (progn (c-forward-syntactic-ws)
1529 (setq bo-decl (point))
1530 (or (not (looking-at c-protection-key))
1531 (c-forward-keyword-clause 1)))
1532 (progn
1533 ;; Are we looking at a keyword such as "template" or
1534 ;; "typedef" which can decorate a type, or the type itself?
1535 (when (or (looking-at c-prefix-spec-kwds-re)
1536 (c-forward-type t))
1537 ;; We've found another candidate position.
1538 (setq new-pos (min new-pos bo-decl))
1539 (goto-char bo-decl))
1541 ;; Try and go out a level to search again.
1542 (progn
1543 (c-backward-syntactic-ws bod-lim)
1544 (and (> (point) bod-lim)
1545 (or (memq (char-before) '(?\( ?\[))
1546 (and (eq (char-before) ?\<)
1547 (eq (c-get-char-property
1548 (1- (point)) 'syntax-table)
1549 c-<-as-paren-syntax))
1550 (and (eq (char-before) ?{)
1551 (save-excursion
1552 (backward-char)
1553 (consp (c-looking-at-or-maybe-in-bracelist))))
1555 (not (bobp)))
1556 (backward-char)) ; back over (, [, <.
1557 (when (and capture-opener (< capture-opener new-pos))
1558 (setq new-pos capture-opener))
1559 (and (/= new-pos pos) new-pos)))
1561 (defun c-fl-decl-end (pos)
1562 ;; If POS is inside a declarator, return the end of the token that follows
1563 ;; the declarator, otherwise return nil.
1564 (goto-char pos)
1565 (let ((lit-start (c-literal-start))
1566 pos1)
1567 (if lit-start (goto-char lit-start))
1568 (c-backward-syntactic-ws)
1569 (when (setq pos1 (c-on-identifier))
1570 (goto-char pos1)
1571 (let ((lim (save-excursion
1572 (and (c-beginning-of-macro)
1573 (progn (c-end-of-macro) (point))))))
1574 (when (and (c-forward-declarator lim)
1575 (or (not (eq (char-after) ?\())
1576 (c-go-list-forward nil lim))
1577 (eq (c-forward-token-2 1 nil lim) 0))
1578 (c-backward-syntactic-ws)
1579 (point))))))
1581 (defun c-change-expand-fl-region (_beg _end _old-len)
1582 ;; Expand the region (c-new-BEG c-new-END) to an after-change font-lock
1583 ;; region. This will usually be the smallest sequence of whole lines
1584 ;; containing `c-new-BEG' and `c-new-END', but if `c-new-BEG' is in a
1585 ;; "local" declaration (see `c-fl-decl-start') the beginning of this is used
1586 ;; as the lower bound.
1588 ;; This is called from an after-change-function, but the parameters BEG END
1589 ;; and OLD-LEN are not used.
1590 (if font-lock-mode
1591 (setq c-new-BEG
1592 (or (c-fl-decl-start c-new-BEG) (c-point 'bol c-new-BEG))
1593 c-new-END
1594 (or (c-fl-decl-end c-new-END)
1595 (c-point 'bonl c-new-END)))))
1597 (defun c-context-expand-fl-region (beg end)
1598 ;; Return a cons (NEW-BEG . NEW-END), where NEW-BEG is the beginning of a
1599 ;; "local" declaration containing BEG (see `c-fl-decl-start') or BOL BEG is
1600 ;; in. NEW-END is beginning of the line after the one END is in.
1601 (c-save-buffer-state ()
1602 (cons (or (c-fl-decl-start beg) (c-point 'bol beg))
1603 (or (c-fl-decl-end end) (c-point 'bonl (1- end))))))
1605 (defun c-before-context-fl-expand-region (beg end)
1606 ;; Expand the region (BEG END) as specified by
1607 ;; `c-before-context-fontification-functions'. Return a cons of the bounds
1608 ;; of the new region.
1609 (save-restriction
1610 (widen)
1611 (save-excursion
1612 (let ((new-beg beg) (new-end end)
1613 (new-region (cons beg end)))
1614 (mapc (lambda (fn)
1615 (setq new-region (funcall fn new-beg new-end))
1616 (setq new-beg (car new-region) new-end (cdr new-region)))
1617 c-before-context-fontification-functions)
1618 new-region))))
1620 (defun c-font-lock-fontify-region (beg end &optional verbose)
1621 ;; Effectively advice around `font-lock-fontify-region' which extends the
1622 ;; region (BEG END), for example, to avoid context fontification chopping
1623 ;; off the start of the context. Do not extend the region if it's already
1624 ;; been done (i.e. from an after-change fontification. An example (C++)
1625 ;; where the chopping off used to happen is this:
1627 ;; template <typename T>
1630 ;; void myfunc(T* p) {}
1632 ;; Type a space in the first blank line, and the fontification of the next
1633 ;; line was fouled up by context fontification.
1634 (let (new-beg new-end new-region case-fold-search)
1635 (if (and c-in-after-change-fontification
1636 (< beg c-new-END) (> end c-new-BEG))
1637 ;; Region and the latest after-change fontification region overlap.
1638 ;; Determine the upper and lower bounds of our adjusted region
1639 ;; separately.
1640 (progn
1641 (if (<= beg c-new-BEG)
1642 (setq c-in-after-change-fontification nil))
1643 (setq new-beg
1644 (if (and (>= beg (c-point 'bol c-new-BEG))
1645 (<= beg c-new-BEG))
1646 ;; Either jit-lock has accepted `c-new-BEG', or has
1647 ;; (probably) extended the change region spuriously to
1648 ;; BOL, which position likely has a syntactically
1649 ;; different position. To ensure correct fontification,
1650 ;; we start at `c-new-BEG', assuming any characters to the
1651 ;; left of `c-new-BEG' on the line do not require
1652 ;; fontification.
1653 c-new-BEG
1654 (setq new-region (c-before-context-fl-expand-region beg end)
1655 new-end (cdr new-region))
1656 (car new-region)))
1657 (setq new-end
1658 (if (and (>= end (c-point 'bol c-new-END))
1659 (<= end c-new-END))
1660 c-new-END
1661 (or new-end
1662 (cdr (c-before-context-fl-expand-region beg end))))))
1663 ;; Context (etc.) fontification.
1664 (setq new-region (c-before-context-fl-expand-region beg end)
1665 new-beg (car new-region) new-end (cdr new-region)))
1666 (funcall (default-value 'font-lock-fontify-region-function)
1667 new-beg new-end verbose)))
1669 (defun c-after-font-lock-init ()
1670 ;; Put on `font-lock-mode-hook'. This function ensures our after-change
1671 ;; function will get executed before the font-lock one.
1672 (when (memq #'c-after-change after-change-functions)
1673 (remove-hook 'after-change-functions #'c-after-change t)
1674 (add-hook 'after-change-functions #'c-after-change nil t)))
1676 (defun c-font-lock-init ()
1677 "Set up the font-lock variables for using the font-lock support in CC Mode.
1678 This does not load the font-lock package. Use after
1679 `c-basic-common-init' and after cc-fonts has been loaded.
1680 This function is called from `c-common-init', once per mode initialization."
1682 (set (make-local-variable 'font-lock-defaults)
1683 `(,(if (c-major-mode-is 'awk-mode)
1684 ;; awk-mode currently has only one font lock level.
1685 'awk-font-lock-keywords
1686 (mapcar 'c-mode-symbol
1687 '("font-lock-keywords" "font-lock-keywords-1"
1688 "font-lock-keywords-2" "font-lock-keywords-3")))
1689 nil nil
1690 ,c-identifier-syntax-modifications
1691 c-beginning-of-syntax
1692 (font-lock-mark-block-function
1693 . c-mark-function)))
1695 ;; Prevent `font-lock-default-fontify-region' extending the region it will
1696 ;; fontify to whole lines by removing `font-lock-extend-region-wholelines'
1697 ;; from `font-lock-extend-region-functions'. (Emacs only). This fixes
1698 ;; Emacs bug #19669.
1699 (when (boundp 'font-lock-extend-region-functions)
1700 (setq font-lock-extend-region-functions
1701 (delq 'font-lock-extend-region-wholelines
1702 font-lock-extend-region-functions)))
1704 (make-local-variable 'font-lock-fontify-region-function)
1705 (setq font-lock-fontify-region-function 'c-font-lock-fontify-region)
1707 (if (featurep 'xemacs)
1708 (make-local-hook 'font-lock-mode-hook))
1709 (add-hook 'font-lock-mode-hook 'c-after-font-lock-init nil t))
1711 ;; Emacs 22 and later.
1712 (defun c-extend-after-change-region (beg end _old-len)
1713 "Extend the region to be fontified, if necessary."
1714 ;; Note: the parameter OLD-LEN is ignored here. This somewhat indirect
1715 ;; implementation exists because it is minimally different from the
1716 ;; stand-alone CC Mode which, lacking
1717 ;; font-lock-extend-after-change-region-function, is forced to use advice
1718 ;; instead.
1720 ;; Of the seven CC Mode languages, currently (2009-05) only C, C++, Objc
1721 ;; (the languages with #define) and AWK Mode make non-null use of this
1722 ;; function.
1723 (when (eq font-lock-support-mode 'jit-lock-mode)
1724 (save-restriction
1725 (widen)
1726 (c-save-buffer-state () ; Protect the undo-list from put-text-property.
1727 (if (< c-new-BEG beg)
1728 (put-text-property c-new-BEG beg 'fontified nil))
1729 (if (> c-new-END end)
1730 (put-text-property end c-new-END 'fontified nil)))))
1731 (cons c-new-BEG c-new-END))
1733 ;; Emacs < 22 and XEmacs
1734 (defmacro c-advise-fl-for-region (function)
1735 `(defadvice ,function (before get-awk-region activate)
1736 ;; Make sure that any string/regexp is completely font-locked.
1737 (when c-buffer-is-cc-mode
1738 (save-excursion
1739 (ad-set-arg 1 c-new-END) ; end
1740 (ad-set-arg 0 c-new-BEG))))) ; beg
1742 (unless (boundp 'font-lock-extend-after-change-region-function)
1743 (c-advise-fl-for-region font-lock-after-change-function)
1744 (c-advise-fl-for-region jit-lock-after-change)
1745 (c-advise-fl-for-region lazy-lock-defer-rest-after-change)
1746 (c-advise-fl-for-region lazy-lock-defer-line-after-change))
1748 ;; Connect up to `electric-indent-mode' (Emacs 24.4 and later).
1749 (defun c-electric-indent-mode-hook ()
1750 ;; Emacs has en/disabled `electric-indent-mode'. Propagate this through to
1751 ;; each CC Mode buffer.
1752 (mapc (lambda (buf)
1753 (with-current-buffer buf
1754 (when c-buffer-is-cc-mode
1755 ;; Don't use `c-toggle-electric-state' here due to recursion.
1756 (setq c-electric-flag electric-indent-mode)
1757 (c-update-modeline))))
1758 (buffer-list)))
1760 (defun c-electric-indent-local-mode-hook ()
1761 ;; Emacs has en/disabled `electric-indent-local-mode' for this buffer.
1762 ;; Propagate this through to this buffer's value of `c-electric-flag'
1763 (when c-buffer-is-cc-mode
1764 (setq c-electric-flag electric-indent-mode)
1765 (c-update-modeline)))
1768 ;; Support for C
1770 (defvar c-mode-syntax-table
1771 (funcall (c-lang-const c-make-mode-syntax-table c))
1772 "Syntax table used in c-mode buffers.")
1774 (c-define-abbrev-table 'c-mode-abbrev-table
1775 '(("else" "else" c-electric-continued-statement 0)
1776 ("while" "while" c-electric-continued-statement 0))
1777 "Abbreviation table used in c-mode buffers.")
1779 (defvar c-mode-map
1780 (let ((map (c-make-inherited-keymap)))
1781 map)
1782 "Keymap used in c-mode buffers.")
1783 ;; Add bindings which are only useful for C.
1784 (define-key c-mode-map "\C-c\C-e" 'c-macro-expand)
1787 (easy-menu-define c-c-menu c-mode-map "C Mode Commands"
1788 (cons "C" (c-lang-const c-mode-menu c)))
1790 ;; In XEmacs >= 21.5 modes should add their own entries to
1791 ;; `auto-mode-alist'. The comment form of autoload is used to avoid
1792 ;; doing this on load. That since `add-to-list' prepends the value
1793 ;; which could cause it to clobber user settings. Later emacsen have
1794 ;; an append option, but it's not safe to use.
1796 ;; The extension ".C" is associated with C++ while the lowercase
1797 ;; variant goes with C. On case insensitive file systems, this means
1798 ;; that ".c" files also might open C++ mode if the C++ entry comes
1799 ;; first on `auto-mode-alist'. Thus we try to ensure that ".C" comes
1800 ;; after ".c", and since `add-to-list' adds the entry first we have to
1801 ;; add the ".C" entry first.
1802 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(cc\\|hh\\)\\'" . c++-mode))
1803 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.[ch]\\(pp\\|xx\\|\\+\\+\\)\\'" . c++-mode))
1804 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(CC?\\|HH?\\)\\'" . c++-mode))
1806 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.c\\'" . c-mode))
1807 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.h\\'" . c-or-c++-mode))
1809 ;; NB: The following two associate yacc and lex files to C Mode, which
1810 ;; is not really suitable for those formats. Anyway, afaik there's
1811 ;; currently no better mode for them, and besides this is legacy.
1812 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.y\\(acc\\)?\\'" . c-mode))
1813 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.lex\\'" . c-mode))
1815 ;; Preprocessed files generated by C and C++ compilers.
1816 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.i\\'" . c-mode))
1817 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.ii\\'" . c++-mode))
1819 (unless (fboundp 'prog-mode) (defalias 'prog-mode 'fundamental-mode))
1821 ;;;###autoload
1822 (define-derived-mode c-mode prog-mode "C"
1823 "Major mode for editing C code.
1825 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1826 c-mode buffer. This automatically sets up a mail buffer with version
1827 information already added. You just need to add a description of the
1828 problem, including a reproducible test case, and send the message.
1830 To see what version of CC Mode you are running, enter `\\[c-version]'.
1832 The hook `c-mode-common-hook' is run with no args at mode
1833 initialization, then `c-mode-hook'.
1835 Key bindings:
1836 \\{c-mode-map}"
1837 :after-hook (progn (c-make-noise-macro-regexps)
1838 (c-make-macro-with-semi-re)
1839 (c-update-modeline))
1840 (c-initialize-cc-mode t)
1841 (setq abbrev-mode t)
1842 (c-init-language-vars-for 'c-mode)
1843 (c-common-init 'c-mode)
1844 (easy-menu-add c-c-menu)
1845 (cc-imenu-init cc-imenu-c-generic-expression)
1846 (c-run-mode-hooks 'c-mode-common-hook))
1848 (defconst c-or-c++-mode--regexp
1849 (eval-when-compile
1850 (let ((id "[a-zA-Z0-9_]+") (ws "[ \t\r]+") (ws-maybe "[ \t\r]*"))
1851 (concat "^" ws-maybe "\\(?:"
1852 "using" ws "\\(?:namespace" ws "std;\\|std::\\)"
1853 "\\|" "namespace" "\\(:?" ws id "\\)?" ws-maybe "{"
1854 "\\|" "class" ws id ws-maybe "[:{\n]"
1855 "\\|" "template" ws-maybe "<.*>"
1856 "\\|" "#include" ws-maybe "<\\(?:string\\|iostream\\|map\\)>"
1857 "\\)")))
1858 "A regexp applied to C header files to check if they are really C++.")
1860 ;;;###autoload
1861 (defun c-or-c++-mode ()
1862 "Analyse buffer and enable either C or C++ mode.
1864 Some people and projects use .h extension for C++ header files
1865 which is also the one used for C header files. This makes
1866 matching on file name insufficient for detecting major mode that
1867 should be used.
1869 This function attempts to use file contents to determine whether
1870 the code is C or C++ and based on that chooses whether to enable
1871 `c-mode' or `c++-mode'."
1872 (if (save-excursion
1873 (save-restriction
1874 (save-match-data
1875 (widen)
1876 (goto-char (point-min))
1877 (re-search-forward c-or-c++-mode--regexp
1878 (+ (point) c-guess-region-max) t))))
1879 (c++-mode)
1880 (c-mode)))
1883 ;; Support for C++
1885 (defvar c++-mode-syntax-table
1886 (funcall (c-lang-const c-make-mode-syntax-table c++))
1887 "Syntax table used in c++-mode buffers.")
1889 (c-define-abbrev-table 'c++-mode-abbrev-table
1890 '(("else" "else" c-electric-continued-statement 0)
1891 ("while" "while" c-electric-continued-statement 0)
1892 ("catch" "catch" c-electric-continued-statement 0))
1893 "Abbreviation table used in c++-mode buffers.")
1895 (defvar c++-mode-map
1896 (let ((map (c-make-inherited-keymap)))
1897 map)
1898 "Keymap used in c++-mode buffers.")
1899 ;; Add bindings which are only useful for C++.
1900 (define-key c++-mode-map "\C-c\C-e" 'c-macro-expand)
1901 (define-key c++-mode-map "\C-c:" 'c-scope-operator)
1902 (define-key c++-mode-map "<" 'c-electric-lt-gt)
1903 (define-key c++-mode-map ">" 'c-electric-lt-gt)
1905 (easy-menu-define c-c++-menu c++-mode-map "C++ Mode Commands"
1906 (cons "C++" (c-lang-const c-mode-menu c++)))
1908 ;;;###autoload
1909 (define-derived-mode c++-mode prog-mode "C++"
1910 "Major mode for editing C++ code.
1911 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1912 c++-mode buffer. This automatically sets up a mail buffer with
1913 version information already added. You just need to add a description
1914 of the problem, including a reproducible test case, and send the
1915 message.
1917 To see what version of CC Mode you are running, enter `\\[c-version]'.
1919 The hook `c-mode-common-hook' is run with no args at mode
1920 initialization, then `c++-mode-hook'.
1922 Key bindings:
1923 \\{c++-mode-map}"
1924 :after-hook (progn (c-make-noise-macro-regexps)
1925 (c-make-macro-with-semi-re)
1926 (c-update-modeline))
1927 (c-initialize-cc-mode t)
1928 (setq abbrev-mode t)
1929 (c-init-language-vars-for 'c++-mode)
1930 (c-common-init 'c++-mode)
1931 (easy-menu-add c-c++-menu)
1932 (cc-imenu-init cc-imenu-c++-generic-expression)
1933 (c-run-mode-hooks 'c-mode-common-hook))
1936 ;; Support for Objective-C
1938 (defvar objc-mode-syntax-table
1939 (funcall (c-lang-const c-make-mode-syntax-table objc))
1940 "Syntax table used in objc-mode buffers.")
1942 (c-define-abbrev-table 'objc-mode-abbrev-table
1943 '(("else" "else" c-electric-continued-statement 0)
1944 ("while" "while" c-electric-continued-statement 0))
1945 "Abbreviation table used in objc-mode buffers.")
1947 (defvar objc-mode-map
1948 (let ((map (c-make-inherited-keymap)))
1949 map)
1950 "Keymap used in objc-mode buffers.")
1951 ;; Add bindings which are only useful for Objective-C.
1952 (define-key objc-mode-map "\C-c\C-e" 'c-macro-expand)
1954 (easy-menu-define c-objc-menu objc-mode-map "ObjC Mode Commands"
1955 (cons "ObjC" (c-lang-const c-mode-menu objc)))
1957 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.m\\'" . objc-mode))
1959 ;;;###autoload
1960 (define-derived-mode objc-mode prog-mode "ObjC"
1961 "Major mode for editing Objective C code.
1962 To submit a problem report, enter `\\[c-submit-bug-report]' from an
1963 objc-mode buffer. This automatically sets up a mail buffer with
1964 version information already added. You just need to add a description
1965 of the problem, including a reproducible test case, and send the
1966 message.
1968 To see what version of CC Mode you are running, enter `\\[c-version]'.
1970 The hook `c-mode-common-hook' is run with no args at mode
1971 initialization, then `objc-mode-hook'.
1973 Key bindings:
1974 \\{objc-mode-map}"
1975 :after-hook (progn (c-make-noise-macro-regexps)
1976 (c-make-macro-with-semi-re)
1977 (c-update-modeline))
1978 (c-initialize-cc-mode t)
1979 (setq abbrev-mode t)
1980 (c-init-language-vars-for 'objc-mode)
1981 (c-common-init 'objc-mode)
1982 (easy-menu-add c-objc-menu)
1983 (cc-imenu-init nil 'cc-imenu-objc-function)
1984 (c-run-mode-hooks 'c-mode-common-hook))
1987 ;; Support for Java
1989 (defvar java-mode-syntax-table
1990 (funcall (c-lang-const c-make-mode-syntax-table java))
1991 "Syntax table used in java-mode buffers.")
1993 (c-define-abbrev-table 'java-mode-abbrev-table
1994 '(("else" "else" c-electric-continued-statement 0)
1995 ("while" "while" c-electric-continued-statement 0)
1996 ("catch" "catch" c-electric-continued-statement 0)
1997 ("finally" "finally" c-electric-continued-statement 0))
1998 "Abbreviation table used in java-mode buffers.")
2000 (defvar java-mode-map
2001 (let ((map (c-make-inherited-keymap)))
2002 map)
2003 "Keymap used in java-mode buffers.")
2004 ;; Add bindings which are only useful for Java.
2006 ;; Regexp trying to describe the beginning of a Java top-level
2007 ;; definition. This is not used by CC Mode, nor is it maintained
2008 ;; since it's practically impossible to write a regexp that reliably
2009 ;; matches such a construct. Other tools are necessary.
2010 (defconst c-Java-defun-prompt-regexp
2011 "^[ \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-*")
2013 (easy-menu-define c-java-menu java-mode-map "Java Mode Commands"
2014 (cons "Java" (c-lang-const c-mode-menu java)))
2016 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.java\\'" . java-mode))
2018 ;;;###autoload
2019 (define-derived-mode java-mode prog-mode "Java"
2020 "Major mode for editing Java code.
2021 To submit a problem report, enter `\\[c-submit-bug-report]' from a
2022 java-mode buffer. This automatically sets up a mail buffer with
2023 version information already added. You just need to add a description
2024 of the problem, including a reproducible test case, and send the
2025 message.
2027 To see what version of CC Mode you are running, enter `\\[c-version]'.
2029 The hook `c-mode-common-hook' is run with no args at mode
2030 initialization, then `java-mode-hook'.
2032 Key bindings:
2033 \\{java-mode-map}"
2034 :after-hook (c-update-modeline)
2035 (c-initialize-cc-mode t)
2036 (setq abbrev-mode t)
2037 (c-init-language-vars-for 'java-mode)
2038 (c-common-init 'java-mode)
2039 (easy-menu-add c-java-menu)
2040 (cc-imenu-init cc-imenu-java-generic-expression)
2041 (c-run-mode-hooks 'c-mode-common-hook))
2044 ;; Support for CORBA's IDL language
2046 (defvar idl-mode-syntax-table
2047 (funcall (c-lang-const c-make-mode-syntax-table idl))
2048 "Syntax table used in idl-mode buffers.")
2050 (c-define-abbrev-table 'idl-mode-abbrev-table nil
2051 "Abbreviation table used in idl-mode buffers.")
2053 (defvar idl-mode-map
2054 (let ((map (c-make-inherited-keymap)))
2055 map)
2056 "Keymap used in idl-mode buffers.")
2057 ;; Add bindings which are only useful for IDL.
2059 (easy-menu-define c-idl-menu idl-mode-map "IDL Mode Commands"
2060 (cons "IDL" (c-lang-const c-mode-menu idl)))
2062 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.idl\\'" . idl-mode))
2064 ;;;###autoload
2065 (define-derived-mode idl-mode prog-mode "IDL"
2066 "Major mode for editing CORBA's IDL, PSDL and CIDL code.
2067 To submit a problem report, enter `\\[c-submit-bug-report]' from an
2068 idl-mode buffer. This automatically sets up a mail buffer with
2069 version information already added. You just need to add a description
2070 of the problem, including a reproducible test case, and send the
2071 message.
2073 To see what version of CC Mode you are running, enter `\\[c-version]'.
2075 The hook `c-mode-common-hook' is run with no args at mode
2076 initialization, then `idl-mode-hook'.
2078 Key bindings:
2079 \\{idl-mode-map}"
2080 :after-hook (c-update-modeline)
2081 (c-initialize-cc-mode t)
2082 (c-init-language-vars-for 'idl-mode)
2083 (c-common-init 'idl-mode)
2084 (easy-menu-add c-idl-menu)
2085 ;;(cc-imenu-init cc-imenu-idl-generic-expression) ;TODO
2086 (c-run-mode-hooks 'c-mode-common-hook))
2089 ;; Support for Pike
2091 (defvar pike-mode-syntax-table
2092 (funcall (c-lang-const c-make-mode-syntax-table pike))
2093 "Syntax table used in pike-mode buffers.")
2095 (c-define-abbrev-table 'pike-mode-abbrev-table
2096 '(("else" "else" c-electric-continued-statement 0)
2097 ("while" "while" c-electric-continued-statement 0))
2098 "Abbreviation table used in pike-mode buffers.")
2100 (defvar pike-mode-map
2101 (let ((map (c-make-inherited-keymap)))
2102 map)
2103 "Keymap used in pike-mode buffers.")
2104 ;; Additional bindings.
2105 (define-key pike-mode-map "\C-c\C-e" 'c-macro-expand)
2107 (easy-menu-define c-pike-menu pike-mode-map "Pike Mode Commands"
2108 (cons "Pike" (c-lang-const c-mode-menu pike)))
2110 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(u?lpc\\|pike\\|pmod\\(\\.in\\)?\\)\\'" . pike-mode))
2111 ;;;###autoload (add-to-list 'interpreter-mode-alist '("pike" . pike-mode))
2113 ;;;###autoload
2114 (define-derived-mode pike-mode prog-mode "Pike"
2115 "Major mode for editing Pike code.
2116 To submit a problem report, enter `\\[c-submit-bug-report]' from a
2117 pike-mode buffer. This automatically sets up a mail buffer with
2118 version information already added. You just need to add a description
2119 of the problem, including a reproducible test case, and send the
2120 message.
2122 To see what version of CC Mode you are running, enter `\\[c-version]'.
2124 The hook `c-mode-common-hook' is run with no args at mode
2125 initialization, then `pike-mode-hook'.
2127 Key bindings:
2128 \\{pike-mode-map}"
2129 :after-hook (c-update-modeline)
2130 (c-initialize-cc-mode t)
2131 (setq abbrev-mode t)
2132 (c-init-language-vars-for 'pike-mode)
2133 (c-common-init 'pike-mode)
2134 (easy-menu-add c-pike-menu)
2135 ;;(cc-imenu-init cc-imenu-pike-generic-expression) ;TODO
2136 (c-run-mode-hooks 'c-mode-common-hook))
2139 ;; Support for AWK
2141 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.awk\\'" . awk-mode))
2142 ;;;###autoload (add-to-list 'interpreter-mode-alist '("awk" . awk-mode))
2143 ;;;###autoload (add-to-list 'interpreter-mode-alist '("mawk" . awk-mode))
2144 ;;;###autoload (add-to-list 'interpreter-mode-alist '("nawk" . awk-mode))
2145 ;;;###autoload (add-to-list 'interpreter-mode-alist '("gawk" . awk-mode))
2147 (c-define-abbrev-table 'awk-mode-abbrev-table
2148 '(("else" "else" c-electric-continued-statement 0)
2149 ("while" "while" c-electric-continued-statement 0))
2150 "Abbreviation table used in awk-mode buffers.")
2152 (defvar awk-mode-map
2153 (let ((map (c-make-inherited-keymap)))
2154 map)
2155 "Keymap used in awk-mode buffers.")
2156 ;; Add bindings which are only useful for awk.
2157 (define-key awk-mode-map "#" 'self-insert-command);Overrides electric parent binding.
2158 (define-key awk-mode-map "/" 'self-insert-command);Overrides electric parent binding.
2159 (define-key awk-mode-map "*" 'self-insert-command);Overrides electric parent binding.
2160 (define-key awk-mode-map "\C-c\C-n" 'undefined) ; #if doesn't exist in awk.
2161 (define-key awk-mode-map "\C-c\C-p" 'undefined)
2162 (define-key awk-mode-map "\C-c\C-u" 'undefined)
2163 (define-key awk-mode-map "\M-a" 'c-beginning-of-statement) ; 2003/10/7
2164 (define-key awk-mode-map "\M-e" 'c-end-of-statement) ; 2003/10/7
2165 (define-key awk-mode-map "\C-\M-a" 'c-awk-beginning-of-defun)
2166 (define-key awk-mode-map "\C-\M-e" 'c-awk-end-of-defun)
2168 (easy-menu-define c-awk-menu awk-mode-map "AWK Mode Commands"
2169 (cons "AWK" (c-lang-const c-mode-menu awk)))
2171 ;; (require 'cc-awk) brings these in.
2172 (defvar awk-mode-syntax-table)
2173 (declare-function c-awk-unstick-NL-prop "cc-awk" ())
2175 ;;;###autoload
2176 (define-derived-mode awk-mode prog-mode "AWK"
2177 "Major mode for editing AWK code.
2178 To submit a problem report, enter `\\[c-submit-bug-report]' from an
2179 awk-mode buffer. This automatically sets up a mail buffer with version
2180 information already added. You just need to add a description of the
2181 problem, including a reproducible test case, and send the message.
2183 To see what version of CC Mode you are running, enter `\\[c-version]'.
2185 The hook `c-mode-common-hook' is run with no args at mode
2186 initialization, then `awk-mode-hook'.
2188 Key bindings:
2189 \\{awk-mode-map}"
2190 :after-hook (c-update-modeline)
2191 ;; We need the next line to stop the macro defining
2192 ;; `awk-mode-syntax-table'. This would mask the real table which is
2193 ;; declared in cc-awk.el and hasn't yet been loaded.
2194 :syntax-table nil
2195 (require 'cc-awk) ; Added 2003/6/10.
2196 (c-initialize-cc-mode t)
2197 (set-syntax-table awk-mode-syntax-table)
2198 (setq abbrev-mode t)
2199 (c-init-language-vars-for 'awk-mode)
2200 (c-common-init 'awk-mode)
2201 (c-awk-unstick-NL-prop)
2202 (c-run-mode-hooks 'c-mode-common-hook))
2205 ;; bug reporting
2207 (defconst c-mode-help-address
2208 "submit@debbugs.gnu.org"
2209 "Address(es) for CC Mode bug reports.")
2211 (defun c-version ()
2212 "Echo the current version of CC Mode in the minibuffer."
2213 (interactive)
2214 (message "Using CC Mode version %s" c-version)
2215 (c-keep-region-active))
2217 (define-obsolete-variable-alias 'c-prepare-bug-report-hooks
2218 'c-prepare-bug-report-hook "24.3")
2219 (defvar c-prepare-bug-report-hook nil)
2221 ;; Dynamic variables used by reporter.
2222 (defvar reporter-prompt-for-summary-p)
2223 (defvar reporter-dont-compact-list)
2225 ;; This could be "emacs,cc-mode" in the version included in Emacs.
2226 (defconst c-mode-bug-package "cc-mode"
2227 "The package to use in the bug submission.")
2229 ;; reporter-submit-bug-report requires sendmail.
2230 (declare-function mail-position-on-field "sendmail" (field &optional soft))
2232 (defun c-submit-bug-report ()
2233 "Submit via mail a bug report on CC Mode."
2234 (interactive)
2235 (require 'reporter)
2236 ;; load in reporter
2237 (let ((reporter-prompt-for-summary-p t)
2238 (reporter-dont-compact-list '(c-offsets-alist))
2239 (style c-indentation-style)
2240 (c-features c-emacs-features))
2241 (and
2242 (if (y-or-n-p "Do you want to submit a report on CC Mode? ")
2243 t (message "") nil)
2244 (reporter-submit-bug-report
2245 c-mode-help-address
2246 (concat "CC Mode " c-version " (" mode-name ")")
2247 (let ((vars (append
2248 c-style-variables
2249 '(c-buffer-is-cc-mode
2250 c-tab-always-indent
2251 c-syntactic-indentation
2252 c-syntactic-indentation-in-macros
2253 c-ignore-auto-fill
2254 c-auto-align-backslashes
2255 c-backspace-function
2256 c-delete-function
2257 c-electric-pound-behavior
2258 c-default-style
2259 c-enable-xemacs-performance-kludge-p
2260 c-old-style-variable-behavior
2261 defun-prompt-regexp
2262 tab-width
2263 comment-column
2264 parse-sexp-ignore-comments
2265 parse-sexp-lookup-properties
2266 lookup-syntax-properties
2267 ;; A brain-damaged XEmacs only variable that, if
2268 ;; set to nil can cause all kinds of chaos.
2269 signal-error-on-buffer-boundary
2270 ;; Variables that affect line breaking and comments.
2271 auto-fill-mode
2272 auto-fill-function
2273 filladapt-mode
2274 comment-multi-line
2275 comment-start-skip
2276 fill-prefix
2277 fill-column
2278 paragraph-start
2279 adaptive-fill-mode
2280 adaptive-fill-regexp)
2281 nil)))
2282 (mapc (lambda (var) (unless (boundp var)
2283 (setq vars (delq var vars))))
2284 '(signal-error-on-buffer-boundary
2285 filladapt-mode
2286 defun-prompt-regexp
2287 font-lock-mode
2288 auto-fill-mode
2289 font-lock-maximum-decoration
2290 parse-sexp-lookup-properties
2291 lookup-syntax-properties))
2292 vars)
2293 (lambda ()
2294 (run-hooks 'c-prepare-bug-report-hook)
2295 (save-excursion
2296 (or (mail-position-on-field "X-Debbugs-Package")
2297 (insert c-mode-bug-package)))
2298 (insert (format "Buffer Style: %s\nc-emacs-features: %s\n"
2299 style c-features)))))))
2302 (cc-provide 'cc-mode)
2304 ;; Local Variables:
2305 ;; indent-tabs-mode: t
2306 ;; tab-width: 8
2307 ;; End:
2308 ;;; cc-mode.el ends here