Permanent Customization
=======================
To make your changes permanent, you need to add some lisp code to
your `.emacs' file, but first you need to decide whether your styles
should be global and shared in all buffers, or local to each specific
buffer.
If you edit primarily one style of code, you may want to make the CC
Mode style variables have global values so that every buffer will share
the same style settings. This will allow you to set the CC Mode
variables at the top level of your `.emacs' file, and is the way CC
Mode works by default.
If you edit many different styles of code at the same time, you
might want to make the CC Mode style variables have buffer local
values. If you do this, then you will need to set any CC Mode style
variables in a hook function (e.g. off of `c-mode-common-hook' instead
of at the top level of your `.emacs' file). The recommended way to do
this is to set the variable `c-style-variables-are-local-p' to `t'
*before* CC Mode is loaded into your Emacs session. Note that once the
style variables are made buffer-local, they cannot be made global
again, without restarting Emacs.
CC Mode provides several hooks that you can use to customize the
mode according to your coding style. Each language mode has its own
hook, adhering to standard Emacs major mode conventions. There is also
one general hook and one package initialization hook:
* `c-mode-hook' -- for C buffers only
* `c++-mode-hook' -- for C++ buffers only
* `objc-mode-hook' -- for Objective-C buffers only
* `java-mode-hook' -- for Java buffers only
* `idl-mode-hook' -- for CORBA IDL buffers only
* `pike-mode-hook' -- for Pike buffers only
* `c-mode-common-hook' -- common across all languages
* `c-initialization-hook' -- hook run only once per Emacs session,
when CC Mode is initialized.
The language hooks get run as the last thing when you enter that
language mode. The `c-mode-common-hook' is run by all supported modes
*before* the language specific hook, and thus can contain
customizations that are common across all languages. Most of the
examples in this section will assume you are using the common hook(1).
Here's a simplified example of what you can add to your `.emacs'
file to make the changes described in the previous section more
permanent. See the Emacs manuals for more information on customizing
Emacs via hooks. See Sample .emacs File for a more complete sample
`.emacs' file.
(defun my-c-mode-common-hook ()
;; my customizations for all of c-mode and related modes
(c-set-offset 'substatement-open 0)
;; other customizations can go here
)
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
If you are sharing a single style across all CC Mode buffers and want
to make these changes in the top level of your `.emacs' file, you can
add this instead:
(setq c-default-style '((other . "user")))
(c-set-offset 'substatement-open 0)
Any time you make CC Mode customizations at the top-level of your
`.emacs' file, either through the use of `setq''s or as above, or when
you use the Custom interface, you will need to include the first line.
See Built-in Styles for details.
For complex customizations, you will probably want to set up a
*style* that groups all your customizations under a single name.
---------- Footnotes ----------
(1) `java-mode' and the hook variables interact in a slightly
different way than the other modes. `java-mode' sets the style of the
buffer to `java' *before* running the `c-mode-common-hook' or
`java-mode-hook'. You need to be aware of this so that style settings
in `c-mode-common-hook' don't clobber your Java style.