📄 cellbuffer.cxx
字号:
// 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 uses ByteAt, a position outside the range of the buffer// can be retrieved. All characters outside the range have the value '\0'.char CellBuffer::ByteAt(int position) { if (position < part1len) { if (position < 0) { return '\0'; } else { return body[position]; } } else { if (position >= length) { return '\0'; } else { return part2body[position]; } }}void CellBuffer::SetByteAt(int position, char ch) { if (position < 0) { //Platform::DebugPrintf("Bad position %d\n",position); return ; } if (position >= length + 11) { Platform::DebugPrintf("Very Bad position %d of %d\n", position, length); //exit(2); return ; } if (position >= length) { //Platform::DebugPrintf("Bad position %d of %d\n",position,length); return ; } if (position < part1len) { body[position] = ch; } else { part2body[position] = ch; }}char CellBuffer::CharAt(int position) { return ByteAt(position*2);}void CellBuffer::GetCharRange(char *buffer, int position, int lengthRetrieve) { if (lengthRetrieve < 0) return ; if (position < 0) return ; int bytePos = position * 2; if ((bytePos + lengthRetrieve * 2) > length) { Platform::DebugPrintf("Bad GetCharRange %d for %d of %d\n", bytePos, lengthRetrieve, length); return ; } GapTo(0); // Move the buffer so its easy to subscript into it char *pb = part2body + bytePos; while (lengthRetrieve--) { *buffer++ = *pb; pb += 2; }}char CellBuffer::StyleAt(int position) { return ByteAt(position*2 + 1);}const char *CellBuffer::InsertString(int position, char *s, int insertLength) { char *data = 0; // InsertString and DeleteChars are the bottleneck though which all changes occur if (!readOnly) { if (collectingUndo) { // Save into the undo/redo stack, but only the characters - not the formatting // This takes up about half load time data = new char[insertLength / 2]; for (int i = 0; i < insertLength / 2; i++) { data[i] = s[i * 2]; } uh.AppendAction(insertAction, position / 2, data, insertLength / 2); } BasicInsertString(position, s, insertLength); } return data;}bool CellBuffer::SetStyleAt(int position, char style, char mask) { style &= mask; char curVal = ByteAt(position * 2 + 1); if ((curVal & mask) != style) { SetByteAt(position*2 + 1, static_cast<char>((curVal & ~mask) | style)); return true; } else { return false; }}bool CellBuffer::SetStyleFor(int position, int lengthStyle, char style, char mask) { int bytePos = position * 2 + 1; bool changed = false; PLATFORM_ASSERT(lengthStyle == 0 || (lengthStyle > 0 && lengthStyle + position < length)); while (lengthStyle--) { char curVal = ByteAt(bytePos); if ((curVal & mask) != style) { SetByteAt(bytePos, static_cast<char>((curVal & ~mask) | style)); changed = true; } bytePos += 2; } return changed;}const char *CellBuffer::DeleteChars(int position, int deleteLength) { // 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 / 2]; for (int i = 0; i < deleteLength / 2; i++) { data[i] = ByteAt(position + i * 2); } uh.AppendAction(removeAction, position / 2, data, deleteLength / 2); } BasicDeleteChars(position, deleteLength); } return data;}int CellBuffer::ByteLength() { return length;}int CellBuffer::Length() { return ByteLength() / 2;}void CellBuffer::Allocate(int newSize) { if (newSize > length) { GapTo(length); char *newBody = new char[newSize]; memcpy(newBody, body, length); delete []body; body = newBody; gaplen += newSize - size; part2body = body + gaplen; size = newSize; }}int CellBuffer::Lines() { //Platform::DebugPrintf("Lines = %d\n", lv.lines); return lv.lines;}int CellBuffer::LineStart(int line) { if (line < 0) return 0; else if (line > lv.lines) return Length(); else return lv.linesData[line].startPosition;}bool CellBuffer::IsReadOnly() { return readOnly;}void CellBuffer::SetReadOnly(bool set) { readOnly = set;}void CellBuffer::SetSavePoint() { uh.SetSavePoint();}bool CellBuffer::IsSavePoint() { return uh.IsSavePoint();}int CellBuffer::AddMark(int line, int markerNum) { if ((line >= 0) && (line < lv.lines)) { return lv.AddMark(line, markerNum); } return - 1;}void CellBuffer::DeleteMark(int line, int markerNum) { if ((line >= 0) && (line < lv.lines)) { lv.DeleteMark(line, markerNum, false); }}void CellBuffer::DeleteMarkFromHandle(int markerHandle) { lv.DeleteMarkFromHandle(markerHandle);}int CellBuffer::GetMark(int line) { if ((line >= 0) && (line < lv.lines) && (lv.linesData[line].handleSet)) return lv.linesData[line].handleSet->MarkValue(); return 0;}void CellBuffer::DeleteAllMarks(int markerNum) { for (int line = 0; line < lv.lines; line++) { lv.DeleteMark(line, markerNum, true); }}int CellBuffer::LineFromHandle(int markerHandle) { return lv.LineFromHandle(markerHandle);}// Without undovoid CellBuffer::BasicInsertString(int position, char *s, int insertLength) { //Platform::DebugPrintf("Inserting at %d for %d\n", position, insertLength); if (insertLength == 0) return ; PLATFORM_ASSERT(insertLength > 0); RoomFor(insertLength); GapTo(position); memcpy(body + part1len, s, insertLength); length += insertLength; part1len += insertLength; gaplen -= insertLength; part2body = body + gaplen; int lineInsert = lv.LineFromPosition(position / 2) + 1; // Point all the lines after the insertion point further along in the buffer for (int lineAfter = lineInsert; lineAfter <= lv.lines; lineAfter++) { lv.linesData[lineAfter].startPosition += insertLength / 2; } char chPrev = ' '; if ((position - 2) >= 0) chPrev = ByteAt(position - 2); char chAfter = ' '; if ((position + insertLength) < length) chAfter = ByteAt(position + insertLength); if (chPrev == '\r' && chAfter == '\n') { //Platform::DebugPrintf("Splitting a crlf pair at %d\n", lineInsert); // Splitting up a crlf pair at position lv.InsertValue(lineInsert, position / 2); lineInsert++; } char ch = ' '; for (int i = 0; i < insertLength; i += 2) { ch = s[i]; if (ch == '\r') { //Platform::DebugPrintf("Inserting cr at %d\n", lineInsert); lv.InsertValue(lineInsert, (position + i) / 2 + 1); lineInsert++; } else if (ch == '\n') { if (chPrev == '\r') { //Platform::DebugPrintf("Patching cr before lf at %d\n", lineInsert-1); // Patch up what was end of line lv.SetValue(lineInsert - 1, (position + i) / 2 + 1); } else { //Platform::DebugPrintf("Inserting lf at %d\n", lineInsert); lv.InsertValue(lineInsert, (position + i) / 2 + 1); lineInsert++; } } chPrev = ch; } // Joining two lines where last insertion is cr and following text starts with lf if (chAfter == '\n') { if (ch == '\r') { //Platform::DebugPrintf("Joining cr before lf at %d\n", lineInsert-1); // End of line already in buffer so drop the newly created one lv.Remove(lineInsert - 1); } }}void CellBuffer::BasicDeleteChars(int position, int deleteLength) { //Platform::DebugPrintf("Deleting at %d for %d\n", position, deleteLength); if (deleteLength == 0) return ; if ((position == 0) && (deleteLength == length)) { // If whole buffer is being deleted, faster to reinitialise lines data // than to delete each line. //printf("Whole buffer being deleted\n"); lv.Init(); } else { // Have to fix up line positions before doing deletion as looking at text in buffer // to work out which lines have been removed int lineRemove = lv.LineFromPosition(position / 2) + 1; // Point all the lines after the insertion point further along in the buffer for (int lineAfter = lineRemove; lineAfter <= lv.lines; lineAfter++) { lv.linesData[lineAfter].startPosition -= deleteLength / 2; } char chPrev = ' '; if (position >= 2) chPrev = ByteAt(position - 2); char chBefore = chPrev; char chNext = ' '; if (position < length) chNext = ByteAt(position); bool ignoreNL = false; if (chPrev == '\r' && chNext == '\n') { //Platform::DebugPrintf("Deleting lf after cr, move line end to cr at %d\n", lineRemove); // Move back one lv.SetValue(lineRemove, position / 2); lineRemove++; ignoreNL = true; // First \n is not real deletion } char ch = chNext; for (int i = 0; i < deleteLength; i += 2) { chNext = ' '; if ((position + i + 2) < length) chNext = ByteAt(position + i + 2); //Platform::DebugPrintf("Deleting %d %x\n", i, ch); if (ch == '\r') { if (chNext != '\n') { //Platform::DebugPrintf("Removing cr end of line\n"); lv.Remove(lineRemove); } } else if (ch == '\n') { if (ignoreNL) { ignoreNL = false; // Further \n are real deletions } else { //Platform::DebugPrintf("Removing lf end of line\n"); lv.Remove(lineRemove); } } ch = chNext; } // May have to fix up end if last deletion causes cr to be next to lf // or removes one of a crlf pair char chAfter = ' '; if ((position + deleteLength) < length) chAfter = ByteAt(position + deleteLength); if (chBefore == '\r' && chAfter == '\n') { //d.printf("Joining cr before lf at %d\n", lineRemove); // Using lineRemove-1 as cr ended line before start of deletion lv.Remove(lineRemove - 1); lv.SetValue(lineRemove - 1, position / 2 + 1); } } GapTo(position); length -= deleteLength; gaplen += deleteLength; part2body = body + gaplen;}bool CellBuffer::SetUndoCollection(bool collectUndo) { collectingUndo = collectUndo; uh.DropUndoSequence(); return collectingUndo;}bool CellBuffer::IsCollectingUndo() { return collectingUndo;}void CellBuffer::BeginUndoAction() { uh.BeginUndoAction();}void CellBuffer::EndUndoAction() { uh.EndUndoAction();}void CellBuffer::DeleteUndoHistory() { uh.DeleteUndoHistory();}bool CellBuffer::CanUndo() { return uh.CanUndo();}int CellBuffer::StartUndo() { return uh.StartUndo();}const Action &CellBuffer::GetUndoStep() const { return uh.GetUndoStep();}void CellBuffer::PerformUndoStep() { const Action &actionStep = uh.GetUndoStep(); if (actionStep.at == insertAction) { BasicDeleteChars(actionStep.position*2, actionStep.lenData*2); } else if (actionStep.at == removeAction) { char *styledData = new char[actionStep.lenData * 2]; for (int i = 0; i < actionStep.lenData; i++) { styledData[i*2] = actionStep.data[i]; styledData[i*2 + 1] = 0; } BasicInsertString(actionStep.position*2, styledData, actionStep.lenData*2); delete []styledData; } uh.CompletedUndoStep();}bool CellBuffer::CanRedo() { return uh.CanRedo();}int CellBuffer::StartRedo() { return uh.StartRedo();}const Action &CellBuffer::GetRedoStep() const { return uh.GetRedoStep();}void CellBuffer::PerformRedoStep() { const Action &actionStep = uh.GetRedoStep(); if (actionStep.at == insertAction) { char *styledData = new char[actionStep.lenData * 2]; for (int i = 0; i < actionStep.lenData; i++) { styledData[i*2] = actionStep.data[i]; styledData[i*2 + 1] = 0; } BasicInsertString(actionStep.position*2, styledData, actionStep.lenData*2); delete []styledData; } else if (actionStep.at == removeAction) { BasicDeleteChars(actionStep.position*2, actionStep.lenData*2); } uh.CompletedRedoStep();}int CellBuffer::SetLineState(int line, int state) { int stateOld = lineStates[line]; lineStates[line] = state; return stateOld;}int CellBuffer::GetLineState(int line) { return lineStates[line];}int CellBuffer::GetMaxLineState() { return lineStates.Length();}int CellBuffer::SetLevel(int line, int level) { int prev = 0; if ((line >= 0) && (line < lv.lines)) { if (!lv.levels) { lv.ExpandLevels(); } prev = lv.levels[line]; if (lv.levels[line] != level) { lv.levels[line] = level; } } return prev;}int CellBuffer::GetLevel(int line) { if (lv.levels && (line >= 0) && (line < lv.lines)) { return lv.levels[line]; } else { return SC_FOLDLEVELBASE; }}void CellBuffer::ClearLevels() { lv.ClearLevels();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -