From 70e1ac1462ba71c0e0aff0fef9abbed647ca2069 Mon Sep 17 00:00:00 2001 From: Israel Chauca Fuentes Date: Wed, 19 Jun 2013 00:43:42 -0400 Subject: [PATCH] Smart quotes consider balanced quotes on current line. Closes #94. --- autoload/delimitMate.vim | 7 +++++-- doc/delimitMate.txt | 16 ++++++++-------- test/autoclose_quotes.txt | 2 ++ test/expand_cr.txt | 2 +- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/autoload/delimitMate.vim b/autoload/delimitMate.vim index 9e1ae2d..959e0f8 100644 --- a/autoload/delimitMate.vim +++ b/autoload/delimitMate.vim @@ -261,8 +261,11 @@ function! delimitMate#IsSmartQuote(char) "{{{ let valid_char_re = '\w\|[^[:punct:][:space:]]' let word_before = char_before =~ valid_char_re let word_at = char_at =~ valid_char_re - let escaped = delimitMate#CursorIdx() >= 1 && delimitMate#GetCharFromCursor(-1) == '\' - let result = word_before || escaped || word_at + let escaped = delimitMate#CursorIdx() >= 1 + \ && delimitMate#GetCharFromCursor(-1) == '\' + let noescaped = substitute(getline('.'), '\\.', '', 'g') + let even = !(count(split(noescaped, '\zs'), a:char) % 2) + let result = word_before || escaped || word_at || !even return result endfunction "delimitMate#SmartQuote }}} diff --git a/doc/delimitMate.txt b/doc/delimitMate.txt index 329246c..5c91316 100644 --- a/doc/delimitMate.txt +++ b/doc/delimitMate.txt @@ -291,9 +291,9 @@ e.g.: > 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. This feature only -works if the eol marker was inserted on the current insert session. +The contents of this string will be inserted after the closing matchpair or +quote when the respective opening matchapair or quote is inserted at the end +of the line. e.g.: > au FileType c,perl let b:delimitMate_eol_marker = ";" < @@ -425,11 +425,11 @@ e.g. typing at the "|": > ------------------------------------------------------------------------------ 3.4 SMART QUOTES *delimitMateSmartQuotes* -Only one quote will be inserted following a quote, a "\" or, following or -preceding a keyword character. This should cover closing quotes after a -string, opening quotes before a string, escaped quotes and apostrophes. Except -for apostrophes, this feature can be disabled setting the option -|'delimitMate_smart_quotes'| to 0. +Only one quote will be inserted following a quote, a "\", following or +preceding a keyword character, or when the number of quotes in the current +line is odd. This should cover closing quotes after a string, opening quotes +before a string, escaped quotes and apostrophes. Except for apostrophes, this +feature can be disabled setting the option |'delimitMate_smart_quotes'| to 0. e.g. typing at the "|": > diff --git a/test/autoclose_quotes.txt b/test/autoclose_quotes.txt index 79187ba..68f540f 100644 --- a/test/autoclose_quotes.txt +++ b/test/autoclose_quotes.txt @@ -38,3 +38,5 @@ set cpo=ces$ "('test'x" "('test'x)" # Duplicate whole line when inserting quote at bol #105 "}\'" "''}" +"'\abc '" "'abc '" +"''abc '" "''abc ''" diff --git a/test/expand_cr.txt b/test/expand_cr.txt index 4ed1218..cbb6aac 100644 --- a/test/expand_cr.txt +++ b/test/expand_cr.txt @@ -48,7 +48,7 @@ sub foo { call setline(1, '"{bracketed}') normal A"x ================================================================================ -"{bracketed}"x" +"{bracketed}"x -------------------------------------------------------------------------------- # Syntax folding enabled by autocmd breaks expansion. But ti can't be tested # with :normal