home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / elisp / elisp-7 < prev    next >
Encoding:
GNU Info File  |  1993-05-31  |  48.3 KB  |  1,223 lines

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