Subject: Took comments from Koen Danckaert into account (with my thanks) to simplify the implementation of TkUndoCanUndo() and TkUndoCanRedo() in tkUndo.c, and in tkText.c to remove direct calls to internals of the undo or redo stack

From: fvogel <[email-masked]>
Date: Tue, 17 May 2016 20:00:20 +0000

---
 generic/tkText.c | 27 +++++++++++++--------------
 generic/tkUndo.c | 24 ++----------------------
 2 files changed, 15 insertions(+), 36 deletions(-)

diff --git a/generic/tkText.c b/generic/tkText.c
index a9b7527..b610844 100644
--- a/generic/tkText.c
+++ b/generic/tkText.c
@@ -2770,7 +2770,7 @@ TextPushUndoAction(
 				/* Index describing second location. */
 {
     TkUndoSubAtom *iAtom, *dAtom;
-    TkUndoAtom *redoElem, *undoElem;
+    int canUndo, canRedo;
 
     /*
      * Create the helpers.
@@ -2857,8 +2857,8 @@ TextPushUndoAction(
     Tcl_DecrRefCount(index1Obj);
     Tcl_DecrRefCount(index2Obj);
 
-    undoElem = textPtr->sharedTextPtr->undoStack->undoStack;
-    redoElem = textPtr->sharedTextPtr->undoStack->redoStack;
+    canUndo = TkUndoCanUndo(textPtr->sharedTextPtr->undoStack);
+    canRedo = TkUndoCanRedo(textPtr->sharedTextPtr->undoStack);
 
     /*
      * Depending whether the action is to insert or delete, we provide the
@@ -2872,7 +2872,7 @@ TextPushUndoAction(
 	TkUndoPushAction(textPtr->sharedTextPtr->undoStack, dAtom, iAtom);
     }
 
-    if (undoElem == NULL || redoElem != NULL) {
+    if (!canUndo || canRedo) {
         GenerateUndoStackEvent(textPtr);
     }
 }
@@ -5166,7 +5166,6 @@ TextEditCmd(
     int index, setModified, oldModified;
     int canRedo = 0;
     int canUndo = 0;
-    TkUndoAtom *redoElem, *undoElem;
 
     static const char *const editOptionStrings[] = {
 	"canundo", "canredo", "modified", "redo", "reset", "separator",
@@ -5249,14 +5248,14 @@ TextEditCmd(
 	    Tcl_WrongNumArgs(interp, 3, objv, NULL);
 	    return TCL_ERROR;
 	}
-	undoElem = textPtr->sharedTextPtr->undoStack->undoStack;
+	canUndo = TkUndoCanUndo(textPtr->sharedTextPtr->undoStack);
         if (TextEditRedo(textPtr)) {
 	    Tcl_SetObjResult(interp, Tcl_NewStringObj("nothing to redo", -1));
 	    Tcl_SetErrorCode(interp, "TK", "TEXT", "NO_REDO", NULL);
 	    return TCL_ERROR;
 	}
-        redoElem = textPtr->sharedTextPtr->undoStack->redoStack;
-        if (undoElem == NULL || redoElem == NULL) {
+        canRedo = TkUndoCanRedo(textPtr->sharedTextPtr->undoStack);
+        if (!canUndo || !canRedo) {
             GenerateUndoStackEvent(textPtr);
         }
 	break;
@@ -5265,10 +5264,10 @@ TextEditCmd(
 	    Tcl_WrongNumArgs(interp, 3, objv, NULL);
 	    return TCL_ERROR;
 	}
-        undoElem = textPtr->sharedTextPtr->undoStack->undoStack;
-        redoElem = textPtr->sharedTextPtr->undoStack->redoStack;
+        canUndo = TkUndoCanUndo(textPtr->sharedTextPtr->undoStack);
+        canRedo = TkUndoCanRedo(textPtr->sharedTextPtr->undoStack);
 	TkUndoClearStacks(textPtr->sharedTextPtr->undoStack);
-        if (undoElem != NULL || redoElem != NULL) {
+        if (canUndo || canRedo) {
             GenerateUndoStackEvent(textPtr);
         }
 	break;
@@ -5284,14 +5283,14 @@ TextEditCmd(
 	    Tcl_WrongNumArgs(interp, 3, objv, NULL);
 	    return TCL_ERROR;
 	}
-        redoElem = textPtr->sharedTextPtr->undoStack->redoStack;
+        canRedo = TkUndoCanRedo(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;
 	}
-        undoElem = textPtr->sharedTextPtr->undoStack->undoStack;
-        if (redoElem == NULL || undoElem == NULL) {
+        canUndo = TkUndoCanUndo(textPtr->sharedTextPtr->undoStack);
+        if (!canRedo || !canUndo) {
             GenerateUndoStackEvent(textPtr);
         }
 	break;
diff --git a/generic/tkUndo.c b/generic/tkUndo.c
index 5934154..c66905d 100644
--- a/generic/tkUndo.c
+++ b/generic/tkUndo.c
@@ -495,17 +495,7 @@ int
 TkUndoCanRedo(
     TkUndoRedoStack *stack)	/* An Undo/Redo stack */
 {
-    int canRedo = 0;
-    TkUndoAtom *elem = stack->redoStack;
-
-    while (elem != NULL) {
-        if (elem->type != TK_UNDO_SEPARATOR) {
-            canRedo = 1;
-            break;
-        }
-        elem = elem->next;
-    }
-    return canRedo;
+    return stack->redoStack != NULL;
 }
 
 /*
@@ -528,17 +518,7 @@ int
 TkUndoCanUndo(
     TkUndoRedoStack *stack)	/* An Undo/Redo stack */
 {
-    int canUndo = 0;
-    TkUndoAtom *elem = stack->undoStack;
-
-    while (elem != NULL) {
-        if (elem->type != TK_UNDO_SEPARATOR) {
-            canUndo = 1;
-            break;
-        }
-        elem = elem->next;
-    }
-    return canUndo;
+    return stack->undoStack != NULL;
 }
 
 /*
-- 
cgit v0.12