Lexical Scoping In Emacs Lisp

I've said before that Emacs Lisp is pretty much like other Lisps but one area where that is not true is in variable scoping. Common Lisp and Scheme both enjoy lexical scoping whereas Emacs Lisp has dynamic scoping. Until you actually use lexical scoping and see what it can do, this seems like an esoteric point that most programmers needn't worry about. Emacs 24 introduces support for lexical scoping so Elisp programmers can finally make use of this powerful mechanism.

Yoo Box has a long post on lexical scoping and dynamic scoping in Emacs Lisp. It turns out that you can enable lexical scoping for an Elisp file by adding

;;; -*- lexical-binding: t -*-

to the top of the file. If you're already familiar with lexical scoping that's just about everything you need to know. If not, then Box has an excellent and detailed explanation of the differences between lexical and dynamic scoping with plenty of examples. The examples are short and seem simple but they require careful study to get their full value.

I really recommend Box's post if lexical scoping is new to you. A careful reading will pay dividends. For the rest of us, it's really great that Emacs is finally supporting lexical scoping. It's still not Common Lisp but it's a huge improvement.

This entry was posted in Programming and tagged , . Bookmark the permalink.
  • Phil

    Dynamic binding is such an amazing win when it comes to customising Emacs that personally I'm a little bit worried about this development.

    If people start using lexical binding as standard in their code, it will surely make it much harder to customise the behaviour of libraries without modifying the code directly.

    It's not always going to be obvious to a developer which variables people may find a need to manipulate, so I really do hope that elisp coders will be suitably conservative with this.

    • jcs

      That's a potential problem, I guess, but defvar variables will still be special (dynamic) so the natural way to code will pretty much take care of making the variables you want to override be dynamic. It is something for Elisp coders to keep in mind though.

  • i feel that it might be better if emacs used a new function name for it instead. e.g. (llet (...) ...)

    given the way it is (setting lexical-binding to t), i'm not sure i'll ever use it myself.

    • jcs

      I anticipate that most people will either ignore the issue altogether or will routinely set lexical scoping in every file they write. Most of the first group will probably be those whose only Lisp experience is Elisp. Most of the second group will be those who are familiar with other Lisps and are used to having local variables be lexically scoped.

      I agree that the current method is less than ideal but I can't think of a better way of doing it other than, perhaps, using a different let form as you suggested.

    • Arvid Jakobsson

      There is (lexical-let () ...) no?

  • Kevin Brubeck Unhammer

    According to the linked post, you can lexically scope part of a file with:
    (eval
    '(lexicallyscopedcode)
    t)