forked from doublecmd/doublecmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feditor.pas
1026 lines (918 loc) · 28.8 KB
/
feditor.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
{
Double Commander
-------------------------------------------------------------------------
Build-in Editor using SynEdit and his Highlighters
Copyright (C) 2006-2023 Alexander Koblov ([email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Legacy comment from its origin:
Build-in Editor for Seksi Commander
----------------------------
Licence : GNU GPL v 2.0
Author : [email protected]
This form used SynEdit and his Highlighters
contributors:
Copyright (C) 2006-2015 Alexander Koblov ([email protected])
}
unit fEditor;
{$mode objfpc}{$H+}
interface
uses
SysUtils, Classes, Controls, Forms, ActnList, Menus, SynEdit, StdCtrls, LMessages,
ComCtrls, SynEditSearch, SynEditHighlighter, uDebug, uOSForms, uShowForm, types, Graphics,
uFormCommands, uHotkeyManager, LCLVersion, SynPluginMultiCaret, fEditSearch;
const
HotkeysCategory = 'Editor';
type
{ TfrmEditor }
TfrmEditor = class(TAloneForm,IFormCommands)
actEditCut: TAction;
actEditCopy: TAction;
actEditSelectAll: TAction;
actEditUndo: TAction;
actEditRedo: TAction;
actEditPaste: TAction;
actEditDelete: TAction;
actEditFindNext: TAction;
actEditLineEndCrLf: TAction;
actEditLineEndCr: TAction;
actEditLineEndLf: TAction;
actEditGotoLine: TAction;
actEditFindPrevious: TAction;
actFileReload: TAction;
actEditTimeDate: TAction;
ilBookmarks: TImageList;
MainMenu1: TMainMenu;
ActListEdit: TActionList;
actAbout: TAction;
actFileOpen: TAction;
actFileClose: TAction;
actFileSave: TAction;
actFileSaveAs: TAction;
actFileNew: TAction;
actFileExit: TAction;
MenuItem1: TMenuItem;
miFileReload: TMenuItem;
miFindPrevious: TMenuItem;
miGotoLine: TMenuItem;
miTimeDate: TMenuItem;
miEditLineEndCr: TMenuItem;
miEditLineEndLf: TMenuItem;
miEditLineEndCrLf: TMenuItem;
miLineEndType: TMenuItem;
N5: TMenuItem;
miEncodingOut: TMenuItem;
miEncodingIn: TMenuItem;
miEncoding: TMenuItem;
miFindNext: TMenuItem;
miDelete: TMenuItem;
miSelectAll: TMenuItem;
miRedo: TMenuItem;
miDeleteContext: TMenuItem;
miSelectAllContext: TMenuItem;
miSeparator2: TMenuItem;
miPasteContext: TMenuItem;
miCopyContext: TMenuItem;
miCutContext: TMenuItem;
miSeparator1: TMenuItem;
miUndoContext: TMenuItem;
miFile: TMenuItem;
New1: TMenuItem;
Open1: TMenuItem;
pmContextMenu: TPopupMenu;
Save1: TMenuItem;
SaveAs1: TMenuItem;
N1: TMenuItem;
Exit1: TMenuItem;
miEdit: TMenuItem;
miUndo: TMenuItem;
N3: TMenuItem;
miCut: TMenuItem;
miCopy: TMenuItem;
miPaste: TMenuItem;
N4: TMenuItem;
miFind: TMenuItem;
miReplace: TMenuItem;
Help1: TMenuItem;
miAbout: TMenuItem;
StatusBar: TStatusBar;
Editor: TSynEdit;
miHighlight: TMenuItem;
actEditFind: TAction;
actEditRplc: TAction;
actConfHigh: TAction;
miDiv: TMenuItem;
miConfHigh: TMenuItem;
tbToolBar: TToolBar;
tbNew: TToolButton;
tbOpen: TToolButton;
tbSave: TToolButton;
tbSeparator1: TToolButton;
tbCut: TToolButton;
tbCopy: TToolButton;
tbPaste: TToolButton;
tbSeparator2: TToolButton;
tbUndo: TToolButton;
tbRedo: TToolButton;
tbSeparator3: TToolButton;
tbConfig: TToolButton;
tbHelp: TToolButton;
procedure actExecute(Sender: TObject);
procedure EditorMouseWheelDown(Sender: TObject; Shift: TShiftState;
{%H-}MousePos: TPoint; var Handled: Boolean);
procedure EditorMouseWheelUp(Sender: TObject; Shift: TShiftState;
{%H-}MousePos: TPoint; var Handled: Boolean);
procedure FormCreate(Sender: TObject);
procedure EditorReplaceText(Sender: TObject; const ASearch, AReplace: string;
{%H-}Line, {%H-}Column: integer; var ReplaceAction: TSynReplaceAction);
procedure EditorChange(Sender: TObject);
procedure EditorStatusChange(Sender: TObject;
{%H-}Changes: TSynStatusChanges);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure frmEditorClose(Sender: TObject; var CloseAction: TCloseAction);
private
{ Private declarations }
bNoName: Boolean;
FSearchOptions: TEditSearchOptions;
FFileName: String;
sEncodingIn,
sEncodingOut,
sEncodingStat,
sOriginalText: String;
FWaitData: TWaitData;
FElevate: TDuplicates;
FCommands: TFormCommands;
FMultiCaret: TSynPluginMultiCaret;
property Commands: TFormCommands read FCommands implements IFormCommands;
procedure ChooseEncoding(mnuMenuItem: TMenuItem; sEncoding: String);
{en
Saves editor content to a file.
@returns(@true if successful)
}
function SaveFile(const aFileName: String): Boolean;
procedure SetFileName(const AValue: String);
protected
procedure CMThemeChanged(var Message: TLMessage); message CM_THEMECHANGED;
public
{ Public declarations }
SynEditSearch: TSynEditSearch;
{
Function CreateNewTab:Integer; // return tab number
Function OpenFileNewTab(const sFileName:String):Integer;
}
destructor Destroy; override;
procedure AfterConstruction; override;
{en
Opens a file.
@returns(@true if successful)
}
function OpenFile(const aFileName: String): Boolean;
procedure UpdateStatus;
procedure SetEncodingIn(Sender:TObject);
procedure SetEncodingOut(Sender:TObject);
procedure SetHighLighter(Sender:TObject);
procedure UpdateHighlighter(Highlighter: TSynCustomHighlighter);
procedure LoadGlobalOptions;
property FileName: String read FFileName write SetFileName;
published
procedure cm_FileReload(const Params: array of string);
procedure cm_EditFind(const {%H-}Params:array of string);
procedure cm_EditFindNext(const {%H-}Params:array of string);
procedure cm_EditFindPrevious(const {%H-}Params:array of string);
procedure cm_EditGotoLine(const {%H-}Params:array of string);
procedure cm_EditTimeDate(const {%H-}Params:array of string);
procedure cm_EditLineEndCr(const {%H-}Params:array of string);
procedure cm_EditLineEndCrLf(const {%H-}Params:array of string);
procedure cm_EditLineEndLf(const {%H-}Params:array of string);
procedure cm_EditDelete(const {%H-}Params:array of string);
procedure cm_EditRedo(const {%H-}Params:array of string);
procedure cm_About(const {%H-}Params:array of string);
procedure cm_EditCopy(const {%H-}Params:array of string);
procedure cm_EditCut(const {%H-}Params:array of string);
procedure cm_EditPaste(const {%H-}Params:array of string);
procedure cm_EditSelectAll(const {%H-}Params:array of string);
procedure cm_FileNew(const {%H-}Params:array of string);
procedure cm_FileOpen(const {%H-}Params:array of string);
procedure cm_EditUndo(const {%H-}Params:array of string);
procedure cm_FileSave(const {%H-}Params:array of string);
procedure cm_FileSaveAs(const {%H-}Params:array of string);
procedure cm_FileExit(const {%H-}Params:array of string);
procedure cm_ConfHigh(const {%H-}Params:array of string);
procedure cm_EditRplc(const {%H-}Params:array of string);
end;
procedure ShowEditor(const sFileName: String; WaitData: TWaitData = nil);
var
LastEditorUsedForConfiguration: TfrmEditor = nil;
implementation
{$R *.lfm}
uses
Clipbrd, dmCommonData, dmHigh, SynEditTypes, LCLType, LConvEncoding,
uLng, uShowMsg, uGlobs, fOptions, DCClassesUtf8, uAdministrator, uHighlighters,
uOSUtils, uConvEncoding, fOptionsToolsEditor, uDCUtils, uClipboard, uFindFiles,
DCOSUtils;
procedure ShowEditor(const sFileName: String; WaitData: TWaitData = nil);
var
Editor: TfrmEditor;
begin
Editor := TfrmEditor.Create(Application);
Editor.FWaitData := WaitData;
if sFileName = '' then
Editor.cm_FileNew([''])
else
begin
if not Editor.OpenFile(sFileName) then
Exit;
end;
if (WaitData = nil) then
Editor.ShowOnTop
else begin
WaitData.ShowOnTop(Editor);
end;
LastEditorUsedForConfiguration := Editor;
end;
procedure TfrmEditor.FormCreate(Sender: TObject);
var
i:Integer;
mi:TMenuItem;
HMEditor: THMForm;
miOther: TMenuItem = nil;
EncodingsList: TStringList;
Options: TTextSearchOptions;
begin
InitPropStorage(Self);
Menu.Images:= dmComData.ilEditorImages;
LoadGlobalOptions;
// update menu highlighting
miHighlight.Clear;
for i:= 0 to dmHighl.SynHighlighterList.Count - 1 do
begin
mi:= TMenuItem.Create(miHighlight);
mi.Caption:= TSynCustomHighlighter(dmHighl.SynHighlighterList.Objects[i]).LanguageName;
mi.Tag:= i;
mi.Enabled:= True;
mi.OnClick:=@SetHighLighter;
if not TSynCustomHighlighter(dmHighl.SynHighlighterList.Objects[i]).Other then
miHighlight.Add(mi)
else begin
if (miOther = nil) then
begin
miOther:= TMenuItem.Create(miHighlight);
miOther.Caption:= rsDlgButtonOther;
end;
miOther.Add(mi);
end;
end;
if Assigned(miOther) then
miHighlight.Add(miOther);
// update menu encoding
miEncodingIn.Clear;
miEncodingOut.Clear;
EncodingsList:= TStringList.Create;
GetSupportedEncodings(EncodingsList);
for I:= 0 to EncodingsList.Count - 1 do
begin
mi:= TMenuItem.Create(miEncodingIn);
mi.Caption:= EncodingsList[I];
mi.AutoCheck:= True;
mi.RadioItem:= True;
mi.GroupIndex:= 1;
mi.OnClick:= @SetEncodingIn;
miEncodingIn.Add(mi);
end;
for I:= 0 to EncodingsList.Count - 1 do
begin
mi:= TMenuItem.Create(miEncodingOut);
mi.Caption:= EncodingsList[I];
mi.AutoCheck:= True;
mi.RadioItem:= True;
mi.GroupIndex:= 2;
mi.OnClick:= @SetEncodingOut;
miEncodingOut.Add(mi);
end;
EncodingsList.Free;
FSearchOptions.Flags := [ssoEntireScope];
// if we already search text then use last searched text
if not gFirstTextSearch then
begin
for I:= 0 to glsSearchHistory.Count - 1 do
begin
Options:= TTextSearchOptions(UInt32(UIntPtr(glsSearchHistory.Objects[I])));
if (tsoHex in Options) then
Continue;
if (tsoMatchCase in Options) then
FSearchOptions.Flags += [ssoMatchCase];
if (tsoRegExpr in Options) then
FSearchOptions.Flags += [ssoRegExpr];
FSearchOptions.SearchText:= glsSearchHistory[I];
Break;
end;
end;
FixFormIcon(Handle);
HMEditor := HotMan.Register(Self, HotkeysCategory);
HMEditor.RegisterActionList(ActListEdit);
FCommands := TFormCommands.Create(Self, ActListEdit);
FMultiCaret := TSynPluginMultiCaret.Create(Editor);
end;
procedure TfrmEditor.LoadGlobalOptions;
begin
Editor.Options:= gEditorSynEditOptions;
FontOptionsToFont(gFonts[dcfEditor], Editor.Font);
Editor.TabWidth := gEditorSynEditTabWidth;
Editor.RightEdge := gEditorSynEditRightEdge;
Editor.BlockIndent := gEditorSynEditBlockIndent;
end;
procedure TfrmEditor.actExecute(Sender: TObject);
var
cmd: string;
begin
cmd := (Sender as TAction).Name;
cmd := 'cm_' + Copy(cmd, 4, Length(cmd) - 3);
Commands.ExecuteCommand(cmd, []);
end;
procedure TfrmEditor.EditorMouseWheelDown(Sender: TObject; Shift: TShiftState;
MousePos: TPoint; var Handled: Boolean);
var
t:integer;
begin
if (Shift=[ssCtrl])and(gFonts[dcfEditor].Size > gFonts[dcfEditor].MinValue) then
begin
t:=Editor.TopLine;
gFonts[dcfEditor].Size:=gFonts[dcfEditor].Size-1;
FontOptionsToFont(gFonts[dcfEditor], Editor.Font);
Editor.TopLine:=t;
Editor.Refresh;
Handled:=True;
end;
end;
procedure TfrmEditor.EditorMouseWheelUp(Sender: TObject; Shift: TShiftState;
MousePos: TPoint; var Handled: Boolean);
var
t:integer;
begin
if (Shift=[ssCtrl])and(gFonts[dcfEditor].Size < gFonts[dcfEditor].MaxValue) then
begin
t:=Editor.TopLine;
gFonts[dcfEditor].Size:=gFonts[dcfEditor].Size+1;
FontOptionsToFont(gFonts[dcfEditor], Editor.Font);
Editor.TopLine:=t;
Editor.Refresh;
Handled:=True;
end;
end;
function TfrmEditor.OpenFile(const aFileName: String): Boolean;
var
Buffer: AnsiString;
Reader: TFileStreamUAC;
Highlighter: TSynCustomHighlighter;
begin
PushPop(FElevate);
try
Result := False;
try
Reader := TFileStreamUAC.Create(aFileName, fmOpenRead or fmShareDenyNone);
try
SetLength(sOriginalText, Reader.Size);
actFileSave.Enabled:= not FileIsReadOnlyEx(Reader.Handle);
Reader.Read(Pointer(sOriginalText)^, Length(sOriginalText));
finally
Reader.Free;
end;
// Try to detect encoding by first 4 kb of text
Buffer := Copy(sOriginalText, 1, 4096);
sEncodingIn := DetectEncoding(Buffer);
ChooseEncoding(miEncodingIn, sEncodingIn);
sEncodingOut := sEncodingIn; // by default
ChooseEncoding(miEncodingOut, sEncodingOut);
// Try to guess line break style
with Editor.Lines do
begin
if (sEncodingIn <> EncodingUTF16LE) and (sEncodingIn <> EncodingUTF16BE) then
TextLineBreakStyle := GuessLineBreakStyle(Buffer)
else begin
sOriginalText := Copy(sOriginalText, 3, MaxInt); // Skip BOM
TextLineBreakStyle := GuessLineBreakStyle(ConvertEncoding(Buffer, sEncodingIn, EncodingUTF8));
end;
case TextLineBreakStyle of
tlbsCRLF: actEditLineEndCrLf.Checked := True;
tlbsCR: actEditLineEndCr.Checked := True;
tlbsLF: actEditLineEndLf.Checked := True;
end;
end;
// Convert encoding if needed
if sEncodingIn = EncodingUTF8 then
Buffer := sOriginalText
else begin
Buffer := ConvertEncoding(sOriginalText, sEncodingIn, EncodingUTF8);
end;
// Load text into editor
Editor.Lines.Text := Buffer;
// Add empty line if needed
if (Length(Buffer) > 0) and (Buffer[Length(Buffer)] in [#10, #13]) then
Editor.Lines.Add(EmptyStr);
Result := True;
except
on E: EFCreateError do
begin
DCDebug(E.Message);
msgError(rsMsgErrECreate + ' ' + aFileName);
Exit;
end;
on E: EFOpenError do
begin
DCDebug(E.Message);
msgError(rsMsgErrEOpen + ' ' + aFileName);
Exit;
end;
on E: EReadError do
begin
DCDebug(E.Message);
msgError(rsMsgErrERead + ' ' + aFileName);
Exit;
end;
end;
// set up highlighter
Highlighter := dmHighl.GetHighlighter(Editor, ExtractFileExt(aFileName));
UpdateHighlighter(Highlighter);
FileName := aFileName;
Editor.Modified := False;
bNoname := False;
UpdateStatus;
finally
PushPop(FElevate);
end;
end;
function TfrmEditor.SaveFile(const aFileName: String): Boolean;
var
TextOut: String;
Encoding: String;
Writer: TFileStreamUAC;
begin
PushPop(FElevate);
try
Result := False;
try
Writer := TFileStreamUAC.Create(aFileName, fmCreate);
try
Encoding := NormalizeEncoding(sEncodingOut);
// If file is empty and encoding with BOM then write only BOM
if (Editor.Lines.Count = 0) then
begin
if (Encoding = EncodingUTF8BOM) then
Writer.WriteBuffer(UTF8BOM, SizeOf(UTF8BOM))
else if (Encoding = EncodingUTF16LE) then
Writer.WriteBuffer(UTF16LEBOM, SizeOf(UTF16LEBOM))
else if (Encoding = EncodingUTF16BE) then
Writer.WriteBuffer(UTF16BEBOM, SizeOf(UTF16BEBOM));
end
else begin
TextOut := EmptyStr;
if (Encoding = EncodingUTF16LE) then
TextOut := UTF16LEBOM
else if (Encoding = EncodingUTF16BE) then begin
TextOut := UTF16BEBOM
end;
TextOut += ConvertEncoding(Editor.Lines[0], EncodingUTF8, sEncodingOut);
Writer.WriteBuffer(Pointer(TextOut)^, Length(TextOut));
// If file has only one line then write it without line break
if Editor.Lines.Count > 1 then
begin
TextOut := TextLineBreakValue[Editor.Lines.TextLineBreakStyle];
TextOut += GetTextRange(Editor.Lines, 1, Editor.Lines.Count - 2);
// Special case for UTF-8 and UTF-8 with BOM
if (Encoding <> EncodingUTF8) and (Encoding <> EncodingUTF8BOM) then begin
TextOut:= ConvertEncoding(TextOut, EncodingUTF8, sEncodingOut);
end;
Writer.WriteBuffer(Pointer(TextOut)^, Length(TextOut));
// Write last line without line break
TextOut:= Editor.Lines[Editor.Lines.Count - 1];
// Special case for UTF-8 and UTF-8 with BOM
if (Encoding <> EncodingUTF8) and (Encoding <> EncodingUTF8BOM) then begin
TextOut:= ConvertEncoding(TextOut, EncodingUTF8, sEncodingOut);
end;
Writer.WriteBuffer(Pointer(TextOut)^, Length(TextOut));
end;
end;
// Refresh original text and encoding
if (sEncodingIn <> sEncodingOut) or (Length(sOriginalText) = 0) then
begin
sEncodingIn:= sEncodingOut;
ChooseEncoding(miEncodingIn, sEncodingIn);
if (sEncodingOut <> EncodingUTF16LE) and (sEncodingOut <> EncodingUTF16BE) then
begin
Writer.Seek(0, soBeginning);
SetLength(sOriginalText, Writer.Size);
end
else begin
Writer.Seek(2, soBeginning);
SetLength(sOriginalText, Writer.Size - 2);
end;
Writer.Read(Pointer(sOriginalText)^, Length(sOriginalText));
end;
finally
Writer.Free;
end;
Editor.Modified := False; // needed for the undo stack
Editor.MarkTextAsSaved;
Result := True;
except
on E: Exception do
msgError(rsMsgErrSaveFile + ' ' + aFileName + LineEnding + E.Message);
end;
finally
PushPop(FElevate);
end;
end;
procedure TfrmEditor.SetFileName(const AValue: String);
begin
if FFileName = AValue then
Exit;
FFileName := AValue;
Caption := ReplaceHome(FFileName);
end;
procedure TfrmEditor.CMThemeChanged(var Message: TLMessage);
var
Highlighter: TSynCustomHighlighter;
begin
Highlighter:= TSynCustomHighlighter(dmHighl.SynHighlighterHashList.Data[StatusBar.Panels[4].Text]);
if Assigned(Highlighter) then dmHighl.SetHighlighter(Editor, Highlighter);
end;
destructor TfrmEditor.Destroy;
begin
LastEditorUsedForConfiguration := nil;
HotMan.UnRegister(Self);
inherited Destroy;
if Assigned(FWaitData) then FWaitData.Done;
end;
procedure TfrmEditor.AfterConstruction;
begin
inherited AfterConstruction;
tbToolBar.ImagesWidth:= gToolIconsSize;
tbToolBar.SetButtonSize(gToolIconsSize + ScaleX(6, 96),
gToolIconsSize + ScaleY(6, 96));
end;
procedure TfrmEditor.EditorReplaceText(Sender: TObject; const ASearch,
AReplace: string; Line, Column: integer; var ReplaceAction: TSynReplaceAction );
begin
if ASearch = AReplace then
ReplaceAction := raSkip
else begin
case MsgBox(rsMsgReplaceThisText, [msmbYes, msmbNo, msmbCancel, msmbAll], msmbYes, msmbNo) of
mmrYes: ReplaceAction := raReplace;
mmrAll: ReplaceAction := raReplaceAll;
mmrNo: ReplaceAction := raSkip;
else
ReplaceAction := raCancel;
end;
end;
end;
procedure TfrmEditor.SetHighLighter(Sender:TObject);
var
Highlighter: TSynCustomHighlighter;
begin
Highlighter:= TSynCustomHighlighter(dmHighl.SynHighlighterList.Objects[TMenuItem(Sender).Tag]);
UpdateHighlighter(Highlighter);
end;
(*
This is code for multi tabs editor, it's buggy because
Synedit bad handle scrollbars in page control, maybe in
future, workaround: new tab must be visible and maybe must have focus
procedure TfrmEditor.cm_FileNewExecute(Sender: TObject);
var
iPageIndex:Integer;
begin
inherited;
iPageIndex:=CreateNewTab;
with pgEditor.Pages[iPageIndex] do
begin
Caption:='New'+IntToStr(iPageIndex);
Hint:=''; // filename
end;
end;
Function TfrmEditor.CreateNewTab:Integer; // return tab number
var
iPageIndex:Integer;
begin
with TTabSheet.Create(pgEditor) do // create Tab
begin
PageControl:=pgEditor;
iPageIndex:=PageIndex;
// now create Editor
with TSynEdit.Create(pgEditor.Pages[PageIndex]) do
begin
Parent:=pgEditor.Pages[PageIndex];
Align:=alClient;
Lines.Clear;
end;
end;
end;
procedure TfrmEditor.cm_FileOpenExecute(const Params:array of string);
var
iPageIndex:Integer;
begin
inherited;
dmDlg.OpenDialog.Filter:='*.*';
if dmDlg.OpenDialog.Execute then
OpenFileNewTab(dmDlg.OpenDialog.FileName);
end;
Function TfrmEditor.OpenFileNewTab(const sFileName:String):Integer;
var
iPageIndex:Integer;
begin
inherited;
iPageIndex:=CreateNewTab;
pgEditor.ActivePageIndex:=iPageIndex;
with pgEditor.Pages[iPageIndex] do
begin
Caption:=sFileName;
Hint:=sFileName;
TSynEdit(pgEditor.Pages[iPageIndex].Components[0]).Lines.LoadFromFile(sFileName);
end;
end;
procedure ShowEditor(lsFiles:TStringList);
var
i:Integer;
begin
with TfrmEditor.Create(Application) do
begin
try
for i:=0 to lsFiles.Count-1 do
OpenFileNewTab(lsFiles.Strings[i]);
ShowModal;
finally
Free;
end;
end;
end;
*)
procedure TfrmEditor.EditorChange(Sender: TObject);
begin
UpdateStatus;
end;
procedure TfrmEditor.UpdateStatus;
const
BreakStyle: array[TTextLineBreakStyle] of String = ('LF', 'CRLF', 'CR');
begin
if Editor.Modified then
StatusBar.Panels[0].Text:= '*'
else begin
StatusBar.Panels[0].Text:= '';
end;
StatusBar.Panels[1].Text:= Format('%d:%d',[Editor.CaretX, Editor.CaretY]);
StatusBar.Panels[2].Text:= sEncodingStat;
StatusBar.Panels[3].Text:= BreakStyle[Editor.Lines.TextLineBreakStyle];
end;
procedure TfrmEditor.SetEncodingIn(Sender: TObject);
begin
sEncodingStat:= (Sender as TMenuItem).Caption;
sEncodingIn:= sEncodingStat;
sEncodingOut:= sEncodingStat;
ChooseEncoding(miEncodingOut, sEncodingOut);
Editor.Lines.Text:= ConvertEncoding(sOriginalText, sEncodingIn, EncodingUTF8);
UpdateStatus;
end;
procedure TfrmEditor.SetEncodingOut(Sender: TObject);
begin
sEncodingOut:= (Sender as TMenuItem).Caption;
end;
procedure TfrmEditor.EditorStatusChange(Sender: TObject;
Changes: TSynStatusChanges);
begin
UpdateStatus;
miEncodingIn.Enabled := not Editor.Modified;
end;
procedure TfrmEditor.UpdateHighlighter(Highlighter: TSynCustomHighlighter);
begin
dmHighl.SetHighlighter(Editor, Highlighter);
StatusBar.Panels[4].Text:= Highlighter.LanguageName;
end;
procedure TfrmEditor.FormCloseQuery(Sender: TObject;
var CanClose: Boolean);
begin
if not Editor.Modified then
CanClose:= True
else begin
case msgYesNoCancel(Format(rsMsgFileChangedSave,[FileName])) of
mmrYes:
begin
cm_FileSave(['']);
CanClose:= not Editor.Modified;
end;
mmrNo: CanClose:= True;
else
CanClose:= False;
end;
end;
end;
procedure TfrmEditor.cm_FileReload(const Params: array of string);
begin
if Editor.Modified then
begin
if not msgYesNo(rsMsgFileReloadWarning) then
Exit;
end;
OpenFile(FFileName);
end;
procedure TfrmEditor.cm_EditFind(const Params: array of string);
begin
ShowSearchReplaceDialog(Self, Editor, cbUnchecked, FSearchOptions);
end;
procedure TfrmEditor.cm_EditFindNext(const Params:array of string);
begin
if gFirstTextSearch then
begin
FSearchOptions.Flags -= [ssoBackwards];
ShowSearchReplaceDialog(Self, Editor, cbUnchecked, FSearchOptions)
end
else if FSearchOptions.SearchText <> '' then
begin
DoSearchReplaceText(Editor, False, False, FSearchOptions);
FSearchOptions.Flags -= [ssoEntireScope];
end;
end;
procedure TfrmEditor.cm_EditFindPrevious(const Params: array of string);
begin
if gFirstTextSearch then
begin
FSearchOptions.Flags += [ssoBackwards];
ShowSearchReplaceDialog(Self, Editor, cbUnchecked, FSearchOptions);
end
else if FSearchOptions.SearchText <> '' then
begin
Editor.SelEnd := Editor.SelStart;
DoSearchReplaceText(Editor, False, True, FSearchOptions);
FSearchOptions.Flags -= [ssoEntireScope];
end;
end;
procedure TfrmEditor.cm_EditGotoLine(const Params:array of string);
var
P: TPoint;
Value: String;
NewTopLine: Integer;
begin
if ShowInputQuery(rsEditGotoLineTitle, rsEditGotoLineQuery, Value) then
begin
P.X := 1;
P.Y := StrToIntDef(Value, 1);
NewTopLine := P.Y - (Editor.LinesInWindow div 2);
if NewTopLine < 1 then NewTopLine:= 1;
Editor.CaretXY := P;
Editor.TopLine := NewTopLine;
Editor.SetFocus;
end;
end;
procedure TfrmEditor.cm_EditTimeDate(const Params:array of string);
begin
Editor.InsertTextAtCaret (FormatDateTime ('hh:nn ddddd', Now));
end;
procedure TfrmEditor.cm_EditLineEndCr(const Params:array of string);
begin
Editor.Lines.TextLineBreakStyle:= tlbsCR;
UpdateStatus;
end;
procedure TfrmEditor.cm_EditLineEndCrLf(const Params:array of string);
begin
Editor.Lines.TextLineBreakStyle:= tlbsCRLF;
UpdateStatus;
end;
procedure TfrmEditor.cm_EditLineEndLf(const Params:array of string);
begin
Editor.Lines.TextLineBreakStyle:= tlbsLF;
UpdateStatus;
end;
procedure TfrmEditor.cm_About(const Params:array of string);
begin
msgOK(rsEditAboutText);
end;
procedure TfrmEditor.cm_EditCopy(const Params:array of string);
begin
editor.CopyToClipboard;
{$IF DEFINED(LCLGTK2) and (LCL_FULLVERSION < 1100000)}
// Workaround for Lazarus bug #0021453
ClipboardSetText(Clipboard.AsText);
{$ENDIF}
end;
procedure TfrmEditor.cm_EditCut(const Params:array of string);
begin
Editor.CutToClipboard;
{$IF DEFINED(LCLGTK2) and (LCL_FULLVERSION < 1100000)}
// Workaround for Lazarus bug #0021453
ClipboardSetText(Clipboard.AsText);
{$ENDIF}
end;
procedure TfrmEditor.cm_EditPaste(const Params:array of string);
begin
editor.PasteFromClipboard;
end;
procedure TfrmEditor.cm_EditDelete(const Params:array of string);
begin
Editor.ClearSelection;
end;
procedure TfrmEditor.cm_EditRedo(const Params:array of string);
begin
editor.Redo;
end;
procedure TfrmEditor.cm_EditSelectAll(const Params:array of string);
begin
editor.SelectAll;
end;
procedure TfrmEditor.cm_FileNew(const Params:array of string);
var
CanClose: Boolean = False;
begin
FormCloseQuery(Self, CanClose);
if not CanClose then Exit;
FileName := rsMsgNewFile;
Editor.Lines.Clear;
Editor.Modified:= False;
actFileSave.Enabled:= True;
bNoname:= True;
UpdateStatus;
end;
procedure TfrmEditor.cm_FileOpen(const Params:array of string);
var
CanClose: Boolean = False;
begin
FormCloseQuery(Self, CanClose);
if not CanClose then Exit;
dmComData.OpenDialog.Filter:= AllFilesMask;
if not dmComData.OpenDialog.Execute then Exit;
if OpenFile(dmComData.OpenDialog.FileName) then
UpdateStatus;
end;
procedure TfrmEditor.cm_EditUndo(const Params:array of string);
begin
Editor.Undo;
UpdateStatus;
end;
procedure TfrmEditor.cm_FileSave(const Params:array of string);
begin
if bNoname then
actFileSaveAs.Execute
else
begin
SaveFile(FileName);
UpdateStatus;
end;
end;
procedure TfrmEditor.cm_FileSaveAs(const Params:array of string);
var
Highlighter: TSynCustomHighlighter;
begin
dmComData.SaveDialog.FileName := FileName;
dmComData.SaveDialog.Filter:= AllFilesMask; // rewrite for highlighter
if not dmComData.SaveDialog.Execute then
Exit;
FileName := dmComData.SaveDialog.FileName;
if SaveFile(FileName) then
begin
actFileSave.Enabled:= True;
end;
bNoname:=False;
UpdateStatus;
Highlighter:= dmHighl.GetHighlighter(Editor, ExtractFileExt(FileName));
UpdateHighlighter(Highlighter);
end;
procedure TfrmEditor.cm_FileExit(const Params:array of string);
begin
Close;
end;
procedure TfrmEditor.cm_ConfHigh(const Params:array of string);
begin
LastEditorUsedForConfiguration := Self;
ShowOptions(TfrmOptionsEditor);
end;
procedure TfrmEditor.cm_EditRplc(const Params: array of string);
begin
ShowSearchReplaceDialog(Self, Editor, cbChecked, FSearchOptions)
end;