我试图使用ftplugin脚本手动定义.tex文件的缩进级别。
~/.vim/ftplugin/tex/indent.vim
1 setlocal indentexpr=TeXIndent(v:lnum)
2
3 function! TeXIndent(lnum)
4 if a:lnum == 0
5 return 0
6 endif
7
8 let prev = getline(a:lnum - 1)
9 let line = getline(a:lnum)
10
11 " sections have hardcoded indentation; environments are always subordinate to sections
12 if prev =~ '^\s*\\section'
13 return 1
14 elseif prev =~ '^\s*\\subsection'
15 return 2
16 elseif prev =~ '^\s*\\subsubsection'
17 return 3
18 elseif prev =~ '^\s*\\paragraph'
19 return 4
20 " environment indentation
21 elseif prev =~ '\\begin'
22 return indent(a:lnum - 1) + 1 " increase indentation by 1 if previous line has \begin
23 elseif line =~ '\\end'
24 return indent(a:lnum - 1) - 1 " decrease indentation by 1 if line has \end
25 else
26 return indent(a:lnum - 1) " use previous indentation
27 endif
28 endfunction当我键入\section时,我希望按下enter键将光标移动到下一行的缩进级别1(对于TeXIndent中指定的其他条件也是如此),但情况并非如此。使用一个更简单的函数(每次返回2)进行测试也缺乏这种行为,因此我怀疑我的方法存在缺陷。我如何达到预期的行为?
一般来说,如何调试ftplugin脚本?
发布于 2020-06-21 11:58:25
在调试ftplugin时,所有常用的方法都可用。
:debug exe "normal keysequence"可用于调试映射:debug Command可用于调试命令、函数(使用:debug call或:debug echo).:verbose可以用来知道实际定义的内容和定义的位置--参见vi.SE:https://vi.stackexchange.com/q/7722/626上的专用Q/A现在,您的问题不在于ftplugins,而在于缩进表达式。如果我没有弄错的话,它所面临的问题与我们使用折叠表达式时可能遇到的问题相同:缩进/折叠函数由Vim自动每一行调用一次。信息将被压制,:debug的内部用途将以噩梦告终。
我们能做些什么:
v:lnum,以便能够独立地调用该函数。太好了,这就是你已经做过的。我们可以测试来自所有调用的响应
*回波图(范围(1,行(‘$’)),'the_expr_function(v:val)')您会发现我正在维护的折叠插件中使用了这些方法。
(另见关于vi.SE:https://vi.stackexchange.com/a/19916/626的Q/A )
PS:缩进插件应该进入{rtp}/indent/,而不是{rtp}/ftplugin/,我们通常分析最后一个非空行。您应该可以在$VIMRUNTIME/indent中找到示例,而且我在2002年前后编写了很少的TeX缩进插件。
https://stackoverflow.com/questions/62493960
复制相似问题