Skip to content

Commit da585a4

Browse files
jiangmiaovim-scripts
authored andcommitted
Version 1.2.2
compatible with vim-endwise, supertab. Disable Fly mode by default.
1 parent 587fca3 commit da585a4

File tree

2 files changed

+107
-73
lines changed

2 files changed

+107
-73
lines changed

README.md

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -80,44 +80,49 @@ Features
8080

8181
* Fly Mode
8282

83-
input: if(a[3)
84-
output: if(a[3])| (In Fly Mode)
85-
output: if(a[3)]) (Without Fly Mode)
83+
input: if(a[3)
84+
output: if(a[3])| (In Fly Mode)
85+
output: if(a[3)]) (Without Fly Mode)
8686

87-
input:
88-
{
89-
hello();|
90-
world();
91-
}
87+
input:
88+
{
89+
hello();|
90+
world();
91+
}
9292

93-
(press } at |)
93+
(press } at |)
9494

95-
output:
96-
{
97-
hello();
98-
world();
99-
}|
95+
output:
96+
{
97+
hello();
98+
world();
99+
}|
100100

101-
(then press <M-b> at | to do backinsert)
102-
output:
103-
{
104-
hello();}|
105-
world();
106-
}
101+
(then press <M-b> at | to do backinsert)
102+
output:
103+
{
104+
hello();}|
105+
world();
106+
}
107107

108-
See Fly Mode section for details
108+
See Fly Mode section for details
109109

110110
Fly Mode
111111
--------
112+
Fly Mode will always force closed-pair jumping instead of inserting. only for ")", "}", "]"
113+
114+
If jumps in mistake, could use AutoPairsBackInsert(Default Key: <M-b>) to jump back and insert closed pair.
115+
116+
the most situation maybe want to insert single closed pair in the string, eg ")"
117+
118+
Fly Mode is DISABLED by default.
112119

113-
Fly Mode will always force closed-pair jumping instead of inserting. only for ")", "}", "]"
114-
If jumps in mistake, could use AutoPairsBackInsert(Default Key: <M-b>) to jump back and insert closed pair.
115-
the most situation maybe want to insert single closed pair in the string, eg ")"
120+
add **let g:AutoPairsFlyMode = 1** .vimrc to turn it on
116121

117-
Default Options:
122+
Default Options:
118123

119-
let g:AutoPairs_FlyMode = 1
120-
let g:AutoPairsShortcutBackInsert = '<M-b>'
124+
let g:AutoPairsFlyMode = 0
125+
let g:AutoPairsShortcutBackInsert = '<M-b>'
121126

122127
Shortcuts
123128
---------
@@ -128,7 +133,7 @@ Shortcuts
128133
<M-p> : Toggle Autopairs (g:AutoPairsShortcutToggle)
129134
<M-e> : Fast Wrap (g:AutoPairsShortcutFastWrap)
130135
<M-n> : Jump to next closed pair (g:AutoPairsShortcutJump)
131-
<M-b> : BackInsert
136+
<M-b> : BackInsert
132137

133138
If <M-p> <M-e> or <M-n> conflict with another keys or want to bind to another keys, add
134139

@@ -189,17 +194,18 @@ Options
189194
Map <space> to insert a space after the opening character and before the closing one.
190195
execute 'inoremap <buffer> <silent> <CR> <C-R>=AutoPairsSpace()<CR>'
191196

192-
* g:AutoPairsFlyMode
197+
* g:AutoPairsFlyMode
193198

194-
Default : 1
199+
Default : 0
195200

196-
see FlyMode section for details.
201+
set it to 1 to enable FlyMode.
202+
see FlyMode section for details.
197203

198-
* g:AutoPairsShortcutBackInsert
204+
* g:AutoPairsShortcutBackInsert
199205

200-
Default : <M-b>
206+
Default : <M-b>
201207

202-
Work with FlyMode, insert the key at the Fly Mode jumped postion
208+
Work with FlyMode, insert the key at the Fly Mode jumped postion
203209

204210
TroubleShooting
205211
---------------

plugin/auto-pairs.vim

Lines changed: 67 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
" Insert or delete brackets, parens, quotes in pairs.
22
" Maintainer: JiangMiao <jiangfriend@gmail.com>
33
" Contributor: camthompson
4-
" Last Change: 2012-05-14
5-
" Version: 1.2.0
4+
" Last Change: 2012-05-16
5+
" Version: 1.2.2
66
" Homepage: http://www.vim.org/scripts/script.php?script_id=3599
77
" Repository: https://github.com/jiangmiao/auto-pairs
88

@@ -53,7 +53,7 @@ endif
5353
" Fly mode will for closed pair to jump to closed pair instead of insert.
5454
" also support AutoPairsBackInsert to insert pairs where jumped.
5555
if !exists('g:AutoPairsFlyMode')
56-
let g:AutoPairsFlyMode = 1
56+
let g:AutoPairsFlyMode = 0
5757
endif
5858

5959
" Work with Fly Mode, insert pair where jumped
@@ -62,7 +62,7 @@ if !exists('g:AutoPairsShortcutBackInsert')
6262
endif
6363

6464

65-
" Will auto generated {']' => 1, ..., '}' => 1}in initialize.
65+
" Will auto generated {']' => '[', ..., '}' => '{'}in initialize.
6666
let g:AutoPairsClosedPairs = {}
6767

6868

@@ -88,33 +88,35 @@ function! AutoPairsInsert(key)
8888

8989
" The key is difference open-pair, then it means only for ) ] } by default
9090
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('.')]
10592

10693
" Skip the character if current character is the same as input
10794
if current_char == a:key
10895
return "\<Right>"
10996
end
11097

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+
111115
" Fly Mode, and the key is closed-pairs, search closed-pair and jump
112116
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')
116118
return "\<Right>"
117-
end
119+
endif
118120
endif
119121

120122
" Input directly if the key is not an open key
@@ -215,6 +217,7 @@ endfunction
215217

216218
function! AutoPairsMap(key)
217219
let escaped_key = substitute(a:key, "'", "''", 'g')
220+
" use expr will cause search() doesn't work
218221
execute 'inoremap <buffer> <silent> '.a:key." <C-R>=AutoPairsInsert('".escaped_key."')<CR>"
219222
endfunction
220223

@@ -282,16 +285,17 @@ function! AutoPairsInit()
282285
if open != close
283286
call AutoPairsMap(close)
284287
end
285-
let g:AutoPairsClosedPairs[close] = 1
288+
let g:AutoPairsClosedPairs[close] = open
286289
endfor
287290

288291
" Still use <buffer> level mapping for <BS> <SPACE>
289292
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>'
291295
end
292296

293297
if g:AutoPairsMapSpace
294-
execute 'inoremap <buffer> <silent> <expr> <SPACE> AutoPairsSpace()'
298+
execute 'inoremap <buffer> <silent> <SPACE> <C-R>=AutoPairsSpace()<CR>'
295299
end
296300

297301
if g:AutoPairsShortcutFastWrap != ''
@@ -315,28 +319,52 @@ function! AutoPairsInit()
315319

316320
endfunction
317321

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+
318330
function! AutoPairsForceInit()
319331
if exists('b:autopairs_loaded')
320332
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
323361
endif
362+
call AutoPairsInit()
324363
endfunction
325364

326365
" Always silent the command
327366
inoremap <silent> <SID>AutoPairsReturn <C-R>=AutoPairsReturn()<CR>
367+
imap <script> <Plug>AutoPairsReturn <SID>AutoPairsReturn
328368
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
341369

342370
au BufEnter * :call AutoPairsForceInit()

0 commit comments

Comments
 (0)