我想创建一个映射,它将根据光标前面的字符更改ins-completion.如果该字符是{我希望标签完成,如果它:想要正常完成(这取决于完整选项),如果字符是反斜杠加上一些单词(\w+)我想要字典完成.我的ftplugin/tex/latex_settings.vim文件中有以下内容:
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>
Run Code Online (Sandbox Code Playgroud)
这不起作用,我不知道如何解决它.
编辑:这似乎工作,但我想要一个条件,检查\ w +(反斜杠加任何单词)和最后一个给出消息"找不到匹配".
function! MyLatexComplete()
let line = getline('.')
let pos = col('.') - 1
" Citations (comma for 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 as \delta)
return "\<C-X>\<C-K>"
endif
endfunction
Run Code Online (Sandbox Code Playgroud)
在你原来的函数中你有错误:
\n\nstrpart()接受字符串、偏移量和长度 arguments, while you supplied two offsets.col(\'.\')是超出行尾的一个字符。即len(getline(\'.\'))==col(\'.\')+1意思是strpart(getline(\'.\'), col(\'.\')-1) is always empty.您已在第二个变体中修复了这些问题。但如果你想有条件检查\\command you need not just last character. Thus I would suggest matching slice
let line=getline(\'.\')[:col(\'.\')-2]\nif line[-1:] is# \'{\' || line[-1:] is# \',\'\n return "\\<C-X>\\<C-]>"\nelseif line[-1:] is# \':\'\n return "\\<C-X>\\<C-N>"\nelseif line =~# \'\\v\\\\\\w+$\'\n return "\\<C-X>\\<C-K>"\nelse\n echohl ErrorMsg\n echomsg \'Do not know how to complete: use after {, comma or \\command\'\n echohl None\n return \'\'\nendif\nRun Code Online (Sandbox Code Playgroud)\n\n。注意一些事项:
\n\n==字符串比较。在这种情况下这并不重要,但你应该让自己习惯。两者都忽略设置的值(第一个行为就像已设置,第二个就像已设置)。我用得更严格:就像#?==#==?\'ignorecase\'\'noignorecase\'\'ignorecase\'is#a is# btype(a)==type(b) && a ==# b.=~:使用=~#.由于向后兼容string[-1]( string[any_negative_integer]) 始终为空。因此我必须使用line[-1:].
切勿使用普通:echoerr. 它是不稳定的:就您而言,您无法确定这是否会破坏执行缺陷(:echoerr如果放入:try块内,则破坏执行,否则不会这样做)。echohl ErrorMsg|echomsg \xe2\x80\xa6|echohl None从不中断执行,throw \xe2\x80\xa6并且try|echoerr \xe2\x80\xa6|endtry总是中断。