我想在标签中设置自定义颜色背景,但其他不要触摸
(define-derived-mode php-mode fundamental-mode
"php-mode"
:syntax-table php-syntax-table
(setq font-lock-defaults '(php-keywords)))
(defvar php-syntax-table (make-syntax-table) "Syntax table for php-mode")
(make-face 'php-region-face)
(set-face-background 'php-region-face "red")
(setq php-keywords '(("<\\?php[[:ascii:]]*?\\?>" 0 'php-region-face t)))
但突出显示背景标签不正确,请参阅下文:
答案 0 :(得分:1)
您需要设置多行字体锁定并定义字体锁定搜索的边界(因此不需要太多时间)。我的信息(和此代码)来自this SO question。
在这里,我定义多行字体锁定发生在标签(&lt; ...&gt;)中。如果将其添加到您的定义派生模式,它将按您的描述工作。
(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不喜欢我的代码格式化。