Emacs 中的模式局部变量

Håk*_*and 5 emacs elisp

我想要一个显示变量值的全局键盘快捷键。但是,该变量的值可能会根据当前缓冲区中的当前主要模式而变化。

我尝试将以下内容添加到我的~/.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)
Run Code Online (Sandbox Code Playgroud)

如果我现在启动 Emacs emacs test.sh,然后在缓冲区M-x describe-variable *current-mode-var*中输入test.sh,我会得到

*current-mode-var*'s value is "elisp-mode"

  Automatically becomes buffer-local when set.

Documentation:
Not documented as a variable.
Run Code Online (Sandbox Code Playgroud)

虽然我期望得到*current-mode-var*'s value is "sh-mode"

abo*_*abo 4

变量仅在第一次声明时评估。跳过所有进一步的声明。你需要一个setq来代替。

  • 在 emacs 24 中,您可以使用“setq-local”。 (3认同)