smartparens icon indicating copy to clipboard operation
smartparens copied to clipboard

Paren matching in LaTeX mode

Open rgemulla opened this issue 8 years ago • 9 comments

In LaTeX-mode, parens that follow double backslashes (newlines) are not matched correctly. For example, in tabular environment, I sometimes use \\[1cm] to add a custom spacing between successive rows.

Expected behavior

The closing square bracket should be matched to the opening square bracket. In general, smartparens should ignore all sequences of double backslashes preceeding a paren.

Actual behavior

The closing square bracket is marked as mismatched.

Steps to reproduce the problem

Create a new buffer, switch to LaTeX-mode, then type \\[1cm].

rgemulla avatar Aug 28 '17 12:08 rgemulla

I think the problem here is that there is also a pair \[ for the math environment, and it picks this as the "possible opener" instead of just matching [. It should be possible to configure somehow.

Fuco1 avatar Aug 29 '17 11:08 Fuco1

One option may be to define the opener for \[ as a regexp along the lines of [^\](\\)*\[ (and the closing expression likewise). This way, it wouldn't match \\[ as desired, but it would correctly match \[ and \\\[. Can this be done already?

Another issue may be that the initial sequence of double backslashes shouldn't actually be a part of the matched paren pair, which may be more involved.

rgemulla avatar Aug 29 '17 12:08 rgemulla

I think the simplest would be to make the \[ pair be ignored if there is one more slash in front, so something like your last proposal. This can be done with an :unless condition on the \[ pair. ISTR we've done something similar already though...

There is the predicate sp-latex-point-after-backslash which seems to do exactly that. Which means something must've changed in the meantime and we probably didn't have a test for the scenario.

Fuco1 avatar Aug 31 '17 12:08 Fuco1

I've debugged it a bit and the problem is with the global skip function sp--backslash-skip-match which automatically ignores any pair prefixed with a \. This needs to be adjusted for latex a bit.

Fuco1 avatar Aug 31 '17 12:08 Fuco1

Thanks, I'll look into this.

rgemulla avatar Aug 31 '17 17:08 rgemulla

I changed that function plus a modified :unless condition everywhere, see commit adfb37e. This greatly improves things, but there are still open issues. In particular:

Now works correctly

Insertion and pair highlighting

()
\(\)
\\()
\\\(\)
\\\(\\\)

Mismatch highlighting:

(\)
\\(\)

Still does not work

Highlighting only shown when point on opening paren:

(\\)

Backspace deletion removes too much (should only remove "(", but removes "\(")

\\(

Prefixes not detected

\emph{abc}

Tags don't work at all (neither triggers nor highlighting):

\begin{quote}
...
\end{quote}

rgemulla avatar Aug 31 '17 19:08 rgemulla

Even though there are still issues, adfb37e already improves behavior significantly. What about merging it in now (I can create a pull request) and working on the remaining issues as time permits?

rgemulla avatar Oct 03 '17 10:10 rgemulla

Yes, please open a PR. I got a bit lost as we started splitting the work. Also close the other one if it has no additional work. Thanks!

Fuco1 avatar Oct 03 '17 11:10 Fuco1

Done, thanks!

rgemulla avatar Oct 03 '17 15:10 rgemulla