" 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 }
  endif
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:
