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-14
5
- " Version: 1.2.0
4
+ " Last Change: 2012-05-16
5
+ " Version: 1.2.2
6
6
" Homepage: http://www.vim.org/scripts/script.php?script_id=3599
7
7
" Repository: https://github.com/jiangmiao/auto-pairs
8
8
53
53
" Fly mode will for closed pair to jump to closed pair instead of insert.
54
54
" also support AutoPairsBackInsert to insert pairs where jumped.
55
55
if ! exists (' g:AutoPairsFlyMode' )
56
- let g: AutoPairsFlyMode = 1
56
+ let g: AutoPairsFlyMode = 0
57
57
endif
58
58
59
59
" Work with Fly Mode, insert pair where jumped
@@ -62,7 +62,7 @@ if !exists('g:AutoPairsShortcutBackInsert')
62
62
endif
63
63
64
64
65
- " Will auto generated {']' => 1 , ..., '}' => 1 }in initialize.
65
+ " Will auto generated {']' => '[' , ..., '}' => '{' }in initialize.
66
66
let g: AutoPairsClosedPairs = {}
67
67
68
68
@@ -88,33 +88,35 @@ function! AutoPairsInsert(key)
88
88
89
89
" The key is difference open-pair, then it means only for ) ] } by default
90
90
if ! has_key (g: AutoPairs , a: key )
91
- " Skip the character if next character is space
92
- if current_char == ' ' && next_char == a: key
93
- return " \<Right>\<Right> "
94
- end
95
-
96
- " Skip the character if closed pair is next character
97
- if current_char == ' '
98
- let next_lineno = line (' .' )+ 1
99
- let next_line = getline (nextnonblank (next_lineno))
100
- let next_char = matchstr (next_line, ' \s*\zs.' )
101
- if next_char == a: key
102
- return " \<ESC> e^a"
103
- endif
104
- endif
91
+ let b: autopairs_saved_pair = [a: key , getpos (' .' )]
105
92
106
93
" Skip the character if current character is the same as input
107
94
if current_char == a: key
108
95
return " \<Right> "
109
96
end
110
97
98
+ if ! g: AutoPairsFlyMode
99
+ " Skip the character if next character is space
100
+ if current_char == ' ' && next_char == a: key
101
+ return " \<Right>\<Right> "
102
+ end
103
+
104
+ " Skip the character if closed pair is next character
105
+ if current_char == ' '
106
+ let next_lineno = line (' .' )+ 1
107
+ let next_line = getline (nextnonblank (next_lineno))
108
+ let next_char = matchstr (next_line, ' \s*\zs.' )
109
+ if next_char == a: key
110
+ return " \<ESC> e^a"
111
+ endif
112
+ endif
113
+ endif
114
+
111
115
" Fly Mode, and the key is closed-pairs, search closed-pair and jump
112
116
if g: AutoPairsFlyMode && has_key (g: AutoPairsClosedPairs , a: key )
113
- let b: autopairs_saved_pair = [a: key , getpos (' .' )]
114
- " Use 's' flag to overwritee '' or not is a question
115
- if (search (a: key , ' W' ))
117
+ if search (a: key , ' W' )
116
118
return " \<Right> "
117
- end
119
+ endif
118
120
endif
119
121
120
122
" Input directly if the key is not an open key
@@ -215,6 +217,7 @@ endfunction
215
217
216
218
function ! AutoPairsMap (key )
217
219
let escaped_key = substitute (a: key , " '" , " ''" , ' g' )
220
+ " use expr will cause search() doesn't work
218
221
execute ' inoremap <buffer> <silent> ' .a: key ." <C-R>=AutoPairsInsert('" .escaped_key." ')<CR>"
219
222
endfunction
220
223
@@ -282,16 +285,17 @@ function! AutoPairsInit()
282
285
if open != close
283
286
call AutoPairsMap (close )
284
287
end
285
- let g: AutoPairsClosedPairs [close ] = 1
288
+ let g: AutoPairsClosedPairs [close ] = open
286
289
endfor
287
290
288
291
" Still use <buffer> level mapping for <BS> <SPACE>
289
292
if g: AutoPairsMapBS
290
- execute ' inoremap <buffer> <silent> <expr> <BS> AutoPairsDelete()'
293
+ " Use <C-R> instead of <expr> for issue #14 sometimes press BS output strange words
294
+ execute ' inoremap <buffer> <silent> <BS> <C-R>=AutoPairsDelete()<CR>'
291
295
end
292
296
293
297
if g: AutoPairsMapSpace
294
- execute ' inoremap <buffer> <silent> <expr > <SPACE> AutoPairsSpace()'
298
+ execute ' inoremap <buffer> <silent> <SPACE > <C-R>= AutoPairsSpace()<CR> '
295
299
end
296
300
297
301
if g: AutoPairsShortcutFastWrap != ' '
@@ -315,28 +319,52 @@ function! AutoPairsInit()
315
319
316
320
endfunction
317
321
322
+ function ! s: ExpandMap (map )
323
+ let map = a:map
324
+ if map =~ '<Plug> '
325
+ let map = substitute(map, '\(<Plug> \w\+\)', '\=maparg(submatch(1), "i")', 'g')
326
+ endif
327
+ return map
328
+ endfunction
329
+
318
330
function ! AutoPairsForceInit ()
319
331
if exists (' b:autopairs_loaded' )
320
332
return
321
- else
322
- call AutoPairsInit ()
333
+ end
334
+ " for auto-pairs starts with 'a', so the priority is higher than supertab and vim-endwise
335
+ "
336
+ " vim-endwise doesn't support <Plug>AutoPairsReturn
337
+ " when use <Plug>AutoPairsReturn will cause <Plug> isn't expanded
338
+ "
339
+ " supertab doesn't support <SID>AutoPairsReturn
340
+ " when use <SID>AutoPairsReturn will cause Duplicated <CR>
341
+ "
342
+ " and when load after vim-endwise will cause unexpected endwise inserted.
343
+ " so always load AutoPairs at last
344
+
345
+ " Buffer level keys mapping
346
+ " comptible with other plugin
347
+ if g: AutoPairsMapCR
348
+ let old_cr = maparg (' <CR>' , ' i' )
349
+ if old_cr == ' '
350
+ let old_cr = ' <CR>'
351
+ else
352
+ let old_cr = s: ExpandMap (old_cr)
353
+ endif
354
+
355
+ if old_cr !~ ' AutoPairsReturn'
356
+ " generally speaking, <silent> should not be here because every plugin
357
+ " has there own silent solution. but for some plugin which wasn't double silent
358
+ " mapping, when maparg expand the map will lose the silent info, so <silent> always.
359
+ execute ' imap <buffer> <silent> <CR> ' .old_cr.' <SID>AutoPairsReturn'
360
+ end
323
361
endif
362
+ call AutoPairsInit ()
324
363
endfunction
325
364
326
365
" Always silent the command
327
366
inoremap <silent> <SID> AutoPairsReturn <C-R> =AutoPairsReturn()<CR>
367
+ imap <script> <Plug> AutoPairsReturn <SID> AutoPairsReturn
328
368
329
- " Global keys mapping
330
- " comptible with other plugin
331
- if g: AutoPairsMapCR
332
- let old_cr = maparg (' <CR>' , ' i' )
333
- if old_cr == ' '
334
- let old_cr = ' <CR>'
335
- endif
336
-
337
- if old_cr !~ ' AutoPairsReturn'
338
- execute ' imap <CR> ' .old_cr.' <SID>AutoPairsReturn'
339
- end
340
- endif
341
369
342
370
au BufEnter * :call AutoPairsForceInit ()
0 commit comments