-
Notifications
You must be signed in to change notification settings - Fork 0
1696 lines (1626 loc) · 62.3 KB
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
--[[
do return end --]]
--[[
GitHub file:
https://github.com/dr-dba/far-lua-key-efficiency/edit/main/[email protected]
DEPENDS ON MODULES:
https://github.com/dr-dba/far-lua-internals
https://github.com/dr-dba/far-lua-general-utils
https://github.com/dr-dba/far-lua-diagnostics
https://github.com/dr-dba/lua-serialize
]]
-- ### INFO AND OPTIONS BLOCK ###
-- luacheck: ignore 113/Info
local Info = Info or package.loaded.regscript or function(...) return ... end
local nfo = Info({
_filename or ...,
name = "MacroEx",
-- запуск макросов нетрадиционными способами
description = "Launching macros in unconventional ways (+@Xer0X mod)",
id = "35FD5420-D2A1-4A1C-AAFD-4C902231F549",
version = "3.2",
version_mod = "3.1.0.1",
author = "JD",
author_mod = "x-Team",
url = "https://forum.farmanager.com/viewtopic.php?f=15&t=8764",
url_mod = "https://forum.farmanager.com/viewtopic.php?f=15&t=12459",
url_git = "https://github.com/dr-dba/far-lua-key-efficiency",
-- LuaMacro 501 (ConsoleInput)
minfarversion = { 3, 0, 0, 4430, 0 },
options = {
BreakSeqOnModRelease
= false,
AllowSeqWithAnyMod
= true,
AllowSeqInExt = true,
DelayExt = 400, -- 500 is ok
DelaySeq = 2000,
DelayHlp = 3000,
LimitHld = 400,
LimitDbl = 600,
ExtModes = {
HLD = true, -- holding
DBL = true, -- double
STT = true, -- state
MNY = true, -- many of the key expr
},
DbgShowBadExt = false,
DbgShowBadKey = false,
DbgShowLoadTime = true,
}
})
if not nfo then return end
local opts = nfo.options
local BREAK_SEQ_ON_MOD_RELEASE = opts.BreakSeqOnModRelease
local ALLOW_SEQ_WITH_ANY_MOD = opts.AllowSeqWithAnyMod
local ALLOW_SEQ_IN_EXT = opts.AllowSeqInExt
local OPT_DELAY_SEQ = opts.DelaySeq
local OPT_DELAY_HLP = opts.DelayHlp
local OPT_DELAY_EXT = opts.DelayExt
local OPT_LIMIT_HLD = opts.LimitHld
local OPT_LIMIT_DBL = opts.LimitDbl
-- @@@ END OF THE INFO AND OPTIONS BLOCK @@@
local F = far.Flags
local C = far.Colors
local KEY_EVENT = F.KEY_EVENT
local MOUSE_EVENT = F.MOUSE_EVENT
local WND_BUF_SIZE_EVT = F.WINDOW_BUFFER_SIZE_EVENT
local MENU_EVENT = F.MENU_EVENT
local FOCUS_EVENT = F.FOCUS_EVENT
local ACTL_GETFARRECT = F.ACTL_GETFARRECT
local ACTL_GETCURSORPOS = F.ACTL_GETCURSORPOS
local ACTL_SETCURSORPOS = F.ACTL_SETCURSORPOS
local ACTL_REDRAWALL = F.ACTL_REDRAWALL
local EOPT_SHOWTITLEBAR = F.EOPT_SHOWTITLEBAR
local EOPT_SHOWKEYBAR = F.EOPT_SHOWKEYBAR
local EOPT_SHOWSCROLLBAR= F.EOPT_SHOWSCROLLBAR
local DI_SINGLEBOX = F.DI_SINGLEBOX
local DI_TEXT = F.DI_TEXT
local FDLG_NONMODAL = F.FDLG_NONMODAL
local DN_CLOSE = F.DN_CLOSE
local str_lower = utf8.Utf8_lower or utf8.lower
local str_upper = utf8.Utf8_upper or utf8.upper
local str_gmatch= utf8.Utf8_gmatch or utf8.gmatch
local str_match = utf8.Utf8_match or utf8.match
local str_find = utf8.Utf8_find or utf8.find
local str_gsub = utf8.Utf8_gsub or utf8.gsub
local str_len = utf8.Utf8_len or utf8.len
local str_rev = utf8.reverse or utf8.reverse
local str_sub = utf8.sub
local str_cfind = utf8.cfind
local str_format= utf8.format
local str_fmt = utf8.format
local str_fmt_u = utf8.format
local str_fmt_s = string.format
local str_low_s = string.lower
local str_low_a = string.lower
local str_low = string.lower
local math_min = math.min
local math_max = math.max
local tbl_concat= table.concat
local tbl_insert= table.insert
local tbl_ins = table.insert
local tbl_remove= table.remove
local tbl_sort = table.sort
local tostr = tostring
local tostring = tostring
local io_open = io.open
local bit64_band= bit64.band
local dbg_getinf= debug.getinfo
local far_Message = far.Message
local far_ProcessName = far.ProcessName
local far_NameToInputRecord = far.NameToInputRecord
local far_InputRecordToName = far.InputRecordToName
local far_AdvControl = far.AdvControl
local far_Timer = far.Timer
local far_Text = far.Text
local mf_postmacro = mf.postmacro
local mf_waitkey = mf.waitkey
local win_uuid = win.Uuid
local win_GetKeyState = win.GetKeyState
local editor_GetInfo = editor.GetInfo
local panel_GetCmdLineSelection = panel.GetCmdLineSelection
local panel_SetCmdLineSelection = panel.SetCmdLineSelection
local panel_GetCmdLinePos = panel.GetCmdLinePos
local panel_SetCmdLinePos = panel.SetCmdLinePos
local panel_GetCmdLine = panel.GetCmdLine
local MCODE_F_CHECKALL = 0x80C64
local MCODE_F_GETOPTIONS = 0x80C65
local MCODE_F_MACROSETTINGS = 0x80C6A
local EVAL_SHIFT_X = 1000
local EVAL_SUCCESS = 0
local EVAL_SUCCESS_X = 0 + EVAL_SHIFT_X
local EVAL_SYNTAXERROR = 11
local EVAL_BADARGS = -1
local EVAL_MACRONOTFOUND = -2
local EVAL_MACRONOTFOUND_X = -2 - EVAL_SHIFT_X
local EVAL_MACROCANCELED = -3
local EVAL_RUNTIMEERROR = -4
local EVAL_RES = {
[EVAL_SUCCESS] = "EVAL_SUCCESS" -- 0
,[EVAL_SYNTAXERROR] = "EVAL_SYNTAXERROR" -- 11
,[EVAL_BADARGS] = "EVAL_BADARGS" -- -1
,[EVAL_MACRONOTFOUND] = "EVAL_MACRONOTFOUND" -- -2 макрос не найден среди загруженных макросов
,[EVAL_MACROCANCELED] = "EVAL_MACROCANCELED" -- -3 было выведено меню выбора макроса, и пользователь его отменил
,[EVAL_RUNTIMEERROR] = "EVAL_RUNTIMEERROR" -- -4 макрос был прерван в результате ошибки времени исполнения
,[EVAL_SUCCESS_X] = "EVAL_SUCCESS_X"
,[EVAL_MACRONOTFOUND_X] = "EVAL_MACRONOTFOUND_X"
}
local LEFT_ALT_PRESSED = F.LEFT_ALT_PRESSED
local RIGHT_ALT_PRESSED = F.RIGHT_ALT_PRESSED
local LEFT_CTRL_PRESSED = F.LEFT_CTRL_PRESSED
local RIGHT_CTRL_PRESSED= F.RIGHT_CTRL_PRESSED
local SHIFT_PRESSED = F.SHIFT_PRESSED
local MOD_MASK = bor(
LEFT_ALT_PRESSED,
RIGHT_ALT_PRESSED,
LEFT_CTRL_PRESSED,
RIGHT_CTRL_PRESSED,
SHIFT_PRESSED
)
local fnc_extr_file_name= Xer0X.fnc_str_extract_file_name
local fnc_tbl_copy = Xer0X.fnc_tbl_copy
local fnc_str_split = Xer0X.fnc_str_split
local fnc_tbl_copy_to = Xer0X.fnc_tbl_copy_to
local fnc_tbl_count = Xer0X.fnc_tbl_count
local fnc_case_title = Xer0X.fnc_case_title
local fnc_case_toggle = Xer0X.fnc_case_toggle
local fnc_key_prettify = Xer0X.fnc_key_prettify
local obj_status = Xer0X.obj_screen_status_line
local LoadedMacros = Xer0X.utils.LoadedMacros
local GetAreaCode = Xer0X.utils_shared.GetAreaCode
local MacroCallFar = Xer0X.utils.MacroCallFar
local utils_Areas = Xer0X.utils.Areas
local RLSD_KEYS_CHCK_TICK = 100
local rlsd_keys_chck_last = Far.UpTime
local tbl_ext_mode_code = { double = "DBL", hold = "HLD", state = "STT" , many = "MNY" }
local tbl_ext_mode_disp = { DBL = ":Double", HLD = ":Hold", STT = ":State" , MNY = ":Many" }
local Xer0X = require("Lib-Common-@Xer0X")
local dmp_srlz= require("dmp-tbl-alt-@Xer0X")
local req_ok, req_msg, fnc_lua_exp= Xer0X.fnc_safe_require("LuaExplorer-@Xer0X")
if not req_ok
then req_ok, req_msg, fnc_lua_exp= Xer0X.fnc_safe_require("LE")
end
local as_mdl, inp_args, own_fpath = Xer0X.fnc_file_whoami({ ... })
local FileName = ... or own_fpath
local MACRO_HELP_FOOTER = "\n\1\nPress AltEscape to cancel the sequence\nPress ShiftSpace to run as normal keys"
local tbl_hld_keys = { }
Xer0X.tbl_hld_keys = tbl_hld_keys
local tbl_key_states = { }
Xer0X.tbl_key_states = tbl_key_states
local tbl_macro_ex_handlers = { }
Xer0X.tbl_macro_ex_handlers = tbl_macro_ex_handlers
local tbl_ext_key_lev_hist = { }
Xer0X.tbl_ext_key_lev_hist = tbl_ext_key_lev_hist
local tbl_key_inp_rec_cache = { } -- micro optimization
Xer0X.tbl_key_inp_rec_cache = tbl_key_inp_rec_cache
local tbl_key_evt_journal = { [KEY_EVENT] = { }, [MOUSE_EVENT] = { }, [MENU_EVENT] = { }, [FOCUS_EVENT] = { }, [WND_BUF_SIZE_EVT] = { }, [0] = { }, ALL = { } }
Xer0X.tbl_key_evt_journal = tbl_key_evt_journal
local tbl_parsed_keys_val_cache = { }
Xer0X.tbl_parsed_keys_val_cache = tbl_parsed_keys_val_cache
local tbl_parsed_keys_bad_cache = { }
Xer0X.tbl_parsed_keys_bad_cache = tbl_parsed_keys_bad_cache
local tbl_key_mcr_journal = { }
Xer0X.tbl_key_mcr_journal = tbl_key_mcr_journal
local tbl_ext_seq_key_reg = { }
Xer0X.tbl_ext_seq_key_reg = tbl_ext_seq_key_reg
local tbl_key_expanded_cache = { }
Xer0X.tbl_key_expanded_cache = tbl_key_expanded_cache
local tbl_normal_ask__load = loadfile(own_fpath..".dat")
local tbl_normal_ask = tbl_normal_ask__load and tbl_normal_ask__load() or { }
local vkc_no_repeat, flg_ext_mod_off -- to disable handler in order to run an original macro
local function fnc_err_msg(msg, ...)
local msg2 = msg.."\n\2"..debug.traceback("", 2):gsub("\t", " ")
local buttons = select('#', ...) > 0 and "Ok;&Lua explorer" or "Ok"
repeat local msg_res = far_Message(msg2, "MacroEx: error in macro definition", buttons, "wl")
if msg_res == 2
then -- to view args
fnc_lua_exp({ ... }, msg)
else break
end
until false
end -- fnc_err_msg
local fnc_NameToInputRecord = function(the_key)
local the_key_rec = tbl_key_inp_rec_cache[the_key]
if the_key_rec
then return the_key_rec
end
--[[ FarNameToInputRecord: "CtrlNumLock"-->VK_PAUSE
http://bugs.farmanager.com/view.php?id=2947
http://forum.farmanager.com/viewtopic.php?f=8&t=9357
Комбинация клавиш CtrlNumLock для системы означает VK_PAUSE, но фар об этом не знает.]]
local key_inp_rec = str_match(the_key, "r?ctrlnumlock")
and far_NameToInputRecord( "Pause")
or far_NameToInputRecord( the_key)
if not key_inp_rec
then if opts.DbgShowBadKey
then fnc_err_msg(str_format("Unexpected AKey: %q", the_key))
end
else tbl_key_inp_rec_cache[the_key] = key_inp_rec
end
return key_inp_rec
end
Xer0X.fnc_NameToInputRecord = fnc_NameToInputRecord
local function fnc_jrnl_inp_key_vis(j_rec)
return str_format("[%s] %s %s c:%s vkc:%s down:%s cks:%s",
tostring(j_rec.key_rec.EventType),
j_rec.area, j_rec.InpRecName or "<?>",
tostring(j_rec.key_rec.UnicodeChar),
tostring(j_rec.key_rec.VirtualKeyCode),
tostring(j_rec.key_rec.KeyDown),
tostring(j_rec.key_rec.ControlKeyState)
)
end
local function fnc_con_inp_journal(key_inp_rec)
if Area.Menu and Menu.Id == "7646F761-8954-42CA-9CFC-E3F98A1C54D3" then return { } end
local rec_j, tbl_j
rec_j = { area = Area.Current, key_rec = key_inp_rec, key_time = Far.UpTime, p1 = p1, p2 = p2, _LEX_DISPLAY_STR = fnc_jrnl_inp_key_vis }
tbl_j = tbl_key_evt_journal.ALL
tbl_j[#tbl_j + 1] = rec_j
if #tbl_j > 1000 then tbl_remove(tbl_j, 1) end
tbl_j = tbl_key_evt_journal[key_inp_rec.EventType]
tbl_j[#tbl_j + 1] = rec_j
if #tbl_j > 1000 then tbl_remove(tbl_j, 1) end
return rec_j
end
local function fnc_key_state_run_remove(key_state, dt_now)
local key_state_id = type(key_state) == "table" and key_state.key_state_id or key_state
local key_state_obj= type(key_state) == "table" and key_state or tbl_key_states[key_state]
if key_state_obj.fnc_state_final
then key_state_obj:fnc_state_final(dt_now)
end
local keys_run_normal =
key_state_obj.key_run_normal_if_state_was_not_used and
key_state_obj.cnt_other_pressed == 0
for ii_key_name, ii_key_obj in pairs(key_state_obj.keys)
-- clean the state from all keys
do ii_key_obj.states[key_state_id] = nil
if keys_run_normal
then tbl_ext_key_lev_hist[#tbl_ext_key_lev_hist].reset_hld_stt = ii_key_name
mf_postmacro(Keys, ii_key_obj.key_name)
end
end
key_state_obj.is_closed = dt_now
key_state_obj.is_open = false
tbl_key_states[key_state_id] = nil
return key_state_obj, key_state_id
end
local function fnc_is_holding(key_inp_rec) return vkc_no_repeat == key_inp_rec.VirtualKeyCode end
Event { description = "MacroEx helper",
group = "ConsoleInput",
action = function(key_inp_rec, p1, p2)
local j_rec = fnc_con_inp_journal(key_inp_rec)
local dt_now = Far.UpTime
local vkc, KeyDown, tbl_key_off
local evt_type = key_inp_rec.EventType
if evt_type == KEY_EVENT
then
vkc = key_inp_rec.VirtualKeyCode
KeyDown = key_inp_rec.KeyDown
if KeyDown
then Xer0X.cnt_inp_key_press = (Xer0X.cnt_inp_key_press or 0) + 1
end
local InpRecName = far_InputRecordToName(fnc_tbl_copy(key_inp_rec, { KeyDown = true }, true))
if InpRecName
then j_rec.InpRecName = InpRecName
local InpRecName_low = str_lower(InpRecName)
local key_is_on, key_toggle = win_GetKeyState(vkc)
for ii_key_name, ii_key_obj in pairs(tbl_hld_keys)
do if ii_key_obj.key_vcd == vkc
then ii_key_obj.key_is_on = key_is_on
ii_key_obj.key_toggle = key_toggle
if KeyDown
then
ii_key_obj.cnt_state_pressed = (ii_key_obj.cnt_state_pressed or 0) + 1
ii_key_obj.dt_state_pressed = dt_now
if ii_key_obj.eat_press_repeat
then j_rec.suppress = true
end
else -- KeyUp!
tbl_key_off = { [ii_key_obj.key_name_low] = ii_key_obj }
if ii_key_obj.key_name_low ~= InpRecName_low
then ii_key_obj.key_name_off = InpRecName
end
end
else
if KeyDown
then ii_key_obj.cnt_other_pressed = (ii_key_obj.cnt_other_pressed or 0) + 1
ii_key_obj.dt_other_pressed = dt_now
end
end
end
for ii_stt_id, ii_stt_obj in pairs(tbl_key_states)
do for jj_stt_key_name, jj_stt_key_obj in pairs(ii_stt_obj.keys)
do local key_belongs_to_state
if jj_stt_key_obj.key_vcd == vkc
then key_belongs_to_state = true
ii_stt_obj.cnt_state_pressed = (ii_stt_obj.cnt_state_pressed or 0) + 1
break
end
if not key_belongs_to_state
then ii_stt_obj.cnt_other_pressed = (ii_stt_obj.cnt_other_pressed or 0) + 1
end
end
end
end
local last_hist = tbl_ext_key_lev_hist[#tbl_ext_key_lev_hist]
local last_hist_key = last_hist and last_hist[#last_hist]
if vkc
and last_hist_key
and not last_hist.reset_hld_stt
and ( last_hist_key.ext_type == "HLD"
or last_hist_key.ext_type == "STT")
and last_hist.run_mode == "ext-seq-run"
then if last_hist_key.key_rec.VirtualKeyCode == vkc
then j_rec.suppress = true
else key_inp_rec.KeyTime = Far.UpTime
key_inp_rec.KeyExitDeltaTime = last_hist.exit_time and key_inp_rec.KeyTime - last_hist.exit_time
key_inp_rec.KeyExecDeltaTime = last_hist.exec_time and key_inp_rec.KeyTime - last_hist.exec_time
last_hist.reset_hld_stt = key_inp_rec
end
end
if vkc_no_repeat
and vkc ~= 0
then -- KEY_NONE? inp_rec.VirtualKeyCode == 0
-- prevent senseless repeating the same key
if vkc == vkc_no_repeat
and key_inp_rec.KeyDown
then return 1 -- eat repetitions
else vkc_no_repeat = false
end
end
end
if dt_now - rlsd_keys_chck_last > RLSD_KEYS_CHCK_TICK
or tbl_key_off -- due to KeyUp event above
then rlsd_keys_chck_last = dt_now
if not next(tbl_hld_keys) then goto after_key_off_chk end
for ii_key_name, ii_key_obj in pairs(tbl_hld_keys)
do if not (tbl_key_off -- again, due to KeyUp event above
and tbl_key_off[ii_key_name])
and ii_key_obj.key_vcd
then ii_key_obj.key_is_on, ii_key_obj.key_toggle = win_GetKeyState(ii_key_obj.key_vcd)
if not ii_key_obj.key_is_on
then if not tbl_key_off then tbl_key_off = { } end
tbl_key_off[ii_key_name] = ii_key_obj
else -- same as for "if KeyDown"
ii_key_obj.cnt_state_checked = (ii_key_obj.cnt_state_checked or 0) + 1
ii_key_obj.dt_state_checked = dt_now
if ii_key_obj.states
then for jj_stt_id, jj_stt_obj in pairs(ii_key_obj.states)
do jj_stt_obj.dt_state_checked = dt_now
jj_stt_obj.cnt_state_checked = (jj_stt_obj.cnt_state_checked or 0) + 1
if jj_stt_obj.fnc_state_repeat
and(not jj_stt_obj.tick_state_repeat
or jj_stt_obj.tick_state_repeat < dt_now - (ii_key_obj.dt_state_repeat or 0))
then jj_stt_obj:fnc_state_repeat(dt_now)
jj_stt_obj.dt_state_repeat = dt_now
end
end
end
end
else
end
end
if not tbl_key_off then goto after_key_off_chk end
local tbl_stt_off = { }
for ii_key_name, ii_key_obj in pairs(tbl_key_off)
do ii_key_obj.is_closed = dt_now
ii_key_obj.is_open = false
tbl_hld_keys[ii_key_name] = nil
if ii_key_obj.states
then for ii_state_id, ii_state_obj in pairs(ii_key_obj.states)
do tbl_stt_off[ii_state_id] = ii_state_obj
end
end
end
for ii_state_id, ii_state_obj in pairs(tbl_stt_off)
do fnc_key_state_run_remove(ii_state_id, dt_now)
end
::after_key_off_chk::
end
return j_rec.suppress
end
}
-- State after last mf.waitkey!!
local function fnc_is_cas_mod_rls(key_rec)
local mouse_cas = band(Mouse.LastCtrlState, MOD_MASK)
local input_cas = band(key_rec.ControlKeyState, MOD_MASK)
return 0 == band(mouse_cas, input_cas)
and ( 0 ~= mouse_cas
or 0 ~= input_cas
),
mouse_cas
end
local function fnc_macro_collect(tbl_keys) -- FIN/ALL/SCP
local tbl_res = { }
for ii_key_full, ii_obj_coll in pairs(tbl_keys)
do for jj_mcr_id, jj_mcr_obj in pairs(ii_obj_coll.scripts)
do local jj_mcr_item = tbl_res[jj_mcr_id]
if jj_mcr_item
then jj_mcr_item.keys_full[ii_key_full] = ii_obj_coll.key_path
else jj_mcr_item = {
keys_full = {[ii_key_full] = ii_obj_coll.key_path },
mcr_extid = jj_mcr_id,
obj_macro = jj_mcr_obj,
}
tbl_res[jj_mcr_id] = jj_mcr_item
end
end
end
return tbl_res
end
local rgx_key_parse_v2 = regex.new("/^(l?ctrl)?(rctrl)?(l?alt)?(ralt)?(shift)?(.+)?$/i")
local function fnc_key_parse(the_key, case_sens)
if not the_key then return end
local key_cache = str_format("%s|%s", the_key, tostring(case_sens))
local obj_parsed = tbl_parsed_keys_val_cache[key_cache]
if obj_parsed
then return obj_parsed
end
if tbl_parsed_keys_bad_cache[key_cache]
then return false
end
local the_key_low = case_sens and the_key or str_lower(the_key)
local t_rec_1 = { }
t_rec_1.ctl_l, t_rec_1.ctl_r, t_rec_1.alt_l, t_rec_1.alt_r, t_rec_1.shift, t_rec_1.plain = rgx_key_parse_v2:match(the_key_low)
local t_rec_2 = { }
if not t_rec_1.plain then goto after_validity_check end
t_rec_2.ctl_l, t_rec_2.ctl_r, t_rec_2.alt_l, t_rec_2.alt_r, t_rec_2.shift, t_rec_2.plain = rgx_key_parse_v2:match(t_rec_1.plain)
if t_rec_2.ctl_l
or t_rec_2.ctl_r
or t_rec_2.alt_l
or t_rec_2.alt_r
or t_rec_2.shift
then tbl_parsed_keys_bad_cache[key_cache] = { t_rec_1, t_rec_2 }
return false
end
::after_validity_check::
t_rec_1.ctl_x = not t_rec_1.ctl_r and t_rec_1.ctl_l and str_lower(t_rec_1.ctl_l) == "ctrl"
t_rec_1.alt_x = not t_rec_1.alt_r and t_rec_1.alt_l and str_lower(t_rec_1.alt_l) == "alt"
t_rec_1.ctl_e = t_rec_1.ctl_r or t_rec_1.ctl_l
t_rec_1.alt_e = t_rec_1.alt_r or t_rec_1.alt_l
tbl_parsed_keys_val_cache[key_cache] = t_rec_1
return t_rec_1
end -- fnc_key_parse
local function fnc_key_expand(the_key, in_lower, as_single , obj_rgx)
-- assert(key:match("^%l+$")) is_regex
if not the_key then return end
local key_cache = str_format("%s|%s|%s", the_key, tostring(in_lower), tostring(as_single))
local obj_cache = tbl_key_expanded_cache[key_cache]
if obj_cache then return unpack(tbl_key_expanded_cache[key_cache]) end
local R = in_lower and "r" or "R"
local L = in_lower and "l" or "L"
local ALT = in_lower and "alt" or "Alt"
local CTL = in_lower and "ctrl" or "Ctrl"
local obj_key = obj_rgx
and { regex = the_key, reobj = obj_rgx }
or fnc_key_parse(the_key, not in_lower)
local tbl_exp = { }
local ctl, alt
ctl = obj_key.ctl_x and L
repeat
alt = obj_key.alt_x and L
repeat
local tbl_key = {
ctl_l = not ctl and not not obj_key.ctl_l or ctl == L,
ctl_r = not ctl and not not obj_key.ctl_r or ctl == R,
alt_l = not alt and not not obj_key.alt_l or alt == L,
alt_r = not alt and not not obj_key.alt_r or alt == R,
shift = obj_key.shift,
plain = obj_key.plain,
regex = obj_key.regex,
reobj = obj_key.reobj,
}
tbl_key.cas = (ctl and ctl..CTL or (obj_key.ctl_l and L..CTL or "")..(obj_key.ctl_r and R..CTL or ""))..
(alt and alt..ALT or (obj_key.alt_l and L..ALT or "")..(obj_key.alt_r and R..ALT or ""))..
(obj_key.shift or "")
tbl_key.rec_cas=(ctl and (ctl == R and R or "")..CTL or (obj_key.ctl_l and L..CTL or "")..(obj_key.ctl_r and R..CTL or ""))..
(alt and (alt == R and R or "")..ALT or (obj_key.alt_l and L..ALT or "")..(obj_key.alt_r and R..ALT or ""))..
(obj_key.shift or "")
tbl_key.str = tbl_key.cas ..(obj_key.plain or obj_key.regex or "")
tbl_key.rec = tbl_key.rec_cas ..(obj_key.plain or "")
tbl_exp[tbl_key.str] = tbl_key
alt = not as_single and alt == L and R
until not alt
ctl = not as_single and ctl == L and R
until not ctl
tbl_key_expanded_cache[key_cache] = { tbl_exp, obj_key }
return tbl_exp, obj_key
end -- fnc_key_expand
local function fnc_key_equal(key_base, key_comp, case_sens)
if not key_base
or not key_comp
or key_base == ""
or key_comp == ""
then return
end
local tbl_base = type(key_base) == "table" and key_base or fnc_key_expand(key_base, not case_sens, false)
local tbl_comp = type(key_comp) == "table" and key_comp or fnc_key_expand(key_comp, not case_sens, false)
for ii_base, ii_base_val in pairs(tbl_base) do
for ii_comp, ii_comp_val in pairs(tbl_comp) do
if str_lower(ii_base)
== str_lower(ii_comp)
then return ii_comp:lower(), ii_comp_val
elseif ii_comp_val.reobj
and ii_comp_val.reobj:match(ii_base) ~= ""
then return ii_comp, ii_comp_val
end
end
end
return false
end -- fnc_key_equal
local function fnc_copy_key_ext_val(tbl_from, tbl_from_loc, tbl_dest, tbl_dest_loc, copy_key_idx, copy_key_val)
if type(copy_key_idx) == "number"
and copy_key_idx <= #tbl_from_loc
then tbl_dest_loc[#tbl_dest_loc + 1] = copy_key_val
else tbl_dest_loc[copy_key_idx] = copy_key_val
end
return true
end
local function fnc_copy_key_ext_tbl(tbl_from, tbl_from_loc, tbl_dest, tbl_dest_loc, copy_key_idx, copy_key_val)
if copy_key_val.action
and copy_key_val.key
and copy_key_val.area
then return fnc_copy_key_ext_val(tbl_from, tbl_from_loc, tbl_dest, tbl_dest_loc, copy_key_idx, copy_key_val)
elseif copy_key_idx == "KXP"
or copy_key_idx == "key_path"
then if not tbl_dest_loc[copy_key_idx]
then tbl_dest_loc[copy_key_idx] = copy_key_val
end
return true
end
end
local function fnc_get_ext_chk(obj_key_lev, key_str_new, t_wait_key)
local tbl_ext_allowed = {
-- holding is considered only after exceeding OPT_DELAY_EXT time frame
["HLD"] = not obj_key_lev.ext_type and key_str_new == "" and fnc_is_holding(obj_key_lev.key_rec) and ((t_wait_key - obj_key_lev.key_time) > OPT_LIMIT_HLD and 2 or 1) or 0,
["DBL"] = not obj_key_lev.ext_type and (t_wait_key - obj_key_lev.key_time) < OPT_LIMIT_DBL and (key_str_new ~= "" and (fnc_key_equal(key_str_new, obj_key_lev.key_str_bas) and 2 or 0) or 1) or 0,
["STT"] = not obj_key_lev.ext_type and key_str_new == "" and vkc_no_repeat and win_GetKeyState(vkc_no_repeat) and 2 or 0,
["MNY"] = (obj_key_lev.ext_type or "MNY") == "MNY" and (key_str_new ~= "" and 2 or 1) or 0,
["NON"] = key_str_new == "" and 2 or 0,
["SEQ"] = key_str_new ~= "" and 2 or 1,
}
local tbl_ext = { }
local cur_ext_type = obj_key_lev.ext_type or "NON"
for ii_mcr_key_cur_full, ii_mcr_key_cur_coll in pairs(obj_key_lev.mcr_key_cur)
do for ii_mcr_key_cur_idx, ii_mcr_key_cur_obj in ipairs(ii_mcr_key_cur_coll)
do
local ii_mcr_key_cur_ext = ii_mcr_key_cur_obj.key_path_cur.ext or "NON"
if ii_mcr_key_cur_ext == cur_ext_type
and ii_mcr_key_cur_ext ~= "MNY"
and ii_mcr_key_cur_obj.key_path_len >
ii_mcr_key_cur_obj.key_path_pos
then ii_mcr_key_cur_ext = "SEQ"
end
if tbl_ext_allowed[ii_mcr_key_cur_ext] == 0
then goto loop_end
elseif tbl_ext_allowed[ii_mcr_key_cur_ext] == 1
then ii_mcr_key_cur_ext = "PRE"
elseif tbl_ext_allowed[ii_mcr_key_cur_ext] == 2
then -- is eligible
else -- just can not be
end
::again_for_seq_after_many::
if not tbl_ext[ii_mcr_key_cur_ext]
then tbl_ext[ii_mcr_key_cur_ext] = { count = 1 }
else tbl_ext[ii_mcr_key_cur_ext].count =
tbl_ext[ii_mcr_key_cur_ext].count + 1
end
if not tbl_ext[ii_mcr_key_cur_ext][ii_mcr_key_cur_obj.key_full]
then tbl_ext[ii_mcr_key_cur_ext][ii_mcr_key_cur_obj.key_full] = { count = 1 }
else tbl_ext[ii_mcr_key_cur_ext][ii_mcr_key_cur_obj.key_full].count =
tbl_ext[ii_mcr_key_cur_ext][ii_mcr_key_cur_obj.key_full].count + 1
end
tbl_ins(tbl_ext[ii_mcr_key_cur_ext][ii_mcr_key_cur_obj.key_full], ii_mcr_key_cur_obj)
if ii_mcr_key_cur_ext == "MNY"
and ii_mcr_key_cur_obj.key_path_len >
ii_mcr_key_cur_obj.key_path_pos
then ii_mcr_key_cur_ext = "SEQ"
goto again_for_seq_after_many
end
::loop_end::
end
end
local str_ext = tbl_ext.STT and "STT" -- potentially every keys can be a state key, highest priority
or tbl_ext.HLD and "HLD"
or tbl_ext.DBL and "DBL"
or tbl_ext.MNY and "MNY"
or tbl_ext.SEQ and "SEQ"
return str_ext, tbl_ext, tbl_ext_allowed
end -- fnc_get_ext_chk
local function fnc_get_fin_scp(key_scr, prio_cond, tbl_key_lev, final_only)
local tbl_mcr_coll = fnc_macro_collect(key_scr or { })
local tbl_mcr_help = { }
local tbl_res_equal = { }
local tbl_res_final = { }
local tbl_mcr_key_cur = { }
for ii_mcr_id, ii_mcr_item in pairs(tbl_mcr_coll)
do for jj_key_full, jj_key_path in pairs(ii_mcr_item.keys_full)
do if prio_cond[ii_mcr_id]
and prio_cond[ii_mcr_id][jj_key_full]
then if not final_only
then local is_equal, is_final, in_many
local jj_key_pos = 1
for kk_seq_pos, kk_seq_key in ipairs(tbl_key_lev)
do ::loop_code_start::
is_many = jj_key_path[jj_key_pos].ext == "MNY" and (kk_seq_key.ext_type or "MNY") == "MNY"
is_equal = fnc_key_equal(
is_many and kk_seq_key.key_str_bas or kk_seq_key.key_exp,
jj_key_path[jj_key_pos].exp,
is_many -- lets suppose MNY is always regexp-based and thus case-sensitive
) and (
is_many -- multiple expected, allow it
or kk_seq_pos == #tbl_key_lev -- the last key in the sequence,
and not kk_seq_key.ext_type -- .. and still no extension determined in the last.
or kk_seq_key.ext_type == jj_key_path[jj_key_pos].ext -- good obviously
)
if is_equal
then if jj_key_pos == #jj_key_path
and kk_seq_pos == #tbl_key_lev
then is_final = kk_seq_key.ext_type == jj_key_path[jj_key_pos].ext
elseif kk_seq_pos < #tbl_key_lev
and not is_many
then jj_key_pos = jj_key_pos + 1
end
else -- is not equal, exit keys path
if not is_many
or jj_key_pos == #jj_key_path
then break
elseif is_many
then jj_key_pos = jj_key_pos + 1
goto loop_code_start
end
end
end
if is_equal
then tbl_res_equal[#tbl_res_equal + 1] = ii_mcr_item
if not is_final
or jj_key_path[jj_key_pos].ext == "MNY"
then tbl_mcr_help[#tbl_mcr_help + 1] = fnc_key_prettify(jj_key_full).." - "..ii_mcr_item.obj_macro.description
end
if not tbl_mcr_key_cur[jj_key_full]
then tbl_mcr_key_cur[jj_key_full] = { count = 1 }
else tbl_mcr_key_cur[jj_key_full].count = tbl_mcr_key_cur[jj_key_full].count + 1
end
tbl_ins(tbl_mcr_key_cur[jj_key_full], {
is_final = is_final,
key_path_pos = jj_key_pos,
key_path_cur = jj_key_path[jj_key_pos],
key_path_len =#jj_key_path,
key_full = jj_key_full,
mcr_item = ii_mcr_item,
mcr_extid = ii_mcr_item.mcr_extid,
})
end
if is_final -- the logic means that it is also equal automatically
then tbl_res_final[#tbl_res_final + 1] = ii_mcr_item
end
end
end
end
end
local hlp_str = next(tbl_mcr_help) and tbl_concat(tbl_mcr_help, "\n")..MACRO_HELP_FOOTER or false
return tbl_res_equal, tbl_res_final, tbl_mcr_key_cur, hlp_str
end -- fnc_get_fin_scp
local function GetMacro(argKey, argUseCommon)
local area_name = str_low_a(Area.Current)
local Area_Names = { area_name, argUseCommon and area_name ~= "common" and "common" or nil }
local key = str_low_a(argKey)
do
local from, to, ctrl, alt, start
do from, to, ctrl = str_find(key, "^(r?ctrl)")
end
if ctrl == "ctrl"
then ctrl = "lctrl"
elseif ctrl ~="rctrl"
then from, to, ctrl = str_find(key, "^(l?ctrl)")
end
start = to and to + 1 or 1
do from, to, alt = str_find(key, "^(r?alt)", start)
end
if alt == "alt"
then alt = "lalt"
elseif alt ~="ralt"
then from, to, alt = str_find(key, "^(l?alt)", start)
end
start = to and to + 1 or start
key = (ctrl or "")..(alt or "")..str_sub(key, start)
end
local Collector, CInfo = { }, { }
local filename = false
or area_name == "editor" and editor.GetFileName()
or area_name == "viewer" and viewer.GetFileName()
or area_name == "shell" and APanel.Path.."\\"..APanel.Current
local function ExamineMacro(m, area_name_test)
local check = not (filename and m.filemask) or far_ProcessName("PN_CMPNAMELIST", m.filemask, filename, "PN_SKIPPATH")
if check and MacroCallFar(MCODE_F_CHECKALL, GetAreaCode(area_name_test), m.flags, m.callback, m.callbackId)
then if not Collector[m]
then local n = #CInfo + 1
Collector[m] = n
CInfo[n] = m.priority or 50
CInfo[n + 1] = area_name_test
end
end
end -- ExamineMacro
for _, ii_areaname in ipairs(Area_Names)
do local ii_areatable = utils_Areas[ii_areaname]
if ii_areatable
then local ii_macros = ii_areatable[key]
if ii_macros
then for _, jj_m in ipairs(ii_macros)
do
if jj_m.data.key_state_active_prevents_new
and jj_m.data.key_states
then for ii_key_stt_id, ii_key_stt_obj in pairs(jj_m.data.key_states)
do if ii_key_stt_obj.is_open
then goto loop_cycle_end
end
end
end
if not jj_m.disabled
and jj_m.data.areas_dict[ii_areaname]
and not(ii_areaname == "common"
and jj_m.data.areas_dict[area_name])
then ExamineMacro(jj_m, ii_areaname)
end
::loop_cycle_end::
end
end
end
end
local max_priority = -1
local nummacros = 0
for ii_m, ii_p in pairs(Collector)
do if ii_m.condition
then local ii_pr = ii_m.condition(key, ii_m.data) -- unprotected call
if ii_pr
then if type(ii_pr) == "number"
then CInfo[ii_p] = ii_pr > 100 and 100 or ii_pr < 0 and 0 or ii_pr
end
else Collector[ii_m] = nil
end
end
if Collector[ii_m]
then nummacros = nummacros + 1
if max_priority < CInfo[ii_p]
then max_priority = CInfo[ii_p]
end
end
end
local macrolist = { }
local nindex = nil
for ii_m, ii_p in pairs(Collector)
do macrolist[#macrolist + 1] = { macro = ii_m, priority = CInfo[ii_p], sortpriority = ii_m.sortpriority }
if CInfo[ii_p] == max_priority then nindex = nindex and -1 or #macrolist end
end
return macrolist, nindex, Collector, CInfo
end -- GetMacro
local function fnc_mcr_key_check(key_scr)
local tbl_mcr_coll = fnc_macro_collect(key_scr)
local tbl_mcr_key_ok = { }
local macrolist = { }
for ii_mcr_idx, ii_mcr_item in pairs(tbl_mcr_coll)
do for jj_key_full, jj_key_path in pairs(ii_mcr_item.keys_full)
do
local jj_mcr_list, mcr_hi_idx = GetMacro(jj_key_full, true)
for jj_mcr_idx, jj_mcr_obj in pairs(jj_mcr_list)
do
local jj_mcr_itm = tbl_mcr_key_ok[jj_mcr_obj.macro.data.id_ext]
if not jj_mcr_itm
then jj_mcr_itm = { }
macrolist[#macrolist + 1] = jj_mcr_obj
end
if not jj_mcr_itm[jj_key_full]
then jj_mcr_itm[jj_key_full] = {
priority = jj_mcr_obj.priority,
is_highest = jj_mcr_idx == mcr_hi_idx
}
end
jj_mcr_itm.highest = mcr_def_idx
tbl_mcr_key_ok[jj_mcr_obj.macro.data.id_ext] = jj_mcr_itm
end
end
end
tbl_sort(macrolist, function(m1, m2)
local m1_prio = m1.priority or 0
local m2_prio = m2.priority or 0
local m1_sort = m1.sortpriority or 0
local m2_sort = m2.sortpriority or 0
if m1_prio > m2_prio
then return true
elseif m1_prio == m2_prio
then return m1_sort > m2_sort
else return false
end
end)
return tbl_mcr_key_ok, macrolist
end -- fnc_mcr_key_check
local obj_waitkeys = {
keys = { },
init = function(self, keys)
local type_keys = type(keys)
self.keys= type_keys == "nil" and { }
or type_keys == "table" and keys
or { keys } -- should be string
self.no_key = nil
end,
save = function(self, ...)
local the_key = mf_waitkey(...)
local has_key = the_key ~= ""
local isNewOn = self.no_key and has_key
self.no_key = not has_key
if the_key ~= ""
then tbl_insert(self.keys, the_key)
end
return the_key, Far.UpTime
end,
last_rem = function(self)
self.keys[#self.keys] = nil
end
}
local function fnc_sett_data_store()
local res_val, file_hnd = dmp_srlz.fnc_file_save(tbl_normal_ask, { file_path = own_fpath..".dat", file_init = true, file_close = true })
end
local function fnc_cas_int_to_str(cas_mod, empty_str)
local ret_val = ""
..(band(cas_mod, LEFT_CTRL_PRESSED ) > 0 and "LCtrl" or "")
..(band(cas_mod, RIGHT_CTRL_PRESSED ) > 0 and "RCtrl" or "")
..(band(cas_mod, LEFT_ALT_PRESSED ) > 0 and "LAlt" or "")
..(band(cas_mod, RIGHT_ALT_PRESSED ) > 0 and "RAlt" or "")
..(band(cas_mod, SHIFT_PRESSED ) > 0 and "Shift" or "")
if ret_val == ""
and empty_str
then return empty_str
else return ret_val
end
end -- fnc_cas_int_to_str
Xer0X.fnc_cas_int_to_str = fnc_cas_int_to_str
local function runMacroOrKey(the_key)
local eval_res = eval(the_key, 2)
if eval_res == EVAL_MACRONOTFOUND
then Keys(the_key)
end
return eval_res
end
local function fnc_eat_hld_stt_rep_outer(rec_key)
local last_hist = tbl_ext_key_lev_hist[#tbl_ext_key_lev_hist]
local last_hist_key = last_hist and last_hist[#last_hist]
if last_hist_key
and not last_hist.reset_hld_stt
and last_hist.run_mode == "ext-seq-run"
and ( last_hist_key.ext_type == "HLD"
or last_hist_key.ext_type == "STT" )
and last_hist_key.key_str_act == rec_key
then return true
end
end
Xer0X.fnc_eat_hld_stt_rep_outer = fnc_eat_hld_stt_rep_outer
local function fnc_key_state_add(state_key, key_state_id, eat_press_repeat, tick_state_repeat, fnc_state_init, fnc_state_repeat, fnc_state_final, key_run_normal_if_state_was_not_used)
local dt_now = Far.UpTime
local state_key_low = state_key:lower()
if not key_state_id
then key_state_id = win_uuid(win_uuid())
end
local obj_hld_key = tbl_hld_keys[state_key_low]
local obj_key_state = tbl_key_states[key_state_id]
if not obj_key_state
then obj_key_state = {
key_state_id = key_state_id,
is_open = dt_now,
keys = { },
obj_self = obj_hld_key,
fnc_state_init = fnc_state_init,
eat_press_repeat = eat_press_repeat,
tick_state_repeat = tick_state_repeat,
fnc_state_repeat = fnc_state_repeat,
fnc_state_final = fnc_state_final,
key_run_normal_if_state_was_not_used = key_run_normal_if_state_was_not_used,
cnt_inp_key_press_start = Xer0X.cnt_inp_key_press,
cnt_other_pressed = 0,
}
tbl_key_states[key_state_id] = obj_key_state
end
if obj_hld_key
then
obj_hld_key.cnt_state_pressed = obj_hld_key.cnt_state_pressed + 1
obj_hld_key.dt_pressed = dt_now
for ii_key in pairs(obj_hld_key.states)
do if ii_key ~= key_state_id
then obj_hld_key.states[ii_key] = nil
end
end
if not obj_hld_key.states[key_state_id]
then obj_hld_key.states[key_state_id] = obj_key_state
end
else
local key_rec = fnc_NameToInputRecord(state_key_low)
obj_hld_key = {
dt_state_created = dt_now,
dt_state_pressed = dt_now,
cnt_state_pressed= 1,
cnt_state_checked= 0,
cnt_inp_key_press_start = Xer0X.cnt_inp_key_press,
area_init = Area.Current,