Skip to content

Commit

Permalink
plugin/magit.vim: add g:magit_default_sections to let user choose whi…
Browse files Browse the repository at this point in the history
…ch sections are displayed (fix #37)

It also removes default display of stash section. If user wants to display stash, it must add 'stash' entry in g:magit_default_sections
  • Loading branch information
jreybert committed Nov 9, 2015
1 parent 1067130 commit e5a4ffd
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 32 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,13 @@ When set to 2, filenames and hunks are unfolded.
Default value is 1.
> let g:magit_default_fold_level=[012]
#### g:magit_default_sections

With this variable, the user is able to choose which sections are displayed in magit
buffer, and in which order.
Default value:
> let g:magit_default_sections = ['info', 'global_help', 'commit', 'staged', 'unstaged']
#### g:magit_warning_max_lines

This variable is the maximum number of diff lines that vimagit will display
Expand Down
6 changes: 6 additions & 0 deletions doc/vimagit.txt
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,12 @@ When set to 2, filenames and hunks are unfolded.
Default value is 1.
let g:magit_default_fold_level=[012]

*vimagit-g:magit_default_sections*
With this variable, the user is able to choose which sections are displayed in
magit buffer, and in which order.
Default value:
let g:magit_default_sections = ['info', 'global_help', 'commit', 'staged', 'unstaged']

*vimagit-g:magit_warning_max_lines*
This variable is the maximum number of diff lines that vimagit will display
without warning the user. If the number of diff lines to display is greater than
Expand Down
80 changes: 48 additions & 32 deletions plugin/magit.vim
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ let g:magit_enabled = get(g:, 'magit_enabled',
let g:magit_show_help = get(g:, 'magit_show_help', 1)
let g:magit_default_show_all_files = get(g:, 'magit_default_show_all_files', 0)
let g:magit_default_fold_level = get(g:, 'magit_default_fold_level', 1)
let g:magit_default_sections = get(g:, 'magit_default_sections', ['info', 'global_help', 'commit', 'staged', 'unstaged'])

let g:magit_warning_max_lines = get(g:, 'magit_warning_max_lines', 10000)

Expand Down Expand Up @@ -227,32 +228,34 @@ let s:magit_commit_mode=''
" 'CC': prepare a brand new commit message
" 'CA': get the last commit message
function! s:mg_get_commit_section()
let commit_mode_str=""
if ( s:magit_commit_mode == 'CC' )
let commit_mode_str="normal"
elseif ( s:magit_commit_mode == 'CA' )
let commit_mode_str="amend"
endif
silent put =''
silent put =g:magit_sections.commit_start
silent put ='Commit mode: '.commit_mode_str
call <SID>mg_section_help('commit')
silent put =magit#utils#underline(g:magit_sections.commit_start)
silent put =''
if ( s:magit_commit_mode != '' )
let commit_mode_str=""
if ( s:magit_commit_mode == 'CC' )
let commit_mode_str="normal"
elseif ( s:magit_commit_mode == 'CA' )
let commit_mode_str="amend"
endif
silent put =''
silent put =g:magit_sections.commit_start
silent put ='Commit mode: '.commit_mode_str
call <SID>mg_section_help('commit')
silent put =magit#utils#underline(g:magit_sections.commit_start)
silent put =''

let git_dir=magit#git#git_dir()
" refresh the COMMIT_EDITMSG file
if ( s:magit_commit_mode == 'CC' )
silent! call magit#utils#system("GIT_EDITOR=/bin/false git commit -e 2> /dev/null")
elseif ( s:magit_commit_mode == 'CA' )
silent! call magit#utils#system("GIT_EDITOR=/bin/false git commit --amend -e 2> /dev/null")
endif
if ( filereadable(git_dir . 'COMMIT_EDITMSG') )
let comment_char=<SID>mg_comment_char()
let commit_msg=magit#utils#join_list(filter(readfile(git_dir . 'COMMIT_EDITMSG'), 'v:val !~ "^' . comment_char . '"'))
put =commit_msg
let git_dir=magit#git#git_dir()
" refresh the COMMIT_EDITMSG file
if ( s:magit_commit_mode == 'CC' )
silent! call magit#utils#system("GIT_EDITOR=/bin/false git commit -e 2> /dev/null")
elseif ( s:magit_commit_mode == 'CA' )
silent! call magit#utils#system("GIT_EDITOR=/bin/false git commit --amend -e 2> /dev/null")
endif
if ( filereadable(git_dir . 'COMMIT_EDITMSG') )
let comment_char=<SID>mg_comment_char()
let commit_msg=magit#utils#join_list(filter(readfile(git_dir . 'COMMIT_EDITMSG'), 'v:val !~ "^' . comment_char . '"'))
put =commit_msg
endif
put =g:magit_sections.commit_end
endif
put =g:magit_sections.commit_end
endfunction

" s:mg_comment_char: this function gets the commentChar from git config
Expand Down Expand Up @@ -502,6 +505,16 @@ function! magit#open_close_folding(...)
call magit#update_buffer()
endfunction

" s:mg_display_functions: Dict wrapping all display related functions
" This Dict should be accessed through g:magit_default_sections
let s:mg_display_functions = {
\ 'info': { 'fn': function("s:mg_get_info"), 'arg': []},
\ 'global_help': { 'fn': function("s:mg_section_help"), 'arg': ['global']},
\ 'commit': { 'fn': function("s:mg_get_commit_section"), 'arg': []},
\ 'staged': { 'fn': function("s:mg_get_staged_section"), 'arg': ['staged']},
\ 'unstaged': { 'fn': function("s:mg_get_staged_section"), 'arg': ['unstaged']},
\ 'stash': { 'fn': function("s:mg_get_stashes"), 'arg': []},
\ }

" magit#update_buffer: this function:
" 1. checks that current buffer is the wanted one
Expand All @@ -527,11 +540,6 @@ function! magit#update_buffer()
" delete buffer
silent! execute "silent :%delete _"

call <SID>mg_get_info()
call <SID>mg_section_help('global')
if ( s:magit_commit_mode != '' )
call <SID>mg_get_commit_section()
endif
call s:state.update()

if ( s:state.nb_diff_lines > g:magit_warning_max_lines && b:magit_warning_answered_yes == 0 )
Expand All @@ -544,9 +552,17 @@ function! magit#update_buffer()
endif
endif

call <SID>mg_get_staged_section('staged')
call <SID>mg_get_staged_section('unstaged')
call <SID>mg_get_stashes()
for section in g:magit_default_sections
try
let func = s:mg_display_functions[section]
catch
echohl WarningMsg
echom 'unknown section to display: ' . section
echom 'please check your redefinition of g:magit_default_sections'
echohl None
endtry
call call(func.fn, func.arg)
endfor

call winrestview(l:winview)

Expand Down

0 comments on commit e5a4ffd

Please sign in to comment.