Skip to content

Commit d0c9b7b

Browse files
committed
Initial
0 parents  commit d0c9b7b

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

README.rst

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
Vim Adjust Scroll
2+
=================
3+
4+
This plugin attempts to automatically adjust your Vim viewport so that about
5+
1/3 of the text you're looking at is above and 2/3 of the text is below your
6+
cursor.
7+
8+
This is achieved by scrolling automatically whenever:
9+
10+
* You open another file
11+
* You jump a large distance in the current buffer
12+
13+
Take a look at the source if you're curious to how it works!
14+
15+
Installing
16+
----------
17+
18+
For NeoBundle_ add the following to your `.vimrc`::
19+
20+
NeoBundle 'dbakker/vim-adjustscroll'
21+
22+
For Pathogen_, execute::
23+
24+
cd ~/.vim/bundle
25+
git clone https://github.com/dbakker/vim-holmes.git
26+
vim +Helptags +q
27+
28+
That's pretty much it!
29+
30+
License
31+
=======
32+
33+
Copyright (c) Daan Bakker. Distributed under the same terms as Vim itself. See `:help license`.
34+
35+
.. _Pathogen: https://github.com/tpope/vim-pathogen
36+
.. _NeoBundle: https://github.com/Shougo/neobundle.vim

autoload/adjustscroll.vim

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
if exists("autoloaded_adjustscroll") || &cp
2+
finish
3+
endif
4+
let autoloaded_adjustscroll = '0.1'
5+
6+
" adjustscroll#reset(): Choose a nice horizontal and vertical scroll position {{{2
7+
fun! adjustscroll#reset()
8+
normal! zt
9+
let scroll = (winheight(0)-&scrolloff*2-1)/3
10+
if scroll>0
11+
exe 'normal! '.scroll."\<C-Y>"
12+
endif
13+
14+
" Show as much of the code as possible near the end of the document
15+
if line('$') == line('w$')
16+
normal! zb
17+
while line('$') != line('w$')
18+
exe "normal! \<C-E>"
19+
endwhile
20+
endif
21+
22+
" Scroll the text horizontally to position the cursor at the right side of the screen.
23+
normal! ze
24+
endf
25+
26+
" adjustscroll#check(command): Runs the specified command and updates viewport {{{2
27+
fun! adjustscroll#check(command)
28+
if len(a:command) == 0
29+
return adjustscroll#reset()
30+
endif
31+
32+
let [buf, start, end] = [bufnr(''), line('w0'), line('w$')]
33+
sil exe a:command
34+
if bufnr('') != buf || (line('w0') > end || line('w$') < start)
35+
call adjustscroll#reset()
36+
endif
37+
endf
38+
39+
fun! adjustscroll#remap_normal(command)
40+
" Insert <C-u> et al literally into the map
41+
let z = substitute(a:command, '\v\<.[\+-].\>', '<C-v>\0', 'g')
42+
exe 'nnoremap <silent> '.a:command.' :<C-u>call adjustscroll#check("normal! ".(v:count>0?v:count : "")."'.z.'")<cr>'
43+
endf

plugin/adjustscroll.vim

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
if exists("loaded_adjustscroll") || &cp
2+
finish
3+
endif
4+
let loaded_adjustscroll = '0.1'
5+
6+
com! -nargs=* AdjustScroll call adjustscroll#check(<q-args>)
7+
8+
if !exists('g:adjustscroll_auto') || g:adjustscroll_auto == 1
9+
aug AutoAdjustScroll
10+
au!
11+
au VimResized * AdjustScroll
12+
au BufWinEnter * AdjustScroll
13+
aug END
14+
endif
15+
16+
if !exists('g:adjustscroll_remap') || g:adjustscroll_remap == 1
17+
for i in split('`. gg G zr zm <C-O> <C-I> <C-W>o <C-U> <C-D> <C-6> * # n N <C-]> gd')
18+
if len(maparg(i, 'n')) == 0
19+
call adjustscroll#remap_normal(i)
20+
endif
21+
endfor
22+
nnoremap <silent> <PageDown> <PageDown>:AdjustScroll<cr>
23+
nnoremap <silent> <PageUp> <PageUp>:AdjustScroll<cr>
24+
endif

0 commit comments

Comments
 (0)