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.
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.
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.
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.
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.
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.
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:
You should replace this with a style name of your own.
The style current when the guessing began, from which the guessed style inherits (see Configuration Basics) the settings which weren’t guessed.
These are the core result of the guessing process. Each of them is marked by a comment.
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.