Syntax highlighting between matching brackets

Say I have a LaTeX document open in Vim and I want to highlight each event

{\color{red} ... } 

(where dots should symbolize some content), that is, I want to have {\color{red} , } and everything between these selections. I did it with

 :syn region WarningMsg start=+{\\color{red}+ end=+}+ 

but I have a problem: if I write something like {\color{red} some{thing} important} , then only {\color{red} some{thing} is highlighted, because Vim, of course, calculates the first occurrence } . How can I make this selection rule so that it skips the corresponding curly braces? Even several levels of these.

+4
source share
1 answer

For clarity, it’s best to give each syntax register a custom name, and then associate it with a standard color group. I renamed your original redTeX region.

You need to define a second innerBrace region that defines the curly braces you want to ignore, and mark this region as transparent. Then redTeX must be marked to contain a transparent area that it will ignore.

 syn region innerBrace start=+{+ end=+}+ transparent contains=redTeX syn region redTeX start=+{\\color{red}+ end=+}+ contains=innerBrace hi link redTeX WarningMsg 

Note that in this case there is an added subtlety that matches redTeX as innerBrace . I fixed this by marking innerBrace as containing redTeX .

Hope this makes sense!

+1
source

Source: https://habr.com/ru/post/1439628/


All Articles