Книга: Learning GNU Emacs, 3rd Edition

9.3.2 Customizing Code Indentation Style

9.3.2 Customizing Code Indentation Style

Coding style in C—or any programming language for that matter—is a very personal thing. C programmers learn from various books or by referring to various different pieces of other people's code; eventually they evolve a personal style that may or may not conform to those that they learned from.

C mode provides a rich set of features for customizing its indentation behavior that mirrors this way of learning the language. At the simplest level, you can choose a coding style by name. Then, if you're not satisfied, you can customize your chosen style or even create your own from scratch. The latter tasks, however, require a fair amount of Emacs Lisp programming knowledge (see Chapter 11) and perhaps a bit of bravery.

You can choose a named coding style with the command M-x c-set-style. This command prompts you for the name of the style you want. The easiest thing to do at this point is to type Tab, the completion character (see Chapter 14), which brings up a *Completions* window that lists all of the choices. Type one of them and press Enter to select it.

By default, Emacs comes loaded with the styles shown in Table 9-4.

Table 9-4. Built-in cc-mode indentation styles

Style Description
bsd Style used in code for BSD-derived versions of Unix.
cc-mode The default coding style, from which all others are derived.
ellemtel Style used in C++ documentation from Ellemtel Telecommunication Systems Laboratories in Sweden.
gnu Style used in C code for Emacs itself and other GNU-related programs.
java Style used in Java code (the default for Java mode).
k&r Style of the classic text on C, Kernighan and Ritchie's The C Programming Language.
linux Style used in C code that is part of the Linux kernel.
python Style used in python extensions.
stroustrup C++ coding style of the standard reference work, Bjarne Stroustrup's The C++ Programming Language.
user Customizations you make to .emacs or via Custom (see Chapter 10). All other styles inherit these customizations if you set them.
whitesmith Style used in Whitesmith Ltd.'s documentation for their C and C++ compilers.

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

Оглавление книги


Генерация: 1.260. Запросов К БД/Cache: 3 / 1
поделиться
Вверх Вниз