Handle "#" operator properly inside macro. Fix coding bug.
[emacs.git] / lisp / progmodes / cc-mode.el
blob1cba5027f288533dd96a33921b59b6f35c22f520
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 ;; Autoload directive for emacsen that doesn't have an older CC Mode
112 ;; version in the dist.
113 (autoload 'subword-mode "subword"
114 "Mode enabling subword movement and editing keys." t)
116 ;; Load cc-fonts first after font-lock is loaded, since it isn't
117 ;; necessary until font locking is requested.
118 ; (eval-after-load "font-lock" ; 2006-07-09: font-lock is now preloaded.
120 (require 'cc-fonts) ;)
123 ;; Other modes and packages which depend on CC Mode should do the
124 ;; following to make sure everything is loaded and available for their
125 ;; use:
127 ;; (require 'cc-mode)
129 ;; And in the major mode function:
131 ;; (c-initialize-cc-mode t)
132 ;; (c-init-language-vars some-mode)
133 ;; (c-common-init 'some-mode) ; Or perhaps (c-basic-common-init 'some-mode)
135 ;; If you're not writing a derived mode using the language variable
136 ;; system, then some-mode is one of the language modes directly
137 ;; supported by CC Mode. You can then use (c-init-language-vars-for
138 ;; 'some-mode) instead of `c-init-language-vars'.
139 ;; `c-init-language-vars-for' is a function that avoids the rather
140 ;; large expansion of `c-init-language-vars'.
142 ;; If you use `c-basic-common-init' then you might want to call
143 ;; `c-font-lock-init' too to set up CC Mode's font lock support.
145 ;; See cc-langs.el for further info. A small example of a derived mode
146 ;; is also available at <http://cc-mode.sourceforge.net/
147 ;; derived-mode-ex.el>.
149 (defun c-leave-cc-mode-mode ()
150 (setq c-buffer-is-cc-mode nil))
152 (defun c-init-language-vars-for (mode)
153 "Initialize the language variables for one of the language modes
154 directly supported by CC Mode. This can be used instead of the
155 `c-init-language-vars' macro if the language you want to use is one of
156 those, rather than a derived language defined through the language
157 variable system (see \"cc-langs.el\")."
158 (cond ((eq mode 'c-mode) (c-init-language-vars c-mode))
159 ((eq mode 'c++-mode) (c-init-language-vars c++-mode))
160 ((eq mode 'objc-mode) (c-init-language-vars objc-mode))
161 ((eq mode 'java-mode) (c-init-language-vars java-mode))
162 ((eq mode 'idl-mode) (c-init-language-vars idl-mode))
163 ((eq mode 'pike-mode) (c-init-language-vars pike-mode))
164 ((eq mode 'awk-mode) (c-init-language-vars awk-mode))
165 (t (error "Unsupported mode %s" mode))))
167 ;;;###autoload
168 (defun c-initialize-cc-mode (&optional new-style-init)
169 "Initialize CC Mode for use in the current buffer.
170 If the optional NEW-STYLE-INIT is nil or left out then all necessary
171 initialization to run CC Mode for the C language is done. Otherwise
172 only some basic setup is done, and a call to `c-init-language-vars' or
173 `c-init-language-vars-for' is necessary too (which gives more
174 control). See \"cc-mode.el\" for more info."
176 (setq c-buffer-is-cc-mode t)
178 (let ((initprop 'cc-mode-is-initialized)
179 c-initialization-ok)
180 (unless (get 'c-initialize-cc-mode initprop)
181 (unwind-protect
182 (progn
183 (put 'c-initialize-cc-mode initprop t)
184 (c-initialize-builtin-style)
185 (run-hooks 'c-initialization-hook)
186 ;; Fix obsolete variables.
187 (if (boundp 'c-comment-continuation-stars)
188 (setq c-block-comment-prefix
189 (symbol-value 'c-comment-continuation-stars)))
190 (add-hook 'change-major-mode-hook 'c-leave-cc-mode-mode)
191 (setq c-initialization-ok t)
192 ;; Connect up with Emacs's electric-indent-mode, for >= Emacs 24.4
193 (when (fboundp 'electric-indent-local-mode)
194 (add-hook 'electric-indent-mode-hook 'c-electric-indent-mode-hook)
195 (add-hook 'electric-indent-local-mode-hook
196 'c-electric-indent-local-mode-hook)))
197 ;; Will try initialization hooks again if they failed.
198 (put 'c-initialize-cc-mode initprop c-initialization-ok))))
200 (unless new-style-init
201 (c-init-language-vars-for 'c-mode)))
204 ;;; Common routines.
206 (defvar c-mode-base-map ()
207 "Keymap shared by all CC Mode related modes.")
209 (defun c-make-inherited-keymap ()
210 (let ((map (make-sparse-keymap)))
211 ;; Necessary to use `cc-bytecomp-fboundp' below since this
212 ;; function is called from top-level forms that are evaluated
213 ;; while cc-bytecomp is active when one does M-x eval-buffer.
214 (cond
215 ;; Emacs
216 ((cc-bytecomp-fboundp 'set-keymap-parent)
217 (set-keymap-parent map c-mode-base-map))
218 ;; XEmacs
219 ((fboundp 'set-keymap-parents)
220 (set-keymap-parents map c-mode-base-map))
221 ;; incompatible
222 (t (error "CC Mode is incompatible with this version of Emacs")))
223 map))
225 (defun c-define-abbrev-table (name defs &optional doc)
226 ;; Compatibility wrapper for `define-abbrev' which passes a non-nil
227 ;; sixth argument for SYSTEM-FLAG in emacsen that support it
228 ;; (currently only Emacs >= 21.2).
229 (let ((table (or (and (boundp name) (symbol-value name))
230 (progn (condition-case nil
231 (define-abbrev-table name nil doc)
232 (wrong-number-of-arguments ;E.g. Emacs<23.
233 (eval `(defvar ,name nil ,doc))
234 (define-abbrev-table name nil)))
235 (symbol-value name)))))
236 (while defs
237 (condition-case nil
238 (apply 'define-abbrev table (append (car defs) '(t)))
239 (wrong-number-of-arguments
240 (apply 'define-abbrev table (car defs))))
241 (setq defs (cdr defs)))))
242 (put 'c-define-abbrev-table 'lisp-indent-function 1)
244 (defun c-bind-special-erase-keys ()
245 ;; Only used in Emacs to bind C-c C-<delete> and C-c C-<backspace>
246 ;; to the proper keys depending on `normal-erase-is-backspace'.
247 (if normal-erase-is-backspace
248 (progn
249 (define-key c-mode-base-map (kbd "C-c C-<delete>")
250 'c-hungry-delete-forward)
251 (define-key c-mode-base-map (kbd "C-c C-<backspace>")
252 'c-hungry-delete-backwards))
253 (define-key c-mode-base-map (kbd "C-c C-<delete>")
254 'c-hungry-delete-backwards)
255 (define-key c-mode-base-map (kbd "C-c C-<backspace>")
256 'c-hungry-delete-forward)))
258 (if c-mode-base-map
261 (setq c-mode-base-map (make-sparse-keymap))
263 ;; Separate M-BS from C-M-h. The former should remain
264 ;; backward-kill-word.
265 (define-key c-mode-base-map [(control meta h)] 'c-mark-function)
266 (define-key c-mode-base-map "\e\C-q" 'c-indent-exp)
267 (substitute-key-definition 'backward-sentence
268 'c-beginning-of-statement
269 c-mode-base-map global-map)
270 (substitute-key-definition 'forward-sentence
271 'c-end-of-statement
272 c-mode-base-map global-map)
273 (substitute-key-definition 'indent-new-comment-line
274 'c-indent-new-comment-line
275 c-mode-base-map global-map)
276 (substitute-key-definition 'indent-for-tab-command
277 ;; XXX Is this the right thing to do
278 ;; here?
279 'c-indent-line-or-region
280 c-mode-base-map global-map)
281 (when (fboundp 'comment-indent-new-line)
282 ;; indent-new-comment-line has changed name to
283 ;; comment-indent-new-line in Emacs 21.
284 (substitute-key-definition 'comment-indent-new-line
285 'c-indent-new-comment-line
286 c-mode-base-map global-map))
288 ;; RMS says don't make these the default.
289 ;; (April 2006): RMS has now approved these commands as defaults.
290 (unless (memq 'argumentative-bod-function c-emacs-features)
291 (define-key c-mode-base-map "\e\C-a" 'c-beginning-of-defun)
292 (define-key c-mode-base-map "\e\C-e" 'c-end-of-defun))
294 (define-key c-mode-base-map "\C-c\C-n" 'c-forward-conditional)
295 (define-key c-mode-base-map "\C-c\C-p" 'c-backward-conditional)
296 (define-key c-mode-base-map "\C-c\C-u" 'c-up-conditional)
298 ;; It doesn't suffice to put `c-fill-paragraph' on
299 ;; `fill-paragraph-function' since `c-fill-paragraph' must be called
300 ;; before any fill prefix adaption is done. E.g. `filladapt-mode'
301 ;; replaces `fill-paragraph' and does the adaption before calling
302 ;; `fill-paragraph-function', and we have to mask comments etc
303 ;; before that. Also, `c-fill-paragraph' chains on to
304 ;; `fill-paragraph' and the value on `fill-paragraph-function' to
305 ;; do the actual filling work.
306 (substitute-key-definition 'fill-paragraph 'c-fill-paragraph
307 c-mode-base-map global-map)
308 ;; In XEmacs the default fill function is called
309 ;; fill-paragraph-or-region.
310 (substitute-key-definition 'fill-paragraph-or-region 'c-fill-paragraph
311 c-mode-base-map global-map)
313 ;; We bind the forward deletion key and (implicitly) C-d to
314 ;; `c-electric-delete-forward', and the backward deletion key to
315 ;; `c-electric-backspace'. The hungry variants are bound to the
316 ;; same keys but prefixed with C-c. This implies that C-c C-d is
317 ;; `c-hungry-delete-forward'. For consistency, we bind not only C-c
318 ;; <backspace> to `c-hungry-delete-backwards' but also
319 ;; C-c C-<backspace>, so that the Ctrl key can be held down during
320 ;; the whole sequence regardless of the direction. This in turn
321 ;; implies that we bind C-c C-<delete> to `c-hungry-delete-forward',
322 ;; for the same reason.
324 ;; Bind the electric deletion functions to C-d and DEL. Emacs 21
325 ;; automatically maps the [delete] and [backspace] keys to these two
326 ;; depending on window system and user preferences. (In earlier
327 ;; versions it's possible to do the same by using `function-key-map'.)
328 (define-key c-mode-base-map "\C-d" 'c-electric-delete-forward)
329 (define-key c-mode-base-map "\177" 'c-electric-backspace)
330 (define-key c-mode-base-map "\C-c\C-d" 'c-hungry-delete-forward)
331 (define-key c-mode-base-map [?\C-c ?\d] 'c-hungry-delete-backwards)
332 (define-key c-mode-base-map [?\C-c ?\C-\d] 'c-hungry-delete-backwards)
333 (define-key c-mode-base-map [?\C-c deletechar] 'c-hungry-delete-forward) ; C-c <delete> on a tty.
334 (define-key c-mode-base-map [?\C-c (control deletechar)] ; C-c C-<delete> on a tty.
335 'c-hungry-delete-forward)
336 (when (boundp 'normal-erase-is-backspace)
337 ;; The automatic C-d and DEL mapping functionality doesn't extend
338 ;; to special combinations like C-c C-<delete>, so we have to hook
339 ;; into the `normal-erase-is-backspace' system to bind it directly
340 ;; as appropriate.
341 (add-hook 'normal-erase-is-backspace-hook 'c-bind-special-erase-keys)
342 (c-bind-special-erase-keys))
344 (when (fboundp 'delete-forward-p)
345 ;; In XEmacs we fix the forward and backward deletion behavior by
346 ;; binding the keysyms for the [delete] and [backspace] keys
347 ;; directly, and use `delete-forward-p' to decide what [delete]
348 ;; should do. That's done in the XEmacs specific
349 ;; `c-electric-delete' and `c-hungry-delete' functions.
350 (define-key c-mode-base-map [delete] 'c-electric-delete)
351 (define-key c-mode-base-map [backspace] 'c-electric-backspace)
352 (define-key c-mode-base-map (kbd "C-c <delete>") 'c-hungry-delete)
353 (define-key c-mode-base-map (kbd "C-c C-<delete>") 'c-hungry-delete)
354 (define-key c-mode-base-map (kbd "C-c <backspace>")
355 'c-hungry-delete-backwards)
356 (define-key c-mode-base-map (kbd "C-c C-<backspace>")
357 'c-hungry-delete-backwards))
359 (define-key c-mode-base-map "#" 'c-electric-pound)
360 (define-key c-mode-base-map "{" 'c-electric-brace)
361 (define-key c-mode-base-map "}" 'c-electric-brace)
362 (define-key c-mode-base-map "/" 'c-electric-slash)
363 (define-key c-mode-base-map "*" 'c-electric-star)
364 (define-key c-mode-base-map ";" 'c-electric-semi&comma)
365 (define-key c-mode-base-map "," 'c-electric-semi&comma)
366 (define-key c-mode-base-map ":" 'c-electric-colon)
367 (define-key c-mode-base-map "(" 'c-electric-paren)
368 (define-key c-mode-base-map ")" 'c-electric-paren)
370 (define-key c-mode-base-map "\C-c\C-\\" 'c-backslash-region)
371 (define-key c-mode-base-map "\C-c\C-a" 'c-toggle-auto-newline)
372 (define-key c-mode-base-map "\C-c\C-b" 'c-submit-bug-report)
373 (define-key c-mode-base-map "\C-c\C-c" 'comment-region)
374 (define-key c-mode-base-map "\C-c\C-l" 'c-toggle-electric-state)
375 (define-key c-mode-base-map "\C-c\C-o" 'c-set-offset)
376 (define-key c-mode-base-map "\C-c\C-q" 'c-indent-defun)
377 (define-key c-mode-base-map "\C-c\C-s" 'c-show-syntactic-information)
378 ;; (define-key c-mode-base-map "\C-c\C-t" 'c-toggle-auto-hungry-state) Commented out by ACM, 2005-03-05.
379 (define-key c-mode-base-map "\C-c." 'c-set-style)
380 ;; conflicts with OOBR
381 ;;(define-key c-mode-base-map "\C-c\C-v" 'c-version)
382 ;; (define-key c-mode-base-map "\C-c\C-y" 'c-toggle-hungry-state) Commented out by ACM, 2005-11-22.
383 (define-key c-mode-base-map "\C-c\C-w" 'subword-mode)
386 ;; We don't require the outline package, but we configure it a bit anyway.
387 (cc-bytecomp-defvar outline-level)
389 (defun c-mode-menu (modestr)
390 "Return a menu spec suitable for `easy-menu-define' that is exactly
391 like the C mode menu except that the menu bar item name is MODESTR
392 instead of \"C\".
394 This function is provided for compatibility only; derived modes should
395 preferably use the `c-mode-menu' language constant directly."
396 (cons modestr (c-lang-const c-mode-menu c)))
398 ;; Ugly hack to pull in the definition of `c-populate-syntax-table'
399 ;; from cc-langs to make it available at runtime. It's either this or
400 ;; moving the definition for it to cc-defs, but that would mean to
401 ;; break up the syntax table setup over two files.
402 (defalias 'c-populate-syntax-table
403 (cc-eval-when-compile
404 (let ((f (symbol-function 'c-populate-syntax-table)))
405 (if (byte-code-function-p f) f (byte-compile f)))))
407 ;; CAUTION: Try to avoid installing things on
408 ;; `before-change-functions'. The macro `combine-after-change-calls'
409 ;; is used and it doesn't work if there are things on that hook. That
410 ;; can cause font lock functions to run in inconvenient places during
411 ;; temporary changes in some font lock support modes, causing extra
412 ;; unnecessary work and font lock glitches due to interactions between
413 ;; various text properties.
415 ;; (2007-02-12): The macro `combine-after-change-calls' ISN'T used any
416 ;; more.
418 (defun c-unfind-enclosing-token (pos)
419 ;; If POS is wholly inside a token, remove that id from
420 ;; `c-found-types', should it be present. Return t if we were in an
421 ;; id, else nil.
422 (save-excursion
423 (let ((tok-beg (progn (goto-char pos)
424 (and (c-beginning-of-current-token) (point))))
425 (tok-end (progn (goto-char pos)
426 (and (c-end-of-current-token) (point)))))
427 (when (and tok-beg tok-end)
428 (c-unfind-type (buffer-substring-no-properties tok-beg tok-end))
429 t))))
431 (defun c-unfind-coalesced-tokens (beg end)
432 ;; unless the non-empty region (beg end) is entirely WS and there's at
433 ;; least one character of WS just before or after this region, remove
434 ;; the tokens which touch the region from `c-found-types' should they
435 ;; be present.
436 (or (c-partial-ws-p beg end)
437 (save-excursion
438 (progn
439 (goto-char beg)
440 (or (eq beg (point-min))
441 (c-skip-ws-backward (1- beg))
442 (/= (point) beg)
443 (= (c-backward-token-2) 1)
444 (c-unfind-type (buffer-substring-no-properties
445 (point) beg)))
446 (goto-char end)
447 (or (eq end (point-max))
448 (c-skip-ws-forward (1+ end))
449 (/= (point) end)
450 (progn (forward-char) (c-end-of-current-token) nil)
451 (c-unfind-type (buffer-substring-no-properties
452 end (point))))))))
454 ;; c-maybe-stale-found-type records a place near the region being
455 ;; changed where an element of `found-types' might become stale. It
456 ;; is set in c-before-change and is either nil, or has the form:
458 ;; (c-decl-id-start "foo" 97 107 " (* ooka) " "o"), where
460 ;; o - `c-decl-id-start' is the c-type text property value at buffer
461 ;; pos 96.
463 ;; o - 97 107 is the region potentially containing the stale type -
464 ;; this is delimited by a non-nil c-type text property at 96 and
465 ;; either another one or a ";", "{", or "}" at 107.
467 ;; o - " (* ooka) " is the (before change) buffer portion containing
468 ;; the suspect type (here "ooka").
470 ;; o - "o" is the buffer contents which is about to be deleted. This
471 ;; would be the empty string for an insertion.
472 (defvar c-maybe-stale-found-type nil)
473 (make-variable-buffer-local 'c-maybe-stale-found-type)
475 (defun c-basic-common-init (mode default-style)
476 "Do the necessary initialization for the syntax handling routines
477 and the line breaking/filling code. Intended to be used by other
478 packages that embed CC Mode.
480 MODE is the CC Mode flavor to set up, e.g. 'c-mode or 'java-mode.
481 DEFAULT-STYLE tells which indentation style to install. It has the
482 same format as `c-default-style'.
484 Note that `c-init-language-vars' must be called before this function.
485 This function cannot do that since `c-init-language-vars' is a macro
486 that requires a literal mode spec at compile time."
488 (setq c-buffer-is-cc-mode mode)
490 ;; these variables should always be buffer local; they do not affect
491 ;; indentation style.
492 (make-local-variable 'comment-start)
493 (make-local-variable 'comment-end)
494 (make-local-variable 'comment-start-skip)
496 (make-local-variable 'paragraph-start)
497 (make-local-variable 'paragraph-separate)
498 (make-local-variable 'paragraph-ignore-fill-prefix)
499 (make-local-variable 'adaptive-fill-mode)
500 (make-local-variable 'adaptive-fill-regexp)
501 (make-local-variable 'fill-paragraph-handle-comment)
503 ;; now set their values
504 (set (make-local-variable 'parse-sexp-ignore-comments) t)
505 (set (make-local-variable 'indent-line-function) 'c-indent-line)
506 (set (make-local-variable 'indent-region-function) 'c-indent-region)
507 (set (make-local-variable 'normal-auto-fill-function) 'c-do-auto-fill)
508 (set (make-local-variable 'comment-multi-line) t)
509 (set (make-local-variable 'comment-line-break-function)
510 'c-indent-new-comment-line)
512 ;; For the benefit of adaptive file, which otherwise mis-fills.
513 (setq fill-paragraph-handle-comment nil)
515 ;; Install `c-fill-paragraph' on `fill-paragraph-function' so that a
516 ;; direct call to `fill-paragraph' behaves better. This still
517 ;; doesn't work with filladapt but it's better than nothing.
518 (set (make-local-variable 'fill-paragraph-function) 'c-fill-paragraph)
520 ;; Initialize the cache of brace pairs, and opening braces/brackets/parens.
521 (c-state-cache-init)
523 (when (or c-recognize-<>-arglists
524 (c-major-mode-is 'awk-mode)
525 (c-major-mode-is '(java-mode c-mode c++-mode objc-mode)))
526 ;; We'll use the syntax-table text property to change the syntax
527 ;; of some chars for this language, so do the necessary setup for
528 ;; that.
530 ;; Note to other package developers: It's ok to turn this on in CC
531 ;; Mode buffers when CC Mode doesn't, but it's not ok to turn it
532 ;; off if CC Mode has turned it on.
534 ;; Emacs.
535 (when (boundp 'parse-sexp-lookup-properties)
536 (set (make-local-variable 'parse-sexp-lookup-properties) t))
538 ;; Same as above for XEmacs.
539 (when (boundp 'lookup-syntax-properties)
540 (set (make-local-variable 'lookup-syntax-properties) t)))
542 ;; Use this in Emacs 21+ to avoid meddling with the rear-nonsticky
543 ;; property on each character.
544 (when (boundp 'text-property-default-nonsticky)
545 (mapc (lambda (tprop)
546 (unless (assq tprop text-property-default-nonsticky)
547 (set (make-local-variable 'text-property-default-nonsticky)
548 (cons `(,tprop . t) text-property-default-nonsticky))))
549 '(syntax-table category c-type)))
551 ;; In Emacs 21 and later it's possible to turn off the ad-hoc
552 ;; heuristic that open parens in column 0 are defun starters. Since
553 ;; we have c-state-cache, that heuristic isn't useful and only causes
554 ;; trouble, so turn it off.
555 ;; 2006/12/17: This facility is somewhat confused, and doesn't really seem
556 ;; helpful. Comment it out for now.
557 ;; (when (memq 'col-0-paren c-emacs-features)
558 ;; (make-local-variable 'open-paren-in-column-0-is-defun-start)
559 ;; (setq open-paren-in-column-0-is-defun-start nil))
561 (c-clear-found-types)
563 ;; now set the mode style based on default-style
564 (let ((style (cc-choose-style-for-mode mode default-style)))
565 ;; Override style variables if `c-old-style-variable-behavior' is
566 ;; set. Also override if we are using global style variables,
567 ;; have already initialized a style once, and are switching to a
568 ;; different style. (It's doubtful whether this is desirable, but
569 ;; the whole situation with nonlocal style variables is a bit
570 ;; awkward. It's at least the most compatible way with the old
571 ;; style init procedure.)
572 (c-set-style style (not (or c-old-style-variable-behavior
573 (and (not c-style-variables-are-local-p)
574 c-indentation-style
575 (not (string-equal c-indentation-style
576 style)))))))
577 (c-setup-paragraph-variables)
579 ;; we have to do something special for c-offsets-alist so that the
580 ;; buffer local value has its own alist structure.
581 (setq c-offsets-alist (copy-alist c-offsets-alist))
583 ;; setup the comment indent variable in a Emacs version portable way
584 (set (make-local-variable 'comment-indent-function) 'c-comment-indent)
586 ;; In Emacs 24.4 onwards, prevent Emacs's built in electric indentation from
587 ;; messing up CC Mode's, and set `c-electric-flag' if `electric-indent-mode'
588 ;; has been called by the user.
589 (when (boundp 'electric-indent-inhibit) (setq electric-indent-inhibit t))
590 ;; CC-mode should obey Emacs's generic preferences, tho only do it if
591 ;; Emacs's generic preferences can be set per-buffer (Emacs>=24.4).
592 (when (fboundp 'electric-indent-local-mode)
593 (setq c-electric-flag electric-indent-mode))
595 ;; ;; Put submode indicators onto minor-mode-alist, but only once.
596 ;; (or (assq 'c-submode-indicators minor-mode-alist)
597 ;; (setq minor-mode-alist
598 ;; (cons '(c-submode-indicators c-submode-indicators)
599 ;; minor-mode-alist)))
600 (c-update-modeline)
602 ;; Install the functions that ensure that various internal caches
603 ;; don't become invalid due to buffer changes.
604 (when (featurep 'xemacs)
605 (make-local-hook 'before-change-functions)
606 (make-local-hook 'after-change-functions))
607 (add-hook 'before-change-functions 'c-before-change nil t)
608 (add-hook 'after-change-functions 'c-after-change nil t)
609 (set (make-local-variable 'font-lock-extend-after-change-region-function)
610 'c-extend-after-change-region)) ; Currently (2009-05) used by all
611 ; languages with #define (C, C++,; ObjC), and by AWK.
613 (defun c-setup-doc-comment-style ()
614 "Initialize the variables that depend on the value of `c-doc-comment-style'."
615 (when (and (featurep 'font-lock)
616 (symbol-value 'font-lock-mode))
617 ;; Force font lock mode to reinitialize itself.
618 (font-lock-mode 0)
619 (font-lock-mode 1)))
621 ;; Buffer local variables defining the region to be fontified by a font lock
622 ;; after-change function. They are set in c-after-change to
623 ;; after-change-functions' BEG and END, and may be modified by functions in
624 ;; `c-before-font-lock-functions'.
625 (defvar c-new-BEG 0)
626 (make-variable-buffer-local 'c-new-BEG)
627 (defvar c-new-END 0)
628 (make-variable-buffer-local 'c-new-END)
630 (defun c-common-init (&optional mode)
631 "Common initialization for all CC Mode modes.
632 In addition to the work done by `c-basic-common-init' and
633 `c-font-lock-init', this function sets up various other things as
634 customary in CC Mode modes but which aren't strictly necessary for CC
635 Mode to operate correctly.
637 MODE is the symbol for the mode to initialize, like 'c-mode. See
638 `c-basic-common-init' for details. It's only optional to be
639 compatible with old code; callers should always specify it."
641 (unless mode
642 ;; Called from an old third party package. The fallback is to
643 ;; initialize for C.
644 (c-init-language-vars-for 'c-mode))
646 (c-basic-common-init mode c-default-style)
647 (when mode
648 ;; Only initialize font locking if we aren't called from an old package.
649 (c-font-lock-init))
651 ;; Starting a mode is a sort of "change". So call the change functions...
652 (save-restriction
653 (widen)
654 (setq c-new-BEG (point-min))
655 (setq c-new-END (point-max))
656 (save-excursion
657 (mapc (lambda (fn)
658 (funcall fn (point-min) (point-max)))
659 c-get-state-before-change-functions)
660 (mapc (lambda (fn)
661 (funcall fn (point-min) (point-max)
662 (- (point-max) (point-min))))
663 c-before-font-lock-functions)))
665 (set (make-local-variable 'outline-regexp) "[^#\n\^M]")
666 (set (make-local-variable 'outline-level) 'c-outline-level)
667 (set (make-local-variable 'add-log-current-defun-function)
668 (lambda ()
669 (or (c-cpp-define-name) (c-defun-name))))
670 (let ((rfn (assq mode c-require-final-newline)))
671 (when rfn
672 (and (cdr rfn)
673 (set (make-local-variable 'require-final-newline)
674 mode-require-final-newline)))))
676 (defun c-count-cfss (lv-alist)
677 ;; LV-ALIST is an alist like `file-local-variables-alist'. Count how many
678 ;; elements with the key `c-file-style' there are in it.
679 (let ((elt-ptr lv-alist) elt (cownt 0))
680 (while elt-ptr
681 (setq elt (car elt-ptr)
682 elt-ptr (cdr elt-ptr))
683 (when (eq (car elt) 'c-file-style)
684 (setq cownt (1+ cownt))))
685 cownt))
687 (defun c-before-hack-hook ()
688 "Set the CC Mode style and \"offsets\" when in the buffer's local variables.
689 They are set only when, respectively, the pseudo variables
690 `c-file-style' and `c-file-offsets' are present in the list.
692 This function is called from the hook `before-hack-local-variables-hook'."
693 (when c-buffer-is-cc-mode
694 (let ((mode-cons (assq 'mode file-local-variables-alist))
695 (stile (cdr (assq 'c-file-style file-local-variables-alist)))
696 (offsets (cdr (assq 'c-file-offsets file-local-variables-alist))))
697 (when mode-cons
698 (hack-one-local-variable (car mode-cons) (cdr mode-cons))
699 (setq file-local-variables-alist
700 (delq mode-cons file-local-variables-alist)))
701 (when stile
702 (or (stringp stile) (error "c-file-style is not a string"))
703 (if (boundp 'dir-local-variables-alist)
704 ;; Determine whether `c-file-style' was set in the file's local
705 ;; variables or in a .dir-locals.el (a directory setting).
706 (let ((cfs-in-file-and-dir-count
707 (c-count-cfss file-local-variables-alist))
708 (cfs-in-dir-count (c-count-cfss dir-local-variables-alist)))
709 (c-set-style stile
710 (and (= cfs-in-file-and-dir-count cfs-in-dir-count)
711 'keep-defaults)))
712 (c-set-style stile)))
713 (when offsets
714 (mapc
715 (lambda (langentry)
716 (let ((langelem (car langentry))
717 (offset (cdr langentry)))
718 (c-set-offset langelem offset)))
719 offsets)))))
721 (defun c-remove-any-local-eval-or-mode-variables ()
722 ;; If the buffer specifies `mode' or `eval' in its File Local Variable list
723 ;; or on the first line, remove all occurrences. See
724 ;; `c-postprocess-file-styles' for justification. There is no need to save
725 ;; point here, or even bother too much about the buffer contents. However,
726 ;; DON'T mess up the kill-ring.
728 ;; Most of the code here is derived from Emacs 21.3's `hack-local-variables'
729 ;; in files.el.
730 (goto-char (point-max))
731 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
732 (let (lv-point (prefix "") (suffix ""))
733 (when (let ((case-fold-search t))
734 (search-forward "Local Variables:" nil t))
735 (setq lv-point (point))
736 ;; The prefix is what comes before "local variables:" in its line.
737 ;; The suffix is what comes after "local variables:" in its line.
738 (skip-chars-forward " \t")
739 (or (eolp)
740 (setq suffix (buffer-substring (point)
741 (progn (end-of-line) (point)))))
742 (goto-char (match-beginning 0))
743 (or (bolp)
744 (setq prefix
745 (buffer-substring (point)
746 (progn (beginning-of-line) (point)))))
748 (while (search-forward-regexp
749 (concat "^[ \t]*"
750 (regexp-quote prefix)
751 "\\(mode\\|eval\\):.*"
752 (regexp-quote suffix)
753 "$")
754 nil t)
755 (forward-line 0)
756 (delete-region (point) (progn (forward-line) (point)))))
758 ;; Delete the first line, if we've got one, in case it contains a mode spec.
759 (unless (and lv-point
760 (progn (goto-char lv-point)
761 (forward-line 0)
762 (bobp)))
763 (goto-char (point-min))
764 (unless (eobp)
765 (delete-region (point) (progn (forward-line) (point)))))))
767 (defun c-postprocess-file-styles ()
768 "Function that post processes relevant file local variables in CC Mode.
769 Currently, this function simply applies any style and offset settings
770 found in the file's Local Variable list. It first applies any style
771 setting found in `c-file-style', then it applies any offset settings
772 it finds in `c-file-offsets'.
774 Note that the style variables are always made local to the buffer."
776 ;; apply file styles and offsets
777 (when c-buffer-is-cc-mode
778 (if (or c-file-style c-file-offsets)
779 (c-make-styles-buffer-local t))
780 (when c-file-style
781 (or (stringp c-file-style)
782 (error "c-file-style is not a string"))
783 (c-set-style c-file-style))
785 (and c-file-offsets
786 (mapc
787 (lambda (langentry)
788 (let ((langelem (car langentry))
789 (offset (cdr langentry)))
790 (c-set-offset langelem offset)))
791 c-file-offsets))
792 ;; Problem: The file local variable block might have explicitly set a
793 ;; style variable. The `c-set-style' or `mapcar' call might have
794 ;; overwritten this. So we run `hack-local-variables' again to remedy
795 ;; this. There are no guarantees this will work properly, particularly as
796 ;; we have no control over what the other hook functions on
797 ;; `hack-local-variables-hook' would have done. We now (2006/2/1) remove
798 ;; any `eval' or `mode' expressions before we evaluate again (see below).
799 ;; ACM, 2005/11/2.
801 ;; Problem (bug reported by Gustav Broberg): if one of the variables is
802 ;; `mode', this will invoke c-mode (etc.) again, setting up the style etc.
803 ;; We prevent this by temporarily removing `mode' from the Local Variables
804 ;; section.
805 (if (or c-file-style c-file-offsets)
806 (let ((hack-local-variables-hook nil) (inhibit-read-only t))
807 (c-tentative-buffer-changes
808 (c-remove-any-local-eval-or-mode-variables)
809 (hack-local-variables))
810 nil))))
812 (if (boundp 'before-hack-local-variables-hook)
813 (add-hook 'before-hack-local-variables-hook 'c-before-hack-hook)
814 (add-hook 'hack-local-variables-hook 'c-postprocess-file-styles))
816 (defmacro c-run-mode-hooks (&rest hooks)
817 ;; Emacs 21.1 has introduced a system with delayed mode hooks that
818 ;; requires the use of the new function `run-mode-hooks'.
819 (if (cc-bytecomp-fboundp 'run-mode-hooks)
820 `(run-mode-hooks ,@hooks)
821 `(progn ,@(mapcar (lambda (hook) `(run-hooks ,hook)) hooks))))
824 ;;; Change hooks, linking with Font Lock and electric-indent-mode.
826 ;; Buffer local variables recording Beginning/End-of-Macro position before a
827 ;; change, when a macro straddles, respectively, the BEG or END (or both) of
828 ;; the change region. Otherwise these have the values BEG/END.
829 (defvar c-old-BOM 0)
830 (make-variable-buffer-local 'c-old-BOM)
831 (defvar c-old-EOM 0)
832 (make-variable-buffer-local 'c-old-EOM)
834 (defun c-extend-region-for-CPP (beg end)
835 ;; Set c-old-BOM or c-old-EOM respectively to BEG, END, each extended to the
836 ;; beginning/end of any preprocessor construct they may be in.
838 ;; Point is undefined both before and after this function call; the buffer
839 ;; has already been widened, and match-data saved. The return value is
840 ;; meaningless.
842 ;; This function is in the C/C++/ObjC values of
843 ;; `c-get-state-before-change-functions' and is called exclusively as a
844 ;; before change function.
845 (goto-char beg)
846 (c-beginning-of-macro)
847 (setq c-old-BOM (point))
849 (goto-char end)
850 (when (c-beginning-of-macro)
851 (c-end-of-macro)
852 (or (eobp) (forward-char))) ; Over the terminating NL which may be marked
853 ; with a c-cpp-delimiter category property
854 (setq c-old-EOM (point)))
856 (defun c-extend-font-lock-region-for-macros (begg endd &optional old-len)
857 ;; Extend the region (BEGG ENDD) to cover all (possibly changed)
858 ;; preprocessor macros; return the cons (new-BEG . new-END). OLD-LEN should
859 ;; be either the old length parameter when called from an
860 ;; after-change-function, or nil otherwise. This defun uses the variables
861 ;; c-old-BOM, c-new-BOM.
863 ;; Point is undefined on both entry and exit to this function. The buffer
864 ;; will have been widened on entry.
865 (let (limits new-beg new-end)
866 (goto-char c-old-BOM) ; already set to old start of macro or begg.
867 (setq new-beg
868 (min begg
869 (if (setq limits (c-state-literal-at (point)))
870 (cdr limits) ; go forward out of any string or comment.
871 (point))))
873 (goto-char endd)
874 (if (setq limits (c-state-literal-at (point)))
875 (goto-char (car limits))) ; go backward out of any string or comment.
876 (if (c-beginning-of-macro)
877 (c-end-of-macro))
878 (setq new-end (max endd
879 (if old-len
880 (+ (- c-old-EOM old-len) (- endd begg))
881 c-old-EOM)
882 (point)))
883 (cons new-beg new-end)))
885 (defun c-neutralize-CPP-line (beg end)
886 ;; BEG and END bound a region, typically a preprocessor line. Put a
887 ;; "punctuation" syntax-table property on syntactically obtrusive
888 ;; characters, ones which would interact syntactically with stuff outside
889 ;; this region.
891 ;; These are unmatched string delimiters, or unmatched
892 ;; parens/brackets/braces. An unclosed comment is regarded as valid, NOT
893 ;; obtrusive.
894 (save-excursion
895 (let (s)
896 (while
897 (progn
898 (setq s (parse-partial-sexp beg end -1))
899 (cond
900 ((< (nth 0 s) 0) ; found an unmated ),},]
901 (c-put-char-property (1- (point)) 'syntax-table '(1))
903 ((nth 3 s) ; In a string
904 (c-put-char-property (nth 8 s) 'syntax-table '(1))
906 ((> (nth 0 s) 0) ; In a (,{,[
907 (c-put-char-property (nth 1 s) 'syntax-table '(1))
909 (t nil)))))))
911 (defun c-neutralize-syntax-in-and-mark-CPP (begg endd old-len)
912 ;; (i) Extend the font lock region to cover all changed preprocessor
913 ;; regions; it does this by setting the variables `c-new-BEG' and
914 ;; `c-new-END' to the new boundaries.
916 ;; (ii) "Neutralize" every preprocessor line wholly or partially in the
917 ;; extended changed region. "Restore" lines which were CPP lines before the
918 ;; change and are no longer so; these can be located from the Buffer local
919 ;; variables `c-old-BOM' and `c-old-EOM'.
921 ;; (iii) Mark every CPP construct by placing a `category' property value
922 ;; `c-cpp-delimiter' at its start and end. The marked characters are the
923 ;; opening # and usually the terminating EOL, but sometimes the character
924 ;; before a comment/string delimiter.
926 ;; That is, set syntax-table properties on characters that would otherwise
927 ;; interact syntactically with those outside the CPP line(s).
929 ;; This function is called from an after-change function, BEGG ENDD and
930 ;; OLD-LEN being the standard parameters. It prepares the buffer for font
931 ;; locking, hence must get called before `font-lock-after-change-function'.
933 ;; Point is undefined both before and after this function call, the buffer
934 ;; has been widened, and match-data saved. The return value is ignored.
936 ;; This function is in the C/C++/ObjC value of `c-before-font-lock-functions'.
938 ;; Note: SPEED _MATTERS_ IN THIS FUNCTION!!!
940 ;; This function might make hidden buffer changes.
941 (c-save-buffer-state (new-bounds)
942 ;; First determine the region, (c-new-BEG c-new-END), which will get font
943 ;; locked. It might need "neutralizing". This region may not start
944 ;; inside a string, comment, or macro.
945 (setq new-bounds (c-extend-font-lock-region-for-macros
946 c-new-BEG c-new-END old-len))
947 (setq c-new-BEG (max (car new-bounds) (c-determine-limit 500 begg))
948 c-new-END (min (cdr new-bounds) (c-determine-+ve-limit 500 endd)))
949 ;; Clear all old relevant properties.
950 (c-clear-char-property-with-value c-new-BEG c-new-END 'syntax-table '(1))
951 (c-clear-char-property-with-value c-new-BEG c-new-END 'category 'c-cpp-delimiter)
952 ;; FIXME!!! What about the "<" and ">" category properties? 2009-11-16
954 ;; Add needed properties to each CPP construct in the region.
955 (goto-char c-new-BEG)
956 (skip-chars-backward " \t")
957 (let ((pps-position (point)) pps-state mbeg)
958 (while (and (< (point) c-new-END)
959 (search-forward-regexp c-anchored-cpp-prefix c-new-END t))
960 ;; If we've found a "#" inside a macro/string/comment, ignore it.
961 (unless
962 (or (save-excursion
963 (goto-char (match-beginning 0))
964 (c-beginning-of-macro))
965 (progn
966 (setq pps-state
967 (parse-partial-sexp pps-position (point) nil nil pps-state)
968 pps-position (point))
969 (or (nth 3 pps-state) ; in a string?
970 (nth 4 pps-state)))) ; in a comment?
971 (goto-char (match-beginning 1))
972 (setq mbeg (point))
973 (if (> (c-syntactic-end-of-macro) mbeg)
974 (progn
975 (c-neutralize-CPP-line mbeg (point))
976 (c-set-cpp-delimiters mbeg (point)))
977 (forward-line)) ; no infinite loop with, e.g., "#//"
978 )))))
980 (defun c-before-change (beg end)
981 ;; Function to be put on `before-change-functions'. Primarily, this calls
982 ;; the language dependent `c-get-state-before-change-functions'. It is
983 ;; otherwise used only to remove stale entries from the `c-found-types'
984 ;; cache, and to record entries which a `c-after-change' function might
985 ;; confirm as stale.
987 ;; Note that this function must be FAST rather than accurate. Note
988 ;; also that it only has any effect when font locking is enabled.
989 ;; We exploit this by checking for font-lock-*-face instead of doing
990 ;; rigorous syntactic analysis.
992 ;; If either change boundary is wholly inside an identifier, delete
993 ;; it/them from the cache. Don't worry about being inside a string
994 ;; or a comment - "wrongly" removing a symbol from `c-found-types'
995 ;; isn't critical.
996 (setq c-maybe-stale-found-type nil)
997 (save-restriction
998 (save-match-data
999 (widen)
1000 (save-excursion
1001 ;; Are we inserting/deleting stuff in the middle of an identifier?
1002 (c-unfind-enclosing-token beg)
1003 (c-unfind-enclosing-token end)
1004 ;; Are we coalescing two tokens together, e.g. "fo o" -> "foo"?
1005 (when (< beg end)
1006 (c-unfind-coalesced-tokens beg end))
1007 ;; Are we (potentially) disrupting the syntactic context which
1008 ;; makes a type a type? E.g. by inserting stuff after "foo" in
1009 ;; "foo bar;", or before "foo" in "typedef foo *bar;"?
1011 ;; We search for appropriate c-type properties "near" the change.
1012 ;; First, find an appropriate boundary for this property search.
1013 (let (lim
1014 type type-pos
1015 marked-id term-pos
1016 (end1
1017 (or (and (eq (get-text-property end 'face) 'font-lock-comment-face)
1018 (previous-single-property-change end 'face))
1019 end)))
1020 (when (>= end1 beg) ; Don't hassle about changes entirely in comments.
1021 ;; Find a limit for the search for a `c-type' property
1022 (while
1023 (and (/= (skip-chars-backward "^;{}") 0)
1024 (> (point) (point-min))
1025 (memq (c-get-char-property (1- (point)) 'face)
1026 '(font-lock-comment-face font-lock-string-face))))
1027 (setq lim (max (point-min) (1- (point))))
1029 ;; Look for the latest `c-type' property before end1
1030 (when (and (> end1 (point-min))
1031 (setq type-pos
1032 (if (get-text-property (1- end1) 'c-type)
1033 end1
1034 (previous-single-property-change end1 'c-type nil lim))))
1035 (setq type (get-text-property (max (1- type-pos) lim) 'c-type))
1037 (when (memq type '(c-decl-id-start c-decl-type-start))
1038 ;; Get the identifier, if any, that the property is on.
1039 (goto-char (1- type-pos))
1040 (setq marked-id
1041 (when (looking-at "\\(\\sw\\|\\s_\\)")
1042 (c-beginning-of-current-token)
1043 (buffer-substring-no-properties (point) type-pos)))
1045 (goto-char end1)
1046 (skip-chars-forward "^;{}") ; FIXME!!! loop for comment, maybe
1047 (setq lim (point))
1048 (setq term-pos
1049 (or (next-single-property-change end 'c-type nil lim) lim))
1050 (setq c-maybe-stale-found-type
1051 (list type marked-id
1052 type-pos term-pos
1053 (buffer-substring-no-properties type-pos term-pos)
1054 (buffer-substring-no-properties beg end)))))))
1056 (if c-get-state-before-change-functions
1057 (let (open-paren-in-column-0-is-defun-start)
1058 (mapc (lambda (fn)
1059 (funcall fn beg end))
1060 c-get-state-before-change-functions)))
1062 ;; The following must be done here rather than in `c-after-change' because
1063 ;; newly inserted parens would foul up the invalidation algorithm.
1064 (c-invalidate-state-cache beg))
1066 (defvar c-in-after-change-fontification nil)
1067 (make-variable-buffer-local 'c-in-after-change-fontification)
1068 ;; A flag to prevent region expanding stuff being done twice for after-change
1069 ;; fontification.
1071 (defun c-after-change (beg end old-len)
1072 ;; Function put on `after-change-functions' to adjust various caches
1073 ;; etc. Prefer speed to finesse here, since there will be an order
1074 ;; of magnitude more calls to this function than any of the
1075 ;; functions that use the caches.
1077 ;; Note that care must be taken so that this is called before any
1078 ;; font-lock callbacks since we might get calls to functions using
1079 ;; these caches from inside them, and we must thus be sure that this
1080 ;; has already been executed.
1082 ;; This calls the language variable c-before-font-lock-functions, if non nil.
1083 ;; This typically sets `syntax-table' properties.
1085 (c-save-buffer-state (case-fold-search open-paren-in-column-0-is-defun-start)
1086 ;; When `combine-after-change-calls' is used we might get calls
1087 ;; with regions outside the current narrowing. This has been
1088 ;; observed in Emacs 20.7.
1089 (save-restriction
1090 (save-match-data ; c-recognize-<>-arglists changes match-data
1091 (widen)
1093 (when (> end (point-max))
1094 ;; Some emacsen might return positions past the end. This has been
1095 ;; observed in Emacs 20.7 when rereading a buffer changed on disk
1096 ;; (haven't been able to minimize it, but Emacs 21.3 appears to
1097 ;; work).
1098 (setq end (point-max))
1099 (when (> beg end)
1100 (setq beg end)))
1102 ;; C-y is capable of spuriously converting category properties
1103 ;; c-</>-as-paren-syntax and c-cpp-delimiter into hard syntax-table
1104 ;; properties. Remove these when it happens.
1105 (c-clear-char-property-with-value beg end 'syntax-table
1106 c-<-as-paren-syntax)
1107 (c-clear-char-property-with-value beg end 'syntax-table
1108 c->-as-paren-syntax)
1109 (c-clear-char-property-with-value beg end 'syntax-table nil)
1111 (c-trim-found-types beg end old-len) ; maybe we don't need all of these.
1112 (c-invalidate-sws-region-after beg end)
1113 ;; (c-invalidate-state-cache beg) ; moved to `c-before-change'.
1114 (c-invalidate-find-decl-cache beg)
1116 (when c-recognize-<>-arglists
1117 (c-after-change-check-<>-operators beg end))
1119 ;; (c-new-BEG c-new-END) will be the region to fontify. It may become
1120 ;; larger than (beg end).
1121 (setq c-new-BEG beg
1122 c-new-END end)
1123 (setq c-in-after-change-fontification t)
1124 (save-excursion
1125 (mapc (lambda (fn)
1126 (funcall fn beg end old-len))
1127 c-before-font-lock-functions))))))
1129 (defun c-set-fl-decl-start (pos)
1130 ;; If the beginning of the line containing POS is in the middle of a "local"
1131 ;; declaration (i.e. one which does not start outside of braces enclosing
1132 ;; POS, such as a struct), return the beginning of that declaration.
1133 ;; Otherwise return POS. Note that declarations, in this sense, can be
1134 ;; nested.
1136 ;; This function is called indirectly from font locking stuff - either from
1137 ;; c-after-change (to prepare for after-change font-locking) or from font
1138 ;; lock context (etc.) fontification.
1139 (let ((lit-limits (c-literal-limits))
1140 (new-pos pos)
1141 bod-lim bo-decl)
1142 (goto-char (c-point 'bol new-pos))
1143 (when lit-limits ; Comment or string.
1144 (goto-char (car lit-limits)))
1145 (setq bod-lim (c-determine-limit 500))
1147 (while
1148 ;; Go to a less nested declaration each time round this loop.
1149 (and
1150 (eq (car (c-beginning-of-decl-1 bod-lim)) 'same)
1151 (progn (setq bo-decl (point))
1152 ;; Are we looking at a keyword such as "template" or
1153 ;; "typedef" which can decorate a type, or the type itself?
1154 (when (or (looking-at c-prefix-spec-kwds-re)
1155 (c-forward-type t))
1156 ;; We've found another candidate position.
1157 (setq new-pos (min new-pos bo-decl))
1158 (goto-char bo-decl))
1160 ;; Try and go out a level to search again.
1161 (progn
1162 (c-backward-syntactic-ws bod-lim)
1163 (and (> (point) bod-lim)
1164 (or (memq (char-before) '(?\( ?\[))
1165 (and (eq (char-before) ?\<)
1166 (eq (c-get-char-property
1167 (1- (point)) 'syntax-table)
1168 c-<-as-paren-syntax)))))
1169 (not (bobp)))
1170 (backward-char))
1171 new-pos)) ; back over (, [, <.
1173 (defun c-change-set-fl-decl-start (_beg _end _old-len)
1174 ;; Set c-new-BEG to the beginning of a "local" declaration if it('s BOL) is
1175 ;; inside one. This is called from an after-change-function, but the
1176 ;; parameters BEG END and OLD-LEN are ignored. See `c-set-fl-decl-start'
1177 ;; for the detailed functionality.
1178 (if font-lock-mode
1179 (setq c-new-BEG (c-set-fl-decl-start c-new-BEG))))
1181 (defun c-context-set-fl-decl-start (beg end)
1182 ;; Return a cons (NEW-BEG . END), where NEW-BEG is the beginning of a
1183 ;; "local" declaration (BOL at) NEW is inside or BEG. See
1184 ;; `c-set-fl-decl-start' for the detailed functionality.
1185 (cons (c-set-fl-decl-start beg) end))
1187 (defun c-font-lock-fontify-region (beg end &optional verbose)
1188 ;; Effectively advice around `font-lock-fontify-region' which extends the
1189 ;; region (BEG END), for example, to avoid context fontification chopping
1190 ;; off the start of the context. Do not do anything if it's already been
1191 ;; done (i.e. from an after-change fontification. An example (C++) where
1192 ;; this used to happen is this:
1194 ;; template <typename T>
1197 ;; void myfunc(T* p) {}
1199 ;; Type a space in the first blank line, and the fontification of the next
1200 ;; line was fouled up by context fontification.
1201 (let ((new-beg beg) (new-end end) new-region case-fold-search
1202 open-paren-in-column-0-is-defun-start)
1203 (if c-in-after-change-fontification
1204 (setq c-in-after-change-fontification nil)
1205 (save-restriction
1206 (widen)
1207 (save-excursion
1208 (mapc (lambda (fn)
1209 (setq new-region (funcall fn new-beg new-end))
1210 (setq new-beg (car new-region) new-end (cdr new-region)))
1211 c-before-context-fontification-functions))))
1212 (funcall (default-value 'font-lock-fontify-region-function)
1213 new-beg new-end verbose)))
1215 (defun c-after-font-lock-init ()
1216 ;; Put on `font-lock-mode-hook'. This function ensures our after-change
1217 ;; function will get executed before the font-lock one.
1218 (remove-hook 'after-change-functions 'c-after-change t)
1219 (add-hook 'after-change-functions 'c-after-change nil t))
1221 (defun c-font-lock-init ()
1222 "Set up the font-lock variables for using the font-lock support in CC Mode.
1223 This does not load the font-lock package. Use after
1224 `c-basic-common-init' and after cc-fonts has been loaded.
1225 This function is called from `c-common-init', once per mode initialization."
1227 (set (make-local-variable 'font-lock-defaults)
1228 `(,(if (c-major-mode-is 'awk-mode)
1229 ;; awk-mode currently has only one font lock level.
1230 'awk-font-lock-keywords
1231 (mapcar 'c-mode-symbol
1232 '("font-lock-keywords" "font-lock-keywords-1"
1233 "font-lock-keywords-2" "font-lock-keywords-3")))
1234 nil nil
1235 ,c-identifier-syntax-modifications
1236 c-beginning-of-syntax
1237 (font-lock-mark-block-function
1238 . c-mark-function)))
1240 (make-local-variable 'font-lock-fontify-region-function)
1241 (setq font-lock-fontify-region-function 'c-font-lock-fontify-region)
1243 (if (featurep 'xemacs)
1244 (make-local-hook 'font-lock-mode-hook))
1245 (add-hook 'font-lock-mode-hook 'c-after-font-lock-init nil t))
1247 (defun c-extend-after-change-region (_beg _end _old-len)
1248 "Extend the region to be fontified, if necessary."
1249 ;; Note: the parameters are ignored here. This somewhat indirect
1250 ;; implementation exists because it is minimally different from the
1251 ;; stand-alone CC Mode which, lacking
1252 ;; font-lock-extend-after-change-region-function, is forced to use advice
1253 ;; instead.
1255 ;; Of the seven CC Mode languages, currently (2009-05) only C, C++, Objc
1256 ;; (the languages with #define) and AWK Mode make non-null use of this
1257 ;; function.
1258 (cons c-new-BEG c-new-END))
1260 ;; Connect up to `electric-indent-mode' (Emacs 24.4 and later).
1261 (defun c-electric-indent-mode-hook ()
1262 ;; Emacs has en/disabled `electric-indent-mode'. Propagate this through to
1263 ;; each CC Mode buffer.
1264 (mapc (lambda (buf)
1265 (with-current-buffer buf
1266 (when c-buffer-is-cc-mode
1267 ;; Don't use `c-toggle-electric-state' here due to recursion.
1268 (setq c-electric-flag electric-indent-mode)
1269 (c-update-modeline))))
1270 (buffer-list)))
1272 (defun c-electric-indent-local-mode-hook ()
1273 ;; Emacs has en/disabled `electric-indent-local-mode' for this buffer.
1274 ;; Propagate this through to this buffer's value of `c-electric-flag'
1275 (when c-buffer-is-cc-mode
1276 (setq c-electric-flag electric-indent-mode)
1277 (c-update-modeline)))
1280 ;; Support for C
1282 (defvar c-mode-syntax-table
1283 (funcall (c-lang-const c-make-mode-syntax-table c))
1284 "Syntax table used in c-mode buffers.")
1286 (c-define-abbrev-table 'c-mode-abbrev-table
1287 '(("else" "else" c-electric-continued-statement 0)
1288 ("while" "while" c-electric-continued-statement 0))
1289 "Abbreviation table used in c-mode buffers.")
1291 (defvar c-mode-map
1292 (let ((map (c-make-inherited-keymap)))
1293 ;; Add bindings which are only useful for C.
1294 (define-key map "\C-c\C-e" 'c-macro-expand)
1295 map)
1296 "Keymap used in c-mode buffers.")
1299 (easy-menu-define c-c-menu c-mode-map "C Mode Commands"
1300 (cons "C" (c-lang-const c-mode-menu c)))
1302 ;; In XEmacs >= 21.5 modes should add their own entries to
1303 ;; `auto-mode-alist'. The comment form of autoload is used to avoid
1304 ;; doing this on load. That since `add-to-list' prepends the value
1305 ;; which could cause it to clobber user settings. Later emacsen have
1306 ;; an append option, but it's not safe to use.
1308 ;; The extension ".C" is associated with C++ while the lowercase
1309 ;; variant goes with C. On case insensitive file systems, this means
1310 ;; that ".c" files also might open C++ mode if the C++ entry comes
1311 ;; first on `auto-mode-alist'. Thus we try to ensure that ".C" comes
1312 ;; after ".c", and since `add-to-list' adds the entry first we have to
1313 ;; add the ".C" entry first.
1314 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(cc\\|hh\\)\\'" . c++-mode))
1315 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.[ch]\\(pp\\|xx\\|\\+\\+\\)\\'" . c++-mode))
1316 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(CC?\\|HH?\\)\\'" . c++-mode))
1318 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.[ch]\\'" . c-mode))
1320 ;; NB: The following two associate yacc and lex files to C Mode, which
1321 ;; is not really suitable for those formats. Anyway, afaik there's
1322 ;; currently no better mode for them, and besides this is legacy.
1323 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.y\\(acc\\)?\\'" . c-mode))
1324 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.lex\\'" . c-mode))
1326 ;; Preprocessed files generated by C and C++ compilers.
1327 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.i\\'" . c-mode))
1328 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.ii\\'" . c++-mode))
1331 ;;;###autoload
1332 (define-derived-mode c-mode prog-mode "C"
1333 "Major mode for editing K&R and ANSI C code.
1334 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1335 c-mode buffer. This automatically sets up a mail buffer with version
1336 information already added. You just need to add a description of the
1337 problem, including a reproducible test case, and send the message.
1339 To see what version of CC Mode you are running, enter `\\[c-version]'.
1341 The hook `c-mode-common-hook' is run with no args at mode
1342 initialization, then `c-mode-hook'.
1344 Key bindings:
1345 \\{c-mode-map}"
1346 (c-initialize-cc-mode t)
1347 (set-syntax-table c-mode-syntax-table)
1348 (setq local-abbrev-table c-mode-abbrev-table
1349 abbrev-mode t)
1350 (use-local-map c-mode-map)
1351 (c-init-language-vars-for 'c-mode)
1352 (c-make-macro-with-semi-re) ; matches macro names whose expansion ends with ;
1353 (c-common-init 'c-mode)
1354 (easy-menu-add c-c-menu)
1355 (cc-imenu-init cc-imenu-c-generic-expression)
1356 (c-run-mode-hooks 'c-mode-common-hook 'c-mode-hook)
1357 (c-update-modeline))
1360 ;; Support for C++
1362 (defvar c++-mode-syntax-table
1363 (funcall (c-lang-const c-make-mode-syntax-table c++))
1364 "Syntax table used in c++-mode buffers.")
1366 (c-define-abbrev-table 'c++-mode-abbrev-table
1367 '(("else" "else" c-electric-continued-statement 0)
1368 ("while" "while" c-electric-continued-statement 0)
1369 ("catch" "catch" c-electric-continued-statement 0))
1370 "Abbreviation table used in c++-mode buffers.")
1372 (defvar c++-mode-map
1373 (let ((map (c-make-inherited-keymap)))
1374 ;; Add bindings which are only useful for C++.
1375 (define-key map "\C-c\C-e" 'c-macro-expand)
1376 (define-key map "\C-c:" 'c-scope-operator)
1377 (define-key map "<" 'c-electric-lt-gt)
1378 (define-key map ">" 'c-electric-lt-gt)
1379 map)
1380 "Keymap used in c++-mode buffers.")
1382 (easy-menu-define c-c++-menu c++-mode-map "C++ Mode Commands"
1383 (cons "C++" (c-lang-const c-mode-menu c++)))
1385 ;;;###autoload
1386 (define-derived-mode c++-mode prog-mode "C++"
1387 "Major mode for editing C++ code.
1388 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1389 c++-mode buffer. This automatically sets up a mail buffer with
1390 version information already added. You just need to add a description
1391 of the problem, including a reproducible test case, and send the
1392 message.
1394 To see what version of CC Mode you are running, enter `\\[c-version]'.
1396 The hook `c-mode-common-hook' is run with no args at mode
1397 initialization, then `c++-mode-hook'.
1399 Key bindings:
1400 \\{c++-mode-map}"
1401 (c-initialize-cc-mode t)
1402 (set-syntax-table c++-mode-syntax-table)
1403 (setq local-abbrev-table c++-mode-abbrev-table
1404 abbrev-mode t)
1405 (use-local-map c++-mode-map)
1406 (c-init-language-vars-for 'c++-mode)
1407 (c-make-macro-with-semi-re) ; matches macro names whose expansion ends with ;
1408 (c-common-init 'c++-mode)
1409 (easy-menu-add c-c++-menu)
1410 (cc-imenu-init cc-imenu-c++-generic-expression)
1411 (c-run-mode-hooks 'c-mode-common-hook 'c++-mode-hook)
1412 (c-update-modeline))
1415 ;; Support for Objective-C
1417 (defvar objc-mode-syntax-table
1418 (funcall (c-lang-const c-make-mode-syntax-table objc))
1419 "Syntax table used in objc-mode buffers.")
1421 (c-define-abbrev-table 'objc-mode-abbrev-table
1422 '(("else" "else" c-electric-continued-statement 0)
1423 ("while" "while" c-electric-continued-statement 0))
1424 "Abbreviation table used in objc-mode buffers.")
1426 (defvar objc-mode-map
1427 (let ((map (c-make-inherited-keymap)))
1428 ;; Add bindings which are only useful for Objective-C.
1429 (define-key map "\C-c\C-e" 'c-macro-expand)
1430 map)
1431 "Keymap used in objc-mode buffers.")
1433 (easy-menu-define c-objc-menu objc-mode-map "ObjC Mode Commands"
1434 (cons "ObjC" (c-lang-const c-mode-menu objc)))
1436 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.m\\'" . objc-mode))
1438 ;;;###autoload
1439 (define-derived-mode objc-mode prog-mode "ObjC"
1440 "Major mode for editing Objective C code.
1441 To submit a problem report, enter `\\[c-submit-bug-report]' from an
1442 objc-mode buffer. This automatically sets up a mail buffer with
1443 version information already added. You just need to add a description
1444 of the problem, including a reproducible test case, and send the
1445 message.
1447 To see what version of CC Mode you are running, enter `\\[c-version]'.
1449 The hook `c-mode-common-hook' is run with no args at mode
1450 initialization, then `objc-mode-hook'.
1452 Key bindings:
1453 \\{objc-mode-map}"
1454 (c-initialize-cc-mode t)
1455 (set-syntax-table objc-mode-syntax-table)
1456 (setq local-abbrev-table objc-mode-abbrev-table
1457 abbrev-mode t)
1458 (use-local-map objc-mode-map)
1459 (c-init-language-vars-for 'objc-mode)
1460 (c-make-macro-with-semi-re) ; matches macro names whose expansion ends with ;
1461 (c-common-init 'objc-mode)
1462 (easy-menu-add c-objc-menu)
1463 (cc-imenu-init nil 'cc-imenu-objc-function)
1464 (c-run-mode-hooks 'c-mode-common-hook 'objc-mode-hook)
1465 (c-update-modeline))
1468 ;; Support for Java
1470 (defvar java-mode-syntax-table
1471 (funcall (c-lang-const c-make-mode-syntax-table java))
1472 "Syntax table used in java-mode buffers.")
1474 (c-define-abbrev-table 'java-mode-abbrev-table
1475 '(("else" "else" c-electric-continued-statement 0)
1476 ("while" "while" c-electric-continued-statement 0)
1477 ("catch" "catch" c-electric-continued-statement 0)
1478 ("finally" "finally" c-electric-continued-statement 0))
1479 "Abbreviation table used in java-mode buffers.")
1481 (defvar java-mode-map
1482 (let ((map (c-make-inherited-keymap)))
1483 ;; Add bindings which are only useful for Java.
1484 map)
1485 "Keymap used in java-mode buffers.")
1487 ;; Regexp trying to describe the beginning of a Java top-level
1488 ;; definition. This is not used by CC Mode, nor is it maintained
1489 ;; since it's practically impossible to write a regexp that reliably
1490 ;; matches such a construct. Other tools are necessary.
1491 (defconst c-Java-defun-prompt-regexp
1492 "^[ \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-*")
1494 (easy-menu-define c-java-menu java-mode-map "Java Mode Commands"
1495 (cons "Java" (c-lang-const c-mode-menu java)))
1497 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.java\\'" . java-mode))
1499 ;;;###autoload
1500 (define-derived-mode java-mode prog-mode "Java"
1501 "Major mode for editing Java code.
1502 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1503 java-mode buffer. This automatically sets up a mail buffer with
1504 version information already added. You just need to add a description
1505 of the problem, including a reproducible test case, and send the
1506 message.
1508 To see what version of CC Mode you are running, enter `\\[c-version]'.
1510 The hook `c-mode-common-hook' is run with no args at mode
1511 initialization, then `java-mode-hook'.
1513 Key bindings:
1514 \\{java-mode-map}"
1515 (c-initialize-cc-mode t)
1516 (set-syntax-table java-mode-syntax-table)
1517 (setq local-abbrev-table java-mode-abbrev-table
1518 abbrev-mode t)
1519 (use-local-map java-mode-map)
1520 (c-init-language-vars-for 'java-mode)
1521 (c-common-init 'java-mode)
1522 (easy-menu-add c-java-menu)
1523 (cc-imenu-init cc-imenu-java-generic-expression)
1524 (c-run-mode-hooks 'c-mode-common-hook 'java-mode-hook)
1525 (c-update-modeline))
1528 ;; Support for CORBA's IDL language
1530 (defvar idl-mode-syntax-table
1531 (funcall (c-lang-const c-make-mode-syntax-table idl))
1532 "Syntax table used in idl-mode buffers.")
1534 (c-define-abbrev-table 'idl-mode-abbrev-table nil
1535 "Abbreviation table used in idl-mode buffers.")
1537 (defvar idl-mode-map
1538 (let ((map (c-make-inherited-keymap)))
1539 ;; Add bindings which are only useful for IDL.
1540 map)
1541 "Keymap used in idl-mode buffers.")
1543 (easy-menu-define c-idl-menu idl-mode-map "IDL Mode Commands"
1544 (cons "IDL" (c-lang-const c-mode-menu idl)))
1546 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.idl\\'" . idl-mode))
1548 ;;;###autoload
1549 (define-derived-mode idl-mode prog-mode "IDL"
1550 "Major mode for editing CORBA's IDL, PSDL and CIDL code.
1551 To submit a problem report, enter `\\[c-submit-bug-report]' from an
1552 idl-mode buffer. This automatically sets up a mail buffer with
1553 version information already added. You just need to add a description
1554 of the problem, including a reproducible test case, and send the
1555 message.
1557 To see what version of CC Mode you are running, enter `\\[c-version]'.
1559 The hook `c-mode-common-hook' is run with no args at mode
1560 initialization, then `idl-mode-hook'.
1562 Key bindings:
1563 \\{idl-mode-map}"
1564 (c-initialize-cc-mode t)
1565 (set-syntax-table idl-mode-syntax-table)
1566 (setq local-abbrev-table idl-mode-abbrev-table)
1567 (use-local-map idl-mode-map)
1568 (c-init-language-vars-for 'idl-mode)
1569 (c-common-init 'idl-mode)
1570 (easy-menu-add c-idl-menu)
1571 ;;(cc-imenu-init cc-imenu-idl-generic-expression) ;TODO
1572 (c-run-mode-hooks 'c-mode-common-hook 'idl-mode-hook)
1573 (c-update-modeline))
1576 ;; Support for Pike
1578 (defvar pike-mode-syntax-table
1579 (funcall (c-lang-const c-make-mode-syntax-table pike))
1580 "Syntax table used in pike-mode buffers.")
1582 (c-define-abbrev-table 'pike-mode-abbrev-table
1583 '(("else" "else" c-electric-continued-statement 0)
1584 ("while" "while" c-electric-continued-statement 0))
1585 "Abbreviation table used in pike-mode buffers.")
1587 (defvar pike-mode-map
1588 (let ((map (c-make-inherited-keymap)))
1589 ;; Additional bindings.
1590 (define-key map "\C-c\C-e" 'c-macro-expand)
1591 map)
1592 "Keymap used in pike-mode buffers.")
1594 (easy-menu-define c-pike-menu pike-mode-map "Pike Mode Commands"
1595 (cons "Pike" (c-lang-const c-mode-menu pike)))
1597 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.\\(u?lpc\\|pike\\|pmod\\(\\.in\\)?\\)\\'" . pike-mode))
1598 ;;;###autoload (add-to-list 'interpreter-mode-alist '("pike" . pike-mode))
1600 ;;;###autoload
1601 (define-derived-mode pike-mode prog-mode "Pike"
1602 "Major mode for editing Pike code.
1603 To submit a problem report, enter `\\[c-submit-bug-report]' from a
1604 pike-mode buffer. This automatically sets up a mail buffer with
1605 version information already added. You just need to add a description
1606 of the problem, including a reproducible test case, and send the
1607 message.
1609 To see what version of CC Mode you are running, enter `\\[c-version]'.
1611 The hook `c-mode-common-hook' is run with no args at mode
1612 initialization, then `pike-mode-hook'.
1614 Key bindings:
1615 \\{pike-mode-map}"
1616 (c-initialize-cc-mode t)
1617 (set-syntax-table pike-mode-syntax-table)
1618 (setq local-abbrev-table pike-mode-abbrev-table
1619 abbrev-mode t)
1620 (use-local-map pike-mode-map)
1621 (c-init-language-vars-for 'pike-mode)
1622 (c-common-init 'pike-mode)
1623 (easy-menu-add c-pike-menu)
1624 ;;(cc-imenu-init cc-imenu-pike-generic-expression) ;TODO
1625 (c-run-mode-hooks 'c-mode-common-hook 'pike-mode-hook)
1626 (c-update-modeline))
1629 ;; Support for AWK
1631 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.awk\\'" . awk-mode))
1632 ;;;###autoload (add-to-list 'interpreter-mode-alist '("awk" . awk-mode))
1633 ;;;###autoload (add-to-list 'interpreter-mode-alist '("mawk" . awk-mode))
1634 ;;;###autoload (add-to-list 'interpreter-mode-alist '("nawk" . awk-mode))
1635 ;;;###autoload (add-to-list 'interpreter-mode-alist '("gawk" . awk-mode))
1637 (c-define-abbrev-table 'awk-mode-abbrev-table
1638 '(("else" "else" c-electric-continued-statement 0)
1639 ("while" "while" c-electric-continued-statement 0))
1640 "Abbreviation table used in awk-mode buffers.")
1642 (defvar awk-mode-map
1643 (let ((map (c-make-inherited-keymap)))
1644 ;; Add bindings which are only useful for awk.
1645 (define-key map "#" 'self-insert-command)
1646 (define-key map "/" 'self-insert-command)
1647 (define-key map "*" 'self-insert-command)
1648 (define-key map "\C-c\C-n" 'undefined) ; #if doesn't exist in awk.
1649 (define-key map "\C-c\C-p" 'undefined)
1650 (define-key map "\C-c\C-u" 'undefined)
1651 (define-key map "\M-a" 'c-beginning-of-statement) ; 2003/10/7
1652 (define-key map "\M-e" 'c-end-of-statement) ; 2003/10/7
1653 (define-key map "\C-\M-a" 'c-awk-beginning-of-defun)
1654 (define-key map "\C-\M-e" 'c-awk-end-of-defun)
1655 map)
1656 "Keymap used in awk-mode buffers.")
1658 (easy-menu-define c-awk-menu awk-mode-map "AWK Mode Commands"
1659 (cons "AWK" (c-lang-const c-mode-menu awk)))
1661 ;; (require 'cc-awk) brings these in.
1662 (defvar awk-mode-syntax-table)
1663 (declare-function c-awk-unstick-NL-prop "cc-awk" ())
1665 ;;;###autoload
1666 (define-derived-mode awk-mode prog-mode "AWK"
1667 "Major mode for editing AWK code.
1668 To submit a problem report, enter `\\[c-submit-bug-report]' from an
1669 awk-mode buffer. This automatically sets up a mail buffer with version
1670 information already added. You just need to add a description of the
1671 problem, including a reproducible test case, and send the message.
1673 To see what version of CC Mode you are running, enter `\\[c-version]'.
1675 The hook `c-mode-common-hook' is run with no args at mode
1676 initialization, then `awk-mode-hook'.
1678 Key bindings:
1679 \\{awk-mode-map}"
1680 ;; We need the next line to stop the macro defining
1681 ;; `awk-mode-syntax-table'. This would mask the real table which is
1682 ;; declared in cc-awk.el and hasn't yet been loaded.
1683 :syntax-table nil
1684 (require 'cc-awk) ; Added 2003/6/10.
1685 (c-initialize-cc-mode t)
1686 (set-syntax-table awk-mode-syntax-table)
1687 (setq local-abbrev-table awk-mode-abbrev-table
1688 abbrev-mode t)
1689 (use-local-map awk-mode-map)
1690 (c-init-language-vars-for 'awk-mode)
1691 (c-common-init 'awk-mode)
1692 (c-awk-unstick-NL-prop)
1694 (c-run-mode-hooks 'c-mode-common-hook 'awk-mode-hook)
1695 (c-update-modeline))
1698 ;; bug reporting
1700 (defconst c-mode-help-address
1701 "bug-cc-mode@gnu.org"
1702 "Address(es) for CC Mode bug reports.")
1704 (defun c-version ()
1705 "Echo the current version of CC Mode in the minibuffer."
1706 (interactive)
1707 (message "Using CC Mode version %s" c-version)
1708 (c-keep-region-active))
1710 (define-obsolete-variable-alias 'c-prepare-bug-report-hooks
1711 'c-prepare-bug-report-hook "24.3")
1712 (defvar c-prepare-bug-report-hook nil)
1714 ;; Dynamic variables used by reporter.
1715 (defvar reporter-prompt-for-summary-p)
1716 (defvar reporter-dont-compact-list)
1718 (defun c-submit-bug-report ()
1719 "Submit via mail a bug report on CC Mode."
1720 (interactive)
1721 (require 'reporter)
1722 ;; load in reporter
1723 (let ((reporter-prompt-for-summary-p t)
1724 (reporter-dont-compact-list '(c-offsets-alist))
1725 (style c-indentation-style)
1726 (c-features c-emacs-features))
1727 (and
1728 (if (y-or-n-p "Do you want to submit a report on CC Mode? ")
1729 t (message "") nil)
1730 (reporter-submit-bug-report
1731 c-mode-help-address
1732 (concat "CC Mode " c-version " (" mode-name ")")
1733 (let ((vars (append
1734 c-style-variables
1735 '(c-buffer-is-cc-mode
1736 c-tab-always-indent
1737 c-syntactic-indentation
1738 c-syntactic-indentation-in-macros
1739 c-ignore-auto-fill
1740 c-auto-align-backslashes
1741 c-backspace-function
1742 c-delete-function
1743 c-electric-pound-behavior
1744 c-default-style
1745 c-enable-xemacs-performance-kludge-p
1746 c-old-style-variable-behavior
1747 defun-prompt-regexp
1748 tab-width
1749 comment-column
1750 parse-sexp-ignore-comments
1751 parse-sexp-lookup-properties
1752 lookup-syntax-properties
1753 ;; A brain-damaged XEmacs only variable that, if
1754 ;; set to nil can cause all kinds of chaos.
1755 signal-error-on-buffer-boundary
1756 ;; Variables that affect line breaking and comments.
1757 auto-fill-mode
1758 auto-fill-function
1759 filladapt-mode
1760 comment-multi-line
1761 comment-start-skip
1762 fill-prefix
1763 fill-column
1764 paragraph-start
1765 adaptive-fill-mode
1766 adaptive-fill-regexp)
1767 nil)))
1768 (mapc (lambda (var) (unless (boundp var)
1769 (setq vars (delq var vars))))
1770 '(signal-error-on-buffer-boundary
1771 filladapt-mode
1772 defun-prompt-regexp
1773 font-lock-mode
1774 auto-fill-mode
1775 font-lock-maximum-decoration
1776 parse-sexp-lookup-properties
1777 lookup-syntax-properties))
1778 vars)
1779 (lambda ()
1780 (run-hooks 'c-prepare-bug-report-hook)
1781 (insert (format "Buffer Style: %s\nc-emacs-features: %s\n"
1782 style c-features)))))))
1785 (cc-provide 'cc-mode)
1787 ;;; cc-mode.el ends here