Next: External Lisp Prev: Lisp Interaction Up: Building
Evaluating Emacs-Lisp Expressions
Lisp programs intended to be run in Emacs should be edited in
Emacs-Lisp mode; this happens automatically for file names ending in
`.el'. By contrast, Lisp mode itself is used for editing Lisp programs
intended for other Lisp systems. To switch to Emacs-Lisp mode
explicitly, use the command `M-x emacs-lisp-mode'.
For testing of Lisp programs to run in Emacs, it is often useful to
evaluate part of the program as it is found in the Emacs buffer. For
example, after changing the text of a Lisp function definition,
evaluating the definition installs the change for future calls to the
function. Evaluation of Lisp expressions is also useful in any kind of
editing, for invoking noninteractive functions (functions that are not
Read a single Lisp expression in the minibuffer, evaluate it, and
print the value in the echo area (`eval-expression').
Evaluate the Lisp expression before point, and print the value in
the echo area (`eval-last-sexp').
Evaluate the defun containing or after point, and print the value
in the echo area (`eval-defun').
Evaluate all the Lisp expressions in the region.
Evaluate all the Lisp expressions in the buffer.
`M-ESC' (`eval-expression') is the most basic command for evaluating
a Lisp expression interactively. It reads the expression using the
minibuffer, so you can execute any expression on a buffer regardless of
what the buffer contains. When the expression is evaluated, the current
buffer is once again the buffer that was current when `M-ESC' was typed.
`M-ESC' can easily confuse users who do not understand it,
especially on keyboards with autorepeat where it can result from holding
down the ESC key for too long. Therefore, `eval-expression' is
normally a disabled command. Attempting to use this command asks for
confirmation and gives you the option of enabling it; once you enable
the command, confirmation will no longer be required for it. *Note
In Emacs-Lisp mode, the key `C-M-x' is bound to the command
`eval-defun', which parses the defun containing or following point as a
Lisp expression and evaluates it. The value is printed in the echo
area. This command is convenient for installing in the Lisp environment
changes that you have just made in the text of a function definition.
The command `C-x C-e' (`eval-last-sexp') performs a similar job but
is available in all major modes, not just Emacs-Lisp mode. It finds
the sexp before point, reads it as a Lisp expression, evaluates it, and
prints the value in the echo area. It is sometimes useful to type in an
expression and then, with point still after it, type `C-x C-e'.
If `C-M-x' or `C-x C-e' is given a numeric argument, it prints the
value by insertion into the current buffer at point, rather than in the
echo area. The argument value does not matter.
The most general command for evaluating Lisp expressions from a
buffer is `eval-region'. `M-x eval-region' parses the text of the
region as one or more Lisp expressions, evaluating them one by one.
`M-x eval-current-buffer' is similar but evaluates the entire buffer.
This is a reasonable way to install the contents of a file of Lisp code
that you are just ready to test. After finding and fixing a bug, use
`C-M-x' on each function that you change, to keep the Lisp world in
step with the source file.
automatically generated by info2www