Next: , Previous: , Up: Config Basics   [Index]


6.1 Hooks

CC Mode provides several hooks that you can use to customize the mode for your coding style. The main hook is c-mode-common-hook; typically, you’ll put the bulk of your customizations here. In addition, each language mode has its own hook, allowing you to fine tune your settings individually for the different CC Mode languages, and there is a package initialization hook. Finally, there is c-special-indent-hook, which enables you to solve anomalous indentation problems. It is described in Other Indentation, not here. All these hooks adhere to the standard Emacs conventions.

When you open a buffer, CC Mode first initializes it with the currently active style (see Styles). Then it calls c-mode-common-hook, and finally it calls the language-specific hook. Thus, any style settings done in these hooks will override those set by c-default-style.

Variable: c-initialization-hook

Hook run only once per Emacs session, when CC Mode is initialized. This is a good place to change key bindings (or add new ones) in any of the CC Mode key maps. See Sample .emacs File.

Variable: c-mode-common-hook

Common hook across all languages. It’s run immediately before the language specific hook.

Variable: c-mode-hook
Variable: c++-mode-hook
Variable: objc-mode-hook
Variable: java-mode-hook
Variable: idl-mode-hook
Variable: pike-mode-hook
Variable: awk-mode-hook

The language specific mode hooks. The appropriate one is run as the last thing when you enter that language mode.

Although these hooks are variables defined in CC Mode, you can give them values before CC Mode’s code is loaded—indeed, this is the only way to use c-initialization-hook. Their values aren’t overwritten when CC Mode gets loaded.

Here’s a simplified example of what you can add to your .emacs file to do things whenever any CC Mode language is edited. 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
  (no-case-fold-search)
  )
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)

Next: , Previous: , Up: Config Basics   [Index]