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