1 // Scintilla source code edit control
\r
3 ** Text document that handles notifications, DBCS, styling, words and end of line.
\r
5 // Copyright 1998-2003 by Neil Hodgson <neilh@scintilla.org>
\r
6 // The License.txt file describes the conditions under which this software may be distributed.
\r
11 #ifdef SCI_NAMESPACE
\r
12 namespace Scintilla {
\r
16 * A Position is a position within a document between two characters or at the beginning or end.
\r
17 * Sometimes used as a character index where it identifies the character after the position.
\r
19 typedef int Position;
\r
20 const Position invalidPosition = -1;
\r
23 * The range class represents a range of text in a document.
\r
24 * The two values are not sorted as one end may be more significant than the other
\r
25 * as is the case for the selection where the end position is the position of the caret.
\r
26 * If either position is invalidPosition then the range is invalid and most operations will fail.
\r
33 Range(Position pos=0) :
\r
34 start(pos), end(pos) {
\r
36 Range(Position start_, Position end_) :
\r
37 start(start_), end(end_) {
\r
40 bool Valid() const {
\r
41 return (start != invalidPosition) && (end != invalidPosition);
\r
44 // Is the position within the range?
\r
45 bool Contains(Position pos) const {
\r
47 return (pos >= start && pos <= end);
\r
49 return (pos <= start && pos >= end);
\r
53 // Is the character after pos within the range?
\r
54 bool ContainsCharacter(Position pos) const {
\r
56 return (pos >= start && pos < end);
\r
58 return (pos < start && pos >= end);
\r
62 bool Contains(Range other) const {
\r
63 return Contains(other.start) && Contains(other.end);
\r
66 bool Overlaps(Range other) const {
\r
68 Contains(other.start) ||
\r
69 Contains(other.end) ||
\r
70 other.Contains(start) ||
\r
71 other.Contains(end);
\r
76 class DocModification;
\r
80 * Interface class for regular expression searching
\r
82 class RegexSearchBase {
\r
84 virtual ~RegexSearchBase(){}
\r
86 virtual long FindText(Document* doc, int minPos, int maxPos, const char *s,
\r
87 bool caseSensitive, bool word, bool wordStart, int flags, int *length) = 0;
\r
89 ///@return String with the substitutions, must remain valid until the next call or destruction
\r
90 virtual const char *SubstituteByPosition(Document* doc, const char *text, int *length) = 0;
\r
93 /// Factory function for RegexSearchBase
\r
94 extern RegexSearchBase* CreateRegexSearch(CharClassify *charClassTable);
\r
101 /** Used to pair watcher pointer with user data. */
\r
102 class WatcherWithUserData {
\r
104 DocWatcher *watcher;
\r
106 WatcherWithUserData() {
\r
112 enum charClassification { ccSpace, ccNewLine, ccWord, ccPunctuation };
\r
117 CharClassify charClass;
\r
121 int enteredModification;
\r
122 int enteredStyling;
\r
123 int enteredReadOnlyCount;
\r
125 WatcherWithUserData *watchers;
\r
129 RegexSearchBase* regex;
\r
133 int stylingBitsMask;
\r
136 /// Can also be SC_CP_UTF8 to enable UTF-8 mode
\r
140 int actualIndentInChars;
\r
143 bool backspaceUnindents;
\r
145 DecorationList decorations;
\r
148 virtual ~Document();
\r
153 int LineFromPosition(int pos);
\r
154 int ClampPositionIntoDocument(int pos);
\r
155 bool IsCrLf(int pos);
\r
156 int LenChar(int pos);
\r
157 bool InGoodUTF8(int pos, int &start, int &end);
\r
158 int MovePositionOutsideChar(int pos, int moveDir, bool checkLineEnd=true);
\r
160 // Gateways to modifying document
\r
161 void ModifiedAt(int pos);
\r
162 void CheckReadOnly();
\r
163 bool DeleteChars(int pos, int len);
\r
164 bool InsertString(int position, const char *s, int insertLength);
\r
167 bool CanUndo() { return cb.CanUndo(); }
\r
168 bool CanRedo() { return cb.CanRedo(); }
\r
169 void DeleteUndoHistory() { cb.DeleteUndoHistory(); }
\r
170 bool SetUndoCollection(bool collectUndo) {
\r
171 return cb.SetUndoCollection(collectUndo);
\r
173 bool IsCollectingUndo() { return cb.IsCollectingUndo(); }
\r
174 void BeginUndoAction() { cb.BeginUndoAction(); }
\r
175 void EndUndoAction() { cb.EndUndoAction(); }
\r
176 void SetSavePoint();
\r
177 bool IsSavePoint() { return cb.IsSavePoint(); }
\r
178 const char *BufferPointer() { return cb.BufferPointer(); }
\r
180 int GetLineIndentation(int line);
\r
181 void SetLineIndentation(int line, int indent);
\r
182 int GetLineIndentPosition(int line) const;
\r
183 int GetColumn(int position);
\r
184 int FindColumn(int line, int column);
\r
185 void Indent(bool forwards, int lineBottom, int lineTop);
\r
186 static char *TransformLineEnds(int *pLenOut, const char *s, size_t len, int eolMode);
\r
187 void ConvertLineEnds(int eolModeSet);
\r
188 void SetReadOnly(bool set) { cb.SetReadOnly(set); }
\r
189 bool IsReadOnly() { return cb.IsReadOnly(); }
\r
191 bool InsertChar(int pos, char ch);
\r
192 bool InsertCString(int position, const char *s);
\r
193 void ChangeChar(int pos, char ch);
\r
194 void DelChar(int pos);
\r
195 void DelCharBack(int pos);
\r
197 char CharAt(int position) { return cb.CharAt(position); }
\r
198 void GetCharRange(char *buffer, int position, int lengthRetrieve) {
\r
199 cb.GetCharRange(buffer, position, lengthRetrieve);
\r
201 char StyleAt(int position) { return cb.StyleAt(position); }
\r
202 int GetMark(int line) { return cb.GetMark(line); }
\r
203 int AddMark(int line, int markerNum);
\r
204 void AddMarkSet(int line, int valueSet);
\r
205 void DeleteMark(int line, int markerNum);
\r
206 void DeleteMarkFromHandle(int markerHandle);
\r
207 void DeleteAllMarks(int markerNum);
\r
208 int LineFromHandle(int markerHandle) { return cb.LineFromHandle(markerHandle); }
\r
209 int LineStart(int line) const;
\r
210 int LineEnd(int line) const;
\r
211 int LineEndPosition(int position);
\r
212 int VCHomePosition(int position);
\r
214 int SetLevel(int line, int level);
\r
215 int GetLevel(int line) { return cb.GetLevel(line); }
\r
216 void ClearLevels() { cb.ClearLevels(); }
\r
217 int GetLastChild(int lineParent, int level=-1);
\r
218 int GetFoldParent(int line);
\r
220 void Indent(bool forwards);
\r
221 int ExtendWordSelect(int pos, int delta, bool onlyWordCharacters=false);
\r
222 int NextWordStart(int pos, int delta);
\r
223 int NextWordEnd(int pos, int delta);
\r
224 int Length() const { return cb.Length(); }
\r
225 void Allocate(int newSize) { cb.Allocate(newSize); }
\r
226 long FindText(int minPos, int maxPos, const char *s,
\r
227 bool caseSensitive, bool word, bool wordStart, bool regExp, int flags, int *length);
\r
228 long FindText(int iMessage, unsigned long wParam, long lParam);
\r
229 const char *SubstituteByPosition(const char *text, int *length);
\r
230 int LinesTotal() const;
\r
232 void ChangeCase(Range r, bool makeUpperCase);
\r
234 void SetDefaultCharClasses(bool includeWordClass);
\r
235 void SetCharClasses(const unsigned char *chars, CharClassify::cc newCharClass);
\r
236 void SetStylingBits(int bits);
\r
237 void StartStyling(int position, char mask);
\r
238 bool SetStyleFor(int length, char style);
\r
239 bool SetStyles(int length, char *styles);
\r
240 int GetEndStyled() { return endStyled; }
\r
241 void EnsureStyledTo(int pos);
\r
242 int GetStyleClock() { return styleClock; }
\r
243 void IncrementStyleClock();
\r
244 void DecorationFillRange(int position, int value, int fillLength);
\r
246 int SetLineState(int line, int state);
\r
247 int GetLineState(int line) { return cb.GetLineState(line); }
\r
248 int GetMaxLineState() { return cb.GetMaxLineState(); }
\r
250 bool AddWatcher(DocWatcher *watcher, void *userData);
\r
251 bool RemoveWatcher(DocWatcher *watcher, void *userData);
\r
252 const WatcherWithUserData *GetWatchers() const { return watchers; }
\r
253 int GetLenWatchers() const { return lenWatchers; }
\r
255 bool IsWordPartSeparator(char ch);
\r
256 int WordPartLeft(int pos);
\r
257 int WordPartRight(int pos);
\r
258 int ExtendStyleRange(int pos, int delta, bool singleLine = false);
\r
259 bool IsWhiteLine(int line) const;
\r
260 int ParaUp(int pos);
\r
261 int ParaDown(int pos);
\r
262 int IndentSize() { return actualIndentInChars; }
\r
263 int BraceMatch(int position, int maxReStyle);
\r
266 CharClassify::cc WordCharClass(unsigned char ch);
\r
267 bool IsWordStartAt(int pos);
\r
268 bool IsWordEndAt(int pos);
\r
269 bool IsWordAt(int start, int end);
\r
271 void NotifyModifyAttempt();
\r
272 void NotifySavePoint(bool atSavePoint);
\r
273 void NotifyModified(DocModification mh);
\r
277 * To optimise processing of document modifications by DocWatchers, a hint is passed indicating the
\r
278 * scope of the change.
\r
279 * If the DocWatcher is a document view then this can be used to optimise screen updating.
\r
281 class DocModification {
\r
283 int modificationType;
\r
286 int linesAdded; /**< Negative if lines deleted. */
\r
287 const char *text; /**< Only valid for changes to text, not for changes to style. */
\r
292 DocModification(int modificationType_, int position_=0, int length_=0,
\r
293 int linesAdded_=0, const char *text_=0, int line_=0) :
\r
294 modificationType(modificationType_),
\r
295 position(position_),
\r
297 linesAdded(linesAdded_),
\r
301 foldLevelPrev(0) {}
\r
303 DocModification(int modificationType_, const Action &act, int linesAdded_=0) :
\r
304 modificationType(modificationType_),
\r
305 position(act.position),
\r
306 length(act.lenData),
\r
307 linesAdded(linesAdded_),
\r
311 foldLevelPrev(0) {}
\r
315 * A class that wants to receive notifications from a Document must be derived from DocWatcher
\r
316 * and implement the notification methods. It can then be added to the watcher list with AddWatcher.
\r
320 virtual ~DocWatcher() {}
\r
322 virtual void NotifyModifyAttempt(Document *doc, void *userData) = 0;
\r
323 virtual void NotifySavePoint(Document *doc, void *userData, bool atSavePoint) = 0;
\r
324 virtual void NotifyModified(Document *doc, DocModification mh, void *userData) = 0;
\r
325 virtual void NotifyDeleted(Document *doc, void *userData) = 0;
\r
326 virtual void NotifyStyleNeeded(Document *doc, void *userData, int endPos) = 0;
\r
329 #ifdef SCI_NAMESPACE
\r