Syntax highlighting in emacs. Marking different data types with different colours

两盒软妹~` 提交于 2019-11-30 03:48:47

问题


In Emacs, is it possible to mark all variables of different data types with different colors? e.g. if I have the following variables in C/C++ my program

int i,j;
float g,h;
char a,b; 

Then throughout the source code i and j would be marked as red, g and h as green, a and b as blue.

I am not sure how useful this will be in future, but I feel it would help me while reading code, and be a good alternative to the Hungarian notation(not that I use this notation :D).


回答1:


No. Emacs has no idea about the type of a specific expression; doing this would be tantamount to writing a significant part of a C compiler in ELisp.

However, there is a light at the end of the tunnel.

E.g., if you edit OCaml code using tuareg-mode, you can ask Emacs about the type of any expression because the ocaml compiler provides that information; thus you should be able to ask it to highlight variables by type. This is the path to follow.

Alas, gcc does not provide that information; however, its extensiongccxml does.

Also, other C compilers, e.g., clang, provide that information out of the box, and there is a new file semantic-clang.el which relies on those features (although for completion only, not for syntax highlighting).

So, nothing out of the box for you here, but if you are willing to use clang instead of gcc and contribute to the CEDET development, you might get what you want.




回答2:


No, it's not possible to selectively assign a given color to a given variable in emacs (or just for one given program).
However, if it's just syntax highlighting you are looking for, of course, emacs will highlight most languages, and you can even create syntax highlighting for languages emacs would not know about.
Ex. Smali: https://github.com/strazzere/Emacs-Smali



来源:https://stackoverflow.com/questions/8480400/syntax-highlighting-in-emacs-marking-different-data-types-with-different-colour

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!