Next: , Previous: , Up: Variable Bindings   [Contents][Index]


4.3.3 Function Bindings

These forms make let-like bindings to functions instead of variables.

Special Form: flet (bindings…) forms…

This form establishes 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.

Special Form: labels (bindings…) forms…

The labels form is a synonym for flet. (In Common Lisp, labels and flet differ in ways that depend on their lexical scoping; these distinctions vanish in dynamically scoped Emacs Lisp.)


Next: , Previous: , Up: Variable Bindings   [Contents][Index]