Subject: bug#16411: undo-only bugs

From: Barry OReilly <[email-masked]>
Date: Tue, 18 Feb 2014 12:40:38 -0500

>   ;; (BEGIN . END)
>   (and (>= (car undo-elt) start)
>        (<= (cdr undo-elt) end))
>         ^^
>
>   ;; (TEXT . POSITION)
>   (and (>= (abs (cdr undo-elt)) start)
>        (< (abs (cdr undo-elt)) end))
>         ^

Using 'git log -L' on these two code fragments, I found the (BEGIN .
END) case changed in this commit to "Fix one-off error at END".

commit 9debee7a5e6ebc0c32141619248e7390f1476946
Author: Richard M. Stallman <[email-masked]>
Date:   Mon Sep 9 00:27:30 2002 +0000

    (undo-elt-in-region): Fix one-off error at END.
    (forward-visible-line): Handle invisibility by ignoring
    invisible newlines.  Also include entire invisible lines beyond
    the stopping point.

diff --git a/lisp/simple.el b/lisp/simple.el
index bfef653..d9ae114 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -1089,7 +1089,7 @@ we stop and ignore all further elements."
 If it crosses the edge, we return nil."
   (cond ((integerp undo-elt)
         (and (>= undo-elt start)
-             (<  undo-elt end)))
+             (<= undo-elt end)))
        ((eq undo-elt nil)
         t)
        ((atom undo-elt)
@@ -1109,16 +1109,16 @@ If it crosses the edge, we return nil."
                   (cons alist-elt undo-adjusted-markers)))
           (and (cdr alist-elt)
                (>= (cdr alist-elt) start)
-               (< (cdr alist-elt) end))))
+               (<= (cdr alist-elt) end))))
        ((null (car undo-elt))
         ;; (nil PROPERTY VALUE BEG . END)
         (let ((tail (nthcdr 3 undo-elt)))
           (and (>= (car tail) start)
-               (< (cdr tail) end))))
+               (<= (cdr tail) end))))
        ((integerp (car undo-elt))
         ;; (BEGIN . END)
         (and (>= (car undo-elt) start)
-             (< (cdr undo-elt) end)))))
+             (<= (cdr undo-elt) end)))))

 (defun undo-elt-crosses-region (undo-elt start end)
   "Test whether UNDO-ELT crosses one edge of that region START ... END.

This didn't change the (TEXT . POSITION) case, which hasn't changed
since 1998 when it was first introduced. Maybe it was forgotten in the
above 2002 change?

Anyway, I made this change locally:

diff --git a/lisp/simple.el b/lisp/simple.el
index b90382d..01d4f19 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -2434,7 +2434,7 @@ If it crosses the edge, we return nil."
        ((stringp (car undo-elt))
         ;; (TEXT . POSITION)
         (and (>= (abs (cdr undo-elt)) start)
-             (< (abs (cdr undo-elt)) end)))
+             (<= (abs (cdr undo-elt)) end)))
        ((and (consp undo-elt) (markerp (car undo-elt)))
         ;; This is a marker-adjustment element (MARKER . ADJUSTMENT).
         ;; See if MARKER is inside the region.

and I didn't witness the incorrect behavior from undo in region.

Another justification for this change, in addition to fixing the
buffer corruption my recipe demonstrates, is that without it, a
deletion at the EOB is inaccessible via undo in region.