2014-01-29 00:29:23 -05:00
|
|
|
scriptencoding utf-8
|
|
|
|
let s:save_cpo = &cpo
|
|
|
|
set cpo&vim
|
|
|
|
|
|
|
|
|
|
|
|
function! s:_uniq(list)
|
|
|
|
let dict = {}
|
|
|
|
for _ in a:list
|
|
|
|
let dict[_] = 0
|
|
|
|
endfor
|
|
|
|
return keys(dict)
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
let s:module = {
|
|
|
|
\ "name" : "BufferComplete",
|
|
|
|
\}
|
|
|
|
|
|
|
|
|
|
|
|
function! s:_buffer_complete()
|
|
|
|
return sort(s:_uniq(filter(split(join(getline(1, '$')), '\W'), '!empty(v:val)')), 1)
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
function! s:_parse_line(line)
|
|
|
|
let keyword = matchstr(a:line, '\zs\w\+\ze$')
|
|
|
|
let pos = strchars(a:line) - strchars(keyword)
|
|
|
|
return [pos, keyword]
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
function! s:_as_statusline(list, count)
|
|
|
|
if empty(a:list)
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
let hl_none = "%#StatusLine#"
|
|
|
|
let hl_select = "%#StatusLineNC#"
|
|
|
|
let tail = " > "
|
|
|
|
let result = a:list[0]
|
|
|
|
let pos = 0
|
|
|
|
for i in range(1, len(a:list)-1)
|
|
|
|
if strdisplaywidth(result . " " . a:list[i]) > &columns - len(tail)
|
|
|
|
if a:count < i
|
|
|
|
break
|
|
|
|
else
|
|
|
|
let pos = -i
|
|
|
|
endif
|
|
|
|
let result = a:list[i]
|
|
|
|
else
|
|
|
|
let result .= (" " . a:list[i])
|
|
|
|
endif
|
|
|
|
if a:count == i
|
|
|
|
let pos = pos + i
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
return join(map(split(result, " "), 'v:key == pos ? hl_select . v:val . hl_none : v:val'))
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
function! s:module.get_complete_words()
|
|
|
|
return s:_buffer_complete()
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
function! s:module.complete(cmdline)
|
2014-02-06 09:04:12 -05:00
|
|
|
call s:_finish()
|
2014-01-29 00:29:23 -05:00
|
|
|
let s:old_statusline = &statusline
|
|
|
|
|
|
|
|
let backward = a:cmdline.backward()
|
|
|
|
let [pos, keyword] = s:_parse_line(backward)
|
|
|
|
|
|
|
|
if !exists("s:complete")
|
|
|
|
let s:complete = self.get_complete_words()
|
|
|
|
endif
|
|
|
|
let s:complete_list = filter(copy(s:complete), 'v:val =~ ''^''.keyword')
|
|
|
|
if empty(s:complete_list)
|
|
|
|
return -1
|
|
|
|
endif
|
|
|
|
|
|
|
|
if pos == 0
|
|
|
|
let backward = ""
|
|
|
|
else
|
|
|
|
let backward = join(split(backward, '\zs')[ : pos-1 ], "")
|
|
|
|
endif
|
|
|
|
let s:line = backward . a:cmdline.forward()
|
|
|
|
let s:pos = pos
|
|
|
|
call a:cmdline.setline(s:line)
|
|
|
|
|
|
|
|
let s:count = 0
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
function! s:_finish()
|
|
|
|
if exists("s:old_statusline")
|
|
|
|
let &statusline = s:old_statusline
|
|
|
|
unlet s:old_statusline
|
2014-02-23 23:43:16 -05:00
|
|
|
redrawstatus
|
2014-01-29 00:29:23 -05:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2014-01-30 01:31:25 -05:00
|
|
|
function! s:module.on_char_pre(cmdline)
|
|
|
|
if a:cmdline.is_input("<Over>(buffer-complete)")
|
2014-01-29 00:29:23 -05:00
|
|
|
if self.complete(a:cmdline) == -1
|
|
|
|
call s:_finish()
|
|
|
|
call a:cmdline.setchar('')
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
call a:cmdline.setchar('')
|
2014-02-04 23:32:29 -05:00
|
|
|
call a:cmdline.tap_keyinput("Completion")
|
2014-02-06 09:04:12 -05:00
|
|
|
" elseif a:cmdline.is_input("\<Tab>", "Completion")
|
2014-01-30 01:31:25 -05:00
|
|
|
elseif a:cmdline.is_input("<Over>(buffer-complete)", "Completion")
|
|
|
|
\ || a:cmdline.is_input("\<Right>", "Completion")
|
2014-01-29 00:29:23 -05:00
|
|
|
call a:cmdline.setchar('')
|
|
|
|
let s:count += 1
|
|
|
|
if s:count >= len(s:complete_list)
|
|
|
|
let s:count = 0
|
|
|
|
endif
|
2014-01-30 01:31:25 -05:00
|
|
|
elseif a:cmdline.is_input("\<Left>", "Completion")
|
2014-01-29 00:29:23 -05:00
|
|
|
call a:cmdline.setchar('')
|
|
|
|
let s:count -= 1
|
|
|
|
if s:count < 0
|
|
|
|
let s:count = len(s:complete_list) - 1
|
|
|
|
endif
|
|
|
|
else
|
2014-02-04 23:32:29 -05:00
|
|
|
if a:cmdline.untap_keyinput("Completion")
|
2014-02-23 23:43:16 -05:00
|
|
|
call a:cmdline.callevent("on_char_pre")
|
2014-01-29 00:29:23 -05:00
|
|
|
endif
|
|
|
|
call s:_finish()
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
call a:cmdline.setline(s:line)
|
|
|
|
call a:cmdline.insert(s:complete_list[s:count], s:pos)
|
|
|
|
if len(s:complete_list) > 1
|
|
|
|
let &statusline = s:_as_statusline(s:complete_list, s:count)
|
2014-02-23 23:43:16 -05:00
|
|
|
redrawstatus
|
2014-01-29 00:29:23 -05:00
|
|
|
endif
|
|
|
|
if len(s:complete_list) == 1
|
2014-02-04 23:32:29 -05:00
|
|
|
call a:cmdline.untap_keyinput("Completion")
|
2014-01-29 00:29:23 -05:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2014-02-23 23:43:16 -05:00
|
|
|
function! s:module.on_draw_pre(...)
|
|
|
|
" redrawstatus
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2014-01-29 00:29:23 -05:00
|
|
|
function! s:module.on_leave(cmdline)
|
2014-02-06 09:04:12 -05:00
|
|
|
call s:_finish()
|
2014-01-29 00:29:23 -05:00
|
|
|
unlet! s:complete
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:make()
|
|
|
|
return deepcopy(s:module)
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
let &cpo = s:save_cpo
|
|
|
|
unlet s:save_cpo
|