我正在使用hs-小模式和折叠-dwim模式。
我在regex下面添加了与html标记相匹配的方法,方法是设置变量hs
(html-mode "<\([A-Za-z][A-Za-z0-9]*\)[^>]*>.*?" "</\1>" "-->" nil nil)
;; Format: (MODE START END COMMENT-START FORWARD-SEXP-FUNC ADJUST-BEG-FUNC)
但是当我使用它时没有效果(使用命令折叠-dwim-切换)。在html文件中。
以下是我的.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
)))发布于 2015-04-06 01:36:16
我对hs-special-modes-alist不熟悉。但是简单地看一下源代码,我看不出END模式可以引用BEGIN模式的子组,这就是您试图通过使用"</\1>"所做的事情。我猜您希望\1被BEGIN模式的第一个子组所匹配的东西所替代。
代码中的hs-special-modes-alist示例中没有一个使用子组匹配号(如\1)。医生说,END本身需要是一个正则表达式。据推测,它与匹配开头的START独立地匹配结尾。
文档确实提到,START本身可以“是表单(COMPLEX-START MDATA-SELECTOR)__的列表,其中COMPLEX-START是regexp /多个部件,MDATA-SELECTOR是一个整数,它指定哪个子匹配是在调用hs-forward-sexp-func__之前调整点的合适位置。
我认为这并不是你想要的,但至少它表明了子群匹配的使用。也许您可以使用它来匹配开始标记和结束标记。我没有进一步研究代码,例如,查看hs-forward-sexp-func在何处以及如何使用。
另一方面,您通常需要在Lisp字符串中加倍反斜杠。因此,如果您想要\1,您可能需要使用"</\\1>"。同样,对于\( --使用\\(等。
也许这会让你更接近你想要的。
(注意,BTW,regexp是一种尝试解析HTML代码的糟糕方法。)
发布于 2020-06-21 18:24:11
我在这里回答了这个问题:Does emacs offer hide show for html mode,但这又是一个问题。
我是为mhtml模式编写的,它工作得很好,它可以按标记折叠HTML,以及嵌入的CSS和JS。只需将其添加到您的emacs配置文件中,您就会被设置为go。
;; 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分解:
"{\\|<[^/>]+?":匹配{或任何开头的HTML标记。它匹配到但不包括开始标记中的结束>。这允许将<script>和<style>标记视为HTML标记,而不是JS或<style>。"}\\|</[^/>]*[^/]>":匹配}或结束标记。https://stackoverflow.com/questions/29463639
复制相似问题