From bba8d1beb37fe933a9f182b6bdf981e01f31499a Mon Sep 17 00:00:00 2001 From: Tim Pope Date: Wed, 1 Mar 2023 16:29:16 -0500 Subject: [PATCH] Avoid narrow :Git subcommand splits in blame windows If commit maps are to made available in blame buffers, then the existing behavior of editing the commit message in a horizontal split of a narrow window isn't going to cut it. My ideal solution would be to create a split that spans both the blame buffer and the original file, but I don't think there's a practical way to do that, so I've instead settled for using :topleft/:botright instead, as appropriate. The other natural solution would be to switch to the original file and split there, but that means we'd end up with a scroll-bound window with a mismatched height, which behaves exactly as poorly as you would expect. Furthermore, I've made the decision to extend this behavior to paginated output windows (e.g., `:Git log`), but *not* to other window creation commands like :Gsplit. References: https://github.com/tpope/vim-fugitive/issues/2035 --- autoload/fugitive.vim | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/autoload/fugitive.vim b/autoload/fugitive.vim index b03a8a0..bb4886e 100644 --- a/autoload/fugitive.vim +++ b/autoload/fugitive.vim @@ -142,14 +142,22 @@ function! s:Mods(mods, ...) abort let mods = substitute(a:mods, '\C', '', '') let mods = mods =~# '\S$' ? mods . ' ' : mods if a:0 && mods !~# '\<\%(aboveleft\|belowright\|leftabove\|rightbelow\|topleft\|botright\|tab\)\>' - if a:1 ==# 'Edge' + let default = a:1 + if default ==# 'SpanOrigin' + if s:OriginBufnr() > 0 && (mods =~# '\' ? &winfixheight : &winfixwidth) + let default = 'Edge' + else + let default = '' + endif + endif + if default ==# 'Edge' if mods =~# '\' ? &splitright : &splitbelow let mods = 'botright ' . mods else let mods = 'topleft ' . mods endif else - let mods = a:1 . ' ' . mods + let mods = default . ' ' . mods endif endif return substitute(mods, '\s\+', ' ', 'g') @@ -3294,6 +3302,11 @@ function! s:TempDelete(file) abort return '' endfunction +function! s:OriginBufnr(...) abort + let state = s:TempState(a:0 ? a:1 : bufnr('')) + return get(state, 'origin_bufnr', -1) +endfunction + augroup fugitive_temp autocmd! autocmd BufReadPre * exe s:TempReadPre( +expand('')) @@ -3346,7 +3359,8 @@ function! s:RunEdit(state, tmp, job) abort let noequalalways = 1 setglobal equalalways endif - exe substitute(a:state.mods, '\', '-tab', 'g') 'keepalt split' s:fnameescape(file) + let mods = s:Mods(a:state.mods, 'SpanOrigin') + exe substitute(mods, '\', '-tab', 'g') 'keepalt split' s:fnameescape(file) finally if exists('l:noequalalways') setglobal noequalalways @@ -3807,9 +3821,9 @@ function! fugitive#Command(line1, line2, range, bang, mods, arg, ...) abort if pager is# 2 && a:bang && a:line2 >= 0 let [do_edit, after_edit] = s:ReadPrepare(a:line1, a:line2, a:range, a:mods) elseif pager is# 2 && a:bang - let do_edit = s:Mods(a:mods) . 'pedit' + let do_edit = s:Mods(a:mods, 'SpanOrigin') . 'pedit' elseif !curwin - let do_edit = s:Mods(a:mods) . 'split' + let do_edit = s:Mods(a:mods, 'SpanOrigin') . 'split' else let do_edit = s:Mods(a:mods) . 'edit' call s:BlurStatus()