Some variables in Emacs are "buffer-local", meaning that each buffer is allowed to have a separate value for that variable that overrides the global default. tab-width
is a good example of a buffer-local variable.
If a variable is buffer-local, then setq
sets its local value in the current buffer and setq-default
sets the global default value.
If a variable is not buffer-local, then setq
and setq-default
do the same thing.
In your case 2, (setq tab-width 4)
set the buffer-local value of tab-width
to 4 in the current buffer, leaving the global default value of tab-width
still at 8, so when you evaluated tab-width
in a different buffer that had no local value, you saw that 8. Then, when you set the default value to 4, that buffer picked it up, since it still had no local value.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…