Subject: Algorithm in electric-pair--unbalanced-strings-p unsuitable for CC Mode
From: Alan Mackenzie <[email-masked]>
To: João Távora <[email-masked]>, [email-masked]
Date: Tue, 2 Jul 2019 13:16:32 +0000
List-Id: <debbugs-submit.debbugs.gnu.org>

Hello João and Emacs.

This is a follow up bug to bug #36423: 27.0.50; electric-pair-mode not
working properly depending of file content.

Start the Emacs master (up to date state as of 2019-07-02T14:30 +0000)
with emacs -Q, put the following in a C++ Mode buffer and enable
electric-pair-mode:

"foo\n

.  Type a " at the end of foo.  electric-pair-mode wrongly inserts two
"s.

Diagnosis: electric-pair--unbalanced-strings-p works after the (single)
newly typed " has been stripped from the buffer.  It attempts to
determine whether there are any open strings after the point of
insertion.  It does this by using parse-partial-sexp, and checks (nth 3
<result>) as evidence of an open string.

This does not work in CC Mode, since although there is an open string
marker (with a string fence syntax-table property on it) this is
"closed" (from parse-partial-sexp's point of view) by the string fence
property on the newline at the end of the line.
electric-pair--unbalanced-strings-p thus returns the wrong result.

A more suitable algorithm might look something like this: check whether
the newly inserted " has a string fence syntax-table text property.
(Its insertion will have already triggered the before- and
after-change-functions which set this property.)  If so, there is an
open string.  Of course, this only applies to CC Mode modes.

-- 
Alan Mackenzie (Nuremberg, Germany).