all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Tino Calancha <tino.calancha@gmail.com>
To: Juri Linkov <juri@linkov.net>
Cc: 37073@debbugs.gnu.org
Subject: bug#37073: 26.2.90; query-replace-regexp undo feature missing quote the regexps
Date: Mon, 19 Aug 2019 17:16:55 +0200	[thread overview]
Message-ID: <87d0h1kwwo.fsf@calancha-pc.dy.bbexcite.jp> (raw)
In-Reply-To: Juri Linkov's message of "Mon\, 19 Aug 2019 00\:06\:20 +0300 \(18 hours\, 3 minutes\, 15 seconds ago\)"


>Thanks.  Are there other looking-at/looking-back occurrences
>in perform-replace that are looking at non-regexp strings?
Yes, there is another one affecting the same feature
(see updated patch at the end).

>This looks good, but please ask Eli about pushing it to emacs-26 branch.
>Recently we fixed another problem exactly in the same place,
>but pushed the fix to the master.
I see.  Added Eli as CC.

--8<-----------------------------cut here---------------start------------->8---
commit 6e58c32caaf34f914b64a6dbc976bc1e62c12d90
Author: Tino Calancha <tino.calancha@gmail.com>
Date:   Mon Aug 19 17:06:02 2019 +0200

    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.

diff --git a/lisp/replace.el b/lisp/replace.el
index 08feb8eae7..0ddebb1270 100644
--- a/lisp/replace.el
+++ b/lisp/replace.el
@@ -2614,7 +2614,8 @@ perform-replace
                                    (setq real-match-data
                                          (save-excursion
                                            (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)))
                                          noedit
                                          (replace-match-maybe-edit
@@ -2624,7 +2625,9 @@ perform-replace
                                          real-match-data
                                          (save-excursion
                                            (goto-char (match-beginning 0))
-                                           (looking-at next-replacement)
+                                           (if regexp-flag
+                                               (looking-at next-replacement)
+                                             (looking-at (regexp-quote next-replacement)))
                                            (match-data t (nth 2 elt))))
                                    ;; Set replaced nil to keep in loop
                                    (when (eq def 'undo-all)
diff --git a/test/lisp/replace-tests.el b/test/lisp/replace-tests.el
index cd30633e37..cd08a522e3 100644
--- a/test/lisp/replace-tests.el
+++ b/test/lisp/replace-tests.el
@@ -365,6 +365,9 @@ replace-occur-test-create
 (defvar replace-tests-bind-read-string nil
   "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)
   "Helper to test `query-replace' undo feature.
 INPUT is a string to insert in a temporary buffer.
@@ -412,7 +415,7 @@ replace-tests-with-undo
                     (if replace-tests-bind-read-string
                         (lambda (&rest args) replace-tests-bind-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))))
 
 (defun replace-tests--query-replace-undo (&optional comma)
@@ -454,5 +457,26 @@ replace-tests--query-replace-undo
       input "a" "B" ((?\s . (1 2 3)) (?E . (4)) (?U . (5))) ?q
       (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

--8<-----------------------------cut here---------------end--------------->8---


In GNU Emacs 26.2.90 (build 29, x86_64-pc-linux-gnu, GTK+ Version 3.24.5)
 of 2019-08-19 built
Repository revision: 876a0c65c4f8a28dc2fbd4d2044321d5a8c48b1a






  parent reply	other threads:[~2019-08-19 15:16 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-18 14:04 bug#37073: 26.2.90; query-replace-regexp undo feature missing quote the regexps Tino Calancha
2019-08-18 21:06 ` Juri Linkov
2019-08-19 15:29   ` Eli Zaretskii
2019-08-19 15:16 ` Tino Calancha [this message]
2019-08-19 15:36 ` 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

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

  git send-email \
    --in-reply-to=87d0h1kwwo.fsf@calancha-pc.dy.bbexcite.jp \
    --to=tino.calancha@gmail.com \
    --cc=37073@debbugs.gnu.org \
    --cc=juri@linkov.net \
    /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 external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.