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