" Insert or delete brackets, parens, quotes in pairs. " Maintainer: JiangMiao " Last Change: 2011-12-07 " Version: 1.1.2 " Homepage: http://www.vim.org/scripts/script.php?script_id=3599 " Repository: https://github.com/jiangmiao/auto-pairs if exists('g:AutoPairsLoaded') || &cp finish end let g:AutoPairsLoaded = 1 " Shortcurs for " newline with indetation " jump to of line " jmup to next pairs if !exists('g:AutoPairsShortcuts') let g:AutoPairsShortcuts = 1 end if !exists('g:AutoPairs') let g:AutoPairs = {'(':')', '[':']', '{':'}',"'":"'",'"':'"'} end let g:AutoExtraPairs = copy(g:AutoPairs) let g:AutoExtraPairs['<'] = '>' if !exists('g:AutoPairsMapBS') let g:AutoPairsMapBS = 1 end if !exists('g:AutoPairsMapCR') let g:AutoPairsMapCR = 1 end if !exists('g:AutoPairsMapSpace') let g:AutoPairsMapSpace = 1 end if !exists('g:AutoPairsCenterLine') let g:AutoPairsCenterLine = 1 end if !exists('g:AutoPairsShortcutToggle') let g:AutoPairsShortcutToggle = '' end if !exists('g:AutoPairsShortcutFastWrap') let g:AutoPairsShortcutFastWrap = '' 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 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) return a:key end let open = a:key let close = g:AutoPairs[open] if current_char == close && open == close return "\" end return open.close."\" endfunction function! AutoPairsDelete() let line = getline('.') let prev_char = line[col('.')-2] let pprev_char = line[col('.')-3] if pprev_char == '\' return "\" end 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) return "\". repeat("\", len(space)+1) end end return "\" endfunction function! AutoPairsJump() call search('[{("\[\]'')}]','W') endfunction " Fast wrap the word in brackets function! AutoPairsFastWrap() let line = getline('.') let current_char = line[col('.')-1] let next_char = line[col('.')] " Ignore EOL if col('.') == col('$') return '' end normal! x if match(next_char, '\s') != -1 call search('\S', 'W') let next_char = getline('.')[col('.')-1] end if has_key(g:AutoExtraPairs, next_char) let close = g:AutoExtraPairs[next_char] call search(close, 'W') return "\".current_char."\" else if match(next_char, '\w') != -1 execute "normal! he" end execute "normal! a".current_char return "" end endfunction 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\cl" end " conflict from javascript and coffee " javascript need indent new line " coffeescript forbid indent new line if &filetype == 'coffeescript' return "\\".cur_char."\k==o".cmd else return "\\".cur_char."\=ko".cmd endif end return "\" endfunction function! AutoPairsSpace() 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 let cmd = "\\i" endif return "\".cmd endfunction function! AutoPairsInit() let b:autopairs_loaded = 1 let b:autopairs_enabled = 1 for [open, close] in items(g:AutoPairs) call AutoPairsMap(open) if open != close call AutoPairsMap(close) end let g:AutoPairsClosedPairs[close] = 1 endfor if g:AutoPairsMapBS execute 'inoremap AutoPairsDelete()' end if g:AutoPairsMapCR execute 'inoremap AutoPairsReturn()' end if g:AutoPairsMapSpace execute 'inoremap AutoPairsSpace()' end execute 'inoremap '.g:AutoPairsShortcutFastWrap.' =AutoPairsFastWrap()' 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' execute 'inoremap ' execute 'inoremap ' end endfunction function! AutoPairsForceInit() if exists('b:autopairs_loaded') return else call AutoPairsInit() endif endfunction au BufEnter * :call AutoPairsForceInit()