Previous: Unloading, Up: Loading

15.10 Hooks for Loading

You can ask for code to be executed each time Emacs loads a library, by using the variable after-load-functions:

— Variable: after-load-functions

This abnormal hook is run after loading a file. Each function in the hook is called with a single argument, the absolute filename of the file that was just loaded.

If you want code to be executed when a particular library is loaded, use the function 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 at the end of any file where (provide library) is called.

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

Normally, well-designed Lisp programs should not use eval-after-load. If you need to examine and set the variables defined in another library (those meant for outside use), you can do it immediately—there is no need to wait until the library is loaded. If you need to call functions defined by that library, you should load the library, preferably with require (see Named Features).

— Variable: after-load-alist

This variable stores an alist built by eval-after-load, containing the expressions to evaluate when certain 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 or feature name of the library being loaded.