forked from Alexey-T/CudaLister
-
Notifications
You must be signed in to change notification settings - Fork 0
/
form_main.pas
1757 lines (1587 loc) · 47 KB
/
form_main.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
unit form_main;
{$mode objfpc}{$H+}
interface
uses
Windows, SysUtils, Classes, Graphics,
LCLType, LCLProc, LCLIntf,
Forms, Controls, ExtCtrls, Dialogs, Menus,
IniFiles, StrUtils,
Clipbrd,
EncConv,
ATSynEdit,
ATSynEdit_Carets,
ATSynEdit_Adapter_EControl,
ATSynEdit_Commands,
ATSynEdit_Finder,
ATSynEdit_Markers,
ATSynEdit_Globals,
ATSynEdit_CharSizer,
ATSynEdit_Keymap,
ATStrings,
ATStringProc,
ATStatusbar,
ATScrollBar,
ATFlatThemes,
ec_SyntAnal,
ec_proc_lexer,
file_proc,
proc_themes,
form_options,
form_find,
FileUtil;
const
cEditorIsReadOnly = false;
type
{ TfmMain }
TfmMain = class(TForm)
ed: TATSynEdit;
mnuWrap: TMenuItem;
mnuFind: TMenuItem;
mnuTextCut: TMenuItem;
mnuTextUndo: TMenuItem;
mnuTextRedo: TMenuItem;
mnuTextSave: TMenuItem;
mnuTextPaste: TMenuItem;
mnuTextDelete: TMenuItem;
mnuTextReadonly: TMenuItem;
mnuTextUpperCase: TMenuItem;
mnuTextLowerCase: TMenuItem;
mnuTextGoto: TMenuItem;
MenuItem2: TMenuItem;
MenuItem3: TMenuItem;
MenuItem4: TMenuItem;
MenuItem5: TMenuItem;
MenuItem6: TMenuItem;
mnuOptions: TMenuItem;
mnuTextCopy: TMenuItem;
mnuTextSel: TMenuItem;
PanelAll: TPanel;
PopupLexers: TPopupMenu;
PopupEnc: TPopupMenu;
PopupText: TPopupMenu;
PopupFileEnd: TPopupMenu;
mnuEndWin: TMenuItem;
mnuEndUnix: TMenuItem;
mnuEndMac: TMenuItem;
TimerEmpty: TTimer;
TimerStatusbar: TTimer;
procedure edChangeCaretPos(Sender: TObject);
procedure edClickLink(Sender: TObject; const ALink: string);
procedure edCommand(Sender: TObject; ACommand: integer;
const AText: string; var AHandled: boolean);
procedure edKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure mnuFindClick(Sender: TObject);
procedure mnuOptionsClick(Sender: TObject);
procedure mnuTextCutClick(Sender: TObject);
procedure mnuTextCopyClick(Sender: TObject);
procedure mnuTextGotoClick(Sender: TObject);
procedure mnuTextPasteClick(Sender: TObject);
procedure mnuTextDeleteClick(Sender: TObject);
procedure mnuTextReadonlyClick(Sender: TObject);
procedure mnuTextUpperCaseClick(Sender: TObject);
procedure mnuTextLowerCaseClick(Sender: TObject);
procedure mnuTextUndoClick(Sender: TObject);
procedure mnuTextRedoClick(Sender: TObject);
procedure mnuTextSaveClick(Sender: TObject);
procedure mnuTextSelClick(Sender: TObject);
procedure mnuWrapClick(Sender: TObject);
procedure mnuEndWinClick(Sender: TObject);
procedure mnuEndUnixClick(Sender: TObject);
procedure mnuEndMacClick(Sender: TObject);
procedure PopupTextPopup(Sender: TObject);
procedure TimerEmptyTimer(Sender: TObject);
procedure TimerStatusbarTimer(Sender: TObject);
private
{ private declarations }
FFileName: string;
FTotalCmdWindow: HWND;
FListerWindow: HWND;
FListerQuickView: Boolean;
FPrevKeyCode: word;
FPrevKeyShift: TShiftState;
FPrevKeyTick: Qword;
FPrevNoCaret: boolean;
FindConfirmAll: TModalResult;
FindMarkAll: boolean;
Statusbar: TATStatus;
Adapter: TATAdapterEControl;
procedure ApplyNoCaret;
procedure ApplyThemes;
procedure DoFindDialog;
procedure DoListerFindDialog;
procedure FinderFound(Sender: TObject; APos1, APos2: TPoint);
//procedure FinderNext;
procedure FinderUpdateEditor(AUpdateText: boolean);
procedure DoFindError;
procedure ShowBadRegex;
procedure FinderConfirmReplace(Sender: TObject; APos1, APos2: TPoint;
AForMany: boolean; var AConfirm, AContinue: boolean; var AReplacement: UnicodeString);
function GetEncodingName: string;
procedure LoadOptions;
procedure MenuEncNoReloadClick(Sender: TObject);
procedure MenuEncWithReloadClick(Sender: TObject);
procedure MenuLexerClick(Sender: TObject);
procedure SaveOptions;
procedure StatusPanelClick(Sender: TObject; AIndex: Integer);
procedure UpdateMenuEnc(AMenu: TMenuItem);
procedure UpdateMenuLexersTo(AMenu: TMenuItem);
procedure UpdateStatusbar;
protected
procedure CreateParams(var Params: TCreateParams); override;
public
{ public declarations }
Finder: TATEditorFinder;
OptRep: boolean;
//
constructor CreateParented(AParentWindow: HWND);
class function PluginShow(AListerWin: HWND; AFileName: string): HWND;
class function PluginHide(APluginWin: HWND): HWND;
procedure MsgStatus(const AMsg: string);
procedure FileOpen(const AFileName: string);
procedure SetWrapMode(AValue: boolean);
procedure SetEncodingName(const Str: string; EncId: TEncConvId);
procedure ToggleWrapMode;
procedure ToggleReadWriteMode;
procedure DoFind(AFindNext, ABack, ACaseSens, AWords: boolean; const AStrFind: Widestring);
procedure ConfirmSave;
end;
var
ListerIniFilename: string = '';
ListerIniSection: string = 'CudaLister';
const
cEncNameUtf8_WithBom = 'UTF-8 with BOM';
cEncNameUtf8_NoBom = 'UTF-8';
cEncNameUtf16LE_WithBom = 'UTF-16 LE with BOM';
cEncNameUtf16LE_NoBom = 'UTF-16 LE';
cEncNameUtf16BE_WithBom = 'UTF-16 BE with BOM';
cEncNameUtf16BE_NoBom = 'UTF-16 BE';
cEncNameAnsi = 'ANSI';
cEncNameOem = 'OEM';
var
AppManager: TecSyntaxManager;
function IsCheckedLexerForFilename(const fn: string): boolean;
function ExpandEnvironmentVariables(var AString: string): string;
implementation
{$R *.lfm}
const
StatusbarIndex_Caret = 0;
StatusbarIndex_Enc = 1;
StatusbarIndex_LineEnds = 2;
StatusbarIndex_Lexer = 3;
StatusbarIndex_Wrap = 4;
StatusbarIndex_ReadWrite = 5;
StatusbarIndex_UndoRedo = 6;
StatusbarIndex_Message = 7;
type
TAppEncodingRecord = record
Sub: string;
Name: string;
Id: TEncConvId;
end;
const
AppEncodings: array[0..41] of TAppEncodingRecord = (
(Sub: ''; Name: cEncNameUtf8_NoBom; Id: eidUTF8),
(Sub: ''; Name: cEncNameUtf8_WithBom; Id: eidUTF8BOM),
(Sub: ''; Name: cEncNameUtf16LE_NoBom; Id: eidUTF8),
(Sub: ''; Name: cEncNameUtf16LE_WithBom; Id: eidUTF8),
(Sub: ''; Name: cEncNameUtf16BE_NoBom; Id: eidUTF8),
(Sub: ''; Name: cEncNameUtf16BE_WithBom; Id: eidUTF8),
(Sub: ''; Name: cEncNameAnsi; Id: eidUTF8),
(Sub: ''; Name: cEncNameOem; Id: eidUTF8),
(Sub: ''; Name: '-'; Id: eidUTF8),
(Sub: 'eu'; Name: 'CP1250'; Id: eidCP1250),
(Sub: 'eu'; Name: 'CP1251'; Id: eidCP1251),
(Sub: 'eu'; Name: 'CP1252'; Id: eidCP1252),
(Sub: 'eu'; Name: 'CP1253'; Id: eidCP1253),
(Sub: 'eu'; Name: 'CP1257'; Id: eidCP1257),
(Sub: 'eu'; Name: '-'; Id: eidUTF8),
(Sub: 'eu'; Name: 'CP437'; Id: eidCP437),
(Sub: 'eu'; Name: 'CP850'; Id: eidCP850),
(Sub: 'eu'; Name: 'CP852'; Id: eidCP852),
(Sub: 'eu'; Name: 'CP866'; Id: eidCP866),
(Sub: 'eu'; Name: '-'; Id: eidUTF8),
(Sub: 'eu'; Name: 'ISO-8859-1'; Id: eidISO1),
(Sub: 'eu'; Name: 'ISO-8859-2'; Id: eidISO2),
(Sub: 'eu'; Name: 'ISO-8859-3'; Id: eidISO3),
(Sub: 'eu'; Name: 'ISO-8859-4'; Id: eidISO4),
(Sub: 'eu'; Name: 'ISO-8859-5'; Id: eidISO5),
(Sub: 'eu'; Name: 'ISO-8859-7'; Id: eidISO7),
(Sub: 'eu'; Name: 'ISO-8859-9'; Id: eidISO9),
(Sub: 'eu'; Name: 'ISO-8859-10'; Id: eidISO10),
(Sub: 'eu'; Name: 'ISO-8859-13'; Id: eidISO13),
(Sub: 'eu'; Name: 'ISO-8859-14'; Id: eidISO14),
(Sub: 'eu'; Name: 'ISO-8859-15'; Id: eidISO15),
(Sub: 'eu'; Name: 'ISO-8859-16'; Id: eidISO16),
(Sub: 'eu'; Name: 'Mac'; Id: eidCPMac),
(Sub: 'mi'; Name: 'CP1254'; Id: eidCP1254),
(Sub: 'mi'; Name: 'CP1255'; Id: eidCP1255),
(Sub: 'mi'; Name: 'CP1256'; Id: eidCP1256),
(Sub: 'as'; Name: 'CP874'; Id: eidCP874),
(Sub: 'as'; Name: 'Shift-JIS'; Id: eidCP932),
(Sub: 'as'; Name: 'GBK'; Id: eidCP936),
(Sub: 'as'; Name: 'UHC'; Id: eidCP949),
(Sub: 'as'; Name: 'Big5'; Id: eidCP950),
(Sub: 'as'; Name: 'CP1258'; Id: eidCP1258)
);
function MsgBox(const S: string; Flags: integer): integer;
begin
Result:= MessageBox(0, PChar(S), 'CudaLister', Flags or MB_APPLMODAL);
end;
procedure LoadLexerLib;
var
dir, fn, lexname: string;
L: TStringlist;
an: TecSyntAnalyzer;
ini: TIniFile;
i, j: integer;
begin
AppManager.Clear;
//load .lcf files to lib
dir:= ExtractFileDir(_GetDllFilename)+'\lexers';
L:= TStringlist.Create;
try
FindAllFiles(L, dir, '*.lcf', false);
L.Sort;
if L.Count=0 then
begin
//MsgStatusAlt('Cannot find lexer files: data/lexlib/*.lcf', 3);
exit
end;
for i:= 0 to L.Count-1 do
begin
an:= AppManager.AddAnalyzer;
an.LoadFromFile(L[i]);
end;
finally
FreeAndNil(L);
end;
//correct sublexer links
for i:= 0 to AppManager.AnalyzerCount-1 do
begin
an:= AppManager.Analyzers[i];
fn:= dir+'\'+an.LexerName+'.cuda-lexmap';
if FileExists(fn) then
begin
ini:= TIniFile.Create(fn);
try
for j:= 0 to an.SubAnalyzers.Count-1 do
begin
lexname:= ini.ReadString('ref', IntToStr(j), '');
if lexname<>'' then
an.SubAnalyzers[j].SyntAnalyzer:= AppManager.FindAnalyzer(lexname);
end;
finally
FreeAndNil(ini);
end;
end;
end;
end;
function IsCheckedLexerForFilename(const fn: string): boolean;
var
op: boolean;
begin
with TIniFile.Create(ListerIniFilename) do
try
op:= ReadBool(ListerIniSection, 'only_known_types', false);
finally
Free
end;
if op then
Result:= Assigned(Lexer_FindForFilename(AppManager, fn))
else
Result:= true;
end;
function ExpandEnvironmentVariables(var AString: string): string;
var
LPos1: Integer;
LPos2: Integer;
LVariableValue, LValue: string;
begin
LPos1 := Pos('%', AString);
if LPos1 > 0 then
begin
LPos2 := PosEx('%', AString, LPos1 + 1);
if LPos2 > 0 then
begin
LVariableValue := GetEnvironmentVariable(Copy(AString, LPos1 + 1, LPos2 - LPos1 - 1));
if LVariableValue <> '' then
begin
LValue := Copy(AString, LPos2 + 1, MaxInt);
ExpandEnvironmentVariables(LValue);
AString := Copy(AString, 1, LPos1 - 1) + LVariableValue + LValue;
end
else
begin
LValue := Copy(AString, LPos2 + 1, MaxInt);
ExpandEnvironmentVariables(LValue);
AString := Copy(AString, 1, LPos2) + LValue;
end;
end;
end;
Result := AString;
end;
function GetLastFindText : string;
var
S: string;
begin
with TIniFile.Create(GetEnvironmentVariable('COMMANDER_INI')) do
try
S:= ReadString('SearchText', '0', '');
if S='' then
S:= ReadString('SearchText', 'RedirectSection', '');
if S='' then
S:= ReadString('Configuration', 'AlternateUserIni', '');
finally
Free
end;
if FileExists(ExpandEnvironmentVariables(S)) then
begin
with TIniFile.Create(ExpandEnvironmentVariables(S)) do
try
S:= ReadString('SearchText', '0', '');
finally
Free
end;
end;
Result := S;
end;
function SetLastFindText(const AString: string) : string;
var
S: string;
begin
with TIniFile.Create(GetEnvironmentVariable('COMMANDER_INI')) do
try
S:= ReadString('SearchText', '0', '');
if S='' then
S:= ReadString('SearchText', 'RedirectSection', '')
else
WriteString('SearchText', '0', AString);
if S='' then
S:= ReadString('Configuration', 'AlternateUserIni', '');
finally
Free
end;
if FileExists(ExpandEnvironmentVariables(S)) then
begin
with TIniFile.Create(ExpandEnvironmentVariables(S)) do
try
WriteString('SearchText', '0', AString);
finally
Free
end;
end;
end;
{ TfmMain }
procedure TfmMain.edKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
//
//note: to add reaction to keys, add code after comment "after checking dups",
//otherwise new keys may be duplicated
//
const
cMaxDupTime = 20;
var
MaybeDups: boolean;
ok, bChanged: boolean;
S: string;
N: integer;
begin
//ignore OS keys: Alt+Space
if (Key=VK_SPACE) and (Shift=[ssAlt]) then exit;
//Lister's F2 must reload file
if (Key=VK_F2) and (Shift=[]) then
begin
if ed.Modified then
if MessageBox(Handle, 'File is modified. Are you sure you want to reload it from disk?', 'CudaLister',
MB_OKCANCEL or MB_ICONWARNING)=ID_OK then
ed.LoadFromFile(FFileName, []);
Key:= 0;
exit;
end;
//support F3, find next
if (Key=VK_F3) and (Shift=[]) then
begin
//DoFind(true, true, Finder.OptCase, Finder.OptWords, '');
Finder.StrFind:= GetLastFindText;
if ed.TextSelected<>'' then begin
Finder.StrFind:= ed.TextSelected;
SetLastFindText(Finder.StrFind);
end;
if Finder.StrFind='' then
begin
DoFindDialog;
exit;
end;
Finder.OptBack:= false;
Finder.OptFromCaret:= true;
ok:= Finder.DoAction_FindOrReplace(false, false, bChanged, true);
FinderUpdateEditor(false);
if not ok then
begin
DoFindError;
DoFindDialog;
end;
Key:= 0;
exit
end;
//Shift+F10: context menu
if (Key=VK_F10) and (Shift=[ssShift]) then
begin
ed.PopupText.PopUp;
Key:= 0;
exit;
end;
//close by Esc
if (Key=VK_ESCAPE) and (Shift=[]) then
if not FListerQuickView then
begin
PostMessage(FListerWindow, WM_CLOSE, 0, 0);
Key:= 0;
exit
end;
//to ignore duplicate keys (some Lazarus bug)
MaybeDups:= (Key in [
VK_LEFT, VK_RIGHT, VK_DOWN, VK_UP,
VK_BACK, VK_DELETE,
VK_INSERT,
VK_RETURN,
VK_PRIOR, VK_NEXT,
VK_HOME, VK_END,
VK_F1..VK_F12,
VK_APPS
]) or
(ssCtrl in Shift) or
(ssAlt in Shift);
//fix for non working W
if ed.ModeReadOnly then
if Key in [VK_A..VK_Z] then
MaybeDups:= true;
//for space it's weird, it must work in NoCaret mode
if (Key in [VK_SPACE]) and OptNoCaret then
MaybeDups:= true;
if MaybeDups then
if (Key=FPrevKeyCode) and
(Shift=FPrevKeyShift) and
(GetTickCount64-FPrevKeyTick<=cMaxDupTime) then
begin Key:= 0; exit; end;
FPrevKeyCode:= Key;
FPrevKeyShift:= Shift;
FPrevKeyTick:= GetTickCount64;
//--------------------------------------
//after checking dups
if (Shift=[]) then
if (Key in [VK_F1..VK_F12]) or
((Key in [Ord('1')..Ord('7'), Ord('A')..Ord('W')]) and ed.ModeReadOnly) then
begin
PostMessage(FListerWindow, WM_KEYDOWN, Key, 0);
Key:= 0;
exit
end;
{$ifdef CPU64}
//we disable hotkeys Ctrl+F Ctrl+G Ctrl+H because in 32-bit version they give inseting chars F G H
//support Ctrl+F
if (Key=VK_F) and (Shift=[ssCtrl]) then
begin
//OptRep:= true;
DoFindDialog;
Key:= 0;
exit
end;
//support Ctrl+G
if (Key=VK_G) and (Shift=[ssCtrl]) then
begin
S:= InputBox('Go to', 'Line number:', '');
if S='' then exit;
N:= StrToIntDef(S, 0);
if (N>0) and (N<=ed.Strings.Count) then
begin
ed.DoGotoPos(
Point(0, N-1),
Point(-1, -1),
10,
3,
true,
true
);
MsgStatus('Go to line '+IntToStr(N));
end
else
MsgStatus('Incorrect line number: '+S);
Key:= 0;
exit
end;
//support Ctrl+H
if (Key=VK_H) and (Shift=[ssCtrl]) then
begin
OptRep:= true;
DoFindDialog;
Key:= 0;
exit
end;
{$endif CPU64}
//support Ctrl+S
if (Key=VK_S) and (Shift=[ssCtrl]) then
begin
if ed.Modified then
try
ed.SaveToFile(FFileName);
ed.Modified:= false;
except
MsgBox('Cannot save file', MB_OK or MB_ICONERROR);
end;
end;
//support Ctrl+U
if (Key=VK_U) and (Shift=[ssCtrl]) then
begin
ed.DoCommand(cCommand_TextCaseUpper, TATCommandInvoke.MenuContext);
Key:= 0;
exit
end;
//support Shift+Ctrl+U
if (Key=VK_U) and (Shift=[ssShift,ssCtrl]) then
begin
ed.DoCommand(cCommand_TextCaseLower, TATCommandInvoke.MenuContext);
Key:= 0;
exit
end;
//support Ctrl+V
if (Key=VK_V) and (Shift=[ssCtrl]) then
begin
ed.DoCommand(cCommand_ClipboardAltPaste, TATCommandInvoke.MenuContext);
Key:= 0;
exit
end;
//support Shift+Ctrl+Z
if (Key=VK_Z) and (Shift=[ssShift,ssCtrl]) then
begin
ed.SetFocus;
ed.DoCommand(cCommand_Redo, TATCommandInvoke.MenuContext);
ed.SetFocus;
Key:= 0;
exit
end;
//support Ctrl+Z
if (Key=VK_Z) and (Shift=[ssCtrl]) then
begin
ed.SetFocus;
ed.DoCommand(cCommand_Undo, TATCommandInvoke.MenuContext);
ed.SetFocus;
Key:= 0;
exit
end;
//support Shift+F3, find back
if (Key=VK_F3) and (Shift=[ssShift]) then
begin
//DoFind(true, true, Finder.OptCase, Finder.OptWords, '');
Finder.OptBack:= true;
Finder.OptFromCaret:= true;
ok:= Finder.DoAction_FindOrReplace(false, false, bChanged, true);
FinderUpdateEditor(false);
if not ok then
begin
DoFindError;
DoFindDialog;
end;
Key:= 0;
exit
end;
if OptNoCaret then
case Key of
VK_SPACE:
begin
if ssShift in Shift then
ed.DoCommand(cCommand_ScrollPageUp, TATCommandInvoke.Hotkey)
else
ed.DoCommand(cCommand_ScrollPageDown, TATCommandInvoke.Hotkey);
Key:= 0;
end;
VK_HOME:
begin
ed.DoCommand(cCommand_ScrollToBegin, TATCommandInvoke.Hotkey);
Key:= 0;
end;
VK_END:
begin
ed.DoCommand(cCommand_GotoLineAbsBegin, TATCommandInvoke.Hotkey); //needed if too long line
ed.DoCommand(cCommand_ScrollToEnd, TATCommandInvoke.Hotkey);
Key:= 0;
end;
VK_UP:
begin
ed.DoCommand(cCommand_ScrollLineUp, TATCommandInvoke.Hotkey);
Key:= 0;
end;
VK_DOWN:
begin
ed.DoCommand(cCommand_ScrollLineDown, TATCommandInvoke.Hotkey);
Key:= 0;
end;
VK_LEFT:
begin
ed.DoCommand(cCommand_ScrollColumnLeft, TATCommandInvoke.Hotkey);
Key:= 0;
end;
VK_RIGHT:
begin
ed.DoCommand(cCommand_ScrollColumnRight, TATCommandInvoke.Hotkey);
Key:= 0;
end;
VK_PRIOR:
begin
ed.DoCommand(cCommand_ScrollPageUp, TATCommandInvoke.Hotkey);
Key:= 0;
end;
VK_NEXT:
begin
ed.DoCommand(cCommand_ScrollPageDown, TATCommandInvoke.Hotkey);
Key:= 0;
end;
end;
end;
procedure TfmMain.DoListerFindDialog;
begin
PostMessage(FListerWindow, WM_KEYDOWN, VK_F7, 0);
PostMessage(FListerWindow, WM_KEYUP, VK_F7, 0);
end;
procedure TfmMain.DoFindDialog;
var
res: TModalResult;
cnt: integer;
ok, bChanged: boolean;
begin
with TfmFind.Create(nil) do
try
AutoAdjustLayout(lapAutoAdjustForDPI,
96,
Screen.PixelsPerInch,
Width,
Scale96ToScreen(Width)
);
if ed.ModeReadOnly then
begin
chkRep.Enabled:= not ed.ModeReadOnly;
chkRep.Checked:= not ed.ModeReadOnly;
OptRep:= false;
end;
if ed.TextSelected<>'' then Finder.StrFind:= ed.TextSelected;
Finder.OptBack:= false;
Finder.OptFromCaret:= false;
edFind.Text:= Utf8Encode(Finder.StrFind);
edRep.Text:= Utf8Encode(Finder.StrReplace);
chkRep.Checked:= OptRep;
chkBack.Checked:= Finder.OptBack;
chkCase.Checked:= Finder.OptCase;
chkWords.Checked:= Finder.OptWords;
chkRegex.Checked:= Finder.OptRegex;
chkFromCaret.Checked:= Finder.OptFromCaret;
chkConfirm.Checked:= Finder.OptConfirmReplace;
chkInSel.Enabled:= ed.Carets.IsSelection;
res:= ShowModal;
if res=mrCancel then Exit;
if edFind.Text='' then Exit;
Finder.StrFind:= Utf8Decode(edFind.Text);
Finder.StrReplace:= Utf8Decode(edRep.Text);
OptRep:= chkRep.Checked;
Finder.OptBack:= chkBack.Checked;
Finder.OptCase:= chkCase.Checked;
Finder.OptWords:= chkWords.Checked;
Finder.OptRegex:= chkRegex.Checked;
Finder.OptFromCaret:= chkFromCaret.Checked;
Finder.OptConfirmReplace:= chkConfirm.Checked;
Finder.OptInSelection:= chkInSel.Checked;
FindConfirmAll:= mrNone;
FindMarkAll:= false;
//btnStop.Show;
//progress.Show;
//progress.Position:= 0;
ed.Markers.Clear; //support "find first" in selection
case res of
mrOk: //find
begin
ok:= Finder.DoAction_FindOrReplace(false, false, bChanged, true);
FinderUpdateEditor(false);
if not ok then
if Finder.IsRegexBad then
ShowBadRegex
else
DoFindError;
end;
mrYes: //replace
begin
ok:= Finder.DoAction_FindOrReplace(true, false, bChanged, true);
FinderUpdateEditor(true);
if not ok then
if Finder.IsRegexBad then
ShowBadRegex
else
DoFindError;
end;
mrYesToAll: //replace all
begin
cnt:= Finder.DoAction_ReplaceAll;
FinderUpdateEditor(true);
MsgStatus('Replaces made: '+Inttostr(cnt));
end;
mrIgnore: //count all
begin
cnt:= Finder.DoAction_CountAll(false);
MsgStatus('Count of "'+Utf8Encode(Finder.StrFind)+'": '+Inttostr(cnt));
end;
mrRetry: //mark all
begin
FindMarkAll:= true;
ed.Markers.Clear;
cnt:= Finder.DoAction_CountAll(true);
FindMarkAll:= false;
FinderUpdateEditor(false);
MsgStatus('Markers placed: '+Inttostr(cnt));
end;
end;
finally
Free;
end;
end;
procedure TfmMain.DoFindError;
begin
MsgStatus('Cannot find: '+Utf8Encode(Finder.StrFind));
end;
procedure TfmMain.FinderConfirmReplace(Sender: TObject; APos1, APos2: TPoint;
AForMany: boolean; var AConfirm, AContinue: boolean;
var AReplacement: UnicodeString);
var
Res: TModalResult;
Buttons: TMsgDlgButtons;
begin
case FindConfirmAll of
mrYesToAll: begin AConfirm:= true; exit end;
mrNoToAll: begin AConfirm:= false; exit end;
end;
with Ed.Carets[0] do
begin
PosX:= APos1.X;
PosY:= APos1.Y;
EndX:= APos2.X;
EndY:= APos2.Y;
end;
Ed.DoCommand(cCommand_ScrollToCaretTop, TATCommandInvoke.AppInternal);
Ed.Update(true);
Buttons:= [mbYes, mbNo];
if AForMany then
Buttons:= Buttons+[mbYesToAll, mbNoToAll];
//Str:= Ed.Strings.TextSubstring(APos1.X, APos1.Y, APos2.X, APos2.Y);
Res:= MessageDlg(
'Confirm replace',
'Replace string at line '+Inttostr(APos1.Y+1),
mtConfirmation,
Buttons, '');
AConfirm:= Res in [mrYes, mrYesToAll];
AContinue:= Res<>mrNoToAll;
if Res in [mrYesToAll, mrNoToAll] then
FindConfirmAll:= Res;
end;
procedure TfmMain.ShowBadRegex;
begin
MessageDlg(
'Incorrect RegEx',
Utf8Encode(Finder.StrFind)+#10+
Finder.RegexErrorMsg,
mtError,
[mbOk], ''
);
end;
procedure TfmMain.FinderUpdateEditor(AUpdateText: boolean);
begin
Finder.Editor.Update(AUpdateText);
Finder.Editor.DoGotoCaret(cEdgeTop);
UpdateStatusbar;
end;
procedure TfmMain.FinderFound(Sender: TObject; APos1, APos2: TPoint);
begin
if FindMarkAll then
begin
ed.Markers.Add(
Point(APos1.X, APos1.Y),
Point(Abs(APos2.X-APos1.X), 0),
TATMarkerTags.Init(0, 0)
);
end
else
begin
ed.DoGotoPos(APos1, APos2, 10, 3, true, true);
end;
end;
//procedure TfmMain.FinderNext;
//var
// ok, bChanged: boolean;
//begin
// if Finder.StrFind='' then Exit;
// Finder.OptFromCaret:= true;
// ok:= Finder.DoAction_FindOrReplace(false, false, bChanged, true);
// FinderUpdateEditor(false);
// if not ok then DoFindError;
//end;
procedure TfmMain.ConfirmSave;
begin
if (FFileName<>'') and ed.Modified then
begin
ed.Modified:= false;
case MsgBox('File was modified. Save it?', MB_YESNOCANCEL or MB_ICONQUESTION) of
ID_YES:
try
ed.SaveToFile(FFileName);
except
MsgBox('Cannot save file', MB_OK or MB_ICONERROR);
end;
ID_CANCEL:
exit;
end;
end;
end;
procedure TfmMain.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
ConfirmSave;
end;
procedure TfmMain.edChangeCaretPos(Sender: TObject);
begin
UpdateStatusbar;
end;
procedure TfmMain.edClickLink(Sender: TObject; const ALink: string);
begin
OpenURL(ALink);
end;
procedure TfmMain.edCommand(Sender: TObject; ACommand: integer;
const AText: string; var AHandled: boolean);
begin
//fix for clipboard pasting from other apps: call Clipboard.Clear as Ghisler suggested
//- dont work
//case ACommand of
// cCommand_ClipboardAltPaste,
// cCommand_ClipboardAltPaste_Column:
//end;
end;
procedure TfmMain.FormCreate(Sender: TObject);
var
N: integer;
begin
//N:= ed.Keymap.IndexOf(cCommand_ToggleReadOnly);
//if N>=0 then
// ed.Keymap[N].Keys1.Data[0]:= Shortcut(VK_R, [ssCtrl]);
ed.OptScrollbarsNew:= true;
ed.OptScrollStyleHorz:= TATEditorScrollbarStyle.Show;
ed.OptMinimapShowSelAlways:= true; //like in Sublime 4
ed.PopupText:= PopupText;
ATFlatTheme.ScalePercents:= Screen.PixelsPerInch * 100 div 96;
ATFlatTheme.ScaleFontPercents:= 100;
ATScrollbarTheme.ScalePercents:= ATFlatTheme.ScalePercents;
ATScrollbarTheme.ColorThumbFill:= clBtnFace;
ATScrollbarTheme.ColorArrowFill:= clBtnFace;
ATEditorOptions.UnprintedEndSymbol:= TATEditorUnptintedEolSymbol.Pilcrow; //fix issue #66
Statusbar:= TATStatus.Create(Self);
Statusbar.Parent:= Self;
Statusbar.Align:= alBottom;
Statusbar.Padding:= 1;
Statusbar.OnPanelClick:= @StatusPanelClick;
Statusbar.ShowHint:= false;
Statusbar.AddPanel(-1, 150, taCenter);
Statusbar.AddPanel(-1, 110, taCenter);
Statusbar.AddPanel(-1, 50, taCenter);
Statusbar.AddPanel(-1, 150, taCenter);
Statusbar.AddPanel(-1, 80, taCenter);
Statusbar.AddPanel(-1, 80, taCenter);
Statusbar.AddPanel(-1, 150, taCenter);
Statusbar.AddPanel(-1, 1600, taLeftJustify);
UpdateMenuLexersTo(PopupLexers.Items);
Adapter:= TATAdapterEControl.Create(Self);
Adapter.ImplementsDataReady:= false; //fix not initially colored c++ file
Adapter.DynamicHiliteEnabled:= false;
Adapter.DynamicHiliteMaxLines:= 5000;
Adapter.AddEditor(ed);
Finder:= TATEditorFinder.Create;
Finder.Editor:= ed;
//Finder.OptRegex:= true;
Finder.OnFound:= @FinderFound;
Finder.OnConfirmReplace:= @FinderConfirmReplace;
LoadOptions;
UpdateMenuEnc(PopupEnc.Items);
{$ifndef CPU64}
mnuTextGoto.ShortCut:= 0;
mnuFind.ShortCut:= 0;
{$endif}
end;
procedure TfmMain.FormDestroy(Sender: TObject);