Now re-do-safe, except for <CR> expansion.

This commit is contained in:
Israel Chauca Fuentes
2010-05-03 03:04:57 -05:00
parent 9292c9294a
commit a751498fbc
3 changed files with 97 additions and 41 deletions

View File

@@ -1,6 +1,10 @@
PLUGIN=delimitMate
install:
install -m 755 -d ~/.vim
install -m 755 -d ~/.vim/plugin/
install -m 755 -d ~/.vim/autoload/
install -m 755 -d ~/.vim/doc/
cp -f doc/${PLUGIN}.txt ~/.vim/doc/${PLUGIN}.txt
cp -f plugin/${PLUGIN}.vim ~/.vim/plugin/${PLUGIN}.vim
cp -f autoload/${PLUGIN}.vim ~/.vim/autoload/${PLUGIN}.vim

View File

@@ -167,6 +167,11 @@ function! delimitMate#IsForbidden(char) "{{{
"return result || region == 'Comment'
return result
endfunction "}}}
function! delimitMate#FlushBuffer() " {{{
let b:delimitMate_buffer = []
return ''
endfunction " }}}
" }}}
" Doers {{{
@@ -175,9 +180,11 @@ function! delimitMate#JumpIn(char) " {{{
let col = col('.')-2
if (col) < 0
call setline('.',a:char.line)
call insert(b:delimitMate_buffer, a:char)
else
"echom string(col).':'.line[:(col)].'|'.line[(col+1):]
call setline('.',line[:(col)].a:char.line[(col+1):])
call insert(b:delimitMate_buffer, a:char)
endif
return ''
endfunction " }}}
@@ -187,6 +194,7 @@ function! delimitMate#JumpOut(char) "{{{
let col = col('.')-2
if line[col+1] == a:char
call setline('.',line[:(col)].line[(col+2):])
call delimitMate#RmBuffer(1)
endif
return a:char
endfunction " }}}
@@ -197,10 +205,13 @@ function! delimitMate#JumpAny() " {{{
if char == " "
" Space expansion.
let char = char . getline('.')[col('.')] . "\<Del>"
call delimitMate#RmBuffer(2)
elseif char == ""
" CR expansion.
let char = "\<CR>" . getline(line('.') + 1)[0] . "\<Del>"
let b:delimitMate_buffer = []
endif
call delimitMate#RmBuffer(1)
return char . "\<Del>"
endfunction " delimitMate#JumpAny() }}}
@@ -257,6 +268,7 @@ function! delimitMate#ExpandReturn() "{{{
if delimitMate#WithinEmptyPair() &&
\ b:delimitMate_expand_cr
" Expand:
call delimitMate#FlushBuffer()
return "\<Esc>a\<CR>x\<CR>\<Esc>k$\"_xa"
else
return "\<CR>"
@@ -267,6 +279,7 @@ function! delimitMate#ExpandSpace() "{{{
if delimitMate#WithinEmptyPair() &&
\ b:delimitMate_expand_space
" Expand:
call insert(b:delimitMate_buffer, 's')
return delimitMate#WriteAfter(' ') . "\<Space>"
else
return "\<Space>"
@@ -275,15 +288,44 @@ endfunction "}}}
function! delimitMate#BS() " {{{
if delimitMate#WithinEmptyPair()
return "\<Right>\<BS>\<BS>"
call delimitMate#RmBuffer(1)
return "\<BS>\<Del>"
" return "\<Right>\<BS>\<BS>"
elseif b:delimitMate_expand_cr &&
\ (delimitMate#IsCRExpansion() != 0 || delimitMate#IsSpaceExpansion())
call delimitMate#RmBuffer(1)
return "\<BS>\<Del>"
else
return "\<BS>"
endif
endfunction " }}} delimitMate#BS()
function! delimitMate#Finish() " {{{
let len = len(b:delimitMate_buffer)
if len > 0
let buffer = join(b:delimitMate_buffer, '')
let line = getline('.')
let col = col('.') -2
echom 'col: ' . col . '-' . line[:col] . "|" . line[col+len+1:] . '%' . buffer
call setline('.', line[:col] . line[col+len+1:])
let i = 1
let lefts = ''
while i < len
let lefts = lefts . "\<Left>"
let i += 1
endwhile
return substitute(buffer, "s", "\<Space>", 'g') . lefts
endif
return ''
endfunction " }}}
function! delimitMate#RmBuffer(num) " {{{
if len(b:delimitMate_buffer) > 0
call remove(b:delimitMate_buffer, 0, (a:num-1))
endif
return ""
endfunction " }}}
" }}}
" Mappers: {{{
@@ -302,7 +344,7 @@ function! delimitMate#AutoClose() "{{{
let ld = b:delimitMate_left_delims[i]
let rd = b:delimitMate_right_delims[i]
exec 'inoremap <buffer> ' . ld . ' ' . ld . '<C-R>=delimitMate#JumpIn("' . rd . '")<CR>'
"exec 'inoremap <buffer> ' . ld . ' ' '<C-R>=delimitMate#JumpIn("' . ld . '")<CR>'
"exec 'inoremap <buffer> ' . ld . ' <C-R>=delimitMate#JumpIn("' . ld . '")<CR>'
let i += 1
endwhile
@@ -368,6 +410,20 @@ function! delimitMate#ExtraMappings() "{{{
if b:delimitMate_tab2exit
inoremap <buffer> <expr> <S-Tab> delimitMate#ShouldJump() ? delimitMate#JumpAny() : "\<S-Tab>"
endif
" Fix the re-do feature:
inoremap <buffer> <Esc> <C-R>=delimitMate#Finish()<CR><Esc>
" Flush the char buffer on mouse click:
inoremap <buffer> <LeftMouse> <C-R>=delimitMate#FlushBuffer()<CR><LeftMouse>
inoremap <buffer> <RightMouse> <C-R>=delimitMate#FlushBuffer()<CR><RightMouse>
" Flush the char buffer on key movements:
inoremap <buffer> <Left> <C-R>=delimitMate#FlushBuffer()<CR><Left>
inoremap <buffer> <Right> <C-R>=delimitMate#FlushBuffer()<CR><Right>
inoremap <buffer> <Up> <C-R>=delimitMate#FlushBuffer()<CR><Up>
inoremap <buffer> <Down> <C-R>=delimitMate#FlushBuffer()<CR><Down>
endfunction "}}}
"}}}
@@ -423,6 +479,9 @@ function! delimitMate#TestMappings() "{{{
exec "normal \<Esc>i"
endfunction "}}}
function! delimitMate#Tests() " {{{
endfunction " }}}
"}}}
" vim:foldmethod=marker:foldcolumn=4

View File

@@ -1,22 +1,9 @@
" ============================================================================
" File: delimitMate.vim
" Version: 2.0
" Description: This plugin tries to emulate the auto-completion of delimiters
" that TextMate provides.
" File: plugin/delimitMate.vim
" Version: 2.1
" Description: This plugin provides auto-completion for quotes, parens, etc.
" Maintainer: Israel Chauca F. <israelchauca@gmail.com>
" Manual: Read ":help delimitMate".
" Credits: Some of the code is modified or just copied from the following:
"
" - Ian McCracken
" Post titled: Vim, Part II: Matching Pairs:
" http://concisionandconcinnity.blogspot.com/
"
" - Aristotle Pagaltzis
" From the comments on the previous blog post and from:
" http://gist.github.com/144619
"
" - Vim Scripts:
" http://www.vim.org/scripts/
" Initialization: {{{
if exists("g:loaded_delimitMate") "{{{
@@ -295,6 +282,12 @@ autocmd FileType * call <SID>DelimitMateDo()
" Run on new buffers.
autocmd BufNewFile,BufRead,BufEnter * if !exists("b:loaded_delimitMate") | call <SID>DelimitMateDo() | endif
" Flush the char buffer:
autocmd InsertEnter * call delimitMate#FlushBuffer()
autocmd BufEnter * if mode() == 'i' | call delimitMate#FlushBuffer() | endif
"function! s:GetSynRegion () | echo synIDattr(synIDtrans(synID(line('.'), col('.'), 1)), 'name') | endfunction
"}}}
" GetLatestVimScripts: 2754 1 :AutoInstall: delimitMate.vim