mirror of
https://github.com/Raimondi/delimitMate.git
synced 2026-05-20 07:19:53 +08:00
Sort things a bit.
This commit is contained in:
+157
-160
@@ -1,3 +1,4 @@
|
|||||||
|
" Script variables {{{1
|
||||||
let s:defaults = {}
|
let s:defaults = {}
|
||||||
let s:defaults.delimitMate_pairs = ['()', '[]', '{}']
|
let s:defaults.delimitMate_pairs = ['()', '[]', '{}']
|
||||||
let s:defaults.delimitMate_quotes = ['"', "'", '`']
|
let s:defaults.delimitMate_quotes = ['"', "'", '`']
|
||||||
@@ -32,7 +33,11 @@ let s:defaults.delimitMate_smart_quotes_base = s:exprs
|
|||||||
|
|
||||||
unlet s:exprs
|
unlet s:exprs
|
||||||
|
|
||||||
function! s:defaults.consolidate()
|
let s:info = {}
|
||||||
|
let s:info.char = ''
|
||||||
|
let s:info.template = {}
|
||||||
|
|
||||||
|
function! s:defaults.consolidate() "{{{1
|
||||||
let g = filter(copy(g:), 'v:key =~# "^delimitMate_"')
|
let g = filter(copy(g:), 'v:key =~# "^delimitMate_"')
|
||||||
let b = filter(copy(b:), 'v:key =~# "^delimitMate_"')
|
let b = filter(copy(b:), 'v:key =~# "^delimitMate_"')
|
||||||
call extend(g, b, 'force')
|
call extend(g, b, 'force')
|
||||||
@@ -42,7 +47,7 @@ function! s:defaults.consolidate()
|
|||||||
return short_options
|
return short_options
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:balance_pairs(pair, info, opts)
|
function! s:balance_pairs(pair, info, opts) "{{{1
|
||||||
let left = strcharpart(a:pair, 0, 1)
|
let left = strcharpart(a:pair, 0, 1)
|
||||||
let right = strcharpart(a:pair, 1, 1)
|
let right = strcharpart(a:pair, 1, 1)
|
||||||
let behind = matchstr(a:info.cur.behind, '['.escape(left, '\^[]').'].*')
|
let behind = matchstr(a:info.cur.behind, '['.escape(left, '\^[]').'].*')
|
||||||
@@ -64,16 +69,12 @@ function! s:balance_pairs(pair, info, opts)
|
|||||||
return lefts - rights
|
return lefts - rights
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
let s:info = {}
|
function! s:info.template.is_escaped(...) "{{{1
|
||||||
let s:info.char = ''
|
|
||||||
let s:info.template = {}
|
|
||||||
|
|
||||||
function! s:info.template.is_escaped(...)
|
|
||||||
let str = a:0 ? a1 : self.behind
|
let str = a:0 ? a1 : self.behind
|
||||||
return len(matchstr(str, '\\*$')) % 2
|
return len(matchstr(str, '\\*$')) % 2
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:option(name, ...)
|
function! s:option(name, ...) "{{{1
|
||||||
if a:0
|
if a:0
|
||||||
let opt = get(a:1, 'delimitMate_' . a:name, '')
|
let opt = get(a:1, 'delimitMate_' . a:name, '')
|
||||||
else
|
else
|
||||||
@@ -87,153 +88,11 @@ function! s:option(name, ...)
|
|||||||
return opt
|
return opt
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! delimitMate#option(name)
|
function! s:synstack(lnum, col) "{{{1
|
||||||
return s:option(a:name)
|
|
||||||
endfunction
|
|
||||||
|
|
||||||
function! s:synstack(lnum, col)
|
|
||||||
return map(synstack(a:lnum, a:col), 'synIDattr(v:val, "name")') + [synIDattr(synIDtrans(synID(line('.'), col('.'), 1)), 'name')]
|
return map(synstack(a:lnum, a:col), 'synIDattr(v:val, "name")') + [synIDattr(synIDtrans(synID(line('.'), col('.'), 1)), 'name')]
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! delimitMate#ex_cmd(global, action)
|
function! s:get_info(...) "{{{1
|
||||||
let scope = a:global ? g: : b:
|
|
||||||
if a:action ==# 'enable'
|
|
||||||
let scope.delimitMate_enabled = 1
|
|
||||||
elseif a:action ==# 'disable'
|
|
||||||
let scope.delimitMate_enabled = 0
|
|
||||||
elseif a:action ==# 'switch'
|
|
||||||
let scope.delimitMate_enabled = !s:option('enabled')
|
|
||||||
endif
|
|
||||||
endfunction
|
|
||||||
|
|
||||||
function! delimitMate#InsertCharPre(str)
|
|
||||||
if s:info.skip_icp
|
|
||||||
" iabbrev fires this event for every char and the trigger
|
|
||||||
echom 11
|
|
||||||
return 0
|
|
||||||
endif
|
|
||||||
let s:info.skip_icp = 1
|
|
||||||
if !s:option('enabled')
|
|
||||||
echom 12
|
|
||||||
return 0
|
|
||||||
endif
|
|
||||||
if !empty(filter(s:option('excluded_regions'), 'index(s:synstack(line("."), col(".")), v:val) >= 0'))
|
|
||||||
echom 13
|
|
||||||
return 0
|
|
||||||
endif
|
|
||||||
return map(split(a:str, '\zs'), 's:handle_vchar(v:val)')
|
|
||||||
endfunction
|
|
||||||
|
|
||||||
function! s:handle_vchar(str)
|
|
||||||
echom 'ICP ' . string(a:str) . ': ' . get(s:info, 'cur', {'text': ''}).text
|
|
||||||
let s:info.char = a:str
|
|
||||||
let opts = s:defaults.consolidate()
|
|
||||||
if s:info.cur.is_escaped()
|
|
||||||
echom 12
|
|
||||||
return
|
|
||||||
elseif a:str == ' '
|
|
||||||
echom 13
|
|
||||||
let keys = s:keys4space(s:info, opts)
|
|
||||||
elseif a:str == "\<C-]>"
|
|
||||||
echom 14
|
|
||||||
return 0
|
|
||||||
elseif !empty(filter(copy(opts.quotes), 'v:val ==# a:str'))
|
|
||||||
echom 15
|
|
||||||
let keys = s:keys4quote(a:str, s:info, opts)
|
|
||||||
elseif !empty(filter(copy(opts.pairs), 'strcharpart(v:val, 0, 1) ==# a:str'))
|
|
||||||
echom 16
|
|
||||||
let pair = get(filter(copy(opts.pairs), 'strcharpart(v:val, 0, 1) ==# a:str'), 0, '')
|
|
||||||
let keys = s:keys4left(a:str, pair, s:info, opts)
|
|
||||||
"echom strtrans(keys)
|
|
||||||
"echom string(pair)
|
|
||||||
elseif !empty(filter(copy(opts.pairs), 'strcharpart(v:val, 1, 1) ==# a:str'))
|
|
||||||
let pair = get(filter(copy(opts.pairs), 'strcharpart(v:val, 1, 1) ==# a:str'), 0, '')
|
|
||||||
let keys = s:keys4right(a:str, pair, s:info, opts)
|
|
||||||
echom 17
|
|
||||||
echom keys
|
|
||||||
else
|
|
||||||
echom 18
|
|
||||||
return 0
|
|
||||||
endif
|
|
||||||
return feedkeys(keys, 'mti')
|
|
||||||
endfunction
|
|
||||||
|
|
||||||
function! s:keys4space(info, opts)
|
|
||||||
if !a:opts.expand_space || empty(filter(copy(a:opts.pairs), 'v:val ==# a:info.cur.around'))
|
|
||||||
return ''
|
|
||||||
endif
|
|
||||||
return " \<C-G>U\<Left>"
|
|
||||||
endfunction
|
|
||||||
|
|
||||||
function! s:keys4left(char, pair, info, opts)
|
|
||||||
if !a:opts.autoclose
|
|
||||||
echom 31
|
|
||||||
return ''
|
|
||||||
endif
|
|
||||||
let exprs = a:opts.smart_pairs_base + a:opts.smart_pairs_extra
|
|
||||||
if a:opts.smart_pairs && s:any_is_true(exprs, a:info, a:opts)
|
|
||||||
echom 32
|
|
||||||
return ''
|
|
||||||
endif
|
|
||||||
if a:opts.balance_pairs && s:balance_pairs(a:pair, a:info, a:opts) < 0
|
|
||||||
echom 33
|
|
||||||
return ''
|
|
||||||
endif
|
|
||||||
echom 34
|
|
||||||
return strcharpart(a:pair, 1, 1) . "\<C-G>U\<Left>"
|
|
||||||
endfunction
|
|
||||||
|
|
||||||
function! s:keys4right(char, pair, info, opts)
|
|
||||||
if !a:opts.autoclose
|
|
||||||
if s:info.cur.around == a:pair
|
|
||||||
return "\<Del>"
|
|
||||||
elseif s:info.cur.p_char == strcharpart(a:pair, 0, 1)
|
|
||||||
return "\<C-G>U\<Left>"
|
|
||||||
endif
|
|
||||||
return ""
|
|
||||||
endif
|
|
||||||
if strcharpart(a:info.cur.text[a:info.cur.col - 1 :], 0, 1) ==# a:char
|
|
||||||
echom 41
|
|
||||||
return "\<Del>"
|
|
||||||
endif
|
|
||||||
return ''
|
|
||||||
endfunction
|
|
||||||
|
|
||||||
function! s:keys4quote(char, info, opts)
|
|
||||||
let quotes_behind = strchars(matchstr(a:info.cur.behind, '['.escape(a:char, '\^[]').']*$'))
|
|
||||||
let quotes_ahead = strchars(matchstr(a:info.cur.ahead, '^['.escape(a:char, '\^[]').']*'))
|
|
||||||
echom 'k4q: ' quotes_behind . ' - ' . quotes_ahead
|
|
||||||
echom string(a:opts.nesting_quotes)
|
|
||||||
if a:opts.autoclose && index(a:opts.nesting_quotes, a:char) >= 0
|
|
||||||
\&& quotes_behind > 1
|
|
||||||
let add2right = quotes_ahead > quotes_behind + 1 ? 0 : quotes_behind - quotes_ahead + 1
|
|
||||||
echom 51
|
|
||||||
echom add2right
|
|
||||||
return repeat(a:char, add2right) . repeat("\<C-G>U\<Left>", add2right)
|
|
||||||
endif
|
|
||||||
if a:info.cur.n_char ==# a:char
|
|
||||||
echom 53
|
|
||||||
return "\<Del>"
|
|
||||||
endif
|
|
||||||
let exprs = a:opts.smart_quotes_base + a:opts.smart_quotes_extra
|
|
||||||
if a:opts.autoclose && a:opts.smart_quotes
|
|
||||||
\&& s:any_is_true(exprs, a:info, a:opts)
|
|
||||||
echom 52
|
|
||||||
return ''
|
|
||||||
endif
|
|
||||||
if !a:opts.autoclose && quotes_behind
|
|
||||||
echom 54
|
|
||||||
return "\<Left>"
|
|
||||||
endif
|
|
||||||
if !a:opts.autoclose
|
|
||||||
echom 55
|
|
||||||
return ''
|
|
||||||
endif
|
|
||||||
echom 59
|
|
||||||
return a:char . "\<C-G>U\<Left>"
|
|
||||||
endfunction
|
|
||||||
|
|
||||||
function! s:get_info(...)
|
|
||||||
if a:0
|
if a:0
|
||||||
let d = a:1
|
let d = a:1
|
||||||
else
|
else
|
||||||
@@ -252,7 +111,7 @@ function! s:get_info(...)
|
|||||||
return d
|
return d
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:any_is_true(expressions, info, options)
|
function! s:any_is_true(expressions, info, options) "{{{1
|
||||||
let char = a:info.char
|
let char = a:info.char
|
||||||
let info = deepcopy(a:info)
|
let info = deepcopy(a:info)
|
||||||
let options = deepcopy(a:options)
|
let options = deepcopy(a:options)
|
||||||
@@ -269,7 +128,22 @@ function! s:any_is_true(expressions, info, options)
|
|||||||
return !empty(exprs)
|
return !empty(exprs)
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! delimitMate#CursorMovedI(...)
|
function! delimitMate#option(name) "{{{1
|
||||||
|
return s:option(a:name)
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! delimitMate#ex_cmd(global, action) "{{{1
|
||||||
|
let scope = a:global ? g: : b:
|
||||||
|
if a:action ==# 'enable'
|
||||||
|
let scope.delimitMate_enabled = 1
|
||||||
|
elseif a:action ==# 'disable'
|
||||||
|
let scope.delimitMate_enabled = 0
|
||||||
|
elseif a:action ==# 'switch'
|
||||||
|
let scope.delimitMate_enabled = !s:option('enabled')
|
||||||
|
endif
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! delimitMate#CursorMovedI(...) "{{{1
|
||||||
let s:info.prev = s:info.cur
|
let s:info.prev = s:info.cur
|
||||||
let s:info.cur = call('s:get_info', a:000)
|
let s:info.cur = call('s:get_info', a:000)
|
||||||
let s:info.skip_icp = 0
|
let s:info.skip_icp = 0
|
||||||
@@ -277,19 +151,15 @@ function! delimitMate#CursorMovedI(...)
|
|||||||
echom 'CMI: ' . s:info.prev.text
|
echom 'CMI: ' . s:info.prev.text
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! delimitMate#InsertEnter(...)
|
function! delimitMate#InsertEnter(...) "{{{1
|
||||||
let s:info.cur = call('s:get_info', a:000)
|
let s:info.cur = call('s:get_info', a:000)
|
||||||
let s:info.prev = {}
|
let s:info.prev = {}
|
||||||
let s:info.skip_icp = 0
|
let s:info.skip_icp = 0
|
||||||
echom 'IE: ' . s:info.cur.text
|
echom 'IE: ' . s:info.cur.text
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! delimitMate#TextChangedI(...)
|
function! delimitMate#TextChangedI(...) "{{{1
|
||||||
echom 'TCI: ' . s:info.cur.text
|
echom 'TCI: ' . s:info.cur.text
|
||||||
call s:is_bs()
|
|
||||||
endfunction
|
|
||||||
|
|
||||||
function! s:is_bs()
|
|
||||||
if !s:option('enabled')
|
if !s:option('enabled')
|
||||||
echom 21
|
echom 21
|
||||||
return
|
return
|
||||||
@@ -323,3 +193,130 @@ function! s:is_bs()
|
|||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
" vim: sw=2 et
|
" vim: sw=2 et
|
||||||
|
function! delimitMate#InsertCharPre(str) "{{{1
|
||||||
|
if s:info.skip_icp
|
||||||
|
" iabbrev fires this event for every char and the trigger
|
||||||
|
echom 11
|
||||||
|
return 0
|
||||||
|
endif
|
||||||
|
let s:info.skip_icp = 1
|
||||||
|
if !s:option('enabled')
|
||||||
|
echom 12
|
||||||
|
return 0
|
||||||
|
endif
|
||||||
|
if !empty(filter(s:option('excluded_regions'), 'index(s:synstack(line("."), col(".")), v:val) >= 0'))
|
||||||
|
echom 13
|
||||||
|
return 0
|
||||||
|
endif
|
||||||
|
return map(split(a:str, '\zs'), 's:handle_vchar(v:val)')
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:handle_vchar(str) "{{{1
|
||||||
|
echom 'ICP ' . string(a:str) . ': ' . get(s:info, 'cur', {'text': ''}).text
|
||||||
|
let s:info.char = a:str
|
||||||
|
let opts = s:defaults.consolidate()
|
||||||
|
if s:info.cur.is_escaped()
|
||||||
|
echom 12
|
||||||
|
return
|
||||||
|
elseif a:str == ' '
|
||||||
|
echom 13
|
||||||
|
let keys = s:keys4space(s:info, opts)
|
||||||
|
elseif a:str == "\<C-]>"
|
||||||
|
echom 14
|
||||||
|
return 0
|
||||||
|
elseif !empty(filter(copy(opts.quotes), 'v:val ==# a:str'))
|
||||||
|
echom 15
|
||||||
|
let keys = s:keys4quote(a:str, s:info, opts)
|
||||||
|
elseif !empty(filter(copy(opts.pairs), 'strcharpart(v:val, 0, 1) ==# a:str'))
|
||||||
|
echom 16
|
||||||
|
let pair = get(filter(copy(opts.pairs), 'strcharpart(v:val, 0, 1) ==# a:str'), 0, '')
|
||||||
|
let keys = s:keys4left(a:str, pair, s:info, opts)
|
||||||
|
"echom strtrans(keys)
|
||||||
|
"echom string(pair)
|
||||||
|
elseif !empty(filter(copy(opts.pairs), 'strcharpart(v:val, 1, 1) ==# a:str'))
|
||||||
|
let pair = get(filter(copy(opts.pairs), 'strcharpart(v:val, 1, 1) ==# a:str'), 0, '')
|
||||||
|
let keys = s:keys4right(a:str, pair, s:info, opts)
|
||||||
|
echom 17
|
||||||
|
echom keys
|
||||||
|
else
|
||||||
|
echom 18
|
||||||
|
return 0
|
||||||
|
endif
|
||||||
|
return feedkeys(keys, 'mti')
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:keys4space(info, opts) "{{{1
|
||||||
|
if !a:opts.expand_space || empty(filter(copy(a:opts.pairs), 'v:val ==# a:info.cur.around'))
|
||||||
|
return ''
|
||||||
|
endif
|
||||||
|
return " \<C-G>U\<Left>"
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:keys4left(char, pair, info, opts) "{{{1
|
||||||
|
if !a:opts.autoclose
|
||||||
|
echom 31
|
||||||
|
return ''
|
||||||
|
endif
|
||||||
|
let exprs = a:opts.smart_pairs_base + a:opts.smart_pairs_extra
|
||||||
|
if a:opts.smart_pairs && s:any_is_true(exprs, a:info, a:opts)
|
||||||
|
echom 32
|
||||||
|
return ''
|
||||||
|
endif
|
||||||
|
if a:opts.balance_pairs && s:balance_pairs(a:pair, a:info, a:opts) < 0
|
||||||
|
echom 33
|
||||||
|
return ''
|
||||||
|
endif
|
||||||
|
echom 34
|
||||||
|
return strcharpart(a:pair, 1, 1) . "\<C-G>U\<Left>"
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:keys4right(char, pair, info, opts) "{{{1
|
||||||
|
if !a:opts.autoclose
|
||||||
|
if s:info.cur.around == a:pair
|
||||||
|
return "\<Del>"
|
||||||
|
elseif s:info.cur.p_char == strcharpart(a:pair, 0, 1)
|
||||||
|
return "\<C-G>U\<Left>"
|
||||||
|
endif
|
||||||
|
return ""
|
||||||
|
endif
|
||||||
|
if strcharpart(a:info.cur.text[a:info.cur.col - 1 :], 0, 1) ==# a:char
|
||||||
|
echom 41
|
||||||
|
return "\<Del>"
|
||||||
|
endif
|
||||||
|
return ''
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:keys4quote(char, info, opts) "{{{1
|
||||||
|
let quotes_behind = strchars(matchstr(a:info.cur.behind, '['.escape(a:char, '\^[]').']*$'))
|
||||||
|
let quotes_ahead = strchars(matchstr(a:info.cur.ahead, '^['.escape(a:char, '\^[]').']*'))
|
||||||
|
echom 'k4q: ' quotes_behind . ' - ' . quotes_ahead
|
||||||
|
echom string(a:opts.nesting_quotes)
|
||||||
|
if a:opts.autoclose && index(a:opts.nesting_quotes, a:char) >= 0
|
||||||
|
\&& quotes_behind > 1
|
||||||
|
let add2right = quotes_ahead > quotes_behind + 1 ? 0 : quotes_behind - quotes_ahead + 1
|
||||||
|
echom 51
|
||||||
|
echom add2right
|
||||||
|
return repeat(a:char, add2right) . repeat("\<C-G>U\<Left>", add2right)
|
||||||
|
endif
|
||||||
|
if a:info.cur.n_char ==# a:char
|
||||||
|
echom 53
|
||||||
|
return "\<Del>"
|
||||||
|
endif
|
||||||
|
let exprs = a:opts.smart_quotes_base + a:opts.smart_quotes_extra
|
||||||
|
if a:opts.autoclose && a:opts.smart_quotes
|
||||||
|
\&& s:any_is_true(exprs, a:info, a:opts)
|
||||||
|
echom 52
|
||||||
|
return ''
|
||||||
|
endif
|
||||||
|
if !a:opts.autoclose && quotes_behind
|
||||||
|
echom 54
|
||||||
|
return "\<Left>"
|
||||||
|
endif
|
||||||
|
if !a:opts.autoclose
|
||||||
|
echom 55
|
||||||
|
return ''
|
||||||
|
endif
|
||||||
|
echom 59
|
||||||
|
return a:char . "\<C-G>U\<Left>"
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user