Tk Source Code

View Ticket
Login
Ticket UUID: ab839efc5f783731a882c875cb7aa0a5cb690917
Title: text edit undo: clearing text instead of restoring
Type: Bug Version: 8.6.10 and on
Submitter: anonymous Created on: 2024-04-18 03:53:31
Subsystem: 18. [text] Assigned To: fvogel
Priority: 5 Medium Severity: Important
Status: Closed Last Modified: 2024-04-21 11:15:59
Resolution: Fixed Closed By: fvogel
    Closed on: 2024-04-21 11:15:59
Description:

Tk 8.6.10+, Linux and Windows.

Run the code snippet below, then

  • select 3rd line using Shift+ArrowUp
  • press Delete key => now 1-2 lines seen only
  • click Undo => now text restored
  • select 3rd line using Shift+ArrowUp
  • press Delete key => now 1-2 lines seen only
  • click Undo => text cleared

What??? The text has been cleared instead of restoring it.

The worst of all is that after this you cannot restore the original text using undo/redo actions incl. Ctrl+Z and Ctrl+Shift+Z.

The issue doesn't appear at:

  • using "event generate" of Undo procedure

or

  • using the mouse instead of Shift+ArrowUp keys to select lines

The latter is nearly mystical.


  puts "Tk v[package require Tk]"
  #_______________________

  proc Undo {} {
    catch {.txt edit undo}
#    catch {event generate .txt <<Undo>>}
    focus .txt
  }
  #_______________________

  pack [text .txt -height 10 -width 50 -undo 1]
  pack [ttk::button .but1 -text Undo -command ::Undo] -side left

  .txt insert 1.0 {1. 123 5 789012  LINE-1
2.
3. 123 5 789012  LINE-3
}
  focus .txt
  #_______________________

  update

User Comments: fvogel added on 2024-04-21 11:15:59:
Merged.

fvogel added on 2024-04-20 10:14:24:

Fixed, with a non-regression test added. See branch bug-ab839efc5f.

Will merge soon (to the legacy text widget only).


fvogel added on 2024-04-18 20:44:17:

Basically, the fix is:

Index: generic/tkText.c
==================================================================
--- generic/tkText.c
+++ generic/tkText.c
@@ -5270,15 +5270,17 @@
 	if (objc != 3) {
 	    Tcl_WrongNumArgs(interp, 3, objv, NULL);
 	    return TCL_ERROR;
 	}
 	canRedo = TkUndoCanRedo(textPtr->sharedTextPtr->undoStack);
+	TkUndoInsertUndoSeparator(textPtr->sharedTextPtr->undoStack);
 	if (TextEditUndo(textPtr)) {
 	    Tcl_SetObjResult(interp, Tcl_NewStringObj("nothing to undo", -1));
 	    Tcl_SetErrorCode(interp, "TK", "TEXT", "NO_UNDO", NULL);
 	    return TCL_ERROR;
 	}
+	TkUndoInsertUndoSeparator(textPtr->sharedTextPtr->undoStack);
 	canUndo = TkUndoCanUndo(textPtr->sharedTextPtr->undoStack);
 	if (!canRedo || !canUndo) {
 	    GenerateUndoStackEvent(textPtr);
 	}
 	break;

I believe [1669632] forgot to address this.


fvogel added on 2024-04-18 20:03:13:
Thanks for the report. I see what's happening and will fix this.

Note to myself: this bug does not affect revised_text.