www.delorie.com/gnu/docs/emacs/cc-mode_19.html   search  
 
Buy the book!


CC Mode Manual

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

7. Customizing Indentation

The style variable c-offsets-alist contains the mappings between syntactic symbols and the offsets to apply for those symbols. It's set at mode initialization from a style you may specify. Styles are groupings of syntactic symbol offsets and other style variable values. Most likely, you'll find that one of the pre-defined styles will suit your needs. See section 7.4 Styles, for an explanation of how to set up named styles.

Only syntactic symbols not already bound on c-offsets-alist will be set from styles. This means that any association you set on it, be it before or after mode initialization, will not be changed. The c-offsets-alist variable may therefore be used from e.g. the Customization interface(20) to easily change indentation offsets without having to bother about styles. Initially c-offsets-alist is empty, so that all syntactic symbols are set by the style system.

You can use the command C-c C-o (c-set-offset) as the way to set offsets, both interactively and from your mode hook(21).

The offset associated with any particular syntactic symbol can be any of an integer, a function or lambda expression, a variable name, a vector, a list, or one of the following symbols: +, -, ++, --, *, or /.

Those last special symbols describe an offset in multiples of the value of the style variable c-basic-offset. By defining a style's indentation in terms of this fundamental variable, you can change the amount of whitespace given to an indentation level while maintaining the same basic shape of your code. Here are the values that the special symbols correspond to:

+
c-basic-offset times 1
-
c-basic-offset times -1
++
c-basic-offset times 2
--
c-basic-offset times -2
*
c-basic-offset times 0.5
/
c-basic-offset times -0.5

When a function is used as offset, it's called an indentation function. Such functions are useful when more context than just the syntactic symbol is needed to get the desired indentation. See section 9. Indentation Functions, and 7.5.1 Custom Indentation Functions, for details about them.

If the offset is a vector, its first element sets the absolute indentation column, which will override any relative indentation.

The offset can also be a list, in which case it is evaluated recursively using the semantics described above. The first element of the list that returns a non-nil value succeeds and the evaluation stops. If none of the list elements return a non-nil value, then an offset of 0 (zero) is used(22).

So, for example, because most of the default offsets are defined in terms of +, -, and 0, if you like the general indentation style, but you use 4 spaces instead of 2 spaces per level, you can probably achieve your style just by changing c-basic-offset like so(23):
 
M-x set-variable RET
Set variable: c-basic-offset RET
Set c-basic-offset to value: 4 RET

This would change
 
int add( int val, int incr, int doit )
{
  if( doit )
    {
      return( val + incr );
    }
  return( val );
}

to
 
int add( int val, int incr, int doit )
{
    if( doit )
        {
            return( val + incr );
        }
    return( val );
}

To change indentation styles more radically, you will want to change the offsets associated with other syntactic symbols. First, I'll show you how to do that interactively, then I'll describe how to make changes to your `.emacs' file so that your changes are more permanent.

7.1 Interactive Customization  
7.2 Permanent Customization  
7.3 Hooks  
7.4 Styles  
7.5 Advanced Customizations  


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003