Previous: Choosing Modes, Up: Major Modes [Contents][Index]
The last step taken by a major mode, by convention, is to invoke a list of user supplied functions that are stored in a “hook” variable. This allows a user to further customize the major mode, and is particularly convenient for setting up buffer local variables (see Locals).
The name of the hook variable is created by appending the string
-hook to the name of the major mode. For example, the hook
variable used by text-mode would be named text-mode-hook.
By convention the mode hook function receives no arguments. If a hook
variable does not exist, or it has the value nil, the major mode
simply ignores it.
The recommended way to add functions to a hook variable is with the
add-hook function. For example, to automatically turn on the
Auto Fill mode when Text mode is invoked the following code can be used in
the initialization file (see Init File)
(add-hook 'text-mode-hook 'turn-on-auto-fill)
The add-hook function will check that the function is not already
listed in the hook variable before adding it. It will also create a hook
variable with the value nil if one does not exist before adding
the function. add-hook adds functions to the front of the hook
variable list. This means that the last hook added is run first by the
major mode. It is considered very poor style to write hook functions
that depend on the order that hooks are executed.
Hooks can be removed from hook variables with remove-hook.
Previous: Choosing Modes, Up: Major Modes [Contents][Index]