]>
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; | |
65ec6247 | 77 | class RESearch; |
9ce192d4 | 78 | |
65ec6247 RD |
79 | /** |
80 | */ | |
9ce192d4 RD |
81 | class Document { |
82 | ||
83 | public: | |
65ec6247 | 84 | /** Used to pair watcher pointer with user data. */ |
9ce192d4 RD |
85 | class WatcherWithUserData { |
86 | public: | |
87 | DocWatcher *watcher; | |
88 | void *userData; | |
89 | WatcherWithUserData() { | |
90 | watcher = 0; | |
91 | userData = 0; | |
92 | } | |
93 | }; | |
591d01be | 94 | |
8e54aaed | 95 | enum charClassification { ccSpace, ccNewLine, ccWord, ccPunctuation }; |
65ec6247 | 96 | |
9e730a78 | 97 | private: |
9ce192d4 RD |
98 | int refCount; |
99 | CellBuffer cb; | |
b8193d80 | 100 | CharClassify charClass; |
f6bcfd97 | 101 | char stylingMask; |
9ce192d4 | 102 | int endStyled; |
1a2fb4cd | 103 | int styleClock; |
7e0c58e9 RD |
104 | int enteredModification; |
105 | int enteredStyling; | |
f6bcfd97 | 106 | int enteredReadOnlyCount; |
65ec6247 | 107 | |
9ce192d4 RD |
108 | WatcherWithUserData *watchers; |
109 | int lenWatchers; | |
65ec6247 RD |
110 | |
111 | bool matchesValid; | |
112 | RESearch *pre; | |
113 | char *substituted; | |
114 | ||
9ce192d4 RD |
115 | public: |
116 | int stylingBits; | |
117 | int stylingBitsMask; | |
65ec6247 | 118 | |
9ce192d4 | 119 | int eolMode; |
65ec6247 | 120 | /// Can also be SC_CP_UTF8 to enable UTF-8 mode |
9ce192d4 RD |
121 | int dbcsCodePage; |
122 | int tabInChars; | |
f6bcfd97 | 123 | int indentInChars; |
591d01be | 124 | int actualIndentInChars; |
f6bcfd97 | 125 | bool useTabs; |
65ec6247 RD |
126 | bool tabIndents; |
127 | bool backspaceUnindents; | |
128 | ||
7e0c58e9 RD |
129 | DecorationList decorations; |
130 | ||
9ce192d4 RD |
131 | Document(); |
132 | virtual ~Document(); | |
65ec6247 | 133 | |
9ce192d4 RD |
134 | int AddRef(); |
135 | int Release(); | |
65ec6247 | 136 | |
9ce192d4 RD |
137 | int LineFromPosition(int pos); |
138 | int ClampPositionIntoDocument(int pos); | |
139 | bool IsCrLf(int pos); | |
f6bcfd97 | 140 | int LenChar(int pos); |
7e0c58e9 | 141 | bool InGoodUTF8(int pos, int &start, int &end); |
9ce192d4 RD |
142 | int MovePositionOutsideChar(int pos, int moveDir, bool checkLineEnd=true); |
143 | ||
144 | // Gateways to modifying document | |
1e9bafca | 145 | void ModifiedAt(int pos); |
7e0c58e9 | 146 | void CheckReadOnly(); |
a834585d | 147 | bool DeleteChars(int pos, int len); |
7e0c58e9 | 148 | bool InsertString(int position, const char *s, int insertLength); |
9ce192d4 RD |
149 | int Undo(); |
150 | int Redo(); | |
151 | bool CanUndo() { return cb.CanUndo(); } | |
152 | bool CanRedo() { return cb.CanRedo(); } | |
153 | void DeleteUndoHistory() { cb.DeleteUndoHistory(); } | |
d134f170 | 154 | bool SetUndoCollection(bool collectUndo) { |
9ce192d4 RD |
155 | return cb.SetUndoCollection(collectUndo); |
156 | } | |
d134f170 | 157 | bool IsCollectingUndo() { return cb.IsCollectingUndo(); } |
9ce192d4 RD |
158 | void BeginUndoAction() { cb.BeginUndoAction(); } |
159 | void EndUndoAction() { cb.EndUndoAction(); } | |
160 | void SetSavePoint(); | |
161 | bool IsSavePoint() { return cb.IsSavePoint(); } | |
f6bcfd97 BP |
162 | |
163 | int GetLineIndentation(int line); | |
164 | void SetLineIndentation(int line, int indent); | |
7e0c58e9 | 165 | int GetLineIndentPosition(int line) const; |
d134f170 | 166 | int GetColumn(int position); |
1a2fb4cd | 167 | int FindColumn(int line, int column); |
9ce192d4 | 168 | void Indent(bool forwards, int lineBottom, int lineTop); |
a33203cb | 169 | static char *TransformLineEnds(int *pLenOut, const char *s, size_t len, int eolMode); |
9ce192d4 RD |
170 | void ConvertLineEnds(int eolModeSet); |
171 | void SetReadOnly(bool set) { cb.SetReadOnly(set); } | |
d134f170 | 172 | bool IsReadOnly() { return cb.IsReadOnly(); } |
9ce192d4 | 173 | |
a834585d | 174 | bool InsertChar(int pos, char ch); |
7e0c58e9 | 175 | bool InsertCString(int position, const char *s); |
f6bcfd97 | 176 | void ChangeChar(int pos, char ch); |
9ce192d4 | 177 | void DelChar(int pos); |
a834585d | 178 | void DelCharBack(int pos); |
9ce192d4 RD |
179 | |
180 | char CharAt(int position) { return cb.CharAt(position); } | |
181 | void GetCharRange(char *buffer, int position, int lengthRetrieve) { | |
182 | cb.GetCharRange(buffer, position, lengthRetrieve); | |
183 | } | |
184 | char StyleAt(int position) { return cb.StyleAt(position); } | |
185 | int GetMark(int line) { return cb.GetMark(line); } | |
f6bcfd97 | 186 | int AddMark(int line, int markerNum); |
1e9bafca | 187 | void AddMarkSet(int line, int valueSet); |
f6bcfd97 BP |
188 | void DeleteMark(int line, int markerNum); |
189 | void DeleteMarkFromHandle(int markerHandle); | |
190 | void DeleteAllMarks(int markerNum); | |
9ce192d4 | 191 | int LineFromHandle(int markerHandle) { return cb.LineFromHandle(markerHandle); } |
7e0c58e9 RD |
192 | int LineStart(int line) const; |
193 | int LineEnd(int line) const; | |
9ce192d4 RD |
194 | int LineEndPosition(int position); |
195 | int VCHomePosition(int position); | |
196 | ||
197 | int SetLevel(int line, int level); | |
198 | int GetLevel(int line) { return cb.GetLevel(line); } | |
d134f170 | 199 | void ClearLevels() { cb.ClearLevels(); } |
9ce192d4 RD |
200 | int GetLastChild(int lineParent, int level=-1); |
201 | int GetFoldParent(int line); | |
202 | ||
203 | void Indent(bool forwards); | |
1a2fb4cd | 204 | int ExtendWordSelect(int pos, int delta, bool onlyWordCharacters=false); |
9ce192d4 | 205 | int NextWordStart(int pos, int delta); |
8e54aaed | 206 | int NextWordEnd(int pos, int delta); |
7e0c58e9 RD |
207 | int Length() const { return cb.Length(); } |
208 | void Allocate(int newSize) { cb.Allocate(newSize); } | |
9e730a78 RD |
209 | long FindText(int minPos, int maxPos, const char *s, |
210 | bool caseSensitive, bool word, bool wordStart, bool regExp, bool posix, int *length); | |
d134f170 | 211 | long FindText(int iMessage, unsigned long wParam, long lParam); |
65ec6247 | 212 | const char *SubstituteByPosition(const char *text, int *length); |
7e0c58e9 | 213 | int LinesTotal() const; |
65ec6247 | 214 | |
f6bcfd97 | 215 | void ChangeCase(Range r, bool makeUpperCase); |
591d01be RD |
216 | |
217 | void SetDefaultCharClasses(bool includeWordClass); | |
b8193d80 | 218 | void SetCharClasses(const unsigned char *chars, CharClassify::cc newCharClass); |
9ce192d4 RD |
219 | void SetStylingBits(int bits); |
220 | void StartStyling(int position, char mask); | |
a834585d RD |
221 | bool SetStyleFor(int length, char style); |
222 | bool SetStyles(int length, char *styles); | |
9ce192d4 | 223 | int GetEndStyled() { return endStyled; } |
7e0c58e9 | 224 | void EnsureStyledTo(int pos); |
1a2fb4cd | 225 | int GetStyleClock() { return styleClock; } |
591d01be | 226 | void IncrementStyleClock(); |
7e0c58e9 | 227 | void DecorationFillRange(int position, int value, int fillLength); |
9ce192d4 | 228 | |
7e0c58e9 | 229 | int SetLineState(int line, int state); |
9ce192d4 RD |
230 | int GetLineState(int line) { return cb.GetLineState(line); } |
231 | int GetMaxLineState() { return cb.GetMaxLineState(); } | |
65ec6247 | 232 | |
9ce192d4 RD |
233 | bool AddWatcher(DocWatcher *watcher, void *userData); |
234 | bool RemoveWatcher(DocWatcher *watcher, void *userData); | |
235 | const WatcherWithUserData *GetWatchers() const { return watchers; } | |
236 | int GetLenWatchers() const { return lenWatchers; } | |
65ec6247 RD |
237 | |
238 | bool IsWordPartSeparator(char ch); | |
239 | int WordPartLeft(int pos); | |
240 | int WordPartRight(int pos); | |
8e54aaed | 241 | int ExtendStyleRange(int pos, int delta, bool singleLine = false); |
7e0c58e9 | 242 | bool IsWhiteLine(int line) const; |
9e730a78 RD |
243 | int ParaUp(int pos); |
244 | int ParaDown(int pos); | |
591d01be | 245 | int IndentSize() { return actualIndentInChars; } |
1e9bafca | 246 | int BraceMatch(int position, int maxReStyle); |
65ec6247 | 247 | |
9ce192d4 | 248 | private: |
b8193d80 | 249 | CharClassify::cc WordCharClass(unsigned char ch); |
d134f170 RD |
250 | bool IsWordStartAt(int pos); |
251 | bool IsWordEndAt(int pos); | |
9ce192d4 | 252 | bool IsWordAt(int start, int end); |
65ec6247 | 253 | |
9ce192d4 RD |
254 | void NotifyModifyAttempt(); |
255 | void NotifySavePoint(bool atSavePoint); | |
256 | void NotifyModified(DocModification mh); | |
257 | }; | |
258 | ||
65ec6247 RD |
259 | /** |
260 | * To optimise processing of document modifications by DocWatchers, a hint is passed indicating the | |
261 | * scope of the change. | |
262 | * If the DocWatcher is a document view then this can be used to optimise screen updating. | |
263 | */ | |
9ce192d4 RD |
264 | class DocModification { |
265 | public: | |
266 | int modificationType; | |
267 | int position; | |
268 | int length; | |
65ec6247 RD |
269 | int linesAdded; /**< Negative if lines deleted. */ |
270 | const char *text; /**< Only valid for changes to text, not for changes to style. */ | |
9ce192d4 RD |
271 | int line; |
272 | int foldLevelNow; | |
273 | int foldLevelPrev; | |
274 | ||
9e730a78 | 275 | DocModification(int modificationType_, int position_=0, int length_=0, |
1e9bafca | 276 | int linesAdded_=0, const char *text_=0, int line_=0) : |
9ce192d4 RD |
277 | modificationType(modificationType_), |
278 | position(position_), | |
279 | length(length_), | |
280 | linesAdded(linesAdded_), | |
281 | text(text_), | |
1e9bafca | 282 | line(line_), |
9ce192d4 RD |
283 | foldLevelNow(0), |
284 | foldLevelPrev(0) {} | |
f6bcfd97 | 285 | |
1e9bafca | 286 | DocModification(int modificationType_, const Action &act, int linesAdded_=0) : |
f6bcfd97 | 287 | modificationType(modificationType_), |
1e9bafca | 288 | position(act.position), |
f6bcfd97 BP |
289 | length(act.lenData), |
290 | linesAdded(linesAdded_), | |
291 | text(act.data), | |
292 | line(0), | |
293 | foldLevelNow(0), | |
294 | foldLevelPrev(0) {} | |
9ce192d4 RD |
295 | }; |
296 | ||
65ec6247 RD |
297 | /** |
298 | * A class that wants to receive notifications from a Document must be derived from DocWatcher | |
299 | * and implement the notification methods. It can then be added to the watcher list with AddWatcher. | |
300 | */ | |
9ce192d4 RD |
301 | class DocWatcher { |
302 | public: | |
303 | virtual ~DocWatcher() {} | |
65ec6247 | 304 | |
9ce192d4 RD |
305 | virtual void NotifyModifyAttempt(Document *doc, void *userData) = 0; |
306 | virtual void NotifySavePoint(Document *doc, void *userData, bool atSavePoint) = 0; | |
307 | virtual void NotifyModified(Document *doc, DocModification mh, void *userData) = 0; | |
308 | virtual void NotifyDeleted(Document *doc, void *userData) = 0; | |
f6bcfd97 | 309 | virtual void NotifyStyleNeeded(Document *doc, void *userData, int endPos) = 0; |
9ce192d4 RD |
310 | }; |
311 | ||
7e0c58e9 RD |
312 | #ifdef SCI_NAMESPACE |
313 | } | |
314 | #endif | |
315 | ||
9ce192d4 | 316 | #endif |