0

How can I enable lexical binding in an init.el file that is tangled from an org mode file? I have an org mode file called Emacs.org which is used to tangle the init.el file using the command M-x org-babel-tangle, but I'm not able to enable lexical bindings in this tangled file.

Note that - I have the property header like so: #+PROPERTY: header-args:elisp :lexical t :tangle /file/path

I found this answer in a related question which says that

  • adding #+begin_src elisp :lexical t as the header of the source code block
  • OR
  • using #+PROPERTY: header-args:elisp :lexical t as property header arguments should work.

But I tried this out and it don't seem to work for me (which is also mentioned in a comment in that post by someone else)

1 Answer 1

1

:lexical t works for evaluating elisp code blocks in an Org file. Your question is different: you want the produced init.el to use lexical binding when it is loaded and that buffer is evaluated. In order for that to happen, init.el has to have the following as its very first line:

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

So make sure that the first code block you tangle contains this line. And check by tangling to produce your init.el file and then look at it to make sure that it has the correct first line. See Using Lexical Binding in the Emacs Lisp Ref manual. That section has been renamed and clarified considerably in the (not-yet-releases) Emacs 30 version. It is now called Selecting Lisp Dialect but I cannot find a publicly available HTML version, so this is a link to the TeXinfo sources.

Try this simple example:

* Init .el

#+begin_src elisp :tangle /tmp/init.el
  ;;; -*- lexical-binding: t -*-


  (message "This is init.el. lexical-binding has the value %S" lexical-binding)
#+end_src

Tangle it, load the resulting /tmp/init.el and check the *Messages* buffer to see what the message call printed out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.