// 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 "SplitVector.h" #include "Partitioning.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() const { int c = 0; MarkerHandleNumber *mhn = root; while (mhn) { c++; mhn = mhn->next; } return c; } int MarkerHandleSet::NumberFromHandle(int handle) const { MarkerHandleNumber *mhn = root; while (mhn) { if (mhn->handle == handle) { return mhn->number; } mhn = mhn->next; } return - 1; } int MarkerHandleSet::MarkValue() const { unsigned int m = 0; MarkerHandleNumber *mhn = root; while (mhn) { m |= (1 << mhn->number); mhn = mhn->next; } return m; } bool MarkerHandleSet::Contains(int handle) const { 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() : starts(256) { handleCurrent = 1; Init(); } LineVector::~LineVector() { starts.DeleteAll(); for (int line = 0; line < markers.Length(); line++) { delete markers[line]; markers[line] = 0; } markers.DeleteAll(); levels.DeleteAll(); } void LineVector::Init() { starts.DeleteAll(); for (int line = 0; line < markers.Length(); line++) { delete markers[line]; markers[line] = 0; } markers.DeleteAll(); levels.DeleteAll(); } void LineVector::ExpandLevels(int sizeNew) { levels.InsertValue(levels.Length(), sizeNew - levels.Length(), SC_FOLDLEVELBASE); } void LineVector::ClearLevels() { levels.DeleteAll(); } int LineVector::SetLevel(int line, int level) { int prev = 0; if ((line >= 0) && (line < Lines())) { if (!levels.Length()) { ExpandLevels(Lines() + 1); } prev = levels[line]; if (prev != level) { levels[line] = level; } } return prev; } int LineVector::GetLevel(int line) { if (levels.Length() && (line >= 0) && (line < Lines())) { return levels[line]; } else { return SC_FOLDLEVELBASE; } } void LineVector::InsertText(int line, int delta) { starts.InsertText(line, delta); } void LineVector::InsertLine(int line, int position) { starts.InsertPartition(line, position); if (markers.Length()) { markers.Insert(line, 0); } if (levels.Length()) { int level = SC_FOLDLEVELBASE; if ((line > 0) && (line < Lines())) { level = levels[line-1] & ~SC_FOLDLEVELWHITEFLAG; } levels.InsertValue(line, 1, level); } } void LineVector::SetLineStart(int line, int position) { starts.SetPartitionStartPosition(line, position); } void LineVector::RemoveLine(int line) { starts.RemovePartition(line); // Retain the markers from the deleted line by oring them into the previous line if (markers.Length()) { if (line > 0) { MergeMarkers(line - 1); } markers.Delete(line); } if (levels.Length()) { // Move up following lines but merge header flag from this line // to line before to avoid a temporary disappearence causing expansion. int firstHeader = levels[line] & SC_FOLDLEVELHEADERFLAG; levels.Delete(line); if (line > 0) levels[line-1] |= firstHeader; } } int LineVector::LineFromPosition(int pos) { return starts.PartitionFromPosition(pos); } int LineVector::MarkValue(int line) { if (markers.Length() && markers[line]) return markers[line]->MarkValue(); else return 0; } int LineVector::AddMark(int line, int markerNum) { handleCurrent++; if (!markers.Length()) { // No existing markers so allocate one element per line markers.InsertValue(0, Lines(), 0); } if (!markers[line]) { // Need new structure to hold marker handle markers[line] = new MarkerHandleSet(); if (!markers[line]) return - 1; } markers[line]->InsertHandle(handleCurrent, markerNum); return handleCurrent; } void LineVector::MergeMarkers(int pos) { if (markers[pos + 1] != NULL) { if (markers[pos] == NULL) markers[pos] = new MarkerHandleSet; markers[pos]->CombineWith(markers[pos + 1]); delete markers[pos + 1]; markers[pos + 1] = NULL; } } void LineVector::DeleteMark(int line, int markerNum, bool all) { if (markers.Length() && markers[line]) { if (markerNum == -1) { delete markers[line]; markers[line] = NULL; } else { bool performedDeletion = markers[line]->RemoveNumber(markerNum); while (all && performedDeletion) { performedDeletion = markers[line]->RemoveNumber(markerNum); } if (markers[line]->Length() == 0) { delete markers[line]; markers[line] = NULL; } } } } void LineVector::DeleteMarkFromHandle(int markerHandle) { int line = LineFromHandle(markerHandle); if (line >= 0) { markers[line]->RemoveHandle(markerHandle); if (markers[line]->Length() == 0) { delete markers[line]; markers[line] = NULL; } } } int LineVector::LineFromHandle(int markerHandle) { if (markers.Length()) { for (int line = 0; line < Lines(); line++) { if (markers[line]) { if (markers[line]->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, bool &startSequence) { 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; } int oldCurrentAction = currentAction; 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))) { // 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) == 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 { // Action coalesced. } } else { // Actions not at top level are always coalesced unless this is after return to top level if (!actions[currentAction].mayCoalesce) currentAction++; } } else { currentAction++; } startSequence = oldCurrentAction != 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() { readOnly = false; collectingUndo = true; } CellBuffer::~CellBuffer() { } char CellBuffer::CharAt(int position) { return substance.ValueAt(position); } void CellBuffer::GetCharRange(char *buffer, int position, int lengthRetrieve) { if (lengthRetrieve < 0) return; if (position < 0) return; if ((position + lengthRetrieve) > substance.Length()) { Platform::DebugPrintf("Bad GetCharRange %d for %d of %d\n", position, lengthRetrieve, substance.Length()); return; } for (int i=0; i((curVal & ~mask) | styleValue)); return true; } else { return false; } } bool CellBuffer::SetStyleFor(int position, int lengthStyle, char styleValue, char mask) { bool changed = false; PLATFORM_ASSERT(lengthStyle == 0 || (lengthStyle > 0 && lengthStyle + position <= style.Length())); while (lengthStyle--) { char curVal = style.ValueAt(position); if ((curVal & mask) != styleValue) { style.SetValueAt(position, static_cast((curVal & ~mask) | styleValue)); changed = true; } position++; } return changed; } // The char* returned is to an allocation owned by the undo history const char *CellBuffer::DeleteChars(int position, int deleteLength, bool &startSequence) { // InsertString and DeleteChars are the bottleneck though which all changes occur PLATFORM_ASSERT(deleteLength > 0); char *data = 0; if (!readOnly) { if (collectingUndo) { // Save into the undo/redo stack, but only the characters - not the formatting data = new char[deleteLength]; for (int i = 0; i < deleteLength; i++) { data[i] = substance.ValHTTP/1.1 200 OK Connection: keep-alive Connection: keep-alive Content-Disposition: inline; filename="CellBuffer.cxx" Content-Disposition: inline; filename="CellBuffer.cxx" Content-Length: 22871 Content-Length: 22871 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: Tue, 21 Oct 2025 02:27:01 UTC ETag: "d7c9c86f4627d52aefb8d81edaa7ab4082f3d89a" ETag: "d7c9c86f4627d52aefb8d81edaa7ab4082f3d89a" Expires: Fri, 19 Oct 2035 02:27:00 GMT Expires: Fri, 19 Oct 2035 02:27:01 GMT Last-Modified: Tue, 21 Oct 2025 02:27:00 GMT Last-Modified: Tue, 21 Oct 2025 02:27:01 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 "SplitVector.h" #include "Partitioning.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() const { int c = 0; MarkerHandleNumber *mhn = root; while (mhn) { c++; mhn = mhn->next; } return c; } int MarkerHandleSet::NumberFromHandle(int handle) const { MarkerHandleNumber *mhn = root; while (mhn) { if (mhn->handle == handle) { return mhn->number; } mhn = mhn->next; } return - 1; } int MarkerHandleSet::MarkValue() const { unsigned int m = 0; MarkerHandleNumber *mhn = root; while (mhn) { m |= (1 << mhn->number); mhn = mhn->next; } return m; } bool MarkerHandleSet::Contains(int handle) const { 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() : starts(256) { handleCurrent = 1; Init(); } LineVector::~LineVector() { starts.DeleteAll(); for (int line = 0; line < markers.Length(); line++) { delete markers[line]; markers[line] = 0; } markers.DeleteAll(); levels.DeleteAll(); } void LineVector::Init() { starts.DeleteAll(); for (int line = 0; line < markers.Length(); line++) { delete markers[line]; markers[line] = 0; } markers.DeleteAll(); levels.DeleteAll(); } void LineVector::ExpandLevels(int sizeNew) { levels.InsertValue(levels.Length(), sizeNew - levels.Length(), SC_FOLDLEVELBASE); } void LineVector::ClearLevels() { levels.DeleteAll(); } int LineVector::SetLevel(int line, int level) { int prev = 0; if ((line >= 0) && (line < Lines())) { if (!levels.Length()) { ExpandLevels(Lines() + 1); } prev = levels[line]; if (prev != level) { levels[line] = level; } } return prev; } int LineVector::GetLevel(int line) { if (levels.Length() && (line >= 0) && (line < Lines())) { return levels[line]; } else { return SC_FOLDLEVELBASE; } } void LineVector::InsertText(int line, int delta) { starts.InsertText(line, delta); } void LineVector::InsertLine(int line, int position) { starts.InsertPartition(line, position); if (markers.Length()) { markers.Insert(line, 0); } if (levels.Length()) { int level = SC_FOLDLEVELBASE; if ((line > 0) && (line < Lines())) { level = levels[line-1] & ~SC_FOLDLEVELWHITEFLAG; } levels.InsertValue(line, 1, level); } } void LineVector::SetLineStart(int line, int position) { starts.SetPartitionStartPosition(line, position); } void LineVector::RemoveLine(int line) { starts.RemovePartition(line); // Retain the markers from the deleted line by oring them into the previous line if (markers.Length()) { if (line > 0) { MergeMarkers(line - 1); } markers.Delete(line); } if (levels.Length()) { // Move up following lines but merge header flag from this line // to line before to avoid a temporary disappearence causing expansion. int firstHeader = levels[line] & SC_FOLDLEVELHEADERFLAG; levels.Delete(line); if (line > 0) levels[line-1] |= firstHeader; } } int LineVector::LineFromPosition(int pos) { return starts.PartitionFromPosition(pos); } int LineVector::MarkValue(int line) { if (markers.Length() && markers[line]) return markers[line]->MarkValue(); else return 0; } int LineVector::AddMark(int line, int markerNum) { handleCurrent++; if (!markers.Length()) { // No existing markers so allocate one element per line markers.InsertValue(0, Lines(), 0); } if (!markers[line]) { // Need new structure to hold marker handle markers[line] = new MarkerHandleSet(); if (!markers[line]) return - 1; } markers[line]->InsertHandle(handleCurrent, markerNum); return handleCurrent; } void LineVector::MergeMarkers(int pos) { if (markers[pos + 1] != NULL) { if (markers[pos] == NULL) markers[pos] = new MarkerHandleSet; markers[pos]->CombineWith(markers[pos + 1]); delete markers[pos + 1]; markers[pos + 1] = NULL; } } void LineVector::DeleteMark(int line, int markerNum, bool all) { if (markers.Length() && markers[line]) { if (markerNum == -1) { delete marker