]>
git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/CellBuffer.cxx
6dae67507527f33fa135e9042e3993ccfba24ff5
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
) {
107 pmhn
= &((*pmhn
)->next
);
112 void MarkerHandleSet::CombineWith(MarkerHandleSet
*other
) {
113 MarkerHandleNumber
**pmhn
= &root
;
115 pmhn
= &((*pmhn
)->next
);
121 LineVector::LineVector() {
133 LineVector::~LineVector() {
134 for (int line
= 0; line
< lines
; line
++) {
135 delete linesData
[line
].handleSet
;
136 linesData
[line
].handleSet
= 0;
144 void LineVector::Init() {
145 for (int line
= 0; line
< lines
; line
++) {
146 delete linesData
[line
].handleSet
;
147 linesData
[line
].handleSet
= 0;
150 linesData
= new LineData
[static_cast<int>(growSize
)];
158 void LineVector::Expand(int sizeNew
) {
159 LineData
*linesDataNew
= new LineData
[sizeNew
];
161 for (int i
= 0; i
< size
; i
++)
162 linesDataNew
[i
] = linesData
[i
];
163 // Do not delete handleSets here as they are transferred to new linesData
165 linesData
= linesDataNew
;
168 Platform::DebugPrintf("No memory available\n");
174 void LineVector::ExpandLevels(int sizeNew
) {
177 int *levelsNew
= new int[sizeNew
];
180 for (; i
< sizeLevels
; i
++)
181 levelsNew
[i
] = levels
[i
];
182 for (; i
< sizeNew
; i
++)
183 levelsNew
[i
] = SC_FOLDLEVELBASE
;
186 sizeLevels
= sizeNew
;
188 Platform::DebugPrintf("No memory available\n");
194 void LineVector::ClearLevels() {
200 void LineVector::InsertValue(int pos
, int value
) {
201 //Platform::DebugPrintf("InsertValue[%d] = %d\n", pos, value);
202 if ((lines
+ 2) >= size
) {
203 if (growSize
* 6 < size
)
205 Expand(size
+ growSize
);
207 ExpandLevels(size
+ growSize
);
211 for (int i
= lines
; i
> pos
; i
--) {
212 linesData
[i
] = linesData
[i
- 1];
214 linesData
[pos
].startPosition
= value
;
215 linesData
[pos
].handleSet
= 0;
217 for (int j
= lines
; j
> pos
; j
--) {
218 levels
[j
] = levels
[j
- 1];
221 levels
[pos
] = SC_FOLDLEVELBASE
;
222 } else if (pos
== (lines
- 1)) { // Last line will not be a folder
223 levels
[pos
] = SC_FOLDLEVELBASE
;
225 levels
[pos
] = levels
[pos
- 1];
230 void LineVector::SetValue(int pos
, int value
) {
231 //Platform::DebugPrintf("SetValue[%d] = %d\n", pos, value);
232 if ((pos
+ 2) >= size
) {
233 //Platform::DebugPrintf("Resize %d %d\n", size,pos);
234 Expand(pos
+ growSize
);
235 //Platform::DebugPrintf("end Resize %d %d\n", size,pos);
238 ExpandLevels(pos
+ growSize
);
241 linesData
[pos
].startPosition
= value
;
244 void LineVector::Remove(int pos
) {
245 //Platform::DebugPrintf("Remove %d\n", pos);
246 // Retain the markers from the deleted line by oring them into the previous line
248 MergeMarkers(pos
- 1);
250 for (int i
= pos
; i
< lines
; i
++) {
251 linesData
[i
] = linesData
[i
+ 1];
254 // Level information merges back onto previous line
255 int posAbove
= pos
- 1;
258 for (int j
= posAbove
; j
< lines
; j
++) {
259 levels
[j
] = levels
[j
+ 1];
265 int LineVector::LineFromPosition(int pos
) {
266 //Platform::DebugPrintf("LineFromPostion %d lines=%d end = %d\n", pos, lines, linesData[lines].startPosition);
269 //Platform::DebugPrintf("LineFromPosition %d\n", pos);
270 if (pos
>= linesData
[lines
].startPosition
)
275 int middle
= (upper
+ lower
+ 1) / 2; // Round high
276 if (pos
< linesData
[middle
].startPosition
) {
281 } while (lower
< upper
);
282 //Platform::DebugPrintf("LineFromPostion %d %d %d\n", pos, lower, linesData[lower].startPosition, linesData[lower > 1 ? lower - 1 : 0].startPosition);
286 int LineVector::AddMark(int line
, int markerNum
) {
288 if (!linesData
[line
].handleSet
) {
289 // Need new structure to hold marker handle
290 linesData
[line
].handleSet
= new MarkerHandleSet
;
291 if (!linesData
[line
].handleSet
)
294 linesData
[line
].handleSet
->InsertHandle(handleCurrent
, markerNum
);
296 return handleCurrent
;
299 void LineVector::MergeMarkers(int pos
) {
300 if (linesData
[pos
+ 1].handleSet
!= NULL
) {
301 if (linesData
[pos
].handleSet
== NULL
)
302 linesData
[pos
].handleSet
= new MarkerHandleSet
;
303 linesData
[pos
].handleSet
->CombineWith(linesData
[pos
+ 1].handleSet
);
304 delete linesData
[pos
+ 1].handleSet
;
305 linesData
[pos
+ 1].handleSet
= NULL
;
309 void LineVector::DeleteMark(int line
, int markerNum
) {
310 if (linesData
[line
].handleSet
) {
311 if (markerNum
== -1) {
312 delete linesData
[line
].handleSet
;
313 linesData
[line
].handleSet
= 0;
315 linesData
[line
].handleSet
->RemoveNumber(markerNum
);
316 if (linesData
[line
].handleSet
->Length() == 0) {
317 delete linesData
[line
].handleSet
;
318 linesData
[line
].handleSet
= 0;
324 void LineVector::DeleteMarkFromHandle(int markerHandle
) {
325 int line
= LineFromHandle(markerHandle
);
327 linesData
[line
].handleSet
->RemoveHandle(markerHandle
);
328 if (linesData
[line
].handleSet
->Length() == 0) {
329 delete linesData
[line
].handleSet
;
330 linesData
[line
].handleSet
= 0;
335 int LineVector::LineFromHandle(int markerHandle
) {
336 for (int line
= 0; line
< lines
; line
++) {
337 if (linesData
[line
].handleSet
) {
338 if (linesData
[line
].handleSet
->Contains(markerHandle
)) {
357 void Action::Create(actionType at_
, int position_
, char *data_
, int lenData_
, bool mayCoalesce_
) {
359 position
= position_
;
363 mayCoalesce
= mayCoalesce_
;
366 void Action::Destroy() {
371 void Action::Grab(Action
*source
) {
374 position
= source
->position
;
377 lenData
= source
->lenData
;
378 mayCoalesce
= source
->mayCoalesce
;
380 // Ownership of source data transferred to this
381 source
->position
= 0;
382 source
->at
= startAction
;
385 source
->mayCoalesce
= true;
388 // The undo history stores a sequence of user operations that represent the user's view of the
389 // commands executed on the text.
390 // Each user operation contains a sequence of text insertion and text deletion actions.
391 // All the user operations are stored in a list of individual actions with 'start' actions used
392 // as delimiters between user operations.
393 // Initially there is one start action in the history.
394 // As each action is performed, it is recorded in the history. The action may either become
395 // part of the current user operation or may start a new user operation. If it is to be part of the
396 // current operation, then it overwrites the current last action. If it is to be part of a new
397 // operation, it is appended after the current last action.
398 // After writing the new action, a new start action is appended at the end of the history.
399 // The decision of whether to start a new user operation is based upon two factors. If a
400 // compound operation has been explicitly started by calling BeginUndoAction and no matching
401 // EndUndoAction (these calls nest) has been called, then the action is coalesced into the current
402 // operation. If there is no outstanding BeginUndoAction call then a new operation is started
403 // unless it looks as if the new action is caused by the user typing or deleting a stream of text.
404 // Sequences that look like typing or deletion are coalesced into a single user operation.
406 UndoHistory::UndoHistory() {
409 actions
= new Action
[lenActions
];
412 undoSequenceDepth
= 0;
415 actions
[currentAction
].Create(startAction
);
418 UndoHistory::~UndoHistory() {
423 void UndoHistory::EnsureUndoRoom() {
424 // Have to test that there is room for 2 more actions in the array
425 // as two actions may be created by the calling function
426 if (currentAction
>= (lenActions
- 2)) {
427 // Run out of undo nodes so extend the array
428 int lenActionsNew
= lenActions
* 2;
429 Action
*actionsNew
= new Action
[lenActionsNew
];
432 for (int act
= 0; act
<= currentAction
; act
++)
433 actionsNew
[act
].Grab(&actions
[act
]);
435 lenActions
= lenActionsNew
;
436 actions
= actionsNew
;
440 void UndoHistory::AppendAction(actionType at
, int position
, char *data
, int lengthData
) {
442 //Platform::DebugPrintf("%% %d action %d %d %d\n", at, position, lengthData, currentAction);
443 //Platform::DebugPrintf("^ %d action %d %d\n", actions[currentAction - 1].at,
444 // actions[currentAction - 1].position, actions[currentAction - 1].lenData);
445 if (currentAction
>= 1) {
446 if (0 == undoSequenceDepth
) {
447 // Top level actions may not always be coalesced
448 Action
&actPrevious
= actions
[currentAction
- 1];
449 // See if current action can be coalesced into previous action
450 // Will work if both are inserts or deletes and position is same
451 if (at
!= actPrevious
.at
) {
453 } else if (currentAction
== savePoint
) {
455 } else if ((at
== insertAction
) &&
456 (position
!= (actPrevious
.position
+ actPrevious
.lenData
*2))) {
457 // Insertions must be immediately after to coalesce
459 } else if (!actions
[currentAction
].mayCoalesce
) {
460 // Not allowed to coalesce if this set
462 } else if (at
== removeAction
) {
463 if ((lengthData
== 1) || (lengthData
== 2)){
464 if ((position
+ lengthData
* 2) == actPrevious
.position
) {
466 } else if (position
== actPrevious
.position
) {
469 // Removals must be at same position to coalesce
473 // Removals must be of one character to coalesce
477 //Platform::DebugPrintf("action coalesced\n");
481 // Actions not at top level are always coalesced unless this is after return to top level
482 if (!actions
[currentAction
].mayCoalesce
)
488 actions
[currentAction
].Create(at
, position
, data
, lengthData
);
490 actions
[currentAction
].Create(startAction
);
491 maxAction
= currentAction
;
494 void UndoHistory::BeginUndoAction() {
496 if (undoSequenceDepth
== 0) {
497 if (actions
[currentAction
].at
!= startAction
) {
499 actions
[currentAction
].Create(startAction
);
500 maxAction
= currentAction
;
502 actions
[currentAction
].mayCoalesce
= false;
507 void UndoHistory::EndUndoAction() {
510 if (0 == undoSequenceDepth
) {
511 if (actions
[currentAction
].at
!= startAction
) {
513 actions
[currentAction
].Create(startAction
);
514 maxAction
= currentAction
;
516 actions
[currentAction
].mayCoalesce
= false;
520 void UndoHistory::DropUndoSequence() {
521 undoSequenceDepth
= 0;
524 void UndoHistory::DeleteUndoHistory() {
525 for (int i
= 1; i
< maxAction
; i
++)
526 actions
[i
].Destroy();
529 actions
[currentAction
].Create(startAction
);
533 void UndoHistory::SetSavePoint() {
534 savePoint
= currentAction
;
537 bool UndoHistory::IsSavePoint() const {
538 return savePoint
== currentAction
;
541 bool UndoHistory::CanUndo() const {
542 return (currentAction
> 0) && (maxAction
> 0);
545 int UndoHistory::StartUndo() {
546 // Drop any trailing startAction
547 if (actions
[currentAction
].at
== startAction
&& currentAction
> 0)
550 // Count the steps in this action
551 int act
= currentAction
;
552 while (actions
[act
].at
!= startAction
&& act
> 0) {
555 return currentAction
- act
;
558 const Action
&UndoHistory::GetUndoStep() const {
559 return actions
[currentAction
];
562 void UndoHistory::CompletedUndoStep() {
566 bool UndoHistory::CanRedo() const {
567 return maxAction
> currentAction
;
570 int UndoHistory::StartRedo() {
571 // Drop any leading startAction
572 if (actions
[currentAction
].at
== startAction
&& currentAction
< maxAction
)
575 // Count the steps in this action
576 int act
= currentAction
;
577 while (actions
[act
].at
!= startAction
&& act
< maxAction
) {
580 return act
- currentAction
;
583 const Action
&UndoHistory::GetRedoStep() const {
584 return actions
[currentAction
];
587 void UndoHistory::CompletedRedoStep() {
591 CellBuffer::CellBuffer(int initialLength
) {
592 body
= new char[initialLength
];
593 size
= initialLength
;
596 gaplen
= initialLength
;
597 part2body
= body
+ gaplen
;
599 collectingUndo
= true;
603 CellBuffer::~CellBuffer() {
608 void CellBuffer::GapTo(int position
) {
609 if (position
== part1len
)
611 if (position
< part1len
) {
612 int diff
= part1len
- position
;
613 //Platform::DebugPrintf("Move gap backwards to %d diff = %d part1len=%d length=%d \n", position,diff, part1len, length);
614 for (int i
= 0; i
< diff
; i
++)
615 body
[part1len
+ gaplen
- i
- 1] = body
[part1len
- i
- 1];
616 } else { // position > part1len
617 int diff
= position
- part1len
;
618 //Platform::DebugPrintf("Move gap forwards to %d diff =%d\n", position,diff);
619 for (int i
= 0; i
< diff
; i
++)
620 body
[part1len
+ i
] = body
[part1len
+ gaplen
+ i
];
623 part2body
= body
+ gaplen
;
626 void CellBuffer::RoomFor(int insertionLength
) {
627 //Platform::DebugPrintf("need room %d %d\n", gaplen, insertionLength);
628 if (gaplen
<= insertionLength
) {
629 //Platform::DebugPrintf("need room %d %d\n", gaplen, insertionLength);
631 if (growSize
* 6 < size
)
633 int newSize
= size
+ insertionLength
+ growSize
;
634 //Platform::DebugPrintf("moved gap %d\n", newSize);
635 char *newBody
= new char[newSize
];
636 memcpy(newBody
, body
, size
);
639 gaplen
+= newSize
- size
;
640 part2body
= body
+ gaplen
;
642 //Platform::DebugPrintf("end need room %d %d - size=%d length=%d\n", gaplen, insertionLength,size,length);
647 // To make it easier to write code that uses ByteAt, a position outside the range of the buffer
648 // can be retrieved. All characters outside the range have the value '\0'.
649 char CellBuffer::ByteAt(int position
) {
650 if (position
< part1len
) {
654 return body
[position
];
657 if (position
>= length
) {
660 return part2body
[position
];
665 void CellBuffer::SetByteAt(int position
, char ch
) {
668 //Platform::DebugPrintf("Bad position %d\n",position);
671 if (position
>= length
+ 11) {
672 Platform::DebugPrintf("Very Bad position %d of %d\n", position
, length
);
676 if (position
>= length
) {
677 //Platform::DebugPrintf("Bad position %d of %d\n",position,length);
681 if (position
< part1len
) {
684 part2body
[position
] = ch
;
688 char CellBuffer::CharAt(int position
) {
689 return ByteAt(position
*2);
692 void CellBuffer::GetCharRange(char *buffer
, int position
, int lengthRetrieve
) {
693 if (lengthRetrieve
< 0)
697 int bytePos
= position
* 2;
698 if ((bytePos
+ lengthRetrieve
* 2) > length
) {
699 Platform::DebugPrintf("Bad GetCharRange %d for %d of %d\n", bytePos
,
700 lengthRetrieve
, length
);
703 GapTo(0); // Move the buffer so its easy to subscript into it
704 char *pb
= part2body
+ bytePos
;
705 while (lengthRetrieve
--) {
711 char CellBuffer::StyleAt(int position
) {
712 return ByteAt(position
*2 + 1);
715 const char *CellBuffer::InsertString(int position
, char *s
, int insertLength
) {
717 // InsertString and DeleteChars are the bottleneck though which all changes occur
719 if (collectingUndo
) {
720 // Save into the undo/redo stack, but only the characters - not the formatting
721 // This takes up about half load time
722 data
= new char[insertLength
/ 2];
723 for (int i
= 0; i
< insertLength
/ 2; i
++) {
726 uh
.AppendAction(insertAction
, position
, data
, insertLength
/ 2);
729 BasicInsertString(position
, s
, insertLength
);
734 void CellBuffer::InsertCharStyle(int position
, char ch
, char style
) {
738 InsertString(position
*2, s
, 2);
741 bool CellBuffer::SetStyleAt(int position
, char style
, char mask
) {
743 char curVal
= ByteAt(position
* 2 + 1);
744 if ((curVal
& mask
) != style
) {
745 SetByteAt(position
*2 + 1, static_cast<char>((curVal
& ~mask
) | style
));
752 bool CellBuffer::SetStyleFor(int position
, int lengthStyle
, char style
, char mask
) {
753 int bytePos
= position
* 2 + 1;
754 bool changed
= false;
755 PLATFORM_ASSERT(lengthStyle
== 0 ||
756 (lengthStyle
> 0 && lengthStyle
+ position
< length
));
757 while (lengthStyle
--) {
758 char curVal
= ByteAt(bytePos
);
759 if ((curVal
& mask
) != style
) {
760 SetByteAt(bytePos
, static_cast<char>((curVal
& ~mask
) | style
));
768 const char *CellBuffer::DeleteChars(int position
, int deleteLength
) {
769 // InsertString and DeleteChars are the bottleneck though which all changes occur
772 if (collectingUndo
) {
773 // Save into the undo/redo stack, but only the characters - not the formatting
774 data
= new char[deleteLength
/ 2];
775 for (int i
= 0; i
< deleteLength
/ 2; i
++) {
776 data
[i
] = ByteAt(position
+ i
* 2);
778 uh
.AppendAction(removeAction
, position
, data
, deleteLength
/ 2);
781 BasicDeleteChars(position
, deleteLength
);
786 int CellBuffer::ByteLength() {
790 int CellBuffer::Length() {
791 return ByteLength() / 2;
794 int CellBuffer::Lines() {
795 //Platform::DebugPrintf("Lines = %d\n", lv.lines);
799 int CellBuffer::LineStart(int line
) {
802 else if (line
> lv
.lines
)
805 return lv
.linesData
[line
].startPosition
;
808 bool CellBuffer::IsReadOnly() {
812 void CellBuffer::SetReadOnly(bool set
) {
816 void CellBuffer::SetSavePoint() {
820 bool CellBuffer::IsSavePoint() {
821 return uh
.IsSavePoint();
824 int CellBuffer::AddMark(int line
, int markerNum
) {
825 if ((line
>= 0) && (line
< lv
.lines
)) {
826 return lv
.AddMark(line
, markerNum
);
831 void CellBuffer::DeleteMark(int line
, int markerNum
) {
832 if ((line
>= 0) && (line
< lv
.lines
)) {
833 lv
.DeleteMark(line
, markerNum
);
837 void CellBuffer::DeleteMarkFromHandle(int markerHandle
) {
838 lv
.DeleteMarkFromHandle(markerHandle
);
841 int CellBuffer::GetMark(int line
) {
842 if ((line
>= 0) && (line
< lv
.lines
) && (lv
.linesData
[line
].handleSet
))
843 return lv
.linesData
[line
].handleSet
->MarkValue();
847 void CellBuffer::DeleteAllMarks(int markerNum
) {
848 for (int line
= 0; line
< lv
.lines
; line
++) {
849 lv
.DeleteMark(line
, markerNum
);
853 int CellBuffer::LineFromHandle(int markerHandle
) {
854 return lv
.LineFromHandle(markerHandle
);
859 void CellBuffer::BasicInsertString(int position
, char *s
, int insertLength
) {
860 //Platform::DebugPrintf("Inserting at %d for %d\n", position, insertLength);
861 if (insertLength
== 0)
863 RoomFor(insertLength
);
866 memcpy(body
+ part1len
, s
, insertLength
);
867 length
+= insertLength
;
868 part1len
+= insertLength
;
869 gaplen
-= insertLength
;
870 part2body
= body
+ gaplen
;
872 int lineInsert
= lv
.LineFromPosition(position
/ 2) + 1;
873 // Point all the lines after the insertion point further along in the buffer
874 for (int lineAfter
= lineInsert
; lineAfter
<= lv
.lines
; lineAfter
++) {
875 lv
.linesData
[lineAfter
].startPosition
+= insertLength
/ 2;
878 if ((position
- 2) >= 0)
879 chPrev
= ByteAt(position
- 2);
881 if ((position
+ insertLength
) < length
)
882 chAfter
= ByteAt(position
+ insertLength
);
883 if (chPrev
== '\r' && chAfter
== '\n') {
884 //Platform::DebugPrintf("Splitting a crlf pair at %d\n", lineInsert);
885 // Splitting up a crlf pair at position
886 lv
.InsertValue(lineInsert
, position
/ 2);
890 for (int i
= 0; i
< insertLength
; i
+= 2) {
893 //Platform::DebugPrintf("Inserting cr at %d\n", lineInsert);
894 lv
.InsertValue(lineInsert
, (position
+ i
) / 2 + 1);
896 } else if (ch
== '\n') {
897 if (chPrev
== '\r') {
898 //Platform::DebugPrintf("Patching cr before lf at %d\n", lineInsert-1);
899 // Patch up what was end of line
900 lv
.SetValue(lineInsert
- 1, (position
+ i
) / 2 + 1);
902 //Platform::DebugPrintf("Inserting lf at %d\n", lineInsert);
903 lv
.InsertValue(lineInsert
, (position
+ i
) / 2 + 1);
909 // Joining two lines where last insertion is cr and following text starts with lf
910 if (chAfter
== '\n') {
912 //Platform::DebugPrintf("Joining cr before lf at %d\n", lineInsert-1);
913 // End of line already in buffer so drop the newly created one
914 lv
.Remove(lineInsert
- 1);
919 void CellBuffer::BasicDeleteChars(int position
, int deleteLength
) {
920 //Platform::DebugPrintf("Deleting at %d for %d\n", position, deleteLength);
921 if (deleteLength
== 0)
924 if ((position
== 0) && (deleteLength
== length
)) {
925 // If whole buffer is being deleted, faster to reinitialise lines data
926 // than to delete each line.
927 //printf("Whole buffer being deleted\n");
930 // Have to fix up line positions before doing deletion as looking at text in buffer
931 // to work out which lines have been removed
933 int lineRemove
= lv
.LineFromPosition(position
/ 2) + 1;
934 // Point all the lines after the insertion point further along in the buffer
935 for (int lineAfter
= lineRemove
; lineAfter
<= lv
.lines
; lineAfter
++) {
936 lv
.linesData
[lineAfter
].startPosition
-= deleteLength
/ 2;
940 chPrev
= ByteAt(position
- 2);
941 char chBefore
= chPrev
;
943 if (position
< length
)
944 chNext
= ByteAt(position
);
945 bool ignoreNL
= false;
946 if (chPrev
== '\r' && chNext
== '\n') {
947 //Platform::DebugPrintf("Deleting lf after cr, move line end to cr at %d\n", lineRemove);
949 lv
.SetValue(lineRemove
, position
/ 2);
951 ignoreNL
= true; // First \n is not real deletion
955 for (int i
= 0; i
< deleteLength
; i
+= 2) {
957 if ((position
+ i
+ 2) < length
)
958 chNext
= ByteAt(position
+ i
+ 2);
959 //Platform::DebugPrintf("Deleting %d %x\n", i, ch);
961 if (chNext
!= '\n') {
962 //Platform::DebugPrintf("Removing cr end of line\n");
963 lv
.Remove(lineRemove
);
965 } else if (ch
== '\n') {
967 ignoreNL
= false; // Further \n are real deletions
969 //Platform::DebugPrintf("Removing lf end of line\n");
970 lv
.Remove(lineRemove
);
976 // May have to fix up end if last deletion causes cr to be next to lf
977 // or removes one of a crlf pair
979 if ((position
+ deleteLength
) < length
)
980 chAfter
= ByteAt(position
+ deleteLength
);
981 if (chBefore
== '\r' && chAfter
== '\n') {
982 //d.printf("Joining cr before lf at %d\n", lineRemove);
983 // Using lineRemove-1 as cr ended line before start of deletion
984 lv
.Remove(lineRemove
- 1);
985 lv
.SetValue(lineRemove
- 1, position
/ 2 + 1);
989 length
-= deleteLength
;
990 gaplen
+= deleteLength
;
991 part2body
= body
+ gaplen
;
994 bool CellBuffer::SetUndoCollection(bool collectUndo
) {
995 collectingUndo
= collectUndo
;
996 uh
.DropUndoSequence();
997 return collectingUndo
;
1000 bool CellBuffer::IsCollectingUndo() {
1001 return collectingUndo
;
1004 void CellBuffer::BeginUndoAction() {
1005 uh
.BeginUndoAction();
1008 void CellBuffer::EndUndoAction() {
1012 void CellBuffer::DeleteUndoHistory() {
1013 uh
.DeleteUndoHistory();
1016 bool CellBuffer::CanUndo() {
1017 return (!readOnly
) && (uh
.CanUndo());
1020 int CellBuffer::StartUndo() {
1021 return uh
.StartUndo();
1024 const Action
&CellBuffer::GetUndoStep() const {
1025 return uh
.GetUndoStep();
1028 void CellBuffer::PerformUndoStep() {
1029 const Action
&actionStep
= uh
.GetUndoStep();
1030 if (actionStep
.at
== insertAction
) {
1031 BasicDeleteChars(actionStep
.position
, actionStep
.lenData
*2);
1032 } else if (actionStep
.at
== removeAction
) {
1033 char *styledData
= new char[actionStep
.lenData
* 2];
1034 for (int i
= 0; i
< actionStep
.lenData
; i
++) {
1035 styledData
[i
*2] = actionStep
.data
[i
];
1036 styledData
[i
*2 + 1] = 0;
1038 BasicInsertString(actionStep
.position
, styledData
, actionStep
.lenData
*2);
1039 delete []styledData
;
1041 uh
.CompletedUndoStep();
1044 bool CellBuffer::CanRedo() {
1045 return (!readOnly
) && (uh
.CanRedo());
1048 int CellBuffer::StartRedo() {
1049 return uh
.StartRedo();
1052 const Action
&CellBuffer::GetRedoStep() const {
1053 return uh
.GetRedoStep();
1056 void CellBuffer::PerformRedoStep() {
1057 const Action
&actionStep
= uh
.GetRedoStep();
1058 if (actionStep
.at
== insertAction
) {
1059 char *styledData
= new char[actionStep
.lenData
* 2];
1060 for (int i
= 0; i
< actionStep
.lenData
; i
++) {
1061 styledData
[i
*2] = actionStep
.data
[i
];
1062 styledData
[i
*2 + 1] = 0;
1064 BasicInsertString(actionStep
.position
, styledData
, actionStep
.lenData
*2);
1065 delete []styledData
;
1066 } else if (actionStep
.at
== removeAction
) {
1067 BasicDeleteChars(actionStep
.position
, actionStep
.lenData
*2);
1069 uh
.CompletedRedoStep();
1072 int CellBuffer::SetLineState(int line
, int state
) {
1073 int stateOld
= lineStates
[line
];
1074 lineStates
[line
] = state
;
1078 int CellBuffer::GetLineState(int line
) {
1079 return lineStates
[line
];
1082 int CellBuffer::GetMaxLineState() {
1083 return lineStates
.Length();
1086 int CellBuffer::SetLevel(int line
, int level
) {
1088 if ((line
>= 0) && (line
< lv
.lines
)) {
1092 prev
= lv
.levels
[line
];
1093 if (lv
.levels
[line
] != level
) {
1094 lv
.levels
[line
] = level
;
1100 int CellBuffer::GetLevel(int line
) {
1101 if (lv
.levels
&& (line
>= 0) && (line
< lv
.lines
)) {
1102 return lv
.levels
[line
];
1104 return SC_FOLDLEVELBASE
;
1108 void CellBuffer::ClearLevels() {