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