您需要设置多行字体锁定并定义字体锁定搜索的边界(因此不会花费太多时间)。我的信息(和这段代码)来自这个 SO question。
在这里,我将多行字体锁定定义为在标签 (< ... >) 中进行。如果将其添加到您的定义派生模式中,它将按照您的描述工作。
(set (make-local-variable 'font-lock-multiline) t)
(add-hook 'font-lock-extend-region-functions
'test-font-lock-extend-region)
(defun test-font-lock-extend-region ()
"Extend the search region to include an entire SGML tag."
;; Avoid compiler warnings about these global variables from font-lock.el.
;; See the documentation for variable `font-lock-extend-region-functions'.
(eval-when-compile (defvar font-lock-beg) (defvar font-lock-end))
(save-excursion
(goto-char font-lock-beg)
(let ((found (or (re-search-backward "<" nil t) (point-min))))
(goto-char font-lock-end)
(when (re-search-forward ">" nil t)
(beginning-of-line)
(setq font-lock-end (point)))
(setq font-lock-beg found))))
编辑:出于某种原因,SO 不喜欢我的代码格式。