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.
@ccmode{} contains other useful command for moving around in C code.
C-c C-u (c-up-conditional)
#elif
is treated
like #else
followed by #if
. When going forwards,
#elif
is ignored.
C-c C-p (c-backward-conditional)
C-c C-n (c-forward-conditional)
M-a (c-beginning-of-statement)
t
) or sentence (if non-nil
).
M-e (c-end-of-statement)
t
) or sentence (if non-nil
).
M-x c-forward-into-nomenclature
M-x c-backward-into-nomenclature
C-c : (c-scope-operator)
M-q (fill-paragraph)
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.