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