*** empty log message ***
[bpt/emacs.git] / lispref / loading.texi
CommitLineData
83ac6b45
RS
1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual.
7baeca0c
LT
3@c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998, 1999,
4@c 2003, 2004
177c0ea7 5@c Free Software Foundation, Inc.
83ac6b45
RS
6@c See the file elisp.texi for copying conditions.
7@setfilename ../info/loading
f9f59935 8@node Loading, Byte Compilation, Customization, Top
83ac6b45
RS
9@chapter Loading
10@cindex loading
11@cindex library
12@cindex Lisp library
13
14 Loading a file of Lisp code means bringing its contents into the Lisp
15environment in the form of Lisp objects. Emacs finds and opens the
16file, reads the text, evaluates each form, and then closes the file.
17
18 The load functions evaluate all the expressions in a file just
19as the @code{eval-current-buffer} function evaluates all the
20expressions in a buffer. The difference is that the load functions
21read and evaluate the text in the file as found on disk, not the text
22in an Emacs buffer.
23
24@cindex top-level form
25 The loaded file must contain Lisp expressions, either as source code
78c71a98
RS
26or as byte-compiled code. Each form in the file is called a
27@dfn{top-level form}. There is no special format for the forms in a
83ac6b45
RS
28loadable file; any form in a file may equally well be typed directly
29into a buffer and evaluated there. (Indeed, most code is tested this
30way.) Most often, the forms are function definitions and variable
31definitions.
32
33 A file containing Lisp code is often called a @dfn{library}. Thus,
34the ``Rmail library'' is a file containing code for Rmail mode.
35Similarly, a ``Lisp library directory'' is a directory of files
36containing Lisp code.
37
38@menu
39* How Programs Do Loading:: The @code{load} function and others.
a9f0a989 40* Library Search:: Finding a library to load.
ad800164 41* Loading Non-ASCII:: Non-@acronym{ASCII} characters in Emacs Lisp files.
83ac6b45
RS
42* Autoload:: Setting up a function to autoload.
43* Repeated Loading:: Precautions about loading a file twice.
bfe721d1 44* Named Features:: Loading a library if it isn't already loaded.
83ac6b45
RS
45* Unloading:: How to ``unload'' a library that was loaded.
46* Hooks for Loading:: Providing code to be run when
47 particular libraries are loaded.
48@end menu
49
50@node How Programs Do Loading
51@section How Programs Do Loading
52
53 Emacs Lisp has several interfaces for loading. For example,
f9f59935
RS
54@code{autoload} creates a placeholder object for a function defined in a
55file; trying to call the autoloading function loads the file to get the
83ac6b45 56function's real definition (@pxref{Autoload}). @code{require} loads a
f9f59935
RS
57file if it isn't already loaded (@pxref{Named Features}). Ultimately,
58all these facilities call the @code{load} function to do the work.
83ac6b45 59
a9f0a989 60@defun load filename &optional missing-ok nomessage nosuffix must-suffix
83ac6b45
RS
61This function finds and opens a file of Lisp code, evaluates all the
62forms in it, and closes the file.
63
64To find the file, @code{load} first looks for a file named
65@file{@var{filename}.elc}, that is, for a file whose name is
66@var{filename} with @samp{.elc} appended. If such a file exists, it is
67loaded. If there is no file by that name, then @code{load} looks for a
78c71a98 68file named @file{@var{filename}.el}. If that file exists, it is loaded.
83ac6b45
RS
69Finally, if neither of those names is found, @code{load} looks for a
70file named @var{filename} with nothing appended, and loads it if it
71exists. (The @code{load} function is not clever about looking at
72@var{filename}. In the perverse case of a file named @file{foo.el.el},
73evaluation of @code{(load "foo.el")} will indeed find it.)
74
75If the optional argument @var{nosuffix} is non-@code{nil}, then the
76suffixes @samp{.elc} and @samp{.el} are not tried. In this case, you
f9f59935
RS
77must specify the precise file name you want. By specifying the precise
78file name and using @code{t} for @var{nosuffix}, you can prevent
79perverse file names such as @file{foo.el.el} from being tried.
83ac6b45 80
a9f0a989
RS
81If the optional argument @var{must-suffix} is non-@code{nil}, then
82@code{load} insists that the file name used must end in either
83@samp{.el} or @samp{.elc}, unless it contains an explicit directory
84name. If @var{filename} does not contain an explicit directory name,
85and does not end in a suffix, then @code{load} insists on adding one.
86
83ac6b45
RS
87If @var{filename} is a relative file name, such as @file{foo} or
88@file{baz/foo.bar}, @code{load} searches for the file using the variable
89@code{load-path}. It appends @var{filename} to each of the directories
90listed in @code{load-path}, and loads the first file it finds whose name
91matches. The current default directory is tried only if it is specified
92in @code{load-path}, where @code{nil} stands for the default directory.
93@code{load} tries all three possible suffixes in the first directory in
94@code{load-path}, then all three suffixes in the second directory, and
a9f0a989 95so on. @xref{Library Search}.
83ac6b45
RS
96
97If you get a warning that @file{foo.elc} is older than @file{foo.el}, it
98means you should consider recompiling @file{foo.el}. @xref{Byte
99Compilation}.
100
969fe9b5
RS
101When loading a source file (not compiled), @code{load} performs
102character set translation just as Emacs would do when visiting the file.
103@xref{Coding Systems}.
104
83ac6b45
RS
105Messages like @samp{Loading foo...} and @samp{Loading foo...done} appear
106in the echo area during loading unless @var{nomessage} is
107non-@code{nil}.
108
109@cindex load errors
110Any unhandled errors while loading a file terminate loading. If the
78c71a98
RS
111load was done for the sake of @code{autoload}, any function definitions
112made during the loading are undone.
83ac6b45
RS
113
114@kindex file-error
115If @code{load} can't find the file to load, then normally it signals the
116error @code{file-error} (with @samp{Cannot open load file
117@var{filename}}). But if @var{missing-ok} is non-@code{nil}, then
118@code{load} just returns @code{nil}.
119
22697dac
KH
120You can use the variable @code{load-read-function} to specify a function
121for @code{load} to use instead of @code{read} for reading expressions.
122See below.
123
83ac6b45
RS
124@code{load} returns @code{t} if the file loads successfully.
125@end defun
126
83ac6b45 127@deffn Command load-file filename
f9f59935
RS
128This command loads the file @var{filename}. If @var{filename} is a
129relative file name, then the current default directory is assumed.
130@code{load-path} is not used, and suffixes are not appended. Use this
a9f0a989 131command if you wish to specify precisely the file name to load.
83ac6b45
RS
132@end deffn
133
134@deffn Command load-library library
f9f59935
RS
135This command loads the library named @var{library}. It is equivalent to
136@code{load}, except in how it reads its argument interactively.
83ac6b45 137@end deffn
83ac6b45 138
a9f0a989
RS
139@defvar load-in-progress
140This variable is non-@code{nil} if Emacs is in the process of loading a
141file, and it is @code{nil} otherwise.
142@end defvar
143
144@defvar load-read-function
7baeca0c 145@anchor{Definition of load-read-function}
a9f0a989
RS
146This variable specifies an alternate expression-reading function for
147@code{load} and @code{eval-region} to use instead of @code{read}.
148The function should accept one argument, just as @code{read} does.
149
150Normally, the variable's value is @code{nil}, which means those
151functions should use @code{read}.
55607887 152
6142d1d0
RS
153Instead of using this variable, it is cleaner to use another, newer
154feature: to pass the function as the @var{read-function} argument to
da9f5ab2 155@code{eval-region}. @xref{Definition of eval-region,, Eval}.
a9f0a989
RS
156@end defvar
157
1911e6e5
RS
158 For information about how @code{load} is used in building Emacs, see
159@ref{Building Emacs}.
a9f0a989
RS
160
161@node Library Search
162@section Library Search
163
164 When Emacs loads a Lisp library, it searches for the library
165in a list of directories specified by the variable @code{load-path}.
166
83ac6b45
RS
167@defopt load-path
168@cindex @code{EMACSLOADPATH} environment variable
169The value of this variable is a list of directories to search when
170loading files with @code{load}. Each element is a string (which must be
171a directory name) or @code{nil} (which stands for the current working
a9f0a989
RS
172directory).
173@end defopt
174
175 The value of @code{load-path} is initialized from the environment
176variable @code{EMACSLOADPATH}, if that exists; otherwise its default
cf11ad96 177value is specified in @file{emacs/src/epaths.h} when Emacs is built.
a9f0a989
RS
178Then the list is expanded by adding subdirectories of the directories
179in the list.
83ac6b45 180
a9f0a989 181 The syntax of @code{EMACSLOADPATH} is the same as used for @code{PATH};
bfe721d1
KH
182@samp{:} (or @samp{;}, according to the operating system) separates
183directory names, and @samp{.} is used for the current default directory.
184Here is an example of how to set your @code{EMACSLOADPATH} variable from
185a @code{csh} @file{.login} file:
83ac6b45 186
83ac6b45 187@smallexample
f1e2c45e 188setenv EMACSLOADPATH .:/user/bil/emacs:/usr/local/share/emacs/20.3/lisp
83ac6b45
RS
189@end smallexample
190
a9f0a989 191 Here is how to set it using @code{sh}:
83ac6b45
RS
192
193@smallexample
194export EMACSLOADPATH
f1e2c45e 195EMACSLOADPATH=.:/user/bil/emacs:/usr/local/share/emacs/20.3/lisp
83ac6b45
RS
196@end smallexample
197
a40d4712
PR
198 Here is an example of code you can place in your init file (@pxref{Init
199File}) to add several directories to the front of your default
200@code{load-path}:
83ac6b45
RS
201
202@smallexample
bda144f4 203@group
83ac6b45
RS
204(setq load-path
205 (append (list nil "/user/bil/emacs"
206 "/usr/local/lisplib"
5e41cf03 207 "~/emacs")
83ac6b45 208 load-path))
bda144f4 209@end group
83ac6b45
RS
210@end smallexample
211
212@c Wordy to rid us of an overfull hbox. --rjc 15mar92
213@noindent
214In this example, the path searches the current working directory first,
5e41cf03
RS
215followed then by the @file{/user/bil/emacs} directory, the
216@file{/usr/local/lisplib} directory, and the @file{~/emacs} directory,
83ac6b45
RS
217which are then followed by the standard directories for Lisp code.
218
a9f0a989 219 Dumping Emacs uses a special value of @code{load-path}. If the value of
c642171c
RS
220@code{load-path} at the end of dumping is unchanged (that is, still the
221same special value), the dumped Emacs switches to the ordinary
cc8c51f1 222@code{load-path} value when it starts up, as described above. But if
c642171c
RS
223@code{load-path} has any other value at the end of dumping, that value
224is used for execution of the dumped Emacs also.
225
a9f0a989 226 Therefore, if you want to change @code{load-path} temporarily for
c642171c
RS
227loading a few libraries in @file{site-init.el} or @file{site-load.el},
228you should bind @code{load-path} locally with @code{let} around the
229calls to @code{load}.
83ac6b45 230
089e089d 231 The default value of @code{load-path}, when running an Emacs which has
a9f0a989
RS
232been installed on the system, includes two special directories (and
233their subdirectories as well):
089e089d
RS
234
235@smallexample
a9f0a989 236"/usr/local/share/emacs/@var{version}/site-lisp"
089e089d
RS
237@end smallexample
238
a9f0a989
RS
239@noindent
240and
241
242@smallexample
243"/usr/local/share/emacs/site-lisp"
244@end smallexample
245
246@noindent
247The first one is for locally installed packages for a particular Emacs
248version; the second is for locally installed packages meant for use with
249all installed Emacs versions.
089e089d
RS
250
251 There are several reasons why a Lisp package that works well in one
252Emacs version can cause trouble in another. Sometimes packages need
253updating for incompatible changes in Emacs; sometimes they depend on
254undocumented internal Emacs data that can change without notice;
255sometimes a newer Emacs version incorporates a version of the package,
256and should be used only with that version.
257
a9f0a989
RS
258 Emacs finds these directories' subdirectories and adds them to
259@code{load-path} when it starts up. Both immediate subdirectories and
260subdirectories multiple levels down are added to @code{load-path}.
261
262 Not all subdirectories are included, though. Subdirectories whose
263names do not start with a letter or digit are excluded. Subdirectories
8241495d
RS
264named @file{RCS} or @file{CVS} are excluded. Also, a subdirectory which
265contains a file named @file{.nosearch} is excluded. You can use these
266methods to prevent certain subdirectories of the @file{site-lisp}
267directories from being searched.
a9f0a989 268
089e089d
RS
269 If you run Emacs from the directory where it was built---that is, an
270executable that has not been formally installed---then @code{load-path}
271normally contains two additional directories. These are the @code{lisp}
272and @code{site-lisp} subdirectories of the main build directory. (Both
273are represented as absolute file names.)
274
f9f59935
RS
275@deffn Command locate-library library &optional nosuffix path interactive-call
276This command finds the precise file name for library @var{library}. It
277searches for the library in the same way @code{load} does, and the
278argument @var{nosuffix} has the same meaning as in @code{load}: don't
279add suffixes @samp{.elc} or @samp{.el} to the specified name
280@var{library}.
281
282If the @var{path} is non-@code{nil}, that list of directories is used
283instead of @code{load-path}.
284
285When @code{locate-library} is called from a program, it returns the file
286name as a string. When the user runs @code{locate-library}
287interactively, the argument @var{interactive-call} is @code{t}, and this
288tells @code{locate-library} to display the file name in the echo area.
289@end deffn
290
cee6bbce
RS
291@defvar load-suffixes
292This variable is a list of suffixes (strings) that @code{load} should
293try adding to the specified file name. The default value is
b7ebcab7 294@code{(".elc" ".el")}. There is no need to include the null suffix.
cee6bbce
RS
295@end defvar
296
a9f0a989 297@node Loading Non-ASCII
ad800164 298@section Loading Non-@acronym{ASCII} Characters
a9f0a989 299
ad800164 300 When Emacs Lisp programs contain string constants with non-@acronym{ASCII}
a9f0a989
RS
301characters, these can be represented within Emacs either as unibyte
302strings or as multibyte strings (@pxref{Text Representations}). Which
303representation is used depends on how the file is read into Emacs. If
304it is read with decoding into multibyte representation, the text of the
305Lisp program will be multibyte text, and its string constants will be
306multibyte strings. If a file containing Latin-1 characters (for
307example) is read without decoding, the text of the program will be
308unibyte text, and its string constants will be unibyte strings.
309@xref{Coding Systems}.
310
311 To make the results more predictable, Emacs always performs decoding
312into the multibyte representation when loading Lisp files, even if it
313was started with the @samp{--unibyte} option. This means that string
ad800164 314constants with non-@acronym{ASCII} characters translate into multibyte
a9f0a989
RS
315strings. The only exception is when a particular file specifies no
316decoding.
317
318 The reason Emacs is designed this way is so that Lisp programs give
319predictable results, regardless of how Emacs was started. In addition,
320this enables programs that depend on using multibyte text to work even
321in a unibyte Emacs. Of course, such programs should be designed to
322notice whether the user prefers unibyte or multibyte text, by checking
323@code{default-enable-multibyte-characters}, and convert representations
324appropriately.
325
ad800164 326 In most Emacs Lisp programs, the fact that non-@acronym{ASCII} strings are
a9f0a989
RS
327multibyte strings should not be noticeable, since inserting them in
328unibyte buffers converts them to unibyte automatically. However, if
329this does make a difference, you can force a particular Lisp file to be
430f8c73 330interpreted as unibyte by writing @samp{-*-unibyte: t;-*-} in a
a9f0a989 331comment on the file's first line. With that designator, the file will
8241495d 332unconditionally be interpreted as unibyte, even in an ordinary
6824708b 333multibyte Emacs session. This can matter when making keybindings to
ad800164 334non-@acronym{ASCII} characters written as @code{?v@var{literal}}.
a9f0a989 335
83ac6b45
RS
336@node Autoload
337@section Autoload
338@cindex autoload
339
340 The @dfn{autoload} facility allows you to make a function or macro
bfe721d1
KH
341known in Lisp, but put off loading the file that defines it. The first
342call to the function automatically reads the proper file to install the
343real definition and other associated code, then runs the real definition
83ac6b45
RS
344as if it had been loaded all along.
345
346 There are two ways to set up an autoloaded function: by calling
347@code{autoload}, and by writing a special ``magic'' comment in the
348source before the real definition. @code{autoload} is the low-level
349primitive for autoloading; any Lisp program can call @code{autoload} at
969fe9b5 350any time. Magic comments are the most convenient way to make a function
a9f0a989
RS
351autoload, for packages installed along with Emacs. These comments do
352nothing on their own, but they serve as a guide for the command
969fe9b5
RS
353@code{update-file-autoloads}, which constructs calls to @code{autoload}
354and arranges to execute them when Emacs is built.
83ac6b45 355
78c71a98
RS
356@defun autoload function filename &optional docstring interactive type
357This function defines the function (or macro) named @var{function} so as
83ac6b45
RS
358to load automatically from @var{filename}. The string @var{filename}
359specifies the file to load to get the real definition of @var{function}.
360
f9f59935
RS
361If @var{filename} does not contain either a directory name, or the
362suffix @code{.el} or @code{.elc}, then @code{autoload} insists on adding
363one of these suffixes, and it will not load from a file whose name is
364just @var{filename} with no added suffix.
365
83ac6b45 366The argument @var{docstring} is the documentation string for the
f9f59935 367function. Normally, this should be identical to the documentation string
83ac6b45
RS
368in the function definition itself. Specifying the documentation string
369in the call to @code{autoload} makes it possible to look at the
370documentation without loading the function's real definition.
371
969fe9b5
RS
372If @var{interactive} is non-@code{nil}, that says @var{function} can be
373called interactively. This lets completion in @kbd{M-x} work without
a9f0a989
RS
374loading @var{function}'s real definition. The complete interactive
375specification is not given here; it's not needed unless the user
376actually calls @var{function}, and when that happens, it's time to load
377the real definition.
83ac6b45
RS
378
379You can autoload macros and keymaps as well as ordinary functions.
380Specify @var{type} as @code{macro} if @var{function} is really a macro.
381Specify @var{type} as @code{keymap} if @var{function} is really a
382keymap. Various parts of Emacs need to know this information without
383loading the real definition.
384
bda144f4
MW
385An autoloaded keymap loads automatically during key lookup when a prefix
386key's binding is the symbol @var{function}. Autoloading does not occur
387for other kinds of access to the keymap. In particular, it does not
388happen when a Lisp program gets the keymap from the value of a variable
389and calls @code{define-key}; not even if the variable name is the same
390symbol @var{function}.
391
83ac6b45 392@cindex function cell in autoload
78c71a98 393If @var{function} already has a non-void function definition that is not
83ac6b45 394an autoload object, @code{autoload} does nothing and returns @code{nil}.
78c71a98 395If the function cell of @var{function} is void, or is already an autoload
83ac6b45
RS
396object, then it is defined as an autoload object like this:
397
398@example
399(autoload @var{filename} @var{docstring} @var{interactive} @var{type})
400@end example
401
177c0ea7 402For example,
83ac6b45
RS
403
404@example
bda144f4 405@group
83ac6b45
RS
406(symbol-function 'run-prolog)
407 @result{} (autoload "prolog" 169681 t nil)
bda144f4 408@end group
83ac6b45
RS
409@end example
410
411@noindent
412In this case, @code{"prolog"} is the name of the file to load, 169681
f9f59935
RS
413refers to the documentation string in the
414@file{emacs/etc/DOC-@var{version}} file (@pxref{Documentation Basics}),
415@code{t} means the function is interactive, and @code{nil} that it is
416not a macro or a keymap.
83ac6b45
RS
417@end defun
418
419@cindex autoload errors
420 The autoloaded file usually contains other definitions and may require
421or provide one or more features. If the file is not completely loaded
422(due to an error in the evaluation of its contents), any function
423definitions or @code{provide} calls that occurred during the load are
424undone. This is to ensure that the next attempt to call any function
425autoloading from this file will try again to load the file. If not for
a9f0a989
RS
426this, then some of the functions in the file might be defined by the
427aborted load, but fail to work properly for the lack of certain
428subroutines not loaded successfully because they come later in the file.
83ac6b45
RS
429
430 If the autoloaded file fails to define the desired Lisp function or
431macro, then an error is signaled with data @code{"Autoloading failed to
432define function @var{function-name}"}.
433
434@findex update-file-autoloads
435@findex update-directory-autoloads
a9f0a989 436 A magic autoload comment consists of @samp{;;;###autoload}, on a line
83ac6b45
RS
437by itself, just before the real definition of the function in its
438autoloadable source file. The command @kbd{M-x update-file-autoloads}
439writes a corresponding @code{autoload} call into @file{loaddefs.el}.
440Building Emacs loads @file{loaddefs.el} and thus calls @code{autoload}.
441@kbd{M-x update-directory-autoloads} is even more powerful; it updates
442autoloads for all files in the current directory.
443
444 The same magic comment can copy any kind of form into
445@file{loaddefs.el}. If the form following the magic comment is not a
8241495d
RS
446function-defining form or a @code{defcustom} form, it is copied
447verbatim. ``Function-defining forms'' include @code{define-skeleton},
448@code{define-derived-mode}, @code{define-generic-mode} and
5858d11f 449@code{define-minor-mode} as well as @code{defun} and
8241495d
RS
450@code{defmacro}. To save space, a @code{defcustom} form is converted to
451a @code{defvar} in @file{loaddefs.el}, with some additional information
452if it uses @code{:require}.
453
454 You can also use a magic comment to execute a form at build time
455@emph{without} executing it when the file itself is loaded. To do this,
456write the form @emph{on the same line} as the magic comment. Since it
457is in a comment, it does nothing when you load the source file; but
458@kbd{M-x update-file-autoloads} copies it to @file{loaddefs.el}, where
459it is executed while building Emacs.
83ac6b45
RS
460
461 The following example shows how @code{doctor} is prepared for
462autoloading with a magic comment:
463
464@smallexample
465;;;###autoload
466(defun doctor ()
467 "Switch to *doctor* buffer and start giving psychotherapy."
468 (interactive)
469 (switch-to-buffer "*doctor*")
470 (doctor-mode))
471@end smallexample
472
473@noindent
474Here's what that produces in @file{loaddefs.el}:
475
476@smallexample
8241495d 477(autoload 'doctor "doctor" "\
83ac6b45
RS
478Switch to *doctor* buffer and start giving psychotherapy."
479 t)
480@end smallexample
481
482@noindent
483The backslash and newline immediately following the double-quote are a
8241495d 484convention used only in the preloaded uncompiled Lisp files such as
83ac6b45
RS
485@file{loaddefs.el}; they tell @code{make-docfile} to put the
486documentation string in the @file{etc/DOC} file. @xref{Building Emacs}.
8241495d 487See also the commentary in @file{lib-src/make-docfile.c}.
83ac6b45 488
7f551e47
RS
489 If you write a function definition with an unusual macro that is not
490one of the known and recognized function definition methods, use of an
491ordinary magic autoload comment would copy the whole definition into
492@code{loaddefs.el}. That is not desirable. You can put the desired
493@code{autoload} call into @code{loaddefs.el} instead by writing this:
494
495@smallexample
496;;;###autoload (autoload 'foo "myfile")
497(mydefunmacro foo
498 ...)
499@end smallexample
500
83ac6b45 501@node Repeated Loading
83ac6b45
RS
502@section Repeated Loading
503@cindex repeated loading
504
a9f0a989 505 You can load a given file more than once in an Emacs session. For
83ac6b45
RS
506example, after you have rewritten and reinstalled a function definition
507by editing it in a buffer, you may wish to return to the original
508version; you can do this by reloading the file it came from.
509
510 When you load or reload files, bear in mind that the @code{load} and
511@code{load-library} functions automatically load a byte-compiled file
512rather than a non-compiled file of similar name. If you rewrite a file
f9f59935
RS
513that you intend to save and reinstall, you need to byte-compile the new
514version; otherwise Emacs will load the older, byte-compiled file instead
515of your newer, non-compiled file! If that happens, the message
a9f0a989 516displayed when loading the file includes, @samp{(compiled; note, source is
969fe9b5 517newer)}, to remind you to recompile it.
83ac6b45
RS
518
519 When writing the forms in a Lisp library file, keep in mind that the
f9f59935
RS
520file might be loaded more than once. For example, think about whether
521each variable should be reinitialized when you reload the library;
522@code{defvar} does not change the value if the variable is already
523initialized. (@xref{Defining Variables}.)
83ac6b45
RS
524
525 The simplest way to add an element to an alist is like this:
526
527@example
528(setq minor-mode-alist
529 (cons '(leif-mode " Leif") minor-mode-alist))
530@end example
531
532@noindent
533But this would add multiple elements if the library is reloaded.
534To avoid the problem, write this:
535
536@example
537(or (assq 'leif-mode minor-mode-alist)
538 (setq minor-mode-alist
539 (cons '(leif-mode " Leif") minor-mode-alist)))
540@end example
541
a9f0a989 542 To add an element to a list just once, you can also use @code{add-to-list}
bfe721d1
KH
543(@pxref{Setting Variables}).
544
83ac6b45
RS
545 Occasionally you will want to test explicitly whether a library has
546already been loaded. Here's one way to test, in a library, whether it
547has been loaded before:
548
549@example
969fe9b5 550(defvar foo-was-loaded nil)
bfe721d1 551
969fe9b5
RS
552(unless foo-was-loaded
553 @var{execute-first-time-only}
554 (setq foo-was-loaded t))
83ac6b45
RS
555@end example
556
557@noindent
558If the library uses @code{provide} to provide a named feature, you can
969fe9b5
RS
559use @code{featurep} earlier in the file to test whether the
560@code{provide} call has been executed before.
37680279 561@ifnottex
bfe721d1 562@xref{Named Features}.
37680279 563@end ifnottex
83ac6b45 564
bfe721d1 565@node Named Features
83ac6b45
RS
566@section Features
567@cindex features
568@cindex requiring features
569@cindex providing features
570
571 @code{provide} and @code{require} are an alternative to
572@code{autoload} for loading files automatically. They work in terms of
573named @dfn{features}. Autoloading is triggered by calling a specific
574function, but a feature is loaded the first time another program asks
575for it by name.
576
577 A feature name is a symbol that stands for a collection of functions,
578variables, etc. The file that defines them should @dfn{provide} the
579feature. Another program that uses them may ensure they are defined by
580@dfn{requiring} the feature. This loads the file of definitions if it
581hasn't been loaded already.
582
583 To require the presence of a feature, call @code{require} with the
584feature name as argument. @code{require} looks in the global variable
585@code{features} to see whether the desired feature has been provided
586already. If not, it loads the feature from the appropriate file. This
78c71a98 587file should call @code{provide} at the top level to add the feature to
83ac6b45
RS
588@code{features}; if it fails to do so, @code{require} signals an error.
589@cindex load error with require
590
177c0ea7 591 For example, in @file{emacs/lisp/prolog.el},
83ac6b45
RS
592the definition for @code{run-prolog} includes the following code:
593
594@smallexample
595(defun run-prolog ()
9e2b495b 596 "Run an inferior Prolog process, with I/O via buffer *prolog*."
83ac6b45
RS
597 (interactive)
598 (require 'comint)
599 (switch-to-buffer (make-comint "prolog" prolog-program-name))
600 (inferior-prolog-mode))
601@end smallexample
602
603@noindent
604The expression @code{(require 'comint)} loads the file @file{comint.el}
605if it has not yet been loaded. This ensures that @code{make-comint} is
969fe9b5
RS
606defined. Features are normally named after the files that provide them,
607so that @code{require} need not be given the file name.
83ac6b45
RS
608
609The @file{comint.el} file contains the following top-level expression:
610
611@smallexample
612(provide 'comint)
613@end smallexample
614
615@noindent
616This adds @code{comint} to the global @code{features} list, so that
617@code{(require 'comint)} will henceforth know that nothing needs to be
618done.
619
620@cindex byte-compiling @code{require}
78c71a98 621 When @code{require} is used at top level in a file, it takes effect
83ac6b45
RS
622when you byte-compile that file (@pxref{Byte Compilation}) as well as
623when you load it. This is in case the required package contains macros
8241495d
RS
624that the byte compiler must know about. It also avoids byte-compiler
625warnings for functions and variables defined in the file loaded with
626@code{require}.
83ac6b45
RS
627
628 Although top-level calls to @code{require} are evaluated during
629byte compilation, @code{provide} calls are not. Therefore, you can
630ensure that a file of definitions is loaded before it is byte-compiled
631by including a @code{provide} followed by a @code{require} for the same
632feature, as in the following example.
633
634@smallexample
635@group
636(provide 'my-feature) ; @r{Ignored by byte compiler,}
637 ; @r{evaluated by @code{load}.}
638(require 'my-feature) ; @r{Evaluated by byte compiler.}
639@end group
640@end smallexample
641
78c71a98
RS
642@noindent
643The compiler ignores the @code{provide}, then processes the
644@code{require} by loading the file in question. Loading the file does
645execute the @code{provide} call, so the subsequent @code{require} call
969fe9b5 646does nothing when the file is loaded.
78c71a98 647
f2aa473a 648@defun provide feature &optional subfeatures
83ac6b45
RS
649This function announces that @var{feature} is now loaded, or being
650loaded, into the current Emacs session. This means that the facilities
651associated with @var{feature} are or will be available for other Lisp
652programs.
653
654The direct effect of calling @code{provide} is to add @var{feature} to
655the front of the list @code{features} if it is not already in the list.
656The argument @var{feature} must be a symbol. @code{provide} returns
657@var{feature}.
658
f2aa473a
SM
659If provided, @var{subfeatures} should be a list of symbols indicating
660a set of specific subfeatures provided by this version of @var{feature}.
0373c25e 661You can test the presence of a subfeature using @code{featurep}.
f2aa473a 662
83ac6b45
RS
663@smallexample
664features
665 @result{} (bar bish)
666
667(provide 'foo)
668 @result{} foo
669features
670 @result{} (foo bar bish)
671@end smallexample
672
bfe721d1 673When a file is loaded to satisfy an autoload, and it stops due to an
b7ebcab7 674error in the evaluation of its contents, any function definitions or
bfe721d1
KH
675@code{provide} calls that occurred during the load are undone.
676@xref{Autoload}.
83ac6b45
RS
677@end defun
678
b6954afd 679@defun require feature &optional filename noerror
83ac6b45 680This function checks whether @var{feature} is present in the current
f9f59935
RS
681Emacs session (using @code{(featurep @var{feature})}; see below). The
682argument @var{feature} must be a symbol.
683
684If the feature is not present, then @code{require} loads @var{filename}
685with @code{load}. If @var{filename} is not supplied, then the name of
686the symbol @var{feature} is used as the base file name to load.
687However, in this case, @code{require} insists on finding @var{feature}
688with an added suffix; a file whose name is just @var{feature} won't be
689used.
83ac6b45 690
d112d11f
RS
691If @var{noerror} is non-@code{nil}, that suppresses errors from actual
692loading of the file. In that case, @code{require} returns @code{nil}
693if loading the file fails. Normally, @code{require} returns
694@var{feature}.
695
696If loading the file succeeds but does not provide @var{feature},
697@code{require} signals an error, @samp{Required feature @var{feature}
698was not provided}.
83ac6b45
RS
699@end defun
700
f2aa473a 701@defun featurep feature &optional subfeature
15bc2f77
RS
702This function returns @code{t} if @var{feature} has been provided in
703the current Emacs session (i.e.@:, if @var{feature} is a member of
704@code{features}.) If @var{subfeature} is non-@code{nil}, then the
705function returns @code{t} only if that subfeature is provided as well
706(i.e.@: if @var{subfeature} is a member of the @code{subfeature}
707property of the @var{feature} symbol.)
83ac6b45
RS
708@end defun
709
710@defvar features
711The value of this variable is a list of symbols that are the features
712loaded in the current Emacs session. Each symbol was put in this list
713with a call to @code{provide}. The order of the elements in the
714@code{features} list is not significant.
715@end defvar
716
717@node Unloading
718@section Unloading
719@cindex unloading
720
721@c Emacs 19 feature
722 You can discard the functions and variables loaded by a library to
723reclaim memory for other Lisp objects. To do this, use the function
724@code{unload-feature}:
725
ee6bcc94 726@deffn Command unload-feature feature &optional force
83ac6b45 727This command unloads the library that provided feature @var{feature}.
78c71a98 728It undefines all functions, macros, and variables defined in that
969fe9b5
RS
729library with @code{defun}, @code{defalias}, @code{defsubst},
730@code{defmacro}, @code{defconst}, @code{defvar}, and @code{defcustom}.
731It then restores any autoloads formerly associated with those symbols.
732(Loading saves these in the @code{autoload} property of the symbol.)
ee6bcc94 733
0373c25e 734@vindex unload-feature-special-hooks
6582d61e
RS
735Before restoring the previous definitions, @code{unload-feature} runs
736@code{remove-hook} to remove functions in the library from certain
0373c25e
RS
737hooks. These hooks include variables whose names end in @samp{hook}
738or @samp{-hooks}, plus those listed in
739@code{unload-feature-special-hooks}. This is to prevent Emacs from
740ceasing to function because important hooks refer to functions that
741are no longer defined.
6582d61e
RS
742
743@vindex @var{feature}-unload-hook
744If these measures are not sufficient to prevent malfunction, a library
745can define an explicit unload hook. If @code{@var{feature}-unload-hook}
746is defined, it is run as a normal hook before restoring the previous
747definitions, @emph{instead of} the usual hook-removing actions. The
748unload hook ought to undo all the global state changes made by the
749library that might cease to work once the library is unloaded.
8241495d
RS
750@code{unload-feature} can cause problems with libraries that fail to do
751this, so it should be used with caution.
6582d61e 752
ee6bcc94
RS
753Ordinarily, @code{unload-feature} refuses to unload a library on which
754other loaded libraries depend. (A library @var{a} depends on library
755@var{b} if @var{a} contains a @code{require} for @var{b}.) If the
756optional argument @var{force} is non-@code{nil}, dependencies are
757ignored and you can unload any library.
83ac6b45
RS
758@end deffn
759
760 The @code{unload-feature} function is written in Lisp; its actions are
761based on the variable @code{load-history}.
762
763@defvar load-history
764This variable's value is an alist connecting library names with the
765names of functions and variables they define, the features they provide,
766and the features they require.
767
768Each element is a list and describes one library. The @sc{car} of the
c80d2280
RS
769list is the name of the library, as a string. The rest of the list
770elements have these forms:
771
772@table @code
773@item @var{fun}
774The function @var{fun} was defined by this library.
775@item (t . @var{fun})
776The function @var{fun} was previously an autoload before this library
777redefined it as a function. The following element is always the
778symbol @var{fun}, which signifies that the library defined @var{fun}
779as a function.
780@item (autoload . @var{fun})
781The function @var{fun} was defined as an autoload.
782@item (defvar . @var{var})
783The symbol @var{var} was defined as a variable.
784@item (require . @var{feature})
785The feature @var{feature} was required.
786@item (provide . @var{feature})
787The feature @var{feature} was provided.
5b1fc152 788@end table
83ac6b45
RS
789
790The value of @code{load-history} may have one element whose @sc{car} is
791@code{nil}. This element describes definitions made with
792@code{eval-buffer} on a buffer that is not visiting a file.
793@end defvar
794
795 The command @code{eval-region} updates @code{load-history}, but does so
796by adding the symbols defined to the element for the file being visited,
55607887 797rather than replacing that element. @xref{Eval}.
83ac6b45 798
cf11ad96 799@defvar unload-feature-special-hooks
6582d61e
RS
800This variable holds a list of hooks to be scanned before unloading a
801library, to remove functions defined in the library.
802@end defvar
803
83ac6b45
RS
804@node Hooks for Loading
805@section Hooks for Loading
806@cindex loading hooks
807@cindex hooks for loading
808
809You can ask for code to be executed if and when a particular library is
810loaded, by calling @code{eval-after-load}.
811
812@defun eval-after-load library form
813This function arranges to evaluate @var{form} at the end of loading the
d2e9ee06
RS
814library @var{library}, if and when @var{library} is loaded. If
815@var{library} is already loaded, it evaluates @var{form} right away.
83ac6b45 816
f2aa473a
SM
817If @var{library} is a string, it must exactly match the argument of
818@code{load} used to load the library. To get the proper results when an
819installed library is found by searching @code{load-path}, you should not
820include any directory names in @var{library}.
821
a28b5ba3 822@var{library} can also be a feature (i.e.@: a symbol), in which case
f2aa473a 823@var{form} is evaluated when @code{(provide @var{library})} is called.
83ac6b45
RS
824
825An error in @var{form} does not undo the load, but does prevent
826execution of the rest of @var{form}.
827@end defun
828
d2e9ee06
RS
829In general, well-designed Lisp programs should not use this feature.
830The clean and modular ways to interact with a Lisp library are (1)
831examine and set the library's variables (those which are meant for
cc8c51f1 832outside use), and (2) call the library's functions. If you wish to
d2e9ee06
RS
833do (1), you can do it immediately---there is no need to wait for when
834the library is loaded. To do (2), you must load the library (preferably
835with @code{require}).
836
969fe9b5
RS
837But it is OK to use @code{eval-after-load} in your personal
838customizations if you don't feel they must meet the design standards for
839programs meant for wider use.
d2e9ee06 840
83ac6b45 841@defvar after-load-alist
8241495d
RS
842This variable holds an alist of expressions to evaluate if and when
843particular libraries are loaded. Each element looks like this:
83ac6b45
RS
844
845@example
846(@var{filename} @var{forms}@dots{})
847@end example
848
849The function @code{load} checks @code{after-load-alist} in order to
850implement @code{eval-after-load}.
851@end defvar
852
853@c Emacs 19 feature
ab5796a9
MB
854
855@ignore
856 arch-tag: df731f89-0900-4389-a436-9105241b6f7a
857@end ignore