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