From ac792c01b62cb64b0eaf133f37e1c3d49d3531ae Mon Sep 17 00:00:00 2001 From: Israel Chauca Fuentes Date: Sun, 4 Jan 2015 20:25:50 -0500 Subject: [PATCH] Indent with spaces. --- autoload/delimitMate.vim | 1104 +++++++++++++++++++------------------- plugin/delimitMate.vim | 600 ++++++++++----------- 2 files changed, 852 insertions(+), 852 deletions(-) diff --git a/autoload/delimitMate.vim b/autoload/delimitMate.vim index 98ae22c..5307124 100644 --- a/autoload/delimitMate.vim +++ b/autoload/delimitMate.vim @@ -9,663 +9,663 @@ "let delimitMate_loaded = 1 if !exists('s:options') - let s:options = {} + let s:options = {} endif function! s:s(name, value, ...) "{{{ - let scope = a:0 ? a:1 : 's' - let bufnr = bufnr('%') - if !exists('s:options[bufnr]') - let s:options[bufnr] = {} - endif - if scope == 's' - let name = 's:options.' . bufnr . '.' . a:name - else - let name = scope . ':delimitMate_' . a:name - if exists('name') - exec 'unlet! ' . name - endif - endif - exec 'let ' . name . ' = a:value' + let scope = a:0 ? a:1 : 's' + let bufnr = bufnr('%') + if !exists('s:options[bufnr]') + let s:options[bufnr] = {} + endif + if scope == 's' + let name = 's:options.' . bufnr . '.' . a:name + else + let name = scope . ':delimitMate_' . a:name + if exists('name') + exec 'unlet! ' . name + endif + endif + exec 'let ' . name . ' = a:value' endfunction "}}} function! s:g(name, ...) "{{{ - if a:0 == 2 - return deepcopy(get(a:2, 'delimitMate_' . a:name, a:1)) - elseif a:0 == 1 - let bufoptions = get(s:options, bufnr('%'), {}) - return deepcopy(get(bufoptions, a:name, a:1)) - else - return deepcopy(eval('s:options.' . bufnr('%') . '.' . a:name)) - endif + if a:0 == 2 + return deepcopy(get(a:2, 'delimitMate_' . a:name, a:1)) + elseif a:0 == 1 + let bufoptions = get(s:options, bufnr('%'), {}) + return deepcopy(get(bufoptions, a:name, a:1)) + else + return deepcopy(eval('s:options.' . bufnr('%') . '.' . a:name)) + endif endfunction "}}} function! s:exists(name, ...) "{{{ - let scope = a:0 ? a:1 : 's' - if scope == 's' - let bufnr = bufnr('%') - let name = 'options.' . bufnr . '.' . a:name - else - let name = 'delimitMate_' . a:name - endif - return exists(scope . ':' . name) + let scope = a:0 ? a:1 : 's' + if scope == 's' + let bufnr = bufnr('%') + let name = 'options.' . bufnr . '.' . a:name + else + let name = 'delimitMate_' . a:name + endif + return exists(scope . ':' . name) endfunction "}}} function! s:is_jump(...) "{{{ - " Returns 1 if the next character is a closing delimiter. - let char = s:get_char(0) - let list = s:g('right_delims') + s:g('quotes_list') + " Returns 1 if the next character is a closing delimiter. + let char = s:get_char(0) + let list = s:g('right_delims') + s:g('quotes_list') - " Closing delimiter on the right. - if (!a:0 && index(list, char) > -1) - \ || (a:0 && char == a:1) - return 1 - endif + " Closing delimiter on the right. + if (!a:0 && index(list, char) > -1) + \ || (a:0 && char == a:1) + return 1 + endif - " Closing delimiter with space expansion. - let nchar = s:get_char(1) - if !a:0 && s:g('expand_space') && char == " " - if index(list, nchar) > -1 - return 2 - endif - elseif a:0 && s:g('expand_space') && nchar == a:1 && char == ' ' - return 3 - endif + " Closing delimiter with space expansion. + let nchar = s:get_char(1) + if !a:0 && s:g('expand_space') && char == " " + if index(list, nchar) > -1 + return 2 + endif + elseif a:0 && s:g('expand_space') && nchar == a:1 && char == ' ' + return 3 + endif - if !s:g('jump_expansion') - return 0 - endif + if !s:g('jump_expansion') + return 0 + endif - " Closing delimiter with CR expansion. - let uchar = matchstr(getline(line('.') + 1), '^\s*\zs\S') - if !a:0 && s:g('expand_cr') && char == "" - if index(list, uchar) > -1 - return 4 - endif - elseif a:0 && s:g('expand_cr') && uchar == a:1 - return 5 - endif - return 0 + " Closing delimiter with CR expansion. + let uchar = matchstr(getline(line('.') + 1), '^\s*\zs\S') + if !a:0 && s:g('expand_cr') && char == "" + if index(list, uchar) > -1 + return 4 + endif + elseif a:0 && s:g('expand_cr') && uchar == a:1 + return 5 + endif + return 0 endfunction "}}} function! s:rquote(char) "{{{ - let pos = matchstr(getline('.')[col('.') : ], escape(a:char, '[]*.^$\'), 1) - let i = 0 - while s:get_char(i) ==# a:char - let i += 1 - endwhile - return i + let pos = matchstr(getline('.')[col('.') : ], escape(a:char, '[]*.^$\'), 1) + let i = 0 + while s:get_char(i) ==# a:char + let i += 1 + endwhile + return i endfunction "}}} function! s:lquote(char) "{{{ - let i = 0 - while s:get_char(i - 1) ==# a:char - let i -= 1 - endwhile - return i * -1 + let i = 0 + while s:get_char(i - 1) ==# a:char + let i -= 1 + endwhile + return i * -1 endfunction "}}} function! s:get_char(...) "{{{ - let idx = col('.') - 1 - if !a:0 || (a:0 && a:1 >= 0) - " Get char from cursor. - let line = getline('.')[idx :] - let pos = a:0 ? a:1 : 0 - return matchstr(line, '^'.repeat('.', pos).'\zs.') - endif - " Get char behind cursor. - let line = getline('.')[: idx - 1] - let pos = 0 - (1 + a:1) - return matchstr(line, '.\ze'.repeat('.', pos).'$') + let idx = col('.') - 1 + if !a:0 || (a:0 && a:1 >= 0) + " Get char from cursor. + let line = getline('.')[idx :] + let pos = a:0 ? a:1 : 0 + return matchstr(line, '^'.repeat('.', pos).'\zs.') + endif + " Get char behind cursor. + let line = getline('.')[: idx - 1] + let pos = 0 - (1 + a:1) + return matchstr(line, '.\ze'.repeat('.', pos).'$') endfunction "s:get_char }}} function! s:is_cr_expansion(...) " {{{ - let nchar = getline(line('.')-1)[-1:] - let schar = matchstr(getline(line('.')+1), '^\s*\zs\S') - let isEmpty = a:0 ? getline('.') =~ '^\s*$' : empty(getline('.')) - if index(s:g('left_delims'), nchar) > -1 - \ && index(s:g('left_delims'), nchar) - \ == index(s:g('right_delims'), schar) - \ && isEmpty - return 1 - elseif index(s:g('quotes_list'), nchar) > -1 - \ && index(s:g('quotes_list'), nchar) - \ == index(s:g('quotes_list'), schar) - \ && isEmpty - return 1 - else - return 0 - endif + let nchar = getline(line('.')-1)[-1:] + let schar = matchstr(getline(line('.')+1), '^\s*\zs\S') + let isEmpty = a:0 ? getline('.') =~ '^\s*$' : empty(getline('.')) + if index(s:g('left_delims'), nchar) > -1 + \ && index(s:g('left_delims'), nchar) + \ == index(s:g('right_delims'), schar) + \ && isEmpty + return 1 + elseif index(s:g('quotes_list'), nchar) > -1 + \ && index(s:g('quotes_list'), nchar) + \ == index(s:g('quotes_list'), schar) + \ && isEmpty + return 1 + else + return 0 + endif endfunction " }}} s:is_cr_expansion() function! s:is_space_expansion() " {{{ - if col('.') > 2 - let pchar = s:get_char(-2) - let nchar = s:get_char(1) - let isSpaces = - \ (s:get_char(-1) - \ == s:get_char(0) - \ && s:get_char(-1) == " ") + if col('.') > 2 + let pchar = s:get_char(-2) + let nchar = s:get_char(1) + let isSpaces = + \ (s:get_char(-1) + \ == s:get_char(0) + \ && s:get_char(-1) == " ") - if index(s:g('left_delims'), pchar) > -1 && - \ index(s:g('left_delims'), pchar) - \ == index(s:g('right_delims'), nchar) && - \ isSpaces - return 1 - elseif index(s:g('quotes_list'), pchar) > -1 && - \ index(s:g('quotes_list'), pchar) - \ == index(s:g('quotes_list'), nchar) && - \ isSpaces - return 1 - endif - endif - return 0 + if index(s:g('left_delims'), pchar) > -1 && + \ index(s:g('left_delims'), pchar) + \ == index(s:g('right_delims'), nchar) && + \ isSpaces + return 1 + elseif index(s:g('quotes_list'), pchar) > -1 && + \ index(s:g('quotes_list'), pchar) + \ == index(s:g('quotes_list'), nchar) && + \ isSpaces + return 1 + endif + endif + return 0 endfunction " }}} IsSpaceExpansion() function! s:is_empty_matchpair() "{{{ - " get char before the cursor. - let open = s:get_char(-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 ==# s:get_char(0) + " get char before the cursor. + let open = s:get_char(-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 ==# s:get_char(0) endfunction "}}} function! s:is_empty_quotes() "{{{ - " get char before the cursor. - let quote = s:get_char(-1) - let idx = index(s:g('quotes_list'), quote) - if idx == -1 - return 0 - endif - return quote ==# s:get_char(0) + " get char before the cursor. + let quote = s:get_char(-1) + let idx = index(s:g('quotes_list'), quote) + if idx == -1 + return 0 + endif + return quote ==# s:get_char(0) endfunction "}}} function! s:cursor_idx() "{{{ - let idx = len(split(getline('.')[: col('.') - 1], '\zs')) - 1 - return idx + let idx = len(split(getline('.')[: col('.') - 1], '\zs')) - 1 + return idx endfunction "delimitMate#CursorCol }}} function! s:get_syn_name() "{{{ - let col = col('.') - if col == col('$') - let col = col - 1 - endif - return synIDattr(synIDtrans(synID(line('.'), col, 1)), 'name') + let col = col('.') + if col == col('$') + let col = col - 1 + endif + return synIDattr(synIDtrans(synID(line('.'), col, 1)), 'name') endfunction " }}} function! s:is_forbidden(char) "{{{ - if !s:g('excluded_regions_enabled') - return 0 - endif - let region = s:get_syn_name() - return index(s:g('excluded_regions_list'), region) >= 0 + if !s:g('excluded_regions_enabled') + return 0 + endif + let region = s:get_syn_name() + return index(s:g('excluded_regions_list'), region) >= 0 endfunction "}}} function! s:balance_matchpairs(char) "{{{ - " Returns: - " = 0 => Parens balanced. - " > 0 => More opening parens. - " < 0 => More closing parens. + " Returns: + " = 0 => Parens balanced. + " > 0 => More opening parens. + " < 0 => More closing parens. - let line = getline('.') - let col = s:cursor_idx() - 1 - let col = col >= 0 ? col : 0 - let list = split(line, '\zs') - let left = s:g('left_delims')[index(s:g('right_delims'), a:char)] - let right = a:char - let opening = 0 - let closing = 0 + let line = getline('.') + let col = s:cursor_idx() - 1 + let col = col >= 0 ? col : 0 + let list = split(line, '\zs') + let left = s:g('left_delims')[index(s:g('right_delims'), a:char)] + let right = a:char + let opening = 0 + let closing = 0 - " If the cursor is not at the beginning, count what's behind it. - if col > 0 - " Find the first opening paren: - let start = index(list, left) - " Must be before cursor: - let start = start < col ? start : col - 1 - " Now count from the first opening until the cursor, this will prevent - " extra closing parens from being counted. - let opening = count(list[start : col - 1], left) - let closing = count(list[start : col - 1], right) - " I don't care if there are more closing parens than opening parens. - let closing = closing > opening ? opening : closing - endif + " If the cursor is not at the beginning, count what's behind it. + if col > 0 + " Find the first opening paren: + let start = index(list, left) + " Must be before cursor: + let start = start < col ? start : col - 1 + " Now count from the first opening until the cursor, this will prevent + " extra closing parens from being counted. + let opening = count(list[start : col - 1], left) + let closing = count(list[start : col - 1], right) + " I don't care if there are more closing parens than opening parens. + let closing = closing > opening ? opening : closing + endif - " Evaluate parens from the cursor to the end: - let opening += count(list[col :], left) - let closing += count(list[col :], right) + " Evaluate parens from the cursor to the end: + let opening += count(list[col :], left) + let closing += count(list[col :], right) - " Return the found balance: - return opening - closing + " Return the found balance: + return opening - closing endfunction "}}} function! s:is_smart_quote(char) "{{{ - " TODO: Allow using a:char in the pattern. - let tmp = s:g('smart_quotes') - if empty(tmp) - return 0 - endif - let regex = matchstr(tmp, '^!\?\zs.*') - " Flip matched value if regex starts with ! - let mod = tmp =~ '^!' ? [1, 0] : [0, 1] - let matched = search(regex, 'ncb', line('.')) > 0 - let noescaped = substitute(getline('.'), '\\.', '', 'g') - let odd = (count(split(noescaped, '\zs'), a:char) % 2) - let result = mod[matched] || odd - return result + " TODO: Allow using a:char in the pattern. + let tmp = s:g('smart_quotes') + if empty(tmp) + return 0 + endif + let regex = matchstr(tmp, '^!\?\zs.*') + " Flip matched value if regex starts with ! + let mod = tmp =~ '^!' ? [1, 0] : [0, 1] + let matched = search(regex, 'ncb', line('.')) > 0 + let noescaped = substitute(getline('.'), '\\.', '', 'g') + let odd = (count(split(noescaped, '\zs'), a:char) % 2) + let result = mod[matched] || odd + return result endfunction "delimitMate#SmartQuote }}} function! delimitMate#Set(...) "{{{ - return call('s:s', a:000) + return call('s:s', a:000) endfunction "}}} function! delimitMate#Get(...) "{{{ - return call('s:g', a:000) + return call('s:g', a:000) endfunction "}}} function! delimitMate#ShouldJump(...) "{{{ - return call('s:is_jump', a:000) + return call('s:is_jump', a:000) endfunction "}}} function! delimitMate#IsEmptyPair(str) "{{{ - if strlen(substitute(a:str, ".", "x", "g")) != 2 - return 0 - endif - let idx = index(s:g('left_delims'), matchstr(a:str, '^.')) - if idx > -1 && - \ s:g('right_delims')[idx] == matchstr(a:str, '.$') - return 1 - endif - let idx = index(s:g('quotes_list'), matchstr(a:str, '^.')) - if idx > -1 && - \ s:g('quotes_list')[idx] == matchstr(a:str, '.$') - return 1 - endif - return 0 + if strlen(substitute(a:str, ".", "x", "g")) != 2 + return 0 + endif + let idx = index(s:g('left_delims'), matchstr(a:str, '^.')) + if idx > -1 && + \ s:g('right_delims')[idx] == matchstr(a:str, '.$') + return 1 + endif + let idx = index(s:g('quotes_list'), matchstr(a:str, '^.')) + if idx > -1 && + \ s:g('quotes_list')[idx] == matchstr(a:str, '.$') + return 1 + endif + return 0 endfunction "}}} function! delimitMate#WithinEmptyPair() "{{{ - " if cursor is at column 1 return 0 - if col('.') == 1 - return 0 - endif - " get char before the cursor. - let char1 = s:get_char(-1) - " get char under the cursor. - let char2 = s:get_char(0) - return delimitMate#IsEmptyPair( char1.char2 ) + " if cursor is at column 1 return 0 + if col('.') == 1 + return 0 + endif + " get char before the cursor. + let char1 = s:get_char(-1) + " get char under the cursor. + let char2 = s:get_char(0) + return delimitMate#IsEmptyPair( char1.char2 ) endfunction "}}} function! delimitMate#SkipDelim(char) "{{{ - if s:is_forbidden(a:char) - return a:char - endif - let col = col('.') - 1 - let line = getline('.') - if col > 0 - let cur = s:get_char(0) - let pre = s:get_char(-1) - else - let cur = s:get_char(0) - let pre = "" - endif - if pre == "\\" - " Escaped character - return a:char - elseif cur == a:char - " Exit pair - return a:char . "\" - elseif delimitMate#IsEmptyPair( pre . a:char ) - " Add closing delimiter and jump back to the middle. - return a:char . "\" - else - " Nothing special here, return the same character. - return a:char - endif + if s:is_forbidden(a:char) + return a:char + endif + let col = col('.') - 1 + let line = getline('.') + if col > 0 + let cur = s:get_char(0) + let pre = s:get_char(-1) + else + let cur = s:get_char(0) + let pre = "" + endif + if pre == "\\" + " Escaped character + return a:char + elseif cur == a:char + " Exit pair + return a:char . "\" + elseif delimitMate#IsEmptyPair( pre . a:char ) + " Add closing delimiter and jump back to the middle. + return a:char . "\" + else + " Nothing special here, return the same character. + return a:char + endif endfunction "}}} function! delimitMate#ParenDelim(right) " {{{ - let left = s:g('left_delims')[index(s:g('right_delims'),a:right)] - if s:is_forbidden(a:right) - return left - endif - " Try to balance matchpairs - if s:g('balance_matchpairs') && - \ s:balance_matchpairs(a:right) < 0 - return left - endif - let line = getline('.') - let col = col('.')-2 - if s:g('smart_matchpairs') != '' - let smart_matchpairs = substitute(s:g('smart_matchpairs'), '\\!', left, 'g') - let smart_matchpairs = substitute(smart_matchpairs, '\\#', a:right, 'g') - if line[col+1:] =~ smart_matchpairs - return left - endif - endif - let tail = len(line) == (col + 1) ? s:g('eol_marker') : '' - "if (col) < 0 - " call setline('.',a:right.line) - "endif - return left . a:right . tail . repeat("\", len(split(tail, '\zs')) + 1) + let left = s:g('left_delims')[index(s:g('right_delims'),a:right)] + if s:is_forbidden(a:right) + return left + endif + " Try to balance matchpairs + if s:g('balance_matchpairs') && + \ s:balance_matchpairs(a:right) < 0 + return left + endif + let line = getline('.') + let col = col('.')-2 + if s:g('smart_matchpairs') != '' + let smart_matchpairs = substitute(s:g('smart_matchpairs'), '\\!', left, 'g') + let smart_matchpairs = substitute(smart_matchpairs, '\\#', a:right, 'g') + if line[col+1:] =~ smart_matchpairs + return left + endif + endif + let tail = len(line) == (col + 1) ? s:g('eol_marker') : '' + "if (col) < 0 + " call setline('.',a:right.line) + "endif + return left . a:right . tail . repeat("\", len(split(tail, '\zs')) + 1) endfunction " }}} function! delimitMate#QuoteDelim(char) "{{{ - if s:is_forbidden(a:char) - return a:char - endif - let char_at = s:get_char(0) - let char_before = s:get_char(-1) - let nesting_on = index(s:g('nesting_quotes'), a:char) > -1 - let left_q = nesting_on ? s:lquote(a:char) : 0 - if nesting_on && left_q > 1 - " Nesting quotes. - let right_q = s:rquote(a:char) - let quotes = right_q > left_q + 1 ? 0 : left_q - right_q + 2 - let lefts = quotes - 1 - return repeat(a:char, quotes) . repeat("\", lefts) - elseif char_at == a:char - " Inside an empty pair, jump out - return a:char . "\" - elseif a:char == '"' && index(split(&ft, '\.'), "vim") != -1 && getline('.') =~ '^\s*$' - " If we are in a vim file and it looks like we're starting a comment, do - " not add a closing char. - return a:char - elseif s:is_smart_quote(a:char) - " Seems like a smart quote, insert a single char. - return a:char - elseif (char_before == a:char && char_at != a:char) - \ && !empty(s:g('smart_quotes')) - " Seems like we have an unbalanced quote, insert one quotation - " mark and jump to the middle. - return a:char . "\" - else - " Insert a pair and jump to the middle. - let sufix = '' - if !empty(s:g('eol_marker')) && col('.') - 1 == len(getline('.')) - let idx = len(s:g('eol_marker')) * -1 - let marker = getline('.')[idx : ] - let has_marker = marker == s:g('eol_marker') - let sufix = !has_marker ? s:g('eol_marker') : '' - endif - return a:char . a:char . "\" - endif + if s:is_forbidden(a:char) + return a:char + endif + let char_at = s:get_char(0) + let char_before = s:get_char(-1) + let nesting_on = index(s:g('nesting_quotes'), a:char) > -1 + let left_q = nesting_on ? s:lquote(a:char) : 0 + if nesting_on && left_q > 1 + " Nesting quotes. + let right_q = s:rquote(a:char) + let quotes = right_q > left_q + 1 ? 0 : left_q - right_q + 2 + let lefts = quotes - 1 + return repeat(a:char, quotes) . repeat("\", lefts) + elseif char_at == a:char + " Inside an empty pair, jump out + return a:char . "\" + elseif a:char == '"' && index(split(&ft, '\.'), "vim") != -1 && getline('.') =~ '^\s*$' + " If we are in a vim file and it looks like we're starting a comment, do + " not add a closing char. + return a:char + elseif s:is_smart_quote(a:char) + " Seems like a smart quote, insert a single char. + return a:char + elseif (char_before == a:char && char_at != a:char) + \ && !empty(s:g('smart_quotes')) + " Seems like we have an unbalanced quote, insert one quotation + " mark and jump to the middle. + return a:char . "\" + else + " Insert a pair and jump to the middle. + let sufix = '' + if !empty(s:g('eol_marker')) && col('.') - 1 == len(getline('.')) + let idx = len(s:g('eol_marker')) * -1 + let marker = getline('.')[idx : ] + let has_marker = marker == s:g('eol_marker') + let sufix = !has_marker ? s:g('eol_marker') : '' + endif + return a:char . a:char . "\" + endif endfunction "}}} function! delimitMate#JumpOut(char) "{{{ - if s:is_forbidden(a:char) - return a:char - endif - let jump = s:is_jump(a:char) - if jump == 1 - " HACK: Instead of , we remove the char to be jumped over and - " insert it again. This will trigger re-indenting via 'indentkeys'. - " Ref: https://github.com/Raimondi/delimitMate/issues/168 - return "\".a:char - elseif jump == 3 - return "\\" - elseif jump == 5 - return "\\I\" - else - return a:char - endif + if s:is_forbidden(a:char) + return a:char + endif + let jump = s:is_jump(a:char) + if jump == 1 + " HACK: Instead of , we remove the char to be jumped over and + " insert it again. This will trigger re-indenting via 'indentkeys'. + " Ref: https://github.com/Raimondi/delimitMate/issues/168 + return "\".a:char + elseif jump == 3 + return "\\" + elseif jump == 5 + return "\\I\" + else + return a:char + endif endfunction " }}} function! delimitMate#JumpAny(...) " {{{ - if s:is_forbidden('') - return '' - endif - if !s:is_jump() - return '' - endif - " Let's get the character on the right. - let char = s:get_char(0) - if char == " " - " Space expansion. - return "\\" - elseif char == "" - " CR expansion. - return "\" . getline(line('.') + 1)[0] . "\\" - else - return "\" - endif + if s:is_forbidden('') + return '' + endif + if !s:is_jump() + return '' + endif + " Let's get the character on the right. + let char = s:get_char(0) + if char == " " + " Space expansion. + return "\\" + elseif char == "" + " CR expansion. + return "\" . getline(line('.') + 1)[0] . "\\" + else + return "\" + endif endfunction " delimitMate#JumpAny() }}} function! delimitMate#JumpMany() " {{{ - let line = split(getline('.')[col('.') - 1 : ], '\zs') - let rights = "" - let found = 0 - for char in line - if index(s:g('quotes_list'), char) >= 0 || - \ index(s:g('right_delims'), char) >= 0 - let rights .= "\" - let found = 1 - elseif found == 0 - let rights .= "\" - else - break - endif - endfor - if found == 1 - return rights - else - return '' - endif + let line = split(getline('.')[col('.') - 1 : ], '\zs') + let rights = "" + let found = 0 + for char in line + if index(s:g('quotes_list'), char) >= 0 || + \ index(s:g('right_delims'), char) >= 0 + let rights .= "\" + let found = 1 + elseif found == 0 + let rights .= "\" + else + break + endif + endfor + if found == 1 + return rights + else + return '' + endif endfunction " delimitMate#JumpMany() }}} function! delimitMate#ExpandReturn() "{{{ - if s:is_forbidden("") - return "\" - endif - let escaped = s:cursor_idx() >= 2 - \ && s:get_char(-2) == '\' - let expand_right_matchpair = s:g('expand_cr') == 2 - \ && index(s:g('right_delims'), s:get_char(0)) > -1 - let expand_inside_quotes = s:g('expand_inside_quotes') - \ && s:is_empty_quotes() - \ && !escaped - if !pumvisible() - \ && (s:is_empty_matchpair() - \ || expand_right_matchpair - \ || expand_inside_quotes) - let val = "\a\" - if &smartindent && !&cindent && !&indentexpr - \ && s:get_char(0) == '}' - " indentation is controlled by 'smartindent', and the first character on - " the new line is '}'. If this were typed manually it would reindent to - " match the current line. Let's reproduce that behavior. - let shifts = indent('.') / &sw - let spaces = indent('.') - (shifts * &sw) - let val .= "^\".repeat("\", shifts).repeat(' ', spaces) - endif - " Expand: - " XXX zv prevents breaking expansion with syntax folding enabled by - " InsertLeave. - let val .= "\zvO" - return val - else - return "\" - endif + if s:is_forbidden("") + return "\" + endif + let escaped = s:cursor_idx() >= 2 + \ && s:get_char(-2) == '\' + let expand_right_matchpair = s:g('expand_cr') == 2 + \ && index(s:g('right_delims'), s:get_char(0)) > -1 + let expand_inside_quotes = s:g('expand_inside_quotes') + \ && s:is_empty_quotes() + \ && !escaped + if !pumvisible() + \ && (s:is_empty_matchpair() + \ || expand_right_matchpair + \ || expand_inside_quotes) + let val = "\a\" + if &smartindent && !&cindent && !&indentexpr + \ && s:get_char(0) == '}' + " indentation is controlled by 'smartindent', and the first character on + " the new line is '}'. If this were typed manually it would reindent to + " match the current line. Let's reproduce that behavior. + let shifts = indent('.') / &sw + let spaces = indent('.') - (shifts * &sw) + let val .= "^\".repeat("\", shifts).repeat(' ', spaces) + endif + " Expand: + " XXX zv prevents breaking expansion with syntax folding enabled by + " InsertLeave. + let val .= "\zvO" + return val + else + return "\" + endif endfunction "}}} function! delimitMate#ExpandSpace() "{{{ - if s:is_forbidden("\") - return "\" - endif - let escaped = s:cursor_idx() >= 2 - \ && s:get_char(-2) == '\' - let expand_inside_quotes = s:g('expand_inside_quotes') - \ && s:is_empty_quotes() - \ && !escaped - if s:is_empty_matchpair() || expand_inside_quotes - " Expand: - return "\\\" - else - return "\" - endif + if s:is_forbidden("\") + return "\" + endif + let escaped = s:cursor_idx() >= 2 + \ && s:get_char(-2) == '\' + let expand_inside_quotes = s:g('expand_inside_quotes') + \ && s:is_empty_quotes() + \ && !escaped + if s:is_empty_matchpair() || expand_inside_quotes + " Expand: + return "\\\" + else + return "\" + endif endfunction "}}} function! delimitMate#BS() " {{{ - if s:is_forbidden("") - let extra = '' - elseif &bs !~ 'start\|2' - let extra = '' - elseif delimitMate#WithinEmptyPair() - let extra = "\" - elseif s:is_space_expansion() - let extra = "\" - elseif s:is_cr_expansion() - let extra = repeat("\", - \ len(matchstr(getline(line('.') + 1), '^\s*\S'))) - else - let extra = '' - endif - return "\" . extra + if s:is_forbidden("") + let extra = '' + elseif &bs !~ 'start\|2' + let extra = '' + elseif delimitMate#WithinEmptyPair() + let extra = "\" + elseif s:is_space_expansion() + let extra = "\" + elseif s:is_cr_expansion() + let extra = repeat("\", + \ len(matchstr(getline(line('.') + 1), '^\s*\S'))) + else + let extra = '' + endif + return "\" . extra endfunction " }}} delimitMate#BS() function! delimitMate#TestMappings() "{{{ - echom 1 - %d - let options = sort([ - \ 'apostrophes', - \ 'autoclose', - \ 'balance_matchpairs', - \ 'jump_expansion', - \ 'eol_marker', - \ 'excluded_ft', - \ 'excluded_regions', - \ 'expand_cr', - \ 'expand_space', - \ 'matchpairs', - \ 'nesting_quotes', - \ 'quotes', - \ 'smart_matchpairs', - \ 'smart_quotes', - \ 'expand_inside_quotes', - \]) - let optoutput = ['delimitMate Report', '==================', '', - \ '* Options: ( ) default, (g) global, (b) buffer',''] - for option in options - let scope = s:exists(option, 'b') ? 'b' - \ : s:exists(option, 'g') ? 'g' : ' ' - call add(optoutput, - \'(' . scope . ')' . ' delimitMate_' . option . ' = ' . string(s:g(option))) - endfor - call append(line('$'), optoutput + ['--------------------','']) + echom 1 + %d + let options = sort([ + \ 'apostrophes', + \ 'autoclose', + \ 'balance_matchpairs', + \ 'jump_expansion', + \ 'eol_marker', + \ 'excluded_ft', + \ 'excluded_regions', + \ 'expand_cr', + \ 'expand_space', + \ 'matchpairs', + \ 'nesting_quotes', + \ 'quotes', + \ 'smart_matchpairs', + \ 'smart_quotes', + \ 'expand_inside_quotes', + \]) + let optoutput = ['delimitMate Report', '==================', '', + \ '* Options: ( ) default, (g) global, (b) buffer',''] + for option in options + let scope = s:exists(option, 'b') ? 'b' + \ : s:exists(option, 'g') ? 'g' : ' ' + call add(optoutput, + \'(' . scope . ')' . ' delimitMate_' . option . ' = ' . string(s:g(option))) + endfor + call append(line('$'), optoutput + ['--------------------','']) - " Check if mappings were set. {{{ - let imaps = s:g('right_delims') - let imaps += ( s:g('autoclose') ? s:g('left_delims') : [] ) - let imaps += - \ s:g('quotes_list') + - \ s:g('apostrophes_list') + - \ ['', '', '', '', 'g'] - let imaps += ( s:g('expand_cr') ? [''] : [] ) - let imaps += ( s:g('expand_space') ? [''] : [] ) + " Check if mappings were set. {{{ + let imaps = s:g('right_delims') + let imaps += ( s:g('autoclose') ? s:g('left_delims') : [] ) + let imaps += + \ s:g('quotes_list') + + \ s:g('apostrophes_list') + + \ ['', '', '', '', 'g'] + let imaps += ( s:g('expand_cr') ? [''] : [] ) + let imaps += ( s:g('expand_space') ? [''] : [] ) - let imappings = [] - for map in imaps - let output = '' - if map == '|' - let map = '' - endif - redir => output | execute "verbose imap ".map | redir END - let imappings += split(output, '\n') - endfor + let imappings = [] + for map in imaps + let output = '' + if map == '|' + let map = '' + endif + redir => output | execute "verbose imap ".map | redir END + let imappings += split(output, '\n') + endfor - unlet! output - let output = ['* Mappings:', ''] + imappings + ['--------------------', ''] - call append('$', output+['* Showcase:', '']) - " }}} - if s:g('autoclose') - " {{{ - for i in range(len(s:g('left_delims'))) - exec "normal Go0\Open: " . s:g('left_delims')[i]. "|" - exec "normal o0\Delete: " . s:g('left_delims')[i] . "\|" - exec "normal o0\Exit: " . s:g('left_delims')[i] . s:g('right_delims')[i] . "|" - if s:g('expand_space') == 1 - exec "normal o0\Space: " . s:g('left_delims')[i] . " |" - exec "normal o0\Delete space: " . s:g('left_delims')[i] - \ . " \|" - endif - if s:g('expand_cr') == 1 - exec "normal o0\Car return: " . s:g('left_delims')[i] . - \ "\|" - exec "normal Go0\Delete car return: " . s:g('left_delims')[i] - \ . "\0\\|" - endif - call append(line('$'), '') - endfor - for i in range(len(s:g('quotes_list'))) - exec "normal Go0\Open: " . s:g('quotes_list')[i] . "|" - exec "normal o0\Delete: " . s:g('quotes_list')[i] . "\|" - exec "normal o0\Exit: " . s:g('quotes_list')[i] . s:g('quotes_list')[i] . "|" - if s:g('expand_space') == 1 - exec "normal o0\Space: " . s:g('quotes_list')[i] . " |" - exec "normal o0\Delete space: " . s:g('quotes_list')[i] - \ . " \|" - endif - if s:g('expand_cr') == 1 - exec "normal o0\Car return: " . s:g('quotes_list')[i] - \ . "\|" - exec "normal Go0\Delete car return: " . s:g('quotes_list')[i] - \ . "\\|" - endif - call append(line('$'), '') - endfor - "}}} - else - "{{{ - for i in range(len(s:g('left_delims'))) - exec "normal GoOpen & close: " . s:g('left_delims')[i] - \ . s:g('right_delims')[i] . "|" - exec "normal oDelete: " . s:g('left_delims')[i] - \ . s:g('right_delims')[i] . "\|" - exec "normal oExit: " . s:g('left_delims')[i] . s:g('right_delims')[i] - \ . s:g('right_delims')[i] . "|" - if s:g('expand_space') == 1 - exec "normal oSpace: " . s:g('left_delims')[i] - \ . s:g('right_delims')[i] . " |" - exec "normal oDelete space: " . s:g('left_delims')[i] - \ . s:g('right_delims')[i] . " \|" - endif - if s:g('expand_cr') == 1 - exec "normal oCar return: " . s:g('left_delims')[i] - \ . s:g('right_delims')[i] . "\|" - exec "normal GoDelete car return: " . s:g('left_delims')[i] - \ . s:g('right_delims')[i] . "\\|" - endif - call append(line('$'), '') - endfor - for i in range(len(s:g('quotes_list'))) - exec "normal GoOpen & close: " . s:g('quotes_list')[i] - \ . s:g('quotes_list')[i] . "|" - exec "normal oDelete: " . s:g('quotes_list')[i] - \ . s:g('quotes_list')[i] . "\|" - exec "normal oExit: " . s:g('quotes_list')[i] . s:g('quotes_list')[i] - \ . s:g('quotes_list')[i] . "|" - if s:g('expand_space') == 1 - exec "normal oSpace: " . s:g('quotes_list')[i] - \ . s:g('quotes_list')[i] . " |" - exec "normal oDelete space: " . s:g('quotes_list')[i] - \ . s:g('quotes_list')[i] . " \|" - endif - if s:g('expand_cr') == 1 - exec "normal oCar return: " . s:g('quotes_list')[i] - \ . s:g('quotes_list')[i] . "\|" - exec "normal GoDelete car return: " . s:g('quotes_list')[i] - \ . s:g('quotes_list')[i] . "\\|" - endif - call append(line('$'), '') - endfor - endif "}}} - redir => setoptions | set | filetype | version | redir END - call append(line('$'), split(setoptions,"\n") - \ + ['--------------------']) - setlocal nowrap - call feedkeys("\\", 'n') + unlet! output + let output = ['* Mappings:', ''] + imappings + ['--------------------', ''] + call append('$', output+['* Showcase:', '']) + " }}} + if s:g('autoclose') + " {{{ + for i in range(len(s:g('left_delims'))) + exec "normal Go0\Open: " . s:g('left_delims')[i]. "|" + exec "normal o0\Delete: " . s:g('left_delims')[i] . "\|" + exec "normal o0\Exit: " . s:g('left_delims')[i] . s:g('right_delims')[i] . "|" + if s:g('expand_space') == 1 + exec "normal o0\Space: " . s:g('left_delims')[i] . " |" + exec "normal o0\Delete space: " . s:g('left_delims')[i] + \ . " \|" + endif + if s:g('expand_cr') == 1 + exec "normal o0\Car return: " . s:g('left_delims')[i] . + \ "\|" + exec "normal Go0\Delete car return: " . s:g('left_delims')[i] + \ . "\0\\|" + endif + call append(line('$'), '') + endfor + for i in range(len(s:g('quotes_list'))) + exec "normal Go0\Open: " . s:g('quotes_list')[i] . "|" + exec "normal o0\Delete: " . s:g('quotes_list')[i] . "\|" + exec "normal o0\Exit: " . s:g('quotes_list')[i] . s:g('quotes_list')[i] . "|" + if s:g('expand_space') == 1 + exec "normal o0\Space: " . s:g('quotes_list')[i] . " |" + exec "normal o0\Delete space: " . s:g('quotes_list')[i] + \ . " \|" + endif + if s:g('expand_cr') == 1 + exec "normal o0\Car return: " . s:g('quotes_list')[i] + \ . "\|" + exec "normal Go0\Delete car return: " . s:g('quotes_list')[i] + \ . "\\|" + endif + call append(line('$'), '') + endfor + "}}} + else + "{{{ + for i in range(len(s:g('left_delims'))) + exec "normal GoOpen & close: " . s:g('left_delims')[i] + \ . s:g('right_delims')[i] . "|" + exec "normal oDelete: " . s:g('left_delims')[i] + \ . s:g('right_delims')[i] . "\|" + exec "normal oExit: " . s:g('left_delims')[i] . s:g('right_delims')[i] + \ . s:g('right_delims')[i] . "|" + if s:g('expand_space') == 1 + exec "normal oSpace: " . s:g('left_delims')[i] + \ . s:g('right_delims')[i] . " |" + exec "normal oDelete space: " . s:g('left_delims')[i] + \ . s:g('right_delims')[i] . " \|" + endif + if s:g('expand_cr') == 1 + exec "normal oCar return: " . s:g('left_delims')[i] + \ . s:g('right_delims')[i] . "\|" + exec "normal GoDelete car return: " . s:g('left_delims')[i] + \ . s:g('right_delims')[i] . "\\|" + endif + call append(line('$'), '') + endfor + for i in range(len(s:g('quotes_list'))) + exec "normal GoOpen & close: " . s:g('quotes_list')[i] + \ . s:g('quotes_list')[i] . "|" + exec "normal oDelete: " . s:g('quotes_list')[i] + \ . s:g('quotes_list')[i] . "\|" + exec "normal oExit: " . s:g('quotes_list')[i] . s:g('quotes_list')[i] + \ . s:g('quotes_list')[i] . "|" + if s:g('expand_space') == 1 + exec "normal oSpace: " . s:g('quotes_list')[i] + \ . s:g('quotes_list')[i] . " |" + exec "normal oDelete space: " . s:g('quotes_list')[i] + \ . s:g('quotes_list')[i] . " \|" + endif + if s:g('expand_cr') == 1 + exec "normal oCar return: " . s:g('quotes_list')[i] + \ . s:g('quotes_list')[i] . "\|" + exec "normal GoDelete car return: " . s:g('quotes_list')[i] + \ . s:g('quotes_list')[i] . "\\|" + endif + call append(line('$'), '') + endfor + endif "}}} + redir => setoptions | set | filetype | version | redir END + call append(line('$'), split(setoptions,"\n") + \ + ['--------------------']) + setlocal nowrap + call feedkeys("\\", 'n') endfunction "}}} " vim:foldmethod=marker:foldcolumn=4:ts=2:sw=2 diff --git a/plugin/delimitMate.vim b/plugin/delimitMate.vim index 3b381e9..55f2242 100644 --- a/plugin/delimitMate.vim +++ b/plugin/delimitMate.vim @@ -9,354 +9,354 @@ " Initialization: {{{ if exists("g:loaded_delimitMate") || &cp - " User doesn't want this plugin or compatible is set, let's get out! - finish + " User doesn't want this plugin or compatible is set, let's get out! + finish endif let g:loaded_delimitMate = 1 let save_cpo = &cpo set cpo&vim if v:version < 700 - echoerr "delimitMate: this plugin requires vim >= 7!" - finish + echoerr "delimitMate: this plugin requires vim >= 7!" + finish endif let s:loaded_delimitMate = 1 -let delimitMate_version = "2.7" +let delimitMate_version = "2.8" "}}} " Functions: {{{ function! s:option_init(name, default) "{{{ - let b = exists("b:delimitMate_" . a:name) - let g = exists("g:delimitMate_" . a:name) - " Find value to use. - if !b && !g - let value = a:default - elseif b - exec "let value = b:delimitMate_" . a:name - else - exec "let value = g:delimitMate_" . a:name - endif - call s:s(a:name, value) + let b = exists("b:delimitMate_" . a:name) + let g = exists("g:delimitMate_" . a:name) + " Find value to use. + if !b && !g + let value = a:default + elseif b + exec "let value = b:delimitMate_" . a:name + else + exec "let value = g:delimitMate_" . a:name + endif + call s:s(a:name, value) endfunction "}}} function! s:init() "{{{ " Initialize variables: - " autoclose - call s:option_init("autoclose", 1) - " matchpairs - call s:option_init("matchpairs", string(&matchpairs)[1:-2]) - call s:option_init("matchpairs_list", map(split(s:g('matchpairs'), ','), 'split(v:val, '':'')')) - let pairs = s:g('matchpairs_list') - if len(filter(pairs, 'v:val[0] ==# v:val[1]')) - echohl ErrorMsg - echom 'delimitMate: each member of a pair in delimitMate_matchpairs must be different from each other.' - echom 'delimitMate: invalid pairs: ' . join(map(pairs, 'join(v:val, ":")'), ', ') - echohl Normal - return 0 - endif - call s:option_init("left_delims", map(copy(s:g('matchpairs_list')), 'v:val[0]')) - call s:option_init("right_delims", map(copy(s:g('matchpairs_list')), 'v:val[1]')) - " quotes - call s:option_init("quotes", "\" ' `") - call s:option_init("quotes_list",split(s:g('quotes'), '\s\+')) - " nesting_quotes - call s:option_init("nesting_quotes", []) - " excluded_regions - call s:option_init("excluded_regions", "Comment") - call s:option_init("excluded_regions_list", split(s:g('excluded_regions'), ',\s*')) - let enabled = len(s:g('excluded_regions_list')) > 0 - call s:option_init("excluded_regions_enabled", enabled) - " expand_space - if exists("b:delimitMate_expand_space") && type(b:delimitMate_expand_space) == type("") - echom "b:delimitMate_expand_space is '".b:delimitMate_expand_space."' but it must be either 1 or 0!" - echom "Read :help 'delimitMate_expand_space' for more details." - unlet b:delimitMate_expand_space - let b:delimitMate_expand_space = 1 - endif - if exists("g:delimitMate_expand_space") && type(g:delimitMate_expand_space) == type("") - echom "delimitMate_expand_space is '".g:delimitMate_expand_space."' but it must be either 1 or 0!" - echom "Read :help 'delimitMate_expand_space' for more details." - unlet g:delimitMate_expand_space - let g:delimitMate_expand_space = 1 - endif - call s:option_init("expand_space", 0) - " expand_cr - if exists("b:delimitMate_expand_cr") && type(b:delimitMate_expand_cr) == type("") - echom "b:delimitMate_expand_cr is '".b:delimitMate_expand_cr."' but it must be either 1 or 0!" - echom "Read :help 'delimitMate_expand_cr' for more details." - unlet b:delimitMate_expand_cr - let b:delimitMate_expand_cr = 1 - endif - if exists("g:delimitMate_expand_cr") && type(g:delimitMate_expand_cr) == type("") - echom "delimitMate_expand_cr is '".g:delimitMate_expand_cr."' but it must be either 1 or 0!" - echom "Read :help 'delimitMate_expand_cr' for more details." - unlet g:delimitMate_expand_cr - let g:delimitMate_expand_cr = 1 - endif - if ((&backspace !~ 'eol' || &backspace !~ 'start') && &backspace != 2) && - \ ((exists('b:delimitMate_expand_cr') && b:delimitMate_expand_cr == 1) || - \ (exists('g:delimitMate_expand_cr') && g:delimitMate_expand_cr == 1)) - echom "delimitMate: There seems to be some incompatibility with your settings that may interfer with the expansion of . See :help 'delimitMate_expand_cr' for details." - 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) - " smart_matchpairs - call s:option_init("smart_matchpairs", '^\%(\w\|\!\|£\|\$\|_\)') - " smart_quotes - " XXX: backward compatibility. Ugly, should go the way of the dodo soon. - let quotes = escape(join(s:g('quotes_list'), ''), '\-^[]') - let default_smart_quotes = '\%(\w\|[^[:punct:][:space:]' . quotes . ']\|\%(\\\\\)*\\\)\%#\|\%#\%(\w\|[^[:space:][:punct:]' . quotes . ']\)' - if exists('g:delimitMate_smart_quotes') && type(g:delimitMate_smart_quotes) == type(0) - if g:delimitMate_smart_quotes - unlet g:delimitMate_smart_quotes - else - unlet g:delimitMate_smart_quotes - let g:delimitMate_smart_quotes = '' - endif - endif - if exists('b:delimitMate_smart_quotes') && type(b:delimitMate_smart_quotes) == type(0) - if b:delimitMate_smart_quotes - unlet b:delimitMate_smart_quotes - if exists('g:delimitMate_smart_quotes') && type(g:delimitMate_smart_quotes) && g:delimitMate_smart_quotes - let b:delimitMate_smart_quotes = default_smart_quotes - endif - else - unlet b:delimitMate_smart_quotes - let b:delimitMate_smart_quotes = '' - endif - endif - call s:option_init("smart_quotes", default_smart_quotes) - " apostrophes - call s:option_init("apostrophes", "") - call s:option_init("apostrophes_list", split(s:g('apostrophes'), ":\s*")) - " tab2exit - call s:option_init("tab2exit", 1) - " balance_matchpairs - call s:option_init("balance_matchpairs", 0) - " eol marker - call s:option_init("eol_marker", "") - " Everything is fine. - return 1 + " autoclose + call s:option_init("autoclose", 1) + " matchpairs + call s:option_init("matchpairs", string(&matchpairs)[1:-2]) + call s:option_init("matchpairs_list", map(split(s:g('matchpairs'), ','), 'split(v:val, '':'')')) + let pairs = s:g('matchpairs_list') + if len(filter(pairs, 'v:val[0] ==# v:val[1]')) + echohl ErrorMsg + echom 'delimitMate: each member of a pair in delimitMate_matchpairs must be different from each other.' + echom 'delimitMate: invalid pairs: ' . join(map(pairs, 'join(v:val, ":")'), ', ') + echohl Normal + return 0 + endif + call s:option_init("left_delims", map(copy(s:g('matchpairs_list')), 'v:val[0]')) + call s:option_init("right_delims", map(copy(s:g('matchpairs_list')), 'v:val[1]')) + " quotes + call s:option_init("quotes", "\" ' `") + call s:option_init("quotes_list",split(s:g('quotes'), '\s\+')) + " nesting_quotes + call s:option_init("nesting_quotes", []) + " excluded_regions + call s:option_init("excluded_regions", "Comment") + call s:option_init("excluded_regions_list", split(s:g('excluded_regions'), ',\s*')) + let enabled = len(s:g('excluded_regions_list')) > 0 + call s:option_init("excluded_regions_enabled", enabled) + " expand_space + if exists("b:delimitMate_expand_space") && type(b:delimitMate_expand_space) == type("") + echom "b:delimitMate_expand_space is '".b:delimitMate_expand_space."' but it must be either 1 or 0!" + echom "Read :help 'delimitMate_expand_space' for more details." + unlet b:delimitMate_expand_space + let b:delimitMate_expand_space = 1 + endif + if exists("g:delimitMate_expand_space") && type(g:delimitMate_expand_space) == type("") + echom "delimitMate_expand_space is '".g:delimitMate_expand_space."' but it must be either 1 or 0!" + echom "Read :help 'delimitMate_expand_space' for more details." + unlet g:delimitMate_expand_space + let g:delimitMate_expand_space = 1 + endif + call s:option_init("expand_space", 0) + " expand_cr + if exists("b:delimitMate_expand_cr") && type(b:delimitMate_expand_cr) == type("") + echom "b:delimitMate_expand_cr is '".b:delimitMate_expand_cr."' but it must be either 1 or 0!" + echom "Read :help 'delimitMate_expand_cr' for more details." + unlet b:delimitMate_expand_cr + let b:delimitMate_expand_cr = 1 + endif + if exists("g:delimitMate_expand_cr") && type(g:delimitMate_expand_cr) == type("") + echom "delimitMate_expand_cr is '".g:delimitMate_expand_cr."' but it must be either 1 or 0!" + echom "Read :help 'delimitMate_expand_cr' for more details." + unlet g:delimitMate_expand_cr + let g:delimitMate_expand_cr = 1 + endif + if ((&backspace !~ 'eol' || &backspace !~ 'start') && &backspace != 2) && + \ ((exists('b:delimitMate_expand_cr') && b:delimitMate_expand_cr == 1) || + \ (exists('g:delimitMate_expand_cr') && g:delimitMate_expand_cr == 1)) + echom "delimitMate: There seems to be some incompatibility with your settings that may interfer with the expansion of . See :help 'delimitMate_expand_cr' for details." + 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) + " smart_matchpairs + call s:option_init("smart_matchpairs", '^\%(\w\|\!\|£\|\$\|_\)') + " smart_quotes + " XXX: backward compatibility. Ugly, should go the way of the dodo soon. + let quotes = escape(join(s:g('quotes_list'), ''), '\-^[]') + let default_smart_quotes = '\%(\w\|[^[:punct:][:space:]' . quotes . ']\|\%(\\\\\)*\\\)\%#\|\%#\%(\w\|[^[:space:][:punct:]' . quotes . ']\)' + if exists('g:delimitMate_smart_quotes') && type(g:delimitMate_smart_quotes) == type(0) + if g:delimitMate_smart_quotes + unlet g:delimitMate_smart_quotes + else + unlet g:delimitMate_smart_quotes + let g:delimitMate_smart_quotes = '' + endif + endif + if exists('b:delimitMate_smart_quotes') && type(b:delimitMate_smart_quotes) == type(0) + if b:delimitMate_smart_quotes + unlet b:delimitMate_smart_quotes + if exists('g:delimitMate_smart_quotes') && type(g:delimitMate_smart_quotes) && g:delimitMate_smart_quotes + let b:delimitMate_smart_quotes = default_smart_quotes + endif + else + unlet b:delimitMate_smart_quotes + let b:delimitMate_smart_quotes = '' + endif + endif + call s:option_init("smart_quotes", default_smart_quotes) + " apostrophes + call s:option_init("apostrophes", "") + call s:option_init("apostrophes_list", split(s:g('apostrophes'), ":\s*")) + " tab2exit + call s:option_init("tab2exit", 1) + " balance_matchpairs + call s:option_init("balance_matchpairs", 0) + " eol marker + call s:option_init("eol_marker", "") + " Everything is fine. + return 1 endfunction "}}} Init() function! s:g(...) " {{{ - return call('delimitMate#Get', a:000) + return call('delimitMate#Get', a:000) endfunction " }}} function! s:s(...) " {{{ - return call('delimitMate#Set', a:000) + return call('delimitMate#Set', a:000) endfunction " }}} function! s:Map() "{{{ - " Set mappings: - try - let save_keymap = &keymap - let save_iminsert = &iminsert - let save_imsearch = &imsearch - let save_cpo = &cpo - set keymap= - set cpo&vim - silent! doautocmd User delimitMate_map - if s:g('autoclose') - call s:AutoClose() - else - call s:NoAutoClose() - endif - call s:ExtraMappings() - finally - let &cpo = save_cpo - let &keymap = save_keymap - let &iminsert = save_iminsert - let &imsearch = save_imsearch - endtry + " Set mappings: + try + let save_keymap = &keymap + let save_iminsert = &iminsert + let save_imsearch = &imsearch + let save_cpo = &cpo + set keymap= + set cpo&vim + silent! doautocmd User delimitMate_map + if s:g('autoclose') + call s:AutoClose() + else + call s:NoAutoClose() + endif + call s:ExtraMappings() + finally + let &cpo = save_cpo + let &keymap = save_keymap + let &iminsert = save_iminsert + let &imsearch = save_imsearch + endtry - let b:delimitMate_enabled = 1 + let b:delimitMate_enabled = 1 endfunction "}}} Map() function! s:Unmap() " {{{ - let imaps = - \ s:g('right_delims', []) + - \ s:g('left_delims', []) + - \ s:g('quotes_list', []) + - \ s:g('apostrophes_list', []) + - \ ['', '', '', '', '', '', '', ''] + - \ ['', '', '', '', '', ''] + - \ ['', ''] + - \ ['', '', '', '', '', '', 'g'] + let imaps = + \ s:g('right_delims', []) + + \ s:g('left_delims', []) + + \ s:g('quotes_list', []) + + \ s:g('apostrophes_list', []) + + \ ['', '', '', '', '', '', '', ''] + + \ ['', '', '', '', '', ''] + + \ ['', ''] + + \ ['', '', '', '', '', '', 'g'] - for map in imaps - if maparg(map, "i") =~# '^delimitMate' - if map == '|' - let map = '' - endif - exec 'silent! iunmap ' . map - endif - endfor - silent! doautocmd User delimitMate_unmap - let b:delimitMate_enabled = 0 + for map in imaps + if maparg(map, "i") =~# '^delimitMate' + if map == '|' + let map = '' + endif + exec 'silent! iunmap ' . map + endif + endfor + silent! doautocmd User delimitMate_unmap + let b:delimitMate_enabled = 0 endfunction " }}} s:Unmap() function! s:TestMappingsDo() "{{{ - if &modified - let confirm = input("Modified buffer, type \"yes\" to write and proceed " - \ . "with test: ") ==? 'yes' - if !confirm - return - endif - endif - call delimitMate#TestMappings() - g/\%^$/d - 0 + if &modified + let confirm = input("Modified buffer, type \"yes\" to write and proceed " + \ . "with test: ") ==? 'yes' + if !confirm + return + endif + endif + call delimitMate#TestMappings() + g/\%^$/d + 0 endfunction "}}} function! s:setup(...) "{{{ - let swap = a:0 && a:1 == 2 - let enable = a:0 && a:1 - let disable = a:0 && !a:1 - " First, remove all magic, if needed: - if get(b:, 'delimitMate_enabled', 0) - call s:Unmap() - " Switch - if swap - echo "delimitMate is disabled." - return - endif - endif - if disable - " Just disable the mappings. - return - endif - if !a:0 - " Check if this file type is excluded: - if exists("g:delimitMate_excluded_ft") && - \ index(split(g:delimitMate_excluded_ft, ','), &filetype, 0, 1) >= 0 - " Finish here: - return 1 - endif - " Check if user tried to disable using b:loaded_delimitMate - if exists("b:loaded_delimitMate") - return 1 - endif - endif - " Initialize settings: - if ! s:init() - " Something went wrong. - return - endif - if enable || swap || !get(g:, 'delimitMate_offByDefault', 0) - " Now, add magic: - call s:Map() - if a:0 - echo "delimitMate is enabled." - endif - endif + let swap = a:0 && a:1 == 2 + let enable = a:0 && a:1 + let disable = a:0 && !a:1 + " First, remove all magic, if needed: + if get(b:, 'delimitMate_enabled', 0) + call s:Unmap() + " Switch + if swap + echo "delimitMate is disabled." + return + endif + endif + if disable + " Just disable the mappings. + return + endif + if !a:0 + " Check if this file type is excluded: + if exists("g:delimitMate_excluded_ft") && + \ index(split(g:delimitMate_excluded_ft, ','), &filetype, 0, 1) >= 0 + " Finish here: + return 1 + endif + " Check if user tried to disable using b:loaded_delimitMate + if exists("b:loaded_delimitMate") + return 1 + endif + endif + " Initialize settings: + if ! s:init() + " Something went wrong. + return + endif + if enable || swap || !get(g:, 'delimitMate_offByDefault', 0) + " Now, add magic: + call s:Map() + if a:0 + echo "delimitMate is enabled." + endif + endif endfunction "}}} function! s:TriggerAbb() "{{{ - if v:version < 703 - \ || ( v:version == 703 && !has('patch489') ) - \ || pumvisible() - return '' - endif - return "\" + if v:version < 703 + \ || ( v:version == 703 && !has('patch489') ) + \ || pumvisible() + return '' + endif + return "\" endfunction "}}} function! s:NoAutoClose() "{{{ - " inoremap ) =delimitMate#SkipDelim('\)') - for delim in s:g('right_delims') + s:g('quotes_list') - if delim == '|' - let delim = '' - endif - exec 'inoremap delimitMate' . delim . ' =TriggerAbb().delimitMate#SkipDelim("' . escape(delim,'"') . '")' - exec 'silent! imap '.delim.' delimitMate'.delim - endfor + " inoremap ) =delimitMate#SkipDelim('\)') + for delim in s:g('right_delims') + s:g('quotes_list') + if delim == '|' + let delim = '' + endif + exec 'inoremap delimitMate' . delim . ' =TriggerAbb().delimitMate#SkipDelim("' . escape(delim,'"') . '")' + exec 'silent! imap '.delim.' delimitMate'.delim + endfor endfunction "}}} function! s:AutoClose() "{{{ - " Add matching pair and jump to the midle: - " inoremap ( () - let i = 0 - while i < len(s:g('matchpairs_list')) - let ld = s:g('left_delims')[i] == '|' ? '' : s:g('left_delims')[i] - let rd = s:g('right_delims')[i] == '|' ? '' : s:g('right_delims')[i] - exec 'inoremap delimitMate' . ld - \. ' TriggerAbb().delimitMate#ParenDelim("' . escape(rd, '|') . '")' - exec 'silent! imap '.ld - \.' delimitMate'.ld - let i += 1 - endwhile + " Add matching pair and jump to the midle: + " inoremap ( () + let i = 0 + while i < len(s:g('matchpairs_list')) + let ld = s:g('left_delims')[i] == '|' ? '' : s:g('left_delims')[i] + let rd = s:g('right_delims')[i] == '|' ? '' : s:g('right_delims')[i] + exec 'inoremap delimitMate' . ld + \. ' TriggerAbb().delimitMate#ParenDelim("' . escape(rd, '|') . '")' + exec 'silent! imap '.ld + \.' delimitMate'.ld + let i += 1 + endwhile - " Exit from inside the matching pair: - for delim in s:g('right_delims') - let delim = delim == '|' ? '' : delim - exec 'inoremap delimitMate' . delim - \. ' TriggerAbb().delimitMate#JumpOut("\' . delim . '")' - exec 'silent! imap ' . delim - \. ' delimitMate'. delim - endfor + " Exit from inside the matching pair: + for delim in s:g('right_delims') + let delim = delim == '|' ? '' : delim + exec 'inoremap delimitMate' . delim + \. ' TriggerAbb().delimitMate#JumpOut("\' . delim . '")' + exec 'silent! imap ' . delim + \. ' delimitMate'. delim + endfor - " Add matching quote and jump to the midle, or exit if inside a pair of matching quotes: - " inoremap " =delimitMate#QuoteDelim("\"") - for delim in s:g('quotes_list') - if delim == '|' - let delim = '' - endif - exec 'inoremap delimitMate' . delim - \. ' TriggerAbb()."=delimitMate#QuoteDelim(\"\\\' . delim . '\")"' - exec 'silent! imap ' . delim - \. ' delimitMate' . delim - endfor + " Add matching quote and jump to the midle, or exit if inside a pair of matching quotes: + " inoremap " =delimitMate#QuoteDelim("\"") + for delim in s:g('quotes_list') + if delim == '|' + let delim = '' + endif + exec 'inoremap delimitMate' . delim + \. ' TriggerAbb()."=delimitMate#QuoteDelim(\"\\\' . delim . '\")"' + exec 'silent! imap ' . delim + \. ' delimitMate' . delim + endfor - " Try to fix the use of apostrophes (kept for backward compatibility): - " inoremap n't n't - for map in s:g('apostrophes_list') - exec "inoremap " . map . " " . map - exec 'silent! imap ' . map . ' delimitMate' . map - endfor + " Try to fix the use of apostrophes (kept for backward compatibility): + " inoremap n't n't + for map in s:g('apostrophes_list') + exec "inoremap " . map . " " . map + exec 'silent! imap ' . map . ' delimitMate' . map + endfor endfunction "}}} function! s:ExtraMappings() "{{{ - " If pair is empty, delete both delimiters: - inoremap delimitMateBS =delimitMate#BS() - if !hasmapto('delimitMateBS','i') - if maparg(''. 'i') == '' + " If pair is empty, delete both delimiters: + inoremap delimitMateBS =delimitMate#BS() + if !hasmapto('delimitMateBS','i') + if maparg(''. 'i') == '' silent! imap delimitMateBS endif - if maparg(''. 'i') == '' + if maparg(''. 'i') == '' silent! imap delimitMateBS endif - endif - " If pair is empty, delete closing delimiter: - inoremap delimitMateS-BS delimitMate#WithinEmptyPair() ? "\" : "\" - if !hasmapto('delimitMateS-BS','i') && maparg('', 'i') == '' - silent! imap delimitMateS-BS - endif - " Expand return if inside an empty pair: - inoremap delimitMateCR TriggerAbb()."\=delimitMate#ExpandReturn()\" - if s:g('expand_cr') && !hasmapto('delimitMateCR', 'i') && maparg('', 'i') == '' - silent! imap delimitMateCR - endif - " Expand space if inside an empty pair: - inoremap delimitMateSpace TriggerAbb()."\=delimitMate#ExpandSpace()\" - if s:g('expand_space') && !hasmapto('delimitMateSpace', 'i') && maparg('', 'i') == '' - silent! imap delimitMateSpace - endif - " Jump over any delimiter: - inoremap delimitMateS-Tab TriggerAbb()."\=delimitMate#JumpAny()\" - if s:g('tab2exit') && !hasmapto('delimitMateS-Tab', 'i') && maparg('', 'i') == '' - silent! imap delimitMateS-Tab - endif - " Jump over next delimiters - inoremap delimitMateJumpMany TriggerAbb()."\=delimitMate#JumpMany()\" - if !hasmapto('delimitMateJumpMany', 'i') && maparg("g", 'i') == '' - imap g delimitMateJumpMany - endif + endif + " If pair is empty, delete closing delimiter: + inoremap delimitMateS-BS delimitMate#WithinEmptyPair() ? "\" : "\" + if !hasmapto('delimitMateS-BS','i') && maparg('', 'i') == '' + silent! imap delimitMateS-BS + endif + " Expand return if inside an empty pair: + inoremap delimitMateCR TriggerAbb()."\=delimitMate#ExpandReturn()\" + if s:g('expand_cr') && !hasmapto('delimitMateCR', 'i') && maparg('', 'i') == '' + silent! imap delimitMateCR + endif + " Expand space if inside an empty pair: + inoremap delimitMateSpace TriggerAbb()."\=delimitMate#ExpandSpace()\" + if s:g('expand_space') && !hasmapto('delimitMateSpace', 'i') && maparg('', 'i') == '' + silent! imap delimitMateSpace + endif + " Jump over any delimiter: + inoremap delimitMateS-Tab TriggerAbb()."\=delimitMate#JumpAny()\" + if s:g('tab2exit') && !hasmapto('delimitMateS-Tab', 'i') && maparg('', 'i') == '' + silent! imap delimitMateS-Tab + endif + " Jump over next delimiters + inoremap delimitMateJumpMany TriggerAbb()."\=delimitMate#JumpMany()\" + if !hasmapto('delimitMateJumpMany', 'i') && maparg("g", 'i') == '' + imap g delimitMateJumpMany + endif endfunction "}}} "}}} @@ -379,18 +379,18 @@ command! -bar DelimitMateOff call s:setup(0) " Autocommands: {{{ augroup delimitMate - au! - " Run on file type change. - au FileType * call setup() + au! + " Run on file type change. + au FileType * call setup() - au VimEnter * call setup() + au VimEnter * call setup() - " Run on new buffers. - au BufNewFile,BufRead,BufEnter * - \ if !exists('b:delimitMate_was_here') | - \ call setup() | - \ let b:delimitMate_was_here = 1 | - \ endif + " Run on new buffers. + au BufNewFile,BufRead,BufEnter * + \ if !exists('b:delimitMate_was_here') | + \ call setup() | + \ let b:delimitMate_was_here = 1 | + \ endif augroup END "}}}