(lispref.info)Eval


Next: Forms Prev: Intro Eval Up: Evaluation

Eval
====

   Most often, forms are evaluated automatically, by virtue of their
occurrence in a program being run.  On rare occasions, you may need to
write code that evaluates a form that is computed at run time, such as
after reading a form from text being edited or getting one from a
property list.  On these occasions, use the `eval' function.

   The functions and variables described in this section evaluate
forms, specify limits to the evaluation process, or record recently
returned values.  Loading a file also does evaluation (*note
Loading::.).

 - Function: eval FORM
     This is the basic function for performing evaluation.  It evaluates
     FORM in the current environment and returns the result.  How the
     evaluation proceeds depends on the type of the object (*note
     Forms::.).

     Since `eval' is a function, the argument expression that appears
     in a call to `eval' is evaluated twice: once as preparation before
     `eval' is called, and again by the `eval' function itself.  Here
     is an example:

          (setq foo 'bar)
               => bar
          (setq bar 'baz)
               => baz
          ;; `eval' receives argument `bar', which is the value of `foo'
          (eval foo)
               => baz

     The number of currently active calls to `eval' is limited to
     `max-lisp-eval-depth' (see below).

 - Command: eval-current-buffer &optional STREAM
     This function evaluates the forms in the current buffer.  It reads
     forms from the buffer and calls `eval' on them until the end of the
     buffer is reached, or until an error is signaled and not handled.

     If STREAM is supplied, the variable `standard-output' is bound to
     STREAM during the evaluation (Note: Output Functions.).

     `eval-current-buffer' always returns `nil'.

 - Command: eval-region START END &optional STREAM
     This function evaluates the forms in the current buffer in the
     region defined by the positions START and END.  It reads forms from
     the region and calls `eval' on them until the end of the region is
     reached, or until an error is signaled and not handled.

     If STREAM is supplied, `standard-output' is bound to it for the
     duration of the command.

     `eval-region' always returns `nil'.

 - Variable: max-lisp-eval-depth
     This variable defines the maximum depth allowed in calls to
     `eval', `apply', and `funcall' before an error is signaled (with
     error message `"Lisp nesting exceeds max-lisp-eval-depth"').
     `eval' is called recursively to evaluate the arguments of Lisp
     function calls and to evaluate bodies of functions.

     This limit, with the associated error when it is exceeded, is one
     way that Lisp avoids infinite recursion on an ill-defined function.

     The default value of this variable is 200.  If you set it to a
     value less than 100, Lisp will reset it to 100 if the given value
     is reached.

     `max-specpdl-size' provides another limit on nesting.  Note: Local
     Variables.

 - Variable: values
     The value of this variable is a list of values returned by all
     expressions which were read from buffers (including the
     minibuffer), evaluated, and printed.  The elements are in order,
     most recent first.

          (setq x 1)
               => 1
          (list 'A (1+ 2) auto-save-default)
               => (A 3 t)
          values
               => ((A 3 t) 1 ...)

     This variable is useful for referring back to values of forms
     recently evaluated.  It is generally a bad idea to print the value
     of `values' itself, since this may be very long.  Instead, examine
     particular elements, like this:

          ;; Refer to the most recent evaluation result.
          (nth 0 values)
               => (A 3 t)
          ;; That put a new element on,
          ;;   so all elements move back one.
          (nth 1 values)
               => (A 3 t)
          ;; This gets the element that was next-to-last
          ;;   before this example.
          (nth 3 values)
               => 1


automatically generated by info2www