6.3.4 Guessing the Style

Instead of specifying a style, you can get CC Mode to guess your style by examining an already formatted code buffer. CC Mode then determines the “most frequent” offset (see c-offsets-alist) for each of the syntactic symbols (see Indentation Engine Basics) encountered in the buffer, and the “most frequent” value of c-basic-offset (see Customizing Indentation), then merges the current style with these “guesses” to form a new style. This combined style is known as the guessed style.

To do this, call c-guess (or one of the other 5 guessing commands) on your sample buffer. The analysis of your code may take some time.

You can then set the guessed style in any CC Mode buffer with c-guess-install. You can display the style with c-guess-view, and preserve it by copying it into your .emacs for future use, preferably after editing it.

M-x c-guess-no-install
M-x c-guess-buffer-no-install
M-x c-guess-region-no-install

These commands analyze a part of the current buffer and guess the style from it.

The part of the buffer examined is either the region (c-guess-region-no-install), the entire buffer (c-guess-buffer-no-install), or the first c-guess-region-max bytes (c-guess-no-install).

Each of these commands can be given an optional prefix argument. This instructs CC Mode to combine the new guesses with the current guesses before forming the guessed style.

M-x c-guess
M-x c-guess-buffer
M-x c-guess-region

These commands analyze a part of the current buffer, guess the style from it, then install the guessed style on the buffer. The guessed style is given a name based on the buffer’s absolute file name, and you can then set this style on any CC Mode buffer with C-c ..

The part of the buffer examined is either the region (c-guess-region), the entire buffer (c-guess-buffer), or the first c-guess-region-max bytes (c-guess).

Each of these commands can be given an optional prefix argument. This instructs CC Mode to combine the new guesses with the current guesses before forming the guessed style.

User Option: c-guess-region-max

This variable, default 50000, is the size in bytes of the buffer portion examined by c-guess and c-guess-no-install. If set to nil, the entire buffer is examined.

User Option: c-guess-offset-threshold

This variable, default 10, is the maximum offset, either outwards or inwards, which will be taken into account by the analysis process. Any offset bigger than this will be ignored. For no limit, set this variable to a large number.

M-x c-guess-install

Set the current buffer’s style to the guessed style. This prompts you to enter an optional new style name to give to the guessed style. By default, this name is based on the buffer’s absolute file name. You can then use this style like any other.

M-x c-guess-view

Display the most recently guessed style in a temporary buffer. This display is in the form of a c-add-style form (see Adding and Amending Styles) which can be easily copied to your .emacs. You will probably want to edit it first.

The display of the guessed style contains these elements:

Placeholder Name

You should replace this with a style name of your own.

Parent Style

The style current when the guessing began, from which the guessed style inherits (see Configuration Basics) the settings which weren’t guessed.

Guessed Offsets

These are the core result of the guessing process. Each of them is marked by a comment.

Inherited Offsets

These are syntactic offsets which have been taken over from the parent style. To avoid possible future conflicts, you should remove either these offsets or the parent style name.