New Feature: Fly Mode

This commit is contained in:
jiangfriend@gmail.com 2012-05-14 09:54:25 +08:00
parent b6a478f891
commit 331060ff4b
2 changed files with 103 additions and 21 deletions

View File

@ -9,12 +9,12 @@ copy plugin/auto-pairs.vim to ~/.vim/plugin
Features
--------
* Insert in pair
input: [
output: [|]
* Delete in pair
input: foo[<BS>
output: foo
@ -57,8 +57,8 @@ Features
output: ('hello')
* Quick jump to closed pair.
input:
input:
{
something;|
}
@ -78,6 +78,47 @@ Features
output:
'''
* Fly Mode
input: if(a[3)
output: if(a[3])| (In Fly Mode)
output: if(a[3)]) (Without Fly Mode)
input:
{
hello();|
world();
}
(press } at |)
output:
{
hello();
world();
}|
(then press <M-b> at | to do backinsert)
output:
{
hello();}|
world();
}
See Fly Mode section for details
Fly Mode
--------
Fly Mode will always force closed-pair jumping instead of inserting. only for ")", "}", "]"
If jumps in mistake, could use AutoPairsBackInsert(Default Key: <M-b>) to jump back and insert closed pair.
the most situation maybe want to insert single closed pair in the string, eg ")"
Default Options:
let g:AutoPairs_FlyMode = 1
let g:AutoPairsShortcutBackInsert = '<M-b>'
Shortcuts
---------
@ -87,10 +128,11 @@ Shortcuts
<M-p> : Toggle Autopairs (g:AutoPairsShortcutToggle)
<M-e> : Fast Wrap (g:AutoPairsShortcutFastWrap)
<M-n> : Jump to next closed pair (g:AutoPairsShortcutJump)
<M-b> : BackInsert
If <M-p> <M-e> or <M-n> conflict with another keys or want to bind to another keys, add
let g:AutoPairscutToggle = '<another key>'
let g:AutoPairShortcutToggle = '<another key>'
to .vimrc, it the key is empty string '', then the shortcut will be disabled.
@ -107,7 +149,7 @@ Options
The shortcut to toggle autopairs.
* g:AutoPairsShortcutFastWrap
Default: '<M-e>'
Fast wrap the word. all pairs will be consider as a block (include <>).
@ -147,9 +189,21 @@ Options
Map <space> to insert a space after the opening character and before the closing one.
execute 'inoremap <buffer> <silent> <CR> <C-R>=AutoPairsSpace()<CR>'
* g:AutoPairsFlyMode
Default : 1
see FlyMode section for details.
* g:AutoPairsShortcutBackInsert
Default : <M-b>
Work with FlyMode, insert the key at the Fly Mode jumped postion
TroubleShooting
---------------
The script will remap keys ([{'"}]) <BS>,
The script will remap keys ([{'"}]) <BS>,
If auto pairs cannot work, use :imap ( to check if the map is corrected.
The correct map should be <C-R>=AutoPairsInsert("\(")<CR>
Or the plugin conflict with some other plugins.

View File

@ -1,8 +1,8 @@
" Insert or delete brackets, parens, quotes in pairs.
" Maintainer: JiangMiao <jiangfriend@gmail.com>
" Contributor: camthompson
" Last Change: 2012-03-22
" Version: 1.1.6
" Last Change: 2012-05-14
" Version: 1.2.0
" Homepage: http://www.vim.org/scripts/script.php?script_id=3599
" Repository: https://github.com/jiangmiao/auto-pairs
@ -11,14 +11,6 @@ if exists('g:AutoPairsLoaded') || &cp
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
@ -58,8 +50,20 @@ if !exists('g:AutoPairsShortcutJump')
let g:AutoPairsShortcutJump = '<M-n>'
endif
let g:AutoPairsClosedPairs = {}
" Fly mode will for closed pair to jump to closed pair instead of insert.
" also support AutoPairsBackInsert to insert pairs where jumped.
if !exists('g:AutoPairsFlyMode')
let g:AutoPairsFlyMode = 1
endif
" Work with Fly Mode, insert pair where jumped
if !exists('g:AutoPairsShortcutBackInsert')
let g:AutoPairsShortcutBackInsert = '<M-b>'
endif
" Will auto generated {']' => 1, ..., '}' => 1}in initialize.
let g:AutoPairsClosedPairs = {}
function! AutoPairsInsert(key)
@ -82,6 +86,7 @@ function! AutoPairsInsert(key)
return a:key
end
" The key is difference open-pair, then it means only for ) ] } by default
if !has_key(g:AutoPairs, a:key)
" Skip the character if next character is space
if current_char == ' ' && next_char == a:key
@ -103,6 +108,15 @@ function! AutoPairsInsert(key)
return "\<Right>"
end
" Fly Mode, and the key is closed-pairs, search closed-pair and jump
if g:AutoPairsFlyMode && has_key(g:AutoPairsClosedPairs, a:key)
let b:autopairs_saved_pair = [a:key, getpos('.')]
" Use 's' flag to overwritee '' or not is a question
if(search(a:key, 'W'))
return "\<Right>"
end
endif
" Input directly if the key is not an open key
return a:key
end
@ -181,7 +195,7 @@ function! AutoPairsFastWrap()
end
normal! x
if match(next_char, '\s') != -1
if next_char =~ '\s'
call search('\S', 'W')
let next_char = getline('.')[col('.')-1]
end
@ -191,7 +205,7 @@ function! AutoPairsFastWrap()
call search(close, 'W')
return "\<RIGHT>".current_char."\<LEFT>"
else
if match(next_char, '\w') != -1
if next_char =~ '\w'
execute "normal! he"
end
execute "normal! a".current_char
@ -228,7 +242,7 @@ function! AutoPairsReturn()
" conflict with javascript and coffee
" javascript need indent new line
" coffeescript forbid indent new line
if &filetype == 'coffeescript'
if &filetype == 'coffeescript' || &filetype == 'coffee'
return "\<ESC>k==o".cmd
else
return "\<ESC>=ko".cmd
@ -248,6 +262,16 @@ function! AutoPairsSpace()
return "\<SPACE>".cmd
endfunction
function! AutoPairsBackInsert()
if exists('b:autopairs_saved_pair')
let pair = b:autopairs_saved_pair[0]
let pos = b:autopairs_saved_pair[1]
call setpos('.', pos)
return pair
endif
return ''
endfunction
function! AutoPairsInit()
let b:autopairs_loaded = 1
let b:autopairs_enabled = 1
@ -274,6 +298,10 @@ function! AutoPairsInit()
execute 'inoremap <buffer> <silent> '.g:AutoPairsShortcutFastWrap.' <C-R>=AutoPairsFastWrap()<CR>'
end
if g:AutoPairsShortcutBackInsert != ''
execute 'inoremap <buffer> <silent> '.g:AutoPairsShortcutBackInsert.' <C-R>=AutoPairsBackInsert()<CR>'
end
if g:AutoPairsShortcutToggle != ''
" use <expr> to ensure showing the status when toggle
execute 'inoremap <buffer> <silent> <expr> '.g:AutoPairsShortcutToggle.' AutoPairsToggle()'