[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

6. Commands

6.1 Indentation Commands  
6.2 Movement Commands  
6.3 Other Commands  

See also 5. Text Filling and Line Breaking, for commands concerning that bit.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

6.1 Indentation Commands

The following list of commands re-indent C constructs. Note that when you change your coding style, either interactively or through some other means, your file does not automatically get re-indented. You will need to execute one of the following commands to see the effects of your changes.

Also, variables like c-hanging-* and c-cleanup-list only affect how on-the-fly code is formatted. Changing the "hanginess" of a brace and then re-indenting, will not move the brace to a different line. For this, you're better off getting an external program like GNU indent, which will re-arrange brace location, among other things.

Re-indenting large sections of code can take a long time. When CC Mode reindents a region of code, it is essentially equivalent to hitting TAB on every line of the region. Especially vulnerable is code generator output(19).

These commands are useful when indenting code:

TAB (c-indent-command)
Indents the current line. The actual behavior is controlled by several variables, described below. See c-tab-always-indent, c-insert-tab-function, and indent-tabs-mode. With a numeric argument, this command rigidly indents the region, preserving the relative indentation among the lines.

M-C-q (c-indent-exp)
Indent 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.

C-c C-q (c-indent-defun)
Indents the entire top-level function or class definition encompassing point. It leaves point unchanged. This function can't be used to re-indent a nested brace construct, such as a nested class or function, or a Java method. The top-level construct being re-indented must be complete, i.e. it must have both a beginning brace and an ending brace.

M-C-\ (indent-region)
Indents an arbitrary region of code. This is a standard Emacs command, tailored for C code in a CC Mode buffer. Note that of course, point and mark must delineate the region you want to indent.

M-C-h (c-mark-function)
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 c-indent-defun, this command operates on top-level constructs, and can't be used to mark say, a Java method.

These variables are also useful when indenting code:

c-tab-always-indent
This variable controls how TAB c-indent-command operates. When this variable is t, TAB always just indents the current line. When it is 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 some whitespace is inserted. If this variable is the symbol other, then some whitespace is inserted only within strings and comments (literals), an inside preprocessor directives, but the line is always reindented.

c-insert-tab-function
When "some whitespace" is inserted as described above, what actually happens is that the function stored in c-insert-tab-function is called. Normally, this just inserts a real tab character, or the equivalent number of spaces, depending on indent-tabs-mode. Some people, however, set c-insert-tab-function to tab-to-tab-stop so as to get hard tab stops when indenting.

indent-tabs-mode
This is a standard Emacs variable that controls how line indentation is composed. When this variable is non-nil, then tabs can be used in a line's indentation, otherwise only spaces can be used.

c-progress-interval
When indenting large regions of code, this variable controls how often a progress message is displayed. Set this variable to nil to inhibit the progress messages, or set it to an integer which is the interval in seconds that progress messages are displayed.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

6.2 Movement Commands

CC Mode contains some useful command for moving around in C code.

M-x c-beginning-of-defun
Moves point back to the least-enclosing brace. This function is analogous to the Emacs built-in command beginning-of-defun, except it eliminates the constraint that the top-level opening brace must be in column zero. See beginning-of-defun for more information.

Depending on the coding style being used, you might prefer c-beginning-of-defun to beginning-of-defun. If so, consider binding C-M-a to the former instead. For backwards compatibility reasons, the default binding remains in effect.

M-x c-end-of-defun
Moves point to the end of the current top-level definition. This function is analogous to the Emacs built-in command end-of-defun, except it eliminates the constraint that the top-level opening brace of the defun must be in column zero. See beginning-of-defun for more information.

Depending on the coding style being used, you might prefer c-end-of-defun to end-of-defun. If so, consider binding C-M-e to the former instead. For backwards compatibility reasons, the default binding remains in effect.

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.

`#elif' is treated like `#else' followed by `#if', so the function stops at them when going backward, but not when going forward.

M-x c-up-conditional-with-else
A variety of c-up-conditional that also stops at `#else' lines. Normally those lines are ignored.

M-x c-down-conditional
Move point forward into the next nested preprocessor conditional, leaving the mark behind. A prefix argument acts as a repeat count. With a negative argument, move point backward into the previous nested preprocessor conditional.

`#elif' is treated like `#else' followed by `#if', so the function stops at them when going forward, but not when going backward.

M-x c-down-conditional-with-else
A variety of c-down-conditional that also stops at `#else' lines. Normally those lines are 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, move to the beginning of the closest preceding statement, even if that means moving into a block (you can use M-C-b to move over a balanced block). With prefix argument n, move back n - 1 statements.

If point is within or next to a comment or a string which spans more than one line, this command moves by sentences instead of statements.

When called from a program, this function takes 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.

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, even if it's inside a nested block (use M-C-f to move to the other side of the block). With prefix argument n, move forward n - 1 statements.

If point is within or next to a comment or a string which spans more than one line, this command moves by sentences instead of statements.

When called from a program, this function takes 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.

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.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

6.3 Other Commands

CC Mode contains a few other useful commands:

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.

C-c C-\ (c-backslash-region)
This function is handy when editing macros split over several lines by ending each line with a backslash. It inserts and aligns, or deletes these end-of-line backslashes in the current region.

With no prefix argument, it inserts any missing backslashes and aligns them to the column specified by the c-backslash-column style variable. 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.


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated on March, 16 2003 using texi2html