11" matchit.vim: (global plugin) Extended "%" matching
2- " Last Change: Mon May 15 10:00 PM 2006 EDT
2+ " Last Change: Fri Jan 25 10:00 AM 2008 EST
33" Maintainer: Benji Fisher PhD <[email protected] >4- " Version: 1.11 , for Vim 6.3+
4+ " Version: 1.13.2 , for Vim 6.3+
55" URL: http://www.vim.org/script.php?script_id=39
66
77" Documentation:
@@ -42,7 +42,7 @@ if exists("loaded_matchit") || &cp
4242endif
4343let loaded_matchit = 1
4444let s: last_mps = " "
45- let s: last_words = " "
45+ let s: last_words = " : "
4646
4747let s: save_cpo = &cpo
4848set cpo &vim
@@ -100,22 +100,21 @@ function! s:Match_wrapper(word, forward, mode) range
100100 " In s:CleanUp(), we may need to check whether the cursor moved forward.
101101 let startline = line (" ." )
102102 let startcol = col (" ." )
103- " Use default behavior if called with a count or if no patterns are defined .
103+ " Use default behavior if called with a count.
104104 if v: count
105105 exe " normal! " . v: count . " %"
106106 return s: CleanUp (restore_options, a: mode , startline, startcol)
107- elseif ! exists (" b:match_words" ) || b: match_words == " "
108- silent ! normal ! %
109- return s: CleanUp (restore_options, a: mode , startline, startcol)
110107 end
111108
112109 " First step: if not already done, set the script variables
113110 " s:do_BR flag for whether there are backrefs
114111 " s:pat parsed version of b:match_words
115112 " s:all regexp based on s:pat and the default groups
116113 "
117- " Allow b:match_words = "GetVimMatchWords()" .
118- if b: match_words = ~ " :"
114+ if ! exists (" b:match_words" ) || b: match_words == " "
115+ let match_words = " "
116+ " Allow b:match_words = "GetVimMatchWords()" .
117+ elseif b: match_words = ~ " :"
119118 let match_words = b: match_words
120119 else
121120 execute " let match_words =" b: match_words
@@ -125,13 +124,6 @@ function! s:Match_wrapper(word, forward, mode) range
125124 \ exists (" b:match_debug" )
126125 let s: last_words = match_words
127126 let s: last_mps = &mps
128- if match_words !~ s: notslash . ' \\\d'
129- let s: do_BR = 0
130- let s: pat = match_words
131- else
132- let s: do_BR = 1
133- let s: pat = s: ParseWords (match_words)
134- endif
135127 " The next several lines were here before
136128 " BF started messing with this script.
137129 " quote the special chars in 'matchpairs', replace [,:] with \| and then
@@ -141,8 +133,15 @@ function! s:Match_wrapper(word, forward, mode) range
141133 let default = escape (&mps , ' [$^.*~\\/?]' ) . (strlen (&mps ) ? " ," : " " ) .
142134 \ ' \/\*:\*\/,#if\%(def\)\=:#else\>:#elif\>:#endif\>'
143135 " s:all = pattern with all the keywords
144- let s: all = s: pat . (strlen (s: pat ) ? " ," : " " ) . default
145- let s: all = substitute (s: all , s: notslash . ' \zs[,:]\+' , ' \\|' , ' g' )
136+ let match_words = match_words . (strlen (match_words) ? " ," : " " ) . default
137+ if match_words !~ s: notslash . ' \\\d'
138+ let s: do_BR = 0
139+ let s: pat = match_words
140+ else
141+ let s: do_BR = 1
142+ let s: pat = s: ParseWords (match_words)
143+ endif
144+ let s: all = substitute (s: pat , s: notslash . ' \zs[,:]\+' , ' \\|' , ' g' )
146145 let s: all = ' \%(' . s: all . ' \)'
147146 " let s:all = '\%(' . substitute(s:all, '\\\ze[,:]', '', 'g') . '\)'
148147 if exists (" b:match_debug" )
@@ -172,16 +171,14 @@ function! s:Match_wrapper(word, forward, mode) range
172171 else " Find the match that ends on or after the cursor and set curcol.
173172 let regexp = s: Wholematch (matchline, s: all , startcol- 1 )
174173 let curcol = match (matchline, regexp)
174+ " If there is no match, give up.
175+ if curcol == -1
176+ return s: CleanUp (restore_options, a: mode , startline, startcol)
177+ endif
175178 let endcol = matchend (matchline, regexp)
176179 let suf = strlen (matchline) - endcol
177180 let prefix = (curcol ? ' ^.*\%' . (curcol + 1 ) . ' c\%(' : ' ^\%(' )
178181 let suffix = (suf ? ' \)\%' . (endcol + 1 ) . ' c.*$' : ' \)$' )
179- " If the match comes from the defaults, bail out.
180- if matchline !~ prefix .
181- \ substitute (s: pat , s: notslash .' \zs[,:]\+' , ' \\|' , ' g' ) . suffix
182- silent ! norm! %
183- return s: CleanUp (restore_options, a: mode , startline, startcol)
184- endif
185182 endif
186183 if exists (" b:match_debug" )
187184 let b: match_match = matchstr (matchline, regexp)
@@ -401,6 +398,7 @@ fun! s:ParseWords(groups)
401398 endwhile " Now, tail has been used up.
402399 let parsed = parsed . " ,"
403400 endwhile " groups =~ '[^,:]'
401+ let parsed = substitute (parsed, ' ,$' , ' ' , ' ' )
404402 return parsed
405403endfun
406404
@@ -651,7 +649,7 @@ fun! s:MultiMatch(spflag, mode)
651649 " s:all regexp based on s:pat and the default groups
652650 " This part is copied and slightly modified from s:Match_wrapper().
653651 let default = escape (&mps , ' [$^.*~\\/?]' ) . (strlen (&mps ) ? " ," : " " ) .
654- \ ' \/\*:\*\/,#if\%(def\)\=:$ else\>:#elif\>:#endif\>'
652+ \ ' \/\*:\*\/,#if\%(def\)\=:# else\>:#elif\>:#endif\>'
655653 " Allow b:match_words = "GetVimMatchWords()" .
656654 if b: match_words = ~ " :"
657655 let match_words = b: match_words
@@ -682,10 +680,12 @@ fun! s:MultiMatch(spflag, mode)
682680 " - maybe even more functionality should be split off
683681 " - into separate functions!
684682 let cdefault = (s: pat = ~ ' [^,]$' ? " ," : " " ) . default
685- let open = substitute (s: pat . cdefault, ' :[^,]*,' , ' \\),\\(' , ' g' )
686- let open = ' \(' . substitute (open , ' :[^,]*$' , ' \\)' , ' ' )
687- let close = substitute (s: pat . cdefault, ' ,[^,]*:' , ' \\),\\(' , ' g' )
688- let close = substitute (close , ' [^,]*:' , ' \\(' , ' ' ) . ' \)'
683+ let open = substitute (s: pat . cdefault,
684+ \ s: notslash . ' \zs:.\{-}' . s: notslash . ' ,' , ' \\),\\(' , ' g' )
685+ let open = ' \(' . substitute (open , s: notslash . ' \zs:.*$' , ' \\)' , ' ' )
686+ let close = substitute (s: pat . cdefault,
687+ \ s: notslash . ' \zs,.\{-}' . s: notslash . ' :' , ' \\),\\(' , ' g' )
688+ let close = substitute (close , ' ^.\{-}' . s: notslash . ' :' , ' \\(' , ' ' ) . ' \)'
689689 if exists (" b:match_skip" )
690690 let skip = b: match_skip
691691 elseif exists (" b:match_comment" ) " backwards compatibility and testing!
0 commit comments