diff options
| author | Tino Calancha | 2018-06-03 23:28:24 +0900 |
|---|---|---|
| committer | Tino Calancha | 2018-06-03 23:28:24 +0900 |
| commit | 031004e81b1507c4594ae253faaafcda31f253c8 (patch) | |
| tree | 7f3c92e2169d93ee3f74facf5a4a24f8db6e1307 | |
| parent | 50c0624b2aecb9668505eb2cea3f30aecbf6d1ec (diff) | |
| download | emacs-031004e81b1507c4594ae253faaafcda31f253c8.tar.gz emacs-031004e81b1507c4594ae253faaafcda31f253c8.zip | |
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)
| -rw-r--r-- | lisp/replace.el | 4 | ||||
| -rw-r--r-- | test/lisp/replace-tests.el | 20 |
2 files changed, 24 insertions, 0 deletions
diff --git a/lisp/replace.el b/lisp/replace.el index d1eabb035d3..88da7e26cb0 100644 --- a/lisp/replace.el +++ b/lisp/replace.el | |||
| @@ -2147,6 +2147,10 @@ passed in. If LITERAL is set, no checking is done, anyway." | |||
| 2147 | noedit nil))) | 2147 | noedit nil))) |
| 2148 | (set-match-data match-data) | 2148 | (set-match-data match-data) |
| 2149 | (replace-match newtext fixedcase literal) | 2149 | (replace-match newtext fixedcase literal) |
| 2150 | ;; `query-replace' undo feature needs the beginning of the match position, | ||
| 2151 | ;; but `replace-match' may change it, for instance, with a regexp like "^". | ||
| 2152 | ;; Ensure that this function preserves the match data (Bug#31492). | ||
| 2153 | (set-match-data match-data) | ||
| 2150 | ;; `replace-match' leaves point at the end of the replacement text, | 2154 | ;; `replace-match' leaves point at the end of the replacement text, |
| 2151 | ;; so move point to the beginning when replacing backward. | 2155 | ;; so move point to the beginning when replacing backward. |
| 2152 | (when backward (goto-char (nth 0 match-data))) | 2156 | (when backward (goto-char (nth 0 match-data))) |
diff --git a/test/lisp/replace-tests.el b/test/lisp/replace-tests.el index 40a1a31cf7c..40ee838e679 100644 --- a/test/lisp/replace-tests.el +++ b/test/lisp/replace-tests.el | |||
| @@ -399,5 +399,25 @@ Each element has the format: | |||
| 399 | ;; After undo text must be the same. | 399 | ;; After undo text must be the same. |
| 400 | (should (string= text (buffer-string)))))) | 400 | (should (string= text (buffer-string)))))) |
| 401 | 401 | ||
| 402 | (ert-deftest query-replace-undo-bug31492 () | ||
| 403 | "Test for https://debbugs.gnu.org/31492 ." | ||
| 404 | (let ((text "a\nb\nc\n") | ||
| 405 | (count 0) | ||
| 406 | (inhibit-message t)) | ||
| 407 | (with-temp-buffer | ||
| 408 | (insert text) | ||
| 409 | (goto-char 1) | ||
| 410 | (cl-letf (((symbol-function 'read-event) | ||
| 411 | (lambda (&rest args) | ||
| 412 | (cl-incf count) | ||
| 413 | (let ((val (pcase count | ||
| 414 | ((or 1 2) ?\s) ; replace current and go next | ||
| 415 | (3 ?U) ; undo-all | ||
| 416 | (_ ?q)))) ; exit | ||
| 417 | val)))) | ||
| 418 | (perform-replace "^\\|\b\\|$" "foo" t t nil)) | ||
| 419 | ;; After undo text must be the same. | ||
| 420 | (should (string= text (buffer-string)))))) | ||
| 421 | |||
| 402 | 422 | ||
| 403 | ;;; replace-tests.el ends here | 423 | ;;; replace-tests.el ends here |