Skip to content
Snippets Groups Projects
Commit 77597be6 authored by Jerome Reybert's avatar Jerome Reybert
Browse files

plugin/magit.vim: add mappings N and P to jump to next and previous hunk (fix #25)

parent d25ee907
Branches
No related tags found
No related merge requests found
...@@ -64,6 +64,9 @@ Open magit buffer. ...@@ -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. 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 #### S
* Modify a file, for example foo.c, in your repository. * Modify a file, for example foo.c, in your repository.
...@@ -184,6 +187,9 @@ Following mappings are set locally, for magit buffer only, in normal mode. ...@@ -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. * If cursor is in diff header, discard whole file at cursor position.
* Only works in "Unstaged changes" section. * Only works in "Unstaged changes" section.
##### N,P
* Move to **N**ext or **P**revious hunk.
##### CC ##### CC
* If not in commit section, set commit mode to "New commit" and show "Commit message" section with brand new commit message. * 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. * If in commit section, commit the all staged changes in commit mode previously set.
......
...@@ -59,6 +59,11 @@ All files diffs are hidden by default. To inspect changes in a file, move ...@@ -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 cursor to the filename line, and press 'Enter' in Normal mode. Diffs are
displayed below the file name. displayed below the file name.
N
-----------
* Jump to next hunk with N.
S S
----------- -----------
...@@ -223,6 +228,10 @@ Following mappings are set locally, for magit buffer only, in normal mode. ...@@ -223,6 +228,10 @@ Following mappings are set locally, for magit buffer only, in normal mode.
position. position.
Only works in "Unstaged changes" section. 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-CC* *magit#commit_command('CC')*
*vimagit-g:magit_commit_mapping* *vimagit-g:magit_commit_mapping*
CC If not in commit section, set commit mode to "New commit" and show CC If not in commit section, set commit mode to "New commit" and show
......
...@@ -38,6 +38,8 @@ let g:magit_folding_toggle_mapping = get(g:, 'magit_folding_toggle_mapping', ...@@ -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_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_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 " user options
let g:magit_enabled = get(g:, 'magit_enabled', 1) let g:magit_enabled = get(g:, 'magit_enabled', 1)
let g:magit_show_help = get(g:, 'magit_show_help', 0) let g:magit_show_help = get(g:, 'magit_show_help', 0)
...@@ -666,6 +668,9 @@ function! magit#show_magit(display, ...) ...@@ -666,6 +668,9 @@ function! magit#show_magit(display, ...)
execute "nnoremap <buffer> <silent> " . g:magit_mark_line_mapping . " :call magit#mark_vselect()<cr>" 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 "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 for mapping in g:magit_folding_toggle_mapping
" trick to pass '<cr>' in a mapping command without being interpreted " trick to pass '<cr>' in a mapping command without being interpreted
let func_arg = ( mapping ==? "<cr>" ) ? '+' : mapping let func_arg = ( mapping ==? "<cr>" ) ? '+' : mapping
...@@ -918,6 +923,28 @@ function! magit#commit_command(mode) ...@@ -918,6 +923,28 @@ function! magit#commit_command(mode)
call magit#update_buffer() call magit#update_buffer()
endfunction 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! Magit call magit#show_magit('v')
command! MagitOnly call magit#show_magit('c') command! MagitOnly call magit#show_magit('c')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment