-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSynEditTextBuffer.pas
1388 lines (1272 loc) · 42.1 KB
/
SynEditTextBuffer.pas
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: SynEditTextBuffer.pas,v 1.2 2004/07/11 02:01:05 andersonrb Exp $
You may retrieve the latest version of this file at the SynEdit home page,
located at http://SynEdit.SourceForge.net
Known Issues:
-------------------------------------------------------------------------------}
{$IFNDEF QSYNEDITTEXTBUFFER}
unit SynEditTextBuffer;
{$ENDIF}
{$I SynEdit.inc}
interface
uses
{$IFDEF SYN_CLX}
kTextDrawer,
Types,
QSynEditTypes,
QSynEditMiscProcs,
{$ELSE}
Windows,
SynEditTypes,
SynEditMiscProcs,
{$ENDIF}
Classes, SysUtils;
type
TSynEditRange = pointer;
TSynEditStringFlag = (sfHasTabs, sfHasNoTabs, sfExpandedLengthUnknown);
TSynEditStringFlags = set of TSynEditStringFlag;
PSynEditStringRec = ^TSynEditStringRec;
TSynEditStringRec = record
fString: string;
fObject: TObject;
fRange: TSynEditRange;
fExpandedLength: integer;
fFlags: TSynEditStringFlags;
end;
const
SLineBreak = {$IFDEF LINUX} #10 {$ELSE} #13#10 {$ENDIF};
SynEditStringRecSize = SizeOf(TSynEditStringRec);
MaxSynEditStrings = MaxInt div SynEditStringRecSize;
NullRange = TSynEditRange(-1);
type
PSynEditStringRecList = ^TSynEditStringRecList;
TSynEditStringRecList = array[0..MaxSynEditStrings - 1] of TSynEditStringRec;
TStringListChangeEvent = procedure(Sender: TObject; Index: Integer;
Count: integer) of object;
TSynEditFileFormat = (sffDos, sffUnix, sffMac); // DOS: CRLF, UNIX: LF, Mac: CR
TSynEditStringList = class(TStrings)
private
fList: PSynEditStringRecList;
fCount: integer;
fCapacity: integer;
fFileFormat: TSynEditFileFormat;
fAppendNewLineAtEOF: Boolean; //gbn 2002-04-25
{begin} //mh 2000-10-19
fConvertTabsProc: TConvertTabsProcEx;
fIndexOfLongestLine: integer;
fTabWidth: integer;
{end} //mh 2000-10-19
fOnChange: TNotifyEvent;
fOnChanging: TNotifyEvent;
fOnCleared: TNotifyEvent;
fOnDeleted: TStringListChangeEvent;
fOnInserted: TStringListChangeEvent;
fOnPutted: TStringListChangeEvent;
function ExpandString(Index: integer): string;
function GetExpandedString(Index: integer): string;
function GetExpandedStringLength(Index: integer): integer;
function GetLengthOfLongestLine: integer;
{end} //mh 2000-10-19
function GetRange(Index: integer): TSynEditRange;
procedure Grow;
procedure InsertItem(Index: integer; const S: string);
procedure PutRange(Index: integer; ARange: TSynEditRange);
protected
fLongestLineIndex: integer; //mh 2000-10-19
function Get(Index: integer): string; override;
function GetCapacity: integer;
{$IFDEF SYN_COMPILER_3_UP} override; {$ENDIF} //mh 2000-10-18
function GetCount: integer; override;
function GetObject(Index: integer): TObject; override;
function GetTextStr: string; override;
procedure Put(Index: integer; const S: string); override;
procedure PutObject(Index: integer; AObject: TObject); override;
procedure SetCapacity(NewCapacity: integer);
{$IFDEF SYN_COMPILER_3_UP} override; {$ENDIF} //mh 2000-10-18
procedure SetTabWidth(Value: integer); //mh 2000-10-19
procedure SetUpdateState(Updating: Boolean); override;
public
constructor Create;
destructor Destroy; override;
function Add(const S: string): integer; override;
procedure AddStrings(Strings: TStrings); override;
procedure Clear; override;
procedure Delete(Index: integer); override;
procedure DeleteLines(Index, NumLines: integer); // DJLP 2000-11-01
procedure Exchange(Index1, Index2: integer); override;
procedure Insert(Index: integer; const S: string); override;
procedure InsertLines(Index, NumLines: integer); // DJLP 2000-11-01
procedure InsertStrings(Index: integer; NewStrings: TStrings);
procedure InsertText(Index: integer; NewText: String);
procedure LoadFromFile(const FileName: string); override;
procedure SaveToFile(const FileName: string); override;
{begin} //Fiala 2001-12-17
procedure SaveToStream(Stream: TStream); override;
procedure LoadFromStream(Stream: TStream); override;
property AppendNewLineAtEOF: Boolean read fAppendNewLineAtEOF write fAppendNewLineAtEOF; //gbn 2002-04-25
property FileFormat: TSynEditFileFormat read fFileFormat write fFileFormat;
{end} //Fiala 2001-12-17
{begin} //mh 2000-10-19
property ExpandedStrings[Index: integer]: string read GetExpandedString;
property ExpandedStringLengths[Index: integer]: integer read GetExpandedStringLength;
property LengthOfLongestLine: integer read GetLengthOfLongestLine;
{end} //mh 2000-10-19
property Ranges[Index: integer]: TSynEditRange read GetRange write PutRange;
property TabWidth: integer read fTabWidth write SetTabWidth; //mh 2000-10-19
property OnChange: TNotifyEvent read fOnChange write fOnChange;
property OnChanging: TNotifyEvent read fOnChanging write fOnChanging;
property OnCleared: TNotifyEvent read fOnCleared write fOnCleared;
property OnDeleted: TStringListChangeEvent read fOnDeleted write fOnDeleted;
property OnInserted: TStringListChangeEvent read fOnInserted
write fOnInserted;
property OnPutted: TStringListChangeEvent read fOnPutted write fOnPutted;
end;
ESynEditStringList = class(Exception);
{end} //mh 2000-10-10
TSynChangeReason = (crInsert, crPaste, crDragDropInsert,
// Note: crSelDelete and crDragDropDelete have been deleted, because
// several undo entries can be chained together now via the ChangeNumber
// see also TCustomSynEdit.[Begin|End]UndoBlock methods
crDeleteAfterCursor, crDelete, {crSelDelete, crDragDropDelete, } //mh 2000-11-20
crLineBreak, crIndent, crUnindent,
crSilentDelete, crSilentDeleteAfterCursor, //mh 2000-10-30
crAutoCompleteBegin, crAutoCompleteEnd, //DDH 10/16/01 for AutoComplete
crPasteBegin, crPasteEnd, //DDH 05/16/03 for Pasting, since it might do a lot of operations
crSpecial1Begin, crSpecial1End, //DDH 10/16/01 for Special1
crSpecial2Begin, crSpecial2End, //DDH 10/16/01 for Special2
crCaret, //just restore the Caret, allowing better Undo behavior
crSelection, //restore Selection
crNothing,
crGroupBreak, //ek 2000-11-04
crDeleteAll, crWrap, crUnWrap, //Fiala 2001-12-17
crWhiteSpaceAdd //DDH 2001-1-7 for undo/redo of adding a character past EOL and repositioning the caret
);
TSynEditUndoItem = class(TPersistent)
protected
fChangeReason: TSynChangeReason;
fChangeSelMode: TSynSelectionMode;
fChangeStartPos: TBufferCoord;
fChangeEndPos: TBufferCoord;
fChangeStr: string;
fChangeNumber: integer; //sbs 2000-11-19
public
procedure Assign(Source: TPersistent); override;
{ public properties }
property ChangeReason: TSynChangeReason read fChangeReason;
property ChangeSelMode: TSynSelectionMode read fChangeSelMode;
property ChangeStartPos: TBufferCoord read fChangeStartPos;
property ChangeEndPos: TBufferCoord read fChangeEndPos;
property ChangeStr: string read fChangeStr;
property ChangeNumber: integer read fChangeNumber;
end;
TSynEditUndoList = class(TPersistent)
protected
fBlockChangeNumber: integer; //sbs 2000-11-19
fBlockCount: integer; //sbs 2000-11-19
fFullUndoImposible: boolean; //mh 2000-10-03
fItems: TList;
fLockCount: integer;
fMaxUndoActions: integer;
fNextChangeNumber: integer; //sbs 2000-11-19
fInitialChangeNumber: integer;
fOnAddedUndo: TNotifyEvent;
procedure EnsureMaxEntries;
function GetCanUndo: boolean;
function GetItemCount: integer;
procedure SetMaxUndoActions(Value: integer);
procedure SetInitialState(const Value: boolean);
function GetInitialState: boolean;
function GetItems(Index: Integer): TSynEditUndoItem;
procedure SetItems(Index: Integer; const Value: TSynEditUndoItem);
public
constructor Create;
destructor Destroy; override;
procedure AddChange(AReason: TSynChangeReason; const AStart, AEnd: TBufferCoord;
const ChangeText: string; SelMode: TSynSelectionMode);
procedure BeginBlock; //sbs 2000-11-19
procedure Clear;
procedure EndBlock; //sbs 2000-11-19
procedure Lock;
function PeekItem: TSynEditUndoItem;
function PopItem: TSynEditUndoItem;
procedure PushItem(Item: TSynEditUndoItem);
procedure Unlock;
function LastChangeReason: TSynChangeReason;
public
procedure Assign(Source: TPersistent); override;
procedure AddGroupBreak; //ek 2000-11-04
procedure DeleteItem(AIndex: Integer);
property BlockChangeNumber: integer read fBlockChangeNumber //sbs 2000-11-19
write fBlockChangeNumber;
property CanUndo: boolean read GetCanUndo;
property FullUndoImpossible: boolean read fFullUndoImposible; //mh 2000-10-03
property InitialState: boolean read GetInitialState write SetInitialState;
property Items[Index: Integer]: TSynEditUndoItem read GetItems write SetItems;
property ItemCount: integer read GetItemCount;
property BlockCount: integer read fBlockCount;
property MaxUndoActions: integer read fMaxUndoActions
write SetMaxUndoActions;
property OnAddedUndo: TNotifyEvent read fOnAddedUndo write fOnAddedUndo;
end;
implementation
{$IFDEF SYN_COMPILER_3_UP} //mh 2000-10-18
resourcestring
{$ELSE}
const
{$ENDIF}
SListIndexOutOfBounds = 'Invalid stringlist index %d';
SInvalidCapacity = 'Stringlist capacity cannot be smaller than count';
{ TSynEditFiler }
type
TSynEditFiler = class(TObject)
protected
fBuffer: PChar;
fBufPtr: Cardinal;
fBufSize: Cardinal;
fFileFormat: TSynEditFileFormat;
fFiler: TFileStream;
procedure Flush; virtual;
procedure SetBufferSize(NewSize: Cardinal);
public
constructor Create;
destructor Destroy; override;
public
property FileFormat: TSynEditFileFormat read fFileFormat write fFileFormat;
end;
constructor TSynEditFiler.Create;
const
kByte = 1024;
begin
inherited Create;
fFileFormat := sffUnix;
SetBufferSize(16 * kByte);
fBuffer[0] := #0;
end;
destructor TSynEditFiler.Destroy;
begin
Flush;
fFiler.Free;
SetBufferSize(0);
inherited Destroy;
end;
procedure TSynEditFiler.Flush;
begin
end;
procedure TSynEditFiler.SetBufferSize(NewSize: Cardinal);
begin
if NewSize <> fBufSize then begin
ReallocMem(fBuffer, NewSize);
fBufSize := NewSize;
end;
end;
{ TSynEditFileReader }
{type
TSynEditFileReader = class(TSynEditFiler)
protected
fFilePos: Cardinal;
fFileSize: Cardinal;
procedure FillBuffer;
public
constructor Create(const FileName: string);
function EOF: boolean;
function ReadLine: string;
end;
constructor TSynEditFileReader.Create(const FileName: string);
begin
inherited Create;
fFiler := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
fFileSize := fFiler.Size;
fFiler.Seek(0, soFromBeginning);
end;
function TSynEditFileReader.EOF: boolean;
begin
Result := (fBuffer[fBufPtr] = #0) and (fFilePos >= fFileSize);
end;
procedure TSynEditFileReader.FillBuffer;
var
Count: Cardinal;
begin
if fBufPtr >= fBufSize - 1 then
fBufPtr := 0;
Count := fFileSize - fFilePos;
if Count >= fBufSize - fBufPtr then
Count := fBufSize - fBufPtr - 1;
fFiler.ReadBuffer(fBuffer[fBufPtr], Count);
fBuffer[fBufPtr + Count] := #0;
fFilePos := fFilePos + Count;
fBufPtr := 0;
end;
function TSynEditFileReader.ReadLine: string;
var
E, P, S: PChar;
begin
repeat
S := PChar(@fBuffer[fBufPtr]);
if S[0] = #0 then begin
FillBuffer;
S := PChar(@fBuffer[0]);
end;
E := PChar(@fBuffer[fBufSize]);
P := S;
while P + 2 < E do begin
case P[0] of
#10, #13:
begin
SetString(Result, S, P - S);
if P[0] = #13 then
begin
if P[1] = #10 then
begin
fFileFormat := sffDos;
Inc(P);
end
else
fFileFOrmat := sffMac;
end;
Inc(P);
fBufPtr := P - fBuffer;
exit;
end;
#0:
if fFilePos >= fFileSize then begin
fBufPtr := P - fBuffer;
SetString(Result, S, P - S);
exit;
end;
end;
Inc(P);
end;
// put the partial string to the start of the buffer, and refill the buffer
Inc(P);
if S > fBuffer then
StrLCopy(fBuffer, S, P - S);
fBufPtr := P - S;
fBuffer[fBufPtr] := #0;
// if line is longer than half the buffer then grow it first
if 2 * Cardinal(P - S) > fBufSize then
SetBufferSize(fBufSize + fBufSize);
until FALSE;
end;
}
{ TSynEditFileWriter }
type
TSynEditFileWriter = class(TSynEditFiler)
protected
procedure Flush; override;
public
constructor Create(const FileName: string);
// procedure WriteLine(const S: string);
procedure WriteLine(const S: string; const FileFormat: TSynEditFileFormat); //Fiala
procedure Write(const S: String); //GBN 2002-04-16
end;
constructor TSynEditFileWriter.Create(const FileName: string);
begin
inherited Create;
fFiler := TFileStream.Create(FileName, fmCreate);
fFiler.Seek(0, soFromBeginning);
end;
procedure TSynEditFileWriter.Flush;
begin
if fBufPtr > 0 then begin
fFiler.WriteBuffer(fBuffer[0], fBufPtr);
fBufPtr := 0;
end;
end;
//GBN 2002-04-16
procedure TSynEditFileWriter.Write(const S: String);
var L: Cardinal;
begin
L := Length(S);
repeat
if fBufPtr + L <= fBufSize then begin
if L > 0 then begin
Move(S[1], fBuffer[fBufPtr], L);
fBufPtr := fBufPtr + L;
end;
exit;
end;
Flush;
if L > fBufSize then
SetBufferSize(L);
until False;
end;
procedure TSynEditFileWriter.WriteLine(const S: string;
const FileFormat: TSynEditFileFormat); //Fiala 2001-12-17
var
L, NL: Cardinal;
begin
L := Length(S);
NL := 1 + Ord(fFileFormat = sffDos);
repeat
if fBufPtr + L + NL <= fBufSize then begin
if L > 0 then begin
Move(S[1], fBuffer[fBufPtr], L);
fBufPtr := fBufPtr + L;
end;
if (fFileFormat <> sffUnix) then
begin
fBuffer[fBufPtr] := #13; // CR
Inc(fBufPtr);
end;
if (fFileFormat <> sffMac) then
begin
fBuffer[fBufPtr] := #10; // LF
Inc(fBufPtr);
end;
Exit;
end;
Flush;
if L + NL > fBufSize then
SetBufferSize(L + NL);
until FALSE;
end;
{ TSynEditStringList }
procedure ListIndexOutOfBounds(Index: integer);
begin
raise ESynEditStringList.CreateFmt(SListIndexOutOfBounds, [Index]);
end;
constructor TSynEditStringList.Create;
begin
inherited Create;
fAppendNewLineAtEOF:=true; //Retain current behavior gbn 2002-04-25
fFileFormat := sffDos;
{begin} //mh 2000-10-19
fIndexOfLongestLine := -1;
TabWidth := 8;
{end} //mh 2000-10-19
end;
destructor TSynEditStringList.Destroy;
begin
fOnChange := nil;
fOnChanging := nil;
inherited Destroy;
if fCount <> 0 then
Finalize(fList^[0], fCount);
fCount := 0;
SetCapacity(0);
end;
function TSynEditStringList.Add(const S: string): integer;
begin
BeginUpdate;
Result := fCount;
InsertItem(Result, S);
if Assigned(OnInserted) then
OnInserted( Self, Result, 1 );
EndUpdate;
end;
procedure TSynEditStringList.AddStrings(Strings: TStrings);
var
i, FirstAdded: integer;
begin
{begin} //mh 2000-10-19
if Strings.Count > 0 then begin
fIndexOfLongestLine := -1;
BeginUpdate;
try
i := fCount + Strings.Count;
if i > fCapacity then
SetCapacity((i + 15) and (not 15));
FirstAdded := fCount;
for i := 0 to Strings.Count - 1 do begin
with fList^[fCount] do begin
Pointer(fString) := nil;
fString := Strings[i];
fObject := Strings.Objects[i];
fRange := NullRange;
fExpandedLength := -1;
fFlags := [sfExpandedLengthUnknown];
end;
Inc(fCount);
end;
if Assigned(OnInserted) then
OnInserted( Self, FirstAdded, Strings.Count );
finally
EndUpdate;
end;
end;
{end} //mh 2000-10-19
end;
procedure TSynEditStringList.Clear;
begin
if fCount <> 0 then begin
BeginUpdate;
Finalize(fList^[0], fCount);
fCount := 0;
SetCapacity(0);
if Assigned(fOnCleared) then
fOnCleared(Self);
EndUpdate;
end;
fIndexOfLongestLine := -1; //mh 2000-10-19
end;
procedure TSynEditStringList.Delete(Index: integer);
begin
if (Index < 0) or (Index > fCount) then
ListIndexOutOfBounds(Index);
BeginUpdate;
Finalize(fList^[Index]);
Dec(fCount);
if Index < fCount then begin
System.Move(fList^[Index + 1], fList^[Index],
(fCount - Index) * SynEditStringRecSize);
end;
fIndexOfLongestLine := -1; //mh 2000-10-19
if Assigned(fOnDeleted) then
fOnDeleted( Self, Index, 1 );
EndUpdate;
end;
{begin} // DJLP 2000-11-01
procedure TSynEditStringList.DeleteLines(Index, NumLines: Integer);
var
LinesAfter: integer;
begin
if NumLines > 0 then begin
if (Index < 0) or (Index > fCount) then
ListIndexOutOfBounds(Index);
LinesAfter := fCount - (Index + NumLines - 1);
if LinesAfter < 0 then
NumLines := fCount - Index - 1;
Finalize(fList^[Index], NumLines);
if LinesAfter > 0 then begin
BeginUpdate;
try
System.Move(fList^[Index + NumLines], fList^[Index],
LinesAfter * SynEditStringRecSize);
finally
EndUpdate;
end;
end;
Dec(fCount, NumLines);
if Assigned(fOnDeleted) then
fOnDeleted( Self, Index, NumLines );
end;
end;
{end} // DJLP 2000-11-01
procedure TSynEditStringList.Exchange(Index1, Index2: integer);
var
Temp: TSynEditStringRec;
begin
if (Index1 < 0) or (Index1 >= fCount) then
ListIndexOutOfBounds(Index1);
if (Index2 < 0) or (Index2 >= fCount) then
ListIndexOutOfBounds(Index2);
BeginUpdate;
Temp := fList^[Index1];
fList^[Index1] := fList^[Index2];
fList^[Index2] := Temp;
{begin} //mh 2000-10-19
if fIndexOfLongestLine = Index1 then
fIndexOfLongestLine := Index2
else if fIndexOfLongestLine = Index2 then
fIndexOfLongestLine := Index1;
{end} //mh 2000-10-19
EndUpdate;
end;
{begin} //mh 2000-10-19
function TSynEditStringList.ExpandString(Index: integer): string;
var
HasTabs: boolean;
begin
with fList^[Index] do
if fString = '' then begin
Result := '';
Exclude(fFlags, sfExpandedLengthUnknown);
Exclude(fFlags, sfHasTabs);
Include(fFlags, sfHasNoTabs);
fExpandedLength := 0;
end else begin
Result := fConvertTabsProc(fString, fTabWidth, HasTabs);
fExpandedLength := Length(Result);
Exclude(fFlags, sfExpandedLengthUnknown);
Exclude(fFlags, sfHasTabs);
Exclude(fFlags, sfHasNoTabs);
if HasTabs then
Include(fFlags, sfHasTabs)
else
Include(fFlags, sfHasNoTabs);
end;
end;
{end} //mh 2000-10-19
function TSynEditStringList.Get(Index: integer): string;
begin
if (Index >= 0) and (Index < fCount) then
Result := fList^[Index].fString
else
Result := '';
end;
function TSynEditStringList.GetCapacity: integer;
begin
Result := fCapacity;
end;
function TSynEditStringList.GetCount: integer;
begin
Result := fCount;
end;
{begin} //mh 2000-10-19
function TSynEditStringList.GetExpandedString(Index: integer): string;
begin
if (Index >= 0) and (Index < fCount) then begin
if sfHasNoTabs in fList^[Index].fFlags then
Result := fList^[Index].fString
else
Result := ExpandString(Index);
end else
Result := '';
end;
function TSynEditStringList.GetExpandedStringLength(Index: integer): integer;
begin
if (Index >= 0) and (Index < fCount) then
begin
if sfExpandedLengthUnknown in fList^[Index].fFlags then
Result := Length( ExpandedStrings[index] )
else
Result := fList^[Index].fExpandedLength;
end
else
Result := 0;
end;
function TSynEditStringList.GetLengthOfLongestLine: integer; //mh 2000-10-19
var
i, MaxLen: integer;
PRec: PSynEditStringRec;
begin
if fIndexOfLongestLine < 0 then begin
MaxLen := 0;
if fCount > 0 then begin
PRec := @fList^[0];
for i := 0 to fCount - 1 do begin
if sfExpandedLengthUnknown in PRec^.fFlags then
ExpandString(i);
if PRec^.fExpandedLength > MaxLen then begin
MaxLen := PRec^.fExpandedLength;
fIndexOfLongestLine := i;
end;
Inc(PRec);
end;
end;
end;
if (fIndexOfLongestLine >= 0) and (fIndexOfLongestLine < fCount) then
Result := fList^[fIndexOfLongestLine].fExpandedLength
else
Result := 0;
end;
{end} //mh 2000-10-19
function TSynEditStringList.GetObject(Index: integer): TObject;
begin
if (Index >= 0) and (Index < fCount) then
Result := fList^[Index].fObject
else
Result := nil;
end;
function TSynEditStringList.GetRange(Index: integer): TSynEditRange;
begin
if (Index >= 0) and (Index < fCount) then
Result := fList^[Index].fRange
else
Result := nil;
end;
function TSynEditStringList.GetTextStr: string;
begin
Result := inherited GetTextStr;
System.Delete(Result, Length(Result) - Length(SLineBreak) + 1, Length(SLineBreak));
end;
procedure TSynEditStringList.Grow;
var
Delta: Integer;
begin
if fCapacity > 64 then
Delta := fCapacity div 4
else
Delta := 16;
SetCapacity(fCapacity + Delta);
end;
procedure TSynEditStringList.Insert(Index: integer; const S: string);
begin
if (Index < 0) or (Index > fCount) then
ListIndexOutOfBounds(Index);
BeginUpdate;
InsertItem(Index, S);
if Assigned(fOnInserted) then
fOnInserted( Self, Index, 1 );
EndUpdate;
end;
procedure TSynEditStringList.InsertItem(Index: integer; const S: string);
begin
BeginUpdate;
if fCount = fCapacity then
Grow;
if Index < fCount then begin
System.Move(fList^[Index], fList^[Index + 1],
(fCount - Index) * SynEditStringRecSize);
end;
fIndexOfLongestLine := -1; //mh 2000-10-19
with fList^[Index] do begin
Pointer(fString) := nil;
fString := S;
fObject := nil;
fRange := NullRange;
fExpandedLength := -1;
fFlags := [sfExpandedLengthUnknown];
end;
Inc(fCount);
EndUpdate;
end;
{begin} // DJLP 2000-11-01
procedure TSynEditStringList.InsertLines(Index, NumLines: integer);
begin
if (Index < 0) or (Index > fCount) then
ListIndexOutOfBounds(Index);
if NumLines > 0 then begin
BeginUpdate;
try
SetCapacity(fCount + NumLines);
if Index < fCount then begin
System.Move(fList^[Index], fList^[Index + NumLines],
(fCount - Index) * SynEditStringRecSize);
end;
FillChar(fList^[Index], NumLines * SynEditStringRecSize, 0);
Inc(fCount, NumLines);
if Assigned(OnInserted) then
OnInserted( Self, Index, NumLines );
finally
EndUpdate;
end;
end;
end;
procedure TSynEditStringList.InsertStrings(Index: integer;
NewStrings: TStrings);
var
i, Cnt: integer;
begin
Cnt := NewStrings.Count;
if Cnt = 0 then exit;
BeginUpdate;
try
InsertLines(Index, Cnt);
for i := 0 to Cnt - 1 do
Strings[Index + i] := NewStrings[i];
finally
EndUpdate;
end;
end;
procedure TSynEditStringList.InsertText(Index: integer;
NewText: String);
var
TmpStringList: TStringList;
begin
if NewText = '' then exit;
TmpStringList := TStringList.Create;
try
TmpStringList.Text := NewText;
InsertStrings(Index, TmpStringList);
finally
TmpStringList.Free;
end;
end;
{end} // DJLP 2000-11-01
procedure TSynEditStringList.LoadFromFile(const FileName: string);
var
// Reader: TSynEditFileReader; //Fiala
Stream: TStream;
{begin} //Fiala
begin
Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyNone);
try
LoadFromStream(Stream);
finally
Stream.Free;
end;
(*//Old Code, for reference
Reader := TSynEditFileReader.Create(FileName);
try
BeginUpdate;
try
Clear;
while not Reader.EOF do
Add(Reader.ReadLine);
fFileFormat := Reader.FileFormat;
finally
EndUpdate;
end;
finally
Reader.Free;
end;
*)
end;
{begin} //Fiala 2001-12-17
procedure TSynEditStringList.LoadFromStream(Stream: TStream);
var
Size: Integer;
S, S1: string;
P, Start: PChar;
fCR, fLF: Boolean;
iPos: Integer;
begin
fCR := False;
fLF := False;
try
BeginUpdate;
Size := Stream.Size;
Stream.Position := 0;
SetString(S, nil, Size);
Stream.Read(Pointer(S)^, Size);
Clear;
P := Pointer(S);
if P <> nil then
begin
iPos := 0;
while (iPos < Size) do // (P^ <> #0) do
begin
Start := P;
while not (P^ in [#10, #13]) and (iPos < Size) do
begin
Inc(P);
Inc(iPos);
end;
SetString(S1, Start, P - Start);
Add(S1);
if (P^ = #13) then
begin
fCR := True;
Inc(P);
Inc(iPos);
end;
if (P^ = #10) then
begin
fLF := True;
Inc(P);
Inc(iPos);
end;
end;
{ keep the old format of the file }
if (not AppendNewLineAtEOF) and (S[Size] in [#10,#13]) then
Add('');
end;
finally
EndUpdate;
end;
if fCR and not fLF then
fFileFormat := sffMac
else if fLF and not fCR then
fFileFormat := sffUnix
else
fFileFormat := sffDos;
end;
procedure TSynEditStringList.SaveToStream(Stream: TStream);
var
S, S1: string;
I, L, Size: Integer;
P: PChar;
LineEndLength: Integer;
begin
Size := 0;
if FileFormat in [sffMac, sffUnix] then
LineEndLength := 1
else
LineEndLength := 2;
for I := 0 to Count - 1 do Inc(Size, Length(Strings[I]) + LineEndLength);
if not AppendNewLineAtEOF then
Dec( Size, LineEndLength );
SetString(S, nil, Size);
P := Pointer(S);
for I := 0 to Count - 1 do begin
S1 := Strings[I];
L := Length(S1);
if L <> 0 then
begin
System.Move(Pointer(S1)^, P^, L);
Inc(P, L);
end;
//GBN 2002-04-16
//Do not add new line to last line
if (I < Count-1) or (AppendNewLineAtEOF) then begin
if FileFormat = sffMac then begin
P^ := #13;
Inc(P);
end else
if FileFormat = sffUnix then begin
P^ := #10;
Inc(P);
end else begin
P^ := #13;
Inc(P);
P^ := #10;
Inc(P);
end;
end;
end;