1

我想在标签中设置自定义颜色背景,但其他不要触摸

(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)))

但是高亮背景标签不正确,见下文: 在此处输入图像描述

4

1 回答 1

1

您需要设置多行字体锁定并定义字体锁定搜索的边界(因此不会花费太多时间)。我的信息(和这段代码)来自这个 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 不喜欢我的代码格式。

于 2013-10-25T20:31:04.547 回答