updated Scintilla to 2.29
[TortoiseGit.git] / ext / scintilla / src / Document.h
blob1816bea44244e3ee71db79878bb2137f4ffa000a
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 {
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 char stylingMask;
216 int endStyled;
217 int styleClock;
218 int enteredModification;
219 int enteredStyling;
220 int enteredReadOnlyCount;
222 WatcherWithUserData *watchers;
223 int lenWatchers;
225 // ldSize is not real data - it is for dimensions and loops
226 enum lineData { ldMarkers, ldLevels, ldState, ldMargin, ldAnnotation, ldSize };
227 PerLine *perLineData[ldSize];
229 bool matchesValid;
230 RegexSearchBase *regex;
232 public:
234 LexInterface *pli;
236 int stylingBits;
237 int stylingBitsMask;
239 int eolMode;
240 /// Can also be SC_CP_UTF8 to enable UTF-8 mode
241 int dbcsCodePage;
242 int tabInChars;
243 int indentInChars;
244 int actualIndentInChars;
245 bool useTabs;
246 bool tabIndents;
247 bool backspaceUnindents;
249 DecorationList decorations;
251 Document();
252 virtual ~Document();
254 int AddRef();
255 int Release();
257 virtual void Init();
258 virtual void InsertLine(int line);
259 virtual void RemoveLine(int line);
261 int SCI_METHOD Version() const {
262 return dvOriginal;
265 void SCI_METHOD SetErrorStatus(int status);
267 int SCI_METHOD LineFromPosition(int pos) const;
268 int ClampPositionIntoDocument(int pos);
269 bool IsCrLf(int pos);
270 int LenChar(int pos);
271 bool InGoodUTF8(int pos, int &start, int &end) const;
272 int MovePositionOutsideChar(int pos, int moveDir, bool checkLineEnd=true);
273 int NextPosition(int pos, int moveDir) const;
274 bool NextCharacter(int &pos, int moveDir); // Returns true if pos changed
275 int SCI_METHOD CodePage() const;
276 bool SCI_METHOD IsDBCSLeadByte(char ch) const;
277 int SafeSegment(const char *text, int length, int lengthSegment);
279 // Gateways to modifying document
280 void ModifiedAt(int pos);
281 void CheckReadOnly();
282 bool DeleteChars(int pos, int len);
283 bool InsertString(int position, const char *s, int insertLength);
284 int Undo();
285 int Redo();
286 bool CanUndo() { return cb.CanUndo(); }
287 bool CanRedo() { return cb.CanRedo(); }
288 void DeleteUndoHistory() { cb.DeleteUndoHistory(); }
289 bool SetUndoCollection(bool collectUndo) {
290 return cb.SetUndoCollection(collectUndo);
292 bool IsCollectingUndo() { return cb.IsCollectingUndo(); }
293 void BeginUndoAction() { cb.BeginUndoAction(); }
294 void EndUndoAction() { cb.EndUndoAction(); }
295 void AddUndoAction(int token, bool mayCoalesce) { cb.AddUndoAction(token, mayCoalesce); }
296 void SetSavePoint();
297 bool IsSavePoint() { return cb.IsSavePoint(); }
298 const char * SCI_METHOD BufferPointer() { return cb.BufferPointer(); }
300 int SCI_METHOD GetLineIndentation(int line);
301 void SetLineIndentation(int line, int indent);
302 int GetLineIndentPosition(int line) const;
303 int GetColumn(int position);
304 int FindColumn(int line, int column);
305 void Indent(bool forwards, int lineBottom, int lineTop);
306 static char *TransformLineEnds(int *pLenOut, const char *s, size_t len, int eolModeWanted);
307 void ConvertLineEnds(int eolModeSet);
308 void SetReadOnly(bool set) { cb.SetReadOnly(set); }
309 bool IsReadOnly() { return cb.IsReadOnly(); }
311 bool InsertChar(int pos, char ch);
312 bool InsertCString(int position, const char *s);
313 void ChangeChar(int pos, char ch);
314 void DelChar(int pos);
315 void DelCharBack(int pos);
317 char CharAt(int position) { return cb.CharAt(position); }
318 void SCI_METHOD GetCharRange(char *buffer, int position, int lengthRetrieve) const {
319 cb.GetCharRange(buffer, position, lengthRetrieve);
321 char SCI_METHOD StyleAt(int position) const { return cb.StyleAt(position); }
322 void GetStyleRange(unsigned char *buffer, int position, int lengthRetrieve) const {
323 cb.GetStyleRange(buffer, position, lengthRetrieve);
325 int GetMark(int line);
326 int AddMark(int line, int markerNum);
327 void AddMarkSet(int line, int valueSet);
328 void DeleteMark(int line, int markerNum);
329 void DeleteMarkFromHandle(int markerHandle);
330 void DeleteAllMarks(int markerNum);
331 int LineFromHandle(int markerHandle);
332 int SCI_METHOD LineStart(int line) const;
333 int LineEnd(int line) const;
334 int LineEndPosition(int position) const;
335 bool IsLineEndPosition(int position) const;
336 int VCHomePosition(int position) const;
338 int SCI_METHOD SetLevel(int line, int level);
339 int SCI_METHOD GetLevel(int line) const;
340 void ClearLevels();
341 int GetLastChild(int lineParent, int level=-1, int lastLine=-1);
342 int GetFoldParent(int line);
343 void GetHighlightDelimiters(HighlightDelimiter &hDelimiter, int line, int lastLine);
345 void Indent(bool forwards);
346 int ExtendWordSelect(int pos, int delta, bool onlyWordCharacters=false);
347 int NextWordStart(int pos, int delta);
348 int NextWordEnd(int pos, int delta);
349 int SCI_METHOD Length() const { return cb.Length(); }
350 void Allocate(int newSize) { cb.Allocate(newSize); }
351 size_t ExtractChar(int pos, char *bytes);
352 bool MatchesWordOptions(bool word, bool wordStart, int pos, int length);
353 long FindText(int minPos, int maxPos, const char *search, bool caseSensitive, bool word,
354 bool wordStart, bool regExp, int flags, int *length, CaseFolder *pcf);
355 const char *SubstituteByPosition(const char *text, int *length);
356 int LinesTotal() const;
358 void ChangeCase(Range r, bool makeUpperCase);
360 void SetDefaultCharClasses(bool includeWordClass);
361 void SetCharClasses(const unsigned char *chars, CharClassify::cc newCharClass);
362 void SetStylingBits(int bits);
363 void SCI_METHOD StartStyling(int position, char mask);
364 bool SCI_METHOD SetStyleFor(int length, char style);
365 bool SCI_METHOD SetStyles(int length, const char *styles);
366 int GetEndStyled() { return endStyled; }
367 void EnsureStyledTo(int pos);
368 void LexerChanged();
369 int GetStyleClock() { return styleClock; }
370 void IncrementStyleClock();
371 void SCI_METHOD DecorationSetCurrentIndicator(int indicator) {
372 decorations.SetCurrentIndicator(indicator);
374 void SCI_METHOD DecorationFillRange(int position, int value, int fillLength);
376 int SCI_METHOD SetLineState(int line, int state);
377 int SCI_METHOD GetLineState(int line) const;
378 int GetMaxLineState();
379 void SCI_METHOD ChangeLexerState(int start, int end);
381 StyledText MarginStyledText(int line);
382 void MarginSetStyle(int line, int style);
383 void MarginSetStyles(int line, const unsigned char *styles);
384 void MarginSetText(int line, const char *text);
385 int MarginLength(int line) const;
386 void MarginClearAll();
388 bool AnnotationAny() const;
389 StyledText AnnotationStyledText(int line);
390 void AnnotationSetText(int line, const char *text);
391 void AnnotationSetStyle(int line, int style);
392 void AnnotationSetStyles(int line, const unsigned char *styles);
393 int AnnotationLength(int line) const;
394 int AnnotationLines(int line) const;
395 void AnnotationClearAll();
397 bool AddWatcher(DocWatcher *watcher, void *userData);
398 bool RemoveWatcher(DocWatcher *watcher, void *userData);
399 const WatcherWithUserData *GetWatchers() const { return watchers; }
400 int GetLenWatchers() const { return lenWatchers; }
402 CharClassify::cc WordCharClass(unsigned char ch);
403 bool IsWordPartSeparator(char ch);
404 int WordPartLeft(int pos);
405 int WordPartRight(int pos);
406 int ExtendStyleRange(int pos, int delta, bool singleLine = false);
407 bool IsWhiteLine(int line) const;
408 int ParaUp(int pos);
409 int ParaDown(int pos);
410 int IndentSize() { return actualIndentInChars; }
411 int BraceMatch(int position, int maxReStyle);
413 private:
414 bool IsWordStartAt(int pos);
415 bool IsWordEndAt(int pos);
416 bool IsWordAt(int start, int end);
418 void NotifyModifyAttempt();
419 void NotifySavePoint(bool atSavePoint);
420 void NotifyModified(DocModification mh);
423 class UndoGroup {
424 Document *pdoc;
425 bool groupNeeded;
426 public:
427 UndoGroup(Document *pdoc_, bool groupNeeded_=true) :
428 pdoc(pdoc_), groupNeeded(groupNeeded_) {
429 if (groupNeeded) {
430 pdoc->BeginUndoAction();
433 ~UndoGroup() {
434 if (groupNeeded) {
435 pdoc->EndUndoAction();
438 bool Needed() const {
439 return groupNeeded;
445 * To optimise processing of document modifications by DocWatchers, a hint is passed indicating the
446 * scope of the change.
447 * If the DocWatcher is a document view then this can be used to optimise screen updating.
449 class DocModification {
450 public:
451 int modificationType;
452 int position;
453 int length;
454 int linesAdded; /**< Negative if lines deleted. */
455 const char *text; /**< Only valid for changes to text, not for changes to style. */
456 int line;
457 int foldLevelNow;
458 int foldLevelPrev;
459 int annotationLinesAdded;
460 int token;
462 DocModification(int modificationType_, int position_=0, int length_=0,
463 int linesAdded_=0, const char *text_=0, int line_=0) :
464 modificationType(modificationType_),
465 position(position_),
466 length(length_),
467 linesAdded(linesAdded_),
468 text(text_),
469 line(line_),
470 foldLevelNow(0),
471 foldLevelPrev(0),
472 annotationLinesAdded(0),
473 token(0) {}
475 DocModification(int modificationType_, const Action &act, int linesAdded_=0) :
476 modificationType(modificationType_),
477 position(act.position),
478 length(act.lenData),
479 linesAdded(linesAdded_),
480 text(act.data),
481 line(0),
482 foldLevelNow(0),
483 foldLevelPrev(0),
484 annotationLinesAdded(0),
485 token(0) {}
489 * A class that wants to receive notifications from a Document must be derived from DocWatcher
490 * and implement the notification methods. It can then be added to the watcher list with AddWatcher.
492 class DocWatcher {
493 public:
494 virtual ~DocWatcher() {}
496 virtual void NotifyModifyAttempt(Document *doc, void *userData) = 0;
497 virtual void NotifySavePoint(Document *doc, void *userData, bool atSavePoint) = 0;
498 virtual void NotifyModified(Document *doc, DocModification mh, void *userData) = 0;
499 virtual void NotifyDeleted(Document *doc, void *userData) = 0;
500 virtual void NotifyStyleNeeded(Document *doc, void *userData, int endPos) = 0;
501 virtual void NotifyLexerChanged(Document *doc, void *userData) = 0;
502 virtual void NotifyErrorOccurred(Document *doc, void *userData, int status) = 0;
505 #ifdef SCI_NAMESPACE
507 #endif
509 #endif