Xpr*_*rog 5 c emacs macros syntax-highlighting multiline
在vim中,整个c宏定义用不同的颜色完全突出显示.我如何在emacs中获得类似的风格?您可以参考下面的屏幕截图,显示在vim(右侧)中,整个EXAMPLE_MACRO突出显示并显示CALL_MACRO(0)是定义的一部分.

#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
}
Run Code Online (Sandbox Code Playgroud)
上述代码的主模式是c模式.
请注意上面的代码不能编译.
我发现下面的代码可以使#if 0和#endif显示为font-lock.
(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)
Run Code Online (Sandbox Code Playgroud)
scottmcpeak.com/elisp/scott.emacs.el
有人提到
;'("^([\ t] #.(\\\n.))"1 font-lock-preprocessor-face); 第二行是我尝试识别多行宏; 并完全突出显示它们作为预处理器(不起作用..)