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

发布于 2024-12-13 13:48:31 字数 791 浏览 0 评论 0原文

我试图让 emacs 为 C 中的一些附加关键字着色。特别是,我添加了 RESTRICT。我做到了:

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

然而,这(不出所料)只会导致 emacs 使用关键字-face 对“RESTRICT”实例进行着色。

“restrict”(小写)已经是 emacs C 关键字知识的一部分。因此,如果我声明:

int * restrict foo;

“int”用 type-face 着色,“restrict”用 keywords-face 着色,“foo”用变量名-face 着色。但是对于我的新 RESTRICT 词,如果我声明:

int * RESTRICT bar;

“int”的颜色与以前一样,并且 RESTRICT 使用关键字面的颜色。但“bar”对其没有任何影响。如果没有我的规则,“RESTRICT”将是彩色的变量名称面,而“bar”将是未修改的,这是正确的。

不管怎样,问题是:如何在带有变量名面的第二个代码块中制作 emacs 颜色“条”?我希望 emacs 实际上将“RESTRICT”视为语言中的关键字(以便变量名称着色),而不仅仅是以某种方式对“RESTRICT”实例进行颜色处理。

I was trying to get emacs to color some additional keywords in C. In particular, I to add 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.

"restrict" (lower case) is already part of emacs' knowledge of C keywords. 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. But with my new RESTRICT word, if I declare:

int * RESTRICT bar;

"int" is colored as before, and RESTRICT is colored with keyword-face. But "bar" has no effects on it. Without my rule in place, "RESTRICT" would be colored variable-name-face, and "bar" would be unmodified, which is proper.

Anyway, the question is: how can I make emacs color "bar" in the second code-block with variable-name-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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萝莉病 2024-12-20 13:48:31

我的猜测是,您以某种方式想要在 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"))

但是,我不是 cc-mode 方面的专家,但我找不到覆盖此绑定的明显方法。

my guess is that you somehow want to override this definition in cc-langs.el (part of 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文