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

From: [email-masked] (GNU bug Tracking System)
Date: Tue, 21 Sep 2021 10:00:04 +0000

Your bug report

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

which was filed against the emacs package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
49518: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=49518
GNU Bug Tracking System
Contact [email-masked] with problems

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]>
To: Jim Porter <[email-masked]>, [email-masked]
Cc: Stephen Berman <[email-masked]>, Daniel Fleischer <[email-masked]>, Stefan Monnier <[email-masked]>
Date: Tue, 21 Sep 2021 10:59:37 +0100
List-Id: <debbugs-submit.debbugs.gnu.org>

After some more testing, pushed the aforementioned simple fix to
master. Expanded the electric-pair-mode tests to python, too.

See commit 0646c6817139aa905a2f6079fdc82eb4be944de0
and commit 9ad962e118a17daf073ef5308233f9301755035d

Marking this bug done as all the problems mentioned here seem
to be solved.  I'd appreciate confirmation/feedback.

João

Subject: 28.0.50; electric-pair-mode skip-self fails for single-quotes in python-mode
From: Jim Porter <[email-masked]>
To: [email-masked]
Date: Sat, 10 Jul 2021 18:32:34 -0700
List-Id: <debbugs-submit.debbugs.gnu.org>

To reproduce this issue:

  1.  emacs -Q --eval '(electric-pair-mode)' foo.py
  2a. Type " (double-quote); result should be "|", where | is the point
  2b. Type " (double-quote); result should be ""|
      (Note: This is the correct behavior, as I understand it.)
  3.  Delete everything (optional)
  4a. Type ' (single-quote); result should be '|'
  4b. Type ' (single-quote); result is ''|', but (I think) it should be ''|

Having looked through the source of both `electric-pair-mode' and
`python-mode', I believe I understand the problem.

First, some background: in `electric-pair-syntax-info', it checks
whether the point is inside a string or comment. If so, it uses
`electric-pair-text-syntax-table' (defaulting to
`prog-mode-syntax-table'); if not, it uses `python-mode-syntax-table'.
Normally, this means if you have a snippet like ''|', the point is
*not* inside string; it's after the end of one string and before the
start of the next.

However, Python has multiline strings, which causes an issue here.
When python-mode sees 3 quotation marks in a row, it doesn't see that
as an empty string followed by the start of a separate string; it sees
the start of a multiline string. Thus, when we have our snippet ''|',
the point is considered to be in a string. That means
`electric-pair-syntax-info' uses `electric-pair-text-syntax-table'.
Since the default prog-mode syntax table doesn't consider single-quote
to be a quote character, `electric-pair-syntax-info' ends up returning
nil.

Notably, this *isn't* an issue for double-quote. While we
(temporarily) have the state ""|", double-quote is a quote character
according to `electric-pair-text-syntax-table', and so skip-self
proceeds as expected.

(You might think the fix here would be to do like `emacs-lisp-mode'
and locally set `electric-pair-text-pairs' to include single-quote.
This wouldn't work though, since it would make it hard to type an
apostrophe in a word; `electric-pair-mode' would double it up.)

One possible fix would be for `python-mode' to locally set
`electric-pair-text-syntax-table' to `python-mode-syntax-table'. This
seems to work for me after some basic testing, but I'm not confident
it's the right thing to do here; I don't see any examples of other
modes that do this, and I haven't thought over all the possible
problems with it.