Upgraded to scintilla 3.2.3
[TortoiseGit.git] / ext / scintilla / src / Document.h
blob1125c8a4e8c950214c5ace99397ed655079161c6
1 // Scintilla source code edit control
2 /** @file Document.h
3 ** Text document that handles notifications, DBCS, styling, words and end of line.
4 **/
5 // Copyright 1998-2011 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
8 #ifndef DOCUMENT_H
9 #define DOCUMENT_H
11 #ifdef SCI_NAMESPACE
12 namespace Scintilla {
13 #endif
15 /**
16 * A Position is a position within a document between two characters or at the beginning or end.
17 * Sometimes used as a character index where it identifies the character after the position.
19 typedef int Position;
20 const Position invalidPosition = -1;
22 /**
23 * The range class represents a range of text in a document.
24 * The two values are not sorted as one end may be more significant than the other
25 * as is the case for the selection where the end position is the position of the caret.
26 * If either position is invalidPosition then the range is invalid and most operations will fail.
28 class Range {
29 public:
30 Position start;
31 Position end;
33 Range(Position pos=0) :
34 start(pos), end(pos) {
36 Range(Position start_, Position end_) :
37 start(start_), end(end_) {
40 bool Valid() const {
41 return (start != invalidPosition) && (end != invalidPosition);
44 // Is the position within the range?
45 bool Contains(Position pos) const {
46 if (start < end) {
47 return (pos >= start && pos <= end);
48 } else {
49 return (pos <= start && pos >= end);
53 // Is the character after pos within the range?
54 bool ContainsCharacter(Position pos) const {
55 if (start < end) {
56 return (pos >= start && pos < end);
57 } else {
58 return (pos < start && pos >= end);
62 bool Contains(Range other) const {
63 return Contains(other.start) && Contains(other.end);
66 bool Overlaps(Range other) const {
67 return
68 Contains(other.start) ||
69 Contains(other.end) ||
70 other.Contains(start) ||
71 other.Contains(end);
75 class DocWatcher;
76 class DocModification;
77 class Document;
79 /**
80 * Interface class for regular expression searching
82 class RegexSearchBase {
83 public:
84 virtual ~RegexSearchBase() {}
86 virtual long FindText(Document *doc, int minPos, int maxPos, const char *s,
87 bool caseSensitive, bool word, bool wordStart, int flags, int *length) = 0;
89 ///@return String with the substitutions, must remain valid until the next call or destruction
90 virtual const char *SubstituteByPosition(Document *doc, const char *text, int *length) = 0;
93 /// Factory function for RegexSearchBase
94 extern RegexSearchBase *CreateRegexSearch(CharClassify *charClassTable);
96 struct StyledText {
97 size_t length;
98 const char *text;
99 bool multipleStyles;
100 size_t style;
101 const unsigned char *styles;
102 StyledText(size_t length_, const char *text_, bool multipleStyles_, int style_, const unsigned char *styles_) :
103 length(length_), text(text_), multipleStyles(multipleStyles_), style(style_), styles(styles_) {
105 // Return number of bytes from start to before '\n' or end of text.
106 // Return 1 when start is outside text
107 size_t LineLength(size_t start) const {
108 size_t cur = start;
109 while ((cur < length) && (text[cur] != '\n'))
110 cur++;
111 return cur-start;
113 size_t StyleAt(size_t i) const {
114 return multipleStyles ? styles[i] : style;
118 class HighlightDelimiter {
119 public:
120 HighlightDelimiter() : isEnabled(false) {
121 Clear();
124 void Clear() {
125 beginFoldBlock = -1;
126 endFoldBlock = -1;
127 firstChangeableLineBefore = -1;
128 firstChangeableLineAfter = -1;
131 bool NeedsDrawing(int line) {
132 return isEnabled && (line <= firstChangeableLineBefore || line >= firstChangeableLineAfter);
135 bool IsFoldBlockHighlighted(int line) {
136 return isEnabled && beginFoldBlock != -1 && beginFoldBlock <= line && line <= endFoldBlock;
139 bool IsHeadOfFoldBlock(int line) {
140 return beginFoldBlock == line && line < endFoldBlock;
143 bool IsBodyOfFoldBlock(int line) {
144 return beginFoldBlock != -1 && beginFoldBlock < line && line < endFoldBlock;
147 bool IsTailOfFoldBlock(int line) {
148 return beginFoldBlock != -1 && beginFoldBlock < line && line == endFoldBlock;
151 int beginFoldBlock; // Begin of current fold block
152 int endFoldBlock; // End of current fold block
153 int firstChangeableLineBefore; // First line that triggers repaint before starting line that determined current fold block
154 int firstChangeableLineAfter; // First line that triggers repaint after starting line that determined current fold block
155 bool isEnabled;
158 class CaseFolder {
159 public:
160 virtual ~CaseFolder() {
162 virtual size_t Fold(char *folded, size_t sizeFolded, const char *mixed, size_t lenMixed) = 0;
165 class CaseFolderTable : public CaseFolder {
166 protected:
167 char mapping[256];
168 public:
169 CaseFolderTable();
170 virtual ~CaseFolderTable();
171 virtual size_t Fold(char *folded, size_t sizeFolded, const char *mixed, size_t lenMixed);
172 void SetTranslation(char ch, char chTranslation);
173 void StandardASCII();
176 class Document;
178 class LexInterface {
179 protected:
180 Document *pdoc;
181 ILexer *instance;
182 bool performingStyle; ///< Prevent reentrance
183 public:
184 LexInterface(Document *pdoc_) : pdoc(pdoc_), instance(0), performingStyle(false) {
186 virtual ~LexInterface() {
188 void Colourise(int start, int end);
189 bool UseContainerLexing() const {
190 return instance == 0;
196 class Document : PerLine, public IDocument, public ILoader {
198 public:
199 /** Used to pair watcher pointer with user data. */
200 class WatcherWithUserData {
201 public:
202 DocWatcher *watcher;
203 void *userData;
204 WatcherWithUserData() {
205 watcher = 0;
206 userData = 0;
210 enum charClassification { ccSpace, ccNewLine, ccWord, ccPunctuation };
211 private:
212 int refCount;
213 CellBuffer cb;
214 CharClassify charClass;
215 CaseFolder *pcf;
216 char stylingMask;
217 int endStyled;
218 int styleClock;
219 int enteredModification;
220 int enteredStyling;
221 int enteredReadOnlyCount;
223 WatcherWithUserData *watchers;
224 int lenWatchers;
226 // ldSize is not real data - it is for dimensions and loops
227 enum lineData { ldMarkers, ldLevels, ldState, ldMargin, ldAnnotation, ldSize };
228 PerLine *perLineData[ldSize];
230 bool matchesValid;
231 RegexSearchBase *regex;
233 public:
235 LexInterface *pli;
237 int stylingBits;
238 int stylingBitsMask;
240 int eolMode;
241 /// Can also be SC_CP_UTF8 to enable UTF-8 mode
242 int dbcsCodePage;
243 int tabInChars;
244 int indentInChars;
245 int actualIndentInChars;
246 bool useTabs;
247 bool tabIndents;
248 bool backspaceUnindents;
250 DecorationList decorations;
252 Document();
253 virtual ~Document();
255 int AddRef();
256 int SCI_METHOD Release();
258 virtual void Init();
259 bool SetDBCSCodePage(int dbcsCodePage_);
260 virtual void InsertLine(int line);
261 virtual void RemoveLine(int line);
263 int SCI_METHOD Version() const {
264 return dvOriginal;
267 void SCI_METHOD SetErrorStatus(int status);
269 int SCI_METHOD LineFromPosition(int pos) const;
270 int ClampPositionIntoDocument(int pos);
271 bool IsCrLf(int pos);
272 int LenChar(int pos);
273 bool InGoodUTF8(int pos, int &start, int &end) const;
274 int MovePositionOutsideChar(int pos, int moveDir, bool checkLineEnd=true);
275 int NextPosition(int pos, int moveDir) const;
276 bool NextCharacter(int &pos, int moveDir); // Returns true if pos changed
277 int SCI_METHOD CodePage() const;
278 bool SCI_METHOD IsDBCSLeadByte(char ch) const;
279 int SafeSegment(const char *text, int length, int lengthSegment);
281 // Gateways to modifying document
282 void ModifiedAt(int pos);
283 void CheckReadOnly();
284 bool DeleteChars(int pos, int len);
285 bool InsertString(int position, const char *s, int insertLength);
286 int SCI_METHOD AddData(char *data, int length);
287 void * SCI_METHOD ConvertToDocument();
288 int Undo();
289 int Redo();
290 bool CanUndo() { return cb.CanUndo(); }
291 bool CanRedo() { return cb.CanRedo(); }
292 void DeleteUndoHistory() { cb.DeleteUndoHistory(); }
293 bool SetUndoCollection(bool collectUndo) {
294 return cb.SetUndoCollection(collectUndo);
296 bool IsCollectingUndo() { return cb.IsCollectingUndo(); }
297 void BeginUndoAction() { cb.BeginUndoAction(); }
298 void EndUndoAction() { cb.EndUndoAction(); }
299 void AddUndoAction(int token, bool mayCoalesce) { cb.AddUndoAction(token, mayCoalesce); }
300 void SetSavePoint();
301 bool IsSavePoint() { return cb.IsSavePoint(); }
302 const char * SCI_METHOD BufferPointer() { return cb.BufferPointer(); }
303 const char *RangePointer(int position, int rangeLength) { return cb.RangePointer(position, rangeLength); }
304 int GapPosition() const { return cb.GapPosition(); }
306 int SCI_METHOD GetLineIndentation(int line);
307 void SetLineIndentation(int line, int indent);
308 int GetLineIndentPosition(int line) const;
309 int GetColumn(int position);
310 int CountCharacters(int startPos, int endPos);
311 int FindColumn(int line, int column);
312 void Indent(bool forwards, int lineBottom, int lineTop);
313 static char *TransformLineEnds(int *pLenOut, const char *s, size_t len, int eolModeWanted);
314 void ConvertLineEnds(int eolModeSet);
315 void SetReadOnly(bool set) { cb.SetReadOnly(set); }
316 bool IsReadOnly() { return cb.IsReadOnly(); }
318 bool InsertChar(int pos, char ch);
319 bool InsertCString(int position, const char *s);
320 void ChangeChar(int pos, char ch);
321 void DelChar(int pos);
322 void DelCharBack(int pos);
324 char CharAt(int position) { return cb.CharAt(position); }
325 void SCI_METHOD GetCharRange(char *buffer, int position, int lengthRetrieve) const {
326 cb.GetCharRange(buffer, position, lengthRetrieve);
328 char SCI_METHOD StyleAt(int position) const { return cb.StyleAt(position); }
329 void GetStyleRange(unsigned char *buffer, int position, int lengthRetrieve) const {
330 cb.GetStyleRange(buffer, position, lengthRetrieve);
332 int GetMark(int line);
333 int MarkerNext(int lineStart, int mask) const;
334 int AddMark(int line, int markerNum);
335 void AddMarkSet(int line, int valueSet);
336 void DeleteMark(int line, int markerNum);
337 void DeleteMarkFromHandle(int markerHandle);
338 void DeleteAllMarks(int markerNum);
339 int LineFromHandle(int markerHandle);
340 int SCI_METHOD LineStart(int line) const;
341 int LineEnd(int line) const;
342 int LineEndPosition(int position) const;
343 bool IsLineEndPosition(int position) const;
344 int VCHomePosition(int position) const;
346 int SCI_METHOD SetLevel(int line, int level);
347 int SCI_METHOD GetLevel(int line) const;
348 void ClearLevels();
349 int GetLastChild(int lineParent, int level=-1, int lastLine=-1);
350 int GetFoldParent(int line);
351 void GetHighlightDelimiters(HighlightDelimiter &hDelimiter, int line, int lastLine);
353 void Indent(bool forwards);
354 int ExtendWordSelect(int pos, int delta, bool onlyWordCharacters=false);
355 int NextWordStart(int pos, int delta);
356 int NextWordEnd(int pos, int delta);
357 int SCI_METHOD Length() const { return cb.Length(); }
358 void Allocate(int newSize) { cb.Allocate(newSize); }
359 bool MatchesWordOptions(bool word, bool wordStart, int pos, int length);
360 bool HasCaseFolder(void) const;
361 void SetCaseFolder(CaseFolder *pcf_);
362 long FindText(int minPos, int maxPos, const char *search, bool caseSensitive, bool word,
363 bool wordStart, bool regExp, int flags, int *length);
364 const char *SubstituteByPosition(const char *text, int *length);
365 int LinesTotal() const;
367 void ChangeCase(Range r, bool makeUpperCase);
369 void SetDefaultCharClasses(bool includeWordClass);
370 void SetCharClasses(const unsigned char *chars, CharClassify::cc newCharClass);
371 int GetCharsOfClass(CharClassify::cc charClass, unsigned char *buffer);
372 void SetStylingBits(int bits);
373 void SCI_METHOD StartStyling(int position, char mask);
374 bool SCI_METHOD SetStyleFor(int length, char style);
375 bool SCI_METHOD SetStyles(int length, const char *styles);
376 int GetEndStyled() { return endStyled; }
377 void EnsureStyledTo(int pos);
378 void LexerChanged();
379 int GetStyleClock() { return styleClock; }
380 void IncrementStyleClock();
381 void SCI_METHOD DecorationSetCurrentIndicator(int indicator) {
382 decorations.SetCurrentIndicator(indicator);
384 void SCI_METHOD DecorationFillRange(int position, int value, int fillLength);
386 int SCI_METHOD SetLineState(int line, int state);
387 int SCI_METHOD GetLineState(int line) const;
388 int GetMaxLineState();
389 void SCI_METHOD ChangeLexerState(int start, int end);
391 StyledText MarginStyledText(int line);
392 void MarginSetStyle(int line, int style);
393 void MarginSetStyles(int line, const unsigned char *styles);
394 void MarginSetText(int line, const char *text);
395 int MarginLength(int line) const;
396 void MarginClearAll();
398 bool AnnotationAny() const;
399 StyledText AnnotationStyledText(int line);
400 void AnnotationSetText(int line, const char *text);
401 void AnnotationSetStyle(int line, int style);
402 void AnnotationSetStyles(int line, const unsigned char *styles);
403 int AnnotationLength(int line) const;
404 int AnnotationLines(int line) const;
405 void AnnotationClearAll();
407 bool AddWatcher(DocWatcher *watcher, void *userData);
408 bool RemoveWatcher(DocWatcher *watcher, void *userData);
409 const WatcherWithUserData *GetWatchers() const { return watchers; }
410 int GetLenWatchers() const { return lenWatchers; }
412 CharClassify::cc WordCharClass(unsigned char ch);
413 bool IsWordPartSeparator(char ch);
414 int WordPartLeft(int pos);
415 int WordPartRight(int pos);
416 int ExtendStyleRange(int pos, int delta, bool singleLine = false);
417 bool IsWhiteLine(int line) const;
418 int ParaUp(int pos);
419 int ParaDown(int pos);
420 int IndentSize() { return actualIndentInChars; }
421 int BraceMatch(int position, int maxReStyle);
423 private:
424 bool IsWordStartAt(int pos);
425 bool IsWordEndAt(int pos);
426 bool IsWordAt(int start, int end);
428 void NotifyModifyAttempt();
429 void NotifySavePoint(bool atSavePoint);
430 void NotifyModified(DocModification mh);
433 class UndoGroup {
434 Document *pdoc;
435 bool groupNeeded;
436 public:
437 UndoGroup(Document *pdoc_, bool groupNeeded_=true) :
438 pdoc(pdoc_), groupNeeded(groupNeeded_) {
439 if (groupNeeded) {
440 pdoc->BeginUndoAction();
443 ~UndoGroup() {
444 if (groupNeeded) {
445 pdoc->EndUndoAction();
448 bool Needed() const {
449 return groupNeeded;
455 * To optimise processing of document modifications by DocWatchers, a hint is passed indicating the
456 * scope of the change.
457 * If the DocWatcher is a document view then this can be used to optimise screen updating.
459 class DocModification {
460 public:
461 int modificationType;
462 int position;
463 int length;
464 int linesAdded; /**< Negative if lines deleted. */
465 const char *text; /**< Only valid for changes to text, not for changes to style. */
466 int line;
467 int foldLevelNow;
468 int foldLevelPrev;
469 int annotationLinesAdded;
470 int token;
472 DocModification(int modificationType_, int position_=0, int length_=0,
473 int linesAdded_=0, const char *text_=0, int line_=0) :
474 modificationType(modificationType_),
475 position(position_),
476 length(length_),
477 linesAdded(linesAdded_),
478 text(text_),
479 line(line_),
480 foldLevelNow(0),
481 foldLevelPrev(0),
482 annotationLinesAdded(0),
483 token(0) {}
485 DocModification(int modificationType_, const Action &act, int linesAdded_=0) :
486 modificationType(modificationType_),
487 position(act.position),
488 length(act.lenData),
489 linesAdded(linesAdded_),
490 text(act.data),
491 line(0),
492 foldLevelNow(0),
493 foldLevelPrev(0),
494 annotationLinesAdded(0),
495 token(0) {}
499 * A class that wants to receive notifications from a Document must be derived from DocWatcher
500 * and implement the notification methods. It can then be added to the watcher list with AddWatcher.
502 class DocWatcher {
503 public:
504 virtual ~DocWatcher() {}
506 virtual void NotifyModifyAttempt(Document *doc, void *userData) = 0;
507 virtual void NotifySavePoint(Document *doc, void *userData, bool atSavePoint) = 0;
508 virtual void NotifyModified(Document *doc, DocModification mh, void *userData) = 0;
509 virtual void NotifyDeleted(Document *doc, void *userData) = 0;
510 virtual void NotifyStyleNeeded(Document *doc, void *userData, int endPos) = 0;
511 virtual void NotifyLexerChanged(Document *doc, void *userData) = 0;
512 virtual void NotifyErrorOccurred(Document *doc, void *userData, int status) = 0;
515 #ifdef SCI_NAMESPACE
517 #endif
519 #endif