wellle / context.vim

Vim plugin that shows the context of the currently visible buffer contents
MIT License
1.27k stars 23 forks source link

Activate on per-window basis #37

Closed rafi closed 4 years ago

rafi commented 4 years ago

What a wonderful plugin. It certainly shines for me when working on long YAML files where it's easy to lose the context and hierarchy of structures. Long messy Python files also gain better perspective with this plugin. Nevertheless, plugin's behavior is obtrusive, not by its own fault, but performance gets a hit and the essence of "always enabled" on all windows can become a nuisance. Therefore, I wish there was a way to make context.vim toggle on a per-window basis, only when I need its help.

My workaround is mapping a toggle shortcut with this function and manage autocmds and mappings myself:

let g:context_enabled = 0
let g:context_add_mappings = 0
let g:context_add_autocmds = 0

command! ContextToggleCustom call s:context_toggle()
map <silent> <Plug>(context-toggle) <Cmd>ContextToggleCustom<CR>

function! s:context_toggle()
    if ! exists('g:context') || g:context.enabled == 0
        augroup context.vim
            autocmd!
            autocmd BufAdd       * call context#update('BufAdd')
            autocmd BufEnter     * call context#update('BufEnter')
            autocmd CursorMoved  * call context#update('CursorMoved')
            autocmd VimResized   * call context#update('VimResized')
            autocmd CursorHold   * call context#update('CursorHold')
            autocmd User GitGutter call context#update('GitGutter')
        augroup END
        nnoremap <silent>        <C-Y> <C-Y>:call context#update('C-Y')<CR>
        nnoremap <silent>        zz     zzzz:call context#update('zz')<CR>
        nnoremap <silent>        zb     zbzb:call context#update('zb')<CR>
        nnoremap <silent> <expr> <C-E>            context#mapping#ce()
        nnoremap <silent> <expr> zt               context#mapping#zt()
        nnoremap <silent> <expr> k                context#mapping#k()
        nnoremap <silent> <expr> H                context#mapping#h()
        ContextEnable
        ContextActivate
    else
        ContextDisable
        nunmap <C-y>
        nunmap zz
        nunmap zb
        nunmap <C-e>
        nunmap zt
        nunmap H
        " Revert mapping back to accelerated-jk, if installed.
        if dein#tap('accelerated-jk')
            nmap <silent>k <Plug>(accelerated_jk_gk)
        else
            nunmap k
        endif
        autocmd! context.vim BufAdd *
        autocmd! context.vim BufEnter *
        autocmd! context.vim CursorMoved *
        autocmd! context.vim VimResized *
        autocmd! context.vim CursorHold *
    endif
endfunction

Thank you for this wonderful plugin :)

wellle commented 4 years ago

@rafi: Thanks! I pushed something to https://github.com/wellle/context.vim/compare/37-toggle-window. Could you try that branch 37-toggle-window?

On that branch you should be able to use the commands like this:

:ContextEnable window
:ContextDisable window
:ContextToggle window

Please let me know if that works for you.

/cc @toddyamakawa

rafi commented 4 years ago

@wellle awesome. had to fix context#util#active() to check either .. && (g:context.enabled || w:context.enabled) ... However as soon as I switch to a different window in split or tab, context.vim gets activated there too, unexpectedly.

wellle commented 4 years ago

I see. What I had implemented would only work if the plugin was enabled (so not let g:context_enabled = 0).

I made some changed and pushed an improved version to the same branch 37-toggle-window. Please try again with that. I think this should cover all cases now. Please let me know :v:

rafi commented 4 years ago

@wellle great. enabled windows are working nice and independently. only issue I've found is after :ContextToggle window, if I create split windows (e.g. :vnew) the context window overlaps the new split. Once I switch back to the context enabled window it realigns nicely.

wellle commented 4 years ago

Good point, thanks for reporting 👍

wellle commented 4 years ago

@rafi: I made some changes to how we check for pending layout updates (to do it also from windows for which context.vim isn't enabled) (on branch 46-disabled-update-layout which hasn't been merged yet) and rebased this branch 37-toggle-window on top of that. To me it seems like this has fixed the issue. Could you try again?

Also I think I'd like to expose separate commands like :ContextToggleWindow instead of the current :ContextToggle window. How do you feel about that?

Thanks!