Fixes #7: compatible with other plugin for key <CR>

This commit is contained in:
jiangfriend@gmail.com 2012-01-24 19:34:25 +08:00
parent 10f56aad0e
commit 207107a684

View File

@ -1,8 +1,8 @@
" Insert or delete brackets, parens, quotes in pairs. " Insert or delete brackets, parens, quotes in pairs.
" Maintainer: JiangMiao <jiangfriend@gmail.com> " Maintainer: JiangMiao <jiangfriend@gmail.com>
" Contributor: camthompson " Contributor: camthompson
" Last Change: 2012-01-17 " Last Change: 2012-01-24
" Version: 1.1.4 " Version: 1.1.5
" Homepage: http://www.vim.org/scripts/script.php?script_id=3599 " Homepage: http://www.vim.org/scripts/script.php?script_id=3599
" Repository: https://github.com/jiangmiao/auto-pairs " Repository: https://github.com/jiangmiao/auto-pairs
@ -206,7 +206,8 @@ endfunction
function! AutoPairsReturn() function! AutoPairsReturn()
let line = getline('.') let line = getline('.')
let prev_char = line[col('.')-2] let pline = getline(line('.')-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(g:AutoPairs, prev_char) && g:AutoPairs[prev_char] == cur_char
@ -217,12 +218,12 @@ function! AutoPairsReturn()
" javascript need indent new line " javascript need indent new line
" coffeescript forbid indent new line " coffeescript forbid indent new line
if &filetype == 'coffeescript' if &filetype == 'coffeescript'
return "\<DEL>\<CR>".cur_char."\<ESC>k==o".cmd return "\<ESC>k==o".cmd
else else
return "\<DEL>\<CR>".cur_char."\<ESC>=ko".cmd return "\<ESC>=ko".cmd
endif endif
end end
return "\<CR>" return ''
endfunction endfunction
function! AutoPairsSpace() function! AutoPairsSpace()
@ -239,6 +240,8 @@ 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
" buffer level map pairs keys
for [open, close] in items(g:AutoPairs) for [open, close] in items(g:AutoPairs)
call AutoPairsMap(open) call AutoPairsMap(open)
if open != close if open != close
@ -247,19 +250,17 @@ function! AutoPairsInit()
let g:AutoPairsClosedPairs[close] = 1 let g:AutoPairsClosedPairs[close] = 1
endfor endfor
" Still use <buffer> level mapping for <BS> <SPACE>
if g:AutoPairsMapBS if g:AutoPairsMapBS
execute 'inoremap <buffer> <silent> <expr> <BS> AutoPairsDelete()' execute 'inoremap <buffer> <silent> <expr> <BS> AutoPairsDelete()'
end end
if g:AutoPairsMapCR
execute 'inoremap <buffer> <silent> <expr> <CR> AutoPairsReturn()'
end
if g:AutoPairsMapSpace if g:AutoPairsMapSpace
execute 'inoremap <buffer> <silent> <expr> <space> AutoPairsSpace()' execute 'inoremap <buffer> <silent> <expr> <SPACE> AutoPairsSpace()'
end end
execute 'inoremap <buffer> <silent> '.g:AutoPairsShortcutFastWrap.' <C-R>=AutoPairsFastWrap()<CR>' execute 'inoremap <buffer> <silent> '.g:AutoPairsShortcutFastWrap.' <C-R>=AutoPairsFastWrap()<CR>'
" use <expr> to ensure showing the status when toggle
execute 'inoremap <buffer> <silent> <expr> '.g:AutoPairsShortcutToggle.' AutoPairsToggle()' execute 'inoremap <buffer> <silent> <expr> '.g:AutoPairsShortcutToggle.' AutoPairsToggle()'
execute 'noremap <buffer> <silent> '.g:AutoPairsShortcutToggle.' :call AutoPairsToggle()<CR>' execute 'noremap <buffer> <silent> '.g:AutoPairsShortcutToggle.' :call AutoPairsToggle()<CR>'
" If the keys map conflict with your own settings, delete or change them " If the keys map conflict with your own settings, delete or change them
@ -278,4 +279,18 @@ function! AutoPairsForceInit()
endif endif
endfunction endfunction
" Global keys mapping
" comptible with other plugin
if g:AutoPairsMapCR
let old_cr = maparg('<CR>', 'i')
if old_cr == ''
let old_cr = '<CR>'
endif
if old_cr !~ 'AutoPairsReturn'
execute 'imap <silent> <CR> '.old_cr.'<C-R>=AutoPairsReturn()<CR>'
end
endif
au BufEnter * :call AutoPairsForceInit() au BufEnter * :call AutoPairsForceInit()