Mercurial > hg > xemacs-beta
diff man/cc-mode.texi @ 32:e04119814345 r19-15b99
Import from CVS: tag r19-15b99
author | cvs |
---|---|
date | Mon, 13 Aug 2007 08:52:56 +0200 |
parents | ac2d302a0011 |
children | 131b0175ea99 |
line wrap: on
line diff
--- a/man/cc-mode.texi Mon Aug 13 08:52:30 2007 +0200 +++ b/man/cc-mode.texi Mon Aug 13 08:52:56 2007 +0200 @@ -1,11 +1,11 @@ -\input texinfo @c -*- texinfo -*- +\input texinfo @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @comment %**start of header (This is for running Texinfo on a region) @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -@setfilename ../info/cc-mode.info -@settitle CC-MODE Version 4 Documentation +@setfilename cc-mode.info +@settitle CC MODE Version 4 Documentation @footnotestyle end @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @@ -17,12 +17,12 @@ @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @comment @comment texinfo manual for @file{cc-mode.el} version 4 -@comment manual version: 2.55 +@comment manual version: 2.66 @comment generated from the original README file by Krishna Padmasola @comment <krishna@earth-gw.njit.edu> @comment @comment Barry A. Warsaw <bwarsaw@cnri.reston.va.us> -@comment Last modification: 1996/08/21 19:29:16 +@comment Last modification: 1997/03/07 23:36:14 @comment @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @@ -48,10 +48,10 @@ @comment The title is printed in a large font. @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -@center @titlefont{CC-MODE Version 4} +@center @titlefont{CC Mode Version 4} @sp 2 @center A GNU Emacs mode for editing C, C++, Objective-C, and Java code. -@center (manual revision: 2.55) +@center (manual revision: 2.66) @sp 2 @center Barry A. Warsaw @@ -89,7 +89,7 @@ * Syntactic Symbols:: * Performance Issues:: * Frequently Asked Questions:: -* Getting the latest cc-mode release:: +* Getting the latest CC Mode release:: * Sample .emacs File:: * Limitations and Known Bugs:: * Mailing Lists and Submitting Bug Reports:: @@ -107,31 +107,35 @@ @chapter Introduction @cindex Introduction +@macro ccmode +CC Mode +@end macro + @cindex BOCM -Welcome to @code{cc-mode}, version 4. This is a GNU Emacs mode for +Welcome to @ccmode{}, version 4. This is a GNU Emacs mode for editing files containing C, C++, Objective-C, and Java code. This incarnation of the mode is descendant from @file{c-mode.el} (also called "Boring Old C Mode" or BOCM @code{:-)}, and @file{c++-mode.el} -version 2, which I have been maintaining since 1992. @code{cc-mode} +version 2, which I have been maintaining since 1992. @ccmode{} represents a significant milestone in the mode's life. It has been fully merged back with Emacs 19's @file{c-mode.el}. Also a new, more intuitive and flexible mechanism for controlling indentation has been developed. -@code{cc-mode} version 4 supports the editing of K&R and ANSI C, -@dfn{ARM} @footnote{i.e. ``The Annotated C++ Reference Manual'', by -Ellis and Stroustrup.} C++, Objective-C, and Java files. In this way, -you can easily set up consistent coding styles for use in editing all C, -C++, Objective-C, and Java programs. @code{cc-mode} does @emph{not} -handle font-locking (a.k.a. syntax coloring, keyword highlighting) or -anything of that nature, for any of the 4 modes. Those are handled by -other Emacs packages. +@ccmode{} version 4 supports the editing of K&R and ANSI C, @dfn{ARM} +@footnote{``The Annotated C++ Reference Manual'', by Ellis and +Stroustrup.} C++, Objective-C, and Java files. In this way, you can +easily set up consistent coding styles for use in editing all C, C++, +Objective-C, and Java programs. @ccmode{} does @emph{not} handle +font-locking (a.k.a. syntax coloring, keyword highlighting) or anything +of that nature, for any of the 4 modes. Those are handled by other +Emacs packages. This manual will describe the following: @itemize @bullet @item -How to get started using @code{cc-mode}. +How to get started using @ccmode{}. @item How the new indentation engine works. @@ -141,36 +145,33 @@ @end itemize -Note that the name of this file is @file{cc-mode.el}, and I'll often -refer to the package as @code{cc-mode}, but there really is no top level -@code{cc-mode} entry point. I call it @code{cc-mode} simply to -differentiate it from @file{c-mode.el}. All of the variables, commands, -and functions in @code{cc-mode} are prefixed with -@code{c-@var{<thing>}}, and @code{c-mode}, @code{c++-mode}, -@code{objc-mode}, and @code{java-mode} entry points are provided. This -file is intended to be a replacement for @file{c-mode.el} and -@file{c++-mode.el}. +Note that the name of this package is ``@ccmode{}''. The main file for +@ccmode{} is @file{cc-mode.el}, but other files are included in the +@ccmode{} distribution. There is no top level @code{cc-mode} entry +point. All of the variables, commands, and functions in @ccmode{} are +prefixed with @code{c-@var{<thing>}}, and @code{c-mode}, +@code{c++-mode}, @code{objc-mode}, and @code{java-mode} entry points are +provided. This file is intended to be a replacement for +@file{c-mode.el} and @file{c++-mode.el}. @findex c-version The major version number was incremented to 4 with the addition of @code{objc-mode}. To find the minor revision number of this release, use @kbd{M-x c-version RET}. -As of this writing (20-Aug-1996), both Emacs 19.33 and XEmacs 19.14 are -distributed with @code{cc-mode}, however neither have the very latest -version. In all likelihood, Emacs 19.34 and XEmacs 19.15 will contain -the latest version of @code{cc-mode}. You may therefore, want to -upgrade your copy of @code{cc-mode}. See @ref{Getting the latest -cc-mode release}. +As of this writing (27-Feb-1997), Emacs 19.34, XEmacs 19.14, and XEmacs +20.0 are all distributed with @ccmode{}, but they may not have the +latest releases. You may therefore, want to upgrade your copy of +@ccmode{}. See @ref{Getting the latest CC Mode release}. @cindex @file{cc-compat.el} file This distribution also contains a file called @file{cc-compat.el} which -should ease your transition from BOCM to @code{cc-mode}. It currently +should ease your transition from BOCM to @ccmode{}. It currently comes unguaranteed and unsupported, but this may change for future versions. If you have a BOCM configuration you are really happy with, -and want to postpone learning how to configure @code{cc-mode}, take a +and want to postpone learning how to configure @ccmode{}, take a look at that file. It maps BOCM configuration variables to -@code{cc-mode}'s new indentation model. +@ccmode{}'s new indentation model. A special word of thanks goes to Krishna Padmasola for his work in converting the original @file{README} file to texinfo format. @@ -185,12 +186,12 @@ @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -@file{cc-mode.el} works well with the 2 main branches of Emacs 19: -XEmacs, maintained by @code{xemacs.org} and the Emacs 19 maintained by -the Free Software Foundation. Emacs 19 users will want to use Emacs -version 19.21 or better, XEmacs users will want 19.6 or better. Earlier -versions of these Emacsen have deficiencies and/or bugs which will -adversely affect the performance and usability of @code{cc-mode}. You +@file{cc-mode.el} works well with the three main branches of Emacs: +XEmacs 19 and XEmacs 20, both maintained by @code{xemacs.org}, and Emacs +19 maintained by the Free Software Foundation. Emacs users will want to +use version 19.21 or better, XEmacs users will want 19.6 or better. +Earlier versions of these Emacsen have deficiencies and/or bugs which +will adversely affect the performance and usability of @ccmode{}. You are better off just getting the latest version of Emacs or XEmacs. @cindex @file{cc-mode-18.el} file @@ -198,18 +199,18 @@ @file{cc-mode-18.el} compatibility file, but only moderately well. A word of warning though, @emph{Emacs 18 lacks some fundamental functionality and ultimately, using Emacs 18 is a losing -battle}. Hence @code{cc-mode} under Emacs 18 is no longer supported and +battle}. Hence @ccmode{} under Emacs 18 is no longer supported and it is highly recommended that you upgrade to Emacs 19. If you use -@code{cc-mode} under Emacs 18, you're on your own. With @code{cc-mode} +@ccmode{} under Emacs 18, you're on your own. With @ccmode{} version 5, Emacs 18 support will be dropped altogether. Note that as of XEmacs 19.13 and Emacs 19.30, your Emacs already comes -with @code{cc-mode} version 4 preconfigured for your use. You should be +with @ccmode{} version 4 preconfigured for your use. You should be able to safely skip the rest of the setup information in this chapter, -unless you want to install the latest version of @code{cc-mode} into one +unless you want to install the latest version of @ccmode{} into one of these Emacsen. -@cindex @file{.emacs} file +@cindex .emacs file The first thing you will want to do is put @file{cc-mode.el} somewhere on your @code{load-path} so Emacs can find it. Do a @kbd{C-h v load-path RET} to see all the directories Emacs looks at when loading a @@ -253,9 +254,9 @@ @end example If you are running a version of Emacs or XEmacs that comes with -@code{cc-mode} by default, you can simply add the following to your +@ccmode{} by default, you can simply add the following to your @file{.emacs} file in order to upgrade to the latest version of -@code{cc-mode}: +@ccmode{}: @example (load "cc-mode") @@ -283,7 +284,7 @@ @end example After those lines you will want to add the following autoloads to your -@file{.emacs} file so that @code{cc-mode} gets loaded at the right time: +@file{.emacs} file so that @ccmode{} gets loaded at the right time: @example @group @@ -295,7 +296,7 @@ @end group @end example -Alternatively, if you want to make sure @code{cc-mode} is loaded when +Alternatively, if you want to make sure @ccmode{} is loaded when Emacs starts up, you could use this line instead of the autoloads above: @example @@ -335,35 +336,36 @@ That's all you need --- I know, I know, it sounds like a lot @code{:-)}, but after you've done all this, you should only need to quit and restart Emacs. The next time you visit a C, C++, Objective-C, or Java file you -should be using @code{cc-mode}. You can check this easily by hitting -@kbd{M-x c-version RET} in the @code{c-mode}, @code{c++-mode}, or -@code{objc-mode} buffer. You should see this message in the echo area: +should be using @ccmode{}. You can check this easily by hitting +@kbd{M-x c-version RET}; you should see this message in the echo area: @example -Using @code{cc-mode} version 4.@var{xxx} - -Where @var{xxx} is the latest release minor number. +Using CC Mode version 4.@var{xxx} @end example +@noindent +where @var{xxx} is the latest minor release number. + + @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node New Indentation Engine, Minor Modes, Getting Connected, Top -@comment node-name, next, previous,up +@comment node-name, next, previous, up @chapter New Indentation Engine @cindex New Indentation Engine @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -@code{cc-mode} has a new indentation engine, providing a simplified, yet -flexible and general mechanism for customizing indentation. It breaks -indentation calculation into two steps. First, for the line of code being -indented, @code{cc-mode} analyzes what kind of language construct it's -looking at, then it applies user defined offsets to the current line -based on this analysis. +@ccmode{} has a new indentation engine, providing a simplified, yet +flexible and general mechanism for customizing indentation. It separates +indentation calculation into two steps: first, @ccmode{} analyzes the +line of code being indented to determine the kind of language construct +it's looking at, then it applies user defined offsets to the current +line based on this analysis. This section will briefly cover how indentation is calculated in -@code{cc-mode}. It is important to understand the indentation model -being used so that you will know how to customize @code{cc-mode} for +@ccmode{}. It is important to understand the indentation model +being used so that you will know how to customize @ccmode{} for your personal coding style. @menu @@ -386,7 +388,7 @@ @cindex syntactic component @cindex syntactic component list @cindex relative buffer position -The first thing @code{cc-mode} does when indenting a line of code, is to +The first thing @ccmode{} does when indenting a line of code, is to analyze the line, determining the @dfn{syntactic component list} of the construct on that line. A @dfn{syntactic component} consists of a pair of information (in lisp parlance, a @emph{cons cell}), where the first @@ -405,9 +407,9 @@ indentation of some line higher up in the buffer. This is represented by the relative buffer position in the syntactic component. -It might help to see an example. Suppose we had the following code as -the only thing in a @code{c++-mode} buffer @footnote{The line numbers in -this and future examples don't actually appear in the buffer, of course!}: +Here is an example. Suppose we had the following code as the only thing +in a @code{c++-mode} buffer @footnote{The line numbers in this and +future examples don't actually appear in the buffer, of course!}: @example @group @@ -439,7 +441,7 @@ This tells us that the line is a statement and it is indented relative to buffer position 35, which happens to be the @samp{i} in @code{int} on -line 3. If you were to move Point to line 3 and hit @kbd{C-c C-s}, you +line 3. If you were to move point to line 3 and hit @kbd{C-c C-s}, you would see: @example @@ -479,10 +481,10 @@ @cindex substatment block @noindent which tells us that this is a brace that @emph{opens} a substatement -block. @footnote{A @dfn{substatement} indicates the line after an -@code{if}, @code{else}, @code{while}, @code{do}, @code{switch}, or -@code{for} statement, and a @dfn{substatement block} is a brace block -following one of those constructs.} +block. @footnote{A @dfn{substatement} indicates the line after a +conditional statement, such as @code{if}, @code{else}, @code{while}, +@code{do}, @code{switch}, or @code{for} in C. A @dfn{substatement +block} is a brace block following one of those conditionals.} @cindex comment only line Syntactic component lists can contain more than one component, and @@ -537,7 +539,7 @@ offsets to apply for those symbols. These offsets are added to a running total. -Second, if the component has a relative buffer position, @code{cc-mode} +Second, if the component has a relative buffer position, @ccmode{} adds the column number of that position to the running total. By adding up the offsets and columns for every syntactic component on the list, the final total indentation for the current line is computed. @@ -558,7 +560,7 @@ @end example @kindex TAB -Let's say Point is on line 3 and we hit the @key{TAB} key to re-indent +Let's say point is on line 3 and we hit the @key{TAB} key to re-indent the line. Remember that the syntactic component list for that line is: @example @@ -568,13 +570,13 @@ @end example @noindent -@code{cc-mode} looks up @code{defun-block-intro} in the +@ccmode{} looks up @code{defun-block-intro} in the @code{c-offsets-alist} variable. Let's say it finds the value @samp{4}; it adds this to the running total (initialized to zero), yielding a running total indentation of 4 spaces. -Next @code{cc-mode} goes to buffer position 29 and asks for the current -column. This brace is in column zero, so @code{cc-mode} +Next @ccmode{} goes to buffer position 29 and asks for the current +column. This brace is in column zero, so @ccmode{} adds @samp{0} to the running total. Since there is only one syntactic component on the list for this line, indentation calculation is complete, and the total indentation for the line @@ -605,9 +607,9 @@ @end example -Here, @code{cc-mode} first looks up the @code{substatement-open} symbol +Here, @ccmode{} first looks up the @code{substatement-open} symbol in @code{c-offsets-alist}. Let's say it finds the value @samp{4}. This -yields a running total of 4. @code{cc-mode} then goes to +yields a running total of 4. @ccmode{} then goes to buffer position 46, which is the @samp{i} in @code{if} on line 3. This character is in the fourth column on that line so adding this to the running total yields an indentation for the line of 8 spaces. @@ -622,7 +624,7 @@ @vindex c-echo-syntactic-information-p @vindex echo-syntactic-information-p (c-) @cindex TAB -To help you configure @code{cc-mode}, you can set the variable +As you configure @ccmode{}, you might want to set the variable @code{c-echo-syntactic-information-p} to non-@code{nil} so that the syntactic component list and calculated offset will always be echoed in the minibuffer when you hit @kbd{TAB}. @@ -636,16 +638,16 @@ @cindex Minor Modes @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -@code{cc-mode} contains two minor-mode-like features that you should +@ccmode{} contains two minor-mode-like features that you should find useful while you enter new C code. The first is called @dfn{auto-newline} mode, and the second is called @dfn{hungry-delete} mode. These minor modes can be toggled on and off independently, and -@code{cc-mode} can be configured so that it starts up with any +@ccmode{} can be configured so that it starts up with any combination of these minor modes. By default, both of these minor modes are turned off. The state of the minor modes is always reflected in the minor mode list -on the modeline of the @code{cc-mode} buffer. When auto-newline mode is +on the modeline of the @ccmode{} buffer. When auto-newline mode is enabled, you will see @samp{C/a} on the mode line @footnote{Remember that the @samp{C} could be replaced with @samp{C++}, @samp{ObjC}, or @samp{Java}.}. When hungry delete mode is enabled you would see @@ -660,7 +662,7 @@ @findex toggle-hungry-state (c-) @findex toggle-auto-state (c-) @findex toggle-auto-hungry-state (c-) -@code{cc-mode} provides keybindings which allow you to toggle the minor +@ccmode{} provides keybindings which allow you to toggle the minor modes on the fly while editing code. To toggle just the auto-newline state, hit @kbd{C-c C-a} (@code{c-toggle-auto-state}). When you do this, you should see the @samp{a} indicator either appear or disappear @@ -680,7 +682,8 @@ to your @file{.emacs} file: @example -(add-hook 'c-mode-common-hook '(lambda () (c-toggle-auto-hungry-state 1))) +(add-hook 'c-mode-common-hook + '(lambda () (c-toggle-auto-hungry-state 1))) @end example @@ -717,10 +720,9 @@ @cindex syntactic whitespace @item The character was not typed inside of a literal @footnote{A -@dfn{literal} is defined in @code{cc-mode} as any comment, -string, or cpp macro definition. These constructs are also known as -@dfn{syntactic whitespace} since they are usually ignored when scanning -C code.}. +@dfn{literal} is defined as any comment, string, or cpp macro +definition. These constructs are also known as @dfn{syntactic +whitespace} since they are usually ignored when scanning C code.}. @item @kindex C-u @@ -729,10 +731,16 @@ @end itemize -Certain other conditions may apply on a language specific basis. For +@findex c-enable-//-in-c-mode +@findex enable-//-in-c-mode (c-) +Some characters are electric in some languages, and not in others. For example, the second slash (@kbd{/}) of a C++ style line comment is electric in @code{c++-mode}, @code{objc-mode}, and @code{java-mode}, but -not in @code{c-mode}. +not in @code{c-mode}@footnote{Ordinarily, @samp{//} does not introduce a +comment in @code{c-mode}. However, if you call the function +@code{c-enable-//-in-c-mode}, @code{c-mode} will recognize C++ style +line comments. Note however that this is a global change which will +affect all your @code{c-mode} buffers.}. @menu @@ -805,7 +813,7 @@ using a function as a brace hanging @var{ACTION}. When the @var{ACTION} is a list, it can contain any combination of the -symbols @code{before} and @code{after}, directing @code{cc-mode} where to +symbols @code{before} and @code{after}, directing @ccmode{} where to put newlines in relationship to the brace being inserted. Thus, if the list contains only the symbol @code{after}, then the brace is said to @dfn{hang} on the right side of the line, as in: @@ -848,6 +856,12 @@ on the next line. Here, in the @code{block-close} entry, you also see an example of using a function as an @var{ACTION}. +A word of caution: it is not a good idea to hang top-level construct +introducing braces, such as @code{class-open} or @code{defun-open}. +Emacs makes an assumption that such braces will always appear in column +zero, hanging such braces can introduce performance problems. +@xref{Performance Issues} for more information. + @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node Hanging Colons, Hanging Semi-colons and commas, Hanging Braces, Auto-newline insertion @@ -872,7 +886,7 @@ In C++, double-colons are used as a scope operator but because these colons always appear right next to each other, newlines before and after them are controlled by a different mechanism, called @dfn{clean-ups} in -@code{cc-mode}. @xref{Clean-ups} for details. +@ccmode{}. @xref{Clean-ups} for details. @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @@ -883,7 +897,7 @@ @cindex Hanging Semi-colons and commas @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -Semicolons and commas are also electric in @code{cc-mode}, but since +Semicolons and commas are also electric in @ccmode{}, but since these characters do not correspond directly to syntactic symbols, a different mechanism is used to determine whether newlines should be automatically inserted after these characters. @xref{Customizing @@ -971,9 +985,9 @@ commands, or whitespace that contains intervening constructs. @cindex literal -You can configure @code{cc-mode}'s clean-ups by setting the variable +You can configure @ccmode{}'s clean-ups by setting the variable @code{c-cleanup-list}, which is a list of clean-up symbols. By default, -@code{cc-mode} cleans up only the @code{scope-operator} construct, which +@ccmode{} cleans up only the @code{scope-operator} construct, which is necessary for proper C++ support. Note that clean-ups are only performed when the construct does not occur within a literal (see @ref{Auto-newline insertion}), and when there is nothing but whitespace @@ -981,7 +995,7 @@ @vindex c-cleanup-list @vindex cleanup-list (c-) -There are currently only five specific constructs that @code{cc-mode} +There are currently only five specific constructs that @ccmode{} can clean up, as indicated by these symbols: @itemize @bullet @@ -1018,6 +1032,37 @@ @end example @item +@code{brace-elseif-brace} --- similar to the @code{brace-else-brace} +clean-up, but cleans up @samp{@} else if (...)@{} constructs. For +example: +@example +@group + +void spam(int i) +@{ + if( i==7 ) + @{ + dosomething(); + @} + else if( i==3 ) @{ + +@end group +@end example +@noindent +appears like this after the open brace is typed: +@example +@group + +void spam(int i) +@{ + if( i==7 ) @{ + dosomething(); + @} else if( i==3 ) @{ + +@end group +@end example + +@item @code{empty-defun-braces} --- cleans up braces following a top-level function or class definition that contains no body. Clean up occurs when the closing brace is typed. Thus the following: @@ -1109,7 +1154,7 @@ @vindex c-delete-function @vindex delete-function (c-) @cindex literal -By default, @code{cc-mode} actually runs the command +By default, @ccmode{} actually runs the command @code{c-electric-delete} when you hit @kbd{DEL}. When this command is used to delete a single character (i.e. when it is called interactively with no numeric argument), it really runs the function contained in the @@ -1142,8 +1187,6 @@ @cindex Indentation Commands @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -@findex c-set-style -@findex set-style (c-) Various commands are provided which allow you to conveniently re-indent C constructs. There are several things to note about these indentation commands. First, when you @@ -1157,24 +1200,24 @@ @findex hanging-braces-alist (c-) Second, changing some variables have no effect on existing code, even when you do re-indent. For example, the @code{c-hanging-*} variables -and @code{c-cleanup-list} only affect new code as it is typed in. So -for example, changing @code{c-hanging-braces-alist} and re-indenting the -buffer will not adjust placement of braces already in the file. +and @code{c-cleanup-list} only affect new code as it is typed in +on-the-fly, so changing @code{c-hanging-braces-alist} and re-indenting +the buffer will not adjust placement of braces already in the file. @vindex c-progress-interval @vindex progress-interval (c-) Third, re-indenting large portions of code is currently rather inefficient. Improvements have been made since previous releases of -@code{cc-mode}, and much more radical improvements are planned, but for -now you need to be aware of this @footnote{In particular, I have had -people complain about the speed that @code{cc-mode} re-indents -@code{lex(1)} output. Lex, yacc, and other code generators usually -output some pretty perversely formatted code. @emph{Don't} try to -indent this stuff with @code{cc-mode}!}. Some provision has been made -to at least inform you as to the progress of the re-indentation. The -variable @code{c-progress-interval} controls how often a progress -message is displayed. Set this variable to @code{nil} to inhibit -progress messages. Note that this feature only works with Emacs 19. +@ccmode{}, and much more radical improvements are planned, but for now +you need to be aware of this @footnote{In particular, I have had people +complain about the speed with which @code{lex(1)} output is re-indented. +Lex, yacc, and other code generators usually output some pretty +perversely formatted code. @emph{Don't} try to indent this stuff!}. +Some provision has been made to at least inform you as to the progress +of the re-indentation. The variable @code{c-progress-interval} controls +how often a progress message is displayed. Set this variable to +@code{nil} to inhibit progress messages. Note that this feature only +works with Emacs 19 and beyond. Also, except as noted below, re-indentation is always driven by the same mechanisms that control on-the-fly indentation of code. @xref{New @@ -1194,10 +1237,10 @@ (@code{c-indent-command}). The behavior of this command is controlled by the variable @code{c-tab-always-indent}. When this variable is @code{t}, @kbd{TAB} always just indents the current line. When -@code{nil}, the line is indented only if Point is at the left margin, or +@code{nil}, the line is indented only if point is at the left margin, or on or before the first non-whitespace character on the line, otherwise @emph{something else happens}@footnote{Actually what happens is that the -function stored in the variable @code{c-insert-tab-function} is called. +function stored in @code{c-insert-tab-function} is called. Normally this just inserts a real tab character, or the equivalent number of spaces, depending on the setting of the variable @code{indent-tabs-mode}. If you preferred, you could set @@ -1205,7 +1248,7 @@ If the value of @code{c-tab-always-indent} is something other than @code{t} or @code{nil} (e.g. @code{'other}), then a real tab character@footnote{The caveat about @code{indent-tabs-mode} in the -previous footnote also applies here.} is inserted only when Point is +previous footnote also applies here.} is inserted only when point is inside a literal (see @ref{Auto-newline insertion}), otherwise the line is indented. @@ -1213,7 +1256,7 @@ @findex c-indent-exp @findex indent-exp (c-) To indent an entire balanced brace or parenthesis expression, use -@kbd{M-C-q} (@code{c-indent-exp}). Note that Point should be on +@kbd{M-C-q} (@code{c-indent-exp}). Note that point should be on the opening brace or parenthesis of the expression you want to indent. @kindex C-c C-q @@ -1221,15 +1264,15 @@ @findex indent-defun (c-) Another very convenient keystroke is @kbd{C-c C-q} (@code{c-indent-defun}) when re-indents the entire top-level function or -class definition that encompases Point. It leaves Point at the +class definition that encompases point. It leaves point at the same position within the buffer. @kindex M-C-\ @findex indent-region To indent any arbitrary region of code, use @kbd{M-C-\} (@code{indent-region}). This is a standard Emacs command, specially -tailored for C code in a @code{cc-mode} buffer. Note that of course, -Point and Mark must delineate the region you +tailored for C code in a @ccmode{} buffer. Note that of course, +point and mark must delineate the region you want to indent. @kindex M-C-h @@ -1247,7 +1290,7 @@ @cindex Other Commands @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -@code{cc-mode} contains other useful command for moving around in C +@ccmode{} contains other useful command for moving around in C code. @table @code @@ -1255,9 +1298,9 @@ @kindex C-c C-u @findex c-up-conditional @findex up-conditional (c-) -Move Point back to the containing preprocessor conditional, leaving the -Mark behind. A prefix argument acts as a repeat count. With a negative -argument, move Point forward to the end of the containing +Move point back to the containing preprocessor conditional, leaving the +mark behind. A prefix argument acts as a repeat count. With a negative +argument, move point forward to the end of the containing preprocessor conditional. When going backwards, @code{#elif} is treated like @code{#else} followed by @code{#if}. When going forwards, @code{#elif} is ignored.@refill @@ -1266,7 +1309,7 @@ @kindex C-c C-p @findex c-backward-conditional @findex backward-conditional (c-) -Move Point back over a preprocessor conditional, leaving Mark +Move point back over a preprocessor conditional, leaving the mark behind. A prefix argument acts as a repeat count. With a negative argument, move forward. @@ -1274,7 +1317,7 @@ @kindex C-c C-n @findex c-forward-conditional @findex forward-conditional (c-) -Move Point forward across a preprocessor conditional, leaving Mark +Move point forward across a preprocessor conditional, leaving the mark behind. A prefix argument acts as a repeat count. With a negative argument, move backward. @@ -1282,34 +1325,34 @@ @kindex ESC a @findex c-beginning-of-statement @findex beginning-of-statement (c-) -Move Point to the beginning of the innermost C statement. If Point is +Move point to the beginning of the innermost C statement. If point is already at the beginning of a statement, it moves to the beginning of the preceding statement. With prefix argument @var{n}, move back @var{n} @minus{} 1 statements. -If Point is within a string or comment, or next to a comment (only -whitespace between them), this command moves by sentences instead of -statements. - -When called from a program, this function takes two optional arguments: -the numeric prefix argument, and a buffer position limit (don't move -back before that place). +If point is within a string or comment, or at the end of a comment, this +command moves by sentences instead of statements. + +When called from a program, this function takes three optional +arguments: the numeric prefix argument, a buffer position limit (don't +move back before that place), and a flag to indicate whether movement +should be by statements (if @code{t}) or sentence (if non-@code{nil}). @item M-e (c-end-of-statement) @kindex ESC e @findex c-end-of-statement @findex end-of-statement (c-) -Move Point to the end of the innermost C statement. If Point is at the +Move point to the end of the innermost C statement. If point is at the end of a statement, move to the end of the next statement. With prefix argument @var{n}, move forward @var{n} @minus{} 1 statements. -If Point is within a string or comment, or next to a comment (only -whitespace between them), this command moves by sentences instead of -statements. - -When called from a program, this function takes two optional arguments: -the numeric prefix argument, and a buffer position limit (don't move -past that place). +If point is within a string or comment, or at the end of a comment, this +command moves by sentences instead of statements. + +When called from a program, this function takes three optional +arguments: the numeric prefix argument, a buffer position limit (don't +move back before that place), and a flag to indicate whether movement +should be by statements (if @code{t}) or sentence (if non-@code{nil}). @item M-x c-forward-into-nomenclature @findex c-forward-into-nomenclature @@ -1319,13 +1362,13 @@ letter of each word is capitalized, and not separated by underscores. E.g. @samp{SymbolsWithMixedCaseAndNoUnderlines}. -This command moves Point forward to end of a C++ nomenclature +This command moves point forward to end of a C++ nomenclature section or word. With prefix argument @var{n}, move @var{n} times. @item M-x c-backward-into-nomenclature @findex c-backward-into-nomenclature @findex backward-into-nomenclature (c-) -Move Point backward to beginning of a C++ nomenclature +Move point backward to beginning of a C++ nomenclature section or word. With prefix argument @var{n}, move @var{n} times. If @var{n} is negative, move forward. @@ -1337,6 +1380,35 @@ operator without performing the electric behavior of colon insertion. @kbd{C-c :} does just this. +@item M-q (fill-paragraph) +@kindex ESC q +@findex fill-paragraph +@vindex c-hanging-comment-starter-p +@vindex c-hanging-comment-ender-p +@vindex hanging-comment-starter-p (c-) +@vindex hanging-comment-ender-p (c-) +The command is used to fill a block style (C) or line style (C++) +comment, in much the same way that text in the various text modes can be +filled. You should never attempt to fill non-comment code sections; +you'll end up with garbage! Two variables control how C style block +comments are filled, specifically how the comment start and end +delimiters are handled. + +The variable @code{c-hanging-comment-starter-p} controls whether comment +start delimiters which appear on a line by themselves, end up on a line +by themselves after the fill. When the value is @code{nil}, the comment +starter will remain on its own line@footnote{It will not be placed on a +separate line if it is not already on a separate line.}. Otherwise, +text on the next line will be put on the same line as the comment +starter. This is called @dfn{hanging} because the following text hangs +on the line with the comment starter. + +The variable @code{c-hanging-comment-ender-p} controls the analogous +behavior for the block comment end delimiter. When the value is +@code{nil}, the comment ender will remain on its own line after the +file@footnote{The same caveat as above holds true.}. Otherwise, the +comment end delimiter will be placed at the end of the previous line. + @end table @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @@ -1347,8 +1419,15 @@ @cindex Customizing Indentation @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! +@vindex c-offsets-alist +@vindex offsets-alist (c-) @cindex c-set-offset @cindex set-offset (c-) +The variable @code{c-offsets-alist} contains the mappings between +syntactic symbols and the offsets to apply for those symbols. You +should never modify this variable directly though. Use the function +@code{c-set-offset} instead (see below for details). + The @code{c-offsets-alist} variable is where you customize all your indentations. You simply need to decide what additional offset you want to add for every syntactic symbol. You can use the command @kbd{C-c @@ -1363,14 +1442,14 @@ @cindex basic-offset (c-) As mentioned previously, the variable @code{c-offsets-alist} is an association list of syntactic symbols and the offsets to be applied for -those symbols. In fact, these offset values can be any of an integer, -a function or variable name, or one of the following symbols: @code{+}, -@code{-}, @code{++}, @code{--}, @code{*}, or @code{/}. These symbols -describe offset in multiples of the value of the variable -@code{c-basic-offset}. By defining a style's indentation in terms of -this fundamental variable, you can change the amount of whitespace given -to an indentation level while leaving the same relationship between -levels. Here are the values that the special +those symbols. In fact, these offset values can be any of an integer, a +function or lambda expression, a variable name, or one of the following +symbols: @code{+}, @code{-}, @code{++}, @code{--}, @code{*}, or +@code{/}. These symbols describe offset in multiples of the value of +the variable @code{c-basic-offset}. By defining a style's indentation +in terms of this fundamental variable, you can change the amount of +whitespace given to an indentation level while leaving the same +relationship between levels. Here are the values that the special symbols correspond to: @table @code @@ -1390,6 +1469,8 @@ @end table +@vindex c-style-variables-are-local-p +@vindex style-variables-are-local-p (c-) @noindent So, for example, because most of the default offsets are defined in terms of @code{+}, @code{-}, and @code{0}, if you like the general @@ -1397,10 +1478,12 @@ you can probably achieve your style just by changing @code{c-basic-offset} like so (in your @file{.emacs} file)@footnote{The reason you need to use @code{setq-default} instead of @code{setq} is -that @code{c-basic-offset} is a buffer local variable, as are most of -the @code{cc-mode} configuration variables. If you were to put this -code in, e.g. your @code{c-mode-common-hook} function, you could use -@code{setq}.}: +that @code{c-basic-offset} is a buffer local variable, as are most +configuration variables. If you were to put this code in, e.g. your +@code{c-mode-common-hook} function, you could use @code{setq}. +Alternatively, you can keep these variables global by setting +@code{c-style-variables-are-local-p} to @code{nil}, but you must do this +before @code{cc-mode.el} is loaded into your Emacs session.}: @example (setq-default c-basic-offset 4) @@ -1521,7 +1604,7 @@ @code{substatement-open}, which is just the syntactic symbol we want to change! -After you hit return, @code{cc-mode} will then prompt you for the new +After you hit return, @ccmode{} will then prompt you for the new offset value, with the old value as the default. The default in this case is @samp{+}, but we want no extra indentation so enter @samp{0} and @kbd{RET}. This will associate the offset 0 with the @@ -1561,8 +1644,8 @@ @node Permanent Customization, Styles, Interactive Customization, Customizing Indentation @comment node-name, next, previous,up -@section Permanent Indentation -@cindex Permanent Indentation +@section Permanent Customization +@cindex Permanent Customization @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @vindex c-mode-common-hook @@ -1572,9 +1655,9 @@ @vindex java-mode-hook @cindex hooks To make this change permanent, you need to add some lisp code to your -@file{.emacs} file. @code{cc-mode} provides several hooks that you can +@file{.emacs} file. @ccmode{} provides several hooks that you can use to customize the mode according to your coding style. Each language -mode has its own hook, adhering to standard to Emacs major mode +mode has its own hook, adhering to standard Emacs major mode conventions. There is also one general hook: @itemize @bullet @@ -1606,11 +1689,12 @@ Here's a simplified example of what you can add to your @file{.emacs} file to make the changes described in the previous section -(@ref{Interactive Customization}) more permanent. See the Emacs -manuals for more information on customizing Emacs via hooks. -@xref{Sample .emacs File} for a more complete sample @file{.emacs} file. +(@ref{Interactive Customization}) more permanent. See the Emacs manuals +for more information on customizing Emacs via hooks. @xref{Sample +.emacs File} for a more complete sample @file{.emacs} file. @footnote{The use of @code{add-hook} in this example only works for -Emacs 19. Workarounds are available if you are using Emacs 18.} +Emacs 19 and beyond. Workarounds are available if you are using Emacs +18.} @example @group @@ -1642,7 +1726,7 @@ people who work on GNU software will have to use the GNU coding style on C code. Some shops are more lenient, allowing some variety of coding styles, and as programmers come and go, there could be a number of -styles in use. For this reason, @code{cc-mode} makes it convenient for +styles in use. For this reason, @ccmode{} makes it convenient for you to set up logical groupings of customizations called @dfn{styles}, associate a single name for any particular style, and pretty easily start editing new or existing code using these styles. This section @@ -1663,9 +1747,8 @@ @cindex Built-in Styles @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -If you're lucky, one of @code{cc-mode}'s built-in styles might be just -what you're looking for. Some of the most common C and C++ styles are -already built-in. These include: +If you're lucky, one of @ccmode{}'s built-in styles might be just +what you're looking for. These include: @itemize @bullet @item @@ -1698,6 +1781,17 @@ @code{euagate.eua.ericsson.se}}. @item +@cindex Linux style +@code{linux} --- C coding standard for Linux development. + +@item +@cindex Python style +@code{python} --- C coding standard for Python extension +modules@footnote{Python is a high level scripting language with a C/C++ +foreign function interface. For more information, see +@code{<http://www.python.org/>}.}. + +@item @cindex Java style @cindex java-mode @code{java} --- The style for editing Java code. Note that this style is @@ -1707,24 +1801,26 @@ @findex c-set-style @findex set-style (c-) +@kindex C-c . If you'd like to experiment with these built-in styles you can simply -type the following in a @code{cc-mode} buffer: +type the following in a @ccmode{} buffer: @example @group -@kbd{M-x c-set-style RET @var{STYLE-NAME} RET} +@kbd{C-c . @var{STYLE-NAME} RET} @end group @end example @noindent -Note that all style names are case insensitive, even the ones you define. +@kbd{C-c .} runs the command @code{c-set-style}. Note that all style +names are case insensitive, even the ones you define. Setting a style in this way does @emph{not} automatically re-indent your file. For commands that you can use to view the effect of your changes, see @ref{Commands}. Once you find a built-in style you like, you can make the change -permanent by adding a call to your @file{.emacs} file. Let's say for +permanent by adding some lisp to your @file{.emacs} file. Let's say for example that you want to use the @samp{ellemtel} style in all your files. You would add this: @example @@ -1740,6 +1836,7 @@ @end group @end example +@cindex cc-mode style There is one other special style you can use, called @samp{cc-mode} style. This style is special because all other styles implicitly inherit from it; in other words, whenever you set a style, @@ -1747,11 +1844,20 @@ your style need only define the differences between it and @samp{cc-mode} style. +Note @emph{you should never change any of the default styles}. +Instead, it's better to add a new style using @code{c-add-style} +(@xref{Adding Styles}). This is especially true for @code{cc-mode} and +@code{java} styles. + +@vindex c-indentation-style +@vindex indentation-style (c-) Note that for BOCM compatibility, @samp{gnu} is the default style, and any non-style based customizations you make (i.e. in @code{c-mode-common-hook} in your @file{.emacs} file) will be based on @samp{gnu} style unless you do -a @code{c-set-style} as the first thing in your hook. +a @code{c-set-style} as the first thing in your hook. The variable +@code{c-indentation-style} always contains the buffer's current style name, +as a string. @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @@ -1769,7 +1875,7 @@ If none of the built-in styles is appropriate, you'll probably want to add a new @dfn{style definition}. Styles are kept in the @code{c-style-alist} variable, but you should never modify this variable -directly. Instead, @code{cc-mode} provides the function +directly. Instead, @ccmode{} provides the function @code{c-add-style} that you can use to easily add new styles or change existing styles. This function takes two arguments, a @var{stylename} string, and an association list @var{description} of style @@ -1795,8 +1901,8 @@ The Emacs manual describes how you can customize certain variables on a per-file basis by including a @dfn{Local Variable} block at the end of the file. So far, you've only seen a functional interface to -@code{cc-mode}, which is highly inconvenient for use in a Local Variable -block. @code{cc-mode} provides two variables that make it easier for +@ccmode{}, which is highly inconvenient for use in a Local Variable +block. @ccmode{} provides two variables that make it easier for you to customize your style on a per-file basis. @vindex c-file-style @@ -1805,7 +1911,7 @@ @vindex file-offsets (c-) The variable @code{c-file-style} can be set to a style name string. -When the file is visited, @code{cc-mode} will automatically set the +When the file is visited, @ccmode{} will automatically set the file's style to this style using @code{c-set-style}. @vindex c-offsets-alist @@ -1814,13 +1920,15 @@ @findex set-offset (c-) Another variable, @code{c-file-offsets}, takes an association list similar to what is allowed in @code{c-offsets-alist}. When the file is -visited, @code{cc-mode} will automatically institute these offets using +visited, @ccmode{} will automatically institute these offets using @code{c-set-offset}. Note that file style settings (i.e. @code{c-file-style}) are applied before file offset settings (i.e. @code{c-file-offsets})@footnote{File -styles are only supported since XEmacs 19.12 and Emacs 19.29. They work -via the standard Emacs hook variable @code{hack-local-variables-hook}.}. +styles have only been supported since XEmacs 19.12 and Emacs 19.29. +They work via the standard Emacs hook variable +@code{hack-local-variables-hook}. Older Emacsen lack this hook, so file +styles can't be used with them.}. @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @@ -1835,17 +1943,36 @@ @vindex style-alist (c-) @vindex c-basic-offset @vindex basic-offset (c-) -For most users, @code{cc-mode} will support their coding styles with +For most users, @ccmode{} will support their coding styles with very little need for more advanced customizations. Usually, one of the standard styles defined in @code{c-style-alist} will do the trick. At most, perhaps one of the syntactic symbol offsets will need to be tweaked slightly, or maybe @code{c-basic-offset} will need to be changed. However, some styles require a more flexible framework for -customization, and one of the real strengths of @code{cc-mode} is that +customization, and one of the real strengths of @ccmode{} is that the syntactic analysis model provides just such a framework. This allows -you to implement special indentation calculations for situations not +you to implement custom indentation calculations for situations not handled by the mode directly. +@vindex c-style-variables-are-local-p +@vindex style-variables-are-local-p +Note that the style controlling variables can either have global values, +or can be buffer local (e.g. different in every buffer). If all the C +files you edit tend to have the same style, you might want to keep the +variables global. If you tend to edit files with many different styles, +you will have to make the variables buffer local. The variable +@code{c-style-variables-are-local-p} controls this. + +When @code{c-style-variables-are-local-p} is non-nil, then the style +variables will have a different settable value for each buffer, +otherwise all buffers will share the same values. This variable only +takes effect when @ccmode{} is loaded into your Emacs session. By +default (for backwards compatibility reasons), its value is @code{t}. +Note that once the variables are made buffer local, they will retain +this property for the remainder of the current Emacs session. To change +this behavior, set @code{c-style-variables-are-local-p} to @code{nil} +@emph{before} you load @file{cc-mode.el}. + @menu * Custom Indentation Functions:: * Custom Brace and Colon Hanging:: @@ -1862,11 +1989,11 @@ @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @cindex custom indentation functions -One of the most common ways to customize @code{cc-mode} is by writing -@dfn{custom indentation functions} and associating them with specific -syntactic symbols (see @ref{Syntactic Symbols}). @code{cc-mode} itself -uses custom indentation functions to provide more sophisticated -indentation, for example when lining up C++ stream operator blocks: +The most flexible way to customize @ccmode{} is by writing @dfn{custom +indentation functions} and associating them with specific syntactic +symbols (see @ref{Syntactic Symbols}). @ccmode{} itself uses custom +indentation functions to provide more sophisticated indentation, for +example when lining up C++ stream operator blocks: @example @group @@ -1885,7 +2012,7 @@ syntactic symbol. Here, @code{stream-op} has an offset of @code{+}, and with a @code{c-basic-offset} of 2, you can see that lines 4 through 6 are simply indented two spaces to the right of line 3. But perhaps we'd -like @code{cc-mode} to be a little more intelligent so that it lines up +like @ccmode{} to be a little more intelligent so that it lines up all the @samp{<<} symbols in lines 3 through 6. To do this, we have to write a custom indentation function which finds the column of first stream operator on the first line of the statement. Here is the lisp @@ -1912,7 +2039,7 @@ running total indentation for the line. Note that what actually gets returned is the difference between the column that the first stream operator is on, and the column of the buffer relative position passed in -the function's argument. Remember that @code{cc-mode} automatically +the function's argument. Remember that @ccmode{} automatically adds in the column of the component's relative buffer position and we don't want that value added into the final total twice. @@ -1951,7 +2078,7 @@ @vindex offsets-alist (c-) Custom indentation functions can be as simple or as complex as you like, and any syntactic symbol that appears in @code{c-offsets-alist} can have -a custom indentation function associated with it. @code{cc-mode} comes +a custom indentation function associated with it. @ccmode{} comes with several standard custom indentation functions, not all of which are used by the default styles. @@ -2047,7 +2174,7 @@ @vindex c-hanging-braces-alist @vindex hanging-braces-alist (c-) Syntactic symbols aren't the only place where you can customize -@code{cc-mode} with the lisp equivalent of callback functions. Brace +@ccmode{} with the lisp equivalent of callback functions. Brace ``hanginess'' can also be determined by custom functions associated with syntactic symbols on the @code{c-hanging-braces-alist} variable. Remember that @var{ACTION}'s are typically a list containing some @@ -2064,7 +2191,7 @@ function can also return @code{nil}. This return value has the normal brace hanging semantics. -As an example, @code{cc-mode} itself uses this feature to dynamically +As an example, @ccmode{} itself uses this feature to dynamically determine the hanginess of braces which close ``do-while'' constructs: @example @@ -2084,7 +2211,7 @@ @findex c-snug-do-while @findex snug-do-while (c-) -@code{cc-mode} assigns the @code{block-close} syntactic symbol to the +@ccmode{} assigns the @code{block-close} syntactic symbol to the brace that closes the @code{do} construct, and normally we'd like the line that follows a @code{block-close} brace to begin on a separate line. However, with ``do-while'' constructs, we want the @@ -2130,7 +2257,7 @@ Note that for symmetry, colon hanginess should be customizable by allowing function symbols as @var{ACTION}s on the @code{c-hanging-colon-alist} variable. Since no use has actually been -found for this feature, it isn't currently implemented. +found for this feature, it isn't currently implemented! @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node Customizing Semi-colons and Commas, Other Special Indentations, Custom Brace and Colon Hanging, Advanced Customizations @@ -2173,7 +2300,8 @@ Here's an example of a criteria function that will prevent newlines from being inserted after semicolons when there is a non-blank following -line. Otherwise, it makes no determination: +line. Otherwise, it makes no determination. To use, add this to the +front of the @code{c-hanging-semi&comma-criteria} list. @example @group @@ -2201,20 +2329,20 @@ @vindex c-label-minimum-indentation @vindex label-minimum-indentation (c-) In @samp{gnu} style (see @ref{Built-in Styles}), a minimum indentation -is imposed on lines with @code{label} or @code{case-label} syntax. This -minimum indentation is controlled by the variable +is imposed on lines inside top-level constructs. This minimum +indentation is controlled by the variable @code{c-label-minimum-indentation}. The default value for this variable is 1. @vindex c-special-indent-hook @vindex special-indent-hook (c-) -One other customization variable is available in @code{cc-mode}: +One other customization variable is available in @ccmode{}: @code{c-special-indent-hook}. This is a standard hook variable that is -called after every line is indented by @code{cc-mode}. You can use it +called after every line is indented by @ccmode{}. You can use it to do any special indentation or line adjustments your style dictates, such as adding extra indentation to constructors or destructor declarations in a class definition, etc. Note however, that you should -not change Point or Mark inside your @code{c-special-indent-hook} +not change point or mark inside your @code{c-special-indent-hook} functions (i.e. you'll probably want to wrap your function in a @code{save-excursion}). @@ -2262,14 +2390,16 @@ @item @code{inline-close} --- brace that closes an in-class inline method @item -@code{ansi-funcdecl-cont} --- the nether region between an ANSI function -declaration and the defun opening brace +@code{func-decl-cont} --- the nether region between a function +declaration's argument list and the defun opening brace. In C++ and +Java, this can include the @code{throws} clauses of a method +declaration. @item @code{knr-argdecl-intro} --- first line of a K&R C argument declaration @item @code{knr-argdecl} --- subsequent lines in a K&R C argument declaration @item -@code{topmost-intro} --- the first line in a topmost construct definition +@code{topmost-intro} --- the first line in a topmost definition @item @code{topmost-intro-cont} --- topmost definition continuation lines @item @@ -2293,9 +2423,9 @@ @item @code{brace-list-entry} --- subsequent lines in an enum or static array list @item -@code{statement} --- a C (or like) statement +@code{statement} --- a C statement @item -@code{statement-cont} --- a continuation of a C (or like) statement +@code{statement-cont} --- a continuation of a C statement @item @code{statement-block-intro} --- the first line in a new statement block @item @@ -2304,19 +2434,20 @@ @code{statement-case-open} --- the first line in a case block starting with brace @item -@code{substatement} --- the first line after an if/while/for/do/else +@code{substatement} --- the first line after a conditional @item @code{substatement-open} --- the brace that opens a substatement block @item @code{case-label} --- a case or default label @item -@code{access-label} --- C++ private/protected/public access label +@code{access-label} --- C++ access control label @item -@code{label} --- any non-special C (or like) label +@code{label} --- any non-special C label @item -@code{do-while-closure} --- the `while' that ends a do/while construct +@code{do-while-closure} --- the `while' that ends a +@code{do}-@code{while} construct @item -@code{else-clause} --- the `else' of an if/else construct +@code{else-clause} --- the `else' of an @code{if}-@code{else} construct @item @code{comment-intro} --- a line containing only a comment introduction @item @@ -2330,9 +2461,9 @@ @item @code{arglist-close} --- the solo close paren of an argument list @item -@code{stream-op} --- lines continuing a stream operator construct +@code{stream-op} --- lines continuing a stream operator @item -@code{inclass} --- the construct is nested inside a class definition +@code{inclass} --- the line is nested inside a class definition @item @code{cpp-macro} --- the start of a cpp macro @item @@ -2349,7 +2480,8 @@ @item @code{extern-lang-close} --- brace that closes an external language block @item -@code{inextern-lang} --- analogous to `inclass' syntactic symbol +@code{inextern-lang} --- analogous to `inclass' syntactic symbol, but +for @code{extern} blocks. @end itemize @cindex -open syntactic symbols @@ -2750,19 +2882,14 @@ parenthesis that opens the list is @emph{not empty} following the open parenthesis. Contrast this against lines 6 and 13 which are assigned @code{arglist-cont} syntax. This is because the parenthesis that opens -their argument lists is the last character on that line @footnote{The -need for this somewhat confusing arrangement is that the typical -indentation desired for these lines is calculated differently. -This should be simplified in version 5 of @code{cc-mode}, along with the -added distinction between argument lists in function declarations, and -argument lists in function calls.}. +their argument lists is the last character on that line. Note that there is no @code{arglist-open} syntax. This is because any parenthesis that opens an argument list, appearing on a separate line, is assigned the @code{statement-cont} syntax instead. A few miscellaneous syntactic symbols that haven't been previously -covered are illustrated by this example: +covered are illustrated by this C++ example: @example @group @@ -2772,7 +2899,7 @@ 4: /* this line starts a multi-line 5: * comment. This line should get `c' syntax */ 6: - 7: char* a_long_multiline_string = "This line starts a multi-line \ + 7: char* a_multiline_string = "This line starts a multi-line \ 8: string. This line should get `string' syntax."; 9: 10: note: @@ -2794,17 +2921,14 @@ @itemize @bullet -@cindex ansi-funcdecl-cont syntactic symbol +@cindex func-decl-cont syntactic symbol @item -line 2, assigned the @code{ansi-funcdecl-cont} syntax; +line 2, assigned the @code{func-decl-cont} syntax; @cindex comment-intro syntactic symbol @item line 4, assigned both @code{defun-block-intro} @emph{and} -@code{comment-intro} syntax @footnote{The @code{comment-intro} syntactic -symbol is is another example of a @dfn{modifier} since it always appears -on a syntactic analysis list with other symbols, and rarely has an -associated relative buffer position.}; +@code{comment-intro} syntax; @cindex c syntactic symbol @item @@ -2837,7 +2961,7 @@ @cindex stream-op syntactic symbol @item -line 17, assigned @code{stream-op} syntax @footnote{In C++ only.}. +line 17, assigned @code{stream-op} syntax. @end itemize @@ -2897,24 +3021,19 @@ @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! C and it's derivative languages are highly complex creatures. Often, -ambiguous code situations arise that require @code{cc-mode} to scan +ambiguous code situations arise that require @ccmode{} to scan large portions of the buffer to determine syntactic context. Such pathological code@footnote{such as the output of @code{lex(1)}!} -can cause @code{cc-mode} to perform fairly badly. +can cause @ccmode{} to perform fairly badly. This section identifies some of the coding styles to watch out for, and suggests some workarounds that you can use to improve performance. -Note that this is an area that will get a lot of attention in -@code{cc-mode} version 5. The mode should end up being much faster, at -the expense of dropping Emacs 18 support, owing to the implementation of -syntactic analysis caching. - -Because @code{cc-mode} has to scan the buffer backwards from the current +Because @ccmode{} has to scan the buffer backwards from the current insertion point, and because C's syntax is fairly difficult to parse in -the backwards direction, @code{cc-mode} often tries to find the nearest +the backwards direction, @ccmode{} often tries to find the nearest position higher up in the buffer from which to begin a forward scan. The farther this position is from the current insertion point, the -slower the mode gets. Some coding styles can even force @code{cc-mode} +slower the mode gets. Some coding styles can even force @ccmode{} to scan from the beginning of the buffer! @findex beginning-of-defun @@ -2922,46 +3041,61 @@ One of the simplest things you can do to reduce scan time, is make sure any brace that opens a top-level block construct always appears in the leftmost column. This is actually an Emacs constraint, as embodied in -the @code{beginning-of-defun} function which @code{cc-mode} uses +the @code{beginning-of-defun} function which @ccmode{} uses heavily. If you insist on hanging top-level open braces on the right side of the line, then you should set the variable @code{defun-prompt-regexp} to something reasonable @footnote{Note that this variable is only defined in Emacs 19.}, however that ``something -reasonable'' is difficult to define, so @code{cc-mode} doesn't do it +reasonable'' is difficult to define, so @ccmode{} doesn't do it for you. -You will probably notice pathological behavior from @code{cc-mode} when +@vindex c-Java-defun-prompt-regexp +@vindex Java-defun-prompt-regexp (c-) +A special note about @code{defun-prompt-regexp} in Java mode: while much +of the early sample Java code seems to encourage a style where the brace +that opens a class is hung on the right side of the line, this is not a +good style to pursue in Emacs. @ccmode{} comes with a variable +@code{c-Java-defun-prompt-regexp} which tries to define a regular +expression usable for this style, but there are problems with it. In +some cases it can cause @code{beginning-of-defun} to hang@footnote{This +has been observed in Emacs 19.34 and XEmacs 19.15.}. For this reason, +it is not used by default, but if you feel adventurous, you can set +@code{defun-prompt-regexp} to it in your mode hook. In any event, +setting and rely on @code{defun-prompt-regexp} will definitely slow +things down! + +You will probably notice pathological behavior from @ccmode{} when working in files containing large amounts of cpp macros. This is -because @code{cc-mode} cannot quickly skip backwards over these lines. +because @ccmode{} cannot quickly skip backwards over these lines. @vindex c-recognize-knr-p @vindex recognize-knr-p (c-) -Previous versions of @code{cc-mode} had potential performance problems -when recognizing ``K&R'' style function argument declarations. This was +Previous versions of @ccmode{} had potential performance problems +when recognizing K&R style function argument declarations. This was because there are ambiguities in the C syntax when K&R style argument -lists are used (it is hard to distinguish them from top-level -declarations). @code{cc-mode} has adopted BOCM's convention for +lists are used@footnote{It is hard to distinguish them from top-level +declarations.}. @ccmode{} has adopted BOCM's convention for limiting the search: it assumes that argdecls are indented at least one space, and that the function headers are not indented at all. With -current versions of @code{cc-mode}, -@code{c-recognize-knr-p} is deprecated. +current versions of @ccmode{}, user customization of +@code{c-recognize-knr-p} is deprecated. Just don't put argdecls in +column zero! @cindex @file{cc-lobotomy.el} file @vindex cc-lobotomy-pith-list -You might want to investigate some of the -speed-ups contained in the file @file{cc-lobotomy.el}, which -is part of the canonical @code{cc-mode} distribution. As mentioned -previous, @code{cc-mode} always trades accuracy for speed, however it is -recognized that sometimes you need speed and can sacrifice some accuracy -in indentation. The file @file{cc-lobotomy.el} contains hacks that -will ``dumb down'' @code{cc-mode} in some specific ways, making that -trade-off of speed for accuracy. I won't go into details of its use -here; you should read the comments at the top of the file, and look at -the variable @code{cc-lobotomy-pith-list} for details. +You might want to investigate the speed-ups contained in the +file @file{cc-lobotomy.el}, which is part of the @ccmode{} distribution. +As mentioned previous, @ccmode{} always trades accuracy for speed, +however it is recognized that sometimes you need speed and can sacrifice +some accuracy in indentation. The file @file{cc-lobotomy.el} contains +hacks that will ``dumb down'' @ccmode{} in some specific ways, making +that trade-off of speed for accuracy. I won't go into details of its +use here; you should read the comments at the top of the file, and look +at the variable @code{cc-lobotomy-pith-list} for details. @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -@node Frequently Asked Questions, Getting the latest cc-mode release, Performance Issues, Top +@node Frequently Asked Questions, Getting the latest CC Mode release, Performance Issues, Top @comment node-name, next, previous,up @chapter Frequently Asked Questions @@ -3014,17 +3148,17 @@ @end example -This is a very common question. @code{:-)} If you want this to be the -default behavior, don't lobby me, lobby RMS! +This is a very common question. If you want this to be the default +behavior, don't lobby me, lobby RMS! @code{:-)} @sp 1 @strong{Q.} @emph{I put @code{(c-set-offset 'substatement-open 0)} in my @file{.emacs} file but I get an error saying that @code{c-set-offset}'s function definition is void.} -@strong{A.} This means that @code{cc-mode} wasn't loaded into your +@strong{A.} This means that @ccmode{} wasn't loaded into your Emacs session by the time the @code{c-set-offset} call was reached, -mostly likely because @code{cc-mode} is being autoloaded. Instead +mostly likely because @ccmode{} is being autoloaded. Instead of putting the @code{c-set-offset} line in your top-level @file{.emacs} file, put it in your @code{c-mode-common-hook}, or simply add the following to the top of your @file{.emacs} file: @@ -3042,31 +3176,36 @@ constructs appear in different colors, or in bold face, etc.?} @strong{A.} ``Syntax Colorization'' is an Emacs 19 feature, controlled -by @code{font-lock-mode}. It is not part of @code{cc-mode}. +by @code{font-lock-mode}. It is not part of @ccmode{}. @sp 1 @strong{Q.} @emph{I @code{setq} @code{c-basic-offset} to 4 in my @file{.emacs} file, but why does everything still get indented with only 2 spaces?} -@strong{A.} It's because @code{c-basic-offset} is now a ``buffer local -variable'', meaning its value is unique to each buffer. The prefered -way to customize this is to change its value in a ``mode hook'' (most -likely @code{c-mode-common-hook}. Alternatively you can use -@code{setq-default} to change its value globally. +@vindex c-style-variables-are-local-p +@vindex style-variables-are-local-p +@strong{A.} It's because @code{c-basic-offset} is, by default, a +``buffer local variable'', meaning its value is unique to each buffer. +The prefered way to customize this is to change its value in a ``mode +hook'' (most likely @code{c-mode-common-hook}). Alternatively you can +use @code{setq-default} to change its value globally. Better yet, +before you load @file{cc-mode.el}, set the variable +@code{c-style-variables-are-local-p} to @code{nil}. @xref{Advanced +Customizations}. @end quotation @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -@node Getting the latest cc-mode release, Sample .emacs File, Frequently Asked Questions, Top +@node Getting the latest CC Mode release, Sample .emacs File, Frequently Asked Questions, Top @comment node-name, next, previous,up -@chapter Getting the latest @code{cc-mode} release -@cindex Getting the latest @code{cc-mode} release +@chapter Getting the latest CC Mode release +@cindex Getting the latest CC Mode release @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -@code{cc-mode} is now distributed with both Emacs 19 and XEmacs 19, so +@ccmode{} is now distributed with Emacs 19, XEmacs 19, and XEmacs 20, so you would typically just use the version that comes with your Emacs. These may be slightly out of date due to release schedule skew, so you should always check the canonical site for the latest version. @@ -3089,10 +3228,16 @@ distribution (named @code{cc-mode.tar.gz}; a gzip'd tar file), or any of the individual files, including PostScript documentation. -If you do not have anonymous ftp access, you can get the distribution -through an anonymous ftp-to-mail gateway, such as the one run by DEC at -@code{ftpmail@@decwrl.dec.com}. To get @code{cc-mode} via email, send -the following message in the body of your mail to that address: +If you do not have World Wide Web, or anonymous ftp access, you can get +the distribution through an anonymous ftp-to-mail gateway, such as the +one run by DEC at: +@example + +@code{ftpmail@@decwrl.dec.com} + +@end example +To get @ccmode{} via email, send the following message in the body of +your mail to that address: @example reply <a valid net address back to you> @@ -3110,11 +3255,11 @@ about its availability! @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -@node Sample .emacs File, Limitations and Known Bugs, Getting the latest cc-mode release, Top +@node Sample .emacs File, Limitations and Known Bugs, Getting the latest CC Mode release, Top @comment node-name, next, previous,up -@chapter Sample @file{.emacs} file -@cindex Sample @file{.emacs} file +@chapter Sample .emacs file +@cindex Sample .emacs file @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @example @@ -3123,23 +3268,23 @@ ;; change some of the actual values. (defconst my-c-style - '((c-tab-always-indent . t) - (c-comment-only-line-offset . 4) - (c-hanging-braces-alist . ((substatement-open after) - (brace-list-open))) - (c-hanging-colons-alist . ((member-init-intro before) - (inher-intro) - (case-label after) - (label after) - (access-label after))) - (c-cleanup-list . (scope-operator - empty-defun-braces - defun-close-semi)) - (c-offsets-alist . ((arglist-close . c-lineup-arglist) - (substatement-open . 0) - (case-label . 4) - (block-open . 0) - (knr-argdecl-intro . -))) + '((c-tab-always-indent . t) + (c-comment-only-line-offset . 4) + (c-hanging-braces-alist . ((substatement-open after) + (brace-list-open))) + (c-hanging-colons-alist . ((member-init-intro before) + (inher-intro) + (case-label after) + (label after) + (access-label after))) + (c-cleanup-list . (scope-operator + empty-defun-braces + defun-close-semi)) + (c-offsets-alist . ((arglist-close . c-lineup-arglist) + (substatement-open . 0) + (case-label . 4) + (block-open . 0) + (knr-argdecl-intro . -))) (c-echo-syntactic-information-p . t) ) "My C Programming Style") @@ -3183,14 +3328,13 @@ Re-indenting large regions or expressions can be slow. @item -Use with Emacs 18 can be slow and annoying. You should seriously +Use with Emacs 18 can be slow and annoying. You should seriously consider upgrading to Emacs 19. @item -There is still some weird behavior when filling C block comments. -My suggestion is to check out separate fill packages such as -@code{filladapt} or @code{adaptive-fill-mode}. These can do a much -better job of filling comment regions. +Add-on fill packages may not work as well as @ccmode{}'s built-in +filling routines. I no longer recommend you use @code{filladapt} to +fill comments. @cindex c-indent-exp @cindex indent-exp (c-) @@ -3225,9 +3369,10 @@ Bug reports are now sent to the following email addresses: @code{cc-mode-help@@python.org} and -@code{bug-gnu-emacs@@prep.ai.mit.edu}; the latter is mirrored on the Usenet -newsgroup @code{gnu.emacs.bug}. Other questions and suggestions should -be mailed to @code{help-gnu-emacs@@prep.ai.mit.edu} which is mirrored on +@code{bug-gnu-emacs@@prep.ai.mit.edu}; the latter is mirrored on the +Usenet newsgroup @code{gnu.emacs.bug}. You can send other questions, +suggestions, and kudos to @code{cc-mode-help@@python.org}, or +@code{help-gnu-emacs@@prep.ai.mit.edu} which is mirrored on newsgroup @code{gnu.emacs.help}. @@ -3249,7 +3394,7 @@ @ifinfo @end ifinfo -Since all @code{cc-mode} commands are prepended with the string +Since all @ccmode{} commands are prepended with the string @samp{c-}, each appears under its @code{c-@var{<thing>}} name and its @code{@var{<thing>} (c-)} name. @iftex @@ -3273,10 +3418,7 @@ @unnumbered Variable Index @c !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -@ifinfo - -@end ifinfo -Since all @code{cc-mode} variables are prepended with the string +Since all @ccmode{} variables are prepended with the string @samp{c-}, each appears under its @code{c-@var{<thing>}} name and its @code{@var{<thing>} (c-)} name. @iftex