mirror of
https://github.com/Raimondi/delimitMate.git
synced 2025-12-06 12:44:27 +08:00
Don't use a char buffer anymore.
This commit is contained in:
@@ -181,35 +181,6 @@ function! delimitMate#CursorIdx() "{{{
|
||||
return idx
|
||||
endfunction "delimitMate#CursorCol }}}
|
||||
|
||||
function! delimitMate#WriteBefore(str) "{{{
|
||||
let len = len(a:str)
|
||||
let line = getline('.')
|
||||
let col = delimitMate#CursorIdx() - 1
|
||||
if col < 0
|
||||
call setline('.',line[(col+len+1):])
|
||||
else
|
||||
call setline('.',line[:(col)].line[(col+len+1):])
|
||||
endif
|
||||
return a:str
|
||||
endfunction " }}}
|
||||
|
||||
function! delimitMate#WriteAfter(str) "{{{
|
||||
let len = 1 "len(a:str)
|
||||
let line = split(getline('.'), '\zs')
|
||||
let col = delimitMate#CursorIdx() - 1
|
||||
if (col + 1) < 0 || col('.') == 1
|
||||
let line = insert(line, a:str)
|
||||
elseif col('.') == col('$')
|
||||
let line = add(line, a:str)
|
||||
else
|
||||
let line1 = line[:(col)]
|
||||
let line2 = line[(col+len):]
|
||||
let line = line1 + [a:str] + line2
|
||||
endif
|
||||
call setline('.', join(line, ''))
|
||||
return ''
|
||||
endfunction " }}}
|
||||
|
||||
function! delimitMate#GetSyntaxRegion(line, col) "{{{
|
||||
return synIDattr(synIDtrans(synID(a:line, a:col, 1)), 'name')
|
||||
endfunction " }}}
|
||||
@@ -247,18 +218,6 @@ function! delimitMate#IsForbidden(char) "{{{
|
||||
return index(s:g('excluded_regions_list'), region) >= 0
|
||||
endfunction "}}}
|
||||
|
||||
function! delimitMate#FlushBuffer() " {{{
|
||||
call s:s('buffer', [])
|
||||
return ''
|
||||
endfunction " }}}
|
||||
|
||||
function! delimitMate#AddToBuffer(str, ...) "{{{
|
||||
if a:0 && a:1 == 1
|
||||
return add(s:g('buffer'), a:str)
|
||||
endif
|
||||
return insert(s:g('buffer'), a:str)
|
||||
endfunction "delimitMate#AddToBuffer }}}
|
||||
|
||||
function! delimitMate#BalancedParens(char) "{{{
|
||||
" Returns:
|
||||
" = 0 => Parens balanced.
|
||||
@@ -296,13 +255,6 @@ function! delimitMate#BalancedParens(char) "{{{
|
||||
return opening - closing
|
||||
endfunction "}}}
|
||||
|
||||
function! delimitMate#RmBuffer(num) " {{{
|
||||
if len(s:g('buffer')) > 0
|
||||
call remove(s:g('buffer'), 0, (a:num-1))
|
||||
endif
|
||||
return ""
|
||||
endfunction " }}}
|
||||
|
||||
function! delimitMate#IsSmartQuote(char) "{{{
|
||||
if !s:g('smart_quotes')
|
||||
return 0
|
||||
@@ -341,12 +293,10 @@ function! delimitMate#SkipDelim(char) "{{{
|
||||
return a:char
|
||||
elseif cur == a:char
|
||||
" Exit pair
|
||||
"return delimitMate#WriteBefore(a:char)
|
||||
return a:char . delimitMate#Del()
|
||||
return a:char . "\<Del>"
|
||||
elseif delimitMate#IsEmptyPair( pre . a:char )
|
||||
" Add closing delimiter and jump back to the middle.
|
||||
call delimitMate#AddToBuffer(a:char)
|
||||
return delimitMate#WriteAfter(a:char)
|
||||
return a:char . "\<Left>"
|
||||
else
|
||||
" Nothing special here, return the same character.
|
||||
return a:char
|
||||
@@ -374,9 +324,7 @@ function! delimitMate#ParenDelim(right) " {{{
|
||||
return left
|
||||
"elseif (col) < 0
|
||||
" call setline('.',a:right.line)
|
||||
" call delimitMate#AddToBuffer(a:right)
|
||||
endif
|
||||
call delimitMate#AddToBuffer(a:right . tail)
|
||||
return left . a:right . tail . repeat("\<Left>", len(split(tail, '\zs')) + 1)
|
||||
endfunction " }}}
|
||||
|
||||
@@ -389,7 +337,7 @@ function! delimitMate#QuoteDelim(char) "{{{
|
||||
if char_at == a:char &&
|
||||
\ index(s:g('nesting_quotes'), a:char) < 0
|
||||
" Get out of the string.
|
||||
return a:char . delimitMate#Del()
|
||||
return "\<Right>"
|
||||
elseif delimitMate#IsSmartQuote(a:char)
|
||||
" Seems like a smart quote, insert a single char.
|
||||
return a:char
|
||||
@@ -397,8 +345,6 @@ function! delimitMate#QuoteDelim(char) "{{{
|
||||
\ && s:g('smart_quotes')
|
||||
" Seems like we have an unbalanced quote, insert one quotation
|
||||
" mark and jump to the middle.
|
||||
call delimitMate#AddToBuffer(a:char)
|
||||
"return delimitMate#WriteAfter(a:char)
|
||||
return a:char . "\<Left>"
|
||||
else
|
||||
" Insert a pair and jump to the middle.
|
||||
@@ -409,8 +355,6 @@ function! delimitMate#QuoteDelim(char) "{{{
|
||||
let has_marker = marker == s:g('eol_marker')
|
||||
let sufix = !has_marker ? s:g('eol_marker') : ''
|
||||
endif
|
||||
call delimitMate#AddToBuffer(a:char . sufix)
|
||||
"call delimitMate#WriteAfter(a:char . sufix)
|
||||
return a:char . a:char . "\<Left>"
|
||||
endif
|
||||
endfunction "}}}
|
||||
@@ -421,11 +365,10 @@ function! delimitMate#JumpOut(char) "{{{
|
||||
endif
|
||||
let jump = delimitMate#ShouldJump(a:char)
|
||||
if jump == 1
|
||||
return a:char . delimitMate#Del()
|
||||
return "\<Right>"
|
||||
elseif jump == 3
|
||||
return ' '.a:char.delimitMate#Del().delimitMate#Del()
|
||||
return "\<Right>\<Right>"
|
||||
elseif jump == 5
|
||||
call delimitMate#FlushBuffer()
|
||||
return "\<Down>\<C-O>I\<Right>"
|
||||
else
|
||||
return a:char
|
||||
@@ -443,16 +386,12 @@ function! delimitMate#JumpAny(...) " {{{
|
||||
let char = delimitMate#GetCharFromCursor(0)
|
||||
if char == " "
|
||||
" Space expansion.
|
||||
return char . getline('.')[col('.')] . delimitMate#Del() .
|
||||
\ delimitMate#Del()
|
||||
"call delimitMate#RmBuffer(1)
|
||||
return "\<Right>\<Right>"
|
||||
elseif char == ""
|
||||
" CR expansion.
|
||||
call delimitMate#FlushBuffer()
|
||||
return "\<CR>" . getline(line('.') + 1)[0] . delimitMate#Del() . "\<Del>"
|
||||
return "\<CR>" . getline(line('.') + 1)[0] . "\<Del>\<Del>"
|
||||
else
|
||||
"call delimitMate#RmBuffer(1)
|
||||
return char . delimitMate#Del()
|
||||
return "\<Right>"
|
||||
endif
|
||||
endfunction " delimitMate#JumpAny() }}}
|
||||
|
||||
@@ -484,8 +423,6 @@ function! delimitMate#ExpandReturn() "{{{
|
||||
endif
|
||||
if delimitMate#WithinEmptyPair()
|
||||
" Expand:
|
||||
call delimitMate#FlushBuffer()
|
||||
|
||||
" Not sure why I used the previous combos, but I'm sure somebody will
|
||||
" tell me about it.
|
||||
" XXX zv prevents breaking expansion with syntax folding enabled by
|
||||
@@ -504,76 +441,30 @@ function! delimitMate#ExpandSpace() "{{{
|
||||
\ && delimitMate#GetCharFromCursor(-2) == '\'
|
||||
if delimitMate#WithinEmptyPair() && !escaped
|
||||
" Expand:
|
||||
call delimitMate#AddToBuffer('s')
|
||||
return delimitMate#WriteAfter(' ') . "\<Space>"
|
||||
return "\<Space>\<Space>\<Left>"
|
||||
else
|
||||
return "\<Space>"
|
||||
endif
|
||||
endfunction "}}}
|
||||
|
||||
function! delimitMate#BS() " {{{
|
||||
let buffer_tail = get(s:g('buffer'), '-1', '')
|
||||
if delimitMate#IsForbidden("")
|
||||
let extra = ''
|
||||
elseif &backspace !~ 'start\|2' && empty(s:g('buffer'))
|
||||
elseif &bs !~ 'start\|2'
|
||||
let extra = ''
|
||||
elseif delimitMate#WithinEmptyPair()
|
||||
let extra = delimitMate#Del()
|
||||
let extra = "\<Del>"
|
||||
elseif delimitMate#IsSpaceExpansion()
|
||||
let extra = delimitMate#Del()
|
||||
let extra = "\<Del>"
|
||||
elseif delimitMate#IsCRExpansion()
|
||||
let extra = repeat("\<Del>",
|
||||
\ len(matchstr(getline(line('.') + 1), '^\s*\S')))
|
||||
else
|
||||
let extra = ''
|
||||
endif
|
||||
let tail_re = '\m\C\%('
|
||||
\ . join(s:g('right_delims')+s:g('quotes_list'), '\|')
|
||||
\ . '\)'
|
||||
\ . escape(s:g('eol_marker'), '\*.^$')
|
||||
\ . '$'
|
||||
if buffer_tail =~ tail_re && search('\%#'.tail_re, 'cWn')
|
||||
let extra .= join(map(split(s:g('eol_marker'), '\zs'),
|
||||
\ 'delimitMate#Del()'), '')
|
||||
endif
|
||||
return "\<BS>" . extra
|
||||
endfunction " }}} delimitMate#BS()
|
||||
|
||||
function! delimitMate#Del() " {{{
|
||||
if len(s:g('buffer')) > 0
|
||||
call delimitMate#RmBuffer(1)
|
||||
return "\<Del>"
|
||||
else
|
||||
return "\<Del>"
|
||||
endif
|
||||
endfunction " }}}
|
||||
|
||||
function! delimitMate#Finish(move_back) " {{{
|
||||
let len = len(s:g('buffer'))
|
||||
if len > 0
|
||||
let buffer = join(s:g('buffer'), '')
|
||||
let len2 = len(buffer)
|
||||
" Reset buffer:
|
||||
call s:s('buffer', [])
|
||||
let line = getline('.')
|
||||
let col = col('.') -2
|
||||
if col < 0
|
||||
call setline('.', line[col+len2+1:])
|
||||
else
|
||||
call setline('.', line[:col] . line[col+len2+1:])
|
||||
endif
|
||||
let i = 1
|
||||
let lefts = ""
|
||||
while i <= len && a:move_back
|
||||
let lefts = lefts . "\<Left>"
|
||||
let i += 1
|
||||
endwhile
|
||||
let result = substitute(buffer, "s", "\<Space>", 'g') . lefts
|
||||
return result
|
||||
endif
|
||||
return ''
|
||||
endfunction " }}}
|
||||
|
||||
" }}}
|
||||
|
||||
" Tools: {{{
|
||||
@@ -596,18 +487,7 @@ function! delimitMate#TestMappings() "{{{
|
||||
let imaps +=
|
||||
\ s:g('quotes_list') +
|
||||
\ s:g('apostrophes_list') +
|
||||
\ ['<BS>', '<S-BS>', '<Del>', '<S-Tab>', '<Esc>'] +
|
||||
\ ['<Up>', '<Down>', '<Left>', '<Right>', '<LeftMouse>',
|
||||
\ '<RightMouse>'] +
|
||||
\ ['<Home>', '<End>', '<PageUp>', '<PageDown>', '<S-Down>',
|
||||
\ '<S-Up>', '<C-G>g'] +
|
||||
\ ['<ScrollWheelUp>', '<S-ScrollWheelUp>', '<C-ScrollWheelUp>'] +
|
||||
\ ['<ScrollWheelDown>', '<S-ScrollWheelDown>',
|
||||
\ '<C-ScrollWheelDown>'] +
|
||||
\ ['<ScrollWheelLeft>', '<S-ScrollWheelLeft>',
|
||||
\ '<C-ScrollWheelLeft>'] +
|
||||
\ ['<ScrollWheelRight>', '<S-ScrollWheelRight>',
|
||||
\ '<C-ScrollWheelRight>']
|
||||
\ ['<BS>', '<S-BS>', '<S-Tab>', '<Esc>', '<C-G>g']
|
||||
let imaps += ( s:g('expand_cr') ? ['<CR>'] : [] )
|
||||
let imaps += ( s:g('expand_space') ? ['<Space>'] : [] )
|
||||
|
||||
|
||||
@@ -252,24 +252,6 @@ function! s:DelimitMateSwitch() "{{{
|
||||
endif
|
||||
endfunction "}}}
|
||||
|
||||
function! s:Finish() " {{{
|
||||
if exists('b:delimitMate_enabled')
|
||||
return delimitMate#Finish(1)
|
||||
endif
|
||||
return ''
|
||||
endfunction " }}}
|
||||
|
||||
function! s:FlushBuffer() " {{{
|
||||
if exists('b:delimitMate_enabled')
|
||||
return delimitMate#FlushBuffer()
|
||||
endif
|
||||
return ''
|
||||
endfunction " }}}
|
||||
|
||||
function! s:empty_buffer()
|
||||
return empty(s:g('buffer'))
|
||||
endfunction
|
||||
|
||||
"}}}
|
||||
|
||||
" Mappers: {{{
|
||||
@@ -327,18 +309,18 @@ function! s:ExtraMappings() "{{{
|
||||
silent! imap <unique> <buffer> <BS> <Plug>delimitMateBS
|
||||
endif
|
||||
" If pair is empty, delete closing delimiter:
|
||||
inoremap <silent> <expr> <Plug>delimitMateS-BS delimitMate#WithinEmptyPair() ? "\<C-R>=delimitMate#Del()\<CR>" : "\<S-BS>"
|
||||
inoremap <silent> <expr> <Plug>delimitMateS-BS delimitMate#WithinEmptyPair() ? "\<Del>" : "\<S-BS>"
|
||||
if !hasmapto('<Plug>delimitMateS-BS','i') && maparg('<S-BS>', 'i') == ''
|
||||
silent! imap <unique> <buffer> <S-BS> <Plug>delimitMateS-BS
|
||||
endif
|
||||
" Expand return if inside an empty pair:
|
||||
inoremap <silent> <Plug>delimitMateCR <C-R>=delimitMate#ExpandReturn()<CR>
|
||||
if s:g('expand_cr') != 0 && !hasmapto('<Plug>delimitMateCR', 'i') && maparg('<CR>', 'i') == ''
|
||||
if s:g('expand_cr') && !hasmapto('<Plug>delimitMateCR', 'i') && maparg('<CR>', 'i') == ''
|
||||
silent! imap <unique> <buffer> <CR> <Plug>delimitMateCR
|
||||
endif
|
||||
" Expand space if inside an empty pair:
|
||||
inoremap <silent> <Plug>delimitMateSpace <C-R>=delimitMate#ExpandSpace()<CR>
|
||||
if s:g('expand_space') != 0 && !hasmapto('<Plug>delimitMateSpace', 'i') && maparg('<Space>', 'i') == ''
|
||||
if s:g('expand_space') && !hasmapto('<Plug>delimitMateSpace', 'i') && maparg('<Space>', 'i') == ''
|
||||
silent! imap <unique> <buffer> <Space> <Plug>delimitMateSpace
|
||||
endif
|
||||
" Jump over any delimiter:
|
||||
@@ -346,49 +328,8 @@ function! s:ExtraMappings() "{{{
|
||||
if s:g('tab2exit') && !hasmapto('<Plug>delimitMateS-Tab', 'i') && maparg('<S-Tab>', 'i') == ''
|
||||
silent! imap <unique> <buffer> <S-Tab> <Plug>delimitMateS-Tab
|
||||
endif
|
||||
" Change char buffer on Del:
|
||||
inoremap <silent> <Plug>delimitMateDel <C-R>=delimitMate#Del()<CR>
|
||||
if !hasmapto('<Plug>delimitMateDel', 'i') && maparg('<Del>', 'i') == ''
|
||||
silent! imap <unique> <buffer> <Del> <Plug>delimitMateDel
|
||||
endif
|
||||
let keys = ['Left', 'Right', 'Home', 'End', 'C-Left', 'C-Right',
|
||||
\ 'ScrollWheelUp', 'S-ScrollWheelUp', 'C-ScrollWheelUp',
|
||||
\ 'ScrollWheelDown', 'S-ScrollWheelDown', 'C-ScrollWheelDown',
|
||||
\ 'ScrollWheelLeft', 'S-ScrollWheelLeft', 'C-ScrollWheelLeft',
|
||||
\ 'ScrollWheelRight', 'S-ScrollWheelRight', 'C-ScrollWheelRight']
|
||||
" Flush the char buffer on movement keystrokes:
|
||||
for map in keys
|
||||
exec 'inoremap <silent><expr> <Plug>delimitMate'.map.' !<SID>empty_buffer() ? "<C-R>=delimitMate#Finish(1)<CR><'.map.'>" : "<'.map.'>"'
|
||||
if !hasmapto('<Plug>delimitMate'.map, 'i') && maparg('<'.map.'>', 'i') == ''
|
||||
exec 'silent! imap <unique> <buffer> <'.map.'> <Plug>delimitMate'.map
|
||||
endif
|
||||
endfor
|
||||
" Also for default MacVim movements:
|
||||
if has('gui_macvim')
|
||||
for [key, map] in [['D-Left','Home'], ['D-Right','End'], ['M-Left','C-Left'], ['M-Right','C-Right']]
|
||||
exec 'inoremap <silent> <Plug>delimitMate'.key.' <C-R>=<SID>Finish()<CR><'.map.'>'
|
||||
if mapcheck('<'.key.'>', 'i') == '<'.map.'>'
|
||||
exec 'silent! imap <buffer> <'.key.'> <Plug>delimitMate'.key
|
||||
endif
|
||||
endfor
|
||||
endif
|
||||
" Except when pop-up menu is active:
|
||||
for map in ['Up', 'Down', 'PageUp', 'PageDown', 'S-Down', 'S-Up']
|
||||
exec 'inoremap <silent> <expr> <Plug>delimitMate'.map.' pumvisible() \|\| <SID>empty_buffer() ? "\<'.map.'>" : "\<C-R>=\<SID>Finish()\<CR>\<'.map.'>"'
|
||||
if !hasmapto('<Plug>delimitMate'.map, 'i') && maparg('<'.map.'>', 'i') == ''
|
||||
exec 'silent! imap <unique> <buffer> <'.map.'> <Plug>delimitMate'.map
|
||||
endif
|
||||
endfor
|
||||
" Avoid ambiguous mappings:
|
||||
for map in ['LeftMouse', 'RightMouse']
|
||||
exec 'inoremap <silent> <Plug>delimitMateM'.map.' <C-R>=delimitMate#Finish(1)<CR><'.map.'>'
|
||||
if !hasmapto('<Plug>delimitMate'.map, 'i') && maparg('<'.map.'>', 'i') == ''
|
||||
exec 'silent! imap <unique> <buffer> <'.map.'> <Plug>delimitMateM'.map
|
||||
endif
|
||||
endfor
|
||||
|
||||
" Jump over next delimiters
|
||||
inoremap <buffer> <Plug>delimitMateJumpMany <C-R>=len(delimitMate#Get('buffer')) ? delimitMate#Finish(0) : delimitMate#JumpMany()<CR>
|
||||
inoremap <buffer> <Plug>delimitMateJumpMany <C-R>=delimitMate#JumpMany()<CR>
|
||||
if !hasmapto('<Plug>delimitMateJumpMany', 'i') && maparg("<C-G>g", 'i') == ''
|
||||
imap <silent> <buffer> <C-G>g <Plug>delimitMateJumpMany
|
||||
endif
|
||||
@@ -424,14 +365,6 @@ augroup delimitMate
|
||||
\ call <SID>DelimitMateDo() |
|
||||
\ let b:delimitMate_was_here = 1 |
|
||||
\ endif
|
||||
|
||||
" Flush the char buffer:
|
||||
autocmd InsertEnter * call <SID>FlushBuffer()
|
||||
autocmd BufEnter *
|
||||
\ if mode() == 'i' |
|
||||
\ call <SID>FlushBuffer() |
|
||||
\ endif
|
||||
|
||||
augroup END
|
||||
|
||||
"}}}
|
||||
|
||||
Reference in New Issue
Block a user