Subject: Re: bug#49518: 28.0.50; electric-pair-mode skip-self fails for single-quotes in python-mode

From: João Távora <[email-masked]>
Date: Sun, 19 Sep 2021 01:20:30 +0100

João Távora <[email-masked]> writes:

> Jim Porter <[email-masked]> writes:
>
>> (CCing João since he wanted to hear about `electric-pair-mode' issues[1].)
>
> Thanks you Jim,
>
> I've read your original bug report.  It is very clear in reproduction
> and also the analysis seems mostly correct.  I'll see what I can do.
>
> The solution will probably go into python.el in terms of customization
> of electric-pair-mode's variables.  The only question is whether it can
> be made to serve Stephen's and your requirements.
>
> In the meantime, if you're curious: this was one of the first things I
> worked in with autopair.el. See
> https://github.com/joaotavora/autopair/issues/6.

So after having a look at this, I came up with a patch.  All tests
pass, and your original request is granted.  Unfortunately, Stephen's
desired triple-pair behaviour is also lost.  However, I think that
behaviour can be recovered via other more elegant, less accidental ways.
python.el has 'python-electric-pair-string-delimiter' for triple-pairing
that doesn't seem to be doing its thing.

Here is the patch.  Give it a shot.

diff --git a/lisp/elec-pair.el b/lisp/elec-pair.el
index d8c377a2ef..b8b8a97651 100644
--- a/lisp/elec-pair.el
+++ b/lisp/elec-pair.el
@@ -198,7 +198,9 @@ electric-pair-syntax-info
 inside a comment or string."
   (let* ((pre-string-or-comment (or (bobp)
                                     (nth 8 (save-excursion
-                                             (syntax-ppss (1- (point)))))))
+                                             (skip-chars-backward
+                                              (make-string 1 command-event))
+                                             (syntax-ppss (point))))))
          (post-string-or-comment (nth 8 (syntax-ppss (point))))
          (string-or-comment (and post-string-or-comment
                                  pre-string-or-comment))

The original idea of electric-pair-syntax-info is that
electric-pair-text-syntax-table is consulted if point is "well within" a
string or comment.  That's why it backtracks a character to establish
pre-string-or-comment.  But for strings started with multiple characters
it failed, as you well noticed.

So my patch makes it more likely that it understands if point is right
after the string start.  Maybe better less brittle solutions can
probably be found within the syntax tables framework.  If not, we can
make a variable.  I'll think about it better later.  Anyway, I'm not
entirely unhappy with this patch because all the tests pass, and they
are reasonably strict and sensitive to this stuff.  So no breakage there
is a very good sign.

João