Repository navigation
Expand file tree
/
Copy pathsynedittextbuffer.pp
More file actions
1956 lines (1710 loc) · 62.9 KB
/
Copy pathsynedittextbuffer.pp
File metadata and controls
1956 lines (1710 loc) · 62.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-------------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is: SynEditTextBuffer.pas, released 2000-04-07.
The Original Code is based on parts of mwCustomEdit.pas by Martin Waldenburg,
part of the mwEdit component suite.
Portions created by Martin Waldenburg are Copyright (C) 1998 Martin Waldenburg.
All Rights Reserved.
Contributors to the SynEdit and mwEdit projects are listed in the
Contributors.txt file.
Alternatively, the contents of this file may be used under the terms of the
GNU General Public License Version 2 or later (the "GPL"), in which case
the provisions of the GPL are applicable instead of those above.
If you wish to allow use of your version of this file only under the terms
of the GPL and not to allow others to use your version of this file
under the MPL, indicate your decision by deleting the provisions above and
replace them with the notice and other provisions required by the GPL.
If you do not delete the provisions above, a recipient may use your version
of this file under either the MPL or the GPL.
$Id$
You may retrieve the latest version of this file at the SynEdit home page,
located at http://SynEdit.SourceForge.net
Known Issues:
-------------------------------------------------------------------------------}
unit SynEditTextBuffer;
{$I synedit.inc}
{$IFOPT C+}
{$DEFINE SynAssert}
{$ENDIF}
{$IFDEF SynUndoDebug} {$Define SynUndoDebugItems} {$ENDIF}
interface
uses
Classes, SysUtils, Graphics, LCLProc, LazLoggerBase, tis.LazListClasses,
SynEditTypes, LazSynEditText, SynEditTextBase, SynEditMiscProcs, SynEditMiscClasses,
tis.LazEditMiscProcs, tis.LazEditLineItemLists, tis.LazEditHighlighterUtils;
type
TSynEditFlagsClass = class end; // For Register
TSynEditStringFlag = (
sfModified, // a line is modified and not saved after
sfSaved // a line is modified and saved after
);
TSynEditStringFlags = set of TSynEditStringFlag;
PSynEditStringFlags = ^TSynEditStringFlags;
TStringListIndexEvent = procedure(Index: Integer) of object deprecated 'to be removed in 5.99';
TSynOnBeforeDeleteLines = procedure(Sender: TObject; AnIndex, ACount: Integer) of object;
TSynEditStringMemoryEntry = packed record
Line: AnsiString;
Obj: TObject;
case integer of
1: (Flags: TSynEditStringFlags; );
2: (Dummy: Word; );
end;
{ TLinesModifiedNotificationList }
TLinesModifiedNotificationList = Class(TSynMethodList)
public
Procedure CallRangeNotifyEvents(Sender: TSynEditStrings; aIndex, aNewCount, aOldCount: Integer);
end;
{ TLineRangeNotificationList }
TLineRangeNotificationList = Class(TSynMethodList)
public
Procedure CallRangeNotifyEvents(Sender: TSynEditStrings; aIndex, aCount: Integer);
end;
{ TLineEditNotificationList }
TLineEditNotificationList = Class(TSynMethodList)
public
Procedure CallRangeNotifyEvents(Sender: TSynEditStrings;
aLinePos, aBytePos, aCount, aLineBrkCnt: Integer; aText: String);
end;
TSynEditStringMemoryBase = class(
specialize TGenLazEditLineItemsFixedShiftList<
TSynEditStringMemoryEntry,
specialize TLazListAspectMemInitManagedRefCnt<TSynEditStringMemoryEntry>
>
)
private type
TWrapGenParamClass = class(TWrapGenBaseLazEditLineItems)
public type
TBase = TSynEditStringMemoryBase;
end;
end;
{ TSynEditStringMemory }
TSynEditStringMemory = class(
specialize TGenLazEditParentLineItems<TSynEditStringMemoryBase.TWrapGenParamClass>
)
private type
PSynEditStringMemoryEntry = ^TSynEditStringMemoryEntry;
private
function GetFlags(AnIndex: Integer): TSynEditStringFlags;
function GetObject(AnIndex: Integer): TObject;
function GetRange(AnIndex: Pointer): TLazEditLineItems;
function GetString(AnIndex: Integer): String;
procedure SetCount(AValue: Integer);
procedure SetFlags(AnIndex: Integer; AValue: TSynEditStringFlags);
procedure SetObject(AnIndex: Integer; AValue: TObject);
procedure SetRange(AnIndex: Pointer; AValue: TLazEditLineItems);
procedure SetString(AnIndex: Integer; AValue: String);
public
//procedure Insert(AnIndex, ACount: Integer); reintroduce; inline;
//procedure Delete(AnIndex, ACount: Integer); reintroduce; inline;
// TODO: move => forward to rangelist
procedure InsertRows(AnIndex, ACount: Integer); // deprecated 'use insert / to be removed in 5.99'
procedure DeleteRows(AnIndex, ACount: Integer); // deprecated 'use delete / to be removed in 5.99'
procedure SetLine(AnIndex: Integer; const AString: String; const AnObject: TObject; const AFlags: TSynEditStringFlags);
function GetPChar(ALineIndex: Integer; out ALen: Integer): PChar; // experimental
property Strings[AnIndex: Integer]: String read GetString write SetString; default;
property Objects[AnIndex: Integer]: TObject read GetObject write SetObject;
property RangeList[AnIndex: Pointer]: TLazEditLineItems read GetRange write SetRange;
property Flags[AnIndex: Integer]: TSynEditStringFlags read GetFlags write SetFlags;
property Count: Integer read GetCount write SetCount;
end;
TSynEditStringList = class;
{ TLazSynDisplayBuffer }
TLazSynDisplayBuffer = class(TLazSynDisplayViewEx)
private
FBuffer: TSynEditStringList;
FAtLineStart: Boolean;
public
constructor Create(ABuffer: TSynEditStringList);
procedure SetHighlighterTokensLine(ALine: TLineIdx; out ARealLine: TLineIdx; out ASubLineIdx, AStartBytePos, AStartPhysPos, ALineByteLen: Integer); override;
function GetNextHighlighterToken(out ATokenInfo: TLazSynDisplayTokenInfo): Boolean; override;
function GetDrawDividerInfo: TLazEditDividerDrawConfigSetting; override;
function GetLinesCount: Integer; override;
function TextToViewIndex(ATextIndex: TLineIdx): TLineRange; override;
function ViewToTextIndex(AViewIndex: TLineIdx): TLineIdx; override;
function ViewToTextIndexEx(AViewIndex: TLineIdx; out AViewRange: TLineRange): TLineIdx; override;
end;
{ TSynEditStringList }
TSynEditStringList = class(TSynEditStringListBase)
private
FList: TSynEditStringMemory;
FDisplayView: TLazSynDisplayBuffer;
FAttachedSynEditList: TFPList;
FNotifyLists: Array [TSynEditNotifyReason] of TSynMethodList;
FCachedNotify: Boolean;
FCachedNotifyStart, FCachedNotifyCount: Integer;
FCachedNotifySender: TSynEditStrings;
FModifiedNotifyStart, FModifiedNotifyNewCount, FModifiedNotifyOldCount: Integer;
FIsInEditAction: Integer;
FMarkModifiedLock: integer;
FIgnoreSendNotification: array [TSynEditNotifyReason] of Integer;
fDosFileFormat: boolean;
fIndexOfLongestLine: integer;
FOnBeforeDeleteLines: TSynOnBeforeDeleteLines;
FOwnObjects: Boolean;
FRedoList: TSynEditUndoList;
FUndoList: TSynEditUndoList;
FIsUndoing, FIsRedoing: Boolean;
FIsInDecPaintLock: Boolean;
FIsUtf8: Boolean;
FModified: Boolean;
FTextChangeStamp: int64;
function GetAttachedSynEdits(Index: Integer): TSynEditBase;
function GetFlags(Index: Integer): TSynEditStringFlags;
procedure Grow;
procedure InsertItem(Index: integer; const S: string);
procedure SetFlags(Index: Integer; const AValue: TSynEditStringFlags);
procedure SetModified(const AValue: Boolean);
protected
function GetIsUtf8 : Boolean; override;
procedure SetIsUtf8(const AValue : Boolean); override;
function GetExpandedString(Index: integer): string; override;
function GetLengthOfLongestLine: integer; override;
function GetTextChangeStamp: int64; override;
function GetIsInEditAction: Boolean; override;
procedure IncIsInEditAction; override;
procedure DecIsInEditAction; override;
function GetRedoList: TSynEditUndoList; override;
function GetUndoList: TSynEditUndoList; override;
function GetCurUndoList: TSynEditUndoList; override;
procedure SetIsUndoing(const AValue: Boolean); override;
function GetIsUndoing: Boolean; override;
procedure SetIsRedoing(const AValue: Boolean); override;
function GetIsRedoing: Boolean; override;
procedure UndoRedoAdded(Sender: TObject);
procedure IgnoreSendNotification(AReason: TSynEditNotifyReason;
IncIgnore: Boolean); override;
function GetRange(Index: Pointer): TLazEditLineItems; override;
procedure PutRange(Index: Pointer; const ARange: TLazEditLineItems); override;
function Get(Index: integer): string; override;
function GetCapacity: integer; override;
function GetCount: integer; override;
procedure SetCount(const AValue: Integer);
function GetObject(Index: integer): TObject; override;
procedure Put(Index: integer; const S: string); override;
procedure PutObject(Index: integer; AObject: TObject); override;
procedure SetCapacity(NewCapacity: integer); override;
procedure MaybeSendSenrLinesModified; inline;
procedure SetUpdateState(Updating: Boolean; Sender: TObject); override;
procedure UndoEditLinesDelete(LogY, ACount: Integer);
procedure IncreaseTextChangeStamp; inline;
procedure DoGetPhysicalCharWidths(Line: PChar; LineLen, Index: Integer; PWidths: PPhysicalCharWidth); override;
function GetDisplayView: TLazSynDisplayView; override;
procedure AddManagedHandler(AReason: TSynEditNotifyReason;
AHandler: TMethod); override;
procedure RemoveManagedHandler(AReason: TSynEditNotifyReason;
AHandler: TMethod); override;
procedure RemoveManagedHandlers(AOwner: TObject); override;
public
constructor Create;
destructor Destroy; override;
function Add(const S: string): integer; override;
procedure AddStrings(AStrings: TStrings); override;
procedure Clear; override;
procedure Delete(Index: integer); override;
procedure DeleteLines(Index, NumLines: integer); override;
procedure Insert(Index: integer; const S: string); override;
procedure InsertLines(Index, NumLines: integer); override;
procedure InsertStrings(Index: integer; NewStrings: TStrings); override;
function GetPChar(ALineIndex: Integer; out ALen: Integer): PChar; override; // experimental
procedure MarkModified(AFirst, ALast: Integer);
procedure MarkSaved;
procedure SendNotification(AReason: TSynEditNotifyReason;
ASender: TSynEditStrings; aIndex, aCount: Integer); override;
procedure SendNotification(AReason: TSynEditNotifyReason;
ASender: TSynEditStrings; aIndex, aCount: Integer;
aBytePos: Integer; aLen: Integer; aTxt: String); override;
procedure SendNotification(AReason: TSynEditNotifyReason;
ASender: TObject); override;
procedure FlushNotificationCache; override;
procedure AttachSynEdit(AEdit: TSynEditBase);
procedure DetachSynEdit(AEdit: TSynEditBase);
function AttachedSynEditCount: Integer;
property AttachedSynEdits[Index: Integer]: TSynEditBase read GetAttachedSynEdits;
procedure CopyHanlders(OtherLines: TSynEditStringList; AOwner: TObject = nil); deprecated 'Use "CopyHandlers" / Will be removed in 4.99';
procedure CopyHandlers(OtherLines: TSynEditStringList; AOwner: TObject = nil);
procedure SendCachedNotify; // ToDO: review caching versus changes to topline and other values
public
property DosFileFormat: boolean read fDosFileFormat write fDosFileFormat;
property LengthOfLongestLine: integer read GetLengthOfLongestLine;
property Flags[Index: Integer]: TSynEditStringFlags read GetFlags
write SetFlags;
property Modified: Boolean read FModified write SetModified;
property OwnObjects: Boolean read FOwnObjects write FOwnObjects;
property OnBeforeDeleteLines: TSynOnBeforeDeleteLines read FOnBeforeDeleteLines write FOnBeforeDeleteLines;
public
// Char bounds // 1 based (1 is the 1st char in the line)
function LogicPosAddChars(const ALine: String; ALogicalPos, ACount: integer;
AFlags: LPosFlags = []): Integer; override;
function LogicPosIsAtChar(const ALine: String; ALogicalPos: integer;
AFlags: LPosFlags = []): Boolean; override;
function LogicPosAdjustToChar(const ALine: String; ALogicalPos: integer;
AFlags: LPosFlags = []): Integer; override;
property UndoList: TSynEditUndoList read GetUndoList write fUndoList;
property RedoList: TSynEditUndoList read GetRedoList write fRedoList;
procedure EditInsert(LogX, LogY: Integer; AText: String); override;
function EditDelete(LogX, LogY, ByteLen: Integer): String; override;
function EditReplace(LogX, LogY, ByteLen: Integer; AText: String): String; override;
procedure EditLineBreak(LogX, LogY: Integer); override;
procedure EditLineJoin(LogY: Integer; FillText: String = ''); override;
procedure EditLinesInsert(LogY, ACount: Integer; AText: String = ''); override;
procedure EditLinesDelete(LogY, ACount: Integer); override;
procedure EditUndo(Item: TSynEditUndoItem); override;
procedure EditRedo(Item: TSynEditUndoItem); override;
public
PaintLockOwner: TSynEditBase;
end;
ESynEditStringList = class(Exception);
{end} //mh 2000-10-10
implementation
const
SListIndexOutOfBounds = 'Invalid stringlist index %d';
type
{ TSynEditUndoTxtInsert }
TSynEditUndoTxtInsert = class(TSynEditUndoItem)
private
FPosX, FPosY, FLen: Integer;
protected
function DebugString: String; override;
public
constructor Create(APosX, APosY, ALen: Integer);
function PerformUndo(Caller: TObject): Boolean; override;
end;
{ TSynEditUndoTxtDelete }
TSynEditUndoTxtDelete = class(TSynEditUndoItem)
private
FPosX, FPosY: Integer;
FText: String;
protected
function DebugString: String; override;
public
constructor Create(APosX, APosY: Integer; AText: String);
function PerformUndo(Caller: TObject): Boolean; override;
end;
{ TSynEditUndoTxtLineBreak }
TSynEditUndoTxtLineBreak = class(TSynEditUndoItem)
private
FPosY: Integer;
protected
function DebugString: String; override;
public
constructor Create(APosY: Integer);
function PerformUndo(Caller: TObject): Boolean; override;
end;
{ TSynEditUndoTxtLineJoin }
TSynEditUndoTxtLineJoin = class(TSynEditUndoItem)
private
FPosX, FPosY: Integer;
FFlags: TSynEditStringFlags;
protected
function DebugString: String; override;
public
constructor Create(APosX, APosY: Integer; AFlags: TSynEditStringFlags);
function PerformUndo(Caller: TObject): Boolean; override;
end;
{ TSynEditUndoTxtLinesIns }
TSynEditUndoTxtLinesIns = class(TSynEditUndoItem)
private
FPosY, FCount: Integer;
protected
function DebugString: String; override;
public
constructor Create(ALine, ACount: Integer);
function PerformUndo(Caller: TObject): Boolean; override;
end;
{ TSynEditUndoTxtLinesDel }
TSynEditUndoTxtLinesDel = class(TSynEditUndoItem)
private
FPosY, FCount: Integer;
FFlagsList: array of TSynEditStringFlags;
protected
function DebugString: String; override;
public
constructor Create(ALine, ACount: Integer; Caller: TSynEditStringList);
function PerformUndo(Caller: TObject): Boolean; override;
end;
TSynEditStringFlagsArray = packed array of TSynEditStringFlags;
{ TSynEditUndoMarkModified }
TSynEditUndoMarkModified = class(TSynEditUndoItem)
private
FPosY: TLineIdx;
FWasSaved: TSynEditStringFlagsArray;
protected
function DebugString: String; override;
public
constructor Create(ALine: TLineIdx; AWasSaved: TSynEditStringFlagsArray);
function PerformUndo(Caller: TObject): Boolean; override;
end;
var
(* Re-usable arrays for the most common cases *)
SynEditUndoMarkModifiedOneEmpty: TSynEditStringFlagsArray = nil; // = [];
SynEditUndoMarkModifiedOneSaved: TSynEditStringFlagsArray = nil; // = [sfSaved];
SynEditUndoMarkModifiedOneModified: TSynEditStringFlagsArray = nil; // = [sfModified];
{ TLazSynDisplayBuffer }
constructor TLazSynDisplayBuffer.Create(ABuffer: TSynEditStringList);
begin
inherited Create;
FBuffer := ABuffer;
end;
procedure TLazSynDisplayBuffer.SetHighlighterTokensLine(ALine: TLineIdx; out
ARealLine: TLineIdx; out ASubLineIdx, AStartBytePos, AStartPhysPos, ALineByteLen: Integer);
begin
CurrentTokenLine := ALine;
ARealLine := ALine;
ASubLineIdx := 0;
AStartBytePos := 1;
AStartPhysPos := 1;
ALineByteLen := Length(FBuffer[ARealLine]);
FAtLineStart := True;
end;
function TLazSynDisplayBuffer.GetNextHighlighterToken(out ATokenInfo: TLazSynDisplayTokenInfo): Boolean;
begin
Result := False;
ATokenInfo := Default(TLazSynDisplayTokenInfo);
if not Initialized then exit;
if CurrentTokenHighlighter = nil then begin
ATokenInfo.TokenOrigin := dtoAfterText;
Result := FAtLineStart;
if not Result then exit;
ATokenInfo.TokenStart := FBuffer.GetPChar(CurrentTokenLine, ATokenInfo.TokenLength);
Result := ATokenInfo.TokenLength > 0;
if not Result then exit;
ATokenInfo.TokenAttr := nil;
ATokenInfo.TokenOrigin := dtoVirtualText;
FAtLineStart := False;
end
else begin
if FAtLineStart then
CurrentTokenHighlighter.StartAtLineIndex(CurrentTokenLine);
FAtLineStart := False;
Result := not CurrentTokenHighlighter.GetEol;
if not Result then begin
ATokenInfo.TokenStart := nil;
ATokenInfo.TokenLength := 0;
ATokenInfo.TokenAttr := CurrentTokenHighlighter.GetEndOfLineAttributeEx;
ATokenInfo.TokenOrigin := dtoAfterText;
Result := ATokenInfo.TokenAttr <> nil;
exit;
end;
CurrentTokenHighlighter.GetTokenEx(ATokenInfo.TokenStart, ATokenInfo.TokenLength);
ATokenInfo.TokenAttr := CurrentTokenHighlighter.GetTokenAttributeEx;
ATokenInfo.TokenOrigin := dtoVirtualText;
CurrentTokenHighlighter.Next;
end;
end;
function TLazSynDisplayBuffer.GetDrawDividerInfo: TLazEditDividerDrawConfigSetting;
begin
if CurrentTokenHighlighter <> nil then
Result := CurrentTokenHighlighter.DrawDivider[CurrentTokenLine]
else
Result.Color := clNone;
end;
function TLazSynDisplayBuffer.GetLinesCount: Integer;
begin
Result := FBuffer.Count;
end;
function TLazSynDisplayBuffer.TextToViewIndex(ATextIndex: TLineIdx): TLineRange;
begin
Result.Top := ATextIndex;
Result.Bottom := ATextIndex;
end;
function TLazSynDisplayBuffer.ViewToTextIndex(AViewIndex: TLineIdx): TLineIdx;
begin
Result := AViewIndex;
end;
function TLazSynDisplayBuffer.ViewToTextIndexEx(AViewIndex: TLineIdx; out
AViewRange: TLineRange): TLineIdx;
begin
Result := AViewIndex;
AViewRange.Top := AViewIndex;
AViewRange.Bottom := AViewIndex;
end;
{ TSynEditUndoTxtInsert }
function TSynEditUndoTxtInsert.DebugString: String;
begin
Result := 'X='+dbgs(FPosX) + ' Y='+ dbgs(FPosY) + ' len=' + dbgs(FLen);
end;
constructor TSynEditUndoTxtInsert.Create(APosX, APosY, ALen: Integer);
begin
FPosX := APosX;
FPosY := APosY;
FLen := ALen;
{$IFDEF SynUndoDebugItems}debugln(['--- Undo Insert ',DbgSName(self), ' ', dbgs(Self), ' - ', DebugString]);{$ENDIF}
end;
function TSynEditUndoTxtInsert.PerformUndo(Caller: TObject): Boolean;
begin
Result := Caller is TSynEditStringList;
{$IFDEF SynUndoDebugItems}if Result then debugln(['--- Undo Perform ',DbgSName(self), ' ', dbgs(Self), ' - ', DebugString]);{$ENDIF}
if Result then
TSynEditStringList(Caller).EditDelete(FPosX, FPosY, FLen);
end;
{ TSynEditUndoTxtDelete }
function TSynEditUndoTxtDelete.DebugString: String;
begin
Result := 'X='+dbgs(FPosX) + ' Y='+ dbgs(FPosY) + ' text=' + FText;
end;
constructor TSynEditUndoTxtDelete.Create(APosX, APosY: Integer; AText: String);
begin
FPosX := APosX;
FPosY := APosY;
FText := AText;
{$IFDEF SynUndoDebugItems}debugln(['--- Undo Insert ',DbgSName(self), ' ', dbgs(Self), ' - ', DebugString]);{$ENDIF}
end;
function TSynEditUndoTxtDelete.PerformUndo(Caller: TObject): Boolean;
begin
Result := Caller is TSynEditStringList;
{$IFDEF SynUndoDebugItems}if Result then debugln(['--- Undo Perform ',DbgSName(self), ' ', dbgs(Self), ' - ', DebugString]);{$ENDIF}
if Result then
TSynEditStringList(Caller).EditInsert(FPosX, FPosY, FText);
end;
{ TSynEditUndoTxtLineBreak }
function TSynEditUndoTxtLineBreak.DebugString: String;
begin
Result := ' Y='+ dbgs(FPosY);
end;
constructor TSynEditUndoTxtLineBreak.Create(APosY: Integer);
begin
FPosY := APosY;
{$IFDEF SynUndoDebugItems}debugln(['--- Undo Insert ',DbgSName(self), ' ', dbgs(Self), ' - ', DebugString]);{$ENDIF}
end;
function TSynEditUndoTxtLineBreak.PerformUndo(Caller: TObject): Boolean;
begin
Result := Caller is TSynEditStringList;
{$IFDEF SynUndoDebugItems}if Result then debugln(['--- Undo Perform ',DbgSName(self), ' ', dbgs(Self), ' - ', DebugString]);{$ENDIF}
if Result then
TSynEditStringList(Caller).EditLineJoin(FPosY)
end;
{ TSynEditUndoTxtLineJoin }
function TSynEditUndoTxtLineJoin.DebugString: String;
begin
Result := 'X='+dbgs(FPosX) + ' Y='+ dbgs(FPosY);
end;
constructor TSynEditUndoTxtLineJoin.Create(APosX, APosY: Integer; AFlags: TSynEditStringFlags);
begin
FPosX := APosX;
FPosY := APosY;
FFlags := AFlags;
{$IFDEF SynUndoDebugItems}debugln(['--- Undo Insert ',DbgSName(self), ' ', dbgs(Self), ' - ', DebugString]);{$ENDIF}
end;
function TSynEditUndoTxtLineJoin.PerformUndo(Caller: TObject): Boolean;
begin
Result := Caller is TSynEditStringList;
{$IFDEF SynUndoDebugItems}if Result then debugln(['--- Undo Perform ',DbgSName(self), ' ', dbgs(Self), ' - ', DebugString]);{$ENDIF}
if Result then begin
TSynEditStringList(Caller).EditLineBreak(FPosX, FPosY);
TSynEditStringList(Caller).Flags[FPosY] := FFlags; // ToIdx(FPosY) + 1
end;
end;
{ TSynEditUndoTxtLinesIns }
function TSynEditUndoTxtLinesIns.DebugString: String;
begin
Result := 'Y='+dbgs(FPosY) + ' Cnt='+ dbgs(FCount);
end;
constructor TSynEditUndoTxtLinesIns.Create(ALine, ACount: Integer);
begin
FPosY := ALine;
FCount := ACount;
{$IFDEF SynUndoDebugItems}debugln(['--- Undo Insert ',DbgSName(self), ' ', dbgs(Self), ' - ', DebugString]);{$ENDIF}
end;
function TSynEditUndoTxtLinesIns.PerformUndo(Caller: TObject): Boolean;
begin
Result := Caller is TSynEditStringList;
{$IFDEF SynUndoDebugItems}if Result then debugln(['--- Undo Perform ',DbgSName(self), ' ', dbgs(Self), ' - ', DebugString]);{$ENDIF}
if Result then
TSynEditStringList(Caller).UndoEditLinesDelete(FPosY, FCount)
end;
{ TSynEditUndoTxtLinesDel }
function TSynEditUndoTxtLinesDel.DebugString: String;
begin
Result := 'Y='+dbgs(FPosY) + ' Cnt='+ dbgs(FCount);
end;
constructor TSynEditUndoTxtLinesDel.Create(ALine, ACount: Integer; Caller: TSynEditStringList);
var
i: Integer;
begin
FPosY := ALine;
FCount := ACount;
SetLength(FFlagsList, ACount);
ALine := ToIdx(ALine);
for i := 0 to ACount - 1 do
FFlagsList[i] := Caller.Flags[ALine + i];
{$IFDEF SynUndoDebugItems}debugln(['--- Undo Insert ',DbgSName(self), ' ', dbgs(Self), ' - ', DebugString]);{$ENDIF}
end;
function TSynEditUndoTxtLinesDel.PerformUndo(Caller: TObject): Boolean;
var
y, i: Integer;
begin
Result := Caller is TSynEditStringList;
{$IFDEF SynUndoDebugItems}if Result then debugln(['--- Undo Perform ',DbgSName(self), ' ', dbgs(Self), ' - ', DebugString]);{$ENDIF}
if Result then begin
TSynEditStringList(Caller).EditLinesInsert(FPosY, FCount);
y := ToIdx(FPosY);
for i := 0 to Length(FFlagsList) - 1 do
TSynEditStringList(Caller).Flags[Y + i] := FFlagsList[i];
end;
end;
{ TSynEditUndoMarkModified }
function TSynEditUndoMarkModified.DebugString: String;
begin
Result := 'Y='+dbgs(FPosY) + ' Cnt='+ dbgs(Length(FWasSaved));
end;
constructor TSynEditUndoMarkModified.Create(ALine: TLineIdx;
AWasSaved: TSynEditStringFlagsArray);
begin
FPosY := ALine;
FWasSaved := AWasSaved;
{$IFDEF SynUndoDebugItems}debugln(['--- Undo Insert ',DbgSName(self), ' ', dbgs(Self), ' - ', DebugString]);{$ENDIF}
end;
function TSynEditUndoMarkModified.PerformUndo(Caller: TObject): Boolean;
var
i: Integer;
WasSaved: TSynEditStringFlagsArray;
Buffer: TSynEditStringList absolute Caller;
UnSaved: Boolean;
begin
Result := Caller is TSynEditStringList;
{$IFDEF SynUndoDebugItems}if Result then debugln(['--- Undo Perform ',DbgSName(self), ' ', dbgs(Self), ' - ', DebugString]);{$ENDIF}
WasSaved := nil;
if Result then begin
UnSaved := Buffer.CurUndoList.SavedMarkerExists and (not Buffer.CurUndoList.IsTopMarkedAsSaved);
if Length(FWasSaved) = 1 then begin
if FPosY < Buffer.Count then begin
if sfSaved in Buffer.Flags[FPosY] then
WasSaved := SynEditUndoMarkModifiedOneSaved
else
if sfModified in Buffer.Flags[FPosY] then
WasSaved := SynEditUndoMarkModifiedOneModified
else
WasSaved := SynEditUndoMarkModifiedOneEmpty;
if (sfSaved in FWasSaved[0]) and UnSaved then
Buffer.Flags[FPosY] := [sfModified]
else
Buffer.Flags[FPosY] := FWasSaved[0];
end;
end
else begin
SetLength(WasSaved, Length(FWasSaved));
for i := 0 to Min(Length(FWasSaved), Buffer.Count - FPosY) - 1 do begin
WasSaved[i] := Buffer.Flags[FPosY + i];
if (sfSaved in FWasSaved[i]) and UnSaved then
Buffer.Flags[FPosY + i] := [sfModified]
else
Buffer.Flags[FPosY + i] := FWasSaved[i];
end;
end;
if WasSaved <> nil then
Buffer.CurUndoList.AddChange(TSynEditUndoMarkModified.Create(FPosY, WasSaved), True);
end;
end;
{ TSynEditStringList }
procedure ListIndexOutOfBounds(Index: integer);
begin
raise ESynEditStringList.CreateFmt(SListIndexOutOfBounds, [Index]);
end;
constructor TSynEditStringList.Create;
var
r: TSynEditNotifyReason;
begin
fList := TSynEditStringMemory.Create;
FDisplayView := TLazSynDisplayBuffer.Create(Self);
FAttachedSynEditList := TFPList.Create;
FUndoList := TSynEditUndoList.Create;
fUndoList.OnAddedUndo := @UndoRedoAdded;
FRedoList := TSynEditUndoList.Create;
fRedoList.OnAddedUndo := @UndoRedoAdded;
FIsUndoing := False;
FIsRedoing := False;
FModified := False;
FIsInEditAction := 0;
for r := low(TSynEditNotifyReason) to high(TSynEditNotifyReason)
do case r of
senrLineCount, senrLineChange, senrHighlightChanged, senrLineMappingChanged:
FNotifyLists[r] := TLineRangeNotificationList.Create;
senrLinesModified:
FNotifyLists[r] := TLinesModifiedNotificationList.Create;
senrEditAction:
FNotifyLists[r] := TLineEditNotificationList.Create;
else
FNotifyLists[r] := TSynMethodList.Create;
end;
for r := low(TSynEditNotifyReason) to high(TSynEditNotifyReason) do
FIgnoreSendNotification[r] := 0;
inherited Create;
fDosFileFormat := TRUE;
{begin} //mh 2000-10-19
fIndexOfLongestLine := -1;
{end} //mh 2000-10-19
end;
destructor TSynEditStringList.Destroy;
var
i: TSynEditNotifyReason;
begin
inherited Destroy;
SetCount(0);
SetCapacity(0);
for i := low(TSynEditNotifyReason) to high(TSynEditNotifyReason) do
FreeAndNil(FNotifyLists[i]);
FreeAndNil(FUndoList);
FreeAndNil(FRedoList);
FreeAndNil(FAttachedSynEditList);
FreeAndNil(FDisplayView);
FreeAndNil(fList);
end;
function TSynEditStringList.Add(const S: string): integer;
begin
BeginUpdate;
Result := Count;
InsertItem(Result, S);
SendNotification(senrLineCount, self, Result, 1);
EndUpdate;
end;
procedure TSynEditStringList.AddStrings(AStrings: TStrings);
var
OtherCnt, FirstAdded, InsPos, i: integer;
begin
OtherCnt := AStrings.Count;
if OtherCnt = 0 then
exit;
BeginUpdate;
try
FirstAdded := Count;
i := FirstAdded + OtherCnt;
if i > Capacity then
SetCapacity(i + 32);
SetCount(i);
InsPos := FirstAdded;
for i := 0 to OtherCnt - 1 do begin
FList.SetLine(InsPos, AStrings[i], AStrings.Objects[i], []);
inc(InsPos);
end;
SendNotification(senrLineCount, self, FirstAdded, OtherCnt);
finally
EndUpdate;
end;
end;
procedure TSynEditStringList.Clear;
var
c: Integer;
begin
c := Count;
if c <> 0 then begin
BeginUpdate;
SetCount(0);
SetCapacity(0);
SendNotification(senrLineCount, self, 0, -c);
SendNotification(senrCleared, Self);
EndUpdate;
end;
fIndexOfLongestLine := -1;
end;
procedure TSynEditStringList.Delete(Index: integer);
begin
// Ensure correct index, so DeleteLines will not throw exception
if (Index < 0) or (Index >= Count) then
ListIndexOutOfBounds(Index);
BeginUpdate;
if Assigned(FOnBeforeDeleteLines) then
FOnBeforeDeleteLines(PaintLockOwner, Index, 1);
if FOwnObjects then FList.Objects[Index].Free;
FList.DeleteRows(Index, 1);
IncreaseTextChangeStamp;
fIndexOfLongestLine := -1;
SendNotification(senrLineCount, self, Index, -1);
EndUpdate;
end;
procedure TSynEditStringList.DeleteLines(Index, NumLines: integer);
var
i: Integer;
begin
if NumLines > 0 then begin
// Ensure correct index, so DeleteLines will not throw exception
if (Index < 0) or (Index + NumLines > Count) then
ListIndexOutOfBounds(Index);
BeginUpdate;
if Assigned(FOnBeforeDeleteLines) then
FOnBeforeDeleteLines(PaintLockOwner, Index, NumLines);
if FOwnObjects then begin
for i := Index to Index + NumLines - 1 do
FList.Objects[i].Free;
end;
FList.DeleteRows(Index, NumLines);
IncreaseTextChangeStamp;
SendNotification(senrLineCount, self, Index, -NumLines);
EndUpdate;
end;
end;
function TSynEditStringList.GetFlags(Index: Integer): TSynEditStringFlags;
begin
if (Index >= 0) and (Index < Count) then
Result := FList.Flags[Index]
else
Result := [];
end;
function TSynEditStringList.GetAttachedSynEdits(Index: Integer): TSynEditBase;
begin
Result := TSynEditBase(FAttachedSynEditList[Index]);
end;
function TSynEditStringList.Get(Index: integer): string;
begin
if (Index >= 0) and (Index < Count) then
Result := fList[Index]
else
Result := '';
end;
function TSynEditStringList.GetCapacity: integer;
begin
Result := fList.Capacity;
end;
function TSynEditStringList.GetCount: integer;
begin
Result := FList.Count;
end;
procedure TSynEditStringList.SetCount(const AValue: Integer);
begin
IncreaseTextChangeStamp;
fList.Count := AValue;
end;
{begin} //mh 2000-10-19
function TSynEditStringList.GetExpandedString(Index: integer): string;
begin
if (Index >= 0) and (Index < Count) then begin
Result := FList[Index];
end else
Result := '';
end;
function TSynEditStringList.GetLengthOfLongestLine: integer; //mh 2000-10-19
var
i, j, MaxLen: integer;
begin
if fIndexOfLongestLine < 0 then begin
MaxLen := 0;
if Count > 0 then begin
for i := 0 to Count - 1 do begin
j := length(FList[i]);
if j > MaxLen then begin
MaxLen := j;
fIndexOfLongestLine := i;
end;
end;
end;
end;
if (fIndexOfLongestLine >= 0) and (fIndexOfLongestLine < Count) then
Result := length(FList[fIndexOfLongestLine])
else
Result := 0;
end;
function TSynEditStringList.GetTextChangeStamp: int64;
begin
Result := FTextChangeStamp;
end;
function TSynEditStringList.GetIsInEditAction: Boolean;
begin
Result := FIsInEditAction > 0;
end;
procedure TSynEditStringList.IncIsInEditAction;
begin
inc(FIsInEditAction);
end;
procedure TSynEditStringList.DecIsInEditAction;
begin
dec(FIsInEditAction);
end;
function TSynEditStringList.GetRedoList: TSynEditUndoList;
begin
Result := fRedoList;
end;
function TSynEditStringList.GetUndoList: TSynEditUndoList;
begin
Result := fUndoList;
end;
function TSynEditStringList.GetCurUndoList: TSynEditUndoList;
begin
if FIsUndoing then
Result := fRedoList
else
Result := fUndoList;
end;
procedure TSynEditStringList.SetIsUndoing(const AValue: Boolean);
begin
if FIsUndoing = AValue then
exit;
if not AValue then
SendNotification(senrEndUndoRedo, Self); // before UNDO ends
if (not AValue) and fUndoList.IsTopMarkedAsSaved then begin
fRedoList.MarkTopAsSaved;
MarkSaved;
end;
FIsUndoing := AValue;
if AValue then
SendNotification(senrBeginUndoRedo, Self); // after UNDO started
end;
function TSynEditStringList.GetIsUndoing: Boolean;
begin
Result := FIsUndoing;
end;
procedure TSynEditStringList.SetIsRedoing(const AValue: Boolean);
begin
if FIsRedoing = AValue then
exit;
if not AValue then
SendNotification(senrEndUndoRedo, Self); // before UNDO ends
if (not AValue) and fRedoList.IsTopMarkedAsSaved then begin
fUndoList.MarkTopAsSaved;
MarkSaved;
end;