From dffa56c4ea24d67f330546c18b5b83aaf9ca329f Mon Sep 17 00:00:00 2001 From: Tim Pope Date: Fri, 2 Aug 2019 23:40:13 -0400 Subject: [PATCH] Retool section jump maps --- autoload/fugitive.vim | 94 ++++++++++++++++++++++++++++++++++++------- doc/fugitive.txt | 22 ++++++---- 2 files changed, 93 insertions(+), 23 deletions(-) diff --git a/autoload/fugitive.vim b/autoload/fugitive.vim index 28bd9dc..feb730d 100644 --- a/autoload/fugitive.vim +++ b/autoload/fugitive.vim @@ -2236,7 +2236,18 @@ function! s:StageJump(offset, section, ...) abort let line = search('^' . a:section, 'nw') if line exe line - return s:NextItem(a:offset ? a:offset : 1) + if a:offset + for i in range(a:offset) + call search(s:file_pattern . '\|^$') + if empty(line('.')) + return '' + endif + endfor + call s:StageReveal() + else + call s:StageReveal() + + + endif endif return '' endfunction @@ -2627,15 +2638,21 @@ function! s:StageReveal(...) abort while end < line('$') && getline(end + 1) !~# '^commit ' let end += 1 endwhile + elseif getline(begin) =~# s:section_pattern + let end = begin + while len(getline(end + 1)) + let end += 1 + endwhile endif if exists('end') - while line('w$') < line('$') && end > line('w$') && line('.') > line('w0') + &scrolloff + while line('.') > line('w0') + &scrolloff && end > line('w$') execute "normal! \" endwhile endif endfunction -let s:item_pattern = '^[A-Z?] .\|^diff --\|^\%(\l\{3,\} \)\=[0-9a-f]\{4,\} \|^@' +let s:file_pattern = '^[A-Z?] .\|^diff --' +let s:item_pattern = s:file_pattern . '\|^\%(\l\{3,\} \)\=[0-9a-f]\{4,\} \|^@@' function! s:NextItem(count) abort for i in range(a:count) @@ -2657,27 +2674,72 @@ function! s:PreviousItem(count) abort return '.' endfunction +let s:section_pattern = '^[A-Z][a-z][^:]*$' +let s:section_commit_pattern = s:section_pattern . '\|^commit ' + function! s:NextSection(count) abort + let orig = line('.') + if getline('.') !~# '^commit ' + - + endif for i in range(a:count) - if !search('^[A-Z][a-z][^:]*$','W') - return '.' + if !search(s:section_commit_pattern, 'W') + break endif - + endfor - call s:StageReveal() - return '.' + if getline('.') =~# s:section_commit_pattern + call s:StageReveal() + return getline('.') =~# s:section_pattern ? '+' : ':' + else + return orig + endif endfunction function! s:PreviousSection(count) abort - - + let orig = line('.') + if getline('.') !~# '^commit ' + - + endif for i in range(a:count) - if !search('^[A-Z][a-z][^:]*$\|\%^','bW') || line('.') == 1 - return '.' + if !search(s:section_commit_pattern . '\|\%^', 'bW') + break endif - + endfor - call s:StageReveal() - return '.' + if getline('.') =~# s:section_commit_pattern || line('.') == 1 + call s:StageReveal() + return getline('.') =~# s:section_pattern ? '+' : ':' + else + return orig + endif +endfunction + +function! s:NextSectionEnd(count) abort + + + if empty(getline('.')) + + + endif + for i in range(a:count) + if !search(s:section_commit_pattern, 'W') + return '$' + endif + endfor + return search('^.', 'Wb') +endfunction + +function! s:PreviousSectionEnd(count) abort + let old = line('.') + for i in range(a:count) + if search(s:section_commit_pattern, 'Wb') <= 1 + exe old + if i + break + else + return '' + endif + endif + let old = line('.') + endfor + return search('^.', 'Wb') endfunction function! s:StageInline(mode, ...) abort @@ -2756,7 +2818,7 @@ endfunction function! s:NextExpandedHunk(count) abort for i in range(a:count) call s:StageInline('show', line('.'), 1) - call search('^[A-Z?] .\|^diff --\|^@','W') + call search(s:file_pattern . '\|^@','W') endfor return '.' endfunction @@ -4959,6 +5021,8 @@ function! fugitive#MapJumps(...) abort call s:MapEx('J', 'exe NextItem(v:count1)') call s:MapEx('[[', 'exe PreviousSection(v:count1)') call s:MapEx(']]', 'exe NextSection(v:count1)') + call s:MapEx('[]', 'exe PreviousSectionEnd(v:count1)') + call s:MapEx('][', 'exe NextSectionEnd(v:count1)') endif exe "nnoremap " s:nowait "- :exe 'Gedit ' . fnameescape(NavigateUp(v:count1)) if getline(1) =~# '^tree \x\{40,\}$' && empty(getline(2))call search('^'.escape(expand('#:t'),'.*[]~\').'/\=$','wc')endif" nnoremap P :exe 'Gedit ' . fnameescape(ContainingCommit().'^'.v:count1.Relative(':')) diff --git a/doc/fugitive.txt b/doc/fugitive.txt index ee1ad20..fbc9371 100644 --- a/doc/fugitive.txt +++ b/doc/fugitive.txt @@ -343,28 +343,34 @@ i Jump to the next file or hunk, expanding inline diffs automatically. *fugitive_[[* -[[ Jump to the previous section. +[[ Jump [count] sections backward. *fugitive_]]* -]] Jump to the next section. +]] Jump [count] sections forward. + + *fugitive_[]* +[] Jump [count] section ends backward. + + *fugitive_][* +][ Jump [count] section ends forward. *fugitive_gu* -gu Jump to "Unstaged" section. +gu Jump to file [count] in the "Unstaged" section. *fugitive_gU* -gU Jump to "Untracked" section. +gU Jump to file [count] in the "Untracked" section. *fugitive_gs* -gs Jump to "Staged" section. +gs Jump to file [count] in the "Staged" section. *fugitive_gp* -gp Jump to "Unpushed" section. +gp Jump to file [count] in the "Unpushed" section. *fugitive_gP* -gP Jump to "Unpulled" section. +gP Jump to file [count] in the "Unpulled" section. *fugitive_gr* -gr Jump to "Rebasing" section. +gr Jump to file [count] in the "Rebasing" section. *fugitive_gi* gi Open .git/info/exclude in a split. Use a count to