Skip to content

Commit e80a4ed

Browse files
committed
runtime(matchit): Update to Release 1.21
closes: #19109 Signed-off-by: Christian Brabandt <[email protected]>
1 parent 8e07908 commit e80a4ed

4 files changed

Lines changed: 89 additions & 22 deletions

File tree

runtime/pack/dist/opt/matchit/autoload/matchit.vim

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
" matchit.vim: (global plugin) Extended "%" matching
22
" autload script of matchit plugin, see ../plugin/matchit.vim
3-
" Last Change: May 20, 2024
3+
" Last Change: Jan 06, 2025
44

55
" Neovim does not support scriptversion
66
if has("vimscript-4")
@@ -69,6 +69,26 @@ function matchit#Match_wrapper(word, forward, mode) range
6969
let startpos = [line("."), col(".")]
7070
endif
7171

72+
" Check for custom match function hook
73+
if exists("b:match_function")
74+
let MatchFunc = b:match_function
75+
try
76+
let result = call(MatchFunc, [a:forward])
77+
if !empty(result)
78+
call cursor(result)
79+
return s:CleanUp(restore_options, a:mode, startpos)
80+
endif
81+
catch /.*/
82+
if exists("b:match_debug")
83+
echohl WarningMsg
84+
echom 'matchit: b:match_function error: ' .. v:exception
85+
echohl NONE
86+
endif
87+
return s:CleanUp(restore_options, a:mode, startpos)
88+
endtry
89+
" Empty result: fall through to regular matching
90+
endif
91+
7292
" First step: if not already done, set the script variables
7393
" s:do_BR flag for whether there are backrefs
7494
" s:pat parsed version of b:match_words
@@ -91,7 +111,7 @@ function matchit#Match_wrapper(word, forward, mode) range
91111
let default = escape(&mps, '[$^.*~\\/?]') .. (strlen(&mps) ? "," : "") ..
92112
\ '\/\*:\*\/,#\s*if\%(n\=def\)\=:#\s*else\>:#\s*elif\%(n\=def\)\=\>:#\s*endif\>'
93113
" s:all = pattern with all the keywords
94-
let match_words = match_words .. (strlen(match_words) ? "," : "") .. default
114+
let match_words = s:Append(match_words, default)
95115
let s:last_words = match_words
96116
if match_words !~ s:notslash .. '\\\d'
97117
let s:do_BR = 0
@@ -101,8 +121,8 @@ function matchit#Match_wrapper(word, forward, mode) range
101121
let s:pat = s:ParseWords(match_words)
102122
endif
103123
let s:all = substitute(s:pat, s:notslash .. '\zs[,:]\+', '\\|', 'g')
104-
" un-escape \, to ,
105-
let s:all = substitute(s:all, '\\,', ',', 'g')
124+
" un-escape \, and \: to , and :
125+
let s:all = substitute(s:all, s:notslash .. '\zs\\\(:\|,\)', '\1', 'g')
106126
" Just in case there are too many '\(...)' groups inside the pattern, make
107127
" sure to use \%(...) groups, so that error E872 can be avoided
108128
let s:all = substitute(s:all, '\\(', '\\%(', 'g')
@@ -341,6 +361,18 @@ fun! s:InsertRefs(groupBR, prefix, group, suffix, matchline)
341361
return ini .. ":" .. tailBR
342362
endfun
343363

364+
" String append item2 to item and add ',' in between items
365+
fun! s:Append(item, item2)
366+
if a:item == ''
367+
return a:item2
368+
endif
369+
" there is already a trailing comma, don't add another one
370+
if a:item[-1:] == ','
371+
return a:item .. a:item2
372+
endif
373+
return a:item .. ',' .. a:item2
374+
endfun
375+
344376
" Input a comma-separated list of groups with backrefs, such as
345377
" a:groups = '\(foo\):end\1,\(bar\):end\1'
346378
" and return a comma-separated list of groups with backrefs replaced:
@@ -538,8 +570,8 @@ fun! s:Choose(patterns, string, comma, branch, prefix, suffix, ...)
538570
else
539571
let currpat = substitute(current, s:notslash .. a:branch, '\\|', 'g')
540572
endif
541-
" un-escape \, to ,
542-
let currpat = substitute(currpat, '\\,', ',', 'g')
573+
" un-escape \, and \: to , and :
574+
let currpat = substitute(currpat, s:notslash .. '\zs\\\(:\|,\)', '\1', 'g')
543575
while a:string !~ a:prefix .. currpat .. a:suffix
544576
let tail = strpart(tail, i)
545577
let i = matchend(tail, s:notslash .. a:comma)
@@ -552,6 +584,8 @@ fun! s:Choose(patterns, string, comma, branch, prefix, suffix, ...)
552584
else
553585
let currpat = substitute(current, s:notslash .. a:branch, '\\|', 'g')
554586
endif
587+
" un-escape \, and \: to , and :
588+
let currpat = substitute(currpat, s:notslash .. '\zs\\\(:\|,\)', '\1', 'g')
555589
if a:0
556590
let alttail = strpart(alttail, j)
557591
let j = matchend(alttail, s:notslash .. a:comma)
@@ -621,7 +655,7 @@ fun! matchit#MultiMatch(spflag, mode)
621655
let default = escape(&mps, '[$^.*~\\/?]') .. (strlen(&mps) ? "," : "") ..
622656
\ '\/\*:\*\/,#\s*if\%(n\=def\)\=:#\s*else\>:#\s*elif\>:#\s*endif\>'
623657
let s:last_mps = &mps
624-
let match_words = match_words .. (strlen(match_words) ? "," : "") .. default
658+
let match_words = s:Append(match_words, default)
625659
let s:last_words = match_words
626660
if match_words !~ s:notslash .. '\\\d'
627661
let s:do_BR = 0

runtime/pack/dist/opt/matchit/doc/matchit.txt

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
*matchit.txt* Extended "%" matching
1+
*matchit.txt* Extended "%" matching Last change: 2026 Jan 06
22

3-
For instructions on installing this file, type
4-
`:help matchit-install`
5-
inside Vim.
3+
VIM REFERENCE MANUAL by Benji Fisher et al
64

7-
For Vim version 9.1. Last change: 2024 May 20
8-
9-
10-
VIM REFERENCE MANUAL by Benji Fisher et al
115

126
*matchit* *matchit.vim*
137

@@ -174,7 +168,7 @@ defined automatically.
174168

175169
2.1 Temporarily disable the matchit plugin *matchit-disable* *:MatchDisable*
176170

177-
To temporarily disable the matchit plugin, after it hat been loaded,
171+
To temporarily disable the matchit plugin, after it has been loaded,
178172
execute this command: >
179173
:MatchDisable
180174
@@ -259,6 +253,45 @@ Examples:
259253
See the $VIMRUNTIME/ftplugin/vim.vim for an example that uses both
260254
syntax and a regular expression.
261255

256+
*b:match_function*
257+
If b:match_function is defined, matchit.vim will first call this function to
258+
perform matching. This is useful for languages with an indentation-based block
259+
structure (such as Python) or other complex matching requirements that cannot
260+
be expressed with regular expression patterns.
261+
262+
The function should accept one argument:
263+
forward - 1 for forward search (% command)
264+
0 for backward search (g% command)
265+
266+
The function should return a list with one of these values:
267+
[line, col] - Match found at the specified position
268+
[] - No match found; fall through to regular matching
269+
(|b:match_words|, matchpairs, etc.)
270+
271+
The cursor position is not changed by the function; matchit handles cursor
272+
movement based on the returned position.
273+
274+
If the function throws an error, matchit gives up and doesn't continue.
275+
Enable |b:match_debug| to see error messages from custom match functions.
276+
277+
Python example (simplified): >
278+
let s:keywords = {'if': 'elif\|else', 'elif': 'elif\|else'}
279+
280+
function! s:PythonMatch(forward) abort
281+
let keyword = matchstr(getline('.'), '^\s*\zs\w\+')
282+
let pattern = get(s:keywords, keyword, '')
283+
if empty(pattern) | return [] | endif
284+
285+
let flags = a:forward ? 'nW' : 'nbW'
286+
let [lnum, col] = searchpos('^\s*\%(' . pattern . '\)\>', flags, 0, 0,
287+
\ 'indent(".") != ' . indent('.'))
288+
return lnum > 0 ? [lnum, col] : []
289+
endfunction
290+
291+
let b:match_function = function('s:PythonMatch')
292+
<
293+
See |matchit-newlang| below for more details on supporting new languages.
294+
262295
==============================================================================
263296
4. Supporting a New Language *matchit-newlang*
264297
*b:match_words*
@@ -270,9 +303,9 @@ Vim's |regular-expression|s.
270303

271304
The format for |b:match_words| is similar to that of the 'matchpairs' option:
272305
it is a comma (,)-separated list of groups; each group is a colon(:)-separated
273-
list of patterns (regular expressions). Commas and backslashes that are part
274-
of a pattern should be escaped with backslashes ('\:' and '\,'). It is OK to
275-
have only one group; the effect is undefined if a group has only one pattern.
306+
list of patterns (regular expressions). Commas and colons that are part of a
307+
pattern should be escaped with backslashes ('\:' and '\,'). It is OK to have
308+
only one group; the effect is undefined if a group has only one pattern.
276309
A simple example is >
277310
:let b:match_words = '\<if\>:\<endif\>,'
278311
\ . '\<while\>:\<continue\>:\<break\>:\<endwhile\>'
@@ -315,7 +348,7 @@ expression >
315348
if keywords are only recognized after the start of a line or after a
316349
semicolon (;), with optional white space.
317350

318-
*matchit-backref* *matchit-\1*
351+
*matchit-backref*
319352
In any group, the expressions |\1|, |\2|, ..., |\9| refer to parts of the
320353
INITIAL pattern enclosed in |\(|escaped parentheses|\)|. These are referred
321354
to as back references, or backrefs. For example, >

runtime/pack/dist/opt/matchit/doc/tags

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ MatchError matchit.txt /*MatchError*
66
]% matchit.txt /*]%*
77
b:match_col matchit.txt /*b:match_col*
88
b:match_debug matchit.txt /*b:match_debug*
9+
b:match_function matchit.txt /*b:match_function*
910
b:match_ignorecase matchit.txt /*b:match_ignorecase*
1011
b:match_ini matchit.txt /*b:match_ini*
1112
b:match_iniBR matchit.txt /*b:match_iniBR*
@@ -20,7 +21,6 @@ b:match_words matchit.txt /*b:match_words*
2021
g% matchit.txt /*g%*
2122
matchit matchit.txt /*matchit*
2223
matchit-% matchit.txt /*matchit-%*
23-
matchit-\1 matchit.txt /*matchit-\\1*
2424
matchit-activate matchit.txt /*matchit-activate*
2525
matchit-backref matchit.txt /*matchit-backref*
2626
matchit-bugs matchit.txt /*matchit-bugs*

runtime/pack/dist/opt/matchit/plugin/matchit.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
" matchit.vim: (global plugin) Extended "%" matching
22
" Maintainer: Christian Brabandt
3-
" Version: 1.20
3+
" Version: 1.21
44
" Last Change: 2024 May 20
55
" Repository: https://github.com/chrisbra/matchit
66
" Previous URL:http://www.vim.org/script.php?script_id=39

0 commit comments

Comments
 (0)