]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/Document.cxx
wxMGL fixes (patch #884758)
[wxWidgets.git] / src / stc / scintilla / src / Document.cxx
1 // Scintilla source code edit control
2 /** @file Document.cxx
3 ** Text document that handles notifications, DBCS, styling, words and end of line.
4 **/
5 // Copyright 1998-2003 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
7
8 #include <stdlib.h>
9 #include <string.h>
10 #include <stdio.h>
11 #include <ctype.h>
12
13 #include "Platform.h"
14
15 #include "Scintilla.h"
16 #include "SVector.h"
17 #include "CellBuffer.h"
18 #include "Document.h"
19 #include "RESearch.h"
20
21 // This is ASCII specific but is safe with chars >= 0x80
22 static inline bool isspacechar(unsigned char ch) {
23 return (ch == ' ') || ((ch >= 0x09) && (ch <= 0x0d));
24 }
25
26 static inline bool IsPunctuation(char ch) {
27 return isascii(ch) && ispunct(ch);
28 }
29
30 static inline bool IsADigit(char ch) {
31 return isascii(ch) && isdigit(ch);
32 }
33
34 static inline bool IsLowerCase(char ch) {
35 return isascii(ch) && islower(ch);
36 }
37
38 static inline bool IsUpperCase(char ch) {
39 return isascii(ch) && isupper(ch);
40 }
41
42 Document::Document() {
43 refCount = 0;
44 #ifdef unix
45 eolMode = SC_EOL_LF;
46 #else
47 eolMode = SC_EOL_CRLF;
48 #endif
49 dbcsCodePage = 0;
50 stylingBits = 5;
51 stylingBitsMask = 0x1F;
52 stylingMask = 0;
53 SetWordChars(0);
54 endStyled = 0;
55 styleClock = 0;
56 enteredCount = 0;
57 enteredReadOnlyCount = 0;
58 tabInChars = 8;
59 indentInChars = 0;
60 useTabs = true;
61 tabIndents = true;
62 backspaceUnindents = false;
63 watchers = 0;
64 lenWatchers = 0;
65
66 matchesValid = false;
67 pre = 0;
68 substituted = 0;
69 }
70
71 Document::~Document() {
72 for (int i = 0; i < lenWatchers; i++) {
73 watchers[i].watcher->NotifyDeleted(this, watchers[i].userData);
74 }
75 delete []watchers;
76 watchers = 0;
77 lenWatchers = 0;
78 delete pre;
79 pre = 0;
80 delete []substituted;
81 substituted = 0;
82 }
83
84 // Increase reference count and return its previous value.
85 int Document::AddRef() {
86 return refCount++;
87 }
88
89 // Decrease reference count and return its previous value.
90 // Delete the document if reference count reaches zero.
91 int Document::Release() {
92 int curRefCount = --refCount;
93 if (curRefCount == 0)
94 delete this;
95 return curRefCount;
96 }
97
98 void Document::SetSavePoint() {
99 cb.SetSavePoint();
100 NotifySavePoint(true);
101 }
102
103 int Document::AddMark(int line, int markerNum) {
104 int prev = cb.AddMark(line, markerNum);
105 DocModification mh(SC_MOD_CHANGEMARKER, LineStart(line), 0, 0, 0);
106 NotifyModified(mh);
107 return prev;
108 }
109
110 void Document::DeleteMark(int line, int markerNum) {
111 cb.DeleteMark(line, markerNum);
112 DocModification mh(SC_MOD_CHANGEMARKER, LineStart(line), 0, 0, 0);
113 NotifyModified(mh);
114 }
115
116 void Document::DeleteMarkFromHandle(int markerHandle) {
117 cb.DeleteMarkFromHandle(markerHandle);
118 DocModification mh(SC_MOD_CHANGEMARKER, 0, 0, 0, 0);
119 NotifyModified(mh);
120 }
121
122 void Document::DeleteAllMarks(int markerNum) {
123 cb.DeleteAllMarks(markerNum);
124 DocModification mh(SC_MOD_CHANGEMARKER, 0, 0, 0, 0);
125 NotifyModified(mh);
126 }
127
128 int Document::LineStart(int line) {
129 return cb.LineStart(line);
130 }
131
132 int Document::LineEnd(int line) {
133 if (line == LinesTotal() - 1) {
134 return LineStart(line + 1);
135 } else {
136 int position = LineStart(line + 1) - 1;
137 // When line terminator is CR+LF, may need to go back one more
138 if ((position > LineStart(line)) && (cb.CharAt(position - 1) == '\r')) {
139 position--;
140 }
141 return position;
142 }
143 }
144
145 int Document::LineFromPosition(int pos) {
146 return cb.LineFromPosition(pos);
147 }
148
149 int Document::LineEndPosition(int position) {
150 return LineEnd(LineFromPosition(position));
151 }
152
153 int Document::VCHomePosition(int position) {
154 int line = LineFromPosition(position);
155 int startPosition = LineStart(line);
156 int endLine = LineStart(line + 1) - 1;
157 int startText = startPosition;
158 while (startText < endLine && (cb.CharAt(startText) == ' ' || cb.CharAt(startText) == '\t' ) )
159 startText++;
160 if (position == startText)
161 return startPosition;
162 else
163 return startText;
164 }
165
166 int Document::SetLevel(int line, int level) {
167 int prev = cb.SetLevel(line, level);
168 if (prev != level) {
169 DocModification mh(SC_MOD_CHANGEFOLD | SC_MOD_CHANGEMARKER,
170 LineStart(line), 0, 0, 0);
171 mh.line = line;
172 mh.foldLevelNow = level;
173 mh.foldLevelPrev = prev;
174 NotifyModified(mh);
175 }
176 return prev;
177 }
178
179 static bool IsSubordinate(int levelStart, int levelTry) {
180 if (levelTry & SC_FOLDLEVELWHITEFLAG)
181 return true;
182 else
183 return (levelStart & SC_FOLDLEVELNUMBERMASK) < (levelTry & SC_FOLDLEVELNUMBERMASK);
184 }
185
186 int Document::GetLastChild(int lineParent, int level) {
187 if (level == -1)
188 level = GetLevel(lineParent) & SC_FOLDLEVELNUMBERMASK;
189 int maxLine = LinesTotal();
190 int lineMaxSubord = lineParent;
191 while (lineMaxSubord < maxLine - 1) {
192 EnsureStyledTo(LineStart(lineMaxSubord + 2));
193 if (!IsSubordinate(level, GetLevel(lineMaxSubord + 1)))
194 break;
195 lineMaxSubord++;
196 }
197 if (lineMaxSubord > lineParent) {
198 if (level > (GetLevel(lineMaxSubord + 1) & SC_FOLDLEVELNUMBERMASK)) {
199 // Have chewed up some whitespace that belongs to a parent so seek back
200 if (GetLevel(lineMaxSubord) & SC_FOLDLEVELWHITEFLAG) {
201 lineMaxSubord--;
202 }
203 }
204 }
205 return lineMaxSubord;
206 }
207
208 int Document::GetFoldParent(int line) {
209 int level = GetLevel(line);
210 int lineLook = line - 1;
211 while ((lineLook > 0) && (
212 (!(GetLevel(lineLook) & SC_FOLDLEVELHEADERFLAG)) ||
213 ((GetLevel(lineLook) & SC_FOLDLEVELNUMBERMASK) >= level))
214 ) {
215 lineLook--;
216 }
217 if ((GetLevel(lineLook) & SC_FOLDLEVELHEADERFLAG) &&
218 ((GetLevel(lineLook) & SC_FOLDLEVELNUMBERMASK) < level)) {
219 return lineLook;
220 } else {
221 return -1;
222 }
223 }
224
225 int Document::ClampPositionIntoDocument(int pos) {
226 return Platform::Clamp(pos, 0, Length());
227 }
228
229 bool Document::IsCrLf(int pos) {
230 if (pos < 0)
231 return false;
232 if (pos >= (Length() - 1))
233 return false;
234 return (cb.CharAt(pos) == '\r') && (cb.CharAt(pos + 1) == '\n');
235 }
236
237 static const int maxBytesInDBCSCharacter=5;
238
239 int Document::LenChar(int pos) {
240 if (pos < 0) {
241 return 1;
242 } else if (IsCrLf(pos)) {
243 return 2;
244 } else if (SC_CP_UTF8 == dbcsCodePage) {
245 unsigned char ch = static_cast<unsigned char>(cb.CharAt(pos));
246 if (ch < 0x80)
247 return 1;
248 int len = 2;
249 if (ch >= (0x80 + 0x40 + 0x20))
250 len = 3;
251 int lengthDoc = Length();
252 if ((pos + len) > lengthDoc)
253 return lengthDoc -pos;
254 else
255 return len;
256 } else if (dbcsCodePage) {
257 char mbstr[maxBytesInDBCSCharacter+1];
258 int i;
259 for (i=0; i<Platform::DBCSCharMaxLength(); i++) {
260 mbstr[i] = cb.CharAt(pos+i);
261 }
262 mbstr[i] = '\0';
263 return Platform::DBCSCharLength(dbcsCodePage, mbstr);
264 } else {
265 return 1;
266 }
267 }
268 #include <assert.h>
269 // Normalise a position so that it is not halfway through a two byte character.
270 // This can occur in two situations -
271 // When lines are terminated with \r\n pairs which should be treated as one character.
272 // When displaying DBCS text such as Japanese.
273 // If moving, move the position in the indicated direction.
274 int Document::MovePositionOutsideChar(int pos, int moveDir, bool checkLineEnd) {
275 //Platform::DebugPrintf("NoCRLF %d %d\n", pos, moveDir);
276 // If out of range, just return minimum/maximum value.
277 if (pos <= 0)
278 return 0;
279 if (pos >= Length())
280 return Length();
281
282 // assert pos > 0 && pos < Length()
283 if (checkLineEnd && IsCrLf(pos - 1)) {
284 if (moveDir > 0)
285 return pos + 1;
286 else
287 return pos - 1;
288 }
289
290 // Not between CR and LF
291
292 if (dbcsCodePage) {
293 if (SC_CP_UTF8 == dbcsCodePage) {
294 unsigned char ch = static_cast<unsigned char>(cb.CharAt(pos));
295 while ((pos > 0) && (pos < Length()) && (ch >= 0x80) && (ch < (0x80 + 0x40))) {
296 // ch is a trail byte
297 if (moveDir > 0)
298 pos++;
299 else
300 pos--;
301 ch = static_cast<unsigned char>(cb.CharAt(pos));
302 }
303 } else {
304 // Anchor DBCS calculations at start of line because start of line can
305 // not be a DBCS trail byte.
306 int posCheck = LineStart(LineFromPosition(pos));
307 while (posCheck < pos) {
308 char mbstr[maxBytesInDBCSCharacter+1];
309 int i;
310 for(i=0;i<Platform::DBCSCharMaxLength();i++) {
311 mbstr[i] = cb.CharAt(posCheck+i);
312 }
313 mbstr[i] = '\0';
314
315 int mbsize = Platform::DBCSCharLength(dbcsCodePage, mbstr);
316 if (posCheck + mbsize == pos) {
317 return pos;
318 } else if (posCheck + mbsize > pos) {
319 if (moveDir > 0) {
320 return posCheck + mbsize;
321 } else {
322 return posCheck;
323 }
324 }
325 posCheck += mbsize;
326 }
327 }
328 }
329
330 return pos;
331 }
332
333 void Document::ModifiedAt(int pos) {
334 if (endStyled > pos)
335 endStyled = pos;
336 }
337
338 // Document only modified by gateways DeleteChars, InsertStyledString, Undo, Redo, and SetStyleAt.
339 // SetStyleAt does not change the persistent state of a document
340
341 // Unlike Undo, Redo, and InsertStyledString, the pos argument is a cell number not a char number
342 bool Document::DeleteChars(int pos, int len) {
343 if (len == 0)
344 return false;
345 if ((pos + len) > Length())
346 return false;
347 if (cb.IsReadOnly() && enteredReadOnlyCount == 0) {
348 enteredReadOnlyCount++;
349 NotifyModifyAttempt();
350 enteredReadOnlyCount--;
351 }
352 if (enteredCount != 0) {
353 return false;
354 } else {
355 enteredCount++;
356 if (!cb.IsReadOnly()) {
357 NotifyModified(
358 DocModification(
359 SC_MOD_BEFOREDELETE | SC_PERFORMED_USER,
360 pos, len,
361 0, 0));
362 int prevLinesTotal = LinesTotal();
363 bool startSavePoint = cb.IsSavePoint();
364 const char *text = cb.DeleteChars(pos * 2, len * 2);
365 if (startSavePoint && cb.IsCollectingUndo())
366 NotifySavePoint(!startSavePoint);
367 if ((pos < Length()) || (pos == 0))
368 ModifiedAt(pos);
369 else
370 ModifiedAt(pos-1);
371 NotifyModified(
372 DocModification(
373 SC_MOD_DELETETEXT | SC_PERFORMED_USER,
374 pos, len,
375 LinesTotal() - prevLinesTotal, text));
376 }
377 enteredCount--;
378 }
379 return !cb.IsReadOnly();
380 }
381
382 bool Document::InsertStyledString(int position, char *s, int insertLength) {
383 if (cb.IsReadOnly() && enteredReadOnlyCount == 0) {
384 enteredReadOnlyCount++;
385 NotifyModifyAttempt();
386 enteredReadOnlyCount--;
387 }
388 if (enteredCount != 0) {
389 return false;
390 } else {
391 enteredCount++;
392 if (!cb.IsReadOnly()) {
393 NotifyModified(
394 DocModification(
395 SC_MOD_BEFOREINSERT | SC_PERFORMED_USER,
396 position / 2, insertLength / 2,
397 0, s));
398 int prevLinesTotal = LinesTotal();
399 bool startSavePoint = cb.IsSavePoint();
400 const char *text = cb.InsertString(position, s, insertLength);
401 if (startSavePoint && cb.IsCollectingUndo())
402 NotifySavePoint(!startSavePoint);
403 ModifiedAt(position / 2);
404 NotifyModified(
405 DocModification(
406 SC_MOD_INSERTTEXT | SC_PERFORMED_USER,
407 position / 2, insertLength / 2,
408 LinesTotal() - prevLinesTotal, text));
409 }
410 enteredCount--;
411 }
412 return !cb.IsReadOnly();
413 }
414
415 int Document::Undo() {
416 int newPos = 0;
417 if (enteredCount == 0) {
418 enteredCount++;
419 bool startSavePoint = cb.IsSavePoint();
420 int steps = cb.StartUndo();
421 //Platform::DebugPrintf("Steps=%d\n", steps);
422 for (int step = 0; step < steps; step++) {
423 int prevLinesTotal = LinesTotal();
424 const Action &action = cb.GetUndoStep();
425 if (action.at == removeAction) {
426 NotifyModified(DocModification(
427 SC_MOD_BEFOREINSERT | SC_PERFORMED_UNDO, action));
428 } else {
429 NotifyModified(DocModification(
430 SC_MOD_BEFOREDELETE | SC_PERFORMED_UNDO, action));
431 }
432 cb.PerformUndoStep();
433 int cellPosition = action.position / 2;
434 ModifiedAt(cellPosition);
435 newPos = cellPosition;
436
437 int modFlags = SC_PERFORMED_UNDO;
438 // With undo, an insertion action becomes a deletion notification
439 if (action.at == removeAction) {
440 newPos += action.lenData;
441 modFlags |= SC_MOD_INSERTTEXT;
442 } else {
443 modFlags |= SC_MOD_DELETETEXT;
444 }
445 if (step == steps - 1)
446 modFlags |= SC_LASTSTEPINUNDOREDO;
447 NotifyModified(DocModification(modFlags, cellPosition, action.lenData,
448 LinesTotal() - prevLinesTotal, action.data));
449 }
450
451 bool endSavePoint = cb.IsSavePoint();
452 if (startSavePoint != endSavePoint)
453 NotifySavePoint(endSavePoint);
454 enteredCount--;
455 }
456 return newPos;
457 }
458
459 int Document::Redo() {
460 int newPos = 0;
461 if (enteredCount == 0) {
462 enteredCount++;
463 bool startSavePoint = cb.IsSavePoint();
464 int steps = cb.StartRedo();
465 for (int step = 0; step < steps; step++) {
466 int prevLinesTotal = LinesTotal();
467 const Action &action = cb.GetRedoStep();
468 if (action.at == insertAction) {
469 NotifyModified(DocModification(
470 SC_MOD_BEFOREINSERT | SC_PERFORMED_REDO, action));
471 } else {
472 NotifyModified(DocModification(
473 SC_MOD_BEFOREDELETE | SC_PERFORMED_REDO, action));
474 }
475 cb.PerformRedoStep();
476 ModifiedAt(action.position / 2);
477 newPos = action.position / 2;
478
479 int modFlags = SC_PERFORMED_REDO;
480 if (action.at == insertAction) {
481 newPos += action.lenData;
482 modFlags |= SC_MOD_INSERTTEXT;
483 } else {
484 modFlags |= SC_MOD_DELETETEXT;
485 }
486 if (step == steps - 1)
487 modFlags |= SC_LASTSTEPINUNDOREDO;
488 NotifyModified(
489 DocModification(modFlags, action.position / 2, action.lenData,
490 LinesTotal() - prevLinesTotal, action.data));
491 }
492
493 bool endSavePoint = cb.IsSavePoint();
494 if (startSavePoint != endSavePoint)
495 NotifySavePoint(endSavePoint);
496 enteredCount--;
497 }
498 return newPos;
499 }
500
501 bool Document::InsertChar(int pos, char ch) {
502 char chs[2];
503 chs[0] = ch;
504 chs[1] = 0;
505 return InsertStyledString(pos*2, chs, 2);
506 }
507
508 // Insert a null terminated string
509 bool Document::InsertString(int position, const char *s) {
510 return InsertString(position, s, strlen(s));
511 }
512
513 // Insert a string with a length
514 bool Document::InsertString(int position, const char *s, size_t insertLength) {
515 bool changed = false;
516 char *sWithStyle = new char[insertLength * 2];
517 if (sWithStyle) {
518 for (size_t i = 0; i < insertLength; i++) {
519 sWithStyle[i*2] = s[i];
520 sWithStyle[i*2 + 1] = 0;
521 }
522 changed = InsertStyledString(position*2, sWithStyle,
523 static_cast<int>(insertLength*2));
524 delete []sWithStyle;
525 }
526 return changed;
527 }
528
529 void Document::ChangeChar(int pos, char ch) {
530 DeleteChars(pos, 1);
531 InsertChar(pos, ch);
532 }
533
534 void Document::DelChar(int pos) {
535 DeleteChars(pos, LenChar(pos));
536 }
537
538 void Document::DelCharBack(int pos) {
539 if (pos <= 0) {
540 return;
541 } else if (IsCrLf(pos - 2)) {
542 DeleteChars(pos - 2, 2);
543 } else if (dbcsCodePage) {
544 int startChar = MovePositionOutsideChar(pos - 1, -1, false);
545 DeleteChars(startChar, pos - startChar);
546 } else {
547 DeleteChars(pos - 1, 1);
548 }
549 }
550
551 static bool isindentchar(char ch) {
552 return (ch == ' ') || (ch == '\t');
553 }
554
555 static int NextTab(int pos, int tabSize) {
556 return ((pos / tabSize) + 1) * tabSize;
557 }
558
559 static void CreateIndentation(char *linebuf, int length, int indent, int tabSize, bool insertSpaces) {
560 length--; // ensure space for \0
561 if (!insertSpaces) {
562 while ((indent >= tabSize) && (length > 0)) {
563 *linebuf++ = '\t';
564 indent -= tabSize;
565 length--;
566 }
567 }
568 while ((indent > 0) && (length > 0)) {
569 *linebuf++ = ' ';
570 indent--;
571 length--;
572 }
573 *linebuf = '\0';
574 }
575
576 int Document::GetLineIndentation(int line) {
577 int indent = 0;
578 if ((line >= 0) && (line < LinesTotal())) {
579 int lineStart = LineStart(line);
580 int length = Length();
581 for (int i = lineStart;i < length;i++) {
582 char ch = cb.CharAt(i);
583 if (ch == ' ')
584 indent++;
585 else if (ch == '\t')
586 indent = NextTab(indent, tabInChars);
587 else
588 return indent;
589 }
590 }
591 return indent;
592 }
593
594 void Document::SetLineIndentation(int line, int indent) {
595 int indentOfLine = GetLineIndentation(line);
596 if (indent < 0)
597 indent = 0;
598 if (indent != indentOfLine) {
599 char linebuf[1000];
600 CreateIndentation(linebuf, sizeof(linebuf), indent, tabInChars, !useTabs);
601 int thisLineStart = LineStart(line);
602 int indentPos = GetLineIndentPosition(line);
603 DeleteChars(thisLineStart, indentPos - thisLineStart);
604 InsertString(thisLineStart, linebuf);
605 }
606 }
607
608 int Document::GetLineIndentPosition(int line) {
609 if (line < 0)
610 return 0;
611 int pos = LineStart(line);
612 int length = Length();
613 while ((pos < length) && isindentchar(cb.CharAt(pos))) {
614 pos++;
615 }
616 return pos;
617 }
618
619 int Document::GetColumn(int pos) {
620 int column = 0;
621 int line = LineFromPosition(pos);
622 if ((line >= 0) && (line < LinesTotal())) {
623 for (int i = LineStart(line);i < pos;) {
624 char ch = cb.CharAt(i);
625 if (ch == '\t') {
626 column = NextTab(column, tabInChars);
627 i++;
628 } else if (ch == '\r') {
629 return column;
630 } else if (ch == '\n') {
631 return column;
632 } else {
633 column++;
634 i = MovePositionOutsideChar(i + 1, 1);
635 }
636 }
637 }
638 return column;
639 }
640
641 int Document::FindColumn(int line, int column) {
642 int position = LineStart(line);
643 int columnCurrent = 0;
644 if ((line >= 0) && (line < LinesTotal())) {
645 while (columnCurrent < column) {
646 char ch = cb.CharAt(position);
647 if (ch == '\t') {
648 columnCurrent = NextTab(columnCurrent, tabInChars);
649 position++;
650 } else if (ch == '\r') {
651 return position;
652 } else if (ch == '\n') {
653 return position;
654 } else {
655 columnCurrent++;
656 position = MovePositionOutsideChar(position + 1, 1);
657 }
658 }
659 }
660 return position;
661 }
662
663 void Document::Indent(bool forwards, int lineBottom, int lineTop) {
664 // Dedent - suck white space off the front of the line to dedent by equivalent of a tab
665 for (int line = lineBottom; line >= lineTop; line--) {
666 int indentOfLine = GetLineIndentation(line);
667 if (forwards)
668 SetLineIndentation(line, indentOfLine + IndentSize());
669 else
670 SetLineIndentation(line, indentOfLine - IndentSize());
671 }
672 }
673
674 void Document::ConvertLineEnds(int eolModeSet) {
675 BeginUndoAction();
676 for (int pos = 0; pos < Length(); pos++) {
677 if (cb.CharAt(pos) == '\r') {
678 if (cb.CharAt(pos + 1) == '\n') {
679 if (eolModeSet != SC_EOL_CRLF) {
680 DeleteChars(pos, 2);
681 if (eolModeSet == SC_EOL_CR)
682 InsertString(pos, "\r", 1);
683 else
684 InsertString(pos, "\n", 1);
685 } else {
686 pos++;
687 }
688 } else {
689 if (eolModeSet != SC_EOL_CR) {
690 DeleteChars(pos, 1);
691 if (eolModeSet == SC_EOL_CRLF) {
692 InsertString(pos, "\r\n", 2);
693 pos++;
694 } else {
695 InsertString(pos, "\n", 1);
696 }
697 }
698 }
699 } else if (cb.CharAt(pos) == '\n') {
700 if (eolModeSet != SC_EOL_LF) {
701 DeleteChars(pos, 1);
702 if (eolModeSet == SC_EOL_CRLF) {
703 InsertString(pos, "\r\n", 2);
704 pos++;
705 } else {
706 InsertString(pos, "\r", 1);
707 }
708 }
709 }
710 }
711 EndUndoAction();
712 }
713
714 int Document::ParaDown(int pos) {
715 int line = LineFromPosition(pos);
716 while (line < LinesTotal() && LineStart(line) != LineEnd(line)) { // skip non-empty lines
717 line++;
718 }
719 while (line < LinesTotal() && LineStart(line) == LineEnd(line)) { // skip empty lines
720 line++;
721 }
722 if (line < LinesTotal())
723 return LineStart(line);
724 else // end of a document
725 return LineEnd(line-1);
726 }
727
728 int Document::ParaUp(int pos) {
729 int line = LineFromPosition(pos);
730 line--;
731 while (line >= 0 && LineStart(line) == LineEnd(line)) { // skip empty lines
732 line--;
733 }
734 while (line >= 0 && LineStart(line) != LineEnd(line)) { // skip non-empty lines
735 line--;
736 }
737 line++;
738 return LineStart(line);
739 }
740
741 Document::charClassification Document::WordCharClass(unsigned char ch) {
742 if ((SC_CP_UTF8 == dbcsCodePage) && (ch >= 0x80))
743 return ccWord;
744 return charClass[ch];
745 }
746
747 /**
748 * Used by commmands that want to select whole words.
749 * Finds the start of word at pos when delta < 0 or the end of the word when delta >= 0.
750 */
751 int Document::ExtendWordSelect(int pos, int delta, bool onlyWordCharacters) {
752 charClassification ccStart = ccWord;
753 if (delta < 0) {
754 if (!onlyWordCharacters)
755 ccStart = WordCharClass(cb.CharAt(pos-1));
756 while (pos > 0 && (WordCharClass(cb.CharAt(pos - 1)) == ccStart))
757 pos--;
758 } else {
759 if (!onlyWordCharacters)
760 ccStart = WordCharClass(cb.CharAt(pos));
761 while (pos < (Length()) && (WordCharClass(cb.CharAt(pos)) == ccStart))
762 pos++;
763 }
764 return pos;
765 }
766
767 /**
768 * Find the start of the next word in either a forward (delta >= 0) or backwards direction
769 * (delta < 0).
770 * This is looking for a transition between character classes although there is also some
771 * additional movement to transit white space.
772 * Used by cursor movement by word commands.
773 */
774 int Document::NextWordStart(int pos, int delta) {
775 if (delta < 0) {
776 while (pos > 0 && (WordCharClass(cb.CharAt(pos - 1)) == ccSpace))
777 pos--;
778 if (pos > 0) {
779 charClassification ccStart = WordCharClass(cb.CharAt(pos-1));
780 while (pos > 0 && (WordCharClass(cb.CharAt(pos - 1)) == ccStart)) {
781 pos--;
782 }
783 }
784 } else {
785 charClassification ccStart = WordCharClass(cb.CharAt(pos));
786 while (pos < (Length()) && (WordCharClass(cb.CharAt(pos)) == ccStart))
787 pos++;
788 while (pos < (Length()) && (WordCharClass(cb.CharAt(pos)) == ccSpace))
789 pos++;
790 }
791 return pos;
792 }
793
794 /**
795 * Check that the character at the given position is a word or punctuation character and that
796 * the previous character is of a different character class.
797 */
798 bool Document::IsWordStartAt(int pos) {
799 if (pos > 0) {
800 charClassification ccPos = WordCharClass(CharAt(pos));
801 return (ccPos == ccWord || ccPos == ccPunctuation) &&
802 (ccPos != WordCharClass(CharAt(pos - 1)));
803 }
804 return true;
805 }
806
807 /**
808 * Check that the character at the given position is a word or punctuation character and that
809 * the next character is of a different character class.
810 */
811 bool Document::IsWordEndAt(int pos) {
812 if (pos < Length() - 1) {
813 charClassification ccPrev = WordCharClass(CharAt(pos-1));
814 return (ccPrev == ccWord || ccPrev == ccPunctuation) &&
815 (ccPrev != WordCharClass(CharAt(pos)));
816 }
817 return true;
818 }
819
820 /**
821 * Check that the given range is has transitions between character classes at both
822 * ends and where the characters on the inside are word or punctuation characters.
823 */
824 bool Document::IsWordAt(int start, int end) {
825 return IsWordStartAt(start) && IsWordEndAt(end);
826 }
827
828 // The comparison and case changing functions here assume ASCII
829 // or extended ASCII such as the normal Windows code page.
830
831 static inline char MakeUpperCase(char ch) {
832 if (ch < 'a' || ch > 'z')
833 return ch;
834 else
835 return static_cast<char>(ch - 'a' + 'A');
836 }
837
838 static inline char MakeLowerCase(char ch) {
839 if (ch < 'A' || ch > 'Z')
840 return ch;
841 else
842 return static_cast<char>(ch - 'A' + 'a');
843 }
844
845 // Define a way for the Regular Expression code to access the document
846 class DocumentIndexer : public CharacterIndexer {
847 Document *pdoc;
848 int end;
849 public:
850 DocumentIndexer(Document *pdoc_, int end_) :
851 pdoc(pdoc_), end(end_) {
852 }
853
854 virtual char CharAt(int index) {
855 if (index < 0 || index >= end)
856 return 0;
857 else
858 return pdoc->CharAt(index);
859 }
860 };
861
862 /**
863 * Find text in document, supporting both forward and backward
864 * searches (just pass minPos > maxPos to do a backward search)
865 * Has not been tested with backwards DBCS searches yet.
866 */
867 long Document::FindText(int minPos, int maxPos, const char *s,
868 bool caseSensitive, bool word, bool wordStart, bool regExp, bool posix,
869 int *length) {
870 if (regExp) {
871 if (!pre)
872 pre = new RESearch();
873 if (!pre)
874 return -1;
875
876 int increment = (minPos <= maxPos) ? 1 : -1;
877
878 int startPos = minPos;
879 int endPos = maxPos;
880
881 // Range endpoints should not be inside DBCS characters, but just in case, move them.
882 startPos = MovePositionOutsideChar(startPos, 1, false);
883 endPos = MovePositionOutsideChar(endPos, 1, false);
884
885 const char *errmsg = pre->Compile(s, *length, caseSensitive, posix);
886 if (errmsg) {
887 return -1;
888 }
889 // Find a variable in a property file: \$(\([A-Za-z0-9_.]+\))
890 // Replace first '.' with '-' in each property file variable reference:
891 // Search: \$(\([A-Za-z0-9_-]+\)\.\([A-Za-z0-9_.]+\))
892 // Replace: $(\1-\2)
893 int lineRangeStart = LineFromPosition(startPos);
894 int lineRangeEnd = LineFromPosition(endPos);
895 if ((increment == 1) &&
896 (startPos >= LineEnd(lineRangeStart)) &&
897 (lineRangeStart < lineRangeEnd)) {
898 // the start position is at end of line or between line end characters.
899 lineRangeStart++;
900 startPos = LineStart(lineRangeStart);
901 }
902 int pos = -1;
903 int lenRet = 0;
904 char searchEnd = s[*length - 1];
905 int lineRangeBreak = lineRangeEnd + increment;
906 for (int line = lineRangeStart; line != lineRangeBreak; line += increment) {
907 int startOfLine = LineStart(line);
908 int endOfLine = LineEnd(line);
909 if (increment == 1) {
910 if (line == lineRangeStart) {
911 if ((startPos != startOfLine) && (s[0] == '^'))
912 continue; // Can't match start of line if start position after start of line
913 startOfLine = startPos;
914 }
915 if (line == lineRangeEnd) {
916 if ((endPos != endOfLine) && (searchEnd == '$'))
917 continue; // Can't match end of line if end position before end of line
918 endOfLine = endPos;
919 }
920 } else {
921 if (line == lineRangeEnd) {
922 if ((endPos != startOfLine) && (s[0] == '^'))
923 continue; // Can't match start of line if end position after start of line
924 startOfLine = endPos;
925 }
926 if (line == lineRangeStart) {
927 if ((startPos != endOfLine) && (searchEnd == '$'))
928 continue; // Can't match end of line if start position before end of line
929 endOfLine = startPos+1;
930 }
931 }
932
933 DocumentIndexer di(this, endOfLine);
934 int success = pre->Execute(di, startOfLine, endOfLine);
935 if (success) {
936 pos = pre->bopat[0];
937 lenRet = pre->eopat[0] - pre->bopat[0];
938 if (increment == -1) {
939 // Check for the last match on this line.
940 int repetitions = 1000; // Break out of infinite loop
941 while (success && (pre->eopat[0] <= (endOfLine+1)) && (repetitions--)) {
942 success = pre->Execute(di, pos+1, endOfLine+1);
943 if (success) {
944 if (pre->eopat[0] <= (minPos+1)) {
945 pos = pre->bopat[0];
946 lenRet = pre->eopat[0] - pre->bopat[0];
947 } else {
948 success = 0;
949 }
950 }
951 }
952 }
953 break;
954 }
955 }
956 *length = lenRet;
957 return pos;
958
959 } else {
960
961 bool forward = minPos <= maxPos;
962 int increment = forward ? 1 : -1;
963
964 // Range endpoints should not be inside DBCS characters, but just in case, move them.
965 int startPos = MovePositionOutsideChar(minPos, increment, false);
966 int endPos = MovePositionOutsideChar(maxPos, increment, false);
967
968 // Compute actual search ranges needed
969 int lengthFind = *length;
970 if (lengthFind == -1)
971 lengthFind = static_cast<int>(strlen(s));
972 int endSearch = endPos;
973 if (startPos <= endPos) {
974 endSearch = endPos - lengthFind + 1;
975 }
976 //Platform::DebugPrintf("Find %d %d %s %d\n", startPos, endPos, ft->lpstrText, lengthFind);
977 char firstChar = s[0];
978 if (!caseSensitive)
979 firstChar = static_cast<char>(MakeUpperCase(firstChar));
980 int pos = startPos;
981 while (forward ? (pos < endSearch) : (pos >= endSearch)) {
982 char ch = CharAt(pos);
983 if (caseSensitive) {
984 if (ch == firstChar) {
985 bool found = true;
986 for (int posMatch = 1; posMatch < lengthFind && found; posMatch++) {
987 ch = CharAt(pos + posMatch);
988 if (ch != s[posMatch])
989 found = false;
990 }
991 if (found) {
992 if ((!word && !wordStart) ||
993 word && IsWordAt(pos, pos + lengthFind) ||
994 wordStart && IsWordStartAt(pos))
995 return pos;
996 }
997 }
998 } else {
999 if (MakeUpperCase(ch) == firstChar) {
1000 bool found = true;
1001 for (int posMatch = 1; posMatch < lengthFind && found; posMatch++) {
1002 ch = CharAt(pos + posMatch);
1003 if (MakeUpperCase(ch) != MakeUpperCase(s[posMatch]))
1004 found = false;
1005 }
1006 if (found) {
1007 if ((!word && !wordStart) ||
1008 word && IsWordAt(pos, pos + lengthFind) ||
1009 wordStart && IsWordStartAt(pos))
1010 return pos;
1011 }
1012 }
1013 }
1014 pos += increment;
1015 if (dbcsCodePage) {
1016 // Ensure trying to match from start of character
1017 pos = MovePositionOutsideChar(pos, increment, false);
1018 }
1019 }
1020 }
1021 //Platform::DebugPrintf("Not found\n");
1022 return -1;
1023 }
1024
1025 const char *Document::SubstituteByPosition(const char *text, int *length) {
1026 if (!pre)
1027 return 0;
1028 delete []substituted;
1029 substituted = 0;
1030 DocumentIndexer di(this, Length());
1031 if (!pre->GrabMatches(di))
1032 return 0;
1033 unsigned int lenResult = 0;
1034 for (int i = 0; i < *length; i++) {
1035 if ((text[i] == '\\') && (text[i + 1] >= '1' && text[i + 1] <= '9')) {
1036 unsigned int patNum = text[i + 1] - '0';
1037 lenResult += pre->eopat[patNum] - pre->bopat[patNum];
1038 i++;
1039 } else {
1040 lenResult++;
1041 }
1042 }
1043 substituted = new char[lenResult + 1];
1044 if (!substituted)
1045 return 0;
1046 char *o = substituted;
1047 for (int j = 0; j < *length; j++) {
1048 if ((text[j] == '\\') && (text[j + 1] >= '1' && text[j + 1] <= '9')) {
1049 unsigned int patNum = text[j + 1] - '0';
1050 unsigned int len = pre->eopat[patNum] - pre->bopat[patNum];
1051 if (pre->pat[patNum]) // Will be null if try for a match that did not occur
1052 memcpy(o, pre->pat[patNum], len);
1053 o += len;
1054 j++;
1055 } else {
1056 *o++ = text[j];
1057 }
1058 }
1059 *o = '\0';
1060 *length = lenResult;
1061 return substituted;
1062 }
1063
1064 int Document::LinesTotal() {
1065 return cb.Lines();
1066 }
1067
1068 void Document::ChangeCase(Range r, bool makeUpperCase) {
1069 for (int pos = r.start; pos < r.end; pos++) {
1070 int len = LenChar(pos);
1071 if (dbcsCodePage && (len > 1)) {
1072 pos += len;
1073 } else {
1074 char ch = CharAt(pos);
1075 if (makeUpperCase) {
1076 if (IsLowerCase(ch)) {
1077 ChangeChar(pos, static_cast<char>(MakeUpperCase(ch)));
1078 }
1079 } else {
1080 if (IsUpperCase(ch)) {
1081 ChangeChar(pos, static_cast<char>(MakeLowerCase(ch)));
1082 }
1083 }
1084 }
1085 }
1086 }
1087
1088 void Document::SetWordChars(unsigned char *chars) {
1089 int ch;
1090 for (ch = 0; ch < 256; ch++) {
1091 if (ch == '\r' || ch == '\n')
1092 charClass[ch] = ccNewLine;
1093 else if (ch < 0x20 || ch == ' ')
1094 charClass[ch] = ccSpace;
1095 else
1096 charClass[ch] = ccPunctuation;
1097 }
1098 if (chars) {
1099 while (*chars) {
1100 charClass[*chars] = ccWord;
1101 chars++;
1102 }
1103 } else {
1104 for (ch = 0; ch < 256; ch++) {
1105 if (ch >= 0x80 || isalnum(ch) || ch == '_')
1106 charClass[ch] = ccWord;
1107 }
1108 }
1109 }
1110
1111 void Document::SetStylingBits(int bits) {
1112 stylingBits = bits;
1113 stylingBitsMask = 0;
1114 for (int bit = 0; bit < stylingBits; bit++) {
1115 stylingBitsMask <<= 1;
1116 stylingBitsMask |= 1;
1117 }
1118 }
1119
1120 void Document::StartStyling(int position, char mask) {
1121 stylingMask = mask;
1122 endStyled = position;
1123 }
1124
1125 bool Document::SetStyleFor(int length, char style) {
1126 if (enteredCount != 0) {
1127 return false;
1128 } else {
1129 enteredCount++;
1130 style &= stylingMask;
1131 int prevEndStyled = endStyled;
1132 if (cb.SetStyleFor(endStyled, length, style, stylingMask)) {
1133 DocModification mh(SC_MOD_CHANGESTYLE | SC_PERFORMED_USER,
1134 prevEndStyled, length);
1135 NotifyModified(mh);
1136 }
1137 endStyled += length;
1138 enteredCount--;
1139 return true;
1140 }
1141 }
1142
1143 bool Document::SetStyles(int length, char *styles) {
1144 if (enteredCount != 0) {
1145 return false;
1146 } else {
1147 enteredCount++;
1148 int prevEndStyled = endStyled;
1149 bool didChange = false;
1150 int lastChange = 0;
1151 for (int iPos = 0; iPos < length; iPos++, endStyled++) {
1152 PLATFORM_ASSERT(endStyled < Length());
1153 if (cb.SetStyleAt(endStyled, styles[iPos], stylingMask)) {
1154 didChange = true;
1155 lastChange = iPos;
1156 }
1157 }
1158 if (didChange) {
1159 DocModification mh(SC_MOD_CHANGESTYLE | SC_PERFORMED_USER,
1160 prevEndStyled, lastChange);
1161 NotifyModified(mh);
1162 }
1163 enteredCount--;
1164 return true;
1165 }
1166 }
1167
1168 bool Document::EnsureStyledTo(int pos) {
1169 if (pos > GetEndStyled()) {
1170 styleClock++;
1171 if (styleClock > 0x100000) {
1172 styleClock = 0;
1173 }
1174 // Ask the watchers to style, and stop as soon as one responds.
1175 for (int i = 0; pos > GetEndStyled() && i < lenWatchers; i++) {
1176 watchers[i].watcher->NotifyStyleNeeded(this, watchers[i].userData, pos);
1177 }
1178 }
1179 return pos <= GetEndStyled();
1180 }
1181
1182 bool Document::AddWatcher(DocWatcher *watcher, void *userData) {
1183 for (int i = 0; i < lenWatchers; i++) {
1184 if ((watchers[i].watcher == watcher) &&
1185 (watchers[i].userData == userData))
1186 return false;
1187 }
1188 WatcherWithUserData *pwNew = new WatcherWithUserData[lenWatchers + 1];
1189 if (!pwNew)
1190 return false;
1191 for (int j = 0; j < lenWatchers; j++)
1192 pwNew[j] = watchers[j];
1193 pwNew[lenWatchers].watcher = watcher;
1194 pwNew[lenWatchers].userData = userData;
1195 delete []watchers;
1196 watchers = pwNew;
1197 lenWatchers++;
1198 return true;
1199 }
1200
1201 bool Document::RemoveWatcher(DocWatcher *watcher, void *userData) {
1202 for (int i = 0; i < lenWatchers; i++) {
1203 if ((watchers[i].watcher == watcher) &&
1204 (watchers[i].userData == userData)) {
1205 if (lenWatchers == 1) {
1206 delete []watchers;
1207 watchers = 0;
1208 lenWatchers = 0;
1209 } else {
1210 WatcherWithUserData *pwNew = new WatcherWithUserData[lenWatchers];
1211 if (!pwNew)
1212 return false;
1213 for (int j = 0; j < lenWatchers - 1; j++) {
1214 pwNew[j] = (j < i) ? watchers[j] : watchers[j + 1];
1215 }
1216 delete []watchers;
1217 watchers = pwNew;
1218 lenWatchers--;
1219 }
1220 return true;
1221 }
1222 }
1223 return false;
1224 }
1225
1226 void Document::NotifyModifyAttempt() {
1227 for (int i = 0; i < lenWatchers; i++) {
1228 watchers[i].watcher->NotifyModifyAttempt(this, watchers[i].userData);
1229 }
1230 }
1231
1232 void Document::NotifySavePoint(bool atSavePoint) {
1233 for (int i = 0; i < lenWatchers; i++) {
1234 watchers[i].watcher->NotifySavePoint(this, watchers[i].userData, atSavePoint);
1235 }
1236 }
1237
1238 void Document::NotifyModified(DocModification mh) {
1239 for (int i = 0; i < lenWatchers; i++) {
1240 watchers[i].watcher->NotifyModified(this, mh, watchers[i].userData);
1241 }
1242 }
1243
1244 bool Document::IsWordPartSeparator(char ch) {
1245 return (WordCharClass(ch) == ccWord) && IsPunctuation(ch);
1246 }
1247
1248 int Document::WordPartLeft(int pos) {
1249 if (pos > 0) {
1250 --pos;
1251 char startChar = cb.CharAt(pos);
1252 if (IsWordPartSeparator(startChar)) {
1253 while (pos > 0 && IsWordPartSeparator(cb.CharAt(pos))) {
1254 --pos;
1255 }
1256 }
1257 if (pos > 0) {
1258 startChar = cb.CharAt(pos);
1259 --pos;
1260 if (IsLowerCase(startChar)) {
1261 while (pos > 0 && IsLowerCase(cb.CharAt(pos)))
1262 --pos;
1263 if (!IsUpperCase(cb.CharAt(pos)) && !IsLowerCase(cb.CharAt(pos)))
1264 ++pos;
1265 } else if (IsUpperCase(startChar)) {
1266 while (pos > 0 && IsUpperCase(cb.CharAt(pos)))
1267 --pos;
1268 if (!IsUpperCase(cb.CharAt(pos)))
1269 ++pos;
1270 } else if (IsADigit(startChar)) {
1271 while (pos > 0 && IsADigit(cb.CharAt(pos)))
1272 --pos;
1273 if (!IsADigit(cb.CharAt(pos)))
1274 ++pos;
1275 } else if (IsPunctuation(startChar)) {
1276 while (pos > 0 && IsPunctuation(cb.CharAt(pos)))
1277 --pos;
1278 if (!IsPunctuation(cb.CharAt(pos)))
1279 ++pos;
1280 } else if (isspacechar(startChar)) {
1281 while (pos > 0 && isspacechar(cb.CharAt(pos)))
1282 --pos;
1283 if (!isspacechar(cb.CharAt(pos)))
1284 ++pos;
1285 } else if (!isascii(startChar)) {
1286 while (pos > 0 && !isascii(cb.CharAt(pos)))
1287 --pos;
1288 if (isascii(cb.CharAt(pos)))
1289 ++pos;
1290 } else {
1291 ++pos;
1292 }
1293 }
1294 }
1295 return pos;
1296 }
1297
1298 int Document::WordPartRight(int pos) {
1299 char startChar = cb.CharAt(pos);
1300 int length = Length();
1301 if (IsWordPartSeparator(startChar)) {
1302 while (pos < length && IsWordPartSeparator(cb.CharAt(pos)))
1303 ++pos;
1304 startChar = cb.CharAt(pos);
1305 }
1306 if (!isascii(startChar)) {
1307 while (pos < length && !isascii(cb.CharAt(pos)))
1308 ++pos;
1309 } else if (IsLowerCase(startChar)) {
1310 while (pos < length && IsLowerCase(cb.CharAt(pos)))
1311 ++pos;
1312 } else if (IsUpperCase(startChar)) {
1313 if (IsLowerCase(cb.CharAt(pos + 1))) {
1314 ++pos;
1315 while (pos < length && IsLowerCase(cb.CharAt(pos)))
1316 ++pos;
1317 } else {
1318 while (pos < length && IsUpperCase(cb.CharAt(pos)))
1319 ++pos;
1320 }
1321 if (IsLowerCase(cb.CharAt(pos)) && IsUpperCase(cb.CharAt(pos - 1)))
1322 --pos;
1323 } else if (IsADigit(startChar)) {
1324 while (pos < length && IsADigit(cb.CharAt(pos)))
1325 ++pos;
1326 } else if (IsPunctuation(startChar)) {
1327 while (pos < length && IsPunctuation(cb.CharAt(pos)))
1328 ++pos;
1329 } else if (isspacechar(startChar)) {
1330 while (pos < length && isspacechar(cb.CharAt(pos)))
1331 ++pos;
1332 } else {
1333 ++pos;
1334 }
1335 return pos;
1336 }
1337
1338 int Document::ExtendStyleRange(int pos, int delta) {
1339 int sStart = cb.StyleAt(pos);
1340 if (delta < 0) {
1341 while (pos > 0 && (cb.StyleAt(pos) == sStart))
1342 pos--;
1343 pos++;
1344 } else {
1345 while (pos < (Length()) && (cb.StyleAt(pos) == sStart))
1346 pos++;
1347 }
1348 return pos;
1349 }