Subject: Call `modify_text' only on the text being replaced in `subst-char-in-region'

From: Thuna <[email-masked]>
Date: Fri, 07 Feb 2025 00:41:12 +0100

Currently `subst-char-in-region' calls `modify_text' on the entire text,
starting from the first match to the end of the text, which fires off
`before-change-functions' for (almost) every character in the region
it's called in.

The specific place where this caused a problem for me is while filling
paragraphs with latex fragments in org-mode.  Latex fragments are
overlays with the `modification-hooks' property set to delete the
overlay, so when a paragraph is filled, that calls
  (subst-char-in-region ?\n ?\s ...)
on the entire paragraph which then calls `modify_text' on the entire
paragraph (sans the first line), which causes all latex fragments to
disappear.

I'm assuming that there isn't any particular reason why `modify_text'
should be called on the whole paragraph, but if there is, then that's ok
too, I suppose.  I don't see any reason why this should be breaking
stuff, but I haven't actually checked.

>From 51018ff7bd7e15d0d831a6acad704d78285c9903 Mon Sep 17 00:00:00 2001
From: Thuna <[email-masked]>
Date: Thu, 6 Feb 2025 23:31:48 +0100
Subject: [PATCH] Only modify the text being replaced in `subst-char-in-region'

* src/editfns.c (subst-char-in-region): Modify text from `pos' to
`pos + len' instead of from `pos' to `end'.
---
 src/editfns.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/editfns.c b/src/editfns.c
index 6b110b3d0e0..0f7e7334b91 100644
--- a/src/editfns.c
+++ b/src/editfns.c
@@ -2323,7 +2323,7 @@ #define COMBINING_BOTH (COMBINING_BEFORE | COMBINING_AFTER)
 	  else if (!changed)
 	    {
 	      changed = -1;
-	      modify_text (pos, XFIXNUM (end));
+	      modify_text (pos, pos + len);
 
 	      if (! NILP (noundo))
 		{
-- 
2.44.2