Questions tagged [lexical-scoping]
The lexical-scoping tag has no usage guidance.
49
questions
1
vote
2
answers
31
views
Closure does not capture variable
I wrote the following code, but it does not work:
(progn
(define-prefix-command 'unicode-shortcut-keymap)
(dolist (mapping '(("- >" . "→")))
(let ((keys (car mapping))
...
0
votes
0
answers
40
views
dynamic scoping doesn't perform as expected
(require 'cl)
(require 'seq)
(require 'dash)
(eval-when-compile
(require 'cl-lib)
(require 'subr-x)
(require 'env)
(require 'json))
(defgroup chatgpt nil
"ChatGPT frontend."
:...
2
votes
1
answer
60
views
`make-local-variable` Behaves Differently depending on Whether Variable is Special
Two examples (both are in the *scratch* buffer and under lexical scoping rule):
_
(setq xx :default)
(let ((xx :let))
(with-current-buffer (get-buffer-create "tmp")
(make-local-...
2
votes
2
answers
194
views
Lexical capture/access local scope in function, the same way as lambda
I'd like to use a function reference in place of a lambda, however the lambda in question captures a lot of locally scoped vars declared in an enclosing let.
For example:
(let ((x 1)(y 2)(z 3)
(...
0
votes
2
answers
208
views
How to fix byte compiler warnings for "Unused lexical variable" in a macro?
I have a macro and a function which uses it:
(defmacro oauth2-auto--query-case (&rest cases)
"Handle HTTP queries based on the keys present in ‘query-alist’.
‘query-alist’ is a free ...
1
vote
1
answer
91
views
Warning: assignment to free variable ‘skeleton-pair’
Am using the following function, but getting the error
In bracemk-balance:
fencones.el:109:9:Warning: assignment to free variable
‘skeleton-pair’
What is wrong with this?
(defun bracemk-balance ()...
2
votes
1
answer
324
views
Reliable lexical binding?
How does one reliably create a lexically bound/scoped variable? Consider the following:
;; -*- lexical-binding: t; -*-
(let ((f load-file-name))
(defun foo () f))
Normally f would be lexically ...
0
votes
3
answers
179
views
How did `cl-flet` got circumvented?
I want to slightly alter the behavior of counsel-fzf by locally changing the definition of counsel-fzf-action, which activates in the end of the definition of the former. See the source code of ...
4
votes
1
answer
68
views
why is a let binding is ignored in compiled function?
I have this function which works perfectly in an uncompiled function. It should let-bind the variable bibtex-completion-bibliography to the result of (org-ref-find-bibliography).
(defun org-ref-...
1
vote
1
answer
210
views
How to temporarily change the definition of a function?
In the following code the function foo calls the outer f2 function. How to make it call the anonymous function bound within the let?
(defun f2 ()
(message "outer f2"))
(defun foo ()
(f2)...
1
vote
1
answer
53
views
Inconsistency in emacs' rules of scope?
I have always felt that emacs' rules of scope are a bit strange
and prone to contradictions. In fact I have even attempted to file
bug reports just to be told that my interpretation of the rules of ...
1
vote
1
answer
61
views
Top-level variables, local variables, variable scoping and the difference between set and setq
I've been reading about emacs dynamic and lexical bindings. While I generally get the difference between the two types, there is one example that is not clear to me.
I've checked this question and I ...
1
vote
1
answer
62
views
Referencing Lexically Bound Variable in Mocked Function
I'd like to mock a function for a unit test that I'm writing. I want the mocked function to count how many times it was called but I'm having issues related to the scope of the count variable.
Here ...
3
votes
3
answers
301
views
How does scoping work in emacs lisp
A lot of emacs configurations shared publicly have this format:
;; 01-something.el
(provide 'something)
;; init.el
(require 'something)
Suppose I'm writing a defun named "s-join" inside 01-...
1
vote
1
answer
117
views
Void-variable error when lambda function calls itself
Here's a simple test:
(let ((func (lambda (i)
(when (> i 0)
(print i)
(funcall func (1- i))))))
(funcall func 3))
It says (void-variable func), but ...
1
vote
1
answer
59
views
Lexical binding in a process filter
The code constantly prints server-process: nil instead of the server process name:
-*- lexical-binding: t; -*-
(let* ((port 1234)
(server-process 'something))
(setq server-process
(...
4
votes
3
answers
229
views
idiomatic way to lexically scope variables in a cl-loop body
Here is some asynchronous code in a cl-loop:
;;; foo.el --- -*- lexical-binding: t; -*-
(let ((my-list '(a b c)))
(cl-loop for index below (length my-list)
...
4
votes
1
answer
661
views
Can turning on lexical binding in a legacy init file cause problems?
AFAIK turning on lexical binding only affects the current buffer. But if I add lexical scoped code to my crusty old init file which have load and require statements in it (not everything is autoloaded)...
0
votes
1
answer
114
views
Search in readable eww buffer [duplicate]
I would like to open url in eww in readable-mode and find str.
Below is my code that doesn't work:
(defun my-eww-searh-readable (str)
"Once-off call to `my-eww-searh-readable' after EWW is done ...
2
votes
2
answers
533
views
Accessing the global value of a locally altered variable
Is it possible to access the global value of a variable that has been
changed locally?
(setq foo 2)
(let ((foo 3))
;; can I access the original value here?
)
3
votes
2
answers
250
views
byte-compile and lexical-binding
Could someone in the know please help me understand what's going on here:
;; -*- lexical-binding: t; -*-
(let* ((a 0))
(let* ((a 1))
(funcall (byte-compile (lambda () a)))))
;;=> 0
wat? Not ...
5
votes
2
answers
1k
views
defun in defun is not local?
I am reading SICP to learn Lisp, the book uses Scheme dialect. I want accommodate it to emacs lisp, I find something weird in 3.1.1 and cannot figure it out, say I have a test.el file:
(defun make-...
5
votes
1
answer
476
views
About closure creation
After reading this article about readable closures, I check that:
Since closures are byte-code function objects, they print readably.
You can capture an environment in a closure, serialize it, ...
0
votes
1
answer
169
views
Forcing lexical-scope in the middle of dynamic-scope
Today I ran head first into lexical vs dynamic scope in Emacs Lisp. Gist of it is I have a macro defined in lexical scope that expands into a `defun'. Let's say something simple like this:
;; -*- ...
3
votes
3
answers
895
views
How to avoid use of `lexical-let`
I have a macro that intends to creates a closure:
; -*- lexical-binding: t -*-
(defmacro repro ()
(let ((kmap-sym (gensym "kmap-")))
`(let ((,kmap-sym (make-sparse-keymap)))
(define-key ,...
0
votes
1
answer
49
views
capture and replay a closure later
I am having a problem working with closures. I hope I can explain this clearly. When I try to start a compilation I typically do it like this:
(let ((dir (get-dir))
(model-root root)
(default-...
10
votes
3
answers
371
views
Why does defvar scoping work differently without an initvalue?
Suppose I have a file named elisp-defvar-test.el containing:
;;; elisp-defvar-test.el --- -*- lexical-binding: t -*-
(defvar my-dynamic-var)
(defun f1 (x)
"Should return X."
(let ((my-dynamic-...
4
votes
1
answer
244
views
Make a variable visible to some functions only
Consider the following metacode:
;;; -*- lexical-binding: t -*-
(defvar var1 ...)
(defvar var2 ...)
(defun main ()
"Main entry point" ...)
(defun func1 ...)
(defun func2 ...)
...
(defun funcn ...)...
0
votes
2
answers
77
views
Different behavior of same function defined at two places: symbol's value as variable is void
I implemented a couple of extra functions that work with projectile and have been keeping them in my dotfiles as they are not yet a part of the original package. When the functions are defined in the ...
2
votes
1
answer
234
views
What effect does eval-and-compile have on a defun-within-let?
The byte-compiler does not currently seem to recognise that a function has been defined when using defun within a let expression under lexical-binding, as explained in Stefan's answer to this question....
2
votes
1
answer
340
views
Why does this straightforward use of a closure fail?
Suppose you save the following code to some file test.el and then do load-file test.el
;;; -*- lexical-binding: t -*-
(let ((closure-vbl 0))
(defun tst ()
(eval 'closure-vbl)))
(tst)
You get ...
16
votes
3
answers
754
views
Defun inside let with lexical binding gives byte-compile warning "the function is not known to be defined"
I want to get the effect of a static variable by using defun inside of let with lexical binding to create a closure. However, when byte-compiling the file, I get a warning. Am I doing something wrong, ...
2
votes
3
answers
337
views
Bringing a function into a lexical environment
I'm using a closure to keep track of some stuff to do with state; I want to be
able to ‘include’ a function into that closure, so it can access the state. The
only way this is possible is by creating ...
18
votes
2
answers
814
views
Why do setq and set quote act differently on let-bound variables with lexical scope?
I had a bug in one of my extensions that eventually turned out to be caused by set not working as I expected:
;; -*- lexical-binding: t -*-
(let ((a nil))
(setq a t)
(print a))
(let ((a nil))
...
1
vote
2
answers
601
views
How to get an insert-text-button 'action property accessing a dynamically-bound buffer-local variable
In realgud I have a text button that needs access to a dynamically-bound buffer-local variable.
The situation occurs like this. Inside one buffer that has the buffer local variable, there is a ...
2
votes
2
answers
148
views
Increase the level of dynamic binding with backtick / comma
I have a favorite function that contains several levels of set-process-sentinel and lambda sentinels. I have been using lexical-let, but would like to see if it is possible to use dynamically bound ...
18
votes
1
answer
8k
views
Dealing with "Warning: assignment to free variable" when certain libraries can be missing by design
Byte compilation of my mode:
(defun dict-setup-expansions ()
"Add `dict-mode' specific expansions."
(set (make-local-variable 'er/try-expand-list) (list #'dict-mark-article)))
gives ...
2
votes
1
answer
102
views
Allow custom function to take from lexical environment
I am trying to convert my code to use lexical binding.
I have a function (format-template) that calls a lambda function (let-bound to replacer-inner) from a user-customizable list (template-replace-...
6
votes
2
answers
455
views
Why doesn't "complement" work as a function?
I'm trying to implement the complement function from Common Lisp (CL) in Emacs Lisp, but I'm confused by why defining it as a function (as opposed to a macro) does not work.
The macro definition is ...
5
votes
1
answer
785
views
void-function lexical-let
I'm using this code on Xubuntu (emacs 24.5.1) but it throw error on windows (emacs 25.0.50.1 64bit)
(defun ignore-error-wrapper (fn)
"Funtion return new function that ignore errors.
The function ...
2
votes
1
answer
179
views
lexical-binding only set at the buffer level?
According to this, it seems to say setting lexical-binding only happens at the individual buffer level, i.e. I could not do (setq lexical-binding t) in my init file and have it apply to all subsequent ...
4
votes
2
answers
2k
views
Alternative to lexical-let
I'd rather not use cl-lib and cl at the same time. However, I really like using lexical-let to specify the usage of lexical binding on a more granular level.
Is there any equivalent in cl-lib or ...
1
vote
1
answer
126
views
Cannot assign current buffer name in let statement
I'm trying to write some buffer switching function (which in itself may be a fool's errand, but I digress...) - and it looks a little like this
(defun swk--quit-buffer (original-buffer)
(switch-to-...
0
votes
1
answer
124
views
eval-defun ignores lexical-binding
Today I discovered that if you re-evaluate a defun using eval-defun (C-M-x) in a buffer with lexical-binding set to t, the defun is recreated as an ordinary lambda, not a closure.
For example, after ...
6
votes
1
answer
1k
views
Scope in lambda
In my .emacs conf file, I've got this function that adds a hook to set the compile-command based on the mode. It looks something like,
(defun set-compile-cmd (mode-hook cmd)
(add-hook mode-hook (...
11
votes
1
answer
221
views
How is the variable scoping for macros determined?
Take the following example macro, defined in macro.el.
(defmacro some-macro (&rest body)
`(let ((some-variable 1))
,@body))
And take the following function, defined in a different file, ...
13
votes
1
answer
481
views
What are the potential pitfalls of enabling lexical-binding for a buffer?
This was inspired by the discussion of lexical-binding vs lexical-let in this question. As lexical-binding gives you the ability to have useful closures people may be used to in other languages like ...
8
votes
2
answers
1k
views
'Symbol's value as variable is void' in callback from url-retrieve
When executing the following I get an error:
(defun caller (func)
(url-retrieve "http://m0smith.freeshell.org/"
(lambda (status) (funcall func))))
(caller (lambda() (message "called")))
Result:...
32
votes
3
answers
2k
views
Why is `let' faster with lexical scope?
While reading through the source code for the dolist macro, I ran into the following comment.
;; This is not a reliable test, but it does not matter because both semantics are acceptable, tho one ...