home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / unix / info / elisp.i06 < prev    next >
Encoding:
GNU Info File  |  1993-06-14  |  49.6 KB  |  1,175 lines

  1. This is Info file elisp, produced by Makeinfo-1.47 from the input file
  2. elisp.texi.
  3.  
  4.    This file documents GNU Emacs Lisp.
  5.  
  6.    This is edition 1.03 of the GNU Emacs Lisp Reference Manual, for
  7. Emacs Version 18.
  8.  
  9.    Published by the Free Software Foundation, 675 Massachusetts Avenue,
  10. Cambridge, MA 02139 USA
  11.  
  12.    Copyright (C) 1990 Free Software Foundation, Inc.
  13.  
  14.    Permission is granted to make and distribute verbatim copies of this
  15. manual provided the copyright notice and this permission notice are
  16. preserved on all copies.
  17.  
  18.    Permission is granted to copy and distribute modified versions of
  19. this manual under the conditions for verbatim copying, provided that
  20. the entire resulting derived work is distributed under the terms of a
  21. permission notice identical to this one.
  22.  
  23.    Permission is granted to copy and distribute translations of this
  24. manual into another language, under the above conditions for modified
  25. versions, except that this permission notice may be stated in a
  26. translation approved by the Foundation.
  27.  
  28. 
  29. File: elisp,  Node: Nonlocal Exits,  Prev: Iteration,  Up: Control Structures
  30.  
  31. Nonlocal Exits
  32. ==============
  33.  
  34.    A "nonlocal exit" is a transfer of control from one point in a
  35. program to another remote point.  Nonlocal exits can occur in Emacs Lisp
  36. as a result of errors; you can also use them under explicit control.
  37.  
  38. * Menu:
  39.  
  40. * Catch and Throw::     Nonlocal exits for the program's own purposes.
  41. * Examples of Catch::   Showing how such nonlocal exits can be written.
  42. * Errors::              How errors are signaled and handled.
  43. * Cleanups::            Arranging to run a cleanup form if an error happens.
  44.  
  45. 
  46. File: elisp,  Node: Catch and Throw,  Next: Examples of Catch,  Prev: Nonlocal Exits,  Up: Nonlocal Exits
  47.  
  48. Explicit Nonlocal Exits: `catch' and `throw'
  49. --------------------------------------------
  50.  
  51.    Most control constructs affect only the flow of control within the
  52. construct itself.  The function `throw' is the sole exception: it
  53. performs a nonlocal exit on request.  `throw' is used inside a `catch',
  54. and jumps back to that `catch'.  For example:
  55.  
  56.      (catch 'foo
  57.        (progn
  58.          ...
  59.            (throw 'foo t)
  60.          ...))
  61.  
  62. The `throw' transfers control straight back to the corresponding
  63. `catch', which returns immediately.  The code following the `throw' is
  64. not executed.  The second argument of `throw' is used as the return
  65. value of the `catch'.
  66.  
  67.    The `throw' and the `catch' are matched through the first argument:
  68. `throw' searches for a `catch' whose first argument is `eq' to the one
  69. specified.  Thus, in the above example, the `throw' specifies `foo',
  70. and the `catch' specifies the same symbol, so that `catch' is
  71. applicable.  If there is more than one applicable `catch', the
  72. innermost one takes precedence.
  73.  
  74.    All Lisp constructs between the `catch' and the `throw', including
  75. function calls, are exited automatically along with the `catch'.  When
  76. binding constructs such as `let' or function calls are exited in this
  77. way, the bindings are unbound, just as they are when the binding
  78. construct is exited normally (*note Local Variables::.). Likewise, the
  79. buffer and position saved by `save-excursion' (*note Excursions::.) are
  80. restored, and so is the narrowing status saved by `save-restriction'
  81. and the window selection saved by `save-window-excursion' (*note Window
  82. Configurations::.).  Any cleanups established with the `unwind-protect'
  83. special form are executed if the `unwind-protect' is exited with a
  84. `throw'.
  85.  
  86.    The `throw' need not appear lexically within the `catch' that it
  87. jumps to.  It can equally well be called from another function called
  88. within the `catch'.  As long as the `throw' takes place chronologically
  89. after entry to the `catch', and chronologically before exit from it, it
  90. has access to that `catch'.  This is why `throw' can be used in
  91. commands such as `exit-recursive-edit' which throw back to the editor
  92. command loop (*note Recursive Editing::.).
  93.  
  94.      Common Lisp note: most other versions of Lisp, including Common
  95.      Lisp, have several ways of transferring control nonsequentially:
  96.      `return', `return-from', and `go', for example.  Emacs Lisp has
  97.      only `throw'.
  98.  
  99.  -- Special Form: catch TAG BODY...
  100.      `catch' establishes a return point for the `throw' function.  The
  101.      return point is distinguished from other such return points by TAG,
  102.      which may be any Lisp object.  The argument TAG is evaluated
  103.      normally before the return point is established.
  104.  
  105.      With the return point in effect, the forms of the BODY are
  106.      evaluated in textual order.  If the forms execute normally,
  107.      without error or nonlocal exit, the value of the last body form is
  108.      returned from the `catch'.
  109.  
  110.      If a `throw' is done within BODY specifying the same value TAG,
  111.      the `catch' exits immediately; the value it returns is whatever
  112.      was specified as the second argument of `throw'.
  113.  
  114.  -- Function: throw TAG VALUE
  115.      The purpose of `throw' is to return from a return point previously
  116.      established with `catch'.  The argument TAG is used to choose
  117.      among the various existing return points; it must be `eq' to the
  118.      value specified in the `catch'.  If multiple return points match
  119.      TAG, the innermost one is used.
  120.  
  121.      The argument VALUE is used as the value to return from that
  122.      `catch'.
  123.  
  124.      If no return point is in effect with tag TAG, then a `no-catch'
  125.      error is signaled with data `(TAG VALUE)'.
  126.  
  127. 
  128. File: elisp,  Node: Examples of Catch,  Next: Errors,  Prev: Catch and Throw,  Up: Nonlocal Exits
  129.  
  130. Examples of `catch' and `throw'
  131. -------------------------------
  132.  
  133.    One way to use `catch' and `throw' is to exit from a doubly nested
  134. loop.  (In most languages, this would be done with a "go to".) Here we
  135. compute `(foo I J)' for I and J varying from 0 to 9:
  136.  
  137.      (defun search-foo ()
  138.        (catch 'loop
  139.          (let ((i 0))
  140.            (while (< i 10)
  141.              (let ((j 0))
  142.                (while (< j 10)
  143.                  (if (foo i j)
  144.                      (throw 'loop (list i j)))
  145.                  (setq j (1+ j))))
  146.              (setq i (1+ i))))))
  147.  
  148. If `foo' ever returns non-`nil', we stop immediately and return a list
  149. of I and J.  If `foo' always returns `nil', the `catch' returns
  150. normally, and the value is `nil', since that is the result of the
  151. `while'.
  152.  
  153.    Here are two tricky examples, slightly different, showing two return
  154. points at once.  First, two return points with the same tag, `hack':
  155.  
  156.      (defun catch2 (tag)
  157.        (catch tag
  158.          (throw 'hack 'yes)))
  159.      => catch2
  160.      
  161.      (catch 'hack
  162.        (print (catch2 'hack))
  163.        'no)
  164.      -| yes
  165.      => no
  166.  
  167. Since both return points have tags that match the `throw', it goes to
  168. the inner one, the one established in `catch2'.  Therefore, `catch2'
  169. returns normally with value `yes', and this value is printed.  Finally
  170. the second body form in the outer `catch', which is `'no', is evaluated
  171. and returned from the outer `catch'.
  172.  
  173.    Now let's change the argument given to `catch2':
  174.  
  175.      (defun catch2 (tag)
  176.        (catch tag
  177.          (throw 'hack 'yes)))
  178.      => catch2
  179.      
  180.      (catch 'hack
  181.        (print (catch2 'quux))
  182.        'no)
  183.      => yes
  184.  
  185. We still have two return points, but this time only the outer one has
  186. the tag `hack'; the inner one has the tag `quux' instead.  Therefore,
  187. the `throw' returns the value `yes' from the outer return point. The
  188. function `print' is never called, and the body-form `'no' is never
  189. evaluated.
  190.  
  191. 
  192. File: elisp,  Node: Errors,  Next: Cleanups,  Prev: Examples of Catch,  Up: Nonlocal Exits
  193.  
  194. Errors
  195. ------
  196.  
  197.    When Emacs Lisp attempts to evaluate a form that, for some reason,
  198. cannot be evaluated, it "signals" an "error".
  199.  
  200.    When an error is signaled, Emacs's default reaction is to print an
  201. error message and terminate execution of the current command.  This is
  202. the right thing to do in most cases, such as if you type `C-f' at the
  203. end of the buffer.
  204.  
  205.    In complicated programs, simple termination may not be what you want.
  206. For example, the program may have made temporary changes in data
  207. structures, or created temporary buffers which should be deleted before
  208. the program is finished.  In such cases, you would use `unwind-protect'
  209. to establish "cleanup expressions" to be evaluated in case of error. 
  210. Occasionally, you may wish the program to continue execution despite an
  211. error in a subroutine.  In these cases, you would use `condition-case'
  212. to establish "error handlers" to recover control in case of error.
  213.  
  214.    Resist the temptation to use error handling to transfer control from
  215. one part of the program to another; use `catch' and `throw'. *Note
  216. Catch and Throw::.
  217.  
  218. * Menu:
  219.  
  220. * Signaling Errors::      How to report an error.
  221. * Processing of Errors::  What Emacs does when you report an error.
  222. * Handling Errors::       How you can trap errors and continue execution.
  223. * Error Names::           How errors are classified for trapping them.
  224.  
  225. 
  226. File: elisp,  Node: Signaling Errors,  Next: Processing of Errors,  Prev: Errors,  Up: Errors
  227.  
  228. How to Signal an Error
  229. ......................
  230.  
  231.    Most errors are signaled "automatically" within Lisp primitives
  232. which you call for other purposes, such as if you try to take the CAR
  233. of an integer or move forward a character at the end of the buffer; you
  234. can also signal errors explicitly with the functions `error' and
  235. `signal'.
  236.  
  237.  -- Function: error FORMAT-STRING &rest ARGS
  238.      This function signals an error with an error message constructed by
  239.      applying `format' (*note String Conversion::.) to FORMAT-STRING
  240.      and ARGS.
  241.  
  242.      Typical uses of `error' is shown in the following examples:
  243.  
  244.           (error "You have committed an error.  Try something else.")
  245.                error--> You have committed an error.  Try something else.
  246.           
  247.           (error "You have committed %d errors.  You don't learn fast." 10)
  248.                error--> You have committed 10 errors.  You don't learn fast.
  249.  
  250.      `error' works by calling `signal' with two arguments: the error
  251.      symbol `error', and a list containing the string returned by
  252.      `format'.
  253.  
  254.      If you want to use a user-supplied string as an error message
  255.      verbatim, don't just write `(error STRING)'.  If STRING contains
  256.      `%', it will be interpreted as a format specifier, with undesirable
  257.      results.  Instead, use `(error "%s" STRING)'.
  258.  
  259.  -- Function: signal ERROR-SYMBOL DATA
  260.      This function signals an error named by ERROR-SYMBOL.  The
  261.      argument DATA is a list of additional Lisp objects relevant to the
  262.      circumstances of the error.
  263.  
  264.      The argument ERROR-SYMBOL must be an "error symbol"--a symbol that
  265.      has an `error-conditions' property whose value is a list of
  266.      condition names.  This is how different sorts of errors are
  267.      classified.
  268.  
  269.      The number and significance of the objects in DATA depends on
  270.      ERROR-SYMBOL.  For example, with a `wrong-type-arg' error, there
  271.      are two objects in the list: a predicate which describes the type
  272.      that was expected, and the object which failed to fit that type.
  273.      *Note Error Names::, for a description of error symbols.
  274.  
  275.      Both ERROR-SYMBOL and DATA are available to any error handlers
  276.      which handle the error: a list `(ERROR-SYMBOL . DATA)' is
  277.      constructed to become the value of the local variable bound in the
  278.      `condition-case' form (*note Handling Errors::.).  If the error is
  279.      not handled, both of them are used in printing the error message.
  280.  
  281.           (signal 'wrong-number-of-arguments '(x y))
  282.                error--> Wrong number of arguments: x, y
  283.           
  284.           (signal 'no-such-error '("My unknown error condition."))
  285.                error--> peculiar error: "My unknown error condition."
  286.  
  287.      Common Lisp note: Emacs Lisp has nothing like the Common Lisp
  288.      concept of continuable errors.
  289.  
  290. 
  291. File: elisp,  Node: Processing of Errors,  Next: Handling Errors,  Prev: Signaling Errors,  Up: Errors
  292.  
  293. How Emacs Processes Errors
  294. ..........................
  295.  
  296.    When an error is signaled, Emacs searches for an active "handler"
  297. for the error.  A handler is a specially marked place in the Lisp code
  298. of the current function or any of the functions by which it was called.
  299. If an applicable handler exists, its code is executed, and control
  300. resumes following the handler.  The handler executes in the environment
  301. of the `condition-case' which established it; all functions called
  302. within that `condition-case' have already been exited, and the handler
  303. cannot return to them.
  304.  
  305.    If no applicable handler is in effect in your program, the current
  306. command is terminated and control returns to the editor command loop,
  307. because the command loop has an implicit handler for all kinds of
  308. errors.  The command loop's handler uses the error symbol and associated
  309. data to print an error message.
  310.  
  311.    When an error is not handled explicitly, it may cause the Lisp
  312. debugger to be called.  The debugger is enabled if the variable
  313. `debug-on-error' (*note Error Debugging::.) is non-`nil'. Unlike error
  314. handlers, the debugger runs in the environment of the error, so that
  315. you can examine values of variables precisely as they were at the time
  316. of the error.
  317.  
  318. 
  319. File: elisp,  Node: Handling Errors,  Next: Error Names,  Prev: Processing of Errors,  Up: Errors
  320.  
  321. Writing Code to Handle Errors
  322. .............................
  323.  
  324.    The usual effect of signaling an error is to terminate the command
  325. that is running and return immediately to the Emacs editor command loop.
  326. You can arrange to trap errors occurring in a part of your program by
  327. establishing an "error handler" with the special form `condition-case'.
  328.  A simple example looks like this:
  329.  
  330.      (condition-case nil
  331.          (delete-file filename)
  332.        (error nil))
  333.  
  334. This deletes the file named FILENAME, catching any error and returning
  335. `nil' if an error occurs.
  336.  
  337.    The second argument of `condition-case' is called the "protected
  338. form".  (In the example above, the protected form is a call to
  339. `delete-file'.)  The error handlers go into effect when this form
  340. begins execution and are deactivated when this form returns. They
  341. remain in effect for all the intervening time.  In particular, they are
  342. in effect during the execution of subroutines called by this form, and
  343. their subroutines, and so on.  This is a good thing, since, strictly
  344. speaking, errors can be signaled only by Lisp primitives (including
  345. `signal' and `error') called by the protected form, not by the
  346. protected form itself.
  347.  
  348.    The arguments after the protected form are handlers.  Each handler
  349. lists one or more "condition names" (which are symbols) to specify
  350. which errors it will handle.  The error symbol specified when an error
  351. is signaled also defines a list of condition names.  A handler applies
  352. to an error if they have any condition names in common.  In the example
  353. above, there is one handler, and it specifies one condition name,
  354. `error', which covers all errors.
  355.  
  356.    The search for an applicable handler checks all the established
  357. handlers starting with the most recently established one.  Thus, if two
  358. nested `condition-case' forms try to handle the same error, the inner of
  359. the two will actually handle it.
  360.  
  361.    When an error is handled, control returns to the handler, unbinding
  362. all variable bindings made by binding constructs that are exited and
  363. executing the cleanups of all `unwind-protect' forms that are exited by
  364. doing so.  Then the body of the handler is executed.  After this,
  365. execution continues by returning from the `condition-case' form. 
  366. Because the protected form is exited completely before execution of the
  367. handler, the handler cannot resume execution at the point of the error,
  368. nor can it examine variable bindings that were made within the
  369. protected form.  All it can do is clean up and proceed.
  370.  
  371.    Error signaling and handling have some resemblance to `throw' and
  372. `catch', but they are entirely separate facilities.  An error cannot be
  373. caught by a `catch', and a `throw' cannot be handled by an error
  374. handler (though if there is no `catch', `throw' will signal an error
  375. which can be handled).
  376.  
  377.  -- Special Form: condition-case VAR PROTECTED-FORM HANDLERS...
  378.      This special form establishes the error handlers HANDLERS around
  379.      the execution of PROTECTED-FORM.  If PROTECTED-FORM executes
  380.      without error, the value it returns becomes the value of the
  381.      `condition-case' form; in this case, the `condition-case' has no
  382.      effect.  The `condition-case' form makes a difference when an
  383.      error occurs during PROTECTED-FORM.
  384.  
  385.      Each of the HANDLERS is a list of the form `(CONDITIONS BODY...)'.
  386.       CONDITIONS is a condition name to be handled, or a list of
  387.      condition names; BODY is one or more Lisp expressions to be
  388.      executed when this handler handles an error.
  389.  
  390.      Each error that occurs has an "error symbol" which describes what
  391.      kind of error it is.  The `error-conditions' property of this
  392.      symbol is a list of condition names (*note Error Names::.).  Emacs
  393.      searches all the active `condition-case' forms for a handler which
  394.      specifies one or more of these names; the innermost matching
  395.      `condition-case' handles the error.  The handlers in this
  396.      `condition-case' are tested in the order in which they appear.
  397.  
  398.      The body of the handler is then executed, and the `condition-case'
  399.      returns normally, using the value of the last form in the body as
  400.      the overall value.
  401.  
  402.      The argument VAR is a variable.  `condition-case' does not bind
  403.      this variable when executing the PROTECTED-FORM, only when it
  404.      handles an error.  At that time, VAR is bound locally to a list of
  405.      the form `(ERROR-SYMBOL . DATA)', giving the particulars of the
  406.      error.  The handler can refer to this list to decide what to do. 
  407.      For example, if the error is for failure opening a file, the file
  408.      name is the second element of DATA--the third element of VAR.
  409.  
  410.      If VAR is `nil', that means no variable is bound.  Then the error
  411.      symbol and associated data are not made available to the handler.
  412.  
  413.    Here is an example of using `condition-case' to handle the error
  414. that results from dividing by zero.  The handler prints out a warning
  415. message and returns a very large number.
  416.  
  417.      (defun safe-divide (dividend divisor)
  418.        (condition-case err
  419.            ;; Protected form.
  420.            (/ dividend divisor)
  421.          ;; The handler.
  422.          (arith-error                        ; Condition.
  423.           (princ (format "Arithmetic error: %s" err))
  424.           1000000)))
  425.      => safe-divide
  426.      
  427.      (safe-divide 5 0)
  428.           -| Arithmetic error: (arith-error)
  429.      => 1000000
  430.  
  431. The handler specifies condition name `arith-error' so that it will
  432. handle only division-by-zero errors.  Other kinds of errors will not be
  433. handled, at least not by this `condition-case'.  Thus,
  434.  
  435.      (safe-divide nil 3)
  436.           error--> Wrong type argument: integer-or-marker-p, nil
  437.  
  438.    Here is a `condition-case' that catches all kinds of errors,
  439. including those signaled with `error':
  440.  
  441.      (setq baz 34)
  442.           => 34
  443.      
  444.      (condition-case err
  445.          (if (eq baz 35)
  446.              t
  447.            ;; This is a call to the function `error'.
  448.            (error "Rats!  The variable %s was %s, not 35." 'baz baz))
  449.        ;; This is the handler; it is not a form.
  450.        (error (princ (format "The error was: %s" err))
  451.               2))
  452.      
  453.           -| The error was: (error "Rats!  The variable baz was 34, not 35.")
  454.           => 2
  455.  
  456.    `condition-case' is often used to trap errors that are predictable,
  457. such as failure to open a file in a call to `insert-file-contents'.  It
  458. is also used to trap errors that are totally unpredictable, such as
  459. when the program evaluates an expression read from the user.
  460.  
  461. 
  462. File: elisp,  Node: Error Names,  Prev: Handling Errors,  Up: Errors
  463.  
  464. Error Symbols and Condition Names
  465. .................................
  466.  
  467.    When you signal an error, you specify an "error symbol" to specify
  468. the kind of error you have in mind.  Each error has one and only one
  469. error symbol to categorize it.  This is the finest classification of
  470. errors defined by the Lisp language.
  471.  
  472.    These narrow classifications are grouped into a hierarchy of wider
  473. classes called "error conditions", identified by "condition names". 
  474. The narrowest such classes belong to the error symbols themselves: each
  475. error symbol is also a condition name.  There are also condition names
  476. for more extensive classes, up to the condition name `error' which
  477. takes in all kinds of errors.  Thus, each error has one or more
  478. condition names: `error', the error symbol if that is distinct from
  479. `error', and perhaps some intermediate classifications.
  480.  
  481.    In order for a symbol to be usable as an error symbol, it must have
  482. an `error-conditions' property which gives a list of condition names.
  483. This list defines the conditions which this kind of error belongs to.
  484. (The error symbol itself, and the symbol `error', should always be
  485. members of this list.)  Thus, the hierarchy of condition names is
  486. defined by the `error-conditions' properties of the error symbols.
  487.  
  488.    In addition to the `error-conditions' list, the error symbol should
  489. have an `error-message' property whose value is a string to be printed
  490. when that error is signaled but not handled.  If the `error-message'
  491. property exists, but is not a string, the error message `peculiar
  492. error' is used.
  493.  
  494.    Here is how we define a new error symbol, `new-error':
  495.  
  496.      (put 'new-error 'error-conditions '(error my-own-errors new-error))
  497.           => (error my-own-errors new-error)
  498.      (put 'new-error 'error-message "A new error")
  499.           => "A new error"
  500.  
  501. This error has three condition names: `new-error', the narrowest
  502. classification; `my-own-errors', which we imagine is a wider
  503. classification; and `error', which is the widest of all.
  504.  
  505.    Naturally, Emacs will never signal a `new-error' on its own; only an
  506. explicit call to `signal' (*note Errors::.) in your code can do this:
  507.  
  508.      (signal 'new-error '(x y))
  509.           error--> A new error: x, y
  510.  
  511.    This error can be handled through any of the three condition names.
  512. This example handles `new-error' and any other errors in the class
  513. `my-own-errors':
  514.  
  515.      (condition-case foo
  516.          (bar nil t)
  517.        (my-own-errors nil))
  518.  
  519.    The significant way that errors are classified is by their condition
  520. names--the names used to match errors with handlers.  An error symbol
  521. serves only as a convenient way to specify the intended error message
  522. and list of condition names.  If `signal' were given a list of
  523. condition names rather than one error symbol, that would be cumbersome.
  524.  
  525.    By contrast, using only error symbols without condition names would
  526. seriously decrease the power of `condition-case'.  Condition names make
  527. it possible to categorize errors at various levels of generality when
  528. you write an error handler.  Using error symbols alone would eliminate
  529. all but the narrowest level of classification.
  530.  
  531.    *Note Standard Errors::, for a list of all the standard error symbols
  532. and their conditions.
  533.  
  534. 
  535. File: elisp,  Node: Cleanups,  Prev: Errors,  Up: Nonlocal Exits
  536.  
  537. Cleaning up from Nonlocal Exits
  538. -------------------------------
  539.  
  540.    The `unwind-protect' construct is essential whenever you temporarily
  541. put a data structure in an inconsistent state; it permits you to ensure
  542. the data are consistent in the event of an error.
  543.  
  544.  -- Special Form: unwind-protect BODY CLEANUP-FORMS...
  545.      `unwind-protect' executes the BODY with a guarantee that the
  546.      CLEANUP-FORMS will be evaluated if control leaves BODY, no matter
  547.      how that happens.  The BODY may complete normally, or execute a
  548.      `throw' out of the `unwind-protect', or cause an error; in all
  549.      cases, the CLEANUP-FORMS will be evaluated.
  550.  
  551.      Only the BODY is actually protected by the `unwind-protect'. If
  552.      any of the CLEANUP-FORMS themselves exit nonlocally (e.g., via a
  553.      `throw' or an error), it is *not* guaranteed that the rest of them
  554.      will be executed.  If the failure of one of the CLEANUP-FORMS has
  555.      the potential to cause trouble, then it should be protected by
  556.      another `unwind-protect' around that form.
  557.  
  558.      The number of currently active `unwind-protect' forms counts,
  559.      together with the number of local variable bindings, against the
  560.      limit `max-specpdl-size' (*note Local Variables::.).
  561.  
  562.    For example, here we make an invisible buffer for temporary use, and
  563. make sure to kill it before finishing:
  564.  
  565.      (save-excursion
  566.        (let ((buffer (get-buffer-create " *temp*")))
  567.          (set-buffer buffer)
  568.          (unwind-protect
  569.              BODY
  570.            (kill-buffer buffer))))
  571.  
  572. You might think that we could just as well write `(kill-buffer
  573. (current-buffer))' and dispense with the variable `buffer'. However,
  574. the way shown above is safer, if BODY happens to get an error after
  575. switching to a different buffer!  (Alternatively, you could write
  576. another `save-excursion' around the body, to ensure that the temporary
  577. buffer becomes current in time to kill it.)
  578.  
  579.    Here is an actual example taken from the file `ftp.el'.  It creates
  580. a process (*note Processes::.) to try to establish a connection to a
  581. remote machine.  As the function `ftp-login' is highly susceptible to
  582. numerous problems which the writer of the function cannot anticipate,
  583. it is protected with a form that guarantees deletion of the process in
  584. the event of failure.  Otherwise, Emacs might fill up with useless
  585. subprocesses.
  586.  
  587.      (let ((win nil))
  588.        (unwind-protect
  589.            (progn
  590.              (setq process (ftp-setup-buffer host file))
  591.              (if (setq win (ftp-login process host user password))
  592.                  (message "Logged in")
  593.                (error "Ftp login failed")))
  594.          (or win (and process (delete-process process)))))
  595.  
  596.    This example actually has a small bug: if the user types `C-g' to
  597. quit, and the quit happens immediately after the function
  598. `ftp-setup-buffer' returns but before the variable `process' is set,
  599. the process will not be killed.  There is no easy way to fix this bug,
  600. but at least it is very unlikely.
  601.  
  602. 
  603. File: elisp,  Node: Variables,  Next: Functions,  Prev: Control Structures,  Up: Top
  604.  
  605. Variables
  606. *********
  607.  
  608.    A "variable" is a name used in a program to stand for a value.
  609. Nearly all programming languages have variables of some sort.  In the
  610. text for a Lisp program, variables are written using the syntax for
  611. symbols.
  612.  
  613.    In Lisp, unlike most programming languages, programs are represented
  614. primarily as Lisp objects and only secondarily as text.  The Lisp
  615. objects used for variables are symbols: the symbol name is the variable
  616. name, and the variable's value is stored in the value cell of the
  617. symbol.  The use of a symbol as a variable is independent of whether
  618. the same symbol has a function definition.  *Note Symbol Components::.
  619.  
  620.    The textual form of a program is determined by its Lisp object
  621. representation; it is the read syntax for the Lisp object which
  622. constitutes the program.  This is why a variable in a textual Lisp
  623. program is written as the read syntax for the symbol that represents the
  624. variable.
  625.  
  626. * Menu:
  627.  
  628. * Global Variables::      Variable values that exist permanently, everywhere.
  629. * Constant Variables::    Certain "variables" have values that never change.
  630. * Local Variables::       Variable values that exist only temporarily.
  631. * Void Variables::        Symbols that lack values.
  632. * Defining Variables::    A definition says a symbol is used as a variable.
  633. * Accessing Variables::   Examining values of variables whose names
  634.                             are known only at run time.
  635. * Setting Variables::     Storing new values in variables.
  636. * Variable Scoping::      How Lisp chooses among local and global values.
  637. * Buffer-Local Variables::  Variable values in effect only in one buffer.
  638.  
  639. 
  640. File: elisp,  Node: Global Variables,  Next: Constant Variables,  Prev: Variables,  Up: Variables
  641.  
  642. Global Variables
  643. ================
  644.  
  645.    The simplest way to use a variable is "globally".  This means that
  646. the variable has just one value at a time, and this value is in effect
  647. (at least for the moment) throughout the Lisp system.  The value remains
  648. in effect until you specify a new one.  When a new value replaces the
  649. old one, no trace of the old value remains in the variable.
  650.  
  651.    You specify a value for a symbol with `setq'.  For example,
  652.  
  653.      (setq x '(a b))
  654.  
  655. gives the variable `x' the value `(a b)'.  Note that the first argument
  656. of `setq', the name of the variable, is not evaluated, but the second
  657. argument, the desired value, is evaluated normally.
  658.  
  659.    Once the variable has a value, you can refer to it by using the
  660. symbol by itself as an expression.  Thus,
  661.  
  662.      x
  663.           => (a b)
  664.  
  665. assuming the `setq' form shown above has already been executed.
  666.  
  667.    If you do another `setq', the new value replaces the old one:
  668.  
  669.      x
  670.           => (a b)
  671.      (setq x 4)
  672.           => 4
  673.      x
  674.           => 4
  675.  
  676. 
  677. File: elisp,  Node: Constant Variables,  Next: Local Variables,  Prev: Global Variables,  Up: Variables
  678.  
  679. Variables that Never Change
  680. ===========================
  681.  
  682.    Emacs Lisp has two special symbols, `nil' and `t', that always
  683. evaluate to themselves.  These symbols cannot be rebound, nor can their
  684. value cells be changed.  An attempt to change the value of `nil' or `t'
  685. signals a `setting-constant' error.
  686.  
  687.      nil == 'nil
  688.           => nil
  689.      (setq nil 500)
  690.      error--> Attempt to set constant symbol: nil
  691.  
  692. 
  693. File: elisp,  Node: Local Variables,  Next: Void Variables,  Prev: Constant Variables,  Up: Variables
  694.  
  695. Local Variables
  696. ===============
  697.  
  698.    Global variables are given values that last until explicitly
  699. superseded with new values.  Sometimes it is useful to create variable
  700. values that exist temporarily--only while within a certain part of the
  701. program.  These values are called "local", and the variables so used
  702. are called "local variables".
  703.  
  704.    For example, when a function is called, its argument variables
  705. receive new local values which last until the function exits. 
  706. Similarly, the `let' special form explicitly establishes new local
  707. values for specified variables; these last until exit from the `let'
  708. form.
  709.  
  710.    When a local value is established, the previous value (or lack of
  711. one) of the variable is saved away.  When the life span of the local
  712. value is over, the previous value is restored.  In the mean time, we
  713. say that the previous value is "shadowed" and "not visible".  Both
  714. global and local values may be shadowed.
  715.  
  716.    If you set a variable (such as with `setq') while it is local, this
  717. replaces the local value; it does not alter the global value, or
  718. previous local values that are shadowed.  To model this behavior, we
  719. speak of a "local binding" of the variable as well as a local value.
  720.  
  721.    The local binding is a conceptual place that holds a local value.
  722. Entry to a function, or a special form such as `let', creates the local
  723. binding; exit from the function or from the `let' removes the local
  724. binding.  As long as the local binding lasts, the variable's value is
  725. stored within it.  Use of `setq' or `set' while there is a local
  726. binding stores a different value into the local binding; it does not
  727. create a new binding.
  728.  
  729.    We also speak of the "global binding", which is where (conceptually)
  730. the global value is kept.
  731.  
  732.    A variable can have more than one local binding at a time (for
  733. example, if there are nested `let' forms that bind it).  In such a
  734. case, the most recently created local binding that still exists is the
  735. "current binding" of the variable.  (This is called "dynamic scoping";
  736. see *Note Variable Scoping::.)  If there are no local bindings, the
  737. variable's global binding is its current binding.  We also call the
  738. current binding the "most-local existing binding", for emphasis.
  739. Ordinary evaluation of a symbol always returns the value of its current
  740. binding.
  741.  
  742.    The special forms `let' and `let*' exist to create local bindings.
  743.  
  744.  -- Special Form: let (BINDINGS...) FORMS...
  745.      This function binds variables according to BINDINGS and then
  746.      evaluates all of the FORMS in textual order.  The `let'-form
  747.      returns the value of the last form in FORMS.
  748.  
  749.      Each of the BINDINGS is either (i) a symbol, in which case that
  750.      symbol is bound to `nil'; or (ii) a list of the form `(SYMBOL
  751.      VALUE-FORM)', in which case SYMBOL is bound to the result of
  752.      evaluating VALUE-FORM.  If VALUE-FORM is omitted, `nil' is used.
  753.  
  754.      All of the VALUE-FORMs in BINDINGS are evaluated in the order they
  755.      appear and *before* any of the symbols are bound.  Here is an
  756.      example of this: `Z' is bound to the old value of `Y', which is 2,
  757.      not the new value, 1.
  758.  
  759.           (setq Y 2)
  760.                => 2
  761.           (let ((Y 1)
  762.                 (Z Y))
  763.             (list Y Z))
  764.                => (1 2)
  765.  
  766.  -- Special Form: let* (BINDINGS...) FORMS...
  767.      This special form is like `let', except that each symbol in
  768.      BINDINGS is bound as soon as its new value is computed, before the
  769.      computation of the values of the following local bindings. 
  770.      Therefore, an expression in BINDINGS may reasonably refer to the
  771.      preceding symbols bound in this `let*' form.  Compare the
  772.      following example with the example above for `let'.
  773.  
  774.           (setq Y 2)
  775.                => 2
  776.           (let* ((Y 1)
  777.                  (Z Y))    ; Use the just-established value of `Y'.
  778.             (list Y Z))
  779.                => (1 1)
  780.  
  781.    Here is a complete list of the other facilities which create local
  782. bindings:
  783.  
  784.    * Function calls (*note Functions::.).
  785.  
  786.    * Macro calls (*note Macros::.).
  787.  
  788.    * `condition-case' (*note Errors::.).
  789.  
  790.  -- Variable: max-specpdl-size
  791.      This variable defines the limit on the number of local variable
  792.      bindings and `unwind-protect' cleanups (*note Nonlocal Exits::.)
  793.      that are allowed before signaling an error (with data `"Variable
  794.      binding depth exceeds max-specpdl-size"').
  795.  
  796.      This limit, with the associated error when it is exceeded, is one
  797.      way that Lisp avoids infinite recursion on an ill-defined function.
  798.  
  799.      The default value is 600.
  800.  
  801. 
  802. File: elisp,  Node: Void Variables,  Next: Defining Variables,  Prev: Local Variables,  Up: Variables
  803.  
  804. When a Variable is "Void"
  805. =========================
  806.  
  807.    If you have never given a symbol any value as a global variable, we
  808. say that that symbol's global value is "void".  In other words, the
  809. symbol's value cell does not have any Lisp object in it.  If you try to
  810. evaluate the symbol, you get a `void-variable' error rather than a
  811. value.
  812.  
  813.    Note that a value of `nil' is not the same as void.  The symbol
  814. `nil' is a Lisp object and can be the value of a variable just as any
  815. other object can be; but it is *a value*.  A void variable does not
  816. have any value.
  817.  
  818.    After you have given a variable a value, you can make it void once
  819. more using `makunbound'.
  820.  
  821.  -- Function: makunbound SYMBOL
  822.      This function makes the current binding of SYMBOL void.  This
  823.      causes any future attempt to use this symbol as a variable to
  824.      signal the error `void-variable', unless or until you set it again.
  825.  
  826.      `makunbound' returns SYMBOL.
  827.  
  828.           (makunbound 'x)          ; Make the global value of `x' void.
  829.                => x
  830.           x
  831.           error--> Symbol's value as variable is void: x
  832.  
  833.      If SYMBOL is locally bound, `makunbound' affects the most local
  834.      existing binding.  This is the only way a symbol can have a void
  835.      local binding, since all the constructs that create local bindings
  836.      create them with values.  In this case, the voidness lasts at most
  837.      as long as the binding does; when the binding is removed due to
  838.      exit from the construct that made it, the previous or global
  839.      binding is reexposed as usual, and the variable is no longer void
  840.      unless the newly reexposed binding was void all along.
  841.  
  842.           (setq x 1)               ; Put a value in the global binding.
  843.                => 1
  844.           (let ((x 2))             ; Locally bind it.
  845.             (makunbound 'x)        ; Void the local binding.
  846.             x)
  847.           error--> Symbol's value as variable is void: x
  848.           x                        ; The global binding is unchanged.
  849.                => 1
  850.           
  851.           (let ((x 2))             ; Locally bind it.
  852.             (let ((x 3))           ; And again.
  853.               (makunbound 'x)      ; Void the innermost-local binding.
  854.               x))                  ; And refer: it's void.
  855.           error--> Symbol's value as variable is void: x
  856.           
  857.           (let ((x 2))
  858.             (let ((x 3))
  859.               (makunbound 'x))     ; Void inner binding, then remove it.
  860.             x)                     ; Now outer `let' binding is visible.
  861.                => 2
  862.  
  863.    A variable that has been made void with `makunbound' is
  864. indistinguishable from one that has never received a value and has
  865. always been void.
  866.  
  867.    You can use the function `boundp' to test whether a variable is
  868. currently void.
  869.  
  870.  -- Function: boundp VARIABLE
  871.      `boundp' returns `t' if VARIABLE (a symbol) is not void; more
  872.      precisely, if its current binding is not void.  It returns `nil'
  873.      otherwise.
  874.  
  875.           (boundp 'abracadabra)                ; Starts out void.
  876.                => nil
  877.           (let ((abracadabra 5))               ; Locally bind it.
  878.             (boundp 'abracadabra))
  879.                => t
  880.           (boundp 'abracadabra)                ; Still globally void.
  881.                => nil
  882.           (setq abracadabra 5)                 ; Make it globally nonvoid.
  883.                => 5
  884.           (boundp 'abracadabra)
  885.                => t
  886.  
  887. 
  888. File: elisp,  Node: Defining Variables,  Next: Accessing Variables,  Prev: Void Variables,  Up: Variables
  889.  
  890. Defining Global Variables
  891. =========================
  892.  
  893.    You may announce your intention to use a symbol as a global variable
  894. with a definition, using `defconst' or `defvar'.
  895.  
  896.    In Emacs Lisp, definitions serve three purposes.  First, they inform
  897. the user who reads the code that certain symbols are *intended* to be
  898. used as variables.  Second, they inform the Lisp system of these things,
  899. supplying a value and documentation.  Third, they provide information to
  900. utilities such as `etags' and `make-docfile', which create data bases
  901. of the functions and variables in a program.
  902.  
  903.    The difference between `defconst' and `defvar' is primarily a matter
  904. of intent, serving to inform human readers of whether programs will
  905. change the variable.  Emacs Lisp does not restrict the ways in which a
  906. variable can be used based on `defconst' or `defvar' declarations. 
  907. However, it also makes a difference for initialization: `defconst'
  908. unconditionally initializes the variable, while `defvar' initializes it
  909. only if it is void.
  910.  
  911.    One would expect user option variables to be defined with
  912. `defconst', since programs do not change them.  Unfortunately, this has
  913. bad results if the definition is in a library that is not preloaded:
  914. `defconst' would override any prior value when the library is loaded. 
  915. Users would like to be able to set the option in their init files, and
  916. override the default value given in the definition.  For this reason,
  917. user options must be defined with `defvar'.
  918.  
  919.  -- Special Form: defvar SYMBOL [VALUE [DOC-STRING]]
  920.      This special form informs a person reading your code that SYMBOL
  921.      will be used as a variable that the programs are likely to set or
  922.      change.  It is also used for all user option variables except in
  923.      the preloaded parts of Emacs.  Note that SYMBOL is not evaluated;
  924.      the symbol to be defined must appear explicitly in the `defvar'.
  925.  
  926.      If SYMBOL already has a value (i.e., it is not void), VALUE is not
  927.      even evaluated, and SYMBOL's value remains unchanged.  If SYMBOL
  928.      is void and VALUE is specified, it is evaluated and SYMBOL is set
  929.      to the result.  (If VALUE is not specified, the value of SYMBOL is
  930.      not changed in any case.)
  931.  
  932.      If the DOC-STRING argument appears, it specifies the documentation
  933.      for the variable.  (This opportunity to specify documentation is
  934.      one of the main benefits of defining the variable.)  The
  935.      documentation is stored in the symbol's `variable-documentation'
  936.      property.  The Emacs help functions (*note Documentation::.) look
  937.      for this property.
  938.  
  939.      If the first character of DOC-STRING is `*', it means that this
  940.      variable is considered to be a user option.  This affects commands
  941.      such as `set-variable' and `edit-options'.
  942.  
  943.      For example, this form defines `foo' but does not set its value:
  944.  
  945.           (defvar foo)
  946.                => foo
  947.  
  948.      The following example sets the value of `bar' to `23', and gives
  949.      it a documentation string:
  950.  
  951.           (defvar bar 23 "The normal weight of a bar.")
  952.                => bar
  953.  
  954.      The following form changes the documentation string for `bar',
  955.      making it a user option, but does not change the value, since `bar'
  956.      already has a value.  (The addition `(1+ 23)' is not even
  957.      performed.)
  958.  
  959.           (defvar bar (1+ 23) "*The normal weight of a bar.")
  960.                => bar
  961.           bar
  962.                => 23
  963.  
  964.      Here is an equivalent expression for the `defvar' special form:
  965.  
  966.           (defvar SYMBOL VALUE DOC-STRING)
  967.           ==
  968.           (progn
  969.             (if (not (boundp 'SYMBOL))
  970.                 (setq SYMBOL VALUE))
  971.             (put 'SYMBOL 'variable-documentation 'DOC-STRING)
  972.             'SYMBOL)
  973.  
  974.      The `defvar' form returns SYMBOL, but it is normally used at top
  975.      level in a file where its value does not matter.
  976.  
  977.  -- Special Form: defconst SYMBOL [VALUE [DOC-STRING]]
  978.      This special form informs a person reading your code that SYMBOL
  979.      has a global value, established here, that will not normally be
  980.      changed or locally bound by the execution of the program.  The
  981.      user, however, may be welcome to change it.  Note that SYMBOL is
  982.      not evaluated; the symbol to be defined must appear explicitly in
  983.      the `defconst'.
  984.  
  985.      `defconst' always evaluates VALUE and sets the global value of
  986.      SYMBOL to the result, provided VALUE is given.
  987.  
  988.      *Note:* don't use `defconst' for user option variables in
  989.      libraries that are not normally loaded.  The user should be able to
  990.      specify a value for such a variable in the `.emacs' file, so that
  991.      it will be in effect if and when the library is loaded later.
  992.  
  993.      Here, `pi' is a constant that presumably ought not to be changed
  994.      by anyone (attempts by the Indiana State Legislature
  995.      notwithstanding). As the second form illustrates, however, this is
  996.      only advisory.
  997.  
  998.           (defconst pi 3 "Pi to one place.")
  999.                => pi
  1000.           (setq pi 4)
  1001.                => pi
  1002.           pi
  1003.                => 4
  1004.  
  1005.  -- Function: user-variable-p VARIABLE
  1006.      This function returns `t' if VARIABLE is a user option, intended
  1007.      to be set by the user for customization, `nil' otherwise.
  1008.      (Variables other than user options exist for the internal purposes
  1009.      of Lisp programs, and users need not know about them.)
  1010.  
  1011.      User option variables are distinguished from other variables by the
  1012.      first character of the `variable-documentation' property.  If the
  1013.      property exists and is a string, and its first character is `*',
  1014.      then the variable is a user option.
  1015.  
  1016.    Note that if the `defconst' and `defvar' special forms are used
  1017. while the variable has a local binding, the local binding's value is
  1018. set, and the global binding is not changed.  This would be confusing.
  1019. But the normal way to use these special forms is at top level in a file,
  1020. where no local binding should be in effect.
  1021.  
  1022. 
  1023. File: elisp,  Node: Accessing Variables,  Next: Setting Variables,  Prev: Defining Variables,  Up: Variables
  1024.  
  1025. Accessing Variable Values
  1026. =========================
  1027.  
  1028.    The usual way to reference a variable is to write the symbol which
  1029. names it (*note Symbol Forms::.).  This requires you to specify the
  1030. variable name when you write the program.  Usually that is exactly what
  1031. you want to do.  Occasionally you need to choose at run time which
  1032. variable to reference; then you can use `symbol-value'.
  1033.  
  1034.  -- Function: symbol-value SYMBOL
  1035.      This function returns the value of SYMBOL.  This is the value in
  1036.      the innermost local binding of the symbol, or its global value if
  1037.      it has no local bindings.
  1038.  
  1039.           (setq abracadabra 5)
  1040.                => 5
  1041.           (setq foo 9)
  1042.                => 9
  1043.           
  1044.           ;; Here the symbol `abracadabra'
  1045.           ;; is the symbol whose value is examined.
  1046.           (let ((abracadabra 'foo))
  1047.             (symbol-value 'abracadabra))
  1048.                => foo
  1049.           
  1050.           ;; Here the value of `abracadabra',
  1051.           ;; which is `foo',
  1052.           ;; is the symbol whose value is examined.
  1053.           (let ((abracadabra 'foo))
  1054.             (symbol-value abracadabra))
  1055.                => 9
  1056.           
  1057.           (symbol-value 'abracadabra)
  1058.                => 5
  1059.  
  1060.      A `void-variable' error is signaled if SYMBOL has neither a local
  1061.      binding nor a global value.
  1062.  
  1063. 
  1064. File: elisp,  Node: Setting Variables,  Next: Variable Scoping,  Prev: Accessing Variables,  Up: Variables
  1065.  
  1066. How to Alter a Variable Value
  1067. =============================
  1068.  
  1069.    The usual way to change the value of a variable is with the special
  1070. form `setq'.  When you need to compute the choice of variable at run
  1071. time, use the function `set'.
  1072.  
  1073.  -- Special Form: setq [SYMBOL FORM]...
  1074.      This special form is the most common method of changing a
  1075.      variable's value.  Each SYMBOL is given a new value, which is the
  1076.      result of evaluating the corresponding FORM.  The most-local
  1077.      existing binding of the symbol is changed.
  1078.  
  1079.      The value of the `setq' form is the value of the last FORM.
  1080.  
  1081.           (setq x (1+ 2))
  1082.                => 3
  1083.           x                     ; `x' now has a global value.
  1084.                => 3
  1085.           (let ((x 5))
  1086.             (setq x 6)          ; The local binding of `x' is set.
  1087.             x)
  1088.                => 6
  1089.           x                     ; The global value is unchanged.
  1090.                => 3
  1091.  
  1092.      Note that the first FORM is evaluated, then the first SYMBOL is
  1093.      set, then the second FORM is evaluated, then the second SYMBOL is
  1094.      set, and so on:
  1095.  
  1096.           (setq x 10            ; Notice that `x' is set
  1097.                 y (1+ x))       ; before the value of `y' is computed.
  1098.                => 11
  1099.  
  1100.  -- Function: set SYMBOL VALUE
  1101.      This function sets SYMBOL's value to VALUE, then returns VALUE. 
  1102.      Since `set' is a function, the expression written for SYMBOL is
  1103.      evaluated to obtain the symbol to be set.
  1104.  
  1105.      The most-local existing binding of the variable is the binding
  1106.      that is set; shadowed bindings are not affected.  If SYMBOL is not
  1107.      actually a symbol, a `wrong-type-argument' error is signaled.
  1108.  
  1109.           (set one 1)
  1110.           error--> Symbol's value as variable is void: one
  1111.           (set 'one 1)
  1112.                => 1
  1113.           (set 'two 'one)
  1114.                => one
  1115.           (set two 2)            ; `two' evaluates to symbol `one'.
  1116.                => 2
  1117.           one                    ; So it is `one' that was set.
  1118.                => 2
  1119.           (let ((one 1))         ; This binding of `one' is set,
  1120.             (set 'one 3)         ; not the global value.
  1121.             one)
  1122.                => 3
  1123.           one
  1124.                => 2
  1125.  
  1126.      Logically speaking, `set' is a more fundamental primitive that
  1127.      `setq'.  Any use of `setq' can be trivially rewritten to use
  1128.      `set'; `setq' could even be defined as a macro, given the
  1129.      availability of `set'.  However, `set' itself is rarely used;
  1130.      beginners hardly need to know about it.  It is needed only when the
  1131.      choice of variable to be set is made at run time.  For example, the
  1132.      command `set-variable', which reads a variable name from the user
  1133.      and then sets the variable, needs to use `set'.
  1134.  
  1135.           Common Lisp note: in Common Lisp, `set' always changes the
  1136.           symbol's special value, ignoring any lexical bindings.  In
  1137.           Emacs Lisp, all variables and all bindings are special, so
  1138.           `set' always affects the most local existing binding.
  1139.  
  1140. 
  1141. File: elisp,  Node: Variable Scoping,  Next: Buffer-Local Variables,  Prev: Setting Variables,  Up: Variables
  1142.  
  1143. Scoping Rules for Variable Bindings
  1144. ===================================
  1145.  
  1146.    A given symbol `foo' may have several local variable bindings,
  1147. established at different places in the Lisp program, as well as a global
  1148. binding.  The most recently established binding takes precedence over
  1149. the others.
  1150.  
  1151.    Local bindings in Emacs Lisp have "indefinite scope" and "dynamic
  1152. extent".  "Scope" refers to *where* textually in the source code the
  1153. binding can be accessed.  Indefinite scope means that any part of the
  1154. program can potentially access the variable binding.  "Extent" refers
  1155. to *when*, as the program is executing, the binding exists.  Dynamic
  1156. extent means that the binding lasts as long as the activation of the
  1157. construct that established it.
  1158.  
  1159.    The combination of dynamic extent and indefinite scope is called
  1160. "dynamic scoping".  By contrast, most programming languages use
  1161. "lexical scoping", in which references to a local variable must be
  1162. textually within the function or block that binds the variable.
  1163.  
  1164.      Common Lisp note: variables declared "special" in Common Lisp are
  1165.      dynamically scoped like variables in Emacs Lisp.
  1166.  
  1167. * Menu:
  1168.  
  1169. * Scope::          Scope means where in the program a value is visible.
  1170.                      Comparison with other languages.
  1171. * Extent::         Extent means how long in time a value exists.
  1172. * Impl of Scope::  Two ways to implement dynamic scoping.
  1173. * Using Scoping::  How to use dynamic scoping carefully and avoid problems.
  1174.  
  1175.