comparison runtime/macros/matchit.vim @ 1623:53938adac247

updated for version 7.2a
author vimboss
date Tue, 24 Jun 2008 22:14:38 +0000
parents 96cd8222a819
children 7bc41231fbc7
comparison
equal deleted inserted replaced
1622:149d8b46404c 1623:53938adac247
1 " matchit.vim: (global plugin) Extended "%" matching 1 " 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
3 " Maintainer: Benji Fisher PhD <benji@member.AMS.org> 3 " Maintainer: Benji Fisher PhD <benji@member.AMS.org>
4 " Version: 1.11, for Vim 6.3+ 4 " Version: 1.13.2, for Vim 6.3+
5 " URL: http://www.vim.org/script.php?script_id=39 5 " URL: http://www.vim.org/script.php?script_id=39
6 6
7 " Documentation: 7 " Documentation:
8 " The documentation is in a separate file, matchit.txt . 8 " The documentation is in a separate file, matchit.txt .
9 9
40 if exists("loaded_matchit") || &cp 40 if exists("loaded_matchit") || &cp
41 finish 41 finish
42 endif 42 endif
43 let loaded_matchit = 1 43 let loaded_matchit = 1
44 let s:last_mps = "" 44 let s:last_mps = ""
45 let s:last_words = "" 45 let s:last_words = ":"
46 46
47 let s:save_cpo = &cpo 47 let s:save_cpo = &cpo
48 set cpo&vim 48 set cpo&vim
49 49
50 nnoremap <silent> % :<C-U>call <SID>Match_wrapper('',1,'n') <CR> 50 nnoremap <silent> % :<C-U>call <SID>Match_wrapper('',1,'n') <CR>
98 execute "normal! gv\<Esc>" 98 execute "normal! gv\<Esc>"
99 endif 99 endif
100 " In s:CleanUp(), we may need to check whether the cursor moved forward. 100 " In s:CleanUp(), we may need to check whether the cursor moved forward.
101 let startline = line(".") 101 let startline = line(".")
102 let startcol = col(".") 102 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.
104 if v:count 104 if v:count
105 exe "normal! " . v:count . "%" 105 exe "normal! " . v:count . "%"
106 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) 106 return s:CleanUp(restore_options, a:mode, startline, startcol)
110 end 107 end
111 108
112 " First step: if not already done, set the script variables 109 " First step: if not already done, set the script variables
113 " s:do_BR flag for whether there are backrefs 110 " s:do_BR flag for whether there are backrefs
114 " s:pat parsed version of b:match_words 111 " s:pat parsed version of b:match_words
115 " s:all regexp based on s:pat and the default groups 112 " s:all regexp based on s:pat and the default groups
116 " 113 "
117 " Allow b:match_words = "GetVimMatchWords()" . 114 if !exists("b:match_words") || b:match_words == ""
118 if b:match_words =~ ":" 115 let match_words = ""
116 " Allow b:match_words = "GetVimMatchWords()" .
117 elseif b:match_words =~ ":"
119 let match_words = b:match_words 118 let match_words = b:match_words
120 else 119 else
121 execute "let match_words =" b:match_words 120 execute "let match_words =" b:match_words
122 endif 121 endif
123 " Thanks to Preben "Peppe" Guldberg and Bram Moolenaar for this suggestion! 122 " Thanks to Preben "Peppe" Guldberg and Bram Moolenaar for this suggestion!
124 if (match_words != s:last_words) || (&mps != s:last_mps) || 123 if (match_words != s:last_words) || (&mps != s:last_mps) ||
125 \ exists("b:match_debug") 124 \ exists("b:match_debug")
126 let s:last_words = match_words 125 let s:last_words = match_words
127 let s:last_mps = &mps 126 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
135 " The next several lines were here before 127 " The next several lines were here before
136 " BF started messing with this script. 128 " BF started messing with this script.
137 " quote the special chars in 'matchpairs', replace [,:] with \| and then 129 " quote the special chars in 'matchpairs', replace [,:] with \| and then
138 " append the builtin pairs (/*, */, #if, #ifdef, #else, #elif, #endif) 130 " append the builtin pairs (/*, */, #if, #ifdef, #else, #elif, #endif)
139 " let default = substitute(escape(&mps, '[$^.*~\\/?]'), '[,:]\+', 131 " let default = substitute(escape(&mps, '[$^.*~\\/?]'), '[,:]\+',
140 " \ '\\|', 'g').'\|\/\*\|\*\/\|#if\>\|#ifdef\>\|#else\>\|#elif\>\|#endif\>' 132 " \ '\\|', 'g').'\|\/\*\|\*\/\|#if\>\|#ifdef\>\|#else\>\|#elif\>\|#endif\>'
141 let default = escape(&mps, '[$^.*~\\/?]') . (strlen(&mps) ? "," : "") . 133 let default = escape(&mps, '[$^.*~\\/?]') . (strlen(&mps) ? "," : "") .
142 \ '\/\*:\*\/,#if\%(def\)\=:#else\>:#elif\>:#endif\>' 134 \ '\/\*:\*\/,#if\%(def\)\=:#else\>:#elif\>:#endif\>'
143 " s:all = pattern with all the keywords 135 " s:all = pattern with all the keywords
144 let s:all = s:pat . (strlen(s:pat) ? "," : "") . default 136 let match_words = match_words . (strlen(match_words) ? "," : "") . default
145 let s:all = substitute(s:all, s:notslash . '\zs[,:]\+', '\\|', 'g') 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')
146 let s:all = '\%(' . s:all . '\)' 145 let s:all = '\%(' . s:all . '\)'
147 " let s:all = '\%(' . substitute(s:all, '\\\ze[,:]', '', 'g') . '\)' 146 " let s:all = '\%(' . substitute(s:all, '\\\ze[,:]', '', 'g') . '\)'
148 if exists("b:match_debug") 147 if exists("b:match_debug")
149 let b:match_pat = s:pat 148 let b:match_pat = s:pat
150 endif 149 endif
170 let suffix = '\)$' 169 let suffix = '\)$'
171 " Now the case when "word" is not given 170 " Now the case when "word" is not given
172 else " Find the match that ends on or after the cursor and set curcol. 171 else " Find the match that ends on or after the cursor and set curcol.
173 let regexp = s:Wholematch(matchline, s:all, startcol-1) 172 let regexp = s:Wholematch(matchline, s:all, startcol-1)
174 let curcol = match(matchline, regexp) 173 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
175 let endcol = matchend(matchline, regexp) 178 let endcol = matchend(matchline, regexp)
176 let suf = strlen(matchline) - endcol 179 let suf = strlen(matchline) - endcol
177 let prefix = (curcol ? '^.*\%' . (curcol + 1) . 'c\%(' : '^\%(') 180 let prefix = (curcol ? '^.*\%' . (curcol + 1) . 'c\%(' : '^\%(')
178 let suffix = (suf ? '\)\%' . (endcol + 1) . 'c.*$' : '\)$') 181 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
185 endif 182 endif
186 if exists("b:match_debug") 183 if exists("b:match_debug")
187 let b:match_match = matchstr(matchline, regexp) 184 let b:match_match = matchstr(matchline, regexp)
188 let b:match_col = curcol+1 185 let b:match_col = curcol+1
189 endif 186 endif
399 let i = matchend(tail, s:notslash . ':') 396 let i = matchend(tail, s:notslash . ':')
400 let parsed = parsed . ":" . s:Resolve(ini, word, "word") 397 let parsed = parsed . ":" . s:Resolve(ini, word, "word")
401 endwhile " Now, tail has been used up. 398 endwhile " Now, tail has been used up.
402 let parsed = parsed . "," 399 let parsed = parsed . ","
403 endwhile " groups =~ '[^,:]' 400 endwhile " groups =~ '[^,:]'
401 let parsed = substitute(parsed, ',$', '', '')
404 return parsed 402 return parsed
405 endfun 403 endfun
406 404
407 " TODO I think this can be simplified and/or made more efficient. 405 " TODO I think this can be simplified and/or made more efficient.
408 " TODO What should I do if a:start is out of range? 406 " TODO What should I do if a:start is out of range?
649 " s:do_BR flag for whether there are backrefs 647 " s:do_BR flag for whether there are backrefs
650 " s:pat parsed version of b:match_words 648 " s:pat parsed version of b:match_words
651 " s:all regexp based on s:pat and the default groups 649 " s:all regexp based on s:pat and the default groups
652 " This part is copied and slightly modified from s:Match_wrapper(). 650 " This part is copied and slightly modified from s:Match_wrapper().
653 let default = escape(&mps, '[$^.*~\\/?]') . (strlen(&mps) ? "," : "") . 651 let default = escape(&mps, '[$^.*~\\/?]') . (strlen(&mps) ? "," : "") .
654 \ '\/\*:\*\/,#if\%(def\)\=:$else\>:#elif\>:#endif\>' 652 \ '\/\*:\*\/,#if\%(def\)\=:#else\>:#elif\>:#endif\>'
655 " Allow b:match_words = "GetVimMatchWords()" . 653 " Allow b:match_words = "GetVimMatchWords()" .
656 if b:match_words =~ ":" 654 if b:match_words =~ ":"
657 let match_words = b:match_words 655 let match_words = b:match_words
658 else 656 else
659 execute "let match_words =" b:match_words 657 execute "let match_words =" b:match_words
680 " and save the screen, cursor position, and 'ignorecase'. 678 " and save the screen, cursor position, and 'ignorecase'.
681 " - TODO: A lot of this is copied from s:Match_wrapper(). 679 " - TODO: A lot of this is copied from s:Match_wrapper().
682 " - maybe even more functionality should be split off 680 " - maybe even more functionality should be split off
683 " - into separate functions! 681 " - into separate functions!
684 let cdefault = (s:pat =~ '[^,]$' ? "," : "") . default 682 let cdefault = (s:pat =~ '[^,]$' ? "," : "") . default
685 let open = substitute(s:pat . cdefault, ':[^,]*,', '\\),\\(', 'g') 683 let open = substitute(s:pat . cdefault,
686 let open = '\(' . substitute(open, ':[^,]*$', '\\)', '') 684 \ s:notslash . '\zs:.\{-}' . s:notslash . ',', '\\),\\(', 'g')
687 let close = substitute(s:pat . cdefault, ',[^,]*:', '\\),\\(', 'g') 685 let open = '\(' . substitute(open, s:notslash . '\zs:.*$', '\\)', '')
688 let close = substitute(close, '[^,]*:', '\\(', '') . '\)' 686 let close = substitute(s:pat . cdefault,
687 \ s:notslash . '\zs,.\{-}' . s:notslash . ':', '\\),\\(', 'g')
688 let close = substitute(close, '^.\{-}' . s:notslash . ':', '\\(', '') . '\)'
689 if exists("b:match_skip") 689 if exists("b:match_skip")
690 let skip = b:match_skip 690 let skip = b:match_skip
691 elseif exists("b:match_comment") " backwards compatibility and testing! 691 elseif exists("b:match_comment") " backwards compatibility and testing!
692 let skip = "r:" . b:match_comment 692 let skip = "r:" . b:match_comment
693 else 693 else