11

No, it shouldn't, if you use dynamic binding. Here are two choices (alternatives) that can help: Use lexical binding. Set variable lexical-binding for the file. Use dynamic binding (what you are doing now, no doubt), but also substitute the value of the variable cmd for the variable cmd in the lambda body. You can do this only if the function body of the ...


8

In short: dynamic binding. It is maybe a little unfortunate that default-directory was used as the argument to expand-file-name. Note the docstring: (expand-file-name NAME &optional DEFAULT-DIRECTORY) Convert filename NAME to absolute, and canonicalize it. Second arg DEFAULT-DIRECTORY is directory to start with if NAME is relative (does not ...


8

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 basically a "hack" to silence compiler warnings: at execution time it had no effect at all, so as an "accident" it meant that the silencing behavior of (defvar FOO) ...


7

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 with the following quote of the elisp manual: However, the fact that the internal structure of a closure is exposed to the rest of the Lisp world is ...


6

Both macros are lexically scoped, that's why they have no effect on the "distant" call. Either use the deprecated older flet or cl-letf. (cl-letf (((symbol-function 'return-number) (lambda () 2))) ...)


5

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. If two files define functions or variables with the same name then you'll get a warning when the second one is loaded. You can find out where a function was ...


4

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, the error no longer occurred. I originally thought this was on account of evaluating that form, but I then noticed firstly that simply visiting the file with ...


3

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 is called by its name, you need to temporarily modify its symbol-function value. The shortest way to do that is with cl-letf; see (info "(cl) Modify ...


3

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 is in effect (the default), set has the same effect as setq, apart from the fact that set evaluates its symbol argument whereas setq does not. But when a ...


3

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 binding to have your hook function converted into a closure during the invocation of my-eww-url so that once your hook function will be invoked str will be bound ...


3

Using a should-be-unique prefix is recommended. For init file code I like to use my- because: it is suitably unlikely to conflict with anything. it's short, but meaningful. it's not my name -- so when I share code with other people it reads just as well in their init file as it does in my own.


2

As usual for this kind of thing, just put (defvar orig-win-config) near the beginning of the same file. That declares orig-win-config as a dynamic variable. Otherwise, the byte-compiler thinks that (set-window-configuration orig-win-config) refers to an undefined variable. (It is not that the compiler thinks that (make-local-variable 'orig-win-config) "...


2

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 foo 2) sets the "global value" or the "original" value. It depends on whether foo in that context is a special variable (aka dynamically scoped or global ...


2

Looking at Using Lexical Binding it appears that a needs to be defined special which in using setq it is not. Modifying your example to use defvar gives the answers expected. ;;; -*- lexical-binding: t; -*- (defvar b 5) (let ((b nil)) (setq b t) (print b)) ;; prints t (print b) ;; prints 5 (let ((b nil)) (set 'b t) (print b)) ;; prints t (...


1

OP seems to be missing the point already clearly made in the two answers given: you cannot do what you are asking. Emacs uses a voluntary naming convention, not syntax or compilation rules, to define namespaces. It may sound like a recipe for chaos, but in practice it works very well. What you are doing when you name your function s-join is (effectively) ...


1

In Elisp, all functions names normaly (but not necessarily) reside as an entry in a global environment (obarray). In Elisp functions can be redefined. Which means the function definition which has been evaled (or loaded) last will be the only one, which is known and used. This way you can change functions in your init file, you just need to be sure your ...


1

The short answer is "no", there's really no means of escaping to a different scope like you might do in a language like C++. There may be a very hacky way of getting to the variable if you use dynamic binding, although you should definitely not try it (I have not). From Dynamic Binding - GNU Emacs Lisp Reference Manual: Dynamic binding is implemented in ...


1

This is arguably a duplicate of How to avoid use of `lexical-let`, and there are many different questions here. While you may be able to hack something to get some code to sometimes work, I don't recommend you go there: by the time we get to your macro, the surrounding variable has already been defined as dynamically-scoped so it's too late to really fix it ...


1

I was experimenting with the reader macros and I think I figured it out. The problem is that I am trying to evaluate, the let values twice. Once when they are first bound and then again when we rebind them. This is not a problem for variables that are strings, because strings always evaluate to themselves. But lambdas evaluate to closures, and you can’t ...


1

The comment you wrote on of the other answers was very useful in figuring out what you want to do; I hope I understood correctly. I believe you want to write a command that prompts for a string from the minibuffer with TAB temporarily bound to `my-company-backend. Assuming the string prompted for is the only argument your command needs, try something like ...


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