| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
CC Mode contains two minor-mode-like features that you should find useful while you enter new C code. The first is called auto-newline mode, and the second is called hungry-delete mode. These minor modes can be toggled on and off independently, and CC Mode can be configured so that it starts up with any combination of these minor modes. By default, both of these minor modes are turned off.
The state of the minor modes is always reflected in the minor mode list on the modeline of the CC Mode buffer. When auto-newline mode is enabled, you will see `C/a' on the mode line (7). When hungry delete mode is enabled you would see `C/h' and when both modes are enabled, you'd see `C/ah'.
CC Mode provides keybindings which allow you to toggle the minor
modes on the fly while editing code. To toggle just the auto-newline
state, hit C-c C-a (c-toggle-auto-state). When you do
this, you should see the `a' indicator either appear or disappear
on the modeline. Similarly, to toggle just the hungry-delete state, use
C-c C-d (c-toggle-hungry-state), and to toggle both states,
use C-c C-t (c-toggle-auto-hungry-state).
To set up the auto-newline and hungry-delete states to your preferred
values, you would need to add some lisp to your `.emacs' file that
called one of the c-toggle-*-state functions directly. When
called programmatically, each function takes a numeric value, where
a positive number enables the minor mode, a negative number disables the
mode, and zero toggles the current state of the mode.
So for example, if you wanted to enable both auto-newline and hungry-delete for all your C file editing, you could add the following to your `.emacs' file:
(add-hook 'c-mode-common-hook (lambda () (c-toggle-auto-hungry-state 1))) |
4.1 Auto-newline Insertion 4.2 Hungry-deletion of Whitespace
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Auto-newline minor mode works by enabling certain electric commands. Electric commands are typically bound to special characters such as the left and right braces, colons, semi-colons, etc., which when typed, perform some magic formatting in addition to inserting the typed character. As a general rule, electric commands are only electric when the following conditions apply:
4.1.1 Hanging Braces 4.1.2 Hanging Colons 4.1.3 Hanging Semi-colons and Commas 4.1.4 Other Electric Commands 4.1.5 Clean-ups
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
When you type either an open or close brace (i.e. { or }),
the electric command c-electric-brace gets run. This command has
two electric formatting behaviors. First, it will perform some
re-indentation of the line the brace was typed on, and second, it will
add various newlines before and/or after the typed brace.
Re-indentation occurs automatically whenever the electric behavior is
enabled. If the brace ends up on a line other than the one it was typed
on, then that line is also re-indented.
The default in auto-newline mode is to insert newlines both before and
after a brace, but that can be controlled by the
c-hanging-braces-alist style variable. This variable contains a
mapping between syntactic symbols related to braces, and a list of
places to insert a newline. The syntactic symbols that are useful for
this list are: class-open, class-close, defun-open,
defun-close, inline-open, inline-close,
brace-list-open, brace-list-close,
brace-list-intro, brace-entry-open, block-open,
block-close, substatement-open,
statement-case-open, extern-lang-open,
extern-lang-close, namespace-open, namespace-close,
inexpr-class-open, and inexpr-class-close(9). See section 8. Syntactic Symbols, for a more detailed
description of these syntactic symbols, except for
inexpr-class-open and inexpr-class-close, which aren't
actual syntactic symbols.
The braces of anonymous inner classes in Java are given the special
symbols inexpr-class-open and inexpr-class-close, so that
they can be distinguished from the braces of normal classes(10).
The value associated with each syntactic symbol in this association list is called an ACTION which can be either a function or a list. See section 7.5.2 Custom Brace and Colon Hanging, for a more detailed discussion of using a function as a brace hanging ACTION.
When the ACTION is a list, it can contain any combination of the
symbols before and after, directing CC Mode where to
put newlines in relationship to the brace being inserted. Thus, if the
list contains only the symbol after, then the brace is said to
hang on the right side of the line, as in:
// here, open braces always `hang'
void spam( int i ) {
if( i == 7 ) {
dosomething(i);
}
}
|
When the list contains both after and before, the braces
will appear on a line by themselves, as shown by the close braces in the
above example. The list can also be empty, in which case no newlines
are added either before or after the brace.
If a syntactic symbol is missing entirely from
c-hanging-braces-alist, it's treated in the same way as an
ACTION with a list containing before and after, so
that braces by default end up on their own line.
For example, the default value of c-hanging-braces-alist is:
((brace-list-open) (brace-entry-open) (substatement-open after) (block-close . c-snug-do-while) (extern-lang-open after) (inexpr-class-open after) (inexpr-class-close before)) |
which says that brace-list-open and
brace-entry-open braces should both hang on the right side, and
allow subsequent text to follow on the same line as the brace. Also,
substatement-open, extern-lang-open, and
inexpr-class-open braces should hang on the right side, but
subsequent text should follow on the next line. The opposite holds for
inexpr-class-close braces; they won't hang, but the following
text continues on the same line. Here, in the block-close entry,
you also see an example of using a function as an ACTION. In all
other cases, braces are put on a line by themselves.
A word of caution: it is not a good idea to hang top-level construct
introducing braces, such as class-open or defun-open.
Emacs makes an assumption that such braces will always appear in column
zero, hanging them can introduce performance problems.
See section 10. Performance Issues, for more information.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Using a mechanism similar to brace hanging (see section 4.1.1 Hanging Braces),
colons can also be made to hang using the style variable
c-hanging-colons-alist. The syntactic symbols appropriate for
this association list are: case-label, label,
access-label, member-init-intro, and inher-intro.
Note however that for c-hanging-colons-alist, ACTIONs as
functions are not supported. See also 7.5.2 Custom Brace and Colon Hanging for details.
In C++, double-colons are used as a scope operator but because these colons always appear right next to each other, newlines before and after them are controlled by a different mechanism, called clean-ups in CC Mode. See section 4.1.5 Clean-ups, for details.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Semicolons and commas are also electric in CC Mode, but since these characters do not correspond directly to syntactic symbols, a different mechanism is used to determine whether newlines should be automatically inserted after these characters. See section 7.5.3 Customizing Semi-colons and Commas, for details.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
A few other keys also provide electric behavior. For example
# (c-electric-pound) is electric when typed as
the first non-whitespace character on a line. 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
C preprocessor macro definitions.
Stars and slashes (i.e. * and /, c-electric-star and
c-electric-slash respectively) are also electric under
certain circumstances. If a star is inserted as the second character of
a C style block comment on a comment-only line, then the comment
delimiter is indented as defined by c-offsets-alist. A
comment-only line is defined as a line which contains only a comment, as
in:
void spam( int i )
{
// this is a comment-only line...
if( i == 7 ) // but this is not
{
dosomething(i);
}
}
|
Likewise, if a slash is inserted as the second slash in a C++ style line
comment (also only on a comment-only line), then the line is indented as
defined by c-offsets-alist.
Less-than and greater-than signs (c-electric-lt-gt) are also
electric, but only in C++ mode. Hitting the second of two < or
> keys re-indents the line if it is a C++ style stream operator.
The normal parenthesis characters `(' and `)' also reindent the current line if they are used in normal code. This is useful for getting the closing parenthesis of an argument list aligned automatically.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Clean-ups are mechanisms complementary to colon and brace hanging.
On the surface, it would seem that clean-ups overlap the functionality
provided by the c-hanging-*-alist variables. Clean-ups are
however used to adjust code "after-the-fact", i.e. to adjust the
whitespace in constructs after they are typed.
Most of the clean-ups are only applicable to counteract automatically inserted newlines, and will therefore only have any effect if the auto-newline minor mode is turned on. Others will work all the time.
You can configure CC Mode's clean-ups by setting the style variable
c-cleanup-list, which is a list of clean-up symbols. By default,
CC Mode cleans up only the scope-operator construct, which is
necessary for proper C++ support. Note that clean-ups are only
performed when the construct does not occur within a literal
(see section 4.1 Auto-newline Insertion), and when there is nothing but
whitespace appearing between the individual components of the construct.
These are the clean-ups that only are active in the auto-newline minor mode:
brace-else-brace -- Clean up `} else {' constructs by
placing the entire construct on a single line. Clean-up occurs when the
open brace after the `else' is typed. So for example, this:
void spam(int i)
{
if( i==7 )
{
dosomething();
}
else
{
|
void spam(int i)
{
if( i==7 ) {
dosomething();
} else {
|
brace-elseif-brace -- Similar to the brace-else-brace
clean-up, but this cleans up `} else if (...) {' constructs. For
example:
void spam(int i)
{
if( i==7 )
{
dosomething();
}
else if( i==3 )
{
|
void spam(int i)
{
if( i==7 ) {
dosomething();
} else if( i==3 )
{
|
void spam(int i)
{
if( i==7 ) {
dosomething();
} else if( i==3 ) {
|
brace-catch-brace -- Analogous to brace-elseif-brace, but
cleans up `} catch (...) {' in C++ and Java mode.
empty-defun-braces -- Clean up braces following a top-level
function or class definition that contains no body. Clean up occurs
when the closing brace is typed. Thus the following:
class Spam
{
}
|
class Spam
{}
|
defun-close-semi -- Clean up the terminating semi-colon on
top-level function or class definitions when they follow a close
brace. Clean up occurs when the semi-colon is typed.
So for example, the following:
class Spam
{
}
;
|
class Spam
{
};
|
list-close-comma -- Clean up commas following braces in array
and aggregate initializers. Clean up occurs when the comma is typed.
scope-operator -- Clean up double colons which may designate a
C++ scope operator split across multiple lines(11). Clean up occurs when
the second colon is typed. You will always want scope-operator
in the c-cleanup-list when you are editing C++ code.
The following clean-ups are always active when they occur on
c-cleanup-list, and are thus not affected by the auto-newline
minor mode:
space-before-funcall -- Insert a space between the function name
and the opening parenthesis of a function call. This produces function
calls in the style mandated by the GNU coding standards,
e.g. `signal (SIGINT, SIG_IGN)' and `abort ()'. Clean up
occurs when the opening parenthesis is typed.
compact-empty-funcall -- Clean up any space between the function
name and the opening parenthesis of a function call that have no
arguments. This is typically used together with
space-before-funcall if you prefer the GNU function call style
for functions with arguments but think it looks ugly when it's only an
empty parenthesis pair. I.e. you will get `signal (SIGINT,
SIG_IGN)', but `abort()'. Clean up occurs when the closing
parenthesis is typed.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Hungry deletion of whitespace, or as it more commonly called, hungry-delete mode, is a simple feature that some people find extremely useful. In fact, you might find yourself wanting hungry-delete in all your editing modes!
In a nutshell, when hungry-delete mode is enabled, hitting the Backspace key(12) will consume all preceding whitespace, including newlines and tabs. This can really cut down on the number of Backspace's you have to type if, for example you made a mistake on the preceding line.
By default, when you hit the Backspace key CC Mode runs the
command c-electric-backspace, which deletes text in the backwards
direction. When deleting a single character, or when Backspace is
hit in a literal (see section 4.1 Auto-newline Insertion), or when hungry-delete
mode is disabled, the function contained in the
c-backspace-function variable is called with one argument (the
number of characters to delete). This variable is set to
backward-delete-char-untabify by default.
The default behavior of the Delete key depends on the flavor of
Emacs you are using. By default in XEmacs 20.3 and beyond, the
Delete key is bound to c-electric-delete. You control the
direction that the Delete key deletes by setting the variable
delete-key-deletes-forward, a standard XEmacs variable. When
this variable is non-nil and hungry-delete mode is enabled,
c-electric-delete will consume all whitespace following
point. When delete-key-deletes-forward is nil, it deletes
all whitespace preceding point(13) When deleting a single character, or if
Delete is hit in a literal, or hungry-delete mode is disabled, the
function contained in c-delete-function is called with one
argument: the number of characters to delete. This variable is set to
delete-char by default.
In Emacs 19 or Emacs 20, both the Delete and Backspace keys
are bound to c-electric-backspace, however you can change this by
explicitly binding [delete](14).
XEmacsen older than 20.3 behave similar to Emacs 19 and Emacs 20.
| [ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |