Emacs - 正确缩进嵌入在html文件中的css代码

时间:2021-08-04 20:00:55

When I indent the following html file in emacs, the line 'background-color' is not indented correctly. Can emacs be made to use css-mode for the css part?

当我在emacs中缩进以下html文件时,“background-color”行没有正确缩进。是否可以将emacs用于css部分的css-mode?

<!doctype html>
<html lang=en>
    <head>
        <meta charset=utf-8>
        <title>Page</title>
        <style type = "text/css"> 
            h2 {
            background-color: #BBB;
            }
        </style> 
    </head>
    <body>
        <p>
            <h2>Title</h2>
        </p>
    </body>
</html>

Question asked: May 2011

问题问题:2011年5月

Question edited: Dec 2013

编辑问题:2013年12月

Edit:

I have the impression that it is myopic to look for, or to develop, a method for handling css and html in particular. The right solution is a broader one that handles a file consisting of any two subsets. Perhaps the question does not warrant serious attention because the two subsets should be isolated in their own files, but for quick tests and smaller examples the question stands. Suggestions of working solutions are welcome.

我的印象是,寻找或开发一种特别是处理css和html的方法是近视的。正确的解决方案是处理由任意两个子集组成的文件的更广泛的解决方案。也许这个问题不值得认真关注,因为这两个子集应该在他们自己的文件中隔离,但是对于快速测试和较小的例子,问题就在于此。欢迎提出工作解决方案的建议。

2 个解决方案

#1


3  

As you know, you're better off distributing HTML, Javascript, CSS, &c., among their own files, but often that's not possible, especially when working with legacy code. In such cases, I've found web-mode extremely valuable.

如您所知,您最好在他们自己的文件中分发HTML,Javascript,CSS等,但通常这是不可能的,尤其是在使用遗留代码时。在这种情况下,我发现网络模式非常有价值。

Unlike nXhtml and other such solutions, web-mode directly handles font-locking and indentation for each type of code, rather than attempting to invoke the proper major mode for wherever point happens to be, or trying to layer major modes atop one another in a way that's all but guaranteed not to work reliably. While this approach admittedly represents a substantial duplication of effort, and means you don't, for example, get the benefit of Steve Yegge's excellent js2-mode when editing inline Javascript, it does mean you get reliable font-locking and indentation, without having to spend a lot of time dealing with fragile chicanery; having installed and customized web-mode, you can more or less jump right in and start hacking away:

与nXhtml和其他此类解决方案不同,Web模式直接处理每种类型代码的字体锁定和缩进,而不是尝试在任何地方调用适当的主模式,或尝试将主要模式分层在一个所有但都保证不能可靠运行的方式。虽然这种方法确实代表了大量的重复工作,并且意味着你没有,例如,在编辑内联Javascript时得到Steve Yegge优秀的js2模式的好处,它确实意味着你得到可靠的字体锁定和缩进,而没有花大量时间处理脆弱的诡计;安装和自定义网络模式后,你可以或多或少地跳进去开始黑客攻击:

Emacs  - 正确缩进嵌入在html文件中的css代码

I spent more time than I'd care to remember dealing with MuMaMo and its ilk, and all I got out of it was headaches. (And improved discipline regarding the avoidance of inline CSS and Javascript, but that was purely out of self-defense.) Web-mode is a fantastic solution to that problem, and I recommend it without reservation.

我花了更多的时间来记住处理MuMaMo及其类似的事情,而我所得到的只是头痛。 (并且改进了关于避免使用内联CSS和Javascript的规则,但这纯粹是出于自卫。)Web模式是解决该问题的绝佳解决方案,我毫无保留地推荐它。

#2


1  

Check out nXhtml mode: http://ourcomments.org/Emacs/nXhtml/doc/nxhtml.html

查看nXhtml模式:http://ourcomments.org/Emacs/nXhtml/doc/nxhtml.html

From its page:

从它的页面:

Another important feature is the ability to mix several languages in one buffer and get the correct syntax highlighting and indentation for each of them.

另一个重要特性是能够在一个缓冲区中混合多种语言,并为每个语言获得正确的语法突出显示和缩进。

Edit: the EmacsWiki article on multiple modes has some other options (on top of Mumamo, which is what nXhtml uses). One that you may find particularly interesting is multi-web-mode, which changes the mode based on where the point is.

编辑:关于多种模式的EmacsWiki文章有一些其他选项(在Mumamo之上,这是nXhtml使用的)。您可能会发现特别有趣的一种是多网络模式,它根据点的位置更改模式。

I have only used nXhtml mode myself, but multi-web-mode may be simpler to set up and easier to use—while I did like nXhtml, I distinctly remember its being a hassle to set up.

我自己只使用了nXhtml模式,但是多网页模式可能更容易设置和更容易使用 - 虽然我喜欢nXhtml,但我清楚地记得它是一个设置的麻烦。

#1


3  

As you know, you're better off distributing HTML, Javascript, CSS, &c., among their own files, but often that's not possible, especially when working with legacy code. In such cases, I've found web-mode extremely valuable.

如您所知,您最好在他们自己的文件中分发HTML,Javascript,CSS等,但通常这是不可能的,尤其是在使用遗留代码时。在这种情况下,我发现网络模式非常有价值。

Unlike nXhtml and other such solutions, web-mode directly handles font-locking and indentation for each type of code, rather than attempting to invoke the proper major mode for wherever point happens to be, or trying to layer major modes atop one another in a way that's all but guaranteed not to work reliably. While this approach admittedly represents a substantial duplication of effort, and means you don't, for example, get the benefit of Steve Yegge's excellent js2-mode when editing inline Javascript, it does mean you get reliable font-locking and indentation, without having to spend a lot of time dealing with fragile chicanery; having installed and customized web-mode, you can more or less jump right in and start hacking away:

与nXhtml和其他此类解决方案不同,Web模式直接处理每种类型代码的字体锁定和缩进,而不是尝试在任何地方调用适当的主模式,或尝试将主要模式分层在一个所有但都保证不能可靠运行的方式。虽然这种方法确实代表了大量的重复工作,并且意味着你没有,例如,在编辑内联Javascript时得到Steve Yegge优秀的js2模式的好处,它确实意味着你得到可靠的字体锁定和缩进,而没有花大量时间处理脆弱的诡计;安装和自定义网络模式后,你可以或多或少地跳进去开始黑客攻击:

Emacs  - 正确缩进嵌入在html文件中的css代码

I spent more time than I'd care to remember dealing with MuMaMo and its ilk, and all I got out of it was headaches. (And improved discipline regarding the avoidance of inline CSS and Javascript, but that was purely out of self-defense.) Web-mode is a fantastic solution to that problem, and I recommend it without reservation.

我花了更多的时间来记住处理MuMaMo及其类似的事情,而我所得到的只是头痛。 (并且改进了关于避免使用内联CSS和Javascript的规则,但这纯粹是出于自卫。)Web模式是解决该问题的绝佳解决方案,我毫无保留地推荐它。

#2


1  

Check out nXhtml mode: http://ourcomments.org/Emacs/nXhtml/doc/nxhtml.html

查看nXhtml模式:http://ourcomments.org/Emacs/nXhtml/doc/nxhtml.html

From its page:

从它的页面:

Another important feature is the ability to mix several languages in one buffer and get the correct syntax highlighting and indentation for each of them.

另一个重要特性是能够在一个缓冲区中混合多种语言,并为每个语言获得正确的语法突出显示和缩进。

Edit: the EmacsWiki article on multiple modes has some other options (on top of Mumamo, which is what nXhtml uses). One that you may find particularly interesting is multi-web-mode, which changes the mode based on where the point is.

编辑:关于多种模式的EmacsWiki文章有一些其他选项(在Mumamo之上,这是nXhtml使用的)。您可能会发现特别有趣的一种是多网络模式,它根据点的位置更改模式。

I have only used nXhtml mode myself, but multi-web-mode may be simpler to set up and easier to use—while I did like nXhtml, I distinctly remember its being a hassle to set up.

我自己只使用了nXhtml模式,但是多网页模式可能更容易设置和更容易使用 - 虽然我喜欢nXhtml,但我清楚地记得它是一个设置的麻烦。