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