Skip to main content
Share Your Experience: Take the 2024 Developer Survey
17 votes
Accepted

Why do setq and set quote act differently on let-bound variables with lexical scope?

This is documented behaviour. The (much improved) explanation in the Emacs 25.1 elisp manual is as follows: Note that unlike dynamic variables which are tied to the symbol object itself, the ...
phils's user avatar
  • 51.6k
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

Why does defvar scoping work differently without an initvalue?

Why the two are treated differently is mostly "because that's what we needed". More specifically, the single-argument form of defvar appeared a long time ago, but later than the other and was ...
Stefan's user avatar
  • 26.5k
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
  • 77.9k
6 votes
Accepted

Bringing a function into a lexical environment

You're trying here to access the lexical variable x from outside its lexical scope, which obviously can't work. What you can do is pass a lexical environment to eval: LEXICAL can also be an actual ...
npostavs's user avatar
  • 9,203
6 votes

Why do setq and set quote act differently on let-bound variables with lexical scope?

BTW, one way to think about why it can't work is to remember that lexical scoping enjoys the so-called "α-renaming" property: variable names do not matter, and you (or the compiler) can ...
Stefan's user avatar
  • 26.5k
5 votes

How does scoping work in emacs lisp

All functions and variables defined in a file that gets loaded are put into the global environment. Although Emacs has installable packages, those packages are not isolated from each other in any way. ...
db48x's user avatar
  • 18.3k
5 votes

How to temporarily change the definition of a function?

How to temporarily change the definition of a function? Calling a function by its (symbol) name means calling that symbol's function slot. So, to temporarily change the definition of a function that ...
Basil's user avatar
  • 12.5k
5 votes
Accepted

why is a let binding is ignored in compiled function?

You're compiling the file using lexical binding, so I suspect the problem is that your file does neither of the following: (defvar bibtex-completion-bibliography) (require 'bibtex-completion) either ...
phils's user avatar
  • 51.6k
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
4 votes
Accepted

How to get an insert-text-button 'action property accessing a dynamically-bound buffer-local variable

You can add not only action but any other attributes to a text-button, that can be referred later with button-get function. So saving the (reference to the) current buffer, together with action, seems ...
zk_phi's user avatar
  • 446
4 votes

Why does defvar scoping work differently without an initvalue?

Based on experimentation, I believe the issue is that (defvar VAR) with no init value only has an effect on the library(s) it appears in. When I added (defvar my-dynamic-var) to the *scratch* buffer, ...
phils's user avatar
  • 51.6k
4 votes
Accepted

Why does this straightforward use of a closure fail?

There's a few pitfalls here: Using eval with lexical-binding will fail unless you pass it an alist environment argument holding the bindings in question (see F1 f eval) Looking up symbol slots only ...
wasamasa's user avatar
  • 22.2k
4 votes
Accepted

defun in defun is not local?

Use cl-labels to define local functions (cl-flet works, but recursive function reports error), and in the beginning of the .el file enable the lexical scoping: ;; -*- lexical-binding: t -*- (defun ...
Alaneuler's user avatar
  • 277
4 votes

Accessing the global value of a locally altered variable

In addition to what @cyberbisson has said (no, you cannot) ... First, in this: (setq foo 2) (let ((foo 3)) ;; can I access the original value here? ) there is nothing that says that the (setq ...
Drew's user avatar
  • 77.9k
4 votes
Accepted

Inconsistency in emacs' rules of scope?

Lexical scope (recently added to emacs) changes the rules, and some documentation is still out of date. The current documentation for setq is this manual page which says When dynamic variable binding ...
amitp's user avatar
  • 2,541
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
3 votes
Accepted

Search in readable eww buffer

You seem to be using dynamic binding which means that str would be evaluated once your lambda function is executed at which time the variable str is no longer in scope. You should turn on lexical ...
martin_joerg's user avatar
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

Make a variable visible to some functions only

defalias "magically" stores the variables inside its functions, so their values can be reused with subsequent calls. This is not what defalias does; see (elisp) Defining Functions. The means by which ...
Basil's user avatar
  • 12.5k
3 votes

idiomatic way to lexically scope variables in a cl-loop body

Your problem is that the timer runs the function when the loop has finished. The lexically bound variables index and value have for all calls the values after the last iteration. One solution is to ...
Tobias's user avatar
  • 33.3k
3 votes

Lexical capture/access local scope in function, the same way as lambda

Use flet: (let ((x 1)(y 2)(z 3) (l '(1 2 3 4 5 6 7 8 9))) (flet ((fn (i) (list x y z i))) (mapcar #'fn l)))
db48x's user avatar
  • 18.3k
3 votes
Accepted

Closure does not capture variable

You don't have a closure unless your file has set lexical-binding: t as a file-local variable. Without that, everything is using dynamic binding (in which case in your example your variable is indeed ...
phils's user avatar
  • 51.6k
2 votes

Bringing a function into a lexical environment

What you seem to want is access to the lexical-environment of a function, which would allow something like this (let ... (defun include (lambda-form) (eval lambda-form (this-lexical-...
politza's user avatar
  • 3,346
2 votes
Accepted

Different behavior of same function defined at two places: symbol's value as variable is void

You have a quoted text-to-search in those :action forms (because the entire form is quoted), which therefore is not the same thing as your lexically-bound variable; so when those forms are called as ...
phils's user avatar
  • 51.6k
2 votes
Accepted

Can turning on lexical binding in a legacy init file cause problems?

The lexical-binding settings of the files loaded with load and require are not affected by the (buffer-local) lexical-binding setting of the init file. Warning: But, a change of lexical-binding in ...
Tobias's user avatar
  • 33.3k
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

idiomatic way to lexically scope variables in a cl-loop body

Another option is to use backquoting to substitute the values for each loop iteration directly into the lambda form: ;;; foo.el --- -*- lexical-binding: t; -*- (let ((my-...
phils's user avatar
  • 51.6k

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