emacs - 主模式 Hook 配置会影响其他缓冲区

标签 emacs lisp elisp xemacs major-mode

首先让我说我是 emacs 的新手。

我正在尝试为主要模式创建定制。虽然我的设置运行正常,但我观察到当我打开一个新缓冲区时,该缓冲区的主要模式自定义正在应用于其他不同类型的缓冲区。

例如,如果我打开名为“Makefile”的文件,则会使用 makefile 模式并应用我的自定义设置。如果我随后打开另一个文件,例如“test.c”,将使用 c-mode,但来自 makefile-mode 的定制会与来自 c-mode 的定制合并。

我的 .emacs 文件的相关部分如下所示:

(defun c-mode-settings ()
    (c-set-style                   "bsd")
    (set-buffer-file-coding-system 'utf-8-unix)
    (show-paren-mode               1)

    (setq c-basic-offset        4)
    (setq tab-width             4)
    (setq indent-tabs-mode      nil)
    (setq c-tab-always-indent   t)
    (setq require-final-newline t)
)

(defun makefile-mode-settings ()
    (setq whitespace-style '(tabs spaces space-mark tab-mark face lines-tail))
    (whitespace-mode       t)
    (show-paren-mode       1)

    (setq tab-width             4)
    (setq require-final-newline t)
)

(add-hook 'c-mode-hook        'c-mode-settings)
(add-hook 'makefile-mode-hook 'makefile-mode-settings)

如何防止这些模式 Hook 影响不同模式下的其他缓冲区?

谢谢! 安德鲁

最佳答案

您需要考虑到,有些变量在设置时会变成本地缓冲区,而有些变量是全局的。通常他们在描述中有相应的注释(使用 C-h v var-name 来获取此描述。

在某些情况下,您可以使用

强制任何变量成为缓冲区的局部变量
(set (make-local-variable 'var-name) var-value)

但是你需要小心这一点。

关于emacs - 主模式 Hook 配置会影响其他缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13867464/

相关文章:

r - 使用 R 的 org-babel 代码没有输出

lisp - 比较返回期望值调用函数目录,但在list上的process中却不是这样

lisp - CLISP中如何实现限时执行机制?

emacs - 你如何在 emacs 中给特定的字母上色?

emacs - 获取emacs中的处理器数量

windows - Emacs 和 Git 在 Windows 上显示错误的时间

emacs - 在另一个 lisp 文件中调用函数

emacs - 在 "Slime"(最新版本)中设置 "emacs"时,我如何告诉它更快地加载 swank?

Emacs 自动完成弹出菜单损坏

dynamic - 当 "parent"缓冲区具有我绑定(bind)的同名局部变量时,为什么 with-temp-buffer 中的代码会提示 void 变量?