Next: Auto-newlines, Previous: Minor Modes, Up: Commands [Index]
Most punctuation keys provide 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 ‘}’. A few
keywords, such as else
, also trigger electric action.
You can inhibit the electric behavior described here by disabling electric minor mode (see 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
c-syntactic-indentation
has a non-nil
value (which it
does by default).
These keys and keywords are:
Pound (bound to 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 c-electric-pound-behavior
is consulted for the electric behavior. This variable takes a list
value, although the only element currently defined is alignleft
,
which tells this command to force the ‘#’ character into column
zero. This is useful for entering preprocessor macro definitions.
Pound is not electric in AWK buffers, where ‘#’ starts a comment,
and is bound to self-insert-command
like any typical printable
character.
A star (bound to c-electric-star
) or a slash
(c-electric-slash
) causes reindentation when you type it as the
second component of a C style block comment opener (‘/*’) or a
C++ line comment opener (‘//’) 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 CC Mode 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. See Clean-ups.
In AWK mode, ‘*’ and ‘/’ do not delimit comments and are not electric.
A less-than or greater-than sign (bound to c-electric-lt-gt
) is
electric in two circumstances: when it is an angle bracket in a C++
‘template’ declaration (and similar constructs in other
languages) and when it is the second of two < or >
characters in a C++ style stream operator. In either case, the line
is reindented. Angle brackets in C ‘#include’ directives are not
electric.
The normal parenthesis characters ‘(’ and ‘)’ (bound to
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 CC Mode to insert a space automatically between a function name and the ‘(’ you’ve just typed, and to remove it automatically after typing ‘)’, should the argument list be empty. You don’t need to have electric minor mode enabled to get these actions. See Clean-ups.
Typing a brace (bound to c-electric-brace
) reindents the
current line. Also, one or more newlines might be inserted if
auto-newline minor mode is enabled. See Auto-newlines.
Additionally, you can configure CC Mode to compact excess whitespace
inserted by auto-newline mode in certain circumstances.
See Clean-ups.
Typing a colon (bound to c-electric-colon
) reindents the
current line. Additionally, one or more newlines might be inserted if
auto-newline minor mode is enabled. See 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. See Clean-ups.
If you prefer, you can insert ‘::’ in a single operation, avoiding all these spurious reindentations, newlines, and clean-ups. See Other Commands.
Typing a semicolon or comma (bound to c-electric-semi&comma
)
reindents the current line. Also, a newline might be inserted if
auto-newline minor mode is enabled. See Auto-newlines.
Additionally, you can configure CC Mode so that when auto-newline
has inserted whitespace after a ‘}’, it will be removed again
when you type a semicolon or comma just after it. See Clean-ups.
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:
else
, while
, catch
(only in C++ and Java) and
finally
(only in Java).
An example:
for (i = 0; i < 17; i++) if (a[i]) res += a[i]->offset; else
Here, the else
should be indented like the preceding if
,
since it continues that statement. CC Mode will automatically
reindent it after the 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 if
.
CC Mode uses Abbrev mode (see Abbrevs in GNU Emacs Manual) 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.
Next: Auto-newlines, Previous: Minor Modes, Up: Commands [Index]