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"
16 #include "SplitVector.h"
17 #include "Partitioning.h"
18 #include "RunStyles.h"
19 #include "CellBuffer.h"
21 #include "CharClassify.h"
22 #include "Decoration.h"
27 using namespace Scintilla
;
30 // This is ASCII specific but is safe with chars >= 0x80
31 static inline bool isspacechar(unsigned char ch
) {
32 return (ch
== ' ') || ((ch
>= 0x09) && (ch
<= 0x0d));
35 static inline bool IsPunctuation(char ch
) {
36 return isascii(ch
) && ispunct(ch
);
39 static inline bool IsADigit(char ch
) {
40 return isascii(ch
) && isdigit(ch
);
43 static inline bool IsLowerCase(char ch
) {
44 return isascii(ch
) && islower(ch
);
47 static inline bool IsUpperCase(char ch
) {
48 return isascii(ch
) && isupper(ch
);
51 Document::Document() {
56 eolMode
= SC_EOL_CRLF
;
60 stylingBitsMask
= 0x1F;
64 enteredModification
= 0;
66 enteredReadOnlyCount
= 0;
69 actualIndentInChars
= 8;
72 backspaceUnindents
= false;
79 perLineData
[ldMarkers
] = new LineMarkers();
80 perLineData
[ldLevels
] = new LineLevels();
81 perLineData
[ldState
] = new LineState();
82 perLineData
[ldMargin
] = new LineAnnotation();
83 perLineData
[ldAnnotation
] = new LineAnnotation();
88 Document::~Document() {
89 for (int i
= 0; i
< lenWatchers
; i
++) {
90 watchers
[i
].watcher
->NotifyDeleted(this, watchers
[i
].userData
);
93 for (int j
=0; j
<ldSize
; j
++) {
94 delete perLineData
[j
];
103 void Document::Init() {
104 for (int j
=0; j
<ldSize
; j
++) {
106 perLineData
[j
]->Init();
110 void Document::InsertLine(int line
) {
111 for (int j
=0; j
<ldSize
; j
++) {
113 perLineData
[j
]->InsertLine(line
);
117 void Document::RemoveLine(int line
) {
118 for (int j
=0; j
<ldSize
; j
++) {
120 perLineData
[j
]->RemoveLine(line
);
124 // Increase reference count and return its previous value.
125 int Document::AddRef() {
129 // Decrease reference count and return its previous value.
130 // Delete the document if reference count reaches zero.
131 int Document::Release() {
132 int curRefCount
= --refCount
;
133 if (curRefCount
== 0)
138 void Document::SetSavePoint() {
140 NotifySavePoint(true);
143 int Document::GetMark(int line
) {
144 return static_cast<LineMarkers
*>(perLineData
[ldMarkers
])->MarkValue(line
);
147 int Document::AddMark(int line
, int markerNum
) {
148 if (line
<= LinesTotal()) {
149 int prev
= static_cast<LineMarkers
*>(perLineData
[ldMarkers
])->
150 AddMark(line
, markerNum
, LinesTotal());
151 DocModification
mh(SC_MOD_CHANGEMARKER
, LineStart(line
), 0, 0, 0, line
);
159 void Document::AddMarkSet(int line
, int valueSet
) {
160 unsigned int m
= valueSet
;
161 for (int i
= 0; m
; i
++, m
>>= 1)
163 static_cast<LineMarkers
*>(perLineData
[ldMarkers
])->
164 AddMark(line
, i
, LinesTotal());
165 DocModification
mh(SC_MOD_CHANGEMARKER
, LineStart(line
), 0, 0, 0, line
);
169 void Document::DeleteMark(int line
, int markerNum
) {
170 static_cast<LineMarkers
*>(perLineData
[ldMarkers
])->DeleteMark(line
, markerNum
, false);
171 DocModification
mh(SC_MOD_CHANGEMARKER
, LineStart(line
), 0, 0, 0, line
);
175 void Document::DeleteMarkFromHandle(int markerHandle
) {
176 static_cast<LineMarkers
*>(perLineData
[ldMarkers
])->DeleteMarkFromHandle(markerHandle
);
177 DocModification
mh(SC_MOD_CHANGEMARKER
, 0, 0, 0, 0);
182 void Document::DeleteAllMarks(int markerNum
) {
183 for (int line
= 0; line
< LinesTotal(); line
++) {
184 static_cast<LineMarkers
*>(perLineData
[ldMarkers
])->DeleteMark(line
, markerNum
, true);
186 DocModification
mh(SC_MOD_CHANGEMARKER
, 0, 0, 0, 0);
191 int Document::LineFromHandle(int markerHandle
) {
192 return static_cast<LineMarkers
*>(perLineData
[ldMarkers
])->LineFromHandle(markerHandle
);
195 int Document::LineStart(int line
) const {
196 return cb
.LineStart(line
);
199 int Document::LineEnd(int line
) const {
200 if (line
== LinesTotal() - 1) {
201 return LineStart(line
+ 1);
203 int position
= LineStart(line
+ 1) - 1;
204 // When line terminator is CR+LF, may need to go back one more
205 if ((position
> LineStart(line
)) && (cb
.CharAt(position
- 1) == '\r')) {
212 int Document::LineFromPosition(int pos
) const {
213 return cb
.LineFromPosition(pos
);
216 int Document::LineEndPosition(int position
) const {
217 return LineEnd(LineFromPosition(position
));
220 int Document::VCHomePosition(int position
) const {
221 int line
= LineFromPosition(position
);
222 int startPosition
= LineStart(line
);
223 int endLine
= LineEnd(line
);
224 int startText
= startPosition
;
225 while (startText
< endLine
&& (cb
.CharAt(startText
) == ' ' || cb
.CharAt(startText
) == '\t' ) )
227 if (position
== startText
)
228 return startPosition
;
233 int Document::SetLevel(int line
, int level
) {
234 int prev
= static_cast<LineLevels
*>(perLineData
[ldLevels
])->SetLevel(line
, level
, LinesTotal());
236 DocModification
mh(SC_MOD_CHANGEFOLD
| SC_MOD_CHANGEMARKER
,
237 LineStart(line
), 0, 0, 0, line
);
238 mh
.foldLevelNow
= level
;
239 mh
.foldLevelPrev
= prev
;
245 int Document::GetLevel(int line
) {
246 return static_cast<LineLevels
*>(perLineData
[ldLevels
])->GetLevel(line
);
249 void Document::ClearLevels() {
250 static_cast<LineLevels
*>(perLineData
[ldLevels
])->ClearLevels();
253 static bool IsSubordinate(int levelStart
, int levelTry
) {
254 if (levelTry
& SC_FOLDLEVELWHITEFLAG
)
257 return (levelStart
& SC_FOLDLEVELNUMBERMASK
) < (levelTry
& SC_FOLDLEVELNUMBERMASK
);
260 int Document::GetLastChild(int lineParent
, int level
) {
262 level
= GetLevel(lineParent
) & SC_FOLDLEVELNUMBERMASK
;
263 int maxLine
= LinesTotal();
264 int lineMaxSubord
= lineParent
;
265 while (lineMaxSubord
< maxLine
- 1) {
266 EnsureStyledTo(LineStart(lineMaxSubord
+ 2));
267 if (!IsSubordinate(level
, GetLevel(lineMaxSubord
+ 1)))
271 if (lineMaxSubord
> lineParent
) {
272 if (level
> (GetLevel(lineMaxSubord
+ 1) & SC_FOLDLEVELNUMBERMASK
)) {
273 // Have chewed up some whitespace that belongs to a parent so seek back
274 if (GetLevel(lineMaxSubord
) & SC_FOLDLEVELWHITEFLAG
) {
279 return lineMaxSubord
;
282 int Document::GetFoldParent(int line
) {
283 int level
= GetLevel(line
) & SC_FOLDLEVELNUMBERMASK
;
284 int lineLook
= line
- 1;
285 while ((lineLook
> 0) && (
286 (!(GetLevel(lineLook
) & SC_FOLDLEVELHEADERFLAG
)) ||
287 ((GetLevel(lineLook
) & SC_FOLDLEVELNUMBERMASK
) >= level
))
291 if ((GetLevel(lineLook
) & SC_FOLDLEVELHEADERFLAG
) &&
292 ((GetLevel(lineLook
) & SC_FOLDLEVELNUMBERMASK
) < level
)) {
299 int Document::ClampPositionIntoDocument(int pos
) {
300 return Platform::Clamp(pos
, 0, Length());
303 bool Document::IsCrLf(int pos
) {
306 if (pos
>= (Length() - 1))
308 return (cb
.CharAt(pos
) == '\r') && (cb
.CharAt(pos
+ 1) == '\n');
311 static const int maxBytesInDBCSCharacter
=5;
313 int Document::LenChar(int pos
) {
316 } else if (IsCrLf(pos
)) {
318 } else if (SC_CP_UTF8
== dbcsCodePage
) {
319 unsigned char ch
= static_cast<unsigned char>(cb
.CharAt(pos
));
323 if (ch
>= (0x80 + 0x40 + 0x20 + 0x10))
325 else if (ch
>= (0x80 + 0x40 + 0x20))
327 int lengthDoc
= Length();
328 if ((pos
+ len
) > lengthDoc
)
329 return lengthDoc
-pos
;
332 } else if (dbcsCodePage
) {
333 char mbstr
[maxBytesInDBCSCharacter
+1];
335 for (i
=0; i
<Platform::DBCSCharMaxLength(); i
++) {
336 mbstr
[i
] = cb
.CharAt(pos
+i
);
339 return Platform::DBCSCharLength(dbcsCodePage
, mbstr
);
345 static bool IsTrailByte(int ch
) {
346 return (ch
>= 0x80) && (ch
< (0x80 + 0x40));
349 static int BytesFromLead(int leadByte
) {
350 if (leadByte
> 0xF4) {
351 // Characters longer than 4 bytes not possible in current UTF-8
353 } else if (leadByte
>= 0xF0) {
355 } else if (leadByte
>= 0xE0) {
357 } else if (leadByte
>= 0xC2) {
363 bool Document::InGoodUTF8(int pos
, int &start
, int &end
) {
365 while ((lead
>0) && (pos
-lead
< 4) && IsTrailByte(static_cast<unsigned char>(cb
.CharAt(lead
-1))))
371 int leadByte
= static_cast<unsigned char>(cb
.CharAt(start
));
372 int bytes
= BytesFromLead(leadByte
);
376 int trailBytes
= bytes
- 1;
377 int len
= pos
- lead
+ 1;
378 if (len
> trailBytes
)
379 // pos too far from lead
381 // Check that there are enough trails for this lead
383 while ((trail
-lead
<trailBytes
) && (trail
< Length())) {
384 if (!IsTrailByte(static_cast<unsigned char>(cb
.CharAt(trail
)))) {
394 // Normalise a position so that it is not halfway through a two byte character.
395 // This can occur in two situations -
396 // When lines are terminated with \r\n pairs which should be treated as one character.
397 // When displaying DBCS text such as Japanese.
398 // If moving, move the position in the indicated direction.
399 int Document::MovePositionOutsideChar(int pos
, int moveDir
, bool checkLineEnd
) {
400 //Platform::DebugPrintf("NoCRLF %d %d\n", pos, moveDir);
401 // If out of range, just return minimum/maximum value.
407 // PLATFORM_ASSERT(pos > 0 && pos < Length());
408 if (checkLineEnd
&& IsCrLf(pos
- 1)) {
415 // Not between CR and LF
418 if (SC_CP_UTF8
== dbcsCodePage
) {
419 unsigned char ch
= static_cast<unsigned char>(cb
.CharAt(pos
));
422 if (IsTrailByte(ch
) && InGoodUTF8(pos
, startUTF
, endUTF
)) {
423 // ch is a trail byte within a UTF-8 character
430 // Anchor DBCS calculations at start of line because start of line can
431 // not be a DBCS trail byte.
432 int posCheck
= LineStart(LineFromPosition(pos
));
433 while (posCheck
< pos
) {
434 char mbstr
[maxBytesInDBCSCharacter
+1];
436 for(i
=0;i
<Platform::DBCSCharMaxLength();i
++) {
437 mbstr
[i
] = cb
.CharAt(posCheck
+i
);
441 int mbsize
= Platform::DBCSCharLength(dbcsCodePage
, mbstr
);
442 if (posCheck
+ mbsize
== pos
) {
444 } else if (posCheck
+ mbsize
> pos
) {
446 return posCheck
+ mbsize
;
459 void Document::ModifiedAt(int pos
) {
464 void Document::CheckReadOnly() {
465 if (cb
.IsReadOnly() && enteredReadOnlyCount
== 0) {
466 enteredReadOnlyCount
++;
467 NotifyModifyAttempt();
468 enteredReadOnlyCount
--;
472 // Document only modified by gateways DeleteChars, InsertString, Undo, Redo, and SetStyleAt.
473 // SetStyleAt does not change the persistent state of a document
475 bool Document::DeleteChars(int pos
, int len
) {
478 if ((pos
+ len
) > Length())
481 if (enteredModification
!= 0) {
484 enteredModification
++;
485 if (!cb
.IsReadOnly()) {
488 SC_MOD_BEFOREDELETE
| SC_PERFORMED_USER
,
491 int prevLinesTotal
= LinesTotal();
492 bool startSavePoint
= cb
.IsSavePoint();
493 bool startSequence
= false;
494 const char *text
= cb
.DeleteChars(pos
, len
, startSequence
);
495 if (startSavePoint
&& cb
.IsCollectingUndo())
496 NotifySavePoint(!startSavePoint
);
497 if ((pos
< Length()) || (pos
== 0))
503 SC_MOD_DELETETEXT
| SC_PERFORMED_USER
| (startSequence
?SC_STARTACTION
:0),
505 LinesTotal() - prevLinesTotal
, text
));
507 enteredModification
--;
509 return !cb
.IsReadOnly();
513 * Insert a string with a length.
515 bool Document::InsertString(int position
, const char *s
, int insertLength
) {
516 if (insertLength
<= 0) {
520 if (enteredModification
!= 0) {
523 enteredModification
++;
524 if (!cb
.IsReadOnly()) {
527 SC_MOD_BEFOREINSERT
| SC_PERFORMED_USER
,
528 position
, insertLength
,
530 int prevLinesTotal
= LinesTotal();
531 bool startSavePoint
= cb
.IsSavePoint();
532 bool startSequence
= false;
533 const char *text
= cb
.InsertString(position
, s
, insertLength
, startSequence
);
534 if (startSavePoint
&& cb
.IsCollectingUndo())
535 NotifySavePoint(!startSavePoint
);
536 ModifiedAt(position
);
539 SC_MOD_INSERTTEXT
| SC_PERFORMED_USER
| (startSequence
?SC_STARTACTION
:0),
540 position
, insertLength
,
541 LinesTotal() - prevLinesTotal
, text
));
543 enteredModification
--;
545 return !cb
.IsReadOnly();
548 int Document::Undo() {
551 if (enteredModification
== 0) {
552 enteredModification
++;
553 if (!cb
.IsReadOnly()) {
554 bool startSavePoint
= cb
.IsSavePoint();
555 bool multiLine
= false;
556 int steps
= cb
.StartUndo();
557 //Platform::DebugPrintf("Steps=%d\n", steps);
558 for (int step
= 0; step
< steps
; step
++) {
559 const int prevLinesTotal
= LinesTotal();
560 const Action
&action
= cb
.GetUndoStep();
561 if (action
.at
== removeAction
) {
562 NotifyModified(DocModification(
563 SC_MOD_BEFOREINSERT
| SC_PERFORMED_UNDO
, action
));
564 } else if (action
.at
== containerAction
) {
565 DocModification
dm(SC_MOD_CONTAINER
| SC_PERFORMED_UNDO
);
566 dm
.token
= action
.position
;
569 NotifyModified(DocModification(
570 SC_MOD_BEFOREDELETE
| SC_PERFORMED_UNDO
, action
));
572 cb
.PerformUndoStep();
573 int cellPosition
= action
.position
;
574 if (action
.at
!= containerAction
) {
575 ModifiedAt(cellPosition
);
576 newPos
= cellPosition
;
579 int modFlags
= SC_PERFORMED_UNDO
;
580 // With undo, an insertion action becomes a deletion notification
581 if (action
.at
== removeAction
) {
582 newPos
+= action
.lenData
;
583 modFlags
|= SC_MOD_INSERTTEXT
;
584 } else if (action
.at
== insertAction
) {
585 modFlags
|= SC_MOD_DELETETEXT
;
588 modFlags
|= SC_MULTISTEPUNDOREDO
;
589 const int linesAdded
= LinesTotal() - prevLinesTotal
;
592 if (step
== steps
- 1) {
593 modFlags
|= SC_LASTSTEPINUNDOREDO
;
595 modFlags
|= SC_MULTILINEUNDOREDO
;
597 NotifyModified(DocModification(modFlags
, cellPosition
, action
.lenData
,
598 linesAdded
, action
.data
));
601 bool endSavePoint
= cb
.IsSavePoint();
602 if (startSavePoint
!= endSavePoint
)
603 NotifySavePoint(endSavePoint
);
605 enteredModification
--;
610 int Document::Redo() {
613 if (enteredModification
== 0) {
614 enteredModification
++;
615 if (!cb
.IsReadOnly()) {
616 bool startSavePoint
= cb
.IsSavePoint();
617 bool multiLine
= false;
618 int steps
= cb
.StartRedo();
619 for (int step
= 0; step
< steps
; step
++) {
620 const int prevLinesTotal
= LinesTotal();
621 const Action
&action
= cb
.GetRedoStep();
622 if (action
.at
== insertAction
) {
623 NotifyModified(DocModification(
624 SC_MOD_BEFOREINSERT
| SC_PERFORMED_REDO
, action
));
625 } else if (action
.at
== containerAction
) {
626 DocModification
dm(SC_MOD_CONTAINER
| SC_PERFORMED_REDO
);
627 dm
.token
= action
.position
;
630 NotifyModified(DocModification(
631 SC_MOD_BEFOREDELETE
| SC_PERFORMED_REDO
, action
));
633 cb
.PerformRedoStep();
634 if (action
.at
!= containerAction
) {
635 ModifiedAt(action
.position
);
636 newPos
= action
.position
;
639 int modFlags
= SC_PERFORMED_REDO
;
640 if (action
.at
== insertAction
) {
641 newPos
+= action
.lenData
;
642 modFlags
|= SC_MOD_INSERTTEXT
;
643 } else if (action
.at
== removeAction
) {
644 modFlags
|= SC_MOD_DELETETEXT
;
647 modFlags
|= SC_MULTISTEPUNDOREDO
;
648 const int linesAdded
= LinesTotal() - prevLinesTotal
;
651 if (step
== steps
- 1) {
652 modFlags
|= SC_LASTSTEPINUNDOREDO
;
654 modFlags
|= SC_MULTILINEUNDOREDO
;
657 DocModification(modFlags
, action
.position
, action
.lenData
,
658 linesAdded
, action
.data
));
661 bool endSavePoint
= cb
.IsSavePoint();
662 if (startSavePoint
!= endSavePoint
)
663 NotifySavePoint(endSavePoint
);
665 enteredModification
--;
671 * Insert a single character.
673 bool Document::InsertChar(int pos
, char ch
) {
676 return InsertString(pos
, chs
, 1);
680 * Insert a null terminated string.
682 bool Document::InsertCString(int position
, const char *s
) {
683 return InsertString(position
, s
, strlen(s
));
686 void Document::ChangeChar(int pos
, char ch
) {
691 void Document::DelChar(int pos
) {
692 DeleteChars(pos
, LenChar(pos
));
695 void Document::DelCharBack(int pos
) {
698 } else if (IsCrLf(pos
- 2)) {
699 DeleteChars(pos
- 2, 2);
700 } else if (dbcsCodePage
) {
701 int startChar
= MovePositionOutsideChar(pos
- 1, -1, false);
702 DeleteChars(startChar
, pos
- startChar
);
704 DeleteChars(pos
- 1, 1);
708 static bool isindentchar(char ch
) {
709 return (ch
== ' ') || (ch
== '\t');
712 static int NextTab(int pos
, int tabSize
) {
713 return ((pos
/ tabSize
) + 1) * tabSize
;
716 static void CreateIndentation(char *linebuf
, int length
, int indent
, int tabSize
, bool insertSpaces
) {
717 length
--; // ensure space for \0
719 while ((indent
>= tabSize
) && (length
> 0)) {
725 while ((indent
> 0) && (length
> 0)) {
733 int Document::GetLineIndentation(int line
) {
735 if ((line
>= 0) && (line
< LinesTotal())) {
736 int lineStart
= LineStart(line
);
737 int length
= Length();
738 for (int i
= lineStart
;i
< length
;i
++) {
739 char ch
= cb
.CharAt(i
);
743 indent
= NextTab(indent
, tabInChars
);
751 void Document::SetLineIndentation(int line
, int indent
) {
752 int indentOfLine
= GetLineIndentation(line
);
755 if (indent
!= indentOfLine
) {
757 CreateIndentation(linebuf
, sizeof(linebuf
), indent
, tabInChars
, !useTabs
);
758 int thisLineStart
= LineStart(line
);
759 int indentPos
= GetLineIndentPosition(line
);
761 DeleteChars(thisLineStart
, indentPos
- thisLineStart
);
762 InsertCString(thisLineStart
, linebuf
);
767 int Document::GetLineIndentPosition(int line
) const {
770 int pos
= LineStart(line
);
771 int length
= Length();
772 while ((pos
< length
) && isindentchar(cb
.CharAt(pos
))) {
778 int Document::GetColumn(int pos
) {
780 int line
= LineFromPosition(pos
);
781 if ((line
>= 0) && (line
< LinesTotal())) {
782 for (int i
= LineStart(line
);i
< pos
;) {
783 char ch
= cb
.CharAt(i
);
785 column
= NextTab(column
, tabInChars
);
787 } else if (ch
== '\r') {
789 } else if (ch
== '\n') {
791 } else if (i
>= Length()) {
795 i
= MovePositionOutsideChar(i
+ 1, 1, false);
802 int Document::FindColumn(int line
, int column
) {
803 int position
= LineStart(line
);
804 int columnCurrent
= 0;
805 if ((line
>= 0) && (line
< LinesTotal())) {
806 while ((columnCurrent
< column
) && (position
< Length())) {
807 char ch
= cb
.CharAt(position
);
809 columnCurrent
= NextTab(columnCurrent
, tabInChars
);
811 } else if (ch
== '\r') {
813 } else if (ch
== '\n') {
817 position
= MovePositionOutsideChar(position
+ 1, 1, false);
824 void Document::Indent(bool forwards
, int lineBottom
, int lineTop
) {
825 // Dedent - suck white space off the front of the line to dedent by equivalent of a tab
826 for (int line
= lineBottom
; line
>= lineTop
; line
--) {
827 int indentOfLine
= GetLineIndentation(line
);
829 if (LineStart(line
) < LineEnd(line
)) {
830 SetLineIndentation(line
, indentOfLine
+ IndentSize());
833 SetLineIndentation(line
, indentOfLine
- IndentSize());
838 // Convert line endings for a piece of text to a particular mode.
839 // Stop at len or when a NUL is found.
840 // Caller must delete the returned pointer.
841 char *Document::TransformLineEnds(int *pLenOut
, const char *s
, size_t len
, int eolMode
) {
842 char *dest
= new char[2 * len
+ 1];
843 const char *sptr
= s
;
845 for (size_t i
= 0; (i
< len
) && (*sptr
!= '\0'); i
++) {
846 if (*sptr
== '\n' || *sptr
== '\r') {
847 if (eolMode
== SC_EOL_CR
) {
849 } else if (eolMode
== SC_EOL_LF
) {
851 } else { // eolMode == SC_EOL_CRLF
855 if ((*sptr
== '\r') && (i
+1 < len
) && (*(sptr
+1) == '\n')) {
865 *pLenOut
= (dptr
- dest
) - 1;
869 void Document::ConvertLineEnds(int eolModeSet
) {
872 for (int pos
= 0; pos
< Length(); pos
++) {
873 if (cb
.CharAt(pos
) == '\r') {
874 if (cb
.CharAt(pos
+ 1) == '\n') {
876 if (eolModeSet
== SC_EOL_CR
) {
877 DeleteChars(pos
+ 1, 1); // Delete the LF
878 } else if (eolModeSet
== SC_EOL_LF
) {
879 DeleteChars(pos
, 1); // Delete the CR
885 if (eolModeSet
== SC_EOL_CRLF
) {
886 InsertString(pos
+ 1, "\n", 1); // Insert LF
888 } else if (eolModeSet
== SC_EOL_LF
) {
889 InsertString(pos
, "\n", 1); // Insert LF
890 DeleteChars(pos
+ 1, 1); // Delete CR
893 } else if (cb
.CharAt(pos
) == '\n') {
895 if (eolModeSet
== SC_EOL_CRLF
) {
896 InsertString(pos
, "\r", 1); // Insert CR
898 } else if (eolModeSet
== SC_EOL_CR
) {
899 InsertString(pos
, "\r", 1); // Insert CR
900 DeleteChars(pos
+ 1, 1); // Delete LF
908 bool Document::IsWhiteLine(int line
) const {
909 int currentChar
= LineStart(line
);
910 int endLine
= LineEnd(line
);
911 while (currentChar
< endLine
) {
912 if (cb
.CharAt(currentChar
) != ' ' && cb
.CharAt(currentChar
) != '\t') {
920 int Document::ParaUp(int pos
) {
921 int line
= LineFromPosition(pos
);
923 while (line
>= 0 && IsWhiteLine(line
)) { // skip empty lines
926 while (line
>= 0 && !IsWhiteLine(line
)) { // skip non-empty lines
930 return LineStart(line
);
933 int Document::ParaDown(int pos
) {
934 int line
= LineFromPosition(pos
);
935 while (line
< LinesTotal() && !IsWhiteLine(line
)) { // skip non-empty lines
938 while (line
< LinesTotal() && IsWhiteLine(line
)) { // skip empty lines
941 if (line
< LinesTotal())
942 return LineStart(line
);
943 else // end of a document
944 return LineEnd(line
-1);
947 CharClassify::cc
Document::WordCharClass(unsigned char ch
) {
948 if ((SC_CP_UTF8
== dbcsCodePage
) && (ch
>= 0x80))
949 return CharClassify::ccWord
;
950 return charClass
.GetClass(ch
);
954 * Used by commmands that want to select whole words.
955 * Finds the start of word at pos when delta < 0 or the end of the word when delta >= 0.
957 int Document::ExtendWordSelect(int pos
, int delta
, bool onlyWordCharacters
) {
958 CharClassify::cc ccStart
= CharClassify::ccWord
;
960 if (!onlyWordCharacters
)
961 ccStart
= WordCharClass(cb
.CharAt(pos
-1));
962 while (pos
> 0 && (WordCharClass(cb
.CharAt(pos
- 1)) == ccStart
))
965 if (!onlyWordCharacters
&& pos
< Length())
966 ccStart
= WordCharClass(cb
.CharAt(pos
));
967 while (pos
< (Length()) && (WordCharClass(cb
.CharAt(pos
)) == ccStart
))
970 return MovePositionOutsideChar(pos
, delta
);
974 * Find the start of the next word in either a forward (delta >= 0) or backwards direction
976 * This is looking for a transition between character classes although there is also some
977 * additional movement to transit white space.
978 * Used by cursor movement by word commands.
980 int Document::NextWordStart(int pos
, int delta
) {
982 while (pos
> 0 && (WordCharClass(cb
.CharAt(pos
- 1)) == CharClassify::ccSpace
))
985 CharClassify::cc ccStart
= WordCharClass(cb
.CharAt(pos
-1));
986 while (pos
> 0 && (WordCharClass(cb
.CharAt(pos
- 1)) == ccStart
)) {
991 CharClassify::cc ccStart
= WordCharClass(cb
.CharAt(pos
));
992 while (pos
< (Length()) && (WordCharClass(cb
.CharAt(pos
)) == ccStart
))
994 while (pos
< (Length()) && (WordCharClass(cb
.CharAt(pos
)) == CharClassify::ccSpace
))
1001 * Find the end of the next word in either a forward (delta >= 0) or backwards direction
1003 * This is looking for a transition between character classes although there is also some
1004 * additional movement to transit white space.
1005 * Used by cursor movement by word commands.
1007 int Document::NextWordEnd(int pos
, int delta
) {
1010 CharClassify::cc ccStart
= WordCharClass(cb
.CharAt(pos
-1));
1011 if (ccStart
!= CharClassify::ccSpace
) {
1012 while (pos
> 0 && WordCharClass(cb
.CharAt(pos
- 1)) == ccStart
) {
1016 while (pos
> 0 && WordCharClass(cb
.CharAt(pos
- 1)) == CharClassify::ccSpace
) {
1021 while (pos
< Length() && WordCharClass(cb
.CharAt(pos
)) == CharClassify::ccSpace
) {
1024 if (pos
< Length()) {
1025 CharClassify::cc ccStart
= WordCharClass(cb
.CharAt(pos
));
1026 while (pos
< Length() && WordCharClass(cb
.CharAt(pos
)) == ccStart
) {
1035 * Check that the character at the given position is a word or punctuation character and that
1036 * the previous character is of a different character class.
1038 bool Document::IsWordStartAt(int pos
) {
1040 CharClassify::cc ccPos
= WordCharClass(CharAt(pos
));
1041 return (ccPos
== CharClassify::ccWord
|| ccPos
== CharClassify::ccPunctuation
) &&
1042 (ccPos
!= WordCharClass(CharAt(pos
- 1)));
1048 * Check that the character at the given position is a word or punctuation character and that
1049 * the next character is of a different character class.
1051 bool Document::IsWordEndAt(int pos
) {
1052 if (pos
< Length()) {
1053 CharClassify::cc ccPrev
= WordCharClass(CharAt(pos
-1));
1054 return (ccPrev
== CharClassify::ccWord
|| ccPrev
== CharClassify::ccPunctuation
) &&
1055 (ccPrev
!= WordCharClass(CharAt(pos
)));
1061 * Check that the given range is has transitions between character classes at both
1062 * ends and where the characters on the inside are word or punctuation characters.
1064 bool Document::IsWordAt(int start
, int end
) {
1065 return IsWordStartAt(start
) && IsWordEndAt(end
);
1068 // The comparison and case changing functions here assume ASCII
1069 // or extended ASCII such as the normal Windows code page.
1071 static inline char MakeUpperCase(char ch
) {
1072 if (ch
< 'a' || ch
> 'z')
1075 return static_cast<char>(ch
- 'a' + 'A');
1078 static inline char MakeLowerCase(char ch
) {
1079 if (ch
< 'A' || ch
> 'Z')
1082 return static_cast<char>(ch
- 'A' + 'a');
1086 * Find text in document, supporting both forward and backward
1087 * searches (just pass minPos > maxPos to do a backward search)
1088 * Has not been tested with backwards DBCS searches yet.
1090 long Document::FindText(int minPos
, int maxPos
, const char *s
,
1091 bool caseSensitive
, bool word
, bool wordStart
, bool regExp
, int flags
,
1095 regex
= CreateRegexSearch(&charClass
);
1096 return regex
->FindText(this, minPos
, maxPos
, s
, caseSensitive
, word
, wordStart
, flags
, length
);
1099 bool forward
= minPos
<= maxPos
;
1100 int increment
= forward
? 1 : -1;
1102 // Range endpoints should not be inside DBCS characters, but just in case, move them.
1103 int startPos
= MovePositionOutsideChar(minPos
, increment
, false);
1104 int endPos
= MovePositionOutsideChar(maxPos
, increment
, false);
1106 // Compute actual search ranges needed
1107 int lengthFind
= *length
;
1108 if (lengthFind
== -1)
1109 lengthFind
= static_cast<int>(strlen(s
));
1110 int endSearch
= endPos
;
1111 if (startPos
<= endPos
) {
1112 endSearch
= endPos
- lengthFind
+ 1;
1114 //Platform::DebugPrintf("Find %d %d %s %d\n", startPos, endPos, ft->lpstrText, lengthFind);
1115 char firstChar
= s
[0];
1117 firstChar
= static_cast<char>(MakeUpperCase(firstChar
));
1118 int pos
= forward
? startPos
: (startPos
- 1);
1119 while (forward
? (pos
< endSearch
) : (pos
>= endSearch
)) {
1120 char ch
= CharAt(pos
);
1121 if (caseSensitive
) {
1122 if (ch
== firstChar
) {
1124 if (pos
+ lengthFind
> Platform::Maximum(startPos
, endPos
)) found
= false;
1125 for (int posMatch
= 1; posMatch
< lengthFind
&& found
; posMatch
++) {
1126 ch
= CharAt(pos
+ posMatch
);
1127 if (ch
!= s
[posMatch
])
1131 if ((!word
&& !wordStart
) ||
1132 (word
&& IsWordAt(pos
, pos
+ lengthFind
)) ||
1133 (wordStart
&& IsWordStartAt(pos
)))
1138 if (MakeUpperCase(ch
) == firstChar
) {
1140 if (pos
+ lengthFind
> Platform::Maximum(startPos
, endPos
)) found
= false;
1141 for (int posMatch
= 1; posMatch
< lengthFind
&& found
; posMatch
++) {
1142 ch
= CharAt(pos
+ posMatch
);
1143 if (MakeUpperCase(ch
) != MakeUpperCase(s
[posMatch
]))
1147 if ((!word
&& !wordStart
) ||
1148 (word
&& IsWordAt(pos
, pos
+ lengthFind
)) ||
1149 (wordStart
&& IsWordStartAt(pos
)))
1155 if (dbcsCodePage
&& (pos
>= 0)) {
1156 // Ensure trying to match from start of character
1157 pos
= MovePositionOutsideChar(pos
, increment
, false);
1161 //Platform::DebugPrintf("Not found\n");
1165 const char *Document::SubstituteByPosition(const char *text
, int *length
) {
1166 return regex
->SubstituteByPosition(this, text
, length
);
1169 int Document::LinesTotal() const {
1173 void Document::ChangeCase(Range r
, bool makeUpperCase
) {
1174 for (int pos
= r
.start
; pos
< r
.end
;) {
1175 int len
= LenChar(pos
);
1177 char ch
= CharAt(pos
);
1178 if (makeUpperCase
) {
1179 if (IsLowerCase(ch
)) {
1180 ChangeChar(pos
, static_cast<char>(MakeUpperCase(ch
)));
1183 if (IsUpperCase(ch
)) {
1184 ChangeChar(pos
, static_cast<char>(MakeLowerCase(ch
)));
1192 void Document::SetDefaultCharClasses(bool includeWordClass
) {
1193 charClass
.SetDefaultCharClasses(includeWordClass
);
1196 void Document::SetCharClasses(const unsigned char *chars
, CharClassify::cc newCharClass
) {
1197 charClass
.SetCharClasses(chars
, newCharClass
);
1200 void Document::SetStylingBits(int bits
) {
1202 stylingBitsMask
= (1 << stylingBits
) - 1;
1205 void Document::StartStyling(int position
, char mask
) {
1207 endStyled
= position
;
1210 bool Document::SetStyleFor(int length
, char style
) {
1211 if (enteredStyling
!= 0) {
1215 style
&= stylingMask
;
1216 int prevEndStyled
= endStyled
;
1217 if (cb
.SetStyleFor(endStyled
, length
, style
, stylingMask
)) {
1218 DocModification
mh(SC_MOD_CHANGESTYLE
| SC_PERFORMED_USER
,
1219 prevEndStyled
, length
);
1222 endStyled
+= length
;
1228 bool Document::SetStyles(int length
, const char *styles
) {
1229 if (enteredStyling
!= 0) {
1233 bool didChange
= false;
1236 for (int iPos
= 0; iPos
< length
; iPos
++, endStyled
++) {
1237 PLATFORM_ASSERT(endStyled
< Length());
1238 if (cb
.SetStyleAt(endStyled
, styles
[iPos
], stylingMask
)) {
1240 startMod
= endStyled
;
1247 DocModification
mh(SC_MOD_CHANGESTYLE
| SC_PERFORMED_USER
,
1248 startMod
, endMod
- startMod
+ 1);
1256 void Document::EnsureStyledTo(int pos
) {
1257 if ((enteredStyling
== 0) && (pos
> GetEndStyled())) {
1258 IncrementStyleClock();
1259 // Ask the watchers to style, and stop as soon as one responds.
1260 for (int i
= 0; pos
> GetEndStyled() && i
< lenWatchers
; i
++) {
1261 watchers
[i
].watcher
->NotifyStyleNeeded(this, watchers
[i
].userData
, pos
);
1266 int Document::SetLineState(int line
, int state
) {
1267 int statePrevious
= static_cast<LineState
*>(perLineData
[ldState
])->SetLineState(line
, state
);
1268 if (state
!= statePrevious
) {
1269 DocModification
mh(SC_MOD_CHANGELINESTATE
, 0, 0, 0, 0, line
);
1272 return statePrevious
;
1275 int Document::GetLineState(int line
) {
1276 return static_cast<LineState
*>(perLineData
[ldState
])->GetLineState(line
);
1279 int Document::GetMaxLineState() {
1280 return static_cast<LineState
*>(perLineData
[ldState
])->GetMaxLineState();
1283 StyledText
Document::MarginStyledText(int line
) {
1284 LineAnnotation
*pla
= static_cast<LineAnnotation
*>(perLineData
[ldMargin
]);
1285 return StyledText(pla
->Length(line
), pla
->Text(line
),
1286 pla
->MultipleStyles(line
), pla
->Style(line
), pla
->Styles(line
));
1289 void Document::MarginSetText(int line
, const char *text
) {
1290 static_cast<LineAnnotation
*>(perLineData
[ldMargin
])->SetText(line
, text
);
1291 DocModification
mh(SC_MOD_CHANGEMARGIN
, LineStart(line
), 0, 0, 0, line
);
1295 void Document::MarginSetStyle(int line
, int style
) {
1296 static_cast<LineAnnotation
*>(perLineData
[ldMargin
])->SetStyle(line
, style
);
1299 void Document::MarginSetStyles(int line
, const unsigned char *styles
) {
1300 static_cast<LineAnnotation
*>(perLineData
[ldMargin
])->SetStyles(line
, styles
);
1303 int Document::MarginLength(int line
) const {
1304 return static_cast<LineAnnotation
*>(perLineData
[ldMargin
])->Length(line
);
1307 void Document::MarginClearAll() {
1308 int maxEditorLine
= LinesTotal();
1309 for (int l
=0;l
<maxEditorLine
;l
++)
1310 MarginSetText(l
, 0);
1311 // Free remaining data
1312 static_cast<LineAnnotation
*>(perLineData
[ldMargin
])->ClearAll();
1315 bool Document::AnnotationAny() const {
1316 return static_cast<LineAnnotation
*>(perLineData
[ldAnnotation
])->AnySet();
1319 StyledText
Document::AnnotationStyledText(int line
) {
1320 LineAnnotation
*pla
= static_cast<LineAnnotation
*>(perLineData
[ldAnnotation
]);
1321 return StyledText(pla
->Length(line
), pla
->Text(line
),
1322 pla
->MultipleStyles(line
), pla
->Style(line
), pla
->Styles(line
));
1325 void Document::AnnotationSetText(int line
, const char *text
) {
1326 const int linesBefore
= AnnotationLines(line
);
1327 static_cast<LineAnnotation
*>(perLineData
[ldAnnotation
])->SetText(line
, text
);
1328 const int linesAfter
= AnnotationLines(line
);
1329 DocModification
mh(SC_MOD_CHANGEANNOTATION
, LineStart(line
), 0, 0, 0, line
);
1330 mh
.annotationLinesAdded
= linesAfter
- linesBefore
;
1334 void Document::AnnotationSetStyle(int line
, int style
) {
1335 static_cast<LineAnnotation
*>(perLineData
[ldAnnotation
])->SetStyle(line
, style
);
1338 void Document::AnnotationSetStyles(int line
, const unsigned char *styles
) {
1339 static_cast<LineAnnotation
*>(perLineData
[ldAnnotation
])->SetStyles(line
, styles
);
1342 int Document::AnnotationLength(int line
) const {
1343 return static_cast<LineAnnotation
*>(perLineData
[ldAnnotation
])->Length(line
);
1346 int Document::AnnotationLines(int line
) const {
1347 return static_cast<LineAnnotation
*>(perLineData
[ldAnnotation
])->Lines(line
);
1350 void Document::AnnotationClearAll() {
1351 int maxEditorLine
= LinesTotal();
1352 for (int l
=0;l
<maxEditorLine
;l
++)
1353 AnnotationSetText(l
, 0);
1354 // Free remaining data
1355 static_cast<LineAnnotation
*>(perLineData
[ldAnnotation
])->ClearAll();
1358 void Document::IncrementStyleClock() {
1359 styleClock
= (styleClock
+ 1) % 0x100000;
1362 void Document::DecorationFillRange(int position
, int value
, int fillLength
) {
1363 if (decorations
.FillRange(position
, value
, fillLength
)) {
1364 DocModification
mh(SC_MOD_CHANGEINDICATOR
| SC_PERFORMED_USER
,
1365 position
, fillLength
);
1370 bool Document::AddWatcher(DocWatcher
*watcher
, void *userData
) {
1371 for (int i
= 0; i
< lenWatchers
; i
++) {
1372 if ((watchers
[i
].watcher
== watcher
) &&
1373 (watchers
[i
].userData
== userData
))
1376 WatcherWithUserData
*pwNew
= new WatcherWithUserData
[lenWatchers
+ 1];
1379 for (int j
= 0; j
< lenWatchers
; j
++)
1380 pwNew
[j
] = watchers
[j
];
1381 pwNew
[lenWatchers
].watcher
= watcher
;
1382 pwNew
[lenWatchers
].userData
= userData
;
1389 bool Document::RemoveWatcher(DocWatcher
*watcher
, void *userData
) {
1390 for (int i
= 0; i
< lenWatchers
; i
++) {
1391 if ((watchers
[i
].watcher
== watcher
) &&
1392 (watchers
[i
].userData
== userData
)) {
1393 if (lenWatchers
== 1) {
1398 WatcherWithUserData
*pwNew
= new WatcherWithUserData
[lenWatchers
];
1401 for (int j
= 0; j
< lenWatchers
- 1; j
++) {
1402 pwNew
[j
] = (j
< i
) ? watchers
[j
] : watchers
[j
+ 1];
1414 void Document::NotifyModifyAttempt() {
1415 for (int i
= 0; i
< lenWatchers
; i
++) {
1416 watchers
[i
].watcher
->NotifyModifyAttempt(this, watchers
[i
].userData
);
1420 void Document::NotifySavePoint(bool atSavePoint
) {
1421 for (int i
= 0; i
< lenWatchers
; i
++) {
1422 watchers
[i
].watcher
->NotifySavePoint(this, watchers
[i
].userData
, atSavePoint
);
1426 void Document::NotifyModified(DocModification mh
) {
1427 if (mh
.modificationType
& SC_MOD_INSERTTEXT
) {
1428 decorations
.InsertSpace(mh
.position
, mh
.length
);
1429 } else if (mh
.modificationType
& SC_MOD_DELETETEXT
) {
1430 decorations
.DeleteRange(mh
.position
, mh
.length
);
1432 for (int i
= 0; i
< lenWatchers
; i
++) {
1433 watchers
[i
].watcher
->NotifyModified(this, mh
, watchers
[i
].userData
);
1437 bool Document::IsWordPartSeparator(char ch
) {
1438 return (WordCharClass(ch
) == CharClassify::ccWord
) && IsPunctuation(ch
);
1441 int Document::WordPartLeft(int pos
) {
1444 char startChar
= cb
.CharAt(pos
);
1445 if (IsWordPartSeparator(startChar
)) {
1446 while (pos
> 0 && IsWordPartSeparator(cb
.CharAt(pos
))) {
1451 startChar
= cb
.CharAt(pos
);
1453 if (IsLowerCase(startChar
)) {
1454 while (pos
> 0 && IsLowerCase(cb
.CharAt(pos
)))
1456 if (!IsUpperCase(cb
.CharAt(pos
)) && !IsLowerCase(cb
.CharAt(pos
)))
1458 } else if (IsUpperCase(startChar
)) {
1459 while (pos
> 0 && IsUpperCase(cb
.CharAt(pos
)))
1461 if (!IsUpperCase(cb
.CharAt(pos
)))
1463 } else if (IsADigit(startChar
)) {
1464 while (pos
> 0 && IsADigit(cb
.CharAt(pos
)))
1466 if (!IsADigit(cb
.CharAt(pos
)))
1468 } else if (IsPunctuation(startChar
)) {
1469 while (pos
> 0 && IsPunctuation(cb
.CharAt(pos
)))
1471 if (!IsPunctuation(cb
.CharAt(pos
)))
1473 } else if (isspacechar(startChar
)) {
1474 while (pos
> 0 && isspacechar(cb
.CharAt(pos
)))
1476 if (!isspacechar(cb
.CharAt(pos
)))
1478 } else if (!isascii(startChar
)) {
1479 while (pos
> 0 && !isascii(cb
.CharAt(pos
)))
1481 if (isascii(cb
.CharAt(pos
)))
1491 int Document::WordPartRight(int pos
) {
1492 char startChar
= cb
.CharAt(pos
);
1493 int length
= Length();
1494 if (IsWordPartSeparator(startChar
)) {
1495 while (pos
< length
&& IsWordPartSeparator(cb
.CharAt(pos
)))
1497 startChar
= cb
.CharAt(pos
);
1499 if (!isascii(startChar
)) {
1500 while (pos
< length
&& !isascii(cb
.CharAt(pos
)))
1502 } else if (IsLowerCase(startChar
)) {
1503 while (pos
< length
&& IsLowerCase(cb
.CharAt(pos
)))
1505 } else if (IsUpperCase(startChar
)) {
1506 if (IsLowerCase(cb
.CharAt(pos
+ 1))) {
1508 while (pos
< length
&& IsLowerCase(cb
.CharAt(pos
)))
1511 while (pos
< length
&& IsUpperCase(cb
.CharAt(pos
)))
1514 if (IsLowerCase(cb
.CharAt(pos
)) && IsUpperCase(cb
.CharAt(pos
- 1)))
1516 } else if (IsADigit(startChar
)) {
1517 while (pos
< length
&& IsADigit(cb
.CharAt(pos
)))
1519 } else if (IsPunctuation(startChar
)) {
1520 while (pos
< length
&& IsPunctuation(cb
.CharAt(pos
)))
1522 } else if (isspacechar(startChar
)) {
1523 while (pos
< length
&& isspacechar(cb
.CharAt(pos
)))
1531 bool IsLineEndChar(char c
) {
1532 return (c
== '\n' || c
== '\r');
1535 int Document::ExtendStyleRange(int pos
, int delta
, bool singleLine
) {
1536 int sStart
= cb
.StyleAt(pos
);
1538 while (pos
> 0 && (cb
.StyleAt(pos
) == sStart
) && (!singleLine
|| !IsLineEndChar(cb
.CharAt(pos
))) )
1542 while (pos
< (Length()) && (cb
.StyleAt(pos
) == sStart
) && (!singleLine
|| !IsLineEndChar(cb
.CharAt(pos
))) )
1548 static char BraceOpposite(char ch
) {
1571 // TODO: should be able to extend styled region to find matching brace
1572 int Document::BraceMatch(int position
, int /*maxReStyle*/) {
1573 char chBrace
= CharAt(position
);
1574 char chSeek
= BraceOpposite(chBrace
);
1577 char styBrace
= static_cast<char>(StyleAt(position
) & stylingBitsMask
);
1579 if (chBrace
== '(' || chBrace
== '[' || chBrace
== '{' || chBrace
== '<')
1582 position
= position
+ direction
;
1583 while ((position
>= 0) && (position
< Length())) {
1584 position
= MovePositionOutsideChar(position
, direction
);
1585 char chAtPos
= CharAt(position
);
1586 char styAtPos
= static_cast<char>(StyleAt(position
) & stylingBitsMask
);
1587 if ((position
> GetEndStyled()) || (styAtPos
== styBrace
)) {
1588 if (chAtPos
== chBrace
)
1590 if (chAtPos
== chSeek
)
1595 position
= position
+ direction
;
1601 * Implementation of RegexSearchBase for the default built-in regular expression engine
1603 class BuiltinRegex
: public RegexSearchBase
{
1605 BuiltinRegex(CharClassify
*charClassTable
) : search(charClassTable
), substituted(NULL
) {}
1607 virtual ~BuiltinRegex() {
1611 virtual long FindText(Document
*doc
, int minPos
, int maxPos
, const char *s
,
1612 bool caseSensitive
, bool word
, bool wordStart
, int flags
,
1615 virtual const char *SubstituteByPosition(Document
* doc
, const char *text
, int *length
);
1622 // Define a way for the Regular Expression code to access the document
1623 class DocumentIndexer
: public CharacterIndexer
{
1627 DocumentIndexer(Document
*pdoc_
, int end_
) :
1628 pdoc(pdoc_
), end(end_
) {
1631 virtual ~DocumentIndexer() {
1634 virtual char CharAt(int index
) {
1635 if (index
< 0 || index
>= end
)
1638 return pdoc
->CharAt(index
);
1642 long BuiltinRegex::FindText(Document
*doc
, int minPos
, int maxPos
, const char *s
,
1643 bool caseSensitive
, bool, bool, int flags
,
1645 bool posix
= (flags
& SCFIND_POSIX
) != 0;
1646 int increment
= (minPos
<= maxPos
) ? 1 : -1;
1648 int startPos
= minPos
;
1649 int endPos
= maxPos
;
1651 // Range endpoints should not be inside DBCS characters, but just in case, move them.
1652 startPos
= doc
->MovePositionOutsideChar(startPos
, 1, false);
1653 endPos
= doc
->MovePositionOutsideChar(endPos
, 1, false);
1655 const char *errmsg
= search
.Compile(s
, *length
, caseSensitive
, posix
);
1659 // Find a variable in a property file: \$(\([A-Za-z0-9_.]+\))
1660 // Replace first '.' with '-' in each property file variable reference:
1661 // Search: \$(\([A-Za-z0-9_-]+\)\.\([A-Za-z0-9_.]+\))
1662 // Replace: $(\1-\2)
1663 int lineRangeStart
= doc
->LineFromPosition(startPos
);
1664 int lineRangeEnd
= doc
->LineFromPosition(endPos
);
1665 if ((increment
== 1) &&
1666 (startPos
>= doc
->LineEnd(lineRangeStart
)) &&
1667 (lineRangeStart
< lineRangeEnd
)) {
1668 // the start position is at end of line or between line end characters.
1670 startPos
= doc
->LineStart(lineRangeStart
);
1674 char searchEnd
= s
[*length
- 1];
1675 int lineRangeBreak
= lineRangeEnd
+ increment
;
1676 for (int line
= lineRangeStart
; line
!= lineRangeBreak
; line
+= increment
) {
1677 int startOfLine
= doc
->LineStart(line
);
1678 int endOfLine
= doc
->LineEnd(line
);
1679 if (increment
== 1) {
1680 if (line
== lineRangeStart
) {
1681 if ((startPos
!= startOfLine
) && (s
[0] == '^'))
1682 continue; // Can't match start of line if start position after start of line
1683 startOfLine
= startPos
;
1685 if (line
== lineRangeEnd
) {
1686 if ((endPos
!= endOfLine
) && (searchEnd
== '$'))
1687 continue; // Can't match end of line if end position before end of line
1691 if (line
== lineRangeEnd
) {
1692 if ((endPos
!= startOfLine
) && (s
[0] == '^'))
1693 continue; // Can't match start of line if end position after start of line
1694 startOfLine
= endPos
;
1696 if (line
== lineRangeStart
) {
1697 if ((startPos
!= endOfLine
) && (searchEnd
== '$'))
1698 continue; // Can't match end of line if start position before end of line
1699 endOfLine
= startPos
;
1703 DocumentIndexer
di(doc
, endOfLine
);
1704 int success
= search
.Execute(di
, startOfLine
, endOfLine
);
1706 pos
= search
.bopat
[0];
1707 lenRet
= search
.eopat
[0] - search
.bopat
[0];
1708 if (increment
== -1) {
1709 // Check for the last match on this line.
1710 int repetitions
= 1000; // Break out of infinite loop
1711 while (success
&& (search
.eopat
[0] <= endOfLine
) && (repetitions
--)) {
1712 success
= search
.Execute(di
, pos
+1, endOfLine
);
1714 if (search
.eopat
[0] <= minPos
) {
1715 pos
= search
.bopat
[0];
1716 lenRet
= search
.eopat
[0] - search
.bopat
[0];
1730 const char *BuiltinRegex::SubstituteByPosition(Document
* doc
, const char *text
, int *length
) {
1731 delete []substituted
;
1733 DocumentIndexer
di(doc
, doc
->Length());
1734 if (!search
.GrabMatches(di
))
1736 unsigned int lenResult
= 0;
1737 for (int i
= 0; i
< *length
; i
++) {
1738 if (text
[i
] == '\\') {
1739 if (text
[i
+ 1] >= '1' && text
[i
+ 1] <= '9') {
1740 unsigned int patNum
= text
[i
+ 1] - '0';
1741 lenResult
+= search
.eopat
[patNum
] - search
.bopat
[patNum
];
1744 switch (text
[i
+ 1]) {
1760 substituted
= new char[lenResult
+ 1];
1763 char *o
= substituted
;
1764 for (int j
= 0; j
< *length
; j
++) {
1765 if (text
[j
] == '\\') {
1766 if (text
[j
+ 1] >= '1' && text
[j
+ 1] <= '9') {
1767 unsigned int patNum
= text
[j
+ 1] - '0';
1768 unsigned int len
= search
.eopat
[patNum
] - search
.bopat
[patNum
];
1769 if (search
.pat
[patNum
]) // Will be null if try for a match that did not occur
1770 memcpy(o
, search
.pat
[patNum
], len
);
1807 *length
= lenResult
;
1811 #ifndef SCI_OWNREGEX
1813 #ifdef SCI_NAMESPACE
1815 RegexSearchBase
*Scintilla::CreateRegexSearch(CharClassify
*charClassTable
) {
1816 return new BuiltinRegex(charClassTable
);
1821 RegexSearchBase
*CreateRegexSearch(CharClassify
*charClassTable
) {
1822 return new BuiltinRegex(charClassTable
);