From b65d719353bb4c358299119cced0bf00f69617bb Mon Sep 17 00:00:00 2001 From: Israel Chauca Fuentes Date: Sat, 14 Apr 2012 19:35:59 -0400 Subject: [PATCH] Add string next to the closing matchpair when at the eol. Closes #77. --- autoload/delimitMate.vim | 43 +++++++++++++++++++++-------------- doc/delimitMate.txt | 16 ++++++++++++- plugin/delimitMate.vim | 3 +++ test/autoclose_matchpairs.txt | 8 +++++++ test/expand_cr.txt | 6 +++++ 5 files changed, 58 insertions(+), 18 deletions(-) diff --git a/autoload/delimitMate.vim b/autoload/delimitMate.vim index b3722db..c0400f7 100644 --- a/autoload/delimitMate.vim +++ b/autoload/delimitMate.vim @@ -194,8 +194,8 @@ function! delimitMate#FlushBuffer() " {{{ return '' endfunction " }}} -function! delimitMate#AddToBuffer(c) "{{{ - call insert(b:_l_delimitMate_buffer, a:c) +function! delimitMate#AddToBuffer(str) "{{{ + call insert(b:_l_delimitMate_buffer, a:str) endfunction "delimitMate#AddToBuffer }}} function! delimitMate#BalancedParens(char) "{{{ @@ -300,6 +300,7 @@ function! delimitMate#ParenDelim(char) " {{{ endif let line = getline('.') let col = col('.')-2 + let tail = len(line) == (col + 1) ? b:_l_delimitMate_eol_marker : '' let left = b:_l_delimitMate_left_delims[index(b:_l_delimitMate_right_delims,a:char)] let smart_matchpairs = substitute(b:_l_delimitMate_smart_matchpairs, '\\!', left, 'g') let smart_matchpairs = substitute(smart_matchpairs, '\\#', a:char, 'g') @@ -312,8 +313,8 @@ function! delimitMate#ParenDelim(char) " {{{ call delimitMate#AddToBuffer(a:char) else "echom string(col).':'.line[:(col)].'|'.line[(col+1):] - call setline('.',line[:(col)].a:char.line[(col+1):]) - call delimitMate#AddToBuffer(a:char) + call setline('.',line[:(col)].a:char.tail.line[(col+1):]) + call delimitMate#AddToBuffer(a:char . tail) endif return '' endfunction " }}} @@ -439,21 +440,29 @@ endfunction "}}} function! delimitMate#BS() " {{{ if delimitMate#IsForbidden("") - return "\" + let extra = '' + elseif &backspace !~ 'start\|2' && empty(b:_l_delimitMate_buffer) + let extra = '' + elseif delimitMate#WithinEmptyPair() + let extra = delimitMate#Del() + elseif delimitMate#IsSpaceExpansion() + let extra = delimitMate#Del() + elseif delimitMate#IsCRExpansion() + let extra = repeat("\", len(matchstr(getline(line('.') + 1), '^\s*\S'))) + else + let extra = '' endif - if &backspace !~ 'start\|2' && empty(b:_l_delimitMate_buffer) - return "\" + if search('\m\C\%#\%(' + \ . join(b:_l_delimitMate_right_delims, '\|') + \ . '\)' + \ . escape(b:_l_delimitMate_eol_marker, '\*.') + \ . '$', + \ 'cWn') + for c in range(len(split(b:_l_delimitMate_eol_marker, '\zs'))) + let extra .= delimitMate#Del() + endfor endif - if delimitMate#WithinEmptyPair() - return "\" . delimitMate#Del() - endif - if delimitMate#IsSpaceExpansion() - return "\" . delimitMate#Del() - endif - if delimitMate#IsCRExpansion() - return "\" . repeat("\", len(matchstr(getline(line('.') + 1), '^\s*\S'))) - endif - return "\" + return "\" . extra endfunction " }}} delimitMate#BS() function! delimitMate#Del() " {{{ diff --git a/doc/delimitMate.txt b/doc/delimitMate.txt index 679f573..22dcaca 100644 --- a/doc/delimitMate.txt +++ b/doc/delimitMate.txt @@ -103,6 +103,10 @@ specific file types, see |delimitMateOptionDetails| for examples. |'delimitMate_excluded_ft'| Turns off the script for the given file types. +|'delimitMate_eol_marker'| Determines what to insert after the closing + matchpair when typing an opening matchpair on + the end of the line. + |'delimitMate_apostrophes'| Tells delimitMate how it should "fix" balancing of single quotes when used as apostrophes. NOTE: Not needed any more, kept @@ -269,6 +273,16 @@ only if you don't want any of the features it provides on those file types. e.g.: > let delimitMate_excluded_ft = "mail,txt" < +------------------------------------------------------------------------------ + *'delimitMate_eol_marker'* +Values: String. ~ +Default: Empty. ~ + +The contents of this string will be inserted after the closing matchpair when +the opening matchapair is inserted at the end of the line. +e.g.: > + au FileType c,perl let b:delimitMate_eol_marker = ";" +< ------------------------------------------------------------------------------ *'delimitMate_apostrophes'* Values: Strings separated by ":". ~ @@ -824,4 +838,4 @@ _____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|____ ~ __|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_ ~ _____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|____ ~ -vim:tw=78:et:ts=2:sw=2:ft=help:norl:formatoptions+=tcroqn:autoindent: +vim:tw=78:et:ts=8:sw=2:ft=help:norl:formatoptions+=tcroqn:autoindent: diff --git a/plugin/delimitMate.vim b/plugin/delimitMate.vim index 4f72619..191698e 100644 --- a/plugin/delimitMate.vim +++ b/plugin/delimitMate.vim @@ -129,6 +129,9 @@ function! s:init() "{{{ " balance_matchpairs call s:option_init("balance_matchpairs", 0) + " eol marker + call s:option_init("eol_marker", "") + let b:_l_delimitMate_buffer = [] endfunction "}}} Init() diff --git a/test/autoclose_matchpairs.txt b/test/autoclose_matchpairs.txt index da2a1a1..5361af3 100644 --- a/test/autoclose_matchpairs.txt +++ b/test/autoclose_matchpairs.txt @@ -24,3 +24,11 @@ let g:delimitMate_autoclose = 1 # Handle backspace gracefully. set backspace= "(\a\x" "(x)" + +# Add semicolon next to the closing paren. Issue #77. +new +let b:delimitMate_eol_marker = ';' +"abc(x" "abc(x);" +%d +# BS should behave accordingly. +"abc(\" "abc" diff --git a/test/expand_cr.txt b/test/expand_cr.txt index ffbb883..331add6 100644 --- a/test/expand_cr.txt +++ b/test/expand_cr.txt @@ -42,3 +42,9 @@ sub foo { }x } -------------------------------------------------------------------------------- +%d +call setline(1, "\"{bracketed}") +normal A"x +================================================================================ +"{bracketed}"x +--------------------------------------------------------------------------------