From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.help Subject: Re: track-changes and undo Date: Mon, 22 Apr 2024 08:38:14 -0400 Message-ID: References: <864jbvdszb.fsf@p200300d6272f17de4b8317bb694a7515.dip0.t-ipconnect.de> <86il09g8bo.fsf@p200300d6272f1758742b590e6df1ee48.dip0.t-ipconnect.de> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="4201"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: "'Help-Gnu-Emacs (help-gnu-emacs@gnu.org)'" To: Joost Kremers Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Mon Apr 22 14:38:51 2024 Return-path: Envelope-to: geh-help-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 1ryswc-0000mM-Ox for geh-help-gnu-emacs@m.gmane-mx.org; Mon, 22 Apr 2024 14:38:51 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ryswI-00059t-7d; Mon, 22 Apr 2024 08:38:30 -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 1ryswE-00057x-OR for help-gnu-emacs@gnu.org; Mon, 22 Apr 2024 08:38:26 -0400 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ryswC-0003PA-Fa for help-gnu-emacs@gnu.org; Mon, 22 Apr 2024 08:38:26 -0400 Original-Received: from pmg3.iro.umontreal.ca (localhost [127.0.0.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 9B2E44411ED; Mon, 22 Apr 2024 08:38:21 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1713789495; bh=DfVuutXhdgGzlpV3i3uikufXhw4n0fuwFkWPxQaMgrg=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=mi1cl+U/wUfDkaoIjSJsl2WaDaAxvF0SOBCBqBpO8PDUU6JqbjGI4wBK5x/oHIHFs +1/yt8ardgMzQeT+1TLuzLAYXEUaJNCJeBq43yPBR6gWRGK9DRifiB1wKuhHgaAUyI nXmo/hBlQt+98eOhYosfM1wxE6AExWIm8p7KvwRlMg7+svH3LOcYakrRJCkBDxySEq mWJMRdJOljbdXD9YzyI/BVNvwozHfdG+MeZ8sWWMyRLn8E68j5xnaWCW+L6X6Yfah2 r542jVd2SXZIx/3Aa9xsLPaqgvgMpdXqYO+hi/hKZTh754M+A5VXfOhHabkqOh4iCJ S7rUbizJdOA7Q== Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 38D2B44131E; Mon, 22 Apr 2024 08:38:15 -0400 (EDT) Original-Received: from pastel (unknown [45.72.201.215]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 15B561202CB; Mon, 22 Apr 2024 08:38:15 -0400 (EDT) In-Reply-To: <86il09g8bo.fsf@p200300d6272f1758742b590e6df1ee48.dip0.t-ipconnect.de> (Joost Kremers's message of "Mon, 22 Apr 2024 09:33:47 +0200") Received-SPF: pass client-ip=132.204.25.50; envelope-from=monnier@iro.umontreal.ca; helo=mailscanner.iro.umontreal.ca X-Spam_score_int: -42 X-Spam_score: -4.3 X-Spam_bar: ---- X-Spam_report: (-4.3 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.help:146427 Archived-At: --=-=-= Content-Type: text/plain > The short version: The package makes it easier to add CriticMarkup to a > document, i.e., markup to indicate additions, deletions, etc. There are > keybindings for the various markups (`{++...++}` for additions, `{--...--}` for > deletions, etc.). > > The follow-changes mode tries to do that automatically: if you delete a word, > instead of actually deleting it, the word is enclosed in deletion markup. Thanks, Yes, that makes a lot of sense and checking `undo-in-progress` is indispensable to make undo work. With the current API (if you want to use `track-changes`, that is) the best you can do is to use the `:immediate` option so that your code gets called directly from the `after-change-functions` where you can (still) see the `undo-in-progress`. The gain from `track-changes` is just to provide you with the "before" string for deletions so it takes care of reading it in `before-change-functions` and then providing it to you in the `after-change-functions` (with the advantage that it detects/handles the various corner cases where that pairing fails). One other thing that you might have trouble to reproduce with `track-changes` is the following test: (and (= beg (point-min)) (= end (point-max))) that you have in `cm-before-change`. I'm not completely sure what this is for, tho. Is it for `revert-buffer`? [ I tend to do "destructive reads", so the patch below is the result of reading that part of your code. ] Stefan --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=criticmarkup.diff diff --git a/cm-mode.el b/cm-mode.el index 43c705a..882f77d 100644 --- a/cm-mode.el +++ b/cm-mode.el @@ -862,43 +862,31 @@ substitutions, `d' for comments and highlights." ;;; Follow Changes -(defvar cm-follow-changes nil - "Flag indicating whether follow changes mode is active.") -(make-variable-buffer-local 'cm-follow-changes) - (defvar cm-current-deletion nil "The deleted text in follow changes mode. The value is actually a list consisting of the text and a flag indicating whether the deletion was done with the backspace key.") -(defun cm-follow-changes (&optional arg) - "Activate follow changes mode. -If ARG is positive, activate follow changes mode, if ARG is 0 or -negative, deactivate it. If ARG is `toggle', toggle follow -changes mode." - (interactive (list (or current-prefix-arg 'toggle))) - (let ((enable (if (eq arg 'toggle) - (not cm-follow-changes) - (> (prefix-numeric-value arg) 0)))) - (if enable - (progn - (add-to-list 'before-change-functions 'cm-before-change t) - (add-to-list 'after-change-functions 'cm-after-change) - (setq cm-follow-changes t) - (message "Follow changes mode activated.")) - (setq before-change-functions (delq 'cm-before-change before-change-functions)) - (setq after-change-functions (delq 'cm-after-change after-change-functions)) - (setq cm-follow-changes nil) - (message "Follow changes mode deactivated.")))) +(define-minor-mode cm-follow-changes ;FIXME: Shouldn't it end in `mode'? + "Minor mode to follow changes." + :global nil + (if cm-follow-changes + (progn + (add-hook 'before-change-functions #'cm-before-change t t) + (add-hook 'after-change-functions #'cm-after-change nil t)) + (remove-hook 'before-change-functions #'cm-before-change t) + (remove-hook after-change-functions #'cm-after-change t))) (defun cm-before-change (beg end) "Function to execute before a buffer change. BEG and END are the beginning and the end of the region to be changed." (unless (or undo-in-progress + ;; FIXME: What do you mean by "buffer switches"? (and (= beg (point-min)) (= end (point-max)))) ; This happens on buffer switches. (if (= beg end) ; Addition. + ;; FIXME: There can be corner cases where point is not at beg/end. (cm-make-addition (cm-markup-at-point)) ;; When the deletion was done with backspace, point is at end. We record ;; this in `cm-current-deletion' so we can position point correctly. @@ -906,7 +894,7 @@ changed." (defun cm-after-change (beg end length) "Function to execute after a buffer change. -This function marks deletions. See cm-before-change for details. +This function marks deletions. See `cm-before-change' for details. BEG and END mark the region to be changed, LENGTH is the length of the affected text." (unless (or undo-in-progress --=-=-=--