Mode-local variables in Emacs
elisp, emacs
Solution
The variables are evaled only at first declaration. All further declarations are skipped. You need a `setq` instead.
Problem
I would like to have a global keyboard shortcut that shows the value of a variable. However, the variable's value could change according to the current major mode in the current buffer. I tried to add the following to my `~/.emacs`: ``` (defun my-elisp-mode-setup () (defvar-local *current-mode-var* "elisp-mode") ) (defun my-sh-mode-setup () (defvar-local *current-mode-var* "sh-mode") ) (add-hook 'emacs-lisp-mode-hook 'my-elisp-mode-setup) (add-hook 'sh-mode-hook 'my-sh-mode-setup) ``` If I now start Emacs with `emacs test.sh` and then type `M-x describe-variable *current-mode-var*` in the `test.sh` buffer, I get ``` *current-mode-var*'s value is "elisp-mode" Automatically becomes buffer-local when set. Documentation: Not documented as a variable. ``` while I expected to get `*current-mode-var*'s value is "sh-mode"`