From e893ae3d48579b12eeb68ae3a4d57d648eb5644d Mon Sep 17 00:00:00 2001 From: "jiangfriend@gmail.com" Date: Tue, 7 Jun 2011 12:40:51 +0800 Subject: [PATCH] Use { instead of {{ (Thanks to linopolus) https://github.com/jiangmiao/auto-pairs/issues/1 Add Toggle key Add Options g:AutoPairsShortcutToggle g:AutoPairsMapBS g:AutoPairsMapCR g:AutoPairsCenterLine --- README.md | 51 ++++++++++++++++++++++--- plugin/auto-pairs.vim | 89 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 124 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 751eaf3..04943f2 100644 --- a/README.md +++ b/README.md @@ -18,9 +18,9 @@ Features input: foo[ output: foo -### Double input open brackets will insert new indented line. +### Insert new indented line after Return - input: {{ + input: {|} (press at |) output: { | } @@ -35,21 +35,62 @@ Features input: "\' output: "\'" + +Shortcuts +--------- + + System Shortcuts: + : Insert new indented line after return if cursor in blank brackets or quotes. + : Delete brackets in pair + : Toggle Autopairs + + Optional Shortcuts: + could be turn off by let g:AutoPairsShortcuts = 0 + jump to next closed bracket. + jump to end of line. + jump to newline with indented. + Options ------- * g:AutoPairs - Default: {'(':')', '[':']', '{':'}',"'":"'",'"':'"'} + Default: {'(':')', '[':']', '{':'}',"'":"'",'"':'"'} + +* g:AutoPairsShortcutToggle + + Default: '' + + The shortcut to toggle autopairs. * g:AutoPairsShortcuts - - Default: 1 + + Default: 1 imap 3 shortcuts jump to next closed bracket. jump to end of line. jump to newline with indented. +* g:AutoPairsMapBS + + Default : 1 + + Map to delete brackets, quotes in pair + execute 'inoremap =AutoPairsDelete()' + +* g:AutoPairsMapCR + + Default : 1 + + Map to insert a new indented line if cursor in (|), {|} [|], '|', "|" + execute 'inoremap =AutoPairsReturn()' + +* g:AutoPairsCenterLine + + Default : 1 + + When g:AutoPairsMapCR is on, center current line after return if the line is at the bottom 1/3 of the window. + TroubleShooting --------------- The script will remap keys ([{'"}]) , diff --git a/plugin/auto-pairs.vim b/plugin/auto-pairs.vim index 8710334..e51f27a 100644 --- a/plugin/auto-pairs.vim +++ b/plugin/auto-pairs.vim @@ -1,7 +1,8 @@ " Insert or delete brackets, parens, quotes in pairs. " Maintainer: JiangMiao -" Last Change: 2011-05-22 -" Version: 1.0.1 +" Last Change: 2011-06-07 +" Version: 1.0.2 +" Homepage: http://www.vim.org/scripts/script.php?script_id=3599 " Repository: https://github.com/jiangmiao/auto-pairs if exists('g:AutoPairsLoaded') || &cp @@ -21,15 +22,40 @@ if !exists('g:AutoPairs') let g:AutoPairs = {'(':')', '[':']', '{':'}',"'":"'",'"':'"'} end +if !exists('g:AutoPairsMapBS') + let g:AutoPairsMapBS = 1 +end + +if !exists('g:AutoPairsMapCR') + let g:AutoPairsMapCR = 1 +end + +if !exists('g:AutoPairsCenterLine') + let g:AutoPairsCenterLine = 1 +end + +if !exists('g:AutoPairsShortcutToggle') + let g:AutoPairsShortcutToggle = '' +end + let g:AutoPairsClosedPairs = {} function! AutoPairsInsert(key) + if !b:autopairs_enabled + return a:key + end + let line = getline('.') let prev_char = line[col('.')-2] let current_char = line[col('.')-1] + let eol = 0 + if col('$') - col('.') <= 1 + let eol = 1 + end + " Ignore auto close if prev character is \ if prev_char == '\' return a:key @@ -52,12 +78,6 @@ function! AutoPairsInsert(key) return "\" end - - " Auto return only if open and close is same - if prev_char == open && open != close - return "\\==O" - end - return open.close."\" endfunction @@ -86,12 +106,24 @@ function! AutoPairsJump() call search('[{("\[\]'')}]','W') endfunction +" Fast wrap the word in brackets +" Haven't finished yet function! AutoPairsExtend() let line = getline('.') let current_char = line[col('.')-1] + let next_char = line[col('.')] + if has_key(g:AutoPairsClosedPairs, current_char) - return "\lxh:call AutoPairsJump(line('.'))\pi" + if has_key(g:AutoPairs, next_char) + let open = next_char + let close = g:AutoPairs[next_char] + let quote_pattern = '(?:\\\|\"\|[^"])*' + echoe 'search pair '.open.' '.close + call searchpair(open, '', close, 'W') + end + execute "normal! a".current_char."\" + return '' end return '' @@ -101,7 +133,33 @@ function! AutoPairsMap(key) execute 'inoremap '.a:key.' =AutoPairsInsert("\'.a:key.'")' endfunction +function! AutoPairsToggle() + if b:autopairs_enabled + let b:autopairs_enabled = 0 + echo 'AutoPairs Disabled.' + else + let b:autopairs_enabled = 1 + echo 'AutoPairs Enabled.' + end + return '' +endfunction + +function! AutoPairsReturn() + let line = getline('.') + 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 + if g:AutoPairsCenterLine && winline() * 1.5 >= winheight(0) + let cmd = ";\zz\" + end + return "\\O".cmd + end + return "\" +endfunction + function! AutoPairsInit() + let b:autopairs_enabled = 1 for [open, close] in items(g:AutoPairs) call AutoPairsMap(open) if open != close @@ -109,8 +167,17 @@ function! AutoPairsInit() end let g:AutoPairsClosedPairs[close] = 1 endfor - execute 'inoremap =AutoPairsDelete()' + if g:AutoPairsMapBS + execute 'inoremap =AutoPairsDelete()' + end + + if g:AutoPairsMapCR + execute 'inoremap =AutoPairsReturn()' + end + + execute 'inoremap '.g:AutoPairsShortcutToggle.' =AutoPairsToggle()' + execute 'noremap '.g:AutoPairsShortcutToggle.' :call AutoPairsToggle()' " If the keys map conflict with your own settings, delete or change them if g:AutoPairsShortcuts execute 'inoremap :call AutoPairsJump()a' @@ -120,4 +187,4 @@ function! AutoPairsInit() end endfunction -au BufRead,BufNewFile * :call AutoPairsInit() +au BufRead,BufNewFile,BufCreate * :call AutoPairsInit()