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