Next: , Previous: , Up: Indent Program   [Contents][Index]

1.8 Indentation

The most basic, and most controversial issues with regard to code formatting is precisely how indentation should be acoomplished. Fortunately, indent supports several different styles of identation. The default is to use tabs for indentation, which is specified by the -ut option. Assuming the default tab size of 8, the code would look like this:

int a(int b)
{
        return b;
|------|
 1 tab
}

For those that prefer spaces to tabs, indent provides the -nut option. The same code would look like this:

int a(int b)
{
        return b;
|------|
8 spaces
}

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

By default, indent will use tabs to indent as far as possible, and then pad with spaces until the desired position is reached. However, with the -as option, spaces will be used for alignment beyond the current indentation level. By default, assuming -lp is enabled, the code would be indented like so (‘t’ represents tabs, ‘s’ represents spaces):

unsigned long really_long_proc_name(unsigned long x, unsigned long y,
                                    int a)
|------||-------||------||-------|__
   t        t       t       t     ss
{
        p1 = first_procedure (second_procedure (p2, p3),
                              third_procedure (p4, p5));
|------||------||------|_____
   t       t       t    sssss
}

This is fine, if you assume that whoever is reading the code will honor your assumption of 8-space tabs. If the reader was using 4-space tabs, it would look like this:

unsigned long really_long_proc_name(unsigned long x, unsigned long y,
                      int a)
|---||---||---||---|__
  t    t    t    t  ss
{
        p1 = first_procedure (second_procedure (p2, p3),
                     third_procedure (p4, p5));
|---||---||---|______
  t    t    t  ssssss
}

The -as option fixes this so that the code will appear consistent regardless of what tab size the user users to read the code. This looks like:

unsigned long really_long_proc_name(unsigned long x, unsigned long y,
                                    int a)
____________________________________
ssssssssssssssssssssssssssssssssssss
{
        p1 = first_procedure (second_procedure (p2, p3),
                              third_procedure (p4, p5));
|------|______________________
   t    ssssssssssssssssssssss
}

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;
}

Next: , Previous: , Up: Indent Program   [Contents][Index]