diff options
| author | Tino Calancha | 2019-08-19 17:32:09 +0200 |
|---|---|---|
| committer | Tino Calancha | 2019-08-19 17:32:09 +0200 |
| commit | beb1d22260af2e03d80d34fcc1db212785a9d903 (patch) | |
| tree | 04e6f0becdd395cba0f221bed564f2e5defe44fb | |
| parent | 190565b2396d80178fc5f6757117540e3a1ae9e1 (diff) | |
| download | emacs-beb1d22260af2e03d80d34fcc1db212785a9d903.tar.gz emacs-beb1d22260af2e03d80d34fcc1db212785a9d903.zip | |
Fix query-replace-regexp undo feature
Ensure that non-regexp strings used with `looking-at' are quoted.
* lisp/replace.el (perform-replace): Quote regexp (Bug#37073).
* test/lisp/replace-tests.el (replace-tests-perform-replace-regexp-flag):
New variable.
(replace-tests-with-undo): Use it.
(query-replace-undo-bug37073): Add tests.
| -rw-r--r-- | lisp/replace.el | 7 | ||||
| -rw-r--r-- | test/lisp/replace-tests.el | 26 |
2 files changed, 30 insertions, 3 deletions
diff --git a/lisp/replace.el b/lisp/replace.el index 08feb8eae7e..0ddebb12704 100644 --- a/lisp/replace.el +++ b/lisp/replace.el | |||
| @@ -2614,7 +2614,8 @@ It must return a string." | |||
| 2614 | (setq real-match-data | 2614 | (setq real-match-data |
| 2615 | (save-excursion | 2615 | (save-excursion |
| 2616 | (goto-char (match-beginning 0)) | 2616 | (goto-char (match-beginning 0)) |
| 2617 | (looking-at search-string) | 2617 | ;; We must quote the string (Bug#37073) |
| 2618 | (looking-at (regexp-quote search-string)) | ||
| 2618 | (match-data t (nth 2 elt))) | 2619 | (match-data t (nth 2 elt))) |
| 2619 | noedit | 2620 | noedit |
| 2620 | (replace-match-maybe-edit | 2621 | (replace-match-maybe-edit |
| @@ -2624,7 +2625,9 @@ It must return a string." | |||
| 2624 | real-match-data | 2625 | real-match-data |
| 2625 | (save-excursion | 2626 | (save-excursion |
| 2626 | (goto-char (match-beginning 0)) | 2627 | (goto-char (match-beginning 0)) |
| 2627 | (looking-at next-replacement) | 2628 | (if regexp-flag |
| 2629 | (looking-at next-replacement) | ||
| 2630 | (looking-at (regexp-quote next-replacement))) | ||
| 2628 | (match-data t (nth 2 elt)))) | 2631 | (match-data t (nth 2 elt)))) |
| 2629 | ;; Set replaced nil to keep in loop | 2632 | ;; Set replaced nil to keep in loop |
| 2630 | (when (eq def 'undo-all) | 2633 | (when (eq def 'undo-all) |
diff --git a/test/lisp/replace-tests.el b/test/lisp/replace-tests.el index cd30633e377..cd08a522e39 100644 --- a/test/lisp/replace-tests.el +++ b/test/lisp/replace-tests.el | |||
| @@ -365,6 +365,9 @@ Each element has the format: | |||
| 365 | (defvar replace-tests-bind-read-string nil | 365 | (defvar replace-tests-bind-read-string nil |
| 366 | "A string to bind `read-string' and avoid the prompt.") | 366 | "A string to bind `read-string' and avoid the prompt.") |
| 367 | 367 | ||
| 368 | (defvar replace-tests-perform-replace-regexp-flag t | ||
| 369 | "Value for regexp-flag argument passed to `perform-replace' in undo tests.") | ||
| 370 | |||
| 368 | (defmacro replace-tests-with-undo (input from to char-nums def-chr &rest body) | 371 | (defmacro replace-tests-with-undo (input from to char-nums def-chr &rest body) |
| 369 | "Helper to test `query-replace' undo feature. | 372 | "Helper to test `query-replace' undo feature. |
| 370 | INPUT is a string to insert in a temporary buffer. | 373 | INPUT is a string to insert in a temporary buffer. |
| @@ -412,7 +415,7 @@ Return the last evalled form in BODY." | |||
| 412 | (if replace-tests-bind-read-string | 415 | (if replace-tests-bind-read-string |
| 413 | (lambda (&rest args) replace-tests-bind-read-string) | 416 | (lambda (&rest args) replace-tests-bind-read-string) |
| 414 | (symbol-function 'read-string)))) | 417 | (symbol-function 'read-string)))) |
| 415 | (perform-replace ,from ,to t t nil)) | 418 | (perform-replace ,from ,to t replace-tests-perform-replace-regexp-flag nil)) |
| 416 | ,@body)))) | 419 | ,@body)))) |
| 417 | 420 | ||
| 418 | (defun replace-tests--query-replace-undo (&optional comma) | 421 | (defun replace-tests--query-replace-undo (&optional comma) |
| @@ -454,5 +457,26 @@ Return the last evalled form in BODY." | |||
| 454 | input "a" "B" ((?\s . (1 2 3)) (?E . (4)) (?U . (5))) ?q | 457 | input "a" "B" ((?\s . (1 2 3)) (?E . (4)) (?U . (5))) ?q |
| 455 | (string= input (buffer-string)))))) | 458 | (string= input (buffer-string)))))) |
| 456 | 459 | ||
| 460 | (ert-deftest query-replace-undo-bug37073 () | ||
| 461 | "Test for https://debbugs.gnu.org/37073 ." | ||
| 462 | (let ((input "theorem 1\ntheorem 2\ntheorem 3")) | ||
| 463 | (should | ||
| 464 | (replace-tests-with-undo | ||
| 465 | input "theorem \\([0-9]+\\)" | ||
| 466 | "theorem \\\\ref{theo_\\1}" | ||
| 467 | ((?\s . (1 2)) (?U . (3))) | ||
| 468 | ?q | ||
| 469 | (string= input (buffer-string))))) | ||
| 470 | ;; Now run a test with regexp-flag arg in `perform-replace' set to nil | ||
| 471 | (let ((input " ^theorem$ 1\n ^theorem$ 2\n ^theorem$ 3") | ||
| 472 | (replace-tests-perform-replace-regexp-flag nil) | ||
| 473 | (expected " theo 1\n ^theorem$ 2\n ^theorem$ 3")) | ||
| 474 | (should | ||
| 475 | (replace-tests-with-undo | ||
| 476 | input "^theorem$" | ||
| 477 | "theo" | ||
| 478 | ((?\s . (1 2 4)) (?U . (3))) | ||
| 479 | ?q | ||
| 480 | (string= expected (buffer-string)))))) | ||
| 457 | 481 | ||
| 458 | ;;; replace-tests.el ends here | 482 | ;;; replace-tests.el ends here |