From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: phillip.lord@russet.org.uk Newsgroups: gmane.emacs.bugs Subject: bug#70077: An easier way to track buffer changes Date: Fri, 29 Mar 2024 18:20:32 -0400 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="27941"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Roundcube Webmail/1.6.0 Cc: Yuan Fu , 70077@debbugs.gnu.org, Ihor Radchenko , Qiantan Hong , =?UTF-8?Q?Fr=C3=A9d=C3=A9ric?= Bour , =?UTF-8?Q?Jo=C3=A3o_?= =?UTF-8?Q?T=C3=A1vora?= , Nicolas Goaziou , Alan Mackenzie , Stephen Leake , Alan Zimmerman To: Stefan Monnier Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Mar 29 23:22:31 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1rqKcJ-00072A-6d for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 29 Mar 2024 23:22:31 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rqKat-00059i-La; Fri, 29 Mar 2024 18:21:03 -0400 Original-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 ) id 1rqKaq-00059P-Rb for bug-gnu-emacs@gnu.org; Fri, 29 Mar 2024 18:21:01 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rqKaq-0001r6-K1 for bug-gnu-emacs@gnu.org; Fri, 29 Mar 2024 18:21:00 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rqKas-0003cI-3O for bug-gnu-emacs@gnu.org; Fri, 29 Mar 2024 18:21:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: phillip.lord@russet.org.uk Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 29 Mar 2024 22:21:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 70077 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 70077-submit@debbugs.gnu.org id=B70077.171175084413866 (code B ref 70077); Fri, 29 Mar 2024 22:21:02 +0000 Original-Received: (at 70077) by debbugs.gnu.org; 29 Mar 2024 22:20:44 +0000 Original-Received: from localhost ([127.0.0.1]:43576 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rqKaZ-0003bY-Hg for submit@debbugs.gnu.org; Fri, 29 Mar 2024 18:20:43 -0400 Original-Received: from cloud103.planethippo.com ([78.129.190.68]:38172) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rqKaX-0003aw-NM for 70077@debbugs.gnu.org; Fri, 29 Mar 2024 18:20:42 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=russet.org.uk; s=default; h=Content-Transfer-Encoding:Content-Type: Message-ID:References:In-Reply-To:Subject:Cc:To:From:Date:MIME-Version:Sender :Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=j4VfnN77pwpLxdYLNCHa3x9FDkPfvg91FTM3VgVjRP4=; b=2G25W5MMAd+P38mfZ2A8kSm0so 1yfxcQW1puIKjosDnIUSGa6XcDXWq6GB1ZRbdBdWNwjRU4tDClT0UW10d+zQVS8C7KzYrqakInpeO 9UeMDD7dqmBwyvI2RlvrBPx9LpdJBu2ee6ngNrZDwsIUl2DnMJRUWIbhVFOoZstEpZ5rfdP3heUOo aje6o2mMc2K+d/Iyr7mWN1Bvavo6IofGC3T/35cxzHrIfwCMPGOPFjXFZbkdhiSTYmthMmjfGTQel 1ieMknzPK50FV549Qp2hWUGZyv0KmHWdjxHDLXom6oEiuPXoxUhUnXVEF30yVxWwc/LqjbHshzlDE 3TCBqc1Q==; Original-Received: from [::1] (port=51982 helo=cloud103.planethippo.com) by cloud103.planethippo.com with esmtpa (Exim 4.96.2) (envelope-from ) id 1rqKaR-005nA2-0I; Fri, 29 Mar 2024 18:20:33 -0400 In-Reply-To: X-Sender: phillip.lord@russet.org.uk X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - cloud103.planethippo.com X-AntiAbuse: Original Domain - debbugs.gnu.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - russet.org.uk X-Get-Message-Sender-Via: cloud103.planethippo.com: authenticated_id: phillip.lord@russet.org.uk X-Authenticated-Sender: cloud103.planethippo.com: phillip.lord@russet.org.uk X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:282317 Archived-At: On 2024-03-29 12:15, Stefan Monnier wrote: > Tags: patch > > Our `*-change-functions` hook are fairly tricky to use right. > Some of the issues are: > > - before and after calls are not necessarily paired. > - the beg/end values don't always match. > - there can be thousands of calls from within a single command. > - these hooks are run at a fairly low-level so there are things they > really shouldn't do, such as modify the buffer or wait. > - the after call doesn't get enough info to rebuild the before-change > state, > so some callers need to use both before-c-f and after-c-f (and then > deal with the first two points above). > > The worst part is that those problems occur rarely, so many coders > don't > see it at first and have to learn them the hard way, sometimes forcing > them to rethink their original design. > > So I think we should provide something simpler. > I attached a proof-of-concept API which aims to do that, with the > following entry points: > > (defun track-changes-register ( signal) > "Register a new tracker and return a new tracker ID. > SIGNAL is a function that will be called with no argument when > the current buffer is modified, so that we can react to the change. > Once called, SIGNAL is not called again until `track-changes-fetch' > is called with the corresponding tracker ID." > > (defun track-changes-unregister (id) > "Remove the tracker denoted by ID. > Trackers can consume resources (especially if `track-changes-fetch' > is > not called), so it is good practice to unregister them when you > don't > need them any more." > > (defun track-changes-fetch (id func) > "Fetch the pending changes. > ID is the tracker ID returned by a previous > `track-changes-register'. > FUNC is a function. It is called with 3 arguments (BEGIN END > BEFORE) > where BEGIN..END delimit the region that was changed since the last > time `track-changes-fetch' was called and BEFORE is a string > containing > the previous content of that region. > > If no changes occurred since the last time, FUNC is not called and > we return nil, otherwise we return the value returned by FUNC, > and re-enable the TRACKER corresponding to ID." > > It's not meant as a replacement of the existing hooks since it doesn't > try to accommodate some uses such as those that use before-c-f to > implement a finer-grained form of read-only text. > > The driving design was: > > - Try to provide enough info such that it is possible and easy to > maintain a copy of the buffer simply by applying the reported > changes. > E.g. for uses such as `eglot.el` or `crdt.el`. > - Make the API less synchronous: take care of combining small changes > into larger ones, and let the clients decide when they react to > changes. > > If you're in the Cc, it's because I believe you have valuable > experience > with those hooks, so I'd be happy to hear your thought about whether > you think this would indeed (have) be(en) better than what we have. Your description of the problem is entirely consistent with my experience. The last time I checked it was `subst-char-in-region' which was causing most of the difficulties, normally as a result of `fill-paragraph'. If I remember correctly, I think this wouldn't be enough for my use. You keep two buffers in sync, you have to use before-change-function -- it is only before any change that the two buffers are guaranteed to be in sync and it is this that allows you to work out what the `start' and `end' positions mean in the copied buffer. Afterward, you cannot work out what the end position because you don't know if the change is a change, insertion, deletion or both. Last time I checked, I did find relatively few primitives that were guilty of being inconsistent -- in the case of `subst-char-in-region', it returned the maximal area of effect before the and the minimal area of effect after. Would it not be easier to fix these? Phil