Vim match containedin not working as expected

2

1

I have tried to extend the C syntax in Vim with the example in the manual.

First I launch vim without any config:

vim -u NONE test.c

Then I load the c-syntax and create my extension

syntax on
syntax keyword myword HELP containedin=cComment contained
highlight myword guibg=red

code:

1: /* HELP */
2: int main(int argc, char **argv) {
3:     int HELP = 0;
4:     if(HELP);
5: }

HELP is highlighted on line 1 as expected. It is also highlighted on line 4, is there some explanation for this?

Klas. S

Posted 2017-10-29T14:00:00.340

Reputation: 121

Answers

0

You need to find out which syntax group "occupies" the HELP in the third line that is not covered by your syntax rule. :syn list shows all active groups, but it's easier when you install the SyntaxAttr.vim - Show syntax highlighting attributes of character under cursor plugin.

SyntaxAttr tells me the cBlock syntax matches there; this needs to be included in your extension, too:

syntax keyword myword HELP containedin=cComment,cBlock contained

Ingo Karkat

Posted 2017-10-29T14:00:00.340

Reputation: 19 513

If I :call SyntaxAttr() when the cursor is within the parentheses, it only gives me group: myword guibg=red(#ff0000). I do not get why I should include cBlock, since I want to restrict the number of places where the myword highlighting is used. – Klas. S – 2017-11-02T23:15:14.037

The cBlock encompasses everything within {...} and prevents the match. This definition is found in $VIMRUNTIME/syntax/c.vim. It would obscure the HELP in line 4, too; however, that is contained in cParen which contains=ALLBUT,... The ALL makes this include your myword, and so it is highlighted without you having to containedin=cParen. – Ingo Karkat – 2017-11-03T08:08:45.153