1 // Scintilla source code edit control
3 ** Text document that handles notifications, DBCS, styling, words and end of line.
5 // Copyright 1998-2002 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
15 #include "Scintilla.h"
17 #include "CellBuffer.h"
21 // This is ASCII specific but is safe with chars >= 0x80
22 static inline bool isspacechar(unsigned char ch
) {
23 return (ch
== ' ') || ((ch
>= 0x09) && (ch
<= 0x0d));
26 Document::Document() {
31 eolMode
= SC_EOL_CRLF
;
35 stylingBitsMask
= 0x1F;
41 enteredReadOnlyCount
= 0;
46 backspaceUnindents
= false;
55 Document::~Document() {
56 for (int i
= 0; i
< lenWatchers
; i
++) {
57 watchers
[i
].watcher
->NotifyDeleted(this, watchers
[i
].userData
);
68 // Increase reference count and return its previous value.
69 int Document::AddRef() {
73 // Decrease reference count and return its previous value.
74 // Delete the document if reference count reaches zero.
75 int Document::Release() {
76 int curRefCount
= --refCount
;
82 void Document::SetSavePoint() {
84 NotifySavePoint(true);
87 int Document::AddMark(int line
, int markerNum
) {
88 int prev
= cb
.AddMark(line
, markerNum
);
89 DocModification
mh(SC_MOD_CHANGEMARKER
, LineStart(line
), 0, 0, 0);
94 void Document::DeleteMark(int line
, int markerNum
) {
95 cb
.DeleteMark(line
, markerNum
);
96 DocModification
mh(SC_MOD_CHANGEMARKER
, LineStart(line
), 0, 0, 0);
100 void Document::DeleteMarkFromHandle(int markerHandle
) {
101 cb
.DeleteMarkFromHandle(markerHandle
);
102 DocModification
mh(SC_MOD_CHANGEMARKER
, 0, 0, 0, 0);
106 void Document::DeleteAllMarks(int markerNum
) {
107 cb
.DeleteAllMarks(markerNum
);
108 DocModification
mh(SC_MOD_CHANGEMARKER
, 0, 0, 0, 0);
112 int Document::LineStart(int line
) {
113 return cb
.LineStart(line
);
116 int Document::LineEnd(int line
) {
117 if (line
== LinesTotal() - 1) {
118 return LineStart(line
+ 1);
120 int position
= LineStart(line
+ 1) - 1;
121 // When line terminator is CR+LF, may need to go back one more
122 if ((position
> LineStart(line
)) && (cb
.CharAt(position
- 1) == '\r')) {
129 int Document::LineFromPosition(int pos
) {
130 return cb
.LineFromPosition(pos
);
133 int Document::LineEndPosition(int position
) {
134 return LineEnd(LineFromPosition(position
));
137 int Document::VCHomePosition(int position
) {
138 int line
= LineFromPosition(position
);
139 int startPosition
= LineStart(line
);
140 int endLine
= LineStart(line
+ 1) - 1;
141 int startText
= startPosition
;
142 while (startText
< endLine
&& (cb
.CharAt(startText
) == ' ' || cb
.CharAt(startText
) == '\t' ) )
144 if (position
== startText
)
145 return startPosition
;
150 int Document::SetLevel(int line
, int level
) {
151 int prev
= cb
.SetLevel(line
, level
);
153 DocModification
mh(SC_MOD_CHANGEFOLD
| SC_MOD_CHANGEMARKER
,
154 LineStart(line
), 0, 0, 0);
156 mh
.foldLevelNow
= level
;
157 mh
.foldLevelPrev
= prev
;
163 static bool IsSubordinate(int levelStart
, int levelTry
) {
164 if (levelTry
& SC_FOLDLEVELWHITEFLAG
)
167 return (levelStart
& SC_FOLDLEVELNUMBERMASK
) < (levelTry
& SC_FOLDLEVELNUMBERMASK
);
170 int Document::GetLastChild(int lineParent
, int level
) {
172 level
= GetLevel(lineParent
) & SC_FOLDLEVELNUMBERMASK
;
173 int maxLine
= LinesTotal();
174 int lineMaxSubord
= lineParent
;
175 while (lineMaxSubord
< maxLine
- 1) {
176 EnsureStyledTo(LineStart(lineMaxSubord
+ 2));
177 if (!IsSubordinate(level
, GetLevel(lineMaxSubord
+ 1)))
181 if (lineMaxSubord
> lineParent
) {
182 if (level
> (GetLevel(lineMaxSubord
+ 1) & SC_FOLDLEVELNUMBERMASK
)) {
183 // Have chewed up some whitespace that belongs to a parent so seek back
184 if (GetLevel(lineMaxSubord
) & SC_FOLDLEVELWHITEFLAG
) {
189 return lineMaxSubord
;
192 int Document::GetFoldParent(int line
) {
193 int level
= GetLevel(line
);
194 int lineLook
= line
- 1;
195 while ((lineLook
> 0) && (
196 (!(GetLevel(lineLook
) & SC_FOLDLEVELHEADERFLAG
)) ||
197 ((GetLevel(lineLook
) & SC_FOLDLEVELNUMBERMASK
) >= level
))
201 if ((GetLevel(lineLook
) & SC_FOLDLEVELHEADERFLAG
) &&
202 ((GetLevel(lineLook
) & SC_FOLDLEVELNUMBERMASK
) < level
)) {
209 int Document::ClampPositionIntoDocument(int pos
) {
210 return Platform::Clamp(pos
, 0, Length());
213 bool Document::IsCrLf(int pos
) {
216 if (pos
>= (Length() - 1))
218 return (cb
.CharAt(pos
) == '\r') && (cb
.CharAt(pos
+ 1) == '\n');
221 bool Document::IsDBCS(int pos
) {
223 if (SC_CP_UTF8
== dbcsCodePage
) {
224 unsigned char ch
= static_cast<unsigned char>(cb
.CharAt(pos
));
227 // Anchor DBCS calculations at start of line because start of line can
228 // not be a DBCS trail byte.
230 while (startLine
> 0 && cb
.CharAt(startLine
) != '\r' && cb
.CharAt(startLine
) != '\n')
232 while (startLine
<= pos
) {
233 if (Platform::IsDBCSLeadByte(dbcsCodePage
, cb
.CharAt(startLine
))) {
235 if (startLine
>= pos
)
245 int Document::LenChar(int pos
) {
248 } else if (SC_CP_UTF8
== dbcsCodePage
) {
249 unsigned char ch
= static_cast<unsigned char>(cb
.CharAt(pos
));
253 if (ch
>= (0x80 + 0x40 + 0x20))
255 int lengthDoc
= Length();
256 if ((pos
+ len
) > lengthDoc
)
257 return lengthDoc
-pos
;
260 } else if (IsDBCS(pos
)) {
267 // Normalise a position so that it is not halfway through a two byte character.
268 // This can occur in two situations -
269 // When lines are terminated with \r\n pairs which should be treated as one character.
270 // When displaying DBCS text such as Japanese.
271 // If moving, move the position in the indicated direction.
272 int Document::MovePositionOutsideChar(int pos
, int moveDir
, bool checkLineEnd
) {
273 //Platform::DebugPrintf("NoCRLF %d %d\n", pos, moveDir);
274 // If out of range, just return value - should be fixed up after
280 // Position 0 and Length() can not be between any two characters
286 // assert pos > 0 && pos < Length()
287 if (checkLineEnd
&& IsCrLf(pos
- 1)) {
294 // Not between CR and LF
297 if (SC_CP_UTF8
== dbcsCodePage
) {
298 unsigned char ch
= static_cast<unsigned char>(cb
.CharAt(pos
));
299 while ((pos
> 0) && (pos
< Length()) && (ch
>= 0x80) && (ch
< (0x80 + 0x40))) {
300 // ch is a trail byte
305 ch
= static_cast<unsigned char>(cb
.CharAt(pos
));
308 // Anchor DBCS calculations at start of line because start of line can
309 // not be a DBCS trail byte.
311 while (startLine
> 0 && cb
.CharAt(startLine
) != '\r' && cb
.CharAt(startLine
) != '\n')
313 bool atLeadByte
= false;
314 while (startLine
< pos
) {
317 else if (Platform::IsDBCSLeadByte(dbcsCodePage
, cb
.CharAt(startLine
)))
326 // Position is between a lead byte and a trail byte
338 void Document::ModifiedAt(int pos
) {
343 // Document only modified by gateways DeleteChars, InsertStyledString, Undo, Redo, and SetStyleAt.
344 // SetStyleAt does not change the persistent state of a document
346 // Unlike Undo, Redo, and InsertStyledString, the pos argument is a cell number not a char number
347 void Document::DeleteChars(int pos
, int len
) {
350 if ((pos
+ len
) > Length())
352 if (cb
.IsReadOnly() && enteredReadOnlyCount
== 0) {
353 enteredReadOnlyCount
++;
354 NotifyModifyAttempt();
355 enteredReadOnlyCount
--;
357 if (enteredCount
== 0) {
359 if (!cb
.IsReadOnly()) {
362 SC_MOD_BEFOREDELETE
| SC_PERFORMED_USER
,
365 int prevLinesTotal
= LinesTotal();
366 bool startSavePoint
= cb
.IsSavePoint();
367 const char *text
= cb
.DeleteChars(pos
* 2, len
* 2);
368 if (startSavePoint
&& cb
.IsCollectingUndo())
369 NotifySavePoint(!startSavePoint
);
370 if ((pos
< Length()) || (pos
== 0))
376 SC_MOD_DELETETEXT
| SC_PERFORMED_USER
,
378 LinesTotal() - prevLinesTotal
, text
));
384 void Document::InsertStyledString(int position
, char *s
, int insertLength
) {
385 if (cb
.IsReadOnly() && enteredReadOnlyCount
== 0) {
386 enteredReadOnlyCount
++;
387 NotifyModifyAttempt();
388 enteredReadOnlyCount
--;
390 if (enteredCount
== 0) {
392 if (!cb
.IsReadOnly()) {
395 SC_MOD_BEFOREINSERT
| SC_PERFORMED_USER
,
396 position
/ 2, insertLength
/ 2,
398 int prevLinesTotal
= LinesTotal();
399 bool startSavePoint
= cb
.IsSavePoint();
400 const char *text
= cb
.InsertString(position
, s
, insertLength
);
401 if (startSavePoint
&& cb
.IsCollectingUndo())
402 NotifySavePoint(!startSavePoint
);
403 ModifiedAt(position
/ 2);
406 SC_MOD_INSERTTEXT
| SC_PERFORMED_USER
,
407 position
/ 2, insertLength
/ 2,
408 LinesTotal() - prevLinesTotal
, text
));
414 int Document::Undo() {
416 if (enteredCount
== 0) {
418 bool startSavePoint
= cb
.IsSavePoint();
419 int steps
= cb
.StartUndo();
420 //Platform::DebugPrintf("Steps=%d\n", steps);
421 for (int step
= 0; step
< steps
; step
++) {
422 int prevLinesTotal
= LinesTotal();
423 const Action
&action
= cb
.GetUndoStep();
424 if (action
.at
== removeAction
) {
425 NotifyModified(DocModification(
426 SC_MOD_BEFOREINSERT
| SC_PERFORMED_UNDO
, action
));
428 NotifyModified(DocModification(
429 SC_MOD_BEFOREDELETE
| SC_PERFORMED_UNDO
, action
));
431 cb
.PerformUndoStep();
432 int cellPosition
= action
.position
/ 2;
433 ModifiedAt(cellPosition
);
434 newPos
= cellPosition
;
436 int modFlags
= SC_PERFORMED_UNDO
;
437 // With undo, an insertion action becomes a deletion notification
438 if (action
.at
== removeAction
) {
439 newPos
+= action
.lenData
;
440 modFlags
|= SC_MOD_INSERTTEXT
;
442 modFlags
|= SC_MOD_DELETETEXT
;
444 if (step
== steps
- 1)
445 modFlags
|= SC_LASTSTEPINUNDOREDO
;
446 NotifyModified(DocModification(modFlags
, cellPosition
, action
.lenData
,
447 LinesTotal() - prevLinesTotal
, action
.data
));
450 bool endSavePoint
= cb
.IsSavePoint();
451 if (startSavePoint
!= endSavePoint
)
452 NotifySavePoint(endSavePoint
);
458 int Document::Redo() {
460 if (enteredCount
== 0) {
462 bool startSavePoint
= cb
.IsSavePoint();
463 int steps
= cb
.StartRedo();
464 for (int step
= 0; step
< steps
; step
++) {
465 int prevLinesTotal
= LinesTotal();
466 const Action
&action
= cb
.GetRedoStep();
467 if (action
.at
== insertAction
) {
468 NotifyModified(DocModification(
469 SC_MOD_BEFOREINSERT
| SC_PERFORMED_REDO
, action
));
471 NotifyModified(DocModification(
472 SC_MOD_BEFOREDELETE
| SC_PERFORMED_REDO
, action
));
474 cb
.PerformRedoStep();
475 ModifiedAt(action
.position
/ 2);
476 newPos
= action
.position
/ 2;
478 int modFlags
= SC_PERFORMED_REDO
;
479 if (action
.at
== insertAction
) {
480 newPos
+= action
.lenData
;
481 modFlags
|= SC_MOD_INSERTTEXT
;
483 modFlags
|= SC_MOD_DELETETEXT
;
485 if (step
== steps
- 1)
486 modFlags
|= SC_LASTSTEPINUNDOREDO
;
488 DocModification(modFlags
, action
.position
/ 2, action
.lenData
,
489 LinesTotal() - prevLinesTotal
, action
.data
));
492 bool endSavePoint
= cb
.IsSavePoint();
493 if (startSavePoint
!= endSavePoint
)
494 NotifySavePoint(endSavePoint
);
500 void Document::InsertChar(int pos
, char ch
) {
504 InsertStyledString(pos
*2, chs
, 2);
507 // Insert a null terminated string
508 void Document::InsertString(int position
, const char *s
) {
509 InsertString(position
, s
, strlen(s
));
512 // Insert a string with a length
513 void Document::InsertString(int position
, const char *s
, int insertLength
) {
514 char *sWithStyle
= new char[insertLength
* 2];
516 for (int i
= 0; i
< insertLength
; i
++) {
517 sWithStyle
[i
*2] = s
[i
];
518 sWithStyle
[i
*2 + 1] = 0;
520 InsertStyledString(position
*2, sWithStyle
, insertLength
*2);
525 void Document::ChangeChar(int pos
, char ch
) {
530 void Document::DelChar(int pos
) {
531 DeleteChars(pos
, LenChar(pos
));
534 int Document::DelCharBack(int pos
) {
537 } else if (IsCrLf(pos
- 2)) {
538 DeleteChars(pos
- 2, 2);
540 } else if (SC_CP_UTF8
== dbcsCodePage
) {
541 int startChar
= MovePositionOutsideChar(pos
- 1, -1, false);
542 DeleteChars(startChar
, pos
- startChar
);
544 } else if (IsDBCS(pos
- 1)) {
545 DeleteChars(pos
- 2, 2);
548 DeleteChars(pos
- 1, 1);
553 static bool isindentchar(char ch
) {
554 return (ch
== ' ') || (ch
== '\t');
557 static int NextTab(int pos
, int tabSize
) {
558 return ((pos
/ tabSize
) + 1) * tabSize
;
561 static void CreateIndentation(char *linebuf
, int length
, int indent
, int tabSize
, bool insertSpaces
) {
562 length
--; // ensure space for \0
564 while ((indent
>= tabSize
) && (length
> 0)) {
570 while ((indent
> 0) && (length
> 0)) {
578 int Document::GetLineIndentation(int line
) {
580 if ((line
>= 0) && (line
< LinesTotal())) {
581 int lineStart
= LineStart(line
);
582 int length
= Length();
583 for (int i
= lineStart
;i
< length
;i
++) {
584 char ch
= cb
.CharAt(i
);
588 indent
= NextTab(indent
, tabInChars
);
596 void Document::SetLineIndentation(int line
, int indent
) {
597 int indentOfLine
= GetLineIndentation(line
);
600 if (indent
!= indentOfLine
) {
602 CreateIndentation(linebuf
, sizeof(linebuf
), indent
, tabInChars
, !useTabs
);
603 int thisLineStart
= LineStart(line
);
604 int indentPos
= GetLineIndentPosition(line
);
605 DeleteChars(thisLineStart
, indentPos
- thisLineStart
);
606 InsertString(thisLineStart
, linebuf
);
610 int Document::GetLineIndentPosition(int line
) {
613 int pos
= LineStart(line
);
614 int length
= Length();
615 while ((pos
< length
) && isindentchar(cb
.CharAt(pos
))) {
621 int Document::GetColumn(int pos
) {
623 int line
= LineFromPosition(pos
);
624 if ((line
>= 0) && (line
< LinesTotal())) {
625 for (int i
= LineStart(line
);i
< pos
;) {
626 char ch
= cb
.CharAt(i
);
628 column
= NextTab(column
, tabInChars
);
630 } else if (ch
== '\r') {
632 } else if (ch
== '\n') {
636 i
= MovePositionOutsideChar(i
+ 1, 1);
643 int Document::FindColumn(int line
, int column
) {
644 int position
= LineStart(line
);
645 int columnCurrent
= 0;
646 if ((line
>= 0) && (line
< LinesTotal())) {
647 while (columnCurrent
< column
) {
648 char ch
= cb
.CharAt(position
);
650 columnCurrent
= NextTab(columnCurrent
, tabInChars
);
652 } else if (ch
== '\r') {
654 } else if (ch
== '\n') {
658 position
= MovePositionOutsideChar(position
+ 1, 1);
665 void Document::Indent(bool forwards
, int lineBottom
, int lineTop
) {
666 // Dedent - suck white space off the front of the line to dedent by equivalent of a tab
667 for (int line
= lineBottom
; line
>= lineTop
; line
--) {
668 int indentOfLine
= GetLineIndentation(line
);
670 SetLineIndentation(line
, indentOfLine
+ IndentSize());
672 SetLineIndentation(line
, indentOfLine
- IndentSize());
676 void Document::ConvertLineEnds(int eolModeSet
) {
678 for (int pos
= 0; pos
< Length(); pos
++) {
679 if (cb
.CharAt(pos
) == '\r') {
680 if (cb
.CharAt(pos
+ 1) == '\n') {
681 if (eolModeSet
!= SC_EOL_CRLF
) {
683 if (eolModeSet
== SC_EOL_CR
)
684 InsertString(pos
, "\r", 1);
686 InsertString(pos
, "\n", 1);
691 if (eolModeSet
!= SC_EOL_CR
) {
693 if (eolModeSet
== SC_EOL_CRLF
) {
694 InsertString(pos
, "\r\n", 2);
697 InsertString(pos
, "\n", 1);
701 } else if (cb
.CharAt(pos
) == '\n') {
702 if (eolModeSet
!= SC_EOL_LF
) {
704 if (eolModeSet
== SC_EOL_CRLF
) {
705 InsertString(pos
, "\r\n", 2);
708 InsertString(pos
, "\r", 1);
716 Document::charClassification
Document::WordCharClass(unsigned char ch
) {
717 if ((SC_CP_UTF8
== dbcsCodePage
) && (ch
>= 0x80))
719 return charClass
[ch
];
723 * Used by commmands that want to select whole words.
724 * Finds the start of word at pos when delta < 0 or the end of the word when delta >= 0.
726 int Document::ExtendWordSelect(int pos
, int delta
, bool onlyWordCharacters
) {
727 charClassification ccStart
= ccWord
;
729 if (!onlyWordCharacters
)
730 ccStart
= WordCharClass(cb
.CharAt(pos
-1));
731 while (pos
> 0 && (WordCharClass(cb
.CharAt(pos
- 1)) == ccStart
))
734 if (!onlyWordCharacters
)
735 ccStart
= WordCharClass(cb
.CharAt(pos
));
736 while (pos
< (Length()) && (WordCharClass(cb
.CharAt(pos
)) == ccStart
))
743 * Find the start of the next word in either a forward (delta >= 0) or backwards direction
745 * This is looking for a transition between character classes although there is also some
746 * additional movement to transit white space.
747 * Used by cursor movement by word commands.
749 int Document::NextWordStart(int pos
, int delta
) {
751 while (pos
> 0 && (WordCharClass(cb
.CharAt(pos
- 1)) == ccSpace
))
754 charClassification ccStart
= WordCharClass(cb
.CharAt(pos
-1));
755 while (pos
> 0 && (WordCharClass(cb
.CharAt(pos
- 1)) == ccStart
)) {
760 charClassification ccStart
= WordCharClass(cb
.CharAt(pos
));
761 while (pos
< (Length()) && (WordCharClass(cb
.CharAt(pos
)) == ccStart
))
763 while (pos
< (Length()) && (WordCharClass(cb
.CharAt(pos
)) == ccSpace
))
770 * Check that the character at the given position is a word or punctuation character and that
771 * the previous character is of a different character class.
773 bool Document::IsWordStartAt(int pos
) {
775 charClassification ccPos
= WordCharClass(CharAt(pos
));
776 return (ccPos
== ccWord
|| ccPos
== ccPunctuation
) &&
777 (ccPos
!= WordCharClass(CharAt(pos
- 1)));
783 * Check that the character at the given position is a word or punctuation character and that
784 * the next character is of a different character class.
786 bool Document::IsWordEndAt(int pos
) {
787 if (pos
< Length() - 1) {
788 charClassification ccPrev
= WordCharClass(CharAt(pos
-1));
789 return (ccPrev
== ccWord
|| ccPrev
== ccPunctuation
) &&
790 (ccPrev
!= WordCharClass(CharAt(pos
)));
796 * Check that the given range is has transitions between character classes at both
797 * ends and where the characters on the inside are word or punctuation characters.
799 bool Document::IsWordAt(int start
, int end
) {
800 return IsWordStartAt(start
) && IsWordEndAt(end
);
803 // The comparison and case changing functions here assume ASCII
804 // or extended ASCII such as the normal Windows code page.
806 static inline char MakeUpperCase(char ch
) {
807 if (ch
< 'a' || ch
> 'z')
810 return static_cast<char>(ch
- 'a' + 'A');
813 static inline char MakeLowerCase(char ch
) {
814 if (ch
< 'A' || ch
> 'Z')
817 return static_cast<char>(ch
- 'A' + 'a');
820 // Define a way for the Regular Expression code to access the document
821 class DocumentIndexer
: public CharacterIndexer
{
825 DocumentIndexer(Document
*pdoc_
, int end_
) :
826 pdoc(pdoc_
), end(end_
) {}
828 virtual char CharAt(int index
) {
829 if (index
< 0 || index
>= end
)
832 return pdoc
->CharAt(index
);
837 * Find text in document, supporting both forward and backward
838 * searches (just pass minPos > maxPos to do a backward search)
839 * Has not been tested with backwards DBCS searches yet.
841 long Document::FindText(int minPos
, int maxPos
, const char *s
,
842 bool caseSensitive
, bool word
, bool wordStart
, bool regExp
,
846 pre
= new RESearch();
853 if (minPos
<= maxPos
) {
861 // Range endpoints should not be inside DBCS characters, but just in case, move them.
862 startPos
= MovePositionOutsideChar(startPos
, 1, false);
863 endPos
= MovePositionOutsideChar(endPos
, 1, false);
865 const char *errmsg
= pre
->Compile(s
, *length
, caseSensitive
);
869 // Find a variable in a property file: \$(\([A-Za-z0-9_.]+\))
870 // Replace first '.' with '-' in each property file variable reference:
871 // Search: \$(\([A-Za-z0-9_-]+\)\.\([A-Za-z0-9_.]+\))
873 int lineRangeStart
= LineFromPosition(startPos
);
874 int lineRangeEnd
= LineFromPosition(endPos
);
875 if ((startPos
>= LineEnd(lineRangeStart
)) && (lineRangeStart
< lineRangeEnd
)) {
876 // the start position is at end of line or between line end characters.
878 startPos
= LineStart(lineRangeStart
);
882 char searchEnd
= s
[*length
- 1];
884 // These produce empty selections so nudge them on if needed
886 if (startPos
== LineStart(lineRangeStart
))
888 } else if (s
[0] == '$') {
889 if ((startPos
== LineEnd(lineRangeStart
)) && (lineRangeStart
< lineRangeEnd
))
890 startPos
= LineStart(lineRangeStart
+ 1);
892 lineRangeStart
= LineFromPosition(startPos
);
893 lineRangeEnd
= LineFromPosition(endPos
);
895 for (int line
= lineRangeStart
; line
<= lineRangeEnd
; line
++) {
896 int startOfLine
= LineStart(line
);
897 int endOfLine
= LineEnd(line
);
898 if (line
== lineRangeStart
) {
899 if ((startPos
!= startOfLine
) && (s
[0] == '^'))
900 continue; // Can't match start of line if start position after start of line
901 startOfLine
= startPos
;
903 if (line
== lineRangeEnd
) {
904 if ((endPos
!= endOfLine
) && (searchEnd
== '$'))
905 continue; // Can't match end of line if end position before end of line
908 DocumentIndexer
di(this, endOfLine
);
909 int success
= pre
->Execute(di
, startOfLine
, endOfLine
);
912 lenRet
= pre
->eopat
[0] - pre
->bopat
[0];
921 bool forward
= minPos
<= maxPos
;
922 int increment
= forward
? 1 : -1;
924 // Range endpoints should not be inside DBCS characters, but just in case, move them.
925 int startPos
= MovePositionOutsideChar(minPos
, increment
, false);
926 int endPos
= MovePositionOutsideChar(maxPos
, increment
, false);
928 // Compute actual search ranges needed
929 int lengthFind
= *length
;
930 if (lengthFind
== -1)
931 lengthFind
= strlen(s
);
932 int endSearch
= endPos
;
933 if (startPos
<= endPos
) {
934 endSearch
= endPos
- lengthFind
+ 1;
936 //Platform::DebugPrintf("Find %d %d %s %d\n", startPos, endPos, ft->lpstrText, lengthFind);
937 char firstChar
= s
[0];
939 firstChar
= static_cast<char>(MakeUpperCase(firstChar
));
941 while (forward
? (pos
< endSearch
) : (pos
>= endSearch
)) {
942 char ch
= CharAt(pos
);
944 if (ch
== firstChar
) {
946 for (int posMatch
= 1; posMatch
< lengthFind
&& found
; posMatch
++) {
947 ch
= CharAt(pos
+ posMatch
);
948 if (ch
!= s
[posMatch
])
952 if ((!word
&& !wordStart
) ||
953 word
&& IsWordAt(pos
, pos
+ lengthFind
) ||
954 wordStart
&& IsWordStartAt(pos
))
959 if (MakeUpperCase(ch
) == firstChar
) {
961 for (int posMatch
= 1; posMatch
< lengthFind
&& found
; posMatch
++) {
962 ch
= CharAt(pos
+ posMatch
);
963 if (MakeUpperCase(ch
) != MakeUpperCase(s
[posMatch
]))
967 if ((!word
&& !wordStart
) ||
968 word
&& IsWordAt(pos
, pos
+ lengthFind
) ||
969 wordStart
&& IsWordStartAt(pos
))
976 // Ensure trying to match from start of character
977 pos
= MovePositionOutsideChar(pos
, increment
, false);
981 //Platform::DebugPrintf("Not found\n");
985 const char *Document::SubstituteByPosition(const char *text
, int *length
) {
988 delete []substituted
;
990 DocumentIndexer
di(this, Length());
991 if (!pre
->GrabMatches(di
))
993 unsigned int lenResult
= 0;
994 for (int i
= 0; i
< *length
; i
++) {
995 if ((text
[i
] == '\\') && (text
[i
+ 1] >= '1' && text
[i
+ 1] <= '9')) {
996 unsigned int patNum
= text
[i
+ 1] - '0';
997 lenResult
+= pre
->eopat
[patNum
] - pre
->bopat
[patNum
];
1003 substituted
= new char[lenResult
+ 1];
1006 char *o
= substituted
;
1007 for (int j
= 0; j
< *length
; j
++) {
1008 if ((text
[j
] == '\\') && (text
[j
+ 1] >= '1' && text
[j
+ 1] <= '9')) {
1009 unsigned int patNum
= text
[j
+ 1] - '0';
1010 unsigned int len
= pre
->eopat
[patNum
] - pre
->bopat
[patNum
];
1011 if (pre
->pat
[patNum
]) // Will be null if try for a match that did not occur
1012 memcpy(o
, pre
->pat
[patNum
], len
);
1020 *length
= lenResult
;
1024 int Document::LinesTotal() {
1028 void Document::ChangeCase(Range r
, bool makeUpperCase
) {
1029 for (int pos
= r
.start
; pos
< r
.end
; pos
++) {
1030 char ch
= CharAt(pos
);
1031 if (dbcsCodePage
&& IsDBCS(pos
)) {
1032 pos
+= LenChar(pos
);
1034 if (makeUpperCase
) {
1036 ChangeChar(pos
, static_cast<char>(MakeUpperCase(ch
)));
1040 ChangeChar(pos
, static_cast<char>(MakeLowerCase(ch
)));
1047 void Document::SetWordChars(unsigned char *chars
) {
1049 for (ch
= 0; ch
< 256; ch
++) {
1050 if (ch
== '\r' || ch
== '\n')
1051 charClass
[ch
] = ccNewLine
;
1052 else if (ch
< 0x20 || ch
== ' ')
1053 charClass
[ch
] = ccSpace
;
1055 charClass
[ch
] = ccPunctuation
;
1059 charClass
[*chars
] = ccWord
;
1063 for (ch
= 0; ch
< 256; ch
++) {
1064 if (ch
>= 0x80 || isalnum(ch
) || ch
== '_')
1065 charClass
[ch
] = ccWord
;
1070 void Document::SetStylingBits(int bits
) {
1072 stylingBitsMask
= 0;
1073 for (int bit
= 0; bit
< stylingBits
; bit
++) {
1074 stylingBitsMask
<<= 1;
1075 stylingBitsMask
|= 1;
1079 void Document::StartStyling(int position
, char mask
) {
1081 endStyled
= position
;
1084 void Document::SetStyleFor(int length
, char style
) {
1085 if (enteredCount
== 0) {
1087 int prevEndStyled
= endStyled
;
1088 if (cb
.SetStyleFor(endStyled
, length
, style
, stylingMask
)) {
1089 DocModification
mh(SC_MOD_CHANGESTYLE
| SC_PERFORMED_USER
,
1090 prevEndStyled
, length
);
1093 endStyled
+= length
;
1098 void Document::SetStyles(int length
, char *styles
) {
1099 if (enteredCount
== 0) {
1101 int prevEndStyled
= endStyled
;
1102 bool didChange
= false;
1103 for (int iPos
= 0; iPos
< length
; iPos
++, endStyled
++) {
1104 PLATFORM_ASSERT(endStyled
< Length());
1105 if (cb
.SetStyleAt(endStyled
, styles
[iPos
], stylingMask
)) {
1110 DocModification
mh(SC_MOD_CHANGESTYLE
| SC_PERFORMED_USER
,
1111 prevEndStyled
, endStyled
- prevEndStyled
);
1118 bool Document::EnsureStyledTo(int pos
) {
1119 if (pos
> GetEndStyled()) {
1121 if (styleClock
> 0x100000) {
1125 // Ask the watchers to style, and stop as soon as one responds.
1126 for (int i
= 0; pos
> GetEndStyled() && i
< lenWatchers
; i
++)
1127 watchers
[i
].watcher
->NotifyStyleNeeded(this, watchers
[i
].userData
, pos
);
1128 return pos
<= GetEndStyled();
1131 bool Document::AddWatcher(DocWatcher
*watcher
, void *userData
) {
1132 for (int i
= 0; i
< lenWatchers
; i
++) {
1133 if ((watchers
[i
].watcher
== watcher
) &&
1134 (watchers
[i
].userData
== userData
))
1137 WatcherWithUserData
*pwNew
= new WatcherWithUserData
[lenWatchers
+ 1];
1140 for (int j
= 0; j
< lenWatchers
; j
++)
1141 pwNew
[j
] = watchers
[j
];
1142 pwNew
[lenWatchers
].watcher
= watcher
;
1143 pwNew
[lenWatchers
].userData
= userData
;
1150 bool Document::RemoveWatcher(DocWatcher
*watcher
, void *userData
) {
1151 for (int i
= 0; i
< lenWatchers
; i
++) {
1152 if ((watchers
[i
].watcher
== watcher
) &&
1153 (watchers
[i
].userData
== userData
)) {
1154 if (lenWatchers
== 1) {
1159 WatcherWithUserData
*pwNew
= new WatcherWithUserData
[lenWatchers
];
1162 for (int j
= 0; j
< lenWatchers
- 1; j
++) {
1163 pwNew
[j
] = (j
< i
) ? watchers
[j
] : watchers
[j
+ 1];
1175 void Document::NotifyModifyAttempt() {
1176 for (int i
= 0; i
< lenWatchers
; i
++) {
1177 watchers
[i
].watcher
->NotifyModifyAttempt(this, watchers
[i
].userData
);
1181 void Document::NotifySavePoint(bool atSavePoint
) {
1182 for (int i
= 0; i
< lenWatchers
; i
++) {
1183 watchers
[i
].watcher
->NotifySavePoint(this, watchers
[i
].userData
, atSavePoint
);
1187 void Document::NotifyModified(DocModification mh
) {
1188 for (int i
= 0; i
< lenWatchers
; i
++) {
1189 watchers
[i
].watcher
->NotifyModified(this, mh
, watchers
[i
].userData
);
1193 bool Document::IsWordPartSeparator(char ch
) {
1194 return ispunct(ch
) && (WordCharClass(ch
) == ccWord
);
1197 int Document::WordPartLeft(int pos
) {
1200 char startChar
= cb
.CharAt(pos
);
1201 if (IsWordPartSeparator(startChar
)) {
1202 while (pos
> 0 && IsWordPartSeparator(cb
.CharAt(pos
))) {
1207 startChar
= cb
.CharAt(pos
);
1209 if (islower(startChar
)) {
1210 while (pos
> 0 && islower(cb
.CharAt(pos
)))
1212 if (!isupper(cb
.CharAt(pos
)) && !islower(cb
.CharAt(pos
)))
1214 } else if (isupper(startChar
)) {
1215 while (pos
> 0 && isupper(cb
.CharAt(pos
)))
1217 if (!isupper(cb
.CharAt(pos
)))
1219 } else if (isdigit(startChar
)) {
1220 while (pos
> 0 && isdigit(cb
.CharAt(pos
)))
1222 if (!isdigit(cb
.CharAt(pos
)))
1224 } else if (ispunct(startChar
)) {
1225 while (pos
> 0 && ispunct(cb
.CharAt(pos
)))
1227 if (!ispunct(cb
.CharAt(pos
)))
1229 } else if (isspacechar(startChar
)) {
1230 while (pos
> 0 && isspacechar(cb
.CharAt(pos
)))
1232 if (!isspacechar(cb
.CharAt(pos
)))
1240 int Document::WordPartRight(int pos
) {
1241 char startChar
= cb
.CharAt(pos
);
1242 int length
= Length();
1243 if (IsWordPartSeparator(startChar
)) {
1244 while (pos
< length
&& IsWordPartSeparator(cb
.CharAt(pos
)))
1246 startChar
= cb
.CharAt(pos
);
1248 if (islower(startChar
)) {
1249 while (pos
< length
&& islower(cb
.CharAt(pos
)))
1251 } else if (isupper(startChar
)) {
1252 if (islower(cb
.CharAt(pos
+ 1))) {
1254 while (pos
< length
&& islower(cb
.CharAt(pos
)))
1257 while (pos
< length
&& isupper(cb
.CharAt(pos
)))
1260 if (islower(cb
.CharAt(pos
)) && isupper(cb
.CharAt(pos
- 1)))
1262 } else if (isdigit(startChar
)) {
1263 while (pos
< length
&& isdigit(cb
.CharAt(pos
)))
1265 } else if (ispunct(startChar
)) {
1266 while (pos
< length
&& ispunct(cb
.CharAt(pos
)))
1268 } else if (isspacechar(startChar
)) {
1269 while (pos
< length
&& isspacechar(cb
.CharAt(pos
)))