Subject: Re: bug#31492: 26.1; query-replace-regexp undo fails in regexps w/o printable chars

From: Tino Calancha <[email-masked]>
Date: Sun, 20 May 2018 20:51:10 +0900

Eli Zaretskii <[email-masked]> 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 <[email-masked]>
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---