Previous: , Up: Hooks   [Contents][Index]

22.1.2 Setting Hooks

Here’s an example that uses a mode hook to turn on Auto Fill mode when in Lisp Interaction mode:

(add-hook 'lisp-interaction-mode-hook 'auto-fill-mode)
Function: add-hook hook function &optional append local

This function is the handy way to add function function to hook variable hook. You can use it for abnormal hooks as well as for normal hooks. function can be any Lisp function that can accept the proper number of arguments for hook. For example,

(add-hook 'text-mode-hook 'my-text-hook-function)

adds my-text-hook-function to the hook called text-mode-hook.

If function is already present in hook (comparing using equal), then add-hook does not add it a second time.

If function has a non-nil property permanent-local-hook, then kill-all-local-variables (or changing major modes) won’t delete it from the hook variable’s local value.

For a normal hook, hook functions should be designed so that the order in which they are executed does not matter. Any dependence on the order is asking for trouble. However, the order is predictable: normally, function goes at the front of the hook list, so it is executed first (barring another add-hook call). If the optional argument append is non-nil, the new hook function goes at the end of the hook list and is executed last.

add-hook can handle the cases where hook is void or its value is a single function; it sets or changes the value to a list of functions.

If local is non-nil, that says to add function to the buffer-local hook list instead of to the global hook list. This makes the hook buffer-local and adds t to the buffer-local value. The latter acts as a flag to run the hook functions in the default value as well as in the local value.

Function: remove-hook hook function &optional local

This function removes function from the hook variable hook. It compares function with elements of hook using equal, so it works for both symbols and lambda expressions.

If local is non-nil, that says to remove function from the buffer-local hook list instead of from the global hook list.

Previous: , Up: Hooks   [Contents][Index]