vim: better window resizing
This commit is contained in:
parent
6108f3bb47
commit
b3f2d03674
2 changed files with 63 additions and 6 deletions
|
@ -1,4 +1,5 @@
|
|||
local partial = require('user.util').partial
|
||||
local util = require('user.util')
|
||||
local partial = util.partial
|
||||
local nmap = partial(vim.keymap.set, 'n')
|
||||
local imap = partial(vim.keymap.set, 'i')
|
||||
local vmap = partial(vim.keymap.set, 'v')
|
||||
|
@ -33,10 +34,10 @@ nmap('<c-h>', '<c-w>h')
|
|||
nmap('<c-l>', '<c-w>l')
|
||||
|
||||
-- window resizing
|
||||
nmap('<s-Up>', '<cmd>resize +1<cr>')
|
||||
nmap('<s-Down>', '<cmd>resize -1<cr>')
|
||||
nmap('<s-Left>', '<cmd>vertical resize -1<cr>')
|
||||
nmap('<s-Right>', '<cmd>vertical resize +1<cr>')
|
||||
nmap('<s-Up>', util.win_resize_up(2), { desc = 'Resize window upward' })
|
||||
nmap('<s-Down>', util.win_resize_down(2), { desc = 'Resize window downward' })
|
||||
nmap('<s-Left>', util.win_resize_left(2), { desc = 'Resize window leftward' })
|
||||
nmap('<s-Right>', util.win_resize_right(2), { desc = 'Resize window rightward' })
|
||||
|
||||
-- easy tab navigation
|
||||
nmap('<Right>', '<cmd>tabnext<cr>', { silent = true })
|
||||
|
|
|
@ -37,6 +37,17 @@ M.partial = function(f, ...)
|
|||
end
|
||||
end
|
||||
|
||||
--- Delayed function execution.
|
||||
---@param f function: function whose evaluation will be delayed.
|
||||
---@param ... any: arguments to `f`.
|
||||
---@return function: a new function that calls f with provided arguments.
|
||||
M.thunk = function(f, ...)
|
||||
local args = {...}
|
||||
return function()
|
||||
return f(unpack(args))
|
||||
end
|
||||
end
|
||||
|
||||
-- Perform `func` (which can freely use register `reg`) and make sure `reg`
|
||||
-- is restored afterwards.
|
||||
M.with_saved_register = function(reg, func)
|
||||
|
@ -73,8 +84,53 @@ M.goto_prev_diagnostic = function(opts)
|
|||
vim.diagnostic.goto_prev(vim.tbl_extend('keep', opts or {}, diag_opts))
|
||||
end
|
||||
|
||||
M.use_local = function()
|
||||
--- Whether the current window is the last in a given direction.
|
||||
---@param direction string: one of 'h', 'j', 'k', or 'l'
|
||||
local win_is_last = function(direction)
|
||||
local current = vim.api.nvim_get_current_win()
|
||||
vim.cmd('wincmd ' .. direction)
|
||||
local next = vim.api.nvim_get_current_win()
|
||||
|
||||
local is_last = current == next
|
||||
if not is_last then vim.cmd('wincmd p') end
|
||||
|
||||
return is_last
|
||||
end
|
||||
|
||||
--- Resize current window in a given direction.
|
||||
---@param dir string: one of 'h', 'j', 'k', or 'l'
|
||||
---@param size integer: how much to resize
|
||||
local win_resize = function(dir, size)
|
||||
if dir ~= 'h' and dir ~= 'j' and dir ~= 'k' and dir ~= 'l' then return end
|
||||
|
||||
size = math.abs(size)
|
||||
local is_height = dir == 'j' or dir == 'k'
|
||||
local is_positive = dir == 'j' or dir == 'l'
|
||||
|
||||
if win_is_last(is_height and 'j' or 'l') then
|
||||
is_positive = not is_positive
|
||||
end
|
||||
|
||||
local delta = string.format('%s%d', is_positive and '+' or '-', size)
|
||||
local prefix = is_height and '' or 'vertical '
|
||||
vim.cmd(prefix .. 'resize ' .. delta .. '<cr>')
|
||||
end
|
||||
|
||||
--- Resize current window upwards.
|
||||
---@param size integer: how much to resize
|
||||
M.win_resize_up = function(size) return M.thunk(win_resize, 'k', size) end
|
||||
|
||||
--- Resize current window downwards.
|
||||
---@param size integer: how much to resize
|
||||
M.win_resize_down = function(size) return M.thunk(win_resize, 'j', size) end
|
||||
|
||||
--- Resize current window leftwards.
|
||||
---@param size integer: how much to resize
|
||||
M.win_resize_left = function(size) return M.thunk(win_resize, 'h', size) end
|
||||
|
||||
--- Resize current window rightwards.
|
||||
---@param size integer: how much to resize
|
||||
M.win_resize_right = function(size) return M.thunk(win_resize, 'l', size) end
|
||||
|
||||
return M
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue