Add and use utils_get_help_url().
[geany-mirror.git] / scintilla / Document.cxx
blob211d193150e8648bc34e2c90953be22e8ea1306c
1 // Scintilla source code edit control
2 /** @file Document.cxx
3 ** Text document that handles notifications, DBCS, styling, words and end of line.
4 **/
5 // Copyright 1998-2003 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
8 #include <stdlib.h>
9 #include <string.h>
10 #include <stdio.h>
11 #include <ctype.h>
13 #include "Platform.h"
15 #include "Scintilla.h"
16 #include "SplitVector.h"
17 #include "Partitioning.h"
18 #include "RunStyles.h"
19 #include "CellBuffer.h"
20 #include "PerLine.h"
21 #include "CharClassify.h"
22 #include "Decoration.h"
23 #include "Document.h"
24 #include "RESearch.h"
26 #ifdef SCI_NAMESPACE
27 using namespace Scintilla;
28 #endif
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() {
52 refCount = 0;
53 #ifdef unix
54 eolMode = SC_EOL_LF;
55 #else
56 eolMode = SC_EOL_CRLF;
57 #endif
58 dbcsCodePage = 0;
59 stylingBits = 5;
60 stylingBitsMask = 0x1F;
61 stylingMask = 0;
62 endStyled = 0;
63 styleClock = 0;
64 enteredModification = 0;
65 enteredStyling = 0;
66 enteredReadOnlyCount = 0;
67 tabInChars = 8;
68 indentInChars = 0;
69 actualIndentInChars = 8;
70 useTabs = true;
71 tabIndents = true;
72 backspaceUnindents = false;
73 watchers = 0;
74 lenWatchers = 0;
76 matchesValid = false;
77 regex = 0;
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();
85 cb.SetPerLine(this);
88 Document::~Document() {
89 for (int i = 0; i < lenWatchers; i++) {
90 watchers[i].watcher->NotifyDeleted(this, watchers[i].userData);
92 delete []watchers;
93 for (int j=0; j<ldSize; j++) {
94 delete perLineData[j];
95 perLineData[j] = 0;
97 watchers = 0;
98 lenWatchers = 0;
99 delete regex;
100 regex = 0;
103 void Document::Init() {
104 for (int j=0; j<ldSize; j++) {
105 if (perLineData[j])
106 perLineData[j]->Init();
110 void Document::InsertLine(int line) {
111 for (int j=0; j<ldSize; j++) {
112 if (perLineData[j])
113 perLineData[j]->InsertLine(line);
117 void Document::RemoveLine(int line) {
118 for (int j=0; j<ldSize; j++) {
119 if (perLineData[j])
120 perLineData[j]->RemoveLine(line);
124 // Increase reference count and return its previous value.
125 int Document::AddRef() {
126 return refCount++;
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)
134 delete this;
135 return curRefCount;
138 void Document::SetSavePoint() {
139 cb.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);
152 NotifyModified(mh);
153 return prev;
154 } else {
155 return 0;
159 void Document::AddMarkSet(int line, int valueSet) {
160 unsigned int m = valueSet;
161 for (int i = 0; m; i++, m >>= 1)
162 if (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);
166 NotifyModified(mh);
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);
172 NotifyModified(mh);
175 void Document::DeleteMarkFromHandle(int markerHandle) {
176 static_cast<LineMarkers*>(perLineData[ldMarkers])->DeleteMarkFromHandle(markerHandle);
177 DocModification mh(SC_MOD_CHANGEMARKER, 0, 0, 0, 0);
178 mh.line = -1;
179 NotifyModified(mh);
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);
187 mh.line = -1;
188 NotifyModified(mh);
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);
202 } else {
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')) {
206 position--;
208 return position;
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' ) )
226 startText++;
227 if (position == startText)
228 return startPosition;
229 else
230 return startText;
233 int Document::SetLevel(int line, int level) {
234 int prev = static_cast<LineLevels*>(perLineData[ldLevels])->SetLevel(line, level, LinesTotal());
235 if (prev != level) {
236 DocModification mh(SC_MOD_CHANGEFOLD | SC_MOD_CHANGEMARKER,
237 LineStart(line), 0, 0, 0, line);
238 mh.foldLevelNow = level;
239 mh.foldLevelPrev = prev;
240 NotifyModified(mh);
242 return 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)
255 return true;
256 else
257 return (levelStart & SC_FOLDLEVELNUMBERMASK) < (levelTry & SC_FOLDLEVELNUMBERMASK);
260 int Document::GetLastChild(int lineParent, int level) {
261 if (level == -1)
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)))
268 break;
269 lineMaxSubord++;
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) {
275 lineMaxSubord--;
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))
289 lineLook--;
291 if ((GetLevel(lineLook) & SC_FOLDLEVELHEADERFLAG) &&
292 ((GetLevel(lineLook) & SC_FOLDLEVELNUMBERMASK) < level)) {
293 return lineLook;
294 } else {
295 return -1;
299 int Document::ClampPositionIntoDocument(int pos) {
300 return Platform::Clamp(pos, 0, Length());
303 bool Document::IsCrLf(int pos) {
304 if (pos < 0)
305 return false;
306 if (pos >= (Length() - 1))
307 return false;
308 return (cb.CharAt(pos) == '\r') && (cb.CharAt(pos + 1) == '\n');
311 static const int maxBytesInDBCSCharacter=5;
313 int Document::LenChar(int pos) {
314 if (pos < 0) {
315 return 1;
316 } else if (IsCrLf(pos)) {
317 return 2;
318 } else if (SC_CP_UTF8 == dbcsCodePage) {
319 unsigned char ch = static_cast<unsigned char>(cb.CharAt(pos));
320 if (ch < 0x80)
321 return 1;
322 int len = 2;
323 if (ch >= (0x80 + 0x40 + 0x20 + 0x10))
324 len = 4;
325 else if (ch >= (0x80 + 0x40 + 0x20))
326 len = 3;
327 int lengthDoc = Length();
328 if ((pos + len) > lengthDoc)
329 return lengthDoc -pos;
330 else
331 return len;
332 } else if (dbcsCodePage) {
333 char mbstr[maxBytesInDBCSCharacter+1];
334 int i;
335 for (i=0; i<Platform::DBCSCharMaxLength(); i++) {
336 mbstr[i] = cb.CharAt(pos+i);
338 mbstr[i] = '\0';
339 return Platform::DBCSCharLength(dbcsCodePage, mbstr);
340 } else {
341 return 1;
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
352 return 0;
353 } else if (leadByte >= 0xF0) {
354 return 4;
355 } else if (leadByte >= 0xE0) {
356 return 3;
357 } else if (leadByte >= 0xC2) {
358 return 2;
360 return 0;
363 bool Document::InGoodUTF8(int pos, int &start, int &end) {
364 int lead = pos;
365 while ((lead>0) && (pos-lead < 4) && IsTrailByte(static_cast<unsigned char>(cb.CharAt(lead-1))))
366 lead--;
367 start = 0;
368 if (lead > 0) {
369 start = lead-1;
371 int leadByte = static_cast<unsigned char>(cb.CharAt(start));
372 int bytes = BytesFromLead(leadByte);
373 if (bytes == 0) {
374 return false;
375 } else {
376 int trailBytes = bytes - 1;
377 int len = pos - lead + 1;
378 if (len > trailBytes)
379 // pos too far from lead
380 return false;
381 // Check that there are enough trails for this lead
382 int trail = pos + 1;
383 while ((trail-lead<trailBytes) && (trail < Length())) {
384 if (!IsTrailByte(static_cast<unsigned char>(cb.CharAt(trail)))) {
385 return false;
387 trail++;
389 end = start + bytes;
390 return true;
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.
402 if (pos <= 0)
403 return 0;
404 if (pos >= Length())
405 return Length();
407 // PLATFORM_ASSERT(pos > 0 && pos < Length());
408 if (checkLineEnd && IsCrLf(pos - 1)) {
409 if (moveDir > 0)
410 return pos + 1;
411 else
412 return pos - 1;
415 // Not between CR and LF
417 if (dbcsCodePage) {
418 if (SC_CP_UTF8 == dbcsCodePage) {
419 unsigned char ch = static_cast<unsigned char>(cb.CharAt(pos));
420 int startUTF = pos;
421 int endUTF = pos;
422 if (IsTrailByte(ch) && InGoodUTF8(pos, startUTF, endUTF)) {
423 // ch is a trail byte within a UTF-8 character
424 if (moveDir > 0)
425 pos = endUTF;
426 else
427 pos = startUTF;
429 } else {
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];
435 int i;
436 for(i=0;i<Platform::DBCSCharMaxLength();i++) {
437 mbstr[i] = cb.CharAt(posCheck+i);
439 mbstr[i] = '\0';
441 int mbsize = Platform::DBCSCharLength(dbcsCodePage, mbstr);
442 if (posCheck + mbsize == pos) {
443 return pos;
444 } else if (posCheck + mbsize > pos) {
445 if (moveDir > 0) {
446 return posCheck + mbsize;
447 } else {
448 return posCheck;
451 posCheck += mbsize;
456 return pos;
459 void Document::ModifiedAt(int pos) {
460 if (endStyled > pos)
461 endStyled = 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) {
476 if (len == 0)
477 return false;
478 if ((pos + len) > Length())
479 return false;
480 CheckReadOnly();
481 if (enteredModification != 0) {
482 return false;
483 } else {
484 enteredModification++;
485 if (!cb.IsReadOnly()) {
486 NotifyModified(
487 DocModification(
488 SC_MOD_BEFOREDELETE | SC_PERFORMED_USER,
489 pos, len,
490 0, 0));
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))
498 ModifiedAt(pos);
499 else
500 ModifiedAt(pos-1);
501 NotifyModified(
502 DocModification(
503 SC_MOD_DELETETEXT | SC_PERFORMED_USER | (startSequence?SC_STARTACTION:0),
504 pos, len,
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) {
517 return false;
519 CheckReadOnly();
520 if (enteredModification != 0) {
521 return false;
522 } else {
523 enteredModification++;
524 if (!cb.IsReadOnly()) {
525 NotifyModified(
526 DocModification(
527 SC_MOD_BEFOREINSERT | SC_PERFORMED_USER,
528 position, insertLength,
529 0, s));
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);
537 NotifyModified(
538 DocModification(
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() {
549 int newPos = -1;
550 CheckReadOnly();
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;
567 NotifyModified(dm);
568 } else {
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;
587 if (steps > 1)
588 modFlags |= SC_MULTISTEPUNDOREDO;
589 const int linesAdded = LinesTotal() - prevLinesTotal;
590 if (linesAdded != 0)
591 multiLine = true;
592 if (step == steps - 1) {
593 modFlags |= SC_LASTSTEPINUNDOREDO;
594 if (multiLine)
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--;
607 return newPos;
610 int Document::Redo() {
611 int newPos = -1;
612 CheckReadOnly();
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;
628 NotifyModified(dm);
629 } else {
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;
646 if (steps > 1)
647 modFlags |= SC_MULTISTEPUNDOREDO;
648 const int linesAdded = LinesTotal() - prevLinesTotal;
649 if (linesAdded != 0)
650 multiLine = true;
651 if (step == steps - 1) {
652 modFlags |= SC_LASTSTEPINUNDOREDO;
653 if (multiLine)
654 modFlags |= SC_MULTILINEUNDOREDO;
656 NotifyModified(
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--;
667 return newPos;
671 * Insert a single character.
673 bool Document::InsertChar(int pos, char ch) {
674 char chs[1];
675 chs[0] = 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) {
687 DeleteChars(pos, 1);
688 InsertChar(pos, ch);
691 void Document::DelChar(int pos) {
692 DeleteChars(pos, LenChar(pos));
695 void Document::DelCharBack(int pos) {
696 if (pos <= 0) {
697 return;
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);
703 } else {
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
718 if (!insertSpaces) {
719 while ((indent >= tabSize) && (length > 0)) {
720 *linebuf++ = '\t';
721 indent -= tabSize;
722 length--;
725 while ((indent > 0) && (length > 0)) {
726 *linebuf++ = ' ';
727 indent--;
728 length--;
730 *linebuf = '\0';
733 int Document::GetLineIndentation(int line) {
734 int indent = 0;
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);
740 if (ch == ' ')
741 indent++;
742 else if (ch == '\t')
743 indent = NextTab(indent, tabInChars);
744 else
745 return indent;
748 return indent;
751 void Document::SetLineIndentation(int line, int indent) {
752 int indentOfLine = GetLineIndentation(line);
753 if (indent < 0)
754 indent = 0;
755 if (indent != indentOfLine) {
756 char linebuf[1000];
757 CreateIndentation(linebuf, sizeof(linebuf), indent, tabInChars, !useTabs);
758 int thisLineStart = LineStart(line);
759 int indentPos = GetLineIndentPosition(line);
760 BeginUndoAction();
761 DeleteChars(thisLineStart, indentPos - thisLineStart);
762 InsertCString(thisLineStart, linebuf);
763 EndUndoAction();
767 int Document::GetLineIndentPosition(int line) const {
768 if (line < 0)
769 return 0;
770 int pos = LineStart(line);
771 int length = Length();
772 while ((pos < length) && isindentchar(cb.CharAt(pos))) {
773 pos++;
775 return pos;
778 int Document::GetColumn(int pos) {
779 int column = 0;
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);
784 if (ch == '\t') {
785 column = NextTab(column, tabInChars);
786 i++;
787 } else if (ch == '\r') {
788 return column;
789 } else if (ch == '\n') {
790 return column;
791 } else if (i >= Length()) {
792 return column;
793 } else {
794 column++;
795 i = MovePositionOutsideChar(i + 1, 1, false);
799 return column;
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);
808 if (ch == '\t') {
809 columnCurrent = NextTab(columnCurrent, tabInChars);
810 position++;
811 } else if (ch == '\r') {
812 return position;
813 } else if (ch == '\n') {
814 return position;
815 } else {
816 columnCurrent++;
817 position = MovePositionOutsideChar(position + 1, 1, false);
821 return position;
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);
828 if (forwards) {
829 if (LineStart(line) < LineEnd(line)) {
830 SetLineIndentation(line, indentOfLine + IndentSize());
832 } else {
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;
844 char *dptr = dest;
845 for (size_t i = 0; (i < len) && (*sptr != '\0'); i++) {
846 if (*sptr == '\n' || *sptr == '\r') {
847 if (eolMode == SC_EOL_CR) {
848 *dptr++ = '\r';
849 } else if (eolMode == SC_EOL_LF) {
850 *dptr++ = '\n';
851 } else { // eolMode == SC_EOL_CRLF
852 *dptr++ = '\r';
853 *dptr++ = '\n';
855 if ((*sptr == '\r') && (i+1 < len) && (*(sptr+1) == '\n')) {
856 i++;
857 sptr++;
859 sptr++;
860 } else {
861 *dptr++ = *sptr++;
864 *dptr++ = '\0';
865 *pLenOut = (dptr - dest) - 1;
866 return dest;
869 void Document::ConvertLineEnds(int eolModeSet) {
870 BeginUndoAction();
872 for (int pos = 0; pos < Length(); pos++) {
873 if (cb.CharAt(pos) == '\r') {
874 if (cb.CharAt(pos + 1) == '\n') {
875 // CRLF
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
880 } else {
881 pos++;
883 } else {
884 // CR
885 if (eolModeSet == SC_EOL_CRLF) {
886 InsertString(pos + 1, "\n", 1); // Insert LF
887 pos++;
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') {
894 // LF
895 if (eolModeSet == SC_EOL_CRLF) {
896 InsertString(pos, "\r", 1); // Insert CR
897 pos++;
898 } else if (eolModeSet == SC_EOL_CR) {
899 InsertString(pos, "\r", 1); // Insert CR
900 DeleteChars(pos + 1, 1); // Delete LF
905 EndUndoAction();
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') {
913 return false;
915 ++currentChar;
917 return true;
920 int Document::ParaUp(int pos) {
921 int line = LineFromPosition(pos);
922 line--;
923 while (line >= 0 && IsWhiteLine(line)) { // skip empty lines
924 line--;
926 while (line >= 0 && !IsWhiteLine(line)) { // skip non-empty lines
927 line--;
929 line++;
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
936 line++;
938 while (line < LinesTotal() && IsWhiteLine(line)) { // skip empty lines
939 line++;
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;
959 if (delta < 0) {
960 if (!onlyWordCharacters)
961 ccStart = WordCharClass(cb.CharAt(pos-1));
962 while (pos > 0 && (WordCharClass(cb.CharAt(pos - 1)) == ccStart))
963 pos--;
964 } else {
965 if (!onlyWordCharacters && pos < Length())
966 ccStart = WordCharClass(cb.CharAt(pos));
967 while (pos < (Length()) && (WordCharClass(cb.CharAt(pos)) == ccStart))
968 pos++;
970 return MovePositionOutsideChar(pos, delta);
974 * Find the start of the next word in either a forward (delta >= 0) or backwards direction
975 * (delta < 0).
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) {
981 if (delta < 0) {
982 while (pos > 0 && (WordCharClass(cb.CharAt(pos - 1)) == CharClassify::ccSpace))
983 pos--;
984 if (pos > 0) {
985 CharClassify::cc ccStart = WordCharClass(cb.CharAt(pos-1));
986 while (pos > 0 && (WordCharClass(cb.CharAt(pos - 1)) == ccStart)) {
987 pos--;
990 } else {
991 CharClassify::cc ccStart = WordCharClass(cb.CharAt(pos));
992 while (pos < (Length()) && (WordCharClass(cb.CharAt(pos)) == ccStart))
993 pos++;
994 while (pos < (Length()) && (WordCharClass(cb.CharAt(pos)) == CharClassify::ccSpace))
995 pos++;
997 return pos;
1001 * Find the end of the next word in either a forward (delta >= 0) or backwards direction
1002 * (delta < 0).
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) {
1008 if (delta < 0) {
1009 if (pos > 0) {
1010 CharClassify::cc ccStart = WordCharClass(cb.CharAt(pos-1));
1011 if (ccStart != CharClassify::ccSpace) {
1012 while (pos > 0 && WordCharClass(cb.CharAt(pos - 1)) == ccStart) {
1013 pos--;
1016 while (pos > 0 && WordCharClass(cb.CharAt(pos - 1)) == CharClassify::ccSpace) {
1017 pos--;
1020 } else {
1021 while (pos < Length() && WordCharClass(cb.CharAt(pos)) == CharClassify::ccSpace) {
1022 pos++;
1024 if (pos < Length()) {
1025 CharClassify::cc ccStart = WordCharClass(cb.CharAt(pos));
1026 while (pos < Length() && WordCharClass(cb.CharAt(pos)) == ccStart) {
1027 pos++;
1031 return pos;
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) {
1039 if (pos > 0) {
1040 CharClassify::cc ccPos = WordCharClass(CharAt(pos));
1041 return (ccPos == CharClassify::ccWord || ccPos == CharClassify::ccPunctuation) &&
1042 (ccPos != WordCharClass(CharAt(pos - 1)));
1044 return true;
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)));
1057 return true;
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')
1073 return ch;
1074 else
1075 return static_cast<char>(ch - 'a' + 'A');
1078 static inline char MakeLowerCase(char ch) {
1079 if (ch < 'A' || ch > 'Z')
1080 return ch;
1081 else
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,
1092 int *length) {
1093 if (regExp) {
1094 if (!regex)
1095 regex = CreateRegexSearch(&charClass);
1096 return regex->FindText(this, minPos, maxPos, s, caseSensitive, word, wordStart, flags, length);
1097 } else {
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];
1116 if (!caseSensitive)
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) {
1123 bool found = true;
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])
1128 found = false;
1130 if (found) {
1131 if ((!word && !wordStart) ||
1132 (word && IsWordAt(pos, pos + lengthFind)) ||
1133 (wordStart && IsWordStartAt(pos)))
1134 return pos;
1137 } else {
1138 if (MakeUpperCase(ch) == firstChar) {
1139 bool found = true;
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]))
1144 found = false;
1146 if (found) {
1147 if ((!word && !wordStart) ||
1148 (word && IsWordAt(pos, pos + lengthFind)) ||
1149 (wordStart && IsWordStartAt(pos)))
1150 return pos;
1154 pos += increment;
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");
1162 return -1;
1165 const char *Document::SubstituteByPosition(const char *text, int *length) {
1166 return regex->SubstituteByPosition(this, text, length);
1169 int Document::LinesTotal() const {
1170 return cb.Lines();
1173 void Document::ChangeCase(Range r, bool makeUpperCase) {
1174 for (int pos = r.start; pos < r.end;) {
1175 int len = LenChar(pos);
1176 if (len == 1) {
1177 char ch = CharAt(pos);
1178 if (makeUpperCase) {
1179 if (IsLowerCase(ch)) {
1180 ChangeChar(pos, static_cast<char>(MakeUpperCase(ch)));
1182 } else {
1183 if (IsUpperCase(ch)) {
1184 ChangeChar(pos, static_cast<char>(MakeLowerCase(ch)));
1188 pos += len;
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) {
1201 stylingBits = bits;
1202 stylingBitsMask = (1 << stylingBits) - 1;
1205 void Document::StartStyling(int position, char mask) {
1206 stylingMask = mask;
1207 endStyled = position;
1210 bool Document::SetStyleFor(int length, char style) {
1211 if (enteredStyling != 0) {
1212 return false;
1213 } else {
1214 enteredStyling++;
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);
1220 NotifyModified(mh);
1222 endStyled += length;
1223 enteredStyling--;
1224 return true;
1228 bool Document::SetStyles(int length, const char *styles) {
1229 if (enteredStyling != 0) {
1230 return false;
1231 } else {
1232 enteredStyling++;
1233 bool didChange = false;
1234 int startMod = 0;
1235 int endMod = 0;
1236 for (int iPos = 0; iPos < length; iPos++, endStyled++) {
1237 PLATFORM_ASSERT(endStyled < Length());
1238 if (cb.SetStyleAt(endStyled, styles[iPos], stylingMask)) {
1239 if (!didChange) {
1240 startMod = endStyled;
1242 didChange = true;
1243 endMod = endStyled;
1246 if (didChange) {
1247 DocModification mh(SC_MOD_CHANGESTYLE | SC_PERFORMED_USER,
1248 startMod, endMod - startMod + 1);
1249 NotifyModified(mh);
1251 enteredStyling--;
1252 return true;
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);
1270 NotifyModified(mh);
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);
1292 NotifyModified(mh);
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;
1331 NotifyModified(mh);
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);
1366 NotifyModified(mh);
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))
1374 return false;
1376 WatcherWithUserData *pwNew = new WatcherWithUserData[lenWatchers + 1];
1377 if (!pwNew)
1378 return false;
1379 for (int j = 0; j < lenWatchers; j++)
1380 pwNew[j] = watchers[j];
1381 pwNew[lenWatchers].watcher = watcher;
1382 pwNew[lenWatchers].userData = userData;
1383 delete []watchers;
1384 watchers = pwNew;
1385 lenWatchers++;
1386 return true;
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) {
1394 delete []watchers;
1395 watchers = 0;
1396 lenWatchers = 0;
1397 } else {
1398 WatcherWithUserData *pwNew = new WatcherWithUserData[lenWatchers];
1399 if (!pwNew)
1400 return false;
1401 for (int j = 0; j < lenWatchers - 1; j++) {
1402 pwNew[j] = (j < i) ? watchers[j] : watchers[j + 1];
1404 delete []watchers;
1405 watchers = pwNew;
1406 lenWatchers--;
1408 return true;
1411 return false;
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) {
1442 if (pos > 0) {
1443 --pos;
1444 char startChar = cb.CharAt(pos);
1445 if (IsWordPartSeparator(startChar)) {
1446 while (pos > 0 && IsWordPartSeparator(cb.CharAt(pos))) {
1447 --pos;
1450 if (pos > 0) {
1451 startChar = cb.CharAt(pos);
1452 --pos;
1453 if (IsLowerCase(startChar)) {
1454 while (pos > 0 && IsLowerCase(cb.CharAt(pos)))
1455 --pos;
1456 if (!IsUpperCase(cb.CharAt(pos)) && !IsLowerCase(cb.CharAt(pos)))
1457 ++pos;
1458 } else if (IsUpperCase(startChar)) {
1459 while (pos > 0 && IsUpperCase(cb.CharAt(pos)))
1460 --pos;
1461 if (!IsUpperCase(cb.CharAt(pos)))
1462 ++pos;
1463 } else if (IsADigit(startChar)) {
1464 while (pos > 0 && IsADigit(cb.CharAt(pos)))
1465 --pos;
1466 if (!IsADigit(cb.CharAt(pos)))
1467 ++pos;
1468 } else if (IsPunctuation(startChar)) {
1469 while (pos > 0 && IsPunctuation(cb.CharAt(pos)))
1470 --pos;
1471 if (!IsPunctuation(cb.CharAt(pos)))
1472 ++pos;
1473 } else if (isspacechar(startChar)) {
1474 while (pos > 0 && isspacechar(cb.CharAt(pos)))
1475 --pos;
1476 if (!isspacechar(cb.CharAt(pos)))
1477 ++pos;
1478 } else if (!isascii(startChar)) {
1479 while (pos > 0 && !isascii(cb.CharAt(pos)))
1480 --pos;
1481 if (isascii(cb.CharAt(pos)))
1482 ++pos;
1483 } else {
1484 ++pos;
1488 return 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)))
1496 ++pos;
1497 startChar = cb.CharAt(pos);
1499 if (!isascii(startChar)) {
1500 while (pos < length && !isascii(cb.CharAt(pos)))
1501 ++pos;
1502 } else if (IsLowerCase(startChar)) {
1503 while (pos < length && IsLowerCase(cb.CharAt(pos)))
1504 ++pos;
1505 } else if (IsUpperCase(startChar)) {
1506 if (IsLowerCase(cb.CharAt(pos + 1))) {
1507 ++pos;
1508 while (pos < length && IsLowerCase(cb.CharAt(pos)))
1509 ++pos;
1510 } else {
1511 while (pos < length && IsUpperCase(cb.CharAt(pos)))
1512 ++pos;
1514 if (IsLowerCase(cb.CharAt(pos)) && IsUpperCase(cb.CharAt(pos - 1)))
1515 --pos;
1516 } else if (IsADigit(startChar)) {
1517 while (pos < length && IsADigit(cb.CharAt(pos)))
1518 ++pos;
1519 } else if (IsPunctuation(startChar)) {
1520 while (pos < length && IsPunctuation(cb.CharAt(pos)))
1521 ++pos;
1522 } else if (isspacechar(startChar)) {
1523 while (pos < length && isspacechar(cb.CharAt(pos)))
1524 ++pos;
1525 } else {
1526 ++pos;
1528 return 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);
1537 if (delta < 0) {
1538 while (pos > 0 && (cb.StyleAt(pos) == sStart) && (!singleLine || !IsLineEndChar(cb.CharAt(pos))) )
1539 pos--;
1540 pos++;
1541 } else {
1542 while (pos < (Length()) && (cb.StyleAt(pos) == sStart) && (!singleLine || !IsLineEndChar(cb.CharAt(pos))) )
1543 pos++;
1545 return pos;
1548 static char BraceOpposite(char ch) {
1549 switch (ch) {
1550 case '(':
1551 return ')';
1552 case ')':
1553 return '(';
1554 case '[':
1555 return ']';
1556 case ']':
1557 return '[';
1558 case '{':
1559 return '}';
1560 case '}':
1561 return '{';
1562 case '<':
1563 return '>';
1564 case '>':
1565 return '<';
1566 default:
1567 return '\0';
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);
1575 if (chSeek == '\0')
1576 return - 1;
1577 char styBrace = static_cast<char>(StyleAt(position) & stylingBitsMask);
1578 int direction = -1;
1579 if (chBrace == '(' || chBrace == '[' || chBrace == '{' || chBrace == '<')
1580 direction = 1;
1581 int depth = 1;
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)
1589 depth++;
1590 if (chAtPos == chSeek)
1591 depth--;
1592 if (depth == 0)
1593 return position;
1595 position = position + direction;
1597 return - 1;
1601 * Implementation of RegexSearchBase for the default built-in regular expression engine
1603 class BuiltinRegex : public RegexSearchBase {
1604 public:
1605 BuiltinRegex(CharClassify *charClassTable) : search(charClassTable), substituted(NULL) {}
1607 virtual ~BuiltinRegex() {
1608 delete substituted;
1611 virtual long FindText(Document *doc, int minPos, int maxPos, const char *s,
1612 bool caseSensitive, bool word, bool wordStart, int flags,
1613 int *length);
1615 virtual const char *SubstituteByPosition(Document* doc, const char *text, int *length);
1617 private:
1618 RESearch search;
1619 char *substituted;
1622 // Define a way for the Regular Expression code to access the document
1623 class DocumentIndexer : public CharacterIndexer {
1624 Document *pdoc;
1625 int end;
1626 public:
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)
1636 return 0;
1637 else
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,
1644 int *length) {
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);
1656 if (errmsg) {
1657 return -1;
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.
1669 lineRangeStart++;
1670 startPos = doc->LineStart(lineRangeStart);
1672 int pos = -1;
1673 int lenRet = 0;
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
1688 endOfLine = endPos;
1690 } else {
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);
1705 if (success) {
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);
1713 if (success) {
1714 if (search.eopat[0] <= minPos) {
1715 pos = search.bopat[0];
1716 lenRet = search.eopat[0] - search.bopat[0];
1717 } else {
1718 success = 0;
1723 break;
1726 *length = lenRet;
1727 return pos;
1730 const char *BuiltinRegex::SubstituteByPosition(Document* doc, const char *text, int *length) {
1731 delete []substituted;
1732 substituted = 0;
1733 DocumentIndexer di(doc, doc->Length());
1734 if (!search.GrabMatches(di))
1735 return 0;
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];
1742 i++;
1743 } else {
1744 switch (text[i + 1]) {
1745 case 'a':
1746 case 'b':
1747 case 'f':
1748 case 'n':
1749 case 'r':
1750 case 't':
1751 case 'v':
1752 i++;
1754 lenResult++;
1756 } else {
1757 lenResult++;
1760 substituted = new char[lenResult + 1];
1761 if (!substituted)
1762 return 0;
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);
1771 o += len;
1772 j++;
1773 } else {
1774 j++;
1775 switch (text[j]) {
1776 case 'a':
1777 *o++ = '\a';
1778 break;
1779 case 'b':
1780 *o++ = '\b';
1781 break;
1782 case 'f':
1783 *o++ = '\f';
1784 break;
1785 case 'n':
1786 *o++ = '\n';
1787 break;
1788 case 'r':
1789 *o++ = '\r';
1790 break;
1791 case 't':
1792 *o++ = '\t';
1793 break;
1794 case 'v':
1795 *o++ = '\v';
1796 break;
1797 default:
1798 *o++ = '\\';
1799 j--;
1802 } else {
1803 *o++ = text[j];
1806 *o = '\0';
1807 *length = lenResult;
1808 return substituted;
1811 #ifndef SCI_OWNREGEX
1813 #ifdef SCI_NAMESPACE
1815 RegexSearchBase *Scintilla::CreateRegexSearch(CharClassify *charClassTable) {
1816 return new BuiltinRegex(charClassTable);
1819 #else
1821 RegexSearchBase *CreateRegexSearch(CharClassify *charClassTable) {
1822 return new BuiltinRegex(charClassTable);
1825 #endif
1827 #endif