【问题标题】:Does emacs offer hide show for html modeemacs 是否为 html 模式提供隐藏显示
【发布时间】:2014-01-24 20:42:02
【问题描述】:

emacs 是否对 html 具有隐藏显示代码折叠功能?我在使用 org 模式时拥有它,但在 nXML/html 端似乎找不到它。

【问题讨论】:

  • 如果您正在使用 html,我强烈建议您使用 web 模式而不是内置的 html 模式,后者确实具有内置代码折叠功能。web-mode.org
  • 库 hideshow 也有 hs-special-modes-alist 可以添加到自定义函数,定义开始和结束 - 例如使用正则表达式。

标签: html emacs elisp


【解决方案1】:

我为 mhtml-mode 编写了这个,它工作得很好,它可以按标签折叠 HTML 以及嵌入的 CSS 和 JS。只需将其添加到您的 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))

正则表达式分解:

  • "{\\|&lt;[^/&gt;]+?":匹配{ 或任何打开的 HTML 标记。它匹配到但不包括在开始标记中的结束&gt;。这允许将 &lt;script&gt;&lt;style&gt; 标签视为 HTML 标签,而不是 JS 或 CSS。

  • "}\\|&lt;/[^/&gt;]*[^/]&gt;":匹配} 或结束标记。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-05
    • 2013-11-09
    • 2011-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多