[英]How to fold html tag in Emacs?
I am using hs-minor-mode and fold-dwim mode.我正在使用 hs-minor-mode 和 fold-dwim 模式。
I added below regex to match html tags by setting the variable hs-special-modes-alist:我在正则表达式下面添加了通过设置变量 hs-special-modes-alist 来匹配 html 标签:
(html-mode "<\\([A-Za-z][A-Za-z0-9]*\\)[^>]*>.*?" "</\\1>" "-->" nil nil)
;; Format: (MODE START END COMMENT-START FORWARD-SEXP-FUNC ADJUST-BEG-FUNC)
But there is no effect when I use it (using command fold-dwim-toggle.) inside html file.但是当我在 html 文件中使用它(使用命令 fold-dwim-toggle.)时没有效果。
Here is the relevant section in my .emacs file:这是我的 .emacs 文件中的相关部分:
;; hideshow
(setq hs-special-modes-alist
(mapcar 'purecopy
'((c-mode "{" "}" "/[*/]" nil nil)
(c++-mode "{" "}" "/[*/]" nil nil)
(bibtex-mode ("@\\S(*\\(\\s(\\)" 1))
(java-mode "{" "}" "/[*/]" nil nil)
(js-mode "{" "}" "/[*/]" nil)
;; (html-mode "<!-- {{{ " "<!-- }}} -->" " -->" nil t)
(html-mode "<\([A-Za-z][A-Za-z0-9]*\)[^>]*>.*?" "</\1>" "-->" nil nil) ;gw: self edited, see blw ref:
;; http://www.regular-expressions.info/examples.html
)))
I'm not familiar with hs-special-modes-alist
.我不熟悉
hs-special-modes-alist
。 But looking at the source code briefly, I see nothing that suggests that the END
pattern can refer to the BEGIN
pattern's subgroups, which is, I assume, what you are trying to do by using "</\\1>"
.但是简要地查看源代码,我看不出任何迹象表明
END
模式可以引用BEGIN
模式的子组,我假设,这是您尝试使用"</\\1>"
。 I'm guessing that you want \\1
to be substituted by whatever was matched by the first subgroup of the BEGIN
pattern.我猜您希望
\\1
被BEGIN
模式的第一个子组匹配的任何内容替换。
None of the examples of hs-special-modes-alist
in the code make use of a subgroup match number (such as \\1
).代码中的
hs-special-modes-alist
示例都没有使用子组匹配号(例如\\1
)。 And the doc says that END
needs to be, itself, a regexp.并且文档说
END
本身需要是一个正则表达式。 Presumably it matches the end independently from START
matching the beginning.据推测,它独立于匹配开头的
START
匹配结尾。
The doc does mention that START
can itself " be a list of the form (COMPLEX-START MDATA-SELECTOR)
, where COMPLEX-START
is a regexp w/ multiple parts and MDATA-SELECTOR
an integer that specifies which sub-match is the proper place to adjust point, before calling hs-forward-sexp-func
.该文档确实提到
START
本身可以“是(COMPLEX-START MDATA-SELECTOR)
形式的列表,其中COMPLEX-START
是带有多个部分的正则表达式, MDATA-SELECTOR
是一个整数,指定哪个子匹配是正确的在调用hs-forward-sexp-func
之前调整点的位置。
I don't think that corresponds immediately to what you want, but at least it indicates a use of subgroup matching.我不认为这与您想要的立即对应,但至少它表明使用了子组匹配。 Perhaps you can use that to match both beginning and ending tags.
也许您可以使用它来匹配开始和结束标签。 I haven't looked further at the code, eg to see where and how
hs-forward-sexp-func
is used.我没有进一步查看代码,例如查看
hs-forward-sexp-func
的使用位置和方式。
For another thing, you generally need to double backslashes in Lisp strings.另一方面,您通常需要在 Lisp 字符串中使用双反斜杠。 So if you want
\\1
you might need to use "</\\\\1>"
.所以如果你想要
\\1
你可能需要使用"</\\\\1>"
。 Likewise, for \\(
- use \\\\(
etc.同样,对于
\\(
- 使用\\\\(
等。
Maybe this will get you a little further toward what you want.也许这会让你更接近你想要的。
(Note, BTW, that regexps are a lousy way to try to parse things like HTML code.) (请注意,顺便说一句,正则表达式是尝试解析 HTML 代码等内容的糟糕方法。)
I answered the question here: Does emacs offer hide show for html mode , but here it is again.我在这里回答了这个问题: Does emacs offer hide show for html mode ,但又来了。
I wrote this for mhtml-mode and it works pretty well, it can fold HTML by tag as well as the embedded CSS and JS.我为 mhtml-mode 写了这个,它工作得很好,它可以按标签折叠 HTML 以及嵌入的 CSS 和 JS。 Simply add it to your emacs config file and you'll be set to go.
只需将它添加到您的 emacs 配置文件中,您就可以开始使用了。
;; When called this automatically detects the submode at the current location.
;; It will then either forward to end of tag(HTML) or end of code block(JS/CSS).
;; This will be passed to hs-minor-mode to properly navigate and fold the code.
(defun mhtml-forward (arg)
(interactive "P")
(pcase (get-text-property (point) 'mhtml-submode)
('nil (sgml-skip-tag-forward 1))
(submode (forward-sexp))))
;; Adds the tag and curly-brace detection to hs-minor-mode for mhtml.
(add-to-list 'hs-special-modes-alist
'(mhtml-mode
"{\\|<[^/>]*?"
"}\\|</[^/>]*[^/]>"
"<!--"
mhtml-forward
nil))
Regex Breakdown:正则表达式细分:
"{\\\\|<[^/>]*?"
: Match either {
or any opening HTML tags. : 匹配
{
或任何打开的 HTML 标签。 It matches up to but doesn't include the closing >
in the opening tag.它最多匹配但不包括开始标记中的结束
>
。 That allows the <script>
and <style>
tags to be treated as HTML tags instead of JS or CSS.这允许将
<script>
和<style>
标签视为 HTML 标签而不是 JS 或 CSS。
"}\\\\|</[^/>]*[^/]>"
: Match either }
or a closing tag. "}\\\\|</[^/>]*[^/]>"
:匹配}
或结束标记。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.