Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
kiryph opened this issue Mar 17, 2017 · 5 comments
Closed

Comments

@kiryph
Copy link
Contributor

kiryph commented Mar 17, 2017

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.

@lervag lervag closed this as completed in 9aace77 Mar 17, 2017
@lervag
Copy link
Owner

lervag commented Mar 17, 2017

I assume I could also add support for asymptote. I don't know it or use it myself. It seems to use environments asy and asydef, so doing something similar to the implementations for Dot and Gnuplot but for these environments should be enough. Or?

@kiryph
Copy link
Contributor Author

kiryph commented Mar 18, 2017

Yes, that should be fine.
Does following addition to after/syntax/tex.vim look ok?

" {{{1 Nested syntax highlighting for asymptote
unlet b:current_syntax
syntax include @ASYMPTOTE syntax/asy.vim
syntax cluster texDocGroup add=texZoneAsymptote
syntax region texZoneAsymptote
      \ start='\\begin{asy}'rs=s
      \ end='\\end{asy}'re=e
      \ keepend
      \ transparent
      \ contains=texBeginEnd,texBeginEndModifier,@ASYMPTOTE
syntax region texZoneAsymptote
      \ start='\\begin{asydef}'rs=s
      \ end='\\end{asydef}'re=e
      \ keepend
      \ transparent
      \ contains=texBeginEnd,texBeginEndModifier,@ASYMPTOTE
let b:current_syntax = 'tex'

" }}}1

To see the syntax highlighting and sensitive commenting behaviour in action, you can download the syntax file with

$ wget https://raw.githubusercontent.com/vectorgraphics/asymptote/master/base/asy.vim ~/.vim/syntax/

and set

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

Shortened example file from http://asymptote.sourceforge.net/doc/LaTeX-usage.html:

\documentclass[12pt]{article}
\usepackage[inline]{asymptote}
\begin{document}

\begin{asydef}
// Global Asymptote definitions can be put here.
import three;
usepackage("bm");
texpreamble("\def\V#1{\bm{#1}}");
// One can globally override the default toolbar settings here:
// settings.toolbar=true;
\end{asydef}

Here is a venn diagram produced with Asymptote, drawn to width 4cm:

\def\A{A}
\def\B{\V{B}}

%\begin{figure}
\begin{center}
\begin{asy}
size(4cm,0);
pen colour1=red;
pen colour2=green;

pair z0=(0,0);
pair z1=(-1,0);
pair z2=(1,0);
real r=1.5;
path c1=circle(z1,r);
path c2=circle(z2,r);
fill(c1,colour1);
fill(c2,colour2);

picture intersection=new picture;
fill(intersection,c1,colour1+colour2);
clip(intersection,c2);

add(intersection);

draw(c1);
draw(c2);

// draw("$\A$",box,z1);              // Requires [inline] package option.
// draw(Label("$\B$","$B$"),box,z2); // Requires [inline] package option.
draw("$A$",box,z1);            
draw("$\V{B}$",box,z2);

pair z=(0,-2);
real m=3;
margin BigMargin=Margin(0,m*dot(unit(z1-z),unit(z0-z)));

draw(Label("$A\cap B$",0),conj(z)--z0,Arrow,BigMargin);
draw(Label("$A\cup B$",0),z--z0,Arrow,BigMargin);
draw(z--z1,Arrow,Margin(0,m));
draw(z--z2,Arrow,Margin(0,m));

shipout(bbox(0.25cm));
\end{asy}
%\caption{Venn diagram}\label{venn}
\end{center}
%\end{figure}
\end{document}

I think the syntax file is quite basic, however, it is better than nothing.

@lervag
Copy link
Owner

lervag commented Mar 18, 2017

Done, nested syntax for asymptote should work now.

@avidseeker
Copy link

Has this feature been removed? grep -r @ASYMPTOTE doesn't return anything on master.

Following @kiryph instructions, I got it working but with a few bugs:
image

lervag added a commit that referenced this issue Feb 27, 2024
@lervag
Copy link
Owner

lervag commented Feb 27, 2024

There was a minor bug that I've fixed now.

Notice, though: The asy.vim syntax script is not really compatible with nested syntax highlighting. This is because it relies on the contains=TOP,... syntax options, which breaks the nesting. This is why you get those bugs you are seeing. And there is unfortunately nothing I can do about it (as far as I know). My suggestion would be to remove/uninstall the asy.vim script and instead work with asymptote without highlighting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants