Function: eval-buffer

eval-buffer is an interactive function defined in lread.c.

Signature

(eval-buffer &optional BUFFER PRINTFLAG FILENAME UNIBYTE DO-ALLOW-PRINT)

Documentation

Execute the accessible portion of current buffer as Lisp code.

You can use C-x n n (narrow-to-region) to limit the part of buffer to be evaluated. When called from a Lisp program (i.e., not interactively), this function accepts up to five optional arguments: BUFFER is the buffer to evaluate (nil means use current buffer),
 or a name of a buffer (a string).
PRINTFLAG controls printing of output by any output functions in the
 evaluated code, such as print, princ, and prin1:
  a value of nil means discard it; anything else is the stream to print to.
  See Info node (elisp)Output Streams for details on streams.
FILENAME specifies the file name to use for load-history. UNIBYTE is obsolete and ignored. DO-ALLOW-PRINT, if non-nil, specifies that output functions in the
 evaluated code should work normally even if PRINTFLAG is nil, in
 which case the output is displayed in the echo area.

This function ignores the global value of the lexical-binding variable. Instead it will heed the buffer-local value of that variable and any
  -*- lexical-binding: t -*-
settings in the buffer; if there is no such setting, and the buffer-local value of the variable is nil, the buffer will be evaluated with the value of lexical binding equal to its top-level default value, as returned by default-toplevel-value.

This function preserves the position of point.

View in manual

Key Bindings

Source Code

// Defined in /usr/src/emacs/src/lread.c
{
  specpdl_ref count = SPECPDL_INDEX ();
  Lisp_Object tem, buf;

  if (NILP (buffer))
    buf = Fcurrent_buffer ();
  else
    buf = Fget_buffer (buffer);
  if (NILP (buf))
    error ("No such buffer");

  if (NILP (printflag) && NILP (do_allow_print))
    tem = Qsymbolp;
  else
    tem = printflag;

  if (NILP (filename))
    filename = BVAR (XBUFFER (buf), filename);

  specbind (Qeval_buffer_list, Fcons (buf, Veval_buffer_list));
  specbind (Qstandard_output, tem);
  record_unwind_protect_excursion ();
  BUF_TEMP_SET_PT (XBUFFER (buf), BUF_BEGV (XBUFFER (buf)));
  /* Don't emit a warning about 'lexical-binding' if it already has a
     local binding in the buffer.  */
  if (NILP (Flocal_variable_p (Qlexical_binding, buf)))
    specbind (Qlexical_binding, get_lexical_binding (buf, buf));
  BUF_TEMP_SET_PT (XBUFFER (buf), BUF_BEGV (XBUFFER (buf)));
  readevalloop (buf, 0, filename,
		!NILP (printflag), Qnil, Qnil, Qnil);
  return unbind_to (count, Qnil);
}