next up previous
Next: Common Lisp Up: Hooks and Open Points Previous: Introduction

Hooks in Lisp

  The hook mechanism of Lisp is based on the simple metaphor illustrated in figure 1.

  
Figure 1: A program with hooks.

The thin curve illustrates the flow of control of a program, which is symbolized by the big oval. Along this curve there are hooks to which it is possible to attach functions that affect the functionality of the program.

In Sandewall's paper on the experiences with Lisp [19], a notion of handles is briefly discussed (in section 5.1 of the paper). Such handles are identical to what we call hooks in this paper. Following Sandewall, a handle is

``... a user-defined procedure that can be inserted instead of the original procedure provided by the system'', [19, section 1.1,].
Sandewall points out that an algorithm with handles cannot be regarded as a black box, because the handles can be substituted by arbitrary procedures. A program with handles is typically the main program---a pattern which may be augmented with user-defined contributions. Sandewall also mentions that it is convenient to use a special technique for attachment of procedures to handles, instead of using procedural arguments to higher order procedures. An incremental parameter binding technique with support of default parameters is more practical than the conventional parameter binding technique.

Hooks are also discussed by Stallman in a paper about the Emacs text editor [21]. Several Emacs systems use Lisp for extension purposes. GNU Emacs [20] is probably the most well-known of these. According to Stallman, a hook is a characteristics of an extensible system, which

``... allows the user to provide a function to be called on certain well-defined occasions'', [21, section 5.6,].
The main usage of hooks in Lisp-based Emacs systems is to provide for customization and extensions of the editor's modes. Different kinds of activities in Emacs are supported by different modes. Among other things, modes determine the mapping of keys on the keyboard to functionality defined by Lisp functions. As one of the last actions during the entrance of a mode in GNU Emacs, ``the hooks of the mode are run'' (using Emacs terminology). Each hook may attach several functions, all of which are supposed to be without parameters. The execution of hooks makes it possible to change the behavior of the mode (such as the established bindings of keys on the keyboard) as well as to add new functionality to the mode. In addition to mode entrance, hooks are also associated with a few other interactions in Emacs, such as loading a file into a buffer and vice versa.





next up previous
Next: Common Lisp Up: Hooks and Open Points Previous: Introduction



Kurt Noermark
Wed Mar 6 09:44:24 MET 1996