1
votes

Gived this script (packages suggestion was simplified, original takes care of <cword>)

function! CompleteImport()
  let packages = ['java.util.Vector','java.lang.String']
  call complete(col('.'),packages)
  return ''
endfunction

inoremap <F8> import <C-R>=CompleteImport()<CR>

while on insert mode you can add an import and choose between suggested packages pressing F8

But I want to be enable to reach that popup selection from normal mode

function! InsertImport() 
   exe "normal iimport \<C-R>=CompleteImport()\<CR>"
   "this commented line would work too
   "exe "normal i\<F8>" 
endfunction 

map <Leader>ji :call InsertImport()<CR> 

so from normal mode ,ji (stands for java import) add an import to word under cursor if it is found

(Move to right position is not a problem so I ommit here)

By now ,ji adds first suggestion from popup and exists insert mode

I have tried :startinsert but no luck.

see on http://vimdoc.sourceforge.net/htmldoc/eval.html#:execute there is a suggested code:

:execute "normal ixxx\<Esc>"

but that final Esc doesn't matter at all (for my vim install at least) This do exactly the same for me:

:execute "normal ixxx"

I would think this is not possible if I haven't found that on docs. So, it's possible to stay on a popup calling from a function?

Other interested docs:

http://vimdoc.sourceforge.net/htmldoc/various.html#:normal http://vimdoc.sourceforge.net/htmldoc/insert.html#:startinsert

1

1 Answers

1
votes

:startinsert usually is the right approach, but it indeed gives control back to the user, so you cannot automatically trigger the completion.

Through the feedkeys() function, you can send arbitrary keys "as if typed". This allows you to start insert mode and trigger the completion:

function! InsertImport()
    call feedkeys("iimport \<C-R>=CompleteImport()\<CR>", 't')
endfunction
nnoremap <Leader>ji :call InsertImport()<CR>

PS: You should use :noremap for the normal mode mapping, too; it makes the mapping immune to remapping and recursion.