Previous: Quoting, Up: Evaluation
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 (see Loading).
Note: it is generally cleaner and more flexible to store a
function in a data structure, and call it with funcall
or
apply
, than to store an expression in the data structure and
evaluate it. Using functions provides the ability to pass information
to them as arguments.
This is the basic function evaluating an expression. It evaluates form in the current environment and returns the result. How the evaluation proceeds depends on the type of the object (see Forms).
Since
eval
is a function, the argument expression that appears in a call toeval
is evaluated twice: once as preparation beforeeval
is called, and again by theeval
function itself. Here is an example:(setq foo 'bar) => bar (setq bar 'baz) => baz ;; Hereeval
receives argumentfoo
(eval 'foo) => bar ;; Hereeval
receives argumentbar
, which is the value offoo
(eval foo) => bazThe number of currently active calls to
eval
is limited tomax-lisp-eval-depth
(see below).
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 non-
nil
, the values that result from evaluating the expressions in the region are printed using stream. See Output Streams.If read-function is non-
nil
, it should be a function, which is used instead ofread
to read expressions one by one. This function is called with one argument, the stream for reading input. You can also use the variableload-read-function
(see How Programs Do Loading) to specify this function, but it is more robust to use the read-function argument.
eval-region
always returnsnil
.
This is like
eval-region
except that it operates on the whole buffer.
This variable defines the maximum depth allowed in calls to
eval
,apply
, andfuncall
before an error is signaled (with error message"Lisp nesting exceeds max-lisp-eval-depth"
). This limit, with the associated error when it is exceeded, is one way that Lisp avoids infinite recursion on an ill-defined function. The depth limit counts internal uses ofeval
,apply
, andfuncall
, such as for calling the functions mentioned in Lisp expressions, and recursive evaluation of function call arguments and function body forms, as well as explicit calls in Lisp code.The default value of this variable is 300. If you set it to a value less than 100, Lisp will reset it to 100 if the given value is reached. Entry to the Lisp debugger increases the value, if there is little room left, to make sure the debugger itself has room to execute.
max-specpdl-size
provides another limit on nesting. See Local Variables.
The value of this variable is a list of the values returned by all the expressions that were read, evaluated, and printed from buffers (including the minibuffer) by the standard Emacs commands which do this. The elements are ordered 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-most-recent ;; before this example. (nth 3 values) => 1