Home Explore Blog CI



neovim

5th chunk of `runtime/pack/dist/opt/matchit/doc/matchit.txt`
b21a26e6a6dad7c83c70d8f20496152f5220f9f7d23622630000000100000e62
 is similar to that of the 'matchpairs' option:
it is a comma (,)-separated list of groups; each group is a colon(:)-separated
list of patterns (regular expressions).  Commas and backslashes that are part
of a pattern should be escaped with backslashes ('\:' and '\,').  It is OK to
have only one group; the effect is undefined if a group has only one pattern.
A simple example is >
	:let b:match_words = '\<if\>:\<endif\>,'
		\ . '\<while\>:\<continue\>:\<break\>:\<endwhile\>'
(In Vim regular expressions, |\<| and |\>| denote word boundaries.  Thus "if"
matches the end of "endif" but "\<if\>" does not.)  Then banging on the "%"
key will bounce the cursor between "if" and the matching "endif"; and from
"while" to any matching "continue" or "break", then to the matching "endwhile"
and back to the "while".  It is almost always easier to use |literal-string|s
(single quotes) as above:  '\<if\>' rather than "\\<if\\>" and so on.

Exception:  If the ":" character does not appear in b:match_words, then it is
treated as an expression to be evaluated.  For example, >
	:let b:match_words = 'GetMatchWords()'
allows you to define a function.  This can return a different string depending
on the current syntax, for example.

Once you have defined the appropriate value of |b:match_words|, you will
probably want to have this set automatically each time you edit the
appropriate file type.  The recommended way to do this is by adding the
definition to a |filetype-plugin| file.

Tips: Be careful that your initial pattern does not match your final pattern.
See the example above for the use of word-boundary expressions.  It is usually
better to use ".\{-}" (as many as necessary) instead of ".*" (as many as
possible).  See |\{-|.  For example, in the string "<tag>label</tag>", "<.*>"
matches the whole string whereas "<.\{-}>" and "<[^>]*>" match "<tag>" and
"</tag>".

				*matchit-spaces* *matchit-s:notend*
If "if" is to be paired with "end if" (Note the space!) then word boundaries
are not enough.  Instead, define a regular expression s:notend that will match
anything but "end" and use it as follows: >
	:let s:notend = '\%(\<end\s\+\)\@<!'
	:let b:match_words = s:notend . '\<if\>:\<end\s\+if\>'
<							*matchit-s:sol*
This is a simplified version of what is done for Ada.  The s:notend is a
|script-variable|.  Similarly, you may want to define a start-of-line regular
expression >
	:let s:sol = '\%(^\|;\)\s*'
if keywords are only recognized after the start of a line or after a
semicolon (;), with optional white space.

					*matchit-backref* *matchit-\1*
In any group, the expressions |\1|, |\2|, ..., |\9| refer to parts of the
INITIAL pattern enclosed in |\(|escaped parentheses|\)|.  These are referred
to as back references, or backrefs.  For example, >
	:let b:match_words = '\<b\(o\+\)\>:\(h\)\1\>'
means that "bo" pairs with "ho" and "boo" pairs with "hoo" and so on.  Note
that "\1" does not refer to the "\(h\)" in this example.  If you have
"\(nested \(parentheses\)\) then "\d" refers to the d-th "\(" and everything
up to and including the matching "\)":  in "\(nested\(parentheses\)\)", "\1"
refers to everything and "\2" refers to "\(parentheses\)".  If you use a
variable such as |s:notend| or |s:sol| in the previous paragraph then remember
to count any "\(" patterns in this variable.  You do not have to count groups
defined by |\%(\)|.

It should be possible to resolve back references from any pattern in the
group.  For example, >
	:let b:match_words = '\(foo\)\(bar\):more\1:and\2:end\1\2'
would not work because "\2" cannot be determined from "morefoo" and "\1"
cannot be determined from "andbar".  On the other hand, >
	:let b:match_words

Title: Defining b:match_words for Custom Matching in matchit.vim
Summary
This section describes how to define custom matching rules in 'matchit.vim' using the 'b:match_words' variable. It explains the format of 'b:match_words' as a comma-separated list of colon-separated regular expression patterns. It covers escaping special characters, using literal strings, and handling cases where 'b:match_words' is an expression. It also provides tips for avoiding matching the wrong patterns, dealing with spaces in keywords using 's:notend', and using start-of-line regular expressions with 's:sol'. It further explains how to use back references (\1, \2, etc.) to refer to parts of the initial pattern and avoid undefined behavior.