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