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