Interactive Customization
Customizing Indentation
Styles
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:
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 IDL 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
(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.