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.
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 static inline bool IsPunctuation(char ch
) {
27 return isascii(ch
) && ispunct(ch
);
30 static inline bool IsADigit(char ch
) {
31 return isascii(ch
) && isdigit(ch
);
34 static inline bool IsLowerCase(char ch
) {
35 return isascii(ch
) && islower(ch
);
38 static inline bool IsUpperCase(char ch
) {
39 return isascii(ch
) && isupper(ch
);
42 Document::Document() {
47 eolMode
= SC_EOL_CRLF
;
51 stylingBitsMask
= 0x1F;
57 enteredReadOnlyCount
= 0;
62 backspaceUnindents
= false;
71 Document::~Document() {
72 for (int i
= 0; i
< lenWatchers
; i
++) {
73 watchers
[i
].watcher
->NotifyDeleted(this, watchers
[i
].userData
);
84 // Increase reference count and return its previous value.
85 int Document::AddRef() {
89 // Decrease reference count and return its previous value.
90 // Delete the document if reference count reaches zero.
91 int Document::Release() {
92 int curRefCount
= --refCount
;
98 void Document::SetSavePoint() {
100 NotifySavePoint(true);
103 int Document::AddMark(int line
, int markerNum
) {
104 int prev
= cb
.AddMark(line
, markerNum
);
105 DocModification
mh(SC_MOD_CHANGEMARKER
, LineStart(line
), 0, 0, 0);
110 void Document::DeleteMark(int line
, int markerNum
) {
111 cb
.DeleteMark(line
, markerNum
);
112 DocModification
mh(SC_MOD_CHANGEMARKER
, LineStart(line
), 0, 0, 0);
116 void Document::DeleteMarkFromHandle(int markerHandle
) {
117 cb
.DeleteMarkFromHandle(markerHandle
);
118 DocModification
mh(SC_MOD_CHANGEMARKER
, 0, 0, 0, 0);
122 void Document::DeleteAllMarks(int markerNum
) {
123 cb
.DeleteAllMarks(markerNum
);
124 DocModification
mh(SC_MOD_CHANGEMARKER
, 0, 0, 0, 0);
128 int Document::LineStart(int line
) {
129 return cb
.LineStart(line
);
132 int Document::LineEnd(int line
) {
133 if (line
== LinesTotal() - 1) {
134 return LineStart(line
+ 1);
136 int position
= LineStart(line
+ 1) - 1;
137 // When line terminator is CR+LF, may need to go back one more
138 if ((position
> LineStart(line
)) && (cb
.CharAt(position
- 1) == '\r')) {
145 int Document::LineFromPosition(int pos
) {
146 return cb
.LineFromPosition(pos
);
149 int Document::LineEndPosition(int position
) {
150 return LineEnd(LineFromPosition(position
));
153 int Document::VCHomePosition(int position
) {
154 int line
= LineFromPosition(position
);
155 int startPosition
= LineStart(line
);
156 int endLine
= LineStart(line
+ 1) - 1;
157 int startText
= startPosition
;
158 while (startText
< endLine
&& (cb
.CharAt(startText
) == ' ' || cb
.CharAt(startText
) == '\t' ) )
160 if (position
== startText
)
161 return startPosition
;
166 int Document::SetLevel(int line
, int level
) {
167 int prev
= cb
.SetLevel(line
, level
);
169 DocModification
mh(SC_MOD_CHANGEFOLD
| SC_MOD_CHANGEMARKER
,
170 LineStart(line
), 0, 0, 0);
172 mh
.foldLevelNow
= level
;
173 mh
.foldLevelPrev
= prev
;
179 static bool IsSubordinate(int levelStart
, int levelTry
) {
180 if (levelTry
& SC_FOLDLEVELWHITEFLAG
)
183 return (levelStart
& SC_FOLDLEVELNUMBERMASK
) < (levelTry
& SC_FOLDLEVELNUMBERMASK
);
186 int Document::GetLastChild(int lineParent
, int level
) {
188 level
= GetLevel(lineParent
) & SC_FOLDLEVELNUMBERMASK
;
189 int maxLine
= LinesTotal();
190 int lineMaxSubord
= lineParent
;
191 while (lineMaxSubord
< maxLine
- 1) {
192 EnsureStyledTo(LineStart(lineMaxSubord
+ 2));
193 if (!IsSubordinate(level
, GetLevel(lineMaxSubord
+ 1)))
197 if (lineMaxSubord
> lineParent
) {
198 if (level
> (GetLevel(lineMaxSubord
+ 1) & SC_FOLDLEVELNUMBERMASK
)) {
199 // Have chewed up some whitespace that belongs to a parent so seek back
200 if (GetLevel(lineMaxSubord
) & SC_FOLDLEVELWHITEFLAG
) {
205 return lineMaxSubord
;
208 int Document::GetFoldParent(int line
) {
209 int level
= GetLevel(line
);
210 int lineLook
= line
- 1;
211 while ((lineLook
> 0) && (
212 (!(GetLevel(lineLook
) & SC_FOLDLEVELHEADERFLAG
)) ||
213 ((GetLevel(lineLook
) & SC_FOLDLEVELNUMBERMASK
) >= level
))
217 if ((GetLevel(lineLook
) & SC_FOLDLEVELHEADERFLAG
) &&
218 ((GetLevel(lineLook
) & SC_FOLDLEVELNUMBERMASK
) < level
)) {
225 int Document::ClampPositionIntoDocument(int pos
) {
226 return Platform::Clamp(pos
, 0, Length());
229 bool Document::IsCrLf(int pos
) {
232 if (pos
>= (Length() - 1))
234 return (cb
.CharAt(pos
) == '\r') && (cb
.CharAt(pos
+ 1) == '\n');
237 static const int maxBytesInDBCSCharacter
=5;
239 int Document::LenChar(int pos
) {
242 } else if (IsCrLf(pos
)) {
244 } else if (SC_CP_UTF8
== dbcsCodePage
) {
245 unsigned char ch
= static_cast<unsigned char>(cb
.CharAt(pos
));
249 if (ch
>= (0x80 + 0x40 + 0x20))
251 int lengthDoc
= Length();
252 if ((pos
+ len
) > lengthDoc
)
253 return lengthDoc
-pos
;
256 } else if (dbcsCodePage
) {
257 char mbstr
[maxBytesInDBCSCharacter
+1];
259 for (i
=0; i
<Platform::DBCSCharMaxLength(); i
++) {
260 mbstr
[i
] = cb
.CharAt(pos
+i
);
263 return Platform::DBCSCharLength(dbcsCodePage
, mbstr
);
269 // Normalise a position so that it is not halfway through a two byte character.
270 // This can occur in two situations -
271 // When lines are terminated with \r\n pairs which should be treated as one character.
272 // When displaying DBCS text such as Japanese.
273 // If moving, move the position in the indicated direction.
274 int Document::MovePositionOutsideChar(int pos
, int moveDir
, bool checkLineEnd
) {
275 //Platform::DebugPrintf("NoCRLF %d %d\n", pos, moveDir);
276 // If out of range, just return minimum/maximum value.
282 // assert pos > 0 && pos < Length()
283 if (checkLineEnd
&& IsCrLf(pos
- 1)) {
290 // Not between CR and LF
293 if (SC_CP_UTF8
== dbcsCodePage
) {
294 unsigned char ch
= static_cast<unsigned char>(cb
.CharAt(pos
));
295 while ((pos
> 0) && (pos
< Length()) && (ch
>= 0x80) && (ch
< (0x80 + 0x40))) {
296 // ch is a trail byte
301 ch
= static_cast<unsigned char>(cb
.CharAt(pos
));
304 // Anchor DBCS calculations at start of line because start of line can
305 // not be a DBCS trail byte.
306 int posCheck
= LineStart(LineFromPosition(pos
));
307 while (posCheck
< pos
) {
308 char mbstr
[maxBytesInDBCSCharacter
+1];
310 for(i
=0;i
<Platform::DBCSCharMaxLength();i
++) {
311 mbstr
[i
] = cb
.CharAt(posCheck
+i
);
315 int mbsize
= Platform::DBCSCharLength(dbcsCodePage
, mbstr
);
316 if (posCheck
+ mbsize
== pos
) {
318 } else if (posCheck
+ mbsize
> pos
) {
320 return posCheck
+ mbsize
;
333 void Document::ModifiedAt(int pos
) {
338 // Document only modified by gateways DeleteChars, InsertStyledString, Undo, Redo, and SetStyleAt.
339 // SetStyleAt does not change the persistent state of a document
341 // Unlike Undo, Redo, and InsertStyledString, the pos argument is a cell number not a char number
342 bool Document::DeleteChars(int pos
, int len
) {
345 if ((pos
+ len
) > Length())
347 if (cb
.IsReadOnly() && enteredReadOnlyCount
== 0) {
348 enteredReadOnlyCount
++;
349 NotifyModifyAttempt();
350 enteredReadOnlyCount
--;
352 if (enteredCount
!= 0) {
356 if (!cb
.IsReadOnly()) {
359 SC_MOD_BEFOREDELETE
| SC_PERFORMED_USER
,
362 int prevLinesTotal
= LinesTotal();
363 bool startSavePoint
= cb
.IsSavePoint();
364 const char *text
= cb
.DeleteChars(pos
* 2, len
* 2);
365 if (startSavePoint
&& cb
.IsCollectingUndo())
366 NotifySavePoint(!startSavePoint
);
367 if ((pos
< Length()) || (pos
== 0))
373 SC_MOD_DELETETEXT
| SC_PERFORMED_USER
,
375 LinesTotal() - prevLinesTotal
, text
));
379 return !cb
.IsReadOnly();
382 bool Document::InsertStyledString(int position
, char *s
, int insertLength
) {
383 if (cb
.IsReadOnly() && enteredReadOnlyCount
== 0) {
384 enteredReadOnlyCount
++;
385 NotifyModifyAttempt();
386 enteredReadOnlyCount
--;
388 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
));
412 return !cb
.IsReadOnly();
415 int Document::Undo() {
417 if (enteredCount
== 0) {
419 bool startSavePoint
= cb
.IsSavePoint();
420 int steps
= cb
.StartUndo();
421 //Platform::DebugPrintf("Steps=%d\n", steps);
422 for (int step
= 0; step
< steps
; step
++) {
423 int prevLinesTotal
= LinesTotal();
424 const Action
&action
= cb
.GetUndoStep();
425 if (action
.at
== removeAction
) {
426 NotifyModified(DocModification(
427 SC_MOD_BEFOREINSERT
| SC_PERFORMED_UNDO
, action
));
429 NotifyModified(DocModification(
430 SC_MOD_BEFOREDELETE
| SC_PERFORMED_UNDO
, action
));
432 cb
.PerformUndoStep();
433 int cellPosition
= action
.position
/ 2;
434 ModifiedAt(cellPosition
);
435 newPos
= cellPosition
;
437 int modFlags
= SC_PERFORMED_UNDO
;
438 // With undo, an insertion action becomes a deletion notification
439 if (action
.at
== removeAction
) {
440 newPos
+= action
.lenData
;
441 modFlags
|= SC_MOD_INSERTTEXT
;
443 modFlags
|= SC_MOD_DELETETEXT
;
445 if (step
== steps
- 1)
446 modFlags
|= SC_LASTSTEPINUNDOREDO
;
447 NotifyModified(DocModification(modFlags
, cellPosition
, action
.lenData
,
448 LinesTotal() - prevLinesTotal
, action
.data
));
451 bool endSavePoint
= cb
.IsSavePoint();
452 if (startSavePoint
!= endSavePoint
)
453 NotifySavePoint(endSavePoint
);
459 int Document::Redo() {
461 if (enteredCount
== 0) {
463 bool startSavePoint
= cb
.IsSavePoint();
464 int steps
= cb
.StartRedo();
465 for (int step
= 0; step
< steps
; step
++) {
466 int prevLinesTotal
= LinesTotal();
467 const Action
&action
= cb
.GetRedoStep();
468 if (action
.at
== insertAction
) {
469 NotifyModified(DocModification(
470 SC_MOD_BEFOREINSERT
| SC_PERFORMED_REDO
, action
));
472 NotifyModified(DocModification(
473 SC_MOD_BEFOREDELETE
| SC_PERFORMED_REDO
, action
));
475 cb
.PerformRedoStep();
476 ModifiedAt(action
.position
/ 2);
477 newPos
= action
.position
/ 2;
479 int modFlags
= SC_PERFORMED_REDO
;
480 if (action
.at
== insertAction
) {
481 newPos
+= action
.lenData
;
482 modFlags
|= SC_MOD_INSERTTEXT
;
484 modFlags
|= SC_MOD_DELETETEXT
;
486 if (step
== steps
- 1)
487 modFlags
|= SC_LASTSTEPINUNDOREDO
;
489 DocModification(modFlags
, action
.position
/ 2, action
.lenData
,
490 LinesTotal() - prevLinesTotal
, action
.data
));
493 bool endSavePoint
= cb
.IsSavePoint();
494 if (startSavePoint
!= endSavePoint
)
495 NotifySavePoint(endSavePoint
);
501 bool Document::InsertChar(int pos
, char ch
) {
505 return InsertStyledString(pos
*2, chs
, 2);
508 // Insert a null terminated string
509 bool Document::InsertString(int position
, const char *s
) {
510 return InsertString(position
, s
, strlen(s
));
513 // Insert a string with a length
514 bool Document::InsertString(int position
, const char *s
, size_t insertLength
) {
515 bool changed
= false;
516 char *sWithStyle
= new char[insertLength
* 2];
518 for (size_t i
= 0; i
< insertLength
; i
++) {
519 sWithStyle
[i
*2] = s
[i
];
520 sWithStyle
[i
*2 + 1] = 0;
522 changed
= InsertStyledString(position
*2, sWithStyle
,
523 static_cast<int>(insertLength
*2));
529 void Document::ChangeChar(int pos
, char ch
) {
534 void Document::DelChar(int pos
) {
535 DeleteChars(pos
, LenChar(pos
));
538 void Document::DelCharBack(int pos
) {
541 } else if (IsCrLf(pos
- 2)) {
542 DeleteChars(pos
- 2, 2);
543 } else if (dbcsCodePage
) {
544 int startChar
= MovePositionOutsideChar(pos
- 1, -1, false);
545 DeleteChars(startChar
, pos
- startChar
);
547 DeleteChars(pos
- 1, 1);
551 static bool isindentchar(char ch
) {
552 return (ch
== ' ') || (ch
== '\t');
555 static int NextTab(int pos
, int tabSize
) {
556 return ((pos
/ tabSize
) + 1) * tabSize
;
559 static void CreateIndentation(char *linebuf
, int length
, int indent
, int tabSize
, bool insertSpaces
) {
560 length
--; // ensure space for \0
562 while ((indent
>= tabSize
) && (length
> 0)) {
568 while ((indent
> 0) && (length
> 0)) {
576 int Document::GetLineIndentation(int line
) {
578 if ((line
>= 0) && (line
< LinesTotal())) {
579 int lineStart
= LineStart(line
);
580 int length
= Length();
581 for (int i
= lineStart
;i
< length
;i
++) {
582 char ch
= cb
.CharAt(i
);
586 indent
= NextTab(indent
, tabInChars
);
594 void Document::SetLineIndentation(int line
, int indent
) {
595 int indentOfLine
= GetLineIndentation(line
);
598 if (indent
!= indentOfLine
) {
600 CreateIndentation(linebuf
, sizeof(linebuf
), indent
, tabInChars
, !useTabs
);
601 int thisLineStart
= LineStart(line
);
602 int indentPos
= GetLineIndentPosition(line
);
603 DeleteChars(thisLineStart
, indentPos
- thisLineStart
);
604 InsertString(thisLineStart
, linebuf
);
608 int Document::GetLineIndentPosition(int line
) {
611 int pos
= LineStart(line
);
612 int length
= Length();
613 while ((pos
< length
) && isindentchar(cb
.CharAt(pos
))) {
619 int Document::GetColumn(int pos
) {
621 int line
= LineFromPosition(pos
);
622 if ((line
>= 0) && (line
< LinesTotal())) {
623 for (int i
= LineStart(line
);i
< pos
;) {
624 char ch
= cb
.CharAt(i
);
626 column
= NextTab(column
, tabInChars
);
628 } else if (ch
== '\r') {
630 } else if (ch
== '\n') {
634 i
= MovePositionOutsideChar(i
+ 1, 1);
641 int Document::FindColumn(int line
, int column
) {
642 int position
= LineStart(line
);
643 int columnCurrent
= 0;
644 if ((line
>= 0) && (line
< LinesTotal())) {
645 while (columnCurrent
< column
) {
646 char ch
= cb
.CharAt(position
);
648 columnCurrent
= NextTab(columnCurrent
, tabInChars
);
650 } else if (ch
== '\r') {
652 } else if (ch
== '\n') {
656 position
= MovePositionOutsideChar(position
+ 1, 1);
663 void Document::Indent(bool forwards
, int lineBottom
, int lineTop
) {
664 // Dedent - suck white space off the front of the line to dedent by equivalent of a tab
665 for (int line
= lineBottom
; line
>= lineTop
; line
--) {
666 int indentOfLine
= GetLineIndentation(line
);
668 SetLineIndentation(line
, indentOfLine
+ IndentSize());
670 SetLineIndentation(line
, indentOfLine
- IndentSize());
674 void Document::ConvertLineEnds(int eolModeSet
) {
676 for (int pos
= 0; pos
< Length(); pos
++) {
677 if (cb
.CharAt(pos
) == '\r') {
678 if (cb
.CharAt(pos
+ 1) == '\n') {
679 if (eolModeSet
!= SC_EOL_CRLF
) {
681 if (eolModeSet
== SC_EOL_CR
)
682 InsertString(pos
, "\r", 1);
684 InsertString(pos
, "\n", 1);
689 if (eolModeSet
!= SC_EOL_CR
) {
691 if (eolModeSet
== SC_EOL_CRLF
) {
692 InsertString(pos
, "\r\n", 2);
695 InsertString(pos
, "\n", 1);
699 } else if (cb
.CharAt(pos
) == '\n') {
700 if (eolModeSet
!= SC_EOL_LF
) {
702 if (eolModeSet
== SC_EOL_CRLF
) {
703 InsertString(pos
, "\r\n", 2);
706 InsertString(pos
, "\r", 1);
714 int Document::ParaDown(int pos
) {
715 int line
= LineFromPosition(pos
);
716 while (line
< LinesTotal() && LineStart(line
) != LineEnd(line
)) { // skip non-empty lines
719 while (line
< LinesTotal() && LineStart(line
) == LineEnd(line
)) { // skip empty lines
722 if (line
< LinesTotal())
723 return LineStart(line
);
724 else // end of a document
725 return LineEnd(line
-1);
728 int Document::ParaUp(int pos
) {
729 int line
= LineFromPosition(pos
);
731 while (line
>= 0 && LineStart(line
) == LineEnd(line
)) { // skip empty lines
734 while (line
>= 0 && LineStart(line
) != LineEnd(line
)) { // skip non-empty lines
738 return LineStart(line
);
741 Document::charClassification
Document::WordCharClass(unsigned char ch
) {
742 if ((SC_CP_UTF8
== dbcsCodePage
) && (ch
>= 0x80))
744 return charClass
[ch
];
748 * Used by commmands that want to select whole words.
749 * Finds the start of word at pos when delta < 0 or the end of the word when delta >= 0.
751 int Document::ExtendWordSelect(int pos
, int delta
, bool onlyWordCharacters
) {
752 charClassification ccStart
= ccWord
;
754 if (!onlyWordCharacters
)
755 ccStart
= WordCharClass(cb
.CharAt(pos
-1));
756 while (pos
> 0 && (WordCharClass(cb
.CharAt(pos
- 1)) == ccStart
))
759 if (!onlyWordCharacters
)
760 ccStart
= WordCharClass(cb
.CharAt(pos
));
761 while (pos
< (Length()) && (WordCharClass(cb
.CharAt(pos
)) == ccStart
))
768 * Find the start of the next word in either a forward (delta >= 0) or backwards direction
770 * This is looking for a transition between character classes although there is also some
771 * additional movement to transit white space.
772 * Used by cursor movement by word commands.
774 int Document::NextWordStart(int pos
, int delta
) {
776 while (pos
> 0 && (WordCharClass(cb
.CharAt(pos
- 1)) == ccSpace
))
779 charClassification ccStart
= WordCharClass(cb
.CharAt(pos
-1));
780 while (pos
> 0 && (WordCharClass(cb
.CharAt(pos
- 1)) == ccStart
)) {
785 charClassification ccStart
= WordCharClass(cb
.CharAt(pos
));
786 while (pos
< (Length()) && (WordCharClass(cb
.CharAt(pos
)) == ccStart
))
788 while (pos
< (Length()) && (WordCharClass(cb
.CharAt(pos
)) == ccSpace
))
795 * Check that the character at the given position is a word or punctuation character and that
796 * the previous character is of a different character class.
798 bool Document::IsWordStartAt(int pos
) {
800 charClassification ccPos
= WordCharClass(CharAt(pos
));
801 return (ccPos
== ccWord
|| ccPos
== ccPunctuation
) &&
802 (ccPos
!= WordCharClass(CharAt(pos
- 1)));
808 * Check that the character at the given position is a word or punctuation character and that
809 * the next character is of a different character class.
811 bool Document::IsWordEndAt(int pos
) {
812 if (pos
< Length() - 1) {
813 charClassification ccPrev
= WordCharClass(CharAt(pos
-1));
814 return (ccPrev
== ccWord
|| ccPrev
== ccPunctuation
) &&
815 (ccPrev
!= WordCharClass(CharAt(pos
)));
821 * Check that the given range is has transitions between character classes at both
822 * ends and where the characters on the inside are word or punctuation characters.
824 bool Document::IsWordAt(int start
, int end
) {
825 return IsWordStartAt(start
) && IsWordEndAt(end
);
828 // The comparison and case changing functions here assume ASCII
829 // or extended ASCII such as the normal Windows code page.
831 static inline char MakeUpperCase(char ch
) {
832 if (ch
< 'a' || ch
> 'z')
835 return static_cast<char>(ch
- 'a' + 'A');
838 static inline char MakeLowerCase(char ch
) {
839 if (ch
< 'A' || ch
> 'Z')
842 return static_cast<char>(ch
- 'A' + 'a');
845 // Define a way for the Regular Expression code to access the document
846 class DocumentIndexer
: public CharacterIndexer
{
850 DocumentIndexer(Document
*pdoc_
, int end_
) :
851 pdoc(pdoc_
), end(end_
) {
854 virtual char CharAt(int index
) {
855 if (index
< 0 || index
>= end
)
858 return pdoc
->CharAt(index
);
863 * Find text in document, supporting both forward and backward
864 * searches (just pass minPos > maxPos to do a backward search)
865 * Has not been tested with backwards DBCS searches yet.
867 long Document::FindText(int minPos
, int maxPos
, const char *s
,
868 bool caseSensitive
, bool word
, bool wordStart
, bool regExp
, bool posix
,
872 pre
= new RESearch();
876 int increment
= (minPos
<= maxPos
) ? 1 : -1;
878 int startPos
= minPos
;
881 // Range endpoints should not be inside DBCS characters, but just in case, move them.
882 startPos
= MovePositionOutsideChar(startPos
, 1, false);
883 endPos
= MovePositionOutsideChar(endPos
, 1, false);
885 const char *errmsg
= pre
->Compile(s
, *length
, caseSensitive
, posix
);
889 // Find a variable in a property file: \$(\([A-Za-z0-9_.]+\))
890 // Replace first '.' with '-' in each property file variable reference:
891 // Search: \$(\([A-Za-z0-9_-]+\)\.\([A-Za-z0-9_.]+\))
893 int lineRangeStart
= LineFromPosition(startPos
);
894 int lineRangeEnd
= LineFromPosition(endPos
);
895 if ((increment
== 1) &&
896 (startPos
>= LineEnd(lineRangeStart
)) &&
897 (lineRangeStart
< lineRangeEnd
)) {
898 // the start position is at end of line or between line end characters.
900 startPos
= LineStart(lineRangeStart
);
904 char searchEnd
= s
[*length
- 1];
905 int lineRangeBreak
= lineRangeEnd
+ increment
;
906 for (int line
= lineRangeStart
; line
!= lineRangeBreak
; line
+= increment
) {
907 int startOfLine
= LineStart(line
);
908 int endOfLine
= LineEnd(line
);
909 if (increment
== 1) {
910 if (line
== lineRangeStart
) {
911 if ((startPos
!= startOfLine
) && (s
[0] == '^'))
912 continue; // Can't match start of line if start position after start of line
913 startOfLine
= startPos
;
915 if (line
== lineRangeEnd
) {
916 if ((endPos
!= endOfLine
) && (searchEnd
== '$'))
917 continue; // Can't match end of line if end position before end of line
921 if (line
== lineRangeEnd
) {
922 if ((endPos
!= startOfLine
) && (s
[0] == '^'))
923 continue; // Can't match start of line if end position after start of line
924 startOfLine
= endPos
;
926 if (line
== lineRangeStart
) {
927 if ((startPos
!= endOfLine
) && (searchEnd
== '$'))
928 continue; // Can't match end of line if start position before end of line
929 endOfLine
= startPos
+1;
933 DocumentIndexer
di(this, endOfLine
);
934 int success
= pre
->Execute(di
, startOfLine
, endOfLine
);
937 lenRet
= pre
->eopat
[0] - pre
->bopat
[0];
938 if (increment
== -1) {
939 // Check for the last match on this line.
940 int repetitions
= 1000; // Break out of infinite loop
941 while (success
&& (pre
->eopat
[0] <= (endOfLine
+1)) && (repetitions
--)) {
942 success
= pre
->Execute(di
, pos
+1, endOfLine
+1);
944 if (pre
->eopat
[0] <= (minPos
+1)) {
946 lenRet
= pre
->eopat
[0] - pre
->bopat
[0];
961 bool forward
= minPos
<= maxPos
;
962 int increment
= forward
? 1 : -1;
964 // Range endpoints should not be inside DBCS characters, but just in case, move them.
965 int startPos
= MovePositionOutsideChar(minPos
, increment
, false);
966 int endPos
= MovePositionOutsideChar(maxPos
, increment
, false);
968 // Compute actual search ranges needed
969 int lengthFind
= *length
;
970 if (lengthFind
== -1)
971 lengthFind
= static_cast<int>(strlen(s
));
972 int endSearch
= endPos
;
973 if (startPos
<= endPos
) {
974 endSearch
= endPos
- lengthFind
+ 1;
976 //Platform::DebugPrintf("Find %d %d %s %d\n", startPos, endPos, ft->lpstrText, lengthFind);
977 char firstChar
= s
[0];
979 firstChar
= static_cast<char>(MakeUpperCase(firstChar
));
981 while (forward
? (pos
< endSearch
) : (pos
>= endSearch
)) {
982 char ch
= CharAt(pos
);
984 if (ch
== firstChar
) {
986 for (int posMatch
= 1; posMatch
< lengthFind
&& found
; posMatch
++) {
987 ch
= CharAt(pos
+ posMatch
);
988 if (ch
!= s
[posMatch
])
992 if ((!word
&& !wordStart
) ||
993 word
&& IsWordAt(pos
, pos
+ lengthFind
) ||
994 wordStart
&& IsWordStartAt(pos
))
999 if (MakeUpperCase(ch
) == firstChar
) {
1001 for (int posMatch
= 1; posMatch
< lengthFind
&& found
; posMatch
++) {
1002 ch
= CharAt(pos
+ posMatch
);
1003 if (MakeUpperCase(ch
) != MakeUpperCase(s
[posMatch
]))
1007 if ((!word
&& !wordStart
) ||
1008 word
&& IsWordAt(pos
, pos
+ lengthFind
) ||
1009 wordStart
&& IsWordStartAt(pos
))
1016 // Ensure trying to match from start of character
1017 pos
= MovePositionOutsideChar(pos
, increment
, false);
1021 //Platform::DebugPrintf("Not found\n");
1025 const char *Document::SubstituteByPosition(const char *text
, int *length
) {
1028 delete []substituted
;
1030 DocumentIndexer
di(this, Length());
1031 if (!pre
->GrabMatches(di
))
1033 unsigned int lenResult
= 0;
1034 for (int i
= 0; i
< *length
; i
++) {
1035 if ((text
[i
] == '\\') && (text
[i
+ 1] >= '1' && text
[i
+ 1] <= '9')) {
1036 unsigned int patNum
= text
[i
+ 1] - '0';
1037 lenResult
+= pre
->eopat
[patNum
] - pre
->bopat
[patNum
];
1043 substituted
= new char[lenResult
+ 1];
1046 char *o
= substituted
;
1047 for (int j
= 0; j
< *length
; j
++) {
1048 if ((text
[j
] == '\\') && (text
[j
+ 1] >= '1' && text
[j
+ 1] <= '9')) {
1049 unsigned int patNum
= text
[j
+ 1] - '0';
1050 unsigned int len
= pre
->eopat
[patNum
] - pre
->bopat
[patNum
];
1051 if (pre
->pat
[patNum
]) // Will be null if try for a match that did not occur
1052 memcpy(o
, pre
->pat
[patNum
], len
);
1060 *length
= lenResult
;
1064 int Document::LinesTotal() {
1068 void Document::ChangeCase(Range r
, bool makeUpperCase
) {
1069 for (int pos
= r
.start
; pos
< r
.end
; pos
++) {
1070 int len
= LenChar(pos
);
1071 if (dbcsCodePage
&& (len
> 1)) {
1074 char ch
= CharAt(pos
);
1075 if (makeUpperCase
) {
1076 if (IsLowerCase(ch
)) {
1077 ChangeChar(pos
, static_cast<char>(MakeUpperCase(ch
)));
1080 if (IsUpperCase(ch
)) {
1081 ChangeChar(pos
, static_cast<char>(MakeLowerCase(ch
)));
1088 void Document::SetWordChars(unsigned char *chars
) {
1090 for (ch
= 0; ch
< 256; ch
++) {
1091 if (ch
== '\r' || ch
== '\n')
1092 charClass
[ch
] = ccNewLine
;
1093 else if (ch
< 0x20 || ch
== ' ')
1094 charClass
[ch
] = ccSpace
;
1096 charClass
[ch
] = ccPunctuation
;
1100 charClass
[*chars
] = ccWord
;
1104 for (ch
= 0; ch
< 256; ch
++) {
1105 if (ch
>= 0x80 || isalnum(ch
) || ch
== '_')
1106 charClass
[ch
] = ccWord
;
1111 void Document::SetStylingBits(int bits
) {
1113 stylingBitsMask
= 0;
1114 for (int bit
= 0; bit
< stylingBits
; bit
++) {
1115 stylingBitsMask
<<= 1;
1116 stylingBitsMask
|= 1;
1120 void Document::StartStyling(int position
, char mask
) {
1122 endStyled
= position
;
1125 bool Document::SetStyleFor(int length
, char style
) {
1126 if (enteredCount
!= 0) {
1130 style
&= stylingMask
;
1131 int prevEndStyled
= endStyled
;
1132 if (cb
.SetStyleFor(endStyled
, length
, style
, stylingMask
)) {
1133 DocModification
mh(SC_MOD_CHANGESTYLE
| SC_PERFORMED_USER
,
1134 prevEndStyled
, length
);
1137 endStyled
+= length
;
1143 bool Document::SetStyles(int length
, char *styles
) {
1144 if (enteredCount
!= 0) {
1148 int prevEndStyled
= endStyled
;
1149 bool didChange
= false;
1151 for (int iPos
= 0; iPos
< length
; iPos
++, endStyled
++) {
1152 PLATFORM_ASSERT(endStyled
< Length());
1153 if (cb
.SetStyleAt(endStyled
, styles
[iPos
], stylingMask
)) {
1159 DocModification
mh(SC_MOD_CHANGESTYLE
| SC_PERFORMED_USER
,
1160 prevEndStyled
, lastChange
);
1168 bool Document::EnsureStyledTo(int pos
) {
1169 if (pos
> GetEndStyled()) {
1171 if (styleClock
> 0x100000) {
1174 // Ask the watchers to style, and stop as soon as one responds.
1175 for (int i
= 0; pos
> GetEndStyled() && i
< lenWatchers
; i
++) {
1176 watchers
[i
].watcher
->NotifyStyleNeeded(this, watchers
[i
].userData
, pos
);
1179 return pos
<= GetEndStyled();
1182 bool Document::AddWatcher(DocWatcher
*watcher
, void *userData
) {
1183 for (int i
= 0; i
< lenWatchers
; i
++) {
1184 if ((watchers
[i
].watcher
== watcher
) &&
1185 (watchers
[i
].userData
== userData
))
1188 WatcherWithUserData
*pwNew
= new WatcherWithUserData
[lenWatchers
+ 1];
1191 for (int j
= 0; j
< lenWatchers
; j
++)
1192 pwNew
[j
] = watchers
[j
];
1193 pwNew
[lenWatchers
].watcher
= watcher
;
1194 pwNew
[lenWatchers
].userData
= userData
;
1201 bool Document::RemoveWatcher(DocWatcher
*watcher
, void *userData
) {
1202 for (int i
= 0; i
< lenWatchers
; i
++) {
1203 if ((watchers
[i
].watcher
== watcher
) &&
1204 (watchers
[i
].userData
== userData
)) {
1205 if (lenWatchers
== 1) {
1210 WatcherWithUserData
*pwNew
= new WatcherWithUserData
[lenWatchers
];
1213 for (int j
= 0; j
< lenWatchers
- 1; j
++) {
1214 pwNew
[j
] = (j
< i
) ? watchers
[j
] : watchers
[j
+ 1];
1226 void Document::NotifyModifyAttempt() {
1227 for (int i
= 0; i
< lenWatchers
; i
++) {
1228 watchers
[i
].watcher
->NotifyModifyAttempt(this, watchers
[i
].userData
);
1232 void Document::NotifySavePoint(bool atSavePoint
) {
1233 for (int i
= 0; i
< lenWatchers
; i
++) {
1234 watchers
[i
].watcher
->NotifySavePoint(this, watchers
[i
].userData
, atSavePoint
);
1238 void Document::NotifyModified(DocModification mh
) {
1239 for (int i
= 0; i
< lenWatchers
; i
++) {
1240 watchers
[i
].watcher
->NotifyModified(this, mh
, watchers
[i
].userData
);
1244 bool Document::IsWordPartSeparator(char ch
) {
1245 return (WordCharClass(ch
) == ccWord
) && IsPunctuation(ch
);
1248 int Document::WordPartLeft(int pos
) {
1251 char startChar
= cb
.CharAt(pos
);
1252 if (IsWordPartSeparator(startChar
)) {
1253 while (pos
> 0 && IsWordPartSeparator(cb
.CharAt(pos
))) {
1258 startChar
= cb
.CharAt(pos
);
1260 if (IsLowerCase(startChar
)) {
1261 while (pos
> 0 && IsLowerCase(cb
.CharAt(pos
)))
1263 if (!IsUpperCase(cb
.CharAt(pos
)) && !IsLowerCase(cb
.CharAt(pos
)))
1265 } else if (IsUpperCase(startChar
)) {
1266 while (pos
> 0 && IsUpperCase(cb
.CharAt(pos
)))
1268 if (!IsUpperCase(cb
.CharAt(pos
)))
1270 } else if (IsADigit(startChar
)) {
1271 while (pos
> 0 && IsADigit(cb
.CharAt(pos
)))
1273 if (!IsADigit(cb
.CharAt(pos
)))
1275 } else if (IsPunctuation(startChar
)) {
1276 while (pos
> 0 && IsPunctuation(cb
.CharAt(pos
)))
1278 if (!IsPunctuation(cb
.CharAt(pos
)))
1280 } else if (isspacechar(startChar
)) {
1281 while (pos
> 0 && isspacechar(cb
.CharAt(pos
)))
1283 if (!isspacechar(cb
.CharAt(pos
)))
1285 } else if (!isascii(startChar
)) {
1286 while (pos
> 0 && !isascii(cb
.CharAt(pos
)))
1288 if (isascii(cb
.CharAt(pos
)))
1298 int Document::WordPartRight(int pos
) {
1299 char startChar
= cb
.CharAt(pos
);
1300 int length
= Length();
1301 if (IsWordPartSeparator(startChar
)) {
1302 while (pos
< length
&& IsWordPartSeparator(cb
.CharAt(pos
)))
1304 startChar
= cb
.CharAt(pos
);
1306 if (!isascii(startChar
)) {
1307 while (pos
< length
&& !isascii(cb
.CharAt(pos
)))
1309 } else if (IsLowerCase(startChar
)) {
1310 while (pos
< length
&& IsLowerCase(cb
.CharAt(pos
)))
1312 } else if (IsUpperCase(startChar
)) {
1313 if (IsLowerCase(cb
.CharAt(pos
+ 1))) {
1315 while (pos
< length
&& IsLowerCase(cb
.CharAt(pos
)))
1318 while (pos
< length
&& IsUpperCase(cb
.CharAt(pos
)))
1321 if (IsLowerCase(cb
.CharAt(pos
)) && IsUpperCase(cb
.CharAt(pos
- 1)))
1323 } else if (IsADigit(startChar
)) {
1324 while (pos
< length
&& IsADigit(cb
.CharAt(pos
)))
1326 } else if (IsPunctuation(startChar
)) {
1327 while (pos
< length
&& IsPunctuation(cb
.CharAt(pos
)))
1329 } else if (isspacechar(startChar
)) {
1330 while (pos
< length
&& isspacechar(cb
.CharAt(pos
)))
1338 int Document::ExtendStyleRange(int pos
, int delta
) {
1339 int sStart
= cb
.StyleAt(pos
);
1341 while (pos
> 0 && (cb
.StyleAt(pos
) == sStart
))
1345 while (pos
< (Length()) && (cb
.StyleAt(pos
) == sStart
))