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