vim: complete depending on previous character -
i want create mapping changed ins-completion depending on character before cursor. if character { want tag completion, if : want normal completion (that depends on complete option) , if characters backslash plus word (\w+) want dictionary completion. have following in ftplugin/tex/latex_settings.vim file:
setlocal dictionary=$dotvim/ftplugin/tex/tex_dictionary setlocal complete=.,k setlocal tags=./bibtags; function! mylatexcomplete() let character = strpart(getline('.'), col('.') - 1, col('.')) if character == '{' return "\<c-x>\<c-]>" elseif character == ':' return "\<c-x>\<c-n>" else return "\<c-x>\<c-k>" endif endfunction inoremap <c-n> <c-r>=mylatexcomplete()<cr> this doesn't work , don't know how fix it.
edit: seems work i'm want conditional checks \w+ (backslash plus word) , final 1 gives message "no match found".
function! mylatexcomplete() let line = getline('.') let pos = col('.') - 1 " citations (comma multiple ones) if line[pos - 1] == '{' || line[pos - 1] == ',' return "\<c-x>\<c-]>" " sections, equations, etc elseif line[pos - 1] == ':' return "\<c-x>\<c-n>" else " commands (such \delta) return "\<c-x>\<c-k>" endif endfunction
in original function have mistakes:
strpart()takes string, offset , length arguments, while supplied 2 offsets.col('.')1 character past end-of-line. i.e.len(getline('.'))==col('.')+1meaningstrpart(getline('.'), col('.')-1)empty.
you have fixed these issues in second variant. if want conditional check \command need not last character. suggest matching slice
let line=getline('.')[:col('.')-2] if line[-1:] is# '{' || line[-1:] is# ',' return "\<c-x>\<c-]>" elseif line[-1:] is# ':' return "\<c-x>\<c-n>" elseif line =~# '\v\\\w+$' return "\<c-x>\<c-k>" else echohl errormsg echomsg 'do not know how complete: use after {, comma or \command' echohl none return '' endif . note things:
- never use
==string comparison without#or?attached. not matter in case, should make used.==#,==?both ignore value of'ignorecase'setting (first acts if'noignorecase'set, second if'ignorecase'set). use stricteris#:a is# btype(a)==type(b) && ==# b. - same
=~: use=~#. due backwards compatibility
string[-1](string[any_negative_integer]) empty. have useline[-1:].never use plain
:echoerr. unstable: in terms cannot sure whether or not break execution flaw (:echoerrbreaks execution if put inside:tryblock , not otherwise).echohl errormsg|echomsg …|echohl nonenever breaks execution,throw …,try|echoerr …|endtrybreak always.
Comments
Post a Comment