diff options
author | dkf <donal.k.fellows@manchester.ac.uk> | 2005-07-29 13:35:07 (GMT) |
---|---|---|
committer | dkf <donal.k.fellows@manchester.ac.uk> | 2005-07-29 13:35:07 (GMT) |
commit | 70e025eadba7672d7d0e171e84723aa16f244be1 (patch) | |
tree | b463c2ec7ba433bcf086a1255ec7b02fb774ebec | |
parent | 88c441da5934cca7cd566d26c8e93d866d6aef86 (diff) | |
download | tk-70e025eadba7672d7d0e171e84723aa16f244be1.zip tk-70e025eadba7672d7d0e171e84723aa16f244be1.tar.gz tk-70e025eadba7672d7d0e171e84723aa16f244be1.tar.bz2 |
Getting more systematic about style
-rw-r--r-- | generic/tkTextBTree.c | 2149 |
1 files changed, 1075 insertions, 1074 deletions
diff --git a/generic/tkTextBTree.c b/generic/tkTextBTree.c index 1919849..d77197f 100644 --- a/generic/tkTextBTree.c +++ b/generic/tkTextBTree.c @@ -1,17 +1,17 @@ -/* +/* * tkTextBTree.c -- * - * This file contains code that manages the B-tree representation - * of text for Tk's text widget and implements character and - * toggle segment types. - * + * This file contains code that manages the B-tree representation of text + * for Tk's text widget and implements character and toggle segment + * types. + * * Copyright (c) 1992-1994 The Regents of the University of California. * Copyright (c) 1994-1995 Sun Microsystems, Inc. * - * See the file "license.terms" for information on usage and redistribution - * of this file, and for a DISCLAIMER OF ALL WARRANTIES. + * See the file "license.terms" for information on usage and redistribution of + * this file, and for a DISCLAIMER OF ALL WARRANTIES. * - * RCS: @(#) $Id: tkTextBTree.c,v 1.16 2005/02/14 23:00:44 vincentdarley Exp $ + * RCS: @(#) $Id: tkTextBTree.c,v 1.17 2005/07/29 13:35:07 dkf Exp $ */ #include "tkInt.h" @@ -20,37 +20,36 @@ /* * Implementation notes: - * - * Most of this file is independent of the text widget implementation - * and representation now. Without much effort this could be developed - * further into a new Tcl object type of which the Tk text widget is one - * example of a client. - * + * + * Most of this file is independent of the text widget implementation and + * representation now. Without much effort this could be developed further + * into a new Tcl object type of which the Tk text widget is one example of a + * client. + * * The B-tree is set up with a dummy last line of text which must not be - * displayed, and must _never_ have a non-zero pixel count. This dummy - * line is a historical convenience to avoid other code having to deal - * with NULL TkTextLines. Since Tk 8.5, with pixel line height - * calculations and peer widgets, this dummy line is becoming somewhat - * of a liability, and special case code has been required to deal with - * it. It is probably a good idea to investigate removing the dummy - * line completely. This could result in an overall simplification - * (although it would require new special case code to deal with the - * fact that '.text index end' would then not really point to a valid - * line, rather it would point to the beginning of a non-existent line + * displayed, and must _never_ have a non-zero pixel count. This dummy line is + * a historical convenience to avoid other code having to deal with NULL + * TkTextLines. Since Tk 8.5, with pixel line height calculations and peer + * widgets, this dummy line is becoming somewhat of a liability, and special + * case code has been required to deal with it. It is probably a good idea to + * investigate removing the dummy line completely. This could result in an + * overall simplification (although it would require new special case code to + * deal with the fact that '.text index end' would then not really point to a + * valid line, rather it would point to the beginning of a non-existent line * one beyond all current lines - we could perhaps define that as a * TkTextIndex with a NULL TkTextLine ptr). */ /* - * The data structure below keeps summary information about one tag as part - * of the tag information in a node. + * The data structure below keeps summary information about one tag as part of + * the tag information in a node. */ typedef struct Summary { TkTextTag *tagPtr; /* Handle for tag. */ - int toggleCount; /* Number of transitions into or - * out of this tag that occur in - * the subtree rooted at this node. */ + int toggleCount; /* Number of transitions into or out + * of this tag that occur in the + * subtree rooted at this node. */ struct Summary *nextPtr; /* Next in list of all tags for same * node, or NULL if at end of list. */ } Summary; @@ -78,44 +77,43 @@ typedef struct Node { int numChildren; /* Number of children of this node. */ int numLines; /* Total number of lines (leaves) in * the subtree rooted here. */ - int* numPixels; /* Array containing total number - * of vertical display pixels in - * the subtree rooted here, one - * entry for each peer widget. */ + int* numPixels; /* Array containing total number of + * vertical display pixels in the + * subtree rooted here, one entry for + * each peer widget. */ } Node; -/* - * Used to avoid having to allocate and deallocate arrays on the - * fly for commonly used procedures. Must be > 0. +/* + * Used to avoid having to allocate and deallocate arrays on the fly for + * commonly used functions. Must be > 0. */ #define PIXEL_CLIENTS 5 /* - * Upper and lower bounds on how many children a node may have: - * rebalance when either of these limits is exceeded. MAX_CHILDREN - * should be twice MIN_CHILDREN and MIN_CHILDREN must be >= 2. + * Upper and lower bounds on how many children a node may have: rebalance when + * either of these limits is exceeded. MAX_CHILDREN should be twice + * MIN_CHILDREN and MIN_CHILDREN must be >= 2. */ #define MAX_CHILDREN 12 #define MIN_CHILDREN 6 /* - * The data structure below defines an entire B-tree. Since text - * widgets are the only current B-tree clients, 'clients' and - * 'pixelReferences' are identical. + * The data structure below defines an entire B-tree. Since text widgets are + * the only current B-tree clients, 'clients' and 'pixelReferences' are + * identical. */ typedef struct BTree { Node *rootPtr; /* Pointer to root of B-tree. */ - int clients; /* Number of clients of this - * B-tree */ - int pixelReferences; /* Number of clients of this - * B-tree which care about - * pixel heights */ - TkSharedText *sharedTextPtr; /* Used to find tagTable in consistency - * checking code, and to access - * list of all B-tree clients */ + int clients; /* Number of clients of this B-tree */ + int pixelReferences; /* Number of clients of this B-tree + * which care about pixel heights. */ + TkSharedText *sharedTextPtr; /* Used to find tagTable in + * consistency checking code, and to + * access list of all B-tree + * clients. */ int startEndCount; TkTextLine **startEnd; TkText **startEndRef; @@ -127,15 +125,15 @@ typedef struct BTree { */ typedef struct TagInfo { - int numTags; /* Number of tags for which there - * is currently information in - * tags and counts. */ + int numTags; /* Number of tags for which there is + * currently information in tags and + * counts. */ int arraySize; /* Number of entries allocated for * tags and counts. */ TkTextTag **tagPtrs; /* Array of tags seen so far. * Malloc-ed. */ int *counts; /* Toggle count (so far) for each - * entry in tags. Malloc-ed. */ + * entry in tags. Malloc-ed. */ } TagInfo; /* @@ -155,12 +153,12 @@ int tkBTreeDebug = 0; + sizeof(TkTextToggle))) /* - * Forward declarations for procedures defined in this file: + * Forward declarations for functions defined in this file: */ -static int AdjustPixelClient _ANSI_ARGS_((BTree *treePtr, +static int AdjustPixelClient _ANSI_ARGS_((BTree *treePtr, int defaultHeight, Node *nodePtr, TkTextLine *start, - TkTextLine *end, int useReference, + TkTextLine *end, int useReference, int newPixelReferences, int *counting)); static void ChangeNodeToggleCount _ANSI_ARGS_((Node *nodePtr, TkTextTag *tagPtr, int delta)); @@ -173,18 +171,18 @@ static TkTextSegment * CharCleanupProc _ANSI_ARGS_((TkTextSegment *segPtr, static TkTextSegment * CharSplitProc _ANSI_ARGS_((TkTextSegment *segPtr, int index)); static void CheckNodeConsistency _ANSI_ARGS_((Node *nodePtr, - int references)); + int references)); static void CleanupLine _ANSI_ARGS_((TkTextLine *linePtr)); static void DeleteSummaries _ANSI_ARGS_((Summary *tagPtr)); static void DestroyNode _ANSI_ARGS_((Node *nodePtr)); -static TkTextSegment * FindTagEnd _ANSI_ARGS_((TkTextBTree tree, +static TkTextSegment * FindTagEnd _ANSI_ARGS_((TkTextBTree tree, TkTextTag *tagPtr, TkTextIndex *indexPtr)); static void IncCount _ANSI_ARGS_((TkTextTag *tagPtr, int inc, TagInfo *tagInfoPtr)); static void Rebalance _ANSI_ARGS_((BTree *treePtr, Node *nodePtr)); -static void RecomputeNodeCounts _ANSI_ARGS_((BTree *treePtr, - Node *nodePtr)); -static void RemovePixelClient _ANSI_ARGS_((BTree *treePtr, +static void RecomputeNodeCounts _ANSI_ARGS_((BTree *treePtr, + Node *nodePtr)); +static void RemovePixelClient _ANSI_ARGS_((BTree *treePtr, Node *nodePtr, int overwriteWithLast)); static TkTextSegment * SplitSeg _ANSI_ARGS_((TkTextIndex *indexPtr)); static void ToggleCheckProc _ANSI_ARGS_((TkTextSegment *segPtr, @@ -197,60 +195,59 @@ static void ToggleLineChangeProc _ANSI_ARGS_((TkTextSegment *segPtr, TkTextLine *linePtr)); static TkTextSegment * FindTagStart _ANSI_ARGS_((TkTextBTree tree, TkTextTag *tagPtr, TkTextIndex *indexPtr)); -static void AdjustStartEndRefs _ANSI_ARGS_((BTree *treePtr, +static void AdjustStartEndRefs _ANSI_ARGS_((BTree *treePtr, TkText *textPtr, int action)); -/* +/* * Actions for use by AdjustStartEndRefs */ -#define TEXT_ADD_REFS 1 -#define TEXT_REMOVE_REFS 2 + +#define TEXT_ADD_REFS 1 +#define TEXT_REMOVE_REFS 2 /* * Type record for character segments: */ Tk_SegType tkTextCharType = { - "character", /* name */ - 0, /* leftGravity */ - CharSplitProc, /* splitProc */ - CharDeleteProc, /* deleteProc */ - CharCleanupProc, /* cleanupProc */ - (Tk_SegLineChangeProc *) NULL, /* lineChangeProc */ - TkTextCharLayoutProc, /* layoutProc */ - CharCheckProc /* checkProc */ + "character", /* name */ + 0, /* leftGravity */ + CharSplitProc, /* splitProc */ + CharDeleteProc, /* deleteProc */ + CharCleanupProc, /* cleanupProc */ + NULL, /* lineChangeProc */ + TkTextCharLayoutProc, /* layoutProc */ + CharCheckProc /* checkProc */ }; /* - * Type record for segments marking the beginning of a tagged - * range: + * Type record for segments marking the beginning of a tagged range: */ Tk_SegType tkTextToggleOnType = { - "toggleOn", /* name */ - 0, /* leftGravity */ - (Tk_SegSplitProc *) NULL, /* splitProc */ - ToggleDeleteProc, /* deleteProc */ - ToggleCleanupProc, /* cleanupProc */ - ToggleLineChangeProc, /* lineChangeProc */ - (Tk_SegLayoutProc *) NULL, /* layoutProc */ - ToggleCheckProc /* checkProc */ + "toggleOn", /* name */ + 0, /* leftGravity */ + NULL, /* splitProc */ + ToggleDeleteProc, /* deleteProc */ + ToggleCleanupProc, /* cleanupProc */ + ToggleLineChangeProc, /* lineChangeProc */ + NULL, /* layoutProc */ + ToggleCheckProc /* checkProc */ }; /* - * Type record for segments marking the end of a tagged - * range: + * Type record for segments marking the end of a tagged range: */ Tk_SegType tkTextToggleOffType = { - "toggleOff", /* name */ - 1, /* leftGravity */ - (Tk_SegSplitProc *) NULL, /* splitProc */ - ToggleDeleteProc, /* deleteProc */ - ToggleCleanupProc, /* cleanupProc */ - ToggleLineChangeProc, /* lineChangeProc */ - (Tk_SegLayoutProc *) NULL, /* layoutProc */ - ToggleCheckProc /* checkProc */ + "toggleOff", /* name */ + 1, /* leftGravity */ + NULL, /* splitProc */ + ToggleDeleteProc, /* deleteProc */ + ToggleCleanupProc, /* cleanupProc */ + ToggleLineChangeProc, /* lineChangeProc */ + NULL, /* layoutProc */ + ToggleCheckProc /* checkProc */ }; /* @@ -258,11 +255,11 @@ Tk_SegType tkTextToggleOffType = { * * TkBTreeCreate -- * - * This procedure is called to create a new text B-tree. + * This function is called to create a new text B-tree. * * Results: - * The return value is a pointer to a new B-tree containing - * one line with nothing but a newline character. + * The return value is a pointer to a new B-tree containing one line with + * nothing but a newline character. * * Side effects: * Memory is allocated and initialized. @@ -278,18 +275,18 @@ TkBTreeCreate(sharedTextPtr) register Node *rootPtr; register TkTextLine *linePtr, *linePtr2; register TkTextSegment *segPtr; - + /* - * The tree will initially have two empty lines. The second line - * isn't actually part of the tree's contents, but its presence - * makes several operations easier. The tree will have one node, - * which is also the root of the tree. + * The tree will initially have two empty lines. The second line isn't + * actually part of the tree's contents, but its presence makes several + * operations easier. The tree will have one node, which is also the root + * of the tree. */ rootPtr = (Node *) ckalloc(sizeof(Node)); linePtr = (TkTextLine *) ckalloc(sizeof(TkTextLine)); linePtr2 = (TkTextLine *) ckalloc(sizeof(TkTextLine)); - + rootPtr->parentPtr = NULL; rootPtr->nextPtr = NULL; rootPtr->summaryPtr = NULL; @@ -297,14 +294,16 @@ TkBTreeCreate(sharedTextPtr) rootPtr->children.linePtr = linePtr; rootPtr->numChildren = 2; rootPtr->numLines = 2; - /* - * The tree currently has no registered clients, so all pixel - * count pointers are simply NULL + + /* + * The tree currently has no registered clients, so all pixel count + * pointers are simply NULL */ + rootPtr->numPixels = NULL; linePtr->pixels = NULL; linePtr2->pixels = NULL; - + linePtr->parentPtr = rootPtr; linePtr->nextPtr = linePtr2; segPtr = (TkTextSegment *) ckalloc(CSEG_SIZE(1)); @@ -314,7 +313,7 @@ TkBTreeCreate(sharedTextPtr) segPtr->size = 1; segPtr->body.chars[0] = '\n'; segPtr->body.chars[1] = 0; - + linePtr2->parentPtr = rootPtr; linePtr2->nextPtr = NULL; segPtr = (TkTextSegment *) ckalloc(CSEG_SIZE(1)); @@ -333,7 +332,7 @@ TkBTreeCreate(sharedTextPtr) treePtr->startEndCount = 0; treePtr->startEnd = NULL; treePtr->startEndRef = NULL; - + return (TkTextBTree) treePtr; } @@ -342,28 +341,26 @@ TkBTreeCreate(sharedTextPtr) * * TkBTreeAddClient -- * - * This procedure is called to provide a client with access to - * a given B-tree. If the client wishes to make use of the - * B-tree's pixel height storage, caching and calculation - * mechanisms, then a non-negative 'defaultHeight' must be - * provided. In this case the return value is a pixel tree - * reference which must be provided in all of the B-tree API which - * refers to or modifies pixel heights: - * - * TkBTreeAdjustPixelHeight, + * This function is called to provide a client with access to a given + * B-tree. If the client wishes to make use of the B-tree's pixel height + * storage, caching and calculation mechanisms, then a non-negative + * 'defaultHeight' must be provided. In this case the return value is a + * pixel tree reference which must be provided in all of the B-tree API + * which refers to or modifies pixel heights: + * + * TkBTreeAdjustPixelHeight, * TkBTreeFindPixelLine, - * TkBTreeNumPixels, + * TkBTreeNumPixels, * TkBTreePixelsTo, * (and two private functions AdjustPixelClient, RemovePixelClient). - * - * If this is not provided, then the above functions must - * never be called for this client. + * + * If this is not provided, then the above functions must never be called + * for this client. * * Results: - * The return value is the pixelReference used by the B-tree to - * refer to pixel counts for the new client. It should be stored - * by the caller. If defaultHeight was negative, then the return - * value will be -1. + * The return value is the pixelReference used by the B-tree to refer to + * pixel counts for the new client. It should be stored by the caller. + * If defaultHeight was negative, then the return value will be -1. * * Side effects: * Memory may be allocated and initialized. @@ -373,14 +370,13 @@ TkBTreeCreate(sharedTextPtr) void TkBTreeAddClient(tree, textPtr, defaultHeight) - TkTextBTree tree; /* B-tree to add a client to */ - TkText *textPtr; /* Client to add */ - int defaultHeight; /* Default line height for the new - * client, or -1 if no pixel - * heights are to be kept. */ + TkTextBTree tree; /* B-tree to add a client to */ + TkText *textPtr; /* Client to add */ + int defaultHeight; /* Default line height for the new client, or + * -1 if no pixel heights are to be kept. */ { register BTree *treePtr = (BTree*) tree; - + if (treePtr == NULL) { Tcl_Panic("NULL treePtr in TkBTreeAddClient"); } @@ -393,19 +389,19 @@ TkBTreeAddClient(tree, textPtr, defaultHeight) TkTextLine *end; int counting = (textPtr->start == NULL ? 1 : 0); int useReference = treePtr->pixelReferences; - /* - * We must set the 'end' value in AdjustPixelClient so that - * the last dummy line in the B-tree doesn't contain - * a pixel height. + + /* + * We must set the 'end' value in AdjustPixelClient so that the last + * dummy line in the B-tree doesn't contain a pixel height. */ + end = textPtr->end; if (end == NULL) { end = TkBTreeFindLine(tree, NULL, TkBTreeNumLines(tree, NULL)); } - AdjustPixelClient(treePtr, defaultHeight, treePtr->rootPtr, - textPtr->start, end, useReference, - 1 + useReference, &counting); - + AdjustPixelClient(treePtr, defaultHeight, treePtr->rootPtr, + textPtr->start, end, useReference, useReference+1, &counting); + textPtr->pixelReference = useReference; treePtr->pixelReferences++; } else { @@ -419,48 +415,46 @@ TkBTreeAddClient(tree, textPtr, defaultHeight) * * TkBTreeClientRangeChanged -- * - * Called when the -startline or -endline options of a text - * widget client of the B-tree have changed. + * Called when the -startline or -endline options of a text widget client + * of the B-tree have changed. * * Results: * None. * * Side effects: - * Lots of processing of the B-tree is done, with potential - * for memory to be allocated and initialized for the pixel - * heights of the widget. + * Lots of processing of the B-tree is done, with potential for memory to + * be allocated and initialized for the pixel heights of the widget. * *---------------------------------------------------------------------- */ void TkBTreeClientRangeChanged(textPtr, defaultHeight) - TkText *textPtr; /* Client whose start, end have - * changed. */ - int defaultHeight; /* Default line height for the new - * client, or -1 if no pixel - * heights are to be kept. */ + TkText *textPtr; /* Client whose start, end have changed. */ + int defaultHeight; /* Default line height for the new client, or + * -1 if no pixel heights are to be kept. */ { TkTextLine *end; BTree *treePtr = (BTree*) textPtr->sharedTextPtr->tree; - + int counting = (textPtr->start == NULL ? 1 : 0); int useReference = textPtr->pixelReference; AdjustStartEndRefs(treePtr, textPtr, TEXT_ADD_REFS | TEXT_REMOVE_REFS); - /* - * We must set the 'end' value in AdjustPixelClient so that - * the last dummy line in the B-tree doesn't contain - * a pixel height. + + /* + * We must set the 'end' value in AdjustPixelClient so that the last dummy + * line in the B-tree doesn't contain a pixel height. */ + end = textPtr->end; if (end == NULL) { - end = TkBTreeFindLine(textPtr->sharedTextPtr->tree, + end = TkBTreeFindLine(textPtr->sharedTextPtr->tree, NULL, TkBTreeNumLines(textPtr->sharedTextPtr->tree, NULL)); } AdjustPixelClient(treePtr, defaultHeight, treePtr->rootPtr, - textPtr->start, end, useReference, - treePtr->pixelReferences, &counting); + textPtr->start, end, useReference, treePtr->pixelReferences, + &counting); } /* @@ -471,8 +465,7 @@ TkBTreeClientRangeChanged(textPtr, defaultHeight) * Delete a B-tree, recycling all of the storage it contains. * * Results: - * The tree is deleted, so 'tree' should never - * again be used. + * The tree is deleted, so 'tree' should never again be used. * * Side effects: * Memory is freed. @@ -482,19 +475,19 @@ TkBTreeClientRangeChanged(textPtr, defaultHeight) void TkBTreeDestroy(tree) - TkTextBTree tree; /* Tree to clean up */ + TkTextBTree tree; /* Tree to clean up */ { BTree *treePtr = (BTree *) tree; - /* + /* * There's no need to loop over each client of the tree, calling - * 'TkBTreeRemoveClient', since the 'DestroyNode' will clean - * everything up itself. + * 'TkBTreeRemoveClient', since the 'DestroyNode' will clean everything up + * itself. */ - + DestroyNode(treePtr->rootPtr); if (treePtr->startEnd != NULL) { - ckfree((char *) treePtr->startEnd); + ckfree((char *) treePtr->startEnd); ckfree((char *) treePtr->startEndRef); } ckfree((char *) treePtr); @@ -505,14 +498,14 @@ TkBTreeDestroy(tree) * * TkBTreeRemoveClient -- * - * Remove a client widget from its B-tree, cleaning up the pixel - * arrays which it uses if necessary. If this is the last such - * widget, we also destroy the whole tree. + * Remove a client widget from its B-tree, cleaning up the pixel arrays + * which it uses if necessary. If this is the last such widget, we also + * destroy the whole tree. * * Results: - * All tree-specific aspects of the given client are deleted. - * If no more references exist, then the given tree is also - * deleted (in which case 'tree' must not be used again). + * All tree-specific aspects of the given client are deleted. If no more + * references exist, then the given tree is also deleted (in which case + * 'tree' must not be used again). * * Side effects: * Memory may be freed. @@ -522,45 +515,55 @@ TkBTreeDestroy(tree) void TkBTreeRemoveClient(tree, textPtr) - TkTextBTree tree; /* Tree to remove client from */ - TkText *textPtr; /* Client to remove. */ + TkTextBTree tree; /* Tree to remove client from. */ + TkText *textPtr; /* Client to remove. */ { BTree *treePtr = (BTree *) tree; int pixelReference = textPtr->pixelReference; - + if (treePtr->clients == 1) { - /* The last reference to the tree */ + /* + * The last reference to the tree. + */ + DestroyNode(treePtr->rootPtr); ckfree((char *) treePtr); return; } else if (pixelReference == -1) { - /* A client which doesn't care about pixels */ + /* + * A client which doesn't care about pixels. + */ + treePtr->clients--; } else { - /* Clean up pixel data for the given reference */ - + /* + * Clean up pixel data for the given reference. + */ + if (pixelReference == (treePtr->pixelReferences-1)) { - /* - * The widget we're removing has the last index, - * so deletion is easier. + /* + * The widget we're removing has the last index, so deletion is + * easier. */ + RemovePixelClient(treePtr, treePtr->rootPtr, -1); } else { TkText *adjustPtr; - + RemovePixelClient(treePtr, treePtr->rootPtr, pixelReference); - /* - * Now we need to adjust the 'pixelReference' of the - * peer widget whose storage we've just moved. + /* + * Now we need to adjust the 'pixelReference' of the peer widget + * whose storage we've just moved. */ + adjustPtr = treePtr->sharedTextPtr->peers; while (adjustPtr != NULL) { - if (adjustPtr->pixelReference == (treePtr->pixelReferences-1)) { + if (adjustPtr->pixelReference == (treePtr->pixelReferences-1)) { adjustPtr->pixelReference = pixelReference; - break; - } - adjustPtr = adjustPtr->next; + break; + } + adjustPtr = adjustPtr->next; } if (adjustPtr == NULL) { Tcl_Panic("Couldn't find text widget with correct reference"); @@ -569,7 +572,7 @@ TkBTreeRemoveClient(tree, textPtr) treePtr->pixelReferences--; treePtr->clients--; } - + if (textPtr->start != NULL || textPtr->end != NULL) { AdjustStartEndRefs(treePtr, textPtr, TEXT_REMOVE_REFS); } @@ -580,25 +583,23 @@ TkBTreeRemoveClient(tree, textPtr) * * AdjustStartEndRefs -- * - * Modify B-tree's cache of start, end lines for the given text - * widget. + * Modify B-tree's cache of start, end lines for the given text widget. * * Results: * None. * * Side effects: - * The number of cached items may change - * (treePtr->startEndCount). + * The number of cached items may change (treePtr->startEndCount). * *---------------------------------------------------------------------- */ static void -AdjustStartEndRefs(treePtr, textPtr, action) - BTree *treePtr; /* The entire B-tree */ - TkText *textPtr; /* The text widget for which we want to - * adjust it's start and end cache. */ - int action; /* Action to perform */ +AdjustStartEndRefs(treePtr, textPtr, action) + BTree *treePtr; /* The entire B-tree */ + TkText *textPtr; /* The text widget for which we want to adjust + * it's start and end cache. */ + int action; /* Action to perform */ { if (action & TEXT_REMOVE_REFS) { int i = 0; @@ -610,30 +611,38 @@ AdjustStartEndRefs(treePtr, textPtr, action) treePtr->startEndRef[count] = treePtr->startEndRef[i]; } if (treePtr->startEndRef[i] != textPtr) { - count++; + count++; } i++; } treePtr->startEndCount = count; - treePtr->startEnd = (TkTextLine**)ckrealloc((char*)treePtr->startEnd, - sizeof(TkTextLine*)*count); - treePtr->startEndRef = (TkText**)ckrealloc((char*)treePtr->startEndRef, - sizeof(TkText*)*count); - } - if ((action & TEXT_ADD_REFS) - && (textPtr->start != NULL || textPtr->end != NULL)) { + treePtr->startEnd = (TkTextLine **) + ckrealloc((char *) treePtr->startEnd, + sizeof(TkTextLine *) * count); + treePtr->startEndRef = (TkText **) + ckrealloc((char *) treePtr->startEndRef, + sizeof(TkText *) * count); + } + if ((action & TEXT_ADD_REFS) + && (textPtr->start != NULL || textPtr->end != NULL)) { int count; - if (textPtr->start != NULL) treePtr->startEndCount++; - if (textPtr->end != NULL) treePtr->startEndCount++; + if (textPtr->start != NULL) { + treePtr->startEndCount++; + } + if (textPtr->end != NULL) { + treePtr->startEndCount++; + } count = treePtr->startEndCount; - - treePtr->startEnd = (TkTextLine**)ckrealloc((char*)treePtr->startEnd, - sizeof(TkTextLine*)*count); - treePtr->startEndRef = (TkText**)ckrealloc((char*)treePtr->startEndRef, - sizeof(TkText*)*count); - + + treePtr->startEnd = (TkTextLine **) + ckrealloc((char *) treePtr->startEnd, + sizeof(TkTextLine *) * count); + treePtr->startEndRef = (TkText **) + ckrealloc((char *) treePtr->startEndRef, + sizeof(TkText *) * count); + if (textPtr->start != NULL) { count--; treePtr->startEnd[count] = textPtr->start; @@ -652,69 +661,60 @@ AdjustStartEndRefs(treePtr, textPtr, action) * * AdjustPixelClient -- * - * Utility procedure used to update all data structures for the - * existence of a new peer widget based on this B-tree, or for - * the modification of the start, end lines of an existing peer - * widget. - * + * Utility function used to update all data structures for the existence + * of a new peer widget based on this B-tree, or for the modification of + * the start, end lines of an existing peer widget. + * * Immediately _after_ calling this, treePtr->clients and - * treePtr->pixelReferences should be adjusted if needed (i.e. - * if this is a new peer). + * treePtr->pixelReferences should be adjusted if needed (i.e. if this is + * a new peer). * * Results: * None. * * Side effects: - * All the storage for Nodes and TkTextLines in the tree may - * be adjusted. + * All the storage for Nodes and TkTextLines in the tree may be adjusted. * *---------------------------------------------------------------------- */ static int -AdjustPixelClient(treePtr, defaultHeight, nodePtr, start, end, - useReference, newPixelReferences, counting) - BTree *treePtr; /* Pointer to tree */ - int defaultHeight; /* Default pixel line - * height, which can be zero. */ - Node *nodePtr; /* Adjust from this node - * downwards */ - TkTextLine *start; /* First line for this pixel - * client */ - TkTextLine *end; /* Last line for this pixel - * client */ - int useReference; /* pixel reference for the - * client we are adding or - * changing */ - int newPixelReferences; /* New number of pixel - * references to this B-tree */ - int *counting; /* References an integer which - * is zero if we're outside the - * relevant range for this - * client, and 1 if we're - * inside. */ +AdjustPixelClient(treePtr, defaultHeight, nodePtr, start, end, + useReference, newPixelReferences, counting) + BTree *treePtr; /* Pointer to tree */ + int defaultHeight; /* Default pixel line height, which can be + * zero. */ + Node *nodePtr; /* Adjust from this node downwards */ + TkTextLine *start; /* First line for this pixel client */ + TkTextLine *end; /* Last line for this pixel client */ + int useReference; /* pixel reference for the client we are + * adding or changing */ + int newPixelReferences; /* New number of pixel references to this + * B-tree */ + int *counting; /* References an integer which is zero if + * we're outside the relevant range for this + * client, and 1 if we're inside. */ { int pixelCount = 0; /* - * Traverse entire tree down from nodePtr, reallocating pixel - * structures for each Node and TkTextLine, adding room for the new - * peer's pixel information (1 extra int per Node, 2 extra ints per - * TkTextLine). Also copy the information from the last peer into - * the new space (so it contains something sensible). + * Traverse entire tree down from nodePtr, reallocating pixel structures + * for each Node and TkTextLine, adding room for the new peer's pixel + * information (1 extra int per Node, 2 extra ints per TkTextLine). Also + * copy the information from the last peer into the new space (so it + * contains something sensible). */ - + if (nodePtr->level != 0) { Node *loopPtr = nodePtr->children.nodePtr; while (loopPtr != NULL) { pixelCount += AdjustPixelClient(treePtr, defaultHeight, loopPtr, - start, end, useReference, - newPixelReferences, counting); + start, end, useReference, newPixelReferences, counting); loopPtr = loopPtr->nextPtr; } } else { register TkTextLine *linePtr = nodePtr->children.linePtr; - + while (linePtr != NULL) { if (!*counting && (linePtr == start)) { *counting = 1; @@ -723,24 +723,25 @@ AdjustPixelClient(treePtr, defaultHeight, nodePtr, start, end, *counting = 0; } if (newPixelReferences != treePtr->pixelReferences) { - linePtr->pixels = (int*)ckrealloc((char*)linePtr->pixels, - sizeof(int)*2*newPixelReferences); + linePtr->pixels = (int *) ckrealloc((char *) linePtr->pixels, + sizeof(int) * 2 * newPixelReferences); } - /* - * Notice that for the very last line, we are never counting - * and therefore this always has a height of 0 and an epoch - * of 1. + + /* + * Notice that for the very last line, we are never counting and + * therefore this always has a height of 0 and an epoch of 1. */ + linePtr->pixels[2*useReference] = (*counting ? defaultHeight : 0); linePtr->pixels[1+2*useReference] = (*counting ? 0 : 1); pixelCount += linePtr->pixels[2*useReference]; - + linePtr = linePtr->nextPtr; } } if (newPixelReferences != treePtr->pixelReferences) { - nodePtr->numPixels = (int*)ckrealloc((char*)nodePtr->numPixels, - sizeof(int)*newPixelReferences); + nodePtr->numPixels = (int *) ckrealloc((char *) nodePtr->numPixels, + sizeof(int) * newPixelReferences); } nodePtr->numPixels[useReference] = pixelCount; return pixelCount; @@ -751,48 +752,45 @@ AdjustPixelClient(treePtr, defaultHeight, nodePtr, start, end, * * RemovePixelClient -- * - * Utility procedure used to update all data structures for the - * removal of a peer widget which used to be based on this B-tree. - * - * Immediately _after_ calling this, treePtr->clients should - * be decremented. + * Utility function used to update all data structures for the removal of + * a peer widget which used to be based on this B-tree. + * + * Immediately _after_ calling this, treePtr->clients should be + * decremented. * * Results: * None. * * Side effects: - * All the storage for Nodes and TkTextLines in the tree may - * be adjusted. + * All the storage for Nodes and TkTextLines in the tree may be adjusted. * *---------------------------------------------------------------------- */ static void RemovePixelClient(treePtr, nodePtr, overwriteWithLast) - BTree *treePtr; /* Pointer to tree */ - Node *nodePtr; /* Adjust from this node - * downwards */ - int overwriteWithLast; /* Over-write this peer widget's - * information with the last one - */ + BTree *treePtr; /* Pointer to tree */ + Node *nodePtr; /* Adjust from this node downwards */ + int overwriteWithLast; /* Over-write this peer widget's information + * with the last one. */ { /* - * Traverse entire tree down from nodePtr, reallocating pixel - * structures for each Node and TkTextLine, removing space allocated - * for one peer. If 'overwriteWithLast' is not -1, then copy the - * information which was in the last slot on top of one of the - * others (i.e. it's not the last one we're deleting). + * Traverse entire tree down from nodePtr, reallocating pixel structures + * for each Node and TkTextLine, removing space allocated for one peer. + * If 'overwriteWithLast' is not -1, then copy the information which was + * in the last slot on top of one of the others (i.e. it's not the last + * one we're deleting). */ if (overwriteWithLast != -1) { - nodePtr->numPixels[overwriteWithLast] - = nodePtr->numPixels[treePtr->pixelReferences-1]; + nodePtr->numPixels[overwriteWithLast] = + nodePtr->numPixels[treePtr->pixelReferences-1]; } if (treePtr->pixelReferences == 1) { nodePtr->numPixels = NULL; } else { - nodePtr->numPixels = (int*)ckrealloc((char*)nodePtr->numPixels, - sizeof(int)*(treePtr->pixelReferences-1)); + nodePtr->numPixels = (int *) ckrealloc((char *) nodePtr->numPixels, + sizeof(int) * (treePtr->pixelReferences - 1)); } if (nodePtr->level != 0) { nodePtr = nodePtr->children.nodePtr; @@ -804,16 +802,16 @@ RemovePixelClient(treePtr, nodePtr, overwriteWithLast) register TkTextLine *linePtr = nodePtr->children.linePtr; while (linePtr != NULL) { if (overwriteWithLast != -1) { - linePtr->pixels[2*overwriteWithLast] - = linePtr->pixels[2*(treePtr->pixelReferences-1)]; - linePtr->pixels[1+2*overwriteWithLast] - = linePtr->pixels[1+2*(treePtr->pixelReferences-1)]; + linePtr->pixels[2*overwriteWithLast] = + linePtr->pixels[2*(treePtr->pixelReferences-1)]; + linePtr->pixels[1+2*overwriteWithLast] = + linePtr->pixels[1+2*(treePtr->pixelReferences-1)]; } if (treePtr->pixelReferences == 1) { linePtr->pixels = NULL; } else { - linePtr->pixels = (int*)ckrealloc((char*)linePtr->pixels, - sizeof(int)*2*(treePtr->pixelReferences-1)); + linePtr->pixels = (int *) ckrealloc((char *) linePtr->pixels, + sizeof(int) * 2 * (treePtr->pixelReferences-1)); } linePtr = linePtr->nextPtr; } @@ -825,8 +823,8 @@ RemovePixelClient(treePtr, nodePtr, overwriteWithLast) * * DestroyNode -- * - * This is a recursive utility procedure used during the deletion - * of a B-tree. + * This is a recursive utility function used during the deletion of a + * B-tree. * * Results: * None. @@ -839,7 +837,7 @@ RemovePixelClient(treePtr, nodePtr, overwriteWithLast) static void DestroyNode(nodePtr) - register Node *nodePtr; /* Destroy from this node downwards */ + register Node *nodePtr; /* Destroy from this node downwards */ { if (nodePtr->level == 0) { TkTextLine *linePtr; @@ -875,8 +873,8 @@ DestroyNode(nodePtr) * * DeleteSummaries -- * - * Free up all of the memory in a list of tag summaries associated - * with a node. + * Free up all of the memory in a list of tag summaries associated with a + * node. * * Results: * None. @@ -889,8 +887,8 @@ DestroyNode(nodePtr) static void DeleteSummaries(summaryPtr) - register Summary *summaryPtr; /* First in list of node's tag - * summaries. */ + register Summary *summaryPtr; + /* First in list of node's tag summaries. */ { register Summary *nextPtr; while (summaryPtr != NULL) { @@ -905,67 +903,65 @@ DeleteSummaries(summaryPtr) * * TkBTreeAdjustPixelHeight -- * - * Adjust the pixel height of a given logical line to the - * specified value. + * Adjust the pixel height of a given logical line to the specified + * value. * * Results: * Total number of valid pixels currently known in the tree. * * Side effects: - * Updates overall data structures so pixel height count is - * consistent. + * Updates overall data structures so pixel height count is consistent. * *---------------------------------------------------------------------- */ int TkBTreeAdjustPixelHeight(textPtr, linePtr, newPixelHeight, mergedLogicalLines) - CONST TkText *textPtr; /* Client of the B-tree */ - register TkTextLine *linePtr; /* The logical line to update */ - int newPixelHeight; /* The line's known height - * in pixels */ - int mergedLogicalLines; /* The number of extra logical - * lines which have been merged - * with this one (due to elided - * eols). They will have their - * pixel height set to zero, and - * the total pixel height - * associated with the given - * linePtr. */ + CONST TkText *textPtr; /* Client of the B-tree */ + register TkTextLine *linePtr; + /* The logical line to update. */ + int newPixelHeight; /* The line's known height in pixels. */ + int mergedLogicalLines; /* The number of extra logical lines which + * have been merged with this one (due to + * elided eols). They will have their pixel + * height set to zero, and the total pixel + * height associated with the given linePtr */ { register Node *nodePtr; - int changeToPixelCount; /* Counts change to total number of - * pixels in file. */ + int changeToPixelCount; /* Counts change to total number of pixels in + * file. */ int pixelReference = textPtr->pixelReference; - + changeToPixelCount = newPixelHeight - linePtr->pixels[2*pixelReference]; /* - * Increment the pixel counts in all the parent nodes of the - * current line, then rebalance the tree if necessary. + * Increment the pixel counts in all the parent nodes of the current line, + * then rebalance the tree if necessary. */ nodePtr = linePtr->parentPtr; nodePtr->numPixels[pixelReference] += changeToPixelCount; - + while (nodePtr->parentPtr != NULL) { nodePtr = nodePtr->parentPtr; nodePtr->numPixels[pixelReference] += changeToPixelCount; } linePtr->pixels[2*pixelReference] = newPixelHeight; - - /* - * Any merged logical lines must have their height set to zero. + + /* + * Any merged logical lines must have their height set to zero. */ + while (mergedLogicalLines-- > 0) { - linePtr = TkBTreeNextLine(textPtr, linePtr); + linePtr = TkBTreeNextLine(textPtr, linePtr); TkBTreeAdjustPixelHeight(textPtr, linePtr, 0, 0); } - + /* * Return total number of pixels in the tree. */ + return nodePtr->numPixels[pixelReference]; } @@ -980,69 +976,67 @@ TkBTreeAdjustPixelHeight(textPtr, linePtr, newPixelHeight, mergedLogicalLines) * None. * * Side effects: - * Characters are added to the B-tree at the given position. - * If the string contains newlines, new lines will be added, - * which could cause the structure of the B-tree to change. + * Characters are added to the B-tree at the given position. If the + * string contains newlines, new lines will be added, which could cause + * the structure of the B-tree to change. * *---------------------------------------------------------------------- */ void TkBTreeInsertChars(tree, indexPtr, string) - TkTextBTree tree; /* Tree to insert into */ - register TkTextIndex *indexPtr; /* Indicates where to insert text. - * When the procedure returns, this - * index is no longer valid because - * of changes to the segment - * structure. */ - CONST char *string; /* Pointer to bytes to insert (may - * contain newlines, must be null- - * terminated). */ + TkTextBTree tree; /* Tree to insert into */ + register TkTextIndex *indexPtr; + /* Indicates where to insert text. When the + * function returns, this index is no longer + * valid because of changes to the segment + * structure. */ + CONST char *string; /* Pointer to bytes to insert (may contain + * newlines, must be null-terminated). */ { register Node *nodePtr; - register TkTextSegment *prevPtr; /* The segment just before the first - * new segment (NULL means new segment - * is at beginning of line). */ - TkTextSegment *curPtr; /* Current segment; new characters - * are inserted just after this one. - * NULL means insert at beginning of - * line. */ - TkTextLine *linePtr; /* Current line (new segments are - * added to this line). */ + register TkTextSegment *prevPtr; + /* The segment just before the first new + * segment (NULL means new segment is at + * beginning of line). */ + TkTextSegment *curPtr; /* Current segment; new characters are + * inserted just after this one. NULL means + * insert at beginning of line. */ + TkTextLine *linePtr; /* Current line (new segments are added to + * this line). */ register TkTextSegment *segPtr; TkTextLine *newLinePtr; - int chunkSize; /* # characters in current chunk. */ - register CONST char *eol; /* Pointer to character just after last - * one in current chunk. */ - int changeToLineCount; /* Counts change to total number of - * lines in file. */ - int *changeToPixelCount; /* Counts change to total number of - * pixels in file. */ + int chunkSize; /* # characters in current chunk. */ + register CONST char *eol; /* Pointer to character just after last one in + * current chunk. */ + int changeToLineCount; /* Counts change to total number of lines in + * file. */ + int *changeToPixelCount; /* Counts change to total number of pixels in + * file. */ int ref; int pixels[PIXEL_CLIENTS]; - + BTree *treePtr = (BTree*)tree; prevPtr = SplitSeg(indexPtr); linePtr = indexPtr->linePtr; curPtr = prevPtr; /* - * Chop the string up into lines and create a new segment for - * each line, plus a new line for the leftovers from the - * previous line. + * Chop the string up into lines and create a new segment for each line, + * plus a new line for the leftovers from the previous line. */ changeToLineCount = 0; if (treePtr->pixelReferences > PIXEL_CLIENTS) { - changeToPixelCount = (int*) ckalloc(sizeof(int) * - treePtr->pixelReferences); + changeToPixelCount = (int *) + ckalloc(sizeof(int) * treePtr->pixelReferences); } else { changeToPixelCount = pixels; } for (ref = 0; ref < treePtr->pixelReferences; ref++) { changeToPixelCount[ref] = 0; } - + while (*string != 0) { for (eol = string; *eol != 0; eol++) { if (*eol == '\n') { @@ -1069,22 +1063,24 @@ TkBTreeInsertChars(tree, indexPtr, string) } /* - * The chunk ended with a newline, so create a new TkTextLine - * and move the remainder of the old line to it. + * The chunk ended with a newline, so create a new TkTextLine and move + * the remainder of the old line to it. */ newLinePtr = (TkTextLine *) ckalloc(sizeof(TkTextLine)); - newLinePtr->pixels = - (int*) ckalloc(sizeof(int)*2*treePtr->pixelReferences); - + newLinePtr->pixels = (int *) + ckalloc(sizeof(int)*2*treePtr->pixelReferences); + newLinePtr->parentPtr = linePtr->parentPtr; newLinePtr->nextPtr = linePtr->nextPtr; linePtr->nextPtr = newLinePtr; newLinePtr->segPtr = segPtr->nextPtr; + /* - * Set up a starting default height, which will be re-adjusted + * Set up a starting default height, which will be re-adjusted * later. We need to do this for each referenced widget */ + for (ref = 0; ref < treePtr->pixelReferences; ref++) { newLinePtr->pixels[2*ref] = linePtr->pixels[2*ref]; newLinePtr->pixels[1+2*ref] = 0; @@ -1095,30 +1091,30 @@ TkBTreeInsertChars(tree, indexPtr, string) linePtr = newLinePtr; curPtr = NULL; changeToLineCount++; - + string = eol; } /* - * I don't believe it's possible for either of the two lines - * passed to this function to be the last line of text, but - * the function is robust to that case anyway. (We must never - * re-calculated the line height of the last line). + * I don't believe it's possible for either of the two lines passed to + * this function to be the last line of text, but the function is robust + * to that case anyway. (We must never re-calculated the line height of + * the last line). */ - TkTextInvalidateLineMetrics(treePtr->sharedTextPtr, NULL, - indexPtr->linePtr, changeToLineCount, - TK_TEXT_INVALIDATE_INSERT); + + TkTextInvalidateLineMetrics(treePtr->sharedTextPtr, NULL, + indexPtr->linePtr, changeToLineCount, TK_TEXT_INVALIDATE_INSERT); /* - * Cleanup the starting line for the insertion, plus the ending - * line if it's different. + * Cleanup the starting line for the insertion, plus the ending line if + * it's different. */ CleanupLine(indexPtr->linePtr); if (linePtr != indexPtr->linePtr) { CleanupLine(linePtr); } - + /* * Increment the line and pixel counts in all the parent nodes of the * insertion point, then rebalance the tree if necessary. @@ -1134,7 +1130,7 @@ TkBTreeInsertChars(tree, indexPtr, string) if (treePtr->pixelReferences > PIXEL_CLIENTS) { ckfree((char*)changeToPixelCount); } - + nodePtr = linePtr->parentPtr; nodePtr->numChildren += changeToLineCount; if (nodePtr->numChildren > MAX_CHILDREN) { @@ -1151,42 +1147,40 @@ TkBTreeInsertChars(tree, indexPtr, string) * * SplitSeg -- * - * This procedure is called before adding or deleting - * segments. It does three things: (a) it finds the segment - * containing indexPtr; (b) if there are several such - * segments (because some segments have zero length) then - * it picks the first segment that does not have left - * gravity; (c) if the index refers to the middle of - * a segment then it splits the segment so that the - * index now refers to the beginning of a segment. + * This function is called before adding or deleting segments. It does + * three things: (a) it finds the segment containing indexPtr; (b) if + * there are several such segments (because some segments have zero + * length) then it picks the first segment that does not have left + * gravity; (c) if the index refers to the middle of a segment then it + * splits the segment so that the index now refers to the beginning of a + * segment. * * Results: - * The return value is a pointer to the segment just - * before the segment corresponding to indexPtr (as - * described above). If the segment corresponding to - * indexPtr is the first in its line then the return + * The return value is a pointer to the segment just before the segment + * corresponding to indexPtr (as described above). If the segment + * corresponding to indexPtr is the first in its line then the return * value is NULL. * * Side effects: - * The segment referred to by indexPtr is split unless - * indexPtr refers to its first character. + * The segment referred to by indexPtr is split unless indexPtr refers to + * its first character. * *-------------------------------------------------------------- */ static TkTextSegment * SplitSeg(indexPtr) - TkTextIndex *indexPtr; /* Index identifying position - * at which to split a segment. */ + TkTextIndex *indexPtr; /* Index identifying position at which to + * split a segment. */ { TkTextSegment *prevPtr, *segPtr; TkTextLine *linePtr; - + int count = indexPtr->byteIndex; linePtr = indexPtr->linePtr; prevPtr = NULL; segPtr = linePtr->segPtr; - + while (segPtr != NULL) { if (segPtr->size > count) { if (count == 0) { @@ -1203,15 +1197,16 @@ SplitSeg(indexPtr) && !segPtr->typePtr->leftGravity) { return prevPtr; } - + count -= segPtr->size; prevPtr = segPtr; segPtr = segPtr->nextPtr; if (segPtr == NULL) { - /* - * Two logical lines merged into one display line - * through eliding of a newline + /* + * Two logical lines merged into one display line through eliding + * of a newline */ + linePtr = TkBTreeNextLine(NULL, linePtr); if (linePtr == NULL) { /* Reached end of the text */ @@ -1228,17 +1223,16 @@ SplitSeg(indexPtr) * * CleanupLine -- * - * This procedure is called after modifications have been - * made to a line. It scans over all of the segments in - * the line, giving each a chance to clean itself up, e.g. - * by merging with the following segments, updating internal - * information, etc. + * This function is called after modifications have been made to a line. + * It scans over all of the segments in the line, giving each a chance to + * clean itself up, e.g. by merging with the following segments, updating + * internal information, etc. * * Results: * None. * * Side effects: - * Depends on what the segment-specific cleanup procedures do. + * Depends on what the segment-specific cleanup functions do. * *-------------------------------------------------------------- */ @@ -1251,13 +1245,12 @@ CleanupLine(linePtr) int anyChanges; /* - * Make a pass over all of the segments in the line, giving each - * a chance to clean itself up. This could potentially change - * the structure of the line, e.g. by merging two segments - * together or having two segments cancel themselves; if so, - * then repeat the whole process again, since the first structure - * change might make other structure changes possible. Repeat - * until eventually there are no changes. + * Make a pass over all of the segments in the line, giving each a chance + * to clean itself up. This could potentially change the structure of the + * line, e.g. by merging two segments together or having two segments + * cancel themselves; if so, then repeat the whole process again, since + * the first structure change might make other structure changes possible. + * Repeat until eventually there are no changes. */ while (1) { @@ -1283,45 +1276,45 @@ CleanupLine(linePtr) * * TkBTreeDeleteChars -- * - * Delete a range of characters from a B-tree. The caller - * must make sure that the final newline of the B-tree is - * never deleted. + * Delete a range of characters from a B-tree. The caller must make sure + * that the final newline of the B-tree is never deleted. * * Results: * None. * * Side effects: - * Information is deleted from the B-tree. This can cause the - * internal structure of the B-tree to change. Note: because - * of changes to the B-tree structure, the indices pointed - * to by index1Ptr and index2Ptr should not be used after this - * procedure returns. + * Information is deleted from the B-tree. This can cause the internal + * structure of the B-tree to change. Note: because of changes to the + * B-tree structure, the indices pointed to by index1Ptr and index2Ptr + * should not be used after this function returns. * *---------------------------------------------------------------------- */ void TkBTreeDeleteChars(tree, index1Ptr, index2Ptr) - TkTextBTree tree; /* Tree to delete from */ - register TkTextIndex *index1Ptr; /* Indicates first character that is - * to be deleted. */ - register TkTextIndex *index2Ptr; /* Indicates character just after the - * last one that is to be deleted. */ + TkTextBTree tree; /* Tree to delete from */ + register TkTextIndex *index1Ptr; + /* Indicates first character that is to be + * deleted. */ + register TkTextIndex *index2Ptr; + /* Indicates character just after the last one + * that is to be deleted. */ { - TkTextSegment *prevPtr; /* The segment just before the start - * of the deletion range. */ - TkTextSegment *lastPtr; /* The segment just after the end - * of the deletion range. */ + TkTextSegment *prevPtr; /* The segment just before the start of the + * deletion range. */ + TkTextSegment *lastPtr; /* The segment just after the end of the + * deletion range. */ TkTextSegment *segPtr, *nextPtr; TkTextLine *curLinePtr; Node *curNodePtr, *nodePtr; int changeToLineCount = 0; int ref; BTree *treePtr = (BTree*)tree; - + /* - * Tricky point: split at index2Ptr first; otherwise the split - * at index2Ptr may invalidate segPtr and/or prevPtr. + * Tricky point: split at index2Ptr first; otherwise the split at + * index2Ptr may invalidate segPtr and/or prevPtr. */ lastPtr = SplitSeg(index2Ptr); @@ -1351,9 +1344,9 @@ TkBTreeDeleteChars(tree, index1Ptr, index2Ptr) TkTextLine *nextLinePtr; /* - * We just ran off the end of a line. First find the - * next line, then go back to the old line and delete it - * (unless it's the starting line for the range). + * We just ran off the end of a line. First find the next line, + * then go back to the old line and delete it (unless it's the + * starting line for the range). */ nextLinePtr = TkBTreeNextLine(NULL, curLinePtr); @@ -1372,17 +1365,24 @@ TkBTreeDeleteChars(tree, index1Ptr, index2Ptr) } changeToLineCount++; curNodePtr->numChildren--; - /* Check if we need to adjust any partial clients */ + + /* + * Check if we need to adjust any partial clients. + */ + if (treePtr->startEnd != NULL) { int checkCount = 0; + while (checkCount < treePtr->startEndCount) { if (treePtr->startEnd[checkCount] == curLinePtr) { TkText *peer = treePtr->startEndRef[checkCount]; - /* - * We're deleting a line which is the start - * or end of a current client. This means - * we need to adjust that client. + + /* + * We're deleting a line which is the start or end + * of a current client. This means we need to + * adjust that client. */ + treePtr->startEnd[checkCount] = nextLinePtr; if (peer->start == curLinePtr) { peer->start = nextLinePtr; @@ -1429,8 +1429,8 @@ TkBTreeDeleteChars(tree, index1Ptr, index2Ptr) nextPtr = segPtr->nextPtr; if ((*segPtr->typePtr->deleteProc)(segPtr, curLinePtr, 0) != 0) { /* - * This segment refuses to die. Move it to prevPtr and - * advance prevPtr if the segment has left gravity. + * This segment refuses to die. Move it to prevPtr and advance + * prevPtr if the segment has left gravity. */ if (prevPtr == NULL) { @@ -1448,8 +1448,8 @@ TkBTreeDeleteChars(tree, index1Ptr, index2Ptr) } /* - * If the beginning and end of the deletion range are in different - * lines, join the two lines together and discard the ending line. + * If the beginning and end of the deletion range are in different lines, + * join the two lines together and discard the ending line. */ if (index1Ptr->linePtr != index2Ptr->linePtr) { @@ -1480,24 +1480,28 @@ TkBTreeDeleteChars(tree, index1Ptr, index2Ptr) } prevLinePtr->nextPtr = index2Ptr->linePtr->nextPtr; } - /* - * Check if we need to adjust any partial clients. In this case - * if we're deleting the line, we actually move back to the - * previous line for our (start,end) storage. We do this - * because we still want the portion of the second line that - * still exists to be in the start,end range. + + /* + * Check if we need to adjust any partial clients. In this case if + * we're deleting the line, we actually move back to the previous line + * for our (start,end) storage. We do this because we still want the + * portion of the second line that still exists to be in the start,end + * range. */ + if (treePtr->startEnd != NULL) { int checkCount = 0; while (treePtr->startEnd[checkCount] != NULL) { if (treePtr->startEnd[checkCount] == index2Ptr->linePtr) { TkText *peer = treePtr->startEndRef[checkCount]; - /* - * We're deleting a line which is the start - * or end of a current client. This means - * we need to adjust that client. + + /* + * We're deleting a line which is the start or end of a + * current client. This means we need to adjust that + * client. */ + treePtr->startEnd[checkCount] = index1Ptr->linePtr; if (peer->start == index2Ptr->linePtr) { peer->start = index1Ptr->linePtr; @@ -1522,17 +1526,18 @@ TkBTreeDeleteChars(tree, index1Ptr, index2Ptr) CleanupLine(index1Ptr->linePtr); /* - * This line now needs to have its height recalculated. For safety, - * ensure we don't call this function with the last artificial line - * of text. I _believe_ that it isn't possible to get this far with - * the last line, but it is good to be safe. + * This line now needs to have its height recalculated. For safety, ensure + * we don't call this function with the last artificial line of text. I + * _believe_ that it isn't possible to get this far with the last line, + * but it is good to be safe. */ + if (TkBTreeNextLine(NULL, index1Ptr->linePtr) != NULL) { - TkTextInvalidateLineMetrics(treePtr->sharedTextPtr, NULL, - index1Ptr->linePtr, changeToLineCount, - TK_TEXT_INVALIDATE_DELETE); + TkTextInvalidateLineMetrics(treePtr->sharedTextPtr, NULL, + index1Ptr->linePtr, changeToLineCount, + TK_TEXT_INVALIDATE_DELETE); } - + /* * Lastly, rebalance the first node of the range. */ @@ -1551,8 +1556,8 @@ TkBTreeDeleteChars(tree, index1Ptr, index2Ptr) * Find a particular line in a B-tree based on its line number. * * Results: - * The return value is a pointer to the line structure for the - * line whose index is "line", or NULL if no such line exists. + * The return value is a pointer to the line structure for the line whose + * index is "line", or NULL if no such line exists. * * Side effects: * None. @@ -1562,10 +1567,9 @@ TkBTreeDeleteChars(tree, index1Ptr, index2Ptr) TkTextLine * TkBTreeFindLine(tree, textPtr, line) - TkTextBTree tree; /* B-tree in which to find line. */ - CONST TkText *textPtr; /* Relative to this client of the - * B-tree */ - int line; /* Index of desired line. */ + TkTextBTree tree; /* B-tree in which to find line. */ + CONST TkText *textPtr; /* Relative to this client of the B-tree */ + int line; /* Index of desired line. */ { BTree *treePtr = (BTree *) tree; register Node *nodePtr; @@ -1574,17 +1578,18 @@ TkBTreeFindLine(tree, textPtr, line) if (treePtr == NULL) { treePtr = (BTree *) textPtr->sharedTextPtr->tree; } - + nodePtr = treePtr->rootPtr; if ((line < 0) || (line >= nodePtr->numLines)) { return NULL; } - - /* - * Check for the any start/end offset for this text widget + + /* + * Check for the any start/end offset for this text widget */ + if (textPtr != NULL) { - if (textPtr->start != NULL) { + if (textPtr->start != NULL) { line += TkBTreeLinesTo(NULL, textPtr->start); if (line >= nodePtr->numLines) { return NULL; @@ -1596,10 +1601,9 @@ TkBTreeFindLine(tree, textPtr, line) } } } - + /* - * Work down through levels of the tree until a node is found at - * level 0. + * Work down through levels of the tree until a node is found at level 0. */ while (nodePtr->level != 0) { @@ -1635,15 +1639,14 @@ TkBTreeFindLine(tree, textPtr, line) * Find a particular line in a B-tree based on its pixel count. * * Results: - * The return value is a pointer to the line structure for the - * line which contains the pixel "pixels", or NULL if no such - * line exists. If the first line is of height 20, then pixels - * 0-19 will return it, and pixels = 20 will return the next - * line. - * - * If pixelOffset is non-NULL, it is set to the amount by which - * 'pixels' exceeds the first pixel located on the returned - * line. This should always be non-negative. + * The return value is a pointer to the line structure for the line which + * contains the pixel "pixels", or NULL if no such line exists. If the + * first line is of height 20, then pixels 0-19 will return it, and + * pixels = 20 will return the next line. + * + * If pixelOffset is non-NULL, it is set to the amount by which 'pixels' + * exceeds the first pixel located on the returned line. This should + * always be non-negative. * * Side effects: * None. @@ -1653,17 +1656,16 @@ TkBTreeFindLine(tree, textPtr, line) TkTextLine * TkBTreeFindPixelLine(tree, textPtr, pixels, pixelOffset) - TkTextBTree tree; /* B-tree to use. */ - CONST TkText *textPtr; /* Relative to this client of the - * B-tree */ - int pixels; /* Pixel index of desired line. */ - int *pixelOffset; /* Used to return offset */ + TkTextBTree tree; /* B-tree to use. */ + CONST TkText *textPtr; /* Relative to this client of the B-tree */ + int pixels; /* Pixel index of desired line. */ + int *pixelOffset; /* Used to return offset */ { BTree *treePtr = (BTree *) tree; register Node *nodePtr; register TkTextLine *linePtr; int pixelReference = textPtr->pixelReference; - + nodePtr = treePtr->rootPtr; if ((pixels < 0) || (pixels > nodePtr->numPixels[pixelReference])) { @@ -1673,10 +1675,9 @@ TkBTreeFindPixelLine(tree, textPtr, pixels, pixelOffset) if (nodePtr->numPixels[pixelReference] == 0) { Tcl_Panic("TkBTreeFindPixelLine called with empty window"); } - + /* - * Work down through levels of the tree until a node is found at - * level 0. + * Work down through levels of the tree until a node is found at level 0. */ while (nodePtr->level != 0) { @@ -1694,7 +1695,7 @@ TkBTreeFindPixelLine(tree, textPtr, pixels, pixelOffset) * Work through the lines attached to the level-0 node. */ - for (linePtr = nodePtr->children.linePtr; + for (linePtr = nodePtr->children.linePtr; linePtr->pixels[2*pixelReference] < pixels; linePtr = linePtr->nextPtr) { if (linePtr == NULL) { @@ -1703,7 +1704,7 @@ TkBTreeFindPixelLine(tree, textPtr, pixels, pixelOffset) pixels -= linePtr->pixels[2*pixelReference]; } if (pixelOffset != NULL && linePtr != NULL) { - *pixelOffset = pixels; + *pixelOffset = pixels; } return linePtr; } @@ -1713,13 +1714,13 @@ TkBTreeFindPixelLine(tree, textPtr, pixels, pixelOffset) * * TkBTreeNextLine -- * - * Given an existing line in a B-tree, this procedure locates the - * next line in the B-tree. This procedure is used for scanning - * through the B-tree. + * Given an existing line in a B-tree, this function locates the next + * line in the B-tree. This function is used for scanning through the + * B-tree. * * Results: - * The return value is a pointer to the line that immediately - * follows linePtr, or NULL if there is no such line. + * The return value is a pointer to the line that immediately follows + * linePtr, or NULL if there is no such line. * * Side effects: * None. @@ -1729,10 +1730,9 @@ TkBTreeFindPixelLine(tree, textPtr, pixels, pixelOffset) TkTextLine * TkBTreeNextLine(textPtr, linePtr) - CONST TkText *textPtr; /* Next line in the context of - * this client */ - register TkTextLine *linePtr; /* Pointer to existing line in - * B-tree. */ + CONST TkText *textPtr; /* Next line in the context of this client */ + register TkTextLine *linePtr; + /* Pointer to existing line in B-tree. */ { register Node *nodePtr; @@ -1743,11 +1743,11 @@ TkBTreeNextLine(textPtr, linePtr) return linePtr->nextPtr; } } - + /* * This was the last line associated with the particular parent node. - * Search up the tree for the next node, then search down from that - * node to find the first line. + * Search up the tree for the next node, then search down from that node + * to find the first line. */ for (nodePtr = linePtr->parentPtr; ; nodePtr = nodePtr->parentPtr) { @@ -1770,13 +1770,13 @@ TkBTreeNextLine(textPtr, linePtr) * * TkBTreePreviousLine -- * - * Given an existing line in a B-tree, this procedure locates the - * previous line in the B-tree. This procedure is used for scanning - * through the B-tree in the reverse direction. + * Given an existing line in a B-tree, this function locates the previous + * line in the B-tree. This function is used for scanning through the + * B-tree in the reverse direction. * * Results: - * The return value is a pointer to the line that immediately - * preceeds linePtr, or NULL if there is no such line. + * The return value is a pointer to the line that immediately preceeds + * linePtr, or NULL if there is no such line. * * Side effects: * None. @@ -1786,23 +1786,23 @@ TkBTreeNextLine(textPtr, linePtr) TkTextLine * TkBTreePreviousLine(textPtr, linePtr) - TkText *textPtr; /* Relative to this client of the - * B-tree */ - register TkTextLine *linePtr; /* Pointer to existing line in - * B-tree. */ + TkText *textPtr; /* Relative to this client of the B-tree */ + register TkTextLine *linePtr; + /* Pointer to existing line in B-tree. */ { register Node *nodePtr; register Node *node2Ptr; register TkTextLine *prevPtr; if (textPtr != NULL && textPtr->start == linePtr) { - return NULL; + return NULL; } - + /* * Find the line under this node just before the starting line. */ - prevPtr = linePtr->parentPtr->children.linePtr; /* First line at leaf */ + + prevPtr = linePtr->parentPtr->children.linePtr; /* First line at leaf */ while (prevPtr != linePtr) { if (prevPtr->nextPtr == linePtr) { return prevPtr; @@ -1818,6 +1818,7 @@ TkBTreePreviousLine(textPtr, linePtr) * Search up the tree for the previous node, then search down from that * node to find its last line. */ + for (nodePtr = linePtr->parentPtr; ; nodePtr = nodePtr->parentPtr) { if (nodePtr == (Node *) NULL || nodePtr->parentPtr == (Node *) NULL) { return (TkTextLine *) NULL; @@ -1826,7 +1827,7 @@ TkBTreePreviousLine(textPtr, linePtr) break; } } - for (node2Ptr = nodePtr->parentPtr->children.nodePtr; ; + for (node2Ptr = nodePtr->parentPtr->children.nodePtr; ; node2Ptr = node2Ptr->children.nodePtr) { while (node2Ptr->nextPtr != nodePtr) { node2Ptr = node2Ptr->nextPtr; @@ -1848,13 +1849,13 @@ TkBTreePreviousLine(textPtr, linePtr) * * TkBTreePixelsTo -- * - * Given a pointer to a line in a B-tree, return the numerical - * pixel index of the top of that line (i.e. the result does - * not include the height of the given line). - * - * Since the last line of text (the artificial one) has zero - * height by defintion, calling this with the last line will - * return the total number of pixels in the widget. + * Given a pointer to a line in a B-tree, return the numerical pixel + * index of the top of that line (i.e. the result does not include the + * height of the given line). + * + * Since the last line of text (the artificial one) has zero height by + * defintion, calling this with the last line will return the total + * number of pixels in the widget. * * Results: * The result is the index of linePtr within the tree, where 0 @@ -1868,10 +1869,8 @@ TkBTreePreviousLine(textPtr, linePtr) int TkBTreePixelsTo(textPtr, linePtr) - CONST TkText *textPtr; /* Relative to this client of the - * B-tree */ - TkTextLine *linePtr; /* Pointer to existing line in - * B-tree. */ + CONST TkText *textPtr; /* Relative to this client of the B-tree */ + TkTextLine *linePtr; /* Pointer to existing line in B-tree. */ { register TkTextLine *linePtr2; register Node *nodePtr, *parentPtr; @@ -1879,8 +1878,7 @@ TkBTreePixelsTo(textPtr, linePtr) int pixelReference = textPtr->pixelReference; /* - * First count how many pixels precede this line in its level-0 - * node. + * First count how many pixels precede this line in its level-0 node. */ nodePtr = linePtr->parentPtr; @@ -1894,9 +1892,8 @@ TkBTreePixelsTo(textPtr, linePtr) } /* - * Now work up through the levels of the tree one at a time, - * counting how many pixels are in nodes preceding the current - * node. + * Now work up through the levels of the tree one at a time, counting how + * many pixels are in nodes preceding the current node. */ for (parentPtr = nodePtr->parentPtr ; parentPtr != NULL; @@ -1918,8 +1915,8 @@ TkBTreePixelsTo(textPtr, linePtr) * * TkBTreeLinesTo -- * - * Given a pointer to a line in a B-tree, return the numerical - * index of that line. + * Given a pointer to a line in a B-tree, return the numerical index of + * that line. * * Results: * The result is the index of linePtr within the tree, where 0 @@ -1933,18 +1930,15 @@ TkBTreePixelsTo(textPtr, linePtr) int TkBTreeLinesTo(textPtr, linePtr) - CONST TkText *textPtr; /* Relative to this client of the - * B-tree */ - TkTextLine *linePtr; /* Pointer to existing line in - * B-tree. */ + CONST TkText *textPtr; /* Relative to this client of the B-tree */ + TkTextLine *linePtr; /* Pointer to existing line in B-tree. */ { register TkTextLine *linePtr2; register Node *nodePtr, *parentPtr, *nodePtr2; int index; /* - * First count how many lines precede this one in its level-0 - * node. + * First count how many lines precede this one in its level-0 node. */ nodePtr = linePtr->parentPtr; @@ -1958,9 +1952,8 @@ TkBTreeLinesTo(textPtr, linePtr) } /* - * Now work up through the levels of the tree one at a time, - * counting how many lines are in nodes preceding the current - * node. + * Now work up through the levels of the tree one at a time, counting how + * many lines are in nodes preceding the current node. */ for (parentPtr = nodePtr->parentPtr ; parentPtr != NULL; @@ -1974,7 +1967,7 @@ TkBTreeLinesTo(textPtr, linePtr) } } if (textPtr != NULL && textPtr->start != NULL) { - index -= TkBTreeLinesTo(NULL, textPtr->start); + index -= TkBTreeLinesTo(NULL, textPtr->start); } return index; } @@ -1984,8 +1977,7 @@ TkBTreeLinesTo(textPtr, linePtr) * * TkBTreeLinkSegment -- * - * This procedure adds a new segment to a B-tree at a given - * location. + * This function adds a new segment to a B-tree at a given location. * * Results: * None. @@ -2000,11 +1992,10 @@ TkBTreeLinesTo(textPtr, linePtr) void TkBTreeLinkSegment(segPtr, indexPtr) TkTextSegment *segPtr; /* Pointer to new segment to be added to - * B-tree. Should be completely initialized - * by caller except for nextPtr field. */ - TkTextIndex *indexPtr; /* Where to add segment: it gets linked - * in just before the segment indicated - * here. */ + * B-tree. Should be completely initialized by + * caller except for nextPtr field. */ + TkTextIndex *indexPtr; /* Where to add segment: it gets linked in + * just before the segment indicated here. */ { register TkTextSegment *prevPtr; @@ -2027,14 +2018,14 @@ TkBTreeLinkSegment(segPtr, indexPtr) * * TkBTreeUnlinkSegment -- * - * This procedure unlinks a segment from its line in a B-tree. + * This function unlinks a segment from its line in a B-tree. * * Results: * None. * * Side effects: - * SegPtr will be unlinked from linePtr. The segment itself - * isn't modified by this procedure. + * SegPtr will be unlinked from linePtr. The segment itself isn't + * modified by this function. * *---------------------------------------------------------------------- */ @@ -2042,9 +2033,8 @@ TkBTreeLinkSegment(segPtr, indexPtr) /* ARGSUSED */ void TkBTreeUnlinkSegment(segPtr, linePtr) - TkTextSegment *segPtr; /* Segment to be unlinked. */ - TkTextLine *linePtr; /* Line that currently contains - * segment. */ + TkTextSegment *segPtr; /* Segment to be unlinked. */ + TkTextLine *linePtr; /* Line that currently contains segment. */ { register TkTextSegment *prevPtr; @@ -2054,12 +2044,13 @@ TkBTreeUnlinkSegment(segPtr, linePtr) prevPtr = linePtr->segPtr; while (prevPtr->nextPtr != segPtr) { prevPtr = prevPtr->nextPtr; - + if (prevPtr == NULL) { - /* - * Two logical lines merged into one display line - * through eliding of a newline + /* + * Two logical lines merged into one display line through + * eliding of a newline */ + linePtr = TkBTreeNextLine(NULL, linePtr); prevPtr = linePtr->segPtr; } @@ -2074,35 +2065,35 @@ TkBTreeUnlinkSegment(segPtr, linePtr) * * TkBTreeTag -- * - * Turn a given tag on or off for a given range of characters in - * a B-tree of text. + * Turn a given tag on or off for a given range of characters in a B-tree + * of text. * * Results: - * 1 if the tags on any characters in the range were changed, - * and zero otherwise (i.e. if the tag was already absent (add = 0) - * or present (add = 1) on the index range in question). + * 1 if the tags on any characters in the range were changed, and zero + * otherwise (i.e. if the tag was already absent (add = 0) or present + * (add = 1) on the index range in question). * * Side effects: - * The given tag is added to the given range of characters - * in the tree or removed from all those characters, depending - * on the "add" argument. The structure of the btree is modified - * enough that index1Ptr and index2Ptr are no longer valid after - * this procedure returns, and the indexes may be modified by - * this procedure. + * The given tag is added to the given range of characters in the tree or + * removed from all those characters, depending on the "add" argument. + * The structure of the btree is modified enough that index1Ptr and + * index2Ptr are no longer valid after this function returns, and the + * indexes may be modified by this function. * *---------------------------------------------------------------------- */ int TkBTreeTag(index1Ptr, index2Ptr, tagPtr, add) - register TkTextIndex *index1Ptr; /* Indicates first character in - * range. */ - register TkTextIndex *index2Ptr; /* Indicates character just after the - * last one in range. */ - TkTextTag *tagPtr; /* Tag to add or remove. */ - int add; /* One means add tag to the given - * range of characters; zero means - * remove the tag from the range. */ + register TkTextIndex *index1Ptr; + /* Indicates first character in range. */ + register TkTextIndex *index2Ptr; + /* Indicates character just after the last one + * in range. */ + TkTextTag *tagPtr; /* Tag to add or remove. */ + int add; /* One means add tag to the given range of + * characters; zero means remove the tag from + * the range. */ { TkTextSegment *segPtr, *prevPtr; TkTextSearch search; @@ -2110,11 +2101,10 @@ TkBTreeTag(index1Ptr, index2Ptr, tagPtr, add) int oldState; int changed; int anyChanges = 0; - + /* - * See whether the tag is present at the start of the range. If - * the state doesn't already match what we want then add a toggle - * there. + * See whether the tag is present at the start of the range. If the state + * doesn't already match what we want then add a toggle there. */ oldState = TkBTreeCharTagged(index1Ptr, tagPtr); @@ -2136,9 +2126,9 @@ TkBTreeTag(index1Ptr, index2Ptr, tagPtr, add) } /* - * Scan the range of characters and delete any internal tag - * transitions. Keep track of what the old state was at the end - * of the range, and add a toggle there if it's needed. + * Scan the range of characters and delete any internal tag transitions. + * Keep track of what the old state was at the end of the range, and add a + * toggle there if it's needed. */ TkBTreeStartSearch(index1Ptr, index2Ptr, tagPtr, &search); @@ -2167,24 +2157,24 @@ TkBTreeTag(index1Ptr, index2Ptr, tagPtr, add) ckfree((char *) segPtr); /* - * The code below is a bit tricky. After deleting a toggle - * we eventually have to call CleanupLine, in order to allow - * character segments to be merged together. To do this, we - * remember in cleanupLinePtr a line that needs to be - * cleaned up, but we don't clean it up until we've moved - * on to a different line. That way the cleanup process - * won't goof up segPtr. + * The code below is a bit tricky. After deleting a toggle we + * eventually have to call CleanupLine, in order to allow character + * segments to be merged together. To do this, we remember in + * cleanupLinePtr a line that needs to be cleaned up, but we don't + * clean it up until we've moved on to a different line. That way the + * cleanup process won't goof up segPtr. */ if (cleanupLinePtr != search.curIndex.linePtr) { CleanupLine(cleanupLinePtr); cleanupLinePtr = search.curIndex.linePtr; } + /* - * Quick hack. ChangeNodeToggleCount may move the tag's root - * location around and leave the search in the void. This resets - * the search. + * Quick hack. ChangeNodeToggleCount may move the tag's root location + * around and leave the search in the void. This resets the search. */ + if (changed) { TkBTreeStartSearch(index1Ptr, index2Ptr, tagPtr, &search); } @@ -2207,12 +2197,12 @@ TkBTreeTag(index1Ptr, index2Ptr, tagPtr, add) } /* - * Cleanup cleanupLinePtr and the last line of the range, if - * these are different. + * Cleanup cleanupLinePtr and the last line of the range, if these are + * different. */ if (anyChanges) { - CleanupLine(cleanupLinePtr); + CleanupLine(cleanupLinePtr); if (cleanupLinePtr != index2Ptr->linePtr) { CleanupLine(index2Ptr->linePtr); } @@ -2229,32 +2219,32 @@ TkBTreeTag(index1Ptr, index2Ptr, tagPtr, add) * * ChangeNodeToggleCount -- * - * This procedure increments or decrements the toggle count for - * a particular tag in a particular node and all its ancestors - * up to the per-tag root node. + * This function increments or decrements the toggle count for a + * particular tag in a particular node and all its ancestors up to the + * per-tag root node. * * Results: * None. * * Side effects: - * The toggle count for tag is adjusted up or down by "delta" in - * nodePtr. This routine maintains the tagRootPtr that identifies - * the root node for the tag, moving it up or down the tree as needed. + * The toggle count for tag is adjusted up or down by "delta" in nodePtr. + * This routine maintains the tagRootPtr that identifies the root node + * for the tag, moving it up or down the tree as needed. * *---------------------------------------------------------------------- */ static void ChangeNodeToggleCount(nodePtr, tagPtr, delta) - register Node *nodePtr; /* Node whose toggle count for a tag - * must be changed. */ - TkTextTag *tagPtr; /* Information about tag. */ - int delta; /* Amount to add to current toggle - * count for tag (may be negative). */ + register Node *nodePtr; /* Node whose toggle count for a tag must be + * changed. */ + TkTextTag *tagPtr; /* Information about tag. */ + int delta; /* Amount to add to current toggle count for + * tag (may be negative). */ { register Summary *summaryPtr, *prevPtr; register Node *node2Ptr; - int rootLevel; /* Level of original tag root */ + int rootLevel; /* Level of original tag root */ tagPtr->toggleCount += delta; if (tagPtr->tagRootPtr == (Node *) NULL) { @@ -2263,8 +2253,8 @@ ChangeNodeToggleCount(nodePtr, tagPtr, delta) } /* - * Note the level of the existing root for the tag so we can detect - * if it needs to be moved because of the toggle count change. + * Note the level of the existing root for the tag so we can detect if it + * needs to be moved because of the toggle count change. */ rootLevel = tagPtr->tagRootPtr->level; @@ -2277,10 +2267,10 @@ ChangeNodeToggleCount(nodePtr, tagPtr, delta) for ( ; nodePtr != tagPtr->tagRootPtr; nodePtr = nodePtr->parentPtr) { /* - * See if there's already an entry for this tag for this node. If so, + * See if there's already an entry for this tag for this node. If so, * perhaps all we have to do is adjust its count. */ - + for (prevPtr = NULL, summaryPtr = nodePtr->summaryPtr; summaryPtr != NULL; prevPtr = summaryPtr, summaryPtr = summaryPtr->nextPtr) { @@ -2304,7 +2294,7 @@ ChangeNodeToggleCount(nodePtr, tagPtr, delta) Tcl_Panic("ChangeNodeToggleCount: bad toggle count (%d) max (%d)", summaryPtr->toggleCount, tagPtr->toggleCount); } - + /* * Zero toggle count; must remove this tag from the list. */ @@ -2319,20 +2309,19 @@ ChangeNodeToggleCount(nodePtr, tagPtr, delta) /* * This tag isn't currently in the summary information list. */ - + if (rootLevel == nodePtr->level) { - + /* * The old tag root is at the same level in the tree as this - * node, but it isn't at this node. Move the tag root up - * a level, in the hopes that it will now cover this node - * as well as the old root (if not, we'll move it up again - * the next time through the loop). To push it up one level - * we copy the original toggle count into the summary - * information at the old root and change the root to its - * parent node. + * node, but it isn't at this node. Move the tag root up a + * level, in the hopes that it will now cover this node as + * well as the old root (if not, we'll move it up again the + * next time through the loop). To push it up one level we + * copy the original toggle count into the summary information + * at the old root and change the root to its parent node. */ - + Node *rootNodePtr = tagPtr->tagRootPtr; summaryPtr = (Summary *) ckalloc(sizeof(Summary)); summaryPtr->tagPtr = tagPtr; @@ -2352,8 +2341,8 @@ ChangeNodeToggleCount(nodePtr, tagPtr, delta) } /* - * If we've decremented the toggle count, then it may be necessary - * to push the tag root down one or more levels. + * If we've decremented the toggle count, then it may be necessary to push + * the tag root down one or more levels. */ if (delta >= 0) { @@ -2366,8 +2355,8 @@ ChangeNodeToggleCount(nodePtr, tagPtr, delta) nodePtr = tagPtr->tagRootPtr; while (nodePtr->level > 0) { /* - * See if a single child node accounts for all of the tag's - * toggles. If so, push the root down one level. + * See if a single child node accounts for all of the tag's toggles. + * If so, push the root down one level. */ for (node2Ptr = nodePtr->children.nodePtr; @@ -2416,10 +2405,10 @@ ChangeNodeToggleCount(nodePtr, tagPtr, delta) * Find the start of the first range of a tag. * * Results: - * The return value is a pointer to the first tag toggle segment - * for the tag. This can be either a tagon or tagoff segments because - * of the way TkBTreeAdd removes a tag. - * Sets *indexPtr to be the index of the tag toggle. + * The return value is a pointer to the first tag toggle segment for the + * tag. This can be either a tagon or tagoff segments because of the way + * TkBTreeAdd removes a tag. Sets *indexPtr to be the index of the tag + * toggle. * * Side effects: * None. @@ -2429,9 +2418,9 @@ ChangeNodeToggleCount(nodePtr, tagPtr, delta) static TkTextSegment * FindTagStart(tree, tagPtr, indexPtr) - TkTextBTree tree; /* Tree to search within */ - TkTextTag *tagPtr; /* Tag to search for. */ - TkTextIndex *indexPtr; /* Return - index information */ + TkTextBTree tree; /* Tree to search within */ + TkTextTag *tagPtr; /* Tag to search for. */ + TkTextIndex *indexPtr; /* Return - index information */ { register Node *nodePtr; register TkTextLine *linePtr; @@ -2445,8 +2434,8 @@ FindTagStart(tree, tagPtr, indexPtr) } /* - * Search from the root of the subtree that contains the tag down - * to the level 0 node. + * Search from the root of the subtree that contains the tag down to the + * level 0 node. */ while (nodePtr->level > 0) { @@ -2459,7 +2448,7 @@ FindTagStart(tree, tagPtr, indexPtr) } } } - gotNodeWithTag: + gotNodeWithTag: continue; } @@ -2475,9 +2464,10 @@ FindTagStart(tree, tagPtr, indexPtr) || (segPtr->typePtr == &tkTextToggleOffType)) && (segPtr->body.toggle.tagPtr == tagPtr)) { /* - * It is possible that this is a tagoff tag, but that - * gets cleaned up later. + * It is possible that this is a tagoff tag, but that gets + * cleaned up later. */ + indexPtr->tree = tree; indexPtr->linePtr = linePtr; indexPtr->byteIndex = offset; @@ -2496,10 +2486,10 @@ FindTagStart(tree, tagPtr, indexPtr) * Find the end of the last range of a tag. * * Results: - * The return value is a pointer to the last tag toggle segment - * for the tag. This can be either a tagon or tagoff segments because - * of the way TkBTreeAdd removes a tag. - * Sets *indexPtr to be the index of the tag toggle. + * The return value is a pointer to the last tag toggle segment for the + * tag. This can be either a tagon or tagoff segments because of the way + * TkBTreeAdd removes a tag. Sets *indexPtr to be the index of the tag + * toggle. * * Side effects: * None. @@ -2509,9 +2499,9 @@ FindTagStart(tree, tagPtr, indexPtr) static TkTextSegment * FindTagEnd(tree, tagPtr, indexPtr) - TkTextBTree tree; /* Tree to search within */ - TkTextTag *tagPtr; /* Tag to search for. */ - TkTextIndex *indexPtr; /* Return - index information */ + TkTextBTree tree; /* Tree to search within */ + TkTextTag *tagPtr; /* Tag to search for. */ + TkTextIndex *indexPtr; /* Return - index information */ { register Node *nodePtr, *lastNodePtr; register TkTextLine *linePtr ,*lastLinePtr; @@ -2525,8 +2515,8 @@ FindTagEnd(tree, tagPtr, indexPtr) } /* - * Search from the root of the subtree that contains the tag down - * to the level 0 node. + * Search from the root of the subtree that contains the tag down to the + * level 0 node. */ while (nodePtr->level > 0) { @@ -2546,13 +2536,14 @@ FindTagEnd(tree, tagPtr, indexPtr) /* * Work through the lines attached to the level-0 node. */ + last2SegPtr = NULL; lastoffset2 = 0; lastoffset = 0; for (lastLinePtr = NULL, linePtr = nodePtr->children.linePtr; linePtr != (TkTextLine *) NULL; linePtr = linePtr->nextPtr) { for (offset = 0, lastSegPtr = NULL, segPtr = linePtr->segPtr ; - segPtr != NULL; + segPtr != NULL; offset += segPtr->size, segPtr = segPtr->nextPtr) { if (((segPtr->typePtr == &tkTextToggleOnType) || (segPtr->typePtr == &tkTextToggleOffType)) @@ -2578,19 +2569,19 @@ FindTagEnd(tree, tagPtr, indexPtr) * * TkBTreeStartSearch -- * - * This procedure sets up a search for tag transitions involving - * a given tag (or all tags) in a given range of the text. + * This function sets up a search for tag transitions involving a given + * tag (or all tags) in a given range of the text. * * Results: * None. * * Side effects: - * The information at *searchPtr is set up so that subsequent calls - * to TkBTreeNextTag or TkBTreePrevTag will return information about the - * locations of tag transitions. Note that TkBTreeNextTag or - * TkBTreePrevTag must be called to get the first transition. - * Note: unlike TkBTreeNextTag and TkBTreePrevTag, this routine does not - * guarantee that searchPtr->curIndex is equal to *index1Ptr. It may be + * The information at *searchPtr is set up so that subsequent calls to + * TkBTreeNextTag or TkBTreePrevTag will return information about the + * locations of tag transitions. Note that TkBTreeNextTag or + * TkBTreePrevTag must be called to get the first transition. Note: + * unlike TkBTreeNextTag and TkBTreePrevTag, this routine does not + * guarantee that searchPtr->curIndex is equal to *index1Ptr. It may be * greater than that if *index1Ptr is less than the first tag transition. * *---------------------------------------------------------------------- @@ -2598,31 +2589,30 @@ FindTagEnd(tree, tagPtr, indexPtr) void TkBTreeStartSearch(index1Ptr, index2Ptr, tagPtr, searchPtr) - TkTextIndex *index1Ptr; /* Search starts here. Tag toggles - * at this position will not be - * returned. */ - TkTextIndex *index2Ptr; /* Search stops here. Tag toggles - * at this position *will* be - * returned. */ - TkTextTag *tagPtr; /* Tag to search for. NULL means - * search for any tag. */ - register TkTextSearch *searchPtr; /* Where to store information about - * search's progress. */ + TkTextIndex *index1Ptr; /* Search starts here. Tag toggles at this + * position will not be returned. */ + TkTextIndex *index2Ptr; /* Search stops here. Tag toggles at this + * position *will* be returned. */ + TkTextTag *tagPtr; /* Tag to search for. NULL means search for + * any tag. */ + register TkTextSearch *searchPtr; + /* Where to store information about search's + * progress. */ { int offset; TkTextIndex index0; /* First index of the tag */ TkTextSegment *seg0Ptr; /* First segment of the tag */ /* - * Find the segment that contains the first toggle for the tag. This - * may become the starting point in the search. + * Find the segment that contains the first toggle for the tag. This may + * become the starting point in the search. */ seg0Ptr = FindTagStart(index1Ptr->tree, tagPtr, &index0); if (seg0Ptr == (TkTextSegment *) NULL) { /* - * Even though there are no toggles, the display code still - * uses the search curIndex, so initialize that anyway. + * Even though there are no toggles, the display code still uses the + * search curIndex, so initialize that anyway. */ searchPtr->linesLeft = 0; @@ -2655,14 +2645,15 @@ TkBTreeStartSearch(index1Ptr, index2Ptr, tagPtr, searchPtr) /* * Starting and stopping segments are in the same line; mark the * search as over immediately if the second segment is before the - * first. A search does not return a toggle at the very start of - * the range, unless the range is artificially moved up to index0. + * first. A search does not return a toggle at the very start of the + * range, unless the range is artificially moved up to index0. */ - if (((index1Ptr == &index0) && + + if (((index1Ptr == &index0) && (index1Ptr->byteIndex > index2Ptr->byteIndex)) || - ((index1Ptr != &index0) && + ((index1Ptr != &index0) && (index1Ptr->byteIndex >= index2Ptr->byteIndex))) { - searchPtr->linesLeft = 0; + searchPtr->linesLeft = 0; } } } @@ -2672,39 +2663,37 @@ TkBTreeStartSearch(index1Ptr, index2Ptr, tagPtr, searchPtr) * * TkBTreeStartSearchBack -- * - * This procedure sets up a search backwards for tag transitions involving - * a given tag (or all tags) in a given range of the text. In the - * normal case the first index (*index1Ptr) is beyond the second - * index (*index2Ptr). - * + * This function sets up a search backwards for tag transitions involving + * a given tag (or all tags) in a given range of the text. In the normal + * case the first index (*index1Ptr) is beyond the second index + * (*index2Ptr). * * Results: * None. * * Side effects: - * The information at *searchPtr is set up so that subsequent calls - * to TkBTreePrevTag will return information about the - * locations of tag transitions. Note that TkBTreePrevTag must be called - * to get the first transition. - * Note: unlike TkBTreeNextTag and TkBTreePrevTag, this routine does not - * guarantee that searchPtr->curIndex is equal to *index1Ptr. It may be - * less than that if *index1Ptr is greater than the last tag transition. + * The information at *searchPtr is set up so that subsequent calls to + * TkBTreePrevTag will return information about the locations of tag + * transitions. Note that TkBTreePrevTag must be called to get the first + * transition. Note: unlike TkBTreeNextTag and TkBTreePrevTag, this + * routine does not guarantee that searchPtr->curIndex is equal to + * *index1Ptr. It may be less than that if *index1Ptr is greater than the + * last tag transition. * *---------------------------------------------------------------------- */ void TkBTreeStartSearchBack(index1Ptr, index2Ptr, tagPtr, searchPtr) - TkTextIndex *index1Ptr; /* Search starts here. Tag toggles - * at this position will not be - * returned. */ - TkTextIndex *index2Ptr; /* Search stops here. Tag toggles - * at this position *will* be - * returned. */ - TkTextTag *tagPtr; /* Tag to search for. NULL means - * search for any tag. */ - register TkTextSearch *searchPtr; /* Where to store information about - * search's progress. */ + TkTextIndex *index1Ptr; /* Search starts here. Tag toggles at this + * position will not be returned. */ + TkTextIndex *index2Ptr; /* Search stops here. Tag toggles at this + * position *will* be returned. */ + TkTextTag *tagPtr; /* Tag to search for. NULL means search for + * any tag. */ + register TkTextSearch *searchPtr; + /* Where to store information about + * search's progress. */ { int offset; TkTextIndex index0; /* Last index of the tag */ @@ -2712,15 +2701,15 @@ TkBTreeStartSearchBack(index1Ptr, index2Ptr, tagPtr, searchPtr) TkTextSegment *seg0Ptr; /* Last segment of the tag */ /* - * Find the segment that contains the last toggle for the tag. This - * may become the starting point in the search. + * Find the segment that contains the last toggle for the tag. This may + * become the starting point in the search. */ seg0Ptr = FindTagEnd(index1Ptr->tree, tagPtr, &index0); if (seg0Ptr == (TkTextSegment *) NULL) { /* - * Even though there are no toggles, the display code still - * uses the search curIndex, so initialize that anyway. + * Even though there are no toggles, the display code still uses the + * search curIndex, so initialize that anyway. */ searchPtr->linesLeft = 0; @@ -2739,16 +2728,16 @@ TkBTreeStartSearchBack(index1Ptr, index2Ptr, tagPtr, searchPtr) searchPtr->curIndex = index0; index1Ptr = &index0; } else { - TkTextIndexBackChars(NULL, index1Ptr, 1, &searchPtr->curIndex, - COUNT_INDICES); + TkTextIndexBackChars(NULL, index1Ptr, 1, &searchPtr->curIndex, + COUNT_INDICES); } searchPtr->segPtr = NULL; searchPtr->nextPtr = TkTextIndexToSeg(&searchPtr->curIndex, &offset); searchPtr->curIndex.byteIndex -= offset; /* - * Adjust the end of the search so it does find toggles that are right - * at the second index specified by the user. + * Adjust the end of the search so it does find toggles that are right at + * the second index specified by the user. */ if ((TkBTreeLinesTo(NULL, index2Ptr->linePtr) == 0) && @@ -2781,18 +2770,18 @@ TkBTreeStartSearchBack(index1Ptr, index2Ptr, tagPtr, searchPtr) * * TkBTreeNextTag -- * - * Once a tag search has begun, successive calls to this procedure - * return successive tag toggles. Note: it is NOT SAFE to call this - * procedure if characters have been inserted into or deleted from - * the B-tree since the call to TkBTreeStartSearch. + * Once a tag search has begun, successive calls to this function return + * successive tag toggles. Note: it is NOT SAFE to call this function if + * characters have been inserted into or deleted from the B-tree since + * the call to TkBTreeStartSearch. * * Results: * The return value is 1 if another toggle was found that met the - * criteria specified in the call to TkBTreeStartSearch; in this - * case searchPtr->curIndex gives the toggle's position and - * searchPtr->curTagPtr points to its segment. 0 is returned if - * no more matching tag transitions were found; in this case - * searchPtr->curIndex is the same as searchPtr->stopIndex. + * criteria specified in the call to TkBTreeStartSearch; in this case + * searchPtr->curIndex gives the toggle's position and + * searchPtr->curTagPtr points to its segment. 0 is returned if no more + * matching tag transitions were found; in this case searchPtr->curIndex + * is the same as searchPtr->stopIndex. * * Side effects: * Information in *searchPtr is modified to update the state of the @@ -2803,9 +2792,10 @@ TkBTreeStartSearchBack(index1Ptr, index2Ptr, tagPtr, searchPtr) int TkBTreeNextTag(searchPtr) - register TkTextSearch *searchPtr; /* Information about search in - * progress; must have been set up by - * call to TkBTreeStartSearch. */ + register TkTextSearch *searchPtr; + /* Information about search in progress; must + * have been set up by call to + * TkBTreeStartSearch. */ { register TkTextSegment *segPtr; register Node *nodePtr; @@ -2816,9 +2806,9 @@ TkBTreeNextTag(searchPtr) } /* - * The outermost loop iterates over lines that may potentially contain - * a relevant tag transition, starting from the current segment in - * the current line. + * The outermost loop iterates over lines that may potentially contain a + * relevant tag transition, starting from the current segment in the + * current line. */ segPtr = searchPtr->nextPtr; @@ -2842,11 +2832,10 @@ TkBTreeNextTag(searchPtr) } searchPtr->curIndex.byteIndex += segPtr->size; } - + /* * See if there are more lines associated with the current parent - * node. If so, go back to the top of the loop to search the next - * one. + * node. If so, go back to the top of the loop to search the next one. */ nodePtr = searchPtr->curIndex.linePtr->parentPtr; @@ -2863,14 +2852,14 @@ TkBTreeNextTag(searchPtr) if (nodePtr == searchPtr->tagPtr->tagRootPtr) { goto searchOver; } - + /* * Search across and up through the B-tree's node hierarchy looking * for the next node that has a relevant tag transition somewhere in - * its subtree. Be sure to update linesLeft as we skip over large + * its subtree. Be sure to update linesLeft as we skip over large * chunks of lines. */ - + while (1) { while (nodePtr->nextPtr == NULL) { if (nodePtr->parentPtr == NULL || @@ -2889,14 +2878,14 @@ TkBTreeNextTag(searchPtr) } searchPtr->linesLeft -= nodePtr->numLines; } - + /* * At this point we've found a subtree that has a relevant tag - * transition. Now search down (and across) through that subtree - * to find the first level-0 node that has a relevant tag transition. + * transition. Now search down (and across) through that subtree to + * find the first level-0 node that has a relevant tag transition. */ - - gotNodeWithTag: + + gotNodeWithTag: while (nodePtr->level > 0) { for (nodePtr = nodePtr->children.nodePtr; ; nodePtr = nodePtr->nextPtr) { @@ -2904,6 +2893,10 @@ TkBTreeNextTag(searchPtr) summaryPtr = summaryPtr->nextPtr) { if ((searchPtr->allTags) || (summaryPtr->tagPtr == searchPtr->tagPtr)) { + /* + * Would really like a multi-level continue here... + */ + goto nextChild; } } @@ -2912,13 +2905,13 @@ TkBTreeNextTag(searchPtr) Tcl_Panic("TkBTreeNextTag found incorrect tag summary info."); } } - nextChild: + nextChild: continue; } - + /* * Now we're down to a level-0 node that contains a line that contains - * a relevant tag transition. Set up line information and go back to + * a relevant tag transition. Set up line information and go back to * the beginning of the loop to search through lines. */ @@ -2931,7 +2924,7 @@ TkBTreeNextTag(searchPtr) continue; } - searchOver: + searchOver: searchPtr->linesLeft = 0; searchPtr->segPtr = NULL; return 0; @@ -2942,19 +2935,18 @@ TkBTreeNextTag(searchPtr) * * TkBTreePrevTag -- * - * Once a tag search has begun, successive calls to this procedure - * return successive tag toggles in the reverse direction. - * Note: it is NOT SAFE to call this - * procedure if characters have been inserted into or deleted from - * the B-tree since the call to TkBTreeStartSearch. + * Once a tag search has begun, successive calls to this function return + * successive tag toggles in the reverse direction. Note: it is NOT SAFE + * to call this function if characters have been inserted into or deleted + * from the B-tree since the call to TkBTreeStartSearch. * * Results: * The return value is 1 if another toggle was found that met the - * criteria specified in the call to TkBTreeStartSearch; in this - * case searchPtr->curIndex gives the toggle's position and - * searchPtr->curTagPtr points to its segment. 0 is returned if - * no more matching tag transitions were found; in this case - * searchPtr->curIndex is the same as searchPtr->stopIndex. + * criteria specified in the call to TkBTreeStartSearch; in this case + * searchPtr->curIndex gives the toggle's position and + * searchPtr->curTagPtr points to its segment. 0 is returned if no more + * matching tag transitions were found; in this case searchPtr->curIndex + * is the same as searchPtr->stopIndex. * * Side effects: * Information in *searchPtr is modified to update the state of the @@ -2965,16 +2957,17 @@ TkBTreeNextTag(searchPtr) int TkBTreePrevTag(searchPtr) - register TkTextSearch *searchPtr; /* Information about search in - * progress; must have been set up by - * call to TkBTreeStartSearch. */ + register TkTextSearch *searchPtr; + /* Information about search in progress; must + * have been set up by call to + * TkBTreeStartSearch. */ { register TkTextSegment *segPtr, *prevPtr; register TkTextLine *linePtr, *prevLinePtr; register Node *nodePtr, *node2Ptr, *prevNodePtr; register Summary *summaryPtr; int byteIndex; - int pastLast; /* Saw last marker during scan */ + int pastLast; /* Saw last marker during scan */ int linesSkipped; if (searchPtr->linesLeft <= 0) { @@ -2982,25 +2975,28 @@ TkBTreePrevTag(searchPtr) } /* - * The outermost loop iterates over lines that may potentially contain - * a relevant tag transition, starting from the current segment in - * the current line. "nextPtr" is maintained as the last segment in - * a line that we can look at. + * The outermost loop iterates over lines that may potentially contain a + * relevant tag transition, starting from the current segment in the + * current line. "nextPtr" is maintained as the last segment in a line + * that we can look at. */ while (1) { /* * Check for the last toggle before the current segment on this line. */ + byteIndex = 0; if (searchPtr->lastPtr == NULL) { - /* + /* * Search back to the very beginning, so pastLast is irrelevent. */ - pastLast = 1; + + pastLast = 1; } else { pastLast = 0; } + for (prevPtr = NULL, segPtr = searchPtr->curIndex.linePtr->segPtr ; segPtr != NULL && segPtr != searchPtr->nextPtr; segPtr = segPtr->nextPtr) { @@ -3012,7 +3008,7 @@ TkBTreePrevTag(searchPtr) searchPtr->curIndex.byteIndex = byteIndex; } if (segPtr == searchPtr->lastPtr) { - prevPtr = NULL; /* Segments earlier than last don't count */ + prevPtr = NULL; /* Segments earlier than last don't count */ pastLast = 1; } byteIndex += segPtr->size; @@ -3020,9 +3016,10 @@ TkBTreePrevTag(searchPtr) if (prevPtr != NULL) { if (searchPtr->linesLeft == 1 && !pastLast) { /* - * We found a segment that is before the stopping index. - * Note that it is OK if prevPtr == lastPtr. + * We found a segment that is before the stopping index. Note + * that it is OK if prevPtr == lastPtr. */ + goto searchOver; } searchPtr->segPtr = prevPtr; @@ -3030,7 +3027,7 @@ TkBTreePrevTag(searchPtr) searchPtr->tagPtr = prevPtr->body.toggle.tagPtr; return 1; } - + searchPtr->linesLeft--; if (searchPtr->linesLeft <= 0) { goto searchOver; @@ -3038,7 +3035,7 @@ TkBTreePrevTag(searchPtr) /* * See if there are more lines associated with the current parent - * node. If so, go back to the top of the loop to search the previous + * node. If so, go back to the top of the loop to search the previous * one. */ @@ -3056,17 +3053,17 @@ TkBTreePrevTag(searchPtr) if (nodePtr == searchPtr->tagPtr->tagRootPtr) { goto searchOver; } - + /* * Search across and up through the B-tree's node hierarchy looking - * for the previous node that has a relevant tag transition somewhere in - * its subtree. The search and line counting is trickier with/out - * back pointers. We'll scan all the nodes under a parent up to - * the current node, searching all of them for tag state. The last - * one we find, if any, is recorded in prevNodePtr, and any nodes - * past prevNodePtr that don't have tag state increment linesSkipped. + * for the previous node that has a relevant tag transition somewhere + * in its subtree. The search and line counting is trickier with/out + * back pointers. We'll scan all the nodes under a parent up to the + * current node, searching all of them for tag state. The last one we + * find, if any, is recorded in prevNodePtr, and any nodes past + * prevNodePtr that don't have tag state increment linesSkipped. */ - + while (1) { for (prevNodePtr = NULL, linesSkipped = 0, node2Ptr = nodePtr->parentPtr->children.nodePtr ; @@ -3082,7 +3079,7 @@ TkBTreePrevTag(searchPtr) } linesSkipped += node2Ptr->numLines; - keepLooking: + keepLooking: continue; } if (prevNodePtr != NULL) { @@ -3096,14 +3093,14 @@ TkBTreePrevTag(searchPtr) goto searchOver; } } - + /* * At this point we've found a subtree that has a relevant tag - * transition. Now search down (and across) through that subtree - * to find the last level-0 node that has a relevant tag transition. + * transition. Now search down (and across) through that subtree to + * find the last level-0 node that has a relevant tag transition. */ - - gotNodeWithTag: + + gotNodeWithTag: while (nodePtr->level > 0) { for (linesSkipped = 0, prevNodePtr = NULL, nodePtr = nodePtr->children.nodePtr; nodePtr != NULL ; @@ -3119,7 +3116,7 @@ TkBTreePrevTag(searchPtr) } linesSkipped += nodePtr->numLines; - keepLooking2: + keepLooking2: continue; } if (prevNodePtr == NULL) { @@ -3128,11 +3125,11 @@ TkBTreePrevTag(searchPtr) searchPtr->linesLeft -= linesSkipped; nodePtr = prevNodePtr; } - + /* * Now we're down to a level-0 node that contains a line that contains - * a relevant tag transition. Set up line information and go back to - * the beginning of the loop to search through lines. We start with + * a relevant tag transition. Set up line information and go back to + * the beginning of the loop to search through lines. We start with * the last line below the node. */ @@ -3149,7 +3146,7 @@ TkBTreePrevTag(searchPtr) continue; } - searchOver: + searchOver: searchPtr->linesLeft = 0; searchPtr->segPtr = NULL; return 0; @@ -3163,8 +3160,8 @@ TkBTreePrevTag(searchPtr) * Determine whether a particular character has a particular tag. * * Results: - * The return value is 1 if the given tag is in effect at the - * character given by linePtr and ch, and 0 otherwise. + * The return value is 1 if the given tag is in effect at the character + * given by linePtr and ch, and 0 otherwise. * * Side effects: * None. @@ -3174,9 +3171,9 @@ TkBTreePrevTag(searchPtr) int TkBTreeCharTagged(indexPtr, tagPtr) - CONST TkTextIndex *indexPtr; /* Indicates a character position at - * which to check for a tag. */ - TkTextTag *tagPtr; /* Tag of interest. */ + CONST TkTextIndex *indexPtr;/* Indicates a character position at which to + * check for a tag. */ + TkTextTag *tagPtr; /* Tag of interest. */ { register Node *nodePtr; register TkTextLine *siblingLinePtr; @@ -3184,10 +3181,10 @@ TkBTreeCharTagged(indexPtr, tagPtr) TkTextSegment *toggleSegPtr; int toggles, index; - /* - * Check for toggles for the tag in indexPtr's line but before - * indexPtr. If there is one, its type indicates whether or - * not the character is tagged. + /* + * Check for toggles for the tag in indexPtr's line but before indexPtr. + * If there is one, its type indicates whether or not the character is + * tagged. */ toggleSegPtr = NULL; @@ -3205,9 +3202,8 @@ TkBTreeCharTagged(indexPtr, tagPtr) } /* - * No toggle in this line. Look for toggles for the tag in lines - * that are predecessors of indexPtr->linePtr but under the same - * level-0 node. + * No toggle in this line. Look for toggles for the tag in lines that are + * predecessors of indexPtr->linePtr but under the same level-0 node. */ for (siblingLinePtr = indexPtr->linePtr->parentPtr->children.linePtr; @@ -3227,9 +3223,9 @@ TkBTreeCharTagged(indexPtr, tagPtr) } /* - * No toggle in this node. Scan upwards through the ancestors of - * this node, counting the number of toggles of the given tag in - * siblings that precede that node. + * No toggle in this node. Scan upwards through the ancestors of this + * node, counting the number of toggles of the given tag in siblings that + * precede that node. */ toggles = 0; @@ -3238,7 +3234,7 @@ TkBTreeCharTagged(indexPtr, tagPtr) register Node *siblingPtr; register Summary *summaryPtr; - for (siblingPtr = nodePtr->parentPtr->children.nodePtr; + for (siblingPtr = nodePtr->parentPtr->children.nodePtr; siblingPtr != nodePtr; siblingPtr = siblingPtr->nextPtr) { for (summaryPtr = siblingPtr->summaryPtr; summaryPtr != NULL; summaryPtr = summaryPtr->nextPtr) { @@ -3253,8 +3249,8 @@ TkBTreeCharTagged(indexPtr, tagPtr) } /* - * An odd number of toggles means that the tag is present at the - * given point. + * An odd number of toggles means that the tag is present at the given + * point. */ return toggles & 1; @@ -3265,17 +3261,17 @@ TkBTreeCharTagged(indexPtr, tagPtr) * * TkBTreeGetTags -- * - * Return information about all of the tags that are associated - * with a particular character in a B-tree of text. + * Return information about all of the tags that are associated with a + * particular character in a B-tree of text. * * Results: * The return value is a malloc-ed array containing pointers to - * information for each of the tags that is associated with - * the character at the position given by linePtr and ch. The - * word at *numTagsPtr is filled in with the number of pointers - * in the array. It is up to the caller to free the array by - * passing it to free. If there are no tags at the given character - * then a NULL pointer is returned and *numTagsPtr will be set to 0. + * information for each of the tags that is associated with the character + * at the position given by linePtr and ch. The word at *numTagsPtr is + * filled in with the number of pointers in the array. It is up to the + * caller to free the array by passing it to free. If there are no tags + * at the given character then a NULL pointer is returned and *numTagsPtr + * will be set to 0. * * Side effects: * None. @@ -3286,11 +3282,11 @@ TkBTreeCharTagged(indexPtr, tagPtr) /* ARGSUSED */ TkTextTag ** TkBTreeGetTags(indexPtr, textPtr, numTagsPtr) - CONST TkTextIndex *indexPtr;/* Indicates a particular position in - * the B-tree. */ - CONST TkText *textPtr; /* If non-NULL, then only return tags - * for this text widget (when there are - * peer widgets). */ + CONST TkTextIndex *indexPtr;/* Indicates a particular position in the + * B-tree. */ + CONST TkText *textPtr; /* If non-NULL, then only return tags for this + * text widget (when there are peer + * widgets). */ int *numTagsPtr; /* Store number of tags found at this * location. */ { @@ -3310,8 +3306,7 @@ TkBTreeGetTags(indexPtr, textPtr, numTagsPtr) NUM_TAG_INFOS*sizeof(int)); /* - * Record tag toggles within the line of indexPtr but preceding - * indexPtr. + * Record tag toggles within the line of indexPtr but preceding indexPtr. */ linePtr = indexPtr->linePtr; @@ -3324,12 +3319,13 @@ TkBTreeGetTags(indexPtr, textPtr, numTagsPtr) } index += segPtr->size; segPtr = segPtr->nextPtr; - + if (segPtr == NULL) { - /* - * Two logical lines merged into one display line - * through eliding of a newline + /* + * Two logical lines merged into one display line through eliding + * of a newline. */ + linePtr = TkBTreeNextLine(NULL, linePtr); segPtr = linePtr->segPtr; } @@ -3353,8 +3349,8 @@ TkBTreeGetTags(indexPtr, textPtr, numTagsPtr) } /* - * For each node in the ancestry of this line, record tag toggles - * for all siblings that precede that node. + * For each node in the ancestry of this line, record tag toggles for all + * siblings that precede that node. */ for (nodePtr = indexPtr->linePtr->parentPtr; nodePtr->parentPtr != NULL; @@ -3362,7 +3358,7 @@ TkBTreeGetTags(indexPtr, textPtr, numTagsPtr) register Node *siblingPtr; register Summary *summaryPtr; - for (siblingPtr = nodePtr->parentPtr->children.nodePtr; + for (siblingPtr = nodePtr->parentPtr->children.nodePtr; siblingPtr != nodePtr; siblingPtr = siblingPtr->nextPtr) { for (summaryPtr = siblingPtr->summaryPtr; summaryPtr != NULL; summaryPtr = summaryPtr->nextPtr) { @@ -3375,10 +3371,10 @@ TkBTreeGetTags(indexPtr, textPtr, numTagsPtr) } /* - * Go through the tag information and squash out all of the tags - * that have even toggle counts (these tags exist before the point - * of interest, but not at the desired character itself). Also - * squash out all tags that don't belong to the requested widget. + * Go through the tag information and squash out all of the tags that have + * even toggle counts (these tags exist before the point of interest, but + * not at the desired character itself). Also squash out all tags that + * don't belong to the requested widget. */ for (src = 0, dst = 0; src < tagInfo.numTags; src++) { @@ -3406,24 +3402,22 @@ TkBTreeGetTags(indexPtr, textPtr, numTagsPtr) * * Special case to just return information about elided attribute. * Specialized from TkBTreeGetTags(indexPtr, textPtr, numTagsPtr) and - * GetStyle(textPtr, indexPtr). Just need to keep track of - * invisibility settings for each priority, pick highest one active - * at end. - * - * Note that this returns all elide information up to and including - * the given index (quite obviously). However, this does mean that - * if indexPtr is a line-start and one then iterates from the beginning - * of that line forwards, one will actually revisit the segPtrs of - * size zero (for tag toggling, for example) which have already been - * seen here. - * - * For this reason we fill in the fields 'segPtr' and 'segOffset' - * of elideInfo, enabling our caller easily to calculate - * incremental changes from where we left off. + * GetStyle(textPtr, indexPtr). Just need to keep track of invisibility + * settings for each priority, pick highest one active at end. + * + * Note that this returns all elide information up to and including the + * given index (quite obviously). However, this does mean that if + * indexPtr is a line-start and one then iterates from the beginning of + * that line forwards, one will actually revisit the segPtrs of size zero + * (for tag toggling, for example) which have already been seen here. + * + * For this reason we fill in the fields 'segPtr' and 'segOffset' of + * elideInfo, enabling our caller easily to calculate incremental changes + * from where we left off. * * Results: * Returns whether this text should be elided or not. - * + * * Optionally returns more detailed information in elideInfo. * * Side effects: @@ -3436,11 +3430,11 @@ TkBTreeGetTags(indexPtr, textPtr, numTagsPtr) int TkTextIsElided(textPtr, indexPtr, elideInfo) CONST TkText *textPtr; /* Overall information about text widget. */ - CONST TkTextIndex *indexPtr;/* The character in the text for which - * display information is wanted. */ - TkTextElideInfo *elideInfo; /* NULL or a pointer to a structure in - * which indexPtr's elide state will - * be stored and returned. */ + CONST TkTextIndex *indexPtr;/* The character in the text for which display + * information is wanted. */ + TkTextElideInfo *elideInfo; /* NULL or a pointer to a structure in which + * indexPtr's elide state will be stored and + * returned. */ { register Node *nodePtr; register TkTextLine *siblingLinePtr; @@ -3450,33 +3444,35 @@ TkTextIsElided(textPtr, indexPtr, elideInfo) register TkTextElideInfo *infoPtr; TkTextLine *linePtr; int elide; - + if (elideInfo == NULL) { infoPtr = (TkTextElideInfo*)ckalloc((unsigned)sizeof(TkTextElideInfo)); } else { infoPtr = elideInfo; } - + infoPtr->elide = 0; /* if nobody says otherwise, it's visible */ infoPtr->tagCnts = infoPtr->deftagCnts; infoPtr->tagPtrs = infoPtr->deftagPtrs; infoPtr->numTags = textPtr->sharedTextPtr->numTags; - /* Almost always avoid malloc, so stay out of system calls */ + /* + * Almost always avoid malloc, so stay out of system calls. + */ + if (LOTSA_TAGS < infoPtr->numTags) { - infoPtr->tagCnts = (int *)ckalloc((unsigned)sizeof(int) - * infoPtr->numTags); - infoPtr->tagPtrs = (TkTextTag **)ckalloc((unsigned)sizeof(TkTextTag*) - * infoPtr->numTags); + infoPtr->tagCnts = (int *) + ckalloc((unsigned) sizeof(int) * infoPtr->numTags); + infoPtr->tagPtrs = (TkTextTag **) + ckalloc((unsigned)sizeof(TkTextTag *) * infoPtr->numTags); } - + for (i=0; i<infoPtr->numTags; i++) { infoPtr->tagCnts[i] = 0; } /* - * Record tag toggles within the line of indexPtr but preceding - * indexPtr. + * Record tag toggles within the line of indexPtr but preceding indexPtr. */ index = 0; @@ -3491,35 +3487,38 @@ TkTextIsElided(textPtr, indexPtr, elideInfo) infoPtr->tagCnts[tagPtr->priority]++; } } - + index += segPtr->size; segPtr = segPtr->nextPtr; if (segPtr == NULL) { - /* - * Two logical lines merged into one display line - * through eliding of a newline + /* + * Two logical lines merged into one display line through eliding + * of a newline. */ + linePtr = TkBTreeNextLine(NULL, linePtr); segPtr = linePtr->segPtr; } } - /* - * Store the first segPtr we haven't examined completely - * so that our caller knows where to start. + + /* + * Store the first segPtr we haven't examined completely so that our + * caller knows where to start. */ + infoPtr->segPtr = segPtr; infoPtr->segOffset = index; - + /* * Record toggles for tags in lines that are predecessors of * indexPtr->linePtr but under the same level-0 node. */ for (siblingLinePtr = indexPtr->linePtr->parentPtr->children.linePtr; - siblingLinePtr != indexPtr->linePtr; - siblingLinePtr = siblingLinePtr->nextPtr) { + siblingLinePtr != indexPtr->linePtr; + siblingLinePtr = siblingLinePtr->nextPtr) { for (segPtr = siblingLinePtr->segPtr; segPtr != NULL; - segPtr = segPtr->nextPtr) { + segPtr = segPtr->nextPtr) { if ((segPtr->typePtr == &tkTextToggleOnType) || (segPtr->typePtr == &tkTextToggleOffType)) { tagPtr = segPtr->body.toggle.tagPtr; @@ -3532,24 +3531,25 @@ TkTextIsElided(textPtr, indexPtr, elideInfo) } /* - * For each node in the ancestry of this line, record tag toggles - * for all siblings that precede that node. + * For each node in the ancestry of this line, record tag toggles for all + * siblings that precede that node. */ for (nodePtr = indexPtr->linePtr->parentPtr; nodePtr->parentPtr != NULL; - nodePtr = nodePtr->parentPtr) { + nodePtr = nodePtr->parentPtr) { register Node *siblingPtr; register Summary *summaryPtr; - for (siblingPtr = nodePtr->parentPtr->children.nodePtr; - siblingPtr != nodePtr; siblingPtr = siblingPtr->nextPtr) { + for (siblingPtr = nodePtr->parentPtr->children.nodePtr; + siblingPtr != nodePtr; siblingPtr = siblingPtr->nextPtr) { for (summaryPtr = siblingPtr->summaryPtr; summaryPtr != NULL; - summaryPtr = summaryPtr->nextPtr) { + summaryPtr = summaryPtr->nextPtr) { if (summaryPtr->toggleCount & 1) { tagPtr = summaryPtr->tagPtr; if (tagPtr->elideString != NULL) { infoPtr->tagPtrs[tagPtr->priority] = tagPtr; - infoPtr->tagCnts[tagPtr->priority] += summaryPtr->toggleCount; + infoPtr->tagCnts[tagPtr->priority] += + summaryPtr->toggleCount; } } } @@ -3557,21 +3557,28 @@ TkTextIsElided(textPtr, indexPtr, elideInfo) } /* - * Now traverse from highest priority to lowest, - * take elided value from first odd count (= on) + * Now traverse from highest priority to lowest, take elided value from + * first odd count (= on) */ infoPtr->elidePriority = -1; for (i = infoPtr->numTags-1; i >=0; i--) { if (infoPtr->tagCnts[i] & 1) { - /* Who would make the selection elided? */ + /* + * Who would make the selection elided? + */ + if ((tagPtr == textPtr->selTagPtr) && !(textPtr->flags & GOT_FOCUS) && (textPtr->inactiveSelBorder == NULL)) { continue; } infoPtr->elide = infoPtr->tagPtrs[i]->elide; - /* Note: i == infoPtr->tagPtrs[i]->priority */ + + /* + * Note: i == infoPtr->tagPtrs[i]->priority + */ + infoPtr->elidePriority = i; break; } @@ -3580,11 +3587,11 @@ TkTextIsElided(textPtr, indexPtr, elideInfo) elide = infoPtr->elide; if (elideInfo == NULL) { - if (LOTSA_TAGS < infoPtr->numTags) { + if (LOTSA_TAGS < infoPtr->numTags) { ckfree((char *) infoPtr->tagCnts); ckfree((char *) infoPtr->tagPtrs); } - + ckfree((char*) infoPtr); } @@ -3596,8 +3603,8 @@ TkTextIsElided(textPtr, indexPtr, elideInfo) * * TkTextFreeElideInfo -- * - * This is a utility procedure used to free up any memory - * allocated by the TkTextIsElided function above. + * This is a utility function used to free up any memory allocated by the + * TkTextIsElided function above. * * Results: * None. @@ -3611,7 +3618,7 @@ TkTextIsElided(textPtr, indexPtr, elideInfo) void TkTextFreeElideInfo(elideInfo) TkTextElideInfo *elideInfo; /* Free any allocated memory in this - * structure. */ + * structure. */ { if (LOTSA_TAGS < elideInfo->numTags) { ckfree((char*)elideInfo->tagCnts); @@ -3624,16 +3631,16 @@ TkTextFreeElideInfo(elideInfo) * * IncCount -- * - * This is a utility procedure used by TkBTreeGetTags. It - * increments the count for a particular tag, adding a new - * entry for that tag if there wasn't one previously. + * This is a utility function used by TkBTreeGetTags. It increments the + * count for a particular tag, adding a new entry for that tag if there + * wasn't one previously. * * Results: * None. * * Side effects: - * The information at *tagInfoPtr may be modified, and the arrays - * may be reallocated to make them larger. + * The information at *tagInfoPtr may be modified, and the arrays may be + * reallocated to make them larger. * *---------------------------------------------------------------------- */ @@ -3657,9 +3664,8 @@ IncCount(tagPtr, inc, tagInfoPtr) } /* - * There isn't currently an entry for this tag, so we have to - * make a new one. If the arrays are full, then enlarge the - * arrays first. + * There isn't currently an entry for this tag, so we have to make a new + * one. If the arrays are full, then enlarge the arrays first. */ if (tagInfoPtr->numTags == tagInfoPtr->arraySize) { @@ -3668,7 +3674,7 @@ IncCount(tagPtr, inc, tagInfoPtr) newSize = 2*tagInfoPtr->arraySize; newTags = (TkTextTag **) ckalloc((unsigned) - (newSize*sizeof(TkTextTag *))); + (newSize * sizeof(TkTextTag *))); memcpy((VOID *) newTags, (VOID *) tagInfoPtr->tagPtrs, tagInfoPtr->arraySize * sizeof(TkTextTag *)); ckfree((char *) tagInfoPtr->tagPtrs); @@ -3691,15 +3697,15 @@ IncCount(tagPtr, inc, tagInfoPtr) * * TkBTreeCheck -- * - * This procedure runs a set of consistency checks over a B-tree - * and panics if any inconsistencies are found. + * This function runs a set of consistency checks over a B-tree and + * panics if any inconsistencies are found. * * Results: * None. * * Side effects: - * If a structural defect is found, the procedure panics with an - * error message. + * If a structural defect is found, the function panics with an error + * message. * *---------------------------------------------------------------------- */ @@ -3721,14 +3727,15 @@ TkBTreeCheck(tree) /* * Make sure that the tag toggle counts and the tag root pointers are OK. */ - for (entryPtr = Tcl_FirstHashEntry(&treePtr->sharedTextPtr->tagTable, &search); + + for (entryPtr=Tcl_FirstHashEntry(&treePtr->sharedTextPtr->tagTable,&search); entryPtr != NULL ; entryPtr = Tcl_NextHashEntry(&search)) { tagPtr = (TkTextTag *) Tcl_GetHashValue(entryPtr); nodePtr = tagPtr->tagRootPtr; if (nodePtr == (Node *) NULL) { if (tagPtr->toggleCount != 0) { Tcl_Panic("TkBTreeCheck found \"%s\" with toggles (%d) but no root", - tagPtr->name, tagPtr->toggleCount); + tagPtr->name, tagPtr->toggleCount); } continue; /* no ranges for the tag */ } else if (tagPtr->toggleCount == 0) { @@ -3770,20 +3777,20 @@ TkBTreeCheck(tree) } if (count != tagPtr->toggleCount) { Tcl_Panic("TkBTreeCheck toggleCount (%d) wrong for \"%s\" should be (%d)", - tagPtr->toggleCount, tagPtr->name, count); + tagPtr->toggleCount, tagPtr->name, count); } } /* - * Call a recursive procedure to do the main body of checks. + * Call a recursive function to do the main body of checks. */ nodePtr = treePtr->rootPtr; CheckNodeConsistency(treePtr->rootPtr, treePtr->pixelReferences); /* - * Make sure that there are at least two lines in the text and - * that the last line has no characters except a newline. + * Make sure that there are at least two lines in the text and that the + * last line has no characters except a newline. */ if (nodePtr->numLines < 2) { @@ -3804,9 +3811,8 @@ TkBTreeCheck(tree) || (segPtr->typePtr == &tkTextRightMarkType) || (segPtr->typePtr == &tkTextLeftMarkType)) { /* - * It's OK to toggle a tag off in the last line, but - * not to start a new range. It's also OK to have marks - * in the last line. + * It's OK to toggle a tag off in the last line, but not to start a + * new range. It's also OK to have marks in the last line. */ segPtr = segPtr->nextPtr; @@ -3832,26 +3838,25 @@ TkBTreeCheck(tree) * * CheckNodeConsistency -- * - * This procedure is called as part of consistency checking for - * B-trees: it checks several aspects of a node and also runs - * checks recursively on the node's children. + * This function is called as part of consistency checking for B-trees: + * it checks several aspects of a node and also runs checks recursively + * on the node's children. * * Results: * None. * * Side effects: - * If anything suspicious is found in the tree structure, the - * procedure panics. + * If anything suspicious is found in the tree structure, the function + * panics. * *---------------------------------------------------------------------- */ static void CheckNodeConsistency(nodePtr, references) - register Node *nodePtr; /* Node whose subtree should be - * checked. */ - int references; /* Number of referring widgets - * which have pixel counts. */ + register Node *nodePtr; /* Node whose subtree should be checked. */ + int references; /* Number of referring widgets which have + * pixel counts. */ { register Node *childNodePtr; register Summary *summaryPtr, *summaryPtr2; @@ -3913,7 +3918,7 @@ CheckNodeConsistency(nodePtr, references) numChildren++; numLines++; for (i = 0; i<references; i++) { - numPixels[i] += linePtr->pixels[2*i]; + numPixels[i] += linePtr->pixels[2*i]; } } } else { @@ -3968,7 +3973,7 @@ CheckNodeConsistency(nodePtr, references) if (references > PIXEL_CLIENTS) { ckfree((char*)numPixels); } - + for (summaryPtr = nodePtr->summaryPtr; summaryPtr != NULL; summaryPtr = summaryPtr->nextPtr) { if (summaryPtr->tagPtr->toggleCount == summaryPtr->toggleCount) { @@ -4022,9 +4027,9 @@ CheckNodeConsistency(nodePtr, references) * * Rebalance -- * - * This procedure is called when a node of a B-tree appears to be - * out of balance (too many children, or too few). It rebalances - * that node and all of its ancestors in the tree. + * This function is called when a node of a B-tree appears to be out of + * balance (too many children, or too few). It rebalances that node and + * all of its ancestors in the tree. * * Results: * None. @@ -4037,13 +4042,12 @@ CheckNodeConsistency(nodePtr, references) static void Rebalance(treePtr, nodePtr) - BTree *treePtr; /* Tree that is being rebalanced. */ - register Node *nodePtr; /* Node that may be out of balance. */ + BTree *treePtr; /* Tree that is being rebalanced. */ + register Node *nodePtr; /* Node that may be out of balance. */ { /* - * Loop over the entire ancestral chain of the node, working up - * through the tree one node at a time until the root node has - * been processed. + * Loop over the entire ancestral chain of the node, working up through + * the tree one node at a time until the root node has been processed. */ for ( ; nodePtr != NULL; nodePtr = nodePtr->parentPtr) { @@ -4052,19 +4056,19 @@ Rebalance(treePtr, nodePtr) int i; /* - * Check to see if the node has too many children. If it does, - * then split off all but the first MIN_CHILDREN into a separate - * node following the original one. Then repeat until the - * node has a decent size. + * Check to see if the node has too many children. If it does, then + * split off all but the first MIN_CHILDREN into a separate node + * following the original one. Then repeat until the node has a decent + * size. */ if (nodePtr->numChildren > MAX_CHILDREN) { while (1) { /* - * If the node being split is the root node, then make a - * new root node above it first. + * If the node being split is the root node, then make a new + * root node above it first. */ - + if (nodePtr->parentPtr == NULL) { newPtr = (Node *) ckalloc(sizeof(Node)); newPtr->parentPtr = NULL; @@ -4074,17 +4078,17 @@ Rebalance(treePtr, nodePtr) newPtr->children.nodePtr = nodePtr; newPtr->numChildren = 1; newPtr->numLines = nodePtr->numLines; - newPtr->numPixels = (int*) ckalloc(sizeof(int)* - treePtr->pixelReferences); + newPtr->numPixels = (int *) + ckalloc(sizeof(int) * treePtr->pixelReferences); for (i=0; i<treePtr->pixelReferences; i++) { - newPtr->numPixels[i] = nodePtr->numPixels[i]; + newPtr->numPixels[i] = nodePtr->numPixels[i]; } RecomputeNodeCounts(treePtr, newPtr); treePtr->rootPtr = newPtr; } newPtr = (Node *) ckalloc(sizeof(Node)); - newPtr->numPixels = (int*) ckalloc(sizeof(int)* - treePtr->pixelReferences); + newPtr->numPixels = (int *) + ckalloc(sizeof(int) * treePtr->pixelReferences); for (i=0; i<treePtr->pixelReferences; i++) { newPtr->numPixels[i] = 0; } @@ -4123,16 +4127,16 @@ Rebalance(treePtr, nodePtr) while (nodePtr->numChildren < MIN_CHILDREN) { register Node *otherPtr; - Node *halfwayNodePtr = NULL; /* Initialization needed only */ - TkTextLine *halfwayLinePtr = NULL; /* to prevent cc warnings. */ + Node *halfwayNodePtr = NULL; /* Initialization needed only */ + TkTextLine *halfwayLinePtr = NULL; /* to prevent cc warnings. */ int totalChildren, firstChildren, i; /* - * Too few children for this node. If this is the root then, - * it's OK for it to have less than MIN_CHILDREN children - * as long as it's got at least two. If it has only one - * (and isn't at level 0), then chop the root node out of - * the tree and use its child as the new root. + * Too few children for this node. If this is the root then, it's + * OK for it to have less than MIN_CHILDREN children as long as + * it's got at least two. If it has only one (and isn't at level + * 0), then chop the root node out of the tree and use its child + * as the new root. */ if (nodePtr->parentPtr == NULL) { @@ -4146,8 +4150,8 @@ Rebalance(treePtr, nodePtr) } /* - * Not the root. Make sure that there are siblings to - * balance with. + * Not the root. Make sure that there are siblings to balance + * with. */ if (nodePtr->parentPtr->numChildren < 2) { @@ -4156,8 +4160,8 @@ Rebalance(treePtr, nodePtr) } /* - * Find a sibling neighbor to borrow from, and arrange for - * nodePtr to be the earlier of the pair. + * Find a sibling neighbor to borrow from, and arrange for nodePtr + * to be the earlier of the pair. */ if (nodePtr->nextPtr == NULL) { @@ -4171,11 +4175,10 @@ Rebalance(treePtr, nodePtr) otherPtr = nodePtr->nextPtr; /* - * We're going to either merge the two siblings together - * into one node or redivide the children among them to - * balance their loads. As preparation, join their two - * child lists into a single list and remember the half-way - * point in the list. + * We're going to either merge the two siblings together into one + * node or redivide the children among them to balance their + * loads. As preparation, join their two child lists into a single + * list and remember the half-way point in the list. */ totalChildren = nodePtr->numChildren + otherPtr->numChildren; @@ -4257,28 +4260,27 @@ Rebalance(treePtr, nodePtr) * * RecomputeNodeCounts -- * - * This procedure is called to recompute all the counts in a node - * (tags, child information, etc.) by scanning the information in - * its descendants. This procedure is called during rebalancing - * when a node's child structure has changed. + * This function is called to recompute all the counts in a node (tags, + * child information, etc.) by scanning the information in its + * descendants. This function is called during rebalancing when a node's + * child structure has changed. * * Results: * None. * * Side effects: - * The tag counts for nodePtr are modified to reflect its current - * child structure, as are its numChildren and numLines fields. - * Also, all of the childrens' parentPtr fields are made to point - * to nodePtr. + * The tag counts for nodePtr are modified to reflect its current child + * structure, as are its numChildren and numLines fields. Also, all of + * the childrens' parentPtr fields are made to point to nodePtr. * *---------------------------------------------------------------------- */ static void RecomputeNodeCounts(treePtr, nodePtr) - register BTree *treePtr; /* The whole B-tree */ - register Node *nodePtr; /* Node whose tag summary information - * must be recomputed. */ + register BTree *treePtr; /* The whole B-tree */ + register Node *nodePtr; /* Node whose tag summary information must be + * recomputed. */ { register Summary *summaryPtr, *summaryPtr2; register Node *childPtr; @@ -4286,10 +4288,10 @@ RecomputeNodeCounts(treePtr, nodePtr) register TkTextSegment *segPtr; TkTextTag *tagPtr; int ref; - + /* - * Zero out all the existing counts for the node, but don't delete - * the existing Summary records (most of them will probably be reused). + * Zero out all the existing counts for the node, but don't delete the + * existing Summary records (most of them will probably be reused). */ for (summaryPtr = nodePtr->summaryPtr; summaryPtr != NULL; @@ -4303,9 +4305,8 @@ RecomputeNodeCounts(treePtr, nodePtr) } /* - * Scan through the children, adding the childrens' tag counts into - * the node's tag counts and adding new Summary structures if - * necessary. + * Scan through the children, adding the childrens' tag counts into the + * node's tag counts and adding new Summary structures if necessary. */ if (nodePtr->level == 0) { @@ -4314,7 +4315,7 @@ RecomputeNodeCounts(treePtr, nodePtr) nodePtr->numChildren++; nodePtr->numLines++; for (ref = 0; ref<treePtr->pixelReferences; ref++) { - nodePtr->numPixels[ref] += linePtr->pixels[2*ref]; + nodePtr->numPixels[ref] += linePtr->pixels[2*ref]; } linePtr->parentPtr = nodePtr; for (segPtr = linePtr->segPtr; segPtr != NULL; @@ -4375,19 +4376,20 @@ RecomputeNodeCounts(treePtr, nodePtr) /* * Scan through the node's tag records again and delete any Summary * records that still have a zero count, or that have all the toggles. - * The node with the children that account for all the tags toggles - * have no summary information, and they become the tagRootPtr for the tag. + * The node with the children that account for all the tags toggles have + * no summary information, and they become the tagRootPtr for the tag. */ summaryPtr2 = NULL; for (summaryPtr = nodePtr->summaryPtr; summaryPtr != NULL; ) { - if (summaryPtr->toggleCount > 0 && + if (summaryPtr->toggleCount > 0 && summaryPtr->toggleCount < summaryPtr->tagPtr->toggleCount) { if (nodePtr->level == summaryPtr->tagPtr->tagRootPtr->level) { /* - * The tag's root node split and some toggles left. - * The tag root must move up a level. + * The tag's root node split and some toggles left. The tag + * root must move up a level. */ + summaryPtr->tagPtr->tagRootPtr = nodePtr->parentPtr; } summaryPtr2 = summaryPtr; @@ -4399,6 +4401,7 @@ RecomputeNodeCounts(treePtr, nodePtr) * A node merge has collected all the toggles under one node. * Push the root down to this level. */ + summaryPtr->tagPtr->tagRootPtr = nodePtr; } if (summaryPtr2 != NULL) { @@ -4418,13 +4421,13 @@ RecomputeNodeCounts(treePtr, nodePtr) * * TkBTreeNumLines -- * - * This procedure returns a count of the number of logical lines of - * text present in a given B-tree. + * This function returns a count of the number of logical lines of text + * present in a given B-tree. * * Results: - * The return value is a count of the number of usable lines - * in tree (i.e. it doesn't include the dummy line that is just - * used to mark the end of the tree). + * The return value is a count of the number of usable lines in tree + * (i.e. it doesn't include the dummy line that is just used to mark the + * end of the tree). * * Side effects: * None. @@ -4434,13 +4437,12 @@ RecomputeNodeCounts(treePtr, nodePtr) int TkBTreeNumLines(tree, textPtr) - TkTextBTree tree; /* Information about tree. */ - CONST TkText *textPtr; /* Relative to this client of the - * B-tree */ + TkTextBTree tree; /* Information about tree. */ + CONST TkText *textPtr; /* Relative to this client of the B-tree */ { BTree *treePtr = (BTree *) tree; int count; - + if (textPtr != NULL && textPtr->end != NULL) { count = TkBTreeLinesTo(NULL, textPtr->end); } else { @@ -4458,15 +4460,15 @@ TkBTreeNumLines(tree, textPtr) * * TkBTreeNumPixels -- * - * This procedure returns a count of the number of pixels of - * text present in a given widget's B-tree representation. + * This function returns a count of the number of pixels of text present + * in a given widget's B-tree representation. * * Results: - * The return value is a count of the number of usable pixels in - * tree (since the dummy line used to mark the end of the B-tree is - * maintained with zero height, as are any lines that are before or - * after the '-start -end' range of the text widget in question, - * the number stored at the root is the number we want). + * The return value is a count of the number of usable pixels in tree + * (since the dummy line used to mark the end of the B-tree is maintained + * with zero height, as are any lines that are before or after the + * '-start -end' range of the text widget in question, the number stored + * at the root is the number we want). * * Side effects: * None. @@ -4476,9 +4478,8 @@ TkBTreeNumLines(tree, textPtr) int TkBTreeNumPixels(tree, textPtr) - TkTextBTree tree; /* The B-tree */ - CONST TkText *textPtr; /* Relative to this client of the - * B-tree */ + TkTextBTree tree; /* The B-tree */ + CONST TkText *textPtr; /* Relative to this client of the B-tree */ { BTree *treePtr = (BTree *) tree; return treePtr->rootPtr->numPixels[textPtr->pixelReference]; @@ -4489,12 +4490,11 @@ TkBTreeNumPixels(tree, textPtr) * * CharSplitProc -- * - * This procedure implements splitting for character segments. + * This function implements splitting for character segments. * * Results: - * The return value is a pointer to a chain of two segments - * that have the same characters as segPtr except split - * among the two segments. + * The return value is a pointer to a chain of two segments that have the + * same characters as segPtr except split among the two segments. * * Side effects: * Storage for segPtr is freed. @@ -4504,9 +4504,9 @@ TkBTreeNumPixels(tree, textPtr) static TkTextSegment * CharSplitProc(segPtr, index) - TkTextSegment *segPtr; /* Pointer to segment to split. */ - int index; /* Position within segment at which - * to split. */ + TkTextSegment *segPtr; /* Pointer to segment to split. */ + int index; /* Position within segment at which to + * split. */ { TkTextSegment *newPtr1, *newPtr2; @@ -4531,12 +4531,12 @@ CharSplitProc(segPtr, index) * * CharCleanupProc -- * - * This procedure merges adjacent character segments into - * a single character segment, if possible. + * This function merges adjacent character segments into a single + * character segment, if possible. * * Results: - * The return value is a pointer to the first segment in - * the (new) list of segments that used to start with segPtr. + * The return value is a pointer to the first segment in the (new) list + * of segments that used to start with segPtr. * * Side effects: * Storage for the segments may be allocated and freed. @@ -4547,10 +4547,9 @@ CharSplitProc(segPtr, index) /* ARGSUSED */ static TkTextSegment * CharCleanupProc(segPtr, linePtr) - TkTextSegment *segPtr; /* Pointer to first of two adjacent - * segments to join. */ - TkTextLine *linePtr; /* Line containing segments (not - * used). */ + TkTextSegment *segPtr; /* Pointer to first of two adjacent segments + * to join. */ + TkTextLine *linePtr; /* Line containing segments (not used). */ { TkTextSegment *segPtr2, *newPtr; @@ -4575,7 +4574,7 @@ CharCleanupProc(segPtr, linePtr) * * CharDeleteProc -- * - * This procedure is invoked to delete a character segment. + * This function is invoked to delete a character segment. * * Results: * Always returns 0 to indicate that the segment was deleted. @@ -4589,11 +4588,11 @@ CharCleanupProc(segPtr, linePtr) /* ARGSUSED */ static int CharDeleteProc(segPtr, linePtr, treeGone) - TkTextSegment *segPtr; /* Segment to delete. */ - TkTextLine *linePtr; /* Line containing segment. */ - int treeGone; /* Non-zero means the entire tree is - * being deleted, so everything must - * get cleaned up. */ + TkTextSegment *segPtr; /* Segment to delete. */ + TkTextLine *linePtr; /* Line containing segment. */ + int treeGone; /* Non-zero means the entire tree is being + * deleted, so everything must get cleaned + * up. */ { ckfree((char*) segPtr); return 0; @@ -4604,15 +4603,14 @@ CharDeleteProc(segPtr, linePtr, treeGone) * * CharCheckProc -- * - * This procedure is invoked to perform consistency checks - * on character segments. + * This function is invoked to perform consistency checks on character + * segments. * * Results: * None. * * Side effects: - * If the segment isn't inconsistent then the procedure - * panics. + * If the segment isn't inconsistent then the function panics. * *-------------------------------------------------------------- */ @@ -4624,11 +4622,10 @@ CharCheckProc(segPtr, linePtr) TkTextLine *linePtr; /* Line containing segment. */ { /* - * Make sure that the segment contains the number of - * characters indicated by its header, and that the last - * segment in a line ends in a newline. Also make sure - * that there aren't ever two character segments adjacent - * to each other: they should be merged together. + * Make sure that the segment contains the number of characters indicated + * by its header, and that the last segment in a line ends in a newline. + * Also make sure that there aren't ever two character segments adjacent + * to each other: they should be merged together. */ if (segPtr->size <= 0) { @@ -4641,10 +4638,8 @@ CharCheckProc(segPtr, linePtr) if (segPtr->body.chars[segPtr->size-1] != '\n') { Tcl_Panic("CharCheckProc: line doesn't end with newline"); } - } else { - if (segPtr->nextPtr->typePtr == &tkTextCharType) { - Tcl_Panic("CharCheckProc: adjacent character segments weren't merged"); - } + } else if (segPtr->nextPtr->typePtr == &tkTextCharType) { + Tcl_Panic("CharCheckProc: adjacent character segments weren't merged"); } } @@ -4653,27 +4648,26 @@ CharCheckProc(segPtr, linePtr) * * ToggleDeleteProc -- * - * This procedure is invoked to delete toggle segments. + * This function is invoked to delete toggle segments. * * Results: - * Returns 1 to indicate that the segment may not be deleted, - * unless the entire B-tree is going away. + * Returns 1 to indicate that the segment may not be deleted, unless the + * entire B-tree is going away. * * Side effects: - * If the tree is going away then the toggle's memory is - * freed; otherwise the toggle counts in nodes above the - * segment get updated. + * If the tree is going away then the toggle's memory is freed; otherwise + * the toggle counts in nodes above the segment get updated. * *-------------------------------------------------------------- */ static int ToggleDeleteProc(segPtr, linePtr, treeGone) - TkTextSegment *segPtr; /* Segment to check. */ - TkTextLine *linePtr; /* Line containing segment. */ - int treeGone; /* Non-zero means the entire tree is - * being deleted, so everything must - * get cleaned up. */ + TkTextSegment *segPtr; /* Segment to check. */ + TkTextLine *linePtr; /* Line containing segment. */ + int treeGone; /* Non-zero means the entire tree is being + * deleted, so everything must get cleaned + * up. */ { if (treeGone) { ckfree((char *) segPtr); @@ -4681,11 +4675,11 @@ ToggleDeleteProc(segPtr, linePtr, treeGone) } /* - * This toggle is in the middle of a range of characters that's - * being deleted. Refuse to die. We'll be moved to the end of - * the deleted range and our cleanup procedure will be called - * later. Decrement node toggle counts here, and set a flag - * so we'll re-increment them in the cleanup procedure. + * This toggle is in the middle of a range of characters that's being + * deleted. Refuse to die. We'll be moved to the end of the deleted range + * and our cleanup function will be called later. Decrement node toggle + * counts here, and set a flag so we'll re-increment them in the cleanup + * function. */ if (segPtr->body.toggle.inNodeCounts) { @@ -4701,20 +4695,19 @@ ToggleDeleteProc(segPtr, linePtr, treeGone) * * ToggleCleanupProc -- * - * This procedure is called when a toggle is part of a line that's - * been modified in some way. It's invoked after the - * modifications are complete. + * This function is called when a toggle is part of a line that's been + * modified in some way. It's invoked after the modifications are + * complete. * * Results: - * The return value is the head segment in a new list - * that is to replace the tail of the line that used to - * start at segPtr. This allows the procedure to delete - * or modify segPtr. + * The return value is the head segment in a new list that is to replace + * the tail of the line that used to start at segPtr. This allows the + * function to delete or modify segPtr. * * Side effects: - * Toggle counts in the nodes above the new line will be - * updated if they're not already. Toggles may be collapsed - * if there are duplicate toggles at the same position. + * Toggle counts in the nodes above the new line will be updated if + * they're not already. Toggles may be collapsed if there are duplicate + * toggles at the same position. * *-------------------------------------------------------------- */ @@ -4728,10 +4721,10 @@ ToggleCleanupProc(segPtr, linePtr) int counts; /* - * If this is a toggle-off segment, look ahead through the next - * segments to see if there's a toggle-on segment for the same tag - * before any segments with non-zero size. If so then the two - * toggles cancel each other; remove them both. + * If this is a toggle-off segment, look ahead through the next segments + * to see if there's a toggle-on segment for the same tag before any + * segments with non-zero size. If so then the two toggles cancel each + * other; remove them both. */ if (segPtr->typePtr == &tkTextToggleOffType) { @@ -4771,8 +4764,8 @@ ToggleCleanupProc(segPtr, linePtr) * * ToggleLineChangeProc -- * - * This procedure is invoked when a toggle segment is about - * to move from one line to another. + * This function is invoked when a toggle segment is about to move from + * one line to another. * * Results: * None. @@ -4800,22 +4793,22 @@ ToggleLineChangeProc(segPtr, linePtr) * * ToggleCheckProc -- * - * This procedure is invoked to perform consistency checks - * on toggle segments. + * This function is invoked to perform consistency checks on toggle + * segments. * * Results: * None. * * Side effects: - * If a consistency problem is found the procedure panics. + * If a consistency problem is found the function panics. * *-------------------------------------------------------------- */ static void ToggleCheckProc(segPtr, linePtr) - TkTextSegment *segPtr; /* Segment to check. */ - TkTextLine *linePtr; /* Line containing segment. */ + TkTextSegment *segPtr; /* Segment to check. */ + TkTextLine *linePtr; /* Line containing segment. */ { register Summary *summaryPtr; int needSummary; @@ -4844,3 +4837,11 @@ ToggleCheckProc(segPtr, linePtr) } } } + +/* + * Local Variables: + * mode: c + * c-basic-offset: 4 + * fill-column: 78 + * End: + */ |