jrblevin / markdown-mode

Emacs Markdown Mode
http://jblevins.org/projects/markdown-mode/
GNU General Public License v3.0
877 stars 160 forks source link

Backslash escapes inside math and code are fontified as Markdown markup #766

Closed saf-dmitry closed 1 year ago

saf-dmitry commented 1 year ago

Backslashes inside math and code are fontified as Markdown markup.

Expected Behavior

Backslashes as part of math or code are fontified according to the type of content (i.e., LaTeX or corresponding code language).

Actual Behavior

Backslashes inside math or code are fontified and propertized as Markdown markup:

Screen Shot 2023-05-21 at 18 18 36

Screen Shot 2023-05-21 at 18 38 47

This is especially misleading when hiding Markdown syntax is enabled.

Steps to Reproduce

See the examples above.

Backtrace

Software Versions

Note

Commit ae1085a

jimporter commented 1 year ago

I think this would just need to add an extra check to not fontify backslash escapes when fontification has already added one of the following faces to the backslash:

That should be pretty straightforward with some new markdown-match-escape function that checks the backslash's face before returning whether it matched.

Maybe there's a cleaner way to do that though?

saf-dmitry commented 1 year ago

I think this would just need to add an extra check to not fontify backslash escapes when fontification has already added one of the following faces to the backslash:

  • markdown-inline-code-face
  • markdown-pre-face
  • markdown-math-face

That should be pretty straightforward with some new markdown-match-escape function that checks the backslash's face before returning whether it matched.

Yes, that is how I would approach this. Another point to consider is, it should fontify only backslashes not preceding by another backslashes, i.e., in \\ only the first backslash should be fontified because the second backslash stands for itself. Generally speaking, in something like \\\\\\ only odd, i.e., 1, 3, and 5 backslash positions should be fontified.

jimporter commented 1 year ago

Yes, that is how I would approach this. Another point to consider is, it should fontify only backslashes not preceding by another backslashes, i.e., in \\ only the first backslash should be fontified because the second backslash stands for itself. Generally speaking, in something like \\\\\\ only odd, i.e., 1, 3, and 5 backslash positions should be fontified.

This part should already work. The regexp is (approximately) \\., so it'll match a backslash followed by anything. That means it'll see the first backslash, then skip past the next one before trying again. If you type a bunch of backslashes in a row into a markdown-mode buffer, it should do the right thing.

jimporter commented 1 year ago

@saf-dmitry Ok, hopefully my PR fixes this. Can you try it out to see?

saf-dmitry commented 1 year ago

I just played with it a little bit and it looks good.

The markdown-literal-faces variable can be re-used for various other purposes as well, so I would consider adding the following faces to the list:

And maybe these faces too:

jimporter commented 1 year ago

markdown-link-title-face

Link titles look like they support escape sequences, so I think they should be excluded from markdown-literal-faces. The others make sense though (I think...)

saf-dmitry commented 16 hours ago

It seems that the current solution works in code blocks only if native fontification is disabled (which is the default). If the variable markdown-fontify-code-blocks-natively is set to t, the backslash fontification issue comes again:

Screen Shot 2024-07-14 at 08 16 04

I think we should reopen the issue.

jimporter commented 4 hours ago

It seems that the current solution works in code blocks only if native fontification is disabled (which is the default). If the variable markdown-fontify-code-blocks-natively is set to t, the backslash fontification issue comes again:

@saf-dmitry Thanks for noticing, here's a fix: https://github.com/jrblevin/markdown-mode/pull/836