matchit.vim 28 KB

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