From 6a4b2ca82002a9ea104f1ca376e0c3b77e121e1f Mon Sep 17 00:00:00 2001 From: "jiangfriend@gmail.com" Date: Wed, 14 Dec 2011 17:00:45 +0800 Subject: [PATCH] Improve AutoPairsMapSpace Feature MapSpace now only works with [], {}, () Delete last two space with one backspace, [ | ], press at |, output: [|] Smarter paren insertion [ foo| ], press ] at |, ouput: [ foo ]| --- README.md | 18 ++++++++++++++++-- plugin/auto-pairs.vim | 40 +++++++++++++++++++++++++++++----------- 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 6fe619a..1aed80e 100644 --- a/README.md +++ b/README.md @@ -25,11 +25,17 @@ Features | } -* Insert spaces before closing characters +* Insert spaces before closing characters, only for [], (), {} input: {|} (press at |) output: { | } + input: {|} (press foo} at |) + output: { foo }| + + input: '|' (press at |) + output: ' |' + * Skip closed bracket. input: [] @@ -115,7 +121,7 @@ Options Default : 1 Map to insert a space after the opening character and before the closing one. - execute 'inoremap =AutoPairsReturn()' + execute 'inoremap =AutoPairsSpace()' 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 x to delete the character insert by plugin. + diff --git a/plugin/auto-pairs.vim b/plugin/auto-pairs.vim index 76b86c3..c17ec78 100644 --- a/plugin/auto-pairs.vim +++ b/plugin/auto-pairs.vim @@ -1,7 +1,7 @@ " Insert or delete brackets, parens, quotes in pairs. " Maintainer: JiangMiao -" 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 "\" - 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 "\\" + end + + " Skip the character if current character is the same as input + if current_char == a:key + return "\" + 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 "\" 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 "\\" + 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 = " \zz\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 = "\\i" + if has_key(g:AutoPairsParens, prev_char) && g:AutoPairsParens[prev_char] == cur_char + let cmd = "\\" endif return "\".cmd endfunction