]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/CellBuffer.cxx
Moved from Scintilla version 1.25 to 1.32
[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 size = 0;
123 levels = 0;
124 sizeLevels = 0;
125 handleCurrent = 1;
126
127 Init();
128 }
129
130 LineVector::~LineVector() {
131 for (int line = 0; line < lines; line++) {
132 delete linesData[line].handleSet;
133 linesData[line].handleSet = 0;
134 }
135 delete []linesData;
136 linesData = 0;
137 delete []levels;
138 levels = 0;
139 }
140
141 void LineVector::Init() {
142 for (int line = 0; line < lines; line++) {
143 delete linesData[line].handleSet;
144 linesData[line].handleSet = 0;
145 }
146 delete []linesData;
147 linesData = new LineData[static_cast<int>(growSize)];
148 size = growSize;
149 lines = 1;
150 delete []levels;
151 levels = 0;
152 sizeLevels = 0;
153 }
154
155 void LineVector::Expand(int sizeNew) {
156 LineData *linesDataNew = new LineData[sizeNew];
157 if (linesDataNew) {
158 for (int i = 0; i < size; i++)
159 linesDataNew[i] = linesData[i];
160 // Do not delete handleSets here as they are transferred to new linesData
161 delete []linesData;
162 linesData = linesDataNew;
163 size = sizeNew;
164 } else {
165 Platform::DebugPrintf("No memory available\n");
166 // TODO: Blow up
167 }
168
169 }
170
171 void LineVector::ExpandLevels(int sizeNew) {
172 if (sizeNew == -1)
173 sizeNew = size;
174 int *levelsNew = new int[sizeNew];
175 if (levelsNew) {
176 int i = 0;
177 for (; i < sizeLevels; i++)
178 levelsNew[i] = levels[i];
179 for (; i < sizeNew; i++)
180 levelsNew[i] = SC_FOLDLEVELBASE;
181 delete []levels;
182 levels = levelsNew;
183 sizeLevels = sizeNew;
184 } else {
185 Platform::DebugPrintf("No memory available\n");
186 // TODO: Blow up
187 }
188
189 }
190
191 void LineVector::ClearLevels() {
192 delete []levels;
193 levels = 0;
194 sizeLevels = 0;
195 }
196
197 void LineVector::InsertValue(int pos, int value) {
198 //Platform::DebugPrintf("InsertValue[%d] = %d\n", pos, value);
199 if ((lines + 2) >= size) {
200 Expand(size + growSize);
201 if (levels) {
202 ExpandLevels(size + growSize);
203 }
204 }
205 lines++;
206 for (int i = lines; i > pos; i--) {
207 linesData[i] = linesData[i - 1];
208 }
209 linesData[pos].startPosition = value;
210 linesData[pos].handleSet = 0;
211 if (levels) {
212 for (int j = lines; j > pos; j--) {
213 levels[j] = levels[j - 1];
214 }
215 if (pos == 0) {
216 levels[pos] = SC_FOLDLEVELBASE;
217 } else if (pos == (lines - 1)) { // Last line will not be a folder
218 levels[pos] = SC_FOLDLEVELBASE;
219 } else {
220 levels[pos] = levels[pos - 1];
221 }
222 }
223 }
224
225 void LineVector::SetValue(int pos, int value) {
226 //Platform::DebugPrintf("SetValue[%d] = %d\n", pos, value);
227 if ((pos + 2) >= size) {
228 //Platform::DebugPrintf("Resize %d %d\n", size,pos);
229 Expand(pos + growSize);
230 //Platform::DebugPrintf("end Resize %d %d\n", size,pos);
231 lines = pos;
232 if (levels) {
233 ExpandLevels(pos + growSize);
234 }
235 }
236 linesData[pos].startPosition = value;
237 }
238
239 void LineVector::Remove(int pos) {
240 //Platform::DebugPrintf("Remove %d\n", pos);
241 // Retain the markers from the deleted line by oring them into the previous line
242 if (pos > 0) {
243 MergeMarkers(pos - 1);
244 }
245 for (int i = pos; i < lines; i++) {
246 linesData[i] = linesData[i + 1];
247 }
248 if (levels) {
249 // Level information merges back onto previous line
250 int posAbove = pos - 1;
251 if (posAbove < 0)
252 posAbove = 0;
253 for (int j = posAbove; j < lines; j++) {
254 levels[j] = levels[j + 1];
255 }
256 }
257 lines--;
258 }
259
260 int LineVector::LineFromPosition(int pos) {
261 //Platform::DebugPrintf("LineFromPostion %d lines=%d end = %d\n", pos, lines, linesData[lines].startPosition);
262 if (lines == 0)
263 return 0;
264 //Platform::DebugPrintf("LineFromPosition %d\n", pos);
265 if (pos >= linesData[lines].startPosition)
266 return lines - 1;
267 int lower = 0;
268 int upper = lines;
269 do {
270 int middle = (upper + lower + 1) / 2; // Round high
271 if (pos < linesData[middle].startPosition) {
272 upper = middle - 1;
273 } else {
274 lower = middle;
275 }
276 } while (lower < upper);
277 //Platform::DebugPrintf("LineFromPostion %d %d %d\n", pos, lower, linesData[lower].startPosition, linesData[lower > 1 ? lower - 1 : 0].startPosition);
278 return lower;
279 }
280
281 int LineVector::AddMark(int line, int markerNum) {
282 handleCurrent++;
283 if (!linesData[line].handleSet) {
284 // Need new structure to hold marker handle
285 linesData[line].handleSet = new MarkerHandleSet;
286 if (!linesData[line].handleSet)
287 return - 1;
288 }
289 linesData[line].handleSet->InsertHandle(handleCurrent, markerNum);
290
291 return handleCurrent;
292 }
293
294 void LineVector::MergeMarkers(int pos) {
295 if (linesData[pos + 1].handleSet != NULL) {
296 if (linesData[pos].handleSet == NULL )
297 linesData[pos].handleSet = new MarkerHandleSet;
298 linesData[pos].handleSet->CombineWith(linesData[pos + 1].handleSet);
299 delete linesData[pos + 1].handleSet;
300 linesData[pos + 1].handleSet = NULL;
301 }
302 }
303
304 void LineVector::DeleteMark(int line, int markerNum) {
305 if (linesData[line].handleSet) {
306 if (markerNum == -1) {
307 delete linesData[line].handleSet;
308 linesData[line].handleSet = 0;
309 } else {
310 linesData[line].handleSet->RemoveNumber(markerNum);
311 if (linesData[line].handleSet->Length() == 0) {
312 delete linesData[line].handleSet;
313 linesData[line].handleSet = 0;
314 }
315 }
316 }
317 }
318
319 void LineVector::DeleteMarkFromHandle(int markerHandle) {
320 int line = LineFromHandle(markerHandle);
321 if (line >= 0) {
322 linesData[line].handleSet->RemoveHandle(markerHandle);
323 if (linesData[line].handleSet->Length() == 0) {
324 delete linesData[line].handleSet;
325 linesData[line].handleSet = 0;
326 }
327 }
328 }
329
330 int LineVector::LineFromHandle(int markerHandle) {
331 for (int line = 0; line < lines; line++) {
332 if (linesData[line].handleSet) {
333 if (linesData[line].handleSet->Contains(markerHandle)) {
334 return line;
335 }
336 }
337 }
338 return - 1;
339 }
340
341 Action::Action() {
342 at = startAction;
343 position = 0;
344 data = 0;
345 lenData = 0;
346 }
347
348 Action::~Action() {
349 Destroy();
350 }
351
352 void Action::Create(actionType at_, int position_, char *data_, int lenData_, bool mayCoalesce_) {
353 delete []data;
354 position = position_;
355 at = at_;
356 data = data_;
357 lenData = lenData_;
358 mayCoalesce = mayCoalesce_;
359 }
360
361 void Action::Destroy() {
362 delete []data;
363 data = 0;
364 }
365
366 void Action::Grab(Action *source) {
367 delete []data;
368
369 position = source->position;
370 at = source->at;
371 data = source->data;
372 lenData = source->lenData;
373 mayCoalesce = source->mayCoalesce;
374
375 // Ownership of source data transferred to this
376 source->position = 0;
377 source->at = startAction;
378 source->data = 0;
379 source->lenData = 0;
380 source->mayCoalesce = true;
381 }
382
383 // The undo history stores a sequence of user operations that represent the user's view of the
384 // commands executed on the text.
385 // Each user operation contains a sequence of text insertion and text deletion actions.
386 // All the user operations are stored in a list of individual actions with 'start' actions used
387 // as delimiters between user operations.
388 // Initially there is one start action in the history.
389 // As each action is performed, it is recorded in the history. The action may either become
390 // part of the current user operation or may start a new user operation. If it is to be part of the
391 // current operation, then it overwrites the current last action. If it is to be part of a new
392 // operation, it is appended after the current last action.
393 // After writing the new action, a new start action is appended at the end of the history.
394 // The decision of whether to start a new user operation is based upon two factors. If a
395 // compound operation has been explicitly started by calling BeginUndoAction and no matching
396 // EndUndoAction (these calls nest) has been called, then the action is coalesced into the current
397 // operation. If there is no outstanding BeginUndoAction call then a new operation is started
398 // unless it looks as if the new action is caused by the user typing or deleting a stream of text.
399 // Sequences that look like typing or deletion are coalesced into a single user operation.
400
401 UndoHistory::UndoHistory() {
402
403 lenActions = 100;
404 actions = new Action[lenActions];
405 maxAction = 0;
406 currentAction = 0;
407 undoSequenceDepth = 0;
408 savePoint = 0;
409
410 actions[currentAction].Create(startAction);
411 }
412
413 UndoHistory::~UndoHistory() {
414 delete []actions;
415 actions = 0;
416 }
417
418 void UndoHistory::EnsureUndoRoom() {
419 //Platform::DebugPrintf("%% %d action %d %d %d\n", at, position, length, currentAction);
420 if (currentAction >= 2) {
421 // Have to test that there is room for 2 more actions in the array
422 // as two actions may be created by this function
423 if (currentAction >= (lenActions - 2)) {
424 // Run out of undo nodes so extend the array
425 int lenActionsNew = lenActions * 2;
426 Action *actionsNew = new Action[lenActionsNew];
427 if (!actionsNew)
428 return ;
429 for (int act = 0; act <= currentAction; act++)
430 actionsNew[act].Grab(&actions[act]);
431 delete []actions;
432 lenActions = lenActionsNew;
433 actions = actionsNew;
434 }
435 }
436 }
437
438 void UndoHistory::AppendAction(actionType at, int position, char *data, int lengthData) {
439 EnsureUndoRoom();
440 //Platform::DebugPrintf("%% %d action %d %d %d\n", at, position, lengthData, currentAction);
441 //Platform::DebugPrintf("^ %d action %d %d\n", actions[currentAction - 1].at,
442 // actions[currentAction - 1].position, actions[currentAction - 1].lenData);
443 if (currentAction >= 1) {
444 if (0 == undoSequenceDepth) {
445 // Top level actions may not always be coalesced
446 Action &actPrevious = actions[currentAction - 1];
447 // See if current action can be coalesced into previous action
448 // Will work if both are inserts or deletes and position is same
449 if (at != actPrevious.at) {
450 currentAction++;
451 } else if (currentAction == savePoint) {
452 currentAction++;
453 } else if ((at == removeAction) &&
454 ((position + lengthData * 2) != actPrevious.position)) {
455 // Removals must be at same position to coalesce
456 currentAction++;
457 } else if ((at == insertAction) &&
458 (position != (actPrevious.position + actPrevious.lenData*2))) {
459 // Insertions must be immediately after to coalesce
460 currentAction++;
461 } else {
462 //Platform::DebugPrintf("action coalesced\n");
463 }
464
465 } else {
466 // Actions not at top level are always coalesced unless this is after return to top level
467 if (!actions[currentAction].mayCoalesce)
468 currentAction++;
469 }
470 } else {
471 currentAction++;
472 }
473 actions[currentAction].Create(at, position, data, lengthData);
474 currentAction++;
475 actions[currentAction].Create(startAction);
476 maxAction = currentAction;
477 }
478
479 void UndoHistory::BeginUndoAction() {
480 EnsureUndoRoom();
481 if (undoSequenceDepth == 0) {
482 if (actions[currentAction].at != startAction) {
483 currentAction++;
484 actions[currentAction].Create(startAction);
485 maxAction = currentAction;
486 }
487 actions[currentAction].mayCoalesce = false;
488 }
489 undoSequenceDepth++;
490 }
491
492 void UndoHistory::EndUndoAction() {
493 EnsureUndoRoom();
494 undoSequenceDepth--;
495 if (0 == undoSequenceDepth) {
496 if (actions[currentAction].at != startAction) {
497 currentAction++;
498 actions[currentAction].Create(startAction);
499 maxAction = currentAction;
500 }
501 actions[currentAction].mayCoalesce = false;
502 }
503 }
504
505 void UndoHistory::DropUndoSequence() {
506 undoSequenceDepth = 0;
507 }
508
509 void UndoHistory::DeleteUndoHistory() {
510 for (int i = 1; i < maxAction; i++)
511 actions[i].Destroy();
512 maxAction = 0;
513 currentAction = 0;
514 actions[currentAction].Create(startAction);
515 savePoint = 0;
516 }
517
518 void UndoHistory::SetSavePoint() {
519 savePoint = currentAction;
520 }
521
522 bool UndoHistory::IsSavePoint() const {
523 return savePoint == currentAction;
524 }
525
526 bool UndoHistory::CanUndo() const {
527 return (currentAction > 0) && (maxAction > 0);
528 }
529
530 int UndoHistory::StartUndo() {
531 // Drop any trailing startAction
532 if (actions[currentAction].at == startAction && currentAction > 0)
533 currentAction--;
534
535 // Count the steps in this action
536 int act = currentAction;
537 while (actions[act].at != startAction && act > 0) {
538 act--;
539 }
540 return currentAction - act;
541 }
542
543 const Action &UndoHistory::GetUndoStep() const {
544 return actions[currentAction];
545 }
546
547 void UndoHistory::CompletedUndoStep() {
548 currentAction--;
549 }
550
551 bool UndoHistory::CanRedo() const {
552 return maxAction > currentAction;
553 }
554
555 int UndoHistory::StartRedo() {
556 // Drop any leading startAction
557 if (actions[currentAction].at == startAction && currentAction < maxAction)
558 currentAction++;
559
560 // Count the steps in this action
561 int act = currentAction;
562 while (actions[act].at != startAction && act < maxAction) {
563 act++;
564 }
565 return act - currentAction;
566 }
567
568 const Action &UndoHistory::GetRedoStep() const {
569 return actions[currentAction];
570 }
571
572 void UndoHistory::CompletedRedoStep() {
573 currentAction++;
574 }
575
576 CellBuffer::CellBuffer(int initialLength) {
577 body = new char[initialLength];
578 size = initialLength;
579 length = 0;
580 part1len = 0;
581 gaplen = initialLength;
582 part2body = body + gaplen;
583 readOnly = false;
584 collectingUndo = true;
585 }
586
587 CellBuffer::~CellBuffer() {
588 delete []body;
589 body = 0;
590 }
591
592 void CellBuffer::GapTo(int position) {
593 if (position == part1len)
594 return ;
595 if (position < part1len) {
596 int diff = part1len - position;
597 //Platform::DebugPrintf("Move gap backwards to %d diff = %d part1len=%d length=%d \n", position,diff, part1len, length);
598 for (int i = 0; i < diff; i++)
599 body[part1len + gaplen - i - 1] = body[part1len - i - 1];
600 } else { // position > part1len
601 int diff = position - part1len;
602 //Platform::DebugPrintf("Move gap forwards to %d diff =%d\n", position,diff);
603 for (int i = 0; i < diff; i++)
604 body[part1len + i] = body[part1len + gaplen + i];
605 }
606 part1len = position;
607 part2body = body + gaplen;
608 }
609
610 void CellBuffer::RoomFor(int insertionLength) {
611 //Platform::DebugPrintf("need room %d %d\n", gaplen, insertionLength);
612 if (gaplen <= insertionLength) {
613 //Platform::DebugPrintf("need room %d %d\n", gaplen, insertionLength);
614 GapTo(length);
615 int newSize = size + insertionLength + 4000;
616 //Platform::DebugPrintf("moved gap %d\n", newSize);
617 char *newBody = new char[newSize];
618 memcpy(newBody, body, size);
619 delete []body;
620 body = newBody;
621 gaplen += newSize - size;
622 part2body = body + gaplen;
623 size = newSize;
624 //Platform::DebugPrintf("end need room %d %d - size=%d length=%d\n", gaplen, insertionLength,size,length);
625 }
626
627 }
628
629 // To make it easier to write code that uses ByteAt, a position outside the range of the buffer
630 // can be retrieved. All characters outside the range have the value '\0'.
631 char CellBuffer::ByteAt(int position) {
632 if (position < part1len) {
633 if (position < 0) {
634 return '\0';
635 } else {
636 return body[position];
637 }
638 } else {
639 if (position >= length) {
640 return '\0';
641 } else {
642 return part2body[position];
643 }
644 }
645 }
646
647 void CellBuffer::SetByteAt(int position, char ch) {
648
649 if (position < 0) {
650 //Platform::DebugPrintf("Bad position %d\n",position);
651 return ;
652 }
653 if (position >= length + 11) {
654 Platform::DebugPrintf("Very Bad position %d of %d\n", position, length);
655 //exit(2);
656 return ;
657 }
658 if (position >= length) {
659 //Platform::DebugPrintf("Bad position %d of %d\n",position,length);
660 return ;
661 }
662
663 if (position < part1len) {
664 body[position] = ch;
665 } else {
666 part2body[position] = ch;
667 }
668 }
669
670 char CellBuffer::CharAt(int position) {
671 return ByteAt(position*2);
672 }
673
674 void CellBuffer::GetCharRange(char *buffer, int position, int lengthRetrieve) {
675 if (lengthRetrieve < 0)
676 return ;
677 if (position < 0)
678 return ;
679 int bytePos = position * 2;
680 if ((bytePos + lengthRetrieve * 2) > length) {
681 Platform::DebugPrintf("Bad GetCharRange %d for %d of %d\n", bytePos,
682 lengthRetrieve, length);
683 return ;
684 }
685 GapTo(0); // Move the buffer so its easy to subscript into it
686 char *pb = part2body + bytePos;
687 while (lengthRetrieve--) {
688 *buffer++ = *pb;
689 pb += 2;
690 }
691 }
692
693 char CellBuffer::StyleAt(int position) {
694 return ByteAt(position*2 + 1);
695 }
696
697 const char *CellBuffer::InsertString(int position, char *s, int insertLength) {
698 char *data = 0;
699 // InsertString and DeleteChars are the bottleneck though which all changes occur
700 if (!readOnly) {
701 if (collectingUndo) {
702 // Save into the undo/redo stack, but only the characters - not the formatting
703 // This takes up about half load time
704 data = new char[insertLength / 2];
705 for (int i = 0; i < insertLength / 2; i++) {
706 data[i] = s[i * 2];
707 }
708 uh.AppendAction(insertAction, position, data, insertLength / 2);
709 }
710
711 BasicInsertString(position, s, insertLength);
712 }
713 return data;
714 }
715
716 void CellBuffer::InsertCharStyle(int position, char ch, char style) {
717 char s[2];
718 s[0] = ch;
719 s[1] = style;
720 InsertString(position*2, s, 2);
721 }
722
723 bool CellBuffer::SetStyleAt(int position, char style, char mask) {
724 char curVal = ByteAt(position * 2 + 1);
725 if ((curVal & mask) != style) {
726 SetByteAt(position*2 + 1, static_cast<char>((curVal & ~mask) | style));
727 return true;
728 } else {
729 return false;
730 }
731 }
732
733 bool CellBuffer::SetStyleFor(int position, int lengthStyle, char style, char mask) {
734 int bytePos = position * 2 + 1;
735 bool changed = false;
736 while (lengthStyle--) {
737 char curVal = ByteAt(bytePos);
738 if ((curVal & mask) != style) {
739 SetByteAt(bytePos, static_cast<char>((curVal & ~mask) | style));
740 changed = true;
741 }
742 bytePos += 2;
743 }
744 return changed;
745 }
746
747 const char *CellBuffer::DeleteChars(int position, int deleteLength) {
748 // InsertString and DeleteChars are the bottleneck though which all changes occur
749 char *data = 0;
750 if (!readOnly) {
751 if (collectingUndo) {
752 // Save into the undo/redo stack, but only the characters - not the formatting
753 data = new char[deleteLength / 2];
754 for (int i = 0; i < deleteLength / 2; i++) {
755 data[i] = ByteAt(position + i * 2);
756 }
757 uh.AppendAction(removeAction, position, data, deleteLength / 2);
758 }
759
760 BasicDeleteChars(position, deleteLength);
761 }
762 return data;
763 }
764
765 int CellBuffer::ByteLength() {
766 return length;
767 }
768
769 int CellBuffer::Length() {
770 return ByteLength() / 2;
771 }
772
773 int CellBuffer::Lines() {
774 //Platform::DebugPrintf("Lines = %d\n", lv.lines);
775 return lv.lines;
776 }
777
778 int CellBuffer::LineStart(int line) {
779 if (line < 0)
780 return 0;
781 else if (line > lv.lines)
782 return length;
783 else
784 return lv.linesData[line].startPosition;
785 }
786
787 bool CellBuffer::IsReadOnly() {
788 return readOnly;
789 }
790
791 void CellBuffer::SetReadOnly(bool set) {
792 readOnly = set;
793 }
794
795 void CellBuffer::SetSavePoint() {
796 uh.SetSavePoint();
797 }
798
799 bool CellBuffer::IsSavePoint() {
800 return uh.IsSavePoint();
801 }
802
803 int CellBuffer::AddMark(int line, int markerNum) {
804 if ((line >= 0) && (line < lv.lines)) {
805 return lv.AddMark(line, markerNum);
806 }
807 return - 1;
808 }
809
810 void CellBuffer::DeleteMark(int line, int markerNum) {
811 if ((line >= 0) && (line < lv.lines)) {
812 lv.DeleteMark(line, markerNum);
813 }
814 }
815
816 void CellBuffer::DeleteMarkFromHandle(int markerHandle) {
817 lv.DeleteMarkFromHandle(markerHandle);
818 }
819
820 int CellBuffer::GetMark(int line) {
821 if ((line >= 0) && (line < lv.lines) && (lv.linesData[line].handleSet))
822 return lv.linesData[line].handleSet->MarkValue();
823 return 0;
824 }
825
826 void CellBuffer::DeleteAllMarks(int markerNum) {
827 for (int line = 0; line < lv.lines; line++) {
828 lv.DeleteMark(line, markerNum);
829 }
830 }
831
832 int CellBuffer::LineFromHandle(int markerHandle) {
833 return lv.LineFromHandle(markerHandle);
834 }
835
836 // Without undo
837
838 void CellBuffer::BasicInsertString(int position, char *s, int insertLength) {
839 //Platform::DebugPrintf("Inserting at %d for %d\n", position, insertLength);
840 if (insertLength == 0)
841 return ;
842 RoomFor(insertLength);
843 GapTo(position);
844
845 memcpy(body + part1len, s, insertLength);
846 length += insertLength;
847 part1len += insertLength;
848 gaplen -= insertLength;
849 part2body = body + gaplen;
850
851 int lineInsert = lv.LineFromPosition(position / 2) + 1;
852 // Point all the lines after the insertion point further along in the buffer
853 for (int lineAfter = lineInsert; lineAfter <= lv.lines; lineAfter++) {
854 lv.linesData[lineAfter].startPosition += insertLength / 2;
855 }
856 char chPrev = ' ';
857 if ((position - 2) >= 0)
858 chPrev = ByteAt(position - 2);
859 char chAfter = ' ';
860 if ((position + insertLength) < length)
861 chAfter = ByteAt(position + insertLength);
862 if (chPrev == '\r' && chAfter == '\n') {
863 //Platform::DebugPrintf("Splitting a crlf pair at %d\n", lineInsert);
864 // Splitting up a crlf pair at position
865 lv.InsertValue(lineInsert, position / 2);
866 lineInsert++;
867 }
868 char ch = ' ';
869 for (int i = 0; i < insertLength; i += 2) {
870 ch = s[i];
871 if (ch == '\r') {
872 //Platform::DebugPrintf("Inserting cr at %d\n", lineInsert);
873 lv.InsertValue(lineInsert, (position + i) / 2 + 1);
874 lineInsert++;
875 } else if (ch == '\n') {
876 if (chPrev == '\r') {
877 //Platform::DebugPrintf("Patching cr before lf at %d\n", lineInsert-1);
878 // Patch up what was end of line
879 lv.SetValue(lineInsert - 1, (position + i) / 2 + 1);
880 } else {
881 //Platform::DebugPrintf("Inserting lf at %d\n", lineInsert);
882 lv.InsertValue(lineInsert, (position + i) / 2 + 1);
883 lineInsert++;
884 }
885 }
886 chPrev = ch;
887 }
888 // Joining two lines where last insertion is cr and following text starts with lf
889 if (chAfter == '\n') {
890 if (ch == '\r') {
891 //Platform::DebugPrintf("Joining cr before lf at %d\n", lineInsert-1);
892 // End of line already in buffer so drop the newly created one
893 lv.Remove(lineInsert - 1);
894 }
895 }
896 }
897
898 void CellBuffer::BasicDeleteChars(int position, int deleteLength) {
899 //Platform::DebugPrintf("Deleting at %d for %d\n", position, deleteLength);
900 if (deleteLength == 0)
901 return ;
902
903 if ((position == 0) && (deleteLength == length)) {
904 // If whole buffer is being deleted, faster to reinitialise lines data
905 // than to delete each line.
906 //printf("Whole buffer being deleted\n");
907 lv.Init();
908 } else {
909 // Have to fix up line positions before doing deletion as looking at text in buffer
910 // to work out which lines have been removed
911
912 int lineRemove = lv.LineFromPosition(position / 2) + 1;
913 // Point all the lines after the insertion point further along in the buffer
914 for (int lineAfter = lineRemove; lineAfter <= lv.lines; lineAfter++) {
915 lv.linesData[lineAfter].startPosition -= deleteLength / 2;
916 }
917 char chPrev = ' ';
918 if (position >= 2)
919 chPrev = ByteAt(position - 2);
920 char chBefore = chPrev;
921 char chNext = ' ';
922 if (position < length)
923 chNext = ByteAt(position);
924 bool ignoreNL = false;
925 if (chPrev == '\r' && chNext == '\n') {
926 //Platform::DebugPrintf("Deleting lf after cr, move line end to cr at %d\n", lineRemove);
927 // Move back one
928 lv.SetValue(lineRemove, position / 2);
929 lineRemove++;
930 ignoreNL = true; // First \n is not real deletion
931 }
932
933
934 char ch = chNext;
935 for (int i = 0; i < deleteLength; i += 2) {
936 chNext = ' ';
937 if ((position + i + 2) < length)
938 chNext = ByteAt(position + i + 2);
939 //Platform::DebugPrintf("Deleting %d %x\n", i, ch);
940 if (ch == '\r') {
941 if (chNext != '\n') {
942 //Platform::DebugPrintf("Removing cr end of line\n");
943 lv.Remove(lineRemove);
944 }
945 } else if ((ch == '\n') && !ignoreNL) {
946 //Platform::DebugPrintf("Removing lf end of line\n");
947 lv.Remove(lineRemove);
948 ignoreNL = false; // Further \n are not real deletions
949 }
950
951
952 ch = chNext;
953 }
954 // May have to fix up end if last deletion causes cr to be next to lf
955 // or removes one of a crlf pair
956 char chAfter = ' ';
957 if ((position + deleteLength) < length)
958 chAfter = ByteAt(position + deleteLength);
959 if (chBefore == '\r' && chAfter == '\n') {
960 //d.printf("Joining cr before lf at %d\n", lineRemove);
961 // Using lineRemove-1 as cr ended line before start of deletion
962 lv.Remove(lineRemove - 1);
963 lv.SetValue(lineRemove - 1, position / 2 + 1);
964 }
965 }
966 GapTo(position);
967 length -= deleteLength;
968 gaplen += deleteLength;
969 part2body = body + gaplen;
970 }
971
972 bool CellBuffer::SetUndoCollection(bool collectUndo) {
973 collectingUndo = collectUndo;
974 uh.DropUndoSequence();
975 return collectingUndo;
976 }
977
978 bool CellBuffer::IsCollectingUndo() {
979 return collectingUndo;
980 }
981
982 void CellBuffer::BeginUndoAction() {
983 uh.BeginUndoAction();
984 }
985
986 void CellBuffer::EndUndoAction() {
987 uh.EndUndoAction();
988 }
989
990 void CellBuffer::DeleteUndoHistory() {
991 uh.DeleteUndoHistory();
992 }
993
994 bool CellBuffer::CanUndo() {
995 return (!readOnly) && (uh.CanUndo());
996 }
997
998 int CellBuffer::StartUndo() {
999 return uh.StartUndo();
1000 }
1001
1002 const Action &CellBuffer::GetUndoStep() const {
1003 return uh.GetUndoStep();
1004 }
1005
1006 void CellBuffer::PerformUndoStep() {
1007 const Action &actionStep = uh.GetUndoStep();
1008 if (actionStep.at == insertAction) {
1009 BasicDeleteChars(actionStep.position, actionStep.lenData*2);
1010 } else if (actionStep.at == removeAction) {
1011 char *styledData = new char[actionStep.lenData * 2];
1012 for (int i = 0; i < actionStep.lenData; i++) {
1013 styledData[i*2] = actionStep.data[i];
1014 styledData[i*2 + 1] = 0;
1015 }
1016 BasicInsertString(actionStep.position, styledData, actionStep.lenData*2);
1017 delete []styledData;
1018 }
1019 uh.CompletedUndoStep();
1020 }
1021
1022 bool CellBuffer::CanRedo() {
1023 return (!readOnly) && (uh.CanRedo());
1024 }
1025
1026 int CellBuffer::StartRedo() {
1027 return uh.StartRedo();
1028 }
1029
1030 const Action &CellBuffer::GetRedoStep() const {
1031 return uh.GetRedoStep();
1032 }
1033
1034 void CellBuffer::PerformRedoStep() {
1035 const Action &actionStep = uh.GetRedoStep();
1036 if (actionStep.at == insertAction) {
1037 char *styledData = new char[actionStep.lenData * 2];
1038 for (int i = 0; i < actionStep.lenData; i++) {
1039 styledData[i*2] = actionStep.data[i];
1040 styledData[i*2 + 1] = 0;
1041 }
1042 BasicInsertString(actionStep.position, styledData, actionStep.lenData*2);
1043 delete []styledData;
1044 } else if (actionStep.at == removeAction) {
1045 BasicDeleteChars(actionStep.position, actionStep.lenData*2);
1046 }
1047 uh.CompletedRedoStep();
1048 }
1049
1050 int CellBuffer::SetLineState(int line, int state) {
1051 int stateOld = lineStates[line];
1052 lineStates[line] = state;
1053 return stateOld;
1054 }
1055
1056 int CellBuffer::GetLineState(int line) {
1057 return lineStates[line];
1058 }
1059
1060 int CellBuffer::GetMaxLineState() {
1061 return lineStates.Length();
1062 }
1063
1064 int CellBuffer::SetLevel(int line, int level) {
1065 int prev = 0;
1066 if ((line >= 0) && (line < lv.lines)) {
1067 if (!lv.levels) {
1068 lv.ExpandLevels();
1069 }
1070 prev = lv.levels[line];
1071 if (lv.levels[line] != level) {
1072 lv.levels[line] = level;
1073 }
1074 }
1075 return prev;
1076 }
1077
1078 int CellBuffer::GetLevel(int line) {
1079 if (lv.levels && (line >= 0) && (line < lv.lines)) {
1080 return lv.levels[line];
1081 } else {
1082 return SC_FOLDLEVELBASE;
1083 }
1084 }
1085
1086 void CellBuffer::ClearLevels() {
1087 lv.ClearLevels();
1088 }