1 // Scintilla source code edit control
3 ** Text document that handles notifications, DBCS, styling, words and end of line.
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.
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.
20 const Position invalidPosition
= -1;
22 enum EncodingFamily
{ efEightBit
, efUnicode
, efDBCS
};
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.
35 explicit Range(Position pos
=0) :
36 start(pos
), end(pos
) {
38 Range(Position start_
, Position end_
) :
39 start(start_
), end(end_
) {
43 return (start
!= invalidPosition
) && (end
!= invalidPosition
);
46 // Is the position within the range?
47 bool Contains(Position pos
) const {
49 return (pos
>= start
&& pos
<= end
);
51 return (pos
<= start
&& pos
>= end
);
55 // Is the character after pos within the range?
56 bool ContainsCharacter(Position pos
) const {
58 return (pos
>= start
&& pos
< end
);
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 {
70 Contains(other
.start
) ||
71 Contains(other
.end
) ||
72 other
.Contains(start
) ||
78 class DocModification
;
82 * Interface class for regular expression searching
84 class RegexSearchBase
{
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
);
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 {
111 while ((cur
< length
) && (text
[cur
] != '\n'))
115 size_t StyleAt(size_t i
) const {
116 return multipleStyles
? styles
[i
] : style
;
120 class HighlightDelimiter
{
122 HighlightDelimiter() : isEnabled(false) {
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
166 bool performingStyle
; ///< Prevent reentrance
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
{
184 /** Used to pair watcher pointer with user data. */
185 struct WatcherWithUserData
{
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
);
199 CharClassify charClass
;
204 int enteredModification
;
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
];
215 RegexSearchBase
*regex
;
225 /// Can also be SC_CP_UTF8 to enable UTF-8 mode
230 int actualIndentInChars
;
233 bool backspaceUnindents
;
235 DecorationList decorations
;
241 int SCI_METHOD
Release();
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 {
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();
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
); }
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;
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
);
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
);
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
);
422 UndoGroup(Document
*pdoc_
, bool groupNeeded_
=true) :
423 pdoc(pdoc_
), groupNeeded(groupNeeded_
) {
425 pdoc
->BeginUndoAction();
430 pdoc
->EndUndoAction();
433 bool Needed() const {
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
{
446 int modificationType
;
449 int linesAdded
; /**< Negative if lines deleted. */
450 const char *text
; /**< Only valid for changes to text, not for changes to style. */
454 int annotationLinesAdded
;
457 DocModification(int modificationType_
, int position_
=0, int length_
=0,
458 int linesAdded_
=0, const char *text_
=0, int line_
=0) :
459 modificationType(modificationType_
),
462 linesAdded(linesAdded_
),
467 annotationLinesAdded(0),
470 DocModification(int modificationType_
, const Action
&act
, int linesAdded_
=0) :
471 modificationType(modificationType_
),
472 position(act
.position
),
474 linesAdded(linesAdded_
),
479 annotationLinesAdded(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.
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;