This section explains the structure and semantics of the style
variable c-offset-alist, the principal variable for configuring
indentation. Details of how to set it up, and its relationship to
CC Mode's style system are given in Style Variables.
This is an alist which associates an offset with each syntactic symbol. This offset is a rule specifying how to indent a line whose syntactic context matches the symbol. See Syntactic Analysis.
Note that the buffer-local binding of this alist in a CC Mode buffer contains an entry for every syntactic symbol. Its global binding and its settings within style specifications usually contain only a few entries. See Style Variables.
The offset specification associated with any particular syntactic symbol can be an integer, a variable name, a vector, a function or lambda expression, a list, or one of the following special symbols:
+,-,++,--,*, or/. The meanings of these values are described in detail below.Here is an example fragment of a
c-offsets-alist, showing some of these kinds of offsets:((statement . 0) (substatement . +) (cpp-macro . [0]) (topmost-intro-cont . c-lineup-topmost-intro-cont) (statement-block-intro . (add c-lineup-whitesmith-in-block c-indent-multi-line-block)) ...
)
This command changes the entry for a syntactic symbol in the current binding of
c-offsets-alist, or it inserts a new entry if there isn't already one for that syntactic symbol.You can use
c-set-offsetsinteractively within a CC Mode buffer to make experimental changes to your indentation settings. C-c C-o prompts you for the syntactic symbol to change (defaulting to that of the current line) and the new offset (defaulting to the current offset).
c-set-offsetstakes two arguments when used programmatically: symbol, the syntactic element symbol to change and offset, the new offset for that syntactic element. You can call the command in your .emacs to change the global binding ofc-offsets-alist(see Style Variables); you can use it in a hook function to make changes from the current style. CC Mode itself uses this function when initializing styles.
The “offset specifications” in c-offsets-alist can be any of
the following:
+ than an integer (apart
from zero).
+, -, ++, --, *, or /c-basic-offset:
By defining a style's indentation in terms of c-basic-offset,
you can change the amount of whitespace given to an indentation level
while maintaining the same basic shape of your code. 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
nil value.
nil values are always ignored when the offsets are combined.
The first element of the list specifies the method of combining the
non-nil offsets from the remaining elements:
firstnil. Subsequent
elements of the list don't get evaluated.
minmaxaddAs a compatibility measure, if the first element is none of the above
then it too will be taken as an offset specification and the whole list
will be combined according to the method first.
If an offset specification evaluates to nil, then a relative
offset of 0 (zero) is used2.
[1] The syntactic context ((defun-block-intro 2724) (comment-intro)) would likely have two relative offsets.
[2] There is however a variable
c-strict-syntax-p that when set to non-nil will cause an
error to be signaled in that case. It's now considered obsolete since
it doesn't work well with some of the alignment functions that return
nil instead of zero. You should therefore leave
c-strict-syntax-p set to nil.