GNU bug report logs - #21523
25.0.50; Undo with active region adds extra text

Previous Next

Package: emacs;

Reported by: Drew Adams <drew.adams <at> oracle.com>

Date: Sun, 20 Sep 2015 07:42:01 UTC

Severity: minor

Tags: confirmed

Found in version 25.0.50

To reply to this bug, email your comments to 21523 AT debbugs.gnu.org.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#21523; Package emacs. (Sun, 20 Sep 2015 07:42:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Drew Adams <drew.adams <at> oracle.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Sun, 20 Sep 2015 07:42:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Drew Adams <drew.adams <at> oracle.com>
To: bug-gnu-emacs <at> gnu.org
Subject: 25.0.50; Undo with active region adds extra text
Date: Sun, 20 Sep 2015 00:34:06 -0700 (PDT)
The Subject line is not very clear.  Here is a recipe.  You decide
whether the behavior is correct or a bug.  FWIW, this behavior is at
least as old as Emacs 20 (with transient-mark-mode on).

emacs -Q

In buffer *scratch*, put point before "that" on the last line.
`M-c', to capitalize "That".
`C-SPC C-e', to select the text after "That", up to eol.
(The region does not contain the word "That".)

`C-_' to undo the last change within the region.
The word "that" is inserted, giving this:

;; then enter the text in Thatthat file=A1=AFs own buffer.

This seems disconcerting, at least.  (Same behavior for `M-u' etc.)

(I can see the explanation of why this happens coming as a reply.  As I
say, you can decide whether this behavior is OK as is.)


In GNU Emacs 25.0.50.1 (i686-pc-mingw32)
 of 2015-09-05
Bzr revision: 2330ca33a97867f2ea1123bcf7bfe5cfcc030b36
Windowing system distributor `Microsoft Corp.', version 6.1.7601
Configured using:
 `configure --host=3Di686-pc-mingw32 --enable-checking=3Dyes,glyphs'




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#21523; Package emacs. (Sun, 20 Sep 2015 15:15:03 GMT) Full text and rfc822 format available.

Message #8 received at 21523 <at> debbugs.gnu.org (full text, mbox):

From: Stefan Monnier <monnier <at> iro.umontreal.ca>
To: Drew Adams <drew.adams <at> oracle.com>
Cc: 21523 <at> debbugs.gnu.org
Subject: Re: bug#21523: 25.0.50; Undo with active region adds extra text
Date: Sun, 20 Sep 2015 11:14:29 -0400
> `C-_' to undo the last change within the region.
> The word "that" is inserted, giving this:
>   ;; then enter the text in Thatthat file's own buffer.
> This seems disconcerting, at least.  (Same behavior for `M-u' etc.)

Indeed, that's wrong.  I haven't looked in detail of why this happens,
but I can guess that it's because the previous change is represented as
"insert That" and "remove that", and when we undo them, the exact
location of "remove that" ends up right at the region boundary, making
it unclear whether it should be considered as "inside" or "outside".

Not sure how best to fix it.  Maybe such replacements should have
a special status in the undo-list, so they aren't considered as
two independent changes (remove+insert) but as a single one.

Or maybe the "undo-in-region" should only ever undo complete steps
(i.e. everything between two undo boundaries), so if any part of an undo
step affects text outside of the region, then the whole step is skipped.


        Stefan




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#21523; Package emacs. (Sun, 20 Sep 2015 18:32:02 GMT) Full text and rfc822 format available.

Message #11 received at 21523 <at> debbugs.gnu.org (full text, mbox):

From: Richard Stallman <rms <at> gnu.org>
To: Stefan Monnier <monnier <at> iro.umontreal.ca>
Cc: 21523 <at> debbugs.gnu.org, drew.adams <at> oracle.com
Subject: Re: bug#21523: 25.0.50; Undo with active region adds extra text
Date: Sun, 20 Sep 2015 14:26:43 -0400
[[[ To any NSA and FBI agents reading my email: please consider    ]]]
[[[ whether defending the US Constitution against all enemies,     ]]]
[[[ foreign or domestic, requires you to follow Snowden's example. ]]]

  > Or maybe the "undo-in-region" should only ever undo complete steps
  > (i.e. everything between two undo boundaries), so if any part of an undo
  > step affects text outside of the region, then the whole step is skipped.

The undo commands are supposed to undo a complete change
as one unit.  If in this case it fails to do that, that would seem
to be a bug, right?

-- 
Dr Richard Stallman
President, Free Software Foundation (gnu.org, fsf.org)
Internet Hall-of-Famer (internethalloffame.org)
Skype: No way! See stallman.org/skype.html.





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#21523; Package emacs. (Sun, 20 Sep 2015 19:44:02 GMT) Full text and rfc822 format available.

Message #14 received at 21523 <at> debbugs.gnu.org (full text, mbox):

From: Stefan Monnier <monnier <at> iro.umontreal.ca>
To: Richard Stallman <rms <at> gnu.org>
Cc: 21523 <at> debbugs.gnu.org, drew.adams <at> oracle.com
Subject: Re: bug#21523: 25.0.50; Undo with active region adds extra text
Date: Sun, 20 Sep 2015 15:43:14 -0400
>> Or maybe the "undo-in-region" should only ever undo complete steps
>> (i.e. everything between two undo boundaries), so if any part of an undo
>> step affects text outside of the region, then the whole step is skipped.

> The undo commands are supposed to undo a complete change
> as one unit.  If in this case it fails to do that, that would seem
> to be a bug, right?

Yes, that's what I was also trying to say.  I think that'd be the
better fix.


        Stefan




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#21523; Package emacs. (Mon, 16 Aug 2021 12:40:02 GMT) Full text and rfc822 format available.

Message #17 received at 21523 <at> debbugs.gnu.org (full text, mbox):

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Drew Adams <drew.adams <at> oracle.com>
Cc: 21523 <at> debbugs.gnu.org
Subject: Re: bug#21523: 25.0.50; Undo with active region adds extra text
Date: Mon, 16 Aug 2021 14:39:15 +0200
Drew Adams <drew.adams <at> oracle.com> writes:

> The Subject line is not very clear.  Here is a recipe.  You decide
> whether the behavior is correct or a bug.  FWIW, this behavior is at
> least as old as Emacs 20 (with transient-mark-mode on).
>
> emacs -Q
>
> In buffer *scratch*, put point before "that" on the last line.
> `M-c', to capitalize "That".
> `C-SPC C-e', to select the text after "That", up to eol.
> (The region does not contain the word "That".)
>
> `C-_' to undo the last change within the region.
> The word "that" is inserted, giving this:
>
> ;; then enter the text in Thatthat file=A1=AFs own buffer.

This bug is still present in Emacs 28.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no




Added tag(s) confirmed. Request was from Lars Ingebrigtsen <larsi <at> gnus.org> to control <at> debbugs.gnu.org. (Mon, 16 Aug 2021 12:40:03 GMT) Full text and rfc822 format available.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#21523; Package emacs. (Mon, 16 Aug 2021 13:14:01 GMT) Full text and rfc822 format available.

Message #22 received at 21523 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Lars Ingebrigtsen <larsi <at> gnus.org>
Cc: 21523 <at> debbugs.gnu.org, drew.adams <at> oracle.com
Subject: Re: bug#21523: 25.0.50; Undo with active region adds extra text
Date: Mon, 16 Aug 2021 16:13:26 +0300
> From: Lars Ingebrigtsen <larsi <at> gnus.org>
> Date: Mon, 16 Aug 2021 14:39:15 +0200
> Cc: 21523 <at> debbugs.gnu.org
> 
> Drew Adams <drew.adams <at> oracle.com> writes:
> 
> > The Subject line is not very clear.  Here is a recipe.  You decide
> > whether the behavior is correct or a bug.  FWIW, this behavior is at
> > least as old as Emacs 20 (with transient-mark-mode on).
> >
> > emacs -Q
> >
> > In buffer *scratch*, put point before "that" on the last line.
> > `M-c', to capitalize "That".
> > `C-SPC C-e', to select the text after "That", up to eol.
> > (The region does not contain the word "That".)
> >
> > `C-_' to undo the last change within the region.
> > The word "that" is inserted, giving this:
> >
> > ;; then enter the text in Thatthat file=A1=AFs own buffer.
> 
> This bug is still present in Emacs 28.

Why is it a bug?  What is the expected "non-buggy" behavior in this
case?





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#21523; Package emacs. (Mon, 16 Aug 2021 13:55:02 GMT) Full text and rfc822 format available.

Message #25 received at 21523 <at> debbugs.gnu.org (full text, mbox):

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 21523 <at> debbugs.gnu.org, drew.adams <at> oracle.com
Subject: Re: bug#21523: 25.0.50; Undo with active region adds extra text
Date: Mon, 16 Aug 2021 15:53:55 +0200
Eli Zaretskii <eliz <at> gnu.org> writes:

>> This bug is still present in Emacs 28.
>
> Why is it a bug?  What is the expected "non-buggy" behavior in this
> case?

I'd expect it to either do nothing (since we're undoing in a region, and
the changed text is outside the region), or change the "This" to
"this".  Leaving "Thisthis" in the buffer has to be a bug no matter how
you look at it.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#21523; Package emacs. (Mon, 16 Aug 2021 16:31:02 GMT) Full text and rfc822 format available.

Message #28 received at 21523 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Lars Ingebrigtsen <larsi <at> gnus.org>
Cc: 21523 <at> debbugs.gnu.org, drew.adams <at> oracle.com
Subject: Re: bug#21523: 25.0.50; Undo with active region adds extra text
Date: Mon, 16 Aug 2021 19:30:37 +0300
> From: Lars Ingebrigtsen <larsi <at> gnus.org>
> Cc: drew.adams <at> oracle.com,  21523 <at> debbugs.gnu.org
> Date: Mon, 16 Aug 2021 15:53:55 +0200
> 
> Eli Zaretskii <eliz <at> gnu.org> writes:
> 
> >> This bug is still present in Emacs 28.
> >
> > Why is it a bug?  What is the expected "non-buggy" behavior in this
> > case?
> 
> I'd expect it to either do nothing (since we're undoing in a region, and
> the changed text is outside the region), or change the "This" to
> "this".  Leaving "Thisthis" in the buffer has to be a bug no matter how
> you look at it.

Is signaling an error okay?  Because then it looks like an off-by-one
error somewhere -- the following slightly modified recipe works as
expected:

  emacs -Q
  C-u 23 M-g c ; go to "text"
  M-c
  C-f          ; the crucial difference!
  C-SPC
  C-e
  C-/
    => user-error: No further undo information for region

So the problem seems to be that in the original recipe the region
starts immediately after the end of the modified portion of text.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#21523; Package emacs. (Wed, 18 Aug 2021 13:32:02 GMT) Full text and rfc822 format available.

Message #31 received at 21523 <at> debbugs.gnu.org (full text, mbox):

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 21523 <at> debbugs.gnu.org, drew.adams <at> oracle.com
Subject: Re: bug#21523: 25.0.50; Undo with active region adds extra text
Date: Wed, 18 Aug 2021 15:31:36 +0200
Eli Zaretskii <eliz <at> gnu.org> writes:

> Is signaling an error okay?

Yup.

> Because then it looks like an off-by-one
> error somewhere -- the following slightly modified recipe works as
> expected:
>
>   emacs -Q
>   C-u 23 M-g c ; go to "text"
>   M-c
>   C-f          ; the crucial difference!
>   C-SPC
>   C-e
>   C-/
>     => user-error: No further undo information for region
>
> So the problem seems to be that in the original recipe the region
> starts immediately after the end of the modified portion of text.

Yup; I'm seeing the same thing, so this does indeed look like a
off-by-one error.  And...  it's in `undo-elt-in-region', I think, called
by (basically) (undo-make-selective-list (mark) (point)).

There's basically no test cases for this function, I think?

Actually, the problem seems to be in undo-adjust-elt, which was
rewritten in 2014 to fix bug#17235.

I've now added a test case (commented out), but I don't quite understand
the logic in undo-adjust-elt...  anybody see something obviously wrong?

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#21523; Package emacs. (Thu, 05 May 2022 14:38:02 GMT) Full text and rfc822 format available.

Message #34 received at 21523 <at> debbugs.gnu.org (full text, mbox):

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 21523 <at> debbugs.gnu.org, Barry O'Reilly <gundaetiapo <at> gmail.com>,
 drew.adams <at> oracle.com
Subject: Re: bug#21523: 25.0.50; Undo with active region adds extra text
Date: Thu, 05 May 2022 16:37:45 +0200
Lars Ingebrigtsen <larsi <at> gnus.org> writes:

> Actually, the problem seems to be in undo-adjust-elt, which was
> rewritten in 2014 to fix bug#17235.
>
> I've now added a test case (commented out), but I don't quite understand
> the logic in undo-adjust-elt...  anybody see something obviously wrong?

(defun undo-adjust-elt (elt deltas)

[...]

    ;; (TEXT . POSITION)
    (`(,(and text (pred stringp)) . ,(and pos (pred integerp)))
     (cons text (* (if (< pos 0) -1 1)
                   (undo-adjust-pos (abs pos) deltas))))

The problem seems to be here.  In my test case, this make the ("This"
. 1) entry into a ("This" . 5) entry, which is then included in the
region.  Using < instead if <= works for this particular test case, but
not for undo-test-region-eob.

I've added Barry to the CCs; perhaps he has some insights here.

For reference, this is the test case:

(ert-deftest test-undo-region ()
  (with-temp-buffer
    (insert "This is a test\n")
    (goto-char (point-min))
    (setq buffer-undo-list nil)
    (downcase-word 1)
    (should (= (length (delq nil (undo-make-selective-list 1 9))) 2))
    ;; FIXME: These should give 0, but currently give 1.
    ;;(should (= (length (delq nil (undo-make-selective-list 4 9))) 0))
    ;;(should (= (length (delq nil (undo-make-selective-list 5 9))) 0))
    (should (= (length (delq nil (undo-make-selective-list 6 9))) 0))))


-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no




Added tag(s) moreinfo. Request was from Lars Ingebrigtsen <larsi <at> gnus.org> to control <at> debbugs.gnu.org. (Tue, 07 Jun 2022 14:13:02 GMT) Full text and rfc822 format available.

Removed tag(s) moreinfo. Request was from Lars Ingebrigtsen <larsi <at> gnus.org> to control <at> debbugs.gnu.org. (Mon, 11 Jul 2022 13:14:02 GMT) Full text and rfc822 format available.

This bug report was last modified 2 years and 338 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.