CC MODE Version 5 Documentation. Node: Customizing Indentation

PREV Commands UP Top NEXT Syntactic Symbols

Chapter 6: Customizing Indentation

The variable c-offsets-alist contains the mappings between syntactic symbols and the offsets to apply for those symbols. You should never modify this variable directly though. Use the function c-set-offset instead (see below for details).

The c-offsets-alist variable is where you customize all your indentations. You simply need to decide what additional offset you want to add for every syntactic symbol. 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. Also, you can set up styles of indentatio. Most likely, you'll find one of the pre-defined styles will suit your needs, but if not, this section will describe how to set up basic editing configurations. See Styles for an explanation of how to set up named styles.

As mentioned previously, the variable c-offsets-alist is an association list of syntactic symbols and the offsets to be applied for those symbols. In fact, these offset values can be any of an integer, a function or lambda expression, a variable name, or one of the following symbols: +, -, ++, --, *, or /. These symbols describe offset in multiples of the value of the 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 leaving the same relationship between levels. 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

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 (in your `.emacs' file):


(setq c-basic-offset 4)

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 value associated with the syntactic symbols in the c-offsets-alist variable. 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.

  • Interactive Customization
  • Permanent Customization
  • Styles
  • Advanced Customizations
  • PREV Commands UP Top NEXT Syntactic Symbols