\input texinfo @c Notes to self regarding line handling: @c @c Empty lines are often significant before @end directives; avoid them. @c @c Empty lines before and after @example directives are significant in @c info output but not in TeX. Empty lines inside @example directives @c are significant. @c Conventions for formatting examples: @c o If the example contains empty lines then put the surrounding empty @c lines inside the @example directives. Put them outside otherwise. @c o Use @group inside the example only if it shows indentation where @c the relation between lines inside is relevant. @c o Format line number columns like this: @c 1: foo @c 2: bar @c ^ one space @c ^^ two columns, right alignment @c o Check line lengths in TeX output; they can typically be no longer @c than 70 chars, 60 if the paragraph is indented. @comment TBD: Document the finer details of statement anchoring? @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @comment %**start of header (This is for running Texinfo on a region) @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @comment How to make the various output formats: @comment (Thanks to Robert Chassell for supplying this information.) @comment Note that Texinfo 4.7 (or later) is needed. @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @ignore In each of the following pairs of commands, the first generates a version with cross references pointing to the GNU Emacs manuals, the second with them pointing to the XEmacs manuals. ## Info output makeinfo cc-mode.texi makeinfo -DXEMACS cc-mode.texi ## DVI output ## You may need to set up the environment variable TEXINPUTS so ## that tex can find the file texinfo.tex - See the tex ## manpage. texi2dvi cc-mode.texi texi2dvi -t "@set XEMACS " cc-mode.texi ## HTML output. (The --no-split parameter is optional) makeinfo --html --no-split cc-mode.texi makeinfo --html --no-split -DXEMACS cc-mode.texi ## Plain text output makeinfo --fill-column=70 --no-split --paragraph-indent=0 \ --no-headers --output=cc-mode.txt cc-mode.texi makeinfo --fill-column=70 --no-split --paragraph-indent=0 \ --no-headers --output=cc-mode.txt -DXEMACS cc-mode.texi ## DocBook output makeinfo --docbook --no-split --paragraph-indent=0 \ cc-mode.texi makeinfo --docbook --no-split --paragraph-indent=0 \ -DXEMACS cc-mode.texi ## XML output makeinfo --xml --no-split --paragraph-indent=0 \ cc-mode.texi makeinfo --xml --no-split --paragraph-indent=0 \ -DXEMACS cc-mode.texi #### (You must be in the same directory as the viewed file.) ## View DVI output xdvi cc-mode.dvi & ## View HTML output mozilla cc-mode.html @end ignore @comment No overfull hbox marks in the dvi file. @finalout @setfilename ../info/ccmode @settitle CC Mode Manual @footnotestyle end @c The following four macros generate the filenames and titles of the @c main (X)Emacs manual and the Elisp/Lispref manual. Leave the @c Texinfo variable `XEMACS' unset to generate a GNU Emacs version, set it @c to generate an XEmacs version, e.g. with @c "makeinfo -DXEMACS cc-mode.texi". @ifset XEMACS @macro emacsman xemacs @end macro @macro emacsmantitle XEmacs User's Manual @end macro @macro lispref lispref @end macro @macro lispreftitle XEmacs Lisp Reference Manual @end macro @end ifset @ifclear XEMACS @macro emacsman emacs @end macro @macro emacsmantitle GNU Emacs Manual @end macro @macro lispref elisp @end macro @macro lispreftitle GNU Emacs Lisp Reference Manual @end macro @end ifclear @macro ccmode CC Mode @end macro @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @comment @setchapternewpage odd !! we don't want blank pages !! @comment %**end of header (This is for running Texinfo on a region) @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @comment @comment Texinfo manual for CC Mode @comment Generated from the original README file by Krishna Padmasola @comment @comment @comment Authors: @comment Barry A. Warsaw @comment Martin Stjernholm @comment Alan Mackenzie @comment @comment Maintained by Martin Stjernholm and Alan Mackenzie @comment @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @comment Define an index for syntactic symbols. @ifnottex @c In texi2dvi, the @defindex would create an empty cc-mode.ss @c For Info, unlike tex, @syncodeindex needs a matching @defindex. @defindex ss @end ifnottex @comment Combine key, syntactic symbol and concept indices into one. @syncodeindex ss cp @syncodeindex ky cp @copying This manual is for CC Mode in Emacs. Copyright @copyright{} 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc. @quotation Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with the Invariant Sections being ``The GNU Manifesto'', ``Distribution'' and ``GNU GENERAL PUBLIC LICENSE'', with the Front-Cover texts being ``A GNU Manual'', and with the Back-Cover Texts as in (a) below. A copy of the license is included in the section entitled ``GNU Free Documentation License'' in the Emacs manual. (a) The FSF's Back-Cover Text is: ``You have freedom to copy and modify this GNU Manual, like GNU software. Copies published by the Free Software Foundation raise funds for GNU development.'' This document is part of a collection distributed under the GNU Free Documentation License. If you want to distribute this document separately from the collection, you can do so by adding a copy of the license to the document, as described in section 6 of the license. @end quotation @end copying @comment Info directory entry for use by install-info. The indentation @comment here is by request from the FSF folks. @dircategory Emacs @direntry * CC Mode: (ccmode). Emacs mode for editing C, C++, Objective-C, Java, Pike, AWK, and CORBA IDL code. @end direntry @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @comment TeX title page @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @titlepage @sp 10 @center @titlefont{CC Mode 5.31} @sp 2 @center @subtitlefont{A GNU Emacs mode for editing C and C-like languages} @sp 2 @center Barry A. Warsaw, Martin Stjernholm, Alan Mackenzie @page @vskip 0pt plus 1filll @insertcopying This manual was generated from $Revision$ of $RCSfile$, which can be downloaded from @url{http://cvs.savannah.gnu.org/viewcvs/emacs/emacs/man/cc-mode.texi}. @end titlepage @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @comment The Top node contains the master menu for the Info file. @comment This appears only in the Info file, not the printed manual. @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node Top, Introduction, (dir), (dir) @comment node-name, next, previous, up @ifinfo @top @ccmode{} @ccmode{} is a GNU Emacs mode for editing files containing C, C++, Objective-C, Java, CORBA IDL (and the variants PSDL and CIDL), Pike and AWK code. It provides syntax-based indentation, font locking, and has several handy commands and some minor modes to make the editing easier. It does not provide tools to look up and navigate between functions, classes etc - there are other packages for that. @end ifinfo @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @menu * Introduction:: * Overview:: * Getting Started:: * Commands:: * Font Locking:: * Config Basics:: * Custom Filling and Breaking:: * Custom Auto-newlines:: * Clean-ups:: * Indentation Engine Basics:: * Customizing Indentation:: * Custom Macros:: * Odds and Ends:: * Sample .emacs File:: * Performance Issues:: * Limitations and Known Bugs:: * FAQ:: * Updating CC Mode:: * Mailing Lists and Bug Reports:: * GNU Free Documentation License:: * Command and Function Index:: * Variable Index:: * Concept and Key Index:: @detailmenu --- The Detailed Node Listing --- Commands * Indentation Commands:: * Comment Commands:: * Movement Commands:: * Filling and Breaking:: * Minor Modes:: * Electric Keys:: * Auto-newlines:: * Hungry WS Deletion:: * Subword Movement:: * Other Commands:: Font Locking * Font Locking Preliminaries:: * Faces:: * Doc Comments:: * AWK Mode Font Locking:: Configuration Basics * CC Hooks:: * Style Variables:: * Styles:: Styles * Built-in Styles:: * Choosing a Style:: * Adding Styles:: * File Styles:: Customizing Auto-newlines * Hanging Braces:: * Hanging Colons:: * Hanging Semicolons and Commas:: Hanging Braces * Custom Braces:: Indentation Engine Basics * Syntactic Analysis:: * Syntactic Symbols:: * Indentation Calculation:: Syntactic Symbols * Function Symbols:: * Class Symbols:: * Conditional Construct Symbols:: * Switch Statement Symbols:: * Brace List Symbols:: * External Scope Symbols:: * Paren List Symbols:: * Literal Symbols:: * Multiline Macro Symbols:: * Objective-C Method Symbols:: * Anonymous Class Symbol:: * Statement Block Symbols:: * K&R Symbols:: Customizing Indentation * c-offsets-alist:: * Interactive Customization:: * Line-Up Functions:: * Custom Line-Up:: * Other Indentation:: Line-Up Functions * Brace/Paren Line-Up:: * List Line-Up:: * Operator Line-Up:: * Comment Line-Up:: * Misc Line-Up:: @end detailmenu @end menu @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node Introduction, Overview, Top, Top @comment node-name, next, previous, up @chapter Introduction @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @cindex BOCM @cindex history @cindex awk-mode.el @cindex c-mode.el @cindex c++-mode.el Welcome to @ccmode{}, a GNU Emacs mode for editing files containing C, C++, Objective-C, Java, CORBA IDL (and the variants CORBA PSDL and CIDL), Pike and AWK code. This incarnation of the mode is descended from @file{c-mode.el} (also called ``Boring Old C Mode'' or BOCM @t{:-)}, @file{c++-mode.el} version 2, which Barry Warsaw had been maintaining since 1992, and @file{awk-mode.el}, a long neglected mode in the (X)Emacs base. Late in 1997, Martin Stjernholm joined Barry on the @ccmode{} Maintainers Team, and implemented the Pike support. In 2000 Martin took over as the sole maintainer. In 2001 Alan Mackenzie joined the team, implementing AWK support in version 5.30. @ccmode{} did not originally contain the font lock support for its languages --- that was added in version 5.30. This manual describes @ccmode{} @comment The following line must appear on its own, so that the version 5.31. @comment Release.py script can update the version number automatically @ccmode{} supports the editing of K&R and ANSI C, C++, Objective-C, Java, CORBA's Interface Definition Language, Pike@footnote{A C-like scripting language with its roots in the LPC language used in some MUD engines. See @uref{http://pike.ida.liu.se/}.} and AWK files. In this way, you can easily set up consistent font locking and coding styles for use in editing all of these languages, although AWK is not yet as uniformly integrated as the other languages. @findex c-mode @findex c++-mode @findex objc-mode @findex java-mode @findex idl-mode @findex pike-mode @findex awk-mode Note that the name of this package is ``@ccmode{}'', but 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}, @code{java-mode}, @code{idl-mode}, @code{pike-mode}, and @code{awk-mode} entry points are provided. This package is intended to be a replacement for @file{c-mode.el}, @file{c++-mode.el} and @file{awk-mode.el}. A special word of thanks goes to Krishna Padmasola for his work in converting the original @file{README} file to Texinfo format. I'd also like to thank all the @ccmode{} victims who help enormously during the early beta stages of @ccmode{}'s development. @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node Overview, Getting Started, Introduction, Top @comment node-name, next, previous, up@cindex organization of the manual @chapter Overview of the Manual @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @noindent The manual starts with several introductory chapters (including this one). @noindent The next chunk of the manual describes the day to day @emph{use} of @ccmode{} (as contrasted with how to customize it). @itemize @bullet @item The chapter ``Commands'' describes in detail how to use (nearly) all of @ccmode{}'s features. There are extensive cross-references from here to the corresponding sections later in the manual which tell you how to customize these features. @item ``Font Locking'' describes how ``syntax highlighting'' is applied to your buffers. It is mainly background information and can be skipped over at a first reading. @end itemize @noindent The next chunk of the manual describes how to @emph{customize} @ccmode{}. Typically, an overview of a topic is given at the chapter level, then the sections and subsections describe the material in increasing detail. @itemize @bullet @item The chapter ``Configuration Basics'' tells you @emph{how} to write customizations - whether in hooks, in styles, in both, or in neither, depending on your needs. It describes the @ccmode{} style system and lists the standard styles that @ccmode{} supplies. @item The next few chapters describe in detail how to customize the various features of @ccmode{}. @item Finally, there is a sample @file{.emacs} fragment, which might help you in creating your own customization. @end itemize @noindent The manual ends with ``this and that'', things that don't fit cleanly into any of the previous chunks. @itemize @bullet @item Two chapters discuss the performance of @ccmode{} and known bugs/limitations. @item The FAQ contains a list of common problems and questions. @item The next two chapters tell you how to get in touch with the @ccmode{} project - whether for updating @ccmode{} or submitting bug reports. @end itemize @noindent Finally, there are the customary indices. @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node Getting Started, Commands, Overview, Top @comment node-name, next, previous, up @chapter Getting Started @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! If you got this version of @ccmode{} with Emacs or XEmacs, it should work just fine right out of the box. Note however that you might not have the latest @ccmode{} release and might want to upgrade your copy (see below). You should probably start by skimming through the entire Commands chapter (@pxref{Commands}) to get an overview of @ccmode{}'s capabilities. After trying out some commands, you may dislike some aspects of @ccmode{}'s default configuration. Here is an outline of how to change some of the settings that newcomers to @ccmode{} most often want to change: @table @asis @item c-basic-offset This Lisp variable holds an integer, the number of columns @ccmode{} indents nested code. To set this value to 6, customize @code{c-basic-offset} or put this into your @file{.emacs}: @example (setq c-basic-offset 6) @end example @item The (indentation) style The basic ``shape'' of indentation created by @ccmode{}---by default, this is @code{gnu} style (except for Java and AWK buffers). A list of the available styles and their descriptions can be found in @ref{Built-in Styles}. A complete specification of the @ccmode{} style system, including how to create your own style, can be found in the chapter @ref{Styles}. To set your style to @code{linux}, either customize @code{c-default-style} or put this into your @file{.emacs}: @example (setq c-default-style '((java-mode . "java") (awk-mode . "awk") (other . "linux"))) @end example @item Electric Indentation Normally, when you type ``punctuation'' characters such as @samp{;} or @samp{@{}, @ccmode{} instantly reindents the current line. This can be disconcerting until you get used to it. To disable @dfn{electric indentation} in the current buffer, type @kbd{C-c C-l}. Type the same thing to enable it again. To have electric indentation disabled by default, put the following into your @file{.emacs} file@footnote{There is no ``easy customization'' facility for making this change.}: @example (setq-default c-electric-flag nil) @end example @noindent Details of this and other similar ``Minor Modes'' appear in the section @ref{Minor Modes}. @item Making the @key{RET} key indent the new line The standard Emacs binding for @key{RET} just adds a new line. If you want it to reindent the new line as well, rebind the key. Note that the action of rebinding would fail if the pertinent keymap didn't yet exist---we thus need to delay the action until after @ccmode{} has been loaded. Put the following code into your @file{.emacs}: @example (defun my-make-CR-do-indent () (define-key c-mode-base-map "\C-m" 'c-context-line-break)) (add-hook 'c-initialization-hook 'my-make-CR-do-indent) @end example @noindent This example demonstrates the use of a very powerful @ccmode{} (and Emacs) facility, the hook. The use of @ccmode{}'s hooks is described in @ref{CC Hooks}. @end table All these settings should occur in your @file{.emacs} @emph{before} any @ccmode{} buffers get loaded---in particular, before any call of @code{desktop-read}. As you get to know the mode better, you may want to make more ambitious changes to your configuration. For this, you should start reading the chapter @ref{Config Basics}. If you are upgrading an existing @ccmode{} installation, please see the @file{README} file for installation details. In particular, if you are going to be editing AWK files, @file{README} describes how to configure your (X)Emacs so that @ccmode{} will supersede the obsolete @code{awk-mode.el} which might have been supplied with your (X)Emacs. @ccmode{} might not work with older versions of Emacs or XEmacs. See the @ccmode{} release notes at @uref{http://cc-mode.sourceforge.net} for the latest information on Emacs version and package compatibility (@pxref{Updating CC Mode}). @deffn Command c-version @findex version (c-) You can find out what version of @ccmode{} you are using by visiting a C file and entering @kbd{M-x c-version RET}. You should see this message in the echo area: @example Using CC Mode version 5.XX @end example @noindent where @samp{XX} is the minor release number. @end deffn @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node Commands, Font Locking, Getting Started, Top @comment node-name, next, previous, up @chapter Commands @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! This chapter specifies all of CC Mode's commands, and thus contains nearly everything you need to know to @emph{use} @ccmode{} (as contrasted with configuring it). @dfn{Commands} here means both control key sequences and @dfn{electric keys}, these being characters such as @samp{;} which, as well as inserting themselves into the buffer, also do other things. You might well want to review @ifset XEMACS @ref{Lists,,,@emacsman{}, @emacsmantitle{}}, @end ifset @ifclear XEMACS @ref{Moving by Parens,,,@emacsman{}, @emacsmantitle{}}, @end ifclear which describes commands for moving around brace and parenthesis structures. @menu * Indentation Commands:: * Comment Commands:: * Movement Commands:: * Filling and Breaking:: * Minor Modes:: * Electric Keys:: * Auto-newlines:: * Hungry WS Deletion:: * Subword Movement:: * Other Commands:: @end menu @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node Indentation Commands, Comment Commands, Commands, Commands @comment node-name, next, previous,up @section Indentation Commands @cindex indentation @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! The following commands reindent C constructs. Note that when you change your coding style, either interactively or through some other means, your file does @emph{not} automatically get reindented. You will need to execute one of the following commands to see the effects of your changes. @cindex GNU indent program Also, variables like @code{c-hanging-*} and @code{c-cleanup-list} (@pxref{Custom Auto-newlines}) only affect how on-the-fly code is formatted. Changing the ``hanginess'' of a brace and then reindenting, will not move the brace to a different line. For this, you're better off getting an external program like GNU @code{indent}, which will rearrange brace location, amongst other things. Preprocessor directives are handled as syntactic whitespace from other code, i.e. they can be interspersed anywhere without affecting the indentation of the surrounding code, just like comments. The code inside macro definitions is, by default, still analyzed syntactically so that you get relative indentation there just as you'd get if the same code was outside a macro. However, since there is no hint about the syntactic context, i.e. whether the macro expands to an expression, to some statements, or perhaps to whole functions, the syntactic recognition can be wrong. @ccmode{} manages to figure it out correctly most of the time, though. Reindenting large sections of code can take a long time. When @ccmode{} reindents a region of code, it is essentially equivalent to hitting @key{TAB} on every line of the region. These commands indent code: @table @asis @item @kbd{@key{TAB}} (@code{c-indent-command}) @kindex TAB @findex c-indent-command @findex indent-command (c-) This command indents the current line. That is all you need to know about it for normal use. @code{c-indent-command} does different things, depending on the setting of @code{c-syntactic-indentation} (@pxref{Indentation Engine Basics}): @itemize @bullet @item When it's non-@code{nil} (which it normally is), the command indents the line according to its syntactic context. With a prefix argument (@kbd{C-u @key{TAB}}), it will re-indent the entire expression@footnote{this is only useful for a line starting with a comment opener or an opening brace, parenthesis, or string quote.} that begins at the line's left margin. @item When it's @code{nil}, the command indents the line by an extra @code{c-basic-offset} columns. A prefix argument acts as a multiplier. A bare prefix (@kbd{C-u @key{TAB}}) is equivalent to -1, removing @code{c-basic-offset} columns from the indentation. @end itemize The precise behavior is modified by several variables: With @code{c-tab-always-indent}, you can make @key{TAB} insert whitespace in some circumstances---@code{c-insert-tab-function} then defines precisely what sort of ``whitespace'' this will be. Set the standard Emacs variable @code{indent-tabs-mode} to @code{t} if you want real @samp{tab} characters to be used in the indentation, to @code{nil} if you want only spaces. @xref{Just Spaces,,, @emacsman{}, @emacsmantitle{}}. @defopt c-tab-always-indent @vindex tab-always-indent (c-) @cindex literal This variable modifies how @key{TAB} operates. @itemize @bullet @item When it is @code{t} (the default), @key{TAB} simply indents the current line. @item When it is @code{nil}, @key{TAB} (re)indents the line only if point is to the left of the first non-whitespace character on the line. Otherwise it inserts some whitespace (a tab or an equivalent number of spaces - see below) at point. @item With some other value, the line is reindented. Additionally, if point is within a string or comment, some whitespace is inserted. @end itemize @end defopt @defopt c-insert-tab-function @vindex insert-tab-function (c-) @findex tab-to-tab-stop When ``some whitespace'' is inserted as described above, what actually happens is that the function stored in @code{c-insert-tab-function} is called. Normally, this is @code{insert-tab}, which inserts a real tab character or the equivalent number of spaces (depending on @code{indent-tabs-mode}). Some people, however, set @code{c-insert-tab-function} to @code{tab-to-tab-stop} so as to get hard tab stops when indenting. @end defopt @end table @noindent The kind of indentation the next five commands do depends on the setting of @code{c-syntactic-indentation} (@pxref{Indentation Engine Basics}): @itemize @bullet @item when it is non-@code{nil} (the default), the commands indent lines according to their syntactic context; @item when it is @code{nil}, they just indent each line the same amount as the previous non-blank line. The commands that indent a region aren't very useful in this case. @end itemize @table @asis @item @kbd{C-j} (@code{newline-and-indent}) @kindex C-j @findex newline-and-indent Inserts a newline and indents the new blank line, ready to start typing. This is a standard (X)Emacs command. @item @kbd{C-M-q} (@code{c-indent-exp}) @kindex C-M-q @findex c-indent-exp @findex indent-exp (c-) Indents an entire balanced brace or parenthesis expression. Note that point must be on the opening brace or parenthesis of the expression you want to indent. @item @kbd{C-c C-q} (@code{c-indent-defun}) @kindex C-c C-q @findex c-indent-defun @findex indent-defun (c-) Indents the entire top-level function, class or macro definition encompassing point. It leaves point unchanged. This function can't be used to reindent a nested brace construct, such as a nested class or function, or a Java method. The top-level construct being reindented must be complete, i.e. it must have both a beginning brace and an ending brace. @item @kbd{C-M-\} (@code{indent-region}) @kindex C-M-\ @findex indent-region Indents an arbitrary region of code. This is a standard Emacs command, tailored for C code in a @ccmode{} buffer. Note, of course, that point and mark must delineate the region you want to indent. @item @kbd{C-M-h} (@code{c-mark-function}) @kindex C-M-h @findex c-mark-function @findex mark-function (c-) While not strictly an indentation command, this is useful for marking the current top-level function or class definition as the current region. As with @code{c-indent-defun}, this command operates on top-level constructs, and can't be used to mark say, a Java method. @end table These variables are also useful when indenting code: @defopt indent-tabs-mode This is a standard Emacs variable that controls how line indentation is composed. When it's non-@code{nil}, tabs can be used in a line's indentation, otherwise only spaces are used. @end defopt @defopt c-progress-interval @vindex progress-interval (c-) When indenting large regions of code, this variable controls how often a progress message is displayed. Set this variable to @code{nil} to inhibit the progress messages, or set it to an integer which is how often (in seconds) progress messages are to be displayed. @end defopt @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node Comment Commands, Movement Commands, Indentation Commands, Commands @comment node-name, next, previous, up @section Comment Commands @cindex comments (insertion of) @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @table @asis @item @kbd{C-c C-c} (@code{comment-region}) @kindex C-c C-c @findex comment-region This command comments out the lines that start in the region. With a negative argument, it does the opposite - it deletes the comment delimiters from these lines. @xref{Multi-Line Comments,,, emacs, GNU Emacs Manual}, for fuller details. @code{comment-region} isn't actually part of @ccmode{} - it is given a @ccmode{} binding for convenience. @item @kbd{M-;} (@code{comment-dwim} or @code{indent-for-comment} @footnote{The name of this command varies between (X)Emacs versions.}) @kindex M-; @findex comment-dwim @findex indent-for-comment Insert a comment at the end of the current line, if none is there already. Then reindent the comment according to @code{comment-column} @ifclear XEMACS (@pxref{Options for Comments,,, emacs, GNU Emacs Manual}) @end ifclear @ifset XEMACS (@pxref{Comments,,, xemacs, XEmacs User's Manual}) @end ifset and the variables below. Finally, position the point after the comment starter. @kbd{C-u M-;} kills any comment on the current line, together with any whitespace before it. This is a standard Emacs command, but @ccmode{} enhances it a bit with two variables: @defopt c-indent-comment-alist @vindex indent-comment-alist (c-) @vindex comment-column This style variable allows you to vary the column that @kbd{M-;} puts the comment at, depending on what sort of code is on the line, and possibly the indentation of any similar comment on the preceding line. It is an association list that maps different types of lines to actions describing how they should be handled. If a certain line type isn't present on the list then the line is indented to the column specified by @code{comment-column}. See the documentation string for a full description of this variable (use @kbd{C-h v c-indent-comment-alist}). @end defopt @defopt c-indent-comments-syntactically-p @vindex indent-comments-syntactically-p (c-) Normally, when this style variable is @code{nil}, @kbd{M-;} will indent comment-only lines according to @code{c-indent-comment-alist}, just as it does with lines where other code precede the comments. However, if you want it to act just like @key{TAB} for comment-only lines you can get that by setting @code{c-indent-comments-syntactically-p} to non-@code{nil}. If @code{c-indent-comments-syntactically-p} is non-@code{nil} then @code{c-indent-comment-alist} won't be consulted at all for comment-only lines. @end defopt @end table @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node Movement Commands, Filling and Breaking, Comment Commands, Commands @comment node-name, next, previous, up @section Movement Commands @cindex movement @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @ccmode{} contains some useful commands for moving around in C code. @table @asis @item @kbd{C-M-a} (@code{c-beginning-of-defun}) @itemx @kbd{C-M-e} (@code{c-end-of-defun}) @findex c-beginning-of-defun @findex c-end-of-defun Move to the beginning or end of the current or next function. Other constructs (such as a structs or classes) which have a brace block also count as ``functions'' here. To move over several functions, you can give these commands a repeat count. The start of a function is at its header. The end of the function is after its closing brace, or after the semicolon of a construct (such as a @code{struct}) which doesn't end at the brace. These two commands try to leave point at the beginning of a line near the actual start or end of the function. This occasionally causes point not to move at all. These functions are analogous to the Emacs built-in commands @code{beginning-of-defun} and @code{end-of-defun}, except they eliminate the constraint that the top-level opening brace of the defun must be in column zero. See @ref{Defuns,,,@emacsman{}, @emacsmantitle{}}, for more information. @item @kbd{C-M-a} (AWK Mode) (@code{c-awk-beginning-of-defun}) @itemx @kbd{C-M-e} (AWK Mode) (@code{c-awk-end-of-defun}) @kindex C-M-a (AWK Mode) @kindex C-M-e (AWK Mode) @findex c-awk-beginning-of-defun @findex awk-beginning-of-defun (c-) @findex c-awk-end-of-defun @findex awk-end-of-defun (c-) Move to the beginning or end of the current or next AWK defun. These commands can take prefix-arguments, their functionality being entirely equivalent to @code{beginning-of-defun} and @code{end-of-defun}. AWK Mode @dfn{defuns} are either pattern/action pairs (either of which might be implicit) or user defined functions. Having the @samp{@{} and @samp{@}} (if there are any) in column zero, as is suggested for some modes, is neither necessary nor helpful in AWK mode. @item @kbd{M-a} (@code{c-beginning-of-statement}) @itemx @kbd{M-e} (@code{c-end-of-statement}) @kindex M-a @kindex M-e @findex c-beginning-of-statement @findex c-end-of-statement @findex beginning-of-statement (c-) @findex end-of-statement (c-) Move to the beginning or end of the innermost C statement. If point is already there, move to the next beginning or end of a statement, even if that means moving into a block. (Use @kbd{C-M-b} or @kbd{C-M-f} to move over a balanced block.) A prefix argument @var{n} means move over @var{n} statements. If point is within or next to a comment or a string which spans more than one line, these commands move by sentences instead of statements. When called from a program, these functions take three optional arguments: the repetition count, a buffer position limit which is the farthest back to search for the syntactic context, and a flag saying whether to do sentence motion in or near comments and multiline strings. @item @kbd{C-c C-u} (@code{c-up-conditional}) @kindex C-c C-u @findex c-up-conditional @findex up-conditional (c-) Move back to the containing preprocessor conditional, leaving the mark behind. A prefix argument acts as a repeat count. With a negative argument, move forward to the end of the containing preprocessor conditional. @samp{#elif} is treated like @samp{#else} followed by @samp{#if}, so the function stops at them when going backward, but not when going forward. This key sequence is not bound in AWK Mode, which doesn't have preprocessor statements. @item @kbd{M-x c-up-conditional-with-else} @findex c-up-conditional-with-else @findex up-conditional-with-else (c-) A variety of @code{c-up-conditional} that also stops at @samp{#else} lines. Normally those lines are ignored. @item @kbd{M-x c-down-conditional} @findex c-down-conditional @findex down-conditional (c-) Move forward into the next nested preprocessor conditional, leaving the mark behind. A prefix argument acts as a repeat count. With a negative argument, move backward into the previous nested preprocessor conditional. @samp{#elif} is treated like @samp{#else} followed by @samp{#if}, so the function stops at them when going forward, but not when going backward. @item @kbd{M-x c-down-conditional-with-else} @findex c-down-conditional-with-else @findex down-conditional-with-else (c-) A variety of @code{c-down-conditional} that also stops at @samp{#else} lines. Normally those lines are ignored. @item @kbd{C-c C-p} (@code{c-backward-conditional}) @itemx @kbd{C-c C-n} (@code{c-forward-conditional}) @kindex C-c C-p @kindex C-c C-n @findex c-backward-conditional @findex c-forward-conditional @findex backward-conditional (c-) @findex forward-conditional (c-) Move backward or forward across a preprocessor conditional, leaving the mark behind. A prefix argument acts as a repeat count. With a negative argument, move in the opposite direction. These key sequences are not bound in AWK Mode, which doesn't have preprocessor statements. @item @kbd{M-x c-backward-into-nomenclature} @itemx @kbd{M-x c-forward-into-nomenclature} @findex c-backward-into-nomenclature @findex c-forward-into-nomenclature @findex backward-into-nomenclature (c-) @findex forward-into-nomenclature (c-) A popular programming style, especially for object-oriented languages such as C++ is to write symbols in a mixed case format, where the first letter of each word is capitalized, and not separated by underscores. E.g. @samp{SymbolsWithMixedCaseAndNoUnderlines}. These commands move backward or forward to the beginning of the next capitalized word. With prefix argument @var{n}, move @var{n} times. If @var{n} is negative, move in the opposite direction. Note that these two commands have been superseded by @code{c-subword-mode}, which you should use instead. @xref{Subword Movement}. They might be removed from a future release of @ccmode{}. @end table @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node Filling and Breaking, Minor Modes, Movement Commands, Commands @comment node-name, next, previous, up @section Filling and Line Breaking Commands @cindex text filling @cindex line breaking @cindex comment handling @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Since there's a lot of normal text in comments and string literals, @ccmode{} provides features to edit these like in text mode. The goal is to do it seamlessly, i.e. you can use auto fill mode, sentence and paragraph movement, paragraph filling, adaptive filling etc. wherever there's a piece of normal text without having to think much about it. @ccmode{} keeps the indentation, fixes suitable comment line prefixes, and so on. You can configure the exact way comments get filled and broken, and where Emacs does auto-filling (see @pxref{Custom Filling and Breaking}). Typically, the style system (@pxref{Styles}) will have set this up for you, so you probably won't have to bother. @findex auto-fill-mode @cindex Auto Fill mode @cindex paragraph filling Line breaks are by default handled (almost) the same regardless of whether they are made by auto fill mode (@pxref{Auto Fill,,, @emacsman{}, @emacsmantitle{}}), by paragraph filling (e.g. with @kbd{M-q}), or explicitly with @kbd{M-j} or similar methods. In string literals, the new line gets the same indentation as the previous nonempty line.@footnote{You can change this default by setting the @code{string} syntactic symbol (@pxref{Syntactic Symbols} and @pxref{Customizing Indentation})}. @table @asis @item @kbd{M-q} (@code{c-fill-paragraph}) @kindex M-q @findex c-fill-paragraph @findex fill-paragraph (c-) @cindex Javadoc markup @cindex Pike autodoc markup This command fills multiline string literals and both block and line style comments. In Java buffers, the Javadoc markup words are recognized as paragraph starters. The line oriented Pike autodoc markup words are recognized in the same way in Pike mode. The formatting of the starters (@code{/*}) and enders (@code{*/}) of block comments are kept as they were before the filling. I.e., if either the starter or ender were on a line of its own, then it stays on its own line; conversely, if the delimiter has comment text on its line, it keeps at least one word of that text with it on the line. This command is the replacement for @code{fill-paragraph} in @ccmode{} buffers. @item @kbd{M-j} (@code{c-indent-new-comment-line}) @kindex M-j @findex c-indent-new-comment-line @findex indent-new-comment-line (c-) This breaks the current line at point and indents the new line. If point was in a comment, the new line gets the proper comment line prefix. If point was inside a macro, a backslash is inserted before the line break. It is the replacement for @code{indent-new-comment-line}. @item @kbd{M-x c-context-line-break} @findex c-context-line-break @findex context-line-break (c-) Insert a line break suitable to the context: If the point is inside a comment, the new line gets the suitable indentation and comment line prefix like @code{c-indent-new-comment-line}. In normal code it's indented like @code{newline-and-indent} would do. In macros it acts like @code{newline-and-indent} but additionally inserts and optionally aligns the line ending backslash so that the macro remains unbroken. @xref{Custom Macros}, for details about the backslash alignment. In a string, a backslash is inserted only if the string is within a macro@footnote{In GCC, unescaped line breaks within strings are valid.}. This function is not bound to a key by default, but it's intended to be used on the @kbd{RET} key. If you like the behavior of @code{newline-and-indent} on @kbd{RET}, you should consider switching to this function. @xref{Sample .emacs File}. @item @kbd{M-x c-context-open-line} @findex c-context-open-line @findex context-open-line (c-) This is to @kbd{C-o} (@kbd{M-x open-line}) as @code{c-context-line-break} is to @kbd{RET}. I.e. it works just like @code{c-context-line-break} but leaves the point before the inserted line break. @end table @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node Minor Modes, Electric Keys, Filling and Breaking, Commands @comment node-name, next, previous, up @section Minor Modes @cindex Minor Modes @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @ccmode{} contains several minor-mode-like features that you might find useful while writing new code or editing old code: @table @asis @item electric mode When this is enabled, certain visible characters cause reformatting as they are typed. This is normally helpful, but can be a nuisance when editing chaotically formatted code. It can also be disconcerting, especially for users who are new to @ccmode{}. @item auto-newline mode This automatically inserts newlines where you'd probably want to type them yourself, e.g. after typing @samp{@}}s. Its action is suppressed when electric mode is disabled. @item hungry-delete mode This lets you delete a contiguous block of whitespace with a single key - for example, the newline and indentation just inserted by auto-newline when you want to back up and write a comment after the last statement. @item subword mode This mode makes basic word movement commands like @kbd{M-f} (@code{forward-word}) and @kbd{M-b} (@code{backward-word}) treat the parts of sillycapsed symbols as different words. E.g. @samp{NSGraphicsContext} is treated as three words @samp{NS}, @samp{Graphics}, and @samp{Context}. @item syntactic-indentation mode When this is enabled (which it normally is), indentation commands such as @kbd{C-j} indent lines of code according to their syntactic structure. Otherwise, a line is simply indented to the same level as the previous one and @kbd{@key{TAB}} adjusts the indentation in steps of `c-basic-offset'. @end table Full details on how these minor modes work are at @ref{Electric Keys}, @ref{Auto-newlines}, @ref{Hungry WS Deletion}, @ref{Subword Movement}, and @ref{Indentation Engine Basics}. You can toggle each of these minor modes on and off, and you can configure @ccmode{} so that it starts up with your favourite combination of them (@pxref{Sample .emacs File}). By default, when you initialize a buffer, electric mode and syntactic-indentation mode are enabled but the other two modes are disabled. @ccmode{} displays the current state of the first four of these minor modes on the modeline by appending letters to the major mode's name, one letter for each enabled minor mode - @samp{l} for electric mode, @samp{a} for auto-newline mode, @samp{h} for hungry delete mode, and @samp{w} for subword mode. If all these modes were enabled, you'd see @samp{C/lahw}@footnote{The @samp{C} would be replaced with the name of the language in question for the other languages @ccmode{} supports.}. Here are the commands to toggle these modes: @table @asis @item @kbd{C-c C-l} (@code{c-toggle-electric-state}) @kindex C-c C-l @findex c-toggle-electric-state @findex toggle-electric-state (c-) Toggle electric minor mode. When the command turns the mode off, it also suppresses auto-newline mode. @item @kbd{C-c C-a} (@code{c-toggle-auto-newline}) @kindex C-c C-a @findex c-toggle-auto-newline @findex toggle-auto-newline (c-) Toggle auto-newline minor mode. When the command turns the mode on, it also enables electric minor mode. @item @kbd{M-x c-toggle-hungry-state}@footnote{Prior to @ccmode{} 5.31, this command was bound to @kbd{C-c C-d}.} @findex c-toggle-hungry-state @findex toggle-hungry-state (c-) Toggle hungry-delete minor mode. @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}.} @findex c-toggle-auto-hungry-state @findex toggle-auto-hungry-state (c-) Toggle both auto-newline and hungry delete minor modes. @item @kbd{C-c C-w} (@code{M-x c-subword-mode}) @kindex C-c C-w @findex c-subword-mode @findex subword-mode (c-) Toggle subword mode. @item @kbd{M-x c-toggle-syntactic-indentation} @findex c-toggle-syntactic-indentation @findex toggle-syntactic-indentation (c-) Toggle syntactic-indentation mode. @end table Common to all the toggle functions above is that if they are called programmatically, they take an optional numerical argument. A positive value will turn on the minor mode (or both of them in the case of @code{c-toggle-auto-hungry-state}) and a negative value will turn it (or them) off. @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node Electric Keys, Auto-newlines, Minor Modes, Commands @comment node-name, next, previous, up @section Electric Keys and Keywords @cindex electric characters @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Most punctuation keys provide @dfn{electric} behavior - as well as inserting themselves they perform some other action, such as reindenting the line. This reindentation saves you from having to reindent a line manually after typing, say, a @samp{@}}. A few keywords, such as @code{else}, also trigger electric action. You can inhibit the electric behavior described here by disabling electric minor mode (@pxref{Minor Modes}). Common to all these keys is that they only behave electrically when used in normal code (as contrasted with getting typed in a string literal or comment). Those which cause re-indentation do so only when @code{c-syntactic-indentation} has a non-@code{nil} value (which it does by default). These keys and keywords are: @c ACM, 2004/8/24: c-electric-pound doesn't check c-s-i: this is more @c like a bug in the code than a bug in this document. It'll get @c fixed in the code sometime. @table @kbd @item # @kindex # @findex c-electric-pound @findex electric-pound (c-) @vindex c-electric-pound-behavior @vindex electric-pound-behavior (c-) Pound (bound to @code{c-electric-pound}) is electric when typed as the first non-whitespace character on a line and not within a macro definition. In this case, the variable @code{c-electric-pound-behavior} is consulted for the electric behavior. This variable takes a list value, although the only element currently defined is @code{alignleft}, which tells this command to force the @samp{#} character into column zero. This is useful for entering preprocessor macro definitions. Pound is not electric in AWK buffers, where @samp{#} starts a comment, and is bound to @code{self-insert-command} like any typical printable character. @c ACM, 2004/8/24: Change this (and the code) to do AWK comment @c reindentation. @item * @kindex * @itemx / @kindex / @findex c-electric-star @findex electric-star (c-) @findex c-electric-slash @findex electric-slash (c-) A star (bound to @code{c-electric-star}) or a slash (@code{c-electric-slash}) causes reindentation when you type it as the second component of a C style block comment opener (@samp{/*}) or a C++ line comment opener (@samp{//}) respectively, but only if the comment opener is the first thing on the line (i.e. there's only whitespace before it). Additionally, you can configure @ccmode{} so that typing a slash at the start of a line within a block comment will terminate the comment. You don't need to have electric minor mode enabled to get this behavior. @xref{Clean-ups}. In AWK mode, @samp{*} and @samp{/} do not delimit comments and are not electric. @item < @kindex < @itemx > @kindex > @findex c-electric-lt-gt @findex electric-lt-gt (c-) A less-than or greater-than sign (bound to @code{c-electric-lt-gt}) is electric in two circumstances: when it is an angle bracket in a C++ @samp{template} declaration (and similar constructs in other languages) and when it is the second of two @kbd{<} or @kbd{>} characters in a C++ style stream operator. In either case, the line is reindented. Angle brackets in C @samp{#include} directives are not electric. @item ( @kindex ( @itemx ) @kindex ) @findex c-electric-paren @findex electric-paren (c-) The normal parenthesis characters @samp{(} and @samp{)} (bound to @code{c-electric-paren}) reindent the current line. This is useful for getting the closing parenthesis of an argument list aligned automatically. You can also configure @ccmode{} to insert a space automatically between a function name and the @samp{(} you've just typed, and to remove it automatically after typing @samp{)}, should the argument list be empty. You don't need to have electric minor mode enabled to get these actions. @xref{Clean-ups}. @item @{ @kindex @{ @itemx @} @kindex @} @findex c-electric-brace @findex electric-brace (c-) Typing a brace (bound to @code{c-electric-brace}) reindents the current line. Also, one or more newlines might be inserted if auto-newline minor mode is enabled. @xref{Auto-newlines}. Additionally, you can configure @ccmode{} to compact excess whitespace inserted by auto-newline mode in certain circumstances. @xref{Clean-ups}. @item : @kindex : @findex c-electric-colon @findex electric-colon (c-) Typing a colon (bound to @code{c-electric-colon}) reindents the current line. Additionally, one or more newlines might be inserted if auto-newline minor mode is enabled. @xref{Auto-newlines}. If you type a second colon immediately after such an auto-newline, by default the whitespace between the two colons is removed, leaving a C++ scope operator. @xref{Clean-ups}. If you prefer, you can insert @samp{::} in a single operation, avoiding all these spurious reindentations, newlines, and clean-ups. @xref{Other Commands}. @item ; @kindex ; @itemx , @kindex , @findex c-electric-semi&comma @findex electric-semi&comma (c-) Typing a semicolon or comma (bound to @code{c-electric-semi&comma}) reindents the current line. Also, a newline might be inserted if auto-newline minor mode is enabled. @xref{Auto-newlines}. Additionally, you can configure @ccmode{} so that when auto-newline has inserted whitespace after a @samp{@}}, it will be removed again when you type a semicolon or comma just after it. @xref{Clean-ups}. @end table @deffn Command c-electric-continued-statement @findex electric-continued-statement (c-) Certain keywords are electric, causing reindentation when they are preceded only by whitespace on the line. The keywords are those that continue an earlier statement instead of starting a new one: @code{else}, @code{while}, @code{catch} (only in C++ and Java) and @code{finally} (only in Java). An example: @example @group for (i = 0; i < 17; i++) if (a[i]) res += a[i]->offset; else @end group @end example Here, the @code{else} should be indented like the preceding @code{if}, since it continues that statement. @ccmode{} will automatically reindent it after the @code{else} has been typed in full, since only then is it possible to decide whether it's a new statement or a continuation of the preceding @code{if}. @vindex abbrev-mode @findex abbrev-mode @cindex Abbrev mode @ccmode{} uses Abbrev mode (@pxref{Abbrevs,,, @emacsman{}, @emacsmantitle{}}) to accomplish this. It's therefore turned on by default in all language modes except IDL mode, since CORBA IDL doesn't have any statements. @end deffn @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node Auto-newlines, Hungry WS Deletion, Electric Keys, Commands @comment node-name, next, previous, up @section Auto-newline Insertion @cindex auto-newline @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! When you have @dfn{Auto-newline minor mode} enabled (@pxref{Minor Modes}), @ccmode{} inserts newlines for you automatically (in certain syntactic contexts) when you type a left or right brace, a colon, a semicolon, or a comma. Sometimes a newline appears before the character you type, sometimes after it, sometimes both. Auto-newline only triggers when the following conditions hold: @itemize @bullet @item Auto-newline minor mode is enabled, as evidenced by the indicator @samp{a} after the mode name on the modeline (e.g. @samp{C/a} or @samp{C/la}). @item The character was typed at the end of a line, or with only whitespace after it, and possibly a @samp{\} escaping the newline. @item The character is not on its own line already. (This applies only to insertion of a newline @emph{before} the character.) @item @cindex literal @cindex syntactic whitespace The character was not typed inside of a literal @footnote{A @dfn{literal} is defined as any comment, string, or preprocessor macro definition. These constructs are also known as @dfn{syntactic whitespace} since they are usually ignored when scanning C code.}. @item No numeric argument was supplied to the command (i.e. it was typed as normal, with no @kbd{C-u} prefix). @end itemize You can configure the precise circumstances in which newlines get inserted (see @pxref{Custom Auto-newlines}). Typically, the style system (@pxref{Styles}) will have set this up for you, so you probably won't have to bother. Sometimes @ccmode{} inserts an auto-newline where you don't want one, such as after a @samp{@}} when you're about to type a @samp{;}. Hungry deletion can help here (@pxref{Hungry WS Deletion}), or you can activate an appropriate @dfn{clean-up}, which will remove the excess whitespace after you've typed the @samp{;}. See @ref{Clean-ups} for a full description. See also @ref{Electric Keys} for a summary of clean-ups listed by key. @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node Hungry WS Deletion, Subword Movement, Auto-newlines, Commands @comment node-name, next, previous, up @section Hungry Deletion of Whitespace @cindex hungry-deletion @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! If you want to delete an entire block of whitespace at point, you can use @dfn{hungry deletion}. This deletes all the contiguous whitespace either before point or after point in a single operation. ``Whitespace'' here includes tabs and newlines, but not comments or preprocessor commands. Hungry deletion can markedly cut down on the number of times you have to hit deletion keys when, for example, you've made a mistake on the preceding line and have already pressed @kbd{C-j}. Hungry deletion is a simple feature that some people find extremely useful. In fact, you might find yourself wanting it in @strong{all} your editing modes! Loosely speaking, in what follows, @dfn{@key{DEL}} means ``the backspace key'' and @dfn{@key{DELETE}} means ``the forward delete key''. This is discussed in more detail below. There are two different ways you can use hungry deletion: @table @asis @item Using @dfn{Hungry Delete Mode} with @kbd{@key{DEL}} and @kbd{C-d} Here you toggle Hungry Delete minor mode with @kbd{M-x c-toggle-hungry-state}@footnote{Prior to @ccmode{} 5.31, this command was bound to @kbd{C-c C-d}. @kbd{C-c C-d} is now the default binding for @code{c-hungry-delete-forward}.} (@pxref{Minor Modes}.) This makes @kbd{@key{DEL}} and @kbd{C-d} do backwards and forward hungry deletion. @table @asis @item @kbd{@key{DEL}} (@code{c-electric-backspace}) @kindex DEL @findex c-electric-backspace @findex electric-backspace (c-) This command is run by default when you hit the @kbd{DEL} key. When hungry delete mode is enabled, it deletes any amount of whitespace in the backwards direction. Otherwise, or when used with a prefix argument or in a literal (@pxref{Auto-newlines}), the command just deletes backwards in the usual way. (More precisely, it calls the function contained in the variable @code{c-backspace-function}, passing it the prefix argument, if any.) @item @code{c-backspace-function} @vindex c-backspace-function @vindex backspace-function (c-) @findex backward-delete-char-untabify Hook that gets called by @code{c-electric-backspace} when it doesn't do an ``electric'' deletion of the preceding whitespace. The default value is @code{backward-delete-char-untabify} (@pxref{Deletion,,,@lispref{}, @lispreftitle{}}, the function which deletes a single character. @item @kbd{C-d} (@code{c-electric-delete-forward}) @kindex C-d @findex c-electric-delete-forward @findex electric-delete-forward (c-) This function, which is bound to @kbd{C-d} by default, works just like @code{c-electric-backspace} but in the forward direction. When it doesn't do an ``electric'' deletion of the following whitespace, it just does @code{delete-char}, more or less. (Strictly speaking, it calls the function in @code{c-delete-function} with the prefix argument.) @item @code{c-delete-function} @vindex c-delete-function @vindex delete-function (c-) @findex delete-char Hook that gets called by @code{c-electric-delete-forward} when it doesn't do an ``electric'' deletion of the following whitespace. The default value is @code{delete-char}. @end table @item Using Distinct Bindings The other (newer and recommended) way to use hungry deletion is to perform @code{c-hungry-delete-backwards} and @code{c-hungry-delete-forward} directly through their key sequences rather than using the minor mode toggling. @table @asis @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}.} @kindex C-c C- @kindex C-c @kindex C-c C-DEL @kindex C-c DEL @findex c-hungry-delete-backwards @findex hungry-delete-backwards (c-) Delete any amount of whitespace in the backwards direction (regardless whether hungry-delete mode is enabled or not). This command is bound to both @kbd{C-c C-@key{DEL}} and @kbd{C-c @key{DEL}}, since the more natural one, @kbd{C-c C-@key{DEL}}, is sometimes difficult to type at a character terminal. @item @kbd{C-c C-d}, @kbd{C-c C-@key{DELETE}}, or @kbd{C-c @key{DELETE}} (@code{c-hungry-delete-forward}) @kindex C-c C-d @kindex C-c C- @kindex C-c @findex c-hungry-delete-forward @findex hungry-delete-forward (c-) Delete any amount of whitespace in the forward direction (regardless whether hungry-delete mode is enabled or not). This command is bound to both @kbd{C-c C-@key{DELETE}} and @kbd{C-c @key{DELETE}} for the same reason as for @key{DEL} above. @end table @end table @kindex @kindex When we talk about @kbd{@key{DEL}}, and @kbd{@key{DELETE}} above, we actually do so without connecting them to the physical keys commonly known as @key{Backspace} and @key{Delete}. The default bindings to those two keys depends on the flavor of (X)Emacs you are using. @findex c-electric-delete @findex electric-delete (c-) @findex c-hungry-delete @findex hungry-delete (c-) @vindex delete-key-deletes-forward In XEmacs 20.3 and beyond, the @key{Backspace} key is bound to @code{c-electric-backspace} and the @key{Delete} key is bound to @code{c-electric-delete}. You control the direction it deletes in by setting the variable @code{delete-key-deletes-forward}, a standard XEmacs variable. @c This variable is encapsulated by XEmacs's (defsubst delete-forward-p ...). When this variable is non-@code{nil}, @code{c-electric-delete} will do forward deletion with @code{c-electric-delete-forward}, otherwise it does backward deletion with @code{c-electric-backspace}. Similarly, @kbd{C-c @key{Delete}} and @kbd{C-c C-@key{Delete}} are bound to @code{c-hungry-delete} which is controlled in the same way by @code{delete-key-deletes-forward}. @findex normal-erase-is-backspace-mode Emacs 21 and later automatically binds @key{Backspace} and @key{Delete} to @kbd{DEL} and @kbd{C-d} according to your environment, and @ccmode{} extends those bindings to @kbd{C-c C-@key{Backspace}} etc. If you need to change the bindings through @code{normal-erase-is-backspace-mode} then @ccmode{} will also adapt its extended bindings accordingly. In earlier (X)Emacs versions, @ccmode{} doesn't bind either @key{Backspace} or @key{Delete} directly. Only the key codes @kbd{DEL} and @kbd{C-d} are bound, and it's up to the default bindings to map the physical keys to them. You might need to modify this yourself if the defaults are unsuitable. Getting your @key{Backspace} and @key{Delete} keys properly set up can sometimes be tricky. The information in @ref{DEL Does Not Delete,,,emacs, GNU Emacs Manual}, might be helpful if you're having trouble with this in GNU Emacs. @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node Subword Movement, Other Commands, Hungry WS Deletion, Commands @comment node-name, next, previous, up @section Subword Movement and Editing @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @cindex nomenclature @cindex subword In spite of the GNU Coding Standards, it is popular to name a symbol by mixing uppercase and lowercase letters, e.g. @samp{GtkWidget}, @samp{EmacsFrameClass}, or @samp{NSGraphicsContext}. Here we call these mixed case symbols @dfn{nomenclatures}. Also, each capitalized (or completely uppercase) part of a nomenclature is called a @dfn{subword}. Here are some examples: @multitable {@samp{NSGraphicsContext}} {@samp{NS}, @samp{Graphics}, and @samp{Context}} @c This could be converted to @headitem when we require Texinfo 4.7 @iftex @item @b{Nomenclature} @tab @b{Subwords} @end iftex @ifnottex @item Nomenclature @tab Subwords @item --------------------------------------------------------- @end ifnottex @item @samp{GtkWindow} @tab @samp{Gtk} and @samp{Window} @item @samp{EmacsFrameClass} @tab @samp{Emacs}, @samp{Frame}, and @samp{Class} @item @samp{NSGraphicsContext} @tab @samp{NS}, @samp{Graphics}, and @samp{Context} @end multitable The subword minor mode replaces the basic word oriented movement and editing commands with variants that recognize subwords in a nomenclature and treat them as separate words: @findex c-forward-subword @findex forward-subword (c-) @findex c-backward-subword @findex backward-subword (c-) @findex c-mark-subword @findex mark-subword (c-) @findex c-kill-subword @findex kill-subword (c-) @findex c-backward-kill-subword @findex backward-kill-subword (c-) @findex c-transpose-subwords @findex transpose-subwords (c-) @findex c-capitalize-subword @findex capitalize-subword (c-) @findex c-upcase-subword @findex upcase-subword (c-) @findex c-downcase-subword @findex downcase-subword (c-) @multitable @columnfractions .20 .40 .40 @c This could be converted to @headitem when we require Texinfo 4.7 @iftex @item @b{Key} @tab @b{Word oriented command} @tab @b{Subword oriented command} @end iftex @ifnottex @item Key @tab Word oriented command @tab Subword oriented command @item ---------------------------------------------------------------------------- @end ifnottex @item @kbd{M-f} @tab @code{forward-word} @tab @code{c-forward-subword} @item @kbd{M-b} @tab @code{backward-word} @tab @code{c-backward-subword} @item @kbd{M-@@} @tab @code{mark-word} @tab @code{c-mark-subword} @item @kbd{M-d} @tab @code{kill-word} @tab @code{c-kill-subword} @item @kbd{M-DEL} @tab @code{backward-kill-word} @tab @code{c-backward-kill-subword} @item @kbd{M-t} @tab @code{transpose-words} @tab @code{c-transpose-subwords} @item @kbd{M-c} @tab @code{capitalize-word} @tab @code{c-capitalize-subword} @item @kbd{M-u} @tab @code{upcase-word} @tab @code{c-upcase-subword} @item @kbd{M-l} @tab @code{downcase-word} @tab @code{c-downcase-subword} @end multitable Note that if you have changed the key bindings for the word oriented commands in your @file{.emacs} or a similar place, the keys you have configured are also used for the corresponding subword oriented commands. Type @kbd{C-c C-w} to toggle subword mode on and off. To make the mode turn on automatically, put the following code in your @file{.emacs}: @example (add-hook 'c-mode-common-hook (lambda () (c-subword-mode 1))) @end example As a bonus, you can also use @code{c-subword-mode} in non-@ccmode{} buffers by typing @kbd{M-x c-subword-mode}. @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node Other Commands, , Subword Movement, Commands @comment node-name, next, previous, up @section Other Commands @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Here are the various other commands that didn't fit anywhere else: @table @asis @item @kbd{C-c .} (@code{c-set-style}) @kindex C-c . @findex c-set-style @findex set-style (c-) Switch to the specified style in the current buffer. Use like this: @example @kbd{C-c . @var{style-name} @key{RET}} @end example You can use the @key{TAB} in the normal way to do completion on the style name. Note that all style names are case insensitive, even the ones you define yourself. Setting a style in this way does @emph{not} automatically reindent your file. For commands that you can use to view the effect of your changes, see @ref{Indentation Commands} and @ref{Filling and Breaking}. For details of the @ccmode{} style system, see @ref{Styles}. @item @kbd{C-c :} (@code{c-scope-operator}) @kindex C-c : @findex c-scope-operator @findex scope-operator (c-) In C++, it is also sometimes desirable to insert the double-colon scope operator without performing the electric behavior of colon insertion. @kbd{C-c :} does just this. @item @kbd{C-c C-\} (@code{c-backslash-region}) @kindex C-c C-\ @findex c-backslash-region @findex backslash-region (c-) This function inserts and aligns or deletes end-of-line backslashes in the current region. These are typically used in multi-line macros. With no prefix argument, it inserts any missing backslashes and aligns them according to the @code{c-backslash-column} and @code{c-backslash-max-column} variables. With a prefix argument, it deletes any backslashes. The function does not modify blank lines at the start of the region. If the region ends at the start of a line, it always deletes the backslash (if any) at the end of the previous line. To customize the precise workings of this command, @ref{Custom Macros}. @end table @noindent The recommended line breaking function, @code{c-context-line-break} (@pxref{Filling and Breaking}), is especially nice if you edit multiline macros frequently. When used inside a macro, it automatically inserts and adjusts the mandatory backslash at the end of the line to keep the macro together, and it leaves the point at the right indentation column for the code. Thus you can write code inside macros almost exactly as you can elsewhere, without having to bother with the trailing backslashes. @table @asis @item @kbd{C-c C-e} (@code{c-macro-expand}) @kindex C-c C-e @findex c-macro-expand @findex macro-expand (c-) This command expands C, C++, Objective C or Pike macros in the region, using an appropriate external preprocessor program. Normally it displays its output in a temporary buffer, but if you give it a prefix arg (with @kbd{C-u C-c C-e}) it will overwrite the original region with the expansion. The command does not work in any of the other modes, and the key sequence is not bound in these other modes. @code{c-macro-expand} isn't actually part of @ccmode{}, even though it is bound to a @ccmode{} key sequence. If you need help setting it up or have other problems with it, you can either read its source code or ask for help in the standard (X)Emacs forums. @end table @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node Font Locking, Config Basics, Commands, Top @comment node-name, next, previous, up @chapter Font Locking @cindex font locking @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @cindex Font Lock mode @ccmode{} provides font locking for its supported languages by supplying patterns for use with Font Lock mode. This means that you get distinct faces on the various syntactic parts such as comments, strings, keywords and types, which is very helpful in telling them apart at a glance and discovering syntactic errors. @xref{Font Lock,,, emacs, GNU Emacs Manual}, for ways to enable font locking in @ccmode{} buffers. @strong{Please note:} The font locking in AWK mode is currently not integrated with the rest of @ccmode{}. Only the last section of this chapter, @ref{AWK Mode Font Locking}, applies to AWK. The other sections apply to the other languages. @menu * Font Locking Preliminaries:: * Faces:: * Doc Comments:: * AWK Mode Font Locking:: @end menu @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node Font Locking Preliminaries, Faces, Font Locking, Font Locking @comment node-name, next, previous, up @section Font Locking Preliminaries @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! The font locking for most of the @ccmode{} languages were provided directly by the Font Lock package prior to version 5.30 of @ccmode{}. In the transition to @ccmode{} the patterns have been reworked completely and are applied uniformly across all the languages except AWK mode, just like the indentation rules (although each language still has some peculiarities of its own, of course). Since the languages previously had completely separate font locking patterns, this means that it's a bit different in most languages now. The main goal for the font locking in @ccmode{} is accuracy, to provide a dependable aid in recognizing the various constructs. Some, like strings and comments, are easy to recognize while others, like declarations and types, can be very tricky. @ccmode{} can go to great lengths to recognize declarations and casts correctly, especially when the types aren't recognized by standard patterns. This is a fairly demanding analysis which can be slow on older hardware, and it can therefore be disabled by choosing a lower decoration level with the variable @code{font-lock-maximum-decoration} (@pxref{Font Lock,,, emacs, GNU Emacs Manual}). @vindex font-lock-maximum-decoration The decoration levels are used as follows: @enumerate @comment 1 @item Minimal font locking: Fontify only comments, strings and preprocessor directives (in the languages that use cpp). @comment 2 @item Fast font locking: In addition to level 1, fontify keywords, simple types and declarations that are easy to recognize. The variables @code{*-font-lock-extra-types} (where @samp{*} is the name of the language) are used to recognize types (see below). Documentation comments like Javadoc are fontified according to @code{c-doc-comment-style} (@pxref{Doc Comments}). Use this if you think the font locking is too slow. It's the closest corresponding level to level 3 in the old font lock patterns. @comment 3 @item Accurate font locking: Like level 2 but uses a different approach that can recognize types and declarations much more accurately. The @code{*-font-lock-extra-types} variables are still used, but user defined types are recognized correctly anyway in most cases. Therefore those variables should be fairly restrictive and not contain patterns that are uncertain. @cindex Lazy Lock mode @cindex Just-in-time Lock mode This level is designed for fairly modern hardware and a font lock support mode like Lazy Lock or Just-in-time Lock mode that only fontifies the parts that are actually shown. Fontifying the whole buffer at once can easily get bothersomely slow even on contemporary hardware. @xref{Font Lock,,,@emacsman{}, @emacsmantitle{}}. @end enumerate @cindex user defined types @cindex types, user defined Since user defined types are hard to recognize you can provide additional regexps to match those you use: @defopt c-font-lock-extra-types @defoptx c++-font-lock-extra-types @defoptx objc-font-lock-extra-types @defoptx java-font-lock-extra-types @defoptx idl-font-lock-extra-types @defoptx pike-font-lock-extra-types For each language there's a variable @code{*-font-lock-extra-types}, where @samp{*} stands for the language in question. It contains a list of regexps that matches identifiers that should be recognized as types, e.g. @samp{\\sw+_t} to recognize all identifiers ending with @samp{_t} as is customary in C code. Each regexp should not match more than a single identifier. The default values contain regexps for many types in standard runtime libraries that are otherwise difficult to recognize, and patterns for standard type naming conventions like the @samp{_t} suffix in C and C++. Java, Objective-C and Pike have as a convention to start class names with capitals, so there are patterns for that in those languages. Despite the names of these variables, they are not only used for fontification but in other places as well where @ccmode{} needs to recognize types. @end defopt @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node Faces, Doc Comments, Font Locking Preliminaries, Font Locking @comment node-name, next, previous, up @section Faces @cindex faces @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @ccmode{} attempts to use the standard faces for programming languages in accordance with their intended purposes as far as possible. No extra faces are currently provided, with the exception of a replacement face @code{c-invalid-face} for emacsen that don't provide @code{font-lock-warning-face}. @itemize @bullet @item @vindex font-lock-comment-face Normal comments are fontified in @code{font-lock-comment-face}. @item @vindex font-lock-doc-face @vindex font-lock-doc-string-face @vindex font-lock-comment-face Comments that are recognized as documentation (@pxref{Doc Comments}) get @code{font-lock-doc-face} (Emacs) or @code{font-lock-doc-string-face} (XEmacs) if those faces exist. If they don't then @code{font-lock-comment-face} is used. @item @vindex font-lock-string-face String and character literals are fontified in @code{font-lock-string-face}. @item @vindex font-lock-keyword-face Keywords are fontified with @code{font-lock-keyword-face}. @item @vindex font-lock-function-name-face @code{font-lock-function-name-face} is used for function names in declarations and definitions, and classes in those contexts. It's also used for preprocessor defines with arguments. @item @vindex font-lock-variable-name-face Variables in declarations and definitions, and other identifiers in such variable contexts, get @code{font-lock-variable-name-face}. It's also used for preprocessor defines without arguments. @item @vindex font-lock-constant-face @vindex font-lock-reference-face Builtin constants are fontified in @code{font-lock-constant-face} if it exists, @code{font-lock-reference-face} otherwise. As opposed to the preceding two faces, this is used on the names in expressions, and it's not used in declarations, even if there happen to be a @samp{const} in them somewhere. @item @vindex font-lock-type-face @code{font-lock-type-face} is put on types (both predefined and user defined) and classes in type contexts. @item @vindex font-lock-constant-face @vindex font-lock-reference-face Label identifiers get @code{font-lock-constant-face} if it exists, @code{font-lock-reference-face} otherwise. @item Name qualifiers and identifiers for scope constructs are fontified like labels. @item Special markup inside documentation comments are also fontified like labels. @item @vindex font-lock-preprocessor-face @vindex font-lock-builtin-face @vindex font-lock-reference-face Preprocessor directives get @code{font-lock-preprocessor-face} if it exists (i.e. XEmacs). In Emacs they get @code{font-lock-builtin-face} or @code{font-lock-reference-face}, for lack of a closer equivalent. @item @vindex font-lock-warning-face @vindex c-invalid-face @vindex invalid-face (c-) Some kinds of syntactic errors are fontified with @code{font-lock-warning-face} in Emacs. In older XEmacs versions there's no corresponding standard face, so there a special @code{c-invalid-face} is used, which is defined to stand out sharply by default. Note that it's not used for @samp{#error} or @samp{#warning} directives, since those aren't syntactic errors in themselves. @end itemize @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node Doc Comments, AWK Mode Font Locking, Faces, Font Locking @comment node-name, next, previous, up @section Documentation Comments @cindex documentation comments @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! There are various tools to supply documentation in the source as specially structured comments, e.g. the standard Javadoc tool in Java. @ccmode{} provides an extensible mechanism to fontify such comments and the special markup inside them. @defopt c-doc-comment-style @vindex doc-comment-style (c-) This is a style variable that specifies which documentation comment style to recognize, e.g. @code{javadoc} for Javadoc comments. The value may also be a list of styles, in which case all of them are recognized simultaneously (presumably with markup cues that don't conflict). The value may also be an association list to specify different comment styles for different languages. The symbol for the major mode is then looked up in the alist, and the value of that element is interpreted as above if found. If it isn't found then the symbol `other' is looked up and its value is used instead. The default value for @code{c-doc-comment-style} is @w{@code{((java-mode . javadoc) (pike-mode . autodoc) (c-mode . gtkdoc))}}. Note that @ccmode{} uses this variable to set other variables that handle fontification etc. That's done at mode initialization or when you switch to a style which sets this variable. Thus, if you change it in some other way, e.g. interactively in a CC Mode buffer, you will need to do @kbd{M-x java-mode} (or whatever mode you're currently using) to reinitialize. @findex c-setup-doc-comment-style @findex setup-doc-comment-style (c-) Note also that when @ccmode{} starts up, the other variables are modified before the mode hooks are run. If you change this variable in a mode hook, you'll have to call @code{c-setup-doc-comment-style} afterwards to redo that work. @end defopt @ccmode{} currently provides handing of the following doc comment styles: @table @code @item javadoc @cindex Javadoc markup Javadoc comments, the standard tool in Java. @item autodoc @cindex Pike autodoc markup For Pike autodoc markup, the standard in Pike. @item gtkdoc @cindex GtkDoc markup For GtkDoc markup, widely used in the Gnome community. @end table The above is by no means complete. If you'd like to see support for other doc comment styles, please let us know (@pxref{Mailing Lists and Bug Reports}). You can also write your own doc comment fontification support to use with @code{c-doc-comment-style}: Supply a variable or function @code{*-font-lock-keywords} where @samp{*} is the name you want to use in @code{c-doc-comment-style}. If it's a variable, it's prepended to @code{font-lock-keywords}. If it's a function, it's called at mode initialization and the result is prepended. For an example, see @code{javadoc-font-lock-keywords} in @file{cc-fonts.el}. If you add support for another doc comment style, please consider contributing it - send a note to @email{bug-cc-mode@@gnu.org}. @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node AWK Mode Font Locking, , Doc Comments, Font Locking @comment node-name, next, previous, up @section AWK Mode Font Locking @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! The general appearance of font-locking in AWK mode is much like in any other programming mode. @xref{Faces For Font Lock,,,elisp, GNU Emacs Lisp Reference Manual}. The following faces are, however, used in a non-standard fashion in AWK mode: @table @asis @item @code{font-lock-variable-name-face} This face was intended for variable declarations. Since variables are not declared in AWK, this face is used instead for AWK system variables (such as @code{NF}) and ``Special File Names'' (such as @code{"/dev/stderr"}). @item @code{font-lock-builtin-face} (Emacs)/@code{font-lock-preprocessor-face} (XEmacs) This face is normally used for preprocessor directives in @ccmode{}. There are no such things in AWK, so this face is used instead for standard functions (such as @code{match}). @item @code{font-lock-string-face} As well as being used for strings, including localizable strings, (delimited by @samp{"} and @samp{_"}), this face is also used for AWK regular expressions (delimited by @samp{/}). @item @code{font-lock-warning-face} (Emacs)/@code{c-invalid-face} (XEmacs) This face highlights the following syntactically invalid AWK constructs: @itemize @bullet @item An unterminated string or regular expression. Here the opening delimiter (@samp{"} or @samp{/} or @samp{_"}) is displayed in @code{font-lock-warning-face}. This is most noticeable when typing in a new string/regular expression into a buffer, when the warning-face serves as a continual reminder to terminate the construct. AWK mode fontifies unterminated strings/regular expressions differently from other modes: Only the text up to the end of the line is fontified as a string (escaped newlines being handled correctly), rather than the text up to the next string quote. @item A space between the function name and opening parenthesis when calling a user function. The last character of the function name and the opening parenthesis are highlighted. This font-locking rule will spuriously highlight a valid concatenation expression where an identifier precedes a parenthesised expression. Unfortunately. @item Whitespace following the @samp{\} in what otherwise looks like an escaped newline. The @samp{\} is highlighted. @end itemize @end table @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node Config Basics, Custom Filling and Breaking, Font Locking, Top @comment node-name, next, previous, up @chapter Configuration Basics @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @cindex Emacs Initialization File @cindex Configuration You configure @ccmode{} by setting Lisp variables and calling (and perhaps writing) Lisp functions@footnote{DON'T PANIC!!! This isn't difficult.}, which is usually done by adding code to an Emacs initialization file. This file might be @file{site-start.el} or @file{.emacs} or @file{init.el} or @file{default.el} or perhaps some other file. @xref{Init File,,,@emacsman{}, @emacsmantitle{}}. For the sake of conciseness, we just call this file ``your @file{.emacs}'' throughout the rest of the manual. Several of these variables (currently 16), are known collectively as @dfn{style variables}. @ccmode{} provides a special mechanism, known as @dfn{styles} to make it easier to set these variables as a group, to ``inherit'' settings from one style into another, and so on. Style variables remain ordinary Lisp variables, whose values can be read and changed independently of the style system. @xref{Style Variables}. There are several ways you can write the code, depending on the precise effect you want---they are described further down on this page. If you are new to @ccmode{}, we suggest you begin with the simplest method, ``Top-level commands or the customization interface''. If you make conflicting settings in several of these ways, the way that takes precedence is the one that appears latest in this list: @itemize @asis @item @table @asis @item Style @itemx Top-level command or ``customization interface'' @itemx Hook @itemx File Style @end table @end itemize Here is a summary of the different ways of writing your configuration settings: @table @asis @item Top-level commands or the ``customization interface'' Most simply, you can write @code{setq} and similar commands at the top level of your @file{.emacs} file. When you load a @ccmode{} buffer, it initializes its configuration from these global values (at least, for those settings you have given values to), so it makes sense to have these @code{setq} commands run @emph{before} @ccmode{} is first initialized---in particular, before any call to @code{desktop-read} (@pxref{Saving Emacs Sessions,,, emacs, GNU Emacs Manual}). For example, you might set c-basic-offset thus: @example (setq c-basic-offset 4) @end example You can use the more user friendly Customization interface instead, but this manual does not cover in detail how that works. To do this, start by typing @kbd{M-x customize-group @key{RET} c @key{RET}}. @xref{Easy Customization,,,@emacsman{}, @emacsmantitle{}}. @c The following note really belongs in the Emacs manual. Emacs normally writes the customizations at the end of your @file{.emacs} file. If you use @code{desktop-read}, you should edit your @file{.emacs} to place the call to @code{desktop-read} @emph{after} the customizations. The first initialization of @ccmode{} puts a snapshot of the configuration settings into the special style @code{user}. @xref{Built-in Styles}. For basic use of Emacs, either of these ways of configuring is adequate. However, the settings are then the same in all @ccmode{} buffers and it can be clumsy to communicate them between programmers. For more flexibility, you'll want to use one (or both) of @ccmode{}'s more sophisticated facilities, hooks and styles. @item Hooks An Emacs @dfn{hook} is a place to put Lisp functions that you want Emacs to execute later in specific circumstances. @xref{Hooks,,,@lispref{}, @lispreftitle{}}. @ccmode{} supplies a main hook and a language-specific hook for each language it supports - any functions you put onto these hooks get executed as the last part of a buffer's initialization. Typically you put most of your customization within the main hook, and use the language-specific hooks to vary the customization settings between language modes. For example, if you wanted different (non-standard) values of @code{c-basic-offset} in C Mode and Java Mode buffers, you could do it like this: @example @group (defun my-c-mode-hook () (setq c-basic-offset 3)) (add-hook 'c-mode-hook 'my-c-mode-hook) (defun my-java-mode-hook () (setq c-basic-offset 6)) (add-hook 'java-mode-hook 'my-java-mode-hook) @end group @end example See @ref{CC Hooks} for more details on the use of @ccmode{} hooks. @item Styles A @ccmode{} @dfn{style} is a coherent collection of customizations with a name. At any time, exactly one style is active in each @ccmode{} buffer, either the one you have selected or a default. @ccmode{} is delivered with several existing styles. Additionally, you can create your own styles, possibly based on these existing styles. If you worked in a programming team called the ``Free Group'', which had its own coding standards, you might well have this in your @file{.emacs} file: @example (setq c-default-style '((java-mode . "java") (awk-mode . "awk") (other . "free-group-style"))) @end example See @ref{Styles} for fuller details on using @ccmode{} styles and how to create them. @item File Styles A @dfn{file style} is a rarely used variant of the ``style'' mechanism described above, which applies to an individual source file. To use it, you set certain Emacs local variables in a special block at the end of the source file. @xref{File Styles}. @item Hooks with Styles For ultimate flexibility, you can use hooks and styles together. For example, if your team were developing a product which required a Linux driver, you'd probably want to use the ``linux'' style for the driver, and your own team's style for the rest of the code. You could achieve this with code like this in your @file{.emacs}: @example @group (defun my-c-mode-hook () (c-set-style (if (and (buffer-file-name) (string-match "/usr/src/linux" (buffer-file-name))) "linux" "free-group-style"))) (add-hook 'c-mode-hook 'my-c-mode-hook) @end group @end example In a programming team, a hook is a also a good place for each member to put his own personal preferences. For example, you might be the only person in your team who likes Auto-newline minor mode. You could have it enabled by default by placing the following in your @file{.emacs}: @example @group (defun my-turn-on-auto-newline () (c-toggle-auto-newline 1)) (add-hook 'c-mode-common-hook 'my-turn-on-auto-newline) @end group @end example @end table @menu * CC Hooks:: * Style Variables:: * Styles:: @end menu @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node CC Hooks, Style Variables, Config Basics, Config Basics @comment node-name, next, previous, up @section Hooks @cindex mode hooks @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @c The node name is "CC Hooks" rather than "Hooks" because of a bug in @c some older versions of Info, e.g. the info.el in GNU Emacs 21.3. @c If you go to "Config Basics" and hit on the xref to "CC @c Hooks" the function Info-follow-reference searches for "*Note: CC @c Hooks" from the beginning of the page. If this node were instead @c named "Hooks", that search would spuriously find "*Note: @c Hooks(elisp)" and go to the wrong node. @ccmode{} provides several hooks that you can use to customize the mode for your coding style. The main hook is @code{c-mode-common-hook}; typically, you'll put the bulk of your customizations here. In addition, each language mode has its own hook, allowing you to fine tune your settings individually for the different @ccmode{} languages, and there is a package initialization hook. Finally, there is @code{c-special-indent-hook}, which enables you to solve anomalous indentation problems. It is described in @ref{Other Indentation}, not here. All these hooks adhere to the standard Emacs conventions. When you open a buffer, @ccmode{} first initializes it with the currently active style (@pxref{Styles}). Then it calls @code{c-mode-common-hook}, and finally it calls the language-specific hook. Thus, any style settings done in these hooks will override those set by @code{c-default-style}. @defvar c-initialization-hook @vindex initialization-hook (c-) Hook run only once per Emacs session, when @ccmode{} is initialized. This is a good place to change key bindings (or add new ones) in any of the @ccmode{} key maps. @xref{Sample .emacs File}. @end defvar @defvar c-mode-common-hook @vindex mode-common-hook (c-) Common hook across all languages. It's run immediately before the language specific hook. @end defvar @defvar c-mode-hook @defvarx c++-mode-hook @defvarx objc-mode-hook @defvarx java-mode-hook @defvarx idl-mode-hook @defvarx pike-mode-hook @defvarx awk-mode-hook The language specific mode hooks. The appropriate one is run as the last thing when you enter that language mode. @end defvar Although these hooks are variables defined in @ccmode{}, you can give them values before @ccmode{}'s code is loaded---indeed, this is the only way to use @code{c-initialization-hook}. Their values aren't overwritten when @ccmode{} gets loaded. Here's a simplified example of what you can add to your @file{.emacs} file to do things whenever any @ccmode{} language is edited. See the Emacs manuals for more information on customizing Emacs via hooks. @xref{Sample .emacs File}, for a more complete sample @file{.emacs} file. @example (defun my-c-mode-common-hook () ;; my customizations for all of c-mode and related modes (no-case-fold-search) ) (add-hook 'c-mode-common-hook 'my-c-mode-common-hook) @end example @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node Style Variables, Styles, CC Hooks, Config Basics @comment node-name, next, previous, up @section Style Variables @cindex styles @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @cindex style variables The variables that @ccmode{}'s style system control are called @dfn{style variables}. Note that style variables are ordinary Lisp variables, which the style system initializes; you can change their values at any time (e.g. in a hook function). The style system can also set other variables, to some extent. @xref{Styles}. @dfn{Style variables} are handled specially in several ways: @itemize @bullet @item Style variables are by default buffer-local variables. However, they can instead be made global by setting @code{c-style-variables-are-local-p} to @code{nil} before @ccmode{} is initialized. @item @vindex c-old-style-variable-behavior @vindex old-style-variable-behavior (c-) The default global binding of any style variable (with two exceptions - see below) is the special symbol @code{set-from-style}. When the style system initializes a buffer-local copy of a style variable for a @ccmode{} buffer, if its global binding is still that symbol then it will be set from the current style. Otherwise it will retain its global default@footnote{This is a big change from versions of @ccmode{} earlier than 5.26, where such settings would get overridden by the style system unless special precautions were taken. That was changed since it was counterintuitive and confusing, especially to novice users. If your configuration depends on the old overriding behavior, you can set the variable @code{c-old-style-variable-behavior} to non-@code{nil}.}. This ``otherwise'' happens, for example, when you've set the variable with @code{setq} at the top level of your @file{.emacs} (@pxref{Config Basics}). @item The style variable @code{c-offsets-alist} (@pxref{c-offsets-alist}) is an association list with an element for each syntactic symbol. It's handled a little differently from the other style variables. It's default global binding is the empty list @code{nil}, rather than @code{set-from-style}. Before the style system is initialized, you can add individual elements to @code{c-offsets-alist} by calling @code{c-set-offset}(@pxref{c-offsets-alist}) just like you would set other style variables with @code{setq}. Those elements will then prevail when the style system later initializes a buffer-local copy of @code{c-offsets-alist}. @item The style variable @code{c-special-indent-hook} is also handled in a special way. Styles can only add functions to this hook, not remove them, so any global settings you put on it are always preserved@footnote{This did not change in version 5.26.}. The value you give this variable in a style definition can be either a function or a list of functions. @item The global bindings of the style variables get captured in the special @code{user} style when the style system is first initialized. @xref{Built-in Styles}, for details. @end itemize The style variables are:@* @code{c-indent-comment-alist}, @code{c-indent-comments-syntactically-p} (@pxref{Indentation Commands});@* @code{c-doc-comment-style} (@pxref{Doc Comments});@* @code{c-block-comment-prefix}, @code{c-comment-prefix-regexp} (@pxref{Custom Filling and Breaking});@* @code{c-hanging-braces-alist} (@pxref{Hanging Braces});@* @code{c-hanging-colons-alist} (@pxref{Hanging Colons});@* @code{c-hanging-semi&comma-criteria} (@pxref{Hanging Semicolons and Commas});@* @code{c-cleanup-list} (@pxref{Clean-ups});@* @code{c-basic-offset} (@pxref{Customizing Indentation});@* @code{c-offsets-alist} (@pxref{c-offsets-alist});@* @code{c-comment-only-line-offset} (@pxref{Comment Line-Up});@* @code{c-special-indent-hook}, @code{c-label-minimum-indentation} (@pxref{Other Indentation});@* @code{c-backslash-column}, @code{c-backslash-max-column} (@pxref{Custom Macros}). @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node Styles, , Style Variables, Config Basics @comment node-name, next, previous, up @section Styles @cindex styles @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! By @dfn{style} we mean the layout of the code---things like how many columns to indent a block of code, whether an opening brace gets indented to the level of the code it encloses, or of the construct that introduces it, or ``hangs'' at the end of a line. Most people only need to edit code formatted in just a few well-defined and consistent styles. For example, their organization might impose a ``blessed'' style that all its programmers must conform to. Similarly, people who work on GNU software will have to use the GNU coding style. Some shops are more lenient, allowing a variety of coding styles, and as programmers come and go, there could be a number of 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. @menu * Built-in Styles:: * Choosing a Style:: * Adding Styles:: * File Styles:: @end menu @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node Built-in Styles, Choosing a Style, Styles, Styles @comment node-name, next, previous, up @subsection Built-in Styles @cindex styles, built-in @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! If you're lucky, one of @ccmode{}'s built-in styles might be just what you're looking for. These are: @table @code @item gnu @cindex GNU style Coding style blessed by the Free Software Foundation for C code in GNU programs. @item k&r @cindex K&R style The classic Kernighan and Ritchie style for C code. @item bsd @cindex BSD style Also known as ``Allman style'' after Eric Allman. @item whitesmith @cindex Whitesmith style Popularized by the examples that came with Whitesmiths C, an early commercial C compiler. @item stroustrup @cindex Stroustrup style The classic Stroustrup style for C++ code. @item ellemtel @cindex Ellemtel style Popular C++ coding standards as defined by ``Programming in C++, Rules and Recommendations,'' Erik Nyquist and Mats Henricson, Ellemtel@footnote{This document is available at @uref{http://www.doc.ic.ac.uk/lab/cplus/c++.rules/} among other places.}. @c N.B. This URL was still valid at 2005/8/28 (ACM). @item linux @cindex Linux style C coding standard for Linux (the kernel). @item python @cindex Python style 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 @uref{http://www.python.org/}.}. @item java @cindex Java style The style for editing Java code. Note that the default value for @code{c-default-style} installs this style when you enter @code{java-mode}. @item awk @cindex AWK style The style for editing AWK code. Note that the default value for @code{c-default-style} installs this style when you enter @code{awk-mode}. @item user @cindex User style This is a special style created by you. It consists of the factory defaults for all the style variables as modified by the customizations you do either with the Customization interface or by writing @code{setq}s and @code{c-set-offset}s at the top level of your @file{.emacs} file (@pxref{Config Basics}). The style system creates this style as part of its initialization and doesn't modify it afterwards. @end table @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node Choosing a Style, Adding Styles, Built-in Styles, Styles @comment node-name, next, previous, up @subsection Choosing a Style @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! When you create a new buffer, its style will be set from @code{c-default-style}. The factory default is the style @code{gnu}, except in Java and AWK modes where it's @code{java} and @code{awk}. Remember that if you set a style variable with the Customization interface or at the top level of your @file{.emacs} file before the style system is initialised (@pxref{Config Basics}), this setting will override the one that the style system would have given the variable. To set a buffer's style interactively, use the command @kbd{C-c .} (@pxref{Other Commands}). To set it from a file's local variable list, @ref{File Styles}. @defopt c-default-style @vindex default-style (c-) This variable specifies which style to install by default in new buffers. It takes either a style name string, or an association list of major mode symbols to style names: @enumerate @item When @code{c-default-style} is a string, it must be an existing style name. This style is then used for all modes. @item When @code{c-default-style} is an association list, the mode language is looked up to find a style name string. @item If @code{c-default-style} is an association list where the mode language mode isn't found then the special symbol @samp{other} is looked up. If it's found then the associated style is used. @item If @samp{other} is not found then the @samp{gnu} style is used. @end enumerate In all cases, the style described in @code{c-default-style} is installed @emph{before} the language hooks are run, so you can always override this setting by including an explicit call to @code{c-set-style} in your language mode hook, or in @code{c-mode-common-hook}. The standard value of @code{c-default-style} is @w{@code{((java-mode . "java") (awk-mode . "awk") (other . "gnu"))}}. @end defopt @defvar c-indentation-style @vindex indentation-style (c-) This variable always contains the buffer's current style name, as a string. @end defvar @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node Adding Styles, File Styles, Choosing a Style, Styles @comment node-name, next, previous, up @subsection Adding and Amending Styles @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! If none of the built-in styles is appropriate, you'll probably want to create a new @dfn{style definition}, possibly based on an existing style. To do this, put the new style's settings into a list with the following format - the list can then be passed as an argument to the function @code{c-add-style}. You can see an example of a style definition in @ref{Sample .emacs File}. @cindex style definition @c @defvr {List} style definition @table @asis @item Structure of a Style Definition List ([@var{base-style}] [(@var{variable} . @var{value}) @dots{}]) Optional @var{base-style}, if present, must be a string which is the name of the @dfn{base style} from which this style inherits. At most one @var{base-style} is allowed in a style definition. If @var{base-style} is not specified, the style inherits from the table of factory default values@footnote{This table is stored internally in the variable c-fallback-style.} instead. All styles eventually inherit from this internal table. Style loops generate errors. The list of pre-existing styles can be seen in @ref{Built-in Styles}. The dotted pairs (@var{variable} . @var{value}) each consist of a variable and the value it is to be set to when the style is later activated.@footnote{Note that if the variable has been given a value by the Customization interface or a @code{setq} at the top level of your @file{.emacs}, this value will override the one the style system tries to give it. @xref{Config Basics}.} The variable can be either a @ccmode{} style variable or an arbitrary Emacs variable. In the latter case, it is @emph{not} made buffer-local by the @ccmode{} style system. @c @end defvr Two variables are treated specially in the dotted pair list: @table @code @item c-offsets-alist The value is in turn a list of dotted pairs of the form @example (@r{@var{syntactic-symbol}} . @r{@var{offset}}) @end example as described in @ref{c-offsets-alist}. These are passed to @code{c-set-offset} so there is no need to set every syntactic symbol in your style, only those that are different from the inherited style. @item c-special-indent-hook The value is added to @code{c-special-indent-hook} using @code{add-hook}, so any functions already on it are kept. If the value is a list, each element of the list is added with @code{add-hook}. @end table @end table Styles are kept in the @code{c-style-alist} variable, but you should never modify this variable directly. Instead, @ccmode{} provides the function @code{c-add-style} for this purpose. @defun c-add-style stylename description &optional set-p @findex add-style (c-) Add or update a style called @var{stylename}, a string. @var{description} is the new style definition in the form described above. If @var{stylename} already exists in @code{c-style-alist} then it is replaced by @var{description}. (Note, this replacement is total. The old style is @emph{not} merged into the new one.) Otherwise, a new style is added. If the optional @var{set-p} is non-@code{nil} then the new style is applied to the current buffer as well. The use of this facility is deprecated and it might be removed from @ccmode{} in a future release. You should use @code{c-set-style} instead. The sample @file{.emacs} file provides a concrete example of how a new style can be added and automatically set. @xref{Sample .emacs File}. @end defun @defvar c-style-alist @vindex style-alist (c-) This is the variable that holds the definitions for the styles. It should not be changed directly; use @code{c-add-style} instead. @end defvar @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node File Styles, , Adding Styles, Styles @comment node-name, next, previous, up @subsection File Styles @cindex styles, file local @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @cindex file local variables The Emacs manual describes how you can customize certain variables on a per-file basis by including a @dfn{file local variable} block at the end of the file (@pxref{File Variables,, Local Variables in Files, @emacsman{}, @emacsmantitle{}}). So far, you've only seen a functional interface for setting styles in @ccmode{}, and this can't be used here. @ccmode{} fills the gap by providing two variables for use in a file's local variable list. Don't use them anywhere else! These allow you to customize the style on a per-file basis: @defvar c-file-style @vindex file-style (c-) Set this variable to a style name string in the Local Variables list. From now on, when you visit the file, @ccmode{} will automatically set the file's style to this one using @code{c-set-style}. @end defvar @defvar c-file-offsets @vindex file-offsets (c-) Set this variable (in the Local Variables list) to an association list of the same format as @code{c-offsets-alist}. From now on, when you visit the file, @ccmode{} will automatically institute these offsets using @code{c-set-offset}. @end defvar Note that file style settings (i.e. @code{c-file-style}) are applied before file offset settings (i.e. @code{c-file-offsets})@footnote{Also, if either of these are set in a file's local variable section, all the style variable values are made local to that buffer, even if @code{c-style-variables-are-local-p} is @code{nil}. Since this variable is virtually always non-@code{nil} anyhow, you're unlikely to notice this effect.}. If you set any variables, including style variables, by the file local variables mechanism, these settings take priority over all other settings, even those in your mode hooks (@pxref{CC Hooks}). If you use @code{c-file-style} or @code{c-file-offsets} and also explicitly set a style variable in a local variable block, the explicit setting will take priority. @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node Custom Filling and Breaking, Custom Auto-newlines, Config Basics, Top @comment node-name, next, previous, up @chapter Customizing Filling and Line Breaking @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Since there's a lot of normal text in comments and string literals, @ccmode{} provides features to edit these like in text mode. It does this by hooking in on the different line breaking functions and tuning relevant variables as necessary. @vindex c-comment-prefix-regexp @vindex comment-prefix-regexp (c-) @cindex comment line prefix @vindex comment-start @vindex comment-end @vindex comment-start-skip @vindex paragraph-start @vindex paragraph-separate @vindex paragraph-ignore-fill-prefix @vindex adaptive-fill-mode @vindex adaptive-fill-regexp @vindex adaptive-fill-first-line-regexp To make Emacs recognize comments and treat text in them as normal paragraphs, @ccmode{} makes several standard variables@footnote{@code{comment-start}, @code{comment-end}, @code{comment-start-skip}, @code{paragraph-start}, @code{paragraph-separate}, @code{paragraph-ignore-fill-prefix}, @code{adaptive-fill-mode}, @code{adaptive-fill-regexp}, and @code{adaptive-fill-first-line-regexp}.} buffer-local and modifies them according to the language syntax and the comment line prefix. @defopt c-comment-prefix-regexp @vindex comment-prefix-regexp (c-) This style variable contains the regexp used to recognize the @dfn{comment line prefix}, which is the line decoration that starts every line in a comment. The variable is either the comment line prefix itself, or (more usually) an association list with different values for different languages. The symbol for the major mode is looked up in the alist to get the regexp for the language, and if it isn't found then the special symbol @samp{other} is looked up instead. When a comment line gets divided by @kbd{M-j} or the like, @ccmode{} inserts the comment line prefix from a neighbouring line at the start of the new line. The default value of c-comment-prefix-regexp is @samp{//+\\|\\**}, which matches C++ style line comments like @example // blah blah @end example @noindent with two or more slashes in front of them, and the second and subsequent lines of C style block comments like @example @group /* * blah blah */ @end group @end example @noindent with zero or more stars at the beginning of every line. If you change this variable, please make sure it still matches the comment starter (i.e. @code{//}) of line comments @emph{and} the line prefix inside block comments. @findex c-setup-paragraph-variables @findex setup-paragraph-variables (c-) Also note that since @ccmode{} uses the value of @code{c-comment-prefix-regexp} to set up several other variables at mode initialization, there won't be any effect if you just change it inside a @ccmode{} buffer. You need to call the command @code{c-setup-paragraph-variables} too, to update those other variables. That's also the case if you modify @code{c-comment-prefix-regexp} in a mode hook, since @ccmode{} will already have set up these variables before calling the hook. @end defopt In comments, @ccmode{} uses @code{c-comment-prefix-regexp} to adapt the line prefix from the other lines in the comment. @vindex adaptive-fill-mode @cindex Adaptive Fill mode @ccmode{} uses adaptive fill mode (@pxref{Adaptive Fill,,, emacs, GNU Emacs Manual}) to make Emacs correctly keep the line prefix when filling paragraphs. That also makes Emacs preserve the text indentation @emph{inside} the comment line prefix. E.g. in the following comment, both paragraphs will be filled with the left margins of the texts kept intact: @example @group /* Make a balanced b-tree of the nodes in the incoming * stream. But, to quote the famous words of Donald E. * Knuth, * * Beware of bugs in the above code; I have only * proved it correct, not tried it. */ @end group @end example @findex c-setup-filladapt @findex setup-filladapt (c-) @findex filladapt-mode @vindex filladapt-mode @cindex Filladapt mode It's also possible to use other adaptive filling packages, notably Kyle E. Jones' Filladapt package@footnote{It's available from @uref{http://www.wonderworks.com/}. As of version 2.12, it does however lack a feature that makes it work suboptimally when @code{c-comment-prefix-regexp} matches the empty string (which it does by default). A patch for that is available from @uref{http://cc-mode.sourceforge.net/,, the CC Mode web site}.}, @c 2005/11/22: The above is still believed to be the case. which handles things like bulleted lists nicely. There's a convenience function @code{c-setup-filladapt} that tunes the relevant variables in Filladapt for use in @ccmode{}. Call it from a mode hook, e.g. with something like this in your @file{.emacs}: @example (defun my-c-mode-common-hook () (c-setup-filladapt) (filladapt-mode 1)) (add-hook 'c-mode-common-hook 'my-c-mode-common-hook) @end example @defopt c-block-comment-prefix @vindex block-comment-prefix (c-) @vindex c-comment-continuation-stars @vindex comment-continuation-stars (c-) Normally the comment line prefix inserted for a new line inside a comment is deduced from other lines in it. However there's one situation when there's no hint about what the prefix should look like, namely when a block comment is broken for the first time. This style variable@footnote{In versions before 5.26, this variable was called @code{c-comment-continuation-stars}. As a compatibility measure, @ccmode{} still uses the value on that variable if it's set.} is used then as the comment prefix. It defaults to @samp{* }@footnote{Actually, this default setting of @code{c-block-comment-prefix} typically gets overridden by the default style @code{gnu}, which sets it to blank. You can see the line splitting effect described here by setting a different style, e.g. @code{k&r} @xref{Choosing a Style}.}, which makes a comment @example /* Got O(n^2) here, which is a Bad Thing. */ @end example @noindent break into @example @group /* Got O(n^2) here, which * is a Bad Thing. */ @end group @end example Note that it won't work to adjust the indentation by putting leading spaces in @code{c-block-comment-prefix}, since @ccmode{} still uses the normal indentation engine to indent the line. Thus, the right way to fix the indentation is by customizing the @code{c} syntactic symbol. It defaults to @code{c-lineup-C-comments}, which handles the indentation of most common comment styles, see @ref{Line-Up Functions}. @end defopt @defopt c-ignore-auto-fill @vindex ignore-auto-fill (c-) When auto fill mode is enabled, @ccmode{} can selectively ignore it depending on the context the line break would occur in, e.g. to never break a line automatically inside a string literal. This variable takes a list of symbols for the different contexts where auto-filling never should occur: @table @code @item string Inside a string or character literal. @item c Inside a C style block comment. @item c++ Inside a C++ style line comment. @item cpp Inside a preprocessor directive. @item code Anywhere else, i.e. in normal code. @end table By default, @code{c-ignore-auto-fill} is set to @code{(string cpp code)}, which means that when auto-fill mode is activated, auto-filling only occurs in comments. In literals, it's often desirable to have explicit control over newlines. In preprocessor directives, the necessary @samp{\} escape character before the newline is not automatically inserted, so an automatic line break would produce invalid code. In normal code, line breaks are normally dictated by some logical structure in the code rather than the last whitespace character, so automatic line breaks there will produce poor results in the current implementation. @end defopt @vindex comment-multi-line If inside a comment and @code{comment-multi-line} (@pxref{Auto Fill,,, @emacsman{}, @emacsmantitle{}} is non-@code{nil}, the indentation and line prefix are preserved. If inside a comment and @code{comment-multi-line} is @code{nil}, a new comment of the same type is started on the next line and indented as appropriate for comments. Note that @ccmode{} sets @code{comment-multi-line} to @code{t} at startup. The reason is that @kbd{M-j} could otherwise produce sequences of single line block comments for texts that should logically be treated as one comment, and the rest of the paragraph handling code (e.g. @kbd{M-q} and @kbd{M-a}) can't cope with that, which would lead to inconsistent behavior. @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node Custom Auto-newlines, Clean-ups, Custom Filling and Breaking, Top @comment node-name, next, previous, up @chapter Customizing Auto-newlines @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @ccmode{} determines whether to insert auto-newlines in two basically different ways, depending on the character just typed: @table @asis @item Braces and Colons @ccmode{} first determines the syntactic context of the brace or colon (@pxref{Syntactic Symbols}), then looks for a corresponding element in an alist. This element specifies where to put newlines - this is any combination of before and after the brace or colon. If no alist element is found, newlines are inserted both before and after a brace, but none are inserted around a colon. See @ref{Hanging Braces} and @ref{Hanging Colons}. @item Semicolons and Commas The variable @code{c-hanging-semi&comma-criteria} contains a list of functions which determine whether to insert a newline after a newly typed semicolon or comma. @xref{Hanging Semicolons and Commas}. @end table The names of these configuration variables contain @samp{hanging} because they let you @dfn{hang} the pertinent characters. A character which introduces a C construct is said to @dfn{hang on the right} when it appears at the end of a line after other code, being separated by a line break from the construct it introduces, like the opening brace in: @example @group while (i < MAX) @{ total += entry[i]; entry [i++] = 0; @} @end group @end example @noindent A character @dfn{hangs on the left} when it appears at the start of the line after the construct it closes off, like the above closing brace. The next chapter, ``Clean-ups'', describes how to configure @ccmode{} to remove these automatically added newlines in certain specific circumstances. @xref{Clean-ups}. @menu * Hanging Braces:: * Hanging Colons:: * Hanging Semicolons and Commas:: @end menu @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node Hanging Braces, Hanging Colons, Custom Auto-newlines, Custom Auto-newlines @comment node-name, next, previous, up @section Hanging Braces @cindex hanging braces @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! To specify which kinds of braces you want auto-newlines put around, you set the style variable @code{c-hanging-braces-alist}. Its structure and semantics are described in this section. Details of how to set it up, and its relationship to CC Mode's style system are given in @ref{Style Variables}. Say you wanted an auto-newline after (but not before) the following @samp{@{}: @example if (foo < 17) @{ @end example @noindent First you need to find the @dfn{syntactic context} of the brace---type a @key{RET} before the brace to get it on a line of its own@footnote{Also insert a @samp{\} at the end of the previous line if you're in AWK Mode.}, then type @kbd{C-c C-s}. That will tell you something like: @example ((substatement-open 1061)) @end example @noindent So here you need to put the entry @code{(substatement-open . (after))} into @code{c-hanging-braces-alist}. If you don't want any auto-newlines for a particular syntactic symbol, put this into @code{c-hanging-braces-alist}: @example (brace-entry-open) @end example If some brace syntactic symbol is not in @code{c-hanging-brace-alist}, its entry is taken by default as @code{(before after)}---insert a newline both before and after the brace. In place of a ``before/after'' list you can specify a function in this alist---this is useful when the auto newlines depend on the code around the brace. @defopt c-hanging-braces-alist @vindex hanging-braces-alist (c-) This variable is an association list which maps syntactic symbols to lists of places to insert a newline. @xref{Association Lists,,,@lispref{}, @lispreftitle{}}. The key of each element is the syntactic symbol, the associated value is either @code{nil}, a list, or a function. @table @asis @item The Key - the syntactic symbol The syntactic symbols that are useful as keys in this list are @code{brace-list-intro}, @code{statement-cont}, @code{inexpr-class-open}, @code{inexpr-class-close}, and all the @code{*-open} and @code{*-close} symbols. @xref{Syntactic Symbols}, for a more detailed description of these syntactic symbols, except for @code{inexpr-class-open} and @code{inexpr-class-close}, which aren't actual syntactic symbols. Elements with any other value as a key get ignored. The braces of anonymous inner classes in Java are given the special symbols @code{inexpr-class-open} and @code{inexpr-class-close}, so that they can be distinguished from the braces of normal classes@footnote{The braces of anonymous classes produce a combination of @code{inexpr-class}, and @code{class-open} or @code{class-close} in normal indentation analysis.}. Note that the aggregate constructs in Pike mode, @samp{(@{}, @samp{@})}, @samp{([}, @samp{])}, and @samp{(<}, @samp{>)}, do not count as brace lists in this regard, even though they do for normal indentation purposes. It's currently not possible to set automatic newlines on these constructs. @item The associated value - the ``ACTION'' list or function The value associated with each syntactic symbol in this association list is called an @var{action}, which can be either a list or a function which returns a list. @xref{Custom Braces}, for how to use a function as a brace hanging @var{action}. The list @var{action} (or the list returned by @var{action} when it's a function) contains some combination of the 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 hangs on the right side of the line, as in: @example // here, open braces always `hang' void spam( int i ) @{ if( i == 7 ) @{ dosomething(i); @} @} @end example When the list contains both @code{after} and @code{before}, the braces will appear on a line by themselves, as shown by the close braces in the above example. The list can also be empty, in which case newlines are added neither before nor after the brace. @end table If a syntactic symbol is missing entirely from @code{c-hanging-braces-alist}, it's treated in the same way as an @var{action} with a list containing @code{before} and @code{after}, so that braces by default end up on their own line. For example, the default value of @code{c-hanging-braces-alist} is: @example ((brace-list-open) (brace-entry-open) (statement-cont) (substatement-open after) (block-close . c-snug-do-while) (extern-lang-open after) (namespace-open after) (module-open after) (composition-open after) (inexpr-class-open after) (inexpr-class-close before)) @end example @noindent which says that @code{brace-list-open}, @code{brace-entry-open} and @code{statement-cont}@footnote{Brace lists inside statements, such as initializers for static array variables inside functions in C, are recognized as @code{statement-cont}. All normal substatement blocks are recognized with other symbols.} braces should both hang on the right side and allow subsequent text to follow on the same line as the brace. Also, @code{substatement-open}, @code{extern-lang-open}, and @code{inexpr-class-open} braces should hang on the right side, but subsequent text should follow on the next line. The opposite holds for @code{inexpr-class-close} braces; they won't hang, but the following text continues on the same line. Here, in the @code{block-close} entry, you also see an example of using a function as an @var{action}. In all other cases, braces are put on a line by themselves. @end defopt @menu * Custom Braces:: @end menu @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node Custom Braces, , Hanging Braces, Hanging Braces @comment node-name, next, previous, up @subsection Custom Brace Hanging @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @vindex c-hanging-braces-alist @vindex hanging-braces-alist (c-) @cindex action functions Syntactic symbols aren't the only place where you can customize @ccmode{} with the lisp equivalent of callback functions. Remember that @var{action}s are usually a list containing some combination of the symbols @code{before} and @code{after} (@pxref{Hanging Braces}). For more flexibility, you can instead specify brace ``hanginess'' by giving a syntactic symbol an @dfn{action function} in @code{c-hanging-braces-alist}; this function determines the ``hanginess'' of a brace, usually by looking at the code near it. @cindex customization, brace hanging An action function is called with two arguments: the syntactic symbol for the brace (e.g. @code{substatement-open}), and the buffer position where the brace has been inserted. Point is undefined on entry to an action function, but the function must preserve it (e.g. by using @code{save-excursion}). The return value should be a list containing some combination of @code{before} and @code{after}, including neither of them (i.e. @code{nil}). @defvar c-syntactic-context @vindex syntactic-context (c-) During the call to the indentation or brace hanging @var{action} function, this variable is bound to the full syntactic analysis list. This might be, for example, @samp{((block-close 73))}. Don't ever give @code{c-syntactic-context} a value yourself---this would disrupt the proper functioning of @ccmode{}. This variable is also bound in three other circumstances: (i)@w{ }when calling a c-hanging-semi&comma-criteria function (@pxref{Hanging Semicolons and Commas}); (ii)@w{ }when calling a line-up function (@pxref{Custom Line-Up}); (iii)@w{ }when calling a c-special-indent-hook function (@pxref{Other Indentation}). @end defvar As an example, @ccmode{} itself uses this feature to dynamically determine the hanginess of braces which close ``do-while'' constructs: @example void do_list( int count, char** atleast_one_string ) @{ int i=0; do @{ handle_string( atleast_one_string[i] ); i++; @} while( i < count ); @} @end example @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 @code{while} clause to follow the closing brace. To do this, we associate the @code{block-close} symbol with the @var{action} function @code{c-snug-do-while}: @example (defun c-snug-do-while (syntax pos) "Dynamically calculate brace hanginess for do-while statements." (save-excursion (let (langelem) (if (and (eq syntax 'block-close) (setq langelem (assq 'block-close c-syntactic-context)) (progn (goto-char (cdr langelem)) (if (= (following-char) ?@{) (forward-sexp -1)) (looking-at "\\[^_]"))) '(before) '(before after))))) @end example @findex c-snug-do-while @findex snug-do-while (c-) This function simply looks to see if the brace closes a ``do-while'' clause and if so, returns the list @samp{(before)} indicating that a newline should be inserted before the brace, but not after it. In all other cases, it returns the list @samp{(before after)} so that the brace appears on a line by itself. @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node Hanging Colons, Hanging Semicolons and Commas, Hanging Braces, Custom Auto-newlines @comment node-name, next, previous, up @section Hanging Colons @cindex hanging colons @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @cindex customization, colon hanging @vindex c-hanging-colons-alist @vindex hanging-colons-alist (c-) Using a mechanism similar to brace hanging (@pxref{Hanging Braces}), colons can also be made to hang using the style variable @code{c-hanging-colons-alist} - When a colon is typed, @ccmode determines its syntactic context, looks this up in the alist @code{c-changing-colons-alist} and inserts up to two newlines accordingly. Here, however, If @ccmode fails to find an entry for a syntactic symbol in the alist, no newlines are inserted around the newly typed colon. @defopt c-hanging-colons-alist @vindex hanging-colons-alist (c-) @table @asis @item The Key - the syntactic symbol The syntactic symbols appropriate as keys in this association list are: @code{case-label}, @code{label}, @code{access-label}, @code{member-init-intro}, and @code{inher-intro}. @xref{Syntactic Symbols}. Elements with any other value as a key get ignored. @item The associate value - the ``ACTION'' list The @var{action} here is simply a list containing a combination of the symbols @code{before} and @code{after}. Unlike in @code{c-hanging-braces-alist}, functions as @var{actions} are not supported - there doesn't seem to be any need for them. @end table @end defopt 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 @ccmode{}. @xref{Clean-ups}, for details. @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @node Hanging Semicolons and Commas, , Hanging Colons, Custom Auto-newlines @comment node-name, next, previous, up @section Hanging Semicolons and Commas @cindex hanging semicolons @cindex hanging commas @cindex customization, semicolon newlines @cindex customization, comma newlines @comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @defopt c-hanging-semi&comma-criteria @vindex hanging-semi&comma-criteria (c-) This style variable takes a list of functions; these get called when you type a semicolon or comma. The functions are called in order without arguments. When these functions are entered, point is just after the newly inserted @samp{;} or @samp{,} and they must preserve point (e.g., by using @code{save-excursion}). During the call, the variable @code{c-syntactic-context} is bound to the syntactic context of the current line@footnote{This was first introduced in @ccmode{} 5.31.} @pxref{Custom Braces}. These functions don't insert newlines themselves, rather they direct @ccmode{} whether or not to do so. They should return one of the following values: @table @code @item t A newline is to be inserted after the @samp{;} or @samp{,}, and no more functions from the list are to be called. @item stop No more functions from the list are to be called, and no newline is to be inserted. @item nil No determination has been made, and the next function in the list is to be called. @end