Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for gg/G #23

Open
ouuan opened this issue May 9, 2021 · 9 comments
Open

Add support for gg/G #23

ouuan opened this issue May 9, 2021 · 9 comments
Labels
enhancement New feature or request

Comments

@ouuan
Copy link

ouuan commented May 9, 2021

I added these in my config:

t['gg']    = {'scroll', {'1 - vim.api.nvim_win_get_cursor(0)[1]', 'true', '1', '5', e}}
t['G']     = {'scroll', {'vim.api.nvim_buf_line_count(0) - vim.api.nvim_win_get_cursor(0)[1]', 'true', '1', '5', e}}

And it turns out that it doesn't work well when there are wrapped lines, because neoscroll uses gj and gk, which is good for other commands like <c-u>/<c-d> but bad for gg and G.

Or is there any way to get the display line number? I didn't find it in help or online.

@ouuan ouuan changed the title Add an argument to use j/k instead of gj/gk Add a parameter to use j/k instead of gj/gk May 9, 2021
@karb94
Copy link
Owner

karb94 commented May 9, 2021

Yes gg and G have some complications. The good news is that it is relatively easy to implement inside Neoscroll so the best solution is for me to make wrapper functions like I did for zt/zz/zb.

@karb94 karb94 added the enhancement New feature or request label May 9, 2021
@karb94
Copy link
Owner

karb94 commented May 12, 2021

as a temporary solution you could do something like:

t['gg']    = {'scroll', {'-2*vim.api.nvim_buf_line_count(0)', 'true', '1', '5', e}}
t['G']     = {'scroll', {'2*vim.api.nvim_buf_line_count(0)', 'true', '1', '5', e}}

@karb94 karb94 changed the title Add a parameter to use j/k instead of gj/gk Add support for gg/G Jun 24, 2021
@vext01
Copy link

vext01 commented Oct 7, 2021

Similarly, it would be good if pageup and pagedown were captured by neoscroll.

@karb94
Copy link
Owner

karb94 commented Oct 9, 2021

I finally had some time to look at this. I'm not entirely sure what would be the expected behaviour when an easing function is enabled. There are two parts for this animation:

  1. The window scrolls till the edge of the buffer
  2. The cursor scrolls till the edge of the buffer

The cursor has to scroll after the window reaches the edge of the buffer because the cursor cannot be at the bottom/top of the window if scrolloff is greater than 0.

Having said that, when an easing function is enabled do we want the easing function to apply to both movements, only on the window scroll movement or only on the cursor movement?

@maxwelljens
Copy link

@vext01 You can add map <PageUp> <C-b> and map <PageDown> <C-f> to your config. That will make page up and page down do smooth scrolling.

@veyxov
Copy link

veyxov commented May 10, 2022

@vext01 You can add map <PageUp> <C-b> and map <PageDown> <C-f> to your config. That will make page up and page down do smooth scrolling.

Didn't work for me.

@lawrence-laz
Copy link

@vext01 You can add map <PageUp> <C-b> and map <PageDown> <C-f> to your config. That will make page up and page down do smooth scrolling.

Didn't work for me.

I just duplicated mapping for PageUp and PageDown and it seems to work nicely:

t['<C-b>'] = {'scroll', {'-vim.api.nvim_win_get_height(0)', 'true', '100'}}
t['<PageUp>'] = {'scroll', {'-vim.api.nvim_win_get_height(0)', 'true', '100'}}
t['<C-f>'] = {'scroll', { 'vim.api.nvim_win_get_height(0)', 'true', '100'}}
t['<PageDown>'] = {'scroll', { 'vim.api.nvim_win_get_height(0)', 'true', '100'}}

@benwoodward
Copy link

benwoodward commented May 3, 2023

as a temporary solution you could do something like:

t['gg']    = {'scroll', {'-2*vim.api.nvim_buf_line_count(0)', 'true', '1', '5', e}}
t['G']     = {'scroll', {'2*vim.api.nvim_buf_line_count(0)', 'true', '1', '5', e}}

This is great, thanks. The only issue with this for me is that if the file is shorter than the height of the window, then the file will scroll to the last line leaving only the last line of the file visible at the top of the window.

My workaround for this: (only smooth scroll if buffer is longer than window height)

local api = vim.api

vim.keymap.set('n', 'G', [[<Cmd> lua maybe_smooth_scroll()<CR>]], { noremap = true })

function _G.maybe_smooth_scroll()
  local win_height = api.nvim_win_get_height(0)
  local buf_line_count = api.nvim_buf_line_count(0)

  if buf_line_count < win_height then
    api.nvim_command('normal! G')
  else
    require('neoscroll').scroll(1 * buf_line_count, true, 1, 5)
  end
end

@peter-cardenas-ai
Copy link

i have the above in addition to a post hook to set the cursor to the last and first lines for G and gg respectively. i believe this is the desired workflow for these commands?

  post_hook = function(info)
    if info == nil then
      return
    end
    if info.kind == 'gg' then
      vim.api.nvim_win_set_cursor(info.winid, { 1, 0 })
    elseif info.kind == 'G' then
      local line = vim.api.nvim_buf_line_count(info.bufnr)
      vim.api.nvim_win_set_cursor(info.winid, { line, 0 })
    end
  end,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

8 participants