]> code.delx.au - gnu-emacs/blob - man/cc-mode.texi
*** empty log message ***
[gnu-emacs] / man / cc-mode.texi
1 \input texinfo
2
3 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4 @comment %**start of header (This is for running Texinfo on a region)
5 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
6
7 @comment No overfull hbox marks in the dvi file.
8 @finalout
9
10 @setfilename ../info/ccmode
11 @settitle CC Mode Version 5 Documentation
12 @dircategory Editors
13 @direntry
14 * CC mode: (ccmode). The GNU Emacs mode for editing C, C++, Objective-C
15 and Java code.
16 @end direntry
17 @footnotestyle end
18
19 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
20 @comment @setchapternewpage odd !! we don't want blank pages !!
21 @comment %**end of header (This is for running Texinfo on a region)
22 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
23
24
25 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
26 @comment
27 @comment Texinfo manual for CC Mode
28 @comment Generated from the original README file by Krishna Padmasola
29 @comment <krishna@earth-gw.njit.edu>
30 @comment
31 @comment Maintained by Barry A. Warsaw and Martin Stjernholm
32 @comment <bug-cc-mode@gnu.org> (or <cc-mode-help@python.org>)
33 @comment
34 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
35
36
37 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
38 @comment The following line inserts the copyright notice
39 @comment into the Info file.
40 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
41
42 @ifinfo
43 Copyright @copyright{} 1995,1996,1997,1998,1999 Free Software Foundation, Inc.
44 @end ifinfo
45
46 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
47 @comment !!!The titlepage section does not appear in the Info file.!!!
48 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
49
50 @titlepage
51 @sp 10
52
53
54 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
55 @comment The title is printed in a large font.
56 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
57
58 @center @titlefont{CC Mode 5.26}
59 @sp 2
60 @center @subtitlefont{A GNU Emacs mode for editing C and C-like languages}
61 @sp 2
62 @center Barry A. Warsaw
63 @center Martin Stjernholm
64
65
66 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
67 @comment The following two commands start the copyright page
68 @comment for the printed manual. This will not appear in the Info file.
69 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
70
71 @page
72 @vskip 0pt plus 1filll
73 Copyright @copyright{} 1995,1996,1997,1998,1999 Free Software Foundation, Inc.
74 @end titlepage
75
76
77 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
78 @comment The Top node contains the master menu for the Info file.
79 @comment This appears only in the Info file, not the printed manual.
80 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
81
82 @node Top, Introduction, (dir), (dir)
83 @comment node-name, next, previous, up
84
85 @macro ccmode
86 CC Mode
87 @end macro
88
89 @ifinfo
90 @top @ccmode{}
91
92 @ccmode{} is a GNU Emacs mode for editing files containing C, C++,
93 Objective-C, Java, CORBA IDL, and Pike code. It provides syntax-based
94 indentation and has several handy commands and some minor modes to make
95 the editing easier. Note that @ccmode{} does @emph{not} provide
96 font-locking; there are other Emacs packages for that.
97 @end ifinfo
98
99 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
100 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
101
102 @menu
103 * Introduction::
104 * Getting Connected::
105 * New Indentation Engine::
106 * Minor Modes::
107 * Text Filling and Line Breaking::
108 * Commands::
109 * Customizing Indentation::
110 * Syntactic Symbols::
111 * Indentation Functions::
112 * Performance Issues::
113 * Frequently Asked Questions::
114 * Getting the Latest CC Mode Release::
115 * Sample .emacs File::
116 * Limitations and Known Bugs::
117 * Mailing Lists and Submitting Bug Reports::
118
119 --- Indices ---
120
121 * Concept Index::
122 * Command Index::
123 * Key Index::
124 * Variable Index::
125
126 --- The Detailed Node Listing ---
127
128 New Indentation Engine
129
130 * Syntactic Analysis::
131 * Indentation Calculation::
132
133 Minor Modes
134
135 * Auto-newline Insertion::
136 * Hungry-deletion of Whitespace::
137
138 Auto-newline Insertion
139
140 * Hanging Braces::
141 * Hanging Colons::
142 * Hanging Semi-colons and Commas::
143 * Other Electric Commands::
144 * Clean-ups::
145
146 Commands
147
148 * Indentation Commands::
149 * Movement Commands::
150 * Other Commands::
151
152 Customizing Indentation
153
154 * Interactive Customization::
155 * Permanent Customization::
156 * Hooks::
157 * Styles::
158 * Advanced Customizations::
159
160 Styles
161
162 * Built-in Styles::
163 * Adding Styles::
164 * File Styles::
165
166 Advanced Customizations
167
168 * Custom Indentation Functions::
169 * Custom Brace and Colon Hanging::
170 * Customizing Semi-colons and Commas::
171 * Other Special Indentations::
172 @end menu
173
174
175 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
176 @node Introduction, Getting Connected, Top, Top
177 @comment node-name, next, previous, up
178 @chapter Introduction
179 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
180
181 @cindex BOCM
182
183 Welcome to @ccmode{}, a GNU Emacs mode for editing files containing C,
184 C++, Objective-C, Java, CORBA IDL, and Pike code. This incarnation of
185 the mode is descendant from @file{c-mode.el} (also called "Boring Old C
186 Mode" or BOCM @t{:-)}, and @file{c++-mode.el} version 2, which Barry has
187 been maintaining since 1992. @ccmode{} represents a significant
188 milestone in the mode's life. It has been fully merged back with Emacs
189 19's @file{c-mode.el}. Also a new, more intuitive and flexible mechanism
190 for controlling indentation has been developed. Late in 1997, Martin
191 joined the @ccmode{} Maintainers Team, and implemented the Pike support.
192
193 This manual describes @ccmode{}
194 @comment The following line must appear on its own, so that the automated
195 version 5.26.
196 @comment Release.py script can update the version number automatically
197
198 @ccmode{} supports the editing of K&R and ANSI C, @dfn{ARM}
199 @footnote{@cite{The Annotated C++ Reference Manual}, by Ellis and
200 Stroustrup.} C++, Objective-C, Java, CORBA's Interface Definition
201 Language, and Pike@footnote{A C-like scripting language with its roots
202 in the LPC language used in some MUD engines. See
203 @uref{http://pike.idonex.se/}.} files. In this way, you can easily set
204 up consistent coding styles for use in editing all of these languages.
205 @ccmode{} does @emph{not} handle font-locking (a.k.a. syntax coloring,
206 keyword highlighting) or anything of that nature, for any of these
207 modes. Font-locking is handled by other Emacs packages.
208
209 This manual will describe the following:
210
211 @itemize @bullet
212 @item
213 How to get started using @ccmode{}.
214
215 @item
216 How the new indentation engine works.
217
218 @item
219 How to customize the new indentation engine.
220
221 @end itemize
222
223 @findex c-mode
224 @findex c++-mode
225 @findex objc-mode
226 @findex java-mode
227 @findex idl-mode
228 @findex pike-mode
229 Note that the name of this package is ``@ccmode{}'', but there is no top
230 level @code{cc-mode} entry point. All of the variables, commands, and
231 functions in @ccmode{} are prefixed with @code{c-@var{<thing>}}, and
232 @code{c-mode}, @code{c++-mode}, @code{objc-mode}, @code{java-mode},
233 @code{idl-mode}, and @code{pike-mode} entry points are provided. This
234 package is intended to be a replacement for @file{c-mode.el} and
235 @file{c++-mode.el}.
236
237 @cindex @file{cc-compat.el} file
238 This distribution also contains a file
239 called @file{cc-compat.el} which should ease your transition from BOCM
240 to @ccmode{}. If you have a BOCM configuration you are really happy
241 with, and want to postpone learning how to configure @ccmode{}, take a
242 look at that file. It maps BOCM configuration variables to @ccmode{}'s
243 new indentation model. It is not actively supported so for the long
244 run, you should learn how to customize @ccmode{} to support your coding
245 style.
246
247 A special word of thanks goes to Krishna Padmasola for his work in
248 converting the original @file{README} file to Texinfo format. I'd also
249 like to thank all the @ccmode{} victims who help enormously during the
250 early beta stages of @ccmode{}'s development.
251
252
253 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
254 @node Getting Connected, New Indentation Engine, Introduction, Top
255 @comment node-name, next, previous, up
256 @chapter Getting Connected
257 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
258
259 If you got this version of @ccmode{} with Emacs or XEmacs, it should
260 work just fine right out of the box. Note however that you may not have
261 the latest @ccmode{} release and may want to upgrade your copy.
262
263 If you are upgrading an existing @ccmode{} installation, please see the
264 @file{README} file for installation details. @ccmode{} may not work
265 with older versions of Emacs or XEmacs. See the @ccmode{} release notes
266 Web pages for the latest information on Emacs version and package
267 compatibility (@pxref{Getting the Latest CC Mode Release}).
268
269 @cindex @file{cc-mode-18.el} file
270 @emph{Note that @ccmode{} no longer
271 works with Emacs 18!}, so if you haven't upgraded from Emacs 18 by now,
272 you are out of luck.
273
274 @findex c-version
275 @findex version (c-)
276 You can find out what version of @ccmode{} you are using by visiting a C
277 file and entering @kbd{M-x c-version RET}. You should see this message in
278 the echo area:
279 @example
280
281 Using CC Mode version 5.XX
282
283 @end example
284
285 @noindent
286 where @samp{XX} is the minor release number.
287
288
289 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
290 @node New Indentation Engine, Minor Modes, Getting Connected, Top
291 @comment node-name, next, previous, up
292 @chapter New Indentation Engine
293 @cindex indentation engine
294 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
295
296 @ccmode{} has a new indentation engine, providing a simplified, yet
297 flexible and general mechanism for customizing indentation. It separates
298 indentation calculation into two steps: first, @ccmode{} analyzes the
299 line of code being indented to determine the kind of language construct
300 it's looking at, then it applies user defined offsets to the current
301 line based on this analysis.
302
303 This section will briefly cover how indentation is calculated in
304 @ccmode{}. It is important to understand the indentation model
305 being used so that you will know how to customize @ccmode{} for
306 your personal coding style.
307
308 @menu
309 * Syntactic Analysis::
310 * Indentation Calculation::
311 @end menu
312
313
314 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
315 @node Syntactic Analysis, Indentation Calculation, , New Indentation Engine
316 @comment node-name, next, previous, up
317 @section Syntactic Analysis
318 @cindex syntactic analysis
319 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
320
321 @cindex relative buffer position
322 @cindex syntactic symbol
323 @cindex syntactic component
324 @cindex syntactic component list
325 The first thing @ccmode{} does when indenting a line of code, is to
326 analyze the line, determining the @dfn{syntactic component list} of the
327 construct on that line. A syntactic component consists of a pair of
328 information (in lisp parlance, a @emph{cons cell}), where the first part
329 is a @dfn{syntactic symbol}, and the second part is a @dfn{relative
330 buffer position}. Syntactic symbols describe elements of C code
331 @footnote{Unless otherwise noted, the term ``C code'' to refers to all
332 the C-like languages.}, e.g. @code{statement}, @code{substatement},
333 @code{class-open}, @code{class-close}, etc. @xref{Syntactic Symbols},
334 for a complete list of currently recognized syntactic symbols and their
335 semantics. The style variable @code{c-offsets-alist} also contains the
336 list of currently supported syntactic symbols.
337
338 Conceptually, a line of C code is always indented relative to the
339 indentation of some line higher up in the buffer. This is represented
340 by the relative buffer position in the syntactic component.
341
342 Here is an example. Suppose we had the following code as the only thing
343 in a C++ buffer @footnote{The line numbers in this and future examples
344 don't actually appear in the buffer, of course!}:
345 @example
346 @group
347
348 1: void swap( int& a, int& b )
349 2: @{
350 3: int tmp = a;
351 4: a = b;
352 5: b = tmp;
353 6: @}
354
355 @end group
356 @end example
357
358 @kindex C-c C-s
359 @findex c-show-syntactic-information
360 @findex show-syntactic-information (c-)
361 We can use the command @kbd{C-c C-s}
362 (@code{c-show-syntactic-information}) to simply report what the
363 syntactic analysis is for the current line. Running this command on
364 line 4 of this example, we'd see in the echo area@footnote{With a universal
365 argument (i.e. @kbd{C-u C-c C-s}) the analysis is inserted into the
366 buffer as a comment
367 on the current line.}:
368 @example
369
370 ((statement . 35))
371
372 @end example
373
374 This tells us that the line is a statement and it is indented relative
375 to buffer position 35, which happens to be the @samp{i} in @code{int} on
376 line 3. If you were to move point to line 3 and hit @kbd{C-c C-s}, you
377 would see:
378 @example
379
380 ((defun-block-intro . 29))
381
382 @end example
383
384 This indicates that the @samp{int} line is the first statement in a top
385 level function block, and is indented relative to buffer position 29,
386 which is the brace just after the function header.
387
388 Here's another example:
389 @example
390 @group
391
392 1: int add( int val, int incr, int doit )
393 2: @{
394 3: if( doit )
395 4: @{
396 5: return( val + incr );
397 6: @}
398 7: return( val );
399 8: @}
400
401 @end group
402 @end example
403
404 @noindent
405 Hitting @kbd{C-c C-s} on line 4 gives us:
406 @example
407
408 ((substatement-open . 46))
409
410 @end example
411
412 @cindex substatement
413 @cindex substatement block
414 @noindent
415 which tells us that this is a brace that @emph{opens} a substatement
416 block. @footnote{A @dfn{substatement} is the line after a
417 conditional statement, such as @code{if}, @code{else}, @code{while},
418 @code{do}, @code{switch}, etc. A @dfn{substatement
419 block} is a brace block following one of these conditional statements.}
420
421 @cindex comment-only line
422 Syntactic component lists can contain more than one component, and
423 individual syntactic components need not have relative buffer positions.
424 The most common example of this is a line that contains a @dfn{comment
425 only line}.
426 @example
427 @group
428
429 1: void draw_list( List<Drawables>& drawables )
430 2: @{
431 3: // call the virtual draw() method on each element in list
432 4: for( int i=0; i < drawables.count(), ++i )
433 5: @{
434 6: drawables[i].draw();
435 7: @}
436 8: @}
437
438 @end group
439 @end example
440
441 @noindent
442 Hitting @kbd{C-c C-s} on line 3 of this example gives:
443 @example
444
445 ((comment-intro) (defun-block-intro . 46))
446
447 @end example
448
449 @noindent
450 and you can see that the syntactic component list contains two syntactic
451 components. Also notice that the first component,
452 @samp{(comment-intro)} has no relative buffer position.
453
454
455 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
456 @node Indentation Calculation, , Syntactic Analysis, New Indentation Engine
457 @comment node-name, next, previous, up
458 @section Indentation Calculation
459 @cindex indentation calculation
460 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
461
462 Indentation for a line is calculated using the syntactic
463 component list derived in step 1 above (@pxref{Syntactic Analysis}).
464 Each component contributes to the final total indentation of the line in
465 two ways.
466
467 First, the syntactic symbols are looked up in the @code{c-offsets-alist}
468 style variable, which is an association list of syntactic symbols and
469 the offsets to apply for those symbols. These offsets are added to a
470 running total.
471
472 Second, if the component has a relative buffer position, @ccmode{}
473 adds the column number of that position to the running total. By adding
474 up the offsets and columns for every syntactic component on the list,
475 the final total indentation for the current line is computed.
476
477 Let's use our two code examples above to see how this works. Here is
478 our first example again:
479 @example
480 @group
481
482 1: void swap( int& a, int& b )
483 2: @{
484 3: int tmp = a;
485 4: a = b;
486 5: b = tmp;
487 6: @}
488
489 @end group
490 @end example
491
492 Let's say point is on line 3 and we hit the @kbd{TAB} key to re-indent
493 the line. Remember that the syntactic component list for that
494 line is:
495 @example
496
497 ((defun-block-intro . 29))
498
499 @end example
500
501 @noindent
502 @ccmode{} looks up @code{defun-block-intro} in the
503 @code{c-offsets-alist} style variable. Let's say it finds the value
504 @samp{4}; it adds this to the running total (initialized to zero),
505 yielding a running total indentation of 4 spaces.
506
507 Next @ccmode{} goes to buffer position 29 and asks for the current
508 column. This brace is in column zero, so @ccmode{}
509 adds @samp{0} to the running total. Since there is only one syntactic
510 component on the list for this line, indentation calculation is
511 complete, and the total indentation for the line
512 is 4 spaces.
513
514 Here's another example:
515 @example
516 @group
517
518 1: int add( int val, int incr, int doit )
519 2: @{
520 3: if( doit )
521 4: @{
522 5: return( val + incr );
523 6: @}
524 7: return( val );
525 8: @}
526
527 @end group
528 @end example
529
530 If we were to hit @kbd{TAB} on line 4 in the above example, the same
531 basic process is performed, despite the differences in the syntactic
532 component list. Remember that the list for this line is:
533 @example
534
535 ((substatement-open . 46))
536
537 @end example
538
539 Here, @ccmode{} first looks up the @code{substatement-open} symbol
540 in @code{c-offsets-alist}. Let's say it finds the value @samp{4}. This
541 yields a running total of 4. @ccmode{} then goes to
542 buffer position 46, which is the @samp{i} in @code{if} on line 3. This
543 character is in the fourth column on that line so adding this to the
544 running total yields an indentation for the line of 8 spaces.
545
546 Simple, huh?
547
548 Actually, the mode usually just does The Right Thing without you having
549 to think about it in this much detail. But when customizing
550 indentation, it's helpful to understand the general indentation model
551 being used.
552
553 @vindex c-echo-syntactic-information-p
554 @vindex echo-syntactic-information-p (c-)
555 As you configure @ccmode{}, you might want to set the variable
556 @code{c-echo-syntactic-information-p} to non-@code{nil} so that the
557 syntactic component list and calculated offset will always be echoed in
558 the minibuffer when you hit @kbd{TAB}.
559
560
561 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
562 @node Minor Modes, Text Filling and Line Breaking, New Indentation Engine, Top
563 @comment node-name, next, previous, up
564 @chapter Minor Modes
565 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
566
567 @ccmode{} contains two minor-mode-like features that you should
568 find useful while you enter new C code. The first is called
569 @dfn{auto-newline} mode, and the second is called @dfn{hungry-delete}
570 mode. These minor modes can be toggled on and off independently, and
571 @ccmode{} can be configured so that it starts up with any
572 combination of these minor modes. By default, both of these minor modes
573 are turned off.
574
575 The state of the minor modes is always reflected in the minor mode list
576 on the modeline of the @ccmode{} buffer. When auto-newline mode is
577 enabled, you will see @samp{C/a} on the mode line @footnote{The @samp{C}
578 would be replaced with @samp{C++}, @samp{ObjC}, @samp{Java}, @samp{IDL},
579 or @samp{Pike} for the respective languages.}. When hungry delete mode
580 is enabled you would see @samp{C/h} and when both modes are enabled,
581 you'd see @samp{C/ah}.
582
583 @kindex C-c C-a
584 @kindex C-c C-d
585 @kindex C-c C-t
586 @findex c-toggle-hungry-state
587 @findex c-toggle-auto-state
588 @findex c-toggle-auto-hungry-state
589 @findex toggle-hungry-state (c-)
590 @findex toggle-auto-state (c-)
591 @findex toggle-auto-hungry-state (c-)
592 @ccmode{} provides keybindings which allow you to toggle the minor
593 modes on the fly while editing code. To toggle just the auto-newline
594 state, hit @kbd{C-c C-a} (@code{c-toggle-auto-state}). When you do
595 this, you should see the @samp{a} indicator either appear or disappear
596 on the modeline. Similarly, to toggle just the hungry-delete state, use
597 @kbd{C-c C-d} (@code{c-toggle-hungry-state}), and to toggle both states,
598 use @kbd{C-c C-t} (@code{c-toggle-auto-hungry-state}).
599
600 To set up the auto-newline and hungry-delete states to your preferred
601 values, you would need to add some lisp to your @file{.emacs} file that
602 called one of the @code{c-toggle-*-state} functions directly. When
603 called programmatically, each function takes a numeric value, where
604 a positive number enables the minor mode, a negative number disables the
605 mode, and zero toggles the current state of the mode.
606
607 So for example, if you wanted to enable both auto-newline and
608 hungry-delete for all your C file editing, you could add the following
609 to your @file{.emacs} file:
610 @example
611
612 (add-hook 'c-mode-common-hook
613 '(lambda () (c-toggle-auto-hungry-state 1)))
614
615 @end example
616
617
618 @cindex electric characters
619
620 @menu
621 * Auto-newline Insertion::
622 * Hungry-deletion of Whitespace::
623 @end menu
624
625
626 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
627 @node Auto-newline Insertion, Hungry-deletion of Whitespace, , Minor Modes
628 @comment node-name, next, previous, up
629 @section Auto-newline Insertion
630 @cindex auto-newline insertion
631 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
632
633 @cindex electric commands
634 Auto-newline minor mode works by enabling certain @dfn{electric
635 commands}. Electric commands are typically bound to special characters
636 such as the left and right braces, colons, semi-colons, etc., which when
637 typed, perform some magic formatting in addition to inserting the typed
638 character. As a general rule, electric commands are only electric when
639 the following conditions apply:
640
641 @itemize @bullet
642 @item
643 Auto-newline minor mode is enabled, as evidenced by a @samp{C/a} or
644 @samp{C/ah} indicator on the modeline.
645
646 @cindex literal
647 @cindex syntactic whitespace
648 @item
649 The character was not typed inside of a literal @footnote{A
650 @dfn{literal} is defined as any comment, string, or C preprocessor macro
651 definition. These constructs are also known as @dfn{syntactic
652 whitespace} since they are usually ignored when scanning C code.}.
653
654 @item
655 @kindex C-u
656 No numeric argument was supplied to the command (i.e. it was typed as
657 normal, with no @kbd{C-u} prefix).
658
659 @end itemize
660
661 @menu
662 * Hanging Braces::
663 * Hanging Colons::
664 * Hanging Semi-colons and Commas::
665 * Other Electric Commands::
666 * Clean-ups::
667 @end menu
668
669
670 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
671 @node Hanging Braces, Hanging Colons, , Auto-newline Insertion
672 @comment node-name, next, previous, up
673 @subsection Hanging Braces
674 @cindex hanging braces
675 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
676
677 @findex c-electric-brace
678 @findex electric-brace (c-)
679 @vindex c-hanging-braces-alist
680 @vindex hanging-braces-alist (c-)
681 When you type either an open or close brace (i.e. @kbd{@{} or @kbd{@}}),
682 the electric command @code{c-electric-brace} gets run. This command has
683 two electric formatting behaviors. First, it will perform some
684 re-indentation of the line the brace was typed on, and second, it will
685 add various newlines before and/or after the typed brace.
686 Re-indentation occurs automatically whenever the electric behavior is
687 enabled. If the brace ends up on a line other than the one it was typed
688 on, then that line is also re-indented.
689
690 @cindex class-open syntactic symbol
691 @cindex class-close syntactic symbol
692 @cindex defun-open syntactic symbol
693 @cindex defun-close syntactic symbol
694 @cindex inline-open syntactic symbol
695 @cindex inline-close syntactic symbol
696 @cindex brace-list-open syntactic symbol
697 @cindex brace-list-close syntactic symbol
698 @cindex brace-list-intro syntactic symbol
699 @cindex brace-entry-open syntactic symbol
700 @cindex block-open syntactic symbol
701 @cindex block-close syntactic symbol
702 @cindex substatement-open syntactic symbol
703 @cindex statement-case-open syntactic symbol
704 @cindex extern-lang-open syntactic symbol
705 @cindex extern-lang-close syntactic symbol
706 @cindex namespace-open syntactic symbol
707 @cindex namespace-close syntactic symbol
708 @cindex inexpr-class-open symbol
709 @cindex inexpr-class-close symbol
710
711 The default in auto-newline mode is to insert newlines both before and
712 after a brace, but that can be controlled by the
713 @code{c-hanging-braces-alist} style variable. This variable contains a
714 mapping between syntactic symbols related to braces, and a list of
715 places to insert a newline. The syntactic symbols that are useful for
716 this list are: @code{class-open}, @code{class-close}, @code{defun-open},
717 @code{defun-close}, @code{inline-open}, @code{inline-close},
718 @code{brace-list-open}, @code{brace-list-close},
719 @code{brace-list-intro}, @code{brace-entry-open}, @code{block-open},
720 @code{block-close}, @code{substatement-open},
721 @code{statement-case-open}, @code{extern-lang-open},
722 @code{extern-lang-close}, @code{namespace-open}, @code{namespace-close},
723 @code{inexpr-class-open}, and @code{inexpr-class-close}@footnote{Note
724 that the aggregate constructs in Pike mode, @samp{(@{}, @samp{@})},
725 @samp{([}, @samp{])}, and @samp{(<}, @samp{>)}, do not count as brace
726 lists in this regard, even though they do for normal indentation
727 purposes. It's currently not possible to set automatic newlines on
728 these constructs.}. @xref{Syntactic Symbols}, for a more detailed
729 description of these syntactic symbols, except for
730 @code{inexpr-class-open} and @code{inexpr-class-close}, which aren't
731 actual syntactic symbols.
732
733 The braces of anonymous inner classes in Java are given the special
734 symbols @code{inexpr-class-open} and @code{inexpr-class-close}, so that
735 they can be distinguished from the braces of normal classes@footnote{The
736 braces of anonymous classes produces a combination of
737 @code{inexpr-class}, and @code{class-open} or @code{class-close} in
738 normal indentation analysis.}.
739
740 The value associated with each syntactic symbol in this association list
741 is called an @var{ACTION} which can be either a function or a list.
742 @xref{Custom Brace and Colon Hanging}, for a more detailed discussion of
743 using a function as a brace hanging @var{ACTION}.
744
745 When the @var{ACTION} is a list, it can contain any combination of the
746 symbols @code{before} and @code{after}, directing @ccmode{} where to
747 put newlines in relationship to the brace being inserted. Thus, if the
748 list contains only the symbol @code{after}, then the brace is said to
749 @dfn{hang} on the right side of the line, as in:
750 @example
751 @group
752
753 // here, open braces always `hang'
754 void spam( int i ) @{
755 if( i == 7 ) @{
756 dosomething(i);
757 @}
758 @}
759
760
761 @end group
762 @end example
763
764 When the list contains both @code{after} and @code{before}, the braces
765 will appear on a line by themselves, as shown by the close braces in the
766 above example. The list can also be empty, in which case no newlines
767 are added either before or after the brace.
768
769 If a syntactic symbol is missing entirely from
770 @code{c-hanging-braces-alist}, it's treated in the same way as an
771 @var{ACTION} with a list containing @code{before} and @code{after}, so
772 that braces by default end up on their own line.
773
774 For example, the default value of @code{c-hanging-braces-alist} is:
775 @example
776 @group
777
778 ((brace-list-open)
779 (brace-entry-open)
780 (substatement-open after)
781 (block-close . c-snug-do-while)
782 (extern-lang-open after)
783 (inexpr-class-open after)
784 (inexpr-class-close before))
785
786 @end group
787 @end example
788
789 @noindent which says that @code{brace-list-open} and
790 @code{brace-entry-open} braces should both hang on the right side, and
791 allow subsequent text to follow on the same line as the brace. Also,
792 @code{substatement-open}, @code{extern-lang-open}, and
793 @code{inexpr-class-open} braces should hang on the right side, but
794 subsequent text should follow on the next line. The opposite holds for
795 @code{inexpr-class-close} braces; they won't hang, but the following
796 text continues on the same line. Here, in the @code{block-close} entry,
797 you also see an example of using a function as an @var{ACTION}. In all
798 other cases, braces are put on a line by themselves.
799
800 A word of caution: it is not a good idea to hang top-level construct
801 introducing braces, such as @code{class-open} or @code{defun-open}.
802 Emacs makes an assumption that such braces will always appear in column
803 zero, hanging them can introduce performance problems.
804 @xref{Performance Issues}, for more information.
805
806
807 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
808 @node Hanging Colons, Hanging Semi-colons and Commas, Hanging Braces, Auto-newline Insertion
809 @comment node-name, next, previous, up
810 @subsection Hanging Colons
811 @cindex hanging colons
812 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
813
814 @vindex hanging-colons-alist (c-)
815 @vindex c-hanging-colons-alist
816 Using a mechanism similar to brace hanging (@pxref{Hanging Braces}),
817 colons can also be made to hang using the style variable
818 @code{c-hanging-colons-alist}. The syntactic symbols appropriate for
819 this assocation list are: @code{case-label}, @code{label},
820 @code{access-label}, @code{member-init-intro}, and @code{inher-intro}.
821 Note however that for @code{c-hanging-colons-alist}, @var{ACTION}s as
822 functions are not supported. See also @ref{Custom Brace and Colon
823 Hanging} for details.
824
825 In C++, double-colons are used as a scope operator but because these
826 colons always appear right next to each other, newlines before and after
827 them are controlled by a different mechanism, called @dfn{clean-ups} in
828 @ccmode{}. @xref{Clean-ups}, for details.
829
830
831 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
832 @node Hanging Semi-colons and Commas, Other Electric Commands, Hanging Colons, Auto-newline Insertion
833 @comment node-name, next, previous, up
834 @subsection Hanging Semi-colons and Commas
835 @cindex hanging semi-colons
836 @cindex hanging commas
837 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
838
839 Semicolons and commas are also electric in @ccmode{}, but since
840 these characters do not correspond directly to syntactic symbols, a
841 different mechanism is used to determine whether newlines should be
842 automatically inserted after these characters. @xref{Customizing
843 Semi-colons and Commas}, for details.
844
845
846 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
847 @node Other Electric Commands, Clean-ups, Hanging Semi-colons and Commas, Auto-newline Insertion
848 @comment node-name, next, previous, up
849 @subsection Other Electric Commands
850 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
851
852 @kindex #
853 @findex c-electric-pound
854 @vindex c-electric-pound-behavior
855 @findex electric-pound (c-)
856 @vindex electric-pound-behavior (c-)
857 A few other keys also provide electric behavior. For example
858 @kbd{#} (@code{c-electric-pound}) is electric when typed as
859 the first non-whitespace character on a line. In this case, the
860 variable @code{c-electric-pound-behavior} is consulted for the electric
861 behavior. This variable takes a list value, although the only element
862 currently defined is @code{alignleft}, which tells this command to force
863 the @samp{#} character into column zero. This is useful for entering
864 C preprocessor macro definitions.
865
866 @findex c-electric-star
867 @findex c-electric-slash
868 @findex electric-star (c-)
869 @findex electric-slash (c-)
870 Stars and slashes (i.e. @kbd{*} and @kbd{/}, @code{c-electric-star} and
871 @code{c-electric-slash} respectively) are also electric under
872 certain circumstances. If a star is inserted as the second character of
873 a C style block comment on a comment-only line, then the comment
874 delimiter is indented as defined by @code{c-offsets-alist}. A
875 comment-only line is defined as a line which contains only a comment, as
876 in:
877 @example
878 @group
879
880 void spam( int i )
881 @{
882 // this is a comment-only line...
883 if( i == 7 ) // but this is not
884 @{
885 dosomething(i);
886 @}
887 @}
888
889 @end group
890 @end example
891
892 Likewise, if a slash is inserted as the second slash in a C++ style line
893 comment (also only on a comment-only line), then the line is indented as
894 defined by @code{c-offsets-alist}.
895
896 @findex c-electric-lt-gt
897 @findex electric-lt-gt (c-)
898 @kindex <
899 @kindex >
900 Less-than and greater-than signs (@code{c-electric-lt-gt}) are also
901 electric, but only in C++ mode. Hitting the second of two @kbd{<} or
902 @kbd{>} keys re-indents the line if it is a C++ style stream operator.
903
904 @findex c-electric-paren
905 @findex electric-paren (c-)
906 @kindex (
907 @kindex )
908 The normal parenthesis characters @samp{(} and @samp{)} also reindent
909 the current line if they are used in normal code. This is useful for
910 getting the closing parenthesis of an argument list aligned
911 automatically.
912
913
914 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
915 @node Clean-ups, , Other Electric Commands, Auto-newline Insertion
916 @comment node-name, next, previous, up
917 @subsection Clean-ups
918 @cindex clean-ups
919 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
920
921 @dfn{Clean-ups} are a mechanism complementary to colon and brace
922 hanging. On the surface, it would seem that clean-ups overlap the
923 functionality provided by the @code{c-hanging-*-alist} variables, and
924 similarly, clean-ups are only enabled when auto-newline minor mode is
925 enabled. Clean-ups are used however to adjust code ``after-the-fact'',
926 i.e. to eliminate some whitespace that is inserted by electric
927 commands, or whitespace that contains intervening constructs.
928
929 @vindex c-cleanup-list
930 @vindex cleanup-list (c-)
931 @cindex literal
932 You can configure @ccmode{}'s clean-ups by setting the style variable
933 @code{c-cleanup-list}, which is a list of clean-up symbols. By default,
934 @ccmode{} cleans up only the @code{scope-operator} construct, which is
935 necessary for proper C++ support. Note that clean-ups are only
936 performed when the construct does not occur within a literal
937 (@pxref{Auto-newline Insertion}), and when there is nothing but
938 whitespace appearing between the individual components of the construct.
939
940 There are currently only five specific constructs that @ccmode{}
941 can clean up, as indicated by these symbols:
942
943 @itemize @bullet
944 @item
945 @code{brace-else-brace} --- cleans up @samp{@} else @{} constructs by
946 placing the entire construct on a single line. Clean-up occurs when the
947 open brace after the @samp{else} is typed. So for example, this:
948 @example
949 @group
950
951 void spam(int i)
952 @{
953 if( i==7 )
954 @{
955 dosomething();
956 @}
957 else
958 @{
959
960 @end group
961 @end example
962 @noindent
963 appears like this after the open brace is typed:
964 @example
965 @group
966
967 void spam(int i)
968 @{
969 if( i==7 ) @{
970 dosomething();
971 @} else @{
972
973 @end group
974 @end example
975
976 @item
977 @code{brace-elseif-brace} --- similar to the @code{brace-else-brace}
978 clean-up, but this cleans up @samp{@} else if (...) @{} constructs. For
979 example:
980 @example
981 @group
982
983 void spam(int i)
984 @{
985 if( i==7 )
986 @{
987 dosomething();
988 @}
989 else if( i==3 )
990 @{
991
992 @end group
993 @end example
994 @noindent
995 appears like this after the open parenthesis is typed:
996 @example
997 @group
998
999 void spam(int i)
1000 @{
1001 if( i==7 ) @{
1002 dosomething();
1003 @} else if( i==3 )
1004 @{
1005
1006 @end group
1007 @end example
1008 @noindent
1009 and like this after the open brace is typed:
1010 @example
1011 @group
1012
1013 void spam(int i)
1014 @{
1015 if( i==7 ) @{
1016 dosomething();
1017 @} else if( i==3 ) @{
1018
1019 @end group
1020 @end example
1021
1022 @item
1023 @code{brace-catch-brace} --- analogous to @code{brace-elseif-brace}, but
1024 cleans up @samp{@} catch (...) @{} in C++ and Java mode.
1025
1026 @item
1027 @code{empty-defun-braces} --- cleans up braces following a top-level
1028 function or class definition that contains no body. Clean up occurs
1029 when the closing brace is typed. Thus the following:
1030 @example
1031 @group
1032
1033 class Spam
1034 @{
1035 @}
1036
1037 @end group
1038 @end example
1039 @noindent
1040 is transformed into this when the close brace is typed:
1041 @example
1042 @group
1043
1044 class Spam
1045 @{@}
1046
1047 @end group
1048 @end example
1049
1050 @item
1051 @code{defun-close-semi} --- cleans up the terminating semi-colon on
1052 top-level function or class definitions when they follow a close
1053 brace. Clean up occurs when the semi-colon is typed.
1054 So for example, the following:
1055 @example
1056 @group
1057
1058 class Spam
1059 @{
1060 @}
1061 ;
1062
1063 @end group
1064 @end example
1065 @noindent
1066 is transformed into this when the semi-colon is typed:
1067
1068 @example
1069 @group
1070
1071 class Spam
1072 @{
1073 @};
1074
1075 @end group
1076 @end example
1077
1078 @item
1079 @code{list-close-comma} --- cleans up commas following braces in array
1080 and aggregate initializers. Clean up occurs when the comma is typed.
1081
1082 @item
1083 @code{scope-operator} --- cleans up double colons which may designate a
1084 C++ scope operator split across multiple lines@footnote{Certain C++
1085 constructs introduce ambiguous situations, so @code{scope-operator}
1086 clean-ups may not always be correct. This usually only occurs when
1087 scoped identifiers appear in switch label tags.}. Clean up occurs when
1088 the second colon is typed. You will always want @code{scope-operator}
1089 in the @code{c-cleanup-list} when you are editing C++ code.
1090
1091 @end itemize
1092
1093
1094 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1095 @node Hungry-deletion of Whitespace, , Auto-newline Insertion, Minor Modes
1096 @comment node-name, next, previous, up
1097 @section Hungry-deletion of Whitespace
1098 @cindex hungry-deletion of whitespace
1099 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1100
1101 Hungry deletion of whitespace, or as it more commonly called,
1102 @dfn{hungry-delete mode}, is a simple feature that some people find
1103 extremely useful. In fact, you might find yourself wanting
1104 hungry-delete in @strong{all} your editing modes!
1105
1106 @kindex DEL
1107 @kindex Backspace
1108 In a nutshell, when hungry-delete mode is enabled, hitting the
1109 @key{Backspace} key@footnote{I say ``hit the @key{Backspace} key'' but
1110 what I really mean is ``when Emacs receives the @code{BackSpace} key
1111 event''. The difference usually isn't significant to most users, but
1112 advanced users will realize that under window systems such as X, any
1113 physical key (keycap) on the keyboard can be configured to generate any
1114 keysym, and thus any Emacs key event. Also, the use of Emacs on TTYs
1115 will affect which keycap generates which key event. From a pedantic
1116 point of view, here we are only concerned with the key event that
1117 Emacs receives.} will consume all preceding whitespace, including
1118 newlines and tabs. This can really cut down on the number of
1119 @key{Backspace}'s you have to type if, for example you made a mistake on
1120 the preceding line.
1121
1122 @findex c-electric-backspace
1123 @findex electric-backspace (c-)
1124 @vindex c-backspace-function
1125 @vindex backspace-function (c-)
1126
1127 @findex c-electric-delete
1128 @findex electric-delete (c-)
1129 @vindex c-delete-function
1130 @vindex delete-function (c-)
1131 @cindex literal
1132
1133 @findex backward-delete-char-untabify
1134
1135 By default, when you hit the @key{Backspace} key @ccmode{} runs the
1136 command @code{c-electric-backspace}, which deletes text in the backwards
1137 direction. When deleting a single character, or when @key{Backspace} is
1138 hit in a literal (@pxref{Auto-newline Insertion}), or when hungry-delete
1139 mode is disabled, the function contained in the
1140 @code{c-backspace-function} variable is called with one argument (the
1141 number of characters to delete). This variable is set to
1142 @code{backward-delete-char-untabify} by default.
1143
1144 @vindex delete-key-deletes-forward
1145 @findex delete-char
1146
1147 The default behavior of the @key{Delete} key depends on the flavor of
1148 Emacs you are using. By default in XEmacs 20.3 and beyond, the
1149 @key{Delete} key is bound to @code{c-electric-delete}. You control the
1150 direction that the @key{Delete} key deletes by setting the variable
1151 @code{delete-key-deletes-forward}, a standard XEmacs variable. When
1152 this variable is non-@code{nil} and hungry-delete mode is enabled,
1153 @code{c-electric-delete} will consume all whitespace @emph{following}
1154 point. When @code{delete-key-deletes-forward} is @code{nil}, it deletes
1155 all whitespace @emph{preceding} point@footnote{i.e. it literally calls
1156 @code{c-electric-backspace}.} When deleting a single character, or if
1157 @key{Delete} is hit in a literal, or hungry-delete mode is disabled, the
1158 function contained in @code{c-delete-function} is called with one
1159 argument: the number of characters to delete. This variable is set to
1160 @code{delete-char} by default.
1161
1162 In Emacs 19 or Emacs 20, both the @key{Delete} and @key{Backspace} keys
1163 are bound to @code{c-electric-backspace}, however you can change this by
1164 explicitly binding @code{[delete]}@footnote{E.g. to
1165 @code{c-electric-delete} in your @file{.emacs} file. Note however, that
1166 Emacs 20 does not have a standard variable such as
1167 @code{delete-key-deletes-forward}.}.
1168
1169 XEmacsen older than 20.3 behave similar to Emacs 19 and Emacs 20.
1170
1171
1172 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1173 @node Text Filling and Line Breaking, Commands, Minor Modes, Top
1174 @comment node-name, next, previous, up
1175 @chapter Text Filling and Line Breaking
1176 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1177
1178 Since there's a lot of normal text in comments and string literals,
1179 @ccmode{} provides features to edit these like in text mode. The goal
1180 is to do it as seamlessly as possible, i.e. you can use auto fill mode,
1181 sentence and paragraph movement, paragraph filling, adaptive filling etc
1182 wherever there's a piece of normal text without having to think much
1183 about it. @ccmode{} should keep the indentation, fix the comment line
1184 decorations, and so on, for you. It does that by hooking in on the
1185 different line breaking functions and tuning relevant variables as
1186 necessary.
1187
1188 @vindex c-comment-prefix-regexp
1189 @vindex comment-prefix-regexp (c-)
1190 @cindex comment line prefix
1191 @vindex comment-start
1192 @vindex comment-end
1193 @vindex comment-start-skip
1194 @vindex paragraph-start
1195 @vindex paragraph-separate
1196 @vindex paragraph-ignore-fill-prefix
1197 @vindex adaptive-fill-mode
1198 @vindex adaptive-fill-regexp
1199 @vindex adaptive-fill-first-line-regexp
1200 To make Emacs recognize comments and treat text in them as normal
1201 paragraphs, @ccmode{} makes several standard
1202 variables@footnote{@code{comment-start}, @code{comment-end},
1203 @code{comment-start-skip}, @code{paragraph-start},
1204 @code{paragraph-separate}, @code{paragraph-ignore-fill-prefix},
1205 @code{adaptive-fill-mode}, @code{adaptive-fill-regexp}, and
1206 @code{adaptive-fill-first-line-regexp}.} buffer local and modifies them
1207 according to the language syntax and the style of line decoration that
1208 starts every line in a comment. The style variable
1209 @code{c-comment-prefix-regexp} contains the regexp used to recognize
1210 this @dfn{comment line prefix}. The default is @samp{//+\\|\\**}, which
1211 matches C++ style line comments like
1212 @example
1213
1214 // blah blah
1215
1216 @end example
1217
1218 @noindent
1219 with two or more slashes in front of them, and C style block comments
1220 like
1221 @example
1222 @group
1223
1224 /*
1225 * blah blah
1226 */
1227
1228 @end group
1229 @end example
1230
1231 @noindent
1232 with zero or more stars at the beginning of every line. If you change
1233 that variable, please make sure it still matches the comment starter
1234 (i.e. @code{//}) of line comments @emph{and} the line prefix inside
1235 block comments. Also note that since @ccmode{} uses the value of
1236 @code{c-comment-prefix-regexp} to set up several other variables at mode
1237 initialization, you need to reinitialize the program mode if you change
1238 it inside a @ccmode{} buffer.
1239
1240 @findex auto-fill-mode
1241 @cindex auto fill mode
1242 @cindex paragraph fill
1243 Line breaks are by default handled (almost) the same regardless whether
1244 they are made by auto fill mode (@pxref{Auto Fill,,, emacs, The Emacs
1245 Editor}), paragraph filling (e.g. with @kbd{M-q}), or explicitly with
1246 @kbd{M-j} or similar methods. In string literals, the new line gets the
1247 same indentation as the previous nonempty line (may be changed with the
1248 @code{string} syntactic symbol). In comments, @ccmode{} uses
1249 @code{c-comment-prefix-regexp} to adapt the line prefix from the other
1250 lines in the comment.
1251
1252 @vindex adaptive-fill-mode
1253 @cindex adaptive fill mode
1254 @ccmode{} uses adaptive fill mode (@pxref{Adaptive Fill,,, emacs, The
1255 Emacs Editor}) to make Emacs correctly keep the line prefix when filling
1256 paragraphs. That also makes Emacs preserve the text indentation
1257 @emph{inside} the comment line prefix. E.g. in the following comment,
1258 both paragraphs will be filled with the left margins kept intact:
1259 @example
1260 @group
1261
1262 /* Make a balanced b-tree of the nodes in the incoming
1263 * stream. But, to quote the famous words of Donald E.
1264 * Knuth,
1265 *
1266 * Beware of bugs in the above code; I have only
1267 * proved it correct, not tried it.
1268 */
1269
1270 @end group
1271 @end example
1272
1273 @findex c-setup-filladapt
1274 @findex setup-filladapt (c-)
1275 @findex filladapt-mode
1276 @vindex filladapt-mode
1277 @cindex Filladapt mode
1278 It's also possible to use other adaptive filling packages, notably Kyle
1279 E. Jones' Filladapt package@footnote{It's available from
1280 @uref{http://www.wonderworks.com/}. As of version 2.12, it does however
1281 lack a feature that makes it work suboptimally when
1282 @code{c-comment-prefix-regexp} matches the empty string (which it does
1283 by default). A patch for that is available from
1284 @uref{http://www.python.org/emacs/cc-mode/,, the CC Mode site}.},
1285 which handles things like bulleted lists nicely. There's a convenience
1286 function @code{c-setup-filladapt} that tunes the relevant variables in
1287 Filladapt for use in @ccmode{}. Call it from a mode hook, e.g. with
1288 something like this in your @file{.emacs}:
1289 @example
1290 @group
1291
1292 (defun my-c-mode-common-hook ()
1293 (c-setup-filladapt)
1294 (filladapt-mode 1))
1295 (add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
1296
1297 @end group
1298 @end example
1299
1300 @vindex c-block-comment-prefix
1301 @vindex block-comment-prefix (c-)
1302 @vindex c-comment-continuation-stars
1303 @vindex comment-continuation-stars (c-)
1304 Normally the comment line prefix inserted for a new line inside a
1305 comment is deduced from other lines in it. However there's one
1306 situation when there's no clue about how the prefix should look, namely
1307 when a block comment is broken for the first time. The string in the
1308 style variable @code{c-block-comment-prefix}@footnote{In versions before
1309 5.26, this variable was called @code{c-comment-continuation-stars}. As
1310 a compatibility measure, @ccmode{} still uses the value on that variable
1311 if it's set.} is used in that case. It defaults to @samp{* }, which
1312 makes a comment
1313 @example
1314
1315 /* Got O(n^2) here, which is a Bad Thing. */
1316
1317 @end example
1318
1319 @noindent
1320 break into
1321 @example
1322 @group
1323
1324 /* Got O(n^2) here,
1325 * which is a Bad Thing. */
1326
1327 @end group
1328 @end example
1329
1330 Note that it won't work to justify the indentation by putting leading
1331 spaces in the @code{c-block-comment-prefix} string, since @ccmode{}
1332 still uses the normal indentation engine to indent the line. Thus, the
1333 right way to fix the indentation is by setting the @code{c} syntactic
1334 symbol. It defaults to @code{c-lineup-C-comments}, which handles the
1335 indentation of most common comment styles, see @ref{Indentation
1336 Functions}.
1337
1338 @vindex c-ignore-auto-fill
1339 @vindex ignore-auto-fill (c-)
1340 When auto fill mode is enabled, @ccmode{} can selectively ignore it
1341 depending on the context the line break would occur in, e.g. to never
1342 break a line automatically inside a string literal. This behavior can
1343 be controlled with the @code{c-ignore-auto-fill} variable. It takes a
1344 list of symbols for the different contexts where auto-filling never
1345 should occur:
1346
1347 @itemize @bullet
1348 @item @code{string} --- Inside a string or character literal.
1349 @item @code{c} --- Inside a C style block comment.
1350 @item @code{c++} --- Inside a C++ style line comment.
1351 @item @code{cpp} --- Inside a preprocessor directive.
1352 @item @code{code} --- Anywhere else, i.e. in normal code.
1353 @end itemize
1354
1355 By default, @code{c-ignore-auto-fill} is set to @code{'(string cpp
1356 code)}, which means that auto-filling only occurs in comments when
1357 auto-fill mode is activated. In literals, it's often desirable to have
1358 explicit control over newlines. In preprocessor directives, the
1359 necessary @samp{\} escape character before the newline is not
1360 automatically inserted, so an automatic line break would produce invalid
1361 code. In normal code, line breaks are normally dictated by some logical
1362 structure in the code rather than the last whitespace character, so
1363 automatic line breaks there will produce poor results in the current
1364 implementation.
1365
1366 The commands that does the actual work follows.
1367
1368 @table @asis
1369
1370 @kindex M-q
1371 @findex c-fill-paragraph
1372 @findex fill-paragraph (c-)
1373 @cindex Javadoc markup
1374 @item @kbd{M-q} (@code{c-fill-paragraph})
1375 This is the replacement for @code{fill-paragraph} in @ccmode{}
1376 buffers. It's used to fill multiline string literals and both block and
1377 line style comments. In Java buffers, the Javadoc markup words are
1378 recognized as paragraph starters.
1379
1380 The function keeps the comment starters and enders of block comments as
1381 they were before the filling. This means that a comment ender on the
1382 same line as the paragraph being filled will be filled with the
1383 paragraph, and one on a line by itself will stay as it is. The comment
1384 starter is handled similarly@footnote{This means that the variables
1385 @code{c-hanging-comment-starter-p} and @code{c-hanging-comment-ender-p},
1386 which controlled this behavior in earlier versions of @ccmode{}, are now
1387 obsolete.}.
1388
1389 @kindex M-j
1390 @findex c-indent-new-comment-line
1391 @findex indent-new-comment-line (c-)
1392 @item @kbd{M-j} (@code{c-indent-new-comment-line})
1393 This is the replacement for @code{indent-new-comment-line}. It breaks
1394 the line at point and indents the new line like the current one.
1395
1396 @vindex comment-multi-line
1397 If inside a comment and @code{comment-multi-line} is non-@code{nil}, the
1398 indentation and line prefix are preserved. If inside a comment and
1399 @code{comment-multi-line} is @code{nil}, a new comment of the same type
1400 is started on the next line and indented as appropriate for comments.
1401
1402 @findex c-context-line-break
1403 @findex context-line-break (c-)
1404 @item @kbd{M-x c-context-line-break}
1405 This is a function that works like @code{indent-new-comment-line} in
1406 comments and @code{newline-and-indent} elsewhere, thus combining those
1407 two in a way that uses each one in the context it's best suited for.
1408 I.e. in comments the comment line prefix and indentation is kept for the
1409 new line, and in normal code it's indented according to context by the
1410 indentation engine.
1411
1412 It's not bound to a key by default, but it's intended to be used on the
1413 @kbd{RET} key. If you like the behavior of @code{newline-and-indent} on
1414 @kbd{RET}, you might consider switching to this function.
1415
1416 @end table
1417
1418
1419 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1420 @node Commands, Customizing Indentation, Text Filling and Line Breaking, Top
1421 @comment node-name, next, previous, up
1422 @chapter Commands
1423 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1424
1425 @menu
1426 * Indentation Commands::
1427 * Movement Commands::
1428 * Other Commands::
1429 @end menu
1430
1431 See also @ref{Text Filling and Line Breaking}, for commands concerning
1432 that bit.
1433
1434
1435 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1436 @node Indentation Commands, Movement Commands, , Commands
1437 @comment node-name, next, previous,up
1438 @section Indentation Commands
1439 @cindex indentation commands
1440 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1441
1442 The following list of commands re-indent C constructs. Note that when
1443 you change your coding style, either interactively or through some other
1444 means, your file does @emph{not} automatically get re-indented. You
1445 will need to execute one of the following commands to see the effects of
1446 your changes.
1447
1448 @cindex GNU indent program
1449 Also, variables like @code{c-hanging-*} and @code{c-cleanup-list}
1450 only affect how on-the-fly code is formatted. Changing the
1451 ``hanginess'' of a brace and then re-indenting, will not move the brace
1452 to a different line. For this, you're better off getting an external
1453 program like GNU @code{indent}, which will re-arrange brace location,
1454 among other things.
1455
1456 Re-indenting large sections of code can take a long time. When
1457 @ccmode{} reindents a region of code, it is essentially equivalent to
1458 hitting @kbd{TAB} on every line of the region. Especially vulnerable is
1459 code generator output@footnote{In particular, I have had people
1460 complain about the speed with which @code{lex(1)} output is re-indented.
1461 Lex, yacc, and other code generators usually output some pretty
1462 perversely formatted code. Re-indenting such code will be slow.}.
1463
1464 These commands are useful when indenting code:
1465
1466 @table @asis
1467
1468 @kindex TAB
1469 @findex c-indent-command
1470 @findex indent-command (c-)
1471 @item @kbd{TAB} (@code{c-indent-command})
1472 Indents the current line. The actual behavior is controlled by several
1473 variables, described below. See @code{c-tab-always-indent},
1474 @code{c-insert-tab-function}, and @code{indent-tabs-mode}. With a
1475 numeric argument, this command rigidly indents the region, preserving
1476 the relative indentation among the lines.
1477
1478 @kindex M-C-q
1479 @findex c-indent-exp
1480 @findex indent-exp (c-)
1481 @item @kbd{M-C-q} (@code{c-indent-exp})
1482 Indent an entire balanced brace or parenthesis expression. Note that
1483 point must be on the opening brace or parenthesis of the expression you
1484 want to indent.
1485
1486 @kindex C-c C-q
1487 @findex c-indent-defun
1488 @findex indent-defun (c-)
1489 @item @kbd{C-c C-q} (@code{c-indent-defun})
1490 Indents the entire top-level function or class definition encompassing
1491 point. It leaves point unchanged. This function can't be used to
1492 re-indent a nested brace construct, such as a nested class or function,
1493 or a Java method. The top-level construct being re-indented must be
1494 complete, i.e. it must have both a beginning brace and an ending brace.
1495
1496 @kindex M-C-\
1497 @findex indent-region
1498 @item @kbd{M-C-\} (@code{indent-region})
1499 Indents an arbitrary region of code. This is a standard Emacs command,
1500 tailored for C code in a @ccmode{} buffer. Note that of course, point
1501 and mark must delineate the region you want to indent.
1502
1503 @kindex M-C-h
1504 @findex c-mark-function
1505 @findex mark-function (c-)
1506 @item @kbd{M-C-h} (@code{c-mark-function})
1507 While not strictly an indentation command, this is useful for marking
1508 the current top-level function or class definition as the current
1509 region. As with @code{c-indent-defun}, this command operates on
1510 top-level constructs, and can't be used to mark say, a Java method.
1511
1512 @end table
1513
1514 These variables are also useful when indenting code:
1515
1516 @table @code
1517
1518 @vindex c-tab-always-indent
1519 @vindex tab-always-indent (c-)
1520 @kindex TAB
1521 @cindex literal
1522 @item c-tab-always-indent
1523 This variable controls how @kbd{TAB} @code{c-indent-command} operates.
1524 When this variable is @code{t}, @kbd{TAB} always just indents the
1525 current line. When it is @code{nil}, the line is indented only if point
1526 is at the left margin, or on or before the first non-whitespace
1527 character on the line, otherwise some whitespace is inserted. If this
1528 variable is the symbol @code{other}, then some whitespace is inserted
1529 only within strings and comments (literals), an inside preprocessor
1530 directives, but the line is always reindented.
1531
1532 @vindex c-insert-tab-function
1533 @vindex insert-tab-function (c-)
1534 @findex tab-to-tab-stop
1535 @item c-insert-tab-function
1536 When ``some whitespace'' is inserted as described above, what actually
1537 happens is that the function stored in @code{c-insert-tab-function} is
1538 called. Normally, this just inserts a real tab character, or the
1539 equivalent number of spaces, depending on @code{indent-tabs-mode}.
1540 Some people, however, set @code{c-insert-tab-function} to
1541 @code{tab-to-tab-stop} so as to get hard tab stops when indenting.
1542
1543 @vindex indent-tabs-mode
1544 @item indent-tabs-mode
1545 This is a standard Emacs variable that controls how line indentation is
1546 composed. When this variable is non-@code{nil}, then tabs can be used
1547 in a line's indentation, otherwise only spaces can be used.
1548
1549 @vindex c-progress-interval
1550 @vindex progress-interval (c-)
1551 @item c-progress-interval
1552 When indenting large regions of code, this variable controls how often a
1553 progress message is displayed. Set this variable to @code{nil} to
1554 inhibit the progress messages, or set it to an integer which is the
1555 interval in seconds that progress messages are displayed.
1556
1557 @end table
1558
1559
1560 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1561 @node Movement Commands, Other Commands, Indentation Commands, Commands
1562 @comment node-name, next, previous, up
1563 @section Movement Commands
1564 @cindex movement commands
1565 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1566
1567 @ccmode{} contains some useful command for moving around in C
1568 code.
1569
1570 @table @asis
1571
1572 @findex c-beginning-of-defun
1573 @findex beginning-of-defun (c-)
1574 @findex beginning-of-defun
1575 @item @kbd{M-x c-beginning-of-defun}
1576 Moves point back to the least-enclosing brace. This function is
1577 analogous to the Emacs built-in command @code{beginning-of-defun},
1578 except it eliminates the constraint that the top-level opening brace
1579 must be in column zero. See @code{beginning-of-defun} for more
1580 information.
1581
1582 Depending on the coding style being used, you might prefer
1583 @code{c-beginning-of-defun} to @code{beginning-of-defun}. If so,
1584 consider binding @kbd{C-M-a} to the former instead. For backwards
1585 compatibility reasons, the default binding remains in effect.
1586
1587 @findex c-end-of-defun
1588 @findex end-of-defun (c-)
1589 @findex end-of-defun
1590 @item @kbd{M-x c-end-of-defun}
1591 Moves point to the end of the current top-level definition. This
1592 function is analogous to the Emacs built-in command @code{end-of-defun},
1593 except it eliminates the constraint that the top-level opening brace of
1594 the defun must be in column zero. See @code{beginning-of-defun} for more
1595 information.
1596
1597 Depending on the coding style being used, you might prefer
1598 @code{c-end-of-defun} to @code{end-of-defun}. If so,
1599 consider binding @kbd{C-M-e} to the former instead. For backwards
1600 compatibility reasons, the default binding remains in effect.
1601
1602 @kindex C-c C-u
1603 @findex c-up-conditional
1604 @findex up-conditional (c-)
1605 @item @kbd{C-c C-u} (@code{c-up-conditional})
1606 Move point back to the containing preprocessor conditional, leaving the
1607 mark behind. A prefix argument acts as a repeat count. With a negative
1608 argument, move point forward to the end of the containing
1609 preprocessor conditional.
1610
1611 @samp{#elif} is treated like @samp{#else} followed by @samp{#if}, so the
1612 function stops at them when going backward, but not when going forward.
1613
1614 @findex c-up-conditional-with-else
1615 @findex up-conditional-with-else (c-)
1616 @item @kbd{M-x c-up-conditional-with-else}
1617 A variety of @code{c-up-conditional} that also stops at @samp{#else}
1618 lines. Normally those lines are ignored.
1619
1620 @findex c-down-conditional
1621 @findex down-conditional (c-)
1622 @item @kbd{M-x c-down-conditional}
1623 Move point forward into the next nested preprocessor conditional,
1624 leaving the mark behind. A prefix argument acts as a repeat count.
1625 With a negative argument, move point backward into the previous
1626 nested preprocessor conditional.
1627
1628 @samp{#elif} is treated like @samp{#else} followed by @samp{#if}, so the
1629 function stops at them when going forward, but not when going backward.
1630
1631 @findex c-down-conditional-with-else
1632 @findex down-conditional-with-else (c-)
1633 @item @kbd{M-x c-down-conditional-with-else}
1634 A variety of @code{c-down-conditional} that also stops at @samp{#else}
1635 lines. Normally those lines are ignored.
1636
1637 @kindex C-c C-p
1638 @findex c-backward-conditional
1639 @findex backward-conditional (c-)
1640 @item @kbd{C-c C-p} (@code{c-backward-conditional})
1641 Move point back over a preprocessor conditional, leaving the mark
1642 behind. A prefix argument acts as a repeat count. With a negative
1643 argument, move forward.
1644
1645 @kindex C-c C-n
1646 @findex c-forward-conditional
1647 @findex forward-conditional (c-)
1648 @item @kbd{C-c C-n} (@code{c-forward-conditional})
1649 Move point forward across a preprocessor conditional, leaving the mark
1650 behind. A prefix argument acts as a repeat count. With a negative
1651 argument, move backward.
1652
1653 @kindex M-a
1654 @findex c-beginning-of-statement
1655 @findex beginning-of-statement (c-)
1656 @item @kbd{M-a} (@code{c-beginning-of-statement})
1657 Move point to the beginning of the innermost C statement. If point is
1658 already at the beginning of a statement, it moves to the beginning of
1659 the closest preceding statement, even if that means moving into a block
1660 (you can use @kbd{M-C-b} to move over a balanced block). With prefix
1661 argument @var{n}, move back @var{n} @minus{} 1 statements.
1662
1663 If point is within a comment, or next to a comment, this command moves
1664 by sentences instead of statements.
1665
1666 When called from a program, this function takes three optional
1667 arguments: the numeric prefix argument, a buffer position limit which is
1668 the farthest back to search, and a flag to enable moving by sentence
1669 inside comments.
1670
1671 @kindex M-e
1672 @findex c-end-of-statement
1673 @findex end-of-statement (c-)
1674 @item @kbd{M-e} (@code{c-end-of-statement})
1675 Move point to the end of the innermost C statement. If point is at the
1676 end of a statement, move to the end of the next statement, even if it's
1677 inside a nested block (use @kbd{M-C-f} to move to the other side of the
1678 block). With prefix argument @var{n}, move forward @var{n} @minus{} 1
1679 statements.
1680
1681 If point is within a comment, or next to a comment, this command moves
1682 by sentences instead of statements.
1683
1684 When called from a program, this function takes three optional
1685 arguments: the numeric prefix argument, a buffer position limit which is
1686 the farthest back to search, and a flag to enable moving by sentence
1687 inside comments.
1688
1689 @findex c-forward-into-nomenclature
1690 @findex forward-into-nomenclature (c-)
1691 @item @kbd{M-x c-forward-into-nomenclature}
1692 A popular programming style, especially for object-oriented languages
1693 such as C++ is to write symbols in a mixed case format, where the first
1694 letter of each word is capitalized, and not separated by underscores.
1695 E.g. @samp{SymbolsWithMixedCaseAndNoUnderlines}.
1696
1697 This command moves point forward to next capitalized word. With prefix
1698 argument @var{n}, move @var{n} times.
1699
1700 @findex c-backward-into-nomenclature
1701 @findex backward-into-nomenclature (c-)
1702 @item @kbd{M-x c-backward-into-nomenclature}
1703 Move point backward to beginning of the next capitalized
1704 word. With prefix argument @var{n}, move @var{n} times. If
1705 @var{n} is negative, move forward.
1706
1707 @end table
1708
1709
1710 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1711 @node Other Commands, , Movement Commands, Commands
1712 @comment node-name, next, previous, up
1713 @section Other Commands
1714 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1715
1716 @ccmode{} contains a few other useful commands:
1717
1718 @table @asis
1719
1720 @kindex C-c :
1721 @findex c-scope-operator
1722 @findex scope-operator (c-)
1723 @item @kbd{C-c :} (@code{c-scope-operator})
1724 In C++, it is also sometimes desirable to insert the double-colon scope
1725 operator without performing the electric behavior of colon insertion.
1726 @kbd{C-c :} does just this.
1727
1728 @kindex C-c C-\
1729 @findex c-backslash-region
1730 @findex backslash-region (c-)
1731 @item @kbd{C-c C-\} (@code{c-backslash-region})
1732 This function is handy when editing macros split over several lines by
1733 ending each line with a backslash. It inserts and aligns, or deletes
1734 these end-of-line backslashes in the current region.
1735
1736 @vindex c-backslash-column
1737 @vindex backslash-column (c-)
1738 With no prefix argument, it inserts any missing backslashes and aligns
1739 them to the column specified by the @code{c-backslash-column} style
1740 variable. With a prefix argument, it deletes any backslashes.
1741
1742 The function does not modify blank lines at the start of the region. If
1743 the region ends at the start of a line, it always deletes the backslash
1744 (if any) at the end of the previous line.
1745
1746 @end table
1747
1748
1749 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1750 @node Customizing Indentation, Syntactic Symbols, Commands, Top
1751 @comment node-name, next, previous, up
1752 @chapter Customizing Indentation
1753 @cindex customizing indentation
1754 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1755
1756 @vindex c-offsets-alist
1757 @vindex offsets-alist (c-)
1758 The style variable @code{c-offsets-alist} contains the mappings between
1759 syntactic symbols and the offsets to apply for those symbols. It's set
1760 at mode initialization from a @emph{style} you may specify. Styles are
1761 groupings of syntactic symbol offsets and other style variable values.
1762 Most likely, you'll find that one of the pre-defined styles will suit
1763 your needs. @xref{Styles}, for an explanation of how to set up named
1764 styles.
1765
1766 Only syntactic symbols not already bound on @code{c-offsets-alist} will
1767 be set from styles. This means that any association you set on it, be
1768 it before or after mode initialization, will not be changed. The
1769 @code{c-offsets-alist} variable may therefore be used from e.g. the
1770 Customization interface@footnote{Available in Emacs 20 and later, and
1771 XEmacs 19.15 and later.} to easily change indentation offsets without
1772 having to bother about styles. Initially @code{c-offsets-alist} is
1773 empty, so that all syntactic symbols are set by the style system.
1774
1775 @kindex C-c C-o
1776 @findex c-set-offset
1777 @findex set-offset (c-)
1778 You can use the command @kbd{C-c C-o} (@code{c-set-offset}) as the way
1779 to set offsets, both interactively and from your mode
1780 hook@footnote{Obviously, you use the keybinding interactively, and the
1781 function call programmatically!}.
1782
1783 @vindex c-basic-offset
1784 @vindex basic-offset (c-)
1785 The offset associated with any particular syntactic symbol can be any of
1786 an integer, a function or lambda expression, a variable name, or one of
1787 the following symbols: @code{+}, @code{-}, @code{++}, @code{--},
1788 @code{*}, or @code{/}. These latter describe offset in multiples of the
1789 value of the style variable @code{c-basic-offset}. By defining a
1790 style's indentation in terms of this fundamental variable, you can
1791 change the amount of whitespace given to an indentation level while
1792 maintaining the same basic shape of your code. Here are the values that
1793 the special symbols correspond to:
1794
1795 @table @code
1796
1797 @item +
1798 @code{c-basic-offset} times 1
1799 @item -
1800 @code{c-basic-offset} times -1
1801 @item ++
1802 @code{c-basic-offset} times 2
1803 @item --
1804 @code{c-basic-offset} times -2
1805 @item *
1806 @code{c-basic-offset} times 0.5
1807 @item /
1808 @code{c-basic-offset} times -0.5
1809
1810 @end table
1811
1812 @cindex indentation functions
1813
1814 When a function is used as offset, it's called an @dfn{indentation
1815 function}. Such functions are useful when more context than just the
1816 syntactic symbol is needed to get the desired indentation.
1817 @xref{Indentation Functions}, and @ref{Custom Indentation Functions},
1818 for details about them.
1819
1820 @vindex c-strict-syntax-p
1821 @vindex strict-syntax-p (c-)
1822 The offset can also be a list, in which case it is evaluated recursively
1823 using the semantics described above. The first element of the list that
1824 returns a non-@code{nil} value succeeds and the evaluation stops. If
1825 none of the list elements return a non-@code{nil} value, then an offset
1826 of 0 (zero) is used@footnote{There is however a variable
1827 @code{c-strict-syntax-p} that, when set to non-@code{nil}, will cause an
1828 error to be signalled in that case. It's now considered obsolete since
1829 it doesn't work well with some of the alignment functions that now
1830 returns @code{nil} instead of zero to be more usable in lists. You
1831 should therefore leave @code{c-strict-syntax-p} set to @code{nil}.}.
1832
1833 So, for example, because most of the default offsets are defined in
1834 terms of @code{+}, @code{-}, and @code{0}, if you like the general
1835 indentation style, but you use 4 spaces instead of 2 spaces per level,
1836 you can probably achieve your style just by changing
1837 @code{c-basic-offset} like so@footnote{You can try this interactively in
1838 a C buffer by typing the text that appears in italics.}:
1839 @example
1840
1841 @emph{M-x set-variable RET}
1842 Set variable: @emph{c-basic-offset RET}
1843 Set c-basic-offset to value: @emph{4 RET}
1844
1845 @end example
1846
1847 @noindent
1848 This would change
1849 @example
1850 @group
1851
1852 int add( int val, int incr, int doit )
1853 @{
1854 if( doit )
1855 @{
1856 return( val + incr );
1857 @}
1858 return( val );
1859 @}
1860
1861 @end group
1862 @end example
1863 @noindent
1864 to
1865 @example
1866 @group
1867
1868 int add( int val, int incr, int doit )
1869 @{
1870 if( doit )
1871 @{
1872 return( val + incr );
1873 @}
1874 return( val );
1875 @}
1876
1877 @end group
1878 @end example
1879
1880 To change indentation styles more radically, you will want to change the
1881 offsets associated with other syntactic symbols. First, I'll show you
1882 how to do that interactively, then I'll describe how to make changes to
1883 your @file{.emacs} file so that your changes are more permanent.
1884
1885 @menu
1886 * Interactive Customization::
1887 * Permanent Customization::
1888 * Hooks::
1889 * Styles::
1890 * Advanced Customizations::
1891 @end menu
1892
1893
1894 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1895 @node Interactive Customization, Permanent Customization, , Customizing Indentation
1896 @comment node-name, next, previous, up
1897 @section Interactive Customization
1898 @cindex interactive customization
1899 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1900
1901 As an example of how to customize indentation, let's change the
1902 style of this example@footnote{In this an subsequent examples, the
1903 original code is formatted using the @samp{gnu} style unless otherwise
1904 indicated. @xref{Styles}.}:
1905 @example
1906 @group
1907
1908 1: int add( int val, int incr, int doit )
1909 2: @{
1910 3: if( doit )
1911 4: @{
1912 5: return( val + incr );
1913 6: @}
1914 7: return( val );
1915 8: @}
1916
1917 @end group
1918 @end example
1919 @noindent
1920 to:
1921 @example
1922 @group
1923
1924 1: int add( int val, int incr, int doit )
1925 2: @{
1926 3: if( doit )
1927 4: @{
1928 5: return( val + incr );
1929 6: @}
1930 7: return( val );
1931 8: @}
1932
1933 @end group
1934 @end example
1935
1936 In other words, we want to change the indentation of braces that open a
1937 block following a condition so that the braces line up under the
1938 conditional, instead of being indented. Notice that the construct we
1939 want to change starts on line 4. To change the indentation of a line,
1940 we need to see which syntactic components affect the offset calculations
1941 for that line. Hitting @kbd{C-c C-s} on line 4 yields:
1942 @example
1943
1944 ((substatement-open . 44))
1945
1946 @end example
1947
1948 @noindent
1949 so we know that to change the offset of the open brace, we need to
1950 change the indentation for the @code{substatement-open} syntactic
1951 symbol. To do this interactively, just hit @kbd{C-c C-o}. This prompts
1952 you for the syntactic symbol to change, providing a reasonable default.
1953 In this case, the default is @code{substatement-open}, which is just the
1954 syntactic symbol we want to change!
1955
1956 After you hit return, @ccmode{} will then prompt you for the new
1957 offset value, with the old value as the default. The default in this
1958 case is @samp{+}, but we want no extra indentation so enter
1959 @samp{0} and @kbd{RET}. This will associate the offset 0 with the
1960 syntactic symbol @code{substatement-open}.
1961
1962 To check your changes quickly, just hit @kbd{C-c C-q}
1963 (@code{c-indent-defun}) to reindent the entire function. The example
1964 should now look like:
1965 @example
1966 @group
1967
1968 1: int add( int val, int incr, int doit )
1969 2: @{
1970 3: if( doit )
1971 4: @{
1972 5: return( val + incr );
1973 6: @}
1974 7: return( val );
1975 8: @}
1976
1977 @end group
1978 @end example
1979
1980 Notice how just changing the open brace offset on line 4 is all we
1981 needed to do. Since the other affected lines are indented relative to
1982 line 4, they are automatically indented the way you'd expect. For more
1983 complicated examples, this may not always work. The general approach to
1984 take is to always start adjusting offsets for lines higher up in the
1985 file, then re-indent and see if any following lines need further
1986 adjustments.
1987
1988
1989 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1990 @node Permanent Customization, Hooks, Interactive Customization, Customizing Indentation
1991 @comment node-name, next, previous, up
1992 @section Permanent Customization
1993 @cindex permanent customization
1994 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1995
1996 To make your changes permanent, you need to add some lisp code to your
1997 @file{.emacs} file. @ccmode{} supports many different ways to be
1998 configured, from the straightforward way by setting variables globally
1999 in @file{.emacs} or in the Customization interface, to the complex and
2000 precisely controlled way by using styles and hook functions.
2001
2002 The simplest way of customizing @ccmode{} permanently is to set the
2003 variables in your @file{.emacs} with @code{setq} and similar commands.
2004 So to make the setting of @code{substatement-open} permanent, add this
2005 to the @file{.emacs} file:
2006 @example
2007 @group
2008
2009 (require 'cc-mode)
2010 (c-set-offset 'substatement-open 0)
2011
2012 @end group
2013 @end example
2014
2015 The @code{require} line is only needed once in the beginning to make
2016 sure @ccmode{} is loaded so that the @code{c-set-offset} function is
2017 defined.
2018
2019 You can also use the more user friendly Customization interface, but
2020 this manual does not cover how that works.
2021
2022 Variables set like this at the top level in @file{.emacs} take effect in
2023 all @ccmode{} buffers, regardless of language. The indentation style
2024 related variables that you don't set get their value from the style
2025 system (@pxref{Styles}), and they therefore depend on the setting of
2026 @code{c-default-style}. Note that if you use Customize, this means that
2027 the greyed-out default values presented there might not be the ones you
2028 actually get, since the actual values depend on the style, which may
2029 very well be different for different languages.
2030
2031 If you want to make more advanced configurations, e.g. language-specific
2032 customization, global variable settings isn't enough. For that you can
2033 use the language hooks, see @ref{Hooks}, and/or the style system, see
2034 @ref{Styles}.
2035
2036 @vindex c-style-variables-are-local-p
2037 @vindex style-variables-are-local-p (c-)
2038 By default, all style variables are global, so that every buffer will
2039 share the same style settings. This is fine if you primarily edit one
2040 style of code, but if you edit several languages and want to use
2041 different styles for them, you need finer control by making the style
2042 variables buffer local. The recommended way to do this is to set the
2043 variable @code{c-style-variables-are-local-p} to @code{t}. The
2044 variables will be made buffer local when @ccmode{} is activated in a
2045 buffer for the first time in the Emacs session. Note that once the
2046 style variables are made buffer local, they cannot be made global again,
2047 without restarting Emacs.
2048
2049
2050 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2051 @node Hooks, Styles, Permanent Customization, Customizing Indentation
2052 @comment node-name, next, previous, up
2053 @section Hooks
2054 @cindex hooks
2055 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2056
2057 @vindex c-mode-common-hook
2058 @vindex mode-common-hook (c-)
2059 @vindex c-mode-hook
2060 @vindex c++-mode-hook
2061 @vindex objc-mode-hook
2062 @vindex java-mode-hook
2063 @vindex idl-mode-hook
2064 @vindex pike-mode-hook
2065 @vindex c-initialization-hook
2066 @vindex initialization-hook (c-)
2067 @ccmode{} provides several hooks that you can use to customize the mode
2068 according to your coding style. Each language mode has its own hook,
2069 adhering to standard Emacs major mode conventions. There is also one
2070 general hook and one package initialization hook:
2071
2072 @itemize @bullet
2073
2074 @item
2075 @code{c-mode-hook} --- For C buffers only.
2076 @item
2077 @code{c++-mode-hook} --- For C++ buffers only.
2078 @item
2079 @code{objc-mode-hook} --- For Objective-C buffers only.
2080 @item
2081 @code{java-mode-hook} --- For Java buffers only.
2082 @item
2083 @code{idl-mode-hook} --- For CORBA IDL buffers only.
2084 @item
2085 @code{pike-mode-hook} --- For Pike buffers only.
2086 @item
2087 @code{c-mode-common-hook} --- Common across all languages.
2088 @item
2089 @code{c-initialization-hook} --- Hook run only once per Emacs session,
2090 when @ccmode{} is initialized.
2091
2092 @end itemize
2093
2094 The language hooks get run as the last thing when you enter that
2095 language mode. The @code{c-mode-common-hook} is run by all supported
2096 modes @emph{before} the language specific hook, and thus can contain
2097 customizations that are common across all languages. Most of the
2098 examples in this section will assume you are using the common
2099 hook.@footnote{@code{java-mode} and the hook variables interact in a
2100 slightly different way than the other modes. @code{java-mode} normally
2101 sets the style of the buffer to @samp{java} @emph{before} running the
2102 @code{c-mode-common-hook} or @code{java-mode-hook}. You need to be
2103 aware of this so that style settings in @code{c-mode-common-hook} don't
2104 clobber your Java style. This is arguably bogus, but it's kept for
2105 historical reasons. @xref{Built-in Styles}, the documentation of
2106 @code{c-default-style}, for details.}
2107
2108 Here's a simplified example of what you can add to your @file{.emacs}
2109 file to do things whenever any @ccmode{} language is edited. See the
2110 Emacs manuals for more information on customizing Emacs via hooks.
2111 @xref{Sample .emacs File}, for a more complete sample @file{.emacs}
2112 file.
2113 @example
2114 @group
2115
2116 (defun my-c-mode-common-hook ()
2117 ;; my customizations for all of c-mode and related modes
2118 (no-case-fold-search)
2119 )
2120 (add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
2121
2122 @end group
2123 @end example
2124
2125
2126 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2127 @node Styles, Advanced Customizations, Hooks, Customizing Indentation
2128 @comment node-name, next, previous, up
2129 @section Styles
2130 @cindex styles
2131 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2132
2133 Most people only need to edit code formatted in just a few well-defined
2134 and consistent styles. For example, their organization might impose a
2135 ``blessed'' style that all its programmers must conform to. Similarly,
2136 people who work on GNU software will have to use the GNU coding style.
2137 Some shops are more lenient, allowing a variety of coding styles, and as
2138 programmers come and go, there could be a number of styles in use. For
2139 this reason, @ccmode{} makes it convenient for you to set up logical
2140 groupings of customizations called @dfn{styles}, associate a single name
2141 for any particular style, and pretty easily start editing new or
2142 existing code using these styles.
2143
2144 @cindex style variables
2145 The variables that the style system affect are called @dfn{style
2146 variables}. They are handled specially in several ways:
2147
2148 @itemize @bullet
2149 @item
2150 Style variables are by default global variables, i.e. they have the same
2151 value in all Emacs buffers. However, they can instead be made always
2152 buffer local by setting @code{c-style-variables-are-local-p} to
2153 non-@code{nil} before @ccmode{} is initialized.
2154
2155 @vindex c-old-style-variable-behavior
2156 @vindex old-style-variable-behavior (c-)
2157 @item
2158 The default value of any style variable (with two exceptions --- see
2159 below) is the special symbol @code{set-from-style}. Variables that are
2160 still set to that symbol when a @ccmode{} buffer is initialized will be
2161 set according to the current style, otherwise they will keep their
2162 current value@footnote{This is a big change from versions of @ccmode{}
2163 earlier than 5.26, where such settings would get overridden by the style
2164 system unless special precautions were taken. That was changed since it
2165 was counterintuitive and confusing, especially to novice users. If your
2166 configuration depends on the old overriding behavior, you can set the
2167 variable @code{c-old-style-variable-behavior} to non-@code{nil}.}.
2168
2169 Note that when we talk about the ``default value'' for a style variable,
2170 we don't mean the @code{set-from-style} symbol that all style variables
2171 are set to initially, but instead the value it will get at mode
2172 initialization when neither a style nor a global setting has set its
2173 value.
2174
2175 The style variable @code{c-offsets-alist} is handled a little
2176 differently from the other style variables. It's an association list,
2177 and is thus by default set to the empty list, @code{nil}. When the
2178 style system is initialized, any syntactic symbols already on it are
2179 kept --- only the missing ones are filled in from the chosen style.
2180
2181 The style variable @code{c-special-indent-hook} is also handled in a
2182 special way. Styles may only add more functions on this hook, so the
2183 global settings on it are always preserved@footnote{This did not change
2184 in version 5.26.}.
2185
2186 @item
2187 The global settings of style variables get captured in the special
2188 @code{user} style, which is used as the base for all the other styles.
2189 @xref{Built-in Styles}, for details.
2190
2191 @end itemize
2192
2193 The style variables are:
2194 @code{c-basic-offset},
2195 @code{c-comment-only-line-offset},
2196 @code{c-block-comment-prefix},
2197 @code{c-comment-prefix-regexp},
2198 @code{c-cleanup-list},
2199 @code{c-hanging-braces-alist},
2200 @code{c-hanging-colons-alist},
2201 @code{c-hanging-semi&comma-criteria},
2202 @code{c-backslash-column},
2203 @code{c-special-indent-hook},
2204 @code{c-label-minimum-indentation}, and
2205 @code{c-offsets-alist}.
2206
2207 @menu
2208 * Built-in Styles::
2209 * Adding Styles::
2210 * File Styles::
2211 @end menu
2212
2213
2214 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2215 @node Built-in Styles, Adding Styles, , Styles
2216 @comment node-name, next, previous, up
2217 @subsection Built-in Styles
2218 @cindex built-in styles
2219 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2220
2221 If you're lucky, one of @ccmode{}'s built-in styles might be just
2222 what you're looking for. These include:
2223
2224 @itemize @bullet
2225 @cindex GNU style
2226 @item
2227 @code{gnu} --- Coding style blessed by the Free Software Foundation
2228 for C code in GNU programs.
2229
2230 @cindex K&R style
2231 @item
2232 @code{k&r} --- The classic Kernighan and Ritchie style for C code.
2233
2234 @cindex BSD style
2235 @item
2236 @code{bsd} --- Also known as ``Allman style'' after Eric Allman.
2237
2238 @cindex Whitesmith style
2239 @item
2240 @code{whitesmith} --- Popularized by the examples that came with
2241 Whitesmiths C, an early commercial C compiler.
2242
2243 @cindex Stroustrup style
2244 @item
2245 @code{stroustrup} --- The classic Stroustrup style for C++ code.
2246
2247 @cindex Ellemtel style
2248 @item
2249 @code{ellemtel} --- Popular C++ coding standards as defined by
2250 ``Programming in C++, Rules and Recommendations'', Erik Nyquist and Mats
2251 Henricson, Ellemtel@footnote{This document is available at
2252 @uref{http://www.doc.ic.ac.uk/lab/cplus/c++.rules/} among other
2253 places.}.
2254
2255 @cindex Linux style
2256 @item
2257 @code{linux} --- C coding standard for Linux development.
2258
2259 @cindex Python style
2260 @item
2261 @code{python} --- C coding standard for Python extension
2262 modules@footnote{Python is a high level scripting language with a C/C++
2263 foreign function interface. For more information, see
2264 @uref{http://www.python.org/}.}.
2265
2266 @cindex Java style
2267 @findex java-mode
2268 @item
2269 @code{java} --- The style for editing Java code. Note that this style is
2270 automatically installed when you enter @code{java-mode}.
2271
2272 @cindex User style
2273 @item
2274 @code{user} --- This is a special style for several reasons. First, the
2275 @ccmode{} customizations you do by using either the Customization
2276 interface, or by writing @code{setq}'s at the top level of your
2277 @file{.emacs} file, will be captured in the @code{user} style. Also,
2278 all other styles implicitly inherit their settings from @code{user}
2279 style. This means that for any styles you add via @code{c-add-style}
2280 (@pxref{Adding Styles}) you need only define the differences between
2281 your new style and @code{user} style.
2282
2283 @end itemize
2284
2285 @vindex c-default-style
2286 @vindex default-style (c-)
2287 The default style in all newly created buffers is @code{gnu}, but you
2288 can change this by setting variable @code{c-default-style}. Although
2289 the @code{user} style is not the default style, any style variable
2290 settings you do with the Customization interface or on the top level in
2291 your @file{.emacs} file will by default override the style system, so
2292 you don't need to set @code{c-default-style} to @code{user} to see the
2293 effect of these settings.
2294
2295 @code{c-default-style} takes either a style name string, or an
2296 association list of major mode symbols to style names. Thus you can
2297 control exactly which default style is used for which @ccmode{} language
2298 mode. Here are the rules:
2299
2300 @vindex c-style-alist
2301 @vindex style-alist (c-)
2302 @vindex c-mode-common-hook
2303 @vindex mode-common-hook (c-)
2304 @enumerate
2305 @item
2306 When @code{c-default-style} is a string, it must be an existing style
2307 name as found in @code{c-style-alist}. This style is then used for all
2308 modes @emph{except} @code{java-mode}, where the style @samp{java} is
2309 used by default@footnote{This is for backwards compatibility reasons.
2310 The hard-coding of @code{java-mode} style is admittedly bogus!}.
2311
2312 @item
2313 When @code{c-default-style} is an association list, the current major
2314 mode is looked up to find a style name string. In this case, this style
2315 is always used exactly as specified and an error will occur if the named
2316 style does not exist.
2317
2318 @item
2319 If @code{c-default-style} is an association list, but the current major
2320 mode isn't found, then the special symbol @samp{other} is looked up. If
2321 this value is found, the associated style is used.
2322
2323 @item
2324 If @samp{other} is not found, then the @samp{gnu} style is used.
2325
2326 @item
2327 In all cases, the style described in @code{c-default-style} is installed
2328 @emph{before} the language hooks are run, so you can always override
2329 this setting by including an explicit call to @code{c-set-style} in your
2330 language mode hook, or in @code{c-mode-common-hook}.
2331
2332 @end enumerate
2333
2334 @findex c-set-style
2335 @findex set-style (c-)
2336 @kindex C-c .
2337 If you'd like to experiment with these built-in styles you can simply
2338 type the following in a @ccmode{} buffer:
2339 @example
2340 @group
2341
2342 @kbd{C-c . @var{STYLE-NAME} RET}
2343
2344 @end group
2345 @end example
2346 @noindent
2347 @kbd{C-c .} runs the command @code{c-set-style}. Note that all style
2348 names are case insensitive, even the ones you define.
2349
2350 Setting a style in this way does @emph{not} automatically re-indent your
2351 file. For commands that you can use to view the effect of your changes,
2352 see @ref{Commands}.
2353
2354 @vindex c-indentation-style
2355 @vindex indentation-style (c-)
2356 Note that for BOCM compatibility, @samp{gnu} is the default style, and
2357 any non-style based customizations you make (i.e. in
2358 @code{c-mode-common-hook} in your @file{.emacs} file) will be based on
2359 @samp{gnu} style unless you set @code{c-default-style} or do a
2360 @code{c-set-style} as the first thing in your hook. The variable
2361 @code{c-indentation-style} always contains the buffer's current style
2362 name, as a string.
2363
2364
2365 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2366 @node Adding Styles, File Styles, Built-in Styles, Styles
2367 @comment node-name, next, previous, up
2368 @subsection Adding Styles
2369 @cindex adding styles
2370 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2371
2372 @vindex c-style-alist
2373 @vindex style-alist (c-)
2374 @findex c-add-style
2375 @findex add-style (c-)
2376 If none of the built-in styles is appropriate, you'll probably want to
2377 add a new @dfn{style definition}. Styles are kept in the
2378 @code{c-style-alist} variable, but you should never modify this variable
2379 directly. Instead, @ccmode{} provides the function
2380 @code{c-add-style} that you can use to easily add new styles or change
2381 existing styles. This function takes two arguments, a @var{stylename}
2382 string, and an association list @var{description} of style
2383 customizations. If @var{stylename} is not already in
2384 @code{c-style-alist}, the new style is added, otherwise the style is
2385 changed to the new @var{description}.
2386 This function also takes an optional third argument, which if
2387 non-@code{nil}, automatically applies the new style to the current
2388 buffer.
2389
2390 @comment TBD: The next paragraph is bogus. I really need to better
2391 @comment document adding styles, including setting up inherited styles.
2392
2393 The sample @file{.emacs} file provides a concrete example of how a new
2394 style can be added and automatically set. @xref{Sample .emacs File}.
2395
2396
2397 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2398 @node File Styles, , Adding Styles, Styles
2399 @comment node-name, next, previous, up
2400 @subsection File Styles
2401 @cindex file styles
2402 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2403
2404 @cindex local variables
2405
2406 The Emacs manual describes how you can customize certain variables on a
2407 per-file basis by including a @dfn{Local Variable} block at the end of
2408 the file. So far, you've only seen a functional interface to @ccmode{}
2409 customization, which is highly inconvenient for use in a Local Variable
2410 block. @ccmode{} provides two variables that make it easier for you to
2411 customize your style on a per-file basis.
2412
2413 @vindex c-file-style
2414 @vindex file-style (c-)
2415 @vindex c-file-offsets
2416 @vindex file-offsets (c-)
2417
2418 The variable @code{c-file-style} can be set to a style name string.
2419 When the file is visited, @ccmode{} will automatically set the
2420 file's style to this style using @code{c-set-style}.
2421
2422 Another variable, @code{c-file-offsets}, takes an association list
2423 similar to what is allowed in @code{c-offsets-alist}. When the file is
2424 visited, @ccmode{} will automatically institute these offsets using
2425 @code{c-set-offset}.
2426
2427 Note that file style settings (i.e. @code{c-file-style}) are applied
2428 before file offset settings (i.e. @code{c-file-offsets}). Also, if
2429 either of these are set in a file's local variable section, all the
2430 style variable values are made local to that buffer.
2431
2432
2433 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2434 @node Advanced Customizations, , Styles, Customizing Indentation
2435 @comment node-name, next, previous, up
2436 @section Advanced Customizations
2437 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2438
2439 @vindex c-style-alist
2440 @vindex style-alist (c-)
2441 For most users, @ccmode{} will support their coding styles with
2442 very little need for more advanced customizations. Usually, one of the
2443 standard styles defined in @code{c-style-alist} will do the trick. At
2444 most, perhaps one of the syntactic symbol offsets will need to be
2445 tweaked slightly, or maybe @code{c-basic-offset} will need to be
2446 changed. However, some styles require a more flexible framework for
2447 customization, and one of the real strengths of @ccmode{} is that
2448 the syntactic analysis model provides just such a framework. This allows
2449 you to implement custom indentation calculations for situations not
2450 handled by the mode directly.
2451
2452 @menu
2453 * Custom Indentation Functions::
2454 * Custom Brace and Colon Hanging::
2455 * Customizing Semi-colons and Commas::
2456 * Other Special Indentations::
2457 @end menu
2458
2459 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2460 @node Custom Indentation Functions, Custom Brace and Colon Hanging, , Advanced Customizations
2461 @comment node-name, next, previous, up
2462 @subsection Custom Indentation Functions
2463 @cindex custom indentation functions
2464 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2465
2466 The most flexible way to customize @ccmode{} is by writing custom
2467 indentation functions, and associating them with specific syntactic
2468 symbols (@pxref{Syntactic Symbols}). @ccmode{} itself uses indentation
2469 functions to provide more sophisticated indentation, for example when
2470 lining up C++ stream operator blocks:
2471 @example
2472 @group
2473
2474 1: void main(int argc, char**)
2475 2: @{
2476 3: cout << "There were "
2477 4: << argc
2478 5: << "arguments passed to the program"
2479 6: << endl;
2480 7: @}
2481
2482 @end group
2483 @end example
2484
2485 In this example, lines 4 through 6 are assigned the @code{stream-op}
2486 syntactic symbol. Here, @code{stream-op} has an offset of @code{+}, and
2487 with a @code{c-basic-offset} of 2, you can see that lines 4 through 6
2488 are simply indented two spaces to the right of line 3. But perhaps we'd
2489 like @ccmode{} to be a little more intelligent so that it aligns
2490 all the @samp{<<} symbols in lines 3 through 6. To do this, we have
2491 to write a custom indentation function which finds the column of first
2492 stream operator on the first line of the statement. Here is sample
2493 lisp code implementing this:
2494 @example
2495 @group
2496
2497 (defun c-lineup-streamop (langelem)
2498 ;; lineup stream operators
2499 (save-excursion
2500 (let* ((relpos (cdr langelem))
2501 (curcol (progn (goto-char relpos)
2502 (current-column))))
2503 (re-search-forward "<<\\|>>" (c-point 'eol) 'move)
2504 (goto-char (match-beginning 0))
2505 (- (current-column) curcol))))
2506
2507 @end group
2508 @end example
2509 @noindent
2510 Indentation functions take a single argument, which is a syntactic
2511 component cons cell (@pxref{Syntactic Analysis}). The function returns
2512 an integer offset value that will be added to the running total
2513 indentation for the line. Note that what actually gets returned is the
2514 difference between the column that the first stream operator is on, and
2515 the column of the buffer relative position passed in the function's
2516 argument. Remember that @ccmode{} automatically adds in the column of
2517 the component's relative buffer position and we don't the column offset
2518 added in twice.
2519
2520 The function should return @code{nil} if it's used in a situation where
2521 it doesn't want to do any decision. If the function is used in a list
2522 expression (@pxref{Customizing Indentation}), that will cause @ccmode{}
2523 to go on and check the next entry in the list.
2524
2525 @cindex stream-op syntactic symbol
2526 @findex c-lineup-streamop
2527 @findex lineup-streamop (c-)
2528 Now, to associate the function @code{c-lineup-streamop} with the
2529 @code{stream-op} syntactic symbol, we can add something like the
2530 following to our @code{c++-mode-hook}@footnote{It probably makes more
2531 sense to add this to @code{c++-mode-hook} than @code{c-mode-common-hook}
2532 since stream operators are only relevant for C++.}:
2533 @example
2534
2535 (c-set-offset 'stream-op 'c-lineup-streamop)
2536
2537 @end example
2538
2539 Now the function looks like this after re-indenting (using @kbd{C-c
2540 C-q}):
2541 @example
2542 @group
2543
2544 1: void main(int argc, char**)
2545 2: @{
2546 3: cout << "There were "
2547 4: << argc
2548 5: << " arguments passed to the program"
2549 6: << endl;
2550 7: @}
2551
2552 @end group
2553 @end example
2554
2555 Custom indentation functions can be as simple or as complex as you like,
2556 and any syntactic symbol that appears in @code{c-offsets-alist} can have
2557 a custom indentation function associated with it.
2558
2559 @ccmode{} comes with an extensive set of predefined indentation
2560 functions, not all of which are used by the default styles. So there's
2561 a good chance the function you want already exists. @xref{Indentation
2562 Functions}, for a list of them. If you have written an indentation
2563 function that you think is generally useful, you're very welcome to
2564 contribute it; please contact @email{bug-cc-mode@@gnu.org}.
2565
2566
2567 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2568 @node Custom Brace and Colon Hanging, Customizing Semi-colons and Commas, Custom Indentation Functions, Advanced Customizations
2569 @comment node-name, next, previous, up
2570 @subsection Custom Brace and Colon Hanging
2571 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2572
2573 @vindex c-hanging-braces-alist
2574 @vindex hanging-braces-alist (c-)
2575 Syntactic symbols aren't the only place where you can customize
2576 @ccmode{} with the lisp equivalent of callback functions. Brace
2577 ``hanginess'' can also be determined by custom functions associated with
2578 syntactic symbols on the @code{c-hanging-braces-alist} style variable.
2579 Remember that @var{ACTION}'s are typically a list containing some
2580 combination of the symbols @code{before} and @code{after}
2581 (@pxref{Hanging Braces}). However, an @var{ACTION} can also be a
2582 function which gets called when a brace matching that syntactic symbol
2583 is entered.
2584
2585 @cindex customizing brace hanging
2586 These @var{ACTION} functions are called with two arguments: the
2587 syntactic symbol for the brace, and the buffer position at which the
2588 brace was inserted. The @var{ACTION} function is expected to return a
2589 list containing some combination of @code{before} and @code{after},
2590 including neither of them (i.e. @code{nil}). This return value has the
2591 normal brace hanging semantics.
2592
2593 As an example, @ccmode{} itself uses this feature to dynamically
2594 determine the hanginess of braces which close ``do-while''
2595 constructs:
2596 @example
2597 @group
2598
2599 void do_list( int count, char** atleast_one_string )
2600 @{
2601 int i=0;
2602 do @{
2603 handle_string( atleast_one_string[i] );
2604 i++;
2605 @} while( i < count );
2606 @}
2607
2608 @end group
2609 @end example
2610
2611 @findex c-snug-do-while
2612 @findex snug-do-while (c-)
2613 @ccmode{} assigns the @code{block-close} syntactic symbol to the
2614 brace that closes the @code{do} construct, and normally we'd like the
2615 line that follows a @code{block-close} brace to begin on a separate
2616 line. However, with ``do-while'' constructs, we want the
2617 @code{while} clause to follow the closing brace. To do this, we
2618 associate the @code{block-close} symbol with the @var{ACTION} function
2619 @code{c-snug-do-while}:
2620 @example
2621
2622 (defun c-snug-do-while (syntax pos)
2623 "Dynamically calculate brace hanginess for do-while statements.
2624 Using this function, `while' clauses that end a `do-while' block will
2625 remain on the same line as the brace that closes that block.
2626
2627 See `c-hanging-braces-alist' for how to utilize this function as an
2628 ACTION associated with `block-close' syntax."
2629 (save-excursion
2630 (let (langelem)
2631 (if (and (eq syntax 'block-close)
2632 (setq langelem (assq 'block-close c-syntactic-context))
2633 (progn (goto-char (cdr langelem))
2634 (if (= (following-char) ?@{)
2635 (forward-sexp -1))
2636 (looking-at "\\<do\\>[^_]")))
2637 '(before)
2638 '(before after)))))
2639
2640 @end example
2641
2642 This function simply looks to see if the brace closes a ``do-while''
2643 clause and if so, returns the list @samp{(before)} indicating
2644 that a newline should be inserted before the brace, but not after it.
2645 In all other cases, it returns the list @samp{(before after)} so
2646 that the brace appears on a line by itself.
2647
2648 @vindex c-syntactic-context
2649 @vindex syntactic-context (c-)
2650 During the call to the brace hanging @var{ACTION} function, the variable
2651 @code{c-syntactic-context} is bound to the full syntactic analysis list.
2652
2653 @cindex customizing colon hanging
2654 @vindex c-hanging-colon-alist
2655 @vindex hanging-colon-alist (c-)
2656 Note that for symmetry, colon hanginess should be customizable by
2657 allowing function symbols as @var{ACTION}s on the
2658 @code{c-hanging-colon-alist} style variable. Since no use has actually
2659 been found for this feature, it isn't currently implemented!
2660
2661
2662 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2663 @node Customizing Semi-colons and Commas, Other Special Indentations, Custom Brace and Colon Hanging, Advanced Customizations
2664 @comment node-name, next, previous, up
2665 @subsection Customizing Semi-colons and Commas
2666 @cindex customizing semi-colons and commas
2667 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2668
2669 @vindex c-hanging-semi&comma-criteria
2670 @vindex hanging-semi&comma-criteria (c-)
2671 You can also customize the insertion of newlines after semi-colons and
2672 commas, when the auto-newline minor mode is enabled (@pxref{Minor
2673 Modes}). This is controlled by the style variable
2674 @code{c-hanging-semi&comma-criteria}, which contains a list of functions
2675 that are called in the order they appear. Each function is called with
2676 zero arguments, and is expected to return one of the following values:
2677
2678 @itemize @bullet
2679 @item
2680 non-@code{nil} --- A newline is inserted, and no more functions from the
2681 list are called.
2682
2683 @item
2684 @code{stop} --- No more functions from the list are called, but no
2685 newline is inserted.
2686
2687 @item
2688 @code{nil} --- No determination is made, and the next function in the
2689 list is called.
2690
2691 @end itemize
2692
2693 If every function in the list is called without a determination being
2694 made, then no newline is added. The default value for this variable is a
2695 list containing a single function which inserts newlines only after
2696 semi-colons which do not appear inside parenthesis lists (i.e. those
2697 that separate @code{for}-clause statements).
2698
2699 @findex c-semi&comma-no-newlines-before-nonblanks
2700 @findex semi&comma-no-newlines-before-nonblanks (c-)
2701 Here's an example of a criteria function, provided by @ccmode{}, that
2702 will prevent newlines from being inserted after semicolons when there is
2703 a non-blank following line. Otherwise, it makes no determination. To
2704 use, add this to the front of the @code{c-hanging-semi&comma-criteria}
2705 list.
2706
2707 @example
2708 @group
2709
2710 (defun c-semi&comma-no-newlines-before-nonblanks ()
2711 (save-excursion
2712 (if (and (eq last-command-char ?\;)
2713 (zerop (forward-line 1))
2714 (not (looking-at "^[ \t]*$")))
2715 'stop
2716 nil)))
2717
2718 @end group
2719 @end example
2720
2721 @findex c-semi&comma-inside-parenlist
2722 @findex c-semi&comma-no-newlines-for-oneline-inliners
2723 @findex semi&comma-inside-parenlist (c-)
2724 @findex semi&comma-no-newlines-for-oneline-inliners (c-)
2725 The function @code{c-semi&comma-inside-parenlist} is what prevents
2726 newlines from being inserted inside the parenthesis list of @code{for}
2727 statements. In addition to
2728 @code{c-semi&comma-no-newlines-before-nonblanks} described above,
2729 @ccmode{} also comes with the criteria function
2730 @code{c-semi&comma-no-newlines-for-oneline-inliners}, which suppresses
2731 newlines after semicolons inside one-line inline method definitions
2732 (i.e. in C++ or Java).
2733
2734
2735 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2736 @node Other Special Indentations, , Customizing Semi-colons and Commas, Advanced Customizations
2737 @comment node-name, next, previous, up
2738 @subsection Other Special Indentations
2739 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2740
2741 @vindex c-label-minimum-indentation
2742 @vindex label-minimum-indentation (c-)
2743 In @samp{gnu} style (@pxref{Built-in Styles}), a minimum indentation
2744 is imposed on lines inside top-level constructs. This minimum
2745 indentation is controlled by the style variable
2746 @code{c-label-minimum-indentation}. The default value for this variable
2747 is 1.
2748
2749 @vindex c-special-indent-hook
2750 @vindex special-indent-hook (c-)
2751 One other customization variable is available in @ccmode{}: The style
2752 variable @code{c-special-indent-hook}. This is a standard hook variable
2753 that is called after every line is indented by @ccmode{}. You can use
2754 it to do any special indentation or line adjustments your style
2755 dictates, such as adding extra indentation to constructors or destructor
2756 declarations in a class definition, etc. Note however, that you should
2757 not change point or mark inside your @code{c-special-indent-hook}
2758 functions (i.e. you'll probably want to wrap your function in a
2759 @code{save-excursion}).
2760
2761 Setting @code{c-special-indent-hook} in your style definition is handled
2762 slightly differently than other variables. In your style definition,
2763 you should set the value for
2764 @code{c-special-indent-hook} to a function or list of functions, which
2765 will be appended to @code{c-special-indent-hook} using @code{add-hook}.
2766 That way, the current setting for the buffer local value of
2767 @code{c-special-indent-hook} won't be overridden.
2768
2769 @kindex M-;
2770 @findex indent-for-comment
2771 @vindex c-indent-comments-syntactically-p
2772 @vindex indent-comments-syntactically-p (c-)
2773 @vindex comment-column
2774 Normally, the standard Emacs command @kbd{M-;}
2775 (@code{indent-for-comment}) will indent comment only lines to
2776 @code{comment-column}. Some users however, prefer that @kbd{M-;} act
2777 just like @kbd{TAB} for purposes of indenting comment-only lines;
2778 i.e. they want the comments to always indent as they would for normal
2779 code, regardless of whether @kbd{TAB} or @kbd{M-;} were used. This
2780 behavior is controlled by the variable
2781 @code{c-indent-comments-syntactically-p}. When @code{nil} (the
2782 default), @kbd{M-;} indents comment-only lines to @code{comment-column},
2783 otherwise, they are indented just as they would be if @kbd{TAB} were
2784 typed.
2785
2786 Note that this has no effect for comment lines that are inserted with
2787 @kbd{M-;} at the end of regular code lines. These comments will always
2788 start at @code{comment-column}.
2789
2790
2791 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2792 @node Syntactic Symbols, Indentation Functions, Customizing Indentation, Top
2793 @comment node-name, next, previous, up
2794 @chapter Syntactic Symbols
2795 @cindex syntactic symbols
2796 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2797
2798 @vindex c-offsets-alist
2799 @vindex offsets-alist (c-)
2800 Here is a complete list of the recognized syntactic symbols as described
2801 in the @code{c-offsets-alist} style variable, along with a brief
2802 description. More detailed descriptions follow.
2803
2804 @table @code
2805 @item string
2806 Inside a multi-line string.
2807 @item c
2808 Inside a multi-line C style block comment.
2809 @item defun-open
2810 Brace that opens a top-level function definition.
2811 @item defun-close
2812 Brace that closes a top-level function definition.
2813 @item defun-block-intro
2814 The first line in a top-level defun.
2815 @item class-open
2816 Brace that opens a class definition.
2817 @item class-close
2818 Brace that closes a class definition.
2819 @item inline-open
2820 Brace that opens an in-class inline method.
2821 @item inline-close
2822 Brace that closes an in-class inline method.
2823 @item func-decl-cont
2824 The region between a function definition's argument list and the
2825 function opening brace (excluding K&R argument declarations). In C, you
2826 cannot put anything but whitespace and comments in this region, however
2827 in C++ and Java, @code{throws} declarations and other things can appear
2828 here.
2829 @item knr-argdecl-intro
2830 First line of a K&R C argument declaration.
2831 @item knr-argdecl
2832 Subsequent lines in a K&R C argument declaration.
2833 @item topmost-intro
2834 The first line in a ``topmost'' definition.
2835 @item topmost-intro-cont
2836 Topmost definition continuation lines.
2837 @item member-init-intro
2838 First line in a member initialization list.
2839 @item member-init-cont
2840 Subsequent member initialization list lines.
2841 @item inher-intro
2842 First line of a multiple inheritance list.
2843 @item inher-cont
2844 Subsequent multiple inheritance lines.
2845 @item block-open
2846 Statement block open brace.
2847 @item block-close
2848 Statement block close brace.
2849 @item brace-list-open
2850 Open brace of an enum or static array list.
2851 @item brace-list-close
2852 Close brace of an enum or static array list.
2853 @item brace-list-intro
2854 First line in an enum or static array list.
2855 @item brace-list-entry
2856 Subsequent lines in an enum or static array list.
2857 @item brace-entry-open
2858 Subsequent lines in an enum or static array list where the line begins
2859 with an open brace.
2860 @item statement
2861 A statement.
2862 @item statement-cont
2863 A continuation of a statement.
2864 @item statement-block-intro
2865 The first line in a new statement block.
2866 @item statement-case-intro
2867 The first line in a case block.
2868 @item statement-case-open
2869 The first line in a case block that starts with a brace.
2870 @item substatement
2871 The first line after a conditional or loop construct.
2872 @item substatement-open
2873 The brace that opens a substatement block.
2874 @item case-label
2875 A @code{case} or @code{default} label.
2876 @item access-label
2877 C++ access control label.
2878 @item label
2879 Any non-special C label.
2880 @item do-while-closure
2881 The @code{while} line that ends a @code{do}-@code{while} construct.
2882 @item else-clause
2883 The @code{else} line of an @code{if}-@code{else} construct.
2884 @item catch-clause
2885 The @code{catch} or @code{finally} (in Java) line of a
2886 @code{try}-@code{catch} construct.
2887 @item comment-intro
2888 A line containing only a comment introduction.
2889 @item arglist-intro
2890 The first line in an argument list.
2891 @item arglist-cont
2892 Subsequent argument list lines when no arguments follow on the same line
2893 as the the arglist opening paren.
2894 @item arglist-cont-nonempty
2895 Subsequent argument list lines when at least one argument follows on the
2896 same line as the arglist opening paren.
2897 @item arglist-close
2898 The solo close paren of an argument list.
2899 @item stream-op
2900 Lines continuing a stream operator (C++ only).
2901 @item inclass
2902 The line is nested inside a class definition.
2903 @item cpp-macro
2904 The start of a C preprocessor macro definition.
2905 @item cpp-macro-cont
2906 Subsequent lines of a multi-line C preprocessor macro definition.
2907 @item friend
2908 A C++ friend declaration.
2909 @item objc-method-intro
2910 The first line of an Objective-C method. definition.
2911 @item objc-method-args-cont
2912 Lines continuing an Objective-C method. definition
2913 @item objc-method-call-cont
2914 Lines continuing an Objective-C method call.
2915 @item extern-lang-open
2916 Brace that opens an external language block.
2917 @item extern-lang-close
2918 Brace that closes an external language block.
2919 @item inextern-lang
2920 Analogous to @code{inclass} syntactic symbol, but used inside external
2921 language blocks (e.g. @code{extern "C" @{}).
2922 @item namespace-open
2923 Brace that opens a C++ namespace block.
2924 @item namespace-close
2925 Brace that closes a C++ namespace block.
2926 @item innamespace
2927 Analogous to @code{inextern-lang} syntactic symbol, but used inside C++
2928 namespace blocks.
2929 @item template-args-cont
2930 C++ template argument list continuations.
2931 @item inlambda
2932 Analogous to @code{inclass} syntactic symbol, but used inside lambda
2933 (i.e. anonymous) functions. Only used in Pike mode.
2934 @item lambda-intro-cont
2935 Lines continuing the header of a lambda function, i.e. between the
2936 @code{lambda} keyword and the function body. Only used in Pike mode.
2937 @item inexpr-statement
2938 A statement block inside an expression. The gcc C extension of this is
2939 recognized. It's also used for the special functions that takes a
2940 statement block as an argument in Pike.
2941 @item inexpr-class
2942 A class definition inside an expression. This is used for anonymous
2943 classes in Java. It's also used for anonymous array initializers in
2944 Java.
2945 @end table
2946
2947 @cindex -open syntactic symbols
2948 @cindex -close syntactic symbols
2949 Most syntactic symbol names follow a general naming convention. When a
2950 line begins with an open or close brace, the syntactic symbol will
2951 contain the suffix @code{-open} or @code{-close} respectively.
2952
2953 @cindex -intro syntactic symbols
2954 @cindex -cont syntactic symbols
2955 @cindex -block-intro syntactic symbols
2956 Usually, a distinction is made between the first line that introduces a
2957 construct and lines that continue a construct, and the syntactic symbols
2958 that represent these lines will contain the suffix @code{-intro} or
2959 @code{-cont} respectively. As a sub-classification of this scheme, a
2960 line which is the first of a particular brace block construct will
2961 contain the suffix @code{-block-intro}.
2962
2963 Let's look at some examples to understand how this works. Remember that
2964 you can check the syntax of any line by using @kbd{C-c C-s}.
2965 @example
2966 @group
2967
2968 1: void
2969 2: swap( int& a, int& b )
2970 3: @{
2971 4: int tmp = a;
2972 5: a = b;
2973 6: b = tmp;
2974 7: int ignored =
2975 8: a + b;
2976 9: @}
2977
2978 @end group
2979 @end example
2980
2981 @cindex topmost-intro syntactic symbol
2982 @cindex topmost-intro-cont syntactic symbol
2983 @cindex defun-open syntactic symbol
2984 @cindex defun-close syntactic symbol
2985 @cindex defun-block-intro syntactic symbol
2986 Line 1 shows a @code{topmost-intro} since it is the first line that
2987 introduces a top-level construct. Line 2 is a continuation of the
2988 top-level construct introduction so it has the syntax
2989 @code{topmost-intro-cont}. Line 3 shows a @code{defun-open} since it is
2990 the brace that opens a top-level function definition. Line 9 is the
2991 corresponding
2992 @code{defun-close} since it contains the brace that closes the top-level
2993 function definition. Line 4 is a @code{defun-block-intro}, i.e. it is
2994 the first line of a brace-block, enclosed in a
2995 top-level function definition.
2996
2997 @cindex statement syntactic symbol
2998 @cindex statement-cont syntactic symbol
2999 Lines 5, 6, and 7 are all given @code{statement} syntax since there
3000 isn't much special about them. Note however that line 8 is given
3001 @code{statement-cont} syntax since it continues the statement begun
3002 on the previous line.
3003
3004 Here's another example, which illustrates some C++ class syntactic
3005 symbols:
3006 @example
3007 @group
3008
3009 1: class Bass
3010 2: : public Guitar,
3011 3: public Amplifiable
3012 4: @{
3013 5: public:
3014 6: Bass()
3015 7: : eString( new BassString( 0.105 )),
3016 8: aString( new BassString( 0.085 )),
3017 9: dString( new BassString( 0.065 )),
3018 10: gString( new BassString( 0.045 ))
3019 11: @{
3020 12: eString.tune( 'E' );
3021 13: aString.tune( 'A' );
3022 14: dString.tune( 'D' );
3023 15: gString.tune( 'G' );
3024 16: @}
3025 17: friend class Luthier;
3026 18: @}
3027
3028 @end group
3029 @end example
3030
3031 @cindex class-open syntactic symbol
3032 @cindex class-close syntactic symbol
3033 As in the previous example, line 1 has the @code{topmost-intro} syntax.
3034 Here however, the brace that opens a C++ class definition on line 4 is
3035 assigned the @code{class-open} syntax. Note that in C++, classes,
3036 structs, and unions are essentially equivalent syntactically (and are
3037 very similar semantically), so replacing the @code{class} keyword in the
3038 example above with @code{struct} or @code{union} would still result in a
3039 syntax of @code{class-open} for line 4 @footnote{This is the case even
3040 for C and Objective-C. For consistency, structs in all supported
3041 languages are syntactically equivalent to classes. Note however that
3042 the keyword @code{class} is meaningless in C and Objective-C.}.
3043 Similarly, line 18 is assigned @code{class-close} syntax.
3044
3045 @cindex inher-intro syntactic symbol
3046 @cindex inher-cont syntactic symbol
3047 Line 2 introduces the inheritance list for the class so it is assigned
3048 the @code{inher-intro} syntax, and line 3, which continues the
3049 inheritance list is given @code{inher-cont} syntax.
3050
3051 @cindex access-label syntactic symbol
3052 @cindex inclass syntactic symbol
3053 Hitting @kbd{C-c C-s} on line 5 shows the following analysis:
3054
3055 @example
3056 @group
3057
3058 @code{((inclass . 58) (access-label . 67))}
3059
3060 @end group
3061 @end example
3062
3063 @noindent
3064 The primary syntactic symbol for this line is @code{access-label} as
3065 this a label keyword that specifies access protection in C++. However,
3066 because this line is also a top-level construct inside a class
3067 definition, the analysis actually shows two syntactic symbols. The
3068 other syntactic symbol assigned to this line is @code{inclass}.
3069 Similarly, line 6 is given both @code{inclass} and @code{topmost-intro}
3070 syntax:
3071
3072 @example
3073 @group
3074
3075 @code{((inclass . 58) (topmost-intro . 60))}
3076
3077 @end group
3078 @end example
3079
3080 @cindex member-init-intro syntactic symbol
3081 @cindex member-init-cont syntactic symbol
3082 Line 7 introduces a C++ member initialization list and as such is given
3083 @code{member-init-intro} syntax. Note that in this case it is
3084 @emph{not} assigned @code{inclass} since this is not considered a
3085 top-level construct. Lines 8 through 10 are all assigned
3086 @code{member-init-cont} since they continue the member initialization
3087 list started on line 7.
3088
3089 @cindex in-class inline methods
3090 @cindex inline-open syntactic symbol
3091 @cindex inline-close syntactic symbol
3092 Line 11's analysis is a bit more complicated:
3093
3094 @example
3095 @group
3096
3097 @code{((inclass . 58) (inline-open))}
3098
3099 @end group
3100 @end example
3101
3102 This line is assigned a syntax of both @code{inline-open} and
3103 @code{inclass} because it opens an @dfn{in-class} C++ inline method
3104 definition. This is distinct from, but related to, the C++ notion of an
3105 inline function in that its definition occurs inside an enclosing class
3106 definition, which in C++ implies that the function should be inlined.
3107 If though, the definition of the @code{Bass} constructor appeared
3108 outside the class definition, the construct would be given the
3109 @code{defun-open} syntax, even if the keyword @code{inline} appeared
3110 before the method name, as in:
3111 @example
3112 @group
3113
3114 class Bass
3115 : public Guitar,
3116 public Amplifiable
3117 @{
3118 public:
3119 Bass();
3120 @}
3121
3122 inline
3123 Bass::Bass()
3124 : eString( new BassString( 0.105 )),
3125 aString( new BassString( 0.085 )),
3126 dString( new BassString( 0.065 )),
3127 gString( new BassString( 0.045 ))
3128 @{
3129 eString.tune( 'E' );
3130 aString.tune( 'A' );
3131 dString.tune( 'D' );
3132 gString.tune( 'G' );
3133 @}
3134
3135 @end group
3136 @end example
3137
3138 @cindex friend syntactic symbol
3139 Returning to the previous example, line 16 is given @code{inline-close}
3140 syntax, while line 12 is given @code{defun-block-open} syntax, and lines
3141 13 through 15 are all given @code{statement} syntax. Line 17 is
3142 interesting in that its syntactic analysis list contains three
3143 elements:
3144
3145 @example
3146
3147 @code{((friend) (inclass . 58) (topmost-intro . 380))}
3148
3149 @end example
3150
3151 The @code{friend} syntactic symbol is a modifier that typically does not
3152 have a relative buffer position.
3153
3154 Template definitions introduce yet another syntactic symbol:
3155
3156 @example
3157 @group
3158
3159 1: ThingManager <int,
3160 2: Framework::Callback *,
3161 3: Mutex> framework_callbacks;
3162
3163 @end group
3164 @end example
3165
3166 Here, line 1 is analyzed as a @code{topmost-intro}, but lines 2 and 3
3167 are both analyzed as @code{template-args-cont} lines.
3168
3169 Here is another (totally contrived) example which illustrates how syntax
3170 is assigned to various conditional constructs:
3171 @example
3172 @group
3173
3174 1: void spam( int index )
3175 2: @{
3176 3: for( int i=0; i<index; i++ )
3177 4: @{
3178 5: if( i == 10 )
3179 6: @{
3180 7: do_something_special();
3181 8: @}
3182 9: else
3183 10: do_something( i );
3184 11: @}
3185 12: do @{
3186 13: another_thing( i-- );
3187 14: @}
3188 15: while( i > 0 );
3189 16: @}
3190
3191
3192 @end group
3193 @end example
3194
3195 @noindent
3196 Only the lines that illustrate new syntactic symbols will be discussed.
3197
3198 @cindex substatement-open syntactic symbol
3199 @cindex substatement-block-intro syntactic symbol
3200 @cindex block-close syntactic symbol
3201 Line 4 has a brace which opens a conditional's substatement block. It
3202 is thus assigned @code{substatement-open} syntax, and since line 5 is
3203 the first line in the substatement block, it is assigned
3204 @code{substatement-block-intro} syntax. Lines 6 and 7 are assigned
3205 similar syntax. Line 8 contains the brace that closes the inner
3206 substatement block. It is given the syntax @code{block-close},
3207 as are lines 11 and 14.
3208
3209 @cindex else-clause syntactic symbol
3210 @cindex catch-clause syntactic symbol
3211 Line 9 is a little different --- since it contains the keyword
3212 @code{else} matching the @code{if} statement introduced on line 5, it is
3213 given the @code{else-clause} syntax. The @code{try}-@code{catch}
3214 constructs in C++ and Java are treated this way too, with the only
3215 difference that the @code{catch}, and in Java also @code{finally}, is
3216 marked with @code{catch-clause}.
3217
3218 @cindex substatement syntactic symbol
3219 Line 10 is also slightly different. Because @code{else} is considered a
3220 conditional introducing keyword @footnote{The list of conditional
3221 keywords are (in C, C++, Objective-C, Java, and Pike): @code{for},
3222 @code{if}, @code{do}, @code{else}, @code{while}, and @code{switch}. C++
3223 and Java have two additional conditional keywords: @code{try} and
3224 @code{catch}. Java also has the @code{finally} and @code{synchronized}
3225 keywords.}, and because the following substatement is not a brace block,
3226 line 10 is assigned the @code{substatement} syntax.
3227
3228 @cindex do-while-closure syntactic symbol
3229 One other difference is seen on line 15. The @code{while} construct
3230 that closes a @code{do} conditional is given the special syntax
3231 @code{do-while-closure} if it appears on a line by itself. Note that if
3232 the @code{while} appeared on the same line as the preceding close brace,
3233 that line would have been assigned @code{block-close} syntax instead.
3234
3235 Switch statements have their own set of syntactic symbols. Here's an
3236 example:
3237 @example
3238 @group
3239
3240 1: void spam( enum Ingredient i )
3241 2: @{
3242 3: switch( i ) @{
3243 4: case Ham:
3244 5: be_a_pig();
3245 6: break;
3246 7: case Salt:
3247 8: drink_some_water();
3248 9: break;
3249 10: default:
3250 11: @{
3251 12: what_is_it();
3252 13: break;
3253 14: @}
3254 15: @}
3255 14: @}
3256
3257 @end group
3258 @end example
3259
3260 @cindex case-label syntactic symbol
3261 @cindex statement-case-intro syntactic symbol
3262 @cindex statement-case-open syntactic symbol
3263 Here, lines 4, 7, and 10 are all assigned @code{case-label} syntax,
3264 while lines 5 and 8 are assigned @code{statement-case-intro}. Line 11
3265 is treated slightly differently since it contains a brace that opens a
3266 block --- it is given @code{statement-case-open} syntax.
3267
3268 @cindex brace lists
3269 There are a set of syntactic symbols that are used to recognize
3270 constructs inside of brace lists. A brace list is defined as an
3271 @code{enum} or aggregate initializer list, such as might statically
3272 initialize an array of structs. The three special aggregate constructs
3273 in Pike, @code{(@{ @})}, @code{([ ])} and @code{(< >)}, are treated as
3274 brace lists too. An example:
3275 @example
3276 @group
3277
3278 1: static char* ingredients[] =
3279 2: @{
3280 3: "Ham",
3281 4: "Salt",
3282 5: NULL
3283 6: @}
3284
3285 @end group
3286 @end example
3287
3288 @cindex brace-list-open syntactic symbol
3289 @cindex brace-list-intro syntactic symbol
3290 @cindex brace-list-close syntactic symbol
3291 @cindex brace-list-entry syntactic symbol
3292 Following convention, line 2 in this example is assigned
3293 @code{brace-list-open} syntax, and line 3 is assigned
3294 @code{brace-list-intro} syntax. Likewise, line 6 is assigned
3295 @code{brace-list-close} syntax. Lines 4 and 5 however, are assigned
3296 @code{brace-list-entry} syntax, as would all subsequent lines in this
3297 initializer list.
3298
3299 @cindex brace-entry-open syntactic symbol
3300 Your static initializer might be initializing nested structures, for
3301 example:
3302 @example
3303 @group
3304
3305 1: struct intpairs[] =
3306 2: @{
3307 3: @{ 1, 2 @},
3308 4: @{
3309 5: 3,
3310 6: 4
3311 7: @}
3312 8: @{ 1,
3313 9: 2 @},
3314 10: @{ 3, 4 @}
3315 11: @}
3316
3317 @end group
3318 @end example
3319
3320 Here, you've already seen the analysis of lines 1, 2, 3, and 11. On
3321 line 4, things get interesting; this line is assigned
3322 @code{brace-entry-open} syntactic symbol because it's a bracelist entry
3323 line that starts with an open brace. Lines 5 and 6 (and line 9) are
3324 pretty standard, and line 7 is a @code{brace-list-close} as you'd
3325 expect. Once again, line 8 is assigned as @code{brace-entry-open} as is
3326 line 10.
3327
3328 External language definition blocks also have their own syntactic
3329 symbols. In this example:
3330 @example
3331 @group
3332
3333 1: extern "C"
3334 2: @{
3335 3: int thing_one( int );
3336 4: int thing_two( double );
3337 5: @}
3338
3339 @end group
3340 @end example
3341
3342 @cindex extern-lang-open syntactic symbol
3343 @cindex extern-lang-close syntactic symbol
3344 @cindex inextern-lang syntactic symbol
3345 @cindex inclass syntactic symbol
3346 @noindent
3347 line 2 is given the @code{extern-lang-open} syntax, while line 5 is given
3348 the @code{extern-lang-close} syntax. The analysis for line 3 yields:
3349 @code{((inextern-lang) (topmost-intro . 14))}, where
3350 @code{inextern-lang} is a modifier similar in purpose to @code{inclass}.
3351
3352 Similarly, C++ namespace constructs have their own associated syntactic
3353 symbols. In this example:
3354 @example
3355 @group
3356
3357 1: namespace foo
3358 2: @{
3359 3: void xxx() @{@}
3360 4: @}
3361
3362 @end group
3363 @end example
3364
3365 @cindex namespace-open syntactic symbol
3366 @cindex namespace-close syntactic symbol
3367 @cindex innamespace syntactic symbol
3368 @noindent
3369 line 2 is given the @code{namespace-open} syntax, while line 4 is given
3370 the @code{namespace-close} syntax. The analysis for line 3 yields:
3371 @code{((innamespace) (topmost-intro . 17))}, where @code{innamespace} is
3372 a modifier similar in purpose to @code{inextern-lang} and @code{inclass}.
3373
3374 A number of syntactic symbols are associated with parenthesis lists,
3375 a.k.a argument lists, as found in function declarations and function
3376 calls. This example illustrates these:
3377 @example
3378 @group
3379
3380 1: void a_function( int line1,
3381 2: int line2 );
3382 3:
3383 4: void a_longer_function(
3384 5: int line1,
3385 6: int line2
3386 7: );
3387 8:
3388 9: void call_them( int line1, int line2 )
3389 10: @{
3390 11: a_function(
3391 12: line1,
3392 13: line2
3393 14: );
3394 15:
3395 16: a_longer_function( line1,
3396 17: line2 );
3397 18: @}
3398
3399 @end group
3400 @end example
3401
3402 @cindex arglist-intro syntactic symbol
3403 @cindex arglist-close syntactic symbol
3404 Lines 5 and 12 are assigned @code{arglist-intro} syntax since they are
3405 the first line following the open parenthesis, and lines 7 and 14 are
3406 assigned @code{arglist-close} syntax since they contain the parenthesis
3407 that closes the argument list.
3408
3409 @cindex arglist-cont-nonempty syntactic symbol
3410 @cindex arglist-cont syntactic symbol
3411 Lines that continue argument lists can be assigned one of two syntactic
3412 symbols. For example, Lines 2 and 17
3413 are assigned @code{arglist-cont-nonempty} syntax. What this means
3414 is that they continue an argument list, but that the line containing the
3415 parenthesis that opens the list is @emph{not empty} following the open
3416 parenthesis. Contrast this against lines 6 and 13 which are assigned
3417 @code{arglist-cont} syntax. This is because the parenthesis that opens
3418 their argument lists is the last character on that line.
3419
3420 Note that there is no @code{arglist-open} syntax. This is because any
3421 parenthesis that opens an argument list, appearing on a separate line,
3422 is assigned the @code{statement-cont} syntax instead.
3423
3424 A few miscellaneous syntactic symbols that haven't been previously
3425 covered are illustrated by this C++ example:
3426 @example
3427 @group
3428
3429 1: void Bass::play( int volume )
3430 2: const
3431 3: @{
3432 4: /* this line starts a multi-line
3433 5: * comment. This line should get `c' syntax */
3434 6:
3435 7: char* a_multiline_string = "This line starts a multi-line \
3436 8: string. This line should get `string' syntax.";
3437 9:
3438 10: note:
3439 11: @{
3440 12: #ifdef LOCK
3441 13: Lock acquire();
3442 14: #endif // LOCK
3443 15: slap_pop();
3444 16: cout << "I played "
3445 17: << "a note\n";
3446 18: @}
3447 19: @}
3448
3449 @end group
3450 @end example
3451
3452 The lines to note in this example include:
3453
3454 @itemize @bullet
3455
3456 @cindex func-decl-cont syntactic symbol
3457 @item
3458 line 2, assigned the @code{func-decl-cont} syntax;
3459
3460 @cindex comment-intro syntactic symbol
3461 @item
3462 line 4, assigned both @code{defun-block-intro} @emph{and}
3463 @code{comment-intro} syntax;
3464
3465 @cindex c syntactic symbol
3466 @item
3467 line 5, assigned @code{c} syntax;
3468
3469 @item
3470 @cindex syntactic whitespace
3471 line 6 which, even though it contains nothing but whitespace, is
3472 assigned @code{defun-block-intro}. Note that the appearance of the
3473 comment on lines 4 and 5 do not cause line 6 to be assigned
3474 @code{statement} syntax because comments are considered to be
3475 @dfn{syntactic whitespace}, which are ignored when analyzing
3476 code;
3477
3478 @cindex string syntactic symbol
3479 @item
3480 line 8, assigned @code{string} syntax;
3481
3482 @cindex label syntactic symbol
3483 @item
3484 line 10, assigned @code{label} syntax;
3485
3486 @cindex block-open syntactic symbol
3487 @item
3488 line 11, assigned @code{block-open} syntax;
3489
3490 @cindex cpp-macro syntactic symbol
3491 @cindex cpp-macro-cont syntactic symbol
3492 @item
3493 lines 12 and 14, assigned @code{cpp-macro} syntax.
3494
3495 @cindex stream-op syntactic symbol
3496 @item
3497 line 17, assigned @code{stream-op} syntax.
3498
3499 @end itemize
3500
3501 @cindex multi-line macros
3502 @cindex syntactic whitespace
3503 Multi-line C preprocessor macros are now (somewhat) supported. At least
3504 @ccmode{} now recognizes the fact that it is inside a multi-line macro,
3505 and it properly skips such macros as syntactic whitespace. In this
3506 example:
3507 @example
3508 @group
3509
3510 1: #define LIST_LOOP(cons, listp) \
3511 2: for (cons = listp; !NILP (cons); cons = XCDR (cons)) \
3512 3: if (!CONSP (cons)) \
3513 4: signal_error ("Invalid list format", listp); \
3514 5: else
3515
3516 @end group
3517 @end example
3518 @noindent
3519 line 1 is given the syntactic symbol @code{cpp-macro}. This first line
3520 of a macro is always given this symbol. The second and subsequent lines
3521 (e.g. lines 2 through 5) are given the @code{cpp-macro-cont} syntactic
3522 symbol, with a relative buffer position pointing to the @code{#} which
3523 starts the macro definition.
3524
3525 In Objective-C buffers, there are three additional syntactic symbols
3526 assigned to various message calling constructs. Here's an example
3527 illustrating these:
3528 @example
3529 @group
3530
3531 1: - (void)setDelegate:anObject
3532 2: withStuff:stuff
3533 3: @{
3534 4: [delegate masterWillRebind:self
3535 5: toDelegate:anObject
3536 6: withExtraStuff:stuff];
3537 7: @}
3538
3539 @end group
3540 @end example
3541
3542 @cindex objc-method-intro syntactic symbol
3543 @cindex objc-method-args-cont syntactic symbol
3544 @cindex objc-method-call-cont syntactic symbol
3545 Here, line 1 is assigned @code{objc-method-intro} syntax, and line 2 is
3546 assigned @code{objc-method-args-cont} syntax. Lines 5 and 6 are both
3547 assigned @code{objc-method-call-cont} syntax.
3548
3549 Java has a concept of anonymous classes, which may look something like
3550 this:
3551 @example
3552 @group
3553
3554 1: public void watch(Observable o) @{
3555 2: o.addObserver(new Observer() @{
3556 3: public void update(Observable o, Object arg) @{
3557 4: history.addElement(arg);
3558 5: @}
3559 6: @});
3560 7: @}
3561
3562 @end group
3563 @end example
3564
3565 @cindex inexpr-class syntactic symbol
3566 The brace following the @code{new} operator opens the anonymous class.
3567 Lines 3 and 6 are assigned the @code{inexpr-class} syntax, besides the
3568 @code{inclass} symbol used in normal classes. Thus, the class will be
3569 indented just like a normal class, with the added indentation given to
3570 @code{inexpr-class}.
3571
3572 There are a few occasions where a statement block may be used inside an
3573 expression. One is in C code using the gcc extension for this, e.g:
3574 @example
3575 @group
3576
3577 1: int res = (@{
3578 2: int y = foo (); int z;
3579 3: if (y > 0) z = y; else z = - y;
3580 4: z;
3581 5: @});
3582
3583 @end group
3584 @end example
3585
3586 @cindex inexpr-statement syntactic symbol
3587 Lines 2 and 5 get the @code{inexpr-statement} syntax, besides the
3588 symbols they'd get in a normal block. Therefore, the indentation put on
3589 @code{inexpr-statement} is added to the normal statement block
3590 indentation.
3591
3592 In Pike code, there are a few other situations where blocks occur inside
3593 statements, as illustrated here:
3594 @example
3595 @group
3596
3597 1: array itgob()
3598 2: @{
3599 3: string s = map (backtrace()[-2][3..],
3600 4: lambda
3601 5: (mixed arg)
3602 6: @{
3603 7: return sprintf ("%t", arg);
3604 8: @}) * ", " + "\n";
3605 9: return catch @{
3606 10: write (s + "\n");
3607 11: @};
3608 12: @}
3609
3610 @end group
3611 @end example
3612
3613 @cindex inlambda syntactic symbol
3614 @cindex lambda-intro-cont syntactic symbol
3615 Lines 4 through 8 contain a lambda function, which @ccmode{} recognizes
3616 by the @code{lambda} keyword. If the function argument list is put
3617 on a line of its own, as in line 5, it gets the @code{lambda-intro-cont}
3618 syntax. The function body is handled as an inline method body, with the
3619 addition of the @code{inlambda} syntactic symbol. This means that line
3620 6 gets @code{inlambda} and @code{inline-open}, and line 8 gets
3621 @code{inline-close}@footnote{You might wonder why it doesn't get
3622 @code{inlambda} too. It's because the closing brace is relative to the
3623 opening brace, which stands on its own line in this example. If the
3624 opening brace was hanging on the previous line, then the closing brace
3625 would get the @code{inlambda} syntax too to be indented correctly.}.
3626
3627 @cindex inexpr-statement syntactic symbol
3628 On line 9, @code{catch} is a special function taking a statement block
3629 as its argument. The block is handled as an in-expression statement
3630 with the @code{inexpr-statement} syntax, just like the gcc extended C
3631 example above. The other similar special function, @code{gauge}, is
3632 handled like this too.
3633
3634 @cindex knr-argdecl-intro syntactic symbol
3635 @cindex knr-argdecl syntactic symbol
3636 Two other syntactic symbols can appear in old style, non-prototyped C
3637 code @footnote{a.k.a. K&R C, or Kernighan & Ritchie C}:
3638 @example
3639 @group
3640
3641 1: int add_three_integers(a, b, c)
3642 2: int a;
3643 3: int b;
3644 4: int c;
3645 5: @{
3646 6: return a + b + c;
3647 7: @}
3648
3649 @end group
3650 @end example
3651
3652 Here, line 2 is the first line in an argument declaration list and so is
3653 given the @code{knr-argdecl-intro} syntactic symbol. Subsequent lines
3654 (i.e. lines 3 and 4 in this example), are given @code{knr-argdecl}
3655 syntax.
3656
3657
3658 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3659 @node Indentation Functions, Performance Issues, Syntactic Symbols, Top
3660 @comment node-name, next, previous, up
3661 @chapter Indentation Functions
3662 @cindex indentation functions
3663 @cindex line-up functions
3664 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3665
3666 Often there are cases when a simple offset setting on a syntactic symbol
3667 isn't enough to get the desired indentation. Therefore, it's also
3668 possible to use a @dfn{indentation function} (a.k.a. line-up function)
3669 for a syntactic symbol.
3670
3671 @ccmode{} comes with many predefined indentation functions for common
3672 situations. If none of these does what you want, you can write your
3673 own, see @ref{Custom Indentation Functions}. If you do, it's probably a
3674 good idea to start working from one of these predefined functions, they
3675 can be found in the file @file{cc-align.el}.
3676
3677 For every function below there is a ``works with'' list that indicates
3678 which syntactic symbols the function is intended to be used with.
3679
3680 @macro workswith
3681 @emph{Works with:}
3682 @end macro
3683 @ifinfo
3684 @unmacro workswith
3685 @macro workswith
3686 Works with:
3687 @end macro
3688 @end ifinfo
3689
3690 @table @code
3691
3692 @findex c-lineup-arglist
3693 @findex lineup-arglist (c-)
3694 @item c-lineup-arglist
3695 Line up the current argument line under the first argument.
3696
3697 @workswith @code{arglist-cont-nonempty}.
3698
3699 @findex c-lineup-arglist-intro-after-paren
3700 @findex lineup-arglist-intro-after-paren (c-)
3701 @item c-lineup-arglist-intro-after-paren
3702 Line up a line just after the open paren of the surrounding paren or
3703 brace block.
3704
3705 @workswith @code{defun-block-intro}, @code{brace-list-intro},
3706 @code{statement-block-intro}, @code{statement-case-intro},
3707 @code{arglist-intro}.
3708
3709 @findex c-lineup-arglist-close-under-paren
3710 @findex lineup-arglist-close-under-paren (c-)
3711 @item c-lineup-arglist-close-under-paren
3712 Set e.g. your @code{arglist-close} syntactic symbol to this line-up
3713 function so that parentheses that close argument lists will line up
3714 under the parenthesis that opened the argument list.
3715
3716 @workswith @code{defun-close}, @code{class-close}, @code{inline-close},
3717 @code{block-close}, @code{brace-list-close}, @code{arglist-close},
3718 @code{extern-lang-close}, @code{namespace-close} (for most of these, a
3719 zero offset will normally produce the same result, though).
3720
3721 @findex c-lineup-close-paren
3722 @findex lineup-close-paren (c-)
3723 @item c-lineup-close-paren
3724 Line up the closing paren under its corresponding open paren if the
3725 open paren is followed by code. If the open paren ends its line, no
3726 indentation is added. E.g:
3727 @example
3728 @group
3729
3730 main (int,
3731 char **
3732 ) // c-lineup-close-paren
3733
3734 @end group
3735 @end example
3736 @noindent
3737 and
3738 @example
3739 @group
3740
3741 main (
3742 int, char **
3743 ) // c-lineup-close-paren
3744
3745 @end group
3746 @end example
3747
3748 @workswith @code{defun-close}, @code{class-close}, @code{inline-close},
3749 @code{block-close}, @code{brace-list-close}, @code{arglist-close},
3750 @code{extern-lang-close}, @code{namespace-close}.
3751
3752 @findex c-lineup-streamop
3753 @findex lineup-streamop (c-)
3754 @item c-lineup-streamop
3755 Line up C++ stream operators (i.e. @samp{<<} and @samp{>>}).
3756
3757 @workswith @code{stream-op}.
3758
3759 @findex c-lineup-multi-inher
3760 @findex lineup-multi-inher (c-)
3761 @item c-lineup-multi-inher
3762 Line up the classes in C++ multiple inheritance clauses under each
3763 other.
3764
3765 @workswith @code{inher-cont}.
3766
3767 @findex c-lineup-java-inher
3768 @findex lineup-java-inher (c-)
3769 @item c-lineup-java-inher
3770 Line up Java implements and extends declarations. If class names
3771 follows on the same line as the @samp{implements}/@samp{extends}
3772 keyword, they are lined up under each other. Otherwise, they are
3773 indented by adding @code{c-basic-offset} to the column of the keyword.
3774 E.g:
3775 @example
3776 @group
3777
3778 class Foo
3779 extends
3780 Bar // c-lineup-java-inher
3781
3782 <--> c-basic-offset
3783
3784 @end group
3785 @end example
3786 @noindent
3787 and
3788 @example
3789 @group
3790
3791 class Foo
3792 extends Cyphr,
3793 Bar // c-lineup-java-inher
3794
3795 @end group
3796 @end example
3797
3798 @workswith @code{inher-cont}.
3799
3800 @findex c-lineup-java-throws
3801 @findex lineup-java-throws (c-)
3802 @item c-lineup-java-throws
3803 Line up Java throws declarations. If exception names follows on the
3804 same line as the throws keyword, they are lined up under each other.
3805 Otherwise, they are indented by adding @code{c-basic-offset} to the
3806 column of the @samp{throws} keyword. The @samp{throws} keyword itself
3807 is also indented by @code{c-basic-offset} from the function declaration
3808 start if it doesn't hang. E.g:
3809 @example
3810 @group
3811
3812 int foo()
3813 throws // c-lineup-java-throws
3814 Bar // c-lineup-java-throws
3815
3816 <--><--> c-basic-offset
3817
3818 @end group
3819 @end example
3820 @noindent
3821 and
3822 @example
3823 @group
3824
3825 int foo() throws Cyphr,
3826 Bar, // c-lineup-java-throws
3827 Vlod // c-lineup-java-throws
3828
3829 @end group
3830 @end example
3831
3832 @workswith @code{func-decl-cont}.
3833
3834 @findex c-indent-one-line-block
3835 @findex indent-one-line-block (c-)
3836 @item c-indent-one-line-block
3837 Indent a one line block @code{c-basic-offset} extra. E.g:
3838 @example
3839 @group
3840
3841 if (n > 0)
3842 @{m+=n; n=0;@} // c-indent-one-line-block
3843
3844 <--> c-basic-offset
3845
3846 @end group
3847 @end example
3848 @noindent
3849 and
3850 @example
3851 @group
3852
3853 if (n > 0)
3854 @{ // c-indent-one-line-block
3855 m+=n; n=0;
3856 @}
3857
3858 @end group
3859 @end example
3860
3861 The block may be surrounded by any kind of parenthesis characters.
3862 @code{nil} is returned if the line doesn't start with a one line block,
3863 which makes the function usable in list expressions.
3864
3865 @workswith Almost all syntactic symbols, but most useful on the
3866 @code{-open} symbols.
3867
3868 @findex c-indent-multi-line-block
3869 @findex indent-multi-line-block (c-)
3870 @item c-indent-multi-line-block
3871 Indent a multi line block @code{c-basic-offset} extra. E.g:
3872 @example
3873 @group
3874
3875 int *foo[] = @{
3876 NULL,
3877 @{17@}, // c-indent-multi-line-block
3878
3879 @end group
3880 @end example
3881 @noindent
3882 and
3883 @example
3884 @group
3885
3886 int *foo[] = @{
3887 NULL,
3888 @{ // c-indent-multi-line-block
3889 17
3890 @},
3891
3892 <--> c-basic-offset
3893
3894 @end group
3895 @end example
3896
3897 The block may be surrounded by any kind of parenthesis characters.
3898 @code{nil} is returned if the line doesn't start with a multi line
3899 block, which makes the function usable in list expressions.
3900
3901 @workswith Almost all syntactic symbols, but most useful on the
3902 @code{-open} symbols.
3903
3904 @findex c-lineup-C-comments
3905 @findex lineup-C-comments (c-)
3906 @item c-lineup-C-comments
3907 Line up C block comment continuation lines. Various heuristics are used
3908 to handle most of the common comment styles. Some examples:
3909 @example
3910
3911 @group
3912 /* /** /*
3913 * text * text text
3914 */ */ */
3915 @end group
3916
3917 @group
3918 /* text /* /**
3919 text ** text ** text
3920 */ */ */
3921 @end group
3922
3923 @group
3924 /**************************************************
3925 * text
3926 *************************************************/
3927 @end group
3928
3929 @vindex comment-start-skip
3930 @group
3931 /**************************************************
3932 Free form text comments:
3933 In comments with a long delimiter line at the
3934 start, the indentation is kept unchanged for lines
3935 that start with an empty comment line prefix. The
3936 delimiter line is whatever matches the
3937 @code{comment-start-skip} regexp.
3938 **************************************************/
3939 @end group
3940
3941 @end example
3942
3943 The style variable @code{c-comment-prefix-regexp} is used to recognize
3944 the comment line prefix, e.g. the @samp{*} that usually starts every
3945 line inside a comment.
3946
3947 @workswith The @code{c} syntactic symbol.
3948
3949 @findex c-lineup-comment
3950 @findex lineup-comment (c-)
3951 @item c-lineup-comment
3952 Line up a comment-only line according to the style variable
3953 @code{c-comment-only-line-offset}. If the comment is lined up with a
3954 comment starter on the previous line, that alignment is preserved.
3955
3956 @vindex c-comment-only-line-offset
3957 @vindex comment-only-line-offset (c-)
3958 @code{c-comment-only-line-offset} specifies the extra offset for the
3959 line. It can contain an integer or a cons cell of the form
3960 @example
3961
3962 (@r{<non-anchored-offset>} . @r{<anchored-offset>})
3963
3964 @end example
3965
3966 @noindent
3967 where @var{non-anchored-offset} is the amount of offset given to
3968 non-column-zero anchored lines, and @var{anchored-offset} is the amount
3969 of offset to give column-zero anchored lines. Just an integer as value
3970 is equivalent to @code{(@r{<value>} . -1000)}.
3971
3972 @workswith @code{comment-intro}.
3973
3974 @findex c-lineup-runin-statements
3975 @findex lineup-runin-statements (c-)
3976 @item c-lineup-runin-statements
3977 Line up statements for coding standards which place the first statement
3978 in a block on the same line as the block opening brace@footnote{Run-in
3979 style doesn't really work too well. You might need to write your own
3980 custom indentation functions to better support this style.}. E.g:
3981 @example
3982 @group
3983
3984 int main()
3985 @{ puts (\"Hello world!\");
3986 return 0; // c-lineup-runin-statements
3987 @}
3988
3989 @end group
3990 @end example
3991
3992 If there is no statement after the opening brace to align with,
3993 @code{nil} is returned. This makes the function usable in list
3994 expressions.
3995
3996 @workswith The @code{statement} syntactic symbol.
3997
3998 @findex c-lineup-math
3999 @findex lineup-math (c-)
4000 @item c-lineup-math
4001 Line up the current line after the equal sign on the first line in the
4002 statement. If there isn't any, indent with @code{c-basic-offset}. If
4003 the current line contains an equal sign too, try to align it with the
4004 first one.
4005
4006 @workswith @code{statement-cont}.
4007
4008 @findex c-lineup-template-args
4009 @findex lineup-template-args (c-)
4010 @item c-lineup-template-args
4011 Line up the arguments of a template argument list under each other, but
4012 only in the case where the first argument is on the same line as the
4013 opening @samp{<}.
4014
4015 To allow this function to be used in a list expression, @code{nil} is
4016 returned if there's no template argument on the first line.
4017
4018 @workswith @code{template-args-cont}.
4019
4020 @findex c-lineup-ObjC-method-call
4021 @findex lineup-ObjC-method-call (c-)
4022 @item c-lineup-ObjC-method-call
4023 For Objective-C code, line up selector args as @code{elisp-mode} does
4024 with function args: go to the position right after the message receiver,
4025 and if you are at the end of the line, indent the current line
4026 c-basic-offset columns from the opening bracket; otherwise you are
4027 looking at the first character of the first method call argument, so
4028 lineup the current line with it.
4029
4030 @workswith @code{objc-method-call-cont}.
4031
4032 @findex c-lineup-ObjC-method-args
4033 @findex lineup-ObjC-method-args (c-)
4034 @item c-lineup-ObjC-method-args
4035 For Objective-C code, line up the colons that separate args. The colon
4036 on the current line is aligned with the one on the first line.
4037
4038 @workswith @code{objc-method-args-cont}.
4039
4040 @findex c-lineup-ObjC-method-args-2
4041 @findex lineup-ObjC-method-args-2 (c-)
4042 @item c-lineup-ObjC-method-args-2
4043 Similar to @code{c-lineup-ObjC-method-args} but lines up the colon on
4044 the current line with the colon on the previous line.
4045
4046 @workswith @code{objc-method-args-cont}.
4047
4048 @findex c-lineup-inexpr-block
4049 @findex lineup-inexpr-block (c-)
4050 @item c-lineup-inexpr-block
4051 This can be used with the in-expression block symbols to indent the
4052 whole block to the column where the construct is started. E.g. for Java
4053 anonymous classes, this lines up the class under the @samp{new} keyword,
4054 and in Pike it lines up the lambda function body under the @samp{lambda}
4055 keyword. Returns @code{nil} if the block isn't part of such a
4056 construct.
4057
4058 @workswith @code{inlambda}, @code{inexpr-statement},
4059 @code{inexpr-class}.
4060
4061 @findex c-lineup-whitesmith-in-block
4062 @findex lineup-whitesmith-in-block (c-)
4063 @item c-lineup-whitesmith-in-block
4064 Line up lines inside a block in Whitesmith style. It's done in a way
4065 that works both when the opening brace hangs and when it doesn't. E.g:
4066 @example
4067 @group
4068
4069 something
4070 @{
4071 foo; // c-lineup-whitesmith-in-block
4072 @}
4073
4074 @end group
4075 @end example
4076 @noindent
4077 and
4078 @example
4079 @group
4080
4081 something @{
4082 foo; // c-lineup-whitesmith-in-block
4083 @}
4084
4085 <--> c-basic-offset
4086
4087 @end group
4088 @end example
4089
4090 In the first case the indentation is kept unchanged, in the second
4091 @code{c-basic-offset} is added.
4092
4093 @workswith @code{defun-close}, @code{defun-block-intro},
4094 @code{block-close}, @code{brace-list-close}, @code{brace-list-intro},
4095 @code{statement-block-intro}, @code{inclass}, @code{inextern-lang},
4096 @code{innamespace}.
4097
4098 @findex c-lineup-dont-change
4099 @findex lineup-dont-change (c-)
4100 @item c-lineup-dont-change
4101 This lineup function returns the indentation of the current line. Think
4102 of it as an identity function for lineups; it is used for
4103 @code{cpp-macro-cont} lines.
4104
4105 @workswith Any syntactic symbol.
4106
4107 @end table
4108
4109
4110 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4111 @node Performance Issues, Frequently Asked Questions, Indentation Functions, Top
4112 @comment node-name, next, previous, up
4113 @chapter Performance Issues
4114 @cindex performance issues
4115 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4116
4117 C and its derivative languages are highly complex creatures. Often,
4118 ambiguous code situations arise that require @ccmode{} to scan
4119 large portions of the buffer to determine syntactic context. Such
4120 pathological code@footnote{such as the output of @code{lex(1)}!}
4121 can cause @ccmode{} to perform fairly badly.
4122 This section identifies some of the coding styles to watch out for, and
4123 suggests some workarounds that you can use to improve performance.
4124
4125 Because @ccmode{} has to scan the buffer backwards from the current
4126 insertion point, and because C's syntax is fairly difficult to parse in
4127 the backwards direction, @ccmode{} often tries to find the nearest
4128 position higher up in the buffer from which to begin a forward scan.
4129 The farther this position is from the current insertion point, the
4130 slower the mode gets. Some coding styles can even force @ccmode{}
4131 to scan from the beginning of the buffer for every line of code!
4132
4133 @findex beginning-of-defun
4134 @findex defun-prompt-regexp
4135 One of the simplest things you can do to reduce scan time, is make sure
4136 any brace that opens a top-level construct@footnote{E.g. a function in
4137 C, or outermost class definition in C++ or Java.} always appears in the
4138 leftmost column. This is actually an Emacs constraint, as embodied in
4139 the @code{beginning-of-defun} function which @ccmode{} uses heavily. If
4140 you insist on hanging top-level open braces on the right side of the
4141 line, then you might want to set the variable @code{defun-prompt-regexp}
4142 to something reasonable, however that ``something reasonable'' is
4143 difficult to define, so @ccmode{} doesn't do it for you.
4144
4145 @vindex c-Java-defun-prompt-regexp
4146 @vindex Java-defun-prompt-regexp (c-)
4147 A special note about @code{defun-prompt-regexp} in Java mode: while much
4148 of the early sample Java code seems to encourage a style where the brace
4149 that opens a class is hung on the right side of the line, this is not a
4150 good style to pursue in Emacs. @ccmode{} comes with a variable
4151 @code{c-Java-defun-prompt-regexp} which tries to define a regular
4152 expression usable for this style, but there are problems with it. In
4153 some cases it can cause @code{beginning-of-defun} to hang@footnote{This
4154 has been observed in Emacs 19.34 and XEmacs 19.15.}. For this reason,
4155 it is not used by default, but if you feel adventurous, you can set
4156 @code{defun-prompt-regexp} to it in your mode hook. In any event,
4157 setting and rely on @code{defun-prompt-regexp} will definitely slow
4158 things down anyway because you'll be doing regular expression searches
4159 for every line you indent, so you're probably screwed either way!
4160
4161 @vindex c-enable-xemacs-performance-kludge-p
4162 @vindex enable-xemacs-performance-kludge-p (c-)
4163 Another alternative for XEmacs users, is to set the variable
4164 @code{c-enable-xemacs-performance-kludge-p} to non-@code{nil}. This
4165 tells @ccmode{} to use XEmacs-specific built-in functions which, in some
4166 circumstances, can locate the top-most opening brace much quicker than
4167 @code{beginning-of-defun}. Preliminary testing has shown that for
4168 styles where these braces are hung (e.g. most JDK-derived Java styles),
4169 this hack can improve performance of the core syntax parsing routines
4170 from 3 to 60 times. However, for styles which @emph{do} conform to
4171 Emacs' recommended style of putting top-level braces in column zero,
4172 this hack can degrade performance by about as much. Thus this variable
4173 is set to @code{nil} by default, since the Emacs-friendly styles
4174 should be more common (and
4175 encouraged!). Note that this variable has no effect in Emacs since the
4176 necessary built-in functions don't exist (in Emacs 20.2 or 20.3 as of
4177 this writing 27-Apr-1998).
4178
4179 You will probably notice pathological behavior from @ccmode{} when
4180 working in files containing large amounts of C preprocessor macros.
4181 This is because Emacs cannot skip backwards over these lines as quickly
4182 as it can comments.
4183
4184 @vindex c-recognize-knr-p
4185 @vindex recognize-knr-p (c-)
4186 Previous versions of @ccmode{} had potential performance problems
4187 when recognizing K&R style function argument declarations. This was
4188 because there are ambiguities in the C syntax when K&R style argument
4189 lists are used@footnote{It is hard to distinguish them from top-level
4190 declarations.}. @ccmode{} has adopted BOCM's convention for
4191 limiting the search: it assumes that argdecls are indented at least one
4192 space, and that the function headers are not indented at all. With
4193 current versions of @ccmode{}, user customization of
4194 @code{c-recognize-knr-p} is deprecated. Just don't put argdecls in
4195 column zero!
4196
4197 @cindex @file{cc-lobotomy.el} file
4198 @vindex cc-lobotomy-pith-list
4199 You might want to investigate the speed-ups contained in the
4200 file @file{cc-lobotomy.el}, which comes as part of the @ccmode{}
4201 distribution, but is completely unsupported.
4202 As mentioned previous, @ccmode{} always trades speed for accuracy,
4203 however it is recognized that sometimes you need speed and can sacrifice
4204 some accuracy in indentation. The file @file{cc-lobotomy.el} contains
4205 hacks that will ``dumb down'' @ccmode{} in some specific ways, making
4206 that trade-off of accurancy for speed. I won't go into details of its
4207 use here; you should read the comments at the top of the file, and look
4208 at the variable @code{cc-lobotomy-pith-list} for details.
4209
4210
4211 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4212 @node Frequently Asked Questions, Getting the Latest CC Mode Release, Performance Issues, Top
4213 @comment node-name, next, previous, up
4214 @chapter Frequently Asked Questions
4215 @cindex frequently asked questions
4216 @cindex FAQ
4217 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4218
4219 @kindex C-x h
4220 @kindex C-M-\
4221 @kindex C-M-x
4222 @kindex C-M-q
4223 @kindex C-M-u
4224 @kindex RET
4225 @kindex C-j
4226 @quotation
4227
4228 @strong{Q.} @emph{How do I re-indent the whole file?}
4229
4230 @strong{A.} Visit the file and hit @kbd{C-x h} to mark the whole
4231 buffer. Then hit @kbd{C-M-\}.
4232
4233 @sp 1
4234 @strong{Q.} @emph{How do I re-indent the entire function?
4235 @kbd{C-M-x} doesn't work.}
4236
4237 @strong{A.} @kbd{C-M-x} is reserved for future Emacs use.
4238 To re-indent the entire function hit @kbd{C-c C-q}.
4239
4240 @sp 1
4241 @strong{Q.} @emph{How do I re-indent the current block?}
4242
4243 @strong{A.} First move to the brace which opens the block with
4244 @kbd{C-M-u}, then re-indent that expression with
4245 @kbd{C-M-q}.
4246
4247 @sp 1
4248 @strong{Q.} @emph{Why doesn't the @kbd{RET} key indent the new line?}
4249
4250 @strong{A.} Emacs' convention is that @kbd{RET} just adds a newline,
4251 and that @kbd{C-j} adds a newline and indents it. You can make
4252 @kbd{RET} do this too by adding this to your
4253 @code{c-mode-common-hook}:
4254 @example
4255
4256 (define-key c-mode-base-map "\C-m" 'c-context-line-break)
4257
4258 @end example
4259
4260 This is a very common question. If you want this to be the default
4261 behavior, don't lobby me, lobby RMS! @t{:-)}
4262
4263 @sp 1
4264 @strong{Q.} @emph{I put @code{(c-set-offset 'substatement-open 0)}
4265 in my @file{.emacs} file but I get an error saying that
4266 @code{c-set-offset}'s function definition is void.}
4267
4268 @strong{A.} This means that @ccmode{} wasn't loaded into your
4269 Emacs session by the time the @code{c-set-offset} call was reached,
4270 mostly likely because @ccmode{} is being autoloaded. Instead
4271 of putting the @code{c-set-offset} line in your top-level
4272 @file{.emacs} file, put it in your @code{c-mode-common-hook}, or
4273 simply modify @code{c-offsets-alist} directly:
4274 @example
4275 @group
4276
4277 (setq c-offsets-alist (cons '(substatement-open . 0)
4278 c-offsets-alist))
4279 @end group
4280 @end example
4281
4282 @sp 1
4283 @strong{Q.} @emph{My style settings works in all the @ccmode{} language
4284 modes except for Java, where I still get e.g. 4 column indentation.}
4285
4286 @strong{A.} Java mode switches to the @samp{java} style by default for
4287 historical reasons. You can override it by putting an association list
4288 on @code{c-default-style}:
4289 @example
4290
4291 (setq c-default-style '((other . "my-style")))
4292
4293 @end example
4294 @noindent
4295 The @samp{other} symbol says that @ccmode{} should use "my-style" in all
4296 modes not explicitly listed. Since there's nothing else on the list
4297 this causes "my-style" to be used in every mode.
4298
4299 @sp 1
4300 @strong{Q.} @emph{How do I make strings, comments, keywords, and other
4301 constructs appear in different colors, or in bold face, etc.?}
4302
4303 @strong{A.} ``Syntax Colorization'' is a standard Emacs feature,
4304 controlled by @code{font-lock-mode}. @ccmode{} does not contain
4305 font-lock definitions for any of its supported languages.
4306
4307 @sp 1
4308 @strong{Q.} @emph{@kbd{M-a} and @kbd{M-e} used to move over entire
4309 balanced brace lists, but now they move into blocks. How do I get the
4310 old behavior back?}
4311
4312 @strong{A.} Use @kbd{C-M-f} and @kbd{C-M-b} to move over balanced brace
4313 blocks. Use @kbd{M-a} and @kbd{M-e} to move by statements, which will
4314 also move into blocks.
4315
4316 @sp 1
4317 @strong{Q.} @emph{Whenever I try to indent a line or type an
4318 ``electric'' key such as @kbd{;}, @kbd{@{}, or @kbd{@}}, I get an error
4319 that look like this: @code{Invalid function: (macro . #[...}. What
4320 gives?}
4321
4322 @strong{A.} This is a common error when @ccmode{} hasn't been compiled
4323 correctly, especially under Emacs 19.34@footnote{Technically, it's
4324 because some macros wasn't defined during the compilation, so the byte
4325 compiler put in function calls instead of the macro expansions. Later,
4326 when the interpreter tries to call the macros as functions, it shows
4327 this (somewhat cryptic) error message.}. If you are using the standalone
4328 @ccmode{} distribution, try recompiling it according to the instructions
4329 in the @file{README} file.
4330
4331 @end quotation
4332
4333
4334 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4335 @node Getting the Latest CC Mode Release, Sample .emacs File, Frequently Asked Questions, Top
4336 @comment node-name, next, previous, up
4337 @chapter Getting the Latest CC Mode Release
4338 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4339
4340 @ccmode{} is standard with all versions of Emacs since 19.34 and of
4341 XEmacs since 19.16.
4342
4343 Due to release schedule skew, it is likely that all of these Emacsen
4344 have old versions of @ccmode{} and so should be upgraded. Access to the
4345 @ccmode{} source code, as well as more detailed information on Emacsen
4346 compatibility, etc. are all available via the Web at:
4347
4348 @example
4349 @group
4350
4351 @uref{http://www.python.org/emacs/cc-mode/}
4352
4353 @end group
4354 @end example
4355
4356 @emph{Old URLs, including the FTP URLs, should no longer be used.}
4357
4358 There are many files under these directories; you can pick up the entire
4359 distribution (named @code{cc-mode.tar.gz}; a gzip'd tar file), or any of
4360 the individual files, including PostScript documentation.
4361
4362
4363 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4364 @node Sample .emacs File, Limitations and Known Bugs, Getting the Latest CC Mode Release, Top
4365 @comment node-name, next, previous, up
4366 @chapter Sample .emacs file
4367 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4368
4369 @example
4370 ;; Here's a sample .emacs file that might help you along the way. Just
4371 ;; copy this region and paste it into your .emacs file. You may want to
4372 ;; change some of the actual values.
4373
4374 (defconst my-c-style
4375 '((c-tab-always-indent . t)
4376 (c-comment-only-line-offset . 4)
4377 (c-hanging-braces-alist . ((substatement-open after)
4378 (brace-list-open)))
4379 (c-hanging-colons-alist . ((member-init-intro before)
4380 (inher-intro)
4381 (case-label after)
4382 (label after)
4383 (access-label after)))
4384 (c-cleanup-list . (scope-operator
4385 empty-defun-braces
4386 defun-close-semi))
4387 (c-offsets-alist . ((arglist-close . c-lineup-arglist)
4388 (substatement-open . 0)
4389 (case-label . 4)
4390 (block-open . 0)
4391 (knr-argdecl-intro . -)))
4392 (c-echo-syntactic-information-p . t)
4393 )
4394 "My C Programming Style")
4395
4396 ;; offset customizations not in my-c-style
4397 (setq c-offsets-alist (cons '(member-init-intro . ++)
4398 c-offsets-alist))
4399
4400 ;; Customizations for all modes in CC Mode.
4401 (defun my-c-mode-common-hook ()
4402 ;; add my personal style and set it for the current buffer
4403 (c-add-style "PERSONAL" my-c-style t)
4404 ;; other customizations
4405 (setq tab-width 8
4406 ;; this will make sure spaces are used instead of tabs
4407 indent-tabs-mode nil)
4408 ;; we like auto-newline and hungry-delete
4409 (c-toggle-auto-hungry-state 1)
4410 ;; keybindings for all supported languages. We can put these in
4411 ;; c-mode-base-map because c-mode-map, c++-mode-map, objc-mode-map,
4412 ;; java-mode-map, idl-mode-map, and pike-mode-map inherit from it.
4413 (define-key c-mode-base-map "\C-m" 'c-context-line-break)
4414 )
4415
4416 (add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
4417 @end example
4418
4419
4420 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4421 @node Limitations and Known Bugs, Mailing Lists and Submitting Bug Reports, Sample .emacs File, Top
4422 @comment node-name, next, previous, up
4423 @chapter Limitations and Known Bugs
4424 @cindex limitations
4425 @cindex bugs
4426 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4427
4428 @itemize @bullet
4429 @item
4430 Re-indenting large regions or expressions can be slow.
4431
4432 @findex c-indent-exp
4433 @findex indent-exp (c-)
4434 @item
4435 @code{c-indent-exp} has not been fully optimized. It essentially
4436 equivalent to hitting @kbd{TAB} (@code{c-indent-command}) on every
4437 line. Some information is cached from line to line, but such caching
4438 invariable causes inaccuracies in analysis in some bizarre situations.
4439
4440 @vindex signal-error-on-buffer-boundary
4441 @item
4442 XEmacs versions from 19.15 until (as of this writing 12-Mar-1998) 20.4
4443 contain a variable called @code{signal-error-on-buffer-boundary}. This
4444 was intended as a solution to user interface problems associated with
4445 buffer movement and the @code{zmacs-region} deactivation on errors.
4446 However, setting this variable to a non-default value had the
4447 deleterious side effect of breaking many built-in primitive functions.
4448 Most users will not be affected since they never change the value of
4449 this variable. @strong{Do not set this variable to @code{nil}}; you
4450 will cause serious problems in @ccmode{} and probably other XEmacs
4451 packages! As of at least XEmacs 20.4, the effects this variable tried
4452 to correct have been fixed in other, better ways.
4453
4454 @end itemize
4455
4456
4457 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4458 @node Mailing Lists and Submitting Bug Reports, Concept Index, Limitations and Known Bugs, Top
4459 @comment node-name, next, previous, up
4460 @chapter Mailing Lists and Submitting Bug Reports
4461 @cindex mailing lists
4462 @cindex reporting bugs
4463 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4464
4465 @kindex C-c C-b
4466 @findex c-submit-bug-report
4467 @findex submit-bug-report (c-)
4468 To report bugs, use the @kbd{C-c C-b} (@code{c-submit-bug-report})
4469 command. This provides vital information we need to reproduce your
4470 problem. Make sure you include a concise, but complete code example.
4471 Please try to boil your example down to just the essential code needed
4472 to reproduce the problem, and include an exact recipe of steps needed to
4473 expose the bug. Be especially sure to include any code that appears
4474 @emph{before} your bug example, if you think it might affect our ability
4475 to reproduce it.
4476
4477 Please try to produce the problem in an Emacs instance without any
4478 customizations loaded (i.e. start it with the @code{-q -no-site-file}
4479 arguments). If it works correctly there, the problem might be caused by
4480 faulty customizations in either your own or your site configuration. In
4481 that case, we'd appreciate if you isolate the Emacs Lisp code that trigs
4482 the bug and include it in your report.
4483
4484 Bug reports are now sent to the following email addresses:
4485 @email{bug-cc-mode@@gnu.org} and @email{bug-gnu-emacs@@gnu.org}; the
4486 latter is mirrored on the Usenet newsgroup @code{gnu.emacs.bug}. You
4487 can send other questions and suggestions (kudos? @t{;-)} to
4488 @email{bug-cc-mode@@gnu.org}, or @email{help-gnu-emacs@@gnu.org} which is
4489 mirrored on newsgroup @code{gnu.emacs.help}. The old contact address
4490 @email{cc-mode-help@@python.org} is still active, but its use is
4491 discouraged.
4492
4493 @cindex beta testers mailing list
4494 @cindex announcement mailing list
4495 If you want to get announcements of new @ccmode{} releases, send the word
4496 @emph{subscribe} in the body of a message to
4497 @email{cc-mode-announce-request@@python.org}. Announcements will also
4498 be posted to the Usenet newsgroups @code{gnu.emacs.sources},
4499 @code{comp.emacs}, @code{comp.emacs.xemacs}, and possibly some of the
4500 language oriented newsgroups. Note that the
4501 @code{cc-mode-victims@@python.org} mailing list has been
4502 decommissioned.
4503
4504
4505 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4506 @node Concept Index, Command Index, Mailing Lists and Submitting Bug Reports, Top
4507 @comment node-name, next, previous, up
4508 @unnumbered Concept Index
4509 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4510
4511 @printindex cp
4512
4513
4514 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4515 @node Command Index, Key Index, Concept Index, Top
4516 @comment node-name, next, previous, up
4517 @unnumbered Command Index
4518 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4519
4520 Since most @ccmode{} commands are prepended with the string
4521 @samp{c-}, each appears under its @code{c-@var{<thing>}} name and its
4522 @code{@var{<thing>} (c-)} name.
4523 @iftex
4524 @sp 2
4525 @end iftex
4526 @printindex fn
4527
4528
4529 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4530 @node Key Index, Variable Index, Command Index, Top
4531 @comment node-name, next, previous, up
4532 @unnumbered Key Index
4533 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4534
4535 @printindex ky
4536
4537
4538 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4539 @node Variable Index, , Key Index, Top
4540 @comment node-name, next, previous, up
4541 @unnumbered Variable Index
4542 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4543
4544 Since most @ccmode{} variables are prepended with the string
4545 @samp{c-}, each appears under its @code{c-@var{<thing>}} name and its
4546 @code{@var{<thing>} (c-)} name.
4547 @iftex
4548 @sp 2
4549 @end iftex
4550 @printindex vr
4551 @page
4552 @summarycontents
4553 @contents
4554 @bye