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