5

In Vim, I want the syntax

\begin{pycode}
...
\end{pycode}

to syntax highlight the content inside the environment in Python, and the syntax

(#TeX
r"""
...
""")

to syntax highlight the content inside the string in TeX.

So I do the following

  • put in .vim/after/syntax/tex.vim:

    call SyntaxRange#Include('\\begin{pycode}', '\\end{pycode}', 'python', 'Statement')
    
  • put in .vim/after/syntax/python.vim:

    call SyntaxRange#Include( '(#TeX\n\s*[rfb]\?"""' , '""")' , 'tex')
    

Each of them work fine without the other, but including both gives a lot of errors such as...

E132: Function call depth is higher than 'maxfuncdepth'
E169: Command too recursive

How can I fix it?

I do want to be able to nest the highlighting:

\begin{pycode}
# this part is highlighted as Python
print(#TeX
r"""
% this part is highlighted as TeX
\relax
""")
\end{pycode}

If allowing arbitrary nesting is not possible then at least 1 layer of nesting should be allowed?

By the way I was using SyntaxRange plugin, but without that the equivalent vim commands are

syntax include @synIncludeTex syntax/tex.vim
syntax region synIncludeTex  keepend start="(#TeX\n\s*[rfb]\?\"\"\"" end="\"\"\")" containedin=ALL contains=@synIncludeTex

syntax include @synIncludePython syntax/python.vim
syntax region synIncludePython matchgroup=Statement keepend start="\\begin{pycode}" end="\\end{pycode}" containedin=ALL contains=@synIncludePython
1

1 Answer 1

0

In the end I did something like this, seems to work

python.vim:

if !exists("b:syntax_nest_level")
  let b:syntax_nest_level = 0
endif
let b:syntax_nest_level += 1

" ======== actual code here ========
if b:syntax_nest_level <= 3
    call SyntaxRange#Include(...)
endif

let b:syntax_nest_level -= 1

And tex.vim follows a similar pattern.

(you can clearly see why there's no recursion error. I'll assume that nesting is limited to that many levels)

It seems that even though the limit is written to be 3, it works for arbitrarily many levels of nesting. (for which I don't know why. When the limit is 2, only finitely many levels is allowed)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.