简体   繁体   English

Emacs中字体锁定和字符串文字着色之间的冲突

[英]Conflict between font-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. 在Emacs中,我正在编写一个混合了PHP和非PHP代码的PHP文件,该文件将处于C ++模式。 I would like the PHP code to be highlighted with a link-pink background in order to make it stand out visually. 我希望用链接粉色背景突出显示PHP代码,以使其在视觉上脱颖而出。

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. 正则表达式匹配典型的PHP标记及其包含的文本。 However, if there are any string literals in the body of the PHP block, then the highlighting fails. 但是,如果PHP块的主体中​​有任何字符串文字,则突出显示将失败。 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. 代码<?= $className ?>用粉红色背景突出显示。

The code <?= inputs_to_vector($factors, 'factors') ?> does not have a highlighted background and the string literal 'factors' is displayed with red colored text. 代码<?= inputs_to_vector($factors, 'factors') ?>没有突出显示的背景,字符串文字'factors'用红色文本显示。

This happens regardless of whether the leading PHP tag <? 无论领先的PHP标签是否<? or <?= is used. <?=被使用。

Try this in place of the sexp you have: 尝试使用此方法来代替您拥有的sexp:

 '("<\\?[^\\?]*\\?>" 0 font-lock-special-macro-face t)
                                                    ^

The last part, t means that this highlighting should override any existing highlighting for the same text. 最后一部分t表示此突出显示应覆盖同一文本的所有现有突出显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM