Skip to content

Instantly share code, notes, and snippets.

@koron
Created September 24, 2011 02:59
Show Gist options
  • Save koron/1238900 to your computer and use it in GitHub Desktop.
Save koron/1238900 to your computer and use it in GitHub Desktop.
Sample of onselected hook of completion.
" Special completion function.
"
" This function completes very very limited strings:
"
" "a" -> "aa1", "foo2", "foo3", "many words start with 'aa'"
" "ab" -> "ab1", "ab2", "ab3"
function! LimitedComplete1(findstart, base)
if a:findstart == 1
let s = getline(line('.'))[0 : col('.') - 1]
if s =~ '\<a$'
return col('.') - 2
elseif s =~ '\<ab$'
return col('.') - 3
else
return -1
endif
elseif a:findstart == 0
if a:base == 'a'
echomsg 'Case: a'
return ['aa1', 'aa2', 'aa3', "many words start with 'aa'"]
elseif a:base == 'ab'
echomsg 'Case: ab'
return ['ab1', 'ab2', 'ab3']
else
echomsg 'Case: (none)'
return []
endif
else
" Nothing to do.
return 0
end
endfunction
function! LimitedComplete2(findstart, base)
let retval = LimitedComplete1(a:findstart, a:base)
if a:findstart == 1
return retval
else
return {
\ 'refresh': 'always',
\ 'words': retval,
\ 'onselected': function('OnSelected')
\}
endif
endfunction
function! OnSelected(base, result)
" TODO: Learn user's selection.
echomsg 'onselected('.reltimestr(reltime()).'): '.a:base.' -> '.a:result
endfunction
" Test for list form return value.
"setlocal completefunc=LimitedComplete1
" Test for dict form return value.
setlocal completefunc=LimitedComplete2
" vim:set ts=8 sts=2 sw=2 tw=0 et:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment