Skip to content

Commit

Permalink
luamacro: implement custom events (POC)
Browse files Browse the repository at this point in the history
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)
```
  • Loading branch information
johndoe committed May 29, 2020
1 parent 7bda1c2 commit 8a04edf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 2 additions & 0 deletions plugins/luamacro/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ mf.usermenu = function(mode, filename)
end

mf.GetMacroCopy = utils.GetMacroCopy

mf.ProcessEvent = utils.ProcessEvent
--------------------------------------------------------------------------------

Object = {
Expand Down
20 changes: 13 additions & 7 deletions plugins/luamacro/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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={}

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1245,6 +1250,7 @@ return {
InitMacroSystem = InitMacroSystem,
LoadingInProgress = function() return LoadingInProgress end,
LoadMacros = LoadMacros,
ProcessEvent = ProcessEvent,
ProcessRecordedMacro = ProcessRecordedMacro,
RunStartMacro = RunStartMacro,
UnloadMacros = InitMacroSystem,
Expand Down

0 comments on commit 8a04edf

Please sign in to comment.