Skip to content

Commit 5409f2e

Browse files
authored
Merge pull request #333 from lambdalisue/improve-zoom
Change behavior of "auto resize" and improve "zoom" related actions
2 parents ff5bcbf + c8676e0 commit 5409f2e

File tree

3 files changed

+79
-27
lines changed

3 files changed

+79
-27
lines changed

autoload/fern/internal/drawer/auto_resize.vim

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,35 @@ function! fern#internal#drawer#auto_resize#init() abort
55

66
augroup fern_internal_drawer_init
77
autocmd! * <buffer>
8-
autocmd BufEnter <buffer> call s:resize()
9-
autocmd BufLeave <buffer> call s:resize()
8+
autocmd BufEnter <buffer> call s:load_width()
9+
autocmd BufLeave <buffer> call s:save_width()
1010
augroup END
1111
endfunction
1212

1313
if has('nvim')
14-
function! s:is_relative() abort
14+
function! s:should_ignore() abort
1515
return nvim_win_get_config(win_getid()).relative !=# ''
1616
endfunction
17-
18-
function! s:resize() abort
19-
if s:is_relative()
20-
return
21-
endif
22-
call fern#internal#drawer#resize()
23-
endfunction
2417
else
25-
let s:resize = funcref('fern#internal#drawer#resize')
18+
function! s:should_ignore() abort
19+
return 0
20+
endfunction
2621
endif
22+
23+
function! s:save_width() abort
24+
if s:should_ignore()
25+
return
26+
endif
27+
let t:fern_drawer_auto_resize_width = winwidth(0)
28+
endfunction
29+
30+
function! s:load_width() abort
31+
if s:should_ignore()
32+
return
33+
endif
34+
if !exists('t:fern_drawer_auto_resize_width')
35+
call fern#internal#drawer#resize()
36+
else
37+
execute 'vertical resize' t:fern_drawer_auto_resize_width
38+
endif
39+
endfunction

autoload/fern/mapping/drawer.vim

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
function! fern#mapping#drawer#init(disable_default_mappings) abort
2-
nnoremap <buffer><silent> <Plug>(fern-action-zoom:half) :<C-u>call <SID>call('zoom', 0.4)<CR>
3-
nnoremap <buffer><silent> <Plug>(fern-action-zoom:full) :<C-u>call <SID>call('zoom', 0.9)<CR>
4-
5-
nmap <buffer> <Plug>(fern-action-zoom) <Plug>(fern-action-zoom:half)
2+
nnoremap <buffer><silent> <Plug>(fern-action-zoom) :<C-u>call <SID>call('zoom')<CR>
3+
nnoremap <buffer><silent> <Plug>(fern-action-zoom:reset) :<C-u>call <SID>call('zoom_reset')<CR>
4+
nmap <buffer><silent> <Plug>(fern-action-zoom:half) 4<Plug>(fern-action-zoom)
5+
nmap <buffer><silent> <Plug>(fern-action-zoom:full) 9<Plug>(fern-action-zoom)
66
77
if !a:disable_default_mappings
88
nmap <buffer><nowait> z <Plug>(fern-action-zoom)
9+
nmap <buffer><nowait> Z <Plug>(fern-action-zoom:reset)
910
endif
1011
endfunction
1112

@@ -16,9 +17,42 @@ function! s:call(name, ...) abort
1617
\)
1718
endfunction
1819

19-
function! s:map_zoom(helper, alpha) abort
20-
if fern#internal#drawer#is_drawer()
21-
let width = &columns * a:alpha
22-
execute printf('%d wincmd |', float2nr(width))
20+
function! s:map_zoom(helper) abort
21+
if !fern#internal#drawer#is_drawer()
22+
call fern#warn('zoom is available only on drawer')
23+
return
24+
endif
25+
let alpha = v:count
26+
if alpha <= 0 || alpha > 10
27+
let current = float2nr(ceil(str2float(winwidth(0)) / &columns * 10))
28+
let alpha = s:input_alpha(printf('Width ratio [%d -> 1-10]: ', current))
29+
if alpha is# v:null
30+
return
31+
endif
2332
endif
33+
let alpha = str2float(alpha)
34+
let width = &columns * (alpha / 10)
35+
execute 'vertical resize' float2nr(width)
36+
endfunction
37+
38+
function! s:map_zoom_reset(helper) abort
39+
if !fern#internal#drawer#is_drawer()
40+
call fern#warn('zoom:resize is available only on drawer')
41+
return
42+
endif
43+
call fern#internal#drawer#resize()
44+
endfunction
45+
46+
function! s:input_alpha(prompt) abort
47+
while v:true
48+
let result = input(a:prompt)
49+
if result ==# ''
50+
redraw | echo ''
51+
return v:null
52+
elseif result =~# '^\%(10\|[1-9]\)$'
53+
redraw | echo ''
54+
return result
55+
endif
56+
redraw | echo 'Please input a digit from 1 to 10'
57+
endwhile
2458
endfunction

doc/fern.txt

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,7 @@ VARIABLE *fern-variable*
422422
Default: 0
423423

424424
*g:fern#disable_drawer_auto_resize*
425-
Set 1 to disable automatically resize drawer on |BufEnter| and
426-
|BufLeave| autocmd.
425+
Set 1 to disable automatically resize drawer on |BufEnter| autocmd.
427426

428427
Note that this feature is automatically disabled on floating windows
429428
of Neovim to avoid unwilling resize reported as #294
@@ -782,16 +781,22 @@ See |fern-action| for more detail.
782781
-----------------------------------------------------------------------------
783782
GLOBAL *fern-mapping-global*
784783

785-
*<Plug>(fern-action-zoom:half)*
786-
Zoom width of the drawer style fern to half of the global width.
787-
The original window width will be restored once user leave the window.
784+
*<Plug>(fern-action-zoom)*
785+
Zoom width of the drawer style fern to the ratio of the global width.
786+
It prompt users to ask a desired ratio of the width if no |v:count| is
787+
given. Users can use 1 to 10 for the ratio.
788788
It only works on a drawer style fern window.
789789

790-
*<Plug>(fern-action-zoom:full)*
791-
Zoom width of the drawer style fern to full of the global width.
792-
The original window width will be restored once user leave the window.
790+
*<Plug>(fern-action-zoom:reset)*
791+
Reset the width of the drawer style fern to its original width.
793792
It only works on a drawer style fern window.
794793

794+
*<Plug>(fern-action-zoom:half)*
795+
This is an alias of "4<Plug>(fern-action-zoom)"
796+
797+
*<Plug>(fern-action-zoom:full)*
798+
This is an alias of "9<Plug>(fern-action-zoom)"
799+
795800
*<Plug>(fern-action-hidden:set)*
796801
Show hidden nodes. For example hidden nodes in file:// scheme is a
797802
file or directory starts from '.' character.

0 commit comments

Comments
 (0)