Questions tagged [lexical-scoping]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
1
vote
1answer
39 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
3answers
79 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) ...
3
votes
1answer
47 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
1answer
54 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
2answers
47 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
2answers
90 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 ...
4
votes
2answers
184 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-...
3
votes
1answer
97 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
1answer
66 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
3answers
278 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
1answer
34 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
3answers
160 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-...
1
vote
1answer
150 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
2answers
66 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
1answer
102 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
1answer
277 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 ...
13
votes
3answers
254 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
3answers
185 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 ...
14
votes
1answer
477 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
2answers
290 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
2answers
104 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 ...
9
votes
1answer
2k 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 warning: ...
2
votes
1answer
72 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
2answers
289 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 ...
4
votes
1answer
366 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
1answer
139 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
2answers
597 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
1answer
94 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
1answer
67 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 ...
5
votes
1answer
715 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
1answer
154 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, ...
12
votes
1answer
323 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
2answers
789 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:...
31
votes
3answers
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 ...