32
votes
Accepted
What's the difference between setq and defvar
You should ask Emacs this question: C-h i, choose the Elisp manual (m Elisp), and search the index for "defvar" (i defvar). That takes you to node Defining Variables.
Emacs tells you that defvar ...
26
votes
Accepted
What does `setq-local` do, and when should I use it?
Most of your assumptions are close. I'll mention a few later. But, first the main question.
The form setq-local is merely a convenience, it is the same as doing make-local-variable followed by setq....
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 ...
6
votes
Accepted
Test whether variable is defined, but not initialized
The following works only with lexical binding, because with dynamic binding (defvar foo) has no real effect. It checks whether let-binding the variable affects the dynamic value or not. I used the ...
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 ...
5
votes
Accepted
Lambda in `defun` Captures the Lexical Environment, But in `let` It Doesn't
(defvar wtf 10) made wtf bound dynamically so the variable g would get a closure which captured nothing.
This is correct.
However, this cannot explain what (f 20) returned.
It can if, under lexical ...
4
votes
Lambda in `defun` Captures the Lexical Environment, But in `let` It Doesn't
A long comment instead of an answer. (SE limits the length of a comment.)
Hi phils, this is my comment on your answer.
potentially causing other functions, called later in the stack, to obtain an ...
4
votes
Accepted
Can I reload a library and have defvar re-assign values?
(progn (unload-feature 'your-lib) (load-library "your-lib"))
This will work as long as you first loaded the defvars by loading the library through emacs, and not using eval-defun, eval-buffer etc.
...
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, ...
3
votes
Accepted
How to use defvar with the symbol name in another variable?
It isn't equivalent. If you test the symbol of a defvar'd variable with special-variable-p you get t, however this is not the case for your code. This bit must be set for the variable to be usable ...
3
votes
Use of defvar in straight.el's bootstrapping
The form (defvar bootstrap-version) doesn't set the value of bootstrap-version, and will not over-write the value of bootstrap-version if it was already set. It is also not necessary to make the ...
3
votes
Accepted
Why are `defvars` in my macro ignored?
The defvars are evaluated -- your uninterned symbols will have the values you've assigned.
Change make-symbol to intern.
3
votes
Can I reload a library and have defvar re-assign values?
defvar does not reassign a variable's value in the same way as, say
setq or setf. Once a variable has a value, defvar
won't touch it.
From defvar's docstring:
(defvar SYMBOL &optional ...
Dan♦
- 33.3k
2
votes
Can I reload a library and have defvar re-assign values?
Try this:
(defun foo ()
"(Re-)evaluate all `defvar's in the buffer (or its restriction)."
(interactive)
(save-excursion
(goto-char (point-min))
(while (not (eobp))
(when (re-...
2
votes
Accepted
How to provide my own ediff-make-wide-display-function for ediff
I think you've diagnosed the issue correctly, and you can either:
Use ediff-mode-hook to set the buffer-local value.
Use setq-default to set the default value.
Offhand I think #2 seems like the best ...
2
votes
Is the capture template bound to a variable?
Yes. It can also be written this way:
(defvar my/capture-template-contact
"* %^{Name}
:PROPERTIES:
:Created: %U
:Birthday: yyyy-mm-dd
:Email:
:Mobile:
:Skype:
:Address:
:City:
:State:
:Country:...
1
vote
Lambda in `defun` Captures the Lexical Environment, But in `let` It Doesn't
This answer is an extension of Phils answer.
Your question shows the potential danger of locally binding special-declared variables.
The convention of prefixing symbol names with library names ...
1
vote
Is the capture template bound to a variable?
Yes, please see the manual about defvar:
the variable my/capture-template-contact...
...should always be dynamically bound...
and it is
... set to the result ...
of evaluating the value form (...
1
vote
Accepted
What does defvar do in this code?
defvar declares a global variable, more precisely, a variable that is dynamically bound. And it can define that variable, that is, give it an initial value.
If only one value (the variable symbol) is ...
1
vote
Accepted
Converting an expression to a string
If you want to pass a non-evaluated expression as argument without quote you have to use a macro since the arguments of (non-special) functions are evaluated before the function call.
(quote a) ...
1
vote
Warning assigning variables
magit-last-seen-setup-instructions was not well received and has been removed. Also, when it was still in use, you were supposed to set it before loading magit.
magit-log-edit-mode-hook is also ...
1
vote
Warning assigning variables
You declare variables as global with defvar and optionally initialize them. When you use the setq the variable apparently hasn't been declared. It sounds like what your after is this construction
(...
1
vote
Can I reload a library and have defvar re-assign values?
After I heard that there is no convenient solution for re-evaluation of buffer with reassigning of defvar's I made simple function that relays on eval-defun:
(defun my/eval-buffer ()
"Evaluate ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
defvar × 19variables × 7
elisp × 2
symbols × 2
buffer-local × 2
lexical-scoping × 2
let-binding × 2
dynamic-scoping × 2
magit × 1
customize × 1
shell-command × 1
elisp-macros × 1
byte-compilation × 1
ediff × 1
warning × 1
environment × 1
configuration-files × 1
defcustom × 1
defun × 1
setq × 1
lexical-binding × 1
straight.el × 1
closures × 1
macroexpansion × 1
defvar-local × 1