subrp fix
[bpt/emacs.git] / doc / lispref / loading.texi
CommitLineData
b8d4c8d0
GM
1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual.
ba318903 3@c Copyright (C) 1990-1995, 1998-1999, 2001-2014 Free Software
ab422c4d 4@c Foundation, Inc.
b8d4c8d0 5@c See the file elisp.texi for copying conditions.
ecc6530d 6@node Loading
b8d4c8d0
GM
7@chapter Loading
8@cindex loading
9@cindex library
10@cindex Lisp library
11
6c1e4b46
CY
12 Loading a file of Lisp code means bringing its contents into the
13Lisp environment in the form of Lisp objects. Emacs finds and opens
14the file, reads the text, evaluates each form, and then closes the
15file. Such a file is also called a @dfn{Lisp library}.
b8d4c8d0
GM
16
17 The load functions evaluate all the expressions in a file just
18as the @code{eval-buffer} function evaluates all the
19expressions in a buffer. The difference is that the load functions
20read and evaluate the text in the file as found on disk, not the text
21in an Emacs buffer.
22
23@cindex top-level form
24 The loaded file must contain Lisp expressions, either as source code
25or as byte-compiled code. Each form in the file is called a
26@dfn{top-level form}. There is no special format for the forms in a
27loadable file; any form in a file may equally well be typed directly
28into a buffer and evaluated there. (Indeed, most code is tested this
29way.) Most often, the forms are function definitions and variable
30definitions.
31
a6e3a5d5
XF
32For on-demand loading of external libraries, @pxref{Dynamic Libraries}.
33
b8d4c8d0
GM
34@menu
35* How Programs Do Loading:: The @code{load} function and others.
36* Load Suffixes:: Details about the suffixes that @code{load} tries.
37* Library Search:: Finding a library to load.
38* Loading Non-ASCII:: Non-@acronym{ASCII} characters in Emacs Lisp files.
39* Autoload:: Setting up a function to autoload.
40* Repeated Loading:: Precautions about loading a file twice.
41* Named Features:: Loading a library if it isn't already loaded.
42* Where Defined:: Finding which file defined a certain symbol.
d24880de
GM
43* Unloading:: How to "unload" a library that was loaded.
44* Hooks for Loading:: Providing code to be run when
45 particular libraries are loaded.
b8d4c8d0
GM
46@end menu
47
48@node How Programs Do Loading
49@section How Programs Do Loading
50
51 Emacs Lisp has several interfaces for loading. For example,
52@code{autoload} creates a placeholder object for a function defined in a
53file; trying to call the autoloading function loads the file to get the
54function's real definition (@pxref{Autoload}). @code{require} loads a
55file if it isn't already loaded (@pxref{Named Features}). Ultimately,
56all these facilities call the @code{load} function to do the work.
57
58@defun load filename &optional missing-ok nomessage nosuffix must-suffix
59This function finds and opens a file of Lisp code, evaluates all the
60forms in it, and closes the file.
61
62To find the file, @code{load} first looks for a file named
63@file{@var{filename}.elc}, that is, for a file whose name is
64@var{filename} with the extension @samp{.elc} appended. If such a
65file exists, it is loaded. If there is no file by that name, then
66@code{load} looks for a file named @file{@var{filename}.el}. If that
67file exists, it is loaded. Finally, if neither of those names is
68found, @code{load} looks for a file named @var{filename} with nothing
69appended, and loads it if it exists. (The @code{load} function is not
70clever about looking at @var{filename}. In the perverse case of a
71file named @file{foo.el.el}, evaluation of @code{(load "foo.el")} will
72indeed find it.)
73
74If Auto Compression mode is enabled, as it is by default, then if
75@code{load} can not find a file, it searches for a compressed version
76of the file before trying other file names. It decompresses and loads
77it if it exists. It looks for compressed versions by appending each
78of the suffixes in @code{jka-compr-load-suffixes} to the file name.
79The value of this variable must be a list of strings. Its standard
80value is @code{(".gz")}.
81
82If the optional argument @var{nosuffix} is non-@code{nil}, then
83@code{load} does not try the suffixes @samp{.elc} and @samp{.el}. In
84this case, you must specify the precise file name you want, except
85that, if Auto Compression mode is enabled, @code{load} will still use
86@code{jka-compr-load-suffixes} to find compressed versions. By
87specifying the precise file name and using @code{t} for
6c1e4b46
CY
88@var{nosuffix}, you can prevent file names like @file{foo.el.el} from
89being tried.
b8d4c8d0
GM
90
91If the optional argument @var{must-suffix} is non-@code{nil}, then
92@code{load} insists that the file name used must end in either
93@samp{.el} or @samp{.elc} (possibly extended with a compression
94suffix), unless it contains an explicit directory name.
95
aec3bf9f
GM
96If the option @code{load-prefer-newer} is non-@code{nil}, then when
97searching suffixes, @code{load} selects whichever version of a file
98(@samp{.elc}, @samp{.el}, etc.) has been modified most recently.
99
b8d4c8d0
GM
100If @var{filename} is a relative file name, such as @file{foo} or
101@file{baz/foo.bar}, @code{load} searches for the file using the variable
102@code{load-path}. It appends @var{filename} to each of the directories
103listed in @code{load-path}, and loads the first file it finds whose name
104matches. The current default directory is tried only if it is specified
105in @code{load-path}, where @code{nil} stands for the default directory.
106@code{load} tries all three possible suffixes in the first directory in
107@code{load-path}, then all three suffixes in the second directory, and
108so on. @xref{Library Search}.
109
c7926fe2
EZ
110Whatever the name under which the file is eventually found, and the
111directory where Emacs found it, Emacs sets the value of the variable
112@code{load-file-name} to that file's name.
113
b8d4c8d0
GM
114If you get a warning that @file{foo.elc} is older than @file{foo.el}, it
115means you should consider recompiling @file{foo.el}. @xref{Byte
116Compilation}.
117
118When loading a source file (not compiled), @code{load} performs
119character set translation just as Emacs would do when visiting the file.
120@xref{Coding Systems}.
121
7351b73d
GM
122@c This is referred to from the Macros chapter.
123@c Not sure if it should be the other way round.
124@cindex eager macro expansion
125When loading an uncompiled file, Emacs tries to expand any macros
126that the file contains (@pxref{Macros}). We refer to this as
127@dfn{eager macro expansion}. Doing this (rather than deferring
128the expansion until the relevant code runs) can significantly speed
129up the execution of uncompiled code. Sometimes, this macro expansion
130cannot be done, owing to a cyclic dependency. In the simplest
131example of this, the file you are loading refers to a macro defined
132in another file, and that file in turn requires the file you are
133loading. This is generally harmless. Emacs prints a warning
134(@samp{Eager macro-expansion skipped due to cycle@dots{}})
135giving details of the problem, but it still loads the file, just
136leaving the macro unexpanded for now. You may wish to restructure
137your code so that this does not happen. Loading a compiled file does
138not cause macroexpansion, because this should already have happened
139during compilation. @xref{Compiling Macros}.
140
b8d4c8d0
GM
141Messages like @samp{Loading foo...} and @samp{Loading foo...done} appear
142in the echo area during loading unless @var{nomessage} is
143non-@code{nil}.
144
145@cindex load errors
146Any unhandled errors while loading a file terminate loading. If the
147load was done for the sake of @code{autoload}, any function definitions
148made during the loading are undone.
149
150@kindex file-error
151If @code{load} can't find the file to load, then normally it signals the
152error @code{file-error} (with @samp{Cannot open load file
153@var{filename}}). But if @var{missing-ok} is non-@code{nil}, then
154@code{load} just returns @code{nil}.
155
156You can use the variable @code{load-read-function} to specify a function
157for @code{load} to use instead of @code{read} for reading expressions.
158See below.
159
160@code{load} returns @code{t} if the file loads successfully.
161@end defun
162
163@deffn Command load-file filename
164This command loads the file @var{filename}. If @var{filename} is a
165relative file name, then the current default directory is assumed.
166This command does not use @code{load-path}, and does not append
167suffixes. However, it does look for compressed versions (if Auto
168Compression Mode is enabled). Use this command if you wish to specify
169precisely the file name to load.
170@end deffn
171
172@deffn Command load-library library
173This command loads the library named @var{library}. It is equivalent to
f6de8a37
CY
174@code{load}, except for the way it reads its argument interactively.
175@xref{Lisp Libraries,,,emacs, The GNU Emacs Manual}.
b8d4c8d0
GM
176@end deffn
177
178@defvar load-in-progress
179This variable is non-@code{nil} if Emacs is in the process of loading a
180file, and it is @code{nil} otherwise.
181@end defvar
182
c7926fe2
EZ
183@defvar load-file-name
184When Emacs is in the process of loading a file, this variable's value
185is the name of that file, as Emacs found it during the search
186described earlier in this section.
187@end defvar
188
b8d4c8d0
GM
189@defvar load-read-function
190@anchor{Definition of load-read-function}
191@c do not allow page break at anchor; work around Texinfo deficiency.
192This variable specifies an alternate expression-reading function for
193@code{load} and @code{eval-region} to use instead of @code{read}.
194The function should accept one argument, just as @code{read} does.
195
196Normally, the variable's value is @code{nil}, which means those
197functions should use @code{read}.
198
199Instead of using this variable, it is cleaner to use another, newer
200feature: to pass the function as the @var{read-function} argument to
201@code{eval-region}. @xref{Definition of eval-region,, Eval}.
202@end defvar
203
204 For information about how @code{load} is used in building Emacs, see
205@ref{Building Emacs}.
206
207@node Load Suffixes
208@section Load Suffixes
209We now describe some technical details about the exact suffixes that
210@code{load} tries.
211
212@defvar load-suffixes
213This is a list of suffixes indicating (compiled or source) Emacs Lisp
214files. It should not include the empty string. @code{load} uses
215these suffixes in order when it appends Lisp suffixes to the specified
216file name. The standard value is @code{(".elc" ".el")} which produces
217the behavior described in the previous section.
218@end defvar
219
220@defvar load-file-rep-suffixes
221This is a list of suffixes that indicate representations of the same
222file. This list should normally start with the empty string.
223When @code{load} searches for a file it appends the suffixes in this
224list, in order, to the file name, before searching for another file.
225
226Enabling Auto Compression mode appends the suffixes in
227@code{jka-compr-load-suffixes} to this list and disabling Auto
228Compression mode removes them again. The standard value of
229@code{load-file-rep-suffixes} if Auto Compression mode is disabled is
230@code{("")}. Given that the standard value of
231@code{jka-compr-load-suffixes} is @code{(".gz")}, the standard value
232of @code{load-file-rep-suffixes} if Auto Compression mode is enabled
233is @code{("" ".gz")}.
234@end defvar
235
236@defun get-load-suffixes
237This function returns the list of all suffixes that @code{load} should
238try, in order, when its @var{must-suffix} argument is non-@code{nil}.
239This takes both @code{load-suffixes} and @code{load-file-rep-suffixes}
240into account. If @code{load-suffixes}, @code{jka-compr-load-suffixes}
241and @code{load-file-rep-suffixes} all have their standard values, this
242function returns @code{(".elc" ".elc.gz" ".el" ".el.gz")} if Auto
243Compression mode is enabled and @code{(".elc" ".el")} if Auto
244Compression mode is disabled.
245@end defun
246
247To summarize, @code{load} normally first tries the suffixes in the
248value of @code{(get-load-suffixes)} and then those in
249@code{load-file-rep-suffixes}. If @var{nosuffix} is non-@code{nil},
250it skips the former group, and if @var{must-suffix} is non-@code{nil},
251it skips the latter group.
252
aec3bf9f
GM
253@defopt load-prefer-newer
254If this option is non-@code{nil}, then rather than stopping at the
255first suffix that exists, @code{load} tests them all, and uses
256whichever file is the newest.
257@end defopt
258
b8d4c8d0
GM
259@node Library Search
260@section Library Search
261@cindex library search
262@cindex find library
263
264 When Emacs loads a Lisp library, it searches for the library
265in a list of directories specified by the variable @code{load-path}.
266
6c1e4b46 267@defvar load-path
b8d4c8d0
GM
268The value of this variable is a list of directories to search when
269loading files with @code{load}. Each element is a string (which must be
270a directory name) or @code{nil} (which stands for the current working
271directory).
6c1e4b46 272@end defvar
b8d4c8d0 273
90794abb
GM
274 When Emacs starts up, it sets up the value of @code{load-path}
275in several steps. First, it initializes @code{load-path} using
276default locations set when Emacs was compiled. Normally, this
277is a directory something like
b8d4c8d0 278
ddff3351 279@example
90794abb 280"/usr/local/share/emacs/@var{version}/lisp"
ddff3351 281@end example
b8d4c8d0 282
88fa559d
GM
283(In this and the following examples, replace @file{/usr/local} with
284the installation prefix appropriate for your Emacs.)
285These directories contain the standard Lisp files that come with
286Emacs. If Emacs cannot find them, it will not start correctly.
b8d4c8d0 287
90794abb
GM
288If you run Emacs from the directory where it was built---that is, an
289executable that has not been formally installed---Emacs instead
cb6c95a3
GM
290initializes @code{load-path} using the @file{lisp}
291directory in the directory containing the sources from which it
292was built.
293@c Though there should be no *.el files in builddir/lisp, so it's pointless.
294If you built Emacs in a separate directory from the
295sources, it also adds the lisp directories from the build directory.
90794abb 296(In all cases, elements are represented as absolute file names.)
b8d4c8d0 297
ab4c47d3 298@cindex site-lisp directories
90794abb
GM
299Unless you start Emacs with the @option{--no-site-lisp} option,
300it then adds two more @file{site-lisp} directories to the front of
301@code{load-path}. These are intended for locally installed Lisp files,
302and are normally of the form:
b8d4c8d0 303
ddff3351 304@example
b8d4c8d0 305"/usr/local/share/emacs/@var{version}/site-lisp"
ddff3351 306@end example
b8d4c8d0
GM
307
308@noindent
309and
310
ddff3351 311@example
b8d4c8d0 312"/usr/local/share/emacs/site-lisp"
ddff3351 313@end example
b8d4c8d0
GM
314
315@noindent
90794abb
GM
316The first one is for locally installed files for a specific Emacs
317version; the second is for locally installed files meant for use
318with all installed Emacs versions. (If Emacs is running uninstalled,
319it also adds @file{site-lisp} directories from the source and build
320directories, if they exist. Normally these directories do not contain
321@file{site-lisp} directories.)
322
323@cindex @env{EMACSLOADPATH} environment variable
17e0445b
GM
324If the environment variable @env{EMACSLOADPATH} is set, it modifies
325the above initialization procedure. Emacs initializes
326@code{load-path} based on the value of the environment variable.
90794abb
GM
327
328The syntax of @env{EMACSLOADPATH} is the same as used for @code{PATH};
329directory names are separated by @samp{:} (or @samp{;}, on some
17e0445b
GM
330operating systems).
331@ignore
332@c AFAICS, does not (yet) work right to specify non-absolute elements.
333and @samp{.} stands for the current default directory.
334@end ignore
335Here is an example of how to set @env{EMACSLOADPATH} variable (from a
336@command{sh}-style shell):
90794abb
GM
337
338@example
17e0445b 339export EMACSLOADPATH=/home/foo/.emacs.d/lisp:
90794abb
GM
340@end example
341
17e0445b
GM
342An empty element in the value of the environment variable, whether
343trailing (as in the above example), leading, or embedded, is replaced
344by the default value of @code{load-path} as determined by the standard
345initialization procedure. If there are no such empty elements, then
346@env{EMACSLOADPATH} specifies the entire @code{load-path}. You must
347include either an empty element, or the explicit path to the directory
348containing the standard Lisp files, else Emacs will not function.
349(Another way to modify @code{load-path} is to use the @option{-L}
350command-line option when starting Emacs; see below.)
351
90794abb
GM
352 For each directory in @code{load-path}, Emacs then checks to see if
353it contains a file @file{subdirs.el}, and if so, loads it. The
354@file{subdirs.el} file is created when Emacs is built/installed,
355and contains code that causes Emacs to add any subdirectories of those
356directories to @code{load-path}. Both immediate subdirectories and
357subdirectories multiple levels down are added. But it excludes
358subdirectories whose names do not start with a letter or digit, and
359subdirectories named @file{RCS} or @file{CVS}, and subdirectories
360containing a file named @file{.nosearch}.
361
362 Next, Emacs adds any extra load directories that you specify using the
363@option{-L} command-line option (@pxref{Action Arguments,,,emacs, The
6c1e4b46
CY
364GNU Emacs Manual}). It also adds the directories where optional
365packages are installed, if any (@pxref{Packaging Basics}).
366
367 It is common to add code to one's init file (@pxref{Init File}) to
368add one or more directories to @code{load-path}. For example:
369
ddff3351 370@example
6c1e4b46 371(push "~/.emacs.d/lisp" load-path)
ddff3351 372@end example
6c1e4b46 373
06a4f110
GM
374 Dumping Emacs uses a special value of @code{load-path}. If you use
375a @file{site-load.el} or @file{site-init.el} file to customize the
376dumped Emacs (@pxref{Building Emacs}), any changes to @code{load-path}
377that these files make will be lost after dumping.
6c1e4b46 378
b8d4c8d0
GM
379@deffn Command locate-library library &optional nosuffix path interactive-call
380This command finds the precise file name for library @var{library}. It
381searches for the library in the same way @code{load} does, and the
382argument @var{nosuffix} has the same meaning as in @code{load}: don't
383add suffixes @samp{.elc} or @samp{.el} to the specified name
384@var{library}.
385
386If the @var{path} is non-@code{nil}, that list of directories is used
387instead of @code{load-path}.
388
389When @code{locate-library} is called from a program, it returns the file
390name as a string. When the user runs @code{locate-library}
391interactively, the argument @var{interactive-call} is @code{t}, and this
392tells @code{locate-library} to display the file name in the echo area.
393@end deffn
394
e6cf7a82
CY
395@cindex shadowed Lisp files
396@deffn Command list-load-path-shadows &optional stringp
397This command shows a list of @dfn{shadowed} Emacs Lisp files. A
398shadowed file is one that will not normally be loaded, despite being
399in a directory on @code{load-path}, due to the existence of another
400similarly-named file in a directory earlier on @code{load-path}.
401
402For instance, suppose @code{load-path} is set to
403
ddff3351 404@example
e6cf7a82 405 ("/opt/emacs/site-lisp" "/usr/share/emacs/23.3/lisp")
ddff3351 406@end example
e6cf7a82
CY
407
408@noindent
409and that both these directories contain a file named @file{foo.el}.
410Then @code{(require 'foo)} never loads the file in the second
411directory. Such a situation might indicate a problem in the way Emacs
412was installed.
413
414When called from Lisp, this function prints a message listing the
415shadowed files, instead of displaying them in a buffer. If the
416optional argument @code{stringp} is non-@code{nil}, it instead returns
417the shadowed files as a string.
418@end deffn
419
b8d4c8d0
GM
420@node Loading Non-ASCII
421@section Loading Non-@acronym{ASCII} Characters
422
423 When Emacs Lisp programs contain string constants with non-@acronym{ASCII}
424characters, these can be represented within Emacs either as unibyte
425strings or as multibyte strings (@pxref{Text Representations}). Which
426representation is used depends on how the file is read into Emacs. If
427it is read with decoding into multibyte representation, the text of the
428Lisp program will be multibyte text, and its string constants will be
429multibyte strings. If a file containing Latin-1 characters (for
430example) is read without decoding, the text of the program will be
431unibyte text, and its string constants will be unibyte strings.
432@xref{Coding Systems}.
433
6c1e4b46
CY
434 In most Emacs Lisp programs, the fact that non-@acronym{ASCII}
435strings are multibyte strings should not be noticeable, since
436inserting them in unibyte buffers converts them to unibyte
437automatically. However, if this does make a difference, you can force
438a particular Lisp file to be interpreted as unibyte by writing
b8a82b69 439@samp{coding: raw-text} in a local variables section. With
6c1e4b46 440that designator, the file will unconditionally be interpreted as
51b1e059
GM
441unibyte. This can matter when making keybindings to
442non-@acronym{ASCII} characters written as @code{?v@var{literal}}.
b8d4c8d0
GM
443
444@node Autoload
445@section Autoload
446@cindex autoload
447
48de8b12
CY
448 The @dfn{autoload} facility lets you register the existence of a
449function or macro, but put off loading the file that defines it. The
450first call to the function automatically loads the proper library, in
6c1e4b46
CY
451order to install the real definition and other associated code, then
452runs the real definition as if it had been loaded all along.
48de8b12
CY
453Autoloading can also be triggered by looking up the documentation of
454the function or macro (@pxref{Documentation Basics}).
b8d4c8d0
GM
455
456 There are two ways to set up an autoloaded function: by calling
457@code{autoload}, and by writing a special ``magic'' comment in the
458source before the real definition. @code{autoload} is the low-level
459primitive for autoloading; any Lisp program can call @code{autoload} at
460any time. Magic comments are the most convenient way to make a function
461autoload, for packages installed along with Emacs. These comments do
462nothing on their own, but they serve as a guide for the command
463@code{update-file-autoloads}, which constructs calls to @code{autoload}
464and arranges to execute them when Emacs is built.
465
466@defun autoload function filename &optional docstring interactive type
467This function defines the function (or macro) named @var{function} so as
468to load automatically from @var{filename}. The string @var{filename}
469specifies the file to load to get the real definition of @var{function}.
470
471If @var{filename} does not contain either a directory name, or the
e29e39c9
CY
472suffix @code{.el} or @code{.elc}, this function insists on adding one
473of these suffixes, and it will not load from a file whose name is just
474@var{filename} with no added suffix. (The variable
b8d4c8d0
GM
475@code{load-suffixes} specifies the exact required suffixes.)
476
477The argument @var{docstring} is the documentation string for the
478function. Specifying the documentation string in the call to
479@code{autoload} makes it possible to look at the documentation without
480loading the function's real definition. Normally, this should be
481identical to the documentation string in the function definition
482itself. If it isn't, the function definition's documentation string
483takes effect when it is loaded.
484
485If @var{interactive} is non-@code{nil}, that says @var{function} can be
486called interactively. This lets completion in @kbd{M-x} work without
487loading @var{function}'s real definition. The complete interactive
488specification is not given here; it's not needed unless the user
489actually calls @var{function}, and when that happens, it's time to load
490the real definition.
491
492You can autoload macros and keymaps as well as ordinary functions.
493Specify @var{type} as @code{macro} if @var{function} is really a macro.
494Specify @var{type} as @code{keymap} if @var{function} is really a
495keymap. Various parts of Emacs need to know this information without
496loading the real definition.
497
498An autoloaded keymap loads automatically during key lookup when a prefix
499key's binding is the symbol @var{function}. Autoloading does not occur
500for other kinds of access to the keymap. In particular, it does not
501happen when a Lisp program gets the keymap from the value of a variable
502and calls @code{define-key}; not even if the variable name is the same
503symbol @var{function}.
504
505@cindex function cell in autoload
e8b3825d 506If @var{function} already has a non-void function definition that is not
e29e39c9
CY
507an autoload object, this function does nothing and returns @code{nil}.
508Otherwise, it constructs an autoload object (@pxref{Autoload Type}),
509and stores it as the function definition for @var{function}. The
510autoload object has this form:
b8d4c8d0
GM
511
512@example
513(autoload @var{filename} @var{docstring} @var{interactive} @var{type})
514@end example
515
516For example,
517
518@example
519@group
520(symbol-function 'run-prolog)
521 @result{} (autoload "prolog" 169681 t nil)
522@end group
523@end example
524
525@noindent
526In this case, @code{"prolog"} is the name of the file to load, 169681
527refers to the documentation string in the
6e911150 528@file{emacs/etc/DOC} file (@pxref{Documentation Basics}),
b8d4c8d0
GM
529@code{t} means the function is interactive, and @code{nil} that it is
530not a macro or a keymap.
531@end defun
532
e29e39c9
CY
533@defun autoloadp object
534This function returns non-@code{nil} if @var{object} is an autoload
535object. For example, to check if @code{run-prolog} is defined as an
536autoloaded function, evaluate
537
538@smallexample
539(autoloadp (symbol-function 'run-prolog))
540@end smallexample
541@end defun
542
b8d4c8d0
GM
543@cindex autoload errors
544 The autoloaded file usually contains other definitions and may require
545or provide one or more features. If the file is not completely loaded
546(due to an error in the evaluation of its contents), any function
547definitions or @code{provide} calls that occurred during the load are
548undone. This is to ensure that the next attempt to call any function
549autoloading from this file will try again to load the file. If not for
550this, then some of the functions in the file might be defined by the
551aborted load, but fail to work properly for the lack of certain
552subroutines not loaded successfully because they come later in the file.
553
554 If the autoloaded file fails to define the desired Lisp function or
555macro, then an error is signaled with data @code{"Autoloading failed to
556define function @var{function-name}"}.
557
558@findex update-file-autoloads
559@findex update-directory-autoloads
560@cindex magic autoload comment
561@cindex autoload cookie
562@anchor{autoload cookie}
563 A magic autoload comment (often called an @dfn{autoload cookie})
564consists of @samp{;;;###autoload}, on a line by itself,
565just before the real definition of the function in its
566autoloadable source file. The command @kbd{M-x update-file-autoloads}
567writes a corresponding @code{autoload} call into @file{loaddefs.el}.
b8afe7e4
EZ
568(The string that serves as the autoload cookie and the name of the
569file generated by @code{update-file-autoloads} can be changed from the
570above defaults, see below.)
b8d4c8d0
GM
571Building Emacs loads @file{loaddefs.el} and thus calls @code{autoload}.
572@kbd{M-x update-directory-autoloads} is even more powerful; it updates
573autoloads for all files in the current directory.
574
575 The same magic comment can copy any kind of form into
bc44be50
CY
576@file{loaddefs.el}. The form following the magic comment is copied
577verbatim, @emph{except} if it is one of the forms which the autoload
1df7defd 578facility handles specially (e.g., by conversion into an
bc44be50
CY
579@code{autoload} call). The forms which are not copied verbatim are
580the following:
581
582@table @asis
583@item Definitions for function or function-like objects:
0d8e94e9
GM
584@code{defun} and @code{defmacro}; also @code{cl-defun} and
585@code{cl-defmacro} (@pxref{Argument Lists,,,cl,Common Lisp Extensions}),
586and @code{define-overloadable-function} (see the commentary in
bc44be50
CY
587@file{mode-local.el}).
588
589@item Definitions for major or minor modes:
7eac3782 590@code{define-minor-mode}, @code{define-globalized-minor-mode},
84f4a531
CY
591@code{define-generic-mode}, @code{define-derived-mode},
592@code{easy-mmode-define-minor-mode},
7eac3782 593@code{easy-mmode-define-global-mode}, @code{define-compilation-mode},
84f4a531 594and @code{define-global-minor-mode}.
bc44be50
CY
595
596@item Other definition types:
597@code{defcustom}, @code{defgroup}, @code{defclass}
598(@pxref{Top,EIEIO,,eieio,EIEIO}), and @code{define-skeleton} (see the
599commentary in @file{skeleton.el}).
600@end table
b8d4c8d0
GM
601
602 You can also use a magic comment to execute a form at build time
603@emph{without} executing it when the file itself is loaded. To do this,
604write the form @emph{on the same line} as the magic comment. Since it
605is in a comment, it does nothing when you load the source file; but
606@kbd{M-x update-file-autoloads} copies it to @file{loaddefs.el}, where
607it is executed while building Emacs.
608
609 The following example shows how @code{doctor} is prepared for
610autoloading with a magic comment:
611
ddff3351 612@example
b8d4c8d0
GM
613;;;###autoload
614(defun doctor ()
615 "Switch to *doctor* buffer and start giving psychotherapy."
616 (interactive)
617 (switch-to-buffer "*doctor*")
618 (doctor-mode))
ddff3351 619@end example
b8d4c8d0
GM
620
621@noindent
622Here's what that produces in @file{loaddefs.el}:
623
ddff3351 624@example
b8d4c8d0
GM
625(autoload (quote doctor) "doctor" "\
626Switch to *doctor* buffer and start giving psychotherapy.
627
628\(fn)" t nil)
ddff3351 629@end example
b8d4c8d0
GM
630
631@noindent
632@cindex @code{fn} in function's documentation string
633The backslash and newline immediately following the double-quote are a
634convention used only in the preloaded uncompiled Lisp files such as
635@file{loaddefs.el}; they tell @code{make-docfile} to put the
636documentation string in the @file{etc/DOC} file. @xref{Building Emacs}.
637See also the commentary in @file{lib-src/make-docfile.c}. @samp{(fn)}
638in the usage part of the documentation string is replaced with the
639function's name when the various help functions (@pxref{Help
640Functions}) display it.
641
642 If you write a function definition with an unusual macro that is not
643one of the known and recognized function definition methods, use of an
644ordinary magic autoload comment would copy the whole definition into
645@code{loaddefs.el}. That is not desirable. You can put the desired
646@code{autoload} call into @code{loaddefs.el} instead by writing this:
647
ddff3351 648@example
b8d4c8d0
GM
649;;;###autoload (autoload 'foo "myfile")
650(mydefunmacro foo
651 ...)
ddff3351 652@end example
b8d4c8d0 653
b8afe7e4
EZ
654 You can use a non-default string as the autoload cookie and have the
655corresponding autoload calls written into a file whose name is
656different from the default @file{loaddefs.el}. Emacs provides two
657variables to control this:
658
659@defvar generate-autoload-cookie
660The value of this variable should be a string whose syntax is a Lisp
661comment. @kbd{M-x update-file-autoloads} copies the Lisp form that
662follows the cookie into the autoload file it generates. The default
663value of this variable is @code{";;;###autoload"}.
664@end defvar
665
666@defvar generated-autoload-file
667The value of this variable names an Emacs Lisp file where the autoload
668calls should go. The default value is @file{loaddefs.el}, but you can
669override that, e.g., in the ``Local Variables'' section of a
670@file{.el} file (@pxref{File Local Variables}). The autoload file is
671assumed to contain a trailer starting with a formfeed character.
672@end defvar
673
e29e39c9
CY
674 The following function may be used to explicitly load the library
675specified by an autoload object:
676
677@defun autoload-do-load autoload &optional name macro-only
678This function performs the loading specified by @var{autoload}, which
5c6ce1c7 679should be an autoload object. The optional argument @var{name}, if
e29e39c9
CY
680non-@code{nil}, should be a symbol whose function value is
681@var{autoload}; in that case, the return value of this function is the
682symbol's new function value. If the value of the optional argument
683@var{macro-only} is @code{macro}, this function avoids loading a
684function, only a macro.
685@end defun
686
b8d4c8d0
GM
687@node Repeated Loading
688@section Repeated Loading
689@cindex repeated loading
690
691 You can load a given file more than once in an Emacs session. For
692example, after you have rewritten and reinstalled a function definition
693by editing it in a buffer, you may wish to return to the original
694version; you can do this by reloading the file it came from.
695
696 When you load or reload files, bear in mind that the @code{load} and
697@code{load-library} functions automatically load a byte-compiled file
698rather than a non-compiled file of similar name. If you rewrite a file
699that you intend to save and reinstall, you need to byte-compile the new
700version; otherwise Emacs will load the older, byte-compiled file instead
701of your newer, non-compiled file! If that happens, the message
702displayed when loading the file includes, @samp{(compiled; note, source is
703newer)}, to remind you to recompile it.
704
705 When writing the forms in a Lisp library file, keep in mind that the
706file might be loaded more than once. For example, think about whether
707each variable should be reinitialized when you reload the library;
708@code{defvar} does not change the value if the variable is already
709initialized. (@xref{Defining Variables}.)
710
711 The simplest way to add an element to an alist is like this:
712
713@example
714(push '(leif-mode " Leif") minor-mode-alist)
715@end example
716
717@noindent
dc401175
CY
718But this would add multiple elements if the library is reloaded. To
719avoid the problem, use @code{add-to-list} (@pxref{List Variables}):
b8d4c8d0
GM
720
721@example
9af167bc 722(add-to-list 'minor-mode-alist '(leif-mode " Leif"))
b8d4c8d0
GM
723@end example
724
725 Occasionally you will want to test explicitly whether a library has
dc401175
CY
726already been loaded. If the library uses @code{provide} to provide a
727named feature, you can use @code{featurep} earlier in the file to test
728whether the @code{provide} call has been executed before (@pxref{Named
729Features}). Alternatively, you could use something like this:
b8d4c8d0
GM
730
731@example
732(defvar foo-was-loaded nil)
733
734(unless foo-was-loaded
735 @var{execute-first-time-only}
736 (setq foo-was-loaded t))
737@end example
738
739@noindent
b8d4c8d0
GM
740
741@node Named Features
742@section Features
743@cindex features
744@cindex requiring features
745@cindex providing features
746
747 @code{provide} and @code{require} are an alternative to
748@code{autoload} for loading files automatically. They work in terms of
749named @dfn{features}. Autoloading is triggered by calling a specific
750function, but a feature is loaded the first time another program asks
751for it by name.
752
753 A feature name is a symbol that stands for a collection of functions,
754variables, etc. The file that defines them should @dfn{provide} the
755feature. Another program that uses them may ensure they are defined by
756@dfn{requiring} the feature. This loads the file of definitions if it
757hasn't been loaded already.
758
dc401175 759@cindex load error with require
b8d4c8d0
GM
760 To require the presence of a feature, call @code{require} with the
761feature name as argument. @code{require} looks in the global variable
762@code{features} to see whether the desired feature has been provided
763already. If not, it loads the feature from the appropriate file. This
764file should call @code{provide} at the top level to add the feature to
765@code{features}; if it fails to do so, @code{require} signals an error.
b8d4c8d0 766
14a1f380
GM
767 For example, in @file{idlwave.el}, the definition for
768@code{idlwave-complete-filename} includes the following code:
b8d4c8d0 769
ddff3351 770@example
14a1f380
GM
771(defun idlwave-complete-filename ()
772 "Use the comint stuff to complete a file name."
773 (require 'comint)
cb6f5650 774 (let* ((comint-file-name-chars "~/A-Za-z0-9+@@:_.$#%=@{@}\\-")
14a1f380
GM
775 (comint-completion-addsuffix nil)
776 ...)
777 (comint-dynamic-complete-filename)))
ddff3351 778@end example
b8d4c8d0
GM
779
780@noindent
781The expression @code{(require 'comint)} loads the file @file{comint.el}
14a1f380
GM
782if it has not yet been loaded, ensuring that
783@code{comint-dynamic-complete-filename} is defined. Features are
784normally named after the files that provide them, so that
785@code{require} need not be given the file name. (Note that it is
786important that the @code{require} statement be outside the body of the
787@code{let}. Loading a library while its variables are let-bound can
788have unintended consequences, namely the variables becoming unbound
789after the let exits.)
b8d4c8d0
GM
790
791The @file{comint.el} file contains the following top-level expression:
792
ddff3351 793@example
b8d4c8d0 794(provide 'comint)
ddff3351 795@end example
b8d4c8d0
GM
796
797@noindent
798This adds @code{comint} to the global @code{features} list, so that
799@code{(require 'comint)} will henceforth know that nothing needs to be
800done.
801
802@cindex byte-compiling @code{require}
803 When @code{require} is used at top level in a file, it takes effect
804when you byte-compile that file (@pxref{Byte Compilation}) as well as
805when you load it. This is in case the required package contains macros
5c63cc6b 806that the byte compiler must know about. It also avoids byte compiler
b8d4c8d0
GM
807warnings for functions and variables defined in the file loaded with
808@code{require}.
809
810 Although top-level calls to @code{require} are evaluated during
811byte compilation, @code{provide} calls are not. Therefore, you can
812ensure that a file of definitions is loaded before it is byte-compiled
813by including a @code{provide} followed by a @code{require} for the same
814feature, as in the following example.
815
ddff3351 816@example
b8d4c8d0
GM
817@group
818(provide 'my-feature) ; @r{Ignored by byte compiler,}
819 ; @r{evaluated by @code{load}.}
820(require 'my-feature) ; @r{Evaluated by byte compiler.}
821@end group
ddff3351 822@end example
b8d4c8d0
GM
823
824@noindent
825The compiler ignores the @code{provide}, then processes the
826@code{require} by loading the file in question. Loading the file does
827execute the @code{provide} call, so the subsequent @code{require} call
828does nothing when the file is loaded.
829
830@defun provide feature &optional subfeatures
831This function announces that @var{feature} is now loaded, or being
832loaded, into the current Emacs session. This means that the facilities
833associated with @var{feature} are or will be available for other Lisp
834programs.
835
4c98b9ed
GM
836The direct effect of calling @code{provide} is if not already in
837@var{features} then to add @var{feature} to the front of that list and
838call any @code{eval-after-load} code waiting for it (@pxref{Hooks for
839Loading}). The argument @var{feature} must be a symbol.
840@code{provide} returns @var{feature}.
b8d4c8d0
GM
841
842If provided, @var{subfeatures} should be a list of symbols indicating
843a set of specific subfeatures provided by this version of
844@var{feature}. You can test the presence of a subfeature using
845@code{featurep}. The idea of subfeatures is that you use them when a
846package (which is one @var{feature}) is complex enough to make it
847useful to give names to various parts or functionalities of the
848package, which might or might not be loaded, or might or might not be
849present in a given version. @xref{Network Feature Testing}, for
850an example.
851
ddff3351 852@example
b8d4c8d0
GM
853features
854 @result{} (bar bish)
855
856(provide 'foo)
857 @result{} foo
858features
859 @result{} (foo bar bish)
ddff3351 860@end example
b8d4c8d0
GM
861
862When a file is loaded to satisfy an autoload, and it stops due to an
863error in the evaluation of its contents, any function definitions or
864@code{provide} calls that occurred during the load are undone.
865@xref{Autoload}.
866@end defun
867
868@defun require feature &optional filename noerror
869This function checks whether @var{feature} is present in the current
870Emacs session (using @code{(featurep @var{feature})}; see below). The
871argument @var{feature} must be a symbol.
872
873If the feature is not present, then @code{require} loads @var{filename}
874with @code{load}. If @var{filename} is not supplied, then the name of
875the symbol @var{feature} is used as the base file name to load.
876However, in this case, @code{require} insists on finding @var{feature}
877with an added @samp{.el} or @samp{.elc} suffix (possibly extended with
878a compression suffix); a file whose name is just @var{feature} won't
879be used. (The variable @code{load-suffixes} specifies the exact
880required Lisp suffixes.)
881
882If @var{noerror} is non-@code{nil}, that suppresses errors from actual
883loading of the file. In that case, @code{require} returns @code{nil}
884if loading the file fails. Normally, @code{require} returns
885@var{feature}.
886
887If loading the file succeeds but does not provide @var{feature},
888@code{require} signals an error, @samp{Required feature @var{feature}
889was not provided}.
890@end defun
891
892@defun featurep feature &optional subfeature
893This function returns @code{t} if @var{feature} has been provided in
1df7defd 894the current Emacs session (i.e., if @var{feature} is a member of
b8d4c8d0
GM
895@code{features}.) If @var{subfeature} is non-@code{nil}, then the
896function returns @code{t} only if that subfeature is provided as well
1df7defd 897(i.e., if @var{subfeature} is a member of the @code{subfeature}
b8d4c8d0
GM
898property of the @var{feature} symbol.)
899@end defun
900
901@defvar features
902The value of this variable is a list of symbols that are the features
903loaded in the current Emacs session. Each symbol was put in this list
904with a call to @code{provide}. The order of the elements in the
905@code{features} list is not significant.
906@end defvar
907
908@node Where Defined
909@section Which File Defined a Certain Symbol
910
911@defun symbol-file symbol &optional type
912This function returns the name of the file that defined @var{symbol}.
d632fb82
MR
913If @var{type} is @code{nil}, then any kind of definition is acceptable.
914If @var{type} is @code{defun}, @code{defvar}, or @code{defface}, that
915specifies function definition, variable definition, or face definition
916only.
917
918The value is normally an absolute file name. It can also be @code{nil},
919if the definition is not associated with any file. If @var{symbol}
920specifies an autoloaded function, the value can be a relative file name
921without extension.
b8d4c8d0
GM
922@end defun
923
924 The basis for @code{symbol-file} is the data in the variable
925@code{load-history}.
926
927@defvar load-history
da0bbbc4 928The value of this variable is an alist that associates the names of
4801c5fa
CY
929loaded library files with the names of the functions and variables
930they defined, as well as the features they provided or required.
931
932Each element in this alist describes one loaded library (including
933libraries that are preloaded at startup). It is a list whose @sc{car}
934is the absolute file name of the library (a string). The rest of the
935list elements have these forms:
b8d4c8d0
GM
936
937@table @code
938@item @var{var}
939The symbol @var{var} was defined as a variable.
940@item (defun . @var{fun})
941The function @var{fun} was defined.
942@item (t . @var{fun})
943The function @var{fun} was previously an autoload before this library
944redefined it as a function. The following element is always
945@code{(defun . @var{fun})}, which represents defining @var{fun} as a
946function.
947@item (autoload . @var{fun})
948The function @var{fun} was defined as an autoload.
6a57054b
JB
949@item (defface . @var{face})
950The face @var{face} was defined.
b8d4c8d0
GM
951@item (require . @var{feature})
952The feature @var{feature} was required.
953@item (provide . @var{feature})
954The feature @var{feature} was provided.
955@end table
956
957The value of @code{load-history} may have one element whose @sc{car} is
958@code{nil}. This element describes definitions made with
959@code{eval-buffer} on a buffer that is not visiting a file.
960@end defvar
961
962 The command @code{eval-region} updates @code{load-history}, but does so
963by adding the symbols defined to the element for the file being visited,
964rather than replacing that element. @xref{Eval}.
965
966@node Unloading
967@section Unloading
968@cindex unloading packages
969
970@c Emacs 19 feature
971 You can discard the functions and variables loaded by a library to
972reclaim memory for other Lisp objects. To do this, use the function
973@code{unload-feature}:
974
975@deffn Command unload-feature feature &optional force
976This command unloads the library that provided feature @var{feature}.
977It undefines all functions, macros, and variables defined in that
978library with @code{defun}, @code{defalias}, @code{defsubst},
979@code{defmacro}, @code{defconst}, @code{defvar}, and @code{defcustom}.
980It then restores any autoloads formerly associated with those symbols.
981(Loading saves these in the @code{autoload} property of the symbol.)
982
b8d4c8d0
GM
983Before restoring the previous definitions, @code{unload-feature} runs
984@code{remove-hook} to remove functions in the library from certain
d1069532
SM
985hooks. These hooks include variables whose names end in @samp{-hook}
986(or the deprecated suffix @samp{-hooks}), plus those listed in
0ade8edb
RS
987@code{unload-feature-special-hooks}, as well as
988@code{auto-mode-alist}. This is to prevent Emacs from ceasing to
989function because important hooks refer to functions that are no longer
990defined.
b8d4c8d0 991
0ade8edb
RS
992Standard unloading activities also undoes ELP profiling of functions
993in that library, unprovides any features provided by the library, and
994cancels timers held in variables defined by the library.
995
996@vindex @var{feature}-unload-function
b8d4c8d0 997If these measures are not sufficient to prevent malfunction, a library
0ade8edb
RS
998can define an explicit unloader named @code{@var{feature}-unload-function}.
999If that symbol is defined as a function, @code{unload-feature} calls
1000it with no arguments before doing anything else. It can do whatever
1001is appropriate to unload the library. If it returns @code{nil},
1002@code{unload-feature} proceeds to take the normal unload actions.
1003Otherwise it considers the job to be done.
b8d4c8d0
GM
1004
1005Ordinarily, @code{unload-feature} refuses to unload a library on which
1006other loaded libraries depend. (A library @var{a} depends on library
1007@var{b} if @var{a} contains a @code{require} for @var{b}.) If the
1008optional argument @var{force} is non-@code{nil}, dependencies are
1009ignored and you can unload any library.
1010@end deffn
1011
1012 The @code{unload-feature} function is written in Lisp; its actions are
1013based on the variable @code{load-history}.
1014
1015@defvar unload-feature-special-hooks
1016This variable holds a list of hooks to be scanned before unloading a
1017library, to remove functions defined in the library.
1018@end defvar
1019
1020@node Hooks for Loading
1021@section Hooks for Loading
1022@cindex loading hooks
1023@cindex hooks for loading
1024
c3863713
CY
1025You can ask for code to be executed each time Emacs loads a library,
1026by using the variable @code{after-load-functions}:
1027
1028@defvar after-load-functions
1029This abnormal hook is run after loading a file. Each function in the
1030hook is called with a single argument, the absolute filename of the
1031file that was just loaded.
1032@end defvar
1033
1034If you want code to be executed when a @emph{particular} library is
de0503df 1035loaded, use the macro @code{with-eval-after-load}:
b8d4c8d0 1036
de0503df
SM
1037@defmac with-eval-after-load library body@dots{}
1038This macro arranges to evaluate @var{body} at the end of loading
b8d4c8d0 1039the file @var{library}, each time @var{library} is loaded. If
de0503df 1040@var{library} is already loaded, it evaluates @var{body} right away.
b8d4c8d0
GM
1041
1042You don't need to give a directory or extension in the file name
c3863713 1043@var{library}. Normally, you just give a bare file name, like this:
b8d4c8d0
GM
1044
1045@example
de0503df 1046(with-eval-after-load "edebug" (def-edebug-spec c-point t))
b8d4c8d0
GM
1047@end example
1048
1049To restrict which files can trigger the evaluation, include a
1050directory or an extension or both in @var{library}. Only a file whose
1051absolute true name (i.e., the name with all symbolic links chased out)
1052matches all the given name components will match. In the following
1053example, @file{my_inst.elc} or @file{my_inst.elc.gz} in some directory
1054@code{..../foo/bar} will trigger the evaluation, but not
1055@file{my_inst.el}:
1056
1057@example
de0503df 1058(with-eval-after-load "foo/bar/my_inst.elc" @dots{})
b8d4c8d0
GM
1059@end example
1060
1df7defd 1061@var{library} can also be a feature (i.e., a symbol), in which case
de0503df 1062@var{body} is evaluated at the end of any file where
3fa173b4 1063@code{(provide @var{library})} is called.
b8d4c8d0 1064
de0503df
SM
1065An error in @var{body} does not undo the load, but does prevent
1066execution of the rest of @var{body}.
1067@end defmac
b8d4c8d0 1068
c3863713
CY
1069Normally, well-designed Lisp programs should not use
1070@code{eval-after-load}. If you need to examine and set the variables
1071defined in another library (those meant for outside use), you can do
1072it immediately---there is no need to wait until the library is loaded.
1073If you need to call functions defined by that library, you should load
1074the library, preferably with @code{require} (@pxref{Named Features}).