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;
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.
33 Range(Position pos
=0) :
34 start(pos
), end(pos
) {
36 Range(Position start_
, Position end_
) :
37 start(start_
), end(end_
) {
41 return (start
!= invalidPosition
) && (end
!= invalidPosition
);
44 // Is the position within the range?
45 bool Contains(Position pos
) const {
47 return (pos
>= start
&& pos
<= end
);
49 return (pos
<= start
&& pos
>= end
);
53 // Is the character after pos within the range?
54 bool ContainsCharacter(Position pos
) const {
56 return (pos
>= start
&& pos
< end
);
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 {
68 Contains(other
.start
) ||
69 Contains(other
.end
) ||
70 other
.Contains(start
) ||
76 class DocModification
;
80 * Interface class for regular expression searching
82 class RegexSearchBase
{
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
);
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 {
109 while ((cur
< length
) && (text
[cur
] != '\n'))
113 size_t StyleAt(size_t i
) const {
114 return multipleStyles
? styles
[i
] : style
;
118 class HighlightDelimiter
{
120 HighlightDelimiter() : isEnabled(false) {
127 firstChangeableLineBefore
= -1;
128 firstChangeableLineAfter
= -1;
131 bool NeedsDrawing(int line
) {
132 return isEnabled
&& (line
<= firstChangeableLineBefore
|| line
>= firstChangeableLineAfter
);
135 bool IsFoldBlockHighlighted(int line
) {
136 return isEnabled
&& beginFoldBlock
!= -1 && beginFoldBlock
<= line
&& line
<= endFoldBlock
;
139 bool IsHeadOfFoldBlock(int line
) {
140 return beginFoldBlock
== line
&& line
< endFoldBlock
;
143 bool IsBodyOfFoldBlock(int line
) {
144 return beginFoldBlock
!= -1 && beginFoldBlock
< line
&& line
< endFoldBlock
;
147 bool IsTailOfFoldBlock(int line
) {
148 return beginFoldBlock
!= -1 && beginFoldBlock
< line
&& line
== endFoldBlock
;
151 int beginFoldBlock
; // Begin of current fold block
152 int endFoldBlock
; // End of current fold block
153 int firstChangeableLineBefore
; // First line that triggers repaint before starting line that determined current fold block
154 int firstChangeableLineAfter
; // First line that triggers repaint after starting line that determined current fold block
160 virtual ~CaseFolder() {
162 virtual size_t Fold(char *folded
, size_t sizeFolded
, const char *mixed
, size_t lenMixed
) = 0;
165 class CaseFolderTable
: public CaseFolder
{
170 virtual ~CaseFolderTable();
171 virtual size_t Fold(char *folded
, size_t sizeFolded
, const char *mixed
, size_t lenMixed
);
172 void SetTranslation(char ch
, char chTranslation
);
173 void StandardASCII();
182 bool performingStyle
; ///< Prevent reentrance
184 LexInterface(Document
*pdoc_
) : pdoc(pdoc_
), instance(0), performingStyle(false) {
186 virtual ~LexInterface() {
188 void Colourise(int start
, int end
);
189 bool UseContainerLexing() const {
190 return instance
== 0;
196 class Document
: PerLine
, public IDocument
, public ILoader
{
199 /** Used to pair watcher pointer with user data. */
200 class WatcherWithUserData
{
204 WatcherWithUserData() {
210 enum charClassification
{ ccSpace
, ccNewLine
, ccWord
, ccPunctuation
};
214 CharClassify charClass
;
218 int enteredModification
;
220 int enteredReadOnlyCount
;
222 WatcherWithUserData
*watchers
;
225 // ldSize is not real data - it is for dimensions and loops
226 enum lineData
{ ldMarkers
, ldLevels
, ldState
, ldMargin
, ldAnnotation
, ldSize
};
227 PerLine
*perLineData
[ldSize
];
230 RegexSearchBase
*regex
;
240 /// Can also be SC_CP_UTF8 to enable UTF-8 mode
244 int actualIndentInChars
;
247 bool backspaceUnindents
;
249 DecorationList decorations
;
255 int SCI_METHOD
Release();
258 virtual void InsertLine(int line
);
259 virtual void RemoveLine(int line
);
261 int SCI_METHOD
Version() const {
265 void SCI_METHOD
SetErrorStatus(int status
);
267 int SCI_METHOD
LineFromPosition(int pos
) const;
268 int ClampPositionIntoDocument(int pos
);
269 bool IsCrLf(int pos
);
270 int LenChar(int pos
);
271 bool InGoodUTF8(int pos
, int &start
, int &end
) const;
272 int MovePositionOutsideChar(int pos
, int moveDir
, bool checkLineEnd
=true);
273 int NextPosition(int pos
, int moveDir
) const;
274 bool NextCharacter(int &pos
, int moveDir
); // Returns true if pos changed
275 int SCI_METHOD
CodePage() const;
276 bool SCI_METHOD
IsDBCSLeadByte(char ch
) const;
277 int SafeSegment(const char *text
, int length
, int lengthSegment
);
279 // Gateways to modifying document
280 void ModifiedAt(int pos
);
281 void CheckReadOnly();
282 bool DeleteChars(int pos
, int len
);
283 bool InsertString(int position
, const char *s
, int insertLength
);
284 int SCI_METHOD
AddData(char *data
, int length
);
285 void * SCI_METHOD
ConvertToDocument();
288 bool CanUndo() { return cb
.CanUndo(); }
289 bool CanRedo() { return cb
.CanRedo(); }
290 void DeleteUndoHistory() { cb
.DeleteUndoHistory(); }
291 bool SetUndoCollection(bool collectUndo
) {
292 return cb
.SetUndoCollection(collectUndo
);
294 bool IsCollectingUndo() { return cb
.IsCollectingUndo(); }
295 void BeginUndoAction() { cb
.BeginUndoAction(); }
296 void EndUndoAction() { cb
.EndUndoAction(); }
297 void AddUndoAction(int token
, bool mayCoalesce
) { cb
.AddUndoAction(token
, mayCoalesce
); }
299 bool IsSavePoint() { return cb
.IsSavePoint(); }
300 const char * SCI_METHOD
BufferPointer() { return cb
.BufferPointer(); }
301 const char *RangePointer(int position
, int rangeLength
) { return cb
.RangePointer(position
, rangeLength
); }
302 int GapPosition() const { return cb
.GapPosition(); }
304 int SCI_METHOD
GetLineIndentation(int line
);
305 void SetLineIndentation(int line
, int indent
);
306 int GetLineIndentPosition(int line
) const;
307 int GetColumn(int position
);
308 int CountCharacters(int startPos
, int endPos
);
309 int FindColumn(int line
, int column
);
310 void Indent(bool forwards
, int lineBottom
, int lineTop
);
311 static char *TransformLineEnds(int *pLenOut
, const char *s
, size_t len
, int eolModeWanted
);
312 void ConvertLineEnds(int eolModeSet
);
313 void SetReadOnly(bool set
) { cb
.SetReadOnly(set
); }
314 bool IsReadOnly() { return cb
.IsReadOnly(); }
316 bool InsertChar(int pos
, char ch
);
317 bool InsertCString(int position
, const char *s
);
318 void ChangeChar(int pos
, char ch
);
319 void DelChar(int pos
);
320 void DelCharBack(int pos
);
322 char CharAt(int position
) { return cb
.CharAt(position
); }
323 void SCI_METHOD
GetCharRange(char *buffer
, int position
, int lengthRetrieve
) const {
324 cb
.GetCharRange(buffer
, position
, lengthRetrieve
);
326 char SCI_METHOD
StyleAt(int position
) const { return cb
.StyleAt(position
); }
327 void GetStyleRange(unsigned char *buffer
, int position
, int lengthRetrieve
) const {
328 cb
.GetStyleRange(buffer
, position
, lengthRetrieve
);
330 int GetMark(int line
);
331 int MarkerNext(int lineStart
, int mask
) const;
332 int AddMark(int line
, int markerNum
);
333 void AddMarkSet(int line
, int valueSet
);
334 void DeleteMark(int line
, int markerNum
);
335 void DeleteMarkFromHandle(int markerHandle
);
336 void DeleteAllMarks(int markerNum
);
337 int LineFromHandle(int markerHandle
);
338 int SCI_METHOD
LineStart(int line
) const;
339 int LineEnd(int line
) const;
340 int LineEndPosition(int position
) const;
341 bool IsLineEndPosition(int position
) const;
342 int VCHomePosition(int position
) const;
344 int SCI_METHOD
SetLevel(int line
, int level
);
345 int SCI_METHOD
GetLevel(int line
) const;
347 int GetLastChild(int lineParent
, int level
=-1, int lastLine
=-1);
348 int GetFoldParent(int line
);
349 void GetHighlightDelimiters(HighlightDelimiter
&hDelimiter
, int line
, int lastLine
);
351 void Indent(bool forwards
);
352 int ExtendWordSelect(int pos
, int delta
, bool onlyWordCharacters
=false);
353 int NextWordStart(int pos
, int delta
);
354 int NextWordEnd(int pos
, int delta
);
355 int SCI_METHOD
Length() const { return cb
.Length(); }
356 void Allocate(int newSize
) { cb
.Allocate(newSize
); }
357 bool MatchesWordOptions(bool word
, bool wordStart
, int pos
, int length
);
358 long FindText(int minPos
, int maxPos
, const char *search
, bool caseSensitive
, bool word
,
359 bool wordStart
, bool regExp
, int flags
, int *length
, CaseFolder
*pcf
);
360 const char *SubstituteByPosition(const char *text
, int *length
);
361 int LinesTotal() const;
363 void ChangeCase(Range r
, bool makeUpperCase
);
365 void SetDefaultCharClasses(bool includeWordClass
);
366 void SetCharClasses(const unsigned char *chars
, CharClassify::cc newCharClass
);
367 int GetCharsOfClass(CharClassify::cc charClass
, unsigned char *buffer
);
368 void SetStylingBits(int bits
);
369 void SCI_METHOD
StartStyling(int position
, char mask
);
370 bool SCI_METHOD
SetStyleFor(int length
, char style
);
371 bool SCI_METHOD
SetStyles(int length
, const char *styles
);
372 int GetEndStyled() { return endStyled
; }
373 void EnsureStyledTo(int pos
);
375 int GetStyleClock() { return styleClock
; }
376 void IncrementStyleClock();
377 void SCI_METHOD
DecorationSetCurrentIndicator(int indicator
) {
378 decorations
.SetCurrentIndicator(indicator
);
380 void SCI_METHOD
DecorationFillRange(int position
, int value
, int fillLength
);
382 int SCI_METHOD
SetLineState(int line
, int state
);
383 int SCI_METHOD
GetLineState(int line
) const;
384 int GetMaxLineState();
385 void SCI_METHOD
ChangeLexerState(int start
, int end
);
387 StyledText
MarginStyledText(int line
);
388 void MarginSetStyle(int line
, int style
);
389 void MarginSetStyles(int line
, const unsigned char *styles
);
390 void MarginSetText(int line
, const char *text
);
391 int MarginLength(int line
) const;
392 void MarginClearAll();
394 bool AnnotationAny() const;
395 StyledText
AnnotationStyledText(int line
);
396 void AnnotationSetText(int line
, const char *text
);
397 void AnnotationSetStyle(int line
, int style
);
398 void AnnotationSetStyles(int line
, const unsigned char *styles
);
399 int AnnotationLength(int line
) const;
400 int AnnotationLines(int line
) const;
401 void AnnotationClearAll();
403 bool AddWatcher(DocWatcher
*watcher
, void *userData
);
404 bool RemoveWatcher(DocWatcher
*watcher
, void *userData
);
405 const WatcherWithUserData
*GetWatchers() const { return watchers
; }
406 int GetLenWatchers() const { return lenWatchers
; }
408 CharClassify::cc
WordCharClass(unsigned char ch
);
409 bool IsWordPartSeparator(char ch
);
410 int WordPartLeft(int pos
);
411 int WordPartRight(int pos
);
412 int ExtendStyleRange(int pos
, int delta
, bool singleLine
= false);
413 bool IsWhiteLine(int line
) const;
415 int ParaDown(int pos
);
416 int IndentSize() { return actualIndentInChars
; }
417 int BraceMatch(int position
, int maxReStyle
);
420 bool IsWordStartAt(int pos
);
421 bool IsWordEndAt(int pos
);
422 bool IsWordAt(int start
, int end
);
424 void NotifyModifyAttempt();
425 void NotifySavePoint(bool atSavePoint
);
426 void NotifyModified(DocModification mh
);
433 UndoGroup(Document
*pdoc_
, bool groupNeeded_
=true) :
434 pdoc(pdoc_
), groupNeeded(groupNeeded_
) {
436 pdoc
->BeginUndoAction();
441 pdoc
->EndUndoAction();
444 bool Needed() const {
451 * To optimise processing of document modifications by DocWatchers, a hint is passed indicating the
452 * scope of the change.
453 * If the DocWatcher is a document view then this can be used to optimise screen updating.
455 class DocModification
{
457 int modificationType
;
460 int linesAdded
; /**< Negative if lines deleted. */
461 const char *text
; /**< Only valid for changes to text, not for changes to style. */
465 int annotationLinesAdded
;
468 DocModification(int modificationType_
, int position_
=0, int length_
=0,
469 int linesAdded_
=0, const char *text_
=0, int line_
=0) :
470 modificationType(modificationType_
),
473 linesAdded(linesAdded_
),
478 annotationLinesAdded(0),
481 DocModification(int modificationType_
, const Action
&act
, int linesAdded_
=0) :
482 modificationType(modificationType_
),
483 position(act
.position
),
485 linesAdded(linesAdded_
),
490 annotationLinesAdded(0),
495 * A class that wants to receive notifications from a Document must be derived from DocWatcher
496 * and implement the notification methods. It can then be added to the watcher list with AddWatcher.
500 virtual ~DocWatcher() {}
502 virtual void NotifyModifyAttempt(Document
*doc
, void *userData
) = 0;
503 virtual void NotifySavePoint(Document
*doc
, void *userData
, bool atSavePoint
) = 0;
504 virtual void NotifyModified(Document
*doc
, DocModification mh
, void *userData
) = 0;
505 virtual void NotifyDeleted(Document
*doc
, void *userData
) = 0;
506 virtual void NotifyStyleNeeded(Document
*doc
, void *userData
, int endPos
) = 0;
507 virtual void NotifyLexerChanged(Document
*doc
, void *userData
) = 0;
508 virtual void NotifyErrorOccurred(Document
*doc
, void *userData
, int status
) = 0;