1
// Scintilla source code edit control
3
** Text document that handles notifications, DBCS, styling, words and end of line.
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.
20
#include "Scintilla.h"
22
#include "SplitVector.h"
23
#include "Partitioning.h"
24
#include "RunStyles.h"
25
#include "CellBuffer.h"
27
#include "CharClassify.h"
28
#include "CharacterSet.h"
29
#include "Decoration.h"
32
#include "UniConversion.h"
35
using namespace Scintilla;
38
// This is ASCII specific but is safe with chars >= 0x80
39
static inline bool isspacechar(unsigned char ch) {
40
return (ch == ' ') || ((ch >= 0x09) && (ch <= 0x0d));
43
static inline bool IsPunctuation(char ch) {
44
return isascii(ch) && ispunct(ch);
47
static inline bool IsADigit(char ch) {
48
return isascii(ch) && isdigit(ch);
51
static inline bool IsLowerCase(char ch) {
52
return isascii(ch) && islower(ch);
55
static inline bool IsUpperCase(char ch) {
56
return isascii(ch) && isupper(ch);
59
void LexInterface::Colourise(int start, int end) {
61
if (pdoc && instance && !performingStyle) {
62
// Protect against reentrance, which may occur, for example, when
63
// fold points are discovered while performing styling and the folding
64
// code looks for child lines which may trigger styling.
65
performingStyle = true;
67
int lengthDoc = pdoc->Length();
70
int len = end - start;
72
PLATFORM_ASSERT(len >= 0);
73
PLATFORM_ASSERT(start + len <= lengthDoc);
77
styleStart = pdoc->StyleAt(start - 1) & pdoc->stylingBitsMask;
80
instance->Lex(start, len, styleStart, pdoc);
81
instance->Fold(start, len, styleStart, pdoc);
84
performingStyle = false;
88
Document::Document() {
93
eolMode = SC_EOL_CRLF;
97
stylingBitsMask = 0x1F;
101
enteredModification = 0;
103
enteredReadOnlyCount = 0;
106
actualIndentInChars = 8;
109
backspaceUnindents = false;
113
matchesValid = false;
116
perLineData[ldMarkers] = new LineMarkers();
117
perLineData[ldLevels] = new LineLevels();
118
perLineData[ldState] = new LineState();
119
perLineData[ldMargin] = new LineAnnotation();
120
perLineData[ldAnnotation] = new LineAnnotation();
127
Document::~Document() {
128
for (int i = 0; i < lenWatchers; i++) {
129
watchers[i].watcher->NotifyDeleted(this, watchers[i].userData);
132
for (int j=0; j<ldSize; j++) {
133
delete perLineData[j];
144
void Document::Init() {
145
for (int j=0; j<ldSize; j++) {
147
perLineData[j]->Init();
151
void Document::InsertLine(int line) {
152
for (int j=0; j<ldSize; j++) {
154
perLineData[j]->InsertLine(line);
158
void Document::RemoveLine(int line) {
159
for (int j=0; j<ldSize; j++) {
161
perLineData[j]->RemoveLine(line);
165
// Increase reference count and return its previous value.
166
int Document::AddRef() {
170
// Decrease reference count and return its previous value.
171
// Delete the document if reference count reaches zero.
172
int Document::Release() {
173
int curRefCount = --refCount;
174
if (curRefCount == 0)
179
void Document::SetSavePoint() {
181
NotifySavePoint(true);
184
int Document::GetMark(int line) {
185
return static_cast<LineMarkers *>(perLineData[ldMarkers])->MarkValue(line);
188
int Document::AddMark(int line, int markerNum) {
189
if (line >= 0 && line <= LinesTotal()) {
190
int prev = static_cast<LineMarkers *>(perLineData[ldMarkers])->
191
AddMark(line, markerNum, LinesTotal());
192
DocModification mh(SC_MOD_CHANGEMARKER, LineStart(line), 0, 0, 0, line);
200
void Document::AddMarkSet(int line, int valueSet) {
201
if (line < 0 || line > LinesTotal()) {
204
unsigned int m = valueSet;
205
for (int i = 0; m; i++, m >>= 1)
207
static_cast<LineMarkers *>(perLineData[ldMarkers])->
208
AddMark(line, i, LinesTotal());
209
DocModification mh(SC_MOD_CHANGEMARKER, LineStart(line), 0, 0, 0, line);
213
void Document::DeleteMark(int line, int markerNum) {
214
static_cast<LineMarkers *>(perLineData[ldMarkers])->DeleteMark(line, markerNum, false);
215
DocModification mh(SC_MOD_CHANGEMARKER, LineStart(line), 0, 0, 0, line);
219
void Document::DeleteMarkFromHandle(int markerHandle) {
220
static_cast<LineMarkers *>(perLineData[ldMarkers])->DeleteMarkFromHandle(markerHandle);
221
DocModification mh(SC_MOD_CHANGEMARKER, 0, 0, 0, 0);
226
void Document::DeleteAllMarks(int markerNum) {
227
bool someChanges = false;
228
for (int line = 0; line < LinesTotal(); line++) {
229
if (static_cast<LineMarkers *>(perLineData[ldMarkers])->DeleteMark(line, markerNum, true))
233
DocModification mh(SC_MOD_CHANGEMARKER, 0, 0, 0, 0);
239
int Document::LineFromHandle(int markerHandle) {
240
return static_cast<LineMarkers *>(perLineData[ldMarkers])->LineFromHandle(markerHandle);
243
int SCI_METHOD Document::LineStart(int line) const {
244
return cb.LineStart(line);
247
int Document::LineEnd(int line) const {
248
if (line == LinesTotal() - 1) {
249
return LineStart(line + 1);
251
int position = LineStart(line + 1) - 1;
252
// When line terminator is CR+LF, may need to go back one more
253
if ((position > LineStart(line)) && (cb.CharAt(position - 1) == '\r')) {
260
void SCI_METHOD Document::SetErrorStatus(int status) {
261
// Tell the watchers the lexer has changed.
262
for (int i = 0; i < lenWatchers; i++) {
263
watchers[i].watcher->NotifyErrorOccurred(this, watchers[i].userData, status);
267
int SCI_METHOD Document::LineFromPosition(int pos) const {
268
return cb.LineFromPosition(pos);
271
int Document::LineEndPosition(int position) const {
272
return LineEnd(LineFromPosition(position));
275
bool Document::IsLineEndPosition(int position) const {
276
return LineEnd(LineFromPosition(position)) == position;
279
int Document::VCHomePosition(int position) const {
280
int line = LineFromPosition(position);
281
int startPosition = LineStart(line);
282
int endLine = LineEnd(line);
283
int startText = startPosition;
284
while (startText < endLine && (cb.CharAt(startText) == ' ' || cb.CharAt(startText) == '\t'))
286
if (position == startText)
287
return startPosition;
292
int SCI_METHOD Document::SetLevel(int line, int level) {
293
int prev = static_cast<LineLevels *>(perLineData[ldLevels])->SetLevel(line, level, LinesTotal());
295
DocModification mh(SC_MOD_CHANGEFOLD | SC_MOD_CHANGEMARKER,
296
LineStart(line), 0, 0, 0, line);
297
mh.foldLevelNow = level;
298
mh.foldLevelPrev = prev;
304
int SCI_METHOD Document::GetLevel(int line) const {
305
return static_cast<LineLevels *>(perLineData[ldLevels])->GetLevel(line);
308
void Document::ClearLevels() {
309
static_cast<LineLevels *>(perLineData[ldLevels])->ClearLevels();
312
static bool IsSubordinate(int levelStart, int levelTry) {
313
if (levelTry & SC_FOLDLEVELWHITEFLAG)
316
return (levelStart & SC_FOLDLEVELNUMBERMASK) < (levelTry & SC_FOLDLEVELNUMBERMASK);
319
int Document::GetLastChild(int lineParent, int level) {
321
level = GetLevel(lineParent) & SC_FOLDLEVELNUMBERMASK;
322
int maxLine = LinesTotal();
323
int lineMaxSubord = lineParent;
324
while (lineMaxSubord < maxLine - 1) {
325
EnsureStyledTo(LineStart(lineMaxSubord + 2));
326
if (!IsSubordinate(level, GetLevel(lineMaxSubord + 1)))
330
if (lineMaxSubord > lineParent) {
331
if (level > (GetLevel(lineMaxSubord + 1) & SC_FOLDLEVELNUMBERMASK)) {
332
// Have chewed up some whitespace that belongs to a parent so seek back
333
if (GetLevel(lineMaxSubord) & SC_FOLDLEVELWHITEFLAG) {
338
return lineMaxSubord;
341
int Document::GetFoldParent(int line) {
342
int level = GetLevel(line) & SC_FOLDLEVELNUMBERMASK;
343
int lineLook = line - 1;
344
while ((lineLook > 0) && (
345
(!(GetLevel(lineLook) & SC_FOLDLEVELHEADERFLAG)) ||
346
((GetLevel(lineLook) & SC_FOLDLEVELNUMBERMASK) >= level))
350
if ((GetLevel(lineLook) & SC_FOLDLEVELHEADERFLAG) &&
351
((GetLevel(lineLook) & SC_FOLDLEVELNUMBERMASK) < level)) {
358
int Document::ClampPositionIntoDocument(int pos) {
359
return Platform::Clamp(pos, 0, Length());
362
bool Document::IsCrLf(int pos) {
365
if (pos >= (Length() - 1))
367
return (cb.CharAt(pos) == '\r') && (cb.CharAt(pos + 1) == '\n');
370
int Document::LenChar(int pos) {
373
} else if (IsCrLf(pos)) {
375
} else if (SC_CP_UTF8 == dbcsCodePage) {
376
unsigned char ch = static_cast<unsigned char>(cb.CharAt(pos));
380
if (ch >= (0x80 + 0x40 + 0x20 + 0x10))
382
else if (ch >= (0x80 + 0x40 + 0x20))
384
int lengthDoc = Length();
385
if ((pos + len) > lengthDoc)
386
return lengthDoc -pos;
389
} else if (dbcsCodePage) {
390
return IsDBCSLeadByte(cb.CharAt(pos)) ? 2 : 1;
396
static bool IsTrailByte(int ch) {
397
return (ch >= 0x80) && (ch < (0x80 + 0x40));
400
static int BytesFromLead(int leadByte) {
401
if (leadByte > 0xF4) {
402
// Characters longer than 4 bytes not possible in current UTF-8
404
} else if (leadByte >= 0xF0) {
406
} else if (leadByte >= 0xE0) {
408
} else if (leadByte >= 0xC2) {
414
bool Document::InGoodUTF8(int pos, int &start, int &end) const {
416
while ((lead>0) && (pos-lead < 4) && IsTrailByte(static_cast<unsigned char>(cb.CharAt(lead-1))))
422
int leadByte = static_cast<unsigned char>(cb.CharAt(start));
423
int bytes = BytesFromLead(leadByte);
427
int trailBytes = bytes - 1;
428
int len = pos - lead + 1;
429
if (len > trailBytes)
430
// pos too far from lead
432
// Check that there are enough trails for this lead
434
while ((trail-lead<trailBytes) && (trail < Length())) {
435
if (!IsTrailByte(static_cast<unsigned char>(cb.CharAt(trail)))) {
445
// Normalise a position so that it is not halfway through a two byte character.
446
// This can occur in two situations -
447
// When lines are terminated with \r\n pairs which should be treated as one character.
448
// When displaying DBCS text such as Japanese.
449
// If moving, move the position in the indicated direction.
450
int Document::MovePositionOutsideChar(int pos, int moveDir, bool checkLineEnd) {
451
//Platform::DebugPrintf("NoCRLF %d %d\n", pos, moveDir);
452
// If out of range, just return minimum/maximum value.
458
// PLATFORM_ASSERT(pos > 0 && pos < Length());
459
if (checkLineEnd && IsCrLf(pos - 1)) {
467
if (SC_CP_UTF8 == dbcsCodePage) {
468
unsigned char ch = static_cast<unsigned char>(cb.CharAt(pos));
471
if (IsTrailByte(ch) && InGoodUTF8(pos, startUTF, endUTF)) {
472
// ch is a trail byte within a UTF-8 character
479
// Anchor DBCS calculations at start of line because start of line can
480
// not be a DBCS trail byte.
481
int posStartLine = LineStart(LineFromPosition(pos));
482
if (pos == posStartLine)
485
// Step back until a non-lead-byte is found.
487
while ((posCheck > posStartLine) && IsDBCSLeadByte(cb.CharAt(posCheck-1)))
490
// Check from known start of character.
491
while (posCheck < pos) {
492
int mbsize = IsDBCSLeadByte(cb.CharAt(posCheck)) ? 2 : 1;
493
if (posCheck + mbsize == pos) {
495
} else if (posCheck + mbsize > pos) {
497
return posCheck + mbsize;
510
// NextPosition moves between valid positions - it can not handle a position in the middle of a
511
// multi-byte character. It is used to iterate through text more efficiently than MovePositionOutsideChar.
512
// A \r\n pair is treated as two characters.
513
int Document::NextPosition(int pos, int moveDir) const {
514
// If out of range, just return minimum/maximum value.
515
int increment = (moveDir > 0) ? 1 : -1;
516
if (pos + increment <= 0)
518
if (pos + increment >= Length())
522
if (SC_CP_UTF8 == dbcsCodePage) {
524
unsigned char ch = static_cast<unsigned char>(cb.CharAt(pos));
527
if (IsTrailByte(ch) && InGoodUTF8(pos, startUTF, endUTF)) {
528
// ch is a trail byte within a UTF-8 character
536
int mbsize = IsDBCSLeadByte(cb.CharAt(pos)) ? 2 : 1;
541
// Anchor DBCS calculations at start of line because start of line can
542
// not be a DBCS trail byte.
543
int posStartLine = LineStart(LineFromPosition(pos));
544
// See http://msdn.microsoft.com/en-us/library/cc194792%28v=MSDN.10%29.aspx
545
// http://msdn.microsoft.com/en-us/library/cc194790.aspx
546
if ((pos - 1) <= posStartLine) {
548
} else if (IsDBCSLeadByte(cb.CharAt(pos - 1))) {
549
// Must actually be trail byte
552
// Otherwise, step back until a non-lead-byte is found.
553
int posTemp = pos - 1;
554
while (posStartLine <= --posTemp && IsDBCSLeadByte(cb.CharAt(posTemp)))
556
// Now posTemp+1 must point to the beginning of a character,
557
// so figure out whether we went back an even or an odd
558
// number of bytes and go back 1 or 2 bytes, respectively.
559
return (pos - 1 - ((pos - posTemp) & 1));
570
bool Document::NextCharacter(int &pos, int moveDir) {
571
// Returns true if pos changed
572
int posNext = NextPosition(pos, moveDir);
573
if (posNext == pos) {
581
int SCI_METHOD Document::CodePage() const {
585
bool SCI_METHOD Document::IsDBCSLeadByte(char ch) const {
586
// Byte ranges found in Wikipedia articles with relevant search strings in each case
587
unsigned char uch = static_cast<unsigned char>(ch);
588
switch (dbcsCodePage) {
591
return ((uch >= 0x81) && (uch <= 0x9F)) ||
592
((uch >= 0xE0) && (uch <= 0xEF));
595
return (uch >= 0x81) && (uch <= 0xFE);
597
// Korean Wansung KS C-5601-1987
598
return (uch >= 0x81) && (uch <= 0xFE);
601
return (uch >= 0x81) && (uch <= 0xFE);
603
// Korean Johab KS C-5601-1992
605
((uch >= 0x84) && (uch <= 0xD3)) ||
606
((uch >= 0xD8) && (uch <= 0xDE)) ||
607
((uch >= 0xE0) && (uch <= 0xF9));
612
void Document::ModifiedAt(int pos) {
617
void Document::CheckReadOnly() {
618
if (cb.IsReadOnly() && enteredReadOnlyCount == 0) {
619
enteredReadOnlyCount++;
620
NotifyModifyAttempt();
621
enteredReadOnlyCount--;
625
// Document only modified by gateways DeleteChars, InsertString, Undo, Redo, and SetStyleAt.
626
// SetStyleAt does not change the persistent state of a document
628
bool Document::DeleteChars(int pos, int len) {
631
if ((pos + len) > Length())
634
if (enteredModification != 0) {
637
enteredModification++;
638
if (!cb.IsReadOnly()) {
641
SC_MOD_BEFOREDELETE | SC_PERFORMED_USER,
644
int prevLinesTotal = LinesTotal();
645
bool startSavePoint = cb.IsSavePoint();
646
bool startSequence = false;
647
const char *text = cb.DeleteChars(pos, len, startSequence);
648
if (startSavePoint && cb.IsCollectingUndo())
649
NotifySavePoint(!startSavePoint);
650
if ((pos < Length()) || (pos == 0))
656
SC_MOD_DELETETEXT | SC_PERFORMED_USER | (startSequence?SC_STARTACTION:0),
658
LinesTotal() - prevLinesTotal, text));
660
enteredModification--;
662
return !cb.IsReadOnly();
666
* Insert a string with a length.
668
bool Document::InsertString(int position, const char *s, int insertLength) {
669
if (insertLength <= 0) {
673
if (enteredModification != 0) {
676
enteredModification++;
677
if (!cb.IsReadOnly()) {
680
SC_MOD_BEFOREINSERT | SC_PERFORMED_USER,
681
position, insertLength,
683
int prevLinesTotal = LinesTotal();
684
bool startSavePoint = cb.IsSavePoint();
685
bool startSequence = false;
686
const char *text = cb.InsertString(position, s, insertLength, startSequence);
687
if (startSavePoint && cb.IsCollectingUndo())
688
NotifySavePoint(!startSavePoint);
689
ModifiedAt(position);
692
SC_MOD_INSERTTEXT | SC_PERFORMED_USER | (startSequence?SC_STARTACTION:0),
693
position, insertLength,
694
LinesTotal() - prevLinesTotal, text));
696
enteredModification--;
698
return !cb.IsReadOnly();
701
int Document::Undo() {
704
if (enteredModification == 0) {
705
enteredModification++;
706
if (!cb.IsReadOnly()) {
707
bool startSavePoint = cb.IsSavePoint();
708
bool multiLine = false;
709
int steps = cb.StartUndo();
710
//Platform::DebugPrintf("Steps=%d\n", steps);
711
for (int step = 0; step < steps; step++) {
712
const int prevLinesTotal = LinesTotal();
713
const Action &action = cb.GetUndoStep();
714
if (action.at == removeAction) {
715
NotifyModified(DocModification(
716
SC_MOD_BEFOREINSERT | SC_PERFORMED_UNDO, action));
717
} else if (action.at == containerAction) {
718
DocModification dm(SC_MOD_CONTAINER | SC_PERFORMED_UNDO);
719
dm.token = action.position;
722
NotifyModified(DocModification(
723
SC_MOD_BEFOREDELETE | SC_PERFORMED_UNDO, action));
725
cb.PerformUndoStep();
726
int cellPosition = action.position;
727
if (action.at != containerAction) {
728
ModifiedAt(cellPosition);
729
newPos = cellPosition;
732
int modFlags = SC_PERFORMED_UNDO;
733
// With undo, an insertion action becomes a deletion notification
734
if (action.at == removeAction) {
735
newPos += action.lenData;
736
modFlags |= SC_MOD_INSERTTEXT;
737
} else if (action.at == insertAction) {
738
modFlags |= SC_MOD_DELETETEXT;
741
modFlags |= SC_MULTISTEPUNDOREDO;
742
const int linesAdded = LinesTotal() - prevLinesTotal;
745
if (step == steps - 1) {
746
modFlags |= SC_LASTSTEPINUNDOREDO;
748
modFlags |= SC_MULTILINEUNDOREDO;
750
NotifyModified(DocModification(modFlags, cellPosition, action.lenData,
751
linesAdded, action.data));
754
bool endSavePoint = cb.IsSavePoint();
755
if (startSavePoint != endSavePoint)
756
NotifySavePoint(endSavePoint);
758
enteredModification--;
763
int Document::Redo() {
766
if (enteredModification == 0) {
767
enteredModification++;
768
if (!cb.IsReadOnly()) {
769
bool startSavePoint = cb.IsSavePoint();
770
bool multiLine = false;
771
int steps = cb.StartRedo();
772
for (int step = 0; step < steps; step++) {
773
const int prevLinesTotal = LinesTotal();
774
const Action &action = cb.GetRedoStep();
775
if (action.at == insertAction) {
776
NotifyModified(DocModification(
777
SC_MOD_BEFOREINSERT | SC_PERFORMED_REDO, action));
778
} else if (action.at == containerAction) {
779
DocModification dm(SC_MOD_CONTAINER | SC_PERFORMED_REDO);
780
dm.token = action.position;
783
NotifyModified(DocModification(
784
SC_MOD_BEFOREDELETE | SC_PERFORMED_REDO, action));
786
cb.PerformRedoStep();
787
if (action.at != containerAction) {
788
ModifiedAt(action.position);
789
newPos = action.position;
792
int modFlags = SC_PERFORMED_REDO;
793
if (action.at == insertAction) {
794
newPos += action.lenData;
795
modFlags |= SC_MOD_INSERTTEXT;
796
} else if (action.at == removeAction) {
797
modFlags |= SC_MOD_DELETETEXT;
800
modFlags |= SC_MULTISTEPUNDOREDO;
801
const int linesAdded = LinesTotal() - prevLinesTotal;
804
if (step == steps - 1) {
805
modFlags |= SC_LASTSTEPINUNDOREDO;
807
modFlags |= SC_MULTILINEUNDOREDO;
810
DocModification(modFlags, action.position, action.lenData,
811
linesAdded, action.data));
814
bool endSavePoint = cb.IsSavePoint();
815
if (startSavePoint != endSavePoint)
816
NotifySavePoint(endSavePoint);
818
enteredModification--;
824
* Insert a single character.
826
bool Document::InsertChar(int pos, char ch) {
829
return InsertString(pos, chs, 1);
833
* Insert a null terminated string.
835
bool Document::InsertCString(int position, const char *s) {
836
return InsertString(position, s, strlen(s));
839
void Document::ChangeChar(int pos, char ch) {
844
void Document::DelChar(int pos) {
845
DeleteChars(pos, LenChar(pos));
848
void Document::DelCharBack(int pos) {
851
} else if (IsCrLf(pos - 2)) {
852
DeleteChars(pos - 2, 2);
853
} else if (dbcsCodePage) {
854
int startChar = NextPosition(pos, -1);
855
DeleteChars(startChar, pos - startChar);
857
DeleteChars(pos - 1, 1);
861
static bool isindentchar(char ch) {
862
return (ch == ' ') || (ch == '\t');
865
static int NextTab(int pos, int tabSize) {
866
return ((pos / tabSize) + 1) * tabSize;
869
static void CreateIndentation(char *linebuf, int length, int indent, int tabSize, bool insertSpaces) {
870
length--; // ensure space for \0
872
while ((indent >= tabSize) && (length > 0)) {
878
while ((indent > 0) && (length > 0)) {
886
int SCI_METHOD Document::GetLineIndentation(int line) {
888
if ((line >= 0) && (line < LinesTotal())) {
889
int lineStart = LineStart(line);
890
int length = Length();
891
for (int i = lineStart; i < length; i++) {
892
char ch = cb.CharAt(i);
896
indent = NextTab(indent, tabInChars);
904
void Document::SetLineIndentation(int line, int indent) {
905
int indentOfLine = GetLineIndentation(line);
908
if (indent != indentOfLine) {
910
CreateIndentation(linebuf, sizeof(linebuf), indent, tabInChars, !useTabs);
911
int thisLineStart = LineStart(line);
912
int indentPos = GetLineIndentPosition(line);
914
DeleteChars(thisLineStart, indentPos - thisLineStart);
915
InsertCString(thisLineStart, linebuf);
919
int Document::GetLineIndentPosition(int line) const {
922
int pos = LineStart(line);
923
int length = Length();
924
while ((pos < length) && isindentchar(cb.CharAt(pos))) {
930
int Document::GetColumn(int pos) {
932
int line = LineFromPosition(pos);
933
if ((line >= 0) && (line < LinesTotal())) {
934
for (int i = LineStart(line); i < pos;) {
935
char ch = cb.CharAt(i);
937
column = NextTab(column, tabInChars);
939
} else if (ch == '\r') {
941
} else if (ch == '\n') {
943
} else if (i >= Length()) {
947
i = NextPosition(i, 1);
954
int Document::FindColumn(int line, int column) {
955
int position = LineStart(line);
956
if ((line >= 0) && (line < LinesTotal())) {
957
int columnCurrent = 0;
958
while ((columnCurrent < column) && (position < Length())) {
959
char ch = cb.CharAt(position);
961
columnCurrent = NextTab(columnCurrent, tabInChars);
963
} else if (ch == '\r') {
965
} else if (ch == '\n') {
969
position = NextPosition(position, 1);
976
void Document::Indent(bool forwards, int lineBottom, int lineTop) {
977
// Dedent - suck white space off the front of the line to dedent by equivalent of a tab
978
for (int line = lineBottom; line >= lineTop; line--) {
979
int indentOfLine = GetLineIndentation(line);
981
if (LineStart(line) < LineEnd(line)) {
982
SetLineIndentation(line, indentOfLine + IndentSize());
985
SetLineIndentation(line, indentOfLine - IndentSize());
990
// Convert line endings for a piece of text to a particular mode.
991
// Stop at len or when a NUL is found.
992
// Caller must delete the returned pointer.
993
char *Document::TransformLineEnds(int *pLenOut, const char *s, size_t len, int eolMode) {
994
char *dest = new char[2 * len + 1];
995
const char *sptr = s;
997
for (size_t i = 0; (i < len) && (*sptr != '\0'); i++) {
998
if (*sptr == '\n' || *sptr == '\r') {
999
if (eolMode == SC_EOL_CR) {
1001
} else if (eolMode == SC_EOL_LF) {
1003
} else { // eolMode == SC_EOL_CRLF
1007
if ((*sptr == '\r') && (i+1 < len) && (*(sptr+1) == '\n')) {
1017
*pLenOut = (dptr - dest) - 1;
1021
void Document::ConvertLineEnds(int eolModeSet) {
1024
for (int pos = 0; pos < Length(); pos++) {
1025
if (cb.CharAt(pos) == '\r') {
1026
if (cb.CharAt(pos + 1) == '\n') {
1028
if (eolModeSet == SC_EOL_CR) {
1029
DeleteChars(pos + 1, 1); // Delete the LF
1030
} else if (eolModeSet == SC_EOL_LF) {
1031
DeleteChars(pos, 1); // Delete the CR
1037
if (eolModeSet == SC_EOL_CRLF) {
1038
InsertString(pos + 1, "\n", 1); // Insert LF
1040
} else if (eolModeSet == SC_EOL_LF) {
1041
InsertString(pos, "\n", 1); // Insert LF
1042
DeleteChars(pos + 1, 1); // Delete CR
1045
} else if (cb.CharAt(pos) == '\n') {
1047
if (eolModeSet == SC_EOL_CRLF) {
1048
InsertString(pos, "\r", 1); // Insert CR
1050
} else if (eolModeSet == SC_EOL_CR) {
1051
InsertString(pos, "\r", 1); // Insert CR
1052
DeleteChars(pos + 1, 1); // Delete LF
1059
bool Document::IsWhiteLine(int line) const {
1060
int currentChar = LineStart(line);
1061
int endLine = LineEnd(line);
1062
while (currentChar < endLine) {
1063
if (cb.CharAt(currentChar) != ' ' && cb.CharAt(currentChar) != '\t') {
1071
int Document::ParaUp(int pos) {
1072
int line = LineFromPosition(pos);
1074
while (line >= 0 && IsWhiteLine(line)) { // skip empty lines
1077
while (line >= 0 && !IsWhiteLine(line)) { // skip non-empty lines
1081
return LineStart(line);
1084
int Document::ParaDown(int pos) {
1085
int line = LineFromPosition(pos);
1086
while (line < LinesTotal() && !IsWhiteLine(line)) { // skip non-empty lines
1089
while (line < LinesTotal() && IsWhiteLine(line)) { // skip empty lines
1092
if (line < LinesTotal())
1093
return LineStart(line);
1094
else // end of a document
1095
return LineEnd(line-1);
1098
CharClassify::cc Document::WordCharClass(unsigned char ch) {
1099
if ((SC_CP_UTF8 == dbcsCodePage) && (ch >= 0x80))
1100
return CharClassify::ccWord;
1101
return charClass.GetClass(ch);
1105
* Used by commmands that want to select whole words.
1106
* Finds the start of word at pos when delta < 0 or the end of the word when delta >= 0.
1108
int Document::ExtendWordSelect(int pos, int delta, bool onlyWordCharacters) {
1109
CharClassify::cc ccStart = CharClassify::ccWord;
1111
if (!onlyWordCharacters)
1112
ccStart = WordCharClass(cb.CharAt(pos-1));
1113
while (pos > 0 && (WordCharClass(cb.CharAt(pos - 1)) == ccStart))
1116
if (!onlyWordCharacters && pos < Length())
1117
ccStart = WordCharClass(cb.CharAt(pos));
1118
while (pos < (Length()) && (WordCharClass(cb.CharAt(pos)) == ccStart))
1121
return MovePositionOutsideChar(pos, delta, true);
1125
* Find the start of the next word in either a forward (delta >= 0) or backwards direction
1127
* This is looking for a transition between character classes although there is also some
1128
* additional movement to transit white space.
1129
* Used by cursor movement by word commands.
1131
int Document::NextWordStart(int pos, int delta) {
1133
while (pos > 0 && (WordCharClass(cb.CharAt(pos - 1)) == CharClassify::ccSpace))
1136
CharClassify::cc ccStart = WordCharClass(cb.CharAt(pos-1));
1137
while (pos > 0 && (WordCharClass(cb.CharAt(pos - 1)) == ccStart)) {
1142
CharClassify::cc ccStart = WordCharClass(cb.CharAt(pos));
1143
while (pos < (Length()) && (WordCharClass(cb.CharAt(pos)) == ccStart))
1145
while (pos < (Length()) && (WordCharClass(cb.CharAt(pos)) == CharClassify::ccSpace))
1152
* Find the end of the next word in either a forward (delta >= 0) or backwards direction
1154
* This is looking for a transition between character classes although there is also some
1155
* additional movement to transit white space.
1156
* Used by cursor movement by word commands.
1158
int Document::NextWordEnd(int pos, int delta) {
1161
CharClassify::cc ccStart = WordCharClass(cb.CharAt(pos-1));
1162
if (ccStart != CharClassify::ccSpace) {
1163
while (pos > 0 && WordCharClass(cb.CharAt(pos - 1)) == ccStart) {
1167
while (pos > 0 && WordCharClass(cb.CharAt(pos - 1)) == CharClassify::ccSpace) {
1172
while (pos < Length() && WordCharClass(cb.CharAt(pos)) == CharClassify::ccSpace) {
1175
if (pos < Length()) {
1176
CharClassify::cc ccStart = WordCharClass(cb.CharAt(pos));
1177
while (pos < Length() && WordCharClass(cb.CharAt(pos)) == ccStart) {
1186
* Check that the character at the given position is a word or punctuation character and that
1187
* the previous character is of a different character class.
1189
bool Document::IsWordStartAt(int pos) {
1191
CharClassify::cc ccPos = WordCharClass(CharAt(pos));
1192
return (ccPos == CharClassify::ccWord || ccPos == CharClassify::ccPunctuation) &&
1193
(ccPos != WordCharClass(CharAt(pos - 1)));
1199
* Check that the character at the given position is a word or punctuation character and that
1200
* the next character is of a different character class.
1202
bool Document::IsWordEndAt(int pos) {
1203
if (pos < Length()) {
1204
CharClassify::cc ccPrev = WordCharClass(CharAt(pos-1));
1205
return (ccPrev == CharClassify::ccWord || ccPrev == CharClassify::ccPunctuation) &&
1206
(ccPrev != WordCharClass(CharAt(pos)));
1212
* Check that the given range is has transitions between character classes at both
1213
* ends and where the characters on the inside are word or punctuation characters.
1215
bool Document::IsWordAt(int start, int end) {
1216
return IsWordStartAt(start) && IsWordEndAt(end);
1219
static inline char MakeLowerCase(char ch) {
1220
if (ch < 'A' || ch > 'Z')
1223
return static_cast<char>(ch - 'A' + 'a');
1226
static bool GoodTrailByte(int v) {
1227
return (v >= 0x80) && (v < 0xc0);
1230
size_t Document::ExtractChar(int pos, char *bytes) {
1231
unsigned char ch = static_cast<unsigned char>(cb.CharAt(pos));
1232
size_t widthChar = UTF8CharLength(ch);
1234
for (size_t i=1; i<widthChar; i++) {
1235
bytes[i] = cb.CharAt(pos+i);
1236
if (!GoodTrailByte(static_cast<unsigned char>(bytes[i]))) { // Bad byte
1243
CaseFolderTable::CaseFolderTable() {
1244
for (size_t iChar=0; iChar<sizeof(mapping); iChar++) {
1245
mapping[iChar] = static_cast<char>(iChar);
1249
CaseFolderTable::~CaseFolderTable() {
1252
size_t CaseFolderTable::Fold(char *folded, size_t sizeFolded, const char *mixed, size_t lenMixed) {
1253
if (lenMixed > sizeFolded) {
1256
for (size_t i=0; i<lenMixed; i++) {
1257
folded[i] = mapping[static_cast<unsigned char>(mixed[i])];
1263
void CaseFolderTable::SetTranslation(char ch, char chTranslation) {
1264
mapping[static_cast<unsigned char>(ch)] = chTranslation;
1267
void CaseFolderTable::StandardASCII() {
1268
for (size_t iChar=0; iChar<sizeof(mapping); iChar++) {
1269
if (iChar >= 'A' && iChar <= 'Z') {
1270
mapping[iChar] = static_cast<char>(iChar - 'A' + 'a');
1272
mapping[iChar] = static_cast<char>(iChar);
1277
bool Document::MatchesWordOptions(bool word, bool wordStart, int pos, int length) {
1278
return (!word && !wordStart) ||
1279
(word && IsWordAt(pos, pos + length)) ||
1280
(wordStart && IsWordStartAt(pos));
1284
* Find text in document, supporting both forward and backward
1285
* searches (just pass minPos > maxPos to do a backward search)
1286
* Has not been tested with backwards DBCS searches yet.
1288
long Document::FindText(int minPos, int maxPos, const char *search,
1289
bool caseSensitive, bool word, bool wordStart, bool regExp, int flags,
1290
int *length, CaseFolder *pcf) {
1295
regex = CreateRegexSearch(&charClass);
1296
return regex->FindText(this, minPos, maxPos, search, caseSensitive, word, wordStart, flags, length);
1299
const bool forward = minPos <= maxPos;
1300
const int increment = forward ? 1 : -1;
1302
// Range endpoints should not be inside DBCS characters, but just in case, move them.
1303
const int startPos = MovePositionOutsideChar(minPos, increment, false);
1304
const int endPos = MovePositionOutsideChar(maxPos, increment, false);
1306
// Compute actual search ranges needed
1307
const int lengthFind = (*length == -1) ? static_cast<int>(strlen(search)) : *length;
1308
const int endSearch = (startPos <= endPos) ? endPos - lengthFind + 1 : endPos;
1310
//Platform::DebugPrintf("Find %d %d %s %d\n", startPos, endPos, ft->lpstrText, lengthFind);
1311
const int limitPos = Platform::Maximum(startPos, endPos);
1314
// Back all of a character
1315
pos = NextPosition(pos, increment);
1317
if (caseSensitive) {
1318
while (forward ? (pos < endSearch) : (pos >= endSearch)) {
1319
bool found = (pos + lengthFind) <= limitPos;
1320
for (int indexSearch = 0; (indexSearch < lengthFind) && found; indexSearch++) {
1321
found = CharAt(pos + indexSearch) == search[indexSearch];
1323
if (found && MatchesWordOptions(word, wordStart, pos, lengthFind)) {
1326
if (!NextCharacter(pos, increment))
1329
} else if (SC_CP_UTF8 == dbcsCodePage) {
1330
const size_t maxBytesCharacter = 4;
1331
const size_t maxFoldingExpansion = 4;
1332
std::vector<char> searchThing(lengthFind * maxBytesCharacter * maxFoldingExpansion + 1);
1333
const int lenSearch = pcf->Fold(&searchThing[0], searchThing.size(), search, lengthFind);
1334
while (forward ? (pos < endSearch) : (pos >= endSearch)) {
1335
int widthFirstCharacter = 0;
1336
int indexDocument = 0;
1337
int indexSearch = 0;
1338
bool characterMatches = true;
1339
while (characterMatches &&
1340
((pos + indexDocument) < limitPos) &&
1341
(indexSearch < lenSearch)) {
1342
char bytes[maxBytesCharacter + 1];
1343
bytes[maxBytesCharacter] = 0;
1344
const int widthChar = ExtractChar(pos + indexDocument, bytes);
1345
if (!widthFirstCharacter)
1346
widthFirstCharacter = widthChar;
1347
char folded[maxBytesCharacter * maxFoldingExpansion + 1];
1348
const int lenFlat = pcf->Fold(folded, sizeof(folded), bytes, widthChar);
1349
folded[lenFlat] = 0;
1350
// Does folded match the buffer
1351
characterMatches = 0 == memcmp(folded, &searchThing[0] + indexSearch, lenFlat);
1352
indexDocument += widthChar;
1353
indexSearch += lenFlat;
1355
if (characterMatches && (indexSearch == static_cast<int>(lenSearch))) {
1356
if (MatchesWordOptions(word, wordStart, pos, indexDocument)) {
1357
*length = indexDocument;
1362
pos += widthFirstCharacter;
1364
if (!NextCharacter(pos, increment))
1368
} else if (dbcsCodePage) {
1369
const size_t maxBytesCharacter = 2;
1370
const size_t maxFoldingExpansion = 4;
1371
std::vector<char> searchThing(lengthFind * maxBytesCharacter * maxFoldingExpansion + 1);
1372
const int lenSearch = pcf->Fold(&searchThing[0], searchThing.size(), search, lengthFind);
1373
while (forward ? (pos < endSearch) : (pos >= endSearch)) {
1374
int indexDocument = 0;
1375
int indexSearch = 0;
1376
bool characterMatches = true;
1377
while (characterMatches &&
1378
((pos + indexDocument) < limitPos) &&
1379
(indexSearch < lenSearch)) {
1380
char bytes[maxBytesCharacter + 1];
1381
bytes[0] = cb.CharAt(pos + indexDocument);
1382
const int widthChar = IsDBCSLeadByte(bytes[0]) ? 2 : 1;
1384
bytes[1] = cb.CharAt(pos + indexDocument + 1);
1385
char folded[maxBytesCharacter * maxFoldingExpansion + 1];
1386
const int lenFlat = pcf->Fold(folded, sizeof(folded), bytes, widthChar);
1387
folded[lenFlat] = 0;
1388
// Does folded match the buffer
1389
characterMatches = 0 == memcmp(folded, &searchThing[0] + indexSearch, lenFlat);
1390
indexDocument += widthChar;
1391
indexSearch += lenFlat;
1393
if (characterMatches && (indexSearch == static_cast<int>(lenSearch))) {
1394
if (MatchesWordOptions(word, wordStart, pos, indexDocument)) {
1395
*length = indexDocument;
1399
if (!NextCharacter(pos, increment))
1403
CaseFolderTable caseFolder;
1404
std::vector<char> searchThing(lengthFind + 1);
1405
pcf->Fold(&searchThing[0], searchThing.size(), search, lengthFind);
1406
while (forward ? (pos < endSearch) : (pos >= endSearch)) {
1407
bool found = (pos + lengthFind) <= limitPos;
1408
for (int indexSearch = 0; (indexSearch < lengthFind) && found; indexSearch++) {
1409
char ch = CharAt(pos + indexSearch);
1411
pcf->Fold(folded, sizeof(folded), &ch, 1);
1412
found = folded[0] == searchThing[indexSearch];
1414
if (found && MatchesWordOptions(word, wordStart, pos, lengthFind)) {
1417
if (!NextCharacter(pos, increment))
1422
//Platform::DebugPrintf("Not found\n");
1426
const char *Document::SubstituteByPosition(const char *text, int *length) {
1428
return regex->SubstituteByPosition(this, text, length);
1433
int Document::LinesTotal() const {
1437
void Document::ChangeCase(Range r, bool makeUpperCase) {
1438
for (int pos = r.start; pos < r.end;) {
1439
int len = LenChar(pos);
1441
char ch = CharAt(pos);
1442
if (makeUpperCase) {
1443
if (IsLowerCase(ch)) {
1444
ChangeChar(pos, static_cast<char>(MakeUpperCase(ch)));
1447
if (IsUpperCase(ch)) {
1448
ChangeChar(pos, static_cast<char>(MakeLowerCase(ch)));
1456
void Document::SetDefaultCharClasses(bool includeWordClass) {
1457
charClass.SetDefaultCharClasses(includeWordClass);
1460
void Document::SetCharClasses(const unsigned char *chars, CharClassify::cc newCharClass) {
1461
charClass.SetCharClasses(chars, newCharClass);
1464
void Document::SetStylingBits(int bits) {
1466
stylingBitsMask = (1 << stylingBits) - 1;
1469
void SCI_METHOD Document::StartStyling(int position, char mask) {
1471
endStyled = position;
1474
bool SCI_METHOD Document::SetStyleFor(int length, char style) {
1475
if (enteredStyling != 0) {
1479
style &= stylingMask;
1480
int prevEndStyled = endStyled;
1481
if (cb.SetStyleFor(endStyled, length, style, stylingMask)) {
1482
DocModification mh(SC_MOD_CHANGESTYLE | SC_PERFORMED_USER,
1483
prevEndStyled, length);
1486
endStyled += length;
1492
bool SCI_METHOD Document::SetStyles(int length, const char *styles) {
1493
if (enteredStyling != 0) {
1497
bool didChange = false;
1500
for (int iPos = 0; iPos < length; iPos++, endStyled++) {
1501
PLATFORM_ASSERT(endStyled < Length());
1502
if (cb.SetStyleAt(endStyled, styles[iPos], stylingMask)) {
1504
startMod = endStyled;
1511
DocModification mh(SC_MOD_CHANGESTYLE | SC_PERFORMED_USER,
1512
startMod, endMod - startMod + 1);
1520
void Document::EnsureStyledTo(int pos) {
1521
if ((enteredStyling == 0) && (pos > GetEndStyled())) {
1522
IncrementStyleClock();
1523
if (pli && !pli->UseContainerLexing()) {
1524
int lineEndStyled = LineFromPosition(GetEndStyled());
1525
int endStyledTo = LineStart(lineEndStyled);
1526
pli->Colourise(endStyledTo, pos);
1528
// Ask the watchers to style, and stop as soon as one responds.
1529
for (int i = 0; pos > GetEndStyled() && i < lenWatchers; i++) {
1530
watchers[i].watcher->NotifyStyleNeeded(this, watchers[i].userData, pos);
1536
void Document::LexerChanged() {
1537
// Tell the watchers the lexer has changed.
1538
for (int i = 0; i < lenWatchers; i++) {
1539
watchers[i].watcher->NotifyLexerChanged(this, watchers[i].userData);
1543
int SCI_METHOD Document::SetLineState(int line, int state) {
1544
int statePrevious = static_cast<LineState *>(perLineData[ldState])->SetLineState(line, state);
1545
if (state != statePrevious) {
1546
DocModification mh(SC_MOD_CHANGELINESTATE, LineStart(line), 0, 0, 0, line);
1549
return statePrevious;
1552
int SCI_METHOD Document::GetLineState(int line) const {
1553
return static_cast<LineState *>(perLineData[ldState])->GetLineState(line);
1556
int Document::GetMaxLineState() {
1557
return static_cast<LineState *>(perLineData[ldState])->GetMaxLineState();
1560
void SCI_METHOD Document::ChangeLexerState(int start, int end) {
1561
DocModification mh(SC_MOD_LEXERSTATE, start, end-start, 0, 0, 0);
1565
StyledText Document::MarginStyledText(int line) {
1566
LineAnnotation *pla = static_cast<LineAnnotation *>(perLineData[ldMargin]);
1567
return StyledText(pla->Length(line), pla->Text(line),
1568
pla->MultipleStyles(line), pla->Style(line), pla->Styles(line));
1571
void Document::MarginSetText(int line, const char *text) {
1572
static_cast<LineAnnotation *>(perLineData[ldMargin])->SetText(line, text);
1573
DocModification mh(SC_MOD_CHANGEMARGIN, LineStart(line), 0, 0, 0, line);
1577
void Document::MarginSetStyle(int line, int style) {
1578
static_cast<LineAnnotation *>(perLineData[ldMargin])->SetStyle(line, style);
1581
void Document::MarginSetStyles(int line, const unsigned char *styles) {
1582
static_cast<LineAnnotation *>(perLineData[ldMargin])->SetStyles(line, styles);
1585
int Document::MarginLength(int line) const {
1586
return static_cast<LineAnnotation *>(perLineData[ldMargin])->Length(line);
1589
void Document::MarginClearAll() {
1590
int maxEditorLine = LinesTotal();
1591
for (int l=0; l<maxEditorLine; l++)
1592
MarginSetText(l, 0);
1593
// Free remaining data
1594
static_cast<LineAnnotation *>(perLineData[ldMargin])->ClearAll();
1597
bool Document::AnnotationAny() const {
1598
return static_cast<LineAnnotation *>(perLineData[ldAnnotation])->AnySet();
1601
StyledText Document::AnnotationStyledText(int line) {
1602
LineAnnotation *pla = static_cast<LineAnnotation *>(perLineData[ldAnnotation]);
1603
return StyledText(pla->Length(line), pla->Text(line),
1604
pla->MultipleStyles(line), pla->Style(line), pla->Styles(line));
1607
void Document::AnnotationSetText(int line, const char *text) {
1608
const int linesBefore = AnnotationLines(line);
1609
static_cast<LineAnnotation *>(perLineData[ldAnnotation])->SetText(line, text);
1610
const int linesAfter = AnnotationLines(line);
1611
DocModification mh(SC_MOD_CHANGEANNOTATION, LineStart(line), 0, 0, 0, line);
1612
mh.annotationLinesAdded = linesAfter - linesBefore;
1616
void Document::AnnotationSetStyle(int line, int style) {
1617
static_cast<LineAnnotation *>(perLineData[ldAnnotation])->SetStyle(line, style);
1618
DocModification mh(SC_MOD_CHANGEANNOTATION, LineStart(line), 0, 0, 0, line);
1622
void Document::AnnotationSetStyles(int line, const unsigned char *styles) {
1623
static_cast<LineAnnotation *>(perLineData[ldAnnotation])->SetStyles(line, styles);
1626
int Document::AnnotationLength(int line) const {
1627
return static_cast<LineAnnotation *>(perLineData[ldAnnotation])->Length(line);
1630
int Document::AnnotationLines(int line) const {
1631
return static_cast<LineAnnotation *>(perLineData[ldAnnotation])->Lines(line);
1634
void Document::AnnotationClearAll() {
1635
int maxEditorLine = LinesTotal();
1636
for (int l=0; l<maxEditorLine; l++)
1637
AnnotationSetText(l, 0);
1638
// Free remaining data
1639
static_cast<LineAnnotation *>(perLineData[ldAnnotation])->ClearAll();
1642
void Document::IncrementStyleClock() {
1643
styleClock = (styleClock + 1) % 0x100000;
1646
void SCI_METHOD Document::DecorationFillRange(int position, int value, int fillLength) {
1647
if (decorations.FillRange(position, value, fillLength)) {
1648
DocModification mh(SC_MOD_CHANGEINDICATOR | SC_PERFORMED_USER,
1649
position, fillLength);
1654
bool Document::AddWatcher(DocWatcher *watcher, void *userData) {
1655
for (int i = 0; i < lenWatchers; i++) {
1656
if ((watchers[i].watcher == watcher) &&
1657
(watchers[i].userData == userData))
1660
WatcherWithUserData *pwNew = new WatcherWithUserData[lenWatchers + 1];
1661
for (int j = 0; j < lenWatchers; j++)
1662
pwNew[j] = watchers[j];
1663
pwNew[lenWatchers].watcher = watcher;
1664
pwNew[lenWatchers].userData = userData;
1671
bool Document::RemoveWatcher(DocWatcher *watcher, void *userData) {
1672
for (int i = 0; i < lenWatchers; i++) {
1673
if ((watchers[i].watcher == watcher) &&
1674
(watchers[i].userData == userData)) {
1675
if (lenWatchers == 1) {
1680
WatcherWithUserData *pwNew = new WatcherWithUserData[lenWatchers];
1681
for (int j = 0; j < lenWatchers - 1; j++) {
1682
pwNew[j] = (j < i) ? watchers[j] : watchers[j + 1];
1694
void Document::NotifyModifyAttempt() {
1695
for (int i = 0; i < lenWatchers; i++) {
1696
watchers[i].watcher->NotifyModifyAttempt(this, watchers[i].userData);
1700
void Document::NotifySavePoint(bool atSavePoint) {
1701
for (int i = 0; i < lenWatchers; i++) {
1702
watchers[i].watcher->NotifySavePoint(this, watchers[i].userData, atSavePoint);
1706
void Document::NotifyModified(DocModification mh) {
1707
if (mh.modificationType & SC_MOD_INSERTTEXT) {
1708
decorations.InsertSpace(mh.position, mh.length);
1709
} else if (mh.modificationType & SC_MOD_DELETETEXT) {
1710
decorations.DeleteRange(mh.position, mh.length);
1712
for (int i = 0; i < lenWatchers; i++) {
1713
watchers[i].watcher->NotifyModified(this, mh, watchers[i].userData);
1717
bool Document::IsWordPartSeparator(char ch) {
1718
return (WordCharClass(ch) == CharClassify::ccWord) && IsPunctuation(ch);
1721
int Document::WordPartLeft(int pos) {
1724
char startChar = cb.CharAt(pos);
1725
if (IsWordPartSeparator(startChar)) {
1726
while (pos > 0 && IsWordPartSeparator(cb.CharAt(pos))) {
1731
startChar = cb.CharAt(pos);
1733
if (IsLowerCase(startChar)) {
1734
while (pos > 0 && IsLowerCase(cb.CharAt(pos)))
1736
if (!IsUpperCase(cb.CharAt(pos)) && !IsLowerCase(cb.CharAt(pos)))
1738
} else if (IsUpperCase(startChar)) {
1739
while (pos > 0 && IsUpperCase(cb.CharAt(pos)))
1741
if (!IsUpperCase(cb.CharAt(pos)))
1743
} else if (IsADigit(startChar)) {
1744
while (pos > 0 && IsADigit(cb.CharAt(pos)))
1746
if (!IsADigit(cb.CharAt(pos)))
1748
} else if (IsPunctuation(startChar)) {
1749
while (pos > 0 && IsPunctuation(cb.CharAt(pos)))
1751
if (!IsPunctuation(cb.CharAt(pos)))
1753
} else if (isspacechar(startChar)) {
1754
while (pos > 0 && isspacechar(cb.CharAt(pos)))
1756
if (!isspacechar(cb.CharAt(pos)))
1758
} else if (!isascii(startChar)) {
1759
while (pos > 0 && !isascii(cb.CharAt(pos)))
1761
if (isascii(cb.CharAt(pos)))
1771
int Document::WordPartRight(int pos) {
1772
char startChar = cb.CharAt(pos);
1773
int length = Length();
1774
if (IsWordPartSeparator(startChar)) {
1775
while (pos < length && IsWordPartSeparator(cb.CharAt(pos)))
1777
startChar = cb.CharAt(pos);
1779
if (!isascii(startChar)) {
1780
while (pos < length && !isascii(cb.CharAt(pos)))
1782
} else if (IsLowerCase(startChar)) {
1783
while (pos < length && IsLowerCase(cb.CharAt(pos)))
1785
} else if (IsUpperCase(startChar)) {
1786
if (IsLowerCase(cb.CharAt(pos + 1))) {
1788
while (pos < length && IsLowerCase(cb.CharAt(pos)))
1791
while (pos < length && IsUpperCase(cb.CharAt(pos)))
1794
if (IsLowerCase(cb.CharAt(pos)) && IsUpperCase(cb.CharAt(pos - 1)))
1796
} else if (IsADigit(startChar)) {
1797
while (pos < length && IsADigit(cb.CharAt(pos)))
1799
} else if (IsPunctuation(startChar)) {
1800
while (pos < length && IsPunctuation(cb.CharAt(pos)))
1802
} else if (isspacechar(startChar)) {
1803
while (pos < length && isspacechar(cb.CharAt(pos)))
1811
bool IsLineEndChar(char c) {
1812
return (c == '\n' || c == '\r');
1815
int Document::ExtendStyleRange(int pos, int delta, bool singleLine) {
1816
int sStart = cb.StyleAt(pos);
1818
while (pos > 0 && (cb.StyleAt(pos) == sStart) && (!singleLine || !IsLineEndChar(cb.CharAt(pos))))
1822
while (pos < (Length()) && (cb.StyleAt(pos) == sStart) && (!singleLine || !IsLineEndChar(cb.CharAt(pos))))
1828
static char BraceOpposite(char ch) {
1851
// TODO: should be able to extend styled region to find matching brace
1852
int Document::BraceMatch(int position, int /*maxReStyle*/) {
1853
char chBrace = CharAt(position);
1854
char chSeek = BraceOpposite(chBrace);
1857
char styBrace = static_cast<char>(StyleAt(position) & stylingBitsMask);
1859
if (chBrace == '(' || chBrace == '[' || chBrace == '{' || chBrace == '<')
1862
position = NextPosition(position, direction);
1863
while ((position >= 0) && (position < Length())) {
1864
char chAtPos = CharAt(position);
1865
char styAtPos = static_cast<char>(StyleAt(position) & stylingBitsMask);
1866
if ((position > GetEndStyled()) || (styAtPos == styBrace)) {
1867
if (chAtPos == chBrace)
1869
if (chAtPos == chSeek)
1874
int positionBeforeMove = position;
1875
position = NextPosition(position, direction);
1876
if (position == positionBeforeMove)
1883
* Implementation of RegexSearchBase for the default built-in regular expression engine
1885
class BuiltinRegex : public RegexSearchBase {
1887
BuiltinRegex(CharClassify *charClassTable) : search(charClassTable), substituted(NULL) {}
1889
virtual ~BuiltinRegex() {
1893
virtual long FindText(Document *doc, int minPos, int maxPos, const char *s,
1894
bool caseSensitive, bool word, bool wordStart, int flags,
1897
virtual const char *SubstituteByPosition(Document *doc, const char *text, int *length);
1904
// Define a way for the Regular Expression code to access the document
1905
class DocumentIndexer : public CharacterIndexer {
1909
DocumentIndexer(Document *pdoc_, int end_) :
1910
pdoc(pdoc_), end(end_) {
1913
virtual ~DocumentIndexer() {
1916
virtual char CharAt(int index) {
1917
if (index < 0 || index >= end)
1920
return pdoc->CharAt(index);
1924
long BuiltinRegex::FindText(Document *doc, int minPos, int maxPos, const char *s,
1925
bool caseSensitive, bool, bool, int flags,
1927
bool posix = (flags & SCFIND_POSIX) != 0;
1928
int increment = (minPos <= maxPos) ? 1 : -1;
1930
int startPos = minPos;
1931
int endPos = maxPos;
1933
// Range endpoints should not be inside DBCS characters, but just in case, move them.
1934
startPos = doc->MovePositionOutsideChar(startPos, 1, false);
1935
endPos = doc->MovePositionOutsideChar(endPos, 1, false);
1937
const char *errmsg = search.Compile(s, *length, caseSensitive, posix);
1941
// Find a variable in a property file: \$(\([A-Za-z0-9_.]+\))
1942
// Replace first '.' with '-' in each property file variable reference:
1943
// Search: \$(\([A-Za-z0-9_-]+\)\.\([A-Za-z0-9_.]+\))
1944
// Replace: $(\1-\2)
1945
int lineRangeStart = doc->LineFromPosition(startPos);
1946
int lineRangeEnd = doc->LineFromPosition(endPos);
1947
if ((increment == 1) &&
1948
(startPos >= doc->LineEnd(lineRangeStart)) &&
1949
(lineRangeStart < lineRangeEnd)) {
1950
// the start position is at end of line or between line end characters.
1952
startPos = doc->LineStart(lineRangeStart);
1956
char searchEnd = s[*length - 1];
1957
int lineRangeBreak = lineRangeEnd + increment;
1958
for (int line = lineRangeStart; line != lineRangeBreak; line += increment) {
1959
int startOfLine = doc->LineStart(line);
1960
int endOfLine = doc->LineEnd(line);
1961
if (increment == 1) {
1962
if (line == lineRangeStart) {
1963
if ((startPos != startOfLine) && (s[0] == '^'))
1964
continue; // Can't match start of line if start position after start of line
1965
startOfLine = startPos;
1967
if (line == lineRangeEnd) {
1968
if ((endPos != endOfLine) && (searchEnd == '$'))
1969
continue; // Can't match end of line if end position before end of line
1973
if (line == lineRangeEnd) {
1974
if ((endPos != startOfLine) && (s[0] == '^'))
1975
continue; // Can't match start of line if end position after start of line
1976
startOfLine = endPos;
1978
if (line == lineRangeStart) {
1979
if ((startPos != endOfLine) && (searchEnd == '$'))
1980
continue; // Can't match end of line if start position before end of line
1981
endOfLine = startPos;
1985
DocumentIndexer di(doc, endOfLine);
1986
int success = search.Execute(di, startOfLine, endOfLine);
1988
pos = search.bopat[0];
1989
lenRet = search.eopat[0] - search.bopat[0];
1990
if (increment == -1) {
1991
// Check for the last match on this line.
1992
int repetitions = 1000; // Break out of infinite loop
1993
while (success && (search.eopat[0] <= endOfLine) && (repetitions--)) {
1994
success = search.Execute(di, pos+1, endOfLine);
1996
if (search.eopat[0] <= minPos) {
1997
pos = search.bopat[0];
1998
lenRet = search.eopat[0] - search.bopat[0];
2012
const char *BuiltinRegex::SubstituteByPosition(Document *doc, const char *text, int *length) {
2013
delete []substituted;
2015
DocumentIndexer di(doc, doc->Length());
2016
if (!search.GrabMatches(di))
2018
unsigned int lenResult = 0;
2019
for (int i = 0; i < *length; i++) {
2020
if (text[i] == '\\') {
2021
if (text[i + 1] >= '1' && text[i + 1] <= '9') {
2022
unsigned int patNum = text[i + 1] - '0';
2023
lenResult += search.eopat[patNum] - search.bopat[patNum];
2026
switch (text[i + 1]) {
2043
substituted = new char[lenResult + 1];
2044
char *o = substituted;
2045
for (int j = 0; j < *length; j++) {
2046
if (text[j] == '\\') {
2047
if (text[j + 1] >= '1' && text[j + 1] <= '9') {
2048
unsigned int patNum = text[j + 1] - '0';
2049
unsigned int len = search.eopat[patNum] - search.bopat[patNum];
2050
if (search.pat[patNum]) // Will be null if try for a match that did not occur
2051
memcpy(o, search.pat[patNum], len);
2091
*length = lenResult;
2095
#ifndef SCI_OWNREGEX
2097
#ifdef SCI_NAMESPACE
2099
RegexSearchBase *Scintilla::CreateRegexSearch(CharClassify *charClassTable) {
2100
return new BuiltinRegex(charClassTable);
2105
RegexSearchBase *CreateRegexSearch(CharClassify *charClassTable) {
2106
return new BuiltinRegex(charClassTable);