2
|
1 ;;; cc-mode.el --- major mode for editing C, C++, Objective-C, and Java code
|
0
|
2
|
32
|
3 ;; Copyright (C) 1985,87,92,93,94,95,96,97 Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Authors: 1992-1997 Barry A. Warsaw
|
0
|
6 ;; 1987 Dave Detlefs and Stewart Clamen
|
|
7 ;; 1985 Richard M. Stallman
|
|
8 ;; Created: a long, long, time ago. adapted from the original c-mode.el
|
44
|
9 ;; Version: 4.388
|
|
10 ;; Last Modified: 1997/03/25 03:19:17
|
0
|
11 ;; Keywords: c languages oop
|
|
12
|
|
13 ;; NOTE: Read the commentary below for the right way to submit bug reports!
|
|
14 ;; NOTE: See the accompanying texinfo manual for details on using this mode!
|
|
15
|
|
16 ;; This file is part of GNU Emacs.
|
|
17
|
|
18 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
19 ;; it under the terms of the GNU General Public License as published by
|
|
20 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
21 ;; any later version.
|
|
22
|
|
23 ;; GNU Emacs is distributed in the hope that it will be useful,
|
|
24 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
25 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
26 ;; GNU General Public License for more details.
|
|
27
|
|
28 ;; You should have received a copy of the GNU General Public License
|
|
29 ;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
30 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
31 ;; Boston, MA 02111-1307, USA.
|
|
32
|
|
33 ;;; Commentary:
|
|
34
|
40
|
35 ;; This package provides GNU Emacs major modes for editing C, C++,
|
|
36 ;; Objective-C, and Java code. As of the latest Emacs and XEmacs
|
|
37 ;; releases, it is the default package for editing these languages.
|
|
38 ;; This package is called "CC Mode", and should be spelled exactly
|
|
39 ;; this way. It supports K&R and ANSI C, ANSI C++, Objective-C, and
|
|
40 ;; Java, with a consistent indentation model across all modes. This
|
|
41 ;; indentation model is intuitive and very flexible, so that almost
|
|
42 ;; any desired style of indentation can be supported. Installation,
|
|
43 ;; usage, and programming details are contained in an accompanying
|
|
44 ;; texinfo manual.
|
|
45
|
|
46 ;; CC Mode's immediate ancestors were, c++-mode.el, cplus-md.el, and
|
|
47 ;; cplus-md1.el..
|
0
|
48
|
2
|
49 ;; NOTE: This mode does not perform font-locking (a.k.a syntactic
|
32
|
50 ;; coloring, keyword highlighting, etc.) for any of the supported
|
|
51 ;; modes. Typically this is done by a package called font-lock.el
|
|
52 ;; which I do *not* maintain. You should contact the Emacs
|
|
53 ;; maintainers for questions about coloring or highlighting in any
|
|
54 ;; language mode.
|
2
|
55
|
0
|
56 ;; To submit bug reports, type "C-c C-b". These will be sent to
|
2
|
57 ;; bug-gnu-emacs@prep.ai.mit.edu as well as cc-mode-help@python.org,
|
|
58 ;; and I'll read about them there (the former is mirrored as the
|
|
59 ;; Usenet newsgroup gnu.emacs.bug). Questions can sent to
|
32
|
60 ;; help-gnu-emacs@prep.ai.mit.edu (mirrored as gnu.emacs.help) and/or
|
2
|
61 ;; cc-mode-help@python.org. Please do not send bugs or questions to
|
|
62 ;; my personal account.
|
0
|
63
|
|
64 ;; YOU CAN IGNORE ALL BYTE-COMPILER WARNINGS. They are the result of
|
32
|
65 ;; the cross-Emacsen support. GNU Emacs 19 (from the FSF), GNU XEmacs
|
|
66 ;; 19 (formerly Lucid Emacs), and GNU Emacs 18 all do things
|
|
67 ;; differently and there's no way to shut the byte-compiler up at the
|
|
68 ;; necessary granularity. Let me say this again: YOU CAN IGNORE ALL
|
0
|
69 ;; BYTE-COMPILER WARNINGS (you'd be surprised at how many people don't
|
|
70 ;; follow this advice :-).
|
|
71
|
|
72 ;; Many, many thanks go out to all the folks on the beta test list.
|
|
73 ;; Without their patience, testing, insight, code contributions, and
|
32
|
74 ;; encouragement CC Mode would be a far inferior package.
|
|
75
|
|
76 ;; You can get the latest version of CC Mode, including PostScript
|
2
|
77 ;; documentation and separate individual files from:
|
0
|
78 ;;
|
2
|
79 ;; http://www.python.org/ftp/emacs/
|
|
80
|
|
81 ;; Or if you don't have access to the World Wide Web, through
|
|
82 ;; anonymous ftp from:
|
|
83 ;;
|
|
84 ;; ftp://ftp.python.org/pub/emacs
|
0
|
85
|
|
86 ;;; Code:
|
|
87
|
|
88
|
|
89 ;; user definable variables
|
|
90 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
|
|
91
|
|
92 (defvar c-inhibit-startup-warnings-p nil
|
|
93 "*If non-nil, inhibits start up compatibility warnings.")
|
|
94 (defvar c-strict-syntax-p nil
|
|
95 "*If non-nil, all syntactic symbols must be found in `c-offsets-alist'.
|
|
96 If the syntactic symbol for a particular line does not match a symbol
|
|
97 in the offsets alist, an error is generated, otherwise no error is
|
|
98 reported and the syntactic symbol is ignored.")
|
|
99 (defvar c-echo-syntactic-information-p nil
|
|
100 "*If non-nil, syntactic info is echoed when the line is indented.")
|
|
101 (defvar c-basic-offset 4
|
|
102 "*Amount of basic offset used by + and - symbols in `c-offsets-alist'.")
|
|
103
|
32
|
104 (defconst c-offsets-alist
|
0
|
105 '((string . -1000)
|
|
106 (c . c-lineup-C-comments)
|
|
107 (defun-open . 0)
|
|
108 (defun-close . 0)
|
|
109 (defun-block-intro . +)
|
|
110 (class-open . 0)
|
|
111 (class-close . 0)
|
|
112 (inline-open . +)
|
|
113 (inline-close . 0)
|
32
|
114 (func-decl-cont . +)
|
0
|
115 (knr-argdecl-intro . +)
|
|
116 (knr-argdecl . 0)
|
|
117 (topmost-intro . 0)
|
|
118 (topmost-intro-cont . 0)
|
|
119 (member-init-intro . +)
|
|
120 (member-init-cont . 0)
|
|
121 (inher-intro . +)
|
|
122 (inher-cont . c-lineup-multi-inher)
|
|
123 (block-open . 0)
|
|
124 (block-close . 0)
|
|
125 (brace-list-open . 0)
|
|
126 (brace-list-close . 0)
|
|
127 (brace-list-intro . +)
|
|
128 (brace-list-entry . 0)
|
|
129 (statement . 0)
|
|
130 ;; some people might prefer
|
|
131 ;;(statement . c-lineup-runin-statements)
|
|
132 (statement-cont . +)
|
|
133 ;; some people might prefer
|
|
134 ;;(statement-cont . c-lineup-math)
|
|
135 (statement-block-intro . +)
|
|
136 (statement-case-intro . +)
|
|
137 (statement-case-open . 0)
|
|
138 (substatement . +)
|
|
139 (substatement-open . +)
|
|
140 (case-label . 0)
|
|
141 (access-label . -)
|
|
142 (label . 2)
|
|
143 (do-while-closure . 0)
|
|
144 (else-clause . 0)
|
|
145 (comment-intro . c-lineup-comment)
|
|
146 (arglist-intro . +)
|
|
147 (arglist-cont . 0)
|
|
148 (arglist-cont-nonempty . c-lineup-arglist)
|
|
149 (arglist-close . +)
|
|
150 (stream-op . c-lineup-streamop)
|
|
151 (inclass . +)
|
|
152 (cpp-macro . -1000)
|
|
153 (friend . 0)
|
|
154 (objc-method-intro . -1000)
|
|
155 (objc-method-args-cont . c-lineup-ObjC-method-args)
|
|
156 (objc-method-call-cont . c-lineup-ObjC-method-call)
|
2
|
157 (extern-lang-open . 0)
|
|
158 (extern-lang-close . 0)
|
|
159 (inextern-lang . +)
|
0
|
160 )
|
|
161 "*Association list of syntactic element symbols and indentation offsets.
|
|
162 As described below, each cons cell in this list has the form:
|
|
163
|
|
164 (SYNTACTIC-SYMBOL . OFFSET)
|
|
165
|
32
|
166 When a line is indented, CC Mode first determines the syntactic
|
0
|
167 context of the line by generating a list of symbols called syntactic
|
|
168 elements. This list can contain more than one syntactic element and
|
|
169 the global variable `c-syntactic-context' contains the context list
|
|
170 for the line being indented. Each element in this list is actually a
|
|
171 cons cell of the syntactic symbol and a buffer position. This buffer
|
|
172 position is called the relative indent point for the line. Some
|
|
173 syntactic symbols may not have a relative indent point associated with
|
|
174 them.
|
|
175
|
32
|
176 After the syntactic context list for a line is generated, CC Mode
|
0
|
177 calculates the absolute indentation for the line by looking at each
|
|
178 syntactic element in the list. First, it compares the syntactic
|
|
179 element against the SYNTACTIC-SYMBOL's in `c-offsets-alist'. When it
|
|
180 finds a match, it adds the OFFSET to the column of the relative indent
|
|
181 point. The sum of this calculation for each element in the syntactic
|
|
182 list is the absolute offset for line being indented.
|
|
183
|
|
184 If the syntactic element does not match any in the `c-offsets-alist',
|
32
|
185 an error is generated if `c-strict-syntax-p' is non-nil, otherwise the
|
|
186 element is ignored.
|
0
|
187
|
|
188 Actually, OFFSET can be an integer, a function, a variable, or one of
|
|
189 the following symbols: `+', `-', `++', `--', `*', or `/'. These
|
|
190 latter designate positive or negative multiples of `c-basic-offset',
|
|
191 respectively: *1, *-1, *2, *-2, *0.5, and *-0.5. If OFFSET is a
|
|
192 function, it is called with a single argument containing the cons of
|
|
193 the syntactic element symbol and the relative indent point. The
|
|
194 function should return an integer offset.
|
|
195
|
|
196 Here is the current list of valid syntactic element symbols:
|
|
197
|
|
198 string -- inside multi-line string
|
|
199 c -- inside a multi-line C style block comment
|
|
200 defun-open -- brace that opens a function definition
|
|
201 defun-close -- brace that closes a function definition
|
|
202 defun-block-intro -- the first line in a top-level defun
|
|
203 class-open -- brace that opens a class definition
|
|
204 class-close -- brace that closes a class definition
|
|
205 inline-open -- brace that opens an in-class inline method
|
|
206 inline-close -- brace that closes an in-class inline method
|
32
|
207 func-decl-cont -- the nether region between a function
|
|
208 declaration and the defun opening brace.
|
|
209 In C++ and Java, this can include `throws'
|
|
210 declarations
|
0
|
211 knr-argdecl-intro -- first line of a K&R C argument declaration
|
|
212 knr-argdecl -- subsequent lines in a K&R C argument declaration
|
|
213 topmost-intro -- the first line in a topmost construct definition
|
|
214 topmost-intro-cont -- topmost definition continuation lines
|
|
215 member-init-intro -- first line in a member initialization list
|
|
216 member-init-cont -- subsequent member initialization list lines
|
|
217 inher-intro -- first line of a multiple inheritance list
|
|
218 inher-cont -- subsequent multiple inheritance lines
|
|
219 block-open -- statement block open brace
|
|
220 block-close -- statement block close brace
|
|
221 brace-list-open -- open brace of an enum or static array list
|
|
222 brace-list-close -- close brace of an enum or static array list
|
|
223 brace-list-intro -- first line in an enum or static array list
|
|
224 brace-list-entry -- subsequent lines in an enum or static array list
|
2
|
225 statement -- a C (or like) statement
|
|
226 statement-cont -- a continuation of a C (or like) statement
|
0
|
227 statement-block-intro -- the first line in a new statement block
|
|
228 statement-case-intro -- the first line in a case `block'
|
|
229 statement-case-open -- the first line in a case block starting with brace
|
|
230 substatement -- the first line after an if/while/for/do/else
|
|
231 substatement-open -- the brace that opens a substatement block
|
|
232 case-label -- a case or default label
|
|
233 access-label -- C++ private/protected/public access label
|
2
|
234 label -- any non-special C (or like) label
|
0
|
235 do-while-closure -- the `while' that ends a do/while construct
|
|
236 else-clause -- the `else' of an if/else construct
|
|
237 comment-intro -- a line containing only a comment introduction
|
|
238 arglist-intro -- the first line in an argument list
|
|
239 arglist-cont -- subsequent argument list lines when no
|
|
240 arguments follow on the same line as the
|
2
|
241 arglist opening paren
|
0
|
242 arglist-cont-nonempty -- subsequent argument list lines when at
|
|
243 least one argument follows on the same
|
|
244 line as the arglist opening paren
|
|
245 arglist-close -- the solo close paren of an argument list
|
|
246 stream-op -- lines continuing a stream operator construct
|
|
247 inclass -- the construct is nested inside a class definition
|
|
248 cpp-macro -- the start of a cpp macro
|
|
249 friend -- a C++ friend declaration
|
|
250 objc-method-intro -- the first line of an Objective-C method definition
|
|
251 objc-method-args-cont -- lines continuing an Objective-C method definition
|
|
252 objc-method-call-cont -- lines continuing an Objective-C method call
|
2
|
253 extern-lang-open -- brace that opens an external language block
|
|
254 extern-lang-close -- brace that closes an external language block
|
|
255 inextern-lang -- analogous to `inclass' syntactic symbol
|
0
|
256 ")
|
|
257
|
|
258 (defvar c-tab-always-indent t
|
|
259 "*Controls the operation of the TAB key.
|
|
260 If t, hitting TAB always just indents the current line. If nil,
|
|
261 hitting TAB indents the current line if point is at the left margin or
|
2
|
262 in the line's indentation, otherwise it insert a `real' tab character
|
32
|
263 \(see note\). If other than nil or t, then tab is inserted only
|
|
264 within literals -- defined as comments and strings -- and inside
|
|
265 preprocessor directives, but line is always reindented.
|
0
|
266
|
2
|
267 Note: The value of `indent-tabs-mode' will determine whether a real
|
|
268 tab character will be inserted, or the equivalent number of space.
|
|
269 When inserting a tab, actually the function stored in the variable
|
|
270 `c-insert-tab-function' is called.
|
|
271
|
|
272 Note: indentation of lines containing only comments is also controlled
|
|
273 by the `c-comment-only-line-offset' variable.")
|
|
274
|
|
275 (defvar c-insert-tab-function 'insert-tab
|
|
276 "*Function used when inserting a tab for \\[TAB].
|
|
277 Only used when `c-tab-always-indent' indicates a `real' tab character
|
|
278 should be inserted. Value must be a function taking no arguments.")
|
0
|
279
|
|
280 (defvar c-comment-only-line-offset 0
|
|
281 "*Extra offset for line which contains only the start of a comment.
|
|
282 Can contain an integer or a cons cell of the form:
|
|
283
|
|
284 (NON-ANCHORED-OFFSET . ANCHORED-OFFSET)
|
|
285
|
|
286 Where NON-ANCHORED-OFFSET is the amount of offset given to
|
|
287 non-column-zero anchored comment-only lines, and ANCHORED-OFFSET is
|
|
288 the amount of offset to give column-zero anchored comment-only lines.
|
|
289 Just an integer as value is equivalent to (<val> . -1000).")
|
|
290
|
|
291 (defvar c-indent-comments-syntactically-p nil
|
|
292 "*Specifies how comment-only lines should be indented.
|
|
293 When this variable is non-nil, comment-only lines are indented
|
|
294 according to syntactic analysis via `c-offsets-alist', even when
|
|
295 \\[indent-for-comment] is used.")
|
|
296
|
|
297 (defvar c-cleanup-list '(scope-operator)
|
|
298 "*List of various C/C++/ObjC constructs to \"clean up\".
|
32
|
299 These clean ups only take place when the auto-newline feature is
|
|
300 turned on, as evidenced by the `/a' or `/ah' appearing next to the
|
|
301 mode name. Valid symbols are:
|
0
|
302
|
|
303 brace-else-brace -- cleans up `} else {' constructs by placing entire
|
32
|
304 construct on a single line. This clean up
|
|
305 only takes place when there is nothing but
|
|
306 white space between the braces and the `else'.
|
|
307 Clean up occurs when the open-brace after the
|
|
308 `else' is typed.
|
|
309 brace-elseif-brace -- similar to brace-else-brace, but cleans up
|
|
310 `} else if {' constructs.
|
0
|
311 empty-defun-braces -- cleans up empty defun braces by placing the
|
|
312 braces on the same line. Clean up occurs when
|
|
313 the defun closing brace is typed.
|
|
314 defun-close-semi -- cleans up the terminating semi-colon on defuns
|
|
315 by placing the semi-colon on the same line as
|
|
316 the closing brace. Clean up occurs when the
|
|
317 semi-colon is typed.
|
|
318 list-close-comma -- cleans up commas following braces in array
|
|
319 and aggregate initializers. Clean up occurs
|
|
320 when the comma is typed.
|
|
321 scope-operator -- cleans up double colons which may designate
|
|
322 a C++ scope operator split across multiple
|
|
323 lines. Note that certain C++ constructs can
|
|
324 generate ambiguous situations. This clean up
|
|
325 only takes place when there is nothing but
|
|
326 whitespace between colons. Clean up occurs
|
|
327 when the second colon is typed.")
|
|
328
|
|
329 (defvar c-hanging-braces-alist '((brace-list-open)
|
|
330 (substatement-open after)
|
2
|
331 (block-close . c-snug-do-while)
|
|
332 (extern-lang-open after)
|
|
333 )
|
0
|
334 "*Controls the insertion of newlines before and after braces.
|
|
335 This variable contains an association list with elements of the
|
|
336 following form: (SYNTACTIC-SYMBOL . ACTION).
|
|
337
|
|
338 When a brace (either opening or closing) is inserted, the syntactic
|
|
339 context it defines is looked up in this list, and if found, the
|
|
340 associated ACTION is used to determine where newlines are inserted.
|
|
341 If the context is not found, the default is to insert a newline both
|
|
342 before and after the brace.
|
|
343
|
|
344 SYNTACTIC-SYMBOL can be any of: defun-open, defun-close, class-open,
|
|
345 class-close, inline-open, inline-close, block-open, block-close,
|
2
|
346 substatement-open, statement-case-open, extern-lang-open,
|
|
347 extern-lang-close, brace-list-open, brace-list-close,
|
|
348 brace-list-intro, or brace-list-entry. See `c-offsets-alist' for
|
|
349 details.
|
0
|
350
|
|
351 ACTION can be either a function symbol or a list containing any
|
|
352 combination of the symbols `before' or `after'. If the list is empty,
|
|
353 no newlines are inserted either before or after the brace.
|
|
354
|
|
355 When ACTION is a function symbol, the function is called with a two
|
|
356 arguments: the syntactic symbol for the brace and the buffer position
|
|
357 at which the brace was inserted. The function must return a list as
|
|
358 described in the preceding paragraph. Note that during the call to
|
|
359 the function, the variable `c-syntactic-context' is set to the entire
|
|
360 syntactic context for the brace line.")
|
|
361
|
|
362 (defvar c-hanging-colons-alist nil
|
|
363 "*Controls the insertion of newlines before and after certain colons.
|
|
364 This variable contains an association list with elements of the
|
|
365 following form: (SYNTACTIC-SYMBOL . ACTION).
|
|
366
|
|
367 See the variable `c-hanging-braces-alist' for the semantics of this
|
|
368 variable. Note however that making ACTION a function symbol is
|
|
369 currently not supported for this variable.")
|
|
370
|
|
371 (defvar c-hanging-semi&comma-criteria '(c-semi&comma-inside-parenlist)
|
|
372 "*List of functions that decide whether to insert a newline or not.
|
|
373 The functions in this list are called, in order, whenever the
|
|
374 auto-newline minor mode is activated (as evidenced by a `/a' or `/ah'
|
|
375 string in the mode line), and a semicolon or comma is typed (see
|
|
376 `c-electric-semi&comma'). Each function in this list is called with
|
|
377 no arguments, and should return one of the following values:
|
|
378
|
|
379 nil -- no determination made, continue checking
|
|
380 'stop -- do not insert a newline, and stop checking
|
|
381 (anything else) -- insert a newline, and stop checking
|
|
382
|
|
383 If every function in the list is called with no determination made,
|
|
384 then no newline is inserted.")
|
|
385
|
|
386 (defvar c-hanging-comment-ender-p t
|
32
|
387 "*Controls what \\[fill-paragraph] does to C block comment enders.
|
|
388 When set to nil, C block comment enders are left on their own line.
|
|
389 When set to t, block comment enders will be placed at the end of the
|
|
390 previous line (i.e. they `hang' on that line).")
|
|
391
|
|
392 (defvar c-hanging-comment-starter-p t
|
|
393 "*Controls what \\[fill-paragraph] does to C block comment starters.
|
|
394 When set to nil, C block comment starters are left on their own line.
|
|
395 When set to t, text that follows a block comment starter will be
|
|
396 placed on the same line as the block comment starter (i.e. the text
|
|
397 `hangs' on that line).")
|
0
|
398
|
|
399 (defvar c-backslash-column 48
|
|
400 "*Column to insert backslashes when macroizing a region.")
|
|
401 (defvar c-special-indent-hook nil
|
|
402 "*Hook for user defined special indentation adjustments.
|
|
403 This hook gets called after a line is indented by the mode.")
|
|
404 (defvar c-delete-function 'backward-delete-char-untabify
|
|
405 "*Function called by `c-electric-delete' when deleting characters.")
|
|
406 (defvar c-electric-pound-behavior nil
|
|
407 "*List of behaviors for electric pound insertion.
|
|
408 Only currently supported behavior is `alignleft'.")
|
40
|
409
|
2
|
410 (defvar c-label-minimum-indentation 1
|
40
|
411 "*Minimum indentation for lines inside of top-level constructs.
|
|
412 This variable typically only affects code using the `gnu' style, which
|
|
413 mandates a minimum of one space in front of every line inside
|
|
414 top-level constructs. Specifically, the function
|
|
415 `c-gnu-impose-minimum' on your `c-special-indent-hook' is what
|
|
416 enforces this.")
|
0
|
417
|
|
418 (defvar c-progress-interval 5
|
|
419 "*Interval used to update progress status during long re-indentation.
|
|
420 If a number, percentage complete gets updated after each interval of
|
32
|
421 that many seconds. Set to nil to inhibit updating. This is only
|
0
|
422 useful for Emacs 19.")
|
|
423
|
2
|
424 (defconst c-style-alist
|
0
|
425 '(("gnu"
|
|
426 (c-basic-offset . 2)
|
|
427 (c-comment-only-line-offset . (0 . 0))
|
|
428 (c-offsets-alist . ((statement-block-intro . +)
|
|
429 (knr-argdecl-intro . 5)
|
|
430 (substatement-open . +)
|
|
431 (label . 0)
|
|
432 (statement-case-open . +)
|
|
433 (statement-cont . +)
|
|
434 (arglist-intro . c-lineup-arglist-intro-after-paren)
|
|
435 (arglist-close . c-lineup-arglist)
|
|
436 ))
|
2
|
437 (c-special-indent-hook . c-gnu-impose-minimum)
|
0
|
438 )
|
|
439 ("k&r"
|
|
440 (c-basic-offset . 5)
|
|
441 (c-comment-only-line-offset . 0)
|
|
442 (c-offsets-alist . ((statement-block-intro . +)
|
|
443 (knr-argdecl-intro . 0)
|
|
444 (substatement-open . 0)
|
|
445 (label . 0)
|
|
446 (statement-cont . +)
|
|
447 ))
|
|
448 )
|
|
449 ("bsd"
|
|
450 (c-basic-offset . 4)
|
|
451 (c-comment-only-line-offset . 0)
|
|
452 (c-offsets-alist . ((statement-block-intro . +)
|
|
453 (knr-argdecl-intro . +)
|
|
454 (substatement-open . 0)
|
|
455 (label . 0)
|
|
456 (statement-cont . +)
|
|
457 ))
|
|
458 )
|
|
459 ("stroustrup"
|
|
460 (c-basic-offset . 4)
|
|
461 (c-comment-only-line-offset . 0)
|
|
462 (c-offsets-alist . ((statement-block-intro . +)
|
|
463 (substatement-open . 0)
|
|
464 (label . 0)
|
|
465 (statement-cont . +)
|
|
466 ))
|
|
467 )
|
|
468 ("whitesmith"
|
|
469 (c-basic-offset . 4)
|
|
470 (c-comment-only-line-offset . 0)
|
|
471 (c-offsets-alist . ((statement-block-intro . +)
|
|
472 (knr-argdecl-intro . +)
|
|
473 (substatement-open . 0)
|
|
474 (label . 0)
|
|
475 (statement-cont . +)
|
|
476 ))
|
|
477
|
|
478 )
|
|
479 ("ellemtel"
|
|
480 (c-basic-offset . 3)
|
|
481 (c-comment-only-line-offset . 0)
|
|
482 (c-hanging-braces-alist . ((substatement-open before after)))
|
|
483 (c-offsets-alist . ((topmost-intro . 0)
|
|
484 (topmost-intro-cont . 0)
|
32
|
485 (substatement . +)
|
0
|
486 (substatement-open . 0)
|
|
487 (case-label . +)
|
32
|
488 (access-label . -)
|
|
489 (inclass . ++)
|
0
|
490 (inline-open . 0)
|
|
491 ))
|
|
492 )
|
4
|
493 ("linux"
|
|
494 (c-basic-offset . 8)
|
|
495 (c-comment-only-line-offset . 0)
|
|
496 (c-hanging-braces-alist . ((brace-list-open)
|
|
497 (substatement-open after)
|
|
498 (block-close . c-snug-do-while)))
|
|
499 (c-cleanup-list . (brace-else-brace))
|
|
500 (c-offsets-alist . ((statement-block-intro . +)
|
|
501 (knr-argdecl-intro . 0)
|
|
502 (substatement-open . 0)
|
|
503 (label . 0)
|
|
504 (statement-cont . +)
|
|
505 ))
|
|
506 )
|
32
|
507 ("python"
|
|
508 (indent-tabs-mode . t)
|
|
509 (c-basic-offset . 8)
|
|
510 (c-offsets-alist . ((substatement-open . 0)
|
|
511 ))
|
|
512 (c-hanging-braces-alist . ((brace-list-open)
|
|
513 (brace-list-intro)
|
|
514 (brace-list-close)
|
|
515 (substatement-open after)
|
|
516 (block-close . c-snug-do-while)
|
|
517 ))
|
|
518 )
|
0
|
519 ("java"
|
|
520 (c-basic-offset . 2)
|
|
521 (c-comment-only-line-offset . (0 . 0))
|
|
522 (c-offsets-alist . ((statement-block-intro . +)
|
|
523 (knr-argdecl-intro . 5)
|
|
524 (substatement-open . +)
|
|
525 (label . 0)
|
|
526 (statement-case-open . +)
|
|
527 (statement-cont . +)
|
|
528 (arglist-intro . c-lineup-arglist-intro-after-paren)
|
|
529 (arglist-close . c-lineup-arglist)
|
|
530 (access-label . 0)
|
32
|
531 (inher-cont . c-lineup-java-inher)
|
0
|
532 ))
|
|
533
|
|
534 )
|
|
535 )
|
|
536 "Styles of Indentation.
|
|
537 Elements of this alist are of the form:
|
|
538
|
|
539 (STYLE-STRING (VARIABLE . VALUE) [(VARIABLE . VALUE) ...])
|
|
540
|
|
541 where STYLE-STRING is a short descriptive string used to select a
|
32
|
542 style, VARIABLE is any CC Mode variable, and VALUE is the intended
|
0
|
543 value for that variable when using the selected style.
|
|
544
|
|
545 There is one special case when VARIABLE is `c-offsets-alist'. In this
|
|
546 case, the VALUE is a list containing elements of the form:
|
|
547
|
|
548 (SYNTACTIC-SYMBOL . VALUE)
|
|
549
|
|
550 as described in `c-offsets-alist'. These are passed directly to
|
|
551 `c-set-offset' so there is no need to set every syntactic symbol in
|
|
552 your style, only those that are different from the default.
|
|
553
|
|
554 Note that all styles inherit from the `cc-mode' style, which is
|
|
555 computed at the time the mode is loaded.")
|
|
556
|
|
557 (defvar c-file-style nil
|
|
558 "*Variable interface for setting style via File Local Variables.
|
|
559 In a file's Local Variable section, you can set this variable to a
|
32
|
560 string suitable for `c-set-style'. When the file is visited, CC Mode
|
0
|
561 will set the style of the file to this value automatically.
|
|
562
|
|
563 Note that file style settings are applied before file offset settings
|
|
564 as designated in the variable `c-file-offsets'.")
|
|
565
|
|
566 (defvar c-file-offsets nil
|
|
567 "*Variable interface for setting offsets via File Local Variables.
|
|
568 In a file's Local Variable section, you can set this variable to an
|
|
569 association list similar to the values allowed in `c-offsets-alist'.
|
32
|
570 When the file is visited, CC Mode will institute these offset settings
|
0
|
571 automatically.
|
|
572
|
|
573 Note that file offset settings are applied after file style settings
|
|
574 as designated in the variable `c-file-style'.")
|
|
575
|
|
576 (defvar c-site-default-style "gnu"
|
|
577 "Default style for your site.
|
|
578 To change the default style at your site, you can set this variable to
|
32
|
579 any style defined in `c-style-alist'. However, if CC Mode is usually
|
0
|
580 loaded into your Emacs at compile time, you will need to set this
|
32
|
581 variable in the `site-init.el' file before CC Mode is loaded, then
|
0
|
582 re-dump Emacs.")
|
|
583
|
32
|
584 (defvar c-style-variables-are-local-p t
|
|
585 "*Whether style variables should be buffer local by default.
|
|
586 If non-nil, then all indentation style related variables will be made
|
|
587 buffer local by default. If nil, they will remain global. Variables
|
|
588 are made buffer local when this file is loaded, and once buffer
|
|
589 localized, they cannot be made global again.
|
|
590
|
|
591 The list of variables to buffer localize are:
|
|
592 c-offsets-alist
|
|
593 c-basic-offset
|
|
594 c-file-style
|
|
595 c-file-offsets
|
|
596 c-comment-only-line-offset
|
|
597 c-cleanup-list
|
|
598 c-hanging-braces-alist
|
|
599 c-hanging-colons-alist
|
|
600 c-hanging-comment-starter-p
|
|
601 c-hanging-comment-ender-p
|
|
602 c-backslash-column
|
|
603 c-label-minimum-indentation
|
|
604 c-special-indent-hook
|
|
605 c-indentation-style")
|
|
606
|
|
607
|
0
|
608 (defvar c-mode-hook nil
|
|
609 "*Hook called by `c-mode'.")
|
|
610 (defvar c++-mode-hook nil
|
|
611 "*Hook called by `c++-mode'.")
|
|
612 (defvar objc-mode-hook nil
|
|
613 "*Hook called by `objc-mode'.")
|
|
614 (defvar java-mode-hook nil
|
|
615 "*Hook called by `java-mode'.")
|
|
616
|
|
617 (defvar c-mode-common-hook nil
|
32
|
618 "*Hook called by all CC Mode modes for common initializations.")
|
0
|
619
|
|
620 (defvar c-mode-menu
|
|
621 '(["Comment Out Region" comment-region (mark)]
|
|
622 ["Macro Expand Region" c-macro-expand (mark)]
|
|
623 ["Backslashify" c-backslash-region (mark)]
|
|
624 ["Indent Expression" c-indent-exp
|
|
625 (memq (following-char) '(?\( ?\[ ?\{))]
|
|
626 ["Indent Line" c-indent-command t]
|
|
627 ["Fill Comment Paragraph" c-fill-paragraph t]
|
|
628 ["Up Conditional" c-up-conditional t]
|
|
629 ["Backward Conditional" c-backward-conditional t]
|
|
630 ["Forward Conditional" c-forward-conditional t]
|
|
631 ["Backward Statement" c-beginning-of-statement t]
|
|
632 ["Forward Statement" c-end-of-statement t]
|
|
633 )
|
32
|
634 "Basic XEmacs 19 menu for C/C++/ObjC/Java modes.")
|
|
635
|
|
636
|
|
637 ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
638 ;; NO USER DEFINABLE VARIABLES BEYOND THIS POINT
|
|
639
|
|
640 ;; imenu integration
|
0
|
641 (defvar cc-imenu-c++-generic-expression
|
|
642 (`
|
|
643 ((nil
|
|
644 (,
|
|
645 (concat
|
32
|
646 "^" ; beginning of line is required
|
|
647 "\\(template[ \t]*<[^>]+>[ \t]*\\)?" ; there may be a "template <...>"
|
|
648 "\\([a-zA-Z0-9_:]+[ \t]+\\)?" ; type specs; there can be no
|
|
649 "\\([a-zA-Z0-9_:]+[ \t]+\\)?" ; more than 3 tokens, right?
|
0
|
650
|
32
|
651 "\\(" ; last type spec including */&
|
0
|
652 "[a-zA-Z0-9_:]+"
|
32
|
653 "\\([ \t]*[*&]+[ \t]*\\|[ \t]+\\)" ; either ptr/ref sign or ws
|
|
654 "\\)?" ; if there is a last type spec
|
|
655 "\\(" ; name, take into the imenu entry
|
|
656 "[a-zA-Z0-9_:~]+" ; member func, ctor or dtor...
|
|
657 ; (may not contain * because then
|
|
658 ; "a::operator char*" would
|
|
659 ; become "char*"!)
|
0
|
660 "\\|"
|
|
661 "\\([a-zA-Z0-9_:~]*::\\)?operator"
|
32
|
662 "[^a-zA-Z1-9_][^(]*" ; ...or operator
|
0
|
663 " \\)"
|
32
|
664 "[ \t]*([^)]*)[ \t\n]*[^ ;]" ; require something other than
|
|
665 ; a `;' after the (...) to
|
|
666 ; avoid prototypes. Can't
|
|
667 ; catch cases with () inside
|
|
668 ; the parentheses surrounding
|
|
669 ; the parameters. e.g.:
|
|
670 ; "int foo(int a=bar()) {...}"
|
0
|
671
|
|
672 )) 6)
|
|
673 ("Class"
|
|
674 (, (concat
|
32
|
675 "^" ; beginning of line is required
|
0
|
676 "\\(template[ \t]*<[^>]+>[ \t]*\\)?" ; there may be a "template <...>"
|
|
677 "class[ \t]+"
|
32
|
678 "\\([a-zA-Z0-9_]+\\)" ; the string we want to get
|
0
|
679 "[ \t]*[:{]"
|
|
680 )) 2)))
|
|
681 "Imenu generic expression for C++ mode. See `imenu-generic-expression'.")
|
|
682
|
|
683 (defvar cc-imenu-c-generic-expression
|
|
684 cc-imenu-c++-generic-expression
|
|
685 "Imenu generic expression for C mode. See `imenu-generic-expression'.")
|
|
686
|
32
|
687 ;(defvar cc-imenu-objc-generic-expression
|
|
688 ; ())
|
|
689 ; Please contribute one!
|
|
690
|
|
691 (defvar cc-imenu-java-generic-expression
|
|
692 (`
|
|
693 ((nil
|
|
694 (,
|
|
695 (concat
|
|
696 "^\\([ \t]\\)*"
|
|
697 "\\([A-Za-z0-9_-]+[ \t]+\\)?" ; type specs; there can be
|
|
698 "\\([A-Za-z0-9_-]+[ \t]+\\)?" ; more than 3 tokens, right?
|
|
699 "\\([A-Za-z0-9_-]+[ \t]*[[]?[]]?\\)"
|
|
700 "\\([ \t]\\)"
|
|
701 "\\([A-Za-z0-9_-]+\\)" ; the string we want to get
|
|
702 "\\([ \t]*\\)+("
|
|
703 "\\([a-zA-Z,_1-9\n \t]*[[]?[]]?\\)*" ; arguments
|
|
704 ")[ \t]*"
|
|
705 "[^;(]"
|
|
706 "[,a-zA-Z_1-9\n \t]*{"
|
|
707 )) 6)))
|
|
708 "Imenu generic expression for Java mode. See `imenu-generic-expression'.")
|
|
709
|
|
710
|
0
|
711
|
|
712 ;; Shut the byte-compiler up. Requires Emacs 19 or JWZ's improved
|
|
713 ;; byte-compiler. Otherwise, comment this line out and ignore
|
|
714 ;; any warnings.
|
|
715 ;;(byte-compiler-options (warnings nil))
|
|
716
|
|
717 ;; figure out what features this Emacs has
|
|
718 (defconst c-emacs-features
|
|
719 (let ((major (and (boundp 'emacs-major-version)
|
|
720 emacs-major-version))
|
|
721 (minor (and (boundp 'emacs-minor-version)
|
|
722 emacs-minor-version))
|
|
723 (re-suite 'old-re)
|
32
|
724 flavor comments infodock-p)
|
0
|
725 ;; figure out version numbers if not already discovered
|
|
726 (and (or (not major) (not minor))
|
|
727 (string-match "\\([0-9]+\\).\\([0-9]+\\)" emacs-version)
|
|
728 (setq major (string-to-int (substring emacs-version
|
|
729 (match-beginning 1)
|
|
730 (match-end 1)))
|
|
731 minor (string-to-int (substring emacs-version
|
|
732 (match-beginning 2)
|
|
733 (match-end 2)))))
|
|
734 (if (not (and major minor))
|
|
735 (error "Cannot figure out the major and minor version numbers."))
|
|
736 ;; calculate the major version
|
|
737 (cond
|
|
738 ((= major 18) (setq major 'v18)) ;Emacs 18
|
|
739 ((= major 4) (setq major 'v18)) ;Epoch 4
|
|
740 ((= major 19) (setq major 'v19 ;Emacs 19
|
|
741 flavor (if (or (string-match "Lucid" emacs-version)
|
|
742 (string-match "XEmacs" emacs-version))
|
32
|
743 'XEmacs 'FSF)
|
|
744 infodock-p (boundp 'infodock-version)))
|
2
|
745 ((= major 20) (setq major 'v20 ;XEmacs 20
|
|
746 flavor 'XEmacs))
|
0
|
747 ;; I don't know
|
|
748 (t (error "Cannot recognize major version number: %s" major)))
|
|
749 ;; Regular expression suites...
|
2
|
750 (if (or (eq major 'v20)
|
|
751 (and (eq major 'v19)
|
|
752 (or (and (eq flavor 'XEmacs) (>= minor 14))
|
|
753 (and (eq flavor 'FSF) (>= minor 30)))))
|
0
|
754 (setq re-suite 'new-re))
|
|
755 ;; XEmacs 19 uses 8-bit modify-syntax-entry flags, as do all
|
|
756 ;; patched Emacs 19, Emacs 18, Epoch 4's. Only Emacs 19 uses a
|
|
757 ;; 1-bit flag. Let's be as smart as we can about figuring this
|
|
758 ;; out.
|
2
|
759 (if (or (eq major 'v20) (eq major 'v19))
|
0
|
760 (let ((table (copy-syntax-table)))
|
|
761 (modify-syntax-entry ?a ". 12345678" table)
|
|
762 (cond
|
|
763 ;; XEmacs pre 20 and Emacs pre 19.30 use vectors for syntax tables.
|
|
764 ((vectorp table)
|
|
765 (if (= (logand (lsh (aref table ?a) -16) 255) 255)
|
|
766 (setq comments '8-bit)
|
|
767 (setq comments '1-bit)))
|
|
768 ;; XEmacs 20 is known to be 8-bit
|
|
769 ((eq flavor 'XEmacs) (setq comments '8-bit))
|
|
770 ;; Emacs 19.30 and beyond are known to be 1-bit
|
|
771 ((eq flavor 'FSF) (setq comments '1-bit))
|
|
772 ;; Don't know what this is
|
|
773 (t (error "Couldn't figure out syntax table format."))
|
|
774 ))
|
|
775 ;; Emacs 18 has no support for dual comments
|
|
776 (setq comments 'no-dual-comments))
|
|
777 ;; lets do some minimal sanity checking.
|
|
778 (if (and (or
|
|
779 ;; Lucid Emacs before 19.6 had bugs
|
|
780 (and (eq major 'v19) (eq flavor 'XEmacs) (< minor 6))
|
|
781 ;; Emacs 19 before 19.21 has known bugs
|
|
782 (and (eq major 'v19) (eq flavor 'FSF) (< minor 21)))
|
|
783 (not c-inhibit-startup-warnings-p))
|
|
784 (with-output-to-temp-buffer "*cc-mode warnings*"
|
|
785 (print (format
|
|
786 "The version of Emacs that you are running, %s,
|
|
787 has known bugs in its syntax.c parsing routines which will affect the
|
32
|
788 performance of CC Mode. You should strongly consider upgrading to the
|
|
789 latest available version. CC Mode may continue to work, after a
|
0
|
790 fashion, but strange indentation errors could be encountered."
|
|
791 emacs-version))))
|
|
792 ;; Emacs 18, with no patch is not too good
|
|
793 (if (and (eq major 'v18) (eq comments 'no-dual-comments)
|
|
794 (not c-inhibit-startup-warnings-p))
|
|
795 (with-output-to-temp-buffer "*cc-mode warnings*"
|
|
796 (print (format
|
|
797 "The version of Emacs 18 you are running, %s,
|
|
798 has known deficiencies in its ability to handle dual C++ comments,
|
|
799 i.e. C++ line style comments and C block style comments. This will
|
|
800 not be much of a problem for you if you are only editing C code, but
|
|
801 if you are doing much C++ editing, you should strongly consider
|
|
802 upgrading to one of the latest Emacs 19's. In Emacs 18, you may also
|
|
803 experience performance degradations. Emacs 19 has some new built-in
|
|
804 routines which will speed things up for you.
|
|
805
|
32
|
806 Because of these inherent problems, CC Mode is no longer being
|
0
|
807 actively maintained for Emacs 18, however, until you can upgrade to
|
32
|
808 Emacs 19, you may want to look at cc-mode-18.el in the CC Mode
|
0
|
809 distribution. THIS FILE IS COMPLETELY UNSUPPORTED! If you use it,
|
|
810 you are on your own, although patch contributions will be folded into
|
|
811 the main release."
|
|
812 emacs-version))))
|
|
813 ;; Emacs 18 with the syntax patches are no longer supported
|
|
814 (if (and (eq major 'v18) (not (eq comments 'no-dual-comments))
|
|
815 (not c-inhibit-startup-warnings-p))
|
|
816 (with-output-to-temp-buffer "*cc-mode warnings*"
|
|
817 (print (format
|
|
818 "You are running a syntax patched Emacs 18 variant. While this should
|
|
819 work for you, you may want to consider upgrading to Emacs 19. The
|
|
820 syntax patches are no longer supported either for syntax.c or
|
32
|
821 CC Mode."))))
|
|
822 (if infodock-p
|
|
823 (list major comments re-suite 'infodock)
|
|
824 (list major comments re-suite)))
|
0
|
825 "A list of features extant in the Emacs you are using.
|
|
826 There are many flavors of Emacs out there, each with different
|
32
|
827 features supporting those needed by CC Mode. Here's the current
|
0
|
828 supported list, along with the values for this variable:
|
|
829
|
|
830 Emacs 18/Epoch 4: (v18 no-dual-comments RS)
|
|
831 Emacs 18/Epoch 4 (patch2): (v18 8-bit RS)
|
|
832 XEmacs 19: (v19 8-bit RS)
|
2
|
833 XEmacs 20: (v20 8-bit RS)
|
0
|
834 Emacs 19: (v19 1-bit RS)
|
|
835
|
|
836 RS is the regular expression suite to use. XEmacs versions after
|
|
837 19.13, and Emacs versions after 19.29 use the `new-re' regex suite.
|
32
|
838 All other Emacsen use the `old-re' suite.
|
|
839
|
|
840 Infodock (based on XEmacs) has an additional symbol on this list:
|
|
841 'infodock")
|
0
|
842
|
|
843 (defvar c++-mode-abbrev-table nil
|
|
844 "Abbrev table in use in c++-mode buffers.")
|
|
845 (define-abbrev-table 'c++-mode-abbrev-table ())
|
|
846
|
|
847 (defvar c-mode-abbrev-table nil
|
|
848 "Abbrev table in use in c-mode buffers.")
|
|
849 (define-abbrev-table 'c-mode-abbrev-table ())
|
|
850
|
|
851 (defvar objc-mode-abbrev-table nil
|
|
852 "Abbrev table in use in objc-mode buffers.")
|
|
853 (define-abbrev-table 'objc-mode-abbrev-table ())
|
|
854
|
|
855 (defvar java-mode-abbrev-table nil
|
|
856 "Abbrev table in use in java-mode buffers.")
|
|
857 (define-abbrev-table 'java-mode-abbrev-table ())
|
|
858
|
|
859 (defun c-mode-fsf-menu (name map)
|
32
|
860 ;; Add menu to a keymap, but don't add them for XEmacs. This
|
|
861 ;; feature test will fail on other than Emacs 19.
|
0
|
862 (condition-case nil
|
|
863 (progn
|
|
864 (define-key map [menu-bar] (make-sparse-keymap))
|
|
865 (define-key map [menu-bar c] (cons name (make-sparse-keymap name)))
|
|
866
|
|
867 (define-key map [menu-bar c comment-region]
|
|
868 '("Comment Out Region" . comment-region))
|
|
869 (define-key map [menu-bar c c-macro-expand]
|
|
870 '("Macro Expand Region" . c-macro-expand))
|
|
871 (define-key map [menu-bar c c-backslash-region]
|
|
872 '("Backslashify" . c-backslash-region))
|
|
873 (define-key map [menu-bar c indent-exp]
|
|
874 '("Indent Expression" . c-indent-exp))
|
|
875 (define-key map [menu-bar c indent-line]
|
|
876 '("Indent Line" . c-indent-command))
|
|
877 (define-key map [menu-bar c fill]
|
|
878 '("Fill Comment Paragraph" . c-fill-paragraph))
|
32
|
879 (define-key map [menu-bar c separator2]
|
|
880 '("----"))
|
0
|
881 (define-key map [menu-bar c up]
|
|
882 '("Up Conditional" . c-up-conditional))
|
|
883 (define-key map [menu-bar c backward]
|
|
884 '("Backward Conditional" . c-backward-conditional))
|
|
885 (define-key map [menu-bar c forward]
|
|
886 '("Forward Conditional" . c-forward-conditional))
|
|
887 (define-key map [menu-bar c backward-stmt]
|
|
888 '("Backward Statement" . c-beginning-of-statement))
|
|
889 (define-key map [menu-bar c forward-stmt]
|
|
890 '("Forward Statement" . c-end-of-statement))
|
|
891
|
4
|
892 ;; RMS says: mouse-3 should not select this menu. mouse-3's
|
|
893 ;; global definition is useful in C mode and we should not
|
|
894 ;; interfere with that. The menu is mainly for beginners, and
|
|
895 ;; for them, the menubar requires less memory than a special
|
|
896 ;; click.
|
0
|
897 t)
|
|
898 (error nil)))
|
|
899
|
|
900 (defvar c-mode-map ()
|
|
901 "Keymap used in c-mode buffers.")
|
|
902 (if c-mode-map
|
|
903 ()
|
|
904 ;; TBD: should we even worry about naming this keymap. My vote: no,
|
|
905 ;; because Emacs and XEmacs do it differently.
|
|
906 (setq c-mode-map (make-sparse-keymap))
|
|
907 ;; put standard keybindings into MAP
|
|
908 ;; the following mappings correspond more or less directly to BOCM
|
|
909 (define-key c-mode-map "{" 'c-electric-brace)
|
|
910 (define-key c-mode-map "}" 'c-electric-brace)
|
|
911 (define-key c-mode-map ";" 'c-electric-semi&comma)
|
|
912 (define-key c-mode-map "#" 'c-electric-pound)
|
|
913 (define-key c-mode-map ":" 'c-electric-colon)
|
|
914 ;; Lucid Emacs 19.9 defined these two, the second of which was
|
|
915 ;; commented out...
|
|
916 ;; (define-key c-mode-map "\e{" 'c-insert-braces)
|
|
917 ;; Commented out electric square brackets because nobody likes them.
|
|
918 ;; (define-key c-mode-map "[" 'c-insert-brackets)
|
2
|
919 (define-key c-mode-map "\C-c\C-m" 'c-mark-function)
|
0
|
920 (define-key c-mode-map "\e\C-q" 'c-indent-exp)
|
|
921 (define-key c-mode-map "\ea" 'c-beginning-of-statement)
|
|
922 (define-key c-mode-map "\ee" 'c-end-of-statement)
|
|
923 ;; Emacs 19.30 introduces fill-paragraph-function, but it's not in
|
32
|
924 ;; every version of Emacs CC Mode supports.
|
0
|
925 (if (not (boundp 'fill-paragraph-function))
|
|
926 ;; I'd rather use an adaptive fill program instead of this.
|
|
927 (define-key c-mode-map "\eq" 'c-fill-paragraph))
|
|
928 (define-key c-mode-map "\C-c\C-n" 'c-forward-conditional)
|
|
929 (define-key c-mode-map "\C-c\C-p" 'c-backward-conditional)
|
|
930 (define-key c-mode-map "\C-c\C-u" 'c-up-conditional)
|
|
931 (define-key c-mode-map "\t" 'c-indent-command)
|
|
932 (define-key c-mode-map "\177" 'c-electric-delete)
|
|
933 ;; these are new keybindings, with no counterpart to BOCM
|
|
934 (define-key c-mode-map "," 'c-electric-semi&comma)
|
|
935 (define-key c-mode-map "*" 'c-electric-star)
|
|
936 (define-key c-mode-map "\C-c\C-q" 'c-indent-defun)
|
|
937 (define-key c-mode-map "\C-c\C-\\" 'c-backslash-region)
|
|
938 ;; TBD: where if anywhere, to put c-backward|forward-into-nomenclature
|
|
939 (define-key c-mode-map "\C-c\C-a" 'c-toggle-auto-state)
|
|
940 (define-key c-mode-map "\C-c\C-b" 'c-submit-bug-report)
|
|
941 (define-key c-mode-map "\C-c\C-c" 'comment-region)
|
|
942 (define-key c-mode-map "\C-c\C-d" 'c-toggle-hungry-state)
|
|
943 (define-key c-mode-map "\C-c\C-e" 'c-macro-expand)
|
|
944 (define-key c-mode-map "\C-c\C-o" 'c-set-offset)
|
|
945 (define-key c-mode-map "\C-c\C-s" 'c-show-syntactic-information)
|
|
946 (define-key c-mode-map "\C-c\C-t" 'c-toggle-auto-hungry-state)
|
32
|
947 (define-key c-mode-map "\C-c." 'c-set-style)
|
0
|
948 ;; conflicts with OOBR
|
|
949 ;;(define-key c-mode-map "\C-c\C-v" 'c-version)
|
|
950 ;;
|
32
|
951 (if (and
|
|
952 ;; Infodock has it's own menu
|
|
953 (not (memq 'infodock c-emacs-features))
|
|
954 ;; Emacs 19 defines menus in the mode map. This call will
|
|
955 ;; return t on Emacs 19, otherwise no-op and return nil.
|
|
956 (not (c-mode-fsf-menu "CC Mode" c-mode-map))
|
|
957 ;; In XEmacs 19, we want the menu to popup when the 3rd button
|
|
958 ;; is hit. In Lucid Emacs 19.10 and beyond this is done
|
|
959 ;; automatically if we put the menu on mode-popup-menu
|
|
960 ;; variable, see c-common-init. Emacs 19 uses C-Mouse-3 for
|
|
961 ;; this, and it works with no special effort.
|
|
962 (boundp 'current-menubar)
|
|
963 (not (boundp 'mode-popup-menu)))
|
0
|
964 (define-key c-mode-map 'button3 'c-popup-menu)))
|
|
965
|
|
966 (defvar c++-mode-map ()
|
|
967 "Keymap used in c++-mode buffers.")
|
|
968 (if c++-mode-map
|
|
969 ()
|
|
970 ;; In Emacs 19, it makes more sense to inherit c-mode-map
|
2
|
971 (if (or
|
|
972 (memq 'v19 c-emacs-features)
|
|
973 (memq 'v20 c-emacs-features))
|
0
|
974 ;; XEmacs and Emacs 19 do this differently
|
|
975 (cond
|
|
976 ;; XEmacs 19.13
|
|
977 ((fboundp 'set-keymap-parents)
|
|
978 (setq c++-mode-map (make-sparse-keymap))
|
|
979 (set-keymap-parents c++-mode-map c-mode-map))
|
|
980 ((fboundp 'set-keymap-parent)
|
|
981 (setq c++-mode-map (make-sparse-keymap))
|
|
982 (set-keymap-parent c++-mode-map c-mode-map))
|
|
983 (t (setq c++-mode-map (cons 'keymap c-mode-map))))
|
|
984 ;; Do it the hard way for Emacs 18 -- given by JWZ
|
|
985 (setq c++-mode-map (nconc (make-sparse-keymap) c-mode-map)))
|
|
986 ;; add bindings which are only useful for C++
|
|
987 (define-key c++-mode-map "\C-c:" 'c-scope-operator)
|
|
988 (define-key c++-mode-map "/" 'c-electric-slash)
|
|
989 (define-key c++-mode-map "<" 'c-electric-lt-gt)
|
|
990 (define-key c++-mode-map ">" 'c-electric-lt-gt)
|
|
991 ;; Emacs 19 defines menus in the mode map. This call will return
|
|
992 ;; t on Emacs 19, otherwise no-op and return nil.
|
4
|
993 ; (c-mode-fsf-menu "C++" c++-mode-map)
|
|
994 )
|
0
|
995
|
|
996 (defvar objc-mode-map ()
|
|
997 "Keymap used in objc-mode buffers.")
|
|
998 (if objc-mode-map
|
|
999 ()
|
|
1000 ;; In Emacs 19, it makes more sense to inherit c-mode-map
|
2
|
1001 (if (or (memq 'v19 c-emacs-features) (memq 'v20 c-emacs-features))
|
0
|
1002 ;; XEmacs and Emacs 19 do this differently
|
|
1003 (cond
|
|
1004 ;; XEmacs 19.13
|
|
1005 ((fboundp 'set-keymap-parents)
|
|
1006 (setq objc-mode-map (make-sparse-keymap))
|
|
1007 (set-keymap-parents objc-mode-map c-mode-map))
|
|
1008 ((fboundp 'set-keymap-parent)
|
|
1009 (setq objc-mode-map (make-sparse-keymap))
|
|
1010 (set-keymap-parent objc-mode-map c-mode-map))
|
|
1011 (t (setq objc-mode-map (cons 'keymap c-mode-map))))
|
|
1012 ;; Do it the hard way for Emacs 18 -- given by JWZ
|
|
1013 (setq objc-mode-map (nconc (make-sparse-keymap) c-mode-map)))
|
|
1014 ;; add bindings which are only useful for Objective-C
|
|
1015 (define-key objc-mode-map "/" 'c-electric-slash)
|
|
1016 ;; Emacs 19 defines menus in the mode map. This call will return
|
|
1017 ;; t on Emacs 19, otherwise no-op and return nil.
|
4
|
1018 ; (c-mode-fsf-menu "ObjC" objc-mode-map)
|
|
1019 )
|
0
|
1020
|
|
1021 (defvar java-mode-map ()
|
|
1022 "Keymap used in java-mode buffers.")
|
|
1023 (if java-mode-map
|
|
1024 ()
|
|
1025 ;; In Emacs 19, it makes more sense to inherit c-mode-map
|
2
|
1026 (if (or (memq 'v19 c-emacs-features) (memq 'v20 c-emacs-features))
|
0
|
1027 ;; XEmacs and Emacs 19 do this differently
|
|
1028 (cond
|
|
1029 ;; XEmacs 19.13
|
|
1030 ((fboundp 'set-keymap-parents)
|
|
1031 (setq java-mode-map (make-sparse-keymap))
|
|
1032 (set-keymap-parents java-mode-map c-mode-map))
|
|
1033 ((fboundp 'set-keymap-parent)
|
|
1034 (setq java-mode-map (make-sparse-keymap))
|
|
1035 (set-keymap-parent java-mode-map c-mode-map))
|
|
1036 (t (setq java-mode-map (cons 'keymap c-mode-map)))
|
|
1037 )
|
|
1038 ;; Do it the hard way for Emacs 18 -- given by JWZ
|
|
1039 (setq java-mode-map (nconc (make-sparse-keymap) c-mode-map)))
|
|
1040 ;; add bindings which are only useful for Java
|
|
1041 (define-key java-mode-map "/" 'c-electric-slash)
|
|
1042 ;; Emacs 19 defines menus in the mode map. This call will return t
|
|
1043 ;; on Emacs 19, otherwise no-op and return nil.
|
4
|
1044 ; (c-mode-fsf-menu "Java" java-mode-map)
|
|
1045 )
|
0
|
1046
|
|
1047 (defun c-populate-syntax-table (table)
|
|
1048 ;; Populate the syntax TABLE
|
|
1049 ;; DO NOT TRY TO SET _ (UNDERSCORE) TO WORD CLASS!
|
|
1050 (modify-syntax-entry ?_ "_" table)
|
|
1051 (modify-syntax-entry ?\\ "\\" table)
|
|
1052 (modify-syntax-entry ?+ "." table)
|
|
1053 (modify-syntax-entry ?- "." table)
|
|
1054 (modify-syntax-entry ?= "." table)
|
|
1055 (modify-syntax-entry ?% "." table)
|
|
1056 (modify-syntax-entry ?< "." table)
|
|
1057 (modify-syntax-entry ?> "." table)
|
|
1058 (modify-syntax-entry ?& "." table)
|
|
1059 (modify-syntax-entry ?| "." table)
|
|
1060 (modify-syntax-entry ?\' "\"" table))
|
|
1061
|
|
1062 (defun c-setup-dual-comments (table)
|
|
1063 ;; Set up TABLE to handle block and line style comments
|
|
1064 (cond
|
|
1065 ((memq '8-bit c-emacs-features)
|
|
1066 ;; XEmacs 19 has the best implementation
|
|
1067 (modify-syntax-entry ?/ ". 1456" table)
|
|
1068 (modify-syntax-entry ?* ". 23" table)
|
|
1069 (modify-syntax-entry ?\n "> b" table)
|
|
1070 ;; Give CR the same syntax as newline, for selective-display
|
|
1071 (modify-syntax-entry ?\^m "> b" table))
|
|
1072 ((memq '1-bit c-emacs-features)
|
|
1073 ;; Emacs 19 does things differently, but we can work with it
|
|
1074 (modify-syntax-entry ?/ ". 124b" table)
|
|
1075 (modify-syntax-entry ?* ". 23" table)
|
|
1076 (modify-syntax-entry ?\n "> b" table)
|
|
1077 ;; Give CR the same syntax as newline, for selective-display
|
|
1078 (modify-syntax-entry ?\^m "> b" table))
|
|
1079 ))
|
|
1080
|
|
1081 (defvar c-mode-syntax-table nil
|
|
1082 "Syntax table used in c-mode buffers.")
|
|
1083 (if c-mode-syntax-table
|
|
1084 ()
|
|
1085 (setq c-mode-syntax-table (make-syntax-table))
|
|
1086 (c-populate-syntax-table c-mode-syntax-table)
|
|
1087 ;; add extra comment syntax
|
|
1088 (modify-syntax-entry ?/ ". 14" c-mode-syntax-table)
|
|
1089 (modify-syntax-entry ?* ". 23" c-mode-syntax-table))
|
|
1090
|
|
1091 (defvar c++-mode-syntax-table nil
|
|
1092 "Syntax table used in c++-mode buffers.")
|
|
1093 (if c++-mode-syntax-table
|
|
1094 ()
|
|
1095 (setq c++-mode-syntax-table (make-syntax-table))
|
|
1096 (c-populate-syntax-table c++-mode-syntax-table)
|
|
1097 ;; add extra comment syntax
|
|
1098 (c-setup-dual-comments c++-mode-syntax-table)
|
|
1099 ;; TBD: does it make sense for colon to be symbol class in C++?
|
|
1100 ;; I'm not so sure, since c-label-key is busted on lines like:
|
|
1101 ;; Foo::bar( i );
|
|
1102 ;; maybe c-label-key should be fixed instead of commenting this out,
|
|
1103 ;; but it also bothers me that this only seems appropriate for C++
|
|
1104 ;; and not C.
|
|
1105 ;;(modify-syntax-entry ?: "_" c++-mode-syntax-table)
|
|
1106 )
|
|
1107
|
|
1108 (defvar objc-mode-syntax-table nil
|
|
1109 "Syntax table used in objc-mode buffers.")
|
|
1110 (if objc-mode-syntax-table
|
|
1111 ()
|
|
1112 (setq objc-mode-syntax-table (make-syntax-table))
|
|
1113 (c-populate-syntax-table objc-mode-syntax-table)
|
|
1114 ;; add extra comment syntax
|
|
1115 (c-setup-dual-comments objc-mode-syntax-table)
|
|
1116 ;; everyone gets these
|
|
1117 (modify-syntax-entry ?@ "_" objc-mode-syntax-table)
|
|
1118 )
|
|
1119
|
|
1120 (defvar java-mode-syntax-table nil
|
|
1121 "Syntax table used in java-mode buffers.")
|
|
1122 (if java-mode-syntax-table
|
|
1123 ()
|
|
1124 (setq java-mode-syntax-table (make-syntax-table))
|
|
1125 (c-populate-syntax-table java-mode-syntax-table)
|
|
1126 ;; add extra comment syntax
|
|
1127 (c-setup-dual-comments java-mode-syntax-table)
|
|
1128 ;; everyone gets these
|
|
1129 (modify-syntax-entry ?@ "_" java-mode-syntax-table)
|
|
1130 )
|
|
1131
|
|
1132 (defvar c-hungry-delete-key nil
|
|
1133 "Internal state of hungry delete key feature.")
|
|
1134 (defvar c-auto-newline nil
|
|
1135 "Internal state of auto newline feature.")
|
|
1136 (defvar c-auto-hungry-string nil
|
|
1137 "Internal auto-newline/hungry-delete designation string for mode line.")
|
|
1138 (defvar c-syntactic-context nil
|
|
1139 "Variable containing syntactic analysis list during indentation.")
|
|
1140 (defvar c-comment-start-regexp nil
|
|
1141 "Buffer local variable describing how comment are introduced.")
|
|
1142 (defvar c-conditional-key nil
|
|
1143 "Buffer local language-specific conditional keyword regexp.")
|
|
1144 (defvar c-access-key nil
|
|
1145 "Buffer local language-specific access key regexp.")
|
|
1146 (defvar c-class-key nil
|
|
1147 "Buffer local language-specific class key regexp.")
|
|
1148 (defvar c-method-key nil
|
|
1149 "Buffer local language-specific method regexp.")
|
|
1150 (defvar c-double-slash-is-comments-p nil
|
|
1151 "Buffer local language-specific comment style flag.")
|
|
1152 (defconst c-protection-key
|
|
1153 "\\<\\(public\\|protected\\|private\\)\\>"
|
|
1154 "Regexp describing protection keywords.")
|
|
1155 (defconst c-symbol-key "\\(\\w\\|\\s_\\)+"
|
|
1156 "Regexp describing a C/C++/ObjC symbol.
|
|
1157 We cannot use just `word' syntax class since `_' cannot be in word
|
|
1158 class. Putting underscore in word class breaks forward word movement
|
|
1159 behavior that users are familiar with.")
|
|
1160 (defconst c-baseclass-key
|
|
1161 (concat
|
|
1162 ":?[ \t]*\\(virtual[ \t]+\\)?\\("
|
|
1163 c-protection-key "[ \t]+\\)" c-symbol-key)
|
|
1164 "Regexp describing C++ base classes in a derived class definition.")
|
|
1165
|
2
|
1166 ;; defconst'd instead of defvar'd to override any old pre-loaded versions
|
|
1167 (defconst c-recognize-knr-p t
|
|
1168 "Non-nil means K&R style argument declarations are valid.")
|
32
|
1169 (defvar c-indentation-style c-site-default-style
|
|
1170 "Name of style installed in the current buffer.")
|
|
1171
|
|
1172 ;; these variables should always be buffer local. they do not affect
|
|
1173 ;; indentation styles.
|
|
1174 ;;
|
0
|
1175 ;; minor mode variables
|
|
1176 (make-variable-buffer-local 'c-auto-newline)
|
|
1177 (make-variable-buffer-local 'c-hungry-delete-key)
|
|
1178 (make-variable-buffer-local 'c-auto-hungry-string)
|
|
1179 ;; language differences
|
|
1180 (make-variable-buffer-local 'c-comment-start-regexp)
|
|
1181 (make-variable-buffer-local 'c-conditional-key)
|
|
1182 (make-variable-buffer-local 'c-access-key)
|
|
1183 (make-variable-buffer-local 'c-class-key)
|
|
1184 (make-variable-buffer-local 'c-method-key)
|
|
1185 (make-variable-buffer-local 'c-double-slash-is-comments-p)
|
|
1186 (make-variable-buffer-local 'c-baseclass-key)
|
|
1187 (make-variable-buffer-local 'c-recognize-knr-p)
|
|
1188 ;; style variables are made buffer local at tail end of this file.
|
|
1189
|
|
1190 ;; cmacexp is lame because it uses no preprocessor symbols.
|
|
1191 ;; It isn't very extensible either -- hardcodes /lib/cpp.
|
|
1192 ;; [I add it here only because c-mode has it -- BAW]
|
6
|
1193 (autoload 'c-macro-expand "cmacexp"
|
|
1194 "Display the result of expanding all C macros occurring in the region.
|
|
1195 The expansion is entirely correct because it uses the C preprocessor."
|
|
1196 t)
|
0
|
1197
|
|
1198
|
|
1199 ;; constant regular expressions for looking at various constructs
|
|
1200 (defconst c-C++-class-key "\\(class\\|struct\\|union\\)"
|
|
1201 "Regexp describing a C++ class declaration, including templates.")
|
|
1202 (defconst c-C-class-key "\\(struct\\|union\\)"
|
|
1203 "Regexp describing a C struct declaration.")
|
|
1204 (defconst c-inher-key
|
|
1205 (concat "\\(\\<static\\>\\s +\\)?"
|
|
1206 c-C++-class-key "[ \t]+" c-symbol-key
|
36
|
1207 "\\([ \t]*:[ \t]*\\)\\s *[^;]")
|
0
|
1208 "Regexp describing a class inheritance declaration.")
|
|
1209 (defconst c-switch-label-key
|
|
1210 "\\(\\(case[( \t]+\\S .*\\)\\|default[ \t]*\\):"
|
|
1211 "Regexp describing a switch's case or default label")
|
|
1212 (defconst c-C++-access-key
|
2
|
1213 (concat c-protection-key "[ \t]*:")
|
0
|
1214 "Regexp describing C++ access specification keywords.")
|
|
1215 (defconst c-label-key
|
|
1216 (concat c-symbol-key ":\\([^:]\\|$\\)")
|
|
1217 "Regexp describing any label.")
|
2
|
1218 (defconst c-C-conditionals '("for" "if" "do" "else" "while" "switch")
|
|
1219 "Shared conditional keywords for C-like languages.")
|
0
|
1220 (defconst c-C-conditional-key
|
2
|
1221 (concat "\\b\\("
|
|
1222 (mapconcat 'identity c-C-conditionals "\\|")
|
|
1223 "\\)\\b[^_]")
|
|
1224 "Regexp describing a conditional control for C.")
|
0
|
1225 (defconst c-C++-conditional-key
|
2
|
1226 (concat "\\b\\(" (mapconcat 'identity
|
|
1227 (append '("try" "catch") c-C-conditionals) "\\|")
|
|
1228 "\\)\\b[^_]")
|
0
|
1229 "Regexp describing a conditional control for C++.")
|
|
1230 (defconst c-C++-friend-key
|
|
1231 "friend[ \t]+\\|template[ \t]*<.+>[ \t]*friend[ \t]+"
|
|
1232 "Regexp describing friend declarations in C++ classes.")
|
40
|
1233
|
|
1234 ;; comment starter definitions for various languages. the language
|
|
1235 ;; modes will set c-comment-start-regexp to this value.
|
|
1236 (defconst c-C++-comment-start-regexp "/[/*]")
|
|
1237 (defconst c-C-comment-start-regexp "/[*]")
|
|
1238 ;; We need to match all 3 Java style comments
|
|
1239 ;; 1) Traditional C block; 2) javadoc /** ...; 3) C++ style
|
|
1240 (defconst c-Java-comment-start-regexp "/\\(/\\|[*][*]?\\)")
|
0
|
1241
|
|
1242 (defconst c-ObjC-method-key
|
|
1243 (concat
|
|
1244 "^\\s *[+-]\\s *"
|
|
1245 "\\(([^)]*)\\)?" ; return type
|
|
1246 ;; \\s- in objc syntax table does not include \n
|
|
1247 ;; since it is considered the end of //-comments.
|
|
1248 "[ \t\n]*" c-symbol-key)
|
|
1249 "Regexp describing an Objective-C method intro.")
|
|
1250 (defconst c-ObjC-access-key
|
|
1251 (concat "@" c-protection-key)
|
|
1252 "Regexp describing access specification keywords for Objective-C.")
|
|
1253 (defconst c-ObjC-class-key
|
|
1254 (concat
|
|
1255 "@\\(interface\\|implementation\\)\\s +"
|
|
1256 c-symbol-key ;name of the class
|
|
1257 "\\(\\s *:\\s *" c-symbol-key "\\)?" ;maybe followed by the superclass
|
|
1258 "\\(\\s *<[^>]+>\\)?" ;and maybe the adopted protocols list
|
|
1259 )
|
|
1260 "Regexp describing a class or protocol declaration for Objective-C.")
|
|
1261
|
|
1262 (defconst c-Java-method-key
|
|
1263 (concat
|
|
1264 "^\\s *[+-]\\s *"
|
|
1265 "\\(([^)]*)\\)?" ; return type
|
|
1266 ;; \\s- in java syntax table does not include \n
|
|
1267 ;; since it is considered the end of //-comments.
|
|
1268 "[ \t\n]*" c-symbol-key)
|
|
1269 "Regexp describing a Java method intro.")
|
32
|
1270 (defconst c-Java-access-key nil
|
|
1271 "Regexp describing access labels for Java.")
|
0
|
1272 (defconst c-Java-class-key
|
|
1273 (concat
|
32
|
1274 "\\(" c-protection-key "\\s +\\)?"
|
0
|
1275 "\\(interface\\|class\\)\\s +"
|
|
1276 c-symbol-key ;name of the class
|
|
1277 "\\(\\s *extends\\s *" c-symbol-key "\\)?" ;maybe followed by superclass
|
|
1278 ;;"\\(\\s *implements *[^{]+{\\)?" ;and maybe the adopted protocols list
|
|
1279 )
|
|
1280 "Regexp describing a class or protocol declaration for Java.")
|
32
|
1281 (defconst c-Java-special-key "\\(implements\\|extends\\|throws\\)[^_]"
|
|
1282 "Regexp describing Java inheritance and throws clauses.")
|
2
|
1283 (defconst c-Java-conditional-key
|
|
1284 (concat "\\b\\("
|
|
1285 (mapconcat 'identity
|
|
1286 (append '("try" "catch" "finally" "synchronized")
|
|
1287 c-C-conditionals) "\\|")
|
|
1288 "\\)\\b[^_]")
|
|
1289 "Regexp describing a conditional control for Java.")
|
32
|
1290 (defconst c-Java-defun-prompt-regexp
|
|
1291 "^[ \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:;.,{}()=]*\\|\\([_$a-zA-Z][_$.a-zA-Z0-9]*\\)\\)\\s-*\\(([^);{}]*)\\)?\\([] \t]*\\)\\(\\s-*\\<throws\\>\\s-*\\(\\([_$a-zA-Z][_$.a-zA-Z0-9]*\\)[, \t\n\r\f]*\\)+\\)?\\s-*"
|
|
1292 "Regexp describing the beginning of a Java top-level definition.")
|
0
|
1293
|
|
1294 ;; KLUDGE ALERT. We default these variables to their `C' values so
|
|
1295 ;; that non-cc-mode-ized modes that depend on c-mode will still work
|
|
1296 ;; out of the box. The most glaring example is awk-mode. There ought
|
|
1297 ;; to be a better way.
|
|
1298 (setq-default c-conditional-key c-C-conditional-key
|
|
1299 c-class-key c-C-class-key
|
|
1300 c-comment-start-regexp c-C-comment-start-regexp)
|
|
1301
|
|
1302
|
|
1303 ;; main entry points for the modes
|
|
1304 (defconst c-list-of-mode-names nil)
|
|
1305
|
6
|
1306 ;;;###autoload
|
0
|
1307 (defun c-mode ()
|
|
1308 "Major mode for editing K&R and ANSI C code.
|
|
1309 To submit a problem report, enter `\\[c-submit-bug-report]' from a
|
|
1310 c-mode buffer. This automatically sets up a mail buffer with version
|
|
1311 information already added. You just need to add a description of the
|
|
1312 problem, including a reproducible test case and send the message.
|
|
1313
|
32
|
1314 To see what version of CC Mode you are running, enter `\\[c-version]'.
|
0
|
1315
|
|
1316 The hook variable `c-mode-hook' is run with no args, if that value is
|
|
1317 bound and has a non-nil value. Also the hook `c-mode-common-hook' is
|
|
1318 run first.
|
|
1319
|
|
1320 Key bindings:
|
|
1321 \\{c-mode-map}"
|
|
1322 (interactive)
|
|
1323 (kill-all-local-variables)
|
|
1324 (set-syntax-table c-mode-syntax-table)
|
|
1325 (setq major-mode 'c-mode
|
|
1326 mode-name "C"
|
|
1327 local-abbrev-table c-mode-abbrev-table)
|
|
1328 (use-local-map c-mode-map)
|
|
1329 (c-common-init)
|
|
1330 (setq comment-start "/* "
|
|
1331 comment-end " */"
|
|
1332 comment-multi-line t
|
|
1333 c-conditional-key c-C-conditional-key
|
|
1334 c-class-key c-C-class-key
|
|
1335 c-baseclass-key nil
|
|
1336 c-comment-start-regexp c-C-comment-start-regexp
|
|
1337 imenu-generic-expression cc-imenu-c-generic-expression)
|
|
1338 (run-hooks 'c-mode-common-hook)
|
|
1339 (run-hooks 'c-mode-hook))
|
|
1340 (setq c-list-of-mode-names (cons "C" c-list-of-mode-names))
|
|
1341
|
6
|
1342 ;;;###autoload
|
0
|
1343 (defun c++-mode ()
|
|
1344 "Major mode for editing C++ code.
|
|
1345 To submit a problem report, enter `\\[c-submit-bug-report]' from a
|
|
1346 c++-mode buffer. This automatically sets up a mail buffer with
|
|
1347 version information already added. You just need to add a description
|
|
1348 of the problem, including a reproducible test case, and send the
|
|
1349 message.
|
|
1350
|
32
|
1351 To see what version of CC Mode you are running, enter `\\[c-version]'.
|
0
|
1352
|
|
1353 The hook variable `c++-mode-hook' is run with no args, if that
|
|
1354 variable is bound and has a non-nil value. Also the hook
|
|
1355 `c-mode-common-hook' is run first.
|
|
1356
|
|
1357 Key bindings:
|
|
1358 \\{c++-mode-map}"
|
|
1359 (interactive)
|
|
1360 (kill-all-local-variables)
|
|
1361 (set-syntax-table c++-mode-syntax-table)
|
|
1362 (setq major-mode 'c++-mode
|
|
1363 mode-name "C++"
|
|
1364 local-abbrev-table c++-mode-abbrev-table)
|
|
1365 (use-local-map c++-mode-map)
|
|
1366 (c-common-init)
|
|
1367 (setq comment-start "// "
|
|
1368 comment-end ""
|
|
1369 comment-multi-line nil
|
|
1370 c-conditional-key c-C++-conditional-key
|
|
1371 c-comment-start-regexp c-C++-comment-start-regexp
|
|
1372 c-class-key c-C++-class-key
|
|
1373 c-access-key c-C++-access-key
|
|
1374 c-double-slash-is-comments-p t
|
2
|
1375 c-recognize-knr-p nil
|
0
|
1376 imenu-generic-expression cc-imenu-c++-generic-expression)
|
|
1377 (run-hooks 'c-mode-common-hook)
|
|
1378 (run-hooks 'c++-mode-hook))
|
|
1379 (setq c-list-of-mode-names (cons "C++" c-list-of-mode-names))
|
|
1380
|
6
|
1381 ;;;###autoload
|
0
|
1382 (defun objc-mode ()
|
|
1383 "Major mode for editing Objective C code.
|
|
1384 To submit a problem report, enter `\\[c-submit-bug-report]' from an
|
|
1385 objc-mode buffer. This automatically sets up a mail buffer with
|
|
1386 version information already added. You just need to add a description
|
|
1387 of the problem, including a reproducible test case, and send the
|
|
1388 message.
|
|
1389
|
32
|
1390 To see what version of CC Mode you are running, enter `\\[c-version]'.
|
0
|
1391
|
|
1392 The hook variable `objc-mode-hook' is run with no args, if that value
|
|
1393 is bound and has a non-nil value. Also the hook `c-mode-common-hook'
|
|
1394 is run first.
|
|
1395
|
|
1396 Key bindings:
|
|
1397 \\{objc-mode-map}"
|
|
1398 (interactive)
|
|
1399 (kill-all-local-variables)
|
|
1400 (set-syntax-table objc-mode-syntax-table)
|
|
1401 (setq major-mode 'objc-mode
|
|
1402 mode-name "ObjC"
|
|
1403 local-abbrev-table objc-mode-abbrev-table)
|
|
1404 (use-local-map objc-mode-map)
|
|
1405 (c-common-init)
|
|
1406 (setq comment-start "// "
|
|
1407 comment-end ""
|
|
1408 comment-multi-line nil
|
|
1409 c-conditional-key c-C-conditional-key
|
|
1410 c-comment-start-regexp c-C++-comment-start-regexp
|
|
1411 c-class-key c-ObjC-class-key
|
|
1412 c-baseclass-key nil
|
|
1413 c-access-key c-ObjC-access-key
|
|
1414 c-double-slash-is-comments-p t
|
|
1415 c-method-key c-ObjC-method-key)
|
|
1416 (run-hooks 'c-mode-common-hook)
|
|
1417 (run-hooks 'objc-mode-hook))
|
|
1418 (setq c-list-of-mode-names (cons "ObjC" c-list-of-mode-names))
|
|
1419
|
6
|
1420 ;;;###autoload
|
0
|
1421 (defun java-mode ()
|
|
1422 "Major mode for editing Java code.
|
|
1423 To submit a problem report, enter `\\[c-submit-bug-report]' from an
|
|
1424 java-mode buffer. This automatically sets up a mail buffer with
|
|
1425 version information already added. You just need to add a description
|
|
1426 of the problem, including a reproducible test case and send the
|
|
1427 message.
|
|
1428
|
32
|
1429 To see what version of CC Mode you are running, enter `\\[c-version]'.
|
0
|
1430
|
|
1431 The hook variable `java-mode-hook' is run with no args, if that value
|
|
1432 is bound and has a non-nil value. Also the common hook
|
2
|
1433 `c-mode-common-hook' is run first. Note that this mode automatically
|
|
1434 sets the \"java\" style before calling any hooks so be careful if you
|
|
1435 set styles in `c-mode-common-hook'.
|
0
|
1436
|
|
1437 Key bindings:
|
|
1438 \\{java-mode-map}"
|
|
1439 (interactive)
|
|
1440 (kill-all-local-variables)
|
|
1441 (set-syntax-table java-mode-syntax-table)
|
|
1442 (setq major-mode 'java-mode
|
|
1443 mode-name "Java"
|
|
1444 local-abbrev-table java-mode-abbrev-table)
|
|
1445 (use-local-map java-mode-map)
|
|
1446 (c-common-init)
|
|
1447 (setq comment-start "// "
|
|
1448 comment-end ""
|
|
1449 comment-multi-line nil
|
|
1450 c-conditional-key c-Java-conditional-key
|
40
|
1451 c-comment-start-regexp c-Java-comment-start-regexp
|
0
|
1452 c-class-key c-Java-class-key
|
|
1453 c-method-key c-Java-method-key
|
|
1454 c-double-slash-is-comments-p t
|
|
1455 c-baseclass-key nil
|
2
|
1456 c-recognize-knr-p nil
|
32
|
1457 c-access-key c-Java-access-key
|
|
1458 ;defun-prompt-regexp c-Java-defun-prompt-regexp
|
|
1459 imenu-generic-expression cc-imenu-java-generic-expression
|
|
1460 )
|
2
|
1461 (c-set-style "java")
|
0
|
1462 (run-hooks 'c-mode-common-hook)
|
|
1463 (run-hooks 'java-mode-hook))
|
|
1464 (setq c-list-of-mode-names (cons "Java" c-list-of-mode-names))
|
|
1465
|
2
|
1466 (defun c-use-java-style ()
|
|
1467 "Institutes `java' indentation style.
|
|
1468 For use with the variable `java-mode-hook'."
|
|
1469 (c-set-style "java"))
|
|
1470
|
0
|
1471 (defun c-common-init ()
|
|
1472 ;; Common initializations for c++-mode and c-mode.
|
32
|
1473 ;;
|
|
1474 ;; these variables should always be buffer local; they do not affect
|
|
1475 ;; indentation style.
|
0
|
1476 (make-local-variable 'paragraph-start)
|
|
1477 (make-local-variable 'paragraph-separate)
|
|
1478 (make-local-variable 'paragraph-ignore-fill-prefix)
|
|
1479 (make-local-variable 'require-final-newline)
|
|
1480 (make-local-variable 'parse-sexp-ignore-comments)
|
|
1481 (make-local-variable 'indent-line-function)
|
|
1482 (make-local-variable 'indent-region-function)
|
|
1483 (make-local-variable 'comment-start)
|
|
1484 (make-local-variable 'comment-end)
|
|
1485 (make-local-variable 'comment-column)
|
|
1486 (make-local-variable 'comment-start-skip)
|
|
1487 (make-local-variable 'comment-multi-line)
|
|
1488 (make-local-variable 'outline-regexp)
|
|
1489 (make-local-variable 'outline-level)
|
|
1490 (make-local-variable 'adaptive-fill-regexp)
|
|
1491 (make-local-variable 'imenu-generic-expression) ;set in the mode functions
|
|
1492 ;; Emacs 19.30 and beyond only, AFAIK
|
|
1493 (if (boundp 'fill-paragraph-function)
|
|
1494 (progn
|
|
1495 (make-local-variable 'fill-paragraph-function)
|
|
1496 (setq fill-paragraph-function 'c-fill-paragraph)))
|
|
1497 ;; now set their values
|
|
1498 (setq paragraph-start (if (memq 'new-re c-emacs-features)
|
|
1499 (concat page-delimiter "\\|$")
|
|
1500 (concat "^$\\|" page-delimiter))
|
|
1501 paragraph-separate paragraph-start
|
|
1502 paragraph-ignore-fill-prefix t
|
|
1503 require-final-newline t
|
|
1504 parse-sexp-ignore-comments t
|
|
1505 indent-line-function 'c-indent-line
|
|
1506 indent-region-function 'c-indent-region
|
|
1507 outline-regexp "[^#\n\^M]"
|
|
1508 outline-level 'c-outline-level
|
|
1509 comment-column 32
|
|
1510 comment-start-skip "/\\*+ *\\|// *"
|
32
|
1511 ;; For all but XEmacs 19.13, the default should be nil
|
|
1512 adaptive-fill-regexp (and (memq 'v19 c-emacs-features)
|
|
1513 (= emacs-minor-version 13)
|
|
1514 "[ \t]*\\([#;>*]+ +\\)?")
|
|
1515 )
|
0
|
1516 ;; we have to do something special for c-offsets-alist so that the
|
|
1517 ;; buffer local value has its own alist structure.
|
|
1518 (setq c-offsets-alist (copy-alist c-offsets-alist))
|
|
1519 ;; setup the comment indent variable in a Emacs version portable way
|
|
1520 ;; ignore any byte compiler warnings you might get here
|
|
1521 (if (boundp 'comment-indent-function)
|
|
1522 (progn
|
|
1523 (make-local-variable 'comment-indent-function)
|
|
1524 (setq comment-indent-function 'c-comment-indent))
|
|
1525 (make-local-variable 'comment-indent-hook)
|
|
1526 (setq comment-indent-hook 'c-comment-indent))
|
|
1527 ;; Put C menu into menubar and on popup menu for XEmacs 19. I think
|
32
|
1528 ;; this happens automatically for Emacs 19. Skip it for Infodock.
|
|
1529 (if (and (not (memq 'infodock c-emacs-features))
|
|
1530 (boundp 'current-menubar)
|
0
|
1531 current-menubar
|
|
1532 (not (assoc mode-name current-menubar)))
|
|
1533 ;; its possible that this buffer has changed modes from one of
|
32
|
1534 ;; the other CC Mode modes. In that case, only the menubar
|
0
|
1535 ;; title of the menu changes.
|
|
1536 (let ((modes (copy-sequence c-list-of-mode-names))
|
|
1537 changed-p)
|
|
1538 (setq modes (delete major-mode modes))
|
|
1539 (while modes
|
|
1540 (if (not (assoc (car modes) current-menubar))
|
|
1541 (setq modes (cdr modes))
|
|
1542 (relabel-menu-item (list (car modes)) mode-name)
|
|
1543 (setq modes nil
|
|
1544 changed-p t)))
|
|
1545 (if (not changed-p)
|
|
1546 (progn
|
|
1547 (set-buffer-menubar (copy-sequence current-menubar))
|
32
|
1548 (if (fboundp 'add-submenu)
|
|
1549 (add-submenu nil (c-mode-menu))
|
|
1550 (add-menu nil mode-name c-mode-menu)
|
|
1551 )))))
|
0
|
1552 (if (boundp 'mode-popup-menu)
|
32
|
1553 (setq mode-popup-menu (c-mode-menu)))
|
0
|
1554 ;; put auto-hungry designators onto minor-mode-alist, but only once
|
|
1555 (or (assq 'c-auto-hungry-string minor-mode-alist)
|
|
1556 (setq minor-mode-alist
|
|
1557 (cons '(c-auto-hungry-string c-auto-hungry-string)
|
|
1558 minor-mode-alist))))
|
|
1559
|
|
1560 (defun c-postprocess-file-styles ()
|
|
1561 "Function that post processes relevant file local variables.
|
|
1562 Currently, this function simply applies any style and offset settings
|
|
1563 found in the file's Local Variable list. It first applies any style
|
|
1564 setting found in `c-file-style', then it applies any offset settings
|
|
1565 it finds in `c-file-offsets'."
|
|
1566 ;; apply file styles and offsets
|
|
1567 (and c-file-style
|
|
1568 (c-set-style c-file-style))
|
|
1569 (and c-file-offsets
|
|
1570 (mapcar
|
|
1571 (function
|
|
1572 (lambda (langentry)
|
|
1573 (let ((langelem (car langentry))
|
|
1574 (offset (cdr langentry)))
|
|
1575 (c-set-offset langelem offset)
|
|
1576 )))
|
|
1577 c-file-offsets)))
|
|
1578
|
|
1579 ;; Add the postprocessing function to hack-local-variables-hook. As
|
|
1580 ;; of 28-Aug-1995, XEmacs 19.12 and Emacs 19.29 support this.
|
|
1581 (and (fboundp 'add-hook)
|
|
1582 (add-hook 'hack-local-variables-hook 'c-postprocess-file-styles))
|
|
1583
|
|
1584 (defun c-enable-//-in-c-mode ()
|
|
1585 "Enables // as a comment delimiter in `c-mode'.
|
|
1586 ANSI C currently does *not* allow this, although many C compilers
|
|
1587 support optional C++ style comments. To use, call this function from
|
|
1588 your `.emacs' file before you visit any C files. The changes are
|
|
1589 global and affect all future `c-mode' buffers."
|
|
1590 (c-setup-dual-comments c-mode-syntax-table)
|
|
1591 (setq-default c-C-comment-start-regexp c-C++-comment-start-regexp))
|
|
1592
|
|
1593
|
|
1594 ;; macros must be defined before first use
|
40
|
1595 (defmacro c-add-syntax (symbol &optional relpos)
|
|
1596 ;; a simple macro to append the syntax in symbol to the syntax list.
|
|
1597 ;; try to increase performance by using this macro
|
|
1598 (` (setq syntax (cons (cons (, symbol) (, relpos)) syntax))))
|
|
1599
|
0
|
1600 (defmacro c-point (position)
|
|
1601 ;; Returns the value of point at certain commonly referenced POSITIONs.
|
|
1602 ;; POSITION can be one of the following symbols:
|
|
1603 ;;
|
|
1604 ;; bol -- beginning of line
|
|
1605 ;; eol -- end of line
|
|
1606 ;; bod -- beginning of defun
|
|
1607 ;; boi -- back to indentation
|
|
1608 ;; ionl -- indentation of next line
|
|
1609 ;; iopl -- indentation of previous line
|
|
1610 ;; bonl -- beginning of next line
|
|
1611 ;; bopl -- beginning of previous line
|
|
1612 ;;
|
|
1613 ;; This function does not modify point or mark.
|
|
1614 (or (and (eq 'quote (car-safe position))
|
|
1615 (null (cdr (cdr position))))
|
|
1616 (error "bad buffer position requested: %s" position))
|
|
1617 (setq position (nth 1 position))
|
|
1618 (` (let ((here (point)))
|
|
1619 (,@ (cond
|
|
1620 ((eq position 'bol) '((beginning-of-line)))
|
|
1621 ((eq position 'eol) '((end-of-line)))
|
|
1622 ((eq position 'bod)
|
|
1623 '((beginning-of-defun)
|
|
1624 ;; if defun-prompt-regexp is non-nil, b-o-d won't leave
|
|
1625 ;; us at the open brace.
|
|
1626 (and (boundp 'defun-prompt-regexp)
|
|
1627 defun-prompt-regexp
|
|
1628 (looking-at defun-prompt-regexp)
|
|
1629 (goto-char (match-end 0)))
|
|
1630 ))
|
|
1631 ((eq position 'boi) '((back-to-indentation)))
|
|
1632 ((eq position 'bonl) '((forward-line 1)))
|
|
1633 ((eq position 'bopl) '((forward-line -1)))
|
|
1634 ((eq position 'iopl)
|
|
1635 '((forward-line -1)
|
|
1636 (back-to-indentation)))
|
|
1637 ((eq position 'ionl)
|
|
1638 '((forward-line 1)
|
|
1639 (back-to-indentation)))
|
|
1640 (t (error "unknown buffer position requested: %s" position))
|
|
1641 ))
|
|
1642 (prog1
|
|
1643 (point)
|
|
1644 (goto-char here))
|
|
1645 ;; workaround for an Emacs18 bug -- blech! Well, at least it
|
|
1646 ;; doesn't hurt for v19
|
|
1647 (,@ nil)
|
|
1648 )))
|
|
1649
|
|
1650 (defmacro c-auto-newline ()
|
|
1651 ;; if auto-newline feature is turned on, insert a newline character
|
|
1652 ;; and return t, otherwise return nil.
|
|
1653 (` (and c-auto-newline
|
|
1654 (not (c-in-literal))
|
|
1655 (not (newline)))))
|
|
1656
|
|
1657 (defmacro c-safe (&rest body)
|
|
1658 ;; safely execute BODY, return nil if an error occurred
|
|
1659 (` (condition-case nil
|
|
1660 (progn (,@ body))
|
|
1661 (error nil))))
|
|
1662
|
|
1663 (defun c-insert-special-chars (arg)
|
|
1664 ;; simply call self-insert-command in Emacs 19
|
|
1665 (self-insert-command (prefix-numeric-value arg)))
|
|
1666
|
|
1667 (defun c-intersect-lists (list alist)
|
|
1668 ;; return the element of ALIST that matches the first element found
|
|
1669 ;; in LIST. Uses assq.
|
|
1670 (let (match)
|
|
1671 (while (and list
|
|
1672 (not (setq match (assq (car list) alist))))
|
|
1673 (setq list (cdr list)))
|
|
1674 match))
|
|
1675
|
|
1676 (defun c-lookup-lists (list alist1 alist2)
|
|
1677 ;; first, find the first entry from LIST that is present in ALIST1,
|
|
1678 ;; then find the entry in ALIST2 for that entry.
|
|
1679 (assq (car (c-intersect-lists list alist1)) alist2))
|
|
1680
|
|
1681
|
|
1682 ;; This is used by indent-for-comment to decide how much to indent a
|
|
1683 ;; comment in C code based on its context.
|
|
1684 (defun c-comment-indent ()
|
|
1685 (if (looking-at (concat "^\\(" c-comment-start-regexp "\\)"))
|
|
1686 0 ;Existing comment at bol stays there.
|
|
1687 (let ((opoint (point))
|
|
1688 placeholder)
|
|
1689 (save-excursion
|
|
1690 (beginning-of-line)
|
|
1691 (cond
|
|
1692 ;; CASE 1: A comment following a solitary close-brace should
|
|
1693 ;; have only one space.
|
|
1694 ((looking-at (concat "[ \t]*}[ \t]*\\($\\|"
|
|
1695 c-comment-start-regexp
|
|
1696 "\\)"))
|
|
1697 (search-forward "}")
|
|
1698 (1+ (current-column)))
|
|
1699 ;; CASE 2: 2 spaces after #endif
|
|
1700 ((or (looking-at "^#[ \t]*endif[ \t]*")
|
|
1701 (looking-at "^#[ \t]*else[ \t]*"))
|
|
1702 7)
|
|
1703 ;; CASE 3: when comment-column is nil, calculate the offset
|
|
1704 ;; according to c-offsets-alist. E.g. identical to hitting
|
|
1705 ;; TAB.
|
|
1706 ((and c-indent-comments-syntactically-p
|
|
1707 (save-excursion
|
|
1708 (skip-chars-forward " \t")
|
|
1709 (or (looking-at comment-start)
|
|
1710 (eolp))))
|
|
1711 (let ((syntax (c-guess-basic-syntax)))
|
|
1712 ;; BOGOSITY ALERT: if we're looking at the eol, its
|
|
1713 ;; because indent-for-comment hasn't put the comment-start
|
|
1714 ;; in the buffer yet. this will screw up the syntactic
|
|
1715 ;; analysis so we kludge in the necessary info. Another
|
|
1716 ;; kludge is that if we're at the bol, then we really want
|
|
1717 ;; to ignore any anchoring as specified by
|
|
1718 ;; c-comment-only-line-offset since it doesn't apply here.
|
|
1719 (if (save-excursion
|
|
1720 (beginning-of-line)
|
|
1721 (skip-chars-forward " \t")
|
|
1722 (eolp))
|
|
1723 (c-add-syntax 'comment-intro))
|
|
1724 (let ((c-comment-only-line-offset
|
|
1725 (if (consp c-comment-only-line-offset)
|
|
1726 c-comment-only-line-offset
|
|
1727 (cons c-comment-only-line-offset
|
|
1728 c-comment-only-line-offset))))
|
|
1729 (apply '+ (mapcar 'c-get-offset syntax)))))
|
|
1730 ;; CASE 4: use comment-column if previous line is a
|
|
1731 ;; comment-only line indented to the left of comment-column
|
|
1732 ((save-excursion
|
|
1733 (beginning-of-line)
|
|
1734 (and (not (bobp))
|
|
1735 (forward-line -1))
|
|
1736 (skip-chars-forward " \t")
|
|
1737 (prog1
|
|
1738 (looking-at c-comment-start-regexp)
|
|
1739 (setq placeholder (point))))
|
|
1740 (goto-char placeholder)
|
|
1741 (if (< (current-column) comment-column)
|
|
1742 comment-column
|
|
1743 (current-column)))
|
|
1744 ;; CASE 5: If comment-column is 0, and nothing but space
|
|
1745 ;; before the comment, align it at 0 rather than 1.
|
|
1746 ((progn
|
|
1747 (goto-char opoint)
|
|
1748 (skip-chars-backward " \t")
|
|
1749 (and (= comment-column 0) (bolp)))
|
|
1750 0)
|
|
1751 ;; CASE 6: indent at comment column except leave at least one
|
|
1752 ;; space.
|
|
1753 (t (max (1+ (current-column))
|
|
1754 comment-column))
|
|
1755 )))))
|
|
1756
|
|
1757 ;; used by outline-minor-mode
|
|
1758 (defun c-outline-level ()
|
|
1759 (save-excursion
|
|
1760 (skip-chars-forward "\t ")
|
|
1761 (current-column)))
|
|
1762
|
|
1763 ;; active regions, and auto-newline/hungry delete key
|
|
1764 (defun c-keep-region-active ()
|
|
1765 ;; Do whatever is necessary to keep the region active in
|
|
1766 ;; XEmacs 19. ignore byte-compiler warnings you might see
|
|
1767 (and (boundp 'zmacs-region-stays)
|
|
1768 (setq zmacs-region-stays t)))
|
|
1769
|
|
1770 (defun c-update-modeline ()
|
|
1771 ;; set the c-auto-hungry-string for the correct designation on the modeline
|
|
1772 (setq c-auto-hungry-string
|
|
1773 (if c-auto-newline
|
|
1774 (if c-hungry-delete-key "/ah" "/a")
|
|
1775 (if c-hungry-delete-key "/h" nil)))
|
|
1776 ;; updates the modeline for all Emacsen
|
2
|
1777 (if (or (memq 'v19 c-emacs-features) (memq 'v20 c-emacs-features))
|
32
|
1778 (if (boundp 'redraw-modeline)
|
|
1779 (redraw-modeline)
|
|
1780 (force-mode-line-update))
|
0
|
1781 (set-buffer-modified-p (buffer-modified-p))))
|
|
1782
|
|
1783 (defun c-calculate-state (arg prevstate)
|
|
1784 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
|
|
1785 ;; arg is nil or zero, toggle the state. If arg is negative, turn
|
|
1786 ;; the state off, and if arg is positive, turn the state on
|
|
1787 (if (or (not arg)
|
|
1788 (zerop (setq arg (prefix-numeric-value arg))))
|
|
1789 (not prevstate)
|
|
1790 (> arg 0)))
|
|
1791
|
|
1792 (defun c-toggle-auto-state (arg)
|
|
1793 "Toggle auto-newline feature.
|
|
1794 Optional numeric ARG, if supplied turns on auto-newline when positive,
|
|
1795 turns it off when negative, and just toggles it when zero.
|
|
1796
|
|
1797 When the auto-newline feature is enabled (as evidenced by the `/a' or
|
|
1798 `/ah' on the modeline after the mode name) newlines are automatically
|
|
1799 inserted after special characters such as brace, comma, semi-colon,
|
|
1800 and colon."
|
|
1801 (interactive "P")
|
|
1802 (setq c-auto-newline (c-calculate-state arg c-auto-newline))
|
|
1803 (c-update-modeline)
|
|
1804 (c-keep-region-active))
|
|
1805
|
|
1806 (defun c-toggle-hungry-state (arg)
|
|
1807 "Toggle hungry-delete-key feature.
|
|
1808 Optional numeric ARG, if supplied turns on hungry-delete when positive,
|
|
1809 turns it off when negative, and just toggles it when zero.
|
|
1810
|
|
1811 When the hungry-delete-key feature is enabled (as evidenced by the
|
|
1812 `/h' or `/ah' on the modeline after the mode name) the delete key
|
|
1813 gobbles all preceding whitespace in one fell swoop."
|
|
1814 (interactive "P")
|
|
1815 (setq c-hungry-delete-key (c-calculate-state arg c-hungry-delete-key))
|
|
1816 (c-update-modeline)
|
|
1817 (c-keep-region-active))
|
|
1818
|
|
1819 (defun c-toggle-auto-hungry-state (arg)
|
|
1820 "Toggle auto-newline and hungry-delete-key features.
|
|
1821 Optional numeric ARG, if supplied turns on auto-newline and
|
|
1822 hungry-delete when positive, turns them off when negative, and just
|
|
1823 toggles them when zero.
|
|
1824
|
|
1825 See `c-toggle-auto-state' and `c-toggle-hungry-state' for details."
|
|
1826 (interactive "P")
|
|
1827 (setq c-auto-newline (c-calculate-state arg c-auto-newline))
|
|
1828 (setq c-hungry-delete-key (c-calculate-state arg c-hungry-delete-key))
|
|
1829 (c-update-modeline)
|
|
1830 (c-keep-region-active))
|
|
1831
|
|
1832
|
|
1833 ;; COMMANDS
|
|
1834 (defun c-electric-delete (arg)
|
|
1835 "Deletes preceding character or whitespace.
|
|
1836 If `c-hungry-delete-key' is non-nil, as evidenced by the \"/h\" or
|
|
1837 \"/ah\" string on the mode line, then all preceding whitespace is
|
|
1838 consumed. If however an ARG is supplied, or `c-hungry-delete-key' is
|
|
1839 nil, or point is inside a literal then the function in the variable
|
|
1840 `c-delete-function' is called."
|
|
1841 (interactive "P")
|
|
1842 (if (or (not c-hungry-delete-key)
|
|
1843 arg
|
|
1844 (c-in-literal))
|
|
1845 (funcall c-delete-function (prefix-numeric-value arg))
|
|
1846 (let ((here (point)))
|
|
1847 (skip-chars-backward " \t\n")
|
|
1848 (if (/= (point) here)
|
|
1849 (delete-region (point) here)
|
|
1850 (funcall c-delete-function 1)
|
|
1851 ))))
|
|
1852
|
|
1853 (defun c-electric-pound (arg)
|
|
1854 "Electric pound (`#') insertion.
|
|
1855 Inserts a `#' character specially depending on the variable
|
|
1856 `c-electric-pound-behavior'. If a numeric ARG is supplied, or if
|
|
1857 point is inside a literal, nothing special happens."
|
|
1858 (interactive "P")
|
|
1859 (if (or (c-in-literal)
|
|
1860 arg
|
|
1861 (not (memq 'alignleft c-electric-pound-behavior)))
|
|
1862 ;; do nothing special
|
|
1863 (self-insert-command (prefix-numeric-value arg))
|
|
1864 ;; place the pound character at the left edge
|
|
1865 (let ((pos (- (point-max) (point)))
|
|
1866 (bolp (bolp)))
|
|
1867 (beginning-of-line)
|
|
1868 (delete-horizontal-space)
|
|
1869 (insert-char last-command-char 1)
|
|
1870 (and (not bolp)
|
|
1871 (goto-char (- (point-max) pos)))
|
|
1872 )))
|
|
1873
|
|
1874 (defun c-electric-brace (arg)
|
|
1875 "Insert a brace.
|
|
1876
|
|
1877 If the auto-newline feature is turned on, as evidenced by the \"/a\"
|
|
1878 or \"/ah\" string on the mode line, newlines are inserted before and
|
|
1879 after braces based on the value of `c-hanging-braces-alist'.
|
|
1880
|
|
1881 Also, the line is re-indented unless a numeric ARG is supplied, there
|
|
1882 are non-whitespace characters present on the line after the brace, or
|
|
1883 the brace is inserted inside a literal."
|
|
1884 (interactive "P")
|
|
1885 (let* ((c-state-cache (c-parse-state))
|
|
1886 (safepos (c-safe-position (point) c-state-cache))
|
|
1887 (literal (c-in-literal safepos)))
|
|
1888 ;; if we're in a literal, or we're not at the end of the line, or
|
|
1889 ;; a numeric arg is provided, or auto-newlining is turned off,
|
|
1890 ;; then just insert the character.
|
|
1891 (if (or literal arg
|
|
1892 ; (not c-auto-newline)
|
|
1893 (not (looking-at "[ \t]*$")))
|
|
1894 (c-insert-special-chars arg)
|
|
1895 (let* ((syms '(class-open class-close defun-open defun-close
|
|
1896 inline-open inline-close brace-list-open brace-list-close
|
|
1897 brace-list-intro brace-list-entry block-open block-close
|
2
|
1898 substatement-open statement-case-open
|
|
1899 extern-lang-open extern-lang-close))
|
0
|
1900 ;; we want to inhibit blinking the paren since this will
|
|
1901 ;; be most disruptive. we'll blink it ourselves later on
|
|
1902 (old-blink-paren (if (boundp 'blink-paren-function)
|
|
1903 blink-paren-function
|
|
1904 blink-paren-hook))
|
|
1905 blink-paren-function ; emacs19
|
|
1906 blink-paren-hook ; emacs18
|
|
1907 (insertion-point (point))
|
|
1908 delete-temp-newline
|
|
1909 (preserve-p (= 32 (char-syntax (preceding-char))))
|
|
1910 ;; shut this up too
|
|
1911 (c-echo-syntactic-information-p nil)
|
|
1912 (syntax (progn
|
|
1913 ;; only insert a newline if there is
|
|
1914 ;; non-whitespace behind us
|
|
1915 (if (save-excursion
|
|
1916 (skip-chars-backward " \t")
|
|
1917 (not (bolp)))
|
|
1918 (progn (newline)
|
|
1919 (setq delete-temp-newline t)))
|
|
1920 (self-insert-command (prefix-numeric-value arg))
|
|
1921 ;; state cache doesn't change
|
|
1922 (c-guess-basic-syntax)))
|
|
1923 (newlines (and
|
|
1924 c-auto-newline
|
|
1925 (or (c-lookup-lists syms syntax c-hanging-braces-alist)
|
|
1926 '(ignore before after)))))
|
|
1927 ;; If syntax is a function symbol, then call it using the
|
|
1928 ;; defined semantics.
|
|
1929 (if (and (not (consp (cdr newlines)))
|
2
|
1930 (c-functionp (cdr newlines)))
|
0
|
1931 (let ((c-syntactic-context syntax))
|
|
1932 (setq newlines
|
|
1933 (funcall (cdr newlines) (car newlines) insertion-point))))
|
|
1934 ;; does a newline go before the open brace?
|
|
1935 (if (memq 'before newlines)
|
|
1936 ;; we leave the newline we've put in there before,
|
|
1937 ;; but we need to re-indent the line above
|
|
1938 (let ((pos (- (point-max) (point)))
|
|
1939 (here (point))
|
|
1940 (c-state-cache c-state-cache))
|
|
1941 (forward-line -1)
|
|
1942 ;; we may need to update the cache. this should still be
|
|
1943 ;; faster than recalculating the state in many cases
|
|
1944 (save-excursion
|
|
1945 (save-restriction
|
|
1946 (narrow-to-region here (point))
|
|
1947 (if (and (c-safe (progn (backward-up-list -1) t))
|
|
1948 (memq (preceding-char) '(?\) ?}))
|
|
1949 (progn (widen)
|
|
1950 (c-safe (progn (forward-sexp -1) t))))
|
|
1951 (setq c-state-cache
|
|
1952 (c-hack-state (point) 'open c-state-cache))
|
|
1953 (if (and (car c-state-cache)
|
|
1954 (not (consp (car c-state-cache)))
|
|
1955 (<= (point) (car c-state-cache)))
|
|
1956 (setq c-state-cache (cdr c-state-cache))
|
|
1957 ))))
|
|
1958 (let ((here (point))
|
|
1959 (shift (c-indent-line)))
|
|
1960 (setq c-state-cache (c-adjust-state (c-point 'bol) here
|
|
1961 (- shift) c-state-cache)))
|
|
1962 (goto-char (- (point-max) pos))
|
|
1963 ;; if the buffer has changed due to the indentation, we
|
|
1964 ;; need to recalculate syntax for the current line, but
|
|
1965 ;; we won't need to update the state cache.
|
|
1966 (if (/= (point) here)
|
|
1967 (setq syntax (c-guess-basic-syntax))))
|
|
1968 ;; must remove the newline we just stuck in (if we really did it)
|
|
1969 (and delete-temp-newline
|
|
1970 (save-excursion
|
|
1971 ;; if there is whitespace before point, then preserve
|
|
1972 ;; at least one space.
|
|
1973 (delete-indentation)
|
|
1974 (just-one-space)
|
|
1975 (if (not preserve-p)
|
|
1976 (delete-char -1))))
|
|
1977 ;; since we're hanging the brace, we need to recalculate
|
|
1978 ;; syntax. Update the state to accurately reflect the
|
|
1979 ;; beginning of the line. We punt if we cross any open or
|
|
1980 ;; closed parens because its just too hard to modify the
|
|
1981 ;; known state. This limitation will be fixed in v5.
|
|
1982 (save-excursion
|
|
1983 (let ((bol (c-point 'bol)))
|
|
1984 (if (zerop (car (parse-partial-sexp bol (1- (point)))))
|
|
1985 (setq c-state-cache (c-whack-state bol c-state-cache)
|
|
1986 syntax (c-guess-basic-syntax))
|
|
1987 ;; gotta punt. this requires some horrible kludgery
|
|
1988 (beginning-of-line)
|
|
1989 (makunbound 'c-state-cache)
|
|
1990 (setq c-state-cache (c-parse-state)
|
|
1991 syntax nil))))
|
|
1992 )
|
|
1993 ;; now adjust the line's indentation. don't update the state
|
|
1994 ;; cache since c-guess-basic-syntax isn't called when the
|
|
1995 ;; syntax is passed to c-indent-line
|
|
1996 (let ((here (point))
|
|
1997 (shift (c-indent-line syntax)))
|
|
1998 (setq c-state-cache (c-adjust-state (c-point 'bol) here
|
|
1999 (- shift) c-state-cache)))
|
|
2000 ;; Do all appropriate clean ups
|
|
2001 (let ((here (point))
|
|
2002 (pos (- (point-max) (point)))
|
|
2003 mbeg mend)
|
|
2004 ;; clean up empty defun braces
|
|
2005 (if (and c-auto-newline
|
|
2006 (memq 'empty-defun-braces c-cleanup-list)
|
|
2007 (= last-command-char ?\})
|
|
2008 (c-intersect-lists '(defun-close class-close inline-close)
|
|
2009 syntax)
|
|
2010 (progn
|
|
2011 (forward-char -1)
|
|
2012 (skip-chars-backward " \t\n")
|
|
2013 (= (preceding-char) ?\{))
|
|
2014 ;; make sure matching open brace isn't in a comment
|
|
2015 (not (c-in-literal)))
|
|
2016 (delete-region (point) (1- here)))
|
|
2017 ;; clean up brace-else-brace
|
|
2018 (if (and c-auto-newline
|
|
2019 (memq 'brace-else-brace c-cleanup-list)
|
|
2020 (= last-command-char ?\{)
|
|
2021 (re-search-backward "}[ \t\n]*else[ \t\n]*{" nil t)
|
|
2022 (progn
|
|
2023 (setq mbeg (match-beginning 0)
|
|
2024 mend (match-end 0))
|
|
2025 (= mend here))
|
|
2026 (not (c-in-literal)))
|
|
2027 (progn
|
|
2028 (delete-region mbeg mend)
|
|
2029 (insert "} else {")))
|
32
|
2030 ;; clean up brace-elseif-brace
|
|
2031 (if (and c-auto-newline
|
|
2032 (memq 'brace-elseif-brace c-cleanup-list)
|
|
2033 (= last-command-char ?\{)
|
|
2034 (re-search-backward "}[ \t\n]*else[ \t\n]+if[ \t\n]*" nil t)
|
|
2035 (save-excursion
|
|
2036 (goto-char (match-end 0))
|
|
2037 (c-safe (forward-sexp 1))
|
|
2038 (skip-chars-forward " \t\n")
|
|
2039 (setq mbeg (match-beginning 0)
|
|
2040 mend (match-end 0))
|
|
2041 (= here (1+ (point))))
|
|
2042 (not (c-in-literal)))
|
|
2043 (progn
|
|
2044 (delete-region mbeg mend)
|
|
2045 (insert "} else if ")))
|
0
|
2046 (goto-char (- (point-max) pos))
|
|
2047 )
|
|
2048 ;; does a newline go after the brace?
|
|
2049 (if (memq 'after newlines)
|
|
2050 (progn
|
|
2051 (newline)
|
|
2052 ;; update on c-state-cache
|
|
2053 (let* ((bufpos (- (point) 2))
|
|
2054 (which (if (= (char-after bufpos) ?{) 'open 'close))
|
|
2055 (c-state-cache (c-hack-state bufpos which c-state-cache)))
|
|
2056 (c-indent-line))))
|
|
2057 ;; blink the paren
|
|
2058 (and (= last-command-char ?\})
|
|
2059 old-blink-paren
|
|
2060 (save-excursion
|
|
2061 (c-backward-syntactic-ws safepos)
|
|
2062 (if (boundp 'blink-paren-function)
|
|
2063 (funcall old-blink-paren)
|
|
2064 (run-hooks old-blink-paren))))
|
|
2065 ))))
|
|
2066
|
|
2067 (defun c-electric-slash (arg)
|
|
2068 "Insert a slash character.
|
|
2069 If slash is second of a double-slash C++ style comment introducing
|
|
2070 construct, and we are on a comment-only-line, indent line as comment.
|
|
2071 If numeric ARG is supplied or point is inside a literal, indentation
|
|
2072 is inhibited."
|
|
2073 (interactive "P")
|
|
2074 (let ((indentp (and (not arg)
|
|
2075 (= (preceding-char) ?/)
|
|
2076 (= last-command-char ?/)
|
|
2077 (not (c-in-literal))))
|
|
2078 ;; shut this up
|
|
2079 (c-echo-syntactic-information-p nil))
|
|
2080 (self-insert-command (prefix-numeric-value arg))
|
|
2081 (if indentp
|
|
2082 (c-indent-line))))
|
|
2083
|
|
2084 (defun c-electric-star (arg)
|
|
2085 "Insert a star character.
|
|
2086 If the star is the second character of a C style comment introducing
|
|
2087 construct, and we are on a comment-only-line, indent line as comment.
|
|
2088 If numeric ARG is supplied or point is inside a literal, indentation
|
|
2089 is inhibited."
|
|
2090 (interactive "P")
|
|
2091 (self-insert-command (prefix-numeric-value arg))
|
|
2092 ;; if we are in a literal, or if arg is given do not re-indent the
|
|
2093 ;; current line, unless this star introduces a comment-only line.
|
|
2094 (if (and (not arg)
|
|
2095 (memq (c-in-literal) '(c))
|
|
2096 (= (preceding-char) ?*)
|
|
2097 (save-excursion
|
|
2098 (forward-char -1)
|
|
2099 (skip-chars-backward "*")
|
|
2100 (if (= (preceding-char) ?/)
|
|
2101 (forward-char -1))
|
|
2102 (skip-chars-backward " \t")
|
|
2103 (bolp)))
|
|
2104 ;; shut this up
|
|
2105 (let (c-echo-syntactic-information-p)
|
|
2106 (c-indent-line))
|
|
2107 ))
|
|
2108
|
|
2109 (defun c-electric-semi&comma (arg)
|
|
2110 "Insert a comma or semicolon.
|
|
2111 When the auto-newline feature is turned on, as evidenced by the \"/a\"
|
|
2112 or \"/ah\" string on the mode line, a newline might be inserted. See
|
|
2113 the variable `c-hanging-semi&comma-criteria' for how newline insertion
|
|
2114 is determined.
|
|
2115
|
|
2116 When semicolon is inserted, the line is re-indented unless a numeric
|
|
2117 arg is supplied, point is inside a literal, or there are
|
|
2118 non-whitespace characters on the line following the semicolon."
|
|
2119 (interactive "P")
|
|
2120 (let* ((lim (c-most-enclosing-brace (c-parse-state)))
|
|
2121 (literal (c-in-literal lim))
|
|
2122 (here (point))
|
|
2123 ;; shut this up
|
|
2124 (c-echo-syntactic-information-p nil))
|
|
2125 (if (or literal
|
|
2126 arg
|
|
2127 (not (looking-at "[ \t]*$")))
|
|
2128 (c-insert-special-chars arg)
|
|
2129 ;; do some special stuff with the character
|
|
2130 (self-insert-command (prefix-numeric-value arg))
|
|
2131 ;; do all cleanups, reindentations, and newline insertions, but
|
|
2132 ;; only if c-auto-newline is turned on
|
|
2133 (if (not c-auto-newline) nil
|
|
2134 ;; clean ups
|
|
2135 (let ((pos (- (point-max) (point))))
|
|
2136 (if (and (or (and
|
|
2137 (= last-command-char ?,)
|
|
2138 (memq 'list-close-comma c-cleanup-list))
|
|
2139 (and
|
|
2140 (= last-command-char ?\;)
|
|
2141 (memq 'defun-close-semi c-cleanup-list)))
|
|
2142 (progn
|
|
2143 (forward-char -1)
|
|
2144 (skip-chars-backward " \t\n")
|
|
2145 (= (preceding-char) ?}))
|
|
2146 ;; make sure matching open brace isn't in a comment
|
|
2147 (not (c-in-literal lim)))
|
|
2148 (delete-region (point) here))
|
|
2149 (goto-char (- (point-max) pos)))
|
|
2150 ;; re-indent line
|
|
2151 (c-indent-line)
|
|
2152 ;; check to see if a newline should be added
|
|
2153 (let ((criteria c-hanging-semi&comma-criteria)
|
|
2154 answer add-newline-p)
|
|
2155 (while criteria
|
|
2156 (setq answer (funcall (car criteria)))
|
|
2157 ;; only nil value means continue checking
|
|
2158 (if (not answer)
|
|
2159 (setq criteria (cdr criteria))
|
|
2160 (setq criteria nil)
|
|
2161 ;; only 'stop specifically says do not add a newline
|
|
2162 (setq add-newline-p (not (eq answer 'stop)))
|
|
2163 ))
|
|
2164 (if add-newline-p
|
|
2165 (progn (newline)
|
|
2166 (c-indent-line)))
|
|
2167 )))))
|
|
2168
|
|
2169 (defun c-semi&comma-inside-parenlist ()
|
|
2170 "Determine if a newline should be added after a semicolon.
|
|
2171 If a comma was inserted, no determination is made. If a semicolon was
|
|
2172 inserted inside a parenthesis list, no newline is added otherwise a
|
|
2173 newline is added. In either case, checking is stopped. This supports
|
|
2174 exactly the old newline insertion behavior."
|
|
2175 ;; newline only after semicolon, but only if that semicolon is not
|
|
2176 ;; inside a parenthesis list (e.g. a for loop statement)
|
|
2177 (if (/= last-command-char ?\;)
|
|
2178 nil ; continue checking
|
|
2179 (if (condition-case nil
|
|
2180 (save-excursion
|
|
2181 (up-list -1)
|
|
2182 (/= (following-char) ?\())
|
|
2183 (error t))
|
|
2184 t
|
|
2185 'stop)))
|
|
2186
|
|
2187 (defun c-electric-colon (arg)
|
|
2188 "Insert a colon.
|
|
2189
|
|
2190 If the auto-newline feature is turned on, as evidenced by the \"/a\"
|
|
2191 or \"/ah\" string on the mode line, newlines are inserted before and
|
|
2192 after colons based on the value of `c-hanging-colons-alist'.
|
|
2193
|
|
2194 Also, the line is re-indented unless a numeric ARG is supplied, there
|
|
2195 are non-whitespace characters present on the line after the colon, or
|
|
2196 the colon is inserted inside a literal.
|
|
2197
|
|
2198 This function cleans up double colon scope operators based on the
|
|
2199 value of `c-cleanup-list'."
|
|
2200 (interactive "P")
|
|
2201 (let* ((bod (c-point 'bod))
|
|
2202 (literal (c-in-literal bod))
|
|
2203 syntax newlines
|
|
2204 ;; shut this up
|
|
2205 (c-echo-syntactic-information-p nil))
|
|
2206 (if (or literal
|
|
2207 arg
|
|
2208 (not (looking-at "[ \t]*$")))
|
|
2209 (c-insert-special-chars arg)
|
|
2210 ;; insert the colon, then do any specified cleanups
|
|
2211 (self-insert-command (prefix-numeric-value arg))
|
|
2212 (let ((pos (- (point-max) (point)))
|
|
2213 (here (point)))
|
|
2214 (if (and c-auto-newline
|
|
2215 (memq 'scope-operator c-cleanup-list)
|
|
2216 (= (preceding-char) ?:)
|
|
2217 (progn
|
|
2218 (forward-char -1)
|
|
2219 (skip-chars-backward " \t\n")
|
|
2220 (= (preceding-char) ?:))
|
|
2221 (not (c-in-literal))
|
|
2222 (not (= (char-after (- (point) 2)) ?:)))
|
|
2223 (delete-region (point) (1- here)))
|
|
2224 (goto-char (- (point-max) pos)))
|
|
2225 ;; lets do some special stuff with the colon character
|
|
2226 (setq syntax (c-guess-basic-syntax)
|
|
2227 ;; some language elements can only be determined by
|
|
2228 ;; checking the following line. Lets first look for ones
|
|
2229 ;; that can be found when looking on the line with the
|
|
2230 ;; colon
|
|
2231 newlines
|
|
2232 (and c-auto-newline
|
|
2233 (or (c-lookup-lists '(case-label label access-label)
|
|
2234 syntax c-hanging-colons-alist)
|
|
2235 (c-lookup-lists '(member-init-intro inher-intro)
|
|
2236 (prog2
|
|
2237 (insert "\n")
|
|
2238 (c-guess-basic-syntax)
|
|
2239 (delete-char -1))
|
|
2240 c-hanging-colons-alist))))
|
|
2241 ;; indent the current line
|
|
2242 (c-indent-line syntax)
|
|
2243 ;; does a newline go before the colon? Watch out for already
|
|
2244 ;; non-hung colons. However, we don't unhang them because that
|
|
2245 ;; would be a cleanup (and anti-social).
|
|
2246 (if (and (memq 'before newlines)
|
|
2247 (save-excursion
|
|
2248 (skip-chars-backward ": \t")
|
|
2249 (not (bolp))))
|
|
2250 (let ((pos (- (point-max) (point))))
|
|
2251 (forward-char -1)
|
|
2252 (newline)
|
|
2253 (c-indent-line)
|
|
2254 (goto-char (- (point-max) pos))))
|
|
2255 ;; does a newline go after the colon?
|
|
2256 (if (memq 'after (cdr-safe newlines))
|
|
2257 (progn
|
|
2258 (newline)
|
|
2259 (c-indent-line)))
|
|
2260 )))
|
|
2261
|
|
2262 (defun c-electric-lt-gt (arg)
|
|
2263 "Insert a less-than, or greater-than character.
|
|
2264 When the auto-newline feature is turned on, as evidenced by the \"/a\"
|
|
2265 or \"/ah\" string on the mode line, the line will be re-indented if
|
|
2266 the character inserted is the second of a C++ style stream operator
|
|
2267 and the buffer is in C++ mode.
|
|
2268
|
|
2269 The line will also not be re-indented if a numeric argument is
|
|
2270 supplied, or point is inside a literal."
|
|
2271 (interactive "P")
|
|
2272 (let ((indentp (and (not arg)
|
|
2273 (= (preceding-char) last-command-char)
|
|
2274 (not (c-in-literal))))
|
|
2275 ;; shut this up
|
|
2276 (c-echo-syntactic-information-p nil))
|
|
2277 (self-insert-command (prefix-numeric-value arg))
|
|
2278 (if indentp
|
|
2279 (c-indent-line))))
|
|
2280
|
|
2281 ;; set up electric character functions to work with pending-del,
|
|
2282 ;; (a.k.a. delsel) mode. All symbols get the t value except
|
|
2283 ;; c-electric-delete which gets 'supersede.
|
|
2284 (mapcar
|
|
2285 (function
|
|
2286 (lambda (sym)
|
|
2287 (put sym 'delete-selection t) ; for delsel (Emacs)
|
|
2288 (put sym 'pending-delete t))) ; for pending-del (XEmacs)
|
|
2289 '(c-electric-pound
|
|
2290 c-electric-brace
|
|
2291 c-electric-slash
|
|
2292 c-electric-star
|
|
2293 c-electric-semi&comma
|
|
2294 c-electric-lt-gt
|
|
2295 c-electric-colon))
|
|
2296 (put 'c-electric-delete 'delete-selection 'supersede) ; delsel
|
|
2297 (put 'c-electric-delete 'pending-delete 'supersede) ; pending-del
|
|
2298
|
|
2299
|
|
2300
|
32
|
2301 (defvar c-read-offset-history nil)
|
|
2302
|
0
|
2303 (defun c-read-offset (langelem)
|
|
2304 ;; read new offset value for LANGELEM from minibuffer. return a
|
|
2305 ;; legal value only
|
|
2306 (let* ((oldoff (cdr-safe (assq langelem c-offsets-alist)))
|
|
2307 (defstr (format "(default %s): " oldoff))
|
|
2308 (errmsg (concat "Offset must be int, func, var, "
|
|
2309 "or in [+,-,++,--,*,/] "
|
|
2310 defstr))
|
|
2311 (prompt (concat "Offset " defstr))
|
2
|
2312 offset input interned raw)
|
0
|
2313 (while (not offset)
|
32
|
2314 (setq input (completing-read prompt obarray 'fboundp nil nil
|
|
2315 'c-read-offset-history)
|
0
|
2316 offset (cond ((string-equal "" input) oldoff) ; default
|
|
2317 ((string-equal "+" input) '+)
|
|
2318 ((string-equal "-" input) '-)
|
|
2319 ((string-equal "++" input) '++)
|
|
2320 ((string-equal "--" input) '--)
|
|
2321 ((string-equal "*" input) '*)
|
|
2322 ((string-equal "/" input) '/)
|
|
2323 ((string-match "^-?[0-9]+$" input)
|
|
2324 (string-to-int input))
|
2
|
2325 ;; a symbol with a function binding
|
0
|
2326 ((fboundp (setq interned (intern input)))
|
|
2327 interned)
|
2
|
2328 ;; a lambda function
|
|
2329 ((condition-case nil
|
|
2330 (c-functionp (setq raw (read input)))
|
|
2331 (error nil))
|
|
2332 raw)
|
|
2333 ;; a symbol with variable binding
|
0
|
2334 ((boundp interned) interned)
|
|
2335 ;; error, but don't signal one, keep trying
|
|
2336 ;; to read an input value
|
|
2337 (t (ding)
|
|
2338 (setq prompt errmsg)
|
|
2339 nil))))
|
|
2340 offset))
|
|
2341
|
|
2342 (defun c-set-offset (symbol offset &optional add-p)
|
|
2343 "Change the value of a syntactic element symbol in `c-offsets-alist'.
|
|
2344 SYMBOL is the syntactic element symbol to change and OFFSET is the new
|
|
2345 offset for that syntactic element. Optional ADD says to add SYMBOL to
|
|
2346 `c-offsets-alist' if it doesn't already appear there."
|
|
2347 (interactive
|
|
2348 (let* ((langelem
|
|
2349 (intern (completing-read
|
|
2350 (concat "Syntactic symbol to change"
|
|
2351 (if current-prefix-arg " or add" "")
|
|
2352 ": ")
|
|
2353 (mapcar
|
|
2354 (function
|
|
2355 (lambda (langelem)
|
|
2356 (cons (format "%s" (car langelem)) nil)))
|
|
2357 c-offsets-alist)
|
|
2358 nil (not current-prefix-arg)
|
|
2359 ;; initial contents tries to be the last element
|
|
2360 ;; on the syntactic analysis list for the current
|
|
2361 ;; line
|
|
2362 (let* ((syntax (c-guess-basic-syntax))
|
|
2363 (len (length syntax))
|
|
2364 (ic (format "%s" (car (nth (1- len) syntax)))))
|
2
|
2365 (if (or (memq 'v19 c-emacs-features)
|
|
2366 (memq 'v20 c-emacs-features))
|
0
|
2367 (cons ic 0)
|
|
2368 ic))
|
|
2369 )))
|
|
2370 (offset (c-read-offset langelem)))
|
|
2371 (list langelem offset current-prefix-arg)))
|
|
2372 ;; sanity check offset
|
|
2373 (or (eq offset '+)
|
|
2374 (eq offset '-)
|
|
2375 (eq offset '++)
|
|
2376 (eq offset '--)
|
|
2377 (eq offset '*)
|
|
2378 (eq offset '/)
|
|
2379 (integerp offset)
|
2
|
2380 (c-functionp offset)
|
0
|
2381 (boundp offset)
|
|
2382 (error "Offset must be int, func, var, or in [+,-,++,--,*,/]: %s"
|
|
2383 offset))
|
|
2384 (let ((entry (assq symbol c-offsets-alist)))
|
|
2385 (if entry
|
|
2386 (setcdr entry offset)
|
|
2387 (if add-p
|
|
2388 (setq c-offsets-alist (cons (cons symbol offset) c-offsets-alist))
|
|
2389 (error "%s is not a valid syntactic symbol." symbol))))
|
|
2390 (c-keep-region-active))
|
|
2391
|
|
2392 (defun c-set-style-1 (stylevars)
|
|
2393 ;; given a style's variable alist, institute the style
|
|
2394 (mapcar
|
|
2395 (function
|
|
2396 (lambda (conscell)
|
|
2397 (let ((attr (car conscell))
|
|
2398 (val (cdr conscell)))
|
2
|
2399 (cond
|
|
2400 ((eq attr 'c-offsets-alist)
|
0
|
2401 (mapcar
|
|
2402 (function
|
|
2403 (lambda (langentry)
|
|
2404 (let ((langelem (car langentry))
|
|
2405 (offset (cdr langentry)))
|
|
2406 (c-set-offset langelem offset)
|
|
2407 )))
|
|
2408 val))
|
2
|
2409 ((eq attr 'c-special-indent-hook)
|
|
2410 (if (listp val)
|
|
2411 (while val
|
|
2412 (add-hook 'c-special-indent-hook (car val))
|
|
2413 (setq val (cdr val)))
|
|
2414 (add-hook 'c-special-indent-hook val)))
|
|
2415 (t (set attr val)))
|
0
|
2416 )))
|
|
2417 stylevars))
|
|
2418
|
32
|
2419 (defvar c-set-style-history nil)
|
|
2420
|
6
|
2421 ;;;###autoload
|
0
|
2422 (defun c-set-style (stylename)
|
32
|
2423 "Set CC Mode variables to use one of several different indentation styles.
|
0
|
2424 STYLENAME is a string representing the desired style from the list of
|
|
2425 styles described in the variable `c-style-alist'. See that variable
|
32
|
2426 for details of setting up styles.
|
|
2427
|
|
2428 The variable `c-indentation-style' always contains the buffer's current
|
|
2429 style name."
|
0
|
2430 (interactive (list (let ((completion-ignore-case t)
|
|
2431 (prompt (format "Which %s indentation style? "
|
|
2432 mode-name)))
|
32
|
2433 (completing-read prompt c-style-alist nil t
|
|
2434 (cons c-indentation-style 0)
|
|
2435 'c-set-style-history))))
|
0
|
2436 (let ((vars (cdr (or (assoc (downcase stylename) c-style-alist)
|
|
2437 (assoc (upcase stylename) c-style-alist)
|
2
|
2438 (assoc stylename c-style-alist)
|
0
|
2439 )))
|
|
2440 (default (cdr (assoc "cc-mode" c-style-alist))))
|
|
2441 (or vars (error "Invalid indentation style `%s'" stylename))
|
|
2442 (or default (error "No `cc-mode' style found!"))
|
|
2443 ;; first reset the style to `cc-mode' to give every style a common
|
|
2444 ;; base. Then institute the new style.
|
|
2445 (c-set-style-1 default)
|
32
|
2446 (setq c-indentation-style stylename)
|
0
|
2447 (if (not (string= stylename "cc-mode"))
|
|
2448 (c-set-style-1 vars)))
|
|
2449 (c-keep-region-active))
|
|
2450
|
|
2451 (defun c-add-style (style descrip &optional set-p)
|
|
2452 "Adds a style to `c-style-alist', or updates an existing one.
|
|
2453 STYLE is a string identifying the style to add or update. DESCRIP is
|
|
2454 an association list describing the style and must be of the form:
|
|
2455
|
|
2456 ((VARIABLE . VALUE) [(VARIABLE . VALUE) ...])
|
|
2457
|
|
2458 See the variable `c-style-alist' for the semantics of VARIABLE and
|
|
2459 VALUE. This function also sets the current style to STYLE using
|
|
2460 `c-set-style' if the optional SET-P flag is non-nil."
|
|
2461 (interactive
|
|
2462 (let ((stylename (completing-read "Style to add: " c-style-alist))
|
|
2463 (description (eval-minibuffer "Style description: ")))
|
|
2464 (list stylename description
|
|
2465 (y-or-n-p "Set the style too? "))))
|
|
2466 (setq style (downcase style))
|
|
2467 (let ((s (assoc style c-style-alist)))
|
|
2468 (if s
|
|
2469 (setcdr s (copy-alist descrip)) ; replace
|
|
2470 (setq c-style-alist (cons (cons style descrip) c-style-alist))))
|
|
2471 (and set-p (c-set-style style)))
|
|
2472
|
4
|
2473
|
0
|
2474 (defun c-fill-paragraph (&optional arg)
|
|
2475 "Like \\[fill-paragraph] but handles C and C++ style comments.
|
|
2476 If any of the current line is a comment or within a comment,
|
|
2477 fill the comment or the paragraph of it that point is in,
|
|
2478 preserving the comment indentation or line-starting decorations.
|
|
2479
|
|
2480 Optional prefix ARG means justify paragraph as well."
|
|
2481 (interactive "P")
|
|
2482 (let* (comment-start-place
|
|
2483 (first-line
|
|
2484 ;; Check for obvious entry to comment.
|
|
2485 (save-excursion
|
|
2486 (beginning-of-line)
|
|
2487 (skip-chars-forward " \t\n")
|
|
2488 (and (looking-at comment-start-skip)
|
|
2489 (setq comment-start-place (point)))))
|
|
2490 (re1 (if (memq 'new-re c-emacs-features)
|
|
2491 "\\|[ \t]*/\\*[ \t]*$\\|[ \t]*\\*/[ \t]*$\\|[ \t/*]*$"
|
|
2492 "\\|^[ \t]*/\\*[ \t]*$\\|^[ \t]*\\*/[ \t]*$\\|^[ \t/*]*$"))
|
|
2493 )
|
|
2494 (if (and c-double-slash-is-comments-p
|
|
2495 (save-excursion
|
|
2496 (beginning-of-line)
|
|
2497 (looking-at ".*//")))
|
|
2498 (let (fill-prefix
|
|
2499 ;; Lines containing just a comment start or just an end
|
|
2500 ;; should not be filled into paragraphs they are next
|
|
2501 ;; to.
|
|
2502 (paragraph-start (concat paragraph-start re1))
|
|
2503 (paragraph-separate (concat paragraph-separate re1)))
|
|
2504 (save-excursion
|
|
2505 (beginning-of-line)
|
|
2506 ;; Move up to first line of this comment.
|
|
2507 (while (and (not (bobp))
|
2
|
2508 (looking-at "[ \t]*//[ \t]*[^ \t\n]"))
|
0
|
2509 (forward-line -1))
|
2
|
2510 (if (not (looking-at ".*//[ \t]*[^ \t\n]"))
|
0
|
2511 (forward-line 1))
|
|
2512 ;; Find the comment start in this line.
|
|
2513 (re-search-forward "[ \t]*//[ \t]*")
|
|
2514 ;; Set the fill-prefix to be what all lines except the first
|
|
2515 ;; should start with.
|
|
2516 (setq fill-prefix (buffer-substring (match-beginning 0)
|
|
2517 (match-end 0)))
|
|
2518 (save-restriction
|
|
2519 ;; Narrow down to just the lines of this comment.
|
|
2520 (narrow-to-region (c-point 'bol)
|
|
2521 (save-excursion
|
|
2522 (forward-line 1)
|
|
2523 (while (looking-at fill-prefix)
|
|
2524 (forward-line 1))
|
|
2525 (point)))
|
|
2526 (fill-paragraph arg)
|
|
2527 t)))
|
|
2528 ;; else C style comments
|
|
2529 (if (or first-line
|
|
2530 ;; t if we enter a comment between start of function and
|
|
2531 ;; this line.
|
|
2532 (eq (c-in-literal) 'c)
|
|
2533 ;; t if this line contains a comment starter.
|
|
2534 (setq first-line
|
|
2535 (save-excursion
|
|
2536 (beginning-of-line)
|
|
2537 (prog1
|
|
2538 (re-search-forward comment-start-skip
|
|
2539 (save-excursion (end-of-line)
|
|
2540 (point))
|
|
2541 t)
|
|
2542 (setq comment-start-place (point))))))
|
|
2543 ;; Inside a comment: fill one comment paragraph.
|
|
2544 (let ((fill-prefix
|
|
2545 ;; The prefix for each line of this paragraph
|
|
2546 ;; is the appropriate part of the start of this line,
|
|
2547 ;; up to the column at which text should be indented.
|
|
2548 (save-excursion
|
|
2549 (beginning-of-line)
|
|
2550 (if (looking-at "[ \t]*/\\*.*\\*/")
|
|
2551 (progn (re-search-forward comment-start-skip)
|
|
2552 (make-string (current-column) ?\ ))
|
|
2553 (if first-line (forward-line 1))
|
|
2554
|
|
2555 (let ((line-width (progn (end-of-line) (current-column))))
|
|
2556 (beginning-of-line)
|
|
2557 (prog1
|
|
2558 (buffer-substring
|
|
2559 (point)
|
|
2560
|
|
2561 ;; How shall we decide where the end of the
|
|
2562 ;; fill-prefix is?
|
|
2563 (progn
|
|
2564 (beginning-of-line)
|
|
2565 (skip-chars-forward " \t*" (c-point 'eol))
|
36
|
2566 ;; kludge alert, watch out for */, in
|
|
2567 ;; which case fill-prefix should *not*
|
|
2568 ;; be "*"!
|
|
2569 (if (and (= (following-char) ?/)
|
|
2570 (= (preceding-char) ?*))
|
|
2571 (forward-char -1))
|
0
|
2572 (point)))
|
|
2573
|
|
2574 ;; If the comment is only one line followed
|
|
2575 ;; by a blank line, calling move-to-column
|
|
2576 ;; above may have added some spaces and tabs
|
|
2577 ;; to the end of the line; the fill-paragraph
|
|
2578 ;; function will then delete it and the
|
|
2579 ;; newline following it, so we'll lose a
|
|
2580 ;; blank line when we shouldn't. So delete
|
|
2581 ;; anything move-to-column added to the end
|
|
2582 ;; of the line. We record the line width
|
|
2583 ;; instead of the position of the old line
|
|
2584 ;; end because move-to-column might break a
|
|
2585 ;; tab into spaces, and the new characters
|
|
2586 ;; introduced there shouldn't be deleted.
|
|
2587
|
|
2588 ;; If you can see a better way to do this,
|
|
2589 ;; please make the change. This seems very
|
|
2590 ;; messy to me.
|
|
2591 (delete-region (progn (move-to-column line-width)
|
|
2592 (point))
|
|
2593 (progn (end-of-line) (point))))))))
|
|
2594
|
|
2595 ;; Lines containing just a comment start or just an end
|
|
2596 ;; should not be filled into paragraphs they are next
|
|
2597 ;; to.
|
|
2598 (paragraph-start (concat paragraph-start re1))
|
|
2599 (paragraph-separate (concat paragraph-separate re1))
|
32
|
2600 (chars-to-delete 0)
|
|
2601 )
|
0
|
2602 (save-restriction
|
|
2603 ;; Don't fill the comment together with the code
|
|
2604 ;; following it. So temporarily exclude everything
|
|
2605 ;; before the comment start, and everything after the
|
|
2606 ;; line where the comment ends. If comment-start-place
|
|
2607 ;; is non-nil, the comment starter is there. Otherwise,
|
|
2608 ;; point is inside the comment.
|
|
2609 (narrow-to-region (save-excursion
|
|
2610 (if comment-start-place
|
|
2611 (goto-char comment-start-place)
|
|
2612 (search-backward "/*"))
|
32
|
2613 (if (and (not c-hanging-comment-starter-p)
|
40
|
2614 (looking-at
|
|
2615 (concat c-comment-start-regexp
|
|
2616 "[ \t]*$")))
|
32
|
2617 (forward-line 1))
|
0
|
2618 ;; Protect text before the comment
|
|
2619 ;; start by excluding it. Add
|
|
2620 ;; spaces to bring back proper
|
|
2621 ;; indentation of that point.
|
|
2622 (let ((column (current-column)))
|
|
2623 (prog1 (point)
|
|
2624 (setq chars-to-delete column)
|
|
2625 (insert-char ?\ column))))
|
|
2626 (save-excursion
|
|
2627 (if comment-start-place
|
|
2628 (goto-char (+ comment-start-place 2)))
|
|
2629 (search-forward "*/" nil 'move)
|
|
2630 (forward-line 1)
|
|
2631 (point)))
|
|
2632 (fill-paragraph arg)
|
|
2633 (save-excursion
|
|
2634 ;; Delete the chars we inserted to avoid clobbering
|
|
2635 ;; the stuff before the comment start.
|
|
2636 (goto-char (point-min))
|
|
2637 (if (> chars-to-delete 0)
|
|
2638 (delete-region (point) (+ (point) chars-to-delete)))
|
|
2639 ;; Find the comment ender (should be on last line of
|
|
2640 ;; buffer, given the narrowing) and don't leave it on
|
|
2641 ;; its own line, unless that's the style that's desired.
|
|
2642 (goto-char (point-max))
|
|
2643 (forward-line -1)
|
|
2644 (search-forward "*/" nil 'move)
|
|
2645 (beginning-of-line)
|
|
2646 (if (and c-hanging-comment-ender-p
|
|
2647 (looking-at "[ \t]*\\*/"))
|
|
2648 ;(delete-indentation)))))
|
|
2649 (let ((fill-column (+ fill-column 9999)))
|
|
2650 (forward-line -1)
|
|
2651 (fill-region-as-paragraph (point) (point-max))))))
|
|
2652 t)))))
|
|
2653
|
2
|
2654
|
0
|
2655 ;; better movement routines for ThisStyleOfVariablesCommonInCPlusPlus
|
|
2656 ;; originally contributed by Terry_Glanfield.Southern@rxuk.xerox.com
|
|
2657 (defun c-forward-into-nomenclature (&optional arg)
|
|
2658 "Move forward to end of a nomenclature section or word.
|
|
2659 With arg, to it arg times."
|
|
2660 (interactive "p")
|
|
2661 (let ((case-fold-search nil))
|
|
2662 (if (> arg 0)
|
|
2663 (re-search-forward "\\W*\\([A-Z]*[a-z0-9]*\\)" (point-max) t arg)
|
|
2664 (while (and (< arg 0)
|
|
2665 (re-search-backward
|
|
2666 "\\(\\(\\W\\|[a-z0-9]\\)[A-Z]+\\|\\W\\w+\\)"
|
|
2667 (point-min) 0))
|
|
2668 (forward-char 1)
|
|
2669 (setq arg (1+ arg)))))
|
|
2670 (c-keep-region-active))
|
|
2671
|
|
2672 (defun c-backward-into-nomenclature (&optional arg)
|
|
2673 "Move backward to beginning of a nomenclature section or word.
|
|
2674 With optional ARG, move that many times. If ARG is negative, move
|
|
2675 forward."
|
|
2676 (interactive "p")
|
|
2677 (c-forward-into-nomenclature (- arg))
|
|
2678 (c-keep-region-active))
|
|
2679
|
|
2680 (defun c-scope-operator ()
|
|
2681 "Insert a double colon scope operator at point.
|
|
2682 No indentation or other \"electric\" behavior is performed."
|
|
2683 (interactive)
|
|
2684 (insert "::"))
|
|
2685
|
|
2686
|
|
2687 (defun c-beginning-of-statement (&optional count lim sentence-flag)
|
|
2688 "Go to the beginning of the innermost C statement.
|
|
2689 With prefix arg, go back N - 1 statements. If already at the
|
|
2690 beginning of a statement then go to the beginning of the preceding
|
|
2691 one. If within a string or comment, or next to a comment (only
|
|
2692 whitespace between), move by sentences instead of statements.
|
|
2693
|
|
2694 When called from a program, this function takes 3 optional args: the
|
|
2695 repetition count, a buffer position limit which is the farthest back
|
|
2696 to search, and a flag saying whether to do sentence motion when in a
|
|
2697 comment."
|
|
2698 (interactive (list (prefix-numeric-value current-prefix-arg)
|
|
2699 nil t))
|
|
2700 (let ((here (point))
|
|
2701 (count (or count 1))
|
|
2702 (lim (or lim (c-point 'bod)))
|
|
2703 state)
|
|
2704 (save-excursion
|
|
2705 (goto-char lim)
|
|
2706 (setq state (parse-partial-sexp (point) here nil nil)))
|
|
2707 (if (and sentence-flag
|
|
2708 (or (nth 3 state)
|
|
2709 (nth 4 state)
|
32
|
2710 ; (looking-at (concat "[ \t]*" comment-start-skip))
|
0
|
2711 (save-excursion
|
|
2712 (skip-chars-backward " \t")
|
|
2713 (goto-char (- (point) 2))
|
|
2714 (looking-at "\\*/"))))
|
|
2715 (forward-sentence (- count))
|
|
2716 (while (> count 0)
|
|
2717 (c-beginning-of-statement-1 lim)
|
|
2718 (setq count (1- count)))
|
|
2719 (while (< count 0)
|
|
2720 (c-end-of-statement-1)
|
|
2721 (setq count (1+ count))))
|
|
2722 ;; its possible we've been left up-buf of lim
|
|
2723 (goto-char (max (point) lim))
|
|
2724 )
|
|
2725 (c-keep-region-active))
|
|
2726
|
|
2727 (defun c-end-of-statement (&optional count lim sentence-flag)
|
|
2728 "Go to the end of the innermost C statement.
|
|
2729
|
|
2730 With prefix arg, go forward N - 1 statements. Move forward to end of
|
|
2731 the next statement if already at end. If within a string or comment,
|
|
2732 move by sentences instead of statements.
|
|
2733
|
|
2734 When called from a program, this function takes 3 optional args: the
|
|
2735 repetition count, a buffer position limit which is the farthest back
|
|
2736 to search, and a flag saying whether to do sentence motion when in a
|
|
2737 comment."
|
|
2738 (interactive (list (prefix-numeric-value current-prefix-arg)
|
|
2739 nil t))
|
|
2740 (c-beginning-of-statement (- (or count 1)) lim sentence-flag)
|
|
2741 (c-keep-region-active))
|
|
2742
|
2
|
2743 ;; WARNING: Be *exceptionally* careful about modifications to this
|
32
|
2744 ;; function! Much of CC Mode depends on this Doing The Right Thing.
|
2
|
2745 ;; If you break it you will be sorry.
|
0
|
2746 (defun c-beginning-of-statement-1 (&optional lim)
|
|
2747 ;; move to the start of the current statement, or the previous
|
|
2748 ;; statement if already at the beginning of one.
|
|
2749 (let ((firstp t)
|
|
2750 (substmt-p t)
|
|
2751 donep c-in-literal-cache
|
|
2752 ;; KLUDGE ALERT: maybe-labelp is used to pass information
|
|
2753 ;; between c-crosses-statement-barrier-p and
|
|
2754 ;; c-beginning-of-statement-1. A better way should be
|
|
2755 ;; implemented.
|
32
|
2756 maybe-labelp saved
|
0
|
2757 (last-begin (point)))
|
32
|
2758 ;; first check for bare semicolon
|
|
2759 (if (and (progn (c-backward-syntactic-ws lim)
|
|
2760 (= (preceding-char) ?\;))
|
|
2761 (c-safe (progn (forward-char -1)
|
|
2762 (setq saved (point))
|
|
2763 t))
|
|
2764 (progn (c-backward-syntactic-ws lim)
|
|
2765 (memq (preceding-char) '(?\; ?{ ?} ?:)))
|
|
2766 )
|
|
2767 (setq last-begin saved)
|
|
2768 (goto-char last-begin)
|
|
2769 (while (not donep)
|
|
2770 ;; stop at beginning of buffer
|
|
2771 (if (bobp) (setq donep t)
|
|
2772 ;; go backwards one balanced expression, but be careful of
|
|
2773 ;; unbalanced paren being reached
|
|
2774 (if (not (c-safe (progn (backward-sexp 1) t)))
|
0
|
2775 (progn
|
32
|
2776 (if firstp
|
|
2777 (backward-up-list 1)
|
|
2778 (goto-char last-begin))
|
|
2779 ;; skip over any unary operators, or other special
|
|
2780 ;; characters appearing at front of identifier
|
|
2781 (save-excursion
|
|
2782 (c-backward-syntactic-ws lim)
|
|
2783 (skip-chars-backward "-+!*&:.~ \t\n")
|
|
2784 (if (= (preceding-char) ?\()
|
|
2785 (setq last-begin (point))))
|
|
2786 (goto-char last-begin)
|
|
2787 (setq last-begin (point)
|
|
2788 donep t)))
|
|
2789
|
|
2790 (setq maybe-labelp nil)
|
|
2791 ;; see if we're in a literal. if not, then this bufpos may be
|
|
2792 ;; a candidate for stopping
|
|
2793 (cond
|
|
2794 ;; CASE 0: did we hit the error condition above?
|
|
2795 (donep)
|
|
2796 ;; CASE 1: are we in a literal?
|
|
2797 ((eq (c-in-literal lim) 'pound)
|
|
2798 (beginning-of-line))
|
|
2799 ;; CASE 2: some other kind of literal?
|
|
2800 ((c-in-literal lim))
|
|
2801 ;; CASE 3: are we looking at a conditional keyword?
|
|
2802 ((or (looking-at c-conditional-key)
|
|
2803 (and (= (following-char) ?\()
|
|
2804 (save-excursion
|
|
2805 (forward-sexp 1)
|
|
2806 (c-forward-syntactic-ws)
|
|
2807 (/= (following-char) ?\;))
|
|
2808 (let ((here (point))
|
|
2809 (foundp (progn
|
|
2810 (c-backward-syntactic-ws lim)
|
|
2811 (forward-word -1)
|
|
2812 (and lim
|
|
2813 (<= lim (point))
|
|
2814 (not (c-in-literal lim))
|
|
2815 (looking-at c-conditional-key)
|
|
2816 ))))
|
|
2817 ;; did we find a conditional?
|
|
2818 (if (not foundp)
|
|
2819 (goto-char here))
|
|
2820 foundp)))
|
|
2821 ;; are we in the middle of an else-if clause?
|
|
2822 (if (save-excursion
|
|
2823 (and (not substmt-p)
|
|
2824 (c-safe (progn (forward-sexp -1) t))
|
|
2825 (looking-at "\\<else\\>[ \t\n]+\\<if\\>")
|
|
2826 (not (c-in-literal lim))))
|
|
2827 (progn
|
|
2828 (forward-sexp -1)
|
|
2829 (c-backward-to-start-of-if lim)))
|
|
2830 ;; are we sitting at an else clause, that we are not a
|
|
2831 ;; substatement of?
|
|
2832 (if (and (not substmt-p)
|
|
2833 (looking-at "\\<else\\>[^_]"))
|
|
2834 (c-backward-to-start-of-if lim))
|
|
2835 ;; are we sitting at the while of a do-while?
|
|
2836 (if (and (looking-at "\\<while\\>[^_]")
|
|
2837 (c-backward-to-start-of-do lim))
|
|
2838 (setq substmt-p nil))
|
|
2839 (setq last-begin (point)
|
|
2840 donep substmt-p))
|
|
2841 ;; CASE 4: are we looking at a label?
|
|
2842 ((looking-at c-label-key))
|
|
2843 ;; CASE 5: is this the first time we're checking?
|
|
2844 (firstp (setq firstp nil
|
|
2845 substmt-p (not (c-crosses-statement-barrier-p
|
|
2846 (point) last-begin))
|
|
2847 last-begin (point)))
|
|
2848 ;; CASE 6: have we crossed a statement barrier?
|
|
2849 ((c-crosses-statement-barrier-p (point) last-begin)
|
|
2850 (setq donep t))
|
|
2851 ;; CASE 7: ignore labels
|
|
2852 ((and maybe-labelp
|
|
2853 (or (and c-access-key (looking-at c-access-key))
|
|
2854 ;; with switch labels, we have to go back further
|
|
2855 ;; to try to pick up the case or default
|
|
2856 ;; keyword. Potential bogosity alert: we assume
|
|
2857 ;; `case' or `default' is first thing on line
|
|
2858 (let ((here (point)))
|
|
2859 (beginning-of-line)
|
|
2860 (c-forward-syntactic-ws)
|
|
2861 (if (looking-at c-switch-label-key)
|
|
2862 t
|
|
2863 (goto-char here)
|
|
2864 nil))
|
|
2865 (looking-at c-label-key))))
|
|
2866 ;; CASE 8: ObjC or Java method def
|
|
2867 ((and c-method-key
|
|
2868 (setq last-begin (c-in-method-def-p)))
|
|
2869 (setq donep t))
|
|
2870 ;; CASE 9: nothing special
|
|
2871 (t (setq last-begin (point)))
|
|
2872 ))))
|
0
|
2873 (goto-char last-begin)
|
|
2874 ;; we always do want to skip over non-whitespace modifier
|
|
2875 ;; characters that didn't get skipped above
|
|
2876 (skip-chars-backward "-+!*&:.~" (c-point 'boi))))
|
|
2877
|
|
2878 (defun c-end-of-statement-1 ()
|
|
2879 (condition-case ()
|
|
2880 (progn
|
|
2881 (while (and (not (eobp))
|
|
2882 (let ((beg (point)))
|
|
2883 (forward-sexp 1)
|
|
2884 (let ((end (point)))
|
|
2885 (save-excursion
|
|
2886 (goto-char beg)
|
|
2887 (not (re-search-forward "[;{}]" end t)))))))
|
|
2888 (re-search-backward "[;}]")
|
|
2889 (forward-char 1))
|
|
2890 (error
|
|
2891 (let ((beg (point)))
|
|
2892 (backward-up-list -1)
|
|
2893 (let ((end (point)))
|
|
2894 (goto-char beg)
|
|
2895 (search-forward ";" end 'move))))))
|
|
2896
|
|
2897 (defun c-crosses-statement-barrier-p (from to)
|
|
2898 ;; Does buffer positions FROM to TO cross a C statement boundary?
|
|
2899 (let ((here (point))
|
|
2900 (lim from)
|
|
2901 crossedp)
|
|
2902 (condition-case ()
|
|
2903 (progn
|
|
2904 (goto-char from)
|
|
2905 (while (and (not crossedp)
|
|
2906 (< (point) to))
|
|
2907 (skip-chars-forward "^;{}:" to)
|
|
2908 (if (not (c-in-literal lim))
|
|
2909 (progn
|
|
2910 (if (memq (following-char) '(?\; ?{ ?}))
|
|
2911 (setq crossedp t)
|
|
2912 (if (= (following-char) ?:)
|
|
2913 (setq maybe-labelp t))
|
|
2914 (forward-char 1))
|
|
2915 (setq lim (point)))
|
|
2916 (forward-char 1))))
|
|
2917 (error (setq crossedp nil)))
|
|
2918 (goto-char here)
|
|
2919 crossedp))
|
|
2920
|
|
2921
|
|
2922 (defun c-up-conditional (count)
|
|
2923 "Move back to the containing preprocessor conditional, leaving mark behind.
|
|
2924 A prefix argument acts as a repeat count. With a negative argument,
|
|
2925 move forward to the end of the containing preprocessor conditional.
|
|
2926 When going backwards, `#elif' is treated like `#else' followed by
|
|
2927 `#if'. When going forwards, `#elif' is ignored."
|
|
2928 (interactive "p")
|
|
2929 (c-forward-conditional (- count) t)
|
|
2930 (c-keep-region-active))
|
|
2931
|
|
2932 (defun c-backward-conditional (count &optional up-flag)
|
|
2933 "Move back across a preprocessor conditional, leaving mark behind.
|
|
2934 A prefix argument acts as a repeat count. With a negative argument,
|
|
2935 move forward across a preprocessor conditional."
|
|
2936 (interactive "p")
|
|
2937 (c-forward-conditional (- count) up-flag)
|
|
2938 (c-keep-region-active))
|
|
2939
|
|
2940 (defun c-forward-conditional (count &optional up-flag)
|
|
2941 "Move forward across a preprocessor conditional, leaving mark behind.
|
|
2942 A prefix argument acts as a repeat count. With a negative argument,
|
|
2943 move backward across a preprocessor conditional."
|
|
2944 (interactive "p")
|
|
2945 (let* ((forward (> count 0))
|
|
2946 (increment (if forward -1 1))
|
|
2947 (search-function (if forward 're-search-forward 're-search-backward))
|
|
2948 (new))
|
|
2949 (save-excursion
|
|
2950 (while (/= count 0)
|
|
2951 (let ((depth (if up-flag 0 -1)) found)
|
|
2952 (save-excursion
|
|
2953 ;; Find the "next" significant line in the proper direction.
|
|
2954 (while (and (not found)
|
|
2955 ;; Rather than searching for a # sign that
|
|
2956 ;; comes at the beginning of a line aside from
|
|
2957 ;; whitespace, search first for a string
|
|
2958 ;; starting with # sign. Then verify what
|
|
2959 ;; precedes it. This is faster on account of
|
|
2960 ;; the fastmap feature of the regexp matcher.
|
|
2961 (funcall search-function
|
|
2962 "#[ \t]*\\(if\\|elif\\|endif\\)"
|
|
2963 nil t))
|
|
2964 (beginning-of-line)
|
|
2965 ;; Now verify it is really a preproc line.
|
|
2966 (if (looking-at "^[ \t]*#[ \t]*\\(if\\|elif\\|endif\\)")
|
|
2967 (let ((prev depth))
|
|
2968 ;; Update depth according to what we found.
|
|
2969 (beginning-of-line)
|
|
2970 (cond ((looking-at "[ \t]*#[ \t]*endif")
|
|
2971 (setq depth (+ depth increment)))
|
|
2972 ((looking-at "[ \t]*#[ \t]*elif")
|
|
2973 (if (and forward (= depth 0))
|
|
2974 (setq found (point))))
|
|
2975 (t (setq depth (- depth increment))))
|
|
2976 ;; If we are trying to move across, and we find an
|
|
2977 ;; end before we find a beginning, get an error.
|
|
2978 (if (and (< prev 0) (< depth prev))
|
|
2979 (error (if forward
|
|
2980 "No following conditional at this level"
|
|
2981 "No previous conditional at this level")))
|
|
2982 ;; When searching forward, start from next line so
|
|
2983 ;; that we don't find the same line again.
|
|
2984 (if forward (forward-line 1))
|
|
2985 ;; If this line exits a level of conditional, exit
|
|
2986 ;; inner loop.
|
|
2987 (if (< depth 0)
|
|
2988 (setq found (point))))
|
|
2989 ;; else
|
|
2990 (if forward (forward-line 1))
|
|
2991 )))
|
|
2992 (or found
|
|
2993 (error "No containing preprocessor conditional"))
|
|
2994 (goto-char (setq new found)))
|
|
2995 (setq count (+ count increment))))
|
|
2996 (push-mark)
|
|
2997 (goto-char new))
|
|
2998 (c-keep-region-active))
|
|
2999
|
|
3000
|
|
3001 ;; commands to indent lines, regions, defuns, and expressions
|
|
3002 (defun c-indent-command (&optional whole-exp)
|
4
|
3003 "Indent current line as C code, and/or insert some whitespace.
|
0
|
3004
|
|
3005 If `c-tab-always-indent' is t, always just indent the current line.
|
|
3006 If nil, indent the current line only if point is at the left margin or
|
4
|
3007 in the line's indentation; otherwise insert some whitespace[*]. If
|
|
3008 other than nil or t, then some whitespace[*] is inserted only within
|
|
3009 literals (comments and strings) and inside preprocessor directives,
|
|
3010 but the line is always reindented.
|
0
|
3011
|
|
3012 A numeric argument, regardless of its value, means indent rigidly all
|
|
3013 the lines of the expression starting after point so that this line
|
|
3014 becomes properly indented. The relative indentation among the lines
|
4
|
3015 of the expression are preserved.
|
|
3016
|
|
3017 [*] The amount and kind of whitespace inserted is controlled by the
|
|
3018 variable `c-insert-tab-function', which is called to do the actual
|
|
3019 insertion of whitespace. Normally the function in this variable
|
|
3020 just inserts a tab character, or the equivalent number of spaces,
|
|
3021 depending on the variable `indent-tabs-mode'."
|
|
3022
|
0
|
3023 (interactive "P")
|
|
3024 (let ((bod (c-point 'bod)))
|
|
3025 (if whole-exp
|
|
3026 ;; If arg, always indent this line as C
|
|
3027 ;; and shift remaining lines of expression the same amount.
|
|
3028 (let ((shift-amt (c-indent-line))
|
|
3029 beg end)
|
|
3030 (save-excursion
|
|
3031 (if (eq c-tab-always-indent t)
|
|
3032 (beginning-of-line))
|
|
3033 (setq beg (point))
|
|
3034 (forward-sexp 1)
|
|
3035 (setq end (point))
|
|
3036 (goto-char beg)
|
|
3037 (forward-line 1)
|
|
3038 (setq beg (point)))
|
|
3039 (if (> end beg)
|
|
3040 (indent-code-rigidly beg end (- shift-amt) "#")))
|
|
3041 ;; No arg supplied, use c-tab-always-indent to determine
|
|
3042 ;; behavior
|
|
3043 (cond
|
|
3044 ;; CASE 1: indent when at column zero or in lines indentation,
|
|
3045 ;; otherwise insert a tab
|
|
3046 ((not c-tab-always-indent)
|
|
3047 (if (save-excursion
|
|
3048 (skip-chars-backward " \t")
|
|
3049 (not (bolp)))
|
2
|
3050 (funcall c-insert-tab-function)
|
0
|
3051 (c-indent-line)))
|
|
3052 ;; CASE 2: just indent the line
|
|
3053 ((eq c-tab-always-indent t)
|
|
3054 (c-indent-line))
|
|
3055 ;; CASE 3: if in a literal, insert a tab, but always indent the
|
|
3056 ;; line
|
|
3057 (t
|
|
3058 (if (c-in-literal bod)
|
2
|
3059 (funcall c-insert-tab-function))
|
0
|
3060 (c-indent-line)
|
|
3061 )))))
|
|
3062
|
|
3063 (defun c-indent-exp (&optional shutup-p)
|
|
3064 "Indent each line in balanced expression following point.
|
|
3065 Optional SHUTUP-P if non-nil, inhibits message printing and error checking."
|
|
3066 (interactive "P")
|
|
3067 (let ((here (point))
|
|
3068 end progress-p)
|
|
3069 (unwind-protect
|
|
3070 (let ((c-echo-syntactic-information-p nil) ;keep quiet for speed
|
|
3071 (start (progn
|
|
3072 ;; try to be smarter about finding the range of
|
|
3073 ;; lines to indent. skip all following
|
|
3074 ;; whitespace. failing that, try to find any
|
|
3075 ;; opening brace on the current line
|
|
3076 (skip-chars-forward " \t\n")
|
|
3077 (if (memq (following-char) '(?\( ?\[ ?\{))
|
|
3078 (point)
|
|
3079 (let ((state (parse-partial-sexp (point)
|
|
3080 (c-point 'eol))))
|
|
3081 (and (nth 1 state)
|
|
3082 (goto-char (nth 1 state))
|
|
3083 (memq (following-char) '(?\( ?\[ ?\{))
|
|
3084 (point)))))))
|
|
3085 ;; find balanced expression end
|
|
3086 (setq end (and (c-safe (progn (forward-sexp 1) t))
|
|
3087 (point-marker)))
|
|
3088 ;; sanity check
|
|
3089 (and (not start)
|
|
3090 (not shutup-p)
|
|
3091 (error "Cannot find start of balanced expression to indent."))
|
|
3092 (and (not end)
|
|
3093 (not shutup-p)
|
|
3094 (error "Cannot find end of balanced expression to indent."))
|
|
3095 (c-progress-init start end 'c-indent-exp)
|
|
3096 (setq progress-p t)
|
|
3097 (goto-char start)
|
|
3098 (beginning-of-line)
|
|
3099 (while (< (point) end)
|
|
3100 (if (not (looking-at "[ \t]*$"))
|
|
3101 (c-indent-line))
|
|
3102 (c-progress-update)
|
|
3103 (forward-line 1)))
|
|
3104 ;; make sure marker is deleted
|
|
3105 (and end
|
|
3106 (set-marker end nil))
|
|
3107 (and progress-p
|
|
3108 (c-progress-fini 'c-indent-exp))
|
|
3109 (goto-char here))))
|
|
3110
|
|
3111 (defun c-indent-defun ()
|
|
3112 "Re-indents the current top-level function def, struct or class declaration."
|
|
3113 (interactive)
|
|
3114 (let ((here (point-marker))
|
|
3115 (c-echo-syntactic-information-p nil)
|
|
3116 (brace (c-least-enclosing-brace (c-parse-state))))
|
|
3117 (if brace
|
|
3118 (goto-char brace)
|
|
3119 (beginning-of-defun))
|
|
3120 ;; if we're sitting at b-o-b, it might be because there was no
|
|
3121 ;; least enclosing brace and we were sitting on the defun's open
|
|
3122 ;; brace.
|
|
3123 (if (and (bobp) (not (= (following-char) ?\{)))
|
|
3124 (goto-char here))
|
|
3125 ;; if defun-prompt-regexp is non-nil, b-o-d might not leave us at
|
|
3126 ;; the open brace. I consider this an Emacs bug.
|
|
3127 (and (boundp 'defun-prompt-regexp)
|
|
3128 defun-prompt-regexp
|
|
3129 (looking-at defun-prompt-regexp)
|
|
3130 (goto-char (match-end 0)))
|
|
3131 ;; catch all errors in c-indent-exp so we can 1. give more
|
|
3132 ;; meaningful error message, and 2. restore point
|
|
3133 (unwind-protect
|
|
3134 (c-indent-exp)
|
|
3135 (goto-char here)
|
|
3136 (set-marker here nil))))
|
|
3137
|
|
3138 (defun c-indent-region (start end)
|
|
3139 ;; Indent every line whose first char is between START and END inclusive.
|
|
3140 (save-excursion
|
|
3141 (goto-char start)
|
|
3142 ;; Advance to first nonblank line.
|
|
3143 (skip-chars-forward " \t\n")
|
|
3144 (beginning-of-line)
|
|
3145 (let (endmark)
|
|
3146 (unwind-protect
|
|
3147 (let ((c-tab-always-indent t)
|
|
3148 ;; shut up any echo msgs on indiv lines
|
32
|
3149 (c-echo-syntactic-information-p nil)
|
|
3150 fence)
|
0
|
3151 (c-progress-init start end 'c-indent-region)
|
|
3152 (setq endmark (copy-marker end))
|
|
3153 (while (and (bolp)
|
|
3154 (not (eobp))
|
|
3155 (< (point) endmark))
|
|
3156 ;; update progress
|
|
3157 (c-progress-update)
|
|
3158 ;; Indent one line as with TAB.
|
|
3159 (let (nextline sexpend sexpbeg)
|
|
3160 ;; skip blank lines
|
|
3161 (skip-chars-forward " \t\n")
|
|
3162 (beginning-of-line)
|
|
3163 ;; indent the current line
|
|
3164 (c-indent-line)
|
32
|
3165 (setq fence (point))
|
0
|
3166 (if (save-excursion
|
|
3167 (beginning-of-line)
|
|
3168 (looking-at "[ \t]*#"))
|
|
3169 (forward-line 1)
|
|
3170 (save-excursion
|
|
3171 ;; Find beginning of following line.
|
|
3172 (setq nextline (c-point 'bonl))
|
|
3173 ;; Find first beginning-of-sexp for sexp extending past
|
|
3174 ;; this line.
|
|
3175 (beginning-of-line)
|
|
3176 (while (< (point) nextline)
|
|
3177 (condition-case nil
|
|
3178 (progn
|
|
3179 (forward-sexp 1)
|
|
3180 (setq sexpend (point)))
|
|
3181 (error (setq sexpend nil)
|
|
3182 (goto-char nextline)))
|
|
3183 (c-forward-syntactic-ws))
|
|
3184 (if sexpend
|
|
3185 (progn
|
|
3186 ;; make sure the sexp we found really starts on the
|
|
3187 ;; current line and extends past it
|
|
3188 (goto-char sexpend)
|
|
3189 (setq sexpend (point-marker))
|
|
3190 (c-safe (backward-sexp 1))
|
32
|
3191 (setq sexpbeg (point))))
|
|
3192 (if (and sexpbeg (< sexpbeg fence))
|
|
3193 (setq sexpbeg fence)))
|
0
|
3194 ;; check to see if the next line starts a
|
|
3195 ;; comment-only line
|
|
3196 (save-excursion
|
|
3197 (forward-line 1)
|
|
3198 (skip-chars-forward " \t")
|
|
3199 (if (looking-at c-comment-start-regexp)
|
|
3200 (setq sexpbeg (c-point 'bol))))
|
|
3201 ;; If that sexp ends within the region, indent it all at
|
|
3202 ;; once, fast.
|
|
3203 (condition-case nil
|
|
3204 (if (and sexpend
|
|
3205 (> sexpend nextline)
|
|
3206 (<= sexpend endmark))
|
|
3207 (progn
|
|
3208 (goto-char sexpbeg)
|
|
3209 (c-indent-exp 'shutup)
|
|
3210 (c-progress-update)
|
|
3211 (goto-char sexpend)))
|
|
3212 (error
|
|
3213 (goto-char sexpbeg)
|
|
3214 (c-indent-line)))
|
|
3215 ;; Move to following line and try again.
|
|
3216 (and sexpend
|
|
3217 (markerp sexpend)
|
|
3218 (set-marker sexpend nil))
|
32
|
3219 (forward-line 1)
|
|
3220 (setq fence (point))))))
|
0
|
3221 (set-marker endmark nil)
|
|
3222 (c-progress-fini 'c-indent-region)
|
|
3223 ))))
|
|
3224
|
|
3225 (defun c-mark-function ()
|
|
3226 "Put mark at end of a C, C++, or Objective-C defun, point at beginning."
|
|
3227 (interactive)
|
|
3228 (let ((here (point))
|
|
3229 ;; there should be a c-point position for 'eod
|
|
3230 (eod (save-excursion (end-of-defun) (point)))
|
|
3231 (state (c-parse-state))
|
|
3232 brace)
|
|
3233 (while state
|
|
3234 (setq brace (car state))
|
|
3235 (if (consp brace)
|
|
3236 (goto-char (cdr brace))
|
|
3237 (goto-char brace))
|
|
3238 (setq state (cdr state)))
|
|
3239 (if (= (following-char) ?{)
|
|
3240 (progn
|
|
3241 (forward-line -1)
|
|
3242 (while (not (or (bobp)
|
|
3243 (looking-at "[ \t]*$")))
|
|
3244 (forward-line -1)))
|
|
3245 (forward-line 1)
|
|
3246 (skip-chars-forward " \t\n"))
|
|
3247 (push-mark here)
|
|
3248 (push-mark eod nil t)))
|
|
3249
|
|
3250
|
|
3251 ;; for progress reporting
|
|
3252 (defvar c-progress-info nil)
|
|
3253
|
|
3254 (defun c-progress-init (start end context)
|
|
3255 ;; start the progress update messages. if this emacs doesn't have a
|
|
3256 ;; built-in timer, just be dumb about it
|
|
3257 (if (not (fboundp 'current-time))
|
|
3258 (message "indenting region... (this may take a while)")
|
|
3259 ;; if progress has already been initialized, do nothing. otherwise
|
|
3260 ;; initialize the counter with a vector of:
|
|
3261 ;; [start end lastsec context]
|
|
3262 (if c-progress-info
|
|
3263 ()
|
|
3264 (setq c-progress-info (vector start
|
|
3265 (save-excursion
|
|
3266 (goto-char end)
|
|
3267 (point-marker))
|
|
3268 (nth 1 (current-time))
|
|
3269 context))
|
|
3270 (message "indenting region..."))))
|
|
3271
|
|
3272 (defun c-progress-update ()
|
|
3273 ;; update progress
|
|
3274 (if (not (and c-progress-info c-progress-interval))
|
|
3275 nil
|
|
3276 (let ((now (nth 1 (current-time)))
|
|
3277 (start (aref c-progress-info 0))
|
|
3278 (end (aref c-progress-info 1))
|
|
3279 (lastsecs (aref c-progress-info 2)))
|
|
3280 ;; should we update? currently, update happens every 2 seconds,
|
|
3281 ;; what's the right value?
|
|
3282 (if (< c-progress-interval (- now lastsecs))
|
|
3283 (progn
|
|
3284 (message "indenting region... (%d%% complete)"
|
|
3285 (/ (* 100 (- (point) start)) (- end start)))
|
|
3286 (aset c-progress-info 2 now)))
|
|
3287 )))
|
|
3288
|
|
3289 (defun c-progress-fini (context)
|
|
3290 ;; finished
|
|
3291 (if (or (eq context (aref c-progress-info 3))
|
|
3292 (eq context t))
|
|
3293 (progn
|
|
3294 (set-marker (aref c-progress-info 1) nil)
|
|
3295 (setq c-progress-info nil)
|
|
3296 (message "indenting region...done"))))
|
|
3297
|
|
3298
|
|
3299 ;; Skipping of "syntactic whitespace" for Emacs 19. Syntactic
|
|
3300 ;; whitespace is defined as lexical whitespace, C and C++ style
|
|
3301 ;; comments, and preprocessor directives. Search no farther back or
|
|
3302 ;; forward than optional LIM. If LIM is omitted, `beginning-of-defun'
|
|
3303 ;; is used for backward skipping, point-max is used for forward
|
|
3304 ;; skipping. Note that Emacs 18 support has been moved to cc-mode-18.el.
|
|
3305
|
|
3306 (defun c-forward-syntactic-ws (&optional lim)
|
|
3307 ;; Forward skip of syntactic whitespace for Emacs 19.
|
|
3308 (save-restriction
|
|
3309 (let* ((lim (or lim (point-max)))
|
|
3310 (here lim)
|
|
3311 (hugenum (point-max)))
|
|
3312 (narrow-to-region lim (point))
|
|
3313 (while (/= here (point))
|
|
3314 (setq here (point))
|
|
3315 (forward-comment hugenum)
|
|
3316 ;; skip preprocessor directives
|
|
3317 (if (and (= (following-char) ?#)
|
|
3318 (= (c-point 'boi) (point)))
|
|
3319 (end-of-line)
|
|
3320 )))))
|
|
3321
|
|
3322 (defun c-backward-syntactic-ws (&optional lim)
|
|
3323 ;; Backward skip over syntactic whitespace for Emacs 19.
|
|
3324 (save-restriction
|
|
3325 (let* ((lim (or lim (c-point 'bod)))
|
|
3326 (here lim)
|
|
3327 (hugenum (- (point-max))))
|
|
3328 (if (< lim (point))
|
|
3329 (progn
|
|
3330 (narrow-to-region lim (point))
|
|
3331 (while (/= here (point))
|
|
3332 (setq here (point))
|
|
3333 (forward-comment hugenum)
|
|
3334 (if (eq (c-in-literal lim) 'pound)
|
|
3335 (beginning-of-line))
|
|
3336 )))
|
|
3337 )))
|
|
3338
|
|
3339
|
|
3340 ;; Return `c' if in a C-style comment, `c++' if in a C++ style
|
|
3341 ;; comment, `string' if in a string literal, `pound' if on a
|
|
3342 ;; preprocessor line, or nil if not in a comment at all. Optional LIM
|
|
3343 ;; is used as the backward limit of the search. If omitted, or nil,
|
|
3344 ;; `beginning-of-defun' is used."
|
|
3345
|
|
3346 ;; This is for all v19 Emacsen supporting either 1-bit or 8-bit syntax
|
|
3347 (defun c-in-literal (&optional lim)
|
|
3348 ;; Determine if point is in a C++ literal. we cache the last point
|
|
3349 ;; calculated if the cache is enabled
|
|
3350 (if (and (boundp 'c-in-literal-cache)
|
|
3351 c-in-literal-cache
|
|
3352 (= (point) (aref c-in-literal-cache 0)))
|
|
3353 (aref c-in-literal-cache 1)
|
|
3354 (let ((rtn (save-excursion
|
|
3355 (let* ((lim (or lim (c-point 'bod)))
|
|
3356 (here (point))
|
|
3357 (state (parse-partial-sexp lim (point))))
|
|
3358 (cond
|
|
3359 ((nth 3 state) 'string)
|
|
3360 ((nth 4 state) (if (nth 7 state) 'c++ 'c))
|
|
3361 ((progn
|
|
3362 (goto-char here)
|
|
3363 (beginning-of-line)
|
|
3364 (looking-at "[ \t]*#"))
|
|
3365 'pound)
|
|
3366 (t nil))))))
|
|
3367 ;; cache this result if the cache is enabled
|
|
3368 (and (boundp 'c-in-literal-cache)
|
|
3369 (setq c-in-literal-cache (vector (point) rtn)))
|
|
3370 rtn)))
|
|
3371
|
|
3372
|
|
3373 ;; utilities for moving and querying around syntactic elements
|
|
3374 (defun c-parse-state ()
|
|
3375 ;; Finds and records all open parens between some important point
|
|
3376 ;; earlier in the file and point.
|
|
3377 ;;
|
|
3378 ;; if there's a state cache, return it
|
|
3379 (if (boundp 'c-state-cache) c-state-cache
|
|
3380 (let* (at-bob
|
|
3381 (pos (save-excursion
|
|
3382 ;; go back 2 bods, but ignore any bogus positions
|
|
3383 ;; returned by beginning-of-defun (i.e. open paren
|
|
3384 ;; in column zero)
|
|
3385 (let ((cnt 2))
|
|
3386 (while (not (or at-bob (zerop cnt)))
|
|
3387 (beginning-of-defun)
|
|
3388 (if (= (following-char) ?\{)
|
|
3389 (setq cnt (1- cnt)))
|
|
3390 (if (bobp)
|
|
3391 (setq at-bob t))))
|
|
3392 (point)))
|
|
3393 (here (save-excursion
|
|
3394 ;;(skip-chars-forward " \t}")
|
|
3395 (point)))
|
|
3396 (last-bod pos) (last-pos pos)
|
|
3397 placeholder state sexp-end)
|
|
3398 ;; cache last bod position
|
|
3399 (while (catch 'backup-bod
|
|
3400 (setq state nil)
|
|
3401 (while (and pos (< pos here))
|
|
3402 (setq last-pos pos)
|
|
3403 (if (and (setq pos (c-safe (scan-lists pos 1 -1)))
|
|
3404 (<= pos here))
|
|
3405 (progn
|
|
3406 (setq sexp-end (c-safe (scan-sexps (1- pos) 1)))
|
|
3407 (if (and sexp-end
|
|
3408 (<= sexp-end here))
|
|
3409 ;; we want to record both the start and end
|
|
3410 ;; of this sexp, but we only want to record
|
|
3411 ;; the last-most of any of them before here
|
|
3412 (progn
|
|
3413 (if (= (char-after (1- pos)) ?\{)
|
|
3414 (setq state (cons (cons (1- pos) sexp-end)
|
|
3415 (if (consp (car state))
|
|
3416 (cdr state)
|
|
3417 state))))
|
|
3418 (setq pos sexp-end))
|
|
3419 ;; we're contained in this sexp so put pos on
|
|
3420 ;; front of list
|
|
3421 (setq state (cons (1- pos) state))))
|
|
3422 ;; something bad happened. check to see if we
|
|
3423 ;; crossed an unbalanced close brace. if so, we
|
|
3424 ;; didn't really find the right `important bufpos'
|
|
3425 ;; so lets back up and try again
|
|
3426 (if (and (not pos) (not at-bob)
|
|
3427 (setq placeholder
|
|
3428 (c-safe (scan-lists last-pos 1 1)))
|
|
3429 ;;(char-after (1- placeholder))
|
|
3430 (<= placeholder here)
|
|
3431 (= (char-after (1- placeholder)) ?\}))
|
|
3432 (while t
|
|
3433 (setq last-bod (c-safe (scan-lists last-bod -1 1)))
|
|
3434 (if (not last-bod)
|
|
3435 (error "unbalanced close brace at position %d"
|
|
3436 (1- placeholder))
|
|
3437 (setq at-bob (= last-bod (point-min))
|
|
3438 pos last-bod)
|
|
3439 (if (= (char-after last-bod) ?\{)
|
|
3440 (throw 'backup-bod t)))
|
|
3441 )) ;end-if
|
|
3442 )) ;end-while
|
|
3443 nil))
|
|
3444 state)))
|
|
3445
|
|
3446 (defun c-whack-state (bufpos state)
|
|
3447 ;; whack off any state information that appears on STATE which lies
|
|
3448 ;; after the bounds of BUFPOS.
|
|
3449 (let (newstate car)
|
|
3450 (while state
|
|
3451 (setq car (car state)
|
|
3452 state (cdr state))
|
|
3453 (if (consp car)
|
|
3454 ;; just check the car, because in a balanced brace
|
|
3455 ;; expression, it must be impossible for the corresponding
|
|
3456 ;; close brace to be before point, but the open brace to be
|
|
3457 ;; after.
|
|
3458 (if (<= bufpos (car car))
|
|
3459 nil ; whack it off
|
|
3460 ;; its possible that the open brace is before bufpos, but
|
|
3461 ;; the close brace is after. In that case, convert this
|
|
3462 ;; to a non-cons element.
|
|
3463 (if (<= bufpos (cdr car))
|
|
3464 (setq newstate (append newstate (list (car car))))
|
|
3465 ;; we know that both the open and close braces are
|
|
3466 ;; before bufpos, so we also know that everything else
|
|
3467 ;; on state is before bufpos, so we can glom up the
|
|
3468 ;; whole thing and exit.
|
|
3469 (setq newstate (append newstate (list car) state)
|
|
3470 state nil)))
|
|
3471 (if (<= bufpos car)
|
|
3472 nil ; whack it off
|
|
3473 ;; it's before bufpos, so everything else should too
|
|
3474 (setq newstate (append newstate (list car) state)
|
|
3475 state nil))))
|
|
3476 newstate))
|
|
3477
|
|
3478 (defun c-hack-state (bufpos which state)
|
|
3479 ;; Using BUFPOS buffer position, and WHICH (must be 'open or
|
|
3480 ;; 'close), hack the c-parse-state STATE and return the results.
|
|
3481 (if (eq which 'open)
|
|
3482 (let ((car (car state)))
|
|
3483 (if (or (null car)
|
|
3484 (consp car)
|
|
3485 (/= bufpos car))
|
|
3486 (cons bufpos state)
|
|
3487 state))
|
|
3488 (if (not (eq which 'close))
|
|
3489 (error "c-hack-state, bad argument: %s" which))
|
|
3490 ;; 'close brace
|
|
3491 (let ((car (car state))
|
|
3492 (cdr (cdr state)))
|
|
3493 (if (consp car)
|
|
3494 (setq car (car cdr)
|
|
3495 cdr (cdr cdr)))
|
|
3496 ;; TBD: is this test relevant???
|
|
3497 (if (consp car)
|
|
3498 state ;on error, don't change
|
|
3499 ;; watch out for balanced expr already on cdr of list
|
|
3500 (cons (cons car bufpos)
|
|
3501 (if (consp (car cdr))
|
|
3502 (cdr cdr) cdr))
|
|
3503 ))))
|
|
3504
|
|
3505 (defun c-adjust-state (from to shift state)
|
|
3506 ;; Adjust all points in state that lie in the region FROM..TO by
|
|
3507 ;; SHIFT amount (as would be returned by c-indent-line).
|
|
3508 (mapcar
|
|
3509 (function
|
|
3510 (lambda (e)
|
|
3511 (if (consp e)
|
|
3512 (let ((car (car e))
|
|
3513 (cdr (cdr e)))
|
|
3514 (if (and (<= from car) (< car to))
|
|
3515 (setcar e (+ shift car)))
|
|
3516 (if (and (<= from cdr) (< cdr to))
|
|
3517 (setcdr e (+ shift cdr))))
|
|
3518 (if (and (<= from e) (< e to))
|
|
3519 (setq e (+ shift e))))
|
|
3520 e))
|
|
3521 state))
|
|
3522
|
|
3523
|
|
3524 (defun c-beginning-of-inheritance-list (&optional lim)
|
|
3525 ;; Go to the first non-whitespace after the colon that starts a
|
|
3526 ;; multiple inheritance introduction. Optional LIM is the farthest
|
|
3527 ;; back we should search.
|
|
3528 (let ((lim (or lim (c-point 'bod)))
|
|
3529 (placeholder (progn
|
|
3530 (back-to-indentation)
|
|
3531 (point))))
|
|
3532 (c-backward-syntactic-ws lim)
|
|
3533 (while (and (> (point) lim)
|
|
3534 (memq (preceding-char) '(?, ?:))
|
|
3535 (progn
|
|
3536 (beginning-of-line)
|
|
3537 (setq placeholder (point))
|
|
3538 (skip-chars-forward " \t")
|
|
3539 (not (looking-at c-class-key))
|
|
3540 ))
|
|
3541 (c-backward-syntactic-ws lim))
|
|
3542 (goto-char placeholder)
|
|
3543 (skip-chars-forward "^:" (c-point 'eol))))
|
|
3544
|
|
3545 (defun c-beginning-of-macro (&optional lim)
|
|
3546 ;; Go to the beginning of the macro. Right now we don't support
|
|
3547 ;; multi-line macros too well
|
|
3548 (back-to-indentation))
|
|
3549
|
|
3550 (defun c-in-method-def-p ()
|
|
3551 ;; Return nil if we aren't in a method definition, otherwise the
|
|
3552 ;; position of the initial [+-].
|
|
3553 (save-excursion
|
|
3554 (beginning-of-line)
|
|
3555 (and c-method-key
|
|
3556 (looking-at c-method-key)
|
|
3557 (point))
|
|
3558 ))
|
|
3559
|
|
3560 (defun c-just-after-func-arglist-p (&optional containing)
|
|
3561 ;; Return t if we are between a function's argument list closing
|
|
3562 ;; paren and its opening brace. Note that the list close brace
|
|
3563 ;; could be followed by a "const" specifier or a member init hanging
|
|
3564 ;; colon. Optional CONTAINING is position of containing s-exp open
|
|
3565 ;; brace. If not supplied, point is used as search start.
|
|
3566 (save-excursion
|
|
3567 (c-backward-syntactic-ws)
|
|
3568 (let ((checkpoint (or containing (point))))
|
|
3569 (goto-char checkpoint)
|
|
3570 ;; could be looking at const specifier
|
|
3571 (if (and (= (preceding-char) ?t)
|
|
3572 (forward-word -1)
|
|
3573 (looking-at "\\<const\\>"))
|
|
3574 (c-backward-syntactic-ws)
|
|
3575 ;; otherwise, we could be looking at a hanging member init
|
|
3576 ;; colon
|
|
3577 (goto-char checkpoint)
|
|
3578 (if (and (= (preceding-char) ?:)
|
|
3579 (progn
|
|
3580 (forward-char -1)
|
|
3581 (c-backward-syntactic-ws)
|
|
3582 (looking-at "[ \t\n]*:\\([^:]+\\|$\\)")))
|
|
3583 nil
|
|
3584 (goto-char checkpoint))
|
|
3585 )
|
|
3586 (and (= (preceding-char) ?\))
|
|
3587 ;; check if we are looking at a method def
|
|
3588 (or (not c-method-key)
|
|
3589 (progn
|
|
3590 (forward-sexp -1)
|
|
3591 (forward-char -1)
|
|
3592 (c-backward-syntactic-ws)
|
|
3593 (not (or (= (preceding-char) ?-)
|
|
3594 (= (preceding-char) ?+)
|
|
3595 ;; or a class category
|
|
3596 (progn
|
|
3597 (forward-sexp -2)
|
|
3598 (looking-at c-class-key))
|
|
3599 )))))
|
|
3600 )))
|
|
3601
|
|
3602 ;; defuns to look backwards for things
|
|
3603 (defun c-backward-to-start-of-do (&optional lim)
|
|
3604 ;; Move to the start of the last "unbalanced" do expression.
|
|
3605 ;; Optional LIM is the farthest back to search. If none is found,
|
|
3606 ;; nil is returned and point is left unchanged, otherwise t is returned.
|
|
3607 (let ((do-level 1)
|
|
3608 (case-fold-search nil)
|
|
3609 (lim (or lim (c-point 'bod)))
|
|
3610 (here (point))
|
|
3611 foundp)
|
|
3612 (while (not (zerop do-level))
|
|
3613 ;; we protect this call because trying to execute this when the
|
|
3614 ;; while is not associated with a do will throw an error
|
|
3615 (condition-case nil
|
|
3616 (progn
|
|
3617 (backward-sexp 1)
|
|
3618 (cond
|
|
3619 ((memq (c-in-literal lim) '(c c++)))
|
|
3620 ((looking-at "while\\b[^_]")
|
|
3621 (setq do-level (1+ do-level)))
|
|
3622 ((looking-at "do\\b[^_]")
|
|
3623 (if (zerop (setq do-level (1- do-level)))
|
|
3624 (setq foundp t)))
|
|
3625 ((<= (point) lim)
|
|
3626 (setq do-level 0)
|
|
3627 (goto-char lim))))
|
|
3628 (error
|
|
3629 (goto-char lim)
|
|
3630 (setq do-level 0))))
|
|
3631 (if (not foundp)
|
|
3632 (goto-char here))
|
|
3633 foundp))
|
|
3634
|
|
3635 (defun c-backward-to-start-of-if (&optional lim)
|
|
3636 ;; Move to the start of the last "unbalanced" if and return t. If
|
|
3637 ;; none is found, and we are looking at an if clause, nil is
|
|
3638 ;; returned. If none is found and we are looking at an else clause,
|
|
3639 ;; an error is thrown.
|
|
3640 (let ((if-level 1)
|
|
3641 (here (c-point 'bol))
|
|
3642 (case-fold-search nil)
|
|
3643 (lim (or lim (c-point 'bod)))
|
|
3644 (at-if (looking-at "if\\b[^_]")))
|
|
3645 (catch 'orphan-if
|
|
3646 (while (and (not (bobp))
|
|
3647 (not (zerop if-level)))
|
|
3648 (c-backward-syntactic-ws)
|
|
3649 (condition-case nil
|
|
3650 (backward-sexp 1)
|
|
3651 (error
|
|
3652 (if at-if
|
|
3653 (throw 'orphan-if nil)
|
|
3654 (error "No matching `if' found for `else' on line %d."
|
|
3655 (1+ (count-lines 1 here))))))
|
|
3656 (cond
|
|
3657 ((looking-at "else\\b[^_]")
|
|
3658 (setq if-level (1+ if-level)))
|
|
3659 ((looking-at "if\\b[^_]")
|
|
3660 ;; check for else if... skip over
|
|
3661 (let ((here (point)))
|
|
3662 (c-safe (forward-sexp -1))
|
|
3663 (if (looking-at "\\<else\\>[ \t]+\\<if\\>")
|
|
3664 nil
|
|
3665 (setq if-level (1- if-level))
|
|
3666 (goto-char here))))
|
|
3667 ((< (point) lim)
|
|
3668 (setq if-level 0)
|
|
3669 (goto-char lim))
|
|
3670 ))
|
|
3671 t)))
|
|
3672
|
|
3673 (defun c-skip-conditional ()
|
|
3674 ;; skip forward over conditional at point, including any predicate
|
|
3675 ;; statements in parentheses. No error checking is performed.
|
2
|
3676 (forward-sexp (cond
|
|
3677 ;; else if()
|
|
3678 ((looking-at "\\<else\\>[ \t]+\\<if\\>") 3)
|
|
3679 ;; do, else, try, finally
|
|
3680 ((looking-at "\\<\\(do\\|else\\|try\\|finally\\)\\>") 1)
|
|
3681 ;; for, if, while, switch, catch, synchronized
|
|
3682 (t 2))))
|
0
|
3683
|
|
3684 (defun c-skip-case-statement-forward (state &optional lim)
|
|
3685 ;; skip forward over case/default bodies, with optional maximal
|
|
3686 ;; limit. if no next case body is found, nil is returned and point
|
|
3687 ;; is not moved
|
|
3688 (let ((lim (or lim (point-max)))
|
|
3689 (here (point))
|
|
3690 donep foundp bufpos
|
|
3691 (safepos (point))
|
|
3692 (balanced (car state)))
|
|
3693 ;; search until we've passed the limit, or we've found our match
|
|
3694 (while (and (< (point) lim)
|
|
3695 (not donep))
|
|
3696 (setq safepos (point))
|
|
3697 ;; see if we can find a case statement, not in a literal
|
|
3698 (if (and (re-search-forward c-switch-label-key lim 'move)
|
|
3699 (setq bufpos (match-beginning 0))
|
|
3700 (not (c-in-literal safepos))
|
|
3701 (/= bufpos here))
|
|
3702 ;; if we crossed into a balanced sexp, we know the case is
|
|
3703 ;; not part of our switch statement, so just bound over the
|
|
3704 ;; sexp and keep looking.
|
|
3705 (if (and (consp balanced)
|
|
3706 (> bufpos (car balanced))
|
|
3707 (< bufpos (cdr balanced)))
|
|
3708 (goto-char (cdr balanced))
|
|
3709 (goto-char bufpos)
|
|
3710 (setq donep t
|
|
3711 foundp t))))
|
|
3712 (if (not foundp)
|
|
3713 (goto-char here))
|
|
3714 foundp))
|
|
3715
|
|
3716 (defun c-search-uplist-for-classkey (brace-state)
|
|
3717 ;; search for the containing class, returning a 2 element vector if
|
|
3718 ;; found. aref 0 contains the bufpos of the class key, and aref 1
|
|
3719 ;; contains the bufpos of the open brace.
|
|
3720 (if (null brace-state)
|
|
3721 ;; no brace-state means we cannot be inside a class
|
|
3722 nil
|
|
3723 (let ((carcache (car brace-state))
|
|
3724 search-start search-end)
|
|
3725 (if (consp carcache)
|
|
3726 ;; a cons cell in the first element means that there is some
|
|
3727 ;; balanced sexp before the current bufpos. this we can
|
|
3728 ;; ignore. the nth 1 and nth 2 elements define for us the
|
|
3729 ;; search boundaries
|
|
3730 (setq search-start (nth 2 brace-state)
|
|
3731 search-end (nth 1 brace-state))
|
|
3732 ;; if the car was not a cons cell then nth 0 and nth 1 define
|
|
3733 ;; for us the search boundaries
|
|
3734 (setq search-start (nth 1 brace-state)
|
|
3735 search-end (nth 0 brace-state)))
|
|
3736 ;; search-end cannot be a cons cell
|
|
3737 (and (consp search-end)
|
|
3738 (error "consp search-end: %s" search-end))
|
|
3739 ;; if search-end is nil, or if the search-end character isn't an
|
|
3740 ;; open brace, we are definitely not in a class
|
|
3741 (if (or (not search-end)
|
|
3742 (< search-end (point-min))
|
|
3743 (/= (char-after search-end) ?{))
|
|
3744 nil
|
|
3745 ;; now, we need to look more closely at search-start. if
|
|
3746 ;; search-start is nil, then our start boundary is really
|
|
3747 ;; point-min.
|
|
3748 (if (not search-start)
|
|
3749 (setq search-start (point-min))
|
|
3750 ;; if search-start is a cons cell, then we can start
|
|
3751 ;; searching from the end of the balanced sexp just ahead of
|
|
3752 ;; us
|
|
3753 (if (consp search-start)
|
|
3754 (setq search-start (cdr search-start))))
|
|
3755 ;; now we can do a quick regexp search from search-start to
|
|
3756 ;; search-end and see if we can find a class key. watch for
|
|
3757 ;; class like strings in literals
|
|
3758 (save-excursion
|
|
3759 (save-restriction
|
|
3760 (goto-char search-start)
|
2
|
3761 (let ((search-key (concat c-class-key "\\|extern[^_]"))
|
|
3762 foundp class match-end)
|
0
|
3763 (while (and (not foundp)
|
|
3764 (progn
|
|
3765 (c-forward-syntactic-ws)
|
|
3766 (> search-end (point)))
|
2
|
3767 (re-search-forward search-key search-end t))
|
0
|
3768 (setq class (match-beginning 0)
|
|
3769 match-end (match-end 0))
|
|
3770 (if (c-in-literal search-start)
|
|
3771 nil ; its in a comment or string, ignore
|
|
3772 (goto-char class)
|
|
3773 (skip-chars-forward " \t\n")
|
|
3774 (setq foundp (vector (c-point 'boi) search-end))
|
|
3775 (cond
|
|
3776 ;; check for embedded keywords
|
|
3777 ((let ((char (char-after (1- class))))
|
|
3778 (and char
|
|
3779 (memq (char-syntax char) '(?w ?_))))
|
|
3780 (goto-char match-end)
|
|
3781 (setq foundp nil))
|
|
3782 ;; make sure we're really looking at the start of a
|
|
3783 ;; class definition, and not a forward decl, return
|
|
3784 ;; arg, template arg list, or an ObjC or Java method.
|
|
3785 ((and c-method-key
|
|
3786 (re-search-forward c-method-key search-end t))
|
|
3787 (setq foundp nil))
|
|
3788 ;; Its impossible to define a regexp for this, and
|
|
3789 ;; nearly so to do it programmatically.
|
|
3790 ;;
|
|
3791 ;; ; picks up forward decls
|
|
3792 ;; = picks up init lists
|
|
3793 ;; ) picks up return types
|
|
3794 ;; > picks up templates, but remember that we can
|
|
3795 ;; inherit from templates!
|
|
3796 ((let ((skipchars "^;=)"))
|
|
3797 ;; try to see if we found the `class' keyword
|
|
3798 ;; inside a template arg list
|
|
3799 (save-excursion
|
|
3800 (skip-chars-backward "^<>" search-start)
|
|
3801 (if (= (preceding-char) ?<)
|
|
3802 (setq skipchars (concat skipchars ">"))))
|
|
3803 (skip-chars-forward skipchars search-end)
|
|
3804 (/= (point) search-end))
|
|
3805 (setq foundp nil))
|
|
3806 )))
|
|
3807 foundp))
|
|
3808 )))))
|
|
3809
|
|
3810 (defun c-inside-bracelist-p (containing-sexp brace-state)
|
|
3811 ;; return the buffer position of the beginning of the brace list
|
|
3812 ;; statement if we're inside a brace list, otherwise return nil.
|
|
3813 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
|
|
3814 ;; paren. BRACE-STATE is the remainder of the state of enclosing braces
|
|
3815 ;;
|
|
3816 ;; N.B.: This algorithm can potentially get confused by cpp macros
|
|
3817 ;; places in inconvenient locations. Its a trade-off we make for
|
|
3818 ;; speed.
|
|
3819 (or
|
|
3820 ;; this will pick up enum lists
|
|
3821 (condition-case ()
|
|
3822 (save-excursion
|
|
3823 (goto-char containing-sexp)
|
|
3824 (forward-sexp -1)
|
|
3825 (if (or (looking-at "enum[\t\n ]+")
|
|
3826 (progn (forward-sexp -1)
|
|
3827 (looking-at "enum[\t\n ]+")))
|
|
3828 (point)))
|
|
3829 (error nil))
|
|
3830 ;; this will pick up array/aggregate init lists, even if they are nested.
|
|
3831 (save-excursion
|
|
3832 (let (bufpos failedp)
|
|
3833 (while (and (not bufpos)
|
|
3834 containing-sexp)
|
|
3835 (if (consp containing-sexp)
|
|
3836 (setq containing-sexp (car brace-state)
|
|
3837 brace-state (cdr brace-state))
|
|
3838 ;; see if significant character just before brace is an equal
|
|
3839 (goto-char containing-sexp)
|
|
3840 (setq failedp nil)
|
|
3841 (condition-case ()
|
|
3842 (progn
|
|
3843 (forward-sexp -1)
|
|
3844 (forward-sexp 1)
|
|
3845 (c-forward-syntactic-ws containing-sexp))
|
|
3846 (error (setq failedp t)))
|
|
3847 (if (or failedp (/= (following-char) ?=))
|
|
3848 ;; lets see if we're nested. find the most nested
|
|
3849 ;; containing brace
|
|
3850 (setq containing-sexp (car brace-state)
|
|
3851 brace-state (cdr brace-state))
|
|
3852 ;; we've hit the beginning of the aggregate list
|
|
3853 (c-beginning-of-statement-1 (c-most-enclosing-brace brace-state))
|
|
3854 (setq bufpos (point)))
|
|
3855 ))
|
|
3856 bufpos))
|
|
3857 ))
|
|
3858
|
|
3859
|
|
3860 ;; defuns for calculating the syntactic state and indenting a single
|
|
3861 ;; line of C/C++/ObjC code
|
|
3862 (defun c-most-enclosing-brace (state)
|
|
3863 ;; return the bufpos of the most enclosing brace that hasn't been
|
|
3864 ;; narrowed out by any enclosing class, or nil if none was found
|
|
3865 (let (enclosingp)
|
|
3866 (while (and state (not enclosingp))
|
|
3867 (setq enclosingp (car state)
|
|
3868 state (cdr state))
|
|
3869 (if (consp enclosingp)
|
|
3870 (setq enclosingp nil)
|
|
3871 (if (> (point-min) enclosingp)
|
|
3872 (setq enclosingp nil))
|
|
3873 (setq state nil)))
|
|
3874 enclosingp))
|
|
3875
|
|
3876 (defun c-least-enclosing-brace (state)
|
|
3877 ;; return the bufpos of the least (highest) enclosing brace that
|
|
3878 ;; hasn't been narrowed out by any enclosing class, or nil if none
|
|
3879 ;; was found.
|
|
3880 (c-most-enclosing-brace (nreverse state)))
|
|
3881
|
|
3882 (defun c-safe-position (bufpos state)
|
|
3883 ;; return the closest known safe position higher up than point
|
|
3884 (let ((safepos nil))
|
|
3885 (while state
|
|
3886 (setq safepos
|
|
3887 (if (consp (car state))
|
|
3888 (cdr (car state))
|
|
3889 (car state)))
|
|
3890 (if (< safepos bufpos)
|
|
3891 (setq state nil)
|
|
3892 (setq state (cdr state))))
|
|
3893 safepos))
|
|
3894
|
|
3895 (defun c-narrow-out-enclosing-class (state lim)
|
|
3896 ;; narrow the buffer so that the enclosing class is hidden
|
|
3897 (let (inclass-p)
|
|
3898 (and state
|
|
3899 (setq inclass-p (c-search-uplist-for-classkey state))
|
|
3900 (narrow-to-region
|
|
3901 (progn
|
|
3902 (goto-char (1+ (aref inclass-p 1)))
|
|
3903 (skip-chars-forward " \t\n" lim)
|
|
3904 ;; if point is now left of the class opening brace, we're
|
|
3905 ;; hosed, so try a different tact
|
|
3906 (if (<= (point) (aref inclass-p 1))
|
|
3907 (progn
|
|
3908 (goto-char (1+ (aref inclass-p 1)))
|
|
3909 (c-forward-syntactic-ws lim)))
|
|
3910 (point))
|
|
3911 ;; end point is the end of the current line
|
|
3912 (progn
|
|
3913 (goto-char lim)
|
|
3914 (c-point 'eol))))
|
|
3915 ;; return the class vector
|
|
3916 inclass-p))
|
|
3917
|
|
3918 (defun c-guess-basic-syntax ()
|
|
3919 ;; guess the syntactic description of the current line of C++ code.
|
|
3920 (save-excursion
|
|
3921 (save-restriction
|
|
3922 (beginning-of-line)
|
|
3923 (let* ((indent-point (point))
|
|
3924 (case-fold-search nil)
|
|
3925 (fullstate (c-parse-state))
|
|
3926 (state fullstate)
|
|
3927 (in-method-intro-p (and c-method-key
|
|
3928 (looking-at c-method-key)))
|
|
3929 literal containing-sexp char-before-ip char-after-ip lim
|
|
3930 syntax placeholder c-in-literal-cache inswitch-p
|
32
|
3931 injava-inher
|
2
|
3932 ;; narrow out any enclosing class or extern "C" block
|
0
|
3933 (inclass-p (c-narrow-out-enclosing-class state indent-point))
|
2
|
3934 (inextern-p (and inclass-p
|
|
3935 (save-excursion
|
|
3936 (save-restriction
|
|
3937 (widen)
|
|
3938 (goto-char (aref inclass-p 0))
|
|
3939 (looking-at "extern[^_]")))))
|
0
|
3940 )
|
|
3941
|
|
3942 ;; get the buffer position of the most nested opening brace,
|
|
3943 ;; if there is one, and it hasn't been narrowed out
|
|
3944 (save-excursion
|
|
3945 (goto-char indent-point)
|
|
3946 (skip-chars-forward " \t}")
|
|
3947 (skip-chars-backward " \t")
|
|
3948 (while (and state
|
|
3949 (not in-method-intro-p)
|
|
3950 (not containing-sexp))
|
|
3951 (setq containing-sexp (car state)
|
|
3952 state (cdr state))
|
|
3953 (if (consp containing-sexp)
|
|
3954 ;; if cdr == point, then containing sexp is the brace
|
|
3955 ;; that opens the sexp we close
|
|
3956 (if (= (cdr containing-sexp) (point))
|
|
3957 (setq containing-sexp (car containing-sexp))
|
|
3958 ;; otherwise, ignore this element
|
|
3959 (setq containing-sexp nil))
|
|
3960 ;; ignore the bufpos if its been narrowed out by the
|
|
3961 ;; containing class
|
|
3962 (if (<= containing-sexp (point-min))
|
|
3963 (setq containing-sexp nil)))))
|
|
3964
|
|
3965 ;; set the limit on the farthest back we need to search
|
|
3966 (setq lim (or containing-sexp
|
|
3967 (if (consp (car fullstate))
|
|
3968 (cdr (car fullstate))
|
|
3969 nil)
|
|
3970 (point-min)))
|
|
3971
|
|
3972 ;; cache char before and after indent point, and move point to
|
|
3973 ;; the most likely position to perform the majority of tests
|
|
3974 (goto-char indent-point)
|
|
3975 (skip-chars-forward " \t")
|
|
3976 (setq char-after-ip (following-char))
|
|
3977 (c-backward-syntactic-ws lim)
|
|
3978 (setq char-before-ip (preceding-char))
|
|
3979 (goto-char indent-point)
|
|
3980 (skip-chars-forward " \t")
|
|
3981
|
|
3982 ;; are we in a literal?
|
|
3983 (setq literal (c-in-literal lim))
|
|
3984
|
|
3985 ;; now figure out syntactic qualities of the current line
|
|
3986 (cond
|
|
3987 ;; CASE 1: in a string.
|
|
3988 ((memq literal '(string))
|
|
3989 (c-add-syntax 'string (c-point 'bopl)))
|
|
3990 ;; CASE 2: in a C or C++ style comment.
|
|
3991 ((memq literal '(c c++))
|
|
3992 ;; we need to catch multi-paragraph C comments
|
|
3993 (while (and (zerop (forward-line -1))
|
|
3994 (looking-at "^[ \t]*$")))
|
32
|
3995 (c-add-syntax literal (c-point 'boi)))
|
0
|
3996 ;; CASE 3: in a cpp preprocessor
|
|
3997 ((eq literal 'pound)
|
|
3998 (c-beginning-of-macro lim)
|
|
3999 (c-add-syntax 'cpp-macro (c-point 'boi)))
|
|
4000 ;; CASE 4: in an objective-c method intro
|
|
4001 (in-method-intro-p
|
|
4002 (c-add-syntax 'objc-method-intro (c-point 'boi)))
|
|
4003 ;; CASE 5: Line is at top level.
|
|
4004 ((null containing-sexp)
|
|
4005 (cond
|
|
4006 ;; CASE 5A: we are looking at a defun, class, or
|
|
4007 ;; inline-inclass method opening brace
|
|
4008 ((= char-after-ip ?{)
|
|
4009 (cond
|
2
|
4010 ;; CASE 5A.1: extern declaration
|
|
4011 ((save-excursion
|
|
4012 (goto-char indent-point)
|
|
4013 (skip-chars-forward " \t")
|
|
4014 (and (c-safe (progn (backward-sexp 2) t))
|
|
4015 (looking-at "extern[^_]")
|
|
4016 (progn
|
|
4017 (setq placeholder (point))
|
|
4018 (forward-sexp 1)
|
|
4019 (c-forward-syntactic-ws)
|
|
4020 (= (following-char) ?\"))))
|
|
4021 (goto-char placeholder)
|
|
4022 (c-add-syntax 'extern-lang-open (c-point 'boi)))
|
|
4023 ;; CASE 5A.2: we are looking at a class opening brace
|
0
|
4024 ((save-excursion
|
|
4025 (goto-char indent-point)
|
|
4026 (skip-chars-forward " \t{")
|
|
4027 ;; TBD: watch out! there could be a bogus
|
|
4028 ;; c-state-cache in place when we get here. we have
|
|
4029 ;; to go through much chicanery to ignore the cache.
|
|
4030 ;; But of course, there may not be! BLECH! BOGUS!
|
|
4031 (let ((decl
|
|
4032 (if (boundp 'c-state-cache)
|
|
4033 (let ((old-cache c-state-cache))
|
|
4034 (prog2
|
|
4035 (makunbound 'c-state-cache)
|
|
4036 (c-search-uplist-for-classkey (c-parse-state))
|
|
4037 (setq c-state-cache old-cache)))
|
|
4038 (c-search-uplist-for-classkey (c-parse-state))
|
|
4039 )))
|
|
4040 (and decl
|
|
4041 (setq placeholder (aref decl 0)))
|
|
4042 ))
|
|
4043 (c-add-syntax 'class-open placeholder))
|
2
|
4044 ;; CASE 5A.3: brace list open
|
0
|
4045 ((save-excursion
|
|
4046 (c-beginning-of-statement-1 lim)
|
|
4047 ;; c-b-o-s could have left us at point-min
|
|
4048 (and (bobp)
|
|
4049 (c-forward-syntactic-ws indent-point))
|
32
|
4050 (if (looking-at "typedef[^_]")
|
|
4051 (progn (forward-sexp 1)
|
|
4052 (c-forward-syntactic-ws indent-point)))
|
44
|
4053 (setq placeholder (c-point 'boi))
|
0
|
4054 (and (or (looking-at "enum[ \t\n]+")
|
|
4055 (= char-before-ip ?=))
|
|
4056 (save-excursion
|
|
4057 (skip-chars-forward "^;(" indent-point)
|
|
4058 (not (memq (following-char) '(?\; ?\()))
|
|
4059 )))
|
|
4060 (c-add-syntax 'brace-list-open placeholder))
|
2
|
4061 ;; CASE 5A.4: inline defun open
|
|
4062 ((and inclass-p (not inextern-p))
|
|
4063 (c-add-syntax 'inline-open)
|
|
4064 (c-add-syntax 'inclass (aref inclass-p 0)))
|
|
4065 ;; CASE 5A.5: ordinary defun open
|
0
|
4066 (t
|
|
4067 (goto-char placeholder)
|
|
4068 (c-add-syntax 'defun-open (c-point 'bol))
|
|
4069 )))
|
|
4070 ;; CASE 5B: first K&R arg decl or member init
|
|
4071 ((c-just-after-func-arglist-p)
|
|
4072 (cond
|
|
4073 ;; CASE 5B.1: a member init
|
|
4074 ((or (= char-before-ip ?:)
|
|
4075 (= char-after-ip ?:))
|
|
4076 ;; this line should be indented relative to the beginning
|
|
4077 ;; of indentation for the topmost-intro line that contains
|
|
4078 ;; the prototype's open paren
|
|
4079 ;; TBD: is the following redundant?
|
|
4080 (if (= char-before-ip ?:)
|
|
4081 (forward-char -1))
|
|
4082 (c-backward-syntactic-ws lim)
|
|
4083 ;; TBD: is the preceding redundant?
|
|
4084 (if (= (preceding-char) ?:)
|
|
4085 (progn (forward-char -1)
|
|
4086 (c-backward-syntactic-ws lim)))
|
|
4087 (if (= (preceding-char) ?\))
|
|
4088 (backward-sexp 1))
|
32
|
4089 (setq placeholder (point))
|
|
4090 (save-excursion
|
|
4091 (and (c-safe (backward-sexp 1) t)
|
|
4092 (looking-at "throw[^_]")
|
|
4093 (c-safe (backward-sexp 1) t)
|
|
4094 (setq placeholder (point))))
|
|
4095 (goto-char placeholder)
|
0
|
4096 (c-add-syntax 'member-init-intro (c-point 'boi))
|
|
4097 ;; we don't need to add any class offset since this
|
|
4098 ;; should be relative to the ctor's indentation
|
|
4099 )
|
|
4100 ;; CASE 5B.2: K&R arg decl intro
|
|
4101 (c-recognize-knr-p
|
|
4102 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
|
|
4103 (and inclass-p (c-add-syntax 'inclass (aref inclass-p 0))))
|
32
|
4104 ;; CASE 5B.3: Nether region after a C++ or Java func
|
|
4105 ;; decl, which could include a `throws' declaration.
|
0
|
4106 (t
|
|
4107 (c-beginning-of-statement-1 lim)
|
32
|
4108 (c-add-syntax 'func-decl-cont (c-point 'boi))
|
0
|
4109 )))
|
|
4110 ;; CASE 5C: inheritance line. could be first inheritance
|
|
4111 ;; line, or continuation of a multiple inheritance
|
|
4112 ((or (and c-baseclass-key (looking-at c-baseclass-key))
|
|
4113 (and (or (= char-before-ip ?:)
|
32
|
4114 ;; watch out for scope operator
|
|
4115 (save-excursion
|
|
4116 (and (= char-after-ip ?:)
|
|
4117 (c-safe (progn (forward-char 1) t))
|
|
4118 (/= (following-char) ?:)
|
|
4119 )))
|
0
|
4120 (save-excursion
|
|
4121 (c-backward-syntactic-ws lim)
|
|
4122 (if (= char-before-ip ?:)
|
|
4123 (progn
|
|
4124 (forward-char -1)
|
|
4125 (c-backward-syntactic-ws lim)))
|
|
4126 (back-to-indentation)
|
32
|
4127 (looking-at c-class-key)))
|
|
4128 ;; for Java
|
|
4129 (and (eq major-mode 'java-mode)
|
|
4130 (let ((fence (save-excursion
|
|
4131 (c-beginning-of-statement-1 lim)
|
|
4132 (point)))
|
|
4133 cont done)
|
|
4134 (save-excursion
|
|
4135 (while (not done)
|
|
4136 (cond ((looking-at c-Java-special-key)
|
|
4137 (setq injava-inher (cons cont (point))
|
|
4138 done t))
|
|
4139 ((or (not (c-safe (forward-sexp -1) t))
|
|
4140 (<= (point) fence))
|
|
4141 (setq done t))
|
|
4142 )
|
|
4143 (setq cont t)))
|
|
4144 injava-inher))
|
|
4145 )
|
0
|
4146 (cond
|
|
4147 ;; CASE 5C.1: non-hanging colon on an inher intro
|
|
4148 ((= char-after-ip ?:)
|
|
4149 (c-backward-syntactic-ws lim)
|
|
4150 (c-add-syntax 'inher-intro (c-point 'boi))
|
|
4151 ;; don't add inclass symbol since relative point already
|
|
4152 ;; contains any class offset
|
|
4153 )
|
|
4154 ;; CASE 5C.2: hanging colon on an inher intro
|
|
4155 ((= char-before-ip ?:)
|
|
4156 (c-add-syntax 'inher-intro (c-point 'boi))
|
|
4157 (and inclass-p (c-add-syntax 'inclass (aref inclass-p 0))))
|
32
|
4158 ;; CASE 5C.3: in a Java implements/extends
|
|
4159 (injava-inher
|
|
4160 (let ((where (cdr injava-inher))
|
|
4161 (cont (car injava-inher))
|
|
4162 (here (point)))
|
|
4163 (goto-char where)
|
|
4164 (cond ((looking-at "throws[^_]")
|
|
4165 (c-add-syntax 'func-decl-cont
|
|
4166 (progn (c-beginning-of-statement-1 lim)
|
|
4167 (c-point 'boi))))
|
|
4168 (cont (c-add-syntax 'inher-cont where))
|
|
4169 (t (c-add-syntax 'inher-intro
|
|
4170 (progn (goto-char (cdr injava-inher))
|
|
4171 (c-beginning-of-statement-1 lim)
|
|
4172 (point))))
|
|
4173 )))
|
|
4174 ;; CASE 5C.4: a continued inheritance line
|
0
|
4175 (t
|
|
4176 (c-beginning-of-inheritance-list lim)
|
|
4177 (c-add-syntax 'inher-cont (point))
|
|
4178 ;; don't add inclass symbol since relative point already
|
|
4179 ;; contains any class offset
|
|
4180 )))
|
|
4181 ;; CASE 5D: this could be a top-level compound statement or a
|
|
4182 ;; member init list continuation
|
|
4183 ((= char-before-ip ?,)
|
|
4184 (goto-char indent-point)
|
|
4185 (c-backward-syntactic-ws lim)
|
|
4186 (while (and (< lim (point))
|
|
4187 (= (preceding-char) ?,))
|
|
4188 ;; this will catch member inits with multiple
|
|
4189 ;; line arglists
|
|
4190 (forward-char -1)
|
|
4191 (c-backward-syntactic-ws (c-point 'bol))
|
|
4192 (if (= (preceding-char) ?\))
|
|
4193 (backward-sexp 1))
|
|
4194 ;; now continue checking
|
|
4195 (beginning-of-line)
|
|
4196 (c-backward-syntactic-ws lim))
|
|
4197 (cond
|
|
4198 ;; CASE 5D.1: hanging member init colon, but watch out
|
|
4199 ;; for bogus matches on access specifiers inside classes.
|
|
4200 ((and (= (preceding-char) ?:)
|
|
4201 (save-excursion
|
|
4202 (forward-word -1)
|
|
4203 (not (looking-at c-access-key))))
|
|
4204 (goto-char indent-point)
|
|
4205 (c-backward-syntactic-ws lim)
|
|
4206 (c-safe (backward-sexp 1))
|
|
4207 (c-add-syntax 'member-init-cont (c-point 'boi))
|
|
4208 ;; we do not need to add class offset since relative
|
|
4209 ;; point is the member init above us
|
|
4210 )
|
|
4211 ;; CASE 5D.2: non-hanging member init colon
|
|
4212 ((progn
|
|
4213 (c-forward-syntactic-ws indent-point)
|
|
4214 (= (following-char) ?:))
|
|
4215 (skip-chars-forward " \t:")
|
|
4216 (c-add-syntax 'member-init-cont (point)))
|
|
4217 ;; CASE 5D.3: perhaps a multiple inheritance line?
|
|
4218 ((looking-at c-inher-key)
|
|
4219 (c-add-syntax 'inher-cont (c-point 'boi)))
|
|
4220 ;; CASE 5D.4: perhaps a template list continuation?
|
|
4221 ((save-excursion
|
|
4222 (skip-chars-backward "^<" lim)
|
|
4223 ;; not sure if this is the right test, but it should
|
|
4224 ;; be fast and mostly accurate.
|
|
4225 (and (= (preceding-char) ?<)
|
|
4226 (not (c-in-literal lim))))
|
|
4227 ;; we can probably indent it just like and arglist-cont
|
|
4228 (c-add-syntax 'arglist-cont (point)))
|
|
4229 ;; CASE 5D.5: perhaps a top-level statement-cont
|
|
4230 (t
|
|
4231 (c-beginning-of-statement-1 lim)
|
|
4232 ;; skip over any access-specifiers
|
|
4233 (and inclass-p c-access-key
|
|
4234 (while (looking-at c-access-key)
|
|
4235 (forward-line 1)))
|
|
4236 ;; skip over comments, whitespace
|
|
4237 (c-forward-syntactic-ws indent-point)
|
|
4238 (c-add-syntax 'statement-cont (c-point 'boi)))
|
|
4239 ))
|
|
4240 ;; CASE 5E: we are looking at a access specifier
|
|
4241 ((and inclass-p
|
|
4242 c-access-key
|
|
4243 (looking-at c-access-key))
|
|
4244 (c-add-syntax 'access-label (c-point 'bonl))
|
|
4245 (c-add-syntax 'inclass (aref inclass-p 0)))
|
2
|
4246 ;; CASE 5F: extern-lang-close?
|
|
4247 ((and inextern-p
|
|
4248 (= char-after-ip ?}))
|
|
4249 (c-add-syntax 'extern-lang-close (aref inclass-p 1)))
|
|
4250 ;; CASE 5G: we are looking at the brace which closes the
|
0
|
4251 ;; enclosing nested class decl
|
|
4252 ((and inclass-p
|
|
4253 (= char-after-ip ?})
|
|
4254 (save-excursion
|
|
4255 (save-restriction
|
|
4256 (widen)
|
|
4257 (forward-char 1)
|
|
4258 (and
|
|
4259 (condition-case nil
|
|
4260 (progn (backward-sexp 1) t)
|
|
4261 (error nil))
|
|
4262 (= (point) (aref inclass-p 1))
|
|
4263 ))))
|
|
4264 (save-restriction
|
|
4265 (widen)
|
|
4266 (goto-char (aref inclass-p 0))
|
|
4267 (c-add-syntax 'class-close (c-point 'boi))))
|
2
|
4268 ;; CASE 5H: we could be looking at subsequent knr-argdecls
|
0
|
4269 ((and c-recognize-knr-p
|
2
|
4270 ;; here we essentially use the hack that is used in
|
|
4271 ;; Emacs' c-mode.el to limit how far back we should
|
|
4272 ;; look. The assumption is made that argdecls are
|
|
4273 ;; indented at least one space and that function
|
|
4274 ;; headers are not indented.
|
|
4275 (let ((limit (save-excursion
|
|
4276 (re-search-backward "^[^ \^L\t\n#]" nil 'move)
|
|
4277 (point))))
|
|
4278 (save-excursion
|
|
4279 (c-backward-syntactic-ws limit)
|
32
|
4280 (setq placeholder (point))
|
2
|
4281 (while (and (memq (preceding-char) '(?\; ?,))
|
|
4282 (> (point) limit))
|
|
4283 (beginning-of-line)
|
|
4284 (setq placeholder (point))
|
|
4285 (c-backward-syntactic-ws limit))
|
|
4286 (and (= (preceding-char) ?\))
|
|
4287 (or (not c-method-key)
|
|
4288 (progn
|
|
4289 (forward-sexp -1)
|
|
4290 (forward-char -1)
|
|
4291 (c-backward-syntactic-ws)
|
|
4292 (not (or (= (preceding-char) ?-)
|
|
4293 (= (preceding-char) ?+)
|
|
4294 ;; or a class category
|
|
4295 (progn
|
|
4296 (forward-sexp -2)
|
|
4297 (looking-at c-class-key))
|
|
4298 )))))
|
|
4299 ))
|
0
|
4300 (save-excursion
|
|
4301 (c-beginning-of-statement-1)
|
|
4302 (not (looking-at "typedef[ \t\n]+"))))
|
|
4303 (goto-char placeholder)
|
|
4304 (c-add-syntax 'knr-argdecl (c-point 'boi)))
|
2
|
4305 ;; CASE 5I: we are at the topmost level, make sure we skip
|
0
|
4306 ;; back past any access specifiers
|
|
4307 ((progn
|
|
4308 (c-backward-syntactic-ws lim)
|
|
4309 (while (and inclass-p
|
|
4310 c-access-key
|
|
4311 (not (bobp))
|
|
4312 (save-excursion
|
|
4313 (c-safe (progn (backward-sexp 1) t))
|
|
4314 (looking-at c-access-key)))
|
|
4315 (backward-sexp 1)
|
|
4316 (c-backward-syntactic-ws lim))
|
|
4317 (or (bobp)
|
|
4318 (memq (preceding-char) '(?\; ?\}))))
|
|
4319 ;; real beginning-of-line could be narrowed out due to
|
|
4320 ;; enclosure in a class block
|
|
4321 (save-restriction
|
|
4322 (widen)
|
|
4323 (c-add-syntax 'topmost-intro (c-point 'bol))
|
|
4324 (if inclass-p
|
|
4325 (progn
|
|
4326 (goto-char (aref inclass-p 1))
|
2
|
4327 (if inextern-p
|
|
4328 (c-add-syntax 'inextern-lang)
|
|
4329 (c-add-syntax 'inclass (c-point 'boi)))))
|
|
4330 ))
|
|
4331 ;; CASE 5J: we are at an ObjC or Java method definition
|
0
|
4332 ;; continuation line.
|
|
4333 ((and c-method-key
|
|
4334 (progn
|
|
4335 (c-beginning-of-statement-1 lim)
|
|
4336 (beginning-of-line)
|
|
4337 (looking-at c-method-key)))
|
|
4338 (c-add-syntax 'objc-method-args-cont (point)))
|
2
|
4339 ;; CASE 5K: we are at a topmost continuation line
|
0
|
4340 (t
|
|
4341 (c-beginning-of-statement-1 lim)
|
|
4342 (c-forward-syntactic-ws)
|
|
4343 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
|
|
4344 )) ; end CASE 5
|
|
4345 ;; CASE 6: line is an expression, not a statement. Most
|
|
4346 ;; likely we are either in a function prototype or a function
|
|
4347 ;; call argument list
|
|
4348 ((/= (char-after containing-sexp) ?{)
|
|
4349 (c-backward-syntactic-ws containing-sexp)
|
|
4350 (cond
|
|
4351 ;; CASE 6A: we are looking at the arglist closing paren or
|
|
4352 ;; at an Objective-C or Java method call closing bracket.
|
|
4353 ((and (/= char-before-ip ?,)
|
|
4354 (memq char-after-ip '(?\) ?\])))
|
|
4355 (if (and c-method-key
|
|
4356 (progn
|
|
4357 (goto-char (1- containing-sexp))
|
|
4358 (c-backward-syntactic-ws lim)
|
|
4359 (not (looking-at c-symbol-key))))
|
|
4360 (c-add-syntax 'statement-cont containing-sexp)
|
|
4361 (goto-char containing-sexp)
|
|
4362 (c-add-syntax 'arglist-close (c-point 'boi))))
|
|
4363 ;; CASE 6B: we are looking at the first argument in an empty
|
|
4364 ;; argument list. Use arglist-close if we're actually
|
|
4365 ;; looking at a close paren or bracket.
|
|
4366 ((memq char-before-ip '(?\( ?\[))
|
|
4367 (goto-char containing-sexp)
|
|
4368 (c-add-syntax 'arglist-intro (c-point 'boi)))
|
|
4369 ;; CASE 6C: we are inside a conditional test clause. treat
|
|
4370 ;; these things as statements
|
|
4371 ((save-excursion
|
|
4372 (goto-char containing-sexp)
|
|
4373 (and (c-safe (progn (forward-sexp -1) t))
|
|
4374 (looking-at "\\<for\\>")))
|
|
4375 (goto-char (1+ containing-sexp))
|
|
4376 (c-forward-syntactic-ws indent-point)
|
|
4377 (c-beginning-of-statement-1 containing-sexp)
|
|
4378 (if (= char-before-ip ?\;)
|
|
4379 (c-add-syntax 'statement (point))
|
|
4380 (c-add-syntax 'statement-cont (point))
|
|
4381 ))
|
|
4382 ;; CASE 6D: maybe a continued method call. This is the case
|
|
4383 ;; when we are inside a [] bracketed exp, and what precede
|
|
4384 ;; the opening bracket is not an identifier.
|
|
4385 ((and c-method-key
|
|
4386 (= (char-after containing-sexp) ?\[)
|
|
4387 (save-excursion
|
|
4388 (goto-char (1- containing-sexp))
|
|
4389 (c-backward-syntactic-ws (c-point 'bod))
|
|
4390 (if (not (looking-at c-symbol-key))
|
|
4391 (c-add-syntax 'objc-method-call-cont containing-sexp))
|
|
4392 )))
|
|
4393 ;; CASE 6E: we are looking at an arglist continuation line,
|
|
4394 ;; but the preceding argument is on the same line as the
|
|
4395 ;; opening paren. This case includes multi-line
|
|
4396 ;; mathematical paren groupings, but we could be on a
|
|
4397 ;; for-list continuation line
|
|
4398 ((and (save-excursion
|
|
4399 (goto-char (1+ containing-sexp))
|
|
4400 (skip-chars-forward " \t")
|
|
4401 (not (eolp)))
|
|
4402 (save-excursion
|
|
4403 (c-beginning-of-statement-1 lim)
|
|
4404 (skip-chars-backward " \t([")
|
|
4405 (<= (point) containing-sexp)))
|
|
4406 (goto-char containing-sexp)
|
|
4407 (c-add-syntax 'arglist-cont-nonempty (c-point 'boi)))
|
|
4408 ;; CASE 6F: we are looking at just a normal arglist
|
|
4409 ;; continuation line
|
|
4410 (t (c-beginning-of-statement-1 containing-sexp)
|
|
4411 (forward-char 1)
|
|
4412 (c-forward-syntactic-ws indent-point)
|
|
4413 (c-add-syntax 'arglist-cont (c-point 'boi)))
|
|
4414 ))
|
|
4415 ;; CASE 7: func-local multi-inheritance line
|
|
4416 ((and c-baseclass-key
|
|
4417 (save-excursion
|
|
4418 (goto-char indent-point)
|
|
4419 (skip-chars-forward " \t")
|
|
4420 (looking-at c-baseclass-key)))
|
|
4421 (goto-char indent-point)
|
|
4422 (skip-chars-forward " \t")
|
|
4423 (cond
|
|
4424 ;; CASE 7A: non-hanging colon on an inher intro
|
|
4425 ((= char-after-ip ?:)
|
|
4426 (c-backward-syntactic-ws lim)
|
|
4427 (c-add-syntax 'inher-intro (c-point 'boi)))
|
|
4428 ;; CASE 7B: hanging colon on an inher intro
|
|
4429 ((= char-before-ip ?:)
|
|
4430 (c-add-syntax 'inher-intro (c-point 'boi)))
|
|
4431 ;; CASE 7C: a continued inheritance line
|
|
4432 (t
|
|
4433 (c-beginning-of-inheritance-list lim)
|
|
4434 (c-add-syntax 'inher-cont (point))
|
|
4435 )))
|
|
4436 ;; CASE 8: we are inside a brace-list
|
|
4437 ((setq placeholder (c-inside-bracelist-p containing-sexp state))
|
|
4438 (cond
|
|
4439 ;; CASE 8A: brace-list-close brace
|
|
4440 ((and (= char-after-ip ?})
|
|
4441 (c-safe (progn (forward-char 1)
|
|
4442 (backward-sexp 1)
|
|
4443 t))
|
|
4444 (= (point) containing-sexp))
|
|
4445 (c-add-syntax 'brace-list-close (c-point 'boi)))
|
|
4446 ;; CASE 8B: we're looking at the first line in a brace-list
|
|
4447 ((save-excursion
|
|
4448 (goto-char indent-point)
|
|
4449 (c-backward-syntactic-ws containing-sexp)
|
|
4450 (= (point) (1+ containing-sexp)))
|
|
4451 (goto-char containing-sexp)
|
|
4452 ;;(if (= char-after-ip ?{)
|
|
4453 ;;(c-add-syntax 'brace-list-open (c-point 'boi))
|
|
4454 (c-add-syntax 'brace-list-intro (c-point 'boi))
|
|
4455 )
|
|
4456 ;;)) ; end CASE 8B
|
|
4457 ;; CASE 8C: this is just a later brace-list-entry
|
|
4458 (t (goto-char (1+ containing-sexp))
|
|
4459 (c-forward-syntactic-ws indent-point)
|
|
4460 (if (= char-after-ip ?{)
|
|
4461 (c-add-syntax 'brace-list-open (point))
|
|
4462 (c-add-syntax 'brace-list-entry (point))
|
|
4463 )) ; end CASE 8C
|
|
4464 )) ; end CASE 8
|
|
4465 ;; CASE 9: A continued statement
|
|
4466 ((and (not (memq char-before-ip '(?\; ?} ?:)))
|
|
4467 (> (point)
|
|
4468 (save-excursion
|
|
4469 (c-beginning-of-statement-1 containing-sexp)
|
|
4470 (setq placeholder (point))))
|
|
4471 (/= placeholder containing-sexp))
|
|
4472 (goto-char indent-point)
|
|
4473 (skip-chars-forward " \t")
|
|
4474 (let ((after-cond-placeholder
|
|
4475 (save-excursion
|
|
4476 (goto-char placeholder)
|
|
4477 (if (looking-at c-conditional-key)
|
|
4478 (progn
|
|
4479 (c-safe (c-skip-conditional))
|
|
4480 (c-forward-syntactic-ws)
|
|
4481 (if (memq (following-char) '(?\;))
|
|
4482 (progn
|
|
4483 (forward-char 1)
|
|
4484 (c-forward-syntactic-ws)))
|
|
4485 (point))
|
|
4486 nil))))
|
|
4487 (cond
|
|
4488 ;; CASE 9A: substatement
|
|
4489 ((and after-cond-placeholder
|
|
4490 (>= after-cond-placeholder indent-point))
|
|
4491 (goto-char placeholder)
|
|
4492 (if (= char-after-ip ?{)
|
|
4493 (c-add-syntax 'substatement-open (c-point 'boi))
|
|
4494 (c-add-syntax 'substatement (c-point 'boi))))
|
|
4495 ;; CASE 9B: open braces for class or brace-lists
|
|
4496 ((= char-after-ip ?{)
|
|
4497 (cond
|
|
4498 ;; CASE 9B.1: class-open
|
|
4499 ((save-excursion
|
|
4500 (goto-char indent-point)
|
|
4501 (skip-chars-forward " \t{")
|
|
4502 (let ((decl (c-search-uplist-for-classkey (c-parse-state))))
|
|
4503 (and decl
|
|
4504 (setq placeholder (aref decl 0)))
|
|
4505 ))
|
|
4506 (c-add-syntax 'class-open placeholder))
|
|
4507 ;; CASE 9B.2: brace-list-open
|
|
4508 ((or (save-excursion
|
|
4509 (goto-char placeholder)
|
|
4510 (looking-at "\\<enum\\>"))
|
|
4511 (= char-before-ip ?=))
|
|
4512 (c-add-syntax 'brace-list-open placeholder))
|
|
4513 ;; CASE 9B.3: catch-all for unknown construct.
|
|
4514 (t
|
|
4515 ;; Can and should I add an extensibility hook here?
|
|
4516 ;; Something like c-recognize-hook so support for
|
|
4517 ;; unknown constructs could be added. It's probably a
|
|
4518 ;; losing proposition, so I dunno.
|
|
4519 (goto-char placeholder)
|
|
4520 (c-add-syntax 'statement-cont (c-point 'boi))
|
|
4521 (c-add-syntax 'block-open))
|
|
4522 ))
|
|
4523 ;; CASE 9C: iostream insertion or extraction operator
|
|
4524 ((looking-at "<<\\|>>")
|
|
4525 (goto-char placeholder)
|
|
4526 (and after-cond-placeholder
|
|
4527 (goto-char after-cond-placeholder))
|
|
4528 (while (and (re-search-forward "<<\\|>>" indent-point 'move)
|
|
4529 (c-in-literal placeholder)))
|
|
4530 ;; if we ended up at indent-point, then the first
|
|
4531 ;; streamop is on a separate line. Indent the line like
|
|
4532 ;; a statement-cont instead
|
|
4533 (if (/= (point) indent-point)
|
|
4534 (c-add-syntax 'stream-op (c-point 'boi))
|
|
4535 (c-backward-syntactic-ws lim)
|
|
4536 (c-add-syntax 'statement-cont (c-point 'boi))))
|
|
4537 ;; CASE 9D: continued statement. find the accurate
|
|
4538 ;; beginning of statement or substatement
|
|
4539 (t
|
|
4540 (c-beginning-of-statement-1 after-cond-placeholder)
|
|
4541 ;; KLUDGE ALERT! c-beginning-of-statement-1 can leave
|
|
4542 ;; us before the lim we're passing in. It should be
|
|
4543 ;; fixed, but I'm worried about side-effects at this
|
|
4544 ;; late date. Fix for v5.
|
|
4545 (goto-char (or (and after-cond-placeholder
|
|
4546 (max after-cond-placeholder (point)))
|
|
4547 (point)))
|
|
4548 (c-add-syntax 'statement-cont (point)))
|
|
4549 )))
|
|
4550 ;; CASE 10: an else clause?
|
|
4551 ((looking-at "\\<else\\>[^_]")
|
|
4552 (c-backward-to-start-of-if containing-sexp)
|
|
4553 (c-add-syntax 'else-clause (c-point 'boi)))
|
|
4554 ;; CASE 11: Statement. But what kind? Lets see if its a
|
|
4555 ;; while closure of a do/while construct
|
|
4556 ((progn
|
|
4557 (goto-char indent-point)
|
|
4558 (skip-chars-forward " \t")
|
|
4559 (and (looking-at "while\\b[^_]")
|
|
4560 (save-excursion
|
|
4561 (c-backward-to-start-of-do containing-sexp)
|
|
4562 (setq placeholder (point))
|
|
4563 (looking-at "do\\b[^_]"))
|
|
4564 ))
|
|
4565 (c-add-syntax 'do-while-closure placeholder))
|
|
4566 ;; CASE 12: A case or default label
|
|
4567 ((looking-at c-switch-label-key)
|
|
4568 (goto-char containing-sexp)
|
|
4569 ;; check for hanging braces
|
|
4570 (if (/= (point) (c-point 'boi))
|
|
4571 (forward-sexp -1))
|
|
4572 (c-add-syntax 'case-label (c-point 'boi)))
|
|
4573 ;; CASE 13: any other label
|
|
4574 ((looking-at c-label-key)
|
|
4575 (goto-char containing-sexp)
|
|
4576 (c-add-syntax 'label (c-point 'boi)))
|
|
4577 ;; CASE 14: block close brace, possibly closing the defun or
|
|
4578 ;; the class
|
|
4579 ((= char-after-ip ?})
|
|
4580 (let* ((lim (c-safe-position containing-sexp fullstate))
|
|
4581 (relpos (save-excursion
|
|
4582 (goto-char containing-sexp)
|
|
4583 (if (/= (point) (c-point 'boi))
|
|
4584 (c-beginning-of-statement-1 lim))
|
|
4585 (c-point 'boi))))
|
|
4586 (cond
|
|
4587 ;; CASE 14A: does this close an inline?
|
2
|
4588 ((let ((inclass-p (progn
|
|
4589 (goto-char containing-sexp)
|
|
4590 (c-search-uplist-for-classkey state))))
|
|
4591 ;; inextern-p in higher level let*
|
|
4592 (setq inextern-p (and inclass-p
|
|
4593 (progn
|
|
4594 (goto-char (aref inclass-p 0))
|
|
4595 (looking-at "extern[^_]"))))
|
|
4596 (and inclass-p (not inextern-p)))
|
0
|
4597 (c-add-syntax 'inline-close relpos))
|
|
4598 ;; CASE 14B: if there an enclosing brace that hasn't
|
|
4599 ;; been narrowed out by a class, then this is a
|
|
4600 ;; block-close
|
2
|
4601 ((and (not inextern-p)
|
|
4602 (c-most-enclosing-brace state))
|
0
|
4603 (c-add-syntax 'block-close relpos))
|
|
4604 ;; CASE 14C: find out whether we're closing a top-level
|
|
4605 ;; class or a defun
|
|
4606 (t
|
|
4607 (save-restriction
|
|
4608 (narrow-to-region (point-min) indent-point)
|
|
4609 (let ((decl (c-search-uplist-for-classkey (c-parse-state))))
|
|
4610 (if decl
|
|
4611 (c-add-syntax 'class-close (aref decl 0))
|
|
4612 (c-add-syntax 'defun-close relpos)))))
|
|
4613 )))
|
|
4614 ;; CASE 15: statement catchall
|
|
4615 (t
|
|
4616 ;; we know its a statement, but we need to find out if it is
|
|
4617 ;; the first statement in a block
|
|
4618 (goto-char containing-sexp)
|
|
4619 (forward-char 1)
|
|
4620 (c-forward-syntactic-ws indent-point)
|
|
4621 ;; now skip forward past any case/default clauses we might find.
|
|
4622 (while (or (c-skip-case-statement-forward fullstate indent-point)
|
|
4623 (and (looking-at c-switch-label-key)
|
|
4624 (not inswitch-p)))
|
|
4625 (setq inswitch-p t))
|
|
4626 ;; we want to ignore non-case labels when skipping forward
|
|
4627 (while (and (looking-at c-label-key)
|
|
4628 (goto-char (match-end 0)))
|
|
4629 (c-forward-syntactic-ws indent-point))
|
|
4630 (cond
|
|
4631 ;; CASE 15A: we are inside a case/default clause inside a
|
|
4632 ;; switch statement. find out if we are at the statement
|
|
4633 ;; just after the case/default label.
|
|
4634 ((and inswitch-p
|
|
4635 (progn
|
|
4636 (goto-char indent-point)
|
|
4637 (c-backward-syntactic-ws containing-sexp)
|
|
4638 (back-to-indentation)
|
|
4639 (setq placeholder (point))
|
|
4640 (looking-at c-switch-label-key)))
|
|
4641 (goto-char indent-point)
|
|
4642 (skip-chars-forward " \t")
|
|
4643 (if (= (following-char) ?{)
|
|
4644 (c-add-syntax 'statement-case-open placeholder)
|
|
4645 (c-add-syntax 'statement-case-intro placeholder)))
|
|
4646 ;; CASE 15B: continued statement
|
|
4647 ((= char-before-ip ?,)
|
|
4648 (c-add-syntax 'statement-cont (c-point 'boi)))
|
|
4649 ;; CASE 15C: a question/colon construct? But make sure
|
|
4650 ;; what came before was not a label, and what comes after
|
|
4651 ;; is not a globally scoped function call!
|
|
4652 ((or (and (memq char-before-ip '(?: ??))
|
|
4653 (save-excursion
|
|
4654 (goto-char indent-point)
|
|
4655 (c-backward-syntactic-ws lim)
|
|
4656 (back-to-indentation)
|
|
4657 (not (looking-at c-label-key))))
|
|
4658 (and (memq char-after-ip '(?: ??))
|
|
4659 (save-excursion
|
|
4660 (goto-char indent-point)
|
|
4661 (skip-chars-forward " \t")
|
|
4662 ;; watch out for scope operator
|
|
4663 (not (looking-at "::")))))
|
|
4664 (c-add-syntax 'statement-cont (c-point 'boi)))
|
|
4665 ;; CASE 15D: any old statement
|
|
4666 ((< (point) indent-point)
|
32
|
4667 (let ((safepos (c-most-enclosing-brace fullstate))
|
|
4668 relpos done)
|
0
|
4669 (goto-char indent-point)
|
|
4670 (c-beginning-of-statement-1 safepos)
|
|
4671 ;; It is possible we're on the brace that opens a nested
|
|
4672 ;; function.
|
|
4673 (if (and (= (following-char) ?{)
|
|
4674 (save-excursion
|
|
4675 (c-backward-syntactic-ws safepos)
|
|
4676 (/= (preceding-char) ?\;)))
|
|
4677 (c-beginning-of-statement-1 safepos))
|
32
|
4678 (if (and inswitch-p
|
|
4679 (looking-at c-switch-label-key))
|
|
4680 (progn
|
|
4681 (goto-char placeholder)
|
|
4682 (end-of-line)
|
|
4683 (forward-sexp -1)))
|
|
4684 (setq relpos (c-point 'boi))
|
|
4685 (while (and (not done)
|
|
4686 (<= safepos (point))
|
|
4687 (/= relpos (point)))
|
|
4688 (c-beginning-of-statement-1 safepos)
|
|
4689 (if (= relpos (c-point 'boi))
|
|
4690 (setq done t))
|
|
4691 (setq relpos (c-point 'boi)))
|
|
4692 (c-add-syntax 'statement relpos)
|
0
|
4693 (if (= char-after-ip ?{)
|
|
4694 (c-add-syntax 'block-open))))
|
|
4695 ;; CASE 15E: first statement in an inline, or first
|
|
4696 ;; statement in a top-level defun. we can tell this is it
|
|
4697 ;; if there are no enclosing braces that haven't been
|
|
4698 ;; narrowed out by a class (i.e. don't use bod here!)
|
|
4699 ((save-excursion
|
|
4700 (save-restriction
|
|
4701 (widen)
|
|
4702 (goto-char containing-sexp)
|
|
4703 (c-narrow-out-enclosing-class state containing-sexp)
|
|
4704 (not (c-most-enclosing-brace state))))
|
|
4705 (goto-char containing-sexp)
|
|
4706 ;; if not at boi, then defun-opening braces are hung on
|
|
4707 ;; right side, so we need a different relpos
|
|
4708 (if (/= (point) (c-point 'boi))
|
|
4709 (progn
|
|
4710 (c-backward-syntactic-ws)
|
|
4711 (c-safe (forward-sexp (if (= (preceding-char) ?\))
|
|
4712 -1 -2)))
|
|
4713 ))
|
|
4714 (c-add-syntax 'defun-block-intro (c-point 'boi)))
|
|
4715 ;; CASE 15F: first statement in a block
|
|
4716 (t (goto-char containing-sexp)
|
|
4717 (if (/= (point) (c-point 'boi))
|
|
4718 (c-beginning-of-statement-1
|
|
4719 (if (= (point) lim)
|
|
4720 (c-safe-position (point) state) lim)))
|
|
4721 (c-add-syntax 'statement-block-intro (c-point 'boi))
|
|
4722 (if (= char-after-ip ?{)
|
|
4723 (c-add-syntax 'block-open)))
|
|
4724 ))
|
|
4725 )
|
|
4726
|
|
4727 ;; now we need to look at any modifiers
|
|
4728 (goto-char indent-point)
|
|
4729 (skip-chars-forward " \t")
|
|
4730 ;; are we looking at a comment only line?
|
|
4731 (if (looking-at c-comment-start-regexp)
|
|
4732 (c-add-syntax 'comment-intro))
|
|
4733 ;; we might want to give additional offset to friends (in C++).
|
|
4734 (if (and (eq major-mode 'c++-mode)
|
|
4735 (looking-at c-C++-friend-key))
|
|
4736 (c-add-syntax 'friend))
|
|
4737 ;; return the syntax
|
|
4738 syntax))))
|
|
4739
|
|
4740
|
|
4741 ;; indent via syntactic language elements
|
|
4742 (defun c-get-offset (langelem)
|
|
4743 ;; Get offset from LANGELEM which is a cons cell of the form:
|
|
4744 ;; (SYMBOL . RELPOS). The symbol is matched against
|
|
4745 ;; c-offsets-alist and the offset found there is either returned,
|
|
4746 ;; or added to the indentation at RELPOS. If RELPOS is nil, then
|
|
4747 ;; the offset is simply returned.
|
|
4748 (let* ((symbol (car langelem))
|
|
4749 (relpos (cdr langelem))
|
|
4750 (match (assq symbol c-offsets-alist))
|
|
4751 (offset (cdr-safe match)))
|
|
4752 ;; offset can be a number, a function, a variable, or one of the
|
|
4753 ;; symbols + or -
|
|
4754 (cond
|
|
4755 ((not match)
|
|
4756 (if c-strict-syntax-p
|
|
4757 (error "don't know how to indent a %s" symbol)
|
|
4758 (setq offset 0
|
|
4759 relpos 0)))
|
2
|
4760 ((eq offset '+) (setq offset c-basic-offset))
|
|
4761 ((eq offset '-) (setq offset (- c-basic-offset)))
|
|
4762 ((eq offset '++) (setq offset (* 2 c-basic-offset)))
|
|
4763 ((eq offset '--) (setq offset (* 2 (- c-basic-offset))))
|
|
4764 ((eq offset '*) (setq offset (/ c-basic-offset 2)))
|
|
4765 ((eq offset '/) (setq offset (/ (- c-basic-offset) 2)))
|
|
4766 ((c-functionp offset) (setq offset (funcall offset langelem)))
|
|
4767 ((not (numberp offset)) (setq offset (symbol-value offset)))
|
0
|
4768 )
|
|
4769 (+ (if (and relpos
|
|
4770 (< relpos (c-point 'bol)))
|
|
4771 (save-excursion
|
|
4772 (goto-char relpos)
|
|
4773 (current-column))
|
|
4774 0)
|
|
4775 offset)))
|
|
4776
|
|
4777 (defun c-indent-line (&optional syntax)
|
|
4778 ;; indent the current line as C/C++/ObjC code. Optional SYNTAX is the
|
|
4779 ;; syntactic information for the current line. Returns the amount of
|
|
4780 ;; indentation change
|
|
4781 (let* ((c-syntactic-context (or syntax (c-guess-basic-syntax)))
|
|
4782 (pos (- (point-max) (point)))
|
|
4783 (indent (apply '+ (mapcar 'c-get-offset c-syntactic-context)))
|
|
4784 (shift-amt (- (current-indentation) indent)))
|
|
4785 (and c-echo-syntactic-information-p
|
|
4786 (message "syntax: %s, indent= %d" c-syntactic-context indent))
|
|
4787 (if (zerop shift-amt)
|
|
4788 nil
|
|
4789 (delete-region (c-point 'bol) (c-point 'boi))
|
|
4790 (beginning-of-line)
|
|
4791 (indent-to indent))
|
|
4792 (if (< (point) (c-point 'boi))
|
|
4793 (back-to-indentation)
|
|
4794 ;; If initial point was within line's indentation, position after
|
|
4795 ;; the indentation. Else stay at same point in text.
|
|
4796 (if (> (- (point-max) pos) (point))
|
|
4797 (goto-char (- (point-max) pos)))
|
|
4798 )
|
|
4799 (run-hooks 'c-special-indent-hook)
|
|
4800 shift-amt))
|
|
4801
|
2
|
4802 (defun c-show-syntactic-information (arg)
|
|
4803 "Show syntactic information for current line.
|
|
4804 With universal argument, inserts the analysis as a comment on that line."
|
|
4805 (interactive "P")
|
|
4806 (let ((syntax (c-guess-basic-syntax)))
|
|
4807 (if (not (consp arg))
|
|
4808 (message "syntactic analysis: %s" (c-guess-basic-syntax))
|
|
4809 (indent-for-comment)
|
|
4810 (insert (format "%s" syntax))
|
|
4811 ))
|
0
|
4812 (c-keep-region-active))
|
|
4813
|
|
4814
|
|
4815 ;; Standard indentation line-ups
|
|
4816 (defun c-lineup-arglist (langelem)
|
|
4817 ;; lineup the current arglist line with the arglist appearing just
|
|
4818 ;; after the containing paren which starts the arglist.
|
|
4819 (save-excursion
|
|
4820 (let* ((containing-sexp
|
|
4821 (save-excursion
|
|
4822 ;; arglist-cont-nonempty gives relpos ==
|
|
4823 ;; to boi of containing-sexp paren. This
|
|
4824 ;; is good when offset is +, but bad
|
|
4825 ;; when it is c-lineup-arglist, so we
|
|
4826 ;; have to special case a kludge here.
|
|
4827 (if (memq (car langelem) '(arglist-intro arglist-cont-nonempty))
|
|
4828 (progn
|
|
4829 (beginning-of-line)
|
|
4830 (backward-up-list 1)
|
|
4831 (skip-chars-forward " \t" (c-point 'eol)))
|
|
4832 (goto-char (cdr langelem)))
|
|
4833 (point)))
|
|
4834 (cs-curcol (save-excursion
|
|
4835 (goto-char (cdr langelem))
|
|
4836 (current-column))))
|
|
4837 (if (save-excursion
|
|
4838 (beginning-of-line)
|
|
4839 (looking-at "[ \t]*)"))
|
|
4840 (progn (goto-char (match-end 0))
|
|
4841 (forward-sexp -1)
|
|
4842 (forward-char 1)
|
|
4843 (c-forward-syntactic-ws)
|
|
4844 (- (current-column) cs-curcol))
|
|
4845 (goto-char containing-sexp)
|
|
4846 (or (eolp)
|
|
4847 (not (memq (following-char) '(?{ ?\( )))
|
|
4848 (let ((eol (c-point 'eol))
|
|
4849 (here (progn
|
|
4850 (forward-char 1)
|
|
4851 (skip-chars-forward " \t")
|
|
4852 (point))))
|
|
4853 (c-forward-syntactic-ws)
|
|
4854 (if (< (point) eol)
|
|
4855 (goto-char here))))
|
|
4856 (- (current-column) cs-curcol)
|
|
4857 ))))
|
|
4858
|
|
4859 (defun c-lineup-arglist-intro-after-paren (langelem)
|
|
4860 ;; lineup an arglist-intro line to just after the open paren
|
|
4861 (save-excursion
|
|
4862 (let ((cs-curcol (save-excursion
|
|
4863 (goto-char (cdr langelem))
|
|
4864 (current-column)))
|
|
4865 (ce-curcol (save-excursion
|
|
4866 (beginning-of-line)
|
|
4867 (backward-up-list 1)
|
|
4868 (skip-chars-forward " \t" (c-point 'eol))
|
|
4869 (current-column))))
|
|
4870 (- ce-curcol cs-curcol -1))))
|
|
4871
|
2
|
4872 (defun c-lineup-arglist-close-under-paren (langelem)
|
|
4873 ;; lineup an arglist-intro line to just after the open paren
|
|
4874 (save-excursion
|
|
4875 (let ((cs-curcol (save-excursion
|
|
4876 (goto-char (cdr langelem))
|
|
4877 (current-column)))
|
|
4878 (ce-curcol (save-excursion
|
|
4879 (beginning-of-line)
|
|
4880 (backward-up-list 1)
|
|
4881 (current-column))))
|
|
4882 (- ce-curcol cs-curcol))))
|
|
4883
|
0
|
4884 (defun c-lineup-streamop (langelem)
|
|
4885 ;; lineup stream operators
|
|
4886 (save-excursion
|
|
4887 (let* ((relpos (cdr langelem))
|
|
4888 (curcol (progn (goto-char relpos)
|
|
4889 (current-column))))
|
|
4890 (re-search-forward "<<\\|>>" (c-point 'eol) 'move)
|
|
4891 (goto-char (match-beginning 0))
|
|
4892 (- (current-column) curcol))))
|
|
4893
|
|
4894 (defun c-lineup-multi-inher (langelem)
|
|
4895 ;; line up multiple inheritance lines
|
|
4896 (save-excursion
|
|
4897 (let (cs-curcol
|
|
4898 (eol (c-point 'eol))
|
|
4899 (here (point)))
|
|
4900 (goto-char (cdr langelem))
|
|
4901 (setq cs-curcol (current-column))
|
|
4902 (skip-chars-forward "^:" eol)
|
|
4903 (skip-chars-forward " \t:" eol)
|
|
4904 (if (or (eolp)
|
|
4905 (looking-at c-comment-start-regexp))
|
|
4906 (c-forward-syntactic-ws here))
|
|
4907 (- (current-column) cs-curcol)
|
|
4908 )))
|
|
4909
|
32
|
4910 (defun c-lineup-java-inher (langelem)
|
|
4911 ;; line up Java implements and extends continuations
|
|
4912 (save-excursion
|
|
4913 (let ((cs-curcol (progn (goto-char (cdr langelem))
|
|
4914 (current-column))))
|
|
4915 (forward-word 1)
|
|
4916 (if (looking-at "[ \t]*$")
|
|
4917 cs-curcol
|
|
4918 (c-forward-syntactic-ws)
|
|
4919 (- (current-column) cs-curcol)))))
|
|
4920
|
0
|
4921 (defun c-lineup-C-comments (langelem)
|
|
4922 ;; line up C block comment continuation lines
|
|
4923 (save-excursion
|
32
|
4924 (let ((here (point))
|
|
4925 (stars (progn (back-to-indentation)
|
|
4926 (skip-chars-forward "*")))
|
0
|
4927 (cs-curcol (progn (goto-char (cdr langelem))
|
|
4928 (current-column))))
|
|
4929 (back-to-indentation)
|
32
|
4930 (if (not (re-search-forward "/[*]+" (c-point 'eol) t))
|
|
4931 (progn
|
|
4932 (if (not (looking-at "[*]+"))
|
|
4933 (progn
|
|
4934 ;; we now have to figure out where this comment begins.
|
|
4935 (goto-char here)
|
|
4936 (back-to-indentation)
|
40
|
4937 (if (looking-at "[*]+/")
|
|
4938 (progn (goto-char (match-end 0))
|
|
4939 (forward-comment -1))
|
32
|
4940 (goto-char (cdr langelem))
|
|
4941 (back-to-indentation))))
|
|
4942 (- (current-column) cs-curcol))
|
|
4943 (if (zerop stars)
|
|
4944 (skip-chars-forward " \t"))
|
|
4945 (- (current-column) stars cs-curcol))
|
|
4946 )))
|
0
|
4947
|
|
4948 (defun c-lineup-comment (langelem)
|
|
4949 ;; support old behavior for comment indentation. we look at
|
|
4950 ;; c-comment-only-line-offset to decide how to indent comment
|
|
4951 ;; only-lines
|
|
4952 (save-excursion
|
|
4953 (back-to-indentation)
|
2
|
4954 ;; this highly kludgiforous flag prevents the mapcar over
|
|
4955 ;; c-syntactic-context from entering an infinite loop
|
|
4956 (let ((recurse-prevention-flag (boundp 'recurse-prevention-flag)))
|
|
4957 (cond
|
|
4958 ;; CASE 1: preserve comment-column
|
|
4959 (recurse-prevention-flag 0)
|
|
4960 ((= (current-column) comment-column)
|
|
4961 ;; we have to subtract out all other indentation
|
|
4962 (- comment-column (apply '+ (mapcar 'c-get-offset
|
|
4963 c-syntactic-context))))
|
|
4964 ;; indent as specified by c-comment-only-line-offset
|
|
4965 ((not (bolp))
|
0
|
4966 (or (car-safe c-comment-only-line-offset)
|
2
|
4967 c-comment-only-line-offset))
|
|
4968 (t
|
|
4969 (or (cdr-safe c-comment-only-line-offset)
|
|
4970 (car-safe c-comment-only-line-offset)
|
|
4971 -1000)) ;jam it against the left side
|
|
4972 ))))
|
0
|
4973
|
|
4974 (defun c-lineup-runin-statements (langelem)
|
|
4975 ;; line up statements in coding standards which place the first
|
|
4976 ;; statement on the same line as the block opening brace.
|
|
4977 (if (= (char-after (cdr langelem)) ?{)
|
|
4978 (save-excursion
|
|
4979 (let ((curcol (progn
|
|
4980 (goto-char (cdr langelem))
|
|
4981 (current-column))))
|
|
4982 (forward-char 1)
|
|
4983 (skip-chars-forward " \t")
|
|
4984 (- (current-column) curcol)))
|
|
4985 0))
|
|
4986
|
|
4987 (defun c-lineup-math (langelem)
|
|
4988 ;; line up math statement-cont after the equals
|
|
4989 (save-excursion
|
|
4990 (let* ((relpos (cdr langelem))
|
|
4991 (equalp (save-excursion
|
|
4992 (goto-char (c-point 'boi))
|
|
4993 (skip-chars-forward "^=" (c-point 'eol))
|
|
4994 (and (= (following-char) ?=)
|
|
4995 (- (point) (c-point 'boi)))))
|
|
4996 (curcol (progn
|
|
4997 (goto-char relpos)
|
|
4998 (current-column)))
|
|
4999 donep)
|
|
5000 (while (and (not donep)
|
|
5001 (< (point) (c-point 'eol)))
|
|
5002 (skip-chars-forward "^=" (c-point 'eol))
|
|
5003 (if (c-in-literal (cdr langelem))
|
|
5004 (forward-char 1)
|
|
5005 (setq donep t)))
|
|
5006 (if (/= (following-char) ?=)
|
|
5007 ;; there's no equal sign on the line
|
|
5008 c-basic-offset
|
|
5009 ;; calculate indentation column after equals and ws, unless
|
|
5010 ;; our line contains an equals sign
|
|
5011 (if (not equalp)
|
|
5012 (progn
|
|
5013 (forward-char 1)
|
|
5014 (skip-chars-forward " \t")
|
|
5015 (setq equalp 0)))
|
|
5016 (- (current-column) equalp curcol))
|
|
5017 )))
|
|
5018
|
|
5019 (defun c-lineup-ObjC-method-call (langelem)
|
|
5020 ;; Line up methods args as elisp-mode does with function args: go to
|
|
5021 ;; the position right after the message receiver, and if you are at
|
|
5022 ;; (eolp) indent the current line by a constant offset from the
|
|
5023 ;; opening bracket; otherwise we are looking at the first character
|
|
5024 ;; of the first method call argument, so lineup the current line
|
|
5025 ;; with it.
|
|
5026 (save-excursion
|
|
5027 (let* ((extra (save-excursion
|
|
5028 (back-to-indentation)
|
|
5029 (c-backward-syntactic-ws (cdr langelem))
|
|
5030 (if (= (preceding-char) ?:)
|
|
5031 (- c-basic-offset)
|
|
5032 0)))
|
|
5033 (open-bracket-pos (cdr langelem))
|
|
5034 (open-bracket-col (progn
|
|
5035 (goto-char open-bracket-pos)
|
|
5036 (current-column)))
|
|
5037 (target-col (progn
|
|
5038 (forward-char)
|
|
5039 (forward-sexp)
|
|
5040 (skip-chars-forward " \t")
|
|
5041 (if (eolp)
|
|
5042 (+ open-bracket-col c-basic-offset)
|
|
5043 (current-column))))
|
|
5044 )
|
|
5045 (- target-col open-bracket-col extra))))
|
|
5046
|
|
5047 (defun c-lineup-ObjC-method-args (langelem)
|
|
5048 ;; Line up the colons that separate args. This is done trying to
|
|
5049 ;; align colons vertically.
|
|
5050 (save-excursion
|
|
5051 (let* ((here (c-point 'boi))
|
|
5052 (curcol (progn (goto-char here) (current-column)))
|
|
5053 (eol (c-point 'eol))
|
|
5054 (relpos (cdr langelem))
|
|
5055 (first-col-column (progn
|
|
5056 (goto-char relpos)
|
|
5057 (skip-chars-forward "^:" eol)
|
|
5058 (and (= (following-char) ?:)
|
|
5059 (current-column)))))
|
|
5060 (if (not first-col-column)
|
|
5061 c-basic-offset
|
|
5062 (goto-char here)
|
|
5063 (skip-chars-forward "^:" eol)
|
|
5064 (if (= (following-char) ?:)
|
|
5065 (+ curcol (- first-col-column (current-column)))
|
|
5066 c-basic-offset)))))
|
|
5067
|
|
5068 (defun c-lineup-ObjC-method-args-2 (langelem)
|
|
5069 ;; Line up the colons that separate args. This is done trying to
|
|
5070 ;; align the colon on the current line with the previous one.
|
|
5071 (save-excursion
|
|
5072 (let* ((here (c-point 'boi))
|
|
5073 (curcol (progn (goto-char here) (current-column)))
|
|
5074 (eol (c-point 'eol))
|
|
5075 (relpos (cdr langelem))
|
|
5076 (prev-col-column (progn
|
|
5077 (skip-chars-backward "^:" relpos)
|
|
5078 (and (= (preceding-char) ?:)
|
|
5079 (- (current-column) 1)))))
|
|
5080 (if (not prev-col-column)
|
|
5081 c-basic-offset
|
|
5082 (goto-char here)
|
|
5083 (skip-chars-forward "^:" eol)
|
|
5084 (if (= (following-char) ?:)
|
|
5085 (+ curcol (- prev-col-column (current-column)))
|
|
5086 c-basic-offset)))))
|
|
5087
|
|
5088 (defun c-snug-do-while (syntax pos)
|
|
5089 "Dynamically calculate brace hanginess for do-while statements.
|
|
5090 Using this function, `while' clauses that end a `do-while' block will
|
|
5091 remain on the same line as the brace that closes that block.
|
|
5092
|
|
5093 See `c-hanging-braces-alist' for how to utilize this function as an
|
|
5094 ACTION associated with `block-close' syntax."
|
|
5095 (save-excursion
|
|
5096 (let (langelem)
|
|
5097 (if (and (eq syntax 'block-close)
|
|
5098 (setq langelem (assq 'block-close c-syntactic-context))
|
|
5099 (progn (goto-char (cdr langelem))
|
|
5100 (if (= (following-char) ?{)
|
|
5101 (c-safe (forward-sexp -1)))
|
|
5102 (looking-at "\\<do\\>[^_]")))
|
|
5103 '(before)
|
|
5104 '(before after)))))
|
|
5105
|
2
|
5106 (defun c-gnu-impose-minimum ()
|
32
|
5107 "Imposes a minimum indentation for lines inside a top-level construct.
|
2
|
5108 The variable `c-label-minimum-indentation' specifies the minimum
|
|
5109 indentation amount."
|
|
5110 (let ((non-top-levels '(defun-block-intro statement statement-cont
|
|
5111 statement-block-intro statement-case-intro
|
|
5112 statement-case-open substatement substatement-open
|
|
5113 case-label label do-while-closure else-clause
|
|
5114 ))
|
|
5115 (syntax c-syntactic-context)
|
|
5116 langelem)
|
|
5117 (while syntax
|
|
5118 (setq langelem (car (car syntax))
|
|
5119 syntax (cdr syntax))
|
|
5120 ;; don't adjust comment-only lines
|
|
5121 (cond ((eq langelem 'comment-intro)
|
|
5122 (setq syntax nil))
|
|
5123 ((memq langelem non-top-levels)
|
|
5124 (save-excursion
|
|
5125 (setq syntax nil)
|
|
5126 (back-to-indentation)
|
|
5127 (if (zerop (current-column))
|
|
5128 (insert (make-string c-label-minimum-indentation 32)))
|
|
5129 ))
|
|
5130 ))))
|
|
5131
|
0
|
5132
|
|
5133 ;;; This page handles insertion and removal of backslashes for C macros.
|
|
5134
|
|
5135 (defun c-backslash-region (from to delete-flag)
|
|
5136 "Insert, align, or delete end-of-line backslashes on the lines in the region.
|
|
5137 With no argument, inserts backslashes and aligns existing backslashes.
|
|
5138 With an argument, deletes the backslashes.
|
|
5139
|
4
|
5140 This function does not modify blank lines at the start of the region.
|
|
5141 If the region ends at the start of a line, it always deletes the
|
|
5142 backslash (if any) at the end of the previous line.
|
|
5143
|
|
5144 You can put the region around an entire macro definition and use this
|
|
5145 command to conveniently insert and align the necessary backslashes."
|
0
|
5146 (interactive "r\nP")
|
|
5147 (save-excursion
|
|
5148 (goto-char from)
|
|
5149 (let ((column c-backslash-column)
|
|
5150 (endmark (make-marker)))
|
|
5151 (move-marker endmark to)
|
|
5152 ;; Compute the smallest column number past the ends of all the lines.
|
|
5153 (if (not delete-flag)
|
|
5154 (while (< (point) to)
|
|
5155 (end-of-line)
|
|
5156 (if (= (preceding-char) ?\\)
|
|
5157 (progn (forward-char -1)
|
|
5158 (skip-chars-backward " \t")))
|
|
5159 (setq column (max column (1+ (current-column))))
|
|
5160 (forward-line 1)))
|
|
5161 ;; Adjust upward to a tab column, if that doesn't push past the margin.
|
|
5162 (if (> (% column tab-width) 0)
|
|
5163 (let ((adjusted (* (/ (+ column tab-width -1) tab-width) tab-width)))
|
|
5164 (if (< adjusted (window-width))
|
|
5165 (setq column adjusted))))
|
|
5166 ;; Don't modify blank lines at start of region.
|
|
5167 (goto-char from)
|
|
5168 (while (and (< (point) endmark) (eolp))
|
|
5169 (forward-line 1))
|
|
5170 ;; Add or remove backslashes on all the lines.
|
4
|
5171 (while (< (point) endmark)
|
|
5172 (if (and (not delete-flag)
|
|
5173 ;; Un-backslashify the last line
|
|
5174 ;; if the region ends right at the start of the next line.
|
|
5175 (save-excursion
|
|
5176 (forward-line 1)
|
|
5177 (< (point) endmark)))
|
0
|
5178 (c-append-backslash column)
|
|
5179 (c-delete-backslash))
|
|
5180 (forward-line 1))
|
4
|
5181 (move-marker endmark nil)))
|
|
5182 (c-keep-region-active))
|
0
|
5183
|
|
5184 (defun c-append-backslash (column)
|
|
5185 (end-of-line)
|
|
5186 ;; Note that "\\\\" is needed to get one backslash.
|
|
5187 (if (= (preceding-char) ?\\)
|
|
5188 (progn (forward-char -1)
|
|
5189 (delete-horizontal-space)
|
|
5190 (indent-to column))
|
|
5191 (indent-to column)
|
|
5192 (insert "\\")))
|
|
5193
|
|
5194 (defun c-delete-backslash ()
|
|
5195 (end-of-line)
|
|
5196 (or (bolp)
|
|
5197 (progn
|
2
|
5198 (forward-char -1)
|
|
5199 (if (looking-at "\\\\")
|
|
5200 (delete-region (1+ (point))
|
|
5201 (progn (skip-chars-backward " \t") (point)))))))
|
0
|
5202
|
|
5203
|
|
5204 ;; defuns for submitting bug reports
|
|
5205
|
44
|
5206 (defconst c-version "4.388"
|
32
|
5207 "CC Mode version number.")
|
2
|
5208 (defconst c-mode-help-address
|
|
5209 "bug-gnu-emacs@prep.ai.mit.edu, cc-mode-help@python.org"
|
32
|
5210 "Address for CC Mode bug reports.")
|
0
|
5211
|
|
5212 (defun c-version ()
|
32
|
5213 "Echo the current version of CC Mode in the minibuffer."
|
0
|
5214 (interactive)
|
32
|
5215 (message "Using CC Mode version %s" c-version)
|
0
|
5216 (c-keep-region-active))
|
|
5217
|
|
5218 ;; get reporter-submit-bug-report when byte-compiling
|
|
5219 (eval-when-compile
|
|
5220 (require 'reporter))
|
|
5221
|
|
5222 (defun c-submit-bug-report ()
|
32
|
5223 "Submit via mail a bug report on CC Mode."
|
0
|
5224 (interactive)
|
|
5225 ;; load in reporter
|
|
5226 (let ((reporter-prompt-for-summary-p t)
|
40
|
5227 (reporter-dont-compact-list '(c-offsets-alist))
|
|
5228 (style c-indentation-style)
|
|
5229 (hook c-special-indent-hook)
|
|
5230 (c-features c-emacs-features))
|
0
|
5231 (and
|
32
|
5232 (if (y-or-n-p "Do you want to submit a report on CC Mode? ")
|
0
|
5233 t (message "") nil)
|
|
5234 (require 'reporter)
|
|
5235 (reporter-submit-bug-report
|
|
5236 c-mode-help-address
|
32
|
5237 (concat "CC Mode " c-version " ("
|
0
|
5238 (cond ((eq major-mode 'c++-mode) "C++")
|
|
5239 ((eq major-mode 'c-mode) "C")
|
|
5240 ((eq major-mode 'objc-mode) "ObjC")
|
|
5241 ((eq major-mode 'java-mode) "Java")
|
|
5242 )
|
|
5243 ")")
|
|
5244 (let ((vars (list
|
|
5245 ;; report only the vars that affect indentation
|
|
5246 'c-basic-offset
|
|
5247 'c-offsets-alist
|
|
5248 'c-cleanup-list
|
|
5249 'c-comment-only-line-offset
|
|
5250 'c-backslash-column
|
|
5251 'c-delete-function
|
|
5252 'c-electric-pound-behavior
|
|
5253 'c-hanging-braces-alist
|
|
5254 'c-hanging-colons-alist
|
32
|
5255 'c-hanging-comment-starter-p
|
0
|
5256 'c-hanging-comment-ender-p
|
40
|
5257 'c-indent-comments-syntactically-p
|
0
|
5258 'c-tab-always-indent
|
|
5259 'c-recognize-knr-p
|
2
|
5260 'c-label-minimum-indentation
|
0
|
5261 'defun-prompt-regexp
|
|
5262 'tab-width
|
|
5263 )))
|
|
5264 (if (not (boundp 'defun-prompt-regexp))
|
|
5265 (delq 'defun-prompt-regexp vars)
|
|
5266 vars))
|
|
5267 (function
|
|
5268 (lambda ()
|
|
5269 (insert
|
40
|
5270 "Buffer Style: " style "\n\n"
|
|
5271 (if hook
|
0
|
5272 (concat "\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
|
|
5273 "c-special-indent-hook is set to '"
|
40
|
5274 (format "%s" hook)
|
0
|
5275 ".\nPerhaps this is your problem?\n"
|
|
5276 "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n")
|
|
5277 "\n")
|
40
|
5278 (format "c-emacs-features: %s\n" c-features)
|
0
|
5279 )))
|
|
5280 nil
|
|
5281 "Dear Barry,"
|
|
5282 ))))
|
|
5283
|
|
5284
|
|
5285 ;; menus for XEmacs 19
|
32
|
5286 (defun c-mode-menu ()
|
|
5287 (cons (concat mode-name " Mode Commands") c-mode-menu))
|
|
5288
|
0
|
5289 (defun c-popup-menu (e)
|
|
5290 "Pops up the C/C++/ObjC menu."
|
|
5291 (interactive "@e")
|
32
|
5292 (popup-menu (c-mode-menu))
|
0
|
5293 (c-keep-region-active))
|
|
5294
|
|
5295
|
2
|
5296 ;; Emacs/XEmacs Compatibility
|
32
|
5297 ;; XEmacs has these, Emacs does not
|
|
5298
|
|
5299 (if (fboundp 'functionp)
|
|
5300 (defalias 'c-functionp 'functionp)
|
|
5301 ;; Lift XEmacs 19.13's functionp from subr.el
|
|
5302 (defun c-functionp (obj)
|
|
5303 "Returns t if OBJ is a function, nil otherwise."
|
|
5304 (cond
|
|
5305 ((symbolp obj) (fboundp obj))
|
|
5306 ((subrp obj))
|
|
5307 ((compiled-function-p obj))
|
|
5308 ((consp obj)
|
|
5309 (if (eq (car obj) 'lambda) (listp (car (cdr obj)))))
|
|
5310 (t nil))))
|
|
5311
|
|
5312 (if (fboundp 'copy-tree)
|
|
5313 (defalias 'c-copy-tree 'copy-tree)
|
0
|
5314 ;; Lift XEmacs 19.12's copy-tree
|
32
|
5315 (defun c-copy-tree (tree)
|
|
5316 (if (consp tree)
|
|
5317 (cons (c-copy-tree (car tree))
|
|
5318 (c-copy-tree (cdr tree)))
|
|
5319 (if (vectorp tree)
|
|
5320 (let* ((new (copy-sequence tree))
|
|
5321 (i (1- (length new))))
|
|
5322 (while (>= i 0)
|
|
5323 (aset new i (c-copy-tree (aref new i)))
|
|
5324 (setq i (1- i)))
|
|
5325 new)
|
|
5326 tree))))
|
2
|
5327
|
|
5328
|
0
|
5329 ;; Dynamically append the default value of most variables. This is
|
|
5330 ;; crucial because future c-set-style calls will always reset the
|
|
5331 ;; variables first to the `cc-mode' style before instituting the new
|
|
5332 ;; style. Only do this once!
|
|
5333 (or (assoc "cc-mode" c-style-alist)
|
|
5334 (progn
|
|
5335 (c-add-style "cc-mode"
|
32
|
5336 (mapcar
|
|
5337 (function
|
|
5338 (lambda (var)
|
|
5339 (let ((val (symbol-value var)))
|
|
5340 (cons var (if (atom val) val
|
|
5341 (c-copy-tree val)
|
|
5342 ))
|
|
5343 )))
|
|
5344 '(c-backslash-column
|
|
5345 c-basic-offset
|
|
5346 c-cleanup-list
|
|
5347 c-comment-only-line-offset
|
|
5348 c-electric-pound-behavior
|
|
5349 c-hanging-braces-alist
|
|
5350 c-hanging-colons-alist
|
|
5351 c-hanging-comment-starter-p
|
|
5352 c-hanging-comment-ender-p
|
|
5353 c-offsets-alist
|
|
5354 )))
|
0
|
5355 ;; the default style is now GNU. This can be overridden in
|
2
|
5356 ;; c-mode-common-hook or {c,c++,objc,java}-mode-hook.
|
0
|
5357 (c-set-style c-site-default-style)))
|
|
5358
|
32
|
5359 (if c-style-variables-are-local-p
|
|
5360 (progn
|
|
5361 ;; style variables
|
|
5362 (make-variable-buffer-local 'c-offsets-alist)
|
|
5363 (make-variable-buffer-local 'c-basic-offset)
|
|
5364 (make-variable-buffer-local 'c-file-style)
|
|
5365 (make-variable-buffer-local 'c-file-offsets)
|
|
5366 (make-variable-buffer-local 'c-comment-only-line-offset)
|
|
5367 (make-variable-buffer-local 'c-cleanup-list)
|
|
5368 (make-variable-buffer-local 'c-hanging-braces-alist)
|
|
5369 (make-variable-buffer-local 'c-hanging-colons-alist)
|
|
5370 (make-variable-buffer-local 'c-hanging-comment-starter-p)
|
|
5371 (make-variable-buffer-local 'c-hanging-comment-ender-p)
|
|
5372 (make-variable-buffer-local 'c-backslash-column)
|
|
5373 (make-variable-buffer-local 'c-label-minimum-indentation)
|
|
5374 (make-variable-buffer-local 'c-special-indent-hook)
|
|
5375 (make-variable-buffer-local 'c-indentation-style)))
|
0
|
5376
|
|
5377
|
|
5378 ;; fsets for compatibility with BOCM
|
|
5379 (fset 'electric-c-brace 'c-electric-brace)
|
|
5380 (fset 'electric-c-semi 'c-electric-semi&comma)
|
|
5381 (fset 'electric-c-sharp-sign 'c-electric-pound)
|
32
|
5382 ;; there is no CC Mode equivalent for electric-c-terminator
|
0
|
5383 (fset 'mark-c-function 'c-mark-function)
|
|
5384 (fset 'indent-c-exp 'c-indent-exp)
|
4
|
5385 ;;;###autoload (fset 'set-c-style 'c-set-style)
|
32
|
5386 ;; Lucid Emacs 19.9 + font-lock + CC Mode - c++-mode lossage
|
0
|
5387 (fset 'c++-beginning-of-defun 'beginning-of-defun)
|
|
5388 (fset 'c++-end-of-defun 'end-of-defun)
|
|
5389
|
|
5390 ;; set up bc warnings for obsolete variables, but for now lets not
|
|
5391 ;; worry about obsolete functions. maybe later some will be important
|
|
5392 ;; to flag
|
2
|
5393 (and (or (memq 'v19 c-emacs-features) (memq 'v20 c-emacs-features))
|
0
|
5394 (let* ((na "Nothing appropriate.")
|
|
5395 (vars
|
|
5396 (list
|
|
5397 (cons 'c++-c-mode-syntax-table 'c-mode-syntax-table)
|
|
5398 (cons 'c++-tab-always-indent 'c-tab-always-indent)
|
|
5399 (cons 'c++-always-arglist-indent-p na)
|
|
5400 (cons 'c++-block-close-brace-offset 'c-offsets-alist)
|
|
5401 (cons 'c++-paren-as-block-close-p na)
|
|
5402 (cons 'c++-continued-member-init-offset 'c-offsets-alist)
|
|
5403 (cons 'c++-member-init-indent 'c-offsets-alist)
|
|
5404 (cons 'c++-friend-offset na)
|
|
5405 (cons 'c++-access-specifier-offset 'c-offsets-alist)
|
|
5406 (cons 'c++-empty-arglist-indent 'c-offsets-alist)
|
|
5407 (cons 'c++-comment-only-line-offset 'c-comment-only-line-offset)
|
32
|
5408 (cons 'c++-C-block-comments-indent-p na)
|
0
|
5409 (cons 'c++-cleanup-list 'c-cleanup-list)
|
|
5410 (cons 'c++-hanging-braces 'c-hanging-braces-alist)
|
|
5411 (cons 'c++-hanging-member-init-colon 'c-hanging-colons-alist)
|
|
5412 (cons 'c++-auto-hungry-initial-state
|
|
5413 "Use `c-auto-newline' and `c-hungry-delete-key' instead.")
|
|
5414 (cons 'c++-auto-hungry-toggle na)
|
|
5415 (cons 'c++-relative-offset-p na)
|
|
5416 (cons 'c++-special-indent-hook 'c-special-indent-hook)
|
|
5417 (cons 'c++-delete-function 'c-delete-function)
|
|
5418 (cons 'c++-electric-pound-behavior 'c-electric-pound-behavior)
|
|
5419 (cons 'c++-hungry-delete-key 'c-hungry-delete-key)
|
|
5420 (cons 'c++-auto-newline 'c-auto-newline)
|
|
5421 (cons 'c++-match-header-strongly na)
|
|
5422 (cons 'c++-defun-header-strong-struct-equivs na)
|
|
5423 (cons 'c++-version 'c-version)
|
|
5424 (cons 'c++-mode-help-address 'c-mode-help-address)
|
|
5425 (cons 'c-indent-level 'c-basic-offset)
|
|
5426 (cons 'c-brace-imaginary-offset na)
|
|
5427 (cons 'c-brace-offset 'c-offsets-alist)
|
|
5428 (cons 'c-argdecl-indent 'c-offsets-alist)
|
|
5429 (cons 'c-label-offset 'c-offsets-alist)
|
|
5430 (cons 'c-continued-statement-offset 'c-offsets-alist)
|
|
5431 (cons 'c-continued-brace-offset 'c-offsets-alist)
|
|
5432 (cons 'c-default-macroize-column 'c-backslash-column)
|
|
5433 (cons 'c++-default-macroize-column 'c-backslash-column)
|
32
|
5434 (cons 'c-block-comments-indent-p na)
|
0
|
5435 )))
|
|
5436 (mapcar
|
|
5437 (function
|
|
5438 (lambda (elt)
|
|
5439 (make-obsolete-variable (car elt) (cdr elt))))
|
|
5440 vars)))
|
|
5441
|
|
5442 (provide 'cc-mode)
|
|
5443 ;;; cc-mode.el ends here
|