Skip to content

Commit

Permalink
Merge pull request #193 from mrjones2014/mrj/192/stackmap
Browse files Browse the repository at this point in the history
fix(api): Restore previous mappings on exit from resize mode
  • Loading branch information
mrjones2014 authored Apr 29, 2024
2 parents 69721b6 + e95dacc commit 85dc2e7
Showing 1 changed file with 49 additions and 7 deletions.
56 changes: 49 additions & 7 deletions lua/smart-splits/resize-mode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,51 @@ local function wrap(fn)
end
end

M.__map_cache = {}
---Store any existing mappings to be restored after exit
---@param resize_keys string[]
---@param quit_key string
local function cache_mappings(resize_keys, quit_key)
local maps = vim.api.nvim_get_keymap('n')
M.__map_cache = {}
for _, map in ipairs(maps) do
if
map.lhs == resize_keys[1]
or map.lhs == resize_keys[2]
or map.lhs == resize_keys[3]
or map.lhs == resize_keys[4]
or map.lhs == quit_key
then
M.__map_cache[map.lhs] = map
end
end
end

---Restore mappings from cache on exit
local function restore_maps(resize_keys, quit_key)
-- delete the keymaps we've set
vim.api.nvim_del_keymap('n', resize_keys[1])
vim.api.nvim_del_keymap('n', resize_keys[2])
vim.api.nvim_del_keymap('n', resize_keys[3])
vim.api.nvim_del_keymap('n', resize_keys[4])
vim.api.nvim_del_keymap('n', quit_key)

-- restore previous ones, if there are any
for lhs, mapdef in pairs(M.__map_cache) do
local opts = {
buffer = mapdef.buffer,
nowait = mapdef.nowait == 1,
silent = mapdef.silent == 1,
script = mapdef.script ~= 0 and mapdef.script or nil,
expr = mapdef.expr == 1,
remap = mapdef.noremap == 0,
}
local rhs = mapdef.callback or mapdef.rhs
vim.keymap.set('n', lhs, rhs, opts)
end
M.__map_cache = {}
end

function M.start_resize_mode()
if vim.fn.mode() ~= 'n' then
log.error('Resize mode must be triggered from normal mode')
Expand All @@ -21,8 +66,9 @@ function M.start_resize_mode()
-- luacheck:ignore
vim.g.smart_resize_mode = true

local quit_key = config.resize_mode.quit_key
local resize_keys = config.resize_mode.resize_keys
local quit_key = config.resize_mode.quit_key
cache_mappings(resize_keys, quit_key)
vim.keymap.set('n', resize_keys[1], wrap(require('smart-splits').resize_left), { silent = true })
vim.keymap.set('n', resize_keys[2], wrap(require('smart-splits').resize_down), { silent = true })
vim.keymap.set('n', resize_keys[3], wrap(require('smart-splits').resize_up), { silent = true })
Expand All @@ -42,13 +88,9 @@ function M.start_resize_mode()
end

function M.end_resize_mode()
local quit_key = config.resize_mode.quit_key
local resize_keys = config.resize_mode.resize_keys
vim.api.nvim_del_keymap('n', resize_keys[1])
vim.api.nvim_del_keymap('n', resize_keys[2])
vim.api.nvim_del_keymap('n', resize_keys[3])
vim.api.nvim_del_keymap('n', resize_keys[4])
vim.api.nvim_del_keymap('n', quit_key)
local quit_key = config.resize_mode.quit_key
restore_maps(resize_keys, quit_key)

pcall(config.resize_mode.hooks.on_leave)
-- luacheck:ignore
Expand Down

0 comments on commit 85dc2e7

Please sign in to comment.