我想要一个显示变量值的全局键盘快捷键。但是,变量的值可能会根据当前缓冲区中的当前主模式而改变。Emacs中的模式局部变量
我尝试添加以下到我的~/.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)
如果我现在emacs test.sh
启动Emacs,然后在test.sh
缓冲类型M-x describe-variable *current-mode-var*
,我得到
*current-mode-var*'s value is "elisp-mode"
Automatically becomes buffer-local when set.
Documentation:
Not documented as a variable.
,而我预期得到*current-mode-var*'s value is "sh-mode"
感谢的价值!如果我使用'setq',那么变量如何变成本地? –
声明后它是本地缓冲区。这类似于声明类型并在C中分配 :赋值不会更改类型。所以'defvar-local'后面跟'setq'将*不会*改变 变量到非本地。 –
好的。这是否意味着你必须在'〜/ .emacs'中添加'(defvar-local * current-mode-var * nil)',然后在模式钩子中使用'setq'? –