2011-05-24 07:31:55 -04:00
|
|
|
" Insert or delete brackets, parens, quotes in pairs.
|
2011-05-22 13:11:23 -04:00
|
|
|
" Maintainer: JiangMiao <jiangfriend@gmail.com>
|
2011-06-09 14:23:47 -04:00
|
|
|
" Last Change: 2011-06-10
|
|
|
|
" Version: 1.0.3
|
2011-06-07 00:40:51 -04:00
|
|
|
" Homepage: http://www.vim.org/scripts/script.php?script_id=3599
|
2011-05-22 13:11:23 -04:00
|
|
|
" Repository: https://github.com/jiangmiao/auto-pairs
|
|
|
|
|
|
|
|
if exists('g:AutoPairsLoaded') || &cp
|
|
|
|
finish
|
|
|
|
end
|
|
|
|
let g:AutoPairsLoaded = 1
|
|
|
|
|
|
|
|
" Shortcurs for
|
|
|
|
" <M-o> newline with indetation
|
|
|
|
" <M-a> jump to of line
|
|
|
|
" <M-n> jmup to next pairs
|
|
|
|
if !exists('g:AutoPairsShortcuts')
|
|
|
|
let g:AutoPairsShortcuts = 1
|
|
|
|
end
|
|
|
|
|
|
|
|
if !exists('g:AutoPairs')
|
|
|
|
let g:AutoPairs = {'(':')', '[':']', '{':'}',"'":"'",'"':'"'}
|
|
|
|
end
|
2011-06-09 14:23:47 -04:00
|
|
|
let g:AutoExtraPairs = copy(g:AutoPairs)
|
|
|
|
let g:AutoExtraPairs['<'] = '>'
|
2011-05-22 13:11:23 -04:00
|
|
|
|
2011-06-07 00:40:51 -04:00
|
|
|
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 = '<M-p>'
|
|
|
|
end
|
|
|
|
|
2011-05-24 07:07:26 -04:00
|
|
|
let g:AutoPairsClosedPairs = {}
|
|
|
|
|
|
|
|
|
2011-05-22 13:11:23 -04:00
|
|
|
|
|
|
|
function! AutoPairsInsert(key)
|
2011-06-07 00:40:51 -04:00
|
|
|
if !b:autopairs_enabled
|
|
|
|
return a:key
|
|
|
|
end
|
|
|
|
|
2011-05-22 13:11:23 -04:00
|
|
|
let line = getline('.')
|
|
|
|
let prev_char = line[col('.')-2]
|
|
|
|
let current_char = line[col('.')-1]
|
|
|
|
|
2011-06-07 00:40:51 -04:00
|
|
|
let eol = 0
|
|
|
|
if col('$') - col('.') <= 1
|
|
|
|
let eol = 1
|
|
|
|
end
|
|
|
|
|
2011-05-22 13:11:23 -04:00
|
|
|
" 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
|
2011-05-24 14:37:25 -04:00
|
|
|
if current_char == a:key && !has_key(g:AutoPairs, a:key)
|
2011-05-22 13:11:23 -04:00
|
|
|
return "\<Right>"
|
|
|
|
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]
|
|
|
|
|
2011-05-24 14:37:25 -04:00
|
|
|
if current_char == close && open == close
|
|
|
|
return "\<Right>"
|
|
|
|
end
|
|
|
|
|
2011-05-22 13:11:23 -04:00
|
|
|
return open.close."\<Left>"
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function! AutoPairsDelete()
|
|
|
|
let line = getline('.')
|
|
|
|
let prev_char = line[col('.')-2]
|
|
|
|
let pprev_char = line[col('.')-3]
|
|
|
|
|
|
|
|
if pprev_char == '\'
|
|
|
|
return "\<BS>"
|
|
|
|
end
|
|
|
|
|
|
|
|
if has_key(g:AutoPairs, prev_char)
|
|
|
|
let close = g:AutoPairs[prev_char]
|
|
|
|
if match(line,'^\s*'.close, col('.')-1) != -1
|
|
|
|
return "\<Left>\<C-O>cf".close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return "\<BS>"
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! AutoPairsJump()
|
|
|
|
call search('[{("\[\]'')}]','W')
|
|
|
|
endfunction
|
|
|
|
|
2011-06-07 00:40:51 -04:00
|
|
|
" Fast wrap the word in brackets
|
|
|
|
" Haven't finished yet
|
2011-05-24 07:07:26 -04:00
|
|
|
function! AutoPairsExtend()
|
|
|
|
let line = getline('.')
|
|
|
|
let current_char = line[col('.')-1]
|
2011-06-07 00:40:51 -04:00
|
|
|
let next_char = line[col('.')]
|
2011-06-09 14:23:47 -04:00
|
|
|
|
|
|
|
normal! x
|
|
|
|
if match(next_char, '\s') != -1
|
|
|
|
call search('\S', 'W')
|
|
|
|
let next_char = getline('.')[col('.')-1]
|
2011-05-24 07:07:26 -04:00
|
|
|
end
|
|
|
|
|
2011-06-09 14:23:47 -04:00
|
|
|
if has_key(g:AutoExtraPairs, next_char)
|
|
|
|
let close = g:AutoExtraPairs[next_char]
|
|
|
|
call search(close, 'W')
|
|
|
|
return "\<RIGHT>".current_char."\<LEFT>"
|
|
|
|
else
|
|
|
|
execute "normal! ea".current_char
|
|
|
|
return ""
|
|
|
|
end
|
2011-05-24 07:07:26 -04:00
|
|
|
endfunction
|
|
|
|
|
2011-05-22 13:11:23 -04:00
|
|
|
function! AutoPairsMap(key)
|
2011-05-24 07:26:32 -04:00
|
|
|
execute 'inoremap <buffer> <silent> '.a:key.' <C-R>=AutoPairsInsert("\'.a:key.'")<CR>'
|
2011-05-22 13:11:23 -04:00
|
|
|
endfunction
|
|
|
|
|
2011-06-07 00:40:51 -04:00
|
|
|
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 = ";\<C-O>zz\<DEL>"
|
|
|
|
end
|
|
|
|
return "\<CR>\<C-O>O".cmd
|
|
|
|
end
|
|
|
|
return "\<CR>"
|
|
|
|
endfunction
|
|
|
|
|
2011-05-22 13:11:23 -04:00
|
|
|
function! AutoPairsInit()
|
2011-06-07 00:40:51 -04:00
|
|
|
let b:autopairs_enabled = 1
|
2011-05-22 13:11:23 -04:00
|
|
|
for [open, close] in items(g:AutoPairs)
|
|
|
|
call AutoPairsMap(open)
|
|
|
|
if open != close
|
|
|
|
call AutoPairsMap(close)
|
|
|
|
end
|
2011-05-24 07:07:26 -04:00
|
|
|
let g:AutoPairsClosedPairs[close] = 1
|
2011-05-22 13:11:23 -04:00
|
|
|
endfor
|
|
|
|
|
2011-06-07 00:40:51 -04:00
|
|
|
if g:AutoPairsMapBS
|
|
|
|
execute 'inoremap <buffer> <silent> <BS> <C-R>=AutoPairsDelete()<CR>'
|
|
|
|
end
|
|
|
|
|
|
|
|
if g:AutoPairsMapCR
|
|
|
|
execute 'inoremap <buffer> <silent> <CR> <C-R>=AutoPairsReturn()<CR>'
|
|
|
|
end
|
|
|
|
|
|
|
|
execute 'inoremap <buffer> <silent> '.g:AutoPairsShortcutToggle.' <C-R>=AutoPairsToggle()<CR>'
|
|
|
|
execute 'noremap <buffer> <silent> '.g:AutoPairsShortcutToggle.' :call AutoPairsToggle()<CR>'
|
2011-05-22 13:11:23 -04:00
|
|
|
" If the keys map conflict with your own settings, delete or change them
|
|
|
|
if g:AutoPairsShortcuts
|
2011-05-24 07:26:32 -04:00
|
|
|
execute 'inoremap <buffer> <silent> <M-n> <ESC>:call AutoPairsJump()<CR>a'
|
|
|
|
execute 'inoremap <buffer> <silent> <M-a> <END>'
|
|
|
|
execute 'inoremap <buffer> <silent> <M-o> <END><CR>'
|
|
|
|
execute 'inoremap <buffer> <silent> <M-e> <C-R>=AutoPairsExtend()<CR>'
|
2011-05-22 13:11:23 -04:00
|
|
|
end
|
|
|
|
endfunction
|
|
|
|
|
2011-06-07 00:40:51 -04:00
|
|
|
au BufRead,BufNewFile,BufCreate * :call AutoPairsInit()
|