70
|
1 \input texinfo @c -*- texinfo -*-
|
0
|
2
|
|
3 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
4 @comment %**start of header (This is for running Texinfo on a region)
|
|
5 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
6
|
32
|
7 @setfilename cc-mode.info
|
70
|
8 @settitle CC-MODE Version 4 Documentation
|
0
|
9 @footnotestyle end
|
|
10
|
|
11 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
12 @comment @setchapternewpage odd !! we don't want blank pages !!
|
|
13 @comment %**end of header (This is for running Texinfo on a region)
|
|
14 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
15
|
|
16
|
|
17 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
18 @comment
|
|
19 @comment texinfo manual for @file{cc-mode.el} version 4
|
70
|
20 @comment manual version: 2.55
|
0
|
21 @comment generated from the original README file by Krishna Padmasola
|
|
22 @comment <krishna@earth-gw.njit.edu>
|
|
23 @comment
|
|
24 @comment Barry A. Warsaw <bwarsaw@cnri.reston.va.us>
|
70
|
25 @comment Last modification: 1996/08/21 19:29:16
|
0
|
26 @comment
|
|
27 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
28
|
|
29
|
|
30 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
31 @comment The following line inserts the copyright notice
|
|
32 @comment into the Info file.
|
|
33 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
34
|
|
35 @ifinfo
|
2
|
36 Copyright @copyright{} 1995, 1996 Free Software Foundation, Inc.
|
0
|
37 @end ifinfo
|
|
38
|
|
39 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
40 @comment !!!The titlepage section does not appear in the Info file.!!!
|
|
41 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
42
|
|
43 @titlepage
|
|
44 @sp 10
|
|
45
|
|
46
|
|
47 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
48 @comment The title is printed in a large font.
|
|
49 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
50
|
70
|
51 @center @titlefont{CC-MODE Version 4}
|
0
|
52 @sp 2
|
2
|
53 @center A GNU Emacs mode for editing C, C++, Objective-C, and Java code.
|
70
|
54 @center (manual revision: 2.55)
|
0
|
55 @sp 2
|
|
56 @center Barry A. Warsaw
|
|
57
|
|
58
|
|
59 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
60 @comment The following two commands start the copyright page
|
|
61 @comment for the printed manual. This will not appear in the Info file.
|
|
62 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
63
|
|
64 @page
|
|
65 @vskip 0pt plus 1filll
|
|
66 Copyright @copyright{} 1995 Free Software Foundation, Inc.
|
|
67 @end titlepage
|
|
68
|
|
69
|
|
70 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
71 @comment The Top node contains the master menu for the Info file.
|
|
72 @comment This appears only in the Info file, not the printed manual.
|
|
73 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
74
|
|
75 @node Top, Introduction, (dir), (dir)
|
|
76 @comment node-name, next, previous, up
|
|
77
|
|
78
|
|
79 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
80 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
81
|
|
82 @menu
|
|
83 * Introduction::
|
|
84 * Getting Connected::
|
|
85 * New Indentation Engine::
|
|
86 * Minor Modes::
|
2
|
87 * Commands::
|
0
|
88 * Customizing Indentation::
|
|
89 * Syntactic Symbols::
|
|
90 * Performance Issues::
|
|
91 * Frequently Asked Questions::
|
70
|
92 * Getting the latest cc-mode release::
|
0
|
93 * Sample .emacs File::
|
|
94 * Limitations and Known Bugs::
|
|
95 * Mailing Lists and Submitting Bug Reports::
|
|
96 * Concept Index::
|
|
97 * Command Index:: Command Index
|
|
98 * Key Index:: Key Index
|
|
99 * Variable Index:: Variable Index
|
|
100 @end menu
|
|
101
|
|
102 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
103 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
104
|
|
105 @node Introduction, Getting Connected, Top, Top
|
|
106 @comment node-name, next, previous, up
|
|
107 @chapter Introduction
|
|
108 @cindex Introduction
|
|
109
|
|
110 @cindex BOCM
|
70
|
111 Welcome to @code{cc-mode}, version 4. This is a GNU Emacs mode for
|
0
|
112 editing files containing C, C++, Objective-C, and Java code.
|
|
113 This incarnation of the mode is descendant from @file{c-mode.el} (also
|
|
114 called "Boring Old C Mode" or BOCM @code{:-)}, and @file{c++-mode.el}
|
70
|
115 version 2, which I have been maintaining since 1992. @code{cc-mode}
|
0
|
116 represents a significant milestone in the mode's life. It has been
|
|
117 fully merged back with Emacs 19's @file{c-mode.el}. Also a new, more
|
|
118 intuitive and flexible mechanism for controlling indentation has been
|
|
119 developed.
|
|
120
|
70
|
121 @code{cc-mode} version 4 supports the editing of K&R and ANSI C,
|
|
122 @dfn{ARM} @footnote{i.e. ``The Annotated C++ Reference Manual'', by
|
|
123 Ellis and Stroustrup.} C++, Objective-C, and Java files. In this way,
|
|
124 you can easily set up consistent coding styles for use in editing all C,
|
|
125 C++, Objective-C, and Java programs. @code{cc-mode} does @emph{not}
|
|
126 handle font-locking (a.k.a. syntax coloring, keyword highlighting) or
|
|
127 anything of that nature, for any of the 4 modes. Those are handled by
|
|
128 other Emacs packages.
|
0
|
129
|
|
130 This manual will describe the following:
|
|
131
|
|
132 @itemize @bullet
|
|
133 @item
|
70
|
134 How to get started using @code{cc-mode}.
|
0
|
135
|
|
136 @item
|
|
137 How the new indentation engine works.
|
|
138
|
|
139 @item
|
|
140 How to customize the new indentation engine.
|
|
141
|
|
142 @end itemize
|
|
143
|
70
|
144 Note that the name of this file is @file{cc-mode.el}, and I'll often
|
|
145 refer to the package as @code{cc-mode}, but there really is no top level
|
|
146 @code{cc-mode} entry point. I call it @code{cc-mode} simply to
|
|
147 differentiate it from @file{c-mode.el}. All of the variables, commands,
|
|
148 and functions in @code{cc-mode} are prefixed with
|
|
149 @code{c-@var{<thing>}}, and @code{c-mode}, @code{c++-mode},
|
|
150 @code{objc-mode}, and @code{java-mode} entry points are provided. This
|
|
151 file is intended to be a replacement for @file{c-mode.el} and
|
|
152 @file{c++-mode.el}.
|
0
|
153
|
|
154 @findex c-version
|
|
155 The major version number was incremented to 4 with the addition of
|
|
156 @code{objc-mode}. To find the minor revision number of this release, use
|
2
|
157 @kbd{M-x c-version RET}.
|
|
158
|
70
|
159 As of this writing (20-Aug-1996), both Emacs 19.33 and XEmacs 19.14 are
|
|
160 distributed with @code{cc-mode}, however neither have the very latest
|
|
161 version. In all likelihood, Emacs 19.34 and XEmacs 19.15 will contain
|
|
162 the latest version of @code{cc-mode}. You may therefore, want to
|
|
163 upgrade your copy of @code{cc-mode}. See @ref{Getting the latest
|
|
164 cc-mode release}.
|
0
|
165
|
|
166 @cindex @file{cc-compat.el} file
|
2
|
167 This distribution also contains a file called @file{cc-compat.el} which
|
70
|
168 should ease your transition from BOCM to @code{cc-mode}. It currently
|
2
|
169 comes unguaranteed and unsupported, but this may change for future
|
|
170 versions. If you have a BOCM configuration you are really happy with,
|
70
|
171 and want to postpone learning how to configure @code{cc-mode}, take a
|
2
|
172 look at that file. It maps BOCM configuration variables to
|
70
|
173 @code{cc-mode}'s new indentation model.
|
0
|
174
|
|
175 A special word of thanks goes to Krishna Padmasola for his work in
|
|
176 converting the original @file{README} file to texinfo format.
|
|
177
|
|
178
|
|
179 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
180
|
|
181 @node Getting Connected, New Indentation Engine, Introduction, Top
|
|
182 @comment node-name, next, previous, up
|
|
183 @chapter Getting Connected
|
|
184 @cindex Getting Connected
|
|
185
|
|
186 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
187
|
70
|
188 @file{cc-mode.el} works well with the 2 main branches of Emacs 19:
|
|
189 XEmacs, maintained by @code{xemacs.org} and the Emacs 19 maintained by
|
|
190 the Free Software Foundation. Emacs 19 users will want to use Emacs
|
|
191 version 19.21 or better, XEmacs users will want 19.6 or better. Earlier
|
|
192 versions of these Emacsen have deficiencies and/or bugs which will
|
|
193 adversely affect the performance and usability of @code{cc-mode}. You
|
2
|
194 are better off just getting the latest version of Emacs or XEmacs.
|
0
|
195
|
|
196 @cindex @file{cc-mode-18.el} file
|
2
|
197 @file{cc-mode.el} will work with Emacs 18 if you use the
|
|
198 @file{cc-mode-18.el} compatibility file, but only moderately well. A
|
0
|
199 word of warning though, @emph{Emacs 18 lacks some fundamental
|
2
|
200 functionality and ultimately, using Emacs 18 is a losing
|
70
|
201 battle}. Hence @code{cc-mode} under Emacs 18 is no longer supported and
|
0
|
202 it is highly recommended that you upgrade to Emacs 19. If you use
|
70
|
203 @code{cc-mode} under Emacs 18, you're on your own. With @code{cc-mode}
|
0
|
204 version 5, Emacs 18 support will be dropped altogether.
|
|
205
|
|
206 Note that as of XEmacs 19.13 and Emacs 19.30, your Emacs already comes
|
70
|
207 with @code{cc-mode} version 4 preconfigured for your use. You should be
|
2
|
208 able to safely skip the rest of the setup information in this chapter,
|
70
|
209 unless you want to install the latest version of @code{cc-mode} into one
|
2
|
210 of these Emacsen.
|
0
|
211
|
70
|
212 @cindex @file{.emacs} file
|
0
|
213 The first thing you will want to do is put @file{cc-mode.el} somewhere
|
|
214 on your @code{load-path} so Emacs can find it. Do a @kbd{C-h v
|
|
215 load-path RET} to see all the directories Emacs looks at when loading a
|
|
216 file. If none of these directories are appropriate, create a new
|
|
217 directory and add it to your @code{load-path}:
|
|
218
|
|
219 @noindent
|
|
220 @emph{[in the shell]}
|
|
221 @example
|
|
222 @group
|
|
223
|
|
224 % cd
|
|
225 % mkdir mylisp
|
|
226 % mv cc-mode.el mylisp
|
|
227 % cd mylisp
|
|
228
|
|
229 @end group
|
|
230 @end example
|
|
231
|
|
232 @noindent
|
|
233 @emph{[in your .emacs file add]}
|
|
234 @example
|
|
235
|
|
236 (setq load-path (cons "~/mylisp" load-path))
|
|
237
|
|
238 @end example
|
|
239
|
|
240 @cindex byte compile
|
|
241 Next you want to @dfn{byte compile} @file{cc-mode.el}. The mode uses a
|
|
242 lot of macros so if you don't byte compile it, things will be unbearably
|
|
243 slow. @emph{You can ignore all byte-compiler warnings!} They are the
|
|
244 result of the supporting different versions of Emacs, and none of the
|
|
245 warnings have any effect on operation. Let me say this again:
|
|
246 @strong{You really can ignore all byte-compiler warnings!}
|
|
247
|
|
248 Here's what to do to byte-compile the file [in emacs]:
|
|
249 @example
|
|
250
|
|
251 M-x byte-compile-file RET ~/mylisp/cc-mode.el RET
|
|
252
|
|
253 @end example
|
|
254
|
|
255 If you are running a version of Emacs or XEmacs that comes with
|
70
|
256 @code{cc-mode} by default, you can simply add the following to your
|
0
|
257 @file{.emacs} file in order to upgrade to the latest version of
|
70
|
258 @code{cc-mode}:
|
0
|
259 @example
|
|
260
|
|
261 (load "cc-mode")
|
|
262
|
|
263 @end example
|
|
264
|
|
265 Users of even older versions of Emacs 19, Emacs 18, or of the older
|
|
266 Lucid Emacs will probably be running an Emacs that has BOCM
|
|
267 @file{c-mode.el} and possible @file{c++-mode.el} pre-dumped. If your
|
|
268 Emacs is dumped with either of these files you first need to make Emacs
|
|
269 ``forget'' about those older modes.
|
|
270
|
|
271 If you can do a @kbd{C-h v c-mode-map RET} without getting an error, you
|
|
272 need to add these lines at the top of your @file{.emacs} file:
|
|
273 @example
|
|
274 @group
|
|
275
|
|
276 (fmakunbound 'c-mode)
|
|
277 (makunbound 'c-mode-map)
|
|
278 (fmakunbound 'c++-mode)
|
|
279 (makunbound 'c++-mode-map)
|
|
280 (makunbound 'c-style-alist)
|
|
281
|
|
282 @end group
|
|
283 @end example
|
|
284
|
|
285 After those lines you will want to add the following autoloads to your
|
70
|
286 @file{.emacs} file so that @code{cc-mode} gets loaded at the right time:
|
0
|
287 @example
|
|
288 @group
|
|
289
|
|
290 (autoload 'c++-mode "cc-mode" "C++ Editing Mode" t)
|
|
291 (autoload 'c-mode "cc-mode" "C Editing Mode" t)
|
|
292 (autoload 'objc-mode "cc-mode" "Objective-C Editing Mode" t)
|
|
293 (autoload 'java-mode "cc-mode" "Java Editing Mode" t)
|
|
294
|
|
295 @end group
|
|
296 @end example
|
|
297
|
70
|
298 Alternatively, if you want to make sure @code{cc-mode} is loaded when
|
2
|
299 Emacs starts up, you could use this line instead of the autoloads
|
0
|
300 above:
|
|
301 @example
|
|
302
|
|
303 (require 'cc-mode)
|
|
304
|
|
305 @end example
|
|
306
|
|
307 Next, you will want to set up Emacs so that it edits C files in
|
2
|
308 @code{c-mode}, C++ files in @code{c++-mode}, Objective-C files in
|
|
309 @code{objc-mode}, and Java files in @code{java-mode}. You should
|
|
310 add the following to your @file{.emacs} file, which assumes
|
|
311 you'll be editing @code{.h} and @code{.c} files as C, @code{.hh},
|
|
312 @code{.cc}, @code{.H}, and @code{.C} files as C++, @code{.m} files as
|
|
313 Objective-C, and @code{.java} files as Java code. YMMV:
|
0
|
314 @example
|
|
315 @group
|
|
316
|
|
317 (setq auto-mode-alist
|
|
318 (append
|
|
319 '(("\\.C$" . c++-mode)
|
|
320 ("\\.H$" . c++-mode)
|
|
321 ("\\.cc$" . c++-mode)
|
|
322 ("\\.hh$" . c++-mode)
|
|
323 ("\\.c$" . c-mode)
|
|
324 ("\\.h$" . c-mode)
|
|
325 ("\\.m$" . objc-mode)
|
|
326 ("\\.java$" . java-mode)
|
|
327 ) auto-mode-alist))
|
|
328
|
|
329 @end group
|
|
330 @end example
|
|
331
|
|
332 You may already have some or all of these settings on your
|
|
333 @code{auto-mode-alist}, but it won't hurt to put them on there again.
|
|
334
|
2
|
335 That's all you need --- I know, I know, it sounds like a lot @code{:-)},
|
0
|
336 but after you've done all this, you should only need to quit and restart
|
|
337 Emacs. The next time you visit a C, C++, Objective-C, or Java file you
|
70
|
338 should be using @code{cc-mode}. You can check this easily by hitting
|
|
339 @kbd{M-x c-version RET} in the @code{c-mode}, @code{c++-mode}, or
|
|
340 @code{objc-mode} buffer. You should see this message in the echo area:
|
0
|
341 @example
|
|
342
|
70
|
343 Using @code{cc-mode} version 4.@var{xxx}
|
|
344
|
|
345 Where @var{xxx} is the latest release minor number.
|
0
|
346
|
|
347 @end example
|
|
348
|
|
349 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
350 @node New Indentation Engine, Minor Modes, Getting Connected, Top
|
70
|
351 @comment node-name, next, previous,up
|
0
|
352
|
|
353 @chapter New Indentation Engine
|
|
354 @cindex New Indentation Engine
|
|
355 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
356
|
70
|
357 @code{cc-mode} has a new indentation engine, providing a simplified, yet
|
|
358 flexible and general mechanism for customizing indentation. It breaks
|
|
359 indentation calculation into two steps. First, for the line of code being
|
|
360 indented, @code{cc-mode} analyzes what kind of language construct it's
|
|
361 looking at, then it applies user defined offsets to the current line
|
|
362 based on this analysis.
|
0
|
363
|
|
364 This section will briefly cover how indentation is calculated in
|
70
|
365 @code{cc-mode}. It is important to understand the indentation model
|
|
366 being used so that you will know how to customize @code{cc-mode} for
|
0
|
367 your personal coding style.
|
|
368
|
|
369 @menu
|
|
370 * Syntactic Analysis::
|
|
371 * Indentation Calculation::
|
|
372 @end menu
|
2
|
373
|
0
|
374
|
|
375 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
376 @node Syntactic Analysis, Indentation Calculation, , New Indentation Engine
|
|
377 @comment node-name, next, previous,up
|
|
378 @section Syntactic Analysis
|
|
379 @cindex Syntactic Analysis
|
|
380 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
381
|
|
382 @vindex c-offsets-alist
|
|
383 @vindex offsets-alist (c-)
|
|
384 @cindex relative buffer position
|
|
385 @cindex syntactic symbol
|
|
386 @cindex syntactic component
|
|
387 @cindex syntactic component list
|
|
388 @cindex relative buffer position
|
70
|
389 The first thing @code{cc-mode} does when indenting a line of code, is to
|
0
|
390 analyze the line, determining the @dfn{syntactic component list} of the
|
|
391 construct on that line. A @dfn{syntactic component} consists of a pair
|
|
392 of information (in lisp parlance, a @emph{cons cell}), where the first
|
|
393 part is a @dfn{syntactic symbol}, and the second part is a @dfn{relative
|
|
394 buffer position}. Syntactic symbols describe elements of C code
|
|
395 @footnote{or C++, Objective-C, or Java code. In general, for the rest
|
|
396 of this manual I'll use the term ``C code'' to refer to all the C-like
|
|
397 dialects, unless otherwise noted.}, e.g. @code{statement},
|
|
398 @code{substatement}, @code{class-open}, @code{class-close}, etc.
|
|
399 @xref{Syntactic Symbols}, for a complete list of currently recognized
|
|
400 syntactic symbols and their semantics. The variable
|
|
401 @code{c-offsets-alist} also contains the list of currently supported
|
|
402 syntactic symbols.
|
|
403
|
|
404 Conceptually, a line of C code is always indented relative to the
|
|
405 indentation of some line higher up in the buffer. This is represented
|
|
406 by the relative buffer position in the syntactic component.
|
|
407
|
70
|
408 It might help to see an example. Suppose we had the following code as
|
|
409 the only thing in a @code{c++-mode} buffer @footnote{The line numbers in
|
|
410 this and future examples don't actually appear in the buffer, of course!}:
|
0
|
411 @example
|
|
412 @group
|
|
413
|
|
414 1: void swap( int& a, int& b )
|
|
415 2: @{
|
|
416 3: int tmp = a;
|
|
417 4: a = b;
|
|
418 5: b = tmp;
|
|
419 6: @}
|
|
420
|
|
421 @end group
|
|
422 @end example
|
|
423
|
|
424 @kindex C-c C-s
|
|
425 @findex c-show-syntactic-information
|
|
426 @findex show-syntactic-information (c-)
|
|
427 We can use the command @kbd{C-c C-s}
|
|
428 (@code{c-show-syntactic-information}) to simply report what the
|
|
429 syntactic analysis is for the current line. Running this command on
|
2
|
430 line 4 this example, we'd see in the echo area@footnote{With a universal
|
|
431 argument (i.e. @kbd{C-u C-c C-s}) the analysis is inserted into the
|
|
432 buffer as a comment
|
|
433 on the current line.}:
|
0
|
434 @example
|
|
435
|
|
436 ((statement . 35))
|
|
437
|
|
438 @end example
|
|
439
|
|
440 This tells us that the line is a statement and it is indented relative
|
|
441 to buffer position 35, which happens to be the @samp{i} in @code{int} on
|
70
|
442 line 3. If you were to move Point to line 3 and hit @kbd{C-c C-s}, you
|
0
|
443 would see:
|
|
444 @example
|
|
445
|
|
446 ((defun-block-intro . 29))
|
|
447
|
|
448 @end example
|
|
449
|
|
450 This indicates that the @samp{int} line is the first statement in a top
|
|
451 level function block, and is indented relative to buffer position 29,
|
|
452 which is the brace just after the function header.
|
|
453
|
|
454 Here's another example:
|
|
455 @example
|
|
456 @group
|
|
457
|
|
458 1: int add( int val, int incr, int doit )
|
|
459 2: @{
|
|
460 3: if( doit )
|
|
461 4: @{
|
|
462 5: return( val + incr );
|
|
463 6: @}
|
|
464 7: return( val );
|
|
465 8: @}
|
|
466
|
|
467 @end group
|
|
468 @end example
|
|
469
|
|
470 @noindent
|
|
471 Hitting @kbd{C-c C-s} on line 4 gives us:
|
|
472 @example
|
|
473
|
|
474 ((substatement-open . 46))
|
|
475
|
|
476 @end example
|
|
477
|
|
478 @cindex substatement
|
|
479 @cindex substatment block
|
|
480 @noindent
|
|
481 which tells us that this is a brace that @emph{opens} a substatement
|
70
|
482 block. @footnote{A @dfn{substatement} indicates the line after an
|
|
483 @code{if}, @code{else}, @code{while}, @code{do}, @code{switch}, or
|
|
484 @code{for} statement, and a @dfn{substatement block} is a brace block
|
|
485 following one of those constructs.}
|
0
|
486
|
|
487 @cindex comment only line
|
|
488 Syntactic component lists can contain more than one component, and
|
|
489 individual syntactic components need not have relative buffer positions.
|
|
490 The most common example of this is a line that contains a @dfn{comment
|
|
491 only line}.
|
|
492 @example
|
|
493 @group
|
|
494
|
|
495 1: void draw_list( List<Drawables>& drawables )
|
|
496 2: @{
|
|
497 3: // call the virtual draw() method on each element in list
|
|
498 4: for( int i=0; i < drawables.count(), ++i )
|
|
499 5: @{
|
|
500 6: drawables[i].draw();
|
|
501 7: @}
|
|
502 8: @}
|
|
503
|
|
504 @end group
|
|
505 @end example
|
|
506
|
|
507 @noindent
|
2
|
508 Hitting @kbd{C-c C-s} on line 3 of this example gives:
|
0
|
509 @example
|
|
510
|
|
511 ((comment-intro) (defun-block-intro . 46))
|
|
512
|
|
513 @end example
|
|
514
|
|
515 @noindent
|
2
|
516 and you can see that the syntactic component list contains two syntactic
|
0
|
517 components. Also notice that the first component,
|
|
518 @samp{(comment-intro)} has no relative buffer position.
|
|
519
|
|
520
|
|
521 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
522 @node Indentation Calculation, , Syntactic Analysis, New Indentation Engine
|
|
523 @comment node-name, next, previous,up
|
|
524 @section Indentation Calculation
|
|
525 @cindex Indentation Calculation
|
|
526 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
527
|
|
528 @vindex c-offsets-alist
|
|
529 @vindex offsets-alist (c-)
|
|
530 Indentation for the current line is calculated using the syntactic
|
|
531 component list derived in step 1 above (see @ref{Syntactic Analysis}).
|
|
532 Each component contributes to the final total indentation of the line in
|
|
533 two ways.
|
|
534
|
|
535 First, the syntactic symbols are looked up in the @code{c-offsets-alist}
|
|
536 variable, which is an association list of syntactic symbols and the
|
2
|
537 offsets to apply for those symbols. These offsets are added to a
|
0
|
538 running total.
|
|
539
|
70
|
540 Second, if the component has a relative buffer position, @code{cc-mode}
|
0
|
541 adds the column number of that position to the running total. By adding
|
|
542 up the offsets and columns for every syntactic component on the list,
|
|
543 the final total indentation for the current line is computed.
|
|
544
|
|
545 Let's use our two code examples above to see how this works. Here is
|
|
546 our first example again:
|
|
547 @example
|
|
548 @group
|
|
549
|
|
550 1: void swap( int& a, int& b )
|
|
551 2: @{
|
|
552 3: int tmp = a;
|
|
553 4: a = b;
|
|
554 5: b = tmp;
|
|
555 6: @}
|
|
556
|
|
557 @end group
|
|
558 @end example
|
|
559
|
|
560 @kindex TAB
|
70
|
561 Let's say Point is on line 3 and we hit the @key{TAB} key to re-indent
|
0
|
562 the line. Remember that the syntactic component list for that
|
|
563 line is:
|
|
564 @example
|
|
565
|
|
566 ((defun-block-intro . 29))
|
|
567
|
|
568 @end example
|
|
569
|
|
570 @noindent
|
70
|
571 @code{cc-mode} looks up @code{defun-block-intro} in the
|
0
|
572 @code{c-offsets-alist} variable. Let's say it finds the value @samp{4};
|
|
573 it adds this to the running total (initialized to zero), yielding a
|
|
574 running total indentation of 4 spaces.
|
|
575
|
70
|
576 Next @code{cc-mode} goes to buffer position 29 and asks for the current
|
|
577 column. This brace is in column zero, so @code{cc-mode}
|
0
|
578 adds @samp{0} to the running total. Since there is only one syntactic
|
|
579 component on the list for this line, indentation calculation is
|
|
580 complete, and the total indentation for the line
|
|
581 is 4 spaces.
|
|
582
|
|
583 Here's another example:
|
|
584 @example
|
|
585 @group
|
|
586
|
|
587 1: int add( int val, int incr, int doit )
|
|
588 2: @{
|
|
589 3: if( doit )
|
|
590 4: @{
|
|
591 5: return( val + incr );
|
|
592 6: @}
|
|
593 7: return( val );
|
|
594 8: @}
|
|
595
|
|
596 @end group
|
|
597 @end example
|
|
598
|
|
599 If we were to hit @kbd{TAB} on line 4 in the above example, the same
|
|
600 basic process is performed, despite the differences in the syntactic
|
|
601 component list. Remember that the list for this line is:
|
|
602 @example
|
|
603
|
|
604 ((substatement-open . 46))
|
|
605
|
|
606 @end example
|
|
607
|
70
|
608 Here, @code{cc-mode} first looks up the @code{substatement-open} symbol
|
0
|
609 in @code{c-offsets-alist}. Let's say it finds the value @samp{4}. This
|
70
|
610 yields a running total of 4. @code{cc-mode} then goes to
|
0
|
611 buffer position 46, which is the @samp{i} in @code{if} on line 3. This
|
|
612 character is in the fourth column on that line so adding this to the
|
|
613 running total yields an indentation for the line of 8 spaces.
|
|
614
|
|
615 Simple, huh?
|
|
616
|
|
617 Actually, the mode usually just does The Right Thing without you having
|
|
618 to think about it in this much detail. But when customizing
|
|
619 indentation, it's helpful to understand the general indentation model
|
|
620 being used.
|
|
621
|
|
622 @vindex c-echo-syntactic-information-p
|
|
623 @vindex echo-syntactic-information-p (c-)
|
|
624 @cindex TAB
|
70
|
625 To help you configure @code{cc-mode}, you can set the variable
|
0
|
626 @code{c-echo-syntactic-information-p} to non-@code{nil} so that the
|
|
627 syntactic component list and calculated offset will always be echoed in
|
|
628 the minibuffer when you hit @kbd{TAB}.
|
|
629
|
|
630
|
|
631 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
2
|
632 @node Minor Modes, Commands, New Indentation Engine, Top
|
0
|
633 @comment node-name, next, previous,up
|
|
634
|
|
635 @chapter Minor Modes
|
|
636 @cindex Minor Modes
|
|
637 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
638
|
70
|
639 @code{cc-mode} contains two minor-mode-like features that you should
|
0
|
640 find useful while you enter new C code. The first is called
|
|
641 @dfn{auto-newline} mode, and the second is called @dfn{hungry-delete}
|
|
642 mode. These minor modes can be toggled on and off independently, and
|
70
|
643 @code{cc-mode} can be configured so that it starts up with any
|
0
|
644 combination of these minor modes. By default, both of these minor modes
|
|
645 are turned off.
|
|
646
|
|
647 The state of the minor modes is always reflected in the minor mode list
|
70
|
648 on the modeline of the @code{cc-mode} buffer. When auto-newline mode is
|
0
|
649 enabled, you will see @samp{C/a} on the mode line @footnote{Remember
|
2
|
650 that the @samp{C} could be replaced with @samp{C++}, @samp{ObjC}, or
|
|
651 @samp{Java}.}. When hungry delete mode is enabled you would see
|
|
652 @samp{C/h} and when both modes are enabled, you'd see @samp{C/ah}.
|
0
|
653
|
|
654 @kindex C-c C-a
|
|
655 @kindex C-c C-d
|
|
656 @kindex C-c C-t
|
|
657 @findex c-toggle-hungry-state
|
|
658 @findex c-toggle-auto-state
|
|
659 @findex c-toggle-auto-hungry-state
|
|
660 @findex toggle-hungry-state (c-)
|
|
661 @findex toggle-auto-state (c-)
|
|
662 @findex toggle-auto-hungry-state (c-)
|
70
|
663 @code{cc-mode} provides keybindings which allow you to toggle the minor
|
2
|
664 modes on the fly while editing code. To toggle just the auto-newline
|
0
|
665 state, hit @kbd{C-c C-a} (@code{c-toggle-auto-state}). When you do
|
|
666 this, you should see the @samp{a} indicator either appear or disappear
|
|
667 on the modeline. Similarly, to toggle just the hungry-delete state, use
|
2
|
668 @kbd{C-c C-d} (@code{c-toggle-hungry-state}), and to toggle both states,
|
|
669 use @kbd{C-c C-t} (@code{c-toggle-auto-hungry-state}).
|
0
|
670
|
|
671 To set up the auto-newline and hungry-delete states to your preferred
|
|
672 values, you would need to add some lisp to your @file{.emacs} file that
|
|
673 called one of the @code{c-toggle-*-state} functions directly. When
|
|
674 called programmatically, each function takes a numeric value, where
|
|
675 a positive number enables the minor mode, a negative number disables the
|
|
676 mode, and zero toggles the current state of the mode.
|
|
677
|
|
678 So for example, if you wanted to enable both auto-newline and
|
|
679 hungry-delete for all your C file editing, you could add the following
|
|
680 to your @file{.emacs} file:
|
|
681 @example
|
|
682
|
70
|
683 (add-hook 'c-mode-common-hook '(lambda () (c-toggle-auto-hungry-state 1)))
|
0
|
684
|
|
685 @end example
|
|
686
|
|
687
|
|
688 @cindex electric characters
|
|
689
|
|
690 @menu
|
|
691 * Auto-newline insertion::
|
|
692 * Hungry-deletion of whitespace::
|
|
693 @end menu
|
|
694
|
|
695 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
696 @node Auto-newline insertion, Hungry-deletion of whitespace, , Minor Modes
|
|
697 @comment node-name, next, previous,up
|
|
698
|
|
699 @section Auto-newline insertion
|
|
700 @cindex Auto-newline insertion
|
|
701 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
702
|
|
703 @cindex electric commands
|
|
704 Auto-newline minor mode works by enabling certain @dfn{electric
|
|
705 commands}. Electric commands are typically bound to special characters
|
|
706 such as the left and right braces, colons, semi-colons, etc., which when
|
|
707 typed, perform some magic formatting in addition to inserting the typed
|
|
708 character. As a general rule, electric commands are only electric when
|
|
709 the following conditions apply:
|
|
710
|
|
711 @itemize @bullet
|
|
712 @item
|
|
713 Auto-newline minor mode is enabled, as evidenced by a @samp{C/a} or
|
|
714 @samp{C/ah} indicator on the modeline.
|
|
715
|
|
716 @cindex literal
|
|
717 @cindex syntactic whitespace
|
|
718 @item
|
|
719 The character was not typed inside of a literal @footnote{A
|
70
|
720 @dfn{literal} is defined in @code{cc-mode} as any comment,
|
|
721 string, or cpp macro definition. These constructs are also known as
|
|
722 @dfn{syntactic whitespace} since they are usually ignored when scanning
|
|
723 C code.}.
|
0
|
724
|
|
725 @item
|
|
726 @kindex C-u
|
|
727 No numeric argument was supplied to the command (i.e. it was typed as
|
|
728 normal, with no @kbd{C-u} prefix).
|
|
729
|
|
730 @end itemize
|
|
731
|
70
|
732 Certain other conditions may apply on a language specific basis. For
|
0
|
733 example, the second slash (@kbd{/}) of a C++ style line comment is
|
|
734 electric in @code{c++-mode}, @code{objc-mode}, and @code{java-mode}, but
|
70
|
735 not in @code{c-mode}.
|
0
|
736
|
|
737
|
|
738 @menu
|
|
739 * Hanging Braces::
|
|
740 * Hanging Colons::
|
|
741 * Hanging Semi-colons and commas::
|
|
742 * Other electric commands::
|
|
743 * Clean-ups::
|
|
744 @end menu
|
|
745
|
|
746 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
747 @node Hanging Braces, Hanging Colons, , Auto-newline insertion
|
|
748 @comment node-name, next, previous,up
|
|
749
|
|
750 @subsection Hanging Braces
|
|
751 @cindex Hanging Braces
|
|
752 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
753
|
|
754 @findex c-electric-brace
|
|
755 @findex electric-brace (c-)
|
|
756 @vindex c-hanging-braces-alist
|
|
757 @vindex hanging-braces-alist (c-)
|
|
758 @vindex c-offsets-alist
|
|
759 @vindex offsets-alist (c-)
|
|
760 When you type either an open or close brace (i.e. @kbd{@{} or @kbd{@}}),
|
|
761 the electric command @code{c-electric-brace} gets run. This command has
|
|
762 two electric formatting behaviors. First, it will perform some
|
|
763 re-indentation of the line the brace was typed on, and second, it will
|
|
764 add various newlines before and/or after the typed brace.
|
|
765 Re-indentation occurs automatically whenever the electric behavior is
|
|
766 enabled. If the brace ends up on a line other than the one it was typed
|
2
|
767 on, then that line is also indented according to
|
0
|
768 @code{c-offsets-alist}.
|
|
769
|
|
770 @cindex class-open syntactic symbol
|
|
771 @cindex class-close syntactic symbol
|
|
772 @cindex defun-open syntactic symbol
|
|
773 @cindex defun-close syntactic symbol
|
|
774 @cindex inline-open syntactic symbol
|
|
775 @cindex inline-close syntactic symbol
|
|
776 @cindex brace-list-open syntactic symbol
|
|
777 @cindex brace-list-close syntactic symbol
|
|
778 @cindex brace-list-intro syntactic symbol
|
|
779 @cindex brace-list-entry syntactic symbol
|
|
780 @cindex block-open syntactic symbol
|
|
781 @cindex block-close syntactic symbol
|
|
782 @cindex substatement-open syntactic symbol
|
|
783 @cindex statement-case-open syntactic symbol
|
2
|
784 @cindex extern-lang-open syntactic symbol
|
|
785 @cindex extern-lang-close syntactic symbol
|
0
|
786
|
|
787 The insertion of newlines is controlled by the
|
|
788 @code{c-hanging-braces-alist} variable. This variable contains a
|
|
789 mapping between syntactic symbols related to braces, and a list of
|
|
790 places to insert a newline. The syntactic symbols that are useful for
|
|
791 this list are: @code{class-open}, @code{class-close}, @code{defun-open},
|
|
792 @code{defun-close}, @code{inline-open}, @code{inline-close},
|
|
793 @code{brace-list-open}, @code{brace-list-close},
|
|
794 @code{brace-list-intro}, @code{brace-list-entry}, @code{block-open},
|
2
|
795 @code{block-close}, @code{substatement-open},
|
|
796 @code{statement-case-open},
|
|
797 @code{extern-lang-open}, and @code{extern-lang-close}.
|
|
798 @xref{Syntactic Symbols} for a more
|
0
|
799 detailed description of these syntactic symbols.
|
|
800
|
|
801 @cindex custom indentation function
|
|
802 The value associated with each syntactic symbol in this association list
|
|
803 is called an @var{ACTION} which can be either a function or a list.
|
|
804 @xref{Custom Brace and Colon Hanging} for a more detailed discussion of
|
|
805 using a function as a brace hanging @var{ACTION}.
|
|
806
|
2
|
807 When the @var{ACTION} is a list, it can contain any combination of the
|
70
|
808 symbols @code{before} and @code{after}, directing @code{cc-mode} where to
|
0
|
809 put newlines in relationship to the brace being inserted. Thus, if the
|
|
810 list contains only the symbol @code{after}, then the brace is said to
|
|
811 @dfn{hang} on the right side of the line, as in:
|
|
812 @example
|
|
813 @group
|
|
814
|
|
815 // here, open braces always `hang'
|
|
816 void spam( int i ) @{
|
|
817 if( i == 7 ) @{
|
|
818 dosomething(i);
|
|
819 @}
|
|
820 @}
|
|
821
|
|
822
|
|
823 @end group
|
|
824 @end example
|
|
825
|
|
826 When the list contains both @code{after} and @code{before}, the braces
|
|
827 will appear on a line by themselves, as shown by the close braces in the
|
|
828 above example. The list can also be empty, in which case no newlines
|
|
829 are added either before or after the brace.
|
|
830
|
|
831 For example, the default value of @code{c-hanging-braces-alist} is:
|
|
832 @example
|
|
833 @group
|
|
834
|
|
835 (defvar c-hanging-braces-alist '((brace-list-open)
|
|
836 (substatement-open after)
|
2
|
837 (block-close . c-snug-do-while)
|
|
838 (extern-lang-open after)))
|
0
|
839
|
|
840 @end group
|
|
841 @end example
|
|
842
|
|
843 @noindent
|
|
844 which says that @code{brace-list-open} braces should both hang on the
|
|
845 right side, and allow subsequent text to follow on the same line as the
|
2
|
846 brace. Also, @code{substatement-open} and @code{extern-lang-open}
|
|
847 braces should hang on the right side, but subsequent text should follow
|
|
848 on the next line. Here, in the @code{block-close} entry, you also see
|
|
849 an example of using a function as an @var{ACTION}.
|
0
|
850
|
|
851
|
|
852 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
853 @node Hanging Colons, Hanging Semi-colons and commas, Hanging Braces, Auto-newline insertion
|
|
854 @comment node-name, next, previous,up
|
|
855
|
|
856 @subsection Hanging Colons
|
|
857 @cindex Hanging Colons
|
|
858 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
859
|
|
860 @vindex hanging-colons-alist (c-)
|
|
861 @vindex c-hanging-colons-alist
|
|
862 Using a mechanism similar to brace hanging (see @ref{Hanging Braces}),
|
|
863 colons can also be made to hang using the variable
|
|
864 @code{c-hanging-colons-alist}. The syntactic symbols appropriate for
|
|
865 this assocation list are: @code{case-label}, @code{label},
|
|
866 @code{access-label}, @code{member-init-intro}, and @code{inher-intro}.
|
2
|
867 Note however, that for @code{c-hanging-colons-alist} @var{ACTION}s as
|
|
868 functions are not supported. See also @ref{Custom Brace and Colon
|
|
869 Hanging} for details.
|
0
|
870
|
|
871 @cindex clean-ups
|
|
872 In C++, double-colons are used as a scope operator but because these
|
|
873 colons always appear right next to each other, newlines before and after
|
|
874 them are controlled by a different mechanism, called @dfn{clean-ups} in
|
70
|
875 @code{cc-mode}. @xref{Clean-ups} for details.
|
0
|
876
|
|
877
|
|
878 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
879 @node Hanging Semi-colons and commas, Other electric commands, Hanging Colons, Auto-newline insertion
|
|
880 @comment node-name, next, previous,up
|
|
881
|
|
882 @subsection Hanging Semi-colons and commas
|
|
883 @cindex Hanging Semi-colons and commas
|
|
884 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
885
|
70
|
886 Semicolons and commas are also electric in @code{cc-mode}, but since
|
0
|
887 these characters do not correspond directly to syntactic symbols, a
|
|
888 different mechanism is used to determine whether newlines should be
|
|
889 automatically inserted after these characters. @xref{Customizing
|
|
890 Semi-colons and Commas} for details.
|
|
891
|
|
892
|
|
893 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
894 @node Other electric commands, Clean-ups, Hanging Semi-colons and commas, Auto-newline insertion
|
|
895 @comment node-name, next, previous,up
|
|
896
|
|
897 @subsection Other electric commands
|
|
898 @cindex Other electric commands
|
|
899 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
900
|
|
901 @kindex #
|
|
902 @findex c-electric-pound
|
|
903 @vindex c-electric-pound-behavior
|
|
904 @findex electric-pound (c-)
|
|
905 @vindex electric-pound-behavior (c-)
|
|
906 @vindex c-offsets-alist
|
|
907 @vindex offsets-alist (c-)
|
2
|
908 A few other keys also provide electric behavior. For example
|
|
909 @kbd{#} (@code{c-electric-pound}) is electric when typed as
|
0
|
910 the first non-whitespace character on a line. In this case, the
|
|
911 variable @code{c-electric-pound-behavior} is consulted for the electric
|
|
912 behavior. This variable takes a list value, although the only element
|
|
913 currently defined is @code{alignleft}, which tells this command to force
|
|
914 the @samp{#} character into column zero. This is useful for entering
|
|
915 cpp macro definitions.
|
|
916
|
|
917 @findex c-electric-star
|
|
918 @findex c-electric-slash
|
|
919 @findex electric-star (c-)
|
|
920 @findex electric-slash (c-)
|
|
921 @cindex comment-only line
|
2
|
922 Stars and slashes (i.e. @kbd{*} and @kbd{/}, @code{c-electric-star} and
|
|
923 @code{c-electric-slash} respectively) are also electric under
|
0
|
924 certain circumstances. If a star is inserted as the second character of
|
|
925 a C style block comment on a @dfn{comment-only} line, then the comment
|
|
926 delimiter is indented as defined by @code{c-offsets-alist}. A
|
|
927 comment-only line is defined as a line which contains only a comment, as
|
|
928 in:
|
|
929 @example
|
|
930 @group
|
|
931
|
|
932 void spam( int i )
|
|
933 @{
|
|
934 // this is a comment-only line...
|
|
935 if( i == 7 ) // but this is not
|
|
936 @{
|
|
937 dosomething(i);
|
|
938 @}
|
|
939 @}
|
|
940
|
|
941 @end group
|
|
942 @end example
|
|
943
|
|
944 Likewise, if a slash is inserted as the second slash in a C++ style line
|
|
945 comment (also only on a comment-only line), then the line is indented as
|
|
946 defined by @code{c-offsets-alist}.
|
|
947
|
2
|
948 @findex c-electric-lt-gt
|
|
949 @findex electric-lt-gt (c-)
|
|
950 @kindex <
|
|
951 @kindex >
|
|
952 Less-than and greater-than signs (@code{c-electric-lt-gt}) are also
|
|
953 electric, but only in C++ mode. Hitting the second of two @kbd{<} or
|
|
954 @kbd{>} keys re-indents the line if it is a C++ style stream operator.
|
|
955
|
0
|
956
|
|
957 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
958 @node Clean-ups, , Other electric commands, Auto-newline insertion
|
|
959 @comment node-name, next, previous,up
|
|
960
|
|
961 @subsection Clean-ups
|
|
962 @cindex Clean-ups
|
|
963 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
964
|
|
965 @dfn{Clean-ups} are a mechanism complementary to colon and brace
|
|
966 hanging. On the surface, it would seem that clean-ups overlap the
|
|
967 functionality provided by the @code{c-hanging-*-alist} variables, and
|
|
968 similarly, clean-ups are only enabled when auto-newline minor mode is
|
|
969 enabled. Clean-ups are used however to adjust code ``after-the-fact'',
|
|
970 i.e. to eliminate some whitespace that isn't inserted by electric
|
|
971 commands, or whitespace that contains intervening constructs.
|
|
972
|
|
973 @cindex literal
|
70
|
974 You can configure @code{cc-mode}'s clean-ups by setting the variable
|
0
|
975 @code{c-cleanup-list}, which is a list of clean-up symbols. By default,
|
70
|
976 @code{cc-mode} cleans up only the @code{scope-operator} construct, which
|
0
|
977 is necessary for proper C++ support. Note that clean-ups are only
|
|
978 performed when the construct does not occur within a literal (see
|
|
979 @ref{Auto-newline insertion}), and when there is nothing but whitespace
|
|
980 appearing between the individual components of the construct.
|
|
981
|
|
982 @vindex c-cleanup-list
|
|
983 @vindex cleanup-list (c-)
|
70
|
984 There are currently only five specific constructs that @code{cc-mode}
|
0
|
985 can clean up, as indicated by these symbols:
|
|
986
|
|
987 @itemize @bullet
|
|
988 @item
|
2
|
989 @code{brace-else-brace} --- cleans up @samp{@} else @{} constructs by
|
0
|
990 placing the entire construct on a single line. Clean-up occurs when the
|
|
991 open brace after the @samp{else} is typed. So for example, this:
|
|
992 @example
|
|
993 @group
|
|
994
|
|
995 void spam(int i)
|
|
996 @{
|
|
997 if( i==7 )
|
|
998 @{
|
|
999 dosomething();
|
|
1000 @}
|
|
1001 else
|
|
1002 @{
|
|
1003
|
|
1004 @end group
|
|
1005 @end example
|
|
1006 @noindent
|
|
1007 appears like this after the open brace is typed:
|
|
1008 @example
|
|
1009 @group
|
|
1010
|
|
1011 void spam(int i)
|
|
1012 @{
|
|
1013 if( i==7 ) @{
|
|
1014 dosomething();
|
|
1015 @} else @{
|
|
1016
|
|
1017 @end group
|
|
1018 @end example
|
|
1019
|
|
1020 @item
|
2
|
1021 @code{empty-defun-braces} --- cleans up braces following a top-level
|
0
|
1022 function or class definition that contains no body. Clean up occurs
|
|
1023 when the closing brace is typed. Thus the following:
|
|
1024 @example
|
|
1025 @group
|
|
1026
|
|
1027 class Spam
|
|
1028 @{
|
|
1029 @}
|
|
1030
|
|
1031 @end group
|
|
1032 @end example
|
|
1033 @noindent
|
|
1034 is transformed into this when the close brace is typed:
|
|
1035 @example
|
|
1036 @group
|
|
1037
|
|
1038 class Spam
|
|
1039 @{@}
|
|
1040
|
|
1041 @end group
|
|
1042 @end example
|
|
1043
|
|
1044 @item
|
2
|
1045 @code{defun-close-semi} --- cleans up the terminating semi-colon on
|
0
|
1046 top-level function or class definitions when they follow a close
|
|
1047 brace. Clean up occurs when the semi-colon is typed.
|
|
1048 So for example, the following:
|
|
1049 @example
|
|
1050 @group
|
|
1051
|
|
1052 class Spam
|
|
1053 @{
|
|
1054 @}
|
|
1055 ;
|
|
1056
|
|
1057 @end group
|
|
1058 @end example
|
|
1059 @noindent
|
|
1060 is transformed into this when the semi-colon is typed:
|
|
1061
|
|
1062 @example
|
|
1063 @group
|
|
1064
|
|
1065 class Spam
|
|
1066 @{
|
|
1067 @};
|
|
1068
|
|
1069 @end group
|
|
1070 @end example
|
|
1071
|
|
1072 @item
|
2
|
1073 @code{list-close-comma} --- cleans up commas following braces in array
|
0
|
1074 and aggregate initializers. Clean up occurs when the comma is typed.
|
|
1075
|
|
1076 @item
|
2
|
1077 @code{scope-operator} --- cleans up double colons which may designate a
|
0
|
1078 C++ scope operator split across multiple lines@footnote{Certain C++
|
|
1079 constructs introduce ambiguous situations, so @code{scope-operator}
|
|
1080 clean-ups may not always be correct. This usually only occurs when
|
|
1081 scoped identifiers appear in switch label tags.}. Clean up occurs when
|
|
1082 the second colon is typed. You will always want @code{scope-operator}
|
|
1083 in the @code{c-cleanup-list} when you are editing C++ code.
|
|
1084
|
|
1085 @end itemize
|
|
1086
|
|
1087
|
|
1088 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
1089 @node Hungry-deletion of whitespace, , Auto-newline insertion, Minor Modes
|
|
1090 @comment node-name, next, previous,up
|
|
1091
|
|
1092 @section Hungry-deletion of whitespace
|
|
1093 @cindex Hungry-deletion of whitespace
|
|
1094 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
1095
|
|
1096 Hungry deletion of whitespace, or as it more commonly called,
|
|
1097 @dfn{hungry-delete mode}, is a simple feature that some people find
|
|
1098 extremely useful. In fact, you might find yourself wanting
|
|
1099 hungry-delete in @strong{all} your editing modes!
|
|
1100
|
|
1101 @kindex DEL
|
|
1102 In a nutshell, when hungry-delete mode is enabled, hitting the @kbd{DEL}
|
|
1103 character will consume all preceding whitespace, including newlines and
|
|
1104 tabs. This can really cut down on the number of @kbd{DEL}'s you have to
|
|
1105 type if, for example you made a mistake on the preceding line.
|
|
1106
|
|
1107 @findex c-electric-delete
|
|
1108 @findex electric-delete (c-)
|
|
1109 @vindex c-delete-function
|
|
1110 @vindex delete-function (c-)
|
|
1111 @cindex literal
|
70
|
1112 By default, @code{cc-mode} actually runs the command
|
0
|
1113 @code{c-electric-delete} when you hit @kbd{DEL}. When this command is
|
|
1114 used to delete a single character (i.e. when it is called interactively
|
|
1115 with no numeric argument), it really runs the function contained in the
|
|
1116 variable @code{c-delete-function}. This function is called with a
|
|
1117 single argument, which is the number of characters to delete.
|
|
1118 @code{c-delete-function} is also called when the @kbd{DEL} key is typed
|
|
1119 inside a literal (see @ref{Auto-newline insertion}. Inside a literal,
|
|
1120 @code{c-electric-delete} is not electric, which is typical of all the
|
|
1121 so-called electric commands.
|
|
1122
|
|
1123
|
|
1124 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
2
|
1125 @node Commands, Customizing Indentation, Minor Modes, Top
|
0
|
1126 @comment node-name, next, previous,up
|
|
1127
|
2
|
1128 @chapter Commands
|
|
1129 @cindex Commands
|
|
1130 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
1131
|
|
1132 @menu
|
|
1133 * Indentation Commands::
|
|
1134 * Other Commands::
|
|
1135 @end menu
|
|
1136
|
|
1137 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
1138 @node Indentation Commands, Other Commands, , Commands
|
|
1139 @comment node-name, next, previous,up
|
|
1140
|
|
1141 @section Indentation Commands
|
0
|
1142 @cindex Indentation Commands
|
|
1143 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
1144
|
70
|
1145 @findex c-set-style
|
|
1146 @findex set-style (c-)
|
0
|
1147 Various commands are provided which allow you to conveniently re-indent
|
2
|
1148 C constructs. There are several things to
|
0
|
1149 note about these indentation commands. First, when you
|
2
|
1150 change your programming style, either interactively or through some
|
0
|
1151 other means, your file does @emph{not} automatically get re-indented.
|
|
1152 When you change style parameters, you will typically need to reformat
|
|
1153 the line, expression, or buffer to see the effects of your changes.
|
|
1154
|
|
1155 @cindex c-hanging- functions
|
|
1156 @findex c-hanging-braces-alist
|
|
1157 @findex hanging-braces-alist (c-)
|
|
1158 Second, changing some variables have no effect on existing code, even
|
2
|
1159 when you do re-indent. For example, the @code{c-hanging-*} variables
|
70
|
1160 and @code{c-cleanup-list} only affect new code as it is typed in. So
|
|
1161 for example, changing @code{c-hanging-braces-alist} and re-indenting the
|
|
1162 buffer will not adjust placement of braces already in the file.
|
0
|
1163
|
|
1164 @vindex c-progress-interval
|
|
1165 @vindex progress-interval (c-)
|
|
1166 Third, re-indenting large portions of code is currently rather
|
|
1167 inefficient. Improvements have been made since previous releases of
|
70
|
1168 @code{cc-mode}, and much more radical improvements are planned, but for
|
|
1169 now you need to be aware of this @footnote{In particular, I have had
|
|
1170 people complain about the speed that @code{cc-mode} re-indents
|
|
1171 @code{lex(1)} output. Lex, yacc, and other code generators usually
|
|
1172 output some pretty perversely formatted code. @emph{Don't} try to
|
|
1173 indent this stuff with @code{cc-mode}!}. Some provision has been made
|
|
1174 to at least inform you as to the progress of the re-indentation. The
|
|
1175 variable @code{c-progress-interval} controls how often a progress
|
|
1176 message is displayed. Set this variable to @code{nil} to inhibit
|
|
1177 progress messages. Note that this feature only works with Emacs 19.
|
0
|
1178
|
|
1179 Also, except as noted below, re-indentation is always driven by the
|
|
1180 same mechanisms that control on-the-fly indentation of code. @xref{New
|
|
1181 Indentation Engine} for details.
|
|
1182
|
|
1183 @findex c-indent-command
|
|
1184 @findex indent-command (c-)
|
|
1185 @vindex c-tab-always-indent
|
|
1186 @vindex tab-always-indent (c-)
|
|
1187 @kindex TAB
|
|
1188 @cindex literal
|
2
|
1189 @vindex indent-tabs-mode
|
|
1190 @vindex c-insert-tab-function
|
|
1191 @vindex insert-tab-function (c-)
|
|
1192 @findex tab-to-tab-stop
|
0
|
1193 To indent a single line of code, use @kbd{TAB}
|
|
1194 (@code{c-indent-command}). The behavior of this command is controlled
|
|
1195 by the variable @code{c-tab-always-indent}. When this variable is
|
|
1196 @code{t}, @kbd{TAB} always just indents the current line. When
|
70
|
1197 @code{nil}, the line is indented only if Point is at the left margin, or
|
2
|
1198 on or before the first non-whitespace character on the line, otherwise
|
|
1199 @emph{something else happens}@footnote{Actually what happens is that the
|
70
|
1200 function stored in the variable @code{c-insert-tab-function} is called.
|
2
|
1201 Normally this just inserts a real tab character, or the equivalent
|
|
1202 number of spaces, depending on the setting of the variable
|
|
1203 @code{indent-tabs-mode}. If you preferred, you could set
|
|
1204 @code{c-insert-tab-function} to @code{tab-to-tab-stop} for example.}.
|
|
1205 If the value of @code{c-tab-always-indent} is something other than
|
|
1206 @code{t} or @code{nil} (e.g. @code{'other}), then a real tab
|
|
1207 character@footnote{The caveat about @code{indent-tabs-mode} in the
|
70
|
1208 previous footnote also applies here.} is inserted only when Point is
|
2
|
1209 inside a literal (see @ref{Auto-newline insertion}), otherwise the line
|
|
1210 is indented.
|
0
|
1211
|
|
1212 @kindex M-C-q
|
|
1213 @findex c-indent-exp
|
|
1214 @findex indent-exp (c-)
|
|
1215 To indent an entire balanced brace or parenthesis expression, use
|
70
|
1216 @kbd{M-C-q} (@code{c-indent-exp}). Note that Point should be on
|
0
|
1217 the opening brace or parenthesis of the expression you want to indent.
|
|
1218
|
|
1219 @kindex C-c C-q
|
|
1220 @findex c-indent-defun
|
|
1221 @findex indent-defun (c-)
|
|
1222 Another very convenient keystroke is @kbd{C-c C-q}
|
|
1223 (@code{c-indent-defun}) when re-indents the entire top-level function or
|
70
|
1224 class definition that encompases Point. It leaves Point at the
|
0
|
1225 same position within the buffer.
|
|
1226
|
|
1227 @kindex M-C-\
|
|
1228 @findex indent-region
|
|
1229 To indent any arbitrary region of code, use @kbd{M-C-\}
|
|
1230 (@code{indent-region}). This is a standard Emacs command, specially
|
70
|
1231 tailored for C code in a @code{cc-mode} buffer. Note that of course,
|
|
1232 Point and Mark must delineate the region you
|
0
|
1233 want to indent.
|
|
1234
|
|
1235 @kindex M-C-h
|
|
1236 @findex c-mark-function
|
|
1237 @findex mark-function (c-)
|
|
1238 While not strictly an indentation function, @kbd{M-C-h}
|
|
1239 (@code{c-mark-function}) is useful for marking the current top-level
|
|
1240 function or class definition as the current region.
|
|
1241
|
2
|
1242 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
1243 @node Other Commands, , Indentation Commands, Commands
|
|
1244 @comment node-name, next, previous,up
|
|
1245
|
|
1246 @section Other Commands
|
|
1247 @cindex Other Commands
|
|
1248 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
1249
|
70
|
1250 @code{cc-mode} contains other useful command for moving around in C
|
2
|
1251 code.
|
|
1252
|
|
1253 @table @code
|
|
1254 @item C-c C-u (c-up-conditional)
|
|
1255 @kindex C-c C-u
|
|
1256 @findex c-up-conditional
|
|
1257 @findex up-conditional (c-)
|
70
|
1258 Move Point back to the containing preprocessor conditional, leaving the
|
|
1259 Mark behind. A prefix argument acts as a repeat count. With a negative
|
|
1260 argument, move Point forward to the end of the containing
|
2
|
1261 preprocessor conditional. When going backwards, @code{#elif} is treated
|
|
1262 like @code{#else} followed by @code{#if}. When going forwards,
|
|
1263 @code{#elif} is ignored.@refill
|
|
1264
|
|
1265 @item C-c C-p (c-backward-conditional)
|
|
1266 @kindex C-c C-p
|
|
1267 @findex c-backward-conditional
|
|
1268 @findex backward-conditional (c-)
|
70
|
1269 Move Point back over a preprocessor conditional, leaving Mark
|
2
|
1270 behind. A prefix argument acts as a repeat count. With a negative
|
|
1271 argument, move forward.
|
|
1272
|
|
1273 @item C-c C-n (c-forward-conditional)
|
|
1274 @kindex C-c C-n
|
|
1275 @findex c-forward-conditional
|
|
1276 @findex forward-conditional (c-)
|
70
|
1277 Move Point forward across a preprocessor conditional, leaving Mark
|
2
|
1278 behind. A prefix argument acts as a repeat count. With a negative
|
|
1279 argument, move backward.
|
|
1280
|
|
1281 @item M-a (c-beginning-of-statement)
|
|
1282 @kindex ESC a
|
|
1283 @findex c-beginning-of-statement
|
|
1284 @findex beginning-of-statement (c-)
|
70
|
1285 Move Point to the beginning of the innermost C statement. If Point is
|
2
|
1286 already at the beginning of a statement, it moves to the beginning of
|
|
1287 the preceding statement. With prefix argument @var{n}, move back
|
|
1288 @var{n} @minus{} 1 statements.
|
|
1289
|
70
|
1290 If Point is within a string or comment, or next to a comment (only
|
|
1291 whitespace between them), this command moves by sentences instead of
|
|
1292 statements.
|
|
1293
|
|
1294 When called from a program, this function takes two optional arguments:
|
|
1295 the numeric prefix argument, and a buffer position limit (don't move
|
|
1296 back before that place).
|
2
|
1297
|
|
1298 @item M-e (c-end-of-statement)
|
|
1299 @kindex ESC e
|
|
1300 @findex c-end-of-statement
|
|
1301 @findex end-of-statement (c-)
|
70
|
1302 Move Point to the end of the innermost C statement. If Point is at the
|
2
|
1303 end of a statement, move to the end of the next statement. With prefix
|
|
1304 argument @var{n}, move forward @var{n} @minus{} 1 statements.
|
|
1305
|
70
|
1306 If Point is within a string or comment, or next to a comment (only
|
|
1307 whitespace between them), this command moves by sentences instead of
|
|
1308 statements.
|
|
1309
|
|
1310 When called from a program, this function takes two optional arguments:
|
|
1311 the numeric prefix argument, and a buffer position limit (don't move
|
|
1312 past that place).
|
2
|
1313
|
|
1314 @item M-x c-forward-into-nomenclature
|
|
1315 @findex c-forward-into-nomenclature
|
|
1316 @findex forward-into-nomenclature (c-)
|
|
1317 A popular programming style, especially for object-oriented languages
|
|
1318 such as C++ is to write symbols in a mixed case format, where the first
|
|
1319 letter of each word is capitalized, and not separated by underscores.
|
|
1320 E.g. @samp{SymbolsWithMixedCaseAndNoUnderlines}.
|
|
1321
|
70
|
1322 This command moves Point forward to end of a C++ nomenclature
|
2
|
1323 section or word. With prefix argument @var{n}, move @var{n} times.
|
|
1324
|
|
1325 @item M-x c-backward-into-nomenclature
|
|
1326 @findex c-backward-into-nomenclature
|
|
1327 @findex backward-into-nomenclature (c-)
|
70
|
1328 Move Point backward to beginning of a C++ nomenclature
|
2
|
1329 section or word. With prefix argument @var{n}, move @var{n} times. If
|
|
1330 @var{n} is negative, move forward.
|
|
1331
|
|
1332 @kindex C-c :
|
|
1333 @findex c-scope-operator
|
|
1334 @findex scope-operator (c-)
|
|
1335 @item C-c : (c-scope-operator)
|
|
1336 In C++, it is also sometimes desirable to insert the double-colon scope
|
|
1337 operator without performing the electric behavior of colon insertion.
|
|
1338 @kbd{C-c :} does just this.
|
|
1339
|
|
1340 @end table
|
0
|
1341
|
|
1342 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
2
|
1343 @node Customizing Indentation, Syntactic Symbols, Commands, Top
|
0
|
1344 @comment node-name, next, previous,up
|
|
1345
|
|
1346 @chapter Customizing Indentation
|
|
1347 @cindex Customizing Indentation
|
|
1348 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
1349
|
|
1350 @cindex c-set-offset
|
|
1351 @cindex set-offset (c-)
|
|
1352 The @code{c-offsets-alist} variable is where you customize all your
|
|
1353 indentations. You simply need to decide what additional offset you want
|
|
1354 to add for every syntactic symbol. You can use the command @kbd{C-c
|
|
1355 C-o} (@code{c-set-offset}) as the way to set offsets, both interactively
|
|
1356 and from your mode hook. Also, you can set up @emph{styles} of
|
|
1357 indentation just like in BOCM. Most likely, you'll
|
|
1358 find one of the pre-defined styles will suit your needs, but if not,
|
|
1359 this section will describe how to set up basic editing configurations.
|
|
1360 @xref{Styles} for an explanation of how to set up named styles.
|
|
1361
|
|
1362 @cindex c-basic-offset
|
|
1363 @cindex basic-offset (c-)
|
|
1364 As mentioned previously, the variable @code{c-offsets-alist} is an
|
2
|
1365 association list of syntactic symbols and the offsets to be applied for
|
70
|
1366 those symbols. In fact, these offset values can be any of an integer,
|
|
1367 a function or variable name, or one of the following symbols: @code{+},
|
|
1368 @code{-}, @code{++}, @code{--}, @code{*}, or @code{/}. These symbols
|
|
1369 describe offset in multiples of the value of the variable
|
|
1370 @code{c-basic-offset}. By defining a style's indentation in terms of
|
|
1371 this fundamental variable, you can change the amount of whitespace given
|
|
1372 to an indentation level while leaving the same relationship between
|
|
1373 levels. Here are the values that the special
|
0
|
1374 symbols correspond to:
|
|
1375
|
2
|
1376 @table @code
|
|
1377
|
|
1378 @item +
|
|
1379 @code{c-basic-offset} times 1
|
|
1380 @item -
|
|
1381 @code{c-basic-offset} times -1
|
|
1382 @item ++
|
|
1383 @code{c-basic-offset} times 2
|
|
1384 @item --
|
|
1385 @code{c-basic-offset} times -2
|
|
1386 @item *
|
|
1387 @code{c-basic-offset} times 0.5
|
|
1388 @item /
|
|
1389 @code{c-basic-offset} times -0.5
|
|
1390
|
|
1391 @end table
|
0
|
1392
|
|
1393 @noindent
|
|
1394 So, for example, because most of the default offsets are defined in
|
|
1395 terms of @code{+}, @code{-}, and @code{0}, if you like the general
|
|
1396 indentation style, but you use 4 spaces instead of 2 spaces per level,
|
|
1397 you can probably achieve your style just by changing
|
|
1398 @code{c-basic-offset} like so (in your @file{.emacs} file)@footnote{The
|
|
1399 reason you need to use @code{setq-default} instead of @code{setq} is
|
70
|
1400 that @code{c-basic-offset} is a buffer local variable, as are most of
|
|
1401 the @code{cc-mode} configuration variables. If you were to put this
|
|
1402 code in, e.g. your @code{c-mode-common-hook} function, you could use
|
|
1403 @code{setq}.}:
|
0
|
1404 @example
|
|
1405
|
|
1406 (setq-default c-basic-offset 4)
|
|
1407
|
|
1408 @end example
|
|
1409
|
|
1410 @noindent
|
|
1411 This would change
|
|
1412 @example
|
|
1413 @group
|
|
1414
|
|
1415 int add( int val, int incr, int doit )
|
|
1416 @{
|
2
|
1417 if( doit )
|
|
1418 @{
|
|
1419 return( val + incr );
|
|
1420 @}
|
|
1421 return( val );
|
|
1422 @}
|
|
1423
|
|
1424 @end group
|
|
1425 @end example
|
|
1426 @noindent
|
|
1427 to
|
|
1428 @example
|
|
1429 @group
|
|
1430
|
|
1431 int add( int val, int incr, int doit )
|
|
1432 @{
|
0
|
1433 if( doit )
|
|
1434 @{
|
|
1435 return( val + incr );
|
|
1436 @}
|
|
1437 return( val );
|
|
1438 @}
|
|
1439
|
|
1440 @end group
|
|
1441 @end example
|
|
1442
|
|
1443
|
|
1444 To change indentation styles more radically, you will want to change the
|
|
1445 value associated with the syntactic symbols in the
|
|
1446 @code{c-offsets-alist} variable. First, I'll show you how to do that
|
|
1447 interactively, then I'll describe how to make changes to your
|
|
1448 @file{.emacs} file so that your changes are more permanent.
|
|
1449
|
|
1450 @menu
|
|
1451 * Interactive Customization::
|
|
1452 * Permanent Customization::
|
|
1453 * Styles::
|
|
1454 * Advanced Customizations::
|
|
1455 @end menu
|
|
1456
|
|
1457 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
1458 @node Interactive Customization, Permanent Customization, , Customizing Indentation
|
|
1459 @comment node-name, next, previous,up
|
|
1460
|
|
1461 @section Interactive Customization
|
|
1462 @cindex Interactive Customization
|
|
1463 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
1464
|
|
1465 As an example of how to customize indentation, let's change the
|
2
|
1466 style of this example@footnote{In this an subsequent examples, the
|
|
1467 original code is formatted using the @samp{gnu} style unless otherwise
|
|
1468 indicated. @xref{Styles}.}:
|
0
|
1469 @example
|
|
1470 @group
|
|
1471
|
|
1472 1: int add( int val, int incr, int doit )
|
|
1473 2: @{
|
2
|
1474 3: if( doit )
|
|
1475 4: @{
|
|
1476 5: return( val + incr );
|
|
1477 6: @}
|
|
1478 7: return( val );
|
0
|
1479 8: @}
|
|
1480
|
|
1481 @end group
|
|
1482 @end example
|
|
1483 @noindent
|
|
1484 to:
|
|
1485 @example
|
|
1486 @group
|
|
1487
|
|
1488 1: int add( int val, int incr, int doit )
|
|
1489 2: @{
|
2
|
1490 3: if( doit )
|
|
1491 4: @{
|
|
1492 5: return( val + incr );
|
|
1493 6: @}
|
|
1494 7: return( val );
|
0
|
1495 8: @}
|
|
1496
|
|
1497 @end group
|
|
1498 @end example
|
|
1499
|
|
1500 In other words, we want to change the indentation of braces that open a
|
|
1501 block following a condition so that the braces line up under the
|
|
1502 conditional, instead of being indented. Notice that the construct we
|
|
1503 want to change starts on line 4. To change the indentation of a line,
|
2
|
1504 we need to see which syntactic components affect the offset calculations
|
0
|
1505 for that line. Hitting @kbd{C-c C-s} on line 4 yields:
|
|
1506 @example
|
|
1507
|
2
|
1508 ((substatement-open . 44))
|
0
|
1509
|
|
1510 @end example
|
|
1511
|
|
1512 @findex c-set-offset
|
|
1513 @findex set-offset (c-)
|
|
1514 @kindex C-c C-o
|
|
1515 @noindent
|
|
1516 so we know that to change the offset of the open brace, we need to
|
|
1517 change the indentation for the @code{substatement-open} syntactic
|
|
1518 symbol. To do this interactively, just hit @kbd{C-c C-o}
|
|
1519 (@code{c-set-offset}). This prompts you for the syntactic symbol to
|
|
1520 change, providing a reasonable default. In this case, the default is
|
|
1521 @code{substatement-open}, which is just the syntactic symbol we want to
|
|
1522 change!
|
|
1523
|
70
|
1524 After you hit return, @code{cc-mode} will then prompt you for the new
|
0
|
1525 offset value, with the old value as the default. The default in this
|
2
|
1526 case is @samp{+}, but we want no extra indentation so enter
|
0
|
1527 @samp{0} and @kbd{RET}. This will associate the offset 0 with the
|
|
1528 syntactic symbol @code{substatement-open} in the @code{c-offsets-alist}
|
|
1529 variable.
|
|
1530
|
|
1531 @findex c-indent-defun
|
|
1532 @findex indent-defun (c-)
|
|
1533 @kindex C-c C-q
|
|
1534 To check your changes quickly, just hit @kbd{C-c C-q}
|
|
1535 (@code{c-indent-defun}) to reindent the entire function. The example
|
|
1536 should now look like:
|
|
1537 @example
|
|
1538 @group
|
|
1539
|
|
1540 1: int add( int val, int incr, int doit )
|
|
1541 2: @{
|
2
|
1542 3: if( doit )
|
|
1543 4: @{
|
|
1544 5: return( val + incr );
|
|
1545 6: @}
|
|
1546 7: return( val );
|
0
|
1547 8: @}
|
|
1548
|
|
1549 @end group
|
|
1550 @end example
|
|
1551
|
|
1552 Notice how just changing the open brace offset on line 4 is all we
|
|
1553 needed to do. Since the other affected lines are indented relative to
|
|
1554 line 4, they are automatically indented the way you'd expect. For more
|
|
1555 complicated examples, this may not always work. The general approach to
|
|
1556 take is to always start adjusting offsets for lines higher up in the
|
|
1557 file, then re-indent and see if any following lines need further
|
|
1558 adjustments.
|
|
1559
|
|
1560 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
1561 @node Permanent Customization, Styles, Interactive Customization, Customizing Indentation
|
|
1562 @comment node-name, next, previous,up
|
|
1563
|
70
|
1564 @section Permanent Indentation
|
|
1565 @cindex Permanent Indentation
|
0
|
1566 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
1567
|
|
1568 @vindex c-mode-common-hook
|
|
1569 @vindex c-mode-hook
|
|
1570 @vindex c++-mode-hook
|
|
1571 @vindex objc-mode-hook
|
|
1572 @vindex java-mode-hook
|
|
1573 @cindex hooks
|
|
1574 To make this change permanent, you need to add some lisp code to your
|
70
|
1575 @file{.emacs} file. @code{cc-mode} provides several hooks that you can
|
2
|
1576 use to customize the mode according to your coding style. Each language
|
70
|
1577 mode has its own hook, adhering to standard to Emacs major mode
|
2
|
1578 conventions. There is also one general hook:
|
|
1579
|
|
1580 @itemize @bullet
|
|
1581
|
|
1582 @item
|
|
1583 @code{c-mode-hook} --- for C buffers only
|
|
1584 @item
|
|
1585 @code{c++-mode-hook} --- for C++ buffers only
|
|
1586 @item
|
|
1587 @code{objc-mode-hook} --- for Objective-C buffers only
|
|
1588 @item
|
|
1589 @code{java-mode-hook} --- for Java buffers only
|
|
1590 @item
|
|
1591 @code{c-mode-common-hook} --- common across all languages
|
|
1592
|
|
1593 @end itemize
|
|
1594
|
|
1595 The language hooks get run as the last thing when you enter that
|
|
1596 language-specific mode. The @code{c-mode-common-hook} is run by all
|
|
1597 supported modes @emph{before} the language specific hook, and thus can
|
|
1598 contain customizations that are common across all languages. Most of
|
|
1599 the examples in this section will assume you are using the common
|
|
1600 hook@footnote{The interaction between @code{java-mode} and the hook
|
|
1601 variables is slightly different than for the other modes.
|
|
1602 @code{java-mode} sets the style (see @ref{Styles}) of the buffer to
|
|
1603 @samp{java} @emph{before} running the @code{c-mode-common-hook} or
|
|
1604 @code{java-mode-hook}. You need to be aware of this so any style
|
|
1605 settings in @code{c-mode-common-hook} doesn't clobber your Java style.}.
|
0
|
1606
|
|
1607 Here's a simplified example of what you can add to your @file{.emacs}
|
|
1608 file to make the changes described in the previous section
|
70
|
1609 (@ref{Interactive Customization}) more permanent. See the Emacs
|
|
1610 manuals for more information on customizing Emacs via hooks.
|
|
1611 @xref{Sample .emacs File} for a more complete sample @file{.emacs} file.
|
0
|
1612 @footnote{The use of @code{add-hook} in this example only works for
|
70
|
1613 Emacs 19. Workarounds are available if you are using Emacs 18.}
|
0
|
1614 @example
|
|
1615 @group
|
|
1616
|
|
1617 (defun my-c-mode-common-hook ()
|
|
1618 ;; my customizations for all of c-mode, c++-mode, objc-mode, java-mode
|
|
1619 (c-set-offset 'substatement-open 0)
|
|
1620 ;; other customizations can go here
|
|
1621 )
|
|
1622 (add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
|
|
1623
|
|
1624 @end group
|
|
1625 @end example
|
|
1626
|
|
1627 For complex customizations, you will probably want to set up a
|
|
1628 @emph{style} that groups all your customizations under a single
|
|
1629 name.
|
|
1630
|
|
1631 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
1632 @node Styles, Advanced Customizations, Permanent Customization, Customizing Indentation
|
|
1633 @comment node-name, next, previous,up
|
|
1634
|
|
1635 @section Styles
|
|
1636 @cindex Styles
|
|
1637 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
1638
|
|
1639 Most people only need to edit code formatted in just a few well-defined
|
|
1640 and consistent styles. For example, their organization might impose a
|
|
1641 ``blessed'' style that all its programmers must conform to. Similarly,
|
|
1642 people who work on GNU software will have to use the GNU coding style on
|
|
1643 C code. Some shops are more lenient, allowing some variety of coding
|
|
1644 styles, and as programmers come and go, there could be a number of
|
70
|
1645 styles in use. For this reason, @code{cc-mode} makes it convenient for
|
0
|
1646 you to set up logical groupings of customizations called @dfn{styles},
|
|
1647 associate a single name for any particular style, and pretty easily
|
2
|
1648 start editing new or existing code using these styles. This section
|
0
|
1649 describes how to set up styles and how to edit your C code using styles.
|
|
1650
|
|
1651 @menu
|
|
1652 * Built-in Styles::
|
|
1653 * Adding Styles::
|
|
1654 * File Styles::
|
|
1655 @end menu
|
|
1656
|
|
1657
|
|
1658 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
1659 @node Built-in Styles, Adding Styles, , Styles
|
|
1660 @comment node-name, next, previous,up
|
|
1661
|
|
1662 @subsection Built-in Styles
|
|
1663 @cindex Built-in Styles
|
|
1664 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
1665
|
70
|
1666 If you're lucky, one of @code{cc-mode}'s built-in styles might be just
|
|
1667 what you're looking for. Some of the most common C and C++ styles are
|
|
1668 already built-in. These include:
|
0
|
1669
|
|
1670 @itemize @bullet
|
|
1671 @item
|
|
1672 @cindex GNU style
|
2
|
1673 @code{gnu} --- coding style blessed by the Free Software Foundation
|
0
|
1674 for C code in GNU programs.
|
|
1675
|
|
1676 @item
|
|
1677 @cindex K&R style
|
2
|
1678 @code{k&r} --- The classic Kernighan and Ritchie style for C code.
|
0
|
1679
|
|
1680 @item
|
|
1681 @cindex BSD style
|
2
|
1682 @code{bsd} --- Also known as ``Allman style'' after Eric Allman.
|
|
1683
|
|
1684 @item
|
|
1685 @cindex Whitesmith style
|
|
1686 @code{whitesmith} --- Popularized by the examples that came with
|
|
1687 Whitesmiths C, an early commercial C compiler.
|
0
|
1688
|
|
1689 @item
|
|
1690 @cindex Stroustrup style
|
2
|
1691 @code{stroustrup} --- The classic Stroustrup style for C++ code.
|
0
|
1692
|
|
1693 @item
|
|
1694 @cindex Ellemtel style
|
2
|
1695 @code{ellemtel} --- Popular C++ coding standards as defined by
|
0
|
1696 ``Programming in C++, Rules and Recommendations'', Erik Nyquist and Mats
|
|
1697 Henricson, Ellemtel @footnote{This document is ftp'able from
|
|
1698 @code{euagate.eua.ericsson.se}}.
|
|
1699
|
|
1700 @item
|
|
1701 @cindex Java style
|
|
1702 @cindex java-mode
|
2
|
1703 @code{java} --- The style for editing Java code. Note that this style is
|
0
|
1704 automatically installed when you enter @code{java-mode}.
|
|
1705
|
|
1706 @end itemize
|
|
1707
|
|
1708 @findex c-set-style
|
|
1709 @findex set-style (c-)
|
|
1710 If you'd like to experiment with these built-in styles you can simply
|
70
|
1711 type the following in a @code{cc-mode} buffer:
|
0
|
1712 @example
|
|
1713 @group
|
|
1714
|
70
|
1715 @kbd{M-x c-set-style RET @var{STYLE-NAME} RET}
|
0
|
1716
|
|
1717 @end group
|
|
1718 @end example
|
|
1719 @noindent
|
70
|
1720 Note that all style names are case insensitive, even the ones you define.
|
0
|
1721
|
|
1722 Setting a style in this way does @emph{not} automatically re-indent your
|
|
1723 file. For commands that you can use to view the effect of your changes,
|
2
|
1724 see @ref{Commands}.
|
0
|
1725
|
|
1726 Once you find a built-in style you like, you can make the change
|
70
|
1727 permanent by adding a call to your @file{.emacs} file. Let's say for
|
2
|
1728 example that you want to use the @samp{ellemtel} style in all your
|
0
|
1729 files. You would add this:
|
|
1730 @example
|
|
1731 @group
|
|
1732
|
|
1733 (defun my-c-mode-common-hook ()
|
2
|
1734 ;; use Ellemtel style for all C like languages
|
0
|
1735 (c-set-style "ellemtel")
|
|
1736 ;; other customizations can go here
|
|
1737 )
|
|
1738 (add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
|
|
1739
|
|
1740 @end group
|
|
1741 @end example
|
|
1742
|
2
|
1743 There is one other special style you can use, called @samp{cc-mode}
|
|
1744 style. This style is special because all other styles implicitly
|
|
1745 inherit from it; in other words, whenever you set a style,
|
|
1746 @samp{cc-mode} is applied before the one you selected. This means
|
|
1747 your style need only define the differences between it and
|
|
1748 @samp{cc-mode} style.
|
|
1749
|
|
1750 Note that for BOCM compatibility, @samp{gnu} is the default
|
|
1751 style, and any non-style based customizations you make (i.e. in
|
|
1752 @code{c-mode-common-hook} in your
|
|
1753 @file{.emacs} file) will be based on @samp{gnu} style unless you do
|
70
|
1754 a @code{c-set-style} as the first thing in your hook.
|
0
|
1755
|
|
1756
|
|
1757 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
1758 @node Adding Styles, File Styles, Built-in Styles, Styles
|
|
1759 @comment node-name, next, previous,up
|
|
1760
|
|
1761 @subsection Adding Styles
|
|
1762 @cindex Adding Styles
|
|
1763 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
1764
|
|
1765 @vindex c-style-alist
|
|
1766 @vindex style-alist (c-)
|
|
1767 @findex c-add-style
|
|
1768 @findex add-style (c-)
|
|
1769 If none of the built-in styles is appropriate, you'll probably want to
|
2
|
1770 add a new @dfn{style definition}. Styles are kept in the
|
|
1771 @code{c-style-alist} variable, but you should never modify this variable
|
70
|
1772 directly. Instead, @code{cc-mode} provides the function
|
2
|
1773 @code{c-add-style} that you can use to easily add new styles or change
|
|
1774 existing styles. This function takes two arguments, a @var{stylename}
|
|
1775 string, and an association list @var{description} of style
|
|
1776 customizations. If @var{stylename} is not already in
|
|
1777 @code{c-style-alist}, the new style is added, otherwise the style is
|
|
1778 changed to the new @var{description}.
|
|
1779 This function also takes an optional third argument, which if
|
|
1780 non-@code{nil}, automatically applies the new style to the current
|
|
1781 buffer.
|
0
|
1782
|
|
1783 The sample @file{.emacs} file provides a concrete example of how a new
|
|
1784 style can be added and automatically set. @xref{Sample .emacs File}.
|
|
1785
|
|
1786 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
1787 @node File Styles, , Adding Styles, Styles
|
|
1788 @comment node-name, next, previous,up
|
|
1789
|
|
1790 @subsection File Styles
|
|
1791 @cindex File Styles
|
|
1792 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
1793
|
|
1794 @cindex local variables
|
|
1795 The Emacs manual describes how you can customize certain variables on a
|
|
1796 per-file basis by including a @dfn{Local Variable} block at the end of
|
|
1797 the file. So far, you've only seen a functional interface to
|
70
|
1798 @code{cc-mode}, which is highly inconvenient for use in a Local Variable
|
|
1799 block. @code{cc-mode} provides two variables that make it easier for
|
0
|
1800 you to customize your style on a per-file basis.
|
|
1801
|
|
1802 @vindex c-file-style
|
|
1803 @vindex file-style (c-)
|
|
1804 @vindex c-file-offsets
|
|
1805 @vindex file-offsets (c-)
|
|
1806
|
2
|
1807 The variable @code{c-file-style} can be set to a style name string.
|
70
|
1808 When the file is visited, @code{cc-mode} will automatically set the
|
2
|
1809 file's style to this style using @code{c-set-style}.
|
0
|
1810
|
|
1811 @vindex c-offsets-alist
|
|
1812 @vindex offsets-alist (c-)
|
|
1813 @findex c-set-offset
|
|
1814 @findex set-offset (c-)
|
|
1815 Another variable, @code{c-file-offsets}, takes an association list
|
|
1816 similar to what is allowed in @code{c-offsets-alist}. When the file is
|
70
|
1817 visited, @code{cc-mode} will automatically institute these offets using
|
0
|
1818 @code{c-set-offset}.
|
|
1819
|
|
1820 Note that file style settings (i.e. @code{c-file-style}) are applied
|
2
|
1821 before file offset settings (i.e. @code{c-file-offsets})@footnote{File
|
70
|
1822 styles are only supported since XEmacs 19.12 and Emacs 19.29. They work
|
|
1823 via the standard Emacs hook variable @code{hack-local-variables-hook}.}.
|
0
|
1824
|
|
1825
|
|
1826 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
1827 @node Advanced Customizations, , Styles, Customizing Indentation
|
|
1828 @comment node-name, next, previous,up
|
|
1829
|
|
1830 @section Advanced Customizations
|
|
1831 @cindex Advanced Customizations
|
|
1832 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
1833
|
|
1834 @vindex c-style-alist
|
|
1835 @vindex style-alist (c-)
|
|
1836 @vindex c-basic-offset
|
|
1837 @vindex basic-offset (c-)
|
70
|
1838 For most users, @code{cc-mode} will support their coding styles with
|
2
|
1839 very little need for more advanced customizations. Usually, one of the
|
|
1840 standard styles defined in @code{c-style-alist} will do the trick. At
|
|
1841 most, perhaps one of the syntactic symbol offsets will need to be
|
|
1842 tweaked slightly, or maybe @code{c-basic-offset} will need to be
|
|
1843 changed. However, some styles require a more flexible framework for
|
70
|
1844 customization, and one of the real strengths of @code{cc-mode} is that
|
2
|
1845 the syntactic analysis model provides just such a framework. This allows
|
70
|
1846 you to implement special indentation calculations for situations not
|
2
|
1847 handled by the mode directly.
|
0
|
1848
|
|
1849 @menu
|
|
1850 * Custom Indentation Functions::
|
|
1851 * Custom Brace and Colon Hanging::
|
|
1852 * Customizing Semi-colons and Commas::
|
|
1853 * Other Special Indentations::
|
|
1854 @end menu
|
|
1855
|
|
1856 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
1857 @node Custom Indentation Functions, Custom Brace and Colon Hanging, , Advanced Customizations
|
|
1858 @comment node-name, next, previous,up
|
|
1859
|
|
1860 @subsection Custom Indentation Functions
|
|
1861 @cindex Custom Indentation Functions
|
|
1862 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
1863
|
|
1864 @cindex custom indentation functions
|
70
|
1865 One of the most common ways to customize @code{cc-mode} is by writing
|
|
1866 @dfn{custom indentation functions} and associating them with specific
|
|
1867 syntactic symbols (see @ref{Syntactic Symbols}). @code{cc-mode} itself
|
|
1868 uses custom indentation functions to provide more sophisticated
|
|
1869 indentation, for example when lining up C++ stream operator blocks:
|
0
|
1870 @example
|
|
1871 @group
|
|
1872
|
|
1873 1: void main(int argc, char**)
|
|
1874 2: @{
|
|
1875 3: cout << "There were "
|
|
1876 4: << argc
|
|
1877 5: << "arguments passed to the program"
|
|
1878 6: << endl;
|
|
1879 7: @}
|
|
1880
|
|
1881 @end group
|
|
1882 @end example
|
|
1883
|
|
1884 In this example, lines 4 through 6 are assigned the @code{stream-op}
|
2
|
1885 syntactic symbol. Here, @code{stream-op} has an offset of @code{+}, and
|
|
1886 with a @code{c-basic-offset} of 2, you can see that lines 4 through 6
|
|
1887 are simply indented two spaces to the right of line 3. But perhaps we'd
|
70
|
1888 like @code{cc-mode} to be a little more intelligent so that it lines up
|
2
|
1889 all the @samp{<<} symbols in lines 3 through 6. To do this, we have
|
|
1890 to write a custom indentation function which finds the column of first
|
|
1891 stream operator on the first line of the statement. Here is the lisp
|
|
1892 code (from the @file{cc-mode.el} source file) that implements this:
|
0
|
1893 @example
|
|
1894 @group
|
|
1895
|
|
1896 (defun c-lineup-streamop (langelem)
|
|
1897 ;; lineup stream operators
|
|
1898 (save-excursion
|
|
1899 (let* ((relpos (cdr langelem))
|
|
1900 (curcol (progn (goto-char relpos)
|
|
1901 (current-column))))
|
|
1902 (re-search-forward "<<\\|>>" (c-point 'eol) 'move)
|
|
1903 (goto-char (match-beginning 0))
|
|
1904 (- (current-column) curcol))))
|
|
1905
|
|
1906 @end group
|
|
1907 @end example
|
|
1908 @noindent
|
|
1909 Custom indent functions take a single argument, which is a syntactic
|
|
1910 component cons cell (see @ref{Syntactic Analysis}). The
|
|
1911 function returns an integer offset value that will be added to the
|
2
|
1912 running total indentation for the line. Note that what actually gets
|
0
|
1913 returned is the difference between the column that the first stream
|
|
1914 operator is on, and the column of the buffer relative position passed in
|
70
|
1915 the function's argument. Remember that @code{cc-mode} automatically
|
0
|
1916 adds in the column of the component's relative buffer position and we
|
|
1917 don't want that value added into the final total twice.
|
|
1918
|
|
1919 @cindex stream-op syntactic symbol
|
|
1920 @findex c-lineup-streamop
|
|
1921 @findex lineup-streamop (c-)
|
|
1922 Now, to associate the function @code{c-lineup-streamop} with the
|
|
1923 @code{stream-op} syntactic symbol, we can add something like the
|
|
1924 following to our @code{c++-mode-hook}@footnote{It probably makes more
|
|
1925 sense to add this to @code{c++-mode-hook} than @code{c-mode-common-hook}
|
|
1926 since stream operators are only relevent for C++.}:
|
|
1927 @example
|
|
1928
|
|
1929 (c-set-offset 'stream-op 'c-lineup-streamop)
|
|
1930
|
|
1931 @end example
|
|
1932
|
|
1933 @kindex C-c C-q
|
|
1934 Now the function looks like this after re-indenting (using @kbd{C-c
|
|
1935 C-q}):
|
|
1936 @example
|
|
1937 @group
|
|
1938
|
|
1939 1: void main(int argc, char**)
|
|
1940 2: @{
|
|
1941 3: cout << "There were "
|
|
1942 4: << argc
|
|
1943 5: << "arguments passed to the program"
|
|
1944 6: << endl;
|
|
1945 7: @}
|
|
1946
|
|
1947 @end group
|
|
1948 @end example
|
|
1949
|
|
1950 @vindex c-offsets-alist
|
|
1951 @vindex offsets-alist (c-)
|
|
1952 Custom indentation functions can be as simple or as complex as you like,
|
|
1953 and any syntactic symbol that appears in @code{c-offsets-alist} can have
|
70
|
1954 a custom indentation function associated with it. @code{cc-mode} comes
|
2
|
1955 with several standard custom indentation functions, not all of which are
|
|
1956 used by the default styles.
|
|
1957
|
|
1958 @itemize @bullet
|
|
1959 @item
|
|
1960 @findex c-lineup-arglist
|
|
1961 @findex lineup-arglist (c-)
|
|
1962 @code{c-lineup-arglist} --- lines up function argument lines under the
|
|
1963 argument on the previous line.
|
|
1964
|
|
1965 @item
|
|
1966 @findex c-lineup-arglist-intro-after-paren
|
|
1967 @findex lineup-arglist-intro-after-paren (c-)
|
|
1968 @code{c-lineup-arglist-intro-after-paren} --- similar to
|
|
1969 @code{c-lineup-arglist}, but works for argument lists that begin with an
|
|
1970 open parenthesis followed by a newline.
|
|
1971
|
|
1972 @item
|
|
1973 @findex c-lineup-arglist-close-under-paren
|
|
1974 @findex lineup-arglist-close-under-paren (c-)
|
|
1975 @code{c-lineup-arglist-close-under-paren} --- set your
|
|
1976 @code{arglist-close} syntactic symbol to this line-up function so that
|
|
1977 parentheses that close argument lists will line up under the parenthesis
|
|
1978 that opened the argument list.
|
|
1979
|
|
1980 @item
|
|
1981 @findex c-lineup-streamop
|
|
1982 @findex lineup-streamop (c-)
|
|
1983 @code{c-lineup-streamop} --- lines up C++ stream operators
|
|
1984 (e.g. @samp{<<} and @samp{>>}).
|
|
1985
|
|
1986 @item
|
|
1987 @findex c-lineup-multi-inher
|
|
1988 @findex lineup-multi-inher (c-)
|
|
1989 @code{c-lineup-multi-inher} --- lines up multiple inheritance lines.
|
|
1990
|
|
1991 @item
|
|
1992 @findex c-lineup-C-comments
|
|
1993 @findex lineup-C-comments (c-)
|
|
1994 @code{c-lineup-C-comments} --- lines up C block comment continuation
|
|
1995 lines.
|
|
1996
|
|
1997 @item
|
|
1998 @findex c-lineup-comment
|
|
1999 @findex lineup-comment (c-)
|
|
2000 @vindex c-comment-only-line-offset
|
|
2001 @vindex comment-only-line-offset (c-)
|
|
2002 @code{c-lineup-comment} --- implements the old comment line up behavior
|
|
2003 specified by the variable @code{c-comment-only-line-offset}.
|
|
2004
|
|
2005 @item
|
|
2006 @findex c-lineup-runin-statements
|
|
2007 @findex lineup-runin-statements (c-)
|
|
2008 @code{c-lineup-runin-statements} --- lines up @code{statement}s for coding
|
|
2009 standards which place the first statement in a block on the same line as
|
|
2010 the block opening brace.
|
|
2011
|
|
2012 @item
|
|
2013 @findex c-lineup-math
|
|
2014 @findex lineup-math (c-)
|
|
2015 @code{c-lineup-math} --- lines up math @code{statement-cont} lines under
|
|
2016 the previous line after the equals sign.
|
|
2017
|
|
2018 @item
|
|
2019 @findex c-lineup-ObjC-method-call
|
|
2020 @findex lineup-ObjC-method-call (c-)
|
|
2021 @code{c-lineup-ObjC-method-call} --- for Objective-C code, lines up
|
|
2022 selector arguments just after the message receiver.
|
|
2023
|
|
2024 @item
|
|
2025 @findex c-lineup-ObjC-method-args
|
|
2026 @findex lineup-ObjC-method-args (c-)
|
|
2027 @code{c-lineup-ObjC-method-args} --- for Objective-C code, lines up the
|
|
2028 colons that separate arguments by aligning colons vertically.
|
|
2029
|
|
2030 @item
|
|
2031 @findex c-lineup-ObjC-method-args-2
|
|
2032 @findex lineup-ObjC-method-args-2 (c-)
|
|
2033 @code{c-lineup-ObjC-method-args-2} --- similar to
|
|
2034 @code{c-lineup-ObjC-method-args} but lines up the colon on the current
|
|
2035 line with the colon on the previous line.
|
|
2036
|
|
2037 @end itemize
|
0
|
2038
|
|
2039 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
2040 @node Custom Brace and Colon Hanging, Customizing Semi-colons and Commas, Custom Indentation Functions, Advanced Customizations
|
|
2041 @comment node-name, next, previous,up
|
|
2042
|
|
2043 @subsection Custom Brace and Colon Hanging
|
|
2044 @cindex Custom Brace and Colon Hanging
|
|
2045 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
2046
|
|
2047 @vindex c-hanging-braces-alist
|
|
2048 @vindex hanging-braces-alist (c-)
|
|
2049 Syntactic symbols aren't the only place where you can customize
|
70
|
2050 @code{cc-mode} with the lisp equivalent of callback functions. Brace
|
2
|
2051 ``hanginess'' can also be determined by custom functions associated with
|
0
|
2052 syntactic symbols on the @code{c-hanging-braces-alist} variable.
|
|
2053 Remember that @var{ACTION}'s are typically a list containing some
|
|
2054 combination of the symbols @code{before} and @code{after} (see
|
|
2055 @ref{Hanging Braces}). However, an @var{ACTION} can also be a function
|
|
2056 symbol which gets called when a brace matching that syntactic symbol is
|
|
2057 typed.
|
|
2058
|
|
2059 @cindex customizing brace hanging
|
|
2060 These @var{ACTION} functions are called with two arguments: the
|
|
2061 syntactic symbol for the brace, and the buffer position at which the
|
|
2062 brace was inserted. The @var{ACTION} function is expected to return a
|
|
2063 list containing some combination of @code{before} and @code{after}. The
|
|
2064 function can also return @code{nil}. This return value has the normal
|
2
|
2065 brace hanging semantics.
|
0
|
2066
|
70
|
2067 As an example, @code{cc-mode} itself uses this feature to dynamically
|
2
|
2068 determine the hanginess of braces which close ``do-while''
|
0
|
2069 constructs:
|
|
2070 @example
|
|
2071 @group
|
|
2072
|
|
2073 void do_list( int count, char** atleast_one_string )
|
|
2074 @{
|
|
2075 int i=0;
|
|
2076 do @{
|
2
|
2077 handle_string( atleast_one_string[i] );
|
0
|
2078 i++;
|
|
2079 @} while( i < count );
|
|
2080 @}
|
|
2081
|
|
2082 @end group
|
|
2083 @end example
|
|
2084
|
|
2085 @findex c-snug-do-while
|
|
2086 @findex snug-do-while (c-)
|
70
|
2087 @code{cc-mode} assigns the @code{block-close} syntactic symbol to the
|
0
|
2088 brace that closes the @code{do} construct, and normally we'd like the
|
|
2089 line that follows a @code{block-close} brace to begin on a separate
|
2
|
2090 line. However, with ``do-while'' constructs, we want the
|
0
|
2091 @code{while} clause to follow the closing brace. To do this, we
|
|
2092 associate the @code{block-close} symbol with the @var{ACTION} function
|
|
2093 @code{c-snug-do-while}:
|
|
2094 @example
|
|
2095
|
|
2096 (defun c-snug-do-while (syntax pos)
|
|
2097 "Dynamically calculate brace hanginess for do-while statements.
|
|
2098 Using this function, `while' clauses that end a `do-while' block will
|
|
2099 remain on the same line as the brace that closes that block.
|
|
2100
|
|
2101 See `c-hanging-braces-alist' for how to utilize this function as an
|
|
2102 ACTION associated with `block-close' syntax."
|
|
2103 (save-excursion
|
|
2104 (let (langelem)
|
|
2105 (if (and (eq syntax 'block-close)
|
|
2106 (setq langelem (assq 'block-close c-syntactic-context))
|
|
2107 (progn (goto-char (cdr langelem))
|
|
2108 (if (= (following-char) ?@{)
|
|
2109 (forward-sexp -1))
|
|
2110 (looking-at "\\<do\\>[^_]")))
|
|
2111 '(before)
|
|
2112 '(before after)))))
|
|
2113
|
|
2114 @end example
|
|
2115
|
2
|
2116 This function simply looks to see if the brace closes a ``do-while''
|
|
2117 clause and if so, returns the list @samp{(before)} indicating
|
0
|
2118 that a newline should be inserted before the brace, but not after it.
|
2
|
2119 In all other cases, it returns the list @samp{(before after)} so
|
0
|
2120 that the brace appears on a line by itself.
|
|
2121
|
|
2122 @vindex c-syntactic-context
|
|
2123 @vindex syntactic-context (c-)
|
|
2124 During the call to the brace hanging @var{ACTION} function, the variable
|
|
2125 @code{c-syntactic-context} is bound to the full syntactic analysis list.
|
|
2126
|
|
2127 @cindex customizing colon hanging
|
|
2128 @vindex c-hanging-colon-alist
|
|
2129 @vindex hanging-colon-alist (c-)
|
|
2130 Note that for symmetry, colon hanginess should be customizable by
|
|
2131 allowing function symbols as @var{ACTION}s on the
|
|
2132 @code{c-hanging-colon-alist} variable. Since no use has actually been
|
70
|
2133 found for this feature, it isn't currently implemented.
|
0
|
2134
|
|
2135 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
2136 @node Customizing Semi-colons and Commas, Other Special Indentations, Custom Brace and Colon Hanging, Advanced Customizations
|
|
2137 @comment node-name, next, previous,up
|
|
2138
|
|
2139 @subsection Customizing Semi-colons and Commas
|
|
2140 @cindex Customizing Semi-colons and Commas
|
|
2141 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
2142
|
|
2143 @cindex customizing semi-colons and commas
|
|
2144 @vindex c-hanging-semi&comma-criteria
|
|
2145 @vindex hanging-semi&comma-criteria (c-)
|
|
2146 You can also customize the insertion of newlines after semi-colons and
|
|
2147 commas, when the auto-newline minor mode is enabled (see @ref{Minor
|
|
2148 Modes}). This is controlled by the variable
|
|
2149 @code{c-hanging-semi&comma-criteria}, which contains a list of functions
|
|
2150 that are called in the order they appear. Each function is called with
|
|
2151 zero arguments, and is expected to return one of the following values:
|
|
2152
|
|
2153 @itemize @bullet
|
|
2154 @item
|
2
|
2155 non-@code{nil} --- A newline is inserted, and no more functions from the
|
0
|
2156 list are called.
|
|
2157
|
|
2158 @item
|
2
|
2159 @code{stop} --- No more functions from the list are called, but no
|
0
|
2160 newline is inserted.
|
|
2161
|
|
2162 @item
|
2
|
2163 @code{nil} --- No determination is made, and the next function in the
|
0
|
2164 list is called.
|
|
2165
|
|
2166 @end itemize
|
|
2167
|
|
2168 If every function in the list is called without a determination being
|
|
2169 made, then no newline is added. The default value for this variable is a
|
|
2170 list containing a single function which inserts newlines only after
|
|
2171 semi-colons which do not appear inside parenthesis lists (i.e. those
|
|
2172 that separate @code{for}-clause statements).
|
|
2173
|
2
|
2174 Here's an example of a criteria function that will prevent newlines from
|
|
2175 being inserted after semicolons when there is a non-blank following
|
70
|
2176 line. Otherwise, it makes no determination:
|
2
|
2177
|
|
2178 @example
|
|
2179 @group
|
|
2180
|
|
2181 (defun my-semicolon-criteria ()
|
|
2182 (save-excursion
|
|
2183 (if (and (= last-command-char ?\;)
|
|
2184 (zerop (forward-line 1))
|
|
2185 (not (looking-at "^[ \t]*$")))
|
|
2186 'stop
|
|
2187 nil)))
|
|
2188
|
|
2189 @end group
|
|
2190 @end example
|
|
2191
|
|
2192
|
0
|
2193 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
2194 @node Other Special Indentations, , Customizing Semi-colons and Commas, Advanced Customizations
|
|
2195 @comment node-name, next, previous,up
|
|
2196
|
|
2197 @subsection Other Special Indentations
|
|
2198 @cindex Customizing Semi-colons and Commas
|
|
2199 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
2200
|
2
|
2201 @vindex c-label-minimum-indentation
|
|
2202 @vindex label-minimum-indentation (c-)
|
|
2203 In @samp{gnu} style (see @ref{Built-in Styles}), a minimum indentation
|
70
|
2204 is imposed on lines with @code{label} or @code{case-label} syntax. This
|
|
2205 minimum indentation is controlled by the variable
|
2
|
2206 @code{c-label-minimum-indentation}. The default value for this variable
|
|
2207 is 1.
|
|
2208
|
0
|
2209 @vindex c-special-indent-hook
|
|
2210 @vindex special-indent-hook (c-)
|
70
|
2211 One other customization variable is available in @code{cc-mode}:
|
0
|
2212 @code{c-special-indent-hook}. This is a standard hook variable that is
|
70
|
2213 called after every line is indented by @code{cc-mode}. You can use it
|
0
|
2214 to do any special indentation or line adjustments your style dictates,
|
|
2215 such as adding extra indentation to constructors or destructor
|
|
2216 declarations in a class definition, etc. Note however, that you should
|
70
|
2217 not change Point or Mark inside your @code{c-special-indent-hook}
|
0
|
2218 functions (i.e. you'll probably want to wrap your function in a
|
|
2219 @code{save-excursion}).
|
|
2220
|
2
|
2221 Setting @code{c-special-indent-hook} in your style definition is handled
|
|
2222 slightly differently than other variables. In your style definition,
|
|
2223 you should set the value for
|
|
2224 @code{c-special-indent-hook} to a function or list of functions, which
|
|
2225 will be appended to @code{c-special-indent-hook} using @code{add-hook}.
|
|
2226 That way, the current setting for the buffer local value of
|
|
2227 @code{c-special-indent-hook} won't be overridden.
|
|
2228
|
0
|
2229
|
|
2230 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
2231 @node Syntactic Symbols, Performance Issues, Customizing Indentation, Top
|
|
2232 @comment node-name, next, previous,up
|
|
2233
|
|
2234 @chapter Syntactic Symbols
|
|
2235 @cindex Syntactic Symbols
|
|
2236 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
2237
|
|
2238 @vindex c-offsets-alist
|
|
2239 @vindex offsets-alist (c-)
|
2
|
2240
|
|
2241 Here is a complete list of the recognized syntactic symbols as described
|
|
2242 in the @code{c-offsets-alist} variable, along with a brief description.
|
|
2243 More detailed descriptions follow below.
|
|
2244
|
|
2245 @itemize @bullet
|
|
2246 @item
|
|
2247 @code{string} --- inside multi-line string
|
|
2248 @item
|
|
2249 @code{c} --- inside a multi-line C style block comment
|
|
2250 @item
|
|
2251 @code{defun-open} --- brace that opens a function definition
|
|
2252 @item
|
|
2253 @code{defun-close} --- brace that closes a function definition
|
|
2254 @item
|
|
2255 @code{defun-block-intro} --- the first line in a top-level defun
|
|
2256 @item
|
|
2257 @code{class-open} --- brace that opens a class definition
|
|
2258 @item
|
|
2259 @code{class-close} --- brace that closes a class definition
|
|
2260 @item
|
|
2261 @code{inline-open} --- brace that opens an in-class inline method
|
|
2262 @item
|
|
2263 @code{inline-close} --- brace that closes an in-class inline method
|
|
2264 @item
|
70
|
2265 @code{ansi-funcdecl-cont} --- the nether region between an ANSI function
|
|
2266 declaration and the defun opening brace
|
2
|
2267 @item
|
|
2268 @code{knr-argdecl-intro} --- first line of a K&R C argument declaration
|
|
2269 @item
|
|
2270 @code{knr-argdecl} --- subsequent lines in a K&R C argument declaration
|
|
2271 @item
|
70
|
2272 @code{topmost-intro} --- the first line in a topmost construct definition
|
2
|
2273 @item
|
|
2274 @code{topmost-intro-cont} --- topmost definition continuation lines
|
|
2275 @item
|
|
2276 @code{member-init-intro} --- first line in a member initialization list
|
|
2277 @item
|
|
2278 @code{member-init-cont} --- subsequent member initialization list lines
|
|
2279 @item
|
|
2280 @code{inher-intro} --- first line of a multiple inheritance list
|
|
2281 @item
|
|
2282 @code{inher-cont} --- subsequent multiple inheritance lines
|
|
2283 @item
|
|
2284 @code{block-open} --- statement block open brace
|
|
2285 @item
|
|
2286 @code{block-close} --- statement block close brace
|
|
2287 @item
|
|
2288 @code{brace-list-open} --- open brace of an enum or static array list
|
|
2289 @item
|
|
2290 @code{brace-list-close} --- close brace of an enum or static array list
|
|
2291 @item
|
|
2292 @code{brace-list-intro} --- first line in an enum or static array list
|
|
2293 @item
|
|
2294 @code{brace-list-entry} --- subsequent lines in an enum or static array list
|
|
2295 @item
|
70
|
2296 @code{statement} --- a C (or like) statement
|
2
|
2297 @item
|
70
|
2298 @code{statement-cont} --- a continuation of a C (or like) statement
|
2
|
2299 @item
|
|
2300 @code{statement-block-intro} --- the first line in a new statement block
|
|
2301 @item
|
|
2302 @code{statement-case-intro} --- the first line in a case `block'
|
|
2303 @item
|
|
2304 @code{statement-case-open} --- the first line in a case block starting
|
|
2305 with brace
|
|
2306 @item
|
70
|
2307 @code{substatement} --- the first line after an if/while/for/do/else
|
2
|
2308 @item
|
|
2309 @code{substatement-open} --- the brace that opens a substatement block
|
|
2310 @item
|
|
2311 @code{case-label} --- a case or default label
|
|
2312 @item
|
70
|
2313 @code{access-label} --- C++ private/protected/public access label
|
2
|
2314 @item
|
70
|
2315 @code{label} --- any non-special C (or like) label
|
2
|
2316 @item
|
70
|
2317 @code{do-while-closure} --- the `while' that ends a do/while construct
|
2
|
2318 @item
|
70
|
2319 @code{else-clause} --- the `else' of an if/else construct
|
2
|
2320 @item
|
|
2321 @code{comment-intro} --- a line containing only a comment introduction
|
|
2322 @item
|
|
2323 @code{arglist-intro} --- the first line in an argument list
|
|
2324 @item
|
|
2325 @code{arglist-cont} --- subsequent argument list lines when no arguments
|
|
2326 follow on the same line as the the arglist opening paren
|
|
2327 @item
|
|
2328 @code{arglist-cont-nonempty} --- subsequent argument list lines when at
|
|
2329 least one argument follows on the same line as the arglist opening paren
|
|
2330 @item
|
|
2331 @code{arglist-close} --- the solo close paren of an argument list
|
|
2332 @item
|
70
|
2333 @code{stream-op} --- lines continuing a stream operator construct
|
2
|
2334 @item
|
70
|
2335 @code{inclass} --- the construct is nested inside a class definition
|
2
|
2336 @item
|
|
2337 @code{cpp-macro} --- the start of a cpp macro
|
|
2338 @item
|
|
2339 @code{friend} --- a C++ friend declaration
|
|
2340 @item
|
|
2341 @code{objc-method-intro} --- the first line of an Objective-C method definition
|
|
2342 @item
|
|
2343 @code{objc-method-args-cont} --- lines continuing an Objective-C method
|
|
2344 definition
|
|
2345 @item
|
|
2346 @code{objc-method-call-cont} --- lines continuing an Objective-C method call
|
|
2347 @item
|
|
2348 @code{extern-lang-open} --- brace that opens an external language block
|
|
2349 @item
|
|
2350 @code{extern-lang-close} --- brace that closes an external language block
|
|
2351 @item
|
70
|
2352 @code{inextern-lang} --- analogous to `inclass' syntactic symbol
|
2
|
2353 @end itemize
|
0
|
2354
|
|
2355 @cindex -open syntactic symbols
|
|
2356 @cindex -close syntactic symbols
|
|
2357 Most syntactic symbol names follow a general naming convention. When a
|
|
2358 line begins with an open or close brace, the syntactic symbol will
|
|
2359 contain the suffix @code{-open} or @code{-close} respectively.
|
|
2360
|
|
2361 @cindex -intro syntactic symbols
|
|
2362 @cindex -cont syntactic symbols
|
|
2363 @cindex -block-intro syntactic symbols
|
|
2364 Usually, a distinction is made between the first line that introduces a
|
|
2365 construct and lines that continue a construct, and the syntactic symbols
|
|
2366 that represent these lines will contain the suffix @code{-intro} or
|
|
2367 @code{-cont} respectively. As a sub-classification of this scheme, a
|
|
2368 line which is the first of a particular brace block construct will
|
|
2369 contain the suffix @code{-block-intro}.
|
|
2370
|
|
2371 @kindex C-c C-s
|
|
2372 Let's look at some examples to understand how this works. Remember that
|
|
2373 you can check the syntax of any line by using @kbd{C-c C-s}.
|
|
2374 @example
|
|
2375 @group
|
|
2376
|
|
2377 1: void
|
|
2378 2: swap( int& a, int& b )
|
|
2379 3: @{
|
|
2380 4: int tmp = a;
|
|
2381 5: a = b;
|
|
2382 6: b = tmp;
|
|
2383 7: int ignored =
|
|
2384 8: a + b;
|
|
2385 9: @}
|
|
2386
|
|
2387 @end group
|
|
2388 @end example
|
|
2389
|
2
|
2390 @cindex topmost-intro syntactic symbol
|
|
2391 @cindex topmost-intro-cont syntactic symbol
|
|
2392 @cindex defun-open syntactic symbol
|
|
2393 @cindex defun-close syntactic symbol
|
|
2394 @cindex defun-block-intro syntactic symbol
|
0
|
2395 Line 1 shows a @code{topmost-intro} since it is the first line that
|
|
2396 introduces a top-level construct. Line 2 is a continuation of the
|
|
2397 top-level construct introduction so it has the syntax
|
|
2398 @code{topmost-intro-cont}. Line 3 shows a @code{defun-open} since it is
|
|
2399 the brace that opens a top-level function definition. Line 9 is a
|
|
2400 @code{defun-close} since it contains the brace that closes the top-level
|
|
2401 function definition. Line 4 is a @code{defun-block-intro}, i.e. it is
|
|
2402 the first line of a brace-block, which happens to be enclosed in a
|
|
2403 top-level function definition.
|
|
2404
|
2
|
2405 @cindex statement syntactic symbol
|
|
2406 @cindex statement-cont syntactic symbol
|
0
|
2407 Lines 5, 6, and 7 are all given @code{statement} syntax since there
|
|
2408 isn't much special about them. Note however that line 8 is given
|
|
2409 @code{statement-cont} syntax since it continues the statement begun
|
|
2410 on the previous line.
|
|
2411
|
|
2412 Here's another example, which illustrates some C++ class syntactic
|
|
2413 symbols:
|
|
2414 @example
|
|
2415 @group
|
|
2416
|
|
2417 1: class Bass
|
|
2418 2: : public Guitar,
|
|
2419 3: public Amplifiable
|
|
2420 4: @{
|
|
2421 5: public:
|
|
2422 6: Bass()
|
|
2423 7: : eString( new BassString( 0.105 )),
|
|
2424 8: aString( new BassString( 0.085 )),
|
|
2425 9: dString( new BassString( 0.065 )),
|
|
2426 10: gString( new BassString( 0.045 ))
|
|
2427 11: @{
|
|
2428 12: eString.tune( 'E' );
|
|
2429 13: aString.tune( 'A' );
|
|
2430 14: dString.tune( 'D' );
|
|
2431 15: gString.tune( 'G' );
|
|
2432 16: @}
|
2
|
2433 17: friend class Luthier;
|
|
2434 18: @}
|
0
|
2435
|
|
2436 @end group
|
|
2437 @end example
|
|
2438
|
2
|
2439 @cindex class-open syntactic symbol
|
|
2440 @cindex class-close syntactic symbol
|
0
|
2441 As in the previous example, line 1 has the @code{topmost-intro} syntax.
|
|
2442 Here however, the brace that opens a C++ class definition on line 4 is
|
2
|
2443 assigned the @code{class-open} syntax. Note that in C++, classes,
|
|
2444 structs, and unions are essentially equivalent syntactically (and are
|
|
2445 very similar semantically), so replacing the @code{class} keyword in the
|
|
2446 example above with @code{struct} or @code{union} would still result in a
|
|
2447 syntax of @code{class-open} for line 4 @footnote{This is the case even
|
|
2448 for C and Objective-C. For consistency, structs in all supported
|
|
2449 languages are syntactically equivalent to classes. Note however that
|
|
2450 the keyword @code{class} is meaningless in C and Objective-C.}.
|
|
2451 Similarly, line 18 is assigned @code{class-close} syntax.
|
|
2452
|
|
2453 @cindex inher-intro syntactic symbol
|
|
2454 @cindex inher-cont syntactic symbol
|
0
|
2455 Line 2 introduces the inheritance list for the class so it is assigned
|
|
2456 the @code{inher-intro} syntax, and line 3, which continues the
|
|
2457 inheritance list is given @code{inher-cont} syntax.
|
|
2458
|
2
|
2459 @cindex access-label syntactic symbol
|
|
2460 @cindex inclass syntactic symbol
|
|
2461 Hitting @kbd{C-c C-s} on line 5 shows the following analysis:
|
|
2462
|
|
2463 @example
|
|
2464 @group
|
|
2465
|
|
2466 @code{((inclass . 1) (access-label . 67))}
|
|
2467
|
|
2468 @end group
|
|
2469 @end example
|
|
2470
|
|
2471 @noindent
|
|
2472 The primary syntactic symbol for this line is @code{access-label} as
|
|
2473 this a label keyword that specifies access protection in C++. However,
|
|
2474 because this line is also a top-level construct inside a class
|
|
2475 definition, the analysis actually shows two syntactic symbols. The
|
|
2476 other syntactic symbol assigned to this line is @code{inclass}.
|
|
2477 Similarly, line 6 is given both @code{inclass} and @code{topmost-intro}
|
|
2478 syntax:
|
|
2479
|
|
2480 @example
|
|
2481 @group
|
|
2482
|
|
2483 @code{((inclass . 58) (topmost-intro . 60))}
|
|
2484
|
|
2485 @end group
|
|
2486 @end example
|
|
2487
|
|
2488 @cindex member-init-intro syntactic symbol
|
|
2489 @cindex member-init-cont syntactic symbol
|
0
|
2490 Line 7 introduces a C++ member initialization list and as such is given
|
|
2491 @code{member-init-intro} syntax. Note that in this case it is
|
|
2492 @emph{not} assigned @code{inclass} since this is not considered a
|
|
2493 top-level construct. Lines 8 through 10 are all assigned
|
|
2494 @code{member-init-cont} since they continue the member initialization
|
|
2495 list started on line 7.
|
|
2496
|
|
2497 @cindex in-class inline methods
|
2
|
2498 @cindex inline-open syntactic symbol
|
|
2499 @cindex inline-close syntactic symbol
|
|
2500 But the line 11's analysis is a bit more complicated:
|
|
2501
|
|
2502 @example
|
|
2503 @group
|
|
2504
|
|
2505 @code{((inclass . 1) (inline-open))}
|
|
2506
|
|
2507 @end group
|
|
2508 @end example
|
|
2509
|
|
2510 This line is assigned a syntax of both @code{inline-open} and
|
|
2511 @code{inclass} because it opens an @dfn{in-class} C++ inline method
|
|
2512 definition. This is distinct from, but related to, the C++ notion of an
|
|
2513 inline function in that its definition occurs inside an enclosing class
|
|
2514 definition, which in C++ implies that the function should be inlined.
|
|
2515 For example, if the definition of the @code{Bass} constructor appeared
|
|
2516 outside the class definition, line 11 would be given the
|
|
2517 @code{defun-open} syntax, even if the keyword @code{inline} appeared
|
|
2518 before the method name, as in:
|
0
|
2519 @example
|
|
2520 @group
|
|
2521
|
|
2522 class Bass
|
|
2523 : public Guitar,
|
|
2524 public Amplifiable
|
|
2525 @{
|
|
2526 public:
|
|
2527 Bass();
|
|
2528 @}
|
|
2529
|
|
2530 inline
|
|
2531 Bass::Bass()
|
|
2532 : eString( new BassString( 0.105 )),
|
|
2533 aString( new BassString( 0.085 )),
|
|
2534 dString( new BassString( 0.065 )),
|
|
2535 gString( new BassString( 0.045 ))
|
|
2536 @{
|
|
2537 eString.tune( 'E' );
|
|
2538 aString.tune( 'A' );
|
|
2539 dString.tune( 'D' );
|
|
2540 gString.tune( 'G' );
|
|
2541 @}
|
|
2542
|
|
2543 @end group
|
|
2544 @end example
|
|
2545
|
2
|
2546 @cindex friend syntactic symbol
|
|
2547 Returning to the previous example, line 16 is given @code{inline-close}
|
|
2548 syntax, while line 12 is given @code{defun-block-open} syntax, and lines
|
|
2549 13 through 15 are all given @code{statement} syntax. Line 17 is
|
|
2550 interesting in that it's syntactic analysis list contains three
|
|
2551 elements:
|
|
2552
|
|
2553 @example
|
|
2554
|
|
2555 @code{((friend) (inclass . 58) (topmost-intro . 380))}
|
|
2556
|
|
2557 @end example
|
|
2558
|
|
2559 The @code{friend} syntactic symbol is a modifier that typically does not
|
|
2560 have a relative buffer position.
|
0
|
2561
|
|
2562 Here is another (totally contrived) example which illustrates how syntax
|
|
2563 is assigned to various conditional constructs:
|
|
2564 @example
|
|
2565 @group
|
|
2566
|
|
2567 1: void spam( int index )
|
|
2568 2: @{
|
|
2569 3: for( int i=0; i<index; i++ )
|
|
2570 4: @{
|
|
2571 5: if( i == 10 )
|
|
2572 6: @{
|
|
2573 7: do_something_special();
|
|
2574 8: @}
|
|
2575 9: else
|
|
2576 10: do_something( i );
|
|
2577 11: @}
|
|
2578 12: do @{
|
|
2579 13: another_thing( i-- );
|
|
2580 14: @}
|
|
2581 15: while( i > 0 );
|
|
2582 16: @}
|
|
2583
|
|
2584
|
|
2585 @end group
|
|
2586 @end example
|
|
2587
|
|
2588 @noindent
|
|
2589 Only the lines that illustrate new syntactic symbols will be discussed.
|
|
2590
|
2
|
2591 @cindex substatement-open syntactic symbol
|
|
2592 @cindex substatement-block-intro syntactic symbol
|
|
2593 @cindex block-close syntactic symbol
|
0
|
2594 Line 4 has a brace which opens a conditional's substatement block. It
|
|
2595 is thus assigned @code{substatement-open} syntax, and since line 5 is
|
|
2596 the first line in the substatement block, it is assigned
|
|
2597 @code{substatement-block-intro} syntax. Lines 6 and 7 are assigned
|
|
2598 similar syntax. Line 8 contains the brace that closes the inner
|
2
|
2599 substatement block. It is given the syntax @code{block-close},
|
0
|
2600 as are lines 11 and 14.
|
|
2601
|
2
|
2602 @cindex else-clause syntactic symbol
|
|
2603 @cindex substatement syntactic symbol
|
|
2604 Line 9 is a little different --- since it contains the keyword
|
|
2605 @code{else} matching the @code{if} statement introduced on line 5, it is
|
0
|
2606 given the @code{else-clause} syntax. Note also that line 10 is slightly
|
|
2607 different too. Because @code{else} is considered a conditional
|
|
2608 introducing keyword @footnote{The list of conditional keywords are (in
|
2
|
2609 C, C++, Objective-C, and Java): @code{for}, @code{if}, @code{do},
|
|
2610 @code{else}, @code{while}, and @code{switch}. C++ and Java have two
|
|
2611 additional conditional keywords: @code{try} and @code{catch}. Java also
|
|
2612 has the @code{finally} and @code{synchronized} keywords.}, and because
|
|
2613 the following substatement is not a brace block, line 10 is assigned the
|
0
|
2614 @code{substatement} syntax.
|
|
2615
|
2
|
2616 @cindex do-while-closure syntactic symbol
|
0
|
2617 One other difference is seen on line 15. The @code{while} construct
|
|
2618 that closes a @code{do} conditional is given the special syntax
|
|
2619 @code{do-while-closure} if it appears on a line by itself. Note that if
|
|
2620 the @code{while} appeared on the same line as the preceding close brace,
|
|
2621 that line would have been assigned @code{block-close} syntax instead.
|
|
2622
|
|
2623 Switch statements have their own set of syntactic symbols. Here's an
|
|
2624 example:
|
|
2625 @example
|
|
2626 @group
|
|
2627
|
|
2628 1: void spam( enum Ingredient i )
|
|
2629 2: @{
|
|
2630 3: switch( i ) @{
|
|
2631 4: case Ham:
|
|
2632 5: be_a_pig();
|
|
2633 6: break;
|
|
2634 7: case Salt:
|
|
2635 8: drink_some_water();
|
|
2636 9: break;
|
|
2637 10: default:
|
|
2638 11: @{
|
|
2639 12: what_is_it();
|
|
2640 13: break;
|
|
2641 14: @}
|
|
2642 15: @}
|
|
2643 14: @}
|
|
2644
|
|
2645 @end group
|
|
2646 @end example
|
|
2647
|
2
|
2648 @cindex case-label syntactic symbol
|
|
2649 @cindex statement-case-intro syntactic symbol
|
|
2650 @cindex statement-case-open syntactic symbol
|
0
|
2651 Here, lines 4, 7, and 10 are all assigned @code{case-label} syntax,
|
|
2652 while lines 5 and 8 are assigned @code{statement-case-intro}. Line 11
|
|
2653 is treated slightly differently since it contains a brace that opens a
|
2
|
2654 block --- it is given @code{statement-case-open} syntax.
|
0
|
2655
|
|
2656 @cindex brace lists
|
|
2657 There are a set of syntactic symbols that are used to recognize
|
|
2658 constructs inside of brace lists. A brace list is defined as an
|
|
2659 @code{enum} or aggregate initializer list, such as might statically
|
|
2660 initialize an array of structs. For example:
|
|
2661 @example
|
|
2662 @group
|
|
2663
|
|
2664 1: static char* ingredients[] =
|
|
2665 2: @{
|
|
2666 3: "Ham",
|
|
2667 4: "Salt",
|
|
2668 5: NULL
|
|
2669 6: @}
|
|
2670
|
|
2671 @end group
|
|
2672 @end example
|
|
2673
|
2
|
2674 @cindex brace-list-open syntactic symbol
|
|
2675 @cindex brace-list-intro syntactic symbol
|
|
2676 @cindex brace-list-close syntactic symbol
|
|
2677 @cindex brace-list-entry syntactic symbol
|
0
|
2678 Following convention, line 2 in this example is assigned
|
|
2679 @code{brace-list-open} syntax, and line 3 is assigned
|
|
2680 @code{brace-list-intro} syntax. Likewise, line 6 is assigned
|
|
2681 @code{brace-list-close} syntax. Lines 4 and 5 however, are assigned
|
|
2682 @code{brace-list-entry} syntax, as would all subsequent lines in this
|
|
2683 initializer list.
|
|
2684
|
2
|
2685 External language definition blocks also have their own syntactic
|
|
2686 symbols. In this example:
|
|
2687 @example
|
|
2688 @group
|
|
2689
|
|
2690 1: extern "C"
|
|
2691 2: @{
|
|
2692 3: int thing_one( int );
|
|
2693 4: int thing_two( double );
|
|
2694 5: @}
|
|
2695
|
|
2696 @end group
|
|
2697 @end example
|
|
2698
|
|
2699 @cindex extern-lang-open syntactic symbol
|
|
2700 @cindex extern-lang-close syntactic symbol
|
|
2701 @cindex inextern-lang syntactic symbol
|
|
2702 @cindex inclass syntactic symbol
|
|
2703 @noindent
|
|
2704 line 2 is given the @code{extern-lang-open} syntax while line 5 is given
|
|
2705 the @code{extern-lang-close} syntax. The analysis for line 3 yields:
|
|
2706 @code{((inextern-lang) (topmost-intro . 14))}, where
|
|
2707 @code{inextern-lang} is a modifier similar in purpose to @code{inclass}.
|
|
2708
|
0
|
2709 A number of syntactic symbols are associated with parenthesis lists,
|
|
2710 a.k.a argument lists, as found in function declarations and function
|
|
2711 calls. This example illustrates these:
|
|
2712 @example
|
|
2713 @group
|
|
2714
|
|
2715 1: void a_function( int line1,
|
|
2716 2: int line2 );
|
|
2717 3:
|
|
2718 4: void a_longer_function(
|
|
2719 5: int line1,
|
|
2720 6: int line2
|
|
2721 7: );
|
|
2722 8:
|
|
2723 9: void call_them( int line1, int line2 )
|
|
2724 10: @{
|
|
2725 11: a_function(
|
|
2726 12: line1,
|
|
2727 13: line2
|
|
2728 14: );
|
|
2729 15:
|
|
2730 16: a_longer_function( line1,
|
|
2731 17: line2 );
|
|
2732 18: @}
|
|
2733
|
|
2734 @end group
|
|
2735 @end example
|
|
2736
|
2
|
2737 @cindex arglist-intro syntactic symbol
|
|
2738 @cindex arglist-close syntactic symbol
|
0
|
2739 Lines 5 and 12 are assigned @code{arglist-intro} syntax since they are
|
|
2740 the first line following the open parenthesis, and lines 7 and 14 are
|
|
2741 assigned @code{arglist-close} syntax since they contain the parenthesis
|
|
2742 that closes the argument list.
|
|
2743
|
2
|
2744 @cindex arglist-cont-nonempty syntactic symbol
|
|
2745 @cindex arglist-cont syntactic symbol
|
|
2746 Lines that continue argument lists can be assigned one of two syntactic
|
|
2747 symbols. For example, Lines 2 and 17
|
|
2748 are assigned @code{arglist-cont-nonempty} syntax. What this means
|
0
|
2749 is that they continue an argument list, but that the line containing the
|
2
|
2750 parenthesis that opens the list is @emph{not empty} following the open
|
0
|
2751 parenthesis. Contrast this against lines 6 and 13 which are assigned
|
|
2752 @code{arglist-cont} syntax. This is because the parenthesis that opens
|
70
|
2753 their argument lists is the last character on that line @footnote{The
|
|
2754 need for this somewhat confusing arrangement is that the typical
|
|
2755 indentation desired for these lines is calculated differently.
|
|
2756 This should be simplified in version 5 of @code{cc-mode}, along with the
|
|
2757 added distinction between argument lists in function declarations, and
|
|
2758 argument lists in function calls.}.
|
0
|
2759
|
|
2760 Note that there is no @code{arglist-open} syntax. This is because any
|
|
2761 parenthesis that opens an argument list, appearing on a separate line,
|
|
2762 is assigned the @code{statement-cont} syntax instead.
|
|
2763
|
|
2764 A few miscellaneous syntactic symbols that haven't been previously
|
70
|
2765 covered are illustrated by this example:
|
0
|
2766 @example
|
|
2767 @group
|
|
2768
|
|
2769 1: void Bass::play( int volume )
|
|
2770 2: const
|
|
2771 3: @{
|
|
2772 4: /* this line starts a multi-line
|
|
2773 5: * comment. This line should get `c' syntax */
|
|
2774 6:
|
70
|
2775 7: char* a_long_multiline_string = "This line starts a multi-line \
|
0
|
2776 8: string. This line should get `string' syntax.";
|
|
2777 9:
|
|
2778 10: note:
|
|
2779 11: @{
|
|
2780 12: #ifdef LOCK
|
|
2781 13: Lock acquire();
|
|
2782 14: #endif // LOCK
|
|
2783 15: slap_pop();
|
|
2784 16: cout << "I played "
|
|
2785 17: << "a note\n";
|
|
2786 18: @}
|
|
2787 19: @}
|
|
2788
|
|
2789 @end group
|
|
2790 @end example
|
|
2791
|
|
2792 @cindex modifier syntactic symbol
|
|
2793 The lines to note in this example include:
|
|
2794
|
|
2795 @itemize @bullet
|
|
2796
|
70
|
2797 @cindex ansi-funcdecl-cont syntactic symbol
|
0
|
2798 @item
|
70
|
2799 line 2, assigned the @code{ansi-funcdecl-cont} syntax;
|
2
|
2800
|
|
2801 @cindex comment-intro syntactic symbol
|
0
|
2802 @item
|
2
|
2803 line 4, assigned both @code{defun-block-intro} @emph{and}
|
70
|
2804 @code{comment-intro} syntax @footnote{The @code{comment-intro} syntactic
|
|
2805 symbol is is another example of a @dfn{modifier} since it always appears
|
|
2806 on a syntactic analysis list with other symbols, and rarely has an
|
|
2807 associated relative buffer position.};
|
2
|
2808
|
|
2809 @cindex c syntactic symbol
|
0
|
2810 @item
|
2
|
2811 line 5, assigned @code{c} syntax;
|
0
|
2812
|
|
2813 @item
|
|
2814 @cindex syntactic whitespace
|
|
2815 line 6 which, even though it contains nothing but whitespace, is
|
|
2816 assigned @code{defun-block-intro}. Note that the appearance of the
|
|
2817 comment on lines 4 and 5 do not cause line 6 to be assigned
|
|
2818 @code{statement} syntax because comments are considered to be
|
2
|
2819 @dfn{syntactic whitespace}, which are ignored when analyzing
|
0
|
2820 code;
|
|
2821
|
2
|
2822 @cindex string syntactic symbol
|
0
|
2823 @item
|
2
|
2824 line 8, assigned @code{string} syntax;
|
|
2825
|
|
2826 @cindex label syntactic symbol
|
0
|
2827 @item
|
2
|
2828 line 10, assigned @code{label} syntax;
|
|
2829
|
|
2830 @cindex block-open syntactic symbol
|
0
|
2831 @item
|
2
|
2832 line 11, assigned @code{block-open} syntax;
|
|
2833
|
|
2834 @cindex cpp-macro syntactic symbol
|
0
|
2835 @item
|
2
|
2836 lines 12 and 14, assigned @code{cpp-macro} syntax;
|
|
2837
|
|
2838 @cindex stream-op syntactic symbol
|
0
|
2839 @item
|
70
|
2840 line 17, assigned @code{stream-op} syntax @footnote{In C++ only.}.
|
0
|
2841
|
|
2842 @end itemize
|
|
2843
|
|
2844 In Objective-C buffers, there are three additional syntactic symbols
|
|
2845 assigned to various message calling constructs. Here's an example
|
|
2846 illustrating these:
|
|
2847 @example
|
|
2848 @group
|
|
2849
|
|
2850 1: - (void)setDelegate:anObject
|
|
2851 2: withStuff:stuff
|
|
2852 3: @{
|
|
2853 4: [delegate masterWillRebind:self
|
|
2854 5: toDelegate:anObject
|
|
2855 6: withExtraStuff:stuff];
|
|
2856 7: @}
|
|
2857
|
|
2858 @end group
|
|
2859 @end example
|
|
2860
|
2
|
2861 @cindex objc-method-intro syntactic symbol
|
|
2862 @cindex objc-method-args-cont syntactic symbol
|
|
2863 @cindex objc-method-call-cont syntactic symbol
|
0
|
2864 Here, line 1 is assigned @code{objc-method-intro} syntax, and line 2 is
|
|
2865 assigned @code{objc-method-args-cont} syntax. Lines 5 and 6 are both
|
|
2866 assigned @code{objc-method-call-cont} syntax.
|
|
2867
|
2
|
2868 @cindex knr-argdecl-intro
|
|
2869 @cindex knr-argdecl
|
|
2870 Two other syntactic symbols can appear in old style, non-prototyped C
|
|
2871 code @footnote{a.k.a. K&R C, or Kernighan & Ritchie C}:
|
|
2872 @example
|
|
2873 @group
|
|
2874
|
|
2875 1: int add_three_integers(a, b, c)
|
|
2876 2: int a;
|
|
2877 3: int b;
|
|
2878 4: int c;
|
|
2879 5: @{
|
|
2880 6: return a + b + c;
|
|
2881 7: @}
|
|
2882
|
|
2883 @end group
|
|
2884 @end example
|
|
2885
|
|
2886 Here, line 2 is the first line in an argument declaration list and so is
|
|
2887 given the @code{knr-argdecl-intro} syntactic symbol. Subsequent lines
|
|
2888 (i.e. lines 3 and 4 in this example), are given @code{knr-argdecl}
|
|
2889 syntax.
|
0
|
2890
|
|
2891 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
2892 @node Performance Issues, Frequently Asked Questions, Syntactic Symbols, Top
|
|
2893 @comment node-name, next, previous,up
|
|
2894
|
|
2895 @chapter Performance Issues
|
|
2896 @cindex Performance Issues
|
|
2897 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
2898
|
|
2899 C and it's derivative languages are highly complex creatures. Often,
|
70
|
2900 ambiguous code situations arise that require @code{cc-mode} to scan
|
2
|
2901 large portions of the buffer to determine syntactic context. Such
|
|
2902 pathological code@footnote{such as the output of @code{lex(1)}!}
|
70
|
2903 can cause @code{cc-mode} to perform fairly badly.
|
0
|
2904 This section identifies some of the coding styles to watch out for, and
|
|
2905 suggests some workarounds that you can use to improve performance.
|
|
2906
|
70
|
2907 Note that this is an area that will get a lot of attention in
|
|
2908 @code{cc-mode} version 5. The mode should end up being much faster, at
|
|
2909 the expense of dropping Emacs 18 support, owing to the implementation of
|
|
2910 syntactic analysis caching.
|
|
2911
|
|
2912 Because @code{cc-mode} has to scan the buffer backwards from the current
|
0
|
2913 insertion point, and because C's syntax is fairly difficult to parse in
|
70
|
2914 the backwards direction, @code{cc-mode} often tries to find the nearest
|
0
|
2915 position higher up in the buffer from which to begin a forward scan.
|
|
2916 The farther this position is from the current insertion point, the
|
70
|
2917 slower the mode gets. Some coding styles can even force @code{cc-mode}
|
0
|
2918 to scan from the beginning of the buffer!
|
|
2919
|
|
2920 @findex beginning-of-defun
|
|
2921 @findex defun-prompt-regexp
|
|
2922 One of the simplest things you can do to reduce scan time, is make sure
|
|
2923 any brace that opens a top-level block construct always appears in the
|
|
2924 leftmost column. This is actually an Emacs constraint, as embodied in
|
70
|
2925 the @code{beginning-of-defun} function which @code{cc-mode} uses
|
0
|
2926 heavily. If you insist on hanging top-level open braces on the right
|
|
2927 side of the line, then you should set the variable
|
|
2928 @code{defun-prompt-regexp} to something reasonable @footnote{Note that
|
|
2929 this variable is only defined in Emacs 19.}, however that ``something
|
70
|
2930 reasonable'' is difficult to define, so @code{cc-mode} doesn't do it
|
0
|
2931 for you.
|
|
2932
|
70
|
2933 You will probably notice pathological behavior from @code{cc-mode} when
|
0
|
2934 working in files containing large amounts of cpp macros. This is
|
70
|
2935 because @code{cc-mode} cannot quickly skip backwards over these lines.
|
0
|
2936
|
|
2937 @vindex c-recognize-knr-p
|
|
2938 @vindex recognize-knr-p (c-)
|
70
|
2939 Previous versions of @code{cc-mode} had potential performance problems
|
|
2940 when recognizing ``K&R'' style function argument declarations. This was
|
2
|
2941 because there are ambiguities in the C syntax when K&R style argument
|
70
|
2942 lists are used (it is hard to distinguish them from top-level
|
|
2943 declarations). @code{cc-mode} has adopted BOCM's convention for
|
2
|
2944 limiting the search: it assumes that argdecls are indented at least one
|
|
2945 space, and that the function headers are not indented at all. With
|
70
|
2946 current versions of @code{cc-mode},
|
|
2947 @code{c-recognize-knr-p} is deprecated.
|
2
|
2948
|
|
2949 @cindex @file{cc-lobotomy.el} file
|
0
|
2950 @vindex cc-lobotomy-pith-list
|
70
|
2951 You might want to investigate some of the
|
|
2952 speed-ups contained in the file @file{cc-lobotomy.el}, which
|
|
2953 is part of the canonical @code{cc-mode} distribution. As mentioned
|
|
2954 previous, @code{cc-mode} always trades accuracy for speed, however it is
|
|
2955 recognized that sometimes you need speed and can sacrifice some accuracy
|
|
2956 in indentation. The file @file{cc-lobotomy.el} contains hacks that
|
|
2957 will ``dumb down'' @code{cc-mode} in some specific ways, making that
|
|
2958 trade-off of speed for accuracy. I won't go into details of its use
|
|
2959 here; you should read the comments at the top of the file, and look at
|
|
2960 the variable @code{cc-lobotomy-pith-list} for details.
|
0
|
2961
|
|
2962
|
|
2963 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
70
|
2964 @node Frequently Asked Questions, Getting the latest cc-mode release, Performance Issues, Top
|
0
|
2965 @comment node-name, next, previous,up
|
|
2966
|
|
2967 @chapter Frequently Asked Questions
|
|
2968 @cindex Frequently Asked Questions
|
2
|
2969 @comment FAQ
|
0
|
2970 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
2971
|
|
2972 @kindex C-x h
|
|
2973 @kindex ESC C-\
|
|
2974 @kindex ESC C-x
|
|
2975 @kindex C-c C-q
|
|
2976 @kindex ESC C-q
|
|
2977 @kindex ESC C-u
|
|
2978 @kindex RET
|
|
2979 @kindex LFD
|
|
2980 @findex newline-and-indent
|
|
2981 @quotation
|
|
2982
|
|
2983 @strong{Q.} @emph{How do I re-indent the whole file?}
|
|
2984
|
|
2985 @strong{A.} Visit the file and hit @kbd{C-x h} to mark the whole
|
|
2986 buffer. Then hit @kbd{@key{ESC} C-\}.
|
2
|
2987 @sp 1
|
0
|
2988
|
|
2989 @strong{Q.} @emph{How do I re-indent the entire function?
|
|
2990 @kbd{@key{ESC} C-x} doesn't work.}
|
|
2991
|
|
2992 @strong{A.} @kbd{@key{ESC} C-x} is reserved for future Emacs use.
|
|
2993 To re-indent the entire function hit @kbd{C-c C-q}.
|
2
|
2994 @sp 1
|
0
|
2995
|
|
2996 @strong{Q.} @emph{How do I re-indent the current block?}
|
|
2997
|
|
2998 @strong{A.} First move to the brace which opens the block with
|
|
2999 @kbd{@key{ESC} C-u}, then re-indent that expression with
|
|
3000 @kbd{@key{ESC} C-q}.
|
2
|
3001 @sp 1
|
0
|
3002
|
|
3003 @strong{Q.} @emph{Why doesn't the @key{RET} key indent the line to
|
|
3004 where the new text should go after inserting the newline?}
|
|
3005
|
|
3006 @strong{A.} Emacs' convention is that @key{RET} just adds a newline,
|
|
3007 and that @key{LFD} adds a newline and indents it. You can make
|
|
3008 @key{RET} do this too by adding this to your
|
|
3009 @code{c-mode-common-hook} (see the sample @file{.emacs} file
|
|
3010 @ref{Sample .emacs File}):
|
|
3011 @example
|
|
3012
|
|
3013 (define-key c-mode-map "\C-m" 'newline-and-indent)
|
|
3014
|
|
3015 @end example
|
|
3016
|
70
|
3017 This is a very common question. @code{:-)} If you want this to be the
|
|
3018 default behavior, don't lobby me, lobby RMS!
|
2
|
3019 @sp 1
|
0
|
3020
|
|
3021 @strong{Q.} @emph{I put @code{(c-set-offset 'substatement-open 0)}
|
|
3022 in my @file{.emacs} file but I get an error saying that
|
|
3023 @code{c-set-offset}'s function definition is void.}
|
|
3024
|
70
|
3025 @strong{A.} This means that @code{cc-mode} wasn't loaded into your
|
0
|
3026 Emacs session by the time the @code{c-set-offset} call was reached,
|
70
|
3027 mostly likely because @code{cc-mode} is being autoloaded. Instead
|
0
|
3028 of putting the @code{c-set-offset} line in your top-level
|
|
3029 @file{.emacs} file, put it in your @code{c-mode-common-hook}, or
|
|
3030 simply add the following to the top of your @file{.emacs} file:
|
|
3031 @example
|
|
3032
|
|
3033 (require 'cc-mode)
|
|
3034
|
|
3035 @end example
|
|
3036
|
|
3037 See the sample @file{.emacs} file @ref{Sample .emacs File} for
|
|
3038 details.
|
|
3039
|
2
|
3040 @sp 1
|
0
|
3041 @strong{Q.} @emph{How do I make strings, comments, keywords, and other
|
|
3042 constructs appear in different colors, or in bold face, etc.?}
|
|
3043
|
|
3044 @strong{A.} ``Syntax Colorization'' is an Emacs 19 feature, controlled
|
70
|
3045 by @code{font-lock-mode}. It is not part of @code{cc-mode}.
|
0
|
3046
|
2
|
3047 @sp 1
|
|
3048 @strong{Q.} @emph{I @code{setq} @code{c-basic-offset} to 4 in my
|
|
3049 @file{.emacs} file, but why does everything still get indented with only
|
|
3050 2 spaces?}
|
|
3051
|
70
|
3052 @strong{A.} It's because @code{c-basic-offset} is now a ``buffer local
|
|
3053 variable'', meaning its value is unique to each buffer. The prefered
|
|
3054 way to customize this is to change its value in a ``mode hook'' (most
|
|
3055 likely @code{c-mode-common-hook}. Alternatively you can use
|
|
3056 @code{setq-default} to change its value globally.
|
2
|
3057
|
0
|
3058 @end quotation
|
|
3059
|
|
3060
|
|
3061 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
70
|
3062 @node Getting the latest cc-mode release, Sample .emacs File, Frequently Asked Questions, Top
|
0
|
3063 @comment node-name, next, previous,up
|
|
3064
|
70
|
3065 @chapter Getting the latest @code{cc-mode} release
|
|
3066 @cindex Getting the latest @code{cc-mode} release
|
0
|
3067 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
3068
|
70
|
3069 @code{cc-mode} is now distributed with both Emacs 19 and XEmacs 19, so
|
0
|
3070 you would typically just use the version that comes with your Emacs.
|
2
|
3071 These may be slightly out of date due to release schedule skew, so you
|
|
3072 should always check the canonical site for the latest version.
|
0
|
3073
|
|
3074 @example
|
2
|
3075 @group
|
|
3076
|
|
3077 World Wide Web:
|
|
3078
|
|
3079 @code{http://www.python.org/ftp/emacs/}
|
|
3080
|
|
3081 Anonymous FTP:
|
|
3082
|
|
3083 @code{ftp://ftp.python.org/pub/emacs/}
|
|
3084
|
|
3085 @end group
|
0
|
3086 @end example
|
|
3087
|
2
|
3088 There are many files under these directories; you can pick up the entire
|
|
3089 distribution (named @code{cc-mode.tar.gz}; a gzip'd tar file), or any of
|
|
3090 the individual files, including PostScript documentation.
|
0
|
3091
|
70
|
3092 If you do not have anonymous ftp access, you can get the distribution
|
|
3093 through an anonymous ftp-to-mail gateway, such as the one run by DEC at
|
|
3094 @code{ftpmail@@decwrl.dec.com}. To get @code{cc-mode} via email, send
|
|
3095 the following message in the body of your mail to that address:
|
0
|
3096 @example
|
|
3097
|
|
3098 reply <a valid net address back to you>
|
|
3099 connect ftp.python.org
|
|
3100 binary
|
|
3101 uuencode
|
|
3102 chdir pub/emacs
|
|
3103 get cc-mode.tar.gz
|
|
3104
|
|
3105 @end example
|
|
3106 @noindent
|
|
3107 or just send the message "help" for more information on ftpmail.
|
2
|
3108 Response times will vary with the number of requests in the queue. I am
|
|
3109 in no way connected to this service, so I make no claims or guarantees
|
|
3110 about its availability!
|
0
|
3111
|
|
3112 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
70
|
3113 @node Sample .emacs File, Limitations and Known Bugs, Getting the latest cc-mode release, Top
|
0
|
3114 @comment node-name, next, previous,up
|
|
3115
|
70
|
3116 @chapter Sample @file{.emacs} file
|
|
3117 @cindex Sample @file{.emacs} file
|
0
|
3118 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
3119
|
|
3120 @example
|
|
3121 ;; Here's a sample .emacs file that might help you along the way. Just
|
|
3122 ;; copy this region and paste it into your .emacs file. You may want to
|
|
3123 ;; change some of the actual values.
|
|
3124
|
|
3125 (defconst my-c-style
|
70
|
3126 '((c-tab-always-indent . t)
|
|
3127 (c-comment-only-line-offset . 4)
|
|
3128 (c-hanging-braces-alist . ((substatement-open after)
|
|
3129 (brace-list-open)))
|
|
3130 (c-hanging-colons-alist . ((member-init-intro before)
|
|
3131 (inher-intro)
|
|
3132 (case-label after)
|
|
3133 (label after)
|
|
3134 (access-label after)))
|
|
3135 (c-cleanup-list . (scope-operator
|
|
3136 empty-defun-braces
|
|
3137 defun-close-semi))
|
|
3138 (c-offsets-alist . ((arglist-close . c-lineup-arglist)
|
|
3139 (substatement-open . 0)
|
|
3140 (case-label . 4)
|
|
3141 (block-open . 0)
|
|
3142 (knr-argdecl-intro . -)))
|
0
|
3143 (c-echo-syntactic-information-p . t)
|
|
3144 )
|
|
3145 "My C Programming Style")
|
|
3146
|
|
3147 ;; Customizations for all of c-mode, c++-mode, and objc-mode
|
|
3148 (defun my-c-mode-common-hook ()
|
|
3149 ;; add my personal style and set it for the current buffer
|
|
3150 (c-add-style "PERSONAL" my-c-style t)
|
|
3151 ;; offset customizations not in my-c-style
|
|
3152 (c-set-offset 'member-init-intro '++)
|
|
3153 ;; other customizations
|
|
3154 (setq tab-width 8
|
|
3155 ;; this will make sure spaces are used instead of tabs
|
|
3156 indent-tabs-mode nil)
|
|
3157 ;; we like auto-newline and hungry-delete
|
|
3158 (c-toggle-auto-hungry-state 1)
|
2
|
3159 ;; keybindings for all supported languages. We can put these in
|
|
3160 ;; c-mode-map because c++-mode-map, objc-mode-map, and java-mode-map
|
|
3161 ;; inherit from it.
|
0
|
3162 (define-key c-mode-map "\C-m" 'newline-and-indent)
|
|
3163 )
|
|
3164
|
|
3165 ;; the following only works in Emacs 19
|
|
3166 ;; Emacs 18ers can use (setq c-mode-common-hook 'my-c-mode-common-hook)
|
|
3167 (add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
|
|
3168 @end example
|
|
3169
|
|
3170 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
2
|
3171 @node Limitations and Known Bugs, Mailing Lists and Submitting Bug Reports, Sample .emacs File, Top
|
0
|
3172 @comment node-name, next, previous,up
|
|
3173 @chapter Limitations and Known Bugs
|
|
3174 @cindex Limitations and Known Bugs
|
|
3175 @comment * Limitations and Known Bugs
|
|
3176 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
3177
|
|
3178 @itemize @bullet
|
|
3179 @item
|
|
3180 Multi-line macros are not handled properly.
|
|
3181
|
|
3182 @item
|
|
3183 Re-indenting large regions or expressions can be slow.
|
|
3184
|
|
3185 @item
|
70
|
3186 Use with Emacs 18 can be slow and annoying. You should seriously
|
0
|
3187 consider upgrading to Emacs 19.
|
|
3188
|
|
3189 @item
|
70
|
3190 There is still some weird behavior when filling C block comments.
|
|
3191 My suggestion is to check out separate fill packages such as
|
|
3192 @code{filladapt} or @code{adaptive-fill-mode}. These can do a much
|
|
3193 better job of filling comment regions.
|
2
|
3194
|
|
3195 @cindex c-indent-exp
|
|
3196 @cindex indent-exp (c-)
|
0
|
3197 @item
|
2
|
3198 @code{c-indent-exp} has not been fully optimized. It essentially
|
|
3199 equivalent to hitting @kbd{TAB} (@code{c-indent-command}) on every
|
|
3200 line. Some information is cached from line to line, but such caching
|
|
3201 invariable causes inaccuracies in analysis in some bizarre situations.
|
0
|
3202
|
|
3203 @end itemize
|
|
3204
|
|
3205 @c !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
3206 @node Mailing Lists and Submitting Bug Reports, Concept Index, Limitations and Known Bugs, Top
|
|
3207 @comment node-name, next, previous,up
|
|
3208 @chapter Mailing Lists and Submitting Bug Reports
|
|
3209 @cindex Mailing Lists and Submitting Bug Reports
|
|
3210 @c !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
3211
|
|
3212 @kindex C-c C-b
|
|
3213 @findex c-submit-bug-report
|
|
3214 @findex submit-bug-report (c-)
|
|
3215 @cindex beta testers mailing list
|
|
3216 @cindex announcement mailing list
|
|
3217 To report bugs, use the @kbd{C-c C-b} (@code{c-submit-bug-report})
|
|
3218 command. This provides vital information I need to reproduce your
|
|
3219 problem. Make sure you include a concise, but complete code example.
|
|
3220 Please try to boil your example down to just the essential code needed
|
|
3221 to reproduce the problem, and include an exact recipe of steps needed to
|
|
3222 expose the bug. Be especially sure to include any code that appears
|
2
|
3223 @emph{before} your bug example, if you think it might affect my ability
|
|
3224 to reproduce it.
|
|
3225
|
|
3226 Bug reports are now sent to the following email addresses:
|
|
3227 @code{cc-mode-help@@python.org} and
|
70
|
3228 @code{bug-gnu-emacs@@prep.ai.mit.edu}; the latter is mirrored on the Usenet
|
|
3229 newsgroup @code{gnu.emacs.bug}. Other questions and suggestions should
|
|
3230 be mailed to @code{help-gnu-emacs@@prep.ai.mit.edu} which is mirrored on
|
0
|
3231 @code{gnu.emacs.help}.
|
|
3232
|
|
3233
|
|
3234 @c !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
3235 @node Concept Index, Command Index, Mailing Lists and Submitting Bug Reports, Top
|
|
3236 @comment node-name, next, previous, up
|
|
3237 @unnumbered Concept Index
|
|
3238 @c !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
3239
|
|
3240 @printindex cp
|
|
3241
|
|
3242
|
|
3243 @c !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
3244 @node Command Index, Key Index, Concept Index, Top
|
|
3245 @comment node-name, next, previous, up
|
|
3246 @unnumbered Command Index
|
|
3247 @c !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
3248
|
|
3249 @ifinfo
|
|
3250
|
|
3251 @end ifinfo
|
70
|
3252 Since all @code{cc-mode} commands are prepended with the string
|
2
|
3253 @samp{c-}, each appears under its @code{c-@var{<thing>}} name and its
|
|
3254 @code{@var{<thing>} (c-)} name.
|
0
|
3255 @iftex
|
|
3256 @sp 2
|
|
3257 @end iftex
|
|
3258 @printindex fn
|
|
3259
|
|
3260
|
|
3261 @c !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
3262 @node Key Index, Variable Index, Command Index, Top
|
|
3263 @comment node-name, next, previous, up
|
|
3264 @unnumbered Key Index
|
|
3265 @c !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
3266
|
|
3267 @printindex ky
|
|
3268
|
|
3269
|
|
3270 @c !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
3271 @node Variable Index, , Key Index, Top
|
|
3272 @comment node-name, next, previous, up
|
|
3273 @unnumbered Variable Index
|
|
3274 @c !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
3275
|
70
|
3276 @ifinfo
|
|
3277
|
|
3278 @end ifinfo
|
|
3279 Since all @code{cc-mode} variables are prepended with the string
|
2
|
3280 @samp{c-}, each appears under its @code{c-@var{<thing>}} name and its
|
|
3281 @code{@var{<thing>} (c-)} name.
|
0
|
3282 @iftex
|
|
3283 @sp 2
|
|
3284 @end iftex
|
|
3285 @printindex vr
|
|
3286 @summarycontents
|
|
3287 @contents
|
|
3288 @bye
|