Skip to content

Commit

Permalink
plugin/magit.vim: add mappings N and P to jump to next and previous h…
Browse files Browse the repository at this point in the history
…unk (fix #25)
  • Loading branch information
jreybert committed Nov 30, 2015
1 parent d25ee90 commit 77597be
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ Open magit buffer.

All files diffs are hidden by default. To inspect changes in a file, move cursor to the filename line, and press 'Enter' in Normal mode. Diffs are displayed below the file name.

#### N
* Jump to next hunk with **N**.

#### S

* Modify a file, for example foo.c, in your repository.
Expand Down Expand Up @@ -184,6 +187,9 @@ Following mappings are set locally, for magit buffer only, in normal mode.
* If cursor is in diff header, discard whole file at cursor position.
* Only works in "Unstaged changes" section.

##### N,P
* Move to **N**ext or **P**revious hunk.

##### CC
* If not in commit section, set commit mode to "New commit" and show "Commit message" section with brand new commit message.
* If in commit section, commit the all staged changes in commit mode previously set.
Expand Down
9 changes: 9 additions & 0 deletions doc/vimagit.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ All files diffs are hidden by default. To inspect changes in a file, move
cursor to the filename line, and press 'Enter' in Normal mode. Diffs are
displayed below the file name.

N
-----------

* Jump to next hunk with N.

S
-----------

Expand Down Expand Up @@ -223,6 +228,10 @@ Following mappings are set locally, for magit buffer only, in normal mode.
position.
Only works in "Unstaged changes" section.

*vimagit-N* *magit#jump_hunk()*
*vimagit-P*
N,P Move to Next or Previous hunk.

*vimagit-CC* *magit#commit_command('CC')*
*vimagit-g:magit_commit_mapping*
CC If not in commit section, set commit mode to "New commit" and show
Expand Down
29 changes: 28 additions & 1 deletion plugin/magit.vim
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ let g:magit_folding_toggle_mapping = get(g:, 'magit_folding_toggle_mapping',
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' ])

let g:magit_jump_next_hunk = get(g:, 'magit_jump_next_hunk', 'N')
let g:magit_jump_prev_hunk = get(g:, 'magit_jump_prev_hunk', 'P')
" user options
let g:magit_enabled = get(g:, 'magit_enabled', 1)
let g:magit_show_help = get(g:, 'magit_show_help', 0)
Expand Down Expand Up @@ -665,7 +667,10 @@ function! magit#show_magit(display, ...)

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


execute "nnoremap <buffer> <silent> " . g:magit_jump_next_hunk . " :call magit#jump_hunk('N')<cr>"
execute "nnoremap <buffer> <silent> " . 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
Expand Down Expand Up @@ -918,6 +923,28 @@ function! magit#commit_command(mode)
call magit#update_buffer()
endfunction

" magit#jump_hunk: function to jump among hunks
" it closes the current fold (if any), jump to next hunk and unfold it
" param[in] dir: can be 'N' (for next) or 'P' (for previous)
function! magit#jump_hunk(dir)
let back = ( a:dir == 'P' ) ? 'b' : ''
let line = search("^@@ ", back . 'wn')
if ( line != 0 )
try
foldclose
catch /^Vim\%((\a\+)\)\=:E490/
endtry
call cursor(line, 0)
try
foldopen
catch /^Vim\%((\a\+)\)\=:E490/
echohl WarningMsg
echom "Warning: you should have jumped on a folded hunk"
echohl None
endtry
endif
endfunction

command! Magit call magit#show_magit('v')
command! MagitOnly call magit#show_magit('c')

Expand Down

0 comments on commit 77597be

Please sign in to comment.