Skip to main content
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 ...
Drew's user avatar
  • 78.5k
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....
MAP's user avatar
  • 592
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.6k
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 ...
npostavs's user avatar
  • 9,233
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
  • 52.4k
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 ...
phils's user avatar
  • 52.4k
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 ...
shynur's user avatar
  • 5,815
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. ...
Jordon Biondo's user avatar
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
  • 52.4k
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 ...
wasamasa's user avatar
  • 22.3k
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 ...
Tyler's user avatar
  • 22.6k
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.
phils's user avatar
  • 52.4k
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's user avatar
  • 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-...
Drew's user avatar
  • 78.5k
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 ...
phils's user avatar
  • 52.4k
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:...
shynur's user avatar
  • 5,815
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 ...
Tobias's user avatar
  • 33.5k
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 (...
sds's user avatar
  • 6,174
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 ...
Drew's user avatar
  • 78.5k
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) ...
Tobias's user avatar
  • 33.5k
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 ...
tarsius's user avatar
  • 26.1k
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 (...
justbur's user avatar
  • 1,510
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 ...
gavenkoa's user avatar
  • 3,502

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