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

8. Commands

8.1 Indentation Commands  
8.2 Movement Commands  
8.3 Other Commands  

See also 5. Text Filling and Line Breaking and 6. Macro Handling, for commands concerning those bits.


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

8.1 Indentation Commands

The following list of commands reindent C constructs. Note that when you change your coding style, either interactively or through some other means, your file does not automatically get reindented. 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 reindenting, will not move the brace to a different line. For this, you're better off getting an external program like GNU indent, which will rearrange brace location, among other things.

Reindenting 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.

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.

C-M-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, 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.

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

M-; (indent-for-comment)
Insert a comment at the end of the current line, if none is there already. Then reindent the comment according to the variables c-indent-comment-alist, c-indent-comments-syntactically-p and comment-column. Then position the point after the comment starter. This is a standard Emacs command, but CC Mode enhances it a bit with two variables:

User Option: c-indent-comment-alist
This style variable allows you to control which column M-; indents the comment to, depending on the preceding code and the indentation of a similar comment on the preceding line, if there is any. 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 comment-column. See the documentation string for c-indent-comment-alist for a full description of the available line types and actions (use C-h v c-indent-comment-alist).

User Option: c-indent-comments-syntactically-p
Normally, when this variable is nil, M-; will indent comment-only lines according to 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 TAB for comment-only lines you can get that by setting c-indent-comments-syntactically-p to non-nil.

If c-indent-comments-syntactically-p is non-nil then c-indent-comment-alist won't be consulted at all for comment-only lines.

C-M-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:

User Option: c-tab-always-indent
This variable controls how TAB (c-indent-command) operates. When it is t, TAB always 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 some other value (not nil or t), then some whitespace is inserted only within strings and comments (literals), but the line is always reindented.

User Option: 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.

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

User Option: 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 how often (in seconds) progress messages are to be displayed.


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

8.2 Movement Commands

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

M-x c-beginning-of-defun
Move 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.

In AWK mode, a defun doesn't necessarily have braces at all. AWK Mode therefore has its own version of this function which is bound by default to C-M-a. You can thus chose freely which function to bind to C-M-a for the other modes without worrying about AWK buffers. See section 12.3 AWK Mode Defuns.

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 end-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.

In AWK Mode, a defun doesn't necessarily have braces at all. AWK Mode therefore has its own version of this function which is bound by default to C-M-e. You can thus chose freely which function to bind to C-M-e for the other modes without worrying about AWK buffers. 12.3 AWK Mode Defuns.

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 C-M-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 C-M-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] [ ? ]

8.3 Other Commands

Here are the various other commands that didn't fit anywhere else:

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.


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

This document was generated by XEmacs shared group account on December, 19 2009 using texi2html 1.65.