From 5ef40e0234407a3ef460073afe39e7993fd9b776 Mon Sep 17 00:00:00 2001 From: Israel Chauca Fuentes Date: Tue, 24 Dec 2013 02:48:35 -0500 Subject: [PATCH] Do not expand cr and space inside quotes by default. Close #153. - Add delimitMate_expand_inside_quotes. - Add tests. --- autoload/delimitMate.vim | 37 +++++++++++++++++++++++++++++++++---- doc/delimitMate.txt | 37 +++++++++++++++++++++++++++++-------- plugin/delimitMate.vim | 5 ++++- test/autoclose_quotes.txt | 2 ++ test/expand_cr.txt | 19 +++++++++++++++++++ 5 files changed, 87 insertions(+), 13 deletions(-) diff --git a/autoload/delimitMate.vim b/autoload/delimitMate.vim index ac04233..c7c490d 100644 --- a/autoload/delimitMate.vim +++ b/autoload/delimitMate.vim @@ -192,6 +192,27 @@ function! delimitMate#WithinEmptyPair() "{{{ return delimitMate#IsEmptyPair( char1.char2 ) endfunction "}}} +function! delimitMate#WithinEmptyMatchpair() "{{{ + " get char before the cursor. + let open = delimitMate#GetCharFromCursor(-1) + let idx = index(s:g('left_delims'), open) + if idx == -1 + return 0 + endif + let close = get(s:g('right_delims'), idx, '') + return close ==# delimitMate#GetCharFromCursor(0) +endfunction "}}} + +function! delimitMate#WithinEmptyQuotes() "{{{ + " get char before the cursor. + let quote = delimitMate#GetCharFromCursor(-1) + let idx = index(s:g('quotes_list'), quote) + if idx == -1 + return 0 + endif + return quote ==# delimitMate#GetCharFromCursor(0) +endfunction "}}} + function! delimitMate#CursorIdx() "{{{ let idx = len(split(getline('.')[: col('.') - 1], '\zs')) - 1 return idx @@ -448,10 +469,15 @@ function! delimitMate#ExpandReturn() "{{{ if delimitMate#IsForbidden("") return "\" endif - if delimitMate#WithinEmptyPair() + let escaped = delimitMate#CursorIdx() >= 2 + \ && delimitMate#GetCharFromCursor(-2) == '\' + if delimitMate#WithinEmptyMatchpair() + \ || (s:g('expand_cr') == 2 + \ && index(s:g('right_delims'), delimitMate#GetCharFromCursor(0)) > -1) + \ || (s:g('expand_inside_quotes') + \ && delimitMate#WithinEmptyQuotes() + \ && !escaped) " Expand: - " Not sure why I used the previous combos, but I'm sure somebody will - " tell me about it. " XXX zv prevents breaking expansion with syntax folding enabled by " InsertLeave. return "\a\\zvO" @@ -466,7 +492,10 @@ function! delimitMate#ExpandSpace() "{{{ endif let escaped = delimitMate#CursorIdx() >= 2 \ && delimitMate#GetCharFromCursor(-2) == '\' - if delimitMate#WithinEmptyPair() && !escaped + if delimitMate#WithinEmptyMatchpair() + \ || (s:g('expand_inside_quotes') + \ && delimitMate#WithinEmptyQuotes() + \ && !escaped) " Expand: return "\\\" else diff --git a/doc/delimitMate.txt b/doc/delimitMate.txt index a556520..82ebd67 100644 --- a/doc/delimitMate.txt +++ b/doc/delimitMate.txt @@ -193,7 +193,7 @@ e.g.: > ------------------------------------------------------------------------------ *'delimitMate_expand_cr'* *'b:delimitMate_expand_cr'* -Values: 1 or 0 ~ +Values: 0, 1 or 2 ~ Default: 0 ~ This option turns on/off the expansion of . Read |delimitMateExpansion| @@ -214,6 +214,18 @@ e.g.: > let delimitMate_expand_space = 1 au FileType tcl let b:delimitMate_expand_space = 1 < +------------------------------------------------------------------------------ + *'delimitMate_expand_inside_quotes'* + *'b:delimitMate_expand_inside_quotes'* +Values: 1 or 0 ~ +Default: 0 ~ +When this option is set to 1 the expansion of space and cr will also be +applied to quotes. Read |delimitMateExpansion| for details. + +e.g.: > + let delimitMate_expand_inside_quotes = 1 + au FileType mail let b:delimitMate_expand_inside_quotes = 1 +< ------------------------------------------------------------------------------ *'delimitMate_jump_expansion'* *'b:delimitMate_jump_expansion'* @@ -369,21 +381,21 @@ you should use (read |i_CTRL-]|) to expand them on the go. ------------------------------------------------------------------------------ 3.2 EXPANSION OF SPACE AND CAR RETURN *delimitMateExpansion* -When the cursor is inside an empty pair of delimiters, and can be +When the cursor is inside an empty pair of any matchpair, and can be expanded, see |'delimitMate_expand_space'| and |'delimitMate_expand_cr'|: Expand to: > - | You get - ==================================== - (|) | ( | ) + You start with | You get + ============================== + (|) | ( | ) < Expand to: > - | You get - ============================ - (|) | ( + You start with | You get + ============================== + (|) | ( | | | ) < @@ -393,6 +405,15 @@ closing paren/bracket/etc. on the next line, delimitMate will make the cursor jump over any whitespace/ and place it after the existing closing delimiter instead of inserting a new one. +When |'delimitMate_expand_cr'| is set to 2, the following will also happen: > + + You start with | You get + ============================== + (foo|) | (foo + | | + | ) +< + Since and are used everywhere, I have made the functions involved in expansions global, so they can be used to make custom mappings. Read |delimitMateFunctions| for more details. diff --git a/plugin/delimitMate.vim b/plugin/delimitMate.vim index aded089..72f29ef 100644 --- a/plugin/delimitMate.vim +++ b/plugin/delimitMate.vim @@ -108,6 +108,9 @@ function! s:init() "{{{ endif call s:option_init("expand_cr", 0) + " expand_in_quotes + call s:option_init('expand_inside_quotes', 0) + " jump_expansion call s:option_init("jump_expansion", 0) @@ -395,4 +398,4 @@ augroup END let &cpo = save_cpo " GetLatestVimScripts: 2754 1 :AutoInstall: delimitMate.vim -" vim:foldmethod=marker:foldcolumn=4 +" vim:foldmethod=marker:foldcolumn=4:ts=2:sw=2 diff --git a/test/autoclose_quotes.txt b/test/autoclose_quotes.txt index 25f075c..ef2ce0b 100644 --- a/test/autoclose_quotes.txt +++ b/test/autoclose_quotes.txt @@ -22,6 +22,8 @@ let g:delimitMate_autoclose = 0 "@#\''x" "@'x'#" let g:delimitMate_expand_space = 1 let g:delimitMate_autoclose = 1 +"'\x" "' x'" +let g:delimitMate_expand_inside_quotes = 1 "'\x" "' x '" "'\\x" "'x'" "abc\\''\x" "abc\\' x'" diff --git a/test/expand_cr.txt b/test/expand_cr.txt index cbb6aac..7f406dd 100644 --- a/test/expand_cr.txt +++ b/test/expand_cr.txt @@ -65,3 +65,22 @@ abc { x } -------------------------------------------------------------------------------- +# expand_cr != 2 +%d_ +call setline(1, 'abc(def)') +exec "normal $i\x" +================================================================================ +abc(def + x) +-------------------------------------------------------------------------------- +# expand_cr == 2 +%d_ +let delimitMate_expand_cr = 2 +DelimitMateReload +call setline(1, 'abc(def)') +exec "normal $i\x" +================================================================================ +abc(def + x + ) +--------------------------------------------------------------------------------