unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: Joost Kremers <joostkremers@fastmail.fm>
Cc: "'Help-Gnu-Emacs (help-gnu-emacs@gnu.org)'" <help-gnu-emacs@gnu.org>
Subject: Re: track-changes and undo
Date: Mon, 22 Apr 2024 08:38:14 -0400	[thread overview]
Message-ID: <jwvpluhmwgl.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <86il09g8bo.fsf@p200300d6272f1758742b590e6df1ee48.dip0.t-ipconnect.de> (Joost Kremers's message of "Mon, 22 Apr 2024 09:33:47 +0200")

[-- Attachment #1: Type: text/plain, Size: 1466 bytes --]

> 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

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: criticmarkup.diff --]
[-- Type: text/x-diff, Size: 2963 bytes --]

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

  reply	other threads:[~2024-04-22 12:38 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-21  8:23 track-changes and undo Joost Kremers
2024-04-21 14:54 ` Stefan Monnier
2024-04-22  7:33   ` Joost Kremers
2024-04-22 12:38     ` Stefan Monnier [this message]
2024-04-22 21:41       ` Joost Kremers
2024-04-22 23:04         ` Stefan Monnier
2024-04-23  5:58           ` Eli Zaretskii
2024-04-23  7:06           ` Joost Kremers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=jwvpluhmwgl.fsf-monnier+emacs@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --cc=help-gnu-emacs@gnu.org \
    --cc=joostkremers@fastmail.fm \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).