Skip to content
This repository has been archived by the owner on Jul 2, 2021. It is now read-only.

Commit

Permalink
Added smoothie version of the gg motion
Browse files Browse the repository at this point in the history
Closes psliwka#1
  • Loading branch information
subnut committed Nov 17, 2020
1 parent fa61acd commit 171d6d9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
48 changes: 46 additions & 2 deletions autoload/smoothie.vim
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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\<C-U>")
return 0
else
Expand All @@ -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! \<C-E>")
Expand Down Expand Up @@ -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:count = v:count
execute "normal! m'"
call s:update_target(l:count - 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:count ? l:count : 1)
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
6 changes: 6 additions & 0 deletions plugin/smoothie.vim
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ if has('nvim') || has('patch-8.2.1280')
noremap <silent> <Plug>(SmoothieUpwards) <cmd>call smoothie#upwards() <CR>
noremap <silent> <Plug>(SmoothieForwards) <cmd>call smoothie#forwards() <CR>
noremap <silent> <Plug>(SmoothieBackwards) <cmd>call smoothie#backwards() <CR>
noremap <silent> <Plug>(Smoothie_gg) <cmd>call smoothie#gg() <CR>
if !get(g:, 'smoothie_no_default_mappings', v:false)
silent! map <unique> <C-D> <Plug>(SmoothieDownwards)
Expand All @@ -13,12 +14,14 @@ if has('nvim') || has('patch-8.2.1280')
silent! map <unique> <C-B> <Plug>(SmoothieBackwards)
silent! map <unique> <S-Up> <Plug>(SmoothieBackwards)
silent! map <unique> <PageUp> <Plug>(SmoothieBackwards)
silent! map <unique> gg <Plug>(Smoothie_gg)
endif
else
nnoremap <silent> <Plug>(SmoothieDownwards) :<C-U>call smoothie#downwards() <CR>
nnoremap <silent> <Plug>(SmoothieUpwards) :<C-U>call smoothie#upwards() <CR>
nnoremap <silent> <Plug>(SmoothieForwards) :<C-U>call smoothie#forwards() <CR>
nnoremap <silent> <Plug>(SmoothieBackwards) :<C-U>call smoothie#backwards() <CR>
nnoremap <silent> <Plug>(Smoothie_gg) :<C-U>call smoothie#gg() <CR>
if !get(g:, 'smoothie_no_default_mappings', v:false)
silent! nmap <unique> <C-D> <Plug>(SmoothieDownwards)
Expand All @@ -29,5 +32,8 @@ else
silent! nmap <unique> <C-B> <Plug>(SmoothieBackwards)
silent! nmap <unique> <S-Up> <Plug>(SmoothieBackwards)
silent! nmap <unique> <PageUp> <Plug>(SmoothieBackwards)
silent! nmap <unique> gg <Plug>(Smoothie_gg)
endif
endif

" vim: et ts=2

0 comments on commit 171d6d9

Please sign in to comment.