Skip to content

Commit

Permalink
limit some mapping to work only within some sections
Browse files Browse the repository at this point in the history
it mainly allows the user to get all vim mapping in commit editing mode
ref #106 #101
  • Loading branch information
jreybert committed Oct 2, 2017
1 parent a9adf1b commit 554e10a
Showing 1 changed file with 83 additions and 24 deletions.
107 changes: 83 additions & 24 deletions autoload/magit/mapping.vim
Original file line number Diff line number Diff line change
Expand Up @@ -47,32 +47,91 @@ function! s:mg_open_close_folding_wrapper(mapping, ...)
endif
endfunction

" s:mapping_wrapper: wrapper for mapping commands
" it needs a wrapper because some mappings must only be enabled in some
" sections. For example, wa want that 'S' mapping to be enabled in staged and
" unstaged sections, but not in commit section.
" param[in] mapping the key for the mapping (lhs)
" param[in] function the function to call (rhs)
" param[in] ... : optional, section, the regex of the sections where to enable the
" mapping. If there is no section parameter or if the section parameter regex
" match the current section, the rhs is called. Otherwise, the mapping is
" applied to its original meaning.
function! s:mapping_wrapper(mapping, function, ...)
if ( a:0 == 0 || magit#helper#get_section() =~ a:1 )
execute "call " . a:function
else
" feedkeys(..., 'n') is prefered over execute normal!
" normal! does not enter in insert mode
call feedkeys(a:mapping, 'n')
endif
endfunction

" s:mg_set_mapping: helper function to setup the mapping
" param[in] mode the mapping mode, one letter. Can be 'n', 'x', 'i', ...
" param[in] mapping the key for the mapping (lhs)
" param[in] function the function to call (rhs)
" param[in] ... : optional, section, the regex of the section(s)
function! s:mg_set_mapping(mode, mapping, function, ...)
execute a:mode . "noremap <buffer><silent><nowait> "
\ . a:mapping .
\ " :call <SID>mapping_wrapper(\"" .
\ a:mapping . "\", \"" .
\ a:function . "\"" .
\ ( ( a:0 == 1 ) ?
\ ", \'" . a:1 . "\'" : '' )
\ . ")<cr>"
endfunction

function! magit#mapping#set_default()

execute "nnoremap <buffer><silent><nowait> " . g:magit_stage_file_mapping . " :call magit#stage_file()<cr>"
execute "nnoremap <buffer><silent><nowait> " . g:magit_stage_hunk_mapping . " :call magit#stage_hunk(0)<cr>"
execute "nnoremap <buffer><silent><nowait> " . g:magit_discard_hunk_mapping . " :call magit#stage_hunk(1)<cr>"
execute "nnoremap <buffer><silent><nowait> " . g:magit_reload_mapping . " :call magit#update_buffer()<cr>"
execute "nnoremap <buffer><silent><nowait> " . g:magit_edit_mapping . " :call magit#jump_to()<cr>"
execute "nnoremap <buffer><silent><nowait> " . g:magit_commit_mapping . " :call magit#commit_command('CC')<cr>"
execute "nnoremap <buffer><silent><nowait> " . g:magit_commit_amend_mapping . " :call magit#commit_command('CA')<cr>"
execute "nnoremap <buffer><silent><nowait> " . g:magit_commit_fixup_mapping . " :call magit#commit_command('CF')<cr>"
execute "nnoremap <buffer><silent><nowait> " . g:magit_close_commit_mapping . " :call magit#close_commit()<cr>"
execute "nnoremap <buffer><silent><nowait> " . g:magit_ignore_mapping . " :call magit#ignore_file()<cr>"
execute "nnoremap <buffer><silent><nowait> " . g:magit_close_mapping . " :call magit#close_magit()<cr>"
execute "nnoremap <buffer><silent><nowait> " . g:magit_diff_shrink . " :call magit#update_diff('-')<cr>"
execute "nnoremap <buffer><silent><nowait> " . g:magit_diff_enlarge . " :call magit#update_diff('+')<cr>"
execute "nnoremap <buffer><silent><nowait> " . g:magit_diff_reset . " :call magit#update_diff('0')<cr>"
execute "nnoremap <buffer><silent><nowait> " . g:magit_toggle_help_mapping . " :call magit#toggle_help()<cr>"

execute "nnoremap <buffer><silent><nowait> " . g:magit_stage_line_mapping . " :call magit#stage_vselect()<cr>"
execute "xnoremap <buffer><silent><nowait> " . g:magit_stage_hunk_mapping . " :call magit#stage_vselect()<cr>"

execute "nnoremap <buffer><silent><nowait> " . g:magit_mark_line_mapping . " :call magit#mark_vselect()<cr>"
execute "xnoremap <buffer><silent><nowait> " . g:magit_mark_line_mapping . " :call magit#mark_vselect()<cr>"

execute "nnoremap <buffer><silent><nowait> " . g:magit_jump_next_hunk . " :call magit#jump_hunk('N')<cr>"
execute "nnoremap <buffer><silent><nowait> " . g:magit_jump_prev_hunk . " :call magit#jump_hunk('P')<cr>"
call s:mg_set_mapping('n', g:magit_stage_hunk_mapping,
\"magit#stage_hunk(0)", '\<\%(un\)\?staged\>')
call s:mg_set_mapping('n', g:magit_stage_file_mapping,
\ "magit#stage_file()", '\<\%(un\)\?staged\>')
call s:mg_set_mapping('n', g:magit_discard_hunk_mapping,
\ "magit#stage_hunk(1)", '\<\%(un\)\?staged\>')
call s:mg_set_mapping('n', g:magit_stage_line_mapping,
\ "magit#stage_vselect()", '\<\%(un\)\?staged\>')
call s:mg_set_mapping('x', g:magit_stage_hunk_mapping,
\ "magit#stage_vselect()", '\<\%(un\)\?staged\>')
call s:mg_set_mapping('n', g:magit_mark_line_mapping,
\ "magit#mark_vselect()", '\<\%(un\)\?staged\>')
call s:mg_set_mapping('x', g:magit_mark_line_mapping,
\ "magit#mark_vselect()", '\<\%(un\)\?staged\>')

call s:mg_set_mapping('n', g:magit_ignore_mapping,
\ "magit#ignore_file()", '\<\%(un\)\?staged\>')
call s:mg_set_mapping('n', g:magit_edit_mapping,
\ "magit#jump_to()", '\<\%(un\)\?staged\>')

call s:mg_set_mapping('n', g:magit_reload_mapping,
\ "magit#update_buffer()")
call s:mg_set_mapping('n', g:magit_close_mapping,
\ "magit#close_magit()")
call s:mg_set_mapping('n', g:magit_diff_shrink,
\ "magit#update_diff('-')")
call s:mg_set_mapping('n', g:magit_diff_enlarge,
\ "magit#update_diff('+')")
call s:mg_set_mapping('n', g:magit_diff_reset,
\ "magit#update_diff('0')")
call s:mg_set_mapping('n', g:magit_toggle_help_mapping,
\ "magit#toggle_help()")

call s:mg_set_mapping('n', g:magit_commit_mapping,
\ "magit#commit_command('CC')")
call s:mg_set_mapping('n', g:magit_commit_amend_mapping,
\ "magit#commit_command('CA')")
call s:mg_set_mapping('n', g:magit_commit_fixup_mapping,
\ "magit#commit_command('CF')")
call s:mg_set_mapping('n', g:magit_close_commit_mapping,
\ "magit#close_commit()")

call s:mg_set_mapping('n', g:magit_jump_next_hunk,
\ "magit#jump_hunk('N')")
call s:mg_set_mapping('n', g:magit_jump_prev_hunk,
\ "magit#jump_hunk('P')")

for mapping in g:magit_folding_toggle_mapping
" trick to pass '<cr>' in a mapping command without being interpreted
let func_arg = ( mapping ==? "<cr>" ) ? '+' : mapping
Expand Down

0 comments on commit 554e10a

Please sign in to comment.