CC MODE Version 5 Documentation. Node: Permanent Customization

PREV Interactive Customization UP Customizing Indentation NEXT Styles

6.2: 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 in every buffer, 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 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.

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:

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 (Interactive Customization) 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)

For complex customizations, you will probably want to set up a style that groups all your customizations under a single name.

[1] The interaction between java-mode and the hook variables is slightly different than for the other modes. java-mode sets the style (see Styles) 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.

PREV Interactive Customization UP Customizing Indentation NEXT Styles