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