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