New feature: Move character under the cursor to the pair.

This commit is contained in:
Miao Jiang 2017-06-17 01:23:20 +08:00
parent 20ec5b043f
commit 6afc850e24
2 changed files with 40 additions and 3 deletions

View File

@ -67,6 +67,14 @@ Features
input: |[foo, bar()] (press (<M-e> at |) input: |[foo, bar()] (press (<M-e> at |)
output: ([foo, bar()]) output: ([foo, bar()])
* Quick move char to closed pair
input: (|){["foo"]} (press <M-}> at |)
output: ({["foo"]}|)
input: |[foo, bar()] (press (<M-]> at |)
output: ([foo, bar()]|)
* Quick jump to closed pair. * Quick jump to closed pair.
input: input:
@ -249,6 +257,13 @@ Options
Work with FlyMode, insert the key at the Fly Mode jumped postion Work with FlyMode, insert the key at the Fly Mode jumped postion
* g:AutoPairsMoveCharacter
Default: "()[]{}\"'"
Map <M-(> <M-)> <M-[> <M-]> <M-{> <M-}> <M-"> <M-'> to
move character under the cursor to the pair.
Buffer Level Pairs Setting Buffer Level Pairs Setting
-------------------------- --------------------------

View File

@ -1,8 +1,8 @@
" Insert or delete brackets, parens, quotes in pairs. " Insert or delete brackets, parens, quotes in pairs.
" Maintainer: JiangMiao <jiangfriend@gmail.com> " Maintainer: JiangMiao <jiangfriend@gmail.com>
" Contributor: camthompson " Contributor: camthompson
" Last Change: 2013-07-13 " Last Change: 2017-06-17
" Version: 1.3.2 " Version: 1.3.3
" Homepage: http://www.vim.org/scripts/script.php?script_id=3599 " Homepage: http://www.vim.org/scripts/script.php?script_id=3599
" Repository: https://github.com/jiangmiao/auto-pairs " Repository: https://github.com/jiangmiao/auto-pairs
" License: MIT " License: MIT
@ -49,6 +49,10 @@ if !exists('g:AutoPairsShortcutFastWrap')
let g:AutoPairsShortcutFastWrap = '<M-e>' let g:AutoPairsShortcutFastWrap = '<M-e>'
end end
if !exists('g:AutoPairsMoveCharacter')
let g:AutoPairsMoveCharacter = "()[]{}\"'"
end
if !exists('g:AutoPairsShortcutJump') if !exists('g:AutoPairsShortcutJump')
let g:AutoPairsShortcutJump = '<M-n>' let g:AutoPairsShortcutJump = '<M-n>'
endif endif
@ -356,6 +360,7 @@ function! AutoPairsMap(key)
let escaped_key = substitute(key, "'", "''", 'g') let escaped_key = substitute(key, "'", "''", 'g')
" use expr will cause search() doesn't work " use expr will cause search() doesn't work
execute 'inoremap <buffer> <silent> '.key." <C-R>=AutoPairsInsert('".escaped_key."')<CR>" execute 'inoremap <buffer> <silent> '.key." <C-R>=AutoPairsInsert('".escaped_key."')<CR>"
endfunction endfunction
function! AutoPairsToggle() function! AutoPairsToggle()
@ -369,6 +374,12 @@ function! AutoPairsToggle()
return '' return ''
endfunction endfunction
function! AutoPairsMoveCharacter(key)
let c = getline(".")[col(".")-1]
let escaped_key = substitute(a:key, "'", "''", 'g')
return "\<DEL>\<ESC>:call search("."'".escaped_key."'".")\<CR>a".c."\<LEFT>"
endfunction
function! AutoPairsReturn() function! AutoPairsReturn()
if b:autopairs_enabled == 0 if b:autopairs_enabled == 0
return '' return ''
@ -425,13 +436,19 @@ endfunction
function! AutoPairsInit() function! AutoPairsInit()
let b:autopairs_loaded = 1 let b:autopairs_loaded = 1
let b:autopairs_enabled = 1 if !exists('b:autopairs_enabled')
let b:autopairs_enabled = 1
end
let b:AutoPairsClosedPairs = {} let b:AutoPairsClosedPairs = {}
if !exists('b:AutoPairs') if !exists('b:AutoPairs')
let b:AutoPairs = g:AutoPairs let b:AutoPairs = g:AutoPairs
end end
if !exists('b:AutoPairsMoveCharacter')
let b:AutoPairsMoveCharacter = g:AutoPairsMoveCharacter
end
" buffer level map pairs keys " buffer level map pairs keys
for [open, close] in items(b:AutoPairs) for [open, close] in items(b:AutoPairs)
call AutoPairsMap(open) call AutoPairsMap(open)
@ -441,6 +458,11 @@ function! AutoPairsInit()
let b:AutoPairsClosedPairs[close] = open let b:AutoPairsClosedPairs[close] = open
endfor endfor
for key in split(b:AutoPairsMoveCharacter, '\s*')
let escaped_key = substitute(key, "'", "''", 'g')
execute 'inoremap <silent> <buffer> <M-'.key."> <C-R>=AutoPairsMoveCharacter('".escaped_key."')<CR>"
endfor
" Still use <buffer> level mapping for <BS> <SPACE> " Still use <buffer> level mapping for <BS> <SPACE>
if g:AutoPairsMapBS if g:AutoPairsMapBS
" Use <C-R> instead of <expr> for issue #14 sometimes press BS output strange words " Use <C-R> instead of <expr> for issue #14 sometimes press BS output strange words