From c97db25d011e7cef5dbb37cf3ce0a2a50bcbb5ca Mon Sep 17 00:00:00 2001 From: fvogel Date: Tue, 5 Apr 2016 20:28:54 +0000 Subject: Implementation of TIP #446 - Introspect Undo/Redo Stack Depths --- generic/tkText.c | 30 ++++++++++++++++++++++--- generic/tkUndo.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- generic/tkUndo.h | 3 ++- 3 files changed, 93 insertions(+), 7 deletions(-) diff --git a/generic/tkText.c b/generic/tkText.c index 506075d..c293557 100644 --- a/generic/tkText.c +++ b/generic/tkText.c @@ -2093,7 +2093,7 @@ ConfigureText( textPtr->sharedTextPtr->maxUndo = textPtr->maxUndo; textPtr->sharedTextPtr->autoSeparators = textPtr->autoSeparators; - TkUndoSetDepth(textPtr->sharedTextPtr->undoStack, + TkUndoSetMaxDepth(textPtr->sharedTextPtr->undoStack, textPtr->sharedTextPtr->maxUndo); /* @@ -5165,10 +5165,12 @@ TextEditCmd( { int index, setModified, oldModified; static const char *const editOptionStrings[] = { - "modified", "redo", "reset", "separator", "undo", NULL + "modified", "redo", "redodepth", "reset", "separator", "undo", + "undodepth", NULL }; enum editOptions { - EDIT_MODIFIED, EDIT_REDO, EDIT_RESET, EDIT_SEPARATOR, EDIT_UNDO + EDIT_MODIFIED, EDIT_REDO, EDIT_REDODEPTH, EDIT_RESET, + EDIT_SEPARATOR, EDIT_UNDO, EDIT_UNDODEPTH }; if (objc < 3) { @@ -5229,6 +5231,17 @@ TextEditCmd( return TCL_ERROR; } break; + case EDIT_REDODEPTH: { + int depth; + + if (objc != 3) { + Tcl_WrongNumArgs(interp, 3, objv, NULL); + return TCL_ERROR; + } + depth = TkUndoGetDepth(textPtr->sharedTextPtr->undoStack, 1); + Tcl_SetObjResult(interp, Tcl_NewIntObj(depth)); + break; + } case EDIT_RESET: if (objc != 3) { Tcl_WrongNumArgs(interp, 3, objv, NULL); @@ -5254,6 +5267,17 @@ TextEditCmd( return TCL_ERROR; } break; + case EDIT_UNDODEPTH: { + int depth; + + if (objc != 3) { + Tcl_WrongNumArgs(interp, 3, objv, NULL); + return TCL_ERROR; + } + depth = TkUndoGetDepth(textPtr->sharedTextPtr->undoStack, 0); + Tcl_SetObjResult(interp, Tcl_NewIntObj(depth)); + break; + } } return TCL_OK; } diff --git a/generic/tkUndo.c b/generic/tkUndo.c index 8359e0a..3aa3ee2 100644 --- a/generic/tkUndo.c +++ b/generic/tkUndo.c @@ -353,7 +353,7 @@ TkUndoInitStack( /* *---------------------------------------------------------------------- * - * TkUndoSetDepth -- + * TkUndoSetMaxDepth -- * * Set the maximum depth of stack. * @@ -368,7 +368,7 @@ TkUndoInitStack( */ void -TkUndoSetDepth( +TkUndoSetMaxDepth( TkUndoRedoStack *stack, /* An Undo/Redo stack */ int maxdepth) /* The maximum stack depth */ { @@ -428,6 +428,67 @@ TkUndoSetDepth( /* *---------------------------------------------------------------------- * + * TkUndoGetDepth + * + * Return the depth of the undo (or redo) stack. + * + * Results: + * An integer representing the number of undoable (or redoable) actions. + * + * Side effects: + * None. + * + *---------------------------------------------------------------------- + */ + +int +TkUndoGetDepth( + TkUndoRedoStack *stack, /* An Undo/Redo stack */ + int whichStack) /* 0 means the undo stack, + * otherwise the redo stack */ +{ + int depth = 0; + TkUndoAtom *elem = NULL; + + if (stack != NULL) { + if (whichStack) { + elem = stack->redoStack; + } else { + elem = stack->undoStack; + } + + if (elem != NULL) { + /* + * Skip the first (top) separator if there is one. + */ + + if (elem->type == TK_UNDO_SEPARATOR) { + elem = elem->next; + } + + /* + * The number of compound actions in the stack is then + * the number of separators plus one, except if there is + * a separator at the bottom of the stack. This latter + * case cannot however happen (TkUndoInsertSeparator + * prevents from inserting a separator there). + */ + + while (elem != NULL) { + if (elem->type == TK_UNDO_SEPARATOR) { + depth++; + } + elem = elem->next; + } + depth++; + } + } + return depth; +} + +/* + *---------------------------------------------------------------------- + * * TkUndoClearStacks -- * * Clear both the undo and redo stack. @@ -498,7 +559,7 @@ TkUndoInsertUndoSeparator( { if (TkUndoInsertSeparator(&stack->undoStack)) { stack->depth++; - TkUndoSetDepth(stack, stack->maxdepth); + TkUndoSetMaxDepth(stack, stack->maxdepth); } } diff --git a/generic/tkUndo.h b/generic/tkUndo.h index e63aac4..883f6ee 100644 --- a/generic/tkUndo.h +++ b/generic/tkUndo.h @@ -96,7 +96,8 @@ MODULE_SCOPE void TkUndoClearStack(TkUndoAtom **stack); */ MODULE_SCOPE TkUndoRedoStack *TkUndoInitStack(Tcl_Interp *interp, int maxdepth); -MODULE_SCOPE void TkUndoSetDepth(TkUndoRedoStack *stack, int maxdepth); +MODULE_SCOPE void TkUndoSetMaxDepth(TkUndoRedoStack *stack, int maxdepth); +MODULE_SCOPE int TkUndoGetDepth(TkUndoRedoStack *stack, int whichStack); MODULE_SCOPE void TkUndoClearStacks(TkUndoRedoStack *stack); MODULE_SCOPE void TkUndoFreeStack(TkUndoRedoStack *stack); MODULE_SCOPE void TkUndoInsertUndoSeparator(TkUndoRedoStack *stack); -- cgit v0.12 From 6b0ad3ecbb13963f417e8a56292c24679f23126e Mon Sep 17 00:00:00 2001 From: fvogel Date: Thu, 7 Apr 2016 19:32:29 +0000 Subject: [.t edit undodepth/redodepth] return 0 when -undo false --- generic/tkText.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/generic/tkText.c b/generic/tkText.c index c293557..3114835 100644 --- a/generic/tkText.c +++ b/generic/tkText.c @@ -5232,13 +5232,15 @@ TextEditCmd( } break; case EDIT_REDODEPTH: { - int depth; + int depth = 0; if (objc != 3) { Tcl_WrongNumArgs(interp, 3, objv, NULL); return TCL_ERROR; } - depth = TkUndoGetDepth(textPtr->sharedTextPtr->undoStack, 1); + if (textPtr->sharedTextPtr->undo) { + depth = TkUndoGetDepth(textPtr->sharedTextPtr->undoStack, 1); + } Tcl_SetObjResult(interp, Tcl_NewIntObj(depth)); break; } @@ -5268,13 +5270,15 @@ TextEditCmd( } break; case EDIT_UNDODEPTH: { - int depth; + int depth = 0; if (objc != 3) { Tcl_WrongNumArgs(interp, 3, objv, NULL); return TCL_ERROR; } - depth = TkUndoGetDepth(textPtr->sharedTextPtr->undoStack, 0); + if (textPtr->sharedTextPtr->undo) { + depth = TkUndoGetDepth(textPtr->sharedTextPtr->undoStack, 0); + } Tcl_SetObjResult(interp, Tcl_NewIntObj(depth)); break; } -- cgit v0.12 From cd651e6819b4cf35018f8bbb1114655f5eb80b0b Mon Sep 17 00:00:00 2001 From: fvogel Date: Thu, 7 Apr 2016 19:50:31 +0000 Subject: Documentation for [.t edit undodepth/redodepth] --- doc/text.n | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/doc/text.n b/doc/text.n index cf516f4..e38b4f1 100644 --- a/doc/text.n +++ b/doc/text.n @@ -1295,6 +1295,12 @@ When the \fB\-undo\fR option is true, reapplies the last undone edits provided no other edits were done since then. Generates an error when the redo stack is empty. Does nothing when the \fB\-undo\fR option is false. .TP +\fIpathName \fBedit redodepth\fR +. +Returns the depth of the redo stack (number of redoable actions). When this is +zero there is nothing to redo. When the \fB\-undo\fR option is false zero is +returned. +.TP \fIpathName \fBedit reset\fR . Clears the undo and redo stacks. @@ -1310,6 +1316,12 @@ Undoes the last edit action when the \fB\-undo\fR option is true. An edit action is defined as all the insert and delete commands that are recorded on the undo stack in between two separators. Generates an error when the undo stack is empty. Does nothing when the \fB\-undo\fR option is false. +.TP +\fIpathName \fBedit undodepth\fR +. +Returns the depth of the undo stack (number of undoable actions). When this is +zero there is nothing to undo. When the \fB\-undo\fR option is false zero is +returned. .RE .TP \fIpathName \fBget\fR ?\fB\-displaychars\fR? ?\fB\-\-\fR? \fIindex1\fR ?\fIindex2 ...\fR? -- cgit v0.12 From 16e7c0d35d096adcb40d6dded28593d6a907f581 Mon Sep 17 00:00:00 2001 From: fvogel Date: Thu, 7 Apr 2016 20:18:24 +0000 Subject: Tests for [.t edit undodepth/redodepth] --- tests/text.test | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/tests/text.test b/tests/text.test index a778b79..da246f9 100644 --- a/tests/text.test +++ b/tests/text.test @@ -6188,7 +6188,7 @@ test text-27.2 {TextEditCmd procedure, argument parsing} -body { .t edit gorp } -cleanup { destroy .t -} -returnCodes {error} -result {bad edit option "gorp": must be modified, redo, reset, separator, or undo} +} -returnCodes {error} -result {bad edit option "gorp": must be modified, redo, redodepth, reset, separator, undo, or undodepth} test text-27.3 {TextEditUndo procedure, undoing changes} -body { text .t -undo 1 pack .t @@ -6456,6 +6456,32 @@ test text-27.22 {patch 1669632 (v) - <> is atomic} -setup { } -cleanup { destroy .top.t .top } -result "This A an example text" +test text-27.24 {TextEditCmd procedure, undo and redo stack depths} -setup { + destroy .t + set res {} +} -body { + text .t -undo false -autoseparators false + lappend res [.t edit undodepth] [.t edit redodepth] + .t configure -undo true + lappend res [.t edit undodepth] [.t edit redodepth] + .t insert end "DO\n" + .t edit separator + .t insert end "IT\n" + .t insert end "YOURSELF\n" + .t edit separator + lappend res [.t edit undodepth] [.t edit redodepth] + .t edit undo + lappend res [.t edit undodepth] [.t edit redodepth] + .t configure -undo false + lappend res [.t edit undodepth] [.t edit redodepth] + .t configure -undo true + lappend res [.t edit undodepth] [.t edit redodepth] + .t edit redo + lappend res [.t edit undodepth] [.t edit redodepth] +} -cleanup { + destroy .t +} -result {0 0 0 0 2 0 1 1 0 0 1 1 2 0} + test text-28.1 {bug fix - 624372, ControlUtfProc long lines} -body { pack [text .t -wrap none] -- cgit v0.12