diff --git a/autoload/magit/state.vim b/autoload/magit/state.vim index 84407061751cd4e8d98760bf2e266be4501e9fdb..d33229e0e03f90ba0348628b9b5144206479ca27 100644 --- a/autoload/magit/state.vim +++ b/autoload/magit/state.vim @@ -321,6 +321,13 @@ function! magit#state#get_files(mode) dict return self.dict[a:mode] endfunction +" magit#state#get_files_nb: returns the number of files in a given section +" param[in] mode: mode to select, can be 'staged' or 'unstaged' +" return number of files of this section +function! magit#state#get_files_nb(mode) dict + return len(self.dict[a:mode]) +endfunction + " magit#state#get_files: global dict file objects (copy) getter function " param[in] mode: mode to select, can be 'staged' or 'unstaged' " return ordered list of file objects belonging to mode @@ -366,6 +373,7 @@ let magit#state#state = { \ 'nb_diff_lines': 0, \ 'get_file': function("magit#state#get_file"), \ 'get_files': function("magit#state#get_files"), + \ 'get_files_nb': function("magit#state#get_files_nb"), \ 'get_files_ordered': function("magit#state#get_files_ordered"), \ 'get_filenames': function("magit#state#get_filenames"), \ 'add_file': function("magit#state#add_file"), diff --git a/plugin/magit.vim b/plugin/magit.vim index ba33e9fd09bef1816144589f041a6c78d4719095..46845ac25e49b29347cafb40078617777aea0c3b 100644 --- a/plugin/magit.vim +++ b/plugin/magit.vim @@ -654,31 +654,43 @@ function! magit#update_buffer(...) endif if ( a:0 >= 3 ) - " if, in this order, current file, next file, previous file exists in - " current section, move cursor to it - let cur_file = 1 - for fname in [cur_filename, next_filename, prev_filename] - try - let file = b:state.get_file(cur_section, fname) - if ( cur_file ) - let hunk_id = max([0, min([len(file.get_hunks())-1, cur_hunk_id])]) - let cur_file = 0 - else - let hunk_id = 0 - endif + if (b:state.get_files_nb(cur_section) > 0) + " if, in this order, current file, next file, previous file exists in + " current section, move cursor to it + let cur_file = 1 + for fname in [cur_filename, next_filename, prev_filename] + try + let file = b:state.get_file(cur_section, fname) + if ( cur_file ) + let hunk_id = max([0, min([len(file.get_hunks())-1, cur_hunk_id])]) + let cur_file = 0 + else + let hunk_id = 0 + endif - if ( file.is_visible() ) - call cursor(file.get_hunks()[hunk_id].line_pos, 0) - if ( g:magit_auto_foldopen ) - foldopen + if ( file.is_visible() ) + call cursor(file.get_hunks()[hunk_id].line_pos, 0) + if ( g:magit_auto_foldopen ) + foldopen + endif + else + call cursor(file.line_pos, 0) endif - else - call cursor(file.line_pos, 0) - endif - break - catch 'file_doesnt_exists' - endtry - endfor + break + catch 'file_doesnt_exists' + endtry + endfor + else + " if current section is empty, move cursor to top to other section + if (cur_section == 'staged') + let cur_section = 'unstaged' + elseif (cur_section == 'unstaged') + let cur_section = 'staged' + endif + let section_line=search(g:magit_sections[cur_section], "bnw") + call cursor(section_line, 0) + endif + silent execute "normal! zt" endif if exists(':AirlineRefresh')