3

我的 emacs 设置为使用 2 个空格进行全局缩进:

(setq-default indent-tabs-mode nil)
(setq tab-width 2)
(setq js-indent-level 2)
(setq css-indent-offset 2)

但我想为一个使用 4 个空格进行缩进的 Web 项目(主要是 html、css 和 js)做出贡献。所以我试图.dir-locals.el在项目目录中设置一个文件。该文件具有以下设置(使用add-dir-local-variable命令添加):

((nil . ((tab-width . 4)
         (js-indent-level . 4)))

;;; Directory Local Variables
;;; See Info node `(emacs) Directory Variables' for more information.

((js-mode
  (tab-width . 4)))
;;; Directory Local Variables
;;; See Info node `(emacs) Directory Variables' for more information.

((js-mode
  (js-indent-level . 4)))
;;; Directory Local Variables
;;; See Info node `(emacs) Directory Variables' for more information.

((html-mode
  (tab-width . 4)))

但这些设置不会生效。当我在项目子目录中打开 .js 或 .html 文件时,按 Tab 会缩进 2 个空格。

我究竟做错了什么?

4

1 回答 1

5

首先,您的 .dir-locals.el 数据是不平衡的 ( M-x check-parens)。

我不确定那会如何发生,但如果你能让 Emacs 做到这一点,那么你应该M-x report-emacs-bug. 我假设它来自手动编辑。

我不确定多个 js-mode项目是否有效。也许这很好,但似乎不寻常。(不过,Emacs 可能被不平衡的括号弄糊涂了。)

这是您的文件重写以使用更常见的(或至少记录的)点对符号:

((nil . ((tab-width . 4)
         (js-indent-level . 4)))

 (js-mode . ((tab-width . 4)
             (js-indent-level . 4)))

 (html-mode . ((tab-width . 4))))

请注意(对于此特定数据)您不需要js-modeandhtml-mode条目,因为它们复制了nil模式条目的默认值。

编辑:实验上,add-dir-local-variable一旦文件处于有效状态,它似乎会按预期运行。

它更喜欢在可能的情况下创建更紧凑的列表表示法——这很好;它们是等价的——但了解格式差异很有用。
C-hig (elisp) Dotted Pair Notation RET

于 2015-01-01T23:47:33.380 回答