Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VIM syntax: conditional function coloring

I'm customizing the standard "c.vim" syntax file in order to tune the visualisation of my C code. I would like to distinguish the color of the "called functions" from the one of the "declared functions".

Exemple:

int declared_function()
{
    int m;

    m = called_function();
    return (m)
}

I read in depth the VIM documentation, and millions of forums and google results, but all the solutions I tried didn't work.

To resume, I did this:

I defined a region in a recursive way in order to consider all the code within the braces:

syn region Body start="{" end="}" contains=Body

Then I defined through VIM patterns a general function syntax:

syn match cFunction "\<\h\w*\>\(\s\|\n\)*("me=e-1 contains=cType,cDelimiter,cDefine

I did this because I thought I could combine the two in a "if else" condition in the .vimrc file... but after a whole day of failing tests I need the help of someone, who can tell me if it's possible and how to do it.

Thanks everybody.

like image 733
user3154898 Avatar asked Nov 11 '22 15:11

user3154898


1 Answers

You're very close. First, you don't need the recursive definition, but contain all other top-level C syntax elements in it, plus the special group you'll define for called functions:

:syn region Body start="{" end="}" contains=TOP,cFunctionUse

Actually, scratch that, the default $VIMRUNTIME/syntax/c.vim already defines a cBlock syntax group.

Then, define a different syntax group that is contained in the cBlock group.

:syn match cFunctionUse "\<\h\w*\>\(\s\|\n\)*("me=e-1 contained containedin=cBlock contains=cType,cDelimiter,cDefine

Finally, link or define a different highlight group for it, so that it actually looks different:

:hi link cFunctionUse Special

You can put those into ~/.vim/after/syntax/c.vim, so that they'll be added automatically to the default C syntax.

like image 183
Ingo Karkat Avatar answered Nov 14 '22 21:11

Ingo Karkat