Skip to content

Commit

Permalink
Add a mode setting to control bar position and height.
Browse files Browse the repository at this point in the history
  • Loading branch information
dstein64 committed Dec 22, 2020
1 parent 588bd4a commit f28adcb
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 7 deletions.
37 changes: 30 additions & 7 deletions autoload/scrollview.vim
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ function! s:VisibleLineCount(winid, start, end) abort
let l:current_winid = win_getid(winnr())
call win_gotoid(a:winid)
let l:end = a:end
if l:end ==# '$'
if type(l:end) ==# v:t_string && l:end ==# '$'
let l:end = line('$')
endif
let l:module = luaeval('require("scrollview")')
Expand Down Expand Up @@ -192,15 +192,35 @@ function! s:CalculatePosition(winnr) abort
let [l:row, l:col] = win_screenpos(l:winnr)
let l:winheight = winheight(l:winnr)
let l:winwidth = winwidth(l:winnr)
" l:top is relative to the window, and 0-indexed.
let l:mode = s:GetVariable('scrollview_mode', l:winnr)
if l:mode ==# 'virtual'
" Update topline, botline, and line_count to correspond to virtual lines,
" which account for closed folds.
let l:virtual_counts = {
\ 'before': s:VisibleLineCount(l:winid, 1, l:topline - 1),
\ 'here': s:VisibleLineCount(l:winid, l:topline, l:botline),
\ 'after': s:VisibleLineCount(l:winid, l:botline + 1, '$')
\ }
let l:topline = l:virtual_counts.before + 1
let l:botline = l:virtual_counts.before + l:virtual_counts.here
let l:line_count = l:virtual_counts.before
\ + l:virtual_counts.here
\ + l:virtual_counts.after
endif
" l:top is the position for the top of the scrollbar, relative to the
" window, and 0-indexed.
let l:top = 0
if l:line_count ># 1
let l:top = (l:topline - 1.0) / (l:line_count - 1)
let l:top = float2nr(round((l:winheight - 1) * l:top))
endif
let l:height = l:winheight
if l:line_count ># l:height
let l:height = str2float(l:winheight) / l:line_count
let l:numerator = l:winheight
if l:mode ==# 'document'
let l:numerator = l:botline - l:topline + 1
endif
let l:height = str2float(l:numerator) / l:line_count
let l:height = float2nr(ceil(l:height * l:winheight))
endif
" Make sure bar properly reflects bottom of document.
Expand Down Expand Up @@ -247,19 +267,22 @@ function! s:ShowScrollbar(winnr) abort
if s:Contains(l:excluded_filetypes, l:buf_filetype)
return
endif
let l:wininfo = getwininfo(l:winid)[0]
" Don't show in terminal mode, since the bar won't be properly updated for
" insertions.
if getwininfo(l:winid)[0].terminal
if l:wininfo.terminal
return
endif
if l:winheight ==# 0 || l:winwidth ==# 0
return
endif
let l:bar_position = s:CalculatePosition(l:winnr)
" Don't show the position bar when it would span the entire screen.
if l:winheight ==# l:bar_position.height
let l:line_count = nvim_buf_line_count(l:bufnr)
" Don't show the position bar when all lines are on screen.
" WARN: See the botline usage warning in CalculatePosition.
if l:wininfo.botline - l:wininfo.topline + 1 ==# l:line_count
return
endif
let l:bar_position = s:CalculatePosition(l:winnr)
" Don't show scrollbar when its column is beyond what's valid.
let l:min_valid_col = 1
let l:max_valid_col = l:winwidth
Expand Down
29 changes: 29 additions & 0 deletions doc/scrollview.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ have the highest precedence, and global variables have the lowest).
*scrollview_on_startup* `1`
Specifies whether scrollbars are Considered only at global scope
enabled on startup
*scrollview_mode* `'default'`
Specifies what the scrollbar See |scrollview-modes| for details on
position and size correspond to the available modes
*scrollview_excluded_filetypes* `[]`
Optional file types for which
scrollbars should not be displayed
Expand Down Expand Up @@ -78,6 +81,32 @@ example.
let g:scrollview_base = 'buffer'
let g:scrollview_column = 80
Scrollview Modes ~
*scrollview-modes*
The following modes are available for the |scrollview_mode| configuration
setting (set as a |string|).

Mode Description
---- -----------
*default* The scrollbar top position reflects the window's top line number
relative to the document's line count. The scrollbar height reflects
the size of the window relative to the document's line count.

*document* The scrollbar top position reflects the window's top line number
relative to the document's line count. The scrollbar bottom
position reflects the window's bottom line number relative to the
document's line count. Thus, the scrollbar height reflects the
number of lines in the window (counting lines in closed folds)
relative to the document's line count.

*virtual* The scrollbar position and height are calculated similarly to
the |default| mode, but line numbers and the document line count
are implicitly updated to virtual counterparts that account for
closed folds. The scrollbar top position reflects the window's top
virtual line number relative to the document's virtual line count.
The scrollbar height reflects the size of the window relative to
the document's virtual line count.

Color Customization ~
*scrollview-color-customization*
The following highlight group can be configured to change |nvim-scrollview|'s
Expand Down
5 changes: 5 additions & 0 deletions doc/tags
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
:ScrollViewDisable scrollview.txt /*:ScrollViewDisable*
:ScrollViewEnable scrollview.txt /*:ScrollViewEnable*
:ScrollViewRefresh scrollview.txt /*:ScrollViewRefresh*
default scrollview.txt /*default*
document scrollview.txt /*document*
nvim-scrollview scrollview.txt /*nvim-scrollview*
scrollview-color-customization scrollview.txt /*scrollview-color-customization*
scrollview-configuration scrollview.txt /*scrollview-configuration*
scrollview-installation scrollview.txt /*scrollview-installation*
scrollview-issues scrollview.txt /*scrollview-issues*
scrollview-modes scrollview.txt /*scrollview-modes*
scrollview-requirements scrollview.txt /*scrollview-requirements*
scrollview-usage scrollview.txt /*scrollview-usage*
scrollview.txt scrollview.txt /*scrollview.txt*
scrollview_base scrollview.txt /*scrollview_base*
scrollview_column scrollview.txt /*scrollview_column*
scrollview_current_only scrollview.txt /*scrollview_current_only*
scrollview_excluded_filetypes scrollview.txt /*scrollview_excluded_filetypes*
scrollview_mode scrollview.txt /*scrollview_mode*
scrollview_on_startup scrollview.txt /*scrollview_on_startup*
scrollview_winblend scrollview.txt /*scrollview_winblend*
virtual scrollview.txt /*virtual*
1 change: 1 addition & 0 deletions plugin/scrollview.vim
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ endif
" *************************************************

let g:scrollview_on_startup = get(g:, 'scrollview_on_startup', 1)
let g:scrollview_mode = get(g:, 'scrollview_mode', 'default')
let g:scrollview_excluded_filetypes =
\ get(g:, 'scrollview_excluded_filetypes', [])
let g:scrollview_current_only = get(g:, 'scrollview_current_only', 0)
Expand Down

0 comments on commit f28adcb

Please sign in to comment.