vim-pandoc-syntax icon indicating copy to clipboard operation
vim-pandoc-syntax copied to clipboard

'$' breaks highlighting and folding

Open Konfekt opened this issue 7 years ago • 20 comments

A dollar sign in single quotes '$' breaks the highlighting and folding. It has to be escaped '\$' for the highlighting and folding work again. For example, in image

Here the $ in single quotes is concealed.

Konfekt avatar Oct 19 '17 13:10 Konfekt

As a quick fix, one can use backticks: `$`.

itaraju avatar Jul 11 '18 14:07 itaraju

Same issue here. Importantly it does also concern fenced code blocks and I can't imagine a workaround that preserves the output of those.

yngman avatar Sep 14 '18 09:09 yngman

Confirmed still an issue. Notably at least Neovim's build in markdown syntax highlighter does not have this problem.

alerque avatar Apr 20 '19 09:04 alerque

So this issue is due to a Vim bug? Is there a reasonable workaround by modifying the syntax file?

Konfekt avatar Apr 20 '19 10:04 Konfekt

@Konfekt No no it's bug in this plugin's syntax file. I'm sure there is a way to fix it, somebody just has to do so. I've been triaging a few issue reports and was just confirming that I determined this was/is a bug (as opposed to a question, feature request, etc.).

alerque avatar Apr 20 '19 10:04 alerque

Do we need to avoid other characters than '. A general rule that fixes it (as far as i can tell) is

syn region pandocLaTeXInlineMath start=/\v([[:graph:]])@<!\$\S@=/ end=/\v\\@<!\$\d@!/ keepend contains=@LATEX

fmoralesc avatar Apr 20 '19 10:04 fmoralesc

Maybe [:punct:] is a better choice.

fmoralesc avatar Apr 20 '19 10:04 fmoralesc

Why is ([[:graph:]]) or ([[:punct:]]) necessary? Regarding

https://groups.google.com/forum/#!msg/pandoc-discuss/KiyMZn1wFHg/lPUzI8U-KukJ

we have

The opening $ must have a character immediately to its right, while the closing $ must have a character immediately to its left.

Could something like \<@<!\$\S@= for detecting the opening $ and \S\\@<!\$ for detecting the closing $ be sufficient?

Konfekt avatar Apr 20 '19 14:04 Konfekt

Sure, we can try that out. The rule using [:punct:] was just an idea.

fmoralesc avatar Apr 20 '19 14:04 fmoralesc

An option:

syn region pandocLaTeXInlineMath start=/\$\ze\<\S\@=/ end=/\S\zs\\@<!\$/ keepend contains=@LATEX

But it is faulty for the final $ in

'$' a $1$ '$aa$' $a+a$ aa$a aaa

aaaaa aaaa aaa

fmoralesc avatar Apr 20 '19 15:04 fmoralesc

Shouldn't it be

start=/\<\$\ze\S\@=/ end=/\S\zs\\@<!\$\>/

Konfekt avatar Apr 20 '19 15:04 Konfekt

The first \< won't match unless keyword is set to include $, I think. It doesn't work for me, in any case.

fmoralesc avatar Apr 20 '19 15:04 fmoralesc

Then perhaps something like

start=/[^[:keyword:]]\$\ze\S\@=/  end=/\S\zs\\@<!\$[^[:keyword:]]/

Konfekt avatar Apr 20 '19 16:04 Konfekt

I still have the issue with that. The best I've come up with so far is

start=/\$\ze\<.\{-}\>\$/ end=/\v\\@<!\S\zs\$\d@!/

This version looks for a matching $ in the current line before matching the start pattern. For multiline matches it will not work:

aa$a
a$

It is possible to capture these changing . with \_. in the start pattern, but then there are other issues with stuff like

$ '$' a $1$ '$aa$' $a+a$ $ aa$a aaa

aaaaa aaaa a$aa

I don't know how serious a problem this is; I think that having inline math across several lines is probably a mistake in any case.

fmoralesc avatar Apr 20 '19 19:04 fmoralesc

That is, you want to match

aa$a
a$

? Then start=/[^[:keyword:]]\$\ze\S\@=/ would exclude that. While I find an inline math command spanning more than a single line common, starting an inline math pattern with a character such as a$ ... $ does not look like best practice to me.

Konfekt avatar Apr 20 '19 19:04 Konfekt

Again citing

The opening $ must have a character immediately to its right, while the closing $ must have a character immediately to its left.

this is also not what pandoc expects for a starting delimiter, but for a closing one.

Konfekt avatar Apr 20 '19 19:04 Konfekt

@Konfekt The example is just to illustrate the kind of problem. It could have just as easily been

a. $...
...$

I also think that multiline math is worth having.

Again, [^[:keyword:]]\$\ze\S\@= does not work, because it doesn't even match for

$ '$' a $1$ '$aa$'

fmoralesc avatar Apr 20 '19 19:04 fmoralesc

What I'm trying to do is to prevent the syntax to start the region for cases like this

aaaa. $...

regular text here.
...

EOF

fmoralesc avatar Apr 20 '19 19:04 fmoralesc

Regarding your example, why is it that Vim does not deem a closing delimiter necessary before starting highlighting ?

I guess you dismissed already for good reason, such as slowness, the rules given in the built-in syntax/tex.vim such as

   syn region texMathZoneX	matchgroup=Delimiter start="\$" skip="\\\\\|\\\$"	matchgroup=Delimiter	end="\$"	end="%stopzone\>"		concealends contains=@texMathZoneGroup

Also, by

The opening $ must have a character immediately to its right, while the closing $ must have a character immediately to its left.

the matching rules in pandoc are more restrictive.

Konfekt avatar Apr 20 '19 20:04 Konfekt

Regarding your example, why is it that Vim does not deem a closing delimiter necessary before starting highlighting ?

I think the way it works is it starts highlighting once it finds the match and then it stops when it finds the end region. So it's not really clever. This is one of the reasons we cannot use naive rules for the highlighting.

I don't think the proposed rules are more restrictive besides the corner cases that the spec doesn't talk about (for example, what happens when a paragraph ends after a $). This is what the manual says:

Anything between two $ characters will be treated as TeX math. The opening $ must have a non-space character immediately to its right, while the closing $ must have a non-space character immediately to its left, and must not be followed immediately by a digit. Thus, $20,000 and $30,000 won’t parse as math. If for some reason you need to enclose text in literal $ characters, backslash-escape them and they won’t be treated as math delimiters.

What I have now is

syn region pandocLaTeXInlineMath start=/\\\@<!\$\ze\<\_.\{-}\>\$/ skip=/\\\$/ end=/\v\\@<!\S\zs\$\d@!/ end=/\n\s*\n/ keepend contains=@LATEX

This still has problems with dangling $... at the end of a line, but covers all the other cases.

EDIT: And the TeX rule simply gives the wrong rendering, it is too liberal.

fmoralesc avatar Apr 20 '19 20:04 fmoralesc