From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: Alan Mackenzie Newsgroups: gmane.emacs.bugs Subject: bug#39680: 27.0.60; electric-pair-mode broken by undo Date: Tue, 25 Feb 2020 19:34:51 +0000 Message-ID: <20200225193451.GA5896@ACM> References: <87pneaifya.fsf@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="95742"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mutt/1.10.1 (2018-07-13) Cc: 39680@debbugs.gnu.org To: =?UTF-8?Q?K=C3=A9vin?= Le Gouguec , Stefan Monnier Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue Feb 25 20:36:34 2020 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1j6g0P-000Omm-QM for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 25 Feb 2020 20:36:33 +0100 Original-Received: from localhost ([::1]:34344 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j6g0O-0005ME-Sa for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 25 Feb 2020 14:36:32 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:34209) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j6fz0-0003vQ-Kf for bug-gnu-emacs@gnu.org; Tue, 25 Feb 2020 14:35:07 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j6fyy-0001WG-Cu for bug-gnu-emacs@gnu.org; Tue, 25 Feb 2020 14:35:06 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]:50170) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1j6fyx-0001VA-2G for bug-gnu-emacs@gnu.org; Tue, 25 Feb 2020 14:35:03 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1j6fyw-0002NE-Vz for bug-gnu-emacs@gnu.org; Tue, 25 Feb 2020 14:35:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Alan Mackenzie Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 25 Feb 2020 19:35:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 39680 X-GNU-PR-Package: emacs Original-Received: via spool by 39680-submit@debbugs.gnu.org id=B39680.15826592989104 (code B ref 39680); Tue, 25 Feb 2020 19:35:02 +0000 Original-Received: (at 39680) by debbugs.gnu.org; 25 Feb 2020 19:34:58 +0000 Original-Received: from localhost ([127.0.0.1]:56142 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1j6fys-0002Mm-0Z for submit@debbugs.gnu.org; Tue, 25 Feb 2020 14:34:58 -0500 Original-Received: from colin.muc.de ([193.149.48.1]:27816 helo=mail.muc.de) by debbugs.gnu.org with smtp (Exim 4.84_2) (envelope-from ) id 1j6fyo-0002Ma-K8 for 39680@debbugs.gnu.org; Tue, 25 Feb 2020 14:34:56 -0500 Original-Received: (qmail 93093 invoked by uid 3782); 25 Feb 2020 19:34:52 -0000 Original-Received: from acm.muc.de (p4FE15DA7.dip0.t-ipconnect.de [79.225.93.167]) by colin.muc.de (tmda-ofmipd) with ESMTP; Tue, 25 Feb 2020 20:34:51 +0100 Original-Received: (qmail 6148 invoked by uid 1000); 25 Feb 2020 19:34:51 -0000 Content-Disposition: inline In-Reply-To: <87pneaifya.fsf@gmail.com> X-Delivery-Agent: TMDA/1.1.12 (Macallan) X-Primary-Address: acm@muc.de X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.51.188.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:176475 Archived-At: Hello, Kévin and Stefan. On Wed, Feb 19, 2020 at 19:34:37 +0100, Kévin Le Gouguec wrote: > Hello, > Commit e66d5a1c45 (2019-02-19T00:00:44Z!monnier@iro.umontreal.ca) might > have introduced a bug. From emacs -Q: > 1. C-x b foo RET > 2. M-x electric-pair-mode RET > 3. ( > - A closing parenthesis has been inserted. > 4. C-b C-f > - This is to break undo grouping. > 5. a > 6. C-_ > 7. ( > In Emacs 26.3, buffer foo contains "(())" and point is after the > innermost opening bracket. > In Emacs 27, buffer foo contains "()" and point is after the closing > bracket. The *Messages* buffer shows: > > cancel-change-group: Undoing to some unrelated state The cause of this bug is a bug in cancel-change-group, part of the atomic-change-group group of functions. The commit you (Kévin) refer to above substitutes atomic-change-group for a simple insertion and deletion of a character (for a good reason). cancel-change-group looks like this: (defun cancel-change-group (handle) "Finish a change group made with `prepare-change-group' (which see). This finishes the change group by reverting all of its changes." (dolist (elt handle) (with-current-buffer (car elt) (setq elt (cdr elt)) (save-restriction ;; Widen buffer temporarily so if the buffer was narrowed within ;; the body of `atomic-change-group' all changes can be undone. (widen) (let ((old-car (car-safe elt)) (old-cdr (cdr-safe elt))) (unwind-protect (progn ;; Temporarily truncate the undo log at ELT. (when (consp elt) (setcar elt nil) (setcdr elt nil)) (unless (eq last-command 'undo) (undo-start)) <======= ;; Make sure there's no confusion. ============> (when (and (consp elt) (not (eq elt (last pending-undo-list)))) (error "Undoing to some unrelated state")) ;; Undo it all. (save-excursion (while (listp pending-undo-list) (undo-more 1))) ;; Revert the undo info to what it was when we grabbed ;; the state. (setq buffer-undo-list elt)) ;; Reset the modified cons cell ELT to its original content. (when (consp elt) (setcar elt old-car) (setcdr elt old-cdr)))))))) On entry to this function, HANDLE has the value: ((# (2 . 3) nil ("a" . 2) (# . -1) nil (2 . 3))) . At the first indicated spot above, last-command is indeed 'undo, so undo-start is not invoked. Since the undo which undid "a" emptied pending-undo-list, pending-undo-list has been set to t. So when the eq is done in the second indicated spot, pending-undo-list is not ELT (the first cons cell from (cdr handle)). The function thus spuriously signals the error "Undoing to some unrelated state". ######################################################################### I admit I don't fully understand the mechanism of atomic-change-group, but I see the problem as the EQ comparison on the <======= line. If pending-undo-list has been replaced by t (after being exhausted by the previous 'undo operation), there is no point EQing it with the cons from HANDLE. So, as a first approximation to a fix, I added a (consp pending-undo-list) into this test, as follow: diff --git a/lisp/subr.el b/lisp/subr.el index b5ec0de156..8b7d9b5451 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -2975,7 +2975,9 @@ cancel-change-group ;; Temporarily truncate the undo log at ELT. (when (consp elt) (setcar elt nil) (setcdr elt nil)) - (unless (eq last-command 'undo) (undo-start)) + (unless (and (eq last-command 'undo) + (consp pending-undo-list)) + (undo-start)) ;; Make sure there's no confusion. (when (and (consp elt) (not (eq elt (last pending-undo-list)))) (error "Undoing to some unrelated state")) Stefan, what is your view on this attempted patch? Is it sound? [ .... ] > Thank you for your time. Thank you for a good bug report, conveniently reduced to a minimum test case. > In GNU Emacs 28.0.50 (build 5, x86_64-pc-linux-gnu, GTK+ Version 3.24.13, cairo version 1.16.0) > of 2020-02-19 built on my-little-tumbleweed > Repository revision: e1e1bd8f85c53fea9f61b6ec99b461ddd93461b9 > Repository branch: master > Windowing system distributor 'The X.Org Foundation', version 11.0.12007000 > System Description: openSUSE Tumbleweed > Configured using: > 'configure --with-xwidgets --with-cairo' -- Alan Mackenzie (Nuremberg, Germany).