]>
Commit | Line | Data |
---|---|---|
9ce192d4 | 1 | // Scintilla source code edit control |
65ec6247 RD |
2 | /** @file Document.h |
3 | ** Text document that handles notifications, DBCS, styling, words and end of line. | |
4 | **/ | |
9e730a78 | 5 | // Copyright 1998-2003 by Neil Hodgson <neilh@scintilla.org> |
9ce192d4 RD |
6 | // The License.txt file describes the conditions under which this software may be distributed. |
7 | ||
8 | #ifndef DOCUMENT_H | |
9 | #define DOCUMENT_H | |
10 | ||
7e0c58e9 RD |
11 | #ifdef SCI_NAMESPACE |
12 | namespace Scintilla { | |
13 | #endif | |
14 | ||
65ec6247 RD |
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. | |
18 | */ | |
9ce192d4 RD |
19 | typedef int Position; |
20 | const Position invalidPosition = -1; | |
21 | ||
65ec6247 RD |
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. | |
27 | */ | |
9ce192d4 RD |
28 | class Range { |
29 | public: | |
30 | Position start; | |
31 | Position end; | |
65ec6247 | 32 | |
9e730a78 | 33 | Range(Position pos=0) : |
9ce192d4 | 34 | start(pos), end(pos) { |
7e0c58e9 | 35 | }; |
9e730a78 | 36 | Range(Position start_, Position end_) : |
9ce192d4 | 37 | start(start_), end(end_) { |
7e0c58e9 | 38 | }; |
65ec6247 | 39 | |
9ce192d4 RD |
40 | bool Valid() const { |
41 | return (start != invalidPosition) && (end != invalidPosition); | |
42 | } | |
65ec6247 | 43 | |
1a2fb4cd | 44 | // Is the position within the range? |
9ce192d4 RD |
45 | bool Contains(Position pos) const { |
46 | if (start < end) { | |
47 | return (pos >= start && pos <= end); | |
48 | } else { | |
49 | return (pos <= start && pos >= end); | |
50 | } | |
51 | } | |
65ec6247 | 52 | |
1a2fb4cd RD |
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); | |
59 | } | |
60 | } | |
61 | ||
9ce192d4 RD |
62 | bool Contains(Range other) const { |
63 | return Contains(other.start) && Contains(other.end); | |
64 | } | |
65ec6247 | 65 | |
9ce192d4 | 66 | bool Overlaps(Range other) const { |
9e730a78 | 67 | return |
9ce192d4 RD |
68 | Contains(other.start) || |
69 | Contains(other.end) || | |
70 | other.Contains(start) || | |
71 | other.Contains(end); | |
72 | } | |
73 | }; | |
74 | ||
75 | class DocWatcher; | |
76 | class DocModification; | |
9e96e16f | 77 | class Document; |
9ce192d4 | 78 | |
65ec6247 | 79 | /** |
9e96e16f | 80 | * Interface class for regular expression searching |
65ec6247 | 81 | */ |
9e96e16f RD |
82 | class RegexSearchBase { |
83 | public: | |
84 | virtual ~RegexSearchBase(){} | |
85 | ||
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; | |
88 | ||
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; | |
91 | }; | |
92 | ||
93 | /// Factory function for RegexSearchBase | |
94 | extern RegexSearchBase* CreateRegexSearch(CharClassify *charClassTable); | |
95 | ||
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_) { | |
104 | } | |
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; | |
112 | } | |
113 | size_t StyleAt(size_t i) const { | |
114 | return multipleStyles ? styles[i] : style; | |
115 | } | |
116 | }; | |
117 | ||
118 | /** | |
119 | */ | |
120 | class Document : PerLine { | |
9ce192d4 RD |
121 | |
122 | public: | |
65ec6247 | 123 | /** Used to pair watcher pointer with user data. */ |
9ce192d4 RD |
124 | class WatcherWithUserData { |
125 | public: | |
126 | DocWatcher *watcher; | |
127 | void *userData; | |
128 | WatcherWithUserData() { | |
129 | watcher = 0; | |
130 | userData = 0; | |
131 | } | |
132 | }; | |
591d01be | 133 | |
8e54aaed | 134 | enum charClassification { ccSpace, ccNewLine, ccWord, ccPunctuation }; |
9e730a78 | 135 | private: |
9ce192d4 RD |
136 | int refCount; |
137 | CellBuffer cb; | |
b8193d80 | 138 | CharClassify charClass; |
f6bcfd97 | 139 | char stylingMask; |
9ce192d4 | 140 | int endStyled; |
1a2fb4cd | 141 | int styleClock; |
7e0c58e9 RD |
142 | int enteredModification; |
143 | int enteredStyling; | |
f6bcfd97 | 144 | int enteredReadOnlyCount; |
65ec6247 | 145 | |
9ce192d4 RD |
146 | WatcherWithUserData *watchers; |
147 | int lenWatchers; | |
65ec6247 | 148 | |
9e96e16f RD |
149 | // ldSize is not real data - it is for dimensions and loops |
150 | enum lineData { ldMarkers, ldLevels, ldState, ldMargin, ldAnnotation, ldSize }; | |
151 | PerLine *perLineData[ldSize]; | |
152 | ||
65ec6247 | 153 | bool matchesValid; |
9e96e16f | 154 | RegexSearchBase* regex; |
65ec6247 | 155 | |
9ce192d4 RD |
156 | public: |
157 | int stylingBits; | |
158 | int stylingBitsMask; | |
65ec6247 | 159 | |
9ce192d4 | 160 | int eolMode; |
65ec6247 | 161 | /// Can also be SC_CP_UTF8 to enable UTF-8 mode |
9ce192d4 RD |
162 | int dbcsCodePage; |
163 | int tabInChars; | |
f6bcfd97 | 164 | int indentInChars; |
591d01be | 165 | int actualIndentInChars; |
f6bcfd97 | 166 | bool useTabs; |
65ec6247 RD |
167 | bool tabIndents; |
168 | bool backspaceUnindents; | |
169 | ||
7e0c58e9 RD |
170 | DecorationList decorations; |
171 | ||
9ce192d4 RD |
172 | Document(); |
173 | virtual ~Document(); | |
65ec6247 | 174 | |
9ce192d4 RD |
175 | int AddRef(); |
176 | int Release(); | |
65ec6247 | 177 | |
9e96e16f RD |
178 | virtual void Init(); |
179 | virtual void InsertLine(int line); | |
180 | virtual void RemoveLine(int line); | |
181 | ||
182 | int LineFromPosition(int pos) const; | |
9ce192d4 RD |
183 | int ClampPositionIntoDocument(int pos); |
184 | bool IsCrLf(int pos); | |
f6bcfd97 | 185 | int LenChar(int pos); |
7e0c58e9 | 186 | bool InGoodUTF8(int pos, int &start, int &end); |
9ce192d4 RD |
187 | int MovePositionOutsideChar(int pos, int moveDir, bool checkLineEnd=true); |
188 | ||
189 | // Gateways to modifying document | |
1e9bafca | 190 | void ModifiedAt(int pos); |
7e0c58e9 | 191 | void CheckReadOnly(); |
a834585d | 192 | bool DeleteChars(int pos, int len); |
7e0c58e9 | 193 | bool InsertString(int position, const char *s, int insertLength); |
9ce192d4 RD |
194 | int Undo(); |
195 | int Redo(); | |
196 | bool CanUndo() { return cb.CanUndo(); } | |
197 | bool CanRedo() { return cb.CanRedo(); } | |
198 | void DeleteUndoHistory() { cb.DeleteUndoHistory(); } | |
d134f170 | 199 | bool SetUndoCollection(bool collectUndo) { |
9ce192d4 RD |
200 | return cb.SetUndoCollection(collectUndo); |
201 | } | |
d134f170 | 202 | bool IsCollectingUndo() { return cb.IsCollectingUndo(); } |
9ce192d4 RD |
203 | void BeginUndoAction() { cb.BeginUndoAction(); } |
204 | void EndUndoAction() { cb.EndUndoAction(); } | |
9e96e16f | 205 | void AddUndoAction(int token, bool mayCoalesce) { cb.AddUndoAction(token, mayCoalesce); } |
9ce192d4 RD |
206 | void SetSavePoint(); |
207 | bool IsSavePoint() { return cb.IsSavePoint(); } | |
9e96e16f | 208 | const char *BufferPointer() { return cb.BufferPointer(); } |
f6bcfd97 BP |
209 | |
210 | int GetLineIndentation(int line); | |
211 | void SetLineIndentation(int line, int indent); | |
7e0c58e9 | 212 | int GetLineIndentPosition(int line) const; |
d134f170 | 213 | int GetColumn(int position); |
1a2fb4cd | 214 | int FindColumn(int line, int column); |
9ce192d4 | 215 | void Indent(bool forwards, int lineBottom, int lineTop); |
a33203cb | 216 | static char *TransformLineEnds(int *pLenOut, const char *s, size_t len, int eolMode); |
9ce192d4 RD |
217 | void ConvertLineEnds(int eolModeSet); |
218 | void SetReadOnly(bool set) { cb.SetReadOnly(set); } | |
d134f170 | 219 | bool IsReadOnly() { return cb.IsReadOnly(); } |
9ce192d4 | 220 | |
a834585d | 221 | bool InsertChar(int pos, char ch); |
7e0c58e9 | 222 | bool InsertCString(int position, const char *s); |
f6bcfd97 | 223 | void ChangeChar(int pos, char ch); |
9ce192d4 | 224 | void DelChar(int pos); |
a834585d | 225 | void DelCharBack(int pos); |
9ce192d4 RD |
226 | |
227 | char CharAt(int position) { return cb.CharAt(position); } | |
228 | void GetCharRange(char *buffer, int position, int lengthRetrieve) { | |
229 | cb.GetCharRange(buffer, position, lengthRetrieve); | |
230 | } | |
231 | char StyleAt(int position) { return cb.StyleAt(position); } | |
9e96e16f | 232 | int GetMark(int line); |
f6bcfd97 | 233 | int AddMark(int line, int markerNum); |
1e9bafca | 234 | void AddMarkSet(int line, int valueSet); |
f6bcfd97 BP |
235 | void DeleteMark(int line, int markerNum); |
236 | void DeleteMarkFromHandle(int markerHandle); | |
237 | void DeleteAllMarks(int markerNum); | |
9e96e16f | 238 | int LineFromHandle(int markerHandle); |
7e0c58e9 RD |
239 | int LineStart(int line) const; |
240 | int LineEnd(int line) const; | |
9e96e16f RD |
241 | int LineEndPosition(int position) const; |
242 | bool IsLineEndPosition(int position) const; | |
243 | int VCHomePosition(int position) const; | |
9ce192d4 RD |
244 | |
245 | int SetLevel(int line, int level); | |
9e96e16f RD |
246 | int GetLevel(int line); |
247 | void ClearLevels(); | |
9ce192d4 RD |
248 | int GetLastChild(int lineParent, int level=-1); |
249 | int GetFoldParent(int line); | |
250 | ||
251 | void Indent(bool forwards); | |
1a2fb4cd | 252 | int ExtendWordSelect(int pos, int delta, bool onlyWordCharacters=false); |
9ce192d4 | 253 | int NextWordStart(int pos, int delta); |
8e54aaed | 254 | int NextWordEnd(int pos, int delta); |
7e0c58e9 RD |
255 | int Length() const { return cb.Length(); } |
256 | void Allocate(int newSize) { cb.Allocate(newSize); } | |
9e730a78 | 257 | long FindText(int minPos, int maxPos, const char *s, |
9e96e16f | 258 | bool caseSensitive, bool word, bool wordStart, bool regExp, int flags, int *length); |
d134f170 | 259 | long FindText(int iMessage, unsigned long wParam, long lParam); |
65ec6247 | 260 | const char *SubstituteByPosition(const char *text, int *length); |
7e0c58e9 | 261 | int LinesTotal() const; |
65ec6247 | 262 | |
f6bcfd97 | 263 | void ChangeCase(Range r, bool makeUpperCase); |
591d01be RD |
264 | |
265 | void SetDefaultCharClasses(bool includeWordClass); | |
b8193d80 | 266 | void SetCharClasses(const unsigned char *chars, CharClassify::cc newCharClass); |
9ce192d4 RD |
267 | void SetStylingBits(int bits); |
268 | void StartStyling(int position, char mask); | |
a834585d | 269 | bool SetStyleFor(int length, char style); |
9e96e16f | 270 | bool SetStyles(int length, const char *styles); |
9ce192d4 | 271 | int GetEndStyled() { return endStyled; } |
7e0c58e9 | 272 | void EnsureStyledTo(int pos); |
1a2fb4cd | 273 | int GetStyleClock() { return styleClock; } |
591d01be | 274 | void IncrementStyleClock(); |
7e0c58e9 | 275 | void DecorationFillRange(int position, int value, int fillLength); |
9ce192d4 | 276 | |
7e0c58e9 | 277 | int SetLineState(int line, int state); |
9e96e16f RD |
278 | int GetLineState(int line); |
279 | int GetMaxLineState(); | |
280 | ||
281 | StyledText MarginStyledText(int line); | |
282 | void MarginSetStyle(int line, int style); | |
283 | void MarginSetStyles(int line, const unsigned char *styles); | |
284 | void MarginSetText(int line, const char *text); | |
285 | int MarginLength(int line) const; | |
286 | void MarginClearAll(); | |
287 | ||
288 | bool AnnotationAny() const; | |
289 | StyledText AnnotationStyledText(int line); | |
290 | void AnnotationSetText(int line, const char *text); | |
291 | void AnnotationSetStyle(int line, int style); | |
292 | void AnnotationSetStyles(int line, const unsigned char *styles); | |
293 | int AnnotationLength(int line) const; | |
294 | int AnnotationLines(int line) const; | |
295 | void AnnotationClearAll(); | |
65ec6247 | 296 | |
9ce192d4 RD |
297 | bool AddWatcher(DocWatcher *watcher, void *userData); |
298 | bool RemoveWatcher(DocWatcher *watcher, void *userData); | |
299 | const WatcherWithUserData *GetWatchers() const { return watchers; } | |
300 | int GetLenWatchers() const { return lenWatchers; } | |
65ec6247 RD |
301 | |
302 | bool IsWordPartSeparator(char ch); | |
303 | int WordPartLeft(int pos); | |
304 | int WordPartRight(int pos); | |
8e54aaed | 305 | int ExtendStyleRange(int pos, int delta, bool singleLine = false); |
7e0c58e9 | 306 | bool IsWhiteLine(int line) const; |
9e730a78 RD |
307 | int ParaUp(int pos); |
308 | int ParaDown(int pos); | |
591d01be | 309 | int IndentSize() { return actualIndentInChars; } |
1e9bafca | 310 | int BraceMatch(int position, int maxReStyle); |
65ec6247 | 311 | |
9ce192d4 | 312 | private: |
b8193d80 | 313 | CharClassify::cc WordCharClass(unsigned char ch); |
d134f170 RD |
314 | bool IsWordStartAt(int pos); |
315 | bool IsWordEndAt(int pos); | |
9ce192d4 | 316 | bool IsWordAt(int start, int end); |
65ec6247 | 317 | |
9ce192d4 RD |
318 | void NotifyModifyAttempt(); |
319 | void NotifySavePoint(bool atSavePoint); | |
320 | void NotifyModified(DocModification mh); | |
321 | }; | |
322 | ||
9e96e16f RD |
323 | class UndoGroup { |
324 | Document *pdoc; | |
325 | bool groupNeeded; | |
326 | public: | |
327 | UndoGroup(Document *pdoc_, bool groupNeeded_=true) : | |
328 | pdoc(pdoc_), groupNeeded(groupNeeded_) { | |
329 | if (groupNeeded) { | |
330 | pdoc->BeginUndoAction(); | |
331 | } | |
332 | } | |
333 | ~UndoGroup() { | |
334 | if (groupNeeded) { | |
335 | pdoc->EndUndoAction(); | |
336 | } | |
337 | } | |
338 | bool Needed() const { | |
339 | return groupNeeded; | |
340 | } | |
341 | }; | |
342 | ||
343 | ||
65ec6247 RD |
344 | /** |
345 | * To optimise processing of document modifications by DocWatchers, a hint is passed indicating the | |
346 | * scope of the change. | |
347 | * If the DocWatcher is a document view then this can be used to optimise screen updating. | |
348 | */ | |
9ce192d4 RD |
349 | class DocModification { |
350 | public: | |
351 | int modificationType; | |
352 | int position; | |
353 | int length; | |
65ec6247 RD |
354 | int linesAdded; /**< Negative if lines deleted. */ |
355 | const char *text; /**< Only valid for changes to text, not for changes to style. */ | |
9ce192d4 RD |
356 | int line; |
357 | int foldLevelNow; | |
358 | int foldLevelPrev; | |
9e96e16f RD |
359 | int annotationLinesAdded; |
360 | int token; | |
9ce192d4 | 361 | |
9e730a78 | 362 | DocModification(int modificationType_, int position_=0, int length_=0, |
1e9bafca | 363 | int linesAdded_=0, const char *text_=0, int line_=0) : |
9ce192d4 RD |
364 | modificationType(modificationType_), |
365 | position(position_), | |
366 | length(length_), | |
367 | linesAdded(linesAdded_), | |
368 | text(text_), | |
1e9bafca | 369 | line(line_), |
9ce192d4 | 370 | foldLevelNow(0), |
9e96e16f RD |
371 | foldLevelPrev(0), |
372 | annotationLinesAdded(0), | |
373 | token(0) {} | |
f6bcfd97 | 374 | |
1e9bafca | 375 | DocModification(int modificationType_, const Action &act, int linesAdded_=0) : |
f6bcfd97 | 376 | modificationType(modificationType_), |
1e9bafca | 377 | position(act.position), |
f6bcfd97 BP |
378 | length(act.lenData), |
379 | linesAdded(linesAdded_), | |
380 | text(act.data), | |
381 | line(0), | |
382 | foldLevelNow(0), | |
9e96e16f RD |
383 | foldLevelPrev(0), |
384 | annotationLinesAdded(0), | |
385 | token(0) {} | |
9ce192d4 RD |
386 | }; |
387 | ||
65ec6247 RD |
388 | /** |
389 | * A class that wants to receive notifications from a Document must be derived from DocWatcher | |
390 | * and implement the notification methods. It can then be added to the watcher list with AddWatcher. | |
391 | */ | |
9ce192d4 RD |
392 | class DocWatcher { |
393 | public: | |
394 | virtual ~DocWatcher() {} | |
65ec6247 | 395 | |
9ce192d4 RD |
396 | virtual void NotifyModifyAttempt(Document *doc, void *userData) = 0; |
397 | virtual void NotifySavePoint(Document *doc, void *userData, bool atSavePoint) = 0; | |
398 | virtual void NotifyModified(Document *doc, DocModification mh, void *userData) = 0; | |
399 | virtual void NotifyDeleted(Document *doc, void *userData) = 0; | |
f6bcfd97 | 400 | virtual void NotifyStyleNeeded(Document *doc, void *userData, int endPos) = 0; |
9ce192d4 RD |
401 | }; |
402 | ||
7e0c58e9 RD |
403 | #ifdef SCI_NAMESPACE |
404 | } | |
405 | #endif | |
406 | ||
9ce192d4 | 407 | #endif |