GNU bug report logs - #76108
Call `modify_text' only on the text being replaced in `subst-char-in-region'

Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.

Package: emacs; Reported by: Thuna <thuna.cing@HIDDEN>; dated Thu, 6 Feb 2025 23:42:01 UTC; Maintainer for emacs is bug-gnu-emacs@HIDDEN.

Message received at 76108 <at> debbugs.gnu.org:


Received: (at 76108) by debbugs.gnu.org; 12 Feb 2025 03:44:01 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Tue Feb 11 22:44:01 2025
Received: from localhost ([127.0.0.1]:60007 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1ti3fN-0005FP-1e
	for submit <at> debbugs.gnu.org; Tue, 11 Feb 2025 22:44:01 -0500
Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:20946)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.84_2) (envelope-from <monnier@HIDDEN>)
 id 1ti3fK-0005F8-Hs
 for 76108 <at> debbugs.gnu.org; Tue, 11 Feb 2025 22:43:59 -0500
Received: from pmg2.iro.umontreal.ca (localhost.localdomain [127.0.0.1])
 by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id A57F280860;
 Tue, 11 Feb 2025 22:43:51 -0500 (EST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca;
 s=mail; t=1739331826;
 bh=P93P1MCHxalTfy/r5m+eTxr6TI4sCZnjOCKpY34Q7mE=;
 h=From:To:Cc:Subject:In-Reply-To:References:Date:From;
 b=AiUAizUDUhSyk7P20XX8dlYKmumML76h9HoVgsigL24ffAIls/p8QHDIu7kEkbMF9
 8wDMlKdAYHvi5K8lOR9A7eD2coI4UaEjCmna3pj5MLoIJQ1MqlukLZHmhC9BAp4ph+
 aBohci+sl0pgoO2rjzY5uxt9lUEAvPf5XpOemLskMFS23ZkVuuaWMvfiF6zB/jiZqi
 fAotwXd30jnByb7qmhi5FSvAT/23tpLST0K0S+DJmlSUGAlKqiz5eG17qvrwPj/8RA
 yxyBo6NsvP7jORvfh9+BEDeJl3e9J0vo9/3rtPT4wmS4p5wE6KCizsEFLCrixz0VF2
 8eV4kPJwR5T4Q==
Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1])
 by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id 8F3718029E;
 Tue, 11 Feb 2025 22:43:46 -0500 (EST)
Received: from pastel (104-195-232-86.cpe.teksavvy.com [104.195.232.86])
 by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 649311204DD;
 Tue, 11 Feb 2025 22:43:46 -0500 (EST)
From: Stefan Monnier <monnier@HIDDEN>
To: Thuna <thuna.cing@HIDDEN>
Subject: Re: bug#76108: Call `modify_text' only on the text being replaced
 in `subst-char-in-region'
In-Reply-To: <87frkkathd.fsf@HIDDEN> (Thuna's message of "Wed, 12 Feb 2025
 01:37:18 +0100")
Message-ID: <jwvfrkj971t.fsf-monnier+emacs@HIDDEN>
References: <877c62iquf.fsf@HIDDEN> <jwvcyftj3ck.fsf-monnier+emacs@HIDDEN>
 <87y0yhhjef.fsf@HIDDEN> <jwv34gpiwcr.fsf-monnier+emacs@HIDDEN>
 <87seophdyd.fsf@HIDDEN> <jwvwme1h7mb.fsf-monnier+emacs@HIDDEN>
 <87msewgtwi.fsf@HIDDEN> <jwvv7tg81ku.fsf-monnier+emacs@HIDDEN>
 <87frkkathd.fsf@HIDDEN>
Date: Tue, 11 Feb 2025 22:43:45 -0500
User-Agent: Gnus/5.13 (Gnus v5.13)
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
X-SPAM-INFO: Spam detection results:  0
 ALL_TRUSTED                -1 Passed through trusted hosts only via SMTP
 AWL -0.043 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DKIM_SIGNED               0.1 Message has a DKIM or DK signature,
 not necessarily valid
 DKIM_VALID -0.1 Message has at least one valid DKIM or DK signature
 DKIM_VALID_AU -0.1 Message has a valid DKIM or DK signature from author's
 domain
 DKIM_VALID_EF -0.1 Message has a valid DKIM or DK signature from envelope-from
 domain
X-SPAM-LEVEL: 
X-Spam-Score: -2.3 (--)
X-Debbugs-Envelope-To: 76108
Cc: 76108 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -3.3 (---)

>> And I guess we could use the same approach to run the
>> `modification-hooks` from text properties also *after* the modification.
>
> I'm still not happy with the loose way in which these hooks are called.

Think of it this way: while `subst-char-in-region` can turn

   foo
   bar

into

   foo bar

by modifying a single character, the major mode needs to be able to
handle the situation where the user replaces `foo\nbar` with `foo bar`.
So while it's better for `subst-char-in-region` to use tighter bounds,
it should not be needed for correctness.

IOW, if your hook function misbehaves when the region is not tight
enough, it's (also) a problem in your hook function.

> I imagine that any modification hook that's useful will essentially be
> implementing a half-working version of what calling them strictly would
> achieve.  My biggest problem is that I don't have a reference for how
> significant the performance issues are, so the limitations end up
> feeling much more impactful.

Most hook functions like coarse regions.  E.g. the one used by font-lock
rounds up the region to whole lines; the one used by `syntax-ppss` only
cares about the BEG arg and considers everything after it as lost; the
one used by Eglot also likes them rather coarse to keep the description
of changes (sent to the LSP server) smaller.

The cost of running too many hook functions prompted the introduction of
`combine-after-change-calls` in Emacs-20, and `combine-change-calls` in
Emacs-27.

> At the very least, the documentation of precisely when these functions
> are called and what sort of guarantees they give should be updated,
> since it currently reads
>> =E2=80=98modification-hooks=E2=80=99
>>      This property's value is a list of functions to be called if any
>>      character within the overlay is changed or if text is inserted
>>      strictly within the overlay.
> in (info "(elisp) Overlay Properties").

Agreed.  I'd be in favor of changing the code also to avoid running the
after change when it turns out it's outside the bounds of the overlay.


        Stefan





Information forwarded to bug-gnu-emacs@HIDDEN:
bug#76108; Package emacs. Full text available.

Message received at 76108 <at> debbugs.gnu.org:


Received: (at 76108) by debbugs.gnu.org; 12 Feb 2025 00:37:30 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Tue Feb 11 19:37:30 2025
Received: from localhost ([127.0.0.1]:59643 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1ti0kr-0006lb-Ss
	for submit <at> debbugs.gnu.org; Tue, 11 Feb 2025 19:37:30 -0500
Received: from mail-ej1-x62e.google.com ([2a00:1450:4864:20::62e]:55622)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.84_2) (envelope-from <thuna.cing@HIDDEN>)
 id 1ti0kq-0006lJ-18
 for 76108 <at> debbugs.gnu.org; Tue, 11 Feb 2025 19:37:28 -0500
Received: by mail-ej1-x62e.google.com with SMTP id
 a640c23a62f3a-ab7b80326cdso527773966b.3
 for <76108 <at> debbugs.gnu.org>; Tue, 11 Feb 2025 16:37:27 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20230601; t=1739320641; x=1739925441; darn=debbugs.gnu.org;
 h=content-transfer-encoding:mime-version:message-id:date:references
 :in-reply-to:subject:cc:to:from:from:to:cc:subject:date:message-id
 :reply-to; bh=4vBoWMZ/XxXkCqIen0sCUmLdvJXSn5eCP6Ja2oPURNA=;
 b=aBDOq9G+LnmkXu2bwTkIcN42pjBaIvFk5loHNfZTsNpfXxYtncHkVXewO4etacxMEg
 K6BKBl9BHvHG/Kh7wOjjdmMtfuFZ6oi+Okq4FMisAfT4JkMqqpxXVdfjdbEv2aw2jeAA
 WYhsgNgL/2TmQQIU1werXEChxS2Cqm6jpm9DEXxHs9sQWuwLcy3i9+RAM5DQ7bgCJXx8
 9+DJ4g2zbTCXIJJlLJExmAIyid039cVX/Ws6aFycCeJIPzW7YiBVqhW283SUm/zbYioh
 dlfPuiTB10C6j95OmdDeWLcf8fQ6FxKdUjxaKfJPsyguJq1UAneAfHjTyKWkY+WAz0Yo
 LzaQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1739320641; x=1739925441;
 h=content-transfer-encoding:mime-version:message-id:date:references
 :in-reply-to:subject:cc:to:from:x-gm-message-state:from:to:cc
 :subject:date:message-id:reply-to;
 bh=4vBoWMZ/XxXkCqIen0sCUmLdvJXSn5eCP6Ja2oPURNA=;
 b=KA0lS29OmCLUWsG0dpzZ86dHCW36yaaExUPRz/bpfQlj3GrMJjmtKY9WkUGyifnNXh
 Yhf5wlZ6qYZQ90GExwMEyQaznnCxvWCuNP58Cmcf3ek5/jGIW4TdbDOXTFTf/4kIg+q4
 iCjtgVMv/zxm7wOJza7dDRw/s2xW/O+p4i3TN9VRczDKCkNNEk8jnFOe27Vn0j/BDz2j
 q8Ll46rmJkOu2ci4r3/Iz/kEw51dApvdhcBIk9mjrVbhpP1ljXEPb00ibVMR3GDcqXHC
 GU5NUeXtMiIcV7B3sFvMjULT5rrzwmBgRAWKtPhT83ddCAESf0uUN4PNMOxKJIEvIi/I
 I8Ww==
X-Gm-Message-State: AOJu0YxM7VEBSTi2jh2ODccYFe2cUwJMd7zjby587m+Tz/XYdoxBQIDi
 xsInffEXazbdLQOkHkvux/KtffHdcHbVjVPdTBK7cIKYg5ieCwPGDYHD45dfOz4=
X-Gm-Gg: ASbGncvrPfUC0YvG9mmqBbibWH4fZMEZSHLcIwZ5+4Yc0CnXeGcqsLF1P9iWJQKJG2W
 8ax/8IopkZhSe4CpG9Zp/21aNr9lJvk1oY/N/gbYhzdUAIf07OLewsHkhOfDfN14EOd5Cxs1bot
 isPyhw+hY6izcBGy+rLgKWLTzBGqWVTSDoguQKGC90QndH2AVNa5R3yH7JYnf6diRItCQKq2YMC
 PT9fFcxmHRctpNTbejaW7Yp9GgUSzw33ax/zRc0KD97HkQRHr7nKbKBh0sp91dgC6d3teDasbY1
 timperfimYUFAu0R
X-Google-Smtp-Source: AGHT+IE9Izys9I3qK+QN/bPAdVk8kmRQpBIf0Uh8OKZ9fsKfSCC1WlcXJYuPb3sqhg9Y4h/HechLqg==
X-Received: by 2002:a17:906:c149:b0:ab6:d6c3:f1e2 with SMTP id
 a640c23a62f3a-ab7f34a7863mr70104766b.38.1739320640906; 
 Tue, 11 Feb 2025 16:37:20 -0800 (PST)
Received: from thuna-lis3 ([178.249.211.103]) by smtp.gmail.com with ESMTPSA id
 a640c23a62f3a-ab7b99be322sm570419266b.8.2025.02.11.16.37.19
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Tue, 11 Feb 2025 16:37:20 -0800 (PST)
From: Thuna <thuna.cing@HIDDEN>
To: Stefan Monnier <monnier@HIDDEN>
Subject: Re: bug#76108: Call `modify_text' only on the text being replaced
 in `subst-char-in-region'
In-Reply-To: <jwvv7tg81ku.fsf-monnier+emacs@HIDDEN>
References: <877c62iquf.fsf@HIDDEN>
 <jwvcyftj3ck.fsf-monnier+emacs@HIDDEN> <87y0yhhjef.fsf@HIDDEN>
 <jwv34gpiwcr.fsf-monnier+emacs@HIDDEN> <87seophdyd.fsf@HIDDEN>
 <jwvwme1h7mb.fsf-monnier+emacs@HIDDEN> <87msewgtwi.fsf@HIDDEN>
 <jwvv7tg81ku.fsf-monnier+emacs@HIDDEN>
Date: Wed, 12 Feb 2025 01:37:18 +0100
Message-ID: <87frkkathd.fsf@HIDDEN>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
X-Spam-Score: 0.0 (/)
X-Debbugs-Envelope-To: 76108
Cc: 76108 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.0 (-)

>> I thought it might have been because `subst-char-in-region' calls
>> `signal_after_change' from the first to the last change (and that is
>> likely not unrelated) but even if that were the case the first and last
>> change in the above situation is the sole newline at the middle, so it
>> shouldn't have reached the overlay regardless.  Maybe
>> `report_overlay_modification' calls everything called in the before call
>> as well, but I honestly can't tell.
>
> It turns out that, indeed, the C code makes extra efforts to call the
> same `modification-hooks` functions from overlays before and after
> the change, hence the weird behavior (from `editfns.c`):
>
>     /* Lisp vector holding overlay hook functions to call.
>        Vector elements come in pairs.
>        Each even-index element is a list of hook functions.
>        The following odd-index element is the overlay they came from.
>=20=20=20=20=20
>        Before the buffer change, we fill in this vector
>        as we call overlay hook functions.
>        After the buffer change, we get the functions to call from this ve=
ctor.
>        This way we always call the same functions before and after the ch=
ange.  */
>     static Lisp_Object last_overlay_modification_hooks;
>=20=20=20=20=20
>     /* Number of elements actually used in last_overlay_modification_hook=
s.  */
>     static ptrdiff_t last_overlay_modification_hooks_used;
>
> so, while it looks like a bug to me, it's very much done on purpose.
> Not sure whether we should fix it or document it.
>
> And I guess we could use the same approach to run the
> `modification-hooks` from text properties also *after* the modification.

I'm still not happy with the loose way in which these hooks are called.
I imagine that any modification hook that's useful will essentially be
implementing a half-working version of what calling them strictly would
achieve.  My biggest problem is that I don't have a reference for how
significant the performance issues are, so the limitations end up
feeling much more impactful.

At the very least, the documentation of precisely when these functions
are called and what sort of guarantees they give should be updated,
since it currently reads
> =E2=80=98modification-hooks=E2=80=99
>      This property's value is a list of functions to be called if any
>      character within the overlay is changed or if text is inserted
>      strictly within the overlay.
in (info "(elisp) Overlay Properties").




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#76108; Package emacs. Full text available.

Message received at 76108 <at> debbugs.gnu.org:


Received: (at 76108) by debbugs.gnu.org; 12 Feb 2025 00:18:02 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Tue Feb 11 19:18:01 2025
Received: from localhost ([127.0.0.1]:59621 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1ti0S1-0005pE-6s
	for submit <at> debbugs.gnu.org; Tue, 11 Feb 2025 19:18:01 -0500
Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:11114)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.84_2) (envelope-from <monnier@HIDDEN>)
 id 1ti0Rx-0005ov-9u
 for 76108 <at> debbugs.gnu.org; Tue, 11 Feb 2025 19:17:58 -0500
Received: from pmg1.iro.umontreal.ca (localhost.localdomain [127.0.0.1])
 by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id 80B99100034;
 Tue, 11 Feb 2025 19:17:50 -0500 (EST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca;
 s=mail; t=1739319465;
 bh=myZjQIsw3ue1/rzCVjwnH3/U7lffw1oIfVdH6hq0VZY=;
 h=From:To:Cc:Subject:In-Reply-To:References:Date:From;
 b=owq9m1gStGvHmINR/5Q51hcPj8mrZBZgffhromFvIom4gQpTzOAZpJXAp1FmGbCqJ
 XLfYJRlDp0HmoA0loR0gIZfWJKu2MLg7j0SSQjj79dA6kgjl3RvJ3eGKl1UQCyDeQq
 C0F5MsdH8IAHX/9YsaDJjN4kL5dWlotJxkT+MvgGU6Xt15XNnd7/0eKSiiSz5x7+SI
 AR8QUb+I9Jx/UUu0qYW75nHyr2KhfpjQV3NvrwKqoWusTSwm7xWGJIuGQ7xahSaK2I
 iNx0e4aYwGzyRRg3pcfXGl4DBtOwQUvqzedu0iAV4NQVipHdAKl/icjk1R7T0ycGfi
 HXfqq0v3vw+ZA==
Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1])
 by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id 87992100035;
 Tue, 11 Feb 2025 19:17:45 -0500 (EST)
Received: from pastel (104-195-232-86.cpe.teksavvy.com [104.195.232.86])
 by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 5FC2C1204EE;
 Tue, 11 Feb 2025 19:17:45 -0500 (EST)
From: Stefan Monnier <monnier@HIDDEN>
To: Thuna <thuna.cing@HIDDEN>
Subject: Re: bug#76108: Call `modify_text' only on the text being replaced
 in `subst-char-in-region'
In-Reply-To: <87msewgtwi.fsf@HIDDEN> (Thuna's message of "Sat, 08 Feb 2025
 19:42:37 +0100")
Message-ID: <jwvv7tg81ku.fsf-monnier+emacs@HIDDEN>
References: <877c62iquf.fsf@HIDDEN> <jwvcyftj3ck.fsf-monnier+emacs@HIDDEN>
 <87y0yhhjef.fsf@HIDDEN> <jwv34gpiwcr.fsf-monnier+emacs@HIDDEN>
 <87seophdyd.fsf@HIDDEN> <jwvwme1h7mb.fsf-monnier+emacs@HIDDEN>
 <87msewgtwi.fsf@HIDDEN>
Date: Tue, 11 Feb 2025 19:17:44 -0500
User-Agent: Gnus/5.13 (Gnus v5.13)
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
X-SPAM-INFO: Spam detection results:  0
 ALL_TRUSTED                -1 Passed through trusted hosts only via SMTP
 AWL -0.103 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DKIM_SIGNED               0.1 Message has a DKIM or DK signature,
 not necessarily valid
 DKIM_VALID -0.1 Message has at least one valid DKIM or DK signature
 DKIM_VALID_AU -0.1 Message has a valid DKIM or DK signature from author's
 domain
 DKIM_VALID_EF -0.1 Message has a valid DKIM or DK signature from envelope-from
 domain
X-SPAM-LEVEL: 
X-Spam-Score: -2.3 (--)
X-Debbugs-Envelope-To: 76108
Cc: 76108 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -3.3 (---)

>> The `nil 8 21` and `t 8 9 1` are the values I'd expect, but the `17 to
>> 17` seems very odd and the fact that the hook is called at all for the
>> "after" case sounds like a bug, as you point out.
>
> It doesn't change anything but I should make a correction: That first
> case is also an overlay from 14 to 17.

Ah, well it does change things in the sense that it was quite weird.
A copy/paste error is a nice explanation and I can stop worrying
about it. =F0=9F=99=82

> I thought it might have been because `subst-char-in-region' calls
> `signal_after_change' from the first to the last change (and that is
> likely not unrelated) but even if that were the case the first and last
> change in the above situation is the sole newline at the middle, so it
> shouldn't have reached the overlay regardless.  Maybe
> `report_overlay_modification' calls everything called in the before call
> as well, but I honestly can't tell.

It turns out that, indeed, the C code makes extra efforts to call the
same `modification-hooks` functions from overlays before and after
the change, hence the weird behavior (from `editfns.c`):

    /* Lisp vector holding overlay hook functions to call.
       Vector elements come in pairs.
       Each even-index element is a list of hook functions.
       The following odd-index element is the overlay they came from.
=20=20=20=20
       Before the buffer change, we fill in this vector
       as we call overlay hook functions.
       After the buffer change, we get the functions to call from this vect=
or.
       This way we always call the same functions before and after the chan=
ge.  */
    static Lisp_Object last_overlay_modification_hooks;
=20=20=20=20
    /* Number of elements actually used in last_overlay_modification_hooks.=
  */
    static ptrdiff_t last_overlay_modification_hooks_used;

so, while it looks like a bug to me, it's very much done on purpose.
Not sure whether we should fix it or document it.

And I guess we could use the same approach to run the
`modification-hooks` from text properties also *after* the modification.


        Stefan





Information forwarded to bug-gnu-emacs@HIDDEN:
bug#76108; Package emacs. Full text available.

Message received at 76108 <at> debbugs.gnu.org:


Received: (at 76108) by debbugs.gnu.org; 8 Feb 2025 18:42:49 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Sat Feb 08 13:42:49 2025
Received: from localhost ([127.0.0.1]:41438 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tgpmz-0003Vl-Cq
	for submit <at> debbugs.gnu.org; Sat, 08 Feb 2025 13:42:49 -0500
Received: from mail-ej1-x62e.google.com ([2a00:1450:4864:20::62e]:55782)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.84_2) (envelope-from <thuna.cing@HIDDEN>)
 id 1tgpmw-0003VS-JM
 for 76108 <at> debbugs.gnu.org; Sat, 08 Feb 2025 13:42:47 -0500
Received: by mail-ej1-x62e.google.com with SMTP id
 a640c23a62f3a-ab78a7db35eso272803966b.3
 for <76108 <at> debbugs.gnu.org>; Sat, 08 Feb 2025 10:42:46 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20230601; t=1739040159; x=1739644959; darn=debbugs.gnu.org;
 h=mime-version:message-id:date:references:in-reply-to:subject:cc:to
 :from:from:to:cc:subject:date:message-id:reply-to;
 bh=HosNhHn5OdaQXIycWflUIucI6Pg9LoVkYRwSrZXpc8Q=;
 b=VyNl/2cvMvvvWbXM9b2ZUAd5hW0Md5buptU0mwYg5XBLfiOadRBTiGlDa8GPWRpfnn
 kooXX9zbB7/4zlAlyL7Ltm00rP3lm8XQbNUtltH51D4c7OzZevrzzMraHHMKkSmt/cGl
 Lnbu5CtU4lnDfYgU3YKSBfEqMpJgbJZliEMUdfeVDWys1oZMtTkb2QRUdx87s+3dh78Y
 pbIQ0zoovTG2vNBoOFHeqGf73W/OmgVe++WTH1Mzlp+mPnhLNtKhLA+dR99GOTbWVlIq
 FA2E0pBght3/EzJ+yVoPmNOvI/Ph8pVeMo9M/t/9dHjXcyP+I7o/5Q5FDHL8TsRcoVCr
 nWFA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1739040159; x=1739644959;
 h=mime-version:message-id:date:references:in-reply-to:subject:cc:to
 :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
 bh=HosNhHn5OdaQXIycWflUIucI6Pg9LoVkYRwSrZXpc8Q=;
 b=HgPrsB1GCni2EbP2i6Zuz9QkUxMjElAPvLw8nRmns195+RgIA99doMzi/Q565JD3KM
 wKzCwm9QEQVkbuxGLsDcNTPzp04u2G/FUedEvnFliMKo514PjeUmrpTf+yFsqXMX9kEE
 cYTtsFMRFzXL6McdXBUyO+BJxOR/tdbi5eLAxDoC4CnekJBR1cBx0ppyGwOGDlyguA6/
 0ohZN+S6jepKlTnjiypOEWh7WLDNNsfASdfYngEs8D2EmUAJco5ivMVIyZI6tjLDgksH
 6AmfjytgI7ahwR116tuWTFIgl1Jrfc6Xw8GEbrXguG5AEot1DHB19fJAbwCk/IXghhFE
 55qA==
X-Gm-Message-State: AOJu0YxR/S3Ww1no4jbCh0OddMIFq2mkwH8dyN18evT9OYgHB2YU+b7t
 fOkg08FmuvhkIOqHqmeOchZYd1AIRaNjvjqBxkgjUcUd9DJhpmqPkMnSJGrYFWyrUA==
X-Gm-Gg: ASbGnctI5vX60WOuOvA8JcWAPGpFLDPFZu6sQrADpC/hbeYSkMDyQWX+bSw7k6Q+6yZ
 GnhRa3qYDGbhg514iYW2H4kNPf5flGkBApoDn74QEiFHiEE2qsfSIu0H71QZQBbptw4yszZQhlC
 Yprj+BQ1x4mACy/t8FBJuZ6ChQ68I+ipr+Doty0Eix9igBDi02/SW65DoA2wW6dszqITkFrdMI2
 lQqQ3vQlzfh2ytyriHPF55blWPuem3z8Enbe9uoh46K9/g03mYtAOq4dDiDys36l9vFN1/ADqDt
 QOJhm/H/fGC0Rc/d
X-Google-Smtp-Source: AGHT+IGHA9Se9F0sNga3KfkrC8zXQdXo+yMU1IoYTAuSRprDJCqofJ4/CPOh7fh8YSrw+dLehBaQCA==
X-Received: by 2002:a17:907:7e9f:b0:aa6:9503:aa73 with SMTP id
 a640c23a62f3a-ab789c756demr883846966b.51.1739040159342; 
 Sat, 08 Feb 2025 10:42:39 -0800 (PST)
Received: from thuna-lis3 ([178.249.211.103]) by smtp.gmail.com with ESMTPSA id
 a640c23a62f3a-ab7733393f2sm519592966b.150.2025.02.08.10.42.38
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Sat, 08 Feb 2025 10:42:38 -0800 (PST)
From: Thuna <thuna.cing@HIDDEN>
To: Stefan Monnier <monnier@HIDDEN>
Subject: Re: bug#76108: Call `modify_text' only on the text being replaced
 in `subst-char-in-region'
In-Reply-To: <jwvwme1h7mb.fsf-monnier+emacs@HIDDEN>
References: <877c62iquf.fsf@HIDDEN>
 <jwvcyftj3ck.fsf-monnier+emacs@HIDDEN> <87y0yhhjef.fsf@HIDDEN>
 <jwv34gpiwcr.fsf-monnier+emacs@HIDDEN> <87seophdyd.fsf@HIDDEN>
 <jwvwme1h7mb.fsf-monnier+emacs@HIDDEN>
Date: Sat, 08 Feb 2025 19:42:37 +0100
Message-ID: <87msewgtwi.fsf@HIDDEN>
MIME-Version: 1.0
Content-Type: text/plain
X-Spam-Score: 0.0 (/)
X-Debbugs-Envelope-To: 76108
Cc: 76108 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.0 (-)

>> Now, I am not entirely sure how I've managed to miss this, but
>> `subst-char-in-region' actually _also_ messes up the after calls.  I
>> have no idea how or why this is happening, but a buffer whose contents
>> are
>>
>>   "foo bar\nquux baz ban"
>>
>> with an overlay from 14 to 17 (that is, around the word "baz") with the
>> modification hook
>>
>>   (lambda (&rest args) (print args))
>>
>> reports being called with the arguments
>>
>>   (#<overlay from 17 to 17 in temp> nil 8 21)
>>   (#<overlay from 14 to 17 in temp> t 8 9 1)
>>
>> when I do
>>
>>   (subst-char-in-region (point-min) (point-max) ?\n ?\s)
>
> The `nil 8 21` and `t 8 9 1` are the values I'd expect, but the `17 to
> 17` seems very odd and the fact that the hook is called at all for the
> "after" case sounds like a bug, as you point out.

It doesn't change anything but I should make a correction: That first
case is also an overlay from 14 to 17.




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#76108; Package emacs. Full text available.

Message received at 76108 <at> debbugs.gnu.org:


Received: (at 76108) by debbugs.gnu.org; 7 Feb 2025 19:44:29 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Fri Feb 07 14:44:29 2025
Received: from localhost ([127.0.0.1]:36607 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tgUH6-00018n-Gc
	for submit <at> debbugs.gnu.org; Fri, 07 Feb 2025 14:44:28 -0500
Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:37986)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.84_2) (envelope-from <monnier@HIDDEN>)
 id 1tgUH3-00018V-PL
 for 76108 <at> debbugs.gnu.org; Fri, 07 Feb 2025 14:44:26 -0500
Received: from pmg1.iro.umontreal.ca (localhost.localdomain [127.0.0.1])
 by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id 328D010004C;
 Fri,  7 Feb 2025 14:44:19 -0500 (EST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca;
 s=mail; t=1738957458;
 bh=4d2gCrevFVchGLQxTAE7gggKzcUccHS+k8t7s1IIi8E=;
 h=From:To:Cc:Subject:In-Reply-To:References:Date:From;
 b=kGBqJ/QthJa20p0mgdbQSVU4HZWJ8GIJaJA616QVAvboMWdwcn76n0o6TLCWkypas
 xh1w6hzqdMLxLUsngXIvqaa9JjxIfoSvWBv2IE6tN+ZOxsbvhPcYLEdSLLJ0bk3/ER
 MuTH8aI2wivaTjZDADzHVTtv5wpPTDzN3qonNyFdtkff9sbzFmXrqU3irV+Prq+6m6
 oES/f/Buq98zI1cTNn6r4YI0pTIDwjnnMkKPhrS7S5SSA+vyGsmGSBLXfgWvBLKY/L
 V/FSsYdUwwNaAldwTz15x8cKhphRzQWowsr3levxFg/sHfewsDhREwwNNQzJZvZrzA
 FYAoXlKm9Z0+w==
Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1])
 by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id 7BE4C100034;
 Fri,  7 Feb 2025 14:44:18 -0500 (EST)
Received: from pastel (104-195-232-86.cpe.teksavvy.com [104.195.232.86])
 by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 51275120418;
 Fri,  7 Feb 2025 14:44:18 -0500 (EST)
From: Stefan Monnier <monnier@HIDDEN>
To: Thuna <thuna.cing@HIDDEN>
Subject: Re: bug#76108: Call `modify_text' only on the text being replaced
 in `subst-char-in-region'
In-Reply-To: <87seophdyd.fsf@HIDDEN> (Thuna's message of "Fri, 07 Feb 2025
 18:17:14 +0100")
Message-ID: <jwvwme1h7mb.fsf-monnier+emacs@HIDDEN>
References: <877c62iquf.fsf@HIDDEN> <jwvcyftj3ck.fsf-monnier+emacs@HIDDEN>
 <87y0yhhjef.fsf@HIDDEN> <jwv34gpiwcr.fsf-monnier+emacs@HIDDEN>
 <87seophdyd.fsf@HIDDEN>
Date: Fri, 07 Feb 2025 14:44:16 -0500
User-Agent: Gnus/5.13 (Gnus v5.13)
MIME-Version: 1.0
Content-Type: text/plain
X-SPAM-INFO: Spam detection results:  0
 ALL_TRUSTED                -1 Passed through trusted hosts only via SMTP
 AWL -0.063 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DKIM_SIGNED               0.1 Message has a DKIM or DK signature,
 not necessarily valid
 DKIM_VALID -0.1 Message has at least one valid DKIM or DK signature
 DKIM_VALID_AU -0.1 Message has a valid DKIM or DK signature from author's
 domain
 DKIM_VALID_EF -0.1 Message has a valid DKIM or DK signature from envelope-from
 domain
X-SPAM-LEVEL: 
X-Spam-Score: -2.3 (--)
X-Debbugs-Envelope-To: 76108
Cc: 76108 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -3.3 (---)

>> You have to understand the distinction between the "before" and the
>> "after" calls (which correspond to the `before-change-functions` and the
>> `after-change-functions` hooks): the "before" call is there to announce
>> that some change*s* *may* happen in the specified region.
> It still reads to me as though it's called before some changes _will_
> happen - that is, when changing text, you call before-change-functions
> (and co.) on the text, change it, and call after-change-functions (and
> co.) afterwards - but I understand that it's up to interpretation and
> mine just happens to not match with the implementation.

Your interpretation is right for the simple case.  But the API is
designed to accommodate other cases, and `subst-char-in-region` takes
advantage of that.

Of course, calling the `before-change-functions` without calling
`after-change-functions` is something we strive to avoid, and similarly
we try to make the region passed to `before-change-functions` as small
as possible, but both of those are done for performance/optimization
reasons, they are not needed for correctness.

> Now, I am not entirely sure how I've managed to miss this, but
> `subst-char-in-region' actually _also_ messes up the after calls.  I
> have no idea how or why this is happening, but a buffer whose contents
> are
>
>   "foo bar\nquux baz ban"
>
> with an overlay from 14 to 17 (that is, around the word "baz") with the
> modification hook
>
>   (lambda (&rest args) (print args))
>
> reports being called with the arguments
>
>   (#<overlay from 17 to 17 in temp> nil 8 21)
>   (#<overlay from 14 to 17 in temp> t 8 9 1)
>
> when I do
>
>   (subst-char-in-region (point-min) (point-max) ?\n ?\s)

The `nil 8 21` and `t 8 9 1` are the values I'd expect, but the `17 to
17` seems very odd and the fact that the hook is called at all for the
"after" case sounds like a bug, as you point out.

> Maybe `report_overlay_modification' calls everything called in the
> before call as well, but I honestly can't tell.

That would be my guess, indeed.
[ Haven't looked at the code yet.  ]


        Stefan





Information forwarded to bug-gnu-emacs@HIDDEN:
bug#76108; Package emacs. Full text available.

Message received at 76108 <at> debbugs.gnu.org:


Received: (at 76108) by debbugs.gnu.org; 7 Feb 2025 17:17:27 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Fri Feb 07 12:17:27 2025
Received: from localhost ([127.0.0.1]:36264 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tgRyo-0002WZ-KT
	for submit <at> debbugs.gnu.org; Fri, 07 Feb 2025 12:17:26 -0500
Received: from mail-ej1-x629.google.com ([2a00:1450:4864:20::629]:54513)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.84_2) (envelope-from <thuna.cing@HIDDEN>)
 id 1tgRym-0002WH-Oi
 for 76108 <at> debbugs.gnu.org; Fri, 07 Feb 2025 12:17:25 -0500
Received: by mail-ej1-x629.google.com with SMTP id
 a640c23a62f3a-ab737e78900so410989366b.0
 for <76108 <at> debbugs.gnu.org>; Fri, 07 Feb 2025 09:17:24 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20230601; t=1738948638; x=1739553438; darn=debbugs.gnu.org;
 h=mime-version:message-id:date:references:in-reply-to:subject:cc:to
 :from:from:to:cc:subject:date:message-id:reply-to;
 bh=k5R8B6451dt0kvW/2En1l60dz3JNQZI5v6T70dS/BYc=;
 b=D1vkADxy4O0pvr4Gb4P4uXVxpT4UG6LgtjOqROOfnU9ROWIPPqYdK3nqjqqBzu4Zq4
 3vcsqLYVbPJVbZhYN7qrhXFqh8QoeLVjA/x36aKF/tZhfRLJgfSWCfh73MamgjXOdv95
 0RzkJsCOCraIM4xfrfgHogGlJe6nUqVXxVqu9Gb91ojCLXqAfbg8nTo3f/riJ16D5xLU
 y9KsRo6W5dWXc0RKp6rdKc7HFteBCj9lcC6x+uf2UtgjKOaGUe0Jqg9as2X5SWL+dY4p
 HmJa/8vL5olsCFea+UENkuGCQ4Jjlo8aqJHDCXmrR/xu24a25rnROLJY9drgwu1hDbRm
 1Vdg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1738948638; x=1739553438;
 h=mime-version:message-id:date:references:in-reply-to:subject:cc:to
 :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
 bh=k5R8B6451dt0kvW/2En1l60dz3JNQZI5v6T70dS/BYc=;
 b=KzFRBtcFAjtP6n2VBeHwyWK7AVEUYt99YlJPnwutT/x80BkSH01a2c6qLefPQIjR3T
 +0lEAwlP4mL/ZZld66j5Mjuue5r+TAfEZBKy9eipSlmIqo3ELcmZS1/4aVQFYqVsxkSr
 bYJuls/RweuYYjkp9QBSGj6Po7b55aB1FDThqocBRdvWk5McjnxrEnvQmeQTdX8afhCZ
 86QpAQBn5SDdCsR3X6vKo7ATCsyHRWxbmWoYtqjPtoNKzEYwXj0TH/P8yWf3LbSXoH6f
 ebYOFyEcOPJqZUDUJ+CIOh1PEZ5TaUb7TPQ9CIUEHevtUtMP+NDt8vUr0ehFpK616e1u
 miOA==
X-Gm-Message-State: AOJu0YwM8M+27atvocHjXlwgenySx+Va7cZVOM+52sezJ40A6fW/i11f
 qEvDcX7I0QekW/B1Jpzq2od0o2ltw+L5d209sCs9AYCAgskAS0s8MKLys3hEnXU=
X-Gm-Gg: ASbGnctnuBLb1wNaeF9ietZIfVzLVsr/3nfXXhoaPEKhqLbR4PxanRL3AlkkvmvBQg5
 2clkSi8gLhhYDghijPV1AI0xPAouRXhyUcyVl1SM0t7YT739CGEwi4pwrlMoluEUKSGjveLniwx
 7Vy5/snrR6kyMC7vUuCJUIdnQaHzsC1lp/HuvmDYxD31/Bqxx6bhDm/POIg4uuKBztbOWWVzE1k
 +rQ+Fc42GJiwd4X3fe4Ljzu+F53pX6rGv6BP0plRPvDKecisyB0YvgSJ/pvXRYAGydMqjGwk0L/
 yx62VktNz4IQ4FwE
X-Google-Smtp-Source: AGHT+IHf/dOwnC5I3cZncZ+5ouyByxHq2SVffx4MocrhyhOy+iaRvbcTJRrLqzO3naY3sFbOvdjpfg==
X-Received: by 2002:a17:907:6eac:b0:ab7:3e27:ff01 with SMTP id
 a640c23a62f3a-ab789b24372mr458680966b.29.1738948635902; 
 Fri, 07 Feb 2025 09:17:15 -0800 (PST)
Received: from thuna-lis3 ([178.249.211.103]) by smtp.gmail.com with ESMTPSA id
 a640c23a62f3a-ab772f49046sm298968766b.25.2025.02.07.09.17.15
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Fri, 07 Feb 2025 09:17:15 -0800 (PST)
From: Thuna <thuna.cing@HIDDEN>
To: Stefan Monnier <monnier@HIDDEN>
Subject: Re: bug#76108: Call `modify_text' only on the text being replaced
 in `subst-char-in-region'
In-Reply-To: <jwv34gpiwcr.fsf-monnier+emacs@HIDDEN>
References: <877c62iquf.fsf@HIDDEN>
 <jwvcyftj3ck.fsf-monnier+emacs@HIDDEN> <87y0yhhjef.fsf@HIDDEN>
 <jwv34gpiwcr.fsf-monnier+emacs@HIDDEN>
Date: Fri, 07 Feb 2025 18:17:14 +0100
Message-ID: <87seophdyd.fsf@HIDDEN>
MIME-Version: 1.0
Content-Type: text/plain
X-Spam-Score: 0.0 (/)
X-Debbugs-Envelope-To: 76108
Cc: 76108 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.0 (-)

>> Personally I find it quite unintuitive to have `modification-hooks' be
>> "Hooks that are called when some characters in region _might_ be
>> modified".
>
> You have to understand the distinction between the "before" and the
> "after" calls (which correspond to the `before-change-functions` and the
> `after-change-functions` hooks): the "before" call is there to announce
> that some change*s* *may* happen in the specified region.

It still reads to me as though it's called before some changes _will_
happen - that is, when changing text, you call before-change-functions
(and co.) on the text, change it, and call after-change-functions (and
co.) afterwards - but I understand that it's up to interpretation and
mine just happens to not match with the implementation.

> In the vast majority of cases you should care only about the "after" call.
> [ Sadly, for the `modification-hooks` placed on text properties (as
>   opposed to overlays), only the "before" call is available.  ]

Now, I am not entirely sure how I've managed to miss this, but
`subst-char-in-region' actually _also_ messes up the after calls.  I
have no idea how or why this is happening, but a buffer whose contents
are

  "foo bar\nquux baz ban"

with an overlay from 14 to 17 (that is, around the word "baz") with the
modification hook

  (lambda (&rest args) (print args))

reports being called with the arguments

  (#<overlay from 17 to 17 in temp> nil 8 21)
  (#<overlay from 14 to 17 in temp> t 8 9 1)

when I do

  (subst-char-in-region (point-min) (point-max) ?\n ?\s)

which means that it's being called even though it doesn't overlap at all
with the range of characters reported as being modified.

I thought it might have been because `subst-char-in-region' calls
`signal_after_change' from the first to the last change (and that is
likely not unrelated) but even if that were the case the first and last
change in the above situation is the sole newline at the middle, so it
shouldn't have reached the overlay regardless.  Maybe
`report_overlay_modification' calls everything called in the before call
as well, but I honestly can't tell.




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#76108; Package emacs. Full text available.

Message received at 76108 <at> debbugs.gnu.org:


Received: (at 76108) by debbugs.gnu.org; 7 Feb 2025 16:06:40 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Fri Feb 07 11:06:40 2025
Received: from localhost ([127.0.0.1]:36036 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tgQsJ-0007MX-Tz
	for submit <at> debbugs.gnu.org; Fri, 07 Feb 2025 11:06:40 -0500
Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:61159)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.84_2) (envelope-from <monnier@HIDDEN>)
 id 1tgQsG-0007MG-C1
 for 76108 <at> debbugs.gnu.org; Fri, 07 Feb 2025 11:06:38 -0500
Received: from pmg1.iro.umontreal.ca (localhost.localdomain [127.0.0.1])
 by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id 5527B10004C;
 Fri,  7 Feb 2025 11:06:28 -0500 (EST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca;
 s=mail; t=1738944387;
 bh=vbbdU5TCfDCAYOsha6orQTzSEVhktF1oLO0NgAoA2M4=;
 h=From:To:Cc:Subject:In-Reply-To:References:Date:From;
 b=gxZVnO+/XpQF7QHTyMJ0vKLS8Bnhq3ZR3CQekVRAqPur9DHkQzjHB/v+TIqoSFbAp
 /vjDiTkVPKeal82BfrzJ+dReM24faYX63P8+Sy/Y4lFxojGw/vWJvPImYAANWFgn2l
 WUkpR2ZU7HXI53AFks1sV6/qQqKugu6fVwMZxZJf3y8bnUBKmX494+EHLqTxptfv2W
 w2FrazyJ8lEoexD2x2jp/a1rhnELZfW5AVufJMKAK/Lcjv+/q7a/3xp0ThyuAezAX+
 g81zsjwCERLm+QbbwPq/8hBh7a+8ok+cIxciLJ8jMMG+X2Cwko4Qdn96cYllNeL+KU
 ptJbKL3aOnERg==
Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1])
 by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id 49455100034;
 Fri,  7 Feb 2025 11:06:27 -0500 (EST)
Received: from pastel (104-195-232-86.cpe.teksavvy.com [104.195.232.86])
 by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 14AEC1201E0;
 Fri,  7 Feb 2025 11:06:27 -0500 (EST)
From: Stefan Monnier <monnier@HIDDEN>
To: Thuna <thuna.cing@HIDDEN>
Subject: Re: bug#76108: Call `modify_text' only on the text being replaced
 in `subst-char-in-region'
In-Reply-To: <87y0yhhjef.fsf@HIDDEN> (Thuna's message of "Fri, 07 Feb 2025
 16:19:36 +0100")
Message-ID: <jwv34gpiwcr.fsf-monnier+emacs@HIDDEN>
References: <877c62iquf.fsf@HIDDEN> <jwvcyftj3ck.fsf-monnier+emacs@HIDDEN>
 <87y0yhhjef.fsf@HIDDEN>
Date: Fri, 07 Feb 2025 11:06:25 -0500
User-Agent: Gnus/5.13 (Gnus v5.13)
MIME-Version: 1.0
Content-Type: text/plain
X-SPAM-INFO: Spam detection results:  0
 ALL_TRUSTED                -1 Passed through trusted hosts only via SMTP
 AWL -0.064 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DKIM_SIGNED               0.1 Message has a DKIM or DK signature,
 not necessarily valid
 DKIM_VALID -0.1 Message has at least one valid DKIM or DK signature
 DKIM_VALID_AU -0.1 Message has a valid DKIM or DK signature from author's
 domain
 DKIM_VALID_EF -0.1 Message has a valid DKIM or DK signature from envelope-from
 domain
X-SPAM-LEVEL: 
X-Spam-Score: -2.3 (--)
X-Debbugs-Envelope-To: 76108
Cc: 76108 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -3.3 (---)

>> The reason is that running the modification hooks N times (to replace
>> N chars) would be expensive.
> What if we called `modify_text' for each contiguous region of FROMCHARs?

AFAIK, in practice most such regions would span a single char anyway, so
I don't think it would make a big difference.

[ Furthermore, IIRC, another reasons we like to call the hooks only once
  is that those hooks would otherwise be called while in the middle of
  `subst-char-in-region`, so we'd need to be more careful with potential
  unexpected changes happening while running the hooks.  Nothing that
  can't be handled, but a known source of corner case bugs.  ]

> Do you know of a way we could benchmark it?

I think there are too many different cases to handle it with a benchmark.

>> The `modification-hooks` text property is weaker than the
>> `modification-hooks` overlay property: if you use an overlay here, then
>> you should be able to get the behavior your test wants by checking in
>> your `hook` the value of the second argument (which indicates if it's
>> the "before change" or the "after change" case): while the "before
>> change" call covers the whole region (actually, IIRC it covers the
>> region between the first substituted chars and the end argument), the
>> "after change" should be more specific and cover only the region between
>> the first and the last chars that were substituted.
>
> And that is the intended and desired behavior?

Yes.

> Personally I find it quite unintuitive to have `modification-hooks' be
> "Hooks that are called when some characters in region _might_ be
> modified".

You have to understand the distinction between the "before" and the
"after" calls (which correspond to the `before-change-functions` and the
`after-change-functions` hooks): the "before" call is there to announce
that some change*s* *may* happen in the specified region.

In the vast majority of cases you should care only about the "after" call.
[ Sadly, for the `modification-hooks` placed on text properties (as
  opposed to overlays), only the "before" call is available.  ]


        Stefan





Information forwarded to bug-gnu-emacs@HIDDEN:
bug#76108; Package emacs. Full text available.

Message received at 76108 <at> debbugs.gnu.org:


Received: (at 76108) by debbugs.gnu.org; 7 Feb 2025 15:19:49 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Fri Feb 07 10:19:49 2025
Received: from localhost ([127.0.0.1]:35891 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tgQ8z-0004k4-4K
	for submit <at> debbugs.gnu.org; Fri, 07 Feb 2025 10:19:49 -0500
Received: from mail-ed1-x52d.google.com ([2a00:1450:4864:20::52d]:49311)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.84_2) (envelope-from <thuna.cing@HIDDEN>)
 id 1tgQ8w-0004jj-5g
 for 76108 <at> debbugs.gnu.org; Fri, 07 Feb 2025 10:19:46 -0500
Received: by mail-ed1-x52d.google.com with SMTP id
 4fb4d7f45d1cf-5dcef33eeceso3954729a12.2
 for <76108 <at> debbugs.gnu.org>; Fri, 07 Feb 2025 07:19:46 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20230601; t=1738941579; x=1739546379; darn=debbugs.gnu.org;
 h=mime-version:message-id:date:references:in-reply-to:subject:cc:to
 :from:from:to:cc:subject:date:message-id:reply-to;
 bh=D3Eiv4bDgcfHyTHe5Q4ehqpfMW6FlELIqLFX3qCK6QA=;
 b=HrhXaipgdqdv7tZSv18JAtbp3ZoIlqE3T165G/lRgW1ECF6PrR/xPoWpHjS5h1NWNJ
 /eLSMsKqu3/nl+INQYcZbSAn6ka2Vfu1JUfHAbHiGRISAxmcH3tMYk7TIpjr9BO056Bs
 SEiFT0PSL/dx+hmnQOE6slRj8A8IGJyfvv/TtvGFtiqE30wT5M0ZwpAu4F3LMqrTUqPq
 iFnAhTgWJwW3qQPPpRze4BKd4F4FMmOeMSwX3sUNvBDwOW1Me+wB2+M9YHdQZ0uL2yKq
 N7k9T+nInZsMZZ9lb+UKrdQNVw0ypvwNWgWzwfiGe7Mw+sP9WaZkt45oja4/MP6NihPE
 QY7Q==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1738941579; x=1739546379;
 h=mime-version:message-id:date:references:in-reply-to:subject:cc:to
 :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
 bh=D3Eiv4bDgcfHyTHe5Q4ehqpfMW6FlELIqLFX3qCK6QA=;
 b=uyYxSGkCPQ1oUhDCJXN2tJMyPGkjmuc2Bj3gJi1NuFx1uQVjzGoSyjYju48IOvbiLz
 TJUVe0iwDP3coQfT7Q9FdP2nDnAxaGc8fRZ90TmherdNmlX92TRPSrPM/KX5e0AGmh/G
 AG6Ub3vPl3ikDV1d45usnJgQGPrtV9PbmTwRinIAAiiG5RgMpgH9iAAX19gXApIg5LeK
 Lglsj348WHhqjz7SqJXgXBKPCfNGq96LHXeomZvRxcT3cQ5sz77yqNtApJIZf6ZKdUXA
 GgaG9DjLrV0D2ZG/0k6scHzkPQ3W5ZNum7qhx3rYGbhvQq1RG6nPU6KoBik5bqryOg2F
 hXeQ==
X-Gm-Message-State: AOJu0Yxi+R88eIMdmPNrubbDbb+ottWNf/5yu4M9UO5LQhHl76L9hu3M
 mmToEq9x+IqwMzk2XlP6y+IEQp5mUEBh8Qc0HWsWHozNAISHCv6qqjEruuAT/D4=
X-Gm-Gg: ASbGnct7eTqrOXvq7v3vS/WqTbWOmZdrR7gQja83r9ms4Tx8LbfKvYkFIqQ650Bq+4G
 Q5xnyciVZwMSd/fDSI+kGreDGyLlZCFYYDDjQyabd6nKwXA9uYycdatim7Xgzcs8hPpBThjgF/2
 6ky59IgISKBsVYvBIFtKLUpQV4qTrj9NldVAQckLUYMiV4kOxNw8DjcDoSAyAdct3QvxKNM47qP
 o5oF8jS/hGHwSMRmBaKXokScU8dkdnClE4KNXF1yTRcBtxT+7c0pVkJsBd3wh8WGoqCHHy3B1Vu
 T9mMYy8v803X/QyF
X-Google-Smtp-Source: AGHT+IFe0v7LfWI4uaMMitVyqCt+0LIrUHHNa4QIdFrFiyVAyd3tRNyQOopAFO1hFuVNHZS7VWJt4A==
X-Received: by 2002:a05:6402:2085:b0:5de:3747:cddf with SMTP id
 4fb4d7f45d1cf-5de4503ffd0mr4043122a12.7.1738941579018; 
 Fri, 07 Feb 2025 07:19:39 -0800 (PST)
Received: from thuna-lis3 ([178.249.211.103]) by smtp.gmail.com with ESMTPSA id
 4fb4d7f45d1cf-5de4ad2c205sm1079373a12.33.2025.02.07.07.19.37
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Fri, 07 Feb 2025 07:19:37 -0800 (PST)
From: Thuna <thuna.cing@HIDDEN>
To: Stefan Monnier <monnier@HIDDEN>
Subject: Re: bug#76108: Call `modify_text' only on the text being replaced
 in `subst-char-in-region'
In-Reply-To: <jwvcyftj3ck.fsf-monnier+emacs@HIDDEN>
References: <877c62iquf.fsf@HIDDEN> <jwvcyftj3ck.fsf-monnier+emacs@HIDDEN>
Date: Fri, 07 Feb 2025 16:19:36 +0100
Message-ID: <87y0yhhjef.fsf@HIDDEN>
MIME-Version: 1.0
Content-Type: text/plain
X-Spam-Score: 0.0 (/)
X-Debbugs-Envelope-To: 76108
Cc: 76108 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.0 (-)

>> 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.
>
> The reason is that running the modification hooks N times (to replace
> N chars) would be expensive.

What if we called `modify_text' for each contiguous region of FROMCHARs?
It should be faster than calling it for each character and even
occasionally (though I wouldn't count on it) faster than calling it over
the whole region since it would avoid running hooks unnecessarily.  Do
you know of a way we could benchmark it?

> The `modification-hooks` text property is weaker than the
> `modification-hooks` overlay property: if you use an overlay here, then
> you should be able to get the behavior your test wants by checking in
> your `hook` the value of the second argument (which indicates if it's
> the "before change" or the "after change" case): while the "before
> change" call covers the whole region (actually, IIRC it covers the
> region between the first substituted chars and the end argument), the
> "after change" should be more specific and cover only the region between
> the first and the last chars that were substituted.

And that is the intended and desired behavior?  Personally I find it
quite unintuitive to have `modification-hooks' be "Hooks that are called
when some characters in region _might_ be modified".

I don't think that it's just me, either; in buffer-tests.el's
`overlay-modification-hooks' there is a comment stating
> Replacing text never calls `insert-in-front-hooks' or
> `insert-behind-hooks'.  It calls `modification-hooks' if the overlay
> covers any text that has changed.
which seems to suggest that the tests there were also written with the
understanding that `modification-hooks' should only be called for
characters actually being replaced.  Of course, those tests are for
`replace-match', but still, I see no reason why the two should have
different semantics (sans performance considerations, of course).




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#76108; Package emacs. Full text available.

Message received at 76108 <at> debbugs.gnu.org:


Received: (at 76108) by debbugs.gnu.org; 7 Feb 2025 13:34:09 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Fri Feb 07 08:34:09 2025
Received: from localhost ([127.0.0.1]:33460 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tgOUi-0003yZ-R7
	for submit <at> debbugs.gnu.org; Fri, 07 Feb 2025 08:34:09 -0500
Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:30407)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.84_2) (envelope-from <monnier@HIDDEN>)
 id 1tgOUf-0003y1-Dy
 for 76108 <at> debbugs.gnu.org; Fri, 07 Feb 2025 08:34:06 -0500
Received: from pmg2.iro.umontreal.ca (localhost.localdomain [127.0.0.1])
 by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id 92CF580251;
 Fri,  7 Feb 2025 08:33:57 -0500 (EST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca;
 s=mail; t=1738935236;
 bh=V8yWJe2L9d1TWCx0+F7Kh/uSyXAlG4+xt/TOIrygQ40=;
 h=From:To:Cc:Subject:In-Reply-To:References:Date:From;
 b=DtL6E3f9ARoIN6QMV5o0XTc48gYjh8VAptIZydQYyTRo1q9T5LmP97LAWZF7BN2Hs
 g5IxyohN/ZD00EBaHA612FytAuJ+VkasSvErf2C/8iIDK9xv/rFjfrl/9OR7hkr/hZ
 57y5ATNONG26x5cWjybTr6uIEgY+/8X2YiU1EIr8hDlOwBnv/l3py0tWih3sUeGpPg
 gewrA9xxufPRXn2/p6YaKSWrALs4EESG9zMStzJ1h1iK4wZFp7hEfdARhr5zT052bj
 sAtZRLvV5P/DjwFPQOg23SmG34MSE1i6gO6QVE5D9OmLMQnqHdU7LOgDe5SjKb2HHK
 fofU3iPfHnvsg==
Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1])
 by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id 252DE80144;
 Fri,  7 Feb 2025 08:33:56 -0500 (EST)
Received: from pastel (104-195-232-86.cpe.teksavvy.com [104.195.232.86])
 by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id F300412027F;
 Fri,  7 Feb 2025 08:33:55 -0500 (EST)
From: Stefan Monnier <monnier@HIDDEN>
To: Thuna <thuna.cing@HIDDEN>
Subject: Re: bug#76108: Call `modify_text' only on the text being replaced
 in `subst-char-in-region'
In-Reply-To: <877c62iquf.fsf@HIDDEN> (Thuna's message of "Fri, 07 Feb 2025
 00:41:12 +0100")
Message-ID: <jwvcyftj3ck.fsf-monnier+emacs@HIDDEN>
References: <877c62iquf.fsf@HIDDEN>
Date: Fri, 07 Feb 2025 08:33:55 -0500
User-Agent: Gnus/5.13 (Gnus v5.13)
MIME-Version: 1.0
Content-Type: text/plain
X-SPAM-INFO: Spam detection results:  0
 ALL_TRUSTED                -1 Passed through trusted hosts only via SMTP
 AWL -0.042 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DKIM_SIGNED               0.1 Message has a DKIM or DK signature,
 not necessarily valid
 DKIM_VALID -0.1 Message has at least one valid DKIM or DK signature
 DKIM_VALID_AU -0.1 Message has a valid DKIM or DK signature from author's
 domain
 DKIM_VALID_EF -0.1 Message has a valid DKIM or DK signature from envelope-from
 domain
X-SPAM-LEVEL: 
X-Spam-Score: -2.3 (--)
X-Debbugs-Envelope-To: 76108
Cc: 76108 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -3.3 (---)

> 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 probably misunderstanding something, but what you describe here
sounds like a bug in Org.

> 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.

The reason is that running the modification hooks N times (to replace
N chars) would be expensive.

> (ert-deftest test-subst-char-in-region-before-change-functions ()
>   (let ((hook (lambda (_beg _end) (error "Poor man's read-only"))))
>     (with-temp-buffer
>       (insert (propertize "foo" 'modification-hooks (list hook)) "," "bar")
>       (should-error (subst-char-in-region (point-min) (point-max) ?o ?e)))
>
>     (with-temp-buffer
>       (insert "foo" (propertize "," 'modification-hooks (list hook)) "bar")
>       ;; My best approximation of `should-not-error'.
>       (should (ignore-errors
>                 (subst-char-in-region (point-min) (point-max) ?o ?e)
>                 t))
>       (should (equal "fee,bar" (buffer-string))))))

The `modification-hooks` text property is weaker than the
`modification-hooks` overlay property: if you use an overlay here, then
you should be able to get the behavior your test wants by checking in
your `hook` the value of the second argument (which indicates if it's
the "before change" or the "after change" case): while the "before
change" call covers the whole region (actually, IIRC it covers the
region between the first substituted chars and the end argument), the
"after change" should be more specific and cover only the region between
the first and the last chars that were substituted.


        Stefan





Information forwarded to bug-gnu-emacs@HIDDEN:
bug#76108; Package emacs. Full text available.

Message received at 76108 <at> debbugs.gnu.org:


Received: (at 76108) by debbugs.gnu.org; 7 Feb 2025 10:15:34 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Fri Feb 07 05:15:34 2025
Received: from localhost ([127.0.0.1]:32918 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tgLOY-0002AY-Ct
	for submit <at> debbugs.gnu.org; Fri, 07 Feb 2025 05:15:34 -0500
Received: from mail-ej1-x635.google.com ([2a00:1450:4864:20::635]:60888)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.84_2) (envelope-from <thuna.cing@HIDDEN>)
 id 1tgLOW-0002AE-1N
 for 76108 <at> debbugs.gnu.org; Fri, 07 Feb 2025 05:15:32 -0500
Received: by mail-ej1-x635.google.com with SMTP id
 a640c23a62f3a-aaee2c5ee6eso326955566b.1
 for <76108 <at> debbugs.gnu.org>; Fri, 07 Feb 2025 02:15:32 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20230601; t=1738923326; x=1739528126; darn=debbugs.gnu.org;
 h=mime-version:message-id:date:references:in-reply-to:subject:cc:to
 :from:from:to:cc:subject:date:message-id:reply-to;
 bh=yAbtlOzCfFKGwVG3XYbE4M+Jf6s529Crxrmo1TIKjqQ=;
 b=ZDUh0kuMuB7ElkkbqJ6rcQZny0HYb+RqhuKBFsoAu28DwQoat13oKEmLz0cYtKPjFE
 ZoF9COUw3kjtnwmHgkvwrp0g+ARp6elrYUnujF/WI7QJtRZYxOBjA0qZxvfIVGOYbCif
 upAx9KdTkpCQlpjNMINlq75JsJDRwPphbThCpOsYJk7+d8AARbbcD4W/UzQ7DQzmw7qQ
 mMIKq9GI9VFLfVU/58onx4MB7WMwLGIvqcZkaRWnWw/SwUajbq4SjhH5oJsiMDtkoCw9
 YEAWiU1k8XAuBvFGj2gIvfljjOJylT7DuQW7nDdVIhvEooGDOoTwA0m6tEV6ggq7MVK3
 A3aQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1738923326; x=1739528126;
 h=mime-version:message-id:date:references:in-reply-to:subject:cc:to
 :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
 bh=yAbtlOzCfFKGwVG3XYbE4M+Jf6s529Crxrmo1TIKjqQ=;
 b=SYYkhtfS1KjWN/xScxc7u1a6sj5ppcD6aBEDD7AQIDPolp0KsMUhFODvLYJcjrf8Bc
 M/RQzM6MW7iTm284EYyI3xs4Jm0ms54YMrPSU1ojiM00lbG1D0hUfgU06tUkKgpPru97
 aLmTftODl5P2Vizna0Eby3eNYUTiiqdJRuLA9bUGYd+Xhjpzl64UjcHUn2gVF0xvVJw/
 pN/xTun3ryYDCtoxHPKVOf0TPaLi+vZZ2JNorRShCeAmpqUodcqxUbY3LAn0h1Z/Mjhl
 OcC7cio7mnt+2B3gYgkH2u7Sj2YKd/rpxmwxwNTCXTzpNnLqkXGnu7oImkeLZb05asKw
 EwFQ==
X-Gm-Message-State: AOJu0Yyqx/atBLUeZ9z5SLZACEzUoDb4m/6UFmNoF+Gs/8v26SUvj362
 KOmtb4d0a6ew/VaBVhV0t71JiK7VIZkoBVvYP17qhhudvsqMKfly2XDhF4FfYXWe3g==
X-Gm-Gg: ASbGncvQjHm60gdBGvv5F1BYWp4UClsVKCI3UjpQViraUhOynreOb4cQToqjQhHe+i6
 MzwggHbBxd+OPOufqPqhfGyBHVoOk3kn7ivpAKxy6Xpm0VWe2a9dCMkj/EolfELxy5ukYizWyj+
 admnYbPe0Y9RfTdxH2c1VhGqaIkcvAMUNvCpuZA8jMTMRpIS5Mo82Wq/EZkI+qJIs8M3Z/yXtqW
 wwlp9LRXl8u32WkQq9eSWOMlrczCvJH3o+wU2hgVW711IvmVmLovpkOykqVNed5pvva+iY9AGtq
 2vr/1a0mTTMMuQ8J
X-Google-Smtp-Source: AGHT+IGmOZ3sSIEKj+fuR32BA0bCBOO83KFNc3OMJgHWYoDK/YnheJdYOHal2ajBtN02F6B/t7CcKQ==
X-Received: by 2002:a17:907:3dac:b0:ab2:fefe:7156 with SMTP id
 a640c23a62f3a-ab789bfba87mr215061466b.43.1738923325345; 
 Fri, 07 Feb 2025 02:15:25 -0800 (PST)
Received: from thuna-lis3 ([178.249.211.103]) by smtp.gmail.com with ESMTPSA id
 a640c23a62f3a-ab772f4870bsm242080166b.32.2025.02.07.02.15.24
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Fri, 07 Feb 2025 02:15:24 -0800 (PST)
From: Thuna <thuna.cing@HIDDEN>
To: Eli Zaretskii <eliz@HIDDEN>, Stefan Monnier <monnier@HIDDEN>
Subject: Re: bug#76108: Call `modify_text' only on the text being replaced
 in `subst-char-in-region'
In-Reply-To: <86h6565eyi.fsf@HIDDEN>
References: <877c62iquf.fsf@HIDDEN> <86h6565eyi.fsf@HIDDEN>
Date: Fri, 07 Feb 2025 11:15:24 +0100
Message-ID: <871pwahxhf.fsf@HIDDEN>
MIME-Version: 1.0
Content-Type: text/plain
X-Spam-Score: 0.0 (/)
X-Debbugs-Envelope-To: 76108
Cc: 76108 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.0 (-)

> Why do you think this is a bug in Emacs core and not in how org-mode
> handles latex fragments?

I figured this was a bug in Emacs so I came here first, but I'll go
ahead and patch org-mode if this turns out to be intended behavior.

>> 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.
>
> AFAIU, your change will cause modify_text to be called only once, like
> we do now, but only for the first replacement of the character in
> question.  So if the loop in subst-char-in-region replaces the
> character more than once, the fact that there were changes in other
> places in the region will not be reported.

It's possible that I'm reading the code wrong, but it seems to me that
it's being called for every change.  If not, I could just make it so.
I'm not familiar with `modify_text' so I could be misunderstanding, but
shouldn't it only be called on the text that is actively being modified
(in this case replaced)?

> Or maybe I'm missing something.  But to establish whether this change
> introduces a regression, we need some test, and the test should
> include both single and multiple substitutions, and also both ASCII
> and non-ASCII characters.  Then we will have a good base for assessing
> this patch, and also our test suite will become better -- a nice
> bonus.
>
> So could you please write such a test, and then see whether your
> change still passes it?

Sure, I'll do that.  For a start, just to demonstrate the problem,
here's a test which fails on a July build (I don't think anything
changed since?) and passes with my patch.  Specifically, it's the
`should-not-error' below that fails.  (Also, is there a canonical way to
write `should-not-error' that I don't know about?)

(ert-deftest test-subst-char-in-region-before-change-functions ()
  (let ((hook (lambda (_beg _end) (error "Poor man's read-only"))))
    (with-temp-buffer
      (insert (propertize "foo" 'modification-hooks (list hook)) "," "bar")
      (should-error (subst-char-in-region (point-min) (point-max) ?o ?e)))

    (with-temp-buffer
      (insert "foo" (propertize "," 'modification-hooks (list hook)) "bar")
      ;; My best approximation of `should-not-error'.
      (should (ignore-errors
                (subst-char-in-region (point-min) (point-max) ?o ?e)
                t))
      (should (equal "fee,bar" (buffer-string))))))




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#76108; Package emacs. Full text available.

Message received at 76108 <at> debbugs.gnu.org:


Received: (at 76108) by debbugs.gnu.org; 7 Feb 2025 08:37:02 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Fri Feb 07 03:37:02 2025
Received: from localhost ([127.0.0.1]:60965 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tgJrC-0005jY-9A
	for submit <at> debbugs.gnu.org; Fri, 07 Feb 2025 03:37:02 -0500
Received: from eggs.gnu.org ([2001:470:142:3::10]:43338)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.84_2) (envelope-from <eliz@HIDDEN>) id 1tgJr9-0005j0-DT
 for 76108 <at> debbugs.gnu.org; Fri, 07 Feb 2025 03:37:00 -0500
Received: from fencepost.gnu.org ([2001:470:142:3::e])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <eliz@HIDDEN>)
 id 1tgJr2-0007kD-Lo; Fri, 07 Feb 2025 03:36:52 -0500
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org;
 s=fencepost-gnu-org; h=References:Subject:In-Reply-To:To:From:Date:
 mime-version; bh=nL6HjsKEVg42Cts239AgYg/4RITUa4uh0TT7yWNXdto=; b=oi3RCY6nBnve
 UuUEmgWuff5kpCe8QLG0o5L+eQsLraegB7kFHEIRdM6LrCDfXTbjn+pmP7bHeQfMRIhYP7be+tXP4
 sS4SVntxyzx+HDhGoMbjmEirbCc7pCL41ws9F4F9+QuyHSvAYctaRNYPqxsNNPSQMLVe5tyuokfK8
 WUHATjVgtRu4EBB+y4fIXYejfa7dk+K8G+ZOEdC5aTQpzsNZxCPGtVfEGJ7GwHVFbDIxUpcZ7xu2D
 JDUGMmPzMqUdppZfjo+7XMhpLSfp2SPbtQeOZpIRGCE52W+IkOhI+5eMB7kuYvrBzypO8bltgtS+l
 aCPkFui2RGMXj5zuhCPiNw==;
Date: Fri, 07 Feb 2025 10:36:21 +0200
Message-Id: <86h6565eyi.fsf@HIDDEN>
From: Eli Zaretskii <eliz@HIDDEN>
To: Thuna <thuna.cing@HIDDEN>,
 Stefan Monnier <monnier@HIDDEN>
In-Reply-To: <877c62iquf.fsf@HIDDEN> (message from Thuna on Fri, 07 Feb
 2025 00:41:12 +0100)
Subject: Re: bug#76108: Call `modify_text' only on the text being replaced in
 `subst-char-in-region'
References: <877c62iquf.fsf@HIDDEN>
X-Spam-Score: -2.3 (--)
X-Debbugs-Envelope-To: 76108
Cc: 76108 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -3.3 (---)

> From: Thuna <thuna.cing@HIDDEN>
> 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.

That's right.  But if the character in question is substituted more
than once, we call modify_text only once, for the entire region.

> 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.

Why do you think this is a bug in Emacs core and not in how org-mode
handles latex fragments?

> 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.

AFAIU, your change will cause modify_text to be called only once, like
we do now, but only for the first replacement of the character in
question.  So if the loop in subst-char-in-region replaces the
character more than once, the fact that there were changes in other
places in the region will not be reported.

Or maybe I'm missing something.  But to establish whether this change
introduces a regression, we need some test, and the test should
include both single and multiple substitutions, and also both ASCII
and non-ASCII characters.  Then we will have a good base for assessing
this patch, and also our test suite will become better -- a nice
bonus.

So could you please write such a test, and then see whether your
change still passes it?

TIA

P.S. Adding Stefan to the discussion, because I think we once
discussed this function in this respect, and decided that the way it
calls modify_text is correct -- but I cannot find that discussion in
my archives, so maybe I was dreaming.




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#76108; Package emacs. Full text available.

Message received at submit <at> debbugs.gnu.org:


Received: (at submit) by debbugs.gnu.org; 6 Feb 2025 23:41:27 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Thu Feb 06 18:41:27 2025
Received: from localhost ([127.0.0.1]:59870 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tgBUs-0005D3-SX
	for submit <at> debbugs.gnu.org; Thu, 06 Feb 2025 18:41:27 -0500
Received: from lists.gnu.org ([2001:470:142::17]:58936)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.84_2) (envelope-from <thuna.cing@HIDDEN>)
 id 1tgBUr-0005Co-Bf
 for submit <at> debbugs.gnu.org; Thu, 06 Feb 2025 18:41:25 -0500
Received: from eggs.gnu.org ([2001:470:142:3::10])
 by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <thuna.cing@HIDDEN>)
 id 1tgBUl-0007u7-Ux
 for bug-gnu-emacs@HIDDEN; Thu, 06 Feb 2025 18:41:19 -0500
Received: from mail-ed1-x52b.google.com ([2a00:1450:4864:20::52b])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.90_1) (envelope-from <thuna.cing@HIDDEN>)
 id 1tgBUk-0006j8-CG
 for bug-gnu-emacs@HIDDEN; Thu, 06 Feb 2025 18:41:19 -0500
Received: by mail-ed1-x52b.google.com with SMTP id
 4fb4d7f45d1cf-5dca468c5e4so278230a12.1
 for <bug-gnu-emacs@HIDDEN>; Thu, 06 Feb 2025 15:41:18 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20230601; t=1738885275; x=1739490075; darn=gnu.org;
 h=mime-version:message-id:date:subject:to:from:from:to:cc:subject
 :date:message-id:reply-to;
 bh=qy8pjHHe/Mn/NRt6qh70/rzjzxaRE33D1mfa8A4JIA8=;
 b=IjXMWe8Lesc5LYxiLl0r0EYmnJTJxpqSx34qa9s+d1Tawi9K26fxLvotl8Ak+4iQ0S
 xqmRXD+Lx4w1YotaasT0y6ulswKybuRpPxSUgf720zxW1p4zFxX5I2Gb+xPGstc/UCSF
 zhTYiTVxCm6mj3VnTVwEhl5qhSRu1nvAaufRg4fLiDjgwkfMq/HbMZYYhf9BV9PaWFOF
 tmPVELyuXwb7fhqt75p1Jc5uAJhUk+R3SmbE2YJ5v1kQdV69FZJMaImx8fwNbqey2tsx
 60LSJsPZ5CNGQ/e4akUPqZ1/3V8CEjy4AD9qpyWefLh47pzPWSRG8Pv/PCQaOT44gi5t
 X/qQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1738885275; x=1739490075;
 h=mime-version:message-id:date:subject:to:from:x-gm-message-state
 :from:to:cc:subject:date:message-id:reply-to;
 bh=qy8pjHHe/Mn/NRt6qh70/rzjzxaRE33D1mfa8A4JIA8=;
 b=iIguCaDyQDUVmka0j/vc6/4UA/eRrqaKexeTv2OT+BtAismqeLB21nbtIInDuAZP4M
 iDdyvEkyNj/aXgd8bP/FyzHdchpIbMpdRKR7FH6HPX5kn2WGHLTHr/MUyrKFVMbGDCyM
 HMEr6RnWsq9psQNWMAt4PKOE7cpYiaMJg1VHM+gPRm62NbGH8FE8EmoL/QpURTNR0hAh
 vr4d/HCC9qYb/4BQAxQWV4SM29z4HWSsMMRfOzSDzQJzApJxAFJRG7aKWoOAkLqNKMoq
 QjrXvYwqimSjgwyO6K8grsnnREe7DB1poA8W+RFkgzj/wyFXzQsAKQWzao1kFTrRP9NO
 eHZg==
X-Gm-Message-State: AOJu0YzpOPVxUhc1/0UM22UFS757+4hP6Pu4ThsdqakBEPBfg0dnX5Uq
 Vn08OKYj4p1JXh2u6r+mscT9KqPxM7rVLA0WHco5uL+K6Np+MUNjRs6GwvET30vaNw==
X-Gm-Gg: ASbGnctzurA90abrdUhCyRNZ8rSBN/pDbAh736lYa5YMrnZHMEWlZ8bNIb7jDuEbSH0
 AFVX2Ly8KW9jM1lJx8iD8ledC+luth5Ibi1+O64aF+0HrQgJj6jnVn/tQi7ZMMh7hEmhd5EAtSg
 9GTBuBW9c3M+O1uQs3idIPPSTO3fUC1zHjLspOEpUj8U+YD0G5RaextBj5O0MdkO7YqAjV4DmK4
 X5xdShRPNbRpg6fXYVa8RVeh/d7H6lzWFSJHnrnk72YxPNgxgjYf3D1JfzQF3e++EFSwLamW4n5
 zXitLKEvVlQIxTLX
X-Google-Smtp-Source: AGHT+IGBkLSc2kRVOyXNc6oakBZJD2BhWwv4ielXCJ/C1F/9mqHCsdff4eVB4aYncNyKezE6xOK7DQ==
X-Received: by 2002:a17:907:948a:b0:aac:29a:2817 with SMTP id
 a640c23a62f3a-ab789b22c1dmr56134566b.26.1738885275120; 
 Thu, 06 Feb 2025 15:41:15 -0800 (PST)
Received: from thuna-lis3 ([178.249.211.103]) by smtp.gmail.com with ESMTPSA id
 a640c23a62f3a-ab772f84383sm167155366b.50.2025.02.06.15.41.12
 for <bug-gnu-emacs@HIDDEN>
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Thu, 06 Feb 2025 15:41:13 -0800 (PST)
From: Thuna <thuna.cing@HIDDEN>
To: bug-gnu-emacs@HIDDEN
Subject: Call `modify_text' only on the text being replaced in
 `subst-char-in-region'
Date: Fri, 07 Feb 2025 00:41:12 +0100
Message-ID: <877c62iquf.fsf@HIDDEN>
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="=-=-="
Received-SPF: pass client-ip=2a00:1450:4864:20::52b;
 envelope-from=thuna.cing@HIDDEN; helo=mail-ed1-x52b.google.com
X-Spam_score_int: -20
X-Spam_score: -2.1
X-Spam_bar: --
X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1,
 DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001,
 RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001,
 SPF_PASS=-0.001 autolearn=ham autolearn_force=no
X-Spam_action: no action
X-Spam-Score: 1.0 (+)
X-Debbugs-Envelope-To: submit
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -0.0 (/)

--=-=-=
Content-Type: text/plain

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.


--=-=-=
Content-Type: text/x-patch
Content-Disposition: attachment;
 filename=0001-Only-modify-the-text-being-replaced-in-subst-char-in.patch

From 51018ff7bd7e15d0d831a6acad704d78285c9903 Mon Sep 17 00:00:00 2001
From: Thuna <thuna.cing@HIDDEN>
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


--=-=-=--




Acknowledgement sent to Thuna <thuna.cing@HIDDEN>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs@HIDDEN. Full text available.
Report forwarded to bug-gnu-emacs@HIDDEN:
bug#76108; Package emacs. Full text available.
Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.
Last modified: Wed, 12 Feb 2025 03:45:02 UTC

GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997 nCipher Corporation Ltd, 1994-97 Ian Jackson.