annotate lispref/control.texi @ 6885:3c007de39916

(shell-dynamic-complete-as-command): Don't match ignored-extensions if it's nil.
author Karl Heuer <kwzh@gnu.org>
date Thu, 14 Apr 1994 19:19:30 +0000
parents 974a37e5c414
children cd57cd335fff
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6453
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1 @c -*-texinfo-*-
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
2 @c This is part of the GNU Emacs Lisp Reference Manual.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
4 @c See the file elisp.texi for copying conditions.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
5 @setfilename ../info/control
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
6 @node Control Structures, Variables, Evaluation, Top
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
7 @chapter Control Structures
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
8 @cindex special forms for control structures
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
9 @cindex control structures
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
10
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
11 A Lisp program consists of expressions or @dfn{forms} (@pxref{Forms}).
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
12 We control the order of execution of the forms by enclosing them in
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
13 @dfn{control structures}. Control structures are special forms which
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
14 control when, whether, or how many times to execute the forms they contain.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
15
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
16 The simplest control structure is sequential execution: first form
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
17 @var{a}, then form @var{b}, and so on. This is what happens when you
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
18 write several forms in succession in the body of a function, or at top
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
19 level in a file of Lisp code---the forms are executed in the order they
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
20 are written. We call this @dfn{textual order}. For example, if a
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
21 function body consists of two forms @var{a} and @var{b}, evaluation of
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
22 the function evaluates first @var{a} and then @var{b}, and the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
23 function's value is the value of @var{b}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
24
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
25 Emacs Lisp provides several kinds of control structure, including
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
26 other varieties of sequencing, function calls, conditionals, iteration,
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
27 and (controlled) jumps. The built-in control structures are special
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
28 forms since their subforms are not necessarily evaluated. You can use
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
29 macros to define your own control structure constructs (@pxref{Macros}).
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
30
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
31 @menu
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
32 * Sequencing:: Evaluation in textual order.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
33 * Conditionals:: @code{if}, @code{cond}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
34 * Combining Conditions:: @code{and}, @code{or}, @code{not}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
35 * Iteration:: @code{while} loops.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
36 * Nonlocal Exits:: Jumping out of a sequence.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
37 @end menu
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
38
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
39 @node Sequencing
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
40 @section Sequencing
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
41
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
42 Evaluating forms in the order they are written is the most common
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
43 control structure. Sometimes this happens automatically, such as in a
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
44 function body. Elsewhere you must use a control structure construct to
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
45 do this: @code{progn}, the simplest control construct of Lisp.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
46
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
47 A @code{progn} special form looks like this:
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
48
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
49 @example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
50 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
51 (progn @var{a} @var{b} @var{c} @dots{})
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
52 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
53 @end example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
54
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
55 @noindent
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
56 and it says to execute the forms @var{a}, @var{b}, @var{c} and so on, in
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
57 that order. These forms are called the body of the @code{progn} form.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
58 The value of the last form in the body becomes the value of the entire
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
59 @code{progn}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
60
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
61 @cindex implicit @code{progn}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
62 In the early days of Lisp, @code{progn} was the only way to execute
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
63 two or more forms in succession and use the value of the last of them.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
64 But programmers found they often needed to use a @code{progn} in the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
65 body of a function, where (at that time) only one form was allowed. So
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
66 the body of a function was made into an ``implicit @code{progn}'':
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
67 several forms are allowed just as in the body of an actual @code{progn}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
68 Many other control structures likewise contain an implicit @code{progn}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
69 As a result, @code{progn} is not used as often as it used to be. It is
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
70 needed now most often inside of an @code{unwind-protect}, @code{and},
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
71 @code{or}, or the @var{else}-part of an @code{if}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
72
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
73 @defspec progn forms@dots{}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
74 This special form evaluates all of the @var{forms}, in textual
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
75 order, returning the result of the final form.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
76
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
77 @example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
78 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
79 (progn (print "The first form")
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
80 (print "The second form")
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
81 (print "The third form"))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
82 @print{} "The first form"
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
83 @print{} "The second form"
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
84 @print{} "The third form"
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
85 @result{} "The third form"
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
86 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
87 @end example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
88 @end defspec
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
89
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
90 Two other control constructs likewise evaluate a series of forms but return
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
91 a different value:
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
92
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
93 @defspec prog1 form1 forms@dots{}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
94 This special form evaluates @var{form1} and all of the @var{forms}, in
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
95 textual order, returning the result of @var{form1}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
96
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
97 @example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
98 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
99 (prog1 (print "The first form")
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
100 (print "The second form")
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
101 (print "The third form"))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
102 @print{} "The first form"
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
103 @print{} "The second form"
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
104 @print{} "The third form"
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
105 @result{} "The first form"
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
106 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
107 @end example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
108
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
109 Here is a way to remove the first element from a list in the variable
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
110 @code{x}, then return the value of that former element:
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
111
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
112 @example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
113 (prog1 (car x) (setq x (cdr x)))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
114 @end example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
115 @end defspec
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
116
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
117 @defspec prog2 form1 form2 forms@dots{}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
118 This special form evaluates @var{form1}, @var{form2}, and all of the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
119 following @var{forms}, in textual order, returning the result of
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
120 @var{form2}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
121
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
122 @example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
123 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
124 (prog2 (print "The first form")
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
125 (print "The second form")
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
126 (print "The third form"))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
127 @print{} "The first form"
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
128 @print{} "The second form"
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
129 @print{} "The third form"
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
130 @result{} "The second form"
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
131 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
132 @end example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
133 @end defspec
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
134
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
135 @node Conditionals
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
136 @section Conditionals
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
137 @cindex conditional evaluation
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
138
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
139 Conditional control structures choose among alternatives. Emacs Lisp
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
140 has two conditional forms: @code{if}, which is much the same as in other
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
141 languages, and @code{cond}, which is a generalized case statement.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
142
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
143 @defspec if condition then-form else-forms@dots{}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
144 @code{if} chooses between the @var{then-form} and the @var{else-forms}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
145 based on the value of @var{condition}. If the evaluated @var{condition} is
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
146 non-@code{nil}, @var{then-form} is evaluated and the result returned.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
147 Otherwise, the @var{else-forms} are evaluated in textual order, and the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
148 value of the last one is returned. (The @var{else} part of @code{if} is
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
149 an example of an implicit @code{progn}. @xref{Sequencing}.)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
150
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
151 If @var{condition} has the value @code{nil}, and no @var{else-forms} are
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
152 given, @code{if} returns @code{nil}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
153
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
154 @code{if} is a special form because the branch which is not selected is
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
155 never evaluated---it is ignored. Thus, in the example below,
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
156 @code{true} is not printed because @code{print} is never called.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
157
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
158 @example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
159 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
160 (if nil
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
161 (print 'true)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
162 'very-false)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
163 @result{} very-false
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
164 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
165 @end example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
166 @end defspec
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
167
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
168 @defspec cond clause@dots{}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
169 @code{cond} chooses among an arbitrary number of alternatives. Each
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
170 @var{clause} in the @code{cond} must be a list. The @sc{car} of this
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
171 list is the @var{condition}; the remaining elements, if any, the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
172 @var{body-forms}. Thus, a clause looks like this:
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
173
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
174 @example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
175 (@var{condition} @var{body-forms}@dots{})
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
176 @end example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
177
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
178 @code{cond} tries the clauses in textual order, by evaluating the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
179 @var{condition} of each clause. If the value of @var{condition} is
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
180 non-@code{nil}, the clause ``succeeds''; then @code{cond} evaluates its
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
181 @var{body-forms}, and the value of the last of @var{body-forms} becomes
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
182 the value of the @code{cond}. The remaining clauses are ignored.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
183
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
184 If the value of @var{condition} is @code{nil}, the clause ``fails'', so
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
185 the @code{cond} moves on to the following clause, trying its
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
186 @var{condition}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
187
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
188 If every @var{condition} evaluates to @code{nil}, so that every clause
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
189 fails, @code{cond} returns @code{nil}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
190
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
191 A clause may also look like this:
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
192
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
193 @example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
194 (@var{condition})
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
195 @end example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
196
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
197 @noindent
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
198 Then, if @var{condition} is non-@code{nil} when tested, the value of
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
199 @var{condition} becomes the value of the @code{cond} form.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
200
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
201 The following example has four clauses, which test for the cases where
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
202 the value of @code{x} is a number, string, buffer and symbol,
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
203 respectively:
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
204
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
205 @example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
206 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
207 (cond ((numberp x) x)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
208 ((stringp x) x)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
209 ((bufferp x)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
210 (setq temporary-hack x) ; @r{multiple body-forms}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
211 (buffer-name x)) ; @r{in one clause}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
212 ((symbolp x) (symbol-value x)))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
213 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
214 @end example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
215
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
216 Often we want to execute the last clause whenever none of the previous
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
217 clauses was successful. To do this, we use @code{t} as the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
218 @var{condition} of the last clause, like this: @code{(t
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
219 @var{body-forms})}. The form @code{t} evaluates to @code{t}, which is
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
220 never @code{nil}, so this clause never fails, provided the @code{cond}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
221 gets to it at all.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
222
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
223 For example,
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
224
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
225 @example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
226 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
227 (cond ((eq a 1) 'foo)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
228 (t "default"))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
229 @result{} "default"
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
230 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
231 @end example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
232
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
233 @noindent
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
234 This expression is a @code{cond} which returns @code{foo} if the value
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
235 of @code{a} is 1, and returns the string @code{"default"} otherwise.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
236 @end defspec
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
237
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
238 Both @code{cond} and @code{if} can usually be written in terms of the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
239 other. Therefore, the choice between them is a matter of style. For
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
240 example:
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
241
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
242 @example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
243 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
244 (if @var{a} @var{b} @var{c})
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
245 @equiv{}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
246 (cond (@var{a} @var{b}) (t @var{c}))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
247 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
248 @end example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
249
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
250 @node Combining Conditions
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
251 @section Constructs for Combining Conditions
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
252
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
253 This section describes three constructs that are often used together
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
254 with @code{if} and @code{cond} to express complicated conditions. The
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
255 constructs @code{and} and @code{or} can also be used individually as
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
256 kinds of multiple conditional constructs.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
257
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
258 @defun not condition
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
259 This function tests for the falsehood of @var{condition}. It returns
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
260 @code{t} if @var{condition} is @code{nil}, and @code{nil} otherwise.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
261 The function @code{not} is identical to @code{null}, and we recommend
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
262 using the name @code{null} if you are testing for an empty list.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
263 @end defun
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
264
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
265 @defspec and conditions@dots{}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
266 The @code{and} special form tests whether all the @var{conditions} are
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
267 true. It works by evaluating the @var{conditions} one by one in the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
268 order written.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
269
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
270 If any of the @var{conditions} evaluates to @code{nil}, then the result
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
271 of the @code{and} must be @code{nil} regardless of the remaining
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
272 @var{conditions}; so @code{and} returns right away, ignoring the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
273 remaining @var{conditions}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
274
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
275 If all the @var{conditions} turn out non-@code{nil}, then the value of
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
276 the last of them becomes the value of the @code{and} form.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
277
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
278 Here is an example. The first condition returns the integer 1, which is
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
279 not @code{nil}. Similarly, the second condition returns the integer 2,
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
280 which is not @code{nil}. The third condition is @code{nil}, so the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
281 remaining condition is never evaluated.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
282
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
283 @example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
284 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
285 (and (print 1) (print 2) nil (print 3))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
286 @print{} 1
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
287 @print{} 2
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
288 @result{} nil
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
289 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
290 @end example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
291
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
292 Here is a more realistic example of using @code{and}:
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
293
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
294 @example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
295 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
296 (if (and (consp foo) (eq (car foo) 'x))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
297 (message "foo is a list starting with x"))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
298 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
299 @end example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
300
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
301 @noindent
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
302 Note that @code{(car foo)} is not executed if @code{(consp foo)} returns
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
303 @code{nil}, thus avoiding an error.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
304
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
305 @code{and} can be expressed in terms of either @code{if} or @code{cond}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
306 For example:
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
307
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
308 @example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
309 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
310 (and @var{arg1} @var{arg2} @var{arg3})
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
311 @equiv{}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
312 (if @var{arg1} (if @var{arg2} @var{arg3}))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
313 @equiv{}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
314 (cond (@var{arg1} (cond (@var{arg2} @var{arg3}))))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
315 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
316 @end example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
317 @end defspec
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
318
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
319 @defspec or conditions@dots{}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
320 The @code{or} special form tests whether at least one of the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
321 @var{conditions} is true. It works by evaluating all the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
322 @var{conditions} one by one in the order written.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
323
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
324 If any of the @var{conditions} evaluates to a non-@code{nil} value, then
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
325 the result of the @code{or} must be non-@code{nil}; so @code{or} returns
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
326 right away, ignoring the remaining @var{conditions}. The value it
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
327 returns is the non-@code{nil} value of the condition just evaluated.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
328
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
329 If all the @var{conditions} turn out @code{nil}, then the @code{or}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
330 expression returns @code{nil}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
331
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
332 For example, this expression tests whether @code{x} is either 0 or
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
333 @code{nil}:
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
334
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
335 @example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
336 (or (eq x nil) (eq x 0))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
337 @end example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
338
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
339 Like the @code{and} construct, @code{or} can be written in terms of
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
340 @code{cond}. For example:
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
341
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
342 @example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
343 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
344 (or @var{arg1} @var{arg2} @var{arg3})
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
345 @equiv{}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
346 (cond (@var{arg1})
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
347 (@var{arg2})
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
348 (@var{arg3}))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
349 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
350 @end example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
351
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
352 You could almost write @code{or} in terms of @code{if}, but not quite:
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
353
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
354 @example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
355 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
356 (if @var{arg1} @var{arg1}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
357 (if @var{arg2} @var{arg2}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
358 @var{arg3}))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
359 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
360 @end example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
361
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
362 @noindent
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
363 This is not completely equivalent because it can evaluate @var{arg1} or
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
364 @var{arg2} twice. By contrast, @code{(or @var{arg1} @var{arg2}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
365 @var{arg3})} never evaluates any argument more than once.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
366 @end defspec
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
367
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
368 @node Iteration
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
369 @section Iteration
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
370 @cindex iteration
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
371 @cindex recursion
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
372
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
373 Iteration means executing part of a program repetitively. For
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
374 example, you might want to repeat some computation once for each element
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
375 of a list, or once for each integer from 0 to @var{n}. You can do this
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
376 in Emacs Lisp with the special form @code{while}:
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
377
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
378 @defspec while condition forms@dots{}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
379 @code{while} first evaluates @var{condition}. If the result is
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
380 non-@code{nil}, it evaluates @var{forms} in textual order. Then it
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
381 reevaluates @var{condition}, and if the result is non-@code{nil}, it
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
382 evaluates @var{forms} again. This process repeats until @var{condition}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
383 evaluates to @code{nil}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
384
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
385 There is no limit on the number of iterations that may occur. The loop
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
386 will continue until either @var{condition} evaluates to @code{nil} or
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
387 until an error or @code{throw} jumps out of it (@pxref{Nonlocal Exits}).
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
388
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
389 The value of a @code{while} form is always @code{nil}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
390
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
391 @example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
392 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
393 (setq num 0)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
394 @result{} 0
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
395 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
396 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
397 (while (< num 4)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
398 (princ (format "Iteration %d." num))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
399 (setq num (1+ num)))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
400 @print{} Iteration 0.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
401 @print{} Iteration 1.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
402 @print{} Iteration 2.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
403 @print{} Iteration 3.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
404 @result{} nil
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
405 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
406 @end example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
407
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
408 If you would like to execute something on each iteration before the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
409 end-test, put it together with the end-test in a @code{progn} as the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
410 first argument of @code{while}, as shown here:
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
411
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
412 @example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
413 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
414 (while (progn
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
415 (forward-line 1)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
416 (not (looking-at "^$"))))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
417 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
418 @end example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
419
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
420 @noindent
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
421 This moves forward one line and continues moving by lines until an empty
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
422 line is reached.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
423 @end defspec
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
424
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
425 @node Nonlocal Exits
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
426 @section Nonlocal Exits
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
427 @cindex nonlocal exits
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
428
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
429 A @dfn{nonlocal exit} is a transfer of control from one point in a
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
430 program to another remote point. Nonlocal exits can occur in Emacs Lisp
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
431 as a result of errors; you can also use them under explicit control.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
432 Nonlocal exits unbind all variable bindings made by the constructs being
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
433 exited.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
434
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
435 @menu
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
436 * Catch and Throw:: Nonlocal exits for the program's own purposes.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
437 * Examples of Catch:: Showing how such nonlocal exits can be written.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
438 * Errors:: How errors are signaled and handled.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
439 * Cleanups:: Arranging to run a cleanup form if an error happens.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
440 @end menu
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
441
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
442 @node Catch and Throw
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
443 @subsection Explicit Nonlocal Exits: @code{catch} and @code{throw}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
444
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
445 Most control constructs affect only the flow of control within the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
446 construct itself. The function @code{throw} is the exception to this
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
447 rule of normal program execution: it performs a nonlocal exit on
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
448 request. (There are other exceptions, but they are for error handling
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
449 only.) @code{throw} is used inside a @code{catch}, and jumps back to
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
450 that @code{catch}. For example:
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
451
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
452 @example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
453 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
454 (catch 'foo
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
455 (progn
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
456 @dots{}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
457 (throw 'foo t)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
458 @dots{}))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
459 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
460 @end example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
461
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
462 @noindent
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
463 The @code{throw} transfers control straight back to the corresponding
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
464 @code{catch}, which returns immediately. The code following the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
465 @code{throw} is not executed. The second argument of @code{throw} is used
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
466 as the return value of the @code{catch}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
467
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
468 The @code{throw} and the @code{catch} are matched through the first
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
469 argument: @code{throw} searches for a @code{catch} whose first argument
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
470 is @code{eq} to the one specified. Thus, in the above example, the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
471 @code{throw} specifies @code{foo}, and the @code{catch} specifies the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
472 same symbol, so that @code{catch} is applicable. If there is more than
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
473 one applicable @code{catch}, the innermost one takes precedence.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
474
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
475 Executing @code{throw} exits all Lisp constructs up to the matching
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
476 @code{catch}, including function calls. When binding constructs such as
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
477 @code{let} or function calls are exited in this way, the bindings are
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
478 unbound, just as they are when these constructs exit normally
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
479 (@pxref{Local Variables}). Likewise, @code{throw} restores the buffer
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
480 and position saved by @code{save-excursion} (@pxref{Excursions}), and
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
481 the narrowing status saved by @code{save-restriction} and the window
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
482 selection saved by @code{save-window-excursion} (@pxref{Window
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
483 Configurations}). It also runs any cleanups established with the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
484 @code{unwind-protect} special form when it exits that form.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
485
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
486 The @code{throw} need not appear lexically within the @code{catch}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
487 that it jumps to. It can equally well be called from another function
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
488 called within the @code{catch}. As long as the @code{throw} takes place
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
489 chronologically after entry to the @code{catch}, and chronologically
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
490 before exit from it, it has access to that @code{catch}. This is why
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
491 @code{throw} can be used in commands such as @code{exit-recursive-edit}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
492 which throw back to the editor command loop (@pxref{Recursive Editing}).
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
493
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
494 @cindex CL note---only @code{throw} in Emacs
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
495 @quotation
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
496 @b{Common Lisp note:} most other versions of Lisp, including Common Lisp,
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
497 have several ways of transferring control nonsequentially: @code{return},
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
498 @code{return-from}, and @code{go}, for example. Emacs Lisp has only
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
499 @code{throw}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
500 @end quotation
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
501
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
502 @defspec catch tag body@dots{}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
503 @cindex tag on run time stack
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
504 @code{catch} establishes a return point for the @code{throw} function. The
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
505 return point is distinguished from other such return points by @var{tag},
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
506 which may be any Lisp object. The argument @var{tag} is evaluated normally
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
507 before the return point is established.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
508
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
509 With the return point in effect, @code{catch} evaluates the forms of the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
510 @var{body} in textual order. If the forms execute normally, without
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
511 error or nonlocal exit, the value of the last body form is returned from
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
512 the @code{catch}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
513
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
514 If a @code{throw} is done within @var{body} specifying the same value
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
515 @var{tag}, the @code{catch} exits immediately; the value it returns is
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
516 whatever was specified as the second argument of @code{throw}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
517 @end defspec
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
518
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
519 @defun throw tag value
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
520 The purpose of @code{throw} is to return from a return point previously
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
521 established with @code{catch}. The argument @var{tag} is used to choose
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
522 among the various existing return points; it must be @code{eq} to the value
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
523 specified in the @code{catch}. If multiple return points match @var{tag},
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
524 the innermost one is used.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
525
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
526 The argument @var{value} is used as the value to return from that
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
527 @code{catch}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
528
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
529 @kindex no-catch
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
530 If no return point is in effect with tag @var{tag}, then a @code{no-catch}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
531 error is signaled with data @code{(@var{tag} @var{value})}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
532 @end defun
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
533
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
534 @node Examples of Catch
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
535 @subsection Examples of @code{catch} and @code{throw}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
536
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
537 One way to use @code{catch} and @code{throw} is to exit from a doubly
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
538 nested loop. (In most languages, this would be done with a ``go to''.)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
539 Here we compute @code{(foo @var{i} @var{j})} for @var{i} and @var{j}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
540 varying from 0 to 9:
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
541
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
542 @example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
543 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
544 (defun search-foo ()
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
545 (catch 'loop
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
546 (let ((i 0))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
547 (while (< i 10)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
548 (let ((j 0))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
549 (while (< j 10)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
550 (if (foo i j)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
551 (throw 'loop (list i j)))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
552 (setq j (1+ j))))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
553 (setq i (1+ i))))))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
554 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
555 @end example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
556
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
557 @noindent
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
558 If @code{foo} ever returns non-@code{nil}, we stop immediately and return a
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
559 list of @var{i} and @var{j}. If @code{foo} always returns @code{nil}, the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
560 @code{catch} returns normally, and the value is @code{nil}, since that
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
561 is the result of the @code{while}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
562
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
563 Here are two tricky examples, slightly different, showing two
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
564 return points at once. First, two return points with the same tag,
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
565 @code{hack}:
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
566
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
567 @example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
568 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
569 (defun catch2 (tag)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
570 (catch tag
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
571 (throw 'hack 'yes)))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
572 @result{} catch2
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
573 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
574
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
575 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
576 (catch 'hack
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
577 (print (catch2 'hack))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
578 'no)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
579 @print{} yes
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
580 @result{} no
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
581 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
582 @end example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
583
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
584 @noindent
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
585 Since both return points have tags that match the @code{throw}, it goes to
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
586 the inner one, the one established in @code{catch2}. Therefore,
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
587 @code{catch2} returns normally with value @code{yes}, and this value is
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
588 printed. Finally the second body form in the outer @code{catch}, which is
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
589 @code{'no}, is evaluated and returned from the outer @code{catch}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
590
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
591 Now let's change the argument given to @code{catch2}:
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
592
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
593 @example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
594 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
595 (defun catch2 (tag)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
596 (catch tag
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
597 (throw 'hack 'yes)))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
598 @result{} catch2
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
599 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
600
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
601 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
602 (catch 'hack
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
603 (print (catch2 'quux))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
604 'no)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
605 @result{} yes
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
606 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
607 @end example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
608
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
609 @noindent
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
610 We still have two return points, but this time only the outer one has the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
611 tag @code{hack}; the inner one has the tag @code{quux} instead. Therefore,
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
612 the @code{throw} returns the value @code{yes} from the outer return point.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
613 The function @code{print} is never called, and the body-form @code{'no} is
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
614 never evaluated.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
615
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
616 @node Errors
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
617 @subsection Errors
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
618 @cindex errors
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
619
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
620 When Emacs Lisp attempts to evaluate a form that, for some reason,
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
621 cannot be evaluated, it @dfn{signals} an @dfn{error}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
622
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
623 When an error is signaled, Emacs's default reaction is to print an
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
624 error message and terminate execution of the current command. This is
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
625 the right thing to do in most cases, such as if you type @kbd{C-f} at
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
626 the end of the buffer.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
627
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
628 In complicated programs, simple termination may not be what you want.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
629 For example, the program may have made temporary changes in data
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
630 structures, or created temporary buffers which should be deleted before
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
631 the program is finished. In such cases, you would use
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
632 @code{unwind-protect} to establish @dfn{cleanup expressions} to be
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
633 evaluated in case of error. Occasionally, you may wish the program to
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
634 continue execution despite an error in a subroutine. In these cases,
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
635 you would use @code{condition-case} to establish @dfn{error handlers} to
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
636 recover control in case of error.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
637
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
638 Resist the temptation to use error handling to transfer control from
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
639 one part of the program to another; use @code{catch} and @code{throw}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
640 instead. @xref{Catch and Throw}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
641
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
642 @menu
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
643 * Signaling Errors:: How to report an error.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
644 * Processing of Errors:: What Emacs does when you report an error.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
645 * Handling Errors:: How you can trap errors and continue execution.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
646 * Error Names:: How errors are classified for trapping them.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
647 @end menu
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
648
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
649 @node Signaling Errors
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
650 @subsubsection How to Signal an Error
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
651 @cindex signaling errors
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
652
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
653 Most errors are signaled ``automatically'' within Lisp primitives
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
654 which you call for other purposes, such as if you try to take the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
655 @sc{car} of an integer or move forward a character at the end of the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
656 buffer; you can also signal errors explicitly with the functions
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
657 @code{error} and @code{signal}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
658
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
659 Quitting, which happens when the user types @kbd{C-g}, is not
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
660 considered an error, but it is handled almost like an error.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
661 @xref{Quitting}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
662
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
663 @defun error format-string &rest args
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
664 This function signals an error with an error message constructed by
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
665 applying @code{format} (@pxref{String Conversion}) to
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
666 @var{format-string} and @var{args}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
667
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
668 These examples show typical uses of @code{error}:
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
669
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
670 @example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
671 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
672 (error "You have committed an error.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
673 Try something else.")
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
674 @error{} You have committed an error.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
675 Try something else.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
676 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
677
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
678 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
679 (error "You have committed %d errors." 10)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
680 @error{} You have committed 10 errors.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
681 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
682 @end example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
683
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
684 @code{error} works by calling @code{signal} with two arguments: the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
685 error symbol @code{error}, and a list containing the string returned by
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
686 @code{format}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
687
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
688 If you want to use your own string as an error message verbatim, don't
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
689 just write @code{(error @var{string})}. If @var{string} contains
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
690 @samp{%}, it will be interpreted as a format specifier, with undesirable
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
691 results. Instead, use @code{(error "%s" @var{string})}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
692 @end defun
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
693
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
694 @defun signal error-symbol data
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
695 This function signals an error named by @var{error-symbol}. The
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
696 argument @var{data} is a list of additional Lisp objects relevant to the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
697 circumstances of the error.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
698
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
699 The argument @var{error-symbol} must be an @dfn{error symbol}---a symbol
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
700 bearing a property @code{error-conditions} whose value is a list of
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
701 condition names. This is how Emacs Lisp classifies different sorts of
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
702 errors.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
703
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
704 The number and significance of the objects in @var{data} depends on
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
705 @var{error-symbol}. For example, with a @code{wrong-type-arg} error,
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
706 there are two objects in the list: a predicate which describes the type
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
707 that was expected, and the object which failed to fit that type.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
708 @xref{Error Names}, for a description of error symbols.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
709
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
710 Both @var{error-symbol} and @var{data} are available to any error
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
711 handlers which handle the error: @code{condition-case} binds a local
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
712 variable to a list of the form @code{(@var{error-symbol} .@:
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
713 @var{data})} (@pxref{Handling Errors}). If the error is not handled,
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
714 these two values are used in printing the error message.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
715
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
716 The function @code{signal} never returns (though in older Emacs versions
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
717 it could sometimes return).
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
718
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
719 @smallexample
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
720 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
721 (signal 'wrong-number-of-arguments '(x y))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
722 @error{} Wrong number of arguments: x, y
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
723 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
724
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
725 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
726 (signal 'no-such-error '("My unknown error condition."))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
727 @error{} peculiar error: "My unknown error condition."
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
728 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
729 @end smallexample
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
730 @end defun
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
731
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
732 @cindex CL note---no continuable errors
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
733 @quotation
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
734 @b{Common Lisp note:} Emacs Lisp has nothing like the Common Lisp
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
735 concept of continuable errors.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
736 @end quotation
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
737
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
738 @node Processing of Errors
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
739 @subsubsection How Emacs Processes Errors
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
740
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
741 When an error is signaled, @code{signal} searches for an active
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
742 @dfn{handler} for the error. A handler is a sequence of Lisp
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
743 expressions designated to be executed if an error happens in part of the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
744 Lisp program. If the error has an applicable handler, the handler is
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
745 executed, and control resumes following the handler. The handler
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
746 executes in the environment of the @code{condition-case} which
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
747 established it; all functions called within that @code{condition-case}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
748 have already been exited, and the handler cannot return to them.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
749
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
750 If there is no applicable handler for the error, the current command is
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
751 terminated and control returns to the editor command loop, because the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
752 command loop has an implicit handler for all kinds of errors. The
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
753 command loop's handler uses the error symbol and associated data to
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
754 print an error message.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
755
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
756 @cindex @code{debug-on-error} use
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
757 An error that has no explicit handler may call the Lisp debugger. The
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
758 debugger is enabled if the variable @code{debug-on-error} (@pxref{Error
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
759 Debugging}) is non-@code{nil}. Unlike error handlers, the debugger runs
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
760 in the environment of the error, so that you can examine values of
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
761 variables precisely as they were at the time of the error.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
762
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
763 @node Handling Errors
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
764 @subsubsection Writing Code to Handle Errors
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
765 @cindex error handler
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
766 @cindex handling errors
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
767
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
768 The usual effect of signaling an error is to terminate the command
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
769 that is running and return immediately to the Emacs editor command loop.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
770 You can arrange to trap errors occurring in a part of your program by
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
771 establishing an error handler, with the special form
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
772 @code{condition-case}. A simple example looks like this:
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
773
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
774 @example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
775 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
776 (condition-case nil
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
777 (delete-file filename)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
778 (error nil))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
779 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
780 @end example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
781
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
782 @noindent
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
783 This deletes the file named @var{filename}, catching any error and
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
784 returning @code{nil} if an error occurs.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
785
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
786 The second argument of @code{condition-case} is called the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
787 @dfn{protected form}. (In the example above, the protected form is a
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
788 call to @code{delete-file}.) The error handlers go into effect when
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
789 this form begins execution and are deactivated when this form returns.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
790 They remain in effect for all the intervening time. In particular, they
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
791 are in effect during the execution of subroutines called by this form,
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
792 and their subroutines, and so on. This is a good thing, since, strictly
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
793 speaking, errors can be signaled only by Lisp primitives (including
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
794 @code{signal} and @code{error}) called by the protected form, not by the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
795 protected form itself.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
796
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
797 The arguments after the protected form are handlers. Each handler
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
798 lists one or more @dfn{condition names} (which are symbols) to specify
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
799 which errors it will handle. The error symbol specified when an error
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
800 is signaled also defines a list of condition names. A handler applies
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
801 to an error if they have any condition names in common. In the example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
802 above, there is one handler, and it specifies one condition name,
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
803 @code{error}, which covers all errors.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
804
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
805 The search for an applicable handler checks all the established handlers
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
806 starting with the most recently established one. Thus, if two nested
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
807 @code{condition-case} forms offer to handle the same error, the inner of
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
808 the two will actually handle it.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
809
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
810 When an error is handled, control returns to the handler. Before this
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
811 happens, Emacs unbinds all variable bindings made by binding constructs
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
812 that are being exited and executes the cleanups of all
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
813 @code{unwind-protect} forms that are exited. Once control arrives at
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
814 the handler, the body of the handler is executed.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
815
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
816 After execution of the handler body, execution continues by returning
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
817 from the @code{condition-case} form. Because the protected form is
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
818 exited completely before execution of the handler, the handler cannot
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
819 resume execution at the point of the error, nor can it examine variable
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
820 bindings that were made within the protected form. All it can do is
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
821 clean up and proceed.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
822
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
823 @code{condition-case} is often used to trap errors that are
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
824 predictable, such as failure to open a file in a call to
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
825 @code{insert-file-contents}. It is also used to trap errors that are
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
826 totally unpredictable, such as when the program evaluates an expression
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
827 read from the user.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
828
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
829 Error signaling and handling have some resemblance to @code{throw} and
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
830 @code{catch}, but they are entirely separate facilities. An error
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
831 cannot be caught by a @code{catch}, and a @code{throw} cannot be handled
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
832 by an error handler (though using @code{throw} when there is no suitable
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
833 @code{catch} signals an error which can be handled).
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
834
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
835 @defspec condition-case var protected-form handlers@dots{}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
836 This special form establishes the error handlers @var{handlers} around
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
837 the execution of @var{protected-form}. If @var{protected-form} executes
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
838 without error, the value it returns becomes the value of the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
839 @code{condition-case} form; in this case, the @code{condition-case} has
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
840 no effect. The @code{condition-case} form makes a difference when an
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
841 error occurs during @var{protected-form}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
842
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
843 Each of the @var{handlers} is a list of the form @code{(@var{conditions}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
844 @var{body}@dots{})}. Here @var{conditions} is an error condition name
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
845 to be handled, or a list of condition names; @var{body} is one or more
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
846 Lisp expressions to be executed when this handler handles an error.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
847 Here are examples of handlers:
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
848
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
849 @smallexample
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
850 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
851 (error nil)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
852
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
853 (arith-error (message "Division by zero"))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
854
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
855 ((arith-error file-error)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
856 (message
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
857 "Either division by zero or failure to open a file"))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
858 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
859 @end smallexample
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
860
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
861 Each error that occurs has an @dfn{error symbol} which describes what
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
862 kind of error it is. The @code{error-conditions} property of this
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
863 symbol is a list of condition names (@pxref{Error Names}). Emacs
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
864 searches all the active @code{condition-case} forms for a handler which
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
865 specifies one or more of these condition names; the innermost matching
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
866 @code{condition-case} handles the error. Within this
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
867 @code{condition-case}, the first applicable handler handles the error.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
868
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
869 After executing the body of the handler, the @code{condition-case}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
870 returns normally, using the value of the last form in the handler body
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
871 as the overall value.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
872
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
873 The argument @var{var} is a variable. @code{condition-case} does not
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
874 bind this variable when executing the @var{protected-form}, only when it
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
875 handles an error. At that time, it binds @var{var} locally to a list of
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
876 the form @code{(@var{error-symbol} . @var{data})}, giving the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
877 particulars of the error. The handler can refer to this list to decide
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
878 what to do. For example, if the error is for failure opening a file,
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
879 the file name is the second element of @var{data}---the third element of
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
880 @var{var}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
881
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
882 If @var{var} is @code{nil}, that means no variable is bound. Then the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
883 error symbol and associated data are not available to the handler.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
884 @end defspec
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
885
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
886 @cindex @code{arith-error} example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
887 Here is an example of using @code{condition-case} to handle the error
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
888 that results from dividing by zero. The handler prints out a warning
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
889 message and returns a very large number.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
890
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
891 @smallexample
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
892 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
893 (defun safe-divide (dividend divisor)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
894 (condition-case err
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
895 ;; @r{Protected form.}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
896 (/ dividend divisor)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
897 ;; @r{The handler.}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
898 (arith-error ; @r{Condition.}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
899 (princ (format "Arithmetic error: %s" err))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
900 1000000)))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
901 @result{} safe-divide
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
902 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
903
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
904 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
905 (safe-divide 5 0)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
906 @print{} Arithmetic error: (arith-error)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
907 @result{} 1000000
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
908 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
909 @end smallexample
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
910
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
911 @noindent
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
912 The 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,
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
913
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
914 @smallexample
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
915 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
916 (safe-divide nil 3)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
917 @error{} Wrong type argument: integer-or-marker-p, nil
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
918 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
919 @end smallexample
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
920
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
921 Here is a @code{condition-case} that catches all kinds of errors,
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
922 including those signaled with @code{error}:
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
923
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
924 @smallexample
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
925 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
926 (setq baz 34)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
927 @result{} 34
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
928 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
929
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
930 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
931 (condition-case err
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
932 (if (eq baz 35)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
933 t
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
934 ;; @r{This is a call to the function @code{error}.}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
935 (error "Rats! The variable %s was %s, not 35." 'baz baz))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
936 ;; @r{This is the handler; it is not a form.}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
937 (error (princ (format "The error was: %s" err))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
938 2))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
939 @print{} The error was: (error "Rats! The variable baz was 34, not 35.")
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
940 @result{} 2
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
941 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
942 @end smallexample
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
943
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
944 @node Error Names
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
945 @subsubsection Error Symbols and Condition Names
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
946 @cindex error symbol
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
947 @cindex error name
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
948 @cindex condition name
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
949 @cindex user-defined error
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
950 @kindex error-conditions
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
951
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
952 When you signal an error, you specify an @dfn{error symbol} to specify
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
953 the kind of error you have in mind. Each error has one and only one
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
954 error symbol to categorize it. This is the finest classification of
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
955 errors defined by the Emacs Lisp language.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
956
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
957 These narrow classifications are grouped into a hierarchy of wider
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
958 classes called @dfn{error conditions}, identified by @dfn{condition
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
959 names}. The narrowest such classes belong to the error symbols
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
960 themselves: each error symbol is also a condition name. There are also
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
961 condition names for more extensive classes, up to the condition name
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
962 @code{error} which takes in all kinds of errors. Thus, each error has
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
963 one or more condition names: @code{error}, the error symbol if that
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
964 is distinct from @code{error}, and perhaps some intermediate
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
965 classifications.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
966
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
967 In order for a symbol to be an error symbol, it must have an
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
968 @code{error-conditions} property which gives a list of condition names.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
969 This list defines the conditions which this kind of error belongs to.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
970 (The error symbol itself, and the symbol @code{error}, should always be
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
971 members of this list.) Thus, the hierarchy of condition names is
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
972 defined by the @code{error-conditions} properties of the error symbols.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
973
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
974 In addition to the @code{error-conditions} list, the error symbol
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
975 should have an @code{error-message} property whose value is a string to
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
976 be printed when that error is signaled but not handled. If the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
977 @code{error-message} property exists, but is not a string, the error
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
978 message @samp{peculiar error} is used.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
979 @cindex peculiar error
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
980
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
981 Here is how we define a new error symbol, @code{new-error}:
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
982
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
983 @example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
984 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
985 (put 'new-error
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
986 'error-conditions
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
987 '(error my-own-errors new-error))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
988 @result{} (error my-own-errors new-error)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
989 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
990 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
991 (put 'new-error 'error-message "A new error")
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
992 @result{} "A new error"
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
993 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
994 @end example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
995
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
996 @noindent
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
997 This error has three condition names: @code{new-error}, the narrowest
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
998 classification; @code{my-own-errors}, which we imagine is a wider
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
999 classification; and @code{error}, which is the widest of all.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1000
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1001 Naturally, Emacs will never signal @code{new-error} on its own; only
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1002 an explicit call to @code{signal} (@pxref{Errors}) in your code can do
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1003 this:
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1004
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1005 @example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1006 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1007 (signal 'new-error '(x y))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1008 @error{} A new error: x, y
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1009 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1010 @end example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1011
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1012 This error can be handled through any of the three condition names.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1013 This example handles @code{new-error} and any other errors in the class
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1014 @code{my-own-errors}:
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1015
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1016 @example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1017 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1018 (condition-case foo
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1019 (bar nil t)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1020 (my-own-errors nil))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1021 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1022 @end example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1023
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1024 The significant way that errors are classified is by their condition
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1025 names---the names used to match errors with handlers. An error symbol
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1026 serves only as a convenient way to specify the intended error message
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1027 and list of condition names. It would be cumbersome to give
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1028 @code{signal} a list of condition names rather than one error symbol.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1029
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1030 By contrast, using only error symbols without condition names would
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1031 seriously decrease the power of @code{condition-case}. Condition names
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1032 make it possible to categorize errors at various levels of generality
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1033 when you write an error handler. Using error symbols alone would
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1034 eliminate all but the narrowest level of classification.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1035
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1036 @xref{Standard Errors}, for a list of all the standard error symbols
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1037 and their conditions.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1038
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1039 @node Cleanups
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1040 @subsection Cleaning Up from Nonlocal Exits
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1041
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1042 The @code{unwind-protect} construct is essential whenever you
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1043 temporarily put a data structure in an inconsistent state; it permits
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1044 you to ensure the data are consistent in the event of an error or throw.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1045
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1046 @defspec unwind-protect body cleanup-forms@dots{}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1047 @cindex cleanup forms
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1048 @cindex protected forms
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1049 @cindex error cleanup
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1050 @cindex unwinding
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1051 @code{unwind-protect} executes the @var{body} with a guarantee that the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1052 @var{cleanup-forms} will be evaluated if control leaves @var{body}, no
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1053 matter how that happens. The @var{body} may complete normally, or
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1054 execute a @code{throw} out of the @code{unwind-protect}, or cause an
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1055 error; in all cases, the @var{cleanup-forms} will be evaluated.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1056
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1057 If the @var{body} forms finish normally, @code{unwind-protect} returns
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1058 the value of the last @var{body} form, after it evaluates the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1059 @var{cleanup-forms}. If the @var{body} forms do not finish,
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1060 @code{unwind-protect} does not return any value in the normal sense.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1061
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1062 Only the @var{body} is actually protected by the @code{unwind-protect}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1063 If any of the @var{cleanup-forms} themselves exits nonlocally (e.g., via
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1064 a @code{throw} or an error), @code{unwind-protect} is @emph{not}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1065 guaranteed to evaluate the rest of them. If the failure of one of the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1066 @var{cleanup-forms} has the potential to cause trouble, then protect it
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1067 with another @code{unwind-protect} around that form.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1068
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1069 The number of currently active @code{unwind-protect} forms counts,
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1070 together with the number of local variable bindings, against the limit
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1071 @code{max-specpdl-size} (@pxref{Local Variables}).
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1072 @end defspec
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1073
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1074 For example, here we make an invisible buffer for temporary use, and
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1075 make sure to kill it before finishing:
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1076
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1077 @smallexample
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1078 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1079 (save-excursion
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1080 (let ((buffer (get-buffer-create " *temp*")))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1081 (set-buffer buffer)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1082 (unwind-protect
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1083 @var{body}
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1084 (kill-buffer buffer))))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1085 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1086 @end smallexample
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1087
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1088 @noindent
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1089 You might think that we could just as well write @code{(kill-buffer
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1090 (current-buffer))} and dispense with the variable @code{buffer}.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1091 However, the way shown above is safer, if @var{body} happens to get an
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1092 error after switching to a different buffer! (Alternatively, you could
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1093 write another @code{save-excursion} around the body, to ensure that the
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1094 temporary buffer becomes current in time to kill it.)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1095
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1096 @findex ftp-login
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1097 Here is an actual example taken from the file @file{ftp.el}. It creates
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1098 a process (@pxref{Processes}) to try to establish a connection to a remote
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1099 machine. As the function @code{ftp-login} is highly susceptible to
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1100 numerous problems which the writer of the function cannot anticipate, it is
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1101 protected with a form that guarantees deletion of the process in the event
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1102 of failure. Otherwise, Emacs might fill up with useless subprocesses.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1103
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1104 @smallexample
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1105 @group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1106 (let ((win nil))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1107 (unwind-protect
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1108 (progn
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1109 (setq process (ftp-setup-buffer host file))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1110 (if (setq win (ftp-login process host user password))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1111 (message "Logged in")
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1112 (error "Ftp login failed")))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1113 (or win (and process (delete-process process)))))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1114 @end group
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1115 @end smallexample
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1116
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1117 This example actually has a small bug: if the user types @kbd{C-g} to
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1118 quit, and the quit happens immediately after the function
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1119 @code{ftp-setup-buffer} returns but before the variable @code{process} is
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1120 set, the process will not be killed. There is no easy way to fix this bug,
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1121 but at least it is very unlikely.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1122
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1123 Here is another example which uses @code{unwind-protect} to make sure
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1124 to kill a temporary buffer. In this example, the value returned by
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1125 @code{unwind-protect} is used.
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1126
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1127 @example
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1128 (defun shell-command-string (cmd)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1129 "Return the output of the shell command CMD, as a string."
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1130 (save-excursion
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1131 (set-buffer (generate-new-buffer " OS*cmd"))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1132 (shell-command cmd t)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1133 (unwind-protect
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1134 (buffer-string)
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1135 (kill-buffer (current-buffer)))))
974a37e5c414 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1136 @end example