Home Explore Blog CI



neovim

3rd chunk of `runtime/doc/usr_44.txt`
255f33b342230bed833022786e2b62a49ef8dbfdcf2fe0e90000000100000fa1
 match item.  See |pattern| for help on search patterns.

==============================================================================
*44.4*	Regions

In the example x language, strings are enclosed in double quotation marks (").
To highlight strings you define a region.  You need a region start (double
quote) and a region end (double quote).  The definition is as follows: >

	:syntax region xString start=/"/ end=/"/

The "start" and "end" directives define the patterns used to find the start
and end of the region.  But what about strings that look like this?

	"A string with a double quote (\") in it" ~

This creates a problem: The double quotation marks in the middle of the string
will end the region.  You need to tell Vim to skip over any escaped double
quotes in the string.  Do this with the skip keyword: >

	:syntax region xString start=/"/ skip=/\\"/ end=/"/

The double backslash matches a single backslash, since the backslash is a
special character in search patterns.

When to use a region instead of a match?  The main difference is that a match
item is a single pattern, which must match as a whole.  A region starts as
soon as the "start" pattern matches.  Whether the "end" pattern is found or
not doesn't matter.  Thus when the item depends on the "end" pattern to match,
you cannot use a region.  Otherwise, regions are often simpler to define.  And
it is easier to use nested items, as is explained in the next section.

==============================================================================
*44.5*	Nested items

Take a look at this comment:

	%Get input  TODO: Skip white space ~

You want to highlight TODO in big yellow letters, even though it is in a
comment that is highlighted blue.  To let Vim know about this, you define the
following syntax groups: >

	:syntax keyword xTodo TODO contained
	:syntax match xComment /%.*/ contains=xTodo

In the first line, the "contained" argument tells Vim that this keyword can
exist only inside another syntax item.  The next line has "contains=xTodo".
This indicates that the xTodo syntax element is inside it.  The result is that
the comment line as a whole is matched with "xComment" and made blue.  The
word TODO inside it is matched by xTodo and highlighted yellow (highlighting
for xTodo was setup for this).


RECURSIVE NESTING

The x language defines code blocks in curly braces.  And a code block may
contain other code blocks.  This can be defined this way: >

	:syntax region xBlock start=/{/ end=/}/ contains=xBlock

Suppose you have this text:

	while i < b { ~
		if a { ~
			b = c; ~
		} ~
	} ~

First a xBlock starts at the { in the first line.  In the second line another
{ is found.  Since we are inside a xBlock item, and it contains itself, a
nested xBlock item will start here.  Thus the "b = c" line is inside the
second level xBlock region.  Then a } is found in the next line, which matches
with the end pattern of the region.  This ends the nested xBlock.  Because the
} is included in the nested region, it is hidden from the first xBlock region.
Then at the last } the first xBlock region ends.


KEEPING THE END

Consider the following two syntax items: >

	:syntax region xComment start=/%/ end=/$/ contained
	:syntax region xPreProc start=/#/ end=/$/ contains=xComment

You define a comment as anything from % to the end of the line.  A
preprocessor directive is anything from # to the end of the line.  Because you
can have a comment on a preprocessor line, the preprocessor definition
includes a "contains=xComment" argument.  Now look what happens with this
text:

	#define X = Y  % Comment text ~
	int foo = 1; ~

What you see is that the second line is also highlighted as xPreProc.  The
preprocessor directive should end at the end of the line.  That is why
you have used "end=/$/".  So what is going wrong?
   The problem is the contained comment.  The comment starts with % and ends
at the end of the line.  After the comment ends, the preprocessor syntax
continues.  This is after

Title: Regions and Nested Syntax Items
Summary
This section discusses regions in syntax highlighting and how they differ from matches, particularly regarding the 'end' pattern. It also covers nested syntax items, explaining how to highlight specific elements within other syntax groups using 'contained' and 'contains' arguments, and how to handle recursive nesting in code blocks.