Emacs CSS 模式不会跨多行缩进值

Emacs CSS mode won't indent values across multiple lines

当我的 CSS 文件中的一行看起来太长时(通常是一个 属性 后跟一堆值),我想像这样将它们分成几行(对不起我的低名声):

https://i.stack.imgur.com/bxXvv.png

但我必须在行前手动输入空格才能实现。实际上,当我用光标在 url 行上按 TAB 键时,我得到的是:

https://i.stack.imgur.com/r4nxa.png

最糟糕的是,在手动插入空格后,当我在同一行再次按 TAB 键时,由于 indent-region.

,它又回到了丑陋的格式

这不是什么大问题,但它确实让我很痛苦,我真的希望我们能在这里找到一个像样的解决方案。提前致谢!

M-x 版本:

GNU Emacs 25.1.1 (x86_64-apple-darwin16.1.0, NS appkit-1504.60 Version 10.12.1 (Build 16B2555)) of 2016-11-27

编辑: 还尝试了 css 文件的网络模式和 xah-css-模式。 None 他们成功了。

css-mode 使用 smie 进行缩进。看起来那个场景中的 : 被标记为“:-属性”。一种选择是更改 css-smie-rules 以包含在该标记后缩进的另一条规则。

评估以下重新定义似乎给出了您想要的缩进,

(defun css-smie-rules (kind token)
  (pcase (cons kind token)
    (`(:elem . basic) css-indent-offset)
    (`(:elem . arg) 0)
    (`(:list-intro . ,(or `";" `"")) t) ;"" stands for BOB (bug#15467).
    (`(:before . "{")
     (when (or (smie-rule-hanging-p) (smie-rule-bolp))
       (smie-backward-sexp ";")
       (smie-indent-virtual)))
    (`(:before . ,(or "{" "("))
     (if (smie-rule-hanging-p) (smie-rule-parent 0)))
    ;; *** Additional rule ***
    (`(:after . ":-property") css-indent-offset)))

命令 smie-config-show-indent 可用于确定在给定点使用的缩进规则。

@jenesaisquoi 提到了 smie 并提供了一个非常有用的示例。这是我对自己问题的最终解决方案。

将这些添加到 init.el 文件:

(require 'smie)
(defun css-smie-rules (kind token)
  (pcase (cons kind token)
    (`(:elem . basic) css-indent-offset)
    (`(:elem . arg) 0)
    (`(:list-intro . ,(or `";" `"")) t) ;"" stands for BOB (bug#15467).
    (`(:before . "{")
     (when (or (smie-rule-hanging-p) (smie-rule-bolp))
       (smie-backward-sexp ";")
       (smie-indent-virtual)))
    (`(:before . ,(or "{" "("))
     (if (smie-rule-hanging-p) (smie-rule-parent 0)))
    ;; *** Additional rules below ***
    (`(:after . ":") css-indent-offset)
    (`(:after . ",") css-indent-offset)))

语法不言自明。

也许有一些方法可以在不重写函数的情况下修改它,但我还不熟悉所有的建议,所以如果我找到更优雅的方法,我会回来编辑。

转到 jenesaiquoi 的回答,了解我们现在可以拥有的最佳解决方案。

我在 Github 中为 web-mode 开了一个问题,web-mode 的作者现在正在处理它。我们很快就会看到对我在问题中提到的缩进的集成支持。