2007-09-06 04:25:08 +00:00
|
|
|
@c -*-texinfo-*-
|
|
|
|
@c This is part of the GNU Emacs Lisp Reference Manual.
|
2015-01-01 22:26:41 +00:00
|
|
|
@c Copyright (C) 1990-1995, 1998-1999, 2001-2015 Free Software
|
2013-01-01 09:11:05 +00:00
|
|
|
@c Foundation, Inc.
|
2007-09-06 04:25:08 +00:00
|
|
|
@c See the file elisp.texi for copying conditions.
|
2012-05-27 01:34:14 +00:00
|
|
|
@node Control Structures
|
2007-09-06 04:25:08 +00:00
|
|
|
@chapter Control Structures
|
|
|
|
@cindex special forms for control structures
|
|
|
|
@cindex control structures
|
|
|
|
|
2012-01-23 04:23:50 +00:00
|
|
|
A Lisp program consists of a set of @dfn{expressions}, or
|
|
|
|
@dfn{forms} (@pxref{Forms}). 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.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2009-02-23 17:41:38 +00:00
|
|
|
@cindex textual order
|
2007-09-06 04:25:08 +00:00
|
|
|
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
|
|
|
|
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, 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}, @code{when}, @code{unless}.
|
|
|
|
* Combining Conditions:: @code{and}, @code{or}, @code{not}.
|
|
|
|
* Iteration:: @code{while} loops.
|
2015-03-02 07:57:51 +00:00
|
|
|
* Generators:: Generic sequences and coroutines.
|
2007-09-06 04:25:08 +00:00
|
|
|
* Nonlocal Exits:: Jumping out of a sequence.
|
|
|
|
@end menu
|
|
|
|
|
|
|
|
@node Sequencing
|
|
|
|
@section Sequencing
|
Improve indexing on the chapter/section/subsection levels.
doc/lispref/windows.texi (Recombining Windows): Index subject of sections.
doc/lispref/variables.texi (Variables with Restricted Values)
(Generalized Variables): Index subject of sections.
doc/lispref/text.texi (Buffer Contents, Examining Properties)
(Changing Properties, Property Search, Substitution): Index
subject of sections.
doc/lispref/syntax.texi (Motion and Syntax, Parsing Expressions)
(Motion via Parsing, Position Parse, Control Parsing): Index
subject of sections.
doc/lispref/strings.texi (Predicates for Strings, Creating Strings)
(Modifying Strings, Text Comparison): Index subject of sections.
doc/lispref/searching.texi (Syntax of Regexps, Regexp Special)
(Regexp Functions, Regexp Functions): Index subject of sections.
doc/lispref/processes.texi (Subprocess Creation, Process Information): Index
subject of sections.
doc/lispref/positions.texi (Screen Lines): Index subject of sections.
doc/lispref/nonascii.texi (Scanning Charsets, Specifying Coding Systems):
Index subject of sections.
doc/lispref/minibuf.texi (Text from Minibuffer, Object from Minibuffer)
(Multiple Queries, Minibuffer Contents): Index subject of
sections.
doc/lispref/markers.texi (Predicates on Markers, Creating Markers)
(Information from Markers, Moving Markers): Index subject of
sections.
doc/lispref/macros.texi (Defining Macros, Problems with Macros): Index
subject of sections.
doc/lispref/loading.texi (Loading Non-ASCII, Where Defined): Index subject
of sections.
doc/lispref/lists.texi (List-related Predicates, List Variables, Setcar)
(Setcdr, Plist Access): Index subject of sections.
doc/lispref/keymaps.texi (Controlling Active Maps, Scanning Keymaps)
(Modifying Menus): Index subject of sections.
doc/lispref/help.texi (Accessing Documentation, Help Functions): Index
subject of sections.
doc/lispref/hash.texi (Hash Access): Index subject of sections.
doc/lispref/functions.texi (Core Advising Primitives)
(Advising Named Functions, Porting old advices): Index subject of
sections.
doc/lispref/frames.texi (Creating Frames, Initial Parameters)
(Position Parameters, Buffer Parameters, Minibuffers and Frames)
(Pop-Up Menus, Drag and Drop): Index subject of sections.
doc/lispref/files.texi (Visiting Functions, Kinds of Files)
(Unique File Names): Index subject of sections.
doc/lispref/display.texi (Refresh Screen, Echo Area Customization)
(Warning Variables, Warning Options, Delayed Warnings)
(Temporary Displays, Managing Overlays, Overlay Properties)
(Finding Overlays, Size of Displayed Text, Defining Faces)
(Attribute Functions, Displaying Faces, Face Remapping)
(Basic Faces, Font Lookup, Fontsets, Replacing Specs)
(Defining Images, Showing Images): Index subject of sections.
doc/lispref/debugging.texi (Debugging, Explicit Debug)
(Invoking the Debugger, Excess Open, Excess Close): Index subject
of sections.
doc/lispref/customize.texi (Defining New Types, Applying Customizations)
(Custom Themes): Index subject of sections.
doc/lispref/control.texi (Sequencing, Combining Conditions)
(Processing of Errors, Cleanups): Index subject of sections.
doc/lispref/compile.texi (Eval During Compile): Index subject of sections.
doc/lispref/commands.texi (Using Interactive, Distinguish Interactive)
(Command Loop Info, Classifying Events, Event Mod)
(Invoking the Input Method): Index subject of sections.
doc/lispref/buffers.texi (Buffer List, Buffer Gap): Index subject of sections.
doc/lispref/backups.texi (Making Backups, Numbered Backups, Backup Names)
(Reverting): Index subject of sections.
doc/lispref/abbrevs.texi (Abbrev Tables, Defining Abbrevs, Abbrev Files)
(Abbrev Expansion, Standard Abbrev Tables, Abbrev Properties)
(Abbrev Table Properties): Index subject of sections.
doc/lispref/os.texi (Time of Day, Time Conversion, Time Parsing)
(Time Calculations, Idle Timers): Index subject of sections.
2014-12-23 18:42:30 +00:00
|
|
|
@cindex sequencing
|
|
|
|
@cindex sequential execution
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(progn @var{a} @var{b} @var{c} @dots{})
|
|
|
|
@end group
|
|
|
|
@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 @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)} returns @code{nil}.
|
|
|
|
|
|
|
|
@cindex implicit @code{progn}
|
|
|
|
In the early days of Lisp, @code{progn} was the only way to execute
|
|
|
|
two or more forms in succession and use the value of the last of them.
|
|
|
|
But programmers found they often needed to use a @code{progn} in the
|
|
|
|
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 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
|
|
|
|
order, returning the result of the final form.
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(progn (print "The first form")
|
|
|
|
(print "The second form")
|
|
|
|
(print "The third form"))
|
|
|
|
@print{} "The first form"
|
|
|
|
@print{} "The second form"
|
|
|
|
@print{} "The third form"
|
|
|
|
@result{} "The third form"
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
@end defspec
|
|
|
|
|
More small edits for doc/lispref
* control.texi: Where possible, use example rather than smallexample.
(Sequencing, Conditionals, Signaling Errors, Handling Errors):
Tweak page breaks.
* customize.texi: Where possible, use example rather than smallexample.
(Common Keywords, Variable Definitions, Applying Customizations)
(Custom Themes): Tweak page breaks.
* eval.texi, functions.texi, loading.texi, macros.texi:
Where possible, use example rather than smallexample.
* sequences.texi (Arrays): Tweak page breaks.
* symbols.texi: Where possible, use example rather than smallexample.
(Symbol Components): Fix typo.
(Other Plists): Tweak page break.
2012-05-05 00:37:30 +00:00
|
|
|
Two other constructs likewise evaluate a series of forms but return
|
|
|
|
different values:
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@defspec prog1 form1 forms@dots{}
|
|
|
|
This special form evaluates @var{form1} and all of the @var{forms}, in
|
|
|
|
textual order, returning the result of @var{form1}.
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(prog1 (print "The first form")
|
|
|
|
(print "The second form")
|
|
|
|
(print "The third form"))
|
|
|
|
@print{} "The first form"
|
|
|
|
@print{} "The second form"
|
|
|
|
@print{} "The third form"
|
|
|
|
@result{} "The first form"
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
Here is a way to remove the first element from a list in the variable
|
|
|
|
@code{x}, then return the value of that former element:
|
|
|
|
|
|
|
|
@example
|
|
|
|
(prog1 (car x) (setq x (cdr x)))
|
|
|
|
@end example
|
|
|
|
@end defspec
|
|
|
|
|
|
|
|
@defspec prog2 form1 form2 forms@dots{}
|
|
|
|
This special form evaluates @var{form1}, @var{form2}, and all of the
|
|
|
|
following @var{forms}, in textual order, returning the result of
|
|
|
|
@var{form2}.
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(prog2 (print "The first form")
|
|
|
|
(print "The second form")
|
|
|
|
(print "The third form"))
|
|
|
|
@print{} "The first form"
|
|
|
|
@print{} "The second form"
|
|
|
|
@print{} "The third form"
|
|
|
|
@result{} "The second form"
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
@end defspec
|
|
|
|
|
|
|
|
@node Conditionals
|
|
|
|
@section Conditionals
|
|
|
|
@cindex conditional evaluation
|
|
|
|
|
|
|
|
Conditional control structures choose among alternatives. Emacs Lisp
|
|
|
|
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}
|
|
|
|
based on the value of @var{condition}. If the evaluated @var{condition} is
|
|
|
|
non-@code{nil}, @var{then-form} is evaluated and the result returned.
|
|
|
|
Otherwise, the @var{else-forms} are evaluated in textual order, and the
|
|
|
|
value of the last one is returned. (The @var{else} part of @code{if} is
|
|
|
|
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 that is not selected is
|
More small edits for doc/lispref
* control.texi: Where possible, use example rather than smallexample.
(Sequencing, Conditionals, Signaling Errors, Handling Errors):
Tweak page breaks.
* customize.texi: Where possible, use example rather than smallexample.
(Common Keywords, Variable Definitions, Applying Customizations)
(Custom Themes): Tweak page breaks.
* eval.texi, functions.texi, loading.texi, macros.texi:
Where possible, use example rather than smallexample.
* sequences.texi (Arrays): Tweak page breaks.
* symbols.texi: Where possible, use example rather than smallexample.
(Symbol Components): Fix typo.
(Other Plists): Tweak page break.
2012-05-05 00:37:30 +00:00
|
|
|
never evaluated---it is ignored. Thus, in this example,
|
|
|
|
@code{true} is not printed because @code{print} is never called:
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(if nil
|
|
|
|
(print 'true)
|
|
|
|
'very-false)
|
|
|
|
@result{} very-false
|
|
|
|
@end group
|
|
|
|
@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
|
|
|
|
list is the @var{condition}; the remaining elements, if any, the
|
|
|
|
@var{body-forms}. Thus, a clause looks like this:
|
|
|
|
|
|
|
|
@example
|
|
|
|
(@var{condition} @var{body-forms}@dots{})
|
|
|
|
@end example
|
|
|
|
|
|
|
|
@code{cond} tries the clauses in textual order, by evaluating the
|
|
|
|
@var{condition} of each clause. If the value of @var{condition} is
|
|
|
|
non-@code{nil}, the clause ``succeeds''; then @code{cond} evaluates its
|
2013-10-09 17:17:20 +00:00
|
|
|
@var{body-forms}, and returns the value of the last of @var{body-forms}.
|
|
|
|
Any remaining clauses are ignored.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2012-04-26 00:31:47 +00:00
|
|
|
If the value of @var{condition} is @code{nil}, the clause ``fails'', so
|
2013-10-09 17:17:20 +00:00
|
|
|
the @code{cond} moves on to the following clause, trying its @var{condition}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
A clause may also look like this:
|
|
|
|
|
|
|
|
@example
|
|
|
|
(@var{condition})
|
|
|
|
@end example
|
|
|
|
|
|
|
|
@noindent
|
2013-10-09 17:17:20 +00:00
|
|
|
Then, if @var{condition} is non-@code{nil} when tested, the @code{cond}
|
|
|
|
form returns the value of @var{condition}.
|
|
|
|
|
|
|
|
If every @var{condition} evaluates to @code{nil}, so that every clause
|
|
|
|
fails, @code{cond} returns @code{nil}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
The following example has four clauses, which test for the cases where
|
|
|
|
the value of @code{x} is a number, string, buffer and symbol,
|
|
|
|
respectively:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(cond ((numberp x) x)
|
|
|
|
((stringp x) x)
|
|
|
|
((bufferp x)
|
|
|
|
(setq temporary-hack x) ; @r{multiple body-forms}
|
|
|
|
(buffer-name x)) ; @r{in one clause}
|
|
|
|
((symbolp x) (symbol-value x)))
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
Often we want to execute the last clause whenever none of the previous
|
|
|
|
clauses was successful. To do this, we use @code{t} as the
|
|
|
|
@var{condition} of the last clause, like this: @code{(t
|
|
|
|
@var{body-forms})}. The form @code{t} evaluates to @code{t}, which is
|
|
|
|
never @code{nil}, so this clause never fails, provided the @code{cond}
|
More small edits for doc/lispref
* control.texi: Where possible, use example rather than smallexample.
(Sequencing, Conditionals, Signaling Errors, Handling Errors):
Tweak page breaks.
* customize.texi: Where possible, use example rather than smallexample.
(Common Keywords, Variable Definitions, Applying Customizations)
(Custom Themes): Tweak page breaks.
* eval.texi, functions.texi, loading.texi, macros.texi:
Where possible, use example rather than smallexample.
* sequences.texi (Arrays): Tweak page breaks.
* symbols.texi: Where possible, use example rather than smallexample.
(Symbol Components): Fix typo.
(Other Plists): Tweak page break.
2012-05-05 00:37:30 +00:00
|
|
|
gets to it at all. For example:
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(setq a 5)
|
|
|
|
(cond ((eq a 'hack) 'foo)
|
|
|
|
(t "default"))
|
|
|
|
@result{} "default"
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
@noindent
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
(if @var{a} @var{b} @var{c})
|
|
|
|
@equiv{}
|
|
|
|
(cond (@var{a} @var{b}) (t @var{c}))
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
2012-12-09 15:36:46 +00:00
|
|
|
@menu
|
2012-12-11 18:43:57 +00:00
|
|
|
* Pattern matching case statement::
|
2012-12-09 15:36:46 +00:00
|
|
|
@end menu
|
|
|
|
|
2012-12-11 18:43:57 +00:00
|
|
|
@node Pattern matching case statement
|
|
|
|
@subsection Pattern matching case statement
|
2012-12-09 15:36:46 +00:00
|
|
|
@cindex pcase
|
|
|
|
@cindex pattern matching
|
|
|
|
|
|
|
|
To compare a particular value against various possible cases, the macro
|
|
|
|
@code{pcase} can come handy. It takes the following form:
|
|
|
|
|
|
|
|
@example
|
|
|
|
(pcase @var{exp} @var{branch}1 @var{branch}2 @var{branch}3 @dots{})
|
|
|
|
@end example
|
|
|
|
|
|
|
|
where each @var{branch} takes the form @code{(@var{upattern}
|
|
|
|
@var{body-forms}@dots{})}.
|
|
|
|
|
|
|
|
It will first evaluate @var{exp} and then compare the value against each
|
|
|
|
@var{upattern} to see which @var{branch} to use, after which it will run the
|
|
|
|
corresponding @var{body-forms}. A common use case is to distinguish
|
|
|
|
between a few different constant values:
|
|
|
|
|
|
|
|
@example
|
|
|
|
(pcase (get-return-code x)
|
|
|
|
(`success (message "Done!"))
|
Go back to grave quoting in source-code docstrings etc.
This reverts almost all my recent changes to use curved quotes
in docstrings and/or strings used for error diagnostics.
There are a few exceptions, e.g., Bahá’í proper names.
* admin/unidata/unidata-gen.el (unidata-gen-table):
* lisp/abbrev.el (expand-region-abbrevs):
* lisp/align.el (align-region):
* lisp/allout.el (allout-mode, allout-solicit-alternate-bullet)
(outlineify-sticky):
* lisp/apropos.el (apropos-library):
* lisp/bookmark.el (bookmark-default-annotation-text):
* lisp/button.el (button-category-symbol, button-put)
(make-text-button):
* lisp/calc/calc-aent.el (math-read-if, math-read-factor):
* lisp/calc/calc-embed.el (calc-do-embedded):
* lisp/calc/calc-ext.el (calc-user-function-list):
* lisp/calc/calc-graph.el (calc-graph-show-dumb):
* lisp/calc/calc-help.el (calc-describe-key)
(calc-describe-thing, calc-full-help):
* lisp/calc/calc-lang.el (calc-c-language)
(math-parse-fortran-vector-end, math-parse-tex-sum)
(math-parse-eqn-matrix, math-parse-eqn-prime)
(calc-yacas-language, calc-maxima-language, calc-giac-language)
(math-read-giac-subscr, math-read-math-subscr)
(math-read-big-rec, math-read-big-balance):
* lisp/calc/calc-misc.el (calc-help, report-calc-bug):
* lisp/calc/calc-mode.el (calc-auto-why, calc-save-modes)
(calc-auto-recompute):
* lisp/calc/calc-prog.el (calc-fix-token-name)
(calc-read-parse-table-part, calc-user-define-invocation)
(math-do-arg-check):
* lisp/calc/calc-store.el (calc-edit-variable):
* lisp/calc/calc-units.el (math-build-units-table-buffer):
* lisp/calc/calc-vec.el (math-read-brackets):
* lisp/calc/calc-yank.el (calc-edit-mode):
* lisp/calc/calc.el (calc, calc-do, calc-user-invocation):
* lisp/calendar/appt.el (appt-display-message):
* lisp/calendar/diary-lib.el (diary-check-diary-file)
(diary-mail-entries, diary-from-outlook):
* lisp/calendar/icalendar.el (icalendar-export-region)
(icalendar--convert-float-to-ical)
(icalendar--convert-date-to-ical)
(icalendar--convert-ical-to-diary)
(icalendar--convert-recurring-to-diary)
(icalendar--add-diary-entry):
* lisp/calendar/time-date.el (format-seconds):
* lisp/calendar/timeclock.el (timeclock-mode-line-display)
(timeclock-make-hours-explicit, timeclock-log-data):
* lisp/calendar/todo-mode.el (todo-prefix, todo-delete-category)
(todo-item-mark, todo-check-format)
(todo-insert-item--next-param, todo-edit-item--next-key)
(todo-mode):
* lisp/cedet/ede/pmake.el (ede-proj-makefile-insert-dist-rules):
* lisp/cedet/mode-local.el (describe-mode-local-overload)
(mode-local-print-binding, mode-local-describe-bindings-2):
* lisp/cedet/semantic/complete.el (semantic-displayor-show-request):
* lisp/cedet/srecode/srt-mode.el (srecode-macro-help):
* lisp/cus-start.el (standard):
* lisp/cus-theme.el (describe-theme-1):
* lisp/custom.el (custom-add-dependencies, custom-check-theme)
(custom--sort-vars-1, load-theme):
* lisp/descr-text.el (describe-text-properties-1, describe-char):
* lisp/dired-x.el (dired-do-run-mail):
* lisp/dired.el (dired-log):
* lisp/emacs-lisp/advice.el (ad-read-advised-function)
(ad-read-advice-class, ad-read-advice-name, ad-enable-advice)
(ad-disable-advice, ad-remove-advice, ad-set-argument)
(ad-set-arguments, ad--defalias-fset, ad-activate)
(ad-deactivate):
* lisp/emacs-lisp/byte-opt.el (byte-compile-inline-expand)
(byte-compile-unfold-lambda, byte-optimize-form-code-walker)
(byte-optimize-while, byte-optimize-apply):
* lisp/emacs-lisp/byte-run.el (defun, defsubst):
* lisp/emacs-lisp/bytecomp.el (byte-compile-lapcode)
(byte-compile-log-file, byte-compile-format-warn)
(byte-compile-nogroup-warn, byte-compile-arglist-warn)
(byte-compile-cl-warn)
(byte-compile-warn-about-unresolved-functions)
(byte-compile-file, byte-compile--declare-var)
(byte-compile-file-form-defmumble, byte-compile-form)
(byte-compile-normal-call, byte-compile-check-variable)
(byte-compile-variable-ref, byte-compile-variable-set)
(byte-compile-subr-wrong-args, byte-compile-setq-default)
(byte-compile-negation-optimizer)
(byte-compile-condition-case--old)
(byte-compile-condition-case--new, byte-compile-save-excursion)
(byte-compile-defvar, byte-compile-autoload)
(byte-compile-lambda-form)
(byte-compile-make-variable-buffer-local, display-call-tree)
(batch-byte-compile):
* lisp/emacs-lisp/cconv.el (cconv-convert, cconv--analyze-use):
* lisp/emacs-lisp/chart.el (chart-space-usage):
* lisp/emacs-lisp/check-declare.el (check-declare-scan)
(check-declare-warn, check-declare-file)
(check-declare-directory):
* lisp/emacs-lisp/checkdoc.el (checkdoc-this-string-valid-engine)
(checkdoc-message-text-engine):
* lisp/emacs-lisp/cl-extra.el (cl-parse-integer)
(cl--describe-class):
* lisp/emacs-lisp/cl-generic.el (cl-defgeneric)
(cl--generic-describe, cl-generic-generalizers):
* lisp/emacs-lisp/cl-macs.el (cl--parse-loop-clause, cl-tagbody)
(cl-symbol-macrolet):
* lisp/emacs-lisp/cl.el (cl-unload-function, flet):
* lisp/emacs-lisp/copyright.el (copyright)
(copyright-update-directory):
* lisp/emacs-lisp/edebug.el (edebug-read-list):
* lisp/emacs-lisp/eieio-base.el (eieio-persistent-read):
* lisp/emacs-lisp/eieio-core.el (eieio--slot-override)
(eieio-oref):
* lisp/emacs-lisp/eieio-opt.el (eieio-help-constructor):
* lisp/emacs-lisp/eieio-speedbar.el:
(eieio-speedbar-child-make-tag-lines)
(eieio-speedbar-child-description):
* lisp/emacs-lisp/eieio.el (defclass, change-class):
* lisp/emacs-lisp/elint.el (elint-file, elint-get-top-forms)
(elint-init-form, elint-check-defalias-form)
(elint-check-let-form):
* lisp/emacs-lisp/ert.el (ert-get-test, ert-results-mode-menu)
(ert-results-pop-to-backtrace-for-test-at-point)
(ert-results-pop-to-messages-for-test-at-point)
(ert-results-pop-to-should-forms-for-test-at-point)
(ert-describe-test):
* lisp/emacs-lisp/find-func.el (find-function-search-for-symbol)
(find-function-library):
* lisp/emacs-lisp/generator.el (iter-yield):
* lisp/emacs-lisp/gv.el (gv-define-simple-setter):
* lisp/emacs-lisp/lisp-mnt.el (lm-verify):
* lisp/emacs-lisp/macroexp.el (macroexp--obsolete-warning):
* lisp/emacs-lisp/map-ynp.el (map-y-or-n-p):
* lisp/emacs-lisp/nadvice.el (advice--make-docstring)
(advice--make, define-advice):
* lisp/emacs-lisp/package-x.el (package-upload-file):
* lisp/emacs-lisp/package.el (package-version-join)
(package-disabled-p, package-activate-1, package-activate)
(package--download-one-archive)
(package--download-and-read-archives)
(package-compute-transaction, package-install-from-archive)
(package-install, package-install-selected-packages)
(package-delete, package-autoremove, describe-package-1)
(package-install-button-action, package-delete-button-action)
(package-menu-hide-package, package-menu--list-to-prompt)
(package-menu--perform-transaction)
(package-menu--find-and-notify-upgrades):
* lisp/emacs-lisp/pcase.el (pcase-exhaustive, pcase--u1):
* lisp/emacs-lisp/re-builder.el (reb-enter-subexp-mode):
* lisp/emacs-lisp/ring.el (ring-previous, ring-next):
* lisp/emacs-lisp/rx.el (rx-check, rx-anything)
(rx-check-any-string, rx-check-any, rx-check-not, rx-=)
(rx-repeat, rx-check-backref, rx-syntax, rx-check-category)
(rx-form):
* lisp/emacs-lisp/smie.el (smie-config-save):
* lisp/emacs-lisp/subr-x.el (internal--check-binding):
* lisp/emacs-lisp/tabulated-list.el (tabulated-list-put-tag):
* lisp/emacs-lisp/testcover.el (testcover-1value):
* lisp/emacs-lisp/timer.el (timer-event-handler):
* lisp/emulation/viper-cmd.el (viper-toggle-parse-sexp-ignore-comments)
(viper-toggle-search-style, viper-kill-buffer)
(viper-brac-function):
* lisp/emulation/viper-macs.el (viper-record-kbd-macro):
* lisp/env.el (setenv):
* lisp/erc/erc-button.el (erc-nick-popup):
* lisp/erc/erc.el (erc-cmd-LOAD, erc-handle-login, english):
* lisp/eshell/em-dirs.el (eshell/cd):
* lisp/eshell/em-glob.el (eshell-glob-regexp)
(eshell-glob-entries):
* lisp/eshell/em-pred.el (eshell-parse-modifiers):
* lisp/eshell/esh-opt.el (eshell-show-usage):
* lisp/facemenu.el (facemenu-add-new-face)
(facemenu-add-new-color):
* lisp/faces.el (read-face-name, read-face-font, describe-face)
(x-resolve-font-name):
* lisp/files-x.el (modify-file-local-variable):
* lisp/files.el (locate-user-emacs-file, find-alternate-file)
(set-auto-mode, hack-one-local-variable--obsolete)
(dir-locals-set-directory-class, write-file, basic-save-buffer)
(delete-directory, copy-directory, recover-session)
(recover-session-finish, insert-directory)
(file-modes-char-to-who, file-modes-symbolic-to-number)
(move-file-to-trash):
* lisp/filesets.el (filesets-add-buffer, filesets-remove-buffer):
* lisp/find-cmd.el (find-generic, find-to-string):
* lisp/finder.el (finder-commentary):
* lisp/font-lock.el (font-lock-fontify-buffer):
* lisp/format.el (format-write-file, format-find-file)
(format-insert-file):
* lisp/frame.el (get-device-terminal, select-frame-by-name):
* lisp/fringe.el (fringe--check-style):
* lisp/gnus/nnmairix.el (nnmairix-widget-create-query):
* lisp/help-fns.el (help-fns--key-bindings)
(help-fns--compiler-macro, help-fns--parent-mode)
(help-fns--obsolete, help-fns--interactive-only)
(describe-function-1, describe-variable):
* lisp/help.el (describe-mode)
(describe-minor-mode-from-indicator):
* lisp/image.el (image-type):
* lisp/international/ccl.el (ccl-dump):
* lisp/international/fontset.el (x-must-resolve-font-name):
* lisp/international/mule-cmds.el (prefer-coding-system)
(select-safe-coding-system-interactively)
(select-safe-coding-system, activate-input-method)
(toggle-input-method, describe-current-input-method)
(describe-language-environment):
* lisp/international/mule-conf.el (code-offset):
* lisp/international/mule-diag.el (describe-character-set)
(list-input-methods-1):
* lisp/mail/feedmail.el (feedmail-run-the-queue):
* lisp/mouse.el (minor-mode-menu-from-indicator):
* lisp/mpc.el (mpc-playlist-rename):
* lisp/msb.el (msb--choose-menu):
* lisp/net/ange-ftp.el (ange-ftp-shell-command):
* lisp/net/imap.el (imap-interactive-login):
* lisp/net/mairix.el (mairix-widget-create-query):
* lisp/net/newst-backend.el (newsticker--sentinel-work):
* lisp/net/newst-treeview.el (newsticker--treeview-load):
* lisp/net/rlogin.el (rlogin):
* lisp/obsolete/iswitchb.el (iswitchb-possible-new-buffer):
* lisp/obsolete/otodo-mode.el (todo-more-important-p):
* lisp/obsolete/pgg-gpg.el (pgg-gpg-process-region):
* lisp/obsolete/pgg-pgp.el (pgg-pgp-process-region):
* lisp/obsolete/pgg-pgp5.el (pgg-pgp5-process-region):
* lisp/org/ob-core.el (org-babel-goto-named-src-block)
(org-babel-goto-named-result):
* lisp/org/ob-fortran.el (org-babel-fortran-ensure-main-wrap):
* lisp/org/ob-ref.el (org-babel-ref-resolve):
* lisp/org/org-agenda.el (org-agenda-prepare):
* lisp/org/org-clock.el (org-clock-notify-once-if-expired)
(org-clock-resolve):
* lisp/org/org-ctags.el (org-ctags-ask-rebuild-tags-file-then-find-tag):
* lisp/org/org-feed.el (org-feed-parse-atom-entry):
* lisp/org/org-habit.el (org-habit-parse-todo):
* lisp/org/org-mouse.el (org-mouse-popup-global-menu)
(org-mouse-context-menu):
* lisp/org/org-table.el (org-table-edit-formulas):
* lisp/org/ox.el (org-export-async-start):
* lisp/proced.el (proced-log):
* lisp/progmodes/ada-mode.el (ada-get-indent-case)
(ada-check-matching-start, ada-goto-matching-start):
* lisp/progmodes/ada-prj.el (ada-prj-display-page):
* lisp/progmodes/ada-xref.el (ada-find-executable):
* lisp/progmodes/ebrowse.el (ebrowse-tags-apropos):
* lisp/progmodes/etags.el (etags-tags-apropos-additional):
* lisp/progmodes/flymake.el (flymake-parse-err-lines)
(flymake-start-syntax-check-process):
* lisp/progmodes/python.el (python-shell-get-process-or-error)
(python-define-auxiliary-skeleton):
* lisp/progmodes/sql.el (sql-comint):
* lisp/progmodes/verilog-mode.el (verilog-load-file-at-point):
* lisp/progmodes/vhdl-mode.el (vhdl-widget-directory-validate):
* lisp/recentf.el (recentf-open-files):
* lisp/replace.el (query-replace-read-from)
(occur-after-change-function, occur-1):
* lisp/scroll-bar.el (scroll-bar-columns):
* lisp/server.el (server-get-auth-key):
* lisp/simple.el (execute-extended-command)
(undo-outer-limit-truncate, list-processes--refresh)
(compose-mail, set-variable, choose-completion-string)
(define-alternatives):
* lisp/startup.el (site-run-file, tty-handle-args, command-line)
(command-line-1):
* lisp/subr.el (noreturn, define-error, add-to-list)
(read-char-choice, version-to-list):
* lisp/term/common-win.el (x-handle-xrm-switch)
(x-handle-name-switch, x-handle-args):
* lisp/term/x-win.el (x-handle-parent-id, x-handle-smid):
* lisp/textmodes/reftex-ref.el (reftex-label):
* lisp/textmodes/reftex-toc.el (reftex-toc-rename-label):
* lisp/textmodes/two-column.el (2C-split):
* lisp/tutorial.el (tutorial--describe-nonstandard-key)
(tutorial--find-changed-keys):
* lisp/type-break.el (type-break-noninteractive-query):
* lisp/wdired.el (wdired-do-renames, wdired-do-symlink-changes)
(wdired-do-perm-changes):
* lisp/whitespace.el (whitespace-report-region):
Prefer grave quoting in source-code strings used to generate help
and diagnostics.
* lisp/faces.el (face-documentation):
No need to convert quotes, since the result is a docstring.
* lisp/info.el (Info-virtual-index-find-node)
(Info-virtual-index, info-apropos):
Simplify by generating only curved quotes, since info files are
typically that ways nowadays anyway.
* lisp/international/mule-diag.el (list-input-methods):
Don’t assume text quoting style is curved.
* lisp/org/org-bibtex.el (org-bibtex-fields):
Revert my recent changes, going back to the old quoting style.
2015-09-07 15:41:44 +00:00
|
|
|
(`would-block (message "Sorry, can't do it now"))
|
2012-12-09 15:36:46 +00:00
|
|
|
(`read-only (message "The shmliblick is read-only"))
|
|
|
|
(`access-denied (message "You do not have the needed rights"))
|
|
|
|
(code (message "Unknown return code %S" code)))
|
|
|
|
@end example
|
|
|
|
|
|
|
|
In the last clause, @code{code} is a variable that gets bound to the value that
|
|
|
|
was returned by @code{(get-return-code x)}.
|
|
|
|
|
|
|
|
To give a more complex example, a simple interpreter for a little
|
2013-12-28 00:47:15 +00:00
|
|
|
expression language could look like (note that this example requires
|
2013-12-28 00:48:28 +00:00
|
|
|
lexical binding):
|
2012-12-09 15:36:46 +00:00
|
|
|
|
|
|
|
@example
|
|
|
|
(defun evaluate (exp env)
|
|
|
|
(pcase exp
|
2014-10-09 17:36:02 +00:00
|
|
|
(`(add ,x ,y) (+ (evaluate x env) (evaluate y env)))
|
|
|
|
(`(call ,fun ,arg) (funcall (evaluate fun env) (evaluate arg env)))
|
|
|
|
(`(fn ,arg ,body) (lambda (val)
|
|
|
|
(evaluate body (cons (cons arg val) env))))
|
|
|
|
((pred numberp) exp)
|
|
|
|
((pred symbolp) (cdr (assq exp env)))
|
|
|
|
(_ (error "Unknown expression %S" exp))))
|
2012-12-09 15:36:46 +00:00
|
|
|
@end example
|
|
|
|
|
|
|
|
Where @code{`(add ,x ,y)} is a pattern that checks that @code{exp} is a three
|
|
|
|
element list starting with the symbol @code{add}, then extracts the second and
|
|
|
|
third elements and binds them to the variables @code{x} and @code{y}.
|
|
|
|
@code{(pred numberp)} is a pattern that simply checks that @code{exp}
|
|
|
|
is a number, and @code{_} is the catch-all pattern that matches anything.
|
|
|
|
|
2013-12-24 15:30:59 +00:00
|
|
|
Here are some sample programs including their evaluation results:
|
|
|
|
|
|
|
|
@example
|
|
|
|
(evaluate '(add 1 2) nil) ;=> 3
|
|
|
|
(evaluate '(add x y) '((x . 1) (y . 2))) ;=> 3
|
|
|
|
(evaluate '(call (fn x (add 1 x)) 2) nil) ;=> 3
|
2013-12-25 09:12:24 +00:00
|
|
|
(evaluate '(sub 1 2) nil) ;=> error
|
2013-12-24 15:30:59 +00:00
|
|
|
@end example
|
|
|
|
|
2012-12-09 15:36:46 +00:00
|
|
|
There are two kinds of patterns involved in @code{pcase}, called
|
|
|
|
@emph{U-patterns} and @emph{Q-patterns}. The @var{upattern} mentioned above
|
|
|
|
are U-patterns and can take the following forms:
|
|
|
|
|
|
|
|
@table @code
|
|
|
|
@item `@var{qpattern}
|
|
|
|
This is one of the most common form of patterns. The intention is to mimic the
|
|
|
|
backquote macro: this pattern matches those values that could have been built
|
|
|
|
by such a backquote expression. Since we're pattern matching rather than
|
|
|
|
building a value, the unquote does not indicate where to plug an expression,
|
|
|
|
but instead it lets one specify a U-pattern that should match the value at
|
|
|
|
that location.
|
|
|
|
|
|
|
|
More specifically, a Q-pattern can take the following forms:
|
|
|
|
@table @code
|
|
|
|
@item (@var{qpattern1} . @var{qpattern2})
|
2014-12-25 00:00:44 +00:00
|
|
|
This pattern matches any cons cell whose @code{car} matches @var{qpattern1} and
|
2015-08-21 18:54:49 +00:00
|
|
|
whose @code{cdr} matches @var{qpattern2}.
|
2014-12-25 00:54:55 +00:00
|
|
|
@item [@var{qpattern1} @var{qpattern2} @dots{} @var{qpatternm}]
|
|
|
|
This pattern matches a vector of length @var{M} whose 0..(@var{M}-1)th
|
|
|
|
elements match @var{qpattern1}, @var{qpattern2} @dots{} @var{qpatternm},
|
2014-09-06 00:59:00 +00:00
|
|
|
respectively.
|
2012-12-09 15:36:46 +00:00
|
|
|
@item @var{atom}
|
|
|
|
This pattern matches any atom @code{equal} to @var{atom}.
|
|
|
|
@item ,@var{upattern}
|
|
|
|
This pattern matches any object that matches the @var{upattern}.
|
|
|
|
@end table
|
|
|
|
|
|
|
|
@item @var{symbol}
|
|
|
|
A mere symbol in a U-pattern matches anything, and additionally let-binds this
|
|
|
|
symbol to the value that it matched, so that you can later refer to it, either
|
|
|
|
in the @var{body-forms} or also later in the pattern.
|
|
|
|
@item _
|
|
|
|
This so-called @emph{don't care} pattern matches anything, like the previous
|
2013-03-11 17:05:30 +00:00
|
|
|
one, but unlike symbol patterns it does not bind any variable.
|
2012-12-09 15:36:46 +00:00
|
|
|
@item (pred @var{pred})
|
|
|
|
This pattern matches if the function @var{pred} returns non-@code{nil} when
|
|
|
|
called with the object being matched.
|
|
|
|
@item (or @var{upattern1} @var{upattern2}@dots{})
|
|
|
|
This pattern matches as soon as one of the argument patterns succeeds.
|
|
|
|
All argument patterns should let-bind the same variables.
|
|
|
|
@item (and @var{upattern1} @var{upattern2}@dots{})
|
|
|
|
This pattern matches only if all the argument patterns succeed.
|
|
|
|
@item (guard @var{exp})
|
|
|
|
This pattern ignores the object being examined and simply succeeds if @var{exp}
|
|
|
|
evaluates to non-@code{nil} and fails otherwise. It is typically used inside
|
|
|
|
an @code{and} pattern. For example, @code{(and x (guard (< x 10)))}
|
|
|
|
is a pattern which matches any number smaller than 10 and let-binds it to
|
|
|
|
the variable @code{x}.
|
|
|
|
@end table
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@node Combining Conditions
|
|
|
|
@section Constructs for Combining Conditions
|
Improve indexing on the chapter/section/subsection levels.
doc/lispref/windows.texi (Recombining Windows): Index subject of sections.
doc/lispref/variables.texi (Variables with Restricted Values)
(Generalized Variables): Index subject of sections.
doc/lispref/text.texi (Buffer Contents, Examining Properties)
(Changing Properties, Property Search, Substitution): Index
subject of sections.
doc/lispref/syntax.texi (Motion and Syntax, Parsing Expressions)
(Motion via Parsing, Position Parse, Control Parsing): Index
subject of sections.
doc/lispref/strings.texi (Predicates for Strings, Creating Strings)
(Modifying Strings, Text Comparison): Index subject of sections.
doc/lispref/searching.texi (Syntax of Regexps, Regexp Special)
(Regexp Functions, Regexp Functions): Index subject of sections.
doc/lispref/processes.texi (Subprocess Creation, Process Information): Index
subject of sections.
doc/lispref/positions.texi (Screen Lines): Index subject of sections.
doc/lispref/nonascii.texi (Scanning Charsets, Specifying Coding Systems):
Index subject of sections.
doc/lispref/minibuf.texi (Text from Minibuffer, Object from Minibuffer)
(Multiple Queries, Minibuffer Contents): Index subject of
sections.
doc/lispref/markers.texi (Predicates on Markers, Creating Markers)
(Information from Markers, Moving Markers): Index subject of
sections.
doc/lispref/macros.texi (Defining Macros, Problems with Macros): Index
subject of sections.
doc/lispref/loading.texi (Loading Non-ASCII, Where Defined): Index subject
of sections.
doc/lispref/lists.texi (List-related Predicates, List Variables, Setcar)
(Setcdr, Plist Access): Index subject of sections.
doc/lispref/keymaps.texi (Controlling Active Maps, Scanning Keymaps)
(Modifying Menus): Index subject of sections.
doc/lispref/help.texi (Accessing Documentation, Help Functions): Index
subject of sections.
doc/lispref/hash.texi (Hash Access): Index subject of sections.
doc/lispref/functions.texi (Core Advising Primitives)
(Advising Named Functions, Porting old advices): Index subject of
sections.
doc/lispref/frames.texi (Creating Frames, Initial Parameters)
(Position Parameters, Buffer Parameters, Minibuffers and Frames)
(Pop-Up Menus, Drag and Drop): Index subject of sections.
doc/lispref/files.texi (Visiting Functions, Kinds of Files)
(Unique File Names): Index subject of sections.
doc/lispref/display.texi (Refresh Screen, Echo Area Customization)
(Warning Variables, Warning Options, Delayed Warnings)
(Temporary Displays, Managing Overlays, Overlay Properties)
(Finding Overlays, Size of Displayed Text, Defining Faces)
(Attribute Functions, Displaying Faces, Face Remapping)
(Basic Faces, Font Lookup, Fontsets, Replacing Specs)
(Defining Images, Showing Images): Index subject of sections.
doc/lispref/debugging.texi (Debugging, Explicit Debug)
(Invoking the Debugger, Excess Open, Excess Close): Index subject
of sections.
doc/lispref/customize.texi (Defining New Types, Applying Customizations)
(Custom Themes): Index subject of sections.
doc/lispref/control.texi (Sequencing, Combining Conditions)
(Processing of Errors, Cleanups): Index subject of sections.
doc/lispref/compile.texi (Eval During Compile): Index subject of sections.
doc/lispref/commands.texi (Using Interactive, Distinguish Interactive)
(Command Loop Info, Classifying Events, Event Mod)
(Invoking the Input Method): Index subject of sections.
doc/lispref/buffers.texi (Buffer List, Buffer Gap): Index subject of sections.
doc/lispref/backups.texi (Making Backups, Numbered Backups, Backup Names)
(Reverting): Index subject of sections.
doc/lispref/abbrevs.texi (Abbrev Tables, Defining Abbrevs, Abbrev Files)
(Abbrev Expansion, Standard Abbrev Tables, Abbrev Properties)
(Abbrev Table Properties): Index subject of sections.
doc/lispref/os.texi (Time of Day, Time Conversion, Time Parsing)
(Time Calculations, Idle Timers): Index subject of sections.
2014-12-23 18:42:30 +00:00
|
|
|
@cindex combining conditions
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
This section describes three constructs that are often used together
|
|
|
|
with @code{if} and @code{cond} to express complicated conditions. The
|
|
|
|
constructs @code{and} and @code{or} can also be used individually as
|
|
|
|
kinds of multiple conditional constructs.
|
|
|
|
|
|
|
|
@defun not condition
|
|
|
|
This function tests for the falsehood of @var{condition}. It returns
|
|
|
|
@code{t} if @var{condition} is @code{nil}, and @code{nil} otherwise.
|
|
|
|
The function @code{not} is identical to @code{null}, and we recommend
|
|
|
|
using the name @code{null} if you are testing for an empty list.
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defspec and conditions@dots{}
|
|
|
|
The @code{and} special form tests whether all the @var{conditions} are
|
|
|
|
true. It works by evaluating the @var{conditions} one by one in the
|
|
|
|
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 @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. 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,
|
|
|
|
which is not @code{nil}. The third condition is @code{nil}, so the
|
|
|
|
remaining condition is never evaluated.
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(and (print 1) (print 2) nil (print 3))
|
|
|
|
@print{} 1
|
|
|
|
@print{} 2
|
|
|
|
@result{} nil
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
Here is a more realistic example of using @code{and}:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(if (and (consp foo) (eq (car foo) 'x))
|
|
|
|
(message "foo is a list starting with x"))
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
@noindent
|
|
|
|
Note that @code{(car foo)} is not executed if @code{(consp foo)} returns
|
|
|
|
@code{nil}, thus avoiding an error.
|
|
|
|
|
|
|
|
@code{and} expressions can also be written using either @code{if} or
|
|
|
|
@code{cond}. Here's how:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(and @var{arg1} @var{arg2} @var{arg3})
|
|
|
|
@equiv{}
|
|
|
|
(if @var{arg1} (if @var{arg2} @var{arg3}))
|
|
|
|
@equiv{}
|
|
|
|
(cond (@var{arg1} (cond (@var{arg2} @var{arg3}))))
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
@end defspec
|
|
|
|
|
|
|
|
@defspec or conditions@dots{}
|
|
|
|
The @code{or} special form tests whether at least one of the
|
|
|
|
@var{conditions} is true. It works by evaluating all the
|
|
|
|
@var{conditions} one by one in the order written.
|
|
|
|
|
|
|
|
If any of the @var{conditions} evaluates to a non-@code{nil} value, then
|
|
|
|
the result of the @code{or} must be non-@code{nil}; so @code{or} returns
|
|
|
|
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}. 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
|
|
|
|
@code{nil} or the integer zero:
|
|
|
|
|
|
|
|
@example
|
|
|
|
(or (eq x nil) (eq x 0))
|
|
|
|
@end example
|
|
|
|
|
|
|
|
Like the @code{and} construct, @code{or} can be written in terms of
|
|
|
|
@code{cond}. For example:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(or @var{arg1} @var{arg2} @var{arg3})
|
|
|
|
@equiv{}
|
|
|
|
(cond (@var{arg1})
|
|
|
|
(@var{arg2})
|
|
|
|
(@var{arg3}))
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
You could almost write @code{or} in terms of @code{if}, but not quite:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(if @var{arg1} @var{arg1}
|
|
|
|
(if @var{arg2} @var{arg2}
|
|
|
|
@var{arg3}))
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
@noindent
|
|
|
|
This is not completely equivalent because it can evaluate @var{arg1} or
|
|
|
|
@var{arg2} twice. By contrast, @code{(or @var{arg1} @var{arg2}
|
|
|
|
@var{arg3})} never evaluates any argument more than once.
|
|
|
|
@end defspec
|
|
|
|
|
|
|
|
@node Iteration
|
|
|
|
@section Iteration
|
|
|
|
@cindex iteration
|
|
|
|
@cindex recursion
|
|
|
|
|
|
|
|
Iteration means executing part of a program repetitively. For
|
|
|
|
example, you might want to repeat some computation once for each element
|
|
|
|
of a list, or once for each integer from 0 to @var{n}. You can do this
|
|
|
|
in Emacs Lisp with the special form @code{while}:
|
|
|
|
|
|
|
|
@defspec while condition forms@dots{}
|
|
|
|
@code{while} first evaluates @var{condition}. If the result is
|
|
|
|
non-@code{nil}, it evaluates @var{forms} in textual order. Then it
|
|
|
|
reevaluates @var{condition}, and if the result is non-@code{nil}, it
|
|
|
|
evaluates @var{forms} again. This process repeats until @var{condition}
|
|
|
|
evaluates to @code{nil}.
|
|
|
|
|
|
|
|
There is no limit on the number of iterations that may occur. The loop
|
|
|
|
will continue until either @var{condition} evaluates to @code{nil} or
|
|
|
|
until an error or @code{throw} jumps out of it (@pxref{Nonlocal Exits}).
|
|
|
|
|
|
|
|
The value of a @code{while} form is always @code{nil}.
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(setq num 0)
|
|
|
|
@result{} 0
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
(while (< num 4)
|
|
|
|
(princ (format "Iteration %d." num))
|
|
|
|
(setq num (1+ num)))
|
|
|
|
@print{} Iteration 0.
|
|
|
|
@print{} Iteration 1.
|
|
|
|
@print{} Iteration 2.
|
|
|
|
@print{} Iteration 3.
|
|
|
|
@result{} nil
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
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
|
|
|
|
(while (progn
|
|
|
|
(forward-line 1)
|
|
|
|
(not (looking-at "^$"))))
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
@noindent
|
|
|
|
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{}
|
|
|
|
This construct executes @var{body} once for each element of
|
|
|
|
@var{list}, binding the variable @var{var} locally 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{}
|
|
|
|
This construct executes @var{body} once for each integer from 0
|
|
|
|
(inclusive) to @var{count} (exclusive), binding the variable @var{var}
|
|
|
|
to 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} to do something 100 times:
|
|
|
|
|
|
|
|
@example
|
|
|
|
(dotimes (i 100)
|
|
|
|
(insert "I will not obey absurd orders\n"))
|
|
|
|
@end example
|
|
|
|
@end defmac
|
|
|
|
|
2015-03-02 07:57:51 +00:00
|
|
|
@node Generators
|
|
|
|
@section Generators
|
|
|
|
@cindex generators
|
|
|
|
|
|
|
|
A @dfn{generator} is a function that produces a potentially-infinite
|
|
|
|
stream of values. Each time the function produces a value, it
|
|
|
|
suspends itself and waits for a caller to request the next value.
|
|
|
|
|
|
|
|
@defmac iter-defun name args [doc] [declare] [interactive] body@dots{}
|
|
|
|
@code{iter-defun} defines a generator function. A generator function
|
|
|
|
has the same signature as a normal function, but works differently.
|
|
|
|
Instead of executing @var{body} when called, a generator function
|
|
|
|
returns an iterator object. That iterator runs @var{body} to generate
|
|
|
|
values, emitting a value and pausing where @code{iter-yield} or
|
|
|
|
@code{iter-yield-from} appears. When @var{body} returns normally,
|
|
|
|
@code{iter-next} signals @code{iter-end-of-sequence} with @var{body}'s
|
|
|
|
result as its condition data.
|
|
|
|
|
|
|
|
Any kind of Lisp code is valid inside @var{body}, but
|
|
|
|
@code{iter-yield} and @code{iter-yield-from} cannot appear inside
|
|
|
|
@code{unwind-protect} forms.
|
|
|
|
|
|
|
|
@end defmac
|
|
|
|
|
|
|
|
@defmac iter-lambda args [doc] [interactive] body@dots{}
|
|
|
|
@code{iter-lambda} produces an unnamed generator function that works
|
|
|
|
just like a generator function produced with @code{iter-defun}.
|
|
|
|
@end defmac
|
|
|
|
|
|
|
|
@defmac iter-yield value
|
|
|
|
When it appears inside a generator function, @code{iter-yield}
|
|
|
|
indicates that the current iterator should pause and return
|
|
|
|
@var{value} from @code{iter-next}. @code{iter-yield} evaluates to the
|
|
|
|
@code{value} parameter of next call to @code{iter-next}.
|
|
|
|
@end defmac
|
|
|
|
|
|
|
|
@defmac iter-yield-from iterator
|
|
|
|
@code{iter-yield-from} yields all the values that @var{iterator}
|
|
|
|
produces and evaluates to the value that @var{iterator}'s generator
|
|
|
|
function returns normally. While it has control, @var{iterator}
|
2015-03-03 18:56:24 +00:00
|
|
|
receives values sent to the iterator using @code{iter-next}.
|
2015-03-02 07:57:51 +00:00
|
|
|
@end defmac
|
|
|
|
|
|
|
|
To use a generator function, first call it normally, producing a
|
|
|
|
@dfn{iterator} object. An iterator is a specific instance of a
|
|
|
|
generator. Then use @code{iter-next} to retrieve values from this
|
|
|
|
iterator. When there are no more values to pull from an iterator,
|
|
|
|
@code{iter-next} raises an @code{iter-end-of-sequence} condition with
|
|
|
|
the iterator's final value.
|
|
|
|
|
|
|
|
It's important to note that generator function bodies only execute
|
|
|
|
inside calls to @code{iter-next}. A call to a function defined with
|
|
|
|
@code{iter-defun} produces an iterator; you must ``drive'' this
|
|
|
|
iterator with @code{iter-next} for anything interesting to happen.
|
|
|
|
Each call to a generator function produces a @emph{different}
|
|
|
|
iterator, each with its own state.
|
|
|
|
|
|
|
|
@defun iter-next iterator value
|
|
|
|
Retrieve the next value from @var{iterator}. If there are no more
|
|
|
|
values to be generated (because @var{iterator}'s generator function
|
|
|
|
returned), @code{iter-next} signals the @code{iter-end-of-sequence}
|
|
|
|
condition; the data value associated with this condition is the value
|
|
|
|
with which @var{iterator}'s generator function returned.
|
|
|
|
|
|
|
|
@var{value} is sent into the iterator and becomes the value to which
|
|
|
|
@code{iter-yield} evaluates. @var{value} is ignored for the first
|
|
|
|
@code{iter-next} call to a given iterator, since at the start of
|
|
|
|
@var{iterator}'s generator function, the generator function is not
|
|
|
|
evaluating any @code{iter-yield} form.
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun iter-close iterator
|
2015-03-03 18:56:24 +00:00
|
|
|
If @var{iterator} is suspended inside an @code{unwind-protect}'s
|
|
|
|
@code{bodyform} and becomes unreachable, Emacs will eventually run
|
|
|
|
unwind handlers after a garbage collection pass. (Note that
|
|
|
|
@code{iter-yield} is illegal inside an @code{unwind-protect}'s
|
|
|
|
@code{unwindforms}.) To ensure that these handlers are run before
|
2015-03-02 07:57:51 +00:00
|
|
|
then, use @code{iter-close}.
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
Some convenience functions are provided to make working with
|
|
|
|
iterators easier:
|
|
|
|
|
|
|
|
@defmac iter-do (var iterator) body @dots{}
|
|
|
|
Run @var{body} with @var{var} bound to each value that
|
|
|
|
@var{iterator} produces.
|
|
|
|
@end defmac
|
|
|
|
|
|
|
|
The Common Lisp loop facility also contains features for working with
|
|
|
|
iterators. See @xref{Loop Facility,,,cl,Common Lisp Extensions}.
|
|
|
|
|
|
|
|
The following piece of code demonstrates some important principles of
|
|
|
|
working with iterators.
|
|
|
|
|
|
|
|
@example
|
|
|
|
(iter-defun my-iter (x)
|
|
|
|
(iter-yield (1+ (iter-yield (1+ x))))
|
2015-03-03 18:56:24 +00:00
|
|
|
;; Return normally
|
|
|
|
-1)
|
2015-03-02 07:57:51 +00:00
|
|
|
|
|
|
|
(let* ((iter (my-iter 5))
|
|
|
|
(iter2 (my-iter 0)))
|
|
|
|
;; Prints 6
|
|
|
|
(print (iter-next iter))
|
|
|
|
;; Prints 9
|
|
|
|
(print (iter-next iter 8))
|
|
|
|
;; Prints 1; iter and iter2 have distinct states
|
|
|
|
(print (iter-next iter2 nil))
|
|
|
|
|
|
|
|
;; We expect the iter sequence to end now
|
|
|
|
(condition-case x
|
|
|
|
(iter-next iter)
|
|
|
|
(iter-end-of-sequence
|
|
|
|
;; Prints -1, which my-iter returned normally
|
|
|
|
(print (cdr x)))))
|
|
|
|
@end example
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@node Nonlocal Exits
|
|
|
|
@section Nonlocal Exits
|
|
|
|
@cindex nonlocal exits
|
|
|
|
|
|
|
|
A @dfn{nonlocal exit} is a transfer of control from one point in a
|
|
|
|
program to another remote point. Nonlocal exits can occur in Emacs Lisp
|
|
|
|
as a result of errors; you can also use them under explicit control.
|
|
|
|
Nonlocal exits unbind all variable bindings made by the constructs being
|
|
|
|
exited.
|
|
|
|
|
|
|
|
@menu
|
|
|
|
* Catch and Throw:: Nonlocal exits for the program's own purposes.
|
|
|
|
* Examples of Catch:: Showing how such nonlocal exits can be written.
|
|
|
|
* Errors:: How errors are signaled and handled.
|
|
|
|
* Cleanups:: Arranging to run a cleanup form if an error happens.
|
|
|
|
@end menu
|
|
|
|
|
|
|
|
@node Catch and Throw
|
|
|
|
@subsection Explicit Nonlocal Exits: @code{catch} and @code{throw}
|
|
|
|
|
|
|
|
Most control constructs affect only the flow of control within the
|
|
|
|
construct itself. The function @code{throw} is the exception to this
|
|
|
|
rule of normal program execution: it performs a nonlocal exit on
|
|
|
|
request. (There are other exceptions, but they are for error handling
|
|
|
|
only.) @code{throw} is used inside a @code{catch}, and jumps back to
|
|
|
|
that @code{catch}. For example:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(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} 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
|
2012-09-02 04:47:28 +00:00
|
|
|
@code{catch}, including function calls. When binding constructs such
|
|
|
|
as @code{let} or function calls are exited in this way, the bindings
|
|
|
|
are unbound, just as they are when these constructs exit normally
|
2007-09-06 04:25:08 +00:00
|
|
|
(@pxref{Local Variables}). Likewise, @code{throw} restores the buffer
|
|
|
|
and position saved by @code{save-excursion} (@pxref{Excursions}), and
|
2012-09-02 04:47:28 +00:00
|
|
|
the narrowing status saved by @code{save-restriction}. It also runs
|
|
|
|
any cleanups established with the @code{unwind-protect} special form
|
|
|
|
when it exits that form (@pxref{Cleanups}).
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
The @code{throw} need not appear lexically within the @code{catch}
|
|
|
|
that it jumps to. It can equally well be called from another function
|
|
|
|
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}
|
|
|
|
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,
|
|
|
|
have several ways of transferring control nonsequentially: @code{return},
|
|
|
|
@code{return-from}, and @code{go}, for example. Emacs Lisp has only
|
2012-10-31 21:00:57 +00:00
|
|
|
@code{throw}. The @file{cl-lib} library provides versions of some of
|
|
|
|
these. @xref{Blocks and Exits,,,cl,Common Lisp Extensions}.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end quotation
|
|
|
|
|
|
|
|
@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 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 normally (without
|
|
|
|
error or nonlocal exit) the value of the last body form is returned from
|
|
|
|
the @code{catch}.
|
|
|
|
|
|
|
|
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
|
|
|
|
The purpose of @code{throw} is to return from a return point previously
|
|
|
|
established with @code{catch}. The argument @var{tag} is used to choose
|
|
|
|
among the various existing return points; it must be @code{eq} to the value
|
|
|
|
specified in the @code{catch}. If multiple return points match @var{tag},
|
|
|
|
the innermost one is used.
|
|
|
|
|
|
|
|
The argument @var{value} is used as the value to return from that
|
|
|
|
@code{catch}.
|
|
|
|
|
|
|
|
@kindex no-catch
|
|
|
|
If no return point is in effect with tag @var{tag}, then a @code{no-catch}
|
|
|
|
error is signaled with data @code{(@var{tag} @var{value})}.
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@node Examples of Catch
|
|
|
|
@subsection Examples of @code{catch} and @code{throw}
|
|
|
|
|
|
|
|
One way to use @code{catch} and @code{throw} is to exit from a doubly
|
2012-04-26 00:31:47 +00:00
|
|
|
nested loop. (In most languages, this would be done with a ``goto''.)
|
2007-09-06 04:25:08 +00:00
|
|
|
Here we compute @code{(foo @var{i} @var{j})} for @var{i} and @var{j}
|
|
|
|
varying from 0 to 9:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(defun search-foo ()
|
|
|
|
(catch 'loop
|
|
|
|
(let ((i 0))
|
|
|
|
(while (< i 10)
|
|
|
|
(let ((j 0))
|
|
|
|
(while (< j 10)
|
|
|
|
(if (foo i j)
|
|
|
|
(throw 'loop (list i j)))
|
|
|
|
(setq j (1+ j))))
|
|
|
|
(setq i (1+ i))))))
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
@noindent
|
|
|
|
If @code{foo} ever returns non-@code{nil}, we stop immediately and return a
|
|
|
|
list of @var{i} and @var{j}. If @code{foo} always returns @code{nil}, the
|
|
|
|
@code{catch} returns normally, and the value is @code{nil}, since that
|
|
|
|
is the result of the @code{while}.
|
|
|
|
|
|
|
|
Here are two tricky examples, slightly different, showing two
|
|
|
|
return points at once. First, two return points with the same tag,
|
|
|
|
@code{hack}:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(defun catch2 (tag)
|
|
|
|
(catch tag
|
|
|
|
(throw 'hack 'yes)))
|
|
|
|
@result{} catch2
|
|
|
|
@end group
|
|
|
|
|
|
|
|
@group
|
|
|
|
(catch 'hack
|
|
|
|
(print (catch2 'hack))
|
|
|
|
'no)
|
|
|
|
@print{} yes
|
|
|
|
@result{} no
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
@noindent
|
|
|
|
Since both return points have tags that match the @code{throw}, it goes to
|
|
|
|
the inner one, the one established in @code{catch2}. Therefore,
|
|
|
|
@code{catch2} returns normally with value @code{yes}, and this value is
|
|
|
|
printed. Finally the second body form in the outer @code{catch}, which is
|
|
|
|
@code{'no}, is evaluated and returned from the outer @code{catch}.
|
|
|
|
|
|
|
|
Now let's change the argument given to @code{catch2}:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(catch 'hack
|
|
|
|
(print (catch2 'quux))
|
|
|
|
'no)
|
|
|
|
@result{} yes
|
|
|
|
@end group
|
|
|
|
@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, @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
|
|
|
|
@cindex errors
|
|
|
|
|
|
|
|
When Emacs Lisp attempts to evaluate a form that, for some reason,
|
|
|
|
cannot be evaluated, it @dfn{signals} an @dfn{error}.
|
|
|
|
|
|
|
|
When an error is signaled, Emacs's default reaction is to print an
|
|
|
|
error message and terminate execution of the current command. This is
|
|
|
|
the right thing to do in most cases, such as if you type @kbd{C-f} at
|
|
|
|
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 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. (@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}
|
|
|
|
instead. @xref{Catch and Throw}.
|
|
|
|
|
|
|
|
@menu
|
|
|
|
* 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 Symbols:: How errors are classified for trapping them.
|
|
|
|
@end menu
|
|
|
|
|
|
|
|
@node Signaling Errors
|
|
|
|
@subsubsection How to Signal an Error
|
|
|
|
@cindex signaling errors
|
|
|
|
|
|
|
|
@dfn{Signaling} 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
|
|
|
|
@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
|
More-conservative ‘format’ quote restyling
Instead of restyling curved quotes for every call to ‘format’,
create a new function ‘format-message’ that does the restyling,
and using the new function instead of ‘format’ only in contexts
where this seems appropriate.
Problem reported by Dmitry Gutov and Andreas Schwab in:
http://lists.gnu.org/archive/html/emacs-devel/2015-08/msg00826.html
http://lists.gnu.org/archive/html/emacs-devel/2015-08/msg00827.html
* doc/lispref/commands.texi (Using Interactive):
* doc/lispref/control.texi (Signaling Errors, Signaling Errors):
* doc/lispref/display.texi (Displaying Messages, Progress):
* doc/lispref/elisp.texi:
* doc/lispref/help.texi (Keys in Documentation):
* doc/lispref/minibuf.texi (Minibuffer Misc):
* doc/lispref/strings.texi (Formatting Strings):
* etc/NEWS:
Document the changes.
* lisp/abbrev.el (expand-region-abbrevs):
* lisp/apropos.el (apropos-library):
* lisp/calc/calc-ext.el (calc-record-message)
(calc-user-function-list):
* lisp/calc/calc-help.el (calc-describe-key, calc-full-help):
* lisp/calc/calc-lang.el (math-read-big-balance):
* lisp/calc/calc-store.el (calc-edit-variable):
* lisp/calc/calc-units.el (math-build-units-table-buffer):
* lisp/calc/calc-yank.el (calc-edit-mode):
* lisp/calendar/icalendar.el (icalendar-export-region)
(icalendar--add-diary-entry):
* lisp/cedet/mode-local.el (mode-local-print-binding)
(mode-local-describe-bindings-2):
* lisp/cedet/semantic/complete.el (semantic-completion-message):
* lisp/cedet/semantic/edit.el (semantic-parse-changes-failed):
* lisp/cedet/semantic/wisent/comp.el (wisent-log):
* lisp/cedet/srecode/insert.el (srecode-insert-show-error-report):
* lisp/descr-text.el (describe-text-properties-1, describe-char):
* lisp/dframe.el (dframe-message):
* lisp/dired-aux.el (dired-query):
* lisp/emacs-lisp/byte-opt.el (byte-compile-log-lap-1):
* lisp/emacs-lisp/bytecomp.el (byte-compile-log)
(byte-compile-log-file, byte-compile-warn, byte-compile-form):
* lisp/emacs-lisp/cconv.el (cconv-convert, cconv--analyze-use)
(cconv-analyze-form):
* lisp/emacs-lisp/check-declare.el (check-declare-warn):
* lisp/emacs-lisp/checkdoc.el (checkdoc-this-string-valid-engine):
* lisp/emacs-lisp/cl-macs.el (cl-symbol-macrolet):
* lisp/emacs-lisp/edebug.el (edebug-format):
* lisp/emacs-lisp/eieio-core.el (eieio-oref):
* lisp/emacs-lisp/eldoc.el (eldoc-minibuffer-message)
(eldoc-message):
* lisp/emacs-lisp/elint.el (elint-file, elint-log):
* lisp/emacs-lisp/find-func.el (find-function-library):
* lisp/emacs-lisp/macroexp.el (macroexp--obsolete-warning):
* lisp/emacs-lisp/map-ynp.el (map-y-or-n-p):
* lisp/emacs-lisp/nadvice.el (advice--make-docstring):
* lisp/emacs-lisp/package.el (package-compute-transaction)
(package-install-button-action, package-delete-button-action)
(package-menu--list-to-prompt):
* lisp/emacs-lisp/timer.el (timer-event-handler):
* lisp/emacs-lisp/warnings.el (lwarn, warn):
* lisp/emulation/viper-cmd.el:
(viper-toggle-parse-sexp-ignore-comments)
(viper-kill-buffer, viper-brac-function):
* lisp/emulation/viper-macs.el (viper-record-kbd-macro):
* lisp/facemenu.el (facemenu-add-new-face):
* lisp/faces.el (face-documentation, read-face-name)
(face-read-string, read-face-font, describe-face):
* lisp/files.el (find-alternate-file, hack-local-variables)
(hack-one-local-variable--obsolete, write-file)
(basic-save-buffer, delete-directory):
* lisp/format.el (format-write-file, format-find-file)
(format-insert-file):
* lisp/help-fns.el (help-fns--key-bindings)
(help-fns--compiler-macro, help-fns--obsolete)
(help-fns--interactive-only, describe-function-1)
(describe-variable):
* lisp/help.el (describe-mode):
* lisp/info-xref.el (info-xref-output):
* lisp/info.el (Info-virtual-index-find-node)
(Info-virtual-index, info-apropos):
* lisp/international/kkc.el (kkc-error):
* lisp/international/mule-cmds.el:
(select-safe-coding-system-interactively)
(select-safe-coding-system, describe-input-method):
* lisp/international/mule-conf.el (code-offset):
* lisp/international/mule-diag.el (describe-character-set)
(list-input-methods-1):
* lisp/international/quail.el (quail-error):
* lisp/minibuffer.el (minibuffer-message):
* lisp/mpc.el (mpc--debug):
* lisp/msb.el (msb--choose-menu):
* lisp/net/ange-ftp.el (ange-ftp-message):
* lisp/net/gnutls.el (gnutls-message-maybe):
* lisp/net/newst-backend.el (newsticker--sentinel-work):
* lisp/net/newst-treeview.el (newsticker--treeview-load):
* lisp/net/nsm.el (nsm-query-user):
* lisp/net/rlogin.el (rlogin):
* lisp/net/soap-client.el (soap-warning):
* lisp/net/tramp.el (tramp-debug-message):
* lisp/nxml/nxml-outln.el (nxml-report-outline-error):
* lisp/nxml/nxml-parse.el (nxml-parse-error):
* lisp/nxml/rng-cmpct.el (rng-c-error):
* lisp/nxml/rng-match.el (rng-compile-error):
* lisp/nxml/rng-uri.el (rng-uri-error):
* lisp/obsolete/iswitchb.el (iswitchb-possible-new-buffer):
* lisp/org/org-ctags.el:
(org-ctags-ask-rebuild-tags-file-then-find-tag):
* lisp/proced.el (proced-log):
* lisp/progmodes/ebnf2ps.el (ebnf-log):
* lisp/progmodes/flymake.el (flymake-log):
* lisp/progmodes/vhdl-mode.el (vhdl-warning-when-idle):
* lisp/replace.el (occur-1):
* lisp/simple.el (execute-extended-command)
(undo-outer-limit-truncate, define-alternatives):
* lisp/startup.el (command-line):
* lisp/subr.el (error, user-error, add-to-list):
* lisp/tutorial.el (tutorial--describe-nonstandard-key)
(tutorial--find-changed-keys):
* src/callint.c (Fcall_interactively):
* src/editfns.c (Fmessage, Fmessage_box):
Restyle the quotes of format strings intended for use as a
diagnostic, when restyling seems appropriate.
* lisp/subr.el (format-message): New function.
* src/doc.c (Finternal__text_restyle): New function.
(syms_of_doc): Define it.
2015-08-24 05:38:02 +00:00
|
|
|
applying @code{format-message} (@pxref{Formatting Strings}) to
|
2007-09-06 04:25:08 +00:00
|
|
|
@var{format-string} and @var{args}.
|
|
|
|
|
|
|
|
These examples show typical uses of @code{error}:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(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
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
@code{error} works by calling @code{signal} with two arguments: the
|
|
|
|
error symbol @code{error}, and a list containing the string returned by
|
More-conservative ‘format’ quote restyling
Instead of restyling curved quotes for every call to ‘format’,
create a new function ‘format-message’ that does the restyling,
and using the new function instead of ‘format’ only in contexts
where this seems appropriate.
Problem reported by Dmitry Gutov and Andreas Schwab in:
http://lists.gnu.org/archive/html/emacs-devel/2015-08/msg00826.html
http://lists.gnu.org/archive/html/emacs-devel/2015-08/msg00827.html
* doc/lispref/commands.texi (Using Interactive):
* doc/lispref/control.texi (Signaling Errors, Signaling Errors):
* doc/lispref/display.texi (Displaying Messages, Progress):
* doc/lispref/elisp.texi:
* doc/lispref/help.texi (Keys in Documentation):
* doc/lispref/minibuf.texi (Minibuffer Misc):
* doc/lispref/strings.texi (Formatting Strings):
* etc/NEWS:
Document the changes.
* lisp/abbrev.el (expand-region-abbrevs):
* lisp/apropos.el (apropos-library):
* lisp/calc/calc-ext.el (calc-record-message)
(calc-user-function-list):
* lisp/calc/calc-help.el (calc-describe-key, calc-full-help):
* lisp/calc/calc-lang.el (math-read-big-balance):
* lisp/calc/calc-store.el (calc-edit-variable):
* lisp/calc/calc-units.el (math-build-units-table-buffer):
* lisp/calc/calc-yank.el (calc-edit-mode):
* lisp/calendar/icalendar.el (icalendar-export-region)
(icalendar--add-diary-entry):
* lisp/cedet/mode-local.el (mode-local-print-binding)
(mode-local-describe-bindings-2):
* lisp/cedet/semantic/complete.el (semantic-completion-message):
* lisp/cedet/semantic/edit.el (semantic-parse-changes-failed):
* lisp/cedet/semantic/wisent/comp.el (wisent-log):
* lisp/cedet/srecode/insert.el (srecode-insert-show-error-report):
* lisp/descr-text.el (describe-text-properties-1, describe-char):
* lisp/dframe.el (dframe-message):
* lisp/dired-aux.el (dired-query):
* lisp/emacs-lisp/byte-opt.el (byte-compile-log-lap-1):
* lisp/emacs-lisp/bytecomp.el (byte-compile-log)
(byte-compile-log-file, byte-compile-warn, byte-compile-form):
* lisp/emacs-lisp/cconv.el (cconv-convert, cconv--analyze-use)
(cconv-analyze-form):
* lisp/emacs-lisp/check-declare.el (check-declare-warn):
* lisp/emacs-lisp/checkdoc.el (checkdoc-this-string-valid-engine):
* lisp/emacs-lisp/cl-macs.el (cl-symbol-macrolet):
* lisp/emacs-lisp/edebug.el (edebug-format):
* lisp/emacs-lisp/eieio-core.el (eieio-oref):
* lisp/emacs-lisp/eldoc.el (eldoc-minibuffer-message)
(eldoc-message):
* lisp/emacs-lisp/elint.el (elint-file, elint-log):
* lisp/emacs-lisp/find-func.el (find-function-library):
* lisp/emacs-lisp/macroexp.el (macroexp--obsolete-warning):
* lisp/emacs-lisp/map-ynp.el (map-y-or-n-p):
* lisp/emacs-lisp/nadvice.el (advice--make-docstring):
* lisp/emacs-lisp/package.el (package-compute-transaction)
(package-install-button-action, package-delete-button-action)
(package-menu--list-to-prompt):
* lisp/emacs-lisp/timer.el (timer-event-handler):
* lisp/emacs-lisp/warnings.el (lwarn, warn):
* lisp/emulation/viper-cmd.el:
(viper-toggle-parse-sexp-ignore-comments)
(viper-kill-buffer, viper-brac-function):
* lisp/emulation/viper-macs.el (viper-record-kbd-macro):
* lisp/facemenu.el (facemenu-add-new-face):
* lisp/faces.el (face-documentation, read-face-name)
(face-read-string, read-face-font, describe-face):
* lisp/files.el (find-alternate-file, hack-local-variables)
(hack-one-local-variable--obsolete, write-file)
(basic-save-buffer, delete-directory):
* lisp/format.el (format-write-file, format-find-file)
(format-insert-file):
* lisp/help-fns.el (help-fns--key-bindings)
(help-fns--compiler-macro, help-fns--obsolete)
(help-fns--interactive-only, describe-function-1)
(describe-variable):
* lisp/help.el (describe-mode):
* lisp/info-xref.el (info-xref-output):
* lisp/info.el (Info-virtual-index-find-node)
(Info-virtual-index, info-apropos):
* lisp/international/kkc.el (kkc-error):
* lisp/international/mule-cmds.el:
(select-safe-coding-system-interactively)
(select-safe-coding-system, describe-input-method):
* lisp/international/mule-conf.el (code-offset):
* lisp/international/mule-diag.el (describe-character-set)
(list-input-methods-1):
* lisp/international/quail.el (quail-error):
* lisp/minibuffer.el (minibuffer-message):
* lisp/mpc.el (mpc--debug):
* lisp/msb.el (msb--choose-menu):
* lisp/net/ange-ftp.el (ange-ftp-message):
* lisp/net/gnutls.el (gnutls-message-maybe):
* lisp/net/newst-backend.el (newsticker--sentinel-work):
* lisp/net/newst-treeview.el (newsticker--treeview-load):
* lisp/net/nsm.el (nsm-query-user):
* lisp/net/rlogin.el (rlogin):
* lisp/net/soap-client.el (soap-warning):
* lisp/net/tramp.el (tramp-debug-message):
* lisp/nxml/nxml-outln.el (nxml-report-outline-error):
* lisp/nxml/nxml-parse.el (nxml-parse-error):
* lisp/nxml/rng-cmpct.el (rng-c-error):
* lisp/nxml/rng-match.el (rng-compile-error):
* lisp/nxml/rng-uri.el (rng-uri-error):
* lisp/obsolete/iswitchb.el (iswitchb-possible-new-buffer):
* lisp/org/org-ctags.el:
(org-ctags-ask-rebuild-tags-file-then-find-tag):
* lisp/proced.el (proced-log):
* lisp/progmodes/ebnf2ps.el (ebnf-log):
* lisp/progmodes/flymake.el (flymake-log):
* lisp/progmodes/vhdl-mode.el (vhdl-warning-when-idle):
* lisp/replace.el (occur-1):
* lisp/simple.el (execute-extended-command)
(undo-outer-limit-truncate, define-alternatives):
* lisp/startup.el (command-line):
* lisp/subr.el (error, user-error, add-to-list):
* lisp/tutorial.el (tutorial--describe-nonstandard-key)
(tutorial--find-changed-keys):
* src/callint.c (Fcall_interactively):
* src/editfns.c (Fmessage, Fmessage_box):
Restyle the quotes of format strings intended for use as a
diagnostic, when restyling seems appropriate.
* lisp/subr.el (format-message): New function.
* src/doc.c (Finternal__text_restyle): New function.
(syms_of_doc): Define it.
2015-08-24 05:38:02 +00:00
|
|
|
@code{format-message}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@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
|
|
|
|
@anchor{Definition of signal}
|
|
|
|
This function signals an error named by @var{error-symbol}. The
|
|
|
|
argument @var{data} is a list of additional Lisp objects relevant to
|
|
|
|
the circumstances of the error.
|
|
|
|
|
|
|
|
The argument @var{error-symbol} must be an @dfn{error symbol}---a symbol
|
* lisp/subr.el (define-error): New function.
* doc/lispref/control.texi (Signaling Errors): Refer to define-error.
(Error Symbols): Add `define-error'.
* doc/lispref/errors.texi (Standard Errors): Don't refer to `error-conditions'.
* lisp/progmodes/ada-xref.el (ada-error-file-not-found): Rename from
error-file-not-found and define with define-error.
* lisp/emacs-lisp/cl-lib.el (cl-assertion-failed): Move here from subr.el
and define with define-error.
* lisp/userlock.el (file-locked, file-supersession):
* lisp/simple.el (mark-inactive):
* lisp/progmodes/js.el (js-moz-bad-rpc, js-js-error):
* lisp/progmodes/ada-mode.el (ada-mode-errors):
* lisp/play/life.el (life-extinct):
* lisp/nxml/xsd-regexp.el (xsdre-invalid-regexp, xsdre-parse-error):
* lisp/nxml/xmltok.el (xmltok-markup-declaration-parse-error):
* lisp/nxml/rng-util.el (rng-error):
* lisp/nxml/rng-uri.el (rng-uri-error):
* lisp/nxml/rng-match.el (rng-compile-error):
* lisp/nxml/rng-cmpct.el (rng-c-incorrect-schema):
* lisp/nxml/nxml-util.el (nxml-error, nxml-file-parse-error):
* lisp/nxml/nxml-rap.el (nxml-scan-error):
* lisp/nxml/nxml-outln.el (nxml-outline-error):
* lisp/net/soap-client.el (soap-error):
* lisp/net/gnutls.el (gnutls-error):
* lisp/net/ange-ftp.el (ftp-error):
* lisp/mpc.el (mpc-proc-error):
* lisp/json.el (json-error, json-readtable-error, json-unknown-keyword)
(json-number-format, json-string-escape, json-string-format)
(json-key-format, json-object-format):
* lisp/jka-compr.el (compression-error):
* lisp/international/quail.el (quail-error):
* lisp/international/kkc.el (kkc-error):
* lisp/emacs-lisp/ert.el (ert-test-failed):
* lisp/calc/calc.el (calc-error, inexact-result, math-overflow)
(math-underflow):
* lisp/bookmark.el (bookmark-error-no-filename):
* lisp/epg.el (epg-error): Define with define-error.
2013-08-09 21:22:44 +00:00
|
|
|
defined with @code{define-error}. This is how Emacs Lisp classifies different
|
Minor quoting etc. fixes to lispref manual
* doc/lispref/tips.texi (Documentation Tips):
Distinguish more clearly among grave accent, apostrophe,
and single quote.
* doc/lispref/README, doc/lispref/buffers.texi:
* doc/lispref/commands.texi, doc/lispref/control.texi:
* doc/lispref/customize.texi, doc/lispref/display.texi:
* doc/lispref/elisp.texi, doc/lispref/files.texi:
* doc/lispref/frames.texi, doc/lispref/hash.texi:
* doc/lispref/help.texi, doc/lispref/internals.texi:
* doc/lispref/loading.texi, doc/lispref/makefile.w32-in:
* doc/lispref/markers.texi, doc/lispref/modes.texi:
* doc/lispref/nonascii.texi, doc/lispref/objects.texi:
* doc/lispref/os.texi, doc/lispref/positions.texi:
* doc/lispref/strings.texi, doc/lispref/syntax.texi:
* doc/lispref/text.texi, doc/lispref/tips.texi:
* doc/lispref/two-volume-cross-refs.txt, doc/lispref/windows.texi:
Use American-style double quoting in ordinary text,
and quote 'like this' when single-quoting in ASCII text.
Also, fix some minor spacing issues.
2015-04-10 18:27:21 +00:00
|
|
|
sorts of errors. @xref{Error Symbols}, for a description of error symbols,
|
2007-09-06 04:25:08 +00:00
|
|
|
error conditions and condition names.
|
|
|
|
|
|
|
|
If the error is not handled, the two arguments are used in printing
|
|
|
|
the error message. Normally, this error message is provided by the
|
|
|
|
@code{error-message} property of @var{error-symbol}. If @var{data} is
|
|
|
|
non-@code{nil}, this is followed by a colon and a comma separated list
|
|
|
|
of the unevaluated elements of @var{data}. For @code{error}, the
|
|
|
|
error message is the @sc{car} of @var{data} (that must be a string).
|
|
|
|
Subcategories of @code{file-error} are handled specially.
|
|
|
|
|
|
|
|
The number and significance of the objects in @var{data} depends on
|
2008-11-25 04:27:29 +00:00
|
|
|
@var{error-symbol}. For example, with a @code{wrong-type-argument} error,
|
2007-09-06 04:25:08 +00:00
|
|
|
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.
|
|
|
|
|
|
|
|
Both @var{error-symbol} and @var{data} are available to any error
|
|
|
|
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}).
|
|
|
|
|
More small edits for doc/lispref
* control.texi: Where possible, use example rather than smallexample.
(Sequencing, Conditionals, Signaling Errors, Handling Errors):
Tweak page breaks.
* customize.texi: Where possible, use example rather than smallexample.
(Common Keywords, Variable Definitions, Applying Customizations)
(Custom Themes): Tweak page breaks.
* eval.texi, functions.texi, loading.texi, macros.texi:
Where possible, use example rather than smallexample.
* sequences.texi (Arrays): Tweak page breaks.
* symbols.texi: Where possible, use example rather than smallexample.
(Symbol Components): Fix typo.
(Other Plists): Tweak page break.
2012-05-05 00:37:30 +00:00
|
|
|
The function @code{signal} never returns.
|
|
|
|
@c (though in older Emacs versions it sometimes could).
|
2007-09-06 04:25:08 +00:00
|
|
|
|
More small edits for doc/lispref
* control.texi: Where possible, use example rather than smallexample.
(Sequencing, Conditionals, Signaling Errors, Handling Errors):
Tweak page breaks.
* customize.texi: Where possible, use example rather than smallexample.
(Common Keywords, Variable Definitions, Applying Customizations)
(Custom Themes): Tweak page breaks.
* eval.texi, functions.texi, loading.texi, macros.texi:
Where possible, use example rather than smallexample.
* sequences.texi (Arrays): Tweak page breaks.
* symbols.texi: Where possible, use example rather than smallexample.
(Symbol Components): Fix typo.
(Other Plists): Tweak page break.
2012-05-05 00:37:30 +00:00
|
|
|
@example
|
2007-09-06 04:25:08 +00:00
|
|
|
@group
|
|
|
|
(signal 'wrong-number-of-arguments '(x y))
|
|
|
|
@error{} Wrong number of arguments: x, y
|
|
|
|
@end group
|
|
|
|
|
|
|
|
@group
|
|
|
|
(signal 'no-such-error '("My unknown error condition"))
|
|
|
|
@error{} peculiar error: "My unknown error condition"
|
|
|
|
@end group
|
More small edits for doc/lispref
* control.texi: Where possible, use example rather than smallexample.
(Sequencing, Conditionals, Signaling Errors, Handling Errors):
Tweak page breaks.
* customize.texi: Where possible, use example rather than smallexample.
(Common Keywords, Variable Definitions, Applying Customizations)
(Custom Themes): Tweak page breaks.
* eval.texi, functions.texi, loading.texi, macros.texi:
Where possible, use example rather than smallexample.
* sequences.texi (Arrays): Tweak page breaks.
* symbols.texi: Where possible, use example rather than smallexample.
(Symbol Components): Fix typo.
(Other Plists): Tweak page break.
2012-05-05 00:37:30 +00:00
|
|
|
@end example
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defun
|
|
|
|
|
2012-11-11 00:37:40 +00:00
|
|
|
@cindex user errors, signaling
|
|
|
|
@defun user-error format-string &rest args
|
|
|
|
This function behaves exactly like @code{error}, except that it uses
|
|
|
|
the error symbol @code{user-error} rather than @code{error}. As the
|
|
|
|
name suggests, this is intended to report errors on the part of the
|
|
|
|
user, rather than errors in the code itself. For example,
|
|
|
|
if you try to use the command @code{Info-history-back} (@kbd{l}) to
|
|
|
|
move back beyond the start of your Info browsing history, Emacs
|
|
|
|
signals a @code{user-error}. Such errors do not cause entry to the
|
|
|
|
debugger, even when @code{debug-on-error} is non-@code{nil}.
|
|
|
|
@xref{Error Debugging}.
|
|
|
|
@end defun
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@cindex CL note---no continuable errors
|
|
|
|
@quotation
|
|
|
|
@b{Common Lisp note:} Emacs Lisp has nothing like the Common Lisp
|
|
|
|
concept of continuable errors.
|
|
|
|
@end quotation
|
|
|
|
|
|
|
|
@node Processing of Errors
|
|
|
|
@subsubsection How Emacs Processes Errors
|
Improve indexing on the chapter/section/subsection levels.
doc/lispref/windows.texi (Recombining Windows): Index subject of sections.
doc/lispref/variables.texi (Variables with Restricted Values)
(Generalized Variables): Index subject of sections.
doc/lispref/text.texi (Buffer Contents, Examining Properties)
(Changing Properties, Property Search, Substitution): Index
subject of sections.
doc/lispref/syntax.texi (Motion and Syntax, Parsing Expressions)
(Motion via Parsing, Position Parse, Control Parsing): Index
subject of sections.
doc/lispref/strings.texi (Predicates for Strings, Creating Strings)
(Modifying Strings, Text Comparison): Index subject of sections.
doc/lispref/searching.texi (Syntax of Regexps, Regexp Special)
(Regexp Functions, Regexp Functions): Index subject of sections.
doc/lispref/processes.texi (Subprocess Creation, Process Information): Index
subject of sections.
doc/lispref/positions.texi (Screen Lines): Index subject of sections.
doc/lispref/nonascii.texi (Scanning Charsets, Specifying Coding Systems):
Index subject of sections.
doc/lispref/minibuf.texi (Text from Minibuffer, Object from Minibuffer)
(Multiple Queries, Minibuffer Contents): Index subject of
sections.
doc/lispref/markers.texi (Predicates on Markers, Creating Markers)
(Information from Markers, Moving Markers): Index subject of
sections.
doc/lispref/macros.texi (Defining Macros, Problems with Macros): Index
subject of sections.
doc/lispref/loading.texi (Loading Non-ASCII, Where Defined): Index subject
of sections.
doc/lispref/lists.texi (List-related Predicates, List Variables, Setcar)
(Setcdr, Plist Access): Index subject of sections.
doc/lispref/keymaps.texi (Controlling Active Maps, Scanning Keymaps)
(Modifying Menus): Index subject of sections.
doc/lispref/help.texi (Accessing Documentation, Help Functions): Index
subject of sections.
doc/lispref/hash.texi (Hash Access): Index subject of sections.
doc/lispref/functions.texi (Core Advising Primitives)
(Advising Named Functions, Porting old advices): Index subject of
sections.
doc/lispref/frames.texi (Creating Frames, Initial Parameters)
(Position Parameters, Buffer Parameters, Minibuffers and Frames)
(Pop-Up Menus, Drag and Drop): Index subject of sections.
doc/lispref/files.texi (Visiting Functions, Kinds of Files)
(Unique File Names): Index subject of sections.
doc/lispref/display.texi (Refresh Screen, Echo Area Customization)
(Warning Variables, Warning Options, Delayed Warnings)
(Temporary Displays, Managing Overlays, Overlay Properties)
(Finding Overlays, Size of Displayed Text, Defining Faces)
(Attribute Functions, Displaying Faces, Face Remapping)
(Basic Faces, Font Lookup, Fontsets, Replacing Specs)
(Defining Images, Showing Images): Index subject of sections.
doc/lispref/debugging.texi (Debugging, Explicit Debug)
(Invoking the Debugger, Excess Open, Excess Close): Index subject
of sections.
doc/lispref/customize.texi (Defining New Types, Applying Customizations)
(Custom Themes): Index subject of sections.
doc/lispref/control.texi (Sequencing, Combining Conditions)
(Processing of Errors, Cleanups): Index subject of sections.
doc/lispref/compile.texi (Eval During Compile): Index subject of sections.
doc/lispref/commands.texi (Using Interactive, Distinguish Interactive)
(Command Loop Info, Classifying Events, Event Mod)
(Invoking the Input Method): Index subject of sections.
doc/lispref/buffers.texi (Buffer List, Buffer Gap): Index subject of sections.
doc/lispref/backups.texi (Making Backups, Numbered Backups, Backup Names)
(Reverting): Index subject of sections.
doc/lispref/abbrevs.texi (Abbrev Tables, Defining Abbrevs, Abbrev Files)
(Abbrev Expansion, Standard Abbrev Tables, Abbrev Properties)
(Abbrev Table Properties): Index subject of sections.
doc/lispref/os.texi (Time of Day, Time Conversion, Time Parsing)
(Time Calculations, Idle Timers): Index subject of sections.
2014-12-23 18:42:30 +00:00
|
|
|
@cindex processing of errors
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
When an error is signaled, @code{signal} searches for an active
|
|
|
|
@dfn{handler} for the error. A handler is a sequence of Lisp
|
|
|
|
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} that
|
|
|
|
established it; all functions called within that @code{condition-case}
|
|
|
|
have already been exited, and the handler cannot return to them.
|
|
|
|
|
|
|
|
If there is no applicable handler for the error, it terminates the
|
|
|
|
current command and returns control to the editor command loop. (The
|
|
|
|
command loop has an implicit handler for all kinds of errors.) The
|
|
|
|
command loop's handler uses the error symbol and associated data to
|
|
|
|
print an error message. You can use the variable
|
|
|
|
@code{command-error-function} to control how this is done:
|
|
|
|
|
|
|
|
@defvar command-error-function
|
|
|
|
This variable, if non-@code{nil}, specifies a function to use to
|
|
|
|
handle errors that return control to the Emacs command loop. The
|
|
|
|
function should take three arguments: @var{data}, a list of the same
|
|
|
|
form that @code{condition-case} would bind to its variable;
|
|
|
|
@var{context}, a string describing the situation in which the error
|
|
|
|
occurred, or (more often) @code{nil}; and @var{caller}, the Lisp
|
|
|
|
function which called the primitive that signaled the error.
|
|
|
|
@end defvar
|
|
|
|
|
|
|
|
@cindex @code{debug-on-error} use
|
|
|
|
An error that has no explicit handler may call the Lisp debugger. The
|
|
|
|
debugger is enabled if the variable @code{debug-on-error} (@pxref{Error
|
|
|
|
Debugging}) is non-@code{nil}. Unlike error handlers, the debugger runs
|
|
|
|
in the environment of the error, so that you can examine values of
|
|
|
|
variables precisely as they were at the time of the error.
|
|
|
|
|
|
|
|
@node Handling Errors
|
|
|
|
@subsubsection Writing Code to Handle Errors
|
|
|
|
@cindex error handler
|
|
|
|
@cindex handling errors
|
|
|
|
|
|
|
|
The usual effect of signaling an error is to terminate the command
|
|
|
|
that is running and return immediately to the Emacs editor command loop.
|
|
|
|
You can arrange to trap errors occurring in a part of your program by
|
|
|
|
establishing an error handler, with the special form
|
|
|
|
@code{condition-case}. A simple example looks like this:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(condition-case nil
|
|
|
|
(delete-file filename)
|
|
|
|
(error nil))
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
@noindent
|
|
|
|
This deletes the file named @var{filename}, catching any error and
|
2012-02-06 00:48:02 +00:00
|
|
|
returning @code{nil} if an error occurs. (You can use the macro
|
|
|
|
@code{ignore-errors} for a simple case like this; see below.)
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
The second argument of @code{condition-case} is called the
|
|
|
|
@dfn{protected form}. (In the example above, the protected form is a
|
|
|
|
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 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.
|
|
|
|
|
|
|
|
The arguments after the protected form are handlers. Each handler
|
|
|
|
lists one or more @dfn{condition names} (which are symbols) to specify
|
|
|
|
which errors it will handle. The error symbol specified when an error
|
|
|
|
is signaled also defines a list of condition names. A handler applies
|
|
|
|
to an error if they have any condition names in common. In the example
|
|
|
|
above, there is one handler, and it specifies one condition name,
|
|
|
|
@code{error}, which covers all errors.
|
|
|
|
|
|
|
|
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 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.
|
|
|
|
|
|
|
|
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. You can also specify that a particular handler
|
|
|
|
should let the debugger run first, by writing @code{debug} among the
|
|
|
|
conditions, like this:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(condition-case nil
|
|
|
|
(delete-file filename)
|
|
|
|
((debug error) nil))
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
@noindent
|
|
|
|
The effect of @code{debug} here is only to prevent
|
|
|
|
@code{condition-case} from suppressing the call to the debugger. Any
|
|
|
|
given error will invoke the debugger only if @code{debug-on-error} and
|
|
|
|
the other usual filtering mechanisms say it should. @xref{Error Debugging}.
|
|
|
|
|
2012-02-10 15:59:29 +00:00
|
|
|
@defmac condition-case-unless-debug var protected-form handlers@dots{}
|
|
|
|
The macro @code{condition-case-unless-debug} provides another way to
|
2012-02-06 00:48:02 +00:00
|
|
|
handle debugging of such forms. It behaves exactly like
|
|
|
|
@code{condition-case}, unless the variable @code{debug-on-error} is
|
|
|
|
non-@code{nil}, in which case it does not handle any errors at all.
|
|
|
|
@end defmac
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
Once Emacs decides that a certain handler handles the error, it
|
|
|
|
returns control to that handler. To do so, Emacs unbinds all variable
|
|
|
|
bindings made by binding constructs that are being exited, and
|
|
|
|
executes the cleanups of all @code{unwind-protect} forms that are
|
|
|
|
being exited. Once control arrives at the handler, the body of the
|
|
|
|
handler executes normally.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
Error signaling and handling have some resemblance to @code{throw} and
|
|
|
|
@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
|
|
|
|
the execution of @var{protected-form}. If @var{protected-form} executes
|
|
|
|
without error, the value it returns becomes the value of the
|
|
|
|
@code{condition-case} form; in this case, the @code{condition-case} has
|
|
|
|
no effect. The @code{condition-case} form makes a difference when an
|
|
|
|
error occurs during @var{protected-form}.
|
|
|
|
|
|
|
|
Each of the @var{handlers} is a list of the form @code{(@var{conditions}
|
|
|
|
@var{body}@dots{})}. Here @var{conditions} is an error condition name
|
|
|
|
to be handled, or a list of condition names (which can include @code{debug}
|
|
|
|
to allow the debugger to run before the handler); @var{body} is one or more
|
|
|
|
Lisp expressions to be executed when this handler handles an error.
|
|
|
|
Here are examples of handlers:
|
|
|
|
|
More small edits for doc/lispref
* control.texi: Where possible, use example rather than smallexample.
(Sequencing, Conditionals, Signaling Errors, Handling Errors):
Tweak page breaks.
* customize.texi: Where possible, use example rather than smallexample.
(Common Keywords, Variable Definitions, Applying Customizations)
(Custom Themes): Tweak page breaks.
* eval.texi, functions.texi, loading.texi, macros.texi:
Where possible, use example rather than smallexample.
* sequences.texi (Arrays): Tweak page breaks.
* symbols.texi: Where possible, use example rather than smallexample.
(Symbol Components): Fix typo.
(Other Plists): Tweak page break.
2012-05-05 00:37:30 +00:00
|
|
|
@example
|
2007-09-06 04:25:08 +00:00
|
|
|
@group
|
|
|
|
(error nil)
|
|
|
|
|
|
|
|
(arith-error (message "Division by zero"))
|
|
|
|
|
|
|
|
((arith-error file-error)
|
|
|
|
(message
|
|
|
|
"Either division by zero or failure to open a file"))
|
|
|
|
@end group
|
More small edits for doc/lispref
* control.texi: Where possible, use example rather than smallexample.
(Sequencing, Conditionals, Signaling Errors, Handling Errors):
Tweak page breaks.
* customize.texi: Where possible, use example rather than smallexample.
(Common Keywords, Variable Definitions, Applying Customizations)
(Custom Themes): Tweak page breaks.
* eval.texi, functions.texi, loading.texi, macros.texi:
Where possible, use example rather than smallexample.
* sequences.texi (Arrays): Tweak page breaks.
* symbols.texi: Where possible, use example rather than smallexample.
(Symbol Components): Fix typo.
(Other Plists): Tweak page break.
2012-05-05 00:37:30 +00:00
|
|
|
@end example
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
Each error that occurs has an @dfn{error symbol} that describes what
|
* lisp/subr.el (define-error): New function.
* doc/lispref/control.texi (Signaling Errors): Refer to define-error.
(Error Symbols): Add `define-error'.
* doc/lispref/errors.texi (Standard Errors): Don't refer to `error-conditions'.
* lisp/progmodes/ada-xref.el (ada-error-file-not-found): Rename from
error-file-not-found and define with define-error.
* lisp/emacs-lisp/cl-lib.el (cl-assertion-failed): Move here from subr.el
and define with define-error.
* lisp/userlock.el (file-locked, file-supersession):
* lisp/simple.el (mark-inactive):
* lisp/progmodes/js.el (js-moz-bad-rpc, js-js-error):
* lisp/progmodes/ada-mode.el (ada-mode-errors):
* lisp/play/life.el (life-extinct):
* lisp/nxml/xsd-regexp.el (xsdre-invalid-regexp, xsdre-parse-error):
* lisp/nxml/xmltok.el (xmltok-markup-declaration-parse-error):
* lisp/nxml/rng-util.el (rng-error):
* lisp/nxml/rng-uri.el (rng-uri-error):
* lisp/nxml/rng-match.el (rng-compile-error):
* lisp/nxml/rng-cmpct.el (rng-c-incorrect-schema):
* lisp/nxml/nxml-util.el (nxml-error, nxml-file-parse-error):
* lisp/nxml/nxml-rap.el (nxml-scan-error):
* lisp/nxml/nxml-outln.el (nxml-outline-error):
* lisp/net/soap-client.el (soap-error):
* lisp/net/gnutls.el (gnutls-error):
* lisp/net/ange-ftp.el (ftp-error):
* lisp/mpc.el (mpc-proc-error):
* lisp/json.el (json-error, json-readtable-error, json-unknown-keyword)
(json-number-format, json-string-escape, json-string-format)
(json-key-format, json-object-format):
* lisp/jka-compr.el (compression-error):
* lisp/international/quail.el (quail-error):
* lisp/international/kkc.el (kkc-error):
* lisp/emacs-lisp/ert.el (ert-test-failed):
* lisp/calc/calc.el (calc-error, inexact-result, math-overflow)
(math-underflow):
* lisp/bookmark.el (bookmark-error-no-filename):
* lisp/epg.el (epg-error): Define with define-error.
2013-08-09 21:22:44 +00:00
|
|
|
kind of error it is, and which describes also a list of condition names
|
|
|
|
(@pxref{Error Symbols}). Emacs
|
2007-09-06 04:25:08 +00:00
|
|
|
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.
|
|
|
|
|
|
|
|
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 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.
|
2010-03-31 09:43:53 +00:00
|
|
|
|
|
|
|
@cindex rethrow a signal
|
|
|
|
Sometimes it is necessary to re-throw a signal caught by
|
|
|
|
@code{condition-case}, for some outer-level handler to catch. Here's
|
|
|
|
how to do that:
|
|
|
|
|
More small edits for doc/lispref
* control.texi: Where possible, use example rather than smallexample.
(Sequencing, Conditionals, Signaling Errors, Handling Errors):
Tweak page breaks.
* customize.texi: Where possible, use example rather than smallexample.
(Common Keywords, Variable Definitions, Applying Customizations)
(Custom Themes): Tweak page breaks.
* eval.texi, functions.texi, loading.texi, macros.texi:
Where possible, use example rather than smallexample.
* sequences.texi (Arrays): Tweak page breaks.
* symbols.texi: Where possible, use example rather than smallexample.
(Symbol Components): Fix typo.
(Other Plists): Tweak page break.
2012-05-05 00:37:30 +00:00
|
|
|
@example
|
2010-03-31 09:43:53 +00:00
|
|
|
(signal (car err) (cdr err))
|
More small edits for doc/lispref
* control.texi: Where possible, use example rather than smallexample.
(Sequencing, Conditionals, Signaling Errors, Handling Errors):
Tweak page breaks.
* customize.texi: Where possible, use example rather than smallexample.
(Common Keywords, Variable Definitions, Applying Customizations)
(Custom Themes): Tweak page breaks.
* eval.texi, functions.texi, loading.texi, macros.texi:
Where possible, use example rather than smallexample.
* sequences.texi (Arrays): Tweak page breaks.
* symbols.texi: Where possible, use example rather than smallexample.
(Symbol Components): Fix typo.
(Other Plists): Tweak page break.
2012-05-05 00:37:30 +00:00
|
|
|
@end example
|
2010-03-31 09:43:53 +00:00
|
|
|
|
|
|
|
@noindent
|
|
|
|
where @code{err} is the error description variable, the first argument
|
|
|
|
to @code{condition-case} whose error condition you want to re-throw.
|
|
|
|
@xref{Definition of signal}.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defspec
|
|
|
|
|
2010-08-02 10:35:34 +00:00
|
|
|
@defun error-message-string error-descriptor
|
2007-09-06 04:25:08 +00:00
|
|
|
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. @xref{Definition of signal}.
|
|
|
|
@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 displays the error
|
|
|
|
message (but without a beep), then returns a very large number.
|
|
|
|
|
More small edits for doc/lispref
* control.texi: Where possible, use example rather than smallexample.
(Sequencing, Conditionals, Signaling Errors, Handling Errors):
Tweak page breaks.
* customize.texi: Where possible, use example rather than smallexample.
(Common Keywords, Variable Definitions, Applying Customizations)
(Custom Themes): Tweak page breaks.
* eval.texi, functions.texi, loading.texi, macros.texi:
Where possible, use example rather than smallexample.
* sequences.texi (Arrays): Tweak page breaks.
* symbols.texi: Where possible, use example rather than smallexample.
(Symbol Components): Fix typo.
(Other Plists): Tweak page break.
2012-05-05 00:37:30 +00:00
|
|
|
@example
|
2007-09-06 04:25:08 +00:00
|
|
|
@group
|
|
|
|
(defun safe-divide (dividend divisor)
|
|
|
|
(condition-case err
|
|
|
|
;; @r{Protected form.}
|
|
|
|
(/ dividend divisor)
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
;; @r{The handler.}
|
|
|
|
(arith-error ; @r{Condition.}
|
|
|
|
;; @r{Display the usual message for this error.}
|
|
|
|
(message "%s" (error-message-string err))
|
|
|
|
1000000)))
|
|
|
|
@result{} safe-divide
|
|
|
|
@end group
|
|
|
|
|
|
|
|
@group
|
|
|
|
(safe-divide 5 0)
|
|
|
|
@print{} Arithmetic error: (arith-error)
|
|
|
|
@result{} 1000000
|
|
|
|
@end group
|
More small edits for doc/lispref
* control.texi: Where possible, use example rather than smallexample.
(Sequencing, Conditionals, Signaling Errors, Handling Errors):
Tweak page breaks.
* customize.texi: Where possible, use example rather than smallexample.
(Common Keywords, Variable Definitions, Applying Customizations)
(Custom Themes): Tweak page breaks.
* eval.texi, functions.texi, loading.texi, macros.texi:
Where possible, use example rather than smallexample.
* sequences.texi (Arrays): Tweak page breaks.
* symbols.texi: Where possible, use example rather than smallexample.
(Symbol Components): Fix typo.
(Other Plists): Tweak page break.
2012-05-05 00:37:30 +00:00
|
|
|
@end example
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@noindent
|
More small edits for doc/lispref
* control.texi: Where possible, use example rather than smallexample.
(Sequencing, Conditionals, Signaling Errors, Handling Errors):
Tweak page breaks.
* customize.texi: Where possible, use example rather than smallexample.
(Common Keywords, Variable Definitions, Applying Customizations)
(Custom Themes): Tweak page breaks.
* eval.texi, functions.texi, loading.texi, macros.texi:
Where possible, use example rather than smallexample.
* sequences.texi (Arrays): Tweak page breaks.
* symbols.texi: Where possible, use example rather than smallexample.
(Symbol Components): Fix typo.
(Other Plists): Tweak page break.
2012-05-05 00:37:30 +00:00
|
|
|
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 (by this @code{condition-case}). Thus:
|
2007-09-06 04:25:08 +00:00
|
|
|
|
More small edits for doc/lispref
* control.texi: Where possible, use example rather than smallexample.
(Sequencing, Conditionals, Signaling Errors, Handling Errors):
Tweak page breaks.
* customize.texi: Where possible, use example rather than smallexample.
(Common Keywords, Variable Definitions, Applying Customizations)
(Custom Themes): Tweak page breaks.
* eval.texi, functions.texi, loading.texi, macros.texi:
Where possible, use example rather than smallexample.
* sequences.texi (Arrays): Tweak page breaks.
* symbols.texi: Where possible, use example rather than smallexample.
(Symbol Components): Fix typo.
(Other Plists): Tweak page break.
2012-05-05 00:37:30 +00:00
|
|
|
@example
|
2007-09-06 04:25:08 +00:00
|
|
|
@group
|
|
|
|
(safe-divide nil 3)
|
|
|
|
@error{} Wrong type argument: number-or-marker-p, nil
|
|
|
|
@end group
|
More small edits for doc/lispref
* control.texi: Where possible, use example rather than smallexample.
(Sequencing, Conditionals, Signaling Errors, Handling Errors):
Tweak page breaks.
* customize.texi: Where possible, use example rather than smallexample.
(Common Keywords, Variable Definitions, Applying Customizations)
(Custom Themes): Tweak page breaks.
* eval.texi, functions.texi, loading.texi, macros.texi:
Where possible, use example rather than smallexample.
* sequences.texi (Arrays): Tweak page breaks.
* symbols.texi: Where possible, use example rather than smallexample.
(Symbol Components): Fix typo.
(Other Plists): Tweak page break.
2012-05-05 00:37:30 +00:00
|
|
|
@end example
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
Here is a @code{condition-case} that catches all kinds of errors,
|
More small edits for doc/lispref
* control.texi: Where possible, use example rather than smallexample.
(Sequencing, Conditionals, Signaling Errors, Handling Errors):
Tweak page breaks.
* customize.texi: Where possible, use example rather than smallexample.
(Common Keywords, Variable Definitions, Applying Customizations)
(Custom Themes): Tweak page breaks.
* eval.texi, functions.texi, loading.texi, macros.texi:
Where possible, use example rather than smallexample.
* sequences.texi (Arrays): Tweak page breaks.
* symbols.texi: Where possible, use example rather than smallexample.
(Symbol Components): Fix typo.
(Other Plists): Tweak page break.
2012-05-05 00:37:30 +00:00
|
|
|
including those from @code{error}:
|
2007-09-06 04:25:08 +00:00
|
|
|
|
More small edits for doc/lispref
* control.texi: Where possible, use example rather than smallexample.
(Sequencing, Conditionals, Signaling Errors, Handling Errors):
Tweak page breaks.
* customize.texi: Where possible, use example rather than smallexample.
(Common Keywords, Variable Definitions, Applying Customizations)
(Custom Themes): Tweak page breaks.
* eval.texi, functions.texi, loading.texi, macros.texi:
Where possible, use example rather than smallexample.
* sequences.texi (Arrays): Tweak page breaks.
* symbols.texi: Where possible, use example rather than smallexample.
(Symbol Components): Fix typo.
(Other Plists): Tweak page break.
2012-05-05 00:37:30 +00:00
|
|
|
@example
|
2007-09-06 04:25:08 +00:00
|
|
|
@group
|
|
|
|
(setq baz 34)
|
|
|
|
@result{} 34
|
|
|
|
@end group
|
|
|
|
|
|
|
|
@group
|
|
|
|
(condition-case err
|
|
|
|
(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))
|
|
|
|
;; @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")
|
|
|
|
@result{} 2
|
|
|
|
@end group
|
More small edits for doc/lispref
* control.texi: Where possible, use example rather than smallexample.
(Sequencing, Conditionals, Signaling Errors, Handling Errors):
Tweak page breaks.
* customize.texi: Where possible, use example rather than smallexample.
(Common Keywords, Variable Definitions, Applying Customizations)
(Custom Themes): Tweak page breaks.
* eval.texi, functions.texi, loading.texi, macros.texi:
Where possible, use example rather than smallexample.
* sequences.texi (Arrays): Tweak page breaks.
* symbols.texi: Where possible, use example rather than smallexample.
(Symbol Components): Fix typo.
(Other Plists): Tweak page break.
2012-05-05 00:37:30 +00:00
|
|
|
@end example
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2008-10-15 12:49:11 +00:00
|
|
|
@defmac ignore-errors body@dots{}
|
|
|
|
This construct executes @var{body}, ignoring any errors that occur
|
|
|
|
during its execution. If the execution is without error,
|
|
|
|
@code{ignore-errors} returns the value of the last form in @var{body};
|
|
|
|
otherwise, it returns @code{nil}.
|
|
|
|
|
|
|
|
Here's the example at the beginning of this subsection rewritten using
|
|
|
|
@code{ignore-errors}:
|
|
|
|
|
More small edits for doc/lispref
* control.texi: Where possible, use example rather than smallexample.
(Sequencing, Conditionals, Signaling Errors, Handling Errors):
Tweak page breaks.
* customize.texi: Where possible, use example rather than smallexample.
(Common Keywords, Variable Definitions, Applying Customizations)
(Custom Themes): Tweak page breaks.
* eval.texi, functions.texi, loading.texi, macros.texi:
Where possible, use example rather than smallexample.
* sequences.texi (Arrays): Tweak page breaks.
* symbols.texi: Where possible, use example rather than smallexample.
(Symbol Components): Fix typo.
(Other Plists): Tweak page break.
2012-05-05 00:37:30 +00:00
|
|
|
@example
|
2008-10-15 12:49:11 +00:00
|
|
|
@group
|
|
|
|
(ignore-errors
|
|
|
|
(delete-file filename))
|
|
|
|
@end group
|
More small edits for doc/lispref
* control.texi: Where possible, use example rather than smallexample.
(Sequencing, Conditionals, Signaling Errors, Handling Errors):
Tweak page breaks.
* customize.texi: Where possible, use example rather than smallexample.
(Common Keywords, Variable Definitions, Applying Customizations)
(Custom Themes): Tweak page breaks.
* eval.texi, functions.texi, loading.texi, macros.texi:
Where possible, use example rather than smallexample.
* sequences.texi (Arrays): Tweak page breaks.
* symbols.texi: Where possible, use example rather than smallexample.
(Symbol Components): Fix typo.
(Other Plists): Tweak page break.
2012-05-05 00:37:30 +00:00
|
|
|
@end example
|
2008-10-15 12:49:11 +00:00
|
|
|
@end defmac
|
|
|
|
|
2014-01-24 04:11:48 +00:00
|
|
|
@defmac with-demoted-errors format body@dots{}
|
2012-02-06 00:48:02 +00:00
|
|
|
This macro is like a milder version of @code{ignore-errors}. Rather
|
|
|
|
than suppressing errors altogether, it converts them into messages.
|
2014-01-24 04:11:48 +00:00
|
|
|
It uses the string @var{format} to format the message.
|
|
|
|
@var{format} should contain a single @samp{%}-sequence; e.g.,
|
|
|
|
@code{"Error: %S"}. Use @code{with-demoted-errors} around code
|
|
|
|
that is not expected to signal errors, but
|
2012-02-10 15:59:29 +00:00
|
|
|
should be robust if one does occur. Note that this macro uses
|
|
|
|
@code{condition-case-unless-debug} rather than @code{condition-case}.
|
2012-02-06 00:48:02 +00:00
|
|
|
@end defmac
|
2010-03-31 09:43:53 +00:00
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@node Error Symbols
|
|
|
|
@subsubsection Error Symbols and Condition Names
|
|
|
|
@cindex error symbol
|
|
|
|
@cindex error name
|
|
|
|
@cindex condition name
|
|
|
|
@cindex user-defined error
|
|
|
|
@kindex error-conditions
|
* lisp/subr.el (define-error): New function.
* doc/lispref/control.texi (Signaling Errors): Refer to define-error.
(Error Symbols): Add `define-error'.
* doc/lispref/errors.texi (Standard Errors): Don't refer to `error-conditions'.
* lisp/progmodes/ada-xref.el (ada-error-file-not-found): Rename from
error-file-not-found and define with define-error.
* lisp/emacs-lisp/cl-lib.el (cl-assertion-failed): Move here from subr.el
and define with define-error.
* lisp/userlock.el (file-locked, file-supersession):
* lisp/simple.el (mark-inactive):
* lisp/progmodes/js.el (js-moz-bad-rpc, js-js-error):
* lisp/progmodes/ada-mode.el (ada-mode-errors):
* lisp/play/life.el (life-extinct):
* lisp/nxml/xsd-regexp.el (xsdre-invalid-regexp, xsdre-parse-error):
* lisp/nxml/xmltok.el (xmltok-markup-declaration-parse-error):
* lisp/nxml/rng-util.el (rng-error):
* lisp/nxml/rng-uri.el (rng-uri-error):
* lisp/nxml/rng-match.el (rng-compile-error):
* lisp/nxml/rng-cmpct.el (rng-c-incorrect-schema):
* lisp/nxml/nxml-util.el (nxml-error, nxml-file-parse-error):
* lisp/nxml/nxml-rap.el (nxml-scan-error):
* lisp/nxml/nxml-outln.el (nxml-outline-error):
* lisp/net/soap-client.el (soap-error):
* lisp/net/gnutls.el (gnutls-error):
* lisp/net/ange-ftp.el (ftp-error):
* lisp/mpc.el (mpc-proc-error):
* lisp/json.el (json-error, json-readtable-error, json-unknown-keyword)
(json-number-format, json-string-escape, json-string-format)
(json-key-format, json-object-format):
* lisp/jka-compr.el (compression-error):
* lisp/international/quail.el (quail-error):
* lisp/international/kkc.el (kkc-error):
* lisp/emacs-lisp/ert.el (ert-test-failed):
* lisp/calc/calc.el (calc-error, inexact-result, math-overflow)
(math-underflow):
* lisp/bookmark.el (bookmark-error-no-filename):
* lisp/epg.el (epg-error): Define with define-error.
2013-08-09 21:22:44 +00:00
|
|
|
@kindex define-error
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
When you signal an error, you specify an @dfn{error symbol} to specify
|
|
|
|
the kind of error you have in mind. Each error has one and only one
|
|
|
|
error symbol to categorize it. This is the finest classification of
|
|
|
|
errors defined by the Emacs Lisp language.
|
|
|
|
|
|
|
|
These narrow classifications are grouped into a hierarchy of wider
|
|
|
|
classes called @dfn{error conditions}, identified by @dfn{condition
|
|
|
|
names}. The narrowest such classes belong to the error symbols
|
|
|
|
themselves: each error symbol is also a condition name. There are also
|
|
|
|
condition names for more extensive classes, up to the condition name
|
|
|
|
@code{error} which takes in all kinds of errors (but not @code{quit}).
|
|
|
|
Thus, each error has one or more condition names: @code{error}, the
|
|
|
|
error symbol if that is distinct from @code{error}, and perhaps some
|
|
|
|
intermediate classifications.
|
|
|
|
|
* lisp/subr.el (define-error): New function.
* doc/lispref/control.texi (Signaling Errors): Refer to define-error.
(Error Symbols): Add `define-error'.
* doc/lispref/errors.texi (Standard Errors): Don't refer to `error-conditions'.
* lisp/progmodes/ada-xref.el (ada-error-file-not-found): Rename from
error-file-not-found and define with define-error.
* lisp/emacs-lisp/cl-lib.el (cl-assertion-failed): Move here from subr.el
and define with define-error.
* lisp/userlock.el (file-locked, file-supersession):
* lisp/simple.el (mark-inactive):
* lisp/progmodes/js.el (js-moz-bad-rpc, js-js-error):
* lisp/progmodes/ada-mode.el (ada-mode-errors):
* lisp/play/life.el (life-extinct):
* lisp/nxml/xsd-regexp.el (xsdre-invalid-regexp, xsdre-parse-error):
* lisp/nxml/xmltok.el (xmltok-markup-declaration-parse-error):
* lisp/nxml/rng-util.el (rng-error):
* lisp/nxml/rng-uri.el (rng-uri-error):
* lisp/nxml/rng-match.el (rng-compile-error):
* lisp/nxml/rng-cmpct.el (rng-c-incorrect-schema):
* lisp/nxml/nxml-util.el (nxml-error, nxml-file-parse-error):
* lisp/nxml/nxml-rap.el (nxml-scan-error):
* lisp/nxml/nxml-outln.el (nxml-outline-error):
* lisp/net/soap-client.el (soap-error):
* lisp/net/gnutls.el (gnutls-error):
* lisp/net/ange-ftp.el (ftp-error):
* lisp/mpc.el (mpc-proc-error):
* lisp/json.el (json-error, json-readtable-error, json-unknown-keyword)
(json-number-format, json-string-escape, json-string-format)
(json-key-format, json-object-format):
* lisp/jka-compr.el (compression-error):
* lisp/international/quail.el (quail-error):
* lisp/international/kkc.el (kkc-error):
* lisp/emacs-lisp/ert.el (ert-test-failed):
* lisp/calc/calc.el (calc-error, inexact-result, math-overflow)
(math-underflow):
* lisp/bookmark.el (bookmark-error-no-filename):
* lisp/epg.el (epg-error): Define with define-error.
2013-08-09 21:22:44 +00:00
|
|
|
@defun define-error name message &optional parent
|
|
|
|
In order for a symbol to be an error symbol, it must be defined with
|
|
|
|
@code{define-error} which takes a parent condition (defaults to @code{error}).
|
|
|
|
This parent defines the conditions that this kind of error belongs to.
|
|
|
|
The transitive set of parents always includes the error symbol itself, and the
|
|
|
|
symbol @code{error}. Because quitting is not considered an error, the set of
|
|
|
|
parents of @code{quit} is just @code{(quit)}.
|
2013-08-09 22:34:05 +00:00
|
|
|
@end defun
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@cindex peculiar error
|
2013-08-09 22:34:05 +00:00
|
|
|
In addition to its parents, the error symbol has a @var{message} which
|
* lisp/subr.el (define-error): New function.
* doc/lispref/control.texi (Signaling Errors): Refer to define-error.
(Error Symbols): Add `define-error'.
* doc/lispref/errors.texi (Standard Errors): Don't refer to `error-conditions'.
* lisp/progmodes/ada-xref.el (ada-error-file-not-found): Rename from
error-file-not-found and define with define-error.
* lisp/emacs-lisp/cl-lib.el (cl-assertion-failed): Move here from subr.el
and define with define-error.
* lisp/userlock.el (file-locked, file-supersession):
* lisp/simple.el (mark-inactive):
* lisp/progmodes/js.el (js-moz-bad-rpc, js-js-error):
* lisp/progmodes/ada-mode.el (ada-mode-errors):
* lisp/play/life.el (life-extinct):
* lisp/nxml/xsd-regexp.el (xsdre-invalid-regexp, xsdre-parse-error):
* lisp/nxml/xmltok.el (xmltok-markup-declaration-parse-error):
* lisp/nxml/rng-util.el (rng-error):
* lisp/nxml/rng-uri.el (rng-uri-error):
* lisp/nxml/rng-match.el (rng-compile-error):
* lisp/nxml/rng-cmpct.el (rng-c-incorrect-schema):
* lisp/nxml/nxml-util.el (nxml-error, nxml-file-parse-error):
* lisp/nxml/nxml-rap.el (nxml-scan-error):
* lisp/nxml/nxml-outln.el (nxml-outline-error):
* lisp/net/soap-client.el (soap-error):
* lisp/net/gnutls.el (gnutls-error):
* lisp/net/ange-ftp.el (ftp-error):
* lisp/mpc.el (mpc-proc-error):
* lisp/json.el (json-error, json-readtable-error, json-unknown-keyword)
(json-number-format, json-string-escape, json-string-format)
(json-key-format, json-object-format):
* lisp/jka-compr.el (compression-error):
* lisp/international/quail.el (quail-error):
* lisp/international/kkc.el (kkc-error):
* lisp/emacs-lisp/ert.el (ert-test-failed):
* lisp/calc/calc.el (calc-error, inexact-result, math-overflow)
(math-underflow):
* lisp/bookmark.el (bookmark-error-no-filename):
* lisp/epg.el (epg-error): Define with define-error.
2013-08-09 21:22:44 +00:00
|
|
|
is a string to be printed when that error is signaled but not handled. If that
|
|
|
|
message is not valid, the error message @samp{peculiar error} is used.
|
|
|
|
@xref{Definition of signal}.
|
|
|
|
|
|
|
|
Internally, the set of parents is stored in the @code{error-conditions}
|
|
|
|
property of the error symbol and the message is stored in the
|
|
|
|
@code{error-message} property of the error symbol.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
Here is how we define a new error symbol, @code{new-error}:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
* lisp/subr.el (define-error): New function.
* doc/lispref/control.texi (Signaling Errors): Refer to define-error.
(Error Symbols): Add `define-error'.
* doc/lispref/errors.texi (Standard Errors): Don't refer to `error-conditions'.
* lisp/progmodes/ada-xref.el (ada-error-file-not-found): Rename from
error-file-not-found and define with define-error.
* lisp/emacs-lisp/cl-lib.el (cl-assertion-failed): Move here from subr.el
and define with define-error.
* lisp/userlock.el (file-locked, file-supersession):
* lisp/simple.el (mark-inactive):
* lisp/progmodes/js.el (js-moz-bad-rpc, js-js-error):
* lisp/progmodes/ada-mode.el (ada-mode-errors):
* lisp/play/life.el (life-extinct):
* lisp/nxml/xsd-regexp.el (xsdre-invalid-regexp, xsdre-parse-error):
* lisp/nxml/xmltok.el (xmltok-markup-declaration-parse-error):
* lisp/nxml/rng-util.el (rng-error):
* lisp/nxml/rng-uri.el (rng-uri-error):
* lisp/nxml/rng-match.el (rng-compile-error):
* lisp/nxml/rng-cmpct.el (rng-c-incorrect-schema):
* lisp/nxml/nxml-util.el (nxml-error, nxml-file-parse-error):
* lisp/nxml/nxml-rap.el (nxml-scan-error):
* lisp/nxml/nxml-outln.el (nxml-outline-error):
* lisp/net/soap-client.el (soap-error):
* lisp/net/gnutls.el (gnutls-error):
* lisp/net/ange-ftp.el (ftp-error):
* lisp/mpc.el (mpc-proc-error):
* lisp/json.el (json-error, json-readtable-error, json-unknown-keyword)
(json-number-format, json-string-escape, json-string-format)
(json-key-format, json-object-format):
* lisp/jka-compr.el (compression-error):
* lisp/international/quail.el (quail-error):
* lisp/international/kkc.el (kkc-error):
* lisp/emacs-lisp/ert.el (ert-test-failed):
* lisp/calc/calc.el (calc-error, inexact-result, math-overflow)
(math-underflow):
* lisp/bookmark.el (bookmark-error-no-filename):
* lisp/epg.el (epg-error): Define with define-error.
2013-08-09 21:22:44 +00:00
|
|
|
(define-error 'new-error "A new error" 'my-own-errors)
|
2007-09-06 04:25:08 +00:00
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
@noindent
|
* lisp/subr.el (define-error): New function.
* doc/lispref/control.texi (Signaling Errors): Refer to define-error.
(Error Symbols): Add `define-error'.
* doc/lispref/errors.texi (Standard Errors): Don't refer to `error-conditions'.
* lisp/progmodes/ada-xref.el (ada-error-file-not-found): Rename from
error-file-not-found and define with define-error.
* lisp/emacs-lisp/cl-lib.el (cl-assertion-failed): Move here from subr.el
and define with define-error.
* lisp/userlock.el (file-locked, file-supersession):
* lisp/simple.el (mark-inactive):
* lisp/progmodes/js.el (js-moz-bad-rpc, js-js-error):
* lisp/progmodes/ada-mode.el (ada-mode-errors):
* lisp/play/life.el (life-extinct):
* lisp/nxml/xsd-regexp.el (xsdre-invalid-regexp, xsdre-parse-error):
* lisp/nxml/xmltok.el (xmltok-markup-declaration-parse-error):
* lisp/nxml/rng-util.el (rng-error):
* lisp/nxml/rng-uri.el (rng-uri-error):
* lisp/nxml/rng-match.el (rng-compile-error):
* lisp/nxml/rng-cmpct.el (rng-c-incorrect-schema):
* lisp/nxml/nxml-util.el (nxml-error, nxml-file-parse-error):
* lisp/nxml/nxml-rap.el (nxml-scan-error):
* lisp/nxml/nxml-outln.el (nxml-outline-error):
* lisp/net/soap-client.el (soap-error):
* lisp/net/gnutls.el (gnutls-error):
* lisp/net/ange-ftp.el (ftp-error):
* lisp/mpc.el (mpc-proc-error):
* lisp/json.el (json-error, json-readtable-error, json-unknown-keyword)
(json-number-format, json-string-escape, json-string-format)
(json-key-format, json-object-format):
* lisp/jka-compr.el (compression-error):
* lisp/international/quail.el (quail-error):
* lisp/international/kkc.el (kkc-error):
* lisp/emacs-lisp/ert.el (ert-test-failed):
* lisp/calc/calc.el (calc-error, inexact-result, math-overflow)
(math-underflow):
* lisp/bookmark.el (bookmark-error-no-filename):
* lisp/epg.el (epg-error): Define with define-error.
2013-08-09 21:22:44 +00:00
|
|
|
This error has several condition names: @code{new-error}, the narrowest
|
2007-09-06 04:25:08 +00:00
|
|
|
classification; @code{my-own-errors}, which we imagine is a wider
|
* lisp/subr.el (define-error): New function.
* doc/lispref/control.texi (Signaling Errors): Refer to define-error.
(Error Symbols): Add `define-error'.
* doc/lispref/errors.texi (Standard Errors): Don't refer to `error-conditions'.
* lisp/progmodes/ada-xref.el (ada-error-file-not-found): Rename from
error-file-not-found and define with define-error.
* lisp/emacs-lisp/cl-lib.el (cl-assertion-failed): Move here from subr.el
and define with define-error.
* lisp/userlock.el (file-locked, file-supersession):
* lisp/simple.el (mark-inactive):
* lisp/progmodes/js.el (js-moz-bad-rpc, js-js-error):
* lisp/progmodes/ada-mode.el (ada-mode-errors):
* lisp/play/life.el (life-extinct):
* lisp/nxml/xsd-regexp.el (xsdre-invalid-regexp, xsdre-parse-error):
* lisp/nxml/xmltok.el (xmltok-markup-declaration-parse-error):
* lisp/nxml/rng-util.el (rng-error):
* lisp/nxml/rng-uri.el (rng-uri-error):
* lisp/nxml/rng-match.el (rng-compile-error):
* lisp/nxml/rng-cmpct.el (rng-c-incorrect-schema):
* lisp/nxml/nxml-util.el (nxml-error, nxml-file-parse-error):
* lisp/nxml/nxml-rap.el (nxml-scan-error):
* lisp/nxml/nxml-outln.el (nxml-outline-error):
* lisp/net/soap-client.el (soap-error):
* lisp/net/gnutls.el (gnutls-error):
* lisp/net/ange-ftp.el (ftp-error):
* lisp/mpc.el (mpc-proc-error):
* lisp/json.el (json-error, json-readtable-error, json-unknown-keyword)
(json-number-format, json-string-escape, json-string-format)
(json-key-format, json-object-format):
* lisp/jka-compr.el (compression-error):
* lisp/international/quail.el (quail-error):
* lisp/international/kkc.el (kkc-error):
* lisp/emacs-lisp/ert.el (ert-test-failed):
* lisp/calc/calc.el (calc-error, inexact-result, math-overflow)
(math-underflow):
* lisp/bookmark.el (bookmark-error-no-filename):
* lisp/epg.el (epg-error): Define with define-error.
2013-08-09 21:22:44 +00:00
|
|
|
classification; and all the conditions of @code{my-own-errors} which should
|
|
|
|
include @code{error}, which is the widest of all.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
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{Definition of signal}) in
|
|
|
|
your code can do this:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(signal 'new-error '(x y))
|
|
|
|
@error{} A new error: x, y
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
* lisp/subr.el (define-error): New function.
* doc/lispref/control.texi (Signaling Errors): Refer to define-error.
(Error Symbols): Add `define-error'.
* doc/lispref/errors.texi (Standard Errors): Don't refer to `error-conditions'.
* lisp/progmodes/ada-xref.el (ada-error-file-not-found): Rename from
error-file-not-found and define with define-error.
* lisp/emacs-lisp/cl-lib.el (cl-assertion-failed): Move here from subr.el
and define with define-error.
* lisp/userlock.el (file-locked, file-supersession):
* lisp/simple.el (mark-inactive):
* lisp/progmodes/js.el (js-moz-bad-rpc, js-js-error):
* lisp/progmodes/ada-mode.el (ada-mode-errors):
* lisp/play/life.el (life-extinct):
* lisp/nxml/xsd-regexp.el (xsdre-invalid-regexp, xsdre-parse-error):
* lisp/nxml/xmltok.el (xmltok-markup-declaration-parse-error):
* lisp/nxml/rng-util.el (rng-error):
* lisp/nxml/rng-uri.el (rng-uri-error):
* lisp/nxml/rng-match.el (rng-compile-error):
* lisp/nxml/rng-cmpct.el (rng-c-incorrect-schema):
* lisp/nxml/nxml-util.el (nxml-error, nxml-file-parse-error):
* lisp/nxml/nxml-rap.el (nxml-scan-error):
* lisp/nxml/nxml-outln.el (nxml-outline-error):
* lisp/net/soap-client.el (soap-error):
* lisp/net/gnutls.el (gnutls-error):
* lisp/net/ange-ftp.el (ftp-error):
* lisp/mpc.el (mpc-proc-error):
* lisp/json.el (json-error, json-readtable-error, json-unknown-keyword)
(json-number-format, json-string-escape, json-string-format)
(json-key-format, json-object-format):
* lisp/jka-compr.el (compression-error):
* lisp/international/quail.el (quail-error):
* lisp/international/kkc.el (kkc-error):
* lisp/emacs-lisp/ert.el (ert-test-failed):
* lisp/calc/calc.el (calc-error, inexact-result, math-overflow)
(math-underflow):
* lisp/bookmark.el (bookmark-error-no-filename):
* lisp/epg.el (epg-error): Define with define-error.
2013-08-09 21:22:44 +00:00
|
|
|
This error can be handled through any of its condition names.
|
2007-09-06 04:25:08 +00:00
|
|
|
This example handles @code{new-error} and any other errors in the class
|
|
|
|
@code{my-own-errors}:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(condition-case foo
|
|
|
|
(bar nil t)
|
|
|
|
(my-own-errors nil))
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
The significant way that errors are classified is by their condition
|
|
|
|
names---the names used to match errors with handlers. An error symbol
|
|
|
|
serves only as a convenient way to specify the intended error message
|
|
|
|
and list of condition names. It would be cumbersome to give
|
|
|
|
@code{signal} a list of condition names rather than one error symbol.
|
|
|
|
|
|
|
|
By contrast, using only error symbols without condition names would
|
|
|
|
seriously decrease the power of @code{condition-case}. Condition names
|
|
|
|
make it possible to categorize errors at various levels of generality
|
|
|
|
when you write an error handler. Using error symbols alone would
|
|
|
|
eliminate all but the narrowest level of classification.
|
|
|
|
|
2012-02-25 20:59:46 +00:00
|
|
|
@xref{Standard Errors}, for a list of the main error symbols
|
2007-09-06 04:25:08 +00:00
|
|
|
and their conditions.
|
|
|
|
|
|
|
|
@node Cleanups
|
|
|
|
@subsection Cleaning Up from Nonlocal Exits
|
Improve indexing on the chapter/section/subsection levels.
doc/lispref/windows.texi (Recombining Windows): Index subject of sections.
doc/lispref/variables.texi (Variables with Restricted Values)
(Generalized Variables): Index subject of sections.
doc/lispref/text.texi (Buffer Contents, Examining Properties)
(Changing Properties, Property Search, Substitution): Index
subject of sections.
doc/lispref/syntax.texi (Motion and Syntax, Parsing Expressions)
(Motion via Parsing, Position Parse, Control Parsing): Index
subject of sections.
doc/lispref/strings.texi (Predicates for Strings, Creating Strings)
(Modifying Strings, Text Comparison): Index subject of sections.
doc/lispref/searching.texi (Syntax of Regexps, Regexp Special)
(Regexp Functions, Regexp Functions): Index subject of sections.
doc/lispref/processes.texi (Subprocess Creation, Process Information): Index
subject of sections.
doc/lispref/positions.texi (Screen Lines): Index subject of sections.
doc/lispref/nonascii.texi (Scanning Charsets, Specifying Coding Systems):
Index subject of sections.
doc/lispref/minibuf.texi (Text from Minibuffer, Object from Minibuffer)
(Multiple Queries, Minibuffer Contents): Index subject of
sections.
doc/lispref/markers.texi (Predicates on Markers, Creating Markers)
(Information from Markers, Moving Markers): Index subject of
sections.
doc/lispref/macros.texi (Defining Macros, Problems with Macros): Index
subject of sections.
doc/lispref/loading.texi (Loading Non-ASCII, Where Defined): Index subject
of sections.
doc/lispref/lists.texi (List-related Predicates, List Variables, Setcar)
(Setcdr, Plist Access): Index subject of sections.
doc/lispref/keymaps.texi (Controlling Active Maps, Scanning Keymaps)
(Modifying Menus): Index subject of sections.
doc/lispref/help.texi (Accessing Documentation, Help Functions): Index
subject of sections.
doc/lispref/hash.texi (Hash Access): Index subject of sections.
doc/lispref/functions.texi (Core Advising Primitives)
(Advising Named Functions, Porting old advices): Index subject of
sections.
doc/lispref/frames.texi (Creating Frames, Initial Parameters)
(Position Parameters, Buffer Parameters, Minibuffers and Frames)
(Pop-Up Menus, Drag and Drop): Index subject of sections.
doc/lispref/files.texi (Visiting Functions, Kinds of Files)
(Unique File Names): Index subject of sections.
doc/lispref/display.texi (Refresh Screen, Echo Area Customization)
(Warning Variables, Warning Options, Delayed Warnings)
(Temporary Displays, Managing Overlays, Overlay Properties)
(Finding Overlays, Size of Displayed Text, Defining Faces)
(Attribute Functions, Displaying Faces, Face Remapping)
(Basic Faces, Font Lookup, Fontsets, Replacing Specs)
(Defining Images, Showing Images): Index subject of sections.
doc/lispref/debugging.texi (Debugging, Explicit Debug)
(Invoking the Debugger, Excess Open, Excess Close): Index subject
of sections.
doc/lispref/customize.texi (Defining New Types, Applying Customizations)
(Custom Themes): Index subject of sections.
doc/lispref/control.texi (Sequencing, Combining Conditions)
(Processing of Errors, Cleanups): Index subject of sections.
doc/lispref/compile.texi (Eval During Compile): Index subject of sections.
doc/lispref/commands.texi (Using Interactive, Distinguish Interactive)
(Command Loop Info, Classifying Events, Event Mod)
(Invoking the Input Method): Index subject of sections.
doc/lispref/buffers.texi (Buffer List, Buffer Gap): Index subject of sections.
doc/lispref/backups.texi (Making Backups, Numbered Backups, Backup Names)
(Reverting): Index subject of sections.
doc/lispref/abbrevs.texi (Abbrev Tables, Defining Abbrevs, Abbrev Files)
(Abbrev Expansion, Standard Abbrev Tables, Abbrev Properties)
(Abbrev Table Properties): Index subject of sections.
doc/lispref/os.texi (Time of Day, Time Conversion, Time Parsing)
(Time Calculations, Idle Timers): Index subject of sections.
2014-12-23 18:42:30 +00:00
|
|
|
@cindex nonlocal exits, cleaning up
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
The @code{unwind-protect} construct is essential whenever you
|
|
|
|
temporarily put a data structure in an inconsistent state; it permits
|
|
|
|
you to make the data consistent again in the event of an error or
|
|
|
|
throw. (Another more specific cleanup construct that is used only for
|
|
|
|
changes in buffer contents is the atomic change group; @ref{Atomic
|
|
|
|
Changes}.)
|
|
|
|
|
|
|
|
@defspec unwind-protect body-form cleanup-forms@dots{}
|
|
|
|
@cindex cleanup forms
|
|
|
|
@cindex protected forms
|
|
|
|
@cindex error cleanup
|
|
|
|
@cindex unwinding
|
|
|
|
@code{unwind-protect} executes @var{body-form} with a guarantee that
|
|
|
|
the @var{cleanup-forms} will be evaluated if control leaves
|
|
|
|
@var{body-form}, no matter how that happens. @var{body-form} may
|
|
|
|
complete normally, or execute a @code{throw} out of the
|
|
|
|
@code{unwind-protect}, or cause an error; in all cases, the
|
|
|
|
@var{cleanup-forms} will be evaluated.
|
|
|
|
|
|
|
|
If @var{body-form} finishes normally, @code{unwind-protect} returns the
|
|
|
|
value of @var{body-form}, after it evaluates the @var{cleanup-forms}.
|
|
|
|
If @var{body-form} does not finish, @code{unwind-protect} does not
|
|
|
|
return any value in the normal sense.
|
|
|
|
|
|
|
|
Only @var{body-form} 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.
|
|
|
|
|
|
|
|
The number of currently active @code{unwind-protect} forms counts,
|
|
|
|
together with the number of local variable bindings, against the limit
|
|
|
|
@code{max-specpdl-size} (@pxref{Definition of max-specpdl-size,, Local
|
|
|
|
Variables}).
|
|
|
|
@end defspec
|
|
|
|
|
|
|
|
For example, here we make an invisible buffer for temporary use, and
|
|
|
|
make sure to kill it before finishing:
|
|
|
|
|
More small edits for doc/lispref
* control.texi: Where possible, use example rather than smallexample.
(Sequencing, Conditionals, Signaling Errors, Handling Errors):
Tweak page breaks.
* customize.texi: Where possible, use example rather than smallexample.
(Common Keywords, Variable Definitions, Applying Customizations)
(Custom Themes): Tweak page breaks.
* eval.texi, functions.texi, loading.texi, macros.texi:
Where possible, use example rather than smallexample.
* sequences.texi (Arrays): Tweak page breaks.
* symbols.texi: Where possible, use example rather than smallexample.
(Symbol Components): Fix typo.
(Other Plists): Tweak page break.
2012-05-05 00:37:30 +00:00
|
|
|
@example
|
2007-09-06 04:25:08 +00:00
|
|
|
@group
|
2010-01-04 18:18:38 +00:00
|
|
|
(let ((buffer (get-buffer-create " *temp*")))
|
|
|
|
(with-current-buffer buffer
|
2007-09-06 04:25:08 +00:00
|
|
|
(unwind-protect
|
|
|
|
@var{body-form}
|
|
|
|
(kill-buffer buffer))))
|
|
|
|
@end group
|
More small edits for doc/lispref
* control.texi: Where possible, use example rather than smallexample.
(Sequencing, Conditionals, Signaling Errors, Handling Errors):
Tweak page breaks.
* customize.texi: Where possible, use example rather than smallexample.
(Common Keywords, Variable Definitions, Applying Customizations)
(Custom Themes): Tweak page breaks.
* eval.texi, functions.texi, loading.texi, macros.texi:
Where possible, use example rather than smallexample.
* sequences.texi (Arrays): Tweak page breaks.
* symbols.texi: Where possible, use example rather than smallexample.
(Symbol Components): Fix typo.
(Other Plists): Tweak page break.
2012-05-05 00:37:30 +00:00
|
|
|
@end example
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@noindent
|
|
|
|
You might think that we could just as well write @code{(kill-buffer
|
|
|
|
(current-buffer))} and dispense with the variable @code{buffer}.
|
|
|
|
However, the way shown above is safer, if @var{body-form} happens to
|
|
|
|
get an error after switching to a different buffer! (Alternatively,
|
2010-01-04 18:18:38 +00:00
|
|
|
you could write a @code{save-current-buffer} around @var{body-form},
|
2007-09-06 04:25:08 +00:00
|
|
|
to ensure that the 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{Definition of
|
|
|
|
with-temp-buffer,, 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 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 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.
|
|
|
|
|
More small edits for doc/lispref
* control.texi: Where possible, use example rather than smallexample.
(Sequencing, Conditionals, Signaling Errors, Handling Errors):
Tweak page breaks.
* customize.texi: Where possible, use example rather than smallexample.
(Common Keywords, Variable Definitions, Applying Customizations)
(Custom Themes): Tweak page breaks.
* eval.texi, functions.texi, loading.texi, macros.texi:
Where possible, use example rather than smallexample.
* sequences.texi (Arrays): Tweak page breaks.
* symbols.texi: Where possible, use example rather than smallexample.
(Symbol Components): Fix typo.
(Other Plists): Tweak page break.
2012-05-05 00:37:30 +00:00
|
|
|
@example
|
2007-09-06 04:25:08 +00:00
|
|
|
@group
|
|
|
|
(let ((win nil))
|
|
|
|
(unwind-protect
|
|
|
|
(progn
|
|
|
|
(setq process (ftp-setup-buffer host file))
|
|
|
|
(if (setq win (ftp-login process host user password))
|
|
|
|
(message "Logged in")
|
|
|
|
(error "Ftp login failed")))
|
|
|
|
(or win (and process (delete-process process)))))
|
|
|
|
@end group
|
More small edits for doc/lispref
* control.texi: Where possible, use example rather than smallexample.
(Sequencing, Conditionals, Signaling Errors, Handling Errors):
Tweak page breaks.
* customize.texi: Where possible, use example rather than smallexample.
(Common Keywords, Variable Definitions, Applying Customizations)
(Custom Themes): Tweak page breaks.
* eval.texi, functions.texi, loading.texi, macros.texi:
Where possible, use example rather than smallexample.
* sequences.texi (Arrays): Tweak page breaks.
* symbols.texi: Where possible, use example rather than smallexample.
(Symbol Components): Fix typo.
(Other Plists): Tweak page break.
2012-05-05 00:37:30 +00:00
|
|
|
@end example
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
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.
|