TaDaa / vimade

An eye friendly plugin that fades your inactive buffers and preserves your syntax highlighting!
MIT License
486 stars 8 forks source link

Only fade the other windows when the floating or popup window toggle. #44

Open kevinhwang91 opened 4 years ago

kevinhwang91 commented 4 years ago

I make a trick for achieving this feature.

augroup FadeExceptFloating
    autocmd WinEnter * call s:FadeExceptFloating()
augroup END

function s:FadeExceptFloating() abort
    if empty(nvim_win_get_config(0)['relative'])
        if exists('g:vimade_running') && g:vimade_running != 0
            " execute('VimadeDisable') doesn't work as expect
            call timer_start(0, { -> execute('VimadeDisable')})
        endif
    else
        if !exists('g:vimade_running') || g:vimade_running == 0
            execute('VimadeEnable')
        endif
    endif
endfunction

Is anyone interested?

kevinhwang91 commented 4 years ago

It's so weird. The numhl works as expect by using call timer_start(0, { -> execute('VimadeDisable')}) instead of execute 'VimadeDisable'. Maybe casued by invoking VimadeDisable inside WinEnter event?

TaDaa commented 4 years ago

unfadeAll needed a few updates to support signs and basegroups, it should work as expected now without the timer_start

This sounds like a nice feature and we should be able to include it

kevinhwang91 commented 4 years ago

I found that in the floating window, no matter VimadeEnable or VimadeDisable will change the last window (Running wincmd p in multiple windows in a tabpage will produce different behavior than before running VimadeEnable or VimadeDisable).

I solve this issue by these code:

function s:toggle_vimade(enable) abort
    let cur_win = nvim_get_current_win()
    noautocmd wincmd p
    let last_win = nvim_get_current_win()
    noautocmd wincmd p
    if a:enable
        execute 'VimadeEnable'
    else
        execute 'VimadeDisable'
    endif
    noautocmd call nvim_set_current_win(last_win)
    noautocmd call nvim_set_current_win(cur_win)
endfunction

function s:fade_except_floating() abort
    if empty(nvim_win_get_config(0)['relative'])
        if exists('g:vimade_running') && g:vimade_running != 0
            call s:toggle_vimade(0)
        endif
    else
        if !exists('g:vimade_running') || g:vimade_running == 0
            call s:toggle_vimade(1)
        endif
    endif
endfunction
TaDaa commented 4 years ago

Sounds like a bug, if you can send me steps to reproduce I'll take a look. Still have it in my mind to add a config option for your modal behavior

kevinhwang91 commented 4 years ago

You can reproduce by this script: cat reproduce.vim

call plug#begin('~/.config/nvim/plugged')
Plug 'TaDaa/vimade'
call plug#end()

function CreateFloating() abort
    let buf_handle = nvim_create_buf(v:false, v:true)
    return nvim_open_win(buf_handle, v:true, {
                \'relative': 'editor',
                \'height': float2nr(0.5* &lines),
                \'width': float2nr(0.5 * &columns),
                \'col': float2nr(0.25 * &columns),
                \'row': float2nr(0.25 * &lines)})
endfunction

function Reproduce(vimade_enable) abort
    execute 'VimadeDisable'
    split
    split
    wincmd w
    echom 'current win before openning floating window: ' . nvim_get_current_win()
    call CreateFloating()
    if a:vimade_enable
        execute 'VimadeEnable'
        " comment out VimadeDisable have the same result
        " Only show my code logic
        execute 'VimadeDisable'  
    endif
    close
    echom 'current win after closing floating window: ' . nvim_get_current_win()
endfunction

You can compare the difference between nvim -u reproduce.vim +'call Reproduce(0)' and nvim -u reproduce.vim +'call Reproduce(1)' .