Fix a comment whitespace typo.
[emacs.git] / lisp / progmodes / cc-mode.el
blob48a6619bd1eb42d6531a0c40929f0e5bc5b99418
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 <http://www.gnu.org/licenses/>.
30 ;;; Commentary:
32 ;; NOTE: Read the commentary below for the right way to submit bug reports!
33 ;; NOTE: See the accompanying texinfo manual for details on using this mode!
34 ;; Note: The version string is in cc-defs.
36 ;; This package provides GNU Emacs major modes for editing C, C++,
37 ;; Objective-C, Java, CORBA's IDL, Pike and AWK code. As of the
38 ;; latest Emacs and XEmacs releases, it is the default package for
39 ;; editing these languages. This package is called "CC Mode", and
40 ;; should be spelled exactly this way.
42 ;; CC Mode supports K&R and ANSI C, ANSI C++, Objective-C, Java,
43 ;; CORBA's IDL, Pike and AWK with a consistent indentation model
44 ;; across all modes. This indentation model is intuitive and very
45 ;; flexible, so that almost any desired style of indentation can be
46 ;; supported. Installation, usage, and programming details are
47 ;; contained in an accompanying texinfo manual.
49 ;; CC Mode's immediate ancestors were, c++-mode.el, cplus-md.el, and
50 ;; cplus-md1.el..
52 ;; To submit bug reports, type "C-c C-b". These will be sent to
53 ;; bug-gnu-emacs@gnu.org (mirrored as the Usenet newsgroup
54 ;; gnu.emacs.bug) as well as bug-cc-mode@gnu.org, which directly
55 ;; contacts the CC Mode maintainers. Questions can sent to
56 ;; help-gnu-emacs@gnu.org (mirrored as gnu.emacs.help) and/or
57 ;; bug-cc-mode@gnu.org. Please do not send bugs or questions to our
58 ;; personal accounts; we reserve the right to ignore such email!
60 ;; Many, many thanks go out to all the folks on the beta test list.
61 ;; Without their patience, testing, insight, code contributions, and
62 ;; encouragement CC Mode would be a far inferior package.
64 ;; You can get the latest version of CC Mode, including PostScript
65 ;; documentation and separate individual files from:
67 ;; http://cc-mode.sourceforge.net/
69 ;; You can join a moderated CC Mode announcement-only mailing list by
70 ;; visiting
72 ;; http://lists.sourceforge.net/mailman/listinfo/cc-mode-announce
74 ;; 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))
394 ;; We don't require the outline package, but we configure it a bit anyway.
395 (cc-bytecomp-defvar outline-level)
397 (defun c-mode-menu (modestr)
398 "Return a menu spec suitable for `easy-menu-define' that is exactly
399 like the C mode menu except that the menu bar item name is MODESTR
400 instead of \"C\".
402 This function is provided for compatibility only; derived modes should
403 preferably use the `c-mode-menu' language constant directly."
404 (cons modestr (c-lang-const c-mode-menu c)))
406 ;; Ugly hack to pull in the definition of `c-populate-syntax-table'
407 ;; from cc-langs to make it available at runtime. It's either this or
408 ;; moving the definition for it to cc-defs, but that would mean to
409 ;; break up the syntax table setup over two files.
410 (defalias 'c-populate-syntax-table
411 (cc-eval-when-compile
412 (let ((f (symbol-function 'c-populate-syntax-table)))
413 (if (byte-code-function-p f) f (byte-compile f)))))
415 ;; CAUTION: Try to avoid installing things on
416 ;; `before-change-functions'. The macro `combine-after-change-calls'
417 ;; is used and it doesn't work if there are things on that hook. That
418 ;; can cause font lock functions to run in inconvenient places during
419 ;; temporary changes in some font lock support modes, causing extra
420 ;; unnecessary work and font lock glitches due to interactions between
421 ;; various text properties.
423 ;; (2007-02-12): The macro `combine-after-change-calls' ISN'T used any
424 ;; more.
426 (defun c-unfind-enclosing-token (pos)
427 ;; If POS is wholly inside a token, remove that id from
428 ;; `c-found-types', should it be present. Return t if we were in an
429 ;; id, else nil.
430 (save-excursion
431 (let ((tok-beg (progn (goto-char pos)
432 (and (c-beginning-of-current-token) (point))))
433 (tok-end (progn (goto-char pos)
434 (and (c-end-of-current-token) (point)))))
435 (when (and tok-beg tok-end)
436 (c-unfind-type (buffer-substring-no-properties tok-beg tok-end))
437 t))))
439 (defun c-unfind-coalesced-tokens (beg end)
440 ;; If removing the region (beg end) would coalesce an identifier ending at
441 ;; beg with an identifier (fragment) beginning at end, or an identifier
442 ;; fragment ending at beg with an identifier beginning at end, remove the
443 ;; pertinent identifier(s) from `c-found-types'.
444 (save-excursion
445 (when (< beg end)
446 (goto-char beg)
447 (when
448 (and (not (bobp))
449 (progn (c-backward-syntactic-ws) (eq (point) beg))
450 (/= (skip-chars-backward c-symbol-chars (1- (point))) 0)
451 (progn (goto-char beg) (c-forward-syntactic-ws) (<= (point) end))
452 (> (point) beg)
453 (goto-char end)
454 (looking-at c-symbol-char-key))
455 (goto-char beg)
456 (c-simple-skip-symbol-backward)
457 (c-unfind-type (buffer-substring-no-properties (point) beg)))
459 (goto-char end)
460 (when
461 (and (not (eobp))
462 (progn (c-forward-syntactic-ws) (eq (point) end))
463 (looking-at c-symbol-char-key)
464 (progn (c-backward-syntactic-ws) (>= (point) beg))
465 (< (point) end)
466 (/= (skip-chars-backward c-symbol-chars (1- (point))) 0))
467 (goto-char (1+ end))
468 (c-end-of-current-token)
469 (c-unfind-type (buffer-substring-no-properties end (point)))))))
471 ;; c-maybe-stale-found-type records a place near the region being
472 ;; changed where an element of `found-types' might become stale. It
473 ;; is set in c-before-change and is either nil, or has the form:
475 ;; (c-decl-id-start "foo" 97 107 " (* ooka) " "o"), where
477 ;; o - `c-decl-id-start' is the c-type text property value at buffer
478 ;; pos 96.
480 ;; o - 97 107 is the region potentially containing the stale type -
481 ;; this is delimited by a non-nil c-type text property at 96 and
482 ;; either another one or a ";", "{", or "}" at 107.
484 ;; o - " (* ooka) " is the (before change) buffer portion containing
485 ;; the suspect type (here "ooka").
487 ;; o - "o" is the buffer contents which is about to be deleted. This
488 ;; would be the empty string for an insertion.
489 (defvar c-maybe-stale-found-type nil)
490 (make-variable-buffer-local 'c-maybe-stale-found-type)
492 (defvar c-just-done-before-change nil)
493 (make-variable-buffer-local 'c-just-done-before-change)
494 ;; This variable is set to t by `c-before-change' and to nil by
495 ;; `c-after-change'. It is used for two purposes: (i) to detect a spurious
496 ;; invocation of `before-change-functions' directly following on from a
497 ;; correct one. This happens in some Emacsen, for example when
498 ;; `basic-save-buffer' does (insert ?\n) when `require-final-newline' is
499 ;; non-nil; (ii) to detect when Emacs fails to invoke
500 ;; `before-change-functions'. This can happen when reverting a buffer - see
501 ;; bug #24094. It seems these failures happen only in GNU Emacs; XEmacs
502 ;; seems to maintain the strict alternation of calls to
503 ;; `before-change-functions' and `after-change-functions'.
505 (defun c-basic-common-init (mode default-style)
506 "Do the necessary initialization for the syntax handling routines
507 and the line breaking/filling code. Intended to be used by other
508 packages that embed CC Mode.
510 MODE is the CC Mode flavor to set up, e.g. `c-mode' or `java-mode'.
511 DEFAULT-STYLE tells which indentation style to install. It has the
512 same format as `c-default-style'.
514 Note that `c-init-language-vars' must be called before this function.
515 This function cannot do that since `c-init-language-vars' is a macro
516 that requires a literal mode spec at compile time."
518 (setq c-buffer-is-cc-mode mode)
520 ;; these variables should always be buffer local; they do not affect
521 ;; indentation style.
522 (make-local-variable 'comment-start)
523 (make-local-variable 'comment-end)
524 (make-local-variable 'comment-start-skip)
526 (make-local-variable 'paragraph-start)
527 (make-local-variable 'paragraph-separate)
528 (make-local-variable 'paragraph-ignore-fill-prefix)
529 (make-local-variable 'adaptive-fill-mode)
530 (make-local-variable 'adaptive-fill-regexp)
531 (make-local-variable 'fill-paragraph-handle-comment)
533 ;; now set their values
534 (set (make-local-variable 'parse-sexp-ignore-comments) t)
535 (set (make-local-variable 'indent-line-function) 'c-indent-line)
536 (set (make-local-variable 'indent-region-function) 'c-indent-region)
537 (set (make-local-variable 'normal-auto-fill-function) 'c-do-auto-fill)
538 (set (make-local-variable 'comment-multi-line) t)
539 (set (make-local-variable 'comment-line-break-function)
540 'c-indent-new-comment-line)
542 ;; Prevent time-wasting activity on C-y.
543 (when (boundp 'yank-handled-properties)
544 (make-local-variable 'yank-handled-properties)
545 (let ((yank-cat-handler (assq 'category yank-handled-properties)))
546 (when yank-cat-handler
547 (setq yank-handled-properties (remq yank-cat-handler
548 yank-handled-properties)))))
550 ;; For the benefit of adaptive fill, which otherwise mis-fills.
551 (setq fill-paragraph-handle-comment nil)
553 ;; Install `c-fill-paragraph' on `fill-paragraph-function' so that a
554 ;; direct call to `fill-paragraph' behaves better. This still
555 ;; doesn't work with filladapt but it's better than nothing.
556 (set (make-local-variable 'fill-paragraph-function) 'c-fill-paragraph)
558 ;; Initialize the cache of brace pairs, and opening braces/brackets/parens.
559 (c-state-cache-init)
560 ;; Initialize the "brace stack" cache.
561 (c-init-bs-cache)
563 (when (or c-recognize-<>-arglists
564 (c-major-mode-is 'awk-mode)
565 (c-major-mode-is '(java-mode c-mode c++-mode objc-mode)))
566 ;; We'll use the syntax-table text property to change the syntax
567 ;; of some chars for this language, so do the necessary setup for
568 ;; that.
570 ;; Note to other package developers: It's ok to turn this on in CC
571 ;; Mode buffers when CC Mode doesn't, but it's not ok to turn it
572 ;; off if CC Mode has turned it on.
574 ;; Emacs.
575 (when (boundp 'parse-sexp-lookup-properties)
576 (set (make-local-variable 'parse-sexp-lookup-properties) t))
578 ;; Same as above for XEmacs.
579 (when (boundp 'lookup-syntax-properties)
580 (set (make-local-variable 'lookup-syntax-properties) t)))
582 ;; Use this in Emacs 21+ to avoid meddling with the rear-nonsticky
583 ;; property on each character.
584 (when (boundp 'text-property-default-nonsticky)
585 (make-local-variable 'text-property-default-nonsticky)
586 (mapc (lambda (tprop)
587 (unless (assq tprop text-property-default-nonsticky)
588 (setq text-property-default-nonsticky
589 (cons `(,tprop . t) text-property-default-nonsticky))))
590 '(syntax-table category c-type)))
592 ;; In Emacs 21 and later it's possible to turn off the ad-hoc
593 ;; heuristic that open parens in column 0 are defun starters. Since
594 ;; we have c-state-cache, that heuristic isn't useful and only causes
595 ;; trouble, so turn it off.
596 ;; 2006/12/17: This facility is somewhat confused, and doesn't really seem
597 ;; helpful. Comment it out for now.
598 ;; (when (memq 'col-0-paren c-emacs-features)
599 ;; (make-local-variable 'open-paren-in-column-0-is-defun-start)
600 ;; (setq open-paren-in-column-0-is-defun-start nil))
602 (c-clear-found-types)
604 ;; now set the mode style based on default-style
605 (let ((style (cc-choose-style-for-mode mode default-style)))
606 ;; Override style variables if `c-old-style-variable-behavior' is
607 ;; set. Also override if we are using global style variables,
608 ;; have already initialized a style once, and are switching to a
609 ;; different style. (It's doubtful whether this is desirable, but
610 ;; the whole situation with nonlocal style variables is a bit
611 ;; awkward. It's at least the most compatible way with the old
612 ;; style init procedure.)
613 (c-set-style style (not (or c-old-style-variable-behavior
614 (and (not c-style-variables-are-local-p)
615 c-indentation-style
616 (not (string-equal c-indentation-style
617 style)))))))
618 (c-setup-paragraph-variables)
620 ;; we have to do something special for c-offsets-alist so that the
621 ;; buffer local value has its own alist structure.
622 (setq c-offsets-alist (copy-alist c-offsets-alist))
624 ;; setup the comment indent variable in a Emacs version portable way
625 (set (make-local-variable 'comment-indent-function) 'c-comment-indent)
626 ;; What sort of comments are default for M-;?
627 (setq c-block-comment-flag c-block-comment-is-default)
629 ;; In Emacs 24.4 onwards, prevent Emacs's built in electric indentation from
630 ;; messing up CC Mode's, and set `c-electric-flag' if `electric-indent-mode'
631 ;; has been called by the user.
632 (when (boundp 'electric-indent-inhibit) (setq electric-indent-inhibit t))
633 ;; CC-mode should obey Emacs's generic preferences, tho only do it if
634 ;; Emacs's generic preferences can be set per-buffer (Emacs>=24.4).
635 (when (fboundp 'electric-indent-local-mode)
636 (setq c-electric-flag electric-indent-mode))
638 ;; ;; Put submode indicators onto minor-mode-alist, but only once.
639 ;; (or (assq 'c-submode-indicators minor-mode-alist)
640 ;; (setq minor-mode-alist
641 ;; (cons '(c-submode-indicators c-submode-indicators)
642 ;; minor-mode-alist)))
643 (c-update-modeline)
645 ;; Install the functions that ensure that various internal caches
646 ;; don't become invalid due to buffer changes.
647 (when (featurep 'xemacs)
648 (make-local-hook 'before-change-functions)
649 (make-local-hook 'after-change-functions))
650 (add-hook 'before-change-functions 'c-before-change nil t)
651 (setq c-just-done-before-change nil)
652 (add-hook 'after-change-functions 'c-after-change nil t)
653 (when (boundp 'font-lock-extend-after-change-region-function)
654 (set (make-local-variable 'font-lock-extend-after-change-region-function)
655 'c-extend-after-change-region))) ; Currently (2009-05) used by all
656 ; languages with #define (C, C++,; ObjC), and by AWK.
658 (defun c-setup-doc-comment-style ()
659 "Initialize the variables that depend on the value of `c-doc-comment-style'."
660 (when (and (featurep 'font-lock)
661 (symbol-value 'font-lock-mode))
662 ;; Force font lock mode to reinitialize itself.
663 (font-lock-mode 0)
664 (font-lock-mode 1)))
666 ;; Buffer local variables defining the region to be fontified by a font lock
667 ;; after-change function. They are initialized in c-before-change to
668 ;; before-change-functions' BEG and END. `c-new-END' is amended in
669 ;; c-after-change with after-change-functions' BEG, END, and OLD-LEN. These
670 ;; variables may be modified by any before/after-change function, in
671 ;; particular by functions in `c-get-state-before-change-functions' and
672 ;; `c-before-font-lock-functions'.
673 (defvar c-new-BEG 0)
674 (make-variable-buffer-local 'c-new-BEG)
675 (defvar c-new-END 0)
676 (make-variable-buffer-local 'c-new-END)
677 ;; The following two variables record the values of `c-new-BEG' and
678 ;; `c-new-END' just after `c-new-END' has been adjusted for the length of text
679 ;; inserted or removed. They may be read by any after-change function (but
680 ;; should not be altered by one).
681 (defvar c-old-BEG 0)
682 (make-variable-buffer-local 'c-old-BEG)
683 (defvar c-old-END 0)
684 (make-variable-buffer-local 'c-old-END)
686 (defun c-common-init (&optional mode)
687 "Common initialization for all CC Mode modes.
688 In addition to the work done by `c-basic-common-init' and
689 `c-font-lock-init', this function sets up various other things as
690 customary in CC Mode modes but which aren't strictly necessary for CC
691 Mode to operate correctly.
693 MODE is the symbol for the mode to initialize, like `c-mode'. See
694 `c-basic-common-init' for details. It's only optional to be
695 compatible with old code; callers should always specify it."
697 (unless mode
698 ;; Called from an old third party package. The fallback is to
699 ;; initialize for C.
700 (c-init-language-vars-for 'c-mode))
702 (c-basic-common-init mode c-default-style)
703 (when mode
704 ;; Only initialize font locking if we aren't called from an old package.
705 (c-font-lock-init))
707 ;; Starting a mode is a sort of "change". So call the change functions...
708 (save-restriction
709 (widen)
710 (setq c-new-BEG (point-min))
711 (setq c-new-END (point-max))
712 (save-excursion
713 (let (before-change-functions after-change-functions)
714 (mapc (lambda (fn)
715 (funcall fn (point-min) (point-max)))
716 c-get-state-before-change-functions)
717 (mapc (lambda (fn)
718 (funcall fn (point-min) (point-max)
719 (- (point-max) (point-min))))
720 c-before-font-lock-functions))))
722 (set (make-local-variable 'outline-regexp) "[^#\n\^M]")
723 (set (make-local-variable 'outline-level) 'c-outline-level)
724 (set (make-local-variable 'add-log-current-defun-function)
725 (lambda ()
726 (or (c-cpp-define-name) (c-defun-name))))
727 (let ((rfn (assq mode c-require-final-newline)))
728 (when rfn
729 (if (boundp 'mode-require-final-newline)
730 (and (cdr rfn)
731 (set (make-local-variable 'require-final-newline)
732 mode-require-final-newline))
733 (set (make-local-variable 'require-final-newline) (cdr rfn))))))
735 (defun c-count-cfss (lv-alist)
736 ;; LV-ALIST is an alist like `file-local-variables-alist'. Count how many
737 ;; elements with the key `c-file-style' there are in it.
738 (let ((elt-ptr lv-alist) elt (cownt 0))
739 (while elt-ptr
740 (setq elt (car elt-ptr)
741 elt-ptr (cdr elt-ptr))
742 (when (eq (car elt) 'c-file-style)
743 (setq cownt (1+ cownt))))
744 cownt))
746 (defun c-before-hack-hook ()
747 "Set the CC Mode style and \"offsets\" when in the buffer's local variables.
748 They are set only when, respectively, the pseudo variables
749 `c-file-style' and `c-file-offsets' are present in the list.
751 This function is called from the hook `before-hack-local-variables-hook'."
752 (when c-buffer-is-cc-mode
753 (let ((mode-cons (assq 'mode file-local-variables-alist))
754 (stile (cdr (assq 'c-file-style file-local-variables-alist)))
755 (offsets (cdr (assq 'c-file-offsets file-local-variables-alist))))
756 (when mode-cons
757 (hack-one-local-variable (car mode-cons) (cdr mode-cons))
758 (setq file-local-variables-alist
759 (delq mode-cons file-local-variables-alist)))
760 (when stile
761 (or (stringp stile) (error "c-file-style is not a string"))
762 (if (boundp 'dir-local-variables-alist)
763 ;; Determine whether `c-file-style' was set in the file's local
764 ;; variables or in a .dir-locals.el (a directory setting).
765 (let ((cfs-in-file-and-dir-count
766 (c-count-cfss file-local-variables-alist))
767 (cfs-in-dir-count (c-count-cfss dir-local-variables-alist)))
768 (c-set-style stile
769 (and (= cfs-in-file-and-dir-count cfs-in-dir-count)
770 'keep-defaults)))
771 (c-set-style stile)))
772 (when offsets
773 (mapc
774 (lambda (langentry)
775 (let ((langelem (car langentry))
776 (offset (cdr langentry)))
777 (c-set-offset langelem offset)))
778 offsets)))))
780 (defun c-remove-any-local-eval-or-mode-variables ()
781 ;; If the buffer specifies `mode' or `eval' in its File Local Variable list
782 ;; or on the first line, remove all occurrences. See
783 ;; `c-postprocess-file-styles' for justification. There is no need to save
784 ;; point here, or even bother too much about the buffer contents. However,
785 ;; DON'T mess up the kill-ring.
787 ;; Most of the code here is derived from Emacs 21.3's `hack-local-variables'
788 ;; in files.el.
789 (goto-char (point-max))
790 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
791 (let (lv-point (prefix "") (suffix ""))
792 (when (let ((case-fold-search t))
793 (search-forward "Local Variables:" nil t))
794 (setq lv-point (point))
795 ;; The prefix is what comes before "local variables:" in its line.
796 ;; The suffix is what comes after "local variables:" in its line.
797 (skip-chars-forward " \t")
798 (or (eolp)
799 (setq suffix (buffer-substring (point)
800 (progn (end-of-line) (point)))))
801 (goto-char (match-beginning 0))
802 (or (bolp)
803 (setq prefix
804 (buffer-substring (point)
805 (progn (beginning-of-line) (point)))))
807 (while (search-forward-regexp
808 (concat "^[ \t]*"
809 (regexp-quote prefix)
810 "\\(mode\\|eval\\):.*"
811 (regexp-quote suffix)
812 "$")
813 nil t)
814 (forward-line 0)
815 (delete-region (point) (progn (forward-line) (point)))))
817 ;; Delete the first line, if we've got one, in case it contains a mode spec.
818 (unless (and lv-point
819 (progn (goto-char lv-point)
820 (forward-line 0)
821 (bobp)))
822 (goto-char (point-min))
823 (unless (eobp)
824 (delete-region (point) (progn (forward-line) (point)))))))
826 (defun c-postprocess-file-styles ()
827 "Function that post processes relevant file local variables in CC Mode.
828 Currently, this function simply applies any style and offset settings
829 found in the file's Local Variable list. It first applies any style
830 setting found in `c-file-style', then it applies any offset settings
831 it finds in `c-file-offsets'.
833 Note that the style variables are always made local to the buffer."
835 ;; apply file styles and offsets
836 (when c-buffer-is-cc-mode
837 (if (or c-file-style c-file-offsets)
838 (c-make-styles-buffer-local t))
839 (when c-file-style
840 (or (stringp c-file-style)
841 (error "c-file-style is not a string"))
842 (c-set-style c-file-style))
844 (and c-file-offsets
845 (mapc
846 (lambda (langentry)
847 (let ((langelem (car langentry))
848 (offset (cdr langentry)))
849 (c-set-offset langelem offset)))
850 c-file-offsets))
851 ;; Problem: The file local variable block might have explicitly set a
852 ;; style variable. The `c-set-style' or `mapcar' call might have
853 ;; overwritten this. So we run `hack-local-variables' again to remedy
854 ;; this. There are no guarantees this will work properly, particularly as
855 ;; we have no control over what the other hook functions on
856 ;; `hack-local-variables-hook' would have done. We now (2006/2/1) remove
857 ;; any `eval' or `mode' expressions before we evaluate again (see below).
858 ;; ACM, 2005/11/2.
860 ;; Problem (bug reported by Gustav Broberg): if one of the variables is
861 ;; `mode', this will invoke c-mode (etc.) again, setting up the style etc.
862 ;; We prevent this by temporarily removing `mode' from the Local Variables
863 ;; section.
864 (if (or c-file-style c-file-offsets)
865 (let ((hack-local-variables-hook nil) (inhibit-read-only t))
866 (c-tentative-buffer-changes
867 (c-remove-any-local-eval-or-mode-variables)
868 (hack-local-variables))
869 nil))))
871 (if (boundp 'before-hack-local-variables-hook)
872 (add-hook 'before-hack-local-variables-hook 'c-before-hack-hook)
873 (add-hook 'hack-local-variables-hook 'c-postprocess-file-styles))
875 (defmacro c-run-mode-hooks (&rest hooks)
876 ;; Emacs 21.1 has introduced a system with delayed mode hooks that
877 ;; requires the use of the new function `run-mode-hooks'.
878 (if (cc-bytecomp-fboundp 'run-mode-hooks)
879 `(run-mode-hooks ,@hooks)
880 `(progn ,@(mapcar (lambda (hook) `(run-hooks ,hook)) hooks))))
883 ;;; Change hooks, linking with Font Lock and electric-indent-mode.
885 (defun c-called-from-text-property-change-p ()
886 ;; Is the primitive which invoked `before-change-functions' or
887 ;; `after-change-functions' one which merely changes text properties? This
888 ;; function must be called directly from a member of one of the above hooks.
890 ;; In the following call, frame 0 is `backtrace-frame', frame 1 is
891 ;; `c-called-from-text-property-change-p', frame 2 is
892 ;; `c-before/after-change', frame 3 is the primitive invoking the change
893 ;; hook.
894 (memq (cadr (backtrace-frame 3))
895 '(put-text-property remove-list-of-text-properties)))
897 (defun c-depropertize-CPP (beg end)
898 ;; Remove the punctuation syntax-table text property from the CPP parts of
899 ;; (c-new-BEG c-new-END).
901 ;; This function is in the C/C++/ObjC values of
902 ;; `c-get-state-before-change-functions' and is called exclusively as a
903 ;; before change function.
904 (c-save-buffer-state (m-beg ss-found)
905 (goto-char c-new-BEG)
906 (while (and (< (point) beg)
907 (search-forward-regexp c-anchored-cpp-prefix beg 'bound))
908 (goto-char (match-beginning 1))
909 (setq m-beg (point))
910 (c-end-of-macro)
911 (c-clear-char-property-with-value m-beg (point) 'syntax-table '(1)))
913 (while (and (< (point) end)
914 (setq ss-found
915 (search-forward-regexp c-anchored-cpp-prefix end 'bound)))
916 (goto-char (match-beginning 1))
917 (setq m-beg (point))
918 (c-end-of-macro))
919 (if (and ss-found (> (point) end))
920 (c-clear-char-property-with-value m-beg (point) 'syntax-table '(1)))
922 (while (and (< (point) c-new-END)
923 (search-forward-regexp c-anchored-cpp-prefix c-new-END 'bound))
924 (goto-char (match-beginning 1))
925 (setq m-beg (point))
926 (c-end-of-macro)
927 (c-clear-char-property-with-value
928 m-beg (point) 'syntax-table '(1)))))
930 (defun c-extend-region-for-CPP (_beg _end)
931 ;; Adjust `c-new-BEG', `c-new-END' respectively to the beginning and end of
932 ;; any preprocessor construct they may be in.
934 ;; Point is undefined both before and after this function call; the buffer
935 ;; has already been widened, and match-data saved. The return value is
936 ;; meaningless.
938 ;; This function is in the C/C++/ObjC values of
939 ;; `c-get-state-before-change-functions' and is called exclusively as a
940 ;; before change function.
941 (goto-char c-new-BEG)
942 (c-beginning-of-macro)
943 (when (< (point) c-new-BEG)
944 (setq c-new-BEG (max (point) (c-determine-limit 500 c-new-BEG))))
946 (goto-char c-new-END)
947 (when (c-beginning-of-macro)
948 (c-end-of-macro)
949 (or (eobp) (forward-char))) ; Over the terminating NL which may be marked
950 ; with a c-cpp-delimiter category property
951 (when (> (point) c-new-END)
952 (setq c-new-END (min (point) (c-determine-+ve-limit 500 c-new-END)))))
954 (defun c-depropertize-new-text (beg end _old-len)
955 ;; Remove from the new text in (BEG END) any and all text properties which
956 ;; might interfere with CC Mode's proper working.
958 ;; This function is called exclusively as an after-change function. It
959 ;; appears in the value (for all languages) of
960 ;; `c-before-font-lock-functions'. The value of point is undefined both on
961 ;; entry and exit, and the return value has no significance. The parameters
962 ;; BEG, END, and OLD-LEN are the standard ones supplied to all after-change
963 ;; functions.
964 (c-save-buffer-state ()
965 (when (> end beg)
966 (c-clear-char-properties beg end 'syntax-table)
967 (c-clear-char-properties beg end 'category)
968 (c-clear-char-properties beg end 'c-is-sws)
969 (c-clear-char-properties beg end 'c-in-sws)
970 (c-clear-char-properties beg end 'c-type)
971 (c-clear-char-properties beg end 'c-awk-NL-prop))))
973 (defun c-extend-font-lock-region-for-macros (_begg endd _old-len)
974 ;; Extend the region (c-new-BEG c-new-END) to cover all (possibly changed)
975 ;; preprocessor macros; The return value has no significance.
977 ;; Point is undefined on both entry and exit to this function. The buffer
978 ;; will have been widened on entry.
980 ;; c-new-BEG has already been extended in `c-extend-region-for-CPP' so we
981 ;; don't need to repeat the exercise here.
983 ;; This function is in the C/C++/ObjC value of `c-before-font-lock-functions'.
984 (goto-char endd)
985 (when (c-beginning-of-macro)
986 (c-end-of-macro)
987 ;; Determine the region, (c-new-BEG c-new-END), which will get font
988 ;; locked. This restricts the region should there be long macros.
989 (setq c-new-END (min (max c-new-END (point))
990 (c-determine-+ve-limit 500 c-new-END)))))
992 (defun c-neutralize-CPP-line (beg end)
993 ;; BEG and END bound a region, typically a preprocessor line. Put a
994 ;; "punctuation" syntax-table property on syntactically obtrusive
995 ;; characters, ones which would interact syntactically with stuff outside
996 ;; this region.
998 ;; These are unmatched string delimiters, or unmatched
999 ;; parens/brackets/braces. An unclosed comment is regarded as valid, NOT
1000 ;; obtrusive.
1001 (save-excursion
1002 (let (s)
1003 (while
1004 (progn
1005 (setq s (parse-partial-sexp beg end -1))
1006 (cond
1007 ((< (nth 0 s) 0) ; found an unmated ),},]
1008 (c-put-char-property (1- (point)) 'syntax-table '(1))
1010 ((nth 3 s) ; In a string
1011 (c-put-char-property (nth 8 s) 'syntax-table '(1))
1013 ((> (nth 0 s) 0) ; In a (,{,[
1014 (c-put-char-property (nth 1 s) 'syntax-table '(1))
1016 (t nil)))))))
1018 (defun c-neutralize-syntax-in-and-mark-CPP (_begg _endd _old-len)
1019 ;; (i) "Neutralize" every preprocessor line wholly or partially in the
1020 ;; changed region. "Restore" lines which were CPP lines before the change
1021 ;; and are no longer so.
1023 ;; (ii) Mark each CPP construct by placing a `category' property value
1024 ;; `c-cpp-delimiter' at its start and end. The marked characters are the
1025 ;; opening # and usually the terminating EOL, but sometimes the character
1026 ;; before a comment delimiter.
1028 ;; That is, set syntax-table properties on characters that would otherwise
1029 ;; interact syntactically with those outside the CPP line(s).
1031 ;; This function is called from an after-change function, BEGG ENDD and
1032 ;; OLD-LEN being the standard parameters. It prepares the buffer for font
1033 ;; locking, hence must get called before `font-lock-after-change-function'.
1035 ;; Point is undefined both before and after this function call, the buffer
1036 ;; has been widened, and match-data saved. The return value is ignored.
1038 ;; This function is in the C/C++/ObjC value of `c-before-font-lock-functions'.
1040 ;; Note: SPEED _MATTERS_ IN THIS FUNCTION!!!
1042 ;; This function might make hidden buffer changes.
1043 (c-save-buffer-state (limits)
1044 ;; Clear 'syntax-table properties "punctuation":
1045 ;; (c-clear-char-property-with-value c-new-BEG c-new-END 'syntax-table '(1))
1047 ;; CPP "comment" markers:
1048 (if (eval-when-compile (memq 'category-properties c-emacs-features));Emacs.
1049 (c-clear-char-property-with-value
1050 c-new-BEG c-new-END 'category 'c-cpp-delimiter))
1051 ;; FIXME!!! What about the "<" and ">" category properties? 2009-11-16
1053 ;; Add needed properties to each CPP construct in the region.
1054 (goto-char c-new-BEG)
1055 (if (setq limits (c-literal-limits)) ; Go past any literal.
1056 (goto-char (cdr limits)))
1057 (skip-chars-backward " \t")
1058 (let ((pps-position (point)) pps-state mbeg)
1059 (while (and (< (point) c-new-END)
1060 (search-forward-regexp c-anchored-cpp-prefix c-new-END t))
1061 ;; If we've found a "#" inside a macro/string/comment, ignore it.
1062 (unless
1063 (or (save-excursion
1064 (goto-char (match-beginning 0))
1065 (let ((here (point)))
1066 (and (save-match-data (c-beginning-of-macro))
1067 (< (point) here))))
1068 (progn
1069 (setq pps-state
1070 (parse-partial-sexp pps-position (point) nil nil pps-state)
1071 pps-position (point))
1072 (or (nth 3 pps-state) ; in a string?
1073 (and (nth 4 pps-state)
1074 (not (eq (nth 7 pps-state) 'syntax-table)))))) ; in a comment?
1075 (goto-char (match-beginning 1))
1076 (setq mbeg (point))
1077 (if (> (c-no-comment-end-of-macro) mbeg)
1078 (progn
1079 (c-neutralize-CPP-line mbeg (point)) ; "punctuation" properties
1080 (if (eval-when-compile
1081 (memq 'category-properties c-emacs-features)) ;Emacs.
1082 (c-set-cpp-delimiters mbeg (point)))) ; "comment" markers
1083 (forward-line)) ; no infinite loop with, e.g., "#//"
1084 )))))
1086 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1087 ;; Parsing of quotes.
1089 ;; Valid digit separators in numbers will get the syntax-table "punctuation"
1090 ;; property, '(1), and also the text property `c-digit-separator' value t.
1092 ;; Invalid other quotes (i.e. those not validly bounding a single character,
1093 ;; or escaped character) will get the syntax-table "punctuation" property,
1094 ;; '(1), too.
1096 ;; Note that, for convenience, these properties are applied even inside
1097 ;; comments and strings.
1099 (defconst c-maybe-quoted-number-head
1100 (concat
1101 "\\(0\\("
1102 "\\([Xx]\\([0-9a-fA-F]\\('[0-9a-fA-F]\\|[0-9a-fA-F]\\)*'?\\)?\\)"
1103 "\\|"
1104 "\\([Bb]\\([01]\\('[01]\\|[01]\\)*'?\\)?\\)"
1105 "\\|"
1106 "\\('[0-7]\\|[0-7]\\)*'?"
1107 "\\)"
1108 "\\|"
1109 "[1-9]\\('[0-9]\\|[0-9]\\)*'?"
1110 "\\)")
1111 "Regexp matching the head of a numeric literal, including with digit separators.")
1113 (defun c-quoted-number-head-before-point ()
1114 ;; Return non-nil when the head of a possibly quoted number is found
1115 ;; immediately before point. The value returned in this case is the buffer
1116 ;; position of the start of the head. That position is also in
1117 ;; (match-beginning 0).
1118 (when c-has-quoted-numbers
1119 (save-excursion
1120 (let ((here (point))
1121 found)
1122 (skip-chars-backward "0-9a-fA-F'")
1123 (if (and (memq (char-before) '(?x ?X))
1124 (eq (char-before (1- (point))) ?0))
1125 (backward-char 2))
1126 (while
1127 (and
1128 (setq found
1129 (search-forward-regexp c-maybe-quoted-number-head here t))
1130 (< found here)))
1131 (and (eq found here) (match-beginning 0))))))
1133 (defconst c-maybe-quoted-number-tail
1134 (concat
1135 "\\("
1136 "\\([xX']?[0-9a-fA-F]\\('[0-9a-fA-F]\\|[0-9a-fA-F]\\)*\\)"
1137 "\\|"
1138 "\\([bB']?[01]\\('[01]\\|[01]\\)*\\)"
1139 "\\|"
1140 "\\('?[0-9]\\('[0-9]\\|[0-9]\\)*\\)"
1141 "\\)")
1142 "Regexp matching the tail of a numeric literal, including with digit separators.
1143 Note that this is a strict tail, so won't match, e.g. \"0x....\".")
1145 (defun c-quoted-number-tail-after-point ()
1146 ;; Return non-nil when a proper tail of a possibly quoted number is found
1147 ;; immediately after point. The value returned in this case is the buffer
1148 ;; position of the end of the tail. That position is also in (match-end 0).
1149 (when c-has-quoted-numbers
1150 (and (looking-at c-maybe-quoted-number-tail)
1151 (match-end 0))))
1153 (defconst c-maybe-quoted-number
1154 (concat
1155 "\\(0\\("
1156 "\\([Xx][0-9a-fA-F]\\('[0-9a-fA-F]\\|[0-9a-fA-F]\\)*\\)"
1157 "\\|"
1158 "\\([Bb][01]\\('[01]\\|[01]\\)*\\)"
1159 "\\|"
1160 "\\('[0-7]\\|[0-7]\\)*"
1161 "\\)"
1162 "\\|"
1163 "[1-9]\\('[0-9]\\|[0-9]\\)*"
1164 "\\)")
1165 "Regexp matching a numeric literal, including with digit separators.")
1167 (defun c-quoted-number-straddling-point ()
1168 ;; Return non-nil if a definitely quoted number starts before point and ends
1169 ;; after point. In this case the number is bounded by (match-beginning 0)
1170 ;; and (match-end 0).
1171 (when c-has-quoted-numbers
1172 (save-excursion
1173 (let ((here (point))
1174 (bound (progn (skip-chars-forward "0-9a-fA-F'") (point))))
1175 (goto-char here)
1176 (when (< (skip-chars-backward "0-9a-fA-F'") 0)
1177 (if (and (memq (char-before) '(?x ?X))
1178 (eq (char-before (1- (point))) ?0))
1179 (backward-char 2))
1180 (while (and (search-forward-regexp c-maybe-quoted-number bound t)
1181 (<= (match-end 0) here)))
1182 (and (< (match-beginning 0) here)
1183 (> (match-end 0) here)
1184 (save-match-data
1185 (goto-char (match-beginning 0))
1186 (save-excursion (search-forward "'" (match-end 0) t)))))))))
1188 (defun c-parse-quotes-before-change (beg end)
1189 ;; This function analyzes 's near the region (c-new-BEG c-new-END), amending
1190 ;; those two variables as needed to include 's into that region when they
1191 ;; might be syntactically relevant to the change in progress.
1193 ;; Having amended that region, the function removes pertinent text
1194 ;; properties (syntax-table properties with value '(1) and c-digit-separator
1195 ;; props with value t) from 's in it. This operation is performed even
1196 ;; within strings and comments.
1198 ;; This function is called exclusively as a before-change function via the
1199 ;; variable `c-get-state-before-change-functions'.
1200 (c-save-buffer-state (p-limit found)
1201 ;; Special consideration for deleting \ from '\''.
1202 (if (and (> end beg)
1203 (eq (char-before end) ?\\)
1204 (<= c-new-END end))
1205 (setq c-new-END (min (1+ end) (point-max))))
1207 ;; Do we have a ' (or something like ',',',',',') within range of
1208 ;; c-new-BEG?
1209 (goto-char c-new-BEG)
1210 (setq p-limit (max (- (point) 2) (point-min)))
1211 (while (and (skip-chars-backward "^\\\\'" p-limit)
1212 (> (point) p-limit))
1213 (when (eq (char-before) ?\\)
1214 (setq p-limit (max (1- p-limit) (point-min))))
1215 (backward-char)
1216 (setq c-new-BEG (point)))
1217 (beginning-of-line)
1218 (while (and
1219 (setq found (search-forward-regexp "\\('\\([^'\\]\\|\\\\.\\)\\)*'"
1220 c-new-BEG 'limit))
1221 (< (point) (1- c-new-BEG))))
1222 (if found
1223 (setq c-new-BEG
1224 (if (and (eq (point) (1- c-new-BEG))
1225 (eq (char-after) ?')) ; "''" before c-new-BEG.
1226 (1- c-new-BEG)
1227 (match-beginning 0))))
1229 ;; Check for a number with quote separators straddling c-new-BEG
1230 (when c-has-quoted-numbers
1231 (goto-char c-new-BEG)
1232 (when ;; (c-quoted-number-straddling-point)
1233 (c-quoted-number-head-before-point)
1234 (setq c-new-BEG (match-beginning 0))))
1236 ;; Do we have a ' (or something like ',',',',...,',') within range of
1237 ;; c-new-END?
1238 (goto-char c-new-END)
1239 (setq p-limit (min (+ (point) 2) (point-max)))
1240 (while (and (skip-chars-forward "^\\\\'" p-limit)
1241 (< (point) p-limit))
1242 (when (eq (char-after) ?\\)
1243 (setq p-limit (min (1+ p-limit) (point-max))))
1244 (forward-char)
1245 (setq c-new-END (point)))
1246 (if (looking-at "[^']?\\('\\([^'\\]\\|\\\\.\\)\\)*'")
1247 (setq c-new-END (match-end 0)))
1249 ;; Check for a number with quote separators straddling c-new-END.
1250 (when c-has-quoted-numbers
1251 (goto-char c-new-END)
1252 (when ;; (c-quoted-number-straddling-point)
1253 (c-quoted-number-tail-after-point)
1254 (setq c-new-END (match-end 0))))
1256 ;; Remove the '(1) syntax-table property from all "'"s within (c-new-BEG
1257 ;; c-new-END).
1258 (c-clear-char-property-with-value-on-char
1259 c-new-BEG c-new-END
1260 'syntax-table '(1)
1262 ;; Remove the c-digit-separator text property from the same "'"s.
1263 (when c-has-quoted-numbers
1264 (c-clear-char-property-with-value-on-char
1265 c-new-BEG c-new-END
1266 'c-digit-separator t
1267 ?'))))
1269 (defun c-parse-quotes-after-change (_beg _end _old-len)
1270 ;; This function applies syntax-table properties (value '(1)) and
1271 ;; c-digit-separator properties as needed to 's within the range (c-new-BEG
1272 ;; c-new-END). This operation is performed even within strings and
1273 ;; comments.
1275 ;; This function is called exclusively as an after-change function via the
1276 ;; variable `c-before-font-lock-functions'.
1277 (c-save-buffer-state (num-beg num-end)
1278 ;; Apply the needed syntax-table and c-digit-separator text properties to
1279 ;; quotes.
1280 (goto-char c-new-BEG)
1281 (while (and (< (point) c-new-END)
1282 (search-forward "'" c-new-END 'limit))
1283 (cond ((and (eq (char-before (1- (point))) ?\\)
1284 ;; Check we've got an odd number of \s, here.
1285 (save-excursion
1286 (backward-char)
1287 (eq (logand (skip-chars-backward "\\\\") 1) 1)))) ; not a real '.
1288 ((c-quoted-number-straddling-point)
1289 (setq num-beg (match-beginning 0)
1290 num-end (match-end 0))
1291 (c-put-char-properties-on-char num-beg num-end
1292 'syntax-table '(1) ?')
1293 (c-put-char-properties-on-char num-beg num-end
1294 'c-digit-separator t ?')
1295 (goto-char num-end))
1296 ((looking-at "\\([^\\']\\|\\\\.\\)'") ; balanced quoted expression.
1297 (goto-char (match-end 0)))
1298 (t (c-put-char-property (1- (point)) 'syntax-table '(1)))))))
1300 (defun c-before-change (beg end)
1301 ;; Function to be put on `before-change-functions'. Primarily, this calls
1302 ;; the language dependent `c-get-state-before-change-functions'. It is
1303 ;; otherwise used only to remove stale entries from the `c-found-types'
1304 ;; cache, and to record entries which a `c-after-change' function might
1305 ;; confirm as stale.
1307 ;; Note that this function must be FAST rather than accurate. Note
1308 ;; also that it only has any effect when font locking is enabled.
1309 ;; We exploit this by checking for font-lock-*-face instead of doing
1310 ;; rigorous syntactic analysis.
1312 ;; If either change boundary is wholly inside an identifier, delete
1313 ;; it/them from the cache. Don't worry about being inside a string
1314 ;; or a comment - "wrongly" removing a symbol from `c-found-types'
1315 ;; isn't critical.
1316 (unless (or (c-called-from-text-property-change-p)
1317 c-just-done-before-change) ; guard against a spurious second
1318 ; invocation of before-change-functions.
1319 (setq c-just-done-before-change t)
1320 ;; (c-new-BEG c-new-END) will be the region to fontify.
1321 (setq c-new-BEG beg c-new-END end)
1322 (setq c-maybe-stale-found-type nil)
1323 (save-restriction
1324 (save-match-data
1325 (widen)
1326 (save-excursion
1327 ;; Are we inserting/deleting stuff in the middle of an identifier?
1328 (c-unfind-enclosing-token beg)
1329 (c-unfind-enclosing-token end)
1330 ;; Are we coalescing two tokens together, e.g. "fo o" -> "foo"?
1331 (when (< beg end)
1332 (c-unfind-coalesced-tokens beg end))
1333 (c-invalidate-sws-region-before end)
1334 ;; Are we (potentially) disrupting the syntactic context which
1335 ;; makes a type a type? E.g. by inserting stuff after "foo" in
1336 ;; "foo bar;", or before "foo" in "typedef foo *bar;"?
1338 ;; We search for appropriate c-type properties "near" the change.
1339 ;; First, find an appropriate boundary for this property search.
1340 (let (lim
1341 type type-pos
1342 marked-id term-pos
1343 (end1
1344 (or (and (eq (get-text-property end 'face)
1345 'font-lock-comment-face)
1346 (previous-single-property-change end 'face))
1347 end)))
1348 (when (>= end1 beg) ; Don't hassle about changes entirely in comments.
1349 ;; Find a limit for the search for a `c-type' property
1350 (while
1351 (and (/= (skip-chars-backward "^;{}") 0)
1352 (> (point) (point-min))
1353 (memq (c-get-char-property (1- (point)) 'face)
1354 '(font-lock-comment-face font-lock-string-face))))
1355 (setq lim (max (point-min) (1- (point))))
1357 ;; Look for the latest `c-type' property before end1
1358 (when (and (> end1 (point-min))
1359 (setq type-pos
1360 (if (get-text-property (1- end1) 'c-type)
1361 end1
1362 (previous-single-property-change end1 'c-type
1363 nil lim))))
1364 (setq type (get-text-property (max (1- type-pos) lim) 'c-type))
1366 (when (memq type '(c-decl-id-start c-decl-type-start))
1367 ;; Get the identifier, if any, that the property is on.
1368 (goto-char (1- type-pos))
1369 (setq marked-id
1370 (when (looking-at "\\(\\sw\\|\\s_\\)")
1371 (c-beginning-of-current-token)
1372 (buffer-substring-no-properties (point) type-pos)))
1374 (goto-char end1)
1375 (skip-chars-forward "^;{}") ;FIXME!!! loop for comment, maybe
1376 (setq lim (point))
1377 (setq term-pos
1378 (or (c-next-single-property-change end 'c-type nil lim)
1379 lim))
1380 (setq c-maybe-stale-found-type
1381 (list type marked-id
1382 type-pos term-pos
1383 (buffer-substring-no-properties type-pos
1384 term-pos)
1385 (buffer-substring-no-properties beg end)))))))
1387 (if c-get-state-before-change-functions
1388 (mapc (lambda (fn)
1389 (funcall fn beg end))
1390 c-get-state-before-change-functions))
1392 ;; The following must be done here rather than in `c-after-change' because
1393 ;; newly inserted parens would foul up the invalidation algorithm.
1394 (c-invalidate-state-cache beg)))
1396 (defvar c-in-after-change-fontification nil)
1397 (make-variable-buffer-local 'c-in-after-change-fontification)
1398 ;; A flag to prevent region expanding stuff being done twice for after-change
1399 ;; fontification.
1401 (defun c-after-change (beg end old-len)
1402 ;; Function put on `after-change-functions' to adjust various caches
1403 ;; etc. Prefer speed to finesse here, since there will be an order
1404 ;; of magnitude more calls to this function than any of the
1405 ;; functions that use the caches.
1407 ;; Note that care must be taken so that this is called before any
1408 ;; font-lock callbacks since we might get calls to functions using
1409 ;; these caches from inside them, and we must thus be sure that this
1410 ;; has already been executed.
1412 ;; This calls the language variable c-before-font-lock-functions, if non nil.
1413 ;; This typically sets `syntax-table' properties.
1415 ;; We can sometimes get two consecutive calls to `after-change-functions'
1416 ;; without an intervening call to `before-change-functions' when reverting
1417 ;; the buffer (see bug #24094). Whatever the cause, assume that the entire
1418 ;; buffer has changed.
1419 (when (not c-just-done-before-change)
1420 (save-restriction
1421 (widen)
1422 (c-before-change (point-min) (point-max))
1423 (setq beg (point-min)
1424 end (point-max)
1425 old-len (- end beg))))
1427 ;; (c-new-BEG c-new-END) will be the region to fontify. It may become
1428 ;; larger than (beg end).
1429 (setq c-new-END (- (+ c-new-END (- end beg)) old-len))
1430 (setq c-old-BEG c-new-BEG c-old-END c-new-END)
1432 (unless (c-called-from-text-property-change-p)
1433 (setq c-just-done-before-change nil)
1434 (c-save-buffer-state (case-fold-search)
1435 ;; When `combine-after-change-calls' is used we might get calls
1436 ;; with regions outside the current narrowing. This has been
1437 ;; observed in Emacs 20.7.
1438 (save-restriction
1439 (save-match-data ; c-recognize-<>-arglists changes match-data
1440 (widen)
1442 (when (> end (point-max))
1443 ;; Some emacsen might return positions past the end. This has been
1444 ;; observed in Emacs 20.7 when rereading a buffer changed on disk
1445 ;; (haven't been able to minimize it, but Emacs 21.3 appears to
1446 ;; work).
1447 (setq end (point-max))
1448 (when (> beg end)
1449 (setq beg end)))
1451 ;; C-y is capable of spuriously converting category properties
1452 ;; c-</>-as-paren-syntax and c-cpp-delimiter into hard syntax-table
1453 ;; properties. Remove these when it happens.
1454 (when (eval-when-compile (memq 'category-properties c-emacs-features))
1455 (c-save-buffer-state ()
1456 (c-clear-char-property-with-value beg end 'syntax-table
1457 c-<-as-paren-syntax)
1458 (c-clear-char-property-with-value beg end 'syntax-table
1459 c->-as-paren-syntax)
1460 (c-clear-char-property-with-value beg end 'syntax-table nil)))
1462 (c-trim-found-types beg end old-len) ; maybe we don't need all of these.
1463 (c-invalidate-sws-region-after beg end old-len)
1464 ;; (c-invalidate-state-cache beg) ; moved to `c-before-change'.
1465 (c-invalidate-find-decl-cache beg)
1467 (when c-recognize-<>-arglists
1468 (c-after-change-check-<>-operators beg end))
1470 (setq c-in-after-change-fontification t)
1471 (save-excursion
1472 (mapc (lambda (fn)
1473 (funcall fn beg end old-len))
1474 c-before-font-lock-functions)))))))
1476 (defun c-fl-decl-start (pos)
1477 ;; If the beginning of the line containing POS is in the middle of a "local"
1478 ;; declaration, return the beginning of that declaration. Otherwise return
1479 ;; nil. Note that declarations, in this sense, can be nested. (A local
1480 ;; declaration is one which does not start outside of struct braces (and
1481 ;; similar) enclosing POS. Brace list braces here are not "similar".
1483 ;; This function is called indirectly from font locking stuff - either from
1484 ;; c-after-change (to prepare for after-change font-locking) or from font
1485 ;; lock context (etc.) fontification.
1486 (goto-char pos)
1487 (let ((lit-start (c-literal-start))
1488 (new-pos pos)
1489 capture-opener
1490 bod-lim bo-decl)
1491 (goto-char (c-point 'bol new-pos))
1492 (when lit-start ; Comment or string.
1493 (goto-char lit-start))
1494 (setq bod-lim (c-determine-limit 500))
1496 ;; In C++ Mode, first check if we are within a (possibly nested) lambda
1497 ;; form capture list.
1498 (when (c-major-mode-is 'c++-mode)
1499 (let ((paren-state (c-parse-state))
1500 opener)
1501 (save-excursion
1502 (while (setq opener (c-pull-open-brace paren-state))
1503 (goto-char opener)
1504 (if (c-looking-at-c++-lambda-capture-list)
1505 (setq capture-opener (point)))))))
1507 (while
1508 ;; Go to a less nested declaration each time round this loop.
1509 (and
1510 (c-syntactic-skip-backward "^;{}" bod-lim t)
1511 (> (point) bod-lim)
1512 (progn (c-forward-syntactic-ws)
1513 (setq bo-decl (point))
1514 ;; Are we looking at a keyword such as "template" or
1515 ;; "typedef" which can decorate a type, or the type itself?
1516 (when (or (looking-at c-prefix-spec-kwds-re)
1517 (c-forward-type t))
1518 ;; We've found another candidate position.
1519 (setq new-pos (min new-pos bo-decl))
1520 (goto-char bo-decl))
1522 ;; Try and go out a level to search again.
1523 (progn
1524 (c-backward-syntactic-ws bod-lim)
1525 (and (> (point) bod-lim)
1526 (or (memq (char-before) '(?\( ?\[))
1527 (and (eq (char-before) ?\<)
1528 (eq (c-get-char-property
1529 (1- (point)) 'syntax-table)
1530 c-<-as-paren-syntax))
1531 (and (eq (char-before) ?{)
1532 (save-excursion
1533 (backward-char)
1534 (consp (c-looking-at-or-maybe-in-bracelist))))
1536 (not (bobp)))
1537 (backward-char)) ; back over (, [, <.
1538 (when (and capture-opener (< capture-opener new-pos))
1539 (setq new-pos capture-opener))
1540 (and (/= new-pos pos) new-pos)))
1542 (defun c-fl-decl-end (pos)
1543 ;; If POS is inside a declarator, return the end of the token that follows
1544 ;; the declarator, otherwise return nil.
1545 (goto-char pos)
1546 (let ((lit-start (c-literal-start))
1547 pos1)
1548 (if lit-start (goto-char lit-start))
1549 (c-backward-syntactic-ws)
1550 (when (setq pos1 (c-on-identifier))
1551 (goto-char pos1)
1552 (let ((lim (save-excursion
1553 (and (c-beginning-of-macro)
1554 (progn (c-end-of-macro) (point))))))
1555 (when (and (c-forward-declarator lim)
1556 (eq (c-forward-token-2 1 nil lim) 0))
1557 (c-backward-syntactic-ws)
1558 (point))))))
1560 (defun c-change-expand-fl-region (_beg _end _old-len)
1561 ;; Expand the region (c-new-BEG c-new-END) to an after-change font-lock
1562 ;; region. This will usually be the smallest sequence of whole lines
1563 ;; containing `c-new-BEG' and `c-new-END', but if `c-new-BEG' is in a
1564 ;; "local" declaration (see `c-fl-decl-start') the beginning of this is used
1565 ;; as the lower bound.
1567 ;; This is called from an after-change-function, but the parameters BEG END
1568 ;; and OLD-LEN are not used.
1569 (if font-lock-mode
1570 (setq c-new-BEG
1571 (or (c-fl-decl-start c-new-BEG) (c-point 'bol c-new-BEG))
1572 c-new-END
1573 (or (c-fl-decl-end c-new-END)
1574 (c-point 'bonl (max (1- c-new-END) (point-min)))))))
1576 (defun c-context-expand-fl-region (beg end)
1577 ;; Return a cons (NEW-BEG . NEW-END), where NEW-BEG is the beginning of a
1578 ;; "local" declaration containing BEG (see `c-fl-decl-start') or BOL BEG is
1579 ;; in. NEW-END is beginning of the line after the one END is in.
1580 (c-save-buffer-state ()
1581 (cons (or (c-fl-decl-start beg) (c-point 'bol beg))
1582 (or (c-fl-decl-end end) (c-point 'bonl (1- end))))))
1584 (defun c-before-context-fl-expand-region (beg end)
1585 ;; Expand the region (BEG END) as specified by
1586 ;; `c-before-context-fontification-functions'. Return a cons of the bounds
1587 ;; of the new region.
1588 (save-restriction
1589 (widen)
1590 (save-excursion
1591 (let ((new-beg beg) (new-end end)
1592 (new-region (cons beg end)))
1593 (mapc (lambda (fn)
1594 (setq new-region (funcall fn new-beg new-end))
1595 (setq new-beg (car new-region) new-end (cdr new-region)))
1596 c-before-context-fontification-functions)
1597 new-region))))
1599 (defun c-font-lock-fontify-region (beg end &optional verbose)
1600 ;; Effectively advice around `font-lock-fontify-region' which extends the
1601 ;; region (BEG END), for example, to avoid context fontification chopping
1602 ;; off the start of the context. Do not extend the region if it's already
1603 ;; been done (i.e. from an after-change fontification. An example (C++)
1604 ;; where the chopping off used to happen is this:
1606 ;; template <typename T>
1609 ;; void myfunc(T* p) {}
1611 ;; Type a space in the first blank line, and the fontification of the next
1612 ;; line was fouled up by context fontification.
1613 (let (new-beg new-end new-region case-fold-search)
1614 (if (and c-in-after-change-fontification
1615 (< beg c-new-END) (> end c-new-BEG))
1616 ;; Region and the latest after-change fontification region overlap.
1617 ;; Determine the upper and lower bounds of our adjusted region
1618 ;; separately.
1619 (progn
1620 (if (<= beg c-new-BEG)
1621 (setq c-in-after-change-fontification nil))
1622 (setq new-beg
1623 (if (and (>= beg (c-point 'bol c-new-BEG))
1624 (<= beg c-new-BEG))
1625 ;; Either jit-lock has accepted `c-new-BEG', or has
1626 ;; (probably) extended the change region spuriously to
1627 ;; BOL, which position likely has a syntactically
1628 ;; different position. To ensure correct fontification,
1629 ;; we start at `c-new-BEG', assuming any characters to the
1630 ;; left of `c-new-BEG' on the line do not require
1631 ;; fontification.
1632 c-new-BEG
1633 (setq new-region (c-before-context-fl-expand-region beg end)
1634 new-end (cdr new-region))
1635 (car new-region)))
1636 (setq new-end
1637 (if (and (>= end (c-point 'bol c-new-END))
1638 (<= end c-new-END))
1639 c-new-END
1640 (or new-end
1641 (cdr (c-before-context-fl-expand-region beg end))))))
1642 ;; Context (etc.) fontification.
1643 (setq new-region (c-before-context-fl-expand-region beg end)
1644 new-beg (car new-region) new-end (cdr new-region)))
1645 (funcall (default-value 'font-lock-fontify-region-function)
1646 new-beg new-end verbose)))
1648 (defun c-after-font-lock-init ()
1649 ;; Put on `font-lock-mode-hook'. This function ensures our after-change
1650 ;; function will get executed before the font-lock one.
1651 (when (memq #'c-after-change after-change-functions)
1652 (remove-hook 'after-change-functions #'c-after-change t)
1653 (add-hook 'after-change-functions #'c-after-change nil t)))
1655 (defun c-font-lock-init ()
1656 "Set up the font-lock variables for using the font-lock support in CC Mode.
1657 This does not load the font-lock package. Use after
1658 `c-basic-common-init' and after cc-fonts has been loaded.
1659 This function is called from `c-common-init', once per mode initialization."
1661 (set (make-local-variable 'font-lock-defaults)
1662 `(,(if (c-major-mode-is 'awk-mode)
1663 ;; awk-mode currently has only one font lock level.
1664 'awk-font-lock-keywords
1665 (mapcar 'c-mode-symbol
1666 '("font-lock-keywords" "font-lock-keywords-1"
1667 "font-lock-keywords-2" "font-lock-keywords-3")))
1668 nil nil
1669 ,c-identifier-syntax-modifications
1670 c-beginning-of-syntax
1671 (font-lock-mark-block-function
1672 . c-mark-function)))
1674 ;; Prevent `font-lock-default-fontify-region' extending the region it will
1675 ;; fontify to whole lines by removing `font-lock-extend-region-wholelines'
1676 ;; from `font-lock-extend-region-functions'. (Emacs only). This fixes
1677 ;; Emacs bug #19669.
1678 (when (boundp 'font-lock-extend-region-functions)
1679 (setq font-lock-extend-region-functions
1680 (delq 'font-lock-extend-region-wholelines
1681 font-lock-extend-region-functions)))
1683 (make-local-variable 'font-lock-fontify-region-function)
1684 (setq font-lock-fontify-region-function 'c-font-lock-fontify-region)
1686 (if (featurep 'xemacs)
1687 (make-local-hook 'font-lock-mode-hook))
1688 (add-hook 'font-lock-mode-hook 'c-after-font-lock-init nil t))
1690 ;; Emacs 22 and later.
1691 (defun c-extend-after-change-region (beg end _old-len)
1692 "Extend the region to be fontified, if necessary."
1693 ;; Note: the parameter OLD-LEN is ignored here. This somewhat indirect
1694 ;; implementation exists because it is minimally different from the
1695 ;; stand-alone CC Mode which, lacking
1696 ;; font-lock-extend-after-change-region-function, is forced to use advice
1697 ;; instead.
1699 ;; Of the seven CC Mode languages, currently (2009-05) only C, C++, Objc
1700 ;; (the languages with #define) and AWK Mode make non-null use of this
1701 ;; function.
1702 (when (eq font-lock-support-mode 'jit-lock-mode)
1703 (save-restriction
1704 (widen)
1705 (c-save-buffer-state () ; Protect the undo-list from put-text-property.
1706 (if (< c-new-BEG beg)
1707 (put-text-property c-new-BEG beg 'fontified nil))
1708 (if (> c-new-END end)
1709 (put-text-property end c-new-END 'fontified nil)))))
1710 (cons c-new-BEG c-new-END))
1712 ;; Emacs < 22 and XEmacs
1713 (defmacro c-advise-fl-for-region (function)
1714 `(defadvice ,function (before get-awk-region activate)
1715 ;; Make sure that any string/regexp is completely font-locked.
1716 (when c-buffer-is-cc-mode
1717 (save-excursion
1718 (ad-set-arg 1 c-new-END) ; end
1719 (ad-set-arg 0 c-new-BEG))))) ; beg
1721 (unless (boundp 'font-lock-extend-after-change-region-function)
1722 (c-advise-fl-for-region font-lock-after-change-function)
1723 (c-advise-fl-for-region jit-lock-after-change)
1724 (c-advise-fl-for-region lazy-lock-defer-rest-after-change)
1725 (c-advise-fl-for-region lazy-lock-defer-line-after-change))
1727 ;; Connect up to `electric-indent-mode' (Emacs 24.4 and later).
1728 (defun c-electric-indent-mode-hook ()
1729 ;; Emacs has en/disabled `electric-indent-mode'. Propagate this through to
1730 ;; each CC Mode buffer.
1731 (mapc (lambda (buf)
1732 (with-current-buffer buf
1733 (when c-buffer-is-cc-mode
1734 ;; Don't use `c-toggle-electric-state' here due to recursion.
1735 (setq c-electric-flag electric-indent-mode)
1736 (c-update-modeline))))
1737 (buffer-list)))
1739 (defun c-electric-indent-local-mode-hook ()
1740 ;; Emacs has en/disabled `electric-indent-local-mode' for this buffer.
1741 ;; Propagate this through to this buffer's value of `c-electric-flag'
1742 (when c-buffer-is-cc-mode
1743 (setq c-electric-flag electric-indent-mode)
1744 (c-update-modeline)))
1747 ;; Support for C
1749 (defvar c-mode-syntax-table
1750 (funcall (c-lang-const c-make-mode-syntax-table c))
1751 "Syntax table used in c-mode buffers.")
1753 (c-define-abbrev-table 'c-mode-abbrev-table
1754 '(("else" "else" c-electric-continued-statement 0)
1755 ("while" "while" c-electric-continued-statement 0))
1756 "Abbreviation table used in c-mode buffers.")
1758 (defvar c-mode-map
1759 (let ((map (c-make-inherited-keymap)))
1760 map)
1761 "Keymap used in c-mode buffers.")
1762 ;; Add bindings which are only useful for C.
1763 (define-key c-mode-map "\C-c\C-e" 'c-macro-expand)
1766 (easy-menu-define c-c-menu c-mode-map "C Mode Commands"
1767 (cons "C" (c-lang-const c-mode-menu c)))
1769 ;; In XEmacs >= 21.5 modes should add their own entries to
1770 ;; `auto-mode-alist'. The comment form of autoload is used to avoid
1771 ;; doing this on load. That since `add-to-list' prepends the value
1772 ;; which could cause it to clobber user settings. Later emacsen have
1773 ;; an append option, but it's not safe to use.
1775 ;; The extension ".C" is associated with C++ while the lowercase
1776 ;; variant goes with C. On case insensitive file systems, this means
1777 ;; that ".c" files also might open C++ mode if the C++ entry comes
1778 ;; first on `auto-mode-alist'. Thus we try to ensure that ".C" comes
1779 ;; after ".c", and since `add-to-list' adds the entry first we have to
1780 ;; add the ".C" entry first.
1781 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(cc\\|hh\\)\\'" . c++-mode))
1782 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.[ch]\\(pp\\|xx\\|\\+\\+\\)\\'" . c++-mode))
1783 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(CC?\\|HH?\\)\\'" . c++-mode))
1785 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.c\\'" . c-mode))
1786 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.h\\'" . c-or-c++-mode))
1788 ;; NB: The following two associate yacc and lex files to C Mode, which
1789 ;; is not really suitable for those formats. Anyway, afaik there's
1790 ;; currently no better mode for them, and besides this is legacy.
1791 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.y\\(acc\\)?\\'" . c-mode))
1792 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.lex\\'" . c-mode))
1794 ;; Preprocessed files generated by C and C++ compilers.
1795 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.i\\'" . c-mode))
1796 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.ii\\'" . c++-mode))
1798 (unless (fboundp 'prog-mode) (defalias 'prog-mode 'fundamental-mode))
1800 ;;;###autoload
1801 (define-derived-mode c-mode prog-mode "C"
1802 "Major mode for editing C code.
1804 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1805 c-mode buffer. This automatically sets up a mail buffer with version
1806 information already added. You just need to add a description of the
1807 problem, including a reproducible test case, and send the message.
1809 To see what version of CC Mode you are running, enter `\\[c-version]'.
1811 The hook `c-mode-common-hook' is run with no args at mode
1812 initialization, then `c-mode-hook'.
1814 Key bindings:
1815 \\{c-mode-map}"
1816 :after-hook (progn (c-make-noise-macro-regexps)
1817 (c-make-macro-with-semi-re)
1818 (c-update-modeline))
1819 (c-initialize-cc-mode t)
1820 (setq abbrev-mode t)
1821 (c-init-language-vars-for 'c-mode)
1822 (c-common-init 'c-mode)
1823 (easy-menu-add c-c-menu)
1824 (cc-imenu-init cc-imenu-c-generic-expression)
1825 (c-run-mode-hooks 'c-mode-common-hook))
1827 (defconst c-or-c++-mode--regexp
1828 (eval-when-compile
1829 (let ((id "[a-zA-Z0-9_]+") (ws "[ \t\r]+") (ws-maybe "[ \t\r]*"))
1830 (concat "^" ws-maybe "\\(?:"
1831 "using" ws "\\(?:namespace" ws "std;\\|std::\\)"
1832 "\\|" "namespace" "\\(:?" ws id "\\)?" ws-maybe "{"
1833 "\\|" "class" ws id ws-maybe "[:{\n]"
1834 "\\|" "template" ws-maybe "<.*>"
1835 "\\|" "#include" ws-maybe "<\\(?:string\\|iostream\\|map\\)>"
1836 "\\)")))
1837 "A regexp applied to C header files to check if they are really C++.")
1839 ;;;###autoload
1840 (defun c-or-c++-mode ()
1841 "Analyse buffer and enable either C or C++ mode.
1843 Some people and projects use .h extension for C++ header files
1844 which is also the one used for C header files. This makes
1845 matching on file name insufficient for detecting major mode that
1846 should be used.
1848 This function attempts to use file contents to determine whether
1849 the code is C or C++ and based on that chooses whether to enable
1850 `c-mode' or `c++-mode'."
1851 (if (save-excursion
1852 (save-restriction
1853 (save-match-data
1854 (widen)
1855 (goto-char (point-min))
1856 (re-search-forward c-or-c++-mode--regexp
1857 (+ (point) c-guess-region-max) t))))
1858 (c++-mode)
1859 (c-mode)))
1862 ;; Support for C++
1864 (defvar c++-mode-syntax-table
1865 (funcall (c-lang-const c-make-mode-syntax-table c++))
1866 "Syntax table used in c++-mode buffers.")
1868 (c-define-abbrev-table 'c++-mode-abbrev-table
1869 '(("else" "else" c-electric-continued-statement 0)
1870 ("while" "while" c-electric-continued-statement 0)
1871 ("catch" "catch" c-electric-continued-statement 0))
1872 "Abbreviation table used in c++-mode buffers.")
1874 (defvar c++-mode-map
1875 (let ((map (c-make-inherited-keymap)))
1876 map)
1877 "Keymap used in c++-mode buffers.")
1878 ;; Add bindings which are only useful for C++.
1879 (define-key c++-mode-map "\C-c\C-e" 'c-macro-expand)
1880 (define-key c++-mode-map "\C-c:" 'c-scope-operator)
1881 (define-key c++-mode-map "<" 'c-electric-lt-gt)
1882 (define-key c++-mode-map ">" 'c-electric-lt-gt)
1884 (easy-menu-define c-c++-menu c++-mode-map "C++ Mode Commands"
1885 (cons "C++" (c-lang-const c-mode-menu c++)))
1887 ;;;###autoload
1888 (define-derived-mode c++-mode prog-mode "C++"
1889 "Major mode for editing C++ code.
1890 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1891 c++-mode buffer. This automatically sets up a mail buffer with
1892 version information already added. You just need to add a description
1893 of the problem, including a reproducible test case, and send the
1894 message.
1896 To see what version of CC Mode you are running, enter `\\[c-version]'.
1898 The hook `c-mode-common-hook' is run with no args at mode
1899 initialization, then `c++-mode-hook'.
1901 Key bindings:
1902 \\{c++-mode-map}"
1903 :after-hook (progn (c-make-noise-macro-regexps)
1904 (c-make-macro-with-semi-re)
1905 (c-update-modeline))
1906 (c-initialize-cc-mode t)
1907 (setq abbrev-mode t)
1908 (c-init-language-vars-for 'c++-mode)
1909 (c-common-init 'c++-mode)
1910 (easy-menu-add c-c++-menu)
1911 (cc-imenu-init cc-imenu-c++-generic-expression)
1912 (c-run-mode-hooks 'c-mode-common-hook))
1915 ;; Support for Objective-C
1917 (defvar objc-mode-syntax-table
1918 (funcall (c-lang-const c-make-mode-syntax-table objc))
1919 "Syntax table used in objc-mode buffers.")
1921 (c-define-abbrev-table 'objc-mode-abbrev-table
1922 '(("else" "else" c-electric-continued-statement 0)
1923 ("while" "while" c-electric-continued-statement 0))
1924 "Abbreviation table used in objc-mode buffers.")
1926 (defvar objc-mode-map
1927 (let ((map (c-make-inherited-keymap)))
1928 map)
1929 "Keymap used in objc-mode buffers.")
1930 ;; Add bindings which are only useful for Objective-C.
1931 (define-key objc-mode-map "\C-c\C-e" 'c-macro-expand)
1933 (easy-menu-define c-objc-menu objc-mode-map "ObjC Mode Commands"
1934 (cons "ObjC" (c-lang-const c-mode-menu objc)))
1936 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.m\\'" . objc-mode))
1938 ;;;###autoload
1939 (define-derived-mode objc-mode prog-mode "ObjC"
1940 "Major mode for editing Objective C code.
1941 To submit a problem report, enter `\\[c-submit-bug-report]' from an
1942 objc-mode buffer. This automatically sets up a mail buffer with
1943 version information already added. You just need to add a description
1944 of the problem, including a reproducible test case, and send the
1945 message.
1947 To see what version of CC Mode you are running, enter `\\[c-version]'.
1949 The hook `c-mode-common-hook' is run with no args at mode
1950 initialization, then `objc-mode-hook'.
1952 Key bindings:
1953 \\{objc-mode-map}"
1954 :after-hook (progn (c-make-noise-macro-regexps)
1955 (c-make-macro-with-semi-re)
1956 (c-update-modeline))
1957 (c-initialize-cc-mode t)
1958 (setq abbrev-mode t)
1959 (c-init-language-vars-for 'objc-mode)
1960 (c-common-init 'objc-mode)
1961 (easy-menu-add c-objc-menu)
1962 (cc-imenu-init nil 'cc-imenu-objc-function)
1963 (c-run-mode-hooks 'c-mode-common-hook))
1966 ;; Support for Java
1968 (defvar java-mode-syntax-table
1969 (funcall (c-lang-const c-make-mode-syntax-table java))
1970 "Syntax table used in java-mode buffers.")
1972 (c-define-abbrev-table 'java-mode-abbrev-table
1973 '(("else" "else" c-electric-continued-statement 0)
1974 ("while" "while" c-electric-continued-statement 0)
1975 ("catch" "catch" c-electric-continued-statement 0)
1976 ("finally" "finally" c-electric-continued-statement 0))
1977 "Abbreviation table used in java-mode buffers.")
1979 (defvar java-mode-map
1980 (let ((map (c-make-inherited-keymap)))
1981 map)
1982 "Keymap used in java-mode buffers.")
1983 ;; Add bindings which are only useful for Java.
1985 ;; Regexp trying to describe the beginning of a Java top-level
1986 ;; definition. This is not used by CC Mode, nor is it maintained
1987 ;; since it's practically impossible to write a regexp that reliably
1988 ;; matches such a construct. Other tools are necessary.
1989 (defconst c-Java-defun-prompt-regexp
1990 "^[ \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-*")
1992 (easy-menu-define c-java-menu java-mode-map "Java Mode Commands"
1993 (cons "Java" (c-lang-const c-mode-menu java)))
1995 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.java\\'" . java-mode))
1997 ;;;###autoload
1998 (define-derived-mode java-mode prog-mode "Java"
1999 "Major mode for editing Java code.
2000 To submit a problem report, enter `\\[c-submit-bug-report]' from a
2001 java-mode buffer. This automatically sets up a mail buffer with
2002 version information already added. You just need to add a description
2003 of the problem, including a reproducible test case, and send the
2004 message.
2006 To see what version of CC Mode you are running, enter `\\[c-version]'.
2008 The hook `c-mode-common-hook' is run with no args at mode
2009 initialization, then `java-mode-hook'.
2011 Key bindings:
2012 \\{java-mode-map}"
2013 :after-hook (c-update-modeline)
2014 (c-initialize-cc-mode t)
2015 (setq abbrev-mode t)
2016 (c-init-language-vars-for 'java-mode)
2017 (c-common-init 'java-mode)
2018 (easy-menu-add c-java-menu)
2019 (cc-imenu-init cc-imenu-java-generic-expression)
2020 (c-run-mode-hooks 'c-mode-common-hook))
2023 ;; Support for CORBA's IDL language
2025 (defvar idl-mode-syntax-table
2026 (funcall (c-lang-const c-make-mode-syntax-table idl))
2027 "Syntax table used in idl-mode buffers.")
2029 (c-define-abbrev-table 'idl-mode-abbrev-table nil
2030 "Abbreviation table used in idl-mode buffers.")
2032 (defvar idl-mode-map
2033 (let ((map (c-make-inherited-keymap)))
2034 map)
2035 "Keymap used in idl-mode buffers.")
2036 ;; Add bindings which are only useful for IDL.
2038 (easy-menu-define c-idl-menu idl-mode-map "IDL Mode Commands"
2039 (cons "IDL" (c-lang-const c-mode-menu idl)))
2041 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.idl\\'" . idl-mode))
2043 ;;;###autoload
2044 (define-derived-mode idl-mode prog-mode "IDL"
2045 "Major mode for editing CORBA's IDL, PSDL and CIDL code.
2046 To submit a problem report, enter `\\[c-submit-bug-report]' from an
2047 idl-mode buffer. This automatically sets up a mail buffer with
2048 version information already added. You just need to add a description
2049 of the problem, including a reproducible test case, and send the
2050 message.
2052 To see what version of CC Mode you are running, enter `\\[c-version]'.
2054 The hook `c-mode-common-hook' is run with no args at mode
2055 initialization, then `idl-mode-hook'.
2057 Key bindings:
2058 \\{idl-mode-map}"
2059 :after-hook (c-update-modeline)
2060 (c-initialize-cc-mode t)
2061 (c-init-language-vars-for 'idl-mode)
2062 (c-common-init 'idl-mode)
2063 (easy-menu-add c-idl-menu)
2064 ;;(cc-imenu-init cc-imenu-idl-generic-expression) ;TODO
2065 (c-run-mode-hooks 'c-mode-common-hook))
2068 ;; Support for Pike
2070 (defvar pike-mode-syntax-table
2071 (funcall (c-lang-const c-make-mode-syntax-table pike))
2072 "Syntax table used in pike-mode buffers.")
2074 (c-define-abbrev-table 'pike-mode-abbrev-table
2075 '(("else" "else" c-electric-continued-statement 0)
2076 ("while" "while" c-electric-continued-statement 0))
2077 "Abbreviation table used in pike-mode buffers.")
2079 (defvar pike-mode-map
2080 (let ((map (c-make-inherited-keymap)))
2081 map)
2082 "Keymap used in pike-mode buffers.")
2083 ;; Additional bindings.
2084 (define-key pike-mode-map "\C-c\C-e" 'c-macro-expand)
2086 (easy-menu-define c-pike-menu pike-mode-map "Pike Mode Commands"
2087 (cons "Pike" (c-lang-const c-mode-menu pike)))
2089 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(u?lpc\\|pike\\|pmod\\(\\.in\\)?\\)\\'" . pike-mode))
2090 ;;;###autoload (add-to-list 'interpreter-mode-alist '("pike" . pike-mode))
2092 ;;;###autoload
2093 (define-derived-mode pike-mode prog-mode "Pike"
2094 "Major mode for editing Pike code.
2095 To submit a problem report, enter `\\[c-submit-bug-report]' from a
2096 pike-mode buffer. This automatically sets up a mail buffer with
2097 version information already added. You just need to add a description
2098 of the problem, including a reproducible test case, and send the
2099 message.
2101 To see what version of CC Mode you are running, enter `\\[c-version]'.
2103 The hook `c-mode-common-hook' is run with no args at mode
2104 initialization, then `pike-mode-hook'.
2106 Key bindings:
2107 \\{pike-mode-map}"
2108 :after-hook (c-update-modeline)
2109 (c-initialize-cc-mode t)
2110 (setq abbrev-mode t)
2111 (c-init-language-vars-for 'pike-mode)
2112 (c-common-init 'pike-mode)
2113 (easy-menu-add c-pike-menu)
2114 ;;(cc-imenu-init cc-imenu-pike-generic-expression) ;TODO
2115 (c-run-mode-hooks 'c-mode-common-hook))
2118 ;; Support for AWK
2120 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.awk\\'" . awk-mode))
2121 ;;;###autoload (add-to-list 'interpreter-mode-alist '("awk" . awk-mode))
2122 ;;;###autoload (add-to-list 'interpreter-mode-alist '("mawk" . awk-mode))
2123 ;;;###autoload (add-to-list 'interpreter-mode-alist '("nawk" . awk-mode))
2124 ;;;###autoload (add-to-list 'interpreter-mode-alist '("gawk" . awk-mode))
2126 (c-define-abbrev-table 'awk-mode-abbrev-table
2127 '(("else" "else" c-electric-continued-statement 0)
2128 ("while" "while" c-electric-continued-statement 0))
2129 "Abbreviation table used in awk-mode buffers.")
2131 (defvar awk-mode-map
2132 (let ((map (c-make-inherited-keymap)))
2133 map)
2134 "Keymap used in awk-mode buffers.")
2135 ;; Add bindings which are only useful for awk.
2136 (define-key awk-mode-map "#" 'self-insert-command);Overrides electric parent binding.
2137 (define-key awk-mode-map "/" 'self-insert-command);Overrides electric parent binding.
2138 (define-key awk-mode-map "*" 'self-insert-command);Overrides electric parent binding.
2139 (define-key awk-mode-map "\C-c\C-n" 'undefined) ; #if doesn't exist in awk.
2140 (define-key awk-mode-map "\C-c\C-p" 'undefined)
2141 (define-key awk-mode-map "\C-c\C-u" 'undefined)
2142 (define-key awk-mode-map "\M-a" 'c-beginning-of-statement) ; 2003/10/7
2143 (define-key awk-mode-map "\M-e" 'c-end-of-statement) ; 2003/10/7
2144 (define-key awk-mode-map "\C-\M-a" 'c-awk-beginning-of-defun)
2145 (define-key awk-mode-map "\C-\M-e" 'c-awk-end-of-defun)
2147 (easy-menu-define c-awk-menu awk-mode-map "AWK Mode Commands"
2148 (cons "AWK" (c-lang-const c-mode-menu awk)))
2150 ;; (require 'cc-awk) brings these in.
2151 (defvar awk-mode-syntax-table)
2152 (declare-function c-awk-unstick-NL-prop "cc-awk" ())
2154 ;;;###autoload
2155 (define-derived-mode awk-mode prog-mode "AWK"
2156 "Major mode for editing AWK code.
2157 To submit a problem report, enter `\\[c-submit-bug-report]' from an
2158 awk-mode buffer. This automatically sets up a mail buffer with version
2159 information already added. You just need to add a description of the
2160 problem, including a reproducible test case, and send the message.
2162 To see what version of CC Mode you are running, enter `\\[c-version]'.
2164 The hook `c-mode-common-hook' is run with no args at mode
2165 initialization, then `awk-mode-hook'.
2167 Key bindings:
2168 \\{awk-mode-map}"
2169 :after-hook (c-update-modeline)
2170 ;; We need the next line to stop the macro defining
2171 ;; `awk-mode-syntax-table'. This would mask the real table which is
2172 ;; declared in cc-awk.el and hasn't yet been loaded.
2173 :syntax-table nil
2174 (require 'cc-awk) ; Added 2003/6/10.
2175 (c-initialize-cc-mode t)
2176 (set-syntax-table awk-mode-syntax-table)
2177 (setq abbrev-mode t)
2178 (c-init-language-vars-for 'awk-mode)
2179 (c-common-init 'awk-mode)
2180 (c-awk-unstick-NL-prop)
2181 (c-run-mode-hooks 'c-mode-common-hook))
2184 ;; bug reporting
2186 (defconst c-mode-help-address
2187 "submit@debbugs.gnu.org"
2188 "Address(es) for CC Mode bug reports.")
2190 (defun c-version ()
2191 "Echo the current version of CC Mode in the minibuffer."
2192 (interactive)
2193 (message "Using CC Mode version %s" c-version)
2194 (c-keep-region-active))
2196 (define-obsolete-variable-alias 'c-prepare-bug-report-hooks
2197 'c-prepare-bug-report-hook "24.3")
2198 (defvar c-prepare-bug-report-hook nil)
2200 ;; Dynamic variables used by reporter.
2201 (defvar reporter-prompt-for-summary-p)
2202 (defvar reporter-dont-compact-list)
2204 ;; This could be "emacs,cc-mode" in the version included in Emacs.
2205 (defconst c-mode-bug-package "cc-mode"
2206 "The package to use in the bug submission.")
2208 ;; reporter-submit-bug-report requires sendmail.
2209 (declare-function mail-position-on-field "sendmail" (field &optional soft))
2211 (defun c-submit-bug-report ()
2212 "Submit via mail a bug report on CC Mode."
2213 (interactive)
2214 (require 'reporter)
2215 ;; load in reporter
2216 (let ((reporter-prompt-for-summary-p t)
2217 (reporter-dont-compact-list '(c-offsets-alist))
2218 (style c-indentation-style)
2219 (c-features c-emacs-features))
2220 (and
2221 (if (y-or-n-p "Do you want to submit a report on CC Mode? ")
2222 t (message "") nil)
2223 (reporter-submit-bug-report
2224 c-mode-help-address
2225 (concat "CC Mode " c-version " (" mode-name ")")
2226 (let ((vars (append
2227 c-style-variables
2228 '(c-buffer-is-cc-mode
2229 c-tab-always-indent
2230 c-syntactic-indentation
2231 c-syntactic-indentation-in-macros
2232 c-ignore-auto-fill
2233 c-auto-align-backslashes
2234 c-backspace-function
2235 c-delete-function
2236 c-electric-pound-behavior
2237 c-default-style
2238 c-enable-xemacs-performance-kludge-p
2239 c-old-style-variable-behavior
2240 defun-prompt-regexp
2241 tab-width
2242 comment-column
2243 parse-sexp-ignore-comments
2244 parse-sexp-lookup-properties
2245 lookup-syntax-properties
2246 ;; A brain-damaged XEmacs only variable that, if
2247 ;; set to nil can cause all kinds of chaos.
2248 signal-error-on-buffer-boundary
2249 ;; Variables that affect line breaking and comments.
2250 auto-fill-mode
2251 auto-fill-function
2252 filladapt-mode
2253 comment-multi-line
2254 comment-start-skip
2255 fill-prefix
2256 fill-column
2257 paragraph-start
2258 adaptive-fill-mode
2259 adaptive-fill-regexp)
2260 nil)))
2261 (mapc (lambda (var) (unless (boundp var)
2262 (setq vars (delq var vars))))
2263 '(signal-error-on-buffer-boundary
2264 filladapt-mode
2265 defun-prompt-regexp
2266 font-lock-mode
2267 auto-fill-mode
2268 font-lock-maximum-decoration
2269 parse-sexp-lookup-properties
2270 lookup-syntax-properties))
2271 vars)
2272 (lambda ()
2273 (run-hooks 'c-prepare-bug-report-hook)
2274 (save-excursion
2275 (or (mail-position-on-field "X-Debbugs-Package")
2276 (insert c-mode-bug-package)))
2277 (insert (format "Buffer Style: %s\nc-emacs-features: %s\n"
2278 style c-features)))))))
2281 (cc-provide 'cc-mode)
2283 ;; Local Variables:
2284 ;; indent-tabs-mode: t
2285 ;; tab-width: 8
2286 ;; End:
2287 ;;; cc-mode.el ends here