3 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4 @comment %**start of header (This is for running Texinfo on a region)
5 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7 @comment No overfull hbox marks in the dvi file.
10 @setfilename ../info/ccmode
11 @settitle CC Mode Version 5 Documentation
14 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
15 @comment @setchapternewpage odd !! we don't want blank pages !!
16 @comment %**end of header (This is for running Texinfo on a region)
17 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
20 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
22 @comment Texinfo manual for CC Mode
23 @comment Generated from the original README file by Krishna Padmasola
24 @comment <krishna@earth-gw.njit.edu>
26 @comment Maintained by Martin Stjernholm <bug-cc-mode@gnu.org>
28 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
30 @comment Info directory entry for use by install-info
33 * CC Mode: (cc-mode). Emacs mode for editing C, C++, Objective-C,
34 Java, Pike, and IDL code.
37 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
38 @comment The following line inserts the copyright notice
39 @comment into the Info file.
40 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
43 Copyright @copyright{} 1995-2000 Free Software Foundation, Inc.
46 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
47 @comment !!!The titlepage section does not appear in the Info file.!!!
48 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
54 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
55 @comment The title is printed in a large font.
56 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
58 @center @titlefont{CC Mode 5.27}
60 @center @subtitlefont{A GNU Emacs mode for editing C and C-like languages}
62 @center Current maintainer:
63 @center Martin Stjernholm
66 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
67 @comment The following two commands start the copyright page
68 @comment for the printed manual. This will not appear in the Info file.
69 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
72 @vskip 0pt plus 1filll
73 Copyright @copyright{} 1995-2000 Free Software Foundation, Inc.
77 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
78 @comment The Top node contains the master menu for the Info file.
79 @comment This appears only in the Info file, not the printed manual.
80 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
82 @node Top, Introduction, (dir), (dir)
83 @comment node-name, next, previous, up
92 @ccmode{} is a GNU Emacs mode for editing files containing C, C++,
93 Objective-C, Java, CORBA IDL, and Pike code. It provides syntax-based
94 indentation and has several handy commands and some minor modes to make
95 the editing easier. Note that @ccmode{} does @emph{not} provide
96 font-locking; there are other Emacs packages for that.
99 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
100 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
104 * Getting Connected::
105 * New Indentation Engine::
107 * Text Filling and Line Breaking::
109 * Customizing Indentation::
110 * Syntactic Symbols::
111 * Indentation Functions::
112 * Performance Issues::
113 * Frequently Asked Questions::
114 * Getting the Latest CC Mode Release::
115 * Sample .emacs File::
116 * Limitations and Known Bugs::
117 * Mailing Lists and Submitting Bug Reports::
126 --- The Detailed Node Listing ---
128 New Indentation Engine
130 * Syntactic Analysis::
131 * Indentation Calculation::
135 * Auto-newline Insertion::
136 * Hungry-deletion of Whitespace::
138 Auto-newline Insertion
142 * Hanging Semi-colons and Commas::
143 * Other Electric Commands::
148 * Indentation Commands::
149 * Movement Commands::
152 Customizing Indentation
154 * Interactive Customization::
155 * Permanent Customization::
158 * Advanced Customizations::
166 Advanced Customizations
168 * Custom Indentation Functions::
169 * Custom Brace and Colon Hanging::
170 * Customizing Semi-colons and Commas::
171 * Other Special Indentations::
175 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
176 @node Introduction, Getting Connected, Top, Top
177 @comment node-name, next, previous, up
178 @chapter Introduction
179 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
183 Welcome to @ccmode{}, a GNU Emacs mode for editing files containing C,
184 C++, Objective-C, Java, CORBA IDL, and Pike code. This incarnation of
185 the mode is descendant from @file{c-mode.el} (also called "Boring Old C
186 Mode" or BOCM @t{:-)}, and @file{c++-mode.el} version 2, which Barry has
187 been maintaining since 1992. @ccmode{} represents a significant
188 milestone in the mode's life. It has been fully merged back with Emacs
189 19's @file{c-mode.el}. Also a new, more intuitive and flexible mechanism
190 for controlling indentation has been developed. Late in 1997, Martin
191 joined the @ccmode{} Maintainers Team, and implemented the Pike support.
192 As of 2000 Martin has taken over as the sole maintainer.
194 This manual describes @ccmode{}
195 @comment The following line must appear on its own, so that the automated
197 @comment Release.py script can update the version number automatically
199 @ccmode{} supports the editing of K&R and ANSI C, @dfn{ARM}
200 @footnote{@cite{The Annotated C++ Reference Manual}, by Ellis and
201 Stroustrup.} C++, Objective-C, Java, CORBA's Interface Definition
202 Language, and Pike@footnote{A C-like scripting language with its roots
203 in the LPC language used in some MUD engines. See
204 @uref{http://pike.idonex.se/}.} files. In this way, you can easily set
205 up consistent coding styles for use in editing all of these languages.
206 @ccmode{} does @emph{not} handle font-locking (a.k.a. syntax coloring,
207 keyword highlighting) or anything of that nature, for any of these
208 modes. Font-locking is handled by other Emacs packages.
210 This manual will describe the following:
214 How to get started using @ccmode{}.
217 How the new indentation engine works.
220 How to customize the new indentation engine.
230 Note that the name of this package is ``@ccmode{}'', but there is no top
231 level @code{cc-mode} entry point. All of the variables, commands, and
232 functions in @ccmode{} are prefixed with @code{c-@var{<thing>}}, and
233 @code{c-mode}, @code{c++-mode}, @code{objc-mode}, @code{java-mode},
234 @code{idl-mode}, and @code{pike-mode} entry points are provided. This
235 package is intended to be a replacement for @file{c-mode.el} and
238 @cindex @file{cc-compat.el} file
239 This distribution also contains a file
240 called @file{cc-compat.el} which should ease your transition from BOCM
241 to @ccmode{}. If you have a BOCM configuration you are really happy
242 with, and want to postpone learning how to configure @ccmode{}, take a
243 look at that file. It maps BOCM configuration variables to @ccmode{}'s
244 new indentation model. It is not actively supported so for the long
245 run, you should learn how to customize @ccmode{} to support your coding
248 A special word of thanks goes to Krishna Padmasola for his work in
249 converting the original @file{README} file to Texinfo format. I'd also
250 like to thank all the @ccmode{} victims who help enormously during the
251 early beta stages of @ccmode{}'s development.
254 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
255 @node Getting Connected, New Indentation Engine, Introduction, Top
256 @comment node-name, next, previous, up
257 @chapter Getting Connected
258 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
260 If you got this version of @ccmode{} with Emacs or XEmacs, it should
261 work just fine right out of the box. Note however that you may not have
262 the latest @ccmode{} release and may want to upgrade your copy.
264 If you are upgrading an existing @ccmode{} installation, please see the
265 @file{README} file for installation details. @ccmode{} may not work
266 with older versions of Emacs or XEmacs. See the @ccmode{} release notes
267 Web pages for the latest information on Emacs version and package
268 compatibility (@pxref{Getting the Latest CC Mode Release}).
270 @cindex @file{cc-mode-18.el} file
271 @emph{Note that @ccmode{} no longer
272 works with Emacs 18!}, so if you haven't upgraded from Emacs 18 by now,
277 You can find out what version of @ccmode{} you are using by visiting a C
278 file and entering @kbd{M-x c-version RET}. You should see this message in
282 Using CC Mode version 5.XX
287 where @samp{XX} is the minor release number.
290 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
291 @node New Indentation Engine, Minor Modes, Getting Connected, Top
292 @comment node-name, next, previous, up
293 @chapter New Indentation Engine
294 @cindex indentation engine
295 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
297 @ccmode{} has a new indentation engine, providing a simplified, yet
298 flexible and general mechanism for customizing indentation. It separates
299 indentation calculation into two steps: first, @ccmode{} analyzes the
300 line of code being indented to determine the kind of language construct
301 it's looking at, then it applies user defined offsets to the current
302 line based on this analysis.
304 This section will briefly cover how indentation is calculated in
305 @ccmode{}. It is important to understand the indentation model
306 being used so that you will know how to customize @ccmode{} for
307 your personal coding style.
310 * Syntactic Analysis::
311 * Indentation Calculation::
315 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
316 @node Syntactic Analysis, Indentation Calculation, , New Indentation Engine
317 @comment node-name, next, previous, up
318 @section Syntactic Analysis
319 @cindex syntactic analysis
320 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
322 @cindex relative buffer position
323 @cindex syntactic symbol
324 @cindex syntactic component
325 @cindex syntactic component list
326 The first thing @ccmode{} does when indenting a line of code, is to
327 analyze the line, determining the @dfn{syntactic component list} of the
328 construct on that line. A syntactic component consists of a pair of
329 information (in lisp parlance, a @emph{cons cell}), where the first part
330 is a @dfn{syntactic symbol}, and the second part is a @dfn{relative
331 buffer position}. Syntactic symbols describe elements of C code
332 @footnote{Unless otherwise noted, the term ``C code'' to refers to all
333 the C-like languages.}, e.g. @code{statement}, @code{substatement},
334 @code{class-open}, @code{class-close}, etc. @xref{Syntactic Symbols},
335 for a complete list of currently recognized syntactic symbols and their
336 semantics. The style variable @code{c-offsets-alist} also contains the
337 list of currently supported syntactic symbols.
339 Conceptually, a line of C code is always indented relative to the
340 indentation of some line higher up in the buffer. This is represented
341 by the relative buffer position in the syntactic component.
343 Here is an example. Suppose we had the following code as the only thing
344 in a C++ buffer @footnote{The line numbers in this and future examples
345 don't actually appear in the buffer, of course!}:
349 1: void swap( int& a, int& b )
360 @findex c-show-syntactic-information
361 @findex show-syntactic-information (c-)
362 We can use the command @kbd{C-c C-s}
363 (@code{c-show-syntactic-information}) to simply report what the
364 syntactic analysis is for the current line. Running this command on
365 line 4 of this example, we'd see in the echo area@footnote{With a universal
366 argument (i.e. @kbd{C-u C-c C-s}) the analysis is inserted into the
368 on the current line.}:
375 This tells us that the line is a statement and it is indented relative
376 to buffer position 35, which happens to be the @samp{i} in @code{int} on
377 line 3. If you were to move point to line 3 and hit @kbd{C-c C-s}, you
381 ((defun-block-intro . 29))
385 This indicates that the @samp{int} line is the first statement in a top
386 level function block, and is indented relative to buffer position 29,
387 which is the brace just after the function header.
389 Here's another example:
393 1: int add( int val, int incr, int doit )
397 5: return( val + incr );
406 Hitting @kbd{C-c C-s} on line 4 gives us:
409 ((substatement-open . 46))
414 @cindex substatement block
416 which tells us that this is a brace that @emph{opens} a substatement
417 block. @footnote{A @dfn{substatement} is the line after a
418 conditional statement, such as @code{if}, @code{else}, @code{while},
419 @code{do}, @code{switch}, etc. A @dfn{substatement
420 block} is a brace block following one of these conditional statements.}
422 @cindex comment-only line
423 Syntactic component lists can contain more than one component, and
424 individual syntactic components need not have relative buffer positions.
425 The most common example of this is a line that contains a @dfn{comment
430 1: void draw_list( List<Drawables>& drawables )
432 3: // call the virtual draw() method on each element in list
433 4: for( int i=0; i < drawables.count(), ++i )
435 6: drawables[i].draw();
443 Hitting @kbd{C-c C-s} on line 3 of this example gives:
446 ((comment-intro) (defun-block-intro . 46))
451 and you can see that the syntactic component list contains two syntactic
452 components. Also notice that the first component,
453 @samp{(comment-intro)} has no relative buffer position.
456 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
457 @node Indentation Calculation, , Syntactic Analysis, New Indentation Engine
458 @comment node-name, next, previous, up
459 @section Indentation Calculation
460 @cindex indentation calculation
461 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
463 Indentation for a line is calculated using the syntactic
464 component list derived in step 1 above (@pxref{Syntactic Analysis}).
465 Each component contributes to the final total indentation of the line in
468 First, the syntactic symbols are looked up in the @code{c-offsets-alist}
469 style variable, which is an association list of syntactic symbols and
470 the offsets to apply for those symbols. These offsets are added to a
473 Second, if the component has a relative buffer position, @ccmode{}
474 adds the column number of that position to the running total. By adding
475 up the offsets and columns for every syntactic component on the list,
476 the final total indentation for the current line is computed.
478 Let's use our two code examples above to see how this works. Here is
479 our first example again:
483 1: void swap( int& a, int& b )
493 Let's say point is on line 3 and we hit the @kbd{TAB} key to re-indent
494 the line. Remember that the syntactic component list for that
498 ((defun-block-intro . 29))
503 @ccmode{} looks up @code{defun-block-intro} in the
504 @code{c-offsets-alist} style variable. Let's say it finds the value
505 @samp{4}; it adds this to the running total (initialized to zero),
506 yielding a running total indentation of 4 spaces.
508 Next @ccmode{} goes to buffer position 29 and asks for the current
509 column. This brace is in column zero, so @ccmode{}
510 adds @samp{0} to the running total. Since there is only one syntactic
511 component on the list for this line, indentation calculation is
512 complete, and the total indentation for the line
515 Here's another example:
519 1: int add( int val, int incr, int doit )
523 5: return( val + incr );
531 If we were to hit @kbd{TAB} on line 4 in the above example, the same
532 basic process is performed, despite the differences in the syntactic
533 component list. Remember that the list for this line is:
536 ((substatement-open . 46))
540 Here, @ccmode{} first looks up the @code{substatement-open} symbol
541 in @code{c-offsets-alist}. Let's say it finds the value @samp{4}. This
542 yields a running total of 4. @ccmode{} then goes to
543 buffer position 46, which is the @samp{i} in @code{if} on line 3. This
544 character is in the fourth column on that line so adding this to the
545 running total yields an indentation for the line of 8 spaces.
549 Actually, the mode usually just does The Right Thing without you having
550 to think about it in this much detail. But when customizing
551 indentation, it's helpful to understand the general indentation model
554 @vindex c-echo-syntactic-information-p
555 @vindex echo-syntactic-information-p (c-)
556 As you configure @ccmode{}, you might want to set the variable
557 @code{c-echo-syntactic-information-p} to non-@code{nil} so that the
558 syntactic component list and calculated offset will always be echoed in
559 the minibuffer when you hit @kbd{TAB}.
562 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
563 @node Minor Modes, Text Filling and Line Breaking, New Indentation Engine, Top
564 @comment node-name, next, previous, up
566 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
568 @ccmode{} contains two minor-mode-like features that you should
569 find useful while you enter new C code. The first is called
570 @dfn{auto-newline} mode, and the second is called @dfn{hungry-delete}
571 mode. These minor modes can be toggled on and off independently, and
572 @ccmode{} can be configured so that it starts up with any
573 combination of these minor modes. By default, both of these minor modes
576 The state of the minor modes is always reflected in the minor mode list
577 on the modeline of the @ccmode{} buffer. When auto-newline mode is
578 enabled, you will see @samp{C/a} on the mode line @footnote{The @samp{C}
579 would be replaced with @samp{C++}, @samp{ObjC}, @samp{Java}, @samp{IDL},
580 or @samp{Pike} for the respective languages.}. When hungry delete mode
581 is enabled you would see @samp{C/h} and when both modes are enabled,
582 you'd see @samp{C/ah}.
587 @findex c-toggle-hungry-state
588 @findex c-toggle-auto-state
589 @findex c-toggle-auto-hungry-state
590 @findex toggle-hungry-state (c-)
591 @findex toggle-auto-state (c-)
592 @findex toggle-auto-hungry-state (c-)
593 @ccmode{} provides keybindings which allow you to toggle the minor
594 modes on the fly while editing code. To toggle just the auto-newline
595 state, hit @kbd{C-c C-a} (@code{c-toggle-auto-state}). When you do
596 this, you should see the @samp{a} indicator either appear or disappear
597 on the modeline. Similarly, to toggle just the hungry-delete state, use
598 @kbd{C-c C-d} (@code{c-toggle-hungry-state}), and to toggle both states,
599 use @kbd{C-c C-t} (@code{c-toggle-auto-hungry-state}).
601 To set up the auto-newline and hungry-delete states to your preferred
602 values, you would need to add some lisp to your @file{.emacs} file that
603 called one of the @code{c-toggle-*-state} functions directly. When
604 called programmatically, each function takes a numeric value, where
605 a positive number enables the minor mode, a negative number disables the
606 mode, and zero toggles the current state of the mode.
608 So for example, if you wanted to enable both auto-newline and
609 hungry-delete for all your C file editing, you could add the following
610 to your @file{.emacs} file:
613 (add-hook 'c-mode-common-hook
614 '(lambda () (c-toggle-auto-hungry-state 1)))
619 @cindex electric characters
622 * Auto-newline Insertion::
623 * Hungry-deletion of Whitespace::
627 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
628 @node Auto-newline Insertion, Hungry-deletion of Whitespace, , Minor Modes
629 @comment node-name, next, previous, up
630 @section Auto-newline Insertion
631 @cindex auto-newline insertion
632 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
634 @cindex electric commands
635 Auto-newline minor mode works by enabling certain @dfn{electric
636 commands}. Electric commands are typically bound to special characters
637 such as the left and right braces, colons, semi-colons, etc., which when
638 typed, perform some magic formatting in addition to inserting the typed
639 character. As a general rule, electric commands are only electric when
640 the following conditions apply:
644 Auto-newline minor mode is enabled, as evidenced by a @samp{C/a} or
645 @samp{C/ah} indicator on the modeline.
648 @cindex syntactic whitespace
650 The character was not typed inside of a literal @footnote{A
651 @dfn{literal} is defined as any comment, string, or C preprocessor macro
652 definition. These constructs are also known as @dfn{syntactic
653 whitespace} since they are usually ignored when scanning C code.}.
657 No numeric argument was supplied to the command (i.e. it was typed as
658 normal, with no @kbd{C-u} prefix).
665 * Hanging Semi-colons and Commas::
666 * Other Electric Commands::
671 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
672 @node Hanging Braces, Hanging Colons, , Auto-newline Insertion
673 @comment node-name, next, previous, up
674 @subsection Hanging Braces
675 @cindex hanging braces
676 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
678 @findex c-electric-brace
679 @findex electric-brace (c-)
680 @vindex c-hanging-braces-alist
681 @vindex hanging-braces-alist (c-)
682 When you type either an open or close brace (i.e. @kbd{@{} or @kbd{@}}),
683 the electric command @code{c-electric-brace} gets run. This command has
684 two electric formatting behaviors. First, it will perform some
685 re-indentation of the line the brace was typed on, and second, it will
686 add various newlines before and/or after the typed brace.
687 Re-indentation occurs automatically whenever the electric behavior is
688 enabled. If the brace ends up on a line other than the one it was typed
689 on, then that line is also re-indented.
691 @cindex class-open syntactic symbol
692 @cindex class-close syntactic symbol
693 @cindex defun-open syntactic symbol
694 @cindex defun-close syntactic symbol
695 @cindex inline-open syntactic symbol
696 @cindex inline-close syntactic symbol
697 @cindex brace-list-open syntactic symbol
698 @cindex brace-list-close syntactic symbol
699 @cindex brace-list-intro syntactic symbol
700 @cindex brace-entry-open syntactic symbol
701 @cindex block-open syntactic symbol
702 @cindex block-close syntactic symbol
703 @cindex substatement-open syntactic symbol
704 @cindex statement-case-open syntactic symbol
705 @cindex extern-lang-open syntactic symbol
706 @cindex extern-lang-close syntactic symbol
707 @cindex namespace-open syntactic symbol
708 @cindex namespace-close syntactic symbol
709 @cindex inexpr-class-open symbol
710 @cindex inexpr-class-close symbol
712 The default in auto-newline mode is to insert newlines both before and
713 after a brace, but that can be controlled by the
714 @code{c-hanging-braces-alist} style variable. This variable contains a
715 mapping between syntactic symbols related to braces, and a list of
716 places to insert a newline. The syntactic symbols that are useful for
717 this list are: @code{class-open}, @code{class-close}, @code{defun-open},
718 @code{defun-close}, @code{inline-open}, @code{inline-close},
719 @code{brace-list-open}, @code{brace-list-close},
720 @code{brace-list-intro}, @code{brace-entry-open}, @code{block-open},
721 @code{block-close}, @code{substatement-open},
722 @code{statement-case-open}, @code{extern-lang-open},
723 @code{extern-lang-close}, @code{namespace-open}, @code{namespace-close},
724 @code{inexpr-class-open}, and @code{inexpr-class-close}@footnote{Note
725 that the aggregate constructs in Pike mode, @samp{(@{}, @samp{@})},
726 @samp{([}, @samp{])}, and @samp{(<}, @samp{>)}, do not count as brace
727 lists in this regard, even though they do for normal indentation
728 purposes. It's currently not possible to set automatic newlines on
729 these constructs.}. @xref{Syntactic Symbols}, for a more detailed
730 description of these syntactic symbols, except for
731 @code{inexpr-class-open} and @code{inexpr-class-close}, which aren't
732 actual syntactic symbols.
734 The braces of anonymous inner classes in Java are given the special
735 symbols @code{inexpr-class-open} and @code{inexpr-class-close}, so that
736 they can be distinguished from the braces of normal classes@footnote{The
737 braces of anonymous classes produces a combination of
738 @code{inexpr-class}, and @code{class-open} or @code{class-close} in
739 normal indentation analysis.}.
741 The value associated with each syntactic symbol in this association list
742 is called an @var{ACTION} which can be either a function or a list.
743 @xref{Custom Brace and Colon Hanging}, for a more detailed discussion of
744 using a function as a brace hanging @var{ACTION}.
746 When the @var{ACTION} is a list, it can contain any combination of the
747 symbols @code{before} and @code{after}, directing @ccmode{} where to
748 put newlines in relationship to the brace being inserted. Thus, if the
749 list contains only the symbol @code{after}, then the brace is said to
750 @dfn{hang} on the right side of the line, as in:
754 // here, open braces always `hang'
755 void spam( int i ) @{
765 When the list contains both @code{after} and @code{before}, the braces
766 will appear on a line by themselves, as shown by the close braces in the
767 above example. The list can also be empty, in which case no newlines
768 are added either before or after the brace.
770 If a syntactic symbol is missing entirely from
771 @code{c-hanging-braces-alist}, it's treated in the same way as an
772 @var{ACTION} with a list containing @code{before} and @code{after}, so
773 that braces by default end up on their own line.
775 For example, the default value of @code{c-hanging-braces-alist} is:
781 (substatement-open after)
782 (block-close . c-snug-do-while)
783 (extern-lang-open after)
784 (inexpr-class-open after)
785 (inexpr-class-close before))
790 @noindent which says that @code{brace-list-open} and
791 @code{brace-entry-open} braces should both hang on the right side, and
792 allow subsequent text to follow on the same line as the brace. Also,
793 @code{substatement-open}, @code{extern-lang-open}, and
794 @code{inexpr-class-open} braces should hang on the right side, but
795 subsequent text should follow on the next line. The opposite holds for
796 @code{inexpr-class-close} braces; they won't hang, but the following
797 text continues on the same line. Here, in the @code{block-close} entry,
798 you also see an example of using a function as an @var{ACTION}. In all
799 other cases, braces are put on a line by themselves.
801 A word of caution: it is not a good idea to hang top-level construct
802 introducing braces, such as @code{class-open} or @code{defun-open}.
803 Emacs makes an assumption that such braces will always appear in column
804 zero, hanging them can introduce performance problems.
805 @xref{Performance Issues}, for more information.
808 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
809 @node Hanging Colons, Hanging Semi-colons and Commas, Hanging Braces, Auto-newline Insertion
810 @comment node-name, next, previous, up
811 @subsection Hanging Colons
812 @cindex hanging colons
813 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
815 @vindex hanging-colons-alist (c-)
816 @vindex c-hanging-colons-alist
817 Using a mechanism similar to brace hanging (@pxref{Hanging Braces}),
818 colons can also be made to hang using the style variable
819 @code{c-hanging-colons-alist}. The syntactic symbols appropriate for
820 this assocation list are: @code{case-label}, @code{label},
821 @code{access-label}, @code{member-init-intro}, and @code{inher-intro}.
822 Note however that for @code{c-hanging-colons-alist}, @var{ACTION}s as
823 functions are not supported. See also @ref{Custom Brace and Colon
824 Hanging} for details.
826 In C++, double-colons are used as a scope operator but because these
827 colons always appear right next to each other, newlines before and after
828 them are controlled by a different mechanism, called @dfn{clean-ups} in
829 @ccmode{}. @xref{Clean-ups}, for details.
832 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
833 @node Hanging Semi-colons and Commas, Other Electric Commands, Hanging Colons, Auto-newline Insertion
834 @comment node-name, next, previous, up
835 @subsection Hanging Semi-colons and Commas
836 @cindex hanging semi-colons
837 @cindex hanging commas
838 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
840 Semicolons and commas are also electric in @ccmode{}, but since
841 these characters do not correspond directly to syntactic symbols, a
842 different mechanism is used to determine whether newlines should be
843 automatically inserted after these characters. @xref{Customizing
844 Semi-colons and Commas}, for details.
847 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
848 @node Other Electric Commands, Clean-ups, Hanging Semi-colons and Commas, Auto-newline Insertion
849 @comment node-name, next, previous, up
850 @subsection Other Electric Commands
851 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
854 @findex c-electric-pound
855 @vindex c-electric-pound-behavior
856 @findex electric-pound (c-)
857 @vindex electric-pound-behavior (c-)
858 A few other keys also provide electric behavior. For example
859 @kbd{#} (@code{c-electric-pound}) is electric when typed as
860 the first non-whitespace character on a line. In this case, the
861 variable @code{c-electric-pound-behavior} is consulted for the electric
862 behavior. This variable takes a list value, although the only element
863 currently defined is @code{alignleft}, which tells this command to force
864 the @samp{#} character into column zero. This is useful for entering
865 C preprocessor macro definitions.
867 @findex c-electric-star
868 @findex c-electric-slash
869 @findex electric-star (c-)
870 @findex electric-slash (c-)
871 Stars and slashes (i.e. @kbd{*} and @kbd{/}, @code{c-electric-star} and
872 @code{c-electric-slash} respectively) are also electric under
873 certain circumstances. If a star is inserted as the second character of
874 a C style block comment on a comment-only line, then the comment
875 delimiter is indented as defined by @code{c-offsets-alist}. A
876 comment-only line is defined as a line which contains only a comment, as
883 // this is a comment-only line...
884 if( i == 7 ) // but this is not
893 Likewise, if a slash is inserted as the second slash in a C++ style line
894 comment (also only on a comment-only line), then the line is indented as
895 defined by @code{c-offsets-alist}.
897 @findex c-electric-lt-gt
898 @findex electric-lt-gt (c-)
901 Less-than and greater-than signs (@code{c-electric-lt-gt}) are also
902 electric, but only in C++ mode. Hitting the second of two @kbd{<} or
903 @kbd{>} keys re-indents the line if it is a C++ style stream operator.
905 @findex c-electric-paren
906 @findex electric-paren (c-)
909 The normal parenthesis characters @samp{(} and @samp{)} also reindent
910 the current line if they are used in normal code. This is useful for
911 getting the closing parenthesis of an argument list aligned
915 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
916 @node Clean-ups, , Other Electric Commands, Auto-newline Insertion
917 @comment node-name, next, previous, up
918 @subsection Clean-ups
920 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
922 @dfn{Clean-ups} are a mechanism complementary to colon and brace
923 hanging. On the surface, it would seem that clean-ups overlap the
924 functionality provided by the @code{c-hanging-*-alist} variables, and
925 similarly, clean-ups are only enabled when auto-newline minor mode is
926 enabled. Clean-ups are used however to adjust code ``after-the-fact'',
927 i.e. to eliminate some whitespace that is inserted by electric
928 commands, or whitespace that contains intervening constructs.
930 @vindex c-cleanup-list
931 @vindex cleanup-list (c-)
933 You can configure @ccmode{}'s clean-ups by setting the style variable
934 @code{c-cleanup-list}, which is a list of clean-up symbols. By default,
935 @ccmode{} cleans up only the @code{scope-operator} construct, which is
936 necessary for proper C++ support. Note that clean-ups are only
937 performed when the construct does not occur within a literal
938 (@pxref{Auto-newline Insertion}), and when there is nothing but
939 whitespace appearing between the individual components of the construct.
941 There are currently only five specific constructs that @ccmode{}
942 can clean up, as indicated by these symbols:
946 @code{brace-else-brace} --- cleans up @samp{@} else @{} constructs by
947 placing the entire construct on a single line. Clean-up occurs when the
948 open brace after the @samp{else} is typed. So for example, this:
964 appears like this after the open brace is typed:
978 @code{brace-elseif-brace} --- similar to the @code{brace-else-brace}
979 clean-up, but this cleans up @samp{@} else if (...) @{} constructs. For
996 appears like this after the open parenthesis is typed:
1010 and like this after the open brace is typed:
1018 @} else if( i==3 ) @{
1024 @code{brace-catch-brace} --- analogous to @code{brace-elseif-brace}, but
1025 cleans up @samp{@} catch (...) @{} in C++ and Java mode.
1028 @code{empty-defun-braces} --- cleans up braces following a top-level
1029 function or class definition that contains no body. Clean up occurs
1030 when the closing brace is typed. Thus the following:
1041 is transformed into this when the close brace is typed:
1052 @code{defun-close-semi} --- cleans up the terminating semi-colon on
1053 top-level function or class definitions when they follow a close
1054 brace. Clean up occurs when the semi-colon is typed.
1055 So for example, the following:
1067 is transformed into this when the semi-colon is typed:
1080 @code{list-close-comma} --- cleans up commas following braces in array
1081 and aggregate initializers. Clean up occurs when the comma is typed.
1084 @code{scope-operator} --- cleans up double colons which may designate a
1085 C++ scope operator split across multiple lines@footnote{Certain C++
1086 constructs introduce ambiguous situations, so @code{scope-operator}
1087 clean-ups may not always be correct. This usually only occurs when
1088 scoped identifiers appear in switch label tags.}. Clean up occurs when
1089 the second colon is typed. You will always want @code{scope-operator}
1090 in the @code{c-cleanup-list} when you are editing C++ code.
1095 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1096 @node Hungry-deletion of Whitespace, , Auto-newline Insertion, Minor Modes
1097 @comment node-name, next, previous, up
1098 @section Hungry-deletion of Whitespace
1099 @cindex hungry-deletion of whitespace
1100 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1102 Hungry deletion of whitespace, or as it more commonly called,
1103 @dfn{hungry-delete mode}, is a simple feature that some people find
1104 extremely useful. In fact, you might find yourself wanting
1105 hungry-delete in @strong{all} your editing modes!
1109 In a nutshell, when hungry-delete mode is enabled, hitting the
1110 @key{Backspace} key@footnote{I say ``hit the @key{Backspace} key'' but
1111 what I really mean is ``when Emacs receives the @code{BackSpace} key
1112 event''. The difference usually isn't significant to most users, but
1113 advanced users will realize that under window systems such as X, any
1114 physical key (keycap) on the keyboard can be configured to generate any
1115 keysym, and thus any Emacs key event. Also, the use of Emacs on TTYs
1116 will affect which keycap generates which key event. From a pedantic
1117 point of view, here we are only concerned with the key event that
1118 Emacs receives.} will consume all preceding whitespace, including
1119 newlines and tabs. This can really cut down on the number of
1120 @key{Backspace}'s you have to type if, for example you made a mistake on
1123 @findex c-electric-backspace
1124 @findex electric-backspace (c-)
1125 @vindex c-backspace-function
1126 @vindex backspace-function (c-)
1128 @findex c-electric-delete
1129 @findex electric-delete (c-)
1130 @vindex c-delete-function
1131 @vindex delete-function (c-)
1134 @findex backward-delete-char-untabify
1136 By default, when you hit the @key{Backspace} key @ccmode{} runs the
1137 command @code{c-electric-backspace}, which deletes text in the backwards
1138 direction. When deleting a single character, or when @key{Backspace} is
1139 hit in a literal (@pxref{Auto-newline Insertion}), or when hungry-delete
1140 mode is disabled, the function contained in the
1141 @code{c-backspace-function} variable is called with one argument (the
1142 number of characters to delete). This variable is set to
1143 @code{backward-delete-char-untabify} by default.
1145 @vindex delete-key-deletes-forward
1148 The default behavior of the @key{Delete} key depends on the flavor of
1149 Emacs you are using. By default in XEmacs 20.3 and beyond, the
1150 @key{Delete} key is bound to @code{c-electric-delete}. You control the
1151 direction that the @key{Delete} key deletes by setting the variable
1152 @code{delete-key-deletes-forward}, a standard XEmacs variable. When
1153 this variable is non-@code{nil} and hungry-delete mode is enabled,
1154 @code{c-electric-delete} will consume all whitespace @emph{following}
1155 point. When @code{delete-key-deletes-forward} is @code{nil}, it deletes
1156 all whitespace @emph{preceding} point@footnote{i.e. it literally calls
1157 @code{c-electric-backspace}.} When deleting a single character, or if
1158 @key{Delete} is hit in a literal, or hungry-delete mode is disabled, the
1159 function contained in @code{c-delete-function} is called with one
1160 argument: the number of characters to delete. This variable is set to
1161 @code{delete-char} by default.
1163 In Emacs 19 or Emacs 20, both the @key{Delete} and @key{Backspace} keys
1164 are bound to @code{c-electric-backspace}, however you can change this by
1165 explicitly binding @code{[delete]}@footnote{E.g. to
1166 @code{c-electric-delete} in your @file{.emacs} file. Note however, that
1167 Emacs 20 does not have a standard variable such as
1168 @code{delete-key-deletes-forward}.}.
1170 XEmacsen older than 20.3 behave similar to Emacs 19 and Emacs 20.
1173 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1174 @node Text Filling and Line Breaking, Commands, Minor Modes, Top
1175 @comment node-name, next, previous, up
1176 @chapter Text Filling and Line Breaking
1177 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1179 Since there's a lot of normal text in comments and string literals,
1180 @ccmode{} provides features to edit these like in text mode. The goal
1181 is to do it as seamlessly as possible, i.e. you can use auto fill mode,
1182 sentence and paragraph movement, paragraph filling, adaptive filling etc
1183 wherever there's a piece of normal text without having to think much
1184 about it. @ccmode{} should keep the indentation, fix the comment line
1185 decorations, and so on, for you. It does that by hooking in on the
1186 different line breaking functions and tuning relevant variables as
1189 @vindex c-comment-prefix-regexp
1190 @vindex comment-prefix-regexp (c-)
1191 @cindex comment line prefix
1192 @vindex comment-start
1194 @vindex comment-start-skip
1195 @vindex paragraph-start
1196 @vindex paragraph-separate
1197 @vindex paragraph-ignore-fill-prefix
1198 @vindex adaptive-fill-mode
1199 @vindex adaptive-fill-regexp
1200 @vindex adaptive-fill-first-line-regexp
1201 To make Emacs recognize comments and treat text in them as normal
1202 paragraphs, @ccmode{} makes several standard
1203 variables@footnote{@code{comment-start}, @code{comment-end},
1204 @code{comment-start-skip}, @code{paragraph-start},
1205 @code{paragraph-separate}, @code{paragraph-ignore-fill-prefix},
1206 @code{adaptive-fill-mode}, @code{adaptive-fill-regexp}, and
1207 @code{adaptive-fill-first-line-regexp}.} buffer local and modifies them
1208 according to the language syntax and the style of line decoration that
1209 starts every line in a comment. The style variable
1210 @code{c-comment-prefix-regexp} contains the regexp used to recognize
1211 this @dfn{comment line prefix}. The default is @samp{//+\\|\\**}, which
1212 matches C++ style line comments like
1220 with two or more slashes in front of them, and C style block comments
1233 with zero or more stars at the beginning of every line. If you change
1234 that variable, please make sure it still matches the comment starter
1235 (i.e. @code{//}) of line comments @emph{and} the line prefix inside
1236 block comments. Also note that since @ccmode{} uses the value of
1237 @code{c-comment-prefix-regexp} to set up several other variables at mode
1238 initialization, you need to reinitialize the program mode if you change
1239 it inside a @ccmode{} buffer.
1241 @findex auto-fill-mode
1242 @cindex auto fill mode
1243 @cindex paragraph fill
1244 Line breaks are by default handled (almost) the same regardless whether
1245 they are made by auto fill mode (@pxref{Auto Fill,,, emacs, The Emacs
1246 Editor}), paragraph filling (e.g. with @kbd{M-q}), or explicitly with
1247 @kbd{M-j} or similar methods. In string literals, the new line gets the
1248 same indentation as the previous nonempty line (may be changed with the
1249 @code{string} syntactic symbol). In comments, @ccmode{} uses
1250 @code{c-comment-prefix-regexp} to adapt the line prefix from the other
1251 lines in the comment.
1253 @vindex adaptive-fill-mode
1254 @cindex adaptive fill mode
1255 @ccmode{} uses adaptive fill mode (@pxref{Adaptive Fill,,, emacs, The
1256 Emacs Editor}) to make Emacs correctly keep the line prefix when filling
1257 paragraphs. That also makes Emacs preserve the text indentation
1258 @emph{inside} the comment line prefix. E.g. in the following comment,
1259 both paragraphs will be filled with the left margins kept intact:
1263 /* Make a balanced b-tree of the nodes in the incoming
1264 * stream. But, to quote the famous words of Donald E.
1267 * Beware of bugs in the above code; I have only
1268 * proved it correct, not tried it.
1274 @findex c-setup-filladapt
1275 @findex setup-filladapt (c-)
1276 @findex filladapt-mode
1277 @vindex filladapt-mode
1278 @cindex Filladapt mode
1279 It's also possible to use other adaptive filling packages, notably Kyle
1280 E. Jones' Filladapt package@footnote{It's available from
1281 @uref{http://www.wonderworks.com/}. As of version 2.12, it does however
1282 lack a feature that makes it work suboptimally when
1283 @code{c-comment-prefix-regexp} matches the empty string (which it does
1284 by default). A patch for that is available from
1285 @uref{http://cc-mode.sourceforge.net/,, the CC Mode site}.},
1286 which handles things like bulleted lists nicely. There's a convenience
1287 function @code{c-setup-filladapt} that tunes the relevant variables in
1288 Filladapt for use in @ccmode{}. Call it from a mode hook, e.g. with
1289 something like this in your @file{.emacs}:
1293 (defun my-c-mode-common-hook ()
1296 (add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
1301 @vindex c-block-comment-prefix
1302 @vindex block-comment-prefix (c-)
1303 @vindex c-comment-continuation-stars
1304 @vindex comment-continuation-stars (c-)
1305 Normally the comment line prefix inserted for a new line inside a
1306 comment is deduced from other lines in it. However there's one
1307 situation when there's no clue about how the prefix should look, namely
1308 when a block comment is broken for the first time. The string in the
1309 style variable @code{c-block-comment-prefix}@footnote{In versions before
1310 5.26, this variable was called @code{c-comment-continuation-stars}. As
1311 a compatibility measure, @ccmode{} still uses the value on that variable
1312 if it's set.} is used in that case. It defaults to @samp{* }, which
1316 /* Got O(n^2) here, which is a Bad Thing. */
1326 * which is a Bad Thing. */
1331 Note that it won't work to justify the indentation by putting leading
1332 spaces in the @code{c-block-comment-prefix} string, since @ccmode{}
1333 still uses the normal indentation engine to indent the line. Thus, the
1334 right way to fix the indentation is by setting the @code{c} syntactic
1335 symbol. It defaults to @code{c-lineup-C-comments}, which handles the
1336 indentation of most common comment styles, see @ref{Indentation
1339 @vindex c-ignore-auto-fill
1340 @vindex ignore-auto-fill (c-)
1341 When auto fill mode is enabled, @ccmode{} can selectively ignore it
1342 depending on the context the line break would occur in, e.g. to never
1343 break a line automatically inside a string literal. This behavior can
1344 be controlled with the @code{c-ignore-auto-fill} variable. It takes a
1345 list of symbols for the different contexts where auto-filling never
1349 @item @code{string} --- Inside a string or character literal.
1350 @item @code{c} --- Inside a C style block comment.
1351 @item @code{c++} --- Inside a C++ style line comment.
1352 @item @code{cpp} --- Inside a preprocessor directive.
1353 @item @code{code} --- Anywhere else, i.e. in normal code.
1356 By default, @code{c-ignore-auto-fill} is set to @code{'(string cpp
1357 code)}, which means that auto-filling only occurs in comments when
1358 auto-fill mode is activated. In literals, it's often desirable to have
1359 explicit control over newlines. In preprocessor directives, the
1360 necessary @samp{\} escape character before the newline is not
1361 automatically inserted, so an automatic line break would produce invalid
1362 code. In normal code, line breaks are normally dictated by some logical
1363 structure in the code rather than the last whitespace character, so
1364 automatic line breaks there will produce poor results in the current
1367 The commands that does the actual work follows.
1372 @findex c-fill-paragraph
1373 @findex fill-paragraph (c-)
1374 @cindex Javadoc markup
1375 @item @kbd{M-q} (@code{c-fill-paragraph})
1376 This is the replacement for @code{fill-paragraph} in @ccmode{}
1377 buffers. It's used to fill multiline string literals and both block and
1378 line style comments. In Java buffers, the Javadoc markup words are
1379 recognized as paragraph starters.
1381 The function keeps the comment starters and enders of block comments as
1382 they were before the filling. This means that a comment ender on the
1383 same line as the paragraph being filled will be filled with the
1384 paragraph, and one on a line by itself will stay as it is. The comment
1385 starter is handled similarly@footnote{This means that the variables
1386 @code{c-hanging-comment-starter-p} and @code{c-hanging-comment-ender-p},
1387 which controlled this behavior in earlier versions of @ccmode{}, are now
1391 @findex c-indent-new-comment-line
1392 @findex indent-new-comment-line (c-)
1393 @item @kbd{M-j} (@code{c-indent-new-comment-line})
1394 This is the replacement for @code{indent-new-comment-line}. It breaks
1395 the line at point and indents the new line like the current one.
1397 @vindex comment-multi-line
1398 If inside a comment and @code{comment-multi-line} is non-@code{nil}, the
1399 indentation and line prefix are preserved. If inside a comment and
1400 @code{comment-multi-line} is @code{nil}, a new comment of the same type
1401 is started on the next line and indented as appropriate for comments.
1403 @findex c-context-line-break
1404 @findex context-line-break (c-)
1405 @item @kbd{M-x c-context-line-break}
1406 This is a function that works like @code{indent-new-comment-line} in
1407 comments and @code{newline-and-indent} elsewhere, thus combining those
1408 two in a way that uses each one in the context it's best suited for.
1409 I.e. in comments the comment line prefix and indentation is kept for the
1410 new line, and in normal code it's indented according to context by the
1413 It's not bound to a key by default, but it's intended to be used on the
1414 @kbd{RET} key. If you like the behavior of @code{newline-and-indent} on
1415 @kbd{RET}, you might consider switching to this function.
1420 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1421 @node Commands, Customizing Indentation, Text Filling and Line Breaking, Top
1422 @comment node-name, next, previous, up
1424 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1427 * Indentation Commands::
1428 * Movement Commands::
1432 See also @ref{Text Filling and Line Breaking}, for commands concerning
1436 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1437 @node Indentation Commands, Movement Commands, , Commands
1438 @comment node-name, next, previous,up
1439 @section Indentation Commands
1440 @cindex indentation commands
1441 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1443 The following list of commands re-indent C constructs. Note that when
1444 you change your coding style, either interactively or through some other
1445 means, your file does @emph{not} automatically get re-indented. You
1446 will need to execute one of the following commands to see the effects of
1449 @cindex GNU indent program
1450 Also, variables like @code{c-hanging-*} and @code{c-cleanup-list}
1451 only affect how on-the-fly code is formatted. Changing the
1452 ``hanginess'' of a brace and then re-indenting, will not move the brace
1453 to a different line. For this, you're better off getting an external
1454 program like GNU @code{indent}, which will re-arrange brace location,
1457 Re-indenting large sections of code can take a long time. When
1458 @ccmode{} reindents a region of code, it is essentially equivalent to
1459 hitting @kbd{TAB} on every line of the region. Especially vulnerable is
1460 code generator output@footnote{In particular, I have had people
1461 complain about the speed with which @code{lex(1)} output is re-indented.
1462 Lex, yacc, and other code generators usually output some pretty
1463 perversely formatted code. Re-indenting such code will be slow.}.
1465 These commands are useful when indenting code:
1470 @findex c-indent-command
1471 @findex indent-command (c-)
1472 @item @kbd{TAB} (@code{c-indent-command})
1473 Indents the current line. The actual behavior is controlled by several
1474 variables, described below. See @code{c-tab-always-indent},
1475 @code{c-insert-tab-function}, and @code{indent-tabs-mode}. With a
1476 numeric argument, this command rigidly indents the region, preserving
1477 the relative indentation among the lines.
1480 @findex c-indent-exp
1481 @findex indent-exp (c-)
1482 @item @kbd{M-C-q} (@code{c-indent-exp})
1483 Indent an entire balanced brace or parenthesis expression. Note that
1484 point must be on the opening brace or parenthesis of the expression you
1488 @findex c-indent-defun
1489 @findex indent-defun (c-)
1490 @item @kbd{C-c C-q} (@code{c-indent-defun})
1491 Indents the entire top-level function or class definition encompassing
1492 point. It leaves point unchanged. This function can't be used to
1493 re-indent a nested brace construct, such as a nested class or function,
1494 or a Java method. The top-level construct being re-indented must be
1495 complete, i.e. it must have both a beginning brace and an ending brace.
1498 @findex indent-region
1499 @item @kbd{M-C-\} (@code{indent-region})
1500 Indents an arbitrary region of code. This is a standard Emacs command,
1501 tailored for C code in a @ccmode{} buffer. Note that of course, point
1502 and mark must delineate the region you want to indent.
1505 @findex c-mark-function
1506 @findex mark-function (c-)
1507 @item @kbd{M-C-h} (@code{c-mark-function})
1508 While not strictly an indentation command, this is useful for marking
1509 the current top-level function or class definition as the current
1510 region. As with @code{c-indent-defun}, this command operates on
1511 top-level constructs, and can't be used to mark say, a Java method.
1515 These variables are also useful when indenting code:
1519 @vindex c-tab-always-indent
1520 @vindex tab-always-indent (c-)
1523 @item c-tab-always-indent
1524 This variable controls how @kbd{TAB} @code{c-indent-command} operates.
1525 When this variable is @code{t}, @kbd{TAB} always just indents the
1526 current line. When it is @code{nil}, the line is indented only if point
1527 is at the left margin, or on or before the first non-whitespace
1528 character on the line, otherwise some whitespace is inserted. If this
1529 variable is the symbol @code{other}, then some whitespace is inserted
1530 only within strings and comments (literals), an inside preprocessor
1531 directives, but the line is always reindented.
1533 @vindex c-insert-tab-function
1534 @vindex insert-tab-function (c-)
1535 @findex tab-to-tab-stop
1536 @item c-insert-tab-function
1537 When ``some whitespace'' is inserted as described above, what actually
1538 happens is that the function stored in @code{c-insert-tab-function} is
1539 called. Normally, this just inserts a real tab character, or the
1540 equivalent number of spaces, depending on @code{indent-tabs-mode}.
1541 Some people, however, set @code{c-insert-tab-function} to
1542 @code{tab-to-tab-stop} so as to get hard tab stops when indenting.
1544 @vindex indent-tabs-mode
1545 @item indent-tabs-mode
1546 This is a standard Emacs variable that controls how line indentation is
1547 composed. When this variable is non-@code{nil}, then tabs can be used
1548 in a line's indentation, otherwise only spaces can be used.
1550 @vindex c-progress-interval
1551 @vindex progress-interval (c-)
1552 @item c-progress-interval
1553 When indenting large regions of code, this variable controls how often a
1554 progress message is displayed. Set this variable to @code{nil} to
1555 inhibit the progress messages, or set it to an integer which is the
1556 interval in seconds that progress messages are displayed.
1561 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1562 @node Movement Commands, Other Commands, Indentation Commands, Commands
1563 @comment node-name, next, previous, up
1564 @section Movement Commands
1565 @cindex movement commands
1566 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1568 @ccmode{} contains some useful command for moving around in C
1573 @findex c-beginning-of-defun
1574 @findex beginning-of-defun (c-)
1575 @findex beginning-of-defun
1576 @item @kbd{M-x c-beginning-of-defun}
1577 Moves point back to the least-enclosing brace. This function is
1578 analogous to the Emacs built-in command @code{beginning-of-defun},
1579 except it eliminates the constraint that the top-level opening brace
1580 must be in column zero. See @code{beginning-of-defun} for more
1583 Depending on the coding style being used, you might prefer
1584 @code{c-beginning-of-defun} to @code{beginning-of-defun}. If so,
1585 consider binding @kbd{C-M-a} to the former instead. For backwards
1586 compatibility reasons, the default binding remains in effect.
1588 @findex c-end-of-defun
1589 @findex end-of-defun (c-)
1590 @findex end-of-defun
1591 @item @kbd{M-x c-end-of-defun}
1592 Moves point to the end of the current top-level definition. This
1593 function is analogous to the Emacs built-in command @code{end-of-defun},
1594 except it eliminates the constraint that the top-level opening brace of
1595 the defun must be in column zero. See @code{beginning-of-defun} for more
1598 Depending on the coding style being used, you might prefer
1599 @code{c-end-of-defun} to @code{end-of-defun}. If so,
1600 consider binding @kbd{C-M-e} to the former instead. For backwards
1601 compatibility reasons, the default binding remains in effect.
1604 @findex c-up-conditional
1605 @findex up-conditional (c-)
1606 @item @kbd{C-c C-u} (@code{c-up-conditional})
1607 Move point back to the containing preprocessor conditional, leaving the
1608 mark behind. A prefix argument acts as a repeat count. With a negative
1609 argument, move point forward to the end of the containing
1610 preprocessor conditional.
1612 @samp{#elif} is treated like @samp{#else} followed by @samp{#if}, so the
1613 function stops at them when going backward, but not when going forward.
1615 @findex c-up-conditional-with-else
1616 @findex up-conditional-with-else (c-)
1617 @item @kbd{M-x c-up-conditional-with-else}
1618 A variety of @code{c-up-conditional} that also stops at @samp{#else}
1619 lines. Normally those lines are ignored.
1621 @findex c-down-conditional
1622 @findex down-conditional (c-)
1623 @item @kbd{M-x c-down-conditional}
1624 Move point forward into the next nested preprocessor conditional,
1625 leaving the mark behind. A prefix argument acts as a repeat count.
1626 With a negative argument, move point backward into the previous
1627 nested preprocessor conditional.
1629 @samp{#elif} is treated like @samp{#else} followed by @samp{#if}, so the
1630 function stops at them when going forward, but not when going backward.
1632 @findex c-down-conditional-with-else
1633 @findex down-conditional-with-else (c-)
1634 @item @kbd{M-x c-down-conditional-with-else}
1635 A variety of @code{c-down-conditional} that also stops at @samp{#else}
1636 lines. Normally those lines are ignored.
1639 @findex c-backward-conditional
1640 @findex backward-conditional (c-)
1641 @item @kbd{C-c C-p} (@code{c-backward-conditional})
1642 Move point back over a preprocessor conditional, leaving the mark
1643 behind. A prefix argument acts as a repeat count. With a negative
1644 argument, move forward.
1647 @findex c-forward-conditional
1648 @findex forward-conditional (c-)
1649 @item @kbd{C-c C-n} (@code{c-forward-conditional})
1650 Move point forward across a preprocessor conditional, leaving the mark
1651 behind. A prefix argument acts as a repeat count. With a negative
1652 argument, move backward.
1655 @findex c-beginning-of-statement
1656 @findex beginning-of-statement (c-)
1657 @item @kbd{M-a} (@code{c-beginning-of-statement})
1658 Move point to the beginning of the innermost C statement. If point is
1659 already at the beginning of a statement, it moves to the beginning of
1660 the closest preceding statement, even if that means moving into a block
1661 (you can use @kbd{M-C-b} to move over a balanced block). With prefix
1662 argument @var{n}, move back @var{n} @minus{} 1 statements.
1664 If point is within a comment, or next to a comment, this command moves
1665 by sentences instead of statements.
1667 When called from a program, this function takes three optional
1668 arguments: the numeric prefix argument, a buffer position limit which is
1669 the farthest back to search, and a flag to enable moving by sentence
1673 @findex c-end-of-statement
1674 @findex end-of-statement (c-)
1675 @item @kbd{M-e} (@code{c-end-of-statement})
1676 Move point to the end of the innermost C statement. If point is at the
1677 end of a statement, move to the end of the next statement, even if it's
1678 inside a nested block (use @kbd{M-C-f} to move to the other side of the
1679 block). With prefix argument @var{n}, move forward @var{n} @minus{} 1
1682 If point is within a comment, or next to a comment, this command moves
1683 by sentences instead of statements.
1685 When called from a program, this function takes three optional
1686 arguments: the numeric prefix argument, a buffer position limit which is
1687 the farthest back to search, and a flag to enable moving by sentence
1690 @findex c-forward-into-nomenclature
1691 @findex forward-into-nomenclature (c-)
1692 @item @kbd{M-x c-forward-into-nomenclature}
1693 A popular programming style, especially for object-oriented languages
1694 such as C++ is to write symbols in a mixed case format, where the first
1695 letter of each word is capitalized, and not separated by underscores.
1696 E.g. @samp{SymbolsWithMixedCaseAndNoUnderlines}.
1698 This command moves point forward to next capitalized word. With prefix
1699 argument @var{n}, move @var{n} times.
1701 @findex c-backward-into-nomenclature
1702 @findex backward-into-nomenclature (c-)
1703 @item @kbd{M-x c-backward-into-nomenclature}
1704 Move point backward to beginning of the next capitalized
1705 word. With prefix argument @var{n}, move @var{n} times. If
1706 @var{n} is negative, move forward.
1711 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1712 @node Other Commands, , Movement Commands, Commands
1713 @comment node-name, next, previous, up
1714 @section Other Commands
1715 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1717 @ccmode{} contains a few other useful commands:
1722 @findex c-scope-operator
1723 @findex scope-operator (c-)
1724 @item @kbd{C-c :} (@code{c-scope-operator})
1725 In C++, it is also sometimes desirable to insert the double-colon scope
1726 operator without performing the electric behavior of colon insertion.
1727 @kbd{C-c :} does just this.
1730 @findex c-backslash-region
1731 @findex backslash-region (c-)
1732 @item @kbd{C-c C-\} (@code{c-backslash-region})
1733 This function is handy when editing macros split over several lines by
1734 ending each line with a backslash. It inserts and aligns, or deletes
1735 these end-of-line backslashes in the current region.
1737 @vindex c-backslash-column
1738 @vindex backslash-column (c-)
1739 With no prefix argument, it inserts any missing backslashes and aligns
1740 them to the column specified by the @code{c-backslash-column} style
1741 variable. With a prefix argument, it deletes any backslashes.
1743 The function does not modify blank lines at the start of the region. If
1744 the region ends at the start of a line, it always deletes the backslash
1745 (if any) at the end of the previous line.
1750 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1751 @node Customizing Indentation, Syntactic Symbols, Commands, Top
1752 @comment node-name, next, previous, up
1753 @chapter Customizing Indentation
1754 @cindex customizing indentation
1755 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1757 @vindex c-offsets-alist
1758 @vindex offsets-alist (c-)
1759 The style variable @code{c-offsets-alist} contains the mappings between
1760 syntactic symbols and the offsets to apply for those symbols. It's set
1761 at mode initialization from a @emph{style} you may specify. Styles are
1762 groupings of syntactic symbol offsets and other style variable values.
1763 Most likely, you'll find that one of the pre-defined styles will suit
1764 your needs. @xref{Styles}, for an explanation of how to set up named
1767 Only syntactic symbols not already bound on @code{c-offsets-alist} will
1768 be set from styles. This means that any association you set on it, be
1769 it before or after mode initialization, will not be changed. The
1770 @code{c-offsets-alist} variable may therefore be used from e.g. the
1771 Customization interface@footnote{Available in Emacs 20 and later, and
1772 XEmacs 19.15 and later.} to easily change indentation offsets without
1773 having to bother about styles. Initially @code{c-offsets-alist} is
1774 empty, so that all syntactic symbols are set by the style system.
1777 @findex c-set-offset
1778 @findex set-offset (c-)
1779 You can use the command @kbd{C-c C-o} (@code{c-set-offset}) as the way
1780 to set offsets, both interactively and from your mode
1781 hook@footnote{Obviously, you use the keybinding interactively, and the
1782 function call programmatically!}.
1784 @vindex c-basic-offset
1785 @vindex basic-offset (c-)
1786 The offset associated with any particular syntactic symbol can be any of
1787 an integer, a function or lambda expression, a variable name, or one of
1788 the following symbols: @code{+}, @code{-}, @code{++}, @code{--},
1789 @code{*}, or @code{/}. These latter describe offset in multiples of the
1790 value of the style variable @code{c-basic-offset}. By defining a
1791 style's indentation in terms of this fundamental variable, you can
1792 change the amount of whitespace given to an indentation level while
1793 maintaining the same basic shape of your code. Here are the values that
1794 the special symbols correspond to:
1799 @code{c-basic-offset} times 1
1801 @code{c-basic-offset} times -1
1803 @code{c-basic-offset} times 2
1805 @code{c-basic-offset} times -2
1807 @code{c-basic-offset} times 0.5
1809 @code{c-basic-offset} times -0.5
1813 @cindex indentation functions
1815 When a function is used as offset, it's called an @dfn{indentation
1816 function}. Such functions are useful when more context than just the
1817 syntactic symbol is needed to get the desired indentation.
1818 @xref{Indentation Functions}, and @ref{Custom Indentation Functions},
1819 for details about them.
1821 @vindex c-strict-syntax-p
1822 @vindex strict-syntax-p (c-)
1823 The offset can also be a list, in which case it is evaluated recursively
1824 using the semantics described above. The first element of the list that
1825 returns a non-@code{nil} value succeeds and the evaluation stops. If
1826 none of the list elements return a non-@code{nil} value, then an offset
1827 of 0 (zero) is used@footnote{There is however a variable
1828 @code{c-strict-syntax-p} that, when set to non-@code{nil}, will cause an
1829 error to be signalled in that case. It's now considered obsolete since
1830 it doesn't work well with some of the alignment functions that now
1831 returns @code{nil} instead of zero to be more usable in lists. You
1832 should therefore leave @code{c-strict-syntax-p} set to @code{nil}.}.
1834 So, for example, because most of the default offsets are defined in
1835 terms of @code{+}, @code{-}, and @code{0}, if you like the general
1836 indentation style, but you use 4 spaces instead of 2 spaces per level,
1837 you can probably achieve your style just by changing
1838 @code{c-basic-offset} like so@footnote{You can try this interactively in
1839 a C buffer by typing the text that appears in italics.}:
1842 @emph{M-x set-variable RET}
1843 Set variable: @emph{c-basic-offset RET}
1844 Set c-basic-offset to value: @emph{4 RET}
1853 int add( int val, int incr, int doit )
1857 return( val + incr );
1869 int add( int val, int incr, int doit )
1873 return( val + incr );
1881 To change indentation styles more radically, you will want to change the
1882 offsets associated with other syntactic symbols. First, I'll show you
1883 how to do that interactively, then I'll describe how to make changes to
1884 your @file{.emacs} file so that your changes are more permanent.
1887 * Interactive Customization::
1888 * Permanent Customization::
1891 * Advanced Customizations::
1895 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1896 @node Interactive Customization, Permanent Customization, , Customizing Indentation
1897 @comment node-name, next, previous, up
1898 @section Interactive Customization
1899 @cindex interactive customization
1900 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1902 As an example of how to customize indentation, let's change the
1903 style of this example@footnote{In this an subsequent examples, the
1904 original code is formatted using the @samp{gnu} style unless otherwise
1905 indicated. @xref{Styles}.}:
1909 1: int add( int val, int incr, int doit )
1913 5: return( val + incr );
1925 1: int add( int val, int incr, int doit )
1929 5: return( val + incr );
1937 In other words, we want to change the indentation of braces that open a
1938 block following a condition so that the braces line up under the
1939 conditional, instead of being indented. Notice that the construct we
1940 want to change starts on line 4. To change the indentation of a line,
1941 we need to see which syntactic components affect the offset calculations
1942 for that line. Hitting @kbd{C-c C-s} on line 4 yields:
1945 ((substatement-open . 44))
1950 so we know that to change the offset of the open brace, we need to
1951 change the indentation for the @code{substatement-open} syntactic
1952 symbol. To do this interactively, just hit @kbd{C-c C-o}. This prompts
1953 you for the syntactic symbol to change, providing a reasonable default.
1954 In this case, the default is @code{substatement-open}, which is just the
1955 syntactic symbol we want to change!
1957 After you hit return, @ccmode{} will then prompt you for the new
1958 offset value, with the old value as the default. The default in this
1959 case is @samp{+}, but we want no extra indentation so enter
1960 @samp{0} and @kbd{RET}. This will associate the offset 0 with the
1961 syntactic symbol @code{substatement-open}.
1963 To check your changes quickly, just hit @kbd{C-c C-q}
1964 (@code{c-indent-defun}) to reindent the entire function. The example
1965 should now look like:
1969 1: int add( int val, int incr, int doit )
1973 5: return( val + incr );
1981 Notice how just changing the open brace offset on line 4 is all we
1982 needed to do. Since the other affected lines are indented relative to
1983 line 4, they are automatically indented the way you'd expect. For more
1984 complicated examples, this may not always work. The general approach to
1985 take is to always start adjusting offsets for lines higher up in the
1986 file, then re-indent and see if any following lines need further
1990 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1991 @node Permanent Customization, Hooks, Interactive Customization, Customizing Indentation
1992 @comment node-name, next, previous, up
1993 @section Permanent Customization
1994 @cindex permanent customization
1995 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1997 To make your changes permanent, you need to add some lisp code to your
1998 @file{.emacs} file. @ccmode{} supports many different ways to be
1999 configured, from the straightforward way by setting variables globally
2000 in @file{.emacs} or in the Customization interface, to the complex and
2001 precisely controlled way by using styles and hook functions.
2003 The simplest way of customizing @ccmode{} permanently is to set the
2004 variables in your @file{.emacs} with @code{setq} and similar commands.
2005 So to make the setting of @code{substatement-open} permanent, add this
2006 to the @file{.emacs} file:
2011 (c-set-offset 'substatement-open 0)
2016 The @code{require} line is only needed once in the beginning to make
2017 sure @ccmode{} is loaded so that the @code{c-set-offset} function is
2020 You can also use the more user friendly Customization interface, but
2021 this manual does not cover how that works.
2023 Variables set like this at the top level in @file{.emacs} take effect in
2024 all @ccmode{} buffers, regardless of language. The indentation style
2025 related variables, e.g. @code{c-basic-offset}, that you don't set this
2026 way get their value from the style system (@pxref{Styles}), and they
2027 therefore depend on the setting of @code{c-default-style}. Note that if
2028 you use Customize, this means that the greyed-out default values
2029 presented there might not be the ones you actually get, since the actual
2030 values depend on the style, which may very well be different for
2031 different languages.
2033 If you want to make more advanced configurations, e.g. language-specific
2034 customization, global variable settings isn't enough. For that you can
2035 use the language hooks, see @ref{Hooks}, and/or the style system, see
2038 @vindex c-style-variables-are-local-p
2039 @vindex style-variables-are-local-p (c-)
2040 By default, all style variables are global, so that every buffer will
2041 share the same style settings. This is fine if you primarily edit one
2042 style of code, but if you edit several languages and want to use
2043 different styles for them, you need finer control by making the style
2044 variables buffer local. The recommended way to do this is to set the
2045 variable @code{c-style-variables-are-local-p} to @code{t}. The
2046 variables will be made buffer local when @ccmode{} is activated in a
2047 buffer for the first time in the Emacs session. Note that once the
2048 style variables are made buffer local, they cannot be made global again,
2049 without restarting Emacs.
2052 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2053 @node Hooks, Styles, Permanent Customization, Customizing Indentation
2054 @comment node-name, next, previous, up
2057 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2059 @vindex c-mode-common-hook
2060 @vindex mode-common-hook (c-)
2062 @vindex c++-mode-hook
2063 @vindex objc-mode-hook
2064 @vindex java-mode-hook
2065 @vindex idl-mode-hook
2066 @vindex pike-mode-hook
2067 @vindex c-initialization-hook
2068 @vindex initialization-hook (c-)
2069 @ccmode{} provides several hooks that you can use to customize the mode
2070 according to your coding style. Each language mode has its own hook,
2071 adhering to standard Emacs major mode conventions. There is also one
2072 general hook and one package initialization hook:
2077 @code{c-mode-hook} --- For C buffers only.
2079 @code{c++-mode-hook} --- For C++ buffers only.
2081 @code{objc-mode-hook} --- For Objective-C buffers only.
2083 @code{java-mode-hook} --- For Java buffers only.
2085 @code{idl-mode-hook} --- For CORBA IDL buffers only.
2087 @code{pike-mode-hook} --- For Pike buffers only.
2089 @code{c-mode-common-hook} --- Common across all languages.
2091 @code{c-initialization-hook} --- Hook run only once per Emacs session,
2092 when @ccmode{} is initialized.
2096 The language hooks get run as the last thing when you enter that
2097 language mode. The @code{c-mode-common-hook} is run by all supported
2098 modes @emph{before} the language specific hook, and thus can contain
2099 customizations that are common across all languages. Most of the
2100 examples in this section will assume you are using the common
2101 hook.@footnote{@code{java-mode} and the hook variables interact in a
2102 slightly different way than the other modes. @code{java-mode} normally
2103 sets the style of the buffer to @samp{java} @emph{before} running the
2104 @code{c-mode-common-hook} or @code{java-mode-hook}. You need to be
2105 aware of this so that style settings in @code{c-mode-common-hook} don't
2106 clobber your Java style. This is arguably bogus, but it's kept for
2107 historical reasons. @xref{Built-in Styles}, the documentation of
2108 @code{c-default-style}, for details.}
2110 Here's a simplified example of what you can add to your @file{.emacs}
2111 file to do things whenever any @ccmode{} language is edited. See the
2112 Emacs manuals for more information on customizing Emacs via hooks.
2113 @xref{Sample .emacs File}, for a more complete sample @file{.emacs}
2118 (defun my-c-mode-common-hook ()
2119 ;; my customizations for all of c-mode and related modes
2120 (no-case-fold-search)
2122 (add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
2128 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2129 @node Styles, Advanced Customizations, Hooks, Customizing Indentation
2130 @comment node-name, next, previous, up
2133 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2135 Most people only need to edit code formatted in just a few well-defined
2136 and consistent styles. For example, their organization might impose a
2137 ``blessed'' style that all its programmers must conform to. Similarly,
2138 people who work on GNU software will have to use the GNU coding style.
2139 Some shops are more lenient, allowing a variety of coding styles, and as
2140 programmers come and go, there could be a number of styles in use. For
2141 this reason, @ccmode{} makes it convenient for you to set up logical
2142 groupings of customizations called @dfn{styles}, associate a single name
2143 for any particular style, and pretty easily start editing new or
2144 existing code using these styles.
2146 @cindex style variables
2147 The variables that the style system affect are called @dfn{style
2148 variables}. They are handled specially in several ways:
2152 Style variables are by default global variables, i.e. they have the same
2153 value in all Emacs buffers. However, they can instead be made always
2154 buffer local by setting @code{c-style-variables-are-local-p} to
2155 non-@code{nil} before @ccmode{} is initialized.
2157 @vindex c-old-style-variable-behavior
2158 @vindex old-style-variable-behavior (c-)
2160 The default value of any style variable (with two exceptions --- see
2161 below) is the special symbol @code{set-from-style}. Variables that are
2162 still set to that symbol when a @ccmode{} buffer is initialized will be
2163 set according to the current style, otherwise they will keep their
2164 current value@footnote{This is a big change from versions of @ccmode{}
2165 earlier than 5.26, where such settings would get overridden by the style
2166 system unless special precautions were taken. That was changed since it
2167 was counterintuitive and confusing, especially to novice users. If your
2168 configuration depends on the old overriding behavior, you can set the
2169 variable @code{c-old-style-variable-behavior} to non-@code{nil}.}.
2171 Note that when we talk about the ``default value'' for a style variable,
2172 we don't mean the @code{set-from-style} symbol that all style variables
2173 are set to initially, but instead the value it will get at mode
2174 initialization when neither a style nor a global setting has set its
2177 The style variable @code{c-offsets-alist} is handled a little
2178 differently from the other style variables. It's an association list,
2179 and is thus by default set to the empty list, @code{nil}. When the
2180 style system is initialized, any syntactic symbols already on it are
2181 kept --- only the missing ones are filled in from the chosen style.
2183 The style variable @code{c-special-indent-hook} is also handled in a
2184 special way. Styles may only add more functions on this hook, so the
2185 global settings on it are always preserved@footnote{This did not change
2189 The global settings of style variables get captured in the special
2190 @code{user} style, which is used as the base for all the other styles.
2191 @xref{Built-in Styles}, for details.
2195 The style variables are:
2196 @code{c-basic-offset},
2197 @code{c-comment-only-line-offset},
2198 @code{c-block-comment-prefix},
2199 @code{c-comment-prefix-regexp},
2200 @code{c-cleanup-list},
2201 @code{c-hanging-braces-alist},
2202 @code{c-hanging-colons-alist},
2203 @code{c-hanging-semi&comma-criteria},
2204 @code{c-backslash-column},
2205 @code{c-special-indent-hook},
2206 @code{c-label-minimum-indentation}, and
2207 @code{c-offsets-alist}.
2216 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2217 @node Built-in Styles, Adding Styles, , Styles
2218 @comment node-name, next, previous, up
2219 @subsection Built-in Styles
2220 @cindex built-in styles
2221 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2223 If you're lucky, one of @ccmode{}'s built-in styles might be just
2224 what you're looking for. These include:
2229 @code{gnu} --- Coding style blessed by the Free Software Foundation
2230 for C code in GNU programs.
2234 @code{k&r} --- The classic Kernighan and Ritchie style for C code.
2238 @code{bsd} --- Also known as ``Allman style'' after Eric Allman.
2240 @cindex Whitesmith style
2242 @code{whitesmith} --- Popularized by the examples that came with
2243 Whitesmiths C, an early commercial C compiler.
2245 @cindex Stroustrup style
2247 @code{stroustrup} --- The classic Stroustrup style for C++ code.
2249 @cindex Ellemtel style
2251 @code{ellemtel} --- Popular C++ coding standards as defined by
2252 ``Programming in C++, Rules and Recommendations'', Erik Nyquist and Mats
2253 Henricson, Ellemtel@footnote{This document is available at
2254 @uref{http://www.doc.ic.ac.uk/lab/cplus/c++.rules/} among other
2259 @code{linux} --- C coding standard for Linux development.
2261 @cindex Python style
2263 @code{python} --- C coding standard for Python extension
2264 modules@footnote{Python is a high level scripting language with a C/C++
2265 foreign function interface. For more information, see
2266 @uref{http://www.python.org/}.}.
2271 @code{java} --- The style for editing Java code. Note that this style is
2272 automatically installed when you enter @code{java-mode}.
2276 @code{user} --- This is a special style for several reasons. First, the
2277 @ccmode{} customizations you do by using either the Customization
2278 interface, or by writing @code{setq}'s at the top level of your
2279 @file{.emacs} file, will be captured in the @code{user} style. Also,
2280 all other styles implicitly inherit their settings from @code{user}
2281 style. This means that for any styles you add via @code{c-add-style}
2282 (@pxref{Adding Styles}) you need only define the differences between
2283 your new style and @code{user} style.
2287 @vindex c-default-style
2288 @vindex default-style (c-)
2289 The default style in all newly created buffers is @code{gnu}, but you
2290 can change this by setting variable @code{c-default-style}. Although
2291 the @code{user} style is not the default style, any style variable
2292 settings you do with the Customization interface or on the top level in
2293 your @file{.emacs} file will by default override the style system, so
2294 you don't need to set @code{c-default-style} to @code{user} to see the
2295 effect of these settings.
2297 @code{c-default-style} takes either a style name string, or an
2298 association list of major mode symbols to style names. Thus you can
2299 control exactly which default style is used for which @ccmode{} language
2300 mode. Here are the rules:
2302 @vindex c-style-alist
2303 @vindex style-alist (c-)
2304 @vindex c-mode-common-hook
2305 @vindex mode-common-hook (c-)
2308 When @code{c-default-style} is a string, it must be an existing style
2309 name as found in @code{c-style-alist}. This style is then used for all
2310 modes @emph{except} @code{java-mode}, where the style @samp{java} is
2311 used by default@footnote{This is for backwards compatibility reasons.
2312 The hard-coding of @code{java-mode} style is admittedly bogus!}.
2315 When @code{c-default-style} is an association list, the current major
2316 mode is looked up to find a style name string. In this case, this style
2317 is always used exactly as specified and an error will occur if the named
2318 style does not exist.
2321 If @code{c-default-style} is an association list, but the current major
2322 mode isn't found, then the special symbol @samp{other} is looked up. If
2323 this value is found, the associated style is used.
2326 If @samp{other} is not found, then the @samp{gnu} style is used.
2329 In all cases, the style described in @code{c-default-style} is installed
2330 @emph{before} the language hooks are run, so you can always override
2331 this setting by including an explicit call to @code{c-set-style} in your
2332 language mode hook, or in @code{c-mode-common-hook}.
2337 @findex set-style (c-)
2339 If you'd like to experiment with these built-in styles you can simply
2340 type the following in a @ccmode{} buffer:
2344 @kbd{C-c . @var{STYLE-NAME} RET}
2349 @kbd{C-c .} runs the command @code{c-set-style}. Note that all style
2350 names are case insensitive, even the ones you define.
2352 Setting a style in this way does @emph{not} automatically re-indent your
2353 file. For commands that you can use to view the effect of your changes,
2356 @vindex c-indentation-style
2357 @vindex indentation-style (c-)
2358 Note that for BOCM compatibility, @samp{gnu} is the default style, and
2359 any non-style based customizations you make (i.e. in
2360 @code{c-mode-common-hook} in your @file{.emacs} file) will be based on
2361 @samp{gnu} style unless you set @code{c-default-style} or do a
2362 @code{c-set-style} as the first thing in your hook. The variable
2363 @code{c-indentation-style} always contains the buffer's current style
2367 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2368 @node Adding Styles, File Styles, Built-in Styles, Styles
2369 @comment node-name, next, previous, up
2370 @subsection Adding Styles
2371 @cindex adding styles
2372 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2374 @vindex c-style-alist
2375 @vindex style-alist (c-)
2377 @findex add-style (c-)
2378 If none of the built-in styles is appropriate, you'll probably want to
2379 add a new @dfn{style definition}. Styles are kept in the
2380 @code{c-style-alist} variable, but you should never modify this variable
2381 directly. Instead, @ccmode{} provides the function
2382 @code{c-add-style} that you can use to easily add new styles or change
2383 existing styles. This function takes two arguments, a @var{stylename}
2384 string, and an association list @var{description} of style
2385 customizations. If @var{stylename} is not already in
2386 @code{c-style-alist}, the new style is added, otherwise the style is
2387 changed to the new @var{description}.
2388 This function also takes an optional third argument, which if
2389 non-@code{nil}, automatically applies the new style to the current
2392 @comment TBD: The next paragraph is bogus. I really need to better
2393 @comment document adding styles, including setting up inherited styles.
2395 The sample @file{.emacs} file provides a concrete example of how a new
2396 style can be added and automatically set. @xref{Sample .emacs File}.
2399 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2400 @node File Styles, , Adding Styles, Styles
2401 @comment node-name, next, previous, up
2402 @subsection File Styles
2404 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2406 @cindex local variables
2408 The Emacs manual describes how you can customize certain variables on a
2409 per-file basis by including a @dfn{Local Variable} block at the end of
2410 the file. So far, you've only seen a functional interface to @ccmode{}
2411 customization, which is highly inconvenient for use in a Local Variable
2412 block. @ccmode{} provides two variables that make it easier for you to
2413 customize your style on a per-file basis.
2415 @vindex c-file-style
2416 @vindex file-style (c-)
2417 @vindex c-file-offsets
2418 @vindex file-offsets (c-)
2420 The variable @code{c-file-style} can be set to a style name string.
2421 When the file is visited, @ccmode{} will automatically set the
2422 file's style to this style using @code{c-set-style}.
2424 Another variable, @code{c-file-offsets}, takes an association list
2425 similar to what is allowed in @code{c-offsets-alist}. When the file is
2426 visited, @ccmode{} will automatically institute these offsets using
2427 @code{c-set-offset}.
2429 Note that file style settings (i.e. @code{c-file-style}) are applied
2430 before file offset settings (i.e. @code{c-file-offsets}). Also, if
2431 either of these are set in a file's local variable section, all the
2432 style variable values are made local to that buffer.
2435 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2436 @node Advanced Customizations, , Styles, Customizing Indentation
2437 @comment node-name, next, previous, up
2438 @section Advanced Customizations
2439 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2441 @vindex c-style-alist
2442 @vindex style-alist (c-)
2443 For most users, @ccmode{} will support their coding styles with
2444 very little need for more advanced customizations. Usually, one of the
2445 standard styles defined in @code{c-style-alist} will do the trick. At
2446 most, perhaps one of the syntactic symbol offsets will need to be
2447 tweaked slightly, or maybe @code{c-basic-offset} will need to be
2448 changed. However, some styles require a more flexible framework for
2449 customization, and one of the real strengths of @ccmode{} is that
2450 the syntactic analysis model provides just such a framework. This allows
2451 you to implement custom indentation calculations for situations not
2452 handled by the mode directly.
2455 * Custom Indentation Functions::
2456 * Custom Brace and Colon Hanging::
2457 * Customizing Semi-colons and Commas::
2458 * Other Special Indentations::
2461 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2462 @node Custom Indentation Functions, Custom Brace and Colon Hanging, , Advanced Customizations
2463 @comment node-name, next, previous, up
2464 @subsection Custom Indentation Functions
2465 @cindex custom indentation functions
2466 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2468 The most flexible way to customize @ccmode{} is by writing custom
2469 indentation functions, and associating them with specific syntactic
2470 symbols (@pxref{Syntactic Symbols}). @ccmode{} itself uses indentation
2471 functions to provide more sophisticated indentation, for example when
2472 lining up C++ stream operator blocks:
2476 1: void main(int argc, char**)
2478 3: cout << "There were "
2480 5: << "arguments passed to the program"
2487 In this example, lines 4 through 6 are assigned the @code{stream-op}
2488 syntactic symbol. Here, @code{stream-op} has an offset of @code{+}, and
2489 with a @code{c-basic-offset} of 2, you can see that lines 4 through 6
2490 are simply indented two spaces to the right of line 3. But perhaps we'd
2491 like @ccmode{} to be a little more intelligent so that it aligns
2492 all the @samp{<<} symbols in lines 3 through 6. To do this, we have
2493 to write a custom indentation function which finds the column of first
2494 stream operator on the first line of the statement. Here is sample
2495 lisp code implementing this:
2499 (defun c-lineup-streamop (langelem)
2500 ;; lineup stream operators
2502 (let* ((relpos (cdr langelem))
2503 (curcol (progn (goto-char relpos)
2505 (re-search-forward "<<\\|>>" (c-point 'eol) 'move)
2506 (goto-char (match-beginning 0))
2507 (- (current-column) curcol))))
2512 Indentation functions take a single argument, which is a syntactic
2513 component cons cell (@pxref{Syntactic Analysis}). The function returns
2514 an integer offset value that will be added to the running total
2515 indentation for the line. Note that what actually gets returned is the
2516 difference between the column that the first stream operator is on, and
2517 the column of the buffer relative position passed in the function's
2518 argument. Remember that @ccmode{} automatically adds in the column of
2519 the component's relative buffer position and we don't the column offset
2522 The function should return @code{nil} if it's used in a situation where
2523 it doesn't want to do any decision. If the function is used in a list
2524 expression (@pxref{Customizing Indentation}), that will cause @ccmode{}
2525 to go on and check the next entry in the list.
2527 @cindex stream-op syntactic symbol
2528 @findex c-lineup-streamop
2529 @findex lineup-streamop (c-)
2530 Now, to associate the function @code{c-lineup-streamop} with the
2531 @code{stream-op} syntactic symbol, we can add something like the
2532 following to our @code{c++-mode-hook}@footnote{It probably makes more
2533 sense to add this to @code{c++-mode-hook} than @code{c-mode-common-hook}
2534 since stream operators are only relevant for C++.}:
2537 (c-set-offset 'stream-op 'c-lineup-streamop)
2541 Now the function looks like this after re-indenting (using @kbd{C-c
2546 1: void main(int argc, char**)
2548 3: cout << "There were "
2550 5: << " arguments passed to the program"
2557 Custom indentation functions can be as simple or as complex as you like,
2558 and any syntactic symbol that appears in @code{c-offsets-alist} can have
2559 a custom indentation function associated with it.
2561 @ccmode{} comes with an extensive set of predefined indentation
2562 functions, not all of which are used by the default styles. So there's
2563 a good chance the function you want already exists. @xref{Indentation
2564 Functions}, for a list of them. If you have written an indentation
2565 function that you think is generally useful, you're very welcome to
2566 contribute it; please contact @email{bug-cc-mode@@gnu.org}.
2569 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2570 @node Custom Brace and Colon Hanging, Customizing Semi-colons and Commas, Custom Indentation Functions, Advanced Customizations
2571 @comment node-name, next, previous, up
2572 @subsection Custom Brace and Colon Hanging
2573 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2575 @vindex c-hanging-braces-alist
2576 @vindex hanging-braces-alist (c-)
2577 Syntactic symbols aren't the only place where you can customize
2578 @ccmode{} with the lisp equivalent of callback functions. Brace
2579 ``hanginess'' can also be determined by custom functions associated with
2580 syntactic symbols on the @code{c-hanging-braces-alist} style variable.
2581 Remember that @var{ACTION}'s are typically a list containing some
2582 combination of the symbols @code{before} and @code{after}
2583 (@pxref{Hanging Braces}). However, an @var{ACTION} can also be a
2584 function which gets called when a brace matching that syntactic symbol
2587 @cindex customizing brace hanging
2588 These @var{ACTION} functions are called with two arguments: the
2589 syntactic symbol for the brace, and the buffer position at which the
2590 brace was inserted. The @var{ACTION} function is expected to return a
2591 list containing some combination of @code{before} and @code{after},
2592 including neither of them (i.e. @code{nil}). This return value has the
2593 normal brace hanging semantics.
2595 As an example, @ccmode{} itself uses this feature to dynamically
2596 determine the hanginess of braces which close ``do-while''
2601 void do_list( int count, char** atleast_one_string )
2605 handle_string( atleast_one_string[i] );
2607 @} while( i < count );
2613 @findex c-snug-do-while
2614 @findex snug-do-while (c-)
2615 @ccmode{} assigns the @code{block-close} syntactic symbol to the
2616 brace that closes the @code{do} construct, and normally we'd like the
2617 line that follows a @code{block-close} brace to begin on a separate
2618 line. However, with ``do-while'' constructs, we want the
2619 @code{while} clause to follow the closing brace. To do this, we
2620 associate the @code{block-close} symbol with the @var{ACTION} function
2621 @code{c-snug-do-while}:
2624 (defun c-snug-do-while (syntax pos)
2625 "Dynamically calculate brace hanginess for do-while statements.
2626 Using this function, `while' clauses that end a `do-while' block will
2627 remain on the same line as the brace that closes that block.
2629 See `c-hanging-braces-alist' for how to utilize this function as an
2630 ACTION associated with `block-close' syntax."
2633 (if (and (eq syntax 'block-close)
2634 (setq langelem (assq 'block-close c-syntactic-context))
2635 (progn (goto-char (cdr langelem))
2636 (if (= (following-char) ?@{)
2638 (looking-at "\\<do\\>[^_]")))
2644 This function simply looks to see if the brace closes a ``do-while''
2645 clause and if so, returns the list @samp{(before)} indicating
2646 that a newline should be inserted before the brace, but not after it.
2647 In all other cases, it returns the list @samp{(before after)} so
2648 that the brace appears on a line by itself.
2650 @vindex c-syntactic-context
2651 @vindex syntactic-context (c-)
2652 During the call to the brace hanging @var{ACTION} function, the variable
2653 @code{c-syntactic-context} is bound to the full syntactic analysis list.
2655 @cindex customizing colon hanging
2656 @vindex c-hanging-colon-alist
2657 @vindex hanging-colon-alist (c-)
2658 Note that for symmetry, colon hanginess should be customizable by
2659 allowing function symbols as @var{ACTION}s on the
2660 @code{c-hanging-colon-alist} style variable. Since no use has actually
2661 been found for this feature, it isn't currently implemented!
2664 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2665 @node Customizing Semi-colons and Commas, Other Special Indentations, Custom Brace and Colon Hanging, Advanced Customizations
2666 @comment node-name, next, previous, up
2667 @subsection Customizing Semi-colons and Commas
2668 @cindex customizing semi-colons and commas
2669 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2671 @vindex c-hanging-semi&comma-criteria
2672 @vindex hanging-semi&comma-criteria (c-)
2673 You can also customize the insertion of newlines after semi-colons and
2674 commas, when the auto-newline minor mode is enabled (@pxref{Minor
2675 Modes}). This is controlled by the style variable
2676 @code{c-hanging-semi&comma-criteria}, which contains a list of functions
2677 that are called in the order they appear. Each function is called with
2678 zero arguments, and is expected to return one of the following values:
2682 non-@code{nil} --- A newline is inserted, and no more functions from the
2686 @code{stop} --- No more functions from the list are called, but no
2687 newline is inserted.
2690 @code{nil} --- No determination is made, and the next function in the
2695 If every function in the list is called without a determination being
2696 made, then no newline is added. The default value for this variable is a
2697 list containing a single function which inserts newlines only after
2698 semi-colons which do not appear inside parenthesis lists (i.e. those
2699 that separate @code{for}-clause statements).
2701 @findex c-semi&comma-no-newlines-before-nonblanks
2702 @findex semi&comma-no-newlines-before-nonblanks (c-)
2703 Here's an example of a criteria function, provided by @ccmode{}, that
2704 will prevent newlines from being inserted after semicolons when there is
2705 a non-blank following line. Otherwise, it makes no determination. To
2706 use, add this to the front of the @code{c-hanging-semi&comma-criteria}
2712 (defun c-semi&comma-no-newlines-before-nonblanks ()
2714 (if (and (eq last-command-char ?\;)
2715 (zerop (forward-line 1))
2716 (not (looking-at "^[ \t]*$")))
2723 @findex c-semi&comma-inside-parenlist
2724 @findex c-semi&comma-no-newlines-for-oneline-inliners
2725 @findex semi&comma-inside-parenlist (c-)
2726 @findex semi&comma-no-newlines-for-oneline-inliners (c-)
2727 The function @code{c-semi&comma-inside-parenlist} is what prevents
2728 newlines from being inserted inside the parenthesis list of @code{for}
2729 statements. In addition to
2730 @code{c-semi&comma-no-newlines-before-nonblanks} described above,
2731 @ccmode{} also comes with the criteria function
2732 @code{c-semi&comma-no-newlines-for-oneline-inliners}, which suppresses
2733 newlines after semicolons inside one-line inline method definitions
2734 (i.e. in C++ or Java).
2737 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2738 @node Other Special Indentations, , Customizing Semi-colons and Commas, Advanced Customizations
2739 @comment node-name, next, previous, up
2740 @subsection Other Special Indentations
2741 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2743 @vindex c-label-minimum-indentation
2744 @vindex label-minimum-indentation (c-)
2745 In @samp{gnu} style (@pxref{Built-in Styles}), a minimum indentation
2746 is imposed on lines inside top-level constructs. This minimum
2747 indentation is controlled by the style variable
2748 @code{c-label-minimum-indentation}. The default value for this variable
2751 @vindex c-special-indent-hook
2752 @vindex special-indent-hook (c-)
2753 One other customization variable is available in @ccmode{}: The style
2754 variable @code{c-special-indent-hook}. This is a standard hook variable
2755 that is called after every line is indented by @ccmode{}. You can use
2756 it to do any special indentation or line adjustments your style
2757 dictates, such as adding extra indentation to constructors or destructor
2758 declarations in a class definition, etc. Note however, that you should
2759 not change point or mark inside your @code{c-special-indent-hook}
2760 functions (i.e. you'll probably want to wrap your function in a
2761 @code{save-excursion}).
2763 Setting @code{c-special-indent-hook} in your style definition is handled
2764 slightly differently than other variables. In your style definition,
2765 you should set the value for
2766 @code{c-special-indent-hook} to a function or list of functions, which
2767 will be appended to @code{c-special-indent-hook} using @code{add-hook}.
2768 That way, the current setting for the buffer local value of
2769 @code{c-special-indent-hook} won't be overridden.
2772 @findex indent-for-comment
2773 @vindex c-indent-comments-syntactically-p
2774 @vindex indent-comments-syntactically-p (c-)
2775 @vindex comment-column
2776 Normally, the standard Emacs command @kbd{M-;}
2777 (@code{indent-for-comment}) will indent comment only lines to
2778 @code{comment-column}. Some users however, prefer that @kbd{M-;} act
2779 just like @kbd{TAB} for purposes of indenting comment-only lines;
2780 i.e. they want the comments to always indent as they would for normal
2781 code, regardless of whether @kbd{TAB} or @kbd{M-;} were used. This
2782 behavior is controlled by the variable
2783 @code{c-indent-comments-syntactically-p}. When @code{nil} (the
2784 default), @kbd{M-;} indents comment-only lines to @code{comment-column},
2785 otherwise, they are indented just as they would be if @kbd{TAB} were
2788 Note that this has no effect for comment lines that are inserted with
2789 @kbd{M-;} at the end of regular code lines. These comments will always
2790 start at @code{comment-column}.
2793 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2794 @node Syntactic Symbols, Indentation Functions, Customizing Indentation, Top
2795 @comment node-name, next, previous, up
2796 @chapter Syntactic Symbols
2797 @cindex syntactic symbols
2798 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2800 @vindex c-offsets-alist
2801 @vindex offsets-alist (c-)
2802 Here is a complete list of the recognized syntactic symbols as described
2803 in the @code{c-offsets-alist} style variable, along with a brief
2804 description. More detailed descriptions follow.
2808 Inside a multi-line string.
2810 Inside a multi-line C style block comment.
2812 Brace that opens a top-level function definition.
2814 Brace that closes a top-level function definition.
2815 @item defun-block-intro
2816 The first line in a top-level defun.
2818 Brace that opens a class definition.
2820 Brace that closes a class definition.
2822 Brace that opens an in-class inline method.
2824 Brace that closes an in-class inline method.
2825 @item func-decl-cont
2826 The region between a function definition's argument list and the
2827 function opening brace (excluding K&R argument declarations). In C, you
2828 cannot put anything but whitespace and comments in this region, however
2829 in C++ and Java, @code{throws} declarations and other things can appear
2831 @item knr-argdecl-intro
2832 First line of a K&R C argument declaration.
2834 Subsequent lines in a K&R C argument declaration.
2836 The first line in a ``topmost'' definition.
2837 @item topmost-intro-cont
2838 Topmost definition continuation lines.
2839 @item member-init-intro
2840 First line in a member initialization list.
2841 @item member-init-cont
2842 Subsequent member initialization list lines.
2844 First line of a multiple inheritance list.
2846 Subsequent multiple inheritance lines.
2848 Statement block open brace.
2850 Statement block close brace.
2851 @item brace-list-open
2852 Open brace of an enum or static array list.
2853 @item brace-list-close
2854 Close brace of an enum or static array list.
2855 @item brace-list-intro
2856 First line in an enum or static array list.
2857 @item brace-list-entry
2858 Subsequent lines in an enum or static array list.
2859 @item brace-entry-open
2860 Subsequent lines in an enum or static array list where the line begins
2864 @item statement-cont
2865 A continuation of a statement.
2866 @item statement-block-intro
2867 The first line in a new statement block.
2868 @item statement-case-intro
2869 The first line in a case block.
2870 @item statement-case-open
2871 The first line in a case block that starts with a brace.
2873 The first line after a conditional or loop construct.
2874 @item substatement-open
2875 The brace that opens a substatement block.
2877 A @code{case} or @code{default} label.
2879 C++ access control label.
2881 Any non-special C label.
2882 @item do-while-closure
2883 The @code{while} line that ends a @code{do}-@code{while} construct.
2885 The @code{else} line of an @code{if}-@code{else} construct.
2887 The @code{catch} or @code{finally} (in Java) line of a
2888 @code{try}-@code{catch} construct.
2890 A line containing only a comment introduction.
2892 The first line in an argument list.
2894 Subsequent argument list lines when no arguments follow on the same line
2895 as the the arglist opening paren.
2896 @item arglist-cont-nonempty
2897 Subsequent argument list lines when at least one argument follows on the
2898 same line as the arglist opening paren.
2900 The solo close paren of an argument list.
2902 Lines continuing a stream operator (C++ only).
2904 The line is nested inside a class definition.
2906 The start of a C preprocessor macro definition.
2907 @item cpp-macro-cont
2908 Subsequent lines of a multi-line C preprocessor macro definition.
2910 A C++ friend declaration.
2911 @item objc-method-intro
2912 The first line of an Objective-C method. definition.
2913 @item objc-method-args-cont
2914 Lines continuing an Objective-C method. definition
2915 @item objc-method-call-cont
2916 Lines continuing an Objective-C method call.
2917 @item extern-lang-open
2918 Brace that opens an external language block.
2919 @item extern-lang-close
2920 Brace that closes an external language block.
2922 Analogous to @code{inclass} syntactic symbol, but used inside external
2923 language blocks (e.g. @code{extern "C" @{}).
2924 @item namespace-open
2925 Brace that opens a C++ namespace block.
2926 @item namespace-close
2927 Brace that closes a C++ namespace block.
2929 Analogous to @code{inextern-lang} syntactic symbol, but used inside C++
2931 @item template-args-cont
2932 C++ template argument list continuations.
2934 Analogous to @code{inclass} syntactic symbol, but used inside lambda
2935 (i.e. anonymous) functions. Only used in Pike mode.
2936 @item lambda-intro-cont
2937 Lines continuing the header of a lambda function, i.e. between the
2938 @code{lambda} keyword and the function body. Only used in Pike mode.
2939 @item inexpr-statement
2940 A statement block inside an expression. The gcc C extension of this is
2941 recognized. It's also used for the special functions that takes a
2942 statement block as an argument in Pike.
2944 A class definition inside an expression. This is used for anonymous
2945 classes in Java. It's also used for anonymous array initializers in
2949 @cindex -open syntactic symbols
2950 @cindex -close syntactic symbols
2951 Most syntactic symbol names follow a general naming convention. When a
2952 line begins with an open or close brace, the syntactic symbol will
2953 contain the suffix @code{-open} or @code{-close} respectively.
2955 @cindex -intro syntactic symbols
2956 @cindex -cont syntactic symbols
2957 @cindex -block-intro syntactic symbols
2958 Usually, a distinction is made between the first line that introduces a
2959 construct and lines that continue a construct, and the syntactic symbols
2960 that represent these lines will contain the suffix @code{-intro} or
2961 @code{-cont} respectively. As a sub-classification of this scheme, a
2962 line which is the first of a particular brace block construct will
2963 contain the suffix @code{-block-intro}.
2965 Let's look at some examples to understand how this works. Remember that
2966 you can check the syntax of any line by using @kbd{C-c C-s}.
2971 2: swap( int& a, int& b )
2983 @cindex topmost-intro syntactic symbol
2984 @cindex topmost-intro-cont syntactic symbol
2985 @cindex defun-open syntactic symbol
2986 @cindex defun-close syntactic symbol
2987 @cindex defun-block-intro syntactic symbol
2988 Line 1 shows a @code{topmost-intro} since it is the first line that
2989 introduces a top-level construct. Line 2 is a continuation of the
2990 top-level construct introduction so it has the syntax
2991 @code{topmost-intro-cont}. Line 3 shows a @code{defun-open} since it is
2992 the brace that opens a top-level function definition. Line 9 is the
2994 @code{defun-close} since it contains the brace that closes the top-level
2995 function definition. Line 4 is a @code{defun-block-intro}, i.e. it is
2996 the first line of a brace-block, enclosed in a
2997 top-level function definition.
2999 @cindex statement syntactic symbol
3000 @cindex statement-cont syntactic symbol
3001 Lines 5, 6, and 7 are all given @code{statement} syntax since there
3002 isn't much special about them. Note however that line 8 is given
3003 @code{statement-cont} syntax since it continues the statement begun
3004 on the previous line.
3006 Here's another example, which illustrates some C++ class syntactic
3013 3: public Amplifiable
3017 7: : eString( new BassString( 0.105 )),
3018 8: aString( new BassString( 0.085 )),
3019 9: dString( new BassString( 0.065 )),
3020 10: gString( new BassString( 0.045 ))
3022 12: eString.tune( 'E' );
3023 13: aString.tune( 'A' );
3024 14: dString.tune( 'D' );
3025 15: gString.tune( 'G' );
3027 17: friend class Luthier;
3033 @cindex class-open syntactic symbol
3034 @cindex class-close syntactic symbol
3035 As in the previous example, line 1 has the @code{topmost-intro} syntax.
3036 Here however, the brace that opens a C++ class definition on line 4 is
3037 assigned the @code{class-open} syntax. Note that in C++, classes,
3038 structs, and unions are essentially equivalent syntactically (and are
3039 very similar semantically), so replacing the @code{class} keyword in the
3040 example above with @code{struct} or @code{union} would still result in a
3041 syntax of @code{class-open} for line 4 @footnote{This is the case even
3042 for C and Objective-C. For consistency, structs in all supported
3043 languages are syntactically equivalent to classes. Note however that
3044 the keyword @code{class} is meaningless in C and Objective-C.}.
3045 Similarly, line 18 is assigned @code{class-close} syntax.
3047 @cindex inher-intro syntactic symbol
3048 @cindex inher-cont syntactic symbol
3049 Line 2 introduces the inheritance list for the class so it is assigned
3050 the @code{inher-intro} syntax, and line 3, which continues the
3051 inheritance list is given @code{inher-cont} syntax.
3053 @cindex access-label syntactic symbol
3054 @cindex inclass syntactic symbol
3055 Hitting @kbd{C-c C-s} on line 5 shows the following analysis:
3060 @code{((inclass . 58) (access-label . 67))}
3066 The primary syntactic symbol for this line is @code{access-label} as
3067 this a label keyword that specifies access protection in C++. However,
3068 because this line is also a top-level construct inside a class
3069 definition, the analysis actually shows two syntactic symbols. The
3070 other syntactic symbol assigned to this line is @code{inclass}.
3071 Similarly, line 6 is given both @code{inclass} and @code{topmost-intro}
3077 @code{((inclass . 58) (topmost-intro . 60))}
3082 @cindex member-init-intro syntactic symbol
3083 @cindex member-init-cont syntactic symbol
3084 Line 7 introduces a C++ member initialization list and as such is given
3085 @code{member-init-intro} syntax. Note that in this case it is
3086 @emph{not} assigned @code{inclass} since this is not considered a
3087 top-level construct. Lines 8 through 10 are all assigned
3088 @code{member-init-cont} since they continue the member initialization
3089 list started on line 7.
3091 @cindex in-class inline methods
3092 @cindex inline-open syntactic symbol
3093 @cindex inline-close syntactic symbol
3094 Line 11's analysis is a bit more complicated:
3099 @code{((inclass . 58) (inline-open))}
3104 This line is assigned a syntax of both @code{inline-open} and
3105 @code{inclass} because it opens an @dfn{in-class} C++ inline method
3106 definition. This is distinct from, but related to, the C++ notion of an
3107 inline function in that its definition occurs inside an enclosing class
3108 definition, which in C++ implies that the function should be inlined.
3109 If though, the definition of the @code{Bass} constructor appeared
3110 outside the class definition, the construct would be given the
3111 @code{defun-open} syntax, even if the keyword @code{inline} appeared
3112 before the method name, as in:
3126 : eString( new BassString( 0.105 )),
3127 aString( new BassString( 0.085 )),
3128 dString( new BassString( 0.065 )),
3129 gString( new BassString( 0.045 ))
3131 eString.tune( 'E' );
3132 aString.tune( 'A' );
3133 dString.tune( 'D' );
3134 gString.tune( 'G' );
3140 @cindex friend syntactic symbol
3141 Returning to the previous example, line 16 is given @code{inline-close}
3142 syntax, while line 12 is given @code{defun-block-open} syntax, and lines
3143 13 through 15 are all given @code{statement} syntax. Line 17 is
3144 interesting in that its syntactic analysis list contains three
3149 @code{((friend) (inclass . 58) (topmost-intro . 380))}
3153 The @code{friend} syntactic symbol is a modifier that typically does not
3154 have a relative buffer position.
3156 Template definitions introduce yet another syntactic symbol:
3161 1: ThingManager <int,
3162 2: Framework::Callback *,
3163 3: Mutex> framework_callbacks;
3168 Here, line 1 is analyzed as a @code{topmost-intro}, but lines 2 and 3
3169 are both analyzed as @code{template-args-cont} lines.
3171 Here is another (totally contrived) example which illustrates how syntax
3172 is assigned to various conditional constructs:
3176 1: void spam( int index )
3178 3: for( int i=0; i<index; i++ )
3182 7: do_something_special();
3185 10: do_something( i );
3188 13: another_thing( i-- );
3198 Only the lines that illustrate new syntactic symbols will be discussed.
3200 @cindex substatement-open syntactic symbol
3201 @cindex substatement-block-intro syntactic symbol
3202 @cindex block-close syntactic symbol
3203 Line 4 has a brace which opens a conditional's substatement block. It
3204 is thus assigned @code{substatement-open} syntax, and since line 5 is
3205 the first line in the substatement block, it is assigned
3206 @code{substatement-block-intro} syntax. Lines 6 and 7 are assigned
3207 similar syntax. Line 8 contains the brace that closes the inner
3208 substatement block. It is given the syntax @code{block-close},
3209 as are lines 11 and 14.
3211 @cindex else-clause syntactic symbol
3212 @cindex catch-clause syntactic symbol
3213 Line 9 is a little different --- since it contains the keyword
3214 @code{else} matching the @code{if} statement introduced on line 5, it is
3215 given the @code{else-clause} syntax. The @code{try}-@code{catch}
3216 constructs in C++ and Java are treated this way too, with the only
3217 difference that the @code{catch}, and in Java also @code{finally}, is
3218 marked with @code{catch-clause}.
3220 @cindex substatement syntactic symbol
3221 Line 10 is also slightly different. Because @code{else} is considered a
3222 conditional introducing keyword @footnote{The list of conditional
3223 keywords are (in C, C++, Objective-C, Java, and Pike): @code{for},
3224 @code{if}, @code{do}, @code{else}, @code{while}, and @code{switch}. C++
3225 and Java have two additional conditional keywords: @code{try} and
3226 @code{catch}. Java also has the @code{finally} and @code{synchronized}
3227 keywords.}, and because the following substatement is not a brace block,
3228 line 10 is assigned the @code{substatement} syntax.
3230 @cindex do-while-closure syntactic symbol
3231 One other difference is seen on line 15. The @code{while} construct
3232 that closes a @code{do} conditional is given the special syntax
3233 @code{do-while-closure} if it appears on a line by itself. Note that if
3234 the @code{while} appeared on the same line as the preceding close brace,
3235 that line would have been assigned @code{block-close} syntax instead.
3237 Switch statements have their own set of syntactic symbols. Here's an
3242 1: void spam( enum Ingredient i )
3249 8: drink_some_water();
3262 @cindex case-label syntactic symbol
3263 @cindex statement-case-intro syntactic symbol
3264 @cindex statement-case-open syntactic symbol
3265 Here, lines 4, 7, and 10 are all assigned @code{case-label} syntax,
3266 while lines 5 and 8 are assigned @code{statement-case-intro}. Line 11
3267 is treated slightly differently since it contains a brace that opens a
3268 block --- it is given @code{statement-case-open} syntax.
3271 There are a set of syntactic symbols that are used to recognize
3272 constructs inside of brace lists. A brace list is defined as an
3273 @code{enum} or aggregate initializer list, such as might statically
3274 initialize an array of structs. The three special aggregate constructs
3275 in Pike, @code{(@{ @})}, @code{([ ])} and @code{(< >)}, are treated as
3276 brace lists too. An example:
3280 1: static char* ingredients[] =
3290 @cindex brace-list-open syntactic symbol
3291 @cindex brace-list-intro syntactic symbol
3292 @cindex brace-list-close syntactic symbol
3293 @cindex brace-list-entry syntactic symbol
3294 Following convention, line 2 in this example is assigned
3295 @code{brace-list-open} syntax, and line 3 is assigned
3296 @code{brace-list-intro} syntax. Likewise, line 6 is assigned
3297 @code{brace-list-close} syntax. Lines 4 and 5 however, are assigned
3298 @code{brace-list-entry} syntax, as would all subsequent lines in this
3301 @cindex brace-entry-open syntactic symbol
3302 Your static initializer might be initializing nested structures, for
3307 1: struct intpairs[] =
3322 Here, you've already seen the analysis of lines 1, 2, 3, and 11. On
3323 line 4, things get interesting; this line is assigned
3324 @code{brace-entry-open} syntactic symbol because it's a bracelist entry
3325 line that starts with an open brace. Lines 5 and 6 (and line 9) are
3326 pretty standard, and line 7 is a @code{brace-list-close} as you'd
3327 expect. Once again, line 8 is assigned as @code{brace-entry-open} as is
3330 External language definition blocks also have their own syntactic
3331 symbols. In this example:
3337 3: int thing_one( int );
3338 4: int thing_two( double );
3344 @cindex extern-lang-open syntactic symbol
3345 @cindex extern-lang-close syntactic symbol
3346 @cindex inextern-lang syntactic symbol
3347 @cindex inclass syntactic symbol
3349 line 2 is given the @code{extern-lang-open} syntax, while line 5 is given
3350 the @code{extern-lang-close} syntax. The analysis for line 3 yields:
3351 @code{((inextern-lang) (topmost-intro . 14))}, where
3352 @code{inextern-lang} is a modifier similar in purpose to @code{inclass}.
3354 Similarly, C++ namespace constructs have their own associated syntactic
3355 symbols. In this example:
3367 @cindex namespace-open syntactic symbol
3368 @cindex namespace-close syntactic symbol
3369 @cindex innamespace syntactic symbol
3371 line 2 is given the @code{namespace-open} syntax, while line 4 is given
3372 the @code{namespace-close} syntax. The analysis for line 3 yields:
3373 @code{((innamespace) (topmost-intro . 17))}, where @code{innamespace} is
3374 a modifier similar in purpose to @code{inextern-lang} and @code{inclass}.
3376 A number of syntactic symbols are associated with parenthesis lists,
3377 a.k.a argument lists, as found in function declarations and function
3378 calls. This example illustrates these:
3382 1: void a_function( int line1,
3385 4: void a_longer_function(
3390 9: void call_them( int line1, int line2 )
3397 16: a_longer_function( line1,
3404 @cindex arglist-intro syntactic symbol
3405 @cindex arglist-close syntactic symbol
3406 Lines 5 and 12 are assigned @code{arglist-intro} syntax since they are
3407 the first line following the open parenthesis, and lines 7 and 14 are
3408 assigned @code{arglist-close} syntax since they contain the parenthesis
3409 that closes the argument list.
3411 @cindex arglist-cont-nonempty syntactic symbol
3412 @cindex arglist-cont syntactic symbol
3413 Lines that continue argument lists can be assigned one of two syntactic
3414 symbols. For example, Lines 2 and 17
3415 are assigned @code{arglist-cont-nonempty} syntax. What this means
3416 is that they continue an argument list, but that the line containing the
3417 parenthesis that opens the list is @emph{not empty} following the open
3418 parenthesis. Contrast this against lines 6 and 13 which are assigned
3419 @code{arglist-cont} syntax. This is because the parenthesis that opens
3420 their argument lists is the last character on that line.
3422 Note that there is no @code{arglist-open} syntax. This is because any
3423 parenthesis that opens an argument list, appearing on a separate line,
3424 is assigned the @code{statement-cont} syntax instead.
3426 A few miscellaneous syntactic symbols that haven't been previously
3427 covered are illustrated by this C++ example:
3431 1: void Bass::play( int volume )
3434 4: /* this line starts a multi-line
3435 5: * comment. This line should get `c' syntax */
3437 7: char* a_multiline_string = "This line starts a multi-line \
3438 8: string. This line should get `string' syntax.";
3446 16: cout << "I played "
3454 The lines to note in this example include:
3458 @cindex func-decl-cont syntactic symbol
3460 Line 2 is assigned the @code{func-decl-cont} syntax.
3462 @cindex comment-intro syntactic symbol
3464 Line 4 is assigned both @code{defun-block-intro} @emph{and}
3465 @code{comment-intro} syntax.
3467 @cindex c syntactic symbol
3469 Line 5 is assigned @code{c} syntax.
3472 @cindex syntactic whitespace
3473 Line 6 which, even though it contains nothing but whitespace, is
3474 assigned @code{defun-block-intro}. Note that the appearance of the
3475 comment on lines 4 and 5 do not cause line 6 to be assigned
3476 @code{statement} syntax because comments are considered to be
3477 @dfn{syntactic whitespace}, which are ignored when analyzing
3480 @cindex string syntactic symbol
3482 Line 8 is assigned @code{string} syntax.
3484 @cindex label syntactic symbol
3486 Line 10 is assigned @code{label} syntax.
3488 @cindex block-open syntactic symbol
3490 Line 11 is assigned @code{block-open} syntax.
3492 @cindex cpp-macro syntactic symbol
3493 @cindex cpp-macro-cont syntactic symbol
3495 Lines 12 and 14 are assigned @code{cpp-macro} syntax in addition to the
3496 normal syntactic symbols (@code{statement-block-intro} and
3497 @code{statement}, respectively). Normally @code{cpp-macro} is
3498 configured to cancel out the normal syntactic context to make all
3499 preprocessor directives stick to the first column, but that's easily
3500 changed if you want preprocessor directives to be indented like the rest
3503 @cindex stream-op syntactic symbol
3505 Line 17 is assigned @code{stream-op} syntax.
3509 @cindex multi-line macros
3510 @cindex syntactic whitespace
3511 Multi-line C preprocessor macros are now (somewhat) supported. At least
3512 @ccmode{} now recognizes the fact that it is inside a multi-line macro,
3513 and it properly skips such macros as syntactic whitespace. In this
3518 1: #define LIST_LOOP(cons, listp) \
3519 2: for (cons = listp; !NILP (cons); cons = XCDR (cons)) \
3520 3: if (!CONSP (cons)) \
3521 4: signal_error ("Invalid list format", listp); \
3527 line 1 is given the syntactic symbol @code{cpp-macro}. This first line
3528 of a macro is always given this symbol. The second and subsequent lines
3529 (e.g. lines 2 through 5) are given the @code{cpp-macro-cont} syntactic
3530 symbol, with a relative buffer position pointing to the @code{#} which
3531 starts the macro definition.
3533 In Objective-C buffers, there are three additional syntactic symbols
3534 assigned to various message calling constructs. Here's an example
3539 1: - (void)setDelegate:anObject
3542 4: [delegate masterWillRebind:self
3543 5: toDelegate:anObject
3544 6: withExtraStuff:stuff];
3550 @cindex objc-method-intro syntactic symbol
3551 @cindex objc-method-args-cont syntactic symbol
3552 @cindex objc-method-call-cont syntactic symbol
3553 Here, line 1 is assigned @code{objc-method-intro} syntax, and line 2 is
3554 assigned @code{objc-method-args-cont} syntax. Lines 5 and 6 are both
3555 assigned @code{objc-method-call-cont} syntax.
3557 Java has a concept of anonymous classes, which may look something like
3562 1: public void watch(Observable o) @{
3563 2: o.addObserver(new Observer() @{
3564 3: public void update(Observable o, Object arg) @{
3565 4: history.addElement(arg);
3573 @cindex inexpr-class syntactic symbol
3574 The brace following the @code{new} operator opens the anonymous class.
3575 Lines 3 and 6 are assigned the @code{inexpr-class} syntax, besides the
3576 @code{inclass} symbol used in normal classes. Thus, the class will be
3577 indented just like a normal class, with the added indentation given to
3578 @code{inexpr-class}.
3580 There are a few occasions where a statement block may be used inside an
3581 expression. One is in C code using the gcc extension for this, e.g:
3586 2: int y = foo (); int z;
3587 3: if (y > 0) z = y; else z = - y;
3594 @cindex inexpr-statement syntactic symbol
3595 Lines 2 and 5 get the @code{inexpr-statement} syntax, besides the
3596 symbols they'd get in a normal block. Therefore, the indentation put on
3597 @code{inexpr-statement} is added to the normal statement block
3600 In Pike code, there are a few other situations where blocks occur inside
3601 statements, as illustrated here:
3607 3: string s = map (backtrace()[-2][3..],
3611 7: return sprintf ("%t", arg);
3612 8: @}) * ", " + "\n";
3614 10: write (s + "\n");
3621 @cindex inlambda syntactic symbol
3622 @cindex lambda-intro-cont syntactic symbol
3623 Lines 4 through 8 contain a lambda function, which @ccmode{} recognizes
3624 by the @code{lambda} keyword. If the function argument list is put
3625 on a line of its own, as in line 5, it gets the @code{lambda-intro-cont}
3626 syntax. The function body is handled as an inline method body, with the
3627 addition of the @code{inlambda} syntactic symbol. This means that line
3628 6 gets @code{inlambda} and @code{inline-open}, and line 8 gets
3629 @code{inline-close}@footnote{You might wonder why it doesn't get
3630 @code{inlambda} too. It's because the closing brace is relative to the
3631 opening brace, which stands on its own line in this example. If the
3632 opening brace was hanging on the previous line, then the closing brace
3633 would get the @code{inlambda} syntax too to be indented correctly.}.
3635 @cindex inexpr-statement syntactic symbol
3636 On line 9, @code{catch} is a special function taking a statement block
3637 as its argument. The block is handled as an in-expression statement
3638 with the @code{inexpr-statement} syntax, just like the gcc extended C
3639 example above. The other similar special function, @code{gauge}, is
3640 handled like this too.
3642 @cindex knr-argdecl-intro syntactic symbol
3643 @cindex knr-argdecl syntactic symbol
3644 Two other syntactic symbols can appear in old style, non-prototyped C
3645 code @footnote{a.k.a. K&R C, or Kernighan & Ritchie C}:
3649 1: int add_three_integers(a, b, c)
3654 6: return a + b + c;
3660 Here, line 2 is the first line in an argument declaration list and so is
3661 given the @code{knr-argdecl-intro} syntactic symbol. Subsequent lines
3662 (i.e. lines 3 and 4 in this example), are given @code{knr-argdecl}
3666 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3667 @node Indentation Functions, Performance Issues, Syntactic Symbols, Top
3668 @comment node-name, next, previous, up
3669 @chapter Indentation Functions
3670 @cindex indentation functions
3671 @cindex line-up functions
3672 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3674 Often there are cases when a simple offset setting on a syntactic symbol
3675 isn't enough to get the desired indentation. Therefore, it's also
3676 possible to use a @dfn{indentation function} (a.k.a. line-up function)
3677 for a syntactic symbol.
3679 @ccmode{} comes with many predefined indentation functions for common
3680 situations. If none of these does what you want, you can write your
3681 own, see @ref{Custom Indentation Functions}. If you do, it's probably a
3682 good idea to start working from one of these predefined functions, they
3683 can be found in the file @file{cc-align.el}.
3685 For every function below there is a ``works with'' list that indicates
3686 which syntactic symbols the function is intended to be used with.
3700 @findex c-lineup-arglist
3701 @findex lineup-arglist (c-)
3702 @item c-lineup-arglist
3703 Line up the current argument line under the first argument.
3705 @workswith @code{arglist-cont-nonempty}.
3707 @findex c-lineup-arglist-intro-after-paren
3708 @findex lineup-arglist-intro-after-paren (c-)
3709 @item c-lineup-arglist-intro-after-paren
3710 Line up a line just after the open paren of the surrounding paren or
3713 @workswith @code{defun-block-intro}, @code{brace-list-intro},
3714 @code{statement-block-intro}, @code{statement-case-intro},
3715 @code{arglist-intro}.
3717 @findex c-lineup-arglist-close-under-paren
3718 @findex lineup-arglist-close-under-paren (c-)
3719 @item c-lineup-arglist-close-under-paren
3720 Set e.g. your @code{arglist-close} syntactic symbol to this line-up
3721 function so that parentheses that close argument lists will line up
3722 under the parenthesis that opened the argument list.
3724 @workswith @code{defun-close}, @code{class-close}, @code{inline-close},
3725 @code{block-close}, @code{brace-list-close}, @code{arglist-close},
3726 @code{extern-lang-close}, @code{namespace-close} (for most of these, a
3727 zero offset will normally produce the same result, though).
3729 @findex c-lineup-close-paren
3730 @findex lineup-close-paren (c-)
3731 @item c-lineup-close-paren
3732 Line up the closing paren under its corresponding open paren if the
3733 open paren is followed by code. If the open paren ends its line, no
3734 indentation is added. E.g:
3740 ) // c-lineup-close-paren
3751 ) // c-lineup-close-paren
3756 @workswith @code{defun-close}, @code{class-close}, @code{inline-close},
3757 @code{block-close}, @code{brace-list-close}, @code{arglist-close},
3758 @code{extern-lang-close}, @code{namespace-close}.
3760 @findex c-lineup-streamop
3761 @findex lineup-streamop (c-)
3762 @item c-lineup-streamop
3763 Line up C++ stream operators (i.e. @samp{<<} and @samp{>>}).
3765 @workswith @code{stream-op}.
3767 @findex c-lineup-multi-inher
3768 @findex lineup-multi-inher (c-)
3769 @item c-lineup-multi-inher
3770 Line up the classes in C++ multiple inheritance clauses and member
3771 initializers under each other. E.g:
3775 Foo::Foo (int a, int b):
3777 Bar (b) // c-lineup-multi-inher
3788 public Bar // c-lineup-multi-inher
3797 Foo::Foo (int a, int b)
3799 , Bar (b) // c-lineup-multi-inher
3804 @workswith @code{inher-cont}, @code{member-init-cont}.
3806 @findex c-lineup-java-inher
3807 @findex lineup-java-inher (c-)
3808 @item c-lineup-java-inher
3809 Line up Java implements and extends declarations. If class names
3810 follows on the same line as the @samp{implements}/@samp{extends}
3811 keyword, they are lined up under each other. Otherwise, they are
3812 indented by adding @code{c-basic-offset} to the column of the keyword.
3819 Bar // c-lineup-java-inher
3832 Bar // c-lineup-java-inher
3837 @workswith @code{inher-cont}.
3839 @findex c-lineup-java-throws
3840 @findex lineup-java-throws (c-)
3841 @item c-lineup-java-throws
3842 Line up Java throws declarations. If exception names follows on the
3843 same line as the throws keyword, they are lined up under each other.
3844 Otherwise, they are indented by adding @code{c-basic-offset} to the
3845 column of the @samp{throws} keyword. The @samp{throws} keyword itself
3846 is also indented by @code{c-basic-offset} from the function declaration
3847 start if it doesn't hang. E.g:
3852 throws // c-lineup-java-throws
3853 Bar // c-lineup-java-throws
3855 <--><--> c-basic-offset
3864 int foo() throws Cyphr,
3865 Bar, // c-lineup-java-throws
3866 Vlod // c-lineup-java-throws
3871 @workswith @code{func-decl-cont}.
3873 @findex c-indent-one-line-block
3874 @findex indent-one-line-block (c-)
3875 @item c-indent-one-line-block
3876 Indent a one line block @code{c-basic-offset} extra. E.g:
3881 @{m+=n; n=0;@} // c-indent-one-line-block
3893 @{ // c-indent-one-line-block
3900 The block may be surrounded by any kind of parenthesis characters.
3901 @code{nil} is returned if the line doesn't start with a one line block,
3902 which makes the function usable in list expressions.
3904 @workswith Almost all syntactic symbols, but most useful on the
3905 @code{-open} symbols.
3907 @findex c-indent-multi-line-block
3908 @findex indent-multi-line-block (c-)
3909 @item c-indent-multi-line-block
3910 Indent a multi line block @code{c-basic-offset} extra. E.g:
3916 @{17@}, // c-indent-multi-line-block
3927 @{ // c-indent-multi-line-block
3936 The block may be surrounded by any kind of parenthesis characters.
3937 @code{nil} is returned if the line doesn't start with a multi line
3938 block, which makes the function usable in list expressions.
3940 @workswith Almost all syntactic symbols, but most useful on the
3941 @code{-open} symbols.
3943 @findex c-lineup-C-comments
3944 @findex lineup-C-comments (c-)
3945 @item c-lineup-C-comments
3946 Line up C block comment continuation lines. Various heuristics are used
3947 to handle most of the common comment styles. Some examples:
3958 text ** text ** text
3963 /**************************************************
3965 *************************************************/
3968 @vindex comment-start-skip
3970 /**************************************************
3971 Free form text comments:
3972 In comments with a long delimiter line at the
3973 start, the indentation is kept unchanged for lines
3974 that start with an empty comment line prefix. The
3975 delimiter line is whatever matches the
3976 @code{comment-start-skip} regexp.
3977 **************************************************/
3982 The style variable @code{c-comment-prefix-regexp} is used to recognize
3983 the comment line prefix, e.g. the @samp{*} that usually starts every
3984 line inside a comment.
3986 @workswith The @code{c} syntactic symbol.
3988 @findex c-lineup-comment
3989 @findex lineup-comment (c-)
3990 @item c-lineup-comment
3991 Line up a comment-only line according to the style variable
3992 @code{c-comment-only-line-offset}. If the comment is lined up with a
3993 comment starter on the previous line, that alignment is preserved.
3995 @vindex c-comment-only-line-offset
3996 @vindex comment-only-line-offset (c-)
3997 @code{c-comment-only-line-offset} specifies the extra offset for the
3998 line. It can contain an integer or a cons cell of the form
4001 (@r{<non-anchored-offset>} . @r{<anchored-offset>})
4006 where @var{non-anchored-offset} is the amount of offset given to
4007 non-column-zero anchored lines, and @var{anchored-offset} is the amount
4008 of offset to give column-zero anchored lines. Just an integer as value
4009 is equivalent to @code{(@r{<value>} . -1000)}.
4011 @workswith @code{comment-intro}.
4013 @findex c-lineup-runin-statements
4014 @findex lineup-runin-statements (c-)
4015 @item c-lineup-runin-statements
4016 Line up statements for coding standards which place the first statement
4017 in a block on the same line as the block opening brace@footnote{Run-in
4018 style doesn't really work too well. You might need to write your own
4019 custom indentation functions to better support this style.}. E.g:
4024 @{ puts (\"Hello world!\");
4025 return 0; // c-lineup-runin-statements
4031 If there is no statement after the opening brace to align with,
4032 @code{nil} is returned. This makes the function usable in list
4035 @workswith The @code{statement} syntactic symbol.
4037 @findex c-lineup-math
4038 @findex lineup-math (c-)
4040 Line up the current line after the equal sign on the first line in the
4041 statement. If there isn't any, indent with @code{c-basic-offset}. If
4042 the current line contains an equal sign too, try to align it with the
4045 @workswith @code{statement-cont}.
4047 @findex c-lineup-template-args
4048 @findex lineup-template-args (c-)
4049 @item c-lineup-template-args
4050 Line up the arguments of a template argument list under each other, but
4051 only in the case where the first argument is on the same line as the
4054 To allow this function to be used in a list expression, @code{nil} is
4055 returned if there's no template argument on the first line.
4057 @workswith @code{template-args-cont}.
4059 @findex c-lineup-ObjC-method-call
4060 @findex lineup-ObjC-method-call (c-)
4061 @item c-lineup-ObjC-method-call
4062 For Objective-C code, line up selector args as @code{elisp-mode} does
4063 with function args: go to the position right after the message receiver,
4064 and if you are at the end of the line, indent the current line
4065 c-basic-offset columns from the opening bracket; otherwise you are
4066 looking at the first character of the first method call argument, so
4067 lineup the current line with it.
4069 @workswith @code{objc-method-call-cont}.
4071 @findex c-lineup-ObjC-method-args
4072 @findex lineup-ObjC-method-args (c-)
4073 @item c-lineup-ObjC-method-args
4074 For Objective-C code, line up the colons that separate args. The colon
4075 on the current line is aligned with the one on the first line.
4077 @workswith @code{objc-method-args-cont}.
4079 @findex c-lineup-ObjC-method-args-2
4080 @findex lineup-ObjC-method-args-2 (c-)
4081 @item c-lineup-ObjC-method-args-2
4082 Similar to @code{c-lineup-ObjC-method-args} but lines up the colon on
4083 the current line with the colon on the previous line.
4085 @workswith @code{objc-method-args-cont}.
4087 @findex c-lineup-inexpr-block
4088 @findex lineup-inexpr-block (c-)
4089 @item c-lineup-inexpr-block
4090 This can be used with the in-expression block symbols to indent the
4091 whole block to the column where the construct is started. E.g. for Java
4092 anonymous classes, this lines up the class under the @samp{new} keyword,
4093 and in Pike it lines up the lambda function body under the @samp{lambda}
4094 keyword. Returns @code{nil} if the block isn't part of such a
4097 @workswith @code{inlambda}, @code{inexpr-statement},
4098 @code{inexpr-class}.
4100 @findex c-lineup-whitesmith-in-block
4101 @findex lineup-whitesmith-in-block (c-)
4102 @item c-lineup-whitesmith-in-block
4103 Line up lines inside a block in Whitesmith style. It's done in a way
4104 that works both when the opening brace hangs and when it doesn't. E.g:
4110 foo; // c-lineup-whitesmith-in-block
4121 foo; // c-lineup-whitesmith-in-block
4129 In the first case the indentation is kept unchanged, in the second
4130 @code{c-basic-offset} is added.
4132 @workswith @code{defun-close}, @code{defun-block-intro},
4133 @code{block-close}, @code{brace-list-close}, @code{brace-list-intro},
4134 @code{statement-block-intro}, @code{inclass}, @code{inextern-lang},
4137 @findex c-lineup-dont-change
4138 @findex lineup-dont-change (c-)
4139 @item c-lineup-dont-change
4140 This lineup function returns the indentation of the current line. Think
4141 of it as an identity function for lineups; it is used for
4142 @code{cpp-macro-cont} lines.
4144 @workswith Any syntactic symbol.
4149 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4150 @node Performance Issues, Frequently Asked Questions, Indentation Functions, Top
4151 @comment node-name, next, previous, up
4152 @chapter Performance Issues
4153 @cindex performance issues
4154 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4156 C and its derivative languages are highly complex creatures. Often,
4157 ambiguous code situations arise that require @ccmode{} to scan
4158 large portions of the buffer to determine syntactic context. Such
4159 pathological code@footnote{such as the output of @code{lex(1)}!}
4160 can cause @ccmode{} to perform fairly badly.
4161 This section identifies some of the coding styles to watch out for, and
4162 suggests some workarounds that you can use to improve performance.
4164 Because @ccmode{} has to scan the buffer backwards from the current
4165 insertion point, and because C's syntax is fairly difficult to parse in
4166 the backwards direction, @ccmode{} often tries to find the nearest
4167 position higher up in the buffer from which to begin a forward scan.
4168 The farther this position is from the current insertion point, the
4169 slower the mode gets. Some coding styles can even force @ccmode{}
4170 to scan from the beginning of the buffer for every line of code!
4172 @findex beginning-of-defun
4173 @findex defun-prompt-regexp
4174 One of the simplest things you can do to reduce scan time, is make sure
4175 any brace that opens a top-level construct@footnote{E.g. a function in
4176 C, or outermost class definition in C++ or Java.} always appears in the
4177 leftmost column. This is actually an Emacs constraint, as embodied in
4178 the @code{beginning-of-defun} function which @ccmode{} uses heavily. If
4179 you insist on hanging top-level open braces on the right side of the
4180 line, then you might want to set the variable @code{defun-prompt-regexp}
4181 to something reasonable, however that ``something reasonable'' is
4182 difficult to define, so @ccmode{} doesn't do it for you.
4184 @vindex c-Java-defun-prompt-regexp
4185 @vindex Java-defun-prompt-regexp (c-)
4186 A special note about @code{defun-prompt-regexp} in Java mode: while much
4187 of the early sample Java code seems to encourage a style where the brace
4188 that opens a class is hung on the right side of the line, this is not a
4189 good style to pursue in Emacs. @ccmode{} comes with a variable
4190 @code{c-Java-defun-prompt-regexp} which tries to define a regular
4191 expression usable for this style, but there are problems with it. In
4192 some cases it can cause @code{beginning-of-defun} to hang@footnote{This
4193 has been observed in Emacs 19.34 and XEmacs 19.15.}. For this reason,
4194 it is not used by default, but if you feel adventurous, you can set
4195 @code{defun-prompt-regexp} to it in your mode hook. In any event,
4196 setting and rely on @code{defun-prompt-regexp} will definitely slow
4197 things down anyway because you'll be doing regular expression searches
4198 for every line you indent, so you're probably screwed either way!
4200 @vindex c-enable-xemacs-performance-kludge-p
4201 @vindex enable-xemacs-performance-kludge-p (c-)
4202 Another alternative for XEmacs users, is to set the variable
4203 @code{c-enable-xemacs-performance-kludge-p} to non-@code{nil}. This
4204 tells @ccmode{} to use XEmacs-specific built-in functions which, in some
4205 circumstances, can locate the top-most opening brace much quicker than
4206 @code{beginning-of-defun}. Preliminary testing has shown that for
4207 styles where these braces are hung (e.g. most JDK-derived Java styles),
4208 this hack can improve performance of the core syntax parsing routines
4209 from 3 to 60 times. However, for styles which @emph{do} conform to
4210 Emacs' recommended style of putting top-level braces in column zero,
4211 this hack can degrade performance by about as much. Thus this variable
4212 is set to @code{nil} by default, since the Emacs-friendly styles
4213 should be more common (and
4214 encouraged!). Note that this variable has no effect in Emacs since the
4215 necessary built-in functions don't exist (in Emacs 20.2 or 20.3 as of
4216 this writing 27-Apr-1998).
4218 You will probably notice pathological behavior from @ccmode{} when
4219 working in files containing large amounts of C preprocessor macros.
4220 This is because Emacs cannot skip backwards over these lines as quickly
4223 @vindex c-recognize-knr-p
4224 @vindex recognize-knr-p (c-)
4225 Previous versions of @ccmode{} had potential performance problems
4226 when recognizing K&R style function argument declarations. This was
4227 because there are ambiguities in the C syntax when K&R style argument
4228 lists are used@footnote{It is hard to distinguish them from top-level
4229 declarations.}. @ccmode{} has adopted BOCM's convention for
4230 limiting the search: it assumes that argdecls are indented at least one
4231 space, and that the function headers are not indented at all. With
4232 current versions of @ccmode{}, user customization of
4233 @code{c-recognize-knr-p} is deprecated. Just don't put argdecls in
4236 @cindex @file{cc-lobotomy.el} file
4237 @vindex cc-lobotomy-pith-list
4238 You might want to investigate the speed-ups contained in the
4239 file @file{cc-lobotomy.el}, which comes as part of the @ccmode{}
4240 distribution, but is completely unsupported.
4241 As mentioned previous, @ccmode{} always trades speed for accuracy,
4242 however it is recognized that sometimes you need speed and can sacrifice
4243 some accuracy in indentation. The file @file{cc-lobotomy.el} contains
4244 hacks that will ``dumb down'' @ccmode{} in some specific ways, making
4245 that trade-off of accurancy for speed. I won't go into details of its
4246 use here; you should read the comments at the top of the file, and look
4247 at the variable @code{cc-lobotomy-pith-list} for details.
4250 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4251 @node Frequently Asked Questions, Getting the Latest CC Mode Release, Performance Issues, Top
4252 @comment node-name, next, previous, up
4253 @chapter Frequently Asked Questions
4254 @cindex frequently asked questions
4256 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4267 @strong{Q.} @emph{How do I re-indent the whole file?}
4269 @strong{A.} Visit the file and hit @kbd{C-x h} to mark the whole
4270 buffer. Then hit @kbd{C-M-\}.
4273 @strong{Q.} @emph{How do I re-indent the entire function?
4274 @kbd{C-M-x} doesn't work.}
4276 @strong{A.} @kbd{C-M-x} is reserved for future Emacs use.
4277 To re-indent the entire function hit @kbd{C-c C-q}.
4280 @strong{Q.} @emph{How do I re-indent the current block?}
4282 @strong{A.} First move to the brace which opens the block with
4283 @kbd{C-M-u}, then re-indent that expression with
4287 @strong{Q.} @emph{Why doesn't the @kbd{RET} key indent the new line?}
4289 @strong{A.} Emacs' convention is that @kbd{RET} just adds a newline,
4290 and that @kbd{C-j} adds a newline and indents it. You can make
4291 @kbd{RET} do this too by adding this to your
4292 @code{c-mode-common-hook}:
4295 (define-key c-mode-base-map "\C-m" 'c-context-line-break)
4299 This is a very common question. If you want this to be the default
4300 behavior, don't lobby me, lobby RMS! @t{:-)}
4303 @strong{Q.} @emph{I put @code{(c-set-offset 'substatement-open 0)}
4304 in my @file{.emacs} file but I get an error saying that
4305 @code{c-set-offset}'s function definition is void.}
4307 @strong{A.} This means that @ccmode{} wasn't loaded into your
4308 Emacs session by the time the @code{c-set-offset} call was reached,
4309 mostly likely because @ccmode{} is being autoloaded. Instead
4310 of putting the @code{c-set-offset} line in your top-level
4311 @file{.emacs} file, put it in your @code{c-mode-common-hook}, or
4312 simply modify @code{c-offsets-alist} directly:
4315 (setq c-offsets-alist '((substatement-open . 0)))
4320 @strong{Q.} @emph{My style settings works in all the @ccmode{} language
4321 modes except for Java, where I still get e.g. 4 column indentation.}
4323 @strong{A.} Java mode switches to the @samp{java} style by default for
4324 historical reasons. You can override it by putting an association list
4325 on @code{c-default-style}:
4328 (setq c-default-style '((other . "my-style")))
4332 The @samp{other} symbol says that @ccmode{} should use "my-style" in all
4333 modes not explicitly listed. Since there's nothing else on the list
4334 this causes "my-style" to be used in every mode.
4337 @strong{Q.} @emph{How do I make strings, comments, keywords, and other
4338 constructs appear in different colors, or in bold face, etc.?}
4340 @strong{A.} ``Syntax Colorization'' is a standard Emacs feature,
4341 controlled by @code{font-lock-mode}. @ccmode{} does not contain
4342 font-lock definitions for any of its supported languages.
4345 @strong{Q.} @emph{@kbd{M-a} and @kbd{M-e} used to move over entire
4346 balanced brace lists, but now they move into blocks. How do I get the
4349 @strong{A.} Use @kbd{C-M-f} and @kbd{C-M-b} to move over balanced brace
4350 blocks. Use @kbd{M-a} and @kbd{M-e} to move by statements, which will
4351 also move into blocks.
4354 @strong{Q.} @emph{Whenever I try to indent a line or type an
4355 ``electric'' key such as @kbd{;}, @kbd{@{}, or @kbd{@}}, I get an error
4356 that look like this: @code{Invalid function: (macro . #[...}. What
4359 @strong{A.} This is a common error when @ccmode{} hasn't been compiled
4360 correctly, especially under Emacs 19.34@footnote{Technically, it's
4361 because some macros wasn't defined during the compilation, so the byte
4362 compiler put in function calls instead of the macro expansions. Later,
4363 when the interpreter tries to call the macros as functions, it shows
4364 this (somewhat cryptic) error message.}. If you are using the standalone
4365 @ccmode{} distribution, try recompiling it according to the instructions
4366 in the @file{README} file.
4371 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4372 @node Getting the Latest CC Mode Release, Sample .emacs File, Frequently Asked Questions, Top
4373 @comment node-name, next, previous, up
4374 @chapter Getting the Latest CC Mode Release
4375 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4377 @ccmode{} is standard with all versions of Emacs since 19.34 and of
4380 Due to release schedule skew, it is likely that all of these Emacsen
4381 have old versions of @ccmode{} and so should be upgraded. Access to the
4382 @ccmode{} source code, as well as more detailed information on Emacsen
4383 compatibility, etc. are all available via the Web at:
4388 @uref{http://cc-mode.sourceforge.net/}
4393 @emph{Old URLs, including the FTP URLs, should no longer be used.}
4395 There are many files under these directories; you can pick up the entire
4396 distribution (named @code{cc-mode.tar.gz}; a gzip'd tar file), or any of
4397 the individual files, including PostScript documentation.
4400 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4401 @node Sample .emacs File, Limitations and Known Bugs, Getting the Latest CC Mode Release, Top
4402 @comment node-name, next, previous, up
4403 @chapter Sample .emacs file
4404 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4407 ;; Here's a sample .emacs file that might help you along the way. Just
4408 ;; copy this region and paste it into your .emacs file. You may want to
4409 ;; change some of the actual values.
4411 (defconst my-c-style
4412 '((c-tab-always-indent . t)
4413 (c-comment-only-line-offset . 4)
4414 (c-hanging-braces-alist . ((substatement-open after)
4416 (c-hanging-colons-alist . ((member-init-intro before)
4420 (access-label after)))
4421 (c-cleanup-list . (scope-operator
4424 (c-offsets-alist . ((arglist-close . c-lineup-arglist)
4425 (substatement-open . 0)
4428 (knr-argdecl-intro . -)))
4429 (c-echo-syntactic-information-p . t)
4431 "My C Programming Style")
4433 ;; offset customizations not in my-c-style
4434 (setq c-offsets-alist '((member-init-intro . ++)))
4436 ;; Customizations for all modes in CC Mode.
4437 (defun my-c-mode-common-hook ()
4438 ;; add my personal style and set it for the current buffer
4439 (c-add-style "PERSONAL" my-c-style t)
4440 ;; other customizations
4442 ;; this will make sure spaces are used instead of tabs
4443 indent-tabs-mode nil)
4444 ;; we like auto-newline and hungry-delete
4445 (c-toggle-auto-hungry-state 1)
4446 ;; keybindings for all supported languages. We can put these in
4447 ;; c-mode-base-map because c-mode-map, c++-mode-map, objc-mode-map,
4448 ;; java-mode-map, idl-mode-map, and pike-mode-map inherit from it.
4449 (define-key c-mode-base-map "\C-m" 'c-context-line-break)
4452 (add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
4456 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4457 @node Limitations and Known Bugs, Mailing Lists and Submitting Bug Reports, Sample .emacs File, Top
4458 @comment node-name, next, previous, up
4459 @chapter Limitations and Known Bugs
4462 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4466 Re-indenting large regions or expressions can be slow.
4468 @findex c-indent-exp
4469 @findex indent-exp (c-)
4471 @code{c-indent-exp} has not been fully optimized. It essentially
4472 equivalent to hitting @kbd{TAB} (@code{c-indent-command}) on every
4473 line. Some information is cached from line to line, but such caching
4474 invariable causes inaccuracies in analysis in some bizarre situations.
4476 @vindex signal-error-on-buffer-boundary
4478 XEmacs versions from 19.15 until (as of this writing 12-Mar-1998) 20.4
4479 contain a variable called @code{signal-error-on-buffer-boundary}. This
4480 was intended as a solution to user interface problems associated with
4481 buffer movement and the @code{zmacs-region} deactivation on errors.
4482 However, setting this variable to a non-default value had the
4483 deleterious side effect of breaking many built-in primitive functions.
4484 Most users will not be affected since they never change the value of
4485 this variable. @strong{Do not set this variable to @code{nil}}; you
4486 will cause serious problems in @ccmode{} and probably other XEmacs
4487 packages! As of at least XEmacs 20.4, the effects this variable tried
4488 to correct have been fixed in other, better ways.
4493 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4494 @node Mailing Lists and Submitting Bug Reports, Concept Index, Limitations and Known Bugs, Top
4495 @comment node-name, next, previous, up
4496 @chapter Mailing Lists and Submitting Bug Reports
4497 @cindex mailing lists
4498 @cindex reporting bugs
4499 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4502 @findex c-submit-bug-report
4503 @findex submit-bug-report (c-)
4504 To report bugs, use the @kbd{C-c C-b} (@code{c-submit-bug-report})
4505 command. This provides vital information we need to reproduce your
4506 problem. Make sure you include a concise, but complete code example.
4507 Please try to boil your example down to just the essential code needed
4508 to reproduce the problem, and include an exact recipe of steps needed to
4509 expose the bug. Be especially sure to include any code that appears
4510 @emph{before} your bug example, if you think it might affect our ability
4513 Please try to produce the problem in an Emacs instance without any
4514 customizations loaded (i.e. start it with the @code{-q -no-site-file}
4515 arguments). If it works correctly there, the problem might be caused by
4516 faulty customizations in either your own or your site configuration. In
4517 that case, we'd appreciate if you isolate the Emacs Lisp code that trigs
4518 the bug and include it in your report.
4520 Bug reports are now sent to the following email addresses:
4521 @email{bug-cc-mode@@gnu.org} and @email{bug-gnu-emacs@@gnu.org}; the
4522 latter is mirrored on the Usenet newsgroup @code{gnu.emacs.bug}. You
4523 can send other questions and suggestions (kudos? @t{;-)} to
4524 @email{bug-cc-mode@@gnu.org}, or @email{help-gnu-emacs@@gnu.org} which is
4525 mirrored on newsgroup @code{gnu.emacs.help}.
4527 @cindex beta testers mailing list
4528 @cindex announcement mailing list
4529 If you want to get announcements of new @ccmode{} releases, send the
4530 word @emph{subscribe} in the body of a message to
4531 @email{cc-mode-announce-request@@lists.sourceforge.net}. Announcements
4532 will also be posted to the Usenet newsgroups @code{gnu.emacs.sources},
4533 @code{comp.emacs}, @code{comp.emacs.xemacs}, and possibly some of the
4534 language oriented newsgroups.
4537 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4538 @node Concept Index, Command Index, Mailing Lists and Submitting Bug Reports, Top
4539 @comment node-name, next, previous, up
4540 @unnumbered Concept Index
4541 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4546 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4547 @node Command Index, Key Index, Concept Index, Top
4548 @comment node-name, next, previous, up
4549 @unnumbered Command Index
4550 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4552 Since most @ccmode{} commands are prepended with the string
4553 @samp{c-}, each appears under its @code{c-@var{<thing>}} name and its
4554 @code{@var{<thing>} (c-)} name.
4561 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4562 @node Key Index, Variable Index, Command Index, Top
4563 @comment node-name, next, previous, up
4564 @unnumbered Key Index
4565 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4570 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4571 @node Variable Index, , Key Index, Top
4572 @comment node-name, next, previous, up
4573 @unnumbered Variable Index
4574 @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4576 Since most @ccmode{} variables are prepended with the string
4577 @samp{c-}, each appears under its @code{c-@var{<thing>}} name and its
4578 @code{@var{<thing>} (c-)} name.