" Language: JavaScript " Maintainer: JiangMiao " Last Change: 2011-05-22 " Version: 1.0.1 " Repository: https://github.com/jiangmiao/auto-pairs " " Insert or delete brackets, parens, quotes in 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:AutoPairsClosedPairs = {} function! AutoPairsInsert(key) let line = getline('.') let prev_char = line[col('.')-2] let current_char = line[col('.')-1] " 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 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] " Auto return only if open and close is same if prev_char == open && open != close return "\\==O" 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 return "\\cf".close end end return "\" endfunction function! AutoPairsJump() call search('[{("\[\]'')}]','W') endfunction function! AutoPairsExtend() let line = getline('.') let current_char = line[col('.')-1] if has_key(g:AutoPairsClosedPairs, current_char) return "\lxh:call AutoPairsJump(line('.'))\pi" end return '' endfunction function! AutoPairsMap(key) execute 'inoremap '.a:key.' =AutoPairsInsert("\'.a:key.'")' endfunction function! AutoPairsInit() for [open, close] in items(g:AutoPairs) call AutoPairsMap(open) if open != close call AutoPairsMap(close) end let g:AutoPairsClosedPairs[close] = 1 endfor execute 'inoremap =AutoPairsDelete()' " 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 ' execute 'inoremap =AutoPairsExtend()' end endfunction call AutoPairsInit()