Subject: Re: bug#61091: 30.0.50; y-or-n-p clobbers match data

From: Eli Zaretskii <[email-masked]>
Date: Sat, 04 Feb 2023 10:16:54 +0200

> From: Richard Stallman <[email-masked]>
> Cc: [email-masked]
> Date: Sat, 04 Feb 2023 00:15:24 -0500
> 
>   > > Allowance should be made for the fact that `y-or-n-p'
>   > > was safe for the match data for over 35 years.
> 
>   > I don't object if it's easy to do so.
> 
> I think it should be easy.  How about trying it?

Done with the patch below.

diff --git a/lisp/subr.el b/lisp/subr.el
index 0f754fc..32c9974 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -3573,12 +3573,14 @@ y-or-n-p
 			    (if (or (zerop l) (eq ?\s (aref prompt (1- l))))
 				"" " ")
 			    (if dialog ""
-                              (substitute-command-keys
-                               (if help-form
-                                   (format "(\\`y', \\`n' or \\`%s') "
-                                           (key-description
-                                            (vector help-char)))
-                                 "(\\`y' or \\`n') ")))))))
+                              ;; Don't clobber caller's match data.
+                              (save-match-data
+                                (substitute-command-keys
+                                 (if help-form
+                                     (format "(\\`y', \\`n' or \\`%s') "
+                                             (key-description
+                                              (vector help-char)))
+                                   "(\\`y' or \\`n') "))))))))
         ;; Preserve the actual command that eventually called
         ;; `y-or-n-p' (otherwise `repeat' will be repeating
         ;; `exit-minibuffer').