Next: Obsolete Setf Customization, Previous: Obsolete Lexical Binding, Up: Obsolete Features
The following macros are obsolete, and are replaced by versions with a ‘cl-’ prefix that do not behave in exactly the same way. Consequently, the cl.el versions are not simply aliases to the cl-lib.el versions.
This macro is replaced by
cl-flet
(see Function Bindings), which behaves the same way as Common Lisp'sflet
. Thisflet
takes the same arguments ascl-flet
, but does not behave in precisely the same way.While
flet
in Common Lisp establishes a lexical function binding, thisflet
makes a dynamic binding (it dates from a time before Emacs had lexical binding). The result is thatflet
affects indirect calls to a function as well as calls directly inside theflet
form itself.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 ofmessage
will be restored afterdo-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 Lispmessage
function.For those cases where the dynamic scoping of
flet
is desired,cl-flet
is clearly not a substitute. The most direct replacement would be instead to usecl-letf
to temporarily rebind(symbol-function '
fun)
. But in most cases, a better substitute is to use advice, such as:(defvar my-fun-advice-enable nil) (add-advice 'fun :around (lambda (orig &rest args) (if my-fun-advice-enable (do-something) (apply orig args))))so that you can then replace the
flet
with a simple dynamically scoped binding ofmy-fun-advice-enable
.Note that many primitives (e.g.,
+
) have special byte-compile handling. Attempts to redefine such functions usingflet
,cl-letf
, or advice will fail when byte-compiled.
This macro is replaced by
cl-labels
(see Function Bindings), which behaves the same way as Common Lisp'slabels
. Thislabels
takes the same arguments ascl-labels
, but does not behave in precisely the same way.This version of
labels
uses the obsoletelexical-let
form (see Obsolete Lexical Binding), rather than the true lexical binding thatcl-labels
uses.