Lexical Bindings
Variable Bindings
Macro Bindings
These forms make let-like bindings to functions instead
of variables.
let-style bindings on the function
cells of symbols rather than on the value cells. Each binding
must be a list of the form `(name arglist forms...)', which defines a function exactly as if
it were a defun* form. The function name is defined
accordingly for the duration of the body of the flet; then
the old function definition, or lack thereof, is restored.
While flet in Common Lisp establishes a lexical binding of
name, Emacs Lisp flet makes a dynamic binding. The
result is that flet affects indirect calls to a function as
well as calls directly inside the flet form itself.
You can use flet to disable or modify the behavior of a
function in a temporary fashion. This will even work on Emacs
primitives, although note that some calls to primitive functions
internal to Emacs are made without going through the symbol's
function cell, and so will not be affected by flet. For
example,
(flet ((message (&rest args) (push args saved-msgs))) (do-something))
This code attempts to replace the built-in function message
with a function that simply saves the messages in a list rather
than displaying them. The original definition of message
will be restored after do-something exits. This code will
work fine on messages generated by other Lisp code, but messages
generated directly inside Emacs will not be caught since they make
direct C-language calls to the message routines rather than going
through the Lisp message function.
Functions defined by flet may use the full Common Lisp
argument notation supported by defun*; also, the function
body is enclosed in an implicit block as if by defun*.
See Program Structure.
labels form is like flet, except that it
makes lexical bindings of the function names rather than
dynamic bindings. (In true Common Lisp, both flet and
labels make lexical bindings of slightly different sorts;
since Emacs Lisp is dynamically bound by default, it seemed
more appropriate for flet also to use dynamic binding.
The labels form, with its lexical binding, is fully
compatible with Common Lisp.)
Lexical scoping means that all references to the named
functions must appear physically within the body of the
labels form. References may appear both in the body
forms of labels itself, and in the bodies of
the functions themselves. Thus, labels can define
local recursive functions, or mutually-recursive sets of
functions.
A ``reference'' to a function name is either a call to that
function, or a use of its name quoted by quote or
function to be passed on to, say, mapcar.