Is there a way to make use of two custom, complete functions in Vimscript?

后端 未结 2 1789
离开以前
离开以前 2021-02-20 14:01

Is there a way to achieve the following in Vim?

command! -nargs=* -complete=customlist,CustomFunc1 -complete=customlist,CustomFunc2 Foo call MyFunction(

        
2条回答
  •  悲哀的现实
    2021-02-20 14:55

    There is sufficient information passed to completion function through its arguments. Knowing current cursor position in the command line to be completed, it is possible to determine the number of the argument that is currently being edited. Here is the function that returns that number as the only completion suggestion:

    " Custom completion function for the command 'Foo'
    function! FooComplete(arg, line, pos)
        let l = split(a:line[:a:pos-1], '\%(\%(\%(^\|[^\\]\)\\\)\@

    Substitute the last line with a call to one of the functions completing specific argument (assuming they are already written). For instance:

        let funcs = ['FooCompleteFirst', 'FooCompleteSecond']
        return call(funcs[n], [a:arg, a:line, a:pos])
    

    Note that it is necessary to ignore whitespace-separated words before the command name, because those could be the limits of a range, or a count, if the command has either of them (spaces are allowed in both).

    The regular expression used to split command line into arguments takes into account escaped whitespace which is a part of an argument, and not a separator. (Of course, completion functions should escape whitespace in suggested candidates, as usual in case of the command having more than one possible argument.)

提交回复
热议问题