Skip to content

Commit

Permalink
New feature: Move character under the cursor to the pair.
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangmiao committed Jun 16, 2017
1 parent 20ec5b0 commit 6afc850
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ Features
input: |[foo, bar()] (press (<M-e> at |)
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.

input:
Expand Down Expand Up @@ -249,6 +257,13 @@ Options

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
--------------------------

Expand Down
28 changes: 25 additions & 3 deletions plugin/auto-pairs.vim
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
" Insert or delete brackets, parens, quotes in pairs.
" Maintainer: JiangMiao <jiangfriend@gmail.com>
" Contributor: camthompson
" Last Change: 2013-07-13
" Version: 1.3.2
" Last Change: 2017-06-17
" Version: 1.3.3
" Homepage: http://www.vim.org/scripts/script.php?script_id=3599
" Repository: https://github.com/jiangmiao/auto-pairs
" License: MIT
Expand Down Expand Up @@ -49,6 +49,10 @@ if !exists('g:AutoPairsShortcutFastWrap')
let g:AutoPairsShortcutFastWrap = '<M-e>'
end

if !exists('g:AutoPairsMoveCharacter')
let g:AutoPairsMoveCharacter = "()[]{}\"'"
end

if !exists('g:AutoPairsShortcutJump')
let g:AutoPairsShortcutJump = '<M-n>'
endif
Expand Down Expand Up @@ -356,6 +360,7 @@ function! AutoPairsMap(key)
let escaped_key = substitute(key, "'", "''", 'g')
" use expr will cause search() doesn't work
execute 'inoremap <buffer> <silent> '.key." <C-R>=AutoPairsInsert('".escaped_key."')<CR>"

endfunction

function! AutoPairsToggle()
Expand All @@ -369,6 +374,12 @@ function! AutoPairsToggle()
return ''
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()
if b:autopairs_enabled == 0
return ''
Expand Down Expand Up @@ -425,13 +436,19 @@ endfunction

function! AutoPairsInit()
let b:autopairs_loaded = 1
let b:autopairs_enabled = 1
if !exists('b:autopairs_enabled')
let b:autopairs_enabled = 1
end
let b:AutoPairsClosedPairs = {}

if !exists('b:AutoPairs')
let b:AutoPairs = g:AutoPairs
end

if !exists('b:AutoPairsMoveCharacter')
let b:AutoPairsMoveCharacter = g:AutoPairsMoveCharacter
end

" buffer level map pairs keys
for [open, close] in items(b:AutoPairs)
call AutoPairsMap(open)
Expand All @@ -441,6 +458,11 @@ function! AutoPairsInit()
let b:AutoPairsClosedPairs[close] = open
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>
if g:AutoPairsMapBS
" Use <C-R> instead of <expr> for issue #14 sometimes press BS output strange words
Expand Down

0 comments on commit 6afc850

Please sign in to comment.