From 77597be6850581d2d71fc51ce73960b9fb46b2c0 Mon Sep 17 00:00:00 2001 From: Jerome Reybert <jreybert@gmail.com> Date: Mon, 30 Nov 2015 22:19:57 +0100 Subject: [PATCH] plugin/magit.vim: add mappings N and P to jump to next and previous hunk (fix #25) --- README.md | 6 ++++++ doc/vimagit.txt | 9 +++++++++ plugin/magit.vim | 29 ++++++++++++++++++++++++++++- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index dfdb400..9fff9a9 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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. diff --git a/doc/vimagit.txt b/doc/vimagit.txt index 5d8d65f..fb6576d 100644 --- a/doc/vimagit.txt +++ b/doc/vimagit.txt @@ -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 ----------- @@ -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 diff --git a/plugin/magit.vim b/plugin/magit.vim index 96595be..9c2697b 100644 --- a/plugin/magit.vim +++ b/plugin/magit.vim @@ -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) @@ -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 @@ -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') -- GitLab