From a751498fbcb735c7599c3262b9688f89da855af0 Mon Sep 17 00:00:00 2001 From: Israel Chauca Fuentes Date: Mon, 3 May 2010 03:04:57 -0500 Subject: [PATCH] Now re-do-safe, except for expansion. --- Makefile | 4 ++ autoload/delimitMate.vim | 105 ++++++++++++++++++++++++++++++--------- plugin/delimitMate.vim | 29 ++++------- 3 files changed, 97 insertions(+), 41 deletions(-) diff --git a/Makefile b/Makefile index 00a4403..c710b42 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/autoload/delimitMate.vim b/autoload/delimitMate.vim index eeb8a15..c7c0e08 100644 --- a/autoload/delimitMate.vim +++ b/autoload/delimitMate.vim @@ -144,21 +144,21 @@ function! delimitMate#RestoreRegister() " {{{ endfunction " }}} function! delimitMate#GetCurrentSyntaxRegion() "{{{ - return synIDattr(synIDtrans(synID(line('.'), col('.'), 1)), 'name') + return synIDattr(synIDtrans(synID(line('.'), col('.'), 1)), 'name') endfunction " }}} function! delimitMate#GetCurrentSyntaxRegionIf(char) "{{{ let col = col('.') - let origin_line = getline('.') - let changed_line = strpart(origin_line, 0, col - 1) . a:char . strpart(origin_line, col - 1) - call setline('.', changed_line) - let region = synIDattr(synIDtrans(synID(line('.'), col, 1)), 'name') - call setline('.', origin_line) - return region + let origin_line = getline('.') + let changed_line = strpart(origin_line, 0, col - 1) . a:char . strpart(origin_line, col - 1) + call setline('.', changed_line) + let region = synIDattr(synIDtrans(synID(line('.'), col, 1)), 'name') + call setline('.', origin_line) + return region endfunction "}}} function! delimitMate#IsForbidden(char) "{{{ - let result = index(b:delimitMate_excluded_regions_list, delimitMate#GetCurrentSyntaxRegion()) >= 0 + let result = index(b:delimitMate_excluded_regions_list, delimitMate#GetCurrentSyntaxRegion()) >= 0 if result return result endif @@ -167,19 +167,26 @@ function! delimitMate#IsForbidden(char) "{{{ "return result || region == 'Comment' return result endfunction "}}} + +function! delimitMate#FlushBuffer() " {{{ + let b:delimitMate_buffer = [] + return '' +endfunction " }}} " }}} " Doers {{{ function! delimitMate#JumpIn(char) " {{{ - let line = getline('.') - let col = col('.')-2 - if (col) < 0 - call setline('.',a:char.line) - else - "echom string(col).':'.line[:(col)].'|'.line[(col+1):] - call setline('.',line[:(col)].a:char.line[(col+1):]) - endif - return '' + let line = getline('.') + 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 " }}} function! delimitMate#JumpOut(char) "{{{ @@ -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('.')] . "\" + call delimitMate#RmBuffer(2) elseif char == "" " CR expansion. let char = "\" . getline(line('.') + 1)[0] . "\" + let b:delimitMate_buffer = [] endif + call delimitMate#RmBuffer(1) return char . "\" endfunction " delimitMate#JumpAny() }}} @@ -257,6 +268,7 @@ function! delimitMate#ExpandReturn() "{{{ if delimitMate#WithinEmptyPair() && \ b:delimitMate_expand_cr " Expand: + call delimitMate#FlushBuffer() return "\a\x\\k$\"_xa" else return "\" @@ -267,6 +279,7 @@ function! delimitMate#ExpandSpace() "{{{ if delimitMate#WithinEmptyPair() && \ b:delimitMate_expand_space " Expand: + call insert(b:delimitMate_buffer, 's') return delimitMate#WriteAfter(' ') . "\" else return "\" @@ -275,15 +288,44 @@ endfunction "}}} function! delimitMate#BS() " {{{ if delimitMate#WithinEmptyPair() - return "\\\" + call delimitMate#RmBuffer(1) + return "\\" +" return "\\\" elseif b:delimitMate_expand_cr && \ (delimitMate#IsCRExpansion() != 0 || delimitMate#IsSpaceExpansion()) - return "\\" - else - return "\" - endif + call delimitMate#RmBuffer(1) + return "\\" + else + return "\" + 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 . "\" + let i += 1 + endwhile + return substitute(buffer, "s", "\", '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 ' . ld . ' ' . ld . '=delimitMate#JumpIn("' . rd . '")' - "exec 'inoremap ' . ld . ' ' '=delimitMate#JumpIn("' . ld . '")' + "exec 'inoremap ' . ld . ' =delimitMate#JumpIn("' . ld . '")' let i += 1 endwhile @@ -368,6 +410,20 @@ function! delimitMate#ExtraMappings() "{{{ if b:delimitMate_tab2exit inoremap delimitMate#ShouldJump() ? delimitMate#JumpAny() : "\" endif + + " Fix the re-do feature: + inoremap =delimitMate#Finish() + + " Flush the char buffer on mouse click: + inoremap =delimitMate#FlushBuffer() + inoremap =delimitMate#FlushBuffer() + + " Flush the char buffer on key movements: + inoremap =delimitMate#FlushBuffer() + inoremap =delimitMate#FlushBuffer() + inoremap =delimitMate#FlushBuffer() + inoremap =delimitMate#FlushBuffer() + endfunction "}}} "}}} @@ -423,6 +479,9 @@ function! delimitMate#TestMappings() "{{{ exec "normal \i" endfunction "}}} +function! delimitMate#Tests() " {{{ + +endfunction " }}} "}}} " vim:foldmethod=marker:foldcolumn=4 diff --git a/plugin/delimitMate.vim b/plugin/delimitMate.vim index a611570..d1675d4 100644 --- a/plugin/delimitMate.vim +++ b/plugin/delimitMate.vim @@ -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. " 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") "{{{ @@ -269,8 +256,8 @@ function! s:DelimitMateDo() "{{{ let save_keymap = &keymap set keymap= set cpo&vim - let save_keymap = &keymap - set keymap= + let save_keymap = &keymap + set keymap= call s:Init() finally let &cpo = save_cpo @@ -295,6 +282,12 @@ autocmd FileType * call DelimitMateDo() " Run on new buffers. autocmd BufNewFile,BufRead,BufEnter * if !exists("b:loaded_delimitMate") | call 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