To show how some of these styles work, let's start with the C function example from earlier in this chapter:
int times (x, y)
int x, y;
{
int i;
int result = 0;
for (i = 0; i < x; i++)
{
result += y;
}
}
If you define a region around this code and you type C-M-\ (for indent-region), Emacs reformats the code in the default style like this:
int times (x, y)
int x, y;
{
int i;
int result = 0;
for (i = 0; i < x; i++)
{
result += y;
}
}
If you type C-c . (for c-set-style), enter k&r, and then repeat the reformatting, the code looks like this:
int times (x, y)
int x, y;
{
int i;
int result = 0;
for (i = 0; i < x; i++)
{
result += y;
}
}
Or, if you want to switch to GNU-style indentation, choose the style gnu and reformat. The result is:
int times (x, y)
int x, y;
{
int i;
int result = 0;
for (i = 0; i < x; i++)
{
result += y;
}
}
Once you decide on a coding style, you can set it up permanently by putting a line in your .emacs file that looks like this:
(add-hook 'c-mode-hook
'(lambda ( )
(c-set-style "stylename")))
Unfortunately, we'll have to wait until Chapter 11 to understand exactly what this code does. For now, make sure that you insert a single quote (') before the (lambda in the second line.
Each coding style contains subtleties that makes it nontrivial for Emacs to implement. Older versions of Emacs did this by defining several variables that controlled various indentation levels; these were not easy to work with and, frankly, did not really cover 100 percent of the nuances of each style. The current version of C mode, in contrast, uses a considerably larger set of variables—too large, in fact, for anyone other than hardy Emacs Lisp hackers to deal with.
Therefore, C mode keeps track of groups of these variables and their values under named styles. One huge variable, called c-style-alist, contains all of the styles and their associated information. You can customize this beast either by changing values of variables within existing styles or by adding a style of your own. For further details, look in the file cc-mode.el in your system's Emacs Lisp directory (see Chapter 11).
9.3.3 Additional C and C++ Mode Features
C mode contains a number of other useful features, ranging from the generally useful to the arcanely obscure. Perhaps the most interesting of these are two ways of adding additional electric functionality to certain keystrokes, called auto-newline and hungry-delete-key.[64]
When auto-newline is enabled, it causes Emacs to add a newline character and indent the new line properly whenever you type a semicolon (;), curly brace ({ or }), or, at certain times, comma (,) or colon (:). These features can save you some time and help you format your code in a consistent style.
Auto-newline is off by default. To turn it on, type C-c C-a for c-toggle-auto-state. (Repeat the same command to turn it off again.) You will see the (C) in the mode line change to (C/a) as an indication. As an example of how it works, try typing in the code for our times( ) function. Type the first two lines up to the y on the second line:
int times (x, y)
int x, y_
Now press the semicolon; notice that Emacs inserts a newline and brings you down to the next line:
int times (x, y)
int x, y;
_
Type the opening curly brace, and it happens again:
int times (x, y)
int x, y;
{
_
Of course, the number of spaces Emacs indents after you type the { depends on the indentation style you are using.
The other optional electric feature, hungry-delete-key, is also off by default. To toggle it on, type C-c C-d (for c-toggle-hungry-state). You will see the (C) on the mode line change to (C/h), or if you have auto-newline turned on, from (C/a) to (C/ah).