将vim外部命令结果行存储到VimL数组中,以进行一些自定义自动完成

This little snippet aims to make a little autocomplete script in VimL. the first code:

cat % | grep function | sed  's/(/ /g' | awk '{print $3}'

list all the method inside a php class. For example the output of this command could be:

__construct
__toString
foo
bar

If I send to complete function an array

call complete(col('.'), ["__construct", "__toString", "foo", "bar"])

I can see this menu:

enter image description here

So the final question is, ... how can I transform this:

__construct
__toString
foo
bar

into this ["__construct", "__toString", "foo", "bar"]?

Here my wrong code:

inoremap <F5> <C-R>=CustomComplete()<CR>
func! CustomComplete()
    let l:functions = system("cat % | grep function | sed  's/(/ /g' | awk '{print $3}'")
    call complete(col('.'), l:functions)
    return ''
endfunc

Refering to the current file

First, the % symbol is only parsed in the command-line. With system(), you need to expand it on your own:

system("cat " . expand('%') . " | grep function | sed  's/(/ /g' | awk '{print $3}'")

Now, there can be special characters in the current filename. To be safe, wrap the result in shellescape():

system("cat " . shellescape(expand('%')) . " | grep function | sed  's/(/ /g' | awk '{print $3}'")

Turning external command output into a List

You can use the split() function with a {pattern} of (newline):

let l:functions = split(
\   system("cat " . shellescape(expand('%')) . " | grep function | sed  's/(/ /g' | awk '{print $3}'"),
\   '
'
\)

Recent Vim versions have systemlist(), just for this common use case:

let l:functions = systemlist("cat " . shellescape(expand('%')) . " | grep function | sed  's/(/ /g' | awk '{print $3}'")