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