Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8543487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:18:48+00:00 2026-06-11T12:18:48+00:00

Emacs 24 added optional lexical bindings for local variables. I would like to use

  • 0

Emacs 24 added optional lexical bindings for local variables. I would like to use this functionality in my module, while maintaining compatibility with XEmacs and the previous Emacs versions.

Before Emacs 24, the easiest way to get closures was by using the lexical-let form defined in cl-macs, which emulated lexical scope with some clever macro trickery. While this was never hugely popular among elisp programmers, it did work, creating real and efficient closures, as long as you remembered to wrap them in lexical-let, as in this pseudocode:

(defun foo-open-tag (tag data)
  "Maybe open TAG and return a function that closes it." 
  ... do the work here, initializing state ...
  ;; return a closure that explicitly captures internal state
  (lexical-let ((var1 var1) (var2 var2) ...)
    (lambda ()
      ... use the captured vars without exposing them to the caller ...
      )))

The question is: what is the best way to use the new lexical bindings, while retaining support for Emacs 23 and for XEmacs? Currently I solved it by defining a package-specific macro that expands into lexical-let or into ordinary let depending on whether lexical-binding is bound and true:

(defmacro foo-lexlet (&rest letforms)
  (if (and (boundp 'lexical-binding)
           lexical-binding)
      `(let ,@letforms)
    `(lexical-let ,@letforms)))
(put 'foo-lexlet 'lisp-indent-function 1)

... at the end of file, turn on lexical binding if available:
;; Local Variables:
;; lexical-binding: t
;; End:

This solution works, but it feels clunky because the new special form is non-standard, doesn’t highlight properly, can’t be stepped into under edebug, and generally draws attention to itself. Is there a better way?


EDIT

Two examples of ideas for smarter (not necessarily good) solutions that allow the code to continue using standard forms to create closures:

  • Use an advice or a compiler macro to make lexical-let expand to let under lexical-bindings iff the lexical-let only assigns to symbols which are lexically scoped anyway. This advice would only be temporarily activated during byte-compilation of foo.el, so that the meaning of lexical-let remains unchanged for the rest of Emacs.

  • Use a macro/code-walker facility to compile let of non-prefixed symbols to lexical-let under older Emacsen. This would again only apply during byte-compilation of foo.el.

Do not be alarmed if these ideas smell of overengineering: I am not proposing to use them as-is. I’m interested in the alternatives to the above macro where the package gets the benefit of nicer portable usage of closures for the price of some additional complexity of loading/compilation.


EDIT 2

As no one has stepped up with a solution that would allow the module to keep using let or lexical-let without breaking them for the rest of Emacs, I am accepting Stefan’s answer, which states that the above macro is the way to do it. In addition to that, the answer improves on my code by using bound-and-true-p and adding an elegant declaration for edebug and lisp-indent.

If someone has an alternative proposal for this compatibility layer, or an elegant implementation of the above ideas, I encourage them to answer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:18:49+00:00Added an answer on June 11, 2026 at 12:18 pm

    Since lexical-let and lexical-binding’s let do not do quite the same (more specifically lexical-let always uses lexical binding, whereas let uses either dynamic binding or lexical binding depending on whether the var was defvar‘d or not), I think your approach is about as good as it gets. You can easily make Edebug step into it, tho:

    (defmacro foo-lexlet (&rest letforms)
      (declare (indent 1) (debug let))
      (if (bound-and-true-p lexical-binding)
          `(let ,@letforms)
        `(lexical-let ,@letforms)))
    

    If you don’t want to depend on declare, you can use (put 'foo-lexlet 'edebug-form-spec 'let).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have which-function-mode on by default when I open up Emacs.
Emacs 23 just added the functionality to change the alpha value of the current
Following this web page , I have edited my ~/.emacs file and added the
Any other tweaks for making emacs as vim-like as possible would be appreciated as
I'm setting up emacs for Ruby on Rails development, and would like the ECB
Following this pdf document I added the following to my ~/.emacs file: (load auctex.el
I've placed the dired plus file in ~/.emacs.d , added (require 'dired+) to my
Emacs and Java change propagation Hi, I'm mostly used to code in IDE like
Emacs seems to save synchronously, i.e. the interface locks up during a save. This
Emacs 23.1.1 Ediff 2.81.2 on CentOs 6 Prior to any customization, Ediff would, by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.