Friday, January 27, 2012

Sharing a nice way to use custom completion function preserving omnifunc/completefunc

ZyX has found a nice way:


function! CompleteWith(fun)
" after this characters have been process reset completion function.
" feedkeys must be used, returning same chars with return will hide the
" completion menue.
call feedkeys("\<C-r>=['', setbufvar('%', '&omnifunc', ".string(&l:omnifunc).")][0]\<cr>",'t')
let &l:omnifunc=a:fun
return "\<C-x>\<C-o>"
endfunction

inoremap <buffer> <expr> \start_completion CompleteWith("vam#install#CompleteAddonName")

Do you feel it should be added to Vim's documentation?
Its a nice short solution preventing omnifuncc/ompletefunc collisions.

What magic is done? First keys are added to Vim's input buf resetting
the completion function. Then omnifunc is temporarily set to the new
value and completion is initiated by using <exec> and returning <c-x> <c-o>

This code: setbufvar('%', '&omnifunc', ".string(&l:omnifunc).")
just resets omnifunc to the old value in a smart/obfuscated way. This
workaround is used because you can't run commands using <c-r>.

I feel this piece of code is worth sharing.

Marc Weber

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: