15735
|
1 " matchit.vim: (global plugin) Extended "%" matching
|
|
2 " autload script of matchit plugin, see ../plugin/matchit.vim
|
26708
|
3 " Last Change: Jun 10, 2021
|
|
4
|
|
5 " Neovim does not support scriptversion
|
|
6 if has("vimscript-4")
|
|
7 scriptversion 4
|
|
8 endif
|
15735
|
9
|
|
10 let s:last_mps = ""
|
|
11 let s:last_words = ":"
|
|
12 let s:patBR = ""
|
|
13
|
|
14 let s:save_cpo = &cpo
|
|
15 set cpo&vim
|
|
16
|
|
17 " Auto-complete mappings: (not yet "ready for prime time")
|
|
18 " TODO Read :help write-plugin for the "right" way to let the user
|
|
19 " specify a key binding.
|
|
20 " let g:match_auto = '<C-]>'
|
|
21 " let g:match_autoCR = '<C-CR>'
|
|
22 " if exists("g:match_auto")
|
|
23 " execute "inoremap " . g:match_auto . ' x<Esc>"=<SID>Autocomplete()<CR>Pls'
|
|
24 " endif
|
|
25 " if exists("g:match_autoCR")
|
|
26 " execute "inoremap " . g:match_autoCR . ' <CR><C-R>=<SID>Autocomplete()<CR>'
|
|
27 " endif
|
|
28 " if exists("g:match_gthhoh")
|
|
29 " execute "inoremap " . g:match_gthhoh . ' <C-O>:call <SID>Gthhoh()<CR>'
|
|
30 " endif " gthhoh = "Get the heck out of here!"
|
|
31
|
|
32 let s:notslash = '\\\@1<!\%(\\\\\)*'
|
|
33
|
|
34 function s:RestoreOptions()
|
|
35 " In s:CleanUp(), :execute "set" restore_options .
|
|
36 let restore_options = ""
|
|
37 if get(b:, 'match_ignorecase', &ic) != &ic
|
26708
|
38 let restore_options ..= (&ic ? " " : " no") .. "ignorecase"
|
15735
|
39 let &ignorecase = b:match_ignorecase
|
|
40 endif
|
|
41 if &ve != ''
|
26708
|
42 let restore_options = " ve=" .. &ve .. restore_options
|
15735
|
43 set ve=
|
|
44 endif
|
|
45 return restore_options
|
|
46 endfunction
|
|
47
|
|
48 function matchit#Match_wrapper(word, forward, mode) range
|
|
49 let restore_options = s:RestoreOptions()
|
26708
|
50 " In s:CleanUp(), we may need to check whether the cursor moved forward.
|
|
51 let startpos = [line("."), col(".")]
|
|
52 " if a count has been applied, use the default [count]% mode (see :h N%)
|
|
53 if v:count
|
|
54 exe "normal! " .. v:count .. "%"
|
|
55 return s:CleanUp(restore_options, a:mode, startpos)
|
|
56 end
|
|
57 if a:mode =~# "v" && mode(1) =~# 'ni'
|
|
58 exe "norm! gv"
|
15735
|
59 elseif a:mode == "o" && mode(1) !~# '[vV]'
|
|
60 exe "norm! v"
|
26708
|
61 " If this function was called from Visual mode, make sure that the cursor
|
|
62 " is at the correct end of the Visual range:
|
|
63 elseif a:mode == "v"
|
|
64 execute "normal! gv\<Esc>"
|
|
65 let startpos = [line("."), col(".")]
|
15735
|
66 endif
|
|
67
|
|
68 " First step: if not already done, set the script variables
|
|
69 " s:do_BR flag for whether there are backrefs
|
|
70 " s:pat parsed version of b:match_words
|
|
71 " s:all regexp based on s:pat and the default groups
|
|
72 if !exists("b:match_words") || b:match_words == ""
|
|
73 let match_words = ""
|
|
74 elseif b:match_words =~ ":"
|
|
75 let match_words = b:match_words
|
|
76 else
|
|
77 " Allow b:match_words = "GetVimMatchWords()" .
|
|
78 execute "let match_words =" b:match_words
|
|
79 endif
|
|
80 " Thanks to Preben "Peppe" Guldberg and Bram Moolenaar for this suggestion!
|
|
81 if (match_words != s:last_words) || (&mps != s:last_mps)
|
|
82 \ || exists("b:match_debug")
|
|
83 let s:last_mps = &mps
|
|
84 " quote the special chars in 'matchpairs', replace [,:] with \| and then
|
|
85 " append the builtin pairs (/*, */, #if, #ifdef, #ifndef, #else, #elif,
|
|
86 " #endif)
|
26708
|
87 let default = escape(&mps, '[$^.*~\\/?]') .. (strlen(&mps) ? "," : "") ..
|
15735
|
88 \ '\/\*:\*\/,#\s*if\%(n\=def\)\=:#\s*else\>:#\s*elif\>:#\s*endif\>'
|
|
89 " s:all = pattern with all the keywords
|
26708
|
90 let match_words = match_words .. (strlen(match_words) ? "," : "") .. default
|
15735
|
91 let s:last_words = match_words
|
26708
|
92 if match_words !~ s:notslash .. '\\\d'
|
15735
|
93 let s:do_BR = 0
|
|
94 let s:pat = match_words
|
|
95 else
|
|
96 let s:do_BR = 1
|
|
97 let s:pat = s:ParseWords(match_words)
|
|
98 endif
|
26708
|
99 let s:all = substitute(s:pat, s:notslash .. '\zs[,:]\+', '\\|', 'g')
|
15735
|
100 " Just in case there are too many '\(...)' groups inside the pattern, make
|
|
101 " sure to use \%(...) groups, so that error E872 can be avoided
|
|
102 let s:all = substitute(s:all, '\\(', '\\%(', 'g')
|
26708
|
103 let s:all = '\%(' .. s:all .. '\)'
|
15735
|
104 if exists("b:match_debug")
|
|
105 let b:match_pat = s:pat
|
|
106 endif
|
|
107 " Reconstruct the version with unresolved backrefs.
|
26708
|
108 let s:patBR = substitute(match_words .. ',',
|
|
109 \ s:notslash .. '\zs[,:]*,[,:]*', ',', 'g')
|
|
110 let s:patBR = substitute(s:patBR, s:notslash .. '\zs:\{2,}', ':', 'g')
|
15735
|
111 endif
|
|
112
|
|
113 " Second step: set the following local variables:
|
|
114 " matchline = line on which the cursor started
|
|
115 " curcol = number of characters before match
|
|
116 " prefix = regexp for start of line to start of match
|
|
117 " suffix = regexp for end of match to end of line
|
|
118 " Require match to end on or after the cursor and prefer it to
|
|
119 " start on or before the cursor.
|
|
120 let matchline = getline(startpos[0])
|
|
121 if a:word != ''
|
|
122 " word given
|
|
123 if a:word !~ s:all
|
|
124 echohl WarningMsg|echo 'Missing rule for word:"'.a:word.'"'|echohl NONE
|
|
125 return s:CleanUp(restore_options, a:mode, startpos)
|
|
126 endif
|
|
127 let matchline = a:word
|
|
128 let curcol = 0
|
|
129 let prefix = '^\%('
|
|
130 let suffix = '\)$'
|
|
131 " Now the case when "word" is not given
|
|
132 else " Find the match that ends on or after the cursor and set curcol.
|
|
133 let regexp = s:Wholematch(matchline, s:all, startpos[1]-1)
|
|
134 let curcol = match(matchline, regexp)
|
|
135 " If there is no match, give up.
|
|
136 if curcol == -1
|
26708
|
137 " Make sure macros abort properly
|
|
138 "exe "norm! \<esc>"
|
|
139 call feedkeys("\e", 'tni')
|
15735
|
140 return s:CleanUp(restore_options, a:mode, startpos)
|
|
141 endif
|
|
142 let endcol = matchend(matchline, regexp)
|
|
143 let suf = strlen(matchline) - endcol
|
26708
|
144 let prefix = (curcol ? '^.*\%' .. (curcol + 1) .. 'c\%(' : '^\%(')
|
|
145 let suffix = (suf ? '\)\%' .. (endcol + 1) .. 'c.*$' : '\)$')
|
15735
|
146 endif
|
|
147 if exists("b:match_debug")
|
|
148 let b:match_match = matchstr(matchline, regexp)
|
|
149 let b:match_col = curcol+1
|
|
150 endif
|
|
151
|
|
152 " Third step: Find the group and single word that match, and the original
|
|
153 " (backref) versions of these. Then, resolve the backrefs.
|
|
154 " Set the following local variable:
|
|
155 " group = colon-separated list of patterns, one of which matches
|
|
156 " = ini:mid:fin or ini:fin
|
|
157 "
|
|
158 " Now, set group and groupBR to the matching group: 'if:endif' or
|
|
159 " 'while:endwhile' or whatever. A bit of a kluge: s:Choose() returns
|
|
160 " group . "," . groupBR, and we pick it apart.
|
|
161 let group = s:Choose(s:pat, matchline, ",", ":", prefix, suffix, s:patBR)
|
26708
|
162 let i = matchend(group, s:notslash .. ",")
|
15735
|
163 let groupBR = strpart(group, i)
|
|
164 let group = strpart(group, 0, i-1)
|
|
165 " Now, matchline =~ prefix . substitute(group,':','\|','g') . suffix
|
|
166 if s:do_BR " Do the hard part: resolve those backrefs!
|
|
167 let group = s:InsertRefs(groupBR, prefix, group, suffix, matchline)
|
|
168 endif
|
|
169 if exists("b:match_debug")
|
|
170 let b:match_wholeBR = groupBR
|
26708
|
171 let i = matchend(groupBR, s:notslash .. ":")
|
15735
|
172 let b:match_iniBR = strpart(groupBR, 0, i-1)
|
|
173 endif
|
|
174
|
|
175 " Fourth step: Set the arguments for searchpair().
|
26708
|
176 let i = matchend(group, s:notslash .. ":")
|
|
177 let j = matchend(group, '.*' .. s:notslash .. ":")
|
15735
|
178 let ini = strpart(group, 0, i-1)
|
26708
|
179 let mid = substitute(strpart(group, i,j-i-1), s:notslash .. '\zs:', '\\|', 'g')
|
15735
|
180 let fin = strpart(group, j)
|
|
181 "Un-escape the remaining , and : characters.
|
26708
|
182 let ini = substitute(ini, s:notslash .. '\zs\\\(:\|,\)', '\1', 'g')
|
|
183 let mid = substitute(mid, s:notslash .. '\zs\\\(:\|,\)', '\1', 'g')
|
|
184 let fin = substitute(fin, s:notslash .. '\zs\\\(:\|,\)', '\1', 'g')
|
15735
|
185 " searchpair() requires that these patterns avoid \(\) groups.
|
26708
|
186 let ini = substitute(ini, s:notslash .. '\zs\\(', '\\%(', 'g')
|
|
187 let mid = substitute(mid, s:notslash .. '\zs\\(', '\\%(', 'g')
|
|
188 let fin = substitute(fin, s:notslash .. '\zs\\(', '\\%(', 'g')
|
15735
|
189 " Set mid. This is optimized for readability, not micro-efficiency!
|
26708
|
190 if a:forward && matchline =~ prefix .. fin .. suffix
|
|
191 \ || !a:forward && matchline =~ prefix .. ini .. suffix
|
15735
|
192 let mid = ""
|
|
193 endif
|
|
194 " Set flag. This is optimized for readability, not micro-efficiency!
|
26708
|
195 if a:forward && matchline =~ prefix .. fin .. suffix
|
|
196 \ || !a:forward && matchline !~ prefix .. ini .. suffix
|
15735
|
197 let flag = "bW"
|
|
198 else
|
|
199 let flag = "W"
|
|
200 endif
|
|
201 " Set skip.
|
|
202 if exists("b:match_skip")
|
|
203 let skip = b:match_skip
|
|
204 elseif exists("b:match_comment") " backwards compatibility and testing!
|
26708
|
205 let skip = "r:" .. b:match_comment
|
15735
|
206 else
|
|
207 let skip = 's:comment\|string'
|
|
208 endif
|
|
209 let skip = s:ParseSkip(skip)
|
|
210 if exists("b:match_debug")
|
|
211 let b:match_ini = ini
|
26708
|
212 let b:match_tail = (strlen(mid) ? mid .. '\|' : '') .. fin
|
15735
|
213 endif
|
|
214
|
|
215 " Fifth step: actually start moving the cursor and call searchpair().
|
|
216 " Later, :execute restore_cursor to get to the original screen.
|
|
217 let view = winsaveview()
|
|
218 call cursor(0, curcol + 1)
|
|
219 if skip =~ 'synID' && !(has("syntax") && exists("g:syntax_on"))
|
|
220 let skip = "0"
|
|
221 else
|
26708
|
222 execute "if " .. skip .. "| let skip = '0' | endif"
|
15735
|
223 endif
|
|
224 let sp_return = searchpair(ini, mid, fin, flag, skip)
|
18456
|
225 if &selection isnot# 'inclusive' && a:mode == 'v'
|
|
226 " move cursor one pos to the right, because selection is not inclusive
|
26708
|
227 " add virtualedit=onemore, to make it work even when the match ends the
|
|
228 " line
|
18456
|
229 if !(col('.') < col('$')-1)
|
26708
|
230 let eolmark=1 " flag to set a mark on eol (since we cannot move there)
|
18456
|
231 endif
|
|
232 norm! l
|
|
233 endif
|
26708
|
234 let final_position = "call cursor(" .. line(".") .. "," .. col(".") .. ")"
|
15735
|
235 " Restore cursor position and original screen.
|
|
236 call winrestview(view)
|
|
237 normal! m'
|
|
238 if sp_return > 0
|
|
239 execute final_position
|
|
240 endif
|
26708
|
241 if exists('eolmark') && eolmark
|
|
242 call setpos("''", [0, line('.'), col('$'), 0]) " set mark on the eol
|
|
243 endif
|
|
244 return s:CleanUp(restore_options, a:mode, startpos, mid .. '\|' .. fin)
|
15735
|
245 endfun
|
|
246
|
|
247 " Restore options and do some special handling for Operator-pending mode.
|
|
248 " The optional argument is the tail of the matching group.
|
|
249 fun! s:CleanUp(options, mode, startpos, ...)
|
|
250 if strlen(a:options)
|
|
251 execute "set" a:options
|
|
252 endif
|
|
253 " Open folds, if appropriate.
|
|
254 if a:mode != "o"
|
|
255 if &foldopen =~ "percent"
|
|
256 normal! zv
|
|
257 endif
|
|
258 " In Operator-pending mode, we want to include the whole match
|
|
259 " (for example, d%).
|
|
260 " This is only a problem if we end up moving in the forward direction.
|
|
261 elseif (a:startpos[0] < line(".")) ||
|
|
262 \ (a:startpos[0] == line(".") && a:startpos[1] < col("."))
|
|
263 if a:0
|
|
264 " Check whether the match is a single character. If not, move to the
|
|
265 " end of the match.
|
|
266 let matchline = getline(".")
|
|
267 let currcol = col(".")
|
|
268 let regexp = s:Wholematch(matchline, a:1, currcol-1)
|
|
269 let endcol = matchend(matchline, regexp)
|
|
270 if endcol > currcol " This is NOT off by one!
|
|
271 call cursor(0, endcol)
|
|
272 endif
|
|
273 endif " a:0
|
|
274 endif " a:mode != "o" && etc.
|
|
275 return 0
|
|
276 endfun
|
|
277
|
|
278 " Example (simplified HTML patterns): if
|
|
279 " a:groupBR = '<\(\k\+\)>:</\1>'
|
|
280 " a:prefix = '^.\{3}\('
|
|
281 " a:group = '<\(\k\+\)>:</\(\k\+\)>'
|
|
282 " a:suffix = '\).\{2}$'
|
|
283 " a:matchline = "123<tag>12" or "123</tag>12"
|
|
284 " then extract "tag" from a:matchline and return "<tag>:</tag>" .
|
|
285 fun! s:InsertRefs(groupBR, prefix, group, suffix, matchline)
|
26708
|
286 if a:matchline !~ a:prefix ..
|
|
287 \ substitute(a:group, s:notslash .. '\zs:', '\\|', 'g') .. a:suffix
|
15735
|
288 return a:group
|
|
289 endif
|
26708
|
290 let i = matchend(a:groupBR, s:notslash .. ':')
|
15735
|
291 let ini = strpart(a:groupBR, 0, i-1)
|
|
292 let tailBR = strpart(a:groupBR, i)
|
|
293 let word = s:Choose(a:group, a:matchline, ":", "", a:prefix, a:suffix,
|
|
294 \ a:groupBR)
|
26708
|
295 let i = matchend(word, s:notslash .. ":")
|
15735
|
296 let wordBR = strpart(word, i)
|
|
297 let word = strpart(word, 0, i-1)
|
|
298 " Now, a:matchline =~ a:prefix . word . a:suffix
|
|
299 if wordBR != ini
|
|
300 let table = s:Resolve(ini, wordBR, "table")
|
|
301 else
|
|
302 let table = ""
|
|
303 let d = 0
|
|
304 while d < 10
|
26708
|
305 if tailBR =~ s:notslash .. '\\' .. d
|
|
306 let table = table .. d
|
15735
|
307 else
|
26708
|
308 let table = table .. "-"
|
15735
|
309 endif
|
|
310 let d = d + 1
|
|
311 endwhile
|
|
312 endif
|
|
313 let d = 9
|
|
314 while d
|
|
315 if table[d] != "-"
|
26708
|
316 let backref = substitute(a:matchline, a:prefix .. word .. a:suffix,
|
|
317 \ '\' .. table[d], "")
|
15735
|
318 " Are there any other characters that should be escaped?
|
|
319 let backref = escape(backref, '*,:')
|
|
320 execute s:Ref(ini, d, "start", "len")
|
26708
|
321 let ini = strpart(ini, 0, start) .. backref .. strpart(ini, start+len)
|
|
322 let tailBR = substitute(tailBR, s:notslash .. '\zs\\' .. d,
|
15735
|
323 \ escape(backref, '\\&'), 'g')
|
|
324 endif
|
|
325 let d = d-1
|
|
326 endwhile
|
|
327 if exists("b:match_debug")
|
|
328 if s:do_BR
|
|
329 let b:match_table = table
|
|
330 let b:match_word = word
|
|
331 else
|
|
332 let b:match_table = ""
|
|
333 let b:match_word = ""
|
|
334 endif
|
|
335 endif
|
26708
|
336 return ini .. ":" .. tailBR
|
15735
|
337 endfun
|
|
338
|
|
339 " Input a comma-separated list of groups with backrefs, such as
|
|
340 " a:groups = '\(foo\):end\1,\(bar\):end\1'
|
|
341 " and return a comma-separated list of groups with backrefs replaced:
|
|
342 " return '\(foo\):end\(foo\),\(bar\):end\(bar\)'
|
|
343 fun! s:ParseWords(groups)
|
26708
|
344 let groups = substitute(a:groups .. ",", s:notslash .. '\zs[,:]*,[,:]*', ',', 'g')
|
|
345 let groups = substitute(groups, s:notslash .. '\zs:\{2,}', ':', 'g')
|
15735
|
346 let parsed = ""
|
|
347 while groups =~ '[^,:]'
|
26708
|
348 let i = matchend(groups, s:notslash .. ':')
|
|
349 let j = matchend(groups, s:notslash .. ',')
|
15735
|
350 let ini = strpart(groups, 0, i-1)
|
26708
|
351 let tail = strpart(groups, i, j-i-1) .. ":"
|
15735
|
352 let groups = strpart(groups, j)
|
26708
|
353 let parsed = parsed .. ini
|
|
354 let i = matchend(tail, s:notslash .. ':')
|
15735
|
355 while i != -1
|
|
356 " In 'if:else:endif', ini='if' and word='else' and then word='endif'.
|
|
357 let word = strpart(tail, 0, i-1)
|
|
358 let tail = strpart(tail, i)
|
26708
|
359 let i = matchend(tail, s:notslash .. ':')
|
|
360 let parsed = parsed .. ":" .. s:Resolve(ini, word, "word")
|
15735
|
361 endwhile " Now, tail has been used up.
|
26708
|
362 let parsed = parsed .. ","
|
15735
|
363 endwhile " groups =~ '[^,:]'
|
|
364 let parsed = substitute(parsed, ',$', '', '')
|
|
365 return parsed
|
|
366 endfun
|
|
367
|
|
368 " TODO I think this can be simplified and/or made more efficient.
|
|
369 " TODO What should I do if a:start is out of range?
|
|
370 " Return a regexp that matches all of a:string, such that
|
|
371 " matchstr(a:string, regexp) represents the match for a:pat that starts
|
|
372 " as close to a:start as possible, before being preferred to after, and
|
|
373 " ends after a:start .
|
|
374 " Usage:
|
|
375 " let regexp = s:Wholematch(getline("."), 'foo\|bar', col(".")-1)
|
|
376 " let i = match(getline("."), regexp)
|
|
377 " let j = matchend(getline("."), regexp)
|
|
378 " let match = matchstr(getline("."), regexp)
|
|
379 fun! s:Wholematch(string, pat, start)
|
26708
|
380 let group = '\%(' .. a:pat .. '\)'
|
|
381 let prefix = (a:start ? '\(^.*\%<' .. (a:start + 2) .. 'c\)\zs' : '^')
|
15735
|
382 let len = strlen(a:string)
|
26708
|
383 let suffix = (a:start+1 < len ? '\(\%>' .. (a:start+1) .. 'c.*$\)\@=' : '$')
|
|
384 if a:string !~ prefix .. group .. suffix
|
15735
|
385 let prefix = ''
|
|
386 endif
|
26708
|
387 return prefix .. group .. suffix
|
15735
|
388 endfun
|
|
389
|
|
390 " No extra arguments: s:Ref(string, d) will
|
|
391 " find the d'th occurrence of '\(' and return it, along with everything up
|
|
392 " to and including the matching '\)'.
|
|
393 " One argument: s:Ref(string, d, "start") returns the index of the start
|
|
394 " of the d'th '\(' and any other argument returns the length of the group.
|
|
395 " Two arguments: s:Ref(string, d, "foo", "bar") returns a string to be
|
|
396 " executed, having the effect of
|
|
397 " :let foo = s:Ref(string, d, "start")
|
|
398 " :let bar = s:Ref(string, d, "len")
|
|
399 fun! s:Ref(string, d, ...)
|
|
400 let len = strlen(a:string)
|
|
401 if a:d == 0
|
|
402 let start = 0
|
|
403 else
|
|
404 let cnt = a:d
|
|
405 let match = a:string
|
|
406 while cnt
|
|
407 let cnt = cnt - 1
|
26708
|
408 let index = matchend(match, s:notslash .. '\\(')
|
15735
|
409 if index == -1
|
|
410 return ""
|
|
411 endif
|
|
412 let match = strpart(match, index)
|
|
413 endwhile
|
|
414 let start = len - strlen(match)
|
|
415 if a:0 == 1 && a:1 == "start"
|
|
416 return start - 2
|
|
417 endif
|
|
418 let cnt = 1
|
|
419 while cnt
|
26708
|
420 let index = matchend(match, s:notslash .. '\\(\|\\)') - 1
|
15735
|
421 if index == -2
|
|
422 return ""
|
|
423 endif
|
|
424 " Increment if an open, decrement if a ')':
|
|
425 let cnt = cnt + (match[index]=="(" ? 1 : -1) " ')'
|
|
426 let match = strpart(match, index+1)
|
|
427 endwhile
|
|
428 let start = start - 2
|
|
429 let len = len - start - strlen(match)
|
|
430 endif
|
|
431 if a:0 == 1
|
|
432 return len
|
|
433 elseif a:0 == 2
|
26708
|
434 return "let " .. a:1 .. "=" .. start .. "| let " .. a:2 .. "=" .. len
|
15735
|
435 else
|
|
436 return strpart(a:string, start, len)
|
|
437 endif
|
|
438 endfun
|
|
439
|
|
440 " Count the number of disjoint copies of pattern in string.
|
|
441 " If the pattern is a literal string and contains no '0' or '1' characters
|
|
442 " then s:Count(string, pattern, '0', '1') should be faster than
|
|
443 " s:Count(string, pattern).
|
|
444 fun! s:Count(string, pattern, ...)
|
|
445 let pat = escape(a:pattern, '\\')
|
|
446 if a:0 > 1
|
26708
|
447 let foo = substitute(a:string, '[^' .. a:pattern .. ']', "a:1", "g")
|
15735
|
448 let foo = substitute(a:string, pat, a:2, "g")
|
26708
|
449 let foo = substitute(foo, '[^' .. a:2 .. ']', "", "g")
|
15735
|
450 return strlen(foo)
|
|
451 endif
|
|
452 let result = 0
|
|
453 let foo = a:string
|
|
454 let index = matchend(foo, pat)
|
|
455 while index != -1
|
|
456 let result = result + 1
|
|
457 let foo = strpart(foo, index)
|
|
458 let index = matchend(foo, pat)
|
|
459 endwhile
|
|
460 return result
|
|
461 endfun
|
|
462
|
|
463 " s:Resolve('\(a\)\(b\)', '\(c\)\2\1\1\2') should return table.word, where
|
|
464 " word = '\(c\)\(b\)\(a\)\3\2' and table = '-32-------'. That is, the first
|
|
465 " '\1' in target is replaced by '\(a\)' in word, table[1] = 3, and this
|
|
466 " indicates that all other instances of '\1' in target are to be replaced
|
|
467 " by '\3'. The hard part is dealing with nesting...
|
|
468 " Note that ":" is an illegal character for source and target,
|
|
469 " unless it is preceded by "\".
|
|
470 fun! s:Resolve(source, target, output)
|
|
471 let word = a:target
|
26708
|
472 let i = matchend(word, s:notslash .. '\\\d') - 1
|
15735
|
473 let table = "----------"
|
|
474 while i != -2 " There are back references to be replaced.
|
|
475 let d = word[i]
|
|
476 let backref = s:Ref(a:source, d)
|
|
477 " The idea is to replace '\d' with backref. Before we do this,
|
|
478 " replace any \(\) groups in backref with :1, :2, ... if they
|
|
479 " correspond to the first, second, ... group already inserted
|
|
480 " into backref. Later, replace :1 with \1 and so on. The group
|
|
481 " number w+b within backref corresponds to the group number
|
|
482 " s within a:source.
|
|
483 " w = number of '\(' in word before the current one
|
|
484 let w = s:Count(
|
|
485 \ substitute(strpart(word, 0, i-1), '\\\\', '', 'g'), '\(', '1')
|
|
486 let b = 1 " number of the current '\(' in backref
|
|
487 let s = d " number of the current '\(' in a:source
|
|
488 while b <= s:Count(substitute(backref, '\\\\', '', 'g'), '\(', '1')
|
|
489 \ && s < 10
|
|
490 if table[s] == "-"
|
|
491 if w + b < 10
|
|
492 " let table[s] = w + b
|
26708
|
493 let table = strpart(table, 0, s) .. (w+b) .. strpart(table, s+1)
|
15735
|
494 endif
|
|
495 let b = b + 1
|
|
496 let s = s + 1
|
|
497 else
|
|
498 execute s:Ref(backref, b, "start", "len")
|
|
499 let ref = strpart(backref, start, len)
|
26708
|
500 let backref = strpart(backref, 0, start) .. ":" .. table[s]
|
|
501 \ .. strpart(backref, start+len)
|
15735
|
502 let s = s + s:Count(substitute(ref, '\\\\', '', 'g'), '\(', '1')
|
|
503 endif
|
|
504 endwhile
|
26708
|
505 let word = strpart(word, 0, i-1) .. backref .. strpart(word, i+1)
|
|
506 let i = matchend(word, s:notslash .. '\\\d') - 1
|
15735
|
507 endwhile
|
26708
|
508 let word = substitute(word, s:notslash .. '\zs:', '\\', 'g')
|
15735
|
509 if a:output == "table"
|
|
510 return table
|
|
511 elseif a:output == "word"
|
|
512 return word
|
|
513 else
|
26708
|
514 return table .. word
|
15735
|
515 endif
|
|
516 endfun
|
|
517
|
|
518 " Assume a:comma = ",". Then the format for a:patterns and a:1 is
|
|
519 " a:patterns = "<pat1>,<pat2>,..."
|
|
520 " a:1 = "<alt1>,<alt2>,..."
|
|
521 " If <patn> is the first pattern that matches a:string then return <patn>
|
|
522 " if no optional arguments are given; return <patn>,<altn> if a:1 is given.
|
|
523 fun! s:Choose(patterns, string, comma, branch, prefix, suffix, ...)
|
26708
|
524 let tail = (a:patterns =~ a:comma .. "$" ? a:patterns : a:patterns .. a:comma)
|
|
525 let i = matchend(tail, s:notslash .. a:comma)
|
15735
|
526 if a:0
|
26708
|
527 let alttail = (a:1 =~ a:comma .. "$" ? a:1 : a:1 .. a:comma)
|
|
528 let j = matchend(alttail, s:notslash .. a:comma)
|
15735
|
529 endif
|
|
530 let current = strpart(tail, 0, i-1)
|
|
531 if a:branch == ""
|
|
532 let currpat = current
|
|
533 else
|
26708
|
534 let currpat = substitute(current, s:notslash .. a:branch, '\\|', 'g')
|
15735
|
535 endif
|
26708
|
536 while a:string !~ a:prefix .. currpat .. a:suffix
|
15735
|
537 let tail = strpart(tail, i)
|
26708
|
538 let i = matchend(tail, s:notslash .. a:comma)
|
15735
|
539 if i == -1
|
|
540 return -1
|
|
541 endif
|
|
542 let current = strpart(tail, 0, i-1)
|
|
543 if a:branch == ""
|
|
544 let currpat = current
|
|
545 else
|
26708
|
546 let currpat = substitute(current, s:notslash .. a:branch, '\\|', 'g')
|
15735
|
547 endif
|
|
548 if a:0
|
|
549 let alttail = strpart(alttail, j)
|
26708
|
550 let j = matchend(alttail, s:notslash .. a:comma)
|
15735
|
551 endif
|
|
552 endwhile
|
|
553 if a:0
|
26708
|
554 let current = current .. a:comma .. strpart(alttail, 0, j-1)
|
15735
|
555 endif
|
|
556 return current
|
|
557 endfun
|
|
558
|
|
559 fun! matchit#Match_debug()
|
|
560 let b:match_debug = 1 " Save debugging information.
|
|
561 " pat = all of b:match_words with backrefs parsed
|
|
562 amenu &Matchit.&pat :echo b:match_pat<CR>
|
|
563 " match = bit of text that is recognized as a match
|
|
564 amenu &Matchit.&match :echo b:match_match<CR>
|
|
565 " curcol = cursor column of the start of the matching text
|
|
566 amenu &Matchit.&curcol :echo b:match_col<CR>
|
|
567 " wholeBR = matching group, original version
|
|
568 amenu &Matchit.wh&oleBR :echo b:match_wholeBR<CR>
|
|
569 " iniBR = 'if' piece, original version
|
|
570 amenu &Matchit.ini&BR :echo b:match_iniBR<CR>
|
|
571 " ini = 'if' piece, with all backrefs resolved from match
|
|
572 amenu &Matchit.&ini :echo b:match_ini<CR>
|
|
573 " tail = 'else\|endif' piece, with all backrefs resolved from match
|
|
574 amenu &Matchit.&tail :echo b:match_tail<CR>
|
|
575 " fin = 'endif' piece, with all backrefs resolved from match
|
|
576 amenu &Matchit.&word :echo b:match_word<CR>
|
|
577 " '\'.d in ini refers to the same thing as '\'.table[d] in word.
|
26708
|
578 amenu &Matchit.t&able :echo '0:' .. b:match_table .. ':9'<CR>
|
15735
|
579 endfun
|
|
580
|
|
581 " Jump to the nearest unmatched "(" or "if" or "<tag>" if a:spflag == "bW"
|
|
582 " or the nearest unmatched "</tag>" or "endif" or ")" if a:spflag == "W".
|
|
583 " Return a "mark" for the original position, so that
|
|
584 " let m = MultiMatch("bW", "n") ... call winrestview(m)
|
|
585 " will return to the original position. If there is a problem, do not
|
|
586 " move the cursor and return {}, unless a count is given, in which case
|
|
587 " go up or down as many levels as possible and again return {}.
|
|
588 " TODO This relies on the same patterns as % matching. It might be a good
|
|
589 " idea to give it its own matching patterns.
|
|
590 fun! matchit#MultiMatch(spflag, mode)
|
|
591 let restore_options = s:RestoreOptions()
|
|
592 let startpos = [line("."), col(".")]
|
|
593 " save v:count1 variable, might be reset from the restore_cursor command
|
|
594 let level = v:count1
|
|
595 if a:mode == "o" && mode(1) !~# '[vV]'
|
|
596 exe "norm! v"
|
|
597 endif
|
|
598
|
|
599 " First step: if not already done, set the script variables
|
|
600 " s:do_BR flag for whether there are backrefs
|
|
601 " s:pat parsed version of b:match_words
|
|
602 " s:all regexp based on s:pat and the default groups
|
|
603 " This part is copied and slightly modified from matchit#Match_wrapper().
|
|
604 if !exists("b:match_words") || b:match_words == ""
|
|
605 let match_words = ""
|
|
606 " Allow b:match_words = "GetVimMatchWords()" .
|
|
607 elseif b:match_words =~ ":"
|
|
608 let match_words = b:match_words
|
|
609 else
|
|
610 execute "let match_words =" b:match_words
|
|
611 endif
|
|
612 if (match_words != s:last_words) || (&mps != s:last_mps) ||
|
|
613 \ exists("b:match_debug")
|
26708
|
614 let default = escape(&mps, '[$^.*~\\/?]') .. (strlen(&mps) ? "," : "") ..
|
15735
|
615 \ '\/\*:\*\/,#\s*if\%(n\=def\)\=:#\s*else\>:#\s*elif\>:#\s*endif\>'
|
|
616 let s:last_mps = &mps
|
26708
|
617 let match_words = match_words .. (strlen(match_words) ? "," : "") .. default
|
15735
|
618 let s:last_words = match_words
|
26708
|
619 if match_words !~ s:notslash .. '\\\d'
|
15735
|
620 let s:do_BR = 0
|
|
621 let s:pat = match_words
|
|
622 else
|
|
623 let s:do_BR = 1
|
|
624 let s:pat = s:ParseWords(match_words)
|
|
625 endif
|
26708
|
626 let s:all = '\%(' .. substitute(s:pat, '[,:]\+', '\\|', 'g') .. '\)'
|
15735
|
627 if exists("b:match_debug")
|
|
628 let b:match_pat = s:pat
|
|
629 endif
|
|
630 " Reconstruct the version with unresolved backrefs.
|
26708
|
631 let s:patBR = substitute(match_words .. ',',
|
|
632 \ s:notslash .. '\zs[,:]*,[,:]*', ',', 'g')
|
|
633 let s:patBR = substitute(s:patBR, s:notslash .. '\zs:\{2,}', ':', 'g')
|
15735
|
634 endif
|
|
635
|
|
636 " Second step: figure out the patterns for searchpair()
|
|
637 " and save the screen, cursor position, and 'ignorecase'.
|
|
638 " - TODO: A lot of this is copied from matchit#Match_wrapper().
|
|
639 " - maybe even more functionality should be split off
|
|
640 " - into separate functions!
|
26708
|
641 let openlist = split(s:pat .. ',', s:notslash .. '\zs:.\{-}' .. s:notslash .. ',')
|
|
642 let midclolist = split(',' .. s:pat, s:notslash .. '\zs,.\{-}' .. s:notslash .. ':')
|
|
643 call map(midclolist, {-> split(v:val, s:notslash .. ':')})
|
15735
|
644 let closelist = []
|
|
645 let middlelist = []
|
|
646 call map(midclolist, {i,v -> [extend(closelist, v[-1 : -1]),
|
|
647 \ extend(middlelist, v[0 : -2])]})
|
26708
|
648 call map(openlist, {i,v -> v =~# s:notslash .. '\\|' ? '\%(' .. v .. '\)' : v})
|
|
649 call map(middlelist, {i,v -> v =~# s:notslash .. '\\|' ? '\%(' .. v .. '\)' : v})
|
|
650 call map(closelist, {i,v -> v =~# s:notslash .. '\\|' ? '\%(' .. v .. '\)' : v})
|
15735
|
651 let open = join(openlist, ',')
|
|
652 let middle = join(middlelist, ',')
|
|
653 let close = join(closelist, ',')
|
|
654 if exists("b:match_skip")
|
|
655 let skip = b:match_skip
|
|
656 elseif exists("b:match_comment") " backwards compatibility and testing!
|
26708
|
657 let skip = "r:" .. b:match_comment
|
15735
|
658 else
|
|
659 let skip = 's:comment\|string'
|
|
660 endif
|
|
661 let skip = s:ParseSkip(skip)
|
|
662 let view = winsaveview()
|
|
663
|
|
664 " Third step: call searchpair().
|
|
665 " Replace '\('--but not '\\('--with '\%(' and ',' with '\|'.
|
26708
|
666 let openpat = substitute(open, '\%(' .. s:notslash .. '\)\@<=\\(', '\\%(', 'g')
|
15735
|
667 let openpat = substitute(openpat, ',', '\\|', 'g')
|
26708
|
668 let closepat = substitute(close, '\%(' .. s:notslash .. '\)\@<=\\(', '\\%(', 'g')
|
15735
|
669 let closepat = substitute(closepat, ',', '\\|', 'g')
|
26708
|
670 let middlepat = substitute(middle, '\%(' .. s:notslash .. '\)\@<=\\(', '\\%(', 'g')
|
15735
|
671 let middlepat = substitute(middlepat, ',', '\\|', 'g')
|
|
672
|
|
673 if skip =~ 'synID' && !(has("syntax") && exists("g:syntax_on"))
|
|
674 let skip = '0'
|
|
675 else
|
|
676 try
|
26708
|
677 execute "if " .. skip .. "| let skip = '0' | endif"
|
15735
|
678 catch /^Vim\%((\a\+)\)\=:E363/
|
|
679 " We won't find anything, so skip searching, should keep Vim responsive.
|
|
680 return {}
|
|
681 endtry
|
|
682 endif
|
|
683 mark '
|
|
684 while level
|
|
685 if searchpair(openpat, middlepat, closepat, a:spflag, skip) < 1
|
|
686 call s:CleanUp(restore_options, a:mode, startpos)
|
|
687 return {}
|
|
688 endif
|
|
689 let level = level - 1
|
|
690 endwhile
|
|
691
|
|
692 " Restore options and return a string to restore the original position.
|
|
693 call s:CleanUp(restore_options, a:mode, startpos)
|
|
694 return view
|
|
695 endfun
|
|
696
|
|
697 " Search backwards for "if" or "while" or "<tag>" or ...
|
|
698 " and return "endif" or "endwhile" or "</tag>" or ... .
|
|
699 " For now, this uses b:match_words and the same script variables
|
|
700 " as matchit#Match_wrapper() . Later, it may get its own patterns,
|
|
701 " either from a buffer variable or passed as arguments.
|
|
702 " fun! s:Autocomplete()
|
|
703 " echo "autocomplete not yet implemented :-("
|
|
704 " if !exists("b:match_words") || b:match_words == ""
|
|
705 " return ""
|
|
706 " end
|
|
707 " let startpos = matchit#MultiMatch("bW")
|
|
708 "
|
|
709 " if startpos == ""
|
|
710 " return ""
|
|
711 " endif
|
|
712 " " - TODO: figure out whether 'if' or '<tag>' matched, and construct
|
|
713 " " - the appropriate closing.
|
|
714 " let matchline = getline(".")
|
|
715 " let curcol = col(".") - 1
|
|
716 " " - TODO: Change the s:all argument if there is a new set of match pats.
|
|
717 " let regexp = s:Wholematch(matchline, s:all, curcol)
|
|
718 " let suf = strlen(matchline) - matchend(matchline, regexp)
|
|
719 " let prefix = (curcol ? '^.\{' . curcol . '}\%(' : '^\%(')
|
|
720 " let suffix = (suf ? '\).\{' . suf . '}$' : '\)$')
|
|
721 " " Reconstruct the version with unresolved backrefs.
|
|
722 " let patBR = substitute(b:match_words.',', '[,:]*,[,:]*', ',', 'g')
|
|
723 " let patBR = substitute(patBR, ':\{2,}', ':', "g")
|
|
724 " " Now, set group and groupBR to the matching group: 'if:endif' or
|
|
725 " " 'while:endwhile' or whatever.
|
|
726 " let group = s:Choose(s:pat, matchline, ",", ":", prefix, suffix, patBR)
|
|
727 " let i = matchend(group, s:notslash . ",")
|
|
728 " let groupBR = strpart(group, i)
|
|
729 " let group = strpart(group, 0, i-1)
|
|
730 " " Now, matchline =~ prefix . substitute(group,':','\|','g') . suffix
|
|
731 " if s:do_BR
|
|
732 " let group = s:InsertRefs(groupBR, prefix, group, suffix, matchline)
|
|
733 " endif
|
|
734 " " let g:group = group
|
|
735 "
|
|
736 " " - TODO: Construct the closing from group.
|
|
737 " let fake = "end" . expand("<cword>")
|
|
738 " execute startpos
|
|
739 " return fake
|
|
740 " endfun
|
|
741
|
|
742 " Close all open structures. "Get the heck out of here!"
|
|
743 " fun! s:Gthhoh()
|
|
744 " let close = s:Autocomplete()
|
|
745 " while strlen(close)
|
|
746 " put=close
|
|
747 " let close = s:Autocomplete()
|
|
748 " endwhile
|
|
749 " endfun
|
|
750
|
|
751 " Parse special strings as typical skip arguments for searchpair():
|
|
752 " s:foo becomes (current syntax item) =~ foo
|
|
753 " S:foo becomes (current syntax item) !~ foo
|
|
754 " r:foo becomes (line before cursor) =~ foo
|
|
755 " R:foo becomes (line before cursor) !~ foo
|
|
756 fun! s:ParseSkip(str)
|
|
757 let skip = a:str
|
|
758 if skip[1] == ":"
|
|
759 if skip[0] == "s"
|
26708
|
760 let skip = "synIDattr(synID(line('.'),col('.'),1),'name') =~? '" ..
|
|
761 \ strpart(skip,2) .. "'"
|
15735
|
762 elseif skip[0] == "S"
|
26708
|
763 let skip = "synIDattr(synID(line('.'),col('.'),1),'name') !~? '" ..
|
|
764 \ strpart(skip,2) .. "'"
|
15735
|
765 elseif skip[0] == "r"
|
27459
|
766 let skip = "strpart(getline('.'),0,col('.'))=~'" .. strpart(skip,2) .. "'"
|
15735
|
767 elseif skip[0] == "R"
|
27459
|
768 let skip = "strpart(getline('.'),0,col('.'))!~'" .. strpart(skip,2) .. "'"
|
15735
|
769 endif
|
|
770 endif
|
|
771 return skip
|
|
772 endfun
|
|
773
|
|
774 let &cpo = s:save_cpo
|
|
775 unlet s:save_cpo
|
|
776
|
|
777 " vim:sts=2:sw=2:et:
|