Skip to content

Commit

Permalink
Open magit buffer with a smart cursor position
Browse files Browse the repository at this point in the history
When vimagit is started from a file in the repo, in order of preferences:

    on this file in unstaged section
    if not, on this file in staged section
    if not, on first file in unstaged section
    if not, on first file in staged section

ref #125
  • Loading branch information
jreybert committed Mar 1, 2017
1 parent a09d870 commit 5d1c255
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
21 changes: 15 additions & 6 deletions autoload/magit/state.vim
Original file line number Diff line number Diff line change
Expand Up @@ -321,24 +321,32 @@ function! magit#state#get_files(mode) dict
return self.dict[a:mode]
endfunction

" magit#state#get_filenames: global dict filenames getter function
" 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 filename strings belonging to mode, modified files
" first
function! magit#state#get_filenames(mode) dict
" return ordered list of file objects belonging to mode
function! magit#state#get_files_ordered(mode) dict
let modified = []
let others = []
for filename in sort(keys(self.dict[a:mode]))
let file = self.get_file(a:mode, filename)
if ( file.status == 'M' )
call add(modified, filename)
call add(modified, file)
else
call add(others, filename)
call add(others, file)
endif
endfor
return modified + others
endfunction

" magit#state#get_filenames: global dict filenames getter function
" param[in] mode: mode to select, can be 'staged' or 'unstaged'
" return ordered list of filename strings belonging to mode, modified files
" first
function! magit#state#get_filenames(mode) dict
let files = self.get_files_ordered(a:mode)
return map(copy(files), 'v:val.filename')
endfunction


" dict: structure containing all diffs
" It is formatted as follow
Expand All @@ -358,6 +366,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_ordered': function("magit#state#get_files_ordered"),
\ 'get_filenames': function("magit#state#get_filenames"),
\ 'add_file': function("magit#state#add_file"),
\ 'set_files_visible': function("magit#state#set_files_visible"),
Expand Down
32 changes: 31 additions & 1 deletion plugin/magit.vim
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,7 @@ endfunction
" 'c': current buffer (should be used when opening vim in vimagit mode
function! magit#show_magit(display, ...)
if ( &filetype == 'netrw' )
let cur_file = ""
let cur_file_path = b:netrw_curdir
else
let cur_file = expand("%:p")
Expand Down Expand Up @@ -789,7 +790,36 @@ function! magit#show_magit(display, ...)
endif

call magit#update_buffer()
execute "normal! gg"

" move cursor to (in priority order if not found):
" - current file unstaged
" - current file staged
" - first unstaged file
" - first stage file
let cur_filename = matchlist(cur_file, git_dir . '\(.*\)')
if ( !empty(cur_filename) )
let cur_file = cur_filename[1]
try
let file = b:state.get_file('unstaged', cur_file, 0)
call cursor(file.line_pos, 0)
catch 'file_doesnt_exists'
try
let file = b:state.get_file('staged', cur_file, 0)
call cursor(file.line_pos, 0)
catch 'file_doesnt_exists'
let unstaged_files = b:state.get_files_ordered('unstaged')
if ( !empty(unstaged_files) )
call cursor(unstaged_files[0].line_pos, 0)
else
let staged_files = b:state.get_files_ordered('staged')
if ( !empty(staged_files) )
call cursor(staged_files[0].line_pos, 0)
endif
endif
endtry
endtry
endif

endfunction

function! magit#close_magit()
Expand Down

0 comments on commit 5d1c255

Please sign in to comment.