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