unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Tino Calancha <tino.calancha@gmail.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 31492@debbugs.gnu.org
Subject: bug#31492: 26.1; query-replace-regexp undo fails in regexps w/o printable chars
Date: Mon, 21 May 2018 10:51:43 +0900	[thread overview]
Message-ID: <87y3gdhj2o.fsf@gmail.com> (raw)
In-Reply-To: <83po1qfhxd.fsf@gnu.org> (Eli Zaretskii's message of "Sun, 20 May 2018 18:47:10 +0300")

Eli Zaretskii <eliz@gnu.org> writes:


>> we can use the first patch; the one with the test:
>> (/= (match-beginning 0) (match-end 0))
>
> No, I like the 2nd one better.
Following patch is equivalent to the 2nd and its 1-liner
(excluded clarification comments):
--8<-----------------------------cut here---------------start------------->8---
commit 565c30caa8d5b015fd34446bd8900c55b2f67544
Author: Tino Calancha <tino.calancha@gmail.com>
Date:   Mon May 21 10:46:50 2018 +0900

    Fix corner case in query-replace-regexp undo
    
    This commit fixes Bug#31492.
    * lisp/replace.el (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..a17dd19b0d 100644
--- a/lisp/replace.el
+++ b/lisp/replace.el
@@ -2214,6 +2214,10 @@ replace-match-maybe-edit
 	    noedit nil)))
   (set-match-data match-data)
   (replace-match newtext fixedcase literal)
+  ;; `query-replace' undo feature needs the beginning of the match position,
+  ;; but `replace-match' may change it, for instance, with a regexp like "^".
+  ;; Ensure that this function preserves the match data (Bug#31492).
+  (set-match-data match-data)
   ;; `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---





  reply	other threads:[~2018-05-21  1:51 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-18 13:27 bug#31492: 26.1; query-replace-regexp undo fails in regexps w/o printable chars Tino Calancha
2018-05-18 14:16 ` Eli Zaretskii
2018-05-18 14:22   ` Tino Calancha
2018-05-18 15:07     ` Eli Zaretskii
2018-05-19  1:46       ` Tino Calancha
2018-05-19  7:50         ` Eli Zaretskii
2018-05-19 14:28           ` Tino Calancha
2018-05-20  9:29             ` Eli Zaretskii
2018-05-20 11:51               ` Tino Calancha
2018-05-20 11:59                 ` Eli Zaretskii
2018-05-20 12:06                   ` Tino Calancha
2018-05-20 12:48                     ` Eli Zaretskii
2018-05-20 13:46                       ` Tino Calancha
2018-05-20 15:47                         ` Eli Zaretskii
2018-05-21  1:51                           ` Tino Calancha [this message]
2018-05-21 15:05                             ` Eli Zaretskii
2018-05-23  9:22                               ` Tino Calancha

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87y3gdhj2o.fsf@gmail.com \
    --to=tino.calancha@gmail.com \
    --cc=31492@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).