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-12-29 08:42:43 -05:00
|
|
|
" Contributor: camthompson
|
2012-01-24 06:34:25 -05:00
|
|
|
" Last Change: 2012-01-24
|
|
|
|
" Version: 1.1.5
|
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')
|
2012-01-17 00:14:56 -05:00
|
|
|
let g:AutoPairs = {'(':')', '[':']', '{':'}',"'":"'",'"':'"', '`':'`'}
|
2011-05-22 13:11:23 -04:00
|
|
|
end
|
2011-12-14 04:00:45 -05:00
|
|
|
|
|
|
|
if !exists('g:AutoPairsParens')
|
|
|
|
let g:AutoPairsParens = {'(':')', '[':']', '{':'}'}
|
|
|
|
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
|
|
|
|
|
2011-12-13 16:56:46 -05:00
|
|
|
if !exists('g:AutoPairsMapSpace')
|
|
|
|
let g:AutoPairsMapSpace = 1
|
|
|
|
end
|
|
|
|
|
2011-06-07 00:40:51 -04:00
|
|
|
if !exists('g:AutoPairsCenterLine')
|
|
|
|
let g:AutoPairsCenterLine = 1
|
|
|
|
end
|
|
|
|
|
|
|
|
if !exists('g:AutoPairsShortcutToggle')
|
|
|
|
let g:AutoPairsShortcutToggle = '<M-p>'
|
|
|
|
end
|
|
|
|
|
2011-06-09 14:36:51 -04:00
|
|
|
if !exists('g:AutoPairsShortcutFastWrap')
|
|
|
|
let g:AutoPairsShortcutFastWrap = '<M-e>'
|
|
|
|
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-12-14 04:00:45 -05:00
|
|
|
let next_char = line[col('.')]
|
2011-05-22 13:11:23 -04:00
|
|
|
|
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
|
|
|
|
|
|
|
|
if !has_key(g:AutoPairs, a:key)
|
2011-12-14 04:00:45 -05:00
|
|
|
" Skip the character if next character is space
|
|
|
|
if current_char == ' ' && next_char == a:key
|
|
|
|
return "\<Right>\<Right>"
|
|
|
|
end
|
|
|
|
|
2012-01-17 00:14:56 -05:00
|
|
|
" Skip the character if closed pair is next character
|
2011-12-21 23:30:04 -05:00
|
|
|
if current_char == ''
|
2011-12-21 05:26:26 -05:00
|
|
|
let next_lineno = line('.')+1
|
|
|
|
let next_line = getline(nextnonblank(next_lineno))
|
|
|
|
let next_char = matchstr(next_line, '\s*\zs.')
|
|
|
|
if next_char == a:key
|
|
|
|
return "\<ESC>e^a"
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
2011-12-14 04:00:45 -05:00
|
|
|
" Skip the character if current character is the same as input
|
|
|
|
if current_char == a:key
|
|
|
|
return "\<Right>"
|
|
|
|
end
|
|
|
|
|
|
|
|
" Input directly if the key is not an open key
|
2011-05-22 13:11:23 -04:00
|
|
|
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-12-30 03:05:37 -05:00
|
|
|
" Ignore auto close ' if follows a word
|
|
|
|
" MUST after closed check. 'hello|'
|
|
|
|
if a:key == "'" && prev_char =~ '\v\w'
|
|
|
|
return a:key
|
|
|
|
end
|
|
|
|
|
2012-01-17 00:17:21 -05:00
|
|
|
" support for ''' ``` and """
|
|
|
|
if open == close
|
|
|
|
" The key must be ' " `
|
|
|
|
let pprev_char = line[col('.')-3]
|
|
|
|
if pprev_char == open && prev_char == open
|
|
|
|
" Double pair found
|
|
|
|
return a:key
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-29 08:42:43 -05:00
|
|
|
return open.close."\<Left>"
|
2011-05-22 13:11:23 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! AutoPairsDelete()
|
|
|
|
let line = getline('.')
|
2011-12-14 04:00:45 -05:00
|
|
|
let current_char = line[col('.')-1]
|
2011-05-22 13:11:23 -04:00
|
|
|
let prev_char = line[col('.')-2]
|
|
|
|
let pprev_char = line[col('.')-3]
|
|
|
|
|
|
|
|
if pprev_char == '\'
|
|
|
|
return "\<BS>"
|
|
|
|
end
|
|
|
|
|
2011-12-14 04:00:45 -05:00
|
|
|
" Delete last two spaces in parens, work with MapSpace
|
|
|
|
if has_key(g:AutoPairs, pprev_char) && prev_char == ' ' && current_char == ' '
|
|
|
|
return "\<BS>\<DEL>"
|
|
|
|
endif
|
|
|
|
|
|
|
|
if has_key(g:AutoPairs, prev_char)
|
2011-05-22 13:11:23 -04:00
|
|
|
let close = g:AutoPairs[prev_char]
|
|
|
|
if match(line,'^\s*'.close, col('.')-1) != -1
|
2011-11-13 03:47:49 -05:00
|
|
|
let space = matchstr(line, '^\s*', col('.')-1)
|
|
|
|
return "\<BS>". repeat("\<DEL>", len(space)+1)
|
2012-03-03 11:59:38 -05:00
|
|
|
else
|
|
|
|
let nline = getline(line('.')+1)
|
|
|
|
if nline =~ '^\s*'.close
|
|
|
|
let space = matchstr(nline, '^\s*')
|
|
|
|
return "\<BS>\<DEL>". repeat("\<DEL>", len(space)+1)
|
|
|
|
end
|
2011-05-22 13:11:23 -04:00
|
|
|
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
|
2011-06-09 14:26:22 -04:00
|
|
|
function! AutoPairsFastWrap()
|
2011-05-24 07:07:26 -04:00
|
|
|
let line = getline('.')
|
|
|
|
let current_char = line[col('.')-1]
|
2011-06-07 00:40:51 -04:00
|
|
|
let next_char = line[col('.')]
|
2011-06-10 05:48:35 -04:00
|
|
|
|
|
|
|
" Ignore EOL
|
|
|
|
if col('.') == col('$')
|
|
|
|
return ''
|
|
|
|
end
|
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
|
2011-06-10 05:48:35 -04:00
|
|
|
if match(next_char, '\w') != -1
|
|
|
|
execute "normal! he"
|
|
|
|
end
|
|
|
|
execute "normal! a".current_char
|
2011-06-09 14:23:47 -04:00
|
|
|
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('.')
|
2012-01-24 06:34:25 -05:00
|
|
|
let pline = getline(line('.')-1)
|
|
|
|
let prev_char = pline[strlen(pline)-1]
|
2011-06-07 00:40:51 -04:00
|
|
|
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)
|
2011-06-13 06:13:01 -04:00
|
|
|
let cmd = " \<C-O>zz\<ESC>cl"
|
2011-06-07 00:40:51 -04:00
|
|
|
end
|
2011-12-14 04:00:45 -05:00
|
|
|
" conflict with javascript and coffee
|
2011-12-14 00:17:27 -05:00
|
|
|
" javascript need indent new line
|
|
|
|
" coffeescript forbid indent new line
|
|
|
|
if &filetype == 'coffeescript'
|
2012-01-24 06:34:25 -05:00
|
|
|
return "\<ESC>k==o".cmd
|
2011-12-14 00:17:27 -05:00
|
|
|
else
|
2012-01-24 06:34:25 -05:00
|
|
|
return "\<ESC>=ko".cmd
|
2011-12-14 00:17:27 -05:00
|
|
|
endif
|
2011-06-07 00:40:51 -04:00
|
|
|
end
|
2012-01-24 06:34:25 -05:00
|
|
|
return ''
|
2011-06-07 00:40:51 -04:00
|
|
|
endfunction
|
|
|
|
|
2011-12-13 16:56:46 -05:00
|
|
|
function! AutoPairsSpace()
|
|
|
|
let line = getline('.')
|
|
|
|
let prev_char = line[col('.')-2]
|
|
|
|
let cmd = ''
|
|
|
|
let cur_char =line[col('.')-1]
|
2011-12-14 04:00:45 -05:00
|
|
|
if has_key(g:AutoPairsParens, prev_char) && g:AutoPairsParens[prev_char] == cur_char
|
|
|
|
let cmd = "\<SPACE>\<LEFT>"
|
2011-12-13 16:56:46 -05:00
|
|
|
endif
|
|
|
|
return "\<SPACE>".cmd
|
|
|
|
endfunction
|
|
|
|
|
2011-05-22 13:11:23 -04:00
|
|
|
function! AutoPairsInit()
|
2011-11-13 03:39:45 -05:00
|
|
|
let b:autopairs_loaded = 1
|
2011-06-07 00:40:51 -04:00
|
|
|
let b:autopairs_enabled = 1
|
2012-01-24 06:34:25 -05:00
|
|
|
|
|
|
|
" buffer level map pairs keys
|
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
|
|
|
|
|
2012-01-24 06:34:25 -05:00
|
|
|
" Still use <buffer> level mapping for <BS> <SPACE>
|
2011-06-07 00:40:51 -04:00
|
|
|
if g:AutoPairsMapBS
|
2011-06-10 23:12:20 -04:00
|
|
|
execute 'inoremap <buffer> <silent> <expr> <BS> AutoPairsDelete()'
|
2011-06-07 00:40:51 -04:00
|
|
|
end
|
|
|
|
|
2011-12-13 16:56:46 -05:00
|
|
|
if g:AutoPairsMapSpace
|
2012-01-24 06:34:25 -05:00
|
|
|
execute 'inoremap <buffer> <silent> <expr> <SPACE> AutoPairsSpace()'
|
2011-12-13 16:56:46 -05:00
|
|
|
end
|
|
|
|
|
2011-06-09 14:36:51 -04:00
|
|
|
execute 'inoremap <buffer> <silent> '.g:AutoPairsShortcutFastWrap.' <C-R>=AutoPairsFastWrap()<CR>'
|
2012-01-24 06:34:25 -05:00
|
|
|
" use <expr> to ensure showing the status when toggle
|
2011-06-10 23:12:20 -04:00
|
|
|
execute 'inoremap <buffer> <silent> <expr> '.g:AutoPairsShortcutToggle.' AutoPairsToggle()'
|
2011-06-07 00:40:51 -04:00
|
|
|
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>'
|
2011-05-22 13:11:23 -04:00
|
|
|
end
|
|
|
|
endfunction
|
|
|
|
|
2011-11-13 03:39:45 -05:00
|
|
|
function! AutoPairsForceInit()
|
|
|
|
if exists('b:autopairs_loaded')
|
|
|
|
return
|
|
|
|
else
|
|
|
|
call AutoPairsInit()
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2012-03-06 10:12:55 -05:00
|
|
|
" Always silent the command
|
|
|
|
inoremap <silent> <SID>AutoPairsReturn <C-R>=AutoPairsReturn()<CR>
|
2012-01-24 06:34:25 -05:00
|
|
|
|
|
|
|
" Global keys mapping
|
|
|
|
" comptible with other plugin
|
|
|
|
if g:AutoPairsMapCR
|
|
|
|
let old_cr = maparg('<CR>', 'i')
|
|
|
|
if old_cr == ''
|
|
|
|
let old_cr = '<CR>'
|
|
|
|
endif
|
|
|
|
|
|
|
|
if old_cr !~ 'AutoPairsReturn'
|
2012-03-06 10:12:55 -05:00
|
|
|
execute 'imap <silent> <CR> '.old_cr.'<SID>AutoPairsReturn'
|
2012-01-24 06:34:25 -05:00
|
|
|
end
|
|
|
|
endif
|
|
|
|
|
2011-11-13 03:39:45 -05:00
|
|
|
au BufEnter * :call AutoPairsForceInit()
|