From 1ea8611dbc31722a14ef25f52a5bfe402f8774b6 Mon Sep 17 00:00:00 2001 From: rootiest Date: Tue, 18 Mar 2025 22:29:45 -0400 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Feat(scrolling):=20smarter=20?= =?UTF-8?q?=20and=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Allows and to accept a count and scroll by *. The count is remembered and re-used for both and so repetitively pressing those sequences will continue to use the previous count. Count defaults to 1 initally (scroll by one half-screen) --- lua/config/overrides.lua | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/lua/config/overrides.lua b/lua/config/overrides.lua index 92bc216..abd40fd 100644 --- a/lua/config/overrides.lua +++ b/lua/config/overrides.lua @@ -106,3 +106,42 @@ vim.keymap.set( 'Q', { noremap = true, desc = 'Replay last register' } ) + +--------------------------------------------------------------------------- +-- ╓─────────────────────────────────────────────────────────╖ +-- ║ Scroll half a screen with and ║ +-- ║ (with count and memory) ║ +-- ╙─────────────────────────────────────────────────────────╜ +local last_scroll_count = 1 -- Store the last used count + +-- Define the ScrollDirection type +---@alias ScrollDirection 'up'|'down' + +--- Scroll half a screen up or down +---@param direction ScrollDirection The direction to scroll in +local function scroll(direction) + -- Set the count to the provided count or the last used count + local count = vim.v.count > 0 and vim.v.count or last_scroll_count + -- Remember the count for next time + last_scroll_count = count + -- Calculate the number of lines in a half-screen + local half_screen = math.floor(vim.api.nvim_win_get_height(0) / 2) + -- Generate the key sequence + local keys = count * half_screen .. (direction == 'down' and '\x04' or '\x15') + + -- Execute the key sequence + vim.api.nvim_feedkeys( + vim.api.nvim_replace_termcodes(keys, true, false, true), + 'n', + false + ) +end + +-- Map the scroll functions to and +vim.keymap.set('n', '', function() + scroll('down') +end, { silent = true }) +vim.keymap.set('n', '', function() + scroll('up') +end, { silent = true }) +---------------------------------------------------------------------------