From a8cbf2e42919148e0d70612b1cb09b3fdaa72d7f Mon Sep 17 00:00:00 2001 From: Israel Chauca Fuentes Date: Wed, 7 Apr 2010 03:35:40 -0500 Subject: [PATCH] More support for expansions & syntax groups. --- plugin/delimitMate.vim | 138 +++++++++++++++++++++++++++++------------ 1 file changed, 100 insertions(+), 38 deletions(-) diff --git a/plugin/delimitMate.vim b/plugin/delimitMate.vim index f8f56fe..9eb6526 100644 --- a/plugin/delimitMate.vim +++ b/plugin/delimitMate.vim @@ -108,6 +108,16 @@ function! s:Init() "{{{ endif let b:delimitMate_quotes_list = s:quotes " }}} + " delimitMate_excluded_regions {{{ + if exists("b:delimitMate_excluded_regions") + let s:excluded_regions = b:delimitMate_excluded_regions + elseif exists("g:delimitMate_excluded_regions") + let s:excluded_regions = g:delimitMate_excluded_regions + else + let s:excluded_regions = split("Comment") + endif + let b:delimitMate_excluded_regions_list = s:excluded_regions " }}} + " delimitMate_visual_leader {{{ if !exists("b:delimitMate_visual_leader") && !exists("g:delimitMate_visual_leader") let b:delimitMate_visual_leader = exists('b:maplocalleader') ? b:maplocalleader : @@ -292,8 +302,6 @@ function! s:IsSpaceExpansion() " {{{ \ index(b:delimitMate_quotes_list, pchar) == index(b:delimitMate_quotes_list, nchar) && \ isSpaces return 1 - else - return 0 endif endif return 0 @@ -333,26 +341,52 @@ function! s:RestoreRegister() " {{{ call setreg('"', s:save_reg, s:save_reg_mode) echo "" endfunction " }}} + +function! s:GetCurrentSyntaxRegion() "{{{ + return synIDattr(synIDtrans(synID(line('.'), col('.'), 1)), 'name') +endfunction " }}} + +function! s: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 +endfunction "}}} + +function! s:IsForbidden(char) "{{{ + let result = index(b:delimitMate_excluded_regions_list, s:GetCurrentSyntaxRegion()) >= 0 + if result + return result + endif + let region = s:GetCurrentSyntaxRegionIf(a:char) + let result = index(b:delimitMate_excluded_regions_list, region) >= 0 + "return result || region == 'Comment' + return result +endfunction "}}} + " }}} " Doers: {{{ function! s: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 '' + if s:IsForbidden(b:delimitMate_left_delims[index(b:delimitMate_right_delims, a:char)]) + echom 1 + return a:char + endif + call s:WriteAfter(b:delimitMate_right_delims[index(b:delimitMate_left_delims, a:char)]) + return a:char endfunction " }}} function! s:JumpOut(char) "{{{ + if s:IsForbidden(a:char) + return a:char + endif let line = getline('.') - let col = col('.')-2 - if line[col+1] == a:char - call setline('.',line[:(col)].line[(col+2):]) + let col = col('.') - 2 + if line[col + 1] == a:char + return a:char . "\" endif return a:char endfunction " }}} @@ -371,6 +405,9 @@ function! DelimitMate_JumpAny() " {{{ endfunction " DelimitMate_JumpAny() }}} function! s:SkipDelim(char) "{{{ + if s:IsForbidden(a:char) + return a:char + endif let cur = strpart( getline('.'), col('.')-2, 3 ) if cur[0] == "\\" " Escaped character @@ -391,6 +428,9 @@ function! s:SkipDelim(char) "{{{ endfunction "}}} function! s:QuoteDelim(char) "{{{ + if s:IsForbidden(a:char) + return a:char + endif let line = getline('.') let col = col('.') - 2 if line[col] == "\\" @@ -418,6 +458,42 @@ function! s:MapMsg(msg) "{{{ echomsg a:msg return "" endfunction "}}} + +function! DelimitMate_ExpandReturn() "{{{ + if DelimitMate_WithinEmptyPair() && + \ b:delimitMate_expand_cr && + \ s:IsForbidden('') == 0 + " Expand: + return "\a\x\\k$\"_xa" + else + return "\" + endif +endfunction "}}} + +function! DelimitMate_ExpandSpace() "{{{ + if DelimitMate_WithinEmptyPair() && + \ b:delimitMate_expand_space && + \ s:IsForbidden('') == 0 + " Expand: + return s:WriteAfter(' ') . "\" + else + return "\" + endif +endfunction "}}} + +function! DelimitMate_BS() " {{{ + let IsF = s:IsForbidden('') + if DelimitMate_WithinEmptyPair() && IsF == 0 + return "\\\" + elseif b:delimitMate_expand_cr && + \ IsF == 0 && + \ (IsCRExpansion() != 0 || IsSpaceExpansion()) + return "\\" + else + return "\" + endif +endfunction " }}} DelimitMate_BS() + "}}} " Mappings: {{{ @@ -435,21 +511,22 @@ function! s:AutoClose() "{{{ while i < len(b:delimitMate_matchpairs_list) let ld = b:delimitMate_left_delims[i] let rd = b:delimitMate_right_delims[i] - exec 'inoremap ' . ld . ' ' . ld . '=JumpIn("' . rd . '")' + "exec 'inoremap ' . ld . ' ' . ld . '=JumpIn("' . rd . '")' + exec 'inoremap ' . ld . ' ' '=JumpIn("' . ld . '")' let i += 1 endwhile + " Exit from inside the matching pair: + for delim in b:delimitMate_right_delims + exec 'inoremap ' . delim . ' =JumpOut("\' . delim . '")' + endfor + " Add matching quote and jump to the midle, or exit if inside a pair of matching quotes: " inoremap " =QuoteDelim("\"") for delim in b:delimitMate_quotes_list exec 'inoremap ' . delim . ' =QuoteDelim("\' . delim . '")' endfor - " Exit from inside the matching pair: - for delim in b:delimitMate_right_delims - exec 'inoremap ' . delim . ' =JumpOut("\' . delim . '")' - endfor - " Try to fix the use of apostrophes (de-activated by default): " inoremap n't n't for map in b:delimitMate_apostrophes_list @@ -480,35 +557,21 @@ function! s:VisualMaps() " {{{ endfor endfunction "}}} -function! DelimitMate_ExpandReturn() "{{{ - " Expand: - return "\a\x\\k$\"_xa" -endfunction "}}} - -function! DelimitMate_ExpandSpace() "{{{ - " Expand: - return s:WriteAfter(' ') . "\" -endfunction "}}} - function! s:ExtraMappings() "{{{ " If pair is empty, delete both delimiters: - inoremap DelimitMate_WithinEmptyPair() ? "\\\" : - \ IsCRExpansion() && b:delimitMate_expand_cr != 0 ? "\\" : - \ IsSpaceExpansion() && b:delimitMate_expand_space != 0 ? "\\" : "\" + inoremap =DelimitMate_BS() " If pair is empty, delete closing delimiter: inoremap DelimitMate_WithinEmptyPair() ? "\" : "\" " Expand return if inside an empty pair: if b:delimitMate_expand_cr != 0 - inoremap DelimitMate_WithinEmptyPair() ? - \ DelimitMate_ExpandReturn() : "\" + inoremap =DelimitMate_ExpandReturn() endif " Expand space if inside an empty pair: if b:delimitMate_expand_space != 0 - inoremap DelimitMate_WithinEmptyPair() ? - \ "\=WriteAfter(' ')\\" : "\" + inoremap =DelimitMate_ExpandSpace() endif " Jump out ot any empty pair: @@ -705,7 +768,6 @@ autocmd FileType * call DelimitMateDo() " Run on new buffers. autocmd BufNewFile,BufRead,BufEnter * if !exists("b:loaded_delimitMate") | call DelimitMateDo() | endif -"function! s:GetSynRegion () | echo synIDattr(synIDtrans(synID(line('.'), col('.'), 1)), 'name') | endfunction "}}} " GetLatestVimScripts: 2754 1 :AutoInstall: delimitMate.vim