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