Skip to main content
11 votes
Accepted

Global lexical-binding setting?

Elisp AFAIK the only two reliable ways for using lexical binding in Elisp files are: Setting the buffer-local variable lexical-binding as file-local variable at the beginning of the Elisp file with ;...
Tobias's user avatar
  • 33.3k
10 votes
Accepted

How to make sure no global variables have been created in a piece of lisp code?

Is it possible to automatically verify that a piece of lisp code does not create any new global variables? Turn on lexical-binding: ;;; foo.el --- just frobnicating some foo -*- lexical-binding: t -*-...
Basil's user avatar
  • 12.5k
9 votes
Accepted

Defun inside let with lexical binding gives byte-compile warning "the function is not known to be defined"

The byte-compiler's way to decide whether a function will be defined or not is very "naive" and gets fooled even in your "obvious" case. But you can write it in a way that lets the compiler understand ...
Stefan's user avatar
  • 26.5k
8 votes
Accepted

Variable scope issue in scratch buffer?

Your Emacs version (29, most likely) has silently changed the value of lexical-binding from nil to t in the *scratch* buffer. When you use C-M-x to evaluate only those functions that variable isn't ...
Drew's user avatar
  • 78k
8 votes
Accepted

How to make local function binding for closure?

Without cl-lib: ;; -*- lexical-binding: t; -*- (defun nats () (letrec ((inner (lambda (n) (cons n (lambda () (funcall inner (1+ n))))))) (funcall inner 0))) (let* ((stream (nats)) (i (...
wasamasa's user avatar
  • 22.2k
7 votes
Accepted

About closure creation

You asked: Will be useful to write/build literal closures? Should it be a good practice? any example? Why the closure is not a real type instead of cons? All three questions are answered ...
Tobias's user avatar
  • 33.3k
6 votes

Defun inside let with lexical binding gives byte-compile warning "the function is not known to be defined"

To suppress the byte-compiler warning, try adding this before your code, starting in column 0 (leftmost): (declare-function increase-count "your-file-name.el") C-h f declare-function tells you: ...
Drew's user avatar
  • 78k
6 votes
Accepted

How can I enable lexical binding for Elisp code in org-mode?

Turn on the header :lexical, e.g., #+begin_src elisp :lexical t :results pp (lambda ()) #+end_src #+RESULTS: : (closure : (t) : nil) You can also use #+PROPERTY: header-args:elisp :lexical t to ...
xuchunyang's user avatar
  • 14.6k
6 votes
Accepted

What type of variable binding is setq creating?

Quoth the docstring of special-variable-p: special-variable-p is a built-in function in `src/eval.c'. (special-variable-p SYMBOL) Probably introduced at or before Emacs version 24.1. Return non-...
Basil's user avatar
  • 12.5k
5 votes
Accepted

Reliable lexical binding?

Just use an uninterned symbol that cannot be reached outside of the let. You need that uninterned symbol two times, one time for the let and one time within foo. The purpose of the additional outer ...
Tobias's user avatar
  • 33.3k
5 votes
Accepted

Updating code from lexical-let to lexical-binding: t

This code shows an elisp function (third example) using lexical-let This is the (renamed) third example in the linked page: (eval-when-compile (require 'cl)) ;; for `lexical-let' macro (defun ...
Basil's user avatar
  • 12.5k
5 votes
Accepted

Can I determine the difference of declaring (and not declaring) a file to use lexical-binding?

Here's a trivial example: ;; -*- lexical-binding: t; -*- (defun foo () x) (let ((x 'y)) (foo)) Save that file and use M-x load-file to load it, and you will get an error: let: Symbol’s value ...
phils's user avatar
  • 51.7k
4 votes
Accepted

Using new features in emacs 27 generators

TL;DR M-: (setq lexical-binding t) Details A precondition for iterators in Emacs to work is that the variable binding is set to lexical binding for the respective file or buffer. Evaluate (setq ...
Marco Wahl's user avatar
  • 2,886
3 votes

Defun inside let with lexical binding gives byte-compile warning "the function is not known to be defined"

I believe placing the definition in question within eval-and-compile would also superficially achieve the same result as in Stefan's correct answer: (eval-and-compile (let ((count 0)) (defun ...
Basil's user avatar
  • 12.5k
3 votes

How to avoid use of `lexical-let`

Your example is likely not representative of the actual code you're using, but a good solution might be to create the closure in the macro and return the closure, instead of returning code which may ...
Stefan's user avatar
  • 26.5k
2 votes

How to avoid use of `lexical-let`

There is an ugly workaround using eval with the optional argument LEXICAL set to t. (defmacro repro () (let ((kmap-sym (gensym "kmap-"))) `(eval '(let ((,kmap-sym (make-sparse-keymap))) ...
Tobias's user avatar
  • 33.3k
2 votes
Accepted

lexical-binding: How to create a let-bound function that recursively calls itself

Your problem is that the let-bound symbol the-fn is not yet available when you define the lambda. Use letrec instead of let*. It first binds all variables and afterwards sets them. Example: (defun my-...
Tobias's user avatar
  • 33.3k
2 votes
Accepted

why lexical environment alist ends with a `t'?

Try with a function that takes an argument and has a body: (let ((foo 233)) (lambda (x) (* x foo))) ;; ==> (closure ((foo . 233)) (x) (* x foo)) The Closures section of the Elisp Reference manual ...
NickD's user avatar
  • 30.9k
2 votes
Accepted

How do I call a function within a lambda?

The symbol FUNCTION in your definition of my-func has a local binding for the symbol FUNCTION as variable and not as function. So you need to call (funcall FUNCTION) in your lambda. Furthermore, be ...
Tobias's user avatar
  • 33.3k
1 vote
Accepted

lexical binding in a tangled init.el file

: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 ...
NickD's user avatar
  • 30.9k
1 vote

Any way to access a lexical let variable outside of the let?

The answer can be split into a general answer to the question as stated in the caption Any way to access a lexical let variable outside of the let? and an answer to your specific problem in the ...
Tobias's user avatar
  • 33.3k
1 vote

Any way to access a lexical let variable outside of the let?

You can't do that and you shouldn't even try: let blocks are meant to define variables that are local in scope and limited in lifetime - they spring into existence when the block is entered and they ...
NickD's user avatar
  • 30.9k
1 vote
Accepted

Warnings about malformed functions when using lexical-let

The elisp linter is warning me that ‘(line-num line-num)’ is a malformed function. The elisp linter is saying that the list (line-num line-num) doesn't make sense as a function object. The reason it ...
mmarshall540's user avatar
1 vote

Lexical binding in a process filter

In a private email, Christopher Wellons answered my question. The problem is that server-process is a special variable defined in server.el. Special variables are always dynamically bound. As a result,...
Damien Cassou's user avatar
1 vote

byte-compile and lexical-binding

The lexical-binding is a setting that by-and-large operates at compile-time and not at run-time (more specifically, it applies to code that's being manipulated rather than code that's being executed). ...
Stefan's user avatar
  • 26.5k
1 vote
Accepted

How to avoid use of `lexical-let`

Not very sure if the result meets your expectation. (defun foo (keymap) `(lambda () (interactive) (message "kmap is %s" ',keymap))) (defmacro repro () (let ((kmap-sym (gensym "kmap-"))) `(...
xuchunyang's user avatar
  • 14.6k

Only top scored, non community-wiki answers of a minimum length are eligible