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