== 1.6 ==

Now delimitMate tries to fix the balancing of single quotes when used as apostrophes.
Fixed an error when b:delimitMate_expand_space wasn't set but g:delimitMate_expand_space was.
This commit is contained in:
Israel Chauca Fuentes committed 2009-10-15 23:59:54 -05:00
1 parent 75b4f3b520
commit 6f0c91468a
2 files changed
+105 -53

No files matched your search

+66 -39
View File
@@ -1,9 +1,10 @@
" ============================================================================
" File: delimitMate.vim
" Version: 1.5
" Version: 1.6
" Description: This plugin tries to emulate the auto-completion of delimiters
" that TextMate provides.
" 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
@@ -113,12 +114,19 @@ function! s:Init() "{{{1
if !exists("b:delimitMate_expand_space") && !exists("g:delimitMate_expand_space") " {{{
let s:expand_space = "\<Space>"
elseif b:delimitMate_expand_space == ""
let s:expand_space = "\<Space>"
elseif exists("b:delimitMate_expand_space")
let s:expand_space = b:delimitMate_expand_space
if b:delimitMate_expand_space == ""
let s:expand_space = "\<Space>"
else
let s:expand_space = b:delimitMate_expand_space
endif
else
let s:expand_space = g:delimitMate_expand_space
if g:delimitMate_expand_space == ""
let s:expand_space = "\<Space>"
else
let s:expand_space = g:delimitMate_expand_space
endif
endif " }}}
if !exists("b:delimitMate_expand_cr") && !exists("g:delimitMate_expand_cr") " {{{
@@ -138,24 +146,34 @@ function! s:Init() "{{{1
endif " }}}
if !exists("b:delimitMate_apostrophes") && !exists("g:delimitMate_apostrophes") " {{{
let s:apostrophes = split("n't:'s:'re:'m:'d:'ll:'ve:s'",':')
elseif exists("b:delimitMate_apostrophes")
let s:apostrophes = split(b:delimitMate_apostrophes)
else
let s:apostrophes = split(g:delimitMate_apostrophes)
endif " }}}
let s:matchpairs = split(s:matchpairs_temp, ',')
let s:left_delims = split(s:matchpairs_temp, ':.,\=')
let s:right_delims = split(s:matchpairs_temp, ',\=.:')
let s:VMapMsg = "delimitMate: delimitMate is disabled on blockwise visual mode."
call s:ResetMappings()
call s:UnMap()
if s:autoclose
call s:AutoClose()
else
call s:NoAutoClose()
endif
call s:VisualMaps()
call s:ExtraMappings()
let b:loaded_delimitMate = 1
endfunction "}}}1
function! s:ValidMatchpairs(str) "{{{1
if a:str !~ '^\(.:.\)\+\(,.:.\)*$'
if a:str !~ '^.:.\(,.:.\)*$'
return 0
endif
for pair in split(a:str,',')
@@ -188,15 +206,19 @@ endfunction "}}}1
function! s:SkipDelim(char) "{{{1
let cur = strpart( getline('.'), col('.')-2, 3 )
if cur[0] == "\\"
" Escaped character
return a:char
elseif cur[1] == a:char
" Exit pair
return "\<Right>"
elseif cur[1] == ' ' && cur[2] == a:char
" I'm leaving this in case someone likes it.
" I'm leaving this in case someone likes it. Jump an space and delimiter.
return "\<Right>\<Right>"
elseif s:IsEmptyPair( cur[0] . a:char )
" Add closing delimiter and jump back to the middle.
return a:char . "\<Left>"
else
" Nothing special here, return the same character.
return a:char
endif
endfunction "}}}1
@@ -205,21 +227,23 @@ function! s:QuoteDelim(char) "{{{1
let line = getline('.')
let col = col('.')
if line[col - 2] == "\\"
"Inserting a quoted quotation mark into the string
" Seems like a escaped character, insert a single quotation mark.
return a:char
elseif line[col - 1] == a:char
"Escaping out of the string
" Get out of the string.
return "\<Right>"
else
"Starting a string
" Insert a pair and jump to the middle.
return a:char.a:char."\<Left>"
endif
endfunction "}}}1
function! s:ClosePair(char) "{{{1
if getline('.')[col('.') - 1] == a:char
" Same character on the rigth, jump it.
return "\<Right>"
else
" Insert character.
return a:char
endif
endfunction "}}}1
@@ -244,25 +268,6 @@ function! s:NoAutoClose() "{{{1
for delim in s:right_delims + s:quotes
exec 'inoremap <buffer> ' . delim . ' <C-R>=<SID>SkipDelim("' . escape(delim,'"') . '")<CR>'
endfor
" Wrap the selection with matching pairs, but do nothing if blockwise visual mode is active:
let s:i = 0
while s:i < len(s:matchpairs)
" Map left delimiter:
" vnoremap <buffer> <expr> q( visualmode() == "<C-V>" ? <SID>MapMsg("Message") : "s(\<C-R>\"\<Esc>"
exec 'vnoremap <buffer> <expr> ' . s:visual_leader . s:left_delims[s:i] . ' <SID>IsBlockVisual() ? <SID>MapMsg("' . s:VMapMsg . '") : "s' . s:left_delims[s:i] . '\<C-R>\"' . s:right_delims[s:i] . '\<Esc>:call <SID>RestoreRegister()<CR>"'
" Map right delimiter:
" vnoremap <buffer> <expr> q) visualmode() == "<C-V>" ? <SID>MapMsg("Message") : "s(\<C-R>\""\<Esc>"
exec 'vnoremap <buffer> <expr> ' . s:visual_leader . s:right_delims[s:i] . ' <SID>IsBlockVisual() ? <SID>MapMsg("' . s:VMapMsg . '") : "s' . s:left_delims[s:i] . '\<C-R>\"' . s:right_delims[s:i] . '\<Esc>:call <SID>RestoreRegister()<CR>"'
let s:i += 1
endwhile
" Wrap the selection with matching quotes, but do nothing if blockwise visual mode is active:
for quote in s:quotes
" vnoremap <buffer> <expr> q' visualmode() == "<C-V>" ? <SID>MapMsg("Message") : "s'\<C-R>\"'\<Esc>"
exec 'vnoremap <buffer> <expr> ' . s:visual_leader . quote . ' <SID>IsBlockVisual() ? <SID>MapMsg("' . s:VMapMsg . '") : "s' . escape(quote,'"') .'\<C-R>\"' . escape(quote,'"') . '\<Esc>:call <SID>RestoreRegister()<CR>"'
endfor
endfunction "}}}1
function! s:AutoClose() "{{{1
@@ -286,22 +291,31 @@ function! s:AutoClose() "{{{1
exec 'inoremap <buffer> ' . delim . ' <C-R>=<SID>ClosePair("\' . delim . '")<CR>'
endfor
" Try to fix the use of apostrophes:
" inoremap <buffer> n't n't
for map in s:apostrophes
exec "inoremap <buffer> " . map . " " . map
endfor
endfunction "}}}1
function! s:VisualMaps() " {{{1
" Wrap the selection with matching pairs, but do nothing if blockwise visual mode is active:
let s:i = 0
while s:i < len(s:matchpairs)
" Map left delimiter:
" vnoremap <buffer> <expr> q( visualmode() == "<C-V>" ? <SID>MapMsg("Message") : "s(\<C-R>\"\<Esc>"
" vnoremap <buffer> <expr> \( <SID>IsBlockVisual() ? <SID>MapMsg("Message") : "s(\<C-R>\")\<Esc>:call <SID>RestoreRegister()<CR>"
exec 'vnoremap <buffer> <expr> ' . s:visual_leader . s:left_delims[s:i] . ' <SID>IsBlockVisual() ? <SID>MapMsg("' . s:VMapMsg . '") : "s' . s:left_delims[s:i] . '\<C-R>\"' . s:right_delims[s:i] . '\<Esc>:call <SID>RestoreRegister()<CR>"'
" Map right delimiter:
" vnoremap <buffer> <expr> q) visualmode() == "<C-V>" ? <SID>MapMsg("Message") : "s(\<C-R>\""\<Esc>"
" vnoremap <buffer> <expr> \) <SID>IsBlockVisual() ? <SID>MapMsg("Message") : "s(\<C-R>\")\<Esc>:call <SID>RestoreRegister()<CR>"
exec 'vnoremap <buffer> <expr> ' . s:visual_leader . s:right_delims[s:i] . ' <SID>IsBlockVisual() ? <SID>MapMsg("' . s:VMapMsg . '") : "s' . s:left_delims[s:i] . '\<C-R>\"' . s:right_delims[s:i] . '\<Esc>:call <SID>RestoreRegister()<CR>"'
let s:i += 1
endwhile
" Wrap the selection with matching quotes, but do nothing if blockwise visual mode is active:
for quote in s:quotes
" vnoremap <buffer> <expr> q' visualmode() == "<C-V>" ? <SID>MapMsg("Message") : "s'\<C-R>\"'\<Esc>"
" vnoremap <buffer> <expr> \' <SID>IsBlockVisual() ? <SID>MapMsg("Message") : "s'\<C-R>\"'\<Esc>:call <SID>RestoreRegister()<CR>"
exec 'vnoremap <buffer> <expr> ' . s:visual_leader . quote . ' <SID>IsBlockVisual() ? <SID>MapMsg("' . s:VMapMsg . '") : "s' . escape(quote,'"') .'\<C-R>\"' . escape(quote,'"') . '\<Esc>:call <SID>RestoreRegister()<CR>"'
endfor
endfunction "}}}1
@@ -310,33 +324,39 @@ function! s:IsBlockVisual() " {{{1
if visualmode() == "<C-V>"
return 1
endif
" Store unnamed register values for later use in s:RestoreRegister().
let s:save_reg = getreg('"')
echomsg s:save_reg
let s:save_reg_mode = getregtype('"')
echomsg s:save_reg_mode
if len(getline('.')) == 0
" This for proper wrap of empty lines.
let @" = "\n"
endif
return 0
endfunction " }}}1
function! s:RestoreRegister() " {{{1
" Restore unnamed register values store in s:IsBlockVisual().
call setreg('"', s:save_reg, s:save_reg_mode)
echo ""
endfunction " }}}1
function! s:ExpandReturn() "{{{1
if s:WithinEmptyPair()
" Expand:
return s:expand_return
else
" Don't
return "\<CR>"
endif
endfunction "}}}1
function! s:ExpandSpace() "{{{1
if s:WithinEmptyPair()
" Expand:
return s:expand_space
else
" Don't
return "\<Space>"
endif
endfunction "}}}1
@@ -346,10 +366,14 @@ function! s:ExtraMappings() "{{{1
inoremap <buffer> <expr> <BS> <SID>WithinEmptyPair() ? "\<Right>\<BS>\<BS>" : "\<BS>"
" Expand return if inside an empty pair:
inoremap <buffer> <CR> <C-R>=<SID>ExpandReturn()<CR>
if exists("b:delimitMate_expand_cr") || exists("g:delimitMate_expand_cr")
inoremap <buffer> <CR> <C-R>=<SID>ExpandReturn()<CR>
endif
" Expand space if inside an empty pair:
inoremap <buffer> <Space> <C-R>=<SID>ExpandSpace()<CR>
if exists("b:delimitMate_expand_space") || exists("g:delimitMate_expand_space")
inoremap <buffer> <Space> <C-R>=<SID>ExpandSpace()<CR>
endif
endfunction "}}}1
function! s:TestMappings() "{{{1
@@ -436,6 +460,9 @@ function! s:UnMap() " {{{
"echomsg 'iunmap <buffer> ' . char
endif
endfor
for map in s:apostrophes
exec "silent! iunmap <buffer> " . map
endfor
" Visual Mappings:
for char in s:right_delims + s:left_delims + s:quotes
@@ -476,6 +503,7 @@ endfunction "}}}1
function! s:DelimitMateDo() "{{{1
if exists("g:delimitMate_excluded_ft")
" Check if this file type is excluded:
for ft in split(g:delimitMate_excluded_ft,',')
if ft ==? &filetype
if !exists("s:quotes")
@@ -495,10 +523,9 @@ function! s:DelimitMateDo() "{{{1
finally
let &cpo = save_cpo
endtry
let &cpo = save_cpo
endfunction "}}}1
" Do the real work: {{{1
" Set some commands: {{{1
call s:DelimitMateDo()
" Let me refresh without re-loading the buffer: