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
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 set in c-after-change to
635 ;; after-change-functions' BEG and END, and may be modified by functions in
636 ;; `c-before-font-lock-functions'.
638 (make-variable-buffer-local 'c-new-BEG)
640 (make-variable-buffer-local 'c-new-END)
642 (defun c-common-init (&optional mode)
643 "Common initialization for all CC Mode modes.
644 In addition to the work done by `c-basic-common-init' and
645 `c-font-lock-init', this function sets up various other things as
646 customary in CC Mode modes but which aren't strictly necessary for CC
647 Mode to operate correctly.
649 MODE is the symbol for the mode to initialize, like `c-mode'. See
650 `c-basic-common-init' for details. It's only optional to be
651 compatible with old code; callers should always specify it."
654 ;; Called from an old third party package. The fallback is to
656 (c-init-language-vars-for 'c-mode))
658 (c-basic-common-init mode c-default-style)
660 ;; Only initialize font locking if we aren't called from an old package.
663 ;; Starting a mode is a sort of "change". So call the change functions...
666 (setq c-new-BEG (point-min))
667 (setq c-new-END (point-max))
669 (let (before-change-functions after-change-functions)
671 (funcall fn (point-min) (point-max)))
672 c-get-state-before-change-functions)
674 (funcall fn (point-min) (point-max)
675 (- (point-max) (point-min))))
676 c-before-font-lock-functions))))
678 (set (make-local-variable 'outline-regexp) "[^#\n\^M]")
679 (set (make-local-variable 'outline-level) 'c-outline-level)
680 (set (make-local-variable 'add-log-current-defun-function)
682 (or (c-cpp-define-name) (c-defun-name))))
683 (let ((rfn (assq mode c-require-final-newline)))
685 (if (boundp 'mode-require-final-newline)
687 (set (make-local-variable 'require-final-newline)
688 mode-require-final-newline))
689 (set (make-local-variable 'require-final-newline) (cdr rfn))))))
691 (defun c-count-cfss (lv-alist)
692 ;; LV-ALIST is an alist like `file-local-variables-alist'. Count how many
693 ;; elements with the key `c-file-style' there are in it.
694 (let ((elt-ptr lv-alist) elt (cownt 0))
696 (setq elt (car elt-ptr)
697 elt-ptr (cdr elt-ptr))
698 (when (eq (car elt) 'c-file-style)
699 (setq cownt (1+ cownt))))
702 (defun c-before-hack-hook ()
703 "Set the CC Mode style and \"offsets\" when in the buffer's local variables.
704 They are set only when, respectively, the pseudo variables
705 `c-file-style' and `c-file-offsets' are present in the list.
707 This function is called from the hook `before-hack-local-variables-hook'."
708 (when c-buffer-is-cc-mode
709 (let ((mode-cons (assq 'mode file-local-variables-alist))
710 (stile (cdr (assq 'c-file-style file-local-variables-alist)))
711 (offsets (cdr (assq 'c-file-offsets file-local-variables-alist))))
713 (hack-one-local-variable (car mode-cons) (cdr mode-cons))
714 (setq file-local-variables-alist
715 (delq mode-cons file-local-variables-alist)))
717 (or (stringp stile) (error "c-file-style is not a string"))
718 (if (boundp 'dir-local-variables-alist)
719 ;; Determine whether `c-file-style' was set in the file's local
720 ;; variables or in a .dir-locals.el (a directory setting).
721 (let ((cfs-in-file-and-dir-count
722 (c-count-cfss file-local-variables-alist))
723 (cfs-in-dir-count (c-count-cfss dir-local-variables-alist)))
725 (and (= cfs-in-file-and-dir-count cfs-in-dir-count)
727 (c-set-style stile)))
731 (let ((langelem (car langentry))
732 (offset (cdr langentry)))
733 (c-set-offset langelem offset)))
736 (defun c-remove-any-local-eval-or-mode-variables ()
737 ;; If the buffer specifies `mode' or `eval' in its File Local Variable list
738 ;; or on the first line, remove all occurrences. See
739 ;; `c-postprocess-file-styles' for justification. There is no need to save
740 ;; point here, or even bother too much about the buffer contents. However,
741 ;; DON'T mess up the kill-ring.
743 ;; Most of the code here is derived from Emacs 21.3's `hack-local-variables'
745 (goto-char (point-max))
746 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
747 (let (lv-point (prefix "") (suffix ""))
748 (when (let ((case-fold-search t))
749 (search-forward "Local Variables:" nil t))
750 (setq lv-point (point))
751 ;; The prefix is what comes before "local variables:" in its line.
752 ;; The suffix is what comes after "local variables:" in its line.
753 (skip-chars-forward " \t")
755 (setq suffix (buffer-substring (point)
756 (progn (end-of-line) (point)))))
757 (goto-char (match-beginning 0))
760 (buffer-substring (point)
761 (progn (beginning-of-line) (point)))))
763 (while (search-forward-regexp
765 (regexp-quote prefix)
766 "\\(mode\\|eval\\):.*"
767 (regexp-quote suffix)
771 (delete-region (point) (progn (forward-line) (point)))))
773 ;; Delete the first line, if we've got one, in case it contains a mode spec.
774 (unless (and lv-point
775 (progn (goto-char lv-point)
778 (goto-char (point-min))
780 (delete-region (point) (progn (forward-line) (point)))))))
782 (defun c-postprocess-file-styles ()
783 "Function that post processes relevant file local variables in CC Mode.
784 Currently, this function simply applies any style and offset settings
785 found in the file's Local Variable list. It first applies any style
786 setting found in `c-file-style', then it applies any offset settings
787 it finds in `c-file-offsets'.
789 Note that the style variables are always made local to the buffer."
791 ;; apply file styles and offsets
792 (when c-buffer-is-cc-mode
793 (if (or c-file-style c-file-offsets)
794 (c-make-styles-buffer-local t))
796 (or (stringp c-file-style)
797 (error "c-file-style is not a string"))
798 (c-set-style c-file-style))
803 (let ((langelem (car langentry))
804 (offset (cdr langentry)))
805 (c-set-offset langelem offset)))
807 ;; Problem: The file local variable block might have explicitly set a
808 ;; style variable. The `c-set-style' or `mapcar' call might have
809 ;; overwritten this. So we run `hack-local-variables' again to remedy
810 ;; this. There are no guarantees this will work properly, particularly as
811 ;; we have no control over what the other hook functions on
812 ;; `hack-local-variables-hook' would have done. We now (2006/2/1) remove
813 ;; any `eval' or `mode' expressions before we evaluate again (see below).
816 ;; Problem (bug reported by Gustav Broberg): if one of the variables is
817 ;; `mode', this will invoke c-mode (etc.) again, setting up the style etc.
818 ;; We prevent this by temporarily removing `mode' from the Local Variables
820 (if (or c-file-style c-file-offsets)
821 (let ((hack-local-variables-hook nil) (inhibit-read-only t))
822 (c-tentative-buffer-changes
823 (c-remove-any-local-eval-or-mode-variables)
824 (hack-local-variables))
827 (if (boundp 'before-hack-local-variables-hook)
828 (add-hook 'before-hack-local-variables-hook 'c-before-hack-hook)
829 (add-hook 'hack-local-variables-hook 'c-postprocess-file-styles))
831 (defmacro c-run-mode-hooks (&rest hooks)
832 ;; Emacs 21.1 has introduced a system with delayed mode hooks that
833 ;; requires the use of the new function `run-mode-hooks'.
834 (if (cc-bytecomp-fboundp 'run-mode-hooks)
835 `(run-mode-hooks ,@hooks)
836 `(progn ,@(mapcar (lambda (hook) `(run-hooks ,hook)) hooks))))
839 ;;; Change hooks, linking with Font Lock and electric-indent-mode.
841 ;; Buffer local variables recording Beginning/End-of-Macro position before a
842 ;; change, when a macro straddles, respectively, the BEG or END (or both) of
843 ;; the change region. Otherwise these have the values BEG/END.
845 (make-variable-buffer-local 'c-old-BOM)
847 (make-variable-buffer-local 'c-old-EOM)
849 (defun c-called-from-text-property-change-p ()
850 ;; Is the primitive which invoked `before-change-functions' or
851 ;; `after-change-functions' one which merely changes text properties? This
852 ;; function must be called directly from a member of one of the above hooks.
854 ;; In the following call, frame 0 is `backtrace-frame', frame 1 is
855 ;; `c-called-from-text-property-change-p', frame 2 is
856 ;; `c-before/after-change', frame 3 is the primitive invoking the change
858 (memq (cadr (backtrace-frame 3))
859 '(put-text-property remove-list-of-text-properties)))
861 (defun c-extend-region-for-CPP (beg end)
862 ;; Set c-old-BOM or c-old-EOM respectively to BEG, END, each extended to the
863 ;; beginning/end of any preprocessor construct they may be in.
865 ;; Point is undefined both before and after this function call; the buffer
866 ;; has already been widened, and match-data saved. The return value is
869 ;; This function is in the C/C++/ObjC values of
870 ;; `c-get-state-before-change-functions' and is called exclusively as a
871 ;; before change function.
873 (c-beginning-of-macro)
874 (setq c-old-BOM (point))
877 (when (c-beginning-of-macro)
879 (or (eobp) (forward-char))) ; Over the terminating NL which may be marked
880 ; with a c-cpp-delimiter category property
881 (setq c-old-EOM (point)))
883 (defun c-extend-font-lock-region-for-macros (begg endd &optional old-len)
884 ;; Extend the region (BEGG ENDD) to cover all (possibly changed)
885 ;; preprocessor macros; return the cons (new-BEG . new-END). OLD-LEN should
886 ;; be either the old length parameter when called from an
887 ;; after-change-function, or nil otherwise. This defun uses the variables
888 ;; c-old-BOM, c-new-BOM.
890 ;; Point is undefined on both entry and exit to this function. The buffer
891 ;; will have been widened on entry.
892 (let (limits new-beg new-end)
893 (goto-char c-old-BOM) ; already set to old start of macro or begg.
896 (if (setq limits (c-state-literal-at (point)))
897 (cdr limits) ; go forward out of any string or comment.
901 (if (setq limits (c-state-literal-at (point)))
902 (goto-char (car limits))) ; go backward out of any string or comment.
903 (if (c-beginning-of-macro)
905 (setq new-end (max endd
907 (+ (- c-old-EOM old-len) (- endd begg))
910 (cons new-beg new-end)))
912 (defun c-neutralize-CPP-line (beg end)
913 ;; BEG and END bound a region, typically a preprocessor line. Put a
914 ;; "punctuation" syntax-table property on syntactically obtrusive
915 ;; characters, ones which would interact syntactically with stuff outside
918 ;; These are unmatched string delimiters, or unmatched
919 ;; parens/brackets/braces. An unclosed comment is regarded as valid, NOT
925 (setq s (parse-partial-sexp beg end -1))
927 ((< (nth 0 s) 0) ; found an unmated ),},]
928 (c-put-char-property (1- (point)) 'syntax-table '(1))
930 ((nth 3 s) ; In a string
931 (c-put-char-property (nth 8 s) 'syntax-table '(1))
933 ((> (nth 0 s) 0) ; In a (,{,[
934 (c-put-char-property (nth 1 s) 'syntax-table '(1))
938 (defun c-neutralize-syntax-in-and-mark-CPP (begg endd old-len)
939 ;; (i) Extend the font lock region to cover all changed preprocessor
940 ;; regions; it does this by setting the variables `c-new-BEG' and
941 ;; `c-new-END' to the new boundaries.
943 ;; (ii) "Neutralize" every preprocessor line wholly or partially in the
944 ;; extended changed region. "Restore" lines which were CPP lines before the
945 ;; change and are no longer so; these can be located from the Buffer local
946 ;; variables `c-old-BOM' and `c-old-EOM'.
948 ;; (iii) Mark every CPP construct by placing a `category' property value
949 ;; `c-cpp-delimiter' at its start and end. The marked characters are the
950 ;; opening # and usually the terminating EOL, but sometimes the character
951 ;; before a comment/string delimiter.
953 ;; That is, set syntax-table properties on characters that would otherwise
954 ;; interact syntactically with those outside the CPP line(s).
956 ;; This function is called from an after-change function, BEGG ENDD and
957 ;; OLD-LEN being the standard parameters. It prepares the buffer for font
958 ;; locking, hence must get called before `font-lock-after-change-function'.
960 ;; Point is undefined both before and after this function call, the buffer
961 ;; has been widened, and match-data saved. The return value is ignored.
963 ;; This function is in the C/C++/ObjC value of `c-before-font-lock-functions'.
965 ;; Note: SPEED _MATTERS_ IN THIS FUNCTION!!!
967 ;; This function might make hidden buffer changes.
968 (c-save-buffer-state (new-bounds)
969 ;; First determine the region, (c-new-BEG c-new-END), which will get font
970 ;; locked. It might need "neutralizing". This region may not start
971 ;; inside a string, comment, or macro.
972 (setq new-bounds (c-extend-font-lock-region-for-macros
973 c-new-BEG c-new-END old-len))
974 (setq c-new-BEG (max (car new-bounds) (c-determine-limit 500 begg))
975 c-new-END (min (cdr new-bounds) (c-determine-+ve-limit 500 endd)))
976 ;; Clear all old relevant properties.
977 (c-clear-char-property-with-value c-new-BEG c-new-END 'syntax-table '(1))
979 ;; CPP "comment" markers:
980 (if (eval-when-compile (memq 'category-properties c-emacs-features));Emacs.
981 (c-clear-char-property-with-value
982 c-new-BEG c-new-END 'category 'c-cpp-delimiter))
983 ;; FIXME!!! What about the "<" and ">" category properties? 2009-11-16
985 ;; Add needed properties to each CPP construct in the region.
986 (goto-char c-new-BEG)
987 (skip-chars-backward " \t")
988 (let ((pps-position (point)) pps-state mbeg)
989 (while (and (< (point) c-new-END)
990 (search-forward-regexp c-anchored-cpp-prefix c-new-END t))
991 ;; If we've found a "#" inside a macro/string/comment, ignore it.
994 (goto-char (match-beginning 0))
995 (let ((here (point)))
996 (and (save-match-data (c-beginning-of-macro))
1000 (parse-partial-sexp pps-position (point) nil nil pps-state)
1001 pps-position (point))
1002 (or (nth 3 pps-state) ; in a string?
1003 (nth 4 pps-state)))) ; in a comment?
1004 (goto-char (match-beginning 1))
1006 (if (> (c-syntactic-end-of-macro) mbeg)
1008 (c-neutralize-CPP-line mbeg (point)) ; "punctuation" properties
1009 (if (eval-when-compile
1010 (memq 'category-properties c-emacs-features)) ;Emacs.
1011 (c-set-cpp-delimiters mbeg (point)))) ; "comment" markers
1012 (forward-line)) ; no infinite loop with, e.g., "#//"
1015 (defun c-before-change (beg end)
1016 ;; Function to be put on `before-change-functions'. Primarily, this calls
1017 ;; the language dependent `c-get-state-before-change-functions'. It is
1018 ;; otherwise used only to remove stale entries from the `c-found-types'
1019 ;; cache, and to record entries which a `c-after-change' function might
1020 ;; confirm as stale.
1022 ;; Note that this function must be FAST rather than accurate. Note
1023 ;; also that it only has any effect when font locking is enabled.
1024 ;; We exploit this by checking for font-lock-*-face instead of doing
1025 ;; rigorous syntactic analysis.
1027 ;; If either change boundary is wholly inside an identifier, delete
1028 ;; it/them from the cache. Don't worry about being inside a string
1029 ;; or a comment - "wrongly" removing a symbol from `c-found-types'
1031 (unless (or (c-called-from-text-property-change-p)
1032 c-just-done-before-change) ; guard against a spurious second
1033 ; invocation of before-change-functions.
1034 (setq c-just-done-before-change t)
1035 (setq c-maybe-stale-found-type nil)
1040 ;; Are we inserting/deleting stuff in the middle of an identifier?
1041 (c-unfind-enclosing-token beg)
1042 (c-unfind-enclosing-token end)
1043 ;; Are we coalescing two tokens together, e.g. "fo o" -> "foo"?
1045 (c-unfind-coalesced-tokens beg end))
1046 ;; Are we (potentially) disrupting the syntactic context which
1047 ;; makes a type a type? E.g. by inserting stuff after "foo" in
1048 ;; "foo bar;", or before "foo" in "typedef foo *bar;"?
1050 ;; We search for appropriate c-type properties "near" the change.
1051 ;; First, find an appropriate boundary for this property search.
1056 (or (and (eq (get-text-property end 'face)
1057 'font-lock-comment-face)
1058 (previous-single-property-change end 'face))
1060 (when (>= end1 beg) ; Don't hassle about changes entirely in comments.
1061 ;; Find a limit for the search for a `c-type' property
1063 (and (/= (skip-chars-backward "^;{}") 0)
1064 (> (point) (point-min))
1065 (memq (c-get-char-property (1- (point)) 'face)
1066 '(font-lock-comment-face font-lock-string-face))))
1067 (setq lim (max (point-min) (1- (point))))
1069 ;; Look for the latest `c-type' property before end1
1070 (when (and (> end1 (point-min))
1072 (if (get-text-property (1- end1) 'c-type)
1074 (previous-single-property-change end1 'c-type
1076 (setq type (get-text-property (max (1- type-pos) lim) 'c-type))
1078 (when (memq type '(c-decl-id-start c-decl-type-start))
1079 ;; Get the identifier, if any, that the property is on.
1080 (goto-char (1- type-pos))
1082 (when (looking-at "\\(\\sw\\|\\s_\\)")
1083 (c-beginning-of-current-token)
1084 (buffer-substring-no-properties (point) type-pos)))
1087 (skip-chars-forward "^;{}") ;FIXME!!! loop for comment, maybe
1090 (or (c-next-single-property-change end 'c-type nil lim)
1092 (setq c-maybe-stale-found-type
1093 (list type marked-id
1095 (buffer-substring-no-properties type-pos
1097 (buffer-substring-no-properties beg end)))))))
1099 (if c-get-state-before-change-functions
1101 (funcall fn beg end))
1102 c-get-state-before-change-functions))
1104 ;; The following must be done here rather than in `c-after-change' because
1105 ;; newly inserted parens would foul up the invalidation algorithm.
1106 (c-invalidate-state-cache beg)))
1108 (defvar c-in-after-change-fontification nil)
1109 (make-variable-buffer-local 'c-in-after-change-fontification)
1110 ;; A flag to prevent region expanding stuff being done twice for after-change
1113 (defun c-after-change (beg end old-len)
1114 ;; Function put on `after-change-functions' to adjust various caches
1115 ;; etc. Prefer speed to finesse here, since there will be an order
1116 ;; of magnitude more calls to this function than any of the
1117 ;; functions that use the caches.
1119 ;; Note that care must be taken so that this is called before any
1120 ;; font-lock callbacks since we might get calls to functions using
1121 ;; these caches from inside them, and we must thus be sure that this
1122 ;; has already been executed.
1124 ;; This calls the language variable c-before-font-lock-functions, if non nil.
1125 ;; This typically sets `syntax-table' properties.
1127 ;; (c-new-BEG c-new-END) will be the region to fontify. It may become
1128 ;; larger than (beg end).
1129 (setq c-new-BEG beg c-new-END end)
1131 (unless (c-called-from-text-property-change-p)
1132 (setq c-just-done-before-change nil)
1133 (c-save-buffer-state (case-fold-search)
1134 ;; When `combine-after-change-calls' is used we might get calls
1135 ;; with regions outside the current narrowing. This has been
1136 ;; observed in Emacs 20.7.
1138 (save-match-data ; c-recognize-<>-arglists changes match-data
1141 (when (> end (point-max))
1142 ;; Some emacsen might return positions past the end. This has been
1143 ;; observed in Emacs 20.7 when rereading a buffer changed on disk
1144 ;; (haven't been able to minimize it, but Emacs 21.3 appears to
1146 (setq end (point-max))
1150 ;; C-y is capable of spuriously converting category properties
1151 ;; c-</>-as-paren-syntax and c-cpp-delimiter into hard syntax-table
1152 ;; properties. Remove these when it happens.
1153 (when (eval-when-compile (memq 'category-properties c-emacs-features))
1154 (c-save-buffer-state ()
1155 (c-clear-char-property-with-value beg end 'syntax-table
1156 c-<-as-paren-syntax)
1157 (c-clear-char-property-with-value beg end 'syntax-table
1158 c->-as-paren-syntax)
1159 (c-clear-char-property-with-value beg end 'syntax-table nil)))
1161 (c-trim-found-types beg end old-len) ; maybe we don't need all of these.
1162 (c-invalidate-sws-region-after beg end)
1163 ;; (c-invalidate-state-cache beg) ; moved to `c-before-change'.
1164 (c-invalidate-find-decl-cache beg)
1166 (when c-recognize-<>-arglists
1167 (c-after-change-check-<>-operators beg end))
1169 (setq c-in-after-change-fontification t)
1172 (funcall fn beg end old-len))
1173 c-before-font-lock-functions)))))))
1175 (defun c-fl-decl-start (pos)
1176 ;; If the beginning of the line containing POS is in the middle of a "local"
1177 ;; declaration (i.e. one which does not start outside of braces enclosing
1178 ;; POS, such as a struct), return the beginning of that declaration.
1179 ;; Otherwise return nil. Note that declarations, in this sense, can be
1182 ;; This function is called indirectly from font locking stuff - either from
1183 ;; c-after-change (to prepare for after-change font-locking) or from font
1184 ;; lock context (etc.) fontification.
1185 (let ((lit-limits (c-literal-limits))
1188 (goto-char (c-point 'bol new-pos))
1189 (when lit-limits ; Comment or string.
1190 (goto-char (car lit-limits)))
1191 (setq bod-lim (c-determine-limit 500))
1194 ;; Go to a less nested declaration each time round this loop.
1196 (eq (car (c-beginning-of-decl-1 bod-lim)) 'same)
1198 (progn (setq bo-decl (point))
1199 ;; Are we looking at a keyword such as "template" or
1200 ;; "typedef" which can decorate a type, or the type itself?
1201 (when (or (looking-at c-prefix-spec-kwds-re)
1203 ;; We've found another candidate position.
1204 (setq new-pos (min new-pos bo-decl))
1205 (goto-char bo-decl))
1207 ;; Try and go out a level to search again.
1209 (c-backward-syntactic-ws bod-lim)
1210 (and (> (point) bod-lim)
1211 (or (memq (char-before) '(?\( ?\[))
1212 (and (eq (char-before) ?\<)
1213 (eq (c-get-char-property
1214 (1- (point)) 'syntax-table)
1215 c-<-as-paren-syntax)))))
1217 (backward-char)) ; back over (, [, <.
1218 (and (/= new-pos pos) new-pos)))
1220 (defun c-change-expand-fl-region (beg end old-len)
1221 ;; Expand the region (c-new-BEG c-new-END) to an after-change font-lock
1222 ;; region. This will usually be the smallest sequence of whole lines
1223 ;; containing `c-new-BEG' and `c-new-END', but if `c-new-BEG' is in a
1224 ;; "local" declaration (see `c-fl-decl-start') the beginning of this is used
1225 ;; as the lower bound.
1227 ;; This is called from an after-change-function, but the parameters BEG END
1228 ;; and OLD-LEN are not used.
1231 (or (c-fl-decl-start c-new-BEG) (c-point 'bol c-new-BEG))
1232 c-new-END (c-point 'bonl c-new-END))))
1234 (defun c-context-expand-fl-region (beg end)
1235 ;; Return a cons (NEW-BEG . NEW-END), where NEW-BEG is the beginning of a
1236 ;; "local" declaration containing BEG (see `c-fl-decl-start') or BOL BEG is
1237 ;; in. NEW-END is beginning of the line after the one END is in.
1238 (cons (or (c-fl-decl-start beg) (c-point 'bol beg))
1239 (c-point 'bonl end)))
1241 (defun c-before-context-fl-expand-region (beg end)
1242 ;; Expand the region (BEG END) as specified by
1243 ;; `c-before-context-fontification-functions'. Return a cons of the bounds
1244 ;; of the new region.
1248 (let ((new-beg beg) (new-end end) new-region)
1250 (setq new-region (funcall fn new-beg new-end))
1251 (setq new-beg (car new-region) new-end (cdr new-region)))
1252 c-before-context-fontification-functions)
1255 (defun c-font-lock-fontify-region (beg end &optional verbose)
1256 ;; Effectively advice around `font-lock-fontify-region' which extends the
1257 ;; region (BEG END), for example, to avoid context fontification chopping
1258 ;; off the start of the context. Do not extend the region if it's already
1259 ;; been done (i.e. from an after-change fontification. An example (C++)
1260 ;; where the chopping off used to happen is this:
1262 ;; template <typename T>
1265 ;; void myfunc(T* p) {}
1267 ;; Type a space in the first blank line, and the fontification of the next
1268 ;; line was fouled up by context fontification.
1269 (let (new-beg new-end new-region case-fold-search)
1270 (if (and c-in-after-change-fontification
1271 (< beg c-new-END) (> end c-new-BEG))
1272 ;; Region and the latest after-change fontification region overlap.
1273 ;; Determine the upper and lower bounds of our adjusted region
1276 (if (<= beg c-new-BEG)
1277 (setq c-in-after-change-fontification nil))
1279 (if (and (>= beg (c-point 'bol c-new-BEG))
1281 ;; Either jit-lock has accepted `c-new-BEG', or has
1282 ;; (probably) extended the change region spuriously to
1283 ;; BOL, which position likely has a syntactically
1284 ;; different position. To ensure correct fontification,
1285 ;; we start at `c-new-BEG', assuming any characters to the
1286 ;; left of `c-new-BEG' on the line do not require
1289 (setq new-region (c-before-context-fl-expand-region beg end)
1290 new-end (cdr new-region))
1293 (if (and (>= end (c-point 'bol c-new-END))
1297 (cdr (c-before-context-fl-expand-region beg end))))))
1298 ;; Context (etc.) fontification.
1299 (setq new-region (c-before-context-fl-expand-region beg end)
1300 new-beg (car new-region) new-end (cdr new-region)))
1301 (funcall (default-value 'font-lock-fontify-region-function)
1302 new-beg new-end verbose)))
1304 (defun c-after-font-lock-init ()
1305 ;; Put on `font-lock-mode-hook'. This function ensures our after-change
1306 ;; function will get executed before the font-lock one.
1307 (when (memq #'c-after-change after-change-functions)
1308 (remove-hook 'after-change-functions #'c-after-change t)
1309 (add-hook 'after-change-functions #'c-after-change nil t)))
1311 (defun c-font-lock-init ()
1312 "Set up the font-lock variables for using the font-lock support in CC Mode.
1313 This does not load the font-lock package. Use after
1314 `c-basic-common-init' and after cc-fonts has been loaded.
1315 This function is called from `c-common-init', once per mode initialization."
1317 (set (make-local-variable 'font-lock-defaults)
1318 `(,(if (c-major-mode-is 'awk-mode)
1319 ;; awk-mode currently has only one font lock level.
1320 'awk-font-lock-keywords
1321 (mapcar 'c-mode-symbol
1322 '("font-lock-keywords" "font-lock-keywords-1"
1323 "font-lock-keywords-2" "font-lock-keywords-3")))
1325 ,c-identifier-syntax-modifications
1326 c-beginning-of-syntax
1327 (font-lock-mark-block-function
1328 . c-mark-function)))
1330 ;; Prevent `font-lock-default-fontify-region' extending the region it will
1331 ;; fontify to whole lines by removing `font-lock-extend-region-whole-lines'
1332 ;; (and, coincidentally, `font-lock-extend-region-multiline' (which we do
1333 ;; not need)) from `font-lock-extend-region-functions'. (Emacs only). This
1334 ;; fixes Emacs bug #19669.
1335 (when (boundp 'font-lock-extend-region-functions)
1336 (setq font-lock-extend-region-functions nil))
1338 (make-local-variable 'font-lock-fontify-region-function)
1339 (setq font-lock-fontify-region-function 'c-font-lock-fontify-region)
1341 (if (featurep 'xemacs)
1342 (make-local-hook 'font-lock-mode-hook))
1343 (add-hook 'font-lock-mode-hook 'c-after-font-lock-init nil t))
1345 ;; Emacs 22 and later.
1346 (defun c-extend-after-change-region (beg end _old-len)
1347 "Extend the region to be fontified, if necessary."
1348 ;; Note: the parameter OLD-LEN is ignored here. This somewhat indirect
1349 ;; implementation exists because it is minimally different from the
1350 ;; stand-alone CC Mode which, lacking
1351 ;; font-lock-extend-after-change-region-function, is forced to use advice
1354 ;; Of the seven CC Mode languages, currently (2009-05) only C, C++, Objc
1355 ;; (the languages with #define) and AWK Mode make non-null use of this
1357 (when (eq font-lock-support-mode 'jit-lock-mode)
1360 (c-save-buffer-state () ; Protect the undo-list from put-text-property.
1361 (if (< c-new-BEG beg)
1362 (put-text-property c-new-BEG beg 'fontified nil))
1363 (if (> c-new-END end)
1364 (put-text-property end c-new-END 'fontified nil)))))
1365 (cons c-new-BEG c-new-END))
1367 ;; Emacs < 22 and XEmacs
1368 (defmacro c-advise-fl-for-region (function)
1369 `(defadvice ,function (before get-awk-region activate)
1370 ;; Make sure that any string/regexp is completely font-locked.
1371 (when c-buffer-is-cc-mode
1373 (ad-set-arg 1 c-new-END) ; end
1374 (ad-set-arg 0 c-new-BEG))))) ; beg
1376 (unless (boundp 'font-lock-extend-after-change-region-function)
1377 (c-advise-fl-for-region font-lock-after-change-function)
1378 (c-advise-fl-for-region jit-lock-after-change)
1379 (c-advise-fl-for-region lazy-lock-defer-rest-after-change)
1380 (c-advise-fl-for-region lazy-lock-defer-line-after-change))
1382 ;; Connect up to `electric-indent-mode' (Emacs 24.4 and later).
1383 (defun c-electric-indent-mode-hook ()
1384 ;; Emacs has en/disabled `electric-indent-mode'. Propagate this through to
1385 ;; each CC Mode buffer.
1387 (with-current-buffer buf
1388 (when c-buffer-is-cc-mode
1389 ;; Don't use `c-toggle-electric-state' here due to recursion.
1390 (setq c-electric-flag electric-indent-mode)
1391 (c-update-modeline))))
1394 (defun c-electric-indent-local-mode-hook ()
1395 ;; Emacs has en/disabled `electric-indent-local-mode' for this buffer.
1396 ;; Propagate this through to this buffer's value of `c-electric-flag'
1397 (when c-buffer-is-cc-mode
1398 (setq c-electric-flag electric-indent-mode)
1399 (c-update-modeline)))
1404 (defvar c-mode-syntax-table
1405 (funcall (c-lang-const c-make-mode-syntax-table c))
1406 "Syntax table used in c-mode buffers.")
1408 (c-define-abbrev-table 'c-mode-abbrev-table
1409 '(("else" "else" c-electric-continued-statement 0)
1410 ("while" "while" c-electric-continued-statement 0))
1411 "Abbreviation table used in c-mode buffers.")
1414 (let ((map (c-make-inherited-keymap)))
1415 ;; Add bindings which are only useful for C.
1416 (define-key map "\C-c\C-e" 'c-macro-expand)
1418 "Keymap used in c-mode buffers.")
1421 (easy-menu-define c-c-menu c-mode-map "C Mode Commands"
1422 (cons "C" (c-lang-const c-mode-menu c)))
1424 ;; In XEmacs >= 21.5 modes should add their own entries to
1425 ;; `auto-mode-alist'. The comment form of autoload is used to avoid
1426 ;; doing this on load. That since `add-to-list' prepends the value
1427 ;; which could cause it to clobber user settings. Later emacsen have
1428 ;; an append option, but it's not safe to use.
1430 ;; The extension ".C" is associated with C++ while the lowercase
1431 ;; variant goes with C. On case insensitive file systems, this means
1432 ;; that ".c" files also might open C++ mode if the C++ entry comes
1433 ;; first on `auto-mode-alist'. Thus we try to ensure that ".C" comes
1434 ;; after ".c", and since `add-to-list' adds the entry first we have to
1435 ;; add the ".C" entry first.
1436 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(cc\\|hh\\)\\'" . c++-mode))
1437 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.[ch]\\(pp\\|xx\\|\\+\\+\\)\\'" . c++-mode))
1438 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(CC?\\|HH?\\)\\'" . c++-mode))
1440 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.[ch]\\'" . c-mode))
1442 ;; NB: The following two associate yacc and lex files to C Mode, which
1443 ;; is not really suitable for those formats. Anyway, afaik there's
1444 ;; currently no better mode for them, and besides this is legacy.
1445 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.y\\(acc\\)?\\'" . c-mode))
1446 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.lex\\'" . c-mode))
1448 ;; Preprocessed files generated by C and C++ compilers.
1449 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.i\\'" . c-mode))
1450 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.ii\\'" . c++-mode))
1452 (unless (fboundp 'prog-mode) (defalias 'prog-mode 'fundamental-mode))
1455 (define-derived-mode c-mode prog-mode "C"
1456 "Major mode for editing C code.
1458 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1459 c-mode buffer. This automatically sets up a mail buffer with version
1460 information already added. You just need to add a description of the
1461 problem, including a reproducible test case, and send the message.
1463 To see what version of CC Mode you are running, enter `\\[c-version]'.
1465 The hook `c-mode-common-hook' is run with no args at mode
1466 initialization, then `c-mode-hook'.
1470 (c-initialize-cc-mode t)
1471 (set-syntax-table c-mode-syntax-table)
1472 (setq local-abbrev-table c-mode-abbrev-table
1474 (use-local-map c-mode-map)
1475 (c-init-language-vars-for 'c-mode)
1476 (c-make-macro-with-semi-re) ; matches macro names whose expansion ends with ;
1477 (c-common-init 'c-mode)
1478 (easy-menu-add c-c-menu)
1479 (cc-imenu-init cc-imenu-c-generic-expression)
1480 (c-run-mode-hooks 'c-mode-common-hook 'c-mode-hook)
1481 (c-update-modeline))
1486 (defvar c++-mode-syntax-table
1487 (funcall (c-lang-const c-make-mode-syntax-table c++))
1488 "Syntax table used in c++-mode buffers.")
1490 (c-define-abbrev-table 'c++-mode-abbrev-table
1491 '(("else" "else" c-electric-continued-statement 0)
1492 ("while" "while" c-electric-continued-statement 0)
1493 ("catch" "catch" c-electric-continued-statement 0))
1494 "Abbreviation table used in c++-mode buffers.")
1496 (defvar c++-mode-map
1497 (let ((map (c-make-inherited-keymap)))
1498 ;; Add bindings which are only useful for C++.
1499 (define-key map "\C-c\C-e" 'c-macro-expand)
1500 (define-key map "\C-c:" 'c-scope-operator)
1501 (define-key map "<" 'c-electric-lt-gt)
1502 (define-key map ">" 'c-electric-lt-gt)
1504 "Keymap used in c++-mode buffers.")
1506 (easy-menu-define c-c++-menu c++-mode-map "C++ Mode Commands"
1507 (cons "C++" (c-lang-const c-mode-menu c++)))
1510 (define-derived-mode c++-mode prog-mode "C++"
1511 "Major mode for editing C++ code.
1512 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1513 c++-mode buffer. This automatically sets up a mail buffer with
1514 version information already added. You just need to add a description
1515 of the problem, including a reproducible test case, and send the
1518 To see what version of CC Mode you are running, enter `\\[c-version]'.
1520 The hook `c-mode-common-hook' is run with no args at mode
1521 initialization, then `c++-mode-hook'.
1525 (c-initialize-cc-mode t)
1526 (set-syntax-table c++-mode-syntax-table)
1527 (setq local-abbrev-table c++-mode-abbrev-table
1529 (use-local-map c++-mode-map)
1530 (c-init-language-vars-for 'c++-mode)
1531 (c-make-macro-with-semi-re) ; matches macro names whose expansion ends with ;
1532 (c-common-init 'c++-mode)
1533 (easy-menu-add c-c++-menu)
1534 (cc-imenu-init cc-imenu-c++-generic-expression)
1535 (c-run-mode-hooks 'c-mode-common-hook 'c++-mode-hook)
1536 (c-update-modeline))
1539 ;; Support for Objective-C
1541 (defvar objc-mode-syntax-table
1542 (funcall (c-lang-const c-make-mode-syntax-table objc))
1543 "Syntax table used in objc-mode buffers.")
1545 (c-define-abbrev-table 'objc-mode-abbrev-table
1546 '(("else" "else" c-electric-continued-statement 0)
1547 ("while" "while" c-electric-continued-statement 0))
1548 "Abbreviation table used in objc-mode buffers.")
1550 (defvar objc-mode-map
1551 (let ((map (c-make-inherited-keymap)))
1552 ;; Add bindings which are only useful for Objective-C.
1553 (define-key map "\C-c\C-e" 'c-macro-expand)
1555 "Keymap used in objc-mode buffers.")
1557 (easy-menu-define c-objc-menu objc-mode-map "ObjC Mode Commands"
1558 (cons "ObjC" (c-lang-const c-mode-menu objc)))
1560 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.m\\'" . objc-mode))
1563 (define-derived-mode objc-mode prog-mode "ObjC"
1564 "Major mode for editing Objective C code.
1565 To submit a problem report, enter `\\[c-submit-bug-report]' from an
1566 objc-mode buffer. This automatically sets up a mail buffer with
1567 version information already added. You just need to add a description
1568 of the problem, including a reproducible test case, and send the
1571 To see what version of CC Mode you are running, enter `\\[c-version]'.
1573 The hook `c-mode-common-hook' is run with no args at mode
1574 initialization, then `objc-mode-hook'.
1578 (c-initialize-cc-mode t)
1579 (set-syntax-table objc-mode-syntax-table)
1580 (setq local-abbrev-table objc-mode-abbrev-table
1582 (use-local-map objc-mode-map)
1583 (c-init-language-vars-for 'objc-mode)
1584 (c-make-macro-with-semi-re) ; matches macro names whose expansion ends with ;
1585 (c-common-init 'objc-mode)
1586 (easy-menu-add c-objc-menu)
1587 (cc-imenu-init nil 'cc-imenu-objc-function)
1588 (c-run-mode-hooks 'c-mode-common-hook 'objc-mode-hook)
1589 (c-update-modeline))
1594 (defvar java-mode-syntax-table
1595 (funcall (c-lang-const c-make-mode-syntax-table java))
1596 "Syntax table used in java-mode buffers.")
1598 (c-define-abbrev-table 'java-mode-abbrev-table
1599 '(("else" "else" c-electric-continued-statement 0)
1600 ("while" "while" c-electric-continued-statement 0)
1601 ("catch" "catch" c-electric-continued-statement 0)
1602 ("finally" "finally" c-electric-continued-statement 0))
1603 "Abbreviation table used in java-mode buffers.")
1605 (defvar java-mode-map
1606 (let ((map (c-make-inherited-keymap)))
1607 ;; Add bindings which are only useful for Java.
1609 "Keymap used in java-mode buffers.")
1611 ;; Regexp trying to describe the beginning of a Java top-level
1612 ;; definition. This is not used by CC Mode, nor is it maintained
1613 ;; since it's practically impossible to write a regexp that reliably
1614 ;; matches such a construct. Other tools are necessary.
1615 (defconst c-Java-defun-prompt-regexp
1616 "^[ \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-*")
1618 (easy-menu-define c-java-menu java-mode-map "Java Mode Commands"
1619 (cons "Java" (c-lang-const c-mode-menu java)))
1621 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.java\\'" . java-mode))
1624 (define-derived-mode java-mode prog-mode "Java"
1625 "Major mode for editing Java code.
1626 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1627 java-mode buffer. This automatically sets up a mail buffer with
1628 version information already added. You just need to add a description
1629 of the problem, including a reproducible test case, and send the
1632 To see what version of CC Mode you are running, enter `\\[c-version]'.
1634 The hook `c-mode-common-hook' is run with no args at mode
1635 initialization, then `java-mode-hook'.
1639 (c-initialize-cc-mode t)
1640 (set-syntax-table java-mode-syntax-table)
1641 (setq local-abbrev-table java-mode-abbrev-table
1643 (use-local-map java-mode-map)
1644 (c-init-language-vars-for 'java-mode)
1645 (c-common-init 'java-mode)
1646 (easy-menu-add c-java-menu)
1647 (cc-imenu-init cc-imenu-java-generic-expression)
1648 (c-run-mode-hooks 'c-mode-common-hook 'java-mode-hook)
1649 (c-update-modeline))
1652 ;; Support for CORBA's IDL language
1654 (defvar idl-mode-syntax-table
1655 (funcall (c-lang-const c-make-mode-syntax-table idl))
1656 "Syntax table used in idl-mode buffers.")
1658 (c-define-abbrev-table 'idl-mode-abbrev-table nil
1659 "Abbreviation table used in idl-mode buffers.")
1661 (defvar idl-mode-map
1662 (let ((map (c-make-inherited-keymap)))
1663 ;; Add bindings which are only useful for IDL.
1665 "Keymap used in idl-mode buffers.")
1667 (easy-menu-define c-idl-menu idl-mode-map "IDL Mode Commands"
1668 (cons "IDL" (c-lang-const c-mode-menu idl)))
1670 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.idl\\'" . idl-mode))
1673 (define-derived-mode idl-mode prog-mode "IDL"
1674 "Major mode for editing CORBA's IDL, PSDL and CIDL code.
1675 To submit a problem report, enter `\\[c-submit-bug-report]' from an
1676 idl-mode buffer. This automatically sets up a mail buffer with
1677 version information already added. You just need to add a description
1678 of the problem, including a reproducible test case, and send the
1681 To see what version of CC Mode you are running, enter `\\[c-version]'.
1683 The hook `c-mode-common-hook' is run with no args at mode
1684 initialization, then `idl-mode-hook'.
1688 (c-initialize-cc-mode t)
1689 (set-syntax-table idl-mode-syntax-table)
1690 (setq local-abbrev-table idl-mode-abbrev-table)
1691 (use-local-map idl-mode-map)
1692 (c-init-language-vars-for 'idl-mode)
1693 (c-common-init 'idl-mode)
1694 (easy-menu-add c-idl-menu)
1695 ;;(cc-imenu-init cc-imenu-idl-generic-expression) ;TODO
1696 (c-run-mode-hooks 'c-mode-common-hook 'idl-mode-hook)
1697 (c-update-modeline))
1702 (defvar pike-mode-syntax-table
1703 (funcall (c-lang-const c-make-mode-syntax-table pike))
1704 "Syntax table used in pike-mode buffers.")
1706 (c-define-abbrev-table 'pike-mode-abbrev-table
1707 '(("else" "else" c-electric-continued-statement 0)
1708 ("while" "while" c-electric-continued-statement 0))
1709 "Abbreviation table used in pike-mode buffers.")
1711 (defvar pike-mode-map
1712 (let ((map (c-make-inherited-keymap)))
1713 ;; Additional bindings.
1714 (define-key map "\C-c\C-e" 'c-macro-expand)
1716 "Keymap used in pike-mode buffers.")
1718 (easy-menu-define c-pike-menu pike-mode-map "Pike Mode Commands"
1719 (cons "Pike" (c-lang-const c-mode-menu pike)))
1721 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(u?lpc\\|pike\\|pmod\\(\\.in\\)?\\)\\'" . pike-mode))
1722 ;;;###autoload (add-to-list 'interpreter-mode-alist '("pike" . pike-mode))
1725 (define-derived-mode pike-mode prog-mode "Pike"
1726 "Major mode for editing Pike code.
1727 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1728 pike-mode buffer. This automatically sets up a mail buffer with
1729 version information already added. You just need to add a description
1730 of the problem, including a reproducible test case, and send the
1733 To see what version of CC Mode you are running, enter `\\[c-version]'.
1735 The hook `c-mode-common-hook' is run with no args at mode
1736 initialization, then `pike-mode-hook'.
1740 (c-initialize-cc-mode t)
1741 (set-syntax-table pike-mode-syntax-table)
1742 (setq local-abbrev-table pike-mode-abbrev-table
1744 (use-local-map pike-mode-map)
1745 (c-init-language-vars-for 'pike-mode)
1746 (c-common-init 'pike-mode)
1747 (easy-menu-add c-pike-menu)
1748 ;;(cc-imenu-init cc-imenu-pike-generic-expression) ;TODO
1749 (c-run-mode-hooks 'c-mode-common-hook 'pike-mode-hook)
1750 (c-update-modeline))
1755 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.awk\\'" . awk-mode))
1756 ;;;###autoload (add-to-list 'interpreter-mode-alist '("awk" . awk-mode))
1757 ;;;###autoload (add-to-list 'interpreter-mode-alist '("mawk" . awk-mode))
1758 ;;;###autoload (add-to-list 'interpreter-mode-alist '("nawk" . awk-mode))
1759 ;;;###autoload (add-to-list 'interpreter-mode-alist '("gawk" . awk-mode))
1761 (c-define-abbrev-table 'awk-mode-abbrev-table
1762 '(("else" "else" c-electric-continued-statement 0)
1763 ("while" "while" c-electric-continued-statement 0))
1764 "Abbreviation table used in awk-mode buffers.")
1766 (defvar awk-mode-map
1767 (let ((map (c-make-inherited-keymap)))
1768 ;; Add bindings which are only useful for awk.
1769 (define-key map "#" 'self-insert-command)
1770 (define-key map "/" 'self-insert-command)
1771 (define-key map "*" 'self-insert-command)
1772 (define-key map "\C-c\C-n" 'undefined) ; #if doesn't exist in awk.
1773 (define-key map "\C-c\C-p" 'undefined)
1774 (define-key map "\C-c\C-u" 'undefined)
1775 (define-key map "\M-a" 'c-beginning-of-statement) ; 2003/10/7
1776 (define-key map "\M-e" 'c-end-of-statement) ; 2003/10/7
1777 (define-key map "\C-\M-a" 'c-awk-beginning-of-defun)
1778 (define-key map "\C-\M-e" 'c-awk-end-of-defun)
1780 "Keymap used in awk-mode buffers.")
1782 (easy-menu-define c-awk-menu awk-mode-map "AWK Mode Commands"
1783 (cons "AWK" (c-lang-const c-mode-menu awk)))
1785 ;; (require 'cc-awk) brings these in.
1786 (defvar awk-mode-syntax-table)
1787 (declare-function c-awk-unstick-NL-prop "cc-awk" ())
1790 (define-derived-mode awk-mode prog-mode "AWK"
1791 "Major mode for editing AWK code.
1792 To submit a problem report, enter `\\[c-submit-bug-report]' from an
1793 awk-mode buffer. This automatically sets up a mail buffer with version
1794 information already added. You just need to add a description of the
1795 problem, including a reproducible test case, and send the message.
1797 To see what version of CC Mode you are running, enter `\\[c-version]'.
1799 The hook `c-mode-common-hook' is run with no args at mode
1800 initialization, then `awk-mode-hook'.
1804 ;; We need the next line to stop the macro defining
1805 ;; `awk-mode-syntax-table'. This would mask the real table which is
1806 ;; declared in cc-awk.el and hasn't yet been loaded.
1808 (require 'cc-awk) ; Added 2003/6/10.
1809 (c-initialize-cc-mode t)
1810 (set-syntax-table awk-mode-syntax-table)
1811 (setq local-abbrev-table awk-mode-abbrev-table
1813 (use-local-map awk-mode-map)
1814 (c-init-language-vars-for 'awk-mode)
1815 (c-common-init 'awk-mode)
1816 (c-awk-unstick-NL-prop)
1818 (c-run-mode-hooks 'c-mode-common-hook 'awk-mode-hook)
1819 (c-update-modeline))
1824 (defconst c-mode-help-address
1825 "submit@debbugs.gnu.org"
1826 "Address(es) for CC Mode bug reports.")
1829 "Echo the current version of CC Mode in the minibuffer."
1831 (message "Using CC Mode version %s" c-version)
1832 (c-keep-region-active))
1834 (define-obsolete-variable-alias 'c-prepare-bug-report-hooks
1835 'c-prepare-bug-report-hook "24.3")
1836 (defvar c-prepare-bug-report-hook nil)
1838 ;; Dynamic variables used by reporter.
1839 (defvar reporter-prompt-for-summary-p)
1840 (defvar reporter-dont-compact-list)
1842 ;; This could be "emacs,cc-mode" in the version included in Emacs.
1843 (defconst c-mode-bug-package "cc-mode"
1844 "The package to use in the bug submission.")
1846 ;; reporter-submit-bug-report requires sendmail.
1847 (declare-function mail-position-on-field "sendmail" (field &optional soft))
1849 (defun c-submit-bug-report ()
1850 "Submit via mail a bug report on CC Mode."
1854 (let ((reporter-prompt-for-summary-p t)
1855 (reporter-dont-compact-list '(c-offsets-alist))
1856 (style c-indentation-style)
1857 (c-features c-emacs-features))
1859 (if (y-or-n-p "Do you want to submit a report on CC Mode? ")
1861 (reporter-submit-bug-report
1863 (concat "CC Mode " c-version " (" mode-name ")")
1866 '(c-buffer-is-cc-mode
1868 c-syntactic-indentation
1869 c-syntactic-indentation-in-macros
1871 c-auto-align-backslashes
1872 c-backspace-function
1874 c-electric-pound-behavior
1876 c-enable-xemacs-performance-kludge-p
1877 c-old-style-variable-behavior
1881 parse-sexp-ignore-comments
1882 parse-sexp-lookup-properties
1883 lookup-syntax-properties
1884 ;; A brain-damaged XEmacs only variable that, if
1885 ;; set to nil can cause all kinds of chaos.
1886 signal-error-on-buffer-boundary
1887 ;; Variables that affect line breaking and comments.
1897 adaptive-fill-regexp)
1899 (mapc (lambda (var) (unless (boundp var)
1900 (setq vars (delq var vars))))
1901 '(signal-error-on-buffer-boundary
1906 font-lock-maximum-decoration
1907 parse-sexp-lookup-properties
1908 lookup-syntax-properties))
1911 (run-hooks 'c-prepare-bug-report-hook)
1913 (or (mail-position-on-field "X-Debbugs-Package")
1914 (insert c-mode-bug-package)))
1915 (insert (format "Buffer Style: %s\nc-emacs-features: %s\n"
1916 style c-features)))))))
1919 (cc-provide 'cc-mode)
1922 ;; indent-tabs-mode: t
1925 ;;; cc-mode.el ends here