]>
git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/scintilla/src/CellBuffer.cxx
420dee6ff70ed50386f9e0bc914797201fdae91e
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() {
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
) {
742 char curVal
= ByteAt(position
* 2 + 1);
743 if ((curVal
& mask
) != style
) {
744 SetByteAt(position
*2 + 1, static_cast<char>((curVal
& ~mask
) | style
));
751 bool CellBuffer::SetStyleFor(int position
, int lengthStyle
, char style
, char mask
) {
752 int bytePos
= position
* 2 + 1;
753 bool changed
= false;
754 PLATFORM_ASSERT(lengthStyle
== 0 ||
755 (lengthStyle
> 0 && lengthStyle
+ position
< length
));
756 while (lengthStyle
--) {
757 char curVal
= ByteAt(bytePos
);
758 if ((curVal
& mask
) != style
) {
759 SetByteAt(bytePos
, static_cast<char>((curVal
& ~mask
) | style
));
767 const char *CellBuffer::DeleteChars(int position
, int deleteLength
) {
768 // InsertString and DeleteChars are the bottleneck though which all changes occur
771 if (collectingUndo
) {
772 // Save into the undo/redo stack, but only the characters - not the formatting
773 data
= new char[deleteLength
/ 2];
774 for (int i
= 0; i
< deleteLength
/ 2; i
++) {
775 data
[i
] = ByteAt(position
+ i
* 2);
777 uh
.AppendAction(removeAction
, position
, data
, deleteLength
/ 2);
780 BasicDeleteChars(position
, deleteLength
);
785 int CellBuffer::ByteLength() {
789 int CellBuffer::Length() {
790 return ByteLength() / 2;
793 int CellBuffer::Lines() {
794 //Platform::DebugPrintf("Lines = %d\n", lv.lines);
798 int CellBuffer::LineStart(int line
) {
801 else if (line
> lv
.lines
)
804 return lv
.linesData
[line
].startPosition
;
807 bool CellBuffer::IsReadOnly() {
811 void CellBuffer::SetReadOnly(bool set
) {
815 void CellBuffer::SetSavePoint() {
819 bool CellBuffer::IsSavePoint() {
820 return uh
.IsSavePoint();
823 int CellBuffer::AddMark(int line
, int markerNum
) {
824 if ((line
>= 0) && (line
< lv
.lines
)) {
825 return lv
.AddMark(line
, markerNum
);
830 void CellBuffer::DeleteMark(int line
, int markerNum
) {
831 if ((line
>= 0) && (line
< lv
.lines
)) {
832 lv
.DeleteMark(line
, markerNum
);
836 void CellBuffer::DeleteMarkFromHandle(int markerHandle
) {
837 lv
.DeleteMarkFromHandle(markerHandle
);
840 int CellBuffer::GetMark(int line
) {
841 if ((line
>= 0) && (line
< lv
.lines
) && (lv
.linesData
[line
].handleSet
))
842 return lv
.linesData
[line
].handleSet
->MarkValue();
846 void CellBuffer::DeleteAllMarks(int markerNum
) {
847 for (int line
= 0; line
< lv
.lines
; line
++) {
848 lv
.DeleteMark(line
, markerNum
);
852 int CellBuffer::LineFromHandle(int markerHandle
) {
853 return lv
.LineFromHandle(markerHandle
);
858 void CellBuffer::BasicInsertString(int position
, char *s
, int insertLength
) {
859 //Platform::DebugPrintf("Inserting at %d for %d\n", position, insertLength);
860 if (insertLength
== 0)
862 RoomFor(insertLength
);
865 memcpy(body
+ part1len
, s
, insertLength
);
866 length
+= insertLength
;
867 part1len
+= insertLength
;
868 gaplen
-= insertLength
;
869 part2body
= body
+ gaplen
;
871 int lineInsert
= lv
.LineFromPosition(position
/ 2) + 1;
872 // Point all the lines after the insertion point further along in the buffer
873 for (int lineAfter
= lineInsert
; lineAfter
<= lv
.lines
; lineAfter
++) {
874 lv
.linesData
[lineAfter
].startPosition
+= insertLength
/ 2;
877 if ((position
- 2) >= 0)
878 chPrev
= ByteAt(position
- 2);
880 if ((position
+ insertLength
) < length
)
881 chAfter
= ByteAt(position
+ insertLength
);
882 if (chPrev
== '\r' && chAfter
== '\n') {
883 //Platform::DebugPrintf("Splitting a crlf pair at %d\n", lineInsert);
884 // Splitting up a crlf pair at position
885 lv
.InsertValue(lineInsert
, position
/ 2);
889 for (int i
= 0; i
< insertLength
; i
+= 2) {
892 //Platform::DebugPrintf("Inserting cr at %d\n", lineInsert);
893 lv
.InsertValue(lineInsert
, (position
+ i
) / 2 + 1);
895 } else if (ch
== '\n') {
896 if (chPrev
== '\r') {
897 //Platform::DebugPrintf("Patching cr before lf at %d\n", lineInsert-1);
898 // Patch up what was end of line
899 lv
.SetValue(lineInsert
- 1, (position
+ i
) / 2 + 1);
901 //Platform::DebugPrintf("Inserting lf at %d\n", lineInsert);
902 lv
.InsertValue(lineInsert
, (position
+ i
) / 2 + 1);
908 // Joining two lines where last insertion is cr and following text starts with lf
909 if (chAfter
== '\n') {
911 //Platform::DebugPrintf("Joining cr before lf at %d\n", lineInsert-1);
912 // End of line already in buffer so drop the newly created one
913 lv
.Remove(lineInsert
- 1);
918 void CellBuffer::BasicDeleteChars(int position
, int deleteLength
) {
919 //Platform::DebugPrintf("Deleting at %d for %d\n", position, deleteLength);
920 if (deleteLength
== 0)
923 if ((position
== 0) && (deleteLength
== length
)) {
924 // If whole buffer is being deleted, faster to reinitialise lines data
925 // than to delete each line.
926 //printf("Whole buffer being deleted\n");
929 // Have to fix up line positions before doing deletion as looking at text in buffer
930 // to work out which lines have been removed
932 int lineRemove
= lv
.LineFromPosition(position
/ 2) + 1;
933 // Point all the lines after the insertion point further along in the buffer
934 for (int lineAfter
= lineRemove
; lineAfter
<= lv
.lines
; lineAfter
++) {
935 lv
.linesData
[lineAfter
].startPosition
-= deleteLength
/ 2;
939 chPrev
= ByteAt(position
- 2);
940 char chBefore
= chPrev
;
942 if (position
< length
)
943 chNext
= ByteAt(position
);
944 bool ignoreNL
= false;
945 if (chPrev
== '\r' && chNext
== '\n') {
946 //Platform::DebugPrintf("Deleting lf after cr, move line end to cr at %d\n", lineRemove);
948 lv
.SetValue(lineRemove
, position
/ 2);
950 ignoreNL
= true; // First \n is not real deletion
954 for (int i
= 0; i
< deleteLength
; i
+= 2) {
956 if ((position
+ i
+ 2) < length
)
957 chNext
= ByteAt(position
+ i
+ 2);
958 //Platform::DebugPrintf("Deleting %d %x\n", i, ch);
960 if (chNext
!= '\n') {
961 //Platform::DebugPrintf("Removing cr end of line\n");
962 lv
.Remove(lineRemove
);
964 } else if (ch
== '\n') {
966 ignoreNL
= false; // Further \n are real deletions
968 //Platform::DebugPrintf("Removing lf end of line\n");
969 lv
.Remove(lineRemove
);
975 // May have to fix up end if last deletion causes cr to be next to lf
976 // or removes one of a crlf pair
978 if ((position
+ deleteLength
) < length
)
979 chAfter
= ByteAt(position
+ deleteLength
);
980 if (chBefore
== '\r' && chAfter
== '\n') {
981 //d.printf("Joining cr before lf at %d\n", lineRemove);
982 // Using lineRemove-1 as cr ended line before start of deletion
983 lv
.Remove(lineRemove
- 1);
984 lv
.SetValue(lineRemove
- 1, position
/ 2 + 1);
988 length
-= deleteLength
;
989 gaplen
+= deleteLength
;
990 part2body
= body
+ gaplen
;
993 bool CellBuffer::SetUndoCollection(bool collectUndo
) {
994 collectingUndo
= collectUndo
;
995 uh
.DropUndoSequence();
996 return collectingUndo
;
999 bool CellBuffer::IsCollectingUndo() {
1000 return collectingUndo
;
1003 void CellBuffer::BeginUndoAction() {
1004 uh
.BeginUndoAction();
1007 void CellBuffer::EndUndoAction() {
1011 void CellBuffer::DeleteUndoHistory() {
1012 uh
.DeleteUndoHistory();
1015 bool CellBuffer::CanUndo() {
1016 return (!readOnly
) && (uh
.CanUndo());
1019 int CellBuffer::StartUndo() {
1020 return uh
.StartUndo();
1023 const Action
&CellBuffer::GetUndoStep() const {
1024 return uh
.GetUndoStep();
1027 void CellBuffer::PerformUndoStep() {
1028 const Action
&actionStep
= uh
.GetUndoStep();
1029 if (actionStep
.at
== insertAction
) {
1030 BasicDeleteChars(actionStep
.position
, actionStep
.lenData
*2);
1031 } else if (actionStep
.at
== removeAction
) {
1032 char *styledData
= new char[actionStep
.lenData
* 2];
1033 for (int i
= 0; i
< actionStep
.lenData
; i
++) {
1034 styledData
[i
*2] = actionStep
.data
[i
];
1035 styledData
[i
*2 + 1] = 0;
1037 BasicInsertString(actionStep
.position
, styledData
, actionStep
.lenData
*2);
1038 delete []styledData
;
1040 uh
.CompletedUndoStep();
1043 bool CellBuffer::CanRedo() {
1044 return (!readOnly
) && (uh
.CanRedo());
1047 int CellBuffer::StartRedo() {
1048 return uh
.StartRedo();
1051 const Action
&CellBuffer::GetRedoStep() const {
1052 return uh
.GetRedoStep();
1055 void CellBuffer::PerformRedoStep() {
1056 const Action
&actionStep
= uh
.GetRedoStep();
1057 if (actionStep
.at
== insertAction
) {
1058 char *styledData
= new char[actionStep
.lenData
* 2];
1059 for (int i
= 0; i
< actionStep
.lenData
; i
++) {
1060 styledData
[i
*2] = actionStep
.data
[i
];
1061 styledData
[i
*2 + 1] = 0;
1063 BasicInsertString(actionStep
.position
, styledData
, actionStep
.lenData
*2);
1064 delete []styledData
;
1065 } else if (actionStep
.at
== removeAction
) {
1066 BasicDeleteChars(actionStep
.position
, actionStep
.lenData
*2);
1068 uh
.CompletedRedoStep();
1071 int CellBuffer::SetLineState(int line
, int state
) {
1072 int stateOld
= lineStates
[line
];
1073 lineStates
[line
] = state
;
1077 int CellBuffer::GetLineState(int line
) {
1078 return lineStates
[line
];
1081 int CellBuffer::GetMaxLineState() {
1082 return lineStates
.Length();
1085 int CellBuffer::SetLevel(int line
, int level
) {
1087 if ((line
>= 0) && (line
< lv
.lines
)) {
1091 prev
= lv
.levels
[line
];
1092 if (lv
.levels
[line
] != level
) {
1093 lv
.levels
[line
] = level
;
1099 int CellBuffer::GetLevel(int line
) {
1100 if (lv
.levels
&& (line
>= 0) && (line
< lv
.lines
)) {
1101 return lv
.levels
[line
];
1103 return SC_FOLDLEVELBASE
;
1107 void CellBuffer::ClearLevels() {