10 Indentation Engine Basics

This chapter will briefly cover how CC Mode indents lines of code. It is helpful to understand the indentation model being used so that you will know how to customize CC Mode for your personal coding style. All the details are in Customizing Indentation.

CC Mode has an indentation engine that provides a flexible and general mechanism for customizing indentation. When CC Mode indents a line of code, it separates its calculations into two steps:

  1. It analyzes the line to determine its syntactic symbol(s) (the kind of language construct it’s looking at) and its anchor position (the position earlier in the file that CC Mode will indent the line relative to). The anchor position might be the location of an opening brace in the previous line, for example. See Syntactic Analysis.
  2. It looks up the syntactic symbol(s) in the configuration to get the corresponding offset(s). The symbol +, which means “indent this line one more level” is a typical offset. CC Mode then applies these offset(s) to the anchor position, giving the indentation for the line. The different sorts of offsets are described in c-offsets-alist.

In exceptional circumstances, the syntax directed indentation described here may be a nuisance rather than a help. You can disable it by setting c-syntactic-indentation to nil. (To set the variable interactively, Minor Modes).

User Option: c-syntactic-indentation

When this is non-nil (which it is by default), the indentation of code is done according to its syntactic structure. When it’s nil, every line is just indented to the same level as the previous one, and TAB (c-indent-command) adjusts the indentation in steps of c-basic-offset. The current style (see Configuration Basics) then has no effect on indentation, nor do any of the variables associated with indentation, not even c-special-indent-hook.