reddit / emacs

testing defvar in *scratch* buffer

whudwl · 2022-02-08 08:37
I read [here](https://www.gnu.org/software/emacs/manual/html_node/eintr/defvar.html) that *defvar* only sets the value of a variable if it doesn't already have a value. Then I tried to test it in scratch buffer, but could not verify that. *defvar* always sets the value no matter what. Did some googling, found some stackover flow answers stating the same. Got quite confused for quite some time. Then decided to read elisp mannal, found this: When you evaluate a top-level ‘defvar’ form with ‘C-M-x’ (‘eval-defun’) or with ‘C-x C-e’ (‘eval-last-sexp’) in Emacs Lisp mode, a special feature of these two commands arranges to set the variable unconditionally, without testing whether its value is void. relieved. Isn't this a bit strange? That it doesn't work the same way when you want to test it out by *C-x C-e*
7890yuiop · 2022-02-08 08:43
This is a good thing. Sometimes you want to re-evaluate the `defvar` without messing with the current value (e.g. when editing docstrings), and other times you want to reset the value to whatever the code currently says. The two different behaviours mean you can choose what you want to do.
whudwl · 2022-02-08 08:50
good point. But it can be confusing for beginners like me, especially because the paragraph I quoted is not in the most obvious place.
7890yuiop · 2022-02-08 09:43
Perhaps, but `C-h k C-M-x` is an exceedingly obvious place to look, and it points out this feature pretty clearly, I think.
whudwl · 2022-02-08 21:53
yeah. I just never bothered to read the help of C-M-x. Always thought it to be dead simple.