]>
git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/CellBuffer.cxx
ecb5bc1801554749e8146743a134125639ffea2a
1 // Scintilla source code edit control
2 /** @file CellBuffer.cxx
3 ** Manages a buffer of cells.
5 // Copyright 1998-2001 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
15 #include "Scintilla.h"
17 #include "CellBuffer.h"
19 MarkerHandleSet::MarkerHandleSet() {
23 MarkerHandleSet::~MarkerHandleSet() {
24 MarkerHandleNumber
*mhn
= root
;
26 MarkerHandleNumber
*mhnToFree
= mhn
;
33 int MarkerHandleSet::Length() {
35 MarkerHandleNumber
*mhn
= root
;
43 int MarkerHandleSet::NumberFromHandle(int handle
) {
44 MarkerHandleNumber
*mhn
= root
;
46 if (mhn
->handle
== handle
) {
54 int MarkerHandleSet::MarkValue() {
56 MarkerHandleNumber
*mhn
= root
;
58 m
|= (1 << mhn
->number
);
64 bool MarkerHandleSet::Contains(int handle
) {
65 MarkerHandleNumber
*mhn
= root
;
67 if (mhn
->handle
== handle
) {
75 bool MarkerHandleSet::InsertHandle(int handle
, int markerNum
) {
76 MarkerHandleNumber
*mhn
= new MarkerHandleNumber
;
80 mhn
->number
= markerNum
;
86 void MarkerHandleSet::RemoveHandle(int handle
) {
87 MarkerHandleNumber
**pmhn
= &root
;
89 MarkerHandleNumber
*mhn
= *pmhn
;
90 if (mhn
->handle
== handle
) {
95 pmhn
= &((*pmhn
)->next
);
99 void MarkerHandleSet::RemoveNumber(int markerNum
) {
100 MarkerHandleNumber
**pmhn
= &root
;
102 MarkerHandleNumber
*mhn
= *pmhn
;
103 if (mhn
->number
== markerNum
) {
108 pmhn
= &((*pmhn
)->next
);
112 void MarkerHandleSet::CombineWith(MarkerHandleSet
*other
) {
113 MarkerHandleNumber
**pmhn
= &root
;
115 pmhn
= &((*pmhn
)->next
);
121 LineVector::LineVector() {
132 LineVector::~LineVector() {
133 for (int line
= 0; line
< lines
; line
++) {
134 delete linesData
[line
].handleSet
;
135 linesData
[line
].handleSet
= 0;
143 void LineVector::Init() {
144 for (int line
= 0; line
< lines
; line
++) {
145 delete linesData
[line
].handleSet
;
146 linesData
[line
].handleSet
= 0;
149 linesData
= new LineData
[static_cast<int>(growSize
)];
157 void LineVector::Expand(int sizeNew
) {
158 LineData
*linesDataNew
= new LineData
[sizeNew
];
160 for (int i
= 0; i
< size
; i
++)
161 linesDataNew
[i
] = linesData
[i
];
162 // Do not delete handleSets here as they are transferred to new linesData
164 linesData
= linesDataNew
;
167 Platform::DebugPrintf("No memory available\n");
173 void LineVector::ExpandLevels(int sizeNew
) {
176 int *levelsNew
= new int[sizeNew
];
179 for (; i
< sizeLevels
; i
++)
180 levelsNew
[i
] = levels
[i
];
181 for (; i
< sizeNew
; i
++)
182 levelsNew
[i
] = SC_FOLDLEVELBASE
;
185 sizeLevels
= sizeNew
;
187 Platform::DebugPrintf("No memory available\n");
193 void LineVector::ClearLevels() {
199 void LineVector::InsertValue(int pos
, int value
) {
200 //Platform::DebugPrintf("InsertValue[%d] = %d\n", pos, value);
201 if ((lines
+ 2) >= size
) {
202 Expand(size
+ growSize
);
204 ExpandLevels(size
+ growSize
);
208 for (int i
= lines
; i
> pos
; i
--) {
209 linesData
[i
] = linesData
[i
- 1];
211 linesData
[pos
].startPosition
= value
;
212 linesData
[pos
].handleSet
= 0;
214 for (int j
= lines
; j
> pos
; j
--) {
215 levels
[j
] = levels
[j
- 1];
218 levels
[pos
] = SC_FOLDLEVELBASE
;
219 } else if (pos
== (lines
- 1)) { // Last line will not be a folder
220 levels
[pos
] = SC_FOLDLEVELBASE
;
222 levels
[pos
] = levels
[pos
- 1];
227 void LineVector::SetValue(int pos
, int value
) {
228 //Platform::DebugPrintf("SetValue[%d] = %d\n", pos, value);
229 if ((pos
+ 2) >= size
) {
230 //Platform::DebugPrintf("Resize %d %d\n", size,pos);
231 Expand(pos
+ growSize
);
232 //Platform::DebugPrintf("end Resize %d %d\n", size,pos);
235 ExpandLevels(pos
+ growSize
);
238 linesData
[pos
].startPosition
= value
;
241 void LineVector::Remove(int pos
) {
242 //Platform::DebugPrintf("Remove %d\n", pos);
243 // Retain the markers from the deleted line by oring them into the previous line
245 MergeMarkers(pos
- 1);
247 for (int i
= pos
; i
< lines
; i
++) {
248 linesData
[i
] = linesData
[i
+ 1];
251 // Level information merges back onto previous line
252 int posAbove
= pos
- 1;
255 for (int j
= posAbove
; j
< lines
; j
++) {
256 levels
[j
] = levels
[j
+ 1];
262 int LineVector::LineFromPosition(int pos
) {
263 //Platform::DebugPrintf("LineFromPostion %d lines=%d end = %d\n", pos, lines, linesData[lines].startPosition);
266 //Platform::DebugPrintf("LineFromPosition %d\n", pos);
267 if (pos
>= linesData
[lines
].startPosition
)
272 int middle
= (upper
+ lower
+ 1) / 2; // Round high
273 if (pos
< linesData
[middle
].startPosition
) {
278 } while (lower
< upper
);
279 //Platform::DebugPrintf("LineFromPostion %d %d %d\n", pos, lower, linesData[lower].startPosition, linesData[lower > 1 ? lower - 1 : 0].startPosition);
283 int LineVector::AddMark(int line
, int markerNum
) {
285 if (!linesData
[line
].handleSet
) {
286 // Need new structure to hold marker handle
287 linesData
[line
].handleSet
= new MarkerHandleSet
;
288 if (!linesData
[line
].handleSet
)
291 linesData
[line
].handleSet
->InsertHandle(handleCurrent
, markerNum
);
293 return handleCurrent
;
296 void LineVector::MergeMarkers(int pos
) {
297 if (linesData
[pos
+ 1].handleSet
!= NULL
) {
298 if (linesData
[pos
].handleSet
== NULL
)
299 linesData
[pos
].handleSet
= new MarkerHandleSet
;
300 linesData
[pos
].handleSet
->CombineWith(linesData
[pos
+ 1].handleSet
);
301 delete linesData
[pos
+ 1].handleSet
;
302 linesData
[pos
+ 1].handleSet
= NULL
;
306 void LineVector::DeleteMark(int line
, int markerNum
) {
307 if (linesData
[line
].handleSet
) {
308 if (markerNum
== -1) {
309 delete linesData
[line
].handleSet
;
310 linesData
[line
].handleSet
= 0;
312 linesData
[line
].handleSet
->RemoveNumber(markerNum
);
313 if (linesData
[line
].handleSet
->Length() == 0) {
314 delete linesData
[line
].handleSet
;
315 linesData
[line
].handleSet
= 0;
321 void LineVector::DeleteMarkFromHandle(int markerHandle
) {
322 int line
= LineFromHandle(markerHandle
);
324 linesData
[line
].handleSet
->RemoveHandle(markerHandle
);
325 if (linesData
[line
].handleSet
->Length() == 0) {
326 delete linesData
[line
].handleSet
;
327 linesData
[line
].handleSet
= 0;
332 int LineVector::LineFromHandle(int markerHandle
) {
333 for (int line
= 0; line
< lines
; line
++) {
334 if (linesData
[line
].handleSet
) {
335 if (linesData
[line
].handleSet
->Contains(markerHandle
)) {
354 void Action::Create(actionType at_
, int position_
, char *data_
, int lenData_
, bool mayCoalesce_
) {
356 position
= position_
;
360 mayCoalesce
= mayCoalesce_
;
363 void Action::Destroy() {
368 void Action::Grab(Action
*source
) {
371 position
= source
->position
;
374 lenData
= source
->lenData
;
375 mayCoalesce
= source
->mayCoalesce
;
377 // Ownership of source data transferred to this
378 source
->position
= 0;
379 source
->at
= startAction
;
382 source
->mayCoalesce
= true;
385 // The undo history stores a sequence of user operations that represent the user's view of the
386 // commands executed on the text.
387 // Each user operation contains a sequence of text insertion and text deletion actions.
388 // All the user operations are stored in a list of individual actions with 'start' actions used
389 // as delimiters between user operations.
390 // Initially there is one start action in the history.
391 // As each action is performed, it is recorded in the history. The action may either become
392 // part of the current user operation or may start a new user operation. If it is to be part of the
393 // current operation, then it overwrites the current last action. If it is to be part of a new
394 // operation, it is appended after the current last action.
395 // After writing the new action, a new start action is appended at the end of the history.
396 // The decision of whether to start a new user operation is based upon two factors. If a
397 // compound operation has been explicitly started by calling BeginUndoAction and no matching
398 // EndUndoAction (these calls nest) has been called, then the action is coalesced into the current
399 // operation. If there is no outstanding BeginUndoAction call then a new operation is started
400 // unless it looks as if the new action is caused by the user typing or deleting a stream of text.
401 // Sequences that look like typing or deletion are coalesced into a single user operation.
403 UndoHistory::UndoHistory() {
406 actions
= new Action
[lenActions
];
409 undoSequenceDepth
= 0;
412 actions
[currentAction
].Create(startAction
);
415 UndoHistory::~UndoHistory() {
420 void UndoHistory::EnsureUndoRoom() {
421 // Have to test that there is room for 2 more actions in the array
422 // as two actions may be created by the calling function
423 if (currentAction
>= (lenActions
- 2)) {
424 // Run out of undo nodes so extend the array
425 int lenActionsNew
= lenActions
* 2;
426 Action
*actionsNew
= new Action
[lenActionsNew
];
429 for (int act
= 0; act
<= currentAction
; act
++)
430 actionsNew
[act
].Grab(&actions
[act
]);
432 lenActions
= lenActionsNew
;
433 actions
= actionsNew
;
437 void UndoHistory::AppendAction(actionType at
, int position
, char *data
, int lengthData
) {
439 //Platform::DebugPrintf("%% %d action %d %d %d\n", at, position, lengthData, currentAction);
440 //Platform::DebugPrintf("^ %d action %d %d\n", actions[currentAction - 1].at,
441 // actions[currentAction - 1].position, actions[currentAction - 1].lenData);
442 if (currentAction
>= 1) {
443 if (0 == undoSequenceDepth
) {
444 // Top level actions may not always be coalesced
445 Action
&actPrevious
= actions
[currentAction
- 1];
446 // See if current action can be coalesced into previous action
447 // Will work if both are inserts or deletes and position is same
448 if (at
!= actPrevious
.at
) {
450 } else if (currentAction
== savePoint
) {
452 } else if ((at
== insertAction
) &&
453 (position
!= (actPrevious
.position
+ actPrevious
.lenData
*2))) {
454 // Insertions must be immediately after to coalesce
456 } else if (!actions
[currentAction
].mayCoalesce
) {
457 // Not allowed to coalesce if this set
459 } else if (at
== removeAction
) {
460 if ((lengthData
== 1) || (lengthData
== 2)){
461 if ((position
+ lengthData
* 2) == actPrevious
.position
) {
463 } else if (position
== actPrevious
.position
) {
466 // Removals must be at same position to coalesce
470 // Removals must be of one character to coalesce
474 //Platform::DebugPrintf("action coalesced\n");
478 // Actions not at top level are always coalesced unless this is after return to top level
479 if (!actions
[currentAction
].mayCoalesce
)
485 actions
[currentAction
].Create(at
, position
, data
, lengthData
);
487 actions
[currentAction
].Create(startAction
);
488 maxAction
= currentAction
;
491 void UndoHistory::BeginUndoAction() {
493 if (undoSequenceDepth
== 0) {
494 if (actions
[currentAction
].at
!= startAction
) {
496 actions
[currentAction
].Create(startAction
);
497 maxAction
= currentAction
;
499 actions
[currentAction
].mayCoalesce
= false;
504 void UndoHistory::EndUndoAction() {
507 if (0 == undoSequenceDepth
) {
508 if (actions
[currentAction
].at
!= startAction
) {
510 actions
[currentAction
].Create(startAction
);
511 maxAction
= currentAction
;
513 actions
[currentAction
].mayCoalesce
= false;
517 void UndoHistory::DropUndoSequence() {
518 undoSequenceDepth
= 0;
521 void UndoHistory::DeleteUndoHistory() {
522 for (int i
= 1; i
< maxAction
; i
++)
523 actions
[i
].Destroy();
526 actions
[currentAction
].Create(startAction
);
530 void UndoHistory::SetSavePoint() {
531 savePoint
= currentAction
;
534 bool UndoHistory::IsSavePoint() const {
535 return savePoint
== currentAction
;
538 bool UndoHistory::CanUndo() const {
539 return (currentAction
> 0) && (maxAction
> 0);
542 int UndoHistory::StartUndo() {
543 // Drop any trailing startAction
544 if (actions
[currentAction
].at
== startAction
&& currentAction
> 0)
547 // Count the steps in this action
548 int act
= currentAction
;
549 while (actions
[act
].at
!= startAction
&& act
> 0) {
552 return currentAction
- act
;
555 const Action
&UndoHistory::GetUndoStep() const {
556 return actions
[currentAction
];
559 void UndoHistory::CompletedUndoStep() {
563 bool UndoHistory::CanRedo() const {
564 return maxAction
> currentAction
;
567 int UndoHistory::StartRedo() {
568 // Drop any leading startAction
569 if (actions
[currentAction
].at
== startAction
&& currentAction
< maxAction
)
572 // Count the steps in this action
573 int act
= currentAction
;
574 while (actions
[act
].at
!= startAction
&& act
< maxAction
) {
577 return act
- currentAction
;
580 const Action
&UndoHistory::GetRedoStep() const {
581 return actions
[currentAction
];
584 void UndoHistory::CompletedRedoStep() {
588 CellBuffer::CellBuffer(int initialLength
) {
589 body
= new char[initialLength
];
590 size
= initialLength
;
593 gaplen
= initialLength
;
594 part2body
= body
+ gaplen
;
596 collectingUndo
= true;
600 CellBuffer::~CellBuffer() {
605 void CellBuffer::GapTo(int position
) {
606 if (position
== part1len
)
608 if (position
< part1len
) {
609 int diff
= part1len
- position
;
610 //Platform::DebugPrintf("Move gap backwards to %d diff = %d part1len=%d length=%d \n", position,diff, part1len, length);
611 for (int i
= 0; i
< diff
; i
++)
612 body
[part1len
+ gaplen
- i
- 1] = body
[part1len
- i
- 1];
613 } else { // position > part1len
614 int diff
= position
- part1len
;
615 //Platform::DebugPrintf("Move gap forwards to %d diff =%d\n", position,diff);
616 for (int i
= 0; i
< diff
; i
++)
617 body
[part1len
+ i
] = body
[part1len
+ gaplen
+ i
];
620 part2body
= body
+ gaplen
;
623 void CellBuffer::RoomFor(int insertionLength
) {
624 //Platform::DebugPrintf("need room %d %d\n", gaplen, insertionLength);
625 if (gaplen
<= insertionLength
) {
626 //Platform::DebugPrintf("need room %d %d\n", gaplen, insertionLength);
628 if (growSize
* 6 < size
)
630 int newSize
= size
+ insertionLength
+ growSize
;
631 //Platform::DebugPrintf("moved gap %d\n", newSize);
632 char *newBody
= new char[newSize
];
633 memcpy(newBody
, body
, size
);
636 gaplen
+= newSize
- size
;
637 part2body
= body
+ gaplen
;
639 //Platform::DebugPrintf("end need room %d %d - size=%d length=%d\n", gaplen, insertionLength,size,length);
644 // To make it easier to write code that uses ByteAt, a position outside the range of the buffer
645 // can be retrieved. All characters outside the range have the value '\0'.
646 char CellBuffer::ByteAt(int position
) {
647 if (position
< part1len
) {
651 return body
[position
];
654 if (position
>= length
) {
657 return part2body
[position
];
662 void CellBuffer::SetByteAt(int position
, char ch
) {
665 //Platform::DebugPrintf("Bad position %d\n",position);
668 if (position
>= length
+ 11) {
669 Platform::DebugPrintf("Very Bad position %d of %d\n", position
, length
);
673 if (position
>= length
) {
674 //Platform::DebugPrintf("Bad position %d of %d\n",position,length);
678 if (position
< part1len
) {
681 part2body
[position
] = ch
;
685 char CellBuffer::CharAt(int position
) {
686 return ByteAt(position
*2);
689 void CellBuffer::GetCharRange(char *buffer
, int position
, int lengthRetrieve
) {
690 if (lengthRetrieve
< 0)
694 int bytePos
= position
* 2;
695 if ((bytePos
+ lengthRetrieve
* 2) > length
) {
696 Platform::DebugPrintf("Bad GetCharRange %d for %d of %d\n", bytePos
,
697 lengthRetrieve
, length
);
700 GapTo(0); // Move the buffer so its easy to subscript into it
701 char *pb
= part2body
+ bytePos
;
702 while (lengthRetrieve
--) {
708 char CellBuffer::StyleAt(int position
) {
709 return ByteAt(position
*2 + 1);
712 const char *CellBuffer::InsertString(int position
, char *s
, int insertLength
) {
714 // InsertString and DeleteChars are the bottleneck though which all changes occur
716 if (collectingUndo
) {
717 // Save into the undo/redo stack, but only the characters - not the formatting
718 // This takes up about half load time
719 data
= new char[insertLength
/ 2];
720 for (int i
= 0; i
< insertLength
/ 2; i
++) {
723 uh
.AppendAction(insertAction
, position
, data
, insertLength
/ 2);
726 BasicInsertString(position
, s
, insertLength
);
731 void CellBuffer::InsertCharStyle(int position
, char ch
, char style
) {
735 InsertString(position
*2, s
, 2);
738 bool CellBuffer::SetStyleAt(int position
, char style
, char mask
) {
739 char curVal
= ByteAt(position
* 2 + 1);
740 if ((curVal
& mask
) != style
) {
741 SetByteAt(position
*2 + 1, static_cast<char>((curVal
& ~mask
) | style
));
748 bool CellBuffer::SetStyleFor(int position
, int lengthStyle
, char style
, char mask
) {
749 int bytePos
= position
* 2 + 1;
750 bool changed
= false;
751 PLATFORM_ASSERT(lengthStyle
== 0 ||
752 (lengthStyle
> 0 && lengthStyle
+ position
< length
));
753 while (lengthStyle
--) {
754 char curVal
= ByteAt(bytePos
);
755 if ((curVal
& mask
) != style
) {
756 SetByteAt(bytePos
, static_cast<char>((curVal
& ~mask
) | style
));
764 const char *CellBuffer::DeleteChars(int position
, int deleteLength
) {
765 // InsertString and DeleteChars are the bottleneck though which all changes occur
768 if (collectingUndo
) {
769 // Save into the undo/redo stack, but only the characters - not the formatting
770 data
= new char[deleteLength
/ 2];
771 for (int i
= 0; i
< deleteLength
/ 2; i
++) {
772 data
[i
] = ByteAt(position
+ i
* 2);
774 uh
.AppendAction(removeAction
, position
, data
, deleteLength
/ 2);
777 BasicDeleteChars(position
, deleteLength
);
782 int CellBuffer::ByteLength() {
786 int CellBuffer::Length() {
787 return ByteLength() / 2;
790 int CellBuffer::Lines() {
791 //Platform::DebugPrintf("Lines = %d\n", lv.lines);
795 int CellBuffer::LineStart(int line
) {
798 else if (line
> lv
.lines
)
801 return lv
.linesData
[line
].startPosition
;
804 bool CellBuffer::IsReadOnly() {
808 void CellBuffer::SetReadOnly(bool set
) {
812 void CellBuffer::SetSavePoint() {
816 bool CellBuffer::IsSavePoint() {
817 return uh
.IsSavePoint();
820 int CellBuffer::AddMark(int line
, int markerNum
) {
821 if ((line
>= 0) && (line
< lv
.lines
)) {
822 return lv
.AddMark(line
, markerNum
);
827 void CellBuffer::DeleteMark(int line
, int markerNum
) {
828 if ((line
>= 0) && (line
< lv
.lines
)) {
829 lv
.DeleteMark(line
, markerNum
);
833 void CellBuffer::DeleteMarkFromHandle(int markerHandle
) {
834 lv
.DeleteMarkFromHandle(markerHandle
);
837 int CellBuffer::GetMark(int line
) {
838 if ((line
>= 0) && (line
< lv
.lines
) && (lv
.linesData
[line
].handleSet
))
839 return lv
.linesData
[line
].handleSet
->MarkValue();
843 void CellBuffer::DeleteAllMarks(int markerNum
) {
844 for (int line
= 0; line
< lv
.lines
; line
++) {
845 lv
.DeleteMark(line
, markerNum
);
849 int CellBuffer::LineFromHandle(int markerHandle
) {
850 return lv
.LineFromHandle(markerHandle
);
855 void CellBuffer::BasicInsertString(int position
, char *s
, int insertLength
) {
856 //Platform::DebugPrintf("Inserting at %d for %d\n", position, insertLength);
857 if (insertLength
== 0)
859 RoomFor(insertLength
);
862 memcpy(body
+ part1len
, s
, insertLength
);
863 length
+= insertLength
;
864 part1len
+= insertLength
;
865 gaplen
-= insertLength
;
866 part2body
= body
+ gaplen
;
868 int lineInsert
= lv
.LineFromPosition(position
/ 2) + 1;
869 // Point all the lines after the insertion point further along in the buffer
870 for (int lineAfter
= lineInsert
; lineAfter
<= lv
.lines
; lineAfter
++) {
871 lv
.linesData
[lineAfter
].startPosition
+= insertLength
/ 2;
874 if ((position
- 2) >= 0)
875 chPrev
= ByteAt(position
- 2);
877 if ((position
+ insertLength
) < length
)
878 chAfter
= ByteAt(position
+ insertLength
);
879 if (chPrev
== '\r' && chAfter
== '\n') {
880 //Platform::DebugPrintf("Splitting a crlf pair at %d\n", lineInsert);
881 // Splitting up a crlf pair at position
882 lv
.InsertValue(lineInsert
, position
/ 2);
886 for (int i
= 0; i
< insertLength
; i
+= 2) {
889 //Platform::DebugPrintf("Inserting cr at %d\n", lineInsert);
890 lv
.InsertValue(lineInsert
, (position
+ i
) / 2 + 1);
892 } else if (ch
== '\n') {
893 if (chPrev
== '\r') {
894 //Platform::DebugPrintf("Patching cr before lf at %d\n", lineInsert-1);
895 // Patch up what was end of line
896 lv
.SetValue(lineInsert
- 1, (position
+ i
) / 2 + 1);
898 //Platform::DebugPrintf("Inserting lf at %d\n", lineInsert);
899 lv
.InsertValue(lineInsert
, (position
+ i
) / 2 + 1);
905 // Joining two lines where last insertion is cr and following text starts with lf
906 if (chAfter
== '\n') {
908 //Platform::DebugPrintf("Joining cr before lf at %d\n", lineInsert-1);
909 // End of line already in buffer so drop the newly created one
910 lv
.Remove(lineInsert
- 1);
915 void CellBuffer::BasicDeleteChars(int position
, int deleteLength
) {
916 //Platform::DebugPrintf("Deleting at %d for %d\n", position, deleteLength);
917 if (deleteLength
== 0)
920 if ((position
== 0) && (deleteLength
== length
)) {
921 // If whole buffer is being deleted, faster to reinitialise lines data
922 // than to delete each line.
923 //printf("Whole buffer being deleted\n");
926 // Have to fix up line positions before doing deletion as looking at text in buffer
927 // to work out which lines have been removed
929 int lineRemove
= lv
.LineFromPosition(position
/ 2) + 1;
930 // Point all the lines after the insertion point further along in the buffer
931 for (int lineAfter
= lineRemove
; lineAfter
<= lv
.lines
; lineAfter
++) {
932 lv
.linesData
[lineAfter
].startPosition
-= deleteLength
/ 2;
936 chPrev
= ByteAt(position
- 2);
937 char chBefore
= chPrev
;
939 if (position
< length
)
940 chNext
= ByteAt(position
);
941 bool ignoreNL
= false;
942 if (chPrev
== '\r' && chNext
== '\n') {
943 //Platform::DebugPrintf("Deleting lf after cr, move line end to cr at %d\n", lineRemove);
945 lv
.SetValue(lineRemove
, position
/ 2);
947 ignoreNL
= true; // First \n is not real deletion
951 for (int i
= 0; i
< deleteLength
; i
+= 2) {
953 if ((position
+ i
+ 2) < length
)
954 chNext
= ByteAt(position
+ i
+ 2);
955 //Platform::DebugPrintf("Deleting %d %x\n", i, ch);
957 if (chNext
!= '\n') {
958 //Platform::DebugPrintf("Removing cr end of line\n");
959 lv
.Remove(lineRemove
);
961 } else if ((ch
== '\n') && !ignoreNL
) {
962 //Platform::DebugPrintf("Removing lf end of line\n");
963 lv
.Remove(lineRemove
);
964 ignoreNL
= false; // Further \n are not real deletions
969 // May have to fix up end if last deletion causes cr to be next to lf
970 // or removes one of a crlf pair
972 if ((position
+ deleteLength
) < length
)
973 chAfter
= ByteAt(position
+ deleteLength
);
974 if (chBefore
== '\r' && chAfter
== '\n') {
975 //d.printf("Joining cr before lf at %d\n", lineRemove);
976 // Using lineRemove-1 as cr ended line before start of deletion
977 lv
.Remove(lineRemove
- 1);
978 lv
.SetValue(lineRemove
- 1, position
/ 2 + 1);
982 length
-= deleteLength
;
983 gaplen
+= deleteLength
;
984 part2body
= body
+ gaplen
;
987 bool CellBuffer::SetUndoCollection(bool collectUndo
) {
988 collectingUndo
= collectUndo
;
989 uh
.DropUndoSequence();
990 return collectingUndo
;
993 bool CellBuffer::IsCollectingUndo() {
994 return collectingUndo
;
997 void CellBuffer::BeginUndoAction() {
998 uh
.BeginUndoAction();
1001 void CellBuffer::EndUndoAction() {
1005 void CellBuffer::DeleteUndoHistory() {
1006 uh
.DeleteUndoHistory();
1009 bool CellBuffer::CanUndo() {
1010 return (!readOnly
) && (uh
.CanUndo());
1013 int CellBuffer::StartUndo() {
1014 return uh
.StartUndo();
1017 const Action
&CellBuffer::GetUndoStep() const {
1018 return uh
.GetUndoStep();
1021 void CellBuffer::PerformUndoStep() {
1022 const Action
&actionStep
= uh
.GetUndoStep();
1023 if (actionStep
.at
== insertAction
) {
1024 BasicDeleteChars(actionStep
.position
, actionStep
.lenData
*2);
1025 } else if (actionStep
.at
== removeAction
) {
1026 char *styledData
= new char[actionStep
.lenData
* 2];
1027 for (int i
= 0; i
< actionStep
.lenData
; i
++) {
1028 styledData
[i
*2] = actionStep
.data
[i
];
1029 styledData
[i
*2 + 1] = 0;
1031 BasicInsertString(actionStep
.position
, styledData
, actionStep
.lenData
*2);
1032 delete []styledData
;
1034 uh
.CompletedUndoStep();
1037 bool CellBuffer::CanRedo() {
1038 return (!readOnly
) && (uh
.CanRedo());
1041 int CellBuffer::StartRedo() {
1042 return uh
.StartRedo();
1045 const Action
&CellBuffer::GetRedoStep() const {
1046 return uh
.GetRedoStep();
1049 void CellBuffer::PerformRedoStep() {
1050 const Action
&actionStep
= uh
.GetRedoStep();
1051 if (actionStep
.at
== insertAction
) {
1052 char *styledData
= new char[actionStep
.lenData
* 2];
1053 for (int i
= 0; i
< actionStep
.lenData
; i
++) {
1054 styledData
[i
*2] = actionStep
.data
[i
];
1055 styledData
[i
*2 + 1] = 0;
1057 BasicInsertString(actionStep
.position
, styledData
, actionStep
.lenData
*2);
1058 delete []styledData
;
1059 } else if (actionStep
.at
== removeAction
) {
1060 BasicDeleteChars(actionStep
.position
, actionStep
.lenData
*2);
1062 uh
.CompletedRedoStep();
1065 int CellBuffer::SetLineState(int line
, int state
) {
1066 int stateOld
= lineStates
[line
];
1067 lineStates
[line
] = state
;
1071 int CellBuffer::GetLineState(int line
) {
1072 return lineStates
[line
];
1075 int CellBuffer::GetMaxLineState() {
1076 return lineStates
.Length();
1079 int CellBuffer::SetLevel(int line
, int level
) {
1081 if ((line
>= 0) && (line
< lv
.lines
)) {
1085 prev
= lv
.levels
[line
];
1086 if (lv
.levels
[line
] != level
) {
1087 lv
.levels
[line
] = level
;
1093 int CellBuffer::GetLevel(int line
) {
1094 if (lv
.levels
&& (line
>= 0) && (line
< lv
.lines
)) {
1095 return lv
.levels
[line
];
1097 return SC_FOLDLEVELBASE
;
1101 void CellBuffer::ClearLevels() {