karb94 / neoscroll.nvim

Smooth scrolling neovim plugin written in lua
MIT License
1.46k stars 36 forks source link

Centering after animation #65

Open hacker-DOM opened 2 years ago

hacker-DOM commented 2 years ago

Hi. Firstly thx for the excellent plugin. I would like to center my screen after every animation. Based on the docs, I figured this would do the trick:

require('neoscroll').setup({
    post_hook = function(info) vim.cmd [[ normal zz ]] end
})

but it doesn't seem to work. I also tried exe "normal zz" instead ☁️

jrnxf commented 1 year ago

i also tried the same thing with no luck 😢

znd4 commented 1 year ago

this is working for me:

require("neoscroll").setup({
    post_hook = function(info)
        vim.cmd("normal! zz")
    end,
})

I have a feeling that there's a way to use neoscroll.zz here, although obviously infinite recursion is a concern there xD

EDIT: the behavior still isn't really ideal -- it's exactly what I want for <C-d> and <C-u>, but it basically disables zt, zb, <C-e>, and <C-y>.

znd4 commented 1 year ago

Spent a few hours on this -- I'd recommend tweaking the specific numbers / easing function:

local neoscroll = require("neoscroll")

local easing = "sine"
local zz_time_ms = 100
local jump_time_ms = 200

neoscroll.setup({
    post_hook = function(info)
        if info ~= "center" then
            return
        end

        -- The `defer_fn` is a bit of a hack.
        -- We use it so that `neoscroll.init.scroll` will be false when we call `neoscroll.zz`
        -- As long as we don't input another neoscroll mapping in the timeout,
        -- we should be able to center the cursor.
        local defer_time_ms = 10
        vim.defer_fn(function()
            neoscroll.zz(zz_time_ms, easing)
        end, defer_time_ms)
    end,
})

local mappings = {}

mappings["<C-u>"] = { "scroll", { "-vim.wo.scroll", "true", jump_time_ms, easing, "'center'" } }
mappings["<C-d>"] = { "scroll", { "vim.wo.scroll", "true", jump_time_ms, easing, "'center'" } }

require("neoscroll.config").set_mappings(mappings)
kohane27 commented 1 year ago

@zdog234 Thank you for creating this hack, it works with your config, but the scroll speed is too fast for my eyes. I've tried to tweak the number but the following happens:

https://user-images.githubusercontent.com/57322459/209611462-e6077f34-0131-4862-9aad-bd59ea51ad1d.mp4

Basically it'd start to center with zz before the scrolling is done. It only happens when I scroll at the top of the file, not when the cursor is already centered. I tried tweaking but to no avail. Then I found cinnamon.nvim and with its built-in centered = true I no longer have the above weird issue. Thanks guys!