Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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, int 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?

like image 215
sidyll Avatar asked Apr 29 '11 03:04

sidyll


1 Answers

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
like image 62
sidyll Avatar answered Oct 01 '22 18:10

sidyll