Subject: bug#50538: [PATCH] 28.0.50; electric-pair-mode fails to pair double quotes in some cases in CC mode

From: Jim Porter <[email-masked]>
Date: Sun, 12 Sep 2021 11:05:17 -0700

On 9/11/2021 11:26 PM, Eli Zaretskii wrote:
>> From: Jim Porter <[email-masked]>
>> Date: Sat, 11 Sep 2021 20:58:47 -0700
>>
>> There are a few related issues with pairing double quotes in CC mode
>> while using `electric-pair-mode'. Hopefully the steps to reproduce below
>> will explain the issues. In all the cases, I'd expect
>> `electric-pair-mode' to insert a closing quote, but it doesn't.
> 
> Your expected results seem to expect Emacs to assume that a new string
> will be inserted, but is that an assumption that is always true?

In these cases, I believe that's true (with the default 
`electric-pair-mode' settings, that is). More broadly, the goal of the 
patch is to ensure that pairing of double quotes works the same in CC 
mode as it does in other modes (`ruby-mode', `python-mode', `js-mode', 
`emacs-lisp-mode', etc), which is why I added `c-mode' to the list of 
modes to test in `test/lisp/electric-tests.el'.

That said, there's one potential case I didn't account for (mostly 
because it wasn't accounted for in the patch for bug#36474): if a user 
customizes `electric-pair-inhibit-predicate' to inhibit cases like (2) 
or (3) in my original message, that won't work right in CC modes, since 
the default value of `electric-pair-inhibit-predicate' (set by the user) 
won't be called.

Attached is an updated patch that changes the logic of 
`c-electric-pair-inhibit-predicate' to either a) inhibit insertion of 
the closing quote, or b) call the default-value of 
`electric-pair-inhibit-predicate' to determine what to do. This should 
give users more consistent behavior when customizing 
`electric-pair-inhibit-predicate'.

The tests still pass, although I wasn't able to figure out a good way to 
add a test for setting `electric-pair-inhibit-predicate' that works with 
how CC mode overrides it (using `:bindings' in 
`define-electric-pair-test' didn't work, since the binding is set too 
late). Hopefully that's ok; if not, I can try and see about making some 
more extensive changes to the tests to account for this.

Note however that this solution isn't perfect: it means a user's custom 
`electric-pair-inhibit-predicate' can only inhibit *more* than CC mode's 
default behavior, not less. I think that's a reasonable compromise 
though, and users who want more direct control can set 
`electric-pair-inhibit-predicate' inside `c-mode-common-hook'. A 
"perfect" solution here would probably require adding new customization 
points to `electric-pair-mode' (e.g. a way for major modes to override 
how the syntax is analyzed), and I'm not sure the added complexity would 
be worth it, especially since this code is already a bit tricky.
From 0b038ef868a8c558e66d420b9380b792b409e388 Mon Sep 17 00:00:00 2001
From: Jim Porter <[email-masked]>
Date: Sun, 12 Sep 2021 10:44:55 -0700
Subject: [PATCH] Improve behavior of 'electric-pair-mode' in 'cc-mode'

This ensures that quotes are paired correctly within comments, allows for
insertion of quote pairs immediately before another quote, and allows
inserting quote pairs within a string (thus splitting the string in two).

* lisp/progmodes/cc-mode.el (c-electric-pair-inhibit-predicate):
Inhibit insertion of paired quote in fewer cases.
* test/lisp/electric-tests.el (define-electric-pair-test):
Add 'c-mode' to list of modes to test by default.
---
 lisp/progmodes/cc-mode.el   | 21 ++++++++++++++-------
 test/lisp/electric-tests.el |  5 ++++-
 2 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/lisp/progmodes/cc-mode.el b/lisp/progmodes/cc-mode.el
index 057d292246..012488a7b1 100644
--- a/lisp/progmodes/cc-mode.el
+++ b/lisp/progmodes/cc-mode.el
@@ -2550,18 +2550,25 @@ c-electric-pair-inhibit-predicate
 
 At the time of call, point is just after the newly inserted CHAR.
 
-When CHAR is \", t will be returned unless the \" is marked with
-a string fence syntax-table text property.  For other characters,
-the default value of `electric-pair-inhibit-predicate' is called
-and its value returned.
+When CHAR is \" and not within a comment, t will be returned if
+the quotes on the current line are already balanced (i.e. if the
+last \" is not marked with a string fence syntax-table text
+property).  For other cases, the default value of
+`electric-pair-inhibit-predicate' is called and its value
+returned.
 
 This function is the appropriate value of
 `electric-pair-inhibit-predicate' for CC Mode modes, which mark
 invalid strings with such a syntax table text property on the
 opening \" and the next unescaped end of line."
-  (if (eq char ?\")
-      (not (equal (get-text-property (1- (point)) 'c-fl-syn-tab) '(15)))
-    (funcall (default-value 'electric-pair-inhibit-predicate) char)))
+  (or (and (eq char ?\")
+	   (not (nth 4 (save-excursion (syntax-ppss (1- (point))))))
+	   (let ((last-quote (save-match-data
+			       (save-excursion
+				 (search-forward "\n" nil 'move)
+				 (search-backward "\"" nil)))))
+	     (not (equal (get-text-property last-quote 'c-fl-syn-tab) '(15)))))
+      (funcall (default-value 'electric-pair-inhibit-predicate) char)))
 
 
 ;; Support for C
diff --git a/test/lisp/electric-tests.el b/test/lisp/electric-tests.el
index 666de89c53..35516a9f0b 100644
--- a/test/lisp/electric-tests.el
+++ b/test/lisp/electric-tests.el
@@ -32,6 +32,9 @@
 (require 'elec-pair)
 (require 'cl-lib)
 
+;; When running tests in c-mode, use single-line comments (//).
+(add-hook 'c-mode-hook (lambda () (c-toggle-comment-style -1)))
+
 (defun call-with-saved-electric-modes (fn)
   (let ((saved-electric (if electric-pair-mode 1 -1))
         (saved-layout (if electric-layout-mode 1 -1))
@@ -173,7 +176,7 @@ define-electric-pair-test
           expected-string
           expected-point
           bindings
-          (modes '(quote (ruby-mode js-mode)))
+          (modes '(quote (ruby-mode js-mode c-mode)))
           (test-in-comments t)
           (test-in-strings t)
           (test-in-code t)
-- 
2.25.1