]>
git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/CellBuffer.cxx
1 // Scintilla source code edit control
2 // CellBuffer.cxx - manages a buffer of cells
3 // Copyright 1998-2000 by Neil Hodgson <neilh@scintilla.org>
4 // The License.txt file describes the conditions under which this software may be distributed.
13 #include "Scintilla.h"
15 #include "CellBuffer.h"
17 MarkerHandleSet::MarkerHandleSet() {
21 MarkerHandleSet::~MarkerHandleSet() {
22 MarkerHandleNumber
*mhn
= root
;
24 MarkerHandleNumber
*mhnToFree
= mhn
;
31 int MarkerHandleSet::Length() {
33 MarkerHandleNumber
*mhn
= root
;
41 int MarkerHandleSet::NumberFromHandle(int handle
) {
42 MarkerHandleNumber
*mhn
= root
;
44 if (mhn
->handle
== handle
) {
52 int MarkerHandleSet::MarkValue() {
54 MarkerHandleNumber
*mhn
= root
;
56 m
|= (1 << mhn
->number
);
62 bool MarkerHandleSet::Contains(int handle
) {
63 MarkerHandleNumber
*mhn
= root
;
65 if (mhn
->handle
== handle
) {
73 bool MarkerHandleSet::InsertHandle(int handle
, int markerNum
) {
74 MarkerHandleNumber
*mhn
= new MarkerHandleNumber
;
78 mhn
->number
= markerNum
;
84 void MarkerHandleSet::RemoveHandle(int handle
) {
85 MarkerHandleNumber
**pmhn
= &root
;
87 MarkerHandleNumber
*mhn
= *pmhn
;
88 if (mhn
->handle
== handle
) {
93 pmhn
= &((*pmhn
)->next
);
97 void MarkerHandleSet::RemoveNumber(int markerNum
) {
98 MarkerHandleNumber
**pmhn
= &root
;
100 MarkerHandleNumber
*mhn
= *pmhn
;
101 if (mhn
->number
== markerNum
) {
106 pmhn
= &((*pmhn
)->next
);
110 void MarkerHandleSet::CombineWith(MarkerHandleSet
*other
) {
111 MarkerHandleNumber
**pmhn
= &root
;
113 pmhn
= &((*pmhn
)->next
);
119 LineVector::LineVector() {
130 LineVector::~LineVector() {
131 for (int line
= 0; line
< lines
; line
++) {
132 delete linesData
[line
].handleSet
;
133 linesData
[line
].handleSet
= 0;
141 void LineVector::Init() {
142 for (int line
= 0; line
< lines
; line
++) {
143 delete linesData
[line
].handleSet
;
144 linesData
[line
].handleSet
= 0;
147 linesData
= new LineData
[static_cast<int>(growSize
)];
155 void LineVector::Expand(int sizeNew
) {
156 LineData
*linesDataNew
= new LineData
[sizeNew
];
158 for (int i
= 0; i
< size
; i
++)
159 linesDataNew
[i
] = linesData
[i
];
160 // Do not delete handleSets here as they are transferred to new linesData
162 linesData
= linesDataNew
;
165 Platform::DebugPrintf("No memory available\n");
171 void LineVector::ExpandLevels(int sizeNew
) {
174 int *levelsNew
= new int[sizeNew
];
177 for (; i
< sizeLevels
; i
++)
178 levelsNew
[i
] = levels
[i
];
179 for (; i
< sizeNew
; i
++)
180 levelsNew
[i
] = SC_FOLDLEVELBASE
;
183 sizeLevels
= sizeNew
;
185 Platform::DebugPrintf("No memory available\n");
191 void LineVector::ClearLevels() {
197 void LineVector::InsertValue(int pos
, int value
) {
198 //Platform::DebugPrintf("InsertValue[%d] = %d\n", pos, value);
199 if ((lines
+ 2) >= size
) {
200 Expand(size
+ growSize
);
202 ExpandLevels(size
+ growSize
);
206 for (int i
= lines
; i
> pos
; i
--) {
207 linesData
[i
] = linesData
[i
- 1];
209 linesData
[pos
].startPosition
= value
;
210 linesData
[pos
].handleSet
= 0;
212 for (int j
= lines
; j
> pos
; j
--) {
213 levels
[j
] = levels
[j
- 1];
216 levels
[pos
] = SC_FOLDLEVELBASE
;
217 } else if (pos
== (lines
- 1)) { // Last line will not be a folder
218 levels
[pos
] = SC_FOLDLEVELBASE
;
220 levels
[pos
] = levels
[pos
- 1];
225 void LineVector::SetValue(int pos
, int value
) {
226 //Platform::DebugPrintf("SetValue[%d] = %d\n", pos, value);
227 if ((pos
+ 2) >= size
) {
228 //Platform::DebugPrintf("Resize %d %d\n", size,pos);
229 Expand(pos
+ growSize
);
230 //Platform::DebugPrintf("end Resize %d %d\n", size,pos);
233 ExpandLevels(pos
+ growSize
);
236 linesData
[pos
].startPosition
= value
;
239 void LineVector::Remove(int pos
) {
240 //Platform::DebugPrintf("Remove %d\n", pos);
241 // Retain the markers from the deleted line by oring them into the previous line
243 MergeMarkers(pos
- 1);
245 for (int i
= pos
; i
< lines
; i
++) {
246 linesData
[i
] = linesData
[i
+ 1];
249 // Level information merges back onto previous line
250 int posAbove
= pos
- 1;
253 for (int j
= posAbove
; j
< lines
; j
++) {
254 levels
[j
] = levels
[j
+ 1];
260 int LineVector::LineFromPosition(int pos
) {
261 //Platform::DebugPrintf("LineFromPostion %d lines=%d end = %d\n", pos, lines, linesData[lines].startPosition);
264 //Platform::DebugPrintf("LineFromPosition %d\n", pos);
265 if (pos
>= linesData
[lines
].startPosition
)
270 int middle
= (upper
+ lower
+ 1) / 2; // Round high
271 if (pos
< linesData
[middle
].startPosition
) {
276 } while (lower
< upper
);
277 //Platform::DebugPrintf("LineFromPostion %d %d %d\n", pos, lower, linesData[lower].startPosition, linesData[lower > 1 ? lower - 1 : 0].startPosition);
281 int LineVector::AddMark(int line
, int markerNum
) {
283 if (!linesData
[line
].handleSet
) {
284 // Need new structure to hold marker handle
285 linesData
[line
].handleSet
= new MarkerHandleSet
;
286 if (!linesData
[line
].handleSet
)
289 linesData
[line
].handleSet
->InsertHandle(handleCurrent
, markerNum
);
291 return handleCurrent
;
294 void LineVector::MergeMarkers(int pos
) {
295 if (linesData
[pos
+ 1].handleSet
!= NULL
) {
296 if (linesData
[pos
].handleSet
== NULL
)
297 linesData
[pos
].handleSet
= new MarkerHandleSet
;
298 linesData
[pos
].handleSet
->CombineWith(linesData
[pos
+ 1].handleSet
);
299 delete linesData
[pos
+ 1].handleSet
;
300 linesData
[pos
+ 1].handleSet
= NULL
;
304 void LineVector::DeleteMark(int line
, int markerNum
) {
305 if (linesData
[line
].handleSet
) {
306 if (markerNum
== -1) {
307 delete linesData
[line
].handleSet
;
308 linesData
[line
].handleSet
= 0;
310 linesData
[line
].handleSet
->RemoveNumber(markerNum
);
311 if (linesData
[line
].handleSet
->Length() == 0) {
312 delete linesData
[line
].handleSet
;
313 linesData
[line
].handleSet
= 0;
319 void LineVector::DeleteMarkFromHandle(int markerHandle
) {
320 int line
= LineFromHandle(markerHandle
);
322 linesData
[line
].handleSet
->RemoveHandle(markerHandle
);
323 if (linesData
[line
].handleSet
->Length() == 0) {
324 delete linesData
[line
].handleSet
;
325 linesData
[line
].handleSet
= 0;
330 int LineVector::LineFromHandle(int markerHandle
) {
331 for (int line
= 0; line
< lines
; line
++) {
332 if (linesData
[line
].handleSet
) {
333 if (linesData
[line
].handleSet
->Contains(markerHandle
)) {
352 void Action::Create(actionType at_
, int position_
, char *data_
, int lenData_
, bool mayCoalesce_
) {
354 position
= position_
;
358 mayCoalesce
= mayCoalesce_
;
361 void Action::Destroy() {
366 void Action::Grab(Action
*source
) {
369 position
= source
->position
;
372 lenData
= source
->lenData
;
373 mayCoalesce
= source
->mayCoalesce
;
375 // Ownership of source data transferred to this
376 source
->position
= 0;
377 source
->at
= startAction
;
380 source
->mayCoalesce
= true;
383 // The undo history stores a sequence of user operations that represent the user's view of the
384 // commands executed on the text.
385 // Each user operation contains a sequence of text insertion and text deletion actions.
386 // All the user operations are stored in a list of individual actions with 'start' actions used
387 // as delimiters between user operations.
388 // Initially there is one start action in the history.
389 // As each action is performed, it is recorded in the history. The action may either become
390 // part of the current user operation or may start a new user operation. If it is to be part of the
391 // current operation, then it overwrites the current last action. If it is to be part of a new
392 // operation, it is appended after the current last action.
393 // After writing the new action, a new start action is appended at the end of the history.
394 // The decision of whether to start a new user operation is based upon two factors. If a
395 // compound operation has been explicitly started by calling BeginUndoAction and no matching
396 // EndUndoAction (these calls nest) has been called, then the action is coalesced into the current
397 // operation. If there is no outstanding BeginUndoAction call then a new operation is started
398 // unless it looks as if the new action is caused by the user typing or deleting a stream of text.
399 // Sequences that look like typing or deletion are coalesced into a single user operation.
401 UndoHistory::UndoHistory() {
404 actions
= new Action
[lenActions
];
407 undoSequenceDepth
= 0;
410 actions
[currentAction
].Create(startAction
);
413 UndoHistory::~UndoHistory() {
418 void UndoHistory::EnsureUndoRoom() {
419 //Platform::DebugPrintf("%% %d action %d %d %d\n", at, position, length, currentAction);
420 if (currentAction
>= 2) {
421 // Have to test that there is room for 2 more actions in the array
422 // as two actions may be created by this 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
;
438 void UndoHistory::AppendAction(actionType at
, int position
, char *data
, int lengthData
) {
440 //Platform::DebugPrintf("%% %d action %d %d %d\n", at, position, lengthData, currentAction);
441 //Platform::DebugPrintf("^ %d action %d %d\n", actions[currentAction - 1].at,
442 // actions[currentAction - 1].position, actions[currentAction - 1].lenData);
443 if (currentAction
>= 1) {
444 if (0 == undoSequenceDepth
) {
445 // Top level actions may not always be coalesced
446 Action
&actPrevious
= actions
[currentAction
- 1];
447 // See if current action can be coalesced into previous action
448 // Will work if both are inserts or deletes and position is same
449 if (at
!= actPrevious
.at
) {
451 } else if (currentAction
== savePoint
) {
453 } else if ((at
== removeAction
) &&
454 ((position
+ lengthData
* 2) != actPrevious
.position
)) {
455 // Removals must be at same position to coalesce
457 } else if ((at
== insertAction
) &&
458 (position
!= (actPrevious
.position
+ actPrevious
.lenData
*2))) {
459 // Insertions must be immediately after to coalesce
462 //Platform::DebugPrintf("action coalesced\n");
466 // Actions not at top level are always coalesced unless this is after return to top level
467 if (!actions
[currentAction
].mayCoalesce
)
473 actions
[currentAction
].Create(at
, position
, data
, lengthData
);
475 actions
[currentAction
].Create(startAction
);
476 maxAction
= currentAction
;
479 void UndoHistory::BeginUndoAction() {
481 if (undoSequenceDepth
== 0) {
482 if (actions
[currentAction
].at
!= startAction
) {
484 actions
[currentAction
].Create(startAction
);
485 maxAction
= currentAction
;
487 actions
[currentAction
].mayCoalesce
= false;
492 void UndoHistory::EndUndoAction() {
495 if (0 == undoSequenceDepth
) {
496 if (actions
[currentAction
].at
!= startAction
) {
498 actions
[currentAction
].Create(startAction
);
499 maxAction
= currentAction
;
501 actions
[currentAction
].mayCoalesce
= false;
505 void UndoHistory::DropUndoSequence() {
506 undoSequenceDepth
= 0;
509 void UndoHistory::DeleteUndoHistory() {
510 for (int i
= 1; i
< maxAction
; i
++)
511 actions
[i
].Destroy();
514 actions
[currentAction
].Create(startAction
);
518 void UndoHistory::SetSavePoint() {
519 savePoint
= currentAction
;
522 bool UndoHistory::IsSavePoint() const {
523 return savePoint
== currentAction
;
526 bool UndoHistory::CanUndo() const {
527 return (currentAction
> 0) && (maxAction
> 0);
530 int UndoHistory::StartUndo() {
531 // Drop any trailing startAction
532 if (actions
[currentAction
].at
== startAction
&& currentAction
> 0)
535 // Count the steps in this action
536 int act
= currentAction
;
537 while (actions
[act
].at
!= startAction
&& act
> 0) {
540 return currentAction
- act
;
543 const Action
&UndoHistory::GetUndoStep() const {
544 return actions
[currentAction
];
547 void UndoHistory::CompletedUndoStep() {
551 bool UndoHistory::CanRedo() const {
552 return maxAction
> currentAction
;
555 int UndoHistory::StartRedo() {
556 // Drop any leading startAction
557 if (actions
[currentAction
].at
== startAction
&& currentAction
< maxAction
)
560 // Count the steps in this action
561 int act
= currentAction
;
562 while (actions
[act
].at
!= startAction
&& act
< maxAction
) {
565 return act
- currentAction
;
568 const Action
&UndoHistory::GetRedoStep() const {
569 return actions
[currentAction
];
572 void UndoHistory::CompletedRedoStep() {
576 CellBuffer::CellBuffer(int initialLength
) {
577 body
= new char[initialLength
];
578 size
= initialLength
;
581 gaplen
= initialLength
;
582 part2body
= body
+ gaplen
;
584 collectingUndo
= true;
587 CellBuffer::~CellBuffer() {
592 void CellBuffer::GapTo(int position
) {
593 if (position
== part1len
)
595 if (position
< part1len
) {
596 int diff
= part1len
- position
;
597 //Platform::DebugPrintf("Move gap backwards to %d diff = %d part1len=%d length=%d \n", position,diff, part1len, length);
598 for (int i
= 0; i
< diff
; i
++)
599 body
[part1len
+ gaplen
- i
- 1] = body
[part1len
- i
- 1];
600 } else { // position > part1len
601 int diff
= position
- part1len
;
602 //Platform::DebugPrintf("Move gap forwards to %d diff =%d\n", position,diff);
603 for (int i
= 0; i
< diff
; i
++)
604 body
[part1len
+ i
] = body
[part1len
+ gaplen
+ i
];
607 part2body
= body
+ gaplen
;
610 void CellBuffer::RoomFor(int insertionLength
) {
611 //Platform::DebugPrintf("need room %d %d\n", gaplen, insertionLength);
612 if (gaplen
<= insertionLength
) {
613 //Platform::DebugPrintf("need room %d %d\n", gaplen, insertionLength);
615 int newSize
= size
+ insertionLength
+ 4000;
616 //Platform::DebugPrintf("moved gap %d\n", newSize);
617 char *newBody
= new char[newSize
];
618 memcpy(newBody
, body
, size
);
621 gaplen
+= newSize
- size
;
622 part2body
= body
+ gaplen
;
624 //Platform::DebugPrintf("end need room %d %d - size=%d length=%d\n", gaplen, insertionLength,size,length);
629 // To make it easier to write code that uses ByteAt, a position outside the range of the buffer
630 // can be retrieved. All characters outside the range have the value '\0'.
631 char CellBuffer::ByteAt(int position
) {
632 if (position
< part1len
) {
636 return body
[position
];
639 if (position
>= length
) {
642 return part2body
[position
];
647 void CellBuffer::SetByteAt(int position
, char ch
) {
650 //Platform::DebugPrintf("Bad position %d\n",position);
653 if (position
>= length
+ 11) {
654 Platform::DebugPrintf("Very Bad position %d of %d\n", position
, length
);
658 if (position
>= length
) {
659 //Platform::DebugPrintf("Bad position %d of %d\n",position,length);
663 if (position
< part1len
) {
666 part2body
[position
] = ch
;
670 char CellBuffer::CharAt(int position
) {
671 return ByteAt(position
*2);
674 void CellBuffer::GetCharRange(char *buffer
, int position
, int lengthRetrieve
) {
675 if (lengthRetrieve
< 0)
679 int bytePos
= position
* 2;
680 if ((bytePos
+ lengthRetrieve
* 2) > length
) {
681 Platform::DebugPrintf("Bad GetCharRange %d for %d of %d\n", bytePos
,
682 lengthRetrieve
, length
);
685 GapTo(0); // Move the buffer so its easy to subscript into it
686 char *pb
= part2body
+ bytePos
;
687 while (lengthRetrieve
--) {
693 char CellBuffer::StyleAt(int position
) {
694 return ByteAt(position
*2 + 1);
697 const char *CellBuffer::InsertString(int position
, char *s
, int insertLength
) {
699 // InsertString and DeleteChars are the bottleneck though which all changes occur
701 if (collectingUndo
) {
702 // Save into the undo/redo stack, but only the characters - not the formatting
703 // This takes up about half load time
704 data
= new char[insertLength
/ 2];
705 for (int i
= 0; i
< insertLength
/ 2; i
++) {
708 uh
.AppendAction(insertAction
, position
, data
, insertLength
/ 2);
711 BasicInsertString(position
, s
, insertLength
);
716 void CellBuffer::InsertCharStyle(int position
, char ch
, char style
) {
720 InsertString(position
*2, s
, 2);
723 bool CellBuffer::SetStyleAt(int position
, char style
, char mask
) {
724 char curVal
= ByteAt(position
* 2 + 1);
725 if ((curVal
& mask
) != style
) {
726 SetByteAt(position
*2 + 1, static_cast<char>((curVal
& ~mask
) | style
));
733 bool CellBuffer::SetStyleFor(int position
, int lengthStyle
, char style
, char mask
) {
734 int bytePos
= position
* 2 + 1;
735 bool changed
= false;
736 while (lengthStyle
--) {
737 char curVal
= ByteAt(bytePos
);
738 if ((curVal
& mask
) != style
) {
739 SetByteAt(bytePos
, static_cast<char>((curVal
& ~mask
) | style
));
747 const char *CellBuffer::DeleteChars(int position
, int deleteLength
) {
748 // InsertString and DeleteChars are the bottleneck though which all changes occur
751 if (collectingUndo
) {
752 // Save into the undo/redo stack, but only the characters - not the formatting
753 data
= new char[deleteLength
/ 2];
754 for (int i
= 0; i
< deleteLength
/ 2; i
++) {
755 data
[i
] = ByteAt(position
+ i
* 2);
757 uh
.AppendAction(removeAction
, position
, data
, deleteLength
/ 2);
760 BasicDeleteChars(position
, deleteLength
);
765 int CellBuffer::ByteLength() {
769 int CellBuffer::Length() {
770 return ByteLength() / 2;
773 int CellBuffer::Lines() {
774 //Platform::DebugPrintf("Lines = %d\n", lv.lines);
778 int CellBuffer::LineStart(int line
) {
781 else if (line
> lv
.lines
)
784 return lv
.linesData
[line
].startPosition
;
787 bool CellBuffer::IsReadOnly() {
791 void CellBuffer::SetReadOnly(bool set
) {
795 void CellBuffer::SetSavePoint() {
799 bool CellBuffer::IsSavePoint() {
800 return uh
.IsSavePoint();
803 int CellBuffer::AddMark(int line
, int markerNum
) {
804 if ((line
>= 0) && (line
< lv
.lines
)) {
805 return lv
.AddMark(line
, markerNum
);
810 void CellBuffer::DeleteMark(int line
, int markerNum
) {
811 if ((line
>= 0) && (line
< lv
.lines
)) {
812 lv
.DeleteMark(line
, markerNum
);
816 void CellBuffer::DeleteMarkFromHandle(int markerHandle
) {
817 lv
.DeleteMarkFromHandle(markerHandle
);
820 int CellBuffer::GetMark(int line
) {
821 if ((line
>= 0) && (line
< lv
.lines
) && (lv
.linesData
[line
].handleSet
))
822 return lv
.linesData
[line
].handleSet
->MarkValue();
826 void CellBuffer::DeleteAllMarks(int markerNum
) {
827 for (int line
= 0; line
< lv
.lines
; line
++) {
828 lv
.DeleteMark(line
, markerNum
);
832 int CellBuffer::LineFromHandle(int markerHandle
) {
833 return lv
.LineFromHandle(markerHandle
);
838 void CellBuffer::BasicInsertString(int position
, char *s
, int insertLength
) {
839 //Platform::DebugPrintf("Inserting at %d for %d\n", position, insertLength);
840 if (insertLength
== 0)
842 RoomFor(insertLength
);
845 memcpy(body
+ part1len
, s
, insertLength
);
846 length
+= insertLength
;
847 part1len
+= insertLength
;
848 gaplen
-= insertLength
;
849 part2body
= body
+ gaplen
;
851 int lineInsert
= lv
.LineFromPosition(position
/ 2) + 1;
852 // Point all the lines after the insertion point further along in the buffer
853 for (int lineAfter
= lineInsert
; lineAfter
<= lv
.lines
; lineAfter
++) {
854 lv
.linesData
[lineAfter
].startPosition
+= insertLength
/ 2;
857 if ((position
- 2) >= 0)
858 chPrev
= ByteAt(position
- 2);
860 if ((position
+ insertLength
) < length
)
861 chAfter
= ByteAt(position
+ insertLength
);
862 if (chPrev
== '\r' && chAfter
== '\n') {
863 //Platform::DebugPrintf("Splitting a crlf pair at %d\n", lineInsert);
864 // Splitting up a crlf pair at position
865 lv
.InsertValue(lineInsert
, position
/ 2);
869 for (int i
= 0; i
< insertLength
; i
+= 2) {
872 //Platform::DebugPrintf("Inserting cr at %d\n", lineInsert);
873 lv
.InsertValue(lineInsert
, (position
+ i
) / 2 + 1);
875 } else if (ch
== '\n') {
876 if (chPrev
== '\r') {
877 //Platform::DebugPrintf("Patching cr before lf at %d\n", lineInsert-1);
878 // Patch up what was end of line
879 lv
.SetValue(lineInsert
- 1, (position
+ i
) / 2 + 1);
881 //Platform::DebugPrintf("Inserting lf at %d\n", lineInsert);
882 lv
.InsertValue(lineInsert
, (position
+ i
) / 2 + 1);
888 // Joining two lines where last insertion is cr and following text starts with lf
889 if (chAfter
== '\n') {
891 //Platform::DebugPrintf("Joining cr before lf at %d\n", lineInsert-1);
892 // End of line already in buffer so drop the newly created one
893 lv
.Remove(lineInsert
- 1);
898 void CellBuffer::BasicDeleteChars(int position
, int deleteLength
) {
899 //Platform::DebugPrintf("Deleting at %d for %d\n", position, deleteLength);
900 if (deleteLength
== 0)
903 if ((position
== 0) && (deleteLength
== length
)) {
904 // If whole buffer is being deleted, faster to reinitialise lines data
905 // than to delete each line.
906 //printf("Whole buffer being deleted\n");
909 // Have to fix up line positions before doing deletion as looking at text in buffer
910 // to work out which lines have been removed
912 int lineRemove
= lv
.LineFromPosition(position
/ 2) + 1;
913 // Point all the lines after the insertion point further along in the buffer
914 for (int lineAfter
= lineRemove
; lineAfter
<= lv
.lines
; lineAfter
++) {
915 lv
.linesData
[lineAfter
].startPosition
-= deleteLength
/ 2;
919 chPrev
= ByteAt(position
- 2);
920 char chBefore
= chPrev
;
922 if (position
< length
)
923 chNext
= ByteAt(position
);
924 bool ignoreNL
= false;
925 if (chPrev
== '\r' && chNext
== '\n') {
926 //Platform::DebugPrintf("Deleting lf after cr, move line end to cr at %d\n", lineRemove);
928 lv
.SetValue(lineRemove
, position
/ 2);
930 ignoreNL
= true; // First \n is not real deletion
935 for (int i
= 0; i
< deleteLength
; i
+= 2) {
937 if ((position
+ i
+ 2) < length
)
938 chNext
= ByteAt(position
+ i
+ 2);
939 //Platform::DebugPrintf("Deleting %d %x\n", i, ch);
941 if (chNext
!= '\n') {
942 //Platform::DebugPrintf("Removing cr end of line\n");
943 lv
.Remove(lineRemove
);
945 } else if ((ch
== '\n') && !ignoreNL
) {
946 //Platform::DebugPrintf("Removing lf end of line\n");
947 lv
.Remove(lineRemove
);
948 ignoreNL
= false; // Further \n are not real deletions
954 // May have to fix up end if last deletion causes cr to be next to lf
955 // or removes one of a crlf pair
957 if ((position
+ deleteLength
) < length
)
958 chAfter
= ByteAt(position
+ deleteLength
);
959 if (chBefore
== '\r' && chAfter
== '\n') {
960 //d.printf("Joining cr before lf at %d\n", lineRemove);
961 // Using lineRemove-1 as cr ended line before start of deletion
962 lv
.Remove(lineRemove
- 1);
963 lv
.SetValue(lineRemove
- 1, position
/ 2 + 1);
967 length
-= deleteLength
;
968 gaplen
+= deleteLength
;
969 part2body
= body
+ gaplen
;
972 bool CellBuffer::SetUndoCollection(bool collectUndo
) {
973 collectingUndo
= collectUndo
;
974 uh
.DropUndoSequence();
975 return collectingUndo
;
978 bool CellBuffer::IsCollectingUndo() {
979 return collectingUndo
;
982 void CellBuffer::BeginUndoAction() {
983 uh
.BeginUndoAction();
986 void CellBuffer::EndUndoAction() {
990 void CellBuffer::DeleteUndoHistory() {
991 uh
.DeleteUndoHistory();
994 bool CellBuffer::CanUndo() {
995 return (!readOnly
) && (uh
.CanUndo());
998 int CellBuffer::StartUndo() {
999 return uh
.StartUndo();
1002 const Action
&CellBuffer::GetUndoStep() const {
1003 return uh
.GetUndoStep();
1006 void CellBuffer::PerformUndoStep() {
1007 const Action
&actionStep
= uh
.GetUndoStep();
1008 if (actionStep
.at
== insertAction
) {
1009 BasicDeleteChars(actionStep
.position
, actionStep
.lenData
*2);
1010 } else if (actionStep
.at
== removeAction
) {
1011 char *styledData
= new char[actionStep
.lenData
* 2];
1012 for (int i
= 0; i
< actionStep
.lenData
; i
++) {
1013 styledData
[i
*2] = actionStep
.data
[i
];
1014 styledData
[i
*2 + 1] = 0;
1016 BasicInsertString(actionStep
.position
, styledData
, actionStep
.lenData
*2);
1017 delete []styledData
;
1019 uh
.CompletedUndoStep();
1022 bool CellBuffer::CanRedo() {
1023 return (!readOnly
) && (uh
.CanRedo());
1026 int CellBuffer::StartRedo() {
1027 return uh
.StartRedo();
1030 const Action
&CellBuffer::GetRedoStep() const {
1031 return uh
.GetRedoStep();
1034 void CellBuffer::PerformRedoStep() {
1035 const Action
&actionStep
= uh
.GetRedoStep();
1036 if (actionStep
.at
== insertAction
) {
1037 char *styledData
= new char[actionStep
.lenData
* 2];
1038 for (int i
= 0; i
< actionStep
.lenData
; i
++) {
1039 styledData
[i
*2] = actionStep
.data
[i
];
1040 styledData
[i
*2 + 1] = 0;
1042 BasicInsertString(actionStep
.position
, styledData
, actionStep
.lenData
*2);
1043 delete []styledData
;
1044 } else if (actionStep
.at
== removeAction
) {
1045 BasicDeleteChars(actionStep
.position
, actionStep
.lenData
*2);
1047 uh
.CompletedRedoStep();
1050 int CellBuffer::SetLineState(int line
, int state
) {
1051 int stateOld
= lineStates
[line
];
1052 lineStates
[line
] = state
;
1056 int CellBuffer::GetLineState(int line
) {
1057 return lineStates
[line
];
1060 int CellBuffer::GetMaxLineState() {
1061 return lineStates
.Length();
1064 int CellBuffer::SetLevel(int line
, int level
) {
1066 if ((line
>= 0) && (line
< lv
.lines
)) {
1070 prev
= lv
.levels
[line
];
1071 if (lv
.levels
[line
] != level
) {
1072 lv
.levels
[line
] = level
;
1078 int CellBuffer::GetLevel(int line
) {
1079 if (lv
.levels
&& (line
>= 0) && (line
< lv
.lines
)) {
1080 return lv
.levels
[line
];
1082 return SC_FOLDLEVELBASE
;
1086 void CellBuffer::ClearLevels() {