Rename functions.

This commit is contained in:
Israel Chauca Fuentes
2015-01-02 12:07:31 -05:00
parent bf2b68edab
commit aa9d737763

View File

@@ -6,13 +6,12 @@
" Manual: Read ":help delimitMate". " Manual: Read ":help delimitMate".
" ============================================================================ " ============================================================================
" Utilities {{{
"let delimitMate_loaded = 1 "let delimitMate_loaded = 1
if !exists('s:options') if !exists('s:options')
let s:options = {} let s:options = {}
endif endif
function! s:s(name, value, ...) "{{{ function! s:s(name, value, ...) "{{{
let scope = a:0 ? a:1 : 's' let scope = a:0 ? a:1 : 's'
let bufnr = bufnr('%') let bufnr = bufnr('%')
@@ -52,17 +51,9 @@ function! s:exists(name, ...) "{{{
return exists(scope . ':' . name) return exists(scope . ':' . name)
endfunction "}}} endfunction "}}}
function! delimitMate#Set(...) "{{{ function! s:is_jump(...) "{{{
return call('s:s', a:000)
endfunction "}}}
function! delimitMate#Get(...) "{{{
return call('s:g', a:000)
endfunction "}}}
function! delimitMate#ShouldJump(...) "{{{
" Returns 1 if the next character is a closing delimiter. " Returns 1 if the next character is a closing delimiter.
let char = delimitMate#GetCharFromCursor(0) let char = s:get_char(0)
let list = s:g('right_delims') + s:g('quotes_list') let list = s:g('right_delims') + s:g('quotes_list')
" Closing delimiter on the right. " Closing delimiter on the right.
@@ -72,7 +63,7 @@ function! delimitMate#ShouldJump(...) "{{{
endif endif
" Closing delimiter with space expansion. " Closing delimiter with space expansion.
let nchar = delimitMate#GetCharFromCursor(1) let nchar = s:get_char(1)
if !a:0 && s:g('expand_space') && char == " " if !a:0 && s:g('expand_space') && char == " "
if index(list, nchar) > -1 if index(list, nchar) > -1
return 2 return 2
@@ -97,40 +88,24 @@ function! delimitMate#ShouldJump(...) "{{{
return 0 return 0
endfunction "}}} endfunction "}}}
function! delimitMate#IsEmptyPair(str) "{{{ function! s:rquote(char) "{{{
if strlen(substitute(a:str, ".", "x", "g")) != 2 let pos = matchstr(getline('.')[col('.') : ], escape(a:char, '[]*.^$\'), 1)
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#RightQ(char) "{{{
let i = 0 let i = 0
while delimitMate#GetCharFromCursor(i) ==# a:char while s:get_char(i) ==# a:char
let i += 1 let i += 1
endwhile endwhile
return i return i
endfunction "}}} endfunction "}}}
function! delimitMate#LeftQ(char) "{{{ function! s:lquote(char) "{{{
let i = 0 let i = 0
while delimitMate#GetCharFromCursor(i - 1) ==# a:char while s:get_char(i - 1) ==# a:char
let i -= 1 let i -= 1
endwhile endwhile
return i * -1 return i * -1
endfunction "}}} endfunction "}}}
function! delimitMate#GetCharFromCursor(...) "{{{ function! s:get_char(...) "{{{
let idx = col('.') - 1 let idx = col('.') - 1
if !a:0 || (a:0 && a:1 >= 0) if !a:0 || (a:0 && a:1 >= 0)
" Get char from cursor. " Get char from cursor.
@@ -142,9 +117,9 @@ function! delimitMate#GetCharFromCursor(...) "{{{
let line = getline('.')[: idx - 1] let line = getline('.')[: idx - 1]
let pos = 0 - (1 + a:1) let pos = 0 - (1 + a:1)
return matchstr(line, '.\ze'.repeat('.', pos).'$') return matchstr(line, '.\ze'.repeat('.', pos).'$')
endfunction "delimitMate#GetCharFromCursor }}} endfunction "s:get_char }}}
function! delimitMate#IsCRExpansion(...) " {{{ function! s:is_cr_expansion(...) " {{{
let nchar = getline(line('.')-1)[-1:] let nchar = getline(line('.')-1)[-1:]
let schar = matchstr(getline(line('.')+1), '^\s*\zs\S') let schar = matchstr(getline(line('.')+1), '^\s*\zs\S')
let isEmpty = a:0 ? getline('.') =~ '^\s*$' : empty(getline('.')) let isEmpty = a:0 ? getline('.') =~ '^\s*$' : empty(getline('.'))
@@ -161,16 +136,16 @@ function! delimitMate#IsCRExpansion(...) " {{{
else else
return 0 return 0
endif endif
endfunction " }}} delimitMate#IsCRExpansion() endfunction " }}} s:is_cr_expansion()
function! delimitMate#IsSpaceExpansion() " {{{ function! s:is_space_expansion() " {{{
if col('.') > 2 if col('.') > 2
let pchar = delimitMate#GetCharFromCursor(-2) let pchar = s:get_char(-2)
let nchar = delimitMate#GetCharFromCursor(1) let nchar = s:get_char(1)
let isSpaces = let isSpaces =
\ (delimitMate#GetCharFromCursor(-1) \ (s:get_char(-1)
\ == delimitMate#GetCharFromCursor(0) \ == s:get_char(0)
\ && delimitMate#GetCharFromCursor(-1) == " ") \ && s:get_char(-1) == " ")
if index(s:g('left_delims'), pchar) > -1 && if index(s:g('left_delims'), pchar) > -1 &&
\ index(s:g('left_delims'), pchar) \ index(s:g('left_delims'), pchar)
@@ -187,89 +162,56 @@ function! delimitMate#IsSpaceExpansion() " {{{
return 0 return 0
endfunction " }}} IsSpaceExpansion() endfunction " }}} IsSpaceExpansion()
function! delimitMate#WithinEmptyPair() "{{{ function! s:is_empty_matchpair() "{{{
" if cursor is at column 1 return 0
if col('.') == 1
return 0
endif
" get char before the cursor. " get char before the cursor.
let char1 = delimitMate#GetCharFromCursor(-1) let open = s:get_char(-1)
" get char under the cursor.
let char2 = delimitMate#GetCharFromCursor(0)
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) let idx = index(s:g('left_delims'), open)
if idx == -1 if idx == -1
return 0 return 0
endif endif
let close = get(s:g('right_delims'), idx, '') let close = get(s:g('right_delims'), idx, '')
return close ==# delimitMate#GetCharFromCursor(0) return close ==# s:get_char(0)
endfunction "}}} endfunction "}}}
function! delimitMate#WithinEmptyQuotes() "{{{ function! s:is_empty_quotes() "{{{
" get char before the cursor. " get char before the cursor.
let quote = delimitMate#GetCharFromCursor(-1) let quote = s:get_char(-1)
let idx = index(s:g('quotes_list'), quote) let idx = index(s:g('quotes_list'), quote)
if idx == -1 if idx == -1
return 0 return 0
endif endif
return quote ==# delimitMate#GetCharFromCursor(0) return quote ==# s:get_char(0)
endfunction "}}} endfunction "}}}
function! delimitMate#CursorIdx() "{{{ function! s:cursor_idx() "{{{
let idx = len(split(getline('.')[: col('.') - 1], '\zs')) - 1 let idx = len(split(getline('.')[: col('.') - 1], '\zs')) - 1
return idx return idx
endfunction "delimitMate#CursorCol }}} endfunction "delimitMate#CursorCol }}}
function! delimitMate#GetSyntaxRegion(line, col) "{{{ function! s:get_syn_name() "{{{
return synIDattr(synIDtrans(synID(a:line, a:col, 1)), 'name')
endfunction " }}}
function! delimitMate#GetCurrentSyntaxRegion() "{{{
let col = col('.') let col = col('.')
if col == col('$') if col == col('$')
let col = col - 1 let col = col - 1
endif endif
return delimitMate#GetSyntaxRegion(line('.'), col) return synIDattr(synIDtrans(synID(line('.'), col, 1)), 'name')
endfunction " }}} endfunction " }}}
function! delimitMate#GetCurrentSyntaxRegionIf(char) "{{{ function! s:is_forbidden(char) "{{{
let col = col('.')
let origin_line = getline('.')
let changed_line = strpart(origin_line, 0, col - 1) . a:char
\ . strpart(origin_line, col - 1)
call setline('.', changed_line)
let region = delimitMate#GetSyntaxRegion(line('.'), col)
call setline('.', origin_line)
return region
endfunction "}}}
function! delimitMate#IsForbidden(char) "{{{
if !s:g('excluded_regions_enabled') if !s:g('excluded_regions_enabled')
return 0 return 0
endif endif
let region = delimitMate#GetCurrentSyntaxRegion() let region = s:get_syn_name()
"if index(s:g('excluded_regions_list'), region) >= 0
" "echom "Forbidden 1!"
" return 1
"endif
"let region = delimitMate#GetCurrentSyntaxRegionIf(a:char)
"echom "Forbidden 2!"
return index(s:g('excluded_regions_list'), region) >= 0 return index(s:g('excluded_regions_list'), region) >= 0
endfunction "}}} endfunction "}}}
function! delimitMate#BalancedParens(char) "{{{ function! s:balance_matchpairs(char) "{{{
" Returns: " Returns:
" = 0 => Parens balanced. " = 0 => Parens balanced.
" > 0 => More opening parens. " > 0 => More opening parens.
" < 0 => More closing parens. " < 0 => More closing parens.
let line = getline('.') let line = getline('.')
let col = delimitMate#CursorIdx() - 1 let col = s:cursor_idx() - 1
let col = col >= 0 ? col : 0 let col = col >= 0 ? col : 0
let list = split(line, '\zs') let list = split(line, '\zs')
let left = s:g('left_delims')[index(s:g('right_delims'), a:char)] let left = s:g('left_delims')[index(s:g('right_delims'), a:char)]
@@ -299,7 +241,7 @@ function! delimitMate#BalancedParens(char) "{{{
return opening - closing return opening - closing
endfunction "}}} endfunction "}}}
function! delimitMate#IsSmartQuote(char) "{{{ function! s:is_smart_quote(char) "{{{
" TODO: Allow using a:char in the pattern. " TODO: Allow using a:char in the pattern.
let tmp = s:g('smart_quotes') let tmp = s:g('smart_quotes')
if empty(tmp) if empty(tmp)
@@ -315,20 +257,58 @@ function! delimitMate#IsSmartQuote(char) "{{{
return result return result
endfunction "delimitMate#SmartQuote }}} endfunction "delimitMate#SmartQuote }}}
" }}} function! delimitMate#Set(...) "{{{
return call('s:s', a:000)
endfunction "}}}
function! delimitMate#Get(...) "{{{
return call('s:g', a:000)
endfunction "}}}
function! delimitMate#ShouldJump(...) "{{{
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
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 )
endfunction "}}}
" Doers {{{
function! delimitMate#SkipDelim(char) "{{{ function! delimitMate#SkipDelim(char) "{{{
if delimitMate#IsForbidden(a:char) if s:is_forbidden(a:char)
return a:char return a:char
endif endif
let col = col('.') - 1 let col = col('.') - 1
let line = getline('.') let line = getline('.')
if col > 0 if col > 0
let cur = delimitMate#GetCharFromCursor(0) let cur = s:get_char(0)
let pre = delimitMate#GetCharFromCursor(-1) let pre = s:get_char(-1)
else else
let cur = delimitMate#GetCharFromCursor(0) let cur = s:get_char(0)
let pre = "" let pre = ""
endif endif
if pre == "\\" if pre == "\\"
@@ -348,12 +328,12 @@ endfunction "}}}
function! delimitMate#ParenDelim(right) " {{{ function! delimitMate#ParenDelim(right) " {{{
let left = s:g('left_delims')[index(s:g('right_delims'),a:right)] let left = s:g('left_delims')[index(s:g('right_delims'),a:right)]
if delimitMate#IsForbidden(a:right) if s:is_forbidden(a:right)
return left return left
endif endif
" Try to balance matchpairs " Try to balance matchpairs
if s:g('balance_matchpairs') && if s:g('balance_matchpairs') &&
\ delimitMate#BalancedParens(a:right) < 0 \ s:balance_matchpairs(a:right) < 0
return left return left
endif endif
let line = getline('.') let line = getline('.')
@@ -373,16 +353,16 @@ function! delimitMate#ParenDelim(right) " {{{
endfunction " }}} endfunction " }}}
function! delimitMate#QuoteDelim(char) "{{{ function! delimitMate#QuoteDelim(char) "{{{
if delimitMate#IsForbidden(a:char) if s:is_forbidden(a:char)
return a:char return a:char
endif endif
let char_at = delimitMate#GetCharFromCursor(0) let char_at = s:get_char(0)
let char_before = delimitMate#GetCharFromCursor(-1) let char_before = s:get_char(-1)
let nesting_on = index(s:g('nesting_quotes'), a:char) > -1 let nesting_on = index(s:g('nesting_quotes'), a:char) > -1
let left_q = nesting_on ? delimitMate#LeftQ(a:char) : 0 let left_q = nesting_on ? s:lquote(a:char) : 0
if nesting_on && left_q > 1 if nesting_on && left_q > 1
" Nesting quotes. " Nesting quotes.
let right_q = delimitMate#RightQ(a:char) let right_q = s:rquote(a:char)
let quotes = right_q > left_q + 1 ? 0 : left_q - right_q + 2 let quotes = right_q > left_q + 1 ? 0 : left_q - right_q + 2
let lefts = quotes - 1 let lefts = quotes - 1
return repeat(a:char, quotes) . repeat("\<Left>", lefts) return repeat(a:char, quotes) . repeat("\<Left>", lefts)
@@ -393,7 +373,7 @@ function! delimitMate#QuoteDelim(char) "{{{
" If we are in a vim file and it looks like we're starting a comment, do " If we are in a vim file and it looks like we're starting a comment, do
" not add a closing char. " not add a closing char.
return a:char return a:char
elseif delimitMate#IsSmartQuote(a:char) elseif s:is_smart_quote(a:char)
" Seems like a smart quote, insert a single char. " Seems like a smart quote, insert a single char.
return a:char return a:char
elseif (char_before == a:char && char_at != a:char) elseif (char_before == a:char && char_at != a:char)
@@ -415,10 +395,10 @@ function! delimitMate#QuoteDelim(char) "{{{
endfunction "}}} endfunction "}}}
function! delimitMate#JumpOut(char) "{{{ function! delimitMate#JumpOut(char) "{{{
if delimitMate#IsForbidden(a:char) if s:is_forbidden(a:char)
return a:char return a:char
endif endif
let jump = delimitMate#ShouldJump(a:char) let jump = s:is_jump(a:char)
if jump == 1 if jump == 1
" HACK: Instead of <Right>, we remove the char to be jumped over and " HACK: Instead of <Right>, we remove the char to be jumped over and
" insert it again. This will trigger re-indenting via 'indentkeys'. " insert it again. This will trigger re-indenting via 'indentkeys'.
@@ -434,14 +414,14 @@ function! delimitMate#JumpOut(char) "{{{
endfunction " }}} endfunction " }}}
function! delimitMate#JumpAny(...) " {{{ function! delimitMate#JumpAny(...) " {{{
if delimitMate#IsForbidden('') if s:is_forbidden('')
return '' return ''
endif endif
if !delimitMate#ShouldJump() if !s:is_jump()
return '' return ''
endif endif
" Let's get the character on the right. " Let's get the character on the right.
let char = delimitMate#GetCharFromCursor(0) let char = s:get_char(0)
if char == " " if char == " "
" Space expansion. " Space expansion.
return "\<Right>\<Right>" return "\<Right>\<Right>"
@@ -476,23 +456,23 @@ function! delimitMate#JumpMany() " {{{
endfunction " delimitMate#JumpMany() }}} endfunction " delimitMate#JumpMany() }}}
function! delimitMate#ExpandReturn() "{{{ function! delimitMate#ExpandReturn() "{{{
if delimitMate#IsForbidden("") if s:is_forbidden("")
return "\<CR>" return "\<CR>"
endif endif
let escaped = delimitMate#CursorIdx() >= 2 let escaped = s:cursor_idx() >= 2
\ && delimitMate#GetCharFromCursor(-2) == '\' \ && s:get_char(-2) == '\'
let expand_right_matchpair = s:g('expand_cr') == 2 let expand_right_matchpair = s:g('expand_cr') == 2
\ && index(s:g('right_delims'), delimitMate#GetCharFromCursor(0)) > -1 \ && index(s:g('right_delims'), s:get_char(0)) > -1
let expand_inside_quotes = s:g('expand_inside_quotes') let expand_inside_quotes = s:g('expand_inside_quotes')
\ && delimitMate#WithinEmptyQuotes() \ && s:is_empty_quotes()
\ && !escaped \ && !escaped
if !pumvisible() if !pumvisible()
\ && (delimitMate#WithinEmptyMatchpair() \ && (s:is_empty_matchpair()
\ || expand_right_matchpair \ || expand_right_matchpair
\ || expand_inside_quotes) \ || expand_inside_quotes)
let val = "\<Esc>a\<CR>" let val = "\<Esc>a\<CR>"
if &smartindent && !&cindent && !&indentexpr if &smartindent && !&cindent && !&indentexpr
\ && delimitMate#GetCharFromCursor(0) == '}' \ && s:get_char(0) == '}'
" indentation is controlled by 'smartindent', and the first character on " indentation is controlled by 'smartindent', and the first character on
" the new line is '}'. If this were typed manually it would reindent to " the new line is '}'. If this were typed manually it would reindent to
" match the current line. Let's reproduce that behavior. " match the current line. Let's reproduce that behavior.
@@ -511,15 +491,15 @@ function! delimitMate#ExpandReturn() "{{{
endfunction "}}} endfunction "}}}
function! delimitMate#ExpandSpace() "{{{ function! delimitMate#ExpandSpace() "{{{
if delimitMate#IsForbidden("\<Space>") if s:is_forbidden("\<Space>")
return "\<Space>" return "\<Space>"
endif endif
let escaped = delimitMate#CursorIdx() >= 2 let escaped = s:cursor_idx() >= 2
\ && delimitMate#GetCharFromCursor(-2) == '\' \ && s:get_char(-2) == '\'
let expand_inside_quotes = s:g('expand_inside_quotes') let expand_inside_quotes = s:g('expand_inside_quotes')
\ && delimitMate#WithinEmptyQuotes() \ && s:is_empty_quotes()
\ && !escaped \ && !escaped
if delimitMate#WithinEmptyMatchpair() || expand_inside_quotes if s:is_empty_matchpair() || expand_inside_quotes
" Expand: " Expand:
return "\<Space>\<Space>\<Left>" return "\<Space>\<Space>\<Left>"
else else
@@ -528,15 +508,15 @@ function! delimitMate#ExpandSpace() "{{{
endfunction "}}} endfunction "}}}
function! delimitMate#BS() " {{{ function! delimitMate#BS() " {{{
if delimitMate#IsForbidden("") if s:is_forbidden("")
let extra = '' let extra = ''
elseif &bs !~ 'start\|2' elseif &bs !~ 'start\|2'
let extra = '' let extra = ''
elseif delimitMate#WithinEmptyPair() elseif delimitMate#WithinEmptyPair()
let extra = "\<Del>" let extra = "\<Del>"
elseif delimitMate#IsSpaceExpansion() elseif s:is_space_expansion()
let extra = "\<Del>" let extra = "\<Del>"
elseif delimitMate#IsCRExpansion() elseif s:is_cr_expansion()
let extra = repeat("\<Del>", let extra = repeat("\<Del>",
\ len(matchstr(getline(line('.') + 1), '^\s*\S'))) \ len(matchstr(getline(line('.') + 1), '^\s*\S')))
else else
@@ -545,9 +525,6 @@ function! delimitMate#BS() " {{{
return "\<BS>" . extra return "\<BS>" . extra
endfunction " }}} delimitMate#BS() endfunction " }}} delimitMate#BS()
" }}}
" Tools: {{{
function! delimitMate#TestMappings() "{{{ function! delimitMate#TestMappings() "{{{
echom 1 echom 1
%d %d
@@ -690,6 +667,5 @@ function! delimitMate#TestMappings() "{{{
setlocal nowrap setlocal nowrap
call feedkeys("\<Esc>\<Esc>", 'n') call feedkeys("\<Esc>\<Esc>", 'n')
endfunction "}}} endfunction "}}}
"}}}
" vim:foldmethod=marker:foldcolumn=4:ts=2:sw=2 " vim:foldmethod=marker:foldcolumn=4:ts=2:sw=2