1 ;;; cc-mode.el --- major mode for editing C and similar languages
3 ;; Copyright (C) 1985, 1987, 1992-2016 Free Software Foundation, Inc.
5 ;; Authors: 2003- Alan Mackenzie
6 ;; 1998- Martin Stjernholm
7 ;; 1992-1999 Barry A. Warsaw
10 ;; 1985 Richard M. Stallman
11 ;; Maintainer: bug-cc-mode@gnu.org
12 ;; Created: a long, long, time ago. adapted from the original c-mode.el
13 ;; Keywords: c languages
15 ;; This file is part of GNU Emacs.
17 ;; GNU Emacs is free software: you can redistribute it and/or modify
18 ;; it under the terms of the GNU General Public License as published by
19 ;; the Free Software Foundation, either version 3 of the License, or
20 ;; (at your option) any later version.
22 ;; GNU Emacs is distributed in the hope that it will be useful,
23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 ;; GNU General Public License for more details.
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
32 ;; NOTE: Read the commentary below for the right way to submit bug reports!
33 ;; NOTE: See the accompanying texinfo manual for details on using this mode!
34 ;; Note: The version string is in cc-defs.
36 ;; This package provides GNU Emacs major modes for editing C, C++,
37 ;; Objective-C, Java, CORBA's IDL, Pike and AWK code. As of the
38 ;; latest Emacs and XEmacs releases, it is the default package for
39 ;; editing these languages. This package is called "CC Mode", and
40 ;; should be spelled exactly this way.
42 ;; CC Mode supports K&R and ANSI C, ANSI C++, Objective-C, Java,
43 ;; CORBA's IDL, Pike and AWK with a consistent indentation model
44 ;; across all modes. This indentation model is intuitive and very
45 ;; flexible, so that almost any desired style of indentation can be
46 ;; supported. Installation, usage, and programming details are
47 ;; contained in an accompanying texinfo manual.
49 ;; CC Mode's immediate ancestors were, c++-mode.el, cplus-md.el, and
52 ;; To submit bug reports, type "C-c C-b". These will be sent to
53 ;; bug-gnu-emacs@gnu.org (mirrored as the Usenet newsgroup
54 ;; gnu.emacs.bug) as well as bug-cc-mode@gnu.org, which directly
55 ;; contacts the CC Mode maintainers. Questions can sent to
56 ;; help-gnu-emacs@gnu.org (mirrored as gnu.emacs.help) and/or
57 ;; bug-cc-mode@gnu.org. Please do not send bugs or questions to our
58 ;; personal accounts; we reserve the right to ignore such email!
60 ;; Many, many thanks go out to all the folks on the beta test list.
61 ;; Without their patience, testing, insight, code contributions, and
62 ;; encouragement CC Mode would be a far inferior package.
64 ;; You can get the latest version of CC Mode, including PostScript
65 ;; documentation and separate individual files from:
67 ;; http://cc-mode.sourceforge.net/
69 ;; You can join a moderated CC Mode announcement-only mailing list by
72 ;; http://lists.sourceforge.net/mailman/listinfo/cc-mode-announce
78 (unless (fboundp 'declare-function) (defmacro declare-function (&rest _r))))
82 (if (and (boundp 'byte-compile-dest-file)
83 (stringp byte-compile-dest-file))
84 (cons (file-name-directory byte-compile-dest-file) load-path)
86 (load "cc-bytecomp" nil t)))
90 (cc-require-when-compile 'cc-langs)
91 (cc-require 'cc-engine)
92 (cc-require 'cc-styles)
94 (cc-require 'cc-align)
95 (cc-require 'cc-menus)
96 (cc-require 'cc-guess)
98 ;; Silence the compiler.
99 (cc-bytecomp-defvar adaptive-fill-first-line-regexp) ; Emacs
100 (cc-bytecomp-defun run-mode-hooks) ; Emacs 21.1
102 ;; We set this variable during mode init, yet we don't require
104 (cc-bytecomp-defvar font-lock-defaults)
106 ;; Menu support for both XEmacs and Emacs. If you don't have easymenu
107 ;; with your version of Emacs, you are incompatible!
108 (cc-external-require 'easymenu)
110 ;; Load cc-fonts first after font-lock is loaded, since it isn't
111 ;; necessary until font locking is requested.
112 ; (eval-after-load "font-lock" ; 2006-07-09: font-lock is now preloaded.
114 (require 'cc-fonts) ;)
117 ;; Other modes and packages which depend on CC Mode should do the
118 ;; following to make sure everything is loaded and available for their
121 ;; (require 'cc-mode)
123 ;; And in the major mode function:
125 ;; (c-initialize-cc-mode t)
126 ;; (c-init-language-vars some-mode)
127 ;; (c-common-init 'some-mode) ; Or perhaps (c-basic-common-init 'some-mode)
129 ;; If you're not writing a derived mode using the language variable
130 ;; system, then some-mode is one of the language modes directly
131 ;; supported by CC Mode. You can then use (c-init-language-vars-for
132 ;; 'some-mode) instead of `c-init-language-vars'.
133 ;; `c-init-language-vars-for' is a function that avoids the rather
134 ;; large expansion of `c-init-language-vars'.
136 ;; If you use `c-basic-common-init' then you might want to call
137 ;; `c-font-lock-init' too to set up CC Mode's font lock support.
139 ;; See cc-langs.el for further info. A small example of a derived mode
140 ;; is also available at <http://cc-mode.sourceforge.net/
141 ;; derived-mode-ex.el>.
143 (defun c-leave-cc-mode-mode ()
144 (setq c-buffer-is-cc-mode nil))
146 (defun c-init-language-vars-for (mode)
147 "Initialize the language variables for one of the language modes
148 directly supported by CC Mode. This can be used instead of the
149 `c-init-language-vars' macro if the language you want to use is one of
150 those, rather than a derived language defined through the language
151 variable system (see \"cc-langs.el\")."
152 (cond ((eq mode 'c-mode) (c-init-language-vars c-mode))
153 ((eq mode 'c++-mode) (c-init-language-vars c++-mode))
154 ((eq mode 'objc-mode) (c-init-language-vars objc-mode))
155 ((eq mode 'java-mode) (c-init-language-vars java-mode))
156 ((eq mode 'idl-mode) (c-init-language-vars idl-mode))
157 ((eq mode 'pike-mode) (c-init-language-vars pike-mode))
158 ((eq mode 'awk-mode) (c-init-language-vars awk-mode))
159 (t (error "Unsupported mode %s" mode))))
162 (defun c-initialize-cc-mode (&optional new-style-init)
163 "Initialize CC Mode for use in the current buffer.
164 If the optional NEW-STYLE-INIT is nil or left out then all necessary
165 initialization to run CC Mode for the C language is done. Otherwise
166 only some basic setup is done, and a call to `c-init-language-vars' or
167 `c-init-language-vars-for' is necessary too (which gives more
168 control). See \"cc-mode.el\" for more info."
170 (setq c-buffer-is-cc-mode t)
172 (let ((initprop 'cc-mode-is-initialized)
174 (unless (get 'c-initialize-cc-mode initprop)
177 (put 'c-initialize-cc-mode initprop t)
178 (c-initialize-builtin-style)
179 (run-hooks 'c-initialization-hook)
180 ;; Fix obsolete variables.
181 (if (boundp 'c-comment-continuation-stars)
182 (setq c-block-comment-prefix c-comment-continuation-stars))
183 (add-hook 'change-major-mode-hook 'c-leave-cc-mode-mode)
184 (setq c-initialization-ok t)
185 ;; Connect up with Emacs's electric-indent-mode, for >= Emacs 24.4
186 (when (fboundp 'electric-indent-local-mode)
187 (add-hook 'electric-indent-mode-hook 'c-electric-indent-mode-hook)
188 (add-hook 'electric-indent-local-mode-hook
189 'c-electric-indent-local-mode-hook)))
190 ;; Will try initialization hooks again if they failed.
191 (put 'c-initialize-cc-mode initprop c-initialization-ok))))
193 (unless new-style-init
194 (c-init-language-vars-for 'c-mode)))
199 (defvar c-mode-base-map ()
200 "Keymap shared by all CC Mode related modes.")
202 (defun c-make-inherited-keymap ()
203 (let ((map (make-sparse-keymap)))
204 ;; Necessary to use `cc-bytecomp-fboundp' below since this
205 ;; function is called from top-level forms that are evaluated
206 ;; while cc-bytecomp is active when one does M-x eval-buffer.
209 ((cc-bytecomp-fboundp 'set-keymap-parent)
210 (set-keymap-parent map c-mode-base-map))
212 ((fboundp 'set-keymap-parents)
213 (set-keymap-parents map c-mode-base-map))
215 (t (error "CC Mode is incompatible with this version of Emacs")))
218 (defun c-define-abbrev-table (name defs &optional doc)
219 ;; Compatibility wrapper for `define-abbrev' which passes a non-nil
220 ;; sixth argument for SYSTEM-FLAG in emacsen that support it
221 ;; (currently only Emacs >= 21.2).
222 (let ((table (or (and (boundp name) (symbol-value name))
223 (progn (condition-case nil
224 (define-abbrev-table name nil doc)
225 (wrong-number-of-arguments ;E.g. Emacs<23.
226 (eval `(defvar ,name nil ,doc))
227 (define-abbrev-table name nil)))
228 (symbol-value name)))))
231 (apply 'define-abbrev table (append (car defs) '(t)))
232 (wrong-number-of-arguments
233 (apply 'define-abbrev table (car defs))))
234 (setq defs (cdr defs)))))
235 (put 'c-define-abbrev-table 'lisp-indent-function 1)
237 (defun c-bind-special-erase-keys ()
238 ;; Only used in Emacs to bind C-c C-<delete> and C-c C-<backspace>
239 ;; to the proper keys depending on `normal-erase-is-backspace'.
240 (if normal-erase-is-backspace
242 (define-key c-mode-base-map (kbd "C-c C-<delete>")
243 'c-hungry-delete-forward)
244 (define-key c-mode-base-map (kbd "C-c C-<backspace>")
245 'c-hungry-delete-backwards))
246 (define-key c-mode-base-map (kbd "C-c C-<delete>")
247 'c-hungry-delete-backwards)
248 (define-key c-mode-base-map (kbd "C-c C-<backspace>")
249 'c-hungry-delete-forward)))
254 (setq c-mode-base-map (make-sparse-keymap))
256 ;; Separate M-BS from C-M-h. The former should remain
257 ;; backward-kill-word.
258 (define-key c-mode-base-map [(control meta h)] 'c-mark-function)
259 (define-key c-mode-base-map "\e\C-q" 'c-indent-exp)
260 (substitute-key-definition 'backward-sentence
261 'c-beginning-of-statement
262 c-mode-base-map global-map)
263 (substitute-key-definition 'forward-sentence
265 c-mode-base-map global-map)
266 (substitute-key-definition 'indent-new-comment-line
267 'c-indent-new-comment-line
268 c-mode-base-map global-map)
269 (substitute-key-definition 'indent-for-tab-command
270 ;; XXX Is this the right thing to do
272 'c-indent-line-or-region
273 c-mode-base-map global-map)
274 (when (fboundp 'comment-indent-new-line)
275 ;; indent-new-comment-line has changed name to
276 ;; comment-indent-new-line in Emacs 21.
277 (substitute-key-definition 'comment-indent-new-line
278 'c-indent-new-comment-line
279 c-mode-base-map global-map))
281 ;; RMS says don't make these the default.
282 ;; (April 2006): RMS has now approved these commands as defaults.
283 (unless (memq 'argumentative-bod-function c-emacs-features)
284 (define-key c-mode-base-map "\e\C-a" 'c-beginning-of-defun)
285 (define-key c-mode-base-map "\e\C-e" 'c-end-of-defun))
287 (define-key c-mode-base-map "\C-c\C-n" 'c-forward-conditional)
288 (define-key c-mode-base-map "\C-c\C-p" 'c-backward-conditional)
289 (define-key c-mode-base-map "\C-c\C-u" 'c-up-conditional)
291 ;; It doesn't suffice to put `c-fill-paragraph' on
292 ;; `fill-paragraph-function' since `c-fill-paragraph' must be called
293 ;; before any fill prefix adaption is done. E.g. `filladapt-mode'
294 ;; replaces `fill-paragraph' and does the adaption before calling
295 ;; `fill-paragraph-function', and we have to mask comments etc
296 ;; before that. Also, `c-fill-paragraph' chains on to
297 ;; `fill-paragraph' and the value on `fill-paragraph-function' to
298 ;; do the actual filling work.
299 (substitute-key-definition 'fill-paragraph 'c-fill-paragraph
300 c-mode-base-map global-map)
301 ;; In XEmacs the default fill function is called
302 ;; fill-paragraph-or-region.
303 (substitute-key-definition 'fill-paragraph-or-region 'c-fill-paragraph
304 c-mode-base-map global-map)
306 ;; We bind the forward deletion key and (implicitly) C-d to
307 ;; `c-electric-delete-forward', and the backward deletion key to
308 ;; `c-electric-backspace'. The hungry variants are bound to the
309 ;; same keys but prefixed with C-c. This implies that C-c C-d is
310 ;; `c-hungry-delete-forward'. For consistency, we bind not only C-c
311 ;; <backspace> to `c-hungry-delete-backwards' but also
312 ;; C-c C-<backspace>, so that the Ctrl key can be held down during
313 ;; the whole sequence regardless of the direction. This in turn
314 ;; implies that we bind C-c C-<delete> to `c-hungry-delete-forward',
315 ;; for the same reason.
317 ;; Bind the electric deletion functions to C-d and DEL. Emacs 21
318 ;; automatically maps the [delete] and [backspace] keys to these two
319 ;; depending on window system and user preferences. (In earlier
320 ;; versions it's possible to do the same by using `function-key-map'.)
321 (define-key c-mode-base-map "\C-d" 'c-electric-delete-forward)
322 (define-key c-mode-base-map "\177" 'c-electric-backspace)
323 (define-key c-mode-base-map "\C-c\C-d" 'c-hungry-delete-forward)
324 (define-key c-mode-base-map [?\C-c ?\d] 'c-hungry-delete-backwards)
325 (define-key c-mode-base-map [?\C-c ?\C-\d] 'c-hungry-delete-backwards)
326 (define-key c-mode-base-map [?\C-c deletechar] 'c-hungry-delete-forward) ; C-c <delete> on a tty.
327 (define-key c-mode-base-map [?\C-c (control deletechar)] ; C-c C-<delete> on a tty.
328 'c-hungry-delete-forward)
329 (when (boundp 'normal-erase-is-backspace)
330 ;; The automatic C-d and DEL mapping functionality doesn't extend
331 ;; to special combinations like C-c C-<delete>, so we have to hook
332 ;; into the `normal-erase-is-backspace' system to bind it directly
334 (add-hook 'normal-erase-is-backspace-hook 'c-bind-special-erase-keys)
335 (c-bind-special-erase-keys))
337 (when (fboundp 'delete-forward-p)
338 ;; In XEmacs we fix the forward and backward deletion behavior by
339 ;; binding the keysyms for the [delete] and [backspace] keys
340 ;; directly, and use `delete-forward-p' to decide what [delete]
341 ;; should do. That's done in the XEmacs specific
342 ;; `c-electric-delete' and `c-hungry-delete' functions.
343 (define-key c-mode-base-map [delete] 'c-electric-delete)
344 (define-key c-mode-base-map [backspace] 'c-electric-backspace)
345 (define-key c-mode-base-map (kbd "C-c <delete>") 'c-hungry-delete)
346 (define-key c-mode-base-map (kbd "C-c C-<delete>") 'c-hungry-delete)
347 (define-key c-mode-base-map (kbd "C-c <backspace>")
348 'c-hungry-delete-backwards)
349 (define-key c-mode-base-map (kbd "C-c C-<backspace>")
350 'c-hungry-delete-backwards))
352 (define-key c-mode-base-map "#" 'c-electric-pound)
353 (define-key c-mode-base-map "{" 'c-electric-brace)
354 (define-key c-mode-base-map "}" 'c-electric-brace)
355 (define-key c-mode-base-map "/" 'c-electric-slash)
356 (define-key c-mode-base-map "*" 'c-electric-star)
357 (define-key c-mode-base-map ";" 'c-electric-semi&comma)
358 (define-key c-mode-base-map "," 'c-electric-semi&comma)
359 (define-key c-mode-base-map ":" 'c-electric-colon)
360 (define-key c-mode-base-map "(" 'c-electric-paren)
361 (define-key c-mode-base-map ")" 'c-electric-paren)
363 (define-key c-mode-base-map "\C-c\C-\\" 'c-backslash-region)
364 (define-key c-mode-base-map "\C-c\C-a" 'c-toggle-auto-newline)
365 (define-key c-mode-base-map "\C-c\C-b" 'c-submit-bug-report)
366 (define-key c-mode-base-map "\C-c\C-c" 'comment-region)
367 (define-key c-mode-base-map "\C-c\C-l" 'c-toggle-electric-state)
368 (define-key c-mode-base-map "\C-c\C-o" 'c-set-offset)
369 (define-key c-mode-base-map "\C-c\C-q" 'c-indent-defun)
370 (define-key c-mode-base-map "\C-c\C-s" 'c-show-syntactic-information)
371 ;; (define-key c-mode-base-map "\C-c\C-t" 'c-toggle-auto-hungry-state) Commented out by ACM, 2005-03-05.
372 (define-key c-mode-base-map "\C-c." 'c-set-style)
373 ;; conflicts with OOBR
374 ;;(define-key c-mode-base-map "\C-c\C-v" 'c-version)
375 ;; (define-key c-mode-base-map "\C-c\C-y" 'c-toggle-hungry-state) Commented out by ACM, 2005-11-22.
376 (define-key c-mode-base-map "\C-c\C-w" 'c-subword-mode)
379 ;; We don't require the outline package, but we configure it a bit anyway.
380 (cc-bytecomp-defvar outline-level)
382 (defun c-mode-menu (modestr)
383 "Return a menu spec suitable for `easy-menu-define' that is exactly
384 like the C mode menu except that the menu bar item name is MODESTR
387 This function is provided for compatibility only; derived modes should
388 preferably use the `c-mode-menu' language constant directly."
389 (cons modestr (c-lang-const c-mode-menu c)))
391 ;; Ugly hack to pull in the definition of `c-populate-syntax-table'
392 ;; from cc-langs to make it available at runtime. It's either this or
393 ;; moving the definition for it to cc-defs, but that would mean to
394 ;; break up the syntax table setup over two files.
395 (defalias 'c-populate-syntax-table
396 (cc-eval-when-compile
397 (let ((f (symbol-function 'c-populate-syntax-table)))
398 (if (byte-code-function-p f) f (byte-compile f)))))
400 ;; CAUTION: Try to avoid installing things on
401 ;; `before-change-functions'. The macro `combine-after-change-calls'
402 ;; is used and it doesn't work if there are things on that hook. That
403 ;; can cause font lock functions to run in inconvenient places during
404 ;; temporary changes in some font lock support modes, causing extra
405 ;; unnecessary work and font lock glitches due to interactions between
406 ;; various text properties.
408 ;; (2007-02-12): The macro `combine-after-change-calls' ISN'T used any
411 (defun c-unfind-enclosing-token (pos)
412 ;; If POS is wholly inside a token, remove that id from
413 ;; `c-found-types', should it be present. Return t if we were in an
416 (let ((tok-beg (progn (goto-char pos)
417 (and (c-beginning-of-current-token) (point))))
418 (tok-end (progn (goto-char pos)
419 (and (c-end-of-current-token) (point)))))
420 (when (and tok-beg tok-end)
421 (c-unfind-type (buffer-substring-no-properties tok-beg tok-end))
424 (defun c-unfind-coalesced-tokens (beg end)
425 ;; unless the non-empty region (beg end) is entirely WS and there's at
426 ;; least one character of WS just before or after this region, remove
427 ;; the tokens which touch the region from `c-found-types' should they
429 (or (c-partial-ws-p beg end)
433 (or (eq beg (point-min))
434 (c-skip-ws-backward (1- beg))
436 (= (c-backward-token-2) 1)
437 (c-unfind-type (buffer-substring-no-properties
440 (or (eq end (point-max))
441 (c-skip-ws-forward (1+ end))
443 (progn (forward-char) (c-end-of-current-token) nil)
444 (c-unfind-type (buffer-substring-no-properties
447 ;; c-maybe-stale-found-type records a place near the region being
448 ;; changed where an element of `found-types' might become stale. It
449 ;; is set in c-before-change and is either nil, or has the form:
451 ;; (c-decl-id-start "foo" 97 107 " (* ooka) " "o"), where
453 ;; o - `c-decl-id-start' is the c-type text property value at buffer
456 ;; o - 97 107 is the region potentially containing the stale type -
457 ;; this is delimited by a non-nil c-type text property at 96 and
458 ;; either another one or a ";", "{", or "}" at 107.
460 ;; o - " (* ooka) " is the (before change) buffer portion containing
461 ;; the suspect type (here "ooka").
463 ;; o - "o" is the buffer contents which is about to be deleted. This
464 ;; would be the empty string for an insertion.
465 (defvar c-maybe-stale-found-type nil)
466 (make-variable-buffer-local 'c-maybe-stale-found-type)
468 (defvar c-just-done-before-change nil)
469 (make-variable-buffer-local 'c-just-done-before-change)
470 ;; This variable is set to t by `c-before-change' and to nil by
471 ;; `c-after-change'. It is used to detect a spurious invocation of
472 ;; `before-change-functions' directly following on from a correct one. This
473 ;; happens in some Emacsen, for example when `basic-save-buffer' does (insert
474 ;; ?\n) when `require-final-newline' is non-nil.
476 (defun c-basic-common-init (mode default-style)
477 "Do the necessary initialization for the syntax handling routines
478 and the line breaking/filling code. Intended to be used by other
479 packages that embed CC Mode.
481 MODE is the CC Mode flavor to set up, e.g. `c-mode' or `java-mode'.
482 DEFAULT-STYLE tells which indentation style to install. It has the
483 same format as `c-default-style'.
485 Note that `c-init-language-vars' must be called before this function.
486 This function cannot do that since `c-init-language-vars' is a macro
487 that requires a literal mode spec at compile time."
489 (setq c-buffer-is-cc-mode mode)
491 ;; these variables should always be buffer local; they do not affect
492 ;; indentation style.
493 (make-local-variable 'comment-start)
494 (make-local-variable 'comment-end)
495 (make-local-variable 'comment-start-skip)
497 (make-local-variable 'paragraph-start)
498 (make-local-variable 'paragraph-separate)
499 (make-local-variable 'paragraph-ignore-fill-prefix)
500 (make-local-variable 'adaptive-fill-mode)
501 (make-local-variable 'adaptive-fill-regexp)
502 (make-local-variable 'fill-paragraph-handle-comment)
504 ;; now set their values
505 (set (make-local-variable 'parse-sexp-ignore-comments) t)
506 (set (make-local-variable 'indent-line-function) 'c-indent-line)
507 (set (make-local-variable 'indent-region-function) 'c-indent-region)
508 (set (make-local-variable 'normal-auto-fill-function) 'c-do-auto-fill)
509 (set (make-local-variable 'comment-multi-line) t)
510 (set (make-local-variable 'comment-line-break-function)
511 'c-indent-new-comment-line)
513 ;; Prevent time-wasting activity on C-y.
514 (when (boundp 'yank-handled-properties)
515 (make-local-variable 'yank-handled-properties)
516 (let ((yank-cat-handler (assq 'category yank-handled-properties)))
517 (when yank-cat-handler
518 (setq yank-handled-properties (remq yank-cat-handler
519 yank-handled-properties)))))
521 ;; For the benefit of adaptive file, which otherwise mis-fills.
522 (setq fill-paragraph-handle-comment nil)
524 ;; Install `c-fill-paragraph' on `fill-paragraph-function' so that a
525 ;; direct call to `fill-paragraph' behaves better. This still
526 ;; doesn't work with filladapt but it's better than nothing.
527 (set (make-local-variable 'fill-paragraph-function) 'c-fill-paragraph)
529 ;; Initialize the cache of brace pairs, and opening braces/brackets/parens.
532 (when (or c-recognize-<>-arglists
533 (c-major-mode-is 'awk-mode)
534 (c-major-mode-is '(java-mode c-mode c++-mode objc-mode)))
535 ;; We'll use the syntax-table text property to change the syntax
536 ;; of some chars for this language, so do the necessary setup for
539 ;; Note to other package developers: It's ok to turn this on in CC
540 ;; Mode buffers when CC Mode doesn't, but it's not ok to turn it
541 ;; off if CC Mode has turned it on.
544 (when (boundp 'parse-sexp-lookup-properties)
545 (set (make-local-variable 'parse-sexp-lookup-properties) t))
547 ;; Same as above for XEmacs.
548 (when (boundp 'lookup-syntax-properties)
549 (set (make-local-variable 'lookup-syntax-properties) t)))
551 ;; Use this in Emacs 21+ to avoid meddling with the rear-nonsticky
552 ;; property on each character.
553 (when (boundp 'text-property-default-nonsticky)
554 (make-local-variable 'text-property-default-nonsticky)
555 (mapc (lambda (tprop)
556 (unless (assq tprop text-property-default-nonsticky)
557 (setq text-property-default-nonsticky
558 (cons `(,tprop . t) text-property-default-nonsticky))))
559 '(syntax-table category c-type)))
561 ;; In Emacs 21 and later it's possible to turn off the ad-hoc
562 ;; heuristic that open parens in column 0 are defun starters. Since
563 ;; we have c-state-cache, that heuristic isn't useful and only causes
564 ;; trouble, so turn it off.
565 ;; 2006/12/17: This facility is somewhat confused, and doesn't really seem
566 ;; helpful. Comment it out for now.
567 ;; (when (memq 'col-0-paren c-emacs-features)
568 ;; (make-local-variable 'open-paren-in-column-0-is-defun-start)
569 ;; (setq open-paren-in-column-0-is-defun-start nil))
571 (c-clear-found-types)
573 ;; now set the mode style based on default-style
574 (let ((style (cc-choose-style-for-mode mode default-style)))
575 ;; Override style variables if `c-old-style-variable-behavior' is
576 ;; set. Also override if we are using global style variables,
577 ;; have already initialized a style once, and are switching to a
578 ;; different style. (It's doubtful whether this is desirable, but
579 ;; the whole situation with nonlocal style variables is a bit
580 ;; awkward. It's at least the most compatible way with the old
581 ;; style init procedure.)
582 (c-set-style style (not (or c-old-style-variable-behavior
583 (and (not c-style-variables-are-local-p)
585 (not (string-equal c-indentation-style
587 (c-setup-paragraph-variables)
589 ;; we have to do something special for c-offsets-alist so that the
590 ;; buffer local value has its own alist structure.
591 (setq c-offsets-alist (copy-alist c-offsets-alist))
593 ;; setup the comment indent variable in a Emacs version portable way
594 (set (make-local-variable 'comment-indent-function) 'c-comment-indent)
596 ;; In Emacs 24.4 onwards, prevent Emacs's built in electric indentation from
597 ;; messing up CC Mode's, and set `c-electric-flag' if `electric-indent-mode'
598 ;; has been called by the user.
599 (when (boundp 'electric-indent-inhibit) (setq electric-indent-inhibit t))
600 ;; CC-mode should obey Emacs's generic preferences, tho only do it if
601 ;; Emacs's generic preferences can be set per-buffer (Emacs>=24.4).
602 (when (fboundp 'electric-indent-local-mode)
603 (setq c-electric-flag electric-indent-mode))
605 ;; ;; Put submode indicators onto minor-mode-alist, but only once.
606 ;; (or (assq 'c-submode-indicators minor-mode-alist)
607 ;; (setq minor-mode-alist
608 ;; (cons '(c-submode-indicators c-submode-indicators)
609 ;; minor-mode-alist)))
612 ;; Install the functions that ensure that various internal caches
613 ;; don't become invalid due to buffer changes.
614 (when (featurep 'xemacs)
615 (make-local-hook 'before-change-functions)
616 (make-local-hook 'after-change-functions))
617 (add-hook 'before-change-functions 'c-before-change nil t)
618 (setq c-just-done-before-change nil)
619 (add-hook 'after-change-functions 'c-after-change nil t)
620 (when (boundp 'font-lock-extend-after-change-region-function)
621 (set (make-local-variable 'font-lock-extend-after-change-region-function)
622 'c-extend-after-change-region))) ; Currently (2009-05) used by all
623 ; languages with #define (C, C++,; ObjC), and by AWK.
625 (defun c-setup-doc-comment-style ()
626 "Initialize the variables that depend on the value of `c-doc-comment-style'."
627 (when (and (featurep 'font-lock)
628 (symbol-value 'font-lock-mode))
629 ;; Force font lock mode to reinitialize itself.
633 ;; Buffer local variables defining the region to be fontified by a font lock
634 ;; after-change function. They are initialized in c-before-change to
635 ;; before-change-functions' BEG and END. `c-new-END' is amended in
636 ;; c-after-change with after-change-functions' BEG, END, and OLD-LEN. These
637 ;; variables may be modified by any before/after-change function, in
638 ;; particular by functions in `c-get-state-before-change-functions' and
639 ;; `c-before-font-lock-functions'.
641 (make-variable-buffer-local 'c-new-BEG)
643 (make-variable-buffer-local 'c-new-END)
645 (defun c-common-init (&optional mode)
646 "Common initialization for all CC Mode modes.
647 In addition to the work done by `c-basic-common-init' and
648 `c-font-lock-init', this function sets up various other things as
649 customary in CC Mode modes but which aren't strictly necessary for CC
650 Mode to operate correctly.
652 MODE is the symbol for the mode to initialize, like `c-mode'. See
653 `c-basic-common-init' for details. It's only optional to be
654 compatible with old code; callers should always specify it."
657 ;; Called from an old third party package. The fallback is to
659 (c-init-language-vars-for 'c-mode))
661 (c-basic-common-init mode c-default-style)
663 ;; Only initialize font locking if we aren't called from an old package.
666 ;; Starting a mode is a sort of "change". So call the change functions...
669 (setq c-new-BEG (point-min))
670 (setq c-new-END (point-max))
672 (let (before-change-functions after-change-functions)
674 (funcall fn (point-min) (point-max)))
675 c-get-state-before-change-functions)
677 (if (not (eq fn 'c-restore-<>-properties))
678 (funcall fn (point-min) (point-max)
679 (- (point-max) (point-min)))))
680 c-before-font-lock-functions))))
682 (set (make-local-variable 'outline-regexp) "[^#\n\^M]")
683 (set (make-local-variable 'outline-level) 'c-outline-level)
684 (set (make-local-variable 'add-log-current-defun-function)
686 (or (c-cpp-define-name) (c-defun-name))))
687 (let ((rfn (assq mode c-require-final-newline)))
689 (if (boundp 'mode-require-final-newline)
691 (set (make-local-variable 'require-final-newline)
692 mode-require-final-newline))
693 (set (make-local-variable 'require-final-newline) (cdr rfn))))))
695 (defun c-count-cfss (lv-alist)
696 ;; LV-ALIST is an alist like `file-local-variables-alist'. Count how many
697 ;; elements with the key `c-file-style' there are in it.
698 (let ((elt-ptr lv-alist) elt (cownt 0))
700 (setq elt (car elt-ptr)
701 elt-ptr (cdr elt-ptr))
702 (when (eq (car elt) 'c-file-style)
703 (setq cownt (1+ cownt))))
706 (defun c-before-hack-hook ()
707 "Set the CC Mode style and \"offsets\" when in the buffer's local variables.
708 They are set only when, respectively, the pseudo variables
709 `c-file-style' and `c-file-offsets' are present in the list.
711 This function is called from the hook `before-hack-local-variables-hook'."
712 (when c-buffer-is-cc-mode
713 (let ((mode-cons (assq 'mode file-local-variables-alist))
714 (stile (cdr (assq 'c-file-style file-local-variables-alist)))
715 (offsets (cdr (assq 'c-file-offsets file-local-variables-alist))))
717 (hack-one-local-variable (car mode-cons) (cdr mode-cons))
718 (setq file-local-variables-alist
719 (delq mode-cons file-local-variables-alist)))
721 (or (stringp stile) (error "c-file-style is not a string"))
722 (if (boundp 'dir-local-variables-alist)
723 ;; Determine whether `c-file-style' was set in the file's local
724 ;; variables or in a .dir-locals.el (a directory setting).
725 (let ((cfs-in-file-and-dir-count
726 (c-count-cfss file-local-variables-alist))
727 (cfs-in-dir-count (c-count-cfss dir-local-variables-alist)))
729 (and (= cfs-in-file-and-dir-count cfs-in-dir-count)
731 (c-set-style stile)))
735 (let ((langelem (car langentry))
736 (offset (cdr langentry)))
737 (c-set-offset langelem offset)))
740 (defun c-remove-any-local-eval-or-mode-variables ()
741 ;; If the buffer specifies `mode' or `eval' in its File Local Variable list
742 ;; or on the first line, remove all occurrences. See
743 ;; `c-postprocess-file-styles' for justification. There is no need to save
744 ;; point here, or even bother too much about the buffer contents. However,
745 ;; DON'T mess up the kill-ring.
747 ;; Most of the code here is derived from Emacs 21.3's `hack-local-variables'
749 (goto-char (point-max))
750 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
751 (let (lv-point (prefix "") (suffix ""))
752 (when (let ((case-fold-search t))
753 (search-forward "Local Variables:" nil t))
754 (setq lv-point (point))
755 ;; The prefix is what comes before "local variables:" in its line.
756 ;; The suffix is what comes after "local variables:" in its line.
757 (skip-chars-forward " \t")
759 (setq suffix (buffer-substring (point)
760 (progn (end-of-line) (point)))))
761 (goto-char (match-beginning 0))
764 (buffer-substring (point)
765 (progn (beginning-of-line) (point)))))
767 (while (search-forward-regexp
769 (regexp-quote prefix)
770 "\\(mode\\|eval\\):.*"
771 (regexp-quote suffix)
775 (delete-region (point) (progn (forward-line) (point)))))
777 ;; Delete the first line, if we've got one, in case it contains a mode spec.
778 (unless (and lv-point
779 (progn (goto-char lv-point)
782 (goto-char (point-min))
784 (delete-region (point) (progn (forward-line) (point)))))))
786 (defun c-postprocess-file-styles ()
787 "Function that post processes relevant file local variables in CC Mode.
788 Currently, this function simply applies any style and offset settings
789 found in the file's Local Variable list. It first applies any style
790 setting found in `c-file-style', then it applies any offset settings
791 it finds in `c-file-offsets'.
793 Note that the style variables are always made local to the buffer."
795 ;; apply file styles and offsets
796 (when c-buffer-is-cc-mode
797 (if (or c-file-style c-file-offsets)
798 (c-make-styles-buffer-local t))
800 (or (stringp c-file-style)
801 (error "c-file-style is not a string"))
802 (c-set-style c-file-style))
807 (let ((langelem (car langentry))
808 (offset (cdr langentry)))
809 (c-set-offset langelem offset)))
811 ;; Problem: The file local variable block might have explicitly set a
812 ;; style variable. The `c-set-style' or `mapcar' call might have
813 ;; overwritten this. So we run `hack-local-variables' again to remedy
814 ;; this. There are no guarantees this will work properly, particularly as
815 ;; we have no control over what the other hook functions on
816 ;; `hack-local-variables-hook' would have done. We now (2006/2/1) remove
817 ;; any `eval' or `mode' expressions before we evaluate again (see below).
820 ;; Problem (bug reported by Gustav Broberg): if one of the variables is
821 ;; `mode', this will invoke c-mode (etc.) again, setting up the style etc.
822 ;; We prevent this by temporarily removing `mode' from the Local Variables
824 (if (or c-file-style c-file-offsets)
825 (let ((hack-local-variables-hook nil) (inhibit-read-only t))
826 (c-tentative-buffer-changes
827 (c-remove-any-local-eval-or-mode-variables)
828 (hack-local-variables))
831 (if (boundp 'before-hack-local-variables-hook)
832 (add-hook 'before-hack-local-variables-hook 'c-before-hack-hook)
833 (add-hook 'hack-local-variables-hook 'c-postprocess-file-styles))
835 (defmacro c-run-mode-hooks (&rest hooks)
836 ;; Emacs 21.1 has introduced a system with delayed mode hooks that
837 ;; requires the use of the new function `run-mode-hooks'.
838 (if (cc-bytecomp-fboundp 'run-mode-hooks)
839 `(run-mode-hooks ,@hooks)
840 `(progn ,@(mapcar (lambda (hook) `(run-hooks ,hook)) hooks))))
843 ;;; Change hooks, linking with Font Lock and electric-indent-mode.
845 ;; Buffer local variables recording Beginning/End-of-Macro position before a
846 ;; change, when a macro straddles, respectively, the BEG or END (or both) of
847 ;; the change region. Otherwise these have the values BEG/END.
849 (make-variable-buffer-local 'c-old-BOM)
851 (make-variable-buffer-local 'c-old-EOM)
853 (defun c-called-from-text-property-change-p ()
854 ;; Is the primitive which invoked `before-change-functions' or
855 ;; `after-change-functions' one which merely changes text properties? This
856 ;; function must be called directly from a member of one of the above hooks.
858 ;; In the following call, frame 0 is `backtrace-frame', frame 1 is
859 ;; `c-called-from-text-property-change-p', frame 2 is
860 ;; `c-before/after-change', frame 3 is the primitive invoking the change
862 (memq (cadr (backtrace-frame 3))
863 '(put-text-property remove-list-of-text-properties)))
865 (defun c-extend-region-for-CPP (beg end)
866 ;; Set c-old-BOM or c-old-EOM respectively to BEG, END, each extended to the
867 ;; beginning/end of any preprocessor construct they may be in.
869 ;; Point is undefined both before and after this function call; the buffer
870 ;; has already been widened, and match-data saved. The return value is
873 ;; This function is in the C/C++/ObjC values of
874 ;; `c-get-state-before-change-functions' and is called exclusively as a
875 ;; before change function.
877 (c-beginning-of-macro)
878 (setq c-old-BOM (point))
881 (when (c-beginning-of-macro)
883 (or (eobp) (forward-char))) ; Over the terminating NL which may be marked
884 ; with a c-cpp-delimiter category property
885 (setq c-old-EOM (point)))
887 (defun c-extend-font-lock-region-for-macros (begg endd &optional old-len)
888 ;; Extend the region (BEGG ENDD) to cover all (possibly changed)
889 ;; preprocessor macros; return the cons (new-BEG . new-END). OLD-LEN should
890 ;; be either the old length parameter when called from an
891 ;; after-change-function, or nil otherwise. This defun uses the variables
892 ;; c-old-BOM, c-new-BOM.
894 ;; Point is undefined on both entry and exit to this function. The buffer
895 ;; will have been widened on entry.
896 (let (limits new-beg new-end)
897 (goto-char c-old-BOM) ; already set to old start of macro or begg.
900 (if (setq limits (c-state-literal-at (point)))
901 (cdr limits) ; go forward out of any string or comment.
905 (if (setq limits (c-state-literal-at (point)))
906 (goto-char (car limits))) ; go backward out of any string or comment.
907 (if (c-beginning-of-macro)
909 (setq new-end (max endd
911 (+ (- c-old-EOM old-len) (- endd begg))
914 (cons new-beg new-end)))
916 (defun c-neutralize-CPP-line (beg end)
917 ;; BEG and END bound a region, typically a preprocessor line. Put a
918 ;; "punctuation" syntax-table property on syntactically obtrusive
919 ;; characters, ones which would interact syntactically with stuff outside
922 ;; These are unmatched string delimiters, or unmatched
923 ;; parens/brackets/braces. An unclosed comment is regarded as valid, NOT
929 (setq s (parse-partial-sexp beg end -1))
931 ((< (nth 0 s) 0) ; found an unmated ),},]
932 (c-put-char-property (1- (point)) 'syntax-table '(1))
934 ((nth 3 s) ; In a string
935 (c-put-char-property (nth 8 s) 'syntax-table '(1))
937 ((> (nth 0 s) 0) ; In a (,{,[
938 (c-put-char-property (nth 1 s) 'syntax-table '(1))
942 (defun c-neutralize-syntax-in-and-mark-CPP (begg endd old-len)
943 ;; (i) Extend the font lock region to cover all changed preprocessor
944 ;; regions; it does this by setting the variables `c-new-BEG' and
945 ;; `c-new-END' to the new boundaries.
947 ;; (ii) "Neutralize" every preprocessor line wholly or partially in the
948 ;; extended changed region. "Restore" lines which were CPP lines before the
949 ;; change and are no longer so; these can be located from the Buffer local
950 ;; variables `c-old-BOM' and `c-old-EOM'.
952 ;; (iii) Mark every CPP construct by placing a `category' property value
953 ;; `c-cpp-delimiter' at its start and end. The marked characters are the
954 ;; opening # and usually the terminating EOL, but sometimes the character
955 ;; before a comment/string delimiter.
957 ;; That is, set syntax-table properties on characters that would otherwise
958 ;; interact syntactically with those outside the CPP line(s).
960 ;; This function is called from an after-change function, BEGG ENDD and
961 ;; OLD-LEN being the standard parameters. It prepares the buffer for font
962 ;; locking, hence must get called before `font-lock-after-change-function'.
964 ;; Point is undefined both before and after this function call, the buffer
965 ;; has been widened, and match-data saved. The return value is ignored.
967 ;; This function is in the C/C++/ObjC value of `c-before-font-lock-functions'.
969 ;; Note: SPEED _MATTERS_ IN THIS FUNCTION!!!
971 ;; This function might make hidden buffer changes.
972 (c-save-buffer-state (new-bounds)
973 ;; First determine the region, (c-new-BEG c-new-END), which will get font
974 ;; locked. It might need "neutralizing". This region may not start
975 ;; inside a string, comment, or macro.
976 (setq new-bounds (c-extend-font-lock-region-for-macros
977 c-new-BEG c-new-END old-len))
978 (setq c-new-BEG (max (car new-bounds) (c-determine-limit 500 begg))
979 c-new-END (min (cdr new-bounds) (c-determine-+ve-limit 500 endd)))
980 ;; Clear all old relevant properties.
981 (c-clear-char-property-with-value c-new-BEG c-new-END 'syntax-table '(1))
983 ;; CPP "comment" markers:
984 (if (eval-when-compile (memq 'category-properties c-emacs-features));Emacs.
985 (c-clear-char-property-with-value
986 c-new-BEG c-new-END 'category 'c-cpp-delimiter))
987 ;; FIXME!!! What about the "<" and ">" category properties? 2009-11-16
989 ;; Add needed properties to each CPP construct in the region.
990 (goto-char c-new-BEG)
991 (skip-chars-backward " \t")
992 (let ((pps-position (point)) pps-state mbeg)
993 (while (and (< (point) c-new-END)
994 (search-forward-regexp c-anchored-cpp-prefix c-new-END t))
995 ;; If we've found a "#" inside a macro/string/comment, ignore it.
998 (goto-char (match-beginning 0))
999 (let ((here (point)))
1000 (and (save-match-data (c-beginning-of-macro))
1004 (parse-partial-sexp pps-position (point) nil nil pps-state)
1005 pps-position (point))
1006 (or (nth 3 pps-state) ; in a string?
1007 (nth 4 pps-state)))) ; in a comment?
1008 (goto-char (match-beginning 1))
1010 (if (> (c-syntactic-end-of-macro) mbeg)
1012 (c-neutralize-CPP-line mbeg (point)) ; "punctuation" properties
1013 (if (eval-when-compile
1014 (memq 'category-properties c-emacs-features)) ;Emacs.
1015 (c-set-cpp-delimiters mbeg (point)))) ; "comment" markers
1016 (forward-line)) ; no infinite loop with, e.g., "#//"
1019 (defun c-before-change (beg end)
1020 ;; Function to be put on `before-change-functions'. Primarily, this calls
1021 ;; the language dependent `c-get-state-before-change-functions'. It is
1022 ;; otherwise used only to remove stale entries from the `c-found-types'
1023 ;; cache, and to record entries which a `c-after-change' function might
1024 ;; confirm as stale.
1026 ;; Note that this function must be FAST rather than accurate. Note
1027 ;; also that it only has any effect when font locking is enabled.
1028 ;; We exploit this by checking for font-lock-*-face instead of doing
1029 ;; rigorous syntactic analysis.
1031 ;; If either change boundary is wholly inside an identifier, delete
1032 ;; it/them from the cache. Don't worry about being inside a string
1033 ;; or a comment - "wrongly" removing a symbol from `c-found-types'
1035 (unless (or (c-called-from-text-property-change-p)
1036 c-just-done-before-change) ; guard against a spurious second
1037 ; invocation of before-change-functions.
1038 (setq c-just-done-before-change t)
1039 ;; (c-new-BEG c-new-END) will be the region to fontify.
1040 (setq c-new-BEG beg c-new-END end)
1041 (setq c-maybe-stale-found-type nil)
1046 ;; Are we inserting/deleting stuff in the middle of an identifier?
1047 (c-unfind-enclosing-token beg)
1048 (c-unfind-enclosing-token end)
1049 ;; Are we coalescing two tokens together, e.g. "fo o" -> "foo"?
1051 (c-unfind-coalesced-tokens beg end))
1052 ;; Are we (potentially) disrupting the syntactic context which
1053 ;; makes a type a type? E.g. by inserting stuff after "foo" in
1054 ;; "foo bar;", or before "foo" in "typedef foo *bar;"?
1056 ;; We search for appropriate c-type properties "near" the change.
1057 ;; First, find an appropriate boundary for this property search.
1062 (or (and (eq (get-text-property end 'face)
1063 'font-lock-comment-face)
1064 (previous-single-property-change end 'face))
1066 (when (>= end1 beg) ; Don't hassle about changes entirely in comments.
1067 ;; Find a limit for the search for a `c-type' property
1069 (and (/= (skip-chars-backward "^;{}") 0)
1070 (> (point) (point-min))
1071 (memq (c-get-char-property (1- (point)) 'face)
1072 '(font-lock-comment-face font-lock-string-face))))
1073 (setq lim (max (point-min) (1- (point))))
1075 ;; Look for the latest `c-type' property before end1
1076 (when (and (> end1 (point-min))
1078 (if (get-text-property (1- end1) 'c-type)
1080 (previous-single-property-change end1 'c-type
1082 (setq type (get-text-property (max (1- type-pos) lim) 'c-type))
1084 (when (memq type '(c-decl-id-start c-decl-type-start))
1085 ;; Get the identifier, if any, that the property is on.
1086 (goto-char (1- type-pos))
1088 (when (looking-at "\\(\\sw\\|\\s_\\)")
1089 (c-beginning-of-current-token)
1090 (buffer-substring-no-properties (point) type-pos)))
1093 (skip-chars-forward "^;{}") ;FIXME!!! loop for comment, maybe
1096 (or (c-next-single-property-change end 'c-type nil lim)
1098 (setq c-maybe-stale-found-type
1099 (list type marked-id
1101 (buffer-substring-no-properties type-pos
1103 (buffer-substring-no-properties beg end)))))))
1105 (if c-get-state-before-change-functions
1107 (funcall fn beg end))
1108 c-get-state-before-change-functions))
1110 ;; The following must be done here rather than in `c-after-change' because
1111 ;; newly inserted parens would foul up the invalidation algorithm.
1112 (c-invalidate-state-cache beg)))
1114 (defvar c-in-after-change-fontification nil)
1115 (make-variable-buffer-local 'c-in-after-change-fontification)
1116 ;; A flag to prevent region expanding stuff being done twice for after-change
1119 (defun c-after-change (beg end old-len)
1120 ;; Function put on `after-change-functions' to adjust various caches
1121 ;; etc. Prefer speed to finesse here, since there will be an order
1122 ;; of magnitude more calls to this function than any of the
1123 ;; functions that use the caches.
1125 ;; Note that care must be taken so that this is called before any
1126 ;; font-lock callbacks since we might get calls to functions using
1127 ;; these caches from inside them, and we must thus be sure that this
1128 ;; has already been executed.
1130 ;; This calls the language variable c-before-font-lock-functions, if non nil.
1131 ;; This typically sets `syntax-table' properties.
1133 ;; (c-new-BEG c-new-END) will be the region to fontify. It may become
1134 ;; larger than (beg end).
1135 ;; (setq c-new-BEG beg c-new-END end)
1136 (setq c-new-END (- (+ c-new-END (- end beg)) old-len))
1138 (unless (c-called-from-text-property-change-p)
1139 (setq c-just-done-before-change nil)
1140 (c-save-buffer-state (case-fold-search)
1141 ;; When `combine-after-change-calls' is used we might get calls
1142 ;; with regions outside the current narrowing. This has been
1143 ;; observed in Emacs 20.7.
1145 (save-match-data ; c-recognize-<>-arglists changes match-data
1148 (when (> end (point-max))
1149 ;; Some emacsen might return positions past the end. This has been
1150 ;; observed in Emacs 20.7 when rereading a buffer changed on disk
1151 ;; (haven't been able to minimize it, but Emacs 21.3 appears to
1153 (setq end (point-max))
1157 ;; C-y is capable of spuriously converting category properties
1158 ;; c-</>-as-paren-syntax and c-cpp-delimiter into hard syntax-table
1159 ;; properties. Remove these when it happens.
1160 (when (eval-when-compile (memq 'category-properties c-emacs-features))
1161 (c-save-buffer-state ()
1162 (c-clear-char-property-with-value beg end 'syntax-table
1163 c-<-as-paren-syntax)
1164 (c-clear-char-property-with-value beg end 'syntax-table
1165 c->-as-paren-syntax)
1166 (c-clear-char-property-with-value beg end 'syntax-table nil)))
1168 (c-trim-found-types beg end old-len) ; maybe we don't need all of these.
1169 (c-invalidate-sws-region-after beg end)
1170 ;; (c-invalidate-state-cache beg) ; moved to `c-before-change'.
1171 (c-invalidate-find-decl-cache beg)
1173 (when c-recognize-<>-arglists
1174 (c-after-change-check-<>-operators beg end))
1176 (setq c-in-after-change-fontification t)
1179 (funcall fn beg end old-len))
1180 c-before-font-lock-functions)))))))
1182 (defun c-fl-decl-start (pos)
1183 ;; If the beginning of the line containing POS is in the middle of a "local"
1184 ;; declaration (i.e. one which does not start outside of braces enclosing
1185 ;; POS, such as a struct), return the beginning of that declaration.
1186 ;; Otherwise return nil. Note that declarations, in this sense, can be
1189 ;; This function is called indirectly from font locking stuff - either from
1190 ;; c-after-change (to prepare for after-change font-locking) or from font
1191 ;; lock context (etc.) fontification.
1192 (let ((lit-limits (c-literal-limits))
1195 (goto-char (c-point 'bol new-pos))
1196 (when lit-limits ; Comment or string.
1197 (goto-char (car lit-limits)))
1198 (setq bod-lim (c-determine-limit 500))
1201 ;; Go to a less nested declaration each time round this loop.
1203 (eq (car (c-beginning-of-decl-1 bod-lim)) 'same)
1205 (progn (setq bo-decl (point))
1206 ;; Are we looking at a keyword such as "template" or
1207 ;; "typedef" which can decorate a type, or the type itself?
1208 (when (or (looking-at c-prefix-spec-kwds-re)
1210 ;; We've found another candidate position.
1211 (setq new-pos (min new-pos bo-decl))
1212 (goto-char bo-decl))
1214 ;; Try and go out a level to search again.
1216 (c-backward-syntactic-ws bod-lim)
1217 (and (> (point) bod-lim)
1218 (or (memq (char-before) '(?\( ?\[))
1219 (and (eq (char-before) ?\<)
1220 (eq (c-get-char-property
1221 (1- (point)) 'syntax-table)
1222 c-<-as-paren-syntax)))))
1224 (backward-char)) ; back over (, [, <.
1225 (and (/= new-pos pos) new-pos)))
1227 (defun c-change-expand-fl-region (beg end old-len)
1228 ;; Expand the region (c-new-BEG c-new-END) to an after-change font-lock
1229 ;; region. This will usually be the smallest sequence of whole lines
1230 ;; containing `c-new-BEG' and `c-new-END', but if `c-new-BEG' is in a
1231 ;; "local" declaration (see `c-fl-decl-start') the beginning of this is used
1232 ;; as the lower bound.
1234 ;; This is called from an after-change-function, but the parameters BEG END
1235 ;; and OLD-LEN are not used.
1238 (or (c-fl-decl-start c-new-BEG) (c-point 'bol c-new-BEG))
1239 c-new-END (c-point 'bonl c-new-END))))
1241 (defun c-context-expand-fl-region (beg end)
1242 ;; Return a cons (NEW-BEG . NEW-END), where NEW-BEG is the beginning of a
1243 ;; "local" declaration containing BEG (see `c-fl-decl-start') or BOL BEG is
1244 ;; in. NEW-END is beginning of the line after the one END is in.
1245 (cons (or (c-fl-decl-start beg) (c-point 'bol beg))
1246 (c-point 'bonl end)))
1248 (defun c-before-context-fl-expand-region (beg end)
1249 ;; Expand the region (BEG END) as specified by
1250 ;; `c-before-context-fontification-functions'. Return a cons of the bounds
1251 ;; of the new region.
1255 (let ((new-beg beg) (new-end end)
1256 (new-region (cons beg end)))
1258 (setq new-region (funcall fn new-beg new-end))
1259 (setq new-beg (car new-region) new-end (cdr new-region)))
1260 c-before-context-fontification-functions)
1263 (defun c-font-lock-fontify-region (beg end &optional verbose)
1264 ;; Effectively advice around `font-lock-fontify-region' which extends the
1265 ;; region (BEG END), for example, to avoid context fontification chopping
1266 ;; off the start of the context. Do not extend the region if it's already
1267 ;; been done (i.e. from an after-change fontification. An example (C++)
1268 ;; where the chopping off used to happen is this:
1270 ;; template <typename T>
1273 ;; void myfunc(T* p) {}
1275 ;; Type a space in the first blank line, and the fontification of the next
1276 ;; line was fouled up by context fontification.
1277 (let (new-beg new-end new-region case-fold-search)
1278 (if (and c-in-after-change-fontification
1279 (< beg c-new-END) (> end c-new-BEG))
1280 ;; Region and the latest after-change fontification region overlap.
1281 ;; Determine the upper and lower bounds of our adjusted region
1284 (if (<= beg c-new-BEG)
1285 (setq c-in-after-change-fontification nil))
1287 (if (and (>= beg (c-point 'bol c-new-BEG))
1289 ;; Either jit-lock has accepted `c-new-BEG', or has
1290 ;; (probably) extended the change region spuriously to
1291 ;; BOL, which position likely has a syntactically
1292 ;; different position. To ensure correct fontification,
1293 ;; we start at `c-new-BEG', assuming any characters to the
1294 ;; left of `c-new-BEG' on the line do not require
1297 (setq new-region (c-before-context-fl-expand-region beg end)
1298 new-end (cdr new-region))
1301 (if (and (>= end (c-point 'bol c-new-END))
1305 (cdr (c-before-context-fl-expand-region beg end))))))
1306 ;; Context (etc.) fontification.
1307 (setq new-region (c-before-context-fl-expand-region beg end)
1308 new-beg (car new-region) new-end (cdr new-region)))
1309 (funcall (default-value 'font-lock-fontify-region-function)
1310 new-beg new-end verbose)))
1312 (defun c-after-font-lock-init ()
1313 ;; Put on `font-lock-mode-hook'. This function ensures our after-change
1314 ;; function will get executed before the font-lock one.
1315 (when (memq #'c-after-change after-change-functions)
1316 (remove-hook 'after-change-functions #'c-after-change t)
1317 (add-hook 'after-change-functions #'c-after-change nil t)))
1319 (defun c-font-lock-init ()
1320 "Set up the font-lock variables for using the font-lock support in CC Mode.
1321 This does not load the font-lock package. Use after
1322 `c-basic-common-init' and after cc-fonts has been loaded.
1323 This function is called from `c-common-init', once per mode initialization."
1325 (set (make-local-variable 'font-lock-defaults)
1326 `(,(if (c-major-mode-is 'awk-mode)
1327 ;; awk-mode currently has only one font lock level.
1328 'awk-font-lock-keywords
1329 (mapcar 'c-mode-symbol
1330 '("font-lock-keywords" "font-lock-keywords-1"
1331 "font-lock-keywords-2" "font-lock-keywords-3")))
1333 ,c-identifier-syntax-modifications
1334 c-beginning-of-syntax
1335 (font-lock-mark-block-function
1336 . c-mark-function)))
1338 ;; Prevent `font-lock-default-fontify-region' extending the region it will
1339 ;; fontify to whole lines by removing `font-lock-extend-region-wholelines'
1340 ;; from `font-lock-extend-region-functions'. (Emacs only). This fixes
1341 ;; Emacs bug #19669.
1342 (when (boundp 'font-lock-extend-region-functions)
1343 (setq font-lock-extend-region-functions
1344 (delq 'font-lock-extend-region-wholelines
1345 font-lock-extend-region-functions)))
1347 (make-local-variable 'font-lock-fontify-region-function)
1348 (setq font-lock-fontify-region-function 'c-font-lock-fontify-region)
1350 (if (featurep 'xemacs)
1351 (make-local-hook 'font-lock-mode-hook))
1352 (add-hook 'font-lock-mode-hook 'c-after-font-lock-init nil t))
1354 ;; Emacs 22 and later.
1355 (defun c-extend-after-change-region (beg end _old-len)
1356 "Extend the region to be fontified, if necessary."
1357 ;; Note: the parameter OLD-LEN is ignored here. This somewhat indirect
1358 ;; implementation exists because it is minimally different from the
1359 ;; stand-alone CC Mode which, lacking
1360 ;; font-lock-extend-after-change-region-function, is forced to use advice
1363 ;; Of the seven CC Mode languages, currently (2009-05) only C, C++, Objc
1364 ;; (the languages with #define) and AWK Mode make non-null use of this
1366 (when (eq font-lock-support-mode 'jit-lock-mode)
1369 (c-save-buffer-state () ; Protect the undo-list from put-text-property.
1370 (if (< c-new-BEG beg)
1371 (put-text-property c-new-BEG beg 'fontified nil))
1372 (if (> c-new-END end)
1373 (put-text-property end c-new-END 'fontified nil)))))
1374 (cons c-new-BEG c-new-END))
1376 ;; Emacs < 22 and XEmacs
1377 (defmacro c-advise-fl-for-region (function)
1378 `(defadvice ,function (before get-awk-region activate)
1379 ;; Make sure that any string/regexp is completely font-locked.
1380 (when c-buffer-is-cc-mode
1382 (ad-set-arg 1 c-new-END) ; end
1383 (ad-set-arg 0 c-new-BEG))))) ; beg
1385 (unless (boundp 'font-lock-extend-after-change-region-function)
1386 (c-advise-fl-for-region font-lock-after-change-function)
1387 (c-advise-fl-for-region jit-lock-after-change)
1388 (c-advise-fl-for-region lazy-lock-defer-rest-after-change)
1389 (c-advise-fl-for-region lazy-lock-defer-line-after-change))
1391 ;; Connect up to `electric-indent-mode' (Emacs 24.4 and later).
1392 (defun c-electric-indent-mode-hook ()
1393 ;; Emacs has en/disabled `electric-indent-mode'. Propagate this through to
1394 ;; each CC Mode buffer.
1396 (with-current-buffer buf
1397 (when c-buffer-is-cc-mode
1398 ;; Don't use `c-toggle-electric-state' here due to recursion.
1399 (setq c-electric-flag electric-indent-mode)
1400 (c-update-modeline))))
1403 (defun c-electric-indent-local-mode-hook ()
1404 ;; Emacs has en/disabled `electric-indent-local-mode' for this buffer.
1405 ;; Propagate this through to this buffer's value of `c-electric-flag'
1406 (when c-buffer-is-cc-mode
1407 (setq c-electric-flag electric-indent-mode)
1408 (c-update-modeline)))
1413 (defvar c-mode-syntax-table
1414 (funcall (c-lang-const c-make-mode-syntax-table c))
1415 "Syntax table used in c-mode buffers.")
1417 (c-define-abbrev-table 'c-mode-abbrev-table
1418 '(("else" "else" c-electric-continued-statement 0)
1419 ("while" "while" c-electric-continued-statement 0))
1420 "Abbreviation table used in c-mode buffers.")
1423 (let ((map (c-make-inherited-keymap)))
1424 ;; Add bindings which are only useful for C.
1425 (define-key map "\C-c\C-e" 'c-macro-expand)
1427 "Keymap used in c-mode buffers.")
1430 (easy-menu-define c-c-menu c-mode-map "C Mode Commands"
1431 (cons "C" (c-lang-const c-mode-menu c)))
1433 ;; In XEmacs >= 21.5 modes should add their own entries to
1434 ;; `auto-mode-alist'. The comment form of autoload is used to avoid
1435 ;; doing this on load. That since `add-to-list' prepends the value
1436 ;; which could cause it to clobber user settings. Later emacsen have
1437 ;; an append option, but it's not safe to use.
1439 ;; The extension ".C" is associated with C++ while the lowercase
1440 ;; variant goes with C. On case insensitive file systems, this means
1441 ;; that ".c" files also might open C++ mode if the C++ entry comes
1442 ;; first on `auto-mode-alist'. Thus we try to ensure that ".C" comes
1443 ;; after ".c", and since `add-to-list' adds the entry first we have to
1444 ;; add the ".C" entry first.
1445 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(cc\\|hh\\)\\'" . c++-mode))
1446 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.[ch]\\(pp\\|xx\\|\\+\\+\\)\\'" . c++-mode))
1447 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(CC?\\|HH?\\)\\'" . c++-mode))
1449 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.[ch]\\'" . c-mode))
1451 ;; NB: The following two associate yacc and lex files to C Mode, which
1452 ;; is not really suitable for those formats. Anyway, afaik there's
1453 ;; currently no better mode for them, and besides this is legacy.
1454 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.y\\(acc\\)?\\'" . c-mode))
1455 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.lex\\'" . c-mode))
1457 ;; Preprocessed files generated by C and C++ compilers.
1458 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.i\\'" . c-mode))
1459 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.ii\\'" . c++-mode))
1461 (unless (fboundp 'prog-mode) (defalias 'prog-mode 'fundamental-mode))
1464 (define-derived-mode c-mode prog-mode "C"
1465 "Major mode for editing C code.
1467 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1468 c-mode buffer. This automatically sets up a mail buffer with version
1469 information already added. You just need to add a description of the
1470 problem, including a reproducible test case, and send the message.
1472 To see what version of CC Mode you are running, enter `\\[c-version]'.
1474 The hook `c-mode-common-hook' is run with no args at mode
1475 initialization, then `c-mode-hook'.
1479 (c-initialize-cc-mode t)
1480 (set-syntax-table c-mode-syntax-table)
1481 (setq local-abbrev-table c-mode-abbrev-table
1483 (use-local-map c-mode-map)
1484 (c-init-language-vars-for 'c-mode)
1485 (c-make-macro-with-semi-re) ; matches macro names whose expansion ends with ;
1486 (c-common-init 'c-mode)
1487 (easy-menu-add c-c-menu)
1488 (cc-imenu-init cc-imenu-c-generic-expression)
1489 (c-run-mode-hooks 'c-mode-common-hook 'c-mode-hook)
1490 (c-update-modeline))
1495 (defvar c++-mode-syntax-table
1496 (funcall (c-lang-const c-make-mode-syntax-table c++))
1497 "Syntax table used in c++-mode buffers.")
1499 (c-define-abbrev-table 'c++-mode-abbrev-table
1500 '(("else" "else" c-electric-continued-statement 0)
1501 ("while" "while" c-electric-continued-statement 0)
1502 ("catch" "catch" c-electric-continued-statement 0))
1503 "Abbreviation table used in c++-mode buffers.")
1505 (defvar c++-mode-map
1506 (let ((map (c-make-inherited-keymap)))
1507 ;; Add bindings which are only useful for C++.
1508 (define-key map "\C-c\C-e" 'c-macro-expand)
1509 (define-key map "\C-c:" 'c-scope-operator)
1510 (define-key map "<" 'c-electric-lt-gt)
1511 (define-key map ">" 'c-electric-lt-gt)
1513 "Keymap used in c++-mode buffers.")
1515 (easy-menu-define c-c++-menu c++-mode-map "C++ Mode Commands"
1516 (cons "C++" (c-lang-const c-mode-menu c++)))
1519 (define-derived-mode c++-mode prog-mode "C++"
1520 "Major mode for editing C++ code.
1521 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1522 c++-mode buffer. This automatically sets up a mail buffer with
1523 version information already added. You just need to add a description
1524 of the problem, including a reproducible test case, and send the
1527 To see what version of CC Mode you are running, enter `\\[c-version]'.
1529 The hook `c-mode-common-hook' is run with no args at mode
1530 initialization, then `c++-mode-hook'.
1534 (c-initialize-cc-mode t)
1535 (set-syntax-table c++-mode-syntax-table)
1536 (setq local-abbrev-table c++-mode-abbrev-table
1538 (use-local-map c++-mode-map)
1539 (c-init-language-vars-for 'c++-mode)
1540 (c-make-macro-with-semi-re) ; matches macro names whose expansion ends with ;
1541 (c-common-init 'c++-mode)
1542 (easy-menu-add c-c++-menu)
1543 (cc-imenu-init cc-imenu-c++-generic-expression)
1544 (c-run-mode-hooks 'c-mode-common-hook 'c++-mode-hook)
1545 (c-update-modeline))
1548 ;; Support for Objective-C
1550 (defvar objc-mode-syntax-table
1551 (funcall (c-lang-const c-make-mode-syntax-table objc))
1552 "Syntax table used in objc-mode buffers.")
1554 (c-define-abbrev-table 'objc-mode-abbrev-table
1555 '(("else" "else" c-electric-continued-statement 0)
1556 ("while" "while" c-electric-continued-statement 0))
1557 "Abbreviation table used in objc-mode buffers.")
1559 (defvar objc-mode-map
1560 (let ((map (c-make-inherited-keymap)))
1561 ;; Add bindings which are only useful for Objective-C.
1562 (define-key map "\C-c\C-e" 'c-macro-expand)
1564 "Keymap used in objc-mode buffers.")
1566 (easy-menu-define c-objc-menu objc-mode-map "ObjC Mode Commands"
1567 (cons "ObjC" (c-lang-const c-mode-menu objc)))
1569 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.m\\'" . objc-mode))
1572 (define-derived-mode objc-mode prog-mode "ObjC"
1573 "Major mode for editing Objective C code.
1574 To submit a problem report, enter `\\[c-submit-bug-report]' from an
1575 objc-mode buffer. This automatically sets up a mail buffer with
1576 version information already added. You just need to add a description
1577 of the problem, including a reproducible test case, and send the
1580 To see what version of CC Mode you are running, enter `\\[c-version]'.
1582 The hook `c-mode-common-hook' is run with no args at mode
1583 initialization, then `objc-mode-hook'.
1587 (c-initialize-cc-mode t)
1588 (set-syntax-table objc-mode-syntax-table)
1589 (setq local-abbrev-table objc-mode-abbrev-table
1591 (use-local-map objc-mode-map)
1592 (c-init-language-vars-for 'objc-mode)
1593 (c-make-macro-with-semi-re) ; matches macro names whose expansion ends with ;
1594 (c-common-init 'objc-mode)
1595 (easy-menu-add c-objc-menu)
1596 (cc-imenu-init nil 'cc-imenu-objc-function)
1597 (c-run-mode-hooks 'c-mode-common-hook 'objc-mode-hook)
1598 (c-update-modeline))
1603 (defvar java-mode-syntax-table
1604 (funcall (c-lang-const c-make-mode-syntax-table java))
1605 "Syntax table used in java-mode buffers.")
1607 (c-define-abbrev-table 'java-mode-abbrev-table
1608 '(("else" "else" c-electric-continued-statement 0)
1609 ("while" "while" c-electric-continued-statement 0)
1610 ("catch" "catch" c-electric-continued-statement 0)
1611 ("finally" "finally" c-electric-continued-statement 0))
1612 "Abbreviation table used in java-mode buffers.")
1614 (defvar java-mode-map
1615 (let ((map (c-make-inherited-keymap)))
1616 ;; Add bindings which are only useful for Java.
1618 "Keymap used in java-mode buffers.")
1620 ;; Regexp trying to describe the beginning of a Java top-level
1621 ;; definition. This is not used by CC Mode, nor is it maintained
1622 ;; since it's practically impossible to write a regexp that reliably
1623 ;; matches such a construct. Other tools are necessary.
1624 (defconst c-Java-defun-prompt-regexp
1625 "^[ \t]*\\(\\(\\(public\\|protected\\|private\\|const\\|abstract\\|synchronized\\|final\\|static\\|threadsafe\\|transient\\|native\\|volatile\\)\\s-+\\)*\\(\\(\\([[a-zA-Z][][_$.a-zA-Z0-9]*[][_$.a-zA-Z0-9]+\\|[[a-zA-Z]\\)\\s-*\\)\\s-+\\)\\)?\\(\\([[a-zA-Z][][_$.a-zA-Z0-9]*\\s-+\\)\\s-*\\)?\\([_a-zA-Z][^][ \t:;.,{}()
\7f=]*\\|\\([_$a-zA-Z][_$.a-zA-Z0-9]*\\)\\)\\s-*\\(([^);{}]*)\\)?\\([] \t]*\\)\\(\\s-*\\<throws\\>\\s-*\\(\\([_$a-zA-Z][_$.a-zA-Z0-9]*\\)[, \t\n\r\f\v]*\\)+\\)?\\s-*")
1627 (easy-menu-define c-java-menu java-mode-map "Java Mode Commands"
1628 (cons "Java" (c-lang-const c-mode-menu java)))
1630 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.java\\'" . java-mode))
1633 (define-derived-mode java-mode prog-mode "Java"
1634 "Major mode for editing Java code.
1635 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1636 java-mode buffer. This automatically sets up a mail buffer with
1637 version information already added. You just need to add a description
1638 of the problem, including a reproducible test case, and send the
1641 To see what version of CC Mode you are running, enter `\\[c-version]'.
1643 The hook `c-mode-common-hook' is run with no args at mode
1644 initialization, then `java-mode-hook'.
1648 (c-initialize-cc-mode t)
1649 (set-syntax-table java-mode-syntax-table)
1650 (setq local-abbrev-table java-mode-abbrev-table
1652 (use-local-map java-mode-map)
1653 (c-init-language-vars-for 'java-mode)
1654 (c-common-init 'java-mode)
1655 (easy-menu-add c-java-menu)
1656 (cc-imenu-init cc-imenu-java-generic-expression)
1657 (c-run-mode-hooks 'c-mode-common-hook 'java-mode-hook)
1658 (c-update-modeline))
1661 ;; Support for CORBA's IDL language
1663 (defvar idl-mode-syntax-table
1664 (funcall (c-lang-const c-make-mode-syntax-table idl))
1665 "Syntax table used in idl-mode buffers.")
1667 (c-define-abbrev-table 'idl-mode-abbrev-table nil
1668 "Abbreviation table used in idl-mode buffers.")
1670 (defvar idl-mode-map
1671 (let ((map (c-make-inherited-keymap)))
1672 ;; Add bindings which are only useful for IDL.
1674 "Keymap used in idl-mode buffers.")
1676 (easy-menu-define c-idl-menu idl-mode-map "IDL Mode Commands"
1677 (cons "IDL" (c-lang-const c-mode-menu idl)))
1679 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.idl\\'" . idl-mode))
1682 (define-derived-mode idl-mode prog-mode "IDL"
1683 "Major mode for editing CORBA's IDL, PSDL and CIDL code.
1684 To submit a problem report, enter `\\[c-submit-bug-report]' from an
1685 idl-mode buffer. This automatically sets up a mail buffer with
1686 version information already added. You just need to add a description
1687 of the problem, including a reproducible test case, and send the
1690 To see what version of CC Mode you are running, enter `\\[c-version]'.
1692 The hook `c-mode-common-hook' is run with no args at mode
1693 initialization, then `idl-mode-hook'.
1697 (c-initialize-cc-mode t)
1698 (set-syntax-table idl-mode-syntax-table)
1699 (setq local-abbrev-table idl-mode-abbrev-table)
1700 (use-local-map idl-mode-map)
1701 (c-init-language-vars-for 'idl-mode)
1702 (c-common-init 'idl-mode)
1703 (easy-menu-add c-idl-menu)
1704 ;;(cc-imenu-init cc-imenu-idl-generic-expression) ;TODO
1705 (c-run-mode-hooks 'c-mode-common-hook 'idl-mode-hook)
1706 (c-update-modeline))
1711 (defvar pike-mode-syntax-table
1712 (funcall (c-lang-const c-make-mode-syntax-table pike))
1713 "Syntax table used in pike-mode buffers.")
1715 (c-define-abbrev-table 'pike-mode-abbrev-table
1716 '(("else" "else" c-electric-continued-statement 0)
1717 ("while" "while" c-electric-continued-statement 0))
1718 "Abbreviation table used in pike-mode buffers.")
1720 (defvar pike-mode-map
1721 (let ((map (c-make-inherited-keymap)))
1722 ;; Additional bindings.
1723 (define-key map "\C-c\C-e" 'c-macro-expand)
1725 "Keymap used in pike-mode buffers.")
1727 (easy-menu-define c-pike-menu pike-mode-map "Pike Mode Commands"
1728 (cons "Pike" (c-lang-const c-mode-menu pike)))
1730 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(u?lpc\\|pike\\|pmod\\(\\.in\\)?\\)\\'" . pike-mode))
1731 ;;;###autoload (add-to-list 'interpreter-mode-alist '("pike" . pike-mode))
1734 (define-derived-mode pike-mode prog-mode "Pike"
1735 "Major mode for editing Pike code.
1736 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1737 pike-mode buffer. This automatically sets up a mail buffer with
1738 version information already added. You just need to add a description
1739 of the problem, including a reproducible test case, and send the
1742 To see what version of CC Mode you are running, enter `\\[c-version]'.
1744 The hook `c-mode-common-hook' is run with no args at mode
1745 initialization, then `pike-mode-hook'.
1749 (c-initialize-cc-mode t)
1750 (set-syntax-table pike-mode-syntax-table)
1751 (setq local-abbrev-table pike-mode-abbrev-table
1753 (use-local-map pike-mode-map)
1754 (c-init-language-vars-for 'pike-mode)
1755 (c-common-init 'pike-mode)
1756 (easy-menu-add c-pike-menu)
1757 ;;(cc-imenu-init cc-imenu-pike-generic-expression) ;TODO
1758 (c-run-mode-hooks 'c-mode-common-hook 'pike-mode-hook)
1759 (c-update-modeline))
1764 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.awk\\'" . awk-mode))
1765 ;;;###autoload (add-to-list 'interpreter-mode-alist '("awk" . awk-mode))
1766 ;;;###autoload (add-to-list 'interpreter-mode-alist '("mawk" . awk-mode))
1767 ;;;###autoload (add-to-list 'interpreter-mode-alist '("nawk" . awk-mode))
1768 ;;;###autoload (add-to-list 'interpreter-mode-alist '("gawk" . awk-mode))
1770 (c-define-abbrev-table 'awk-mode-abbrev-table
1771 '(("else" "else" c-electric-continued-statement 0)
1772 ("while" "while" c-electric-continued-statement 0))
1773 "Abbreviation table used in awk-mode buffers.")
1775 (defvar awk-mode-map
1776 (let ((map (c-make-inherited-keymap)))
1777 ;; Add bindings which are only useful for awk.
1778 (define-key map "#" 'self-insert-command)
1779 (define-key map "/" 'self-insert-command)
1780 (define-key map "*" 'self-insert-command)
1781 (define-key map "\C-c\C-n" 'undefined) ; #if doesn't exist in awk.
1782 (define-key map "\C-c\C-p" 'undefined)
1783 (define-key map "\C-c\C-u" 'undefined)
1784 (define-key map "\M-a" 'c-beginning-of-statement) ; 2003/10/7
1785 (define-key map "\M-e" 'c-end-of-statement) ; 2003/10/7
1786 (define-key map "\C-\M-a" 'c-awk-beginning-of-defun)
1787 (define-key map "\C-\M-e" 'c-awk-end-of-defun)
1789 "Keymap used in awk-mode buffers.")
1791 (easy-menu-define c-awk-menu awk-mode-map "AWK Mode Commands"
1792 (cons "AWK" (c-lang-const c-mode-menu awk)))
1794 ;; (require 'cc-awk) brings these in.
1795 (defvar awk-mode-syntax-table)
1796 (declare-function c-awk-unstick-NL-prop "cc-awk" ())
1799 (define-derived-mode awk-mode prog-mode "AWK"
1800 "Major mode for editing AWK code.
1801 To submit a problem report, enter `\\[c-submit-bug-report]' from an
1802 awk-mode buffer. This automatically sets up a mail buffer with version
1803 information already added. You just need to add a description of the
1804 problem, including a reproducible test case, and send the message.
1806 To see what version of CC Mode you are running, enter `\\[c-version]'.
1808 The hook `c-mode-common-hook' is run with no args at mode
1809 initialization, then `awk-mode-hook'.
1813 ;; We need the next line to stop the macro defining
1814 ;; `awk-mode-syntax-table'. This would mask the real table which is
1815 ;; declared in cc-awk.el and hasn't yet been loaded.
1817 (require 'cc-awk) ; Added 2003/6/10.
1818 (c-initialize-cc-mode t)
1819 (set-syntax-table awk-mode-syntax-table)
1820 (setq local-abbrev-table awk-mode-abbrev-table
1822 (use-local-map awk-mode-map)
1823 (c-init-language-vars-for 'awk-mode)
1824 (c-common-init 'awk-mode)
1825 (c-awk-unstick-NL-prop)
1827 (c-run-mode-hooks 'c-mode-common-hook 'awk-mode-hook)
1828 (c-update-modeline))
1833 (defconst c-mode-help-address
1834 "submit@debbugs.gnu.org"
1835 "Address(es) for CC Mode bug reports.")
1838 "Echo the current version of CC Mode in the minibuffer."
1840 (message "Using CC Mode version %s" c-version)
1841 (c-keep-region-active))
1843 (define-obsolete-variable-alias 'c-prepare-bug-report-hooks
1844 'c-prepare-bug-report-hook "24.3")
1845 (defvar c-prepare-bug-report-hook nil)
1847 ;; Dynamic variables used by reporter.
1848 (defvar reporter-prompt-for-summary-p)
1849 (defvar reporter-dont-compact-list)
1851 ;; This could be "emacs,cc-mode" in the version included in Emacs.
1852 (defconst c-mode-bug-package "cc-mode"
1853 "The package to use in the bug submission.")
1855 ;; reporter-submit-bug-report requires sendmail.
1856 (declare-function mail-position-on-field "sendmail" (field &optional soft))
1858 (defun c-submit-bug-report ()
1859 "Submit via mail a bug report on CC Mode."
1863 (let ((reporter-prompt-for-summary-p t)
1864 (reporter-dont-compact-list '(c-offsets-alist))
1865 (style c-indentation-style)
1866 (c-features c-emacs-features))
1868 (if (y-or-n-p "Do you want to submit a report on CC Mode? ")
1870 (reporter-submit-bug-report
1872 (concat "CC Mode " c-version " (" mode-name ")")
1875 '(c-buffer-is-cc-mode
1877 c-syntactic-indentation
1878 c-syntactic-indentation-in-macros
1880 c-auto-align-backslashes
1881 c-backspace-function
1883 c-electric-pound-behavior
1885 c-enable-xemacs-performance-kludge-p
1886 c-old-style-variable-behavior
1890 parse-sexp-ignore-comments
1891 parse-sexp-lookup-properties
1892 lookup-syntax-properties
1893 ;; A brain-damaged XEmacs only variable that, if
1894 ;; set to nil can cause all kinds of chaos.
1895 signal-error-on-buffer-boundary
1896 ;; Variables that affect line breaking and comments.
1906 adaptive-fill-regexp)
1908 (mapc (lambda (var) (unless (boundp var)
1909 (setq vars (delq var vars))))
1910 '(signal-error-on-buffer-boundary
1915 font-lock-maximum-decoration
1916 parse-sexp-lookup-properties
1917 lookup-syntax-properties))
1920 (run-hooks 'c-prepare-bug-report-hook)
1922 (or (mail-position-on-field "X-Debbugs-Package")
1923 (insert c-mode-bug-package)))
1924 (insert (format "Buffer Style: %s\nc-emacs-features: %s\n"
1925 style c-features)))))))
1928 (cc-provide 'cc-mode)
1931 ;; indent-tabs-mode: t
1934 ;;; cc-mode.el ends here