Skip to content

Commit

Permalink
rework mappings: update jump mappings, add nowait, autoload
Browse files Browse the repository at this point in the history
  • Loading branch information
jreybert committed Feb 22, 2017
1 parent 4255d9a commit 488b619
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 158 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ To simply test vimagit, modify/add/delete/rename some files in a git repository

- `:Magit`
Open magit buffer with [:Magit](#magitshow_magit) command.
- `N`
Jump to next hunk with `N`, or move the cursor as you like. The cursor is on a hunk.
- `<C-n>`
Jump to next hunk with `<C-n>`, or move the cursor as you like. The cursor is on a hunk.
- `S`
While the cursor is on an unstaged hunk, press `S` in Normal mode: the hunk is now staged, and appears in "Staged changes" section (you can also unstage a hunk from "Staged section" with `S`).
- `CC`
Expand Down Expand Up @@ -204,7 +204,7 @@ E means 'edit'.

:exclamation: this function is extremely powerful, just give it a try!

##### N,P
##### <C-n>,<C-p>
* Move to **N**ext or **P**revious hunk.

##### CC
Expand Down Expand Up @@ -238,6 +238,12 @@ E means 'edit'.
##### ?
* Toggle help showing in magit buffer

#### Mapping update

Since vimagit 1.7, jump mappings have changed:
* Jump next hunk : **N** -> **<C-n>**
* Jump prev hunk : **P** -> **<C-p>**

#### Autocommand events

Magit will raise some events at some point. User can plug some specific
Expand Down
159 changes: 159 additions & 0 deletions autoload/magit/mapping.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@

let g:magit_stage_file_mapping = get(g:, 'magit_stage_file_mapping', 'F' )
let g:magit_stage_hunk_mapping = get(g:, 'magit_stage_hunk_mapping', 'S' )
let g:magit_stage_line_mapping = get(g:, 'magit_stage_line_mapping', 'L' )
let g:magit_mark_line_mapping = get(g:, 'magit_mark_line_mapping', 'M' )
let g:magit_commit_mapping = get(g:, 'magit_commit_mapping', 'CC' )
let g:magit_commit_amend_mapping = get(g:, 'magit_commit_amend_mapping', 'CA' )
let g:magit_commit_fixup_mapping = get(g:, 'magit_commit_fixup_mapping', 'CF' )
let g:magit_close_commit_mapping = get(g:, 'magit_close_commit_mapping', 'CU' )
let g:magit_reload_mapping = get(g:, 'magit_reload_mapping', 'R' )
let g:magit_edit_mapping = get(g:, 'magit_edit_mapping', 'E' )

let g:magit_jump_next_hunk = get(g:, 'magit_jump_next_hunk', '<C-N>')
let g:magit_jump_prev_hunk = get(g:, 'magit_jump_prev_hunk', '<C-P>')

let g:magit_ignore_mapping = get(g:, 'magit_ignore_mapping', 'I' )
let g:magit_discard_hunk_mapping = get(g:, 'magit_discard_hunk_mapping', 'DDD' )

let g:magit_close_mapping = get(g:, 'magit_close_mapping', 'q' )
let g:magit_toggle_help_mapping = get(g:, 'magit_toggle_help_mapping', '?' )

let g:magit_diff_shrink = get(g:, 'magit_diff_shrink', '-' )
let g:magit_diff_enlarge = get(g:, 'magit_diff_enlarge', '+' )
let g:magit_diff_reset = get(g:, 'magit_diff_reset', '0' )

let g:magit_folding_toggle_mapping = get(g:, 'magit_folding_toggle_mapping', [ '<CR>' ])
let g:magit_folding_open_mapping = get(g:, 'magit_folding_open_mapping', [ 'zo', 'zO' ])
let g:magit_folding_close_mapping = get(g:, 'magit_folding_close_mapping', [ 'zc', 'zC' ])

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>"
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
execute "nnoremap <buffer><silent><nowait> " . mapping . " :call magit#open_close_folding_wrapper('" . func_arg . "')<return>"
endfor
for mapping in g:magit_folding_open_mapping
execute "nnoremap <buffer><silent><nowait> " . mapping . " :call magit#open_close_folding_wrapper('" . mapping . "', 1)<return>"
endfor
for mapping in g:magit_folding_close_mapping
execute "nnoremap <buffer><silent><nowait> " . mapping . " :call magit#open_close_folding_wrapper('" . mapping . "', 0)<return>"
endfor

" s:magit_inline_help: Dict containing inline help for each section
let s:magit_inline_help = {
\ 'staged': [
\g:magit_stage_hunk_mapping
\.' if cursor on filename header, unstage file',
\' if cursor in hunk, unstage hunk',
\' if visual selection in hunk (with v), unstage selection',
\' if lines marked in hunk (with ' . g:magit_mark_line_mapping . '), unstage marked lines',
\g:magit_stage_line_mapping
\.' unstage the line under the cursor',
\g:magit_mark_line_mapping
\.' if cursor in hunk, mark line under cursor "to be unstaged"',
\' if visual selection in hunk (with v), mark selected lines "to be unstaged"',
\g:magit_stage_file_mapping
\.' if cursor on filename header or hunk, unstage whole file',
\g:magit_edit_mapping
\.' edit, jump cursor to file containing this hunk',
\g:magit_jump_next_hunk.','.g:magit_jump_prev_hunk
\. ' move to Next/Previous hunk in magit buffer',
\],
\ 'unstaged': [
\g:magit_stage_hunk_mapping
\.' if cursor on filename header, stage file',
\' if cursor in hunk, stage hunk',
\' if visual selection in hunk (with v), stage selection',
\' if lines marked in hunk (with ' . g:magit_mark_line_mapping . '), stage marked lines',
\g:magit_stage_line_mapping
\.' stage the line under the cursor',
\g:magit_mark_line_mapping
\.' if cursor in hunk, mark line under cursor "to be staged"',
\' if visual selection in hunk (with v), mark selected lines "to be staged"',
\g:magit_stage_file_mapping
\.' if cursor on filename header or hunk, stage whole file',
\g:magit_edit_mapping
\.' edit, jump cursor to file containing this hunk',
\g:magit_jump_next_hunk.','.g:magit_jump_prev_hunk
\. ' move to Next/Previous hunk in magit buffer',
\g:magit_discard_hunk_mapping
\. ' discard file changes (warning, changes will be lost)',
\g:magit_ignore_mapping
\.' add file in .gitgnore',
\],
\ 'global': [
\g:magit_folding_toggle_mapping[0]
\. ' if cursor on filename header line, unhide diffs for this file',
\g:magit_commit_mapping
\. ' set commit mode to normal, and show "Commit message" section',
\g:magit_commit_amend_mapping
\. ' set commit mode amend, and show "Commit message" section with previous',
\' commit message',
\g:magit_commit_fixup_mapping
\. ' amend staged changes to previous commit without modifying the previous',
\' commit message',
\g:magit_close_commit_mapping
\. ' commit undo, cancel and close current commit message',
\g:magit_reload_mapping
\.' refresh magit buffer',
\g:magit_diff_shrink.','.g:magit_diff_enlarge.','.g:magit_diff_reset
\. ' shrink,enlarge,reset diff context',
\g:magit_close_mapping
\.' close magit buffer',
\g:magit_toggle_help_mapping
\.' toggle help showing in magit buffer',
\],
\ 'commit': [
\g:magit_commit_mapping
\. ' commit all staged changes with commit mode previously set (normal or',
\' amend) with message written in this section',
\],
\}
endfunction

" s:mg_get_inline_help_line_nb: this function returns the number of lines of
" a given section, or 0 if help is disabled.
" param[in] section: section identifier
" return number of lines
function! magit#mapping#get_section_help_line_nb(section)
return ( g:magit_show_help == 1 ) ?
\ len(s:magit_inline_help[a:section]) : 0
endfunction

" s:mg_section_help: this function writes in current buffer the inline help
" for a given section, it does nothing if inline help is disabled.
" WARNING: this function writes in file, it should only be called through
" protected functions like magit#update_buffer
" param[in] section: section identifier
function! magit#mapping#get_section_help(section)
if ( g:magit_show_help == 1 )
silent put =s:magit_inline_help[a:section]
endif
endfunction

19 changes: 13 additions & 6 deletions doc/vimagit.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ repository and open vim.
> :Magit
Open magit buffer with :Magit command.

> N
Jump to next hunk with N. The cursor should be on the header of a hunk.
> <C-n>
Jump to next hunk with <C-n>. The cursor should be on the header of a hunk.

> S
If the hunk is in "Unstage changes" section, press S in Normal mode: the
Expand Down Expand Up @@ -145,7 +145,14 @@ variable is described below as *vimagit-g:magit_nameofmapping_mapping*

For example, to redefine the <leader>M mapping, user should add this line in
its |vimrc|:
let g:magit_show_magit_mapping='itsnewmapping'
let g:magit_show_magit_mapping='m'

Mapping update *vimagit-mapping-update*
--------------

Since vimagit 1.7, jump mappings have changed:
Jump next hunk : N -> <C-n>
Jump prev hunk : P -> <C-p>

Global mappings
---------------
Expand Down Expand Up @@ -226,11 +233,11 @@ Following mappings are set locally, for magit buffer only, in normal mode.
E means 'edit'.
NOTE: this function is extremely powerful, just give it a try!

*vimagit-N* *magit#jump_hunk()*
*vimagit-P*
*vimagit-<C-n>* *magit#jump_hunk()*
*vimagit-<C-p>*
*vimagit-g:magit_jump_next_hunk*
*vimagit-g:magit_jump_prev_hunk*
N,P Move to Next or Previous hunk.
<C-n>,<C-p> Move to Next or Previous hunk.

*vimagit-CC* *magit#commit_command('CC')*
*vimagit-g:magit_commit_mapping*
Expand Down
Loading

0 comments on commit 488b619

Please sign in to comment.