aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard M. Stallman1994-03-21 17:36:52 +0000
committerRichard M. Stallman1994-03-21 17:36:52 +0000
commit83ac6b4598049a3ccb361064d5fc0d0cee7c5705 (patch)
tree62ffcdb0b648d627299bd88e410c5ccd89401441
parenta0acfc98dc8b718dc08361c5d2723b3a3ccaa8ff (diff)
downloademacs-83ac6b4598049a3ccb361064d5fc0d0cee7c5705.tar.gz
emacs-83ac6b4598049a3ccb361064d5fc0d0cee7c5705.zip
Initial revision
-rw-r--r--lispref/control.texi1136
-rw-r--r--lispref/intro.texi867
-rw-r--r--lispref/loading.texi582
3 files changed, 2585 insertions, 0 deletions
diff --git a/lispref/control.texi b/lispref/control.texi
new file mode 100644
index 00000000000..7fa06693e4d
--- /dev/null
+++ b/lispref/control.texi
@@ -0,0 +1,1136 @@
1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual.
3@c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4@c See the file elisp.texi for copying conditions.
5@setfilename ../info/control
6@node Control Structures, Variables, Evaluation, Top
7@chapter Control Structures
8@cindex special forms for control structures
9@cindex control structures
10
11 A Lisp program consists of expressions or @dfn{forms} (@pxref{Forms}).
12We control the order of execution of the forms by enclosing them in
13@dfn{control structures}. Control structures are special forms which
14control when, whether, or how many times to execute the forms they contain.
15
16 The simplest control structure is sequential execution: first form
17@var{a}, then form @var{b}, and so on. This is what happens when you
18write several forms in succession in the body of a function, or at top
19level in a file of Lisp code---the forms are executed in the order they
20are written. We call this @dfn{textual order}. For example, if a
21function body consists of two forms @var{a} and @var{b}, evaluation of
22the function evaluates first @var{a} and then @var{b}, and the
23function's value is the value of @var{b}.
24
25 Emacs Lisp provides several kinds of control structure, including
26other varieties of sequencing, function calls, conditionals, iteration,
27and (controlled) jumps. The built-in control structures are special
28forms since their subforms are not necessarily evaluated. You can use
29macros to define your own control structure constructs (@pxref{Macros}).
30
31@menu
32* Sequencing:: Evaluation in textual order.
33* Conditionals:: @code{if}, @code{cond}.
34* Combining Conditions:: @code{and}, @code{or}, @code{not}.
35* Iteration:: @code{while} loops.
36* Nonlocal Exits:: Jumping out of a sequence.
37@end menu
38
39@node Sequencing
40@section Sequencing
41
42 Evaluating forms in the order they are written is the most common
43control structure. Sometimes this happens automatically, such as in a
44function body. Elsewhere you must use a control structure construct to
45do this: @code{progn}, the simplest control construct of Lisp.
46
47 A @code{progn} special form looks like this:
48
49@example
50@group
51(progn @var{a} @var{b} @var{c} @dots{})
52@end group
53@end example
54
55@noindent
56and it says to execute the forms @var{a}, @var{b}, @var{c} and so on, in
57that order. These forms are called the body of the @code{progn} form.
58The value of the last form in the body becomes the value of the entire
59@code{progn}.
60
61@cindex implicit @code{progn}
62 In the early days of Lisp, @code{progn} was the only way to execute
63two or more forms in succession and use the value of the last of them.
64But programmers found they often needed to use a @code{progn} in the
65body of a function, where (at that time) only one form was allowed. So
66the body of a function was made into an ``implicit @code{progn}'':
67several forms are allowed just as in the body of an actual @code{progn}.
68Many other control structures likewise contain an implicit @code{progn}.
69As a result, @code{progn} is not used as often as it used to be. It is
70needed now most often inside of an @code{unwind-protect}, @code{and},
71@code{or}, or the @var{else}-part of an @code{if}.
72
73@defspec progn forms@dots{}
74This special form evaluates all of the @var{forms}, in textual
75order, returning the result of the final form.
76
77@example
78@group
79(progn (print "The first form")
80 (print "The second form")
81 (print "The third form"))
82 @print{} "The first form"
83 @print{} "The second form"
84 @print{} "The third form"
85@result{} "The third form"
86@end group
87@end example
88@end defspec
89
90 Two other control constructs likewise evaluate a series of forms but return
91a different value:
92
93@defspec prog1 form1 forms@dots{}
94This special form evaluates @var{form1} and all of the @var{forms}, in
95textual order, returning the result of @var{form1}.
96
97@example
98@group
99(prog1 (print "The first form")
100 (print "The second form")
101 (print "The third form"))
102 @print{} "The first form"
103 @print{} "The second form"
104 @print{} "The third form"
105@result{} "The first form"
106@end group
107@end example
108
109Here is a way to remove the first element from a list in the variable
110@code{x}, then return the value of that former element:
111
112@example
113(prog1 (car x) (setq x (cdr x)))
114@end example
115@end defspec
116
117@defspec prog2 form1 form2 forms@dots{}
118This special form evaluates @var{form1}, @var{form2}, and all of the
119following @var{forms}, in textual order, returning the result of
120@var{form2}.
121
122@example
123@group
124(prog2 (print "The first form")
125 (print "The second form")
126 (print "The third form"))
127 @print{} "The first form"
128 @print{} "The second form"
129 @print{} "The third form"
130@result{} "The second form"
131@end group
132@end example
133@end defspec
134
135@node Conditionals
136@section Conditionals
137@cindex conditional evaluation
138
139 Conditional control structures choose among alternatives. Emacs Lisp
140has two conditional forms: @code{if}, which is much the same as in other
141languages, and @code{cond}, which is a generalized case statement.
142
143@defspec if condition then-form else-forms@dots{}
144@code{if} chooses between the @var{then-form} and the @var{else-forms}
145based on the value of @var{condition}. If the evaluated @var{condition} is
146non-@code{nil}, @var{then-form} is evaluated and the result returned.
147Otherwise, the @var{else-forms} are evaluated in textual order, and the
148value of the last one is returned. (The @var{else} part of @code{if} is
149an example of an implicit @code{progn}. @xref{Sequencing}.)
150
151If @var{condition} has the value @code{nil}, and no @var{else-forms} are
152given, @code{if} returns @code{nil}.
153
154@code{if} is a special form because the branch which is not selected is
155never evaluated---it is ignored. Thus, in the example below,
156@code{true} is not printed because @code{print} is never called.
157
158@example
159@group
160(if nil
161 (print 'true)
162 'very-false)
163@result{} very-false
164@end group
165@end example
166@end defspec
167
168@defspec cond clause@dots{}
169@code{cond} chooses among an arbitrary number of alternatives. Each
170@var{clause} in the @code{cond} must be a list. The @sc{car} of this
171list is the @var{condition}; the remaining elements, if any, the
172@var{body-forms}. Thus, a clause looks like this:
173
174@example
175(@var{condition} @var{body-forms}@dots{})
176@end example
177
178@code{cond} tries the clauses in textual order, by evaluating the
179@var{condition} of each clause. If the value of @var{condition} is
180non-@code{nil}, the clause ``succeeds''; then @code{cond} evaluates its
181@var{body-forms}, and the value of the last of @var{body-forms} becomes
182the value of the @code{cond}. The remaining clauses are ignored.
183
184If the value of @var{condition} is @code{nil}, the clause ``fails'', so
185the @code{cond} moves on to the following clause, trying its
186@var{condition}.
187
188If every @var{condition} evaluates to @code{nil}, so that every clause
189fails, @code{cond} returns @code{nil}.
190
191A clause may also look like this:
192
193@example
194(@var{condition})
195@end example
196
197@noindent
198Then, if @var{condition} is non-@code{nil} when tested, the value of
199@var{condition} becomes the value of the @code{cond} form.
200
201The following example has four clauses, which test for the cases where
202the value of @code{x} is a number, string, buffer and symbol,
203respectively:
204
205@example
206@group
207(cond ((numberp x) x)
208 ((stringp x) x)
209 ((bufferp x)
210 (setq temporary-hack x) ; @r{multiple body-forms}
211 (buffer-name x)) ; @r{in one clause}
212 ((symbolp x) (symbol-value x)))
213@end group
214@end example
215
216Often we want to execute the last clause whenever none of the previous
217clauses was successful. To do this, we use @code{t} as the
218@var{condition} of the last clause, like this: @code{(t
219@var{body-forms})}. The form @code{t} evaluates to @code{t}, which is
220never @code{nil}, so this clause never fails, provided the @code{cond}
221gets to it at all.
222
223For example,
224
225@example
226@group
227(cond ((eq a 1) 'foo)
228 (t "default"))
229@result{} "default"
230@end group
231@end example
232
233@noindent
234This expression is a @code{cond} which returns @code{foo} if the value
235of @code{a} is 1, and returns the string @code{"default"} otherwise.
236@end defspec
237
238Both @code{cond} and @code{if} can usually be written in terms of the
239other. Therefore, the choice between them is a matter of style. For
240example:
241
242@example
243@group
244(if @var{a} @var{b} @var{c})
245@equiv{}
246(cond (@var{a} @var{b}) (t @var{c}))
247@end group
248@end example
249
250@node Combining Conditions
251@section Constructs for Combining Conditions
252
253 This section describes three constructs that are often used together
254with @code{if} and @code{cond} to express complicated conditions. The
255constructs @code{and} and @code{or} can also be used individually as
256kinds of multiple conditional constructs.
257
258@defun not condition
259This function tests for the falsehood of @var{condition}. It returns
260@code{t} if @var{condition} is @code{nil}, and @code{nil} otherwise.
261The function @code{not} is identical to @code{null}, and we recommend
262using the name @code{null} if you are testing for an empty list.
263@end defun
264
265@defspec and conditions@dots{}
266The @code{and} special form tests whether all the @var{conditions} are
267true. It works by evaluating the @var{conditions} one by one in the
268order written.
269
270If any of the @var{conditions} evaluates to @code{nil}, then the result
271of the @code{and} must be @code{nil} regardless of the remaining
272@var{conditions}; so @code{and} returns right away, ignoring the
273remaining @var{conditions}.
274
275If all the @var{conditions} turn out non-@code{nil}, then the value of
276the last of them becomes the value of the @code{and} form.
277
278Here is an example. The first condition returns the integer 1, which is
279not @code{nil}. Similarly, the second condition returns the integer 2,
280which is not @code{nil}. The third condition is @code{nil}, so the
281remaining condition is never evaluated.
282
283@example
284@group
285(and (print 1) (print 2) nil (print 3))
286 @print{} 1
287 @print{} 2
288@result{} nil
289@end group
290@end example
291
292Here is a more realistic example of using @code{and}:
293
294@example
295@group
296(if (and (consp foo) (eq (car foo) 'x))
297 (message "foo is a list starting with x"))
298@end group
299@end example
300
301@noindent
302Note that @code{(car foo)} is not executed if @code{(consp foo)} returns
303@code{nil}, thus avoiding an error.
304
305@code{and} can be expressed in terms of either @code{if} or @code{cond}.
306For example:
307
308@example
309@group
310(and @var{arg1} @var{arg2} @var{arg3})
311@equiv{}
312(if @var{arg1} (if @var{arg2} @var{arg3}))
313@equiv{}
314(cond (@var{arg1} (cond (@var{arg2} @var{arg3}))))
315@end group
316@end example
317@end defspec
318
319@defspec or conditions@dots{}
320The @code{or} special form tests whether at least one of the
321@var{conditions} is true. It works by evaluating all the
322@var{conditions} one by one in the order written.
323
324If any of the @var{conditions} evaluates to a non-@code{nil} value, then
325the result of the @code{or} must be non-@code{nil}; so @code{or} returns
326right away, ignoring the remaining @var{conditions}. The value it
327returns is the non-@code{nil} value of the condition just evaluated.
328
329If all the @var{conditions} turn out @code{nil}, then the @code{or}
330expression returns @code{nil}.
331
332For example, this expression tests whether @code{x} is either 0 or
333@code{nil}:
334
335@example
336(or (eq x nil) (eq x 0))
337@end example
338
339Like the @code{and} construct, @code{or} can be written in terms of
340@code{cond}. For example:
341
342@example
343@group
344(or @var{arg1} @var{arg2} @var{arg3})
345@equiv{}
346(cond (@var{arg1})
347 (@var{arg2})
348 (@var{arg3}))
349@end group
350@end example
351
352You could almost write @code{or} in terms of @code{if}, but not quite:
353
354@example
355@group
356(if @var{arg1} @var{arg1}
357 (if @var{arg2} @var{arg2}
358 @var{arg3}))
359@end group
360@end example
361
362@noindent
363This is not completely equivalent because it can evaluate @var{arg1} or
364@var{arg2} twice. By contrast, @code{(or @var{arg1} @var{arg2}
365@var{arg3})} never evaluates any argument more than once.
366@end defspec
367
368@node Iteration
369@section Iteration
370@cindex iteration
371@cindex recursion
372
373 Iteration means executing part of a program repetitively. For
374example, you might want to repeat some computation once for each element
375of a list, or once for each integer from 0 to @var{n}. You can do this
376in Emacs Lisp with the special form @code{while}:
377
378@defspec while condition forms@dots{}
379@code{while} first evaluates @var{condition}. If the result is
380non-@code{nil}, it evaluates @var{forms} in textual order. Then it
381reevaluates @var{condition}, and if the result is non-@code{nil}, it
382evaluates @var{forms} again. This process repeats until @var{condition}
383evaluates to @code{nil}.
384
385There is no limit on the number of iterations that may occur. The loop
386will continue until either @var{condition} evaluates to @code{nil} or
387until an error or @code{throw} jumps out of it (@pxref{Nonlocal Exits}).
388
389The value of a @code{while} form is always @code{nil}.
390
391@example
392@group
393(setq num 0)
394 @result{} 0
395@end group
396@group
397(while (< num 4)
398 (princ (format "Iteration %d." num))
399 (setq num (1+ num)))
400 @print{} Iteration 0.
401 @print{} Iteration 1.
402 @print{} Iteration 2.
403 @print{} Iteration 3.
404 @result{} nil
405@end group
406@end example
407
408If you would like to execute something on each iteration before the
409end-test, put it together with the end-test in a @code{progn} as the
410first argument of @code{while}, as shown here:
411
412@example
413@group
414(while (progn
415 (forward-line 1)
416 (not (looking-at "^$"))))
417@end group
418@end example
419
420@noindent
421This moves forward one line and continues moving by lines until an empty
422line is reached.
423@end defspec
424
425@node Nonlocal Exits
426@section Nonlocal Exits
427@cindex nonlocal exits
428
429 A @dfn{nonlocal exit} is a transfer of control from one point in a
430program to another remote point. Nonlocal exits can occur in Emacs Lisp
431as a result of errors; you can also use them under explicit control.
432Nonlocal exits unbind all variable bindings made by the constructs being
433exited.
434
435@menu
436* Catch and Throw:: Nonlocal exits for the program's own purposes.
437* Examples of Catch:: Showing how such nonlocal exits can be written.
438* Errors:: How errors are signaled and handled.
439* Cleanups:: Arranging to run a cleanup form if an error happens.
440@end menu
441
442@node Catch and Throw
443@subsection Explicit Nonlocal Exits: @code{catch} and @code{throw}
444
445 Most control constructs affect only the flow of control within the
446construct itself. The function @code{throw} is the exception to this
447rule of normal program execution: it performs a nonlocal exit on
448request. (There are other exceptions, but they are for error handling
449only.) @code{throw} is used inside a @code{catch}, and jumps back to
450that @code{catch}. For example:
451
452@example
453@group
454(catch 'foo
455 (progn
456 @dots{}
457 (throw 'foo t)
458 @dots{}))
459@end group
460@end example
461
462@noindent
463The @code{throw} transfers control straight back to the corresponding
464@code{catch}, which returns immediately. The code following the
465@code{throw} is not executed. The second argument of @code{throw} is used
466as the return value of the @code{catch}.
467
468 The @code{throw} and the @code{catch} are matched through the first
469argument: @code{throw} searches for a @code{catch} whose first argument
470is @code{eq} to the one specified. Thus, in the above example, the
471@code{throw} specifies @code{foo}, and the @code{catch} specifies the
472same symbol, so that @code{catch} is applicable. If there is more than
473one applicable @code{catch}, the innermost one takes precedence.
474
475 Executing @code{throw} exits all Lisp constructs up to the matching
476@code{catch}, including function calls. When binding constructs such as
477@code{let} or function calls are exited in this way, the bindings are
478unbound, just as they are when these constructs exit normally
479(@pxref{Local Variables}). Likewise, @code{throw} restores the buffer
480and position saved by @code{save-excursion} (@pxref{Excursions}), and
481the narrowing status saved by @code{save-restriction} and the window
482selection saved by @code{save-window-excursion} (@pxref{Window
483Configurations}). It also runs any cleanups established with the
484@code{unwind-protect} special form when it exits that form.
485
486 The @code{throw} need not appear lexically within the @code{catch}
487that it jumps to. It can equally well be called from another function
488called within the @code{catch}. As long as the @code{throw} takes place
489chronologically after entry to the @code{catch}, and chronologically
490before exit from it, it has access to that @code{catch}. This is why
491@code{throw} can be used in commands such as @code{exit-recursive-edit}
492which throw back to the editor command loop (@pxref{Recursive Editing}).
493
494@cindex CL note---only @code{throw} in Emacs
495@quotation
496@b{Common Lisp note:} most other versions of Lisp, including Common Lisp,
497have several ways of transferring control nonsequentially: @code{return},
498@code{return-from}, and @code{go}, for example. Emacs Lisp has only
499@code{throw}.
500@end quotation
501
502@defspec catch tag body@dots{}
503@cindex tag on run time stack
504@code{catch} establishes a return point for the @code{throw} function. The
505return point is distinguished from other such return points by @var{tag},
506which may be any Lisp object. The argument @var{tag} is evaluated normally
507before the return point is established.
508
509With the return point in effect, @code{catch} evaluates the forms of the
510@var{body} in textual order. If the forms execute normally, without
511error or nonlocal exit, the value of the last body form is returned from
512the @code{catch}.
513
514If a @code{throw} is done within @var{body} specifying the same value
515@var{tag}, the @code{catch} exits immediately; the value it returns is
516whatever was specified as the second argument of @code{throw}.
517@end defspec
518
519@defun throw tag value
520The purpose of @code{throw} is to return from a return point previously
521established with @code{catch}. The argument @var{tag} is used to choose
522among the various existing return points; it must be @code{eq} to the value
523specified in the @code{catch}. If multiple return points match @var{tag},
524the innermost one is used.
525
526The argument @var{value} is used as the value to return from that
527@code{catch}.
528
529@kindex no-catch
530If no return point is in effect with tag @var{tag}, then a @code{no-catch}
531error is signaled with data @code{(@var{tag} @var{value})}.
532@end defun
533
534@node Examples of Catch
535@subsection Examples of @code{catch} and @code{throw}
536
537 One way to use @code{catch} and @code{throw} is to exit from a doubly
538nested loop. (In most languages, this would be done with a ``go to''.)
539Here we compute @code{(foo @var{i} @var{j})} for @var{i} and @var{j}
540varying from 0 to 9:
541
542@example
543@group
544(defun search-foo ()
545 (catch 'loop
546 (let ((i 0))
547 (while (< i 10)
548 (let ((j 0))
549 (while (< j 10)
550 (if (foo i j)
551 (throw 'loop (list i j)))
552 (setq j (1+ j))))
553 (setq i (1+ i))))))
554@end group
555@end example
556
557@noindent
558If @code{foo} ever returns non-@code{nil}, we stop immediately and return a
559list of @var{i} and @var{j}. If @code{foo} always returns @code{nil}, the
560@code{catch} returns normally, and the value is @code{nil}, since that
561is the result of the @code{while}.
562
563 Here are two tricky examples, slightly different, showing two
564return points at once. First, two return points with the same tag,
565@code{hack}:
566
567@example
568@group
569(defun catch2 (tag)
570 (catch tag
571 (throw 'hack 'yes)))
572@result{} catch2
573@end group
574
575@group
576(catch 'hack
577 (print (catch2 'hack))
578 'no)
579@print{} yes
580@result{} no
581@end group
582@end example
583
584@noindent
585Since both return points have tags that match the @code{throw}, it goes to
586the inner one, the one established in @code{catch2}. Therefore,
587@code{catch2} returns normally with value @code{yes}, and this value is
588printed. Finally the second body form in the outer @code{catch}, which is
589@code{'no}, is evaluated and returned from the outer @code{catch}.
590
591 Now let's change the argument given to @code{catch2}:
592
593@example
594@group
595(defun catch2 (tag)
596 (catch tag
597 (throw 'hack 'yes)))
598@result{} catch2
599@end group
600
601@group
602(catch 'hack
603 (print (catch2 'quux))
604 'no)
605@result{} yes
606@end group
607@end example
608
609@noindent
610We still have two return points, but this time only the outer one has the
611tag @code{hack}; the inner one has the tag @code{quux} instead. Therefore,
612the @code{throw} returns the value @code{yes} from the outer return point.
613The function @code{print} is never called, and the body-form @code{'no} is
614never evaluated.
615
616@node Errors
617@subsection Errors
618@cindex errors
619
620 When Emacs Lisp attempts to evaluate a form that, for some reason,
621cannot be evaluated, it @dfn{signals} an @dfn{error}.
622
623 When an error is signaled, Emacs's default reaction is to print an
624error message and terminate execution of the current command. This is
625the right thing to do in most cases, such as if you type @kbd{C-f} at
626the end of the buffer.
627
628 In complicated programs, simple termination may not be what you want.
629For example, the program may have made temporary changes in data
630structures, or created temporary buffers which should be deleted before
631the program is finished. In such cases, you would use
632@code{unwind-protect} to establish @dfn{cleanup expressions} to be
633evaluated in case of error. Occasionally, you may wish the program to
634continue execution despite an error in a subroutine. In these cases,
635you would use @code{condition-case} to establish @dfn{error handlers} to
636recover control in case of error.
637
638 Resist the temptation to use error handling to transfer control from
639one part of the program to another; use @code{catch} and @code{throw}
640instead. @xref{Catch and Throw}.
641
642@menu
643* Signaling Errors:: How to report an error.
644* Processing of Errors:: What Emacs does when you report an error.
645* Handling Errors:: How you can trap errors and continue execution.
646* Error Names:: How errors are classified for trapping them.
647@end menu
648
649@node Signaling Errors
650@subsubsection How to Signal an Error
651@cindex signaling errors
652
653 Most errors are signaled ``automatically'' within Lisp primitives
654which you call for other purposes, such as if you try to take the
655@sc{car} of an integer or move forward a character at the end of the
656buffer; you can also signal errors explicitly with the functions
657@code{error} and @code{signal}.
658
659 Quitting, which happens when the user types @kbd{C-g}, is not
660considered an error, but it is handled almost like an error.
661@xref{Quitting}.
662
663@defun error format-string &rest args
664This function signals an error with an error message constructed by
665applying @code{format} (@pxref{String Conversion}) to
666@var{format-string} and @var{args}.
667
668These examples show typical uses of @code{error}:
669
670@example
671@group
672(error "You have committed an error.
673 Try something else.")
674 @error{} You have committed an error.
675 Try something else.
676@end group
677
678@group
679(error "You have committed %d errors." 10)
680 @error{} You have committed 10 errors.
681@end group
682@end example
683
684@code{error} works by calling @code{signal} with two arguments: the
685error symbol @code{error}, and a list containing the string returned by
686@code{format}.
687
688If you want to use your own string as an error message verbatim, don't
689just write @code{(error @var{string})}. If @var{string} contains
690@samp{%}, it will be interpreted as a format specifier, with undesirable
691results. Instead, use @code{(error "%s" @var{string})}.
692@end defun
693
694@defun signal error-symbol data
695This function signals an error named by @var{error-symbol}. The
696argument @var{data} is a list of additional Lisp objects relevant to the
697circumstances of the error.
698
699The argument @var{error-symbol} must be an @dfn{error symbol}---a symbol
700bearing a property @code{error-conditions} whose value is a list of
701condition names. This is how Emacs Lisp classifies different sorts of
702errors.
703
704The number and significance of the objects in @var{data} depends on
705@var{error-symbol}. For example, with a @code{wrong-type-arg} error,
706there are two objects in the list: a predicate which describes the type
707that was expected, and the object which failed to fit that type.
708@xref{Error Names}, for a description of error symbols.
709
710Both @var{error-symbol} and @var{data} are available to any error
711handlers which handle the error: @code{condition-case} binds a local
712variable to a list of the form @code{(@var{error-symbol} .@:
713@var{data})} (@pxref{Handling Errors}). If the error is not handled,
714these two values are used in printing the error message.
715
716The function @code{signal} never returns (though in older Emacs versions
717it could sometimes return).
718
719@smallexample
720@group
721(signal 'wrong-number-of-arguments '(x y))
722 @error{} Wrong number of arguments: x, y
723@end group
724
725@group
726(signal 'no-such-error '("My unknown error condition."))
727 @error{} peculiar error: "My unknown error condition."
728@end group
729@end smallexample
730@end defun
731
732@cindex CL note---no continuable errors
733@quotation
734@b{Common Lisp note:} Emacs Lisp has nothing like the Common Lisp
735concept of continuable errors.
736@end quotation
737
738@node Processing of Errors
739@subsubsection How Emacs Processes Errors
740
741When an error is signaled, @code{signal} searches for an active
742@dfn{handler} for the error. A handler is a sequence of Lisp
743expressions designated to be executed if an error happens in part of the
744Lisp program. If the error has an applicable handler, the handler is
745executed, and control resumes following the handler. The handler
746executes in the environment of the @code{condition-case} which
747established it; all functions called within that @code{condition-case}
748have already been exited, and the handler cannot return to them.
749
750If there is no applicable handler for the error, the current command is
751terminated and control returns to the editor command loop, because the
752command loop has an implicit handler for all kinds of errors. The
753command loop's handler uses the error symbol and associated data to
754print an error message.
755
756@cindex @code{debug-on-error} use
757An error that has no explicit handler may call the Lisp debugger. The
758debugger is enabled if the variable @code{debug-on-error} (@pxref{Error
759Debugging}) is non-@code{nil}. Unlike error handlers, the debugger runs
760in the environment of the error, so that you can examine values of
761variables precisely as they were at the time of the error.
762
763@node Handling Errors
764@subsubsection Writing Code to Handle Errors
765@cindex error handler
766@cindex handling errors
767
768 The usual effect of signaling an error is to terminate the command
769that is running and return immediately to the Emacs editor command loop.
770You can arrange to trap errors occurring in a part of your program by
771establishing an error handler, with the special form
772@code{condition-case}. A simple example looks like this:
773
774@example
775@group
776(condition-case nil
777 (delete-file filename)
778 (error nil))
779@end group
780@end example
781
782@noindent
783This deletes the file named @var{filename}, catching any error and
784returning @code{nil} if an error occurs.
785
786 The second argument of @code{condition-case} is called the
787@dfn{protected form}. (In the example above, the protected form is a
788call to @code{delete-file}.) The error handlers go into effect when
789this form begins execution and are deactivated when this form returns.
790They remain in effect for all the intervening time. In particular, they
791are in effect during the execution of subroutines called by this form,
792and their subroutines, and so on. This is a good thing, since, strictly
793speaking, errors can be signaled only by Lisp primitives (including
794@code{signal} and @code{error}) called by the protected form, not by the
795protected form itself.
796
797 The arguments after the protected form are handlers. Each handler
798lists one or more @dfn{condition names} (which are symbols) to specify
799which errors it will handle. The error symbol specified when an error
800is signaled also defines a list of condition names. A handler applies
801to an error if they have any condition names in common. In the example
802above, there is one handler, and it specifies one condition name,
803@code{error}, which covers all errors.
804
805 The search for an applicable handler checks all the established handlers
806starting with the most recently established one. Thus, if two nested
807@code{condition-case} forms offer to handle the same error, the inner of
808the two will actually handle it.
809
810 When an error is handled, control returns to the handler. Before this
811happens, Emacs unbinds all variable bindings made by binding constructs
812that are being exited and executes the cleanups of all
813@code{unwind-protect} forms that are exited. Once control arrives at
814the handler, the body of the handler is executed.
815
816 After execution of the handler body, execution continues by returning
817from the @code{condition-case} form. Because the protected form is
818exited completely before execution of the handler, the handler cannot
819resume execution at the point of the error, nor can it examine variable
820bindings that were made within the protected form. All it can do is
821clean up and proceed.
822
823 @code{condition-case} is often used to trap errors that are
824predictable, such as failure to open a file in a call to
825@code{insert-file-contents}. It is also used to trap errors that are
826totally unpredictable, such as when the program evaluates an expression
827read from the user.
828
829 Error signaling and handling have some resemblance to @code{throw} and
830@code{catch}, but they are entirely separate facilities. An error
831cannot be caught by a @code{catch}, and a @code{throw} cannot be handled
832by an error handler (though using @code{throw} when there is no suitable
833@code{catch} signals an error which can be handled).
834
835@defspec condition-case var protected-form handlers@dots{}
836This special form establishes the error handlers @var{handlers} around
837the execution of @var{protected-form}. If @var{protected-form} executes
838without error, the value it returns becomes the value of the
839@code{condition-case} form; in this case, the @code{condition-case} has
840no effect. The @code{condition-case} form makes a difference when an
841error occurs during @var{protected-form}.
842
843Each of the @var{handlers} is a list of the form @code{(@var{conditions}
844@var{body}@dots{})}. Here @var{conditions} is an error condition name
845to be handled, or a list of condition names; @var{body} is one or more
846Lisp expressions to be executed when this handler handles an error.
847Here are examples of handlers:
848
849@smallexample
850@group
851(error nil)
852
853(arith-error (message "Division by zero"))
854
855((arith-error file-error)
856 (message
857 "Either division by zero or failure to open a file"))
858@end group
859@end smallexample
860
861Each error that occurs has an @dfn{error symbol} which describes what
862kind of error it is. The @code{error-conditions} property of this
863symbol is a list of condition names (@pxref{Error Names}). Emacs
864searches all the active @code{condition-case} forms for a handler which
865specifies one or more of these condition names; the innermost matching
866@code{condition-case} handles the error. Within this
867@code{condition-case}, the first applicable handler handles the error.
868
869After executing the body of the handler, the @code{condition-case}
870returns normally, using the value of the last form in the handler body
871as the overall value.
872
873The argument @var{var} is a variable. @code{condition-case} does not
874bind this variable when executing the @var{protected-form}, only when it
875handles an error. At that time, it binds @var{var} locally to a list of
876the form @code{(@var{error-symbol} . @var{data})}, giving the
877particulars of the error. The handler can refer to this list to decide
878what to do. For example, if the error is for failure opening a file,
879the file name is the second element of @var{data}---the third element of
880@var{var}.
881
882If @var{var} is @code{nil}, that means no variable is bound. Then the
883error symbol and associated data are not available to the handler.
884@end defspec
885
886@cindex @code{arith-error} example
887Here is an example of using @code{condition-case} to handle the error
888that results from dividing by zero. The handler prints out a warning
889message and returns a very large number.
890
891@smallexample
892@group
893(defun safe-divide (dividend divisor)
894 (condition-case err
895 ;; @r{Protected form.}
896 (/ dividend divisor)
897 ;; @r{The handler.}
898 (arith-error ; @r{Condition.}
899 (princ (format "Arithmetic error: %s" err))
900 1000000)))
901@result{} safe-divide
902@end group
903
904@group
905(safe-divide 5 0)
906 @print{} Arithmetic error: (arith-error)
907@result{} 1000000
908@end group
909@end smallexample
910
911@noindent
912The handler specifies condition name @code{arith-error} so that it will handle only division-by-zero errors. Other kinds of errors will not be handled, at least not by this @code{condition-case}. Thus,
913
914@smallexample
915@group
916(safe-divide nil 3)
917 @error{} Wrong type argument: integer-or-marker-p, nil
918@end group
919@end smallexample
920
921 Here is a @code{condition-case} that catches all kinds of errors,
922including those signaled with @code{error}:
923
924@smallexample
925@group
926(setq baz 34)
927 @result{} 34
928@end group
929
930@group
931(condition-case err
932 (if (eq baz 35)
933 t
934 ;; @r{This is a call to the function @code{error}.}
935 (error "Rats! The variable %s was %s, not 35." 'baz baz))
936 ;; @r{This is the handler; it is not a form.}
937 (error (princ (format "The error was: %s" err))
938 2))
939@print{} The error was: (error "Rats! The variable baz was 34, not 35.")
940@result{} 2
941@end group
942@end smallexample
943
944@node Error Names
945@subsubsection Error Symbols and Condition Names
946@cindex error symbol
947@cindex error name
948@cindex condition name
949@cindex user-defined error
950@kindex error-conditions
951
952 When you signal an error, you specify an @dfn{error symbol} to specify
953the kind of error you have in mind. Each error has one and only one
954error symbol to categorize it. This is the finest classification of
955errors defined by the Emacs Lisp language.
956
957 These narrow classifications are grouped into a hierarchy of wider
958classes called @dfn{error conditions}, identified by @dfn{condition
959names}. The narrowest such classes belong to the error symbols
960themselves: each error symbol is also a condition name. There are also
961condition names for more extensive classes, up to the condition name
962@code{error} which takes in all kinds of errors. Thus, each error has
963one or more condition names: @code{error}, the error symbol if that
964is distinct from @code{error}, and perhaps some intermediate
965classifications.
966
967 In order for a symbol to be an error symbol, it must have an
968@code{error-conditions} property which gives a list of condition names.
969This list defines the conditions which this kind of error belongs to.
970(The error symbol itself, and the symbol @code{error}, should always be
971members of this list.) Thus, the hierarchy of condition names is
972defined by the @code{error-conditions} properties of the error symbols.
973
974 In addition to the @code{error-conditions} list, the error symbol
975should have an @code{error-message} property whose value is a string to
976be printed when that error is signaled but not handled. If the
977@code{error-message} property exists, but is not a string, the error
978message @samp{peculiar error} is used.
979@cindex peculiar error
980
981 Here is how we define a new error symbol, @code{new-error}:
982
983@example
984@group
985(put 'new-error
986 'error-conditions
987 '(error my-own-errors new-error))
988@result{} (error my-own-errors new-error)
989@end group
990@group
991(put 'new-error 'error-message "A new error")
992@result{} "A new error"
993@end group
994@end example
995
996@noindent
997This error has three condition names: @code{new-error}, the narrowest
998classification; @code{my-own-errors}, which we imagine is a wider
999classification; and @code{error}, which is the widest of all.
1000
1001 Naturally, Emacs will never signal @code{new-error} on its own; only
1002an explicit call to @code{signal} (@pxref{Errors}) in your code can do
1003this:
1004
1005@example
1006@group
1007(signal 'new-error '(x y))
1008 @error{} A new error: x, y
1009@end group
1010@end example
1011
1012 This error can be handled through any of the three condition names.
1013This example handles @code{new-error} and any other errors in the class
1014@code{my-own-errors}:
1015
1016@example
1017@group
1018(condition-case foo
1019 (bar nil t)
1020 (my-own-errors nil))
1021@end group
1022@end example
1023
1024 The significant way that errors are classified is by their condition
1025names---the names used to match errors with handlers. An error symbol
1026serves only as a convenient way to specify the intended error message
1027and list of condition names. It would be cumbersome to give
1028@code{signal} a list of condition names rather than one error symbol.
1029
1030 By contrast, using only error symbols without condition names would
1031seriously decrease the power of @code{condition-case}. Condition names
1032make it possible to categorize errors at various levels of generality
1033when you write an error handler. Using error symbols alone would
1034eliminate all but the narrowest level of classification.
1035
1036 @xref{Standard Errors}, for a list of all the standard error symbols
1037and their conditions.
1038
1039@node Cleanups
1040@subsection Cleaning Up from Nonlocal Exits
1041
1042 The @code{unwind-protect} construct is essential whenever you
1043temporarily put a data structure in an inconsistent state; it permits
1044you to ensure the data are consistent in the event of an error or throw.
1045
1046@defspec unwind-protect body cleanup-forms@dots{}
1047@cindex cleanup forms
1048@cindex protected forms
1049@cindex error cleanup
1050@cindex unwinding
1051@code{unwind-protect} executes the @var{body} with a guarantee that the
1052@var{cleanup-forms} will be evaluated if control leaves @var{body}, no
1053matter how that happens. The @var{body} may complete normally, or
1054execute a @code{throw} out of the @code{unwind-protect}, or cause an
1055error; in all cases, the @var{cleanup-forms} will be evaluated.
1056
1057If the @var{body} forms finish normally, @code{unwind-protect} returns
1058the value of the last @var{body} form, after it evaluates the
1059@var{cleanup-forms}. If the @var{body} forms do not finish,
1060@code{unwind-protect} does not return any value in the normal sense.
1061
1062Only the @var{body} is actually protected by the @code{unwind-protect}.
1063If any of the @var{cleanup-forms} themselves exits nonlocally (e.g., via
1064a @code{throw} or an error), @code{unwind-protect} is @emph{not}
1065guaranteed to evaluate the rest of them. If the failure of one of the
1066@var{cleanup-forms} has the potential to cause trouble, then protect it
1067with another @code{unwind-protect} around that form.
1068
1069The number of currently active @code{unwind-protect} forms counts,
1070together with the number of local variable bindings, against the limit
1071@code{max-specpdl-size} (@pxref{Local Variables}).
1072@end defspec
1073
1074 For example, here we make an invisible buffer for temporary use, and
1075make sure to kill it before finishing:
1076
1077@smallexample
1078@group
1079(save-excursion
1080 (let ((buffer (get-buffer-create " *temp*")))
1081 (set-buffer buffer)
1082 (unwind-protect
1083 @var{body}
1084 (kill-buffer buffer))))
1085@end group
1086@end smallexample
1087
1088@noindent
1089You might think that we could just as well write @code{(kill-buffer
1090(current-buffer))} and dispense with the variable @code{buffer}.
1091However, the way shown above is safer, if @var{body} happens to get an
1092error after switching to a different buffer! (Alternatively, you could
1093write another @code{save-excursion} around the body, to ensure that the
1094temporary buffer becomes current in time to kill it.)
1095
1096@findex ftp-login
1097 Here is an actual example taken from the file @file{ftp.el}. It creates
1098a process (@pxref{Processes}) to try to establish a connection to a remote
1099machine. As the function @code{ftp-login} is highly susceptible to
1100numerous problems which the writer of the function cannot anticipate, it is
1101protected with a form that guarantees deletion of the process in the event
1102of failure. Otherwise, Emacs might fill up with useless subprocesses.
1103
1104@smallexample
1105@group
1106(let ((win nil))
1107 (unwind-protect
1108 (progn
1109 (setq process (ftp-setup-buffer host file))
1110 (if (setq win (ftp-login process host user password))
1111 (message "Logged in")
1112 (error "Ftp login failed")))
1113 (or win (and process (delete-process process)))))
1114@end group
1115@end smallexample
1116
1117 This example actually has a small bug: if the user types @kbd{C-g} to
1118quit, and the quit happens immediately after the function
1119@code{ftp-setup-buffer} returns but before the variable @code{process} is
1120set, the process will not be killed. There is no easy way to fix this bug,
1121but at least it is very unlikely.
1122
1123 Here is another example which uses @code{unwind-protect} to make sure
1124to kill a temporary buffer. In this example, the value returned by
1125@code{unwind-protect} is used.
1126
1127@example
1128(defun shell-command-string (cmd)
1129 "Return the output of the shell command CMD, as a string."
1130 (save-excursion
1131 (set-buffer (generate-new-buffer " OS*cmd"))
1132 (shell-command cmd t)
1133 (unwind-protect
1134 (buffer-string)
1135 (kill-buffer (current-buffer)))))
1136@end example
diff --git a/lispref/intro.texi b/lispref/intro.texi
new file mode 100644
index 00000000000..2a2b02f1ad9
--- /dev/null
+++ b/lispref/intro.texi
@@ -0,0 +1,867 @@
1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual.
3@c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4@c See the file elisp.texi for copying conditions.
5@setfilename ../info/intro
6
7@node Copying, Introduction, Top, Top
8@comment node-name, next, previous, up
9@unnumbered GNU GENERAL PUBLIC LICENSE
10@center Version 2, June 1991
11
12@display
13Copyright @copyright{} 1989, 1991 Free Software Foundation, Inc.
14675 Mass Ave, Cambridge, MA 02139, USA
15
16Everyone is permitted to copy and distribute verbatim copies
17of this license document, but changing it is not allowed.
18@end display
19
20@unnumberedsec Preamble
21
22 The licenses for most software are designed to take away your
23freedom to share and change it. By contrast, the GNU General Public
24License is intended to guarantee your freedom to share and change free
25software---to make sure the software is free for all its users. This
26General Public License applies to most of the Free Software
27Foundation's software and to any other program whose authors commit to
28using it. (Some other Free Software Foundation software is covered by
29the GNU Library General Public License instead.) You can apply it to
30your programs, too.
31
32 When we speak of free software, we are referring to freedom, not
33price. Our General Public Licenses are designed to make sure that you
34have the freedom to distribute copies of free software (and charge for
35this service if you wish), that you receive source code or can get it
36if you want it, that you can change the software or use pieces of it
37in new free programs; and that you know you can do these things.
38
39 To protect your rights, we need to make restrictions that forbid
40anyone to deny you these rights or to ask you to surrender the rights.
41These restrictions translate to certain responsibilities for you if you
42distribute copies of the software, or if you modify it.
43
44 For example, if you distribute copies of such a program, whether
45gratis or for a fee, you must give the recipients all the rights that
46you have. You must make sure that they, too, receive or can get the
47source code. And you must show them these terms so they know their
48rights.
49
50 We protect your rights with two steps: (1) copyright the software, and
51(2) offer you this license which gives you legal permission to copy,
52distribute and/or modify the software.
53
54 Also, for each author's protection and ours, we want to make certain
55that everyone understands that there is no warranty for this free
56software. If the software is modified by someone else and passed on, we
57want its recipients to know that what they have is not the original, so
58that any problems introduced by others will not reflect on the original
59authors' reputations.
60
61 Finally, any free program is threatened constantly by software
62patents. We wish to avoid the danger that redistributors of a free
63program will individually obtain patent licenses, in effect making the
64program proprietary. To prevent this, we have made it clear that any
65patent must be licensed for everyone's free use or not licensed at all.
66
67 The precise terms and conditions for copying, distribution and
68modification follow.
69
70@iftex
71@unnumberedsec TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
72@end iftex
73@ifinfo
74@center TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
75@end ifinfo
76
77@enumerate 0
78@item
79This License applies to any program or other work which contains
80a notice placed by the copyright holder saying it may be distributed
81under the terms of this General Public License. The ``Program'', below,
82refers to any such program or work, and a ``work based on the Program''
83means either the Program or any derivative work under copyright law:
84that is to say, a work containing the Program or a portion of it,
85either verbatim or with modifications and/or translated into another
86language. (Hereinafter, translation is included without limitation in
87the term ``modification''.) Each licensee is addressed as ``you''.
88
89Activities other than copying, distribution and modification are not
90covered by this License; they are outside its scope. The act of
91running the Program is not restricted, and the output from the Program
92is covered only if its contents constitute a work based on the
93Program (independent of having been made by running the Program).
94Whether that is true depends on what the Program does.
95
96@item
97You may copy and distribute verbatim copies of the Program's
98source code as you receive it, in any medium, provided that you
99conspicuously and appropriately publish on each copy an appropriate
100copyright notice and disclaimer of warranty; keep intact all the
101notices that refer to this License and to the absence of any warranty;
102and give any other recipients of the Program a copy of this License
103along with the Program.
104
105You may charge a fee for the physical act of transferring a copy, and
106you may at your option offer warranty protection in exchange for a fee.
107
108@item
109You may modify your copy or copies of the Program or any portion
110of it, thus forming a work based on the Program, and copy and
111distribute such modifications or work under the terms of Section 1
112above, provided that you also meet all of these conditions:
113
114@enumerate a
115@item
116You must cause the modified files to carry prominent notices
117stating that you changed the files and the date of any change.
118
119@item
120You must cause any work that you distribute or publish, that in
121whole or in part contains or is derived from the Program or any
122part thereof, to be licensed as a whole at no charge to all third
123parties under the terms of this License.
124
125@item
126If the modified program normally reads commands interactively
127when run, you must cause it, when started running for such
128interactive use in the most ordinary way, to print or display an
129announcement including an appropriate copyright notice and a
130notice that there is no warranty (or else, saying that you provide
131a warranty) and that users may redistribute the program under
132these conditions, and telling the user how to view a copy of this
133License. (Exception: if the Program itself is interactive but
134does not normally print such an announcement, your work based on
135the Program is not required to print an announcement.)
136@end enumerate
137
138These requirements apply to the modified work as a whole. If
139identifiable sections of that work are not derived from the Program,
140and can be reasonably considered independent and separate works in
141themselves, then this License, and its terms, do not apply to those
142sections when you distribute them as separate works. But when you
143distribute the same sections as part of a whole which is a work based
144on the Program, the distribution of the whole must be on the terms of
145this License, whose permissions for other licensees extend to the
146entire whole, and thus to each and every part regardless of who wrote it.
147
148Thus, it is not the intent of this section to claim rights or contest
149your rights to work written entirely by you; rather, the intent is to
150exercise the right to control the distribution of derivative or
151collective works based on the Program.
152
153In addition, mere aggregation of another work not based on the Program
154with the Program (or with a work based on the Program) on a volume of
155a storage or distribution medium does not bring the other work under
156the scope of this License.
157
158@item
159You may copy and distribute the Program (or a work based on it,
160under Section 2) in object code or executable form under the terms of
161Sections 1 and 2 above provided that you also do one of the following:
162
163@enumerate a
164@item
165Accompany it with the complete corresponding machine-readable
166source code, which must be distributed under the terms of Sections
1671 and 2 above on a medium customarily used for software interchange; or,
168
169@item
170Accompany it with a written offer, valid for at least three
171years, to give any third party, for a charge no more than your
172cost of physically performing source distribution, a complete
173machine-readable copy of the corresponding source code, to be
174distributed under the terms of Sections 1 and 2 above on a medium
175customarily used for software interchange; or,
176
177@item
178Accompany it with the information you received as to the offer
179to distribute corresponding source code. (This alternative is
180allowed only for noncommercial distribution and only if you
181received the program in object code or executable form with such
182an offer, in accord with Subsection b above.)
183@end enumerate
184
185The source code for a work means the preferred form of the work for
186making modifications to it. For an executable work, complete source
187code means all the source code for all modules it contains, plus any
188associated interface definition files, plus the scripts used to
189control compilation and installation of the executable. However, as a
190special exception, the source code distributed need not include
191anything that is normally distributed (in either source or binary
192form) with the major components (compiler, kernel, and so on) of the
193operating system on which the executable runs, unless that component
194itself accompanies the executable.
195
196If distribution of executable or object code is made by offering
197access to copy from a designated place, then offering equivalent
198access to copy the source code from the same place counts as
199distribution of the source code, even though third parties are not
200compelled to copy the source along with the object code.
201
202@item
203You may not copy, modify, sublicense, or distribute the Program
204except as expressly provided under this License. Any attempt
205otherwise to copy, modify, sublicense or distribute the Program is
206void, and will automatically terminate your rights under this License.
207However, parties who have received copies, or rights, from you under
208this License will not have their licenses terminated so long as such
209parties remain in full compliance.
210
211@item
212You are not required to accept this License, since you have not
213signed it. However, nothing else grants you permission to modify or
214distribute the Program or its derivative works. These actions are
215prohibited by law if you do not accept this License. Therefore, by
216modifying or distributing the Program (or any work based on the
217Program), you indicate your acceptance of this License to do so, and
218all its terms and conditions for copying, distributing or modifying
219the Program or works based on it.
220
221@item
222Each time you redistribute the Program (or any work based on the
223Program), the recipient automatically receives a license from the
224original licensor to copy, distribute or modify the Program subject to
225these terms and conditions. You may not impose any further
226restrictions on the recipients' exercise of the rights granted herein.
227You are not responsible for enforcing compliance by third parties to
228this License.
229
230@item
231If, as a consequence of a court judgment or allegation of patent
232infringement or for any other reason (not limited to patent issues),
233conditions are imposed on you (whether by court order, agreement or
234otherwise) that contradict the conditions of this License, they do not
235excuse you from the conditions of this License. If you cannot
236distribute so as to satisfy simultaneously your obligations under this
237License and any other pertinent obligations, then as a consequence you
238may not distribute the Program at all. For example, if a patent
239license would not permit royalty-free redistribution of the Program by
240all those who receive copies directly or indirectly through you, then
241the only way you could satisfy both it and this License would be to
242refrain entirely from distribution of the Program.
243
244If any portion of this section is held invalid or unenforceable under
245any particular circumstance, the balance of the section is intended to
246apply and the section as a whole is intended to apply in other
247circumstances.
248
249It is not the purpose of this section to induce you to infringe any
250patents or other property right claims or to contest validity of any
251such claims; this section has the sole purpose of protecting the
252integrity of the free software distribution system, which is
253implemented by public license practices. Many people have made
254generous contributions to the wide range of software distributed
255through that system in reliance on consistent application of that
256system; it is up to the author/donor to decide if he or she is willing
257to distribute software through any other system and a licensee cannot
258impose that choice.
259
260This section is intended to make thoroughly clear what is believed to
261be a consequence of the rest of this License.
262
263@item
264If the distribution and/or use of the Program is restricted in
265certain countries either by patents or by copyrighted interfaces, the
266original copyright holder who places the Program under this License
267may add an explicit geographical distribution limitation excluding
268those countries, so that distribution is permitted only in or among
269countries not thus excluded. In such case, this License incorporates
270the limitation as if written in the body of this License.
271
272@item
273The Free Software Foundation may publish revised and/or new versions
274of the General Public License from time to time. Such new versions will
275be similar in spirit to the present version, but may differ in detail to
276address new problems or concerns.
277
278Each version is given a distinguishing version number. If the Program
279specifies a version number of this License which applies to it and ``any
280later version'', you have the option of following the terms and conditions
281either of that version or of any later version published by the Free
282Software Foundation. If the Program does not specify a version number of
283this License, you may choose any version ever published by the Free Software
284Foundation.
285
286@item
287If you wish to incorporate parts of the Program into other free
288programs whose distribution conditions are different, write to the author
289to ask for permission. For software which is copyrighted by the Free
290Software Foundation, write to the Free Software Foundation; we sometimes
291make exceptions for this. Our decision will be guided by the two goals
292of preserving the free status of all derivatives of our free software and
293of promoting the sharing and reuse of software generally.
294
295@iftex
296@heading NO WARRANTY
297@end iftex
298@ifinfo
299@center NO WARRANTY
300@end ifinfo
301
302@item
303BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
304FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW@. EXCEPT WHEN
305OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
306PROVIDE THE PROGRAM ``AS IS'' WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
307OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
308MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE@. THE ENTIRE RISK AS
309TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU@. SHOULD THE
310PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
311REPAIR OR CORRECTION.
312
313@item
314IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
315WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
316REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
317INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
318OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
319TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
320YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
321PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
322POSSIBILITY OF SUCH DAMAGES.
323@end enumerate
324
325@iftex
326@heading END OF TERMS AND CONDITIONS
327@end iftex
328@ifinfo
329@center END OF TERMS AND CONDITIONS
330@end ifinfo
331
332@page
333@unnumberedsec How to Apply These Terms to Your New Programs
334
335 If you develop a new program, and you want it to be of the greatest
336possible use to the public, the best way to achieve this is to make it
337free software which everyone can redistribute and change under these terms.
338
339 To do so, attach the following notices to the program. It is safest
340to attach them to the start of each source file to most effectively
341convey the exclusion of warranty; and each file should have at least
342the ``copyright'' line and a pointer to where the full notice is found.
343
344@smallexample
345@var{one line to give the program's name and an idea of what it does.}
346Copyright (C) 19@var{yy} @var{name of author}
347
348This program is free software; you can redistribute it and/or
349modify it under the terms of the GNU General Public License
350as published by the Free Software Foundation; either version 2
351of the License, or (at your option) any later version.
352
353This program is distributed in the hope that it will be useful,
354but WITHOUT ANY WARRANTY; without even the implied warranty of
355MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE@. See the
356GNU General Public License for more details.
357
358You should have received a copy of the GNU General Public License
359along with this program; if not, write to the Free Software
360Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
361@end smallexample
362
363Also add information on how to contact you by electronic and paper mail.
364
365If the program is interactive, make it output a short notice like this
366when it starts in an interactive mode:
367
368@smallexample
369Gnomovision version 69, Copyright (C) 19@var{yy} @var{name of author}
370Gnomovision comes with ABSOLUTELY NO WARRANTY; for details
371type `show w'. This is free software, and you are welcome
372to redistribute it under certain conditions; type `show c'
373for details.
374@end smallexample
375
376The hypothetical commands @samp{show w} and @samp{show c} should show
377the appropriate parts of the General Public License. Of course, the
378commands you use may be called something other than @samp{show w} and
379@samp{show c}; they could even be mouse-clicks or menu items---whatever
380suits your program.
381
382You should also get your employer (if you work as a programmer) or your
383school, if any, to sign a ``copyright disclaimer'' for the program, if
384necessary. Here is a sample; alter the names:
385
386@smallexample
387@group
388Yoyodyne, Inc., hereby disclaims all copyright
389interest in the program `Gnomovision'
390(which makes passes at compilers) written
391by James Hacker.
392
393@var{signature of Ty Coon}, 1 April 1989
394Ty Coon, President of Vice
395@end group
396@end smallexample
397
398This General Public License does not permit incorporating your program into
399proprietary programs. If your program is a subroutine library, you may
400consider it more useful to permit linking proprietary applications with the
401library. If this is what you want to do, use the GNU Library General
402Public License instead of this License.
403
404@node Introduction, Types of Lisp Object, Copying, Top
405@chapter Introduction
406
407 Most of the GNU Emacs text editor is written in the programming
408language called Emacs Lisp. You can write new code in Emacs Lisp and
409install it as an extension to the editor. However, Emacs Lisp is more
410than a mere ``extension language''; it is a full computer programming
411language in its own right. You can use it as you would any other
412programming language.
413
414 Because Emacs Lisp is designed for use in an editor, it has special
415features for scanning and parsing text as well as features for handling
416files, buffers, displays, subprocesses, and so on. Emacs Lisp is
417closely integrated with the editing facilities; thus, editing commands
418are functions that can also conveniently be called from Lisp programs,
419and parameters for customization are ordinary Lisp variables.
420
421 This manual describes Emacs Lisp, presuming considerable familiarity
422with the use of Emacs for editing. (See @cite{The GNU Emacs Manual},
423for this basic information.) Generally speaking, the earlier chapters
424describe features of Emacs Lisp that have counterparts in many
425programming languages, and later chapters describe features that are
426peculiar to Emacs Lisp or relate specifically to editing.
427
428 This is edition 2.3.
429
430@menu
431* Caveats:: Flaws and a request for help.
432* Lisp History:: Emacs Lisp is descended from Maclisp.
433* Conventions:: How the manual is formatted.
434* Acknowledgements:: The authors, editors, and sponsors of this manual.
435@end menu
436
437@node Caveats
438@section Caveats
439
440 This manual has gone through numerous drafts. It is nearly complete
441but not flawless. There are a few sections which are not included,
442either because we consider them secondary (such as most of the
443individual modes) or because they are yet to be written.
444
445 Because we are not able to deal with them completely, we have left out
446several parts intentionally. This includes most information about usage
447on VMS.
448
449 The manual should be fully correct in what it does cover, and it is
450therefore open to criticism on anything it says---from specific examples
451and descriptive text, to the ordering of chapters and sections. If
452something is confusing, or you find that you have to look at the sources
453or experiment to learn something not covered in the manual, then perhaps
454the manual should be fixed. Please let us know.
455
456@iftex
457 As you use the manual, we ask that you mark pages with corrections so
458you can later look them up and send them in. If you think of a simple,
459real life example for a function or group of functions, please make an
460effort to write it up and send it in. Please reference any comments to
461the chapter name, section name, and function name, as appropriate, since
462page numbers and chapter and section numbers will change. Also state
463the number of the edition which you are criticizing.
464@end iftex
465@ifinfo
466
467As you use this manual, we ask that you send corrections as soon as you
468find them. If you think of a simple, real life example for a function
469or group of functions, please make an effort to write it up and send it
470in. Please reference any comments to the node name and function or
471variable name, as appropriate. Also state the number of the edition
472which you are criticizing.
473@end ifinfo
474
475Please mail comments and corrections to
476
477@example
478bug-lisp-manual@@prep.ai.mit.edu
479@end example
480
481@noindent
482We let mail to this list accumulate unread until someone decides to
483apply the corrections. Months, and sometimes years, go by between
484updates. So please attach no significance to the lack of a reply---your
485mail @emph{will} be acted on in due time. If you want to contact the
486Emacs maintainers more quickly, send mail to
487@code{bug-gnu-emacs@@prep.ai.mit.edu}.
488
489@display
490 --Bil Lewis, Dan LaLiberte, Richard Stallman
491@end display
492
493@node Lisp History
494@section Lisp History
495@cindex Lisp history
496
497 Lisp (LISt Processing language) was first developed in the late 1950s
498at the Massachusetts Institute of Technology for research in artificial
499intelligence. The great power of the Lisp language makes it superior
500for other purposes as well, such as writing editing commands.
501
502@cindex Maclisp
503@cindex Common Lisp
504 Dozens of Lisp implementations have been built over the years, each
505with its own idiosyncrasies. Many of them were inspired by Maclisp,
506which was written in the 1960's at MIT's Project MAC. Eventually the
507implementors of the descendents of Maclisp came together and developed a
508standard for Lisp systems, called Common Lisp.
509
510 GNU Emacs Lisp is largely inspired by Maclisp, and a little by Common
511Lisp. If you know Common Lisp, you will notice many similarities.
512However, many of the features of Common Lisp have been omitted or
513simplified in order to reduce the memory requirements of GNU Emacs.
514Sometimes the simplifications are so drastic that a Common Lisp user
515might be very confused. We will occasionally point out how GNU Emacs
516Lisp differs from Common Lisp. If you don't know Common Lisp, don't
517worry about it; this manual is self-contained.
518
519@node Conventions
520@section Conventions
521
522This section explains the notational conventions that are used in this
523manual. You may want to skip this section and refer back to it later.
524
525@menu
526* Some Terms:: Explanation of terms we use in this manual.
527* nil and t:: How the symbols @code{nil} and @code{t} are used.
528* Evaluation Notation:: The format we use for examples of evaluation.
529* Printing Notation:: The format we use for examples that print output.
530* Error Messages:: The format we use for examples of errors.
531* Buffer Text Notation:: The format we use for buffer contents in examples.
532* Format of Descriptions:: Notation for describing functions, variables, etc.
533@end menu
534
535@node Some Terms
536@subsection Some Terms
537
538 Throughout this manual, the phrases ``the Lisp reader'' and ``the Lisp
539printer'' are used to refer to those routines in Lisp that convert
540textual representations of Lisp objects into actual objects, and vice
541versa. @xref{Printed Representation}, for more details. You, the
542person reading this manual, are thought of as ``the programmer'' and are
543addressed as ``you''. ``The user'' is the person who uses Lisp programs
544including those you write.
545
546@cindex fonts
547 Examples of Lisp code appear in this font or form: @code{(list 1 2
5483)}. Names that represent arguments or metasyntactic variables appear
549in this font or form: @var{first-number}.
550
551@node nil and t
552@subsection @code{nil} and @code{t}
553@cindex @code{nil}, uses of
554@cindex truth value
555@cindex boolean
556@cindex false
557
558 In Lisp, the symbol @code{nil} is overloaded with three meanings: it
559is a symbol with the name @samp{nil}; it is the logical truth value
560@var{false}; and it is the empty list---the list of zero elements.
561When used as a variable, @code{nil} always has the value @code{nil}.
562
563 As far as the Lisp reader is concerned, @samp{()} and @samp{nil} are
564identical: they stand for the same object, the symbol @code{nil}. The
565different ways of writing the symbol are intended entirely for human
566readers. After the Lisp reader has read either @samp{()} or @samp{nil},
567there is no way to determine which representation was actually written
568by the programmer.
569
570 In this manual, we use @code{()} when we wish to emphasize that it
571means the empty list, and we use @code{nil} when we wish to emphasize
572that it means the truth value @var{false}. That is a good convention to use
573in Lisp programs also.
574
575@example
576(cons 'foo ()) ; @r{Emphasize the empty list}
577(not nil) ; @r{Emphasize the truth value @var{false}}
578@end example
579
580@cindex @code{t} and truth
581@cindex true
582 In contexts where a truth value is expected, any non-@code{nil} value
583is considered to be @var{true}. However, @code{t} is the preferred way
584to represent the truth value @var{true}. When you need to choose a
585value which represents @var{true}, and there is no other basis for
586choosing, use @code{t}. The symbol @code{t} always has value @code{t}.
587
588 In Emacs Lisp, @code{nil} and @code{t} are special symbols that always
589evaluate to themselves. This is so that you do not need to quote them
590to use them as constants in a program. An attempt to change their
591values results in a @code{setting-constant} error. @xref{Accessing
592Variables}.
593
594@node Evaluation Notation
595@subsection Evaluation Notation
596@cindex evaluation notation
597@cindex documentation notation
598
599 A Lisp expression that you can evaluate is called a @dfn{form}.
600Evaluating a form always produces a result, which is a Lisp object. In
601the examples in this manual, this is indicated with @samp{@result{}}:
602
603@example
604(car '(1 2))
605 @result{} 1
606@end example
607
608@noindent
609You can read this as ``@code{(car '(1 2))} evaluates to 1''.
610
611 When a form is a macro call, it expands into a new form for Lisp to
612evaluate. We show the result of the expansion with
613@samp{@expansion{}}. We may or may not show the actual result of the
614evaluation of the expanded form.
615
616@example
617(third '(a b c))
618 @expansion{} (car (cdr (cdr '(a b c))))
619 @result{} c
620@end example
621
622 Sometimes to help describe one form we show another form which
623produces identical results. The exact equivalence of two forms is
624indicated with @samp{@equiv{}}.
625
626@example
627(make-sparse-keymap) @equiv{} (list 'keymap)
628@end example
629
630@node Printing Notation
631@subsection Printing Notation
632@cindex printing notation
633
634 Many of the examples in this manual print text when they are
635evaluated. If you execute the code from an example in a Lisp
636Interaction buffer (such as the buffer @samp{*scratch*}), the printed
637text is inserted into the buffer. If you execute the example by other
638means (such as by evaluating the function @code{eval-region}), it prints
639text by displaying it in the echo area. You should be aware that text
640displayed in the echo area is truncated to a single line.
641
642 Examples in this manual indicate printed text with @samp{@print{}},
643irrespective of where that text goes. The value returned by evaluating
644the form (here @code{bar}) follows on a separate line.
645
646@example
647@group
648(progn (print 'foo) (print 'bar))
649 @print{} foo
650 @print{} bar
651 @result{} bar
652@end group
653@end example
654
655@node Error Messages
656@subsection Error Messages
657@cindex error message notation
658
659 Some examples signal errors. This normally displays an error message
660in the echo area. We show the error message on a line starting with
661@samp{@error{}}. Note that @samp{@error{}} itself does not appear in
662the echo area.
663
664@example
665(+ 23 'x)
666@error{} Wrong type argument: integer-or-marker-p, x
667@end example
668
669@node Buffer Text Notation
670@subsection Buffer Text Notation
671@cindex buffer text notation
672
673 Some examples show modifications to text in a buffer, with ``before''
674and ``after'' versions of the text. These examples show the contents of
675the buffer in question between two lines of dashes containing the buffer
676name. In addition, @samp{@point{}} indicates the location of point.
677(The symbol for point, of course, is not part of the text in the buffer;
678it indicates the place @emph{between} two characters where point is
679located.)
680
681@example
682---------- Buffer: foo ----------
683This is the @point{}contents of foo.
684---------- Buffer: foo ----------
685
686(insert "changed ")
687 @result{} nil
688---------- Buffer: foo ----------
689This is the changed @point{}contents of foo.
690---------- Buffer: foo ----------
691@end example
692
693@node Format of Descriptions
694@subsection Format of Descriptions
695@cindex description format
696
697 Functions, variables, macros, commands, user options, and special
698forms are described in this manual in a uniform format. The first
699line of a description contains the name of the item followed by its
700arguments, if any.
701@ifinfo
702The category---function, variable, or whatever---appears at the
703beginning of the line.
704@end ifinfo
705@iftex
706The category---function, variable, or whatever---is printed next to the
707right margin.
708@end iftex
709The description follows on succeeding lines, sometimes with examples.
710
711@menu
712* A Sample Function Description:: A description of an imaginary
713 function, @code{foo}.
714* A Sample Variable Description:: A description of an imaginary
715 variable,
716 @code{electric-future-map}.
717@end menu
718
719@node A Sample Function Description
720@subsubsection A Sample Function Description
721@cindex function descriptions
722@cindex command descriptions
723@cindex macro descriptions
724@cindex special form descriptions
725
726 In a function description, the name of the function being described
727appears first. It is followed on the same line by a list of parameters.
728The names used for the parameters are also used in the body of the
729description.
730
731 The appearance of the keyword @code{&optional} in the parameter list
732indicates that the arguments for subsequent parameters may be omitted
733(omitted parameters default to @code{nil}). Do not write
734@code{&optional} when you call the function.
735
736 The keyword @code{&rest} (which will always be followed by a single
737parameter) indicates that any number of arguments can follow. The value
738of the single following parameter will be a list of all these arguments.
739Do not write @code{&rest} when you call the function.
740
741 Here is a description of an imaginary function @code{foo}:
742
743@defun foo integer1 &optional integer2 &rest integers
744The function @code{foo} subtracts @var{integer1} from @var{integer2},
745then adds all the rest of the arguments to the result. If @var{integer2}
746is not supplied, then the number 19 is used by default.
747
748@example
749(foo 1 5 3 9)
750 @result{} 16
751(foo 5)
752 @result{} 14
753@end example
754
755More generally,
756
757@example
758(foo @var{w} @var{x} @var{y}@dots{})
759@equiv{}
760(+ (- @var{x} @var{w}) @var{y}@dots{})
761@end example
762@end defun
763
764 Any parameter whose name contains the name of a type (e.g.,
765@var{integer}, @var{integer1} or @var{buffer}) is expected to be of that
766type. A plural of a type (such as @var{buffers}) often means a list of
767objects of that type. Parameters named @var{object} may be of any type.
768(@xref{Types of Lisp Object}, for a list of Emacs object types.)
769Parameters with other sorts of names (e.g., @var{new-file}) are
770discussed specifically in the description of the function. In some
771sections, features common to parameters of several functions are
772described at the beginning.
773
774 @xref{Lambda Expressions}, for a more complete description of optional
775and rest arguments.
776
777 Command, macro, and special form descriptions have the same format,
778but the word `Function' is replaced by `Command', `Macro', or `Special
779Form', respectively. Commands are simply functions that may be called
780interactively; macros process their arguments differently from functions
781(the arguments are not evaluated), but are presented the same way.
782
783 Special form descriptions use a more complex notation to specify
784optional and repeated parameters because they can break the argument
785list down into separate arguments in more complicated ways.
786@samp{@code{@r{[}@var{optional-arg}@r{]}}} means that @var{optional-arg} is
787optional and @samp{@var{repeated-args}@dots{}} stands for zero or more
788arguments. Parentheses are used when several arguments are grouped into
789additional levels of list structure. Here is an example:
790
791@defspec count-loop (@var{var} [@var{from} @var{to} [@var{inc}]]) @var{body}@dots{}
792This imaginary special form implements a loop that executes the
793@var{body} forms and then increments the variable @var{var} on each
794iteration. On the first iteration, the variable has the value
795@var{from}; on subsequent iterations, it is incremented by 1 (or by
796@var{inc} if that is given). The loop exits before executing @var{body}
797if @var{var} equals @var{to}. Here is an example:
798
799@example
800(count-loop (i 0 10)
801 (prin1 i) (princ " ")
802 (prin1 (aref vector i)) (terpri))
803@end example
804
805If @var{from} and @var{to} are omitted, then @var{var} is bound to
806@code{nil} before the loop begins, and the loop exits if @var{var} is
807non-@code{nil} at the beginning of an iteration. Here is an example:
808
809@example
810(count-loop (done)
811 (if (pending)
812 (fixit)
813 (setq done t)))
814@end example
815
816In this special form, the arguments @var{from} and @var{to} are
817optional, but must both be present or both absent. If they are present,
818@var{inc} may optionally be specified as well. These arguments are
819grouped with the argument @var{var} into a list, to distinguish them
820from @var{body}, which includes all remaining elements of the form.
821@end defspec
822
823@node A Sample Variable Description
824@subsubsection A Sample Variable Description
825@cindex variable descriptions
826@cindex option descriptions
827
828 A @dfn{variable} is a name that can hold a value. Although any
829variable can be set by the user, certain variables that exist
830specifically so that users can change them are called @dfn{user
831options}. Ordinary variables and user options are described using a
832format like that for functions except that there are no arguments.
833
834 Here is a description of the imaginary @code{electric-future-map}
835variable.@refill
836
837@defvar electric-future-map
838The value of this variable is a full keymap used by Electric Command
839Future mode. The functions in this map allow you to edit commands you
840have not yet thought about executing.
841@end defvar
842
843 User option descriptions have the same format, but `Variable' is
844replaced by `User Option'.
845
846@node Acknowledgements
847@section Acknowledgements
848
849 This manual was written by Robert Krawitz, Bil Lewis, Dan LaLiberte,
850Richard M. Stallman and Chris Welty, the volunteers of the GNU manual
851group, in an effort extending over several years. Robert J. Chassell
852helped to review and edit the manual, with the support of the Defense
853Advanced Research Projects Agency, ARPA Order 6082, arranged by Warren
854A. Hunt, Jr. of Computational Logic, Inc.
855
856 Corrections were supplied by Karl Berry, Jim Blandy, Bard Bloom,
857Stephane Boucher, David Boyes, Alan Carroll, Richard Davis, Lawrence
858R. Dodd, Peter Doornbosch, David A. Duff, Chris Eich, Beverly
859Erlebacher, David Eckelkamp, Ralf Fassel, Eirik Fuller, Stephen Gildea,
860Bob Glickstein, Eric Hanchrow, George Hartzell, Nathan Hess, Masayuki
861Ida, Dan Jacobson, Jak Kirman, Bob Knighten, Frederick M. Korz, Joe
862Lammens, Glenn M. Lewis, K. Richard Magill, Brian Marick, Roland
863McGrath, Skip Montanaro, John Gardiner Myers, Thomas A. Peterson,
864Francesco Potorti, Friedrich Pukelsheim, Arnold D. Robbins, Raul
865Rockwell, Per Starback, Shinichirou Sugou, Kimmo Suominen, Edward Tharp,
866Bill Trost, Rickard Westman, Jean White, Matthew Wilding, Carl Witty,
867Dale Worley, Rusty Wright, and David D. Zuhn.
diff --git a/lispref/loading.texi b/lispref/loading.texi
new file mode 100644
index 00000000000..59d27a0a13e
--- /dev/null
+++ b/lispref/loading.texi
@@ -0,0 +1,582 @@
1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual.
3@c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4@c See the file elisp.texi for copying conditions.
5@setfilename ../info/loading
6@node Loading, Byte Compilation, Macros, Top
7@chapter Loading
8@cindex loading
9@cindex library
10@cindex Lisp library
11
12 Loading a file of Lisp code means bringing its contents into the Lisp
13environment in the form of Lisp objects. Emacs finds and opens the
14file, reads the text, evaluates each form, and then closes the file.
15
16 The load functions evaluate all the expressions in a file just
17as the @code{eval-current-buffer} function evaluates all the
18expressions in a buffer. The difference is that the load functions
19read and evaluate the text in the file as found on disk, not the text
20in an Emacs buffer.
21
22@cindex top-level form
23 The loaded file must contain Lisp expressions, either as source code
24or, optionally, as byte-compiled code. Each form in the file is called
25a @dfn{top-level form}. There is no special format for the forms in a
26loadable file; any form in a file may equally well be typed directly
27into a buffer and evaluated there. (Indeed, most code is tested this
28way.) Most often, the forms are function definitions and variable
29definitions.
30
31 A file containing Lisp code is often called a @dfn{library}. Thus,
32the ``Rmail library'' is a file containing code for Rmail mode.
33Similarly, a ``Lisp library directory'' is a directory of files
34containing Lisp code.
35
36@menu
37* How Programs Do Loading:: The @code{load} function and others.
38* Autoload:: Setting up a function to autoload.
39* Repeated Loading:: Precautions about loading a file twice.
40* Features:: Loading a library if it isn't already loaded.
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.
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 in a file;
51trying to call the autoloading function loads the file to get the
52function's real definition (@pxref{Autoload}). @code{require} loads a
53file if it isn't already loaded (@pxref{Features}). Ultimately, all
54these facilities call the @code{load} function to do the work.
55
56@defun load filename &optional missing-ok nomessage nosuffix
57This function finds and opens a file of Lisp code, evaluates all the
58forms in it, and closes the file.
59
60To find the file, @code{load} first looks for a file named
61@file{@var{filename}.elc}, that is, for a file whose name is
62@var{filename} with @samp{.elc} appended. If such a file exists, it is
63loaded. If there is no file by that name, then @code{load} looks for a
64file names @file{@var{filename}.el}. If that file exists, it is loaded.
65Finally, if neither of those names is found, @code{load} looks for a
66file named @var{filename} with nothing appended, and loads it if it
67exists. (The @code{load} function is not clever about looking at
68@var{filename}. In the perverse case of a file named @file{foo.el.el},
69evaluation of @code{(load "foo.el")} will indeed find it.)
70
71If the optional argument @var{nosuffix} is non-@code{nil}, then the
72suffixes @samp{.elc} and @samp{.el} are not tried. In this case, you
73must specify the precise file name you want.
74
75If @var{filename} is a relative file name, such as @file{foo} or
76@file{baz/foo.bar}, @code{load} searches for the file using the variable
77@code{load-path}. It appends @var{filename} to each of the directories
78listed in @code{load-path}, and loads the first file it finds whose name
79matches. The current default directory is tried only if it is specified
80in @code{load-path}, where @code{nil} stands for the default directory.
81@code{load} tries all three possible suffixes in the first directory in
82@code{load-path}, then all three suffixes in the second directory, and
83so on.
84
85If you get a warning that @file{foo.elc} is older than @file{foo.el}, it
86means you should consider recompiling @file{foo.el}. @xref{Byte
87Compilation}.
88
89Messages like @samp{Loading foo...} and @samp{Loading foo...done} appear
90in the echo area during loading unless @var{nomessage} is
91non-@code{nil}.
92
93@cindex load errors
94Any unhandled errors while loading a file terminate loading. If the
95load was done for the sake of @code{autoload}, certain kinds of
96top-level forms, those which define functions, are undone.
97
98@kindex file-error
99If @code{load} can't find the file to load, then normally it signals the
100error @code{file-error} (with @samp{Cannot open load file
101@var{filename}}). But if @var{missing-ok} is non-@code{nil}, then
102@code{load} just returns @code{nil}.
103
104@code{load} returns @code{t} if the file loads successfully.
105@end defun
106
107@ignore
108@deffn Command load-file filename
109This function loads the file @var{filename}. If @var{filename} is an
110absolute file name, then it is loaded. If it is relative, then the
111current default directory is assumed. @code{load-path} is not used, and
112suffixes are not appended. Use this function if you wish to specify
113the file to be loaded exactly.
114@end deffn
115
116@deffn Command load-library library
117This function loads the library named @var{library}. A library is
118nothing more than a file that may be loaded as described earlier. This
119function is identical to @code{load}, save that it reads a file name
120interactively with completion.
121@end deffn
122@end ignore
123
124@defopt load-path
125@cindex @code{EMACSLOADPATH} environment variable
126The value of this variable is a list of directories to search when
127loading files with @code{load}. Each element is a string (which must be
128a directory name) or @code{nil} (which stands for the current working
129directory). The value of @code{load-path} is initialized from the
130environment variable @code{EMACSLOADPATH}, if that exists; otherwise its
131default value is specified in @file{emacs/src/paths.h} when Emacs is
132built.
133
134The syntax of @code{EMACSLOADPATH} is the same as used for @code{PATH};
135@samp{:} separates directory names, and @samp{.} is used for the current
136default directory. Here is an example of how to set your
137@code{EMACSLOADPATH} variable from a @code{csh} @file{.login} file:
138
139@c This overfull hbox is OK. --rjc 16mar92
140@smallexample
141setenv EMACSLOADPATH .:/user/bil/emacs:/usr/lib/emacs/lisp
142@end smallexample
143
144Here is how to set it using @code{sh}:
145
146@smallexample
147export EMACSLOADPATH
148EMACSLOADPATH=.:/user/bil/emacs:/usr/local/lib/emacs/lisp
149@end smallexample
150
151Here is an example of code you can place in a @file{.emacs} file to add
152several directories to the front of your default @code{load-path}:
153
154@smallexample
155(setq load-path
156 (append (list nil "/user/bil/emacs"
157 "/usr/local/lisplib"
158 (expand-file-name "~/emacs"))
159 load-path))
160@end smallexample
161
162@c Wordy to rid us of an overfull hbox. --rjc 15mar92
163@noindent
164In this example, the path searches the current working directory first,
165followed then by the @file{/user/bil/emacs} directory and then by
166the @file{/usr/local/lisplib} directory,
167which are then followed by the standard directories for Lisp code.
168
169The command line options @samp{-l} or @samp{-load} specify Lispa library
170to load. Since this file might be in the current directory, Emacs 18
171temporarily adds the current directory to the front of @code{load-path}
172so the file can be found there. Newer Emacs versions also find such
173files in the current directory, but without altering @code{load-path}.
174@end defopt
175
176@defvar load-in-progress
177This variable is non-@code{nil} if Emacs is in the process of loading a
178file, and it is @code{nil} otherwise. This is how @code{defun} and
179@code{provide} determine whether a load is in progress, so that their
180effect can be undone if the load fails.
181@end defvar
182
183 To learn how @code{load} is used to build Emacs, see @ref{Building Emacs}.
184
185@node Autoload
186@section Autoload
187@cindex autoload
188
189 The @dfn{autoload} facility allows you to make a function or macro
190available but put off loading its actual definition. The first call to
191the function automatically reads the proper file to install the real
192definition and other associated code, then runs the real definition
193as if it had been loaded all along.
194
195 There are two ways to set up an autoloaded function: by calling
196@code{autoload}, and by writing a special ``magic'' comment in the
197source before the real definition. @code{autoload} is the low-level
198primitive for autoloading; any Lisp program can call @code{autoload} at
199any time. Magic comments do nothing on their own; they serve as a guide
200for the command @code{update-file-autoloads}, which constructs calls to
201@code{autoload} and arranges to execute them when Emacs is built. Magic
202comments are the most convenient way to make a function autoload, but
203only for packages installed along with Emacs.
204
205@defun autoload symbol filename &optional docstring interactive type
206This function defines the function (or macro) named @var{symbol} so as
207to load automatically from @var{filename}. The string @var{filename}
208specifies the file to load to get the real definition of @var{function}.
209
210The argument @var{docstring} is the documentation string for the
211function. Normally, this is the identical to the documentation string
212in the function definition itself. Specifying the documentation string
213in the call to @code{autoload} makes it possible to look at the
214documentation without loading the function's real definition.
215
216If @var{interactive} is non-@code{nil}, then the function can be called
217interactively. This lets completion in @kbd{M-x} work without loading
218the function's real definition. The complete interactive specification
219need not be given here; it's not needed unless the user actually calls
220@var{function}, and when that happens, it's time to load the real
221definition.
222
223You can autoload macros and keymaps as well as ordinary functions.
224Specify @var{type} as @code{macro} if @var{function} is really a macro.
225Specify @var{type} as @code{keymap} if @var{function} is really a
226keymap. Various parts of Emacs need to know this information without
227loading the real definition.
228
229@cindex function cell in autoload
230If @var{symbol} already has a non-void function definition that is not
231an autoload object, @code{autoload} does nothing and returns @code{nil}.
232If the function cell of @var{symbol} is void, or is already an autoload
233object, then it is defined as an autoload object like this:
234
235@example
236(autoload @var{filename} @var{docstring} @var{interactive} @var{type})
237@end example
238
239For example,
240
241@example
242(symbol-function 'run-prolog)
243 @result{} (autoload "prolog" 169681 t nil)
244@end example
245
246@noindent
247In this case, @code{"prolog"} is the name of the file to load, 169681
248refers to the documentation string in the @file{emacs/etc/DOC} file
249(@pxref{Documentation Basics}), @code{t} means the function is
250interactive, and @code{nil} that it is not a macro or a keymap.
251@end defun
252
253@cindex autoload errors
254 The autoloaded file usually contains other definitions and may require
255or provide one or more features. If the file is not completely loaded
256(due to an error in the evaluation of its contents), any function
257definitions or @code{provide} calls that occurred during the load are
258undone. This is to ensure that the next attempt to call any function
259autoloading from this file will try again to load the file. If not for
260this, then some of the functions in the file might appear defined, but
261they might fail to work properly for the lack of certain subroutines
262defined later in the file and not loaded successfully.
263
264 If the autoloaded file fails to define the desired Lisp function or
265macro, then an error is signaled with data @code{"Autoloading failed to
266define function @var{function-name}"}.
267
268@findex update-file-autoloads
269@findex update-directory-autoloads
270 A magic autoload comment looks like @samp{;;;###autoload}, on a line
271by itself, just before the real definition of the function in its
272autoloadable source file. The command @kbd{M-x update-file-autoloads}
273writes a corresponding @code{autoload} call into @file{loaddefs.el}.
274Building Emacs loads @file{loaddefs.el} and thus calls @code{autoload}.
275@kbd{M-x update-directory-autoloads} is even more powerful; it updates
276autoloads for all files in the current directory.
277
278 The same magic comment can copy any kind of form into
279@file{loaddefs.el}. If the form following the magic comment is not a
280function definition, it is copied verbatim. You can also use a magic
281comment to execute a form at build time executing it when the file
282itself is loaded. To do this, write the form @dfn{on the same line} as
283the magic comment. Since it is in a comment, it does nothing when you
284load the source file; but @code{update-file-autoloads} copies it to
285@file{loaddefs.el}, where it is executed while building Emacs.
286
287 The following example shows how @code{doctor} is prepared for
288autoloading with a magic comment:
289
290@smallexample
291;;;###autoload
292(defun doctor ()
293 "Switch to *doctor* buffer and start giving psychotherapy."
294 (interactive)
295 (switch-to-buffer "*doctor*")
296 (doctor-mode))
297@end smallexample
298
299@noindent
300Here's what that produces in @file{loaddefs.el}:
301
302@smallexample
303(autoload 'doctor "doctor"
304 "\
305Switch to *doctor* buffer and start giving psychotherapy."
306 t)
307@end smallexample
308
309@noindent
310The backslash and newline immediately following the double-quote are a
311convention used only in the preloaded Lisp files such as
312@file{loaddefs.el}; they tell @code{make-docfile} to put the
313documentation string in the @file{etc/DOC} file. @xref{Building Emacs}.
314
315@node Repeated Loading
316@comment node-name, next, previous, up
317@section Repeated Loading
318@cindex repeated loading
319
320 You may load one file more than once in an Emacs session. For
321example, after you have rewritten and reinstalled a function definition
322by editing it in a buffer, you may wish to return to the original
323version; you can do this by reloading the file it came from.
324
325 When you load or reload files, bear in mind that the @code{load} and
326@code{load-library} functions automatically load a byte-compiled file
327rather than a non-compiled file of similar name. If you rewrite a file
328that you intend to save and reinstall, remember to byte-compile it if
329necessary; otherwise you may find yourself inadvertently reloading the
330older, byte-compiled file instead of your newer, non-compiled file!
331
332 When writing the forms in a Lisp library file, keep in mind that the
333file might be loaded more than once. For example, the choice of
334@code{defvar} vs.@: @code{defconst} for defining a variable depends on
335whether it is desirable to reinitialize the variable if the library is
336reloaded: @code{defconst} does so, and @code{defvar} does not.
337(@xref{Defining Variables}.)
338
339 The simplest way to add an element to an alist is like this:
340
341@example
342(setq minor-mode-alist
343 (cons '(leif-mode " Leif") minor-mode-alist))
344@end example
345
346@noindent
347But this would add multiple elements if the library is reloaded.
348To avoid the problem, write this:
349
350@example
351(or (assq 'leif-mode minor-mode-alist)
352 (setq minor-mode-alist
353 (cons '(leif-mode " Leif") minor-mode-alist)))
354@end example
355
356 Occasionally you will want to test explicitly whether a library has
357already been loaded. Here's one way to test, in a library, whether it
358has been loaded before:
359
360@example
361(if (not (boundp 'foo-was-loaded))
362 @var{execute-first-time-only})
363
364(setq foo-was-loaded t)
365@end example
366
367@noindent
368If the library uses @code{provide} to provide a named feature, you can
369use @code{featurep} to test whether the library has been loaded.
370@xref{Features}.
371
372@node Features
373@section Features
374@cindex features
375@cindex requiring features
376@cindex providing features
377
378 @code{provide} and @code{require} are an alternative to
379@code{autoload} for loading files automatically. They work in terms of
380named @dfn{features}. Autoloading is triggered by calling a specific
381function, but a feature is loaded the first time another program asks
382for it by name.
383
384 A feature name is a symbol that stands for a collection of functions,
385variables, etc. The file that defines them should @dfn{provide} the
386feature. Another program that uses them may ensure they are defined by
387@dfn{requiring} the feature. This loads the file of definitions if it
388hasn't been loaded already.
389
390 To require the presence of a feature, call @code{require} with the
391feature name as argument. @code{require} looks in the global variable
392@code{features} to see whether the desired feature has been provided
393already. If not, it loads the feature from the appropriate file. This
394file should call @code{provide} at the top-level to add the feature to
395@code{features}; if it fails to do so, @code{require} signals an error.
396@cindex load error with require
397
398 Features are normally named after the files that provide them, so that
399@code{require} need not be given the file name.
400
401 For example, in @file{emacs/lisp/prolog.el},
402the definition for @code{run-prolog} includes the following code:
403
404@smallexample
405(defun run-prolog ()
406 "Run an inferior Prolog process, input and output via buffer *prolog*."
407 (interactive)
408 (require 'comint)
409 (switch-to-buffer (make-comint "prolog" prolog-program-name))
410 (inferior-prolog-mode))
411@end smallexample
412
413@noindent
414The expression @code{(require 'comint)} loads the file @file{comint.el}
415if it has not yet been loaded. This ensures that @code{make-comint} is
416defined.
417
418The @file{comint.el} file contains the following top-level expression:
419
420@smallexample
421(provide 'comint)
422@end smallexample
423
424@noindent
425This adds @code{comint} to the global @code{features} list, so that
426@code{(require 'comint)} will henceforth know that nothing needs to be
427done.
428
429@cindex byte-compiling @code{require}
430 When @code{require} is used at top-level in a file, it takes effect
431when you byte-compile that file (@pxref{Byte Compilation}) as well as
432when you load it. This is in case the required package contains macros
433that the byte compiler must know about.
434
435 Although top-level calls to @code{require} are evaluated during
436byte compilation, @code{provide} calls are not. Therefore, you can
437ensure that a file of definitions is loaded before it is byte-compiled
438by including a @code{provide} followed by a @code{require} for the same
439feature, as in the following example.
440
441@smallexample
442@group
443(provide 'my-feature) ; @r{Ignored by byte compiler,}
444 ; @r{evaluated by @code{load}.}
445(require 'my-feature) ; @r{Evaluated by byte compiler.}
446@end group
447@end smallexample
448
449@defun provide feature
450This function announces that @var{feature} is now loaded, or being
451loaded, into the current Emacs session. This means that the facilities
452associated with @var{feature} are or will be available for other Lisp
453programs.
454
455The direct effect of calling @code{provide} is to add @var{feature} to
456the front of the list @code{features} if it is not already in the list.
457The argument @var{feature} must be a symbol. @code{provide} returns
458@var{feature}.
459
460@smallexample
461features
462 @result{} (bar bish)
463
464(provide 'foo)
465 @result{} foo
466features
467 @result{} (foo bar bish)
468@end smallexample
469
470If the file isn't completely loaded, due to an error in the evaluating
471its contents, any function definitions or @code{provide} calls that
472occurred during the load are undone. @xref{Autoload}.
473@end defun
474
475@defun require feature &optional filename
476This function checks whether @var{feature} is present in the current
477Emacs session (using @code{(featurep @var{feature})}; see below). If it
478is not, then @code{require} loads @var{filename} with @code{load}. If
479@var{filename} is not supplied, then the name of the symbol
480@var{feature} is used as the file name to load.
481
482If loading the file fails to provide @var{feature}, @code{require}
483signals an error, @samp{Required feature @var{feature} was not
484provided}.
485@end defun
486
487@defun featurep feature
488This function returns @code{t} if @var{feature} has been provided in the
489current Emacs session (i.e., @var{feature} is a member of
490@code{features}.)
491@end defun
492
493@defvar features
494The value of this variable is a list of symbols that are the features
495loaded in the current Emacs session. Each symbol was put in this list
496with a call to @code{provide}. The order of the elements in the
497@code{features} list is not significant.
498@end defvar
499
500@node Unloading
501@section Unloading
502@cindex unloading
503
504@c Emacs 19 feature
505 You can discard the functions and variables loaded by a library to
506reclaim memory for other Lisp objects. To do this, use the function
507@code{unload-feature}:
508
509@deffn Command unload-feature feature
510This command unloads the library that provided feature @var{feature}.
511It undefines all functions and variables defined with @code{defvar},
512@code{defmacro}, @code{defconst}, @code{defsubst} and @code{defalias} by
513that library. It then restores any autoloads associated with those
514symbols.
515@end deffn
516
517 The @code{unload-feature} function is written in Lisp; its actions are
518based on the variable @code{load-history}.
519
520@defvar load-history
521This variable's value is an alist connecting library names with the
522names of functions and variables they define, the features they provide,
523and the features they require.
524
525Each element is a list and describes one library. The @sc{car} of the
526list is the name of the library, as a string. The rest of the list is
527composed of these kinds of objects:
528
529@itemize @bullet
530@item
531Symbols, which were defined as functions or variables.
532@item
533Lists of the form @code{(require . @var{feature})} indicating
534features that were required.
535@item
536Lists of the form @code{(provide . @var{feature})} indicating
537features that were provided.
538@end itemize
539
540The value of @code{load-history} may have one element whose @sc{car} is
541@code{nil}. This element describes definitions made with
542@code{eval-buffer} on a buffer that is not visiting a file.
543@end defvar
544
545 The command @code{eval-region} updates @code{load-history}, but does so
546by adding the symbols defined to the element for the file being visited,
547rather than replacing that element.
548
549@node Hooks for Loading
550@section Hooks for Loading
551@cindex loading hooks
552@cindex hooks for loading
553
554You can ask for code to be executed if and when a particular library is
555loaded, by calling @code{eval-after-load}.
556
557@defun eval-after-load library form
558This function arranges to evaluate @var{form} at the end of loading the
559library @var{library}, if and when @var{library} is loaded.
560
561The library name @var{library} must exactly match the argument of
562@code{load}. To get the proper results when an installed library is
563found by searching @code{load-path}, you should not include any
564directory names in @var{library}.
565
566An error in @var{form} does not undo the load, but does prevent
567execution of the rest of @var{form}.
568@end defun
569
570@defvar after-load-alist
571An alist of expressions to evaluate if and when particular libraries are
572loaded. Each element looks like this:
573
574@example
575(@var{filename} @var{forms}@dots{})
576@end example
577
578The function @code{load} checks @code{after-load-alist} in order to
579implement @code{eval-after-load}.
580@end defvar
581
582@c Emacs 19 feature