简体   繁体   English

使用font-lock-add-keywords定义新的关键字会打破/覆盖变量名的颜色(emacs)

[英]Defining new keywords with font-lock-add-keywords breaks/overrides variable-name coloring (emacs)

I was trying to get emacs to color some additional keywords in C. In particular, I to add RESTRICT. 我试图让emacs为C中的一些其他关键字添加颜色。特别是,我添加了RESTRICT。 I did: 我做了:

(add-hook 'c-mode-common-hook
      (lambda ()
        (font-lock-add-keywords nil
                    '(("\\<\\(RESTRICT\\)\\>" . font-lock-keyword-face))) ))

However, this (unsurprisingly) just causes emacs to color instances of "RESTRICT" with keyword-face. 但是,这(毫不奇怪)只会使emacs用关键字face着色“ RESTRICT”的实例。

"restrict" (lower case) is already part of emacs' knowledge of C keywords. “限制”(小写)已经是emacs的C关键字知识的一部分。 So if I declare: 因此,如果我声明:

int * restrict foo;

The "int" is colored with type-face, "restrict" is colored with keyword-face, and "foo" is colored with variable-name-face. “ int”以字体表示,“ restrict”以关键字表示,“ foo”以变量名表示。 But with my new RESTRICT word, if I declare: 但是,如果我声明:

int * RESTRICT bar;

"int" is colored as before, and RESTRICT is colored with keyword-face. “ int”像以前一样被着色,而RESTRICT则用关键字face进行着色。 But "bar" has no effects on it. 但是“ bar”对此没有影响。 Without my rule in place, "RESTRICT" would be colored variable-name-face, and "bar" would be unmodified, which is proper. 如果没有我的规则,“ RESTRICT”将被涂成变量名-“ face”,而“ bar”将未被修改,这是正确的。

Anyway, the question is: how can I make emacs color "bar" in the second code-block with variable-name-face? 无论如何,问题是:如何使第二个代码块中的emacs颜色为“ bar”并带有变量名-face? I want emacs to actually treat "RESTRICT" as a keyword in the language (so that variable names get colored), not just color instances of "RESTRICT" a certain way. 我希望emacs实际上将“ RESTRICT”作为语言中的关键字(这样变量名才能被着色),而不仅仅是以某种方式为“ RESTRICT”着色实例。

my guess is that you somehow want to override this definition in cc-langs.el (part of cc-mode): 我的猜测是,您某种程度上想在cc-langs.el(cc-mode的一部分)中覆盖此定义:

(c-lang-defconst c-type-modifier-kwds
  "Type modifier keywords.  These can occur almost anywhere in types
but they don't build a type of themselves.  Unlike the keywords on
`c-primitive-type-kwds', they are fontified with the keyword face and
not the type face."
  t    nil
  c    '("const" "restrict" "volatile")
  c++  '("const" "volatile" "throw")
  objc '("const" "volatile"))

however, i'm no expert in cc-mode but i couldn't find an obvious way to override this binding. 但是,我不是cc模式专家,但是我找不到明显的方法来替代此绑定。

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

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