From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Tino Calancha Newsgroups: gmane.emacs.bugs Subject: bug#31492: 26.1; query-replace-regexp undo fails in regexps w/o printable chars Date: Sun, 20 May 2018 20:51:10 +0900 Message-ID: <87sh6mwno1.fsf@gmail.com> References: <8736yp14c7.fsf@gmail.com> <838t8hhwwu.fsf@gnu.org> <837eo1huji.fsf@gnu.org> <83zi0wgk3d.fsf@gnu.org> <87tvr3zpm4.fsf@gmail.com> <83vabifzfg.fsf@gnu.org> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1526817012 468 195.159.176.226 (20 May 2018 11:50:12 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 20 May 2018 11:50:12 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) Cc: 31492@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sun May 20 13:50:08 2018 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1fKMqj-0008P0-Jv for geb-bug-gnu-emacs@m.gmane.org; Sun, 20 May 2018 13:50:05 +0200 Original-Received: from localhost ([::1]:46121 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fKMsp-0004kp-Ve for geb-bug-gnu-emacs@m.gmane.org; Sun, 20 May 2018 07:52:16 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:52078) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fKMsf-0004kW-KI for bug-gnu-emacs@gnu.org; Sun, 20 May 2018 07:52:06 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fKMsc-0003SH-IG for bug-gnu-emacs@gnu.org; Sun, 20 May 2018 07:52:05 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:33765) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fKMsc-0003S9-Eq for bug-gnu-emacs@gnu.org; Sun, 20 May 2018 07:52:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1fKMsc-0000ap-4V for bug-gnu-emacs@gnu.org; Sun, 20 May 2018 07:52:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Tino Calancha Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 20 May 2018 11:52:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 31492 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: Original-Received: via spool by 31492-submit@debbugs.gnu.org id=B31492.15268170862236 (code B ref 31492); Sun, 20 May 2018 11:52:02 +0000 Original-Received: (at 31492) by debbugs.gnu.org; 20 May 2018 11:51:26 +0000 Original-Received: from localhost ([127.0.0.1]:41662 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1fKMs0-0000Zy-Rk for submit@debbugs.gnu.org; Sun, 20 May 2018 07:51:26 -0400 Original-Received: from mail-pg0-f67.google.com ([74.125.83.67]:39938) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1fKMrz-0000Zm-Fx for 31492@debbugs.gnu.org; Sun, 20 May 2018 07:51:23 -0400 Original-Received: by mail-pg0-f67.google.com with SMTP id l2-v6so5152340pgc.7 for <31492@debbugs.gnu.org>; Sun, 20 May 2018 04:51:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version; bh=y7PHizdnpNtCQWlbH5frQCF7JajnxnWm5NB07TpVXCQ=; b=ZqbBp6sRN6j+XH6eTLmyi1fK9ok3lqTe1htDb1XcSFqh5xAnDNApWyPLyMdLECTc4K 0N0K23dL4+naGQVwmJRq2+6rvGEYafS8exHO9OrSYTZcEn3Ur7nWuJR729nrqb0Jsgw+ 8xggfNUgGjtQLxaU94pNXY3yqlz05hSGkTYDXy9xk3RUFTn0KOfhMGqplkspXeBgN4Hu 9M18T7sq8/WUG3rN2yVsmlerzdMcVKZPFCYSfXpYtvW+2Dyluc6AcPQ0dPZ82pkCDsAe d7OpTxFenKtwbDGXCIcZDCNfJy/NffXch7lE+9UWu87TdepTLa/L7hrH8RYFiSr9owqv SHmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version; bh=y7PHizdnpNtCQWlbH5frQCF7JajnxnWm5NB07TpVXCQ=; b=IiwYIiy1biROKSl5rlci6JxdsdhqRbjrMXlM4BBDf7un9qtVEb7pKj/ByYoYvzkCQN HF1V/o0uT7xDKiGsPh1TRxBIHxGjy+rThzm42OH2RRY/1eqQ97WzQUNsHFPLcPrKErEE Cv5tcmraCwlLJLhRLCZ640SuWX62gkjcsSi6jXDN70QZI0D5TpdlwTiSr0nHfY5eVkZ8 nosC6vCAx8KHaNYLbQUwFQYCbgWbT4CRxfIF4L8q0xJaIa8khndbz9tLRZUDKSzWrByV bPNzAfI0K9m+rPIr4+mdgMLr04QGuyiW6oLMGooL3PeeI1RVZafw3EItqFnrxjyMKe8U MVfA== X-Gm-Message-State: ALKqPwdvMkZb5aCRHfmA84jnk0DbL5ng+zasspkFUHoqW6mMmI6nSUwM sGeEYrTFgEVn8WPXh4/UFjJV5A== X-Google-Smtp-Source: AB8JxZoyGmK83vDvQz21JlWtPogfs3sTahHmvQynT2CBIZzeTOvuHxP65Xkq33Vs3dYUOoVwju8nyg== X-Received: by 2002:a65:4945:: with SMTP id q5-v6mr10651987pgs.177.1526817077548; Sun, 20 May 2018 04:51:17 -0700 (PDT) Original-Received: from calancha-pc (228.225.128.101.dy.bbexcite.jp. [101.128.225.228]) by smtp.gmail.com with ESMTPSA id v14-v6sm18662601pfn.105.2018.05.20.04.51.15 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 20 May 2018 04:51:16 -0700 (PDT) In-Reply-To: <83vabifzfg.fsf@gnu.org> (Eli Zaretskii's message of "Sun, 20 May 2018 12:29:07 +0300") 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: 208.118.235.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.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:146317 Archived-At: Eli Zaretskii writes: >> > The relevant >> > element of the replacement stack (whose structure, btw, seems not to >> > be documented anywhere), is (4 4 *scratch*), whereas I'd expect to see >> > (1 4 *scratch) instead, because the replacement was at position 1; >> > then setting match-data from this would DTRT. >> Yes, that is the logic. The thing is, for some unknown reason to me, >> the reported match-data is inexact when there are no printable chars >> in the regexp (maybe it's expected and I am wrong on my assumptions). > > The reason for that is that match-data is recorded as markers, and so > the positions move if text is inserted. In your example, the position > of $ moved due to insertion, so the marker's position was updated as > part of the replacement. Yeah, I have noticed that this afternoon. > Right, and so I submit that the problem is where the replacement stack > is updated: it should account for these subtleties and adjust the > stack positions accordingly, since it has the opportunity to look at > the match position before the matched text is replaced. I have a different approach: If the problem arise from using markers istead of integers, then how about we just use integers? Please take a look: --8<-----------------------------cut here---------------start------------->8--- commit cf13cb2dff040571b289e2ba8dcc0008394ffe3d Author: Tino Calancha Date: Sun May 20 20:43:30 2018 +0900 Fix corner case in query-replace-regexp undo This commit fixes Bug#31492. * lisp/replace.el(replace-save-match-data): New macro. (replace-match-maybe-edit): Preserve match data. * test/lisp/replace-tests.el (query-replace-undo-bug31492): Add test. diff --git a/lisp/replace.el b/lisp/replace.el index 3503b656d9..7d49b977fd 100644 --- a/lisp/replace.el +++ b/lisp/replace.el @@ -2184,6 +2184,16 @@ replace-match-data new))) (match-data integers reuse t))) +(defmacro replace-save-match-data (&rest body) + "Like `save-match-data', but it uses integers instead of markers. +The value returned is the value of the last form in BODY." + (declare (indent 0) (debug t)) + (let ((match (gensym "match-data"))) + `(let ((,match (match-data 'integers))) + (unwind-protect + ,@body + (set-match-data ,match))))) + (defun replace-match-maybe-edit (newtext fixedcase literal noedit match-data &optional backward) "Make a replacement with `replace-match', editing `\\?'. @@ -2213,7 +2223,7 @@ replace-match-maybe-edit nil match-data match-data)))) noedit nil))) (set-match-data match-data) - (replace-match newtext fixedcase literal) + (replace-save-match-data (replace-match newtext fixedcase literal)) ;; `replace-match' leaves point at the end of the replacement text, ;; so move point to the beginning when replacing backward. (when backward (goto-char (nth 0 match-data))) diff --git a/test/lisp/replace-tests.el b/test/lisp/replace-tests.el index 40a1a31cf7..40ee838e67 100644 --- a/test/lisp/replace-tests.el +++ b/test/lisp/replace-tests.el @@ -399,5 +399,25 @@ replace-tests--query-replace-undo ;; After undo text must be the same. (should (string= text (buffer-string)))))) +(ert-deftest query-replace-undo-bug31492 () + "Test for https://debbugs.gnu.org/31492 ." + (let ((text "a\nb\nc\n") + (count 0) + (inhibit-message t)) + (with-temp-buffer + (insert text) + (goto-char 1) + (cl-letf (((symbol-function 'read-event) + (lambda (&rest args) + (cl-incf count) + (let ((val (pcase count + ((or 1 2) ?\s) ; replace current and go next + (3 ?U) ; undo-all + (_ ?q)))) ; exit + val)))) + (perform-replace "^\\|\b\\|$" "foo" t t nil)) + ;; After undo text must be the same. + (should (string= text (buffer-string)))))) + ;;; replace-tests.el ends here --8<-----------------------------cut here---------------end--------------->8---