manpagez: man pages & more
info emacs
Home | html | info | man
[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

32.9 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 commands).

M-:

Read a single Lisp expression in the minibuffer, evaluate it, and print the value in the echo area (eval-expression).

C-x C-e

Evaluate the Lisp expression before point, and print the value in the echo area (eval-last-sexp).

C-M-x

Evaluate the defun containing or after point, and print the value in the echo area (eval-defun).

M-x eval-region

Evaluate all the Lisp expressions in the region.

M-x eval-buffer

Evaluate all the Lisp expressions in the buffer.

M-: (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-: was typed.

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.

C-M-x treats defvar expressions specially. Normally, evaluating a defvar expression does nothing if the variable it defines already has a value. But C-M-x unconditionally resets the variable to the initial value specified in the defvar expression. defcustom expressions are treated similarly. This special feature is convenient for debugging Lisp programs. Typing C-M-x on a defface expression reinitializes the face according to the defface specification.

The command C-x C-e (eval-last-sexp) evaluates the Lisp expression preceding point in the buffer, and displays the value in the echo area. It is available in all major modes, not just Emacs-Lisp mode. It does not treat defvar specially.

When the result of an evaluation is an integer, you can type C-x C-e a second time to display the value of the integer result in additional formats (octal, hexadecimal, and character).

If C-x C-e, or M-: is given a numeric argument, it inserts the value into the current buffer at point, rather than displaying it in the echo area. The argument's value does not matter. C-M-x with a numeric argument instruments the function definition for Edebug (see Instrumenting for Edebug: (elisp)Instrumenting section `Instrumenting' in the Emacs Lisp Reference Manual).

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-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 ready to test. Later, as you find bugs and change individual functions, use C-M-x on each function that you change. This keeps the Lisp world in step with the source file.

The two customizable variables eval-expression-print-level and eval-expression-print-length control the maximum depth and length of lists to print in the result of the evaluation commands before abbreviating them. eval-expression-debug-on-error controls whether evaluation errors invoke the debugger when these commands are used; its default is t.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]
© manpagez.com 2000-2024
Individual documents may contain additional copyright information.