-
Notifications
You must be signed in to change notification settings - Fork 2
/
EnvMan.dpr
1163 lines (1052 loc) · 30.6 KB
/
EnvMan.dpr
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
{$MINENUMSIZE 4}
{$RESOURCE EnvMan.res}
library EnvMan;
{
TODO:
* allow escaping % in strings
* Russian help file
* better error checking?
}
uses
Windows, Types, {$IFDEF UNICODE}PluginW{$ELSE}Plugin{$ENDIF}, PluginEx;
// ****************************************************************************
function CreateSettings: TSettings;
begin
Result := TRegistrySettings.Create('EnvMan');
end;
function GetPluginString(Name: FarString; Default: FarString = ''): FarString;
var
Settings: TSettings;
begin
Settings := CreateSettings;
Result := Settings.GetString(Name, Default);
Settings.Free;
end;
function GetPluginInt(Name: FarString): Integer;
var
Settings: TSettings;
begin
Settings := CreateSettings;
Result := Settings.GetInt(Name);
Settings.Free;
end;
function GetIgnoredVariables: FarString;
begin
Result := GetPluginString('IgnoredVariables', 'FARENV_EXPORT_HWND');
end;
procedure SetPluginString(Name: PFarChar; Value: FarString);
var
Settings: TSettings;
begin
Settings := CreateSettings;
Settings.SetString(Name, Value);
Settings.Free;
end;
function GetName(S: FarString): FarString;
begin
Result := Copy(S, 1, Pos('=', S)-1);
end;
function GetValue(S: FarString): FarString;
begin
Result := Copy(S, Pos('=', S)+1, MaxInt);
end;
function ReadFullEnvironment: TFarStringDynArray;
var
Env: PFarChar;
begin
Env := GetEnvironmentStringsF;
Result := NullStringsToArray(Env);
FreeEnvironmentStringsF(Env);
end;
procedure ApplyNameValuePair(S: FarString);
var
P: Integer;
begin
if S='' then
Exit;
P := Pos('=', S);
if P=0 then Exit;
S[P] := #0;
if P=Length(S) then
SetEnvironmentVariableF(@S[1], nil)
else
SetEnvironmentVariableF(@S[1], @S[P+1]);
end;
// Remove Windows' special environment strings (which start with =)
// Also remove "ignored" variables
function FilterEnv(A: TFarStringDynArray): TFarStringDynArray;
var
I, J: Integer;
IgnoredVariables: TFarStringDynArray;
Ignored: Boolean;
Name: FarString;
begin
Result := nil;
IgnoredVariables := SplitByAny(GetIgnoredVariables, ',;');
for I:=0 to High(A) do
if (Length(A[I])>0) and (A[I][1] <> '=') then
begin
Name := GetName(A[I]);
Ignored := False;
for J:=0 to High(IgnoredVariables) do
if Name=IgnoredVariables[J] then
begin
Ignored := True;
Break
end;
if not Ignored then
AppendToStrings(Result, A[I]);
end;
end;
function ReadEnvironment: TFarStringDynArray;
var
Env: PFarChar;
begin
Env := GetEnvironmentStringsF;
Result := FilterEnv(NullStringsToArray(Env));
FreeEnvironmentStringsF(Env);
end;
function ReadRawEnvironment: FarString;
var
Env: PFarChar;
I: Integer;
begin
Env := GetEnvironmentStringsF;
I := 0;
while (Env[I]<>#0) or (Env[I+1]<>#0) do
Inc(I);
Inc(I);
SetLength(Result, I);
Move(Env^, Result[1], I*SizeOf(FarChar));
FreeEnvironmentStringsF(Env);
end;
procedure ClearEnvironment;
var
Env: TFarStringDynArray;
I: Integer;
begin
Env := ReadEnvironment;
for I:=0 to High(Env) do
SetEnvironmentVariableF(PFarChar(OemToCharStr(GetName(Env[I]))), nil);
end;
procedure SetEnvironment(Env: TFarStringDynArray);
var
I: Integer;
begin
ClearEnvironment;
for I:=0 to High(Env) do
ApplyNameValuePair(OemToCharStr(Env[I]));
end;
function ExpandEnv(S: FarString): FarString;
begin
SetLength(Result, ExpandEnvironmentStringsF(PFarChar(S), nil, 0));
ExpandEnvironmentStringsF(PFarChar(S), @Result[1], Length(Result));
SetLength(Result, Length(Result)-1); // terminating null
end;
// ****************************************************************************
function GetClipboardText: String;
var
Data: HGLOBAL;
begin
if not OpenClipboard(0) then
Exit;
try
Data := GetClipboardData(CF_TEXT);
if Data = 0 then
Exit;
Result := PChar(GlobalLock(Data));
GlobalUnlock(Data);
finally
CloseClipboard;
end;
end;
procedure SetClipboard(Format: Cardinal; const Buffer; Size: Cardinal);
var
Data: HGLOBAL;
DataPtr: Pointer;
begin
if not OpenClipboard(0) then
Exit;
try
Data := GlobalAlloc(GMEM_MOVEABLE+GMEM_DDESHARE, Size);
DataPtr := GlobalLock(Data);
Move(Buffer, DataPtr^, Size);
EmptyClipboard;
SetClipboardData(Format, Data);
GlobalUnlock(Data);
finally
CloseClipboard;
end;
end;
procedure SetClipboardText(const Value: String);
begin
SetClipboard(CF_TEXT, PChar(Value)^, Length(Value) + 1);
end;
// ****************************************************************************
type
TEntry = record
Name: FarString;
Vars: TFarStringDynArray;
Enabled: Boolean;
end;
TEntryDynArray = array of TEntry;
function ReadEntries: TEntryDynArray;
var
Settings, EntryKey: TSettings;
KeyName: FarString;
I: Integer;
begin
Result := nil;
Settings := CreateSettings;
try
I := 0;
repeat
KeyName := IntToStr(I);
if not Settings.KeyExists(KeyName) then
Exit;
EntryKey := Settings.OpenKey(KeyName);
try
SetLength(Result, Length(Result)+1);
Result[High(Result)].Name := EntryKey.GetString('');
Result[High(Result)].Vars := EntryKey.GetStrings('Vars');
Result[High(Result)].Enabled := Boolean(EntryKey.GetInt('Enabled'));
finally
EntryKey.Free;
end;
Inc(I);
until false;
finally
Settings.Free;
end;
end;
var
InitialEnvironment, LastUpdate: TFarStringDynArray;
procedure Update;
var
Entries: TEntryDynArray;
I, J: Integer;
begin
// Reset the environment to the initial state
SetEnvironment(InitialEnvironment);
// Apply entries
Entries := ReadEntries;
for I:=0 to High(Entries) do
if Entries[I].Enabled then
for J:=0 to High(Entries[I].Vars) do
ApplyNameValuePair(ExpandEnv(OemToCharStr(Entries[I].Vars[J])));
LastUpdate := ReadEnvironment;
end;
function StringsEqual(A, B: TFarStringDynArray): Boolean;
var
I: Integer;
begin
Result := False;
if Length(A) <> Length(B) then
Exit;
for I:=0 to High(A) do
if A[I] <> B[I] then
Exit;
Result := True;
end;
function EntriesEqual(A, B: TEntryDynArray): Boolean;
var
I: Integer;
begin
Result := False;
if Length(A) <> Length(B) then
Exit;
for I:=0 to High(A) do
if (A[I].Name <> B[I].Name) or not StringsEqual(A[I].Vars, B[I].Vars) or (A[I].Enabled <> B[I].Enabled) then
Exit;
Result := True;
end;
procedure AppendToCommaList(var S: FarString; S2: FarString);
begin
if S<>'' then
S := S + ', ';
S := S + S2;
end;
function DescribeDiff(NewVars, ChangedVars, DeletedVars: TFarStringDynArray): TFarStringDynArray;
begin
Result := nil;
if Length(NewVars)<>0 then
AppendToStrings(Result, GetMsg(MNewVars) + Join(NewVars, ', '));
if Length(ChangedVars)<>0 then
AppendToStrings(Result, GetMsg(MChangedVars) + Join(ChangedVars, ', '));
if Length(DeletedVars)<>0 then
AppendToStrings(Result, GetMsg(MDeletedVars) + Join(DeletedVars, ', '));
end;
function MakeDiffEntry(Env1, Env2: TFarStringDynArray; var NewVars, ChangedVars, DeletedVars: TFarStringDynArray): TEntry;
var
I, J, P: Integer;
Found: Boolean;
Name, NewValue, OldValue: FarString;
procedure AppendSetting(Name, Value: FarString; var List: TFarStringDynArray);
begin
AppendToStrings(Result.Vars, Name+'='+Value);
AppendToStrings(List, Name);
end;
begin
NewVars := nil;
ChangedVars := nil;
DeletedVars := nil;
// Find new and modified variables
for J:=0 to High(Env2) do
begin
Found := False;
Name := GetName(Env2[J]);
NewValue := GetValue(Env2[J]);
for I:=0 to High(Env1) do
if GetName(Env1[I])=Name then
begin
Found := True;
OldValue := GetValue(Env1[I]);
if NewValue<>OldValue then
begin
if Length(OldValue)>0 then
begin
P := Pos(OldValue, NewValue);
if P>0 then
NewValue := Copy(NewValue, 1, P-1)+'%'+Name+'%'+Copy(NewValue, P+Length(OldValue), MaxInt);
end;
AppendSetting(Name, NewValue, ChangedVars);
end;
Break;
end;
if not Found then
AppendSetting(Name, NewValue, NewVars);
end;
// Find deleted variables
for I:=0 to High(Env1) do
begin
Name := GetName(Env1[I]);
Found := False;
for J:=0 to High(Env2) do
if GetName(Env2[J])=Name then
begin
Found := True;
Break
end;
if not Found then
AppendSetting(Name, '', DeletedVars);
end;
end;
// ****************************************************************************
function DoConfigure(IgnoredVariables: FarString): Boolean; overload;
const
W = 75;
H = 9;
ItemNr = 5;
GUID: TGUID = '{f213d17a-291e-4cb4-8765-d6e383067d25}';
var
Dialog: TFarDialog;
MsgIgnoredVariables: PFarChar;
N, OK, IIgnoredVariables, IAlwaysUseEditor: Integer;
Settings: TSettings;
begin
while Copy(IgnoredVariables, 1, 1)=',' do
Delete(IgnoredVariables, 1, 1);
Settings := CreateSettings;
Dialog := TFarDialog.Create;
try
Dialog.Add(DI_DOUBLEBOX, DIF_NONE, 3, 1, W-1-3, H-1-1, GetMsg(MConfiguration));
MsgIgnoredVariables := GetMsg(MIgnoredVariables);
Dialog.Add(DI_TEXT, DIF_NONE, 5, 2, 5+Length(MsgIgnoredVariables), 2, MsgIgnoredVariables);
IIgnoredVariables := Dialog.Add(DI_EDIT, DIF_NONE, 5+Length(MsgIgnoredVariables), 2, W-1-5, 2, IgnoredVariables);
Dialog.Items[IIgnoredVariables].{$IFDEF FAR3}Flags := Dialog.Items[IIgnoredVariables].Flags or DIF_FOCUS{$ELSE}Focus := 1{$ENDIF};
IAlwaysUseEditor := Dialog.Add(DI_CHECKBOX, DIF_NONE, 5, 4, 0, 0, GetMsg(MAlwaysUseEditor));
Dialog.Items[IAlwaysUseEditor].Param.Selected := Settings.GetInt('AlwaysUseEditor');
OK := Dialog.Add(DI_BUTTON, DIF_CENTERGROUP, 0, H-1-2, 0, 0, GetMsg(MOK));
Dialog.Items[OK].{$IFDEF FAR3}Flags := Dialog.Items[OK].Flags or DIF_DEFAULTBUTTON{$ELSE}DefaultButton := 1{$ENDIF};
Dialog.Add(DI_BUTTON, DIF_CENTERGROUP, 0, H-1-2, 0, 0, GetMsg(MCancel));
Result := False;
N := Dialog.Run(GUID, W, H, 'Configuration');
if N <> OK then
Exit;
Settings.SetString('IgnoredVariables', Dialog.GetData(IIgnoredVariables));
Settings.SetInt('AlwaysUseEditor', Integer(Dialog.GetChecked(IAlwaysUseEditor)));
Result := True;
finally
Dialog.Free;
Settings.Free;
end;
end;
function DoConfigure: Boolean; overload;
begin
Result := DoConfigure(GetIgnoredVariables);
end;
function OpenEntryKey(Index: Integer): TSettings;
var
Settings: TSettings;
begin
Settings := CreateSettings;
Result := Settings.OpenKey(IntToStr(Index));
Settings.Free;
end;
procedure SaveEntry(Index: Integer; const Entry: TEntry);
var
Key: TSettings;
begin
Key := OpenEntryKey(Index);
Key.SetString('', Entry.Name);
Key.SetStrings('Vars', Entry.Vars);
Key.SetInt('Enabled', Ord(Entry.Enabled));
Key.Free;
end;
function EditEntryAlt(var Entry: TEntry): Boolean; forward;
function EditEntry(var Entry: TEntry; Caption: TMessage): Boolean;
const
W = 75;
Rows = 15;
H = Rows + 8;
ItemNr = 6; // not counting rows
TotalItemNr = ItemNr+Rows;
TooManyLinesGUID: TGUID = '{726fd929-25fc-407f-b33a-73462d5a1453}';
GUID: TGUID = '{b15cb128-d71e-4277-bbac-eb6ffb4af77d}';
var
I, N: Integer;
Dialog: TFarDialog;
begin
if GetPluginInt('AlwaysUseEditor')<>0 then
begin
Result := EditEntryAlt(Entry);
Exit;
end;
if Length(Entry.Vars) > Rows then
begin
if Message(TooManyLinesGUID, FMSG_WARNING or FMSG_MB_YESNO, [GetMsg(MWarning), GetMsg(MTooManyLines1), GetMsg(MTooManyLines2)])=0 then
Result := EditEntryAlt(Entry)
else
Result := False;
Exit;
end;
Dialog := TFarDialog.Create;
try
Dialog.Add(DI_DOUBLEBOX, DIF_NONE, 3, 1, W-1-3, H-1-1, GetMsg(Caption));
Dialog.Add(DI_TEXT, DIF_NONE, 5, 2, 10, 2, GetMsg(MName));
N := Dialog.Add(DI_EDIT, DIF_HISTORY, 11, 2, W-1-5-13, 2, Entry.Name);
Dialog.Items[N].{$IFNDEF FAR3}Param.{$ENDIF}History := 'EnvVarsName';
if Entry.Name='' then
Dialog.Items[N].{$IFDEF FAR3}Flags := Dialog.Items[N].Flags or DIF_FOCUS{$ELSE}Focus := 1{$ENDIF};
N := Dialog.Add(DI_CHECKBOX, DIF_NONE, W-1-5-10, 2, 0, 2, GetMsg(MEnabled));
Dialog.Items[N].Param.Selected := Integer(Entry.Enabled);
SetLength(Entry.Vars, Rows);
for I:=0 to Rows-1 do
begin
N := Dialog.Add(DI_EDIT, DIF_EDITOR, 5, 4+I, W-1-5, 4+I, Entry.Vars[I], $10000);
if (I=0) and (Entry.Name<>'') then
Dialog.Items[N].{$IFDEF FAR3}Flags := Dialog.Items[N].Flags or DIF_FOCUS{$ELSE}Focus := 1{$ENDIF};
end;
N := Dialog.Add(DI_BUTTON, DIF_CENTERGROUP, 0, H-1-2, 0, 0, GetMsg(MOK));
Dialog.Items[N].{$IFDEF FAR3}Flags := Dialog.Items[N].Flags or DIF_DEFAULTBUTTON{$ELSE}DefaultButton := 1{$ENDIF};
Dialog.Add(DI_BUTTON, DIF_CENTERGROUP, 0, H-1-2, 0, 0, GetMsg(MCancel));
Result := False;
N := Dialog.Run(GUID, W, H, 'Editor');
if N<>4+Rows then
Exit;
Entry.Name := Dialog.GetData(2);
Entry.Enabled := Dialog.GetChecked(3);
for I:=0 to Rows-1 do
Entry.Vars[I] := Dialog.GetData(4+I);
Result := True;
finally
Dialog.Free;
end;
end;
function EntryToLines(const Entry: TEntry): TFarStringDynArray;
begin
Result := ConcatStrings([
MakeStrings([
'=Name=' + Entry.Name,
'=Enabled=' + IntToStr(Integer(Entry.Enabled)),
''
]),
Entry.Vars,
MakeStrings([''])
]);
end;
function EntryToText(const Entry: TEntry): FarString;
begin
Result := Join(EntryToLines(Entry), #13#10);
end;
function LinesToEntry(Lines: TFarStringDynArray): TEntry;
var
I: Integer;
begin
Result.Name := '';
Result.Enabled := False;
for I := 0 to High(Lines) do
if Copy(Lines[I], 1, 6) = '=Name=' then
Result.Name := Copy(Lines[I], 7, MaxInt)
else
if Copy(Lines[I], 1, 9) = '=Enabled=' then
Result.Enabled := Copy(Lines[I], 10, MaxInt) = '1'
else
if Pos('=', Lines[I]) > 1 then
AppendToStrings(Result.Vars, Lines[I]);
end;
function TextToEntry(Data: FarString): TEntry;
begin
Result := LinesToEntry(SplitLines(Data));
end;
function EditEntryAlt(var Entry: TEntry): Boolean;
var
Data: FarString;
begin
Data := EntryToText(Entry);
Result := EditString(Data, Entry.Name + ' - EnvMan');
if Result then
Entry := TextToEntry(Data);
end;
function EnvToText(Env: TFarStringDynArray): FarString;
begin
Result := Join(Env, #13#10) + #13#10;
end;
function EditEnvironment: Boolean;
var
Data: FarString;
begin
Data := EnvToText(ReadEnvironment);
Result := EditString(Data, 'Environment');
if Result then
SetEnvironment(SplitLines(Data));
end;
procedure ShowEntryMenu(Quiet: Boolean);
var
Entries: TEntryDynArray;
Current: Integer;
procedure InsertEntry(Index: Integer; const Entry: TEntry);
var
I: Integer;
begin
for I:=High(Entries) downto Index do
SaveEntry(I+1, Entries[I]);
SaveEntry(Index, Entry);
end;
procedure DeleteEntry(Index: Integer);
var
Key: TSettings;
I: Integer;
begin
for I:=Index+1 to High(Entries) do
SaveEntry(I-1, Entries[I]);
Key := CreateSettings;
Key.DeleteKey(IntToStr(High(Entries)));
Key.Free;
end;
procedure MoveCurrent(Direction: Integer);
begin
repeat
Inc(Current, Direction);
until (Current<0) or (Current>High(Entries)) or (Entries[Current].Name<>'-');
end;
var
Entry: TEntry;
BreakCode: Integer;
I: Integer;
Items: array of TFarMenuItem;
Key: TSettings;
Env, NewEnv: TFarStringDynArray;
InitialEntries: TEntryDynArray;
NewVars, ChangedVars, DeletedVars, AllVars: TFarStringDynArray;
const
{$IFDEF FAR3}
BreakKeys: array[0..16] of TFarKey = (
(VirtualKeyCode: VK_ADD ; ControlKeyState: 0),
(VirtualKeyCode: VK_SUBTRACT; ControlKeyState: 0),
(VirtualKeyCode: VK_SPACE ; ControlKeyState: 0),
(VirtualKeyCode: VK_INSERT ; ControlKeyState: 0),
(VirtualKeyCode: VK_DELETE ; ControlKeyState: 0),
(VirtualKeyCode: VK_F4 ; ControlKeyState: 0),
(VirtualKeyCode: VK_F5 ; ControlKeyState: 0),
(VirtualKeyCode: VK_UP ; ControlKeyState: LEFT_CTRL_PRESSED),
(VirtualKeyCode: VK_DOWN ; ControlKeyState: LEFT_CTRL_PRESSED),
(VirtualKeyCode: VK_F3 ; ControlKeyState: SHIFT_PRESSED),
(VirtualKeyCode: VK_F4 ; ControlKeyState: LEFT_ALT_PRESSED),
(VirtualKeyCode: VK_F4 ; ControlKeyState: SHIFT_PRESSED),
(VirtualKeyCode: VK_DELETE ; ControlKeyState: SHIFT_PRESSED),
(VirtualKeyCode: VK_INSERT ; ControlKeyState: RIGHT_CTRL_PRESSED),
(VirtualKeyCode: VK_INSERT ; ControlKeyState: SHIFT_PRESSED),
(VirtualKeyCode: VK_F2 ; ControlKeyState: 0),
(VirtualKeyCode: 0 ; ControlKeyState: 0)
);
{$ELSE}
VK_CTRLUP = VK_UP or (PKF_CONTROL shl 16);
VK_CTRLDOWN = VK_DOWN or (PKF_CONTROL shl 16);
VK_SHIFTF3 = VK_F3 or (PKF_SHIFT shl 16);
VK_ALTF4 = VK_F4 or (PKF_ALT shl 16);
VK_SHIFTF4 = VK_F4 or (PKF_SHIFT shl 16);
VK_SHIFTDEL = VK_DELETE or (PKF_SHIFT shl 16);
VK_CTRLINS = VK_INSERT or (PKF_CONTROL shl 16);
VK_SHIFTINS = VK_INSERT or (PKF_SHIFT shl 16);
BreakKeys: array[0..16] of Integer = (
VK_ADD,
VK_SUBTRACT,
VK_SPACE,
VK_INSERT,
VK_DELETE,
VK_F4,
VK_F5,
VK_CTRLUP,
VK_CTRLDOWN,
VK_SHIFTF3,
VK_ALTF4,
VK_SHIFTF4,
VK_SHIFTDEL,
VK_CTRLINS,
VK_SHIFTINS,
VK_F2,
0
);
{$ENDIF}
EditedGUID: TGUID = '{0d0b19a9-93d6-2e4b-b417-05b258e6dee8}';
ConfirmDeleteGUID: TGUID = '{a42cda5f-1d11-86a5-4cf3-b7d0b53dff12}';
ViewEnvironmentGUID: TGUID = '{0a536038-d35c-c9e2-58eb-c6748bcad6ab}';
NoChangeGUID: TGUID = '{3eaf2001-118c-4cdc-ac4e-a75676c2456e}';
{$IFDEF FAR3}
MenuGUID: TGUID = '{b84ab5a0-155e-909e-e4c3-15b3a0cbd19c}';
{$ENDIF}
begin
Env := ReadEnvironment;
if not Quiet and not StringsEqual(LastUpdate, Env) then
begin
Entry := MakeDiffEntry(LastUpdate, Env, NewVars, ChangedVars, DeletedVars);
AllVars := ConcatStrings([NewVars, ChangedVars, DeletedVars]);
Entry.Name := 'Imported: ' + Join(AllVars, ', ');
Entry.Enabled := True;
I := Message(EditedGUID, FMSG_WARNING, ConcatStrings([
MakeStrings([GetMsg(MWarning), GetMsg(MEnvEdited1), GetMsg(MEnvEdited2), GetMsg(MEnvEdited3)]),
DescribeDiff(NewVars, ChangedVars, DeletedVars),
MakeStrings([GetMsg(MContinue), GetMsg(MCancel), GetMsg(MImport), GetMsg(MIgnore)])
]), 4);
if (I=1) or (I=-1) then
Exit;
if I=2 then // import
begin
if EditEntry(Entry, MImportCaption) then
begin
Entries := ReadEntries;
SaveEntry(Length(Entries), Entry);
end
else
Exit; // User cancelled
end
else
if I=3 then // ignore
begin
if not DoConfigure(GetIgnoredVariables+','+Join(AllVars, ',')) then
Exit;
Env := ReadEnvironment;
LastUpdate := Env;
end;
end;
InitialEntries := ReadEntries;
Current := 0;
repeat
Entries := ReadEntries;
// clear heading/trailing separators
while (Length(Entries)>0) and (Entries[0].Name='-') do
begin
DeleteEntry(0);
Entries := ReadEntries;
Dec(Current);
end;
while (Length(Entries)>0) and (Entries[High(Entries)].Name='-') do
begin
DeleteEntry(High(Entries));
Entries := ReadEntries;
end;
SetLength(Items, 0); // Clear
SetLength(Items, Length(Entries));
while (Current >= 0) and (Current < Length(Entries)) and (Entries[Current].Name='-') do
Inc(Current);
if Current >= Length(Entries) then
Current := Length(Entries)-1;
if Current < 0 then
Current := 0;
for I:=0 to High(Entries) do
begin
{$IFNDEF UNICODE}
CopyStrToBuf(Entries[I].Name, Items[I].Text, SizeOf(Items[I].Text));
{$ELSE}
Items[I].TextPtr := PFarChar(Entries[I].Name);
{$ENDIF}
if I=Current then
Items[I].{$IFDEF FAR3}Flags := Items[I].Flags or MIF_SELECTED {$ELSE}Selected := 1{$ENDIF};
if Entries[I].Enabled then
Items[I].{$IFDEF FAR3}Flags := Items[I].Flags or MIF_CHECKED {$ELSE}Checked := 1{$ENDIF};
if Entries[I].Name='-' then
begin
Items[I].{$IFDEF FAR3}Flags := Items[I].Flags or MIF_SEPARATOR{$ELSE}Separator := 1{$ENDIF};
{$IFNDEF UNICODE}
CopyStrToBuf('', Items[I].Text, SizeOf(Items[I].Text));
{$ELSE}
Items[I].TextPtr := nil;
{$ENDIF}
end;
end;
Current := FARAPI.Menu({$IFDEF FAR3}PluginGUID, MenuGUID{$ELSE}FARAPI.ModuleNumber{$ENDIF}, -1, -1, 0, FMENU_AUTOHIGHLIGHT or FMENU_WRAPMODE, 'Environment Manager', 'Space,Ins,Del,F4,... [F1]', 'MainMenu', @BreakKeys, @BreakCode, @Items[0], Length(Items));
if (Current=-1) and (BreakCode=-1) then
Break;
case BreakCode of
0: // VK_ADD
if Current >= 0 then
begin
Key := OpenEntryKey(Current);
Key.SetInt('Enabled', 1);
MoveCurrent(1);
Key.Free;
end;
1: // VK_SUBTRACT
if Current >= 0 then
begin
Key := OpenEntryKey(Current);
Key.SetInt('Enabled', 0);
MoveCurrent(1);
Key.Free;
end;
2: // VK_SPACE
if Current >= 0 then
begin
Key := OpenEntryKey(Current);
Key.SetInt('Enabled', 1-Key.GetInt('Enabled'));
MoveCurrent(1);
Key.Free;
end;
3: // VK_INSERT
begin
if Current < 0 then
Current := 0;
Entry.Name := '';
Entry.Vars := nil;
Entry.Enabled := True;
if EditEntry(Entry, MNewCaption) then
InsertEntry(Current, Entry);
end;
4: // VK_DELETE
if Current >= 0 then
begin
if Message(ConfirmDeleteGUID, FMSG_WARNING or FMSG_MB_OKCANCEL, [GetMsg(MConfirmDeleteTitle), GetMsg(MConfirmDeleteText), Entries[Current].Name])=0 then
DeleteEntry(Current);
end;
5: // VK_F4
if Current >= 0 then
begin
Entry := Entries[Current];
if EditEntry(Entry, MEditCaption) then
SaveEntry(Current, Entry);
end;
6: // VK_F5
if Current >= 0 then
begin
Entry := Entries[Current];
if EditEntry(Entry, MCopyCaption) then
InsertEntry(Current+1, Entry);
end;
7: // VK_CTRLUP
if Current >= 0 then
if Current = 0 then // create separator
begin
Entry.Name := '-';
Entry.Vars := nil;
Entry.Enabled := False;
InsertEntry(1, Entry);
end
else
begin
SaveEntry(Current, Entries[Current-1]);
SaveEntry(Current-1, Entries[Current]);
Dec(Current);
end;
8: // VK_CTRLDOWN
if Current >= 0 then
if Current = High(Entries) then // create separator
begin
Entry.Name := '-';
Entry.Vars := nil;
Entry.Enabled := False;
InsertEntry(Current, Entry);
Inc(Current);
end
else
begin
SaveEntry(Current, Entries[Current+1]);
SaveEntry(Current+1, Entries[Current]);
Inc(Current);
end;
9: // VK_SHIFTF3
Message(ViewEnvironmentGUID, FMSG_MB_OK, ConcatStrings([MakeStrings(['Environment']), Env]));
10: // VK_ALTF4
if Current >= 0 then
begin
Entry := Entries[Current];
if EditEntryAlt(Entry) then
SaveEntry(Current, Entry);
end;
11: // VK_SHIFTF4
begin
if not EntriesEqual(InitialEntries, Entries) then
Update;
if EditEnvironment then
Exit;
end;
12: // VK_SHIFTDEL
if Current >= 0 then
begin
SetClipboardText(EntryToText(Entries[Current]));
DeleteEntry(Current);
end;
13: // VK_CTRLINS
if Current >= 0 then
SetClipboardText(EntryToText(Entries[Current]));
14: // VK_SHIFTINS
begin
if Current < 0 then
Current := 0;
InsertEntry(Current, TextToEntry(GetClipboardText));
end;
15: // VK_F2
begin
DoConfigure;
end;
else // VK_RETURN / hotkey
if Current >= 0 then
begin
Key := OpenEntryKey(Current);
Key.SetInt('Enabled', 1-Key.GetInt('Enabled'));
Key.Free;
end;
//Break;
end;
until false;
Update;
NewEnv := ReadEnvironment;
if not Quiet and EntriesEqual(InitialEntries, Entries) then // no changes
if not StringsEqual(Env, NewEnv) then // but env has changed
begin
if Message(NoChangeGUID, FMSG_WARNING, [
GetMsg(MWarning),
GetMsg(MNoChange1),
GetMsg(MNoChange2),
GetMsg(MNoChange3),
GetMsg(MOverwrite),
GetMsg(MKeep)], 2)<>0 then // Keep was selected?
begin
// Restore last environment
SetEnvironment(Env);
LastUpdate := Env;
end;
end;
end;
procedure LoadEnv(FileName: FarString);
var
S: FarString;
const
ErrorGUID: TGUID = '{c5acb8a8-65ee-5850-bc3d-38aee52fc7ca}';
begin
if not TryLoadText(FileName, S) then
begin
Message(ErrorGUID, FMSG_WARNING or FMSG_MB_OK, [GetMsg(MError), GetMsg(MFileLoadError), FileName]);
Exit
end;
SetEnvironment(SplitLines(S));
end;
procedure SaveEnv(FileName: FarString);
const
ErrorGUID: TGUID = '{a46bea4d-3998-9547-6ed0-c3300cb1620c}';
begin
if not TrySaveText(FileName, EnvToText(ReadEnvironment)) then
Message(ErrorGUID, FMSG_WARNING or FMSG_MB_OK, [GetMsg(MError), GetMsg(MFileCreateError), FileName]);
end;
procedure SaveRawEnv(FileName: FarString);
const
ErrorGUID: TGUID = '{39f779fb-8dcd-5734-70e8-1166bf6c6f1d}';
begin
if not TrySaveString(FileName, ReadRawEnvironment) then
Message(ErrorGUID, FMSG_WARNING or FMSG_MB_OK, [GetMsg(MError), GetMsg(MFileCreateError), FileName]);
end;
procedure ProcessCommandLine(PCmdLine: PFarChar);
var
CmdLine, Name: FarString;
Entries: TEntryDynArray;
I: Integer;
const
BadCommandCharGUID: TGUID = '{71148345-8141-1f0e-4453-3a073e7b5178}';
NoSuchEntryGUID: TGUID = '{7ad27a4e-b753-fd0f-cfb9-7b3409567649}';
begin
CmdLine := PCmdLine;
if Length(CmdLine)=0 then
ShowEntryMenu(True)
else
if CmdLine[1]='<' then
LoadEnv(Copy(CmdLine, 2, MaxInt))
else
if CmdLine[1]='>' then
SaveEnv(Copy(CmdLine, 2, MaxInt))
else
if CmdLine[1]='}' then
SaveRawEnv(Copy(CmdLine, 2, MaxInt))
else
if CmdLine[1]='e' then
EditEnvironment
else
begin
Name := Copy(CmdLine, 2, MaxInt);
Entries := ReadEntries;
for I:=0 to High(Entries) do
if Entries[I].Name = Name then
begin
if CmdLine[1]='-' then
Entries[I].Enabled := False
else
if CmdLine[1]='+' then
Entries[I].Enabled := True
else
if CmdLine[1]='*' then
Entries[I].Enabled := not Entries[I].Enabled