" ============================================================================ " File: delimitMate.vim " Version: 2.0 " Description: This plugin tries to emulate the auto-completion of delimiters " that TextMate provides. " 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") "{{{ " User doesn't want this plugin, let's get out! finish endif let g:loaded_delimitMate = 1 if exists("s:loaded_delimitMate") && !exists("g:delimitMate_testing") " Don't define the functions if they already exist: just do the work " (unless we are testing): call s:DelimitMateDo() finish endif if v:version < 700 echoerr "delimitMate: this plugin requires vim >= 7!" finish endif let s:loaded_delimitMate = 1 " }}} let delimitMate_version = '2.0' function! s:Init() "{{{ let b:loaded_delimitMate = 1 " delimitMate_autoclose {{{ if !exists("b:delimitMate_autoclose") && !exists("g:delimitMate_autoclose") let b:delimitMate_autoclose = 1 elseif !exists("b:delimitMate_autoclose") && exists("g:delimitMate_autoclose") let b:delimitMate_autoclose = g:delimitMate_autoclose else " Nothing to do. endif " }}} " delimitMate_matchpairs {{{ if !exists("b:delimitMate_matchpairs") && !exists("g:delimitMate_matchpairs") if s:ValidMatchpairs(&matchpairs) == 1 let s:matchpairs_temp = &matchpairs else echoerr "delimitMate: There seems to be a problem with 'matchpairs', read ':help matchpairs' and fix it or notify the maintainer of this script if this is a bug." finish endif elseif exists("b:delimitMate_matchpairs") if s:ValidMatchpairs(b:delimitMate_matchpairs) || b:delimitMate_matchpairs == "" let s:matchpairs_temp = b:delimitMate_matchpairs else echoerr "delimitMate: Invalid format in 'b:delimitMate_matchpairs', falling back to matchpairs. Fix the error and use the command :DelimitMateReload to try again." if s:ValidMatchpairs(&matchpairs) == 1 let s:matchpairs_temp = &matchpairs else echoerr "delimitMate: There seems to be a problem with 'matchpairs', read ':help matchpairs' and fix it or notify the maintainer of this script if this is a bug." let s:matchpairs_temp = "" endif endif else if s:ValidMatchpairs(g:delimitMate_matchpairs) || g:delimitMate_matchpairs == "" let s:matchpairs_temp = g:delimitMate_matchpairs else echoerr "delimitMate: Invalid format in 'g:delimitMate_matchpairs', falling back to matchpairs. Fix the error and use the command :DelimitMateReload to try again." if s:ValidMatchpairs(&matchpairs) == 1 let s:matchpairs_temp = &matchpairs else echoerr "delimitMate: There seems to be a problem with 'matchpairs', read ':help matchpairs' and fix it or notify the maintainer of this script if this is a bug." let s:matchpairs_temp = "" endif endif endif " }}} " delimitMate_quotes {{{ if exists("b:delimitMate_quotes") if b:delimitMate_quotes =~ '^\(\S\)\(\s\S\)*$' || b:delimitMate_quotes == "" let s:quotes = split(b:delimitMate_quotes) else let s:quotes = split("\" ' `") echoerr "delimitMate: There is a problem with the format of 'b:delimitMate_quotes', it should be a string of single characters separated by spaces. Falling back to default values." endif elseif exists("g:delimitMate_quotes") if g:delimitMate_quotes =~ '^\(\S\)\(\s\S\)*$' || g:delimitMate_quotes == "" let s:quotes = split(g:delimitMate_quotes) else let s:quotes = split("\" ' `") echoerr "delimitMate: There is a problem with the format of 'g:delimitMate_quotes', it should be a string of single characters separated by spaces. Falling back to default values." endif else let s:quotes = split("\" ' `") endif let b:delimitMate_quotes_list = s:quotes " }}} " delimitMate_visual_leader {{{ if !exists("b:delimitMate_visual_leader") && !exists("g:delimitMate_visual_leader") let b:delimitMate_visual_leader = exists('b:maplocalleader') ? b:maplocalleader : \ exists('g:mapleader') ? g:mapleader : "\\" elseif !exists("b:delimitMate_visual_leader") && exists("g:delimitMate_visual_leader") let b:delimitMate_visual_leader = g:delimitMate_visual_leader else " Nothing to do. endif " }}} " delimitMate_expand_space {{{ if !exists("b:delimitMate_expand_space") && !exists("g:delimitMate_expand_space") let b:delimitMate_expand_space = 0 elseif !exists("b:delimitMate_expand_space") && !exists("g:delimitMate_expand_space") let b:delimitMate_expand_space = g:delimitMate_expand_space else " Nothing to do. endif " }}} " delimitMate_expand_cr {{{ if !exists("b:delimitMate_expand_cr") && !exists("g:delimitMate_expand_cr") let b:delimitMate_expand_cr = 0 elseif !exists("b:delimitMate_expand_cr") && exists("g:delimitMate_expand_cr") let b:delimitMate_expand_cr = g:delimitMate_expand_cr else " Nothing to do. endif " }}} " delimitMate_smart_quotes {{{ if !exists("b:delimitMate_smart_quotes") && !exists("g:delimitMate_smart_quotes") let b:delimitMate_smart_quotes = 1 elseif !exists("b:delimitMate_smart_quotes") && exists("g:delimitMate_smart_quotes") let b:delimitMate_smart_quotes = split(g:delimitMate_smart_quotes) else " Nothing to do. endif " }}} " delimitMate_apostrophes {{{ if !exists("b:delimitMate_apostrophes") && !exists("g:delimitMate_apostrophes") "let s:apostrophes = split("n't:'s:'re:'m:'d:'ll:'ve:s'",':') let s:apostrophes = [] elseif !exists("b:delimitMate_apostrophes") && exists("g:delimitMate_apostrophes") let s:apostrophes = split(g:delimitMate_apostrophes) else let s:apostrophes = split(b:delimitMate_apostrophes) endif let b:delimitMate_apostrophes_list = s:apostrophes " }}} " delimitMate_tab2exit {{{ if !exists("b:delimitMate_tab2exit") && !exists("g:delimitMate_tab2exit") let b:delimitMate_tab2exit = 1 elseif !exists("b:delimitMate_tab2exit") && exists("g:delimitMate_tab2exit") let b:delimitMate_tab2exit = g:delimitMate_tab2exit else " Nothing to do. endif " }}} let b:delimitMate_matchpairs_list = split(s:matchpairs_temp, ',') let b:delimitMate_left_delims = split(s:matchpairs_temp, ':.,\=') let b:delimitMate_right_delims = split(s:matchpairs_temp, ',\=.:') let s:VMapMsg = "delimitMate: delimitMate is disabled on blockwise visual mode." call s:UnMap() if b:delimitMate_autoclose call s:AutoClose() else call s:NoAutoClose() endif call s:VisualMaps() call s:ExtraMappings() endfunction "}}} Init() "}}} " Utilities: {{{ function! s:ValidMatchpairs(str) "{{{ if a:str !~ '^.:.\(,.:.\)*$' return 0 endif for pair in split(a:str,',') if strpart(pair, 0, 1) == strpart(pair, 2, 1) || strlen(pair) != 3 return 0 endif endfor return 1 endfunction "}}} function! DelimitMate_ShouldJump() "{{{ let char = getline('.')[col('.') - 1] for pair in b:delimitMate_matchpairs_list if char == split( pair, ':' )[1] " Same character on the rigth, jump over it. return 1 endif endfor for quote in b:delimitMate_quotes_list if char == quote " Same character on the rigth, jump over it. return 1 endif endfor return 0 endfunction "}}} function! s:IsBlockVisual() " {{{ if visualmode() == "" return 1 endif " Store unnamed register values for later use in s:RestoreRegister(). let s:save_reg = getreg('"') let s:save_reg_mode = getregtype('"') if len(getline('.')) == 0 " This for proper wrap of empty lines. let @" = "\n" endif return 0 endfunction " }}} function! s:IsEmptyPair(str) "{{{ for pair in b:delimitMate_matchpairs_list if a:str == join( split( pair, ':' ),'' ) return 1 endif endfor for quote in b:delimitMate_quotes_list if a:str == quote . quote return 1 endif endfor return 0 endfunction "}}} function! DelimitMate_WithinEmptyPair() "{{{ let cur = strpart( getline('.'), col('.')-2, 2 ) return s:IsEmptyPair( cur ) endfunction "}}} function! s:WriteBefore(str) "{{{ let len = len(a:str) let line = getline('.') let col = col('.')-2 if col < 0 call setline('.',line[(col+len+1):]) else call setline('.',line[:(col)].line[(col+len+1):]) endif return a:str endfunction " }}} function! s:WriteAfter(str) "{{{ let len = len(a:str) let line = getline('.') let col = col('.')-2 if (col) < 0 call setline('.',a:str.line) else call setline('.',line[:(col)].a:str.line[(col+len):]) endif return '' endfunction " }}} function! s:RestoreRegister() " {{{ " Restore unnamed register values store in s:IsBlockVisual(). call setreg('"', s:save_reg, s:save_reg_mode) echo "" 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 '' endfunction " }}} function! s:JumpOut(char) "{{{ let line = getline('.') let col = col('.')-2 if line[col+1] == a:char call setline('.',line[:(col)].line[(col+2):]) endif return a:char endfunction " }}} function! DelimitMate_JumpAny() " {{{ let nchar = getline('.')[col('.')-1] return nchar . "\" endfunction " DelimitMate_JumpAny() }}} function! s:SkipDelim(char) "{{{ let cur = strpart( getline('.'), col('.')-2, 3 ) if cur[0] == "\\" " Escaped character return a:char elseif cur[1] == a:char " Exit pair return s:WriteBefore(a:char) "elseif cur[1] == ' ' && cur[2] == a:char "" I'm leaving this in case someone likes it. Jump an space and delimiter. "return "\\" elseif s:IsEmptyPair( cur[0] . a:char ) " Add closing delimiter and jump back to the middle. return s:WriteAfter(a:char) else " Nothing special here, return the same character. return a:char endif endfunction "}}} function! s:QuoteDelim(char) "{{{ let line = getline('.') let col = col('.') - 2 if line[col] == "\\" " Seems like a escaped character, insert one quotation mark. return a:char elseif line[col + 1] == a:char " Get out of the string. return s:WriteBefore(a:char) elseif (line[col] =~ '[a-zA-Z0-9]' && a:char == "'") || \(line[col] =~ '[a-zA-Z0-9]' && b:delimitMate_smart_quotes) " Seems like an apostrophe or a closing, insert a single quote. return a:char elseif (line[col] == a:char && line[col + 1 ] != a:char) && b:delimitMate_smart_quotes " Seems like we have an unbalanced quote, insert one quotation mark and jump to the middle. return s:WriteAfter(a:char) else " Insert a pair and jump to the middle. call s:WriteAfter(a:char) return a:char endif endfunction "}}} function! s:MapMsg(msg) "{{{ redraw echomsg a:msg return "" endfunction "}}} "}}} " Mappings: {{{ function! s:NoAutoClose() "{{{ " inoremap ) =SkipDelim('\)') for delim in b:delimitMate_right_delims + b:delimitMate_quotes_list exec 'inoremap ' . delim . ' =SkipDelim("' . escape(delim,'"') . '")' endfor endfunction "}}} function! s:AutoClose() "{{{ " Add matching pair and jump to the midle: " inoremap ( () let i = 0 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 . '")' let i += 1 endwhile " 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 exec "inoremap " . map . " " . map endfor endfunction "}}} function! s:VisualMaps() " {{{ let vleader = b:delimitMate_visual_leader " Wrap the selection with matching pairs, but do nothing if blockwise visual mode is active: let i = 0 while i < len(b:delimitMate_matchpairs_list) " Map left delimiter: let ld = b:delimitMate_left_delims[i] let rd = b:delimitMate_right_delims[i] exec 'vnoremap ' . vleader . ld . ' IsBlockVisual() ? MapMsg("' . s:VMapMsg . '") : "s' . ld . '\\"' . rd . '\:call RestoreRegister()"' " Map right delimiter: exec 'vnoremap ' . vleader . rd . ' IsBlockVisual() ? MapMsg("' . s:VMapMsg . '") : "s' . ld . '\\"' . rd . '\:call RestoreRegister()"' let i += 1 endwhile " Wrap the selection with matching quotes, but do nothing if blockwise visual mode is active: for quote in b:delimitMate_quotes_list " vnoremap \' IsBlockVisual() ? MapMsg("Message") : "s'\\"'\:call RestoreRegister()" exec 'vnoremap ' . vleader . quote . ' IsBlockVisual() ? MapMsg("' . s:VMapMsg . '") : "s' . escape(quote,'"') .'\\"' . escape(quote,'"') . '\:call RestoreRegister()"' 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() ? "\\\" : "\" " 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() : "\" endif " Expand space if inside an empty pair: if b:delimitMate_expand_space != 0 inoremap DelimitMate_WithinEmptyPair() ? \ DelimitMate_ExpandSpace() : "\" endif " Jump out ot any empty pair: if b:delimitMate_tab2exit inoremap DelimitMate_ShouldJump() ? DelimitMate_JumpAny() : "\" endif endfunction "}}} "}}} " Tools: {{{ function! s:TestMappings() "{{{ if b:delimitMate_autoclose exec "normal i* AUTOCLOSE:\" for i in range(len(b:delimitMate_left_delims)) exec "normal GGAOpen & close: " . b:delimitMate_left_delims[i]. "|" exec "normal A\Delete: " . b:delimitMate_left_delims[i] . "\|" exec "normal A\Exit: " . b:delimitMate_left_delims[i] . b:delimitMate_right_delims[i] . "|" exec "normal A\Space: " . b:delimitMate_left_delims[i] . " |" exec "normal GGA\Visual-L: v\v" . b:delimitMate_visual_leader . b:delimitMate_left_delims[i] exec "normal A\Visual-R: v\v" . b:delimitMate_visual_leader . b:delimitMate_right_delims[i] exec "normal A\Car return: " . b:delimitMate_left_delims[i] . "\|\GGA\\" endfor for i in range(len(b:delimitMate_quotes_list)) exec "normal GGAOpen & close: " . b:delimitMate_quotes_list[i] . "|" exec "normal A\Delete: " exec "normal A". b:delimitMate_quotes_list[i] exec "normal a\|" exec "normal A\Exit: " . b:delimitMate_quotes_list[i] . b:delimitMate_quotes_list[i] . "|" exec "normal A\Space: " . b:delimitMate_quotes_list[i] . " |" exec "normal GGA\Visual: v\v" . b:delimitMate_visual_leader . b:delimitMate_quotes_list[i] exec "normal A\Car return: " . b:delimitMate_quotes_list[i] . "\|\GGA\\" endfor else exec "normal i* NO AUTOCLOSE:\" for i in range(len(b:delimitMate_left_delims)) exec "normal GGAOpen & close: " . b:delimitMate_left_delims[i] . b:delimitMate_right_delims[i] . "|" exec "normal A\Delete: " . b:delimitMate_left_delims[i] . b:delimitMate_right_delims[i] . "\|" exec "normal A\Exit: " . b:delimitMate_left_delims[i] . b:delimitMate_right_delims[i] . b:delimitMate_right_delims[i] . "|" exec "normal A\Space: " . b:delimitMate_left_delims[i] . b:delimitMate_right_delims[i] . " |" exec "normal GGA\Visual-L: v\v" . b:delimitMate_visual_leader . b:delimitMate_left_delims[i] exec "normal A\Visual-R: v\v" . b:delimitMate_visual_leader . b:delimitMate_right_delims[i] exec "normal A\Car return: " . b:delimitMate_left_delims[i] . b:delimitMate_right_delims[i] . "\|\GGA\\" endfor for i in range(len(b:delimitMate_quotes_list)) exec "normal GGAOpen & close: " . b:delimitMate_quotes_list[i] . b:delimitMate_quotes_list[i] . "|" exec "normal A\Delete: " . b:delimitMate_quotes_list[i] . b:delimitMate_quotes_list[i] . "\|" exec "normal A\Exit: " . b:delimitMate_quotes_list[i] . b:delimitMate_quotes_list[i] . b:delimitMate_quotes_list[i] . "|" exec "normal A\Space: " . b:delimitMate_quotes_list[i] . b:delimitMate_quotes_list[i] . " |" exec "normal GGA\Visual: v\v" . b:delimitMate_visual_leader . b:delimitMate_quotes_list[i] exec "normal A\Car return: " . b:delimitMate_quotes_list[i] . b:delimitMate_quotes_list[i] . "\|\GGA\\" endfor endif exec "normal \i" endfunction "}}} function! s:SwitchAutoclose() "{{{ if !exists("g:delimitMate_autoclose") let g:delimitMate_autoclose = 1 elseif g:delimitMate_autoclose == 1 let g:delimitMate_autoclose = 0 else let g:delimitMate_autoclose = 1 endif DelimitMateReload endfunction "}}} function! s:UnMap() " {{{ " No Autoclose Mappings: for char in b:delimitMate_right_delims + b:delimitMate_quotes_list if maparg(char,"i") =~? 'SkipDelim' exec 'silent! iunmap ' . char "echomsg 'iunmap ' . char endif endfor " Autoclose Mappings: let i = 0 let l = len(b:delimitMate_matchpairs_list) while i < l if maparg(b:delimitMate_left_delims[i],"i") =~? 'JumpIn' exec 'silent! iunmap ' . b:delimitMate_left_delims[i] "echomsg 'iunmap ' . b:delimitMate_left_delims[i] endif let i += 1 endwhile for char in b:delimitMate_quotes_list if maparg(char, "i") =~? 'QuoteDelim' exec 'silent! iunmap ' . char "echomsg 'iunmap ' . char endif endfor for char in b:delimitMate_right_delims if maparg(char, "i") =~? 'JumpOut' exec 'silent! iunmap ' . char "echomsg 'iunmap ' . char endif endfor for map in b:delimitMate_apostrophes_list exec "silent! iunmap " . map endfor " Visual Mappings: for char in b:delimitMate_right_delims + b:delimitMate_left_delims + b:delimitMate_quotes_list if maparg(b:delimitMate_visual_leader . char,"v") =~? 'IsBlock' exec 'silent! vunmap ' . b:delimitMate_visual_leader . char "echomsg 'vunmap ' . b:delimitMate_visual_leader . char endif endfor " Expansion Mappings: if maparg('', "i") =~? 'WithinEmptyPair' silent! iunmap "echomsg "silent! iunmap " endif if maparg('', "i") =~? 'WithinEmptyPair' silent! iunmap "echomsg "silent! iunmap " endif if maparg('',"i") =~? 'DelimitMate_ExpandReturn' silent! iunmap "echomsg "silent! iunmap " endif if maparg('',"i") =~? 'DelimitMate_ExpandSpace' silent! iunmap "echomsg "silent! iunmap " endif if maparg('', "i") =~? 'ShouldJump' silent! iunmap "echomsg "silent! iunmap " endif endfunction " }}} s:ExtraMappings() function! s:TestMappingsDo() "{{{ "DelimitMateReload if !exists("g:delimitMate_testing") "call s:DelimitMateDo() call s:TestMappings() else call s:SwitchAutoclose() call s:TestMappings() exec "normal i\" call s:SwitchAutoclose() call s:TestMappings() endif endfunction "}}} function! s:DelimitMateDo() "{{{ 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("b:delimitMate_quotes_list") return 1 endif "echomsg "excluded" call s:UnMap() return 1 endif endfor endif try "echomsg "included" let save_cpo = &cpo set cpo&vim call s:Init() finally let &cpo = save_cpo endtry endfunction "}}} "}}} " Commands: {{{ call s:DelimitMateDo() " Let me refresh without re-loading the buffer: command! DelimitMateReload call s:DelimitMateDo() " Quick test: command! DelimitMateTest call s:TestMappingsDo() " Run on file type events. "autocmd VimEnter * autocmd FileType * call DelimitMateDo() 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 " vim:foldmethod=marker:foldcolumn=4