]> code.delx.au - gnu-emacs/blob - doc/misc/cc-mode.texi
Sync with Tramp 2.2.2.
[gnu-emacs] / doc / misc / cc-mode.texi
1 \input texinfo
2 @c Notes to self regarding line handling:
3 @c
4 @c Empty lines are often significant before @end directives; avoid them.
5 @c
6 @c Empty lines before and after @example directives are significant in
7 @c info output but not in TeX. Empty lines inside @example directives
8 @c are significant.
9
10 @c Conventions for formatting examples:
11 @c o If the example contains empty lines then put the surrounding empty
12 @c lines inside the @example directives. Put them outside otherwise.
13 @c o Use @group inside the example only if it shows indentation where
14 @c the relation between lines inside is relevant.
15 @c o Format line number columns like this:
16 @c 1: foo
17 @c 2: bar
18 @c ^ one space
19 @c ^^ two columns, right alignment
20 @c o Check line lengths in TeX output; they can typically be no longer
21 @c than 70 chars, 60 if the paragraph is indented.
22
23 @comment TBD: Document the finer details of statement anchoring?
24
25 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
26 @comment %**start of header (This is for running Texinfo on a region)
27 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
28
29
30 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
31 @comment How to make the various output formats:
32 @comment (Thanks to Robert Chassell for supplying this information.)
33 @comment Note that Texinfo 4.7 (or later) is needed.
34 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
35 @ignore
36 In each of the following pairs of commands, the first generates a
37 version with cross references pointing to the GNU Emacs manuals,
38 the second with them pointing to the XEmacs manuals.
39 ## Info output
40 makeinfo cc-mode.texi
41 makeinfo -DXEMACS cc-mode.texi
42
43 ## DVI output
44 ## You may need to set up the environment variable TEXINPUTS so
45 ## that tex can find the file texinfo.tex - See the tex
46 ## manpage.
47 texi2dvi cc-mode.texi
48 texi2dvi -t "@set XEMACS " cc-mode.texi
49
50 ## HTML output. (The --no-split parameter is optional)
51 makeinfo --html --no-split cc-mode.texi
52 makeinfo --html --no-split -DXEMACS cc-mode.texi
53
54 ## Plain text output
55 makeinfo --fill-column=70 --no-split --paragraph-indent=0 \
56 --no-headers --output=cc-mode.txt cc-mode.texi
57 makeinfo --fill-column=70 --no-split --paragraph-indent=0 \
58 --no-headers --output=cc-mode.txt -DXEMACS cc-mode.texi
59
60 ## DocBook output
61 makeinfo --docbook --no-split --paragraph-indent=0 \
62 cc-mode.texi
63 makeinfo --docbook --no-split --paragraph-indent=0 \
64 -DXEMACS cc-mode.texi
65
66 ## XML output
67 makeinfo --xml --no-split --paragraph-indent=0 \
68 cc-mode.texi
69 makeinfo --xml --no-split --paragraph-indent=0 \
70 -DXEMACS cc-mode.texi
71
72 #### (You must be in the same directory as the viewed file.)
73
74 ## View DVI output
75 xdvi cc-mode.dvi &
76
77 ## View HTML output
78 mozilla cc-mode.html
79 @end ignore
80
81 @comment No overfull hbox marks in the dvi file.
82 @finalout
83
84 @setfilename ../../info/ccmode
85 @settitle CC Mode Manual
86 @footnotestyle end
87
88 @c The following four macros generate the filenames and titles of the
89 @c main (X)Emacs manual and the Elisp/Lispref manual. Leave the
90 @c Texinfo variable `XEMACS' unset to generate a GNU Emacs version, set it
91 @c to generate an XEmacs version, e.g. with
92 @c "makeinfo -DXEMACS cc-mode.texi".
93 @ifset XEMACS
94 @macro emacsman
95 xemacs
96 @end macro
97 @macro emacsmantitle
98 XEmacs User's Manual
99 @end macro
100 @macro lispref
101 lispref
102 @end macro
103 @macro lispreftitle
104 XEmacs Lisp Reference Manual
105 @end macro
106 @end ifset
107
108 @ifclear XEMACS
109 @macro emacsman
110 emacs
111 @end macro
112 @macro emacsmantitle
113 GNU Emacs Manual
114 @end macro
115 @macro lispref
116 elisp
117 @end macro
118 @macro lispreftitle
119 GNU Emacs Lisp Reference Manual
120 @end macro
121 @end ifclear
122
123
124 @macro ccmode
125 CC Mode
126 @end macro
127
128 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
129 @comment @setchapternewpage odd !! we don't want blank pages !!
130 @comment %**end of header (This is for running Texinfo on a region)
131 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
132
133
134 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
135 @comment
136 @comment Texinfo manual for CC Mode
137 @comment Generated from the original README file by Krishna Padmasola
138 @comment <krishna@earth-gw.njit.edu>
139 @comment
140 @comment Authors:
141 @comment Barry A. Warsaw
142 @comment Martin Stjernholm
143 @comment Alan Mackenzie
144 @comment
145 @comment Maintained by Martin Stjernholm and Alan Mackenzie <bug-cc-mode@gnu.org>
146 @comment
147 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
148
149 @comment Define an index for syntactic symbols.
150 @ifnottex @c In texi2dvi, the @defindex would create an empty cc-mode.ss
151 @c For Info, unlike tex, @syncodeindex needs a matching @defindex.
152 @defindex ss
153 @end ifnottex
154
155 @comment Combine key, syntactic symbol and concept indices into one.
156 @syncodeindex ss cp
157 @syncodeindex ky cp
158
159 @copying
160 This manual is for CC Mode in Emacs.
161
162 Copyright @copyright{} 1995-2011 Free Software Foundation, Inc.
163
164 @quotation
165 Permission is granted to copy, distribute and/or modify this document
166 under the terms of the GNU Free Documentation License, Version 1.3 or
167 any later version published by the Free Software Foundation; with no
168 Invariant Sections, with the Front-Cover texts being ``A GNU Manual'',
169 and with the Back-Cover Texts as in (a) below. A copy of the license
170 is included in the section entitled ``GNU Free Documentation License''.
171
172 (a) The FSF's Back-Cover Text is: ``You have the freedom to copy and
173 modify this GNU manual. Buying copies from the FSF supports it in
174 developing GNU and promoting software freedom.''
175 @end quotation
176 @end copying
177
178 @comment Info directory entry for use by install-info. The indentation
179 @comment here is by request from the FSF folks.
180 @dircategory Emacs editing modes
181 @direntry
182 * CC Mode: (ccmode). Emacs mode for editing C, C++, Objective-C,
183 Java, Pike, AWK, and CORBA IDL code.
184 @end direntry
185
186 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
187 @comment TeX title page
188 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
189
190 @titlepage
191 @sp 10
192
193 @center @titlefont{CC Mode 5.31}
194 @sp 2
195 @center @subtitlefont{A GNU Emacs mode for editing C and C-like languages}
196 @sp 2
197 @center Barry A. Warsaw, Martin Stjernholm, Alan Mackenzie
198
199 @page
200 @vskip 0pt plus 1filll
201 @insertcopying
202
203 This manual was generated from cc-mode.texi, which is distributed with Emacs,
204 or can be downloaded from @url{http://savannah.gnu.org/projects/emacs/}.
205 @end titlepage
206
207 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
208 @comment The Top node contains the master menu for the Info file.
209 @comment This appears only in the Info file, not the printed manual.
210 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
211
212 @summarycontents
213 @contents
214
215 @node Top, Introduction, (dir), (dir)
216 @comment node-name, next, previous, up
217
218 @ifnottex
219 @top @ccmode{}
220
221 @ccmode{} is a GNU Emacs mode for editing files containing C, C++,
222 Objective-C, Java, CORBA IDL (and the variants PSDL and CIDL), Pike
223 and AWK code. It provides syntax-based indentation, font locking, and
224 has several handy commands and some minor modes to make the editing
225 easier. It does not provide tools to look up and navigate between
226 functions, classes etc - there are other packages for that.
227
228 @insertcopying
229 @end ifnottex
230
231 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
232 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
233
234 @menu
235 * Introduction::
236 * Overview::
237 * Getting Started::
238 * Commands::
239 * Font Locking::
240 * Config Basics::
241 * Custom Filling and Breaking::
242 * Custom Auto-newlines::
243 * Clean-ups::
244 * Indentation Engine Basics::
245 * Customizing Indentation::
246 * Custom Macros::
247 * Odds and Ends::
248 * Sample .emacs File::
249 * Performance Issues::
250 * Limitations and Known Bugs::
251 * FAQ::
252 * Updating CC Mode::
253 * Mailing Lists and Bug Reports::
254 * GNU Free Documentation License::
255 * Command and Function Index::
256 * Variable Index::
257 * Concept and Key Index::
258
259 @detailmenu
260 --- The Detailed Node Listing ---
261
262 Commands
263
264 * Indentation Commands::
265 * Comment Commands::
266 * Movement Commands::
267 * Filling and Breaking::
268 * Minor Modes::
269 * Electric Keys::
270 * Auto-newlines::
271 * Hungry WS Deletion::
272 * Subword Movement::
273 * Other Commands::
274
275 Font Locking
276
277 * Font Locking Preliminaries::
278 * Faces::
279 * Doc Comments::
280 * AWK Mode Font Locking::
281
282 Configuration Basics
283
284 * CC Hooks::
285 * Style Variables::
286 * Styles::
287
288 Styles
289
290 * Built-in Styles::
291 * Choosing a Style::
292 * Adding Styles::
293 * Guessing the Style::
294 * File Styles::
295
296 Customizing Auto-newlines
297
298 * Hanging Braces::
299 * Hanging Colons::
300 * Hanging Semicolons and Commas::
301
302 Hanging Braces
303
304 * Custom Braces::
305
306 Indentation Engine Basics
307
308 * Syntactic Analysis::
309 * Syntactic Symbols::
310 * Indentation Calculation::
311
312 Syntactic Symbols
313
314 * Function Symbols::
315 * Class Symbols::
316 * Conditional Construct Symbols::
317 * Switch Statement Symbols::
318 * Brace List Symbols::
319 * External Scope Symbols::
320 * Paren List Symbols::
321 * Literal Symbols::
322 * Multiline Macro Symbols::
323 * Objective-C Method Symbols::
324 * Java Symbols::
325 * Statement Block Symbols::
326 * K&R Symbols::
327
328 Customizing Indentation
329
330 * c-offsets-alist::
331 * Interactive Customization::
332 * Line-Up Functions::
333 * Custom Line-Up::
334 * Other Indentation::
335
336 Line-Up Functions
337
338 * Brace/Paren Line-Up::
339 * List Line-Up::
340 * Operator Line-Up::
341 * Comment Line-Up::
342 * Misc Line-Up::
343
344 @end detailmenu
345 @end menu
346
347 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
348 @node Introduction, Overview, Top, Top
349 @comment node-name, next, previous, up
350 @chapter Introduction
351 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
352
353 @cindex BOCM
354 @cindex history
355 @cindex awk-mode.el
356 @cindex c-mode.el
357 @cindex c++-mode.el
358
359 Welcome to @ccmode{}, a GNU Emacs mode for editing files containing C,
360 C++, Objective-C, Java, CORBA IDL (and the variants CORBA PSDL and
361 CIDL), Pike and AWK code. This incarnation of the mode is descended
362 from @file{c-mode.el} (also called ``Boring Old C Mode'' or BOCM
363 @t{:-)}, @file{c++-mode.el} version 2, which Barry Warsaw had been
364 maintaining since 1992, and @file{awk-mode.el}, a long neglected mode
365 in the (X)Emacs base.
366
367 Late in 1997, Martin Stjernholm joined Barry on the @ccmode{}
368 Maintainers Team, and implemented the Pike support. In 2000 Martin
369 took over as the sole maintainer. In 2001 Alan Mackenzie joined the
370 team, implementing AWK support in version 5.30. @ccmode{} did not
371 originally contain the font lock support for its languages --- that
372 was added in version 5.30.
373
374 This manual describes @ccmode{}
375 @comment The following line must appear on its own, so that the
376 version 5.31.
377 @comment Release.py script can update the version number automatically
378
379 @ccmode{} supports the editing of K&R and ANSI C, C++, Objective-C,
380 Java, CORBA's Interface Definition Language, Pike@footnote{A C-like
381 scripting language with its roots in the LPC language used in some MUD
382 engines. See @uref{http://pike.ida.liu.se/}.} and AWK files. In this
383 way, you can easily set up consistent font locking and coding styles for
384 use in editing all of these languages, although AWK is not yet as
385 uniformly integrated as the other languages.
386
387 @findex c-mode
388 @findex c++-mode
389 @findex objc-mode
390 @findex java-mode
391 @findex idl-mode
392 @findex pike-mode
393 @findex awk-mode
394 Note that the name of this package is ``@ccmode{}'', but there is no top
395 level @code{cc-mode} entry point. All of the variables, commands, and
396 functions in @ccmode{} are prefixed with @code{c-@var{thing}}, and
397 @code{c-mode}, @code{c++-mode}, @code{objc-mode}, @code{java-mode},
398 @code{idl-mode}, @code{pike-mode}, and @code{awk-mode} entry points are
399 provided. This package is intended to be a replacement for
400 @file{c-mode.el}, @file{c++-mode.el} and @file{awk-mode.el}.
401
402 A special word of thanks goes to Krishna Padmasola for his work in
403 converting the original @file{README} file to Texinfo format. I'd
404 also like to thank all the @ccmode{} victims who help enormously
405 during the early beta stages of @ccmode{}'s development.
406
407 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
408 @node Overview, Getting Started, Introduction, Top
409 @comment node-name, next, previous, up@cindex organization of the manual
410 @chapter Overview of the Manual
411 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
412
413 @noindent
414 The manual starts with several introductory chapters (including this
415 one).
416
417 @noindent
418 The next chunk of the manual describes the day to day @emph{use} of
419 @ccmode{} (as contrasted with how to customize it).
420
421 @itemize @bullet
422 @item
423 The chapter ``Commands'' describes in detail how to use (nearly) all
424 of @ccmode{}'s features. There are extensive cross-references from
425 here to the corresponding sections later in the manual which tell you
426 how to customize these features.
427
428 @item
429 ``Font Locking'' describes how ``syntax highlighting'' is applied to
430 your buffers. It is mainly background information and can be skipped
431 over at a first reading.
432 @end itemize
433
434 @noindent
435 The next chunk of the manual describes how to @emph{customize}
436 @ccmode{}. Typically, an overview of a topic is given at the chapter
437 level, then the sections and subsections describe the material in
438 increasing detail.
439
440 @itemize @bullet
441 @item
442 The chapter ``Configuration Basics'' tells you @emph{how} to write
443 customizations - whether in hooks, in styles, in both, or in neither,
444 depending on your needs. It describes the @ccmode{} style system and
445 lists the standard styles that @ccmode{} supplies.
446
447 @item
448 The next few chapters describe in detail how to customize the various
449 features of @ccmode{}.
450
451 @item
452 Finally, there is a sample @file{.emacs} fragment, which might help you
453 in creating your own customization.
454 @end itemize
455
456 @noindent
457 The manual ends with ``this and that'', things that don't fit cleanly
458 into any of the previous chunks.
459
460 @itemize @bullet
461 @item
462 Two chapters discuss the performance of @ccmode{} and known
463 bugs/limitations.
464
465 @item
466 The FAQ contains a list of common problems and questions.
467
468 @item
469 The next two chapters tell you how to get in touch with the @ccmode{}
470 project - whether for updating @ccmode{} or submitting bug reports.
471 @end itemize
472
473 @noindent
474 Finally, there are the customary indices.
475
476 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
477 @node Getting Started, Commands, Overview, Top
478 @comment node-name, next, previous, up
479 @chapter Getting Started
480 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
481
482 If you got this version of @ccmode{} with Emacs or XEmacs, it should
483 work just fine right out of the box. Note however that you might not
484 have the latest @ccmode{} release and might want to upgrade your copy
485 (see below).
486
487 You should probably start by skimming through the entire Commands chapter
488 (@pxref{Commands}) to get an overview of @ccmode{}'s capabilities.
489
490 After trying out some commands, you may dislike some aspects of
491 @ccmode{}'s default configuration. Here is an outline of how to
492 change some of the settings that newcomers to @ccmode{} most often
493 want to change:
494
495 @table @asis
496 @item c-basic-offset
497 This Lisp variable holds an integer, the number of columns @ccmode{}
498 indents nested code. To set this value to 6, customize
499 @code{c-basic-offset} or put this into your @file{.emacs}:
500
501 @example
502 (setq c-basic-offset 6)
503 @end example
504
505 @item The (indentation) style
506 The basic ``shape'' of indentation created by @ccmode{}---by default,
507 this is @code{gnu} style (except for Java and AWK buffers). A list of
508 the available styles and their descriptions can be found in
509 @ref{Built-in Styles}. A complete specification of the @ccmode{}
510 style system, including how to create your own style, can be found in
511 the chapter @ref{Styles}. To set your style to @code{linux}, either
512 customize @code{c-default-style} or put this into your @file{.emacs}:
513
514 @example
515 (setq c-default-style '((java-mode . "java")
516 (awk-mode . "awk")
517 (other . "linux")))
518 @end example
519
520 @item Electric Indentation
521 Normally, when you type ``punctuation'' characters such as @samp{;} or
522 @samp{@{}, @ccmode{} instantly reindents the current line. This can
523 be disconcerting until you get used to it. To disable @dfn{electric
524 indentation} in the current buffer, type @kbd{C-c C-l}. Type the same
525 thing to enable it again. To have electric indentation disabled by
526 default, put the following into your @file{.emacs} file@footnote{There
527 is no ``easy customization'' facility for making this change.}:
528
529 @example
530 (setq-default c-electric-flag nil)
531 @end example
532
533 @noindent
534 Details of this and other similar ``Minor Modes'' appear in the
535 section @ref{Minor Modes}.
536
537 @item Making the @key{RET} key indent the new line
538 The standard Emacs binding for @key{RET} just adds a new line. If you
539 want it to reindent the new line as well, rebind the key. Note that
540 the action of rebinding would fail if the pertinent keymap didn't yet
541 exist---we thus need to delay the action until after @ccmode{} has
542 been loaded. Put the following code into your @file{.emacs}:
543
544 @example
545 (defun my-make-CR-do-indent ()
546 (define-key c-mode-base-map "\C-m" 'c-context-line-break))
547 (add-hook 'c-initialization-hook 'my-make-CR-do-indent)
548 @end example
549
550 @noindent
551 This example demonstrates the use of a very powerful @ccmode{} (and
552 Emacs) facility, the hook. The use of @ccmode{}'s hooks is described
553 in @ref{CC Hooks}.
554 @end table
555
556 All these settings should occur in your @file{.emacs} @emph{before}
557 any @ccmode{} buffers get loaded---in particular, before any call of
558 @code{desktop-read}.
559
560 As you get to know the mode better, you may want to make more
561 ambitious changes to your configuration. For this, you should start
562 reading the chapter @ref{Config Basics}.
563
564 If you are upgrading an existing @ccmode{} installation, please see
565 the @file{README} file for installation details. In particular, if
566 you are going to be editing AWK files, @file{README} describes how to
567 configure your (X)Emacs so that @ccmode{} will supersede the obsolete
568 @code{awk-mode.el} which might have been supplied with your (X)Emacs.
569 @ccmode{} might not work with older versions of Emacs or XEmacs. See
570 the @ccmode{} release notes at @uref{http://cc-mode.sourceforge.net}
571 for the latest information on Emacs version and package compatibility
572 (@pxref{Updating CC Mode}).
573
574 @deffn Command c-version
575 @findex version (c-)
576 You can find out what version of @ccmode{} you are using by visiting a C
577 file and entering @kbd{M-x c-version RET}. You should see this message in
578 the echo area:
579
580 @example
581 Using CC Mode version 5.XX
582 @end example
583
584 @noindent
585 where @samp{XX} is the minor release number.
586 @end deffn
587
588 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
589 @node Commands, Font Locking, Getting Started, Top
590 @comment node-name, next, previous, up
591 @chapter Commands
592 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
593
594 This chapter specifies all of CC Mode's commands, and thus contains
595 nearly everything you need to know to @emph{use} @ccmode{} (as
596 contrasted with configuring it). @dfn{Commands} here means both
597 control key sequences and @dfn{electric keys}, these being characters
598 such as @samp{;} which, as well as inserting themselves into the
599 buffer, also do other things.
600
601 You might well want to review
602 @ifset XEMACS
603 @ref{Lists,,,@emacsman{}, @emacsmantitle{}},
604 @end ifset
605 @ifclear XEMACS
606 @ref{Moving by Parens,,,@emacsman{}, @emacsmantitle{}},
607 @end ifclear
608 which describes commands for moving around brace and parenthesis
609 structures.
610
611
612 @menu
613 * Indentation Commands::
614 * Comment Commands::
615 * Movement Commands::
616 * Filling and Breaking::
617 * Minor Modes::
618 * Electric Keys::
619 * Auto-newlines::
620 * Hungry WS Deletion::
621 * Subword Movement::
622 * Other Commands::
623 @end menu
624
625 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
626 @node Indentation Commands, Comment Commands, Commands, Commands
627 @comment node-name, next, previous,up
628 @section Indentation Commands
629 @cindex indentation
630 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
631
632 The following commands reindent C constructs. Note that when you
633 change your coding style, either interactively or through some other
634 means, your file does @emph{not} automatically get reindented. You
635 will need to execute one of the following commands to see the effects
636 of your changes.
637
638 @cindex GNU indent program
639 Also, variables like @code{c-hanging-*} and @code{c-cleanup-list}
640 (@pxref{Custom Auto-newlines}) only affect how on-the-fly code is
641 formatted. Changing the ``hanginess'' of a brace and then
642 reindenting, will not move the brace to a different line. For this,
643 you're better off getting an external program like GNU @code{indent},
644 which will rearrange brace location, amongst other things.
645
646 Preprocessor directives are handled as syntactic whitespace from other
647 code, i.e. they can be interspersed anywhere without affecting the
648 indentation of the surrounding code, just like comments.
649
650 The code inside macro definitions is, by default, still analyzed
651 syntactically so that you get relative indentation there just as you'd
652 get if the same code was outside a macro. However, since there is no
653 hint about the syntactic context, i.e. whether the macro expands to an
654 expression, to some statements, or perhaps to whole functions, the
655 syntactic recognition can be wrong. @ccmode{} manages to figure it
656 out correctly most of the time, though.
657
658 Reindenting large sections of code can take a long time. When
659 @ccmode{} reindents a region of code, it is essentially equivalent to
660 hitting @key{TAB} on every line of the region.
661
662 These commands indent code:
663
664 @table @asis
665 @item @kbd{@key{TAB}} (@code{c-indent-command})
666 @kindex TAB
667 @findex c-indent-command
668 @findex indent-command (c-)
669 This command indents the current line. That is all you need to know
670 about it for normal use.
671
672 @code{c-indent-command} does different things, depending on the
673 setting of @code{c-syntactic-indentation} (@pxref{Indentation Engine
674 Basics}):
675
676 @itemize @bullet
677 @item
678 When it's non-@code{nil} (which it normally is), the command indents
679 the line according to its syntactic context. With a prefix argument
680 (@kbd{C-u @key{TAB}}), it will re-indent the entire
681 expression@footnote{this is only useful for a line starting with a
682 comment opener or an opening brace, parenthesis, or string quote.}
683 that begins at the line's left margin.
684
685 @item
686 When it's @code{nil}, the command indents the line by an extra
687 @code{c-basic-offset} columns. A prefix argument acts as a
688 multiplier. A bare prefix (@kbd{C-u @key{TAB}}) is equivalent to -1,
689 removing @code{c-basic-offset} columns from the indentation.
690 @end itemize
691
692 The precise behavior is modified by several variables: With
693 @code{c-tab-always-indent}, you can make @key{TAB} insert whitespace
694 in some circumstances---@code{c-insert-tab-function} then defines
695 precisely what sort of ``whitespace'' this will be. Set the standard
696 Emacs variable @code{indent-tabs-mode} to @code{t} if you want real
697 @samp{tab} characters to be used in the indentation, to @code{nil} if
698 you want only spaces. @xref{Just Spaces,,, @emacsman{},
699 @emacsmantitle{}}.
700
701 @defopt c-tab-always-indent
702 @vindex tab-always-indent (c-)
703 @cindex literal
704 This variable modifies how @key{TAB} operates.
705 @itemize @bullet
706 @item
707 When it is @code{t} (the default), @key{TAB} simply indents the
708 current line.
709 @item
710 When it is @code{nil}, @key{TAB} (re)indents the line only if point is
711 to the left of the first non-whitespace character on the line.
712 Otherwise it inserts some whitespace (a tab or an equivalent number of
713 spaces - see below) at point.
714 @item
715 With some other value, the line is reindented. Additionally, if point
716 is within a string or comment, some whitespace is inserted.
717 @end itemize
718 @end defopt
719
720 @defopt c-insert-tab-function
721 @vindex insert-tab-function (c-)
722 @findex tab-to-tab-stop
723 When ``some whitespace'' is inserted as described above, what actually
724 happens is that the function stored in @code{c-insert-tab-function} is
725 called. Normally, this is @code{insert-tab}, which inserts a real tab
726 character or the equivalent number of spaces (depending on
727 @code{indent-tabs-mode}). Some people, however, set
728 @code{c-insert-tab-function} to @code{tab-to-tab-stop} so as to get
729 hard tab stops when indenting.
730 @end defopt
731 @end table
732
733 @noindent
734 The kind of indentation the next five commands do depends on the
735 setting of @code{c-syntactic-indentation} (@pxref{Indentation Engine
736 Basics}):
737 @itemize @bullet
738 @item
739 when it is non-@code{nil} (the default), the commands indent lines
740 according to their syntactic context;
741 @item
742 when it is @code{nil}, they just indent each line the same amount as
743 the previous non-blank line. The commands that indent a region aren't
744 very useful in this case.
745 @end itemize
746
747 @table @asis
748 @item @kbd{C-j} (@code{newline-and-indent})
749 @kindex C-j
750 @findex newline-and-indent
751 Inserts a newline and indents the new blank line, ready to start
752 typing. This is a standard (X)Emacs command.
753
754 @item @kbd{C-M-q} (@code{c-indent-exp})
755 @kindex C-M-q
756 @findex c-indent-exp
757 @findex indent-exp (c-)
758 Indents an entire balanced brace or parenthesis expression. Note that
759 point must be on the opening brace or parenthesis of the expression
760 you want to indent.
761
762 @item @kbd{C-c C-q} (@code{c-indent-defun})
763 @kindex C-c C-q
764 @findex c-indent-defun
765 @findex indent-defun (c-)
766 Indents the entire top-level function, class or macro definition
767 encompassing point. It leaves point unchanged. This function can't be
768 used to reindent a nested brace construct, such as a nested class or
769 function, or a Java method. The top-level construct being reindented
770 must be complete, i.e. it must have both a beginning brace and an ending
771 brace.
772
773 @item @kbd{C-M-\} (@code{indent-region})
774 @kindex C-M-\
775 @findex indent-region
776 Indents an arbitrary region of code. This is a standard Emacs command,
777 tailored for C code in a @ccmode{} buffer. Note, of course, that point
778 and mark must delineate the region you want to indent.
779
780 @item @kbd{C-M-h} (@code{c-mark-function})
781 @kindex C-M-h
782 @findex c-mark-function
783 @findex mark-function (c-)
784 While not strictly an indentation command, this is useful for marking
785 the current top-level function or class definition as the current
786 region. As with @code{c-indent-defun}, this command operates on
787 top-level constructs, and can't be used to mark say, a Java method.
788 @end table
789
790 These variables are also useful when indenting code:
791
792 @defopt indent-tabs-mode
793 This is a standard Emacs variable that controls how line indentation
794 is composed. When it's non-@code{nil}, tabs can be used in a line's
795 indentation, otherwise only spaces are used.
796 @end defopt
797
798 @defopt c-progress-interval
799 @vindex progress-interval (c-)
800 When indenting large regions of code, this variable controls how often a
801 progress message is displayed. Set this variable to @code{nil} to
802 inhibit the progress messages, or set it to an integer which is how
803 often (in seconds) progress messages are to be displayed.
804 @end defopt
805
806 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
807 @node Comment Commands, Movement Commands, Indentation Commands, Commands
808 @comment node-name, next, previous, up
809 @section Comment Commands
810 @cindex comments (insertion of)
811 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
812
813 @table @asis
814 @item @kbd{C-c C-c} (@code{comment-region})
815 @kindex C-c C-c
816 @findex comment-region
817 This command comments out the lines that start in the region. With a
818 negative argument, it does the opposite - it deletes the comment
819 delimiters from these lines. @xref{Multi-Line Comments,,, emacs, GNU
820 Emacs Manual}, for fuller details. @code{comment-region} isn't
821 actually part of @ccmode{} - it is given a @ccmode{} binding for
822 convenience.
823
824 @item @kbd{M-;} (@code{comment-dwim} or @code{indent-for-comment} @footnote{The name of this command varies between (X)Emacs versions.})
825 @kindex M-;
826 @findex comment-dwim
827 @findex indent-for-comment
828 Insert a comment at the end of the current line, if none is there
829 already. Then reindent the comment according to @code{comment-column}
830 @ifclear XEMACS
831 (@pxref{Options for Comments,,, emacs, GNU Emacs Manual})
832 @end ifclear
833 @ifset XEMACS
834 (@pxref{Comments,,, xemacs, XEmacs User's Manual})
835 @end ifset
836 and the variables below. Finally, position the point after the
837 comment starter. @kbd{C-u M-;} kills any comment on the current line,
838 together with any whitespace before it. This is a standard Emacs
839 command, but @ccmode{} enhances it a bit with two variables:
840
841 @defopt c-indent-comment-alist
842 @vindex indent-comment-alist (c-)
843 @vindex comment-column
844 This style variable allows you to vary the column that @kbd{M-;} puts
845 the comment at, depending on what sort of code is on the line, and
846 possibly the indentation of any similar comment on the preceding line.
847 It is an association list that maps different types of lines to
848 actions describing how they should be handled. If a certain line type
849 isn't present on the list then the line is indented to the column
850 specified by @code{comment-column}.
851
852 See the documentation string for a full description of this
853 variable (use @kbd{C-h v c-indent-comment-alist}).
854 @end defopt
855
856 @defopt c-indent-comments-syntactically-p
857 @vindex indent-comments-syntactically-p (c-)
858 Normally, when this style variable is @code{nil}, @kbd{M-;} will
859 indent comment-only lines according to @code{c-indent-comment-alist},
860 just as it does with lines where other code precede the comments.
861 However, if you want it to act just like @key{TAB} for comment-only
862 lines you can get that by setting
863 @code{c-indent-comments-syntactically-p} to non-@code{nil}.
864
865 If @code{c-indent-comments-syntactically-p} is non-@code{nil} then
866 @code{c-indent-comment-alist} won't be consulted at all for comment-only
867 lines.
868 @end defopt
869 @end table
870
871 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
872 @node Movement Commands, Filling and Breaking, Comment Commands, Commands
873 @comment node-name, next, previous, up
874 @section Movement Commands
875 @cindex movement
876 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
877
878 @ccmode{} contains some useful commands for moving around in C code.
879
880 @table @asis
881 @item @kbd{C-M-a} (@code{c-beginning-of-defun})
882 @itemx @kbd{C-M-e} (@code{c-end-of-defun})
883 @findex c-beginning-of-defun
884 @findex c-end-of-defun
885
886 Move to the beginning or end of the current or next function. Other
887 constructs (such as a structs or classes) which have a brace block
888 also count as ``functions'' here. To move over several functions, you
889 can give these commands a repeat count.
890
891 The start of a function is at its header. The end of the function is
892 after its closing brace, or after the semicolon of a construct (such
893 as a @code{struct}) which doesn't end at the brace. These two
894 commands try to leave point at the beginning of a line near the actual
895 start or end of the function. This occasionally causes point not to
896 move at all.
897
898 These functions are analogous to the Emacs built-in commands
899 @code{beginning-of-defun} and @code{end-of-defun}, except they
900 eliminate the constraint that the top-level opening brace of the defun
901 must be in column zero. See @ref{Defuns,,,@emacsman{},
902 @emacsmantitle{}}, for more information.
903
904 @item @kbd{C-M-a} (AWK Mode) (@code{c-awk-beginning-of-defun})
905 @itemx @kbd{C-M-e} (AWK Mode) (@code{c-awk-end-of-defun})
906 @kindex C-M-a (AWK Mode)
907 @kindex C-M-e (AWK Mode)
908 @findex c-awk-beginning-of-defun
909 @findex awk-beginning-of-defun (c-)
910 @findex c-awk-end-of-defun
911 @findex awk-end-of-defun (c-)
912 Move to the beginning or end of the current or next AWK defun. These
913 commands can take prefix-arguments, their functionality being entirely
914 equivalent to @code{beginning-of-defun} and @code{end-of-defun}.
915
916 AWK Mode @dfn{defuns} are either pattern/action pairs (either of which
917 might be implicit) or user defined functions. Having the @samp{@{} and
918 @samp{@}} (if there are any) in column zero, as is suggested for some
919 modes, is neither necessary nor helpful in AWK mode.
920
921 @item @kbd{M-a} (@code{c-beginning-of-statement})
922 @itemx @kbd{M-e} (@code{c-end-of-statement})
923 @kindex M-a
924 @kindex M-e
925 @findex c-beginning-of-statement
926 @findex c-end-of-statement
927 @findex beginning-of-statement (c-)
928 @findex end-of-statement (c-)
929 Move to the beginning or end of the innermost C statement. If point
930 is already there, move to the next beginning or end of a statement,
931 even if that means moving into a block. (Use @kbd{C-M-b} or
932 @kbd{C-M-f} to move over a balanced block.) A prefix argument @var{n}
933 means move over @var{n} statements.
934
935 If point is within or next to a comment or a string which spans more
936 than one line, these commands move by sentences instead of statements.
937
938 When called from a program, these functions take three optional
939 arguments: the repetition count, a buffer position limit which is the
940 farthest back to search for the syntactic context, and a flag saying
941 whether to do sentence motion in or near comments and multiline
942 strings.
943
944 @item @kbd{C-c C-u} (@code{c-up-conditional})
945 @kindex C-c C-u
946 @findex c-up-conditional
947 @findex up-conditional (c-)
948 Move back to the containing preprocessor conditional, leaving the mark
949 behind. A prefix argument acts as a repeat count. With a negative
950 argument, move forward to the end of the containing preprocessor
951 conditional.
952
953 @samp{#elif} is treated like @samp{#else} followed by @samp{#if}, so the
954 function stops at them when going backward, but not when going
955 forward.
956
957 This key sequence is not bound in AWK Mode, which doesn't have
958 preprocessor statements.
959
960 @item @kbd{M-x c-up-conditional-with-else}
961 @findex c-up-conditional-with-else
962 @findex up-conditional-with-else (c-)
963 A variety of @code{c-up-conditional} that also stops at @samp{#else}
964 lines. Normally those lines are ignored.
965
966 @item @kbd{M-x c-down-conditional}
967 @findex c-down-conditional
968 @findex down-conditional (c-)
969 Move forward into the next nested preprocessor conditional, leaving
970 the mark behind. A prefix argument acts as a repeat count. With a
971 negative argument, move backward into the previous nested preprocessor
972 conditional.
973
974 @samp{#elif} is treated like @samp{#else} followed by @samp{#if}, so the
975 function stops at them when going forward, but not when going backward.
976
977 @item @kbd{M-x c-down-conditional-with-else}
978 @findex c-down-conditional-with-else
979 @findex down-conditional-with-else (c-)
980 A variety of @code{c-down-conditional} that also stops at @samp{#else}
981 lines. Normally those lines are ignored.
982
983 @item @kbd{C-c C-p} (@code{c-backward-conditional})
984 @itemx @kbd{C-c C-n} (@code{c-forward-conditional})
985 @kindex C-c C-p
986 @kindex C-c C-n
987 @findex c-backward-conditional
988 @findex c-forward-conditional
989 @findex backward-conditional (c-)
990 @findex forward-conditional (c-)
991 Move backward or forward across a preprocessor conditional, leaving
992 the mark behind. A prefix argument acts as a repeat count. With a
993 negative argument, move in the opposite direction.
994
995 These key sequences are not bound in AWK Mode, which doesn't have
996 preprocessor statements.
997
998 @item @kbd{M-x c-backward-into-nomenclature}
999 @itemx @kbd{M-x c-forward-into-nomenclature}
1000 @findex c-backward-into-nomenclature
1001 @findex c-forward-into-nomenclature
1002 @findex backward-into-nomenclature (c-)
1003 @findex forward-into-nomenclature (c-)
1004 A popular programming style, especially for object-oriented languages
1005 such as C++ is to write symbols in a mixed case format, where the
1006 first letter of each word is capitalized, and not separated by
1007 underscores. E.g. @samp{SymbolsWithMixedCaseAndNoUnderlines}.
1008
1009 These commands move backward or forward to the beginning of the next
1010 capitalized word. With prefix argument @var{n}, move @var{n} times.
1011 If @var{n} is negative, move in the opposite direction.
1012
1013 Note that these two commands have been superseded by
1014 @code{subword-mode}, which you should use instead. @xref{Subword
1015 Movement}. They might be removed from a future release of @ccmode{}.
1016 @end table
1017
1018 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1019 @node Filling and Breaking, Minor Modes, Movement Commands, Commands
1020 @comment node-name, next, previous, up
1021 @section Filling and Line Breaking Commands
1022 @cindex text filling
1023 @cindex line breaking
1024 @cindex comment handling
1025 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1026
1027 Since there's a lot of normal text in comments and string literals,
1028 @ccmode{} provides features to edit these like in text mode. The goal
1029 is to do it seamlessly, i.e. you can use auto fill mode, sentence and
1030 paragraph movement, paragraph filling, adaptive filling etc. wherever
1031 there's a piece of normal text without having to think much about it.
1032 @ccmode{} keeps the indentation, fixes suitable comment line prefixes,
1033 and so on.
1034
1035 You can configure the exact way comments get filled and broken, and
1036 where Emacs does auto-filling (see @pxref{Custom Filling and
1037 Breaking}). Typically, the style system (@pxref{Styles}) will have
1038 set this up for you, so you probably won't have to bother.
1039
1040 @findex auto-fill-mode
1041 @cindex Auto Fill mode
1042 @cindex paragraph filling
1043 Line breaks are by default handled (almost) the same regardless of
1044 whether they are made by auto fill mode (@pxref{Auto Fill,,,
1045 @emacsman{}, @emacsmantitle{}}), by paragraph filling (e.g. with
1046 @kbd{M-q}), or explicitly with @kbd{M-j} or similar methods. In
1047 string literals, the new line gets the same indentation as the
1048 previous nonempty line.@footnote{You can change this default by
1049 setting the @code{string} syntactic symbol (@pxref{Syntactic Symbols}
1050 and @pxref{Customizing Indentation})}.
1051
1052 @table @asis
1053 @item @kbd{M-q} (@code{c-fill-paragraph})
1054 @kindex M-q
1055 @findex c-fill-paragraph
1056 @findex fill-paragraph (c-)
1057 @cindex Javadoc markup
1058 @cindex Pike autodoc markup
1059 This command fills multiline string literals and both block
1060 and line style comments. In Java buffers, the Javadoc markup words
1061 are recognized as paragraph starters. The line oriented Pike autodoc
1062 markup words are recognized in the same way in Pike mode.
1063
1064 The formatting of the starters (@code{/*}) and enders (@code{*/}) of
1065 block comments are kept as they were before the filling. I.e., if
1066 either the starter or ender were on a line of its own, then it stays
1067 on its own line; conversely, if the delimiter has comment text on its
1068 line, it keeps at least one word of that text with it on the line.
1069
1070 This command is the replacement for @code{fill-paragraph} in @ccmode{}
1071 buffers.
1072
1073 @item @kbd{M-j} (@code{c-indent-new-comment-line})
1074 @kindex M-j
1075 @findex c-indent-new-comment-line
1076 @findex indent-new-comment-line (c-)
1077 This breaks the current line at point and indents the new line. If
1078 point was in a comment, the new line gets the proper comment line
1079 prefix. If point was inside a macro, a backslash is inserted before
1080 the line break. It is the replacement for
1081 @code{indent-new-comment-line}.
1082
1083 @item @kbd{M-x c-context-line-break}
1084 @findex c-context-line-break
1085 @findex context-line-break (c-)
1086 Insert a line break suitable to the context: If the point is inside a
1087 comment, the new line gets the suitable indentation and comment line
1088 prefix like @code{c-indent-new-comment-line}. In normal code it's
1089 indented like @code{newline-and-indent} would do. In macros it acts
1090 like @code{newline-and-indent} but additionally inserts and optionally
1091 aligns the line ending backslash so that the macro remains unbroken.
1092 @xref{Custom Macros}, for details about the backslash alignment. In a
1093 string, a backslash is inserted only if the string is within a
1094 macro@footnote{In GCC, unescaped line breaks within strings are
1095 valid.}.
1096
1097 This function is not bound to a key by default, but it's intended to be
1098 used on the @kbd{RET} key. If you like the behavior of
1099 @code{newline-and-indent} on @kbd{RET}, you should consider switching to
1100 this function. @xref{Sample .emacs File}.
1101
1102 @item @kbd{M-x c-context-open-line}
1103 @findex c-context-open-line
1104 @findex context-open-line (c-)
1105 This is to @kbd{C-o} (@kbd{M-x open-line}) as
1106 @code{c-context-line-break} is to @kbd{RET}. I.e. it works just like
1107 @code{c-context-line-break} but leaves the point before the inserted
1108 line break.
1109 @end table
1110
1111
1112 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1113 @node Minor Modes, Electric Keys, Filling and Breaking, Commands
1114 @comment node-name, next, previous, up
1115 @section Minor Modes
1116 @cindex Minor Modes
1117 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1118
1119 @ccmode{} contains several minor-mode-like features that you might
1120 find useful while writing new code or editing old code:
1121
1122 @table @asis
1123 @item electric mode
1124 When this is enabled, certain visible characters cause reformatting as
1125 they are typed. This is normally helpful, but can be a nuisance when
1126 editing chaotically formatted code. It can also be disconcerting,
1127 especially for users who are new to @ccmode{}.
1128 @item auto-newline mode
1129 This automatically inserts newlines where you'd probably want to type
1130 them yourself, e.g. after typing @samp{@}}s. Its action is suppressed
1131 when electric mode is disabled.
1132 @item hungry-delete mode
1133 This lets you delete a contiguous block of whitespace with a single
1134 key - for example, the newline and indentation just inserted by
1135 auto-newline when you want to back up and write a comment after the
1136 last statement.
1137 @item subword mode
1138 This mode makes basic word movement commands like @kbd{M-f}
1139 (@code{forward-word}) and @kbd{M-b} (@code{backward-word}) treat the
1140 parts of sillycapsed symbols as different words.
1141 E.g. @samp{NSGraphicsContext} is treated as three words @samp{NS},
1142 @samp{Graphics}, and @samp{Context}.
1143 @item syntactic-indentation mode
1144 When this is enabled (which it normally is), indentation commands such
1145 as @kbd{C-j} indent lines of code according to their syntactic
1146 structure. Otherwise, a line is simply indented to the same level as
1147 the previous one and @kbd{@key{TAB}} adjusts the indentation in steps
1148 of `c-basic-offset'.
1149 @end table
1150
1151 Full details on how these minor modes work are at @ref{Electric Keys},
1152 @ref{Auto-newlines}, @ref{Hungry WS Deletion}, @ref{Subword Movement},
1153 and @ref{Indentation Engine Basics}.
1154
1155 You can toggle each of these minor modes on and off, and you can
1156 configure @ccmode{} so that it starts up with your favourite
1157 combination of them (@pxref{Sample .emacs File}). By default, when
1158 you initialize a buffer, electric mode and syntactic-indentation mode
1159 are enabled but the other two modes are disabled.
1160
1161 @ccmode{} displays the current state of the first four of these minor
1162 modes on the modeline by appending letters to the major mode's name,
1163 one letter for each enabled minor mode - @samp{l} for electric mode,
1164 @samp{a} for auto-newline mode, @samp{h} for hungry delete mode, and
1165 @samp{w} for subword mode. If all these modes were enabled, you'd see
1166 @samp{C/lahw}@footnote{The @samp{C} would be replaced with the name of
1167 the language in question for the other languages @ccmode{} supports.}.
1168
1169 Here are the commands to toggle these modes:
1170
1171 @table @asis
1172 @item @kbd{C-c C-l} (@code{c-toggle-electric-state})
1173 @kindex C-c C-l
1174 @findex c-toggle-electric-state
1175 @findex toggle-electric-state (c-)
1176 Toggle electric minor mode. When the command turns the mode off, it
1177 also suppresses auto-newline mode.
1178
1179 @item @kbd{C-c C-a} (@code{c-toggle-auto-newline})
1180 @kindex C-c C-a
1181 @findex c-toggle-auto-newline
1182 @findex toggle-auto-newline (c-)
1183 Toggle auto-newline minor mode. When the command turns the mode on,
1184 it also enables electric minor mode.
1185
1186 @item @kbd{M-x c-toggle-hungry-state}@footnote{Prior to @ccmode{} 5.31, this command was bound to @kbd{C-c C-d}.}
1187 @findex c-toggle-hungry-state
1188 @findex toggle-hungry-state (c-)
1189 Toggle hungry-delete minor mode.
1190
1191 @item @kbd{M-x c-toggle-auto-hungry-state}@footnote{Prior to @ccmode{} 5.31, this command was bound to @kbd{C-c C-t}.}
1192 @findex c-toggle-auto-hungry-state
1193 @findex toggle-auto-hungry-state (c-)
1194 Toggle both auto-newline and hungry delete minor modes.
1195
1196 @item @kbd{C-c C-w} (@code{M-x subword-mode})
1197 @kindex C-c C-w
1198 @findex subword-mode
1199 Toggle subword mode.
1200
1201 @item @kbd{M-x c-toggle-syntactic-indentation}
1202 @findex c-toggle-syntactic-indentation
1203 @findex toggle-syntactic-indentation (c-)
1204 Toggle syntactic-indentation mode.
1205 @end table
1206
1207 Common to all the toggle functions above is that if they are called
1208 programmatically, they take an optional numerical argument. A
1209 positive value will turn on the minor mode (or both of them in the
1210 case of @code{c-toggle-auto-hungry-state}) and a negative value will
1211 turn it (or them) off.
1212
1213
1214 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1215 @node Electric Keys, Auto-newlines, Minor Modes, Commands
1216 @comment node-name, next, previous, up
1217 @section Electric Keys and Keywords
1218 @cindex electric characters
1219 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1220
1221 Most punctuation keys provide @dfn{electric} behavior - as well as
1222 inserting themselves they perform some other action, such as
1223 reindenting the line. This reindentation saves you from having to
1224 reindent a line manually after typing, say, a @samp{@}}. A few
1225 keywords, such as @code{else}, also trigger electric action.
1226
1227 You can inhibit the electric behavior described here by disabling
1228 electric minor mode (@pxref{Minor Modes}).
1229
1230 Common to all these keys is that they only behave electrically when
1231 used in normal code (as contrasted with getting typed in a string
1232 literal or comment). Those which cause re-indentation do so only when
1233 @code{c-syntactic-indentation} has a non-@code{nil} value (which it
1234 does by default).
1235
1236 These keys and keywords are:
1237 @c ACM, 2004/8/24: c-electric-pound doesn't check c-s-i: this is more
1238 @c like a bug in the code than a bug in this document. It'll get
1239 @c fixed in the code sometime.
1240
1241 @table @kbd
1242 @item #
1243 @kindex #
1244 @findex c-electric-pound
1245 @findex electric-pound (c-)
1246 @vindex c-electric-pound-behavior
1247 @vindex electric-pound-behavior (c-)
1248 Pound (bound to @code{c-electric-pound}) is electric when typed as the
1249 first non-whitespace character on a line and not within a macro
1250 definition. In this case, the variable @code{c-electric-pound-behavior}
1251 is consulted for the electric behavior. This variable takes a list
1252 value, although the only element currently defined is @code{alignleft},
1253 which tells this command to force the @samp{#} character into column
1254 zero. This is useful for entering preprocessor macro definitions.
1255
1256 Pound is not electric in AWK buffers, where @samp{#} starts a comment,
1257 and is bound to @code{self-insert-command} like any typical printable
1258 character.
1259 @c ACM, 2004/8/24: Change this (and the code) to do AWK comment
1260 @c reindentation.
1261
1262 @item *
1263 @kindex *
1264 @itemx /
1265 @kindex /
1266 @findex c-electric-star
1267 @findex electric-star (c-)
1268 @findex c-electric-slash
1269 @findex electric-slash (c-)
1270 A star (bound to @code{c-electric-star}) or a slash
1271 (@code{c-electric-slash}) causes reindentation when you type it as the
1272 second component of a C style block comment opener (@samp{/*}) or a
1273 C++ line comment opener (@samp{//}) respectively, but only if the
1274 comment opener is the first thing on the line (i.e. there's only
1275 whitespace before it).
1276
1277 Additionally, you can configure @ccmode{} so that typing a slash at
1278 the start of a line within a block comment will terminate the
1279 comment. You don't need to have electric minor mode enabled to get
1280 this behavior. @xref{Clean-ups}.
1281
1282 In AWK mode, @samp{*} and @samp{/} do not delimit comments and are not
1283 electric.
1284
1285 @item <
1286 @kindex <
1287 @itemx >
1288 @kindex >
1289 @findex c-electric-lt-gt
1290 @findex electric-lt-gt (c-)
1291 A less-than or greater-than sign (bound to @code{c-electric-lt-gt}) is
1292 electric in two circumstances: when it is an angle bracket in a C++
1293 @samp{template} declaration (and similar constructs in other
1294 languages) and when it is the second of two @kbd{<} or @kbd{>}
1295 characters in a C++ style stream operator. In either case, the line
1296 is reindented. Angle brackets in C @samp{#include} directives are not
1297 electric.
1298
1299 @item (
1300 @kindex (
1301 @itemx )
1302 @kindex )
1303 @findex c-electric-paren
1304 @findex electric-paren (c-)
1305 The normal parenthesis characters @samp{(} and @samp{)} (bound to
1306 @code{c-electric-paren}) reindent the current line. This is useful
1307 for getting the closing parenthesis of an argument list aligned
1308 automatically.
1309
1310 You can also configure @ccmode{} to insert a space automatically
1311 between a function name and the @samp{(} you've just typed, and to
1312 remove it automatically after typing @samp{)}, should the argument
1313 list be empty. You don't need to have electric minor mode enabled to
1314 get these actions. @xref{Clean-ups}.
1315
1316 @item @{
1317 @kindex @{
1318 @itemx @}
1319 @kindex @}
1320 @findex c-electric-brace
1321 @findex electric-brace (c-)
1322 Typing a brace (bound to @code{c-electric-brace}) reindents the
1323 current line. Also, one or more newlines might be inserted if
1324 auto-newline minor mode is enabled. @xref{Auto-newlines}.
1325 Additionally, you can configure @ccmode{} to compact excess whitespace
1326 inserted by auto-newline mode in certain circumstances.
1327 @xref{Clean-ups}.
1328
1329 @item :
1330 @kindex :
1331 @findex c-electric-colon
1332 @findex electric-colon (c-)
1333 Typing a colon (bound to @code{c-electric-colon}) reindents the
1334 current line. Additionally, one or more newlines might be inserted if
1335 auto-newline minor mode is enabled. @xref{Auto-newlines}. If you
1336 type a second colon immediately after such an auto-newline, by default
1337 the whitespace between the two colons is removed, leaving a C++ scope
1338 operator. @xref{Clean-ups}.
1339
1340 If you prefer, you can insert @samp{::} in a single operation,
1341 avoiding all these spurious reindentations, newlines, and clean-ups.
1342 @xref{Other Commands}.
1343
1344 @item ;
1345 @kindex ;
1346 @itemx ,
1347 @kindex ,
1348 @findex c-electric-semi&comma
1349 @findex electric-semi&comma (c-)
1350 Typing a semicolon or comma (bound to @code{c-electric-semi&comma})
1351 reindents the current line. Also, a newline might be inserted if
1352 auto-newline minor mode is enabled. @xref{Auto-newlines}.
1353 Additionally, you can configure @ccmode{} so that when auto-newline
1354 has inserted whitespace after a @samp{@}}, it will be removed again
1355 when you type a semicolon or comma just after it. @xref{Clean-ups}.
1356
1357 @end table
1358
1359 @deffn Command c-electric-continued-statement
1360 @findex electric-continued-statement (c-)
1361
1362 Certain keywords are electric, causing reindentation when they are
1363 preceded only by whitespace on the line. The keywords are those that
1364 continue an earlier statement instead of starting a new one:
1365 @code{else}, @code{while}, @code{catch} (only in C++ and Java) and
1366 @code{finally} (only in Java).
1367
1368 An example:
1369
1370 @example
1371 @group
1372 for (i = 0; i < 17; i++)
1373 if (a[i])
1374 res += a[i]->offset;
1375 else
1376 @end group
1377 @end example
1378
1379 Here, the @code{else} should be indented like the preceding @code{if},
1380 since it continues that statement. @ccmode{} will automatically
1381 reindent it after the @code{else} has been typed in full, since only
1382 then is it possible to decide whether it's a new statement or a
1383 continuation of the preceding @code{if}.
1384
1385 @vindex abbrev-mode
1386 @findex abbrev-mode
1387 @cindex Abbrev mode
1388 @ccmode{} uses Abbrev mode (@pxref{Abbrevs,,, @emacsman{}, @emacsmantitle{}})
1389 to accomplish this. It's therefore turned on by default in all language
1390 modes except IDL mode, since CORBA IDL doesn't have any statements.
1391 @end deffn
1392
1393
1394 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1395 @node Auto-newlines, Hungry WS Deletion, Electric Keys, Commands
1396 @comment node-name, next, previous, up
1397 @section Auto-newline Insertion
1398 @cindex auto-newline
1399 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1400
1401 When you have @dfn{Auto-newline minor mode} enabled (@pxref{Minor
1402 Modes}), @ccmode{} inserts newlines for you automatically (in certain
1403 syntactic contexts) when you type a left or right brace, a colon, a
1404 semicolon, or a comma. Sometimes a newline appears before the
1405 character you type, sometimes after it, sometimes both.
1406
1407 Auto-newline only triggers when the following conditions hold:
1408
1409 @itemize @bullet
1410 @item
1411 Auto-newline minor mode is enabled, as evidenced by the indicator
1412 @samp{a} after the mode name on the modeline (e.g. @samp{C/a} or
1413 @samp{C/la}).
1414
1415 @item
1416 The character was typed at the end of a line, or with only whitespace
1417 after it, and possibly a @samp{\} escaping the newline.
1418
1419 @item
1420 The character is not on its own line already. (This applies only to
1421 insertion of a newline @emph{before} the character.)
1422
1423 @item
1424 @cindex literal
1425 @cindex syntactic whitespace
1426 The character was not typed inside of a literal @footnote{A
1427 @dfn{literal} is defined as any comment, string, or preprocessor macro
1428 definition. These constructs are also known as @dfn{syntactic
1429 whitespace} since they are usually ignored when scanning C code.}.
1430
1431 @item
1432 No numeric argument was supplied to the command (i.e. it was typed as
1433 normal, with no @kbd{C-u} prefix).
1434 @end itemize
1435
1436 You can configure the precise circumstances in which newlines get
1437 inserted (see @pxref{Custom Auto-newlines}). Typically, the style
1438 system (@pxref{Styles}) will have set this up for you, so you probably
1439 won't have to bother.
1440
1441 Sometimes @ccmode{} inserts an auto-newline where you don't want one,
1442 such as after a @samp{@}} when you're about to type a @samp{;}.
1443 Hungry deletion can help here (@pxref{Hungry WS Deletion}), or you can
1444 activate an appropriate @dfn{clean-up}, which will remove the excess
1445 whitespace after you've typed the @samp{;}. See @ref{Clean-ups} for a
1446 full description. See also @ref{Electric Keys} for a summary of
1447 clean-ups listed by key.
1448
1449
1450 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1451 @node Hungry WS Deletion, Subword Movement, Auto-newlines, Commands
1452 @comment node-name, next, previous, up
1453 @section Hungry Deletion of Whitespace
1454 @cindex hungry-deletion
1455 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1456
1457 If you want to delete an entire block of whitespace at point, you can
1458 use @dfn{hungry deletion}. This deletes all the contiguous whitespace
1459 either before point or after point in a single operation.
1460 ``Whitespace'' here includes tabs and newlines, but not comments or
1461 preprocessor commands. Hungry deletion can markedly cut down on the
1462 number of times you have to hit deletion keys when, for example,
1463 you've made a mistake on the preceding line and have already pressed
1464 @kbd{C-j}.
1465
1466 Hungry deletion is a simple feature that some people find extremely
1467 useful. In fact, you might find yourself wanting it in @strong{all}
1468 your editing modes!
1469
1470 Loosely speaking, in what follows, @dfn{@key{DEL}} means ``the
1471 backspace key'' and @dfn{@key{DELETE}} means ``the forward delete
1472 key''. This is discussed in more detail below.
1473
1474 There are two different ways you can use hungry deletion:
1475
1476 @table @asis
1477 @item Using @dfn{Hungry Delete Mode} with @kbd{@key{DEL}} and @kbd{C-d}
1478 Here you toggle Hungry Delete minor mode with @kbd{M-x
1479 c-toggle-hungry-state}@footnote{Prior to @ccmode{} 5.31, this command
1480 was bound to @kbd{C-c C-d}. @kbd{C-c C-d} is now the default binding
1481 for @code{c-hungry-delete-forward}.} (@pxref{Minor Modes}.) This
1482 makes @kbd{@key{DEL}} and @kbd{C-d} do backwards and forward hungry
1483 deletion.
1484
1485 @table @asis
1486 @item @kbd{@key{DEL}} (@code{c-electric-backspace})
1487 @kindex DEL
1488 @findex c-electric-backspace
1489 @findex electric-backspace (c-)
1490 This command is run by default when you hit the @kbd{DEL} key. When
1491 hungry delete mode is enabled, it deletes any amount of whitespace in
1492 the backwards direction. Otherwise, or when used with a prefix
1493 argument or in a literal (@pxref{Auto-newlines}), the command just
1494 deletes backwards in the usual way. (More precisely, it calls the
1495 function contained in the variable @code{c-backspace-function},
1496 passing it the prefix argument, if any.)
1497
1498 @item @code{c-backspace-function}
1499 @vindex c-backspace-function
1500 @vindex backspace-function (c-)
1501 @findex backward-delete-char-untabify
1502 Hook that gets called by @code{c-electric-backspace} when it doesn't
1503 do an ``electric'' deletion of the preceding whitespace. The default
1504 value is @code{backward-delete-char-untabify}
1505 (@pxref{Deletion,,,@lispref{}, @lispreftitle{}}, the function which
1506 deletes a single character.
1507
1508 @item @kbd{C-d} (@code{c-electric-delete-forward})
1509 @kindex C-d
1510 @findex c-electric-delete-forward
1511 @findex electric-delete-forward (c-)
1512 This function, which is bound to @kbd{C-d} by default, works just like
1513 @code{c-electric-backspace} but in the forward direction. When it
1514 doesn't do an ``electric'' deletion of the following whitespace, it
1515 just does @code{delete-char}, more or less. (Strictly speaking, it
1516 calls the function in @code{c-delete-function} with the prefix
1517 argument.)
1518
1519 @item @code{c-delete-function}
1520 @vindex c-delete-function
1521 @vindex delete-function (c-)
1522 @findex delete-char
1523 Hook that gets called by @code{c-electric-delete-forward} when it
1524 doesn't do an ``electric'' deletion of the following whitespace. The
1525 default value is @code{delete-char}.
1526 @end table
1527
1528 @item Using Distinct Bindings
1529 The other (newer and recommended) way to use hungry deletion is to
1530 perform @code{c-hungry-delete-backwards} and
1531 @code{c-hungry-delete-forward} directly through their key sequences
1532 rather than using the minor mode toggling.
1533
1534 @table @asis
1535 @item @kbd{C-c C-@key{DEL}}, or @kbd{C-c @key{DEL}} (@code{c-hungry-delete-backwards})@footnote{This command was formerly known as @code{c-hungry-backspace}.}
1536 @kindex C-c C-<backspace>
1537 @kindex C-c <backspace>
1538 @kindex C-c C-DEL
1539 @kindex C-c DEL
1540 @findex c-hungry-delete-backwards
1541 @findex hungry-delete-backwards (c-)
1542 Delete any amount of whitespace in the backwards direction (regardless
1543 whether hungry-delete mode is enabled or not). This command is bound
1544 to both @kbd{C-c C-@key{DEL}} and @kbd{C-c @key{DEL}}, since the more
1545 natural one, @kbd{C-c C-@key{DEL}}, is sometimes difficult to type at
1546 a character terminal.
1547
1548 @item @kbd{C-c C-d}, @kbd{C-c C-@key{DELETE}}, or @kbd{C-c @key{DELETE}} (@code{c-hungry-delete-forward})
1549 @kindex C-c C-d
1550 @kindex C-c C-<DELETE>
1551 @kindex C-c <DELETE>
1552 @findex c-hungry-delete-forward
1553 @findex hungry-delete-forward (c-)
1554 Delete any amount of whitespace in the forward direction (regardless
1555 whether hungry-delete mode is enabled or not). This command is bound
1556 to both @kbd{C-c C-@key{DELETE}} and @kbd{C-c @key{DELETE}} for the
1557 same reason as for @key{DEL} above.
1558 @end table
1559 @end table
1560
1561 @kindex <delete>
1562 @kindex <backspace>
1563
1564 When we talk about @kbd{@key{DEL}}, and @kbd{@key{DELETE}} above, we
1565 actually do so without connecting them to the physical keys commonly
1566 known as @key{Backspace} and @key{Delete}. The default bindings to
1567 those two keys depends on the flavor of (X)Emacs you are using.
1568
1569 @findex c-electric-delete
1570 @findex electric-delete (c-)
1571 @findex c-hungry-delete
1572 @findex hungry-delete (c-)
1573 @vindex delete-key-deletes-forward
1574 In XEmacs 20.3 and beyond, the @key{Backspace} key is bound to
1575 @code{c-electric-backspace} and the @key{Delete} key is bound to
1576 @code{c-electric-delete}. You control the direction it deletes in by
1577 setting the variable @code{delete-key-deletes-forward}, a standard
1578 XEmacs variable.
1579 @c This variable is encapsulated by XEmacs's (defsubst delete-forward-p ...).
1580 When this variable is non-@code{nil}, @code{c-electric-delete} will do
1581 forward deletion with @code{c-electric-delete-forward}, otherwise it
1582 does backward deletion with @code{c-electric-backspace}. Similarly,
1583 @kbd{C-c @key{Delete}} and @kbd{C-c C-@key{Delete}} are bound to
1584 @code{c-hungry-delete} which is controlled in the same way by
1585 @code{delete-key-deletes-forward}.
1586
1587 @findex normal-erase-is-backspace-mode
1588
1589 Emacs 21 and later automatically binds @key{Backspace} and
1590 @key{Delete} to @kbd{DEL} and @kbd{C-d} according to your environment,
1591 and @ccmode{} extends those bindings to @kbd{C-c C-@key{Backspace}}
1592 etc. If you need to change the bindings through
1593 @code{normal-erase-is-backspace-mode} then @ccmode{} will also adapt
1594 its extended bindings accordingly.
1595
1596 In earlier (X)Emacs versions, @ccmode{} doesn't bind either
1597 @key{Backspace} or @key{Delete} directly. Only the key codes
1598 @kbd{DEL} and @kbd{C-d} are bound, and it's up to the default bindings
1599 to map the physical keys to them. You might need to modify this
1600 yourself if the defaults are unsuitable.
1601
1602 Getting your @key{Backspace} and @key{Delete} keys properly set up can
1603 sometimes be tricky. The information in @ref{DEL Does Not
1604 Delete,,,emacs, GNU Emacs Manual}, might be helpful if you're having
1605 trouble with this in GNU Emacs.
1606
1607
1608 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1609 @node Subword Movement, Other Commands, Hungry WS Deletion, Commands
1610 @comment node-name, next, previous, up
1611 @section Subword Movement and Editing
1612 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1613
1614 @cindex nomenclature
1615 @cindex subword
1616 In spite of the GNU Coding Standards, it is popular to name a symbol
1617 by mixing uppercase and lowercase letters, e.g. @samp{GtkWidget},
1618 @samp{EmacsFrameClass}, or @samp{NSGraphicsContext}. Here we call
1619 these mixed case symbols @dfn{nomenclatures}. Also, each capitalized
1620 (or completely uppercase) part of a nomenclature is called a
1621 @dfn{subword}. Here are some examples:
1622
1623 @multitable {@samp{NSGraphicsContext}} {@samp{NS}, @samp{Graphics}, and @samp{Context}}
1624 @c This could be converted to @headitem when we require Texinfo 4.7
1625 @iftex
1626 @item @b{Nomenclature}
1627 @tab @b{Subwords}
1628 @end iftex
1629 @ifnottex
1630 @item Nomenclature
1631 @tab Subwords
1632 @item ---------------------------------------------------------
1633 @end ifnottex
1634 @item @samp{GtkWindow}
1635 @tab @samp{Gtk} and @samp{Window}
1636 @item @samp{EmacsFrameClass}
1637 @tab @samp{Emacs}, @samp{Frame}, and @samp{Class}
1638 @item @samp{NSGraphicsContext}
1639 @tab @samp{NS}, @samp{Graphics}, and @samp{Context}
1640 @end multitable
1641
1642 The subword minor mode replaces the basic word oriented movement and
1643 editing commands with variants that recognize subwords in a
1644 nomenclature and treat them as separate words:
1645
1646 @findex c-forward-subword
1647 @findex forward-subword (c-)
1648 @findex c-backward-subword
1649 @findex backward-subword (c-)
1650 @findex c-mark-subword
1651 @findex mark-subword (c-)
1652 @findex c-kill-subword
1653 @findex kill-subword (c-)
1654 @findex c-backward-kill-subword
1655 @findex backward-kill-subword (c-)
1656 @findex c-transpose-subwords
1657 @findex transpose-subwords (c-)
1658 @findex c-capitalize-subword
1659 @findex capitalize-subword (c-)
1660 @findex c-upcase-subword
1661 @findex upcase-subword (c-)
1662 @findex c-downcase-subword
1663 @findex downcase-subword (c-)
1664 @multitable @columnfractions .20 .40 .40
1665 @c This could be converted to @headitem when we require Texinfo 4.7
1666 @iftex
1667 @item @b{Key} @tab @b{Word oriented command} @tab @b{Subword oriented command}
1668 @end iftex
1669 @ifnottex
1670 @item Key @tab Word oriented command @tab Subword oriented command
1671 @item ----------------------------------------------------------------------------
1672 @end ifnottex
1673 @item @kbd{M-f} @tab @code{forward-word} @tab @code{c-forward-subword}
1674 @item @kbd{M-b} @tab @code{backward-word} @tab @code{c-backward-subword}
1675 @item @kbd{M-@@} @tab @code{mark-word} @tab @code{c-mark-subword}
1676 @item @kbd{M-d} @tab @code{kill-word} @tab @code{c-kill-subword}
1677 @item @kbd{M-DEL} @tab @code{backward-kill-word} @tab @code{c-backward-kill-subword}
1678 @item @kbd{M-t} @tab @code{transpose-words} @tab @code{c-transpose-subwords}
1679 @item @kbd{M-c} @tab @code{capitalize-word} @tab @code{c-capitalize-subword}
1680 @item @kbd{M-u} @tab @code{upcase-word} @tab @code{c-upcase-subword}
1681 @item @kbd{M-l} @tab @code{downcase-word} @tab @code{c-downcase-subword}
1682 @end multitable
1683
1684 Note that if you have changed the key bindings for the word oriented
1685 commands in your @file{.emacs} or a similar place, the keys you have
1686 configured are also used for the corresponding subword oriented
1687 commands.
1688
1689 Type @kbd{C-c C-w} to toggle subword mode on and off. To make the
1690 mode turn on automatically, put the following code in your
1691 @file{.emacs}:
1692
1693 @example
1694 (add-hook 'c-mode-common-hook
1695 (lambda () (subword-mode 1)))
1696 @end example
1697
1698 As a bonus, you can also use @code{subword-mode} in non-@ccmode{}
1699 buffers by typing @kbd{M-x subword-mode}.
1700
1701 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1702 @node Other Commands, , Subword Movement, Commands
1703 @comment node-name, next, previous, up
1704 @section Other Commands
1705 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1706
1707 Here are the various other commands that didn't fit anywhere else:
1708
1709 @table @asis
1710 @item @kbd{C-c .} (@code{c-set-style})
1711 @kindex C-c .
1712 @findex c-set-style
1713 @findex set-style (c-)
1714 Switch to the specified style in the current buffer. Use like this:
1715
1716 @example
1717 @kbd{C-c . @var{style-name} @key{RET}}
1718 @end example
1719
1720 You can use the @key{TAB} in the normal way to do completion on the
1721 style name. Note that all style names are case insensitive, even the
1722 ones you define yourself.
1723
1724 Setting a style in this way does @emph{not} automatically reindent your
1725 file. For commands that you can use to view the effect of your changes,
1726 see @ref{Indentation Commands} and @ref{Filling and Breaking}.
1727
1728 For details of the @ccmode{} style system, see @ref{Styles}.
1729 @item @kbd{C-c :} (@code{c-scope-operator})
1730 @kindex C-c :
1731 @findex c-scope-operator
1732 @findex scope-operator (c-)
1733 In C++, it is also sometimes desirable to insert the double-colon scope
1734 operator without performing the electric behavior of colon insertion.
1735 @kbd{C-c :} does just this.
1736
1737 @item @kbd{C-c C-\} (@code{c-backslash-region})
1738 @kindex C-c C-\
1739 @findex c-backslash-region
1740 @findex backslash-region (c-)
1741 This function inserts and aligns or deletes end-of-line backslashes in
1742 the current region. These are typically used in multi-line macros.
1743
1744 With no prefix argument, it inserts any missing backslashes and aligns
1745 them according to the @code{c-backslash-column} and
1746 @code{c-backslash-max-column} variables. With a prefix argument, it
1747 deletes any backslashes.
1748
1749 The function does not modify blank lines at the start of the region. If
1750 the region ends at the start of a line, it always deletes the backslash
1751 (if any) at the end of the previous line.
1752
1753 To customize the precise workings of this command, @ref{Custom Macros}.
1754 @end table
1755
1756 @noindent
1757 The recommended line breaking function, @code{c-context-line-break}
1758 (@pxref{Filling and Breaking}), is especially nice if you edit
1759 multiline macros frequently. When used inside a macro, it
1760 automatically inserts and adjusts the mandatory backslash at the end
1761 of the line to keep the macro together, and it leaves the point at the
1762 right indentation column for the code. Thus you can write code inside
1763 macros almost exactly as you can elsewhere, without having to bother
1764 with the trailing backslashes.
1765
1766 @table @asis
1767 @item @kbd{C-c C-e} (@code{c-macro-expand})
1768 @kindex C-c C-e
1769 @findex c-macro-expand
1770 @findex macro-expand (c-)
1771 This command expands C, C++, Objective C or Pike macros in the region,
1772 using an appropriate external preprocessor program. Normally it
1773 displays its output in a temporary buffer, but if you give it a prefix
1774 arg (with @kbd{C-u C-c C-e}) it will overwrite the original region
1775 with the expansion.
1776
1777 The command does not work in any of the other modes, and the key
1778 sequence is not bound in these other modes.
1779
1780 @code{c-macro-expand} isn't actually part of @ccmode{}, even though it
1781 is bound to a @ccmode{} key sequence. If you need help setting it up
1782 or have other problems with it, you can either read its source code or
1783 ask for help in the standard (X)Emacs forums.
1784 @end table
1785
1786 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1787 @node Font Locking, Config Basics, Commands, Top
1788 @comment node-name, next, previous, up
1789 @chapter Font Locking
1790 @cindex font locking
1791 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1792
1793 @cindex Font Lock mode
1794
1795 @ccmode{} provides font locking for its supported languages by
1796 supplying patterns for use with Font Lock mode. This means that you
1797 get distinct faces on the various syntactic parts such as comments,
1798 strings, keywords and types, which is very helpful in telling them
1799 apart at a glance and discovering syntactic errors. @xref{Font
1800 Lock,,, emacs, GNU Emacs Manual}, for ways to enable font locking in
1801 @ccmode{} buffers.
1802
1803 @strong{Please note:} The font locking in AWK mode is currently not
1804 integrated with the rest of @ccmode{}. Only the last section of this
1805 chapter, @ref{AWK Mode Font Locking}, applies to AWK. The other
1806 sections apply to the other languages.
1807
1808 @menu
1809 * Font Locking Preliminaries::
1810 * Faces::
1811 * Doc Comments::
1812 * AWK Mode Font Locking::
1813 @end menu
1814
1815
1816 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1817 @node Font Locking Preliminaries, Faces, Font Locking, Font Locking
1818 @comment node-name, next, previous, up
1819 @section Font Locking Preliminaries
1820 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1821
1822 The font locking for most of the @ccmode{} languages were provided
1823 directly by the Font Lock package prior to version 5.30 of @ccmode{}.
1824 In the transition to @ccmode{} the patterns have been reworked
1825 completely and are applied uniformly across all the languages except AWK
1826 mode, just like the indentation rules (although each language still has
1827 some peculiarities of its own, of course). Since the languages
1828 previously had completely separate font locking patterns, this means
1829 that it's a bit different in most languages now.
1830
1831 The main goal for the font locking in @ccmode{} is accuracy, to provide
1832 a dependable aid in recognizing the various constructs. Some, like
1833 strings and comments, are easy to recognize while others, like
1834 declarations and types, can be very tricky. @ccmode{} can go to great
1835 lengths to recognize declarations and casts correctly, especially when
1836 the types aren't recognized by standard patterns. This is a fairly
1837 demanding analysis which can be slow on older hardware, and it can
1838 therefore be disabled by choosing a lower decoration level with the
1839 variable @code{font-lock-maximum-decoration} (@pxref{Font Lock,,,
1840 emacs, GNU Emacs Manual}).
1841
1842 @vindex font-lock-maximum-decoration
1843
1844 The decoration levels are used as follows:
1845
1846 @enumerate
1847 @comment 1
1848 @item
1849 Minimal font locking: Fontify only comments, strings and preprocessor
1850 directives (in the languages that use cpp).
1851
1852 @comment 2
1853 @item
1854 Fast font locking: In addition to level 1, fontify keywords, simple
1855 types and declarations that are easy to recognize. The variables
1856 @code{*-font-lock-extra-types} (where @samp{*} is the name of the
1857 language) are used to recognize types (see below). Documentation
1858 comments like Javadoc are fontified according to
1859 @code{c-doc-comment-style} (@pxref{Doc Comments}).
1860
1861 Use this if you think the font locking is too slow. It's the closest
1862 corresponding level to level 3 in the old font lock patterns.
1863
1864 @comment 3
1865 @item
1866 Accurate font locking: Like level 2 but uses a different approach that
1867 can recognize types and declarations much more accurately. The
1868 @code{*-font-lock-extra-types} variables are still used, but user
1869 defined types are recognized correctly anyway in most cases. Therefore
1870 those variables should be fairly restrictive and not contain patterns
1871 that are uncertain.
1872
1873 @cindex Lazy Lock mode
1874 @cindex Just-in-time Lock mode
1875
1876 This level is designed for fairly modern hardware and a font lock
1877 support mode like Lazy Lock or Just-in-time Lock mode that only
1878 fontifies the parts that are actually shown. Fontifying the whole
1879 buffer at once can easily get bothersomely slow even on contemporary
1880 hardware. @xref{Font Lock,,,@emacsman{}, @emacsmantitle{}}.
1881 @end enumerate
1882
1883 @cindex user defined types
1884 @cindex types, user defined
1885
1886 Since user defined types are hard to recognize you can provide
1887 additional regexps to match those you use:
1888
1889 @defopt c-font-lock-extra-types
1890 @defoptx c++-font-lock-extra-types
1891 @defoptx objc-font-lock-extra-types
1892 @defoptx java-font-lock-extra-types
1893 @defoptx idl-font-lock-extra-types
1894 @defoptx pike-font-lock-extra-types
1895 For each language there's a variable @code{*-font-lock-extra-types},
1896 where @samp{*} stands for the language in question. It contains a list
1897 of regexps that matches identifiers that should be recognized as types,
1898 e.g. @samp{\\sw+_t} to recognize all identifiers ending with @samp{_t}
1899 as is customary in C code. Each regexp should not match more than a
1900 single identifier.
1901
1902 The default values contain regexps for many types in standard runtime
1903 libraries that are otherwise difficult to recognize, and patterns for
1904 standard type naming conventions like the @samp{_t} suffix in C and C++.
1905 Java, Objective-C and Pike have as a convention to start class names
1906 with capitals, so there are patterns for that in those languages.
1907
1908 Despite the names of these variables, they are not only used for
1909 fontification but in other places as well where @ccmode{} needs to
1910 recognize types.
1911 @end defopt
1912
1913
1914 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1915 @node Faces, Doc Comments, Font Locking Preliminaries, Font Locking
1916 @comment node-name, next, previous, up
1917 @section Faces
1918 @cindex faces
1919 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1920
1921 @ccmode{} attempts to use the standard faces for programming languages
1922 in accordance with their intended purposes as far as possible. No extra
1923 faces are currently provided, with the exception of a replacement face
1924 @code{c-invalid-face} for emacsen that don't provide
1925 @code{font-lock-warning-face}.
1926
1927 @itemize @bullet
1928 @item
1929 @vindex font-lock-comment-face
1930 Normal comments are fontified in @code{font-lock-comment-face}.
1931
1932 @item
1933 @vindex font-lock-doc-face
1934 @vindex font-lock-doc-string-face
1935 @vindex font-lock-comment-face
1936 Comments that are recognized as documentation (@pxref{Doc Comments})
1937 get @code{font-lock-doc-face} (Emacs) or
1938 @code{font-lock-doc-string-face} (XEmacs) if those faces exist. If
1939 they don't then @code{font-lock-comment-face} is used.
1940
1941 @item
1942 @vindex font-lock-string-face
1943 String and character literals are fontified in
1944 @code{font-lock-string-face}.
1945
1946 @item
1947 @vindex font-lock-keyword-face
1948 Keywords are fontified with @code{font-lock-keyword-face}.
1949
1950 @item
1951 @vindex font-lock-function-name-face
1952 @code{font-lock-function-name-face} is used for function names in
1953 declarations and definitions, and classes in those contexts. It's also
1954 used for preprocessor defines with arguments.
1955
1956 @item
1957 @vindex font-lock-variable-name-face
1958 Variables in declarations and definitions, and other identifiers in such
1959 variable contexts, get @code{font-lock-variable-name-face}. It's also
1960 used for preprocessor defines without arguments.
1961
1962 @item
1963 @vindex font-lock-constant-face
1964 @vindex font-lock-reference-face
1965 Builtin constants are fontified in @code{font-lock-constant-face} if it
1966 exists, @code{font-lock-reference-face} otherwise. As opposed to the
1967 preceding two faces, this is used on the names in expressions, and it's
1968 not used in declarations, even if there happen to be a @samp{const} in
1969 them somewhere.
1970
1971 @item
1972 @vindex font-lock-type-face
1973 @code{font-lock-type-face} is put on types (both predefined and user
1974 defined) and classes in type contexts.
1975
1976 @item
1977 @vindex font-lock-constant-face
1978 @vindex font-lock-reference-face
1979 Label identifiers get @code{font-lock-constant-face} if it exists,
1980 @code{font-lock-reference-face} otherwise.
1981
1982 @item
1983 Name qualifiers and identifiers for scope constructs are fontified like
1984 labels.
1985
1986 @item
1987 Special markup inside documentation comments are also fontified like
1988 labels.
1989
1990 @item
1991 @vindex font-lock-preprocessor-face
1992 @vindex font-lock-builtin-face
1993 @vindex font-lock-reference-face
1994 Preprocessor directives get @code{font-lock-preprocessor-face} if it
1995 exists (i.e. XEmacs). In Emacs they get @code{font-lock-builtin-face}
1996 or @code{font-lock-reference-face}, for lack of a closer equivalent.
1997
1998 @item
1999 @vindex font-lock-warning-face
2000 @vindex c-invalid-face
2001 @vindex invalid-face (c-)
2002 Some kinds of syntactic errors are fontified with
2003 @code{font-lock-warning-face} in Emacs. In older XEmacs versions
2004 there's no corresponding standard face, so there a special
2005 @code{c-invalid-face} is used, which is defined to stand out sharply by
2006 default.
2007
2008 Note that it's not used for @samp{#error} or @samp{#warning} directives,
2009 since those aren't syntactic errors in themselves.
2010 @end itemize
2011
2012
2013 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2014 @node Doc Comments, AWK Mode Font Locking, Faces, Font Locking
2015 @comment node-name, next, previous, up
2016 @section Documentation Comments
2017 @cindex documentation comments
2018 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2019
2020 There are various tools to supply documentation in the source as
2021 specially structured comments, e.g. the standard Javadoc tool in Java.
2022 @ccmode{} provides an extensible mechanism to fontify such comments and
2023 the special markup inside them.
2024
2025 @defopt c-doc-comment-style
2026 @vindex doc-comment-style (c-)
2027 This is a style variable that specifies which documentation comment
2028 style to recognize, e.g. @code{javadoc} for Javadoc comments.
2029
2030 The value may also be a list of styles, in which case all of them are
2031 recognized simultaneously (presumably with markup cues that don't
2032 conflict).
2033
2034 The value may also be an association list to specify different comment
2035 styles for different languages. The symbol for the major mode is then
2036 looked up in the alist, and the value of that element is interpreted as
2037 above if found. If it isn't found then the symbol `other' is looked up
2038 and its value is used instead.
2039
2040 The default value for @code{c-doc-comment-style} is
2041 @w{@code{((java-mode . javadoc) (pike-mode . autodoc) (c-mode . gtkdoc))}}.
2042
2043 Note that @ccmode{} uses this variable to set other variables that
2044 handle fontification etc. That's done at mode initialization or when
2045 you switch to a style which sets this variable. Thus, if you change it
2046 in some other way, e.g. interactively in a CC Mode buffer, you will need
2047 to do @kbd{M-x java-mode} (or whatever mode you're currently using) to
2048 reinitialize.
2049
2050 @findex c-setup-doc-comment-style
2051 @findex setup-doc-comment-style (c-)
2052 Note also that when @ccmode{} starts up, the other variables are
2053 modified before the mode hooks are run. If you change this variable in
2054 a mode hook, you'll have to call @code{c-setup-doc-comment-style}
2055 afterwards to redo that work.
2056 @end defopt
2057
2058 @ccmode{} currently provides handing of the following doc comment
2059 styles:
2060
2061 @table @code
2062 @item javadoc
2063 @cindex Javadoc markup
2064 Javadoc comments, the standard tool in Java.
2065
2066 @item autodoc
2067 @cindex Pike autodoc markup
2068 For Pike autodoc markup, the standard in Pike.
2069
2070 @item gtkdoc
2071 @cindex GtkDoc markup
2072 For GtkDoc markup, widely used in the Gnome community.
2073 @end table
2074
2075 The above is by no means complete. If you'd like to see support for
2076 other doc comment styles, please let us know (@pxref{Mailing Lists and
2077 Bug Reports}).
2078
2079 You can also write your own doc comment fontification support to use
2080 with @code{c-doc-comment-style}: Supply a variable or function
2081 @code{*-font-lock-keywords} where @samp{*} is the name you want to use
2082 in @code{c-doc-comment-style}. If it's a variable, it's prepended to
2083 @code{font-lock-keywords}. If it's a function, it's called at mode
2084 initialization and the result is prepended. For an example, see
2085 @code{javadoc-font-lock-keywords} in @file{cc-fonts.el}.
2086
2087 If you add support for another doc comment style, please consider
2088 contributing it - send a note to @email{bug-cc-mode@@gnu.org}.
2089
2090
2091 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2092 @node AWK Mode Font Locking, , Doc Comments, Font Locking
2093 @comment node-name, next, previous, up
2094 @section AWK Mode Font Locking
2095 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2096
2097 The general appearance of font-locking in AWK mode is much like in any
2098 other programming mode. @xref{Faces For Font Lock,,,elisp, GNU Emacs
2099 Lisp Reference Manual}.
2100
2101 The following faces are, however, used in a non-standard fashion in
2102 AWK mode:
2103
2104 @table @asis
2105 @item @code{font-lock-variable-name-face}
2106 This face was intended for variable declarations. Since variables are
2107 not declared in AWK, this face is used instead for AWK system
2108 variables (such as @code{NF}) and ``Special File Names'' (such as
2109 @code{"/dev/stderr"}).
2110
2111 @item @code{font-lock-builtin-face} (Emacs)/@code{font-lock-preprocessor-face} (XEmacs)
2112 This face is normally used for preprocessor directives in @ccmode{}.
2113 There are no such things in AWK, so this face is used instead for
2114 standard functions (such as @code{match}).
2115
2116 @item @code{font-lock-string-face}
2117 As well as being used for strings, including localizable strings,
2118 (delimited by @samp{"} and @samp{_"}), this face is also used for AWK
2119 regular expressions (delimited by @samp{/}).
2120
2121 @item @code{font-lock-warning-face} (Emacs)/@code{c-invalid-face} (XEmacs)
2122 This face highlights the following syntactically invalid AWK
2123 constructs:
2124
2125 @itemize @bullet
2126 @item
2127 An unterminated string or regular expression. Here the opening
2128 delimiter (@samp{"} or @samp{/} or @samp{_"}) is displayed in
2129 @code{font-lock-warning-face}. This is most noticeable when typing in a
2130 new string/regular expression into a buffer, when the warning-face
2131 serves as a continual reminder to terminate the construct.
2132
2133 AWK mode fontifies unterminated strings/regular expressions
2134 differently from other modes: Only the text up to the end of the line
2135 is fontified as a string (escaped newlines being handled correctly),
2136 rather than the text up to the next string quote.
2137
2138 @item
2139 A space between the function name and opening parenthesis when calling
2140 a user function. The last character of the function name and the
2141 opening parenthesis are highlighted. This font-locking rule will
2142 spuriously highlight a valid concatenation expression where an
2143 identifier precedes a parenthesised expression. Unfortunately.
2144
2145 @item
2146 Whitespace following the @samp{\} in what otherwise looks like an
2147 escaped newline. The @samp{\} is highlighted.
2148 @end itemize
2149 @end table
2150
2151
2152 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2153 @node Config Basics, Custom Filling and Breaking, Font Locking, Top
2154 @comment node-name, next, previous, up
2155 @chapter Configuration Basics
2156 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2157
2158 @cindex Emacs Initialization File
2159 @cindex Configuration
2160 You configure @ccmode{} by setting Lisp variables and calling (and
2161 perhaps writing) Lisp functions@footnote{DON'T PANIC!!! This isn't
2162 difficult.}, which is usually done by adding code to an Emacs
2163 initialization file. This file might be @file{site-start.el} or
2164 @file{.emacs} or @file{init.el} or @file{default.el} or perhaps some
2165 other file. @xref{Init File,,,@emacsman{}, @emacsmantitle{}}. For
2166 the sake of conciseness, we just call this file ``your @file{.emacs}''
2167 throughout the rest of the manual.
2168
2169 Several of these variables (currently 16), are known collectively as
2170 @dfn{style variables}. @ccmode{} provides a special mechanism, known
2171 as @dfn{styles} to make it easier to set these variables as a group,
2172 to ``inherit'' settings from one style into another, and so on. Style
2173 variables remain ordinary Lisp variables, whose values can be read and
2174 changed independently of the style system. @xref{Style Variables}.
2175
2176 There are several ways you can write the code, depending on the
2177 precise effect you want---they are described further down on this page.
2178 If you are new to @ccmode{}, we suggest you begin with the simplest
2179 method, ``Top-level commands or the customization interface''.
2180
2181 If you make conflicting settings in several of these ways, the way
2182 that takes precedence is the one that appears latest in this list:
2183 @itemize @asis
2184 @item
2185 @table @asis
2186 @item Style
2187 @itemx File Style@footnote{In earlier versions of @ccmode{}, a File Style setting took precedence over any other setting apart from a File Local Variable setting.}
2188 @itemx Top-level command or ``customization interface''
2189 @itemx Hook
2190 @itemx File Local Variable setting
2191 @end table
2192 @end itemize
2193
2194 Here is a summary of the different ways of writing your configuration
2195 settings:
2196
2197 @table @asis
2198 @item Top-level commands or the ``customization interface''
2199 Most simply, you can write @code{setq} and similar commands at the top
2200 level of your @file{.emacs} file. When you load a @ccmode{} buffer,
2201 it initializes its configuration from these global values (at least,
2202 for those settings you have given values to), so it makes sense to
2203 have these @code{setq} commands run @emph{before} @ccmode{} is first
2204 initialized---in particular, before any call to @code{desktop-read}
2205 (@pxref{Saving Emacs Sessions,,, emacs, GNU Emacs Manual}). For
2206 example, you might set c-basic-offset thus:
2207
2208 @example
2209 (setq c-basic-offset 4)
2210 @end example
2211
2212 You can use the more user friendly Customization interface instead,
2213 but this manual does not cover in detail how that works. To do this,
2214 start by typing @kbd{M-x customize-group @key{RET} c @key{RET}}.
2215 @xref{Easy Customization,,,@emacsman{}, @emacsmantitle{}}.
2216 @c The following note really belongs in the Emacs manual.
2217 Emacs normally writes the customizations at the end of your
2218 @file{.emacs} file. If you use @code{desktop-read}, you should edit
2219 your @file{.emacs} to place the call to @code{desktop-read} @emph{after}
2220 the customizations.
2221
2222 The first initialization of @ccmode{} puts a snapshot of the
2223 configuration settings into the special style @code{user}.
2224 @xref{Built-in Styles}.
2225
2226 For basic use of Emacs, either of these ways of configuring is
2227 adequate. However, the settings are then the same in all @ccmode{}
2228 buffers and it can be clumsy to communicate them between programmers.
2229 For more flexibility, you'll want to use one (or both) of @ccmode{}'s
2230 more sophisticated facilities, hooks and styles.
2231
2232 @item Hooks
2233 An Emacs @dfn{hook} is a place to put Lisp functions that you want
2234 Emacs to execute later in specific circumstances.
2235 @xref{Hooks,,,@lispref{}, @lispreftitle{}}. @ccmode{} supplies a main
2236 hook and a language-specific hook for each language it supports - any
2237 functions you put onto these hooks get executed as the last part of a
2238 buffer's initialization. Typically you put most of your customization
2239 within the main hook, and use the language-specific hooks to vary the
2240 customization settings between language modes. For example, if you
2241 wanted different (non-standard) values of @code{c-basic-offset} in C
2242 Mode and Java Mode buffers, you could do it like this:
2243
2244 @example
2245 @group
2246 (defun my-c-mode-hook ()
2247 (setq c-basic-offset 3))
2248 (add-hook 'c-mode-hook 'my-c-mode-hook)
2249
2250 (defun my-java-mode-hook ()
2251 (setq c-basic-offset 6))
2252 (add-hook 'java-mode-hook 'my-java-mode-hook)
2253 @end group
2254 @end example
2255
2256 See @ref{CC Hooks} for more details on the use of @ccmode{} hooks.
2257
2258 @item Styles
2259 A @ccmode{} @dfn{style} is a coherent collection of customizations
2260 with a name. At any time, exactly one style is active in each
2261 @ccmode{} buffer, either the one you have selected or a default.
2262 @ccmode{} is delivered with several existing styles. Additionally,
2263 you can create your own styles, possibly based on these existing
2264 styles. If you worked in a programming team called the ``Free
2265 Group'', which had its own coding standards, you might well have this
2266 in your @file{.emacs} file:
2267
2268 @example
2269 (setq c-default-style '((java-mode . "java")
2270 (awk-mode . "awk")
2271 (other . "free-group-style")))
2272 @end example
2273
2274 See @ref{Styles} for fuller details on using @ccmode{} styles and how
2275 to create them.
2276
2277 @item File Local Variable setting
2278 A @dfn{file local variable setting} is a setting which applies to an
2279 individual source file. You put this in a @dfn{local variables list},
2280 a special block at the end of the source file (@pxref{Specifying File
2281 Variables,,, @emacsman{}}).
2282
2283 @item File Styles
2284 A @dfn{file style} is a rarely used variant of the ``style'' mechanism
2285 described above, which applies to an individual source file.
2286 @xref{File Styles}. You use this by setting certain special variables
2287 in a local variables list (@pxref{Specifying File Variables,,,
2288 @emacsman{}}).
2289
2290 @item Hooks with Styles
2291 For ultimate flexibility, you can use hooks and styles together. For
2292 example, if your team were developing a product which required a
2293 Linux driver, you'd probably want to use the ``linux'' style for the
2294 driver, and your own team's style for the rest of the code. You
2295 could achieve this with code like this in your @file{.emacs}:
2296
2297 @example
2298 @group
2299 (defun my-c-mode-hook ()
2300 (c-set-style
2301 (if (and (buffer-file-name)
2302 (string-match "/usr/src/linux" (buffer-file-name)))
2303 "linux"
2304 "free-group-style")))
2305 (add-hook 'c-mode-hook 'my-c-mode-hook)
2306 @end group
2307 @end example
2308
2309 In a programming team, a hook is a also a good place for each member
2310 to put his own personal preferences. For example, you might be the
2311 only person in your team who likes Auto-newline minor mode. You could
2312 have it enabled by default by placing the following in your
2313 @file{.emacs}:
2314
2315 @example
2316 @group
2317 (defun my-turn-on-auto-newline ()
2318 (c-toggle-auto-newline 1))
2319 (add-hook 'c-mode-common-hook 'my-turn-on-auto-newline)
2320 @end group
2321 @end example
2322 @end table
2323
2324 @menu
2325 * CC Hooks::
2326 * Style Variables::
2327 * Styles::
2328 @end menu
2329
2330 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2331 @node CC Hooks, Style Variables, Config Basics, Config Basics
2332 @comment node-name, next, previous, up
2333 @section Hooks
2334 @cindex mode hooks
2335 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2336 @c The node name is "CC Hooks" rather than "Hooks" because of a bug in
2337 @c some older versions of Info, e.g. the info.el in GNU Emacs 21.3.
2338 @c If you go to "Config Basics" and hit <CR> on the xref to "CC
2339 @c Hooks" the function Info-follow-reference searches for "*Note: CC
2340 @c Hooks" from the beginning of the page. If this node were instead
2341 @c named "Hooks", that search would spuriously find "*Note:
2342 @c Hooks(elisp)" and go to the wrong node.
2343
2344 @ccmode{} provides several hooks that you can use to customize the
2345 mode for your coding style. The main hook is
2346 @code{c-mode-common-hook}; typically, you'll put the bulk of your
2347 customizations here. In addition, each language mode has its own
2348 hook, allowing you to fine tune your settings individually for the
2349 different @ccmode{} languages, and there is a package initialization
2350 hook. Finally, there is @code{c-special-indent-hook}, which enables
2351 you to solve anomalous indentation problems. It is described in
2352 @ref{Other Indentation}, not here. All these hooks adhere to the
2353 standard Emacs conventions.
2354
2355 When you open a buffer, @ccmode{} first initializes it with the
2356 currently active style (@pxref{Styles}). Then it calls
2357 @code{c-mode-common-hook}, and finally it calls the language-specific
2358 hook. Thus, any style settings done in these hooks will override
2359 those set by @code{c-default-style}.
2360
2361 @defvar c-initialization-hook
2362 @vindex initialization-hook (c-)
2363 Hook run only once per Emacs session, when @ccmode{} is initialized.
2364 This is a good place to change key bindings (or add new ones) in any
2365 of the @ccmode{} key maps. @xref{Sample .emacs File}.
2366 @end defvar
2367
2368 @defvar c-mode-common-hook
2369 @vindex mode-common-hook (c-)
2370 Common hook across all languages. It's run immediately before the
2371 language specific hook.
2372 @end defvar
2373
2374 @defvar c-mode-hook
2375 @defvarx c++-mode-hook
2376 @defvarx objc-mode-hook
2377 @defvarx java-mode-hook
2378 @defvarx idl-mode-hook
2379 @defvarx pike-mode-hook
2380 @defvarx awk-mode-hook
2381 The language specific mode hooks. The appropriate one is run as the
2382 last thing when you enter that language mode.
2383 @end defvar
2384
2385 Although these hooks are variables defined in @ccmode{}, you can give
2386 them values before @ccmode{}'s code is loaded---indeed, this is the
2387 only way to use @code{c-initialization-hook}. Their values aren't
2388 overwritten when @ccmode{} gets loaded.
2389
2390 Here's a simplified example of what you can add to your @file{.emacs}
2391 file to do things whenever any @ccmode{} language is edited. See the
2392 Emacs manuals for more information on customizing Emacs via hooks.
2393 @xref{Sample .emacs File}, for a more complete sample @file{.emacs}
2394 file.
2395
2396 @example
2397 (defun my-c-mode-common-hook ()
2398 ;; my customizations for all of c-mode and related modes
2399 (no-case-fold-search)
2400 )
2401 (add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
2402 @end example
2403
2404 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2405 @node Style Variables, Styles, CC Hooks, Config Basics
2406 @comment node-name, next, previous, up
2407 @section Style Variables
2408 @cindex styles
2409 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2410
2411 @cindex style variables
2412 The variables that @ccmode{}'s style system control are called
2413 @dfn{style variables}. Note that style variables are ordinary Lisp
2414 variables, which the style system initializes; you can change their
2415 values at any time (e.g. in a hook function). The style system can
2416 also set other variables, to some extent. @xref{Styles}.
2417
2418 @dfn{Style variables} are handled specially in several ways:
2419
2420 @itemize @bullet
2421 @item
2422 Style variables are by default buffer-local variables. However, they
2423 can instead be made global by setting
2424 @code{c-style-variables-are-local-p} to @code{nil} before @ccmode{} is
2425 initialized.
2426
2427 @item
2428 @vindex c-old-style-variable-behavior
2429 @vindex old-style-variable-behavior (c-)
2430 The default global binding of any style variable (with two exceptions
2431 - see below) is the special symbol @code{set-from-style}. When the
2432 style system initializes a buffer-local copy of a style variable for a
2433 @ccmode{} buffer, if its global binding is still that symbol then it
2434 will be set from the current style. Otherwise it will retain its
2435 global default@footnote{This is a big change from versions of
2436 @ccmode{} earlier than 5.26, where such settings would get overridden
2437 by the style system unless special precautions were taken. That was
2438 changed since it was counterintuitive and confusing, especially to
2439 novice users. If your configuration depends on the old overriding
2440 behavior, you can set the variable
2441 @code{c-old-style-variable-behavior} to non-@code{nil}.}. This
2442 ``otherwise'' happens, for example, when you've set the variable with
2443 @code{setq} at the top level of your @file{.emacs} (@pxref{Config
2444 Basics}).
2445
2446 @item
2447 The style variable @code{c-offsets-alist} (@pxref{c-offsets-alist}) is
2448 an association list with an element for each syntactic symbol. It's
2449 handled a little differently from the other style variables. It's
2450 default global binding is the empty list @code{nil}, rather than
2451 @code{set-from-style}. Before the style system is initialized, you
2452 can add individual elements to @code{c-offsets-alist} by calling
2453 @code{c-set-offset}(@pxref{c-offsets-alist}) just like you would set
2454 other style variables with @code{setq}. Those elements will then
2455 prevail when the style system later initializes a buffer-local copy of
2456 @code{c-offsets-alist}.
2457
2458 @item
2459 The style variable @code{c-special-indent-hook} is also handled in a
2460 special way. Styles can only add functions to this hook, not remove
2461 them, so any global settings you put on it are always
2462 preserved@footnote{This did not change in version 5.26.}. The value
2463 you give this variable in a style definition can be either a function
2464 or a list of functions.
2465
2466 @item
2467 The global bindings of the style variables get captured in the special
2468 @code{user} style when the style system is first initialized.
2469 @xref{Built-in Styles}, for details.
2470 @end itemize
2471
2472 The style variables are:@*
2473 @code{c-indent-comment-alist},
2474 @code{c-indent-comments-syntactically-p} (@pxref{Indentation
2475 Commands});@*
2476 @code{c-doc-comment-style} (@pxref{Doc Comments});@*
2477 @code{c-block-comment-prefix}, @code{c-comment-prefix-regexp}
2478 (@pxref{Custom Filling and Breaking});@*
2479 @code{c-hanging-braces-alist} (@pxref{Hanging Braces});@*
2480 @code{c-hanging-colons-alist} (@pxref{Hanging Colons});@*
2481 @code{c-hanging-semi&comma-criteria} (@pxref{Hanging Semicolons and
2482 Commas});@*
2483 @code{c-cleanup-list} (@pxref{Clean-ups});@*
2484 @code{c-basic-offset} (@pxref{Customizing Indentation});@*
2485 @code{c-offsets-alist} (@pxref{c-offsets-alist});@*
2486 @code{c-comment-only-line-offset} (@pxref{Comment Line-Up});@*
2487 @code{c-special-indent-hook}, @code{c-label-minimum-indentation}
2488 (@pxref{Other Indentation});@*
2489 @code{c-backslash-column}, @code{c-backslash-max-column}
2490 (@pxref{Custom Macros}).
2491
2492 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2493 @node Styles, , Style Variables, Config Basics
2494 @comment node-name, next, previous, up
2495 @section Styles
2496 @cindex styles
2497 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2498
2499 By @dfn{style} we mean the layout of the code---things like how many
2500 columns to indent a block of code, whether an opening brace gets
2501 indented to the level of the code it encloses, or of the construct
2502 that introduces it, or ``hangs'' at the end of a line.
2503
2504 Most people only need to edit code formatted in just a few well-defined
2505 and consistent styles. For example, their organization might impose a
2506 ``blessed'' style that all its programmers must conform to. Similarly,
2507 people who work on GNU software will have to use the GNU coding style.
2508 Some shops are more lenient, allowing a variety of coding styles, and as
2509 programmers come and go, there could be a number of styles in use. For
2510 this reason, @ccmode{} makes it convenient for you to set up logical
2511 groupings of customizations called @dfn{styles}, associate a single name
2512 for any particular style, and pretty easily start editing new or
2513 existing code using these styles.
2514
2515 As an alternative to writing a style definition yourself, you can have
2516 @ccmode{} @dfn{guess} (at least part of) your style by looking at an
2517 already formatted piece of your code, @ref{Guessing the Style}.
2518
2519 @menu
2520 * Built-in Styles::
2521 * Choosing a Style::
2522 * Adding Styles::
2523 * Guessing the Style::
2524 * File Styles::
2525 @end menu
2526
2527 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2528 @node Built-in Styles, Choosing a Style, Styles, Styles
2529 @comment node-name, next, previous, up
2530 @subsection Built-in Styles
2531 @cindex styles, built-in
2532 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2533
2534 If you're lucky, one of @ccmode{}'s built-in styles might be just
2535 what you're looking for. These are:
2536
2537 @table @code
2538 @item gnu
2539 @cindex GNU style
2540 Coding style blessed by the Free Software Foundation
2541 for C code in GNU programs.
2542
2543 @item k&r
2544 @cindex K&R style
2545 The classic Kernighan and Ritchie style for C code.
2546
2547 @item bsd
2548 @cindex BSD style
2549 Also known as ``Allman style'' after Eric Allman.
2550
2551 @item whitesmith
2552 @cindex Whitesmith style
2553 Popularized by the examples that came with Whitesmiths C, an early
2554 commercial C compiler.
2555
2556 @item stroustrup
2557 @cindex Stroustrup style
2558 The classic Stroustrup style for C++ code.
2559
2560 @item ellemtel
2561 @cindex Ellemtel style
2562 Popular C++ coding standards as defined by ``Programming in C++, Rules
2563 and Recommendations,'' Erik Nyquist and Mats Henricson,
2564 Ellemtel@footnote{This document is available at
2565 @uref{http://www.doc.ic.ac.uk/lab/cplus/c++.rules/} among other
2566 places.}.
2567 @c N.B. This URL was still valid at 2005/8/28 (ACM).
2568
2569 @item linux
2570 @cindex Linux style
2571 C coding standard for Linux (the kernel).
2572
2573 @item python
2574 @cindex Python style
2575 C coding standard for Python extension modules@footnote{Python is a
2576 high level scripting language with a C/C++ foreign function interface.
2577 For more information, see @uref{http://www.python.org/}.}.
2578
2579 @item java
2580 @cindex Java style
2581 The style for editing Java code. Note that the default
2582 value for @code{c-default-style} installs this style when you enter
2583 @code{java-mode}.
2584
2585 @item awk
2586 @cindex AWK style
2587 The style for editing AWK code. Note that the default value for
2588 @code{c-default-style} installs this style when you enter
2589 @code{awk-mode}.
2590
2591 @item user
2592 @cindex User style
2593 This is a special style created by you. It consists of the factory
2594 defaults for all the style variables as modified by the customizations
2595 you do either with the Customization interface or by writing
2596 @code{setq}s and @code{c-set-offset}s at the top level of your
2597 @file{.emacs} file (@pxref{Config Basics}). The style system creates
2598 this style as part of its initialization and doesn't modify it
2599 afterwards.
2600 @end table
2601
2602
2603 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2604 @node Choosing a Style, Adding Styles, Built-in Styles, Styles
2605 @comment node-name, next, previous, up
2606 @subsection Choosing a Style
2607 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2608
2609 When you create a new buffer, its style will be set from
2610 @code{c-default-style}. The factory default is the style @code{gnu},
2611 except in Java and AWK modes where it's @code{java} and @code{awk}.
2612
2613 Remember that if you set a style variable with the Customization
2614 interface or at the top level of your @file{.emacs} file before the
2615 style system is initialized (@pxref{Config Basics}), this setting will
2616 override the one that the style system would have given the variable.
2617
2618 To set a buffer's style interactively, use the command @kbd{C-c .}
2619 (@pxref{Other Commands}). To set it from a file's local variable
2620 list, @ref{File Styles}.
2621
2622 @defopt c-default-style
2623 @vindex default-style (c-)
2624 This variable specifies which style to install by default in new
2625 buffers. It takes either a style name string, or an association list
2626 of major mode symbols to style names:
2627
2628 @enumerate
2629 @item
2630 When @code{c-default-style} is a string, it must be an existing style
2631 name. This style is then used for all modes.
2632
2633 @item
2634 When @code{c-default-style} is an association list, the mode language
2635 is looked up to find a style name string.
2636
2637 @item
2638 If @code{c-default-style} is an association list where the mode
2639 language mode isn't found then the special symbol @samp{other} is
2640 looked up. If it's found then the associated style is used.
2641
2642 @item
2643 If @samp{other} is not found then the @samp{gnu} style is used.
2644 @end enumerate
2645
2646 In all cases, the style described in @code{c-default-style} is installed
2647 @emph{before} the language hooks are run, so you can always override
2648 this setting by including an explicit call to @code{c-set-style} in your
2649 language mode hook, or in @code{c-mode-common-hook}.
2650
2651 The standard value of @code{c-default-style} is @w{@code{((java-mode
2652 . "java") (awk-mode . "awk") (other . "gnu"))}}.
2653 @end defopt
2654
2655 @defvar c-indentation-style
2656 @vindex indentation-style (c-)
2657 This variable always contains the buffer's current style name, as a
2658 string.
2659 @end defvar
2660
2661 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2662 @node Adding Styles, Guessing the Style, Choosing a Style, Styles
2663 @comment node-name, next, previous, up
2664 @subsection Adding and Amending Styles
2665 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2666
2667 If none of the built-in styles is appropriate, you'll probably want to
2668 create a new @dfn{style definition}, possibly based on an existing
2669 style. To do this, put the new style's settings into a list with the
2670 following format - the list can then be passed as an argument to the
2671 function @code{c-add-style}. You can see an example of a style
2672 definition in @ref{Sample .emacs File}.
2673
2674 @cindex style definition
2675 @c @defvr {List} style definition
2676 @table @asis
2677 @item Structure of a Style Definition List
2678 ([@var{base-style}] [(@var{variable} . @var{value}) @dots{}])
2679
2680 Optional @var{base-style}, if present, must be a string which is the
2681 name of the @dfn{base style} from which this style inherits. At most
2682 one @var{base-style} is allowed in a style definition. If
2683 @var{base-style} is not specified, the style inherits from the table
2684 of factory default values@footnote{This table is stored internally in
2685 the variable c-fallback-style.} instead. All styles eventually
2686 inherit from this internal table. Style loops generate errors. The
2687 list of pre-existing styles can be seen in @ref{Built-in Styles}.
2688
2689 The dotted pairs (@var{variable} . @var{value}) each consist of a
2690 variable and the value it is to be set to when the style is later
2691 activated.@footnote{Note that if the variable has been given a value
2692 by the Customization interface or a @code{setq} at the top level of
2693 your @file{.emacs}, this value will override the one the style system
2694 tries to give it. @xref{Config Basics}.} The variable can be either a
2695 @ccmode{} style variable or an arbitrary Emacs variable. In the
2696 latter case, it is @emph{not} made buffer-local by the @ccmode{} style
2697 system.
2698 @c @end defvr
2699
2700 Two variables are treated specially in the dotted pair list:
2701
2702 @table @code
2703 @item c-offsets-alist
2704 The value is in turn a list of dotted pairs of the form
2705
2706 @example
2707 (@r{@var{syntactic-symbol}} . @r{@var{offset}})
2708 @end example
2709
2710 as described in @ref{c-offsets-alist}. These are passed to
2711 @code{c-set-offset} so there is no need to set every syntactic symbol
2712 in your style, only those that are different from the inherited style.
2713
2714 @item c-special-indent-hook
2715 The value is added to @code{c-special-indent-hook} using
2716 @code{add-hook}, so any functions already on it are kept. If the value
2717 is a list, each element of the list is added with @code{add-hook}.
2718 @end table
2719 @end table
2720
2721 Styles are kept in the @code{c-style-alist} variable, but you
2722 should never modify this variable directly. Instead, @ccmode{}
2723 provides the function @code{c-add-style} for this purpose.
2724
2725 @defun c-add-style stylename description &optional set-p
2726 @findex add-style (c-)
2727 Add or update a style called @var{stylename}, a string.
2728 @var{description} is the new style definition in the form described
2729 above. If @var{stylename} already exists in @code{c-style-alist} then
2730 it is replaced by @var{description}. (Note, this replacement is
2731 total. The old style is @emph{not} merged into the new one.)
2732 Otherwise, a new style is added.
2733
2734 If the optional @var{set-p} is non-@code{nil} then the new style is
2735 applied to the current buffer as well. The use of this facility is
2736 deprecated and it might be removed from @ccmode{} in a future release.
2737 You should use @code{c-set-style} instead.
2738
2739 The sample @file{.emacs} file provides a concrete example of how a new
2740 style can be added and automatically set. @xref{Sample .emacs File}.
2741 @end defun
2742
2743 @defvar c-style-alist
2744 @vindex style-alist (c-)
2745 This is the variable that holds the definitions for the styles. It
2746 should not be changed directly; use @code{c-add-style} instead.
2747 @end defvar
2748
2749 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2750 @node Guessing the Style, File Styles, Adding Styles, Styles
2751 @comment node-name, next, previous, up
2752 @subsection Guessing the Style
2753 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2754
2755 Instead of specifying a style, you can get @ccmode{} to @dfn{guess}
2756 your style by examining an already formatted code buffer. @ccmode{}
2757 then determines the ''most frequent'' offset (@pxref{c-offsets-alist})
2758 for each of the syntactic symbols (@pxref{Indentation Engine Basics})
2759 encountered in the buffer, and the ''most frequent'' value of
2760 c-basic-offset (@pxref{Customizing Indentation}), then merges the
2761 current style with these ''guesses'' to form a new style. This
2762 combined style is known as the @dfn{guessed style}.
2763
2764 To do this, call @code{c-guess} (or one of the other 5 guessing
2765 commands) on your sample buffer. The analysis of your code may take
2766 some time.
2767
2768 You can then set the guessed style in any @ccmode{} buffer with
2769 @code{c-guess-install}. You can display the style with
2770 @code{c-guess-view}, and preserve it by copying it into your
2771 @file{.emacs} for future use, preferably after editing it.
2772
2773 @table @asis
2774 @item @kbd{M-x c-guess-no-install}
2775 @itemx @kbd{M-x c-guess-buffer-no-install}
2776 @itemx @kbd{M-x c-guess-region-no-install}
2777 @findex c-guess-no-install
2778 @findex c-guess-buffer-no-install
2779 @findex c-guess-region-no-install
2780 @findex guess-no-install (c-)
2781 @findex guess-buffer-no-install (c-)
2782 @findex guess-region-no-install (c-)
2783 These commands analyze a part of the current buffer and guess the
2784 style from it.
2785
2786 The part of the buffer examined is either the region
2787 (@code{c-guess-region-no-install}), the entire buffer
2788 (@code{c-guess-buffer-no-install}), or the first
2789 @code{c-guess-region-max} bytes (@code{c-guess-no-install}).
2790
2791 Each of these commands can be given an optional prefix argument. This
2792 instructs @ccmode{} to combine the new guesses with the current
2793 guesses before forming the guessed style.
2794 @end table
2795
2796 @table @asis
2797 @item @kbd{M-x c-guess}
2798 @itemx @kbd{M-x c-guess-buffer}
2799 @itemx @kbd{M-x c-guess-region}
2800 @findex c-guess
2801 @findex c-guess-buffer
2802 @findex c-guess-region
2803 @findex guess (c-)
2804 @findex guess-buffer (c-)
2805 @findex guess-region (c-)
2806 These commands analyze a part of the current buffer, guess the style
2807 from it, then install the guessed style on the buffer. The guessed
2808 style is given a name based on the buffer's absolute file name, and
2809 you can then set this style on any @ccmode{} buffer with @kbd{C-c .}.
2810
2811 The part of the buffer examined is either the region
2812 (@code{c-guess-region}), the entire buffer (@code{c-guess-buffer}), or
2813 the first @code{c-guess-region-max} bytes (@code{c-guess}).
2814
2815 Each of these commands can be given an optional prefix argument. This
2816 instructs @ccmode{} to combine the new guesses with the current
2817 guesses before forming the guessed style.
2818 @end table
2819
2820 @defopt c-guess-region-max
2821 @vindex guess-region-max (c-)
2822 This variable, default 50000, is the size in bytes of the buffer
2823 portion examined by c-guess and c-guess-no-install. If set to
2824 @code{nil}, the entire buffer is examined.
2825 @end defopt
2826
2827 @defopt c-guess-offset-threshold
2828 @vindex guess-offset-threshold (c-)
2829 This variable, default 10, is the maximum offset, either outwards or
2830 inwards, which will be taken into account by the analysis process.
2831 Any offset bigger than this will be ignored. For no limit, set this
2832 variable to a large number.
2833 @end defopt
2834
2835 @table @asis
2836 @item @kbd{M-x c-guess-install}
2837 @findex c-guess-install
2838 @findex guess-install (c-)
2839
2840 Set the current buffer's style to the guessed style. This prompts you
2841 to enter an optional new style name to give to the guessed style. By
2842 default, this name is based on the buffer's absolute file name. You
2843 can then use this style like any other.
2844
2845 @item @kbd{M-x c-guess-view}
2846 @findex c-guess-view
2847 @findex guess-view (c-)
2848 Display the most recently guessed style in a temporary buffer. This
2849 display is in the form of a @code{c-add-style} form (@pxref{Adding
2850 Styles}) which can be easily copied to your @file{.emacs}. You will
2851 probably want to edit it first.
2852
2853 The display of the guessed style contains these elements:
2854
2855 @table @asis
2856 @item Placeholder Name
2857 You should replace this with a style name of your own.
2858 @item Parent Style
2859 The style current when the guessing began, from which the guessed
2860 style inherits (@pxref{Config Basics}) the settings which weren't
2861 guessed.
2862 @item Guessed Offsets
2863 These are the core result of the guessing process. Each of them is
2864 marked by a comment.
2865 @item Inherited Offsets
2866 These are syntactic offsets which have been taken over from the parent
2867 style. To avoid possible future conflicts, you should remove either
2868 these offsets or the parent style name.
2869 @end table
2870 @end table
2871
2872 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2873 @node File Styles, , Guessing the Style, Styles
2874 @comment node-name, next, previous, up
2875 @subsection File Styles
2876 @cindex styles, file local
2877 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2878
2879 @cindex file local variables
2880
2881 The Emacs manual describes how you can customize certain variables on a
2882 per-file basis by including a @dfn{file local variable} block at the end
2883 of the file (@pxref{File Variables,, Local Variables in Files, @emacsman{},
2884 @emacsmantitle{}}).
2885
2886 So far, you've only seen a functional interface for setting styles in
2887 @ccmode{}, and this can't be used here. @ccmode{} fills the gap by
2888 providing two variables for use in a file's local variable list.
2889 Don't use them anywhere else! These allow you to customize the style
2890 on a per-file basis:
2891
2892 @defvar c-file-style
2893 @vindex file-style (c-)
2894 Set this variable to a style name string in the Local Variables list.
2895 From now on, when you visit the file, @ccmode{} will automatically set
2896 the file's style to this one using @code{c-set-style}.
2897 @end defvar
2898
2899 @defvar c-file-offsets
2900 @vindex file-offsets (c-)
2901 Set this variable (in the Local Variables list) to an association list
2902 of the same format as @code{c-offsets-alist}. From now on, when you
2903 visit the file, @ccmode{} will automatically institute these offsets
2904 using @code{c-set-offset}.
2905 @end defvar
2906
2907 Note that file style settings (i.e. @code{c-file-style}) are applied
2908 before file offset settings
2909 (i.e. @code{c-file-offsets})@footnote{Also, if either of these are set
2910 in a file's local variable section, all the style variable values are
2911 made local to that buffer, even if
2912 @code{c-style-variables-are-local-p} is @code{nil}. Since this
2913 variable is virtually always non-@code{nil} anyhow, you're unlikely to
2914 notice this effect.}.
2915
2916 If you set any variable by the file local variables mechanism, that
2917 setting takes priority over all other settings, even those in your
2918 mode hooks (@pxref{CC Hooks}). Any individual setting of a variable
2919 will override one made through @code{c-file-style} or
2920 @code{c-file-offsets}.
2921 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2922 @node Custom Filling and Breaking, Custom Auto-newlines, Config Basics, Top
2923 @comment node-name, next, previous, up
2924 @chapter Customizing Filling and Line Breaking
2925 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2926
2927 Since there's a lot of normal text in comments and string literals,
2928 @ccmode{} provides features to edit these like in text mode. It does
2929 this by hooking in on the different line breaking functions and tuning
2930 relevant variables as necessary.
2931
2932 @vindex c-comment-prefix-regexp
2933 @vindex comment-prefix-regexp (c-)
2934 @cindex comment line prefix
2935 @vindex comment-start
2936 @vindex comment-end
2937 @vindex comment-start-skip
2938 @vindex paragraph-start
2939 @vindex paragraph-separate
2940 @vindex paragraph-ignore-fill-prefix
2941 @vindex adaptive-fill-mode
2942 @vindex adaptive-fill-regexp
2943 @vindex adaptive-fill-first-line-regexp
2944 To make Emacs recognize comments and treat text in them as normal
2945 paragraphs, @ccmode{} makes several standard
2946 variables@footnote{@code{comment-start}, @code{comment-end},
2947 @code{comment-start-skip}, @code{paragraph-start},
2948 @code{paragraph-separate}, @code{paragraph-ignore-fill-prefix},
2949 @code{adaptive-fill-mode}, @code{adaptive-fill-regexp}, and
2950 @code{adaptive-fill-first-line-regexp}.} buffer-local and modifies them
2951 according to the language syntax and the comment line prefix.
2952
2953 @defopt c-comment-prefix-regexp
2954 @vindex comment-prefix-regexp (c-)
2955 This style variable contains the regexp used to recognize the
2956 @dfn{comment line prefix}, which is the line decoration that starts
2957 every line in a comment. The variable is either the comment line
2958 prefix itself, or (more usually) an association list with different
2959 values for different languages. The symbol for the major mode is
2960 looked up in the alist to get the regexp for the language, and if it
2961 isn't found then the special symbol @samp{other} is looked up instead.
2962
2963 When a comment line gets divided by @kbd{M-j} or the like, @ccmode{}
2964 inserts the comment line prefix from a neighboring line at the start
2965 of the new line. The default value of c-comment-prefix-regexp is
2966 @samp{//+\\|\\**}, which matches C++ style line comments like
2967
2968 @example
2969 // blah blah
2970 @end example
2971
2972 @noindent
2973 with two or more slashes in front of them, and the second and
2974 subsequent lines of C style block comments like
2975
2976 @example
2977 @group
2978 /*
2979 * blah blah
2980 */
2981 @end group
2982 @end example
2983
2984 @noindent
2985 with zero or more stars at the beginning of every line. If you change
2986 this variable, please make sure it still matches the comment starter
2987 (i.e. @code{//}) of line comments @emph{and} the line prefix inside
2988 block comments.
2989
2990 @findex c-setup-paragraph-variables
2991 @findex setup-paragraph-variables (c-)
2992 Also note that since @ccmode{} uses the value of
2993 @code{c-comment-prefix-regexp} to set up several other variables at
2994 mode initialization, there won't be any effect if you just change it
2995 inside a @ccmode{} buffer. You need to call the command
2996 @code{c-setup-paragraph-variables} too, to update those other
2997 variables. That's also the case if you modify
2998 @code{c-comment-prefix-regexp} in a mode hook, since @ccmode{} will
2999 already have set up these variables before calling the hook.
3000 @end defopt
3001
3002 In comments, @ccmode{} uses @code{c-comment-prefix-regexp} to adapt
3003 the line prefix from the other lines in the comment.
3004
3005 @vindex adaptive-fill-mode
3006 @cindex Adaptive Fill mode
3007 @ccmode{} uses adaptive fill mode (@pxref{Adaptive Fill,,, emacs, GNU
3008 Emacs Manual}) to make Emacs correctly keep the line prefix when
3009 filling paragraphs. That also makes Emacs preserve the text
3010 indentation @emph{inside} the comment line prefix. E.g. in the
3011 following comment, both paragraphs will be filled with the left
3012 margins of the texts kept intact:
3013
3014 @example
3015 @group
3016 /* Make a balanced b-tree of the nodes in the incoming
3017 * stream. But, to quote the famous words of Donald E.
3018 * Knuth,
3019 *
3020 * Beware of bugs in the above code; I have only
3021 * proved it correct, not tried it.
3022 */
3023 @end group
3024 @end example
3025
3026 @findex c-setup-filladapt
3027 @findex setup-filladapt (c-)
3028 @findex filladapt-mode
3029 @vindex filladapt-mode
3030 @cindex Filladapt mode
3031 It's also possible to use other adaptive filling packages, notably Kyle
3032 E. Jones' Filladapt package@footnote{It's available from
3033 @uref{http://www.wonderworks.com/}. As of version 2.12, it does however
3034 lack a feature that makes it work suboptimally when
3035 @code{c-comment-prefix-regexp} matches the empty string (which it does
3036 by default). A patch for that is available from
3037 @uref{http://cc-mode.sourceforge.net/,, the CC Mode web site}.},
3038 @c 2005/11/22: The above is still believed to be the case.
3039 which handles things like bulleted lists nicely. There's a convenience
3040 function @code{c-setup-filladapt} that tunes the relevant variables in
3041 Filladapt for use in @ccmode{}. Call it from a mode hook, e.g. with
3042 something like this in your @file{.emacs}:
3043
3044 @example
3045 (defun my-c-mode-common-hook ()
3046 (c-setup-filladapt)
3047 (filladapt-mode 1))
3048 (add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
3049 @end example
3050
3051 @defopt c-block-comment-prefix
3052 @vindex block-comment-prefix (c-)
3053 @vindex c-comment-continuation-stars
3054 @vindex comment-continuation-stars (c-)
3055 Normally the comment line prefix inserted for a new line inside a
3056 comment is deduced from other lines in it. However there's one
3057 situation when there's no hint about what the prefix should look like,
3058 namely when a block comment is broken for the first time. This style
3059 variable@footnote{In versions before 5.26, this variable was called
3060 @code{c-comment-continuation-stars}. As a compatibility measure,
3061 @ccmode{} still uses the value on that variable if it's set.} is used
3062 then as the comment prefix. It defaults to @samp{*
3063 }@footnote{Actually, this default setting of
3064 @code{c-block-comment-prefix} typically gets overridden by the default
3065 style @code{gnu}, which sets it to blank. You can see the line
3066 splitting effect described here by setting a different style,
3067 e.g. @code{k&r} @xref{Choosing a Style}.}, which makes a comment
3068
3069 @example
3070 /* Got O(n^2) here, which is a Bad Thing. */
3071 @end example
3072
3073 @noindent
3074 break into
3075
3076 @example
3077 @group
3078 /* Got O(n^2) here, which
3079 * is a Bad Thing. */
3080 @end group
3081 @end example
3082
3083 Note that it won't work to adjust the indentation by putting leading
3084 spaces in @code{c-block-comment-prefix}, since @ccmode{} still uses the
3085 normal indentation engine to indent the line. Thus, the right way to
3086 fix the indentation is by customizing the @code{c} syntactic symbol. It
3087 defaults to @code{c-lineup-C-comments}, which handles the indentation of
3088 most common comment styles, see @ref{Line-Up Functions}.
3089 @end defopt
3090
3091 @defopt c-ignore-auto-fill
3092 @vindex ignore-auto-fill (c-)
3093 When auto fill mode is enabled, @ccmode{} can selectively ignore it
3094 depending on the context the line break would occur in, e.g. to never
3095 break a line automatically inside a string literal. This variable
3096 takes a list of symbols for the different contexts where auto-filling
3097 never should occur:
3098
3099 @table @code
3100 @item string
3101 Inside a string or character literal.
3102 @item c
3103 Inside a C style block comment.
3104 @item c++
3105 Inside a C++ style line comment.
3106 @item cpp
3107 Inside a preprocessor directive.
3108 @item code
3109 Anywhere else, i.e. in normal code.
3110 @end table
3111
3112 By default, @code{c-ignore-auto-fill} is set to @code{(string cpp
3113 code)}, which means that when auto-fill mode is activated,
3114 auto-filling only occurs in comments. In literals, it's often
3115 desirable to have explicit control over newlines. In preprocessor
3116 directives, the necessary @samp{\} escape character before the newline
3117 is not automatically inserted, so an automatic line break would
3118 produce invalid code. In normal code, line breaks are normally
3119 dictated by some logical structure in the code rather than the last
3120 whitespace character, so automatic line breaks there will produce poor
3121 results in the current implementation.
3122 @end defopt
3123
3124 @vindex comment-multi-line
3125 If inside a comment and @code{comment-multi-line} (@pxref{Auto Fill,,,
3126 @emacsman{}, @emacsmantitle{}} is non-@code{nil}, the indentation and
3127 line prefix are preserved. If inside a comment and
3128 @code{comment-multi-line} is @code{nil}, a new comment of the same
3129 type is started on the next line and indented as appropriate for
3130 comments.
3131
3132 Note that @ccmode{} sets @code{comment-multi-line} to @code{t} at
3133 startup. The reason is that @kbd{M-j} could otherwise produce sequences
3134 of single line block comments for texts that should logically be treated
3135 as one comment, and the rest of the paragraph handling code
3136 (e.g. @kbd{M-q} and @kbd{M-a}) can't cope with that, which would lead to
3137 inconsistent behavior.
3138
3139 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3140 @node Custom Auto-newlines, Clean-ups, Custom Filling and Breaking, Top
3141 @comment node-name, next, previous, up
3142 @chapter Customizing Auto-newlines
3143 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3144
3145 @ccmode{} determines whether to insert auto-newlines in two basically
3146 different ways, depending on the character just typed:
3147
3148 @table @asis
3149 @item Braces and Colons
3150 @ccmode{} first determines the syntactic context of the brace or colon
3151 (@pxref{Syntactic Symbols}), then looks for a corresponding element in
3152 an alist. This element specifies where to put newlines - this is any
3153 combination of before and after the brace or colon. If no alist
3154 element is found, newlines are inserted both before and after a brace,
3155 but none are inserted around a colon. See @ref{Hanging Braces} and
3156 @ref{Hanging Colons}.
3157
3158 @item Semicolons and Commas
3159 The variable @code{c-hanging-semi&comma-criteria} contains a list of
3160 functions which determine whether to insert a newline after a newly
3161 typed semicolon or comma. @xref{Hanging Semicolons and Commas}.
3162 @end table
3163
3164 The names of these configuration variables contain @samp{hanging}
3165 because they let you @dfn{hang} the pertinent characters. A character
3166 which introduces a C construct is said to @dfn{hang on the right} when
3167 it appears at the end of a line after other code, being separated by a
3168 line break from the construct it introduces, like the opening brace in:
3169
3170 @example
3171 @group
3172 while (i < MAX) @{
3173 total += entry[i];
3174 entry [i++] = 0;
3175 @}
3176 @end group
3177 @end example
3178
3179 @noindent
3180 A character @dfn{hangs on the left} when it appears at the start of
3181 the line after the construct it closes off, like the above closing
3182 brace.
3183
3184 The next chapter, ``Clean-ups'', describes how to configure @ccmode{}
3185 to remove these automatically added newlines in certain specific
3186 circumstances. @xref{Clean-ups}.
3187
3188 @menu
3189 * Hanging Braces::
3190 * Hanging Colons::
3191 * Hanging Semicolons and Commas::
3192 @end menu
3193
3194
3195 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3196 @node Hanging Braces, Hanging Colons, Custom Auto-newlines, Custom Auto-newlines
3197 @comment node-name, next, previous, up
3198 @section Hanging Braces
3199 @cindex hanging braces
3200 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3201
3202 To specify which kinds of braces you want auto-newlines put around,
3203 you set the style variable @code{c-hanging-braces-alist}. Its
3204 structure and semantics are described in this section. Details of how
3205 to set it up, and its relationship to CC Mode's style system are given
3206 in @ref{Style Variables}.
3207
3208 Say you wanted an auto-newline after (but not before) the following
3209 @samp{@{}:
3210
3211 @example
3212 if (foo < 17) @{
3213 @end example
3214
3215 @noindent
3216 First you need to find the @dfn{syntactic context} of the brace---type
3217 a @key{RET} before the brace to get it on a line of its
3218 own@footnote{Also insert a @samp{\} at the end of the previous line if
3219 you're in AWK Mode.}, then type @kbd{C-c C-s}. That will tell you
3220 something like:
3221
3222 @example
3223 ((substatement-open 1061))
3224 @end example
3225
3226 @noindent
3227 So here you need to put the entry @code{(substatement-open . (after))}
3228 into @code{c-hanging-braces-alist}.
3229
3230 If you don't want any auto-newlines for a particular syntactic symbol,
3231 put this into @code{c-hanging-braces-alist}:
3232
3233 @example
3234 (brace-entry-open)
3235 @end example
3236
3237 If some brace syntactic symbol is not in @code{c-hanging-brace-alist},
3238 its entry is taken by default as @code{(before after)}---insert a
3239 newline both before and after the brace. In place of a
3240 ``before/after'' list you can specify a function in this alist---this
3241 is useful when the auto newlines depend on the code around the brace.
3242
3243 @defopt c-hanging-braces-alist
3244 @vindex hanging-braces-alist (c-)
3245
3246 This variable is an association list which maps syntactic symbols to
3247 lists of places to insert a newline. @xref{Association
3248 Lists,,,@lispref{}, @lispreftitle{}}. The key of each element is the
3249 syntactic symbol, the associated value is either @code{nil}, a list,
3250 or a function.
3251
3252 @table @asis
3253 @item The Key - the syntactic symbol
3254 The syntactic symbols that are useful as keys in this list are
3255 @code{brace-list-intro}, @code{statement-cont},
3256 @code{inexpr-class-open}, @code{inexpr-class-close}, and all the
3257 @code{*-open} and @code{*-close} symbols. @xref{Syntactic Symbols},
3258 for a more detailed description of these syntactic symbols, except for
3259 @code{inexpr-class-open} and @code{inexpr-class-close}, which aren't
3260 actual syntactic symbols. Elements with any other value as a key get
3261 ignored.
3262
3263 The braces of anonymous inner classes in Java are given the special
3264 symbols @code{inexpr-class-open} and @code{inexpr-class-close}, so that
3265 they can be distinguished from the braces of normal classes@footnote{The
3266 braces of anonymous classes produce a combination of
3267 @code{inexpr-class}, and @code{class-open} or @code{class-close} in
3268 normal indentation analysis.}.
3269
3270 Note that the aggregate constructs in Pike mode, @samp{(@{}, @samp{@})},
3271 @samp{([}, @samp{])}, and @samp{(<}, @samp{>)}, do not count as brace
3272 lists in this regard, even though they do for normal indentation
3273 purposes. It's currently not possible to set automatic newlines on
3274 these constructs.
3275
3276 @item The associated value - the ``ACTION'' list or function
3277 The value associated with each syntactic symbol in this association
3278 list is called an @var{action}, which can be either a list or a
3279 function which returns a list. @xref{Custom Braces}, for how to use
3280 a function as a brace hanging @var{action}.
3281
3282 The list @var{action} (or the list returned by @var{action} when it's
3283 a function) contains some combination of the symbols @code{before} and
3284 @code{after}, directing @ccmode{} where to put newlines in
3285 relationship to the brace being inserted. Thus, if the list contains
3286 only the symbol @code{after}, then the brace hangs on the right side
3287 of the line, as in:
3288
3289 @example
3290 // here, open braces always `hang'
3291 void spam( int i ) @{
3292 if( i == 7 ) @{
3293 dosomething(i);
3294 @}
3295 @}
3296 @end example
3297
3298 When the list contains both @code{after} and @code{before}, the braces
3299 will appear on a line by themselves, as shown by the close braces in
3300 the above example. The list can also be empty, in which case newlines
3301 are added neither before nor after the brace.
3302 @end table
3303
3304 If a syntactic symbol is missing entirely from
3305 @code{c-hanging-braces-alist}, it's treated in the same way as an
3306 @var{action} with a list containing @code{before} and @code{after}, so
3307 that braces by default end up on their own line.
3308
3309 For example, the default value of @code{c-hanging-braces-alist} is:
3310
3311 @example
3312 ((brace-list-open)
3313 (brace-entry-open)
3314 (statement-cont)
3315 (substatement-open after)
3316 (block-close . c-snug-do-while)
3317 (extern-lang-open after)
3318 (namespace-open after)
3319 (module-open after)
3320 (composition-open after)
3321 (inexpr-class-open after)
3322 (inexpr-class-close before))
3323 @end example
3324
3325 @noindent which says that @code{brace-list-open},
3326 @code{brace-entry-open} and @code{statement-cont}@footnote{Brace lists
3327 inside statements, such as initializers for static array variables
3328 inside functions in C, are recognized as @code{statement-cont}. All
3329 normal substatement blocks are recognized with other symbols.} braces
3330 should both hang on the right side and allow subsequent text to follow
3331 on the same line as the brace. Also, @code{substatement-open},
3332 @code{extern-lang-open}, and @code{inexpr-class-open} braces should hang
3333 on the right side, but subsequent text should follow on the next line.
3334 The opposite holds for @code{inexpr-class-close} braces; they won't
3335 hang, but the following text continues on the same line. Here, in the
3336 @code{block-close} entry, you also see an example of using a function as
3337 an @var{action}. In all other cases, braces are put on a line by
3338 themselves.
3339 @end defopt
3340
3341 @menu
3342 * Custom Braces::
3343 @end menu
3344
3345 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3346 @node Custom Braces, , Hanging Braces, Hanging Braces
3347 @comment node-name, next, previous, up
3348 @subsection Custom Brace Hanging
3349 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3350
3351 @vindex c-hanging-braces-alist
3352 @vindex hanging-braces-alist (c-)
3353 @cindex action functions
3354 Syntactic symbols aren't the only place where you can customize
3355 @ccmode{} with the lisp equivalent of callback functions. Remember
3356 that @var{action}s are usually a list containing some combination of
3357 the symbols @code{before} and @code{after} (@pxref{Hanging Braces}).
3358 For more flexibility, you can instead specify brace ``hanginess'' by
3359 giving a syntactic symbol an @dfn{action function} in
3360 @code{c-hanging-braces-alist}; this function determines the
3361 ``hanginess'' of a brace, usually by looking at the code near it.
3362
3363 @cindex customization, brace hanging
3364 An action function is called with two arguments: the syntactic symbol
3365 for the brace (e.g. @code{substatement-open}), and the buffer position
3366 where the brace has been inserted. Point is undefined on entry to an
3367 action function, but the function must preserve it (e.g. by using
3368 @code{save-excursion}). The return value should be a list containing
3369 some combination of @code{before} and @code{after}, including neither
3370 of them (i.e. @code{nil}).
3371
3372 @defvar c-syntactic-context
3373 @vindex syntactic-context (c-)
3374 During the call to the indentation or brace hanging @var{action}
3375 function, this variable is bound to the full syntactic analysis list.
3376 This might be, for example, @samp{((block-close 73))}. Don't ever
3377 give @code{c-syntactic-context} a value yourself---this would disrupt
3378 the proper functioning of @ccmode{}.
3379
3380 This variable is also bound in three other circumstances:
3381 (i)@w{ }when calling a c-hanging-semi&comma-criteria function
3382 (@pxref{Hanging Semicolons and Commas}); (ii)@w{ }when calling a
3383 line-up function (@pxref{Custom Line-Up}); (iii)@w{ }when calling a
3384 c-special-indent-hook function (@pxref{Other Indentation}).
3385 @end defvar
3386
3387 As an example, @ccmode{} itself uses this feature to dynamically
3388 determine the hanginess of braces which close ``do-while''
3389 constructs:
3390
3391 @example
3392 void do_list( int count, char** atleast_one_string )
3393 @{
3394 int i=0;
3395 do @{
3396 handle_string( atleast_one_string[i] );
3397 i++;
3398 @} while( i < count );
3399 @}
3400 @end example
3401
3402 @ccmode{} assigns the @code{block-close} syntactic symbol to the
3403 brace that closes the @code{do} construct, and normally we'd like the
3404 line that follows a @code{block-close} brace to begin on a separate
3405 line. However, with ``do-while'' constructs, we want the
3406 @code{while} clause to follow the closing brace. To do this, we
3407 associate the @code{block-close} symbol with the @var{action} function
3408 @code{c-snug-do-while}:
3409
3410 @example
3411 (defun c-snug-do-while (syntax pos)
3412 "Dynamically calculate brace hanginess for do-while statements."
3413 (save-excursion
3414 (let (langelem)
3415 (if (and (eq syntax 'block-close)
3416 (setq langelem (assq 'block-close c-syntactic-context))
3417 (progn (goto-char (cdr langelem))
3418 (if (= (following-char) ?@{)
3419 (forward-sexp -1))
3420 (looking-at "\\<do\\>[^_]")))
3421 '(before)
3422 '(before after)))))
3423 @end example
3424
3425 @findex c-snug-do-while
3426 @findex snug-do-while (c-)
3427 This function simply looks to see if the brace closes a ``do-while''
3428 clause and if so, returns the list @samp{(before)} indicating
3429 that a newline should be inserted before the brace, but not after it.
3430 In all other cases, it returns the list @samp{(before after)} so
3431 that the brace appears on a line by itself.
3432
3433 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3434 @node Hanging Colons, Hanging Semicolons and Commas, Hanging Braces, Custom Auto-newlines
3435 @comment node-name, next, previous, up
3436 @section Hanging Colons
3437 @cindex hanging colons
3438 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3439
3440 @cindex customization, colon hanging
3441 @vindex c-hanging-colons-alist
3442 @vindex hanging-colons-alist (c-)
3443
3444 Using a mechanism similar to brace hanging (@pxref{Hanging Braces}),
3445 colons can also be made to hang using the style variable
3446 @code{c-hanging-colons-alist} - When a colon is typed, @ccmode
3447 determines its syntactic context, looks this up in the alist
3448 @code{c-changing-colons-alist} and inserts up to two newlines
3449 accordingly. Here, however, If @ccmode fails to find an entry for a
3450 syntactic symbol in the alist, no newlines are inserted around the
3451 newly typed colon.
3452
3453 @defopt c-hanging-colons-alist
3454 @vindex hanging-colons-alist (c-)
3455
3456 @table @asis
3457 @item The Key - the syntactic symbol
3458 The syntactic symbols appropriate as keys in this association list
3459 are: @code{case-label}, @code{label}, @code{access-label},
3460 @code{member-init-intro}, and @code{inher-intro}. @xref{Syntactic
3461 Symbols}. Elements with any other value as a key get ignored.
3462
3463 @item The associate value - the ``ACTION'' list
3464 The @var{action} here is simply a list containing a combination of the
3465 symbols @code{before} and @code{after}. Unlike in
3466 @code{c-hanging-braces-alist}, functions as @var{actions} are not
3467 supported - there doesn't seem to be any need for them.
3468 @end table
3469 @end defopt
3470
3471 In C++, double-colons are used as a scope operator but because these
3472 colons always appear right next to each other, newlines before and after
3473 them are controlled by a different mechanism, called @dfn{clean-ups} in
3474 @ccmode{}. @xref{Clean-ups}, for details.
3475
3476 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3477 @node Hanging Semicolons and Commas, , Hanging Colons, Custom Auto-newlines
3478 @comment node-name, next, previous, up
3479 @section Hanging Semicolons and Commas
3480 @cindex hanging semicolons
3481 @cindex hanging commas
3482 @cindex customization, semicolon newlines
3483 @cindex customization, comma newlines
3484 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3485
3486 @defopt c-hanging-semi&comma-criteria
3487 @vindex hanging-semi&comma-criteria (c-)
3488 This style variable takes a list of functions; these get called when
3489 you type a semicolon or comma. The functions are called in order
3490 without arguments. When these functions are entered, point is just
3491 after the newly inserted @samp{;} or @samp{,} and they must preserve
3492 point (e.g., by using @code{save-excursion}). During the call, the
3493 variable @code{c-syntactic-context} is bound to the syntactic context
3494 of the current line@footnote{This was first introduced in @ccmode{}
3495 5.31.} @pxref{Custom Braces}. These functions don't insert newlines
3496 themselves, rather they direct @ccmode{} whether or not to do so.
3497 They should return one of the following values:
3498
3499 @table @code
3500 @item t
3501 A newline is to be inserted after the @samp{;} or @samp{,}, and no
3502 more functions from the list are to be called.
3503 @item stop
3504 No more functions from the list are to be called, and no newline is to
3505 be inserted.
3506 @item nil
3507 No determination has been made, and the next function in the list is
3508 to be called.
3509 @end table
3510
3511 Note that auto-newlines are never inserted @emph{before} a semicolon
3512 or comma. If every function in the list is called without a
3513 determination being made, then no newline is added.
3514
3515 In AWK mode, this variable is set by default to @code{nil}. In the
3516 other modes, the default value is a list containing a single function,
3517 @code{c-semi&comma-inside-parenlist}. This inserts newlines after all
3518 semicolons, apart from those separating @code{for}-clause statements.
3519 @end defopt
3520
3521 @defun c-semi&comma-no-newlines-before-nonblanks
3522 @findex semi&comma-no-newlines-before-nonblanks (c-)
3523 This is an example of a criteria function, provided by @ccmode{}. It
3524 prevents newlines from being inserted after semicolons when there is a
3525 non-blank following line. Otherwise, it makes no determination. To
3526 use, add this function to the front of the
3527 @code{c-hanging-semi&comma-criteria} list.
3528
3529 @example
3530 (defun c-semi&comma-no-newlines-before-nonblanks ()
3531 (save-excursion
3532 (if (and (eq last-command-char ?\;)
3533 (zerop (forward-line 1))
3534 (not (looking-at "^[ \t]*$")))
3535 'stop
3536 nil)))
3537 @end example
3538 @end defun
3539
3540 @defun c-semi&comma-inside-parenlist
3541 @findex semi&comma-inside-parenlist (c-)
3542 @defunx c-semi&comma-no-newlines-for-oneline-inliners
3543 @findex semi&comma-no-newlines-for-oneline-inliners (c-)
3544 The function @code{c-semi&comma-inside-parenlist} is what prevents
3545 newlines from being inserted inside the parenthesis list of @code{for}
3546 statements. In addition to
3547 @code{c-semi&comma-no-newlines-before-nonblanks} described above,
3548 @ccmode{} also comes with the criteria function
3549 @code{c-semi&comma-no-newlines-for-oneline-inliners}, which suppresses
3550 newlines after semicolons inside one-line inline method definitions
3551 (e.g. in C++ or Java).
3552 @end defun
3553
3554
3555 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3556 @node Clean-ups, Indentation Engine Basics, Custom Auto-newlines, Top
3557 @comment node-name, next, previous, up
3558 @chapter Clean-ups
3559 @cindex clean-ups
3560 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3561
3562 @dfn{Clean-ups} are mechanisms which remove (or exceptionally, add)
3563 whitespace in specific circumstances and are complementary to colon
3564 and brace hanging. You enable a clean-up by adding its symbol into
3565 @code{c-cleanup-list}, e.g. like this:
3566
3567 @example
3568 (add-to-list 'c-cleanup-list 'space-before-funcall)
3569 @end example
3570
3571 On the surface, it would seem that clean-ups overlap the functionality
3572 provided by the @code{c-hanging-*-alist} variables. Clean-ups,
3573 however, are used to adjust code ``after-the-fact'', i.e. to adjust
3574 the whitespace in constructs later than when they were typed.
3575
3576 Most of the clean-ups remove automatically inserted newlines, and are
3577 only active when auto-newline minor mode is turned on. Others will
3578 work all the time. Note that clean-ups are only performed when there
3579 is nothing but whitespace appearing between the individual components
3580 of the construct, and (apart from @code{comment-close-slash}) when the
3581 construct does not occur within a literal (@pxref{Auto-newlines}).
3582
3583 @defopt c-cleanup-list
3584 @vindex cleanup-list (c-)
3585 @cindex literal
3586
3587 You configure @ccmode{}'s clean-ups by setting the style variable
3588 @code{c-cleanup-list}, which is a list of clean-up symbols. By
3589 default, @ccmode{} cleans up only the @code{scope-operator} construct,
3590 which is necessary for proper C++ support.
3591 @end defopt
3592
3593 These are the clean-ups that are only active when electric and
3594 auto-newline minor modes are enabled:
3595
3596 @c TBD: Would like to use some sort of @deffoo here; @table indents a
3597 @c bit too much in dvi output.
3598 @table @code
3599 @item brace-else-brace
3600 Clean up @samp{@} else @{} constructs by placing the entire construct on
3601 a single line. Clean up occurs when the open brace after the
3602 @samp{else} is typed. So for example, this:
3603
3604 @example
3605 @group
3606 void spam(int i)
3607 @{
3608 if( i==7 ) @{
3609 dosomething();
3610 @}
3611 else
3612 @{
3613 @end group
3614 @end example
3615
3616 @noindent
3617 appears like this after the last open brace is typed:
3618
3619 @example
3620 @group
3621 void spam(int i)
3622 @{
3623 if( i==7 ) @{
3624 dosomething();
3625 @} else @{
3626 @end group
3627 @end example
3628
3629 @item brace-elseif-brace
3630 Similar to the @code{brace-else-brace} clean-up, but this cleans up
3631 @samp{@} else if (...) @{} constructs. For example:
3632
3633 @example
3634 @group
3635 void spam(int i)
3636 @{
3637 if( i==7 ) @{
3638 dosomething();
3639 @}
3640 else if( i==3 )
3641 @{
3642 @end group
3643 @end example
3644
3645 @noindent
3646 appears like this after the last open parenthesis is typed:
3647
3648 @example
3649 @group
3650 void spam(int i)
3651 @{
3652 if( i==7 ) @{
3653 dosomething();
3654 @} else if(
3655 @end group
3656 @end example
3657
3658 @noindent
3659 and like this after the last open brace is typed:
3660
3661 @example
3662 @group
3663 void spam(int i)
3664 @{
3665 if( i==7 ) @{
3666 dosomething();
3667 @} else if( i==3 ) @{
3668 @end group
3669 @end example
3670
3671 @item brace-catch-brace
3672 Analogous to @code{brace-elseif-brace}, but cleans up @samp{@} catch
3673 (...) @{} in C++ and Java mode.
3674
3675 @item empty-defun-braces
3676 Clean up braces following a top-level function or class definition that
3677 contains no body. Clean up occurs when the closing brace is typed.
3678 Thus the following:
3679
3680 @example
3681 @group
3682 class Spam
3683 @{
3684 @}
3685 @end group
3686 @end example
3687
3688 @noindent
3689 is transformed into this when the close brace is typed:
3690
3691 @example
3692 @group
3693 class Spam
3694 @{@}
3695 @end group
3696 @end example
3697
3698 @item defun-close-semi
3699 Clean up the terminating semicolon on top-level function or class
3700 definitions when they follow a close brace. Clean up occurs when the
3701 semicolon is typed. So for example, the following:
3702
3703 @example
3704 @group
3705 class Spam
3706 @{
3707 ...
3708 @}
3709 ;
3710 @end group
3711 @end example
3712
3713 @noindent
3714 is transformed into this when the semicolon is typed:
3715
3716 @example
3717 @group
3718 class Spam
3719 @{
3720 ...
3721 @};
3722 @end group
3723 @end example
3724
3725 @item list-close-comma
3726 Clean up commas following braces in array and aggregate initializers.
3727 Clean up occurs when the comma is typed. The space before the comma
3728 is zapped just like the space before the semicolon in
3729 @code{defun-close-semi}.
3730
3731 @item scope-operator
3732 Clean up double colons which might designate a C++ scope operator split
3733 across multiple lines@footnote{Certain C++ constructs introduce
3734 ambiguous situations, so @code{scope-operator} clean-ups might not
3735 always be correct. This usually only occurs when scoped identifiers
3736 appear in switch label tags.}. Clean up occurs when the second colon is
3737 typed. You will always want @code{scope-operator} in the
3738 @code{c-cleanup-list} when you are editing C++ code.
3739
3740 @item one-liner-defun
3741 Clean up a single line of code enclosed by defun braces by removing
3742 the whitespace before and after the code. The clean-up happens when
3743 the closing brace is typed. If the variable
3744 @code{c-max-one-liner-length} is set, the cleanup is only done if the
3745 resulting line would be no longer than the value of that variable.
3746
3747 For example, consider this AWK code:
3748
3749 @example
3750 @group
3751 BEGIN @{
3752 FS = "\t" # use <TAB> as a field separator
3753 @}
3754 @end group
3755 @end example
3756
3757 @noindent
3758 It gets compacted to the following when the closing brace is typed:
3759
3760 @example
3761 @group
3762 BEGIN @{FS = "\t"@} # use <TAB> as a field separator
3763 @end group
3764 @end example
3765
3766 @defopt c-max-one-liner-length
3767 @vindex max-one-liner-length (c-)
3768 The maximum length of the resulting line for which the clean-up
3769 @code{one-liner-defun} will be triggered. This length is that of the entire
3770 line, including any leading whitespace and any trailing comment. Its
3771 default value is 80. If the value is zero or @code{nil}, no limit
3772 applies.
3773 @end defopt
3774 @end table
3775
3776 The following clean-ups are always active when they occur on
3777 @code{c-cleanup-list}, regardless of whether Electric minor mode or
3778 Auto-newline minor mode are enabled:
3779
3780 @table @code
3781 @item space-before-funcall
3782 Insert a space between the function name and the opening parenthesis
3783 of a function call. This produces function calls in the style
3784 mandated by the GNU coding standards, e.g. @samp{signal@w{ }(SIGINT,
3785 SIG_IGN)} and @samp{abort@w{ }()}. Clean up occurs when the opening
3786 parenthesis is typed. This clean-up should never be active in AWK
3787 Mode, since such a space is syntactically invalid for user defined
3788 functions.
3789
3790 @item compact-empty-funcall
3791 Clean up any space between the function name and the opening parenthesis
3792 of a function call that has no arguments. This is typically used
3793 together with @code{space-before-funcall} if you prefer the GNU function
3794 call style for functions with arguments but think it looks ugly when
3795 it's only an empty parenthesis pair. I.e. you will get @samp{signal
3796 (SIGINT, SIG_IGN)}, but @samp{abort()}. Clean up occurs when the
3797 closing parenthesis is typed.
3798
3799 @item comment-close-slash
3800 When inside a block comment, terminate the comment when you type a slash
3801 at the beginning of a line (i.e. immediately after the comment prefix).
3802 This clean-up removes whitespace preceding the slash and if needed,
3803 inserts a star to complete the token @samp{*/}. Type @kbd{C-q /} in this
3804 situation if you just want a literal @samp{/} inserted.
3805 @end table
3806
3807
3808 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3809 @node Indentation Engine Basics, Customizing Indentation, Clean-ups, Top
3810 @comment node-name, next, previous, up
3811 @chapter Indentation Engine Basics
3812 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3813
3814 This chapter will briefly cover how @ccmode{} indents lines of code.
3815 It is helpful to understand the indentation model being used so that
3816 you will know how to customize @ccmode{} for your personal coding
3817 style. All the details are in @ref{Customizing Indentation}.
3818
3819 @ccmode{} has an indentation engine that provides a flexible and
3820 general mechanism for customizing indentation. When @ccmode{} indents
3821 a line of code, it separates its calculations into two steps:
3822
3823 @enumerate
3824 @item
3825 @cindex syntactic symbol
3826 @cindex anchor position
3827 It analyzes the line to determine its @dfn{syntactic symbol(s)} (the
3828 kind of language construct it's looking at) and its @dfn{anchor
3829 position} (the position earlier in the file that @ccmode{} will indent
3830 the line relative to). The anchor position might be the location of
3831 an opening brace in the previous line, for example. @xref{Syntactic
3832 Analysis}.
3833 @item
3834 @cindex offsets
3835 @cindex indentation offset specifications
3836 It looks up the syntactic symbol(s) in the configuration to get the
3837 corresponding @dfn{offset(s)}. The symbol @code{+}, which means
3838 ``indent this line one more level'' is a typical offset. @ccmode{}
3839 then applies these offset(s) to the anchor position, giving the
3840 indentation for the line. The different sorts of offsets are
3841 described in @ref{c-offsets-alist}.
3842 @end enumerate
3843
3844 In exceptional circumstances, the syntax directed indentation
3845 described here may be a nuisance rather than a help. You can disable
3846 it by setting @code{c-syntactic-indentation} to @code{nil}. (To set
3847 the variable interactively, @ref{Minor Modes}).
3848
3849 @defopt c-syntactic-indentation
3850 @vindex syntactic-indentation (c-)
3851 When this is non-@code{nil} (which it is by default), the indentation
3852 of code is done according to its syntactic structure. When it's
3853 @code{nil}, every line is just indented to the same level as the
3854 previous one, and @kbd{TAB} (@code{c-indent-command}) adjusts the
3855 indentation in steps of @code{c-basic-offset}. The current style
3856 (@pxref{Config Basics}) then has no effect on indentation, nor do any
3857 of the variables associated with indentation, not even
3858 @code{c-special-indent-hook}.
3859 @end defopt
3860
3861 @menu
3862 * Syntactic Analysis::
3863 * Syntactic Symbols::
3864 * Indentation Calculation::
3865 @end menu
3866
3867
3868 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3869 @node Syntactic Analysis, Syntactic Symbols, Indentation Engine Basics, Indentation Engine Basics
3870 @comment node-name, next, previous, up
3871 @section Syntactic Analysis
3872 @cindex syntactic analysis
3873 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3874
3875 @cindex syntactic element
3876 @cindex syntactic context
3877 The first thing @ccmode{} does when indenting a line of code, is to
3878 analyze the line, determining the @dfn{syntactic context} of the
3879 (first) construct on that line. It's a list of @dfn{syntactic
3880 elements}, where each syntactic element in turn is a list@footnote{In
3881 @ccmode 5.28 and earlier, a syntactic element was a dotted pair; the
3882 cons was the syntactic symbol and the cdr was the anchor position.
3883 For compatibility's sake, the parameter passed to a line-up function
3884 still has this dotted pair form (@pxref{Custom Line-Up}).} Here is a
3885 brief and typical example:
3886
3887 @example
3888 ((defun-block-intro 1959))
3889 @end example
3890
3891 @cindex syntactic symbol
3892 @noindent
3893 The first thing inside each syntactic element is always a
3894 @dfn{syntactic symbol}. It describes the kind of construct that was
3895 recognized, e.g. @code{statement}, @code{substatement},
3896 @code{class-open}, @code{class-close}, etc. @xref{Syntactic Symbols},
3897 for a complete list of currently recognized syntactic symbols and
3898 their semantics. The remaining entries are various data associated
3899 with the recognized construct - there might be zero or more.
3900
3901 @cindex anchor position
3902 Conceptually, a line of code is always indented relative to some
3903 position higher up in the buffer (typically the indentation of the
3904 previous line). That position is the @dfn{anchor position} in the
3905 syntactic element. If there is an entry after the syntactic symbol in
3906 the syntactic element list then it's either nil or that anchor position.
3907
3908 Here is an example. Suppose we had the following code as the only thing
3909 in a C++ buffer @footnote{The line numbers in this and future examples
3910 don't actually appear in the buffer, of course!}:
3911
3912 @example
3913 1: void swap( int& a, int& b )
3914 2: @{
3915 3: int tmp = a;
3916 4: a = b;
3917 5: b = tmp;
3918 6: @}
3919 @end example
3920
3921 @noindent
3922 We can use @kbd{C-c C-s} (@code{c-show-syntactic-information}) to
3923 report what the syntactic analysis is for the current line:
3924
3925 @table @asis
3926 @item @kbd{C-c C-s} (@code{c-show-syntactic-information})
3927 @kindex C-c C-s
3928 @findex c-show-syntactic-information
3929 @findex show-syntactic-information (c-)
3930 This command calculates the syntactic analysis of the current line and
3931 displays it in the minibuffer. The command also highlights the anchor
3932 position(s).
3933 @end table
3934
3935 Running this command on line 4 of this example, we'd see in the echo
3936 area@footnote{With a universal argument (i.e. @kbd{C-u C-c C-s}) the
3937 analysis is inserted into the buffer as a comment on the current
3938 line.}:
3939
3940 @example
3941 ((statement 35))
3942 @end example
3943
3944 @noindent
3945 and the @samp{i} of @code{int} on line 3 would be highlighted. This
3946 tells us that the line is a statement and it is indented relative to
3947 buffer position 35, the highlighted position. If you were to move
3948 point to line 3 and hit @kbd{C-c C-s}, you would see:
3949
3950 @example
3951 ((defun-block-intro 29))
3952 @end example
3953
3954 @noindent
3955 This indicates that the @samp{int} line is the first statement in a top
3956 level function block, and is indented relative to buffer position 29,
3957 which is the brace just after the function header.
3958
3959 Here's another example:
3960
3961 @example
3962 1: int add( int val, int incr, int doit )
3963 2: @{
3964 3: if( doit )
3965 4: @{
3966 5: return( val + incr );
3967 6: @}
3968 7: return( val );
3969 8: @}
3970 @end example
3971
3972 @noindent
3973 Hitting @kbd{C-c C-s} on line 4 gives us:
3974
3975 @example
3976 ((substatement-open 46))
3977 @end example
3978
3979 @cindex substatement
3980 @cindex substatement block
3981 @noindent
3982 which tells us that this is a brace that @emph{opens} a substatement
3983 block. @footnote{A @dfn{substatement} is the line after a
3984 conditional statement, such as @code{if}, @code{else}, @code{while},
3985 @code{do}, @code{switch}, etc. A @dfn{substatement
3986 block} is a brace block following one of these conditional statements.}
3987
3988 @cindex comment-only line
3989 Syntactic contexts can contain more than one element, and syntactic
3990 elements need not have anchor positions. The most common example of
3991 this is a @dfn{comment-only line}:
3992
3993 @example
3994 1: void draw_list( List<Drawables>& drawables )
3995 2: @{
3996 3: // call the virtual draw() method on each element in list
3997 4: for( int i=0; i < drawables.count(), ++i )
3998 5: @{
3999 6: drawables[i].draw();
4000 7: @}
4001 8: @}
4002 @end example
4003
4004 @noindent
4005 Hitting @kbd{C-c C-s} on line 3 of this example gives:
4006
4007 @example
4008 ((comment-intro) (defun-block-intro 46))
4009 @end example
4010
4011 @noindent
4012 and you can see that the syntactic context contains two syntactic
4013 elements. Notice that the first element, @samp{(comment-intro)}, has no
4014 anchor position.
4015
4016
4017 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4018 @node Syntactic Symbols, Indentation Calculation, Syntactic Analysis, Indentation Engine Basics
4019 @comment node-name, next, previous, up
4020 @section Syntactic Symbols
4021 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4022
4023 @cindex syntactic symbols, brief list
4024 @vindex c-offsets-alist
4025 @vindex offsets-alist (c-)
4026 This section is a complete list of the syntactic symbols which appear
4027 in the @code{c-offsets-alist} style variable, along with brief
4028 descriptions. The previous section (@pxref{Syntactic Analysis})
4029 states what syntactic symbols are and how the indentation engine uses
4030 them.
4031
4032 More detailed descriptions of these symbols, together with snippets of
4033 source code to which they apply, appear in the examples in the
4034 subsections below. Note that, in the interests of brevity, the anchor
4035 position associated with most syntactic symbols is @emph{not}
4036 specified. In cases of doubt, type @kbd{C-c C-s} on a pertinent
4037 line---this highlights the anchor position.
4038
4039 @ssindex -open symbols
4040 @ssindex -close symbols
4041 @ssindex -block-intro symbols
4042 The syntactic symbols which indicate brace constructs follow a general
4043 naming convention. When a line begins with an open or close brace,
4044 its syntactic symbol will contain the suffix @code{-open} or
4045 @code{-close} respectively. The first line within the brace block
4046 construct will contain the suffix @code{-block-intro}.
4047
4048 @ssindex -intro symbols
4049 @ssindex -cont symbols
4050 In constructs which can span several lines, a distinction is usually
4051 made between the first line that introduces the construct and the
4052 lines that continue it. The syntactic symbols that indicate these
4053 lines will contain the suffixes @code{-intro} or @code{-cont}
4054 respectively.
4055
4056 The best way to understand how all this works is by looking at some
4057 examples. Remember that you can see the syntax of any source code
4058 line by using @kbd{C-c C-s}.
4059
4060 @table @code
4061 @item string
4062 Inside a multiline string. @ref{Literal Symbols}.
4063 @item c
4064 Inside a multiline C style block comment. @ref{Literal Symbols}.
4065 @item defun-open
4066 Brace that opens a top-level function definition. @ref{Function
4067 Symbols}.
4068 @item defun-close
4069 Brace that closes a top-level function definition. @ref{Function
4070 Symbols}.
4071 @item defun-block-intro
4072 The first line in a top-level defun. @ref{Function Symbols}.
4073 @item class-open
4074 Brace that opens a class definition. @ref{Class Symbols}.
4075 @item class-close
4076 Brace that closes a class definition. @ref{Class Symbols}.
4077 @item inline-open
4078 Brace that opens an in-class inline method. @ref{Class Symbols}.
4079 @item inline-close
4080 Brace that closes an in-class inline method. @ref{Class Symbols}.
4081 @item func-decl-cont
4082 The region between a function definition's argument list and the
4083 function opening brace (excluding K&R argument declarations). In C,
4084 you cannot put anything but whitespace and comments in this region,
4085 however in C++ and Java, @code{throws} declarations and other things
4086 can appear here. @ref{Literal Symbols}. @c @emph{FIXME!!! Can it not
4087 @c go somewhere better?}
4088 @item knr-argdecl-intro
4089 First line of a K&R C argument declaration. @ref{K&R Symbols}.
4090 @item knr-argdecl
4091 Subsequent lines in a K&R C argument declaration. @ref{K&R Symbols}.
4092 @item topmost-intro
4093 The first line in a ``topmost'' definition. @ref{Function Symbols}.
4094 @item topmost-intro-cont
4095 Topmost definition continuation lines. This is only used in the parts
4096 that aren't covered by other symbols such as @code{func-decl-cont} and
4097 @code{knr-argdecl}. @ref{Function Symbols}.
4098 @item annotation-top-cont
4099 Topmost definition continuation lines where all previous items are
4100 annotations. @ref{Java Symbols}.
4101 @item member-init-intro
4102 First line in a member initialization list. @ref{Class Symbols}.
4103 @item member-init-cont
4104 Subsequent member initialization list lines. @ref{Class Symbols}.
4105 @item inher-intro
4106 First line of a multiple inheritance list. @ref{Class Symbols}.
4107 @item inher-cont
4108 Subsequent multiple inheritance lines. @ref{Class Symbols}.
4109 @item block-open
4110 Statement block open brace. @ref{Literal Symbols}.
4111 @item block-close
4112 Statement block close brace. @ref{Conditional Construct Symbols}.
4113 @item brace-list-open
4114 Open brace of an enum or static array list. @ref{Brace List Symbols}.
4115 @item brace-list-close
4116 Close brace of an enum or static array list. @ref{Brace List Symbols}.
4117 @item brace-list-intro
4118 First line in an enum or static array list. @ref{Brace List Symbols}.
4119 @item brace-list-entry
4120 Subsequent lines in an enum or static array list. @ref{Brace List
4121 Symbols}.
4122 @item brace-entry-open
4123 Subsequent lines in an enum or static array list where the line begins
4124 with an open brace. @ref{Brace List Symbols}.
4125 @item statement
4126 A statement. @ref{Function Symbols}.
4127 @item statement-cont
4128 A continuation of a statement. @ref{Function Symbols}.
4129 @item annotation-var-cont
4130 A continuation of a statement where all previous items are
4131 annotations. @ref{Java Symbols}.
4132 @item statement-block-intro
4133 The first line in a new statement block. @ref{Conditional Construct
4134 Symbols}.
4135 @item statement-case-intro
4136 The first line in a case block. @ref{Switch Statement Symbols}.
4137 @item statement-case-open
4138 The first line in a case block that starts with a brace. @ref{Switch
4139 Statement Symbols}.
4140 @item substatement
4141 The first line after a conditional or loop construct.
4142 @ref{Conditional Construct Symbols}.
4143 @item substatement-open
4144 The brace that opens a substatement block. @ref{Conditional Construct
4145 Symbols}.
4146 @item substatement-label
4147 The first line after a conditional or loop construct if it's a label.
4148 @ref{Conditional Construct Symbols}.
4149 @item case-label
4150 A label in a @code{switch} block. @ref{Switch Statement Symbols}.
4151 @item access-label
4152 C++ access control label. @ref{Class Symbols}.
4153 @item label
4154 Any other label. @ref{Literal Symbols}.
4155 @item do-while-closure
4156 The @code{while} line that ends a @code{do}-@code{while} construct.
4157 @ref{Conditional Construct Symbols}.
4158 @item else-clause
4159 The @code{else} line of an @code{if}-@code{else} construct.
4160 @ref{Conditional Construct Symbols}.
4161 @item catch-clause
4162 The @code{catch} or @code{finally} (in Java) line of a
4163 @code{try}-@code{catch} construct. @ref{Conditional Construct
4164 Symbols}.
4165 @item comment-intro
4166 A line containing only a comment introduction. @ref{Literal Symbols}.
4167 @item arglist-intro
4168 The first line in an argument list. @ref{Paren List Symbols}.
4169 @item arglist-cont
4170 Subsequent argument list lines when no arguments follow on the same
4171 line as the arglist opening paren. @ref{Paren List Symbols}.
4172 @item arglist-cont-nonempty
4173 Subsequent argument list lines when at least one argument follows on
4174 the same line as the arglist opening paren. @ref{Paren List Symbols}.
4175 @item arglist-close
4176 The solo close paren of an argument list. @ref{Paren List Symbols}.
4177 @item stream-op
4178 Lines continuing a stream operator (C++ only). @ref{Literal
4179 Symbols}. @c @emph{FIXME!!! Can this not be moved somewhere better?}
4180 @item inclass
4181 The line is nested inside a class definition. @ref{Class Symbols}.
4182 @item cpp-macro
4183 The start of a preprocessor macro definition. @ref{Literal Symbols}.
4184 @item cpp-define-intro
4185 The first line inside a multiline preprocessor macro if
4186 @code{c-syntactic-indentation-in-macros} is set. @ref{Multiline Macro
4187 Symbols}.
4188 @item cpp-macro-cont
4189 All lines inside multiline preprocessor macros if
4190 @code{c-syntactic-indentation-in-macros} is @code{nil}.
4191 @ref{Multiline Macro Symbols}.
4192 @item friend
4193 A C++ friend declaration. @ref{Class Symbols}.
4194 @item objc-method-intro
4195 The first line of an Objective-C method definition. @ref{Objective-C
4196 Method Symbols}.
4197 @item objc-method-args-cont
4198 Lines continuing an Objective-C method definition. @ref{Objective-C
4199 Method Symbols}.
4200 @item objc-method-call-cont
4201 Lines continuing an Objective-C method call. @ref{Objective-C Method
4202 Symbols}.
4203 @item extern-lang-open
4204 Brace that opens an @code{extern} block (e.g. @code{extern "C"
4205 @{...@}}). @ref{External Scope Symbols}.
4206 @item extern-lang-close
4207 Brace that closes an @code{extern} block. @ref{External Scope
4208 Symbols}.
4209 @item inextern-lang
4210 Analogous to @code{inclass} syntactic symbol, but used inside
4211 @code{extern} blocks. @ref{External Scope Symbols}.
4212 @item namespace-open
4213 @itemx namespace-close
4214 @itemx innamespace
4215 These are analogous to the three @code{extern-lang} symbols above, but
4216 are returned for C++ namespace blocks. @ref{External Scope Symbols}.
4217 @item module-open
4218 @itemx module-close
4219 @itemx inmodule
4220 Analogous to the above, but for CORBA IDL @code{module} blocks.
4221 @ref{External Scope Symbols}.
4222 @item composition-open
4223 @itemx composition-close
4224 @itemx incomposition
4225 Analogous to the above, but for CORBA CIDL @code{composition} blocks.
4226 @ref{External Scope Symbols}.
4227 @item template-args-cont
4228 C++ template argument list continuations. @ref{Class Symbols}.
4229 @item inlambda
4230 Analogous to @code{inclass} syntactic symbol, but used inside lambda
4231 (i.e. anonymous) functions. Only used in Pike mode. @ref{Statement
4232 Block Symbols}.
4233 @item lambda-intro-cont
4234 Lines continuing the header of a lambda function, i.e. between the
4235 @code{lambda} keyword and the function body. Only used in Pike mode.
4236 @ref{Statement Block Symbols}.
4237 @item inexpr-statement
4238 A statement block inside an expression. The gcc C and C++ extension
4239 for this is recognized. It's also used for the special functions that
4240 take a statement block as an argument in Pike. @ref{Statement Block
4241 Symbols}.
4242 @item inexpr-class
4243 A class definition inside an expression. This is used for anonymous
4244 classes in Java. It's also used for anonymous array initializers in
4245 Java. @ref{Java Symbols}.
4246 @end table
4247
4248 @menu
4249 * Function Symbols::
4250 * Class Symbols::
4251 * Conditional Construct Symbols::
4252 * Switch Statement Symbols::
4253 * Brace List Symbols::
4254 * External Scope Symbols::
4255 * Paren List Symbols::
4256 * Literal Symbols::
4257 * Multiline Macro Symbols::
4258 * Objective-C Method Symbols::
4259 * Java Symbols::
4260 * Statement Block Symbols::
4261 * K&R Symbols::
4262 @end menu
4263
4264 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4265 @node Function Symbols, Class Symbols, Syntactic Symbols, Syntactic Symbols
4266 @comment node-name, next, previous, up
4267 @subsection Function Symbols
4268 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4269
4270 This example shows a typical function declaration.
4271
4272 @example
4273 1: void
4274 2: swap( int& a, int& b )
4275 3: @{
4276 4: int tmp = a;
4277 5: a = b;
4278 6: b = tmp;
4279 7: int ignored =
4280 8: a + b;
4281 9: @}
4282 @end example
4283
4284 @ssindex topmost-intro
4285 @ssindex topmost-intro-cont
4286 @ssindex defun-open
4287 @ssindex defun-close
4288 @ssindex defun-block-intro
4289 Line 1 shows a @code{topmost-intro} since it is the first line that
4290 introduces a top-level construct. Line 2 is a continuation of the
4291 top-level construct introduction so it has the syntax
4292 @code{topmost-intro-cont}. Line 3 shows a @code{defun-open} since it is
4293 the brace that opens a top-level function definition. Line 9 is the
4294 corresponding
4295 @code{defun-close} since it contains the brace that closes the top-level
4296 function definition. Line 4 is a @code{defun-block-intro}, i.e. it is
4297 the first line of a brace-block, enclosed in a
4298 top-level function definition.
4299
4300 @ssindex statement
4301 @ssindex statement-cont
4302 Lines 5, 6, and 7 are all given @code{statement} syntax since there
4303 isn't much special about them. Note however that line 8 is given
4304 @code{statement-cont} syntax since it continues the statement begun
4305 on the previous line.
4306
4307 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4308 @node Class Symbols, Conditional Construct Symbols, Function Symbols, Syntactic Symbols
4309 @comment node-name, next, previous, up
4310 @subsection Class related Symbols
4311 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4312
4313 Here's an example which illustrates some C++ class syntactic symbols:
4314
4315 @example
4316 1: class Bass
4317 2: : public Guitar,
4318 3: public Amplifiable
4319 4: @{
4320 5: public:
4321 6: Bass()
4322 7: : eString( new BassString( 0.105 )),
4323 8: aString( new BassString( 0.085 )),
4324 9: dString( new BassString( 0.065 )),
4325 10: gString( new BassString( 0.045 ))
4326 11: @{
4327 12: eString.tune( 'E' );
4328 13: aString.tune( 'A' );
4329 14: dString.tune( 'D' );
4330 15: gString.tune( 'G' );
4331 16: @}
4332 17: friend class Luthier;
4333 18: @};
4334 @end example
4335
4336 @ssindex class-open
4337 @ssindex class-close
4338 As in the previous example, line 1 has the @code{topmost-intro} syntax.
4339 Here however, the brace that opens a C++ class definition on line 4 is
4340 assigned the @code{class-open} syntax. Note that in C++, classes,
4341 structs, and unions are essentially equivalent syntactically (and are
4342 very similar semantically), so replacing the @code{class} keyword in the
4343 example above with @code{struct} or @code{union} would still result in a
4344 syntax of @code{class-open} for line 4 @footnote{This is the case even
4345 for C and Objective-C. For consistency, structs in all supported
4346 languages are syntactically equivalent to classes. Note however that
4347 the keyword @code{class} is meaningless in C and Objective-C.}.
4348 Similarly, line 18 is assigned @code{class-close} syntax.
4349
4350 @ssindex inher-intro
4351 @ssindex inher-cont
4352 Line 2 introduces the inheritance list for the class so it is assigned
4353 the @code{inher-intro} syntax, and line 3, which continues the
4354 inheritance list is given @code{inher-cont} syntax.
4355
4356 @ssindex access-label
4357 @ssindex inclass
4358 Hitting @kbd{C-c C-s} on line 5 shows the following analysis:
4359
4360 @example
4361 ((inclass 58) (access-label 58))
4362 @end example
4363
4364 @noindent
4365 The primary syntactic symbol for this line is @code{access-label} as
4366 this is a label keyword that specifies access protection in C++. However,
4367 because this line is also a top-level construct inside a class
4368 definition, the analysis actually shows two syntactic symbols. The
4369 other syntactic symbol assigned to this line is @code{inclass}.
4370 Similarly, line 6 is given both @code{inclass} and @code{topmost-intro}
4371 syntax:
4372
4373 @example
4374 ((inclass 58) (topmost-intro 60))
4375 @end example
4376
4377 @ssindex member-init-intro
4378 @ssindex member-init-cont
4379 Line 7 introduces a C++ member initialization list and as such is given
4380 @code{member-init-intro} syntax. Note that in this case it is
4381 @emph{not} assigned @code{inclass} since this is not considered a
4382 top-level construct. Lines 8 through 10 are all assigned
4383 @code{member-init-cont} since they continue the member initialization
4384 list started on line 7.
4385
4386 @cindex in-class inline methods
4387 @ssindex inline-open
4388 @ssindex inline-close
4389 Line 11's analysis is a bit more complicated:
4390
4391 @example
4392 ((inclass 58) (inline-open))
4393 @end example
4394
4395 This line is assigned a syntax of both @code{inline-open} and
4396 @code{inclass} because it opens an @dfn{in-class} C++ inline method
4397 definition. This is distinct from, but related to, the C++ notion of an
4398 inline function in that its definition occurs inside an enclosing class
4399 definition, which in C++ implies that the function should be inlined.
4400 However, if the definition of the @code{Bass} constructor appeared
4401 outside the class definition, the construct would be given the
4402 @code{defun-open} syntax, even if the keyword @code{inline} appeared
4403 before the method name, as in:
4404
4405 @example
4406 1: class Bass
4407 2: : public Guitar,
4408 3: public Amplifiable
4409 4: @{
4410 5: public:
4411 6: Bass();
4412 7: @};
4413 8:
4414 9: inline
4415 10: Bass::Bass()
4416 11: : eString( new BassString( 0.105 )),
4417 12: aString( new BassString( 0.085 )),
4418 13: dString( new BassString( 0.065 )),
4419 14: gString( new BassString( 0.045 ))
4420 15: @{
4421 16: eString.tune( 'E' );
4422 17: aString.tune( 'A' );
4423 18: dString.tune( 'D' );
4424 19: gString.tune( 'G' );
4425 20: @}
4426 @end example
4427
4428 @ssindex friend
4429 Returning to the previous example, line 16 is given @code{inline-close}
4430 syntax, while line 12 is given @code{defun-block-open} syntax, and lines
4431 13 through 15 are all given @code{statement} syntax. Line 17 is
4432 interesting in that its syntactic analysis list contains three
4433 elements:
4434
4435 @example
4436 ((inclass 58) (topmost-intro 380) (friend))
4437 @end example
4438
4439 The @code{friend} and @code{inline-open} syntactic symbols are
4440 modifiers that do not have anchor positions.
4441
4442 @ssindex template-args-cont
4443 Template definitions introduce yet another syntactic symbol:
4444
4445 @example
4446 1: ThingManager <int,
4447 2: Framework::Callback *,
4448 3: Mutex> framework_callbacks;
4449 @end example
4450
4451 Here, line 1 is analyzed as a @code{topmost-intro}, but lines 2 and 3
4452 are both analyzed as @code{template-args-cont} lines.
4453
4454 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4455 @node Conditional Construct Symbols, Switch Statement Symbols, Class Symbols, Syntactic Symbols
4456 @comment node-name, next, previous, up
4457 @subsection Conditional Construct Symbols
4458 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4459
4460 Here is a (totally contrived) example which illustrates how syntax is
4461 assigned to various conditional constructs:
4462
4463 @example
4464 1: void spam( int index )
4465 2: @{
4466 3: for( int i=0; i<index; i++ )
4467 4: @{
4468 5: if( i == 10 )
4469 6: do_something_special();
4470 7: else
4471 8: silly_label:
4472 9: do_something( i );
4473 10: @}
4474 11: do @{
4475 12: another_thing( i-- );
4476 13: @}
4477 14: while( i > 0 );
4478 15: @}
4479 @end example
4480
4481 Only the lines that illustrate new syntactic symbols will be discussed.
4482
4483 @ssindex substatement-open
4484 @ssindex statement-block-intro
4485 @ssindex block-close
4486 Line 4 has a brace which opens a conditional's substatement block. It
4487 is thus assigned @code{substatement-open} syntax, and since line 5 is
4488 the first line in the substatement block, it is assigned
4489 @code{statement-block-intro} syntax. Line 10 contains the brace
4490 that closes the inner substatement block, and is therefore given the
4491 syntax @code{block-close}@footnote{@code{block-open} is used only for
4492 ``free-standing'' blocks, and is somewhat rare (@pxref{Literal
4493 Symbols} for an example.)}. Line 13 is treated the same way.
4494
4495 @ssindex substatement
4496 Lines 6 and 9 are also substatements of conditionals, but since they
4497 don't start blocks they are given @code{substatement} syntax
4498 instead of @code{substatement-open}.
4499
4500 @ssindex substatement-label
4501 Line 8 contains a label, which is normally given @code{label} syntax.
4502 This one is however a bit special since it's between a conditional and
4503 its substatement. It's analyzed as @code{substatement-label} to let you
4504 handle this rather odd case differently from normal labels.
4505
4506 @ssindex else-clause
4507 @ssindex catch-clause
4508 Line 7 start with an @code{else} that matches the @code{if} statement on
4509 line 5. It is therefore given the @code{else-clause} syntax and is
4510 anchored on the matching @code{if}. The @code{try}-@code{catch}
4511 constructs in C++ and Java are treated this way too, except that
4512 @code{catch} and (in Java) @code{finally}, are marked with
4513 @code{catch-clause}.
4514
4515 @ssindex do-while-closure
4516 The @code{while} construct on line 14 that closes a @code{do}
4517 conditional is given the special syntax @code{do-while-closure} if it
4518 appears on a line by itself. Note that if the @code{while} appeared on
4519 the same line as the preceding close brace, that line would still have
4520 @code{block-close} syntax.
4521
4522 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4523 @node Switch Statement Symbols, Brace List Symbols, Conditional Construct Symbols, Syntactic Symbols
4524 @comment node-name, next, previous, up
4525 @subsection Switch Statement Symbols
4526 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4527
4528 Switch statements have their own set of syntactic symbols. Here's an
4529 example:
4530
4531 @example
4532 1: void spam( enum Ingredient i )
4533 2: @{
4534 3: switch( i ) @{
4535 4: case Ham:
4536 5: be_a_pig();
4537 6: break;
4538 7: case Salt:
4539 8: drink_some_water();
4540 9: break;
4541 10: default:
4542 11: @{
4543 12: what_is_it();
4544 13: break;
4545 14: @}
4546 15: @}
4547 14: @}
4548 @end example
4549
4550 @ssindex case-label
4551 @ssindex statement-case-intro
4552 @ssindex statement-case-open
4553 Here, lines 4, 7, and 10 are all assigned @code{case-label} syntax,
4554 while lines 5 and 8 are assigned @code{statement-case-intro}. Line 11
4555 is treated slightly differently since it contains a brace that opens a
4556 block --- it is given @code{statement-case-open} syntax.
4557
4558 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4559 @node Brace List Symbols, External Scope Symbols, Switch Statement Symbols, Syntactic Symbols
4560 @comment node-name, next, previous, up
4561 @subsection Brace List Symbols
4562 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4563
4564 @cindex brace lists
4565 There are a set of syntactic symbols that are used to recognize
4566 constructs inside of brace lists. A brace list is defined as an
4567 @code{enum} or aggregate initializer list, such as might statically
4568 initialize an array of structs. The three special aggregate constructs
4569 in Pike, @code{(@{ @})}, @code{([ ])} and @code{(< >)}, are treated as
4570 brace lists too. An example:
4571
4572 @example
4573 1: static char* ingredients[] =
4574 2: @{
4575 3: "Ham",
4576 4: "Salt",
4577 5: NULL
4578 6: @};
4579 @end example
4580
4581 @ssindex brace-list-open
4582 @ssindex brace-list-intro
4583 @ssindex brace-list-close
4584 @ssindex brace-list-entry
4585 Following convention, line 2 in this example is assigned
4586 @code{brace-list-open} syntax, and line 3 is assigned
4587 @code{brace-list-intro} syntax. Likewise, line 6 is assigned
4588 @code{brace-list-close} syntax. Lines 4 and 5 however, are assigned
4589 @code{brace-list-entry} syntax, as would all subsequent lines in this
4590 initializer list.
4591
4592 @ssindex brace-entry-open
4593 Your static initializer might be initializing nested structures, for
4594 example:
4595
4596 @example
4597 1: struct intpairs[] =
4598 2: @{
4599 3: @{ 1, 2 @},
4600 4: @{
4601 5: 3,
4602 6: 4
4603 7: @}
4604 8: @{ 1,
4605 9: 2 @},
4606 10: @{ 3, 4 @}
4607 11: @};
4608 @end example
4609
4610 Here, you've already seen the analysis of lines 1, 2, 3, and 11. On
4611 line 4, things get interesting; this line is assigned
4612 @code{brace-entry-open} syntactic symbol because it's a bracelist entry
4613 line that starts with an open brace. Lines 5 and 6 (and line 9) are
4614 pretty standard, and line 7 is a @code{brace-list-close} as you'd
4615 expect. Once again, line 8 is assigned as @code{brace-entry-open} as is
4616 line 10.
4617
4618 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4619 @node External Scope Symbols, Paren List Symbols, Brace List Symbols, Syntactic Symbols
4620 @comment node-name, next, previous, up
4621 @subsection External Scope Symbols
4622 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4623
4624 External language definition blocks also have their own syntactic
4625 symbols. In this example:
4626
4627 @example
4628 1: extern "C"
4629 2: @{
4630 3: int thing_one( int );
4631 4: int thing_two( double );
4632 5: @}
4633 @end example
4634
4635 @ssindex extern-lang-open
4636 @ssindex extern-lang-close
4637 @ssindex inextern-lang
4638 @ssindex inclass
4639 @noindent
4640 line 2 is given the @code{extern-lang-open} syntax, while line 5 is given
4641 the @code{extern-lang-close} syntax. The analysis for line 3 yields:
4642
4643 @example
4644 ((inextern-lang) (topmost-intro 14))
4645 @end example
4646
4647 @noindent
4648 where @code{inextern-lang} is a modifier similar in purpose to
4649 @code{inclass}.
4650
4651 There are various other top level blocks like @code{extern}, and they
4652 are all treated in the same way except that the symbols are named after
4653 the keyword that introduces the block. E.g. C++ namespace blocks get
4654 the three symbols @code{namespace-open}, @code{namespace-close} and
4655 @code{innamespace}. The currently recognized top level blocks are:
4656
4657 @table @asis
4658 @item @code{extern-lang-open}, @code{extern-lang-close}, @code{inextern-lang}
4659 @code{extern} blocks in C and C++.@footnote{These should logically be
4660 named @code{extern-open}, @code{extern-close} and @code{inextern}, but
4661 that isn't the case for historical reasons.}
4662
4663 @item @code{namespace-open}, @code{namespace-close}, @code{innamespace}
4664 @ssindex namespace-open
4665 @ssindex namespace-close
4666 @ssindex innamespace
4667 @code{namespace} blocks in C++.
4668
4669 @item @code{module-open}, @code{module-close}, @code{inmodule}
4670 @ssindex module-open
4671 @ssindex module-close
4672 @ssindex inmodule
4673 @code{module} blocks in CORBA IDL.
4674
4675 @item @code{composition-open}, @code{composition-close}, @code{incomposition}
4676 @ssindex composition-open
4677 @ssindex composition-close
4678 @ssindex incomposition
4679 @code{composition} blocks in CORBA CIDL.
4680 @end table
4681
4682 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4683 @node Paren List Symbols, Literal Symbols, External Scope Symbols, Syntactic Symbols
4684 @comment node-name, next, previous, up
4685 @subsection Parenthesis (Argument) List Symbols
4686 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4687
4688 A number of syntactic symbols are associated with parenthesis lists,
4689 a.k.a argument lists, as found in function declarations and function
4690 calls. This example illustrates these:
4691
4692 @example
4693 1: void a_function( int line1,
4694 2: int line2 );
4695 3:
4696 4: void a_longer_function(
4697 5: int line1,
4698 6: int line2
4699 7: );
4700 8:
4701 9: void call_them( int line1, int line2 )
4702 10: @{
4703 11: a_function(
4704 12: line1,
4705 13: line2
4706 14: );
4707 15:
4708 16: a_longer_function( line1,
4709 17: line2 );
4710 18: @}
4711 @end example
4712
4713 @ssindex arglist-intro
4714 @ssindex arglist-close
4715 Lines 5 and 12 are assigned @code{arglist-intro} syntax since they are
4716 the first line following the open parenthesis, and lines 7 and 14 are
4717 assigned @code{arglist-close} syntax since they contain the parenthesis
4718 that closes the argument list.
4719
4720 @ssindex arglist-cont-nonempty
4721 @ssindex arglist-cont
4722 Lines that continue argument lists can be assigned one of two syntactic
4723 symbols. For example, Lines 2 and 17
4724 are assigned @code{arglist-cont-nonempty} syntax. What this means
4725 is that they continue an argument list, but that the line containing the
4726 parenthesis that opens the list is @emph{not empty} following the open
4727 parenthesis. Contrast this against lines 6 and 13 which are assigned
4728 @code{arglist-cont} syntax. This is because the parenthesis that opens
4729 their argument lists is the last character on that line.
4730
4731 Syntactic elements with @code{arglist-intro},
4732 @code{arglist-cont-nonempty}, and @code{arglist-close} contain two
4733 buffer positions: the anchor position (the beginning of the
4734 declaration or statement) and the position of the open parenthesis.
4735 The latter position can be used in a line-up function (@pxref{Line-Up
4736 Functions}).
4737
4738 Note that there is no @code{arglist-open} syntax. This is because any
4739 parenthesis that opens an argument list, appearing on a separate line,
4740 is assigned the @code{statement-cont} syntax instead.
4741
4742 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4743 @node Literal Symbols, Multiline Macro Symbols, Paren List Symbols, Syntactic Symbols
4744 @comment node-name, next, previous, up
4745 @subsection Comment String Label and Macro Symbols
4746 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4747
4748 A few miscellaneous syntactic symbols that haven't been previously
4749 covered are illustrated by this C++ example:
4750
4751 @example
4752 1: void Bass::play( int volume )
4753 2: const
4754 3: @{
4755 4: /* this line starts a multiline
4756 5: * comment. This line should get `c' syntax */
4757 6:
4758 7: char* a_multiline_string = "This line starts a multiline \
4759 8: string. This line should get `string' syntax.";
4760 9:
4761 10: note:
4762 11: @{
4763 12: #ifdef LOCK
4764 13: Lock acquire();
4765 14: #endif // LOCK
4766 15: slap_pop();
4767 16: cout << "I played "
4768 17: << "a note\n";
4769 18: @}
4770 19: @}
4771 @end example
4772
4773 The lines to note in this example include:
4774
4775 @itemize @bullet
4776 @item
4777 @ssindex func-decl-cont
4778 Line 2 is assigned the @code{func-decl-cont} syntax.
4779
4780 @item
4781 @ssindex comment-intro
4782 Line 4 is assigned both @code{defun-block-intro} @emph{and}
4783 @code{comment-intro} syntax. A syntactic element with
4784 @code{comment-intro} has no anchor point --- It is always accompanied
4785 by another syntactic element which does have one.
4786
4787 @item
4788 @ssindex c
4789 Line 5 is assigned @code{c} syntax.
4790
4791 @item
4792 @cindex syntactic whitespace
4793 Line 6 which, even though it contains nothing but whitespace, is
4794 assigned @code{defun-block-intro}. Note that the appearance of the
4795 comment on lines 4 and 5 do not cause line 6 to be assigned
4796 @code{statement} syntax because comments are considered to be
4797 @dfn{syntactic whitespace}, which are ignored when analyzing
4798 code.
4799
4800 @item
4801 @ssindex string
4802 Line 8 is assigned @code{string} syntax.
4803
4804 @item
4805 @ssindex label
4806 Line 10 is assigned @code{label} syntax.
4807
4808 @item
4809 @ssindex block-open
4810 Line 11 is assigned @code{block-open} as well as @code{statement}
4811 syntax. A @code{block-open} syntactic element doesn't have an anchor
4812 position, since it always appears with another syntactic element which
4813 does have one.
4814
4815 @item
4816 @ssindex cpp-macro
4817 Lines 12 and 14 are assigned @code{cpp-macro} syntax in addition to the
4818 normal syntactic symbols (@code{statement-block-intro} and
4819 @code{statement}, respectively). Normally @code{cpp-macro} is
4820 configured to cancel out the normal syntactic context to make all
4821 preprocessor directives stick to the first column, but that's easily
4822 changed if you want preprocessor directives to be indented like the rest
4823 of the code. Like @code{comment-intro}, a syntactic element with
4824 @code{cpp-macro} doesn't contain an anchor position.
4825
4826 @item
4827 @ssindex stream-op
4828 Line 17 is assigned @code{stream-op} syntax.
4829 @end itemize
4830
4831 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4832 @node Multiline Macro Symbols, Objective-C Method Symbols, Literal Symbols, Syntactic Symbols
4833 @comment node-name, next, previous, up
4834 @subsection Multiline Macro Symbols
4835 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4836
4837 @cindex multiline macros
4838 @cindex syntactic whitespace
4839 @ssindex cpp-define-intro
4840 @ssindex cpp-macro-cont
4841 Multiline preprocessor macro definitions are normally handled just like
4842 other code, i.e. the lines inside them are indented according to the
4843 syntactic analysis of the preceding lines inside the macro. The first
4844 line inside a macro definition (i.e. the line after the starting line of
4845 the cpp directive itself) gets @code{cpp-define-intro}. In this example:
4846
4847 @example
4848 1: #define LIST_LOOP(cons, listp) \
4849 2: for (cons = listp; !NILP (cons); cons = XCDR (cons)) \
4850 3: if (!CONSP (cons)) \
4851 4: signal_error ("Invalid list format", listp); \
4852 5: else
4853 @end example
4854
4855 @noindent
4856 line 1 is given the syntactic symbol @code{cpp-macro}. The first line
4857 of a cpp directive is always given that symbol. Line 2 is given
4858 @code{cpp-define-intro}, so that you can give the macro body as a whole
4859 some extra indentation. Lines 3 through 5 are then analyzed as normal
4860 code, i.e. @code{substatement} on lines 3 and 4, and @code{else-clause}
4861 on line 5.
4862
4863 The syntactic analysis inside macros can be turned off with
4864 @code{c-syntactic-indentation-in-macros} (@pxref{Custom Macros}). In
4865 that case, lines 2 through 5 would all be given @code{cpp-macro-cont}
4866 with an anchor position pointing to the @code{#} which starts the cpp
4867 directive@footnote{This is how @ccmode{} 5.28 and earlier analyzed
4868 macros.}.
4869
4870 @xref{Custom Macros}, for more info about the treatment of macros.
4871
4872 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4873 @node Objective-C Method Symbols, Java Symbols, Multiline Macro Symbols, Syntactic Symbols
4874 @comment node-name, next, previous, up
4875 @subsection Objective-C Method Symbols
4876 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4877
4878 In Objective-C buffers, there are three additional syntactic symbols
4879 assigned to various message calling constructs. Here's an example
4880 illustrating these:
4881
4882 @example
4883 1: - (void)setDelegate:anObject
4884 2: withStuff:stuff
4885 3: @{
4886 4: [delegate masterWillRebind:self
4887 5: toDelegate:anObject
4888 6: withExtraStuff:stuff];
4889 7: @}
4890 @end example
4891
4892 @ssindex objc-method-intro
4893 @ssindex objc-method-args-cont
4894 @ssindex objc-method-call-cont
4895 Here, line 1 is assigned @code{objc-method-intro} syntax, and line 2 is
4896 assigned @code{objc-method-args-cont} syntax. Lines 5 and 6 are both
4897 assigned @code{objc-method-call-cont} syntax.
4898
4899 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4900 @node Java Symbols, Statement Block Symbols, Objective-C Method Symbols, Syntactic Symbols
4901 @comment node-name, next, previous, up
4902 @subsection Java Symbols
4903 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4904
4905 Java has a concept of anonymous classes which can look something like
4906 this:
4907
4908 @example
4909 1: @@Test
4910 2: public void watch(Observable o) @{
4911 3: @@NonNull
4912 4: Observer obs = new Observer() @{
4913 5: public void update(Observable o, Object arg) @{
4914 6: history.addElement(arg);
4915 7: @}
4916 8: @};
4917 9: o.addObserver(obs);
4918 10: @}
4919 @end example
4920
4921 @ssindex inexpr-class
4922 The brace following the @code{new} operator opens the anonymous class.
4923 Lines 5 and 8 are assigned the @code{inexpr-class} syntax, besides the
4924 @code{inclass} symbol used in normal classes. Thus, the class will be
4925 indented just like a normal class, with the added indentation given to
4926 @code{inexpr-class}. An @code{inexpr-class} syntactic element doesn't
4927 have an anchor position.
4928
4929 @ssindex annotation-top-cont
4930 @ssindex annotation-var-cont
4931 Line 2 is assigned the @code{annotation-top-cont} syntax, due to it being a
4932 continuation of a topmost introduction with an annotation symbol preceding
4933 the current line. Similarly, line 4 is assigned the @code{annotation-var-cont}
4934 syntax due to it being a continuation of a variable declaration where preceding
4935 the declaration is an annotation.
4936
4937 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4938 @node Statement Block Symbols, K&R Symbols, Java Symbols, Syntactic Symbols
4939 @comment node-name, next, previous, up
4940 @subsection Statement Block Symbols
4941 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4942
4943 There are a few occasions where a statement block might be used inside
4944 an expression. One is in C or C++ code using the gcc extension for
4945 this, e.g:
4946
4947 @example
4948 1: int res = (@{
4949 2: int y = foo (); int z;
4950 3: if (y > 0) z = y; else z = - y;
4951 4: z;
4952 5: @});
4953 @end example
4954
4955 @ssindex inexpr-statement
4956 Lines 2 and 5 get the @code{inexpr-statement} syntax, besides the
4957 symbols they'd get in a normal block. Therefore, the indentation put on
4958 @code{inexpr-statement} is added to the normal statement block
4959 indentation. An @code{inexpr-statement} syntactic element doesn't
4960 contain an anchor position.
4961
4962 In Pike code, there are a few other situations where blocks occur inside
4963 statements, as illustrated here:
4964
4965 @example
4966 1: array itgob()
4967 2: @{
4968 3: string s = map (backtrace()[-2][3..],
4969 4: lambda
4970 5: (mixed arg)
4971 6: @{
4972 7: return sprintf ("%t", arg);
4973 8: @}) * ", " + "\n";
4974 9: return catch @{
4975 10: write (s + "\n");
4976 11: @};
4977 12: @}
4978 @end example
4979
4980 @ssindex inlambda
4981 @ssindex lambda-intro-cont
4982 Lines 4 through 8 contain a lambda function, which @ccmode{} recognizes
4983 by the @code{lambda} keyword. If the function argument list is put
4984 on a line of its own, as in line 5, it gets the @code{lambda-intro-cont}
4985 syntax. The function body is handled as an inline method body, with the
4986 addition of the @code{inlambda} syntactic symbol. This means that line
4987 6 gets @code{inlambda} and @code{inline-open}, and line 8 gets
4988 @code{inline-close}@footnote{You might wonder why it doesn't get
4989 @code{inlambda} too. It's because the closing brace is relative to the
4990 opening brace, which stands on its own line in this example. If the
4991 opening brace was hanging on the previous line, then the closing brace
4992 would get the @code{inlambda} syntax too to be indented correctly.}.
4993
4994 @ssindex inexpr-statement
4995 On line 9, @code{catch} is a special function taking a statement block
4996 as its argument. The block is handled as an in-expression statement
4997 with the @code{inexpr-statement} syntax, just like the gcc extended C
4998 example above. The other similar special function, @code{gauge}, is
4999 handled like this too.
5000
5001 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5002 @node K&R Symbols, , Statement Block Symbols, Syntactic Symbols
5003 @comment node-name, next, previous, up
5004 @subsection K&R Symbols
5005 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5006
5007 @ssindex knr-argdecl-intro
5008 @ssindex knr-argdecl
5009 Two other syntactic symbols can appear in old style, non-prototyped C
5010 code @footnote{a.k.a. K&R C, or Kernighan & Ritchie C}:
5011
5012 @example
5013 1: int add_three_integers(a, b, c)
5014 2: int a;
5015 3: int b;
5016 4: int c;
5017 5: @{
5018 6: return a + b + c;
5019 7: @}
5020 @end example
5021
5022 Here, line 2 is the first line in an argument declaration list and so is
5023 given the @code{knr-argdecl-intro} syntactic symbol. Subsequent lines
5024 (i.e. lines 3 and 4 in this example), are given @code{knr-argdecl}
5025 syntax.
5026
5027
5028 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5029 @node Indentation Calculation, , Syntactic Symbols, Indentation Engine Basics
5030 @comment node-name, next, previous, up
5031 @section Indentation Calculation
5032 @cindex indentation
5033 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5034
5035 Indentation for a line is calculated from the syntactic context
5036 (@pxref{Syntactic Analysis}).
5037
5038 First, a buffer position is found whose column will be the base for the
5039 indentation calculation. It's the anchor position in the first
5040 syntactic element that provides one that is used. If no syntactic
5041 element has an anchor position then column zero is used.
5042
5043 Second, the syntactic symbols in each syntactic element are looked up
5044 in the @code{c-offsets-alist} style variable
5045 (@pxref{c-offsets-alist}), which is an association list of syntactic
5046 symbols and the offsets to apply for those symbols. These offsets are
5047 added together with the base column to produce the new indentation
5048 column.
5049
5050 Let's use our two code examples above to see how this works. Here is
5051 our first example again:
5052
5053 @example
5054 1: void swap( int& a, int& b )
5055 2: @{
5056 3: int tmp = a;
5057 4: a = b;
5058 5: b = tmp;
5059 6: @}
5060 @end example
5061
5062 Let's say point is on line 3 and we hit the @key{TAB} key to reindent
5063 the line. The syntactic context for that line is:
5064
5065 @example
5066 ((defun-block-intro 29))
5067 @end example
5068
5069 @noindent
5070 Since buffer position 29 is the first and only anchor position in the
5071 list, @ccmode{} goes there and asks for the current column. This brace
5072 is in column zero, so @ccmode{} uses @samp{0} as the base column.
5073
5074 Next, @ccmode{} looks up @code{defun-block-intro} in the
5075 @code{c-offsets-alist} style variable. Let's say it finds the value
5076 @samp{4}; it adds this to the base column @samp{0}, yielding a running
5077 total indentation of 4 spaces.
5078
5079 Since there is only one syntactic element on the list for this line,
5080 indentation calculation is complete, and the total indentation for the
5081 line is 4 spaces.
5082
5083 Here's another example:
5084
5085 @example
5086 1: int add( int val, int incr, int doit )
5087 2: @{
5088 3: if( doit )
5089 4: @{
5090 5: return( val + incr );
5091 6: @}
5092 7: return( val );
5093 8: @}
5094 @end example
5095
5096 If we were to hit @kbd{TAB} on line 4 in the above example, the same
5097 basic process is performed, despite the differences in the syntactic
5098 context. The context for this line is:
5099
5100 @example
5101 ((substatement-open 46))
5102 @end example
5103
5104 Here, @ccmode{} goes to buffer position 46, which is the @samp{i} in
5105 @code{if} on line 3. This character is in the fourth column on that
5106 line so the base column is @samp{4}. Then @ccmode{} looks up the
5107 @code{substatement-open} symbol in @code{c-offsets-alist}. Let's say it
5108 finds the value @samp{4}. It's added with the base column and yields an
5109 indentation for the line of 8 spaces.
5110
5111 Simple, huh?
5112
5113 Actually, it's a bit more complicated than that since the entries on
5114 @code{c-offsets-alist} can be much more than plain offsets.
5115 @xref{c-offsets-alist}, for the full story.
5116
5117 Anyway, the mode usually just does The Right Thing without you having to
5118 think about it in this much detail. But when customizing indentation,
5119 it's helpful to understand the general indentation model being used.
5120
5121 As you configure @ccmode{}, you might want to set the variable
5122 @code{c-echo-syntactic-information-p} to non-@code{nil} so that the
5123 syntactic context and calculated offset always is echoed in the
5124 minibuffer when you hit @kbd{TAB}.
5125
5126
5127 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5128 @node Customizing Indentation, Custom Macros, Indentation Engine Basics, Top
5129 @comment node-name, next, previous, up
5130 @chapter Customizing Indentation
5131 @cindex customization, indentation
5132 @cindex indentation
5133 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5134
5135 The principal variable for customizing indentation is the style
5136 variable @code{c-offsets-alist}, which gives an @dfn{offset} (an
5137 indentation rule) for each syntactic symbol. Its structure and
5138 semantics are completely described in @ref{c-offsets-alist}. The
5139 various ways you can set the variable, including the use of the
5140 @ccmode{} style system, are described in @ref{Config Basics} and its
5141 sections, in particular @ref{Style Variables}.
5142
5143 The simplest and most used kind of ``offset'' setting in
5144 @code{c-offsets-alist} is in terms of multiples of
5145 @code{c-basic-offset}:
5146
5147 @defopt c-basic-offset
5148 @vindex basic-offset (c-)
5149 This style variable holds the basic offset between indentation levels.
5150 It's factory default is 4, but all the built-in styles set it
5151 themselves, to some value between 2 (for @code{gnu} style) and 8 (for
5152 @code{bsd}, @code{linux}, and @code{python} styles).
5153 @end defopt
5154
5155 The most flexible ``offset'' setting you can make in
5156 @code{c-offsets-alist} is a line-up function (or even a list of them),
5157 either one supplied by @ccmode{} (@pxref{Line-Up Functions}) or one
5158 you write yourself (@pxref{Custom Line-Up}).
5159
5160 Finally, in @ref{Other Indentation} you'll find the tool of last
5161 resort: a hook which is called after a line has been indented. You
5162 can install functions here to make ad-hoc adjustments to any line's
5163 indentation.
5164
5165 @menu
5166 * c-offsets-alist::
5167 * Interactive Customization::
5168 * Line-Up Functions::
5169 * Custom Line-Up::
5170 * Other Indentation::
5171 @end menu
5172
5173
5174 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5175 @node c-offsets-alist, Interactive Customization, Customizing Indentation, Customizing Indentation
5176 @comment node-name, next, previous, up
5177 @section c-offsets-alist
5178 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5179
5180 This section explains the structure and semantics of the style
5181 variable @code{c-offset-alist}, the principal variable for configuring
5182 indentation. Details of how to set it up, and its relationship to
5183 @ccmode{}'s style system are given in @ref{Style Variables}.
5184
5185 @defopt c-offsets-alist
5186 @vindex offsets-alist (c-)
5187 This is an alist which associates an offset with each syntactic
5188 symbol. This @dfn{offset} is a rule specifying how to indent a line
5189 whose syntactic context matches the symbol. @xref{Syntactic
5190 Analysis}.
5191
5192 Note that the buffer-local binding of this alist in a @ccmode{} buffer
5193 contains an entry for @emph{every} syntactic symbol. Its global
5194 binding and its settings within style specifications usually contain
5195 only a few entries. @xref{Style Variables}.
5196
5197 The offset specification associated with any particular syntactic
5198 symbol can be an integer, a variable name, a vector, a function or
5199 lambda expression, a list, or one of the following special symbols:
5200 @code{+}, @code{-}, @code{++}, @code{--}, @code{*}, or @code{/}. The
5201 meanings of these values are described in detail below.
5202
5203 Here is an example fragment of a @code{c-offsets-alist}, showing some
5204 of these kinds of offsets:
5205
5206 @example
5207 ((statement . 0)
5208 (substatement . +)
5209 (cpp-macro . [0])
5210 (topmost-intro-cont . c-lineup-topmost-intro-cont)
5211 (statement-block-intro . (add c-lineup-whitesmith-in-block
5212 c-indent-multi-line-block))
5213 @dots{}
5214 @*)
5215 @end example
5216 @end defopt
5217
5218 @deffn Command c-set-offset (@kbd{C-c C-o})
5219 @findex set-offset (c-)
5220 @kindex C-c C-o
5221 This command changes the entry for a syntactic symbol in the current
5222 binding of @code{c-offsets-alist}, or it inserts a new entry if there
5223 isn't already one for that syntactic symbol.
5224
5225 You can use @code{c-set-offsets} interactively within a @ccmode{}
5226 buffer to make experimental changes to your indentation settings.
5227 @kbd{C-c C-o} prompts you for the syntactic symbol to change
5228 (defaulting to that of the current line) and the new offset
5229 (defaulting to the current offset).
5230
5231 @code{c-set-offsets} takes two arguments when used programmatically:
5232 @var{symbol}, the syntactic element symbol to change and @var{offset},
5233 the new offset for that syntactic element. You can call the command
5234 in your @file{.emacs} to change the global binding of
5235 @code{c-offsets-alist} (@pxref{Style Variables}); you can use it in a
5236 hook function to make changes from the current style. @ccmode{}
5237 itself uses this function when initializing styles.
5238 @end deffn
5239
5240 @cindex offset specification
5241 The ``offset specifications'' in @code{c-offsets-alist} can be any of
5242 the following:
5243
5244 @table @asis
5245 @item An integer
5246 The integer specifies a relative offset. All relative
5247 offsets@footnote{The syntactic context @code{@w{((defun-block-intro
5248 2724) (comment-intro))}} would likely have two relative offsets.} will
5249 be added together and used to calculate the indentation relative to an
5250 anchor position earlier in the buffer. @xref{Indentation
5251 Calculation}, for details. Most of the time, it's probably better to
5252 use one of the special symbols like @code{+} than an integer (apart
5253 from zero).
5254
5255 @item One of the symbols @code{+}, @code{-}, @code{++}, @code{--}, @code{*}, or @code{/}
5256 These special symbols describe a relative offset in multiples of
5257 @code{c-basic-offset}:
5258
5259 By defining a style's indentation in terms of @code{c-basic-offset},
5260 you can change the amount of whitespace given to an indentation level
5261 while maintaining the same basic shape of your code. Here are the
5262 values that the special symbols correspond to:
5263
5264 @table @code
5265 @item +
5266 @code{c-basic-offset} times 1
5267 @item -
5268 @code{c-basic-offset} times -1
5269 @item ++
5270 @code{c-basic-offset} times 2
5271 @item --
5272 @code{c-basic-offset} times -2
5273 @item *
5274 @code{c-basic-offset} times 0.5
5275 @item /
5276 @code{c-basic-offset} times -0.5
5277 @end table
5278
5279 @item A vector
5280 The first element of the vector, an integer, sets the absolute
5281 indentation column. This will override any previously calculated
5282 indentation, but won't override relative indentation calculated from
5283 syntactic elements later on in the syntactic context of the line being
5284 indented. @xref{Indentation Calculation}. Any elements in the vector
5285 beyond the first will be ignored.
5286
5287 @item A function or lambda expression
5288 The function will be called and its return value will in turn be
5289 evaluated as an offset specification. Functions are useful when more
5290 context than just the syntactic symbol is needed to get the desired
5291 indentation. @xref{Line-Up Functions}, and @ref{Custom Line-Up}, for
5292 details about them.
5293
5294 @item A symbol with a variable binding
5295 If the symbol also has a function binding, the function takes
5296 precedence over the variable. Otherwise the value of the variable is
5297 used. It must be an integer (which is used as relative offset) or a
5298 vector (an absolute offset).
5299
5300 @item A list
5301 The offset can also be a list containing several offset
5302 specifications; these are evaluated recursively and combined. A list
5303 is typically only useful when some of the offsets are line-up
5304 functions. A common strategy is calling a sequence of functions in
5305 turn until one of them recognizes that it is appropriate for the
5306 source line and returns a non-@code{nil} value.
5307
5308 @code{nil} values are always ignored when the offsets are combined.
5309 The first element of the list specifies the method of combining the
5310 non-@code{nil} offsets from the remaining elements:
5311
5312 @table @code
5313 @item first
5314 Use the first offset that doesn't evaluate to @code{nil}. Subsequent
5315 elements of the list don't get evaluated.
5316 @item min
5317 Use the minimum of all the offsets. All must be either relative or
5318 absolute - they can't be mixed.
5319 @item max
5320 Use the maximum of all the offsets. All must be either relative or
5321 absolute - they can't be mixed.
5322 @item add
5323 Add all the evaluated offsets together. Exactly one of them may be
5324 absolute, in which case the result is absolute. Any relative offsets
5325 that preceded the absolute one in the list will be ignored in that case.
5326 @end table
5327
5328 As a compatibility measure, if the first element is none of the above
5329 then it too will be taken as an offset specification and the whole list
5330 will be combined according to the method @code{first}.
5331 @end table
5332
5333 @vindex c-strict-syntax-p
5334 @vindex strict-syntax-p (c-)
5335 If an offset specification evaluates to @code{nil}, then a relative
5336 offset of 0 (zero) is used@footnote{There is however a variable
5337 @code{c-strict-syntax-p} that when set to non-@code{nil} will cause an
5338 error to be signaled in that case. It's now considered obsolete since
5339 it doesn't work well with some of the alignment functions that return
5340 @code{nil} instead of zero. You should therefore leave
5341 @code{c-strict-syntax-p} set to @code{nil}.}.
5342
5343 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5344 @node Interactive Customization, Line-Up Functions, c-offsets-alist, Customizing Indentation
5345 @comment node-name, next, previous, up
5346 @section Interactive Customization
5347 @cindex customization, interactive
5348 @cindex interactive customization
5349 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5350
5351 As an example of how to customize indentation, let's change the
5352 style of this example@footnote{In this and subsequent examples, the
5353 original code is formatted using the @samp{gnu} style unless otherwise
5354 indicated. @xref{Styles}.}:
5355
5356 @example
5357 @group
5358 1: int add( int val, int incr, int doit )
5359 2: @{
5360 3: if( doit )
5361 4: @{
5362 5: return( val + incr );
5363 6: @}
5364 7: return( val );
5365 8: @}
5366 @end group
5367 @end example
5368
5369 @noindent
5370 to:
5371
5372 @example
5373 @group
5374 1: int add( int val, int incr, int doit )
5375 2: @{
5376 3: if( doit )
5377 4: @{
5378 5: return( val + incr );
5379 6: @}
5380 7: return( val );
5381 8: @}
5382 @end group
5383 @end example
5384
5385 In other words, we want to change the indentation of braces that open a
5386 block following a condition so that the braces line up under the
5387 conditional, instead of being indented. Notice that the construct we
5388 want to change starts on line 4. To change the indentation of a line,
5389 we need to see which syntactic symbols affect the offset calculations
5390 for that line. Hitting @kbd{C-c C-s} on line 4 yields:
5391
5392 @example
5393 ((substatement-open 44))
5394 @end example
5395
5396 @noindent
5397 so we know that to change the offset of the open brace, we need to
5398 change the indentation for the @code{substatement-open} syntactic
5399 symbol.
5400
5401 To do this interactively, just hit @kbd{C-c C-o}. This prompts
5402 you for the syntactic symbol to change, providing a reasonable default.
5403 In this case, the default is @code{substatement-open}, which is just the
5404 syntactic symbol we want to change!
5405
5406 After you hit return, @ccmode{} will then prompt you for the new
5407 offset value, with the old value as the default. The default in this
5408 case is @samp{+}, but we want no extra indentation so enter
5409 @samp{0} and @kbd{RET}. This will associate the offset 0 with the
5410 syntactic symbol @code{substatement-open}.
5411
5412 To check your changes quickly, just hit @kbd{C-c C-q}
5413 (@code{c-indent-defun}) to reindent the entire function. The example
5414 should now look like:
5415
5416 @example
5417 @group
5418 1: int add( int val, int incr, int doit )
5419 2: @{
5420 3: if( doit )
5421 4: @{
5422 5: return( val + incr );
5423 6: @}
5424 7: return( val );
5425 8: @}
5426 @end group
5427 @end example
5428
5429 Notice how just changing the open brace offset on line 4 is all we
5430 needed to do. Since the other affected lines are indented relative to
5431 line 4, they are automatically indented the way you'd expect. For more
5432 complicated examples, this might not always work. The general approach
5433 to take is to always start adjusting offsets for lines higher up in the
5434 file, then reindent and see if any following lines need further
5435 adjustments.
5436
5437 @c Move this bit to "Styles" (2005/10/7)
5438 @deffn Command c-set-offset symbol offset
5439 @findex set-offset (c-)
5440 @kindex C-c C-o
5441 This is the command bound to @kbd{C-c C-o}. It provides a convenient
5442 way to set offsets on @code{c-offsets-alist} both interactively (see
5443 the example above) and from your mode hook.
5444
5445 It takes two arguments when used programmatically: @var{symbol} is the
5446 syntactic element symbol to change and @var{offset} is the new offset
5447 for that syntactic element.
5448 @end deffn
5449 @c End of MOVE THIS BIT.
5450
5451 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5452 @node Line-Up Functions, Custom Line-Up, Interactive Customization, Customizing Indentation
5453 @comment node-name, next, previous, up
5454 @section Line-Up Functions
5455 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5456
5457 @cindex line-up function
5458 @cindex indentation function
5459 Often there are cases when a simple offset setting on a syntactic
5460 symbol isn't enough to get the desired indentation---for example, you
5461 might want to line up a closing parenthesis with the matching opening
5462 one rather than indenting relative to its ``anchor point''. @ccmode{}
5463 provides this flexibility with @dfn{line-up functions}.
5464
5465 The way you associate a line-up function with a syntactic symbol is
5466 described in @ref{c-offsets-alist}. @ccmode{} comes with many
5467 predefined line-up functions for common situations. If none of these
5468 does what you want, you can write your own. @xref{Custom Line-Up}.
5469 Sometimes, it is easier to tweak the standard indentation by adding a
5470 function to @code{c-special-indent-hook} (@pxref{Other Indentation}).
5471
5472 The line-up functions haven't been adapted for AWK buffers or tested
5473 with them. Some of them might work serendipitously. There shouldn't be
5474 any problems writing custom line-up functions for AWK mode.
5475
5476 The calling convention for line-up functions is described fully in
5477 @ref{Custom Line-Up}. Roughly speaking, the return value is either an
5478 offset itself (such as @code{+} or @code{[0]}) or it's @code{nil},
5479 meaning ``this function is inappropriate in this case - try a
5480 different one''. @xref{c-offsets-alist}.
5481
5482 The subsections below describe all the standard line-up functions,
5483 categorized by the sort of token the lining-up centers around. For
5484 each of these functions there is a ``works with'' list that indicates
5485 which syntactic symbols the function is intended to be used with.
5486
5487 @macro workswith
5488 @emph{Works with:@ }
5489 @end macro
5490 @ifinfo
5491 @unmacro workswith
5492 @macro workswith
5493 Works with:
5494 @end macro
5495 @end ifinfo
5496
5497 @macro sssTBasicOffset
5498 <--> @i{c-basic-offset}@c
5499 @end macro
5500
5501 @macro sssTsssTBasicOffset
5502 <--><--> @i{c-basic-offset}@c
5503 @end macro
5504
5505 @macro hereFn{func}
5506 <- @i{\func\}@c
5507 @end macro
5508
5509 @c The TeX backend seems to insert extra spaces around the argument. :P
5510 @iftex
5511 @unmacro hereFn
5512 @macro hereFn{func}
5513 <-@i{\func\}@c
5514 @end macro
5515 @end iftex
5516
5517 @menu
5518 * Brace/Paren Line-Up::
5519 * List Line-Up::
5520 * Operator Line-Up::
5521 * Comment Line-Up::
5522 * Misc Line-Up::
5523 @end menu
5524
5525 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5526 @node Brace/Paren Line-Up, List Line-Up, Line-Up Functions, Line-Up Functions
5527 @comment node-name, next, previous, up
5528 @subsection Brace and Parenthesis Line-Up Functions
5529 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5530
5531 The line-up functions here calculate the indentation for braces,
5532 parentheses and statements within brace blocks.
5533
5534 @defun c-lineup-close-paren
5535 @findex lineup-close-paren (c-)
5536 Line up the closing paren under its corresponding open paren if the
5537 open paren is followed by code. If the open paren ends its line, no
5538 indentation is added. E.g:
5539
5540 @example
5541 @group
5542 main (int,
5543 char **
5544 ) @hereFn{c-lineup-close-paren}
5545 @end group
5546 @end example
5547
5548 @noindent
5549 and
5550
5551 @example
5552 @group
5553 main (
5554 int, char **
5555 ) @hereFn{c-lineup-close-paren}
5556 @end group
5557 @end example
5558
5559 As a special case, if a brace block is opened at the same line as the
5560 open parenthesis of the argument list, the indentation is
5561 @code{c-basic-offset} instead of the open paren column. See
5562 @code{c-lineup-arglist} for further discussion of this ``DWIM'' measure.
5563
5564 @workswith All @code{*-close} symbols.
5565 @end defun
5566
5567 @comment ------------------------------------------------------------
5568
5569 @anchor{c-lineup-arglist-close-under-paren}
5570 @defun c-lineup-arglist-close-under-paren
5571 @findex lineup-arglist-close-under-paren (c-)
5572 Set your @code{arglist-close} syntactic symbol to this line-up function
5573 so that parentheses that close argument lists will line up under the
5574 parenthesis that opened the argument list. It can also be used with
5575 @code{arglist-cont} and @code{arglist-cont-nonempty} to line up all
5576 lines inside a parenthesis under the open paren.
5577
5578 As a special case, if a brace block is opened at the same line as the
5579 open parenthesis of the argument list, the indentation is
5580 @code{c-basic-offset} only. See @code{c-lineup-arglist} for further
5581 discussion of this ``DWIM'' measure.
5582
5583 @workswith Almost all symbols, but are typically most useful on
5584 @code{arglist-close}, @code{brace-list-close}, @code{arglist-cont} and
5585 @code{arglist-cont-nonempty}.
5586 @end defun
5587
5588 @comment ------------------------------------------------------------
5589
5590 @defun c-indent-one-line-block
5591 @findex indent-one-line-block (c-)
5592 Indent a one line block @code{c-basic-offset} extra. E.g:
5593
5594 @example
5595 @group
5596 if (n > 0)
5597 @{m+=n; n=0;@} @hereFn{c-indent-one-line-block}
5598 @sssTBasicOffset{}
5599 @end group
5600 @end example
5601
5602 @noindent
5603 and
5604
5605 @example
5606 @group
5607 if (n > 0)
5608 @{ @hereFn{c-indent-one-line-block}
5609 m+=n; n=0;
5610 @}
5611 @end group
5612 @end example
5613
5614 The block may be surrounded by any kind of parenthesis characters.
5615 @code{nil} is returned if the line doesn't start with a one line block,
5616 which makes the function usable in list expressions.
5617
5618 @workswith Almost all syntactic symbols, but most useful on the
5619 @code{-open} symbols.
5620 @end defun
5621
5622 @comment ------------------------------------------------------------
5623
5624 @defun c-indent-multi-line-block
5625 @findex indent-multi-line-block (c-)
5626 Indent a multiline block @code{c-basic-offset} extra. E.g:
5627
5628 @example
5629 @group
5630 int *foo[] = @{
5631 NULL,
5632 @{17@}, @hereFn{c-indent-multi-line-block}
5633 @end group
5634 @end example
5635
5636 @noindent
5637 and
5638
5639 @example
5640 @group
5641 int *foo[] = @{
5642 NULL,
5643 @{ @hereFn{c-indent-multi-line-block}
5644 17
5645 @},
5646 @sssTBasicOffset{}
5647 @end group
5648 @end example
5649
5650 The block may be surrounded by any kind of parenthesis characters.
5651 @code{nil} is returned if the line doesn't start with a multiline
5652 block, which makes the function usable in list expressions.
5653
5654 @workswith Almost all syntactic symbols, but most useful on the
5655 @code{-open} symbols.
5656 @end defun
5657
5658 @comment ------------------------------------------------------------
5659
5660 @defun c-lineup-runin-statements
5661 @findex lineup-runin-statements (c-)
5662 Line up statements for coding standards which place the first statement
5663 in a block on the same line as the block opening brace@footnote{Run-in
5664 style doesn't really work too well. You might need to write your own
5665 custom line-up functions to better support this style.}. E.g:
5666
5667 @example
5668 @group
5669 int main()
5670 @{ puts ("Hello!");
5671 return 0; @hereFn{c-lineup-runin-statements}
5672 @}
5673 @end group
5674 @end example
5675
5676 If there is no statement after the opening brace to align with,
5677 @code{nil} is returned. This makes the function usable in list
5678 expressions.
5679
5680 @workswith The @code{statement} syntactic symbol.
5681 @end defun
5682
5683 @comment ------------------------------------------------------------
5684
5685 @defun c-lineup-inexpr-block
5686 @findex lineup-inexpr-block (c-)
5687 This can be used with the in-expression block symbols to indent the
5688 whole block to the column where the construct is started. E.g. for Java
5689 anonymous classes, this lines up the class under the @samp{new} keyword,
5690 and in Pike it lines up the lambda function body under the @samp{lambda}
5691 keyword. Returns @code{nil} if the block isn't part of such a
5692 construct.
5693
5694 @workswith @code{inlambda}, @code{inexpr-statement},
5695 @code{inexpr-class}.
5696 @end defun
5697
5698 @comment ------------------------------------------------------------
5699
5700 @defun c-lineup-after-whitesmith-blocks
5701 @findex lineup-after-whitesmith-blocks (c-)
5702 Compensate for Whitesmith style indentation of blocks. Due to the way
5703 @ccmode{} calculates anchor positions for normal lines inside blocks,
5704 this function is necessary for those lines to get correct Whitesmith
5705 style indentation. Consider the following examples:
5706
5707 @example
5708 @group
5709 int foo()
5710 @{
5711 a;
5712 x; @hereFn{c-lineup-after-whitesmith-blocks}
5713 @end group
5714 @end example
5715
5716 @example
5717 @group
5718 int foo()
5719 @{
5720 @{
5721 a;
5722 @}
5723 x; @hereFn{c-lineup-after-whitesmith-blocks}
5724 @end group
5725 @end example
5726
5727 The fact that the line with @code{x} is preceded by a Whitesmith style
5728 indented block in the latter case and not the first should not affect
5729 its indentation. But since CC Mode in cases like this uses the
5730 indentation of the preceding statement as anchor position, the @code{x}
5731 would in the second case be indented too much if the offset for
5732 @code{statement} was set simply to zero.
5733
5734 This lineup function corrects for this situation by detecting if the
5735 anchor position is at an open paren character. In that case, it instead
5736 indents relative to the surrounding block just like
5737 @code{c-lineup-whitesmith-in-block}.
5738
5739 @workswith @code{brace-list-entry}, @code{brace-entry-open},
5740 @code{statement}, @code{arglist-cont}.
5741 @end defun
5742
5743 @comment ------------------------------------------------------------
5744
5745 @defun c-lineup-whitesmith-in-block
5746 @findex lineup-whitesmith-in-block (c-)
5747 Line up lines inside a block in Whitesmith style. It's done in a way
5748 that works both when the opening brace hangs and when it doesn't. E.g:
5749
5750 @example
5751 @group
5752 something
5753 @{
5754 foo; @hereFn{c-lineup-whitesmith-in-block}
5755 @}
5756 @end group
5757 @end example
5758
5759 @noindent
5760 and
5761
5762 @example
5763 @group
5764 something @{
5765 foo; @hereFn{c-lineup-whitesmith-in-block}
5766 @}
5767 @sssTBasicOffset{}
5768 @end group
5769 @end example
5770
5771 In the first case the indentation is kept unchanged, in the second
5772 @code{c-basic-offset} is added.
5773
5774 @workswith @code{defun-close}, @code{defun-block-intro},
5775 @code{inline-close}, @code{block-close}, @code{brace-list-close},
5776 @code{brace-list-intro}, @code{statement-block-intro},
5777 @code{arglist-intro}, @code{arglist-cont-nonempty},
5778 @code{arglist-close}, and all @code{in*} symbols, e.g. @code{inclass}
5779 and @code{inextern-lang}.
5780 @end defun
5781
5782 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5783 @node List Line-Up, Operator Line-Up, Brace/Paren Line-Up, Line-Up Functions
5784 @comment node-name, next, previous, up
5785 @subsection List Line-Up Functions
5786 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5787
5788 The line-up functions here calculate the indentation for lines which
5789 form lists of items, usually separated by commas.
5790
5791 The function @ref{c-lineup-arglist-close-under-paren}, which is mainly
5792 for indenting a close parenthesis, is also useful for the lines
5793 contained within parentheses.
5794
5795 @defun c-lineup-arglist
5796 @findex lineup-arglist (c-)
5797 Line up the current argument line under the first argument.
5798
5799 As a special case, if an argument on the same line as the open
5800 parenthesis starts with a brace block opener, the indentation is
5801 @code{c-basic-offset} only. This is intended as a ``DWIM'' measure in
5802 cases like macros that contain statement blocks, e.g:
5803
5804 @example
5805 @group
5806 A_VERY_LONG_MACRO_NAME (@{
5807 some (code, with + long, lines * in[it]);
5808 @});
5809 @sssTBasicOffset{}
5810 @end group
5811 @end example
5812
5813 This is motivated partly because it's more in line with how code
5814 blocks are handled, and partly since it approximates the behavior of
5815 earlier CC Mode versions, which due to inaccurate analysis tended to
5816 indent such cases this way.
5817
5818 @workswith @code{arglist-cont-nonempty}, @code{arglist-close}.
5819 @end defun
5820
5821 @comment ------------------------------------------------------------
5822
5823 @defun c-lineup-arglist-intro-after-paren
5824 @findex lineup-arglist-intro-after-paren (c-)
5825 Line up a line to just after the open paren of the surrounding paren or
5826 brace block.
5827
5828 @workswith @code{defun-block-intro}, @code{brace-list-intro},
5829 @code{statement-block-intro}, @code{statement-case-intro},
5830 @code{arglist-intro}.
5831 @end defun
5832
5833 @comment ------------------------------------------------------------
5834
5835 @defun c-lineup-multi-inher
5836 @findex lineup-multi-inher (c-)
5837 Line up the classes in C++ multiple inheritance clauses and member
5838 initializers under each other. E.g:
5839
5840 @example
5841 @group
5842 Foo::Foo (int a, int b):
5843 Cyphr (a),
5844 Bar (b) @hereFn{c-lineup-multi-inher}
5845 @end group
5846 @end example
5847
5848 @noindent
5849 and
5850
5851 @example
5852 @group
5853 class Foo
5854 : public Cyphr,
5855 public Bar @hereFn{c-lineup-multi-inher}
5856 @end group
5857 @end example
5858
5859 @noindent
5860 and
5861
5862 @example
5863 @group
5864 Foo::Foo (int a, int b)
5865 : Cyphr (a)
5866 , Bar (b) @hereFn{c-lineup-multi-inher}
5867 @end group
5868 @end example
5869
5870 @workswith @code{inher-cont}, @code{member-init-cont}.
5871 @end defun
5872
5873 @comment ------------------------------------------------------------
5874
5875 @defun c-lineup-java-inher
5876 @findex lineup-java-inher (c-)
5877 Line up Java implements and extends declarations. If class names
5878 follow on the same line as the @samp{implements}/@samp{extends}
5879 keyword, they are lined up under each other. Otherwise, they are
5880 indented by adding @code{c-basic-offset} to the column of the keyword.
5881 E.g:
5882
5883 @example
5884 @group
5885 class Foo
5886 extends
5887 Bar @hereFn{c-lineup-java-inher}
5888 @sssTBasicOffset{}
5889 @end group
5890 @end example
5891
5892 @noindent
5893 and
5894
5895 @example
5896 @group
5897 class Foo
5898 extends Cyphr,
5899 Bar @hereFn{c-lineup-java-inher}
5900 @end group
5901 @end example
5902
5903 @workswith @code{inher-cont}.
5904 @end defun
5905
5906 @comment ------------------------------------------------------------
5907
5908 @defun c-lineup-java-throws
5909 @findex lineup-java-throws (c-)
5910 Line up Java throws declarations. If exception names follow on the
5911 same line as the throws keyword, they are lined up under each other.
5912 Otherwise, they are indented by adding @code{c-basic-offset} to the
5913 column of the @samp{throws} keyword. The @samp{throws} keyword itself
5914 is also indented by @code{c-basic-offset} from the function declaration
5915 start if it doesn't hang. E.g:
5916
5917 @example
5918 @group
5919 int foo()
5920 throws @hereFn{c-lineup-java-throws}
5921 Bar @hereFn{c-lineup-java-throws}
5922 @sssTsssTBasicOffset{}
5923 @end group
5924 @end example
5925
5926 @noindent
5927 and
5928
5929 @example
5930 @group
5931 int foo() throws Cyphr,
5932 Bar, @hereFn{c-lineup-java-throws}
5933 Vlod @hereFn{c-lineup-java-throws}
5934 @end group
5935 @end example
5936
5937 @workswith @code{func-decl-cont}.
5938 @end defun
5939
5940 @comment ------------------------------------------------------------
5941
5942 @defun c-lineup-template-args
5943 @findex lineup-template-args (c-)
5944 Line up the arguments of a template argument list under each other, but
5945 only in the case where the first argument is on the same line as the
5946 opening @samp{<}.
5947
5948 To allow this function to be used in a list expression, @code{nil} is
5949 returned if there's no template argument on the first line.
5950
5951 @workswith @code{template-args-cont}.
5952 @end defun
5953
5954 @comment ------------------------------------------------------------
5955
5956 @defun c-lineup-ObjC-method-call
5957 @findex lineup-ObjC-method-call (c-)
5958 For Objective-C code, line up selector args as Emacs Lisp mode does
5959 with function args: go to the position right after the message receiver,
5960 and if you are at the end of the line, indent the current line
5961 c-basic-offset columns from the opening bracket; otherwise you are
5962 looking at the first character of the first method call argument, so
5963 lineup the current line with it.
5964
5965 @workswith @code{objc-method-call-cont}.
5966 @end defun
5967
5968 @comment ------------------------------------------------------------
5969
5970 @defun c-lineup-ObjC-method-args
5971 @findex lineup-ObjC-method-args (c-)
5972 For Objective-C code, line up the colons that separate args. The colon
5973 on the current line is aligned with the one on the first line.
5974
5975 @workswith @code{objc-method-args-cont}.
5976 @end defun
5977
5978 @comment ------------------------------------------------------------
5979
5980 @defun c-lineup-ObjC-method-args-2
5981 @findex lineup-ObjC-method-args-2 (c-)
5982 Similar to @code{c-lineup-ObjC-method-args} but lines up the colon on
5983 the current line with the colon on the previous line.
5984
5985 @workswith @code{objc-method-args-cont}.
5986 @end defun
5987
5988 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5989 @node Operator Line-Up, Comment Line-Up, List Line-Up, Line-Up Functions
5990 @comment node-name, next, previous, up
5991 @subsection Operator Line-Up Functions
5992 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5993
5994 The line-up functions here calculate the indentation for lines which
5995 start with an operator, by lining it up with something on the previous
5996 line.
5997
5998 @defun c-lineup-argcont
5999 @findex lineup-argcont (c-)
6000 Line up a continued argument. E.g:
6001
6002 @example
6003 @group
6004 foo (xyz, aaa + bbb + ccc
6005 + ddd + eee + fff); @hereFn{c-lineup-argcont}
6006 @end group
6007 @end example
6008
6009 Only continuation lines like this are touched, @code{nil} is returned on
6010 lines which are the start of an argument.
6011
6012 Within a gcc @code{asm} block, @code{:} is recognized as an argument
6013 separator, but of course only between operand specifications, not in the
6014 expressions for the operands.
6015
6016 @workswith @code{arglist-cont}, @code{arglist-cont-nonempty}.
6017 @end defun
6018
6019 @comment ------------------------------------------------------------
6020
6021 @defun c-lineup-arglist-operators
6022 @findex lineup-arglist-operators (c-)
6023 Line up lines starting with an infix operator under the open paren.
6024 Return @code{nil} on lines that don't start with an operator, to leave
6025 those cases to other line-up functions. Example:
6026
6027 @example
6028 @group
6029 if ( x < 10
6030 || at_limit (x, @hereFn{c-lineup-arglist-operators}
6031 list) @hereFn{c-lineup-arglist-operators@r{ returns nil}}
6032 )
6033 @end group
6034 @end example
6035
6036 Since this function doesn't do anything for lines without an infix
6037 operator you typically want to use it together with some other lineup
6038 settings, e.g. as follows (the @code{arglist-close} setting is just a
6039 suggestion to get a consistent style):
6040
6041 @example
6042 (c-set-offset 'arglist-cont
6043 '(c-lineup-arglist-operators 0))
6044 (c-set-offset 'arglist-cont-nonempty
6045 '(c-lineup-arglist-operators c-lineup-arglist))
6046 (c-set-offset 'arglist-close
6047 '(c-lineup-arglist-close-under-paren))
6048 @end example
6049
6050 @workswith @code{arglist-cont}, @code{arglist-cont-nonempty}.
6051 @end defun
6052
6053 @comment ------------------------------------------------------------
6054
6055 @defun c-lineup-assignments
6056 @findex lineup-assignments (c-)
6057 Line up the current line after the assignment operator on the first line
6058 in the statement. If there isn't any, return nil to allow stacking with
6059 other line-up functions. If the current line contains an assignment
6060 operator too, try to align it with the first one.
6061
6062 @workswith @code{topmost-intro-cont}, @code{statement-cont},
6063 @code{arglist-cont}, @code{arglist-cont-nonempty}.
6064
6065 @end defun
6066
6067 @comment ------------------------------------------------------------
6068
6069 @defun c-lineup-math
6070 @findex lineup-math (c-)
6071 Like @code{c-lineup-assignments} but indent with @code{c-basic-offset}
6072 if no assignment operator was found on the first line. I.e. this
6073 function is the same as specifying a list @code{(c-lineup-assignments
6074 +)}. It's provided for compatibility with old configurations.
6075
6076 @workswith @code{topmost-intro-cont}, @code{statement-cont},
6077 @code{arglist-cont}, @code{arglist-cont-nonempty}.
6078 @end defun
6079
6080 @comment ------------------------------------------------------------
6081
6082 @defun c-lineup-cascaded-calls
6083 @findex lineup-cascaded-calls (c-)
6084 Line up ``cascaded calls'' under each other. If the line begins with
6085 @code{->} or @code{.} and the preceding line ends with one or more
6086 function calls preceded by the same token, then the arrow is lined up
6087 with the first of those tokens. E.g:
6088
6089 @example
6090 @group
6091 r = proc->add(17)->add(18)
6092 ->add(19) + @hereFn{c-lineup-cascaded-calls}
6093 offset; @hereFn{c-lineup-cascaded-calls@r{ (inactive)}}
6094 @end group
6095 @end example
6096
6097 In any other situation @code{nil} is returned to allow use in list
6098 expressions.
6099
6100 @workswith @code{topmost-intro-cont}, @code{statement-cont},
6101 @code{arglist-cont}, @code{arglist-cont-nonempty}.
6102 @end defun
6103
6104 @comment ------------------------------------------------------------
6105
6106 @defun c-lineup-streamop
6107 @findex lineup-streamop (c-)
6108 Line up C++ stream operators (i.e. @samp{<<} and @samp{>>}).
6109
6110 @workswith @code{stream-op}.
6111 @end defun
6112
6113 @comment ------------------------------------------------------------
6114
6115 @defun c-lineup-string-cont
6116 @findex lineup-string-cont (c-)
6117 Line up a continued string under the one it continues. A continued
6118 string in this sense is where a string literal follows directly after
6119 another one. E.g:
6120
6121 @example
6122 @group
6123 result = prefix + "A message "
6124 "string."; @hereFn{c-lineup-string-cont}
6125 @end group
6126 @end example
6127
6128 @code{nil} is returned in other situations, to allow stacking with other
6129 lineup functions.
6130
6131 @workswith @code{topmost-intro-cont}, @code{statement-cont},
6132 @code{arglist-cont}, @code{arglist-cont-nonempty}.
6133 @end defun
6134
6135
6136 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
6137 @node Comment Line-Up, Misc Line-Up, Operator Line-Up, Line-Up Functions
6138 @comment node-name, next, previous, up
6139 @subsection Comment Line-Up Functions
6140 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
6141
6142 The lineup functions here calculate the indentation for several types
6143 of comment structure.
6144
6145 @defun c-lineup-C-comments
6146 @findex lineup-C-comments (c-)
6147 Line up C block comment continuation lines. Various heuristics are used
6148 to handle most of the common comment styles. Some examples:
6149
6150 @example
6151 @group
6152 /* /** /*
6153 * text * text text
6154 */ */ */
6155 @end group
6156 @end example
6157
6158 @example
6159 @group
6160 /* text /* /**
6161 text ** text ** text
6162 */ */ */
6163 @end group
6164 @end example
6165
6166 @example
6167 @group
6168 /**************************************************
6169 * text
6170 *************************************************/
6171 @end group
6172 @end example
6173
6174 @vindex comment-start-skip
6175 @example
6176 @group
6177 /**************************************************
6178 Free form text comments:
6179 In comments with a long delimiter line at the
6180 start, the indentation is kept unchanged for lines
6181 that start with an empty comment line prefix. The
6182 delimiter line is whatever matches the
6183 @code{comment-start-skip} regexp.
6184 **************************************************/
6185 @end group
6186 @end example
6187
6188 The style variable @code{c-comment-prefix-regexp} is used to recognize
6189 the comment line prefix, e.g. the @samp{*} that usually starts every
6190 line inside a comment.
6191
6192 @workswith The @code{c} syntactic symbol.
6193 @end defun
6194
6195 @comment ------------------------------------------------------------
6196
6197 @defun c-lineup-comment
6198 @findex lineup-comment (c-)
6199 Line up a comment-only line according to the style variable
6200 @code{c-comment-only-line-offset}. If the comment is lined up with a
6201 comment starter on the previous line, that alignment is preserved.
6202
6203 @defopt c-comment-only-line-offset
6204 @vindex comment-only-line-offset (c-)
6205 This style variable specifies the extra offset for the line. It can
6206 contain an integer or a cons cell of the form
6207
6208 @example
6209 (@r{@var{non-anchored-offset}} . @r{@var{anchored-offset}})
6210 @end example
6211
6212 @noindent
6213 where @var{non-anchored-offset} is the amount of offset given to
6214 non-column-zero anchored lines, and @var{anchored-offset} is the amount
6215 of offset to give column-zero anchored lines. Just an integer as value
6216 is equivalent to @code{(@r{@var{value}} . -1000)}.
6217 @end defopt
6218
6219 @workswith @code{comment-intro}.
6220 @end defun
6221
6222 @comment ------------------------------------------------------------
6223
6224 @defun c-lineup-knr-region-comment
6225 @findex lineup-knr-region-comment (c-)
6226 Line up a comment in the ``K&R region'' with the declaration. That is
6227 the region between the function or class header and the beginning of the
6228 block. E.g:
6229
6230 @example
6231 @group
6232 int main()
6233 /* Called at startup. */ @hereFn{c-lineup-knr-region-comment}
6234 @{
6235 return 0;
6236 @}
6237 @end group
6238 @end example
6239
6240 Return @code{nil} if called in any other situation, to be useful in list
6241 expressions.
6242
6243 @workswith @code{comment-intro}.
6244 @end defun
6245
6246 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
6247 @node Misc Line-Up, , Comment Line-Up, Line-Up Functions
6248 @comment node-name, next, previous, up
6249 @subsection Miscellaneous Line-Up Functions
6250 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
6251
6252 The line-up functions here are the odds and ends which didn't fit into
6253 any earlier category.
6254
6255 @defun c-lineup-dont-change
6256 @findex lineup-dont-change (c-)
6257 This lineup function makes the line stay at whatever indentation it
6258 already has; think of it as an identity function for lineups.
6259
6260 @workswith Any syntactic symbol.
6261 @end defun
6262
6263 @comment ------------------------------------------------------------
6264
6265 @defun c-lineup-cpp-define
6266 @findex lineup-cpp-define (c-)
6267 Line up macro continuation lines according to the indentation of the
6268 construct preceding the macro. E.g:
6269
6270 @example
6271 @group
6272 const char msg[] = @hereFn{@r{The beginning of the preceding construct.}}
6273 \"Some text.\";
6274
6275 #define X(A, B) \
6276 do @{ \ @hereFn{c-lineup-cpp-define}
6277 printf (A, B); \
6278 @} while (0)
6279 @end group
6280 @end example
6281
6282 @noindent
6283 and:
6284
6285 @example
6286 @group
6287 int dribble() @{
6288 if (!running) @hereFn{@r{The beginning of the preceding construct.}}
6289 error(\"Not running!\");
6290
6291 #define X(A, B) \
6292 do @{ \ @hereFn{c-lineup-cpp-define}
6293 printf (A, B); \
6294 @} while (0)
6295 @end group
6296 @end example
6297
6298 If @code{c-syntactic-indentation-in-macros} is non-@code{nil}, the
6299 function returns the relative indentation to the macro start line to
6300 allow accumulation with other offsets. E.g. in the following cases,
6301 @code{cpp-define-intro} is combined with the
6302 @code{statement-block-intro} that comes from the @samp{do @{} that hangs
6303 on the @samp{#define} line:
6304
6305 @example
6306 @group
6307 const char msg[] =
6308 \"Some text.\";
6309
6310 #define X(A, B) do @{ \
6311 printf (A, B); \ @hereFn{c-lineup-cpp-define}
6312 this->refs++; \
6313 @} while (0) @hereFn{c-lineup-cpp-define}
6314 @end group
6315 @end example
6316
6317 @noindent
6318 and:
6319
6320 @example
6321 @group
6322 int dribble() @{
6323 if (!running)
6324 error(\"Not running!\");
6325
6326 #define X(A, B) do @{ \
6327 printf (A, B); \ @hereFn{c-lineup-cpp-define}
6328 this->refs++; \
6329 @} while (0) @hereFn{c-lineup-cpp-define}
6330 @end group
6331 @end example
6332
6333 The relative indentation returned by @code{c-lineup-cpp-define} is zero
6334 and two, respectively, on the two lines in each of these examples. They
6335 are then added to the two column indentation that
6336 @code{statement-block-intro} gives in both cases here.
6337
6338 If the relative indentation is zero, then @code{nil} is returned
6339 instead. That is useful in a list expression to specify the default
6340 indentation on the top level.
6341
6342 If @code{c-syntactic-indentation-in-macros} is @code{nil} then this
6343 function keeps the current indentation, except for empty lines (ignoring
6344 the ending backslash) where it takes the indentation from the closest
6345 preceding nonempty line in the macro. If there's no such line in the
6346 macro then the indentation is taken from the construct preceding it, as
6347 described above.
6348
6349 @workswith @code{cpp-define-intro}.
6350 @end defun
6351
6352 @comment ------------------------------------------------------------
6353
6354 @defun c-lineup-gcc-asm-reg
6355 @findex lineup-gcc-asm-reg (c-)
6356 Line up a gcc asm register under one on a previous line.
6357
6358 @example
6359 @group
6360 asm ("foo %1, %0\n"
6361 "bar %0, %1"
6362 : "=r" (w),
6363 "=r" (x)
6364 : "0" (y),
6365 "1" (z));
6366 @end group
6367 @end example
6368
6369 The @samp{x} line is aligned to the text after the @samp{:} on the
6370 @samp{w} line, and similarly @samp{z} under @samp{y}.
6371
6372 This is done only in an @samp{asm} or @samp{__asm__} block, and only to
6373 those lines mentioned. Anywhere else @code{nil} is returned. The usual
6374 arrangement is to have this routine as an extra feature at the start of
6375 arglist lineups, e.g.
6376
6377 @example
6378 (c-lineup-gcc-asm-reg c-lineup-arglist)
6379 @end example
6380
6381 @workswith @code{arglist-cont}, @code{arglist-cont-nonempty}.
6382 @end defun
6383
6384 @comment ------------------------------------------------------------
6385
6386 @defun c-lineup-topmost-intro-cont
6387 @findex lineup-topmost-intro-cont (c-)
6388 Line up declaration continuation lines zero or one indentation
6389 step@footnote{This function is mainly provided to mimic the behavior of
6390 CC Mode 5.28 and earlier where this case wasn't handled consistently so
6391 that those lines could be analyzed as either topmost-intro-cont or
6392 statement-cont. It's used for @code{topmost-intro-cont} by default, but
6393 you might consider using @code{+} instead.}. For lines preceding a
6394 definition, zero is used. For other lines, @code{c-basic-offset} is
6395 added to the indentation. E.g:
6396
6397 @example
6398 @group
6399 int
6400 neg (int i) @hereFn{c-lineup-topmost-intro-cont}
6401 @{
6402 return -i;
6403 @}
6404 @end group
6405 @end example
6406
6407 @noindent
6408 and
6409
6410 @example
6411 @group
6412 struct
6413 larch @hereFn{c-lineup-topmost-intro-cont}
6414 @{
6415 double height;
6416 @}
6417 the_larch, @hereFn{c-lineup-topmost-intro-cont}
6418 another_larch; @hereFn{c-lineup-topmost-intro-cont}
6419 @sssTBasicOffset{}
6420 @end group
6421 @end example
6422
6423 @noindent
6424 and
6425
6426 @example
6427 @group
6428 struct larch
6429 the_larch, @hereFn{c-lineup-topmost-intro-cont}
6430 another_larch; @hereFn{c-lineup-topmost-intro-cont}
6431 @end group
6432 @end example
6433
6434 @workswith @code{topmost-intro-cont}.
6435 @end defun
6436
6437 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
6438 @node Custom Line-Up, Other Indentation, Line-Up Functions, Customizing Indentation
6439 @comment node-name, next, previous, up
6440 @section Custom Line-Up Functions
6441 @cindex customization, indentation functions
6442 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
6443
6444 The most flexible way to customize indentation is by writing custom
6445 line-up functions, and associating them with specific syntactic
6446 symbols (@pxref{c-offsets-alist}). Depending on the effect you want,
6447 it might be better to write a @code{c-special-indent-hook} function
6448 rather than a line-up function (@pxref{Other Indentation}).
6449
6450 @ccmode{} comes with an extensive set of predefined line-up functions,
6451 not all of which are used by the default styles. So there's a good
6452 chance the function you want already exists. @xref{Line-Up
6453 Functions}, for a list of them. If you write your own line-up
6454 function, it's probably a good idea to start working from one of these
6455 predefined functions, which can be found in the file
6456 @file{cc-align.el}. If you have written a line-up function that you
6457 think is generally useful, you're very welcome to contribute it;
6458 please contact @email{bug-cc-mode@@gnu.org}.
6459
6460 Line-up functions are passed a single argument, the syntactic
6461 element (see below). The return value is a @code{c-offsets-alist}
6462 offset specification: for example, an integer, a symbol such as
6463 @code{+}, a vector, @code{nil}@footnote{Returning @code{nil} is useful
6464 when the offset specification for a syntactic element is a list
6465 containing the line-up function (@pxref{c-offsets-alist}).}, or even
6466 another line-up function. Full details of these are in
6467 @ref{c-offsets-alist}.
6468
6469 Line-up functions must not move point or change the content of the
6470 buffer (except temporarily). They are however allowed to do
6471 @dfn{hidden buffer changes}, i.e. setting text properties for caching
6472 purposes etc. Buffer undo recording is disabled while they run.
6473
6474 The syntactic element passed as the parameter to a line-up function is
6475 a cons cell of the form
6476
6477 @example
6478 (@r{@var{syntactic-symbol}} . @r{@var{anchor-position}})
6479 @end example
6480
6481 @noindent
6482 @c FIXME!!! The following sentence might be better omitted, since the
6483 @c information is in the cross reference "Syntactic Analysis". 2005/10/2.
6484 where @var{syntactic-symbol} is the symbol that the function was
6485 called for, and @var{anchor-position} is the anchor position (if any)
6486 for the construct that triggered the syntactic symbol
6487 (@pxref{Syntactic Analysis}). This cons cell is how the syntactic
6488 element of a line used to be represented in @ccmode{} 5.28 and
6489 earlier. Line-up functions are still passed this cons cell, so as to
6490 preserve compatibility with older configurations. In the future, we
6491 may decide to convert to using the full list format---you can prepare
6492 your setup for this by using the access functions
6493 (@code{c-langelem-sym}, etc.) described below.
6494
6495 @vindex c-syntactic-element
6496 @vindex syntactic-element (c-)
6497 @vindex c-syntactic-context
6498 @vindex syntactic-context (c-)
6499 Some syntactic symbols, e.g. @code{arglist-cont-nonempty}, have more
6500 info in the syntactic element - typically other positions that can be
6501 interesting besides the anchor position. That info can't be accessed
6502 through the passed argument, which is a cons cell. Instead, you can
6503 get this information from the variable @code{c-syntactic-element},
6504 which is dynamically bound to the complete syntactic element. The
6505 variable @code{c-syntactic-context} might also be useful - it gets
6506 dynamically bound to the complete syntactic context. @xref{Custom
6507 Braces}.
6508
6509 @ccmode{} provides a few functions to access parts of syntactic
6510 elements in a more abstract way. Besides making the code easier to
6511 read, they also hide the difference between the old cons cell form
6512 used in the line-up function argument and the new list form used in
6513 @code{c-syntactic-element} and everywhere else. The functions are:
6514
6515 @defun c-langelem-sym langelem
6516 @findex langelem-sym (c-)
6517 Return the syntactic symbol in @var{langelem}.
6518 @end defun
6519
6520 @defun c-langelem-pos langelem
6521 @findex langelem-pos (c-)
6522 Return the anchor position in @var{langelem}, or nil if there is none.
6523 @end defun
6524
6525 @defun c-langelem-col langelem &optional preserve-point
6526 @findex langelem-col (c-)
6527 Return the column of the anchor position in @var{langelem}. Also move
6528 the point to that position unless @var{preserve-point} is
6529 non-@code{nil}.
6530 @end defun
6531
6532 @defun c-langelem-2nd-pos langelem
6533 @findex langelem-2nd-pos (c-)
6534 Return the secondary position in @var{langelem}, or @code{nil} if there
6535 is none.
6536
6537 Note that the return value of this function is always @code{nil} if
6538 @var{langelem} is in the old cons cell form. Thus this function is
6539 only meaningful when used on syntactic elements taken from
6540 @code{c-syntactic-element} or @code{c-syntactic-context}.
6541 @end defun
6542
6543 Custom line-up functions can be as simple or as complex as you like, and
6544 any syntactic symbol that appears in @code{c-offsets-alist} can have a
6545 custom line-up function associated with it.
6546
6547 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
6548 @node Other Indentation, , Custom Line-Up, Customizing Indentation
6549 @comment node-name, next, previous, up
6550 @section Other Special Indentations
6551 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
6552
6553 Here are the remaining odds and ends regarding indentation:
6554
6555 @defopt c-label-minimum-indentation
6556 @vindex label-minimum-indentation (c-)
6557 In @samp{gnu} style (@pxref{Built-in Styles}), a minimum indentation is
6558 imposed on lines inside code blocks. This minimum indentation is
6559 controlled by this style variable. The default value is 1.
6560
6561 @findex c-gnu-impose-minimum
6562 @findex gnu-impose-minimum (c-)
6563 It's the function @code{c-gnu-impose-minimum} that enforces this minimum
6564 indentation. It must be present on @code{c-special-indent-hook} to
6565 work.
6566 @end defopt
6567
6568 @defopt c-special-indent-hook
6569 @vindex special-indent-hook (c-)
6570 This style variable is a standard hook variable that is called after
6571 every line is indented by @ccmode{}. It is called only if
6572 @code{c-syntactic-indentation} is non-@code{nil} (which it is by
6573 default (@pxref{Indentation Engine Basics})). You can put a function
6574 on this hook to do any special indentation or ad hoc line adjustments
6575 your style dictates, such as adding extra indentation to constructors
6576 or destructor declarations in a class definition, etc. Sometimes it
6577 is better to write a custom Line-up Function instead (@pxref{Custom
6578 Line-Up}).
6579
6580 When the indentation engine calls this hook, the variable
6581 @code{c-syntactic-context} is bound to the current syntactic context
6582 (i.e. what you would get by typing @kbd{C-c C-s} on the source line.
6583 @xref{Custom Braces}.). Note that you should not change point or mark
6584 inside a @code{c-special-indent-hook} function, i.e. you'll probably
6585 want to wrap your function in a @code{save-excursion}@footnote{The
6586 numerical value returned by @code{point} will change if you change the
6587 indentation of the line within a @code{save-excursion} form, but point
6588 itself will still be over the same piece of text.}.
6589
6590 Setting @code{c-special-indent-hook} in style definitions is handled
6591 slightly differently from other variables---A style can only add
6592 functions to this hook, not remove them. @xref{Style Variables}.
6593 @end defopt
6594
6595
6596 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
6597 @node Custom Macros, Odds and Ends, Customizing Indentation, Top
6598 @comment node-name, next, previous, up
6599 @chapter Customizing Macros
6600 @cindex macros
6601 @cindex preprocessor directives
6602 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
6603
6604 Normally, the lines in a multi-line macro are indented relative to
6605 each other as though they were code. You can suppress this behavior
6606 by setting the following user option:
6607
6608 @defopt c-syntactic-indentation-in-macros
6609 @vindex syntactic-indentation-in-macros (c-)
6610 Enable syntactic analysis inside macros, which is the default. If this
6611 is @code{nil}, all lines inside macro definitions are analyzed as
6612 @code{cpp-macro-cont}.
6613 @end defopt
6614
6615 @ccmode{} provides some tools to help keep the line continuation
6616 backslashes in macros neat and tidy. Their precise action is
6617 customized with these variables:
6618
6619 @defopt c-backslash-column
6620 @vindex backslash-column (c-)
6621 @defoptx c-backslash-max-column
6622 @vindex backslash-max-column (c-)
6623 These variables control the alignment columns for line continuation
6624 backslashes in multiline macros. They are used by the functions that
6625 automatically insert or align such backslashes,
6626 e.g. @code{c-backslash-region} and @code{c-context-line-break}.
6627
6628 @code{c-backslash-column} specifies the minimum column for the
6629 backslashes. If any line in the macro goes past this column, then the
6630 next tab stop (i.e. next multiple of @code{tab-width}) in that line is
6631 used as the alignment column for all the backslashes, so that they
6632 remain in a single column. However, if any lines go past
6633 @code{c-backslash-max-column} then the backslashes in the rest of the
6634 macro will be kept at that column, so that the lines which are too
6635 long ``stick out'' instead.
6636
6637 Don't ever set these variables to @code{nil}. If you want to disable
6638 the automatic alignment of backslashes, use
6639 @code{c-auto-align-backslashes}.
6640 @end defopt
6641
6642 @defopt c-auto-align-backslashes
6643 @vindex auto-align-backslashes (c-)
6644 Align automatically inserted line continuation backslashes if
6645 non-@code{nil}. When line continuation backslashes are inserted
6646 automatically for line breaks in multiline macros, e.g. by
6647 @code{c-context-line-break}, they are aligned with the other
6648 backslashes in the same macro if this flag is set.
6649
6650 If @code{c-auto-align-backslashes} is @code{nil}, automatically
6651 inserted backslashes are preceded by a single space, and backslashes
6652 get aligned only when you explicitly invoke the command
6653 @code{c-backslash-region} (@kbd{C-c C-\}).
6654 @end defopt
6655
6656 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
6657 @node Odds and Ends, Sample .emacs File, Custom Macros, Top
6658 @comment node-name, next, previous, up
6659 @chapter Odds and Ends
6660 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
6661
6662 The stuff that didn't fit in anywhere else is documented here.
6663
6664 @defopt c-require-final-newline
6665 @vindex require-final-newline (c-)
6666 Controls whether a final newline is enforced when the file is saved.
6667 The value is an association list that for each language mode specifies
6668 the value to give to @code{require-final-newline} (@pxref{Saving
6669 Buffers,,, @lispref{}, @lispreftitle{}}) at mode initialization. If a
6670 language isn't present on the association list, CC Mode won't touch
6671 @code{require-final-newline} in buffers for that language.
6672
6673 The default is to set @code{require-final-newline} to @code{t} in the
6674 languages that mandate that source files should end with newlines.
6675 These are C, C++ and Objective-C.
6676 @end defopt
6677
6678 @defopt c-echo-syntactic-information-p
6679 @vindex echo-syntactic-information-p (c-)
6680 If non-@code{nil}, the syntactic analysis for the current line is shown
6681 in the echo area when it's indented (unless
6682 @code{c-syntactic-indentation} is @code{nil}). That's useful when
6683 finding out which syntactic symbols to modify to get the indentation you
6684 want.
6685 @end defopt
6686
6687 @defopt c-report-syntactic-errors
6688 @vindex report-syntactic-errors (c-)
6689 If non-@code{nil}, certain syntactic errors are reported with a ding and
6690 a message, for example when an @code{else} is indented for which there
6691 is no corresponding @code{if}.
6692
6693 Note however that @ccmode{} doesn't make any special effort to check for
6694 syntactic errors; that's the job of the compiler. The reason it can
6695 report cases like the one above is that it can't find the correct
6696 anchoring position to indent the line in that case.
6697 @end defopt
6698
6699
6700 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
6701 @node Sample .emacs File, Performance Issues, Odds and Ends, Top
6702 @comment node-name, next, previous, up
6703 @appendix Sample .emacs File
6704 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
6705
6706 Here's a sample .emacs file fragment that might help you along the way.
6707 Just copy this region and paste it into your .emacs file. You might want
6708 to change some of the actual values.
6709
6710 @verbatim
6711 ;; Make a non-standard key binding. We can put this in
6712 ;; c-mode-base-map because c-mode-map, c++-mode-map, and so on,
6713 ;; inherit from it.
6714 (defun my-c-initialization-hook ()
6715 (define-key c-mode-base-map "\C-m" 'c-context-line-break))
6716 (add-hook 'c-initialization-hook 'my-c-initialization-hook)
6717
6718 ;; offset customizations not in my-c-style
6719 ;; This will take precedence over any setting of the syntactic symbol
6720 ;; made by a style.
6721 (setq c-offsets-alist '((member-init-intro . ++)))
6722
6723 ;; Create my personal style.
6724 (defconst my-c-style
6725 '((c-tab-always-indent . t)
6726 (c-comment-only-line-offset . 4)
6727 (c-hanging-braces-alist . ((substatement-open after)
6728 (brace-list-open)))
6729 (c-hanging-colons-alist . ((member-init-intro before)
6730 (inher-intro)
6731 (case-label after)
6732 (label after)
6733 (access-label after)))
6734 (c-cleanup-list . (scope-operator
6735 empty-defun-braces
6736 defun-close-semi))
6737 (c-offsets-alist . ((arglist-close . c-lineup-arglist)
6738 (substatement-open . 0)
6739 (case-label . 4)
6740 (block-open . 0)
6741 (knr-argdecl-intro . -)))
6742 (c-echo-syntactic-information-p . t))
6743 "My C Programming Style")
6744 (c-add-style "PERSONAL" my-c-style)
6745
6746 ;; Customizations for all modes in CC Mode.
6747 (defun my-c-mode-common-hook ()
6748 ;; set my personal style for the current buffer
6749 (c-set-style "PERSONAL")
6750 ;; other customizations
6751 (setq tab-width 8
6752 ;; this will make sure spaces are used instead of tabs
6753 indent-tabs-mode nil)
6754 ;; we like auto-newline, but not hungry-delete
6755 (c-toggle-auto-newline 1))
6756 (add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
6757 @end verbatim
6758
6759 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
6760 @node Performance Issues, Limitations and Known Bugs, Sample .emacs File, Top
6761 @comment node-name, next, previous, up
6762 @chapter Performance Issues
6763 @cindex performance
6764 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
6765
6766 @comment FIXME: (ACM, 2003/5/24). Check whether AWK needs mentioning here.
6767
6768 C and its derivative languages are highly complex creatures. Often,
6769 ambiguous code situations arise that require @ccmode{} to scan large
6770 portions of the buffer to determine syntactic context. Such
6771 pathological code can cause @ccmode{} to perform fairly badly. This
6772 section gives some insight in how @ccmode{} operates, how that interacts
6773 with some coding styles, and what you can use to improve performance.
6774
6775 The overall goal is that @ccmode{} shouldn't be overly slow (i.e. take
6776 more than a fraction of a second) in any interactive operation.
6777 I.e. it's tuned to limit the maximum response time in single operations,
6778 which is sometimes at the expense of batch-like operations like
6779 reindenting whole blocks. If you find that @ccmode{} gradually gets
6780 slower and slower in certain situations, perhaps as the file grows in
6781 size or as the macro or comment you're editing gets bigger, then chances
6782 are that something isn't working right. You should consider reporting
6783 it, unless it's something that's mentioned in this section.
6784
6785 Because @ccmode{} has to scan the buffer backwards from the current
6786 insertion point, and because C's syntax is fairly difficult to parse in
6787 the backwards direction, @ccmode{} often tries to find the nearest
6788 position higher up in the buffer from which to begin a forward scan
6789 (it's typically an opening or closing parenthesis of some kind). The
6790 farther this position is from the current insertion point, the slower it
6791 gets.
6792
6793 @findex beginning-of-defun
6794 In earlier versions of @ccmode{}, we used to recommend putting the
6795 opening brace of a top-level construct@footnote{E.g. a function in C,
6796 or outermost class definition in C++ or Java.} into the leftmost
6797 column. Earlier still, this used to be a rigid Emacs constraint, as
6798 embodied in the @code{beginning-of-defun} function. @ccmode now
6799 caches syntactic information much better, so that the delay caused by
6800 searching for such a brace when it's not in column 0 is minimal,
6801 except perhaps when you've just moved a long way inside the file.
6802
6803 @findex defun-prompt-regexp
6804 @vindex c-Java-defun-prompt-regexp
6805 @vindex Java-defun-prompt-regexp (c-)
6806 A special note about @code{defun-prompt-regexp} in Java mode: The common
6807 style is to hang the opening braces of functions and classes on the
6808 right side of the line, and that doesn't work well with the Emacs
6809 approach. @ccmode{} comes with a constant
6810 @code{c-Java-defun-prompt-regexp} which tries to define a regular
6811 expression usable for this style, but there are problems with it. In
6812 some cases it can cause @code{beginning-of-defun} to hang@footnote{This
6813 has been observed in Emacs 19.34 and XEmacs 19.15.}. For this reason,
6814 it is not used by default, but if you feel adventurous, you can set
6815 @code{defun-prompt-regexp} to it in your mode hook. In any event,
6816 setting and relying on @code{defun-prompt-regexp} will definitely slow
6817 things down because (X)Emacs will be doing regular expression searches a
6818 lot, so you'll probably be taking a hit either way!
6819
6820 @ccmode{} maintains a cache of the opening parentheses of the blocks
6821 surrounding the point, and it adapts that cache as the point is moved
6822 around. That means that in bad cases it can take noticeable time to
6823 indent a line in a new surrounding, but after that it gets fast as long
6824 as the point isn't moved far off. The farther the point is moved, the
6825 less useful is the cache. Since editing typically is done in ``chunks''
6826 rather than on single lines far apart from each other, the cache
6827 typically gives good performance even when the code doesn't fit the
6828 Emacs approach to finding the defun starts.
6829
6830 @vindex c-enable-xemacs-performance-kludge-p
6831 @vindex enable-xemacs-performance-kludge-p (c-)
6832 XEmacs users can set the variable
6833 @code{c-enable-xemacs-performance-kludge-p} to non-@code{nil}. This
6834 tells @ccmode{} to use XEmacs-specific built-in functions which, in some
6835 circumstances, can locate the top-most opening brace much more quickly than
6836 @code{beginning-of-defun}. Preliminary testing has shown that for
6837 styles where these braces are hung (e.g. most JDK-derived Java styles),
6838 this hack can improve performance of the core syntax parsing routines
6839 from 3 to 60 times. However, for styles which @emph{do} conform to
6840 Emacs' recommended style of putting top-level braces in column zero,
6841 this hack can degrade performance by about as much. Thus this variable
6842 is set to @code{nil} by default, since the Emacs-friendly styles should
6843 be more common (and encouraged!). Note that this variable has no effect
6844 in Emacs since the necessary built-in functions don't exist (in Emacs
6845 22.1 as of this writing in February 2007).
6846
6847 Text properties are used to speed up skipping over syntactic whitespace,
6848 i.e. comments and preprocessor directives. Indenting a line after a
6849 huge macro definition can be slow the first time, but after that the
6850 text properties are in place and it should be fast (even after you've
6851 edited other parts of the file and then moved back).
6852
6853 Font locking can be a CPU hog, especially the font locking done on
6854 decoration level 3 which tries to be very accurate. Note that that
6855 level is designed to be used with a font lock support mode that only
6856 fontifies the text that's actually shown, i.e. Lazy Lock or Just-in-time
6857 Lock mode, so make sure you use one of them. Fontification of a whole
6858 buffer with some thousand lines can often take over a minute. That is
6859 a known weakness; the idea is that it never should happen.
6860
6861 The most effective way to speed up font locking is to reduce the
6862 decoration level to 2 by setting @code{font-lock-maximum-decoration}
6863 appropriately. That level is designed to be as pretty as possible
6864 without sacrificing performance. @xref{Font Locking Preliminaries}, for
6865 more info.
6866
6867
6868 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
6869 @node Limitations and Known Bugs, FAQ, Performance Issues, Top
6870 @comment node-name, next, previous, up
6871 @chapter Limitations and Known Bugs
6872 @cindex limitations
6873 @cindex bugs
6874 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
6875
6876 @itemize @bullet
6877 @item
6878 @ccmode{} doesn't support trigraphs. (These are character sequences
6879 such as @samp{??(}, which represents @samp{[}. They date from a time
6880 when some character sets didn't have all the characters that C needs,
6881 and are now utterly obsolete.)
6882
6883 @item
6884 There is no way to apply auto newline settings (@pxref{Auto-newlines})
6885 on already typed lines. That's only a feature to ease interactive
6886 editing.
6887
6888 To generalize this issue a bit: @ccmode{} is not intended to be used as
6889 a reformatter for old code in some more or less batch-like way. With
6890 the exception of some functions like @code{c-indent-region}, it's only
6891 geared to be used interactively to edit new code. There's currently no
6892 intention to change this goal.
6893
6894 If you want to reformat old code, you're probably better off using some
6895 other tool instead, e.g. @ref{Top, , GNU indent, indent, The `indent'
6896 Manual}, which has more powerful reformatting capabilities than
6897 @ccmode{}.
6898
6899 @item
6900 The support for C++ templates (in angle brackets) is not yet complete.
6901 When a non-nested template is used in a declaration, @ccmode{} indents
6902 it and font-locks it OK. Templates used in expressions, and nested
6903 templates do not fare so well. Sometimes a workaround is to refontify
6904 the expression after typing the closing @samp{>}.
6905
6906 @item
6907 In a @dfn{k&r region} (the part of an old-fashioned C function
6908 declaration which specifies the types of its parameters, coming
6909 between the parameter list and the opening brace), there should be at
6910 most 20 top-level parenthesis and bracket pairs. This limit has been
6911 imposed for performance reasons. If it is violated, the source file
6912 might be incorrectly indented or fontified.
6913
6914 @item
6915 On loading @ccmode{}, sometimes this error message appears:
6916
6917 @example
6918 File mode specification error: (void-variable c-font-lock-keywords-3)
6919 @end example
6920
6921 This is due to a bug in the function @code{eval-after-load} in some
6922 versions of (X)Emacs. It can manifest itself when there is a symbolic
6923 link in the path of the directory which contains (X)Emacs. As a
6924 workaround, put the following into your @file{.emacs} file, fairly
6925 early on:
6926
6927 @example
6928 (defun my-load-cc-fonts ()
6929 (require "cc-fonts"))
6930 (add-hook 'c-initialization-hook 'my-load-cc-fonts)
6931 @end example
6932 @end itemize
6933
6934 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
6935 @node FAQ, Updating CC Mode, Limitations and Known Bugs, Top
6936 @comment node-name, next, previous, up
6937 @appendix Frequently Asked Questions
6938 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
6939
6940 @itemize @bullet
6941 @item
6942 @emph{How can I change the indent level from 4 spaces to 2 spaces?}
6943
6944 Set the variable @code{c-basic-offset}. @xref{Getting Started}.
6945
6946 @item
6947 @kindex RET
6948 @kindex C-j
6949 @emph{Why doesn't the @kbd{RET} key indent the new line?}
6950
6951 Emacs' convention is that @kbd{RET} just adds a newline, and that
6952 @kbd{C-j} adds a newline and indents it. You can make @kbd{RET} do this
6953 too by adding this to your @code{c-initialization-hook}:
6954
6955 @example
6956 (define-key c-mode-base-map "\C-m" 'c-context-line-break)
6957 @end example
6958
6959 @xref{Getting Started}. This is a very common question. If you want
6960 this to be the default behavior, don't lobby us, lobby RMS! @t{:-)}
6961
6962 @item
6963 @emph{How do I stop my code jumping all over the place when I type?}
6964
6965 Deactivate ``electric minor mode'' with @kbd{C-c C-l}. @xref{Getting
6966 Started}.
6967
6968 @item
6969 @kindex C-x h
6970 @kindex C-M-\
6971 @emph{How do I reindent the whole file?}
6972
6973 Visit the file and hit @kbd{C-x h} to mark the whole buffer. Then hit
6974 @kbd{C-M-\}. @xref{Indentation Commands}.
6975
6976 @item
6977 @kindex C-M-q
6978 @kindex C-M-u
6979 @emph{How do I reindent the current block?}
6980
6981 First move to the brace which opens the block with @kbd{C-M-u}, then
6982 reindent that expression with @kbd{C-M-q}. @xref{Indentation
6983 Commands}.
6984
6985 @item
6986 @emph{I put @code{(c-set-offset 'substatement-open 0)} in my
6987 @file{.emacs} file but I get an error saying that @code{c-set-offset}'s
6988 function definition is void. What's wrong?}
6989
6990 This means that @ccmode{} hasn't yet been loaded into your Emacs
6991 session by the time the @code{c-set-offset} call is reached, most
6992 likely because @ccmode{} is being autoloaded. Instead of putting the
6993 @code{c-set-offset} line in your top-level @file{.emacs} file, put it
6994 in your @code{c-initialization-hook} (@pxref{CC Hooks}), or simply
6995 modify @code{c-offsets-alist} directly:
6996
6997 @example
6998 (setq c-offsets-alist '((substatement-open . 0)))
6999 @end example
7000
7001 @item
7002 @cindex open paren in column zero
7003 @emph{I have an open paren character at column zero inside a comment or
7004 multiline string literal, and it causes the fontification and/or
7005 indentation to go haywire. What gives?}
7006
7007 It's due to the ad-hoc rule in (X)Emacs that such open parens always
7008 start defuns (which translates to functions, classes, namespaces or any
7009 other top-level block constructs in the @ccmode{} languages).
7010 @ifset XEMACS
7011 @xref{Defuns,,, xemacs, XEmacs User's Manual}, for details.
7012 @end ifset
7013 @ifclear XEMACS
7014 @xref{Left Margin Paren,,, emacs, GNU Emacs Manual}, for details
7015 (@xref{Defuns,,, emacs, GNU Emacs Manual}, in the Emacs 20 manual).
7016 @end ifclear
7017
7018 This heuristic is built into the core syntax analysis routines in
7019 (X)Emacs, so it's not really a @ccmode{} issue. However, in Emacs
7020 21.1 it became possible to turn it off@footnote{Using the variable
7021 @code{open-paren-in-column-0-is-defun-start}.} and @ccmode{} does so
7022 there since it's got its own system to keep track of blocks.
7023
7024 @end itemize
7025
7026
7027 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7028 @node Updating CC Mode, Mailing Lists and Bug Reports, FAQ, Top
7029 @comment node-name, next, previous, up
7030 @appendix Getting the Latest CC Mode Release
7031 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7032
7033 @ccmode{} has been standard with all versions of Emacs since 19.34 and
7034 of XEmacs since 19.16.
7035
7036 @cindex web site
7037 Due to release schedule skew, it is likely that all of these Emacsen
7038 have old versions of @ccmode{} and so should be upgraded. Access to the
7039 @ccmode{} source code, as well as more detailed information on Emacsen
7040 compatibility, etc. are all available on the web site:
7041
7042 @quotation
7043 @uref{http://cc-mode.sourceforge.net/}
7044 @end quotation
7045
7046
7047 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7048 @node Mailing Lists and Bug Reports, GNU Free Documentation License, Updating CC Mode, Top
7049 @comment node-name, next, previous, up
7050 @appendix Mailing Lists and Submitting Bug Reports
7051 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7052
7053 @kindex C-c C-b
7054 @findex c-submit-bug-report
7055 @findex submit-bug-report (c-)
7056 To report bugs, use the @kbd{C-c C-b} (bound to
7057 @code{c-submit-bug-report}) command. This provides vital information
7058 we need to reproduce your problem. Make sure you include a concise,
7059 but complete code example. Please try to boil your example down to
7060 just the essential code needed to reproduce the problem, and include
7061 an exact recipe of steps needed to expose the bug. Be especially sure
7062 to include any code that appears @emph{before} your bug example, if
7063 you think it might affect our ability to reproduce it.
7064
7065 Please try to produce the problem in an Emacs instance without any
7066 customizations loaded (i.e. start it with the @samp{-q --no-site-file}
7067 arguments). If it works correctly there, the problem might be caused
7068 by faulty customizations in either your own or your site
7069 configuration. In that case, we'd appreciate it if you isolate the
7070 Emacs Lisp code that triggers the bug and include it in your report.
7071
7072 @cindex bug report mailing list
7073 Bug reports should be sent to @email{bug-cc-mode@@gnu.org}. You can
7074 also send other questions and suggestions (kudos? @t{;-)} to that
7075 address. It's a mailing list which you can join or browse an archive
7076 of; see the web site at @uref{http://cc-mode.sourceforge.net/} for
7077 further details.
7078
7079 @cindex announcement mailing list
7080 If you want to get announcements of new @ccmode{} releases, send the
7081 word @emph{subscribe} in the body of a message to
7082 @email{cc-mode-announce-request@@lists.sourceforge.net}. It's possible
7083 to subscribe from the web site too. Announcements will also be posted
7084 to the Usenet newsgroups @code{gnu.emacs.sources}, @code{comp.emacs},
7085 @code{comp.emacs.xemacs}, @code{comp.lang.c}, @code{comp.lang.c++},
7086 @code{comp.lang.objective-c}, @code{comp.lang.java.softwaretools},
7087 @code{comp.lang.idl}, and @code{comp.lang.awk}.
7088 @c There is no newsgroup for Pike. :-(
7089
7090
7091 @node GNU Free Documentation License, Command and Function Index, Mailing Lists and Bug Reports, Top
7092 @appendix GNU Free Documentation License
7093 @include doclicense.texi
7094
7095
7096 @c Removed the tentative node "Mode Initialization" from here, 2005/8/27.
7097 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7098 @node Command and Function Index, Variable Index, GNU Free Documentation License, Top
7099 @comment node-name, next, previous, up
7100 @unnumbered Command and Function Index
7101 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7102
7103 Since most @ccmode{} commands are prepended with the string
7104 @samp{c-}, each appears under its @code{c-@var{thing}} name and its
7105 @code{@var{thing} (c-)} name.
7106 @iftex
7107 @sp 2
7108 @end iftex
7109 @printindex fn
7110
7111
7112 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7113 @node Variable Index, Concept and Key Index, Command and Function Index, Top
7114 @comment node-name, next, previous, up
7115 @unnumbered Variable Index
7116 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7117
7118 Since most @ccmode{} variables are prepended with the string
7119 @samp{c-}, each appears under its @code{c-@var{thing}} name and its
7120 @code{@var{thing} (c-)} name.
7121 @iftex
7122 @sp 2
7123 @end iftex
7124 @printindex vr
7125
7126
7127 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7128 @node Concept and Key Index, , Variable Index, Top
7129 @comment node-name, next, previous, up
7130 @unnumbered Concept and Key Index
7131 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7132
7133 @printindex cp
7134
7135
7136 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7137 @comment Epilogue.
7138 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7139
7140 @bye