Subject: Re: bug#74276: 30.0.90; undo does not account for overlays now that markers are not used

From: Stefan Monnier <[email-masked]>
Date: Thu, 14 Nov 2024 10:36:22 -0500

>> > Stefan, does undo restore overlays, and if so, how?
>> No, I don't think `undo` has ever tried to restore overlays.
>> But it does attempt to restore marker positions, so I guess there can be
>> cases where the switch to itree has caused overlays' boundaries to be
>> restored less well than before.
> Well, in this particular recipe Emacs 29 and 30 behave exactly like
> Emacs 25, do at least in this case there was no regression.

Which suggests that maybe the code we have to save/restore the position of
markers in `buffer-undo-list` doesn't(didn't?) really do its job.

> But maybe we should add to undo the capability of restoring the
> overlays.

Maybe.

        Stefan

PS: Reading the `record_marker_adjustments` in Emacs-28, it seems that
this code would happily add to the `buffer-undo-list` markers that were
"internal" to overlays.  That reminds me of a discussion many years ago
where I suggested we add functions to return the begin/end markers of an
overlay so that ELisp code could use `set-marker-insertion-type` to
change the insertion type of the beginning/end of an overlay (which is
currently otherwise fixed at creation).  This was rejected because
exposing such internals would be undesirable (e.g. making
backward-incompatible the change we implemented in Emacs-29) and risky
(allowing inconsistencies like overlays whose beg and end don't point to
the same buffer).  It turns out that such functions could have been
hacked all along in ELisp by (ab)using this `record_marker_adjustments`
code:

    (defun overlay-markers (ol)
      "Return the two markers of an overlay as (BEG . END)."
      (with-temp-buffer
        (insert "aa")
        (let ((beg (point))
              (_ (insert "bbb"))
              (end (point)))
          (insert "cccc")
          (move-overlay ol beg end (current-buffer))
          (let ((buffer-undo-list ()))
            (delete-region (point-min) (point-max))
            (cons (car (rassq -2 buffer-undo-list))
                  (car (rassq -5 buffer-undo-list)))))))

🙂