Subject: Re: 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: Thu, 16 Sep 2021 14:36:06 -0700

On 9/16/2021 1:49 PM, Alan Mackenzie wrote:
> There were two or three minor problems with the patch:
> 
> 1-/.  CC Mode doesn't use syntax-ppss at all.  This was because way back
> when, syntax-ppss was buggy, and even now doesn't do the right thing for
> CC Mode in some edge cases (e.g. with the buffer narrowed and point-min
> inside a string or comment).  Instead it uses its own internal syntactic
> cacheing, largely centred around the function c-semi-pp-to-literal.
> 
> 2/-  Rather than using get-text-property and friends directly, CC Mode
> uses the macros c-get-char-property, etc.  This is (?was) to maintain
> compatibility with XEmacs.
> 
> 3/- (A bit more serious) The patch looks for the last " in the current
> line without taking account of any escaped new lines.  There is already
> a CC Mode macro which does all the work here, c-point, which can be given
> the argument 'eoll for "end of logical line".

Thanks, I've incorporated all these changes into the attached patch. The 
only difference between my patch and the version you provided was to 
keep the `(search-backward "\"")' portion from my patch, since the code 
needs to find the last double-quote, not the end of line.

As an aside, it's probably worth explaining why my patch searches for 
the last double-quote in the first place. As far as I understand CC 
Mode, when there's an unterminated double-quote on a line, both the 
quote and the newline have a string fence property applied to them. This 
means we could check the newline for that property, *but* there's no 
guarantee a newline actually exists: `(c-point 'eoll)' could actually 
point to the end of the buffer. To get around that, we search backwards 
for the last double-quote of the (logical) line, since that's guaranteed 
to exist.

If we wanted to, we could avoid searching backwards for the last 
double-quote when a newline exists, but I'm not sure the gain in 
performance (likely very small) is worth the extra code complexity.
From 4759ed0c8fa7fa8fafd47d166b3f126e606ffe84 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 057d292246..60cea0b858 100644
--- a/lisp/progmodes/cc-mode.el
+++ b/lisp/progmodes/cc-mode.el
@@ -2550,18 +2550,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 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