scintilla: Update scintilla with changeset 3662:1d1c06df8a2f using gtk+3
[anjuta-extras.git] / plugins / scintilla / scintilla / Document.h
blob7858db7273eda50dc872442f71955aca9a30d65b
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() {
121 beginFoldBlock = -1;
122 endFoldBlock = -1;
123 beginMarginCorrectlyDrawnZone = -1;
124 endMarginCorrectlyDrawnZone = -1;
125 isEnabled = false;
128 bool NeedsDrawing(int line) {
129 return isEnabled && (line <= beginMarginCorrectlyDrawnZone || endMarginCorrectlyDrawnZone <= line);
132 bool isCurrentBlockHighlight(int line) {
133 return isEnabled && beginFoldBlock != -1 && beginFoldBlock <= line && line <= endFoldBlock;
136 bool isHeadBlockFold(int line) {
137 return beginFoldBlock == line && line < endFoldBlock;
140 bool isBodyBlockFold(int line) {
141 return beginFoldBlock != -1 && beginFoldBlock < line && line < endFoldBlock;
144 bool isTailBlockFold(int line) {
145 return beginFoldBlock != -1 && beginFoldBlock < line && line == endFoldBlock;
148 // beginFoldBlock : Begin of current fold block.
149 // endStartBlock : End of current fold block.
150 // beginMarginCorrectlyDrawnZone : Begin of zone where margin is correctly drawn.
151 // endMarginCorrectlyDrawnZone : End of zone where margin is correctly drawn.
152 int beginFoldBlock;
153 int endFoldBlock;
154 int beginMarginCorrectlyDrawnZone;
155 int endMarginCorrectlyDrawnZone;
156 bool isEnabled;
159 class CaseFolder {
160 public:
161 virtual ~CaseFolder() {
163 virtual size_t Fold(char *folded, size_t sizeFolded, const char *mixed, size_t lenMixed) = 0;
166 class CaseFolderTable : public CaseFolder {
167 protected:
168 char mapping[256];
169 public:
170 CaseFolderTable();
171 virtual ~CaseFolderTable();
172 virtual size_t Fold(char *folded, size_t sizeFolded, const char *mixed, size_t lenMixed);
173 void SetTranslation(char ch, char chTranslation);
174 void StandardASCII();
177 class Document;
179 class LexInterface {
180 protected:
181 Document *pdoc;
182 ILexer *instance;
183 bool performingStyle; ///< Prevent reentrance
184 public:
185 LexInterface(Document *pdoc_) : pdoc(pdoc_), instance(0), performingStyle(false) {
187 virtual ~LexInterface() {
189 void Colourise(int start, int end);
190 bool UseContainerLexing() const {
191 return instance == 0;
197 class Document : PerLine, public IDocument {
199 public:
200 /** Used to pair watcher pointer with user data. */
201 class WatcherWithUserData {
202 public:
203 DocWatcher *watcher;
204 void *userData;
205 WatcherWithUserData() {
206 watcher = 0;
207 userData = 0;
211 enum charClassification { ccSpace, ccNewLine, ccWord, ccPunctuation };
212 private:
213 int refCount;
214 CellBuffer cb;
215 CharClassify charClass;
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 Release();
258 virtual void Init();
259 virtual void InsertLine(int line);
260 virtual void RemoveLine(int line);
262 int SCI_METHOD Version() const {
263 return dvOriginal;
266 void SCI_METHOD SetErrorStatus(int status);
268 int SCI_METHOD LineFromPosition(int pos) const;
269 int ClampPositionIntoDocument(int pos);
270 bool IsCrLf(int pos);
271 int LenChar(int pos);
272 bool InGoodUTF8(int pos, int &start, int &end) const;
273 int MovePositionOutsideChar(int pos, int moveDir, bool checkLineEnd=true);
274 int NextPosition(int pos, int moveDir) const;
275 bool NextCharacter(int &pos, int moveDir); // Returns true if pos changed
276 int SCI_METHOD CodePage() const;
277 bool SCI_METHOD IsDBCSLeadByte(char ch) const;
278 int SafeSegment(const char *text, int length, int lengthSegment);
280 // Gateways to modifying document
281 void ModifiedAt(int pos);
282 void CheckReadOnly();
283 bool DeleteChars(int pos, int len);
284 bool InsertString(int position, const char *s, int insertLength);
285 int Undo();
286 int Redo();
287 bool CanUndo() { return cb.CanUndo(); }
288 bool CanRedo() { return cb.CanRedo(); }
289 void DeleteUndoHistory() { cb.DeleteUndoHistory(); }
290 bool SetUndoCollection(bool collectUndo) {
291 return cb.SetUndoCollection(collectUndo);
293 bool IsCollectingUndo() { return cb.IsCollectingUndo(); }
294 void BeginUndoAction() { cb.BeginUndoAction(); }
295 void EndUndoAction() { cb.EndUndoAction(); }
296 void AddUndoAction(int token, bool mayCoalesce) { cb.AddUndoAction(token, mayCoalesce); }
297 void SetSavePoint();
298 bool IsSavePoint() { return cb.IsSavePoint(); }
299 const char * SCI_METHOD BufferPointer() { return cb.BufferPointer(); }
301 int SCI_METHOD GetLineIndentation(int line);
302 void SetLineIndentation(int line, int indent);
303 int GetLineIndentPosition(int line) const;
304 int GetColumn(int position);
305 int FindColumn(int line, int column);
306 void Indent(bool forwards, int lineBottom, int lineTop);
307 static char *TransformLineEnds(int *pLenOut, const char *s, size_t len, int eolMode);
308 void ConvertLineEnds(int eolModeSet);
309 void SetReadOnly(bool set) { cb.SetReadOnly(set); }
310 bool IsReadOnly() { return cb.IsReadOnly(); }
312 bool InsertChar(int pos, char ch);
313 bool InsertCString(int position, const char *s);
314 void ChangeChar(int pos, char ch);
315 void DelChar(int pos);
316 void DelCharBack(int pos);
318 char CharAt(int position) { return cb.CharAt(position); }
319 void SCI_METHOD GetCharRange(char *buffer, int position, int lengthRetrieve) const {
320 cb.GetCharRange(buffer, position, lengthRetrieve);
322 char SCI_METHOD StyleAt(int position) const { return cb.StyleAt(position); }
323 void GetStyleRange(unsigned char *buffer, int position, int lengthRetrieve) const {
324 cb.GetStyleRange(buffer, position, lengthRetrieve);
326 int GetMark(int line);
327 int AddMark(int line, int markerNum);
328 void AddMarkSet(int line, int valueSet);
329 void DeleteMark(int line, int markerNum);
330 void DeleteMarkFromHandle(int markerHandle);
331 void DeleteAllMarks(int markerNum);
332 int LineFromHandle(int markerHandle);
333 int SCI_METHOD LineStart(int line) const;
334 int LineEnd(int line) const;
335 int LineEndPosition(int position) const;
336 bool IsLineEndPosition(int position) const;
337 int VCHomePosition(int position) const;
339 int SCI_METHOD SetLevel(int line, int level);
340 int SCI_METHOD GetLevel(int line) const;
341 void ClearLevels();
342 int GetLastChild(int lineParent, int level=-1);
343 int GetFoldParent(int line);
344 void GetHighlightDelimiters(HighlightDelimiter &hDelimiter, int line, int topLine, int bottomLine);
346 void Indent(bool forwards);
347 int ExtendWordSelect(int pos, int delta, bool onlyWordCharacters=false);
348 int NextWordStart(int pos, int delta);
349 int NextWordEnd(int pos, int delta);
350 int SCI_METHOD Length() const { return cb.Length(); }
351 void Allocate(int newSize) { cb.Allocate(newSize); }
352 size_t ExtractChar(int pos, char *bytes);
353 bool MatchesWordOptions(bool word, bool wordStart, int pos, int length);
354 long FindText(int minPos, int maxPos, const char *search, bool caseSensitive, bool word,
355 bool wordStart, bool regExp, int flags, int *length, CaseFolder *pcf);
356 const char *SubstituteByPosition(const char *text, int *length);
357 int LinesTotal() const;
359 void ChangeCase(Range r, bool makeUpperCase);
361 void SetDefaultCharClasses(bool includeWordClass);
362 void SetCharClasses(const unsigned char *chars, CharClassify::cc newCharClass);
363 void SetStylingBits(int bits);
364 void SCI_METHOD StartStyling(int position, char mask);
365 bool SCI_METHOD SetStyleFor(int length, char style);
366 bool SCI_METHOD SetStyles(int length, const char *styles);
367 int GetEndStyled() { return endStyled; }
368 void EnsureStyledTo(int pos);
369 void LexerChanged();
370 int GetStyleClock() { return styleClock; }
371 void IncrementStyleClock();
372 void SCI_METHOD DecorationSetCurrentIndicator(int indicator) {
373 decorations.SetCurrentIndicator(indicator);
375 void SCI_METHOD DecorationFillRange(int position, int value, int fillLength);
377 int SCI_METHOD SetLineState(int line, int state);
378 int SCI_METHOD GetLineState(int line) const;
379 int GetMaxLineState();
380 void SCI_METHOD ChangeLexerState(int start, int end);
382 StyledText MarginStyledText(int line);
383 void MarginSetStyle(int line, int style);
384 void MarginSetStyles(int line, const unsigned char *styles);
385 void MarginSetText(int line, const char *text);
386 int MarginLength(int line) const;
387 void MarginClearAll();
389 bool AnnotationAny() const;
390 StyledText AnnotationStyledText(int line);
391 void AnnotationSetText(int line, const char *text);
392 void AnnotationSetStyle(int line, int style);
393 void AnnotationSetStyles(int line, const unsigned char *styles);
394 int AnnotationLength(int line) const;
395 int AnnotationLines(int line) const;
396 void AnnotationClearAll();
398 bool AddWatcher(DocWatcher *watcher, void *userData);
399 bool RemoveWatcher(DocWatcher *watcher, void *userData);
400 const WatcherWithUserData *GetWatchers() const { return watchers; }
401 int GetLenWatchers() const { return lenWatchers; }
403 CharClassify::cc WordCharClass(unsigned char ch);
404 bool IsWordPartSeparator(char ch);
405 int WordPartLeft(int pos);
406 int WordPartRight(int pos);
407 int ExtendStyleRange(int pos, int delta, bool singleLine = false);
408 bool IsWhiteLine(int line) const;
409 int ParaUp(int pos);
410 int ParaDown(int pos);
411 int IndentSize() { return actualIndentInChars; }
412 int BraceMatch(int position, int maxReStyle);
414 private:
415 bool IsWordStartAt(int pos);
416 bool IsWordEndAt(int pos);
417 bool IsWordAt(int start, int end);
419 void NotifyModifyAttempt();
420 void NotifySavePoint(bool atSavePoint);
421 void NotifyModified(DocModification mh);
424 class UndoGroup {
425 Document *pdoc;
426 bool groupNeeded;
427 public:
428 UndoGroup(Document *pdoc_, bool groupNeeded_=true) :
429 pdoc(pdoc_), groupNeeded(groupNeeded_) {
430 if (groupNeeded) {
431 pdoc->BeginUndoAction();
434 ~UndoGroup() {
435 if (groupNeeded) {
436 pdoc->EndUndoAction();
439 bool Needed() const {
440 return groupNeeded;
446 * To optimise processing of document modifications by DocWatchers, a hint is passed indicating the
447 * scope of the change.
448 * If the DocWatcher is a document view then this can be used to optimise screen updating.
450 class DocModification {
451 public:
452 int modificationType;
453 int position;
454 int length;
455 int linesAdded; /**< Negative if lines deleted. */
456 const char *text; /**< Only valid for changes to text, not for changes to style. */
457 int line;
458 int foldLevelNow;
459 int foldLevelPrev;
460 int annotationLinesAdded;
461 int token;
463 DocModification(int modificationType_, int position_=0, int length_=0,
464 int linesAdded_=0, const char *text_=0, int line_=0) :
465 modificationType(modificationType_),
466 position(position_),
467 length(length_),
468 linesAdded(linesAdded_),
469 text(text_),
470 line(line_),
471 foldLevelNow(0),
472 foldLevelPrev(0),
473 annotationLinesAdded(0),
474 token(0) {}
476 DocModification(int modificationType_, const Action &act, int linesAdded_=0) :
477 modificationType(modificationType_),
478 position(act.position),
479 length(act.lenData),
480 linesAdded(linesAdded_),
481 text(act.data),
482 line(0),
483 foldLevelNow(0),
484 foldLevelPrev(0),
485 annotationLinesAdded(0),
486 token(0) {}
490 * A class that wants to receive notifications from a Document must be derived from DocWatcher
491 * and implement the notification methods. It can then be added to the watcher list with AddWatcher.
493 class DocWatcher {
494 public:
495 virtual ~DocWatcher() {}
497 virtual void NotifyModifyAttempt(Document *doc, void *userData) = 0;
498 virtual void NotifySavePoint(Document *doc, void *userData, bool atSavePoint) = 0;
499 virtual void NotifyModified(Document *doc, DocModification mh, void *userData) = 0;
500 virtual void NotifyDeleted(Document *doc, void *userData) = 0;
501 virtual void NotifyStyleNeeded(Document *doc, void *userData, int endPos) = 0;
502 virtual void NotifyLexerChanged(Document *doc, void *userData) = 0;
503 virtual void NotifyErrorOccurred(Document *doc, void *userData, int status) = 0;
506 #ifdef SCI_NAMESPACE
508 #endif
510 #endif