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