Update Scintilla to version 3.5.7
[TortoiseGit.git] / ext / scintilla / src / Document.cxx
blob34b813838964da483ea1bee51986d3c7bb268ecf
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-2011 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
8 #include <stdlib.h>
9 #include <string.h>
10 #include <stdio.h>
11 #include <assert.h>
12 #include <ctype.h>
14 #include <stdexcept>
15 #include <string>
16 #include <vector>
17 #include <algorithm>
19 #ifdef CXX11_REGEX
20 #include <regex>
21 #endif
23 #include "Platform.h"
25 #include "ILexer.h"
26 #include "Scintilla.h"
28 #include "CharacterSet.h"
29 #include "SplitVector.h"
30 #include "Partitioning.h"
31 #include "RunStyles.h"
32 #include "CellBuffer.h"
33 #include "PerLine.h"
34 #include "CharClassify.h"
35 #include "Decoration.h"
36 #include "CaseFolder.h"
37 #include "Document.h"
38 #include "RESearch.h"
39 #include "UniConversion.h"
40 #include "UnicodeFromUTF8.h"
42 #ifdef SCI_NAMESPACE
43 using namespace Scintilla;
44 #endif
46 static inline bool IsPunctuation(char ch) {
47 return IsASCII(ch) && ispunct(ch);
50 void LexInterface::Colourise(int start, int end) {
51 if (pdoc && instance && !performingStyle) {
52 // Protect against reentrance, which may occur, for example, when
53 // fold points are discovered while performing styling and the folding
54 // code looks for child lines which may trigger styling.
55 performingStyle = true;
57 int lengthDoc = pdoc->Length();
58 if (end == -1)
59 end = lengthDoc;
60 int len = end - start;
62 PLATFORM_ASSERT(len >= 0);
63 PLATFORM_ASSERT(start + len <= lengthDoc);
65 int styleStart = 0;
66 if (start > 0)
67 styleStart = pdoc->StyleAt(start - 1);
69 if (len > 0) {
70 instance->Lex(start, len, styleStart, pdoc);
71 instance->Fold(start, len, styleStart, pdoc);
74 performingStyle = false;
78 int LexInterface::LineEndTypesSupported() {
79 if (instance) {
80 int interfaceVersion = instance->Version();
81 if (interfaceVersion >= lvSubStyles) {
82 ILexerWithSubStyles *ssinstance = static_cast<ILexerWithSubStyles *>(instance);
83 return ssinstance->LineEndTypesSupported();
86 return 0;
89 Document::Document() {
90 refCount = 0;
91 pcf = NULL;
92 #ifdef _WIN32
93 eolMode = SC_EOL_CRLF;
94 #else
95 eolMode = SC_EOL_LF;
96 #endif
97 dbcsCodePage = 0;
98 lineEndBitSet = SC_LINE_END_TYPE_DEFAULT;
99 endStyled = 0;
100 styleClock = 0;
101 enteredModification = 0;
102 enteredStyling = 0;
103 enteredReadOnlyCount = 0;
104 insertionSet = false;
105 tabInChars = 8;
106 indentInChars = 0;
107 actualIndentInChars = 8;
108 useTabs = true;
109 tabIndents = true;
110 backspaceUnindents = false;
112 matchesValid = false;
113 regex = 0;
115 UTF8BytesOfLeadInitialise();
117 perLineData[ldMarkers] = new LineMarkers();
118 perLineData[ldLevels] = new LineLevels();
119 perLineData[ldState] = new LineState();
120 perLineData[ldMargin] = new LineAnnotation();
121 perLineData[ldAnnotation] = new LineAnnotation();
123 cb.SetPerLine(this);
125 pli = 0;
128 Document::~Document() {
129 for (std::vector<WatcherWithUserData>::iterator it = watchers.begin(); it != watchers.end(); ++it) {
130 it->watcher->NotifyDeleted(this, it->userData);
132 for (int j=0; j<ldSize; j++) {
133 delete perLineData[j];
134 perLineData[j] = 0;
136 delete regex;
137 regex = 0;
138 delete pli;
139 pli = 0;
140 delete pcf;
141 pcf = 0;
144 void Document::Init() {
145 for (int j=0; j<ldSize; j++) {
146 if (perLineData[j])
147 perLineData[j]->Init();
151 int Document::LineEndTypesSupported() const {
152 if ((SC_CP_UTF8 == dbcsCodePage) && pli)
153 return pli->LineEndTypesSupported();
154 else
155 return 0;
158 bool Document::SetDBCSCodePage(int dbcsCodePage_) {
159 if (dbcsCodePage != dbcsCodePage_) {
160 dbcsCodePage = dbcsCodePage_;
161 SetCaseFolder(NULL);
162 cb.SetLineEndTypes(lineEndBitSet & LineEndTypesSupported());
163 return true;
164 } else {
165 return false;
169 bool Document::SetLineEndTypesAllowed(int lineEndBitSet_) {
170 if (lineEndBitSet != lineEndBitSet_) {
171 lineEndBitSet = lineEndBitSet_;
172 int lineEndBitSetActive = lineEndBitSet & LineEndTypesSupported();
173 if (lineEndBitSetActive != cb.GetLineEndTypes()) {
174 ModifiedAt(0);
175 cb.SetLineEndTypes(lineEndBitSetActive);
176 return true;
177 } else {
178 return false;
180 } else {
181 return false;
185 void Document::InsertLine(int line) {
186 for (int j=0; j<ldSize; j++) {
187 if (perLineData[j])
188 perLineData[j]->InsertLine(line);
192 void Document::RemoveLine(int line) {
193 for (int j=0; j<ldSize; j++) {
194 if (perLineData[j])
195 perLineData[j]->RemoveLine(line);
199 // Increase reference count and return its previous value.
200 int Document::AddRef() {
201 return refCount++;
204 // Decrease reference count and return its previous value.
205 // Delete the document if reference count reaches zero.
206 int SCI_METHOD Document::Release() {
207 int curRefCount = --refCount;
208 if (curRefCount == 0)
209 delete this;
210 return curRefCount;
213 void Document::SetSavePoint() {
214 cb.SetSavePoint();
215 NotifySavePoint(true);
218 void Document::TentativeUndo() {
219 if (!TentativeActive())
220 return;
221 CheckReadOnly();
222 if (enteredModification == 0) {
223 enteredModification++;
224 if (!cb.IsReadOnly()) {
225 bool startSavePoint = cb.IsSavePoint();
226 bool multiLine = false;
227 int steps = cb.TentativeSteps();
228 //Platform::DebugPrintf("Steps=%d\n", steps);
229 for (int step = 0; step < steps; step++) {
230 const int prevLinesTotal = LinesTotal();
231 const Action &action = cb.GetUndoStep();
232 if (action.at == removeAction) {
233 NotifyModified(DocModification(
234 SC_MOD_BEFOREINSERT | SC_PERFORMED_UNDO, action));
235 } else if (action.at == containerAction) {
236 DocModification dm(SC_MOD_CONTAINER | SC_PERFORMED_UNDO);
237 dm.token = action.position;
238 NotifyModified(dm);
239 } else {
240 NotifyModified(DocModification(
241 SC_MOD_BEFOREDELETE | SC_PERFORMED_UNDO, action));
243 cb.PerformUndoStep();
244 if (action.at != containerAction) {
245 ModifiedAt(action.position);
248 int modFlags = SC_PERFORMED_UNDO;
249 // With undo, an insertion action becomes a deletion notification
250 if (action.at == removeAction) {
251 modFlags |= SC_MOD_INSERTTEXT;
252 } else if (action.at == insertAction) {
253 modFlags |= SC_MOD_DELETETEXT;
255 if (steps > 1)
256 modFlags |= SC_MULTISTEPUNDOREDO;
257 const int linesAdded = LinesTotal() - prevLinesTotal;
258 if (linesAdded != 0)
259 multiLine = true;
260 if (step == steps - 1) {
261 modFlags |= SC_LASTSTEPINUNDOREDO;
262 if (multiLine)
263 modFlags |= SC_MULTILINEUNDOREDO;
265 NotifyModified(DocModification(modFlags, action.position, action.lenData,
266 linesAdded, action.data));
269 bool endSavePoint = cb.IsSavePoint();
270 if (startSavePoint != endSavePoint)
271 NotifySavePoint(endSavePoint);
273 cb.TentativeCommit();
275 enteredModification--;
279 int Document::GetMark(int line) {
280 return static_cast<LineMarkers *>(perLineData[ldMarkers])->MarkValue(line);
283 int Document::MarkerNext(int lineStart, int mask) const {
284 return static_cast<LineMarkers *>(perLineData[ldMarkers])->MarkerNext(lineStart, mask);
287 int Document::AddMark(int line, int markerNum) {
288 if (line >= 0 && line <= LinesTotal()) {
289 int prev = static_cast<LineMarkers *>(perLineData[ldMarkers])->
290 AddMark(line, markerNum, LinesTotal());
291 DocModification mh(SC_MOD_CHANGEMARKER, LineStart(line), 0, 0, 0, line);
292 NotifyModified(mh);
293 return prev;
294 } else {
295 return 0;
299 void Document::AddMarkSet(int line, int valueSet) {
300 if (line < 0 || line > LinesTotal()) {
301 return;
303 unsigned int m = valueSet;
304 for (int i = 0; m; i++, m >>= 1)
305 if (m & 1)
306 static_cast<LineMarkers *>(perLineData[ldMarkers])->
307 AddMark(line, i, LinesTotal());
308 DocModification mh(SC_MOD_CHANGEMARKER, LineStart(line), 0, 0, 0, line);
309 NotifyModified(mh);
312 void Document::DeleteMark(int line, int markerNum) {
313 static_cast<LineMarkers *>(perLineData[ldMarkers])->DeleteMark(line, markerNum, false);
314 DocModification mh(SC_MOD_CHANGEMARKER, LineStart(line), 0, 0, 0, line);
315 NotifyModified(mh);
318 void Document::DeleteMarkFromHandle(int markerHandle) {
319 static_cast<LineMarkers *>(perLineData[ldMarkers])->DeleteMarkFromHandle(markerHandle);
320 DocModification mh(SC_MOD_CHANGEMARKER, 0, 0, 0, 0);
321 mh.line = -1;
322 NotifyModified(mh);
325 void Document::DeleteAllMarks(int markerNum) {
326 bool someChanges = false;
327 for (int line = 0; line < LinesTotal(); line++) {
328 if (static_cast<LineMarkers *>(perLineData[ldMarkers])->DeleteMark(line, markerNum, true))
329 someChanges = true;
331 if (someChanges) {
332 DocModification mh(SC_MOD_CHANGEMARKER, 0, 0, 0, 0);
333 mh.line = -1;
334 NotifyModified(mh);
338 int Document::LineFromHandle(int markerHandle) {
339 return static_cast<LineMarkers *>(perLineData[ldMarkers])->LineFromHandle(markerHandle);
342 int SCI_METHOD Document::LineStart(int line) const {
343 return cb.LineStart(line);
346 bool Document::IsLineStartPosition(int position) const {
347 return LineStart(LineFromPosition(position)) == position;
350 int SCI_METHOD Document::LineEnd(int line) const {
351 if (line >= LinesTotal() - 1) {
352 return LineStart(line + 1);
353 } else {
354 int position = LineStart(line + 1);
355 if (SC_CP_UTF8 == dbcsCodePage) {
356 unsigned char bytes[] = {
357 static_cast<unsigned char>(cb.CharAt(position-3)),
358 static_cast<unsigned char>(cb.CharAt(position-2)),
359 static_cast<unsigned char>(cb.CharAt(position-1)),
361 if (UTF8IsSeparator(bytes)) {
362 return position - UTF8SeparatorLength;
364 if (UTF8IsNEL(bytes+1)) {
365 return position - UTF8NELLength;
368 position--; // Back over CR or LF
369 // When line terminator is CR+LF, may need to go back one more
370 if ((position > LineStart(line)) && (cb.CharAt(position - 1) == '\r')) {
371 position--;
373 return position;
377 void SCI_METHOD Document::SetErrorStatus(int status) {
378 // Tell the watchers an error has occurred.
379 for (std::vector<WatcherWithUserData>::iterator it = watchers.begin(); it != watchers.end(); ++it) {
380 it->watcher->NotifyErrorOccurred(this, it->userData, status);
384 int SCI_METHOD Document::LineFromPosition(int pos) const {
385 return cb.LineFromPosition(pos);
388 int Document::LineEndPosition(int position) const {
389 return LineEnd(LineFromPosition(position));
392 bool Document::IsLineEndPosition(int position) const {
393 return LineEnd(LineFromPosition(position)) == position;
396 bool Document::IsPositionInLineEnd(int position) const {
397 return position >= LineEnd(LineFromPosition(position));
400 int Document::VCHomePosition(int position) const {
401 int line = LineFromPosition(position);
402 int startPosition = LineStart(line);
403 int endLine = LineEnd(line);
404 int startText = startPosition;
405 while (startText < endLine && (cb.CharAt(startText) == ' ' || cb.CharAt(startText) == '\t'))
406 startText++;
407 if (position == startText)
408 return startPosition;
409 else
410 return startText;
413 int SCI_METHOD Document::SetLevel(int line, int level) {
414 int prev = static_cast<LineLevels *>(perLineData[ldLevels])->SetLevel(line, level, LinesTotal());
415 if (prev != level) {
416 DocModification mh(SC_MOD_CHANGEFOLD | SC_MOD_CHANGEMARKER,
417 LineStart(line), 0, 0, 0, line);
418 mh.foldLevelNow = level;
419 mh.foldLevelPrev = prev;
420 NotifyModified(mh);
422 return prev;
425 int SCI_METHOD Document::GetLevel(int line) const {
426 return static_cast<LineLevels *>(perLineData[ldLevels])->GetLevel(line);
429 void Document::ClearLevels() {
430 static_cast<LineLevels *>(perLineData[ldLevels])->ClearLevels();
433 static bool IsSubordinate(int levelStart, int levelTry) {
434 if (levelTry & SC_FOLDLEVELWHITEFLAG)
435 return true;
436 else
437 return (levelStart & SC_FOLDLEVELNUMBERMASK) < (levelTry & SC_FOLDLEVELNUMBERMASK);
440 int Document::GetLastChild(int lineParent, int level, int lastLine) {
441 if (level == -1)
442 level = GetLevel(lineParent) & SC_FOLDLEVELNUMBERMASK;
443 int maxLine = LinesTotal();
444 int lookLastLine = (lastLine != -1) ? Platform::Minimum(LinesTotal() - 1, lastLine) : -1;
445 int lineMaxSubord = lineParent;
446 while (lineMaxSubord < maxLine - 1) {
447 EnsureStyledTo(LineStart(lineMaxSubord + 2));
448 if (!IsSubordinate(level, GetLevel(lineMaxSubord + 1)))
449 break;
450 if ((lookLastLine != -1) && (lineMaxSubord >= lookLastLine) && !(GetLevel(lineMaxSubord) & SC_FOLDLEVELWHITEFLAG))
451 break;
452 lineMaxSubord++;
454 if (lineMaxSubord > lineParent) {
455 if (level > (GetLevel(lineMaxSubord + 1) & SC_FOLDLEVELNUMBERMASK)) {
456 // Have chewed up some whitespace that belongs to a parent so seek back
457 if (GetLevel(lineMaxSubord) & SC_FOLDLEVELWHITEFLAG) {
458 lineMaxSubord--;
462 return lineMaxSubord;
465 int Document::GetFoldParent(int line) const {
466 int level = GetLevel(line) & SC_FOLDLEVELNUMBERMASK;
467 int lineLook = line - 1;
468 while ((lineLook > 0) && (
469 (!(GetLevel(lineLook) & SC_FOLDLEVELHEADERFLAG)) ||
470 ((GetLevel(lineLook) & SC_FOLDLEVELNUMBERMASK) >= level))
472 lineLook--;
474 if ((GetLevel(lineLook) & SC_FOLDLEVELHEADERFLAG) &&
475 ((GetLevel(lineLook) & SC_FOLDLEVELNUMBERMASK) < level)) {
476 return lineLook;
477 } else {
478 return -1;
482 void Document::GetHighlightDelimiters(HighlightDelimiter &highlightDelimiter, int line, int lastLine) {
483 int level = GetLevel(line);
484 int lookLastLine = Platform::Maximum(line, lastLine) + 1;
486 int lookLine = line;
487 int lookLineLevel = level;
488 int lookLineLevelNum = lookLineLevel & SC_FOLDLEVELNUMBERMASK;
489 while ((lookLine > 0) && ((lookLineLevel & SC_FOLDLEVELWHITEFLAG) ||
490 ((lookLineLevel & SC_FOLDLEVELHEADERFLAG) && (lookLineLevelNum >= (GetLevel(lookLine + 1) & SC_FOLDLEVELNUMBERMASK))))) {
491 lookLineLevel = GetLevel(--lookLine);
492 lookLineLevelNum = lookLineLevel & SC_FOLDLEVELNUMBERMASK;
495 int beginFoldBlock = (lookLineLevel & SC_FOLDLEVELHEADERFLAG) ? lookLine : GetFoldParent(lookLine);
496 if (beginFoldBlock == -1) {
497 highlightDelimiter.Clear();
498 return;
501 int endFoldBlock = GetLastChild(beginFoldBlock, -1, lookLastLine);
502 int firstChangeableLineBefore = -1;
503 if (endFoldBlock < line) {
504 lookLine = beginFoldBlock - 1;
505 lookLineLevel = GetLevel(lookLine);
506 lookLineLevelNum = lookLineLevel & SC_FOLDLEVELNUMBERMASK;
507 while ((lookLine >= 0) && (lookLineLevelNum >= SC_FOLDLEVELBASE)) {
508 if (lookLineLevel & SC_FOLDLEVELHEADERFLAG) {
509 if (GetLastChild(lookLine, -1, lookLastLine) == line) {
510 beginFoldBlock = lookLine;
511 endFoldBlock = line;
512 firstChangeableLineBefore = line - 1;
515 if ((lookLine > 0) && (lookLineLevelNum == SC_FOLDLEVELBASE) && ((GetLevel(lookLine - 1) & SC_FOLDLEVELNUMBERMASK) > lookLineLevelNum))
516 break;
517 lookLineLevel = GetLevel(--lookLine);
518 lookLineLevelNum = lookLineLevel & SC_FOLDLEVELNUMBERMASK;
521 if (firstChangeableLineBefore == -1) {
522 for (lookLine = line - 1, lookLineLevel = GetLevel(lookLine), lookLineLevelNum = lookLineLevel & SC_FOLDLEVELNUMBERMASK;
523 lookLine >= beginFoldBlock;
524 lookLineLevel = GetLevel(--lookLine), lookLineLevelNum = lookLineLevel & SC_FOLDLEVELNUMBERMASK) {
525 if ((lookLineLevel & SC_FOLDLEVELWHITEFLAG) || (lookLineLevelNum > (level & SC_FOLDLEVELNUMBERMASK))) {
526 firstChangeableLineBefore = lookLine;
527 break;
531 if (firstChangeableLineBefore == -1)
532 firstChangeableLineBefore = beginFoldBlock - 1;
534 int firstChangeableLineAfter = -1;
535 for (lookLine = line + 1, lookLineLevel = GetLevel(lookLine), lookLineLevelNum = lookLineLevel & SC_FOLDLEVELNUMBERMASK;
536 lookLine <= endFoldBlock;
537 lookLineLevel = GetLevel(++lookLine), lookLineLevelNum = lookLineLevel & SC_FOLDLEVELNUMBERMASK) {
538 if ((lookLineLevel & SC_FOLDLEVELHEADERFLAG) && (lookLineLevelNum < (GetLevel(lookLine + 1) & SC_FOLDLEVELNUMBERMASK))) {
539 firstChangeableLineAfter = lookLine;
540 break;
543 if (firstChangeableLineAfter == -1)
544 firstChangeableLineAfter = endFoldBlock + 1;
546 highlightDelimiter.beginFoldBlock = beginFoldBlock;
547 highlightDelimiter.endFoldBlock = endFoldBlock;
548 highlightDelimiter.firstChangeableLineBefore = firstChangeableLineBefore;
549 highlightDelimiter.firstChangeableLineAfter = firstChangeableLineAfter;
552 int Document::ClampPositionIntoDocument(int pos) const {
553 return Platform::Clamp(pos, 0, Length());
556 bool Document::IsCrLf(int pos) const {
557 if (pos < 0)
558 return false;
559 if (pos >= (Length() - 1))
560 return false;
561 return (cb.CharAt(pos) == '\r') && (cb.CharAt(pos + 1) == '\n');
564 int Document::LenChar(int pos) {
565 if (pos < 0) {
566 return 1;
567 } else if (IsCrLf(pos)) {
568 return 2;
569 } else if (SC_CP_UTF8 == dbcsCodePage) {
570 const unsigned char leadByte = static_cast<unsigned char>(cb.CharAt(pos));
571 const int widthCharBytes = UTF8BytesOfLead[leadByte];
572 int lengthDoc = Length();
573 if ((pos + widthCharBytes) > lengthDoc)
574 return lengthDoc - pos;
575 else
576 return widthCharBytes;
577 } else if (dbcsCodePage) {
578 return IsDBCSLeadByte(cb.CharAt(pos)) ? 2 : 1;
579 } else {
580 return 1;
584 bool Document::InGoodUTF8(int pos, int &start, int &end) const {
585 int trail = pos;
586 while ((trail>0) && (pos-trail < UTF8MaxBytes) && UTF8IsTrailByte(static_cast<unsigned char>(cb.CharAt(trail-1))))
587 trail--;
588 start = (trail > 0) ? trail-1 : trail;
590 const unsigned char leadByte = static_cast<unsigned char>(cb.CharAt(start));
591 const int widthCharBytes = UTF8BytesOfLead[leadByte];
592 if (widthCharBytes == 1) {
593 return false;
594 } else {
595 int trailBytes = widthCharBytes - 1;
596 int len = pos - start;
597 if (len > trailBytes)
598 // pos too far from lead
599 return false;
600 char charBytes[UTF8MaxBytes] = {static_cast<char>(leadByte),0,0,0};
601 for (int b=1; b<widthCharBytes && ((start+b) < Length()); b++)
602 charBytes[b] = cb.CharAt(static_cast<int>(start+b));
603 int utf8status = UTF8Classify(reinterpret_cast<const unsigned char *>(charBytes), widthCharBytes);
604 if (utf8status & UTF8MaskInvalid)
605 return false;
606 end = start + widthCharBytes;
607 return true;
611 // Normalise a position so that it is not halfway through a two byte character.
612 // This can occur in two situations -
613 // When lines are terminated with \r\n pairs which should be treated as one character.
614 // When displaying DBCS text such as Japanese.
615 // If moving, move the position in the indicated direction.
616 int Document::MovePositionOutsideChar(int pos, int moveDir, bool checkLineEnd) const {
617 //Platform::DebugPrintf("NoCRLF %d %d\n", pos, moveDir);
618 // If out of range, just return minimum/maximum value.
619 if (pos <= 0)
620 return 0;
621 if (pos >= Length())
622 return Length();
624 // PLATFORM_ASSERT(pos > 0 && pos < Length());
625 if (checkLineEnd && IsCrLf(pos - 1)) {
626 if (moveDir > 0)
627 return pos + 1;
628 else
629 return pos - 1;
632 if (dbcsCodePage) {
633 if (SC_CP_UTF8 == dbcsCodePage) {
634 unsigned char ch = static_cast<unsigned char>(cb.CharAt(pos));
635 // If ch is not a trail byte then pos is valid intercharacter position
636 if (UTF8IsTrailByte(ch)) {
637 int startUTF = pos;
638 int endUTF = pos;
639 if (InGoodUTF8(pos, startUTF, endUTF)) {
640 // ch is a trail byte within a UTF-8 character
641 if (moveDir > 0)
642 pos = endUTF;
643 else
644 pos = startUTF;
646 // Else invalid UTF-8 so return position of isolated trail byte
648 } else {
649 // Anchor DBCS calculations at start of line because start of line can
650 // not be a DBCS trail byte.
651 int posStartLine = LineStart(LineFromPosition(pos));
652 if (pos == posStartLine)
653 return pos;
655 // Step back until a non-lead-byte is found.
656 int posCheck = pos;
657 while ((posCheck > posStartLine) && IsDBCSLeadByte(cb.CharAt(posCheck-1)))
658 posCheck--;
660 // Check from known start of character.
661 while (posCheck < pos) {
662 int mbsize = IsDBCSLeadByte(cb.CharAt(posCheck)) ? 2 : 1;
663 if (posCheck + mbsize == pos) {
664 return pos;
665 } else if (posCheck + mbsize > pos) {
666 if (moveDir > 0) {
667 return posCheck + mbsize;
668 } else {
669 return posCheck;
672 posCheck += mbsize;
677 return pos;
680 // NextPosition moves between valid positions - it can not handle a position in the middle of a
681 // multi-byte character. It is used to iterate through text more efficiently than MovePositionOutsideChar.
682 // A \r\n pair is treated as two characters.
683 int Document::NextPosition(int pos, int moveDir) const {
684 // If out of range, just return minimum/maximum value.
685 int increment = (moveDir > 0) ? 1 : -1;
686 if (pos + increment <= 0)
687 return 0;
688 if (pos + increment >= Length())
689 return Length();
691 if (dbcsCodePage) {
692 if (SC_CP_UTF8 == dbcsCodePage) {
693 if (increment == 1) {
694 // Simple forward movement case so can avoid some checks
695 const unsigned char leadByte = static_cast<unsigned char>(cb.CharAt(pos));
696 if (UTF8IsAscii(leadByte)) {
697 // Single byte character or invalid
698 pos++;
699 } else {
700 const int widthCharBytes = UTF8BytesOfLead[leadByte];
701 char charBytes[UTF8MaxBytes] = {static_cast<char>(leadByte),0,0,0};
702 for (int b=1; b<widthCharBytes; b++)
703 charBytes[b] = cb.CharAt(static_cast<int>(pos+b));
704 int utf8status = UTF8Classify(reinterpret_cast<const unsigned char *>(charBytes), widthCharBytes);
705 if (utf8status & UTF8MaskInvalid)
706 pos++;
707 else
708 pos += utf8status & UTF8MaskWidth;
710 } else {
711 // Examine byte before position
712 pos--;
713 unsigned char ch = static_cast<unsigned char>(cb.CharAt(pos));
714 // If ch is not a trail byte then pos is valid intercharacter position
715 if (UTF8IsTrailByte(ch)) {
716 // If ch is a trail byte in a valid UTF-8 character then return start of character
717 int startUTF = pos;
718 int endUTF = pos;
719 if (InGoodUTF8(pos, startUTF, endUTF)) {
720 pos = startUTF;
722 // Else invalid UTF-8 so return position of isolated trail byte
725 } else {
726 if (moveDir > 0) {
727 int mbsize = IsDBCSLeadByte(cb.CharAt(pos)) ? 2 : 1;
728 pos += mbsize;
729 if (pos > Length())
730 pos = Length();
731 } else {
732 // Anchor DBCS calculations at start of line because start of line can
733 // not be a DBCS trail byte.
734 int posStartLine = LineStart(LineFromPosition(pos));
735 // See http://msdn.microsoft.com/en-us/library/cc194792%28v=MSDN.10%29.aspx
736 // http://msdn.microsoft.com/en-us/library/cc194790.aspx
737 if ((pos - 1) <= posStartLine) {
738 return pos - 1;
739 } else if (IsDBCSLeadByte(cb.CharAt(pos - 1))) {
740 // Must actually be trail byte
741 return pos - 2;
742 } else {
743 // Otherwise, step back until a non-lead-byte is found.
744 int posTemp = pos - 1;
745 while (posStartLine <= --posTemp && IsDBCSLeadByte(cb.CharAt(posTemp)))
747 // Now posTemp+1 must point to the beginning of a character,
748 // so figure out whether we went back an even or an odd
749 // number of bytes and go back 1 or 2 bytes, respectively.
750 return (pos - 1 - ((pos - posTemp) & 1));
754 } else {
755 pos += increment;
758 return pos;
761 bool Document::NextCharacter(int &pos, int moveDir) const {
762 // Returns true if pos changed
763 int posNext = NextPosition(pos, moveDir);
764 if (posNext == pos) {
765 return false;
766 } else {
767 pos = posNext;
768 return true;
772 // Return -1 on out-of-bounds
773 int SCI_METHOD Document::GetRelativePosition(int positionStart, int characterOffset) const {
774 int pos = positionStart;
775 if (dbcsCodePage) {
776 const int increment = (characterOffset > 0) ? 1 : -1;
777 while (characterOffset != 0) {
778 const int posNext = NextPosition(pos, increment);
779 if (posNext == pos)
780 return INVALID_POSITION;
781 pos = posNext;
782 characterOffset -= increment;
784 } else {
785 pos = positionStart + characterOffset;
786 if ((pos < 0) || (pos > Length()))
787 return INVALID_POSITION;
789 return pos;
792 int Document::GetRelativePositionUTF16(int positionStart, int characterOffset) const {
793 int pos = positionStart;
794 if (dbcsCodePage) {
795 const int increment = (characterOffset > 0) ? 1 : -1;
796 while (characterOffset != 0) {
797 const int posNext = NextPosition(pos, increment);
798 if (posNext == pos)
799 return INVALID_POSITION;
800 if (abs(pos-posNext) > 3) // 4 byte character = 2*UTF16.
801 characterOffset -= increment;
802 pos = posNext;
803 characterOffset -= increment;
805 } else {
806 pos = positionStart + characterOffset;
807 if ((pos < 0) || (pos > Length()))
808 return INVALID_POSITION;
810 return pos;
813 int SCI_METHOD Document::GetCharacterAndWidth(int position, int *pWidth) const {
814 int character;
815 int bytesInCharacter = 1;
816 if (dbcsCodePage) {
817 const unsigned char leadByte = static_cast<unsigned char>(cb.CharAt(position));
818 if (SC_CP_UTF8 == dbcsCodePage) {
819 if (UTF8IsAscii(leadByte)) {
820 // Single byte character or invalid
821 character = leadByte;
822 } else {
823 const int widthCharBytes = UTF8BytesOfLead[leadByte];
824 unsigned char charBytes[UTF8MaxBytes] = {leadByte,0,0,0};
825 for (int b=1; b<widthCharBytes; b++)
826 charBytes[b] = static_cast<unsigned char>(cb.CharAt(position+b));
827 int utf8status = UTF8Classify(charBytes, widthCharBytes);
828 if (utf8status & UTF8MaskInvalid) {
829 // Report as singleton surrogate values which are invalid Unicode
830 character = 0xDC80 + leadByte;
831 } else {
832 bytesInCharacter = utf8status & UTF8MaskWidth;
833 character = UnicodeFromUTF8(charBytes);
836 } else {
837 if (IsDBCSLeadByte(leadByte)) {
838 bytesInCharacter = 2;
839 character = (leadByte << 8) | static_cast<unsigned char>(cb.CharAt(position+1));
840 } else {
841 character = leadByte;
844 } else {
845 character = cb.CharAt(position);
847 if (pWidth) {
848 *pWidth = bytesInCharacter;
850 return character;
853 int SCI_METHOD Document::CodePage() const {
854 return dbcsCodePage;
857 bool SCI_METHOD Document::IsDBCSLeadByte(char ch) const {
858 // Byte ranges found in Wikipedia articles with relevant search strings in each case
859 unsigned char uch = static_cast<unsigned char>(ch);
860 switch (dbcsCodePage) {
861 case 932:
862 // Shift_jis
863 return ((uch >= 0x81) && (uch <= 0x9F)) ||
864 ((uch >= 0xE0) && (uch <= 0xFC));
865 // Lead bytes F0 to FC may be a Microsoft addition.
866 case 936:
867 // GBK
868 return (uch >= 0x81) && (uch <= 0xFE);
869 case 949:
870 // Korean Wansung KS C-5601-1987
871 return (uch >= 0x81) && (uch <= 0xFE);
872 case 950:
873 // Big5
874 return (uch >= 0x81) && (uch <= 0xFE);
875 case 1361:
876 // Korean Johab KS C-5601-1992
877 return
878 ((uch >= 0x84) && (uch <= 0xD3)) ||
879 ((uch >= 0xD8) && (uch <= 0xDE)) ||
880 ((uch >= 0xE0) && (uch <= 0xF9));
882 return false;
885 static inline bool IsSpaceOrTab(int ch) {
886 return ch == ' ' || ch == '\t';
889 // Need to break text into segments near lengthSegment but taking into
890 // account the encoding to not break inside a UTF-8 or DBCS character
891 // and also trying to avoid breaking inside a pair of combining characters.
892 // The segment length must always be long enough (more than 4 bytes)
893 // so that there will be at least one whole character to make a segment.
894 // For UTF-8, text must consist only of valid whole characters.
895 // In preference order from best to worst:
896 // 1) Break after space
897 // 2) Break before punctuation
898 // 3) Break after whole character
900 int Document::SafeSegment(const char *text, int length, int lengthSegment) const {
901 if (length <= lengthSegment)
902 return length;
903 int lastSpaceBreak = -1;
904 int lastPunctuationBreak = -1;
905 int lastEncodingAllowedBreak = 0;
906 for (int j=0; j < lengthSegment;) {
907 unsigned char ch = static_cast<unsigned char>(text[j]);
908 if (j > 0) {
909 if (IsSpaceOrTab(text[j - 1]) && !IsSpaceOrTab(text[j])) {
910 lastSpaceBreak = j;
912 if (ch < 'A') {
913 lastPunctuationBreak = j;
916 lastEncodingAllowedBreak = j;
918 if (dbcsCodePage == SC_CP_UTF8) {
919 j += UTF8BytesOfLead[ch];
920 } else if (dbcsCodePage) {
921 j += IsDBCSLeadByte(ch) ? 2 : 1;
922 } else {
923 j++;
926 if (lastSpaceBreak >= 0) {
927 return lastSpaceBreak;
928 } else if (lastPunctuationBreak >= 0) {
929 return lastPunctuationBreak;
931 return lastEncodingAllowedBreak;
934 EncodingFamily Document::CodePageFamily() const {
935 if (SC_CP_UTF8 == dbcsCodePage)
936 return efUnicode;
937 else if (dbcsCodePage)
938 return efDBCS;
939 else
940 return efEightBit;
943 void Document::ModifiedAt(int pos) {
944 if (endStyled > pos)
945 endStyled = pos;
948 void Document::CheckReadOnly() {
949 if (cb.IsReadOnly() && enteredReadOnlyCount == 0) {
950 enteredReadOnlyCount++;
951 NotifyModifyAttempt();
952 enteredReadOnlyCount--;
956 // Document only modified by gateways DeleteChars, InsertString, Undo, Redo, and SetStyleAt.
957 // SetStyleAt does not change the persistent state of a document
959 bool Document::DeleteChars(int pos, int len) {
960 if (pos < 0)
961 return false;
962 if (len <= 0)
963 return false;
964 if ((pos + len) > Length())
965 return false;
966 CheckReadOnly();
967 if (enteredModification != 0) {
968 return false;
969 } else {
970 enteredModification++;
971 if (!cb.IsReadOnly()) {
972 NotifyModified(
973 DocModification(
974 SC_MOD_BEFOREDELETE | SC_PERFORMED_USER,
975 pos, len,
976 0, 0));
977 int prevLinesTotal = LinesTotal();
978 bool startSavePoint = cb.IsSavePoint();
979 bool startSequence = false;
980 const char *text = cb.DeleteChars(pos, len, startSequence);
981 if (startSavePoint && cb.IsCollectingUndo())
982 NotifySavePoint(!startSavePoint);
983 if ((pos < Length()) || (pos == 0))
984 ModifiedAt(pos);
985 else
986 ModifiedAt(pos-1);
987 NotifyModified(
988 DocModification(
989 SC_MOD_DELETETEXT | SC_PERFORMED_USER | (startSequence?SC_STARTACTION:0),
990 pos, len,
991 LinesTotal() - prevLinesTotal, text));
993 enteredModification--;
995 return !cb.IsReadOnly();
999 * Insert a string with a length.
1001 int Document::InsertString(int position, const char *s, int insertLength) {
1002 if (insertLength <= 0) {
1003 return 0;
1005 CheckReadOnly(); // Application may change read only state here
1006 if (cb.IsReadOnly()) {
1007 return 0;
1009 if (enteredModification != 0) {
1010 return 0;
1012 enteredModification++;
1013 insertionSet = false;
1014 insertion.clear();
1015 NotifyModified(
1016 DocModification(
1017 SC_MOD_INSERTCHECK,
1018 position, insertLength,
1019 0, s));
1020 if (insertionSet) {
1021 s = insertion.c_str();
1022 insertLength = static_cast<int>(insertion.length());
1024 NotifyModified(
1025 DocModification(
1026 SC_MOD_BEFOREINSERT | SC_PERFORMED_USER,
1027 position, insertLength,
1028 0, s));
1029 int prevLinesTotal = LinesTotal();
1030 bool startSavePoint = cb.IsSavePoint();
1031 bool startSequence = false;
1032 const char *text = cb.InsertString(position, s, insertLength, startSequence);
1033 if (startSavePoint && cb.IsCollectingUndo())
1034 NotifySavePoint(!startSavePoint);
1035 ModifiedAt(position);
1036 NotifyModified(
1037 DocModification(
1038 SC_MOD_INSERTTEXT | SC_PERFORMED_USER | (startSequence?SC_STARTACTION:0),
1039 position, insertLength,
1040 LinesTotal() - prevLinesTotal, text));
1041 if (insertionSet) { // Free memory as could be large
1042 std::string().swap(insertion);
1044 enteredModification--;
1045 return insertLength;
1048 void Document::ChangeInsertion(const char *s, int length) {
1049 insertionSet = true;
1050 insertion.assign(s, length);
1053 int SCI_METHOD Document::AddData(char *data, int length) {
1054 try {
1055 int position = Length();
1056 InsertString(position, data, length);
1057 } catch (std::bad_alloc &) {
1058 return SC_STATUS_BADALLOC;
1059 } catch (...) {
1060 return SC_STATUS_FAILURE;
1062 return 0;
1065 void * SCI_METHOD Document::ConvertToDocument() {
1066 return this;
1069 int Document::Undo() {
1070 int newPos = -1;
1071 CheckReadOnly();
1072 if ((enteredModification == 0) && (cb.IsCollectingUndo())) {
1073 enteredModification++;
1074 if (!cb.IsReadOnly()) {
1075 bool startSavePoint = cb.IsSavePoint();
1076 bool multiLine = false;
1077 int steps = cb.StartUndo();
1078 //Platform::DebugPrintf("Steps=%d\n", steps);
1079 int coalescedRemovePos = -1;
1080 int coalescedRemoveLen = 0;
1081 int prevRemoveActionPos = -1;
1082 int prevRemoveActionLen = 0;
1083 for (int step = 0; step < steps; step++) {
1084 const int prevLinesTotal = LinesTotal();
1085 const Action &action = cb.GetUndoStep();
1086 if (action.at == removeAction) {
1087 NotifyModified(DocModification(
1088 SC_MOD_BEFOREINSERT | SC_PERFORMED_UNDO, action));
1089 } else if (action.at == containerAction) {
1090 DocModification dm(SC_MOD_CONTAINER | SC_PERFORMED_UNDO);
1091 dm.token = action.position;
1092 NotifyModified(dm);
1093 if (!action.mayCoalesce) {
1094 coalescedRemovePos = -1;
1095 coalescedRemoveLen = 0;
1096 prevRemoveActionPos = -1;
1097 prevRemoveActionLen = 0;
1099 } else {
1100 NotifyModified(DocModification(
1101 SC_MOD_BEFOREDELETE | SC_PERFORMED_UNDO, action));
1103 cb.PerformUndoStep();
1104 if (action.at != containerAction) {
1105 ModifiedAt(action.position);
1106 newPos = action.position;
1109 int modFlags = SC_PERFORMED_UNDO;
1110 // With undo, an insertion action becomes a deletion notification
1111 if (action.at == removeAction) {
1112 newPos += action.lenData;
1113 modFlags |= SC_MOD_INSERTTEXT;
1114 if ((coalescedRemoveLen > 0) &&
1115 (action.position == prevRemoveActionPos || action.position == (prevRemoveActionPos + prevRemoveActionLen))) {
1116 coalescedRemoveLen += action.lenData;
1117 newPos = coalescedRemovePos + coalescedRemoveLen;
1118 } else {
1119 coalescedRemovePos = action.position;
1120 coalescedRemoveLen = action.lenData;
1122 prevRemoveActionPos = action.position;
1123 prevRemoveActionLen = action.lenData;
1124 } else if (action.at == insertAction) {
1125 modFlags |= SC_MOD_DELETETEXT;
1126 coalescedRemovePos = -1;
1127 coalescedRemoveLen = 0;
1128 prevRemoveActionPos = -1;
1129 prevRemoveActionLen = 0;
1131 if (steps > 1)
1132 modFlags |= SC_MULTISTEPUNDOREDO;
1133 const int linesAdded = LinesTotal() - prevLinesTotal;
1134 if (linesAdded != 0)
1135 multiLine = true;
1136 if (step == steps - 1) {
1137 modFlags |= SC_LASTSTEPINUNDOREDO;
1138 if (multiLine)
1139 modFlags |= SC_MULTILINEUNDOREDO;
1141 NotifyModified(DocModification(modFlags, action.position, action.lenData,
1142 linesAdded, action.data));
1145 bool endSavePoint = cb.IsSavePoint();
1146 if (startSavePoint != endSavePoint)
1147 NotifySavePoint(endSavePoint);
1149 enteredModification--;
1151 return newPos;
1154 int Document::Redo() {
1155 int newPos = -1;
1156 CheckReadOnly();
1157 if ((enteredModification == 0) && (cb.IsCollectingUndo())) {
1158 enteredModification++;
1159 if (!cb.IsReadOnly()) {
1160 bool startSavePoint = cb.IsSavePoint();
1161 bool multiLine = false;
1162 int steps = cb.StartRedo();
1163 for (int step = 0; step < steps; step++) {
1164 const int prevLinesTotal = LinesTotal();
1165 const Action &action = cb.GetRedoStep();
1166 if (action.at == insertAction) {
1167 NotifyModified(DocModification(
1168 SC_MOD_BEFOREINSERT | SC_PERFORMED_REDO, action));
1169 } else if (action.at == containerAction) {
1170 DocModification dm(SC_MOD_CONTAINER | SC_PERFORMED_REDO);
1171 dm.token = action.position;
1172 NotifyModified(dm);
1173 } else {
1174 NotifyModified(DocModification(
1175 SC_MOD_BEFOREDELETE | SC_PERFORMED_REDO, action));
1177 cb.PerformRedoStep();
1178 if (action.at != containerAction) {
1179 ModifiedAt(action.position);
1180 newPos = action.position;
1183 int modFlags = SC_PERFORMED_REDO;
1184 if (action.at == insertAction) {
1185 newPos += action.lenData;
1186 modFlags |= SC_MOD_INSERTTEXT;
1187 } else if (action.at == removeAction) {
1188 modFlags |= SC_MOD_DELETETEXT;
1190 if (steps > 1)
1191 modFlags |= SC_MULTISTEPUNDOREDO;
1192 const int linesAdded = LinesTotal() - prevLinesTotal;
1193 if (linesAdded != 0)
1194 multiLine = true;
1195 if (step == steps - 1) {
1196 modFlags |= SC_LASTSTEPINUNDOREDO;
1197 if (multiLine)
1198 modFlags |= SC_MULTILINEUNDOREDO;
1200 NotifyModified(
1201 DocModification(modFlags, action.position, action.lenData,
1202 linesAdded, action.data));
1205 bool endSavePoint = cb.IsSavePoint();
1206 if (startSavePoint != endSavePoint)
1207 NotifySavePoint(endSavePoint);
1209 enteredModification--;
1211 return newPos;
1214 void Document::DelChar(int pos) {
1215 DeleteChars(pos, LenChar(pos));
1218 void Document::DelCharBack(int pos) {
1219 if (pos <= 0) {
1220 return;
1221 } else if (IsCrLf(pos - 2)) {
1222 DeleteChars(pos - 2, 2);
1223 } else if (dbcsCodePage) {
1224 int startChar = NextPosition(pos, -1);
1225 DeleteChars(startChar, pos - startChar);
1226 } else {
1227 DeleteChars(pos - 1, 1);
1231 static int NextTab(int pos, int tabSize) {
1232 return ((pos / tabSize) + 1) * tabSize;
1235 static std::string CreateIndentation(int indent, int tabSize, bool insertSpaces) {
1236 std::string indentation;
1237 if (!insertSpaces) {
1238 while (indent >= tabSize) {
1239 indentation += '\t';
1240 indent -= tabSize;
1243 while (indent > 0) {
1244 indentation += ' ';
1245 indent--;
1247 return indentation;
1250 int SCI_METHOD Document::GetLineIndentation(int line) {
1251 int indent = 0;
1252 if ((line >= 0) && (line < LinesTotal())) {
1253 int lineStart = LineStart(line);
1254 int length = Length();
1255 for (int i = lineStart; i < length; i++) {
1256 char ch = cb.CharAt(i);
1257 if (ch == ' ')
1258 indent++;
1259 else if (ch == '\t')
1260 indent = NextTab(indent, tabInChars);
1261 else
1262 return indent;
1265 return indent;
1268 int Document::SetLineIndentation(int line, int indent) {
1269 int indentOfLine = GetLineIndentation(line);
1270 if (indent < 0)
1271 indent = 0;
1272 if (indent != indentOfLine) {
1273 std::string linebuf = CreateIndentation(indent, tabInChars, !useTabs);
1274 int thisLineStart = LineStart(line);
1275 int indentPos = GetLineIndentPosition(line);
1276 UndoGroup ug(this);
1277 DeleteChars(thisLineStart, indentPos - thisLineStart);
1278 return thisLineStart + InsertString(thisLineStart, linebuf.c_str(),
1279 static_cast<int>(linebuf.length()));
1280 } else {
1281 return GetLineIndentPosition(line);
1285 int Document::GetLineIndentPosition(int line) const {
1286 if (line < 0)
1287 return 0;
1288 int pos = LineStart(line);
1289 int length = Length();
1290 while ((pos < length) && IsSpaceOrTab(cb.CharAt(pos))) {
1291 pos++;
1293 return pos;
1296 int Document::GetColumn(int pos) {
1297 int column = 0;
1298 int line = LineFromPosition(pos);
1299 if ((line >= 0) && (line < LinesTotal())) {
1300 for (int i = LineStart(line); i < pos;) {
1301 char ch = cb.CharAt(i);
1302 if (ch == '\t') {
1303 column = NextTab(column, tabInChars);
1304 i++;
1305 } else if (ch == '\r') {
1306 return column;
1307 } else if (ch == '\n') {
1308 return column;
1309 } else if (i >= Length()) {
1310 return column;
1311 } else {
1312 column++;
1313 i = NextPosition(i, 1);
1317 return column;
1320 int Document::CountCharacters(int startPos, int endPos) const {
1321 startPos = MovePositionOutsideChar(startPos, 1, false);
1322 endPos = MovePositionOutsideChar(endPos, -1, false);
1323 int count = 0;
1324 int i = startPos;
1325 while (i < endPos) {
1326 count++;
1327 if (IsCrLf(i))
1328 i++;
1329 i = NextPosition(i, 1);
1331 return count;
1334 int Document::CountUTF16(int startPos, int endPos) const {
1335 startPos = MovePositionOutsideChar(startPos, 1, false);
1336 endPos = MovePositionOutsideChar(endPos, -1, false);
1337 int count = 0;
1338 int i = startPos;
1339 while (i < endPos) {
1340 count++;
1341 const int next = NextPosition(i, 1);
1342 if ((next - i) > 3)
1343 count++;
1344 i = next;
1346 return count;
1349 int Document::FindColumn(int line, int column) {
1350 int position = LineStart(line);
1351 if ((line >= 0) && (line < LinesTotal())) {
1352 int columnCurrent = 0;
1353 while ((columnCurrent < column) && (position < Length())) {
1354 char ch = cb.CharAt(position);
1355 if (ch == '\t') {
1356 columnCurrent = NextTab(columnCurrent, tabInChars);
1357 if (columnCurrent > column)
1358 return position;
1359 position++;
1360 } else if (ch == '\r') {
1361 return position;
1362 } else if (ch == '\n') {
1363 return position;
1364 } else {
1365 columnCurrent++;
1366 position = NextPosition(position, 1);
1370 return position;
1373 void Document::Indent(bool forwards, int lineBottom, int lineTop) {
1374 // Dedent - suck white space off the front of the line to dedent by equivalent of a tab
1375 for (int line = lineBottom; line >= lineTop; line--) {
1376 int indentOfLine = GetLineIndentation(line);
1377 if (forwards) {
1378 if (LineStart(line) < LineEnd(line)) {
1379 SetLineIndentation(line, indentOfLine + IndentSize());
1381 } else {
1382 SetLineIndentation(line, indentOfLine - IndentSize());
1387 // Convert line endings for a piece of text to a particular mode.
1388 // Stop at len or when a NUL is found.
1389 std::string Document::TransformLineEnds(const char *s, size_t len, int eolModeWanted) {
1390 std::string dest;
1391 for (size_t i = 0; (i < len) && (s[i]); i++) {
1392 if (s[i] == '\n' || s[i] == '\r') {
1393 if (eolModeWanted == SC_EOL_CR) {
1394 dest.push_back('\r');
1395 } else if (eolModeWanted == SC_EOL_LF) {
1396 dest.push_back('\n');
1397 } else { // eolModeWanted == SC_EOL_CRLF
1398 dest.push_back('\r');
1399 dest.push_back('\n');
1401 if ((s[i] == '\r') && (i+1 < len) && (s[i+1] == '\n')) {
1402 i++;
1404 } else {
1405 dest.push_back(s[i]);
1408 return dest;
1411 void Document::ConvertLineEnds(int eolModeSet) {
1412 UndoGroup ug(this);
1414 for (int pos = 0; pos < Length(); pos++) {
1415 if (cb.CharAt(pos) == '\r') {
1416 if (cb.CharAt(pos + 1) == '\n') {
1417 // CRLF
1418 if (eolModeSet == SC_EOL_CR) {
1419 DeleteChars(pos + 1, 1); // Delete the LF
1420 } else if (eolModeSet == SC_EOL_LF) {
1421 DeleteChars(pos, 1); // Delete the CR
1422 } else {
1423 pos++;
1425 } else {
1426 // CR
1427 if (eolModeSet == SC_EOL_CRLF) {
1428 pos += InsertString(pos + 1, "\n", 1); // Insert LF
1429 } else if (eolModeSet == SC_EOL_LF) {
1430 pos += InsertString(pos, "\n", 1); // Insert LF
1431 DeleteChars(pos, 1); // Delete CR
1432 pos--;
1435 } else if (cb.CharAt(pos) == '\n') {
1436 // LF
1437 if (eolModeSet == SC_EOL_CRLF) {
1438 pos += InsertString(pos, "\r", 1); // Insert CR
1439 } else if (eolModeSet == SC_EOL_CR) {
1440 pos += InsertString(pos, "\r", 1); // Insert CR
1441 DeleteChars(pos, 1); // Delete LF
1442 pos--;
1449 bool Document::IsWhiteLine(int line) const {
1450 int currentChar = LineStart(line);
1451 int endLine = LineEnd(line);
1452 while (currentChar < endLine) {
1453 if (cb.CharAt(currentChar) != ' ' && cb.CharAt(currentChar) != '\t') {
1454 return false;
1456 ++currentChar;
1458 return true;
1461 int Document::ParaUp(int pos) const {
1462 int line = LineFromPosition(pos);
1463 line--;
1464 while (line >= 0 && IsWhiteLine(line)) { // skip empty lines
1465 line--;
1467 while (line >= 0 && !IsWhiteLine(line)) { // skip non-empty lines
1468 line--;
1470 line++;
1471 return LineStart(line);
1474 int Document::ParaDown(int pos) const {
1475 int line = LineFromPosition(pos);
1476 while (line < LinesTotal() && !IsWhiteLine(line)) { // skip non-empty lines
1477 line++;
1479 while (line < LinesTotal() && IsWhiteLine(line)) { // skip empty lines
1480 line++;
1482 if (line < LinesTotal())
1483 return LineStart(line);
1484 else // end of a document
1485 return LineEnd(line-1);
1488 CharClassify::cc Document::WordCharClass(unsigned char ch) const {
1489 if ((SC_CP_UTF8 == dbcsCodePage) && (!UTF8IsAscii(ch)))
1490 return CharClassify::ccWord;
1491 return charClass.GetClass(ch);
1495 * Used by commmands that want to select whole words.
1496 * Finds the start of word at pos when delta < 0 or the end of the word when delta >= 0.
1498 int Document::ExtendWordSelect(int pos, int delta, bool onlyWordCharacters) {
1499 CharClassify::cc ccStart = CharClassify::ccWord;
1500 if (delta < 0) {
1501 if (!onlyWordCharacters)
1502 ccStart = WordCharClass(cb.CharAt(pos-1));
1503 while (pos > 0 && (WordCharClass(cb.CharAt(pos - 1)) == ccStart))
1504 pos--;
1505 } else {
1506 if (!onlyWordCharacters && pos < Length())
1507 ccStart = WordCharClass(cb.CharAt(pos));
1508 while (pos < (Length()) && (WordCharClass(cb.CharAt(pos)) == ccStart))
1509 pos++;
1511 return MovePositionOutsideChar(pos, delta, true);
1515 * Find the start of the next word in either a forward (delta >= 0) or backwards direction
1516 * (delta < 0).
1517 * This is looking for a transition between character classes although there is also some
1518 * additional movement to transit white space.
1519 * Used by cursor movement by word commands.
1521 int Document::NextWordStart(int pos, int delta) {
1522 if (delta < 0) {
1523 while (pos > 0 && (WordCharClass(cb.CharAt(pos - 1)) == CharClassify::ccSpace))
1524 pos--;
1525 if (pos > 0) {
1526 CharClassify::cc ccStart = WordCharClass(cb.CharAt(pos-1));
1527 while (pos > 0 && (WordCharClass(cb.CharAt(pos - 1)) == ccStart)) {
1528 pos--;
1531 } else {
1532 CharClassify::cc ccStart = WordCharClass(cb.CharAt(pos));
1533 while (pos < (Length()) && (WordCharClass(cb.CharAt(pos)) == ccStart))
1534 pos++;
1535 while (pos < (Length()) && (WordCharClass(cb.CharAt(pos)) == CharClassify::ccSpace))
1536 pos++;
1538 return pos;
1542 * Find the end of the next word in either a forward (delta >= 0) or backwards direction
1543 * (delta < 0).
1544 * This is looking for a transition between character classes although there is also some
1545 * additional movement to transit white space.
1546 * Used by cursor movement by word commands.
1548 int Document::NextWordEnd(int pos, int delta) {
1549 if (delta < 0) {
1550 if (pos > 0) {
1551 CharClassify::cc ccStart = WordCharClass(cb.CharAt(pos-1));
1552 if (ccStart != CharClassify::ccSpace) {
1553 while (pos > 0 && WordCharClass(cb.CharAt(pos - 1)) == ccStart) {
1554 pos--;
1557 while (pos > 0 && WordCharClass(cb.CharAt(pos - 1)) == CharClassify::ccSpace) {
1558 pos--;
1561 } else {
1562 while (pos < Length() && WordCharClass(cb.CharAt(pos)) == CharClassify::ccSpace) {
1563 pos++;
1565 if (pos < Length()) {
1566 CharClassify::cc ccStart = WordCharClass(cb.CharAt(pos));
1567 while (pos < Length() && WordCharClass(cb.CharAt(pos)) == ccStart) {
1568 pos++;
1572 return pos;
1576 * Check that the character at the given position is a word or punctuation character and that
1577 * the previous character is of a different character class.
1579 bool Document::IsWordStartAt(int pos) const {
1580 if (pos > 0) {
1581 CharClassify::cc ccPos = WordCharClass(CharAt(pos));
1582 return (ccPos == CharClassify::ccWord || ccPos == CharClassify::ccPunctuation) &&
1583 (ccPos != WordCharClass(CharAt(pos - 1)));
1585 return true;
1589 * Check that the character at the given position is a word or punctuation character and that
1590 * the next character is of a different character class.
1592 bool Document::IsWordEndAt(int pos) const {
1593 if (pos < Length()) {
1594 CharClassify::cc ccPrev = WordCharClass(CharAt(pos-1));
1595 return (ccPrev == CharClassify::ccWord || ccPrev == CharClassify::ccPunctuation) &&
1596 (ccPrev != WordCharClass(CharAt(pos)));
1598 return true;
1602 * Check that the given range is has transitions between character classes at both
1603 * ends and where the characters on the inside are word or punctuation characters.
1605 bool Document::IsWordAt(int start, int end) const {
1606 return (start < end) && IsWordStartAt(start) && IsWordEndAt(end);
1609 bool Document::MatchesWordOptions(bool word, bool wordStart, int pos, int length) const {
1610 return (!word && !wordStart) ||
1611 (word && IsWordAt(pos, pos + length)) ||
1612 (wordStart && IsWordStartAt(pos));
1615 bool Document::HasCaseFolder(void) const {
1616 return pcf != 0;
1619 void Document::SetCaseFolder(CaseFolder *pcf_) {
1620 delete pcf;
1621 pcf = pcf_;
1624 Document::CharacterExtracted Document::ExtractCharacter(int position) const {
1625 const unsigned char leadByte = static_cast<unsigned char>(cb.CharAt(position));
1626 if (UTF8IsAscii(leadByte)) {
1627 // Common case: ASCII character
1628 return CharacterExtracted(leadByte, 1);
1630 const int widthCharBytes = UTF8BytesOfLead[leadByte];
1631 unsigned char charBytes[UTF8MaxBytes] = { leadByte, 0, 0, 0 };
1632 for (int b=1; b<widthCharBytes; b++)
1633 charBytes[b] = static_cast<unsigned char>(cb.CharAt(position + b));
1634 int utf8status = UTF8Classify(charBytes, widthCharBytes);
1635 if (utf8status & UTF8MaskInvalid) {
1636 // Treat as invalid and use up just one byte
1637 return CharacterExtracted(unicodeReplacementChar, 1);
1638 } else {
1639 return CharacterExtracted(UnicodeFromUTF8(charBytes), utf8status & UTF8MaskWidth);
1644 * Find text in document, supporting both forward and backward
1645 * searches (just pass minPos > maxPos to do a backward search)
1646 * Has not been tested with backwards DBCS searches yet.
1648 long Document::FindText(int minPos, int maxPos, const char *search,
1649 int flags, int *length) {
1650 if (*length <= 0)
1651 return minPos;
1652 const bool caseSensitive = (flags & SCFIND_MATCHCASE) != 0;
1653 const bool word = (flags & SCFIND_WHOLEWORD) != 0;
1654 const bool wordStart = (flags & SCFIND_WORDSTART) != 0;
1655 const bool regExp = (flags & SCFIND_REGEXP) != 0;
1656 if (regExp) {
1657 if (!regex)
1658 regex = CreateRegexSearch(&charClass);
1659 return regex->FindText(this, minPos, maxPos, search, caseSensitive, word, wordStart, flags, length);
1660 } else {
1662 const bool forward = minPos <= maxPos;
1663 const int increment = forward ? 1 : -1;
1665 // Range endpoints should not be inside DBCS characters, but just in case, move them.
1666 const int startPos = MovePositionOutsideChar(minPos, increment, false);
1667 const int endPos = MovePositionOutsideChar(maxPos, increment, false);
1669 // Compute actual search ranges needed
1670 const int lengthFind = *length;
1672 //Platform::DebugPrintf("Find %d %d %s %d\n", startPos, endPos, ft->lpstrText, lengthFind);
1673 const int limitPos = Platform::Maximum(startPos, endPos);
1674 int pos = startPos;
1675 if (!forward) {
1676 // Back all of a character
1677 pos = NextPosition(pos, increment);
1679 if (caseSensitive) {
1680 const int endSearch = (startPos <= endPos) ? endPos - lengthFind + 1 : endPos;
1681 const char charStartSearch = search[0];
1682 while (forward ? (pos < endSearch) : (pos >= endSearch)) {
1683 if (CharAt(pos) == charStartSearch) {
1684 bool found = (pos + lengthFind) <= limitPos;
1685 for (int indexSearch = 1; (indexSearch < lengthFind) && found; indexSearch++) {
1686 found = CharAt(pos + indexSearch) == search[indexSearch];
1688 if (found && MatchesWordOptions(word, wordStart, pos, lengthFind)) {
1689 return pos;
1692 if (!NextCharacter(pos, increment))
1693 break;
1695 } else if (SC_CP_UTF8 == dbcsCodePage) {
1696 const size_t maxFoldingExpansion = 4;
1697 std::vector<char> searchThing(lengthFind * UTF8MaxBytes * maxFoldingExpansion + 1);
1698 const int lenSearch = static_cast<int>(
1699 pcf->Fold(&searchThing[0], searchThing.size(), search, lengthFind));
1700 char bytes[UTF8MaxBytes + 1];
1701 char folded[UTF8MaxBytes * maxFoldingExpansion + 1];
1702 while (forward ? (pos < endPos) : (pos >= endPos)) {
1703 int widthFirstCharacter = 0;
1704 int posIndexDocument = pos;
1705 int indexSearch = 0;
1706 bool characterMatches = true;
1707 for (;;) {
1708 const unsigned char leadByte = static_cast<unsigned char>(cb.CharAt(posIndexDocument));
1709 bytes[0] = leadByte;
1710 int widthChar = 1;
1711 if (!UTF8IsAscii(leadByte)) {
1712 const int widthCharBytes = UTF8BytesOfLead[leadByte];
1713 for (int b=1; b<widthCharBytes; b++) {
1714 bytes[b] = cb.CharAt(posIndexDocument+b);
1716 widthChar = UTF8Classify(reinterpret_cast<const unsigned char *>(bytes), widthCharBytes) & UTF8MaskWidth;
1718 if (!widthFirstCharacter)
1719 widthFirstCharacter = widthChar;
1720 if ((posIndexDocument + widthChar) > limitPos)
1721 break;
1722 const int lenFlat = static_cast<int>(pcf->Fold(folded, sizeof(folded), bytes, widthChar));
1723 folded[lenFlat] = 0;
1724 // Does folded match the buffer
1725 characterMatches = 0 == memcmp(folded, &searchThing[0] + indexSearch, lenFlat);
1726 if (!characterMatches)
1727 break;
1728 posIndexDocument += widthChar;
1729 indexSearch += lenFlat;
1730 if (indexSearch >= lenSearch)
1731 break;
1733 if (characterMatches && (indexSearch == static_cast<int>(lenSearch))) {
1734 if (MatchesWordOptions(word, wordStart, pos, posIndexDocument - pos)) {
1735 *length = posIndexDocument - pos;
1736 return pos;
1739 if (forward) {
1740 pos += widthFirstCharacter;
1741 } else {
1742 if (!NextCharacter(pos, increment))
1743 break;
1746 } else if (dbcsCodePage) {
1747 const size_t maxBytesCharacter = 2;
1748 const size_t maxFoldingExpansion = 4;
1749 std::vector<char> searchThing(lengthFind * maxBytesCharacter * maxFoldingExpansion + 1);
1750 const int lenSearch = static_cast<int>(
1751 pcf->Fold(&searchThing[0], searchThing.size(), search, lengthFind));
1752 while (forward ? (pos < endPos) : (pos >= endPos)) {
1753 int indexDocument = 0;
1754 int indexSearch = 0;
1755 bool characterMatches = true;
1756 while (characterMatches &&
1757 ((pos + indexDocument) < limitPos) &&
1758 (indexSearch < lenSearch)) {
1759 char bytes[maxBytesCharacter + 1];
1760 bytes[0] = cb.CharAt(pos + indexDocument);
1761 const int widthChar = IsDBCSLeadByte(bytes[0]) ? 2 : 1;
1762 if (widthChar == 2)
1763 bytes[1] = cb.CharAt(pos + indexDocument + 1);
1764 if ((pos + indexDocument + widthChar) > limitPos)
1765 break;
1766 char folded[maxBytesCharacter * maxFoldingExpansion + 1];
1767 const int lenFlat = static_cast<int>(pcf->Fold(folded, sizeof(folded), bytes, widthChar));
1768 folded[lenFlat] = 0;
1769 // Does folded match the buffer
1770 characterMatches = 0 == memcmp(folded, &searchThing[0] + indexSearch, lenFlat);
1771 indexDocument += widthChar;
1772 indexSearch += lenFlat;
1774 if (characterMatches && (indexSearch == static_cast<int>(lenSearch))) {
1775 if (MatchesWordOptions(word, wordStart, pos, indexDocument)) {
1776 *length = indexDocument;
1777 return pos;
1780 if (!NextCharacter(pos, increment))
1781 break;
1783 } else {
1784 const int endSearch = (startPos <= endPos) ? endPos - lengthFind + 1 : endPos;
1785 std::vector<char> searchThing(lengthFind + 1);
1786 pcf->Fold(&searchThing[0], searchThing.size(), search, lengthFind);
1787 while (forward ? (pos < endSearch) : (pos >= endSearch)) {
1788 bool found = (pos + lengthFind) <= limitPos;
1789 for (int indexSearch = 0; (indexSearch < lengthFind) && found; indexSearch++) {
1790 char ch = CharAt(pos + indexSearch);
1791 char folded[2];
1792 pcf->Fold(folded, sizeof(folded), &ch, 1);
1793 found = folded[0] == searchThing[indexSearch];
1795 if (found && MatchesWordOptions(word, wordStart, pos, lengthFind)) {
1796 return pos;
1798 if (!NextCharacter(pos, increment))
1799 break;
1803 //Platform::DebugPrintf("Not found\n");
1804 return -1;
1807 const char *Document::SubstituteByPosition(const char *text, int *length) {
1808 if (regex)
1809 return regex->SubstituteByPosition(this, text, length);
1810 else
1811 return 0;
1814 int Document::LinesTotal() const {
1815 return cb.Lines();
1818 void Document::SetDefaultCharClasses(bool includeWordClass) {
1819 charClass.SetDefaultCharClasses(includeWordClass);
1822 void Document::SetCharClasses(const unsigned char *chars, CharClassify::cc newCharClass) {
1823 charClass.SetCharClasses(chars, newCharClass);
1826 int Document::GetCharsOfClass(CharClassify::cc characterClass, unsigned char *buffer) {
1827 return charClass.GetCharsOfClass(characterClass, buffer);
1830 void SCI_METHOD Document::StartStyling(int position, char) {
1831 endStyled = position;
1834 bool SCI_METHOD Document::SetStyleFor(int length, char style) {
1835 if (enteredStyling != 0) {
1836 return false;
1837 } else {
1838 enteredStyling++;
1839 int prevEndStyled = endStyled;
1840 if (cb.SetStyleFor(endStyled, length, style)) {
1841 DocModification mh(SC_MOD_CHANGESTYLE | SC_PERFORMED_USER,
1842 prevEndStyled, length);
1843 NotifyModified(mh);
1845 endStyled += length;
1846 enteredStyling--;
1847 return true;
1851 bool SCI_METHOD Document::SetStyles(int length, const char *styles) {
1852 if (enteredStyling != 0) {
1853 return false;
1854 } else {
1855 enteredStyling++;
1856 bool didChange = false;
1857 int startMod = 0;
1858 int endMod = 0;
1859 for (int iPos = 0; iPos < length; iPos++, endStyled++) {
1860 PLATFORM_ASSERT(endStyled < Length());
1861 if (cb.SetStyleAt(endStyled, styles[iPos])) {
1862 if (!didChange) {
1863 startMod = endStyled;
1865 didChange = true;
1866 endMod = endStyled;
1869 if (didChange) {
1870 DocModification mh(SC_MOD_CHANGESTYLE | SC_PERFORMED_USER,
1871 startMod, endMod - startMod + 1);
1872 NotifyModified(mh);
1874 enteredStyling--;
1875 return true;
1879 void Document::EnsureStyledTo(int pos) {
1880 if ((enteredStyling == 0) && (pos > GetEndStyled())) {
1881 IncrementStyleClock();
1882 if (pli && !pli->UseContainerLexing()) {
1883 int lineEndStyled = LineFromPosition(GetEndStyled());
1884 int endStyledTo = LineStart(lineEndStyled);
1885 pli->Colourise(endStyledTo, pos);
1886 } else {
1887 // Ask the watchers to style, and stop as soon as one responds.
1888 for (std::vector<WatcherWithUserData>::iterator it = watchers.begin();
1889 (pos > GetEndStyled()) && (it != watchers.end()); ++it) {
1890 it->watcher->NotifyStyleNeeded(this, it->userData, pos);
1896 void Document::LexerChanged() {
1897 // Tell the watchers the lexer has changed.
1898 for (std::vector<WatcherWithUserData>::iterator it = watchers.begin(); it != watchers.end(); ++it) {
1899 it->watcher->NotifyLexerChanged(this, it->userData);
1903 int SCI_METHOD Document::SetLineState(int line, int state) {
1904 int statePrevious = static_cast<LineState *>(perLineData[ldState])->SetLineState(line, state);
1905 if (state != statePrevious) {
1906 DocModification mh(SC_MOD_CHANGELINESTATE, LineStart(line), 0, 0, 0, line);
1907 NotifyModified(mh);
1909 return statePrevious;
1912 int SCI_METHOD Document::GetLineState(int line) const {
1913 return static_cast<LineState *>(perLineData[ldState])->GetLineState(line);
1916 int Document::GetMaxLineState() {
1917 return static_cast<LineState *>(perLineData[ldState])->GetMaxLineState();
1920 void SCI_METHOD Document::ChangeLexerState(int start, int end) {
1921 DocModification mh(SC_MOD_LEXERSTATE, start, end-start, 0, 0, 0);
1922 NotifyModified(mh);
1925 StyledText Document::MarginStyledText(int line) const {
1926 LineAnnotation *pla = static_cast<LineAnnotation *>(perLineData[ldMargin]);
1927 return StyledText(pla->Length(line), pla->Text(line),
1928 pla->MultipleStyles(line), pla->Style(line), pla->Styles(line));
1931 void Document::MarginSetText(int line, const char *text) {
1932 static_cast<LineAnnotation *>(perLineData[ldMargin])->SetText(line, text);
1933 DocModification mh(SC_MOD_CHANGEMARGIN, LineStart(line), 0, 0, 0, line);
1934 NotifyModified(mh);
1937 void Document::MarginSetStyle(int line, int style) {
1938 static_cast<LineAnnotation *>(perLineData[ldMargin])->SetStyle(line, style);
1939 NotifyModified(DocModification(SC_MOD_CHANGEMARGIN, LineStart(line), 0, 0, 0, line));
1942 void Document::MarginSetStyles(int line, const unsigned char *styles) {
1943 static_cast<LineAnnotation *>(perLineData[ldMargin])->SetStyles(line, styles);
1944 NotifyModified(DocModification(SC_MOD_CHANGEMARGIN, LineStart(line), 0, 0, 0, line));
1947 void Document::MarginClearAll() {
1948 int maxEditorLine = LinesTotal();
1949 for (int l=0; l<maxEditorLine; l++)
1950 MarginSetText(l, 0);
1951 // Free remaining data
1952 static_cast<LineAnnotation *>(perLineData[ldMargin])->ClearAll();
1955 StyledText Document::AnnotationStyledText(int line) const {
1956 LineAnnotation *pla = static_cast<LineAnnotation *>(perLineData[ldAnnotation]);
1957 return StyledText(pla->Length(line), pla->Text(line),
1958 pla->MultipleStyles(line), pla->Style(line), pla->Styles(line));
1961 void Document::AnnotationSetText(int line, const char *text) {
1962 if (line >= 0 && line < LinesTotal()) {
1963 const int linesBefore = AnnotationLines(line);
1964 static_cast<LineAnnotation *>(perLineData[ldAnnotation])->SetText(line, text);
1965 const int linesAfter = AnnotationLines(line);
1966 DocModification mh(SC_MOD_CHANGEANNOTATION, LineStart(line), 0, 0, 0, line);
1967 mh.annotationLinesAdded = linesAfter - linesBefore;
1968 NotifyModified(mh);
1972 void Document::AnnotationSetStyle(int line, int style) {
1973 static_cast<LineAnnotation *>(perLineData[ldAnnotation])->SetStyle(line, style);
1974 DocModification mh(SC_MOD_CHANGEANNOTATION, LineStart(line), 0, 0, 0, line);
1975 NotifyModified(mh);
1978 void Document::AnnotationSetStyles(int line, const unsigned char *styles) {
1979 if (line >= 0 && line < LinesTotal()) {
1980 static_cast<LineAnnotation *>(perLineData[ldAnnotation])->SetStyles(line, styles);
1984 int Document::AnnotationLines(int line) const {
1985 return static_cast<LineAnnotation *>(perLineData[ldAnnotation])->Lines(line);
1988 void Document::AnnotationClearAll() {
1989 int maxEditorLine = LinesTotal();
1990 for (int l=0; l<maxEditorLine; l++)
1991 AnnotationSetText(l, 0);
1992 // Free remaining data
1993 static_cast<LineAnnotation *>(perLineData[ldAnnotation])->ClearAll();
1996 void Document::IncrementStyleClock() {
1997 styleClock = (styleClock + 1) % 0x100000;
2000 void SCI_METHOD Document::DecorationFillRange(int position, int value, int fillLength) {
2001 if (decorations.FillRange(position, value, fillLength)) {
2002 DocModification mh(SC_MOD_CHANGEINDICATOR | SC_PERFORMED_USER,
2003 position, fillLength);
2004 NotifyModified(mh);
2008 bool Document::AddWatcher(DocWatcher *watcher, void *userData) {
2009 WatcherWithUserData wwud(watcher, userData);
2010 std::vector<WatcherWithUserData>::iterator it =
2011 std::find(watchers.begin(), watchers.end(), wwud);
2012 if (it != watchers.end())
2013 return false;
2014 watchers.push_back(wwud);
2015 return true;
2018 bool Document::RemoveWatcher(DocWatcher *watcher, void *userData) {
2019 std::vector<WatcherWithUserData>::iterator it =
2020 std::find(watchers.begin(), watchers.end(), WatcherWithUserData(watcher, userData));
2021 if (it != watchers.end()) {
2022 watchers.erase(it);
2023 return true;
2025 return false;
2028 void Document::NotifyModifyAttempt() {
2029 for (std::vector<WatcherWithUserData>::iterator it = watchers.begin(); it != watchers.end(); ++it) {
2030 it->watcher->NotifyModifyAttempt(this, it->userData);
2034 void Document::NotifySavePoint(bool atSavePoint) {
2035 for (std::vector<WatcherWithUserData>::iterator it = watchers.begin(); it != watchers.end(); ++it) {
2036 it->watcher->NotifySavePoint(this, it->userData, atSavePoint);
2040 void Document::NotifyModified(DocModification mh) {
2041 if (mh.modificationType & SC_MOD_INSERTTEXT) {
2042 decorations.InsertSpace(mh.position, mh.length);
2043 } else if (mh.modificationType & SC_MOD_DELETETEXT) {
2044 decorations.DeleteRange(mh.position, mh.length);
2046 for (std::vector<WatcherWithUserData>::iterator it = watchers.begin(); it != watchers.end(); ++it) {
2047 it->watcher->NotifyModified(this, mh, it->userData);
2051 bool Document::IsWordPartSeparator(char ch) const {
2052 return (WordCharClass(ch) == CharClassify::ccWord) && IsPunctuation(ch);
2055 int Document::WordPartLeft(int pos) {
2056 if (pos > 0) {
2057 --pos;
2058 char startChar = cb.CharAt(pos);
2059 if (IsWordPartSeparator(startChar)) {
2060 while (pos > 0 && IsWordPartSeparator(cb.CharAt(pos))) {
2061 --pos;
2064 if (pos > 0) {
2065 startChar = cb.CharAt(pos);
2066 --pos;
2067 if (IsLowerCase(startChar)) {
2068 while (pos > 0 && IsLowerCase(cb.CharAt(pos)))
2069 --pos;
2070 if (!IsUpperCase(cb.CharAt(pos)) && !IsLowerCase(cb.CharAt(pos)))
2071 ++pos;
2072 } else if (IsUpperCase(startChar)) {
2073 while (pos > 0 && IsUpperCase(cb.CharAt(pos)))
2074 --pos;
2075 if (!IsUpperCase(cb.CharAt(pos)))
2076 ++pos;
2077 } else if (IsADigit(startChar)) {
2078 while (pos > 0 && IsADigit(cb.CharAt(pos)))
2079 --pos;
2080 if (!IsADigit(cb.CharAt(pos)))
2081 ++pos;
2082 } else if (IsPunctuation(startChar)) {
2083 while (pos > 0 && IsPunctuation(cb.CharAt(pos)))
2084 --pos;
2085 if (!IsPunctuation(cb.CharAt(pos)))
2086 ++pos;
2087 } else if (isspacechar(startChar)) {
2088 while (pos > 0 && isspacechar(cb.CharAt(pos)))
2089 --pos;
2090 if (!isspacechar(cb.CharAt(pos)))
2091 ++pos;
2092 } else if (!IsASCII(startChar)) {
2093 while (pos > 0 && !IsASCII(cb.CharAt(pos)))
2094 --pos;
2095 if (IsASCII(cb.CharAt(pos)))
2096 ++pos;
2097 } else {
2098 ++pos;
2102 return pos;
2105 int Document::WordPartRight(int pos) {
2106 char startChar = cb.CharAt(pos);
2107 int length = Length();
2108 if (IsWordPartSeparator(startChar)) {
2109 while (pos < length && IsWordPartSeparator(cb.CharAt(pos)))
2110 ++pos;
2111 startChar = cb.CharAt(pos);
2113 if (!IsASCII(startChar)) {
2114 while (pos < length && !IsASCII(cb.CharAt(pos)))
2115 ++pos;
2116 } else if (IsLowerCase(startChar)) {
2117 while (pos < length && IsLowerCase(cb.CharAt(pos)))
2118 ++pos;
2119 } else if (IsUpperCase(startChar)) {
2120 if (IsLowerCase(cb.CharAt(pos + 1))) {
2121 ++pos;
2122 while (pos < length && IsLowerCase(cb.CharAt(pos)))
2123 ++pos;
2124 } else {
2125 while (pos < length && IsUpperCase(cb.CharAt(pos)))
2126 ++pos;
2128 if (IsLowerCase(cb.CharAt(pos)) && IsUpperCase(cb.CharAt(pos - 1)))
2129 --pos;
2130 } else if (IsADigit(startChar)) {
2131 while (pos < length && IsADigit(cb.CharAt(pos)))
2132 ++pos;
2133 } else if (IsPunctuation(startChar)) {
2134 while (pos < length && IsPunctuation(cb.CharAt(pos)))
2135 ++pos;
2136 } else if (isspacechar(startChar)) {
2137 while (pos < length && isspacechar(cb.CharAt(pos)))
2138 ++pos;
2139 } else {
2140 ++pos;
2142 return pos;
2145 bool IsLineEndChar(char c) {
2146 return (c == '\n' || c == '\r');
2149 int Document::ExtendStyleRange(int pos, int delta, bool singleLine) {
2150 int sStart = cb.StyleAt(pos);
2151 if (delta < 0) {
2152 while (pos > 0 && (cb.StyleAt(pos) == sStart) && (!singleLine || !IsLineEndChar(cb.CharAt(pos))))
2153 pos--;
2154 pos++;
2155 } else {
2156 while (pos < (Length()) && (cb.StyleAt(pos) == sStart) && (!singleLine || !IsLineEndChar(cb.CharAt(pos))))
2157 pos++;
2159 return pos;
2162 static char BraceOpposite(char ch) {
2163 switch (ch) {
2164 case '(':
2165 return ')';
2166 case ')':
2167 return '(';
2168 case '[':
2169 return ']';
2170 case ']':
2171 return '[';
2172 case '{':
2173 return '}';
2174 case '}':
2175 return '{';
2176 case '<':
2177 return '>';
2178 case '>':
2179 return '<';
2180 default:
2181 return '\0';
2185 // TODO: should be able to extend styled region to find matching brace
2186 int Document::BraceMatch(int position, int /*maxReStyle*/) {
2187 char chBrace = CharAt(position);
2188 char chSeek = BraceOpposite(chBrace);
2189 if (chSeek == '\0')
2190 return - 1;
2191 char styBrace = static_cast<char>(StyleAt(position));
2192 int direction = -1;
2193 if (chBrace == '(' || chBrace == '[' || chBrace == '{' || chBrace == '<')
2194 direction = 1;
2195 int depth = 1;
2196 position = NextPosition(position, direction);
2197 while ((position >= 0) && (position < Length())) {
2198 char chAtPos = CharAt(position);
2199 char styAtPos = static_cast<char>(StyleAt(position));
2200 if ((position > GetEndStyled()) || (styAtPos == styBrace)) {
2201 if (chAtPos == chBrace)
2202 depth++;
2203 if (chAtPos == chSeek)
2204 depth--;
2205 if (depth == 0)
2206 return position;
2208 int positionBeforeMove = position;
2209 position = NextPosition(position, direction);
2210 if (position == positionBeforeMove)
2211 break;
2213 return - 1;
2217 * Implementation of RegexSearchBase for the default built-in regular expression engine
2219 class BuiltinRegex : public RegexSearchBase {
2220 public:
2221 explicit BuiltinRegex(CharClassify *charClassTable) : search(charClassTable) {}
2223 virtual ~BuiltinRegex() {
2226 virtual long FindText(Document *doc, int minPos, int maxPos, const char *s,
2227 bool caseSensitive, bool word, bool wordStart, int flags,
2228 int *length);
2230 virtual const char *SubstituteByPosition(Document *doc, const char *text, int *length);
2232 private:
2233 RESearch search;
2234 std::string substituted;
2237 namespace {
2240 * RESearchRange keeps track of search range.
2242 class RESearchRange {
2243 public:
2244 const Document *doc;
2245 int increment;
2246 int startPos;
2247 int endPos;
2248 int lineRangeStart;
2249 int lineRangeEnd;
2250 int lineRangeBreak;
2251 RESearchRange(const Document *doc_, int minPos, int maxPos) : doc(doc_) {
2252 increment = (minPos <= maxPos) ? 1 : -1;
2254 // Range endpoints should not be inside DBCS characters, but just in case, move them.
2255 startPos = doc->MovePositionOutsideChar(minPos, 1, false);
2256 endPos = doc->MovePositionOutsideChar(maxPos, 1, false);
2258 lineRangeStart = doc->LineFromPosition(startPos);
2259 lineRangeEnd = doc->LineFromPosition(endPos);
2260 if ((increment == 1) &&
2261 (startPos >= doc->LineEnd(lineRangeStart)) &&
2262 (lineRangeStart < lineRangeEnd)) {
2263 // the start position is at end of line or between line end characters.
2264 lineRangeStart++;
2265 startPos = doc->LineStart(lineRangeStart);
2266 } else if ((increment == -1) &&
2267 (startPos <= doc->LineStart(lineRangeStart)) &&
2268 (lineRangeStart > lineRangeEnd)) {
2269 // the start position is at beginning of line.
2270 lineRangeStart--;
2271 startPos = doc->LineEnd(lineRangeStart);
2273 lineRangeBreak = lineRangeEnd + increment;
2275 Range LineRange(int line) const {
2276 Range range(doc->LineStart(line), doc->LineEnd(line));
2277 if (increment == 1) {
2278 if (line == lineRangeStart)
2279 range.start = startPos;
2280 if (line == lineRangeEnd)
2281 range.end = endPos;
2282 } else {
2283 if (line == lineRangeEnd)
2284 range.start = endPos;
2285 if (line == lineRangeStart)
2286 range.end = startPos;
2288 return range;
2292 // Define a way for the Regular Expression code to access the document
2293 class DocumentIndexer : public CharacterIndexer {
2294 Document *pdoc;
2295 int end;
2296 public:
2297 DocumentIndexer(Document *pdoc_, int end_) :
2298 pdoc(pdoc_), end(end_) {
2301 virtual ~DocumentIndexer() {
2304 virtual char CharAt(int index) {
2305 if (index < 0 || index >= end)
2306 return 0;
2307 else
2308 return pdoc->CharAt(index);
2312 #ifdef CXX11_REGEX
2314 class ByteIterator : public std::iterator<std::bidirectional_iterator_tag, char> {
2315 public:
2316 const Document *doc;
2317 Position position;
2318 ByteIterator(const Document *doc_ = 0, Position position_ = 0) : doc(doc_), position(position_) {
2320 ByteIterator(const ByteIterator &other) {
2321 doc = other.doc;
2322 position = other.position;
2324 ByteIterator &operator=(const ByteIterator &other) {
2325 if (this != &other) {
2326 doc = other.doc;
2327 position = other.position;
2329 return *this;
2331 char operator*() const {
2332 return doc->CharAt(position);
2334 ByteIterator &operator++() {
2335 position++;
2336 return *this;
2338 ByteIterator operator++(int) {
2339 ByteIterator retVal(*this);
2340 position++;
2341 return retVal;
2343 ByteIterator &operator--() {
2344 position--;
2345 return *this;
2347 bool operator==(const ByteIterator &other) const {
2348 return doc == other.doc && position == other.position;
2350 bool operator!=(const ByteIterator &other) const {
2351 return doc != other.doc || position != other.position;
2353 int Pos() const {
2354 return position;
2356 int PosRoundUp() const {
2357 return position;
2361 // On Windows, wchar_t is 16 bits wide and on Unix it is 32 bits wide.
2362 // Would be better to use sizeof(wchar_t) or similar to differentiate
2363 // but easier for now to hard-code platforms.
2364 // C++11 has char16_t and char32_t but neither Clang nor Visual C++
2365 // appear to allow specializing basic_regex over these.
2367 #ifdef _WIN32
2368 #define WCHAR_T_IS_16 1
2369 #else
2370 #define WCHAR_T_IS_16 0
2371 #endif
2373 #if WCHAR_T_IS_16
2375 // On Windows, report non-BMP characters as 2 separate surrogates as that
2376 // matches wregex since it is based on wchar_t.
2377 class UTF8Iterator : public std::iterator<std::bidirectional_iterator_tag, wchar_t> {
2378 // These 3 fields determine the iterator position and are used for comparisons
2379 const Document *doc;
2380 Position position;
2381 size_t characterIndex;
2382 // Remaining fields are derived from the determining fields so are excluded in comparisons
2383 unsigned int lenBytes;
2384 size_t lenCharacters;
2385 wchar_t buffered[2];
2386 public:
2387 UTF8Iterator(const Document *doc_ = 0, Position position_ = 0) :
2388 doc(doc_), position(position_), characterIndex(0), lenBytes(0), lenCharacters(0) {
2389 buffered[0] = 0;
2390 buffered[1] = 0;
2391 if (doc) {
2392 ReadCharacter();
2395 UTF8Iterator(const UTF8Iterator &other) {
2396 doc = other.doc;
2397 position = other.position;
2398 characterIndex = other.characterIndex;
2399 lenBytes = other.lenBytes;
2400 lenCharacters = other.lenCharacters;
2401 buffered[0] = other.buffered[0];
2402 buffered[1] = other.buffered[1];
2404 UTF8Iterator &operator=(const UTF8Iterator &other) {
2405 if (this != &other) {
2406 doc = other.doc;
2407 position = other.position;
2408 characterIndex = other.characterIndex;
2409 lenBytes = other.lenBytes;
2410 lenCharacters = other.lenCharacters;
2411 buffered[0] = other.buffered[0];
2412 buffered[1] = other.buffered[1];
2414 return *this;
2416 wchar_t operator*() const {
2417 assert(lenCharacters != 0);
2418 return buffered[characterIndex];
2420 UTF8Iterator &operator++() {
2421 if ((characterIndex + 1) < (lenCharacters)) {
2422 characterIndex++;
2423 } else {
2424 position += lenBytes;
2425 ReadCharacter();
2426 characterIndex = 0;
2428 return *this;
2430 UTF8Iterator operator++(int) {
2431 UTF8Iterator retVal(*this);
2432 if ((characterIndex + 1) < (lenCharacters)) {
2433 characterIndex++;
2434 } else {
2435 position += lenBytes;
2436 ReadCharacter();
2437 characterIndex = 0;
2439 return retVal;
2441 UTF8Iterator &operator--() {
2442 if (characterIndex) {
2443 characterIndex--;
2444 } else {
2445 position = doc->NextPosition(position, -1);
2446 ReadCharacter();
2447 characterIndex = lenCharacters - 1;
2449 return *this;
2451 bool operator==(const UTF8Iterator &other) const {
2452 // Only test the determining fields, not the character widths and values derived from this
2453 return doc == other.doc &&
2454 position == other.position &&
2455 characterIndex == other.characterIndex;
2457 bool operator!=(const UTF8Iterator &other) const {
2458 // Only test the determining fields, not the character widths and values derived from this
2459 return doc != other.doc ||
2460 position != other.position ||
2461 characterIndex != other.characterIndex;
2463 int Pos() const {
2464 return position;
2466 int PosRoundUp() const {
2467 if (characterIndex)
2468 return position + lenBytes; // Force to end of character
2469 else
2470 return position;
2472 private:
2473 void ReadCharacter() {
2474 Document::CharacterExtracted charExtracted = doc->ExtractCharacter(position);
2475 lenBytes = charExtracted.widthBytes;
2476 if (charExtracted.character == unicodeReplacementChar) {
2477 lenCharacters = 1;
2478 buffered[0] = static_cast<wchar_t>(charExtracted.character);
2479 } else {
2480 lenCharacters = UTF16FromUTF32Character(charExtracted.character, buffered);
2485 #else
2487 // On Unix, report non-BMP characters as single characters
2489 class UTF8Iterator : public std::iterator<std::bidirectional_iterator_tag, wchar_t> {
2490 const Document *doc;
2491 Position position;
2492 public:
2493 UTF8Iterator(const Document *doc_=0, Position position_=0) : doc(doc_), position(position_) {
2495 UTF8Iterator(const UTF8Iterator &other) {
2496 doc = other.doc;
2497 position = other.position;
2499 UTF8Iterator &operator=(const UTF8Iterator &other) {
2500 if (this != &other) {
2501 doc = other.doc;
2502 position = other.position;
2504 return *this;
2506 wchar_t operator*() const {
2507 Document::CharacterExtracted charExtracted = doc->ExtractCharacter(position);
2508 return charExtracted.character;
2510 UTF8Iterator &operator++() {
2511 position = doc->NextPosition(position, 1);
2512 return *this;
2514 UTF8Iterator operator++(int) {
2515 UTF8Iterator retVal(*this);
2516 position = doc->NextPosition(position, 1);
2517 return retVal;
2519 UTF8Iterator &operator--() {
2520 position = doc->NextPosition(position, -1);
2521 return *this;
2523 bool operator==(const UTF8Iterator &other) const {
2524 return doc == other.doc && position == other.position;
2526 bool operator!=(const UTF8Iterator &other) const {
2527 return doc != other.doc || position != other.position;
2529 int Pos() const {
2530 return position;
2532 int PosRoundUp() const {
2533 return position;
2537 #endif
2539 std::regex_constants::match_flag_type MatchFlags(const Document *doc, int startPos, int endPos) {
2540 std::regex_constants::match_flag_type flagsMatch = std::regex_constants::match_default;
2541 if (!doc->IsLineStartPosition(startPos))
2542 flagsMatch |= std::regex_constants::match_not_bol;
2543 if (!doc->IsLineEndPosition(endPos))
2544 flagsMatch |= std::regex_constants::match_not_eol;
2545 return flagsMatch;
2548 template<typename Iterator, typename Regex>
2549 bool MatchOnLines(const Document *doc, const Regex &regexp, const RESearchRange &resr, RESearch &search) {
2550 bool matched = false;
2551 std::match_results<Iterator> match;
2553 // MSVC and libc++ have problems with ^ and $ matching line ends inside a range
2554 // If they didn't then the line by line iteration could be removed for the forwards
2555 // case and replaced with the following 4 lines:
2556 // Iterator uiStart(doc, startPos);
2557 // Iterator uiEnd(doc, endPos);
2558 // flagsMatch = MatchFlags(doc, startPos, endPos);
2559 // matched = std::regex_search(uiStart, uiEnd, match, regexp, flagsMatch);
2561 // Line by line.
2562 for (int line = resr.lineRangeStart; line != resr.lineRangeBreak; line += resr.increment) {
2563 const Range lineRange = resr.LineRange(line);
2564 Iterator itStart(doc, lineRange.start);
2565 Iterator itEnd(doc, lineRange.end);
2566 std::regex_constants::match_flag_type flagsMatch = MatchFlags(doc, lineRange.start, lineRange.end);
2567 matched = std::regex_search(itStart, itEnd, match, regexp, flagsMatch);
2568 // Check for the last match on this line.
2569 if (matched) {
2570 if (resr.increment == -1) {
2571 while (matched) {
2572 Iterator itNext(doc, match[0].second.PosRoundUp());
2573 flagsMatch = MatchFlags(doc, itNext.Pos(), lineRange.end);
2574 std::match_results<Iterator> matchNext;
2575 matched = std::regex_search(itNext, itEnd, matchNext, regexp, flagsMatch);
2576 if (matched) {
2577 if (match[0].first == match[0].second) {
2578 // Empty match means failure so exit
2579 return false;
2581 match = matchNext;
2584 matched = true;
2586 break;
2589 if (matched) {
2590 for (size_t co = 0; co < match.size(); co++) {
2591 search.bopat[co] = match[co].first.Pos();
2592 search.eopat[co] = match[co].second.PosRoundUp();
2593 size_t lenMatch = search.eopat[co] - search.bopat[co];
2594 search.pat[co].resize(lenMatch);
2595 for (size_t iPos = 0; iPos < lenMatch; iPos++) {
2596 search.pat[co][iPos] = doc->CharAt(iPos + search.bopat[co]);
2600 return matched;
2603 long Cxx11RegexFindText(Document *doc, int minPos, int maxPos, const char *s,
2604 bool caseSensitive, int *length, RESearch &search) {
2605 const RESearchRange resr(doc, minPos, maxPos);
2606 try {
2607 //ElapsedTime et;
2608 std::regex::flag_type flagsRe = std::regex::ECMAScript;
2609 // Flags that apper to have no effect:
2610 // | std::regex::collate | std::regex::extended;
2611 if (!caseSensitive)
2612 flagsRe = flagsRe | std::regex::icase;
2614 // Clear the RESearch so can fill in matches
2615 search.Clear();
2617 bool matched = false;
2618 if (SC_CP_UTF8 == doc->dbcsCodePage) {
2619 unsigned int lenS = static_cast<unsigned int>(strlen(s));
2620 std::vector<wchar_t> ws(lenS + 1);
2621 #if WCHAR_T_IS_16
2622 size_t outLen = UTF16FromUTF8(s, lenS, &ws[0], lenS);
2623 #else
2624 size_t outLen = UTF32FromUTF8(s, lenS, reinterpret_cast<unsigned int *>(&ws[0]), lenS);
2625 #endif
2626 ws[outLen] = 0;
2627 std::wregex regexp;
2628 #if defined(__APPLE__)
2629 // Using a UTF-8 locale doesn't change to Unicode over a byte buffer so '.'
2630 // is one byte not one character.
2631 // However, on OS X this makes wregex act as Unicode
2632 std::locale localeU("en_US.UTF-8");
2633 regexp.imbue(localeU);
2634 #endif
2635 regexp.assign(&ws[0], flagsRe);
2636 matched = MatchOnLines<UTF8Iterator>(doc, regexp, resr, search);
2638 } else {
2639 std::regex regexp;
2640 regexp.assign(s, flagsRe);
2641 matched = MatchOnLines<ByteIterator>(doc, regexp, resr, search);
2644 int posMatch = -1;
2645 if (matched) {
2646 posMatch = search.bopat[0];
2647 *length = search.eopat[0] - search.bopat[0];
2649 // Example - search in doc/ScintillaHistory.html for
2650 // [[:upper:]]eta[[:space:]]
2651 // On MacBook, normally around 1 second but with locale imbued -> 14 seconds.
2652 //double durSearch = et.Duration(true);
2653 //Platform::DebugPrintf("Search:%9.6g \n", durSearch);
2654 return posMatch;
2655 } catch (std::regex_error &) {
2656 // Failed to create regular expression
2657 throw RegexError();
2658 } catch (...) {
2659 // Failed in some other way
2660 return -1;
2664 #endif
2668 long BuiltinRegex::FindText(Document *doc, int minPos, int maxPos, const char *s,
2669 bool caseSensitive, bool, bool, int flags,
2670 int *length) {
2672 #ifdef CXX11_REGEX
2673 if (flags & SCFIND_CXX11REGEX) {
2674 return Cxx11RegexFindText(doc, minPos, maxPos, s,
2675 caseSensitive, length, search);
2677 #endif
2679 const RESearchRange resr(doc, minPos, maxPos);
2681 const bool posix = (flags & SCFIND_POSIX) != 0;
2683 const char *errmsg = search.Compile(s, *length, caseSensitive, posix);
2684 if (errmsg) {
2685 return -1;
2687 // Find a variable in a property file: \$(\([A-Za-z0-9_.]+\))
2688 // Replace first '.' with '-' in each property file variable reference:
2689 // Search: \$(\([A-Za-z0-9_-]+\)\.\([A-Za-z0-9_.]+\))
2690 // Replace: $(\1-\2)
2691 int pos = -1;
2692 int lenRet = 0;
2693 const char searchEnd = s[*length - 1];
2694 const char searchEndPrev = (*length > 1) ? s[*length - 2] : '\0';
2695 for (int line = resr.lineRangeStart; line != resr.lineRangeBreak; line += resr.increment) {
2696 int startOfLine = doc->LineStart(line);
2697 int endOfLine = doc->LineEnd(line);
2698 if (resr.increment == 1) {
2699 if (line == resr.lineRangeStart) {
2700 if ((resr.startPos != startOfLine) && (s[0] == '^'))
2701 continue; // Can't match start of line if start position after start of line
2702 startOfLine = resr.startPos;
2704 if (line == resr.lineRangeEnd) {
2705 if ((resr.endPos != endOfLine) && (searchEnd == '$') && (searchEndPrev != '\\'))
2706 continue; // Can't match end of line if end position before end of line
2707 endOfLine = resr.endPos;
2709 } else {
2710 if (line == resr.lineRangeEnd) {
2711 if ((resr.endPos != startOfLine) && (s[0] == '^'))
2712 continue; // Can't match start of line if end position after start of line
2713 startOfLine = resr.endPos;
2715 if (line == resr.lineRangeStart) {
2716 if ((resr.startPos != endOfLine) && (searchEnd == '$') && (searchEndPrev != '\\'))
2717 continue; // Can't match end of line if start position before end of line
2718 endOfLine = resr.startPos;
2722 DocumentIndexer di(doc, endOfLine);
2723 int success = search.Execute(di, startOfLine, endOfLine);
2724 if (success) {
2725 pos = search.bopat[0];
2726 // Ensure only whole characters selected
2727 search.eopat[0] = doc->MovePositionOutsideChar(search.eopat[0], 1, false);
2728 lenRet = search.eopat[0] - search.bopat[0];
2729 // There can be only one start of a line, so no need to look for last match in line
2730 if ((resr.increment == -1) && (s[0] != '^')) {
2731 // Check for the last match on this line.
2732 int repetitions = 1000; // Break out of infinite loop
2733 while (success && (search.eopat[0] <= endOfLine) && (repetitions--)) {
2734 success = search.Execute(di, pos+1, endOfLine);
2735 if (success) {
2736 if (search.eopat[0] <= minPos) {
2737 pos = search.bopat[0];
2738 lenRet = search.eopat[0] - search.bopat[0];
2739 } else {
2740 success = 0;
2745 break;
2748 *length = lenRet;
2749 return pos;
2752 const char *BuiltinRegex::SubstituteByPosition(Document *doc, const char *text, int *length) {
2753 substituted.clear();
2754 DocumentIndexer di(doc, doc->Length());
2755 search.GrabMatches(di);
2756 for (int j = 0; j < *length; j++) {
2757 if (text[j] == '\\') {
2758 if (text[j + 1] >= '0' && text[j + 1] <= '9') {
2759 unsigned int patNum = text[j + 1] - '0';
2760 unsigned int len = search.eopat[patNum] - search.bopat[patNum];
2761 if (!search.pat[patNum].empty()) // Will be null if try for a match that did not occur
2762 substituted.append(search.pat[patNum].c_str(), len);
2763 j++;
2764 } else {
2765 j++;
2766 switch (text[j]) {
2767 case 'a':
2768 substituted.push_back('\a');
2769 break;
2770 case 'b':
2771 substituted.push_back('\b');
2772 break;
2773 case 'f':
2774 substituted.push_back('\f');
2775 break;
2776 case 'n':
2777 substituted.push_back('\n');
2778 break;
2779 case 'r':
2780 substituted.push_back('\r');
2781 break;
2782 case 't':
2783 substituted.push_back('\t');
2784 break;
2785 case 'v':
2786 substituted.push_back('\v');
2787 break;
2788 case '\\':
2789 substituted.push_back('\\');
2790 break;
2791 default:
2792 substituted.push_back('\\');
2793 j--;
2796 } else {
2797 substituted.push_back(text[j]);
2800 *length = static_cast<int>(substituted.length());
2801 return substituted.c_str();
2804 #ifndef SCI_OWNREGEX
2806 #ifdef SCI_NAMESPACE
2808 RegexSearchBase *Scintilla::CreateRegexSearch(CharClassify *charClassTable) {
2809 return new BuiltinRegex(charClassTable);
2812 #else
2814 RegexSearchBase *CreateRegexSearch(CharClassify *charClassTable) {
2815 return new BuiltinRegex(charClassTable);
2818 #endif
2820 #endif