Instead of installing a function by loading the file that contains it, or by evaluating the function definition, you can make the function available but not actually install it until it is first called. This is called autoloading.
When you execute an autoloaded function, Emacs automatically evaluates the file that contains the definition, and then calls the function.
Emacs starts quicker with autoloaded functions, since their libraries are not loaded right away; but you need to wait a moment when you first use such a function, while its containing file is evaluated.
Rarely used functions are frequently autoloaded. The
loaddefs.el library contains thousands of autoloaded functions,
from 5x5
to zone
. Of course, you may
come to use a rare function frequently. When you do, you should
load that function’s file with a load
expression in your
.emacs file.
In my .emacs file, I load 14 libraries that contain functions that would otherwise be autoloaded. (Actually, it would have been better to include these files in my dumped Emacs, but I forgot. See Building Emacs in The GNU Emacs Lisp Reference Manual, and the INSTALL file for more about dumping.)
You may also want to include autoloaded expressions in your .emacs
file. autoload
is a built-in function that takes up to five
arguments, the final three of which are optional. The first argument
is the name of the function to be autoloaded; the second is the name
of the file to be loaded. The third argument is documentation for the
function, and the fourth tells whether the function can be called
interactively. The fifth argument tells what type of
object—autoload
can handle a keymap or macro as well as a
function (the default is a function).
Here is a typical example:
(autoload 'html-helper-mode "html-helper-mode" "Edit HTML documents" t)
(html-helper-mode
is an older alternative to html-mode
,
which is a standard part of the distribution.)
This expression autoloads the html-helper-mode
function. It
takes it from the html-helper-mode.el file (or from the byte
compiled version html-helper-mode.elc, if that exists.) The
file must be located in a directory specified by load-path
.
The documentation says that this is a mode to help you edit documents
written in the HyperText Markup Language. You can call this mode
interactively by typing M-x html-helper-mode. (You need to
duplicate the function’s regular documentation in the autoload
expression because the regular function is not yet loaded, so its
documentation is not available.)
See Autoload in The GNU Emacs Lisp Reference Manual, for more information.