Jeremy Penner
3d52b70bbc
subrepo: subdir: "vendor/lite" merged: "2783adc" upstream: origin: "https://github.com/jeremypenner/lite.git" branch: "master" commit: "2783adc" git-subrepo: version: "0.4.2" origin: "https://github.com/ingydotnet/git-subrepo" commit: "65fde50"
70 lines
1.4 KiB
Lua
70 lines
1.4 KiB
Lua
local core = require "core"
|
|
local command = require "core.command"
|
|
local keymap = require "core.keymap"
|
|
|
|
local handled_events = {
|
|
["keypressed"] = true,
|
|
["keyreleased"] = true,
|
|
["textinput"] = true,
|
|
}
|
|
|
|
local state = "stopped"
|
|
local event_buffer = {}
|
|
local modkeys = {}
|
|
|
|
local on_event = core.on_event
|
|
|
|
core.on_event = function(type, ...)
|
|
local res = on_event(type, ...)
|
|
if state == "recording" and handled_events[type] then
|
|
table.insert(event_buffer, { type, ... })
|
|
end
|
|
return res
|
|
end
|
|
|
|
|
|
local function clone(t)
|
|
local res = {}
|
|
for k, v in pairs(t) do res[k] = v end
|
|
return res
|
|
end
|
|
|
|
|
|
local function predicate()
|
|
return state ~= "playing"
|
|
end
|
|
|
|
|
|
command.add(predicate, {
|
|
["macro:toggle-record"] = function()
|
|
if state == "stopped" then
|
|
state = "recording"
|
|
event_buffer = {}
|
|
modkeys = clone(keymap.modkeys)
|
|
core.log("Recording macro...")
|
|
else
|
|
state = "stopped"
|
|
core.log("Stopped recording macro (%d events)", #event_buffer)
|
|
end
|
|
end,
|
|
|
|
["macro:play"] = function()
|
|
state = "playing"
|
|
core.log("Playing macro... (%d events)", #event_buffer)
|
|
local mk = keymap.modkeys
|
|
keymap.modkeys = clone(modkeys)
|
|
for _, ev in ipairs(event_buffer) do
|
|
on_event(table.unpack(ev))
|
|
core.root_view:update()
|
|
end
|
|
keymap.modkeys = mk
|
|
state = "stopped"
|
|
end,
|
|
})
|
|
|
|
|
|
keymap.add {
|
|
["ctrl+shift+;"] = "macro:toggle-record",
|
|
["ctrl+;"] = "macro:play",
|
|
}
|