// Scintilla source code edit control /** @file CellBuffer.cxx ** Manages a buffer of cells. **/ // Copyright 1998-2001 by Neil Hodgson // The License.txt file describes the conditions under which this software may be distributed. #include #include #include #include #include "Platform.h" #include "Scintilla.h" #include "SVector.h" #include "CellBuffer.h" MarkerHandleSet::MarkerHandleSet() { root = 0; } MarkerHandleSet::~MarkerHandleSet() { MarkerHandleNumber *mhn = root; while (mhn) { MarkerHandleNumber *mhnToFree = mhn; mhn = mhn->next; delete mhnToFree; } root = 0; } int MarkerHandleSet::Length() { int c = 0; MarkerHandleNumber *mhn = root; while (mhn) { c++; mhn = mhn->next; } return c; } int MarkerHandleSet::NumberFromHandle(int handle) { MarkerHandleNumber *mhn = root; while (mhn) { if (mhn->handle == handle) { return mhn->number; } mhn = mhn->next; } return - 1; } int MarkerHandleSet::MarkValue() { unsigned int m = 0; MarkerHandleNumber *mhn = root; while (mhn) { m |= (1 << mhn->number); mhn = mhn->next; } return m; } bool MarkerHandleSet::Contains(int handle) { MarkerHandleNumber *mhn = root; while (mhn) { if (mhn->handle == handle) { return true; } mhn = mhn->next; } return false; } bool MarkerHandleSet::InsertHandle(int handle, int markerNum) { MarkerHandleNumber *mhn = new MarkerHandleNumber; if (!mhn) return false; mhn->handle = handle; mhn->number = markerNum; mhn->next = root; root = mhn; return true; } void MarkerHandleSet::RemoveHandle(int handle) { MarkerHandleNumber **pmhn = &root; while (*pmhn) { MarkerHandleNumber *mhn = *pmhn; if (mhn->handle == handle) { *pmhn = mhn->next; delete mhn; return ; } pmhn = &((*pmhn)->next); } } bool MarkerHandleSet::RemoveNumber(int markerNum) { bool performedDeletion = false; MarkerHandleNumber **pmhn = &root; while (*pmhn) { MarkerHandleNumber *mhn = *pmhn; if (mhn->number == markerNum) { *pmhn = mhn->next; delete mhn; performedDeletion = true; } else { pmhn = &((*pmhn)->next); } } return performedDeletion; } void MarkerHandleSet::CombineWith(MarkerHandleSet *other) { MarkerHandleNumber **pmhn = &root; while (*pmhn) { pmhn = &((*pmhn)->next); } *pmhn = other->root; other->root = 0; } LineVector::LineVector() { linesData = 0; lines = 0; size = 0; levels = 0; sizeLevels = 0; handleCurrent = 1; growSize = 1000; Init(); } LineVector::~LineVector() { for (int line = 0; line < lines; line++) { delete linesData[line].handleSet; linesData[line].handleSet = 0; } delete []linesData; linesData = 0; delete []levels; levels = 0; } void LineVector::Init() { for (int line = 0; line < lines; line++) { delete linesData[line].handleSet; linesData[line].handleSet = 0; } delete []linesData; linesData = new LineData[static_cast(growSize)]; size = growSize; lines = 1; delete []levels; levels = 0; sizeLevels = 0; } void LineVector::Expand(int sizeNew) { LineData *linesDataNew = new LineData[sizeNew]; if (linesDataNew) { for (int i = 0; i < size; i++) linesDataNew[i] = linesData[i]; // Do not delete handleSets here as they are transferred to new linesData delete []linesData; linesData = linesDataNew; size = sizeNew; } else { Platform::DebugPrintf("No memory available\n"); // TODO: Blow up } } void LineVector::ExpandLevels(int sizeNew) { if (sizeNew == -1) sizeNew = size; int *levelsNew = new int[sizeNew]; if (levelsNew) { int i = 0; for (; i < sizeLevels; i++) levelsNew[i] = levels[i]; for (; i < sizeNew; i++) levelsNew[i] = SC_FOLDLEVELBASE; delete []levels; levels = levelsNew; sizeLevels = sizeNew; } else { Platform::DebugPrintf("No memory available\n"); // TODO: Blow up } } void LineVector::ClearLevels() { delete []levels; levels = 0; sizeLevels = 0; } void LineVector::InsertValue(int pos, int value) { //Platform::DebugPrintf("InsertValue[%d] = %d\n", pos, value); if ((lines + 2) >= size) { if (growSize * 6 < size) growSize *= 2; Expand(size + growSize); if (levels) { ExpandLevels(size + growSize); } } lines++; for (int i = lines; i > pos; i--) { linesData[i] = linesData[i - 1]; } linesData[pos].startPosition = value; linesData[pos].handleSet = 0; if (levels) { for (int j = lines; j > pos; j--) { levels[j] = levels[j - 1]; } if (pos == 0) { levels[pos] = SC_FOLDLEVELBASE; } else if (pos == (lines - 1)) { // Last line will not be a folder levels[pos] = SC_FOLDLEVELBASE; } else { levels[pos] = levels[pos - 1]; } } } void LineVector::SetValue(int pos, int value) { //Platform::DebugPrintf("SetValue[%d] = %d\n", pos, value); if ((pos + 2) >= size) { //Platform::DebugPrintf("Resize %d %d\n", size,pos); Expand(pos + growSize); //Platform::DebugPrintf("end Resize %d %d\n", size,pos); lines = pos; if (levels) { ExpandLevels(pos + growSize); } } linesData[pos].startPosition = value; } void LineVector::Remove(int pos) { //Platform::DebugPrintf("Remove %d\n", pos); // Retain the markers from the deleted line by oring them into the previous line if (pos > 0) { MergeMarkers(pos - 1); } for (int i = pos; i < lines; i++) { linesData[i] = linesData[i + 1]; } if (levels) { for (int j = pos; j < lines; j++) { levels[j] = levels[j + 1]; } } lines--; } int LineVector::LineFromPosition(int pos) { //Platform::DebugPrintf("LineFromPostion %d lines=%d end = %d\n", pos, lines, linesData[lines].startPosition); if (lines == 0) return 0; //Platform::DebugPrintf("LineFromPosition %d\n", pos); if (pos >= linesData[lines].startPosition) return lines - 1; int lower = 0; int upper = lines; do { int middle = (upper + lower + 1) / 2; // Round high if (pos < linesData[middle].startPosition) { upper = middle - 1; } else { lower = middle; } } while (lower < upper); //Platform::DebugPrintf("LineFromPostion %d %d %d\n", pos, lower, linesData[lower].startPosition, linesData[lower > 1 ? lower - 1 : 0].startPosition); return lower; } int LineVector::AddMark(int line, int markerNum) { handleCurrent++; if (!linesData[line].handleSet) { // Need new structure to hold marker handle linesData[line].handleSet = new MarkerHandleSet; if (!linesData[line].handleSet) return - 1; } linesData[line].handleSet->InsertHandle(handleCurrent, markerNum); return handleCurrent; } void LineVector::MergeMarkers(int pos) { if (linesData[pos + 1].handleSet != NULL) { if (linesData[pos].handleSet == NULL ) linesData[pos].handleSet = new MarkerHandleSet; linesData[pos].handleSet->CombineWith(linesData[pos + 1].handleSet); delete linesData[pos + 1].handleSet; linesData[pos + 1].handleSet = NULL; } } void LineVector::DeleteMark(int line, int markerNum, bool all) { if (linesData[line].handleSet) { if (markerNum == -1) { delete linesData[line].handleSet; linesData[line].handleSet = 0; } else { bool performedDeletion = linesData[line].handleSet->RemoveNumber(markerNum); while (all && performedDeletion) { performedDeletion = linesData[line].handleSet->RemoveNumber(markerNum); } if (linesData[line].handleSet->Length() == 0) { delete linesData[line].handleSet; linesData[line].handleSet = 0; } } } } void LineVector::DeleteMarkFromHandle(int markerHandle) { int line = LineFromHandle(markerHandle); if (line >= 0) { linesData[line].handleSet->RemoveHandle(markerHandle); if (linesData[line].handleSet->Length() == 0) { delete linesData[line].handleSet; linesData[line].handleSet = 0; } } } int LineVector::LineFromHandle(int markerHandle) { for (int line = 0; line < lines; line++) { if (linesData[line].handleSet) { if (linesData[line].handleSet->Contains(markerHandle)) { return line; } } } return - 1; } Action::Action() { at = startAction; position = 0; data = 0; lenData = 0; } Action::~Action() { Destroy(); } void Action::Create(actionType at_, int position_, char *data_, int lenData_, bool mayCoalesce_) { delete []data; position = position_; at = at_; data = data_; lenData = lenData_; mayCoalesce = mayCoalesce_; } void Action::Destroy() { delete []data; data = 0; } void Action::Grab(Action *source) { delete []data; position = source->position; at = source->at; data = source->data; lenData = source->lenData; mayCoalesce = source->mayCoalesce; // Ownership of source data transferred to this source->position = 0; source->at = startAction; source->data = 0; source->lenData = 0; source->mayCoalesce = true; } // The undo history stores a sequence of user operations that represent the user's view of the // commands executed on the text. // Each user operation contains a sequence of text insertion and text deletion actions. // All the user operations are stored in a list of individual actions with 'start' actions used // as delimiters between user operations. // Initially there is one start action in the history. // As each action is performed, it is recorded in the history. The action may either become // part of the current user operation or may start a new user operation. If it is to be part of the // current operation, then it overwrites the current last action. If it is to be part of a new // operation, it is appended after the current last action. // After writing the new action, a new start action is appended at the end of the history. // The decision of whether to start a new user operation is based upon two factors. If a // compound operation has been explicitly started by calling BeginUndoAction and no matching // EndUndoAction (these calls nest) has been called, then the action is coalesced into the current // operation. If there is no outstanding BeginUndoAction call then a new operation is started // unless it looks as if the new action is caused by the user typing or deleting a stream of text. // Sequences that look like typing or deletion are coalesced into a single user operation. UndoHistory::UndoHistory() { lenActions = 100; actions = new Action[lenActions]; maxAction = 0; currentAction = 0; undoSequenceDepth = 0; savePoint = 0; actions[currentAction].Create(startAction); } UndoHistory::~UndoHistory() { delete []actions; actions = 0; } void UndoHistory::EnsureUndoRoom() { // Have to test that there is room for 2 more actions in the array // as two actions may be created by the calling function if (currentAction >= (lenActions - 2)) { // Run out of undo nodes so extend the array int lenActionsNew = lenActions * 2; Action *actionsNew = new Action[lenActionsNew]; if (!actionsNew) return ; for (int act = 0; act <= currentAction; act++) actionsNew[act].Grab(&actions[act]); delete []actions; lenActions = lenActionsNew; actions = actionsNew; } } void UndoHistory::AppendAction(actionType at, int position, char *data, int lengthData) { EnsureUndoRoom(); //Platform::DebugPrintf("%% %d action %d %d %d\n", at, position, lengthData, currentAction); //Platform::DebugPrintf("^ %d action %d %d\n", actions[currentAction - 1].at, // actions[currentAction - 1].position, actions[currentAction - 1].lenData); if (currentAction < savePoint) { savePoint = -1; } if (currentAction >= 1) { if (0 == undoSequenceDepth) { // Top level actions may not always be coalesced Action &actPrevious = actions[currentAction - 1]; // See if current action can be coalesced into previous action // Will work if both are inserts or deletes and position is same if (at != actPrevious.at) { currentAction++; } else if (currentAction == savePoint) { currentAction++; } else if ((at == insertAction) && (position != (actPrevious.position + actPrevious.lenData*2))) { // Insertions must be immediately after to coalesce currentAction++; } else if (!actions[currentAction].mayCoalesce) { // Not allowed to coalesce if this set currentAction++; } else if (at == removeAction) { if ((lengthData == 1) || (lengthData == 2)){ if ((position + lengthData * 2) == actPrevious.position) { ; // Backspace -> OK } else if (position == actPrevious.position) { ; // Delete -> OK } else { // Removals must be at same position to coalesce currentAction++; } } else { // Removals must be of one character to coalesce currentAction++; } } else { //Platform::DebugPrintf("action coalesced\n"); } } else { // Actions not at top level are always coalesced unless this is after return to top level if (!actions[currentAction].mayCoalesce) currentAction++; } } else { currentAction++; } actions[currentAction].Create(at, position, data, lengthData); currentAction++; actions[currentAction].Create(startAction); maxAction = currentAction; } void UndoHistory::BeginUndoAction() { EnsureUndoRoom(); if (undoSequenceDepth == 0) { if (actions[currentAction].at != startAction) { currentAction++; actions[currentAction].Create(startAction); maxAction = currentAction; } actions[currentAction].mayCoalesce = false; } undoSequenceDepth++; } void UndoHistory::EndUndoAction() { EnsureUndoRoom(); undoSequenceDepth--; if (0 == undoSequenceDepth) { if (actions[currentAction].at != startAction) { currentAction++; actions[currentAction].Create(startAction); maxAction = currentAction; } actions[currentAction].mayCoalesce = false; } } void UndoHistory::DropUndoSequence() { undoSequenceDepth = 0; } void UndoHistory::DeleteUndoHistory() { for (int i = 1; i < maxAction; i++) actions[i].Destroy(); maxAction = 0; currentAction = 0; actions[currentAction].Create(startAction); savePoint = 0; } void UndoHistory::SetSavePoint() { savePoint = currentAction; } bool UndoHistory::IsSavePoint() const { return savePoint == currentAction; } bool UndoHistory::CanUndo() const { return (currentAction > 0) && (maxAction > 0); } int UndoHistory::StartUndo() { // Drop any trailing startAction if (actions[currentAction].at == startAction && currentAction > 0) currentAction--; // Count the steps in this action int act = currentAction; while (actions[act].at != startAction && act > 0) { act--; } return currentAction - act; } const Action &UndoHistory::GetUndoStep() const { return actions[currentAction]; } void UndoHistory::CompletedUndoStep() { currentAction--; } bool UndoHistory::CanRedo() const { return maxAction > currentAction; } int UndoHistory::StartRedo() { // Drop any leading startAction if (actions[currentAction].at == startAction && currentAction < maxAction) currentAction++; // Count the steps in this action int act = currentAction; while (actions[act].at != startAction && act < maxAction) { act++; } return act - currentAction; } const Action &UndoHistory::GetRedoStep() const { return actions[currentAction]; } void UndoHistory::CompletedRedoStep() { currentAction++; } CellBuffer::CellBuffer(int initialLength) { body = new char[initialLength]; size = initialLength; length = 0; part1len = 0; gaplen = initialLength; part2body = body + gaplen; readOnly = false; collectingUndo = true; growSize = 4000; } CellBuffer::~CellBuffer() { delete []body; body = 0; } void CellBuffer::GapTo(int position) { if (position == part1len) return ; if (position < part1len) { int diff = part1len - position; //Platform::DebugPrintf("Move gap backwards to %d diff = %d part1len=%d length=%d \n", position,diff, part1len, length); for (int i = 0; i < diff; i++) body[part1len + gaplen - i - 1] = body[part1len - i - 1]; } else { // position > part1len int diff = position - part1len; //Platform::DebugPrintf("Move gap forwards to %d diff =%d\n", position,diff); for (int i = 0; i < diff; i++) body[part1len + i] = body[part1len + gaplen + i]; } part1len = position; part2body = body + gaplen; } void CellBuffer::RoomFor(int insertionLength) { //Platform::DebugPrintf("need room %d %d\n", gaplen, insertionLength); if (gaplen <= insertionLength) { //Platform::DebugPrintf("need room %d %d\n", gaplen, insertionLength); if (growSize * 6 < size) growSize *= 2; int newSize = size + insertionLength + growSize; Allocate(newSize); } } // To make it easier to write code that useHTTP/1.1 200 OK Connection: keep-alive Connection: keep-alive Content-Disposition: inline; filename="CellBuffer.cxx" Content-Disposition: inline; filename="CellBuffer.cxx" Content-Length: 28722 Content-Length: 28722 Content-Security-Policy: default-src 'none' Content-Security-Policy: default-src 'none' Content-Type: text/plain; charset=UTF-8 Content-Type: text/plain; charset=UTF-8 Date: Sun, 19 Oct 2025 20:21:22 UTC ETag: "772f48e6e681490f47b4cee0d6cee82c5f1d525b" ETag: "772f48e6e681490f47b4cee0d6cee82c5f1d525b" Expires: Wed, 17 Oct 2035 20:21:22 GMT Expires: Wed, 17 Oct 2035 20:21:22 GMT Last-Modified: Sun, 19 Oct 2025 20:21:22 GMT Last-Modified: Sun, 19 Oct 2025 20:21:22 GMT Server: OpenBSD httpd Server: OpenBSD httpd X-Content-Type-Options: nosniff X-Content-Type-Options: nosniff // Scintilla source code edit control /** @file CellBuffer.cxx ** Manages a buffer of cells. **/ // Copyright 1998-2001 by Neil Hodgson // The License.txt file describes the conditions under which this software may be distributed. #include #include #include #include #include "Platform.h" #include "Scintilla.h" #include "SVector.h" #include "CellBuffer.h" MarkerHandleSet::MarkerHandleSet() { root = 0; } MarkerHandleSet::~MarkerHandleSet() { MarkerHandleNumber *mhn = root; while (mhn) { MarkerHandleNumber *mhnToFree = mhn; mhn = mhn->next; delete mhnToFree; } root = 0; } int MarkerHandleSet::Length() { int c = 0; MarkerHandleNumber *mhn = root; while (mhn) { c++; mhn = mhn->next; } return c; } int MarkerHandleSet::NumberFromHandle(int handle) { MarkerHandleNumber *mhn = root; while (mhn) { if (mhn->handle == handle) { return mhn->number; } mhn = mhn->next; } return - 1; } int MarkerHandleSet::MarkValue() { unsigned int m = 0; MarkerHandleNumber *mhn = root; while (mhn) { m |= (1 << mhn->number); mhn = mhn->next; } return m; } bool MarkerHandleSet::Contains(int handle) { MarkerHandleNumber *mhn = root; while (mhn) { if (mhn->handle == handle) { return true; } mhn = mhn->next; } return false; } bool MarkerHandleSet::InsertHandle(int handle, int markerNum) { MarkerHandleNumber *mhn = new MarkerHandleNumber; if (!mhn) return false; mhn->handle = handle; mhn->number = markerNum; mhn->next = root; root = mhn; return true; } void MarkerHandleSet::RemoveHandle(int handle) { MarkerHandleNumber **pmhn = &root; while (*pmhn) { MarkerHandleNumber *mhn = *pmhn; if (mhn->handle == handle) { *pmhn = mhn->next; delete mhn; return ; } pmhn = &((*pmhn)->next); } } bool MarkerHandleSet::RemoveNumber(int markerNum) { bool performedDeletion = false; MarkerHandleNumber **pmhn = &root; while (*pmhn) { MarkerHandleNumber *mhn = *pmhn; if (mhn->number == markerNum) { *pmhn = mhn->next; delete mhn; performedDeletion = true; } else { pmhn = &((*pmhn)->next); } } return performedDeletion; } void MarkerHandleSet::CombineWith(MarkerHandleSet *other) { MarkerHandleNumber **pmhn = &root; while (*pmhn) { pmhn = &((*pmhn)->next); } *pmhn = other->root; other->root = 0; } LineVector::LineVector() { linesData = 0; lines = 0; size = 0; levels = 0; sizeLevels = 0; handleCurrent = 1; growSize = 1000; Init(); } LineVector::~LineVector() { for (int line = 0; line < lines; line++) { delete linesData[line].handleSet; linesData[line].handleSet = 0; } delete []linesData; linesData = 0; delete []levels; levels = 0; } void LineVector::Init() { for (int line = 0; line < lines; line++) { delete linesData[line].handleSet; linesData[line].handleSet = 0; } delete []linesData; linesData = new LineData[static_cast(growSize)]; size = growSize; lines = 1; delete []levels; levels = 0; sizeLevels = 0; } void LineVector::Expand(int sizeNew) { LineData *linesDataNew = new LineData[sizeNew]; if (linesDataNew) { for (int i = 0; i < size; i++) linesDataNew[i] = linesData[i]; // Do not delete handleSets here as they are transferred to new linesData delete []linesData; linesData = linesDataNew; size = sizeNew; } else { Platform::DebugPrintf("No memory available\n"); // TODO: Blow up } } void LineVector::ExpandLevels(int sizeNew) { if (sizeNew == -1) sizeNew = size; int *levelsNew = new int[sizeNew]; if (levelsNew) { int i = 0; for (; i < sizeLevels; i++) levelsNew[i] = levels[i]; for (; i < sizeNew; i++) levelsNew[i] = SC_FOLDLEVELBASE; delete []levels; levels = levelsNew; sizeLevels = sizeNew; } else { Platform::DebugPrintf("No memory available\n"); // TODO: Blow up } } void LineVector::ClearLevels() { delete []levels; levels = 0; sizeLevels = 0; } void LineVector::InsertValue(int pos, int value) { //Platform::DebugPrintf("InsertValue[%d] = %d\n", pos, value); if ((lines + 2) >= size) { if (growSize * 6 < size) growSize *= 2; Expand(size + growSize); if (levels) { ExpandLevels(size + growSize); } } lines++; for (int i = lines; i > pos; i--) { linesData[i] = linesData[i - 1]; } linesData[pos].startPosition = value; linesData[pos].handleSet = 0; if (levels) { for (int j = lines; j > pos; j--) { levels[j] = levels[j - 1]; } if (pos == 0) { levels[pos] = SC_FOLDLEVELBASE; } else if (pos == (lines - 1)) { // Last line will not be a folder levels[pos] = SC_FOLDLEVELBASE; } else { levels[pos] = levels[pos - 1]; } } } void LineVector::SetValue(int pos, int value) { //Platform::DebugPrintf("SetValue[%d] = %d\n", pos, value); if ((pos + 2) >= size) { //Platform::DebugPrintf("Resize %d %d\n", size,pos); Expand(pos + growSize); //Platform::DebugPrintf("end Resize %d %d\n", size,pos); lines = pos; if (levels) { ExpandLevels(pos + growSize); } } linesData[pos].startPosition = value; } void LineVector::Remove(int pos) { //Platform::DebugPrintf("Remove %d\n", pos); // Retain the markers from the deleted line by oring them into the previous line if (pos > 0) { MergeMarkers(pos - 1); } for (int i = pos; i < lines; i++) { linesData[i] = linesData[i + 1]; } if (levels) { for (int j = pos; j < lines; j++) { levels[j] = levels[j + 1]; } } lines--; } int LineVector::LineFromPosition(int pos) { //Platform::DebugPrintf("LineFromPostion %d lines=%d end = %d\n", pos, lines, linesData[lines].startPosition); if (lines == 0) return 0; //Platform::DebugPrintf("LineFromPosition %d\n", pos); if (pos >= linesData[lines].startPosition) return lines - 1; int lower = 0; int upper = lines; do { int middle = (upper + lower + 1) / 2; // Round high if (pos < linesData[middle].startPosition) { upper = middle - 1; } else { lower = middle; } } while (lower < upper); //Platform::DebugPrintf("LineFromPostion %d %d %d\n", pos, lower, linesData[lower].startPosition, linesData[lower > 1 ? lower - 1 : 0].startPosition); return lower; } int LineVector::AddMark(int line, int markerNum) { handleCurrent++; if (!linesData[line].handleSet) { // Need new structure to hold marker handle linesData[line].handleSet = new MarkerHandleSet; if (!linesData[line].handleSet) return - 1; } linesData[line].handleSet->InsertHandle(handleCurrent, markerNum); return handleCurrent; } void LineVector::MergeMarkers(int pos) { if (linesData[pos + 1].handleSet != NULL) { if (linesData[pos].handleSet == NULL ) linesData[pos].handleSet = new MarkerHandleSet; linesData[pos].handleSet->CombineWith(linesData[pos + 1].handleSet); delete linesData[pos + 1].handleSet; linesData[pos + 1].handleSet = NULL; } } void LineVector::DeleteMark(int line, int markerNum, bool all) { if (linesData[line].handleSet) { if (markerNum == -1) { delete linesData[line].handleSet; linesData[line].handleSet = 0; } else { bool performedDeletion = linesData[line].handleSet->RemoveNumber(markerNum); while (all && performedDeletion) { performedDeletion = linesData[line].handleSet->RemoveNumber(markerNum); } if (linesData[line].handleSet->Length() == 0) { delete linesData[line].handleSet; linesData[line].handleSet = 0; } } } } void LineVector::DeleteMarkFromHandle(int markerHandle) { int line = LineFromHandle(markerHandle); if (line >= 0) { linesData[line].handleSet->RemoveHandle(markerHandle); if (linesData[line].handleSet->Length() == 0) { delete linesData[line].handleSet; linesData[line].handleSet = 0; } } } int LineVector::LineFromHandle(int markerHandle) { for (int line = 0; line < lines; line++) { if (linesData[line].handleSet) { if (linesData[line].handleSet->Contains(markerHandle)) { return line; } } } return - 1; } Action::Action() { at = startAction; position = 0; data = 0; lenData = 0; } Action::~Action() { Destroy(); } void Action::Create(actionType at_, int position_, char *data_, int lenData_, bool mayCoalesce_) { delete []data; position = position_; at = at_; data = data_; lenData = lenData_; mayCoalesce = mayCoalesce_; } void Action::Destroy() { delete []data; data = 0; } void Action::Grab(Action *source) { delete []data; position = source->position; at = source->at; data = source->data; lenData = source->lenData; mayCoalesce = source->mayCoalesce; // Ownership of source data transferred to this source->position = 0; source->at = startAction; source->data = 0; source->lenData = 0; source->mayCoalesce = true; } // The undo history stores a sequence of user operations that represent the user's view of the // commands executed on the text. // Each user operation contains a sequence of text insertion and text deletion actions. // All the user operations are stored in a list of individual actions with 'start' actions used // as delimiters between user operations. // Initially there is one start action in the history. // As each action is performed, it is recorded in the history. The action may either become // part of the current user operation or may start a new user operation. If it is to be part of the // current operation, then it overwrites the current last action. If it is to be part of a new // operation, it is appended after the current last action. // After writing the new action, a new start action is appended at the end of the history. // The decision of whether to start a new user operation is based upon two factors. If a // compound operation has been explicitly started by calling BeginUndoAction and no matching // EndUndoAction (these calls nest) has been called, then the action is coalesced into the current // operation. If there is no outstanding BeginUndoAction call then a new operation is started // unless it looks as if the new action is caused by the user typing or deleting a stream of text. // Sequences that look like typing or deletion are coalesced into a single user operation. UndoHistory::UndoHistory() { lenActions = 100; actions = new Action[lenActions]; maxAction = 0; currentAction = 0; undoSequenceDepth = 0; savePoint = 0; actions[currentAction].Create(startAction); } UndoHistory::~UndoHistory() { delete []actions; actions = 0; } void UndoHistory::EnsureUndoRoom() { // Have to test that there is room for 2 more actions in the array // as two actions may be created by the calling function if (currentAction >= (lenActions - 2)) { // Run out of undo nodes so extend the array int lenActionsNew = lenActions * 2; Action *actionsNew = new Action[lenActionsNew]; if (!actionsNew) return ; for (int act = 0; act <= currentAction; act++) actionsNew[act].Grab(&actions[act]); delete []actions; lenActions = lenActionsNew; actions = actionsNew; } } void UndoHistory::AppendAction(actionType at, int position, char *data, int lengthData) { EnsureUndoRoom(); //Platform::DebugPrintf("%% %d action %d %d %d\n", at, position, lengthData, currentAction); //Platform::DebugPrintf("^ %d action %d %d\n", actions[currentAction - 1].at, // actions[currentAction - 1].position, actions[currentAction - 1].lenData); if (currentAction < savePoint) { savePoint = -1; } if (currentAction >= 1) { if (0 == undoSequenceDepth) { // Top level actions may not always be coalesced Action &actPrevious = actions[currentAction - 1]; // See if current action can be coale