Update Scintilla to version 3.6.2
[geany-mirror.git] / scintilla / src / Document.h
blobea8ddfbed4d3795b7487180310717859a3c910ee
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;
191 struct RegexError : public std::runtime_error {
192 RegexError() : std::runtime_error("regex failure") {}
197 class Document : PerLine, public IDocumentWithLineEnd, public ILoader {
199 public:
200 /** Used to pair watcher pointer with user data. */
201 struct WatcherWithUserData {
202 DocWatcher *watcher;
203 void *userData;
204 WatcherWithUserData(DocWatcher *watcher_=0, void *userData_=0) :
205 watcher(watcher_), userData(userData_) {
207 bool operator==(const WatcherWithUserData &other) const {
208 return (watcher == other.watcher) && (userData == other.userData);
212 private:
213 int refCount;
214 CellBuffer cb;
215 CharClassify charClass;
216 CaseFolder *pcf;
217 int endStyled;
218 int styleClock;
219 int enteredModification;
220 int enteredStyling;
221 int enteredReadOnlyCount;
223 bool insertionSet;
224 std::string insertion;
226 std::vector<WatcherWithUserData> watchers;
228 // ldSize is not real data - it is for dimensions and loops
229 enum lineData { ldMarkers, ldLevels, ldState, ldMargin, ldAnnotation, ldSize };
230 PerLine *perLineData[ldSize];
232 bool matchesValid;
233 RegexSearchBase *regex;
235 public:
237 LexInterface *pli;
239 int eolMode;
240 /// Can also be SC_CP_UTF8 to enable UTF-8 mode
241 int dbcsCodePage;
242 int lineEndBitSet;
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 int LineEndTypesSupported() const;
260 bool SetDBCSCodePage(int dbcsCodePage_);
261 int GetLineEndTypesAllowed() const { return cb.GetLineEndTypes(); }
262 bool SetLineEndTypesAllowed(int lineEndBitSet_);
263 int GetLineEndTypesActive() const { return cb.GetLineEndTypes(); }
264 virtual void InsertLine(int line);
265 virtual void RemoveLine(int line);
267 int SCI_METHOD Version() const {
268 return dvLineEnd;
271 void SCI_METHOD SetErrorStatus(int status);
273 Sci_Position SCI_METHOD LineFromPosition(Sci_Position pos) const;
274 int ClampPositionIntoDocument(int pos) const;
275 bool IsCrLf(int pos) const;
276 int LenChar(int pos);
277 bool InGoodUTF8(int pos, int &start, int &end) const;
278 int MovePositionOutsideChar(int pos, int moveDir, bool checkLineEnd=true) const;
279 int NextPosition(int pos, int moveDir) const;
280 bool NextCharacter(int &pos, int moveDir) const; // Returns true if pos changed
281 Sci_Position SCI_METHOD GetRelativePosition(Sci_Position positionStart, Sci_Position characterOffset) const;
282 int GetRelativePositionUTF16(int positionStart, int characterOffset) const;
283 int SCI_METHOD GetCharacterAndWidth(Sci_Position position, Sci_Position *pWidth) const;
284 int SCI_METHOD CodePage() const;
285 bool SCI_METHOD IsDBCSLeadByte(char ch) const;
286 int SafeSegment(const char *text, int length, int lengthSegment) const;
287 EncodingFamily CodePageFamily() const;
289 // Gateways to modifying document
290 void ModifiedAt(int pos);
291 void CheckReadOnly();
292 bool DeleteChars(int pos, int len);
293 int InsertString(int position, const char *s, int insertLength);
294 void ChangeInsertion(const char *s, int length);
295 int SCI_METHOD AddData(char *data, Sci_Position length);
296 void * SCI_METHOD ConvertToDocument();
297 int Undo();
298 int Redo();
299 bool CanUndo() const { return cb.CanUndo(); }
300 bool CanRedo() const { return cb.CanRedo(); }
301 void DeleteUndoHistory() { cb.DeleteUndoHistory(); }
302 bool SetUndoCollection(bool collectUndo) {
303 return cb.SetUndoCollection(collectUndo);
305 bool IsCollectingUndo() const { return cb.IsCollectingUndo(); }
306 void BeginUndoAction() { cb.BeginUndoAction(); }
307 void EndUndoAction() { cb.EndUndoAction(); }
308 void AddUndoAction(int token, bool mayCoalesce) { cb.AddUndoAction(token, mayCoalesce); }
309 void SetSavePoint();
310 bool IsSavePoint() const { return cb.IsSavePoint(); }
312 void TentativeStart() { cb.TentativeStart(); }
313 void TentativeCommit() { cb.TentativeCommit(); }
314 void TentativeUndo();
315 bool TentativeActive() const { return cb.TentativeActive(); }
317 const char * SCI_METHOD BufferPointer() { return cb.BufferPointer(); }
318 const char *RangePointer(int position, int rangeLength) { return cb.RangePointer(position, rangeLength); }
319 int GapPosition() const { return cb.GapPosition(); }
321 int SCI_METHOD GetLineIndentation(Sci_Position line);
322 int SetLineIndentation(int line, int indent);
323 int GetLineIndentPosition(int line) const;
324 int GetColumn(int position);
325 int CountCharacters(int startPos, int endPos) const;
326 int CountUTF16(int startPos, int endPos) const;
327 int FindColumn(int line, int column);
328 void Indent(bool forwards, int lineBottom, int lineTop);
329 static std::string TransformLineEnds(const char *s, size_t len, int eolModeWanted);
330 void ConvertLineEnds(int eolModeSet);
331 void SetReadOnly(bool set) { cb.SetReadOnly(set); }
332 bool IsReadOnly() const { return cb.IsReadOnly(); }
334 void DelChar(int pos);
335 void DelCharBack(int pos);
337 char CharAt(int position) const { return cb.CharAt(position); }
338 void SCI_METHOD GetCharRange(char *buffer, Sci_Position position, Sci_Position lengthRetrieve) const {
339 cb.GetCharRange(buffer, position, lengthRetrieve);
341 char SCI_METHOD StyleAt(Sci_Position position) const { return cb.StyleAt(position); }
342 void GetStyleRange(unsigned char *buffer, int position, int lengthRetrieve) const {
343 cb.GetStyleRange(buffer, position, lengthRetrieve);
345 int GetMark(int line);
346 int MarkerNext(int lineStart, int mask) const;
347 int AddMark(int line, int markerNum);
348 void AddMarkSet(int line, int valueSet);
349 void DeleteMark(int line, int markerNum);
350 void DeleteMarkFromHandle(int markerHandle);
351 void DeleteAllMarks(int markerNum);
352 int LineFromHandle(int markerHandle);
353 Sci_Position SCI_METHOD LineStart(Sci_Position line) const;
354 bool IsLineStartPosition(int position) const;
355 Sci_Position SCI_METHOD LineEnd(Sci_Position line) const;
356 int LineEndPosition(int position) const;
357 bool IsLineEndPosition(int position) const;
358 bool IsPositionInLineEnd(int position) const;
359 int VCHomePosition(int position) const;
361 int SCI_METHOD SetLevel(Sci_Position line, int level);
362 int SCI_METHOD GetLevel(Sci_Position line) const;
363 void ClearLevels();
364 int GetLastChild(int lineParent, int level=-1, int lastLine=-1);
365 int GetFoldParent(int line) const;
366 void GetHighlightDelimiters(HighlightDelimiter &hDelimiter, int line, int lastLine);
368 void Indent(bool forwards);
369 int ExtendWordSelect(int pos, int delta, bool onlyWordCharacters=false);
370 int NextWordStart(int pos, int delta);
371 int NextWordEnd(int pos, int delta);
372 Sci_Position SCI_METHOD Length() const { return cb.Length(); }
373 void Allocate(int newSize) { cb.Allocate(newSize); }
375 struct CharacterExtracted {
376 unsigned int character;
377 unsigned int widthBytes;
378 CharacterExtracted(unsigned int character_, unsigned int widthBytes_) :
379 character(character_), widthBytes(widthBytes_) {
382 CharacterExtracted ExtractCharacter(int position) const;
384 bool IsWordStartAt(int pos) const;
385 bool IsWordEndAt(int pos) const;
386 bool IsWordAt(int start, int end) const;
388 bool MatchesWordOptions(bool word, bool wordStart, int pos, int length) const;
389 bool HasCaseFolder(void) const;
390 void SetCaseFolder(CaseFolder *pcf_);
391 long FindText(int minPos, int maxPos, const char *search, int flags, int *length);
392 const char *SubstituteByPosition(const char *text, int *length);
393 int LinesTotal() const;
395 void SetDefaultCharClasses(bool includeWordClass);
396 void SetCharClasses(const unsigned char *chars, CharClassify::cc newCharClass);
397 int GetCharsOfClass(CharClassify::cc characterClass, unsigned char *buffer);
398 void SCI_METHOD StartStyling(Sci_Position position, char mask);
399 bool SCI_METHOD SetStyleFor(Sci_Position length, char style);
400 bool SCI_METHOD SetStyles(Sci_Position length, const char *styles);
401 int GetEndStyled() const { return endStyled; }
402 void EnsureStyledTo(int pos);
403 void LexerChanged();
404 int GetStyleClock() const { return styleClock; }
405 void IncrementStyleClock();
406 void SCI_METHOD DecorationSetCurrentIndicator(int indicator) {
407 decorations.SetCurrentIndicator(indicator);
409 void SCI_METHOD DecorationFillRange(Sci_Position position, int value, Sci_Position fillLength);
411 int SCI_METHOD SetLineState(Sci_Position line, int state);
412 int SCI_METHOD GetLineState(Sci_Position line) const;
413 int GetMaxLineState();
414 void SCI_METHOD ChangeLexerState(Sci_Position start, Sci_Position end);
416 StyledText MarginStyledText(int line) const;
417 void MarginSetStyle(int line, int style);
418 void MarginSetStyles(int line, const unsigned char *styles);
419 void MarginSetText(int line, const char *text);
420 void MarginClearAll();
422 StyledText AnnotationStyledText(int line) const;
423 void AnnotationSetText(int line, const char *text);
424 void AnnotationSetStyle(int line, int style);
425 void AnnotationSetStyles(int line, const unsigned char *styles);
426 int AnnotationLines(int line) const;
427 void AnnotationClearAll();
429 bool AddWatcher(DocWatcher *watcher, void *userData);
430 bool RemoveWatcher(DocWatcher *watcher, void *userData);
432 CharClassify::cc WordCharClass(unsigned char ch) const;
433 bool IsWordPartSeparator(char ch) const;
434 int WordPartLeft(int pos);
435 int WordPartRight(int pos);
436 int ExtendStyleRange(int pos, int delta, bool singleLine = false);
437 bool IsWhiteLine(int line) const;
438 int ParaUp(int pos) const;
439 int ParaDown(int pos) const;
440 int IndentSize() const { return actualIndentInChars; }
441 int BraceMatch(int position, int maxReStyle);
443 private:
444 void NotifyModifyAttempt();
445 void NotifySavePoint(bool atSavePoint);
446 void NotifyModified(DocModification mh);
449 class UndoGroup {
450 Document *pdoc;
451 bool groupNeeded;
452 public:
453 UndoGroup(Document *pdoc_, bool groupNeeded_=true) :
454 pdoc(pdoc_), groupNeeded(groupNeeded_) {
455 if (groupNeeded) {
456 pdoc->BeginUndoAction();
459 ~UndoGroup() {
460 if (groupNeeded) {
461 pdoc->EndUndoAction();
464 bool Needed() const {
465 return groupNeeded;
471 * To optimise processing of document modifications by DocWatchers, a hint is passed indicating the
472 * scope of the change.
473 * If the DocWatcher is a document view then this can be used to optimise screen updating.
475 class DocModification {
476 public:
477 int modificationType;
478 int position;
479 int length;
480 int linesAdded; /**< Negative if lines deleted. */
481 const char *text; /**< Only valid for changes to text, not for changes to style. */
482 int line;
483 int foldLevelNow;
484 int foldLevelPrev;
485 int annotationLinesAdded;
486 int token;
488 DocModification(int modificationType_, int position_=0, int length_=0,
489 int linesAdded_=0, const char *text_=0, int line_=0) :
490 modificationType(modificationType_),
491 position(position_),
492 length(length_),
493 linesAdded(linesAdded_),
494 text(text_),
495 line(line_),
496 foldLevelNow(0),
497 foldLevelPrev(0),
498 annotationLinesAdded(0),
499 token(0) {}
501 DocModification(int modificationType_, const Action &act, int linesAdded_=0) :
502 modificationType(modificationType_),
503 position(act.position),
504 length(act.lenData),
505 linesAdded(linesAdded_),
506 text(act.data),
507 line(0),
508 foldLevelNow(0),
509 foldLevelPrev(0),
510 annotationLinesAdded(0),
511 token(0) {}
515 * A class that wants to receive notifications from a Document must be derived from DocWatcher
516 * and implement the notification methods. It can then be added to the watcher list with AddWatcher.
518 class DocWatcher {
519 public:
520 virtual ~DocWatcher() {}
522 virtual void NotifyModifyAttempt(Document *doc, void *userData) = 0;
523 virtual void NotifySavePoint(Document *doc, void *userData, bool atSavePoint) = 0;
524 virtual void NotifyModified(Document *doc, DocModification mh, void *userData) = 0;
525 virtual void NotifyDeleted(Document *doc, void *userData) = 0;
526 virtual void NotifyStyleNeeded(Document *doc, void *userData, int endPos) = 0;
527 virtual void NotifyLexerChanged(Document *doc, void *userData) = 0;
528 virtual void NotifyErrorOccurred(Document *doc, void *userData, int status) = 0;
531 #ifdef SCI_NAMESPACE
533 #endif
535 #endif