本文介绍了使用Vim的syn-include和syn-region嵌入语法高亮时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过以下方式使Vim突出显示以扩展名.Rtex结尾的任何文件:

I am trying to get Vim to syntax highlight any file that ends with extension .Rtex in the following way:

  • 所有顶级文本均以TeX突出显示
  • 例外:\begin{python}...\end{python}中包含的所有文本均突出显示为Python
  • All top level text is highlighted as TeX
  • Exception: any text enclosed in \begin{python}...\end{python} is highlighted as Python

我能够分别达到上述每个标准,但无法同时达到这两个标准.我认为以某种方式TeX突出显示会覆盖我的Python突出显示的区域,或者阻止它们生效,而我一直试图找出方法.

I am able to achieve each of these criteria individually, but unable to achieve both simultaneously. I think that somehow the TeX highlighting overrides my Python-highlighted regions, or prevents them from taking effect, and I am stuck trying to figure out how.

第一步:编辑.vimrc,使扩展名为.Rtex的文件的文件类型为rtex:

First step: edit .vimrc to give files with extension .Rtex the filetype rtex:

  au BufRead *.Rtex setf rtex

第二步:创建~/.vim/syntax/rtex.vim.该文件的内容将决定如何突出显示.Rtex文件.

Second step: create ~/.vim/syntax/rtex.vim. It is the contents of this file that will determine how to highlight .Rtex files.

第三步:通过使rtex.vim看起来像这样,启用常规的顶级TeX高亮显示:

Third step: enable general top-level TeX highlighting, by making rtex.vim look like this:

runtime! syntax/tex.vim

如果我现在打开一个.Rtex文件,则按预期将整个文件突出显示为TeX,包括\begin{python}...\end{python}中的任何文本.

If I now open a .Rtex file, the entire file is highlighted as TeX, including any text within \begin{python}...\end{python}, as expected.

第四步::按照Vim的:help syn-include中的说明进行操作,以包括python高亮显示并将其应用于以\begin{python}\end{python}分隔的所有区域.我的rtex.vim文件现在看起来像这样:

Fourth step: follow the instructions in Vim's :help syn-include to include python highlighting and apply it to all regions delimited by \begin{python} and \end{python}. My rtex.vim file now looks like this:

runtime! syntax/tex.vim
unlet! b:current_syntax
syntax include @Python syntax/python.vim
syntax region pythonCode start="\\begin{python}" end="\\end{python}" contains=@Python

unlet! b:current_syntax命令用于强制执行python.vim语法文件,即使现有的语法(TeX)已经处于活动状态.

The unlet! b:current_syntax command is meant to force the python.vim syntax file to execute even though an existing syntax (TeX) is already active.

问题::如果我现在打开.Rtex文件,则整个文件仍仅以TeX突出显示. \begin{python}...\end{python}区域似乎没有任何作用.

Problem: If I now open a .Rtex file, the entire file is still highlighted only as TeX. The \begin{python}...\end{python} region seems to have no effect.

实验::如果我删除或注释掉runtime!命令,我会在\begin{python}...\end{python}区域内 do 完全按照需要进行python高亮显示,但是当然,在本文档的其余部分中不会突出显示TeX.因此,我得出结论,TeX高亮在某种程度上负责阻止python区域生效.

Experiment: If I remove or comment out the runtime! command, I do get python highlighting, within the \begin{python}...\end{python} regions, exactly as desired, but of course no TeX highlighting in the remainder of the document. I therefore conclude that the TeX highlighting is somehow responsible for preventing the python regions from taking effect.

Vim大师可以给我任何建议吗?我目前很困惑.我看了看似相关的几页和stackoverflow问题,但到目前为止,没有一个问题导致解决方案:

Can a Master of Vim offer me any suggestions? I am currently stumped. I have looked at several pages and stackoverflow questions that seem relevant, but none of them have so far led to a solution:

  • http://vim.wikia.com/wiki/Different_syntax_highlighting_within_regions_of_a_file
  • Embedded syntax highligting in Vim
  • VIM syntax highlighting of html nested in yaml

推荐答案

在对手册进行了更多的研究和更多的反复试验之后,我终于回答了我自己的问题(同时令人尴尬和崇高的成就),我现在保留在这里供后代使用.

After some more study of the manual, and much more trial and error, I have finally answered my own question (a simultaneously embarrassing and sublime accomplishment), which I now preserve here for posterity.

基本上,我认为问题在于python突出显示不会生效,因为pythonCode区域本身包含在tex.vim定义的区域或突出显示组中,因此它不是顶级的.解决方案是也include(而不只是runtime)tex.vim,为其赋予一个类似@TeX的名称,然后将containedin=@TeX添加到我的python区域定义中.所以syntax/rtex.vim现在看起来像这样:

Basically, I think the problem is that the python highlighting wouldn't take effect because the pythonCode region was itself contained in a region or highlight group defined by tex.vim, so it wasn't top-level. The solution is to also include (rather than just runtime) tex.vim, giving it a name like @TeX, and then add containedin=@TeX to my python region definition. So syntax/rtex.vim now looks like this:

let b:current_syntax = ''
unlet b:current_syntax
runtime! syntax/tex.vim

let b:current_syntax = ''
unlet b:current_syntax
syntax include @TeX syntax/tex.vim

let b:current_syntax = ''
unlet b:current_syntax
syntax include @Python syntax/python.vim
syntax region pythonCode matchgroup=Snip start="\\begin{python}" end="\\end{python}" containedin=@TeX contains=@Python

hi link Snip SpecialComment
let b:current_syntax = 'rtex'

这有效!我不确定是否所有这些unlet b:current_syntax命令都是绝对必要的.我还为python区域定界符提供了一个matchgroup(Snip),以便可以将它们自己突出显示(使用SpecialComment颜色),而不是将其突出显示,这显然是默认情况下发生的事情,因为不再考虑它们TeX的一部分.

And this works! I'm not sure if all of those unlet b:current_syntax commands are strictly necessary. I also gave the python region delimiters a matchgroup (Snip) so that they can be highlighted themselves (with the SpecialComment color), rather than left just plain, which is apparently what happens by default, since they are no longer considered part of the TeX.

现在,为不同的语言(例如,\begin{Scode}...\end{Scode})添加更多的突出显示区域是一件微不足道的事情,如果您正在学习识字编程,那将是一个很好的选择-这是我提出问题的原始动机.

Now it is a trivial thing to add more highlight regions for different languages (e.g., \begin{Scode}...\end{Scode}), which is great if you're getting into literate programming -- the original motivation for my question.

这是一个截图,显示了它如何与嵌入TeX文档中的Python和R代码一起使用:

here is a screenshot showing how it works with Python and R code embedded in a TeX document:

这篇关于使用Vim的syn-include和syn-region嵌入语法高亮时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 08:20