[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Emacs-diffs] emacs-26 031004e 2/3: Backport: Fix corner case in query-r
From: |
Tino Calancha |
Subject: |
[Emacs-diffs] emacs-26 031004e 2/3: Backport: Fix corner case in query-replace-regexp undo |
Date: |
Sun, 3 Jun 2018 10:30:17 -0400 (EDT) |
branch: emacs-26
commit 031004e81b1507c4594ae253faaafcda31f253c8
Author: Tino Calancha <address@hidden>
Commit: Tino Calancha <address@hidden>
Backport: 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.
(cherry picked from commit bab73230d1be1fe394b7269c1365ef6fb1a5d9b3)
---
lisp/replace.el | 4 ++++
test/lisp/replace-tests.el | 20 ++++++++++++++++++++
2 files changed, 24 insertions(+)
diff --git a/lisp/replace.el b/lisp/replace.el
index d1eabb0..88da7e2 100644
--- a/lisp/replace.el
+++ b/lisp/replace.el
@@ -2147,6 +2147,10 @@ passed in. If LITERAL is set, no checking is done,
anyway."
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 40a1a31..40ee838 100644
--- a/test/lisp/replace-tests.el
+++ b/test/lisp/replace-tests.el
@@ -399,5 +399,25 @@ Each element has the format:
;; 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