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:

  1. strpart() takes string, offset , length arguments, while supplied 2 offsets.
  2. col('.') 1 character past end-of-line. i.e. len(getline('.'))==col('.')+1 meaning strpart(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:

  1. 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 stricter is#: a is# b type(a)==type(b) && ==# b.
  2. same =~: use =~#.
  3. due backwards compatibility string[-1] (string[any_negative_integer]) empty. have use line[-1:].

  4. never use plain :echoerr. unstable: in terms cannot sure whether or not break execution flaw (:echoerr breaks execution if put inside :try block , not otherwise). echohl errormsg|echomsg …|echohl none never breaks execution, throw … , try|echoerr …|endtry break always.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -