]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/CellBuffer.cxx
no real change: divide in groups the wxEvtHandler methods
[wxWidgets.git] / src / stc / scintilla / src / CellBuffer.cxx
1 // Scintilla source code edit control
2 /** @file CellBuffer.cxx
3 ** Manages a buffer of cells.
4 **/
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.
7
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include <stdarg.h>
12
13 #include "Platform.h"
14
15 #include "Scintilla.h"
16 #include "SplitVector.h"
17 #include "Partitioning.h"
18 #include "CellBuffer.h"
19
20 #ifdef SCI_NAMESPACE
21 using namespace Scintilla;
22 #endif
23
24 MarkerHandleSet::MarkerHandleSet() {
25 root = 0;
26 }
27
28 MarkerHandleSet::~MarkerHandleSet() {
29 MarkerHandleNumber *mhn = root;
30 while (mhn) {
31 MarkerHandleNumber *mhnToFree = mhn;
32 mhn = mhn->next;
33 delete mhnToFree;
34 }
35 root = 0;
36 }
37
38 int MarkerHandleSet::Length() const {
39 int c = 0;
40 MarkerHandleNumber *mhn = root;
41 while (mhn) {
42 c++;
43 mhn = mhn->next;
44 }
45 return c;
46 }
47
48 int MarkerHandleSet::NumberFromHandle(int handle) const {
49 MarkerHandleNumber *mhn = root;
50 while (mhn) {
51 if (mhn->handle == handle) {
52 return mhn->number;
53 }
54 mhn = mhn->next;
55 }
56 return - 1;
57 }
58
59 int MarkerHandleSet::MarkValue() const {
60 unsigned int m = 0;
61 MarkerHandleNumber *mhn = root;
62 while (mhn) {
63 m |= (1 << mhn->number);
64 mhn = mhn->next;
65 }
66 return m;
67 }
68
69 bool MarkerHandleSet::Contains(int handle) const {
70 MarkerHandleNumber *mhn = root;
71 while (mhn) {
72 if (mhn->handle == handle) {
73 return true;
74 }
75 mhn = mhn->next;
76 }
77 return false;
78 }
79
80 bool MarkerHandleSet::InsertHandle(int handle, int markerNum) {
81 MarkerHandleNumber *mhn = new MarkerHandleNumber;
82 if (!mhn)
83 return false;
84 mhn->handle = handle;
85 mhn->number = markerNum;
86 mhn->next = root;
87 root = mhn;
88 return true;
89 }
90
91 void MarkerHandleSet::RemoveHandle(int handle) {
92 MarkerHandleNumber **pmhn = &root;
93 while (*pmhn) {
94 MarkerHandleNumber *mhn = *pmhn;
95 if (mhn->handle == handle) {
96 *pmhn = mhn->next;
97 delete mhn;
98 return;
99 }
100 pmhn = &((*pmhn)->next);
101 }
102 }
103
104 bool MarkerHandleSet::RemoveNumber(int markerNum) {
105 bool performedDeletion = false;
106 MarkerHandleNumber **pmhn = &root;
107 while (*pmhn) {
108 MarkerHandleNumber *mhn = *pmhn;
109 if (mhn->number == markerNum) {
110 *pmhn = mhn->next;
111 delete mhn;
112 performedDeletion = true;
113 } else {
114 pmhn = &((*pmhn)->next);
115 }
116 }
117 return performedDeletion;
118 }
119
120 void MarkerHandleSet::CombineWith(MarkerHandleSet *other) {
121 MarkerHandleNumber **pmhn = &root;
122 while (*pmhn) {
123 pmhn = &((*pmhn)->next);
124 }
125 *pmhn = other->root;
126 other->root = 0;
127 }
128
129 LineVector::LineVector() : starts(256) {
130 handleCurrent = 1;
131
132 Init();
133 }
134
135 LineVector::~LineVector() {
136 starts.DeleteAll();
137 for (int line = 0; line < markers.Length(); line++) {
138 delete markers[line];
139 markers[line] = 0;
140 }
141 markers.DeleteAll();
142 levels.DeleteAll();
143 }
144
145 void LineVector::Init() {
146 starts.DeleteAll();
147 for (int line = 0; line < markers.Length(); line++) {
148 delete markers[line];
149 markers[line] = 0;
150 }
151 markers.DeleteAll();
152 levels.DeleteAll();
153 }
154
155 void LineVector::ExpandLevels(int sizeNew) {
156 levels.InsertValue(levels.Length(), sizeNew - levels.Length(), SC_FOLDLEVELBASE);
157 }
158
159 void LineVector::ClearLevels() {
160 levels.DeleteAll();
161 }
162
163 int LineVector::SetLevel(int line, int level) {
164 int prev = 0;
165 if ((line >= 0) && (line < Lines())) {
166 if (!levels.Length()) {
167 ExpandLevels(Lines() + 1);
168 }
169 prev = levels[line];
170 if (prev != level) {
171 levels[line] = level;
172 }
173 }
174 return prev;
175 }
176
177 int LineVector::GetLevel(int line) {
178 if (levels.Length() && (line >= 0) && (line < Lines())) {
179 return levels[line];
180 } else {
181 return SC_FOLDLEVELBASE;
182 }
183 }
184
185 void LineVector::InsertText(int line, int delta) {
186 starts.InsertText(line, delta);
187 }
188
189 void LineVector::InsertLine(int line, int position) {
190 starts.InsertPartition(line, position);
191 if (markers.Length()) {
192 markers.Insert(line, 0);
193 }
194 if (levels.Length()) {
195 int level = SC_FOLDLEVELBASE;
196 if ((line > 0) && (line < Lines())) {
197 level = levels[line-1] & ~SC_FOLDLEVELWHITEFLAG;
198 }
199 levels.InsertValue(line, 1, level);
200 }
201 }
202
203 void LineVector::SetLineStart(int line, int position) {
204 starts.SetPartitionStartPosition(line, position);
205 }
206
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()) {
211 if (line > 0) {
212 MergeMarkers(line - 1);
213 }
214 markers.Delete(line);
215 }
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;
220 levels.Delete(line);
221 if (line > 0)
222 levels[line-1] |= firstHeader;
223 }
224 }
225
226 int LineVector::LineFromPosition(int pos) {
227 return starts.PartitionFromPosition(pos);
228 }
229
230 int LineVector::MarkValue(int line) {
231 if (markers.Length() && markers[line])
232 return markers[line]->MarkValue();
233 else
234 return 0;
235 }
236
237 int LineVector::AddMark(int line, int markerNum) {
238 handleCurrent++;
239 if (!markers.Length()) {
240 // No existing markers so allocate one element per line
241 markers.InsertValue(0, Lines(), 0);
242 }
243 if (!markers[line]) {
244 // Need new structure to hold marker handle
245 markers[line] = new MarkerHandleSet();
246 if (!markers[line])
247 return - 1;
248 }
249 markers[line]->InsertHandle(handleCurrent, markerNum);
250
251 return handleCurrent;
252 }
253
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;
261 }
262 }
263
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;
269 } else {
270 bool performedDeletion = markers[line]->RemoveNumber(markerNum);
271 while (all && performedDeletion) {
272 performedDeletion = markers[line]->RemoveNumber(markerNum);
273 }
274 if (markers[line]->Length() == 0) {
275 delete markers[line];
276 markers[line] = NULL;
277 }
278 }
279 }
280 }
281
282 void LineVector::DeleteMarkFromHandle(int markerHandle) {
283 int line = LineFromHandle(markerHandle);
284 if (line >= 0) {
285 markers[line]->RemoveHandle(markerHandle);
286 if (markers[line]->Length() == 0) {
287 delete markers[line];
288 markers[line] = NULL;
289 }
290 }
291 }
292
293 int LineVector::LineFromHandle(int markerHandle) {
294 if (markers.Length()) {
295 for (int line = 0; line < Lines(); line++) {
296 if (markers[line]) {
297 if (markers[line]->Contains(markerHandle)) {
298 return line;
299 }
300 }
301 }
302 }
303 return -1;
304 }
305
306 Action::Action() {
307 at = startAction;
308 position = 0;
309 data = 0;
310 lenData = 0;
311 }
312
313 Action::~Action() {
314 Destroy();
315 }
316
317 void Action::Create(actionType at_, int position_, char *data_, int lenData_, bool mayCoalesce_) {
318 delete []data;
319 position = position_;
320 at = at_;
321 data = data_;
322 lenData = lenData_;
323 mayCoalesce = mayCoalesce_;
324 }
325
326 void Action::Destroy() {
327 delete []data;
328 data = 0;
329 }
330
331 void Action::Grab(Action *source) {
332 delete []data;
333
334 position = source->position;
335 at = source->at;
336 data = source->data;
337 lenData = source->lenData;
338 mayCoalesce = source->mayCoalesce;
339
340 // Ownership of source data transferred to this
341 source->position = 0;
342 source->at = startAction;
343 source->data = 0;
344 source->lenData = 0;
345 source->mayCoalesce = true;
346 }
347
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.
365
366 UndoHistory::UndoHistory() {
367
368 lenActions = 100;
369 actions = new Action[lenActions];
370 maxAction = 0;
371 currentAction = 0;
372 undoSequenceDepth = 0;
373 savePoint = 0;
374
375 actions[currentAction].Create(startAction);
376 }
377
378 UndoHistory::~UndoHistory() {
379 delete []actions;
380 actions = 0;
381 }
382
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];
390 if (!actionsNew)
391 return;
392 for (int act = 0; act <= currentAction; act++)
393 actionsNew[act].Grab(&actions[act]);
394 delete []actions;
395 lenActions = lenActionsNew;
396 actions = actionsNew;
397 }
398 }
399
400 void UndoHistory::AppendAction(actionType at, int position, char *data, int lengthData,
401 bool &startSequence) {
402 EnsureUndoRoom();
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) {
407 savePoint = -1;
408 }
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) {
417 currentAction++;
418 } else if (currentAction == savePoint) {
419 currentAction++;
420 } else if ((at == insertAction) &&
421 (position != (actPrevious.position + actPrevious.lenData))) {
422 // Insertions must be immediately after to coalesce
423 currentAction++;
424 } else if (!actions[currentAction].mayCoalesce) {
425 // Not allowed to coalesce if this set
426 currentAction++;
427 } else if (at == removeAction) {
428 if ((lengthData == 1) || (lengthData == 2)){
429 if ((position + lengthData) == actPrevious.position) {
430 ; // Backspace -> OK
431 } else if (position == actPrevious.position) {
432 ; // Delete -> OK
433 } else {
434 // Removals must be at same position to coalesce
435 currentAction++;
436 }
437 } else {
438 // Removals must be of one character to coalesce
439 currentAction++;
440 }
441 } else {
442 // Action coalesced.
443 }
444
445 } else {
446 // Actions not at top level are always coalesced unless this is after return to top level
447 if (!actions[currentAction].mayCoalesce)
448 currentAction++;
449 }
450 } else {
451 currentAction++;
452 }
453 startSequence = oldCurrentAction != currentAction;
454 actions[currentAction].Create(at, position, data, lengthData);
455 currentAction++;
456 actions[currentAction].Create(startAction);
457 maxAction = currentAction;
458 }
459
460 void UndoHistory::BeginUndoAction() {
461 EnsureUndoRoom();
462 if (undoSequenceDepth == 0) {
463 if (actions[currentAction].at != startAction) {
464 currentAction++;
465 actions[currentAction].Create(startAction);
466 maxAction = currentAction;
467 }
468 actions[currentAction].mayCoalesce = false;
469 }
470 undoSequenceDepth++;
471 }
472
473 void UndoHistory::EndUndoAction() {
474 PLATFORM_ASSERT(undoSequenceDepth > 0);
475 EnsureUndoRoom();
476 undoSequenceDepth--;
477 if (0 == undoSequenceDepth) {
478 if (actions[currentAction].at != startAction) {
479 currentAction++;
480 actions[currentAction].Create(startAction);
481 maxAction = currentAction;
482 }
483 actions[currentAction].mayCoalesce = false;
484 }
485 }
486
487 void UndoHistory::DropUndoSequence() {
488 undoSequenceDepth = 0;
489 }
490
491 void UndoHistory::DeleteUndoHistory() {
492 for (int i = 1; i < maxAction; i++)
493 actions[i].Destroy();
494 maxAction = 0;
495 currentAction = 0;
496 actions[currentAction].Create(startAction);
497 savePoint = 0;
498 }
499
500 void UndoHistory::SetSavePoint() {
501 savePoint = currentAction;
502 }
503
504 bool UndoHistory::IsSavePoint() const {
505 return savePoint == currentAction;
506 }
507
508 bool UndoHistory::CanUndo() const {
509 return (currentAction > 0) && (maxAction > 0);
510 }
511
512 int UndoHistory::StartUndo() {
513 // Drop any trailing startAction
514 if (actions[currentAction].at == startAction && currentAction > 0)
515 currentAction--;
516
517 // Count the steps in this action
518 int act = currentAction;
519 while (actions[act].at != startAction && act > 0) {
520 act--;
521 }
522 return currentAction - act;
523 }
524
525 const Action &UndoHistory::GetUndoStep() const {
526 return actions[currentAction];
527 }
528
529 void UndoHistory::CompletedUndoStep() {
530 currentAction--;
531 }
532
533 bool UndoHistory::CanRedo() const {
534 return maxAction > currentAction;
535 }
536
537 int UndoHistory::StartRedo() {
538 // Drop any leading startAction
539 if (actions[currentAction].at == startAction && currentAction < maxAction)
540 currentAction++;
541
542 // Count the steps in this action
543 int act = currentAction;
544 while (actions[act].at != startAction && act < maxAction) {
545 act++;
546 }
547 return act - currentAction;
548 }
549
550 const Action &UndoHistory::GetRedoStep() const {
551 return actions[currentAction];
552 }
553
554 void UndoHistory::CompletedRedoStep() {
555 currentAction++;
556 }
557
558 CellBuffer::CellBuffer() {
559 readOnly = false;
560 collectingUndo = true;
561 }
562
563 CellBuffer::~CellBuffer() {
564 }
565
566 char CellBuffer::CharAt(int position) const {
567 return substance.ValueAt(position);
568 }
569
570 void CellBuffer::GetCharRange(char *buffer, int position, int lengthRetrieve) {
571 if (lengthRetrieve < 0)
572 return;
573 if (position < 0)
574 return;
575 if ((position + lengthRetrieve) > substance.Length()) {
576 Platform::DebugPrintf("Bad GetCharRange %d for %d of %d\n", position,
577 lengthRetrieve, substance.Length());
578 return;
579 }
580
581 for (int i=0; i<lengthRetrieve; i++) {
582 *buffer++ = substance.ValueAt(position + i);
583 }
584 }
585
586 char CellBuffer::StyleAt(int position) {
587 return style.ValueAt(position);
588 }
589
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) {
592 char *data = 0;
593 // InsertString and DeleteChars are the bottleneck though which all changes occur
594 if (!readOnly) {
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++) {
600 data[i] = s[i];
601 }
602 uh.AppendAction(insertAction, position, data, insertLength, startSequence);
603 }
604
605 BasicInsertString(position, s, insertLength);
606 }
607 return data;
608 }
609
610 bool CellBuffer::SetStyleAt(int position, char styleValue, char mask) {
611 styleValue &= mask;
612 char curVal = style.ValueAt(position);
613 if ((curVal & mask) != styleValue) {
614 style.SetValueAt(position, static_cast<char>((curVal & ~mask) | styleValue));
615 return true;
616 } else {
617 return false;
618 }
619 }
620
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));
629 changed = true;
630 }
631 position++;
632 }
633 return changed;
634 }
635
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);
640 char *data = 0;
641 if (!readOnly) {
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);
647 }
648 uh.AppendAction(removeAction, position, data, deleteLength, startSequence);
649 }
650
651 BasicDeleteChars(position, deleteLength);
652 }
653 return data;
654 }
655
656 int CellBuffer::Length() const {
657 return substance.Length();
658 }
659
660 void CellBuffer::Allocate(int newSize) {
661 substance.ReAllocate(newSize);
662 style.ReAllocate(newSize);
663 }
664
665 int CellBuffer::Lines() const {
666 return lv.Lines();
667 }
668
669 int CellBuffer::LineStart(int line) const {
670 if (line < 0)
671 return 0;
672 else if (line >= Lines())
673 return Length();
674 else
675 return lv.LineStart(line);
676 }
677
678 bool CellBuffer::IsReadOnly() {
679 return readOnly;
680 }
681
682 void CellBuffer::SetReadOnly(bool set) {
683 readOnly = set;
684 }
685
686 void CellBuffer::SetSavePoint() {
687 uh.SetSavePoint();
688 }
689
690 bool CellBuffer::IsSavePoint() {
691 return uh.IsSavePoint();
692 }
693
694 int CellBuffer::AddMark(int line, int markerNum) {
695 if ((line >= 0) && (line < Lines())) {
696 return lv.AddMark(line, markerNum);
697 }
698 return - 1;
699 }
700
701 void CellBuffer::DeleteMark(int line, int markerNum) {
702 if ((line >= 0) && (line < Lines())) {
703 lv.DeleteMark(line, markerNum, false);
704 }
705 }
706
707 void CellBuffer::DeleteMarkFromHandle(int markerHandle) {
708 lv.DeleteMarkFromHandle(markerHandle);
709 }
710
711 int CellBuffer::GetMark(int line) {
712 if ((line >= 0) && (line < Lines()))
713 return lv.MarkValue(line);
714 return 0;
715 }
716
717 void CellBuffer::DeleteAllMarks(int markerNum) {
718 for (int line = 0; line < Lines(); line++) {
719 lv.DeleteMark(line, markerNum, true);
720 }
721 }
722
723 int CellBuffer::LineFromHandle(int markerHandle) {
724 return lv.LineFromHandle(markerHandle);
725 }
726
727 // Without undo
728
729 void CellBuffer::InsertLine(int line, int position) {
730 lv.InsertLine(line, position);
731 if (lineStates.Length()) {
732 lineStates.Insert(line, 0);
733 }
734 }
735
736 void CellBuffer::RemoveLine(int line) {
737 lv.RemoveLine(line);
738 if (lineStates.Length()) {
739 lineStates.Delete(line);
740 }
741 }
742
743 void CellBuffer::BasicInsertString(int position, const char *s, int insertLength) {
744 if (insertLength == 0)
745 return;
746 PLATFORM_ASSERT(insertLength > 0);
747
748 substance.InsertFromArray(position, s, 0, insertLength);
749 style.InsertValue(position, insertLength, 0);
750
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);
759 lineInsert++;
760 }
761 char ch = ' ';
762 for (int i = 0; i < insertLength; i++) {
763 ch = s[i];
764 if (ch == '\r') {
765 InsertLine(lineInsert, (position + i) + 1);
766 lineInsert++;
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);
771 } else {
772 InsertLine(lineInsert, (position + i) + 1);
773 lineInsert++;
774 }
775 }
776 chPrev = ch;
777 }
778 // Joining two lines where last insertion is cr and following substance starts with lf
779 if (chAfter == '\n') {
780 if (ch == '\r') {
781 // End of line already in buffer so drop the newly created one
782 RemoveLine(lineInsert - 1);
783 }
784 }
785 }
786
787 void CellBuffer::BasicDeleteChars(int position, int deleteLength) {
788 if (deleteLength == 0)
789 return;
790
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.
794 lv.Init();
795 } else {
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
798
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') {
806 // Move back one
807 lv.SetLineStart(lineRemove, position);
808 lineRemove++;
809 ignoreNL = true; // First \n is not real deletion
810 }
811
812 char ch = chNext;
813 for (int i = 0; i < deleteLength; i++) {
814 chNext = substance.ValueAt(position + i + 1);
815 if (ch == '\r') {
816 if (chNext != '\n') {
817 RemoveLine(lineRemove);
818 }
819 } else if (ch == '\n') {
820 if (ignoreNL) {
821 ignoreNL = false; // Further \n are real deletions
822 } else {
823 RemoveLine(lineRemove);
824 }
825 }
826
827 ch = chNext;
828 }
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);
836 }
837 }
838 substance.DeleteRange(position, deleteLength);
839 style.DeleteRange(position, deleteLength);
840 }
841
842 bool CellBuffer::SetUndoCollection(bool collectUndo) {
843 collectingUndo = collectUndo;
844 uh.DropUndoSequence();
845 return collectingUndo;
846 }
847
848 bool CellBuffer::IsCollectingUndo() {
849 return collectingUndo;
850 }
851
852 void CellBuffer::BeginUndoAction() {
853 uh.BeginUndoAction();
854 }
855
856 void CellBuffer::EndUndoAction() {
857 uh.EndUndoAction();
858 }
859
860 void CellBuffer::DeleteUndoHistory() {
861 uh.DeleteUndoHistory();
862 }
863
864 bool CellBuffer::CanUndo() {
865 return uh.CanUndo();
866 }
867
868 int CellBuffer::StartUndo() {
869 return uh.StartUndo();
870 }
871
872 const Action &CellBuffer::GetUndoStep() const {
873 return uh.GetUndoStep();
874 }
875
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);
882 }
883 uh.CompletedUndoStep();
884 }
885
886 bool CellBuffer::CanRedo() {
887 return uh.CanRedo();
888 }
889
890 int CellBuffer::StartRedo() {
891 return uh.StartRedo();
892 }
893
894 const Action &CellBuffer::GetRedoStep() const {
895 return uh.GetRedoStep();
896 }
897
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);
904 }
905 uh.CompletedRedoStep();
906 }
907
908 int CellBuffer::SetLineState(int line, int state) {
909 lineStates.EnsureLength(line + 1);
910 int stateOld = lineStates[line];
911 lineStates[line] = state;
912 return stateOld;
913 }
914
915 int CellBuffer::GetLineState(int line) {
916 lineStates.EnsureLength(line + 1);
917 return lineStates[line];
918 }
919
920 int CellBuffer::GetMaxLineState() {
921 return lineStates.Length();
922 }
923
924 int CellBuffer::SetLevel(int line, int level) {
925 return lv.SetLevel(line, level);
926 }
927
928 int CellBuffer::GetLevel(int line) {
929 return lv.GetLevel(line);
930 }
931
932 void CellBuffer::ClearLevels() {
933 lv.ClearLevels();
934 }