Go to the first, previous, next, last section, table of contents.


Commands

Indentation Commands

Various commands are provided which allow you to conveniently re-indent C constructs. There are several things to note about these indentation commands. First, when you change your programming style, either interactively or through some other means, your file does not automatically get re-indented. When you change style parameters, you will typically need to reformat the line, expression, or buffer to see the effects of your changes.

Second, changing some variables have no effect on existing code, even when you do re-indent. For example, the c-hanging-* variables and c-cleanup-list only affect new code as it is typed in on-the-fly, so changing c-hanging-braces-alist and re-indenting the buffer will not adjust placement of braces already in the file.

Third, re-indenting large portions of code is currently rather inefficient. Improvements have been made since previous releases of @ccmode{}, and much more radical improvements are planned, but for now you need to be aware of this (13). Some provision has been made to at least inform you as to the progress of the re-indentation. The variable c-progress-interval controls how often a progress message is displayed. Set this variable to nil to inhibit progress messages.

Also, except as noted below, re-indentation is always driven by the same mechanisms that control on-the-fly indentation of code. See section New Indentation Engine for details.

To indent a single line of code, use TAB (c-indent-command). The behavior of this command is controlled by the variable c-tab-always-indent. When this variable is t, TAB always just indents the current line. When nil, the line is indented only if point is at the left margin, or on or before the first non-whitespace character on the line, otherwise something else happens(14). If the value of c-tab-always-indent is something other than t or nil (e.g. 'other), then a real tab character(15) is inserted only when point is inside a literal (see section Auto-newline insertion), otherwise the line is indented.

To indent an entire balanced brace or parenthesis expression, use M-C-q (c-indent-exp). Note that point should be on the opening brace or parenthesis of the expression you want to indent.

Another very convenient keystroke is C-c C-q (c-indent-defun) when re-indents the entire top-level function or class definition that encompasses point. It leaves point at the same position within the buffer.

To indent any arbitrary region of code, use M-C-\ (indent-region). This is a standard Emacs command, specially tailored for C code in a @ccmode{} buffer. Note that of course, point and mark must delineate the region you want to indent.

While not strictly an indentation function, M-C-h (c-mark-function) is useful for marking the current top-level function or class definition as the current region.

Other Commands

@ccmode{} contains other useful command for moving around in C code.

C-c C-u (c-up-conditional)
Move point back to the containing preprocessor conditional, leaving the mark behind. A prefix argument acts as a repeat count. With a negative argument, move point forward to the end of the containing preprocessor conditional. When going backwards, #elif is treated like #else followed by #if. When going forwards, #elif is ignored.
C-c C-p (c-backward-conditional)
Move point back over a preprocessor conditional, leaving the mark behind. A prefix argument acts as a repeat count. With a negative argument, move forward.
C-c C-n (c-forward-conditional)
Move point forward across a preprocessor conditional, leaving the mark behind. A prefix argument acts as a repeat count. With a negative argument, move backward.
M-a (c-beginning-of-statement)
Move point to the beginning of the innermost C statement. If point is already at the beginning of a statement, it moves to the beginning of the preceding statement. With prefix argument n, move back n - 1 statements. If point is within a string or comment, or at the end of a comment, this command moves by sentences instead of statements. When called from a program, this function takes three optional arguments: the numeric prefix argument, a buffer position limit (don't move back before that place), and a flag to indicate whether movement should be by statements (if t) or sentence (if non-nil).
M-e (c-end-of-statement)
Move point to the end of the innermost C statement. If point is at the end of a statement, move to the end of the next statement. With prefix argument n, move forward n - 1 statements. If point is within a string or comment, or at the end of a comment, this command moves by sentences instead of statements. When called from a program, this function takes three optional arguments: the numeric prefix argument, a buffer position limit (don't move back before that place), and a flag to indicate whether movement should be by statements (if t) or sentence (if non-nil).
M-x c-forward-into-nomenclature
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. `SymbolsWithMixedCaseAndNoUnderlines'. This command moves point forward to next capitalized word. With prefix argument n, move n times.
M-x c-backward-into-nomenclature
Move point backward to beginning of the next capitalized word. With prefix argument n, move n times. If n is negative, move forward.
C-c : (c-scope-operator)
In C++, it is also sometimes desirable to insert the double-colon scope operator without performing the electric behavior of colon insertion. C-c : does just this.
M-q (fill-paragraph)
The command is used to fill a block style (C) or line style (C++) comment, in much the same way that text in the various text modes can be filled(16). You should never attempt to fill non-comment code sections; you'll end up with garbage! Two variables control how C style block comments are filled, specifically how the comment start and end delimiters are handled. The variable c-hanging-comment-starter-p controls whether comment start delimiters which appear on a line by themselves, end up on a line by themselves after the fill. When the value is nil, the comment starter will remain on its own line(17). Otherwise, text on the next line will be put on the same line as the comment starter. This is called hanging because the following text hangs on the line with the comment starter(18) The variable c-hanging-comment-ender-p controls the analogous behavior for the block comment end delimiter. When the value is nil, the comment ender will remain on its own line after the file(19). Otherwise, the comment end delimiter will be placed at the end of the previous line.


Go to the first, previous, next, last section, table of contents.