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