Skip to content

Commit 88720f4

Browse files
committed
Rework and simplify magit#utils#search_buffer_in_windows usage
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.
1 parent 65feed9 commit 88720f4

2 files changed

Lines changed: 23 additions & 30 deletions

File tree

autoload/magit/utils.vim

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -153,23 +153,6 @@ function! magit#utils#bufnr()
153153
return s:bufnr
154154
endfunction
155155

156-
" magit#utils#search_buffer_in_windows: search if a buffer is displayed in one
157-
" of opened windows
158-
" NOTE: windo command modify winnr('#'), if you want to use it, save it before
159-
" calling this function
160-
" param[in] filename: filename to search
161-
" return: window id, 0 if not found
162-
function! magit#utils#search_buffer_in_windows(filename)
163-
let cur_win = winnr()
164-
let last_win = winnr('#')
165-
let files={}
166-
windo if ( !empty(@%) ) | let files[@%] = winnr() | endif
167-
execute last_win."wincmd w"
168-
execute cur_win."wincmd w"
169-
return ( has_key(files, buffer_name(a:filename)) ) ?
170-
\files[buffer_name(a:filename)] : 0
171-
endfunction
172-
173156
function! magit#utils#start_profile(...)
174157
let prof_file = ( a:0 == 1 ) ? a:1 : "/tmp/vimagit.log"
175158
profdel *

plugin/magit.vim

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ let g:loaded_magit = 1
77

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

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

12-
" FIXME: find if there is a minimum vim version required
13-
" if v:version < 703
14-
" endif
15+
" Initialisation {{{
1516

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

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

762-
let magit_win = magit#utils#search_buffer_in_windows(buffer_name)
763+
let magit_win = bufwinid(buffer_name)
763764

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

1306-
" winnr('#') is overwritten by magit#get_win()
1307-
let last_win = winnr('#')
1308-
let buf_win = magit#utils#search_buffer_in_windows(filename)
1309-
let buf_win = ( buf_win == 0 ) ? last_win : buf_win
1310-
if ( buf_win == 0 || winnr('$') == 1 )
1307+
if ( winnr('$') == 1 )
1308+
" if single window in current tab, create a new window to the right
13111309
rightbelow vnew
1310+
elseif ( bufwinid(filename) != -1 )
1311+
" window in current tab with 'filename' buffer visible
1312+
call win_gotoid(bufwinid(filename))
1313+
elseif ( win_getid(winnr('#')) )
1314+
" last access window
1315+
call win_gotoid(win_getid(winnr('#')))
1316+
elseif ( win_getid(winnr('1l')) != win_getid() )
1317+
" right window
1318+
call win_gotoid(win_getid(winnr('1l')))
1319+
elseif ( win_getid(winnr('1h')) != win_getid() )
1320+
" left window
1321+
call win_gotoid(win_getid(winnr('1h')))
13121322
else
1313-
execute buf_win."wincmd w"
1323+
throw "unexpected error: no viable window"
13141324
endif
13151325

13161326
try

0 commit comments

Comments
 (0)