增强Vim插件 close pairs的功能--回车跳出
?
出于个人习惯,常用进输入多层括号时,想用回车逐层从内跳出。
如 print(intN.toString(|)) 逐层跳出。当然,对' " () [] {} 都有用:)
?
目前存在一些小问题:
1、当使用<del>删除右边的一个配对字符后,功能将失灵,返回普通模式 :)
2、如果要要强行找行,请使用 Shift+回车?
3、还有一些待发现:)
?
下面为源代码,修改部分已标出。
"==============================================================================
" closepairs.vim - Auto closes pairs of characters
"==============================================================================
"
" Author: ? NoWhereMan (Edoardo Vacchi) <uncommonnonsense at gmail dot com>
" Version: ?0.1
" URL: ? ?http://www.flatpress.org/
" License: ?Public Domain
"==============================================================================
?
?
"inoremap ( ()<left>
"inoremap { {}<left>
"inoremap [ []<left>
?
"by phoenix"
inoremap <expr> <Esc> <SID>esc()
inoremap <expr> <CR> <SID>cr()
?
vnoremap <leader>" "zdi"<c-r>z"
vnoremap <leader>' "zdi'<c-r>z'
vnoremap <leader>( "zdi(<c-r>z)
vnoremap <leader>[ "zdi[<c-r>z]
vnoremap <leader>{ "zdi{<c-r>z}
?
inoremap <expr> <bs> <SID>delpair()
?
inoremap <expr> ) <SID>escapepair(')')
inoremap <expr> } <SID>escapepair('}')
inoremap <expr> ] <SID>escapepair(']')
?
inoremap <expr> " <SID>pairquotes('"')
inoremap <expr> ' <SID>pairquotes("'")
inoremap <expr> ( <SID>pairquotes('(')
inoremap <expr> [ <SID>pairquotes("[")
inoremap <expr> { <SID>pairquotes("{")
?
?
?
function! s:delpair()
let l:lst = ['""',"''",'{}','[]','()']
let l:col = col('.')
let l:line = getline('.')
let l:chr = l:line[l:col-2 : l:col-1]
if index(l:lst, l:chr) > -1
return "\<bs>\<del>"
else
let l:chr = l:line[l:col-3:l:col-2]
if (index(l:lst, l:chr)) > - 1
return "\<bs>\<bs>"
endif
return "\<bs>"
endf
?
function! s:escapepair(right)
let l:col = col('.')
let l:chr = getline('.')[l:col-1]
if a:right == l:chr
return "\<right>"
else
return a:right
endf
"function! s:pairquotes(pair)
"let l:col = col('.')
"let l:line = getline('.')
"let l:chr = l:line[l:col-1]
"if a:pair == l:chr
"return "\<right>"
"else
"return a:pair.a:pair."\<left>"
?
"endf
?
"by phoenix
function! s:pairquotes(pair)
?? ?let l:dict={'"':'"',"'":"'",'(':')','[':']','{':'}'}
let l:col = col('.')
let l:line = getline('.')
?? ?if( a:pair=='"' || a:pair=="'")
?? ? ? ?let l:chr = l:line[l:col-1]
?? ?else
?? ? ? ?let l:chr = ""
?? ?endif
?? ?if l:dict[a:pair]==l:chr
?? ? ? ?if len(s:quotes)>0
?? ? ? ? ? ?call remove(s:quotes,len(s:quotes)-1)
?? ? ? ?endif
?? ? ? ?return "\<right>"
?? ?else
?? ? ? ?call add(s:quotes,l:dict[a:pair])
?? ? ? ?return a:pair.l:dict[a:pair]."\<left>"
?? ?endif
endf
function! s:esc()
?? let s:quotes=[]
??return "\<Esc>"
endf
function! s:cr()
?? ?if !empty(s:quotes)
?? ? ? ?let quote=s:quotes[len(s:quotes)-1]
?? ? ? ?call remove(s:quotes,len(s:quotes)-1)
?? ? ? ?return "\<esc>f".quote."i\<right>"
?? ?else
?? ? ? ?return "\<esc>o"
?? ?endif
endf
let s:quotes=[]