|
1 | 1 | " Insert or delete brackets, parens, quotes in pairs.
|
2 | 2 | " Maintainer: JiangMiao <jiangfriend@gmail.com>
|
3 | 3 | " Contributor: camthompson
|
4 |
| -" Last Change: 2012-05-16 |
5 |
| -" Version: 1.2.2 |
| 4 | +" Last Change: 2012-07-15 |
| 5 | +" Version: 1.2.3 |
6 | 6 | " Homepage: http://www.vim.org/scripts/script.php?script_id=3599
|
7 | 7 | " Repository: https://github.com/jiangmiao/auto-pairs
|
8 | 8 |
|
@@ -184,34 +184,62 @@ endfunction
|
184 | 184 | function! AutoPairsJump()
|
185 | 185 | call search('["\]'')}]','W')
|
186 | 186 | endfunction
|
| 187 | +" string_chunk cannot use standalone |
| 188 | +let s:string_chunk = '\v%(\\\_.|[^\1]|[\r\n]){-}' |
| 189 | +let s:ss_pattern = '\v''' . s:string_chunk . '''' |
| 190 | +let s:ds_pattern = '\v"' . s:string_chunk . '"' |
| 191 | + |
| 192 | +func! s:RegexpQuote(str) |
| 193 | + return substitute(a:str, '\v[\[\{\(\<\>\)\}\]]', '\\&', 'g') |
| 194 | +endf |
| 195 | + |
| 196 | +func! s:RegexpQuoteInSquare(str) |
| 197 | + return substitute(a:str, '\v[\[\]]', '\\&', 'g') |
| 198 | +endf |
| 199 | + |
| 200 | +" Search next open or close pair |
| 201 | +func! s:FormatChunk(open, close) |
| 202 | + let open = s:RegexpQuote(a:open) |
| 203 | + let close = s:RegexpQuote(a:close) |
| 204 | + let open2 = s:RegexpQuoteInSquare(a:open) |
| 205 | + let close2 = s:RegexpQuoteInSquare(a:close) |
| 206 | + if open == close |
| 207 | + return '\v'.open.s:string_chunk.close |
| 208 | + else |
| 209 | + return '\v%(' . s:ss_pattern . '|' . s:ds_pattern . '|' . '[^'.open2.close2.']|[\r\n]' . '){-}(['.open2.close2.'])' |
| 210 | + end |
| 211 | +endf |
187 | 212 |
|
188 | 213 | " Fast wrap the word in brackets
|
189 | 214 | function! AutoPairsFastWrap()
|
190 | 215 | let line = getline('.')
|
191 | 216 | let current_char = line[col('.')-1]
|
192 | 217 | let next_char = line[col('.')]
|
193 |
| - |
194 |
| - " Ignore EOL |
195 |
| - if col('.') == col('$') |
196 |
| - return '' |
197 |
| - end |
198 |
| - |
199 |
| - normal! x |
200 |
| - if next_char =~ '\s' |
201 |
| - call search('\S', 'W') |
202 |
| - let next_char = getline('.')[col('.')-1] |
| 218 | + let open_pair_pattern = '\v[({\[''"]' |
| 219 | + let at_end = col('.') >= col('$') - 1 |
| 220 | + normal x |
| 221 | + " Skip blank |
| 222 | + if next_char =~ '\v\s' || at_end |
| 223 | + call search('\v\S', 'W') |
| 224 | + let line = getline('.') |
| 225 | + let next_char = line[col('.')-1] |
203 | 226 | end
|
204 | 227 |
|
205 |
| - if has_key(g:AutoExtraPairs, next_char) |
206 |
| - let close = g:AutoExtraPairs[next_char] |
207 |
| - call search(close, 'W') |
208 |
| - return "\<RIGHT>".current_char."\<LEFT>" |
209 |
| - else |
210 |
| - if next_char =~ '\w' |
211 |
| - execute "normal! he" |
| 228 | + if has_key(g:AutoPairs, next_char) |
| 229 | + let followed_open_pair = next_char |
| 230 | + let inputed_close_pair = current_char |
| 231 | + let followed_close_pair = g:AutoPairs[next_char] |
| 232 | + if followed_close_pair != followed_open_pair |
| 233 | + " TODO replace system searchpair to skip string and nested pair. |
| 234 | + " eg: (|){"hello}world"} will transform to ({"hello})world"} |
| 235 | + call searchpair('\V'.followed_open_pair, '', '\V'.followed_close_pair, 'W') |
| 236 | + else |
| 237 | + call search(s:FormatChunk(followed_open_pair, followed_close_pair), 'We') |
212 | 238 | end
|
213 |
| - execute "normal! a".current_char |
214 |
| - return "" |
| 239 | + return "\<RIGHT>".inputed_close_pair."\<LEFT>" |
| 240 | + else |
| 241 | + normal e |
| 242 | + return "\<RIGHT>".current_char."\<LEFT>" |
215 | 243 | end
|
216 | 244 | endfunction
|
217 | 245 |
|
@@ -352,11 +380,20 @@ function! AutoPairsForceInit()
|
352 | 380 | let old_cr = s:ExpandMap(old_cr)
|
353 | 381 | endif
|
354 | 382 |
|
| 383 | + " compatible with clang_complete |
| 384 | + " https://github.com/jiangmiao/auto-pairs/issues/18 |
| 385 | + let pattern = '<SNR>\d\+_HandlePossibleSelectionEnter()' |
| 386 | + if old_cr =~ pattern |
| 387 | + execute 'imap <expr> <script> <SID>AutoPairsClangCompleteCR ' . matchstr(old_cr, pattern) |
| 388 | + let old_cr = substitute(old_cr, pattern , '<SID>AutoPairsClangCompleteCR', '') |
| 389 | + endif |
| 390 | + |
355 | 391 | if old_cr !~ 'AutoPairsReturn'
|
356 | 392 | " generally speaking, <silent> should not be here because every plugin
|
357 | 393 | " has there own silent solution. but for some plugin which wasn't double silent
|
358 | 394 | " mapping, when maparg expand the map will lose the silent info, so <silent> always.
|
359 |
| - execute 'imap <buffer> <silent> <CR> '.old_cr.'<SID>AutoPairsReturn' |
| 395 | + " use inoremap for neocomplcache |
| 396 | + execute 'inoremap <script> <buffer> <silent> <CR> '.old_cr.'<SID>AutoPairsReturn' |
360 | 397 | end
|
361 | 398 | endif
|
362 | 399 | call AutoPairsInit()
|
|
0 commit comments