These forms make let-like bindings to functions instead
of variables.
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 adefun*form. The function name is defined accordingly for the duration of the body of theflet; then the old function definition, or lack thereof, is restored.While
fletin Common Lisp establishes a lexical binding of name, Emacs Lispfletmakes a dynamic binding. The result is thatfletaffects indirect calls to a function as well as calls directly inside thefletform itself.You can use
fletto 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 byflet. For example,(flet ((message (&rest args) (push args saved-msgs))) (do-something))This code attempts to replace the built-in function
messagewith a function that simply saves the messages in a list rather than displaying them. The original definition ofmessagewill be restored afterdo-somethingexits. 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 Lispmessagefunction.Functions defined by
fletmay use the full Common Lisp argument notation supported bydefun*; also, the function body is enclosed in an implicit block as if bydefun*. See Program Structure.