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

From: Jim Porter <[email-masked]>
Date: Mon, 27 Sep 2021 21:57:07 -0700

On 9/26/2021 1:58 PM, Alan Mackenzie wrote:
> 
> There's one thing I'm a bit uncertain about in the patch for cc-mode.el.
> 
> On Wed, Sep 22, 2021 at 19:01:11 -0700, Jim Porter wrote:
[snip]
>> -  (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 (memq (cadr (c-semi-pp-to-literal (1- (point)))) '(c c++)))
>> +	   (let ((last-quote (save-match-data
>> +			       (save-excursion
>> +				 (goto-char (c-point 'eoll))
>> +				 (search-backward "\"")))))
>> +	     (not (equal (c-get-char-property last-quote 'c-fl-syn-tab)
>> +			 '(15)))))
>> +      (funcall (default-value 'electric-pair-inhibit-predicate) char)))
>   
> In the line starting (or (and (eq char ?\"), don't we still need an `if'
> form?  I think that otherwise, if any of the sub-forms of the `and'
> return nil, we will call (default-value
> 'electric-pair-inhibit-predicate), which surely isn't what we want.  If
> we have a ", we want the CC Mode function to do all the work, only
> delegating to the standard function when we don't have a ".
> 
> Or have I missed something?

I don't have a strong opinion on this either way, so here's a patch that 
uses an `if' form as you suggest. However, I introduced the `or' form 
in response to Eli's concern[1]:

On 9/11/2021 11:26 PM, Eli Zaretskii wrote:
> 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?  It could be that the user wants to modify the existing string
> instead, in which case your suggested patches will require the user
> to delete more quotes than previously.

I discussed the pros and cons in my followup[2]:

> 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 [override `c-electric-pair-inhibit-predicate'].[3]

Personally, I'm fine with letting CC Mode do all the work here, and 
requiring users to advise this function if they want a different 
behavior. It's probably easier to be sure that things don't break by 
using the `if' form patch, though I think the previous `or' form patch 
should be pretty robust too.

[1] https://lists.gnu.org/archive/html/bug-gnu-emacs/2021-09/msg00976.html
[2] https://lists.gnu.org/archive/html/bug-gnu-emacs/2021-09/msg01014.html
[3] My explanation in the original message was incorrect, so I've fixed 
it here.
From 5c12fe09f7ac710b2c2f279e0faa04be8652db0f Mon Sep 17 00:00:00 2001
From: Jim Porter <[email-masked]>
Date: Thu, 16 Sep 2021 14:32:17 -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   | 22 +++++++++++++++-------
 test/lisp/electric-tests.el |  5 ++++-
 2 files changed, 19 insertions(+), 8 deletions(-)

diff --git a/lisp/progmodes/cc-mode.el b/lisp/progmodes/cc-mode.el
index 8b30241449..80137590c7 100644
--- a/lisp/progmodes/cc-mode.el
+++ b/lisp/progmodes/cc-mode.el
@@ -2549,18 +2549,26 @@ 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 (memq (cadr (c-semi-pp-to-literal (1- (point)))) '(c c++)))
+	   (let ((last-quote (save-match-data
+			       (save-excursion
+				 (goto-char (c-point 'eoll))
+				 (search-backward "\"")))))
+	     (not (equal (c-get-char-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 4e7cbf5419..5eec058ede 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))
@@ -174,7 +177,7 @@ define-electric-pair-test
           expected-string
           expected-point
           bindings
-          (modes '(quote (ruby-mode js-mode python-mode)))
+          (modes '(quote (ruby-mode js-mode python-mode c-mode)))
           (test-in-comments t)
           (test-in-strings t)
           (test-in-code t)
-- 
2.25.1