Go to the first, previous, next, last section, table of contents.


Step 2--Indentation Calculation

The C indentation mechanism calculates the indentation for the current line using the list of syntactic components, c-syntactic-context, derived from syntactic analysis. Each component is a cons cell that contains a syntactic symbol and may also contain a buffer position.

Each component contributes to the final total indentation of the line in two ways. First, the syntactic symbol identifies an element of c-offsets-alist, which is an association list mapping syntactic symbols into indentation offsets. Each syntactic symbol's offset adds to the total indentation. Second, if the component includes a buffer position, the column number of that position adds to the indentation. All these offsets and column numbers, added together, give the total indentation.

The following examples demonstrate the workings of the C indentation mechanism:

1: void swap (int& a, int& b)
2: {
3:   int tmp = a;
4:   a = b;
5:   b = tmp;
6: }

Suppose that point is on line 3 and you type TAB to reindent the line. As explained above (see section Step 1--Syntactic Analysis), the syntactic component list for that line is:

((defun-block-intro . 28))

In this case, the indentation calculation first looks up defun-block-intro in the c-offsets-alist alist. Suppose that it finds the integer 2; it adds this to the running total (initialized to zero), yielding a updated total indentation of 2 spaces.

The next step is to find the column number of buffer position 28. Since the brace at buffer position 28 is in column zero, this adds 0 to the running total. Since this line has only one syntactic component, the total indentation for the line is 2 spaces.

1: int add (int val, int incr, int doit)
2: {
3:   if (doit)
4:     {
5:       return(val + incr);
6:     }
7:   return(val);
8: }

If you type TAB on line 4, the same process is performed, but with different data. The syntactic component list for this line is:

((substatement-open . 43))

Here, the indentation calculation's first job is to look up the symbol substatement-open in c-offsets-alist. Let's assume that the offset for this symbol is 2. At this point the running total is 2 (0 + 2 = 2). Then it adds the column number of buffer position 43, which is the `i' in if on line 3. This character is in column 2 on that line. Adding this yields a total indentation of 4 spaces.

If a syntactic symbol in the analysis of a line does not appear in c-offsets-alist, it is ignored; if in addition the variable c-strict-syntax-p is non-nil, it is an error.


Go to the first, previous, next, last section, table of contents.