unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#50538: [PATCH] 28.0.50; electric-pair-mode fails to pair double quotes in some cases in CC mode
@ 2021-09-12  3:58 Jim Porter
  2021-09-12  6:26 ` Eli Zaretskii
  0 siblings, 1 reply; 24+ messages in thread
From: Jim Porter @ 2021-09-12  3:58 UTC (permalink / raw)
  To: 50538

[-- Attachment #1: Type: text/plain, Size: 2302 bytes --]

(Note: I've just updated my copyright assignment information, but 
haven't received confirmation that everything is in order, so this might 
need to wait until that's done for it to merge.)

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. You can 
try similar steps in a `ruby-mode' buffer to see how it should work.

----------------------------------------

Common setup
------------

   $ cat foo.c
   "foobar"

   $ emacs -Q foo.c
   M-x electric-pair-mode

Note that | represents the point below.

1. Quote pairing in comments
----------------------------

   C-o   ;; to make a blank line
   // "  ;; type this

Expected: line 1 is // "|"
Actual:   line 1 is // "|

2. Inserting quote pairs before existing string
-----------------------------------------------

   "  ;; type this (point is at beginning of buffer, before "foobar")

Expected: line 1 is "|""foobar"
Actual:   line 1 is "|"foobar"

3. Splitting strings into two
-----------------------------

   "foo|bar"  ;; move point here
   "          ;; type this

Expected: line 1 is "foo"|"bar"
Actual:   line 1 is "foo"|bar"

----------------------------------------

This is because the logic in the patch for bug#36474 isn't quite right. 
Currently, `c-electric-pair-inhibit-predicate' checks if the 
newly-inserted quotation mark has "a string fence syntax-table text 
property" (i.e. if it's the start of a string literal not terminated on 
that line[1]). However, this fails in all three cases above: in (1) 
because we're in a comment, not a string literal; and in (2) and (3) 
because it's the *last* quotation mark on the line that's unterminated, 
not the one before point.

The attached patch fixes this by taking those cases into account. I also 
added `c-mode' to the list of modes to check in 
`test/lisp/electric-tests.el'. This required setting single-line 
comments as the default in those tests, since the tests expect 
single-line comments (I tried testing under `c++-mode', but the tests 
failed, I think due to <> being paren-like in C++).

[1] I think this is what it means, at least (or close to it).

[-- Attachment #2: 0001-Improve-behavior-of-electric-pair-mode-in-cc-mode.patch --]
[-- Type: text/plain, Size: 3189 bytes --]

From d9c8879b3082a40375552ddb629c83a0d941bff9 Mon Sep 17 00:00:00 2001
From: Jim Porter <jporterbugs@gmail.com>
Date: Sun, 29 Aug 2021 12:40:26 -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   | 19 +++++++++++++------
 test/lisp/electric-tests.el |  5 ++++-
 2 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/lisp/progmodes/cc-mode.el b/lisp/progmodes/cc-mode.el
index 057d292246..e2e50efa63 100644
--- a/lisp/progmodes/cc-mode.el
+++ b/lisp/progmodes/cc-mode.el
@@ -2550,17 +2550,24 @@ 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)))
+  (if (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)))
 
 \f
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


^ permalink raw reply related	[flat|nested] 24+ messages in thread

end of thread, other threads:[~2021-11-06  0:22 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-09-12  3:58 bug#50538: [PATCH] 28.0.50; electric-pair-mode fails to pair double quotes in some cases in CC mode Jim Porter
2021-09-12  6:26 ` Eli Zaretskii
2021-09-12 18:05   ` Jim Porter
2021-09-15 22:17     ` Jim Porter
2021-09-16  5:25       ` Eli Zaretskii
2021-09-16 12:40         ` Lars Ingebrigtsen
2021-09-16 12:59           ` Dmitry Gutov
2021-09-16 13:17             ` Lars Ingebrigtsen
2021-09-16 17:04               ` João Távora
2021-09-16 17:11                 ` Eli Zaretskii
2021-09-16 17:33                   ` João Távora
2021-09-16 17:29                 ` Jim Porter
2021-09-16 19:05                 ` Alan Mackenzie
2021-09-16 20:51                   ` João Távora
2021-09-17 18:12                     ` Alan Mackenzie
2021-09-16 18:26         ` Alan Mackenzie
2021-09-16 20:49     ` Alan Mackenzie
2021-09-16 21:36       ` Jim Porter
2021-09-17 17:08         ` Alan Mackenzie
2021-09-23  2:01           ` bug#50538: [PATCH v3] " Jim Porter
2021-09-26 20:58             ` Alan Mackenzie
2021-09-28  4:57               ` bug#50538: [PATCH v4] " Jim Porter
2021-10-03 17:58                 ` bug#50538: [PATCH v5] " Jim Porter
2021-11-06  0:22                   ` bug#50538: [PATCH] " Lars Ingebrigtsen

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).