Improve AutoPairsMapSpace Feature

MapSpace now only works with [], {}, ()

Delete last two space with one backspace,

    [ | ], press <BS> at |, output: [|]

Smarter paren insertion

    [ foo| ], press ] at |, ouput: [ foo ]|
This commit is contained in:
jiangfriend@gmail.com 2011-12-14 17:00:45 +08:00
parent 75b6bfb804
commit 6a4b2ca820
2 changed files with 45 additions and 13 deletions

View File

@ -25,11 +25,17 @@ Features
|
}
* Insert spaces before closing characters
* Insert spaces before closing characters, only for [], (), {}
input: {|} (press <SPACE> at |)
output: { | }
input: {|} (press <SPACE>foo} at |)
output: { foo }|
input: '|' (press <SPACE> at |)
output: ' |'
* Skip closed bracket.
input: []
@ -115,7 +121,7 @@ Options
Default : 1
Map <space> to insert a space after the opening character and before the closing one.
execute 'inoremap <buffer> <silent> <CR> <C-R>=AutoPairsReturn()<CR>'
execute 'inoremap <buffer> <silent> <CR> <C-R>=AutoPairsSpace()<CR>'
TroubleShooting
---------------
@ -125,3 +131,11 @@ TroubleShooting
Or the plugin conflict with some other plugins.
use command :call AutoPairsInit() to remap the keys.
* How to insert parens purely
There are 3 ways
1 use Ctrl-V ) to insert paren without trigger the plugin.
2 use Alt-P to turn off the plugin.
3 use DEL or <C-O>x to delete the character insert by plugin.

View File

@ -1,7 +1,7 @@
" Insert or delete brackets, parens, quotes in pairs.
" Maintainer: JiangMiao <jiangfriend@gmail.com>
" Last Change: 2011-12-07
" Version: 1.1.2
" Last Change: 2011-12-13
" Version: 1.1.3
" Homepage: http://www.vim.org/scripts/script.php?script_id=3599
" Repository: https://github.com/jiangmiao/auto-pairs
@ -21,6 +21,11 @@ end
if !exists('g:AutoPairs')
let g:AutoPairs = {'(':')', '[':']', '{':'}',"'":"'",'"':'"'}
end
if !exists('g:AutoPairsParens')
let g:AutoPairsParens = {'(':')', '[':']', '{':'}'}
end
let g:AutoExtraPairs = copy(g:AutoPairs)
let g:AutoExtraPairs['<'] = '>'
@ -60,6 +65,7 @@ function! AutoPairsInsert(key)
let line = getline('.')
let prev_char = line[col('.')-2]
let current_char = line[col('.')-1]
let next_char = line[col('.')]
let eol = 0
if col('$') - col('.') <= 1
@ -71,13 +77,19 @@ function! AutoPairsInsert(key)
return a:key
end
" Skip the character if current character is the same as input
if current_char == a:key && !has_key(g:AutoPairs, a:key)
return "\<Right>"
end
" Input directly if the key is not an open key
if !has_key(g:AutoPairs, a:key)
" Skip the character if next character is space
if current_char == ' ' && next_char == a:key
return "\<Right>\<Right>"
end
" Skip the character if current character is the same as input
if current_char == a:key
return "\<Right>"
end
" Input directly if the key is not an open key
return a:key
end
@ -93,6 +105,7 @@ endfunction
function! AutoPairsDelete()
let line = getline('.')
let current_char = line[col('.')-1]
let prev_char = line[col('.')-2]
let pprev_char = line[col('.')-3]
@ -100,7 +113,12 @@ function! AutoPairsDelete()
return "\<BS>"
end
if has_key(g:AutoPairs, prev_char)
" Delete last two spaces in parens, work with MapSpace
if has_key(g:AutoPairs, pprev_char) && prev_char == ' ' && current_char == ' '
return "\<BS>\<DEL>"
endif
if has_key(g:AutoPairs, prev_char)
let close = g:AutoPairs[prev_char]
if match(line,'^\s*'.close, col('.')-1) != -1
let space = matchstr(line, '^\s*', col('.')-1)
@ -169,7 +187,7 @@ function! AutoPairsReturn()
if g:AutoPairsCenterLine && winline() * 1.5 >= winheight(0)
let cmd = " \<C-O>zz\<ESC>cl"
end
" conflict from javascript and coffee
" conflict with javascript and coffee
" javascript need indent new line
" coffeescript forbid indent new line
if &filetype == 'coffeescript'
@ -186,8 +204,8 @@ function! AutoPairsSpace()
let prev_char = line[col('.')-2]
let cmd = ''
let cur_char =line[col('.')-1]
if has_key(g:AutoPairs, prev_char) && g:AutoPairs[prev_char] == cur_char
let cmd = "\<SPACE>\<ESC>i"
if has_key(g:AutoPairsParens, prev_char) && g:AutoPairsParens[prev_char] == cur_char
let cmd = "\<SPACE>\<LEFT>"
endif
return "\<SPACE>".cmd
endfunction