]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/CellBuffer.cxx
777688511a657a67ed0414993565cb9482f899f0
[wxWidgets.git] / src / stc / scintilla / src / CellBuffer.cxx
1 // Scintilla source code edit control
2 // CellBuffer.cxx - manages a buffer of cells
3 // Copyright 1998-2000 by Neil Hodgson <neilh@scintilla.org>
4 // The License.txt file describes the conditions under which this software may be distributed.
5
6 #include <stdio.h>
7 #include <string.h>
8 #include <stdlib.h>
9 #include <stdarg.h>
10
11 #include "Platform.h"
12
13 #include "Scintilla.h"
14 #include "SVector.h"
15 #include "CellBuffer.h"
16
17 MarkerHandleSet::MarkerHandleSet() {
18 root = 0;
19 }
20
21 MarkerHandleSet::~MarkerHandleSet() {
22 MarkerHandleNumber *mhn = root;
23 while (mhn) {
24 MarkerHandleNumber *mhnToFree = mhn;
25 mhn = mhn->next;
26 delete mhnToFree;
27 }
28 root = 0;
29 }
30
31 int MarkerHandleSet::Length() {
32 int c = 0;
33 MarkerHandleNumber *mhn = root;
34 while (mhn) {
35 c++;
36 mhn = mhn->next;
37 }
38 return c;
39 }
40
41 int MarkerHandleSet::NumberFromHandle(int handle) {
42 MarkerHandleNumber *mhn = root;
43 while (mhn) {
44 if (mhn->handle == handle) {
45 return mhn->number;
46 }
47 mhn = mhn->next;
48 }
49 return - 1;
50 }
51
52 int MarkerHandleSet::MarkValue() {
53 unsigned int m = 0;
54 MarkerHandleNumber *mhn = root;
55 while (mhn) {
56 m |= (1 << mhn->number);
57 mhn = mhn->next;
58 }
59 return m;
60 }
61
62 bool MarkerHandleSet::Contains(int handle) {
63 MarkerHandleNumber *mhn = root;
64 while (mhn) {
65 if (mhn->handle == handle) {
66 return true;
67 }
68 mhn = mhn->next;
69 }
70 return false;
71 }
72
73 bool MarkerHandleSet::InsertHandle(int handle, int markerNum) {
74 MarkerHandleNumber *mhn = new MarkerHandleNumber;
75 if (!mhn)
76 return false;
77 mhn->handle = handle;
78 mhn->number = markerNum;
79 mhn->next = root;
80 root = mhn;
81 return true;
82 }
83
84 void MarkerHandleSet::RemoveHandle(int handle) {
85 MarkerHandleNumber **pmhn = &root;
86 while (*pmhn) {
87 MarkerHandleNumber *mhn = *pmhn;
88 if (mhn->handle == handle) {
89 *pmhn = mhn->next;
90 delete mhn;
91 return;
92 }
93 pmhn = &((*pmhn)->next);
94 }
95 }
96
97 void MarkerHandleSet::RemoveNumber(int markerNum) {
98 MarkerHandleNumber **pmhn = &root;
99 while (*pmhn) {
100 MarkerHandleNumber *mhn = *pmhn;
101 if (mhn->number == markerNum) {
102 *pmhn = mhn->next;
103 delete mhn;
104 return;
105 }
106 pmhn = &((*pmhn)->next);
107 }
108 }
109
110 void MarkerHandleSet::CombineWith(MarkerHandleSet *other) {
111 MarkerHandleNumber **pmhn = &root;
112 while (*pmhn) {
113 pmhn = &((*pmhn)->next);
114 }
115 *pmhn = other->root;
116 other->root = 0;
117 }
118
119 LineVector::LineVector() {
120 linesData = 0;
121 lines = 0;
122 levels = 0;
123 Init();
124 }
125
126 LineVector::~LineVector() {
127 for (int line = 0; line < lines; line++) {
128 delete linesData[line].handleSet;
129 linesData[line].handleSet = 0;
130 }
131 delete []linesData;
132 linesData = 0;
133 delete []levels;
134 levels = 0;
135 }
136
137 void LineVector::Init() {
138 for (int line = 0; line < lines; line++) {
139 delete linesData[line].handleSet;
140 linesData[line].handleSet = 0;
141 }
142 delete []linesData;
143 linesData = new LineData[static_cast<int>(growSize)];
144 size = growSize;
145 lines = 1;
146 delete []levels;
147 levels = 0;
148 sizeLevels = 0;
149 }
150
151 void LineVector::Expand(int sizeNew) {
152 LineData *linesDataNew = new LineData[sizeNew];
153 if (linesDataNew) {
154 for (int i = 0; i < size; i++)
155 linesDataNew[i] = linesData[i];
156 // Do not delete handleSets here as they are transferred to new linesData
157 delete []linesData;
158 linesData = linesDataNew;
159 size = sizeNew;
160 } else {
161 Platform::DebugPrintf("No memory available\n");
162 // TODO: Blow up
163 }
164 }
165
166 void LineVector::ExpandLevels(int sizeNew) {
167 if (sizeNew == -1)
168 sizeNew = size;
169 int *levelsNew = new int[sizeNew];
170 if (levelsNew) {
171 int i = 0;
172 for (; i < sizeLevels; i++)
173 levelsNew[i] = levels[i];
174 for (; i < sizeNew; i++)
175 levelsNew[i] = SC_FOLDLEVELBASE;
176 delete []levels;
177 levels = levelsNew;
178 sizeLevels = sizeNew;
179 } else {
180 Platform::DebugPrintf("No memory available\n");
181 // TODO: Blow up
182 }
183 }
184
185 void LineVector::InsertValue(int pos, int value) {
186 //Platform::DebugPrintf("InsertValue[%d] = %d\n", pos, value);
187 if ((lines + 2) >= size) {
188 Expand(size + growSize);
189 if (levels) {
190 ExpandLevels(size + growSize);
191 }
192 }
193 lines++;
194 for (int i = lines + 1; i > pos; i--) {
195 linesData[i] = linesData[i - 1];
196 }
197 linesData[pos].startPosition = value;
198 linesData[pos].handleSet = 0;
199 }
200
201 void LineVector::SetValue(int pos, int value) {
202 //Platform::DebugPrintf("SetValue[%d] = %d\n", pos, value);
203 if ((pos + 2) >= size) {
204 //Platform::DebugPrintf("Resize %d %d\n", size,pos);
205 Expand(pos + growSize);
206 //Platform::DebugPrintf("end Resize %d %d\n", size,pos);
207 lines = pos;
208 if (levels) {
209 ExpandLevels(pos + growSize);
210 }
211 }
212 linesData[pos].startPosition = value;
213 }
214
215 void LineVector::Remove(int pos) {
216 //Platform::DebugPrintf("Remove %d\n", pos);
217 // Retain the markers from the deleted line by oring them into the previous line
218 if (pos > 0) {
219 MergeMarkers(pos - 1);
220 }
221 for (int i = pos; i < lines; i++) {
222 linesData[i] = linesData[i + 1];
223 }
224 lines--;
225 }
226
227 int LineVector::LineFromPosition(int pos) {
228 //Platform::DebugPrintf("LineFromPostion %d lines=%d end = %d\n", pos, lines, linesData[lines].startPosition);
229 if (lines == 0)
230 return 0;
231 //Platform::DebugPrintf("LineFromPosition %d\n", pos);
232 if (pos >= linesData[lines].startPosition)
233 return lines - 1;
234 int lower = 0;
235 int upper = lines;
236 int middle = 0;
237 do {
238 middle = (upper + lower + 1) / 2; // Round high
239 if (pos < linesData[middle].startPosition) {
240 upper = middle - 1;
241 } else {
242 lower = middle;
243 }
244 } while (lower < upper);
245 //Platform::DebugPrintf("LineFromPostion %d %d %d\n", pos, lower, linesData[lower].startPosition, linesData[lower > 1 ? lower - 1 : 0].startPosition);
246 return lower;
247 }
248
249 int LineVector::AddMark(int line, int markerNum) {
250 handleCurrent++;
251 if (!linesData[line].handleSet) {
252 // Need new structure to hold marker handle
253 linesData[line].handleSet = new MarkerHandleSet;
254 if (!linesData[line].handleSet)
255 return - 1;
256 }
257 linesData[line].handleSet->InsertHandle(handleCurrent, markerNum);
258
259 return handleCurrent;
260 }
261
262 void LineVector::MergeMarkers(int pos) {
263 if (linesData[pos].handleSet || linesData[pos + 1].handleSet) {
264 if (linesData[pos].handleSet && linesData[pos + 1].handleSet) {
265 linesData[pos].handleSet->CombineWith(linesData[pos].handleSet);
266 linesData[pos].handleSet = 0;
267 }
268 }
269 }
270
271 void LineVector::DeleteMark(int line, int markerNum) {
272 if (linesData[line].handleSet) {
273 if (markerNum == -1) {
274 delete linesData[line].handleSet;
275 linesData[line].handleSet = 0;
276 } else {
277 linesData[line].handleSet->RemoveNumber(markerNum);
278 if (linesData[line].handleSet->Length() == 0) {
279 delete linesData[line].handleSet;
280 linesData[line].handleSet = 0;
281 }
282 }
283 }
284 }
285
286 void LineVector::DeleteMarkFromHandle(int markerHandle) {
287 int line = LineFromHandle(markerHandle);
288 if (line >= 0) {
289 linesData[line].handleSet->RemoveHandle(markerHandle);
290 if (linesData[line].handleSet->Length() == 0) {
291 delete linesData[line].handleSet;
292 linesData[line].handleSet = 0;
293 }
294 }
295 }
296
297 int LineVector::LineFromHandle(int markerHandle) {
298 for (int line = 0; line < lines; line++) {
299 if (linesData[line].handleSet) {
300 if (linesData[line].handleSet->Contains(markerHandle)) {
301 return line;
302 }
303 }
304 }
305 return - 1;
306 }
307
308 Action::Action() {
309 at = startAction;
310 position = 0;
311 data = 0;
312 lenData = 0;
313 }
314
315 Action::~Action() {
316 Destroy();
317 }
318
319 void Action::Create(actionType at_, int position_, char *data_, int lenData_) {
320 delete []data;
321 position = position_;
322 at = at_;
323 data = data_;
324 lenData = lenData_;
325 }
326
327 void Action::Destroy() {
328 delete []data;
329 data = 0;
330 }
331
332 void Action::Grab(Action *source) {
333 delete []data;
334
335 position = source->position;
336 at = source->at;
337 data = source->data;
338 lenData = source->lenData;
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 }
346
347 CellBuffer::CellBuffer(int initialLength) {
348 body = new char[initialLength];
349 size = initialLength;
350 length = 0;
351 part1len = 0;
352 gaplen = initialLength;
353 part2body = body + gaplen;
354 readOnly = false;
355
356 lenActions = 100;
357 actions = new Action[lenActions];
358 maxAction = 0;
359 currentAction = 0;
360 collectingUndo = undoCollectAutoStart;
361 undoSequenceDepth = 0;
362 savePoint = 0;
363
364 actions[currentAction].Create(startAction);
365 }
366
367 CellBuffer::~CellBuffer() {
368 delete []body;
369 body = 0;
370 delete []actions;
371 actions = 0;
372 }
373
374 void CellBuffer::GapTo(int position) {
375 if (position == part1len)
376 return;
377 if (position < part1len) {
378 int diff = part1len - position;
379 //Platform::DebugPrintf("Move gap backwards to %d diff = %d part1len=%d length=%d \n", position,diff, part1len, length);
380 for (int i = 0; i < diff; i++)
381 body[part1len + gaplen - i - 1] = body[part1len - i - 1];
382 } else { // position > part1len
383 int diff = position - part1len;
384 //Platform::DebugPrintf("Move gap forwards to %d diff =%d\n", position,diff);
385 for (int i = 0; i < diff; i++)
386 body[part1len + i] = body[part1len + gaplen + i];
387 }
388 part1len = position;
389 part2body = body + gaplen;
390 }
391
392 void CellBuffer::RoomFor(int insertionLength) {
393 //Platform::DebugPrintf("need room %d %d\n", gaplen, insertionLength);
394 if (gaplen <= insertionLength) {
395 //Platform::DebugPrintf("need room %d %d\n", gaplen, insertionLength);
396 GapTo(length);
397 int newSize = size + insertionLength + 4000;
398 //Platform::DebugPrintf("moved gap %d\n", newSize);
399 char *newBody = new char[newSize];
400 memcpy(newBody, body, size);
401 delete []body;
402 body = newBody;
403 gaplen += newSize - size;
404 part2body = body + gaplen;
405 size = newSize;
406 //Platform::DebugPrintf("end need room %d %d - size=%d length=%d\n", gaplen, insertionLength,size,length);
407 }
408 }
409
410 // To make it easier to write code that uses ByteAt, a position outside the range of the buffer
411 // can be retrieved. All characters outside the range have the value '\0'.
412 char CellBuffer::ByteAt(int position) {
413 if (position < part1len) {
414 if (position < 0) {
415 return '\0';
416 } else {
417 return body[position];
418 }
419 } else {
420 if (position >= length) {
421 return '\0';
422 } else {
423 return part2body[position];
424 }
425 }
426 }
427
428 void CellBuffer::SetByteAt(int position, char ch) {
429
430 if (position < 0) {
431 //Platform::DebugPrintf("Bad position %d\n",position);
432 return;
433 }
434 if (position >= length + 11) {
435 Platform::DebugPrintf("Very Bad position %d of %d\n", position, length);
436 //exit(2);
437 return;
438 }
439 if (position >= length) {
440 //Platform::DebugPrintf("Bad position %d of %d\n",position,length);
441 return;
442 }
443
444 if (position < part1len) {
445 body[position] = ch;
446 } else {
447 part2body[position] = ch;
448 }
449 }
450
451 char CellBuffer::CharAt(int position) {
452 return ByteAt(position*2);
453 }
454
455 void CellBuffer::GetCharRange(char *buffer, int position, int lengthRetrieve) {
456 if (lengthRetrieve < 0)
457 return;
458 if (position < 0)
459 return;
460 int bytePos = position * 2;
461 if ((bytePos + lengthRetrieve * 2) > length) {
462 Platform::DebugPrintf("Bad GetCharRange %d for %d of %d\n",bytePos,
463 lengthRetrieve, length);
464 return;
465 }
466 GapTo(0); // Move the buffer so its easy to subscript into it
467 char *pb = part2body + bytePos;
468 while (lengthRetrieve--) {
469 *buffer++ = *pb;
470 pb +=2;
471 }
472 }
473
474 char CellBuffer::StyleAt(int position) {
475 return ByteAt(position*2 + 1);
476 }
477
478 const char *CellBuffer::InsertString(int position, char *s, int insertLength) {
479 char *data = 0;
480 // InsertString and DeleteChars are the bottleneck though which all changes occur
481 if (!readOnly) {
482 if (collectingUndo) {
483 // Save into the undo/redo stack, but only the characters - not the formatting
484 // This takes up about half load time
485 data = new char[insertLength / 2];
486 for (int i = 0; i < insertLength / 2; i++) {
487 data[i] = s[i * 2];
488 }
489 AppendAction(insertAction, position, data, insertLength / 2);
490 }
491
492 BasicInsertString(position, s, insertLength);
493 }
494 return data;
495 }
496
497 void CellBuffer::InsertCharStyle(int position, char ch, char style) {
498 char s[2];
499 s[0] = ch;
500 s[1] = style;
501 InsertString(position*2, s, 2);
502 }
503
504 bool CellBuffer::SetStyleAt(int position, char style, char mask) {
505 char curVal = ByteAt(position*2 + 1);
506 if ((curVal & mask) != style) {
507 SetByteAt(position*2 + 1, (curVal & ~mask) | style);
508 return true;
509 } else {
510 return false;
511 }
512 }
513
514 bool CellBuffer::SetStyleFor(int position, int lengthStyle, char style, char mask) {
515 int bytePos = position * 2 + 1;
516 bool changed = false;
517 while (lengthStyle--) {
518 char curVal = ByteAt(bytePos);
519 if ((curVal & mask) != style) {
520 SetByteAt(bytePos, (curVal & ~mask) | style);
521 changed = true;
522 }
523 bytePos += 2;
524 }
525 return changed;
526 }
527
528 void CellBuffer::EnsureUndoRoom() {
529 //Platform::DebugPrintf("%% %d action %d %d %d\n", at, position, length, currentAction);
530 if (currentAction >= 2) {
531 // Have to test that there is room for 2 more actions in the array
532 // as two actions may be created by this function
533 if (currentAction >= (lenActions - 2)) {
534 // Run out of undo nodes so extend the array
535 int lenActionsNew = lenActions * 2;
536 Action *actionsNew = new Action[lenActionsNew];
537 if (!actionsNew)
538 return;
539 for (int act = 0; act <= currentAction; act++)
540 actionsNew[act].Grab(&actions[act]);
541 delete []actions;
542 lenActions = lenActionsNew;
543 actions = actionsNew;
544 }
545 }
546 }
547
548 void CellBuffer::AppendAction(actionType at, int position, char *data, int lengthData) {
549 EnsureUndoRoom();
550 //Platform::DebugPrintf("%% %d action %d %d %d\n", at, position, lengthData, currentAction);
551 if (currentAction >= 2) {
552 // See if current action can be coalesced into previous action
553 // Will work if both are inserts or deletes and position is same or two different
554 if ((at != actions[currentAction - 1].at) || (abs(position - actions[currentAction - 1].position) > 2)) {
555 currentAction++;
556 } else if (currentAction == savePoint) {
557 currentAction++;
558 }
559 } else {
560 currentAction++;
561 }
562 actions[currentAction].Create(at, position, data, lengthData);
563 if ((collectingUndo == undoCollectAutoStart) && (0 == undoSequenceDepth)) {
564 currentAction++;
565 actions[currentAction].Create(startAction);
566 }
567 maxAction = currentAction;
568 }
569
570 const char *CellBuffer::DeleteChars(int position, int deleteLength) {
571 // InsertString and DeleteChars are the bottleneck though which all changes occur
572 char *data = 0;
573 if (!readOnly) {
574 if (collectingUndo) {
575 // Save into the undo/redo stack, but only the characters - not the formatting
576 data = new char[deleteLength / 2];
577 for (int i = 0; i < deleteLength / 2; i++) {
578 data[i] = ByteAt(position + i * 2);
579 }
580 AppendAction(removeAction, position, data, deleteLength / 2);
581 }
582
583 BasicDeleteChars(position, deleteLength);
584 }
585 return data;
586 }
587
588 int CellBuffer::ByteLength() {
589 return length;
590 }
591
592 int CellBuffer::Length() {
593 return ByteLength() / 2;
594 }
595
596 int CellBuffer::Lines() {
597 //Platform::DebugPrintf("Lines = %d\n", lv.lines);
598 return lv.lines;
599 }
600
601 int CellBuffer::LineStart(int line) {
602 if (line < 0)
603 return 0;
604 else if (line > lv.lines)
605 return length;
606 else
607 return lv.linesData[line].startPosition;
608 }
609
610 bool CellBuffer::IsReadOnly() {
611 return readOnly;
612 }
613
614 void CellBuffer::SetReadOnly(bool set) {
615 readOnly = set;
616 }
617
618 void CellBuffer::SetSavePoint() {
619 savePoint = currentAction;
620 }
621
622 bool CellBuffer::IsSavePoint() {
623 return savePoint == currentAction;
624 }
625
626 int CellBuffer::AddMark(int line, int markerNum) {
627 if ((line >= 0) && (line < lv.lines)) {
628 return lv.AddMark(line, markerNum);
629 }
630 return - 1;
631 }
632
633 void CellBuffer::DeleteMark(int line, int markerNum) {
634 if ((line >= 0) && (line < lv.lines)) {
635 lv.DeleteMark(line, markerNum);
636 }
637 }
638
639 void CellBuffer::DeleteMarkFromHandle(int markerHandle) {
640 lv.DeleteMarkFromHandle(markerHandle);
641 }
642
643 int CellBuffer::GetMark(int line) {
644 if ((line >= 0) && (line < lv.lines) && (lv.linesData[line].handleSet))
645 return lv.linesData[line].handleSet->MarkValue();
646 return 0;
647 }
648
649 void CellBuffer::DeleteAllMarks(int markerNum) {
650 for (int line = 0; line < lv.lines; line++) {
651 lv.DeleteMark(line, markerNum);
652 }
653 }
654
655 int CellBuffer::LineFromHandle(int markerHandle) {
656 return lv.LineFromHandle(markerHandle);
657 }
658
659 // Without undo
660
661 void CellBuffer::BasicInsertString(int position, char *s, int insertLength) {
662 //Platform::DebugPrintf("Inserting at %d for %d\n", position, insertLength);
663 if (insertLength == 0)
664 return;
665 RoomFor(insertLength);
666 GapTo(position);
667
668 memcpy(body + part1len, s, insertLength);
669 length += insertLength;
670 part1len += insertLength;
671 gaplen -= insertLength;
672 part2body = body + gaplen;
673
674 int lineInsert = lv.LineFromPosition(position / 2) + 1;
675 // Point all the lines after the insertion point further along in the buffer
676 for (int lineAfter = lineInsert; lineAfter <= lv.lines; lineAfter++) {
677 lv.linesData[lineAfter].startPosition += insertLength / 2;
678 }
679 char chPrev = ' ';
680 if ((position - 2) >= 0)
681 chPrev = ByteAt(position - 2);
682 char chAfter = ' ';
683 if ((position + insertLength) < length)
684 chAfter = ByteAt(position + insertLength);
685 if (chPrev == '\r' && chAfter == '\n') {
686 //Platform::DebugPrintf("Splitting a crlf pair at %d\n", lineInsert);
687 // Splitting up a crlf pair at position
688 lv.InsertValue(lineInsert, position / 2);
689 lineInsert++;
690 }
691 char ch = ' ';
692 for (int i = 0; i < insertLength; i += 2) {
693 ch = s[i];
694 if (ch == '\r') {
695 //Platform::DebugPrintf("Inserting cr at %d\n", lineInsert);
696 lv.InsertValue(lineInsert, (position + i) / 2 + 1);
697 lineInsert++;
698 } else if (ch == '\n') {
699 if (chPrev == '\r') {
700 //Platform::DebugPrintf("Patching cr before lf at %d\n", lineInsert-1);
701 // Patch up what was end of line
702 lv.SetValue(lineInsert - 1, (position + i) / 2 + 1);
703 } else {
704 //Platform::DebugPrintf("Inserting lf at %d\n", lineInsert);
705 lv.InsertValue(lineInsert, (position + i) / 2 + 1);
706 lineInsert++;
707 }
708 }
709 chPrev = ch;
710 }
711 // Joining two lines where last insertion is cr and following text starts with lf
712 if (chAfter == '\n') {
713 if (ch == '\r') {
714 //Platform::DebugPrintf("Joining cr before lf at %d\n", lineInsert-1);
715 // End of line already in buffer so drop the newly created one
716 lv.Remove(lineInsert - 1);
717 }
718 }
719 }
720
721 void CellBuffer::BasicDeleteChars(int position, int deleteLength) {
722 //Platform::DebugPrintf("Deleting at %d for %d\n", position, deleteLength);
723 if (deleteLength == 0)
724 return;
725
726 if ((position == 0) && (deleteLength == length)) {
727 // If whole buffer is being deleted, faster to reinitialise lines data
728 // than to delete each line.
729 //printf("Whole buffer being deleted\n");
730 lv.Init();
731 } else {
732 // Have to fix up line positions before doing deletion as looking at text in buffer
733 // to work out which lines have been removed
734
735 int lineRemove = lv.LineFromPosition(position / 2) + 1;
736 // Point all the lines after the insertion point further along in the buffer
737 for (int lineAfter = lineRemove; lineAfter <= lv.lines; lineAfter++) {
738 lv.linesData[lineAfter].startPosition -= deleteLength / 2;
739 }
740 char chPrev = ' ';
741 if (position >= 2)
742 chPrev = ByteAt(position - 2);
743 char chBefore = chPrev;
744 char chNext = ' ';
745 if (position < length)
746 chNext = ByteAt(position);
747 bool ignoreNL = false;
748 if (chPrev == '\r' && chNext == '\n') {
749 //Platform::DebugPrintf("Deleting lf after cr, move line end to cr at %d\n", lineRemove);
750 // Move back one
751 lv.SetValue(lineRemove, position / 2);
752 lineRemove++;
753 ignoreNL = true; // First \n is not real deletion
754 }
755
756 char ch = chNext;
757 for (int i = 0; i < deleteLength; i += 2) {
758 chNext = ' ';
759 if ((position + i + 2) < length)
760 chNext = ByteAt(position + i + 2);
761 //Platform::DebugPrintf("Deleting %d %x\n", i, ch);
762 if (ch == '\r') {
763 if (chNext != '\n') {
764 //Platform::DebugPrintf("Removing cr end of line\n");
765 lv.Remove(lineRemove);
766 }
767 } else if ((ch == '\n') && !ignoreNL) {
768 //Platform::DebugPrintf("Removing lf end of line\n");
769 lv.Remove(lineRemove);
770 ignoreNL = false; // Further \n are not real deletions
771 }
772
773 ch = chNext;
774 }
775 // May have to fix up end if last deletion causes cr to be next to lf
776 // or removes one of a crlf pair
777 char chAfter = ' ';
778 if ((position + deleteLength) < length)
779 chAfter = ByteAt(position + deleteLength);
780 if (chBefore == '\r' && chAfter == '\n') {
781 //d.printf("Joining cr before lf at %d\n", lineRemove);
782 // Using lineRemove-1 as cr ended line before start of deletion
783 lv.Remove(lineRemove - 1);
784 lv.SetValue(lineRemove - 1, position / 2 + 1);
785 }
786 }
787 GapTo(position);
788 length -= deleteLength;
789 gaplen += deleteLength;
790 part2body = body + gaplen;
791 }
792
793 undoCollectionType CellBuffer::SetUndoCollection(undoCollectionType collectUndo) {
794 collectingUndo = collectUndo;
795 undoSequenceDepth = 0;
796 return collectingUndo;
797 }
798
799 bool CellBuffer::IsCollectingUndo() {
800 return collectingUndo;
801 }
802
803 void CellBuffer::AppendUndoStartAction() {
804 EnsureUndoRoom();
805 // Finish any currently active undo sequence
806 undoSequenceDepth = 0;
807 if (actions[currentAction].at != startAction) {
808 undoSequenceDepth++;
809 currentAction++;
810 actions[currentAction].Create(startAction);
811 maxAction = currentAction;
812 }
813 }
814
815 void CellBuffer::BeginUndoAction() {
816 EnsureUndoRoom();
817 if (undoSequenceDepth == 0) {
818 if (actions[currentAction].at != startAction) {
819 currentAction++;
820 actions[currentAction].Create(startAction);
821 maxAction = currentAction;
822 }
823 }
824 undoSequenceDepth++;
825 }
826
827 void CellBuffer::EndUndoAction() {
828 EnsureUndoRoom();
829 undoSequenceDepth--;
830 if (0 == undoSequenceDepth) {
831 if (actions[currentAction].at != startAction) {
832 currentAction++;
833 actions[currentAction].Create(startAction);
834 maxAction = currentAction;
835 }
836 }
837 }
838
839 void CellBuffer::DeleteUndoHistory() {
840 for (int i = 1; i < maxAction; i++)
841 actions[i].Destroy();
842 maxAction = 0;
843 currentAction = 0;
844 savePoint = 0;
845 }
846
847 bool CellBuffer::CanUndo() {
848 return (!readOnly) && ((currentAction > 0) && (maxAction > 0));
849 }
850
851 int CellBuffer::StartUndo() {
852 // Drop any trailing startAction
853 if (actions[currentAction].at == startAction && currentAction > 0)
854 currentAction--;
855
856 // Count the steps in this action
857 int act = currentAction;
858 while (actions[act].at != startAction && act > 0) {
859 act--;
860 }
861 return currentAction - act;
862 }
863
864 const Action &CellBuffer::UndoStep() {
865 const Action &actionStep = actions[currentAction];
866 if (actionStep.at == insertAction) {
867 BasicDeleteChars(actionStep.position, actionStep.lenData*2);
868 } else if (actionStep.at == removeAction) {
869 char *styledData = new char[actionStep.lenData * 2];
870 for (int i = 0; i < actionStep.lenData; i++) {
871 styledData[i*2] = actionStep.data[i];
872 styledData[i*2+1] = 0;
873 }
874 BasicInsertString(actionStep.position, styledData, actionStep.lenData*2);
875 delete []styledData;
876 }
877 currentAction--;
878 return actionStep;
879 }
880
881 bool CellBuffer::CanRedo() {
882 return (!readOnly) && (maxAction > currentAction);
883 }
884
885 int CellBuffer::StartRedo() {
886 // Drop any leading startAction
887 if (actions[currentAction].at == startAction && currentAction < maxAction)
888 currentAction++;
889
890 // Count the steps in this action
891 int act = currentAction;
892 while (actions[act].at != startAction && act < maxAction) {
893 act++;
894 }
895 return act - currentAction;
896 }
897
898 const Action &CellBuffer::RedoStep() {
899 const Action &actionStep = actions[currentAction];
900 if (actionStep.at == insertAction) {
901 char *styledData = new char[actionStep.lenData * 2];
902 for (int i = 0; i < actionStep.lenData; i++) {
903 styledData[i*2] = actionStep.data[i];
904 styledData[i*2+1] = 0;
905 }
906 BasicInsertString(actionStep.position, styledData, actionStep.lenData*2);
907 delete []styledData;
908 } else if (actionStep.at == removeAction) {
909 BasicDeleteChars(actionStep.position, actionStep.lenData*2);
910 }
911 currentAction++;
912 return actionStep;
913 }
914
915 int CellBuffer::SetLineState(int line, int state) {
916 int stateOld = lineStates[line];
917 lineStates[line] = state;
918 return stateOld;
919 }
920
921 int CellBuffer::GetLineState(int line) {
922 return lineStates[line];
923 }
924
925 int CellBuffer::GetMaxLineState() {
926 return lineStates.Length();
927 }
928
929 int CellBuffer::SetLevel(int line, int level) {
930 int prev = 0;
931 if ((line >= 0) && (line < lv.lines)) {
932 if (!lv.levels) {
933 lv.ExpandLevels();
934 }
935 prev = lv.levels[line];
936 if (lv.levels[line] != level) {
937 lv.levels[line] = level;
938 }
939 }
940 return prev;
941 }
942
943 int CellBuffer::GetLevel(int line) {
944 if (lv.levels && (line >= 0) && (line < lv.lines)) {
945 return lv.levels[line];
946 } else {
947 return SC_FOLDLEVELBASE;
948 }
949 }
950