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

1.8 Indentation

One issue in the formatting of code is how far each line should be indented from the left margin. When the beginning of a statement such as if or for is encountered, the indentation level is increased by the value specified by the ‘-i’ option. For example, use ‘-i8’ to specify an eight character indentation for each level. When a statement is broken across two lines, the second line is indented by a number of additional spaces specified by the ‘-ci’ option. ‘-ci’ defaults to 0. However, if the ‘-lp’ option is specified, and a line has a left parenthesis which is not closed on that line, then continuation lines will be lined up to start at the character position just after the left parenthesis. This processing also applies to ‘[’ and applies to ‘{’ when it occurs in initialization lists. For example, a piece of continued code might look like this with ‘-nlp -ci3’ in effect:

 
  p1 = first_procedure (second_procedure (p2, p3),
     third_procedure (p4, p5));

With ‘-lp’ in effect the code looks somewhat clearer:

 
  p1 = first_procedure (second_procedure (p2, p3),
                        third_procedure (p4, p5));

When a statement is broken in between two or more paren pairs (...), each extra pair causes the indentation level extra indentation:

 
if ((((i < 2 &&
        k > 0) || p == 0) &&
    q == 1) ||
  n = 0)

The option ‘-ipN’ can be used to set the extra offset per paren. For instance, ‘-ip0’ would format the above as:

 
if ((((i < 2 &&
  k > 0) || p == 0) &&
  q == 1) ||
  n = 0)

indent assumes that tabs are placed at regular intervals of both input and output character streams. These intervals are by default 8 columns wide, but (as of version 1.2) may be changed by the ‘-ts’ option. Tabs are treated as the equivalent number of spaces.

The indentation of type declarations in old-style function definitions is controlled by the ‘-ip’ parameter. This is a numeric parameter specifying how many spaces to indent type declarations. For example, the default ‘-ip5’ makes definitions look like this:

 
char *
create_world (x, y, scale)
     int x;
     int y;
     float scale;
{
  . . .
}

For compatibility with other versions of indent, the option ‘-nip’ is provided, which is equivalent to ‘-ip0’.

ANSI C allows white space to be placed on preprocessor command lines between the character ‘#’ and the command name. By default, indent removes this space, but specifying the ‘-lps’ option directs indent to leave this space unmodified. The option ‘-ppi’ overrides ‘-nlps’ and ‘-lps’.

This option can be used to request that preprocessor conditional statements can be indented by to given number of spaces, for example with the option ‘-ppi 3

 
#if X
#if Y
#define Z 1
#else
#define Z 0
#endif
#endif

becomes

 
#if X
#   if Y
#      define Z 1
#   else
#      define Z 0
#   endif
#endif

This option sets the offset at which a label (except case labels) will be positioned. If it is set to zero or a positive number, this indicates how far from the left margin to indent a label. If it is set to a negative number, this indicates how far back from the current indent level to place the label. The default setting is -2 which matches the behaviour of earlier versions of indent. Note that this parameter does not affect the placing of case labels; see the ‘-cli’ parameter for that. For example with the option ‘-il 1

 
function()
{
    if (do_stuff1() == ERROR)
        goto cleanup1;

    if (do_stuff2() == ERROR)
        goto cleanup2;

    return SUCCESS;

  cleanup2:
    do_cleanup2();

  cleanup1:
    do_cleanup1();

    return ERROR;
}

becomes

 
function()
{
    if (do_stuff1() == ERROR)
        goto cleanup1;

    if (do_stuff2() == ERROR)
        goto cleanup2;

    return SUCCESS;

 cleanup2:
    do_cleanup2();

 cleanup1:
    do_cleanup1();

    return ERROR;
}

[ < ] [ > ]   [ << ] [ Up ] [ >> ]

This document was generated by david on December, 15 2008 using texi2html 1.78.