nvim-libmodal/examples/lua/keymaps-manually.lua

40 lines
997 B
Lua
Raw Normal View History

local libmodal = require 'libmodal'
2022-04-26 15:33:37 +00:00
-- keep track of the user's input history manually
local input_history = {}
2020-05-21 19:49:53 +00:00
2022-04-26 15:33:37 +00:00
-- clear the input history if it grows too long for our usage
function input_history:clear(index_to_check)
if #self >= index_to_check then
2020-05-21 19:49:53 +00:00
for i, _ in ipairs(self) do
self[i] = nil
end
end
end
2022-04-26 15:33:37 +00:00
-- this is the function that will be called whenever the user presses a button
local function foo_mode()
2022-04-26 15:33:37 +00:00
-- append to the input history, the latest button press
table.insert(input_history, string.char(
2022-04-26 15:33:37 +00:00
-- the input is a character number
vim.g.fooModeInput
))
2022-04-26 15:33:37 +00:00
-- custom logic to test for each character index to see if it matches the 'zfo' mapping
local index = 1
if input_history[1] == 'z' then
if input_history[2] == 'f' then
if input_history[3] == 'o' then
vim.api.nvim_command "echom 'It works!'"
2020-05-21 19:49:53 +00:00
else index = 3
end
else index = 2
end
end
input_history:clear(index)
end
2022-04-26 15:33:37 +00:00
-- enter the mode to begin the demo
libmodal.mode.enter('FOO', foo_mode)