Subject: 31.0.50; `apply` elements screw up undo in region

From: Stefan Monnier <[email-masked]>
Date: Mon, 25 Nov 2024 01:19:43 -0500

Package: Emacs
Version: 31.0.50

We currently don't support `apply` undo-elements very well (if at all)
in the undo-in-region feature.
Those elements used to be rare, but now that `comment-region` and other
users of `combine-change-calls` generate them, it's not as rare.

The following test fails because of that:

    (ert-deftest simple-tests--undo-apply ()
      (with-temp-buffer
        (modula-2-mode) ;; A simple mode with non-LF terminated comments.
        (buffer-enable-undo)
        (insert "foo\n\n")
        (let ((midbeg (point-marker))
              (_ (insert "midmid"))
              (midend (point-marker)))
          (insert "\n\nbar")
          (undo-boundary)
          (goto-char (+ midbeg 3))
          (insert "\n")
          (undo-boundary)
          (comment-region (point-min) midbeg)
          (undo-boundary)
          (comment-region midend (point-max))
          (undo-boundary)
          (progn
            (goto-char midbeg)
            (set-mark midend)
            (setq last-command 'something-else) ;Not `undo', so we start a new run.
            (undo '(4))
            (should (equal (buffer-substring midbeg midend) "midmid")))
          )))

- Stefan