2020-08-27 16:10:01 +00:00
|
|
|
-- Imports
|
2020-05-21 19:49:53 +00:00
|
|
|
local api = vim.api
|
2020-05-08 17:18:01 +00:00
|
|
|
local libmodal = require('libmodal')
|
|
|
|
|
2020-08-27 16:10:01 +00:00
|
|
|
-- Keep track of the user's input history manually.
|
2020-05-21 19:49:53 +00:00
|
|
|
local _inputHistory = {}
|
|
|
|
|
2020-08-27 16:10:01 +00:00
|
|
|
-- Clear the input history if it grows too long for our usage.
|
2020-05-21 19:49:53 +00:00
|
|
|
function _inputHistory:clear(indexToCheck)
|
|
|
|
if #self >= indexToCheck then
|
|
|
|
for i, _ in ipairs(self) do
|
|
|
|
self[i] = nil
|
|
|
|
end
|
2020-05-08 17:18:01 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-27 16:10:01 +00:00
|
|
|
-- This is the function that will be called whenever the user presses a button.
|
2020-07-03 17:01:04 +00:00
|
|
|
local function fooMode()
|
2020-08-27 16:10:01 +00:00
|
|
|
-- Append to the input history, the latest button press.
|
2020-05-21 19:52:14 +00:00
|
|
|
_inputHistory[#_inputHistory + 1] = string.char(
|
2020-08-27 16:10:01 +00:00
|
|
|
-- The input is a character number.
|
2020-05-15 02:12:15 +00:00
|
|
|
api.nvim_get_var('fooModeInput')
|
2020-05-13 22:22:25 +00:00
|
|
|
)
|
2020-05-08 17:18:01 +00:00
|
|
|
|
2020-08-27 16:10:01 +00:00
|
|
|
-- Custom logic to test for each character index to see if it matches the 'zfo' mapping.
|
2020-05-08 17:18:01 +00:00
|
|
|
local index = 1
|
2020-05-21 19:52:14 +00:00
|
|
|
if _inputHistory[1] == 'z' then
|
|
|
|
if _inputHistory[2] == 'f' then
|
|
|
|
if _inputHistory[3] == 'o' then
|
2020-05-08 17:18:01 +00:00
|
|
|
api.nvim_command("echom 'It works!'")
|
2020-05-21 19:49:53 +00:00
|
|
|
else index = 3
|
|
|
|
end
|
|
|
|
else index = 2
|
|
|
|
end
|
2020-05-08 17:18:01 +00:00
|
|
|
end
|
|
|
|
|
2020-05-21 19:49:53 +00:00
|
|
|
_inputHistory:clear(index)
|
2020-05-08 17:18:01 +00:00
|
|
|
end
|
|
|
|
|
2020-08-27 16:10:01 +00:00
|
|
|
-- Enter the mode to begin the demo.
|
2020-05-15 02:12:15 +00:00
|
|
|
libmodal.mode.enter('FOO', fooMode)
|