Skip to content

Unique nested syntax region names for context-commentstring or tcomment #724

@kiryph

Description

@kiryph

Commentary.vim and nested code blocks

I use commentary.vim for commenting blocks of code. However, if I am working on a block of lua code inside a tex document commentary.vim uses the wrong commentstring.

I recently found out that the plugin https://github.com/suy/vim-context-commentstring can adjust the commentstring based on the current syntax region.

Installing this plugin next to commentary+vimtex and adding to ~/.vim/after/ftplugin/tex.vim

if !exists('g:context#commentstring#table')
    let g:context#commentstring#table = {}
endif
let g:context#commentstring#table.tex = {
        \ 'texZoneLua':     '--%s',
        \ }

the mapping gcc to toggle a commented line works as epxected:

\documentclass{article}
\begin{document}
\directlua{
  tex.print('This is lua.')
  -- tex.print('This is a line in lua commented with the mapping gcc.')
}
This is latex.
% This is a line in latex commented with the same mapping gcc.
ment}

This works because vimtex defines a nested syntax region in after/syntax/tex.vim with the name texZoneLua.

Unfortunately, it does not work for a other nested constructs such as gnuplottex and luacode environment

\begin{luacode}
tex.print('This is lua.')
\end{luacode}

\begin{gnuplot}[terminal=epslatex, terminaloptions=color dashed]
  set key box top left
  set key width 2
  set key opaque
  set sample 1000
  set xr [-5:5]
  set xlabel '$x$-label'
  set ylabel '$y$-label'
  plot  sin(x) w l lc 1 lw 3 t '$\sin(x)$',\
        cos(x) w l lc 2 lw 3 t '$\cos(x)$',\
        tan(x) w l lc 3 lw 3 t '$\tan(x)$',\
        tanh(x) w l lc 4 lw 3 t '$\tanh(x)$'
\end{gnuplot}

because the syntax region name are the same texZone.

Is it possible to change the shared name texZone in after/syntax/tex.vim to something which includes the associated filetype as following for example

...
syntax region texZoneLua
      \ start='\\begin{luacode\*\?}'rs=s
      \ end='\\end{luacode\*\?}'re=e
      \ keepend
      \ transparent
      \ contains=texBeginEnd,@LUA
...
syntax region texZoneGnuplot
      \ start='\\begin{gnuplot}\(\_s*\[\_[\]]\{-}\]\)\?'rs=s
      \ end='\\end{gnuplot}'re=e
      \ keepend
      \ transparent
      \ contains=texBeginEnd,texBeginEndModifier,@GNUPLOT
...
syntax region texZoneDot
      \ start="\\begin{dot2tex}"rs=s
      \ end="\\end{dot2tex}"re=e
      \ keepend
      \ transparent
      \ contains=texBeginEnd,@DOT
...

tcomment

The plugin tcomment.vim supports out of the box embedded/nested code with a different comment character. It is also based on syntax region names and therefore could also take advantage of vimtex syntax rules. The defaults of tcomment.vim are

    let g:tcommentSyntaxMap = {
                \ 'bladeEcho':          'php',
                \ 'bladePhpParenBlock': 'php',
                \ 'erubyExpression':    'ruby',
                \ 'rmdChung':           'r',
                \ 'vimMzSchemeRegion':  'scheme',
                \ 'vimPerlRegion':      'perl',
                \ 'vimPythonRegion':    'python',
                \ 'vimRubyRegion':      'ruby',
                \ 'vimTclRegion':       'tcl',
                \ 'Delimiter': {
                \     'filetype': {
                \         'php': 'php',
                \     },
                \ },
                \ 'phpRegionDelimiter': {
                \     'prevnonblank': [
                \         {'match': '<?php', 'filetype': 'php'},
                \         {'match': '?>', 'filetype': 'html'},
                \     ],
                \     'nextnonblank': [
                \         {'match': '?>', 'filetype': 'php'},
                \         {'match': '<?php', 'filetype': 'html'},
                \     ],
                \ },
                \ }

One would add g:tcommentSyntaxMap.texZoneLua = 'lua'.

asymptote

Do you like adding support for nested asymptote code (http://asymptote.sourceforge.net/doc/LaTeX-usage.html). There is no default syntax file contained in $VIMRUNTIME, however, upstream of asymptote provides a file https://github.com/vectorgraphics/asymptote/blob/master/base/asy.vim.
Commenting in asymptote is the same as in c/c++ either with // or /* */, see http://asymptote.sourceforge.net/asyRefCard.pdf.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions