Update Scintilla to version 3.4.4
[TortoiseGit.git] / ext / scintilla / src / Document.h
blob600b41c3940d7fe59ae83295dc594bec581f4a09
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 enum EncodingFamily { efEightBit, efUnicode, efDBCS };
24 /**
25 * The range class represents a range of text in a document.
26 * The two values are not sorted as one end may be more significant than the other
27 * as is the case for the selection where the end position is the position of the caret.
28 * If either position is invalidPosition then the range is invalid and most operations will fail.
30 class Range {
31 public:
32 Position start;
33 Position end;
35 explicit Range(Position pos=0) :
36 start(pos), end(pos) {
38 Range(Position start_, Position end_) :
39 start(start_), end(end_) {
42 bool operator==(const Range &other) const {
43 return (start == other.start) && (end == other.end);
46 bool Valid() const {
47 return (start != invalidPosition) && (end != invalidPosition);
50 Position First() const {
51 return (start <= end) ? start : end;
54 Position Last() const {
55 return (start > end) ? start : end;
58 // Is the position within the range?
59 bool Contains(Position pos) const {
60 if (start < end) {
61 return (pos >= start && pos <= end);
62 } else {
63 return (pos <= start && pos >= end);
67 // Is the character after pos within the range?
68 bool ContainsCharacter(Position pos) const {
69 if (start < end) {
70 return (pos >= start && pos < end);
71 } else {
72 return (pos < start && pos >= end);
76 bool Contains(Range other) const {
77 return Contains(other.start) && Contains(other.end);
80 bool Overlaps(Range other) const {
81 return
82 Contains(other.start) ||
83 Contains(other.end) ||
84 other.Contains(start) ||
85 other.Contains(end);
89 class DocWatcher;
90 class DocModification;
91 class Document;
93 /**
94 * Interface class for regular expression searching
96 class RegexSearchBase {
97 public:
98 virtual ~RegexSearchBase() {}
100 virtual long FindText(Document *doc, int minPos, int maxPos, const char *s,
101 bool caseSensitive, bool word, bool wordStart, int flags, int *length) = 0;
103 ///@return String with the substitutions, must remain valid until the next call or destruction
104 virtual const char *SubstituteByPosition(Document *doc, const char *text, int *length) = 0;
107 /// Factory function for RegexSearchBase
108 extern RegexSearchBase *CreateRegexSearch(CharClassify *charClassTable);
110 struct StyledText {
111 size_t length;
112 const char *text;
113 bool multipleStyles;
114 size_t style;
115 const unsigned char *styles;
116 StyledText(size_t length_, const char *text_, bool multipleStyles_, int style_, const unsigned char *styles_) :
117 length(length_), text(text_), multipleStyles(multipleStyles_), style(style_), styles(styles_) {
119 // Return number of bytes from start to before '\n' or end of text.
120 // Return 1 when start is outside text
121 size_t LineLength(size_t start) const {
122 size_t cur = start;
123 while ((cur < length) && (text[cur] != '\n'))
124 cur++;
125 return cur-start;
127 size_t StyleAt(size_t i) const {
128 return multipleStyles ? styles[i] : style;
132 class HighlightDelimiter {
133 public:
134 HighlightDelimiter() : isEnabled(false) {
135 Clear();
138 void Clear() {
139 beginFoldBlock = -1;
140 endFoldBlock = -1;
141 firstChangeableLineBefore = -1;
142 firstChangeableLineAfter = -1;
145 bool NeedsDrawing(int line) const {
146 return isEnabled && (line <= firstChangeableLineBefore || line >= firstChangeableLineAfter);
149 bool IsFoldBlockHighlighted(int line) const {
150 return isEnabled && beginFoldBlock != -1 && beginFoldBlock <= line && line <= endFoldBlock;
153 bool IsHeadOfFoldBlock(int line) const {
154 return beginFoldBlock == line && line < endFoldBlock;
157 bool IsBodyOfFoldBlock(int line) const {
158 return beginFoldBlock != -1 && beginFoldBlock < line && line < endFoldBlock;
161 bool IsTailOfFoldBlock(int line) const {
162 return beginFoldBlock != -1 && beginFoldBlock < line && line == endFoldBlock;
165 int beginFoldBlock; // Begin of current fold block
166 int endFoldBlock; // End of current fold block
167 int firstChangeableLineBefore; // First line that triggers repaint before starting line that determined current fold block
168 int firstChangeableLineAfter; // First line that triggers repaint after starting line that determined current fold block
169 bool isEnabled;
172 class Document;
174 class LexInterface {
175 protected:
176 Document *pdoc;
177 ILexer *instance;
178 bool performingStyle; ///< Prevent reentrance
179 public:
180 explicit LexInterface(Document *pdoc_) : pdoc(pdoc_), instance(0), performingStyle(false) {
182 virtual ~LexInterface() {
184 void Colourise(int start, int end);
185 int LineEndTypesSupported();
186 bool UseContainerLexing() const {
187 return instance == 0;
193 class Document : PerLine, public IDocumentWithLineEnd, public ILoader {
195 public:
196 /** Used to pair watcher pointer with user data. */
197 struct WatcherWithUserData {
198 DocWatcher *watcher;
199 void *userData;
200 WatcherWithUserData(DocWatcher *watcher_=0, void *userData_=0) :
201 watcher(watcher_), userData(userData_) {
203 bool operator==(const WatcherWithUserData &other) const {
204 return (watcher == other.watcher) && (userData == other.userData);
208 private:
209 int refCount;
210 CellBuffer cb;
211 CharClassify charClass;
212 CaseFolder *pcf;
213 int endStyled;
214 int styleClock;
215 int enteredModification;
216 int enteredStyling;
217 int enteredReadOnlyCount;
219 bool insertionSet;
220 std::string insertion;
222 std::vector<WatcherWithUserData> watchers;
224 // ldSize is not real data - it is for dimensions and loops
225 enum lineData { ldMarkers, ldLevels, ldState, ldMargin, ldAnnotation, ldSize };
226 PerLine *perLineData[ldSize];
228 bool matchesValid;
229 RegexSearchBase *regex;
231 public:
233 LexInterface *pli;
235 int eolMode;
236 /// Can also be SC_CP_UTF8 to enable UTF-8 mode
237 int dbcsCodePage;
238 int lineEndBitSet;
239 int tabInChars;
240 int indentInChars;
241 int actualIndentInChars;
242 bool useTabs;
243 bool tabIndents;
244 bool backspaceUnindents;
246 DecorationList decorations;
248 Document();
249 virtual ~Document();
251 int AddRef();
252 int SCI_METHOD Release();
254 virtual void Init();
255 int LineEndTypesSupported() const;
256 bool SetDBCSCodePage(int dbcsCodePage_);
257 int GetLineEndTypesAllowed() const { return cb.GetLineEndTypes(); }
258 bool SetLineEndTypesAllowed(int lineEndBitSet_);
259 int GetLineEndTypesActive() const { return cb.GetLineEndTypes(); }
260 virtual void InsertLine(int line);
261 virtual void RemoveLine(int line);
263 int SCI_METHOD Version() const {
264 return dvLineEnd;
267 void SCI_METHOD SetErrorStatus(int status);
269 int SCI_METHOD LineFromPosition(int pos) const;
270 int ClampPositionIntoDocument(int pos) const;
271 bool IsCrLf(int pos) const;
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) const; // Returns true if pos changed
277 int SCI_METHOD GetRelativePosition(int positionStart, int characterOffset) const;
278 int SCI_METHOD GetCharacterAndWidth(int position, int *pWidth) const;
279 int SCI_METHOD CodePage() const;
280 bool SCI_METHOD IsDBCSLeadByte(char ch) const;
281 int SafeSegment(const char *text, int length, int lengthSegment) const;
282 EncodingFamily CodePageFamily() const;
284 // Gateways to modifying document
285 void ModifiedAt(int pos);
286 void CheckReadOnly();
287 bool DeleteChars(int pos, int len);
288 int InsertString(int position, const char *s, int insertLength);
289 void ChangeInsertion(const char *s, int length);
290 int SCI_METHOD AddData(char *data, int length);
291 void * SCI_METHOD ConvertToDocument();
292 int Undo();
293 int Redo();
294 bool CanUndo() const { return cb.CanUndo(); }
295 bool CanRedo() const { return cb.CanRedo(); }
296 void DeleteUndoHistory() { cb.DeleteUndoHistory(); }
297 bool SetUndoCollection(bool collectUndo) {
298 return cb.SetUndoCollection(collectUndo);
300 bool IsCollectingUndo() const { return cb.IsCollectingUndo(); }
301 void BeginUndoAction() { cb.BeginUndoAction(); }
302 void EndUndoAction() { cb.EndUndoAction(); }
303 void AddUndoAction(int token, bool mayCoalesce) { cb.AddUndoAction(token, mayCoalesce); }
304 void SetSavePoint();
305 bool IsSavePoint() const { return cb.IsSavePoint(); }
306 const char * SCI_METHOD BufferPointer() { return cb.BufferPointer(); }
307 const char *RangePointer(int position, int rangeLength) { return cb.RangePointer(position, rangeLength); }
308 int GapPosition() const { return cb.GapPosition(); }
310 int SCI_METHOD GetLineIndentation(int line);
311 int SetLineIndentation(int line, int indent);
312 int GetLineIndentPosition(int line) const;
313 int GetColumn(int position);
314 int CountCharacters(int startPos, int endPos);
315 int FindColumn(int line, int column);
316 void Indent(bool forwards, int lineBottom, int lineTop);
317 static std::string TransformLineEnds(const char *s, size_t len, int eolModeWanted);
318 void ConvertLineEnds(int eolModeSet);
319 void SetReadOnly(bool set) { cb.SetReadOnly(set); }
320 bool IsReadOnly() const { return cb.IsReadOnly(); }
322 void DelChar(int pos);
323 void DelCharBack(int pos);
325 char CharAt(int position) const { return cb.CharAt(position); }
326 void SCI_METHOD GetCharRange(char *buffer, int position, int lengthRetrieve) const {
327 cb.GetCharRange(buffer, position, lengthRetrieve);
329 char SCI_METHOD StyleAt(int position) const { return cb.StyleAt(position); }
330 void GetStyleRange(unsigned char *buffer, int position, int lengthRetrieve) const {
331 cb.GetStyleRange(buffer, position, lengthRetrieve);
333 int GetMark(int line);
334 int MarkerNext(int lineStart, int mask) const;
335 int AddMark(int line, int markerNum);
336 void AddMarkSet(int line, int valueSet);
337 void DeleteMark(int line, int markerNum);
338 void DeleteMarkFromHandle(int markerHandle);
339 void DeleteAllMarks(int markerNum);
340 int LineFromHandle(int markerHandle);
341 int SCI_METHOD LineStart(int line) const;
342 int SCI_METHOD LineEnd(int line) const;
343 int LineEndPosition(int position) const;
344 bool IsLineEndPosition(int position) const;
345 bool IsPositionInLineEnd(int position) const;
346 int VCHomePosition(int position) const;
348 int SCI_METHOD SetLevel(int line, int level);
349 int SCI_METHOD GetLevel(int line) const;
350 void ClearLevels();
351 int GetLastChild(int lineParent, int level=-1, int lastLine=-1);
352 int GetFoldParent(int line) const;
353 void GetHighlightDelimiters(HighlightDelimiter &hDelimiter, int line, int lastLine);
355 void Indent(bool forwards);
356 int ExtendWordSelect(int pos, int delta, bool onlyWordCharacters=false);
357 int NextWordStart(int pos, int delta);
358 int NextWordEnd(int pos, int delta);
359 int SCI_METHOD Length() const { return cb.Length(); }
360 void Allocate(int newSize) { cb.Allocate(newSize); }
361 bool MatchesWordOptions(bool word, bool wordStart, int pos, int length) const;
362 bool HasCaseFolder(void) const;
363 void SetCaseFolder(CaseFolder *pcf_);
364 long FindText(int minPos, int maxPos, const char *search, bool caseSensitive, bool word,
365 bool wordStart, bool regExp, int flags, int *length);
366 const char *SubstituteByPosition(const char *text, int *length);
367 int LinesTotal() const;
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 SCI_METHOD StartStyling(int position, char mask);
373 bool SCI_METHOD SetStyleFor(int length, char style);
374 bool SCI_METHOD SetStyles(int length, const char *styles);
375 int GetEndStyled() const { return endStyled; }
376 void EnsureStyledTo(int pos);
377 void LexerChanged();
378 int GetStyleClock() const { return styleClock; }
379 void IncrementStyleClock();
380 void SCI_METHOD DecorationSetCurrentIndicator(int indicator) {
381 decorations.SetCurrentIndicator(indicator);
383 void SCI_METHOD DecorationFillRange(int position, int value, int fillLength);
385 int SCI_METHOD SetLineState(int line, int state);
386 int SCI_METHOD GetLineState(int line) const;
387 int GetMaxLineState();
388 void SCI_METHOD ChangeLexerState(int start, int end);
390 StyledText MarginStyledText(int line) const;
391 void MarginSetStyle(int line, int style);
392 void MarginSetStyles(int line, const unsigned char *styles);
393 void MarginSetText(int line, const char *text);
394 void MarginClearAll();
396 StyledText AnnotationStyledText(int line) const;
397 void AnnotationSetText(int line, const char *text);
398 void AnnotationSetStyle(int line, int style);
399 void AnnotationSetStyles(int line, const unsigned char *styles);
400 int AnnotationLines(int line) const;
401 void AnnotationClearAll();
403 bool AddWatcher(DocWatcher *watcher, void *userData);
404 bool RemoveWatcher(DocWatcher *watcher, void *userData);
406 CharClassify::cc WordCharClass(unsigned char ch) const;
407 bool IsWordPartSeparator(char ch) const;
408 int WordPartLeft(int pos);
409 int WordPartRight(int pos);
410 int ExtendStyleRange(int pos, int delta, bool singleLine = false);
411 bool IsWhiteLine(int line) const;
412 int ParaUp(int pos) const;
413 int ParaDown(int pos) const;
414 int IndentSize() const { return actualIndentInChars; }
415 int BraceMatch(int position, int maxReStyle);
417 private:
418 bool IsWordStartAt(int pos) const;
419 bool IsWordEndAt(int pos) const;
420 bool IsWordAt(int start, int end) const;
422 void NotifyModifyAttempt();
423 void NotifySavePoint(bool atSavePoint);
424 void NotifyModified(DocModification mh);
427 class UndoGroup {
428 Document *pdoc;
429 bool groupNeeded;
430 public:
431 UndoGroup(Document *pdoc_, bool groupNeeded_=true) :
432 pdoc(pdoc_), groupNeeded(groupNeeded_) {
433 if (groupNeeded) {
434 pdoc->BeginUndoAction();
437 ~UndoGroup() {
438 if (groupNeeded) {
439 pdoc->EndUndoAction();
442 bool Needed() const {
443 return groupNeeded;
449 * To optimise processing of document modifications by DocWatchers, a hint is passed indicating the
450 * scope of the change.
451 * If the DocWatcher is a document view then this can be used to optimise screen updating.
453 class DocModification {
454 public:
455 int modificationType;
456 int position;
457 int length;
458 int linesAdded; /**< Negative if lines deleted. */
459 const char *text; /**< Only valid for changes to text, not for changes to style. */
460 int line;
461 int foldLevelNow;
462 int foldLevelPrev;
463 int annotationLinesAdded;
464 int token;
466 DocModification(int modificationType_, int position_=0, int length_=0,
467 int linesAdded_=0, const char *text_=0, int line_=0) :
468 modificationType(modificationType_),
469 position(position_),
470 length(length_),
471 linesAdded(linesAdded_),
472 text(text_),
473 line(line_),
474 foldLevelNow(0),
475 foldLevelPrev(0),
476 annotationLinesAdded(0),
477 token(0) {}
479 DocModification(int modificationType_, const Action &act, int linesAdded_=0) :
480 modificationType(modificationType_),
481 position(act.position),
482 length(act.lenData),
483 linesAdded(linesAdded_),
484 text(act.data),
485 line(0),
486 foldLevelNow(0),
487 foldLevelPrev(0),
488 annotationLinesAdded(0),
489 token(0) {}
493 * A class that wants to receive notifications from a Document must be derived from DocWatcher
494 * and implement the notification methods. It can then be added to the watcher list with AddWatcher.
496 class DocWatcher {
497 public:
498 virtual ~DocWatcher() {}
500 virtual void NotifyModifyAttempt(Document *doc, void *userData) = 0;
501 virtual void NotifySavePoint(Document *doc, void *userData, bool atSavePoint) = 0;
502 virtual void NotifyModified(Document *doc, DocModification mh, void *userData) = 0;
503 virtual void NotifyDeleted(Document *doc, void *userData) = 0;
504 virtual void NotifyStyleNeeded(Document *doc, void *userData, int endPos) = 0;
505 virtual void NotifyLexerChanged(Document *doc, void *userData) = 0;
506 virtual void NotifyErrorOccurred(Document *doc, void *userData, int status) = 0;
509 #ifdef SCI_NAMESPACE
511 #endif
513 #endif