Skip to content

Commit

Permalink
refactor: alternative way to fix bidirectional mappings (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam-programs authored Jul 8, 2024
1 parent 769c1e2 commit 5e20575
Showing 1 changed file with 37 additions and 36 deletions.
73 changes: 37 additions & 36 deletions lua/better_escape.lua
Original file line number Diff line number Diff line change
Expand Up @@ -88,47 +88,48 @@ local function map_keys()
parent_keys = {}
for mode, keys in pairs(settings.mappings) do
local map_opts = { expr = true }
for key, _ in pairs(keys) do
vim.keymap.set(mode, key, function()
log_key(key)
return key
end, map_opts)
end
for key, subkeys in pairs(keys) do
-- Ensure correct registration of bidirectional mappings (e.g., `kj` and `jk`). #67
if vim.fn.maparg(key, mode) == "" then
vim.keymap.set(mode, key, function()
log_key(key)
return key
end, map_opts)
end
for subkey, mapping in pairs(subkeys) do
if mapping then
if not parent_keys[mode] then
parent_keys[mode] = {}
if not mapping then
goto continue
end
if not parent_keys[mode] then
parent_keys[mode] = {}
end
if not parent_keys[mode][subkey] then
parent_keys[mode][subkey] = {}
end
parent_keys[mode][subkey][key] = true
vim.keymap.set(mode, subkey, function()
-- In case the subkey happens to also be a starting key
if last_key == nil then
log_key(subkey)
return subkey
end
if not parent_keys[mode][subkey] then
parent_keys[mode][subkey] = {}
-- Make sure we are in the correct sequence
if not parent_keys[mode][subkey][last_key] then
log_key(key)
return subkey
end
parent_keys[mode][subkey][key] = true
vim.keymap.set(mode, subkey, function()
-- In case the subkey happens to also be a starting key
if last_key == nil then
log_key(subkey)
return subkey
end
-- Make sure we are in the correct sequence
if not parent_keys[mode][subkey][last_key] then
log_key(key)
return subkey
end
vim.api.nvim_input(undo_key[mode] or "")
vim.api.nvim_input(
("<cmd>setlocal %smodified<cr>"):format(
bufmodified and "" or "no"
)
vim.api.nvim_input(undo_key[mode] or "")
vim.api.nvim_input(
("<cmd>setlocal %smodified<cr>"):format(
bufmodified and "" or "no"
)
if type(mapping) == "string" then
vim.api.nvim_input(mapping)
elseif type(mapping) == "function" then
vim.api.nvim_input(mapping() or "")
end
end, map_opts)
end
)
if type(mapping) == "string" then
vim.api.nvim_input(mapping)
elseif type(mapping) == "function" then
vim.api.nvim_input(mapping() or "")
end
end, map_opts)
::continue::
end
end
end
Expand Down

0 comments on commit 5e20575

Please sign in to comment.