开发者

Coloring defined types as types

Is there any way to add syntax coloring to new types defined with typedef statements in C?

typedef struct {
    int a,b;
} MyStruct;

MyStruct *InitMyStruct(MyStruct *struct, i开发者_如何学Cnt a, int b);
    ^         ^           ^               ^      ^
    +---------+-----------+               +------+
    Same Color                             Correct type color

If it's not possible natively (I guess so), are there any plugins to make this visual clue work?


I found the exact solution to my question in Vim's help, and I'm posting it here in case someone needs this in the future. It's exactly what I want: a way to read the code and highlight it accordingly.

syntax.txt

Section 15: Highlighting tags

[...]
Only highlighting typedefs, unions and structs can be done too.  For this you
must use Exuberant ctags (found at http://ctags.sf.net).

Put these lines in your Makefile:

# Make a highlight file for types.  Requires Exuberant ctags and awk
types: types.vim
types.vim: *.[ch]
        ctags --c-kinds=gstu -o- *.[ch] |\
                awk 'BEGIN{printf("syntax keyword Type\t")}\
                        {printf("%s ", $$1)}END{print ""}' > $@

And put these lines in your .vimrc: >

   " load the types.vim highlighting file, if it exists
   autocmd BufRead,BufNewFile *.[ch] let fname = expand('<afile>:p:h') . '/types.vim'
   autocmd BufRead,BufNewFile *.[ch] if filereadable(fname)
   autocmd BufRead,BufNewFile *.[ch]   exe 'so ' . fname
   autocmd BufRead,BufNewFile *.[ch] endif
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜