-
Notifications
You must be signed in to change notification settings - Fork 56
/
kn_MacroMng.pas
2881 lines (2562 loc) · 93.7 KB
/
kn_MacroMng.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 kn_MacroMng;
(****** LICENSE INFORMATION **************************************************
- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/.
------------------------------------------------------------------------------
(c) 2000-2005 Marek Jedlinski <marek@tranglos.com> (Poland)
(c) 2007-2015 Daniel Prado Velasco <dprado.keynote@gmail.com> (Spain) [^]
[^]: Changes since v. 1.7.0. Fore more information, please see 'README.md'
and 'doc/README_SourceCode.txt' in https://github.com/dpradov/keynote-nf
*****************************************************************************)
interface
Uses Classes,
kn_Macro, kn_Cmd, kn_Info, kn_NoteObj;
var
StartupMacroFile : wideString; // macro to be autorun on program startup
StartupPluginFile : wideString; // plugin to be autorun on program startup
IsRecordingMacro : boolean;
IsRunningMacro : boolean;
MacroAbortRequest : boolean;
LastMacroFN : wideString;
LastEditCmd : TEditCmd; // last ecXXX command issued
CommandRecall : TCommandRecall; // record that keeps parameters for last ecXXX command
MacroRecordingPaused : boolean;
MacroErrorAbort : boolean;
ActiveMacro : TMacro;
FontFormatToCopy : TkntFontAttributes;
ParaFormatToCopy : TkntParaAttributes;
// macro-related routines
procedure MacroInitialize;
procedure EnumerateMacros;
procedure EditMacro( const AsNew : boolean );
procedure DeleteMacro;
procedure ExecuteMacro( aFileName, aMacroName : wideString );
procedure PlayMacro( const Macro : TMacro );
procedure RecordMacro;
procedure PauseRecordingMacro;
procedure StopRecordingMacro;
procedure AddMacroKeyPress( const Key : Word; const Shift : TShiftState );
procedure AddMacroEditCommand( aCmd : TEditCmd );
function GetCurrentMacro( const DoWarn : boolean ) : TMacro;
function GetMacroByName( const aName : wideString; const DoWarn : boolean ) : TMacro;
procedure ExecuteMacroFile;
procedure AddUserMacroCommand;
function MacroProcess( const DoWarn : boolean ) : boolean;
function GetMacroIconIndex( const Macro : TMacro ) : integer;
// edit commands
procedure PerformCmd( aCmd : TEditCmd );
procedure PerformCmdEx( aCmd : TEditCmd );
procedure PerformCmdPastePlain( Note: TTabNote; StrClp: WideString = ''; HTMLClip: WideString= ''; ForcePlainText: boolean = false);
procedure RepeatLastCommand;
procedure UpdateLastCommand( const aCMD : TEditCmd );
procedure PerformCustomFuncKey( const Key : Word; const Shift : TShiftState ); // OBSOLETE
function RunParagraphDialog : boolean;
function RunLanguageDialog : boolean;
function CmdPaste(const fromButton: boolean; const ForcePlain: boolean): boolean;
Function CmdCopy: boolean;
Function CmdCut: boolean;
implementation
uses Windows, Messages, SysUtils, Forms, Dialogs, Graphics, StdCtrls, Controls, kn_clipUtils,
TreeNT, RichEdit, RxStrUtils, RxRichEd,
gf_miscvcl, gf_strings, gf_misc,
kn_MacroCmd, kn_MacroCmdSelect, kn_MacroEdit, kn_Main, kn_Global,
kn_Const, kn_NodeList, kn_DateTime, kn_LanguageSel, kn_Paragraph,
kn_FindReplaceMng, kn_StyleMng, kn_FavoritesMng, kn_BookmarksMng,
kn_TreeNoteMng, kn_NoteMng,kn_PluginsMng, kn_TemplateMng, kn_NoteFileMng,
kn_EditorUtils, kn_VCLControlsMng, WideStrings, TntSysUtils;
resourcestring
STR_01 = 'Stop recording macro';
STR_02 = '&Stop Recording Macro';
STR_03 = ' Recording macro "%s"';
STR_04 = 'Command "%s" cannot be included in macros. This command has been executed but will not be recorded in the macro.';
STR_05 = 'You have executed a command which opens a dialog box. KeyNote can ' +
'remember the values you have just selected and use them when ' +
'replaying the macro, OR KeyNote can display the dialog box again to let you select values ' +
'each time the macro is executed. Do you want KeyNote to remember current values?' + #13#13 +
'Click YES to save current values. Click NO to always open the dialog box. Click CANCEL to ' +
'skip this command and continue recording macro.';
STR_06 = ' Macro recording PAUSED';
STR_07 = ' Macro recording RESUMED';
STR_08 = 'Macro "%s" contains no commands and will be discarded.';
STR_09 = 'Save new macro "%s" (%d lines)?';
STR_10 = 'Error saving macro "%s": %s';
STR_11 = ' Macro recorded and saved.';
STR_12 = ' Macro discarded.';
STR_13 = ' Macro error';
STR_14 = 'Error adding new macro "%s": %s';
STR_15 = 'Record a new macro';
STR_16 = '&Record Macro...';
STR_17 = 'Active note "%s" is Read-only. Running the macro may cause the note to be modified. Do you want the macro to run anyway?';
STR_18 = 'Cannot load macro file "%s". Reason: %s';
STR_19 = 'Running macro "%s"';
STR_20 = 'Execute most recent macro "%s"';
STR_21 = 'Error loading macro "%s": %s';
STR_22 = 'Cannot execute macro "%s": This macro requires a newer version of KeyNote.';
STR_23 = ' Unexpected error while executing macro';
STR_24 = ' Macro was aborted by user';
STR_25 = ' Macro done';
STR_26 = 'OK to delete selected macro "%s"?';
STR_27 = 'Cannot delete macro file "%s"';
STR_28 = 'Error while deleting macro: ';
STR_29 = 'Macro aborted on line %d: "%s"' + #13 + 'Reason: %s';
STR_30 = 'unknown command';
STR_31 = 'syntax error';
STR_32 = 'unknown user command';
STR_33 = 'string argument required';
STR_34 = 'integer argument required';
STR_35 = 'Cannot run embedded macro "%s". Reason: %s';
STR_36 = 'Note creation failed';
STR_37 = 'Invalid font style argument';
STR_38 = 'Unexpected error while executing macro: %s' + #13#13 +
'Last macro line was: "%s" (line %d)';
STR_39 = 'This command cannot be executed while macro is being recorded or replayed.';
STR_40 = 'Macro "%s" not found.';
STR_41 = 'No macros available or none selected.';
STR_42 = 'Could not access current macro.';
STR_43 = ' This command cannot be repeated';
STR_44 = 'This action cannot be performed, because there is no active note (%d)';
STR_45 = 'This note cannot be set as Read-only, because it is being used for clipboard capture.';
STR_46 = 'Failed to assign font attributes.';
STR_47 = 'Failed to assign paragraph attributes.';
STR_48 = 'Go to line';
STR_49 = 'Enter line number or increment (+-):';
STR_50 = 'Unexpected or not implemented command: ';
STR_51 = 'Cannot perform command:';
STR_52 = 'No font attributes to paste from: Use "Copy font attributes" first.';
STR_53 = 'No paragraph attributes to paste from: Use "Copy paragraph attributes" first.';
STR_54 = '"%s" is not a valid number';
STR_55 = 'New background color will be assigned to ALL TREE NODES in note %s' + #13 + 'Continue?';
STR_56 = 'Repeat %s';
STR_57 = 'Select macro to execute';
var
RecallingCommand : boolean; // if TRUE, we use information in CommandRecall
function GetMacroIconIndex( const Macro : TMacro ) : integer;
begin
result := MACRO_IMAGE_BASE;
if ( pos ( _MACRO_AUTORUN_PREFIX, Macro.FileName ) = 1 ) then
inc( result );
end; // GetMacroIconIndex
procedure EnumerateMacros;
var
i : integer;
macro : TMacro;
begin
// Combo_Macro.Items.BeginUpdate;
Form_Main.ListBox_ResMacro.Items.BeginUpdate;
try
// Combo_Macro.Items.Clear;
Form_Main.ListBox_ResMacro.Items.Clear;
ClearMacroList;
LoadMacroList;
for i := 1 to Macro_List.Count do
begin
Macro := TMacro( Macro_List.Objects[pred(i)] );
Form_Main.ListBox_ResMacro.AddItem( Macro.Name, cbUnchecked, GetMacroIconIndex( Macro ));
// Combo_Macro.AddItem( Macro.Name, GetMacroIconIndex( Macro ));
end;
finally
// Combo_Macro.Items.EndUpdate;
Form_Main.ListBox_ResMacro.Items.EndUpdate;
end;
{ if ( Combo_Macro.Items.Count > 0 ) then
Combo_Macro.ItemIndex := 0; }
if ( Form_Main.ListBox_ResMacro.Items.Count > 0 ) then
Form_Main.ListBox_ResMacro.ItemIndex := 0;
end; // EnumerateMacros
procedure RecordMacro;
begin
if IsRecordingMacro then
begin
StopRecordingMacro;
exit;
end;
if MacroProcess( true ) then exit;
if ( not Form_Main.HaveNotes( true, true )) then exit;
if ( not assigned( ActiveNote )) then exit;
if ( not CheckFolder( 'Macro', Macro_Folder, true, true )) then exit;
EditMacro( true ); // create a new macro
if ( ActiveMacro = nil ) then exit;
LastEditCmd := ecNone;
UpdateLastCommand( ecNone );
try
ActiveNote.Editor.SetFocus;
except
end;
// preset or clear some global settings
FindOptions.Pattern := ''; // enforce new search
IsRecordingMacro := true;
MacroRecordingPaused := false;
with Form_Main do
begin
TB_MacroRecord.ImageIndex := TB_MacroRecord.ImageIndex + 1;
TB_MacroRecord.Hint := STR_01;
MacMMacro_Record.Caption := STR_02;
MacMMacro_Record.Hint := TB_MacroRecord.Hint;
MacMMacroUserCommand.Enabled := true;
Form_Main.StatusBar.Panels[PANEL_HINT].Text := Format( STR_03, [ActiveMacro.Name] );
SelectStatusbarGlyph( true );
end;
end; // RecordMacro
procedure AddMacroKeyPress( const Key : Word; const Shift : TShiftState );
begin
if MacroRecordingPaused then exit;
if ( not assigned( ActiveMacro )) then exit;
ActiveMacro.Lines.Add( Format(
'%d%s%s',
[Key,_MACRO_DELIMITER_CHAR,ShiftStateToStr( Shift )]
));
end; // AddMacroKeyPress
procedure AddMacroEditCommand( aCmd : TEditCmd );
var
OnPlayShowDlg : boolean;
begin
if MacroRecordingPaused then exit;
if ( not assigned( ActiveMacro )) then exit;
if ( aCmd = ecNone ) then exit;
if ( aCmd in CommandsProhibitedInMacros ) then
begin
messagedlg( Format(
STR_04,
[EDITCMD_NAMES[aCmd]]
), mtWarning, [mbOK], 0 );
exit;
end;
if ( aCMD in EditCommandsWithNoArgs ) then
begin
ActiveMacro.Lines.Add( Format(
'%s%s',
[_MACRO_CMD_CHAR,EDITCMD_MACRO_NAMES[aCMD]]
));
end
else
if ( aCMD in EditCommandsWithDialogs ) then
begin
case messagedlg( STR_05,
mtConfirmation, [mbYes,mbNo,mbCancel], 0 ) of
mrYes : OnPlayShowDlg := false;
mrNo : OnPlayShowDlg := true;
else
exit;
end;
if OnPlayShowDlg then
begin
ActiveMacro.Lines.Add( Format(
'%s%s',
[_MACRO_CMD_CHAR,
EDITCMD_MACRO_NAMES[aCMD]
]));
end
else
begin
case aCMD of
ecBGColorDlg : begin
ActiveMacro.Lines.Add( Format(
'%s%s%s%s',
[_MACRO_CMD_CHAR,
EDITCMD_MACRO_NAMES[aCMD],
_MACRO_DELIMITER_CHAR,
ColorToString( CommandRecall.Color )
]));
end;
ecFontColorDlg : begin
ActiveMacro.Lines.Add( Format(
'%s%s%s%s',
[_MACRO_CMD_CHAR,
EDITCMD_MACRO_NAMES[aCMD],
_MACRO_DELIMITER_CHAR,
ColorToString( CommandRecall.Font.Color )
]));
end;
ecFontDlg : with CommandRecall.Font do
begin
ActiveMacro.Lines.Add( Format(
'%s%s%s%d%s%s%s%s%s%d%s%s',
[_MACRO_CMD_CHAR,
EDITCMD_MACRO_NAMES[aCMD],
_MACRO_DELIMITER_CHAR,
Charset,
_MACRO_DELIMITER_CHAR,
ColorToString( Color ),
_MACRO_DELIMITER_CHAR,
Name,
_MACRO_DELIMITER_CHAR,
Size,
_MACRO_DELIMITER_CHAR,
FontStyleToStr( Style )
]));
end;
ecGoTo : begin
ActiveMacro.Lines.Add( Format(
'%s%s%s%s',
[_MACRO_CMD_CHAR,
EDITCMD_MACRO_NAMES[aCMD],
_MACRO_DELIMITER_CHAR,
CommandRecall.GoToidx
]));
end;
ecHighlightDlg : begin
ActiveMacro.Lines.Add( Format(
'%s%s%s%s',
[_MACRO_CMD_CHAR,
EDITCMD_MACRO_NAMES[aCMD],
_MACRO_DELIMITER_CHAR,
ColorToString( CommandRecall.Color )
]));
end;
ecLanguage : begin
ActiveMacro.Lines.Add( Format(
'%s%s%s%d',
[_MACRO_CMD_CHAR,
EDITCMD_MACRO_NAMES[aCMD],
_MACRO_DELIMITER_CHAR,
CommandRecall.Language
]));
end;
ecParaDlg : with CommandRecall.Para do
begin
ActiveMacro.Lines.Add( Format(
'%s%s%s%d%s%d%s%d%s%d%s%d%s%d%s%d%s%d',
[_MACRO_CMD_CHAR,
EDITCMD_MACRO_NAMES[aCMD],
_MACRO_DELIMITER_CHAR,
ord( SpacingRule ),
_MACRO_DELIMITER_CHAR,
ord( LIndent ),
_MACRO_DELIMITER_CHAR,
ord( RIndent ),
_MACRO_DELIMITER_CHAR,
ord( FIndent ),
_MACRO_DELIMITER_CHAR,
SpaceBefore,
_MACRO_DELIMITER_CHAR,
SpaceAfter,
_MACRO_DELIMITER_CHAR,
ord( Numbering ),
_MACRO_DELIMITER_CHAR,
ord( Alignment )
]));
end;
end;
end;
end
else
begin
// special cases
case aCMD of
ecFontName : begin
ActiveMacro.Lines.Add( Format(
'%s%s%s%s',
[_MACRO_CMD_CHAR,
EDITCMD_MACRO_NAMES[aCMD],
_MACRO_DELIMITER_CHAR,
CommandRecall.Font.Name
]));
end;
ecFontSize : begin
ActiveMacro.Lines.Add( Format(
'%s%s%s%d',
[_MACRO_CMD_CHAR,
EDITCMD_MACRO_NAMES[aCMD],
_MACRO_DELIMITER_CHAR,
CommandRecall.Font.Size
]));
end;
ecFontColor, ecFontColorBtn : begin
ActiveMacro.Lines.Add( Format(
'%s%s%s%s',
[_MACRO_CMD_CHAR,
EDITCMD_MACRO_NAMES[aCMD],
_MACRO_DELIMITER_CHAR,
ColorToString( CommandRecall.Font.Color )
]));
end;
ecHighlight, ecHighlightBtn : begin
ActiveMacro.Lines.Add( Format(
'%s%s%s%s',
[_MACRO_CMD_CHAR,
EDITCMD_MACRO_NAMES[aCMD],
_MACRO_DELIMITER_CHAR,
ColorToString( CommandRecall.Color )
]));
end;
ecInsCharacter : begin
ActiveMacro.Lines.Add( Format(
'%s%s%s%d%s%d%s%s%s%d',
[_MACRO_CMD_CHAR,
EDITCMD_MACRO_NAMES[aCMD],
_MACRO_DELIMITER_CHAR,
CommandRecall.CharInfo.Code,
_MACRO_DELIMITER_CHAR,
CommandRecall.CharInfo.Count,
_MACRO_DELIMITER_CHAR,
CommandRecall.CharInfo.Name,
_MACRO_DELIMITER_CHAR,
CommandRecall.CharInfo.Charset
]));
end;
ecStyleApply : begin
ActiveMacro.Lines.Add( Format(
'%s%s%s%s',
[_MACRO_CMD_CHAR,
EDITCMD_MACRO_NAMES[aCMD],
_MACRO_DELIMITER_CHAR,
CommandRecall.StyleName
]));
end;
ecFindText : begin
ActiveMacro.Lines.Add( Format(
'%s%s%s%s%s%s%s%s',
[_MACRO_CMD_CHAR,
EDITCMD_MACRO_NAMES[aCMD],
_MACRO_DELIMITER_CHAR,
FindOptions.Pattern,
_MACRO_DELIMITER_CHAR,
BOOLEANSTR[FindOptions.MatchCase],
_MACRO_DELIMITER_CHAR,
BOOLEANSTR[FindOptions.WholeWordsOnly]
]));
end;
end;
end;
end; // AddMacroEditCommand
procedure PauseRecordingMacro;
begin
if ( not IsRecordingMacro ) then exit;
MacroRecordingPaused := ( not MacroRecordingPaused );
Form_Main.TB_MacroPause.Down := MacroRecordingPaused;
if MacroRecordingPaused then
Form_Main.StatusBar.Panels[PANEL_HINT].Text := STR_06
else
Form_Main.StatusBar.Panels[PANEL_HINT].Text := STR_07;
end; // PauseRecordingMacro
procedure StopRecordingMacro;
begin
try
if ( ActiveMacro = nil ) then exit;
try
if MacroRecordingPaused then
PauseRecordingMacro;
Form_Main.MacMMacroUserCommand.Enabled := false;
if ( ActiveMacro.Lines.Count = 0 ) then
begin
Messagedlg( Format(
STR_08,
[ActiveMacro.Name]
), mtInformation, [mbOK], 0 );
ActiveMacro.Free;
exit;
end;
if ( messagedlg( Format(
STR_09,
[ActiveMacro.Name,ActiveMacro.Lines.Count]
), mtConfirmation, [mbYes,mbNo], 0 ) <> mrNo ) then
begin
if ( not ActiveMacro.Save ) then
begin
messagedlg( Format(
STR_10, [ActiveMacro.FileName,ActiveMacro.LastError]
), mtError, [mbOK], 0 );
ActiveMacro.Free;
exit;
end;
// Combo_Macro.ItemIndex := Combo_Macro.AddItem( ActiveMacro.Name, GetMacroIconIndex( ActiveMacro ));
Form_Main.ListBox_ResMacro.ItemIndex := Form_Main.ListBox_ResMacro.AddItem( ActiveMacro.Name, cbUnchecked, GetMacroIconIndex( ActiveMacro ));
Macro_List.AddObject( ActiveMacro.Name, ActiveMacro );
Form_Main.StatusBar.Panels[PANEL_HINT].Text := STR_11
end
else
begin
Form_Main.StatusBar.Panels[PANEL_HINT].Text := STR_12;
ActiveMacro.Free;
end;
except
on E : Exception do
begin
Form_Main.StatusBar.Panels[PANEL_HINT].Text := STR_13;
messagedlg( Format(
STR_14,
[ActiveMacro.Name,E.Message] ), mtError, [mbOK], 0 );
ActiveMacro.Free;
end;
end;
finally
ActiveMacro := nil;
IsRecordingMacro := false;
MacroRecordingPaused := false;
with Form_Main do
begin
SelectStatusbarGlyph( true );
TB_MacroRecord.ImageIndex := TB_MacroRecord.ImageIndex - 1;
TB_MacroRecord.Hint := STR_15;
MacMMacro_Record.Caption := STR_16;
MacMMacro_Record.Hint := TB_MacroRecord.Hint;
end;
end;
end; // StopRecordingMacro
procedure ExecuteMacro( aFileName, aMacroName : wideString );
var
macro : TMacro;
wasNewMacro, wasreadonly : boolean;
begin
// if aFileName is specified, the macro is loaded from the file
// specified. Else, if aName is specified, the procedure looks
// for a macro in Macro_List
if MacroProcess( true ) then exit;
Macro := nil;
wasNewMacro := false;
// Normally, macros cannot run if there are no notes,
// Exception: We do allow _MACRO_AUTORUN_NEW_FILE to run,
// otherwise the feature would be impossible to implement.
// It is the macro author's responsibility to make sure
// that the macro dooesn't do anything unreasonable, like
// inserting text before creating a note
if ( aFileName <> _MACRO_AUTORUN_NEW_FILE ) then
begin
if ( not Form_Main.HaveNotes( true, true )) then exit;
if ( not assigned( ActiveNote )) then exit;
end;
wasreadonly := Form_Main.NoteIsReadOnly( ActiveNote, false );
if wasreadonly then
begin
if ( DoMessageBox( WideFormat(
STR_17,
[ActiveNote.Name] ),
mtWarning, [mbYes,mbNo], 0 ) <> mrYes ) then exit;
ActiveNote.ReadOnly := false;
end;
if ( aFileName <> '' ) then
begin
if ( pos( '\', aFileName ) = 0 ) then
aFileName := Macro_Folder + aFileName;
Macro := TMacro.Create;
Macro.FileName := aFileName;
if ( not Macro.Load ) then
begin
DoMessageBox( WideFormat(
STR_18,
[aFileName,Macro.LastError]
), mtError, [mbOK], 0 );
Macro.Free;
Macro := nil;
end;
wasNewMacro := true;
end
else
begin
if ( aMacroName = '' ) then
begin
Macro := GetCurrentMacro( true );
end
else
begin
Macro := GetMacroByName( aMacroName, true );
end;
end;
if ( macro = nil ) then
exit;
IsRunningMacro := true;
MacroAbortRequest := false;
MacroErrorAbort := false;
Form_Main.StatusBar.Panels[PANEL_HINT].Text := WideFormat(
STR_19, [Macro.Name] );
screen.Cursor := crAppStart;
SelectStatusbarGlyph( true );
try
ActiveNote.FocusMemory := focRTF;
ActiveNote.Editor.SetFocus;
except
end;
LastMacroFN := WideExtractFilename( Macro.FileName );
Form_Main.MMToolsMacroRunLast.Hint := WideFormat(
STR_20,
[WideExtractFilename( LastMacroFN )]
);
try
try
if ( not Macro.Load ) then
begin
DoMessageBox( WideFormat(
STR_21, [Macro.FileName,Macro.LastError]
), mtError, [mbOK], 0 );
end;
if ( Macro.Version.Major > _MACRO_VERSION_MAJOR ) then
begin
DoMessageBox( WideFormat(
STR_22, [Macro.FileName]
), mtError, [mbOK], 0 );
end;
PlayMacro( Macro );
except
On E : Exception do
begin
messagedlg( E.message, mtError, [mbOK], 0 );
exit;
end;
end;
finally
if MacroErrorAbort then
begin
Form_Main.StatusBar.Panels[PANEL_HINT].Text := STR_23;
end
else
if MacroAbortRequest then
begin
Form_Main.StatusBar.Panels[PANEL_HINT].Text := STR_24;
end
else
begin
Form_Main.StatusBar.Panels[PANEL_HINT].Text := STR_25;
end;
try
if wasNewMacro then
Macro.Free
else
Macro.Clear;
except
end;
MacroAbortRequest := false;
IsRunningMacro := false;
screen.Cursor := crDefault;
SelectStatusbarGlyph( true );
if wasreadonly then
ActiveNote.ReadOnly := true;
UpdateNoteFileState( [fscModified] );
end;
try
ActiveNote.FocusMemory := focRTF;
ActiveNote.Editor.SetFocus;
except
end;
end; // ExecuteMacro
procedure EditMacro( const AsNew : boolean );
var
Form_Macro: TForm_Macro;
Macro : TMacro;
begin
if MacroProcess( true ) then exit;
if AsNew then
begin
Macro := TMacro.Create;
end
else
begin
Macro := GetCurrentMacro( true );
if ( Macro = nil ) then
exit;
if ( not Macro.Load ) then
begin
DoMessageBox( WideFormat( STR_21, [Macro.FileName,Macro.LastError] ), mtError, [mbOK], 0 );
Macro.Free;
exit;
end;
end;
Form_Macro := TForm_Macro.Create( Form_Main );
try
with Form_Macro do
begin
MName := Macro.Name;
MDesc := Macro.Description;
MDate := Macro.DateModified;
MFileName := Macro.FileName;
MAbort := Macro.AbortOnError;
myNewMacro := AsNew;
end;
if ( Form_Macro.ShowModal = mrOK ) then
begin
with Form_Macro do
begin
Macro.Name := MName;
Macro.Description := MDesc;
Macro.DateModified := DateTimeToStr( now );
Macro.AbortOnError := MAbort;
if AsNew then
begin
Macro.FileName := MFileName;
ActiveMacro := Macro;
end
else
begin
// update existing macro
if ( MName <> OriginalName ) then
begin
Form_Main.ListBox_ResMacro.Items.Delete( Form_Main.ListBox_ResMacro.ItemIndex );
Form_Main.ListBox_ResMacro.ItemIndex := Form_Main.ListBox_ResMacro.AddItem( Macro.Name, cbUnchecked, GetMacroIconIndex( Macro ));
Macro_List.Delete( Macro_List.IndexOf( OriginalName ));
Macro_List.AddObject( MName, Macro );
end;
if ( not Macro.Save ) then
begin
DoMessageBox( WideFormat(
STR_10, [Macro.FileName,Macro.LastError]
), mtError, [mbOK], 0 );
exit;
end;
end;
end;
end
else
begin
ActiveMacro := nil;
if AsNew then
Macro.Free;
end;
finally
Form_Macro.Free;
end;
end; // EditMacro
procedure DeleteMacro;
var
Macro : TMacro;
DeleteSuccess : boolean;
i : integer;
begin
if MacroProcess( true ) then exit;
Macro := GetCurrentMacro( true );
if ( macro = nil ) then exit;
if ( DoMessageBox( WideFormat(
STR_26,
[Macro.Name]
), mtConfirmation, [mbYes,mbNo], 0 ) <> mrYes ) then exit;
DeleteSuccess := false;
try
try
if ( not deletefile( Macro_Folder + Macro.FileName )) then
begin
DeleteSuccess := false;
DoMessageBox( WideFormat(
STR_27,
[Macro.Filename] ), mtError, [mbOK], 0 );
exit;
end;
i := Form_Main.ListBox_ResMacro.ItemIndex;
Form_Main.ListBox_ResMacro.Items.Delete( i );
i := Macro_List.IndexOf( Macro.Name );
if ( i >= 0 ) then
begin
Macro.Free;
Macro_List.Delete( i );
end;
DeleteSuccess := true;
except
on E : Exception do
begin
DeleteSuccess := false;
DoMessageBox( STR_28 + E.Message, mtError, [mbOK], 0 );
end;
end;
finally
if DeleteSuccess then
begin
if ( Form_Main.ListBox_ResMacro.Items.Count > 0 ) then
Form_Main.ListBox_ResMacro.ItemIndex := 0;
end;
end;
end; // DeleteMacro
procedure PlayMacro( const Macro : TMacro );
var
MacroCmds, EditCmds, ArgList : TStringList;
newMacro : TMacro;
line : string;
p, i, linecnt, cmdcnt, ErrorCnt, cmdidx : integer;
cmdstr, argstr : string;
argint, counter : integer;
macrocmd : TMacroCmd;
editcmd : TEditCmd;
MacroFinished : boolean;
myFontStyle : TFontStyle;
sndKey : word;
sndShift : TShiftState;
procedure AbortMacro( const s : string; i : integer; const line : string );
begin
messagedlg( Format(
STR_29,
[i,copy( line, 1, 127 ),s]
), mtError, [mbOK], 0 );
MacroFinished := true;
MacroErrorAbort := true;
end;
begin
if ( not assigned( Macro )) then exit;
if ( MacroAbortRequest or MacroErrorAbort ) then exit;
linecnt := 1;
ErrorCnt := 0;
cmdstr := '';
argstr := '';
argint := 0;
myFontStyle := fsBold;
MacroCmds := TStringList.Create;
MacroCmds.Capacity := succ( ord( high( TMacroCmd )));
for macrocmd := low( TMacroCmd ) to high( TMacroCmd ) do
MacroCmds.Add( MACRO_CMD_NAMES[macrocmd] );
EditCmds := TStringList.Create;
for editcmd := low( TEditCmd ) to high( TEditCmd ) do
EditCmds.Add( EDITCMD_MACRO_NAMES[editcmd] );
ArgList := TStringList.Create;
try
try
repeat
MacroFinished := true;
for i := 1 to Macro.Lines.Count do
begin
Application.ProcessMessages;
if MacroAbortRequest then
begin
MacroFinished := true;
break;
end;
linecnt := i;
line := Macro.Lines[pred( i )];
if ( line = '' ) then continue;
case line[1] of
_MACRO_COMMENT_CHAR : continue;
_MACRO_CMD_CHAR : begin
inc( cmdcnt );
// execute internal command
delete( line, 1, 1 );
p := pos( _MACRO_DELIMITER_CHAR, line );
cmdstr := '';
argstr := '';
if ( p = 0 ) then
begin
cmdstr := line;
end
else
begin
cmdstr := copy( line, 1, pred( p ));
argstr := copy( line, succ( p ), length( line ));
end;
cmdstr := lowercase( cmdstr );
cmdidx := EditCmds.IndexOf( cmdstr );
if ( cmdidx < 0 ) then
begin
if Macro.AbortOnError then
begin
AbortMacro( STR_30, i, Macro.Lines[pred( i )] );
break;
end
else
begin
inc( ErrorCnt );
continue;
end;
end;
EditCmd := TEditCmd( cmdidx );
if ( EditCmd in EditCommandsWithNoArgs ) then
begin
LastEditCmd := EditCmd;
RepeatLastCommand;
end
else
if ( EditCmd in EditCommandsWithDialogs ) then
begin
if ( argstr = '' ) then
begin
if ( EditCmd in EditCommandsEx ) then
PerformCmdEx( EditCmd )
else
PerformCmd( EditCmd );
end
else
begin
try
case EditCmd of
ecBGColorDlg : begin
CommandRecall.Color := StringToColor( argstr );
end;
ecFontColorDlg : begin
CommandRecall.Font.Color := StringToColor( argstr );
end;
ecFontDlg : begin
CSVTextToStrs( ArgList, argstr, _MACRO_DELIMITER_CHAR );
with CommandRecall.Font do
begin
Charset := strtoint( ArgList[0] );
Color := StringToColor( Arglist[1] );
Name := ArgList[2];
Size := strtoint( ArgList[3] );
Style := StrToFontStyle( ArgList[4] );
end;
end;
ecLanguage : begin