Home Explore Blog CI



neovim

2nd chunk of `runtime/pack/dist/opt/matchit/doc/matchit.txt`
d0b9bfc8ad301e150bc2fbfba39106a992c796294bc33f590000000100000fa2
 Essbase, Fortran, HTML, JSP (same as HTML), LaTeX, Lua, Pascal,
SGML, Shell, Tcsh, Vim, XML.  Other languages may already have support via
the default |filetype-plugin|s in the standard vim distribution.

To support a new language, see |matchit-newlang| below.

DETAILS:				*matchit-details* *matchit-parse*

Here is an outline of what matchit.vim does each time you hit the "%" key.  If
there are |backref|s in |b:match_words| then the first step is to produce a
version in which these back references have been eliminated; if there are no
|backref|s then this step is skipped.  This step is called parsing.  For
example, "\(foo\|bar\):end\1" is parsed to yield
"\(foo\|bar\):end\(foo\|bar\)".  This can get tricky, especially if there are
nested groups.  If debugging is turned on, the parsed version is saved as
|b:match_pat|.

							*matchit-choose*
Next, the script looks for a word on the current line that matches the pattern
just constructed.  It includes the patterns from the 'matchpairs' option.
The goal is to do what you expect, which turns out to be a little complicated.
The script follows these rules:

	Insist on a match that ends on or after the cursor.
	Prefer a match that includes the cursor position (that is, one that
		starts on or before the cursor).
	Prefer a match that starts as close to the cursor as possible.
	If more than one pattern in |b:match_words| matches, choose the one
		that is listed first.

Examples:

	Suppose you >
		:let b:match_words = '<:>,<tag>:</tag>'
<	and hit "%" with the cursor on or before the "<" in "a <tag> is born".
	The pattern '<' comes first, so it is preferred over '<tag>', which
	also matches.  If the cursor is on the "t", however, then '<tag>' is
	preferred, because this matches a bit of text containing the cursor.
	If the two groups of patterns were reversed then '<' would never be
	preferred.

	Suppose you >
		:let b:match_words = 'if:end if'
<	(Note the space!) and hit "%" with the cursor at the end of "end if".
	Then "if" matches, which is probably not what you want, but if the
	cursor starts on the "end " then "end if" is chosen.  (You can avoid
	this problem by using a more complicated pattern.)

If there is no match, the cursor does not move.  (Before version 1.13 of the
script, it would fall back on the usual behavior of |%|).  If debugging is
turned on, the matched bit of text is saved as |b:match_match| and the cursor
column of the start of the match is saved as |b:match_col|.

Next, the script looks through |b:match_words| (original and parsed versions)
for the group and pattern that match.  If debugging is turned on, the group is
saved as |b:match_ini| (the first pattern) and |b:match_tail| (the rest).  If
there are |backref|s then, in addition, the matching pattern is saved as
|b:match_word| and a table of translations is saved as |b:match_table|.  If
there are |backref|s, these are determined from the matching pattern and
|b:match_match| and substituted into each pattern in the matching group.

The script decides whether to search forwards or backwards and chooses
arguments for the |searchpair()| function.  Then, the cursor is moved to the
start of the match, and |searchpair()| is called.  By default, matching
structures inside strings and comments are ignored.  This can be changed by
setting |b:match_skip|.

==============================================================================
2. Activation						*matchit-activate*

To use the matchit plugin add this line to your |vimrc|: >
	packadd! matchit

The script should start working the next time you start Vim.

To use the matchit plugin after Vim has started, execute this command: >
	packadd matchit

(Earlier versions of the script did nothing unless a |buffer-variable| named
|b:match_words| was defined.  Even earlier versions contained autocommands
that set this variable for various file types.  Now, |b:match_words| is
defined in many of the default |filetype-plugin|s instead.)

For a new language, you can add autocommands

Title: matchit.vim: Details, Pattern Matching Logic, and Activation
Summary
This section details the inner workings of 'matchit.vim', including the parsing of backreferences in 'b:match_words', the logic for choosing the best match based on cursor position, and how it handles 'matchpairs'. It also explains how the script determines whether to search forwards or backwards and utilize the 'searchpair()' function. Additionally, the section provides instructions on how to activate the 'matchit' plugin by adding 'packadd! matchit' to your vimrc file or running 'packadd matchit' after Vim has started.