Search code examples
functionemacselisplexical-scope

Does emacs lisp support lexically redefining a function?


Recent versions of Emacs support lexical binding for variables in elisp code. Is it also possible to lexically redefine functions? In other words, does Emacs Lisp have something like lexical-flet?


Solution

  • In Emacs<24.3, you can (require 'cl) and then use labels. In Emacs-24.3 and up, you can also do (require 'cl-lib) and then use either cl-flet or cl-labels.

    All of those are "complex macros" that generate code that looks like (let ((fun (lambda (args) (body)))) ... (funcall fun my-args) ...), because the underlying language does not natively support local function definitions.