Custom Completion Function

134 views
Skip to first unread message

Amit Dev

unread,
Jul 31, 2011, 7:08:24 AM7/31/11
to v...@vim.org
Hi,

I'm learning to write a vim plugin. One thing I'm trying to do is to use the custom completion function (^X^U). The problem is I need a list of completion items and a short description. So I tried to use each item as a dict ('word' : <word> and 'menu' : <description>). This works fine, except that I need to find out what was the completion chosen by user. How to find that? There are duplicates in the completion items and only way to find correct entry is by knowing description as well. Eg:

abcd <a...@c.com>
dup   <d...@c.com>
dup   <d...@c.com>
xyz   <x...@y.com

Is there a callback or something that can be registered to find out which item user chose?

Another minor thing is doing ^X^U will complete to the first item. Is there a way not to do that? I want something like completion on browsers. While user types it should show the options. Currently I have to do ^X^U then backspace till beginning and start typing.

Thanks,
Amit

herm...@free.fr

unread,
Jul 31, 2011, 8:54:20 PM7/31/11
to vim use, v...@vim.org
Hello,


"Amit Dev" wrote:

> One thing I'm trying to do is to

> use the custom completion function (^X^U). [...]


>
> Is there a callback or something that can be registered to find out
> which item user chose?

In mu-template [1], I've proceeded the following (asynchronous) way. (I've found no way to proceed synchronously) :

- use feedkey (in s:ChooseTemplateFile(), l.680) to call a function that returns nothing, but that calls complete()

- the function called (s:ChooseByComplete(), l.643) prepares the dictionary for complete(), registers a hook (to be called at the end of the process), and calls complete() -- this is all done through a "library" that I wrote for the occasion (lh-vim-lib -> autoload/lh/icomplete.vim)

- the hook function (s:FinishCompletion(), l.659) extracts from the buffer what has really been inserted, and then do what I wished to do (here insert and interpret a template-file)
The hook itself is triggered when <cr> or <c-y> are pressed. Both key are temporarily overriden for this purpose.
NB: I also register a clear-once-the-temporary-mappings hook on InsertLeave, thanks to lh-vim-lib again.


HTH,

[1] the source code that'll interest you is here: http://code.google.com/p/lh-vim/source/browse/mu-template/trunk/autoload/lh/mut.vim

--
Luc Hermitte
http://lh-vim.googlecode.com/
http://hermitte.free.fr/vim/

herm...@free.fr

unread,
Jul 31, 2011, 8:57:54 PM7/31/11
to vim use, v...@vim.org

I forgot something regarding your need to know the exact choice even if there are ambiguities.

> Hello,
>
>
> "Amit Dev" wrote:
>
> > One thing I'm trying to do is to
> > use the custom completion function (^X^U). [...]
> >
> > Is there a callback or something that can be registered to find out
> > which item user chose?
>
> In mu-template [1], I've proceeded the following (asynchronous) way.
> (I've found no way to proceed synchronously) :
>
> - use feedkey (in s:ChooseTemplateFile(), l.680) to call a function
> that returns nothing, but that calls complete()
>
> - the function called (s:ChooseByComplete(), l.643) prepares the
> dictionary for complete(), registers a hook (to be called at the end
> of the process), and calls complete() -- this is all done through a
> "library" that I wrote for the occasion (lh-vim-lib ->
> autoload/lh/icomplete.vim)
>
> - the hook function (s:FinishCompletion(), l.659) extracts from the
> buffer what has really been inserted, and then do what I wished to
> do (here insert and interpret a template-file)

At this point, the string inserted is by no mean the final string, but something like a temporary identifier that I use to know which was the user's choice.

Amit Dev

unread,
Aug 3, 2011, 4:21:54 AM8/3/11
to vim...@googlegroups.com
Thanks Luc. I haven't completely understood it yet, but I'll try it out.

--
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

Reply all
Reply to author
Forward
0 new messages