Use {<enter> instead of {{ (Thanks to linopolus)

https://github.com/jiangmiao/auto-pairs/issues/1

Add Toggle key
Add Options
  g:AutoPairsShortcutToggle
  g:AutoPairsMapBS
  g:AutoPairsMapCR
  g:AutoPairsCenterLine
This commit is contained in:
jiangfriend@gmail.com 2011-06-07 12:40:51 +08:00
parent 8d48d95dd8
commit e893ae3d48
2 changed files with 124 additions and 16 deletions

View File

@ -18,9 +18,9 @@ Features
input: foo[<BS> input: foo[<BS>
output: foo output: foo
### Double input open brackets will insert new indented line. ### Insert new indented line after Return
input: {{ input: {|} (press <CR> at |)
output: { output: {
| |
} }
@ -35,12 +35,33 @@ Features
input: "\' input: "\'
output: "\'" output: "\'"
Shortcuts
---------
System Shortcuts:
<CR> : Insert new indented line after return if cursor in blank brackets or quotes.
<BS> : Delete brackets in pair
<M-p> : Toggle Autopairs
Optional Shortcuts:
could be turn off by let g:AutoPairsShortcuts = 0
<M-n> jump to next closed bracket.
<M-a> jump to end of line.
<M-o> jump to newline with indented.
Options Options
------- -------
* g:AutoPairs * g:AutoPairs
Default: {'(':')', '[':']', '{':'}',"'":"'",'"':'"'} Default: {'(':')', '[':']', '{':'}',"'":"'",'"':'"'}
* g:AutoPairsShortcutToggle
Default: '<M-p>'
The shortcut to toggle autopairs.
* g:AutoPairsShortcuts * g:AutoPairsShortcuts
Default: 1 Default: 1
@ -50,6 +71,26 @@ Options
<M-a> jump to end of line. <M-a> jump to end of line.
<M-o> jump to newline with indented. <M-o> jump to newline with indented.
* g:AutoPairsMapBS
Default : 1
Map <BS> to delete brackets, quotes in pair
execute 'inoremap <buffer> <silent> <BS> <C-R>=AutoPairsDelete()<CR>'
* g:AutoPairsMapCR
Default : 1
Map <CR> to insert a new indented line if cursor in (|), {|} [|], '|', "|"
execute 'inoremap <buffer> <silent> <CR> <C-R>=AutoPairsReturn()<CR>'
* g:AutoPairsCenterLine
Default : 1
When g:AutoPairsMapCR is on, center current line after return if the line is at the bottom 1/3 of the window.
TroubleShooting TroubleShooting
--------------- ---------------
The script will remap keys ([{'"}]) <BS>, The script will remap keys ([{'"}]) <BS>,

View File

@ -1,7 +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>
" Last Change: 2011-05-22 " Last Change: 2011-06-07
" Version: 1.0.1 " Version: 1.0.2
" 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
if exists('g:AutoPairsLoaded') || &cp if exists('g:AutoPairsLoaded') || &cp
@ -21,15 +22,40 @@ if !exists('g:AutoPairs')
let g:AutoPairs = {'(':')', '[':']', '{':'}',"'":"'",'"':'"'} let g:AutoPairs = {'(':')', '[':']', '{':'}',"'":"'",'"':'"'}
end end
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
let g:AutoPairsClosedPairs = {} let g:AutoPairsClosedPairs = {}
function! AutoPairsInsert(key) function! AutoPairsInsert(key)
if !b:autopairs_enabled
return a:key
end
let line = getline('.') let line = getline('.')
let prev_char = line[col('.')-2] let prev_char = line[col('.')-2]
let current_char = line[col('.')-1] let current_char = line[col('.')-1]
let eol = 0
if col('$') - col('.') <= 1
let eol = 1
end
" Ignore auto close if prev character is \ " Ignore auto close if prev character is \
if prev_char == '\' if prev_char == '\'
return a:key return a:key
@ -52,12 +78,6 @@ function! AutoPairsInsert(key)
return "\<Right>" return "\<Right>"
end end
" Auto return only if open and close is same
if prev_char == open && open != close
return "\<CR>\<ESC>==O"
end
return open.close."\<Left>" return open.close."\<Left>"
endfunction endfunction
@ -86,12 +106,24 @@ function! AutoPairsJump()
call search('[{("\[\]'')}]','W') call search('[{("\[\]'')}]','W')
endfunction endfunction
" Fast wrap the word in brackets
" Haven't finished yet
function! AutoPairsExtend() function! AutoPairsExtend()
let line = getline('.') let line = getline('.')
let current_char = line[col('.')-1] let current_char = line[col('.')-1]
let next_char = line[col('.')]
if has_key(g:AutoPairsClosedPairs, current_char) if has_key(g:AutoPairsClosedPairs, current_char)
return "\<ESC>lxh:call AutoPairsJump(line('.'))\<CR>pi" if has_key(g:AutoPairs, next_char)
let open = next_char
let close = g:AutoPairs[next_char]
let quote_pattern = '(?:\\\|\"\|[^"])*'
echoe 'search pair '.open.' '.close
call searchpair(open, '', close, 'W')
end
execute "normal! a".current_char."\<LEFT>"
return ''
end end
return '' return ''
@ -101,7 +133,33 @@ function! AutoPairsMap(key)
execute 'inoremap <buffer> <silent> '.a:key.' <C-R>=AutoPairsInsert("\'.a:key.'")<CR>' execute 'inoremap <buffer> <silent> '.a:key.' <C-R>=AutoPairsInsert("\'.a:key.'")<CR>'
endfunction endfunction
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
function! AutoPairsInit() function! AutoPairsInit()
let b:autopairs_enabled = 1
for [open, close] in items(g:AutoPairs) for [open, close] in items(g:AutoPairs)
call AutoPairsMap(open) call AutoPairsMap(open)
if open != close if open != close
@ -109,8 +167,17 @@ function! AutoPairsInit()
end end
let g:AutoPairsClosedPairs[close] = 1 let g:AutoPairsClosedPairs[close] = 1
endfor endfor
execute 'inoremap <buffer> <silent> <BS> <C-R>=AutoPairsDelete()<CR>'
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>'
" If the keys map conflict with your own settings, delete or change them " If the keys map conflict with your own settings, delete or change them
if g:AutoPairsShortcuts if g:AutoPairsShortcuts
execute 'inoremap <buffer> <silent> <M-n> <ESC>:call AutoPairsJump()<CR>a' execute 'inoremap <buffer> <silent> <M-n> <ESC>:call AutoPairsJump()<CR>a'
@ -120,4 +187,4 @@ function! AutoPairsInit()
end end
endfunction endfunction
au BufRead,BufNewFile * :call AutoPairsInit() au BufRead,BufNewFile,BufCreate * :call AutoPairsInit()