]> code.delx.au - gnu-emacs/blob - lisp/progmodes/cc-mode.el
5d8af2399727ac9c1192c75bb3c897622559b976
[gnu-emacs] / lisp / progmodes / cc-mode.el
1 ;;; cc-mode.el --- major mode for editing C and similar languages
2
3 ;; Copyright (C) 1985, 1987, 1992-2015 Free Software Foundation, Inc.
4
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
14
15 ;; This file is part of GNU Emacs.
16
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.
21
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.
26
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/>.
29
30 ;;; Commentary:
31
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.
35
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.
41
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.
48
49 ;; CC Mode's immediate ancestors were, c++-mode.el, cplus-md.el, and
50 ;; cplus-md1.el..
51
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!
59
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.
63
64 ;; You can get the latest version of CC Mode, including PostScript
65 ;; documentation and separate individual files from:
66 ;;
67 ;; http://cc-mode.sourceforge.net/
68 ;;
69 ;; You can join a moderated CC Mode announcement-only mailing list by
70 ;; visiting
71 ;;
72 ;; http://lists.sourceforge.net/mailman/listinfo/cc-mode-announce
73
74 ;;; Code:
75
76 ;; For Emacs < 22.2.
77 (eval-and-compile
78 (unless (fboundp 'declare-function) (defmacro declare-function (&rest _r))))
79
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)))
87
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)
97
98 ;; Silence the compiler.
99 (cc-bytecomp-defvar adaptive-fill-first-line-regexp) ; Emacs
100 (cc-bytecomp-defun run-mode-hooks) ; Emacs 21.1
101
102 ;; We set this variable during mode init, yet we don't require
103 ;; font-lock.
104 (cc-bytecomp-defvar font-lock-defaults)
105
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)
109
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.
113 ; '
114 (require 'cc-fonts) ;)
115
116 \f
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
119 ;; use:
120 ;;
121 ;; (require 'cc-mode)
122 ;;
123 ;; And in the major mode function:
124 ;;
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)
128 ;;
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'.
135 ;;
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.
138 ;;
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>.
142
143 (defun c-leave-cc-mode-mode ()
144 (setq c-buffer-is-cc-mode nil))
145
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))))
160
161 ;;;###autoload
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."
169
170 (setq c-buffer-is-cc-mode t)
171
172 (let ((initprop 'cc-mode-is-initialized)
173 c-initialization-ok)
174 (unless (get 'c-initialize-cc-mode initprop)
175 (unwind-protect
176 (progn
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))))
192
193 (unless new-style-init
194 (c-init-language-vars-for 'c-mode)))
195
196 \f
197 ;;; Common routines.
198
199 (defvar c-mode-base-map ()
200 "Keymap shared by all CC Mode related modes.")
201
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.
207 (cond
208 ;; Emacs
209 ((cc-bytecomp-fboundp 'set-keymap-parent)
210 (set-keymap-parent map c-mode-base-map))
211 ;; XEmacs
212 ((fboundp 'set-keymap-parents)
213 (set-keymap-parents map c-mode-base-map))
214 ;; incompatible
215 (t (error "CC Mode is incompatible with this version of Emacs")))
216 map))
217
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)))))
229 (while defs
230 (condition-case nil
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)
236
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
241 (progn
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)))
250
251 (if c-mode-base-map
252 nil
253
254 (setq c-mode-base-map (make-sparse-keymap))
255
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
264 'c-end-of-statement
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
271 ;; here?
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))
280
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))
286
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)
290
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)
305
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.
316
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
333 ;; as appropriate.
334 (add-hook 'normal-erase-is-backspace-hook 'c-bind-special-erase-keys)
335 (c-bind-special-erase-keys))
336
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))
351
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)
362
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)
377 )
378
379 ;; We don't require the outline package, but we configure it a bit anyway.
380 (cc-bytecomp-defvar outline-level)
381
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
385 instead of \"C\".
386
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)))
390
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)))))
399
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.
407 ;;
408 ;; (2007-02-12): The macro `combine-after-change-calls' ISN'T used any
409 ;; more.
410
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
414 ;; id, else nil.
415 (save-excursion
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))
422 t))))
423
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
428 ;; be present.
429 (or (c-partial-ws-p beg end)
430 (save-excursion
431 (progn
432 (goto-char beg)
433 (or (eq beg (point-min))
434 (c-skip-ws-backward (1- beg))
435 (/= (point) beg)
436 (= (c-backward-token-2) 1)
437 (c-unfind-type (buffer-substring-no-properties
438 (point) beg)))
439 (goto-char end)
440 (or (eq end (point-max))
441 (c-skip-ws-forward (1+ end))
442 (/= (point) end)
443 (progn (forward-char) (c-end-of-current-token) nil)
444 (c-unfind-type (buffer-substring-no-properties
445 end (point))))))))
446
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:
450 ;;
451 ;; (c-decl-id-start "foo" 97 107 " (* ooka) " "o"), where
452 ;;
453 ;; o - `c-decl-id-start' is the c-type text property value at buffer
454 ;; pos 96.
455 ;;
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.
459 ;;
460 ;; o - " (* ooka) " is the (before change) buffer portion containing
461 ;; the suspect type (here "ooka").
462 ;;
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)
467
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.
475
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.
480
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'.
484
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."
488
489 (setq c-buffer-is-cc-mode mode)
490
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)
496
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)
503
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)
512
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)))))
520
521 ;; For the benefit of adaptive file, which otherwise mis-fills.
522 (setq fill-paragraph-handle-comment nil)
523
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)
528
529 ;; Initialize the cache of brace pairs, and opening braces/brackets/parens.
530 (c-state-cache-init)
531
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
537 ;; that.
538 ;;
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.
542
543 ;; Emacs.
544 (when (boundp 'parse-sexp-lookup-properties)
545 (set (make-local-variable 'parse-sexp-lookup-properties) t))
546
547 ;; Same as above for XEmacs.
548 (when (boundp 'lookup-syntax-properties)
549 (set (make-local-variable 'lookup-syntax-properties) t)))
550
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)))
560
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))
570
571 (c-clear-found-types)
572
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)
584 c-indentation-style
585 (not (string-equal c-indentation-style
586 style)))))))
587 (c-setup-paragraph-variables)
588
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))
592
593 ;; setup the comment indent variable in a Emacs version portable way
594 (set (make-local-variable 'comment-indent-function) 'c-comment-indent)
595
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))
604
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)))
610 (c-update-modeline)
611
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.
624
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.
630 (font-lock-mode 0)
631 (font-lock-mode 1)))
632
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'.
637 (defvar c-new-BEG 0)
638 (make-variable-buffer-local 'c-new-BEG)
639 (defvar c-new-END 0)
640 (make-variable-buffer-local 'c-new-END)
641
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.
648
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."
652
653 (unless mode
654 ;; Called from an old third party package. The fallback is to
655 ;; initialize for C.
656 (c-init-language-vars-for 'c-mode))
657
658 (c-basic-common-init mode c-default-style)
659 (when mode
660 ;; Only initialize font locking if we aren't called from an old package.
661 (c-font-lock-init))
662
663 ;; Starting a mode is a sort of "change". So call the change functions...
664 (save-restriction
665 (widen)
666 (setq c-new-BEG (point-min))
667 (setq c-new-END (point-max))
668 (save-excursion
669 (let (before-change-functions after-change-functions)
670 (mapc (lambda (fn)
671 (funcall fn (point-min) (point-max)))
672 c-get-state-before-change-functions)
673 (mapc (lambda (fn)
674 (funcall fn (point-min) (point-max)
675 (- (point-max) (point-min))))
676 c-before-font-lock-functions))))
677
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)
681 (lambda ()
682 (or (c-cpp-define-name) (c-defun-name))))
683 (let ((rfn (assq mode c-require-final-newline)))
684 (when rfn
685 (if (boundp 'mode-require-final-newline)
686 (and (cdr rfn)
687 (set (make-local-variable 'require-final-newline)
688 mode-require-final-newline))
689 (set (make-local-variable 'require-final-newline) (cdr rfn))))))
690
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))
695 (while elt-ptr
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))))
700 cownt))
701
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.
706
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))))
712 (when mode-cons
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)))
716 (when stile
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)))
724 (c-set-style stile
725 (and (= cfs-in-file-and-dir-count cfs-in-dir-count)
726 'keep-defaults)))
727 (c-set-style stile)))
728 (when offsets
729 (mapc
730 (lambda (langentry)
731 (let ((langelem (car langentry))
732 (offset (cdr langentry)))
733 (c-set-offset langelem offset)))
734 offsets)))))
735
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.
742 ;;
743 ;; Most of the code here is derived from Emacs 21.3's `hack-local-variables'
744 ;; in files.el.
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")
754 (or (eolp)
755 (setq suffix (buffer-substring (point)
756 (progn (end-of-line) (point)))))
757 (goto-char (match-beginning 0))
758 (or (bolp)
759 (setq prefix
760 (buffer-substring (point)
761 (progn (beginning-of-line) (point)))))
762
763 (while (search-forward-regexp
764 (concat "^[ \t]*"
765 (regexp-quote prefix)
766 "\\(mode\\|eval\\):.*"
767 (regexp-quote suffix)
768 "$")
769 nil t)
770 (forward-line 0)
771 (delete-region (point) (progn (forward-line) (point)))))
772
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)
776 (forward-line 0)
777 (bobp)))
778 (goto-char (point-min))
779 (unless (eobp)
780 (delete-region (point) (progn (forward-line) (point)))))))
781
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'.
788
789 Note that the style variables are always made local to the buffer."
790
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))
795 (when c-file-style
796 (or (stringp c-file-style)
797 (error "c-file-style is not a string"))
798 (c-set-style c-file-style))
799
800 (and c-file-offsets
801 (mapc
802 (lambda (langentry)
803 (let ((langelem (car langentry))
804 (offset (cdr langentry)))
805 (c-set-offset langelem offset)))
806 c-file-offsets))
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).
814 ;; ACM, 2005/11/2.
815 ;;
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
819 ;; section.
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))
825 nil))))
826
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))
830
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))))
837
838 \f
839 ;;; Change hooks, linking with Font Lock and electric-indent-mode.
840
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.
844 (defvar c-old-BOM 0)
845 (make-variable-buffer-local 'c-old-BOM)
846 (defvar c-old-EOM 0)
847 (make-variable-buffer-local 'c-old-EOM)
848
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.
853 ;;
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
857 ;; hook.
858 (memq (cadr (backtrace-frame 3))
859 '(put-text-property remove-list-of-text-properties)))
860
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.
864 ;;
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
867 ;; meaningless.
868 ;;
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.
872 (goto-char beg)
873 (c-beginning-of-macro)
874 (setq c-old-BOM (point))
875
876 (goto-char end)
877 (when (c-beginning-of-macro)
878 (c-end-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)))
882
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.
889 ;;
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.
894 (setq new-beg
895 (min begg
896 (if (setq limits (c-state-literal-at (point)))
897 (cdr limits) ; go forward out of any string or comment.
898 (point))))
899
900 (goto-char endd)
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)
904 (c-end-of-macro))
905 (setq new-end (max endd
906 (if old-len
907 (+ (- c-old-EOM old-len) (- endd begg))
908 c-old-EOM)
909 (point)))
910 (cons new-beg new-end)))
911
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
916 ;; this region.
917 ;;
918 ;; These are unmatched string delimiters, or unmatched
919 ;; parens/brackets/braces. An unclosed comment is regarded as valid, NOT
920 ;; obtrusive.
921 (save-excursion
922 (let (s)
923 (while
924 (progn
925 (setq s (parse-partial-sexp beg end -1))
926 (cond
927 ((< (nth 0 s) 0) ; found an unmated ),},]
928 (c-put-char-property (1- (point)) 'syntax-table '(1))
929 t)
930 ((nth 3 s) ; In a string
931 (c-put-char-property (nth 8 s) 'syntax-table '(1))
932 t)
933 ((> (nth 0 s) 0) ; In a (,{,[
934 (c-put-char-property (nth 1 s) 'syntax-table '(1))
935 t)
936 (t nil)))))))
937
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.
942 ;;
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'.
947 ;;
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.
952 ;;
953 ;; That is, set syntax-table properties on characters that would otherwise
954 ;; interact syntactically with those outside the CPP line(s).
955 ;;
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'.
959 ;;
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.
962 ;;
963 ;; This function is in the C/C++/ObjC value of `c-before-font-lock-functions'.
964 ;;
965 ;; Note: SPEED _MATTERS_ IN THIS FUNCTION!!!
966 ;;
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))
978
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
984
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.
992 (unless
993 (or (save-excursion
994 (goto-char (match-beginning 0))
995 (let ((here (point)))
996 (and (save-match-data (c-beginning-of-macro))
997 (< (point) here))))
998 (progn
999 (setq pps-state
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))
1005 (setq mbeg (point))
1006 (if (> (c-syntactic-end-of-macro) mbeg)
1007 (progn
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., "#//"
1013 )))))
1014
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.
1021 ;;
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.
1026
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'
1030 ;; isn't critical.
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)
1036 (save-restriction
1037 (save-match-data
1038 (widen)
1039 (save-excursion
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"?
1044 (when (< beg end)
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;"?
1049 ;;
1050 ;; We search for appropriate c-type properties "near" the change.
1051 ;; First, find an appropriate boundary for this property search.
1052 (let (lim
1053 type type-pos
1054 marked-id term-pos
1055 (end1
1056 (or (and (eq (get-text-property end 'face)
1057 'font-lock-comment-face)
1058 (previous-single-property-change end 'face))
1059 end)))
1060 (when (>= end1 beg) ; Don't hassle about changes entirely in comments.
1061 ;; Find a limit for the search for a `c-type' property
1062 (while
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))))
1068
1069 ;; Look for the latest `c-type' property before end1
1070 (when (and (> end1 (point-min))
1071 (setq type-pos
1072 (if (get-text-property (1- end1) 'c-type)
1073 end1
1074 (previous-single-property-change end1 'c-type
1075 nil lim))))
1076 (setq type (get-text-property (max (1- type-pos) lim) 'c-type))
1077
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))
1081 (setq marked-id
1082 (when (looking-at "\\(\\sw\\|\\s_\\)")
1083 (c-beginning-of-current-token)
1084 (buffer-substring-no-properties (point) type-pos)))
1085
1086 (goto-char end1)
1087 (skip-chars-forward "^;{}") ;FIXME!!! loop for comment, maybe
1088 (setq lim (point))
1089 (setq term-pos
1090 (or (c-next-single-property-change end 'c-type nil lim)
1091 lim))
1092 (setq c-maybe-stale-found-type
1093 (list type marked-id
1094 type-pos term-pos
1095 (buffer-substring-no-properties type-pos
1096 term-pos)
1097 (buffer-substring-no-properties beg end)))))))
1098
1099 (if c-get-state-before-change-functions
1100 (mapc (lambda (fn)
1101 (funcall fn beg end))
1102 c-get-state-before-change-functions))
1103 )))
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)))
1107
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
1111 ;; fontification.
1112
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.
1118 ;;
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.
1123 ;;
1124 ;; This calls the language variable c-before-font-lock-functions, if non nil.
1125 ;; This typically sets `syntax-table' properties.
1126
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)
1130
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.
1137 (save-restriction
1138 (save-match-data ; c-recognize-<>-arglists changes match-data
1139 (widen)
1140
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
1145 ;; work).
1146 (setq end (point-max))
1147 (when (> beg end)
1148 (setq beg end)))
1149
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)))
1160
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)
1165
1166 (when c-recognize-<>-arglists
1167 (c-after-change-check-<>-operators beg end))
1168
1169 (setq c-in-after-change-fontification t)
1170 (save-excursion
1171 (mapc (lambda (fn)
1172 (funcall fn beg end old-len))
1173 c-before-font-lock-functions)))))))
1174
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
1180 ;; nested.
1181 ;;
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))
1186 (new-pos pos)
1187 bod-lim bo-decl)
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))
1192
1193 (while
1194 ;; Go to a less nested declaration each time round this loop.
1195 (and
1196 (eq (car (c-beginning-of-decl-1 bod-lim)) 'same)
1197 (> (point) bod-lim)
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)
1202 (c-forward-type t))
1203 ;; We've found another candidate position.
1204 (setq new-pos (min new-pos bo-decl))
1205 (goto-char bo-decl))
1206 t)
1207 ;; Try and go out a level to search again.
1208 (progn
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)))))
1216 (not (bobp)))
1217 (backward-char)) ; back over (, [, <.
1218 (and (/= new-pos pos) new-pos)))
1219
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.
1226 ;;
1227 ;; This is called from an after-change-function, but the parameters BEG END
1228 ;; and OLD-LEN are not used.
1229 (if font-lock-mode
1230 (setq c-new-BEG
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))))
1233
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)))
1240
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.
1245 (save-restriction
1246 (widen)
1247 (save-excursion
1248 (let ((new-beg beg) (new-end end) new-region)
1249 (mapc (lambda (fn)
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)
1253 new-region))))
1254
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:
1261 ;;
1262 ;; template <typename T>
1263 ;;
1264 ;;
1265 ;; void myfunc(T* p) {}
1266 ;;
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
1274 ;; separately.
1275 (progn
1276 (if (<= beg c-new-BEG)
1277 (setq c-in-after-change-fontification nil))
1278 (setq new-beg
1279 (if (and (>= beg (c-point 'bol c-new-BEG))
1280 (<= beg 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
1287 ;; fontification.
1288 c-new-BEG
1289 (setq new-region (c-before-context-fl-expand-region beg end)
1290 new-end (cdr new-region))
1291 (car new-region)))
1292 (setq new-end
1293 (if (and (>= end (c-point 'bol c-new-END))
1294 (<= end c-new-END))
1295 c-new-END
1296 (or 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)))
1303
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)))
1310
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."
1316
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")))
1324 nil nil
1325 ,c-identifier-syntax-modifications
1326 c-beginning-of-syntax
1327 (font-lock-mark-block-function
1328 . c-mark-function)))
1329
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))
1337
1338 (make-local-variable 'font-lock-fontify-region-function)
1339 (setq font-lock-fontify-region-function 'c-font-lock-fontify-region)
1340
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))
1344
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
1352 ;; instead.
1353 ;;
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
1356 ;; function.
1357 (when (eq font-lock-support-mode 'jit-lock-mode)
1358 (save-restriction
1359 (widen)
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))
1366
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
1372 (save-excursion
1373 (ad-set-arg 1 c-new-END) ; end
1374 (ad-set-arg 0 c-new-BEG))))) ; beg
1375
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))
1381
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.
1386 (mapc (lambda (buf)
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))))
1392 (buffer-list)))
1393
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)))
1400
1401 \f
1402 ;; Support for C
1403
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.")
1407
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.")
1412
1413 (defvar c-mode-map
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)
1417 map)
1418 "Keymap used in c-mode buffers.")
1419
1420
1421 (easy-menu-define c-c-menu c-mode-map "C Mode Commands"
1422 (cons "C" (c-lang-const c-mode-menu c)))
1423
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.
1429
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))
1439
1440 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.[ch]\\'" . c-mode))
1441
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))
1447
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))
1451
1452 (unless (fboundp 'prog-mode) (defalias 'prog-mode 'fundamental-mode))
1453
1454 ;;;###autoload
1455 (define-derived-mode c-mode prog-mode "C"
1456 "Major mode for editing C code.
1457
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.
1462
1463 To see what version of CC Mode you are running, enter `\\[c-version]'.
1464
1465 The hook `c-mode-common-hook' is run with no args at mode
1466 initialization, then `c-mode-hook'.
1467
1468 Key bindings:
1469 \\{c-mode-map}"
1470 (c-initialize-cc-mode t)
1471 (set-syntax-table c-mode-syntax-table)
1472 (setq local-abbrev-table c-mode-abbrev-table
1473 abbrev-mode t)
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))
1482
1483 \f
1484 ;; Support for C++
1485
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.")
1489
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.")
1495
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)
1503 map)
1504 "Keymap used in c++-mode buffers.")
1505
1506 (easy-menu-define c-c++-menu c++-mode-map "C++ Mode Commands"
1507 (cons "C++" (c-lang-const c-mode-menu c++)))
1508
1509 ;;;###autoload
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
1516 message.
1517
1518 To see what version of CC Mode you are running, enter `\\[c-version]'.
1519
1520 The hook `c-mode-common-hook' is run with no args at mode
1521 initialization, then `c++-mode-hook'.
1522
1523 Key bindings:
1524 \\{c++-mode-map}"
1525 (c-initialize-cc-mode t)
1526 (set-syntax-table c++-mode-syntax-table)
1527 (setq local-abbrev-table c++-mode-abbrev-table
1528 abbrev-mode t)
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))
1537
1538 \f
1539 ;; Support for Objective-C
1540
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.")
1544
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.")
1549
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)
1554 map)
1555 "Keymap used in objc-mode buffers.")
1556
1557 (easy-menu-define c-objc-menu objc-mode-map "ObjC Mode Commands"
1558 (cons "ObjC" (c-lang-const c-mode-menu objc)))
1559
1560 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.m\\'" . objc-mode))
1561
1562 ;;;###autoload
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
1569 message.
1570
1571 To see what version of CC Mode you are running, enter `\\[c-version]'.
1572
1573 The hook `c-mode-common-hook' is run with no args at mode
1574 initialization, then `objc-mode-hook'.
1575
1576 Key bindings:
1577 \\{objc-mode-map}"
1578 (c-initialize-cc-mode t)
1579 (set-syntax-table objc-mode-syntax-table)
1580 (setq local-abbrev-table objc-mode-abbrev-table
1581 abbrev-mode t)
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))
1590
1591 \f
1592 ;; Support for Java
1593
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.")
1597
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.")
1604
1605 (defvar java-mode-map
1606 (let ((map (c-make-inherited-keymap)))
1607 ;; Add bindings which are only useful for Java.
1608 map)
1609 "Keymap used in java-mode buffers.")
1610
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-*")
1617
1618 (easy-menu-define c-java-menu java-mode-map "Java Mode Commands"
1619 (cons "Java" (c-lang-const c-mode-menu java)))
1620
1621 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.java\\'" . java-mode))
1622
1623 ;;;###autoload
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
1630 message.
1631
1632 To see what version of CC Mode you are running, enter `\\[c-version]'.
1633
1634 The hook `c-mode-common-hook' is run with no args at mode
1635 initialization, then `java-mode-hook'.
1636
1637 Key bindings:
1638 \\{java-mode-map}"
1639 (c-initialize-cc-mode t)
1640 (set-syntax-table java-mode-syntax-table)
1641 (setq local-abbrev-table java-mode-abbrev-table
1642 abbrev-mode t)
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))
1650
1651 \f
1652 ;; Support for CORBA's IDL language
1653
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.")
1657
1658 (c-define-abbrev-table 'idl-mode-abbrev-table nil
1659 "Abbreviation table used in idl-mode buffers.")
1660
1661 (defvar idl-mode-map
1662 (let ((map (c-make-inherited-keymap)))
1663 ;; Add bindings which are only useful for IDL.
1664 map)
1665 "Keymap used in idl-mode buffers.")
1666
1667 (easy-menu-define c-idl-menu idl-mode-map "IDL Mode Commands"
1668 (cons "IDL" (c-lang-const c-mode-menu idl)))
1669
1670 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.idl\\'" . idl-mode))
1671
1672 ;;;###autoload
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
1679 message.
1680
1681 To see what version of CC Mode you are running, enter `\\[c-version]'.
1682
1683 The hook `c-mode-common-hook' is run with no args at mode
1684 initialization, then `idl-mode-hook'.
1685
1686 Key bindings:
1687 \\{idl-mode-map}"
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))
1698
1699 \f
1700 ;; Support for Pike
1701
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.")
1705
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.")
1710
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)
1715 map)
1716 "Keymap used in pike-mode buffers.")
1717
1718 (easy-menu-define c-pike-menu pike-mode-map "Pike Mode Commands"
1719 (cons "Pike" (c-lang-const c-mode-menu pike)))
1720
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))
1723
1724 ;;;###autoload
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
1731 message.
1732
1733 To see what version of CC Mode you are running, enter `\\[c-version]'.
1734
1735 The hook `c-mode-common-hook' is run with no args at mode
1736 initialization, then `pike-mode-hook'.
1737
1738 Key bindings:
1739 \\{pike-mode-map}"
1740 (c-initialize-cc-mode t)
1741 (set-syntax-table pike-mode-syntax-table)
1742 (setq local-abbrev-table pike-mode-abbrev-table
1743 abbrev-mode t)
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))
1751
1752 \f
1753 ;; Support for AWK
1754
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))
1760
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.")
1765
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)
1779 map)
1780 "Keymap used in awk-mode buffers.")
1781
1782 (easy-menu-define c-awk-menu awk-mode-map "AWK Mode Commands"
1783 (cons "AWK" (c-lang-const c-mode-menu awk)))
1784
1785 ;; (require 'cc-awk) brings these in.
1786 (defvar awk-mode-syntax-table)
1787 (declare-function c-awk-unstick-NL-prop "cc-awk" ())
1788
1789 ;;;###autoload
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.
1796
1797 To see what version of CC Mode you are running, enter `\\[c-version]'.
1798
1799 The hook `c-mode-common-hook' is run with no args at mode
1800 initialization, then `awk-mode-hook'.
1801
1802 Key bindings:
1803 \\{awk-mode-map}"
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.
1807 :syntax-table nil
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
1812 abbrev-mode t)
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)
1817
1818 (c-run-mode-hooks 'c-mode-common-hook 'awk-mode-hook)
1819 (c-update-modeline))
1820
1821 \f
1822 ;; bug reporting
1823
1824 (defconst c-mode-help-address
1825 "submit@debbugs.gnu.org"
1826 "Address(es) for CC Mode bug reports.")
1827
1828 (defun c-version ()
1829 "Echo the current version of CC Mode in the minibuffer."
1830 (interactive)
1831 (message "Using CC Mode version %s" c-version)
1832 (c-keep-region-active))
1833
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)
1837
1838 ;; Dynamic variables used by reporter.
1839 (defvar reporter-prompt-for-summary-p)
1840 (defvar reporter-dont-compact-list)
1841
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.")
1845
1846 ;; reporter-submit-bug-report requires sendmail.
1847 (declare-function mail-position-on-field "sendmail" (field &optional soft))
1848
1849 (defun c-submit-bug-report ()
1850 "Submit via mail a bug report on CC Mode."
1851 (interactive)
1852 (require 'reporter)
1853 ;; load in reporter
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))
1858 (and
1859 (if (y-or-n-p "Do you want to submit a report on CC Mode? ")
1860 t (message "") nil)
1861 (reporter-submit-bug-report
1862 c-mode-help-address
1863 (concat "CC Mode " c-version " (" mode-name ")")
1864 (let ((vars (append
1865 c-style-variables
1866 '(c-buffer-is-cc-mode
1867 c-tab-always-indent
1868 c-syntactic-indentation
1869 c-syntactic-indentation-in-macros
1870 c-ignore-auto-fill
1871 c-auto-align-backslashes
1872 c-backspace-function
1873 c-delete-function
1874 c-electric-pound-behavior
1875 c-default-style
1876 c-enable-xemacs-performance-kludge-p
1877 c-old-style-variable-behavior
1878 defun-prompt-regexp
1879 tab-width
1880 comment-column
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.
1888 auto-fill-mode
1889 auto-fill-function
1890 filladapt-mode
1891 comment-multi-line
1892 comment-start-skip
1893 fill-prefix
1894 fill-column
1895 paragraph-start
1896 adaptive-fill-mode
1897 adaptive-fill-regexp)
1898 nil)))
1899 (mapc (lambda (var) (unless (boundp var)
1900 (setq vars (delq var vars))))
1901 '(signal-error-on-buffer-boundary
1902 filladapt-mode
1903 defun-prompt-regexp
1904 font-lock-mode
1905 auto-fill-mode
1906 font-lock-maximum-decoration
1907 parse-sexp-lookup-properties
1908 lookup-syntax-properties))
1909 vars)
1910 (lambda ()
1911 (run-hooks 'c-prepare-bug-report-hook)
1912 (save-excursion
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)))))))
1917
1918 \f
1919 (cc-provide 'cc-mode)
1920
1921 ;; Local Variables:
1922 ;; indent-tabs-mode: t
1923 ;; tab-width: 8
1924 ;; End:
1925 ;;; cc-mode.el ends here