My thoughts on this would be that if lexical-bindings is
supposed to be
bound to t, it should be done by eval when it gets a non-nil
value for
it's optional argument. If I execute (eval FORM t) in an emacs
lisp
buffer, it looks like lexical-bind is not set either, so I don't
think
it should be in org either. To set it means we could get the
same code
behaving differently depending on whether the source block is
'tangled'
and then evaluated or evaluated within org, which doesn't feel
right.
I agree that we should avoid inconsistency. However it is already
present.
You can verify this with the following src block:
#+begin_src emacs-lisp :lexical t :tangle ./dynamic.el :results
verbatim
(symbol-value lexical-binding)
#+end_src
1. `org-babel-execute-src-block' with point in the src block =>
nil (lexical binding enabled)
2. `org-edit-special' with point in the src block,
`eval-last-sexp' within *Org Src* buffer => t (lexical binding
enabled)
3. Tangle to the block to "dynamic.el", `eval-last-sexp' within
"dynamic.el" => nil (dynamic binding)
Might be worth asking on emacs-devel why (eval FORM t) does not
set this variable?
I understand why it doesn't. Elisp can be interpreted independent
of a buffer, and different buffers need to distinguish which scope
to use.
It's more a question of what do we gain by leaving it unbound
while evaluating a src block?
A user can rebind `lexical-binding' without hurting anything. The
only case I can think of is if they intend to use it as a free
variable, it won't be free. I can't imagine a use case where that
would be true while the user is also explicitly requesting lexical
binding, though.
IMO, it should be set during evaluation (addressed by my patch).
An alternative solution would be to allow one to set the lexical
environment as part of the header args. e.g.
#+begin_src emacs-ilsp :lexical ((lexical-binding . t) t)
(symbol-value lexical-binding)
#+end_src
That way we offer the full flexibility of `eval' to those who
would use it.
A separate issue, but one worth considering, is whether or not
tangling a `:lexical t` src block should automatically add the
file-local variable line in the tangled file.
Prior confusion (its not just me, I swear):
https://emacs.stackexchange.com/questions/61754/how-can-i-enable-lexical-binding-for-elisp-code-in-org-mode#comment100554_61758
https://lists.gnu.org/archive/html/emacs-orgmode/2019-08/msg00247.html