]> code.delx.au - gnu-emacs/blobdiff - lispref/control.texi
*** empty log message ***
[gnu-emacs] / lispref / control.texi
index 7fa06693e4dce69f8426bb28b36d23342d1ad106..adea5277061e7c3d3f8261ba234dabe9ab07dff2 100644 (file)
@@ -1,6 +1,7 @@
 @c -*-texinfo-*-
 @c This is part of the GNU Emacs Lisp Reference Manual.
-@c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc. 
+@c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998, 1999
+@c  Free Software Foundation, Inc. 
 @c See the file elisp.texi for copying conditions.
 @setfilename ../info/control
 @node Control Structures, Variables, Evaluation, Top
 @cindex control structures
 
   A Lisp program consists of expressions or @dfn{forms} (@pxref{Forms}).
-We control the order of execution of the forms by enclosing them in
+We control the order of execution of these forms by enclosing them in
 @dfn{control structures}.  Control structures are special forms which
-control when, whether, or how many times to execute the forms they contain.
+control when, whether, or how many times to execute the forms they
+contain.
 
-  The simplest control structure is sequential execution: first form
+  The simplest order of execution is sequential execution: first form
 @var{a}, then form @var{b}, and so on.  This is what happens when you
 write several forms in succession in the body of a function, or at top
-level in a file of Lisp code---the forms are executed in the order they
-are written.  We call this @dfn{textual order}.  For example, if a
-function body consists of two forms @var{a} and @var{b}, evaluation of
-the function evaluates first @var{a} and then @var{b}, and the
-function's value is the value of @var{b}.
+level in a file of Lisp code---the forms are executed in the order
+written.  We call this @dfn{textual order}.  For example, if a function
+body consists of two forms @var{a} and @var{b}, evaluation of the
+function evaluates first @var{a} and then @var{b}.  The result of
+evaluating @var{b} becomes the value of the function.
+
+  Explicit control structures make possible an order of execution other
+than sequential.
 
   Emacs Lisp provides several kinds of control structure, including
-other varieties of sequencing, function calls, conditionals, iteration,
-and (controlled) jumps.  The built-in control structures are special
-forms since their subforms are not necessarily evaluated.  You can use
-macros to define your own control structure constructs (@pxref{Macros}).
+other varieties of sequencing, conditionals, iteration, and (controlled)
+jumps---all discussed below.  The built-in control structures are
+special forms since their subforms are not necessarily evaluated or not
+evaluated sequentially.  You can use macros to define your own control
+structure constructs (@pxref{Macros}).
 
 @menu
 * Sequencing::             Evaluation in textual order.
-* Conditionals::           @code{if}, @code{cond}.
+* Conditionals::           @code{if}, @code{cond}, @code{when}, @code{unless}.
 * Combining Conditions::   @code{and}, @code{or}, @code{not}.
 * Iteration::              @code{while} loops.
 * Nonlocal Exits::         Jumping out of a sequence.
@@ -39,10 +45,11 @@ macros to define your own control structure constructs (@pxref{Macros}).
 @node Sequencing
 @section Sequencing
 
-  Evaluating forms in the order they are written is the most common
-control structure.  Sometimes this happens automatically, such as in a
-function body.  Elsewhere you must use a control structure construct to
-do this: @code{progn}, the simplest control construct of Lisp.
+  Evaluating forms in the order they appear is the most common way
+control passes from one form to another.  In some contexts, such as in a
+function body, this happens automatically.  Elsewhere you must use a
+control structure construct to do this: @code{progn}, the simplest
+control construct of Lisp.
 
   A @code{progn} special form looks like this:
 
@@ -53,10 +60,10 @@ do this: @code{progn}, the simplest control construct of Lisp.
 @end example
 
 @noindent
-and it says to execute the forms @var{a}, @var{b}, @var{c} and so on, in
-that order.  These forms are called the body of the @code{progn} form.
+and it says to execute the forms @var{a}, @var{b}, @var{c}, and so on, in
+that order.  These forms are called the @dfn{body} of the @code{progn} form.
 The value of the last form in the body becomes the value of the entire
-@code{progn}.
+@code{progn}.  @code{(progn)} returns @code{nil}.
 
 @cindex implicit @code{progn}
   In the early days of Lisp, @code{progn} was the only way to execute
@@ -66,9 +73,9 @@ body of a function, where (at that time) only one form was allowed.  So
 the body of a function was made into an ``implicit @code{progn}'':
 several forms are allowed just as in the body of an actual @code{progn}.
 Many other control structures likewise contain an implicit @code{progn}.
-As a result, @code{progn} is not used as often as it used to be.  It is
-needed now most often inside of an @code{unwind-protect}, @code{and},
-@code{or}, or the @var{else}-part of an @code{if}.
+As a result, @code{progn} is not used as much as it was many years ago.
+It is needed now most often inside an @code{unwind-protect}, @code{and},
+@code{or}, or in the @var{then}-part of an @code{if}.
 
 @defspec progn forms@dots{}
 This special form evaluates all of the @var{forms}, in textual
@@ -137,8 +144,9 @@ following @var{forms}, in textual order, returning the result of
 @cindex conditional evaluation
 
   Conditional control structures choose among alternatives.  Emacs Lisp
-has two conditional forms: @code{if}, which is much the same as in other
-languages, and @code{cond}, which is a generalized case statement.
+has four conditional forms: @code{if}, which is much the same as in
+other languages; @code{when} and @code{unless}, which are variants of
+@code{if}; and @code{cond}, which is a generalized case statement.
 
 @defspec if condition then-form else-forms@dots{}
 @code{if} chooses between the @var{then-form} and the @var{else-forms}
@@ -151,7 +159,7 @@ an example of an implicit @code{progn}.  @xref{Sequencing}.)
 If @var{condition} has the value @code{nil}, and no @var{else-forms} are
 given, @code{if} returns @code{nil}.
 
-@code{if} is a special form because the branch which is not selected is
+@code{if} is a special form because the branch that is not selected is
 never evaluated---it is ignored.  Thus, in the example below,
 @code{true} is not printed because @code{print} is never called.
 
@@ -165,6 +173,38 @@ never evaluated---it is ignored.  Thus, in the example below,
 @end example
 @end defspec
 
+@defmac when condition then-forms@dots{}
+This is a variant of @code{if} where there are no @var{else-forms},
+and possibly several @var{then-forms}.  In particular,
+
+@example
+(when @var{condition} @var{a} @var{b} @var{c})
+@end example
+
+@noindent
+is entirely equivalent to
+
+@example
+(if @var{condition} (progn @var{a} @var{b} @var{c}) nil)
+@end example
+@end defmac
+
+@defmac unless condition forms@dots{}
+This is a variant of @code{if} where there is no @var{then-form}:
+
+@example
+(unless @var{condition} @var{a} @var{b} @var{c})
+@end example
+
+@noindent
+is entirely equivalent to
+
+@example
+(if @var{condition} nil
+   @var{a} @var{b} @var{c})
+@end example
+@end defmac
+
 @defspec cond clause@dots{}
 @code{cond} chooses among an arbitrary number of alternatives.  Each
 @var{clause} in the @code{cond} must be a list.  The @sc{car} of this
@@ -224,20 +264,21 @@ For example,
 
 @example
 @group
-(cond ((eq a 1) 'foo)
+(setq a 5)
+(cond ((eq a 'hack) 'foo)
       (t "default"))
 @result{} "default"
 @end group
 @end example
 
 @noindent
-This expression is a @code{cond} which returns @code{foo} if the value
-of @code{a} is 1, and returns the string @code{"default"} otherwise.
+This @code{cond} expression returns @code{foo} if the value of @code{a}
+is @code{hack}, and returns the string @code{"default"} otherwise.
 @end defspec
 
-Both @code{cond} and @code{if} can usually be written in terms of the
-other.  Therefore, the choice between them is a matter of style.  For
-example:
+Any conditional construct can be expressed with @code{cond} or with
+@code{if}.  Therefore, the choice between them is a matter of style.
+For example:
 
 @example
 @group
@@ -269,11 +310,14 @@ order written.
 
 If any of the @var{conditions} evaluates to @code{nil}, then the result
 of the @code{and} must be @code{nil} regardless of the remaining
-@var{conditions}; so @code{and} returns right away, ignoring the
-remaining @var{conditions}.
+@var{conditions}; so @code{and} returns @code{nil} right away, ignoring
+the remaining @var{conditions}.
 
 If all the @var{conditions} turn out non-@code{nil}, then the value of
-the last of them becomes the value of the @code{and} form.
+the last of them becomes the value of the @code{and} form.  Just
+@code{(and)}, with no @var{conditions}, returns @code{t}, appropriate
+because all the @var{conditions} turned out non-@code{nil}.  (Think
+about it; which one did not?)
 
 Here is an example.  The first condition returns the integer 1, which is
 not @code{nil}.  Similarly, the second condition returns the integer 2,
@@ -327,10 +371,13 @@ right away, ignoring the remaining @var{conditions}.  The value it
 returns is the non-@code{nil} value of the condition just evaluated.
 
 If all the @var{conditions} turn out @code{nil}, then the @code{or}
-expression returns @code{nil}.
+expression returns @code{nil}.  Just @code{(or)}, with no
+@var{conditions}, returns @code{nil}, appropriate because all the
+@var{conditions} turned out @code{nil}.  (Think about it; which one
+did not?)
 
-For example, this expression tests whether @code{x} is either 0 or
-@code{nil}:
+For example, this expression tests whether @code{x} is either 
+@code{nil} or the integer zero:
 
 @example
 (or (eq x nil) (eq x 0))
@@ -405,9 +452,10 @@ The value of a @code{while} form is always @code{nil}.
 @end group
 @end example
 
-If you would like to execute something on each iteration before the
-end-test, put it together with the end-test in a @code{progn} as the
-first argument of @code{while}, as shown here:
+To write a ``repeat...until'' loop, which will execute something on each
+iteration and then do the end-test, put the body followed by the
+end-test in a @code{progn} as the first argument of @code{while}, as
+shown here:
 
 @example
 @group
@@ -418,10 +466,44 @@ first argument of @code{while}, as shown here:
 @end example
 
 @noindent
-This moves forward one line and continues moving by lines until an empty
-line is reached.
+This moves forward one line and continues moving by lines until it
+reaches an empty line.  It is peculiar in that the @code{while} has no
+body, just the end test (which also does the real work of moving point).
 @end defspec
 
+  The @code{dolist} and @code{dotimes} macros provide convenient ways to
+write two common kinds of loops.
+
+@defmac dolist (var list [result]) body@dots{}
+@tindex dolist
+This construct executes @var{body} once for each element of @var{list},
+using the variable @var{var} to hold the current element.  Then it
+returns the value of evaluating @var{result}, or @code{nil} if
+@var{result} is omitted.  For example, here is how you could use
+@code{dolist} to define the @code{reverse} function:
+
+@example
+(defun reverse (list)
+  (let (value)
+    (dolist (elt list value)
+      (setq value (cons elt value)))))
+@end example
+@end defmac
+
+@defmac dotimes (var count [result]) body@dots{}
+@tindex dotimes
+This construct executes @var{body} once for each integer from 0
+(inclusive) to @var{count} (exclusive), using the variable @var{var} to
+hold the integer for the current iteration.  Then it returns the value
+of evaluating @var{result}, or @code{nil} if @var{result} is omitted.
+Here is an example of using @code{dotimes} do something 100 times:
+
+@example
+(dotimes (i 100)
+  (insert "I will not obey absurd orders\n"))
+@end example
+@end defmac
+
 @node Nonlocal Exits
 @section Nonlocal Exits
 @cindex nonlocal exits
@@ -451,26 +533,32 @@ that @code{catch}.  For example:
 
 @example
 @group
-(catch 'foo
-  (progn
-    @dots{}
-      (throw 'foo t)
-    @dots{}))
+(defun foo-outer ()
+  (catch 'foo
+    (foo-inner)))
+
+(defun foo-inner ()
+  @dots{}
+  (if x
+      (throw 'foo t))
+  @dots{})
 @end group
 @end example
 
 @noindent
-The @code{throw} transfers control straight back to the corresponding
-@code{catch}, which returns immediately.  The code following the
-@code{throw} is not executed.  The second argument of @code{throw} is used
-as the return value of the @code{catch}.
-
-  The @code{throw} and the @code{catch} are matched through the first
-argument: @code{throw} searches for a @code{catch} whose first argument
-is @code{eq} to the one specified.  Thus, in the above example, the
-@code{throw} specifies @code{foo}, and the @code{catch} specifies the
-same symbol, so that @code{catch} is applicable.  If there is more than
-one applicable @code{catch}, the innermost one takes precedence.
+The @code{throw} form, if executed, transfers control straight back to
+the corresponding @code{catch}, which returns immediately.  The code
+following the @code{throw} is not executed.  The second argument of
+@code{throw} is used as the return value of the @code{catch}.
+
+  The function @code{throw} finds the matching @code{catch} based on the
+first argument: it searches for a @code{catch} whose first argument is
+@code{eq} to the one specified in the @code{throw}.  If there is more
+than one applicable @code{catch}, the innermost one takes precedence.
+Thus, in the above example, the @code{throw} specifies @code{foo}, and
+the @code{catch} in @code{foo-outer} specifies the same symbol, so that
+@code{catch} is the applicable one (assuming there is no other matching
+@code{catch} in between).
 
   Executing @code{throw} exits all Lisp constructs up to the matching
 @code{catch}, including function calls.  When binding constructs such as
@@ -481,7 +569,8 @@ and position saved by @code{save-excursion} (@pxref{Excursions}), and
 the narrowing status saved by @code{save-restriction} and the window
 selection saved by @code{save-window-excursion} (@pxref{Window
 Configurations}).  It also runs any cleanups established with the
-@code{unwind-protect} special form when it exits that form.
+@code{unwind-protect} special form when it exits that form
+(@pxref{Cleanups}).
 
   The @code{throw} need not appear lexically within the @code{catch}
 that it jumps to.  It can equally well be called from another function
@@ -489,11 +578,11 @@ called within the @code{catch}.  As long as the @code{throw} takes place
 chronologically after entry to the @code{catch}, and chronologically
 before exit from it, it has access to that @code{catch}.  This is why
 @code{throw} can be used in commands such as @code{exit-recursive-edit}
-which throw back to the editor command loop (@pxref{Recursive Editing}).
+that throw back to the editor command loop (@pxref{Recursive Editing}).
 
 @cindex CL note---only @code{throw} in Emacs
 @quotation
-@b{Common Lisp note:} most other versions of Lisp, including Common Lisp,
+@b{Common Lisp note:} Most other versions of Lisp, including Common Lisp,
 have several ways of transferring control nonsequentially: @code{return},
 @code{return-from}, and @code{go}, for example.  Emacs Lisp has only
 @code{throw}.
@@ -501,19 +590,20 @@ have several ways of transferring control nonsequentially: @code{return},
 
 @defspec catch tag body@dots{}
 @cindex tag on run time stack
-@code{catch} establishes a return point for the @code{throw} function.  The
-return point is distinguished from other such return points by @var{tag},
-which may be any Lisp object.  The argument @var{tag} is evaluated normally
-before the return point is established.
+@code{catch} establishes a return point for the @code{throw} function.
+The return point is distinguished from other such return points by
+@var{tag}, which may be any Lisp object except @code{nil}.  The argument
+@var{tag} is evaluated normally before the return point is established.
 
 With the return point in effect, @code{catch} evaluates the forms of the
-@var{body} in textual order.  If the forms execute normallywithout
-error or nonlocal exit, the value of the last body form is returned from
+@var{body} in textual order.  If the forms execute normally (without
+error or nonlocal exit) the value of the last body form is returned from
 the @code{catch}.
 
-If a @code{throw} is done within @var{body} specifying the same value
-@var{tag}, the @code{catch} exits immediately; the value it returns is
-whatever was specified as the second argument of @code{throw}.
+If a @code{throw} is executed during the execution of @var{body},
+specifying the same value @var{tag}, the @code{catch} form exits
+immediately; the value it returns is whatever was specified as the
+second argument of @code{throw}.
 @end defspec
 
 @defun throw tag value
@@ -591,13 +681,6 @@ printed.  Finally the second body form in the outer @code{catch}, which is
   Now let's change the argument given to @code{catch2}:
 
 @example
-@group
-(defun catch2 (tag)
-  (catch tag
-    (throw 'hack 'yes)))
-@result{} catch2
-@end group
-
 @group
 (catch 'hack
   (print (catch2 'quux))
@@ -607,11 +690,11 @@ printed.  Finally the second body form in the outer @code{catch}, which is
 @end example
 
 @noindent
-We still have two return points, but this time only the outer one has the
-tag @code{hack}; the inner one has the tag @code{quux} instead.  Therefore,
-the @code{throw} returns the value @code{yes} from the outer return point.
-The function @code{print} is never called, and the body-form @code{'no} is
-never evaluated.
+We still have two return points, but this time only the outer one has
+the tag @code{hack}; the inner one has the tag @code{quux} instead.
+Therefore, @code{throw} makes the outer @code{catch} return the value
+@code{yes}.  The function @code{print} is never called, and the
+body-form @code{'no} is never evaluated.
 
 @node Errors
 @subsection Errors
@@ -627,13 +710,13 @@ the end of the buffer.
 
   In complicated programs, simple termination may not be what you want.
 For example, the program may have made temporary changes in data
-structures, or created temporary buffers which should be deleted before
+structures, or created temporary buffers that should be deleted before
 the program is finished.  In such cases, you would use
 @code{unwind-protect} to establish @dfn{cleanup expressions} to be
-evaluated in case of error.  Occasionally, you may wish the program to
-continue execution despite an error in a subroutine.  In these cases,
-you would use @code{condition-case} to establish @dfn{error handlers} to
-recover control in case of error.
+evaluated in case of error.  (@xref{Cleanups}.)  Occasionally, you may
+wish the program to continue execution despite an error in a subroutine.
+In these cases, you would use @code{condition-case} to establish
+@dfn{error handlers} to recover control in case of error.
 
   Resist the temptation to use error handling to transfer control from
 one part of the program to another; use @code{catch} and @code{throw}
@@ -643,23 +726,35 @@ instead.  @xref{Catch and Throw}.
 * Signaling Errors::      How to report an error.
 * Processing of Errors::  What Emacs does when you report an error.
 * Handling Errors::       How you can trap errors and continue execution.
-* Error Names::           How errors are classified for trapping them.
+* Error Symbols::         How errors are classified for trapping them.
 @end menu
 
 @node Signaling Errors
 @subsubsection How to Signal an Error
 @cindex signaling errors
 
+   @dfn{Signalling} an error means beginning error processing.  Error
+processing normally aborts all or part of the running program and
+returns to a point that is set up to handle the error
+(@pxref{Processing of Errors}).  Here we describe how to signal an
+error.
+
   Most errors are signaled ``automatically'' within Lisp primitives
 which you call for other purposes, such as if you try to take the
 @sc{car} of an integer or move forward a character at the end of the
-buffer; you can also signal errors explicitly with the functions
+buffer.  You can also signal errors explicitly with the functions
 @code{error} and @code{signal}.
 
   Quitting, which happens when the user types @kbd{C-g}, is not 
 considered an error, but it is handled almost like an error.
 @xref{Quitting}.
 
+  Every error specifies an error message, one way or another.  The
+message should state what is wrong (``File does not exist''), not how
+things ought to be (``File must exist'').  The convention in Emacs
+Lisp is that error messages should start with a capital letter, but
+should not end with any sort of punctuation.
+
 @defun error format-string &rest args
 This function signals an error with an error message constructed by
 applying @code{format} (@pxref{String Conversion}) to
@@ -669,15 +764,13 @@ These examples show typical uses of @code{error}:
 
 @example
 @group
-(error "You have committed an error.  
-        Try something else.")
-     @error{} You have committed an error.  
-        Try something else.
+(error "That is an error -- try something else")
+     @error{} That is an error -- try something else
 @end group
 
 @group
-(error "You have committed %d errors." 10)
-     @error{} You have committed 10 errors.  
+(error "You have committed %d errors" 10)
+     @error{} You have committed 10 errors
 @end group
 @end example
 
@@ -685,10 +778,10 @@ These examples show typical uses of @code{error}:
 error symbol @code{error}, and a list containing the string returned by
 @code{format}.
 
-If you want to use your own string as an error message verbatim, don't
-just write @code{(error @var{string})}.  If @var{string} contains
-@samp{%}, it will be interpreted as a format specifier, with undesirable
-results.  Instead, use @code{(error "%s" @var{string})}.
+@strong{Warning:} If you want to use your own string as an error message
+verbatim, don't just write @code{(error @var{string})}.  If @var{string}
+contains @samp{%}, it will be interpreted as a format specifier, with
+undesirable results.  Instead, use @code{(error "%s" @var{string})}.
 @end defun
 
 @defun signal error-symbol data
@@ -703,12 +796,12 @@ errors.
 
 The number and significance of the objects in @var{data} depends on
 @var{error-symbol}.  For example, with a @code{wrong-type-arg} error,
-there are two objects in the list: a predicate which describes the type
-that was expected, and the object which failed to fit that type.
-@xref{Error Names}, for a description of error symbols.
+there should be two objects in the list: a predicate that describes the type
+that was expected, and the object that failed to fit that type.
+@xref{Error Symbols}, for a description of error symbols.
 
 Both @var{error-symbol} and @var{data} are available to any error
-handlers which handle the error: @code{condition-case} binds a local
+handlers that handle the error: @code{condition-case} binds a local
 variable to a list of the form @code{(@var{error-symbol} .@:
 @var{data})} (@pxref{Handling Errors}).  If the error is not handled,
 these two values are used in printing the error message.
@@ -723,8 +816,8 @@ it could sometimes return).
 @end group
 
 @group
-(signal 'no-such-error '("My unknown error condition."))
-     @error{} peculiar error: "My unknown error condition."
+(signal 'no-such-error '("My unknown error condition"))
+     @error{} peculiar error: "My unknown error condition"
 @end group
 @end smallexample
 @end defun
@@ -743,7 +836,7 @@ When an error is signaled, @code{signal} searches for an active
 expressions designated to be executed if an error happens in part of the
 Lisp program.  If the error has an applicable handler, the handler is
 executed, and control resumes following the handler.  The handler
-executes in the environment of the @code{condition-case} which
+executes in the environment of the @code{condition-case} that
 established it; all functions called within that @code{condition-case}
 have already been exited, and the handler cannot return to them.
 
@@ -788,8 +881,8 @@ returning @code{nil} if an error occurs.
 call to @code{delete-file}.)  The error handlers go into effect when
 this form begins execution and are deactivated when this form returns.
 They remain in effect for all the intervening time.  In particular, they
-are in effect during the execution of subroutines called by this form,
-and their subroutines, and so on.  This is a good thing, since, strictly
+are in effect during the execution of functions called by this form, in
+their subroutines, and so on.  This is a good thing, since, strictly
 speaking, errors can be signaled only by Lisp primitives (including
 @code{signal} and @code{error}) called by the protected form, not by the
 protected form itself.
@@ -805,7 +898,14 @@ above, there is one handler, and it specifies one condition name,
   The search for an applicable handler checks all the established handlers
 starting with the most recently established one.  Thus, if two nested
 @code{condition-case} forms offer to handle the same error, the inner of
-the two will actually handle it.
+the two gets to handle it.
+
+  If an error is handled by some @code{condition-case} form, this
+ordinarily prevents the debugger from being run, even if
+@code{debug-on-error} says this error should invoke the debugger.
+@xref{Error Debugging}.  If you want to be able to debug errors that are
+caught by a @code{condition-case}, set the variable
+@code{debug-on-signal} to a non-@code{nil} value.
 
   When an error is handled, control returns to the handler.  Before this
 happens, Emacs unbinds all variable bindings made by binding constructs
@@ -813,24 +913,25 @@ that are being exited and executes the cleanups of all
 @code{unwind-protect} forms that are exited.  Once control arrives at
 the handler, the body of the handler is executed.
 
-  After execution of the handler body, execution continues by returning
-from the @code{condition-case} form.  Because the protected form is
-exited completely before execution of the handler, the handler cannot
-resume execution at the point of the error, nor can it examine variable
+  After execution of the handler body, execution returns from the
+@code{condition-case} form.  Because the protected form is exited
+completely before execution of the handler, the handler cannot resume
+execution at the point of the error, nor can it examine variable
 bindings that were made within the protected form.  All it can do is
 clean up and proceed.
 
-  @code{condition-case} is often used to trap errors that are
-predictable, such as failure to open a file in a call to
+  The @code{condition-case} construct is often used to trap errors that
+are predictable, such as failure to open a file in a call to
 @code{insert-file-contents}.  It is also used to trap errors that are
 totally unpredictable, such as when the program evaluates an expression
 read from the user.
 
   Error signaling and handling have some resemblance to @code{throw} and
-@code{catch}, but they are entirely separate facilities.  An error
-cannot be caught by a @code{catch}, and a @code{throw} cannot be handled
-by an error handler (though using @code{throw} when there is no suitable
-@code{catch} signals an error which can be handled).
+@code{catch} (@pxref{Catch and Throw}), but they are entirely separate
+facilities.  An error cannot be caught by a @code{catch}, and a
+@code{throw} cannot be handled by an error handler (though using
+@code{throw} when there is no suitable @code{catch} signals an error
+that can be handled).
 
 @defspec condition-case var protected-form handlers@dots{}
 This special form establishes the error handlers @var{handlers} around
@@ -858,10 +959,10 @@ Here are examples of handlers:
 @end group
 @end smallexample
 
-Each error that occurs has an @dfn{error symbol} which describes what
+Each error that occurs has an @dfn{error symbol} that describes what
 kind of error it is.  The @code{error-conditions} property of this
-symbol is a list of condition names (@pxref{Error Names}).  Emacs
-searches all the active @code{condition-case} forms for a handler which
+symbol is a list of condition names (@pxref{Error Symbols}).  Emacs
+searches all the active @code{condition-case} forms for a handler that
 specifies one or more of these condition names; the innermost matching
 @code{condition-case} handles the error.  Within this
 @code{condition-case}, the first applicable handler handles the error.
@@ -870,23 +971,31 @@ After executing the body of the handler, the @code{condition-case}
 returns normally, using the value of the last form in the handler body
 as the overall value.
 
+@cindex error description
 The argument @var{var} is a variable.  @code{condition-case} does not
 bind this variable when executing the @var{protected-form}, only when it
-handles an error.  At that time, it binds @var{var} locally to a list of
-the form @code{(@var{error-symbol} . @var{data})}, giving the
-particulars of the error.  The handler can refer to this list to decide
-what to do.  For example, if the error is for failure opening a file,
-the file name is the second element of @var{data}---the third element of
-@var{var}.
+handles an error.  At that time, it binds @var{var} locally to an
+@dfn{error description}, which is a list giving the particulars of the
+error.  The error description has the form @code{(@var{error-symbol}
+. @var{data})}.  The handler can refer to this list to decide what to
+do.  For example, if the error is for failure opening a file, the file
+name is the second element of @var{data}---the third element of the
+error description.
 
 If @var{var} is @code{nil}, that means no variable is bound.  Then the
 error symbol and associated data are not available to the handler.
 @end defspec
 
+@defun error-message-string error-description
+This function returns the error message string for a given error
+descriptor.  It is useful if you want to handle an error by printing the
+usual error message for that error.
+@end defun
+
 @cindex @code{arith-error} example
 Here is an example of using @code{condition-case} to handle the error
-that results from dividing by zero.  The handler prints out a warning
-message and returns a very large number.
+that results from dividing by zero.  The handler displays the error
+message (but without a beep), then returns a very large number.
 
 @smallexample
 @group
@@ -894,9 +1003,12 @@ message and returns a very large number.
   (condition-case err                
       ;; @r{Protected form.}
       (/ dividend divisor)              
+@end group
+@group
     ;; @r{The handler.}
     (arith-error                        ; @r{Condition.}
-     (princ (format "Arithmetic error: %s" err))
+     ;; @r{Display the usual message for this error.}
+     (message "%s" (error-message-string err))
      1000000)))
 @result{} safe-divide
 @end group
@@ -914,7 +1026,7 @@ The handler specifies condition name @code{arith-error} so that it will handle o
 @smallexample
 @group
 (safe-divide nil 3)
-     @error{} Wrong type argument: integer-or-marker-p, nil
+     @error{} Wrong type argument: number-or-marker-p, nil
 @end group
 @end smallexample
 
@@ -932,16 +1044,16 @@ including those signaled with @code{error}:
     (if (eq baz 35)
         t
       ;; @r{This is a call to the function @code{error}.}
-      (error "Rats!  The variable %s was %s, not 35." 'baz baz))
+      (error "Rats!  The variable %s was %s, not 35" 'baz baz))
   ;; @r{This is the handler; it is not a form.}
   (error (princ (format "The error was: %s" err)) 
          2))
-@print{} The error was: (error "Rats!  The variable baz was 34, not 35.")
+@print{} The error was: (error "Rats!  The variable baz was 34, not 35")
 @result{} 2
 @end group
 @end smallexample
 
-@node Error Names
+@node Error Symbols
 @subsubsection Error Symbols and Condition Names
 @cindex error symbol
 @cindex error name
@@ -966,7 +1078,7 @@ classifications.
 
   In order for a symbol to be an error symbol, it must have an
 @code{error-conditions} property which gives a list of condition names.
-This list defines the conditions which this kind of error belongs to.
+This list defines the conditions that this kind of error belongs to.
 (The error symbol itself, and the symbol @code{error}, should always be
 members of this list.)  Thus, the hierarchy of condition names is
 defined by the @code{error-conditions} properties of the error symbols.
@@ -997,10 +1109,13 @@ message @samp{peculiar error} is used.
 This error has three condition names: @code{new-error}, the narrowest
 classification; @code{my-own-errors}, which we imagine is a wider
 classification; and @code{error}, which is the widest of all.
+
+  The error string should start with a capital letter but it should
+not end with a period.  This is for consistency with the rest of Emacs.
  
   Naturally, Emacs will never signal @code{new-error} on its own; only
-an explicit call to @code{signal} (@pxref{Errors}) in your code can do
-this:
+an explicit call to @code{signal} (@pxref{Signaling Errors}) in your
+code can do this:
 
 @example
 @group
@@ -1041,7 +1156,7 @@ and their conditions.
 
   The @code{unwind-protect} construct is essential whenever you
 temporarily put a data structure in an inconsistent state; it permits
-you to ensure the data are consistent in the event of an error or throw.
+you to make the data consistent again in the event of an error or throw.
 
 @defspec unwind-protect body cleanup-forms@dots{}
 @cindex cleanup forms
@@ -1059,9 +1174,9 @@ the value of the last @var{body} form, after it evaluates the
 @var{cleanup-forms}.  If the @var{body} forms do not finish,
 @code{unwind-protect} does not return any value in the normal sense.
 
-Only the @var{body} is actually protected by the @code{unwind-protect}.
-If any of the @var{cleanup-forms} themselves exits nonlocally (e.g., via
-@code{throw} or an error), @code{unwind-protect} is @emph{not}
+Only the @var{body} is protected by the @code{unwind-protect}.  If any
+of the @var{cleanup-forms} themselves exits nonlocally (via a
+@code{throw} or an error), @code{unwind-protect} is @emph{not}
 guaranteed to evaluate the rest of them.  If the failure of one of the
 @var{cleanup-forms} has the potential to cause trouble, then protect it
 with another @code{unwind-protect} around that form.
@@ -1091,15 +1206,21 @@ You might think that we could just as well write @code{(kill-buffer
 However, the way shown above is safer, if @var{body} happens to get an
 error after switching to a different buffer!  (Alternatively, you could
 write another @code{save-excursion} around the body, to ensure that the
-temporary buffer becomes current in time to kill it.)
+temporary buffer becomes current again in time to kill it.)
+
+  Emacs includes a standard macro called @code{with-temp-buffer} which
+expands into more or less the code shown above (@pxref{Current Buffer}).
+Several of the macros defined in this manual use @code{unwind-protect}
+in this way.
 
 @findex ftp-login
-  Here is an actual example taken from the file @file{ftp.el}.  It creates
-process (@pxref{Processes}) to try to establish a connection to a remote
+  Here is an actual example derived from an FTP package.  It creates a
+process (@pxref{Processes}) to try to establish a connection to a remote
 machine.  As the function @code{ftp-login} is highly susceptible to
-numerous problems which the writer of the function cannot anticipate, it is
-protected with a form that guarantees deletion of the process in the event
-of failure.  Otherwise, Emacs might fill up with useless subprocesses.
+numerous problems that the writer of the function cannot anticipate, it
+is protected with a form that guarantees deletion of the process in the
+event of failure.  Otherwise, Emacs might fill up with useless
+subprocesses.
 
 @smallexample
 @group
@@ -1114,23 +1235,8 @@ of failure.  Otherwise, Emacs might fill up with useless subprocesses.
 @end group
 @end smallexample
 
-  This example actually has a small bug: if the user types @kbd{C-g} to
+  This example has a small bug: if the user types @kbd{C-g} to
 quit, and the quit happens immediately after the function
 @code{ftp-setup-buffer} returns but before the variable @code{process} is
 set, the process will not be killed.  There is no easy way to fix this bug,
 but at least it is very unlikely.
-
-  Here is another example which uses @code{unwind-protect} to make sure
-to kill a temporary buffer.  In this example, the value returned by
-@code{unwind-protect} is used.
-
-@example
-(defun shell-command-string (cmd)
-  "Return the output of the shell command CMD, as a string."
-  (save-excursion
-    (set-buffer (generate-new-buffer " OS*cmd"))
-    (shell-command cmd t)
-    (unwind-protect
-        (buffer-string)
-      (kill-buffer (current-buffer)))))
-@end example