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.
This commit is contained in:
parent
190565b239
commit
beb1d22260
2 changed files with 30 additions and 3 deletions
|
@ -2614,7 +2614,8 @@ It must return a string."
|
||||||
(setq real-match-data
|
(setq real-match-data
|
||||||
(save-excursion
|
(save-excursion
|
||||||
(goto-char (match-beginning 0))
|
(goto-char (match-beginning 0))
|
||||||
(looking-at search-string)
|
;; We must quote the string (Bug#37073)
|
||||||
|
(looking-at (regexp-quote search-string))
|
||||||
(match-data t (nth 2 elt)))
|
(match-data t (nth 2 elt)))
|
||||||
noedit
|
noedit
|
||||||
(replace-match-maybe-edit
|
(replace-match-maybe-edit
|
||||||
|
@ -2624,7 +2625,9 @@ It must return a string."
|
||||||
real-match-data
|
real-match-data
|
||||||
(save-excursion
|
(save-excursion
|
||||||
(goto-char (match-beginning 0))
|
(goto-char (match-beginning 0))
|
||||||
|
(if regexp-flag
|
||||||
(looking-at next-replacement)
|
(looking-at next-replacement)
|
||||||
|
(looking-at (regexp-quote next-replacement)))
|
||||||
(match-data t (nth 2 elt))))
|
(match-data t (nth 2 elt))))
|
||||||
;; Set replaced nil to keep in loop
|
;; Set replaced nil to keep in loop
|
||||||
(when (eq def 'undo-all)
|
(when (eq def 'undo-all)
|
||||||
|
|
|
@ -365,6 +365,9 @@ Each element has the format:
|
||||||
(defvar replace-tests-bind-read-string nil
|
(defvar replace-tests-bind-read-string nil
|
||||||
"A string to bind `read-string' and avoid the prompt.")
|
"A string to bind `read-string' and avoid the prompt.")
|
||||||
|
|
||||||
|
(defvar replace-tests-perform-replace-regexp-flag t
|
||||||
|
"Value for regexp-flag argument passed to `perform-replace' in undo tests.")
|
||||||
|
|
||||||
(defmacro replace-tests-with-undo (input from to char-nums def-chr &rest body)
|
(defmacro replace-tests-with-undo (input from to char-nums def-chr &rest body)
|
||||||
"Helper to test `query-replace' undo feature.
|
"Helper to test `query-replace' undo feature.
|
||||||
INPUT is a string to insert in a temporary buffer.
|
INPUT is a string to insert in a temporary buffer.
|
||||||
|
@ -412,7 +415,7 @@ Return the last evalled form in BODY."
|
||||||
(if replace-tests-bind-read-string
|
(if replace-tests-bind-read-string
|
||||||
(lambda (&rest args) replace-tests-bind-read-string)
|
(lambda (&rest args) replace-tests-bind-read-string)
|
||||||
(symbol-function 'read-string))))
|
(symbol-function 'read-string))))
|
||||||
(perform-replace ,from ,to t t nil))
|
(perform-replace ,from ,to t replace-tests-perform-replace-regexp-flag nil))
|
||||||
,@body))))
|
,@body))))
|
||||||
|
|
||||||
(defun replace-tests--query-replace-undo (&optional comma)
|
(defun replace-tests--query-replace-undo (&optional comma)
|
||||||
|
@ -454,5 +457,26 @@ Return the last evalled form in BODY."
|
||||||
input "a" "B" ((?\s . (1 2 3)) (?E . (4)) (?U . (5))) ?q
|
input "a" "B" ((?\s . (1 2 3)) (?E . (4)) (?U . (5))) ?q
|
||||||
(string= input (buffer-string))))))
|
(string= input (buffer-string))))))
|
||||||
|
|
||||||
|
(ert-deftest query-replace-undo-bug37073 ()
|
||||||
|
"Test for https://debbugs.gnu.org/37073 ."
|
||||||
|
(let ((input "theorem 1\ntheorem 2\ntheorem 3"))
|
||||||
|
(should
|
||||||
|
(replace-tests-with-undo
|
||||||
|
input "theorem \\([0-9]+\\)"
|
||||||
|
"theorem \\\\ref{theo_\\1}"
|
||||||
|
((?\s . (1 2)) (?U . (3)))
|
||||||
|
?q
|
||||||
|
(string= input (buffer-string)))))
|
||||||
|
;; Now run a test with regexp-flag arg in `perform-replace' set to nil
|
||||||
|
(let ((input " ^theorem$ 1\n ^theorem$ 2\n ^theorem$ 3")
|
||||||
|
(replace-tests-perform-replace-regexp-flag nil)
|
||||||
|
(expected " theo 1\n ^theorem$ 2\n ^theorem$ 3"))
|
||||||
|
(should
|
||||||
|
(replace-tests-with-undo
|
||||||
|
input "^theorem$"
|
||||||
|
"theo"
|
||||||
|
((?\s . (1 2 4)) (?U . (3)))
|
||||||
|
?q
|
||||||
|
(string= expected (buffer-string))))))
|
||||||
|
|
||||||
;;; replace-tests.el ends here
|
;;; replace-tests.el ends here
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue