Mercurial > emacs
comparison lispref/control.texi @ 25751:467b88fab665
*** empty log message ***
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Fri, 17 Sep 1999 06:59:04 +0000 |
parents | 309fe4eb6522 |
children | f6b52258db6a |
comparison
equal
deleted
inserted
replaced
25750:f1968a807f56 | 25751:467b88fab665 |
---|---|
18 @var{a}, then form @var{b}, and so on. This is what happens when you | 18 @var{a}, then form @var{b}, and so on. This is what happens when you |
19 write several forms in succession in the body of a function, or at top | 19 write several forms in succession in the body of a function, or at top |
20 level in a file of Lisp code---the forms are executed in the order | 20 level in a file of Lisp code---the forms are executed in the order |
21 written. We call this @dfn{textual order}. For example, if a function | 21 written. We call this @dfn{textual order}. For example, if a function |
22 body consists of two forms @var{a} and @var{b}, evaluation of the | 22 body consists of two forms @var{a} and @var{b}, evaluation of the |
23 function evaluates first @var{a} and then @var{b}, and the function's | 23 function evaluates first @var{a} and then @var{b}. The result of |
24 value is the value of @var{b}. | 24 evaluating @var{b} becomes the value of the function. |
25 | 25 |
26 Explicit control structures make possible an order of execution other | 26 Explicit control structures make possible an order of execution other |
27 than sequential. | 27 than sequential. |
28 | 28 |
29 Emacs Lisp provides several kinds of control structure, including | 29 Emacs Lisp provides several kinds of control structure, including |
58 @end group | 58 @end group |
59 @end example | 59 @end example |
60 | 60 |
61 @noindent | 61 @noindent |
62 and it says to execute the forms @var{a}, @var{b}, @var{c}, and so on, in | 62 and it says to execute the forms @var{a}, @var{b}, @var{c}, and so on, in |
63 that order. These forms are called the body of the @code{progn} form. | 63 that order. These forms are called the @dfn{body} of the @code{progn} form. |
64 The value of the last form in the body becomes the value of the entire | 64 The value of the last form in the body becomes the value of the entire |
65 @code{progn}. | 65 @code{progn}. @code{(progn)} returns @code{nil}. |
66 | 66 |
67 @cindex implicit @code{progn} | 67 @cindex implicit @code{progn} |
68 In the early days of Lisp, @code{progn} was the only way to execute | 68 In the early days of Lisp, @code{progn} was the only way to execute |
69 two or more forms in succession and use the value of the last of them. | 69 two or more forms in succession and use the value of the last of them. |
70 But programmers found they often needed to use a @code{progn} in the | 70 But programmers found they often needed to use a @code{progn} in the |
71 body of a function, where (at that time) only one form was allowed. So | 71 body of a function, where (at that time) only one form was allowed. So |
72 the body of a function was made into an ``implicit @code{progn}'': | 72 the body of a function was made into an ``implicit @code{progn}'': |
73 several forms are allowed just as in the body of an actual @code{progn}. | 73 several forms are allowed just as in the body of an actual @code{progn}. |
74 Many other control structures likewise contain an implicit @code{progn}. | 74 Many other control structures likewise contain an implicit @code{progn}. |
75 As a result, @code{progn} is not used as often as it used to be. It is | 75 As a result, @code{progn} is not used as much as it was many years ago. |
76 needed now most often inside an @code{unwind-protect}, @code{and}, | 76 It is needed now most often inside an @code{unwind-protect}, @code{and}, |
77 @code{or}, or in the @var{then}-part of an @code{if}. | 77 @code{or}, or in the @var{then}-part of an @code{if}. |
78 | 78 |
79 @defspec progn forms@dots{} | 79 @defspec progn forms@dots{} |
80 This special form evaluates all of the @var{forms}, in textual | 80 This special form evaluates all of the @var{forms}, in textual |
81 order, returning the result of the final form. | 81 order, returning the result of the final form. |
263 | 263 |
264 For example, | 264 For example, |
265 | 265 |
266 @example | 266 @example |
267 @group | 267 @group |
268 (setq a 5) | |
268 (cond ((eq a 'hack) 'foo) | 269 (cond ((eq a 'hack) 'foo) |
269 (t "default")) | 270 (t "default")) |
270 @result{} "default" | 271 @result{} "default" |
271 @end group | 272 @end group |
272 @end example | 273 @end example |
273 | 274 |
274 @noindent | 275 @noindent |
275 This expression is a @code{cond} which returns @code{foo} if the value | 276 This @code{cond} expression returns @code{foo} if the value of @code{a} |
276 of @code{a} is @code{hack}, and returns the string @code{"default"} otherwise. | 277 is @code{hack}, and returns the string @code{"default"} otherwise. |
277 @end defspec | 278 @end defspec |
278 | 279 |
279 Any conditional construct can be expressed with @code{cond} or with | 280 Any conditional construct can be expressed with @code{cond} or with |
280 @code{if}. Therefore, the choice between them is a matter of style. | 281 @code{if}. Therefore, the choice between them is a matter of style. |
281 For example: | 282 For example: |
308 true. It works by evaluating the @var{conditions} one by one in the | 309 true. It works by evaluating the @var{conditions} one by one in the |
309 order written. | 310 order written. |
310 | 311 |
311 If any of the @var{conditions} evaluates to @code{nil}, then the result | 312 If any of the @var{conditions} evaluates to @code{nil}, then the result |
312 of the @code{and} must be @code{nil} regardless of the remaining | 313 of the @code{and} must be @code{nil} regardless of the remaining |
313 @var{conditions}; so @code{and} returns right away, ignoring the | 314 @var{conditions}; so @code{and} returns @code{nil} right away, ignoring |
314 remaining @var{conditions}. | 315 the remaining @var{conditions}. |
315 | 316 |
316 If all the @var{conditions} turn out non-@code{nil}, then the value of | 317 If all the @var{conditions} turn out non-@code{nil}, then the value of |
317 the last of them becomes the value of the @code{and} form. | 318 the last of them becomes the value of the @code{and} form. Just |
319 @code{(and)}, with no @var{conditions}, returns @code{t}, appropriate | |
320 because all the @var{conditions} turned out non-@code{nil}. (Think | |
321 about it; which one did not?) | |
318 | 322 |
319 Here is an example. The first condition returns the integer 1, which is | 323 Here is an example. The first condition returns the integer 1, which is |
320 not @code{nil}. Similarly, the second condition returns the integer 2, | 324 not @code{nil}. Similarly, the second condition returns the integer 2, |
321 which is not @code{nil}. The third condition is @code{nil}, so the | 325 which is not @code{nil}. The third condition is @code{nil}, so the |
322 remaining condition is never evaluated. | 326 remaining condition is never evaluated. |
366 the result of the @code{or} must be non-@code{nil}; so @code{or} returns | 370 the result of the @code{or} must be non-@code{nil}; so @code{or} returns |
367 right away, ignoring the remaining @var{conditions}. The value it | 371 right away, ignoring the remaining @var{conditions}. The value it |
368 returns is the non-@code{nil} value of the condition just evaluated. | 372 returns is the non-@code{nil} value of the condition just evaluated. |
369 | 373 |
370 If all the @var{conditions} turn out @code{nil}, then the @code{or} | 374 If all the @var{conditions} turn out @code{nil}, then the @code{or} |
371 expression returns @code{nil}. | 375 expression returns @code{nil}. Just @code{(or)}, with no |
372 | 376 @var{conditions}, returns @code{nil}, appropriate because all the |
373 For example, this expression tests whether @code{x} is either 0 or | 377 @var{conditions} turned out @code{nil}. (Think about it; which one |
374 @code{nil}: | 378 did not?) |
379 | |
380 For example, this expression tests whether @code{x} is either | |
381 @code{nil} or the integer zero: | |
375 | 382 |
376 @example | 383 @example |
377 (or (eq x nil) (eq x 0)) | 384 (or (eq x nil) (eq x 0)) |
378 @end example | 385 @end example |
379 | 386 |
444 @print{} Iteration 3. | 451 @print{} Iteration 3. |
445 @result{} nil | 452 @result{} nil |
446 @end group | 453 @end group |
447 @end example | 454 @end example |
448 | 455 |
449 If you would like to execute something on each iteration before the | 456 To write a ``repeat...until'' loop, which will execute something on each |
450 end-test, put it together with the end-test in a @code{progn} as the | 457 iteration and then do the end-test, put the body followed by the |
451 first argument of @code{while}, as shown here: | 458 end-test in a @code{progn} as the first argument of @code{while}, as |
459 shown here: | |
452 | 460 |
453 @example | 461 @example |
454 @group | 462 @group |
455 (while (progn | 463 (while (progn |
456 (forward-line 1) | 464 (forward-line 1) |
558 With the return point in effect, @code{catch} evaluates the forms of the | 566 With the return point in effect, @code{catch} evaluates the forms of the |
559 @var{body} in textual order. If the forms execute normally (without | 567 @var{body} in textual order. If the forms execute normally (without |
560 error or nonlocal exit) the value of the last body form is returned from | 568 error or nonlocal exit) the value of the last body form is returned from |
561 the @code{catch}. | 569 the @code{catch}. |
562 | 570 |
563 If a @code{throw} is done within @var{body} specifying the same value | 571 If a @code{throw} is executed during the execution of @var{body}, |
564 @var{tag}, the @code{catch} exits immediately; the value it returns is | 572 specifying the same value @var{tag}, the @code{catch} form exits |
565 whatever was specified as the second argument of @code{throw}. | 573 immediately; the value it returns is whatever was specified as the |
574 second argument of @code{throw}. | |
566 @end defspec | 575 @end defspec |
567 | 576 |
568 @defun throw tag value | 577 @defun throw tag value |
569 The purpose of @code{throw} is to return from a return point previously | 578 The purpose of @code{throw} is to return from a return point previously |
570 established with @code{catch}. The argument @var{tag} is used to choose | 579 established with @code{catch}. The argument @var{tag} is used to choose |
639 | 648 |
640 Now let's change the argument given to @code{catch2}: | 649 Now let's change the argument given to @code{catch2}: |
641 | 650 |
642 @example | 651 @example |
643 @group | 652 @group |
644 (defun catch2 (tag) | |
645 (catch tag | |
646 (throw 'hack 'yes))) | |
647 @result{} catch2 | |
648 @end group | |
649 | |
650 @group | |
651 (catch 'hack | 653 (catch 'hack |
652 (print (catch2 'quux)) | 654 (print (catch2 'quux)) |
653 'no) | 655 'no) |
654 @result{} yes | 656 @result{} yes |
655 @end group | 657 @end group |
706 @code{error} and @code{signal}. | 708 @code{error} and @code{signal}. |
707 | 709 |
708 Quitting, which happens when the user types @kbd{C-g}, is not | 710 Quitting, which happens when the user types @kbd{C-g}, is not |
709 considered an error, but it is handled almost like an error. | 711 considered an error, but it is handled almost like an error. |
710 @xref{Quitting}. | 712 @xref{Quitting}. |
713 | |
714 The error message should state what is wrong (``File does not | |
715 exist''), not how things ought to be (``File must exist''). The | |
716 convention in Emacs Lisp is that error messages should start with a | |
717 capital letter, but should not end with any sort of punctuation. | |
711 | 718 |
712 @defun error format-string &rest args | 719 @defun error format-string &rest args |
713 This function signals an error with an error message constructed by | 720 This function signals an error with an error message constructed by |
714 applying @code{format} (@pxref{String Conversion}) to | 721 applying @code{format} (@pxref{String Conversion}) to |
715 @var{format-string} and @var{args}. | 722 @var{format-string} and @var{args}. |
850 @code{error}, which covers all errors. | 857 @code{error}, which covers all errors. |
851 | 858 |
852 The search for an applicable handler checks all the established handlers | 859 The search for an applicable handler checks all the established handlers |
853 starting with the most recently established one. Thus, if two nested | 860 starting with the most recently established one. Thus, if two nested |
854 @code{condition-case} forms offer to handle the same error, the inner of | 861 @code{condition-case} forms offer to handle the same error, the inner of |
855 the two will actually handle it. | 862 the two gets to handle it. |
856 | 863 |
857 If an error is handled by some @code{condition-case} form, this | 864 If an error is handled by some @code{condition-case} form, this |
858 ordinarily prevents the debugger from being run, even if | 865 ordinarily prevents the debugger from being run, even if |
859 @code{debug-on-error} says this error should invoke the debugger. | 866 @code{debug-on-error} says this error should invoke the debugger. |
860 @xref{Error Debugging}. If you want to be able to debug errors that are | 867 @xref{Error Debugging}. If you want to be able to debug errors that are |
879 @code{insert-file-contents}. It is also used to trap errors that are | 886 @code{insert-file-contents}. It is also used to trap errors that are |
880 totally unpredictable, such as when the program evaluates an expression | 887 totally unpredictable, such as when the program evaluates an expression |
881 read from the user. | 888 read from the user. |
882 | 889 |
883 Error signaling and handling have some resemblance to @code{throw} and | 890 Error signaling and handling have some resemblance to @code{throw} and |
884 @code{catch}, but they are entirely separate facilities. An error | 891 @code{catch} (@pxref{Catch and Throw}), but they are entirely separate |
885 cannot be caught by a @code{catch}, and a @code{throw} cannot be handled | 892 facilities. An error cannot be caught by a @code{catch}, and a |
886 by an error handler (though using @code{throw} when there is no suitable | 893 @code{throw} cannot be handled by an error handler (though using |
887 @code{catch} signals an error that can be handled). | 894 @code{throw} when there is no suitable @code{catch} signals an error |
895 that can be handled). | |
888 | 896 |
889 @defspec condition-case var protected-form handlers@dots{} | 897 @defspec condition-case var protected-form handlers@dots{} |
890 This special form establishes the error handlers @var{handlers} around | 898 This special form establishes the error handlers @var{handlers} around |
891 the execution of @var{protected-form}. If @var{protected-form} executes | 899 the execution of @var{protected-form}. If @var{protected-form} executes |
892 without error, the value it returns becomes the value of the | 900 without error, the value it returns becomes the value of the |
1125 If the @var{body} forms finish normally, @code{unwind-protect} returns | 1133 If the @var{body} forms finish normally, @code{unwind-protect} returns |
1126 the value of the last @var{body} form, after it evaluates the | 1134 the value of the last @var{body} form, after it evaluates the |
1127 @var{cleanup-forms}. If the @var{body} forms do not finish, | 1135 @var{cleanup-forms}. If the @var{body} forms do not finish, |
1128 @code{unwind-protect} does not return any value in the normal sense. | 1136 @code{unwind-protect} does not return any value in the normal sense. |
1129 | 1137 |
1130 Only the @var{body} is actually protected by the @code{unwind-protect}. | 1138 Only the @var{body} is protected by the @code{unwind-protect}. If any |
1131 If any of the @var{cleanup-forms} themselves exits nonlocally (e.g., via | 1139 of the @var{cleanup-forms} themselves exits nonlocally (via a |
1132 a @code{throw} or an error), @code{unwind-protect} is @emph{not} | 1140 @code{throw} or an error), @code{unwind-protect} is @emph{not} |
1133 guaranteed to evaluate the rest of them. If the failure of one of the | 1141 guaranteed to evaluate the rest of them. If the failure of one of the |
1134 @var{cleanup-forms} has the potential to cause trouble, then protect it | 1142 @var{cleanup-forms} has the potential to cause trouble, then protect it |
1135 with another @code{unwind-protect} around that form. | 1143 with another @code{unwind-protect} around that form. |
1136 | 1144 |
1137 The number of currently active @code{unwind-protect} forms counts, | 1145 The number of currently active @code{unwind-protect} forms counts, |
1165 expands into more or less the code shown above (@pxref{Current Buffer}). | 1173 expands into more or less the code shown above (@pxref{Current Buffer}). |
1166 Several of the macros defined in this manual use @code{unwind-protect} | 1174 Several of the macros defined in this manual use @code{unwind-protect} |
1167 in this way. | 1175 in this way. |
1168 | 1176 |
1169 @findex ftp-login | 1177 @findex ftp-login |
1170 Here is an actual example taken from the file @file{ftp.el}. It | 1178 Here is an actual example derived from an FTP package. It creates a |
1171 creates a process (@pxref{Processes}) to try to establish a connection | 1179 process (@pxref{Processes}) to try to establish a connection to a remote |
1172 to a remote machine. As the function @code{ftp-login} is highly | 1180 machine. As the function @code{ftp-login} is highly susceptible to |
1173 susceptible to numerous problems that the writer of the function cannot | 1181 numerous problems that the writer of the function cannot anticipate, it |
1174 anticipate, it is protected with a form that guarantees deletion of the | 1182 is protected with a form that guarantees deletion of the process in the |
1175 process in the event of failure. Otherwise, Emacs might fill up with | 1183 event of failure. Otherwise, Emacs might fill up with useless |
1176 useless subprocesses. | 1184 subprocesses. |
1177 | 1185 |
1178 @smallexample | 1186 @smallexample |
1179 @group | 1187 @group |
1180 (let ((win nil)) | 1188 (let ((win nil)) |
1181 (unwind-protect | 1189 (unwind-protect |
1186 (error "Ftp login failed"))) | 1194 (error "Ftp login failed"))) |
1187 (or win (and process (delete-process process))))) | 1195 (or win (and process (delete-process process))))) |
1188 @end group | 1196 @end group |
1189 @end smallexample | 1197 @end smallexample |
1190 | 1198 |
1191 This example actually has a small bug: if the user types @kbd{C-g} to | 1199 This example has a small bug: if the user types @kbd{C-g} to |
1192 quit, and the quit happens immediately after the function | 1200 quit, and the quit happens immediately after the function |
1193 @code{ftp-setup-buffer} returns but before the variable @code{process} is | 1201 @code{ftp-setup-buffer} returns but before the variable @code{process} is |
1194 set, the process will not be killed. There is no easy way to fix this bug, | 1202 set, the process will not be killed. There is no easy way to fix this bug, |
1195 but at least it is very unlikely. | 1203 but at least it is very unlikely. |