[ < ] [ > ]   [ << ] [Plus haut] [ >> ]         [Top] [Table des matières] [Index] [ ? ]

15.10 Hooks for Loading

You can ask for code to be executed if and when a particular library is loaded, by calling eval-after-load.

Function: eval-after-load library form

This function arranges to evaluate form at the end of loading the file library, each time library is loaded. If library is already loaded, it evaluates form right away. Don't forget to quote form!

You don't need to give a directory or extension in the file name library—normally you just give a bare file name, like this:

 
(eval-after-load "edebug" '(def-edebug-spec c-point t))

To restrict which files can trigger the evaluation, include a directory or an extension or both in library. Only a file whose absolute true name (i.e., the name with all symbolic links chased out) matches all the given name components will match. In the following example, ‘my_inst.elc’ or ‘my_inst.elc.gz’ in some directory ..../foo/bar will trigger the evaluation, but not ‘my_inst.el’:

 
(eval-after-load "foo/bar/my_inst.elc" …)

library can also be a feature (i.e. a symbol), in which case form is evaluated when (provide library) is called.

An error in form does not undo the load, but does prevent execution of the rest of form.

In general, well-designed Lisp programs should not use this feature. The clean and modular ways to interact with a Lisp library are (1) examine and set the library's variables (those which are meant for outside use), and (2) call the library's functions. If you wish to do (1), you can do it immediately—there is no need to wait for when the library is loaded. To do (2), you must load the library (preferably with require).

But it is OK to use eval-after-load in your personal customizations if you don't feel they must meet the design standards for programs meant for wider use.

Variable: after-load-alist

This variable, an alist built by eval-after-load, holds the expressions to evaluate when particular libraries are loaded. Each element looks like this:

 
(regexp-or-feature forms…)

The key regexp-or-feature is either a regular expression or a symbol, and the value is a list of forms. The forms are evaluated when the key matches the absolute true name of the file being loaded or the symbol being provided.


[ < ] [ > ]   [ << ] [Plus haut] [ >> ]         [Top] [Table des matières] [Index] [ ? ]

Ce document a été généré par Eric Reinbold le 13 Octobre 2007 en utilisant texi2html 1.78.