diff --git a/autoload/fern/internal/drawer/auto_resize.vim b/autoload/fern/internal/drawer/auto_resize.vim index ff0d8c0..f54e7e3 100644 --- a/autoload/fern/internal/drawer/auto_resize.vim +++ b/autoload/fern/internal/drawer/auto_resize.vim @@ -5,22 +5,35 @@ function! fern#internal#drawer#auto_resize#init() abort augroup fern_internal_drawer_init autocmd! * - autocmd BufEnter call s:resize() - autocmd BufLeave call s:resize() + autocmd BufEnter call s:load_width() + autocmd BufLeave call s:save_width() augroup END endfunction if has('nvim') - function! s:is_relative() abort + function! s:should_ignore() abort return nvim_win_get_config(win_getid()).relative !=# '' endfunction - - function! s:resize() abort - if s:is_relative() - return - endif - call fern#internal#drawer#resize() - endfunction else - let s:resize = funcref('fern#internal#drawer#resize') + function! s:should_ignore() abort + return 0 + endfunction endif + +function! s:save_width() abort + if s:should_ignore() + return + endif + let t:fern_drawer_auto_resize_width = winwidth(0) +endfunction + +function! s:load_width() abort + if s:should_ignore() + return + endif + if !exists('t:fern_drawer_auto_resize_width') + call fern#internal#drawer#resize() + else + execute 'vertical resize' t:fern_drawer_auto_resize_width + endif +endfunction diff --git a/autoload/fern/mapping/drawer.vim b/autoload/fern/mapping/drawer.vim index faaca91..e0fe0ba 100644 --- a/autoload/fern/mapping/drawer.vim +++ b/autoload/fern/mapping/drawer.vim @@ -1,11 +1,12 @@ function! fern#mapping#drawer#init(disable_default_mappings) abort - nnoremap (fern-action-zoom:half) :call call('zoom', 0.4) - nnoremap (fern-action-zoom:full) :call call('zoom', 0.9) - - nmap (fern-action-zoom) (fern-action-zoom:half) + nnoremap (fern-action-zoom) :call call('zoom') + nnoremap (fern-action-zoom:reset) :call call('zoom_reset') + nmap (fern-action-zoom:half) 4(fern-action-zoom) + nmap (fern-action-zoom:full) 9(fern-action-zoom) if !a:disable_default_mappings nmap z (fern-action-zoom) + nmap Z (fern-action-zoom:reset) endif endfunction @@ -16,9 +17,42 @@ function! s:call(name, ...) abort \) endfunction -function! s:map_zoom(helper, alpha) abort - if fern#internal#drawer#is_drawer() - let width = &columns * a:alpha - execute printf('%d wincmd |', float2nr(width)) +function! s:map_zoom(helper) abort + if !fern#internal#drawer#is_drawer() + call fern#warn('zoom is available only on drawer') + return + endif + let alpha = v:count + if alpha <= 0 || alpha > 10 + let current = float2nr(ceil(str2float(winwidth(0)) / &columns * 10)) + let alpha = s:input_alpha(printf('Width ratio [%d -> 1-10]: ', current)) + if alpha is# v:null + return + endif endif + let alpha = str2float(alpha) + let width = &columns * (alpha / 10) + execute 'vertical resize' float2nr(width) +endfunction + +function! s:map_zoom_reset(helper) abort + if !fern#internal#drawer#is_drawer() + call fern#warn('zoom:resize is available only on drawer') + return + endif + call fern#internal#drawer#resize() +endfunction + +function! s:input_alpha(prompt) abort + while v:true + let result = input(a:prompt) + if result ==# '' + redraw | echo '' + return v:null + elseif result =~# '^\%(10\|[1-9]\)$' + redraw | echo '' + return result + endif + redraw | echo 'Please input a digit from 1 to 10' + endwhile endfunction diff --git a/doc/fern.txt b/doc/fern.txt index 26d351a..004a163 100644 --- a/doc/fern.txt +++ b/doc/fern.txt @@ -422,8 +422,7 @@ VARIABLE *fern-variable* Default: 0 *g:fern#disable_drawer_auto_resize* - Set 1 to disable automatically resize drawer on |BufEnter| and - |BufLeave| autocmd. + Set 1 to disable automatically resize drawer on |BufEnter| autocmd. Note that this feature is automatically disabled on floating windows of Neovim to avoid unwilling resize reported as #294 @@ -782,16 +781,22 @@ See |fern-action| for more detail. ----------------------------------------------------------------------------- GLOBAL *fern-mapping-global* -*(fern-action-zoom:half)* - Zoom width of the drawer style fern to half of the global width. - The original window width will be restored once user leave the window. +*(fern-action-zoom)* + Zoom width of the drawer style fern to the ratio of the global width. + It prompt users to ask a desired ratio of the width if no |v:count| is + given. Users can use 1 to 10 for the ratio. It only works on a drawer style fern window. -*(fern-action-zoom:full)* - Zoom width of the drawer style fern to full of the global width. - The original window width will be restored once user leave the window. +*(fern-action-zoom:reset)* + Reset the width of the drawer style fern to its original width. It only works on a drawer style fern window. +*(fern-action-zoom:half)* + This is an alias of "4(fern-action-zoom)" + +*(fern-action-zoom:full)* + This is an alias of "9(fern-action-zoom)" + *(fern-action-hidden:set)* Show hidden nodes. For example hidden nodes in file:// scheme is a file or directory starts from '.' character.