improve #29 Buffer level pairs controlling.

This commit is contained in:
Miao Jiang 2012-12-24 00:53:22 +08:00
parent 0cabb1be77
commit 36ebfb6f29
2 changed files with 35 additions and 16 deletions

View File

@ -154,6 +154,12 @@ Options
Default: {'(':')', '[':']', '{':'}',"'":"'",'"':'"', '`':'`'} Default: {'(':')', '[':']', '{':'}',"'":"'",'"':'"', '`':'`'}
* b:AutoPairs
Default: g:AutoPairs
Buffer level pairs set.
* g:AutoPairsShortcutToggle * g:AutoPairsShortcutToggle
Default: '<M-p>' Default: '<M-p>'
@ -214,6 +220,16 @@ Options
Work with FlyMode, insert the key at the Fly Mode jumped postion Work with FlyMode, insert the key at the Fly Mode jumped postion
Buffer Level Pairs Setting
--------------------------
Set b:AutoPairs before BufEnter
eg:
" When the filetype is FILETYPE then make AutoPairs only match for parenthesis
au Filetype FILETYPE let b:AutoPairs = {"(": ")"}
TroubleShooting TroubleShooting
--------------- ---------------
The script will remap keys ([{'"}]) <BS>, The script will remap keys ([{'"}]) <BS>,

View File

@ -19,9 +19,6 @@ if !exists('g:AutoPairsParens')
let g:AutoPairsParens = {'(':')', '[':']', '{':'}'} let g:AutoPairsParens = {'(':')', '[':']', '{':'}'}
end end
let g:AutoExtraPairs = copy(g:AutoPairs)
let g:AutoExtraPairs['<'] = '>'
if !exists('g:AutoPairsMapBS') if !exists('g:AutoPairsMapBS')
let g:AutoPairsMapBS = 1 let g:AutoPairsMapBS = 1
end end
@ -90,7 +87,7 @@ function! AutoPairsInsert(key)
end end
" The key is difference open-pair, then it means only for ) ] } by default " The key is difference open-pair, then it means only for ) ] } by default
if !has_key(g:AutoPairs, a:key) if !has_key(b:AutoPairs, a:key)
let b:autopairs_saved_pair = [a:key, getpos('.')] let b:autopairs_saved_pair = [a:key, getpos('.')]
" Skip the character if current character is the same as input " Skip the character if current character is the same as input
@ -116,7 +113,7 @@ function! AutoPairsInsert(key)
endif endif
" Fly Mode, and the key is closed-pairs, search closed-pair and jump " Fly Mode, and the key is closed-pairs, search closed-pair and jump
if g:AutoPairsFlyMode && has_key(g:AutoPairsClosedPairs, a:key) if g:AutoPairsFlyMode && has_key(b:AutoPairsClosedPairs, a:key)
if search(a:key, 'W') if search(a:key, 'W')
return "\<Right>" return "\<Right>"
endif endif
@ -127,7 +124,7 @@ function! AutoPairsInsert(key)
end end
let open = a:key let open = a:key
let close = g:AutoPairs[open] let close = b:AutoPairs[open]
if current_char == close && open == close if current_char == close && open == close
return "\<Right>" return "\<Right>"
@ -169,12 +166,12 @@ function! AutoPairsDelete()
end end
" Delete last two spaces in parens, work with MapSpace " Delete last two spaces in parens, work with MapSpace
if has_key(g:AutoPairs, pprev_char) && prev_char == ' ' && current_char == ' ' if has_key(b:AutoPairs, pprev_char) && prev_char == ' ' && current_char == ' '
return "\<BS>\<DEL>" return "\<BS>\<DEL>"
endif endif
if has_key(g:AutoPairs, prev_char) if has_key(b:AutoPairs, prev_char)
let close = g:AutoPairs[prev_char] let close = b:AutoPairs[prev_char]
if match(line,'^\s*'.close, col('.')-1) != -1 if match(line,'^\s*'.close, col('.')-1) != -1
let space = matchstr(line, '^\s*', col('.')-1) let space = matchstr(line, '^\s*', col('.')-1)
return "\<BS>". repeat("\<DEL>", len(space)+1) return "\<BS>". repeat("\<DEL>", len(space)+1)
@ -234,10 +231,10 @@ function! AutoPairsFastWrap()
let next_char = line[col('.')-1] let next_char = line[col('.')-1]
end end
if has_key(g:AutoPairs, next_char) if has_key(b:AutoPairs, next_char)
let followed_open_pair = next_char let followed_open_pair = next_char
let inputed_close_pair = current_char let inputed_close_pair = current_char
let followed_close_pair = g:AutoPairs[next_char] let followed_close_pair = b:AutoPairs[next_char]
if followed_close_pair != followed_open_pair if followed_close_pair != followed_open_pair
" TODO replace system searchpair to skip string and nested pair. " TODO replace system searchpair to skip string and nested pair.
" eg: (|){"hello}world"} will transform to ({"hello})world"} " eg: (|){"hello}world"} will transform to ({"hello})world"}
@ -278,7 +275,7 @@ function! AutoPairsReturn()
let prev_char = pline[strlen(pline)-1] let prev_char = pline[strlen(pline)-1]
let cmd = '' let cmd = ''
let cur_char = line[col('.')-1] let cur_char = line[col('.')-1]
if has_key(g:AutoPairs, prev_char) && g:AutoPairs[prev_char] == cur_char if has_key(b:AutoPairs, prev_char) && b:AutoPairs[prev_char] == cur_char
if g:AutoPairsCenterLine && winline() * 3 >= winheight(0) * 2 if g:AutoPairsCenterLine && winline() * 3 >= winheight(0) * 2
" Use \<BS> instead of \<ESC>cl will cause the placeholder deleted " Use \<BS> instead of \<ESC>cl will cause the placeholder deleted
" incorrect. because <C-O>zz won't leave Normal mode. " incorrect. because <C-O>zz won't leave Normal mode.
@ -328,14 +325,19 @@ endfunction
function! AutoPairsInit() function! AutoPairsInit()
let b:autopairs_loaded = 1 let b:autopairs_loaded = 1
let b:autopairs_enabled = 1 let b:autopairs_enabled = 1
let b:AutoPairsClosedPairs = {}
if !exists('b:AutoPairs')
let b:AutoPairs = g:AutoPairs
end
" buffer level map pairs keys " buffer level map pairs keys
for [open, close] in items(g:AutoPairs) for [open, close] in items(b:AutoPairs)
call AutoPairsMap(open) call AutoPairsMap(open)
if open != close if open != close
call AutoPairsMap(close) call AutoPairsMap(close)
end end
let g:AutoPairsClosedPairs[close] = open let b:AutoPairsClosedPairs[close] = open
endfor endfor
" Still use <buffer> level mapping for <BS> <SPACE> " Still use <buffer> level mapping for <BS> <SPACE>
@ -377,10 +379,11 @@ function! s:ExpandMap(map)
return map return map
endfunction endfunction
function! AutoPairsForceInit() function! AutoPairsTryInit()
if exists('b:autopairs_loaded') if exists('b:autopairs_loaded')
return return
end end
" for auto-pairs starts with 'a', so the priority is higher than supertab and vim-endwise " for auto-pairs starts with 'a', so the priority is higher than supertab and vim-endwise
" "
" vim-endwise doesn't support <Plug>AutoPairsReturn " vim-endwise doesn't support <Plug>AutoPairsReturn
@ -426,4 +429,4 @@ inoremap <silent> <SID>AutoPairsReturn <C-R>=AutoPairsReturn()<CR>
imap <script> <Plug>AutoPairsReturn <SID>AutoPairsReturn imap <script> <Plug>AutoPairsReturn <SID>AutoPairsReturn
au BufEnter * :call AutoPairsForceInit() au BufEnter * :call AutoPairsTryInit()