Update Scintilla to version 3.4.1
[geany-mirror.git] / scintilla / src / Document.h
blobeffdd5fe5529ef18773b62fe84524ac336c817fa
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 Valid() const {
43 return (start != invalidPosition) && (end != invalidPosition);
46 // Is the position within the range?
47 bool Contains(Position pos) const {
48 if (start < end) {
49 return (pos >= start && pos <= end);
50 } else {
51 return (pos <= start && pos >= end);
55 // Is the character after pos within the range?
56 bool ContainsCharacter(Position pos) const {
57 if (start < end) {
58 return (pos >= start && pos < end);
59 } else {
60 return (pos < start && pos >= end);
64 bool Contains(Range other) const {
65 return Contains(other.start) && Contains(other.end);
68 bool Overlaps(Range other) const {
69 return
70 Contains(other.start) ||
71 Contains(other.end) ||
72 other.Contains(start) ||
73 other.Contains(end);
77 class DocWatcher;
78 class DocModification;
79 class Document;
81 /**
82 * Interface class for regular expression searching
84 class RegexSearchBase {
85 public:
86 virtual ~RegexSearchBase() {}
88 virtual long FindText(Document *doc, int minPos, int maxPos, const char *s,
89 bool caseSensitive, bool word, bool wordStart, int flags, int *length) = 0;
91 ///@return String with the substitutions, must remain valid until the next call or destruction
92 virtual const char *SubstituteByPosition(Document *doc, const char *text, int *length) = 0;
95 /// Factory function for RegexSearchBase
96 extern RegexSearchBase *CreateRegexSearch(CharClassify *charClassTable);
98 struct StyledText {
99 size_t length;
100 const char *text;
101 bool multipleStyles;
102 size_t style;
103 const unsigned char *styles;
104 StyledText(size_t length_, const char *text_, bool multipleStyles_, int style_, const unsigned char *styles_) :
105 length(length_), text(text_), multipleStyles(multipleStyles_), style(style_), styles(styles_) {
107 // Return number of bytes from start to before '\n' or end of text.
108 // Return 1 when start is outside text
109 size_t LineLength(size_t start) const {
110 size_t cur = start;
111 while ((cur < length) && (text[cur] != '\n'))
112 cur++;
113 return cur-start;
115 size_t StyleAt(size_t i) const {
116 return multipleStyles ? styles[i] : style;
120 class HighlightDelimiter {
121 public:
122 HighlightDelimiter() : isEnabled(false) {
123 Clear();
126 void Clear() {
127 beginFoldBlock = -1;
128 endFoldBlock = -1;
129 firstChangeableLineBefore = -1;
130 firstChangeableLineAfter = -1;
133 bool NeedsDrawing(int line) const {
134 return isEnabled && (line <= firstChangeableLineBefore || line >= firstChangeableLineAfter);
137 bool IsFoldBlockHighlighted(int line) const {
138 return isEnabled && beginFoldBlock != -1 && beginFoldBlock <= line && line <= endFoldBlock;
141 bool IsHeadOfFoldBlock(int line) const {
142 return beginFoldBlock == line && line < endFoldBlock;
145 bool IsBodyOfFoldBlock(int line) const {
146 return beginFoldBlock != -1 && beginFoldBlock < line && line < endFoldBlock;
149 bool IsTailOfFoldBlock(int line) const {
150 return beginFoldBlock != -1 && beginFoldBlock < line && line == endFoldBlock;
153 int beginFoldBlock; // Begin of current fold block
154 int endFoldBlock; // End of current fold block
155 int firstChangeableLineBefore; // First line that triggers repaint before starting line that determined current fold block
156 int firstChangeableLineAfter; // First line that triggers repaint after starting line that determined current fold block
157 bool isEnabled;
160 class Document;
162 class LexInterface {
163 protected:
164 Document *pdoc;
165 ILexer *instance;
166 bool performingStyle; ///< Prevent reentrance
167 public:
168 explicit LexInterface(Document *pdoc_) : pdoc(pdoc_), instance(0), performingStyle(false) {
170 virtual ~LexInterface() {
172 void Colourise(int start, int end);
173 int LineEndTypesSupported();
174 bool UseContainerLexing() const {
175 return instance == 0;
181 class Document : PerLine, public IDocumentWithLineEnd, public ILoader {
183 public:
184 /** Used to pair watcher pointer with user data. */
185 struct WatcherWithUserData {
186 DocWatcher *watcher;
187 void *userData;
188 WatcherWithUserData(DocWatcher *watcher_=0, void *userData_=0) :
189 watcher(watcher_), userData(userData_) {
191 bool operator==(const WatcherWithUserData &other) const {
192 return (watcher == other.watcher) && (userData == other.userData);
196 private:
197 int refCount;
198 CellBuffer cb;
199 CharClassify charClass;
200 CaseFolder *pcf;
201 char stylingMask;
202 int endStyled;
203 int styleClock;
204 int enteredModification;
205 int enteredStyling;
206 int enteredReadOnlyCount;
208 std::vector<WatcherWithUserData> watchers;
210 // ldSize is not real data - it is for dimensions and loops
211 enum lineData { ldMarkers, ldLevels, ldState, ldMargin, ldAnnotation, ldSize };
212 PerLine *perLineData[ldSize];
214 bool matchesValid;
215 RegexSearchBase *regex;
217 public:
219 LexInterface *pli;
221 int stylingBits;
222 int stylingBitsMask;
224 int eolMode;
225 /// Can also be SC_CP_UTF8 to enable UTF-8 mode
226 int dbcsCodePage;
227 int lineEndBitSet;
228 int tabInChars;
229 int indentInChars;
230 int actualIndentInChars;
231 bool useTabs;
232 bool tabIndents;
233 bool backspaceUnindents;
235 DecorationList decorations;
237 Document();
238 virtual ~Document();
240 int AddRef();
241 int SCI_METHOD Release();
243 virtual void Init();
244 int LineEndTypesSupported() const;
245 bool SetDBCSCodePage(int dbcsCodePage_);
246 int GetLineEndTypesAllowed() const { return cb.GetLineEndTypes(); }
247 bool SetLineEndTypesAllowed(int lineEndBitSet_);
248 int GetLineEndTypesActive() const { return cb.GetLineEndTypes(); }
249 virtual void InsertLine(int line);
250 virtual void RemoveLine(int line);
252 int SCI_METHOD Version() const {
253 return dvLineEnd;
256 void SCI_METHOD SetErrorStatus(int status);
258 int SCI_METHOD LineFromPosition(int pos) const;
259 int ClampPositionIntoDocument(int pos) const;
260 bool IsCrLf(int pos) const;
261 int LenChar(int pos);
262 bool InGoodUTF8(int pos, int &start, int &end) const;
263 int MovePositionOutsideChar(int pos, int moveDir, bool checkLineEnd=true);
264 int NextPosition(int pos, int moveDir) const;
265 bool NextCharacter(int &pos, int moveDir) const; // Returns true if pos changed
266 int SCI_METHOD GetRelativePosition(int positionStart, int characterOffset) const;
267 int SCI_METHOD GetCharacterAndWidth(int position, int *pWidth) const;
268 int SCI_METHOD CodePage() const;
269 bool SCI_METHOD IsDBCSLeadByte(char ch) const;
270 int SafeSegment(const char *text, int length, int lengthSegment) const;
271 EncodingFamily CodePageFamily() const;
273 // Gateways to modifying document
274 void ModifiedAt(int pos);
275 void CheckReadOnly();
276 bool DeleteChars(int pos, int len);
277 bool InsertString(int position, const char *s, int insertLength);
278 int SCI_METHOD AddData(char *data, int length);
279 void * SCI_METHOD ConvertToDocument();
280 int Undo();
281 int Redo();
282 bool CanUndo() const { return cb.CanUndo(); }
283 bool CanRedo() const { return cb.CanRedo(); }
284 void DeleteUndoHistory() { cb.DeleteUndoHistory(); }
285 bool SetUndoCollection(bool collectUndo) {
286 return cb.SetUndoCollection(collectUndo);
288 bool IsCollectingUndo() const { return cb.IsCollectingUndo(); }
289 void BeginUndoAction() { cb.BeginUndoAction(); }
290 void EndUndoAction() { cb.EndUndoAction(); }
291 void AddUndoAction(int token, bool mayCoalesce) { cb.AddUndoAction(token, mayCoalesce); }
292 void SetSavePoint();
293 bool IsSavePoint() const { return cb.IsSavePoint(); }
294 const char * SCI_METHOD BufferPointer() { return cb.BufferPointer(); }
295 const char *RangePointer(int position, int rangeLength) { return cb.RangePointer(position, rangeLength); }
296 int GapPosition() const { return cb.GapPosition(); }
298 int SCI_METHOD GetLineIndentation(int line);
299 void SetLineIndentation(int line, int indent);
300 int GetLineIndentPosition(int line) const;
301 int GetColumn(int position);
302 int CountCharacters(int startPos, int endPos);
303 int FindColumn(int line, int column);
304 void Indent(bool forwards, int lineBottom, int lineTop);
305 static std::string TransformLineEnds(const char *s, size_t len, int eolModeWanted);
306 void ConvertLineEnds(int eolModeSet);
307 void SetReadOnly(bool set) { cb.SetReadOnly(set); }
308 bool IsReadOnly() const { return cb.IsReadOnly(); }
310 bool InsertChar(int pos, char ch);
311 bool InsertCString(int position, const char *s);
312 void DelChar(int pos);
313 void DelCharBack(int pos);
315 char CharAt(int position) const { return cb.CharAt(position); }
316 void SCI_METHOD GetCharRange(char *buffer, int position, int lengthRetrieve) const {
317 cb.GetCharRange(buffer, position, lengthRetrieve);
319 char SCI_METHOD StyleAt(int position) const { return cb.StyleAt(position); }
320 void GetStyleRange(unsigned char *buffer, int position, int lengthRetrieve) const {
321 cb.GetStyleRange(buffer, position, lengthRetrieve);
323 int GetMark(int line);
324 int MarkerNext(int lineStart, int mask) const;
325 int AddMark(int line, int markerNum);
326 void AddMarkSet(int line, int valueSet);
327 void DeleteMark(int line, int markerNum);
328 void DeleteMarkFromHandle(int markerHandle);
329 void DeleteAllMarks(int markerNum);
330 int LineFromHandle(int markerHandle);
331 int SCI_METHOD LineStart(int line) const;
332 int SCI_METHOD LineEnd(int line) const;
333 int LineEndPosition(int position) const;
334 bool IsLineEndPosition(int position) const;
335 bool IsPositionInLineEnd(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) const;
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 bool MatchesWordOptions(bool word, bool wordStart, int pos, int length) const;
352 bool HasCaseFolder(void) const;
353 void SetCaseFolder(CaseFolder *pcf_);
354 long FindText(int minPos, int maxPos, const char *search, bool caseSensitive, bool word,
355 bool wordStart, bool regExp, int flags, int *length);
356 const char *SubstituteByPosition(const char *text, int *length);
357 int LinesTotal() const;
359 void SetDefaultCharClasses(bool includeWordClass);
360 void SetCharClasses(const unsigned char *chars, CharClassify::cc newCharClass);
361 int GetCharsOfClass(CharClassify::cc charClass, unsigned char *buffer);
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() const { return endStyled; }
367 void EnsureStyledTo(int pos);
368 void LexerChanged();
369 int GetStyleClock() const { 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) const;
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 void MarginClearAll();
387 StyledText AnnotationStyledText(int line) const;
388 void AnnotationSetText(int line, const char *text);
389 void AnnotationSetStyle(int line, int style);
390 void AnnotationSetStyles(int line, const unsigned char *styles);
391 int AnnotationLines(int line) const;
392 void AnnotationClearAll();
394 bool AddWatcher(DocWatcher *watcher, void *userData);
395 bool RemoveWatcher(DocWatcher *watcher, void *userData);
397 CharClassify::cc WordCharClass(unsigned char ch) const;
398 bool IsWordPartSeparator(char ch) const;
399 int WordPartLeft(int pos);
400 int WordPartRight(int pos);
401 int ExtendStyleRange(int pos, int delta, bool singleLine = false);
402 bool IsWhiteLine(int line) const;
403 int ParaUp(int pos) const;
404 int ParaDown(int pos) const;
405 int IndentSize() const { return actualIndentInChars; }
406 int BraceMatch(int position, int maxReStyle);
408 private:
409 bool IsWordStartAt(int pos) const;
410 bool IsWordEndAt(int pos) const;
411 bool IsWordAt(int start, int end) const;
413 void NotifyModifyAttempt();
414 void NotifySavePoint(bool atSavePoint);
415 void NotifyModified(DocModification mh);
418 class UndoGroup {
419 Document *pdoc;
420 bool groupNeeded;
421 public:
422 UndoGroup(Document *pdoc_, bool groupNeeded_=true) :
423 pdoc(pdoc_), groupNeeded(groupNeeded_) {
424 if (groupNeeded) {
425 pdoc->BeginUndoAction();
428 ~UndoGroup() {
429 if (groupNeeded) {
430 pdoc->EndUndoAction();
433 bool Needed() const {
434 return groupNeeded;
440 * To optimise processing of document modifications by DocWatchers, a hint is passed indicating the
441 * scope of the change.
442 * If the DocWatcher is a document view then this can be used to optimise screen updating.
444 class DocModification {
445 public:
446 int modificationType;
447 int position;
448 int length;
449 int linesAdded; /**< Negative if lines deleted. */
450 const char *text; /**< Only valid for changes to text, not for changes to style. */
451 int line;
452 int foldLevelNow;
453 int foldLevelPrev;
454 int annotationLinesAdded;
455 int token;
457 DocModification(int modificationType_, int position_=0, int length_=0,
458 int linesAdded_=0, const char *text_=0, int line_=0) :
459 modificationType(modificationType_),
460 position(position_),
461 length(length_),
462 linesAdded(linesAdded_),
463 text(text_),
464 line(line_),
465 foldLevelNow(0),
466 foldLevelPrev(0),
467 annotationLinesAdded(0),
468 token(0) {}
470 DocModification(int modificationType_, const Action &act, int linesAdded_=0) :
471 modificationType(modificationType_),
472 position(act.position),
473 length(act.lenData),
474 linesAdded(linesAdded_),
475 text(act.data),
476 line(0),
477 foldLevelNow(0),
478 foldLevelPrev(0),
479 annotationLinesAdded(0),
480 token(0) {}
484 * A class that wants to receive notifications from a Document must be derived from DocWatcher
485 * and implement the notification methods. It can then be added to the watcher list with AddWatcher.
487 class DocWatcher {
488 public:
489 virtual ~DocWatcher() {}
491 virtual void NotifyModifyAttempt(Document *doc, void *userData) = 0;
492 virtual void NotifySavePoint(Document *doc, void *userData, bool atSavePoint) = 0;
493 virtual void NotifyModified(Document *doc, DocModification mh, void *userData) = 0;
494 virtual void NotifyDeleted(Document *doc, void *userData) = 0;
495 virtual void NotifyStyleNeeded(Document *doc, void *userData, int endPos) = 0;
496 virtual void NotifyLexerChanged(Document *doc, void *userData) = 0;
497 virtual void NotifyErrorOccurred(Document *doc, void *userData, int status) = 0;
500 #ifdef SCI_NAMESPACE
502 #endif
504 #endif