]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/Document.cxx
fb28144dd7d40021f9d97dd20e5b9bc53e009077
[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 SetDefaultCharClasses();
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 MovePositionOutsideChar(pos, delta);
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 * Find the end of the next word in either a forward (delta >= 0) or backwards direction
796 * (delta < 0).
797 * This is looking for a transition between character classes although there is also some
798 * additional movement to transit white space.
799 * Used by cursor movement by word commands.
800 */
801 int Document::NextWordEnd(int pos, int delta) {
802 if (delta < 0) {
803 if (pos > 0) {
804 charClassification ccStart = WordCharClass(cb.CharAt(pos-1));
805 if (ccStart != ccSpace) {
806 while (pos > 0 && WordCharClass(cb.CharAt(pos - 1)) == ccStart) {
807 pos--;
808 }
809 }
810 while (pos > 0 && WordCharClass(cb.CharAt(pos - 1)) == ccSpace) {
811 pos--;
812 }
813 }
814 } else {
815 while (pos < Length() && WordCharClass(cb.CharAt(pos)) == ccSpace) {
816 pos++;
817 }
818 if (pos < Length()) {
819 charClassification ccStart = WordCharClass(cb.CharAt(pos));
820 while (pos < Length() && WordCharClass(cb.CharAt(pos)) == ccStart) {
821 pos++;
822 }
823 }
824 }
825 return pos;
826 }
827
828 /**
829 * Check that the character at the given position is a word or punctuation character and that
830 * the previous character is of a different character class.
831 */
832 bool Document::IsWordStartAt(int pos) {
833 if (pos > 0) {
834 charClassification ccPos = WordCharClass(CharAt(pos));
835 return (ccPos == ccWord || ccPos == ccPunctuation) &&
836 (ccPos != WordCharClass(CharAt(pos - 1)));
837 }
838 return true;
839 }
840
841 /**
842 * Check that the character at the given position is a word or punctuation character and that
843 * the next character is of a different character class.
844 */
845 bool Document::IsWordEndAt(int pos) {
846 if (pos < Length() - 1) {
847 charClassification ccPrev = WordCharClass(CharAt(pos-1));
848 return (ccPrev == ccWord || ccPrev == ccPunctuation) &&
849 (ccPrev != WordCharClass(CharAt(pos)));
850 }
851 return true;
852 }
853
854 /**
855 * Check that the given range is has transitions between character classes at both
856 * ends and where the characters on the inside are word or punctuation characters.
857 */
858 bool Document::IsWordAt(int start, int end) {
859 return IsWordStartAt(start) && IsWordEndAt(end);
860 }
861
862 // The comparison and case changing functions here assume ASCII
863 // or extended ASCII such as the normal Windows code page.
864
865 static inline char MakeUpperCase(char ch) {
866 if (ch < 'a' || ch > 'z')
867 return ch;
868 else
869 return static_cast<char>(ch - 'a' + 'A');
870 }
871
872 static inline char MakeLowerCase(char ch) {
873 if (ch < 'A' || ch > 'Z')
874 return ch;
875 else
876 return static_cast<char>(ch - 'A' + 'a');
877 }
878
879 // Define a way for the Regular Expression code to access the document
880 class DocumentIndexer : public CharacterIndexer {
881 Document *pdoc;
882 int end;
883 public:
884 DocumentIndexer(Document *pdoc_, int end_) :
885 pdoc(pdoc_), end(end_) {
886 }
887
888 virtual char CharAt(int index) {
889 if (index < 0 || index >= end)
890 return 0;
891 else
892 return pdoc->CharAt(index);
893 }
894 };
895
896 /**
897 * Find text in document, supporting both forward and backward
898 * searches (just pass minPos > maxPos to do a backward search)
899 * Has not been tested with backwards DBCS searches yet.
900 */
901 long Document::FindText(int minPos, int maxPos, const char *s,
902 bool caseSensitive, bool word, bool wordStart, bool regExp, bool posix,
903 int *length) {
904 if (regExp) {
905 if (!pre)
906 pre = new RESearch();
907 if (!pre)
908 return -1;
909
910 int increment = (minPos <= maxPos) ? 1 : -1;
911
912 int startPos = minPos;
913 int endPos = maxPos;
914
915 // Range endpoints should not be inside DBCS characters, but just in case, move them.
916 startPos = MovePositionOutsideChar(startPos, 1, false);
917 endPos = MovePositionOutsideChar(endPos, 1, false);
918
919 const char *errmsg = pre->Compile(s, *length, caseSensitive, posix);
920 if (errmsg) {
921 return -1;
922 }
923 // Find a variable in a property file: \$(\([A-Za-z0-9_.]+\))
924 // Replace first '.' with '-' in each property file variable reference:
925 // Search: \$(\([A-Za-z0-9_-]+\)\.\([A-Za-z0-9_.]+\))
926 // Replace: $(\1-\2)
927 int lineRangeStart = LineFromPosition(startPos);
928 int lineRangeEnd = LineFromPosition(endPos);
929 if ((increment == 1) &&
930 (startPos >= LineEnd(lineRangeStart)) &&
931 (lineRangeStart < lineRangeEnd)) {
932 // the start position is at end of line or between line end characters.
933 lineRangeStart++;
934 startPos = LineStart(lineRangeStart);
935 }
936 int pos = -1;
937 int lenRet = 0;
938 char searchEnd = s[*length - 1];
939 int lineRangeBreak = lineRangeEnd + increment;
940 for (int line = lineRangeStart; line != lineRangeBreak; line += increment) {
941 int startOfLine = LineStart(line);
942 int endOfLine = LineEnd(line);
943 if (increment == 1) {
944 if (line == lineRangeStart) {
945 if ((startPos != startOfLine) && (s[0] == '^'))
946 continue; // Can't match start of line if start position after start of line
947 startOfLine = startPos;
948 }
949 if (line == lineRangeEnd) {
950 if ((endPos != endOfLine) && (searchEnd == '$'))
951 continue; // Can't match end of line if end position before end of line
952 endOfLine = endPos;
953 }
954 } else {
955 if (line == lineRangeEnd) {
956 if ((endPos != startOfLine) && (s[0] == '^'))
957 continue; // Can't match start of line if end position after start of line
958 startOfLine = endPos;
959 }
960 if (line == lineRangeStart) {
961 if ((startPos != endOfLine) && (searchEnd == '$'))
962 continue; // Can't match end of line if start position before end of line
963 endOfLine = startPos+1;
964 }
965 }
966
967 DocumentIndexer di(this, endOfLine);
968 int success = pre->Execute(di, startOfLine, endOfLine);
969 if (success) {
970 pos = pre->bopat[0];
971 lenRet = pre->eopat[0] - pre->bopat[0];
972 if (increment == -1) {
973 // Check for the last match on this line.
974 int repetitions = 1000; // Break out of infinite loop
975 while (success && (pre->eopat[0] <= (endOfLine+1)) && (repetitions--)) {
976 success = pre->Execute(di, pos+1, endOfLine+1);
977 if (success) {
978 if (pre->eopat[0] <= (minPos+1)) {
979 pos = pre->bopat[0];
980 lenRet = pre->eopat[0] - pre->bopat[0];
981 } else {
982 success = 0;
983 }
984 }
985 }
986 }
987 break;
988 }
989 }
990 *length = lenRet;
991 return pos;
992
993 } else {
994
995 bool forward = minPos <= maxPos;
996 int increment = forward ? 1 : -1;
997
998 // Range endpoints should not be inside DBCS characters, but just in case, move them.
999 int startPos = MovePositionOutsideChar(minPos, increment, false);
1000 int endPos = MovePositionOutsideChar(maxPos, increment, false);
1001
1002 // Compute actual search ranges needed
1003 int lengthFind = *length;
1004 if (lengthFind == -1)
1005 lengthFind = static_cast<int>(strlen(s));
1006 int endSearch = endPos;
1007 if (startPos <= endPos) {
1008 endSearch = endPos - lengthFind + 1;
1009 }
1010 //Platform::DebugPrintf("Find %d %d %s %d\n", startPos, endPos, ft->lpstrText, lengthFind);
1011 char firstChar = s[0];
1012 if (!caseSensitive)
1013 firstChar = static_cast<char>(MakeUpperCase(firstChar));
1014 int pos = startPos;
1015 while (forward ? (pos < endSearch) : (pos >= endSearch)) {
1016 char ch = CharAt(pos);
1017 if (caseSensitive) {
1018 if (ch == firstChar) {
1019 bool found = true;
1020 for (int posMatch = 1; posMatch < lengthFind && found; posMatch++) {
1021 ch = CharAt(pos + posMatch);
1022 if (ch != s[posMatch])
1023 found = false;
1024 }
1025 if (found) {
1026 if ((!word && !wordStart) ||
1027 word && IsWordAt(pos, pos + lengthFind) ||
1028 wordStart && IsWordStartAt(pos))
1029 return pos;
1030 }
1031 }
1032 } else {
1033 if (MakeUpperCase(ch) == firstChar) {
1034 bool found = true;
1035 for (int posMatch = 1; posMatch < lengthFind && found; posMatch++) {
1036 ch = CharAt(pos + posMatch);
1037 if (MakeUpperCase(ch) != MakeUpperCase(s[posMatch]))
1038 found = false;
1039 }
1040 if (found) {
1041 if ((!word && !wordStart) ||
1042 word && IsWordAt(pos, pos + lengthFind) ||
1043 wordStart && IsWordStartAt(pos))
1044 return pos;
1045 }
1046 }
1047 }
1048 pos += increment;
1049 if (dbcsCodePage && (pos >= 0)) {
1050 // Ensure trying to match from start of character
1051 pos = MovePositionOutsideChar(pos, increment, false);
1052 }
1053 }
1054 }
1055 //Platform::DebugPrintf("Not found\n");
1056 return -1;
1057 }
1058
1059 const char *Document::SubstituteByPosition(const char *text, int *length) {
1060 if (!pre)
1061 return 0;
1062 delete []substituted;
1063 substituted = 0;
1064 DocumentIndexer di(this, Length());
1065 if (!pre->GrabMatches(di))
1066 return 0;
1067 unsigned int lenResult = 0;
1068 for (int i = 0; i < *length; i++) {
1069 if (text[i] == '\\') {
1070 if (text[i + 1] >= '1' && text[i + 1] <= '9') {
1071 unsigned int patNum = text[i + 1] - '0';
1072 lenResult += pre->eopat[patNum] - pre->bopat[patNum];
1073 i++;
1074 } else {
1075 switch (text[i + 1]) {
1076 case 'a':
1077 case 'b':
1078 case 'f':
1079 case 'n':
1080 case 'r':
1081 case 't':
1082 case 'v':
1083 i++;
1084 }
1085 lenResult++;
1086 }
1087 } else {
1088 lenResult++;
1089 }
1090 }
1091 substituted = new char[lenResult + 1];
1092 if (!substituted)
1093 return 0;
1094 char *o = substituted;
1095 for (int j = 0; j < *length; j++) {
1096 if (text[j] == '\\') {
1097 if (text[j + 1] >= '1' && text[j + 1] <= '9') {
1098 unsigned int patNum = text[j + 1] - '0';
1099 unsigned int len = pre->eopat[patNum] - pre->bopat[patNum];
1100 if (pre->pat[patNum]) // Will be null if try for a match that did not occur
1101 memcpy(o, pre->pat[patNum], len);
1102 o += len;
1103 j++;
1104 } else {
1105 j++;
1106 switch (text[j]) {
1107 case 'a':
1108 *o++ = '\a';
1109 break;
1110 case 'b':
1111 *o++ = '\b';
1112 break;
1113 case 'f':
1114 *o++ = '\f';
1115 break;
1116 case 'n':
1117 *o++ = '\n';
1118 break;
1119 case 'r':
1120 *o++ = '\r';
1121 break;
1122 case 't':
1123 *o++ = '\t';
1124 break;
1125 case 'v':
1126 *o++ = '\v';
1127 break;
1128 default:
1129 *o++ = '\\';
1130 j--;
1131 }
1132 }
1133 } else {
1134 *o++ = text[j];
1135 }
1136 }
1137 *o = '\0';
1138 *length = lenResult;
1139 return substituted;
1140 }
1141
1142 int Document::LinesTotal() {
1143 return cb.Lines();
1144 }
1145
1146 void Document::ChangeCase(Range r, bool makeUpperCase) {
1147 for (int pos = r.start; pos < r.end; pos++) {
1148 int len = LenChar(pos);
1149 if (dbcsCodePage && (len > 1)) {
1150 pos += len;
1151 } else {
1152 char ch = CharAt(pos);
1153 if (makeUpperCase) {
1154 if (IsLowerCase(ch)) {
1155 ChangeChar(pos, static_cast<char>(MakeUpperCase(ch)));
1156 }
1157 } else {
1158 if (IsUpperCase(ch)) {
1159 ChangeChar(pos, static_cast<char>(MakeLowerCase(ch)));
1160 }
1161 }
1162 }
1163 }
1164 }
1165
1166 void Document::SetDefaultCharClasses() {
1167 // Initialize all char classes to default values
1168 for (int ch = 0; ch < 256; ch++) {
1169 if (ch == '\r' || ch == '\n')
1170 charClass[ch] = ccNewLine;
1171 else if (ch < 0x20 || ch == ' ')
1172 charClass[ch] = ccSpace;
1173 else if (ch >= 0x80 || isalnum(ch) || ch == '_')
1174 charClass[ch] = ccWord;
1175 else
1176 charClass[ch] = ccPunctuation;
1177 }
1178 }
1179
1180 void Document::SetCharClasses(unsigned char *chars, charClassification newCharClass) {
1181 // Apply the newCharClass to the specifed chars
1182 if (chars) {
1183 while (*chars) {
1184 charClass[*chars] = newCharClass;
1185 chars++;
1186 }
1187 }
1188 }
1189
1190 void Document::SetStylingBits(int bits) {
1191 stylingBits = bits;
1192 stylingBitsMask = 0;
1193 for (int bit = 0; bit < stylingBits; bit++) {
1194 stylingBitsMask <<= 1;
1195 stylingBitsMask |= 1;
1196 }
1197 }
1198
1199 void Document::StartStyling(int position, char mask) {
1200 stylingMask = mask;
1201 endStyled = position;
1202 }
1203
1204 bool Document::SetStyleFor(int length, char style) {
1205 if (enteredCount != 0) {
1206 return false;
1207 } else {
1208 enteredCount++;
1209 style &= stylingMask;
1210 int prevEndStyled = endStyled;
1211 if (cb.SetStyleFor(endStyled, length, style, stylingMask)) {
1212 DocModification mh(SC_MOD_CHANGESTYLE | SC_PERFORMED_USER,
1213 prevEndStyled, length);
1214 NotifyModified(mh);
1215 }
1216 endStyled += length;
1217 enteredCount--;
1218 return true;
1219 }
1220 }
1221
1222 bool Document::SetStyles(int length, char *styles) {
1223 if (enteredCount != 0) {
1224 return false;
1225 } else {
1226 enteredCount++;
1227 int prevEndStyled = endStyled;
1228 bool didChange = false;
1229 int lastChange = 0;
1230 for (int iPos = 0; iPos < length; iPos++, endStyled++) {
1231 PLATFORM_ASSERT(endStyled < Length());
1232 if (cb.SetStyleAt(endStyled, styles[iPos], stylingMask)) {
1233 didChange = true;
1234 lastChange = iPos;
1235 }
1236 }
1237 if (didChange) {
1238 DocModification mh(SC_MOD_CHANGESTYLE | SC_PERFORMED_USER,
1239 prevEndStyled, lastChange);
1240 NotifyModified(mh);
1241 }
1242 enteredCount--;
1243 return true;
1244 }
1245 }
1246
1247 bool Document::EnsureStyledTo(int pos) {
1248 if (pos > GetEndStyled()) {
1249 styleClock++;
1250 if (styleClock > 0x100000) {
1251 styleClock = 0;
1252 }
1253 // Ask the watchers to style, and stop as soon as one responds.
1254 for (int i = 0; pos > GetEndStyled() && i < lenWatchers; i++) {
1255 watchers[i].watcher->NotifyStyleNeeded(this, watchers[i].userData, pos);
1256 }
1257 }
1258 return pos <= GetEndStyled();
1259 }
1260
1261 bool Document::AddWatcher(DocWatcher *watcher, void *userData) {
1262 for (int i = 0; i < lenWatchers; i++) {
1263 if ((watchers[i].watcher == watcher) &&
1264 (watchers[i].userData == userData))
1265 return false;
1266 }
1267 WatcherWithUserData *pwNew = new WatcherWithUserData[lenWatchers + 1];
1268 if (!pwNew)
1269 return false;
1270 for (int j = 0; j < lenWatchers; j++)
1271 pwNew[j] = watchers[j];
1272 pwNew[lenWatchers].watcher = watcher;
1273 pwNew[lenWatchers].userData = userData;
1274 delete []watchers;
1275 watchers = pwNew;
1276 lenWatchers++;
1277 return true;
1278 }
1279
1280 bool Document::RemoveWatcher(DocWatcher *watcher, void *userData) {
1281 for (int i = 0; i < lenWatchers; i++) {
1282 if ((watchers[i].watcher == watcher) &&
1283 (watchers[i].userData == userData)) {
1284 if (lenWatchers == 1) {
1285 delete []watchers;
1286 watchers = 0;
1287 lenWatchers = 0;
1288 } else {
1289 WatcherWithUserData *pwNew = new WatcherWithUserData[lenWatchers];
1290 if (!pwNew)
1291 return false;
1292 for (int j = 0; j < lenWatchers - 1; j++) {
1293 pwNew[j] = (j < i) ? watchers[j] : watchers[j + 1];
1294 }
1295 delete []watchers;
1296 watchers = pwNew;
1297 lenWatchers--;
1298 }
1299 return true;
1300 }
1301 }
1302 return false;
1303 }
1304
1305 void Document::NotifyModifyAttempt() {
1306 for (int i = 0; i < lenWatchers; i++) {
1307 watchers[i].watcher->NotifyModifyAttempt(this, watchers[i].userData);
1308 }
1309 }
1310
1311 void Document::NotifySavePoint(bool atSavePoint) {
1312 for (int i = 0; i < lenWatchers; i++) {
1313 watchers[i].watcher->NotifySavePoint(this, watchers[i].userData, atSavePoint);
1314 }
1315 }
1316
1317 void Document::NotifyModified(DocModification mh) {
1318 for (int i = 0; i < lenWatchers; i++) {
1319 watchers[i].watcher->NotifyModified(this, mh, watchers[i].userData);
1320 }
1321 }
1322
1323 bool Document::IsWordPartSeparator(char ch) {
1324 return (WordCharClass(ch) == ccWord) && IsPunctuation(ch);
1325 }
1326
1327 int Document::WordPartLeft(int pos) {
1328 if (pos > 0) {
1329 --pos;
1330 char startChar = cb.CharAt(pos);
1331 if (IsWordPartSeparator(startChar)) {
1332 while (pos > 0 && IsWordPartSeparator(cb.CharAt(pos))) {
1333 --pos;
1334 }
1335 }
1336 if (pos > 0) {
1337 startChar = cb.CharAt(pos);
1338 --pos;
1339 if (IsLowerCase(startChar)) {
1340 while (pos > 0 && IsLowerCase(cb.CharAt(pos)))
1341 --pos;
1342 if (!IsUpperCase(cb.CharAt(pos)) && !IsLowerCase(cb.CharAt(pos)))
1343 ++pos;
1344 } else if (IsUpperCase(startChar)) {
1345 while (pos > 0 && IsUpperCase(cb.CharAt(pos)))
1346 --pos;
1347 if (!IsUpperCase(cb.CharAt(pos)))
1348 ++pos;
1349 } else if (IsADigit(startChar)) {
1350 while (pos > 0 && IsADigit(cb.CharAt(pos)))
1351 --pos;
1352 if (!IsADigit(cb.CharAt(pos)))
1353 ++pos;
1354 } else if (IsPunctuation(startChar)) {
1355 while (pos > 0 && IsPunctuation(cb.CharAt(pos)))
1356 --pos;
1357 if (!IsPunctuation(cb.CharAt(pos)))
1358 ++pos;
1359 } else if (isspacechar(startChar)) {
1360 while (pos > 0 && isspacechar(cb.CharAt(pos)))
1361 --pos;
1362 if (!isspacechar(cb.CharAt(pos)))
1363 ++pos;
1364 } else if (!isascii(startChar)) {
1365 while (pos > 0 && !isascii(cb.CharAt(pos)))
1366 --pos;
1367 if (isascii(cb.CharAt(pos)))
1368 ++pos;
1369 } else {
1370 ++pos;
1371 }
1372 }
1373 }
1374 return pos;
1375 }
1376
1377 int Document::WordPartRight(int pos) {
1378 char startChar = cb.CharAt(pos);
1379 int length = Length();
1380 if (IsWordPartSeparator(startChar)) {
1381 while (pos < length && IsWordPartSeparator(cb.CharAt(pos)))
1382 ++pos;
1383 startChar = cb.CharAt(pos);
1384 }
1385 if (!isascii(startChar)) {
1386 while (pos < length && !isascii(cb.CharAt(pos)))
1387 ++pos;
1388 } else if (IsLowerCase(startChar)) {
1389 while (pos < length && IsLowerCase(cb.CharAt(pos)))
1390 ++pos;
1391 } else if (IsUpperCase(startChar)) {
1392 if (IsLowerCase(cb.CharAt(pos + 1))) {
1393 ++pos;
1394 while (pos < length && IsLowerCase(cb.CharAt(pos)))
1395 ++pos;
1396 } else {
1397 while (pos < length && IsUpperCase(cb.CharAt(pos)))
1398 ++pos;
1399 }
1400 if (IsLowerCase(cb.CharAt(pos)) && IsUpperCase(cb.CharAt(pos - 1)))
1401 --pos;
1402 } else if (IsADigit(startChar)) {
1403 while (pos < length && IsADigit(cb.CharAt(pos)))
1404 ++pos;
1405 } else if (IsPunctuation(startChar)) {
1406 while (pos < length && IsPunctuation(cb.CharAt(pos)))
1407 ++pos;
1408 } else if (isspacechar(startChar)) {
1409 while (pos < length && isspacechar(cb.CharAt(pos)))
1410 ++pos;
1411 } else {
1412 ++pos;
1413 }
1414 return pos;
1415 }
1416
1417 bool IsLineEndChar(char c) {
1418 return (c == '\n' || c == '\r');
1419 }
1420
1421 int Document::ExtendStyleRange(int pos, int delta, bool singleLine) {
1422 int sStart = cb.StyleAt(pos);
1423 if (delta < 0) {
1424 while (pos > 0 && (cb.StyleAt(pos) == sStart) && (!singleLine || !IsLineEndChar(cb.CharAt(pos))) )
1425 pos--;
1426 pos++;
1427 } else {
1428 while (pos < (Length()) && (cb.StyleAt(pos) == sStart) && (!singleLine || !IsLineEndChar(cb.CharAt(pos))) )
1429 pos++;
1430 }
1431 return pos;
1432 }