Skip to content

Commit

Permalink
Rework and simplify magit#utils#search_buffer_in_windows usage
Browse files Browse the repository at this point in the history
This very complicated function has been created for wrong reasons.
vim offers since 7.4.1926 a lot of window related function, such as
'bufwinid()', 'win_gotoid()'...

This function were used in magit#show_magit and magit#jump_to.

magit#show_magit is a simple update.
magit#jump_to goes a bit furhter, to beat existing corner cases.

This new patch implies a check of vim version at startup.
  • Loading branch information
jreybert committed May 21, 2022
1 parent 65feed9 commit 88720f4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 30 deletions.
17 changes: 0 additions & 17 deletions autoload/magit/utils.vim
Original file line number Diff line number Diff line change
Expand Up @@ -153,23 +153,6 @@ function! magit#utils#bufnr()
return s:bufnr
endfunction

" magit#utils#search_buffer_in_windows: search if a buffer is displayed in one
" of opened windows
" NOTE: windo command modify winnr('#'), if you want to use it, save it before
" calling this function
" param[in] filename: filename to search
" return: window id, 0 if not found
function! magit#utils#search_buffer_in_windows(filename)
let cur_win = winnr()
let last_win = winnr('#')
let files={}
windo if ( !empty(@%) ) | let files[@%] = winnr() | endif
execute last_win."wincmd w"
execute cur_win."wincmd w"
return ( has_key(files, buffer_name(a:filename)) ) ?
\files[buffer_name(a:filename)] : 0
endfunction

function! magit#utils#start_profile(...)
let prof_file = ( a:0 == 1 ) ? a:1 : "/tmp/vimagit.log"
profdel *
Expand Down
36 changes: 23 additions & 13 deletions plugin/magit.vim
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ let g:loaded_magit = 1

let g:vimagit_version = [1, 7, 3]

" Initialisation {{{
let g:vimagit_minium_vim_version = '7.4.1926'
if ! has('patch-' . g:vimagit_minium_vim_version)
throw "vimagit needs at least vim " . g:vimagit_minium_vim_version
endif

" FIXME: find if there is a minimum vim version required
" if v:version < 703
" endif
" Initialisation {{{

" source common file. variables in common file are shared with plugin and
" syntax files
Expand Down Expand Up @@ -759,10 +760,10 @@ function! magit#show_magit(display, ...)

let buffer_name=fnameescape('magit://' . git_dir)

let magit_win = magit#utils#search_buffer_in_windows(buffer_name)
let magit_win = bufwinid(buffer_name)

if ( magit_win != 0 )
silent execute magit_win."wincmd w"
if ( magit_win != -1 )
call win_gotoid(magit_win)
elseif ( a:display == 'v' )
silent execute "vnew " . buffer_name
" next is a workaround for vader, revert as soon as vader bug is fixed
Expand Down Expand Up @@ -1303,14 +1304,23 @@ function! magit#jump_to()
let line_in_hunk = len(filter(hunk_extract, 'v:val =~ "^[ +]"'))
let line_in_file += line_in_hunk

" winnr('#') is overwritten by magit#get_win()
let last_win = winnr('#')
let buf_win = magit#utils#search_buffer_in_windows(filename)
let buf_win = ( buf_win == 0 ) ? last_win : buf_win
if ( buf_win == 0 || winnr('$') == 1 )
if ( winnr('$') == 1 )
" if single window in current tab, create a new window to the right
rightbelow vnew
elseif ( bufwinid(filename) != -1 )
" window in current tab with 'filename' buffer visible
call win_gotoid(bufwinid(filename))
elseif ( win_getid(winnr('#')) )
" last access window
call win_gotoid(win_getid(winnr('#')))
elseif ( win_getid(winnr('1l')) != win_getid() )
" right window
call win_gotoid(win_getid(winnr('1l')))
elseif ( win_getid(winnr('1h')) != win_getid() )
" left window
call win_gotoid(win_getid(winnr('1h')))
else
execute buf_win."wincmd w"
throw "unexpected error: no viable window"
endif

try
Expand Down

0 comments on commit 88720f4

Please sign in to comment.