Merge from origin/emacs-24
[emacs.git] / lisp / progmodes / cc-mode.el
blob01e93b3f202087454ac498dc7b5c7b959889d482
1 ;;; cc-mode.el --- major mode for editing C and similar languages
3 ;; Copyright (C) 1985, 1987, 1992-2015 Free Software Foundation, Inc.
5 ;; Authors: 2003- Alan Mackenzie
6 ;; 1998- Martin Stjernholm
7 ;; 1992-1999 Barry A. Warsaw
8 ;; 1987 Dave Detlefs
9 ;; 1987 Stewart Clamen
10 ;; 1985 Richard M. Stallman
11 ;; Maintainer: bug-cc-mode@gnu.org
12 ;; Created: a long, long, time ago. adapted from the original c-mode.el
13 ;; Keywords: c languages
15 ;; This file is part of GNU Emacs.
17 ;; GNU Emacs is free software: you can redistribute it and/or modify
18 ;; it under the terms of the GNU General Public License as published by
19 ;; the Free Software Foundation, either version 3 of the License, or
20 ;; (at your option) any later version.
22 ;; GNU Emacs is distributed in the hope that it will be useful,
23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 ;; GNU General Public License for more details.
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
30 ;;; Commentary:
32 ;; NOTE: Read the commentary below for the right way to submit bug reports!
33 ;; NOTE: See the accompanying texinfo manual for details on using this mode!
34 ;; Note: The version string is in cc-defs.
36 ;; This package provides GNU Emacs major modes for editing C, C++,
37 ;; Objective-C, Java, CORBA's IDL, Pike and AWK code. As of the
38 ;; latest Emacs and XEmacs releases, it is the default package for
39 ;; editing these languages. This package is called "CC Mode", and
40 ;; should be spelled exactly this way.
42 ;; CC Mode supports K&R and ANSI C, ANSI C++, Objective-C, Java,
43 ;; CORBA's IDL, Pike and AWK with a consistent indentation model
44 ;; across all modes. This indentation model is intuitive and very
45 ;; flexible, so that almost any desired style of indentation can be
46 ;; supported. Installation, usage, and programming details are
47 ;; contained in an accompanying texinfo manual.
49 ;; CC Mode's immediate ancestors were, c++-mode.el, cplus-md.el, and
50 ;; cplus-md1.el..
52 ;; To submit bug reports, type "C-c C-b". These will be sent to
53 ;; bug-gnu-emacs@gnu.org (mirrored as the Usenet newsgroup
54 ;; gnu.emacs.bug) as well as bug-cc-mode@gnu.org, which directly
55 ;; contacts the CC Mode maintainers. Questions can sent to
56 ;; help-gnu-emacs@gnu.org (mirrored as gnu.emacs.help) and/or
57 ;; bug-cc-mode@gnu.org. Please do not send bugs or questions to our
58 ;; personal accounts; we reserve the right to ignore such email!
60 ;; Many, many thanks go out to all the folks on the beta test list.
61 ;; Without their patience, testing, insight, code contributions, and
62 ;; encouragement CC Mode would be a far inferior package.
64 ;; You can get the latest version of CC Mode, including PostScript
65 ;; documentation and separate individual files from:
67 ;; http://cc-mode.sourceforge.net/
69 ;; You can join a moderated CC Mode announcement-only mailing list by
70 ;; visiting
72 ;; http://lists.sourceforge.net/mailman/listinfo/cc-mode-announce
74 ;;; Code:
76 ;; For Emacs < 22.2.
77 (eval-and-compile
78 (unless (fboundp 'declare-function) (defmacro declare-function (&rest _r))))
80 (eval-when-compile
81 (let ((load-path
82 (if (and (boundp 'byte-compile-dest-file)
83 (stringp byte-compile-dest-file))
84 (cons (file-name-directory byte-compile-dest-file) load-path)
85 load-path)))
86 (load "cc-bytecomp" nil t)))
88 (cc-require 'cc-defs)
89 (cc-require 'cc-vars)
90 (cc-require-when-compile 'cc-langs)
91 (cc-require 'cc-engine)
92 (cc-require 'cc-styles)
93 (cc-require 'cc-cmds)
94 (cc-require 'cc-align)
95 (cc-require 'cc-menus)
96 (cc-require 'cc-guess)
98 ;; Silence the compiler.
99 (cc-bytecomp-defvar adaptive-fill-first-line-regexp) ; Emacs
100 (cc-bytecomp-defun run-mode-hooks) ; Emacs 21.1
102 ;; We set these variables during mode init, yet we don't require
103 ;; font-lock.
104 (cc-bytecomp-defvar font-lock-defaults)
105 (cc-bytecomp-defvar font-lock-syntactic-keywords)
107 ;; Menu support for both XEmacs and Emacs. If you don't have easymenu
108 ;; with your version of Emacs, you are incompatible!
109 (cc-external-require 'easymenu)
111 ;; Load cc-fonts first after font-lock is loaded, since it isn't
112 ;; necessary until font locking is requested.
113 ; (eval-after-load "font-lock" ; 2006-07-09: font-lock is now preloaded.
115 (require 'cc-fonts) ;)
118 ;; Other modes and packages which depend on CC Mode should do the
119 ;; following to make sure everything is loaded and available for their
120 ;; use:
122 ;; (require 'cc-mode)
124 ;; And in the major mode function:
126 ;; (c-initialize-cc-mode t)
127 ;; (c-init-language-vars some-mode)
128 ;; (c-common-init 'some-mode) ; Or perhaps (c-basic-common-init 'some-mode)
130 ;; If you're not writing a derived mode using the language variable
131 ;; system, then some-mode is one of the language modes directly
132 ;; supported by CC Mode. You can then use (c-init-language-vars-for
133 ;; 'some-mode) instead of `c-init-language-vars'.
134 ;; `c-init-language-vars-for' is a function that avoids the rather
135 ;; large expansion of `c-init-language-vars'.
137 ;; If you use `c-basic-common-init' then you might want to call
138 ;; `c-font-lock-init' too to set up CC Mode's font lock support.
140 ;; See cc-langs.el for further info. A small example of a derived mode
141 ;; is also available at <http://cc-mode.sourceforge.net/
142 ;; derived-mode-ex.el>.
144 (defun c-leave-cc-mode-mode ()
145 (setq c-buffer-is-cc-mode nil))
147 (defun c-init-language-vars-for (mode)
148 "Initialize the language variables for one of the language modes
149 directly supported by CC Mode. This can be used instead of the
150 `c-init-language-vars' macro if the language you want to use is one of
151 those, rather than a derived language defined through the language
152 variable system (see \"cc-langs.el\")."
153 (cond ((eq mode 'c-mode) (c-init-language-vars c-mode))
154 ((eq mode 'c++-mode) (c-init-language-vars c++-mode))
155 ((eq mode 'objc-mode) (c-init-language-vars objc-mode))
156 ((eq mode 'java-mode) (c-init-language-vars java-mode))
157 ((eq mode 'idl-mode) (c-init-language-vars idl-mode))
158 ((eq mode 'pike-mode) (c-init-language-vars pike-mode))
159 ((eq mode 'awk-mode) (c-init-language-vars awk-mode))
160 (t (error "Unsupported mode %s" mode))))
162 ;;;###autoload
163 (defun c-initialize-cc-mode (&optional new-style-init)
164 "Initialize CC Mode for use in the current buffer.
165 If the optional NEW-STYLE-INIT is nil or left out then all necessary
166 initialization to run CC Mode for the C language is done. Otherwise
167 only some basic setup is done, and a call to `c-init-language-vars' or
168 `c-init-language-vars-for' is necessary too (which gives more
169 control). See \"cc-mode.el\" for more info."
171 (setq c-buffer-is-cc-mode t)
173 (let ((initprop 'cc-mode-is-initialized)
174 c-initialization-ok)
175 (unless (get 'c-initialize-cc-mode initprop)
176 (unwind-protect
177 (progn
178 (put 'c-initialize-cc-mode initprop t)
179 (c-initialize-builtin-style)
180 (run-hooks 'c-initialization-hook)
181 ;; Fix obsolete variables.
182 (if (boundp 'c-comment-continuation-stars)
183 (setq c-block-comment-prefix c-comment-continuation-stars))
184 (add-hook 'change-major-mode-hook 'c-leave-cc-mode-mode)
185 (setq c-initialization-ok t)
186 ;; Connect up with Emacs's electric-indent-mode, for >= Emacs 24.4
187 (when (fboundp 'electric-indent-local-mode)
188 (add-hook 'electric-indent-mode-hook 'c-electric-indent-mode-hook)
189 (add-hook 'electric-indent-local-mode-hook
190 'c-electric-indent-local-mode-hook)))
191 ;; Will try initialization hooks again if they failed.
192 (put 'c-initialize-cc-mode initprop c-initialization-ok))))
194 (unless new-style-init
195 (c-init-language-vars-for 'c-mode)))
198 ;;; Common routines.
200 (defvar c-mode-base-map ()
201 "Keymap shared by all CC Mode related modes.")
203 (defun c-make-inherited-keymap ()
204 (let ((map (make-sparse-keymap)))
205 ;; Necessary to use `cc-bytecomp-fboundp' below since this
206 ;; function is called from top-level forms that are evaluated
207 ;; while cc-bytecomp is active when one does M-x eval-buffer.
208 (cond
209 ;; Emacs
210 ((cc-bytecomp-fboundp 'set-keymap-parent)
211 (set-keymap-parent map c-mode-base-map))
212 ;; XEmacs
213 ((fboundp 'set-keymap-parents)
214 (set-keymap-parents map c-mode-base-map))
215 ;; incompatible
216 (t (error "CC Mode is incompatible with this version of Emacs")))
217 map))
219 (defun c-define-abbrev-table (name defs &optional doc)
220 ;; Compatibility wrapper for `define-abbrev' which passes a non-nil
221 ;; sixth argument for SYSTEM-FLAG in emacsen that support it
222 ;; (currently only Emacs >= 21.2).
223 (let ((table (or (and (boundp name) (symbol-value name))
224 (progn (condition-case nil
225 (define-abbrev-table name nil doc)
226 (wrong-number-of-arguments ;E.g. Emacs<23.
227 (eval `(defvar ,name nil ,doc))
228 (define-abbrev-table name nil)))
229 (symbol-value name)))))
230 (while defs
231 (condition-case nil
232 (apply 'define-abbrev table (append (car defs) '(t)))
233 (wrong-number-of-arguments
234 (apply 'define-abbrev table (car defs))))
235 (setq defs (cdr defs)))))
236 (put 'c-define-abbrev-table 'lisp-indent-function 1)
238 (defun c-bind-special-erase-keys ()
239 ;; Only used in Emacs to bind C-c C-<delete> and C-c C-<backspace>
240 ;; to the proper keys depending on `normal-erase-is-backspace'.
241 (if normal-erase-is-backspace
242 (progn
243 (define-key c-mode-base-map (kbd "C-c C-<delete>")
244 'c-hungry-delete-forward)
245 (define-key c-mode-base-map (kbd "C-c C-<backspace>")
246 'c-hungry-delete-backwards))
247 (define-key c-mode-base-map (kbd "C-c C-<delete>")
248 'c-hungry-delete-backwards)
249 (define-key c-mode-base-map (kbd "C-c C-<backspace>")
250 'c-hungry-delete-forward)))
252 (if c-mode-base-map
255 (setq c-mode-base-map (make-sparse-keymap))
257 ;; Separate M-BS from C-M-h. The former should remain
258 ;; backward-kill-word.
259 (define-key c-mode-base-map [(control meta h)] 'c-mark-function)
260 (define-key c-mode-base-map "\e\C-q" 'c-indent-exp)
261 (substitute-key-definition 'backward-sentence
262 'c-beginning-of-statement
263 c-mode-base-map global-map)
264 (substitute-key-definition 'forward-sentence
265 'c-end-of-statement
266 c-mode-base-map global-map)
267 (substitute-key-definition 'indent-new-comment-line
268 'c-indent-new-comment-line
269 c-mode-base-map global-map)
270 (substitute-key-definition 'indent-for-tab-command
271 ;; XXX Is this the right thing to do
272 ;; here?
273 'c-indent-line-or-region
274 c-mode-base-map global-map)
275 (when (fboundp 'comment-indent-new-line)
276 ;; indent-new-comment-line has changed name to
277 ;; comment-indent-new-line in Emacs 21.
278 (substitute-key-definition 'comment-indent-new-line
279 'c-indent-new-comment-line
280 c-mode-base-map global-map))
282 ;; RMS says don't make these the default.
283 ;; (April 2006): RMS has now approved these commands as defaults.
284 (unless (memq 'argumentative-bod-function c-emacs-features)
285 (define-key c-mode-base-map "\e\C-a" 'c-beginning-of-defun)
286 (define-key c-mode-base-map "\e\C-e" 'c-end-of-defun))
288 (define-key c-mode-base-map "\C-c\C-n" 'c-forward-conditional)
289 (define-key c-mode-base-map "\C-c\C-p" 'c-backward-conditional)
290 (define-key c-mode-base-map "\C-c\C-u" 'c-up-conditional)
292 ;; It doesn't suffice to put `c-fill-paragraph' on
293 ;; `fill-paragraph-function' since `c-fill-paragraph' must be called
294 ;; before any fill prefix adaption is done. E.g. `filladapt-mode'
295 ;; replaces `fill-paragraph' and does the adaption before calling
296 ;; `fill-paragraph-function', and we have to mask comments etc
297 ;; before that. Also, `c-fill-paragraph' chains on to
298 ;; `fill-paragraph' and the value on `fill-paragraph-function' to
299 ;; do the actual filling work.
300 (substitute-key-definition 'fill-paragraph 'c-fill-paragraph
301 c-mode-base-map global-map)
302 ;; In XEmacs the default fill function is called
303 ;; fill-paragraph-or-region.
304 (substitute-key-definition 'fill-paragraph-or-region 'c-fill-paragraph
305 c-mode-base-map global-map)
307 ;; We bind the forward deletion key and (implicitly) C-d to
308 ;; `c-electric-delete-forward', and the backward deletion key to
309 ;; `c-electric-backspace'. The hungry variants are bound to the
310 ;; same keys but prefixed with C-c. This implies that C-c C-d is
311 ;; `c-hungry-delete-forward'. For consistency, we bind not only C-c
312 ;; <backspace> to `c-hungry-delete-backwards' but also
313 ;; C-c C-<backspace>, so that the Ctrl key can be held down during
314 ;; the whole sequence regardless of the direction. This in turn
315 ;; implies that we bind C-c C-<delete> to `c-hungry-delete-forward',
316 ;; for the same reason.
318 ;; Bind the electric deletion functions to C-d and DEL. Emacs 21
319 ;; automatically maps the [delete] and [backspace] keys to these two
320 ;; depending on window system and user preferences. (In earlier
321 ;; versions it's possible to do the same by using `function-key-map'.)
322 (define-key c-mode-base-map "\C-d" 'c-electric-delete-forward)
323 (define-key c-mode-base-map "\177" 'c-electric-backspace)
324 (define-key c-mode-base-map "\C-c\C-d" 'c-hungry-delete-forward)
325 (define-key c-mode-base-map [?\C-c ?\d] 'c-hungry-delete-backwards)
326 (define-key c-mode-base-map [?\C-c ?\C-\d] 'c-hungry-delete-backwards)
327 (define-key c-mode-base-map [?\C-c deletechar] 'c-hungry-delete-forward) ; C-c <delete> on a tty.
328 (define-key c-mode-base-map [?\C-c (control deletechar)] ; C-c C-<delete> on a tty.
329 'c-hungry-delete-forward)
330 (when (boundp 'normal-erase-is-backspace)
331 ;; The automatic C-d and DEL mapping functionality doesn't extend
332 ;; to special combinations like C-c C-<delete>, so we have to hook
333 ;; into the `normal-erase-is-backspace' system to bind it directly
334 ;; as appropriate.
335 (add-hook 'normal-erase-is-backspace-hook 'c-bind-special-erase-keys)
336 (c-bind-special-erase-keys))
338 (when (fboundp 'delete-forward-p)
339 ;; In XEmacs we fix the forward and backward deletion behavior by
340 ;; binding the keysyms for the [delete] and [backspace] keys
341 ;; directly, and use `delete-forward-p' to decide what [delete]
342 ;; should do. That's done in the XEmacs specific
343 ;; `c-electric-delete' and `c-hungry-delete' functions.
344 (define-key c-mode-base-map [delete] 'c-electric-delete)
345 (define-key c-mode-base-map [backspace] 'c-electric-backspace)
346 (define-key c-mode-base-map (kbd "C-c <delete>") 'c-hungry-delete)
347 (define-key c-mode-base-map (kbd "C-c C-<delete>") 'c-hungry-delete)
348 (define-key c-mode-base-map (kbd "C-c <backspace>")
349 'c-hungry-delete-backwards)
350 (define-key c-mode-base-map (kbd "C-c C-<backspace>")
351 'c-hungry-delete-backwards))
353 (define-key c-mode-base-map "#" 'c-electric-pound)
354 (define-key c-mode-base-map "{" 'c-electric-brace)
355 (define-key c-mode-base-map "}" 'c-electric-brace)
356 (define-key c-mode-base-map "/" 'c-electric-slash)
357 (define-key c-mode-base-map "*" 'c-electric-star)
358 (define-key c-mode-base-map ";" 'c-electric-semi&comma)
359 (define-key c-mode-base-map "," 'c-electric-semi&comma)
360 (define-key c-mode-base-map ":" 'c-electric-colon)
361 (define-key c-mode-base-map "(" 'c-electric-paren)
362 (define-key c-mode-base-map ")" 'c-electric-paren)
364 (define-key c-mode-base-map "\C-c\C-\\" 'c-backslash-region)
365 (define-key c-mode-base-map "\C-c\C-a" 'c-toggle-auto-newline)
366 (define-key c-mode-base-map "\C-c\C-b" 'c-submit-bug-report)
367 (define-key c-mode-base-map "\C-c\C-c" 'comment-region)
368 (define-key c-mode-base-map "\C-c\C-l" 'c-toggle-electric-state)
369 (define-key c-mode-base-map "\C-c\C-o" 'c-set-offset)
370 (define-key c-mode-base-map "\C-c\C-q" 'c-indent-defun)
371 (define-key c-mode-base-map "\C-c\C-s" 'c-show-syntactic-information)
372 ;; (define-key c-mode-base-map "\C-c\C-t" 'c-toggle-auto-hungry-state) Commented out by ACM, 2005-03-05.
373 (define-key c-mode-base-map "\C-c." 'c-set-style)
374 ;; conflicts with OOBR
375 ;;(define-key c-mode-base-map "\C-c\C-v" 'c-version)
376 ;; (define-key c-mode-base-map "\C-c\C-y" 'c-toggle-hungry-state) Commented out by ACM, 2005-11-22.
377 (define-key c-mode-base-map "\C-c\C-w" 'c-subword-mode)
380 ;; We don't require the outline package, but we configure it a bit anyway.
381 (cc-bytecomp-defvar outline-level)
383 (defun c-mode-menu (modestr)
384 "Return a menu spec suitable for `easy-menu-define' that is exactly
385 like the C mode menu except that the menu bar item name is MODESTR
386 instead of \"C\".
388 This function is provided for compatibility only; derived modes should
389 preferably use the `c-mode-menu' language constant directly."
390 (cons modestr (c-lang-const c-mode-menu c)))
392 ;; Ugly hack to pull in the definition of `c-populate-syntax-table'
393 ;; from cc-langs to make it available at runtime. It's either this or
394 ;; moving the definition for it to cc-defs, but that would mean to
395 ;; break up the syntax table setup over two files.
396 (defalias 'c-populate-syntax-table
397 (cc-eval-when-compile
398 (let ((f (symbol-function 'c-populate-syntax-table)))
399 (if (byte-code-function-p f) f (byte-compile f)))))
401 ;; CAUTION: Try to avoid installing things on
402 ;; `before-change-functions'. The macro `combine-after-change-calls'
403 ;; is used and it doesn't work if there are things on that hook. That
404 ;; can cause font lock functions to run in inconvenient places during
405 ;; temporary changes in some font lock support modes, causing extra
406 ;; unnecessary work and font lock glitches due to interactions between
407 ;; various text properties.
409 ;; (2007-02-12): The macro `combine-after-change-calls' ISN'T used any
410 ;; more.
412 (defun c-unfind-enclosing-token (pos)
413 ;; If POS is wholly inside a token, remove that id from
414 ;; `c-found-types', should it be present. Return t if we were in an
415 ;; id, else nil.
416 (save-excursion
417 (let ((tok-beg (progn (goto-char pos)
418 (and (c-beginning-of-current-token) (point))))
419 (tok-end (progn (goto-char pos)
420 (and (c-end-of-current-token) (point)))))
421 (when (and tok-beg tok-end)
422 (c-unfind-type (buffer-substring-no-properties tok-beg tok-end))
423 t))))
425 (defun c-unfind-coalesced-tokens (beg end)
426 ;; unless the non-empty region (beg end) is entirely WS and there's at
427 ;; least one character of WS just before or after this region, remove
428 ;; the tokens which touch the region from `c-found-types' should they
429 ;; be present.
430 (or (c-partial-ws-p beg end)
431 (save-excursion
432 (progn
433 (goto-char beg)
434 (or (eq beg (point-min))
435 (c-skip-ws-backward (1- beg))
436 (/= (point) beg)
437 (= (c-backward-token-2) 1)
438 (c-unfind-type (buffer-substring-no-properties
439 (point) beg)))
440 (goto-char end)
441 (or (eq end (point-max))
442 (c-skip-ws-forward (1+ end))
443 (/= (point) end)
444 (progn (forward-char) (c-end-of-current-token) nil)
445 (c-unfind-type (buffer-substring-no-properties
446 end (point))))))))
448 ;; c-maybe-stale-found-type records a place near the region being
449 ;; changed where an element of `found-types' might become stale. It
450 ;; is set in c-before-change and is either nil, or has the form:
452 ;; (c-decl-id-start "foo" 97 107 " (* ooka) " "o"), where
454 ;; o - `c-decl-id-start' is the c-type text property value at buffer
455 ;; pos 96.
457 ;; o - 97 107 is the region potentially containing the stale type -
458 ;; this is delimited by a non-nil c-type text property at 96 and
459 ;; either another one or a ";", "{", or "}" at 107.
461 ;; o - " (* ooka) " is the (before change) buffer portion containing
462 ;; the suspect type (here "ooka").
464 ;; o - "o" is the buffer contents which is about to be deleted. This
465 ;; would be the empty string for an insertion.
466 (defvar c-maybe-stale-found-type nil)
467 (make-variable-buffer-local 'c-maybe-stale-found-type)
469 (defvar c-just-done-before-change nil)
470 (make-variable-buffer-local 'c-just-done-before-change)
471 ;; This variable is set to t by `c-before-change' and to nil by
472 ;; `c-after-change'. It is used to detect a spurious invocation of
473 ;; `before-change-functions' directly following on from a correct one. This
474 ;; happens in some Emacsen, for example when `basic-save-buffer' does (insert
475 ;; ?\n) when `require-final-newline' is non-nil.
477 (defun c-basic-common-init (mode default-style)
478 "Do the necessary initialization for the syntax handling routines
479 and the line breaking/filling code. Intended to be used by other
480 packages that embed CC Mode.
482 MODE is the CC Mode flavor to set up, e.g. 'c-mode or 'java-mode.
483 DEFAULT-STYLE tells which indentation style to install. It has the
484 same format as `c-default-style'.
486 Note that `c-init-language-vars' must be called before this function.
487 This function cannot do that since `c-init-language-vars' is a macro
488 that requires a literal mode spec at compile time."
490 (setq c-buffer-is-cc-mode mode)
492 ;; these variables should always be buffer local; they do not affect
493 ;; indentation style.
494 (make-local-variable 'comment-start)
495 (make-local-variable 'comment-end)
496 (make-local-variable 'comment-start-skip)
498 (make-local-variable 'paragraph-start)
499 (make-local-variable 'paragraph-separate)
500 (make-local-variable 'paragraph-ignore-fill-prefix)
501 (make-local-variable 'adaptive-fill-mode)
502 (make-local-variable 'adaptive-fill-regexp)
503 (make-local-variable 'fill-paragraph-handle-comment)
505 ;; now set their values
506 (set (make-local-variable 'parse-sexp-ignore-comments) t)
507 (set (make-local-variable 'indent-line-function) 'c-indent-line)
508 (set (make-local-variable 'indent-region-function) 'c-indent-region)
509 (set (make-local-variable 'normal-auto-fill-function) 'c-do-auto-fill)
510 (set (make-local-variable 'comment-multi-line) t)
511 (set (make-local-variable 'comment-line-break-function)
512 'c-indent-new-comment-line)
514 ;; For the benefit of adaptive file, which otherwise mis-fills.
515 (setq fill-paragraph-handle-comment nil)
517 ;; Install `c-fill-paragraph' on `fill-paragraph-function' so that a
518 ;; direct call to `fill-paragraph' behaves better. This still
519 ;; doesn't work with filladapt but it's better than nothing.
520 (set (make-local-variable 'fill-paragraph-function) 'c-fill-paragraph)
522 ;; Initialize the cache of brace pairs, and opening braces/brackets/parens.
523 (c-state-cache-init)
525 (when (or c-recognize-<>-arglists
526 (c-major-mode-is 'awk-mode)
527 (c-major-mode-is '(java-mode c-mode c++-mode objc-mode)))
528 ;; We'll use the syntax-table text property to change the syntax
529 ;; of some chars for this language, so do the necessary setup for
530 ;; that.
532 ;; Note to other package developers: It's ok to turn this on in CC
533 ;; Mode buffers when CC Mode doesn't, but it's not ok to turn it
534 ;; off if CC Mode has turned it on.
536 ;; Emacs.
537 (when (boundp 'parse-sexp-lookup-properties)
538 (set (make-local-variable 'parse-sexp-lookup-properties) t))
540 ;; Same as above for XEmacs.
541 (when (boundp 'lookup-syntax-properties)
542 (set (make-local-variable 'lookup-syntax-properties) t)))
544 ;; Use this in Emacs 21+ to avoid meddling with the rear-nonsticky
545 ;; property on each character.
546 (when (boundp 'text-property-default-nonsticky)
547 (make-local-variable 'text-property-default-nonsticky)
548 (mapc (lambda (tprop)
549 (unless (assq tprop text-property-default-nonsticky)
550 (setq text-property-default-nonsticky
551 (cons `(,tprop . t) text-property-default-nonsticky))))
552 '(syntax-table category c-type)))
554 ;; In Emacs 21 and later it's possible to turn off the ad-hoc
555 ;; heuristic that open parens in column 0 are defun starters. Since
556 ;; we have c-state-cache, that heuristic isn't useful and only causes
557 ;; trouble, so turn it off.
558 ;; 2006/12/17: This facility is somewhat confused, and doesn't really seem
559 ;; helpful. Comment it out for now.
560 ;; (when (memq 'col-0-paren c-emacs-features)
561 ;; (make-local-variable 'open-paren-in-column-0-is-defun-start)
562 ;; (setq open-paren-in-column-0-is-defun-start nil))
564 (c-clear-found-types)
566 ;; now set the mode style based on default-style
567 (let ((style (cc-choose-style-for-mode mode default-style)))
568 ;; Override style variables if `c-old-style-variable-behavior' is
569 ;; set. Also override if we are using global style variables,
570 ;; have already initialized a style once, and are switching to a
571 ;; different style. (It's doubtful whether this is desirable, but
572 ;; the whole situation with nonlocal style variables is a bit
573 ;; awkward. It's at least the most compatible way with the old
574 ;; style init procedure.)
575 (c-set-style style (not (or c-old-style-variable-behavior
576 (and (not c-style-variables-are-local-p)
577 c-indentation-style
578 (not (string-equal c-indentation-style
579 style)))))))
580 (c-setup-paragraph-variables)
582 ;; we have to do something special for c-offsets-alist so that the
583 ;; buffer local value has its own alist structure.
584 (setq c-offsets-alist (copy-alist c-offsets-alist))
586 ;; setup the comment indent variable in a Emacs version portable way
587 (set (make-local-variable 'comment-indent-function) 'c-comment-indent)
589 ;; In Emacs 24.4 onwards, prevent Emacs's built in electric indentation from
590 ;; messing up CC Mode's, and set `c-electric-flag' if `electric-indent-mode'
591 ;; has been called by the user.
592 (when (boundp 'electric-indent-inhibit) (setq electric-indent-inhibit t))
593 ;; CC-mode should obey Emacs's generic preferences, tho only do it if
594 ;; Emacs's generic preferences can be set per-buffer (Emacs>=24.4).
595 (when (fboundp 'electric-indent-local-mode)
596 (setq c-electric-flag electric-indent-mode))
598 ;; ;; Put submode indicators onto minor-mode-alist, but only once.
599 ;; (or (assq 'c-submode-indicators minor-mode-alist)
600 ;; (setq minor-mode-alist
601 ;; (cons '(c-submode-indicators c-submode-indicators)
602 ;; minor-mode-alist)))
603 (c-update-modeline)
605 ;; Install the functions that ensure that various internal caches
606 ;; don't become invalid due to buffer changes.
607 (when (featurep 'xemacs)
608 (make-local-hook 'before-change-functions)
609 (make-local-hook 'after-change-functions))
610 (add-hook 'before-change-functions 'c-before-change nil t)
611 (setq c-just-done-before-change nil)
612 (add-hook 'after-change-functions 'c-after-change nil t)
613 (when (boundp 'font-lock-extend-after-change-region-function)
614 (set (make-local-variable 'font-lock-extend-after-change-region-function)
615 'c-extend-after-change-region))) ; Currently (2009-05) used by all
616 ; languages with #define (C, C++,; ObjC), and by AWK.
618 (defun c-setup-doc-comment-style ()
619 "Initialize the variables that depend on the value of `c-doc-comment-style'."
620 (when (and (featurep 'font-lock)
621 (symbol-value 'font-lock-mode))
622 ;; Force font lock mode to reinitialize itself.
623 (font-lock-mode 0)
624 (font-lock-mode 1)))
626 ;; Buffer local variables defining the region to be fontified by a font lock
627 ;; after-change function. They are set in c-after-change to
628 ;; after-change-functions' BEG and END, and may be modified by functions in
629 ;; `c-before-font-lock-functions'.
630 (defvar c-new-BEG 0)
631 (make-variable-buffer-local 'c-new-BEG)
632 (defvar c-new-END 0)
633 (make-variable-buffer-local 'c-new-END)
635 (defun c-common-init (&optional mode)
636 "Common initialization for all CC Mode modes.
637 In addition to the work done by `c-basic-common-init' and
638 `c-font-lock-init', this function sets up various other things as
639 customary in CC Mode modes but which aren't strictly necessary for CC
640 Mode to operate correctly.
642 MODE is the symbol for the mode to initialize, like 'c-mode. See
643 `c-basic-common-init' for details. It's only optional to be
644 compatible with old code; callers should always specify it."
646 (unless mode
647 ;; Called from an old third party package. The fallback is to
648 ;; initialize for C.
649 (c-init-language-vars-for 'c-mode))
651 (c-basic-common-init mode c-default-style)
652 (when mode
653 ;; Only initialize font locking if we aren't called from an old package.
654 (c-font-lock-init))
656 ;; Starting a mode is a sort of "change". So call the change functions...
657 (save-restriction
658 (widen)
659 (setq c-new-BEG (point-min))
660 (setq c-new-END (point-max))
661 (save-excursion
662 (mapc (lambda (fn)
663 (funcall fn (point-min) (point-max)))
664 c-get-state-before-change-functions)
665 (mapc (lambda (fn)
666 (funcall fn (point-min) (point-max)
667 (- (point-max) (point-min))))
668 c-before-font-lock-functions)))
670 (set (make-local-variable 'outline-regexp) "[^#\n\^M]")
671 (set (make-local-variable 'outline-level) 'c-outline-level)
672 (set (make-local-variable 'add-log-current-defun-function)
673 (lambda ()
674 (or (c-cpp-define-name) (c-defun-name))))
675 (let ((rfn (assq mode c-require-final-newline)))
676 (when rfn
677 (if (boundp 'mode-require-final-newline)
678 (and (cdr rfn)
679 (set (make-local-variable 'require-final-newline)
680 mode-require-final-newline))
681 (set (make-local-variable 'require-final-newline) (cdr rfn))))))
683 (defun c-count-cfss (lv-alist)
684 ;; LV-ALIST is an alist like `file-local-variables-alist'. Count how many
685 ;; elements with the key `c-file-style' there are in it.
686 (let ((elt-ptr lv-alist) elt (cownt 0))
687 (while elt-ptr
688 (setq elt (car elt-ptr)
689 elt-ptr (cdr elt-ptr))
690 (when (eq (car elt) 'c-file-style)
691 (setq cownt (1+ cownt))))
692 cownt))
694 (defun c-before-hack-hook ()
695 "Set the CC Mode style and \"offsets\" when in the buffer's local variables.
696 They are set only when, respectively, the pseudo variables
697 `c-file-style' and `c-file-offsets' are present in the list.
699 This function is called from the hook `before-hack-local-variables-hook'."
700 (when c-buffer-is-cc-mode
701 (let ((mode-cons (assq 'mode file-local-variables-alist))
702 (stile (cdr (assq 'c-file-style file-local-variables-alist)))
703 (offsets (cdr (assq 'c-file-offsets file-local-variables-alist))))
704 (when mode-cons
705 (hack-one-local-variable (car mode-cons) (cdr mode-cons))
706 (setq file-local-variables-alist
707 (delq mode-cons file-local-variables-alist)))
708 (when stile
709 (or (stringp stile) (error "c-file-style is not a string"))
710 (if (boundp 'dir-local-variables-alist)
711 ;; Determine whether `c-file-style' was set in the file's local
712 ;; variables or in a .dir-locals.el (a directory setting).
713 (let ((cfs-in-file-and-dir-count
714 (c-count-cfss file-local-variables-alist))
715 (cfs-in-dir-count (c-count-cfss dir-local-variables-alist)))
716 (c-set-style stile
717 (and (= cfs-in-file-and-dir-count cfs-in-dir-count)
718 'keep-defaults)))
719 (c-set-style stile)))
720 (when offsets
721 (mapc
722 (lambda (langentry)
723 (let ((langelem (car langentry))
724 (offset (cdr langentry)))
725 (c-set-offset langelem offset)))
726 offsets)))))
728 (defun c-remove-any-local-eval-or-mode-variables ()
729 ;; If the buffer specifies `mode' or `eval' in its File Local Variable list
730 ;; or on the first line, remove all occurrences. See
731 ;; `c-postprocess-file-styles' for justification. There is no need to save
732 ;; point here, or even bother too much about the buffer contents. However,
733 ;; DON'T mess up the kill-ring.
735 ;; Most of the code here is derived from Emacs 21.3's `hack-local-variables'
736 ;; in files.el.
737 (goto-char (point-max))
738 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
739 (let (lv-point (prefix "") (suffix ""))
740 (when (let ((case-fold-search t))
741 (search-forward "Local Variables:" nil t))
742 (setq lv-point (point))
743 ;; The prefix is what comes before "local variables:" in its line.
744 ;; The suffix is what comes after "local variables:" in its line.
745 (skip-chars-forward " \t")
746 (or (eolp)
747 (setq suffix (buffer-substring (point)
748 (progn (end-of-line) (point)))))
749 (goto-char (match-beginning 0))
750 (or (bolp)
751 (setq prefix
752 (buffer-substring (point)
753 (progn (beginning-of-line) (point)))))
755 (while (search-forward-regexp
756 (concat "^[ \t]*"
757 (regexp-quote prefix)
758 "\\(mode\\|eval\\):.*"
759 (regexp-quote suffix)
760 "$")
761 nil t)
762 (forward-line 0)
763 (delete-region (point) (progn (forward-line) (point)))))
765 ;; Delete the first line, if we've got one, in case it contains a mode spec.
766 (unless (and lv-point
767 (progn (goto-char lv-point)
768 (forward-line 0)
769 (bobp)))
770 (goto-char (point-min))
771 (unless (eobp)
772 (delete-region (point) (progn (forward-line) (point)))))))
774 (defun c-postprocess-file-styles ()
775 "Function that post processes relevant file local variables in CC Mode.
776 Currently, this function simply applies any style and offset settings
777 found in the file's Local Variable list. It first applies any style
778 setting found in `c-file-style', then it applies any offset settings
779 it finds in `c-file-offsets'.
781 Note that the style variables are always made local to the buffer."
783 ;; apply file styles and offsets
784 (when c-buffer-is-cc-mode
785 (if (or c-file-style c-file-offsets)
786 (c-make-styles-buffer-local t))
787 (when c-file-style
788 (or (stringp c-file-style)
789 (error "c-file-style is not a string"))
790 (c-set-style c-file-style))
792 (and c-file-offsets
793 (mapc
794 (lambda (langentry)
795 (let ((langelem (car langentry))
796 (offset (cdr langentry)))
797 (c-set-offset langelem offset)))
798 c-file-offsets))
799 ;; Problem: The file local variable block might have explicitly set a
800 ;; style variable. The `c-set-style' or `mapcar' call might have
801 ;; overwritten this. So we run `hack-local-variables' again to remedy
802 ;; this. There are no guarantees this will work properly, particularly as
803 ;; we have no control over what the other hook functions on
804 ;; `hack-local-variables-hook' would have done. We now (2006/2/1) remove
805 ;; any `eval' or `mode' expressions before we evaluate again (see below).
806 ;; ACM, 2005/11/2.
808 ;; Problem (bug reported by Gustav Broberg): if one of the variables is
809 ;; `mode', this will invoke c-mode (etc.) again, setting up the style etc.
810 ;; We prevent this by temporarily removing `mode' from the Local Variables
811 ;; section.
812 (if (or c-file-style c-file-offsets)
813 (let ((hack-local-variables-hook nil) (inhibit-read-only t))
814 (c-tentative-buffer-changes
815 (c-remove-any-local-eval-or-mode-variables)
816 (hack-local-variables))
817 nil))))
819 (if (boundp 'before-hack-local-variables-hook)
820 (add-hook 'before-hack-local-variables-hook 'c-before-hack-hook)
821 (add-hook 'hack-local-variables-hook 'c-postprocess-file-styles))
823 (defmacro c-run-mode-hooks (&rest hooks)
824 ;; Emacs 21.1 has introduced a system with delayed mode hooks that
825 ;; requires the use of the new function `run-mode-hooks'.
826 (if (cc-bytecomp-fboundp 'run-mode-hooks)
827 `(run-mode-hooks ,@hooks)
828 `(progn ,@(mapcar (lambda (hook) `(run-hooks ,hook)) hooks))))
831 ;;; Change hooks, linking with Font Lock and electric-indent-mode.
833 ;; Buffer local variables recording Beginning/End-of-Macro position before a
834 ;; change, when a macro straddles, respectively, the BEG or END (or both) of
835 ;; the change region. Otherwise these have the values BEG/END.
836 (defvar c-old-BOM 0)
837 (make-variable-buffer-local 'c-old-BOM)
838 (defvar c-old-EOM 0)
839 (make-variable-buffer-local 'c-old-EOM)
841 (defun c-extend-region-for-CPP (beg end)
842 ;; Set c-old-BOM or c-old-EOM respectively to BEG, END, each extended to the
843 ;; beginning/end of any preprocessor construct they may be in.
845 ;; Point is undefined both before and after this function call; the buffer
846 ;; has already been widened, and match-data saved. The return value is
847 ;; meaningless.
849 ;; This function is in the C/C++/ObjC values of
850 ;; `c-get-state-before-change-functions' and is called exclusively as a
851 ;; before change function.
852 (goto-char beg)
853 (c-beginning-of-macro)
854 (setq c-old-BOM (point))
856 (goto-char end)
857 (when (c-beginning-of-macro)
858 (c-end-of-macro)
859 (or (eobp) (forward-char))) ; Over the terminating NL which may be marked
860 ; with a c-cpp-delimiter category property
861 (setq c-old-EOM (point)))
863 (defun c-extend-font-lock-region-for-macros (begg endd &optional old-len)
864 ;; Extend the region (BEGG ENDD) to cover all (possibly changed)
865 ;; preprocessor macros; return the cons (new-BEG . new-END). OLD-LEN should
866 ;; be either the old length parameter when called from an
867 ;; after-change-function, or nil otherwise. This defun uses the variables
868 ;; c-old-BOM, c-new-BOM.
870 ;; Point is undefined on both entry and exit to this function. The buffer
871 ;; will have been widened on entry.
872 (let (limits new-beg new-end)
873 (goto-char c-old-BOM) ; already set to old start of macro or begg.
874 (setq new-beg
875 (min begg
876 (if (setq limits (c-state-literal-at (point)))
877 (cdr limits) ; go forward out of any string or comment.
878 (point))))
880 (goto-char endd)
881 (if (setq limits (c-state-literal-at (point)))
882 (goto-char (car limits))) ; go backward out of any string or comment.
883 (if (c-beginning-of-macro)
884 (c-end-of-macro))
885 (setq new-end (max endd
886 (if old-len
887 (+ (- c-old-EOM old-len) (- endd begg))
888 c-old-EOM)
889 (point)))
890 (cons new-beg new-end)))
892 (defun c-neutralize-CPP-line (beg end)
893 ;; BEG and END bound a region, typically a preprocessor line. Put a
894 ;; "punctuation" syntax-table property on syntactically obtrusive
895 ;; characters, ones which would interact syntactically with stuff outside
896 ;; this region.
898 ;; These are unmatched string delimiters, or unmatched
899 ;; parens/brackets/braces. An unclosed comment is regarded as valid, NOT
900 ;; obtrusive.
901 (save-excursion
902 (let (s)
903 (while
904 (progn
905 (setq s (parse-partial-sexp beg end -1))
906 (cond
907 ((< (nth 0 s) 0) ; found an unmated ),},]
908 (c-put-char-property (1- (point)) 'syntax-table '(1))
910 ((nth 3 s) ; In a string
911 (c-put-char-property (nth 8 s) 'syntax-table '(1))
913 ((> (nth 0 s) 0) ; In a (,{,[
914 (c-put-char-property (nth 1 s) 'syntax-table '(1))
916 (t nil)))))))
918 (defun c-neutralize-syntax-in-and-mark-CPP (begg endd old-len)
919 ;; (i) Extend the font lock region to cover all changed preprocessor
920 ;; regions; it does this by setting the variables `c-new-BEG' and
921 ;; `c-new-END' to the new boundaries.
923 ;; (ii) "Neutralize" every preprocessor line wholly or partially in the
924 ;; extended changed region. "Restore" lines which were CPP lines before the
925 ;; change and are no longer so; these can be located from the Buffer local
926 ;; variables `c-old-BOM' and `c-old-EOM'.
928 ;; (iii) Mark every CPP construct by placing a `category' property value
929 ;; `c-cpp-delimiter' at its start and end. The marked characters are the
930 ;; opening # and usually the terminating EOL, but sometimes the character
931 ;; before a comment/string delimiter.
933 ;; That is, set syntax-table properties on characters that would otherwise
934 ;; interact syntactically with those outside the CPP line(s).
936 ;; This function is called from an after-change function, BEGG ENDD and
937 ;; OLD-LEN being the standard parameters. It prepares the buffer for font
938 ;; locking, hence must get called before `font-lock-after-change-function'.
940 ;; Point is undefined both before and after this function call, the buffer
941 ;; has been widened, and match-data saved. The return value is ignored.
943 ;; This function is in the C/C++/ObjC value of `c-before-font-lock-functions'.
945 ;; Note: SPEED _MATTERS_ IN THIS FUNCTION!!!
947 ;; This function might make hidden buffer changes.
948 (c-save-buffer-state (new-bounds)
949 ;; First determine the region, (c-new-BEG c-new-END), which will get font
950 ;; locked. It might need "neutralizing". This region may not start
951 ;; inside a string, comment, or macro.
952 (setq new-bounds (c-extend-font-lock-region-for-macros
953 c-new-BEG c-new-END old-len))
954 (setq c-new-BEG (max (car new-bounds) (c-determine-limit 500 begg))
955 c-new-END (min (cdr new-bounds) (c-determine-+ve-limit 500 endd)))
956 ;; Clear all old relevant properties.
957 (c-clear-char-property-with-value c-new-BEG c-new-END 'syntax-table '(1))
959 ;; CPP "comment" markers:
960 (if (eval-when-compile (memq 'category-properties c-emacs-features));Emacs.
961 (c-clear-char-property-with-value
962 c-new-BEG c-new-END 'category 'c-cpp-delimiter))
963 ;; FIXME!!! What about the "<" and ">" category properties? 2009-11-16
965 ;; Add needed properties to each CPP construct in the region.
966 (goto-char c-new-BEG)
967 (skip-chars-backward " \t")
968 (let ((pps-position (point)) pps-state mbeg)
969 (while (and (< (point) c-new-END)
970 (search-forward-regexp c-anchored-cpp-prefix c-new-END t))
971 ;; If we've found a "#" inside a string/comment, ignore it.
972 (setq pps-state
973 (parse-partial-sexp pps-position (point) nil nil pps-state)
974 pps-position (point))
975 (unless (or (nth 3 pps-state) ; in a string?
976 (nth 4 pps-state)) ; in a comment?
977 (goto-char (match-beginning 1))
978 (setq mbeg (point))
979 (if (> (c-syntactic-end-of-macro) mbeg)
980 (progn
981 (c-neutralize-CPP-line mbeg (point)) ; "punctuation" properties
982 (if (eval-when-compile
983 (memq 'category-properties c-emacs-features)) ;Emacs.
984 (c-set-cpp-delimiters mbeg (point)))) ; "comment" markers
985 (forward-line)) ; no infinite loop with, e.g., "#//"
986 )))))
988 (defun c-before-change (beg end)
989 ;; Function to be put on `before-change-functions'. Primarily, this calls
990 ;; the language dependent `c-get-state-before-change-functions'. It is
991 ;; otherwise used only to remove stale entries from the `c-found-types'
992 ;; cache, and to record entries which a `c-after-change' function might
993 ;; confirm as stale.
995 ;; Note that this function must be FAST rather than accurate. Note
996 ;; also that it only has any effect when font locking is enabled.
997 ;; We exploit this by checking for font-lock-*-face instead of doing
998 ;; rigorous syntactic analysis.
1000 ;; If either change boundary is wholly inside an identifier, delete
1001 ;; it/them from the cache. Don't worry about being inside a string
1002 ;; or a comment - "wrongly" removing a symbol from `c-found-types'
1003 ;; isn't critical.
1004 (unless c-just-done-before-change ; Guard against a spurious second
1005 ; invocation of before-change-functions.
1006 (setq c-just-done-before-change t)
1007 (setq c-maybe-stale-found-type nil)
1008 (save-restriction
1009 (save-match-data
1010 (widen)
1011 (save-excursion
1012 ;; Are we inserting/deleting stuff in the middle of an identifier?
1013 (c-unfind-enclosing-token beg)
1014 (c-unfind-enclosing-token end)
1015 ;; Are we coalescing two tokens together, e.g. "fo o" -> "foo"?
1016 (when (< beg end)
1017 (c-unfind-coalesced-tokens beg end))
1018 ;; Are we (potentially) disrupting the syntactic context which
1019 ;; makes a type a type? E.g. by inserting stuff after "foo" in
1020 ;; "foo bar;", or before "foo" in "typedef foo *bar;"?
1022 ;; We search for appropriate c-type properties "near" the change.
1023 ;; First, find an appropriate boundary for this property search.
1024 (let (lim
1025 type type-pos
1026 marked-id term-pos
1027 (end1
1028 (or (and (eq (get-text-property end 'face)
1029 'font-lock-comment-face)
1030 (previous-single-property-change end 'face))
1031 end)))
1032 (when (>= end1 beg) ; Don't hassle about changes entirely in comments.
1033 ;; Find a limit for the search for a `c-type' property
1034 (while
1035 (and (/= (skip-chars-backward "^;{}") 0)
1036 (> (point) (point-min))
1037 (memq (c-get-char-property (1- (point)) 'face)
1038 '(font-lock-comment-face font-lock-string-face))))
1039 (setq lim (max (point-min) (1- (point))))
1041 ;; Look for the latest `c-type' property before end1
1042 (when (and (> end1 (point-min))
1043 (setq type-pos
1044 (if (get-text-property (1- end1) 'c-type)
1045 end1
1046 (previous-single-property-change end1 'c-type
1047 nil lim))))
1048 (setq type (get-text-property (max (1- type-pos) lim) 'c-type))
1050 (when (memq type '(c-decl-id-start c-decl-type-start))
1051 ;; Get the identifier, if any, that the property is on.
1052 (goto-char (1- type-pos))
1053 (setq marked-id
1054 (when (looking-at "\\(\\sw\\|\\s_\\)")
1055 (c-beginning-of-current-token)
1056 (buffer-substring-no-properties (point) type-pos)))
1058 (goto-char end1)
1059 (skip-chars-forward "^;{}") ;FIXME!!! loop for comment, maybe
1060 (setq lim (point))
1061 (setq term-pos
1062 (or (c-next-single-property-change end 'c-type nil lim)
1063 lim))
1064 (setq c-maybe-stale-found-type
1065 (list type marked-id
1066 type-pos term-pos
1067 (buffer-substring-no-properties type-pos
1068 term-pos)
1069 (buffer-substring-no-properties beg end)))))))
1071 (if c-get-state-before-change-functions
1072 (let (open-paren-in-column-0-is-defun-start)
1073 (mapc (lambda (fn)
1074 (funcall fn beg end))
1075 c-get-state-before-change-functions)))
1077 ;; The following must be done here rather than in `c-after-change' because
1078 ;; newly inserted parens would foul up the invalidation algorithm.
1079 (c-invalidate-state-cache beg)))
1081 (defvar c-in-after-change-fontification nil)
1082 (make-variable-buffer-local 'c-in-after-change-fontification)
1083 ;; A flag to prevent region expanding stuff being done twice for after-change
1084 ;; fontification.
1086 (defun c-after-change (beg end old-len)
1087 ;; Function put on `after-change-functions' to adjust various caches
1088 ;; etc. Prefer speed to finesse here, since there will be an order
1089 ;; of magnitude more calls to this function than any of the
1090 ;; functions that use the caches.
1092 ;; Note that care must be taken so that this is called before any
1093 ;; font-lock callbacks since we might get calls to functions using
1094 ;; these caches from inside them, and we must thus be sure that this
1095 ;; has already been executed.
1097 ;; This calls the language variable c-before-font-lock-functions, if non nil.
1098 ;; This typically sets `syntax-table' properties.
1100 (setq c-just-done-before-change nil)
1101 (c-save-buffer-state (case-fold-search open-paren-in-column-0-is-defun-start)
1102 ;; When `combine-after-change-calls' is used we might get calls
1103 ;; with regions outside the current narrowing. This has been
1104 ;; observed in Emacs 20.7.
1105 (save-restriction
1106 (save-match-data ; c-recognize-<>-arglists changes match-data
1107 (widen)
1109 (when (> end (point-max))
1110 ;; Some emacsen might return positions past the end. This has been
1111 ;; observed in Emacs 20.7 when rereading a buffer changed on disk
1112 ;; (haven't been able to minimize it, but Emacs 21.3 appears to
1113 ;; work).
1114 (setq end (point-max))
1115 (when (> beg end)
1116 (setq beg end)))
1118 ;; C-y is capable of spuriously converting category properties
1119 ;; c-</>-as-paren-syntax and c-cpp-delimiter into hard syntax-table
1120 ;; properties. Remove these when it happens.
1121 (when (eval-when-compile (memq 'category-properties c-emacs-features))
1122 (c-clear-char-property-with-value beg end 'syntax-table
1123 c-<-as-paren-syntax)
1124 (c-clear-char-property-with-value beg end 'syntax-table
1125 c->-as-paren-syntax)
1126 (c-clear-char-property-with-value beg end 'syntax-table nil))
1128 (c-trim-found-types beg end old-len) ; maybe we don't need all of these.
1129 (c-invalidate-sws-region-after beg end)
1130 ;; (c-invalidate-state-cache beg) ; moved to `c-before-change'.
1131 (c-invalidate-find-decl-cache beg)
1133 (when c-recognize-<>-arglists
1134 (c-after-change-check-<>-operators beg end))
1136 ;; (c-new-BEG c-new-END) will be the region to fontify. It may become
1137 ;; larger than (beg end).
1138 (setq c-new-BEG beg
1139 c-new-END end)
1140 (setq c-in-after-change-fontification t)
1141 (save-excursion
1142 (mapc (lambda (fn)
1143 (funcall fn beg end old-len))
1144 c-before-font-lock-functions))))))
1146 (defun c-set-fl-decl-start (pos)
1147 ;; If the beginning of the line containing POS is in the middle of a "local"
1148 ;; declaration (i.e. one which does not start outside of braces enclosing
1149 ;; POS, such as a struct), return the beginning of that declaration.
1150 ;; Otherwise return POS. Note that declarations, in this sense, can be
1151 ;; nested.
1153 ;; This function is called indirectly from font locking stuff - either from
1154 ;; c-after-change (to prepare for after-change font-locking) or from font
1155 ;; lock context (etc.) fontification.
1156 (let ((lit-limits (c-literal-limits))
1157 (new-pos pos)
1158 bod-lim bo-decl)
1159 (goto-char (c-point 'bol new-pos))
1160 (when lit-limits ; Comment or string.
1161 (goto-char (car lit-limits)))
1162 (setq bod-lim (c-determine-limit 500))
1164 (while
1165 ;; Go to a less nested declaration each time round this loop.
1166 (and
1167 (eq (car (c-beginning-of-decl-1 bod-lim)) 'same)
1168 (progn (setq bo-decl (point))
1169 ;; Are we looking at a keyword such as "template" or
1170 ;; "typedef" which can decorate a type, or the type itself?
1171 (when (or (looking-at c-prefix-spec-kwds-re)
1172 (c-forward-type t))
1173 ;; We've found another candidate position.
1174 (setq new-pos (min new-pos bo-decl))
1175 (goto-char bo-decl))
1177 ;; Try and go out a level to search again.
1178 (progn
1179 (c-backward-syntactic-ws bod-lim)
1180 (and (> (point) bod-lim)
1181 (or (memq (char-before) '(?\( ?\[))
1182 (and (eq (char-before) ?\<)
1183 (eq (c-get-char-property
1184 (1- (point)) 'syntax-table)
1185 c-<-as-paren-syntax)))))
1186 (not (bobp)))
1187 (backward-char))
1188 new-pos)) ; back over (, [, <.
1190 (defun c-change-set-fl-decl-start (_beg _end _old-len)
1191 ;; Set c-new-BEG to the beginning of a "local" declaration if it('s BOL) is
1192 ;; inside one. This is called from an after-change-function, but the
1193 ;; parameters BEG END and OLD-LEN are ignored. See `c-set-fl-decl-start'
1194 ;; for the detailed functionality.
1195 (if font-lock-mode
1196 (setq c-new-BEG (c-set-fl-decl-start c-new-BEG))))
1198 (defun c-context-set-fl-decl-start (beg end)
1199 ;; Return a cons (NEW-BEG . END), where NEW-BEG is the beginning of a
1200 ;; "local" declaration (BOL at) NEW is inside or BEG. See
1201 ;; `c-set-fl-decl-start' for the detailed functionality.
1202 (cons (c-set-fl-decl-start beg) end))
1204 (defun c-font-lock-fontify-region (beg end &optional verbose)
1205 ;; Effectively advice around `font-lock-fontify-region' which extends the
1206 ;; region (BEG END), for example, to avoid context fontification chopping
1207 ;; off the start of the context. Do not do anything if it's already been
1208 ;; done (i.e. from an after-change fontification. An example (C++) where
1209 ;; this used to happen is this:
1211 ;; template <typename T>
1214 ;; void myfunc(T* p) {}
1216 ;; Type a space in the first blank line, and the fontification of the next
1217 ;; line was fouled up by context fontification.
1218 (let ((new-beg beg) (new-end end) new-region case-fold-search
1219 open-paren-in-column-0-is-defun-start)
1220 (if c-in-after-change-fontification
1221 (setq c-in-after-change-fontification nil)
1222 (save-restriction
1223 (widen)
1224 (save-excursion
1225 (mapc (lambda (fn)
1226 (setq new-region (funcall fn new-beg new-end))
1227 (setq new-beg (car new-region) new-end (cdr new-region)))
1228 c-before-context-fontification-functions))))
1229 (funcall (default-value 'font-lock-fontify-region-function)
1230 new-beg new-end verbose)))
1232 (defun c-after-font-lock-init ()
1233 ;; Put on `font-lock-mode-hook'. This function ensures our after-change
1234 ;; function will get executed before the font-lock one.
1235 (remove-hook 'after-change-functions 'c-after-change t)
1236 (add-hook 'after-change-functions 'c-after-change nil t))
1238 (defun c-font-lock-init ()
1239 "Set up the font-lock variables for using the font-lock support in CC Mode.
1240 This does not load the font-lock package. Use after
1241 `c-basic-common-init' and after cc-fonts has been loaded.
1242 This function is called from `c-common-init', once per mode initialization."
1244 (set (make-local-variable 'font-lock-defaults)
1245 `(,(if (c-major-mode-is 'awk-mode)
1246 ;; awk-mode currently has only one font lock level.
1247 'awk-font-lock-keywords
1248 (mapcar 'c-mode-symbol
1249 '("font-lock-keywords" "font-lock-keywords-1"
1250 "font-lock-keywords-2" "font-lock-keywords-3")))
1251 nil nil
1252 ,c-identifier-syntax-modifications
1253 c-beginning-of-syntax
1254 (font-lock-mark-block-function
1255 . c-mark-function)))
1257 (make-local-variable 'font-lock-fontify-region-function)
1258 (setq font-lock-fontify-region-function 'c-font-lock-fontify-region)
1260 (if (featurep 'xemacs)
1261 (make-local-hook 'font-lock-mode-hook))
1262 (add-hook 'font-lock-mode-hook 'c-after-font-lock-init nil t))
1264 ;; Emacs 22 and later.
1265 (defun c-extend-after-change-region (_beg _end _old-len)
1266 "Extend the region to be fontified, if necessary."
1267 ;; Note: the parameters are ignored here. This somewhat indirect
1268 ;; implementation exists because it is minimally different from the
1269 ;; stand-alone CC Mode which, lacking
1270 ;; font-lock-extend-after-change-region-function, is forced to use advice
1271 ;; instead.
1273 ;; Of the seven CC Mode languages, currently (2009-05) only C, C++, Objc
1274 ;; (the languages with #define) and AWK Mode make non-null use of this
1275 ;; function.
1276 (cons c-new-BEG c-new-END))
1278 ;; Emacs < 22 and XEmacs
1279 (defmacro c-advise-fl-for-region (function)
1280 `(defadvice ,function (before get-awk-region activate)
1281 ;; Make sure that any string/regexp is completely font-locked.
1282 (when c-buffer-is-cc-mode
1283 (save-excursion
1284 (ad-set-arg 1 c-new-END) ; end
1285 (ad-set-arg 0 c-new-BEG))))) ; beg
1287 (unless (boundp 'font-lock-extend-after-change-region-function)
1288 (c-advise-fl-for-region font-lock-after-change-function)
1289 (c-advise-fl-for-region jit-lock-after-change)
1290 (c-advise-fl-for-region lazy-lock-defer-rest-after-change)
1291 (c-advise-fl-for-region lazy-lock-defer-line-after-change))
1293 ;; Connect up to `electric-indent-mode' (Emacs 24.4 and later).
1294 (defun c-electric-indent-mode-hook ()
1295 ;; Emacs has en/disabled `electric-indent-mode'. Propagate this through to
1296 ;; each CC Mode buffer.
1297 (mapc (lambda (buf)
1298 (with-current-buffer buf
1299 (when c-buffer-is-cc-mode
1300 ;; Don't use `c-toggle-electric-state' here due to recursion.
1301 (setq c-electric-flag electric-indent-mode)
1302 (c-update-modeline))))
1303 (buffer-list)))
1305 (defun c-electric-indent-local-mode-hook ()
1306 ;; Emacs has en/disabled `electric-indent-local-mode' for this buffer.
1307 ;; Propagate this through to this buffer's value of `c-electric-flag'
1308 (when c-buffer-is-cc-mode
1309 (setq c-electric-flag electric-indent-mode)
1310 (c-update-modeline)))
1313 ;; Support for C
1315 (defvar c-mode-syntax-table
1316 (funcall (c-lang-const c-make-mode-syntax-table c))
1317 "Syntax table used in c-mode buffers.")
1319 (c-define-abbrev-table 'c-mode-abbrev-table
1320 '(("else" "else" c-electric-continued-statement 0)
1321 ("while" "while" c-electric-continued-statement 0))
1322 "Abbreviation table used in c-mode buffers.")
1324 (defvar c-mode-map
1325 (let ((map (c-make-inherited-keymap)))
1326 ;; Add bindings which are only useful for C.
1327 (define-key map "\C-c\C-e" 'c-macro-expand)
1328 map)
1329 "Keymap used in c-mode buffers.")
1332 (easy-menu-define c-c-menu c-mode-map "C Mode Commands"
1333 (cons "C" (c-lang-const c-mode-menu c)))
1335 ;; In XEmacs >= 21.5 modes should add their own entries to
1336 ;; `auto-mode-alist'. The comment form of autoload is used to avoid
1337 ;; doing this on load. That since `add-to-list' prepends the value
1338 ;; which could cause it to clobber user settings. Later emacsen have
1339 ;; an append option, but it's not safe to use.
1341 ;; The extension ".C" is associated with C++ while the lowercase
1342 ;; variant goes with C. On case insensitive file systems, this means
1343 ;; that ".c" files also might open C++ mode if the C++ entry comes
1344 ;; first on `auto-mode-alist'. Thus we try to ensure that ".C" comes
1345 ;; after ".c", and since `add-to-list' adds the entry first we have to
1346 ;; add the ".C" entry first.
1347 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(cc\\|hh\\)\\'" . c++-mode))
1348 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.[ch]\\(pp\\|xx\\|\\+\\+\\)\\'" . c++-mode))
1349 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(CC?\\|HH?\\)\\'" . c++-mode))
1351 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.[ch]\\'" . c-mode))
1353 ;; NB: The following two associate yacc and lex files to C Mode, which
1354 ;; is not really suitable for those formats. Anyway, afaik there's
1355 ;; currently no better mode for them, and besides this is legacy.
1356 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.y\\(acc\\)?\\'" . c-mode))
1357 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.lex\\'" . c-mode))
1359 ;; Preprocessed files generated by C and C++ compilers.
1360 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.i\\'" . c-mode))
1361 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.ii\\'" . c++-mode))
1363 (unless (fboundp 'prog-mode) (defalias 'prog-mode 'fundamental-mode))
1365 ;;;###autoload
1366 (define-derived-mode c-mode prog-mode "C"
1367 "Major mode for editing K&R and ANSI C code.
1368 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1369 c-mode buffer. This automatically sets up a mail buffer with version
1370 information already added. You just need to add a description of the
1371 problem, including a reproducible test case, and send the message.
1373 To see what version of CC Mode you are running, enter `\\[c-version]'.
1375 The hook `c-mode-common-hook' is run with no args at mode
1376 initialization, then `c-mode-hook'.
1378 Key bindings:
1379 \\{c-mode-map}"
1380 (c-initialize-cc-mode t)
1381 (set-syntax-table c-mode-syntax-table)
1382 (setq local-abbrev-table c-mode-abbrev-table
1383 abbrev-mode t)
1384 (use-local-map c-mode-map)
1385 (c-init-language-vars-for 'c-mode)
1386 (c-make-macro-with-semi-re) ; matches macro names whose expansion ends with ;
1387 (c-common-init 'c-mode)
1388 (easy-menu-add c-c-menu)
1389 (cc-imenu-init cc-imenu-c-generic-expression)
1390 (c-run-mode-hooks 'c-mode-common-hook 'c-mode-hook)
1391 (c-update-modeline))
1394 ;; Support for C++
1396 (defvar c++-mode-syntax-table
1397 (funcall (c-lang-const c-make-mode-syntax-table c++))
1398 "Syntax table used in c++-mode buffers.")
1400 (c-define-abbrev-table 'c++-mode-abbrev-table
1401 '(("else" "else" c-electric-continued-statement 0)
1402 ("while" "while" c-electric-continued-statement 0)
1403 ("catch" "catch" c-electric-continued-statement 0))
1404 "Abbreviation table used in c++-mode buffers.")
1406 (defvar c++-mode-map
1407 (let ((map (c-make-inherited-keymap)))
1408 ;; Add bindings which are only useful for C++.
1409 (define-key map "\C-c\C-e" 'c-macro-expand)
1410 (define-key map "\C-c:" 'c-scope-operator)
1411 (define-key map "<" 'c-electric-lt-gt)
1412 (define-key map ">" 'c-electric-lt-gt)
1413 map)
1414 "Keymap used in c++-mode buffers.")
1416 (easy-menu-define c-c++-menu c++-mode-map "C++ Mode Commands"
1417 (cons "C++" (c-lang-const c-mode-menu c++)))
1419 ;;;###autoload
1420 (define-derived-mode c++-mode prog-mode "C++"
1421 "Major mode for editing C++ code.
1422 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1423 c++-mode buffer. This automatically sets up a mail buffer with
1424 version information already added. You just need to add a description
1425 of the problem, including a reproducible test case, and send the
1426 message.
1428 To see what version of CC Mode you are running, enter `\\[c-version]'.
1430 The hook `c-mode-common-hook' is run with no args at mode
1431 initialization, then `c++-mode-hook'.
1433 Key bindings:
1434 \\{c++-mode-map}"
1435 (c-initialize-cc-mode t)
1436 (set-syntax-table c++-mode-syntax-table)
1437 (setq local-abbrev-table c++-mode-abbrev-table
1438 abbrev-mode t)
1439 (use-local-map c++-mode-map)
1440 (c-init-language-vars-for 'c++-mode)
1441 (c-make-macro-with-semi-re) ; matches macro names whose expansion ends with ;
1442 (c-common-init 'c++-mode)
1443 (easy-menu-add c-c++-menu)
1444 (cc-imenu-init cc-imenu-c++-generic-expression)
1445 (c-run-mode-hooks 'c-mode-common-hook 'c++-mode-hook)
1446 (c-update-modeline))
1449 ;; Support for Objective-C
1451 (defvar objc-mode-syntax-table
1452 (funcall (c-lang-const c-make-mode-syntax-table objc))
1453 "Syntax table used in objc-mode buffers.")
1455 (c-define-abbrev-table 'objc-mode-abbrev-table
1456 '(("else" "else" c-electric-continued-statement 0)
1457 ("while" "while" c-electric-continued-statement 0))
1458 "Abbreviation table used in objc-mode buffers.")
1460 (defvar objc-mode-map
1461 (let ((map (c-make-inherited-keymap)))
1462 ;; Add bindings which are only useful for Objective-C.
1463 (define-key map "\C-c\C-e" 'c-macro-expand)
1464 map)
1465 "Keymap used in objc-mode buffers.")
1467 (easy-menu-define c-objc-menu objc-mode-map "ObjC Mode Commands"
1468 (cons "ObjC" (c-lang-const c-mode-menu objc)))
1470 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.m\\'" . objc-mode))
1472 ;;;###autoload
1473 (define-derived-mode objc-mode prog-mode "ObjC"
1474 "Major mode for editing Objective C code.
1475 To submit a problem report, enter `\\[c-submit-bug-report]' from an
1476 objc-mode buffer. This automatically sets up a mail buffer with
1477 version information already added. You just need to add a description
1478 of the problem, including a reproducible test case, and send the
1479 message.
1481 To see what version of CC Mode you are running, enter `\\[c-version]'.
1483 The hook `c-mode-common-hook' is run with no args at mode
1484 initialization, then `objc-mode-hook'.
1486 Key bindings:
1487 \\{objc-mode-map}"
1488 (c-initialize-cc-mode t)
1489 (set-syntax-table objc-mode-syntax-table)
1490 (setq local-abbrev-table objc-mode-abbrev-table
1491 abbrev-mode t)
1492 (use-local-map objc-mode-map)
1493 (c-init-language-vars-for 'objc-mode)
1494 (c-make-macro-with-semi-re) ; matches macro names whose expansion ends with ;
1495 (c-common-init 'objc-mode)
1496 (easy-menu-add c-objc-menu)
1497 (cc-imenu-init nil 'cc-imenu-objc-function)
1498 (c-run-mode-hooks 'c-mode-common-hook 'objc-mode-hook)
1499 (c-update-modeline))
1502 ;; Support for Java
1504 (defvar java-mode-syntax-table
1505 (funcall (c-lang-const c-make-mode-syntax-table java))
1506 "Syntax table used in java-mode buffers.")
1508 (c-define-abbrev-table 'java-mode-abbrev-table
1509 '(("else" "else" c-electric-continued-statement 0)
1510 ("while" "while" c-electric-continued-statement 0)
1511 ("catch" "catch" c-electric-continued-statement 0)
1512 ("finally" "finally" c-electric-continued-statement 0))
1513 "Abbreviation table used in java-mode buffers.")
1515 (defvar java-mode-map
1516 (let ((map (c-make-inherited-keymap)))
1517 ;; Add bindings which are only useful for Java.
1518 map)
1519 "Keymap used in java-mode buffers.")
1521 ;; Regexp trying to describe the beginning of a Java top-level
1522 ;; definition. This is not used by CC Mode, nor is it maintained
1523 ;; since it's practically impossible to write a regexp that reliably
1524 ;; matches such a construct. Other tools are necessary.
1525 (defconst c-Java-defun-prompt-regexp
1526 "^[ \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-*")
1528 (easy-menu-define c-java-menu java-mode-map "Java Mode Commands"
1529 (cons "Java" (c-lang-const c-mode-menu java)))
1531 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.java\\'" . java-mode))
1533 ;;;###autoload
1534 (define-derived-mode java-mode prog-mode "Java"
1535 "Major mode for editing Java code.
1536 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1537 java-mode buffer. This automatically sets up a mail buffer with
1538 version information already added. You just need to add a description
1539 of the problem, including a reproducible test case, and send the
1540 message.
1542 To see what version of CC Mode you are running, enter `\\[c-version]'.
1544 The hook `c-mode-common-hook' is run with no args at mode
1545 initialization, then `java-mode-hook'.
1547 Key bindings:
1548 \\{java-mode-map}"
1549 (c-initialize-cc-mode t)
1550 (set-syntax-table java-mode-syntax-table)
1551 (setq local-abbrev-table java-mode-abbrev-table
1552 abbrev-mode t)
1553 (use-local-map java-mode-map)
1554 (c-init-language-vars-for 'java-mode)
1555 (c-common-init 'java-mode)
1556 (easy-menu-add c-java-menu)
1557 (cc-imenu-init cc-imenu-java-generic-expression)
1558 (c-run-mode-hooks 'c-mode-common-hook 'java-mode-hook)
1559 (c-update-modeline))
1562 ;; Support for CORBA's IDL language
1564 (defvar idl-mode-syntax-table
1565 (funcall (c-lang-const c-make-mode-syntax-table idl))
1566 "Syntax table used in idl-mode buffers.")
1568 (c-define-abbrev-table 'idl-mode-abbrev-table nil
1569 "Abbreviation table used in idl-mode buffers.")
1571 (defvar idl-mode-map
1572 (let ((map (c-make-inherited-keymap)))
1573 ;; Add bindings which are only useful for IDL.
1574 map)
1575 "Keymap used in idl-mode buffers.")
1577 (easy-menu-define c-idl-menu idl-mode-map "IDL Mode Commands"
1578 (cons "IDL" (c-lang-const c-mode-menu idl)))
1580 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.idl\\'" . idl-mode))
1582 ;;;###autoload
1583 (define-derived-mode idl-mode prog-mode "IDL"
1584 "Major mode for editing CORBA's IDL, PSDL and CIDL code.
1585 To submit a problem report, enter `\\[c-submit-bug-report]' from an
1586 idl-mode buffer. This automatically sets up a mail buffer with
1587 version information already added. You just need to add a description
1588 of the problem, including a reproducible test case, and send the
1589 message.
1591 To see what version of CC Mode you are running, enter `\\[c-version]'.
1593 The hook `c-mode-common-hook' is run with no args at mode
1594 initialization, then `idl-mode-hook'.
1596 Key bindings:
1597 \\{idl-mode-map}"
1598 (c-initialize-cc-mode t)
1599 (set-syntax-table idl-mode-syntax-table)
1600 (setq local-abbrev-table idl-mode-abbrev-table)
1601 (use-local-map idl-mode-map)
1602 (c-init-language-vars-for 'idl-mode)
1603 (c-common-init 'idl-mode)
1604 (easy-menu-add c-idl-menu)
1605 ;;(cc-imenu-init cc-imenu-idl-generic-expression) ;TODO
1606 (c-run-mode-hooks 'c-mode-common-hook 'idl-mode-hook)
1607 (c-update-modeline))
1610 ;; Support for Pike
1612 (defvar pike-mode-syntax-table
1613 (funcall (c-lang-const c-make-mode-syntax-table pike))
1614 "Syntax table used in pike-mode buffers.")
1616 (c-define-abbrev-table 'pike-mode-abbrev-table
1617 '(("else" "else" c-electric-continued-statement 0)
1618 ("while" "while" c-electric-continued-statement 0))
1619 "Abbreviation table used in pike-mode buffers.")
1621 (defvar pike-mode-map
1622 (let ((map (c-make-inherited-keymap)))
1623 ;; Additional bindings.
1624 (define-key map "\C-c\C-e" 'c-macro-expand)
1625 map)
1626 "Keymap used in pike-mode buffers.")
1628 (easy-menu-define c-pike-menu pike-mode-map "Pike Mode Commands"
1629 (cons "Pike" (c-lang-const c-mode-menu pike)))
1631 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(u?lpc\\|pike\\|pmod\\(\\.in\\)?\\)\\'" . pike-mode))
1632 ;;;###autoload (add-to-list 'interpreter-mode-alist '("pike" . pike-mode))
1634 ;;;###autoload
1635 (define-derived-mode pike-mode prog-mode "Pike"
1636 "Major mode for editing Pike code.
1637 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1638 pike-mode buffer. This automatically sets up a mail buffer with
1639 version information already added. You just need to add a description
1640 of the problem, including a reproducible test case, and send the
1641 message.
1643 To see what version of CC Mode you are running, enter `\\[c-version]'.
1645 The hook `c-mode-common-hook' is run with no args at mode
1646 initialization, then `pike-mode-hook'.
1648 Key bindings:
1649 \\{pike-mode-map}"
1650 (c-initialize-cc-mode t)
1651 (set-syntax-table pike-mode-syntax-table)
1652 (setq local-abbrev-table pike-mode-abbrev-table
1653 abbrev-mode t)
1654 (use-local-map pike-mode-map)
1655 (c-init-language-vars-for 'pike-mode)
1656 (c-common-init 'pike-mode)
1657 (easy-menu-add c-pike-menu)
1658 ;;(cc-imenu-init cc-imenu-pike-generic-expression) ;TODO
1659 (c-run-mode-hooks 'c-mode-common-hook 'pike-mode-hook)
1660 (c-update-modeline))
1663 ;; Support for AWK
1665 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.awk\\'" . awk-mode))
1666 ;;;###autoload (add-to-list 'interpreter-mode-alist '("awk" . awk-mode))
1667 ;;;###autoload (add-to-list 'interpreter-mode-alist '("mawk" . awk-mode))
1668 ;;;###autoload (add-to-list 'interpreter-mode-alist '("nawk" . awk-mode))
1669 ;;;###autoload (add-to-list 'interpreter-mode-alist '("gawk" . awk-mode))
1671 (c-define-abbrev-table 'awk-mode-abbrev-table
1672 '(("else" "else" c-electric-continued-statement 0)
1673 ("while" "while" c-electric-continued-statement 0))
1674 "Abbreviation table used in awk-mode buffers.")
1676 (defvar awk-mode-map
1677 (let ((map (c-make-inherited-keymap)))
1678 ;; Add bindings which are only useful for awk.
1679 (define-key map "#" 'self-insert-command)
1680 (define-key map "/" 'self-insert-command)
1681 (define-key map "*" 'self-insert-command)
1682 (define-key map "\C-c\C-n" 'undefined) ; #if doesn't exist in awk.
1683 (define-key map "\C-c\C-p" 'undefined)
1684 (define-key map "\C-c\C-u" 'undefined)
1685 (define-key map "\M-a" 'c-beginning-of-statement) ; 2003/10/7
1686 (define-key map "\M-e" 'c-end-of-statement) ; 2003/10/7
1687 (define-key map "\C-\M-a" 'c-awk-beginning-of-defun)
1688 (define-key map "\C-\M-e" 'c-awk-end-of-defun)
1689 map)
1690 "Keymap used in awk-mode buffers.")
1692 (easy-menu-define c-awk-menu awk-mode-map "AWK Mode Commands"
1693 (cons "AWK" (c-lang-const c-mode-menu awk)))
1695 ;; (require 'cc-awk) brings these in.
1696 (defvar awk-mode-syntax-table)
1697 (declare-function c-awk-unstick-NL-prop "cc-awk" ())
1699 ;;;###autoload
1700 (define-derived-mode awk-mode prog-mode "AWK"
1701 "Major mode for editing AWK code.
1702 To submit a problem report, enter `\\[c-submit-bug-report]' from an
1703 awk-mode buffer. This automatically sets up a mail buffer with version
1704 information already added. You just need to add a description of the
1705 problem, including a reproducible test case, and send the message.
1707 To see what version of CC Mode you are running, enter `\\[c-version]'.
1709 The hook `c-mode-common-hook' is run with no args at mode
1710 initialization, then `awk-mode-hook'.
1712 Key bindings:
1713 \\{awk-mode-map}"
1714 ;; We need the next line to stop the macro defining
1715 ;; `awk-mode-syntax-table'. This would mask the real table which is
1716 ;; declared in cc-awk.el and hasn't yet been loaded.
1717 :syntax-table nil
1718 (require 'cc-awk) ; Added 2003/6/10.
1719 (c-initialize-cc-mode t)
1720 (set-syntax-table awk-mode-syntax-table)
1721 (setq local-abbrev-table awk-mode-abbrev-table
1722 abbrev-mode t)
1723 (use-local-map awk-mode-map)
1724 (c-init-language-vars-for 'awk-mode)
1725 (c-common-init 'awk-mode)
1726 (c-awk-unstick-NL-prop)
1728 (c-run-mode-hooks 'c-mode-common-hook 'awk-mode-hook)
1729 (c-update-modeline))
1732 ;; bug reporting
1734 (defconst c-mode-help-address
1735 "bug-cc-mode@gnu.org"
1736 "Address(es) for CC Mode bug reports.")
1738 (defun c-version ()
1739 "Echo the current version of CC Mode in the minibuffer."
1740 (interactive)
1741 (message "Using CC Mode version %s" c-version)
1742 (c-keep-region-active))
1744 (define-obsolete-variable-alias 'c-prepare-bug-report-hooks
1745 'c-prepare-bug-report-hook "24.3")
1746 (defvar c-prepare-bug-report-hook nil)
1748 ;; Dynamic variables used by reporter.
1749 (defvar reporter-prompt-for-summary-p)
1750 (defvar reporter-dont-compact-list)
1752 (defun c-submit-bug-report ()
1753 "Submit via mail a bug report on CC Mode."
1754 (interactive)
1755 (require 'reporter)
1756 ;; load in reporter
1757 (let ((reporter-prompt-for-summary-p t)
1758 (reporter-dont-compact-list '(c-offsets-alist))
1759 (style c-indentation-style)
1760 (c-features c-emacs-features))
1761 (and
1762 (if (y-or-n-p "Do you want to submit a report on CC Mode? ")
1763 t (message "") nil)
1764 (reporter-submit-bug-report
1765 c-mode-help-address
1766 (concat "CC Mode " c-version " (" mode-name ")")
1767 (let ((vars (append
1768 c-style-variables
1769 '(c-buffer-is-cc-mode
1770 c-tab-always-indent
1771 c-syntactic-indentation
1772 c-syntactic-indentation-in-macros
1773 c-ignore-auto-fill
1774 c-auto-align-backslashes
1775 c-backspace-function
1776 c-delete-function
1777 c-electric-pound-behavior
1778 c-default-style
1779 c-enable-xemacs-performance-kludge-p
1780 c-old-style-variable-behavior
1781 defun-prompt-regexp
1782 tab-width
1783 comment-column
1784 parse-sexp-ignore-comments
1785 parse-sexp-lookup-properties
1786 lookup-syntax-properties
1787 ;; A brain-damaged XEmacs only variable that, if
1788 ;; set to nil can cause all kinds of chaos.
1789 signal-error-on-buffer-boundary
1790 ;; Variables that affect line breaking and comments.
1791 auto-fill-mode
1792 auto-fill-function
1793 filladapt-mode
1794 comment-multi-line
1795 comment-start-skip
1796 fill-prefix
1797 fill-column
1798 paragraph-start
1799 adaptive-fill-mode
1800 adaptive-fill-regexp)
1801 nil)))
1802 (mapc (lambda (var) (unless (boundp var)
1803 (setq vars (delq var vars))))
1804 '(signal-error-on-buffer-boundary
1805 filladapt-mode
1806 defun-prompt-regexp
1807 font-lock-mode
1808 auto-fill-mode
1809 font-lock-maximum-decoration
1810 parse-sexp-lookup-properties
1811 lookup-syntax-properties))
1812 vars)
1813 (lambda ()
1814 (run-hooks 'c-prepare-bug-report-hook)
1815 (insert (format "Buffer Style: %s\nc-emacs-features: %s\n"
1816 style c-features)))))))
1819 (cc-provide 'cc-mode)
1821 ;;; Local Variables:
1822 ;;; indent-tabs-mode: t
1823 ;;; tab-width: 8
1824 ;;; End:
1825 ;;; cc-mode.el ends here