Update years in copyright notice; nfc.
[bpt/emacs.git] / man / cc-mode.texi
CommitLineData
6bf7aab6
DL
1\input texinfo
2
3a731e1f
MS
3@c Notes to self regarding line handling:
4@c
5@c Empty lines are often significant before @end directives; avoid them.
6@c
7@c Empty lines before and after @example directives are significant in
8@c info output but not in TeX. Empty lines inside @example directives
9@c are significant.
10
11@c Conventions for formatting examples:
12@c o If the example contains empty lines then put the surrounding empty
13@c lines inside the @example directives. Put them outside otherwise.
14@c o Use @group inside the example only if it shows indentation where
15@c the relation between lines inside is relevant.
16@c o Format line number columns like this:
17@c 1: foo
18@c 2: bar
19@c ^ one space
20@c ^^ two columns, right alignment
21@c o Check line lengths in TeX output; they can typically be no longer
22@c than 70 chars, 60 if the paragraph is indented.
23
24@comment TBD: Document the finer details of statement anchoring?
25
6bf7aab6
DL
26@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
27@comment %**start of header (This is for running Texinfo on a region)
28@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
29
d7bd46ed
GM
30@comment No overfull hbox marks in the dvi file.
31@finalout
6bf7aab6 32
94cae5a8 33@setfilename ../info/ccmode
cb7f2e96 34@settitle CC Mode Manual
d7bd46ed 35@footnotestyle end
6bf7aab6
DL
36
37@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
38@comment @setchapternewpage odd !! we don't want blank pages !!
39@comment %**end of header (This is for running Texinfo on a region)
40@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
41
42
43@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
bf247b6e 44@comment
6bf7aab6
DL
45@comment Texinfo manual for CC Mode
46@comment Generated from the original README file by Krishna Padmasola
47@comment <krishna@earth-gw.njit.edu>
cb7f2e96
GM
48@comment
49@comment Authors:
50@comment Barry A. Warsaw
51@comment Martin Stjernholm
52@comment
f214c025 53@comment Maintained by Martin Stjernholm <bug-cc-mode@gnu.org>
bf247b6e 54@comment
6bf7aab6
DL
55@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
56
3a731e1f 57@comment Define an index for syntactic symbols.
5f36ee6d 58@ifnottex
3a731e1f 59@defindex ss
5f36ee6d 60@end ifnottex
3a731e1f
MS
61
62@comment Combine key, syntactic symbol and concept indices into one.
63@syncodeindex ss cp
64@syncodeindex ky cp
65
18f952d5
KB
66@copying
67This manual is for CC Mode in Emacs.
cb7f2e96 68
3a731e1f 69Copyright @copyright{} 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
b65d8176 70 2003, 2004, 2005 Free Software Foundation, Inc.
94cae5a8 71
18f952d5 72@quotation
94cae5a8
GM
73Permission is granted to copy, distribute and/or modify this document
74under the terms of the GNU Free Documentation License, Version 1.1 or
75any later version published by the Free Software Foundation; with the
76Invariant Sections being ``The GNU Manifesto'', ``Distribution'' and
77``GNU GENERAL PUBLIC LICENSE'', with the Front-Cover texts being ``A GNU
78Manual'', and with the Back-Cover Texts as in (a) below. A copy of the
79license is included in the section entitled ``GNU Free Documentation
80License'' in the Emacs manual.
81
82(a) The FSF's Back-Cover Text is: ``You have freedom to copy and modify
83this GNU Manual, like GNU software. Copies published by the Free
84Software Foundation raise funds for GNU development.''
85
86This document is part of a collection distributed under the GNU Free
87Documentation License. If you want to distribute this document
88separately from the collection, you can do so by adding a copy of the
89license to the document, as described in section 6 of the license.
18f952d5
KB
90@end quotation
91@end copying
92
18f952d5
KB
93@comment Info directory entry for use by install-info. The indentation
94@comment here is by request from the FSF folks.
95@dircategory Emacs
96@direntry
3a731e1f
MS
97* CC Mode: (ccmode). Emacs mode for editing C, C++, Objective-C,
98 Java, Pike, AWK, and CORBA IDL code.
18f952d5 99@end direntry
6bf7aab6
DL
100
101@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
cb7f2e96 102@comment TeX title page
6bf7aab6
DL
103@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
104
105@titlepage
106@sp 10
107
3a731e1f 108@center @titlefont{CC Mode 5.30}
6bf7aab6
DL
109@sp 2
110@center @subtitlefont{A GNU Emacs mode for editing C and C-like languages}
111@sp 2
3a731e1f 112@center Barry A. Warsaw, Martin Stjernholm, Alan Mackenzie (AWK support)
6bf7aab6
DL
113
114@page
115@vskip 0pt plus 1filll
18f952d5 116@insertcopying
6bf7aab6
DL
117@end titlepage
118
6bf7aab6
DL
119@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
120@comment The Top node contains the master menu for the Info file.
121@comment This appears only in the Info file, not the printed manual.
122@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
123
d7bd46ed
GM
124@node Top, Introduction, (dir), (dir)
125@comment node-name, next, previous, up
126
127@macro ccmode
128CC Mode
129@end macro
130
131@ifinfo
132@top @ccmode{}
6bf7aab6 133
d7bd46ed 134@ccmode{} is a GNU Emacs mode for editing files containing C, C++,
3a731e1f
MS
135Objective-C, Java, CORBA IDL (and the variants PSDL and CIDL), Pike
136code and to a certain extent, AWK code @xref{AWK Mode}. It provides
137syntax-based indentation, font locking, and has several handy commands
138and some minor modes to make the editing easier. It does not provide
139tools to look up and navigate between functions, classes etc - there are
140other packages for that.
d7bd46ed 141@end ifinfo
6bf7aab6
DL
142
143@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
144@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
145
146@menu
d7bd46ed
GM
147* Introduction::
148* Getting Connected::
3a731e1f 149* Indentation Engine::
6bf7aab6 150* Minor Modes::
d7bd46ed 151* Text Filling and Line Breaking::
3a731e1f
MS
152* Macro Handling::
153* Font Locking::
6bf7aab6
DL
154* Commands::
155* Customizing Indentation::
156* Syntactic Symbols::
d7bd46ed 157* Indentation Functions::
3a731e1f
MS
158* AWK Mode::
159* Odds and Ends::
6bf7aab6 160* Performance Issues::
cb7f2e96 161* Limitations and Known Bugs::
6bf7aab6 162* Frequently Asked Questions::
d7bd46ed 163* Getting the Latest CC Mode Release::
d7bd46ed 164* Mailing Lists and Submitting Bug Reports::
cb7f2e96 165* Sample .emacs File::
d7bd46ed
GM
166
167 --- Indices ---
168
3a731e1f 169* Command and Function Index::
d7bd46ed 170* Variable Index::
3a731e1f 171* Concept Index::
d7bd46ed 172
3a731e1f 173@detailmenu
d7bd46ed
GM
174 --- The Detailed Node Listing ---
175
3a731e1f 176Indentation Engine
d7bd46ed
GM
177
178* Syntactic Analysis::
179* Indentation Calculation::
180
181Minor Modes
182
183* Auto-newline Insertion::
184* Hungry-deletion of Whitespace::
185
3a731e1f
MS
186Font Locking
187
188* Font Locking Preliminaries::
189* Faces::
190* Documentation Comments::
191
d7bd46ed
GM
192Auto-newline Insertion
193
194* Hanging Braces::
195* Hanging Colons::
3a731e1f 196* Hanging Semicolons and Commas::
d7bd46ed
GM
197* Other Electric Commands::
198* Clean-ups::
199
200Commands
201
202* Indentation Commands::
203* Movement Commands::
204* Other Commands::
205
206Customizing Indentation
207
208* Interactive Customization::
209* Permanent Customization::
210* Hooks::
211* Styles::
212* Advanced Customizations::
213
214Styles
215
216* Built-in Styles::
3a731e1f 217* Choosing a Style::
d7bd46ed
GM
218* Adding Styles::
219* File Styles::
220
221Advanced Customizations
222
223* Custom Indentation Functions::
224* Custom Brace and Colon Hanging::
3a731e1f 225* Customizing Semicolons and Commas::
d7bd46ed 226* Other Special Indentations::
3a731e1f
MS
227
228AWK Mode
229
230* Initialising AWK Mode::
231* AWK Mode Font Locking::
232* AWK Mode Defuns::
233@end detailmenu
6bf7aab6
DL
234@end menu
235
d7bd46ed 236
6bf7aab6 237@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
d7bd46ed
GM
238@node Introduction, Getting Connected, Top, Top
239@comment node-name, next, previous, up
3a731e1f 240@chapter Introduction
6bf7aab6
DL
241@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
242
6bf7aab6
DL
243@cindex BOCM
244
d7bd46ed 245Welcome to @ccmode{}, a GNU Emacs mode for editing files containing C,
3a731e1f
MS
246C++, Objective-C, Java, CORBA IDL (and the variants CORBA PSDL and
247CIDL), Pike and to a certain extent, AWK code (@pxref{AWK Mode}). This
248incarnation of the mode is descended from @file{c-mode.el} (also called
249``Boring Old C Mode'' or BOCM @t{:-)}, and @file{c++-mode.el} version 2,
250which Barry has been maintaining since 1992. Late in 1997, Martin
d7bd46ed 251joined the @ccmode{} Maintainers Team, and implemented the Pike support.
3a731e1f
MS
252As of 2000 Martin has taken over as the sole maintainer. @ccmode{} did
253not originally contain the font lock support for its languages --- that
254was added in version 5.30. AWK support was also added in 5.30 by Alan
255Mackenzie.
d7bd46ed
GM
256
257This manual describes @ccmode{}
258@comment The following line must appear on its own, so that the automated
3a731e1f 259version 5.30.
d7bd46ed 260@comment Release.py script can update the version number automatically
6bf7aab6 261
3a731e1f
MS
262@ccmode{} supports the editing of K&R and ANSI C, C++, Objective-C,
263Java, CORBA's Interface Definition Language, Pike@footnote{A C-like
264scripting language with its roots in the LPC language used in some MUD
265engines. See @uref{http://pike.ida.liu.se/}.} and AWK files. In this
266way, you can easily set up consistent font locking and coding styles for
267use in editing all of these languages, although AWK is not yet as
268uniformly integrated as the other languages.
6bf7aab6
DL
269
270@findex c-mode
271@findex c++-mode
272@findex objc-mode
273@findex java-mode
274@findex idl-mode
d7bd46ed 275@findex pike-mode
3a731e1f 276@findex awk-mode
94cae5a8 277Note that the name of this package is ``@ccmode{},'' but there is no top
6bf7aab6 278level @code{cc-mode} entry point. All of the variables, commands, and
3a731e1f 279functions in @ccmode{} are prefixed with @code{c-@var{thing}}, and
d7bd46ed 280@code{c-mode}, @code{c++-mode}, @code{objc-mode}, @code{java-mode},
3a731e1f
MS
281@code{idl-mode}, @code{pike-mode}, and @code{awk-mode} entry points are
282provided. This package is intended to be a replacement for
283@file{c-mode.el}, @file{c++-mode.el} and @file{awk-mode.el}.
284
285@c @cindex @file{cc-compat.el} file
286@c This distribution also contains a file
287@c called @file{cc-compat.el} which should ease your transition from BOCM
288@c to @ccmode{}. If you have a BOCM configuration you are really happy
289@c with, and want to postpone learning how to configure @ccmode{}, take a
290@c look at that file. It maps BOCM configuration variables to @ccmode{}'s
291@c indentation model. It is not actively supported so for the long run,
292@c you should learn how to customize @ccmode{} to support your coding
293@c style.
6bf7aab6
DL
294
295A special word of thanks goes to Krishna Padmasola for his work in
296converting the original @file{README} file to Texinfo format. I'd also
297like to thank all the @ccmode{} victims who help enormously during the
298early beta stages of @ccmode{}'s development.
299
300
301@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3a731e1f 302@node Getting Connected, Indentation Engine, Introduction, Top
d7bd46ed 303@comment node-name, next, previous, up
3a731e1f 304@chapter Getting Connected
6bf7aab6
DL
305@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
306
307If you got this version of @ccmode{} with Emacs or XEmacs, it should
308work just fine right out of the box. Note however that you may not have
309the latest @ccmode{} release and may want to upgrade your copy.
310
311If you are upgrading an existing @ccmode{} installation, please see the
312@file{README} file for installation details. @ccmode{} may not work
313with older versions of Emacs or XEmacs. See the @ccmode{} release notes
3a731e1f
MS
314at @uref{http://cc-mode.sourceforge.net} for the latest information on
315Emacs version and package compatibility (@pxref{Getting the Latest CC
316Mode Release}).
6bf7aab6 317
3a731e1f 318@deffn Command c-version
6bf7aab6
DL
319@findex version (c-)
320You can find out what version of @ccmode{} you are using by visiting a C
321file and entering @kbd{M-x c-version RET}. You should see this message in
322the echo area:
6bf7aab6 323
3a731e1f 324@example
6bf7aab6 325Using CC Mode version 5.XX
6bf7aab6
DL
326@end example
327
328@noindent
329where @samp{XX} is the minor release number.
3a731e1f 330@end deffn
6bf7aab6
DL
331
332
333@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3a731e1f 334@node Indentation Engine, Minor Modes, Getting Connected, Top
d7bd46ed 335@comment node-name, next, previous, up
3a731e1f 336@chapter Indentation Engine
6bf7aab6
DL
337@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
338
3a731e1f
MS
339@ccmode{} has an indentation engine that provides a flexible and general
340mechanism for customizing indentation. It separates indentation
341calculation into two steps: first, @ccmode{} analyzes the line of code
342being indented to determine the kind of language construct it's looking
343at, then it applies user defined offsets to the current line based on
344this analysis.
6bf7aab6
DL
345
346This section will briefly cover how indentation is calculated in
3a731e1f
MS
347@ccmode{}. It is important to understand the indentation model being
348used so that you will know how to customize @ccmode{} for your personal
349coding style. All the details are in @ref{Customizing Indentation}, and
350later chapters.
351
352@defopt c-syntactic-indentation
353@vindex syntactic-indentation (c-)
354Syntactic analysis for indentation is done when this is non-@code{nil}
355(which is the default). When it's @code{nil} every line is just
356indented to the same level as the previous one, and @kbd{TAB}
357(@code{c-indent-command}) adjusts the indentation in steps of
358@code{c-basic-offset}. The indentation style has no effect, nor any of
c3a2e2d5 359the indentation associated variables, e.g., @code{c-special-indent-hook}.
3a731e1f 360@end defopt
6bf7aab6
DL
361
362@menu
363* Syntactic Analysis::
364* Indentation Calculation::
365@end menu
366
367
368@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3a731e1f 369@node Syntactic Analysis, Indentation Calculation, , Indentation Engine
d7bd46ed 370@comment node-name, next, previous, up
3a731e1f 371@section Syntactic Analysis
d7bd46ed 372@cindex syntactic analysis
6bf7aab6
DL
373@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
374
6bf7aab6 375@cindex relative buffer position
3a731e1f 376@cindex syntactic symbols
6bf7aab6
DL
377@cindex syntactic component
378@cindex syntactic component list
6bf7aab6
DL
379The first thing @ccmode{} does when indenting a line of code, is to
380analyze the line, determining the @dfn{syntactic component list} of the
d7bd46ed 381construct on that line. A syntactic component consists of a pair of
3a731e1f
MS
382elements (in lisp parlance, a @emph{cons cell}), the first being
383a @dfn{syntactic symbol}, the second being a @dfn{relative
6bf7aab6 384buffer position}. Syntactic symbols describe elements of C code
3a731e1f 385@footnote{Unless otherwise noted, the term ``C code'' refers to all
c3a2e2d5 386the C-like languages.}, e.g., @code{statement}, @code{substatement},
d7bd46ed
GM
387@code{class-open}, @code{class-close}, etc. @xref{Syntactic Symbols},
388for a complete list of currently recognized syntactic symbols and their
389semantics. The style variable @code{c-offsets-alist} also contains the
390list of currently supported syntactic symbols.
6bf7aab6
DL
391
392Conceptually, a line of C code is always indented relative to the
393indentation of some line higher up in the buffer. This is represented
394by the relative buffer position in the syntactic component.
395
396Here is an example. Suppose we had the following code as the only thing
d7bd46ed
GM
397in a C++ buffer @footnote{The line numbers in this and future examples
398don't actually appear in the buffer, of course!}:
6bf7aab6 399
3a731e1f
MS
400@example
401 1: void swap( int& a, int& b )
402 2: @{
403 3: int tmp = a;
404 4: a = b;
405 5: b = tmp;
406 6: @}
6bf7aab6
DL
407@end example
408
409@kindex C-c C-s
410@findex c-show-syntactic-information
411@findex show-syntactic-information (c-)
3a731e1f
MS
412We can use the command @kbd{C-c C-s} (bound to
413@code{c-show-syntactic-information}) to simply report what the
6bf7aab6 414syntactic analysis is for the current line. Running this command on
3a731e1f 415line 4 of this example, we'd see in the echo area@footnote{With a
c3a2e2d5 416universal argument (i.e., @kbd{C-u C-c C-s}) the analysis is inserted
3a731e1f 417into the buffer as a comment on the current line.}:
6bf7aab6 418
3a731e1f
MS
419@example
420((statement 35))
6bf7aab6
DL
421@end example
422
423This tells us that the line is a statement and it is indented relative
424to buffer position 35, which happens to be the @samp{i} in @code{int} on
425line 3. If you were to move point to line 3 and hit @kbd{C-c C-s}, you
426would see:
6bf7aab6 427
3a731e1f
MS
428@example
429((defun-block-intro 29))
6bf7aab6
DL
430@end example
431
432This indicates that the @samp{int} line is the first statement in a top
433level function block, and is indented relative to buffer position 29,
434which is the brace just after the function header.
435
436Here's another example:
6bf7aab6 437
3a731e1f
MS
438@example
439 1: int add( int val, int incr, int doit )
440 2: @{
441 3: if( doit )
442 4: @{
443 5: return( val + incr );
444 6: @}
445 7: return( val );
446 8: @}
6bf7aab6
DL
447@end example
448
449@noindent
450Hitting @kbd{C-c C-s} on line 4 gives us:
6bf7aab6 451
3a731e1f
MS
452@example
453((substatement-open 46))
6bf7aab6
DL
454@end example
455
456@cindex substatement
d7bd46ed 457@cindex substatement block
6bf7aab6
DL
458@noindent
459which tells us that this is a brace that @emph{opens} a substatement
460block. @footnote{A @dfn{substatement} is the line after a
461conditional statement, such as @code{if}, @code{else}, @code{while},
462@code{do}, @code{switch}, etc. A @dfn{substatement
463block} is a brace block following one of these conditional statements.}
464
465@cindex comment-only line
466Syntactic component lists can contain more than one component, and
467individual syntactic components need not have relative buffer positions.
468The most common example of this is a line that contains a @dfn{comment
469only line}.
6bf7aab6 470
3a731e1f
MS
471@example
472 1: void draw_list( List<Drawables>& drawables )
473 2: @{
474 3: // call the virtual draw() method on each element in list
475 4: for( int i=0; i < drawables.count(), ++i )
476 5: @{
477 6: drawables[i].draw();
478 7: @}
479 8: @}
6bf7aab6
DL
480@end example
481
482@noindent
483Hitting @kbd{C-c C-s} on line 3 of this example gives:
6bf7aab6 484
3a731e1f
MS
485@example
486((comment-intro) (defun-block-intro 46))
6bf7aab6
DL
487@end example
488
489@noindent
490and you can see that the syntactic component list contains two syntactic
491components. Also notice that the first component,
492@samp{(comment-intro)} has no relative buffer position.
493
494
495@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3a731e1f 496@node Indentation Calculation, , Syntactic Analysis, Indentation Engine
d7bd46ed 497@comment node-name, next, previous, up
3a731e1f
MS
498@section Indentation Calculation
499@cindex indentation
6bf7aab6
DL
500@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
501
6bf7aab6 502Indentation for a line is calculated using the syntactic
d7bd46ed 503component list derived in step 1 above (@pxref{Syntactic Analysis}).
6bf7aab6
DL
504Each component contributes to the final total indentation of the line in
505two ways.
506
507First, the syntactic symbols are looked up in the @code{c-offsets-alist}
d7bd46ed
GM
508style variable, which is an association list of syntactic symbols and
509the offsets to apply for those symbols. These offsets are added to a
6bf7aab6
DL
510running total.
511
512Second, if the component has a relative buffer position, @ccmode{}
513adds the column number of that position to the running total. By adding
514up the offsets and columns for every syntactic component on the list,
515the final total indentation for the current line is computed.
516
517Let's use our two code examples above to see how this works. Here is
518our first example again:
6bf7aab6 519
3a731e1f
MS
520@example
521 1: void swap( int& a, int& b )
522 2: @{
523 3: int tmp = a;
524 4: a = b;
525 5: b = tmp;
526 6: @}
6bf7aab6
DL
527@end example
528
3a731e1f 529Let's say point is on line 3 and we hit the @kbd{TAB} key to reindent
6bf7aab6
DL
530the line. Remember that the syntactic component list for that
531line is:
6bf7aab6 532
3a731e1f
MS
533@example
534((defun-block-intro 29))
6bf7aab6
DL
535@end example
536
537@noindent
538@ccmode{} looks up @code{defun-block-intro} in the
d7bd46ed
GM
539@code{c-offsets-alist} style variable. Let's say it finds the value
540@samp{4}; it adds this to the running total (initialized to zero),
541yielding a running total indentation of 4 spaces.
6bf7aab6
DL
542
543Next @ccmode{} goes to buffer position 29 and asks for the current
544column. This brace is in column zero, so @ccmode{}
545adds @samp{0} to the running total. Since there is only one syntactic
546component on the list for this line, indentation calculation is
547complete, and the total indentation for the line
548is 4 spaces.
549
550Here's another example:
6bf7aab6 551
3a731e1f
MS
552@example
553 1: int add( int val, int incr, int doit )
554 2: @{
555 3: if( doit )
556 4: @{
557 5: return( val + incr );
558 6: @}
559 7: return( val );
560 8: @}
6bf7aab6
DL
561@end example
562
563If we were to hit @kbd{TAB} on line 4 in the above example, the same
564basic process is performed, despite the differences in the syntactic
565component list. Remember that the list for this line is:
6bf7aab6 566
3a731e1f
MS
567@example
568((substatement-open 46))
6bf7aab6
DL
569@end example
570
571Here, @ccmode{} first looks up the @code{substatement-open} symbol
572in @code{c-offsets-alist}. Let's say it finds the value @samp{4}. This
573yields a running total of 4. @ccmode{} then goes to
574buffer position 46, which is the @samp{i} in @code{if} on line 3. This
575character is in the fourth column on that line so adding this to the
576running total yields an indentation for the line of 8 spaces.
577
578Simple, huh?
579
580Actually, the mode usually just does The Right Thing without you having
581to think about it in this much detail. But when customizing
582indentation, it's helpful to understand the general indentation model
583being used.
584
6bf7aab6
DL
585As you configure @ccmode{}, you might want to set the variable
586@code{c-echo-syntactic-information-p} to non-@code{nil} so that the
587syntactic component list and calculated offset will always be echoed in
588the minibuffer when you hit @kbd{TAB}.
589
590
591@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3a731e1f 592@node Minor Modes, Text Filling and Line Breaking, Indentation Engine, Top
d7bd46ed 593@comment node-name, next, previous, up
3a731e1f 594@chapter Minor Modes
6bf7aab6
DL
595@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
596
597@ccmode{} contains two minor-mode-like features that you should
3a731e1f 598find useful while entering new C code. The first is called
6bf7aab6
DL
599@dfn{auto-newline} mode, and the second is called @dfn{hungry-delete}
600mode. These minor modes can be toggled on and off independently, and
601@ccmode{} can be configured so that it starts up with any
602combination of these minor modes. By default, both of these minor modes
603are turned off.
604
605The state of the minor modes is always reflected in the minor mode list
606on the modeline of the @ccmode{} buffer. When auto-newline mode is
3a731e1f
MS
607enabled, you will see @samp{C/a} on the mode line@footnote{The @samp{C}
608would be replaced with the name of the language in question for the
609other languages @ccmode{} supports.}. When hungry delete mode is
610enabled you will see @samp{C/h} and if both modes were enabled, you'd
611see @samp{C/ah}.
6bf7aab6
DL
612
613@kindex C-c C-a
614@kindex C-c C-d
615@kindex C-c C-t
616@findex c-toggle-hungry-state
617@findex c-toggle-auto-state
618@findex c-toggle-auto-hungry-state
619@findex toggle-hungry-state (c-)
620@findex toggle-auto-state (c-)
621@findex toggle-auto-hungry-state (c-)
47d7776c 622@ccmode{} provides key bindings which allow you to toggle the minor
6bf7aab6 623modes on the fly while editing code. To toggle just the auto-newline
3a731e1f
MS
624state, hit @kbd{C-c C-a} (bound to @code{c-toggle-auto-state}). When
625you do this, you should see the @samp{a} indicator either appear or
626disappear on the modeline. Similarly, to toggle just the
627hungry-delete state, use @kbd{C-c C-d} (@code{c-toggle-hungry-state}),
628and to toggle both states, use @kbd{C-c C-t}
629(@code{c-toggle-auto-hungry-state}).
6bf7aab6
DL
630
631To set up the auto-newline and hungry-delete states to your preferred
632values, you would need to add some lisp to your @file{.emacs} file that
633called one of the @code{c-toggle-*-state} functions directly. When
634called programmatically, each function takes a numeric value, where
635a positive number enables the minor mode, a negative number disables the
636mode, and zero toggles the current state of the mode.
637
638So for example, if you wanted to enable both auto-newline and
639hungry-delete for all your C file editing, you could add the following
640to your @file{.emacs} file:
6bf7aab6 641
3a731e1f 642@example
6bf7aab6 643(add-hook 'c-mode-common-hook
3a731e1f 644 (lambda () (c-toggle-auto-hungry-state 1)))
6bf7aab6
DL
645@end example
646
6bf7aab6 647@menu
d7bd46ed
GM
648* Auto-newline Insertion::
649* Hungry-deletion of Whitespace::
6bf7aab6
DL
650@end menu
651
6bf7aab6 652
d7bd46ed
GM
653@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
654@node Auto-newline Insertion, Hungry-deletion of Whitespace, , Minor Modes
655@comment node-name, next, previous, up
3a731e1f
MS
656@section Auto-newline Insertion
657@cindex auto-newline
6bf7aab6
DL
658@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
659
3a731e1f 660@cindex electric characters
6bf7aab6 661Auto-newline minor mode works by enabling certain @dfn{electric
3a731e1f
MS
662characters}. Special characters such as the left and right braces,
663colons, semicolons, etc., have been made electric to perform some
664magic formatting in addition to inserting the typed character. As a
665general rule, electric characters are only electric when the following
666conditions apply:
6bf7aab6
DL
667
668@itemize @bullet
669@item
670Auto-newline minor mode is enabled, as evidenced by a @samp{C/a} or
671@samp{C/ah} indicator on the modeline.
672
3a731e1f 673@item
6bf7aab6
DL
674@cindex literal
675@cindex syntactic whitespace
6bf7aab6 676The character was not typed inside of a literal @footnote{A
3a731e1f 677@dfn{literal} is defined as any comment, string, or preprocessor macro
6bf7aab6
DL
678definition. These constructs are also known as @dfn{syntactic
679whitespace} since they are usually ignored when scanning C code.}.
680
681@item
c3a2e2d5 682No numeric argument was supplied to the command (i.e., it was typed as
6bf7aab6 683normal, with no @kbd{C-u} prefix).
6bf7aab6
DL
684@end itemize
685
686@menu
687* Hanging Braces::
688* Hanging Colons::
3a731e1f 689* Hanging Semicolons and Commas::
d7bd46ed 690* Other Electric Commands::
6bf7aab6
DL
691* Clean-ups::
692@end menu
693
6bf7aab6 694
d7bd46ed
GM
695@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
696@node Hanging Braces, Hanging Colons, , Auto-newline Insertion
697@comment node-name, next, previous, up
698@subsection Hanging Braces
699@cindex hanging braces
6bf7aab6
DL
700@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
701
702@findex c-electric-brace
703@findex electric-brace (c-)
3a731e1f
MS
704@kindex @{
705@kindex @}
706
c3a2e2d5 707When you type either an open or close brace (i.e., @kbd{@{} or @kbd{@}}),
6bf7aab6
DL
708the electric command @code{c-electric-brace} gets run. This command has
709two electric formatting behaviors. First, it will perform some
3a731e1f 710reindentation of the line the brace was typed on, and second, it will
6bf7aab6 711add various newlines before and/or after the typed brace.
3a731e1f 712Reindentation occurs automatically whenever the electric behavior is
6bf7aab6 713enabled. If the brace ends up on a line other than the one it was typed
3a731e1f 714on, then that line is also reindented.
d7bd46ed
GM
715
716The default in auto-newline mode is to insert newlines both before and
717after a brace, but that can be controlled by the
3a731e1f
MS
718@code{c-hanging-braces-alist} style variable.
719
720@defopt c-hanging-braces-alist
721@vindex hanging-braces-alist (c-)
722
723This variable contains a mapping between syntactic symbols related to
724braces, and a list of places to insert a newline. The syntactic symbols
725that are useful for this list are @code{brace-list-intro},
726@code{statement-cont}, @code{inexpr-class-open},
727@code{inexpr-class-close}, and all the @code{*-open} and @code{*-close}
728symbols. @xref{Syntactic Symbols}, for a more detailed description of
729these syntactic symbols, except for @code{inexpr-class-open} and
730@code{inexpr-class-close}, which aren't actual syntactic symbols.
d7bd46ed
GM
731
732The braces of anonymous inner classes in Java are given the special
733symbols @code{inexpr-class-open} and @code{inexpr-class-close}, so that
734they can be distinguished from the braces of normal classes@footnote{The
3a731e1f 735braces of anonymous classes produce a combination of
d7bd46ed
GM
736@code{inexpr-class}, and @code{class-open} or @code{class-close} in
737normal indentation analysis.}.
738
3a731e1f
MS
739Note that the aggregate constructs in Pike mode, @samp{(@{}, @samp{@})},
740@samp{([}, @samp{])}, and @samp{(<}, @samp{>)}, do not count as brace
741lists in this regard, even though they do for normal indentation
742purposes. It's currently not possible to set automatic newlines on
743these constructs.
744
6bf7aab6 745The value associated with each syntactic symbol in this association list
3a731e1f 746is called an @var{action}, which can be either a function or a list.
393759c7 747@xref{Custom Brace and Colon Hanging}, for a more detailed discussion of
3a731e1f 748using a function as a brace hanging @var{action}.
6bf7aab6 749
3a731e1f 750When the @var{action} is a list, it can contain any combination of the
6bf7aab6
DL
751symbols @code{before} and @code{after}, directing @ccmode{} where to
752put newlines in relationship to the brace being inserted. Thus, if the
753list contains only the symbol @code{after}, then the brace is said to
754@dfn{hang} on the right side of the line, as in:
6bf7aab6 755
3a731e1f 756@example
6bf7aab6
DL
757// here, open braces always `hang'
758void spam( int i ) @{
759 if( i == 7 ) @{
760 dosomething(i);
761 @}
762@}
6bf7aab6
DL
763@end example
764
765When the list contains both @code{after} and @code{before}, the braces
766will appear on a line by themselves, as shown by the close braces in the
767above example. The list can also be empty, in which case no newlines
768are added either before or after the brace.
769
d7bd46ed
GM
770If a syntactic symbol is missing entirely from
771@code{c-hanging-braces-alist}, it's treated in the same way as an
3a731e1f 772@var{action} with a list containing @code{before} and @code{after}, so
d7bd46ed
GM
773that braces by default end up on their own line.
774
6bf7aab6 775For example, the default value of @code{c-hanging-braces-alist} is:
6bf7aab6 776
3a731e1f
MS
777@example
778((brace-list-open)
779 (brace-entry-open)
780 (statement-cont)
781 (substatement-open after)
782 (block-close . c-snug-do-while)
783 (extern-lang-open after)
784 (inexpr-class-open after)
785 (inexpr-class-close before))
6bf7aab6
DL
786@end example
787
3a731e1f
MS
788@noindent which says that @code{brace-list-open},
789@code{brace-entry-open} and @code{statement-cont}@footnote{Brace lists
790inside statements, such as initializers for static array variables
791inside functions in C, are recognized as @code{statement-cont}. All
792normal substatement blocks are recognized with other symbols.} braces
793should both hang on the right side and allow subsequent text to follow
794on the same line as the brace. Also, @code{substatement-open},
795@code{extern-lang-open}, and @code{inexpr-class-open} braces should hang
796on the right side, but subsequent text should follow on the next line.
797The opposite holds for @code{inexpr-class-close} braces; they won't
798hang, but the following text continues on the same line. Here, in the
799@code{block-close} entry, you also see an example of using a function as
800an @var{action}. In all other cases, braces are put on a line by
801themselves.
802@end defopt
6bf7aab6
DL
803
804
805@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3a731e1f 806@node Hanging Colons, Hanging Semicolons and Commas, Hanging Braces, Auto-newline Insertion
d7bd46ed
GM
807@comment node-name, next, previous, up
808@subsection Hanging Colons
809@cindex hanging colons
6bf7aab6
DL
810@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
811
d7bd46ed
GM
812Using a mechanism similar to brace hanging (@pxref{Hanging Braces}),
813colons can also be made to hang using the style variable
3a731e1f
MS
814@code{c-hanging-colons-alist}.
815
816@defopt c-hanging-colons-alist
817@vindex hanging-colons-alist (c-)
818
819The syntactic symbols appropriate for this association list are:
820@code{case-label}, @code{label}, @code{access-label},
821@code{member-init-intro}, and @code{inher-intro}. Note however that for
822@code{c-hanging-colons-alist}, @var{action}s as functions are not
823supported. See also @ref{Custom Brace and Colon Hanging} for details.
6bf7aab6 824
6bf7aab6
DL
825In C++, double-colons are used as a scope operator but because these
826colons always appear right next to each other, newlines before and after
827them are controlled by a different mechanism, called @dfn{clean-ups} in
393759c7 828@ccmode{}. @xref{Clean-ups}, for details.
3a731e1f 829@end defopt
6bf7aab6
DL
830
831
832@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3a731e1f 833@node Hanging Semicolons and Commas, Other Electric Commands, Hanging Colons, Auto-newline Insertion
d7bd46ed 834@comment node-name, next, previous, up
3a731e1f
MS
835@subsection Hanging Semicolons and Commas
836@cindex hanging semicolons
d7bd46ed 837@cindex hanging commas
6bf7aab6
DL
838@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
839
840Semicolons and commas are also electric in @ccmode{}, but since
841these characters do not correspond directly to syntactic symbols, a
842different mechanism is used to determine whether newlines should be
843automatically inserted after these characters. @xref{Customizing
3a731e1f 844Semicolons and Commas}, for details.
6bf7aab6
DL
845
846
847@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3a731e1f 848@node Other Electric Commands, Clean-ups, Hanging Semicolons and Commas, Auto-newline Insertion
d7bd46ed
GM
849@comment node-name, next, previous, up
850@subsection Other Electric Commands
6bf7aab6
DL
851@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
852
3a731e1f
MS
853A few other keys also provide electric behavior, often only to reindent
854the line. Common to all of them is that they only reindent if used in
855normal code (as opposed to in a string literal or comment), and
856@code{c-syntactic-indentation} isn't @code{nil}. They are:
857
858@table @kbd
859@item #
6bf7aab6
DL
860@kindex #
861@findex c-electric-pound
6bf7aab6 862@findex electric-pound (c-)
3a731e1f 863@vindex c-electric-pound-behavior
6bf7aab6 864@vindex electric-pound-behavior (c-)
3a731e1f
MS
865Pound (bound to @code{c-electric-pound}) is electric when typed as the
866first non-whitespace character on a line and not within a macro
867definition. In this case, the variable @code{c-electric-pound-behavior}
868is consulted for the electric behavior. This variable takes a list
869value, although the only element currently defined is @code{alignleft},
870which tells this command to force the @samp{#} character into column
871zero. This is useful for entering preprocessor macro definitions.
872
873Pound is not electric in AWK buffers, where @samp{#} starts a comment,
874and is bound to @code{self-insert-command} like any typical printable
875character.
6bf7aab6 876
3a731e1f
MS
877@item *
878@kindex *
879@itemx /
880@kindex /
6bf7aab6 881@findex c-electric-star
6bf7aab6 882@findex electric-star (c-)
3a731e1f 883@findex c-electric-slash
6bf7aab6 884@findex electric-slash (c-)
3a731e1f
MS
885Stars and slashes (bound to @code{c-electric-star} and
886@code{c-electric-slash} respectively) are also electric under certain
887circumstances. If a @samp{*} is inserted as the second character of a C
888style block comment on a comment-only line, then the comment delimiter
889is indented as defined by @code{c-offsets-alist}. A comment-only line
890is defined as a line which contains only a comment, as in:
891
6bf7aab6
DL
892@example
893@group
177c0ea7 894void spam( int i )
6bf7aab6 895@{
3a731e1f
MS
896 // this is a comment-only line...
897 if( i == 7 ) // but this is not
6bf7aab6
DL
898 @{
899 dosomething(i);
900 @}
901@}
6bf7aab6
DL
902@end group
903@end example
904
3a731e1f
MS
905Likewise, if a @samp{/} is inserted as the second slash in a C++ style
906line comment (also only on a comment-only line), then the line is
907indented as defined by @code{c-offsets-alist}.
6bf7aab6 908
3a731e1f
MS
909In AWK mode, @samp{*} and @samp{/} do not delimit comments and are
910bound to @code{self-insert-command}.
911
912@item <
6bf7aab6 913@kindex <
3a731e1f 914@itemx >
6bf7aab6 915@kindex >
3a731e1f
MS
916@findex c-electric-lt-gt
917@findex electric-lt-gt (c-)
918Less-than and greater-than signs (bound to @code{c-electric-lt-gt}) are
6bf7aab6 919electric, but only in C++ mode. Hitting the second of two @kbd{<} or
3a731e1f 920@kbd{>} keys reindents the line if it is a C++ style stream operator.
6bf7aab6 921
3a731e1f 922@item (
d7bd46ed 923@kindex (
3a731e1f 924@itemx )
d7bd46ed 925@kindex )
3a731e1f
MS
926@findex c-electric-paren
927@findex electric-paren (c-)
928The normal parenthesis characters @samp{(} and @samp{)} reindent the
929current line. This is useful for getting the closing parenthesis of an
930argument list aligned automatically.
931@end table
932
933@deffn Command c-electric-continued-statement
934@findex electric-continued-statement (c-)
935
936Certain keywords, depending on language, are electric to cause
937reindentation when they are preceded only by whitespace on the line.
938The keywords are those that continue an earlier statement instead of
939starting a new one: @code{else}, @code{while}, @code{catch} (only in C++
940and Java) and @code{finally} (only in Java).
941
942An example:
943
944@example
945@group
946for (i = 0; i < 17; i++)
947 if (a[i])
948 res += a[i]->offset;
949else
950@end group
951@end example
952
953Here, the @code{else} should be indented like the preceding @code{if},
954since it continues that statement. @ccmode{} will automatically reindent
955it after the @code{else} has been typed in full, since it's not until
956then it's possible to decide whether it's a new statement or a
957continuation of the preceding @code{if}.
958
959@vindex abbrev-mode
960@findex abbrev-mode
961@cindex Abbrev mode
962@ccmode{} uses Abbrev mode (@pxref{Abbrevs,,, emacs, The Emacs Editor})
963to accomplish this. It's therefore turned on by default in all language
964modes except IDL mode, since CORBA IDL doesn't have any statements.
965@end deffn
6bf7aab6 966
6bf7aab6 967
d7bd46ed
GM
968@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
969@node Clean-ups, , Other Electric Commands, Auto-newline Insertion
970@comment node-name, next, previous, up
971@subsection Clean-ups
972@cindex clean-ups
6bf7aab6
DL
973@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
974
cb7f2e96
GM
975@dfn{Clean-ups} are mechanisms complementary to colon and brace hanging.
976On the surface, it would seem that clean-ups overlap the functionality
977provided by the @code{c-hanging-*-alist} variables. Clean-ups are
c3a2e2d5 978however used to adjust code ``after-the-fact,'' i.e., to adjust the
cb7f2e96
GM
979whitespace in constructs after they are typed.
980
981Most of the clean-ups are only applicable to counteract automatically
982inserted newlines, and will therefore only have any effect if the
983auto-newline minor mode is turned on. Others will work all the time.
6bf7aab6 984
3a731e1f 985@defopt c-cleanup-list
d7bd46ed 986@vindex cleanup-list (c-)
6bf7aab6 987@cindex literal
3a731e1f 988
d7bd46ed 989You can configure @ccmode{}'s clean-ups by setting the style variable
6bf7aab6 990@code{c-cleanup-list}, which is a list of clean-up symbols. By default,
d7bd46ed
GM
991@ccmode{} cleans up only the @code{scope-operator} construct, which is
992necessary for proper C++ support. Note that clean-ups are only
993performed when the construct does not occur within a literal
994(@pxref{Auto-newline Insertion}), and when there is nothing but
995whitespace appearing between the individual components of the construct.
3a731e1f 996@end defopt
6bf7aab6 997
3a731e1f 998These are the clean-ups that are only active in the auto-newline minor
cb7f2e96 999mode:
6bf7aab6 1000
3a731e1f
MS
1001@c TBD: Would like to use some sort of @deffoo here; @table indents a
1002@c bit too much in dvi output.
1003@table @code
1004@item brace-else-brace
1005Clean up @samp{@} else @{} constructs by placing the entire construct on
1006a single line. Clean-up occurs when the open brace after the
1007@samp{else} is typed. So for example, this:
1008
6bf7aab6
DL
1009@example
1010@group
6bf7aab6
DL
1011void spam(int i)
1012@{
3a731e1f 1013 if( i==7 ) @{
6bf7aab6
DL
1014 dosomething();
1015 @}
1016 else
1017 @{
6bf7aab6
DL
1018@end group
1019@end example
3a731e1f 1020
6bf7aab6 1021@noindent
3a731e1f
MS
1022appears like this after the last open brace is typed:
1023
6bf7aab6
DL
1024@example
1025@group
6bf7aab6
DL
1026void spam(int i)
1027@{
1028 if( i==7 ) @{
1029 dosomething();
1030 @} else @{
6bf7aab6
DL
1031@end group
1032@end example
1033
3a731e1f
MS
1034@item brace-elseif-brace
1035Similar to the @code{brace-else-brace} clean-up, but this cleans up
1036@samp{@} else if (...) @{} constructs. For example:
1037
6bf7aab6
DL
1038@example
1039@group
6bf7aab6
DL
1040void spam(int i)
1041@{
3a731e1f 1042 if( i==7 ) @{
6bf7aab6
DL
1043 dosomething();
1044 @}
d7bd46ed
GM
1045 else if( i==3 )
1046 @{
6bf7aab6
DL
1047@end group
1048@end example
3a731e1f 1049
6bf7aab6 1050@noindent
3a731e1f
MS
1051appears like this after the last open parenthesis is typed:
1052
d7bd46ed
GM
1053@example
1054@group
d7bd46ed
GM
1055void spam(int i)
1056@{
1057 if( i==7 ) @{
1058 dosomething();
1059 @} else if( i==3 )
1060 @{
d7bd46ed
GM
1061@end group
1062@end example
3a731e1f 1063
d7bd46ed 1064@noindent
3a731e1f
MS
1065and like this after the last open brace is typed:
1066
6bf7aab6
DL
1067@example
1068@group
6bf7aab6
DL
1069void spam(int i)
1070@{
1071 if( i==7 ) @{
1072 dosomething();
1073 @} else if( i==3 ) @{
6bf7aab6
DL
1074@end group
1075@end example
1076
3a731e1f
MS
1077@item brace-catch-brace
1078Analogous to @code{brace-elseif-brace}, but cleans up @samp{@} catch
1079(...) @{} in C++ and Java mode.
1080
1081@item empty-defun-braces
1082Clean up braces following a top-level function or class definition that
1083contains no body. Clean up occurs when the closing brace is typed.
1084Thus the following:
d7bd46ed 1085
6bf7aab6
DL
1086@example
1087@group
6bf7aab6
DL
1088class Spam
1089@{
1090@}
6bf7aab6
DL
1091@end group
1092@end example
3a731e1f 1093
6bf7aab6
DL
1094@noindent
1095is transformed into this when the close brace is typed:
3a731e1f 1096
6bf7aab6
DL
1097@example
1098@group
6bf7aab6
DL
1099class Spam
1100@{@}
6bf7aab6
DL
1101@end group
1102@end example
1103
3a731e1f
MS
1104@item defun-close-semi
1105Clean up the terminating semicolon on top-level function or class
1106definitions when they follow a close brace. Clean up occurs when the
1107semicolon is typed. So for example, the following:
1108
6bf7aab6
DL
1109@example
1110@group
6bf7aab6
DL
1111class Spam
1112@{
1113@}
1114;
6bf7aab6
DL
1115@end group
1116@end example
3a731e1f 1117
6bf7aab6 1118@noindent
3a731e1f 1119is transformed into this when the semicolon is typed:
6bf7aab6
DL
1120
1121@example
1122@group
6bf7aab6
DL
1123class Spam
1124@{
1125@};
6bf7aab6
DL
1126@end group
1127@end example
1128
3a731e1f
MS
1129@item list-close-comma
1130Clean up commas following braces in array and aggregate initializers.
1131Clean up occurs when the comma is typed.
1132
1133@item scope-operator
1134Clean up double colons which may designate a C++ scope operator split
1135across multiple lines@footnote{Certain C++ constructs introduce
1136ambiguous situations, so @code{scope-operator} clean-ups may not always
1137be correct. This usually only occurs when scoped identifiers appear in
1138switch label tags.}. Clean up occurs when the second colon is typed.
1139You will always want @code{scope-operator} in the @code{c-cleanup-list}
1140when you are editing C++ code.
1141@end table
6bf7aab6 1142
cb7f2e96
GM
1143The following clean-ups are always active when they occur on
1144@code{c-cleanup-list}, and are thus not affected by the auto-newline
1145minor mode:
1146
3a731e1f
MS
1147@table @code
1148@item space-before-funcall
1149Insert a space between the function name and the opening parenthesis of
1150a function call. This produces function calls in the style mandated by
c3a2e2d5 1151the GNU coding standards, e.g., @samp{signal (SIGINT, SIG_IGN)} and
3a731e1f
MS
1152@samp{abort ()}. Clean up occurs when the opening parenthesis is typed.
1153
1154@item compact-empty-funcall
1155Clean up any space between the function name and the opening parenthesis
1156of a function call that has no arguments. This is typically used
1157together with @code{space-before-funcall} if you prefer the GNU function
1158call style for functions with arguments but think it looks ugly when
c3a2e2d5 1159it's only an empty parenthesis pair. I.e., you will get @samp{signal
3a731e1f
MS
1160(SIGINT, SIG_IGN)}, but @samp{abort()}. Clean up occurs when the
1161closing parenthesis is typed.
1162@end table
cb7f2e96 1163
6bf7aab6
DL
1164
1165@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
d7bd46ed
GM
1166@node Hungry-deletion of Whitespace, , Auto-newline Insertion, Minor Modes
1167@comment node-name, next, previous, up
3a731e1f
MS
1168@section Hungry-deletion of Whitespace
1169@cindex hungry-deletion
6bf7aab6
DL
1170@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1171
1172Hungry deletion of whitespace, or as it more commonly called,
1173@dfn{hungry-delete mode}, is a simple feature that some people find
1174extremely useful. In fact, you might find yourself wanting
1175hungry-delete in @strong{all} your editing modes!
1176
1177@kindex DEL
3a731e1f
MS
1178@kindex C-d
1179
1180In a nutshell, when hungry-delete mode is enabled, hitting the @kbd{DEL}
1181or @kbd{C-d} keys will consume all preceding or following whitespace,
1182including newlines and tabs. This can really cut down on the number of
1183times you have to hit these keys if, for example, you made a mistake on
6bf7aab6
DL
1184the preceding line.
1185
3a731e1f 1186@deffn Command c-electric-backspace
6bf7aab6 1187@findex electric-backspace (c-)
3a731e1f
MS
1188This command is run by default when you hit the @kbd{DEL} key. It
1189deletes any amount of whitespace in the backwards direction if
1190hungry-delete mode is enabled. When it's disabled, or when used with
1191a prefix argument or in a literal (@pxref{Auto-newline Insertion}),
1192the function contained in the @code{c-backspace-function} variable is
1193called with the prefix argument.
1194@end deffn
1195
1196@defvar c-backspace-function
6bf7aab6 1197@vindex backspace-function (c-)
3a731e1f
MS
1198@findex backward-delete-char-untabify
1199Hook that gets called by @code{c-electric-backspace} when it doesn't
1200do an ``electric'' deletion of the preceding whitespace. The default
1201value is @code{backward-delete-char-untabify}.
1202@end defvar
1203
1204@deffn Command c-electric-delete-forward
1205@findex electric-delete-forward (c-)
1206This function, which is bound to @kbd{C-d} by default, works just like
1207@code{c-electric-backspace} but in the forward direction. When it
1208doesn't do an ``electric'' deletion of the following whitespace, it
1209calls the function in @code{c-delete-function} with its prefix
1210argument.
1211@end deffn
1212
1213@defvar c-delete-function
1214@vindex delete-function (c-)
1215@findex delete-char
1216Hook that gets called by @code{c-electric-delete-forward} when it
1217doesn't do an ``electric'' deletion of the following whitespace. The
1218default value is @code{delete-char}.
1219@end defvar
1220
1221Above we have only talked about the @kbd{DEL} and @kbd{C-d} key events,
1222without connecting them to the physical keys commonly known as
1223@key{Backspace} and @key{Delete}. The default behavior of those two
1224depends on the flavor of (X)Emacs you are using.
6bf7aab6
DL
1225
1226@findex c-electric-delete
1227@findex electric-delete (c-)
3a731e1f 1228@vindex delete-key-deletes-forward
6bf7aab6 1229
3a731e1f
MS
1230In XEmacs 20.3 and beyond, the @key{Backspace} key is bound to
1231@code{c-electric-backspace} and the @key{Delete} key is bound to
1232@code{c-electric-delete}. You control the direction it deletes in by
1233setting the variable @code{delete-key-deletes-forward}, a standard
1234XEmacs variable. When this variable is non-@code{nil},
1235@code{c-electric-delete} will do forward deletion with
1236@code{c-electric-delete-forward}, otherwise it does backward deletion
1237with @code{c-electric-backspace}.
1238
1239In other Emacs versions, @ccmode{} doesn't bind either @key{Backspace}
1240or @key{Delete}. In XEmacs 19 and Emacs prior to 21 that means that
1241it's up to you to fix them. Emacs 21 automatically binds them as
1242appropriate to @kbd{DEL} and @kbd{C-d}.
1243
1244Another way to use hungry deletion is to bind
1245@code{c-hungry-backspace} and @code{c-hungry-delete-forward} directly
1246to keys, and not use the mode toggling. For example @kbd{C-c C-d} and
1247@kbd{C-c DEL} to match plain @kbd{C-d} and @kbd{DEL},
6bf7aab6 1248
3a731e1f
MS
1249@example
1250(add-hook
1251 'c-mode-common-hook
1252 (lambda ()
1253 (define-key c-mode-base-map
1254 [?\C-c ?\d] 'c-hungry-backspace)
1255 (define-key c-mode-base-map
1256 [?\C-c ?\C-d] 'c-hungry-delete-forward)))
1257@end example
6bf7aab6 1258
3a731e1f
MS
1259@deffn Command c-hungry-backspace
1260@findex hungry-backspace (c-)
1261Delete any amount of whitespace in the backwards direction (regardless
1262whether hungry-delete mode is enabled or not).
1263@end deffn
6bf7aab6 1264
3a731e1f
MS
1265@deffn Command c-hungry-delete-forward
1266@findex hungry-delete-forward (c-)
1267Delete any amount of whitespace in the forward direction (regardless
1268whether hungry-delete mode is enabled or not).
1269@end deffn
6bf7aab6
DL
1270
1271
1272@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3a731e1f 1273@node Text Filling and Line Breaking, Macro Handling, Minor Modes, Top
d7bd46ed 1274@comment node-name, next, previous, up
3a731e1f
MS
1275@chapter Text Filling and Line Breaking
1276@cindex text filling
1277@cindex line breaking
1278@cindex comment handling
6bf7aab6
DL
1279@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1280
d7bd46ed
GM
1281Since there's a lot of normal text in comments and string literals,
1282@ccmode{} provides features to edit these like in text mode. The goal
c3a2e2d5 1283is to do it as seamlessly as possible, i.e., you can use auto fill mode,
d7bd46ed
GM
1284sentence and paragraph movement, paragraph filling, adaptive filling etc
1285wherever there's a piece of normal text without having to think much
1286about it. @ccmode{} should keep the indentation, fix the comment line
1287decorations, and so on, for you. It does that by hooking in on the
1288different line breaking functions and tuning relevant variables as
1289necessary.
1290
1291@vindex c-comment-prefix-regexp
1292@vindex comment-prefix-regexp (c-)
1293@cindex comment line prefix
1294@vindex comment-start
1295@vindex comment-end
1296@vindex comment-start-skip
1297@vindex paragraph-start
1298@vindex paragraph-separate
1299@vindex paragraph-ignore-fill-prefix
1300@vindex adaptive-fill-mode
1301@vindex adaptive-fill-regexp
1302@vindex adaptive-fill-first-line-regexp
1303To make Emacs recognize comments and treat text in them as normal
1304paragraphs, @ccmode{} makes several standard
1305variables@footnote{@code{comment-start}, @code{comment-end},
1306@code{comment-start-skip}, @code{paragraph-start},
1307@code{paragraph-separate}, @code{paragraph-ignore-fill-prefix},
1308@code{adaptive-fill-mode}, @code{adaptive-fill-regexp}, and
1309@code{adaptive-fill-first-line-regexp}.} buffer local and modifies them
3a731e1f
MS
1310according to the language syntax and the comment line prefix.
1311
1312@defopt c-comment-prefix-regexp
1313@vindex comment-prefix-regexp (c-)
1314This style variable contains the regexp used to recognize the
1315@dfn{comment line prefix}, which is the line decoration that starts
1316every line in a comment. The default is @samp{//+\\|\\**}, which
d7bd46ed 1317matches C++ style line comments like
d7bd46ed 1318
3a731e1f 1319@example
d7bd46ed 1320// blah blah
d7bd46ed
GM
1321@end example
1322
1323@noindent
1324with two or more slashes in front of them, and C style block comments
1325like
3a731e1f 1326
d7bd46ed
GM
1327@example
1328@group
d7bd46ed
GM
1329/*
1330 * blah blah
1331 */
d7bd46ed
GM
1332@end group
1333@end example
1334
1335@noindent
1336with zero or more stars at the beginning of every line. If you change
3a731e1f 1337this variable, please make sure it still matches the comment starter
c3a2e2d5 1338(i.e., @code{//}) of line comments @emph{and} the line prefix inside
3a731e1f
MS
1339block comments.
1340
1341@findex c-setup-paragraph-variables
1342@findex setup-paragraph-variables (c-)
1343Also note that since @ccmode{} uses the value of
d7bd46ed 1344@code{c-comment-prefix-regexp} to set up several other variables at mode
2a15eb73 1345initialization, there won't be any effect if you just change it inside a
3a731e1f 1346@ccmode{} buffer. You need to call the command
2a15eb73 1347@code{c-setup-paragraph-variables} too, to update those other variables with
3a731e1f
MS
1348the new value. That's also the case if you modify this variable in a
1349mode hook, since @ccmode{} sets up all variables before calling them.
1350@end defopt
d7bd46ed
GM
1351
1352@findex auto-fill-mode
3a731e1f
MS
1353@cindex Auto Fill mode
1354@cindex paragraph filling
d7bd46ed
GM
1355Line breaks are by default handled (almost) the same regardless whether
1356they are made by auto fill mode (@pxref{Auto Fill,,, emacs, The Emacs
c3a2e2d5 1357Editor}), paragraph filling (e.g., with @kbd{M-q}), or explicitly with
d7bd46ed
GM
1358@kbd{M-j} or similar methods. In string literals, the new line gets the
1359same indentation as the previous nonempty line (may be changed with the
1360@code{string} syntactic symbol). In comments, @ccmode{} uses
1361@code{c-comment-prefix-regexp} to adapt the line prefix from the other
1362lines in the comment.
1363
1364@vindex adaptive-fill-mode
3a731e1f 1365@cindex Adaptive Fill mode
d7bd46ed
GM
1366@ccmode{} uses adaptive fill mode (@pxref{Adaptive Fill,,, emacs, The
1367Emacs Editor}) to make Emacs correctly keep the line prefix when filling
1368paragraphs. That also makes Emacs preserve the text indentation
c3a2e2d5 1369@emph{inside} the comment line prefix. e.g., in the following comment,
3a731e1f
MS
1370both paragraphs will be filled with the left margins of the texts kept
1371intact:
1372
d7bd46ed
GM
1373@example
1374@group
d7bd46ed
GM
1375/* Make a balanced b-tree of the nodes in the incoming
1376 * stream. But, to quote the famous words of Donald E.
1377 * Knuth,
1378 *
1379 * Beware of bugs in the above code; I have only
1380 * proved it correct, not tried it.
1381 */
d7bd46ed
GM
1382@end group
1383@end example
1384
1385@findex c-setup-filladapt
1386@findex setup-filladapt (c-)
1387@findex filladapt-mode
1388@vindex filladapt-mode
1389@cindex Filladapt mode
1390It's also possible to use other adaptive filling packages, notably Kyle
1391E. Jones' Filladapt package@footnote{It's available from
1392@uref{http://www.wonderworks.com/}. As of version 2.12, it does however
1393lack a feature that makes it work suboptimally when
1394@code{c-comment-prefix-regexp} matches the empty string (which it does
1395by default). A patch for that is available from
3a731e1f 1396@uref{http://cc-mode.sourceforge.net/,, the CC Mode web site}.},
d7bd46ed
GM
1397which handles things like bulleted lists nicely. There's a convenience
1398function @code{c-setup-filladapt} that tunes the relevant variables in
c3a2e2d5 1399Filladapt for use in @ccmode{}. Call it from a mode hook, e.g., with
d7bd46ed 1400something like this in your @file{.emacs}:
d7bd46ed 1401
3a731e1f 1402@example
d7bd46ed
GM
1403(defun my-c-mode-common-hook ()
1404 (c-setup-filladapt)
1405 (filladapt-mode 1))
1406(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
d7bd46ed 1407@end example
6bf7aab6 1408
3a731e1f 1409@defopt c-block-comment-prefix
d7bd46ed 1410@vindex block-comment-prefix (c-)
6bf7aab6
DL
1411@vindex c-comment-continuation-stars
1412@vindex comment-continuation-stars (c-)
d7bd46ed
GM
1413Normally the comment line prefix inserted for a new line inside a
1414comment is deduced from other lines in it. However there's one
3a731e1f
MS
1415situation when there's no hint about what the prefix should look like,
1416namely when a block comment is broken for the first time. This style
1417variable@footnote{In versions before 5.26, this variable was called
1418@code{c-comment-continuation-stars}. As a compatibility measure,
1419@ccmode{} still uses the value on that variable if it's set.} is used
2a15eb73
MS
1420then as the comment prefix. It defaults to @samp{*
1421}@footnote{Actually, this default setting of
1422@code{c-block-comment-prefix} typically gets overriden by the default
1423style @code{gnu}, which sets it to blank. You can see the line
1424splitting effect described here by setting a different style,
c1386334 1425e.g. @code{k&r} @xref{Choosing a Style}.}, which makes a comment
6bf7aab6 1426
3a731e1f 1427@example
d7bd46ed 1428/* Got O(n^2) here, which is a Bad Thing. */
d7bd46ed 1429@end example
6bf7aab6 1430
d7bd46ed
GM
1431@noindent
1432break into
3a731e1f 1433
d7bd46ed
GM
1434@example
1435@group
d7bd46ed
GM
1436/* Got O(n^2) here,
1437 * which is a Bad Thing. */
d7bd46ed
GM
1438@end group
1439@end example
1440
3a731e1f
MS
1441Note that it won't work to adjust the indentation by putting leading
1442spaces in @code{c-block-comment-prefix}, since @ccmode{} still uses the
1443normal indentation engine to indent the line. Thus, the right way to
1444fix the indentation is by customizing the @code{c} syntactic symbol. It
1445defaults to @code{c-lineup-C-comments}, which handles the indentation of
1446most common comment styles, see @ref{Indentation Functions}.
1447@end defopt
d7bd46ed 1448
3a731e1f 1449@defopt c-ignore-auto-fill
d7bd46ed
GM
1450@vindex ignore-auto-fill (c-)
1451When auto fill mode is enabled, @ccmode{} can selectively ignore it
c3a2e2d5 1452depending on the context the line break would occur in, e.g., to never
3a731e1f
MS
1453break a line automatically inside a string literal. This variable
1454takes a list of symbols for the different contexts where auto-filling
1455never should occur:
d7bd46ed 1456
3a731e1f
MS
1457@table @code
1458@item string
1459Inside a string or character literal.
1460@item c
1461Inside a C style block comment.
1462@item c++
1463Inside a C++ style line comment.
1464@item cpp
1465Inside a preprocessor directive.
1466@item code
c3a2e2d5 1467Anywhere else, i.e., in normal code.
3a731e1f 1468@end table
d7bd46ed
GM
1469
1470By default, @code{c-ignore-auto-fill} is set to @code{'(string cpp
1471code)}, which means that auto-filling only occurs in comments when
1472auto-fill mode is activated. In literals, it's often desirable to have
1473explicit control over newlines. In preprocessor directives, the
1474necessary @samp{\} escape character before the newline is not
1475automatically inserted, so an automatic line break would produce invalid
1476code. In normal code, line breaks are normally dictated by some logical
1477structure in the code rather than the last whitespace character, so
1478automatic line breaks there will produce poor results in the current
1479implementation.
3a731e1f 1480@end defopt
d7bd46ed 1481
3a731e1f 1482The commands that do the actual work follow.
d7bd46ed
GM
1483
1484@table @asis
3a731e1f 1485@item @kbd{M-q} (@code{c-fill-paragraph})
d7bd46ed
GM
1486@kindex M-q
1487@findex c-fill-paragraph
1488@findex fill-paragraph (c-)
1489@cindex Javadoc markup
cb7f2e96 1490@cindex Pike autodoc markup
d7bd46ed
GM
1491This is the replacement for @code{fill-paragraph} in @ccmode{}
1492buffers. It's used to fill multiline string literals and both block and
1493line style comments. In Java buffers, the Javadoc markup words are
cb7f2e96
GM
1494recognized as paragraph starters. The line oriented Pike autodoc markup
1495words are recognized in the same way in Pike mode.
d7bd46ed
GM
1496
1497The function keeps the comment starters and enders of block comments as
1498they were before the filling. This means that a comment ender on the
1499same line as the paragraph being filled will be filled with the
1500paragraph, and one on a line by itself will stay as it is. The comment
1501starter is handled similarly@footnote{This means that the variables
1502@code{c-hanging-comment-starter-p} and @code{c-hanging-comment-ender-p},
1503which controlled this behavior in earlier versions of @ccmode{}, are now
1504obsolete.}.
1505
3a731e1f 1506@item @kbd{M-j} (@code{c-indent-new-comment-line})
d7bd46ed
GM
1507@kindex M-j
1508@findex c-indent-new-comment-line
1509@findex indent-new-comment-line (c-)
d7bd46ed
GM
1510This is the replacement for @code{indent-new-comment-line}. It breaks
1511the line at point and indents the new line like the current one.
1512
1513@vindex comment-multi-line
1514If inside a comment and @code{comment-multi-line} is non-@code{nil}, the
1515indentation and line prefix are preserved. If inside a comment and
1516@code{comment-multi-line} is @code{nil}, a new comment of the same type
1517is started on the next line and indented as appropriate for comments.
1518
3a731e1f
MS
1519Note that @ccmode{} sets @code{comment-multi-line} to @code{t} at
1520startup. The reason is that @kbd{M-j} could otherwise produce sequences
1521of single line block comments for texts that should logically be treated
1522as one comment, and the rest of the paragraph handling code
c3a2e2d5 1523(e.g., @kbd{M-q} and @kbd{M-a}) can't cope with that, which would lead to
3a731e1f
MS
1524inconsistent behavior.
1525
1526@item @kbd{M-x c-context-line-break}
d7bd46ed
GM
1527@findex c-context-line-break
1528@findex context-line-break (c-)
d7bd46ed
GM
1529This is a function that works like @code{indent-new-comment-line} in
1530comments and @code{newline-and-indent} elsewhere, thus combining those
1531two in a way that uses each one in the context it's best suited for.
c3a2e2d5 1532I.e., in comments the comment line prefix and indentation is kept for
3a731e1f
MS
1533the new line, and in normal code it's indented according to context by
1534the indentation engine.
1535
1536In macros it acts like @code{newline-and-indent} but additionally
1537inserts and optionally aligns the line ending backslash so that the
1538macro remains unbroken. @xref{Macro Handling}, for details about the
1539backslash alignment.
d7bd46ed
GM
1540
1541It's not bound to a key by default, but it's intended to be used on the
1542@kbd{RET} key. If you like the behavior of @code{newline-and-indent} on
3a731e1f
MS
1543@kbd{RET}, you should consider switching to this function.
1544
1545@item @kbd{M-x c-context-open-line}
1546@findex c-context-open-line
1547@findex context-open-line (c-)
1548This is to @kbd{C-o} (@kbd{M-x open-line}) as
c3a2e2d5 1549@code{c-context-line-break} is to @kbd{RET}. I.e., it works just like
3a731e1f
MS
1550@code{c-context-line-break} but leaves the point before the inserted
1551line break.
d7bd46ed 1552@end table
6bf7aab6 1553
d7bd46ed
GM
1554
1555@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3a731e1f 1556@node Macro Handling, Font Locking, Text Filling and Line Breaking, Top
d7bd46ed 1557@comment node-name, next, previous, up
3a731e1f
MS
1558@chapter Macro Handling
1559@cindex macros
1560@cindex preprocessor directives
6bf7aab6
DL
1561@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1562
3a731e1f 1563Preprocessor directives are handled as syntactic whitespace from other
c3a2e2d5 1564code, i.e., they can be interspersed anywhere without affecting the
3a731e1f 1565syntactic analysis, just like comments.
6bf7aab6 1566
3a731e1f
MS
1567The code inside macro definitions is still analyzed syntactically so
1568that you get relative indentation there just as you'd get if the same
1569code was outside a macro. However, since there is no hint about the
c3a2e2d5 1570syntactic context, i.e., whether the macro expands to an expression, to some
3a731e1f
MS
1571statements, or perhaps to whole functions, the syntactic recognition can be
1572wrong. @ccmode{} manages to figure it out correctly most of the time,
1573though. @xref{Syntactic Symbols}, for details about the indentation.
6bf7aab6 1574
3a731e1f
MS
1575@defopt c-syntactic-indentation-in-macros
1576@vindex syntactic-indentation-in-macros (c-)
1577Enable syntactic analysis inside macros, which is the default. If this
1578is @code{nil}, all lines inside macro definitions are analyzed as
1579@code{cpp-macro-cont}.
1580@end defopt
6bf7aab6 1581
3a731e1f
MS
1582@ccmode{} provides some tools to help keep the line continuation
1583backslashes in macros neat and tidy:
1584
1585@table @asis
1586@item @kbd{C-c C-\} (@code{c-backslash-region})
1587@kindex C-c C-\
1588@findex c-backslash-region
1589@findex backslash-region (c-)
1590This function inserts and aligns or deletes the end-of-line backslashes
1591in the current region.
1592
1593With no prefix argument, it inserts any missing backslashes and aligns
1594them according to the @code{c-backslash-column} and
1595@code{c-backslash-max-column} variables. With a prefix argument, it
1596deletes any backslashes.
1597
1598The function does not modify blank lines at the start of the region. If
1599the region ends at the start of a line, it always deletes the backslash
1600(if any) at the end of the previous line.
1601@end table
1602
1603@defopt c-backslash-column
1604@vindex backslash-column (c-)
1605@defoptx c-backslash-max-column
1606@vindex backslash-max-column (c-)
1607These variables control the alignment columns for line continuation
1608backslashes in multiline macros. They are used by the functions that
1609automatically insert or align such backslashes,
c3a2e2d5 1610e.g., @code{c-backslash-region} and @code{c-context-line-break}.
3a731e1f
MS
1611
1612@code{c-backslash-column} specifies the minimum column for the
1613backslashes. If any line in the macro exceeds it then the next tab
1614stop from that line is used as the alignment column for all the
1615backslashes, so that they remain in a single column. However, if some
1616lines exceed @code{c-backslash-max-column} then the backslashes in the
1617rest of the macro will be kept at that column, so that the
1618lines which are too long ``stick out'' instead.
1619@end defopt
1620
1621@defopt c-auto-align-backslashes
1622@vindex auto-align-backslashes (c-)
1623Align automatically inserted line continuation backslashes if
1624non-@code{nil}. When line continuation backslashes are inserted
c3a2e2d5 1625automatically for line breaks in multiline macros, e.g., by
3a731e1f
MS
1626@code{c-context-line-break}, they are aligned with the other backslashes
1627in the same macro if this flag is set. Otherwise the inserted
1628backslashes are preceded by a single space.
1629@end defopt
1630
1631The recommended line breaking function, @code{c-context-line-break}
1632(@pxref{Text Filling and Line Breaking}), is especially nice if you edit
1633multiline macros frequently. When used inside a macro, it automatically
1634inserts and adjusts the mandatory backslash at the end of the line to
1635keep the macro together, and it leaves the point at the right
1636indentation column for the code. Thus you can write code inside macros
1637almost exactly as you can elsewhere, without having to bother with the
1638trailing backslashes.
1639
1640
1641@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1642@node Font Locking, Commands, Macro Handling, Top
1643@comment node-name, next, previous, up
1644@chapter Font Locking
1645@cindex font locking
1646@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1647
c1386334 1648@strong{Please note:} The font locking in AWK mode is currently not integrated
3a731e1f
MS
1649with the rest of @ccmode{}, so this section does not apply there.
1650@xref{AWK Mode Font Locking}, instead.
1651
1652@cindex Font Lock mode
1653
1654@ccmode{} provides font locking for its supported languages by supplying
1655patterns for use with Font Lock mode. This means that you get distinct
1656faces on the various syntactic parts such as comments, strings, keywords
1657and types, which is very helpful in telling them apart at a glance and
1658discovering syntactic errors. @xref{Font Lock,,, emacs, The Emacs
1659Editor}, for ways to enable font locking in @ccmode{} buffers.
1660
1661@menu
1662* Font Locking Preliminaries::
1663* Faces::
1664* Documentation Comments::
1665@end menu
1666
1667
1668@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1669@node Font Locking Preliminaries, Faces, , Font Locking
1670@comment node-name, next, previous, up
1671@section Font Locking Preliminaries
1672@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1673
1674The font locking for most of the @ccmode{} languages were provided
1675directly by the Font Lock package prior to version 5.30 of @ccmode{}.
1676In the transition to @ccmode{} the patterns have been reworked
1677completely and are applied uniformly across all the languages except AWK
1678mode, just like the indentation rules (although each language still has
1679some pecularities of its own, of course). Since the languages
1680previously had completely separate font locking patterns, this means
1681that it's a bit different in most languages now.
1682
1683The main goal for the font locking in @ccmode{} is accuracy, to provide
1684a dependable aid in recognizing the various constructs. Some, like
1685strings and comments, are easy to recognize while others like
1686declarations and types can be very tricky. @ccmode{} can go to great
1687lengths to recognize declarations and casts correctly, especially when
1688the types aren't recognized by standard patterns. This is a fairly
1689demanding analysis which can be slow on older hardware, and it can
1690therefore be disabled by choosing a lower decoration level with the
1691variable @code{font-lock-maximum-decoration}.
1692
1693@vindex font-lock-maximum-decoration
1694
1695The decoration levels are used as follows:
1696
1697@enumerate
1698@comment 1
1699@item
1700Minimal font locking: Fontify only comments, strings and preprocessor
1701directives (in the languages that use cpp).
1702
1703@comment 2
1704@item
1705Fast normal font locking: In addition to level 1, fontify keywords,
1706simple types and declarations that are easy to recognize. The variables
1707@code{*-font-lock-extra-types} (where @samp{*} is the name of the
1708language) are used to recognize types (see below). Documentation
1709comments like Javadoc are fontified according to
1710@code{c-doc-comment-style} (@pxref{Documentation Comments}).
1711
1712Use this if you think the font locking is too slow. It's the closest
1713corresponding level to level 3 in the old font lock patterns.
1714
1715@comment 3
1716@item
1717Accurate normal font locking: Like level 2 but uses a different approach
1718that can recognize types and declarations much more accurately. The
1719@code{*-font-lock-extra-types} variables are still used, but user
1720defined types are recognized correctly anyway in most cases. Therefore
1721those variables should be fairly restrictive and not contain patterns
1722that are uncertain.
1723
1724@cindex Lazy Lock mode
1725@cindex Just-in-time Lock mode
1726
1727This level is designed for fairly modern hardware and a font lock
1728support mode like Lazy Lock or Just-in-time Lock mode that only
1729fontifies the parts that are actually shown.
1730@end enumerate
1731
1732@cindex user defined types
1733@cindex types, user defined
1734
1735Since user defined types are hard to recognize you can provide
1736additional regexps to match those you use:
1737
1738@defopt c-font-lock-extra-types
1739@defoptx c++-font-lock-extra-types
1740@defoptx objc-font-lock-extra-types
1741@defoptx java-font-lock-extra-types
1742@defoptx idl-font-lock-extra-types
1743@defoptx pike-font-lock-extra-types
1744For each language there's a variable @code{*-font-lock-extra-types},
1745where @samp{*} stands for the language in question. It contains a list
1746of regexps that matches identifiers that should be recognized as types,
c3a2e2d5 1747e.g., @samp{\\sw+_t} to recognize all identifiers ending with @samp{_t}
3a731e1f
MS
1748as is customary in C code. Each regexp should not match more than a
1749single identifier.
1750
1751The default values contain regexps for many types in standard runtime
1752libraries that are otherwise difficult to recognize, and patterns for
1753standard type naming conventions like the @samp{_t} suffix in C and C++.
1754Java, Objective-C and Pike have as a convention to start class names
1755with capitals, so there are patterns for that in those languages.
1756
1757Despite the names of these variables, they are not only used for
1758fontification but in other places as well where @ccmode{} needs to
1759recognize types.
1760@end defopt
1761
1762
1763@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1764@node Faces, Documentation Comments, Font Locking Preliminaries, Font Locking
1765@comment node-name, next, previous, up
1766@section Faces
1767@cindex faces
1768@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1769
1770@ccmode{} attempts to use the standard faces for programming languages
1771in accordance with their intended purposes as far as possible. No extra
1772faces are currently provided, with the exception of a replacement face
1773@code{c-invalid-face} for emacsen that don't provide
1774@code{font-lock-warning-face}.
1775
1776@itemize @bullet
1777@item
1778@vindex font-lock-comment-face
1779Normal comments are fontified in @code{font-lock-comment-face}.
1780
1781@item
1782@vindex font-lock-doc-face
1783@vindex font-lock-doc-string-face
1784@vindex font-lock-comment-face
1785Comments that are recognized as documentation (@pxref{Documentation
1786Comments}) get @code{font-lock-doc-face} (Emacs) or
1787@code{font-lock-doc-string-face} (XEmacs) if those faces exist. If they
1788don't then @code{font-lock-comment-face} is used.
1789
1790@item
1791@vindex font-lock-string-face
1792String and character literals are fontified in
1793@code{font-lock-string-face}.
1794
1795@item
1796@vindex font-lock-keyword-face
1797Keywords are fontified with @code{font-lock-keyword-face}.
1798
1799@item
1800@vindex font-lock-function-name-face
1801@code{font-lock-function-name-face} is used for function names in
1802declarations and definitions, and classes in those contexts. It's also
1803used for preprocessor defines with arguments.
1804
1805@item
1806@vindex font-lock-variable-name-face
1807Variables in declarations and definitions, and other identifiers in such
1808variable contexts, get @code{font-lock-variable-name-face}. It's also
1809used for preprocessor defines without arguments.
1810
1811@item
1812@vindex font-lock-constant-face
1813@vindex font-lock-reference-face
1814Builtin constants are fontified in @code{font-lock-constant-face} if it
1815exists, @code{font-lock-reference-face} otherwise. As opposed to the
1816preceding two faces, this is used on the names in expressions, and it's
1817not used in declarations, even if there happen to be a @samp{const} in
1818them somewhere.
1819
1820@item
1821@vindex font-lock-type-face
1822@code{font-lock-type-face} is put on types (both predefined and user
1823defined) and classes in type contexts.
1824
1825@item
1826@vindex font-lock-constant-face
1827@vindex font-lock-reference-face
1828Label identifiers get @code{font-lock-constant-face} if it exists,
1829@code{font-lock-reference-face} otherwise.
1830
1831@item
1832Name qualifiers and identifiers for scope constructs are fontified like
1833labels.
1834
1835@item
1836Special markup inside documentation comments are also fontified like
1837labels.
1838
1839@item
1840@vindex font-lock-preprocessor-face
1841@vindex font-lock-builtin-face
1842@vindex font-lock-reference-face
1843Preprocessor directives get @code{font-lock-preprocessor-face} if it
c3a2e2d5 1844exists (i.e., XEmacs). In Emacs they get @code{font-lock-builtin-face}
3a731e1f
MS
1845or @code{font-lock-reference-face}, for lack of a closer equivalent.
1846
1847@item
1848@vindex font-lock-warning-face
1849@vindex c-invalid-face
1850@vindex invalid-face (c-)
1851Some kinds of syntactic errors are fontified with
1852@code{font-lock-warning-face} in Emacs. In older XEmacs versions
1853there's no corresponding standard face, so there a special
1854@code{c-invalid-face} is used, which is defined to stand out sharply by
1855default.
1856
1857Note that it's not used for @samp{#error} or @samp{#warning} directives,
1858since those aren't syntactic errors in themselves.
1859@end itemize
1860
1861
1862@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1863@node Documentation Comments, , Faces, Font Locking
1864@comment node-name, next, previous, up
1865@section Documentation Comments
1866@cindex documentation comments
1867@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1868
1869There are various tools to supply documentation in the source as
c3a2e2d5 1870specially structured comments, e.g., the standard Javadoc tool in Java.
3a731e1f
MS
1871@ccmode{} provides an extensible mechanism to fontify such comments and
1872the special markup inside them.
1873
1874@defopt c-doc-comment-style
1875@vindex doc-comment-style (c-)
1876This is a style variable that specifies which documentation comment
c3a2e2d5 1877style to recognize, e.g., @code{javadoc} for Javadoc comments.
3a731e1f
MS
1878
1879The value may also be a list of styles, in which case all of them are
1880recognized simultaneously (presumably with markup cues that don't
1881conflict).
1882
1883The value may also be an association list to specify different comment
1884styles for different languages. The symbol for the major mode is then
1885looked up in the alist, and the value of that element is interpreted as
1886above if found. If it isn't found then the symbol `other' is looked up
1887and its value is used instead.
1888
1889Note that @ccmode{} uses this variable to set other variables that
1890handle fontification etc. That's done at mode initialization or when
1891you switch to a style which sets this variable. Thus, if you change it
c3a2e2d5 1892in some other way, e.g., interactively in a CC Mode buffer, you will need
3a731e1f
MS
1893to do @kbd{M-x java-mode} (or whatever mode you're currently using) to
1894reinitialize.
1895
1896@findex c-setup-doc-comment-style
1897@findex setup-doc-comment-style (c-)
1898Note also that when @ccmode{} starts up, the other variables are
1899modified before the mode hooks are run. If you change this variable in
1900a mode hook, you have to call @code{c-setup-doc-comment-style}
1901afterwards to redo that work.
1902@end defopt
1903
1904@ccmode{} currently provides handing of the following doc comment
1905styles:
1906
1907@table @code
1908@item javadoc
1909@cindex Javadoc markup
1910Javadoc comments, the standard tool in Java.
1911
1912@item autodoc
1913@cindex Pike autodoc markup
1914For Pike autodoc markup, the standard in Pike.
1915@end table
1916
1917The above is by no means complete. If you'd like to see support for
1918other doc comment styles, please let us know (@pxref{Mailing Lists and
1919Submitting Bug Reports}).
1920
1921You can also write your own doc comment fontification support to use
1922with @code{c-doc-comment-style}: Supply a variable or function
1923@code{*-font-lock-keywords} where @samp{*} is the name you want to use
1924in @code{c-doc-comment-style}. If it's a variable, it's prepended to
1925@code{font-lock-keywords}. If it's a function, it's called at mode
1926initialization and the result is prepended. For an example, see
1927@code{javadoc-font-lock-keywords} in @file{cc-fonts.el}.
1928
1929If you add support for another doc comment style, please consider
1930contributing it --- send a note to @email{bug-cc-mode@@gnu.org}.
1931
1932
1933@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1934@node Commands, Customizing Indentation, Font Locking, Top
1935@comment node-name, next, previous, up
1936@chapter Commands
1937@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1938
1939@menu
1940* Indentation Commands::
1941* Movement Commands::
1942* Other Commands::
1943@end menu
1944
1945See also @ref{Text Filling and Line Breaking} and @ref{Macro Handling},
1946for commands concerning those bits.
1947
1948
1949@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
d7bd46ed
GM
1950@node Indentation Commands, Movement Commands, , Commands
1951@comment node-name, next, previous,up
3a731e1f 1952@section Indentation Commands
d7bd46ed 1953@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
6bf7aab6 1954
3a731e1f 1955The following list of commands reindent C constructs. Note that when
bf247b6e 1956you change your coding style, either interactively or through some other
3a731e1f 1957means, your file does @emph{not} automatically get reindented. You
bf247b6e 1958will need to execute one of the following commands to see the effects of
d7bd46ed
GM
1959your changes.
1960
1961@cindex GNU indent program
1962Also, variables like @code{c-hanging-*} and @code{c-cleanup-list}
1963only affect how on-the-fly code is formatted. Changing the
3a731e1f 1964``hanginess'' of a brace and then reindenting, will not move the brace
d7bd46ed 1965to a different line. For this, you're better off getting an external
3a731e1f 1966program like GNU @code{indent}, which will rearrange brace location,
d7bd46ed
GM
1967among other things.
1968
3a731e1f 1969Reindenting large sections of code can take a long time. When
d7bd46ed 1970@ccmode{} reindents a region of code, it is essentially equivalent to
3a731e1f 1971hitting @kbd{TAB} on every line of the region.
d7bd46ed
GM
1972
1973These commands are useful when indenting code:
6bf7aab6 1974
d7bd46ed 1975@table @asis
3a731e1f 1976@item @kbd{TAB} (@code{c-indent-command})
d7bd46ed 1977@kindex TAB
6bf7aab6
DL
1978@findex c-indent-command
1979@findex indent-command (c-)
d7bd46ed
GM
1980Indents the current line. The actual behavior is controlled by several
1981variables, described below. See @code{c-tab-always-indent},
1982@code{c-insert-tab-function}, and @code{indent-tabs-mode}. With a
1983numeric argument, this command rigidly indents the region, preserving
1984the relative indentation among the lines.
6bf7aab6 1985
3a731e1f 1986@item @kbd{C-M-q} (@code{c-indent-exp})
718fb8a1 1987@kindex C-M-q
6bf7aab6
DL
1988@findex c-indent-exp
1989@findex indent-exp (c-)
d7bd46ed
GM
1990Indent an entire balanced brace or parenthesis expression. Note that
1991point must be on the opening brace or parenthesis of the expression you
1992want to indent.
6bf7aab6 1993
3a731e1f 1994@item @kbd{C-c C-q} (@code{c-indent-defun})
6bf7aab6
DL
1995@kindex C-c C-q
1996@findex c-indent-defun
1997@findex indent-defun (c-)
3a731e1f
MS
1998Indents the entire top-level function, class or macro definition
1999encompassing point. It leaves point unchanged. This function can't be
2000used to reindent a nested brace construct, such as a nested class or
2001function, or a Java method. The top-level construct being reindented
c3a2e2d5 2002must be complete, i.e., it must have both a beginning brace and an ending
3a731e1f 2003brace.
6bf7aab6 2004
3a731e1f 2005@item @kbd{C-M-\} (@code{indent-region})
718fb8a1 2006@kindex C-M-\
6bf7aab6 2007@findex indent-region
d7bd46ed 2008Indents an arbitrary region of code. This is a standard Emacs command,
3a731e1f 2009tailored for C code in a @ccmode{} buffer. Note, of course, that point
d7bd46ed 2010and mark must delineate the region you want to indent.
6bf7aab6 2011
3a731e1f
MS
2012@item @kbd{M-;} (@code{indent-for-comment})
2013@kindex M-;
2014@findex indent-for-comment
2015Insert a comment at the end of the current line, if none is there already.
2016Then reindent the comment according to the variables
2017@code{c-indent-comment-alist}, @code{c-indent-comments-syntactically-p}
2018and @code{comment-column}. Then position the point after the comment
2019starter. This is a standard Emacs command, but @ccmode{} enhances it a
2020bit with two variables:
2021
2022@defopt c-indent-comment-alist
2023@vindex indent-comment-alist (c-)
2024@vindex comment-column
2025This style variable allows you to control which column @kbd{M-;}
2026indents the comment to, depending on the preceding code and the
2027indentation of a similar comment on the preceding line, if there is
2028any. It is an association list that maps different types of lines to
2029actions describing how they should be handled. If a certain line type
2030isn't present on the list then the line is indented to the column
2031specified by @code{comment-column}. See the documentation string for
2032@code{c-indent-comment-alist} for a full description of the available
2033line types and actions (use @kbd{C-h v c-indent-comment-alist}).
2034@end defopt
2035
2036@defopt c-indent-comments-syntactically-p
2037@vindex indent-comments-syntactically-p (c-)
2038Normally, when this variable is @code{nil}, @kbd{M-;} will indent
2039comment-only lines according to @code{c-indent-comment-alist}, just as
2040it does with lines where other code precede the comments. However, if
2041you want it to act just like @kbd{TAB} for comment-only lines you can
2042get that by setting @code{c-indent-comments-syntactically-p} to
2043non-@code{nil}.
2044
2045If @code{c-indent-comments-syntactically-p} is non-@code{nil} then
2046@code{c-indent-comment-alist} won't be consulted at all for comment-only
2047lines.
2048@end defopt
2049
2050@item @kbd{C-M-h} (@code{c-mark-function})
718fb8a1 2051@kindex C-M-h
6bf7aab6
DL
2052@findex c-mark-function
2053@findex mark-function (c-)
d7bd46ed
GM
2054While not strictly an indentation command, this is useful for marking
2055the current top-level function or class definition as the current
2056region. As with @code{c-indent-defun}, this command operates on
2057top-level constructs, and can't be used to mark say, a Java method.
d7bd46ed
GM
2058@end table
2059
2060These variables are also useful when indenting code:
2061
3a731e1f 2062@defopt c-tab-always-indent
d7bd46ed
GM
2063@vindex tab-always-indent (c-)
2064@kindex TAB
2065@cindex literal
2a15eb73
MS
2066This variable controls how @kbd{TAB} (@code{c-indent-command})
2067operates. When it is @code{t}, @kbd{TAB} always indents the current
2068line. When it is @code{nil}, the line is indented only if point is at
2069the left margin, or on or before the first non-whitespace character on
2070the line, otherwise some whitespace is inserted. If this variable is
2071some other value (not @code{nil} or @code{t}), then some whitespace is
2072inserted only within strings and comments (literals), but the line is
3a731e1f
MS
2073always reindented.
2074@end defopt
2075
2076@defopt c-insert-tab-function
d7bd46ed
GM
2077@vindex insert-tab-function (c-)
2078@findex tab-to-tab-stop
d7bd46ed
GM
2079When ``some whitespace'' is inserted as described above, what actually
2080happens is that the function stored in @code{c-insert-tab-function} is
2081called. Normally, this just inserts a real tab character, or the
2082equivalent number of spaces, depending on @code{indent-tabs-mode}.
2083Some people, however, set @code{c-insert-tab-function} to
2084@code{tab-to-tab-stop} so as to get hard tab stops when indenting.
3a731e1f 2085@end defopt
d7bd46ed 2086
3a731e1f
MS
2087@defopt indent-tabs-mode
2088This is a standard Emacs variable that controls how line indentation
2089is composed. When it's non-@code{nil}, tabs can be used in a line's
2090indentation, otherwise only spaces can be used.
2091@end defopt
d7bd46ed 2092
3a731e1f 2093@defopt c-progress-interval
d7bd46ed 2094@vindex progress-interval (c-)
177c0ea7 2095When indenting large regions of code, this variable controls how often a
d7bd46ed 2096progress message is displayed. Set this variable to @code{nil} to
3a731e1f
MS
2097inhibit the progress messages, or set it to an integer which is how
2098often (in seconds) progress messages are to be displayed.
2099@end defopt
d7bd46ed
GM
2100
2101
2102@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2103@node Movement Commands, Other Commands, Indentation Commands, Commands
2104@comment node-name, next, previous, up
3a731e1f
MS
2105@section Movement Commands
2106@cindex movement
6bf7aab6
DL
2107@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2108
3a731e1f 2109@ccmode{} contains some useful commands for moving around in C
6bf7aab6
DL
2110code.
2111
d7bd46ed 2112@table @asis
3a731e1f 2113@item @kbd{M-x c-beginning-of-defun}
6bf7aab6
DL
2114@findex c-beginning-of-defun
2115@findex beginning-of-defun (c-)
2116@findex beginning-of-defun
3a731e1f 2117Move point back to the least-enclosing brace. This function is
6bf7aab6
DL
2118analogous to the Emacs built-in command @code{beginning-of-defun},
2119except it eliminates the constraint that the top-level opening brace
2120must be in column zero. See @code{beginning-of-defun} for more
2121information.
2122
2123Depending on the coding style being used, you might prefer
2124@code{c-beginning-of-defun} to @code{beginning-of-defun}. If so,
2125consider binding @kbd{C-M-a} to the former instead. For backwards
2126compatibility reasons, the default binding remains in effect.
2127
3a731e1f
MS
2128In AWK mode, a defun doesn't necessarily have braces at all. AWK Mode
2129therefore has its own version of this function which is bound by
2130default to @kbd{C-M-a}. You can thus chose freely which function to
2131bind to @kbd{C-M-a} for the other modes without worrying about AWK
2132buffers. @xref{AWK Mode Defuns}.
2133
2134@item @kbd{M-x c-end-of-defun}
6bf7aab6
DL
2135@findex c-end-of-defun
2136@findex end-of-defun (c-)
2137@findex end-of-defun
6bf7aab6
DL
2138Moves point to the end of the current top-level definition. This
2139function is analogous to the Emacs built-in command @code{end-of-defun},
2140except it eliminates the constraint that the top-level opening brace of
3a731e1f 2141the defun must be in column zero. See @code{end-of-defun} for more
6bf7aab6
DL
2142information.
2143
2144Depending on the coding style being used, you might prefer
2145@code{c-end-of-defun} to @code{end-of-defun}. If so,
2146consider binding @kbd{C-M-e} to the former instead. For backwards
2147compatibility reasons, the default binding remains in effect.
2148
3a731e1f
MS
2149In AWK Mode, a defun doesn't necessarily have braces at all. AWK Mode
2150therefore has its own version of this function which is bound by
2151default to @kbd{C-M-e}. You can thus chose freely which function to
2152bind to @kbd{C-M-e} for the other modes without worrying about AWK
2153buffers. @ref{AWK Mode Defuns}.
2154
2155@item @kbd{C-c C-u} (@code{c-up-conditional})
6bf7aab6
DL
2156@kindex C-c C-u
2157@findex c-up-conditional
2158@findex up-conditional (c-)
6bf7aab6
DL
2159Move point back to the containing preprocessor conditional, leaving the
2160mark behind. A prefix argument acts as a repeat count. With a negative
2161argument, move point forward to the end of the containing
d7bd46ed
GM
2162preprocessor conditional.
2163
2164@samp{#elif} is treated like @samp{#else} followed by @samp{#if}, so the
2165function stops at them when going backward, but not when going forward.
2166
3a731e1f 2167@item @kbd{M-x c-up-conditional-with-else}
d7bd46ed
GM
2168@findex c-up-conditional-with-else
2169@findex up-conditional-with-else (c-)
d7bd46ed
GM
2170A variety of @code{c-up-conditional} that also stops at @samp{#else}
2171lines. Normally those lines are ignored.
2172
3a731e1f 2173@item @kbd{M-x c-down-conditional}
d7bd46ed
GM
2174@findex c-down-conditional
2175@findex down-conditional (c-)
d7bd46ed
GM
2176Move point forward into the next nested preprocessor conditional,
2177leaving the mark behind. A prefix argument acts as a repeat count.
2178With a negative argument, move point backward into the previous
2179nested preprocessor conditional.
2180
2181@samp{#elif} is treated like @samp{#else} followed by @samp{#if}, so the
2182function stops at them when going forward, but not when going backward.
2183
3a731e1f 2184@item @kbd{M-x c-down-conditional-with-else}
d7bd46ed
GM
2185@findex c-down-conditional-with-else
2186@findex down-conditional-with-else (c-)
d7bd46ed
GM
2187A variety of @code{c-down-conditional} that also stops at @samp{#else}
2188lines. Normally those lines are ignored.
6bf7aab6 2189
3a731e1f 2190@item @kbd{C-c C-p} (@code{c-backward-conditional})
6bf7aab6
DL
2191@kindex C-c C-p
2192@findex c-backward-conditional
2193@findex backward-conditional (c-)
6bf7aab6
DL
2194Move point back over a preprocessor conditional, leaving the mark
2195behind. A prefix argument acts as a repeat count. With a negative
2196argument, move forward.
2197
3a731e1f 2198@item @kbd{C-c C-n} (@code{c-forward-conditional})
6bf7aab6
DL
2199@kindex C-c C-n
2200@findex c-forward-conditional
2201@findex forward-conditional (c-)
6bf7aab6
DL
2202Move point forward across a preprocessor conditional, leaving the mark
2203behind. A prefix argument acts as a repeat count. With a negative
2204argument, move backward.
2205
3a731e1f 2206@item @kbd{M-a} (@code{c-beginning-of-statement})
d7bd46ed 2207@kindex M-a
6bf7aab6
DL
2208@findex c-beginning-of-statement
2209@findex beginning-of-statement (c-)
6bf7aab6 2210Move point to the beginning of the innermost C statement. If point is
cb7f2e96
GM
2211already at the beginning of a statement, move to the beginning of the
2212closest preceding statement, even if that means moving into a block (you
718fb8a1 2213can use @kbd{C-M-b} to move over a balanced block). With prefix
6bf7aab6
DL
2214argument @var{n}, move back @var{n} @minus{} 1 statements.
2215
cb7f2e96
GM
2216If point is within or next to a comment or a string which spans more
2217than one line, this command moves by sentences instead of statements.
6bf7aab6
DL
2218
2219When called from a program, this function takes three optional
cb7f2e96
GM
2220arguments: the repetition count, a buffer position limit which is the
2221farthest back to search for the syntactic context, and a flag saying
2222whether to do sentence motion in or near comments and multiline strings.
6bf7aab6 2223
3a731e1f 2224@item @kbd{M-e} (@code{c-end-of-statement})
d7bd46ed 2225@kindex M-e
6bf7aab6
DL
2226@findex c-end-of-statement
2227@findex end-of-statement (c-)
6bf7aab6
DL
2228Move point to the end of the innermost C statement. If point is at the
2229end of a statement, move to the end of the next statement, even if it's
718fb8a1 2230inside a nested block (use @kbd{C-M-f} to move to the other side of the
6bf7aab6
DL
2231block). With prefix argument @var{n}, move forward @var{n} @minus{} 1
2232statements.
2233
cb7f2e96
GM
2234If point is within or next to a comment or a string which spans more
2235than one line, this command moves by sentences instead of statements.
6bf7aab6
DL
2236
2237When called from a program, this function takes three optional
cb7f2e96
GM
2238arguments: the repetition count, a buffer position limit which is the
2239farthest back to search for the syntactic context, and a flag saying
2240whether to do sentence motion in or near comments and multiline strings.
6bf7aab6 2241
3a731e1f 2242@item @kbd{M-x c-forward-into-nomenclature}
6bf7aab6
DL
2243@findex c-forward-into-nomenclature
2244@findex forward-into-nomenclature (c-)
6bf7aab6
DL
2245A popular programming style, especially for object-oriented languages
2246such as C++ is to write symbols in a mixed case format, where the first
2247letter of each word is capitalized, and not separated by underscores.
c3a2e2d5 2248e.g., @samp{SymbolsWithMixedCaseAndNoUnderlines}.
6bf7aab6
DL
2249
2250This command moves point forward to next capitalized word. With prefix
2251argument @var{n}, move @var{n} times.
2252
3a731e1f 2253@item @kbd{M-x c-backward-into-nomenclature}
6bf7aab6
DL
2254@findex c-backward-into-nomenclature
2255@findex backward-into-nomenclature (c-)
6bf7aab6
DL
2256Move point backward to beginning of the next capitalized
2257word. With prefix argument @var{n}, move @var{n} times. If
2258@var{n} is negative, move forward.
d7bd46ed
GM
2259@end table
2260
2261
2262@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2263@node Other Commands, , Movement Commands, Commands
2264@comment node-name, next, previous, up
3a731e1f 2265@section Other Commands
d7bd46ed
GM
2266@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2267
3a731e1f 2268Here are the various other commands that didn't fit anywhere else:
d7bd46ed
GM
2269
2270@table @asis
3a731e1f 2271@item @kbd{C-c :} (@code{c-scope-operator})
6bf7aab6
DL
2272@kindex C-c :
2273@findex c-scope-operator
2274@findex scope-operator (c-)
6bf7aab6
DL
2275In C++, it is also sometimes desirable to insert the double-colon scope
2276operator without performing the electric behavior of colon insertion.
2277@kbd{C-c :} does just this.
6bf7aab6
DL
2278@end table
2279
d7bd46ed
GM
2280@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2281@node Customizing Indentation, Syntactic Symbols, Commands, Top
2282@comment node-name, next, previous, up
3a731e1f
MS
2283@chapter Customizing Indentation
2284@cindex customization, indentation
2285@cindex indentation
6bf7aab6
DL
2286@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2287
3a731e1f
MS
2288The context sensitive indentation is mainly controlled by the variable
2289@code{c-offsets-alist}:
2290
2291@defopt c-offsets-alist
6bf7aab6 2292@vindex offsets-alist (c-)
3a731e1f
MS
2293This special style variable contains the mappings between syntactic
2294symbols and the offsets to apply for those symbols. It's set at mode
2295initialization from a @emph{style} you may specify. Styles are
d7bd46ed 2296groupings of syntactic symbol offsets and other style variable values.
3a731e1f 2297Most likely, you'll find that one of the predefined styles will suit
d7bd46ed
GM
2298your needs. @xref{Styles}, for an explanation of how to set up named
2299styles.
2300
2301Only syntactic symbols not already bound on @code{c-offsets-alist} will
2302be set from styles. This means that any association you set on it, be
2303it before or after mode initialization, will not be changed. The
c3a2e2d5 2304@code{c-offsets-alist} variable may therefore be used from e.g., the
d7bd46ed
GM
2305Customization interface@footnote{Available in Emacs 20 and later, and
2306XEmacs 19.15 and later.} to easily change indentation offsets without
2307having to bother about styles. Initially @code{c-offsets-alist} is
2308empty, so that all syntactic symbols are set by the style system.
2309
3a731e1f
MS
2310The offset associated with any particular syntactic symbol can be an
2311integer, a function or lambda expression, a variable name, a vector, a
2312list, or one of the following special symbols: @code{+}, @code{-},
2313@code{++}, @code{--}, @code{*}, or @code{/}. The meaning of these
2314values are described in detail below.
2315@end defopt
d7bd46ed 2316
3a731e1f
MS
2317The special symbols describe an offset in multiples of the value of
2318@code{c-basic-offset}:
2319
2320@defopt c-basic-offset
d7bd46ed 2321@vindex basic-offset (c-)
3a731e1f
MS
2322Style variable that holds the basic offset between indentation levels.
2323@end defopt
6bf7aab6 2324
3a731e1f
MS
2325By defining a style's indentation in terms of @code{c-basic-offset},
2326you can change the amount of whitespace given to an indentation level
2327while maintaining the same basic shape of your code. Here are the
2328values that the special symbols correspond to:
6bf7aab6 2329
3a731e1f 2330@table @code
6bf7aab6
DL
2331@item +
2332@code{c-basic-offset} times 1
2333@item -
2334@code{c-basic-offset} times -1
2335@item ++
2336@code{c-basic-offset} times 2
2337@item --
2338@code{c-basic-offset} times -2
2339@item *
2340@code{c-basic-offset} times 0.5
2341@item /
2342@code{c-basic-offset} times -0.5
6bf7aab6
DL
2343@end table
2344
d7bd46ed
GM
2345@cindex indentation functions
2346
2347When a function is used as offset, it's called an @dfn{indentation
2348function}. Such functions are useful when more context than just the
2349syntactic symbol is needed to get the desired indentation.
2350@xref{Indentation Functions}, and @ref{Custom Indentation Functions},
2351for details about them.
2352
cb7f2e96 2353If the offset is a vector, its first element sets the absolute
3a731e1f
MS
2354indentation column, which will override any previous relative
2355indentation. It won't override additional relative indentation for
2356nested constructs, though.
cb7f2e96 2357
d7bd46ed
GM
2358@vindex c-strict-syntax-p
2359@vindex strict-syntax-p (c-)
2360The offset can also be a list, in which case it is evaluated recursively
2361using the semantics described above. The first element of the list that
2362returns a non-@code{nil} value succeeds and the evaluation stops. If
2363none of the list elements return a non-@code{nil} value, then an offset
2364of 0 (zero) is used@footnote{There is however a variable
2365@code{c-strict-syntax-p} that, when set to non-@code{nil}, will cause an
2c7a8f63 2366error to be signaled in that case. It's now considered obsolete since
d7bd46ed
GM
2367it doesn't work well with some of the alignment functions that now
2368returns @code{nil} instead of zero to be more usable in lists. You
2369should therefore leave @code{c-strict-syntax-p} set to @code{nil}.}.
2370
6bf7aab6
DL
2371So, for example, because most of the default offsets are defined in
2372terms of @code{+}, @code{-}, and @code{0}, if you like the general
2373indentation style, but you use 4 spaces instead of 2 spaces per level,
2374you can probably achieve your style just by changing
d7bd46ed
GM
2375@code{c-basic-offset} like so@footnote{You can try this interactively in
2376a C buffer by typing the text that appears in italics.}:
6bf7aab6 2377
3a731e1f 2378@example
d7bd46ed
GM
2379@emph{M-x set-variable RET}
2380Set variable: @emph{c-basic-offset RET}
2381Set c-basic-offset to value: @emph{4 RET}
6bf7aab6
DL
2382@end example
2383
2384@noindent
2385This would change
3a731e1f 2386
6bf7aab6
DL
2387@example
2388@group
6bf7aab6
DL
2389int add( int val, int incr, int doit )
2390@{
2391 if( doit )
2392 @{
2393 return( val + incr );
2394 @}
2395 return( val );
2396@}
6bf7aab6
DL
2397@end group
2398@end example
3a731e1f 2399
6bf7aab6
DL
2400@noindent
2401to
3a731e1f 2402
6bf7aab6
DL
2403@example
2404@group
6bf7aab6
DL
2405int add( int val, int incr, int doit )
2406@{
2407 if( doit )
2408 @{
2409 return( val + incr );
2410 @}
2411 return( val );
2412@}
6bf7aab6
DL
2413@end group
2414@end example
2415
6bf7aab6 2416To change indentation styles more radically, you will want to change the
d7bd46ed
GM
2417offsets associated with other syntactic symbols. First, I'll show you
2418how to do that interactively, then I'll describe how to make changes to
2419your @file{.emacs} file so that your changes are more permanent.
6bf7aab6
DL
2420
2421@menu
2422* Interactive Customization::
2423* Permanent Customization::
d7bd46ed 2424* Hooks::
6bf7aab6
DL
2425* Styles::
2426* Advanced Customizations::
2427@end menu
2428
6bf7aab6 2429
d7bd46ed
GM
2430@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2431@node Interactive Customization, Permanent Customization, , Customizing Indentation
2432@comment node-name, next, previous, up
3a731e1f
MS
2433@section Interactive Customization
2434@cindex customization, interactive
d7bd46ed 2435@cindex interactive customization
6bf7aab6
DL
2436@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2437
2438As an example of how to customize indentation, let's change the
3a731e1f 2439style of this example@footnote{In this and subsequent examples, the
6bf7aab6 2440original code is formatted using the @samp{gnu} style unless otherwise
d7bd46ed 2441indicated. @xref{Styles}.}:
3a731e1f 2442
6bf7aab6
DL
2443@example
2444@group
3a731e1f
MS
2445 1: int add( int val, int incr, int doit )
2446 2: @{
2447 3: if( doit )
2448 4: @{
2449 5: return( val + incr );
2450 6: @}
2451 7: return( val );
2452 8: @}
6bf7aab6
DL
2453@end group
2454@end example
3a731e1f 2455
6bf7aab6
DL
2456@noindent
2457to:
3a731e1f 2458
6bf7aab6
DL
2459@example
2460@group
3a731e1f
MS
2461 1: int add( int val, int incr, int doit )
2462 2: @{
2463 3: if( doit )
2464 4: @{
2465 5: return( val + incr );
2466 6: @}
2467 7: return( val );
2468 8: @}
6bf7aab6
DL
2469@end group
2470@end example
2471
2472In other words, we want to change the indentation of braces that open a
2473block following a condition so that the braces line up under the
2474conditional, instead of being indented. Notice that the construct we
2475want to change starts on line 4. To change the indentation of a line,
2476we need to see which syntactic components affect the offset calculations
2477for that line. Hitting @kbd{C-c C-s} on line 4 yields:
6bf7aab6 2478
3a731e1f
MS
2479@example
2480((substatement-open 44))
6bf7aab6
DL
2481@end example
2482
6bf7aab6
DL
2483@noindent
2484so we know that to change the offset of the open brace, we need to
2485change the indentation for the @code{substatement-open} syntactic
3a731e1f
MS
2486symbol.
2487
2488To do this interactively, just hit @kbd{C-c C-o}. This prompts
d7bd46ed
GM
2489you for the syntactic symbol to change, providing a reasonable default.
2490In this case, the default is @code{substatement-open}, which is just the
2491syntactic symbol we want to change!
6bf7aab6
DL
2492
2493After you hit return, @ccmode{} will then prompt you for the new
2494offset value, with the old value as the default. The default in this
2495case is @samp{+}, but we want no extra indentation so enter
2496@samp{0} and @kbd{RET}. This will associate the offset 0 with the
d7bd46ed 2497syntactic symbol @code{substatement-open}.
6bf7aab6 2498
6bf7aab6
DL
2499To check your changes quickly, just hit @kbd{C-c C-q}
2500(@code{c-indent-defun}) to reindent the entire function. The example
2501should now look like:
3a731e1f 2502
6bf7aab6
DL
2503@example
2504@group
3a731e1f
MS
2505 1: int add( int val, int incr, int doit )
2506 2: @{
2507 3: if( doit )
2508 4: @{
2509 5: return( val + incr );
2510 6: @}
2511 7: return( val );
2512 8: @}
6bf7aab6
DL
2513@end group
2514@end example
2515
2516Notice how just changing the open brace offset on line 4 is all we
2517needed to do. Since the other affected lines are indented relative to
2518line 4, they are automatically indented the way you'd expect. For more
2519complicated examples, this may not always work. The general approach to
2520take is to always start adjusting offsets for lines higher up in the
3a731e1f 2521file, then reindent and see if any following lines need further
6bf7aab6
DL
2522adjustments.
2523
3a731e1f
MS
2524@deffn Command c-set-offset symbol offset
2525@findex set-offset (c-)
2526@kindex C-c C-o
2527This is the command bound to @kbd{C-c C-o}. It provides a convenient
2528way to set offsets on @code{c-offsets-alist} both interactively (see
2529the example above) and from your mode hook.
2530
2531It takes two arguments when used programmatically: @var{symbol} is the
2532syntactic element symbol to change and @var{offset} is the new offset
2533for that syntactic element.
2534@end deffn
2535
d7bd46ed
GM
2536
2537@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2538@node Permanent Customization, Hooks, Interactive Customization, Customizing Indentation
2539@comment node-name, next, previous, up
3a731e1f
MS
2540@section Permanent Customization
2541@cindex customization, permanent
d7bd46ed 2542@cindex permanent customization
6bf7aab6 2543@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
6bf7aab6 2544
d7bd46ed
GM
2545To make your changes permanent, you need to add some lisp code to your
2546@file{.emacs} file. @ccmode{} supports many different ways to be
2547configured, from the straightforward way by setting variables globally
2548in @file{.emacs} or in the Customization interface, to the complex and
2549precisely controlled way by using styles and hook functions.
2550
2551The simplest way of customizing @ccmode{} permanently is to set the
2552variables in your @file{.emacs} with @code{setq} and similar commands.
3a731e1f
MS
2553So to make a permanent setting of @code{substatement-open} to 0, add
2554this to the @file{.emacs} file:
2555
d7bd46ed
GM
2556@example
2557@group
3a731e1f
MS
2558(setq c-offsets-alist
2559 '((substatement-open . 0)))
d7bd46ed
GM
2560@end group
2561@end example
2562
3a731e1f
MS
2563When @ccmode{} initializes a buffer, it will fill out
2564@code{c-offsets-alist} with the remaining syntactic symbols according to
2565the style system.
d7bd46ed
GM
2566
2567You can also use the more user friendly Customization interface, but
2568this manual does not cover how that works.
2569
2570Variables set like this at the top level in @file{.emacs} take effect in
2571all @ccmode{} buffers, regardless of language. The indentation style
c3a2e2d5 2572related variables, e.g., @code{c-offsets-alist}, that you don't set this
f214c025
GM
2573way get their value from the style system (@pxref{Styles}), and they
2574therefore depend on the setting of @code{c-default-style}. Note that if
2575you use Customize, this means that the greyed-out default values
2576presented there might not be the ones you actually get, since the actual
2577values depend on the style, which may very well be different for
2578different languages.
d7bd46ed 2579
c3a2e2d5 2580If you want to make more advanced configurations, e.g., language-specific
3a731e1f 2581customization, setting global variables isn't enough. For that you can
d7bd46ed
GM
2582use the language hooks, see @ref{Hooks}, and/or the style system, see
2583@ref{Styles}.
2584
3a731e1f 2585@defopt c-style-variables-are-local-p
d7bd46ed 2586@vindex style-variables-are-local-p (c-)
3a731e1f
MS
2587By default, all style variables are buffer local, so that different
2588buffers can have different style settings. If you only use one style
2589in all the files you edit you might want to share them between buffers
2590so that a change take effect in all buffers. That's done by setting
2591this variable to @code{nil}. The value takes effect when @ccmode{} is
2592activated in a buffer for the first time in the Emacs session, so you
2593typically set it in your @file{.emacs} file and then restart Emacs.
2594@end defopt
d7bd46ed
GM
2595
2596
2597@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2598@node Hooks, Styles, Permanent Customization, Customizing Indentation
2599@comment node-name, next, previous, up
3a731e1f
MS
2600@section Hooks
2601@cindex mode hooks
6bf7aab6
DL
2602@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2603
d7bd46ed
GM
2604@ccmode{} provides several hooks that you can use to customize the mode
2605according to your coding style. Each language mode has its own hook,
2606adhering to standard Emacs major mode conventions. There is also one
2607general hook and one package initialization hook:
6bf7aab6 2608
3a731e1f
MS
2609@defvar c-initialization-hook
2610@vindex initialization-hook (c-)
2611Hook run only once per Emacs session, when @ccmode{} is initialized.
2612@end defvar
6bf7aab6 2613
3a731e1f
MS
2614@defvar c-mode-common-hook
2615@vindex mode-common-hook (c-)
2616Common hook across all languages. It's run immediately before the
2617language specific hook.
2618@end defvar
2619
2620@defvar c-mode-hook
2621@defvarx c++-mode-hook
2622@defvarx objc-mode-hook
2623@defvarx java-mode-hook
2624@defvarx idl-mode-hook
2625@defvarx pike-mode-hook
2626@defvarx awk-mode-hook
2627The language specific mode hooks. The appropriate one is run as the
2628last thing when you enter that language mode.
2629@end defvar
cb7f2e96
GM
2630
2631Note that all the language-specific mode setup that CC Mode does is done
2632prior to both @code{c-mode-common-hook} and the language specific hook.
2633That includes installing the indentation style, which can be mode
2634specific (and also is by default for Java mode). Thus, any style
2635settings done in @code{c-mode-common-hook} will override whatever
2636language-specific style is chosen by @code{c-default-style}.
6bf7aab6
DL
2637
2638Here's a simplified example of what you can add to your @file{.emacs}
d7bd46ed
GM
2639file to do things whenever any @ccmode{} language is edited. See the
2640Emacs manuals for more information on customizing Emacs via hooks.
2641@xref{Sample .emacs File}, for a more complete sample @file{.emacs}
2642file.
6bf7aab6 2643
3a731e1f 2644@example
6bf7aab6
DL
2645(defun my-c-mode-common-hook ()
2646 ;; my customizations for all of c-mode and related modes
d7bd46ed 2647 (no-case-fold-search)
6bf7aab6
DL
2648 )
2649(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
6bf7aab6
DL
2650@end example
2651
6bf7aab6
DL
2652
2653@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
d7bd46ed
GM
2654@node Styles, Advanced Customizations, Hooks, Customizing Indentation
2655@comment node-name, next, previous, up
3a731e1f 2656@section Styles
d7bd46ed 2657@cindex styles
6bf7aab6
DL
2658@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2659
2660Most people only need to edit code formatted in just a few well-defined
2661and consistent styles. For example, their organization might impose a
2662``blessed'' style that all its programmers must conform to. Similarly,
d7bd46ed
GM
2663people who work on GNU software will have to use the GNU coding style.
2664Some shops are more lenient, allowing a variety of coding styles, and as
2665programmers come and go, there could be a number of styles in use. For
2666this reason, @ccmode{} makes it convenient for you to set up logical
2667groupings of customizations called @dfn{styles}, associate a single name
2668for any particular style, and pretty easily start editing new or
2669existing code using these styles.
2670
2671@cindex style variables
2672The variables that the style system affect are called @dfn{style
2673variables}. They are handled specially in several ways:
2674
2675@itemize @bullet
2676@item
3a731e1f
MS
2677Style variables are by default buffer local variables. However, they
2678can instead be made global by setting
2679@code{c-style-variables-are-local-p} to @code{nil} before @ccmode{} is
2680initialized.
d7bd46ed 2681
3a731e1f 2682@item
d7bd46ed
GM
2683@vindex c-old-style-variable-behavior
2684@vindex old-style-variable-behavior (c-)
d7bd46ed
GM
2685The default value of any style variable (with two exceptions --- see
2686below) is the special symbol @code{set-from-style}. Variables that are
2687still set to that symbol when a @ccmode{} buffer is initialized will be
2688set according to the current style, otherwise they will keep their
2689current value@footnote{This is a big change from versions of @ccmode{}
2690earlier than 5.26, where such settings would get overridden by the style
2691system unless special precautions were taken. That was changed since it
2692was counterintuitive and confusing, especially to novice users. If your
2693configuration depends on the old overriding behavior, you can set the
2694variable @code{c-old-style-variable-behavior} to non-@code{nil}.}.
2695
2696Note that when we talk about the ``default value'' for a style variable,
2697we don't mean the @code{set-from-style} symbol that all style variables
2698are set to initially, but instead the value it will get at mode
2699initialization when neither a style nor a global setting has set its
2700value.
2701
2702The style variable @code{c-offsets-alist} is handled a little
2703differently from the other style variables. It's an association list,
2704and is thus by default set to the empty list, @code{nil}. When the
2705style system is initialized, any syntactic symbols already on it are
2706kept --- only the missing ones are filled in from the chosen style.
2707
2708The style variable @code{c-special-indent-hook} is also handled in a
2709special way. Styles may only add more functions on this hook, so the
2710global settings on it are always preserved@footnote{This did not change
2711in version 5.26.}.
2712
2713@item
2714The global settings of style variables get captured in the special
2715@code{user} style, which is used as the base for all the other styles.
2716@xref{Built-in Styles}, for details.
d7bd46ed
GM
2717@end itemize
2718
2719The style variables are:
2720@code{c-basic-offset},
2721@code{c-comment-only-line-offset},
2722@code{c-block-comment-prefix},
2723@code{c-comment-prefix-regexp},
2724@code{c-cleanup-list},
2725@code{c-hanging-braces-alist},
2726@code{c-hanging-colons-alist},
2727@code{c-hanging-semi&comma-criteria},
2728@code{c-backslash-column},
3a731e1f 2729@code{c-backslash-max-column},
d7bd46ed
GM
2730@code{c-special-indent-hook},
2731@code{c-label-minimum-indentation}, and
2732@code{c-offsets-alist}.
6bf7aab6
DL
2733
2734@menu
2735* Built-in Styles::
3a731e1f 2736* Choosing a Style::
6bf7aab6
DL
2737* Adding Styles::
2738* File Styles::
2739@end menu
2740
2741
2742@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3a731e1f 2743@node Built-in Styles, Choosing a Style, , Styles
d7bd46ed
GM
2744@comment node-name, next, previous, up
2745@subsection Built-in Styles
3a731e1f 2746@cindex styles, built-in
6bf7aab6
DL
2747@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2748
2749If you're lucky, one of @ccmode{}'s built-in styles might be just
2750what you're looking for. These include:
2751
3a731e1f
MS
2752@table @code
2753@item gnu
6bf7aab6 2754@cindex GNU style
3a731e1f 2755Coding style blessed by the Free Software Foundation
d7bd46ed 2756for C code in GNU programs.
6bf7aab6 2757
3a731e1f 2758@item k&r
6bf7aab6 2759@cindex K&R style
3a731e1f 2760The classic Kernighan and Ritchie style for C code.
6bf7aab6 2761
3a731e1f 2762@item bsd
6bf7aab6 2763@cindex BSD style
3a731e1f 2764Also known as ``Allman style'' after Eric Allman.
6bf7aab6 2765
3a731e1f
MS
2766@item whitesmith
2767@cindex Whitesmith style
2768Popularized by the examples that came with Whitesmiths C, an early
2769commercial C compiler.
6bf7aab6 2770
3a731e1f 2771@item stroustrup
6bf7aab6 2772@cindex Stroustrup style
3a731e1f 2773The classic Stroustrup style for C++ code.
6bf7aab6 2774
3a731e1f 2775@item ellemtel
6bf7aab6 2776@cindex Ellemtel style
3a731e1f
MS
2777Popular C++ coding standards as defined by ``Programming in C++, Rules
2778and Recommendations,'' Erik Nyquist and Mats Henricson,
2779Ellemtel@footnote{This document is available at
d7bd46ed
GM
2780@uref{http://www.doc.ic.ac.uk/lab/cplus/c++.rules/} among other
2781places.}.
6bf7aab6 2782
3a731e1f 2783@item linux
6bf7aab6 2784@cindex Linux style
3a731e1f 2785C coding standard for Linux (the kernel).
6bf7aab6 2786
3a731e1f 2787@item python
6bf7aab6 2788@cindex Python style
3a731e1f
MS
2789C coding standard for Python extension modules@footnote{Python is a
2790high level scripting language with a C/C++ foreign function interface.
2791For more information, see @uref{http://www.python.org/}.}.
6bf7aab6 2792
3a731e1f 2793@item java
6bf7aab6 2794@cindex Java style
3a731e1f 2795The style for editing Java code. Note that the default
cb7f2e96
GM
2796value for @code{c-default-style} installs this style when you enter
2797@code{java-mode}.
6bf7aab6 2798
3a731e1f 2799@item user
6bf7aab6 2800@cindex User style
3a731e1f 2801This is a special style for several reasons. First, the
d7bd46ed
GM
2802@ccmode{} customizations you do by using either the Customization
2803interface, or by writing @code{setq}'s at the top level of your
2804@file{.emacs} file, will be captured in the @code{user} style. Also,
2805all other styles implicitly inherit their settings from @code{user}
2806style. This means that for any styles you add via @code{c-add-style}
2807(@pxref{Adding Styles}) you need only define the differences between
2808your new style and @code{user} style.
3a731e1f 2809@end table
d7bd46ed 2810
d7bd46ed 2811
3a731e1f
MS
2812@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2813@node Choosing a Style, Adding Styles, Built-in Styles, Styles
2814@comment node-name, next, previous, up
2815@subsection Choosing a Style
2816@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2817
2818Use @kbd{C-c .} to choose a style interactively:
2819
2820@deffn Command c-set-style style-name
2821@findex set-style (c-)
2822@kindex C-c .
2823Switch to the specified style in the current buffer. Use
2824interactively like this:
2825
2826@example
2827@kbd{C-c . @var{style-name} RET}
2828@end example
2829
2830Note that all style names are case insensitive, even the ones you
2831define.
2832
2833Setting a style in this way does @emph{not} automatically reindent your
2834file. For commands that you can use to view the effect of your changes,
2835see @ref{Commands}.
2836@end deffn
2837
2838The default style in all newly created buffers is @code{gnu}, except
2839in Java mode where it's @code{java}. Although the @code{user} style
2840is not the default style, any style variable settings you do with the
2841Customization interface or on the top level in your @file{.emacs} file
2842will by default override the style system, so you don't need to set
2843@code{c-default-style} to @code{user} to see the effect of such
2844settings.
2845
2846@defopt c-default-style
2847@vindex default-style (c-)
2848This variable specifies which style to install by default in new
2849buffers. It takes either a style name string, or an association list
2850of major mode symbols to style names:
2851
2852@enumerate
2853@item
2854When @code{c-default-style} is a string, it must be an existing style
2855name. This style is then used for all modes.
6bf7aab6 2856
d7bd46ed 2857@item
3a731e1f
MS
2858When @code{c-default-style} is an association list, the mode language
2859is looked up to find a style name string.
d7bd46ed
GM
2860
2861@item
3a731e1f
MS
2862If @code{c-default-style} is an association list where the mode
2863language mode isn't found then the special symbol @samp{other} is
2864looked up. If it's found then the associated style is used.
d7bd46ed
GM
2865
2866@item
3a731e1f 2867If @samp{other} is not found then the @samp{gnu} style is used.
d7bd46ed
GM
2868
2869@item
bf247b6e 2870In all cases, the style described in @code{c-default-style} is installed
d7bd46ed 2871@emph{before} the language hooks are run, so you can always override
bf247b6e 2872this setting by including an explicit call to @code{c-set-style} in your
d7bd46ed 2873language mode hook, or in @code{c-mode-common-hook}.
d7bd46ed 2874@end enumerate
3a731e1f 2875@end defopt
6bf7aab6 2876
3a731e1f 2877@defvar c-indentation-style
6bf7aab6 2878@vindex indentation-style (c-)
3a731e1f
MS
2879This variable always contains the buffer's current style name, as a
2880string.
2881@end defvar
6bf7aab6
DL
2882
2883
2884@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3a731e1f 2885@node Adding Styles, File Styles, Choosing a Style, Styles
d7bd46ed 2886@comment node-name, next, previous, up
2a15eb73 2887@subsection Adding and Amending Styles
6bf7aab6
DL
2888@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2889
6bf7aab6 2890If none of the built-in styles is appropriate, you'll probably want to
2a15eb73
MS
2891create a new @dfn{style definition}, possibly based on an existing
2892style. To do this, put the new style's settings into a list with the
2893following format - the list can then be passed as an argument to the
2894function @code{c-add-style}:
2895
2896@cindex style definition
2897@defvr {List} style definition
2898([@var{base-style}] [(@var{variable} . @var{value}) @dots{}])
2899
2900Optional @var{base-style}, if present, must be a string which is the
2901name of the @dfn{base style} from which this style inherits. At most
2902one @var{base-style} is allowed in a style definition. If
2903@var{base-style} is not specified, the style inherits from a table of
2904default values@footnote{This table is stored internally in the
2905variable c-fallback-style. It is computed during the initialisation
2906of @ccmode{} from the factory defaults of the style variables and any
2907global values they may have been given since starting Emacs.} instead.
2908All styles eventually inherit from this internal table. Style loops
2909generate errors. The list of pre-existing styles can be seen in
2910@ref{Built-in Styles}.
2911
2912The dotted pairs (@var{variable} . @var{value}) each consist of a
2913variable and the value it is to be set to when the style is later
2914activated.@footnote{In certain circumstances, this value can get
2915overridden by another value.} The variable can be either a @ccmode{}
2916style variable or an arbitrary Emacs variable. In the latter case, it
2917is @emph{not} made buffer local by the @ccmode{} style system.
2918@end defvr
2919
2920Two variables are treated specially in the dotted pair list:
2921
2922@table @code
2923@item c-offsets-alist
2924The value is in turn a dotted list on the form
2925
2926(@var{syntactic-symbol} . @var{offset})
2927
2928as described in @ref{Customizing Indentation}. These are passed to
2929@code{c-set-offset} so there is no need to set every syntactic symbol in
2930your style, only those that are different from the inherited style.
2931
2932@item c-special-indent-hook
2933The value is added to @code{c-special-indent-hook} using
2934@code{add-hook}, so any functions already on it are kept. If the value
2935is a list, each element of the list is added with @code{add-hook}.
2936@end table
2937
2938Styles are kept in the @code{c-style-alist} variable, but you
2939should never modify this variable directly. Instead, @ccmode{}
2940provides the function @code{c-add-style} for this purpose.
3a731e1f
MS
2941
2942@defun c-add-style stylename description &optional set-p
2943@findex add-style (c-)
2a15eb73
MS
2944Add or update a style called @var{stylename}, a string.
2945@var{description} is the new style definition in the form described
2946above. If @var{stylename} already exists in @code{c-style-alist} then
2947it is replaced by @var{description}. (Note, this replacement is
2948total. The old style is @emph{not} merged into the new one.)
2949Otherwise, a new style is added. If the optional @var{set-p} is
2950non-@code{nil} then the new style is applied to the current buffer as
2951well.
6bf7aab6
DL
2952
2953The sample @file{.emacs} file provides a concrete example of how a new
2954style can be added and automatically set. @xref{Sample .emacs File}.
3a731e1f
MS
2955@end defun
2956
2957@defvar c-style-alist
2958@vindex style-alist (c-)
2959This is the variable that holds the definitions for the styles. It
2960should not be changed directly; use @code{c-add-style} instead.
2961@end defvar
6bf7aab6 2962
6bf7aab6 2963
d7bd46ed
GM
2964@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2965@node File Styles, , Adding Styles, Styles
2966@comment node-name, next, previous, up
2967@subsection File Styles
3a731e1f 2968@cindex styles, file local
6bf7aab6
DL
2969@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2970
3a731e1f 2971@cindex file local variables
6bf7aab6 2972
3a731e1f
MS
2973The Emacs manual describes how you can customize certain variables on
2974a per-file basis by including a @dfn{file local variable} block at the
2975end of the file. So far, you've only seen a functional interface to
2976@ccmode{} customization, which can't be used there. @ccmode{}
2977provides two variables allow customization of the indentation style on
2978a per-file basis:
6bf7aab6 2979
3a731e1f 2980@defvar c-file-style
6bf7aab6 2981@vindex file-style (c-)
3a731e1f
MS
2982This variable can be set to a style name string. When the file is
2983visited, @ccmode{} will automatically set the file's style to this
2984one using @code{c-set-style}.
2985@end defvar
6bf7aab6 2986
3a731e1f
MS
2987@defvar c-file-offsets
2988@vindex file-offsets (c-)
2989This variable takes an association list similar to what is allowed in
2990@code{c-offsets-alist}. When the file is visited, @ccmode{} will
2991automatically institute these offsets using @code{c-set-offset}.
2992@end defvar
6bf7aab6 2993
c3a2e2d5
RS
2994Note that file style settings (i.e., @code{c-file-style}) are applied
2995before file offset settings (i.e., @code{c-file-offsets}). Also, if
6bf7aab6
DL
2996either of these are set in a file's local variable section, all the
2997style variable values are made local to that buffer.
2998
2999
3000@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
d7bd46ed
GM
3001@node Advanced Customizations, , Styles, Customizing Indentation
3002@comment node-name, next, previous, up
3a731e1f 3003@section Advanced Customizations
6bf7aab6
DL
3004@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3005
3a731e1f
MS
3006For most users, @ccmode{} will support their coding styles with very
3007little need for more advanced customizations. Usually, one of the
3008standard styles (@pxref{Built-in Styles}) will do the trick. At most,
3009perhaps one of the syntactic symbol offsets will need to be tweaked
3010slightly, or maybe @code{c-basic-offset} will need to be changed.
3011However, some styles require a more flexible framework for
3012customization, and one of the real strengths of @ccmode{} is that the
3013syntactic analysis model provides just such a framework. This allows
6bf7aab6
DL
3014you to implement custom indentation calculations for situations not
3015handled by the mode directly.
3016
6bf7aab6
DL
3017@menu
3018* Custom Indentation Functions::
3019* Custom Brace and Colon Hanging::
3a731e1f 3020* Customizing Semicolons and Commas::
6bf7aab6
DL
3021* Other Special Indentations::
3022@end menu
3023
3024@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
d7bd46ed
GM
3025@node Custom Indentation Functions, Custom Brace and Colon Hanging, , Advanced Customizations
3026@comment node-name, next, previous, up
3027@subsection Custom Indentation Functions
3a731e1f 3028@cindex customization, indentation functions
6bf7aab6
DL
3029@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3030
d7bd46ed
GM
3031The most flexible way to customize @ccmode{} is by writing custom
3032indentation functions, and associating them with specific syntactic
3033symbols (@pxref{Syntactic Symbols}). @ccmode{} itself uses indentation
3034functions to provide more sophisticated indentation, for example when
3035lining up C++ stream operator blocks:
3a731e1f 3036
6bf7aab6
DL
3037@example
3038@group
3a731e1f
MS
3039 1: void main(int argc, char**)
3040 2: @{
3041 3: cout << "There were "
3042 4: << argc
3043 5: << "arguments passed to the program"
3044 6: << endl;
3045 7: @}
6bf7aab6
DL
3046@end group
3047@end example
3048
3049In this example, lines 4 through 6 are assigned the @code{stream-op}
3050syntactic symbol. Here, @code{stream-op} has an offset of @code{+}, and
3051with a @code{c-basic-offset} of 2, you can see that lines 4 through 6
3052are simply indented two spaces to the right of line 3. But perhaps we'd
3053like @ccmode{} to be a little more intelligent so that it aligns
3054all the @samp{<<} symbols in lines 3 through 6. To do this, we have
3a731e1f 3055to write a custom indentation function which finds the column of the first
bf247b6e 3056stream operator on the first line of the statement. Here is sample
6bf7aab6 3057lisp code implementing this:
6bf7aab6 3058
3a731e1f 3059@example
6bf7aab6 3060(defun c-lineup-streamop (langelem)
6bf7aab6 3061 (save-excursion
3a731e1f
MS
3062 (goto-char (cdr langelem))
3063 (re-search-forward "<<\\|>>" (c-point 'eol) 'move)
3064 (goto-char (match-beginning 0))
3065 (vector (current-column))))
6bf7aab6 3066@end example
3a731e1f 3067
d7bd46ed 3068Indentation functions take a single argument, which is a syntactic
3a731e1f
MS
3069component cons cell (@pxref{Syntactic Analysis}). The function can
3070return an integer which is added to the running total indentation for
3071the line, or a vector containing an integer which is an absolute
3072column to align to. Usually an absolute column is wanted when
3073aligning to existing text, as in this example.
d7bd46ed
GM
3074
3075The function should return @code{nil} if it's used in a situation where
3a731e1f 3076it doesn't want to make any decision. If the function is used in a list
d7bd46ed
GM
3077expression (@pxref{Customizing Indentation}), that will cause @ccmode{}
3078to go on and check the next entry in the list.
6bf7aab6 3079
6bf7aab6
DL
3080Now, to associate the function @code{c-lineup-streamop} with the
3081@code{stream-op} syntactic symbol, we can add something like the
3082following to our @code{c++-mode-hook}@footnote{It probably makes more
3083sense to add this to @code{c++-mode-hook} than @code{c-mode-common-hook}
d7bd46ed 3084since stream operators are only relevant for C++.}:
6bf7aab6 3085
3a731e1f 3086@example
6bf7aab6 3087(c-set-offset 'stream-op 'c-lineup-streamop)
6bf7aab6
DL
3088@end example
3089
3a731e1f 3090Now the function looks like this after reindenting (using @kbd{C-c
6bf7aab6 3091C-q}):
3a731e1f 3092
6bf7aab6
DL
3093@example
3094@group
3a731e1f
MS
3095 1: void main(int argc, char**)
3096 2: @{
3097 3: cout << "There were "
3098 4: << argc
3099 5: << " arguments passed to the program"
3100 6: << endl;
3101 7: @}
6bf7aab6
DL
3102@end group
3103@end example
3104
6bf7aab6
DL
3105Custom indentation functions can be as simple or as complex as you like,
3106and any syntactic symbol that appears in @code{c-offsets-alist} can have
d7bd46ed 3107a custom indentation function associated with it.
6bf7aab6 3108
d7bd46ed
GM
3109@ccmode{} comes with an extensive set of predefined indentation
3110functions, not all of which are used by the default styles. So there's
3111a good chance the function you want already exists. @xref{Indentation
3112Functions}, for a list of them. If you have written an indentation
3113function that you think is generally useful, you're very welcome to
3114contribute it; please contact @email{bug-cc-mode@@gnu.org}.
6bf7aab6 3115
6bf7aab6
DL
3116
3117@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3a731e1f 3118@node Custom Brace and Colon Hanging, Customizing Semicolons and Commas, Custom Indentation Functions, Advanced Customizations
d7bd46ed
GM
3119@comment node-name, next, previous, up
3120@subsection Custom Brace and Colon Hanging
6bf7aab6
DL
3121@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3122
3123@vindex c-hanging-braces-alist
3124@vindex hanging-braces-alist (c-)
3125Syntactic symbols aren't the only place where you can customize
3126@ccmode{} with the lisp equivalent of callback functions. Brace
3127``hanginess'' can also be determined by custom functions associated with
d7bd46ed 3128syntactic symbols on the @code{c-hanging-braces-alist} style variable.
3a731e1f 3129Remember that @var{action}'s are typically a list containing some
d7bd46ed 3130combination of the symbols @code{before} and @code{after}
3a731e1f 3131(@pxref{Hanging Braces}). However, an @var{action} can also be a
d7bd46ed
GM
3132function which gets called when a brace matching that syntactic symbol
3133is entered.
6bf7aab6 3134
3a731e1f
MS
3135@cindex customization, brace hanging
3136These @var{action} functions are called with two arguments: the
6bf7aab6 3137syntactic symbol for the brace, and the buffer position at which the
3a731e1f 3138brace was inserted. The @var{action} function is expected to return a
d7bd46ed 3139list containing some combination of @code{before} and @code{after},
c3a2e2d5 3140including neither of them (i.e., @code{nil}). This return value has the
d7bd46ed 3141normal brace hanging semantics.
6bf7aab6
DL
3142
3143As an example, @ccmode{} itself uses this feature to dynamically
3144determine the hanginess of braces which close ``do-while''
3145constructs:
6bf7aab6 3146
3a731e1f 3147@example
6bf7aab6
DL
3148void do_list( int count, char** atleast_one_string )
3149@{
3150 int i=0;
3151 do @{
3152 handle_string( atleast_one_string[i] );
3153 i++;
3154 @} while( i < count );
3155@}
6bf7aab6
DL
3156@end example
3157
6bf7aab6
DL
3158@ccmode{} assigns the @code{block-close} syntactic symbol to the
3159brace that closes the @code{do} construct, and normally we'd like the
3160line that follows a @code{block-close} brace to begin on a separate
3161line. However, with ``do-while'' constructs, we want the
3162@code{while} clause to follow the closing brace. To do this, we
3a731e1f 3163associate the @code{block-close} symbol with the @var{action} function
6bf7aab6 3164@code{c-snug-do-while}:
6bf7aab6 3165
3a731e1f 3166@example
6bf7aab6 3167(defun c-snug-do-while (syntax pos)
3a731e1f 3168 "Dynamically calculate brace hanginess for do-while statements."
6bf7aab6
DL
3169 (save-excursion
3170 (let (langelem)
3171 (if (and (eq syntax 'block-close)
3172 (setq langelem (assq 'block-close c-syntactic-context))
3173 (progn (goto-char (cdr langelem))
3174 (if (= (following-char) ?@{)
3175 (forward-sexp -1))
3176 (looking-at "\\<do\\>[^_]")))
3177 '(before)
3178 '(before after)))))
6bf7aab6
DL
3179@end example
3180
3a731e1f
MS
3181@findex c-snug-do-while
3182@findex snug-do-while (c-)
6bf7aab6
DL
3183This function simply looks to see if the brace closes a ``do-while''
3184clause and if so, returns the list @samp{(before)} indicating
3185that a newline should be inserted before the brace, but not after it.
3186In all other cases, it returns the list @samp{(before after)} so
3187that the brace appears on a line by itself.
3188
3a731e1f 3189@defvar c-syntactic-context
6bf7aab6 3190@vindex syntactic-context (c-)
3a731e1f
MS
3191During the call to the indentation or brace hanging @var{action}
3192function, this variable is bound to the full syntactic analysis list.
3193@end defvar
6bf7aab6 3194
3a731e1f
MS
3195@cindex customization, colon hanging
3196@vindex c-hanging-colons-alist
3197@vindex hanging-colons-alist (c-)
6bf7aab6 3198Note that for symmetry, colon hanginess should be customizable by
3a731e1f
MS
3199allowing function symbols as @var{action}s on the
3200@code{c-hanging-colons-alist} style variable. Since no use has actually
d7bd46ed 3201been found for this feature, it isn't currently implemented!
6bf7aab6 3202
6bf7aab6 3203
d7bd46ed 3204@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3a731e1f 3205@node Customizing Semicolons and Commas, Other Special Indentations, Custom Brace and Colon Hanging, Advanced Customizations
d7bd46ed 3206@comment node-name, next, previous, up
3a731e1f
MS
3207@subsection Customizing Semicolons and Commas
3208@cindex customization, semicolon newlines
3209@cindex customization, comma newlines
6bf7aab6
DL
3210@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3211
3a731e1f
MS
3212You can also customize the insertion of newlines after semicolons and
3213commas when the auto-newline minor mode is enabled (@pxref{Minor
3214Modes}).
6bf7aab6 3215
3a731e1f
MS
3216@defopt c-hanging-semi&comma-criteria
3217@vindex hanging-semi&comma-criteria (c-)
3218This style variable takes a list of hooks that get called when a
3219semicolon or comma is inserted. The hooks are called in order without
3220arguments, and are expected to return one of the following values:
6bf7aab6 3221
3a731e1f
MS
3222@table @code
3223@item t
3224A newline is inserted, and no more functions from the list are called.
3225@item stop
3226No more functions from the list are called, but no newline is
3227inserted.
3228@item nil
3229No determination is made, and the next function in the list is called.
3230@end table
6bf7aab6
DL
3231
3232If every function in the list is called without a determination being
3233made, then no newline is added. The default value for this variable is a
3234list containing a single function which inserts newlines only after
c3a2e2d5 3235semicolons which do not appear inside parenthesis lists (i.e., those
6bf7aab6 3236that separate @code{for}-clause statements).
3a731e1f 3237@end defopt
6bf7aab6 3238
3a731e1f 3239@defun c-semi&comma-no-newlines-before-nonblanks
6bf7aab6 3240@findex semi&comma-no-newlines-before-nonblanks (c-)
3a731e1f
MS
3241This is an example of a criteria function, provided by @ccmode{}. It
3242prevents newlines from being inserted after semicolons when there is a
3243non-blank following line. Otherwise, it makes no determination. To
3244use, add this function to the front of the
3245@code{c-hanging-semi&comma-criteria} list.
6bf7aab6
DL
3246
3247@example
6bf7aab6
DL
3248(defun c-semi&comma-no-newlines-before-nonblanks ()
3249 (save-excursion
3250 (if (and (eq last-command-char ?\;)
3251 (zerop (forward-line 1))
3252 (not (looking-at "^[ \t]*$")))
3253 'stop
3254 nil)))
6bf7aab6 3255@end example
3a731e1f 3256@end defun
6bf7aab6 3257
3a731e1f 3258@defun c-semi&comma-inside-parenlist
6bf7aab6 3259@findex semi&comma-inside-parenlist (c-)
3a731e1f 3260@defunx c-semi&comma-no-newlines-for-oneline-inliners
6bf7aab6 3261@findex semi&comma-no-newlines-for-oneline-inliners (c-)
d7bd46ed
GM
3262The function @code{c-semi&comma-inside-parenlist} is what prevents
3263newlines from being inserted inside the parenthesis list of @code{for}
3264statements. In addition to
6bf7aab6
DL
3265@code{c-semi&comma-no-newlines-before-nonblanks} described above,
3266@ccmode{} also comes with the criteria function
3267@code{c-semi&comma-no-newlines-for-oneline-inliners}, which suppresses
3268newlines after semicolons inside one-line inline method definitions
c3a2e2d5 3269(e.g., in C++ or Java).
3a731e1f 3270@end defun
6bf7aab6 3271
6bf7aab6 3272
d7bd46ed 3273@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3a731e1f 3274@node Other Special Indentations, , Customizing Semicolons and Commas, Advanced Customizations
d7bd46ed
GM
3275@comment node-name, next, previous, up
3276@subsection Other Special Indentations
6bf7aab6
DL
3277@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3278
3a731e1f
MS
3279Here are the remaining odds and ends regarding indentation:
3280
3281@defopt c-label-minimum-indentation
6bf7aab6 3282@vindex label-minimum-indentation (c-)
d7bd46ed 3283In @samp{gnu} style (@pxref{Built-in Styles}), a minimum indentation
6bf7aab6 3284is imposed on lines inside top-level constructs. This minimum
3a731e1f 3285indentation is controlled by this style variable. The default value
6bf7aab6 3286is 1.
3a731e1f 3287@end defopt
6bf7aab6 3288
3a731e1f 3289@defopt c-special-indent-hook
6bf7aab6 3290@vindex special-indent-hook (c-)
3a731e1f
MS
3291This style variable is a standard hook variable that is called after
3292every line is indented by @ccmode{}. You can use it to do any special
3293indentation or line adjustments your style dictates, such as adding
3294extra indentation to constructors or destructor declarations in a
3295class definition, etc. Note that you should not change point or mark
c3a2e2d5 3296inside your @code{c-special-indent-hook} functions, i.e., you'll
3a731e1f
MS
3297probably want to wrap your function in a @code{save-excursion}.
3298
3299Setting @code{c-special-indent-hook} in your style definition is
3300handled slightly differently than other variables. In your style
3301definition, you should set the value for @code{c-special-indent-hook}
3302to a function or list of functions, which will be appended to
3303@code{c-special-indent-hook} using @code{add-hook}. That way, the
3304current setting for the buffer local value of
6bf7aab6 3305@code{c-special-indent-hook} won't be overridden.
3a731e1f 3306@end defopt
d7bd46ed 3307
6bf7aab6 3308
d7bd46ed
GM
3309@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3310@node Syntactic Symbols, Indentation Functions, Customizing Indentation, Top
3311@comment node-name, next, previous, up
3a731e1f 3312@chapter Syntactic Symbols
6bf7aab6
DL
3313@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3314
3a731e1f 3315@cindex syntactic symbols, brief list
6bf7aab6
DL
3316@vindex c-offsets-alist
3317@vindex offsets-alist (c-)
6bf7aab6 3318Here is a complete list of the recognized syntactic symbols as described
d7bd46ed
GM
3319in the @code{c-offsets-alist} style variable, along with a brief
3320description. More detailed descriptions follow.
6bf7aab6 3321
d7bd46ed
GM
3322@table @code
3323@item string
3a731e1f 3324Inside a multiline string.
d7bd46ed 3325@item c
3a731e1f 3326Inside a multiline C style block comment.
d7bd46ed
GM
3327@item defun-open
3328Brace that opens a top-level function definition.
3329@item defun-close
3330Brace that closes a top-level function definition.
3331@item defun-block-intro
3332The first line in a top-level defun.
3333@item class-open
3334Brace that opens a class definition.
3335@item class-close
3336Brace that closes a class definition.
3337@item inline-open
3338Brace that opens an in-class inline method.
3339@item inline-close
3340Brace that closes an in-class inline method.
3341@item func-decl-cont
3342The region between a function definition's argument list and the
3343function opening brace (excluding K&R argument declarations). In C, you
3344cannot put anything but whitespace and comments in this region, however
3345in C++ and Java, @code{throws} declarations and other things can appear
3346here.
3347@item knr-argdecl-intro
3348First line of a K&R C argument declaration.
3349@item knr-argdecl
3350Subsequent lines in a K&R C argument declaration.
3351@item topmost-intro
3352The first line in a ``topmost'' definition.
3353@item topmost-intro-cont
3a731e1f
MS
3354Topmost definition continuation lines. This is only used in the parts
3355that aren't covered by other symbols such as @code{func-decl-cont} and
3356@code{knr-argdecl}.
d7bd46ed
GM
3357@item member-init-intro
3358First line in a member initialization list.
3359@item member-init-cont
3360Subsequent member initialization list lines.
3361@item inher-intro
3362First line of a multiple inheritance list.
3363@item inher-cont
3364Subsequent multiple inheritance lines.
3365@item block-open
3366Statement block open brace.
3367@item block-close
3368Statement block close brace.
3369@item brace-list-open
3370Open brace of an enum or static array list.
3371@item brace-list-close
3372Close brace of an enum or static array list.
3373@item brace-list-intro
3374First line in an enum or static array list.
3375@item brace-list-entry
3376Subsequent lines in an enum or static array list.
3377@item brace-entry-open
3378Subsequent lines in an enum or static array list where the line begins
3379with an open brace.
3380@item statement
3381A statement.
3382@item statement-cont
3383A continuation of a statement.
3384@item statement-block-intro
3385The first line in a new statement block.
3386@item statement-case-intro
3387The first line in a case block.
3388@item statement-case-open
3389The first line in a case block that starts with a brace.
3390@item substatement
3391The first line after a conditional or loop construct.
3392@item substatement-open
3393The brace that opens a substatement block.
3a731e1f
MS
3394@item substatement-label
3395The first line after a conditional or loop construct if it's a label.
d7bd46ed 3396@item case-label
3a731e1f 3397A label in a @code{switch} block.
d7bd46ed
GM
3398@item access-label
3399C++ access control label.
3400@item label
3a731e1f 3401Any other label.
d7bd46ed
GM
3402@item do-while-closure
3403The @code{while} line that ends a @code{do}-@code{while} construct.
3404@item else-clause
3405The @code{else} line of an @code{if}-@code{else} construct.
3406@item catch-clause
3407The @code{catch} or @code{finally} (in Java) line of a
3408@code{try}-@code{catch} construct.
3409@item comment-intro
3410A line containing only a comment introduction.
3411@item arglist-intro
3412The first line in an argument list.
3413@item arglist-cont
3414Subsequent argument list lines when no arguments follow on the same line
6b1f5814 3415as the arglist opening paren.
d7bd46ed
GM
3416@item arglist-cont-nonempty
3417Subsequent argument list lines when at least one argument follows on the
3418same line as the arglist opening paren.
3419@item arglist-close
3420The solo close paren of an argument list.
3421@item stream-op
3422Lines continuing a stream operator (C++ only).
3423@item inclass
3424The line is nested inside a class definition.
3425@item cpp-macro
3a731e1f
MS
3426The start of a preprocessor macro definition.
3427@item cpp-define-intro
3428The first line inside a multiline preproprocessor macro if
3429@code{c-syntactic-indentation-in-macros} is set.
d7bd46ed 3430@item cpp-macro-cont
3a731e1f
MS
3431All lines inside multiline preprocessor macros if
3432@code{c-syntactic-indentation-in-macros} is @code{nil}.
d7bd46ed
GM
3433@item friend
3434A C++ friend declaration.
3435@item objc-method-intro
3a731e1f 3436The first line of an Objective-C method definition.
d7bd46ed 3437@item objc-method-args-cont
3a731e1f 3438Lines continuing an Objective-C method definition.
d7bd46ed
GM
3439@item objc-method-call-cont
3440Lines continuing an Objective-C method call.
3441@item extern-lang-open
c3a2e2d5 3442Brace that opens an @code{extern} block (e.g., @code{extern "C" @{...@}}).
d7bd46ed 3443@item extern-lang-close
3a731e1f 3444Brace that closes an @code{extern} block.
d7bd46ed 3445@item inextern-lang
3a731e1f
MS
3446Analogous to @code{inclass} syntactic symbol, but used inside
3447@code{extern} blocks.
d7bd46ed 3448@item namespace-open
3a731e1f
MS
3449@itemx namespace-close
3450@itemx innamespace
3451These are analogous to the three @code{extern-lang} symbols above, but
3452are returned for C++ namespace blocks.
3453@item module-open
3454@itemx module-close
3455@itemx inmodule
3456Analogous to the above, but for CORBA IDL @code{module} blocks.
3457@item composition-open
3458@itemx composition-close
3459@itemx incomposition
3460Analogous to the above, but for CORBA CIDL @code{composition} blocks.
d7bd46ed
GM
3461@item template-args-cont
3462C++ template argument list continuations.
3463@item inlambda
3464Analogous to @code{inclass} syntactic symbol, but used inside lambda
c3a2e2d5 3465(i.e., anonymous) functions. Only used in Pike mode.
d7bd46ed 3466@item lambda-intro-cont
c3a2e2d5 3467Lines continuing the header of a lambda function, i.e., between the
d7bd46ed
GM
3468@code{lambda} keyword and the function body. Only used in Pike mode.
3469@item inexpr-statement
2a15eb73
MS
3470A statement block inside an expression. The gcc C and C++ extension for
3471this is recognized. It's also used for the special functions that take
3472a statement block as an argument in Pike.
d7bd46ed
GM
3473@item inexpr-class
3474A class definition inside an expression. This is used for anonymous
3475classes in Java. It's also used for anonymous array initializers in
3476Java.
3477@end table
6bf7aab6 3478
3a731e1f
MS
3479@ssindex -open symbols
3480@ssindex -close symbols
6bf7aab6
DL
3481Most syntactic symbol names follow a general naming convention. When a
3482line begins with an open or close brace, the syntactic symbol will
3483contain the suffix @code{-open} or @code{-close} respectively.
3484
3a731e1f
MS
3485@ssindex -intro symbols
3486@ssindex -cont symbols
3487@ssindex -block-intro symbols
6bf7aab6
DL
3488Usually, a distinction is made between the first line that introduces a
3489construct and lines that continue a construct, and the syntactic symbols
3490that represent these lines will contain the suffix @code{-intro} or
3491@code{-cont} respectively. As a sub-classification of this scheme, a
3492line which is the first of a particular brace block construct will
3493contain the suffix @code{-block-intro}.
3494
6bf7aab6
DL
3495Let's look at some examples to understand how this works. Remember that
3496you can check the syntax of any line by using @kbd{C-c C-s}.
6bf7aab6 3497
3a731e1f
MS
3498@example
3499 1: void
3500 2: swap( int& a, int& b )
3501 3: @{
3502 4: int tmp = a;
3503 5: a = b;
3504 6: b = tmp;
3505 7: int ignored =
3506 8: a + b;
3507 9: @}
6bf7aab6
DL
3508@end example
3509
3a731e1f
MS
3510@ssindex topmost-intro
3511@ssindex topmost-intro-cont
3512@ssindex defun-open
3513@ssindex defun-close
3514@ssindex defun-block-intro
6bf7aab6
DL
3515Line 1 shows a @code{topmost-intro} since it is the first line that
3516introduces a top-level construct. Line 2 is a continuation of the
3517top-level construct introduction so it has the syntax
3518@code{topmost-intro-cont}. Line 3 shows a @code{defun-open} since it is
d7bd46ed
GM
3519the brace that opens a top-level function definition. Line 9 is the
3520corresponding
6bf7aab6 3521@code{defun-close} since it contains the brace that closes the top-level
c3a2e2d5 3522function definition. Line 4 is a @code{defun-block-intro}, i.e., it is
6bf7aab6
DL
3523the first line of a brace-block, enclosed in a
3524top-level function definition.
3525
3a731e1f
MS
3526@ssindex statement
3527@ssindex statement-cont
6bf7aab6
DL
3528Lines 5, 6, and 7 are all given @code{statement} syntax since there
3529isn't much special about them. Note however that line 8 is given
3530@code{statement-cont} syntax since it continues the statement begun
3531on the previous line.
3532
3533Here's another example, which illustrates some C++ class syntactic
3534symbols:
6bf7aab6 3535
3a731e1f
MS
3536@example
3537 1: class Bass
3538 2: : public Guitar,
3539 3: public Amplifiable
3540 4: @{
3541 5: public:
3542 6: Bass()
3543 7: : eString( new BassString( 0.105 )),
3544 8: aString( new BassString( 0.085 )),
3545 9: dString( new BassString( 0.065 )),
354610: gString( new BassString( 0.045 ))
354711: @{
354812: eString.tune( 'E' );
354913: aString.tune( 'A' );
355014: dString.tune( 'D' );
355115: gString.tune( 'G' );
355216: @}
355317: friend class Luthier;
355418: @};
6bf7aab6
DL
3555@end example
3556
3a731e1f
MS
3557@ssindex class-open
3558@ssindex class-close
6bf7aab6
DL
3559As in the previous example, line 1 has the @code{topmost-intro} syntax.
3560Here however, the brace that opens a C++ class definition on line 4 is
3561assigned the @code{class-open} syntax. Note that in C++, classes,
3562structs, and unions are essentially equivalent syntactically (and are
3563very similar semantically), so replacing the @code{class} keyword in the
3564example above with @code{struct} or @code{union} would still result in a
3565syntax of @code{class-open} for line 4 @footnote{This is the case even
3566for C and Objective-C. For consistency, structs in all supported
3567languages are syntactically equivalent to classes. Note however that
3568the keyword @code{class} is meaningless in C and Objective-C.}.
3569Similarly, line 18 is assigned @code{class-close} syntax.
3570
3a731e1f
MS
3571@ssindex inher-intro
3572@ssindex inher-cont
6bf7aab6
DL
3573Line 2 introduces the inheritance list for the class so it is assigned
3574the @code{inher-intro} syntax, and line 3, which continues the
3575inheritance list is given @code{inher-cont} syntax.
3576
3a731e1f
MS
3577@ssindex access-label
3578@ssindex inclass
6bf7aab6
DL
3579Hitting @kbd{C-c C-s} on line 5 shows the following analysis:
3580
3581@example
3a731e1f 3582((inclass 58) (access-label 58))
6bf7aab6
DL
3583@end example
3584
3585@noindent
3586The primary syntactic symbol for this line is @code{access-label} as
3587this a label keyword that specifies access protection in C++. However,
3588because this line is also a top-level construct inside a class
3589definition, the analysis actually shows two syntactic symbols. The
3590other syntactic symbol assigned to this line is @code{inclass}.
3591Similarly, line 6 is given both @code{inclass} and @code{topmost-intro}
3592syntax:
3593
3594@example
3a731e1f 3595((inclass 58) (topmost-intro 60))
6bf7aab6
DL
3596@end example
3597
3a731e1f
MS
3598@ssindex member-init-intro
3599@ssindex member-init-cont
6bf7aab6
DL
3600Line 7 introduces a C++ member initialization list and as such is given
3601@code{member-init-intro} syntax. Note that in this case it is
3602@emph{not} assigned @code{inclass} since this is not considered a
3603top-level construct. Lines 8 through 10 are all assigned
3604@code{member-init-cont} since they continue the member initialization
3605list started on line 7.
3606
3607@cindex in-class inline methods
3a731e1f
MS
3608@ssindex inline-open
3609@ssindex inline-close
6bf7aab6
DL
3610Line 11's analysis is a bit more complicated:
3611
3612@example
3a731e1f 3613((inclass 58) (inline-open))
6bf7aab6
DL
3614@end example
3615
3616This line is assigned a syntax of both @code{inline-open} and
3617@code{inclass} because it opens an @dfn{in-class} C++ inline method
3618definition. This is distinct from, but related to, the C++ notion of an
3619inline function in that its definition occurs inside an enclosing class
3620definition, which in C++ implies that the function should be inlined.
3a731e1f 3621However, if the definition of the @code{Bass} constructor appeared
6bf7aab6
DL
3622outside the class definition, the construct would be given the
3623@code{defun-open} syntax, even if the keyword @code{inline} appeared
3624before the method name, as in:
6bf7aab6 3625
3a731e1f
MS
3626@example
3627 1: class Bass
3628 2: : public Guitar,
3629 3: public Amplifiable
3630 4: @{
3631 5: public:
3632 6: Bass();
3633 7: @};
3634 8:
3635 9: inline
363610: Bass::Bass()
363711: : eString( new BassString( 0.105 )),
363812: aString( new BassString( 0.085 )),
363913: dString( new BassString( 0.065 )),
364014: gString( new BassString( 0.045 ))
364115: @{
364216: eString.tune( 'E' );
364317: aString.tune( 'A' );
364418: dString.tune( 'D' );
364519: gString.tune( 'G' );
364620: @}
6bf7aab6
DL
3647@end example
3648
3a731e1f 3649@ssindex friend
6bf7aab6
DL
3650Returning to the previous example, line 16 is given @code{inline-close}
3651syntax, while line 12 is given @code{defun-block-open} syntax, and lines
365213 through 15 are all given @code{statement} syntax. Line 17 is
3653interesting in that its syntactic analysis list contains three
3654elements:
3655
3656@example
3a731e1f 3657((inclass 58) (topmost-intro 380) (friend))
6bf7aab6
DL
3658@end example
3659
3660The @code{friend} syntactic symbol is a modifier that typically does not
3661have a relative buffer position.
3662
3663Template definitions introduce yet another syntactic symbol:
3664
3665@example
3a731e1f
MS
3666 1: ThingManager <int,
3667 2: Framework::Callback *,
3668 3: Mutex> framework_callbacks;
6bf7aab6
DL
3669@end example
3670
3671Here, line 1 is analyzed as a @code{topmost-intro}, but lines 2 and 3
3672are both analyzed as @code{template-args-cont} lines.
3673
3674Here is another (totally contrived) example which illustrates how syntax
3675is assigned to various conditional constructs:
6bf7aab6 3676
3a731e1f
MS
3677@example
3678 1: void spam( int index )
3679 2: @{
3680 3: for( int i=0; i<index; i++ )
3681 4: @{
3682 5: if( i == 10 )
3683 6: do_something_special();
3684 7: else
3685 8: silly_label:
3686 9: do_something( i );
368710: @}
368811: do @{
368912: another_thing( i-- );
369013: @}
369114: while( i > 0 );
369215: @}
6bf7aab6
DL
3693@end example
3694
6bf7aab6
DL
3695Only the lines that illustrate new syntactic symbols will be discussed.
3696
3a731e1f
MS
3697@ssindex substatement-open
3698@ssindex substatement-block-intro
3699@ssindex block-close
6bf7aab6
DL
3700Line 4 has a brace which opens a conditional's substatement block. It
3701is thus assigned @code{substatement-open} syntax, and since line 5 is
3702the first line in the substatement block, it is assigned
3a731e1f
MS
3703@code{substatement-block-intro} syntax. Line 10 contains the brace that
3704closes the inner substatement block, and is therefore given the syntax
3705@code{block-close}. Line 13 is treated the same way.
3706
3707@ssindex substatement
3708Lines 6 and 9 are also substatements of conditionals, but since they
3709don't start blocks they are given @code{substatement} syntax
3710instead of @code{substatement-open}.
3711
3712@ssindex substatement-label
3713Line 8 contains a label, which is normally given @code{label} syntax.
3714This one is however a bit special since it's between a conditional and
3715its substatement. It's analyzed as @code{substatement-label} to let you
3716handle this rather odd case differently from normal labels.
3717
3718@ssindex else-clause
3719@ssindex catch-clause
3720Line 7 start with an @code{else} that matches the @code{if} statement on
3721line 5. It is therefore given the @code{else-clause} syntax and is
3722anchored on the matching @code{if}. The @code{try}-@code{catch}
3723constructs in C++ and Java are treated this way too, except that
3724@code{catch} and (in Java) @code{finally}, are marked with
3725@code{catch-clause}.
3726
3727@ssindex do-while-closure
3728The @code{while} construct on line 14 that closes a @code{do}
3729conditional is given the special syntax @code{do-while-closure} if it
3730appears on a line by itself. Note that if the @code{while} appeared on
3731the same line as the preceding close brace, that line would still have
3732@code{block-close} syntax.
6bf7aab6
DL
3733
3734Switch statements have their own set of syntactic symbols. Here's an
3735example:
6bf7aab6 3736
3a731e1f
MS
3737@example
3738 1: void spam( enum Ingredient i )
3739 2: @{
3740 3: switch( i ) @{
3741 4: case Ham:
3742 5: be_a_pig();
3743 6: break;
3744 7: case Salt:
3745 8: drink_some_water();
3746 9: break;
374710: default:
374811: @{
374912: what_is_it();
375013: break;
375114: @}
375215: @}
375314: @}
6bf7aab6
DL
3754@end example
3755
3a731e1f
MS
3756@ssindex case-label
3757@ssindex statement-case-intro
3758@ssindex statement-case-open
6bf7aab6
DL
3759Here, lines 4, 7, and 10 are all assigned @code{case-label} syntax,
3760while lines 5 and 8 are assigned @code{statement-case-intro}. Line 11
3761is treated slightly differently since it contains a brace that opens a
3762block --- it is given @code{statement-case-open} syntax.
3763
3764@cindex brace lists
3765There are a set of syntactic symbols that are used to recognize
3766constructs inside of brace lists. A brace list is defined as an
3767@code{enum} or aggregate initializer list, such as might statically
d7bd46ed
GM
3768initialize an array of structs. The three special aggregate constructs
3769in Pike, @code{(@{ @})}, @code{([ ])} and @code{(< >)}, are treated as
3770brace lists too. An example:
6bf7aab6 3771
3a731e1f
MS
3772@example
3773 1: static char* ingredients[] =
3774 2: @{
3775 3: "Ham",
3776 4: "Salt",
3777 5: NULL
3778 6: @};
6bf7aab6
DL
3779@end example
3780
3a731e1f
MS
3781@ssindex brace-list-open
3782@ssindex brace-list-intro
3783@ssindex brace-list-close
3784@ssindex brace-list-entry
6bf7aab6
DL
3785Following convention, line 2 in this example is assigned
3786@code{brace-list-open} syntax, and line 3 is assigned
3787@code{brace-list-intro} syntax. Likewise, line 6 is assigned
3788@code{brace-list-close} syntax. Lines 4 and 5 however, are assigned
3789@code{brace-list-entry} syntax, as would all subsequent lines in this
3790initializer list.
3791
3a731e1f 3792@ssindex brace-entry-open
d7bd46ed
GM
3793Your static initializer might be initializing nested structures, for
3794example:
d7bd46ed 3795
3a731e1f
MS
3796@example
3797 1: struct intpairs[] =
3798 2: @{
3799 3: @{ 1, 2 @},
3800 4: @{
3801 5: 3,
3802 6: 4
3803 7: @}
3804 8: @{ 1,
3805 9: 2 @},
380610: @{ 3, 4 @}
380711: @};
d7bd46ed
GM
3808@end example
3809
3810Here, you've already seen the analysis of lines 1, 2, 3, and 11. On
3811line 4, things get interesting; this line is assigned
3812@code{brace-entry-open} syntactic symbol because it's a bracelist entry
3813line that starts with an open brace. Lines 5 and 6 (and line 9) are
3814pretty standard, and line 7 is a @code{brace-list-close} as you'd
3815expect. Once again, line 8 is assigned as @code{brace-entry-open} as is
3816line 10.
3817
6bf7aab6
DL
3818External language definition blocks also have their own syntactic
3819symbols. In this example:
6bf7aab6 3820
3a731e1f
MS
3821@example
3822 1: extern "C"
3823 2: @{
3824 3: int thing_one( int );
3825 4: int thing_two( double );
3826 5: @}
6bf7aab6
DL
3827@end example
3828
3a731e1f
MS
3829@ssindex extern-lang-open
3830@ssindex extern-lang-close
3831@ssindex inextern-lang
3832@ssindex inclass
6bf7aab6
DL
3833@noindent
3834line 2 is given the @code{extern-lang-open} syntax, while line 5 is given
3835the @code{extern-lang-close} syntax. The analysis for line 3 yields:
6bf7aab6 3836
6bf7aab6 3837@example
3a731e1f 3838((inextern-lang) (topmost-intro 14))
6bf7aab6
DL
3839@end example
3840
6bf7aab6 3841@noindent
3a731e1f
MS
3842where @code{inextern-lang} is a modifier similar in purpose to
3843@code{inclass}.
3844
3845There are various other top level blocks like @code{extern}, and they
3846are all treated in the same way except that the symbols are named after
c3a2e2d5 3847the keyword that introduces the block. e.g., C++ namespace blocks get
3a731e1f
MS
3848the three symbols @code{namespace-open}, @code{namespace-close} and
3849@code{innamespace}. The currently recognized top level blocks are:
3850
3851@table @asis
3852@item @code{extern-lang-open}, @code{extern-lang-close}, @code{inextern-lang}
3853@code{extern} blocks in C and C++.@footnote{These should logically be
3854named @code{extern-open}, @code{extern-close} and @code{inextern}, but
3855that isn't the case for historical reasons.}
3856
3857@item @code{namespace-open}, @code{namespace-close}, @code{innamespace}
3858@ssindex namespace-open
3859@ssindex namespace-close
3860@ssindex innamespace
3861@code{namespace} blocks in C++.
3862
3863@item @code{module-open}, @code{module-close}, @code{inmodule}
3864@ssindex module-open
3865@ssindex module-close
3866@ssindex inmodule
3867@code{module} blocks in CORBA IDL.
3868
3869@item @code{composition-open}, @code{composition-close}, @code{incomposition}
3870@ssindex composition-open
3871@ssindex composition-close
3872@ssindex incomposition
3873@code{composition} blocks in CORBA CIDL.
3874@end table
6bf7aab6
DL
3875
3876A number of syntactic symbols are associated with parenthesis lists,
3877a.k.a argument lists, as found in function declarations and function
3878calls. This example illustrates these:
6bf7aab6 3879
3a731e1f
MS
3880@example
3881 1: void a_function( int line1,
3882 2: int line2 );
bf247b6e 3883 3:
3a731e1f
MS
3884 4: void a_longer_function(
3885 5: int line1,
3886 6: int line2
3887 7: );
bf247b6e 3888 8:
3a731e1f
MS
3889 9: void call_them( int line1, int line2 )
389010: @{
389111: a_function(
389212: line1,
389313: line2
389414: );
bf247b6e 389515:
3a731e1f
MS
389616: a_longer_function( line1,
389717: line2 );
389818: @}
6bf7aab6
DL
3899@end example
3900
3a731e1f
MS
3901@ssindex arglist-intro
3902@ssindex arglist-close
6bf7aab6
DL
3903Lines 5 and 12 are assigned @code{arglist-intro} syntax since they are
3904the first line following the open parenthesis, and lines 7 and 14 are
3905assigned @code{arglist-close} syntax since they contain the parenthesis
3906that closes the argument list.
3907
3a731e1f
MS
3908@ssindex arglist-cont-nonempty
3909@ssindex arglist-cont
6bf7aab6
DL
3910Lines that continue argument lists can be assigned one of two syntactic
3911symbols. For example, Lines 2 and 17
3912are assigned @code{arglist-cont-nonempty} syntax. What this means
3913is that they continue an argument list, but that the line containing the
3914parenthesis that opens the list is @emph{not empty} following the open
3915parenthesis. Contrast this against lines 6 and 13 which are assigned
3916@code{arglist-cont} syntax. This is because the parenthesis that opens
3917their argument lists is the last character on that line.
3918
3919Note that there is no @code{arglist-open} syntax. This is because any
3920parenthesis that opens an argument list, appearing on a separate line,
3921is assigned the @code{statement-cont} syntax instead.
3922
3923A few miscellaneous syntactic symbols that haven't been previously
3924covered are illustrated by this C++ example:
6bf7aab6 3925
3a731e1f
MS
3926@example
3927 1: void Bass::play( int volume )
3928 2: const
3929 3: @{
3930 4: /* this line starts a multiline
3931 5: * comment. This line should get `c' syntax */
bf247b6e 3932 6:
3a731e1f
MS
3933 7: char* a_multiline_string = "This line starts a multiline \
3934 8: string. This line should get `string' syntax.";
bf247b6e 3935 9:
3a731e1f
MS
393610: note:
393711: @{
393812: #ifdef LOCK
393913: Lock acquire();
394014: #endif // LOCK
394115: slap_pop();
394216: cout << "I played "
394317: << "a note\n";
394418: @}
394519: @}
6bf7aab6
DL
3946@end example
3947
6bf7aab6
DL
3948The lines to note in this example include:
3949
3950@itemize @bullet
6bf7aab6 3951@item
3a731e1f 3952@ssindex func-decl-cont
f214c025 3953Line 2 is assigned the @code{func-decl-cont} syntax.
6bf7aab6 3954
6bf7aab6 3955@item
3a731e1f 3956@ssindex comment-intro
f214c025
GM
3957Line 4 is assigned both @code{defun-block-intro} @emph{and}
3958@code{comment-intro} syntax.
6bf7aab6 3959
6bf7aab6 3960@item
3a731e1f 3961@ssindex c
f214c025 3962Line 5 is assigned @code{c} syntax.
6bf7aab6
DL
3963
3964@item
3965@cindex syntactic whitespace
f214c025 3966Line 6 which, even though it contains nothing but whitespace, is
6bf7aab6
DL
3967assigned @code{defun-block-intro}. Note that the appearance of the
3968comment on lines 4 and 5 do not cause line 6 to be assigned
3969@code{statement} syntax because comments are considered to be
3970@dfn{syntactic whitespace}, which are ignored when analyzing
f214c025 3971code.
6bf7aab6 3972
6bf7aab6 3973@item
3a731e1f 3974@ssindex string
f214c025 3975Line 8 is assigned @code{string} syntax.
6bf7aab6 3976
6bf7aab6 3977@item
3a731e1f 3978@ssindex label
f214c025 3979Line 10 is assigned @code{label} syntax.
6bf7aab6 3980
6bf7aab6 3981@item
3a731e1f 3982@ssindex block-open
f214c025 3983Line 11 is assigned @code{block-open} syntax.
6bf7aab6 3984
6bf7aab6 3985@item
3a731e1f 3986@ssindex cpp-macro
f214c025
GM
3987Lines 12 and 14 are assigned @code{cpp-macro} syntax in addition to the
3988normal syntactic symbols (@code{statement-block-intro} and
3989@code{statement}, respectively). Normally @code{cpp-macro} is
3990configured to cancel out the normal syntactic context to make all
3991preprocessor directives stick to the first column, but that's easily
3992changed if you want preprocessor directives to be indented like the rest
3993of the code.
6bf7aab6 3994
6bf7aab6 3995@item
3a731e1f 3996@ssindex stream-op
f214c025 3997Line 17 is assigned @code{stream-op} syntax.
6bf7aab6
DL
3998@end itemize
3999
3a731e1f 4000@cindex multiline macros
6bf7aab6 4001@cindex syntactic whitespace
3a731e1f
MS
4002@ssindex cpp-define-intro
4003Multiline preprocessor macro definitions are normally handled just like
c3a2e2d5 4004other code, i.e., the lines inside them are indented according to the
3a731e1f 4005syntactic analysis of the preceding lines inside the macro. The first
c3a2e2d5 4006line inside a macro definition (i.e., the line after the starting line of
3a731e1f 4007the cpp directive itself) gets @code{cpp-define-intro}. In this example:
6bf7aab6 4008
3a731e1f
MS
4009@example
4010 1: #define LIST_LOOP(cons, listp) \
4011 2: for (cons = listp; !NILP (cons); cons = XCDR (cons)) \
4012 3: if (!CONSP (cons)) \
4013 4: signal_error ("Invalid list format", listp); \
4014 5: else
6bf7aab6 4015@end example
3a731e1f 4016
6bf7aab6 4017@noindent
3a731e1f
MS
4018line 1 is given the syntactic symbol @code{cpp-macro}. The first line
4019of a cpp directive is always given that symbol. Line 2 is given
4020@code{cpp-define-intro}, so that you can give the macro body as a whole
4021some extra indentation. Lines 3 through 5 are then analyzed as normal
c3a2e2d5 4022code, i.e., @code{substatement} on lines 3 and 4, and @code{else-clause}
3a731e1f
MS
4023on line 5.
4024
4025The syntactic analysis inside macros can be turned off with
4026@code{c-syntactic-indentation-in-macros}. In that case, lines 2 through
40275 would all be given @code{cpp-macro-cont} with a relative buffer
4028position pointing to the @code{#} which starts the cpp
4029directive@footnote{This is how @ccmode{} 5.28 and earlier analyzed
4030macros.}.
4031
4032@xref{Macro Handling}, for more info about the treatment of macros.
6bf7aab6
DL
4033
4034In Objective-C buffers, there are three additional syntactic symbols
4035assigned to various message calling constructs. Here's an example
4036illustrating these:
6bf7aab6 4037
3a731e1f
MS
4038@example
4039 1: - (void)setDelegate:anObject
4040 2: withStuff:stuff
4041 3: @{
4042 4: [delegate masterWillRebind:self
4043 5: toDelegate:anObject
4044 6: withExtraStuff:stuff];
4045 7: @}
6bf7aab6
DL
4046@end example
4047
3a731e1f
MS
4048@ssindex objc-method-intro
4049@ssindex objc-method-args-cont
4050@ssindex objc-method-call-cont
6bf7aab6
DL
4051Here, line 1 is assigned @code{objc-method-intro} syntax, and line 2 is
4052assigned @code{objc-method-args-cont} syntax. Lines 5 and 6 are both
4053assigned @code{objc-method-call-cont} syntax.
4054
d7bd46ed
GM
4055Java has a concept of anonymous classes, which may look something like
4056this:
d7bd46ed 4057
3a731e1f
MS
4058@example
4059 1: public void watch(Observable o) @{
4060 2: o.addObserver(new Observer() @{
4061 3: public void update(Observable o, Object arg) @{
4062 4: history.addElement(arg);
4063 5: @}
4064 6: @});
4065 7: @}
d7bd46ed
GM
4066@end example
4067
3a731e1f 4068@ssindex inexpr-class
d7bd46ed
GM
4069The brace following the @code{new} operator opens the anonymous class.
4070Lines 3 and 6 are assigned the @code{inexpr-class} syntax, besides the
4071@code{inclass} symbol used in normal classes. Thus, the class will be
4072indented just like a normal class, with the added indentation given to
4073@code{inexpr-class}.
4074
4075There are a few occasions where a statement block may be used inside an
2a15eb73
MS
4076expression. One is in C or C++ code using the gcc extension for this,
4077e.g:
d7bd46ed 4078
3a731e1f
MS
4079@example
4080 1: int res = (@{
4081 2: int y = foo (); int z;
4082 3: if (y > 0) z = y; else z = - y;
4083 4: z;
4084 5: @});
d7bd46ed
GM
4085@end example
4086
3a731e1f 4087@ssindex inexpr-statement
d7bd46ed
GM
4088Lines 2 and 5 get the @code{inexpr-statement} syntax, besides the
4089symbols they'd get in a normal block. Therefore, the indentation put on
4090@code{inexpr-statement} is added to the normal statement block
4091indentation.
4092
4093In Pike code, there are a few other situations where blocks occur inside
4094statements, as illustrated here:
d7bd46ed 4095
3a731e1f
MS
4096@example
4097 1: array itgob()
4098 2: @{
4099 3: string s = map (backtrace()[-2][3..],
4100 4: lambda
4101 5: (mixed arg)
4102 6: @{
4103 7: return sprintf ("%t", arg);
4104 8: @}) * ", " + "\n";
4105 9: return catch @{
410610: write (s + "\n");
410711: @};
410812: @}
d7bd46ed
GM
4109@end example
4110
3a731e1f
MS
4111@ssindex inlambda
4112@ssindex lambda-intro-cont
d7bd46ed
GM
4113Lines 4 through 8 contain a lambda function, which @ccmode{} recognizes
4114by the @code{lambda} keyword. If the function argument list is put
4115on a line of its own, as in line 5, it gets the @code{lambda-intro-cont}
4116syntax. The function body is handled as an inline method body, with the
4117addition of the @code{inlambda} syntactic symbol. This means that line
41186 gets @code{inlambda} and @code{inline-open}, and line 8 gets
4119@code{inline-close}@footnote{You might wonder why it doesn't get
4120@code{inlambda} too. It's because the closing brace is relative to the
4121opening brace, which stands on its own line in this example. If the
4122opening brace was hanging on the previous line, then the closing brace
4123would get the @code{inlambda} syntax too to be indented correctly.}.
4124
3a731e1f 4125@ssindex inexpr-statement
d7bd46ed
GM
4126On line 9, @code{catch} is a special function taking a statement block
4127as its argument. The block is handled as an in-expression statement
4128with the @code{inexpr-statement} syntax, just like the gcc extended C
4129example above. The other similar special function, @code{gauge}, is
4130handled like this too.
4131
3a731e1f
MS
4132@ssindex knr-argdecl-intro
4133@ssindex knr-argdecl
6bf7aab6
DL
4134Two other syntactic symbols can appear in old style, non-prototyped C
4135code @footnote{a.k.a. K&R C, or Kernighan & Ritchie C}:
6bf7aab6 4136
3a731e1f
MS
4137@example
4138 1: int add_three_integers(a, b, c)
4139 2: int a;
4140 3: int b;
4141 4: int c;
4142 5: @{
4143 6: return a + b + c;
4144 7: @}
6bf7aab6
DL
4145@end example
4146
4147Here, line 2 is the first line in an argument declaration list and so is
4148given the @code{knr-argdecl-intro} syntactic symbol. Subsequent lines
c3a2e2d5 4149(i.e., lines 3 and 4 in this example), are given @code{knr-argdecl}
6bf7aab6
DL
4150syntax.
4151
d7bd46ed 4152
6bf7aab6 4153@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3a731e1f 4154@node Indentation Functions, AWK Mode, Syntactic Symbols, Top
d7bd46ed 4155@comment node-name, next, previous, up
3a731e1f 4156@chapter Indentation Functions
d7bd46ed
GM
4157@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4158
3a731e1f
MS
4159@cindex indentation function
4160@cindex line-up function
4161Often there are cases when a simple offset setting on a syntactic
4162symbol isn't enough to get the desired indentation. Therefore, it's
4163also possible to use an @dfn{indentation function} (a.k.a. @dfn{line-up
4164function}) for a syntactic symbol.
d7bd46ed
GM
4165
4166@ccmode{} comes with many predefined indentation functions for common
4167situations. If none of these does what you want, you can write your
4168own, see @ref{Custom Indentation Functions}. If you do, it's probably a
4169good idea to start working from one of these predefined functions, they
4170can be found in the file @file{cc-align.el}.
4171
4172For every function below there is a ``works with'' list that indicates
4173which syntactic symbols the function is intended to be used with.
4174
4175@macro workswith
cb7f2e96 4176@emph{Works with:@ }
d7bd46ed
GM
4177@end macro
4178@ifinfo
4179@unmacro workswith
4180@macro workswith
4181Works with:
4182@end macro
4183@end ifinfo
4184
3a731e1f
MS
4185@macro sssTBasicOffset
4186<--> @i{c-basic-offset}@c
4187@end macro
d7bd46ed 4188
3a731e1f
MS
4189@macro sssTsssTBasicOffset
4190<--><--> @i{c-basic-offset}@c
4191@end macro
d7bd46ed 4192
3a731e1f
MS
4193@macro hereFn{func}
4194<- @i{\func\}@c
4195@end macro
d7bd46ed 4196
3a731e1f
MS
4197@c The TeX backend seems to insert extra spaces around the argument. :P
4198@iftex
4199@unmacro hereFn
4200@macro hereFn{func}
4201<-@i{\func\}@c
4202@end macro
4203@end iftex
d7bd46ed 4204
3a731e1f 4205@comment ------------------------------------------------------------
d7bd46ed 4206
3a731e1f
MS
4207@defun c-indent-one-line-block
4208@findex indent-one-line-block (c-)
4209Indent a one line block @code{c-basic-offset} extra. E.g:
d7bd46ed 4210
d7bd46ed
GM
4211@example
4212@group
3a731e1f
MS
4213if (n > 0)
4214 @{m+=n; n=0;@} @hereFn{c-indent-one-line-block}
4215@sssTBasicOffset{}
d7bd46ed
GM
4216@end group
4217@end example
3a731e1f 4218
d7bd46ed
GM
4219@noindent
4220and
3a731e1f 4221
d7bd46ed
GM
4222@example
4223@group
3a731e1f
MS
4224if (n > 0)
4225@{ @hereFn{c-indent-one-line-block}
4226 m+=n; n=0;
4227@}
d7bd46ed
GM
4228@end group
4229@end example
4230
3a731e1f
MS
4231The block may be surrounded by any kind of parenthesis characters.
4232@code{nil} is returned if the line doesn't start with a one line block,
4233which makes the function usable in list expressions.
d7bd46ed 4234
3a731e1f
MS
4235@workswith Almost all syntactic symbols, but most useful on the
4236@code{-open} symbols.
4237@end defun
d7bd46ed 4238
3a731e1f
MS
4239@comment ------------------------------------------------------------
4240
4241@defun c-indent-multi-line-block
4242@findex indent-multi-line-block (c-)
4243Indent a multiline block @code{c-basic-offset} extra. E.g:
d7bd46ed 4244
f214c025
GM
4245@example
4246@group
3a731e1f 4247int *foo[] = @{
bf247b6e 4248 NULL,
3a731e1f 4249 @{17@}, @hereFn{c-indent-multi-line-block}
f214c025
GM
4250@end group
4251@end example
3a731e1f 4252
f214c025
GM
4253@noindent
4254and
3a731e1f 4255
f214c025
GM
4256@example
4257@group
3a731e1f
MS
4258int *foo[] = @{
4259 NULL,
4260 @{ @hereFn{c-indent-multi-line-block}
4261 17
4262 @},
4263 @sssTBasicOffset{}
4264@end group
4265@end example
f214c025 4266
3a731e1f
MS
4267The block may be surrounded by any kind of parenthesis characters.
4268@code{nil} is returned if the line doesn't start with a multiline
4269block, which makes the function usable in list expressions.
4270
4271@workswith Almost all syntactic symbols, but most useful on the
4272@code{-open} symbols.
4273@end defun
f214c025 4274
3a731e1f
MS
4275@comment ------------------------------------------------------------
4276
4277@defun c-lineup-argcont
4278@findex lineup-argcont (c-)
4279Line up a continued argument. E.g:
4280
4281@example
4282@group
4283foo (xyz, aaa + bbb + ccc
4284 + ddd + eee + fff); @hereFn{c-lineup-argcont}
f214c025
GM
4285@end group
4286@end example
3a731e1f
MS
4287
4288Only continuation lines like this are touched, @code{nil} is returned on
4289lines which are the start of an argument.
4290
4291Within a gcc @code{asm} block, @code{:} is recognised as an argument
4292separator, but of course only between operand specifications, not in the
4293expressions for the operands.
4294
4295@workswith @code{arglist-cont}, @code{arglist-cont-nonempty}.
4296@end defun
4297
4298@comment ------------------------------------------------------------
4299
4300@defun c-lineup-arglist
4301@findex lineup-arglist (c-)
4302Line up the current argument line under the first argument.
4303
4304As a special case, if an argument on the same line as the open
4305parenthesis starts with a brace block opener, the indentation is
4306@code{c-basic-offset} only. This is intended as a ``DWIM'' measure in
4307cases like macros that contains statement blocks, e.g:
4308
f214c025
GM
4309@example
4310@group
3a731e1f
MS
4311A_VERY_LONG_MACRO_NAME (@{
4312 some (code, with + long, lines * in[it]);
4313 @});
4314@sssTBasicOffset{}
4315@end group
4316@end example
f214c025 4317
3a731e1f
MS
4318This is motivated partly because it's more in line with how code
4319blocks are handled, and partly since it approximates the behavior of
4320earlier CC Mode versions, which due to inaccurate analysis tended to
4321indent such cases this way.
4322
4323@workswith @code{arglist-cont-nonempty}, @code{arglist-close}.
4324@end defun
4325
4326@comment ------------------------------------------------------------
4327
4328@defun c-lineup-arglist-intro-after-paren
4329@findex lineup-arglist-intro-after-paren (c-)
4330Line up a line to just after the open paren of the surrounding paren or
4331brace block.
4332
4333@workswith @code{defun-block-intro}, @code{brace-list-intro},
4334@code{statement-block-intro}, @code{statement-case-intro},
4335@code{arglist-intro}.
4336@end defun
4337
4338@comment ------------------------------------------------------------
4339
4340@defun c-lineup-arglist-close-under-paren
4341@findex lineup-arglist-close-under-paren (c-)
4342Set your @code{arglist-close} syntactic symbol to this line-up function
4343so that parentheses that close argument lists will line up under the
4344parenthesis that opened the argument list. It can also be used with
4345@code{arglist-cont} and @code{arglist-cont-nonempty} to line up all
4346lines inside a parenthesis under the open paren.
4347
4348As a special case, if a brace block is opened at the same line as the
4349open parenthesis of the argument list, the indentation is
4350@code{c-basic-offset} only. See @code{c-lineup-arglist} for further
4351discussion of this ``DWIM'' measure.
4352
4353@workswith Almost all symbols, but are typically most useful on
4354@code{arglist-close}, @code{brace-list-close}, @code{arglist-cont} and
4355@code{arglist-cont-nonempty}.
4356@end defun
4357
4358@comment ------------------------------------------------------------
4359
4360@defun c-lineup-arglist-operators
4361@findex lineup-arglist-operators (c-)
4362Line up lines starting with an infix operator under the open paren.
4363Return @code{nil} on lines that don't start with an operator, to leave
4364those cases to other lineup functions. Example:
f214c025 4365
3a731e1f
MS
4366@example
4367@group
4368if ( x < 10
4369 || at_limit (x, @hereFn{c-lineup-arglist-operators}
4370 list) @hereFn{c-lineup-arglist-operators@r{ returns nil}}
4371 )
f214c025
GM
4372@end group
4373@end example
4374
3a731e1f
MS
4375Since this function doesn't do anything for lines without an infix
4376operator you typically want to use it together with some other lineup
c3a2e2d5 4377settings, e.g., as follows (the @code{arglist-close} setting is just a
3a731e1f 4378suggestion to get a consistent style):
d7bd46ed 4379
d7bd46ed 4380@example
3a731e1f
MS
4381(c-set-offset 'arglist-cont
4382 '(c-lineup-arglist-operators 0))
4383(c-set-offset 'arglist-cont-nonempty
4384 '(c-lineup-arglist-operators c-lineup-arglist))
4385(c-set-offset 'arglist-close
4386 '(c-lineup-arglist-close-under-paren))
4387@end example
d7bd46ed 4388
3a731e1f
MS
4389@workswith @code{arglist-cont}, @code{arglist-cont-nonempty}.
4390@end defun
4391
4392@comment ------------------------------------------------------------
d7bd46ed 4393
3a731e1f
MS
4394@defun c-lineup-C-comments
4395@findex lineup-C-comments (c-)
4396Line up C block comment continuation lines. Various heuristics are used
4397to handle most of the common comment styles. Some examples:
d7bd46ed 4398
3a731e1f
MS
4399@example
4400@group
4401/* /** /*
4402 * text * text text
4403 */ */ */
d7bd46ed
GM
4404@end group
4405@end example
3a731e1f 4406
d7bd46ed
GM
4407@example
4408@group
3a731e1f
MS
4409/* text /* /**
4410 text ** text ** text
4411*/ */ */
4412@end group
4413@end example
d7bd46ed 4414
3a731e1f
MS
4415@example
4416@group
4417/**************************************************
4418 * text
4419 *************************************************/
4420@end group
4421@end example
d7bd46ed 4422
3a731e1f
MS
4423@vindex comment-start-skip
4424@example
4425@group
4426/**************************************************
4427 Free form text comments:
4428 In comments with a long delimiter line at the
4429 start, the indentation is kept unchanged for lines
4430 that start with an empty comment line prefix. The
4431 delimiter line is whatever matches the
4432 @code{comment-start-skip} regexp.
4433**************************************************/
d7bd46ed
GM
4434@end group
4435@end example
4436
3a731e1f 4437The style variable @code{c-comment-prefix-regexp} is used to recognize
c3a2e2d5 4438the comment line prefix, e.g., the @samp{*} that usually starts every
3a731e1f
MS
4439line inside a comment.
4440
4441@workswith The @code{c} syntactic symbol.
4442@end defun
4443
4444@comment ------------------------------------------------------------
4445
4446@defun c-lineup-cascaded-calls
4447@findex lineup-cascaded-calls (c-)
4448Line up ``cascaded calls'' under each other. If the line begins with
4449@code{->} or @code{.} and the preceding line ends with one or more
4450function calls preceded by the same token, then the arrow is lined up
4451with the first of those tokens. E.g:
d7bd46ed 4452
d7bd46ed
GM
4453@example
4454@group
3a731e1f
MS
4455r = proc->add(17)->add(18)
4456 ->add(19) + @hereFn{c-lineup-cascaded-calls}
4457 offset; @hereFn{c-lineup-cascaded-calls@r{ (inactive)}}
4458@end group
4459@end example
d7bd46ed 4460
3a731e1f
MS
4461In any other situation @code{nil} is returned to allow use in list
4462expressions.
4463
4464@workswith @code{topmost-intro-cont}, @code{statement-cont},
4465@code{arglist-cont}, @code{arglist-cont-nonempty}.
4466@end defun
d7bd46ed 4467
3a731e1f
MS
4468@comment ------------------------------------------------------------
4469
4470@defun c-lineup-close-paren
4471@findex lineup-close-paren (c-)
4472Line up the closing paren under its corresponding open paren if the
4473open paren is followed by code. If the open paren ends its line, no
4474indentation is added. E.g:
d7bd46ed 4475
3a731e1f
MS
4476@example
4477@group
4478main (int,
bf247b6e 4479 char **
3a731e1f 4480 ) @hereFn{c-lineup-close-paren}
d7bd46ed
GM
4481@end group
4482@end example
3a731e1f 4483
d7bd46ed
GM
4484@noindent
4485and
3a731e1f 4486
d7bd46ed
GM
4487@example
4488@group
3a731e1f
MS
4489main (
4490 int, char **
4491) @hereFn{c-lineup-close-paren}
4492@end group
4493@end example
d7bd46ed 4494
3a731e1f
MS
4495As a special case, if a brace block is opened at the same line as the
4496open parenthesis of the argument list, the indentation is
4497@code{c-basic-offset} instead of the open paren column. See
4498@code{c-lineup-arglist} for further discussion of this ``DWIM'' measure.
4499
4500@workswith All @code{*-close} symbols.
4501@end defun
4502
4503@comment ------------------------------------------------------------
4504
4505@defun c-lineup-comment
4506@findex lineup-comment (c-)
4507Line up a comment-only line according to the style variable
4508@code{c-comment-only-line-offset}. If the comment is lined up with a
4509comment starter on the previous line, that alignment is preserved.
4510
4511@defopt c-comment-only-line-offset
4512@vindex comment-only-line-offset (c-)
4513This style variable specifies the extra offset for the line. It can
4514contain an integer or a cons cell of the form
4515
4516@example
4517(@r{@var{non-anchored-offset}} . @r{@var{anchored-offset}})
4518@end example
4519
4520@noindent
4521where @var{non-anchored-offset} is the amount of offset given to
4522non-column-zero anchored lines, and @var{anchored-offset} is the amount
4523of offset to give column-zero anchored lines. Just an integer as value
4524is equivalent to @code{(@r{@var{value}} . -1000)}.
4525@end defopt
4526
4527@workswith @code{comment-intro}.
4528@end defun
4529
4530@comment ------------------------------------------------------------
4531
4532@defun c-lineup-cpp-define
4533@findex lineup-cpp-define (c-)
4534Line up macro continuation lines according to the indentation of the
4535construct preceding the macro. E.g:
d7bd46ed 4536
3a731e1f
MS
4537@example
4538@group
4539const char msg[] = @hereFn{@r{The beginning of the preceding construct.}}
4540 \"Some text.\";
4541
4542#define X(A, B) \
4543do @{ \ @hereFn{c-lineup-cpp-define}
4544 printf (A, B); \
4545@} while (0)
d7bd46ed
GM
4546@end group
4547@end example
4548
3a731e1f
MS
4549@noindent
4550and:
d7bd46ed 4551
d7bd46ed
GM
4552@example
4553@group
3a731e1f
MS
4554int dribble() @{
4555 if (!running) @hereFn{@r{The beginning of the preceding construct.}}
4556 error(\"Not running!\");
4557
4558#define X(A, B) \
4559 do @{ \ @hereFn{c-lineup-cpp-define}
4560 printf (A, B); \
4561 @} while (0)
4562@end group
4563@end example
d7bd46ed 4564
3a731e1f
MS
4565If @code{c-syntactic-indentation-in-macros} is non-@code{nil}, the
4566function returns the relative indentation to the macro start line to
c3a2e2d5 4567allow accumulation with other offsets. e.g., in the following cases,
3a731e1f
MS
4568@code{cpp-define-intro} is combined with the
4569@code{statement-block-intro} that comes from the @samp{do @{} that hangs
4570on the @samp{#define} line:
177c0ea7 4571
3a731e1f
MS
4572@example
4573@group
4574const char msg[] =
4575 \"Some text.\";
d7bd46ed 4576
3a731e1f
MS
4577#define X(A, B) do @{ \
4578 printf (A, B); \ @hereFn{c-lineup-cpp-define}
4579 this->refs++; \
4580@} while (0) @hereFn{c-lineup-cpp-define}
d7bd46ed
GM
4581@end group
4582@end example
3a731e1f 4583
d7bd46ed 4584@noindent
3a731e1f
MS
4585and:
4586
d7bd46ed
GM
4587@example
4588@group
3a731e1f
MS
4589int dribble() @{
4590 if (!running)
4591 error(\"Not running!\");
4592
4593#define X(A, B) do @{ \
4594 printf (A, B); \ @hereFn{c-lineup-cpp-define}
4595 this->refs++; \
4596 @} while (0) @hereFn{c-lineup-cpp-define}
4597@end group
4598@end example
d7bd46ed 4599
3a731e1f
MS
4600The relative indentation returned by @code{c-lineup-cpp-define} is zero
4601and two, respectively, on the two lines in each of these examples. They
4602are then added to the two column indentation that
4603@code{statement-block-intro} gives in both cases here.
4604
4605If the relative indentation is zero, then @code{nil} is returned
4606instead. That is useful in a list expression to specify the default
4607indentation on the top level.
4608
4609If @code{c-syntactic-indentation-in-macros} is @code{nil} then this
4610function keeps the current indentation, except for empty lines (ignoring
4611the ending backslash) where it takes the indentation from the closest
4612preceding nonempty line in the macro. If there's no such line in the
4613macro then the indentation is taken from the construct preceding it, as
4614described above.
4615
4616@workswith @code{cpp-define-intro}.
4617@end defun
4618
4619@comment ------------------------------------------------------------
4620
4621@defun c-lineup-dont-change
4622@findex lineup-dont-change (c-)
4623This lineup function makes the line stay at whatever indentation it
4624already has; think of it as an identity function for lineups.
4625
4626@workswith Any syntactic symbol.
4627@end defun
4628
4629@comment ------------------------------------------------------------
d7bd46ed 4630
3a731e1f
MS
4631@defun c-lineup-gcc-asm-reg
4632@findex lineup-gcc-asm-reg (c-)
4633Line up a gcc asm register under one on a previous line.
4634
4635@example
4636@group
4637 asm ("foo %1, %0\n"
4638 "bar %0, %1"
4639 : "=r" (w),
4640 "=r" (x)
4641 : "0" (y),
4642 "1" (z));
d7bd46ed
GM
4643@end group
4644@end example
4645
3a731e1f
MS
4646The @samp{x} line is aligned to the text after the @samp{:} on the
4647@samp{w} line, and similarly @samp{z} under @samp{y}.
d7bd46ed 4648
3a731e1f
MS
4649This is done only in an @samp{asm} or @samp{__asm__} block, and only to
4650those lines mentioned. Anywhere else @code{nil} is returned. The usual
4651arrangement is to have this routine as an extra feature at the start of
4652arglist lineups, e.g.
d7bd46ed 4653
d7bd46ed 4654@example
3a731e1f
MS
4655(c-lineup-gcc-asm-reg c-lineup-arglist)
4656@end example
d7bd46ed 4657
3a731e1f
MS
4658@workswith @code{arglist-cont}, @code{arglist-cont-nonempty}.
4659@end defun
4660
4661@comment ------------------------------------------------------------
4662
4663@defun c-lineup-inexpr-block
4664@findex lineup-inexpr-block (c-)
4665This can be used with the in-expression block symbols to indent the
c3a2e2d5 4666whole block to the column where the construct is started. e.g., for Java
3a731e1f
MS
4667anonymous classes, this lines up the class under the @samp{new} keyword,
4668and in Pike it lines up the lambda function body under the @samp{lambda}
4669keyword. Returns @code{nil} if the block isn't part of such a
4670construct.
4671
4672@workswith @code{inlambda}, @code{inexpr-statement},
4673@code{inexpr-class}.
4674@end defun
d7bd46ed 4675
3a731e1f
MS
4676@comment ------------------------------------------------------------
4677
4678@defun c-lineup-java-inher
4679@findex lineup-java-inher (c-)
4680Line up Java implements and extends declarations. If class names
4681follow on the same line as the @samp{implements}/@samp{extends}
4682keyword, they are lined up under each other. Otherwise, they are
4683indented by adding @code{c-basic-offset} to the column of the keyword.
4684E.g:
4685
4686@example
4687@group
4688class Foo
bf247b6e 4689 extends
3a731e1f
MS
4690 Bar @hereFn{c-lineup-java-inher}
4691 @sssTBasicOffset{}
d7bd46ed
GM
4692@end group
4693@end example
3a731e1f 4694
d7bd46ed
GM
4695@noindent
4696and
3a731e1f 4697
d7bd46ed
GM
4698@example
4699@group
3a731e1f
MS
4700class Foo
4701 extends Cyphr,
4702 Bar @hereFn{c-lineup-java-inher}
4703@end group
4704@end example
d7bd46ed 4705
3a731e1f
MS
4706@workswith @code{inher-cont}.
4707@end defun
4708
4709@comment ------------------------------------------------------------
d7bd46ed 4710
3a731e1f
MS
4711@defun c-lineup-java-throws
4712@findex lineup-java-throws (c-)
4713Line up Java throws declarations. If exception names follow on the
4714same line as the throws keyword, they are lined up under each other.
4715Otherwise, they are indented by adding @code{c-basic-offset} to the
4716column of the @samp{throws} keyword. The @samp{throws} keyword itself
4717is also indented by @code{c-basic-offset} from the function declaration
4718start if it doesn't hang. E.g:
d7bd46ed 4719
3a731e1f
MS
4720@example
4721@group
4722int foo()
4723 throws @hereFn{c-lineup-java-throws}
4724 Bar @hereFn{c-lineup-java-throws}
4725@sssTsssTBasicOffset{}
d7bd46ed
GM
4726@end group
4727@end example
4728
3a731e1f
MS
4729@noindent
4730and
d7bd46ed 4731
d7bd46ed 4732@example
d7bd46ed 4733@group
3a731e1f
MS
4734int foo() throws Cyphr,
4735 Bar, @hereFn{c-lineup-java-throws}
4736 Vlod @hereFn{c-lineup-java-throws}
d7bd46ed 4737@end group
3a731e1f
MS
4738@end example
4739
4740@workswith @code{func-decl-cont}.
4741@end defun
4742
4743@comment ------------------------------------------------------------
d7bd46ed 4744
3a731e1f
MS
4745@defun c-lineup-knr-region-comment
4746@findex lineup-knr-region-comment (c-)
4747Line up a comment in the ``K&R region'' with the declaration. That is
4748the region between the function or class header and the beginning of the
4749block. E.g:
4750
4751@example
d7bd46ed 4752@group
3a731e1f
MS
4753int main()
4754/* Called at startup. */ @hereFn{c-lineup-knr-region-comment}
4755@{
4756 return 0;
4757@}
d7bd46ed 4758@end group
3a731e1f 4759@end example
d7bd46ed 4760
3a731e1f
MS
4761Return @code{nil} if called in any other situation, to be useful in list
4762expressions.
4763
4764@workswith @code{comment-intro}.
4765@end defun
4766
4767@comment ------------------------------------------------------------
4768
4769@defun c-lineup-math
4770@findex lineup-math (c-)
4771Line up the current line to after the equal sign on the first line in the
4772statement. If there isn't any, indent with @code{c-basic-offset}. If
4773the current line contains an equal sign too, try to align it with the
4774first one.
4775
4776@workswith @code{topmost-intro-cont}, @code{statement-cont},
4777@code{arglist-cont}, @code{arglist-cont-nonempty}.
4778@end defun
4779
4780@comment ------------------------------------------------------------
4781
4782@defun c-lineup-multi-inher
4783@findex lineup-multi-inher (c-)
4784Line up the classes in C++ multiple inheritance clauses and member
4785initializers under each other. E.g:
4786
4787@example
d7bd46ed 4788@group
3a731e1f
MS
4789Foo::Foo (int a, int b):
4790 Cyphr (a),
4791 Bar (b) @hereFn{c-lineup-multi-inher}
d7bd46ed 4792@end group
3a731e1f 4793@end example
d7bd46ed 4794
3a731e1f
MS
4795@noindent
4796and
4797
4798@example
d7bd46ed 4799@group
3a731e1f
MS
4800class Foo
4801 : public Cyphr,
4802 public Bar @hereFn{c-lineup-multi-inher}
d7bd46ed 4803@end group
3a731e1f
MS
4804@end example
4805
4806@noindent
4807and
d7bd46ed 4808
3a731e1f
MS
4809@example
4810@group
4811Foo::Foo (int a, int b)
4812 : Cyphr (a)
4813 , Bar (b) @hereFn{c-lineup-multi-inher}
4814@end group
d7bd46ed
GM
4815@end example
4816
3a731e1f
MS
4817@workswith @code{inher-cont}, @code{member-init-cont}.
4818@end defun
d7bd46ed 4819
3a731e1f 4820@comment ------------------------------------------------------------
d7bd46ed 4821
3a731e1f
MS
4822@defun c-lineup-ObjC-method-call
4823@findex lineup-ObjC-method-call (c-)
4824For Objective-C code, line up selector args as Emacs Lisp mode does
4825with function args: go to the position right after the message receiver,
4826and if you are at the end of the line, indent the current line
4827c-basic-offset columns from the opening bracket; otherwise you are
4828looking at the first character of the first method call argument, so
4829lineup the current line with it.
d7bd46ed 4830
3a731e1f
MS
4831@workswith @code{objc-method-call-cont}.
4832@end defun
d7bd46ed 4833
3a731e1f 4834@comment ------------------------------------------------------------
d7bd46ed 4835
3a731e1f
MS
4836@defun c-lineup-ObjC-method-args
4837@findex lineup-ObjC-method-args (c-)
4838For Objective-C code, line up the colons that separate args. The colon
4839on the current line is aligned with the one on the first line.
d7bd46ed 4840
3a731e1f
MS
4841@workswith @code{objc-method-args-cont}.
4842@end defun
d7bd46ed 4843
3a731e1f
MS
4844@comment ------------------------------------------------------------
4845
4846@defun c-lineup-ObjC-method-args-2
4847@findex lineup-ObjC-method-args-2 (c-)
4848Similar to @code{c-lineup-ObjC-method-args} but lines up the colon on
4849the current line with the colon on the previous line.
4850
4851@workswith @code{objc-method-args-cont}.
4852@end defun
4853
4854@comment ------------------------------------------------------------
d7bd46ed 4855
3a731e1f 4856@defun c-lineup-runin-statements
d7bd46ed 4857@findex lineup-runin-statements (c-)
d7bd46ed
GM
4858Line up statements for coding standards which place the first statement
4859in a block on the same line as the block opening brace@footnote{Run-in
4860style doesn't really work too well. You might need to write your own
4861custom indentation functions to better support this style.}. E.g:
3a731e1f 4862
d7bd46ed
GM
4863@example
4864@group
d7bd46ed 4865int main()
3a731e1f
MS
4866@{ puts ("Hello!");
4867 return 0; @hereFn{c-lineup-runin-statements}
d7bd46ed 4868@}
d7bd46ed
GM
4869@end group
4870@end example
4871
4872If there is no statement after the opening brace to align with,
4873@code{nil} is returned. This makes the function usable in list
4874expressions.
4875
4876@workswith The @code{statement} syntactic symbol.
3a731e1f 4877@end defun
d7bd46ed 4878
3a731e1f
MS
4879@comment ------------------------------------------------------------
4880
4881@defun c-lineup-streamop
4882@findex lineup-streamop (c-)
c3a2e2d5 4883Line up C++ stream operators (i.e., @samp{<<} and @samp{>>}).
3a731e1f
MS
4884
4885@workswith @code{stream-op}.
4886@end defun
4887
4888@comment ------------------------------------------------------------
4889
4890@defun c-lineup-string-cont
4891@findex lineup-string-cont (c-)
4892Line up a continued string under the one it continues. A continued
4893string in this sense is where a string literal follows directly after
4894another one. E.g:
d7bd46ed 4895
3a731e1f
MS
4896@example
4897@group
4898result = prefix + "A message "
4899 "string."; @hereFn{c-lineup-string-cont}
4900@end group
4901@end example
d7bd46ed 4902
3a731e1f
MS
4903@code{nil} is returned in other situations, to allow stacking with other
4904lineup functions.
4905
4906@workswith @code{topmost-intro-cont}, @code{statement-cont},
4907@code{arglist-cont}, @code{arglist-cont-nonempty}.
4908@end defun
4909
4910@comment ------------------------------------------------------------
4911
4912@defun c-lineup-template-args
d7bd46ed 4913@findex lineup-template-args (c-)
d7bd46ed
GM
4914Line up the arguments of a template argument list under each other, but
4915only in the case where the first argument is on the same line as the
4916opening @samp{<}.
4917
4918To allow this function to be used in a list expression, @code{nil} is
4919returned if there's no template argument on the first line.
4920
4921@workswith @code{template-args-cont}.
3a731e1f 4922@end defun
d7bd46ed 4923
3a731e1f 4924@comment ------------------------------------------------------------
d7bd46ed 4925
3a731e1f
MS
4926@defun c-lineup-topmost-intro-cont
4927@findex lineup-topmost-intro-cont (c-)
4928Line up declaration continuation lines zero or one indentation
4929step@footnote{This function is mainly provided to mimic the behavior of
4930CC Mode 5.28 and earlier where this case wasn't handled consistently so
4931that those lines could be analyzed as either topmost-intro-cont or
4932statement-cont. It's used for @code{topmost-intro-cont} by default, but
4933you might consider using @code{+} instead.}. For lines preceding a
4934definition, zero is used. For other lines, @code{c-basic-offset} is
4935added to the indentation. E.g:
d7bd46ed 4936
3a731e1f
MS
4937@example
4938@group
4939int
4940neg (int i) @hereFn{c-lineup-topmost-intro-cont}
4941@{
4942 return -i;
4943@}
4944@end group
4945@end example
d7bd46ed 4946
3a731e1f
MS
4947@noindent
4948and
d7bd46ed 4949
3a731e1f
MS
4950@example
4951@group
4952struct
4953larch @hereFn{c-lineup-topmost-intro-cont}
4954@{
4955 double height;
4956@}
4957 the_larch, @hereFn{c-lineup-topmost-intro-cont}
4958 another_larch; @hereFn{c-lineup-topmost-intro-cont}
4959@sssTBasicOffset{}
4960@end group
4961@end example
d7bd46ed 4962
3a731e1f
MS
4963@noindent
4964and
d7bd46ed 4965
3a731e1f
MS
4966@example
4967@group
4968struct larch
4969the_larch, @hereFn{c-lineup-topmost-intro-cont}
4970 another_larch; @hereFn{c-lineup-topmost-intro-cont}
4971@end group
4972@end example
d7bd46ed 4973
3a731e1f
MS
4974@workswith @code{topmost-intro-cont}.
4975@end defun
4976
4977@comment ------------------------------------------------------------
d7bd46ed 4978
3a731e1f 4979@defun c-lineup-whitesmith-in-block
d7bd46ed 4980@findex lineup-whitesmith-in-block (c-)
3a731e1f 4981Line up lines inside a block in Whitesmith style. It's done in a way
d7bd46ed 4982that works both when the opening brace hangs and when it doesn't. E.g:
3a731e1f 4983
d7bd46ed
GM
4984@example
4985@group
d7bd46ed
GM
4986something
4987 @{
3a731e1f 4988 foo; @hereFn{c-lineup-whitesmith-in-block}
d7bd46ed 4989 @}
d7bd46ed
GM
4990@end group
4991@end example
3a731e1f 4992
d7bd46ed
GM
4993@noindent
4994and
3a731e1f 4995
d7bd46ed
GM
4996@example
4997@group
d7bd46ed 4998something @{
3a731e1f 4999 foo; @hereFn{c-lineup-whitesmith-in-block}
d7bd46ed 5000 @}
3a731e1f 5001@sssTBasicOffset{}
d7bd46ed
GM
5002@end group
5003@end example
5004
5005In the first case the indentation is kept unchanged, in the second
5006@code{c-basic-offset} is added.
5007
5008@workswith @code{defun-close}, @code{defun-block-intro},
5009@code{block-close}, @code{brace-list-close}, @code{brace-list-intro},
3a731e1f 5010@code{statement-block-intro} and all @code{in*} symbols,
c3a2e2d5 5011e.g., @code{inclass} and @code{inextern-lang}.
3a731e1f 5012@end defun
d7bd46ed 5013
d7bd46ed 5014
3a731e1f
MS
5015@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5016@node AWK Mode, Odds and Ends, Indentation Functions, Top
5017@comment node-name, next, previous, up
5018@chapter Status of AWK Mode
5019@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5020
5021@dfn{AWK mode} existed until recently in the file @file{awk-mode.el}
5022as a mode derived from c-mode. It had not been actively maintained to
5023keep pace with the newer @ccmode{}, and its indentation mechanism no
5024longer worked satisfactorally.
5025
5026The current AWK mode is based around the GNU implementation,
5027@emph{GAWK version 3.1.0}, though it should work pretty well with any
5028AWK. It has now been updated and integrated into @ccmode{} to a
5029substantial extent, though as yet not all the features of @ccmode{}
5030have been adapted to support it.
5031
5032If your (X)Emacs is set up to use the old file @file{awk-mode.elc}
5033(which will usually be the case if you have obtained this @ccmode{}
5034independently of (X)Emacs itself), or if you are not sure, insert the
5035following form into your @file{.emacs} or @file{init.el} so that the new
5036AWK mode will be used instead:
5037
5038@example
5039(autoload 'awk-mode "cc-mode" nil t)
5040@end example
5041
5042You can check which AWK mode you are running by displaying the mode
5043documentation string with @kbd{C-h m} from an AWK buffer. The newer
5044mode's doc string contains @code{To submit a problem report, enter
5045`C-c C-b'} near the top of the doc string where the older mode has
5046@code{This is much like C mode except ....}.
d7bd46ed 5047
3a731e1f
MS
5048Since this newer AWK mode makes essential use of a relatively new
5049Emacs Lisp feature@footnote{Specifically, the @code{syntax-table} text
5050property.}, you need either GNU Emacs 20.1 (or later) or XEmacs 21.4
5051(or later) to use it. If your Emacs version is earlier than one of
5052these, the older @file{awk-mode.el} will get loaded and run in place
5053of the AWK mode described here, even when you have put the above
5054@code{autoload} form into your @file{.emacs} or @file{init.el}.
5055Upgrading your (X)Emacs is strongly recommended if this is the case.
5056
5057Here is an overview of which @ccmode{} features currently work with
5058AWK mode and which don't:
5059
5060@table @asis
5061@item Indentation Engine
5062The @ccmode{} indentation engine fully supports AWK mode.
5063@xref{Indentation Engine}.
5064
5065AWK mode handles code formatted in the conventional AWK fashion:
5066@samp{@{}s which start actions, user-defined functions, or compound
5067statements are placed on the same line as the associated construct; the
5068matching @samp{@}}s are normally placed under the start of the
5069respective pattern, function definition, or structured statement.
5070@c Add in a bit about the @samp{@}} being on the same line when the
5071@c contents are short.
5072
5073The predefined indentation functions (@pxref{Indentation Functions})
5074haven't yet been adapted for AWK mode, though some of them may work
5075serendipitously. There shouldn't be any problems writing custom
5076indentation functions for AWK mode.
5077
5078The command @kbd{C-c C-q} (@code{c-indent-defun}) hasn't yet been
5079adapted for AWK, though in practice it works properly nearly all the
5080time. Should it fail, explicitly set the region around the function
5081(using @kbd{C-u C-SPC}: @kbd{C-M-h} probably won't work either) then do
5082@kbd{C-M-\} (@code{indent-region}).
5083
5084@item Font Locking
5085There is a single level of font locking in AWK mode, rather than the
5086three distinct levels the other modes have. There are several
5087idiosyncrasies in AWK mode's font-locking due to the peculiarities of
5088the AWK language itself. @xref{AWK Mode Font Locking}.
5089
5090@item Comment Commands
5091@kbd{M-;} (@code{indent-for-comment}) works fine. None of the other
5092@ccmode{} comment formatting commands have yet been adapted for AWK
5093mode. @xref{Text Filling and Line Breaking}.
5094
5095@item Movement Commands
5096Most of the movement commands work in AWK mode. The most important
5097exceptions are @kbd{M-a} (@code{c-beginning-of-statement}) and
5098@kbd{M-e} (@code{c-end-of-statement}) which haven't yet been adapted.
5099
5100The notion of @dfn{defun} has been augmented to include pattern-action
5101pairs. See @ref{AWK Mode Defuns} for a description of commands which
5102work on AWK ``defuns''.
5103
5104Since there is no preprocessor in AWK, the commands which move to
c3a2e2d5 5105preprocessor directives (e.g., @code{c-up-conditional}) are meaningless
3a731e1f
MS
5106in AWK mode and are not bound in the AWK mode keymap.
5107
5108@item Auto-newline Insertion and Clean-ups
5109Auto-newline insertion hasn't yet been adapted for AWK. Some of the
5110clean-ups can actually convert good AWK code into syntactically
5111invalid code.
5112
5113If auto-newline or its associated clean-ups are enabled generally for
5114the modes in @ccmode{}, you are strongly recommended to disable them
5115in the AWK Mode hook. @xref{Initialising AWK Mode}.
5116
5117The clean-up @code{space-before-funcall}, which is independent of
5118auto-newline, should never be active in AWK mode (since inserting a
5119space between a user function's name and its opening @samp{(} makes
5120the call syntactically invalid). If necessary, this should be
5121disabled in the AWK Mode hook. @xref{Initialising AWK Mode}.
5122
5123@end table
5124
5125@menu
5126* Initialising AWK Mode::
5127* AWK Mode Font Locking::
5128* AWK Mode Defuns::
5129@end menu
5130
5131
5132@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5133@node Initialising AWK Mode, AWK Mode Font Locking, , AWK Mode
5134@comment node-name, next, previous, up
5135@section AWK mode - What to put in your @file{.emacs} or @file{init.el}
5136@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5137
5138Much of the AWK mode initialization can, of course, be done by the
5139@ccmode{} general initialization procedure. You may want to use certain
5140@ccmode{} features such as @code{auto-newline} and @code{clean-ups} in
5141the other modes, and you might thus have enabled them in a
5142@code{c-mode-common-hook} function, as described in @ref{Sample .emacs File}.
5143These features have not yet been amended for AWK mode, and far from
5144being useful, can be irritating in AWK mode or actually make AWK code
5145syntactically invalid. Adding the following code to your
5146@file{.emacs} or @file{init.el} file will disable them for AWK mode.
5147
5148@example
5149(defun my-awk-mode-hook ()
5150 "Disable certain @ccmode{} features which could impair AWK mode."
5151 (c-toggle-auto-state -1) ; disable automatic insertions of newlines
5152 (if (memq 'space-before-funcall c-cleanup-list)
5153 (setq c-cleanup-list ; don't automatically insert a space into "foo("
5154 (remove 'space-before-funcall c-cleanup-list))))
5155(add-hook 'awk-mode-hook 'my-awk-mode-hook)
5156@end example
5157
5158Naturally you can add your own AWK-specific customizations to this
5159function. @xref{Hooks}.
5160
5161
5162@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5163@node AWK Mode Font Locking, AWK Mode Defuns, Initialising AWK Mode, AWK Mode
5164@comment node-name, next, previous, up
5165@section AWK Mode Font Locking
5166@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5167
5168The general appearance of font-locking in AWK mode is much like in any
5169other programming mode. @xref{Faces For Font Lock,,,elisp}.
5170
5171The following faces are, however, used in a non-standard fashion in
5172AWK mode:
5173
5174@table @asis
5175@item @code{font-lock-variable-name-face}
5176This face was intended for variable declarations. Since variables are
5177not declared in AWK, this face is used instead for AWK system
5178variables (such as @code{NF}) and ``Special File Names'' (such as
5179@code{"/dev/stderr"}).
5180
5181@item @code{font-lock-builtin-face} (Emacs)/@code{font-lock-preprocessor-face} (XEmacs)
5182This face is normally used for preprocessor directives in @ccmode{}.
5183There are no such things in AWK, so this face is used instead for
5184standard functions (such as @code{match}).
5185
5186@item @code{font-lock-string-face}
5187As well as being used for strings, including localizable strings,
5188(delimited by @samp{"} and @samp{_"}), this face is also used for AWK
5189regular expressions (delimited by @samp{/}).
5190
5191@item @code{font-lock-warning-face} (Emacs)/@code{c-invalid-face} (XEmacs)
5192This face highlights the following syntactically invalid AWK
5193constructs:
5194
5195@itemize @bullet
5196@item
5197An unterminated string or regular expression. Here the opening
5198delimiter (@samp{"} or @samp{/} or @samp{_"}) is displayed in
5199@code{font-lock-warning-face}. This is most noticeable when typing in a
5200new string/regular expression into a buffer, when the warning-face
5201serves as a continual reminder to terminate the construct.
5202
5203AWK mode fontifies unterminated strings/regular expressions
5204differently from other modes: Only the text up to the end of the line
5205is fontified as a string (escaped newlines being handled correctly),
5206rather than the text up to the next string quote.
5207
5208@item
5209A space between the function name and opening parenthesis when calling
5210a user function. The last character of the function name and the
5211opening parenthesis are highlighted. This font-locking rule will
5212spuriously highlight a valid concatenation expression where an
5213identifier precedes a parenthesised expression. Unfortunately.
5214
5215@item
5216Whitespace following the @samp{\} in what otherwise looks like an
5217escaped newline. The @samp{\} is highlighted.
5218@end itemize
5219@end table
5220
5221
5222@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5223@node AWK Mode Defuns, , AWK Mode Font Locking, AWK Mode
5224@comment node-name, next, previous, up
5225@section AWK Mode Defuns
5226@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5227
5228In AWK mode, @dfn{defun} means either a user-defined function or a
5229pattern-action pair. Either the pattern or the action may be
5230implicit.
5231
5232The beginning of a defun is recognised heuristically as, more or less,
5233code which begins in column zero. Having the @samp{@{} in column zero,
5234as is suggested for some modes, is neither necessary nor helpful in AWK
5235mode.
5236
5237More precisely, the beginning of a defun is code which begins in
5238column zero, and which isn't a closing brace, a comment, or a
5239continuation of the previous line. Code is the @dfn{continuation of
5240the previous line} when that line is syntactically incomplete, for
5241example when it ends with @samp{@{} or an escaped newline.
5242
5243The end of a defun is the @samp{@}} which matches the @samp{@{} (if
5244any) at the beginning of the action or function body, or the EOL or
5245@samp{;} which marks an implicit action. Although this @samp{@}} is
5246usually placed in column zero, AWK mode doesn't need it to be placed
5247there.
5248
5249@table @asis
5250@item @kbd{C-M-a} @code{c-awk-beginning-of-defun}
5251@itemx @kbd{C-M-e} @code{c-awk-end-of-defun}
5252@findex c-awk-beginning-of-defun
5253@findex awk-beginning-of-defun (c-)
5254@findex c-awk-end-of-defun
5255@findex awk-end-of-defun (c-)
5256Move point back to the beginning or forward to the end of the current
5257AWK defun. These functions can take prefix-arguments, their
5258functionality being entirely equivalent to @code{beginning-of-defun}
5259and @code{end-of-defun}. @xref{Moving by Defuns,,,emacs}.
5260
5261@item @kbd{C-M-h} @code{c-mark-function}
5262This works fine with AWK defuns. @xref{Indentation Commands}.
d7bd46ed
GM
5263@end table
5264
6bf7aab6 5265
d7bd46ed 5266@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3a731e1f
MS
5267@node Odds and Ends, Performance Issues, AWK Mode, Top
5268@comment node-name, next, previous, up
5269@chapter Odds and Ends
5270@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5271
5272The stuff that didn't fit in anywhere else is documented here.
5273
5274@defopt c-require-final-newline
5275@vindex require-final-newline (c-)
5276Controls whether a final newline is ensured when the file is saved. The
5277value is an association list that for each language mode specifies the
5278value to give to @code{require-final-newline} at mode initialization;
5279see that variable for details about the value. If a language isn't
2a15eb73 5280present on the association list, CC Mode won't touch
3a731e1f
MS
5281@code{require-final-newline} in buffers for that language.
5282
5283The default is to set @code{require-final-newline} to @code{t} in the
5284languages that mandates that source files should end with newlines,
c3a2e2d5 5285i.e., C, C++ and Objective-C.
3a731e1f
MS
5286@end defopt
5287
5288@defopt c-echo-syntactic-information-p
5289@vindex echo-syntactic-information-p (c-)
5290If non-@code{nil}, the syntactic analysis for the current line is shown
5291in the echo area when it's indented (unless
5292@code{c-syntactic-indentation} is @code{nil}). That's useful when
5293finding out which syntactic symbols to modify to get the indentation you
5294want.
5295@end defopt
5296
5297@defopt c-report-syntactic-errors
5298@vindex report-syntactic-errors (c-)
5299If non-@code{nil}, certain syntactic errors are reported with a ding and
5300a message, for example when an @code{else} is indented for which there
5301is no corresponding @code{if}.
5302
5303Note however that @ccmode{} doesn't make any special effort to check for
5304syntactic errors; that's the job of the compiler. The reason it can
5305report cases like the one above is that it can't find the correct
5306anchoring position to indent the line in that case.
5307@end defopt
5308
5309
5310@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5311@node Performance Issues, Limitations and Known Bugs, Odds and Ends, Top
d7bd46ed 5312@comment node-name, next, previous, up
3a731e1f
MS
5313@chapter Performance Issues
5314@cindex performance
6bf7aab6
DL
5315@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5316
3a731e1f
MS
5317@comment FIXME: (ACM, 2003/5/24). Check whether AWK needs mentioning here.
5318
6bf7aab6 5319C and its derivative languages are highly complex creatures. Often,
3a731e1f
MS
5320ambiguous code situations arise that require @ccmode{} to scan large
5321portions of the buffer to determine syntactic context. Such
5322pathological code can cause @ccmode{} to perform fairly badly. This
5323section gives some insight in how @ccmode{} operates, how that interacts
5324with some coding styles, and what you can use to improve performance.
5325
c3a2e2d5 5326The overall goal is that @ccmode{} shouldn't be overly slow (i.e., take
3a731e1f 5327more than a fraction of a second) in any interactive operation.
c3a2e2d5 5328i.e., it's tuned to limit the maximum response time in single operations,
3a731e1f
MS
5329which sometimes is at the expense of batch-like operations like
5330reindenting whole blocks. If you find that @ccmode{} gradually gets
5331slower and slower in certain situations, perhaps as the file grows in
5332size or as the macro or comment you're editing gets bigger, then chances
5333are that something isn't working right. You should consider reporting
5334it, unless it's something that's mentioned in this section.
6bf7aab6
DL
5335
5336Because @ccmode{} has to scan the buffer backwards from the current
5337insertion point, and because C's syntax is fairly difficult to parse in
5338the backwards direction, @ccmode{} often tries to find the nearest
3a731e1f
MS
5339position higher up in the buffer from which to begin a forward scan
5340(it's typically an opening or closing parethesis of some kind). The
5341farther this position is from the current insertion point, the slower it
5342gets.
6bf7aab6
DL
5343
5344@findex beginning-of-defun
5345@findex defun-prompt-regexp
5346One of the simplest things you can do to reduce scan time, is make sure
c3a2e2d5 5347any brace that opens a top-level construct@footnote{e.g., a function in
6bf7aab6
DL
5348C, or outermost class definition in C++ or Java.} always appears in the
5349leftmost column. This is actually an Emacs constraint, as embodied in
d7bd46ed 5350the @code{beginning-of-defun} function which @ccmode{} uses heavily. If
3a731e1f
MS
5351you hang top-level open braces on the right side of the line, then you
5352might want to set the variable @code{defun-prompt-regexp} to something
5353reasonable, however that ``something reasonable'' is difficult to
5354define, so @ccmode{} doesn't do it for you.
6bf7aab6
DL
5355
5356@vindex c-Java-defun-prompt-regexp
5357@vindex Java-defun-prompt-regexp (c-)
3a731e1f
MS
5358A special note about @code{defun-prompt-regexp} in Java mode: The common
5359style is to hang the opening braces of functions and classes on the
5360right side of the line, and that doesn't work well with the Emacs
5361approach. @ccmode{} comes with a variable
6bf7aab6
DL
5362@code{c-Java-defun-prompt-regexp} which tries to define a regular
5363expression usable for this style, but there are problems with it. In
5364some cases it can cause @code{beginning-of-defun} to hang@footnote{This
5365has been observed in Emacs 19.34 and XEmacs 19.15.}. For this reason,
5366it is not used by default, but if you feel adventurous, you can set
5367@code{defun-prompt-regexp} to it in your mode hook. In any event,
3a731e1f
MS
5368setting and relying on @code{defun-prompt-regexp} will definitely slow
5369things down because (X)Emacs will be doing regular expression searches a
5370lot, so you'll probably be taking a hit either way!
5371
5372@ccmode{} maintains a cache of the opening parentheses of the blocks
5373surrounding the point, and it adapts that cache as the point is moved
5374around. That means that in bad cases it can take noticeable time to
5375indent a line in a new surrounding, but after that it gets fast as long
5376as the point isn't moved far off. The farther the point is moved, the
5377less useful is the cache. Since editing typically is done in ``chunks''
5378rather than on single lines far apart from each other, the cache
5379typically gives good performance even when the code doesn't fit the
5380Emacs approach to finding the defun starts.
d7bd46ed
GM
5381
5382@vindex c-enable-xemacs-performance-kludge-p
5383@vindex enable-xemacs-performance-kludge-p (c-)
3a731e1f 5384XEmacs users can set the variable
d7bd46ed
GM
5385@code{c-enable-xemacs-performance-kludge-p} to non-@code{nil}. This
5386tells @ccmode{} to use XEmacs-specific built-in functions which, in some
3a731e1f 5387circumstances, can locate the top-most opening brace much more quickly than
d7bd46ed 5388@code{beginning-of-defun}. Preliminary testing has shown that for
c3a2e2d5 5389styles where these braces are hung (e.g., most JDK-derived Java styles),
d7bd46ed
GM
5390this hack can improve performance of the core syntax parsing routines
5391from 3 to 60 times. However, for styles which @emph{do} conform to
5392Emacs' recommended style of putting top-level braces in column zero,
5393this hack can degrade performance by about as much. Thus this variable
3a731e1f
MS
5394is set to @code{nil} by default, since the Emacs-friendly styles should
5395be more common (and encouraged!). Note that this variable has no effect
5396in Emacs since the necessary built-in functions don't exist (in Emacs
539721.3 as of this writing in May 2003).
5398
5399Text properties are used to speed up skipping over syntactic whitespace,
c3a2e2d5 5400i.e., comments and preprocessor directives. Indenting a line after a
3a731e1f
MS
5401huge macro definition can be slow the first time, but after that the
5402text properties are in place and it should be fast (even after you've
5403edited other parts of the file and then moved back).
5404
5405Font locking can be a CPU hog, especially the font locking done on
5406decoration level 3 which tries to be very accurate. Note that that
5407level is designed to be used with a font lock support mode that only
c3a2e2d5 5408fontifies the text that's actually shown, i.e., Lazy Lock or Just-in-time
3a731e1f
MS
5409Lock mode, so make sure you use one of them. Fontification of a whole
5410buffer with some thousand lines can often take over a minute. That is
5411a known weakness; the idea is that it never should happen.
5412
5413The most effective way to speed up font locking is to reduce the
5414decoration level to 2 by setting @code{font-lock-maximum-decoration}
5415appropriately. That level is designed to be as pretty as possible
5416without sacrificing performance. @xref{Font Locking Preliminaries}, for
5417more info.
6bf7aab6
DL
5418
5419
cb7f2e96
GM
5420@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5421@node Limitations and Known Bugs, Frequently Asked Questions, Performance Issues, Top
5422@comment node-name, next, previous, up
3a731e1f 5423@chapter Limitations and Known Bugs
cb7f2e96
GM
5424@cindex limitations
5425@cindex bugs
5426@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5427
5428@itemize @bullet
5429@item
3a731e1f
MS
5430There is no way to apply auto newline settings (@pxref{Auto-newline
5431Insertion}) on already typed lines. That's only a feature to ease
5432interactive editing.
cb7f2e96 5433
3a731e1f
MS
5434To generalize this issue a bit: @ccmode{} is not intended to be used as
5435a reformatter for old code in some more or less batch-like way. With
5436the exception of some functions like @code{c-indent-region}, it's only
5437geared to be used interactively to edit new code. There's currently no
5438intention to change this goal.
5439
5440If you want to reformat old code, you're probably better off using some
c3a2e2d5 5441other tool instead, e.g., @ref{Top, , GNU indent, indent, The `indent'
3a731e1f
MS
5442Manual}, which has more powerful reformatting capabilities than
5443@ccmode{}.
cb7f2e96 5444
cb7f2e96 5445@item
3a731e1f
MS
5446@vindex signal-error-on-buffer-boundary
5447XEmacs has a variable called @code{signal-error-on-buffer-boundary}.
5448It's used as a solution to user interface problems associated with
cb7f2e96 5449buffer movement and the @code{zmacs-region} deactivation on errors.
3a731e1f
MS
5450However, setting this variable to a non-default value in XEmacs 19 and
545120 had the deleterious side effect of breaking many built-in primitive
5452functions. @strong{Do not set this variable to @code{nil} in XEmacs
545319 and 20}; you will cause serious problems in @ccmode{} and probably
5454other XEmacs packages! In XEmacs 21 the effects of the variable is
5455limited to some functions that are only used interactively, so it's
5456not a problem there.
cb7f2e96
GM
5457@end itemize
5458
5459
6bf7aab6 5460@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
cb7f2e96 5461@node Frequently Asked Questions, Getting the Latest CC Mode Release, Limitations and Known Bugs, Top
d7bd46ed 5462@comment node-name, next, previous, up
cb7f2e96 5463@appendix Frequently Asked Questions
6bf7aab6
DL
5464@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5465
3a731e1f
MS
5466@itemize @bullet
5467@item
6bf7aab6 5468@kindex C-x h
d7bd46ed 5469@kindex C-M-\
3a731e1f 5470@emph{How do I reindent the whole file?}
6bf7aab6 5471
3a731e1f
MS
5472Visit the file and hit @kbd{C-x h} to mark the whole buffer. Then hit
5473@kbd{C-M-\}.
6bf7aab6 5474
3a731e1f
MS
5475@item
5476@kindex C-M-q
5477@kindex C-M-u
5478@emph{How do I reindent the current block?}
6bf7aab6 5479
3a731e1f
MS
5480First move to the brace which opens the block with @kbd{C-M-u}, then
5481reindent that expression with @kbd{C-M-q}.
6bf7aab6 5482
3a731e1f
MS
5483@item
5484@kindex RET
5485@kindex C-j
5486@emph{Why doesn't the @kbd{RET} key indent the new line?}
6bf7aab6 5487
3a731e1f
MS
5488Emacs' convention is that @kbd{RET} just adds a newline, and that
5489@kbd{C-j} adds a newline and indents it. You can make @kbd{RET} do this
5490too by adding this to your @code{c-mode-common-hook}:
6bf7aab6 5491
6bf7aab6 5492@example
d7bd46ed 5493(define-key c-mode-base-map "\C-m" 'c-context-line-break)
6bf7aab6
DL
5494@end example
5495
5496This is a very common question. If you want this to be the default
d7bd46ed 5497behavior, don't lobby me, lobby RMS! @t{:-)}
6bf7aab6 5498
3a731e1f
MS
5499@item
5500@emph{I put @code{(c-set-offset 'substatement-open 0)} in my
5501@file{.emacs} file but I get an error saying that @code{c-set-offset}'s
5502function definition is void. What's wrong?}
6bf7aab6 5503
3a731e1f
MS
5504This means that @ccmode{} wasn't loaded into your Emacs session by the
5505time the @code{c-set-offset} call was reached, most likely because
5506@ccmode{} is being autoloaded. Instead of putting the
5507@code{c-set-offset} line in your top-level @file{.emacs} file, put it in
5508your @code{c-mode-common-hook}, or simply modify @code{c-offsets-alist}
5509directly:
f214c025 5510
3a731e1f
MS
5511@example
5512(setq c-offsets-alist '((substatement-open . 0)))
6bf7aab6
DL
5513@end example
5514
3a731e1f
MS
5515@item
5516@kindex M-a
5517@kindex M-e
5518@emph{@kbd{M-a} and @kbd{M-e} used to move over entire balanced brace
5519lists, but now they move into blocks. How do I get the old behavior
5520back?}
5521
5522Use @kbd{C-M-f} and @kbd{C-M-b} to move over balanced brace blocks. Use
5523@kbd{M-a} and @kbd{M-e} to move by statements, which will also move into
5524blocks.
5525
5526@item
5527@emph{Whenever I try to indent a line or type an ``electric'' key such
5528as @kbd{;}, @kbd{@{}, or @kbd{@}}, I get an error that look like this:
5529@code{Invalid function: (macro . #[...}. What gives?}
5530
5531This is a common error when @ccmode{} hasn't been compiled correctly,
5532especially under Emacs 19.34@footnote{Technically, it's because some
5533macro wasn't defined during the compilation, so the byte compiler put
5534in function calls instead of the macro expansions. Later, when the
5535interpreter tries to call the macro as a function, it shows this
5536(somewhat cryptic) error message.}. If you are using the standalone
d7bd46ed
GM
5537@ccmode{} distribution, try recompiling it according to the instructions
5538in the @file{README} file.
2a15eb73
MS
5539
5540@item
5541@cindex open paren in column zero
5542@emph{I have an open paren character at column zero inside a comment or
5543multiline string literal, and it causes the fontification and/or
5544indentation to go haywire. What gives?}
5545
5546It's due to the ad-hoc rule in (X)Emacs that such open parens always
5547start defuns (which translates to functions, classes, namespaces or any
5548other top-level block constructs in the @ccmode{} languages).
5549@xref{Left Margin Paren,,, emacs, The Emacs Editor}, for details
5550(@xref{Defuns,,, emacs, The Emacs Editor}, in the Emacs 20 manual).
5551
5552This heuristic is built into the core syntax analysis routines in
bf247b6e 5553(X)Emacs, so it's not really a @ccmode{} issue. However, in Emacs 22.1
2a15eb73
MS
5554it has become possible to turn it off@footnote{Using the variable
5555@code{open-paren-in-column-0-is-defun-start}.} and @ccmode{} does so
5556there since it got its own system to keep track of blocks.
5557
3a731e1f 5558@end itemize
6bf7aab6
DL
5559
5560
5561@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
cb7f2e96 5562@node Getting the Latest CC Mode Release, Mailing Lists and Submitting Bug Reports, Frequently Asked Questions, Top
d7bd46ed 5563@comment node-name, next, previous, up
cb7f2e96 5564@appendix Getting the Latest CC Mode Release
6bf7aab6
DL
5565@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5566
3a731e1f
MS
5567@ccmode{} has been standard with all versions of Emacs since 19.34 and
5568of XEmacs since 19.16.
d7bd46ed 5569
3a731e1f 5570@cindex web site
d7bd46ed 5571Due to release schedule skew, it is likely that all of these Emacsen
177c0ea7 5572have old versions of @ccmode{} and so should be upgraded. Access to the
d7bd46ed 5573@ccmode{} source code, as well as more detailed information on Emacsen
3a731e1f 5574compatibility, etc. are all available on the web site:
6bf7aab6 5575
3a731e1f
MS
5576@quotation
5577@uref{http://cc-mode.sourceforge.net/}
5578@end quotation
6bf7aab6 5579
6bf7aab6 5580
cb7f2e96
GM
5581@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5582@node Mailing Lists and Submitting Bug Reports, Sample .emacs File, Getting the Latest CC Mode Release, Top
5583@comment node-name, next, previous, up
5584@appendix Mailing Lists and Submitting Bug Reports
cb7f2e96
GM
5585@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5586
5587@kindex C-c C-b
5588@findex c-submit-bug-report
5589@findex submit-bug-report (c-)
3a731e1f
MS
5590To report bugs, use the @kbd{C-c C-b} (bound to
5591@code{c-submit-bug-report}) command. This provides vital information
5592we need to reproduce your problem. Make sure you include a concise,
5593but complete code example. Please try to boil your example down to
5594just the essential code needed to reproduce the problem, and include
5595an exact recipe of steps needed to expose the bug. Be especially sure
5596to include any code that appears @emph{before} your bug example, if
5597you think it might affect our ability to reproduce it.
cb7f2e96
GM
5598
5599Please try to produce the problem in an Emacs instance without any
c3a2e2d5 5600customizations loaded (i.e., start it with the @samp{-q --no-site-file}
cb7f2e96
GM
5601arguments). If it works correctly there, the problem might be caused by
5602faulty customizations in either your own or your site configuration. In
5603that case, we'd appreciate if you isolate the Emacs Lisp code that trigs
5604the bug and include it in your report.
5605
5606@cindex bug report mailing list
3a731e1f
MS
5607Bug reports are sent to @email{bug-cc-mode@@gnu.org}. You can also send
5608other questions and suggestions (kudos? @t{;-)} to that address. It's a
5609mailing list which you can join or browse an archive of; see the web
5610site at @uref{http://cc-mode.sourceforge.net/} for further details.
cb7f2e96
GM
5611
5612@cindex announcement mailing list
5613If you want to get announcements of new @ccmode{} releases, send the
5614word @emph{subscribe} in the body of a message to
3a731e1f
MS
5615@email{cc-mode-announce-request@@lists.sourceforge.net}. It's possible
5616to subscribe from the web site too. Announcements will also be posted
5617to the Usenet newsgroups @code{gnu.emacs.sources}, @code{comp.emacs} and
5618@code{comp.emacs.xemacs}.
cb7f2e96
GM
5619
5620
6bf7aab6 5621@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3a731e1f 5622@node Sample .emacs File, Command and Function Index, Mailing Lists and Submitting Bug Reports, Top
d7bd46ed 5623@comment node-name, next, previous, up
cb7f2e96 5624@appendix Sample .emacs file
6bf7aab6
DL
5625@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5626
5627@example
3a731e1f
MS
5628;; Here's a sample .emacs file that might help you along the way.
5629;; Just copy this region and paste it into your .emacs file. You may
5630;; want to change some of the actual values.
6bf7aab6
DL
5631
5632(defconst my-c-style
5633 '((c-tab-always-indent . t)
5634 (c-comment-only-line-offset . 4)
5635 (c-hanging-braces-alist . ((substatement-open after)
5636 (brace-list-open)))
5637 (c-hanging-colons-alist . ((member-init-intro before)
5638 (inher-intro)
5639 (case-label after)
5640 (label after)
5641 (access-label after)))
5642 (c-cleanup-list . (scope-operator
5643 empty-defun-braces
5644 defun-close-semi))
5645 (c-offsets-alist . ((arglist-close . c-lineup-arglist)
5646 (substatement-open . 0)
5647 (case-label . 4)
5648 (block-open . 0)
5649 (knr-argdecl-intro . -)))
3a731e1f 5650 (c-echo-syntactic-information-p . t))
6bf7aab6
DL
5651 "My C Programming Style")
5652
d7bd46ed 5653;; offset customizations not in my-c-style
f214c025 5654(setq c-offsets-alist '((member-init-intro . ++)))
d7bd46ed
GM
5655
5656;; Customizations for all modes in CC Mode.
6bf7aab6
DL
5657(defun my-c-mode-common-hook ()
5658 ;; add my personal style and set it for the current buffer
5659 (c-add-style "PERSONAL" my-c-style t)
6bf7aab6
DL
5660 ;; other customizations
5661 (setq tab-width 8
5662 ;; this will make sure spaces are used instead of tabs
5663 indent-tabs-mode nil)
5664 ;; we like auto-newline and hungry-delete
5665 (c-toggle-auto-hungry-state 1)
47d7776c 5666 ;; key bindings for all supported languages. We can put these in
6bf7aab6 5667 ;; c-mode-base-map because c-mode-map, c++-mode-map, objc-mode-map,
d7bd46ed 5668 ;; java-mode-map, idl-mode-map, and pike-mode-map inherit from it.
3a731e1f 5669 (define-key c-mode-base-map "\C-m" 'c-context-line-break))
6bf7aab6
DL
5670
5671(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
5672@end example
5673
d7bd46ed 5674
6bf7aab6 5675@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3a731e1f 5676@node Command and Function Index, Variable Index, Sample .emacs File, Top
d7bd46ed 5677@comment node-name, next, previous, up
3a731e1f 5678@unnumbered Command and Function Index
d7bd46ed 5679@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
6bf7aab6 5680
3a731e1f
MS
5681Since most @ccmode{} commands are prepended with the string
5682@samp{c-}, each appears under its @code{c-@var{thing}} name and its
5683@code{@var{thing} (c-)} name.
5684@iftex
5685@sp 2
5686@end iftex
5687@printindex fn
6bf7aab6
DL
5688
5689
d7bd46ed 5690@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3a731e1f 5691@node Variable Index, Concept Index, Command and Function Index, Top
d7bd46ed 5692@comment node-name, next, previous, up
3a731e1f 5693@unnumbered Variable Index
d7bd46ed 5694@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
6bf7aab6 5695
3a731e1f
MS
5696Since most @ccmode{} variables are prepended with the string
5697@samp{c-}, each appears under its @code{c-@var{thing}} name and its
5698@code{@var{thing} (c-)} name.
6bf7aab6
DL
5699@iftex
5700@sp 2
5701@end iftex
3a731e1f 5702@printindex vr
6bf7aab6
DL
5703
5704
d7bd46ed 5705@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3a731e1f 5706@node Concept Index, , Variable Index, Top
d7bd46ed 5707@comment node-name, next, previous, up
3a731e1f 5708@unnumbered Concept Index
d7bd46ed 5709@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
6bf7aab6 5710
3a731e1f 5711@printindex cp
6bf7aab6
DL
5712
5713
d7bd46ed 5714@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3a731e1f 5715@comment Epilogue.
d7bd46ed 5716@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
6bf7aab6 5717
cb7f2e96 5718@iftex
6bf7aab6
DL
5719@page
5720@summarycontents
5721@contents
cb7f2e96
GM
5722@end iftex
5723
6bf7aab6 5724@bye
ab5796a9
MB
5725
5726@ignore
5727 arch-tag: c4cab162-5e57-4366-bdce-4a9db2fc97f0
5728@end ignore