]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/scintilla/src/CellBuffer.cxx
56a4be78c8f7b84704fc0782d792c184f6e4c94a
[wxWidgets.git] / contrib / 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 // The undo history stores a sequence of user operations that represent the user's view of the
348 // commands executed on the text.
349 // Each user operation contains a sequence of text insertion and text deletion actions.
350 // All the user operations are stored in a list of individual actions with 'start' actions used
351 // as delimiters between user operations.
352 // Initially there is one start action in the history.
353 // As each action is performed, it is recorded in the history. The action may either become
354 // part of the current user operation or may start a new user operation. If it is to be part of the
355 // current operation, then it overwrites the current last action. If it is to be part of a new
356 // operation, it is appended after the current last action.
357 // After writing the new action, a new start action is appended at the end of the history.
358 // The decision of whether to start a new user operation is based upon two factors. If a
359 // compound operation has been explicitly started by calling BeginUndoAction and no matching
360 // EndUndoAction (these calls nest) has been called, then the action is coalesced into the current
361 // operation. If there is no outstanding BeginUndoAction call then a new operation is started
362 // unless it looks as if the new action is caused by the user typing or deleting a stream of text.
363 // Sequences that look like typing or deletion are coalesced into a single user operation.
364
365 UndoHistory::UndoHistory() {
366
367 lenActions = 100;
368 actions = new Action[lenActions];
369 maxAction = 0;
370 currentAction = 0;
371 undoSequenceDepth = 0;
372 savePoint = 0;
373
374 actions[currentAction].Create(startAction);
375 }
376
377 UndoHistory::~UndoHistory() {
378 delete []actions;
379 actions = 0;
380 }
381
382 void UndoHistory::EnsureUndoRoom() {
383 //Platform::DebugPrintf("%% %d action %d %d %d\n", at, position, length, currentAction);
384 if (currentAction >= 2) {
385 // Have to test that there is room for 2 more actions in the array
386 // as two actions may be created by this function
387 if (currentAction >= (lenActions - 2)) {
388 // Run out of undo nodes so extend the array
389 int lenActionsNew = lenActions * 2;
390 Action *actionsNew = new Action[lenActionsNew];
391 if (!actionsNew)
392 return;
393 for (int act = 0; act <= currentAction; act++)
394 actionsNew[act].Grab(&actions[act]);
395 delete []actions;
396 lenActions = lenActionsNew;
397 actions = actionsNew;
398 }
399 }
400 }
401
402 void UndoHistory::AppendAction(actionType at, int position, char *data, int lengthData) {
403 EnsureUndoRoom();
404 //Platform::DebugPrintf("%% %d action %d %d %d\n", at, position, lengthData, currentAction);
405 //Platform::DebugPrintf("^ %d action %d %d\n", actions[currentAction - 1].at,
406 // actions[currentAction - 1].position, actions[currentAction - 1].lenData);
407 if (currentAction >= 1) {
408 if (0 == undoSequenceDepth) {
409 // Top level actions may not always be coalesced
410 Action &actPrevious = actions[currentAction - 1];
411 // See if current action can be coalesced into previous action
412 // Will work if both are inserts or deletes and position is same
413 if (at != actPrevious.at) {
414 currentAction++;
415 } else if (currentAction == savePoint) {
416 currentAction++;
417 } else if ((at == removeAction) &&
418 ((position + lengthData * 2) != actPrevious.position)) {
419 // Removals must be at same position to coalesce
420 currentAction++;
421 } else if ((at == insertAction) &&
422 (position != (actPrevious.position + actPrevious.lenData*2))) {
423 // Insertions must be immediately after to coalesce
424 currentAction++;
425 } else {
426 //Platform::DebugPrintf("action coalesced\n");
427 }
428 } else {
429 currentAction++;
430 }
431 } else {
432 currentAction++;
433 }
434 actions[currentAction].Create(at, position, data, lengthData);
435 currentAction++;
436 actions[currentAction].Create(startAction);
437 maxAction = currentAction;
438 }
439
440 void UndoHistory::BeginUndoAction() {
441 EnsureUndoRoom();
442 if (undoSequenceDepth == 0) {
443 if (actions[currentAction].at != startAction) {
444 currentAction++;
445 actions[currentAction].Create(startAction);
446 maxAction = currentAction;
447 }
448 }
449 undoSequenceDepth++;
450 }
451
452 void UndoHistory::EndUndoAction() {
453 EnsureUndoRoom();
454 undoSequenceDepth--;
455 if (0 == undoSequenceDepth) {
456 if (actions[currentAction].at != startAction) {
457 currentAction++;
458 actions[currentAction].Create(startAction);
459 maxAction = currentAction;
460 }
461 }
462 }
463
464 void UndoHistory::DropUndoSequence() {
465 undoSequenceDepth = 0;
466 }
467
468 void UndoHistory::DeleteUndoHistory() {
469 for (int i = 1; i < maxAction; i++)
470 actions[i].Destroy();
471 maxAction = 0;
472 currentAction = 0;
473 actions[currentAction].Create(startAction);
474 savePoint = 0;
475 }
476
477 void UndoHistory::SetSavePoint() {
478 savePoint = currentAction;
479 }
480
481 bool UndoHistory::IsSavePoint() const {
482 return savePoint == currentAction;
483 }
484
485 bool UndoHistory::CanUndo() const {
486 return (currentAction > 0) && (maxAction > 0);
487 }
488
489 int UndoHistory::StartUndo() {
490 // Drop any trailing startAction
491 if (actions[currentAction].at == startAction && currentAction > 0)
492 currentAction--;
493
494 // Count the steps in this action
495 int act = currentAction;
496 while (actions[act].at != startAction && act > 0) {
497 act--;
498 }
499 return currentAction - act;
500 }
501
502 const Action &UndoHistory::UndoStep() {
503 return actions[currentAction--];
504 }
505
506 bool UndoHistory::CanRedo() const {
507 return maxAction > currentAction;
508 }
509
510 int UndoHistory::StartRedo() {
511 // Drop any leading startAction
512 if (actions[currentAction].at == startAction && currentAction < maxAction)
513 currentAction++;
514
515 // Count the steps in this action
516 int act = currentAction;
517 while (actions[act].at != startAction && act < maxAction) {
518 act++;
519 }
520 return act - currentAction;
521 }
522
523 const Action &UndoHistory::RedoStep() {
524 return actions[currentAction++];
525 }
526
527 CellBuffer::CellBuffer(int initialLength) {
528 body = new char[initialLength];
529 size = initialLength;
530 length = 0;
531 part1len = 0;
532 gaplen = initialLength;
533 part2body = body + gaplen;
534 readOnly = false;
535 collectingUndo = undoCollectAutoStart;
536 }
537
538 CellBuffer::~CellBuffer() {
539 delete []body;
540 body = 0;
541 }
542
543 void CellBuffer::GapTo(int position) {
544 if (position == part1len)
545 return;
546 if (position < part1len) {
547 int diff = part1len - position;
548 //Platform::DebugPrintf("Move gap backwards to %d diff = %d part1len=%d length=%d \n", position,diff, part1len, length);
549 for (int i = 0; i < diff; i++)
550 body[part1len + gaplen - i - 1] = body[part1len - i - 1];
551 } else { // position > part1len
552 int diff = position - part1len;
553 //Platform::DebugPrintf("Move gap forwards to %d diff =%d\n", position,diff);
554 for (int i = 0; i < diff; i++)
555 body[part1len + i] = body[part1len + gaplen + i];
556 }
557 part1len = position;
558 part2body = body + gaplen;
559 }
560
561 void CellBuffer::RoomFor(int insertionLength) {
562 //Platform::DebugPrintf("need room %d %d\n", gaplen, insertionLength);
563 if (gaplen <= insertionLength) {
564 //Platform::DebugPrintf("need room %d %d\n", gaplen, insertionLength);
565 GapTo(length);
566 int newSize = size + insertionLength + 4000;
567 //Platform::DebugPrintf("moved gap %d\n", newSize);
568 char *newBody = new char[newSize];
569 memcpy(newBody, body, size);
570 delete []body;
571 body = newBody;
572 gaplen += newSize - size;
573 part2body = body + gaplen;
574 size = newSize;
575 //Platform::DebugPrintf("end need room %d %d - size=%d length=%d\n", gaplen, insertionLength,size,length);
576 }
577 }
578
579 // To make it easier to write code that uses ByteAt, a position outside the range of the buffer
580 // can be retrieved. All characters outside the range have the value '\0'.
581 char CellBuffer::ByteAt(int position) {
582 if (position < part1len) {
583 if (position < 0) {
584 return '\0';
585 } else {
586 return body[position];
587 }
588 } else {
589 if (position >= length) {
590 return '\0';
591 } else {
592 return part2body[position];
593 }
594 }
595 }
596
597 void CellBuffer::SetByteAt(int position, char ch) {
598
599 if (position < 0) {
600 //Platform::DebugPrintf("Bad position %d\n",position);
601 return;
602 }
603 if (position >= length + 11) {
604 Platform::DebugPrintf("Very Bad position %d of %d\n", position, length);
605 //exit(2);
606 return;
607 }
608 if (position >= length) {
609 //Platform::DebugPrintf("Bad position %d of %d\n",position,length);
610 return;
611 }
612
613 if (position < part1len) {
614 body[position] = ch;
615 } else {
616 part2body[position] = ch;
617 }
618 }
619
620 char CellBuffer::CharAt(int position) {
621 return ByteAt(position*2);
622 }
623
624 void CellBuffer::GetCharRange(char *buffer, int position, int lengthRetrieve) {
625 if (lengthRetrieve < 0)
626 return;
627 if (position < 0)
628 return;
629 int bytePos = position * 2;
630 if ((bytePos + lengthRetrieve * 2) > length) {
631 Platform::DebugPrintf("Bad GetCharRange %d for %d of %d\n",bytePos,
632 lengthRetrieve, length);
633 return;
634 }
635 GapTo(0); // Move the buffer so its easy to subscript into it
636 char *pb = part2body + bytePos;
637 while (lengthRetrieve--) {
638 *buffer++ = *pb;
639 pb +=2;
640 }
641 }
642
643 char CellBuffer::StyleAt(int position) {
644 return ByteAt(position*2 + 1);
645 }
646
647 const char *CellBuffer::InsertString(int position, char *s, int insertLength) {
648 char *data = 0;
649 // InsertString and DeleteChars are the bottleneck though which all changes occur
650 if (!readOnly) {
651 if (collectingUndo) {
652 // Save into the undo/redo stack, but only the characters - not the formatting
653 // This takes up about half load time
654 data = new char[insertLength / 2];
655 for (int i = 0; i < insertLength / 2; i++) {
656 data[i] = s[i * 2];
657 }
658 uh.AppendAction(insertAction, position, data, insertLength / 2);
659 }
660
661 BasicInsertString(position, s, insertLength);
662 }
663 return data;
664 }
665
666 void CellBuffer::InsertCharStyle(int position, char ch, char style) {
667 char s[2];
668 s[0] = ch;
669 s[1] = style;
670 InsertString(position*2, s, 2);
671 }
672
673 bool CellBuffer::SetStyleAt(int position, char style, char mask) {
674 char curVal = ByteAt(position*2 + 1);
675 if ((curVal & mask) != style) {
676 SetByteAt(position*2 + 1, (curVal & ~mask) | style);
677 return true;
678 } else {
679 return false;
680 }
681 }
682
683 bool CellBuffer::SetStyleFor(int position, int lengthStyle, char style, char mask) {
684 int bytePos = position * 2 + 1;
685 bool changed = false;
686 while (lengthStyle--) {
687 char curVal = ByteAt(bytePos);
688 if ((curVal & mask) != style) {
689 SetByteAt(bytePos, (curVal & ~mask) | style);
690 changed = true;
691 }
692 bytePos += 2;
693 }
694 return changed;
695 }
696
697 const char *CellBuffer::DeleteChars(int position, int deleteLength) {
698 // InsertString and DeleteChars are the bottleneck though which all changes occur
699 char *data = 0;
700 if (!readOnly) {
701 if (collectingUndo) {
702 // Save into the undo/redo stack, but only the characters - not the formatting
703 data = new char[deleteLength / 2];
704 for (int i = 0; i < deleteLength / 2; i++) {
705 data[i] = ByteAt(position + i * 2);
706 }
707 uh.AppendAction(removeAction, position, data, deleteLength / 2);
708 }
709
710 BasicDeleteChars(position, deleteLength);
711 }
712 return data;
713 }
714
715 int CellBuffer::ByteLength() {
716 return length;
717 }
718
719 int CellBuffer::Length() {
720 return ByteLength() / 2;
721 }
722
723 int CellBuffer::Lines() {
724 //Platform::DebugPrintf("Lines = %d\n", lv.lines);
725 return lv.lines;
726 }
727
728 int CellBuffer::LineStart(int line) {
729 if (line < 0)
730 return 0;
731 else if (line > lv.lines)
732 return length;
733 else
734 return lv.linesData[line].startPosition;
735 }
736
737 bool CellBuffer::IsReadOnly() {
738 return readOnly;
739 }
740
741 void CellBuffer::SetReadOnly(bool set) {
742 readOnly = set;
743 }
744
745 void CellBuffer::SetSavePoint() {
746 uh.SetSavePoint();
747 }
748
749 bool CellBuffer::IsSavePoint() {
750 return uh.IsSavePoint();
751 }
752
753 int CellBuffer::AddMark(int line, int markerNum) {
754 if ((line >= 0) && (line < lv.lines)) {
755 return lv.AddMark(line, markerNum);
756 }
757 return - 1;
758 }
759
760 void CellBuffer::DeleteMark(int line, int markerNum) {
761 if ((line >= 0) && (line < lv.lines)) {
762 lv.DeleteMark(line, markerNum);
763 }
764 }
765
766 void CellBuffer::DeleteMarkFromHandle(int markerHandle) {
767 lv.DeleteMarkFromHandle(markerHandle);
768 }
769
770 int CellBuffer::GetMark(int line) {
771 if ((line >= 0) && (line < lv.lines) && (lv.linesData[line].handleSet))
772 return lv.linesData[line].handleSet->MarkValue();
773 return 0;
774 }
775
776 void CellBuffer::DeleteAllMarks(int markerNum) {
777 for (int line = 0; line < lv.lines; line++) {
778 lv.DeleteMark(line, markerNum);
779 }
780 }
781
782 int CellBuffer::LineFromHandle(int markerHandle) {
783 return lv.LineFromHandle(markerHandle);
784 }
785
786 // Without undo
787
788 void CellBuffer::BasicInsertString(int position, char *s, int insertLength) {
789 //Platform::DebugPrintf("Inserting at %d for %d\n", position, insertLength);
790 if (insertLength == 0)
791 return;
792 RoomFor(insertLength);
793 GapTo(position);
794
795 memcpy(body + part1len, s, insertLength);
796 length += insertLength;
797 part1len += insertLength;
798 gaplen -= insertLength;
799 part2body = body + gaplen;
800
801 int lineInsert = lv.LineFromPosition(position / 2) + 1;
802 // Point all the lines after the insertion point further along in the buffer
803 for (int lineAfter = lineInsert; lineAfter <= lv.lines; lineAfter++) {
804 lv.linesData[lineAfter].startPosition += insertLength / 2;
805 }
806 char chPrev = ' ';
807 if ((position - 2) >= 0)
808 chPrev = ByteAt(position - 2);
809 char chAfter = ' ';
810 if ((position + insertLength) < length)
811 chAfter = ByteAt(position + insertLength);
812 if (chPrev == '\r' && chAfter == '\n') {
813 //Platform::DebugPrintf("Splitting a crlf pair at %d\n", lineInsert);
814 // Splitting up a crlf pair at position
815 lv.InsertValue(lineInsert, position / 2);
816 lineInsert++;
817 }
818 char ch = ' ';
819 for (int i = 0; i < insertLength; i += 2) {
820 ch = s[i];
821 if (ch == '\r') {
822 //Platform::DebugPrintf("Inserting cr at %d\n", lineInsert);
823 lv.InsertValue(lineInsert, (position + i) / 2 + 1);
824 lineInsert++;
825 } else if (ch == '\n') {
826 if (chPrev == '\r') {
827 //Platform::DebugPrintf("Patching cr before lf at %d\n", lineInsert-1);
828 // Patch up what was end of line
829 lv.SetValue(lineInsert - 1, (position + i) / 2 + 1);
830 } else {
831 //Platform::DebugPrintf("Inserting lf at %d\n", lineInsert);
832 lv.InsertValue(lineInsert, (position + i) / 2 + 1);
833 lineInsert++;
834 }
835 }
836 chPrev = ch;
837 }
838 // Joining two lines where last insertion is cr and following text starts with lf
839 if (chAfter == '\n') {
840 if (ch == '\r') {
841 //Platform::DebugPrintf("Joining cr before lf at %d\n", lineInsert-1);
842 // End of line already in buffer so drop the newly created one
843 lv.Remove(lineInsert - 1);
844 }
845 }
846 }
847
848 void CellBuffer::BasicDeleteChars(int position, int deleteLength) {
849 //Platform::DebugPrintf("Deleting at %d for %d\n", position, deleteLength);
850 if (deleteLength == 0)
851 return;
852
853 if ((position == 0) && (deleteLength == length)) {
854 // If whole buffer is being deleted, faster to reinitialise lines data
855 // than to delete each line.
856 //printf("Whole buffer being deleted\n");
857 lv.Init();
858 } else {
859 // Have to fix up line positions before doing deletion as looking at text in buffer
860 // to work out which lines have been removed
861
862 int lineRemove = lv.LineFromPosition(position / 2) + 1;
863 // Point all the lines after the insertion point further along in the buffer
864 for (int lineAfter = lineRemove; lineAfter <= lv.lines; lineAfter++) {
865 lv.linesData[lineAfter].startPosition -= deleteLength / 2;
866 }
867 char chPrev = ' ';
868 if (position >= 2)
869 chPrev = ByteAt(position - 2);
870 char chBefore = chPrev;
871 char chNext = ' ';
872 if (position < length)
873 chNext = ByteAt(position);
874 bool ignoreNL = false;
875 if (chPrev == '\r' && chNext == '\n') {
876 //Platform::DebugPrintf("Deleting lf after cr, move line end to cr at %d\n", lineRemove);
877 // Move back one
878 lv.SetValue(lineRemove, position / 2);
879 lineRemove++;
880 ignoreNL = true; // First \n is not real deletion
881 }
882
883 char ch = chNext;
884 for (int i = 0; i < deleteLength; i += 2) {
885 chNext = ' ';
886 if ((position + i + 2) < length)
887 chNext = ByteAt(position + i + 2);
888 //Platform::DebugPrintf("Deleting %d %x\n", i, ch);
889 if (ch == '\r') {
890 if (chNext != '\n') {
891 //Platform::DebugPrintf("Removing cr end of line\n");
892 lv.Remove(lineRemove);
893 }
894 } else if ((ch == '\n') && !ignoreNL) {
895 //Platform::DebugPrintf("Removing lf end of line\n");
896 lv.Remove(lineRemove);
897 ignoreNL = false; // Further \n are not real deletions
898 }
899
900 ch = chNext;
901 }
902 // May have to fix up end if last deletion causes cr to be next to lf
903 // or removes one of a crlf pair
904 char chAfter = ' ';
905 if ((position + deleteLength) < length)
906 chAfter = ByteAt(position + deleteLength);
907 if (chBefore == '\r' && chAfter == '\n') {
908 //d.printf("Joining cr before lf at %d\n", lineRemove);
909 // Using lineRemove-1 as cr ended line before start of deletion
910 lv.Remove(lineRemove - 1);
911 lv.SetValue(lineRemove - 1, position / 2 + 1);
912 }
913 }
914 GapTo(position);
915 length -= deleteLength;
916 gaplen += deleteLength;
917 part2body = body + gaplen;
918 }
919
920 undoCollectionType CellBuffer::SetUndoCollection(undoCollectionType collectUndo) {
921 collectingUndo = collectUndo;
922 uh.DropUndoSequence();
923 return collectingUndo;
924 }
925
926 bool CellBuffer::IsCollectingUndo() {
927 return collectingUndo;
928 }
929
930 void CellBuffer::BeginUndoAction() {
931 uh.BeginUndoAction();
932 }
933
934 void CellBuffer::EndUndoAction() {
935 uh.EndUndoAction();
936 }
937
938 void CellBuffer::DeleteUndoHistory() {
939 uh.DeleteUndoHistory();
940 }
941
942 bool CellBuffer::CanUndo() {
943 return (!readOnly) && (uh.CanUndo());
944 }
945
946 int CellBuffer::StartUndo() {
947 return uh.StartUndo();
948 }
949
950 const Action &CellBuffer::UndoStep() {
951 const Action &actionStep = uh.UndoStep();
952 if (actionStep.at == insertAction) {
953 BasicDeleteChars(actionStep.position, actionStep.lenData*2);
954 } else if (actionStep.at == removeAction) {
955 char *styledData = new char[actionStep.lenData * 2];
956 for (int i = 0; i < actionStep.lenData; i++) {
957 styledData[i*2] = actionStep.data[i];
958 styledData[i*2+1] = 0;
959 }
960 BasicInsertString(actionStep.position, styledData, actionStep.lenData*2);
961 delete []styledData;
962 }
963 return actionStep;
964 }
965
966 bool CellBuffer::CanRedo() {
967 return (!readOnly) && (uh.CanRedo());
968 }
969
970 int CellBuffer::StartRedo() {
971 return uh.StartRedo();
972 }
973
974 const Action &CellBuffer::RedoStep() {
975 const Action &actionStep = uh.RedoStep();
976 if (actionStep.at == insertAction) {
977 char *styledData = new char[actionStep.lenData * 2];
978 for (int i = 0; i < actionStep.lenData; i++) {
979 styledData[i*2] = actionStep.data[i];
980 styledData[i*2+1] = 0;
981 }
982 BasicInsertString(actionStep.position, styledData, actionStep.lenData*2);
983 delete []styledData;
984 } else if (actionStep.at == removeAction) {
985 BasicDeleteChars(actionStep.position, actionStep.lenData*2);
986 }
987 return actionStep;
988 }
989
990 int CellBuffer::SetLineState(int line, int state) {
991 int stateOld = lineStates[line];
992 lineStates[line] = state;
993 return stateOld;
994 }
995
996 int CellBuffer::GetLineState(int line) {
997 return lineStates[line];
998 }
999
1000 int CellBuffer::GetMaxLineState() {
1001 return lineStates.Length();
1002 }
1003
1004 int CellBuffer::SetLevel(int line, int level) {
1005 int prev = 0;
1006 if ((line >= 0) && (line < lv.lines)) {
1007 if (!lv.levels) {
1008 lv.ExpandLevels();
1009 }
1010 prev = lv.levels[line];
1011 if (lv.levels[line] != level) {
1012 lv.levels[line] = level;
1013 }
1014 }
1015 return prev;
1016 }
1017
1018 int CellBuffer::GetLevel(int line) {
1019 if (lv.levels && (line >= 0) && (line < lv.lines)) {
1020 return lv.levels[line];
1021 } else {
1022 return SC_FOLDLEVELBASE;
1023 }
1024 }
1025