]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/CellBuffer.cxx
27e62ac61c1aedbd5ea11b4e4affbe8a7862e196
[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 "SVector.h"
17 #include "CellBuffer.h"
18
19 MarkerHandleSet::MarkerHandleSet() {
20 root = 0;
21 }
22
23 MarkerHandleSet::~MarkerHandleSet() {
24 MarkerHandleNumber *mhn = root;
25 while (mhn) {
26 MarkerHandleNumber *mhnToFree = mhn;
27 mhn = mhn->next;
28 delete mhnToFree;
29 }
30 root = 0;
31 }
32
33 int MarkerHandleSet::Length() {
34 int c = 0;
35 MarkerHandleNumber *mhn = root;
36 while (mhn) {
37 c++;
38 mhn = mhn->next;
39 }
40 return c;
41 }
42
43 int MarkerHandleSet::NumberFromHandle(int handle) {
44 MarkerHandleNumber *mhn = root;
45 while (mhn) {
46 if (mhn->handle == handle) {
47 return mhn->number;
48 }
49 mhn = mhn->next;
50 }
51 return - 1;
52 }
53
54 int MarkerHandleSet::MarkValue() {
55 unsigned int m = 0;
56 MarkerHandleNumber *mhn = root;
57 while (mhn) {
58 m |= (1 << mhn->number);
59 mhn = mhn->next;
60 }
61 return m;
62 }
63
64 bool MarkerHandleSet::Contains(int handle) {
65 MarkerHandleNumber *mhn = root;
66 while (mhn) {
67 if (mhn->handle == handle) {
68 return true;
69 }
70 mhn = mhn->next;
71 }
72 return false;
73 }
74
75 bool MarkerHandleSet::InsertHandle(int handle, int markerNum) {
76 MarkerHandleNumber *mhn = new MarkerHandleNumber;
77 if (!mhn)
78 return false;
79 mhn->handle = handle;
80 mhn->number = markerNum;
81 mhn->next = root;
82 root = mhn;
83 return true;
84 }
85
86 void MarkerHandleSet::RemoveHandle(int handle) {
87 MarkerHandleNumber **pmhn = &root;
88 while (*pmhn) {
89 MarkerHandleNumber *mhn = *pmhn;
90 if (mhn->handle == handle) {
91 *pmhn = mhn->next;
92 delete mhn;
93 return ;
94 }
95 pmhn = &((*pmhn)->next);
96 }
97 }
98
99 void MarkerHandleSet::RemoveNumber(int markerNum) {
100 MarkerHandleNumber **pmhn = &root;
101 while (*pmhn) {
102 MarkerHandleNumber *mhn = *pmhn;
103 if (mhn->number == markerNum) {
104 *pmhn = mhn->next;
105 delete mhn;
106 } else {
107 pmhn = &((*pmhn)->next);
108 }
109 }
110 }
111
112 void MarkerHandleSet::CombineWith(MarkerHandleSet *other) {
113 MarkerHandleNumber **pmhn = &root;
114 while (*pmhn) {
115 pmhn = &((*pmhn)->next);
116 }
117 *pmhn = other->root;
118 other->root = 0;
119 }
120
121 LineVector::LineVector() {
122 linesData = 0;
123 lines = 0;
124 size = 0;
125 levels = 0;
126 sizeLevels = 0;
127 handleCurrent = 1;
128 growSize = 1000;
129
130 Init();
131 }
132
133 LineVector::~LineVector() {
134 for (int line = 0; line < lines; line++) {
135 delete linesData[line].handleSet;
136 linesData[line].handleSet = 0;
137 }
138 delete []linesData;
139 linesData = 0;
140 delete []levels;
141 levels = 0;
142 }
143
144 void LineVector::Init() {
145 for (int line = 0; line < lines; line++) {
146 delete linesData[line].handleSet;
147 linesData[line].handleSet = 0;
148 }
149 delete []linesData;
150 linesData = new LineData[static_cast<int>(growSize)];
151 size = growSize;
152 lines = 1;
153 delete []levels;
154 levels = 0;
155 sizeLevels = 0;
156 }
157
158 void LineVector::Expand(int sizeNew) {
159 LineData *linesDataNew = new LineData[sizeNew];
160 if (linesDataNew) {
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
164 delete []linesData;
165 linesData = linesDataNew;
166 size = sizeNew;
167 } else {
168 Platform::DebugPrintf("No memory available\n");
169 // TODO: Blow up
170 }
171
172 }
173
174 void LineVector::ExpandLevels(int sizeNew) {
175 if (sizeNew == -1)
176 sizeNew = size;
177 int *levelsNew = new int[sizeNew];
178 if (levelsNew) {
179 int i = 0;
180 for (; i < sizeLevels; i++)
181 levelsNew[i] = levels[i];
182 for (; i < sizeNew; i++)
183 levelsNew[i] = SC_FOLDLEVELBASE;
184 delete []levels;
185 levels = levelsNew;
186 sizeLevels = sizeNew;
187 } else {
188 Platform::DebugPrintf("No memory available\n");
189 // TODO: Blow up
190 }
191
192 }
193
194 void LineVector::ClearLevels() {
195 delete []levels;
196 levels = 0;
197 sizeLevels = 0;
198 }
199
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)
204 growSize *= 2;
205 Expand(size + growSize);
206 if (levels) {
207 ExpandLevels(size + growSize);
208 }
209 }
210 lines++;
211 for (int i = lines; i > pos; i--) {
212 linesData[i] = linesData[i - 1];
213 }
214 linesData[pos].startPosition = value;
215 linesData[pos].handleSet = 0;
216 if (levels) {
217 for (int j = lines; j > pos; j--) {
218 levels[j] = levels[j - 1];
219 }
220 if (pos == 0) {
221 levels[pos] = SC_FOLDLEVELBASE;
222 } else if (pos == (lines - 1)) { // Last line will not be a folder
223 levels[pos] = SC_FOLDLEVELBASE;
224 } else {
225 levels[pos] = levels[pos - 1];
226 }
227 }
228 }
229
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);
236 lines = pos;
237 if (levels) {
238 ExpandLevels(pos + growSize);
239 }
240 }
241 linesData[pos].startPosition = value;
242 }
243
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
247 if (pos > 0) {
248 MergeMarkers(pos - 1);
249 }
250 for (int i = pos; i < lines; i++) {
251 linesData[i] = linesData[i + 1];
252 }
253 if (levels) {
254 // Level information merges back onto previous line
255 int posAbove = pos - 1;
256 if (posAbove < 0)
257 posAbove = 0;
258 for (int j = posAbove; j < lines; j++) {
259 levels[j] = levels[j + 1];
260 }
261 }
262 lines--;
263 }
264
265 int LineVector::LineFromPosition(int pos) {
266 //Platform::DebugPrintf("LineFromPostion %d lines=%d end = %d\n", pos, lines, linesData[lines].startPosition);
267 if (lines == 0)
268 return 0;
269 //Platform::DebugPrintf("LineFromPosition %d\n", pos);
270 if (pos >= linesData[lines].startPosition)
271 return lines - 1;
272 int lower = 0;
273 int upper = lines;
274 do {
275 int middle = (upper + lower + 1) / 2; // Round high
276 if (pos < linesData[middle].startPosition) {
277 upper = middle - 1;
278 } else {
279 lower = middle;
280 }
281 } while (lower < upper);
282 //Platform::DebugPrintf("LineFromPostion %d %d %d\n", pos, lower, linesData[lower].startPosition, linesData[lower > 1 ? lower - 1 : 0].startPosition);
283 return lower;
284 }
285
286 int LineVector::AddMark(int line, int markerNum) {
287 handleCurrent++;
288 if (!linesData[line].handleSet) {
289 // Need new structure to hold marker handle
290 linesData[line].handleSet = new MarkerHandleSet;
291 if (!linesData[line].handleSet)
292 return - 1;
293 }
294 linesData[line].handleSet->InsertHandle(handleCurrent, markerNum);
295
296 return handleCurrent;
297 }
298
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;
306 }
307 }
308
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;
314 } else {
315 linesData[line].handleSet->RemoveNumber(markerNum);
316 if (linesData[line].handleSet->Length() == 0) {
317 delete linesData[line].handleSet;
318 linesData[line].handleSet = 0;
319 }
320 }
321 }
322 }
323
324 void LineVector::DeleteMarkFromHandle(int markerHandle) {
325 int line = LineFromHandle(markerHandle);
326 if (line >= 0) {
327 linesData[line].handleSet->RemoveHandle(markerHandle);
328 if (linesData[line].handleSet->Length() == 0) {
329 delete linesData[line].handleSet;
330 linesData[line].handleSet = 0;
331 }
332 }
333 }
334
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)) {
339 return line;
340 }
341 }
342 }
343 return - 1;
344 }
345
346 Action::Action() {
347 at = startAction;
348 position = 0;
349 data = 0;
350 lenData = 0;
351 }
352
353 Action::~Action() {
354 Destroy();
355 }
356
357 void Action::Create(actionType at_, int position_, char *data_, int lenData_, bool mayCoalesce_) {
358 delete []data;
359 position = position_;
360 at = at_;
361 data = data_;
362 lenData = lenData_;
363 mayCoalesce = mayCoalesce_;
364 }
365
366 void Action::Destroy() {
367 delete []data;
368 data = 0;
369 }
370
371 void Action::Grab(Action *source) {
372 delete []data;
373
374 position = source->position;
375 at = source->at;
376 data = source->data;
377 lenData = source->lenData;
378 mayCoalesce = source->mayCoalesce;
379
380 // Ownership of source data transferred to this
381 source->position = 0;
382 source->at = startAction;
383 source->data = 0;
384 source->lenData = 0;
385 source->mayCoalesce = true;
386 }
387
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.
405
406 UndoHistory::UndoHistory() {
407
408 lenActions = 100;
409 actions = new Action[lenActions];
410 maxAction = 0;
411 currentAction = 0;
412 undoSequenceDepth = 0;
413 savePoint = 0;
414
415 actions[currentAction].Create(startAction);
416 }
417
418 UndoHistory::~UndoHistory() {
419 delete []actions;
420 actions = 0;
421 }
422
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];
430 if (!actionsNew)
431 return ;
432 for (int act = 0; act <= currentAction; act++)
433 actionsNew[act].Grab(&actions[act]);
434 delete []actions;
435 lenActions = lenActionsNew;
436 actions = actionsNew;
437 }
438 }
439
440 void UndoHistory::AppendAction(actionType at, int position, char *data, int lengthData) {
441 EnsureUndoRoom();
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) {
452 currentAction++;
453 } else if (currentAction == savePoint) {
454 currentAction++;
455 } else if ((at == insertAction) &&
456 (position != (actPrevious.position + actPrevious.lenData*2))) {
457 // Insertions must be immediately after to coalesce
458 currentAction++;
459 } else if (!actions[currentAction].mayCoalesce) {
460 // Not allowed to coalesce if this set
461 currentAction++;
462 } else if (at == removeAction) {
463 if ((lengthData == 1) || (lengthData == 2)){
464 if ((position + lengthData * 2) == actPrevious.position) {
465 ; // Backspace -> OK
466 } else if (position == actPrevious.position) {
467 ; // Delete -> OK
468 } else {
469 // Removals must be at same position to coalesce
470 currentAction++;
471 }
472 } else {
473 // Removals must be of one character to coalesce
474 currentAction++;
475 }
476 } else {
477 //Platform::DebugPrintf("action coalesced\n");
478 }
479
480 } else {
481 // Actions not at top level are always coalesced unless this is after return to top level
482 if (!actions[currentAction].mayCoalesce)
483 currentAction++;
484 }
485 } else {
486 currentAction++;
487 }
488 actions[currentAction].Create(at, position, data, lengthData);
489 currentAction++;
490 actions[currentAction].Create(startAction);
491 maxAction = currentAction;
492 }
493
494 void UndoHistory::BeginUndoAction() {
495 EnsureUndoRoom();
496 if (undoSequenceDepth == 0) {
497 if (actions[currentAction].at != startAction) {
498 currentAction++;
499 actions[currentAction].Create(startAction);
500 maxAction = currentAction;
501 }
502 actions[currentAction].mayCoalesce = false;
503 }
504 undoSequenceDepth++;
505 }
506
507 void UndoHistory::EndUndoAction() {
508 EnsureUndoRoom();
509 undoSequenceDepth--;
510 if (0 == undoSequenceDepth) {
511 if (actions[currentAction].at != startAction) {
512 currentAction++;
513 actions[currentAction].Create(startAction);
514 maxAction = currentAction;
515 }
516 actions[currentAction].mayCoalesce = false;
517 }
518 }
519
520 void UndoHistory::DropUndoSequence() {
521 undoSequenceDepth = 0;
522 }
523
524 void UndoHistory::DeleteUndoHistory() {
525 for (int i = 1; i < maxAction; i++)
526 actions[i].Destroy();
527 maxAction = 0;
528 currentAction = 0;
529 actions[currentAction].Create(startAction);
530 savePoint = 0;
531 }
532
533 void UndoHistory::SetSavePoint() {
534 savePoint = currentAction;
535 }
536
537 bool UndoHistory::IsSavePoint() const {
538 return savePoint == currentAction;
539 }
540
541 bool UndoHistory::CanUndo() const {
542 return (currentAction > 0) && (maxAction > 0);
543 }
544
545 int UndoHistory::StartUndo() {
546 // Drop any trailing startAction
547 if (actions[currentAction].at == startAction && currentAction > 0)
548 currentAction--;
549
550 // Count the steps in this action
551 int act = currentAction;
552 while (actions[act].at != startAction && act > 0) {
553 act--;
554 }
555 return currentAction - act;
556 }
557
558 const Action &UndoHistory::GetUndoStep() const {
559 return actions[currentAction];
560 }
561
562 void UndoHistory::CompletedUndoStep() {
563 currentAction--;
564 }
565
566 bool UndoHistory::CanRedo() const {
567 return maxAction > currentAction;
568 }
569
570 int UndoHistory::StartRedo() {
571 // Drop any leading startAction
572 if (actions[currentAction].at == startAction && currentAction < maxAction)
573 currentAction++;
574
575 // Count the steps in this action
576 int act = currentAction;
577 while (actions[act].at != startAction && act < maxAction) {
578 act++;
579 }
580 return act - currentAction;
581 }
582
583 const Action &UndoHistory::GetRedoStep() const {
584 return actions[currentAction];
585 }
586
587 void UndoHistory::CompletedRedoStep() {
588 currentAction++;
589 }
590
591 CellBuffer::CellBuffer(int initialLength) {
592 body = new char[initialLength];
593 size = initialLength;
594 length = 0;
595 part1len = 0;
596 gaplen = initialLength;
597 part2body = body + gaplen;
598 readOnly = false;
599 collectingUndo = true;
600 growSize = 4000;
601 }
602
603 CellBuffer::~CellBuffer() {
604 delete []body;
605 body = 0;
606 }
607
608 void CellBuffer::GapTo(int position) {
609 if (position == part1len)
610 return ;
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];
621 }
622 part1len = position;
623 part2body = body + gaplen;
624 }
625
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);
630 if (growSize * 6 < size)
631 growSize *= 2;
632 int newSize = size + insertionLength + growSize;
633 Allocate(newSize);
634 }
635 }
636
637 // To make it easier to write code that uses ByteAt, a position outside the range of the buffer
638 // can be retrieved. All characters outside the range have the value '\0'.
639 char CellBuffer::ByteAt(int position) {
640 if (position < part1len) {
641 if (position < 0) {
642 return '\0';
643 } else {
644 return body[position];
645 }
646 } else {
647 if (position >= length) {
648 return '\0';
649 } else {
650 return part2body[position];
651 }
652 }
653 }
654
655 void CellBuffer::SetByteAt(int position, char ch) {
656
657 if (position < 0) {
658 //Platform::DebugPrintf("Bad position %d\n",position);
659 return ;
660 }
661 if (position >= length + 11) {
662 Platform::DebugPrintf("Very Bad position %d of %d\n", position, length);
663 //exit(2);
664 return ;
665 }
666 if (position >= length) {
667 //Platform::DebugPrintf("Bad position %d of %d\n",position,length);
668 return ;
669 }
670
671 if (position < part1len) {
672 body[position] = ch;
673 } else {
674 part2body[position] = ch;
675 }
676 }
677
678 char CellBuffer::CharAt(int position) {
679 return ByteAt(position*2);
680 }
681
682 void CellBuffer::GetCharRange(char *buffer, int position, int lengthRetrieve) {
683 if (lengthRetrieve < 0)
684 return ;
685 if (position < 0)
686 return ;
687 int bytePos = position * 2;
688 if ((bytePos + lengthRetrieve * 2) > length) {
689 Platform::DebugPrintf("Bad GetCharRange %d for %d of %d\n", bytePos,
690 lengthRetrieve, length);
691 return ;
692 }
693 GapTo(0); // Move the buffer so its easy to subscript into it
694 char *pb = part2body + bytePos;
695 while (lengthRetrieve--) {
696 *buffer++ = *pb;
697 pb += 2;
698 }
699 }
700
701 char CellBuffer::StyleAt(int position) {
702 return ByteAt(position*2 + 1);
703 }
704
705 const char *CellBuffer::InsertString(int position, char *s, int insertLength) {
706 char *data = 0;
707 // InsertString and DeleteChars are the bottleneck though which all changes occur
708 if (!readOnly) {
709 if (collectingUndo) {
710 // Save into the undo/redo stack, but only the characters - not the formatting
711 // This takes up about half load time
712 data = new char[insertLength / 2];
713 for (int i = 0; i < insertLength / 2; i++) {
714 data[i] = s[i * 2];
715 }
716 uh.AppendAction(insertAction, position, data, insertLength / 2);
717 }
718
719 BasicInsertString(position, s, insertLength);
720 }
721 return data;
722 }
723
724 void CellBuffer::InsertCharStyle(int position, char ch, char style) {
725 char s[2];
726 s[0] = ch;
727 s[1] = style;
728 InsertString(position*2, s, 2);
729 }
730
731 bool CellBuffer::SetStyleAt(int position, char style, char mask) {
732 style &= mask;
733 char curVal = ByteAt(position * 2 + 1);
734 if ((curVal & mask) != style) {
735 SetByteAt(position*2 + 1, static_cast<char>((curVal & ~mask) | style));
736 return true;
737 } else {
738 return false;
739 }
740 }
741
742 bool CellBuffer::SetStyleFor(int position, int lengthStyle, char style, char mask) {
743 int bytePos = position * 2 + 1;
744 bool changed = false;
745 PLATFORM_ASSERT(lengthStyle == 0 ||
746 (lengthStyle > 0 && lengthStyle + position < length));
747 while (lengthStyle--) {
748 char curVal = ByteAt(bytePos);
749 if ((curVal & mask) != style) {
750 SetByteAt(bytePos, static_cast<char>((curVal & ~mask) | style));
751 changed = true;
752 }
753 bytePos += 2;
754 }
755 return changed;
756 }
757
758 const char *CellBuffer::DeleteChars(int position, int deleteLength) {
759 // InsertString and DeleteChars are the bottleneck though which all changes occur
760 char *data = 0;
761 if (!readOnly) {
762 if (collectingUndo) {
763 // Save into the undo/redo stack, but only the characters - not the formatting
764 data = new char[deleteLength / 2];
765 for (int i = 0; i < deleteLength / 2; i++) {
766 data[i] = ByteAt(position + i * 2);
767 }
768 uh.AppendAction(removeAction, position, data, deleteLength / 2);
769 }
770
771 BasicDeleteChars(position, deleteLength);
772 }
773 return data;
774 }
775
776 int CellBuffer::ByteLength() {
777 return length;
778 }
779
780 int CellBuffer::Length() {
781 return ByteLength() / 2;
782 }
783
784 void CellBuffer::Allocate(int newSize) {
785 if (newSize > length) {
786 GapTo(length);
787 char *newBody = new char[newSize];
788 memcpy(newBody, body, length);
789 delete []body;
790 body = newBody;
791 gaplen += newSize - size;
792 part2body = body + gaplen;
793 size = newSize;
794 }
795 }
796
797 int CellBuffer::Lines() {
798 //Platform::DebugPrintf("Lines = %d\n", lv.lines);
799 return lv.lines;
800 }
801
802 int CellBuffer::LineStart(int line) {
803 if (line < 0)
804 return 0;
805 else if (line > lv.lines)
806 return Length();
807 else
808 return lv.linesData[line].startPosition;
809 }
810
811 bool CellBuffer::IsReadOnly() {
812 return readOnly;
813 }
814
815 void CellBuffer::SetReadOnly(bool set) {
816 readOnly = set;
817 }
818
819 void CellBuffer::SetSavePoint() {
820 uh.SetSavePoint();
821 }
822
823 bool CellBuffer::IsSavePoint() {
824 return uh.IsSavePoint();
825 }
826
827 int CellBuffer::AddMark(int line, int markerNum) {
828 if ((line >= 0) && (line < lv.lines)) {
829 return lv.AddMark(line, markerNum);
830 }
831 return - 1;
832 }
833
834 void CellBuffer::DeleteMark(int line, int markerNum) {
835 if ((line >= 0) && (line < lv.lines)) {
836 lv.DeleteMark(line, markerNum);
837 }
838 }
839
840 void CellBuffer::DeleteMarkFromHandle(int markerHandle) {
841 lv.DeleteMarkFromHandle(markerHandle);
842 }
843
844 int CellBuffer::GetMark(int line) {
845 if ((line >= 0) && (line < lv.lines) && (lv.linesData[line].handleSet))
846 return lv.linesData[line].handleSet->MarkValue();
847 return 0;
848 }
849
850 void CellBuffer::DeleteAllMarks(int markerNum) {
851 for (int line = 0; line < lv.lines; line++) {
852 lv.DeleteMark(line, markerNum);
853 }
854 }
855
856 int CellBuffer::LineFromHandle(int markerHandle) {
857 return lv.LineFromHandle(markerHandle);
858 }
859
860 // Without undo
861
862 void CellBuffer::BasicInsertString(int position, char *s, int insertLength) {
863 //Platform::DebugPrintf("Inserting at %d for %d\n", position, insertLength);
864 if (insertLength == 0)
865 return ;
866 RoomFor(insertLength);
867 GapTo(position);
868
869 memcpy(body + part1len, s, insertLength);
870 length += insertLength;
871 part1len += insertLength;
872 gaplen -= insertLength;
873 part2body = body + gaplen;
874
875 int lineInsert = lv.LineFromPosition(position / 2) + 1;
876 // Point all the lines after the insertion point further along in the buffer
877 for (int lineAfter = lineInsert; lineAfter <= lv.lines; lineAfter++) {
878 lv.linesData[lineAfter].startPosition += insertLength / 2;
879 }
880 char chPrev = ' ';
881 if ((position - 2) >= 0)
882 chPrev = ByteAt(position - 2);
883 char chAfter = ' ';
884 if ((position + insertLength) < length)
885 chAfter = ByteAt(position + insertLength);
886 if (chPrev == '\r' && chAfter == '\n') {
887 //Platform::DebugPrintf("Splitting a crlf pair at %d\n", lineInsert);
888 // Splitting up a crlf pair at position
889 lv.InsertValue(lineInsert, position / 2);
890 lineInsert++;
891 }
892 char ch = ' ';
893 for (int i = 0; i < insertLength; i += 2) {
894 ch = s[i];
895 if (ch == '\r') {
896 //Platform::DebugPrintf("Inserting cr at %d\n", lineInsert);
897 lv.InsertValue(lineInsert, (position + i) / 2 + 1);
898 lineInsert++;
899 } else if (ch == '\n') {
900 if (chPrev == '\r') {
901 //Platform::DebugPrintf("Patching cr before lf at %d\n", lineInsert-1);
902 // Patch up what was end of line
903 lv.SetValue(lineInsert - 1, (position + i) / 2 + 1);
904 } else {
905 //Platform::DebugPrintf("Inserting lf at %d\n", lineInsert);
906 lv.InsertValue(lineInsert, (position + i) / 2 + 1);
907 lineInsert++;
908 }
909 }
910 chPrev = ch;
911 }
912 // Joining two lines where last insertion is cr and following text starts with lf
913 if (chAfter == '\n') {
914 if (ch == '\r') {
915 //Platform::DebugPrintf("Joining cr before lf at %d\n", lineInsert-1);
916 // End of line already in buffer so drop the newly created one
917 lv.Remove(lineInsert - 1);
918 }
919 }
920 }
921
922 void CellBuffer::BasicDeleteChars(int position, int deleteLength) {
923 //Platform::DebugPrintf("Deleting at %d for %d\n", position, deleteLength);
924 if (deleteLength == 0)
925 return ;
926
927 if ((position == 0) && (deleteLength == length)) {
928 // If whole buffer is being deleted, faster to reinitialise lines data
929 // than to delete each line.
930 //printf("Whole buffer being deleted\n");
931 lv.Init();
932 } else {
933 // Have to fix up line positions before doing deletion as looking at text in buffer
934 // to work out which lines have been removed
935
936 int lineRemove = lv.LineFromPosition(position / 2) + 1;
937 // Point all the lines after the insertion point further along in the buffer
938 for (int lineAfter = lineRemove; lineAfter <= lv.lines; lineAfter++) {
939 lv.linesData[lineAfter].startPosition -= deleteLength / 2;
940 }
941 char chPrev = ' ';
942 if (position >= 2)
943 chPrev = ByteAt(position - 2);
944 char chBefore = chPrev;
945 char chNext = ' ';
946 if (position < length)
947 chNext = ByteAt(position);
948 bool ignoreNL = false;
949 if (chPrev == '\r' && chNext == '\n') {
950 //Platform::DebugPrintf("Deleting lf after cr, move line end to cr at %d\n", lineRemove);
951 // Move back one
952 lv.SetValue(lineRemove, position / 2);
953 lineRemove++;
954 ignoreNL = true; // First \n is not real deletion
955 }
956
957 char ch = chNext;
958 for (int i = 0; i < deleteLength; i += 2) {
959 chNext = ' ';
960 if ((position + i + 2) < length)
961 chNext = ByteAt(position + i + 2);
962 //Platform::DebugPrintf("Deleting %d %x\n", i, ch);
963 if (ch == '\r') {
964 if (chNext != '\n') {
965 //Platform::DebugPrintf("Removing cr end of line\n");
966 lv.Remove(lineRemove);
967 }
968 } else if (ch == '\n') {
969 if (ignoreNL) {
970 ignoreNL = false; // Further \n are real deletions
971 } else {
972 //Platform::DebugPrintf("Removing lf end of line\n");
973 lv.Remove(lineRemove);
974 }
975 }
976
977 ch = chNext;
978 }
979 // May have to fix up end if last deletion causes cr to be next to lf
980 // or removes one of a crlf pair
981 char chAfter = ' ';
982 if ((position + deleteLength) < length)
983 chAfter = ByteAt(position + deleteLength);
984 if (chBefore == '\r' && chAfter == '\n') {
985 //d.printf("Joining cr before lf at %d\n", lineRemove);
986 // Using lineRemove-1 as cr ended line before start of deletion
987 lv.Remove(lineRemove - 1);
988 lv.SetValue(lineRemove - 1, position / 2 + 1);
989 }
990 }
991 GapTo(position);
992 length -= deleteLength;
993 gaplen += deleteLength;
994 part2body = body + gaplen;
995 }
996
997 bool CellBuffer::SetUndoCollection(bool collectUndo) {
998 collectingUndo = collectUndo;
999 uh.DropUndoSequence();
1000 return collectingUndo;
1001 }
1002
1003 bool CellBuffer::IsCollectingUndo() {
1004 return collectingUndo;
1005 }
1006
1007 void CellBuffer::BeginUndoAction() {
1008 uh.BeginUndoAction();
1009 }
1010
1011 void CellBuffer::EndUndoAction() {
1012 uh.EndUndoAction();
1013 }
1014
1015 void CellBuffer::DeleteUndoHistory() {
1016 uh.DeleteUndoHistory();
1017 }
1018
1019 bool CellBuffer::CanUndo() {
1020 return (!readOnly) && (uh.CanUndo());
1021 }
1022
1023 int CellBuffer::StartUndo() {
1024 return uh.StartUndo();
1025 }
1026
1027 const Action &CellBuffer::GetUndoStep() const {
1028 return uh.GetUndoStep();
1029 }
1030
1031 void CellBuffer::PerformUndoStep() {
1032 const Action &actionStep = uh.GetUndoStep();
1033 if (actionStep.at == insertAction) {
1034 BasicDeleteChars(actionStep.position, actionStep.lenData*2);
1035 } else if (actionStep.at == removeAction) {
1036 char *styledData = new char[actionStep.lenData * 2];
1037 for (int i = 0; i < actionStep.lenData; i++) {
1038 styledData[i*2] = actionStep.data[i];
1039 styledData[i*2 + 1] = 0;
1040 }
1041 BasicInsertString(actionStep.position, styledData, actionStep.lenData*2);
1042 delete []styledData;
1043 }
1044 uh.CompletedUndoStep();
1045 }
1046
1047 bool CellBuffer::CanRedo() {
1048 return (!readOnly) && (uh.CanRedo());
1049 }
1050
1051 int CellBuffer::StartRedo() {
1052 return uh.StartRedo();
1053 }
1054
1055 const Action &CellBuffer::GetRedoStep() const {
1056 return uh.GetRedoStep();
1057 }
1058
1059 void CellBuffer::PerformRedoStep() {
1060 const Action &actionStep = uh.GetRedoStep();
1061 if (actionStep.at == insertAction) {
1062 char *styledData = new char[actionStep.lenData * 2];
1063 for (int i = 0; i < actionStep.lenData; i++) {
1064 styledData[i*2] = actionStep.data[i];
1065 styledData[i*2 + 1] = 0;
1066 }
1067 BasicInsertString(actionStep.position, styledData, actionStep.lenData*2);
1068 delete []styledData;
1069 } else if (actionStep.at == removeAction) {
1070 BasicDeleteChars(actionStep.position, actionStep.lenData*2);
1071 }
1072 uh.CompletedRedoStep();
1073 }
1074
1075 int CellBuffer::SetLineState(int line, int state) {
1076 int stateOld = lineStates[line];
1077 lineStates[line] = state;
1078 return stateOld;
1079 }
1080
1081 int CellBuffer::GetLineState(int line) {
1082 return lineStates[line];
1083 }
1084
1085 int CellBuffer::GetMaxLineState() {
1086 return lineStates.Length();
1087 }
1088
1089 int CellBuffer::SetLevel(int line, int level) {
1090 int prev = 0;
1091 if ((line >= 0) && (line < lv.lines)) {
1092 if (!lv.levels) {
1093 lv.ExpandLevels();
1094 }
1095 prev = lv.levels[line];
1096 if (lv.levels[line] != level) {
1097 lv.levels[line] = level;
1098 }
1099 }
1100 return prev;
1101 }
1102
1103 int CellBuffer::GetLevel(int line) {
1104 if (lv.levels && (line >= 0) && (line < lv.lines)) {
1105 return lv.levels[line];
1106 } else {
1107 return SC_FOLDLEVELBASE;
1108 }
1109 }
1110
1111 void CellBuffer::ClearLevels() {
1112 lv.ClearLevels();
1113 }