Built-in Styles
---------------
If you're lucky, one of CC Mode's built-in styles might be just what
you're looking for. These include:
* `gnu' -- coding style blessed by the Free Software Foundation for
C code in GNU programs.
* `k&r' -- The classic Kernighan and Ritchie style for C code.
* `bsd' -- Also known as "Allman style" after Eric Allman.
* `whitesmith' -- Popularized by the examples that came with
Whitesmiths C, an early commercial C compiler.
* `stroustrup' -- The classic Stroustrup style for C++ code.
* `ellemtel' -- Popular C++ coding standards as defined by
"Programming in C++, Rules and Recommendations", Erik Nyquist and
Mats Henricson, Ellemtel (1).
* `linux' -- C coding standard for Linux development.
* `python' -- C coding standard for Python extension modules(2).
* `java' -- The style for editing Java code. Note that this style is
automatically installed when you enter `java-mode'.
* `user' -- This is a special style for several reasons. First, if
you customize CC Mode by using either the new Custom interface(3)
or by doing `setq''s at the top level of your `.emacs' file, these
settings will be captured in the `user' style. Also, all other
styles implicitly inherit their settings from `user' style. This
means that for any styles you add via `c-add-style' (*Note Adding
Styles::) you need only define the differences between your new
style and `user' style.
Note however that `user' style is *not* the default style. `gnu'
is the default style for all newly created buffers, but you can
change this by setting variable `c-default-style'. Be careful if
you customize CC Mode as described above; since your changes will
be captured in the `user' style, you will also have to change
`c-default-style' to "user" to see the effect of your
customizations.
`c-default-style' actually takes either a style name string, or an
association list of major mode symbols to style names. Thus you can
control exactly which default style is used for which CC Mode language
mode. Here are the rules:
1. When `c-default-style' is a string, it must be an existing style
name as found in `c-style-alist'. This style is then used for all
modes *except* `java-mode', where the style `java' is used by
default(4).
2. When `c-default-style' is an association list, the current major
mode is looked up to find a style name string. In this case, this
style is always used exactly as specified and an error will occur
if the named style does not exist.
3. If `c-default-style' is an association list, but the current major
mode isn't found, then the special symbol `other' is looked up. If
this value is found, the associated style is used.
4. If `other' is not found, then the `gnu' style is used.
5. In all cases, the style described in `c-default-style' is installed
*before* the language hooks are run, so you can always override
this setting by including an explicit call to `c-set-style' in your
language mode hook, or in `c-mode-common-hook'.
If you'd like to experiment with these built-in styles you can simply
type the following in a CC Mode buffer:
C-c . STYLE-NAME RET
`C-c .' runs the command `c-set-style'. Note that all style names are
case insensitive, even the ones you define.
Setting a style in this way does *not* automatically re-indent your
file. For commands that you can use to view the effect of your changes,
see See Commands.
Once you find a built-in style you like, you can make the change
permanent by adding some lisp to your `.emacs' file. Let's say for
example that you want to use the `ellemtel' style in all your files.
You would add this:
(defun my-c-mode-common-hook ()
;; use Ellemtel style for all C like languages
(c-set-style "ellemtel")
;; other customizations can go here
)
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
Note that for BOCM compatibility, `gnu' is the default style, and
any non-style based customizations you make (i.e. in
`c-mode-common-hook' in your `.emacs' file) will be based on `gnu'
style unless you do a `c-set-style' as the first thing in your hook.
The variable `c-indentation-style' always contains the buffer's current
style name, as a string.
---------- Footnotes ----------
(1) This document is ftp'able from `euagate.eua.ericsson.se'
(2) Python is a high level scripting language with a C/C++ foreign
function interface. For more information, see
`<http://www.python.org/>'.
(3) Available in Emacs 20, XEmacs 19.15 and XEmacs 20.
(4) This is for backwards compatibility reasons. The hard-coding of
`java-mode' style is admittedly bogus!