c - 在 emacs 中突出显示整个 c/cpp 宏定义?

标签 c emacs macros syntax-highlighting multiline

在 vim 中,整个 c 宏定义以不同的颜色完全突出显示。我如何在 emacs 中获得类似的样式?您可以引用下面的屏幕截图,显示在 vim(右侧)中,整个 EXAMPLE_MACRO 突出显示并显示 CALL_MACRO(0) 是定义的一部分。

enter image description here

#include <stdio.h>

#define CALL_MACRO(x)                           \
    EXAMPLE_MACRO(x)                            \


int
main(int argc, char *argv[])
{
#define EXAMPLE_MACRO(x)                           \
    if (x) {                                       \
    printf("\n Condition is true");            \
} else {                                       \
    printf("\n Condition is false");           \
}                                              \
                                               \
CALL_MACRO(0);

CALL_MACRO(1);
#undef EXAMPLE_MARCO
}

上面代码的主模式是c模式。

请注意以上代码无法编译。


我发现下面的代码可以使#if 0 和#endif 显示为字体锁定。

(defun my-c-mode-font-lock-if0 (limit)
(save-restriction
(widen)
(save-excursion
  (goto-char (point-min))
  (let ((depth 0) str start start-depth)
    (while (re-search-forward "^\\s-*#\\s-*\\(if\\|else\\|endif\\)" limit 'move)
      (setq str (match-string 1))
      (if (string= str "if")
          (progn
            (setq depth (1+ depth))
            (when (and (null start) (looking-at "\\s-+0"))
              (setq start (match-end 0)
                    start-depth depth)))
        (when (and start (= depth start-depth))
          (c-put-font-lock-face start (match-beginning 0) 'font-lock-comment-face)
          (setq start nil))
        (when (string= str "endif")
          (setq depth (1- depth)))))
    (when (and start (> depth 0))
      (c-put-font-lock-face start (point) 'font-lock-comment-face)))))
nil)

(defun my-c-mode-common-hook ()
(font-lock-add-keywords
 nil
 '((my-c-mode-font-lock-if0 (0 font-lock-comment-face prepend))) 'add-to-end))

(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)

我不确定是否可以修改以上代码以突出显示多行宏。

scottmcpeak.com/elisp/scott.emacs.el

有提到

;'("^([\t]#.(\\\n.))"1 font-lock-preprocessor-face) ;第二行是我试图让它识别多行宏 ;并将它们完全突出显示为预处理器(不起作用..)

最佳答案

我刚看到@Lindydancer 的一个很棒的包,叫做 prepaint.el ,这正是你想要的。它以不同方式突出显示宏,包括多行宏,并且还保留了宏中的关键字突出显示。

关于c - 在 emacs 中突出显示整个 c/cpp 宏定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22208563/

相关文章:

在循环中连接 c 中的宏

c - 海湾合作委员会错误 : "unable to find a register to spill" (problems with OS's dev's inline assembly)

emacs - 在基于 Solaris 的远程服务器上找不到退出状态 "test -e ... "

c - 预处理器数据检索宏

regex - 如何在 Emacs 正则表达式中表示 "not a bracket"?

emacs - 如何为主要模式定义注释语法?

c++ - 定义预处理器宏

c - 将一行字写入C中的二维数组

c - 在使用 scanf 读取的 char 变量中添加两个数字

c - 'main' 的返回类型不是 'int'