Subject: Re: bug#40865: 26.3; Electric pair mode and syntax class $

From: João Távora <[email-masked]>
Date: Tue, 12 May 2020 22:58:36 +0100

Augusto Stoffel <[email-masked]> writes:

Hi Augusto, thank you for the report.

> Electric pair mode does not treat string quote characters and paired
> delimiters (syntax class "$") in the same way, but it should probably
> do so.

Paired delimiters don't formally define any kind of syntactic region
between them, contrary to string delimiters.

Therefore, electric-pair-mode can't easily know if a decision to skip or
auto-pair would be benefitting the balance of the syntax buffer.
Therefore, it decides to be dumb and always autopair, never autoskip.

If I had chosen the alternative, having your point just before a '$foo$'
would make autoskip put it inside the group, instead of creating a new
'$$$foo$', which is probably what you wanted in that particular
situation.

Maybe, even though flawed, that is more defensible as a default
behaviour than the current one.  At least it doesn't seem to break any
tests.

The "correct" way to fix this is to make latex-mode treat paired
delimiters like it does string delimiteres.  But that probably has other
negative implications that I'm not aware of.

I have the vague recollection that this came up when I was developing
electric-pair-mode, particularly in latex-mode, some 6 years ago, but I
can't remember anymore.

Anyway, if you do prefer the alternative I gave earlier you can do this:

(add-hook 'latex-mode-hook
          (lambda ()
            (add-function :before-until
                          (local 'electric-pair-skip-self)
                          (lambda (char) (eq char ?$))
                          '((name . latex-skip-paired-delimiters)))))

João

PS: On a related note, note that AUCTex mode (a different mode to edit
TeX files) has a special binding for the dollar sign, TeX-insert-dollar,
which doesn't follow _any_ of electric-pair-mode.