Tuesday, February 16, 2016

Re: Help with autocomplete

This is commandline complete, if what you want is word complete, you can make use of complete function which is something like:

inoremap <c-k> <C-R>=MyComplete()<CR>
func! MyComplete()
let line = getline('.')
let start = col('.') - 1
while start > 0 && line[start - 1] =~# '\k'
let start -= 1
endwhile
let trigger = line[start:]
" words for complete
let words = ['one', 'two', 'three', 'oneee']
let suggestions = map(filter(words, 'stridx(v:val,"'.trigger.'") == 0'), '{"word":v:val}')
call complete(start + 1, suggestions)
return ''
endfunc

It's triggered by <C-k>

在 2016年2月16日星期二 UTC+8上午5:42:55,Gary Johnson写道:
> On 2016-02-15, David Woodfall wrote:
> > Consider the following piece of latex:
> >
> >
> > ONE: & Some text here...
> >
> > TWO: & Some more text...
> >
> > THREE: & More bla bla bla...
> >
> >
> > I would like to limit the autocomplete word list to contain only the
> > words on the left of the `&'.
> >
> > How could this be done?
> >
> > I don't mind if I have to actually specify the words rather than vim
> > finding them automatically.
>
> I find the easiest way to use completion is to use "custom" and just
> create a function that returns a newline-separated string of
> candidate strings. The MyComplete() function below uses
> getbufline() to put all the lines in the current buffer into a list,
> then uses map() and matchstr() to replace each line by the word
> preceding the '&'. Any non-matching lines are replaced by empty
> lines. Finally, filter() gets rid of the empty lines and join()
> joins the remaining lines into a string, with the lines separated by
> newlines.
>
> function! MyComplete(ArgLead, CmdLine, CursorPos)
> return join(filter(map(getbufline("%", 1, "$"), "matchstr(v:val, '^\\k\\+:\\ze\\s*&')"), "v:val!=''"), "\n")
> endfunction
> command! -nargs=1 -complete=custom,MyComplete MyCmd echo "<args>"
>
> Typing ":MyCmd " followed by a <Tab> then results in the following:
>
> :MyCmd
> ONE: TWO: THREE:
> :MyCmd
>
> Modify to suit.
>
> HTH,
> Gary

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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: