]>
git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/CellBuffer.cxx
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"
16 #include "SplitVector.h"
17 #include "Partitioning.h"
18 #include "CellBuffer.h"
21 using namespace Scintilla
;
24 MarkerHandleSet::MarkerHandleSet() {
28 MarkerHandleSet::~MarkerHandleSet() {
29 MarkerHandleNumber
*mhn
= root
;
31 MarkerHandleNumber
*mhnToFree
= mhn
;
38 int MarkerHandleSet::Length() const {
40 MarkerHandleNumber
*mhn
= root
;
48 int MarkerHandleSet::NumberFromHandle(int handle
) const {
49 MarkerHandleNumber
*mhn
= root
;
51 if (mhn
->handle
== handle
) {
59 int MarkerHandleSet::MarkValue() const {
61 MarkerHandleNumber
*mhn
= root
;
63 m
|= (1 << mhn
->number
);
69 bool MarkerHandleSet::Contains(int handle
) const {
70 MarkerHandleNumber
*mhn
= root
;
72 if (mhn
->handle
== handle
) {
80 bool MarkerHandleSet::InsertHandle(int handle
, int markerNum
) {
81 MarkerHandleNumber
*mhn
= new MarkerHandleNumber
;
85 mhn
->number
= markerNum
;
91 void MarkerHandleSet::RemoveHandle(int handle
) {
92 MarkerHandleNumber
**pmhn
= &root
;
94 MarkerHandleNumber
*mhn
= *pmhn
;
95 if (mhn
->handle
== handle
) {
100 pmhn
= &((*pmhn
)->next
);
104 bool MarkerHandleSet::RemoveNumber(int markerNum
) {
105 bool performedDeletion
= false;
106 MarkerHandleNumber
**pmhn
= &root
;
108 MarkerHandleNumber
*mhn
= *pmhn
;
109 if (mhn
->number
== markerNum
) {
112 performedDeletion
= true;
114 pmhn
= &((*pmhn
)->next
);
117 return performedDeletion
;
120 void MarkerHandleSet::CombineWith(MarkerHandleSet
*other
) {
121 MarkerHandleNumber
**pmhn
= &root
;
123 pmhn
= &((*pmhn
)->next
);
129 LineVector::LineVector() : starts(256) {
135 LineVector::~LineVector() {
137 for (int line
= 0; line
< markers
.Length(); line
++) {
138 delete markers
[line
];
145 void LineVector::Init() {
147 for (int line
= 0; line
< markers
.Length(); line
++) {
148 delete markers
[line
];
155 void LineVector::ExpandLevels(int sizeNew
) {
156 levels
.InsertValue(levels
.Length(), sizeNew
- levels
.Length(), SC_FOLDLEVELBASE
);
159 void LineVector::ClearLevels() {
163 int LineVector::SetLevel(int line
, int level
) {
165 if ((line
>= 0) && (line
< Lines())) {
166 if (!levels
.Length()) {
167 ExpandLevels(Lines() + 1);
171 levels
[line
] = level
;
177 int LineVector::GetLevel(int line
) {
178 if (levels
.Length() && (line
>= 0) && (line
< Lines())) {
181 return SC_FOLDLEVELBASE
;
185 void LineVector::InsertText(int line
, int delta
) {
186 starts
.InsertText(line
, delta
);
189 void LineVector::InsertLine(int line
, int position
) {
190 starts
.InsertPartition(line
, position
);
191 if (markers
.Length()) {
192 markers
.Insert(line
, 0);
194 if (levels
.Length()) {
195 int level
= SC_FOLDLEVELBASE
;
196 if ((line
> 0) && (line
< Lines())) {
197 level
= levels
[line
-1] & ~SC_FOLDLEVELWHITEFLAG
;
199 levels
.InsertValue(line
, 1, level
);
203 void LineVector::SetLineStart(int line
, int position
) {
204 starts
.SetPartitionStartPosition(line
, position
);
207 void LineVector::RemoveLine(int line
) {
208 starts
.RemovePartition(line
);
209 // Retain the markers from the deleted line by oring them into the previous line
210 if (markers
.Length()) {
212 MergeMarkers(line
- 1);
214 markers
.Delete(line
);
216 if (levels
.Length()) {
217 // Move up following lines but merge header flag from this line
218 // to line before to avoid a temporary disappearence causing expansion.
219 int firstHeader
= levels
[line
] & SC_FOLDLEVELHEADERFLAG
;
222 levels
[line
-1] |= firstHeader
;
226 int LineVector::LineFromPosition(int pos
) {
227 return starts
.PartitionFromPosition(pos
);
230 int LineVector::MarkValue(int line
) {
231 if (markers
.Length() && markers
[line
])
232 return markers
[line
]->MarkValue();
237 int LineVector::AddMark(int line
, int markerNum
) {
239 if (!markers
.Length()) {
240 // No existing markers so allocate one element per line
241 markers
.InsertValue(0, Lines(), 0);
243 if (!markers
[line
]) {
244 // Need new structure to hold marker handle
245 markers
[line
] = new MarkerHandleSet();
249 markers
[line
]->InsertHandle(handleCurrent
, markerNum
);
251 return handleCurrent
;
254 void LineVector::MergeMarkers(int pos
) {
255 if (markers
[pos
+ 1] != NULL
) {
256 if (markers
[pos
] == NULL
)
257 markers
[pos
] = new MarkerHandleSet
;
258 markers
[pos
]->CombineWith(markers
[pos
+ 1]);
259 delete markers
[pos
+ 1];
260 markers
[pos
+ 1] = NULL
;
264 void LineVector::DeleteMark(int line
, int markerNum
, bool all
) {
265 if (markers
.Length() && markers
[line
]) {
266 if (markerNum
== -1) {
267 delete markers
[line
];
268 markers
[line
] = NULL
;
270 bool performedDeletion
= markers
[line
]->RemoveNumber(markerNum
);
271 while (all
&& performedDeletion
) {
272 performedDeletion
= markers
[line
]->RemoveNumber(markerNum
);
274 if (markers
[line
]->Length() == 0) {
275 delete markers
[line
];
276 markers
[line
] = NULL
;
282 void LineVector::DeleteMarkFromHandle(int markerHandle
) {
283 int line
= LineFromHandle(markerHandle
);
285 markers
[line
]->RemoveHandle(markerHandle
);
286 if (markers
[line
]->Length() == 0) {
287 delete markers
[line
];
288 markers
[line
] = NULL
;
293 int LineVector::LineFromHandle(int markerHandle
) {
294 if (markers
.Length()) {
295 for (int line
= 0; line
< Lines(); line
++) {
297 if (markers
[line
]->Contains(markerHandle
)) {
317 void Action::Create(actionType at_
, int position_
, char *data_
, int lenData_
, bool mayCoalesce_
) {
319 position
= position_
;
323 mayCoalesce
= mayCoalesce_
;
326 void Action::Destroy() {
331 void Action::Grab(Action
*source
) {
334 position
= source
->position
;
337 lenData
= source
->lenData
;
338 mayCoalesce
= source
->mayCoalesce
;
340 // Ownership of source data transferred to this
341 source
->position
= 0;
342 source
->at
= startAction
;
345 source
->mayCoalesce
= true;
348 // The undo history stores a sequence of user operations that represent the user's view of the
349 // commands executed on the text.
350 // Each user operation contains a sequence of text insertion and text deletion actions.
351 // All the user operations are stored in a list of individual actions with 'start' actions used
352 // as delimiters between user operations.
353 // Initially there is one start action in the history.
354 // As each action is performed, it is recorded in the history. The action may either become
355 // part of the current user operation or may start a new user operation. If it is to be part of the
356 // current operation, then it overwrites the current last action. If it is to be part of a new
357 // operation, it is appended after the current last action.
358 // After writing the new action, a new start action is appended at the end of the history.
359 // The decision of whether to start a new user operation is based upon two factors. If a
360 // compound operation has been explicitly started by calling BeginUndoAction and no matching
361 // EndUndoAction (these calls nest) has been called, then the action is coalesced into the current
362 // operation. If there is no outstanding BeginUndoAction call then a new operation is started
363 // unless it looks as if the new action is caused by the user typing or deleting a stream of text.
364 // Sequences that look like typing or deletion are coalesced into a single user operation.
366 UndoHistory::UndoHistory() {
369 actions
= new Action
[lenActions
];
372 undoSequenceDepth
= 0;
375 actions
[currentAction
].Create(startAction
);
378 UndoHistory::~UndoHistory() {
383 void UndoHistory::EnsureUndoRoom() {
384 // Have to test that there is room for 2 more actions in the array
385 // as two actions may be created by the calling function
386 if (currentAction
>= (lenActions
- 2)) {
387 // Run out of undo nodes so extend the array
388 int lenActionsNew
= lenActions
* 2;
389 Action
*actionsNew
= new Action
[lenActionsNew
];
392 for (int act
= 0; act
<= currentAction
; act
++)
393 actionsNew
[act
].Grab(&actions
[act
]);
395 lenActions
= lenActionsNew
;
396 actions
= actionsNew
;
400 void UndoHistory::AppendAction(actionType at
, int position
, char *data
, int lengthData
,
401 bool &startSequence
) {
403 //Platform::DebugPrintf("%% %d action %d %d %d\n", at, position, lengthData, currentAction);
404 //Platform::DebugPrintf("^ %d action %d %d\n", actions[currentAction - 1].at,
405 // actions[currentAction - 1].position, actions[currentAction - 1].lenData);
406 if (currentAction
< savePoint
) {
409 int oldCurrentAction
= currentAction
;
410 if (currentAction
>= 1) {
411 if (0 == undoSequenceDepth
) {
412 // Top level actions may not always be coalesced
413 Action
&actPrevious
= actions
[currentAction
- 1];
414 // See if current action can be coalesced into previous action
415 // Will work if both are inserts or deletes and position is same
416 if (at
!= actPrevious
.at
) {
418 } else if (currentAction
== savePoint
) {
420 } else if ((at
== insertAction
) &&
421 (position
!= (actPrevious
.position
+ actPrevious
.lenData
))) {
422 // Insertions must be immediately after to coalesce
424 } else if (!actions
[currentAction
].mayCoalesce
) {
425 // Not allowed to coalesce if this set
427 } else if (at
== removeAction
) {
428 if ((lengthData
== 1) || (lengthData
== 2)){
429 if ((position
+ lengthData
) == actPrevious
.position
) {
431 } else if (position
== actPrevious
.position
) {
434 // Removals must be at same position to coalesce
438 // Removals must be of one character to coalesce
446 // Actions not at top level are always coalesced unless this is after return to top level
447 if (!actions
[currentAction
].mayCoalesce
)
453 startSequence
= oldCurrentAction
!= currentAction
;
454 actions
[currentAction
].Create(at
, position
, data
, lengthData
);
456 actions
[currentAction
].Create(startAction
);
457 maxAction
= currentAction
;
460 void UndoHistory::BeginUndoAction() {
462 if (undoSequenceDepth
== 0) {
463 if (actions
[currentAction
].at
!= startAction
) {
465 actions
[currentAction
].Create(startAction
);
466 maxAction
= currentAction
;
468 actions
[currentAction
].mayCoalesce
= false;
473 void UndoHistory::EndUndoAction() {
474 PLATFORM_ASSERT(undoSequenceDepth
> 0);
477 if (0 == undoSequenceDepth
) {
478 if (actions
[currentAction
].at
!= startAction
) {
480 actions
[currentAction
].Create(startAction
);
481 maxAction
= currentAction
;
483 actions
[currentAction
].mayCoalesce
= false;
487 void UndoHistory::DropUndoSequence() {
488 undoSequenceDepth
= 0;
491 void UndoHistory::DeleteUndoHistory() {
492 for (int i
= 1; i
< maxAction
; i
++)
493 actions
[i
].Destroy();
496 actions
[currentAction
].Create(startAction
);
500 void UndoHistory::SetSavePoint() {
501 savePoint
= currentAction
;
504 bool UndoHistory::IsSavePoint() const {
505 return savePoint
== currentAction
;
508 bool UndoHistory::CanUndo() const {
509 return (currentAction
> 0) && (maxAction
> 0);
512 int UndoHistory::StartUndo() {
513 // Drop any trailing startAction
514 if (actions
[currentAction
].at
== startAction
&& currentAction
> 0)
517 // Count the steps in this action
518 int act
= currentAction
;
519 while (actions
[act
].at
!= startAction
&& act
> 0) {
522 return currentAction
- act
;
525 const Action
&UndoHistory::GetUndoStep() const {
526 return actions
[currentAction
];
529 void UndoHistory::CompletedUndoStep() {
533 bool UndoHistory::CanRedo() const {
534 return maxAction
> currentAction
;
537 int UndoHistory::StartRedo() {
538 // Drop any leading startAction
539 if (actions
[currentAction
].at
== startAction
&& currentAction
< maxAction
)
542 // Count the steps in this action
543 int act
= currentAction
;
544 while (actions
[act
].at
!= startAction
&& act
< maxAction
) {
547 return act
- currentAction
;
550 const Action
&UndoHistory::GetRedoStep() const {
551 return actions
[currentAction
];
554 void UndoHistory::CompletedRedoStep() {
558 CellBuffer::CellBuffer() {
560 collectingUndo
= true;
563 CellBuffer::~CellBuffer() {
566 char CellBuffer::CharAt(int position
) const {
567 return substance
.ValueAt(position
);
570 void CellBuffer::GetCharRange(char *buffer
, int position
, int lengthRetrieve
) {
571 if (lengthRetrieve
< 0)
575 if ((position
+ lengthRetrieve
) > substance
.Length()) {
576 Platform::DebugPrintf("Bad GetCharRange %d for %d of %d\n", position
,
577 lengthRetrieve
, substance
.Length());
581 for (int i
=0; i
<lengthRetrieve
; i
++) {
582 *buffer
++ = substance
.ValueAt(position
+ i
);
586 char CellBuffer::StyleAt(int position
) {
587 return style
.ValueAt(position
);
590 // The char* returned is to an allocation owned by the undo history
591 const char *CellBuffer::InsertString(int position
, const char *s
, int insertLength
, bool &startSequence
) {
593 // InsertString and DeleteChars are the bottleneck though which all changes occur
595 if (collectingUndo
) {
596 // Save into the undo/redo stack, but only the characters - not the formatting
597 // This takes up about half load time
598 data
= new char[insertLength
];
599 for (int i
= 0; i
< insertLength
; i
++) {
602 uh
.AppendAction(insertAction
, position
, data
, insertLength
, startSequence
);
605 BasicInsertString(position
, s
, insertLength
);
610 bool CellBuffer::SetStyleAt(int position
, char styleValue
, char mask
) {
612 char curVal
= style
.ValueAt(position
);
613 if ((curVal
& mask
) != styleValue
) {
614 style
.SetValueAt(position
, static_cast<char>((curVal
& ~mask
) | styleValue
));
621 bool CellBuffer::SetStyleFor(int position
, int lengthStyle
, char styleValue
, char mask
) {
622 bool changed
= false;
623 PLATFORM_ASSERT(lengthStyle
== 0 ||
624 (lengthStyle
> 0 && lengthStyle
+ position
<= style
.Length()));
625 while (lengthStyle
--) {
626 char curVal
= style
.ValueAt(position
);
627 if ((curVal
& mask
) != styleValue
) {
628 style
.SetValueAt(position
, static_cast<char>((curVal
& ~mask
) | styleValue
));
636 // The char* returned is to an allocation owned by the undo history
637 const char *CellBuffer::DeleteChars(int position
, int deleteLength
, bool &startSequence
) {
638 // InsertString and DeleteChars are the bottleneck though which all changes occur
639 PLATFORM_ASSERT(deleteLength
> 0);
642 if (collectingUndo
) {
643 // Save into the undo/redo stack, but only the characters - not the formatting
644 data
= new char[deleteLength
];
645 for (int i
= 0; i
< deleteLength
; i
++) {
646 data
[i
] = substance
.ValueAt(position
+ i
);
648 uh
.AppendAction(removeAction
, position
, data
, deleteLength
, startSequence
);
651 BasicDeleteChars(position
, deleteLength
);
656 int CellBuffer::Length() const {
657 return substance
.Length();
660 void CellBuffer::Allocate(int newSize
) {
661 substance
.ReAllocate(newSize
);
662 style
.ReAllocate(newSize
);
665 int CellBuffer::Lines() const {
669 int CellBuffer::LineStart(int line
) const {
672 else if (line
>= Lines())
675 return lv
.LineStart(line
);
678 bool CellBuffer::IsReadOnly() {
682 void CellBuffer::SetReadOnly(bool set
) {
686 void CellBuffer::SetSavePoint() {
690 bool CellBuffer::IsSavePoint() {
691 return uh
.IsSavePoint();
694 int CellBuffer::AddMark(int line
, int markerNum
) {
695 if ((line
>= 0) && (line
< Lines())) {
696 return lv
.AddMark(line
, markerNum
);
701 void CellBuffer::DeleteMark(int line
, int markerNum
) {
702 if ((line
>= 0) && (line
< Lines())) {
703 lv
.DeleteMark(line
, markerNum
, false);
707 void CellBuffer::DeleteMarkFromHandle(int markerHandle
) {
708 lv
.DeleteMarkFromHandle(markerHandle
);
711 int CellBuffer::GetMark(int line
) {
712 if ((line
>= 0) && (line
< Lines()))
713 return lv
.MarkValue(line
);
717 void CellBuffer::DeleteAllMarks(int markerNum
) {
718 for (int line
= 0; line
< Lines(); line
++) {
719 lv
.DeleteMark(line
, markerNum
, true);
723 int CellBuffer::LineFromHandle(int markerHandle
) {
724 return lv
.LineFromHandle(markerHandle
);
729 void CellBuffer::InsertLine(int line
, int position
) {
730 lv
.InsertLine(line
, position
);
731 if (lineStates
.Length()) {
732 lineStates
.Insert(line
, 0);
736 void CellBuffer::RemoveLine(int line
) {
738 if (lineStates
.Length()) {
739 lineStates
.Delete(line
);
743 void CellBuffer::BasicInsertString(int position
, const char *s
, int insertLength
) {
744 if (insertLength
== 0)
746 PLATFORM_ASSERT(insertLength
> 0);
748 substance
.InsertFromArray(position
, s
, 0, insertLength
);
749 style
.InsertValue(position
, insertLength
, 0);
751 int lineInsert
= lv
.LineFromPosition(position
) + 1;
752 // Point all the lines after the insertion point further along in the buffer
753 lv
.InsertText(lineInsert
-1, insertLength
);
754 char chPrev
= substance
.ValueAt(position
- 1);
755 char chAfter
= substance
.ValueAt(position
+ insertLength
);
756 if (chPrev
== '\r' && chAfter
== '\n') {
757 // Splitting up a crlf pair at position
758 InsertLine(lineInsert
, position
);
762 for (int i
= 0; i
< insertLength
; i
++) {
765 InsertLine(lineInsert
, (position
+ i
) + 1);
767 } else if (ch
== '\n') {
768 if (chPrev
== '\r') {
769 // Patch up what was end of line
770 lv
.SetLineStart(lineInsert
- 1, (position
+ i
) + 1);
772 InsertLine(lineInsert
, (position
+ i
) + 1);
778 // Joining two lines where last insertion is cr and following substance starts with lf
779 if (chAfter
== '\n') {
781 // End of line already in buffer so drop the newly created one
782 RemoveLine(lineInsert
- 1);
787 void CellBuffer::BasicDeleteChars(int position
, int deleteLength
) {
788 if (deleteLength
== 0)
791 if ((position
== 0) && (deleteLength
== substance
.Length())) {
792 // If whole buffer is being deleted, faster to reinitialise lines data
793 // than to delete each line.
796 // Have to fix up line positions before doing deletion as looking at text in buffer
797 // to work out which lines have been removed
799 int lineRemove
= lv
.LineFromPosition(position
) + 1;
800 lv
.InsertText(lineRemove
-1, - (deleteLength
));
801 char chPrev
= substance
.ValueAt(position
- 1);
802 char chBefore
= chPrev
;
803 char chNext
= substance
.ValueAt(position
);
804 bool ignoreNL
= false;
805 if (chPrev
== '\r' && chNext
== '\n') {
807 lv
.SetLineStart(lineRemove
, position
);
809 ignoreNL
= true; // First \n is not real deletion
813 for (int i
= 0; i
< deleteLength
; i
++) {
814 chNext
= substance
.ValueAt(position
+ i
+ 1);
816 if (chNext
!= '\n') {
817 RemoveLine(lineRemove
);
819 } else if (ch
== '\n') {
821 ignoreNL
= false; // Further \n are real deletions
823 RemoveLine(lineRemove
);
829 // May have to fix up end if last deletion causes cr to be next to lf
830 // or removes one of a crlf pair
831 char chAfter
= substance
.ValueAt(position
+ deleteLength
);
832 if (chBefore
== '\r' && chAfter
== '\n') {
833 // Using lineRemove-1 as cr ended line before start of deletion
834 RemoveLine(lineRemove
- 1);
835 lv
.SetLineStart(lineRemove
- 1, position
+ 1);
838 substance
.DeleteRange(position
, deleteLength
);
839 style
.DeleteRange(position
, deleteLength
);
842 bool CellBuffer::SetUndoCollection(bool collectUndo
) {
843 collectingUndo
= collectUndo
;
844 uh
.DropUndoSequence();
845 return collectingUndo
;
848 bool CellBuffer::IsCollectingUndo() {
849 return collectingUndo
;
852 void CellBuffer::BeginUndoAction() {
853 uh
.BeginUndoAction();
856 void CellBuffer::EndUndoAction() {
860 void CellBuffer::DeleteUndoHistory() {
861 uh
.DeleteUndoHistory();
864 bool CellBuffer::CanUndo() {
868 int CellBuffer::StartUndo() {
869 return uh
.StartUndo();
872 const Action
&CellBuffer::GetUndoStep() const {
873 return uh
.GetUndoStep();
876 void CellBuffer::PerformUndoStep() {
877 const Action
&actionStep
= uh
.GetUndoStep();
878 if (actionStep
.at
== insertAction
) {
879 BasicDeleteChars(actionStep
.position
, actionStep
.lenData
);
880 } else if (actionStep
.at
== removeAction
) {
881 BasicInsertString(actionStep
.position
, actionStep
.data
, actionStep
.lenData
);
883 uh
.CompletedUndoStep();
886 bool CellBuffer::CanRedo() {
890 int CellBuffer::StartRedo() {
891 return uh
.StartRedo();
894 const Action
&CellBuffer::GetRedoStep() const {
895 return uh
.GetRedoStep();
898 void CellBuffer::PerformRedoStep() {
899 const Action
&actionStep
= uh
.GetRedoStep();
900 if (actionStep
.at
== insertAction
) {
901 BasicInsertString(actionStep
.position
, actionStep
.data
, actionStep
.lenData
);
902 } else if (actionStep
.at
== removeAction
) {
903 BasicDeleteChars(actionStep
.position
, actionStep
.lenData
);
905 uh
.CompletedRedoStep();
908 int CellBuffer::SetLineState(int line
, int state
) {
909 lineStates
.EnsureLength(line
+ 1);
910 int stateOld
= lineStates
[line
];
911 lineStates
[line
] = state
;
915 int CellBuffer::GetLineState(int line
) {
916 lineStates
.EnsureLength(line
+ 1);
917 return lineStates
[line
];
920 int CellBuffer::GetMaxLineState() {
921 return lineStates
.Length();
924 int CellBuffer::SetLevel(int line
, int level
) {
925 return lv
.SetLevel(line
, level
);
928 int CellBuffer::GetLevel(int line
) {
929 return lv
.GetLevel(line
);
932 void CellBuffer::ClearLevels() {