lock and string literal coloring in Emacs
In Emacs, I am writing a PHP file that mixes both PHP and non-PHP code, which will be in C++ mode. I would like the PHP code to be highlighted with a link-pink background in order to make it stand out visually.
To do this, I use the font-lock setting:
(make-face 'font-lock-special-macro-face)
(set-face-background 'font-lock-special-macro-face "pink")
(defun add-custom-keyw()
"adds a few special keywords for c and c++ modes"
;
(font-lock-add-keywords nil
'(
("<?[^?]*?>" . 'font-lock-special-macro-face )
; more of those would go here
)
)
)
(setq font-lock-multiline t)
(add-hook 'c++-mode-hook 'add-custom-keyw)
The regular expression expression matches the typical PHP tags and their enclosed text. However, if there are any string literals in the body of the PHP block, then the highlighting fails. I think this is because the face defined above is clashing with the coloring of string literals, which are by default colored text.
What should I do to fix this issue? I would like to keep both coloring schemes (highlighting and colored string literals) if possible.
Here is an example:
The code <?= $className ?>
is highlighted with a pink background.
The code <?= inputs_to_vector($factors, 'factors') ?>
does not have a highlighted background and the string literal 'factors'
is displayed with red colored text.
This happens regardless of whether the leading PHP tag <?
or <?=
is used.
Try this in place of the sexp you have:
'("<?[^?]*?>" 0 font-lock-special-macro-face t)
^
The last part, t
means that this highlighting should override any existing highlighting for the same text.
上一篇: Emacs:最常用的目录用背景色突出显示
下一篇: 在Emacs中锁定和字符串字面着色