From 8a04edfa09a72265746b1df42c1eb9567050cab6 Mon Sep 17 00:00:00 2001 From: johndoe Date: Fri, 29 May 2020 19:07:43 +0300 Subject: [PATCH] luamacro: implement custom events (POC) define event handler: ```lua Event { description="test!"; group="CustomEvent"; action=function(Event,...) far.Show(Event,...) end; } ``` emit event: ```lua mf.ProcessEvent("CustomEvent",{},1,2,3) ``` --- plugins/luamacro/api.lua | 2 ++ plugins/luamacro/utils.lua | 20 +++++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/plugins/luamacro/api.lua b/plugins/luamacro/api.lua index 1256c79e960..61611faae07 100644 --- a/plugins/luamacro/api.lua +++ b/plugins/luamacro/api.lua @@ -92,6 +92,8 @@ mf.usermenu = function(mode, filename) end mf.GetMacroCopy = utils.GetMacroCopy + +mf.ProcessEvent = utils.ProcessEvent -------------------------------------------------------------------------------- Object = { diff --git a/plugins/luamacro/utils.lua b/plugins/luamacro/utils.lua index 12084358156..accefd0df98 100644 --- a/plugins/luamacro/utils.lua +++ b/plugins/luamacro/utils.lua @@ -183,6 +183,7 @@ local function AddId (trg, src) end local function EV_Handler (macros, filename, ...) + if not macros then return end -- Get current priorities. local indexes,priorities = {},{} for i,m in ipairs(macros) do @@ -293,6 +294,10 @@ local function export_GetContentData (filename, colnames) return tOut end +local function ProcessEvent (group, options, ...) --todo + return EV_Handler(Events[group:lower()], options and options.filename, options, ...) +end + local ExpandKey do -- измеренное время исполнения на ключе "CtrlAltShiftF12" = ??? (Lua); 2.3uS (LuaJIT); local t={} @@ -479,7 +484,8 @@ end local AddEvent_fields = {"group","action","description","priority","condition","filemask"} local function AddEvent (srctable, FileName) local group = type(srctable)=="table" and type(srctable.group)=="string" and srctable.group:lower() - if not (group and Events[group]) then return end + if not group then return end + Events[group] = Events[group] or {} if type(srctable.action)~="function" then return end @@ -710,7 +716,6 @@ local function LoadMacros (unload, paths) local AreaNames = allAreas and AllAreaNames or SomeAreaNames for _,name in pairs(AreaNames) do newAreas[name]={} end - for _,name in ipairs(EventGroups) do Events[name]={} end for k in pairs(package.loaded) do if initial_modules[k]==nil and not package.nounload[k] then package.loaded[k]=nil @@ -851,11 +856,11 @@ local function LoadMacros (unload, paths) far.RecursiveSearch (DirMacros.."internal", "*.lua", LoadRecordedFile, 0) - export.ExitFAR = Events.exitfar[1] and export_ExitFAR - export.ProcessDialogEvent = Events.dialogevent[1] and export_ProcessDialogEvent - export.ProcessEditorInput = Events.editorinput[1] and export_ProcessEditorInput - export.ProcessViewerEvent = Events.viewerevent[1] and export_ProcessViewerEvent - export.ProcessConsoleInput = Events.consoleinput[1] and export_ProcessConsoleInput + export.ExitFAR = Events.exitfar and export_ExitFAR + export.ProcessDialogEvent = Events.dialogevent and export_ProcessDialogEvent + export.ProcessEditorInput = Events.editorinput and export_ProcessEditorInput + export.ProcessViewerEvent = Events.viewerevent and export_ProcessViewerEvent + export.ProcessConsoleInput = Events.consoleinput and export_ProcessConsoleInput if ContentColumns[1] then export.GetContentFields = export_GetContentFields export.GetContentData = export_GetContentData @@ -1245,6 +1250,7 @@ return { InitMacroSystem = InitMacroSystem, LoadingInProgress = function() return LoadingInProgress end, LoadMacros = LoadMacros, + ProcessEvent = ProcessEvent, ProcessRecordedMacro = ProcessRecordedMacro, RunStartMacro = RunStartMacro, UnloadMacros = InitMacroSystem,