From 22fe847bb4d8b5f66c110a51b2e3200698010ffc Mon Sep 17 00:00:00 2001 From: Subhaditya Nath Date: Mon, 16 Nov 2020 17:43:56 +0530 Subject: [PATCH] Added smoothie version of the gg motion Closes psliwka/vim-smoothie#1 --- autoload/smoothie.vim | 48 +++++++++++++++++++++++++++++++++++++++++-- plugin/smoothie.vim | 6 ++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/autoload/smoothie.vim b/autoload/smoothie.vim index d2343c7..9f1c0c3 100644 --- a/autoload/smoothie.vim +++ b/autoload/smoothie.vim @@ -1,6 +1,12 @@ "" -" This variable is needed to let the s:movement_tick(_) function know whether -" to continue scrolling after reaching EOL (as in ^F) or not (^B, ^D, ^U, etc.) +" This variable is used to inform the s:step_*() functions about whether the +" current movement is a cursor movement or a scroll movement. Used for +" motions like gg +let s:cursor_movement = v:false + +"" +" This variable is needed to let the s:step_down() function know whether to +" continue scrolling after reaching EOL (as in ^F) or not (^B, ^D, ^U, etc.) " " NOTE: This variable "MUST" be set to v:false in "every" function that " invokes motion (except smoothie#forwards, where it must be set to v:true) @@ -58,6 +64,10 @@ endfunction " already at the top. Return 1 if cannot move any higher. function s:step_up() if line('.') > 1 + if s:cursor_movement + exe 'normal! k' + return 0 + endif call s:execute_preserving_scroll("normal! 1\") return 0 else @@ -75,6 +85,10 @@ function s:step_down() return 1 endif if line('.') < line('$') + if s:cursor_movement + exe 'normal! j' + return 0 + endif if s:ctrl_f_invoked && (winheight(0) - winline()) >= (line('$') - line('.')) " ^F is pressed, and the last line of the buffer is visible call s:execute_preserving_scroll("normal! \") @@ -270,4 +284,34 @@ function smoothie#backwards() call s:update_target(-winheight(0) * v:count1) endfunction +" Smoothie equivalent to gg. +function smoothie#gg() + let s:cursor_movement = v:true + let s:ctrl_f_invoked = v:false + if g:smoothie_disabled || mode(1) =~# 'o' && mode(1) =~? 'no' + " If in operator pending mode, disable vim-smoothie and force the movement + " to be line-wise, because gg was originally linewise. + " Uses the normal non-smooth version of gg. + exe 'normal! ' . (mode(1) ==# 'no' ? 'V' : '') . v:count . 'gg' + return + endif + " gg behaves like a jump-command + " so, append current position to the jumplist + " but before that, save v:count into a variable + let l:target = v:count1 + execute "normal! m'" + call s:update_target(l:target - line('.')) + " suspend further commands till the destination is reached + " see point (3) of https://github.com/psliwka/vim-smoothie/issues/1#issuecomment-560158642 + while line('.') != l:target + exe 'sleep ' . g:smoothie_update_interval . ' m' + endwhile + " reset s:cursor_movement to false + let s:cursor_movement = v:false + " :help 'startofline' + if &startofline + call cursor(line('.'), 1) + endif +endfunction + " vim: et ts=2 diff --git a/plugin/smoothie.vim b/plugin/smoothie.vim index 3c39aab..9d7c887 100644 --- a/plugin/smoothie.vim +++ b/plugin/smoothie.vim @@ -3,6 +3,7 @@ if has('nvim') || has('patch-8.2.1280') noremap (SmoothieUpwards) call smoothie#upwards() noremap (SmoothieForwards) call smoothie#forwards() noremap (SmoothieBackwards) call smoothie#backwards() + noremap (Smoothie_gg) call smoothie#gg() if !get(g:, 'smoothie_no_default_mappings', v:false) silent! map (SmoothieDownwards) @@ -13,12 +14,14 @@ if has('nvim') || has('patch-8.2.1280') silent! map (SmoothieBackwards) silent! map (SmoothieBackwards) silent! map (SmoothieBackwards) + silent! map gg (Smoothie_gg) endif else nnoremap (SmoothieDownwards) :call smoothie#downwards() nnoremap (SmoothieUpwards) :call smoothie#upwards() nnoremap (SmoothieForwards) :call smoothie#forwards() nnoremap (SmoothieBackwards) :call smoothie#backwards() + nnoremap (Smoothie_gg) :call smoothie#gg() if !get(g:, 'smoothie_no_default_mappings', v:false) silent! nmap (SmoothieDownwards) @@ -29,5 +32,8 @@ else silent! nmap (SmoothieBackwards) silent! nmap (SmoothieBackwards) silent! nmap (SmoothieBackwards) + silent! nmap gg (Smoothie_gg) endif endif + +" vim: et ts=2