Subject: Re: bug#77607: 31.0.50; replace-region-contents moves point

From: Stefan Monnier <[email-masked]>
Date: Mon, 07 Apr 2025 11:54:22 -0400

>>> (with-temp-buffer
>>>   (insert "abc")
>>>   (replace-region-contents (- (point) 3) (- (point) 2) (lambda () "A"))
>>>   (char-to-string (char-before)))
>>>
>>> now returns "A", but I believe that it previously returned "c".

I installed the patch below which should fix it.

        Stefan

diff --git a/src/editfns.c b/src/editfns.c
index 9590b49dc25..669d3cdb140 100644
--- a/src/editfns.c
+++ b/src/editfns.c
@@ -1968,6 +1968,7 @@ DEFUN ("replace-region-contents", Freplace_region_contents,
   if (FUNCTIONP (source))
     {
       specpdl_ref count = SPECPDL_INDEX ();
+      record_unwind_protect_excursion ();
       record_unwind_protect (save_restriction_restore,
 			     save_restriction_save ());
       Fnarrow_to_region (beg, end);
@@ -2119,7 +2120,6 @@ DEFUN ("replace-region-contents", Freplace_region_contents,
 
   Fundo_boundary ();
   bool modification_hooks_inhibited = false;
-  record_unwind_protect_excursion ();
 
   /* We are going to make a lot of small modifications, and having the
      modification hooks called for each of them will slow us down.