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